1 /******************************************************************************
2 *
3 * Copyright 2016 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 #include <stdarg.h>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "bt_trace.h"
24 #include "hcidefs.h"
25 #include "stack/include/smp_api.h"
26 #include "stack/smp/p_256_ecc_pp.h"
27 #include "stack/smp/smp_int.h"
28 #include "types/hci_role.h"
29
30 /*
31 * This test verifies various key distribution methods in SMP works using the
32 * following parameter set:
33 *
34 * When testing target as Central (Initiator is local, Responder is remote)
35 *
36 * Initiator's Pairing Request: 0x070710000001(01)
37 * Responder's Pairing Response: 0x050008000003(02)
38 * Initiator's Bluetooth Address: 0xA1A2A3A4A5A6
39 * Initiator's Bluetooth Address Type: 0x01
40 * Responder's Bluetooth Address: 0xB1B2B3B4B5B6
41 * Responder's Bluetooth Address Type: 0x00
42 * Initiator's Random Number: 0x5783D52156AD6F0E6388274EC6702EE0
43 * TK Encryption Key: 0x0
44 *
45 * Correct values:
46 *
47 * p1: 0x05000800000302070710000001010001
48 * p1 XOR r: 0x5283dd2156ae6d096498274ec7712ee1
49 * p1 prime: 0x02c7aa2a9857ac866ff91232df0e3c95
50 * p2: 0x00000000a1a2a3a4a5a6b1b2b3b4b5b6
51 * MConfirm (c1): 0x1e1e3fef878988ead2a74dc5bef13b86
52 *
53 * NOTE: All these values are presented in mathematical reasonable canonical
54 * form that has MSB on the left and LSB on the right. In Bluetooth packets,
55 * they are mostly reversed to be Little Endian which have LSB on the left and
56 * MSB on the right.
57 */
58
59 // Set remote bda to 0xB1B2B3B4B5B6
BTM_ReadRemoteConnectionAddr(const RawAddress & pseudo_addr,RawAddress & conn_addr,tBLE_ADDR_TYPE * p_addr_type)60 bool BTM_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr,
61 RawAddress& conn_addr,
62 tBLE_ADDR_TYPE* p_addr_type) {
63 conn_addr = RawAddress({0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6});
64 *p_addr_type = 0x00;
65 return true;
66 }
67
68 // Set local_bda to 0xA1A2A3A4A5A6
BTM_ReadConnectionAddr(const RawAddress & remote_bda,RawAddress & local_conn_addr,tBLE_ADDR_TYPE * p_addr_type)69 void BTM_ReadConnectionAddr(const RawAddress& remote_bda,
70 RawAddress& local_conn_addr,
71 tBLE_ADDR_TYPE* p_addr_type) {
72 local_conn_addr = RawAddress({0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6});
73 *p_addr_type = 0x01;
74 }
75
76 // Require bte_logmsg.cc to run, here is just to fake it as we don't care about
77 // trace in unit test
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)78 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {
79 va_list args;
80 va_start(args, fmt_str);
81 vprintf(fmt_str, args);
82 va_end(args);
83 }
84
85 extern Octet16 smp_gen_p1_4_confirm(tSMP_CB* p_cb,
86 tBLE_ADDR_TYPE remote_bd_addr_type);
87
88 extern Octet16 smp_gen_p2_4_confirm(tSMP_CB* p_cb,
89 const RawAddress& remote_bda);
90
91 extern tSMP_STATUS smp_calculate_comfirm(tSMP_CB* p_cb, const Octet16& rand,
92 Octet16* output);
93
94 namespace testing {
95
dump_uint128(const Octet16 & a,char * buffer)96 void dump_uint128(const Octet16& a, char* buffer) {
97 for (unsigned int i = 0; i < OCTET16_LEN; ++i) {
98 snprintf(buffer, 3, "%02x", a[i]);
99 buffer += 2;
100 }
101 *buffer = '\0';
102 }
103
dump_uint128_reverse(const Octet16 & a,char * buffer)104 void dump_uint128_reverse(const Octet16& a, char* buffer) {
105 for (int i = (int)(OCTET16_LEN - 1); i >= 0; --i) {
106 snprintf(buffer, 3, "%02x", a[i]);
107 buffer += 2;
108 }
109 *buffer = '\0';
110 }
111
print_uint128(const Octet16 & a)112 void print_uint128(const Octet16& a) {
113 for (unsigned int i = 0; i < OCTET16_LEN; ++i) {
114 printf("%02x", a[i]);
115 }
116 printf("\n");
117 }
118
parse_uint128(const char * input)119 Octet16 parse_uint128(const char* input) {
120 Octet16 output{0};
121 for (unsigned int count = 0; count < OCTET16_LEN; count++) {
122 sscanf(input, "%2hhx", &output[count]);
123 input += 2;
124 }
125 return output;
126 }
127
128 class SmpCalculateConfirmTest : public Test {
129 protected:
130 tSMP_CB p_cb_;
131 // Set random to 0x5783D52156AD6F0E6388274EC6702EE0
132 Octet16 rand_{0x57, 0x83, 0xD5, 0x21, 0x56, 0xAD, 0x6F, 0x0E,
133 0x63, 0x88, 0x27, 0x4E, 0xC6, 0x70, 0x2E, 0xE0};
134
SetUp()135 void SetUp() override {
136 p_cb_.tk = {0};
137 // Set pairing request packet to 0x070710000001(01)
138 p_cb_.local_io_capability = 0x01;
139 p_cb_.loc_oob_flag = 0x00;
140 p_cb_.loc_auth_req = 0x00;
141 p_cb_.loc_enc_size = 0x10;
142 p_cb_.local_i_key = 0x07;
143 p_cb_.local_r_key = 0x07;
144 // Set pairing response packet to 0x050008000003(02)
145 p_cb_.peer_io_caps = 0x03;
146 p_cb_.peer_oob_flag = 0x00;
147 p_cb_.peer_auth_req = 0x00;
148 p_cb_.peer_enc_size = 0x08;
149 p_cb_.peer_i_key = 0x00;
150 p_cb_.peer_r_key = 0x05;
151 // Set role to central
152 p_cb_.role = HCI_ROLE_CENTRAL;
153 std::reverse(rand_.begin(), rand_.end());
154 }
TearDown()155 void TearDown() override {}
156
157 public:
158 };
159
160 // Test smp_gen_p2_4_confirm function implementation
TEST_F(SmpCalculateConfirmTest,test_smp_gen_p2_4_confirm_as_central)161 TEST_F(SmpCalculateConfirmTest, test_smp_gen_p2_4_confirm_as_central) {
162 RawAddress remote_bda;
163 tBLE_ADDR_TYPE remote_bd_addr_type = 0;
164 BTM_ReadRemoteConnectionAddr(p_cb_.pairing_bda, remote_bda,
165 &remote_bd_addr_type);
166 BTM_ReadConnectionAddr(p_cb_.pairing_bda, p_cb_.local_bda, &p_cb_.addr_type);
167 Octet16 p2 = smp_gen_p2_4_confirm(&p_cb_, remote_bda);
168 // Correct p2 is 0x00000000a1a2a3a4a5a6b1b2b3b4b5b6
169 const char expected_p2_str[] = "00000000a1a2a3a4a5a6b1b2b3b4b5b6";
170 char p2_str[2 * OCTET16_LEN + 1];
171 dump_uint128_reverse(p2, p2_str);
172 ASSERT_THAT(p2_str, StrEq(expected_p2_str));
173 }
174
175 // Test smp_gen_p1_4_confirm and aes_128 function implementation
TEST_F(SmpCalculateConfirmTest,test_aes_128_as_central)176 TEST_F(SmpCalculateConfirmTest, test_aes_128_as_central) {
177 RawAddress remote_bda;
178 tBLE_ADDR_TYPE remote_bd_addr_type = 0;
179 BTM_ReadRemoteConnectionAddr(p_cb_.pairing_bda, remote_bda,
180 &remote_bd_addr_type);
181 BTM_ReadConnectionAddr(p_cb_.pairing_bda, p_cb_.local_bda, &p_cb_.addr_type);
182 Octet16 p1 = smp_gen_p1_4_confirm(&p_cb_, remote_bd_addr_type);
183 // Correct p1 is 0x05000800000302070710000001010001
184 const char expected_p1_str[] = "05000800000302070710000001010001";
185 char p1_str[2 * OCTET16_LEN + 1];
186 dump_uint128_reverse(p1, p1_str);
187 ASSERT_THAT(p1_str, StrEq(expected_p1_str));
188 smp_xor_128(&p1, rand_);
189 // Correct p1 xor r is 0x5283dd2156ae6d096498274ec7712ee1
190 const char expected_p1_xor_r_str[] = "5283dd2156ae6d096498274ec7712ee1";
191 char p1_xor_r_str[2 * OCTET16_LEN + 1];
192 dump_uint128_reverse(p1, p1_xor_r_str);
193 ASSERT_THAT(p1_xor_r_str, StrEq(expected_p1_xor_r_str));
194 Octet16 output = crypto_toolbox::aes_128(p_cb_.tk, p1.data(), OCTET16_LEN);
195 const char expected_p1_prime_str[] = "02c7aa2a9857ac866ff91232df0e3c95";
196 char p1_prime_str[2 * OCTET16_LEN + 1];
197 dump_uint128_reverse(output, p1_prime_str);
198 ASSERT_THAT(p1_prime_str, StrEq(expected_p1_prime_str));
199 }
200
201 // Test smp_calculate_comfirm function implementation
TEST_F(SmpCalculateConfirmTest,test_smp_calculate_comfirm_as_central)202 TEST_F(SmpCalculateConfirmTest, test_smp_calculate_comfirm_as_central) {
203 Octet16 output;
204 tSMP_STATUS status = smp_calculate_comfirm(&p_cb_, rand_, &output);
205 EXPECT_EQ(status, SMP_SUCCESS);
206 // Correct MConfirm is 0x1e1e3fef878988ead2a74dc5bef13b86
207 const char expected_confirm_str[] = "1e1e3fef878988ead2a74dc5bef13b86";
208 char confirm_str[2 * OCTET16_LEN + 1];
209 dump_uint128_reverse(output, confirm_str);
210 ASSERT_THAT(confirm_str, StrEq(expected_confirm_str));
211 }
212
213 // Test ECC point validation
TEST(SmpEccValidationTest,test_valid_points)214 TEST(SmpEccValidationTest, test_valid_points) {
215 Point p;
216
217 // Test data from Bluetooth Core Specification
218 // Version 5.0 | Vol 2, Part G | 7.1.2
219
220 // Sample 1
221 p.x[7] = 0x20b003d2;
222 p.x[6] = 0xf297be2c;
223 p.x[5] = 0x5e2c83a7;
224 p.x[4] = 0xe9f9a5b9;
225 p.x[3] = 0xeff49111;
226 p.x[2] = 0xacf4fddb;
227 p.x[1] = 0xcc030148;
228 p.x[0] = 0x0e359de6;
229
230 p.y[7] = 0xdc809c49;
231 p.y[6] = 0x652aeb6d;
232 p.y[5] = 0x63329abf;
233 p.y[4] = 0x5a52155c;
234 p.y[3] = 0x766345c2;
235 p.y[2] = 0x8fed3024;
236 p.y[1] = 0x741c8ed0;
237 p.y[0] = 0x1589d28b;
238
239 EXPECT_TRUE(ECC_ValidatePoint(p));
240
241 // Sample 2
242 p.x[7] = 0x2c31a47b;
243 p.x[6] = 0x5779809e;
244 p.x[5] = 0xf44cb5ea;
245 p.x[4] = 0xaf5c3e43;
246 p.x[3] = 0xd5f8faad;
247 p.x[2] = 0x4a8794cb;
248 p.x[1] = 0x987e9b03;
249 p.x[0] = 0x745c78dd;
250
251 p.y[7] = 0x91951218;
252 p.y[6] = 0x3898dfbe;
253 p.y[5] = 0xcd52e240;
254 p.y[4] = 0x8e43871f;
255 p.y[3] = 0xd0211091;
256 p.y[2] = 0x17bd3ed4;
257 p.y[1] = 0xeaf84377;
258 p.y[0] = 0x43715d4f;
259
260 EXPECT_TRUE(ECC_ValidatePoint(p));
261 }
262
TEST(SmpEccValidationTest,test_invalid_points)263 TEST(SmpEccValidationTest, test_invalid_points) {
264 Point p;
265 multiprecision_init(p.x);
266 multiprecision_init(p.y);
267
268 EXPECT_FALSE(ECC_ValidatePoint(p));
269
270 // Sample 1
271 p.x[7] = 0x20b003d2;
272 p.x[6] = 0xf297be2c;
273 p.x[5] = 0x5e2c83a7;
274 p.x[4] = 0xe9f9a5b9;
275 p.x[3] = 0xeff49111;
276 p.x[2] = 0xacf4fddb;
277 p.x[1] = 0xcc030148;
278 p.x[0] = 0x0e359de6;
279
280 EXPECT_FALSE(ECC_ValidatePoint(p));
281
282 p.y[7] = 0xdc809c49;
283 p.y[6] = 0x652aeb6d;
284 p.y[5] = 0x63329abf;
285 p.y[4] = 0x5a52155c;
286 p.y[3] = 0x766345c2;
287 p.y[2] = 0x8fed3024;
288 p.y[1] = 0x741c8ed0;
289 p.y[0] = 0x1589d28b;
290
291 p.y[0]--;
292
293 EXPECT_FALSE(ECC_ValidatePoint(p));
294 }
295 } // namespace testing
296