-
Notifications
You must be signed in to change notification settings - Fork 1
/
le_toolbox.py
1398 lines (1037 loc) · 49.5 KB
/
le_toolbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from Crypto.Cipher import AES
import binascii
import random
import math
from EllipticCurve import *
# 加密函数
def encrypt(key, plain_text):
mode = AES.MODE_ECB
cryptos = AES.new(key, mode)
cipher_text = cryptos.encrypt(plain_text)
return cipher_text
# 解密函数
def decrypt(key, plain_text):
mode = AES.MODE_ECB
cryptos = AES.new(key, mode)
cipher_text = cryptos.decrypt(plain_text)
return cipher_text
def reverse_bytes(data_arr):
tmpIndex = len(data_arr) - 1
data_arr_tmp = b''
for i in range(len(data_arr)):
b = data_arr[tmpIndex - i]
data_arr_tmp += b.to_bytes(length=1,byteorder='big',signed=False)
return data_arr_tmp
def get_bytes_from_big_eddian_string(str):
str = str.upper().replace('0X', '')
return reverse_bytes(bytes.fromhex(str))
def reverse_bytes_enc(data_arr):
tmpIndex = 15
data_arr_tmp = b''
for i in range(16):
b = data_arr[(tmpIndex - i)*2 + 0]
data_arr_tmp += b.to_bytes(length=1,byteorder='big',signed=False)
b = data_arr[(tmpIndex - i)*2 + 1]
data_arr_tmp += b.to_bytes(length=1,byteorder='big',signed=False)
return data_arr_tmp
def xor_array(a_arr, b_arr):
data_arr_tmp = b''
for i in range(len(a_arr)):
a = a_arr[i]
b = b_arr[i]
data_arr_tmp += (a^b).to_bytes(length=1,byteorder='big',signed=False)
return data_arr_tmp
def int2bin_8(val):
return '{:08b}'.format(val)
def bytes2bin(arr):
tmp = ''
for i in range(len(arr)):
tmp += int2bin_8(arr[i])
return tmp
def bin2bytes(bin_str):
if len(bin_str) % 8 != 0:
raise Exception("Error length")
cnt = int(len(bin_str) / 8)
tmp = b''
for i in range(cnt):
tmp += int(bin_str[i*8:((i+1)*8)], 2).to_bytes(length=1,byteorder='big',signed=False)
return tmp
# input arr is little_endian
def lshift_array(arr, shift_cnt):
arr = reverse_bytes(arr)
tmp = bytes2bin(arr)
tmp_len = len(tmp)
tmp_shift = tmp[shift_cnt:]
for i in range(shift_cnt):
tmp_shift += '0'
return reverse_bytes(bin2bytes(tmp_shift))
def combine_byte_array(msb, lsb):
tmp = b''
tmp += lsb
tmp += msb
return tmp
def get_ccm_ai(flags, nonce, counter):
tmp = b''
tmp += (int(flags)).to_bytes(length=1,byteorder='big',signed=False)
tmp += nonce
tmp += counter.to_bytes(length=2,byteorder='big',signed=False)
return tmp
def get_data_with_offset_and_expand(dataoffset, packet):
data_array_tmp = b''
for j in range(16):
if (dataoffset + j) < len(packet):
b = packet[dataoffset + j]
else:
b = 0
data_array_tmp += b.to_bytes(length=1, byteorder='big', signed=False)
return data_array_tmp
def get_data_with_offset_with_limit(dataoffset, packet):
data_array_tmp = b''
for j in range(16):
if (dataoffset + j) < len(packet):
b = packet[dataoffset + j]
data_array_tmp += b.to_bytes(length=1, byteorder='big', signed=False)
return data_array_tmp
def aes_ccm_sub_authentication(M, L, K, N, m, a, debug=False):
b0 = b''
a_len = len(a)
data_len = len(m)
work_cnt = int((data_len + 15) / 16)
# flags is define below
# Bit Number Contents
# ---------- ----------------------
# 7 Reserved (always zero)
# 6 Adata
# 5 ... 3 M'
# 2 ... 0 L'
# M' = (M-2)/2
# L' = L-1
# Flags = 64*Adata + 8*M' + L'.
flags = (a_len>0)*64 + ((M-2)/2)*8 + (L-1)
# b0 is define below
# Octet Number Contents
# ------------ ---------
# 0 Flags
# 1 ... 15-L Nonce N
# 16-L ... 15 l(m)
b0 += (int(flags)).to_bytes(length=1,byteorder='big',signed=False)
b0 += N
b0 += data_len.to_bytes(length=2,byteorder='big',signed=False)
if debug: print("CBC IV in[b0]: %s" % (print_hex_little(b0)))
# X_1 := E( K, B_0 )
x1 = encrypt(K, b0)
if debug: print("CBC IV out[x1]: %s" % (print_hex_little(x1)))
# here only care 0 < l(a) < (2^16 - 2^8)
# and limit to 16-2.
# First two octets Followed by Comment
# ----------------- ---------------- -------------------------------
# 0x0000 Nothing Reserved
# 0x0001 ... 0xFEFF Nothing For 0 < l(a) < (2^16 - 2^8)
# 0xFF00 ... 0xFFFD Nothing Reserved
# 0xFFFE 4 octets of l(a) For (2^16 - 2^8) <= l(a) < 2^32
# 0xFFFF 8 octets of l(a) For 2^32 <= l(a) < 2^64
b1 = b''
b1 += a_len.to_bytes(length=2,byteorder='big',signed=False)
for i in range(a_len):
b = a[i]
b1 += b.to_bytes(length=1,byteorder='big',signed=False)
# append zero for a.
for i in range(14 - a_len):
b = 0
b1 += b.to_bytes(length=1,byteorder='big',signed=False)
if debug: print("b1: %s" % (print_hex_little(b1)))
# X_i + 1 := E(K, X_i XOR B_i ) for i=1, ..., n
x1_xor_b1 = xor_array(x1, b1)
if debug: print("After xor: %s [hdr]" % (print_hex_little(x1_xor_b1)))
x2 = encrypt(K, x1_xor_b1)
if debug: print("After aes[x2]: %s" % (print_hex_little(x2)))
# start data process.
# X_i + 1 := E(K, X_i XOR B_i ) for i=1, ..., n
xi = x2
for i in range(work_cnt):
bi = get_data_with_offset_and_expand(i * 16, m)
if debug: print("b%s: %s" % (i + 2, print_hex_little(bi)))
xi_xor_bi = xor_array(xi, bi)
if debug: print("After xor: %s [msg]" % (print_hex_little(xi_xor_bi)))
xi_plus_1 = encrypt(K, xi_xor_bi)
if debug: print("After aes x%s: %s" % (i + 2 + 1, print_hex_little(xi_plus_1)))
xi = xi_plus_1
# T := first-M-bytes( X_n+1 )
T = b''
for i in range(M):
b = xi[i]
T += b.to_bytes(length=1, byteorder='big', signed=False)
if debug: print("CBC-MAC [MIC]: %s" % (print_hex_little(T)))
return T
def aes_ccm_sub_keystream(L, K, N, data_len, debug=False):
work_cnt = int((data_len + 15) / 16)
# ctr work start
# S_i := E( K, A_i ) for i=0, 1, 2, ...
# The Flags field is formatted as follows:
#
# Bit Number Contents
# ---------- ----------------------
# 7 Reserved (always zero)
# 6 Reserved (always zero)
# 5 ... 3 Zero
# 2 ... 0 L'
flags = L-1
# A_i is define below
# Octet Number Contents
# ------------ ---------
# 0 Flags
# 1 ... 15-L Nonce N
# 16-L ... 15 Counter i
s_array = []
for counter in range(work_cnt + 1):
ai = get_ccm_ai(flags, N, counter)
si = encrypt(K, ai)
s_array.append(si)
if counter == 1:
if debug: print("CTR Start: %s" % (print_hex_little(ai)))
if counter > 0:
if debug: print("CTR [%s], s%s: %s" % (counter, counter, print_hex_little(si)))
return s_array
def aes_ccm_sub_ctr_xor(s_array, data, debug=False):
data_xor = b''
for counter in range(len(s_array) - 1):
si = s_array[counter + 1]
sub_data = get_data_with_offset_with_limit((counter) * 16, data)
data_xor += xor_array(sub_data, si)
return data_xor
def aes_ccm_sub_ctr(M, L, K, N, m, debug=False):
data_len = len(m)
# ctr work start
# S_i := E( K, A_i ) for i=0, 1, 2, ...
s_array = aes_ccm_sub_keystream(L, K, N, data_len, debug)
c = aes_ccm_sub_ctr_xor(s_array, m, debug)
s0 = s_array[0]
s0_sub_mac = b''
for i in range(M):
b = s0[i]
s0_sub_mac += b.to_bytes(length=1, byteorder='big', signed=False)
if debug: print("CTR[MAC ]: %s" % (print_hex_little(s0_sub_mac)))
return c, s0_sub_mac
# Name Description Size Encoding
# ---- ---------------------------------------- ------ --------
# M Number of octets in authentication field 3 bits (M-2)/2
# L Number of octets in length field 3 bits L-1
# Name Description Size
# ---- ----------------------------------- -----------------------
# K Block cipher key Depends on block cipher
# N Nonce 15-L octets
# m Message to authenticate and encrypt l(m) octets
# a Additional authenticated data l(a) octets
def aes_ccm_encrypt(M, L, K, N, m, a, debug=False):
if debug: print("M: %s" % (M))
if debug: print("L: %s" % (L))
if debug: print("K: %s" % (print_hex_little(K)))
if debug: print("N: %s" % (print_hex_little(N)))
if debug: print("m: %s" % (print_hex_little(m)))
if debug: print("a: %s" % (print_hex_little(a)))
# 2.2. Authentication
T = aes_ccm_sub_authentication(M, L, K, N, m, a, debug)
# 2.3. Encryption
c, s0_sub_mac = aes_ccm_sub_ctr(M, L, K, N, m, debug)
# U := T XOR first-M-bytes( S_0 )
U = xor_array(T, s0_sub_mac)
data_enc = b''
# first a bytes is un-enc
data_enc += a
# second c bytes is enc
data_enc += c
# last U bytes is MIC
data_enc += U
return data_enc
# Name Description Size Encoding
# ---- ---------------------------------------- ------ --------
# M Number of octets in authentication field 3 bits (M-2)/2
# L Number of octets in length field 3 bits L-1
# Name Description Size
# ---- ----------------------------------- -----------------------
# K Block cipher key Depends on block cipher
# N Nonce 15-L octets
# c Encrypt Message l(m) octets
# a Additional authenticated data l(a) octets
# U_in Authenticate In M octets
def aes_ccm_decrypt(M, L, K, N, c, a, U_in, debug=False):
if debug: print("M: %s" % (M))
if debug: print("L: %s" % (L))
if debug: print("K: %s" % (print_hex_little(K)))
if debug: print("N: %s" % (print_hex_little(N)))
if debug: print("c: %s" % (print_hex_little(c)))
if debug: print("a: %s" % (print_hex_little(a)))
if debug: print("U_in: %s" % (print_hex_little(U_in)))
# 2.3. Decryption
m, s0_sub_mac = aes_ccm_sub_ctr(M, L, K, N, c, debug)
# 2.2. Authentication
T = aes_ccm_sub_authentication(M, L, K, N, m, a, debug)
# U := T XOR first-M-bytes( S_0 )
U = xor_array(T, s0_sub_mac)
assert(U == U_in)
return a + m
def aes_ccm_encrypt_packet(M, L, K, N, a_len, packet_with_add, debug=False):
a = packet_with_add[0 : a_len]
data_real_len = len(packet_with_add) - a_len
m = packet_with_add[a_len : a_len + data_real_len]
return aes_ccm_encrypt(M, L, K, N, m, a, debug)
def aes_ccm_decrypt_packet(M, L, K, N, a_len, packet_with_add, debug=False):
a = packet_with_add[0 : a_len]
data_real_len = len(packet_with_add) - a_len - M
c = packet_with_add[a_len : a_len + data_real_len]
U_in = packet_with_add[-M : ]
return aes_ccm_decrypt(M, L, K, N, c, a, U_in, debug)
def aes_ccm_packet_header_to_a(header):
# NESN, SN, MD set 0.
return (header & 0xe3).to_bytes(length=1, byteorder='big', signed=False)
def aes_ccm_encrypt_bluetooth(M, L, K, IV, packetCounter, directionBit, header, payload, debug=False):
N = b''
N += (packetCounter & 0x7ffffffff | (directionBit << 39)).to_bytes(length=5, byteorder='little', signed=False)
N += reverse_bytes(IV)
a = aes_ccm_packet_header_to_a(header)
m = payload
return aes_ccm_encrypt(M, L, K, N, m, a, debug)
def print_hex_little(arr):
return arr.hex()
def print_hex_big(arr):
return "0x" + reverse_bytes(arr).hex()
def smp_e(key, plaintextData, debug=False):
if debug: print("--- smp_e start ---")
if debug: print("key: ", print_hex_big(key))
if debug: print("plaintextData: ", print_hex_big(plaintextData))
key = reverse_bytes(key)
plaintextData = reverse_bytes(plaintextData)
encryptedData = reverse_bytes(encrypt(key, plaintextData)) # 加密
if debug: print("encryptedData: ", print_hex_big(encryptedData))
if debug: print("--- smp_e end ---")
return encryptedData
# Random address hash function ah.
# k is 128 bits
# r is 24 bits
# padding is 104 bits
def smp_ah(k, r):
print("k: %s" % (print_hex_big(k)))
print("r: %s" % (print_hex_big(r)))
padding = bytes.fromhex('00 00 00 00 00 00 00 00 00 00 00 00 00')
# r’ = padding || r
r = combine_byte_array(padding, r)
print("r': %s" % (print_hex_big(r)))
# ah(k, r) = e(k, r’) mod 2^24
ah = smp_e(k, r)
print("ah_full: %s" % (print_hex_big(ah)))
ah = ah[0:3]
print("ah: %s" % (print_hex_big(ah)))
return ah
# Confirm value generation function c1 for LE legacy pairing
# k is 128 bits
# r is 128 bits
# pres is 56 bits
# preq is 56 bits
# iat is 1 bit
# ia is 48 bits
# rat is 1 bit
# ra is 48 bits
# padding is 32 zero bits
def smp_c1(k, r, pres, preq, iat, ia, rat, ra):
print("k: %s" % (print_hex_big(k)))
print("r: %s" % (print_hex_big(r)))
print("pres: %s" % (print_hex_big(pres)))
print("preq: %s" % (print_hex_big(preq)))
print("iat: %s" % (print_hex_big(iat)))
print("ia: %s" % (print_hex_big(ia)))
print("rat: %s" % (print_hex_big(rat)))
print("ra: %s" % (print_hex_big(ra)))
# iat is concatenated with 7 zero bits to create iat’ which is 8 bits in length. iat is the least significant bit of iat’.
# rat is concatenated with 7 zero bits to create rat’ which is 8 bits in length. rat is the least significant bit of rat’.
# p1 = pres || preq || rat’ || iat’
p1 = combine_byte_array(combine_byte_array(combine_byte_array(pres, preq), rat), iat)
print("p1: %s" % (print_hex_big(p1)))
padding = bytes.fromhex('00 00 00 00')
# p2 = padding || ia || ra
p2 = combine_byte_array(combine_byte_array(padding, ia), ra)
print("p2: %s" % (print_hex_big(p2)))
# c1 (k, r, preq, pres, iat, rat, ia, ra) = e(k, e(k, r XOR p1) XOR p2)
c1 = smp_e(k, xor_array(smp_e(k, xor_array(r, p1)), p2))
print("c1: %s" % (print_hex_big(c1)))
return c1
# Key generation function s1 for LE legacy pairing
# k is 128 bits
# r1 is 128 bits
# r2 is 128 bits
def smp_s1(k, r1, r2):
print("k: %s" % (print_hex_big(k)))
print("r1: %s" % (print_hex_big(r1)))
print("r2: %s" % (print_hex_big(r2)))
# The most significant 64-bits of r1 are discarded to generate r1’
# and the most significant 64-bits of r2 are discarded to generate r2’.
# r’ = r1’ || r2’
r = combine_byte_array(r1[0:8], r2[0:8])
print("r': %s" % (print_hex_big(r)))
# s1(k, r1, r2) = e(k, r’)
s1 = smp_e(k, r)
print("s1: %s" % (print_hex_big(s1)))
return s1
def smp_cmac_msb(info):
return info[-1] & 0x80 != 0
def smp_cmac_padding(arr):
length = len(arr)
pad = b''
reserve_size = 16 - length
for j in range(reserve_size):
if j == reserve_size - 1:
pad += 0x80.to_bytes(length=1,byteorder='big',signed=False)
else:
pad += 0x00.to_bytes(length=1,byteorder='big',signed=False)
return combine_byte_array(arr, pad)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + Algorithm Generate_Subkey +
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + +
# + Input : K (128-bit key) +
# + Output : K1 (128-bit first subkey) +
# + K2 (128-bit second subkey) +
# +-------------------------------------------------------------------+
# + +
# + Constants: const_Zero is 0x00000000000000000000000000000000 +
# + const_Rb is 0x00000000000000000000000000000087 +
# + Variables: L for output of AES-128 applied to 0^128 +
# + +
# + Step 1. L := AES-128(K, const_Zero); +
# + Step 2. if MSB(L) is equal to 0 +
# + then K1 := L << 1; +
# + else K1 := (L << 1) XOR const_Rb; +
# + Step 3. if MSB(K1) is equal to 0 +
# + then K2 := K1 << 1; +
# + else K2 := (K1 << 1) XOR const_Rb; +
# + Step 4. return K1, K2; +
# + +
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def smp_aes_cmac_generate_subkey(K, debug=False):
if debug: print("K: %s" % (print_hex_big(K)))
const_Zero = get_bytes_from_big_eddian_string('00000000000000000000000000000000')
const_Rb = get_bytes_from_big_eddian_string('00000000000000000000000000000087')
L = smp_e(K, const_Zero)
if debug: print("L: %s" % (print_hex_big(L)))
if smp_cmac_msb(L) == 0:
K1 = lshift_array(L, 1)
else:
K1 = xor_array(lshift_array(L, 1), const_Rb)
if smp_cmac_msb(K1) == 0:
K2 = lshift_array(K1, 1)
else:
K2 = xor_array(lshift_array(K1, 1), const_Rb)
if debug: print("K1: %s" % (print_hex_big(K1)))
if debug: print("K2: %s" % (print_hex_big(K2)))
return K1, K2
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + Algorithm AES-CMAC +
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + +
# + Input : K ( 128-bit key ) +
# + : M ( message to be authenticated ) +
# + : len ( length of the message in octets ) +
# + Output : T ( message authentication code ) +
# + +
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# + Constants: const_Zero is 0x00000000000000000000000000000000 +
# + const_Bsize is 16 +
# + +
# + Variables: K1, K2 for 128-bit subkeys +
# + M_i is the i-th block (i=1..ceil(len/const_Bsize)) +
# + M_last is the last block xor-ed with K1 or K2 +
# + n for number of blocks to be processed +
# + r for number of octets of last block +
# + flag for denoting if last block is complete or not +
# + +
# + Step 1. (K1,K2) := Generate_Subkey(K); +
# + Step 2. n := ceil(len/const_Bsize); +
# + Step 3. if n = 0 +
# + then +
# + n := 1; +
# + flag := false; +
# + else +
# + if len mod const_Bsize is 0 +
# + then flag := true; +
# + else flag := false; +
# + +
# + Step 4. if flag is true +
# + then M_last := M_n XOR K1; +
# + else M_last := padding(M_n) XOR K2; +
# + Step 5. X := const_Zero; +
# + Step 6. for i := 1 to n-1 do +
# + begin +
# + Y := X XOR M_i; +
# + X := AES-128(K,Y); +
# + end +
# + Y := M_last XOR X; +
# + T := AES-128(K,Y); +
# + Step 7. return T; +
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def smp_aes_cmac(K, M, debug=False):
m_len = len(M)
if debug: print("K: %s" % (print_hex_big(K)))
if debug: print("M: %s" % (print_hex_big(M)))
if debug: print("len: %s" % m_len)
const_Zero = get_bytes_from_big_eddian_string('00000000000000000000000000000000')
const_Bsize = 16
K1, K2 = smp_aes_cmac_generate_subkey(K, debug)
n = math.ceil(m_len/const_Bsize)
if debug: print("n: %s" % n)
if n == 0:
n = 1
flag = False
else:
if m_len % const_Bsize == 0:
flag = True
else:
flag = False
if debug: print("flag: %s" % flag)
if debug: print("n: %s" % n)
M_n = reverse_bytes(get_data_with_offset_with_limit((n - 1) * 16, reverse_bytes(M)))
if debug: print("M_n: %s" % (print_hex_big(M_n)))
if flag:
M_last = xor_array(M_n, K1)
else:
M_n_padding = smp_cmac_padding(M_n)
if debug: print("M_n_padding: %s" % (print_hex_big(M_n_padding)))
M_last = xor_array(M_n_padding, K2)
#if debug: print("M_n: %s" % (print_hex_big(M_n)))
if debug: print("M_last: %s" % (print_hex_big(M_last)))
X = const_Zero
for i in range(n - 1):
M_i = reverse_bytes(get_data_with_offset_and_expand(i * 16, reverse_bytes(M)))
Y = xor_array(X, M_i)
X = smp_e(K, Y)
if debug: print("i: %s" % i)
if debug: print("M_i: %s" % (print_hex_big(M_i)))
if debug: print("Y: %s" % (print_hex_big(Y)))
if debug: print("X: %s" % (print_hex_big(X)))
Y = xor_array(M_last, X)
T = smp_e(K, Y)
if debug: print("Y: %s" % (print_hex_big(Y)))
if debug: print("T: %s" % (print_hex_big(T)))
return T
# LE Secure Connections confirm value generation function f4
# U is 256 bits
# V is 256 bits
# X is 128 bits
# Z is 8 bits
def smp_f4(U, V, X, Z):
print("U: %s" % (print_hex_big(U)))
print("V: %s" % (print_hex_big(V)))
print("X: %s" % (print_hex_big(X)))
print("Z: %s" % (print_hex_big(Z)))
# The least significant octet of Z becomes the least significant octet of the AESCMAC
# input message m and the most significant octet of U becomes the most
# significant octet of the AES-CMAC input message m.
# f4(U, V, X, Z) = AES-CMACX (U || V || Z)
m = combine_byte_array(combine_byte_array(U, V), Z)
print("m: %s" % (print_hex_big(m)))
f4 = smp_aes_cmac(X, m)
print("f4: %s" % (print_hex_big(f4)))
return f4
# LE Secure Connections key generation function f5
# W is 256 bits
# N1 is 128 bits
# N2 is 128 bits
# A1 is 56 bits
# A2 is 56 bits
def smp_f5(W, N1, N2, A1, A2):
print("W: %s" % (print_hex_big(W)))
print("N1: %s" % (print_hex_big(N1)))
print("N2: %s" % (print_hex_big(N2)))
print("A1: %s" % (print_hex_big(A1)))
print("A2: %s" % (print_hex_big(A2)))
SALT = get_bytes_from_big_eddian_string('6C88 8391 AAF5 A538 6037 0BDB 5A60 83BE')
keyID = get_bytes_from_big_eddian_string('62746C65')
# T = AES-CMACSALT (W)
T = smp_aes_cmac(SALT, W)
print("T: %s" % (print_hex_big(T)))
# f5(W, N1, N2, A1, A2) = AES-CMACT (Counter = 0 || keyID || N1 || N2 || A1 ||
# A2 || Length = 256) || AES-CMACT (Counter = 1 || keyID || N1 || N2 || A1 ||
# A2 || Length = 256)
# MacKey || LTK = f5(DHKey, Nc, Np, BD_ADDR_C, BD_ADDR_P)
# MacKey = AES-CMACT (Counter = 0 || keyID || N1 || N2 || A1 || A2 || Length = 256)
Counter = get_bytes_from_big_eddian_string('00')
Length = get_bytes_from_big_eddian_string('01 00')
m = combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(Counter, keyID), N1), N2), A1), A2), Length)
MacKey = smp_aes_cmac(T, m)
print("MacKey: %s" % (print_hex_big(MacKey)))
# LTK = AES-CMACT (Counter = 1 || keyID || N1 || N2 || A1 || A2 || Length = 256)
Counter = get_bytes_from_big_eddian_string('01')
Length = get_bytes_from_big_eddian_string('01 00')
m = combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(Counter, keyID), N1), N2), A1), A2), Length)
LTK = smp_aes_cmac(T, m)
print("LTK: %s" % (print_hex_big(LTK)))
return MacKey, LTK
# LE Secure Connections check value generation function f6
# W is 128 bits
# N1 is 128 bits
# N2 is 128 bits
# R is 128 bits
# IOcap is 24 bits
# A1 is 56 bits
# A2 is 56 bits
def smp_f6(W, N1, N2, R, IOcap, A1, A2):
print("W: %s" % (print_hex_big(W)))
print("N1: %s" % (print_hex_big(N1)))
print("N2: %s" % (print_hex_big(N2)))
print("R: %s" % (print_hex_big(R)))
print("IOcap: %s" % (print_hex_big(IOcap)))
print("A1: %s" % (print_hex_big(A1)))
print("A2: %s" % (print_hex_big(A2)))
m = combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(combine_byte_array(N1, N2), R), IOcap), A1), A2)
print("m: %s" % (print_hex_big(m)))
# f6(W, N1, N2, R, IOcap, A1, A2) = AES-CMACW (N1 || N2 || R || IOcap || A1 || A2)
f6 = smp_aes_cmac(W, m)
print("f6: %s" % (print_hex_big(f6)))
return f6
# LE Secure Connections numeric comparison value generation function g2
# U is 256 bits
# V is 256 bits
# X is 128 bits
# Y is 128 bits
def smp_g2(U, V, X, Y):
print("U: %s" % (print_hex_big(U)))
print("V: %s" % (print_hex_big(V)))
print("X: %s" % (print_hex_big(X)))
print("Y: %s" % (print_hex_big(Y)))
# g2(U, V, X, Y) = AES-CMACX(U || V || Y) mod 232
m = combine_byte_array(combine_byte_array(U, V), Y)
print("m: %s" % (print_hex_big(m)))
g2 = smp_aes_cmac(X, m)[0:4]
print("g2: %s" % (print_hex_big(g2)))
return g2
# Link key conversion function h6
# W is 128 bits
# keyID is 32 bits
def smp_h6(W, keyID):
print("W: %s" % (print_hex_big(W)))
print("keyID: %s" % (print_hex_big(keyID)))
# h6(W, keyID) = AES-CMACW(keyID)
h6 = smp_aes_cmac(W, keyID)
print("h6: %s" % (print_hex_big(h6)))
return h6
# Link key conversion function h7
# SALT is 32 bits
# W is 128 bits
def smp_h7(SALT, W):
print("SALT: %s" % (print_hex_big(SALT)))
print("W: %s" % (print_hex_big(W)))
# h7(SALT, W) = AES-CMACSALT(W)
h7 = smp_aes_cmac(SALT, W)
print("h7: %s" % (print_hex_big(h7)))
return h7
def print_header(str):
split_line = "###################################################################################"
print(split_line)
split_line_size = len(split_line)
str_size = len(str)
start_pos = int(split_line_size/2 - str_size/2)
header_str = ''
for i in range(split_line_size):
if i == 0 or i == split_line_size - 1:
header_str += '#'
elif i >= start_pos and i < (start_pos + str_size):
header_str += str[i - start_pos]
else:
header_str += ' '
print(header_str)
print(split_line)
def print_result_with_exp(result):
result_str = "Error"
if result:
result_str = "Pass"
print(">>>>>>>>>> %s <<<<<<<<<<" % result_str)
assert(result)
def aes_ecb_test():
print_header("aes_ecb_test")
key = bytes.fromhex("00000000000000000000000000000000")
plain_text = bytes.fromhex("112233445566778899AABBCCDDEEFF00")
e = encrypt(key, plain_text) # 加密
print("key:", key.hex())
print("plain_text:", plain_text.hex())
print("e:", e.hex())
d = decrypt(key, e) # 解密
print("d:", d.hex())
print_result_with_exp(d == plain_text)
def smp_ah_test():
print_header("smp_ah_test")
print_header("D.7 ah RANDOM ADDRESS HASH FUNCTIONS")
# IRK ec0234a3 57c8ad05 341010a6 0a397d9b
# prand 00000000 00000000 00000000 00708194
# M 00000000 00000000 00000000 00708194
# AES_128 159d5fb7 2ebe2311 a48c1bdc c40dfbaa
# ah 0dfbaa
k = get_bytes_from_big_eddian_string('ec0234a3 57c8ad05 341010a6 0a397d9b')
r = get_bytes_from_big_eddian_string('708194')
ah = smp_ah(k, r)
ah_exp = get_bytes_from_big_eddian_string('0dfbaa')
print_result_with_exp(ah == ah_exp)
def smp_c1_test():
print_header("smp_c1_test")
k = get_bytes_from_big_eddian_string('00000000000000000000000000000000')
r = get_bytes_from_big_eddian_string('5783D52156AD6F0E6388274EC6702EE0')
pres = get_bytes_from_big_eddian_string('05000800000302')
preq = get_bytes_from_big_eddian_string('07071000000101')
iat = get_bytes_from_big_eddian_string('01')
ia = get_bytes_from_big_eddian_string('A1A2A3A4A5A6')
rat = get_bytes_from_big_eddian_string('00')
ra = get_bytes_from_big_eddian_string('B1B2B3B4B5B6')
c1 = smp_c1(k, r, pres, preq, iat, ia, rat, ra)
c1_exp = get_bytes_from_big_eddian_string('1E1E3FEF878988EAD2A74DC5BEF13B86')
print_result_with_exp(c1 == c1_exp)
def smp_s1_test():
print_header("smp_s1_test")
k = get_bytes_from_big_eddian_string('00000000000000000000000000000000')
r1 = get_bytes_from_big_eddian_string('000F0E0D0C0B0A091122334455667788')
r2 = get_bytes_from_big_eddian_string('010203040506070899AABBCCDDEEFF00')
s1 = smp_s1(k, r1, r2)
s1_exp = get_bytes_from_big_eddian_string('9a1fe1f0e8b0f49b5b4216ae796da062')
print_result_with_exp(s1 == s1_exp)
def smp_aes_cmac_test():
print_header("smp_aes_cmac_test")
print_header("D.1 AES-CMAC RFC4493 TEST VECTORS")
K = get_bytes_from_big_eddian_string('2b7e1516 28aed2a6 abf71588 09cf4f3c')
K1, K2 = smp_aes_cmac_generate_subkey(K, True)
K1_exp = get_bytes_from_big_eddian_string('fbeed618 35713366 7c85e08f 7236a8de')
K2_exp = get_bytes_from_big_eddian_string('f7ddac30 6ae266cc f90bc11e e46d513b')
print_result_with_exp(K1 == K1_exp)
print_result_with_exp(K2 == K2_exp)
print_header("D.1.1 Example 1: Len = 0")
M = get_bytes_from_big_eddian_string('')
aes_cmac = smp_aes_cmac(K, M, True)
aes_cmac_exp = get_bytes_from_big_eddian_string('bb1d6929 e9593728 7fa37d12 9b756746')
print_result_with_exp(aes_cmac == aes_cmac_exp)
print_header("D.1.2 Example 2: Len = 16")
M = get_bytes_from_big_eddian_string('6bc1bee2 2e409f96 e93d7e11 7393172a')
aes_cmac = smp_aes_cmac(K, M, True)
aes_cmac_exp = get_bytes_from_big_eddian_string('070a16b4 6b4d4144 f79bdd9d d04a287c')
print_result_with_exp(aes_cmac == aes_cmac_exp)
print_header("D.1.3 Example 3: Len = 40")
M = get_bytes_from_big_eddian_string('6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411')
aes_cmac = smp_aes_cmac(K, M, True)
aes_cmac_exp = get_bytes_from_big_eddian_string('dfa66747 de9ae630 30ca3261 1497c827')
print_result_with_exp(aes_cmac == aes_cmac_exp)
print_header("D.1.4 Example 4: Len = 64")
M = get_bytes_from_big_eddian_string('6bc1bee2 2e409f96 e93d7e11 7393172a ae2d8a57 1e03ac9c 9eb76fac 45af8e51 30c81c46 a35ce411 e5fbc119 1a0a52ef f69f2445 df4f9b17 ad2b417b e66c3710')
aes_cmac = smp_aes_cmac(K, M, True)
aes_cmac_exp = get_bytes_from_big_eddian_string('51f0bebf 7e3b9d92 fc497417 79363cfe')
print_result_with_exp(aes_cmac == aes_cmac_exp)
def smp_f4_test():
print_header("smp_f4_test")
print_header("D.2 f4 LE SC CONFIRM VALUE GENERATION FUNCTION")
U = get_bytes_from_big_eddian_string('20b003d2 f297be2c 5e2c83a7 e9f9a5b9 eff49111 acf4fddb cc030148 0e359de6')
V = get_bytes_from_big_eddian_string('55188b3d 32f6bb9a 900afcfb eed4e72a 59cb9ac2 f19d7cfb 6b4fdd49 f47fc5fd')
X = get_bytes_from_big_eddian_string('d5cb8454 d177733e ffffb2ec 712baeab')
Z = get_bytes_from_big_eddian_string('00')
f4 = smp_f4(U, V, X, Z)
f4_exp = get_bytes_from_big_eddian_string('f2c916f1 07a9bd1c f1eda1be a974872d')
print_result_with_exp(f4 == f4_exp)
def smp_f5_test():
print_header("smp_f5_test")
print_header("D.3 f5 LE SC KEY GENERATION FUNCTION")
W = get_bytes_from_big_eddian_string('ec0234a3 57c8ad05 341010a6 0a397d9b 99796b13 b4f866f1 868d34f3 73bfa698')
N1 = get_bytes_from_big_eddian_string('d5cb8454 d177733e ffffb2ec 712baeab')
N2 = get_bytes_from_big_eddian_string('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf')
A1 = get_bytes_from_big_eddian_string('00561237 37bfce')
A2 = get_bytes_from_big_eddian_string('00a71370 2dcfc1')
MacKey, LTK = smp_f5(W, N1, N2, A1, A2)
MacKey_exp = get_bytes_from_big_eddian_string('2965f176 a1084a02 fd3f6a20 ce636e20')
print_result_with_exp(MacKey == MacKey_exp)
LTK_exp = get_bytes_from_big_eddian_string('69867911 69d7cd23 980522b5 94750a38')
print_result_with_exp(LTK == LTK_exp)
def smp_f6_test():
print_header("smp_f6_test")
print_header("D.4 f6 LE SC CHECK VALUE GENERATION FUNCTION")
W = get_bytes_from_big_eddian_string('2965f176 a1084a02 fd3f6a20 ce636e20')
N1 = get_bytes_from_big_eddian_string('d5cb8454 d177733e ffffb2ec 712baeab')
N2 = get_bytes_from_big_eddian_string('a6e8e7cc 25a75f6e 216583f7 ff3dc4cf')
R = get_bytes_from_big_eddian_string('12a3343b b453bb54 08da42d2 0c2d0fc8')
IOcap = get_bytes_from_big_eddian_string('010102')
A1 = get_bytes_from_big_eddian_string('00561237 37bfce')
A2 = get_bytes_from_big_eddian_string('00a71370 2dcfc1')
f6 = smp_f6(W, N1, N2, R, IOcap, A1, A2)