1 /******************************************************************************
2 *
3 * Copyright 2020 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 #include <base/strings/string_number_conversions.h>
22 #include <bluetooth/log.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25
26 #include <memory>
27
28 #include "hci/le_security_interface.h"
29 #include "os/log.h"
30 #include "security/ecc/p_256_ecc_pp.h"
31 #include "security/test/mocks.h"
32
33 using namespace std::chrono_literals;
34
35 namespace bluetooth {
36 namespace security {
37
38 class EcdhKeysTest : public testing::Test {
39 protected:
SetUp()40 void SetUp() {}
41
TearDown()42 void TearDown() {}
43
44 public:
45 };
46
47 /* This test generates two pairs of keys, computes the Diffie–Hellman key using both, and verifies that they match */
TEST_F(EcdhKeysTest,test_generated)48 TEST_F(EcdhKeysTest, test_generated) {
49 std::srand(std::time(nullptr));
50
51 auto [private_key_a, public_key_a] = GenerateECDHKeyPair();
52 auto [private_key_b, public_key_b] = GenerateECDHKeyPair();
53
54 std::array<uint8_t, 32> dhkeya = ComputeDHKey(private_key_a, public_key_b);
55 std::array<uint8_t, 32> dhkeyb = ComputeDHKey(private_key_b, public_key_a);
56
57 EXPECT_EQ(dhkeya, dhkeyb);
58
59 if (dhkeya != dhkeyb) {
60 log::error("private key a : {}", base::HexEncode(private_key_a.data(), private_key_a.size()));
61 log::error(
62 "public key a.x : {}", base::HexEncode(public_key_a.x.data(), public_key_a.x.size()));
63 log::error(
64 "public key a.y : {}", base::HexEncode(public_key_a.y.data(), public_key_a.y.size()));
65
66 log::error("private key b : {}", base::HexEncode(private_key_b.data(), private_key_b.size()));
67 log::error(
68 "public key b.x : {}", base::HexEncode(public_key_b.x.data(), public_key_b.x.size()));
69 log::error(
70 "public key b.y : {}", base::HexEncode(public_key_b.y.data(), public_key_b.y.size()));
71
72 log::error("dhkeya : {}", base::HexEncode(dhkeya.data(), dhkeya.size()));
73 log::error("dhkeyb : {}", base::HexEncode(dhkeyb.data(), dhkeyb.size()));
74 }
75 }
76
77 /* This test uses two fixed pairs of keys, computes the Diffie–Hellman key using both, and verifies that they match
78 precomputed value.
79
80 This code is also useful during debuging - one can replace fixed values with values from failed exchagne to verify which
81 side did bad computation. */
TEST_F(EcdhKeysTest,test_static)82 TEST_F(EcdhKeysTest, test_static) {
83 std::array<uint8_t, 32> private_key_a = {0x3E, 0xC8, 0x2A, 0x32, 0xB3, 0x75, 0x76, 0xBA, 0x7D, 0xB8, 0xB4,
84 0x7B, 0xA0, 0x8A, 0xA3, 0xC3, 0xF2, 0x03, 0x1A, 0x53, 0xF6, 0x52,
85 0x26, 0x32, 0xB6, 0xAE, 0x57, 0x3F, 0x13, 0x15, 0x29, 0x51};
86 bluetooth::security::EcdhPublicKey public_key_a;
87 uint8_t ax[32] = {0xDC, 0x88, 0xD0, 0xE5, 0x59, 0x73, 0xF2, 0x41, 0x88, 0x6C, 0xB4, 0x45, 0x8B, 0x61, 0x3B, 0x10,
88 0xF5, 0xD4, 0xD2, 0x5B, 0x4E, 0xA1, 0x7F, 0x94, 0xE3, 0xA9, 0x38, 0xF8, 0x84, 0xD4, 0x98, 0x10};
89 uint8_t ay[32] = {0x3D, 0x13, 0x76, 0x4F, 0xD1, 0x29, 0x6E, 0xEC, 0x8D, 0xF6, 0x70, 0x33, 0x8B, 0xA7, 0x18, 0xEA,
90 0x84, 0x15, 0xE8, 0x8C, 0x4A, 0xC8, 0x76, 0x45, 0x90, 0x98, 0xBA, 0x52, 0x8B, 0x00, 0x69, 0xAF};
91 memcpy(public_key_a.x.data(), ax, 32);
92 memcpy(public_key_a.y.data(), ay, 32);
93
94 std::array<uint8_t, 32> private_key_b = {0xDD, 0x53, 0x84, 0x91, 0xC8, 0xFA, 0x4B, 0x45, 0xB2, 0xFF, 0xC0,
95 0x53, 0x89, 0x64, 0x16, 0x7B, 0x67, 0x30, 0xCE, 0x5D, 0x82, 0xF4,
96 0x8F, 0x38, 0xA2, 0xE6, 0x78, 0xB6, 0xFB, 0xA1, 0x07, 0xD8};
97 bluetooth::security::EcdhPublicKey public_key_b;
98 uint8_t bx[32] = {0x23, 0x1A, 0xEC, 0xFE, 0x7D, 0xC1, 0x20, 0x2F, 0x03, 0x3E, 0x9A, 0xAA, 0x99, 0x55, 0x78, 0x86,
99 0x58, 0xCB, 0x37, 0x68, 0x7D, 0xE1, 0xFF, 0x19, 0x33, 0xF8, 0xCB, 0x7A, 0x17, 0xAB, 0x0B, 0x73};
100 uint8_t by[32] = {0x4C, 0x25, 0xE2, 0x42, 0x3C, 0x69, 0x0E, 0x3B, 0xC0, 0xEF, 0x94, 0x09, 0x4D, 0x3F, 0x96, 0xBB,
101 0x18, 0xF2, 0x55, 0x81, 0x71, 0x5A, 0xDE, 0xC4, 0x3E, 0xF9, 0x6F, 0xA9, 0xAF, 0x04, 0x4E, 0x86};
102 memcpy(public_key_b.x.data(), bx, 32);
103 memcpy(public_key_b.y.data(), by, 32);
104
105 std::array<uint8_t, 32> dhkey;
106 uint8_t dhkey_val[32] = {0x3B, 0xF8, 0xDF, 0x33, 0x99, 0x94, 0x66, 0x55, 0x4F, 0x2C, 0x4A,
107 0x78, 0x2B, 0x51, 0xD1, 0x49, 0x0F, 0xF1, 0x96, 0x63, 0x51, 0x75,
108 0x9E, 0x65, 0x7F, 0x3C, 0xFE, 0x77, 0xB4, 0x3F, 0x7A, 0x93};
109 memcpy(dhkey.data(), dhkey_val, 32);
110
111 std::array<uint8_t, 32> dhkey_a = ComputeDHKey(private_key_a, public_key_b);
112 std::array<uint8_t, 32> dhkey_b = ComputeDHKey(private_key_b, public_key_a);
113
114 EXPECT_EQ(dhkey_a, dhkey);
115 EXPECT_EQ(dhkey_b, dhkey);
116 }
117
118 } // namespace security
119 } // namespace bluetooth
120