• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 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/smp_int.h"
27 
28 /*
29  * This test verifies various key distribution methods in SMP works using the
30  * following parameter set:
31  *
32  * When testing target as Master (Initiator is local, Responder is remote)
33  *
34  * Initiator's Pairing Request: 0x070710000001(01)
35  * Responder's Pairing Response: 0x050008000003(02)
36  * Initiator's Bluetooth Address: 0xA1A2A3A4A5A6
37  * Initiator's Bluetooth Address Type: 0x01
38  * Responder's Bluetooth Address: 0xB1B2B3B4B5B6
39  * Responder's Bluetooth Address Type: 0x00
40  * Initiator's Random Number: 0x5783D52156AD6F0E6388274EC6702EE0
41  * TK Encryption Key: 0x0
42  *
43  * Correct values:
44  *
45  * p1: 0x05000800000302070710000001010001
46  * p1 XOR r: 0x5283dd2156ae6d096498274ec7712ee1
47  * p1 prime: 0x02c7aa2a9857ac866ff91232df0e3c95
48  * p2: 0x00000000a1a2a3a4a5a6b1b2b3b4b5b6
49  * MConfirm (c1): 0x1e1e3fef878988ead2a74dc5bef13b86
50  *
51  * NOTE: All these values are presented in mathematical reasonable canonical
52  * form that has MSB on the left and LSB on the right. In Bluetooth packets,
53  * they are mostly reversed to be Little Endian which have LSB on the left and
54  * MSB on the right.
55  */
56 
57 // Set remote bda to 0xB1B2B3B4B5B6
BTM_ReadRemoteConnectionAddr(BD_ADDR pseudo_addr,BD_ADDR conn_addr,tBLE_ADDR_TYPE * p_addr_type)58 bool BTM_ReadRemoteConnectionAddr(BD_ADDR pseudo_addr, BD_ADDR conn_addr,
59                                   tBLE_ADDR_TYPE* p_addr_type) {
60   const uint8_t local_bda[] = {0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6};
61   memcpy(conn_addr, local_bda, sizeof(local_bda));
62   *p_addr_type = 0x00;
63   return true;
64 }
65 
66 // Set local_bda to 0xA1A2A3A4A5A6
BTM_ReadConnectionAddr(BD_ADDR remote_bda,BD_ADDR local_conn_addr,tBLE_ADDR_TYPE * p_addr_type)67 void BTM_ReadConnectionAddr(BD_ADDR remote_bda, BD_ADDR local_conn_addr,
68                             tBLE_ADDR_TYPE* p_addr_type) {
69   const uint8_t local_bda[] = {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6};
70   memcpy(local_conn_addr, local_bda, sizeof(local_bda));
71   *p_addr_type = 0x01;
72 }
73 
74 // Require bte_logmsg.cc to run, here is just to fake it as we don't care about
75 // trace in unit test
LogMsg(uint32_t trace_set_mask,const char * fmt_str,...)76 void LogMsg(uint32_t trace_set_mask, const char* fmt_str, ...) {
77   va_list args;
78   va_start(args, fmt_str);
79   vprintf(fmt_str, args);
80   va_end(args);
81 }
82 
83 extern void smp_gen_p1_4_confirm(tSMP_CB* p_cb,
84                                  tBLE_ADDR_TYPE remote_bd_addr_type,
85                                  BT_OCTET16 p1);
86 
87 extern void smp_gen_p2_4_confirm(tSMP_CB* p_cb, BD_ADDR remote_bda,
88                                  BT_OCTET16 p2);
89 
90 extern tSMP_STATUS smp_calculate_comfirm(tSMP_CB* p_cb, BT_OCTET16 rand,
91                                          tSMP_ENC* output);
92 
93 namespace testing {
94 
dump_uint128(BT_OCTET16 a,char * buffer)95 void dump_uint128(BT_OCTET16 a, char* buffer) {
96   for (unsigned int i = 0; i < sizeof(BT_OCTET16); ++i) {
97     snprintf(buffer, 3, "%02x", a[i]);
98     buffer += 2;
99   }
100   *buffer = '\0';
101 }
102 
dump_uint128_reverse(BT_OCTET16 a,char * buffer)103 void dump_uint128_reverse(BT_OCTET16 a, char* buffer) {
104   for (int i = (int)(sizeof(BT_OCTET16) - 1); i >= 0; --i) {
105     snprintf(buffer, 3, "%02x", a[i]);
106     buffer += 2;
107   }
108   *buffer = '\0';
109 }
110 
print_uint128(BT_OCTET16 a)111 void print_uint128(BT_OCTET16 a) {
112   for (unsigned int i = 0; i < sizeof(BT_OCTET16); ++i) {
113     printf("%02x", a[i]);
114   }
115   printf("\n");
116 }
117 
parse_uint128(const char * input,BT_OCTET16 output)118 void parse_uint128(const char* input, BT_OCTET16 output) {
119   memset(output, 0, sizeof(BT_OCTET16));
120   for (unsigned int count = 0; count < sizeof(BT_OCTET16); count++) {
121     sscanf(input, "%2hhx", &output[count]);
122     input += 2;
123   }
124 }
125 
reverse_array_inplace(BT_OCTET16 a)126 void reverse_array_inplace(BT_OCTET16 a) {
127   uint8_t tmp;
128   uint8_t* a_end = a + sizeof(BT_OCTET16) - 1;
129   while (a_end > a) {
130     tmp = *a_end;
131     *a_end = *a;
132     *a = tmp;
133     ++a;
134     --a_end;
135   }
136 }
137 
138 class SmpCalculateConfirmTest : public Test {
139  protected:
140   tSMP_CB p_cb_;
141   // Set random to 0x5783D52156AD6F0E6388274EC6702EE0
142   BT_OCTET16 rand_ = {0x57, 0x83, 0xD5, 0x21, 0x56, 0xAD, 0x6F, 0x0E,
143                       0x63, 0x88, 0x27, 0x4E, 0xC6, 0x70, 0x2E, 0xE0};
144 
SetUp()145   void SetUp() {
146     memset(p_cb_.tk, 0, sizeof(p_cb_.tk));
147     // Set pairing request packet to 0x070710000001(01)
148     p_cb_.local_io_capability = 0x01;
149     p_cb_.loc_oob_flag = 0x00;
150     p_cb_.loc_auth_req = 0x00;
151     p_cb_.loc_enc_size = 0x10;
152     p_cb_.local_i_key = 0x07;
153     p_cb_.local_r_key = 0x07;
154     // Set pairing response packet to 0x050008000003(02)
155     p_cb_.peer_io_caps = 0x03;
156     p_cb_.peer_oob_flag = 0x00;
157     p_cb_.peer_auth_req = 0x00;
158     p_cb_.peer_enc_size = 0x08;
159     p_cb_.peer_i_key = 0x00;
160     p_cb_.peer_r_key = 0x05;
161     // Set role to master
162     p_cb_.role = HCI_ROLE_MASTER;
163     reverse_array_inplace(rand_);
164   }
TearDown()165   void TearDown() {}
166 
167  public:
168 };
169 
170 // Test smp_gen_p2_4_confirm function implementation
TEST_F(SmpCalculateConfirmTest,test_smp_gen_p2_4_confirm_as_master)171 TEST_F(SmpCalculateConfirmTest, test_smp_gen_p2_4_confirm_as_master) {
172   BT_OCTET16 p2;
173   BD_ADDR remote_bda;
174   tBLE_ADDR_TYPE remote_bd_addr_type = 0;
175   BTM_ReadRemoteConnectionAddr(p_cb_.pairing_bda, remote_bda,
176                                &remote_bd_addr_type);
177   BTM_ReadConnectionAddr(p_cb_.pairing_bda, p_cb_.local_bda, &p_cb_.addr_type);
178   smp_gen_p2_4_confirm(&p_cb_, remote_bda, p2);
179   // Correct p2 is 0x00000000a1a2a3a4a5a6b1b2b3b4b5b6
180   const char expected_p2_str[] = "00000000a1a2a3a4a5a6b1b2b3b4b5b6";
181   char p2_str[2 * sizeof(BT_OCTET16) + 1];
182   dump_uint128_reverse(p2, p2_str);
183   ASSERT_THAT(p2_str, StrEq(expected_p2_str));
184 }
185 
186 // Test smp_gen_p1_4_confirm and SMP_Encrypt function implementation
TEST_F(SmpCalculateConfirmTest,test_SMP_Encrypt_as_master)187 TEST_F(SmpCalculateConfirmTest, test_SMP_Encrypt_as_master) {
188   BT_OCTET16 p1;
189   BD_ADDR remote_bda;
190   tBLE_ADDR_TYPE remote_bd_addr_type = 0;
191   BTM_ReadRemoteConnectionAddr(p_cb_.pairing_bda, remote_bda,
192                                &remote_bd_addr_type);
193   BTM_ReadConnectionAddr(p_cb_.pairing_bda, p_cb_.local_bda, &p_cb_.addr_type);
194   smp_gen_p1_4_confirm(&p_cb_, remote_bd_addr_type, p1);
195   // Correct p1 is 0x05000800000302070710000001010001
196   const char expected_p1_str[] = "05000800000302070710000001010001";
197   char p1_str[2 * sizeof(BT_OCTET16) + 1];
198   dump_uint128_reverse(p1, p1_str);
199   ASSERT_THAT(p1_str, StrEq(expected_p1_str));
200   smp_xor_128(p1, rand_);
201   // Correct p1 xor r is 0x5283dd2156ae6d096498274ec7712ee1
202   const char expected_p1_xor_r_str[] = "5283dd2156ae6d096498274ec7712ee1";
203   char p1_xor_r_str[2 * sizeof(BT_OCTET16) + 1];
204   dump_uint128_reverse(p1, p1_xor_r_str);
205   ASSERT_THAT(p1_xor_r_str, StrEq(expected_p1_xor_r_str));
206   tSMP_ENC output;
207   memset(&output, 0, sizeof(tSMP_ENC));
208   ASSERT_TRUE(
209       SMP_Encrypt(p_cb_.tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, &output));
210   const char expected_p1_prime_str[] = "02c7aa2a9857ac866ff91232df0e3c95";
211   char p1_prime_str[2 * sizeof(BT_OCTET16) + 1];
212   dump_uint128_reverse(output.param_buf, p1_prime_str);
213   ASSERT_THAT(p1_prime_str, StrEq(expected_p1_prime_str));
214 }
215 
216 // Test smp_calculate_comfirm function implementation
TEST_F(SmpCalculateConfirmTest,test_smp_calculate_comfirm_as_master)217 TEST_F(SmpCalculateConfirmTest, test_smp_calculate_comfirm_as_master) {
218   tSMP_ENC output;
219   tSMP_STATUS status = smp_calculate_comfirm(&p_cb_, rand_, &output);
220   EXPECT_EQ(status, SMP_SUCCESS);
221   // Correct MConfirm is 0x1e1e3fef878988ead2a74dc5bef13b86
222   const char expected_confirm_str[] = "1e1e3fef878988ead2a74dc5bef13b86";
223   char confirm_str[2 * sizeof(BT_OCTET16) + 1];
224   dump_uint128_reverse(output.param_buf, confirm_str);
225   ASSERT_THAT(confirm_str, StrEq(expected_confirm_str));
226 }
227 }
228