@@ -27,6 +27,7 @@ unsigned char const_Rb[16] = {
27
27
#define LEN_MAC_KEY 16
28
28
29
29
void BluetoothCryptoToolbox::printBytes (uint8_t bytes[], uint8_t length){
30
+ #ifdef _BLE_TRACE_
30
31
for (int i=0 ; i<length; i++){
31
32
if (i>0 ){
32
33
Serial.print (" , 0x" );
@@ -36,7 +37,10 @@ void BluetoothCryptoToolbox::printBytes(uint8_t bytes[], uint8_t length){
36
37
Serial.print (bytes[i],HEX);
37
38
}
38
39
Serial.print (' \n ' );
40
+ #endif
39
41
}
42
+
43
+
40
44
int BluetoothCryptoToolbox::f5 (uint8_t DHKey[],uint8_t N_master[], uint8_t N_slave[],
41
45
uint8_t BD_ADDR_master[], uint8_t BD_ADDR_slave[], uint8_t MacKey[], uint8_t LTK[])
42
46
{
@@ -128,15 +132,15 @@ void BluetoothCryptoToolbox::testAh()
128
132
{
129
133
uint8_t irk[16 ] = {0xec ,0x02 ,0x34 ,0xa3 ,0x57 ,0xc8 ,0xad ,0x05 ,0x34 ,0x10 ,0x10 ,0xa6 ,0x0a ,0x39 ,0x7d ,0x9b };
130
134
uint8_t expected_final[3 ] = {0x0d ,0xfb ,0xaa };
131
-
132
135
uint8_t ourResult[3 ];
133
136
ah (irk, expected_final, ourResult);
134
137
135
-
138
+ # ifdef _BLE_TRACE_
136
139
Serial.print (" Expected : " );
137
140
printBytes (&expected_final[3 ], 3 );
138
141
Serial.print (" Actual : " );
139
142
printBytes (ourResult, 3 );
143
+ #endif
140
144
}
141
145
142
146
int BluetoothCryptoToolbox::g2 (uint8_t U[], uint8_t V[], uint8_t X[], uint8_t Y[], uint8_t out[4 ])
@@ -160,19 +164,19 @@ void BluetoothCryptoToolbox::testg2(){
160
164
uint8_t X[16 ] = {0xd5 ,0xcb ,0x84 ,0x54 ,0xd1 ,0x77 ,0x73 ,0x3e ,0xff ,0xff ,0xb2 ,0xec ,0x71 ,0x2b ,0xae ,0xab };
161
165
uint8_t Y[16 ] = {0xa6 ,0xe8 ,0xe7 ,0xcc ,0x25 ,0xa7 ,0x5f ,0x6e ,0x21 ,0x65 ,0x83 ,0xf7 ,0xff ,0x3d ,0xc4 ,0xcf };
162
166
uint8_t out[4 ];
163
-
164
-
167
+
165
168
uint32_t expected = 0 ;
166
169
g2 (U,V,X,Y,out);
167
170
uint32_t result = 0 ;
168
171
for (int i=0 ; i<4 ; i++) result += out[i] << 8 *i;
169
172
173
+ #ifdef _BLE_TRACE_
170
174
Serial.print (" Expected : " );
171
175
Serial.println (expected);
172
176
Serial.print (" Result : " );
173
177
Serial.println (result);
174
178
Serial.println ();
175
-
179
+ # endif
176
180
}
177
181
178
182
void BluetoothCryptoToolbox::AES_CMAC ( unsigned char *key, unsigned char *input, int length,
@@ -264,16 +268,20 @@ int BluetoothCryptoToolbox::AES_128(uint8_t* key, uint8_t* data_in, uint8_t* dat
264
268
int n = 0 ;
265
269
int tries = 30 ;
266
270
while (HCI.leEncrypt (key,data_in, &status, data_out)!=1 &&n<tries){
271
+ #ifdef _BLE_TRACE_
267
272
Serial.print (" AES failed... retrying: " );
268
273
Serial.println (n);
274
+ #endif
269
275
BLE.end ();
270
276
delay (200 );
271
277
BLE.begin ();
272
278
n++;
273
279
delay (100 *n);
274
280
}
275
281
if (n==tries){
282
+ #ifdef _BLE_TRACE_
276
283
Serial.println (" something went wrong with AES." );
284
+ #endif
277
285
return 0 ;
278
286
}
279
287
return 1 ;
@@ -288,7 +296,7 @@ void BluetoothCryptoToolbox::test(){
288
296
289
297
for ( i=0 ; i<16 ; i++ ) Z[i] = 0x00 ;
290
298
uint8_t k[16 ] = {0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c };
291
-
299
+
292
300
Serial.println (" AES Plaintext:" );
293
301
for (int i=0 ; i<16 ; i++){
294
302
Serial.print (" 0x" );
@@ -352,7 +360,7 @@ void BluetoothCryptoToolbox::test(){
352
360
}
353
361
Serial.println (" ." );
354
362
}
355
- #endif
363
+ #endif // _BLE_TRACE_
356
364
// From RFC
357
365
void BluetoothCryptoToolbox::leftshift_onebit (unsigned char *input,unsigned char *output)
358
366
{
0 commit comments