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
19 #include "security/ecdh_keys.h"
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
28 #include "security/ecc/p_256_ecc_pp.h"
29
30 namespace {
31
32 static bool srand_initiated = false;
33
34 template <size_t SIZE>
GenerateRandom()35 static std::array<uint8_t, SIZE> GenerateRandom() {
36 if (!srand_initiated) {
37 srand_initiated = true;
38 // TODO: We need a proper random number generator here.
39 // use current time as seed for random generator
40 std::srand(std::time(nullptr));
41 }
42
43 std::array<uint8_t, SIZE> r;
44 for (size_t i = 0; i < SIZE; i++) r[i] = std::rand();
45 return r;
46 }
47 } // namespace
48 /*********************************************************************************************************************/
49
50 namespace bluetooth {
51 namespace security {
52
GenerateECDHKeyPair()53 std::pair<std::array<uint8_t, 32>, EcdhPublicKey> GenerateECDHKeyPair() {
54 std::array<uint8_t, 32> private_key = GenerateRandom<32>();
55 std::array<uint8_t, 32> private_key_copy = private_key;
56 ecc::Point public_key;
57
58 ECC_PointMult(&public_key, &(ecc::curve_p256.G), (uint32_t*)private_key_copy.data());
59
60 EcdhPublicKey pk;
61 memcpy(pk.x.data(), public_key.x, 32);
62 memcpy(pk.y.data(), public_key.y, 32);
63
64 /* private_key, public key pair */
65 return std::make_pair<std::array<uint8_t, 32>, EcdhPublicKey>(std::move(private_key), std::move(pk));
66 }
67
ValidateECDHPoint(EcdhPublicKey pk)68 bool ValidateECDHPoint(EcdhPublicKey pk) {
69 ecc::Point public_key;
70 memcpy(public_key.x, pk.x.data(), 32);
71 memcpy(public_key.y, pk.y.data(), 32);
72 memset(public_key.z, 0, 32);
73 return ECC_ValidatePoint(public_key);
74 }
75
ComputeDHKey(std::array<uint8_t,32> my_private_key,EcdhPublicKey remote_public_key)76 std::array<uint8_t, 32> ComputeDHKey(std::array<uint8_t, 32> my_private_key, EcdhPublicKey remote_public_key) {
77 ecc::Point peer_publ_key, new_publ_key;
78 uint32_t private_key[8];
79 memcpy(private_key, my_private_key.data(), 32);
80 memcpy(peer_publ_key.x, remote_public_key.x.data(), 32);
81 memcpy(peer_publ_key.y, remote_public_key.y.data(), 32);
82 memset(peer_publ_key.z, 0, 32);
83 peer_publ_key.z[0] = 1;
84
85 ECC_PointMult(&new_publ_key, &peer_publ_key, (uint32_t*)private_key);
86
87 std::array<uint8_t, 32> dhkey;
88 memcpy(dhkey.data(), new_publ_key.x, 32);
89 return dhkey;
90 }
91
92 } // namespace security
93 } // namespace bluetooth
94