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