Jakub Pawlowski | 72c8dcc | 2019-09-06 16:33:21 +0200 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright 2019 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
Jakub Pawlowski | e79714e | 2019-10-14 14:49:55 +0200 | [diff] [blame] | 19 | #include "security/ecdh_keys.h" |
Jakub Pawlowski | 72c8dcc | 2019-09-06 16:33:21 +0200 | [diff] [blame] | 20 | |
| 21 | /********************************************************************************************************************** |
| 22 | TODO: We should have random number management in separate file, and we |
| 23 | should honour all the random number requirements from the spec!! |
| 24 | **********************************************************************************************************************/ |
| 25 | #include <chrono> |
| 26 | #include <cstdlib> |
| 27 | |
Jakub Pawlowski | e79714e | 2019-10-14 14:49:55 +0200 | [diff] [blame] | 28 | #include "security/ecc/p_256_ecc_pp.h" |
Jakub Pawlowski | 72c8dcc | 2019-09-06 16:33:21 +0200 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | template <size_t SIZE> |
| 32 | static std::array<uint8_t, SIZE> GenerateRandom() { |
| 33 | // TODO: We need a proper random number generator here. |
| 34 | // use current time as seed for random generator |
| 35 | std::srand(std::time(nullptr)); |
| 36 | std::array<uint8_t, SIZE> r; |
| 37 | for (size_t i = 0; i < SIZE; i++) r[i] = std::rand(); |
| 38 | return r; |
| 39 | } |
| 40 | } // namespace |
| 41 | /*********************************************************************************************************************/ |
| 42 | |
| 43 | namespace bluetooth { |
Jakub Pawlowski | e79714e | 2019-10-14 14:49:55 +0200 | [diff] [blame] | 44 | namespace security { |
Jakub Pawlowski | 72c8dcc | 2019-09-06 16:33:21 +0200 | [diff] [blame] | 45 | |
| 46 | std::pair<std::array<uint8_t, 32>, EcdhPublicKey> GenerateECDHKeyPair() { |
| 47 | std::array<uint8_t, 32> private_key = GenerateRandom<32>(); |
| 48 | ecc::Point public_key; |
| 49 | |
| 50 | ECC_PointMult(&public_key, &(ecc::curve_p256.G), (uint32_t*)private_key.data()); |
| 51 | |
| 52 | EcdhPublicKey pk; |
| 53 | memcpy(pk.x.data(), public_key.x, 32); |
| 54 | memcpy(pk.y.data(), public_key.y, 32); |
| 55 | |
| 56 | /* private_key, public key pair */ |
| 57 | return std::make_pair<std::array<uint8_t, 32>, EcdhPublicKey>(std::move(private_key), std::move(pk)); |
| 58 | } |
| 59 | |
| 60 | bool ValidateECDHPoint(EcdhPublicKey pk) { |
| 61 | ecc::Point public_key; |
| 62 | memcpy(public_key.x, pk.x.data(), 32); |
| 63 | memcpy(public_key.y, pk.y.data(), 32); |
| 64 | memset(public_key.z, 0, 32); |
| 65 | return ECC_ValidatePoint(public_key); |
| 66 | } |
| 67 | |
| 68 | std::array<uint8_t, 32> ComputeDHKey(std::array<uint8_t, 32> my_private_key, EcdhPublicKey remote_public_key) { |
| 69 | ecc::Point peer_publ_key, new_publ_key; |
| 70 | uint32_t private_key[8]; |
| 71 | memcpy(private_key, my_private_key.data(), 32); |
| 72 | memcpy(peer_publ_key.x, remote_public_key.x.data(), 32); |
| 73 | memcpy(peer_publ_key.y, remote_public_key.y.data(), 32); |
| 74 | ECC_PointMult(&new_publ_key, &peer_publ_key, (uint32_t*)private_key); |
| 75 | |
| 76 | std::array<uint8_t, 32> dhkey; |
| 77 | memcpy(dhkey.data(), new_publ_key.x, 32); |
| 78 | return dhkey; |
| 79 | } |
| 80 | |
Jakub Pawlowski | e79714e | 2019-10-14 14:49:55 +0200 | [diff] [blame] | 81 | } // namespace security |
Jakub Pawlowski | 72c8dcc | 2019-09-06 16:33:21 +0200 | [diff] [blame] | 82 | } // namespace bluetooth |