-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsysmo_isim_sja2.py
1281 lines (1080 loc) · 38.6 KB
/
sysmo_isim_sja2.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Gadgets to modify sysmoISIM-SJA2/sysmoISIM-SJA5 parameters
(C) 2017-2023 by sysmocom - s.f.m.c. GmbH
All Rights Reserved
Author: Philipp Maier
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from utils import *
from sysmo_usim import *
import math
# Partial File tree:
# The following tree is incomplete, it just contains the propritary files we
# need to perform the tasks implemented below:
#
# [MF 0x3F00]
# |
# +--[DF_SYSTEM 0xA515]
# | |
# | +--[EF_SIM_AUTH_KEY 0x6F20] (regular file)
# |
# +--[ADF_USIM]
# | |
# | +--[EF_USIM_AUTH_KEY 0xAF20] (regular file)
# | |
# | +--[EF_USIM_AUTH_KEY_2G 0xAF22] (link to DF_SYSTEM/EF_SIM_AUTH_KEY)
# |
# +--[ADF_ISIM]
# |
# +--[EF_ISIM_AUTH_KEY 0xAF20] (regular file)
# |
# +--[EF_ISIM_AUTH_KEY_2G 0xAF22] (link to DF_SYSTEM/EF_SIM_AUTH_KEY)
#
# Note: EF_MILENAGE_CFG and EF_USIM_SQN not yet listed here.
# Propritary files
SYSMO_ISIMSJA2_DF_SYSTEM = [0xA5, 0x15]
SYSMO_ISIMSJA2_EF_SIM_AUTH_KEY = [0x6F, 0x20] # DF_SYSTEM
SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY = [0xAF, 0x20] # ADF.USIM
SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY_2G = [0xAF, 0x22] # ADF.USIM
SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY_GBA = [0xAF, 0x23] # ADF.USIM
SYSMO_ISIMSJA2_EF_MILENAGE_CFG = [0xAF, 0x21] # ADF.USIM
SYSMO_ISIMSJA2_EF_USIM_SQN = [0xAF, 0x30] # ADF.USIM
SYSMO_ISIMSJA2_EF_GBA_SK = [0xAF, 0x31] # ADF.USIM
SYSMO_ISIMSJA2_EF_GBA_REC_LIST = [0xAF, 0x32] # ADF.USIM
SYSMO_ISIMSJA2_EF_GBA_INT_KEY = [0xAF, 0x32] # ADF.USIM
# Authentication algorithms
SYSMO_ISIMSJA2_ALGO_COMP12V1 = 0x01
SYSMO_ISIMSJA2_ALGO_COMP12V2 = 0x02
SYSMO_ISIMSJA2_ALGO_COMP12V3 = 0x03
SYSMO_ISIMSJA2_ALGO_MILENAGE = 0x04
SYSMO_ISIMSJA2_ALGO_SHA1AKA = 0x05
SYSMO_ISIMSJA5_ALGO_TUAK = 0x06
SYSMO_ISIMSJA5_ALGO_XOR_2G = 0x0E
SYSMO_ISIMSJA2_ALGO_XOR = 0x0F
# Algorithms that are supported by sysmo-isim-sja2 (and also sysmo-isim-sja5)
sysmo_isimsja2_algorithms = [
(SYSMO_ISIMSJA2_ALGO_COMP12V1, 'COMP128v1'),
(SYSMO_ISIMSJA2_ALGO_COMP12V2, 'COMP128v2'),
(SYSMO_ISIMSJA2_ALGO_COMP12V3, 'COMP128v3'),
(SYSMO_ISIMSJA2_ALGO_MILENAGE, 'MILENAGE'),
(SYSMO_ISIMSJA2_ALGO_SHA1AKA , 'SHA1-AKA'),
(SYSMO_ISIMSJA2_ALGO_XOR, 'XOR'),
]
# Algorithms that are supported by sysmo-isim-sja5. This also includes all
# algorithms supported by sysmo-isim-sja2y
sysmo_isimsja5_algorithms = sysmo_isimsja2_algorithms + [
(SYSMO_ISIMSJA5_ALGO_XOR_2G, 'XOR-2G'),
(SYSMO_ISIMSJA5_ALGO_TUAK, 'TUAK'),
]
# Algorithms that use a 16 byte Key in the familiar format of sysmo-isim-sja2
sysmo_isimsjax_16_byte_key_algorithms = [
SYSMO_ISIMSJA2_ALGO_COMP12V1,
SYSMO_ISIMSJA2_ALGO_COMP12V2,
SYSMO_ISIMSJA2_ALGO_COMP12V3,
SYSMO_ISIMSJA2_ALGO_MILENAGE,
SYSMO_ISIMSJA2_ALGO_SHA1AKA,
SYSMO_ISIMSJA2_ALGO_XOR,
SYSMO_ISIMSJA5_ALGO_XOR_2G,
]
# TUAK configuration byte
SYSMO_ISIMSJA5_TUAK_RES_SIZE_32_BIT = 0
SYSMO_ISIMSJA5_TUAK_RES_SIZE_64_BIT = 1
SYSMO_ISIMSJA5_TUAK_RES_SIZE_128_BIT = 2
SYSMO_ISIMSJA5_TUAK_RES_SIZE_256_BIT = 3
SYSMO_ISIMSJA5_TUAK_MAC_SIZE_64_BIT = 0
SYSMO_ISIMSJA5_TUAK_MAC_SIZE_128_BIT = 1
SYSMO_ISIMSJA5_TUAK_MAC_SIZE_256_BIT = 2
SYSMO_ISIMSJA5_TUAK_CKIK_SIZE_128_BIT = 0
SYSMO_ISIMSJA5_TUAK_CKIK_SIZE_256_BIT = 1
sysmo_isimsja5_res_sizes = [
(SYSMO_ISIMSJA5_TUAK_RES_SIZE_32_BIT, "32"),
(SYSMO_ISIMSJA5_TUAK_RES_SIZE_64_BIT, "64"),
(SYSMO_ISIMSJA5_TUAK_RES_SIZE_128_BIT, "128"),
(SYSMO_ISIMSJA5_TUAK_RES_SIZE_256_BIT, "256")
]
sysmo_isimsja5_mac_sizes = [
(SYSMO_ISIMSJA5_TUAK_MAC_SIZE_64_BIT, "64"),
(SYSMO_ISIMSJA5_TUAK_MAC_SIZE_128_BIT, "128"),
(SYSMO_ISIMSJA5_TUAK_MAC_SIZE_256_BIT, "256")
]
sysmo_isimsja5_ckik_sizes = [
(SYSMO_ISIMSJA5_TUAK_CKIK_SIZE_128_BIT, "128"),
(SYSMO_ISIMSJA5_TUAK_CKIK_SIZE_256_BIT, "256")
]
sysmo_isimsjax_op_opc = [
(True, 'OPc'),
(False, 'OP'),
]
sysmo_isimsja5_top_topc = [
(True, 'TOPc'),
(False, 'TOP'),
]
class SYSMO_ISIMSJAX_ALGO_PARS_MILENAGE:
use_opc = False
sres_dev_func = 1
four_byte_res = 0 #sysmo-usim-sja5 only
def __init__(self, content = None):
if content == None:
return
header = content[0]
self.use_opc = bool((header >> 4) & 1)
if (header >> 5) & 1:
self.sres_dev_func = 2
self.four_byte_res = bool((header >> 6) & 1)
def __str__(self) -> str:
dump = ""
pfx = " "
if self.use_opc == True:
dump += pfx + "use OPc\n"
else:
dump += pfx + "use OP\n"
dump += pfx + "use SRES deviation function " + str(self.sres_dev_func) + "\n"
if self.four_byte_res:
dump += pfx + "Return 4 byte RES\n"
else:
dump += pfx + "Return full 8 byte RES\n"
return dump
def encode(self) -> int:
out = 0x00
if self.use_opc == True:
out |= 1 << 4
out |= ((self.sres_dev_func-1) & 1) << 5
out |= ((self.four_byte_res) & 1) << 6
return out
class SYSMO_ISIMSJAX_ALGO_PARS_SHA1AKA:
four_byte_res = 0 #sysmo-usim-sja5 only
def __init__(self, content = None):
if content == None:
return
header = content[0]
self.four_byte_res = bool((header >> 6) & 1)
def __str__(self) -> str:
dump = ""
pfx = " "
if self.four_byte_res:
dump += pfx + "Return 4 byte RES\n"
else:
dump += pfx + "Return full 8 byte RES (default)\n"
return dump
def encode(self) -> int:
out = 0x00
out |= ((self.four_byte_res) & 1) << 6
return out
class SYSMO_ISIMSJAX_ALGO_PARS_XOR:
sres_dev_func = 1
four_byte_res = 0
sixteen_byte_res = 0 #Return 16 byte RES (ignores full_res)
def __init__(self, content = None):
if content == None:
return
header = content[0]
if (header >> 5) & 1:
self.sres_dev_func = 2
self.four_byte_res = bool((header >> 6) & 1)
self.sixteen_byte_res = bool((header >> 7) & 1)
def __str__(self) -> str:
dump = ""
pfx = " "
dump += pfx + "use SRES deviation function" + str(self.sres_dev_func) + "\n"
if self.sixteen_byte_res:
dump += pfx + "Return extended 16 byte RES\n"
elif self.four_byte_res:
dump += pfx + "Return 4 byte RES\n"
else:
dump += pfx + "Return full 8 byte RES (default)\n"
return dump
def encode(self) -> int:
out = 0x00
out |= ((self.sres_dev_func-1) & 1) << 5
out |= ((self.four_byte_res) & 1) << 6
out |= ((self.sixteen_byte_res) & 1) << 7
return out
class SYSMO_ISIMSJA5_ALGO_PARS_TUAK:
use_topc = False
sres_dev_func = 1
use_256_bit_key = False
def __init__(self, content = None):
if content == None:
return
header = content[0]
self.use_topc = bool((header >> 4) & 1)
if (header >> 5) & 1:
self.sres_dev_func = 2
self.use_256_bit_key = bool((header >> 6) & 1)
def __str__(self) -> str:
dump = ""
pfx = " "
if self.use_topc == True:
dump += pfx + "use TOPc\n"
else:
dump += pfx + "use TOP\n"
dump += pfx + "use SRES deviation function " + str(self.sres_dev_func) + "\n"
if self.use_256_bit_key:
dump += pfx + "256 bit key length\n"
else:
dump += pfx + "128 bit key length\n"
return dump
def encode(self) -> int:
out = 0x00
if self.use_topc == True:
out |= 1 << 4
out |= ((self.sres_dev_func-1) & 1) << 5
out |= ((self.use_256_bit_key) & 1) << 6
return out
class SYSMO_ISIMSJAX_FILE_EF_XSIM_AUTH_KEY:
"""
Superclass model that generates and parses the header byte of
SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY, SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY_2G
and SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY_GBA.
"""
algo = SYSMO_ISIMSJA2_ALGO_COMP12V1
algo_pars = None
def __init__(self, content = None):
if content == None:
return
header = content[0]
self.algo = header & 0x0F
if self.algo == SYSMO_ISIMSJA2_ALGO_MILENAGE:
self.algo_pars = SYSMO_ISIMSJAX_ALGO_PARS_MILENAGE(content)
elif self.algo == SYSMO_ISIMSJA2_ALGO_SHA1AKA:
self.algo_pars = SYSMO_ISIMSJAX_ALGO_PARS_SHA1AKA(content)
elif self.algo == SYSMO_ISIMSJA2_ALGO_XOR:
self.algo_pars = SYSMO_ISIMSJAX_ALGO_PARS_XOR(content)
elif self.algo == SYSMO_ISIMSJA5_ALGO_TUAK:
self.algo_pars = SYSMO_ISIMSJA5_ALGO_PARS_TUAK(content)
def __str__(self) -> str:
dump = ""
pfx = " "
dump += pfx + "Algorithm: "
dump += id_to_str(sysmo_isimsja5_algorithms, self.algo)
dump += "\n"
if self.algo_pars:
dump += str(self.algo_pars)
return dump
def encode(self):
out = [0x00]
out[0] = self.algo & 0x0F
if self.algo_pars:
out[0] |= self.algo_pars.encode()
return out
class SYSMO_ISIMSJAX_ALGO_KEY_COMP128:
ki = [0x00] * 16
def __init__(self, content = None):
if content == None:
return
self.ki = content[1:17]
def __str__(self) -> str:
dump = ""
pfx = " "
dump += pfx + "Ki: " + hexdump(self.ki)
return dump
def encode(self) -> list:
return self.ki
#XOR has the same key length COMP128 (16 byte, no extra data)
class SYSMO_ISIMSJAX_ALGO_KEY_XOR(SYSMO_ISIMSJAX_ALGO_KEY_COMP128):
pass
#SHA1AKA has the same key length COMP128 (16 byte, no extra data)
class SYSMO_ISIMSJAX_ALGO_KEY_SHA1AKA(SYSMO_ISIMSJAX_ALGO_KEY_COMP128):
pass
#Milenage adds a 16 byte OP/c
class SYSMO_ISIMSJAX_ALGO_KEY_MILENAGE(SYSMO_ISIMSJAX_ALGO_KEY_COMP128):
opc = [0x00] * 16
def __init__(self, content = None):
if content == None:
return
super().__init__(content)
self.opc = content[17:33]
def __str__(self) -> str:
dump = ""
pfx = " "
dump += super().__str__()
dump += "\n"
dump += pfx + "OPc: " + hexdump(self.opc)
return dump
def encode(self) -> list:
return super().encode() + self.opc
class SYSMO_ISIMSJAX_ALGO_KEY_TUAK:
res_size = 0 #3 bit value
mac_size = 0 #3 bit value
ckik_size = 0 #1 bit value
num_keccak = 0 #1 byte value
topc = [0x00] * 32
key = [0x00] * 32
def __init__(self, content = None):
if content == None:
return
self.res_size = int(content[1] & 7)
self.mac_size = int((content[1] >> 3) & 7)
self.ckik_size = bool((content[1] >> 6) & 1)
self.num_keccak = content[2]
self.topc = content[3:35]
self.key = content[35:67]
def __str__(self) -> str:
dump = ""
pfx = " "
dump += pfx + "RES size: %s bit" % id_to_str(sysmo_isimsja5_res_sizes, self.res_size) + "\n"
dump += pfx + "MAC-A/MAC-S size: %s bit" % id_to_str(sysmo_isimsja5_mac_sizes, self.mac_size) + "\n"
dump += pfx + "Keccak iterations: %d" % self.num_keccak + "\n"
dump += pfx + "TOPc: " + hexdump(self.topc) + "\n"
#TODO: Keys may be 128 or 256 bits long. The key length is defined
#in the header of the file, which means we cannot access this bit
#from here but it would be nice to display the key in its correct
#length though.
dump += pfx + "Key: " + hexdump(self.key)
return dump
def encode(self) -> list:
param_byte = self.res_size & 7
param_byte |= (self.res_size & 7) << 3
param_byte |= (self.ckik_size & 1) << 6
out = [param_byte]
out += [self.num_keccak]
out += self.topc
out += self.key
return out
class SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(SYSMO_ISIMSJAX_FILE_EF_XSIM_AUTH_KEY):
algo_key = None
def __init__(self, content = None):
# The superclass constructor must ensure that a valid algo and
# algo parameters are set since we need this information to pick
# the key configuration below.
super().__init__(content)
if self.algo == SYSMO_ISIMSJA2_ALGO_COMP12V1 or \
self.algo == SYSMO_ISIMSJA2_ALGO_COMP12V2 or \
self.algo == SYSMO_ISIMSJA2_ALGO_COMP12V3:
self.algo_key = SYSMO_ISIMSJAX_ALGO_KEY_COMP128(content)
elif self.algo == SYSMO_ISIMSJA2_ALGO_MILENAGE:
self.algo_key = SYSMO_ISIMSJAX_ALGO_KEY_MILENAGE(content)
elif self.algo == SYSMO_ISIMSJA2_ALGO_SHA1AKA:
self.algo_key = SYSMO_ISIMSJAX_ALGO_KEY_SHA1AKA(content)
elif self.algo == SYSMO_ISIMSJA2_ALGO_XOR or \
self.algo == SYSMO_ISIMSJA5_ALGO_XOR_2G:
self.algo_key = SYSMO_ISIMSJAX_ALGO_KEY_XOR(content)
elif self.algo == SYSMO_ISIMSJA5_ALGO_TUAK:
self.algo_key = SYSMO_ISIMSJAX_ALGO_KEY_TUAK(content)
def __str__(self) -> str:
dump = ""
dump += super().__str__()
dump += str(self.algo_key)
return dump
def encode(self) -> list:
out = super().encode()
if self.algo_key:
out += self.algo_key.encode()
else:
raise ValueError("key data encoding not supported for selected algorithm!")
return out
# EF_USIM_AUTH_KEY_2G, EF_SIM_AUTH_KEY and EF_USIM_AUTH_KEY_GBA have the same layout as
# EF_USIM_AUTH_KEY, so there is nothing to specialize other than the class name
class SYSMO_ISIMSJA2_FILE_EF_SIM_AUTH_KEY(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY):
pass
class SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY_2G(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY):
pass
class SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY_GBA(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY):
pass
class SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG:
R1 = 0x40
R2 = 0x00
R3 = 0x20
R4 = 0x40
R5 = 0x60
C1 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
C2 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
C3 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02]
C4 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04]
C5 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08]
def __init__(self, content = None):
if content == None:
return
if len(content) != 85:
return
self.R1 = content[0]
self.R2 = content[1]
self.R3 = content[2]
self.R4 = content[3]
self.R5 = content[4]
self.C1 = content[5:5+16]
self.C2 = content[21:37]
self.C3 = content[37:53]
self.C4 = content[53:69]
self.C5 = content[69:85]
def __str__(self) -> str:
dump = " R1: " + str(hex(self.R1)) + "\n"
dump += " R2: " + str(hex(self.R2)) + "\n"
dump += " R3: " + str(hex(self.R3)) + "\n"
dump += " R4: " + str(hex(self.R4)) + "\n"
dump += " R5: " + str(hex(self.R5)) + "\n"
dump += " C1: " + hexdump(self.C1) + "\n"
dump += " C2: " + hexdump(self.C2) + "\n"
dump += " C3: " + hexdump(self.C3) + "\n"
dump += " C4: " + hexdump(self.C4) + "\n"
dump += " C5: " + hexdump(self.C5)
return dump
def encode(self) -> list:
out = [self.R1, self.R2, self.R3, self.R4, self.R5]
out += self.C1 + self.C2 + self.C3 + self.C4 + self.C5
return out
class SYSMO_ISIMSJAX_FILE_EF_USIM_SQN:
# Flag1:
ind_size_bits = 5 # speficy file length by 2^ind_len
sqn_check_enabled = True # perform SQN checks below
sqn_age_limit_enabled = False # perform age limit check: (SQNms-SQN) <= AGE_LIMIT)
sqn_max_delta_enabled = True # perform delta max check: (SWN-SQNms) <= DELTA MAX)
sqn_check_skip_first = True # accept any SQN on the first authentication
# Flag2:
conceal_autn = True # Conceal the value of AUTN
conceal_auts = True # Conceal the value of AUTS
no_amf_clear = False # Do not clear AMF when computing MAC-S
# Data:
max_delta = 2**28 << ind_size_bits
age_limit = 2**28 << ind_size_bits
freshness_data = [0x00] * (6*2**ind_size_bits) # initalize to zero
def __init__(self, content = None):
if content == None:
return
# Check if we have at least the header
if len(content) <= 2:
raise ValueError("unexpected length of %u bytes", len(content))
flag1 = content[0]
self.ind_size_bits = flag1 & 0xf
# The parameter ind_size_bits is not user configurable,
# its a fixed configuration that is specific to the
# card profile and it can be determined by looking at the
# file length (length of the freshness data). If we find
# an ind_size_bits that is intconstant to the file length,
# we automatically set the value to the correct length
ind_size_bits_calculated = int(math.log((len(content) - 14) / 6, 2))
if ind_size_bits_calculated != self.ind_size_bits:
print(" Warning: SQN Parameter ind_size_bits is set to " + str(self.ind_size_bits) + ", resetting it to " + str(ind_size_bits_calculated) + "!")
self.ind_size_bits = ind_size_bits_calculated
self.reset() #ensure freshness data is correctly reset
self.sqn_check_enabled = bool((flag1 >> 4) & 1)
self.sqn_age_limit_enabled = bool((flag1 >> 5) & 1)
self.sqn_max_delta_enabled = bool((flag1 >> 6) & 1)
self.sqn_check_skip_first = bool((flag1 >> 7) & 1)
flag2 = content[1]
self.conceal_autn = bool(flag2 & 1)
self.conceal_auts = bool((flag2 >> 1) & 1)
self.no_amf_clear = bool((flag2 >> 2) & 1)
# Check if the data body is complete
if len(content) < 14+(6*2**self.ind_size_bits):
raise ValueError("unexpected length of %u bytes" % len(content))
self.max_delta = list_to_int(content[2:8])
self.age_limit = list_to_int(content[8:14])
self.freshness_data = content[15:(6*2**self.ind_size_bits)]
def __str__(self) -> str:
pfx = " "
dump = ""
dump += "%sIND (bits): %u\n" % (pfx, self.ind_size_bits)
if self.sqn_check_enabled:
dump += "%sSQN Check enabled\n" % pfx
else:
dump += "%sSQN Check disabled\n" % pfx
if self.sqn_age_limit_enabled:
dump += "%sSQN Age Limit enabled\n" % pfx
else:
dump += "%sSQN Age Limit disabled\n" % pfx
if self.sqn_max_delta_enabled:
dump += "%sSQN Max Delta enabled\n" % pfx
else:
dump += "%sSQN Max Delta disabled\n" % pfx
if self.sqn_check_skip_first:
dump += "%sSQN Skip first enabled\n" % pfx
else:
dump += "%sSQN Skip first disabled\n" % pfx
if self.conceal_autn:
dump += "%sSQN Conceal AUTN enabled\n" % pfx
else:
dump += "%sSQN Conceal AUTN disabled\n" % pfx
if self.conceal_auts:
dump += "%sSQN Conceal AUTS enabled\n" % pfx
else:
dump += "%sSQN Conceal AUTS disabled\n" % pfx
if self.no_amf_clear:
dump += "%sSQN No AMF clear enabled\n" % pfx
else:
dump += "%sSQN No AMF clear disabled\n" % pfx
dump += "%sMax Delta: %u\n" % (pfx, self.max_delta)
dump += "%sAge Limit: %u\n" % (pfx, self.age_limit)
dump += pfx + "Freshness Data:\n" + hexdump(self.freshness_data, True)
return dump
def encode(self) -> list:
out = [0x00, 0x00]
# Flag1:
out[0] = self.ind_size_bits & 0x0f
if self.sqn_check_enabled:
out[0] |= 1 << 4
if self.sqn_age_limit_enabled:
out[0] |= 1 << 5
if self.sqn_max_delta_enabled:
out[0] |= 1 << 6
if self.sqn_check_skip_first:
out[0] |= 1 << 7
# Flag2:
if self.conceal_autn:
out[1] |= 1 << 0
if self.conceal_auts:
out[1] |= 1 << 1
if self.no_amf_clear:
out[1] |= 1 << 2
# Data:
out += int_to_list(self.max_delta, 6)
out += int_to_list(self.age_limit, 6)
out += self.freshness_data
return out
def reset(self):
self.freshness_data = [0x00] * (6*2**self.ind_size_bits)
class Sysmo_isim_sja2(Sysmo_usim):
algorithms = sysmo_isimsja2_algorithms
def __init__(self):
card_detected = False
# Try card model #1
try:
atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9"
print("Trying to find card with ATR: " + atr)
Sysmo_usim.__init__(self, atr)
card_detected = True
except:
print(" * Card not detected!")
if card_detected == True:
return
# Try card model #2
try:
atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2"
print("Trying to find card with ATR: " + atr)
Sysmo_usim.__init__(self, atr)
card_detected = True
except:
print(" * Card not detected!")
if card_detected == True:
return
# Try card model #3 (sysmoTSIM)
try:
atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5"
print("Trying to find card with ATR: " + atr)
Sysmo_usim.__init__(self, atr)
card_detected = True
except:
print(" * Card not detected!")
if card_detected == True:
return
# Exit when we are not able to detect the card
if card_detected != True:
sys.exit(1)
def show_milenage_params(self):
"""
Show current milenage parameters
"""
print("Reading Milenage parameters...")
self._init()
print(" * Reading...")
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
res = self._read_binary(85)
ef = SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu)
print(" * Current Milenage Parameters:")
print(str(ef))
print("")
def write_milenage_params(self, params):
"""
Write new milenage parameters
"""
print("Programming Milenage parameters...")
if (len(params) < 85):
print("Error: Short milenage parameters!")
return
params_swapped = params[80:85] + params[0:80]
self._init()
print(" * New Milenage Parameters for (EF.MILENAGE_CFG):")
ef_milenage_cfg = SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(params_swapped)
print(str(ef_milenage_cfg))
print(" * Programming...")
# Note: The milenage configuration file in ADF_USIM and
# ADF_ISIM are linked, however we write to both locations,
# just to be sure.
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
self.sim.update_binary(ef_milenage_cfg.encode())
if self.sim.has_isim:
self.sim.card.SELECT_ADF_ISIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
self.sim.update_binary(ef_milenage_cfg.encode())
print("")
# Select DF_SYSTEM/EF_SIM_AUTH_KEY
def __select_ef_sim_auth_key(self):
self.sim.select(GSM_SIM_MF)
self.sim.select(SYSMO_ISIMSJA2_DF_SYSTEM)
self.sim.select(SYSMO_ISIMSJA2_EF_SIM_AUTH_KEY)
# Authentication keys exist in various different files, which are
# similar, thie method simplifies the selection of those files
def __select_xsim_auth_key(self, isim = False, _2G = False):
self.sim.select(GSM_SIM_MF)
if isim:
self.sim.card.SELECT_ADF_ISIM()
else:
self.sim.card.SELECT_ADF_USIM()
if _2G:
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY_2G)
else:
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_AUTH_KEY)
# In the SJA2 model the key material and the algorithm configuration
# is distributed over multiple files, which may also have redundant
# contents. Files can also be hard linked to other files so that
# changes in one file may appear in another file as well. The dump
# method provides an overview of contents of all files at once in
# order to help debugging problems
def dump(self):
print("Reading propritary files...")
self._init()
# DF_SYSTEM/EF_SIM_AUTH_KEY:
self.__select_ef_sim_auth_key()
res = self._read_binary(self.sim.filelen)
print(" * DF_SYSTEM/EF_SIM_AUTH_KEY:")
print(SYSMO_ISIMSJA2_FILE_EF_SIM_AUTH_KEY(res.apdu))
# ADF_USIM/EF_USIM_AUTH_KEY_2G:
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
print(" * ADF_USIM/EF_USIM_AUTH_KEY_2G:")
print(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY_2G(res.apdu))
if self.sim.has_isim:
# ADF_ISIM/EF_ISIM_AUTH_KEY_2G:
self.__select_xsim_auth_key(isim = True, _2G = True)
res = self._read_binary(self.sim.filelen)
print(" * ADF_ISIM/EF_ISIM_AUTH_KEY_2G:")
print(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY_2G(res.apdu))
# ADF_USIM/EF_USIM_AUTH_KEY:
self.__select_xsim_auth_key(isim = False, _2G = False)
res = self._read_binary(self.sim.filelen)
print(" * ADF_USIM/EF_USIM_AUTH_KEY:")
print(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu))
if self.sim.has_isim:
# ADF_ISIM/EF_ISIM_AUTH_KEY:
self.__select_xsim_auth_key(isim = True, _2G = False)
res = self._read_binary(self.sim.filelen)
print(" * ADF_ISIM/EF_ISIM_AUTH_KEY:")
print(SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu))
# ADF_USIM/EF_MILENAGE_CFG:
self.sim.select(GSM_SIM_MF)
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
res = self._read_binary(self.sim.filelen)
print(" * ADF_USIM/EF_MILENAGE_CFG:")
print(SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu))
if self.sim.has_isim:
# ADF_ISIM/EF_MILENAGE_CFG:
self.sim.select(GSM_SIM_MF)
self.sim.card.SELECT_ADF_ISIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
res = self._read_binary(self.sim.filelen)
print(" * ADF_ISIM/EF_MILENAGE_CFG:")
print(SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu))
# ADF_USIM/EF_USIM_SQN:
self.sim.select(GSM_SIM_MF)
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_SQN)
res = self._read_binary(self.sim.filelen)
print(" * ADF_USIM/EF_USIM_SQN:")
print(SYSMO_ISIMSJAX_FILE_EF_USIM_SQN(res.apdu))
if self.sim.has_isim:
# ADF_USIM/EF_ISIM_SQN:
self.sim.select(GSM_SIM_MF)
self.sim.card.SELECT_ADF_ISIM()
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_SQN)
res = self._read_binary(self.sim.filelen)
print(" * ADF_ISIM/EF_ISIM_SQN:")
print(SYSMO_ISIMSJAX_FILE_EF_USIM_SQN(res.apdu))
def __display_key(self, ef, gen:str):
"""
Helper method to display key
"""
if ef.algo in sysmo_isimsjax_16_byte_key_algorithms:
print(" %s: Key: %s" % (gen, hexdump(ef.algo_key.ki)))
elif ef.algo is SYSMO_ISIMSJA5_ALGO_TUAK:
if not ef.algo_pars.use_256_bit_key:
print(" %s: Key: %s" % (gen, hexdump(ef.algo_key.key[0:16])))
else:
print(" %s: Key: %s" % (gen, hexdump(ef.algo_key.key)))
else:
print(" * %s: Key not applicable for selected algorithm." % gen)
def show_key_params(self):
"""
Show current Key value
"""
print("Reading Key value...")
self._init()
print(" * Reading...")
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
ef_2g = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
self.__select_xsim_auth_key(isim = False, _2G = False)
res = self._read_binary(self.sim.filelen)
ef_3g = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
if self.sim.has_isim:
self.__select_xsim_auth_key(isim = True, _2G = False)
res = self._read_binary(self.sim.filelen)
ef_4g5g = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
else:
ef_4g5g = None
print(" * Current Key setting:")
self.__display_key(ef_2g, "2g")
self.__display_key(ef_3g, "3g")
if ef_4g5g:
self.__display_key(ef_4g5g, "4g5g")
print("")
def __program_key(self, key, gen:str):
"""
Helper method to program key, EF must be selected first
"""
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
if ef.algo in sysmo_isimsjax_16_byte_key_algorithms:
ef.algo_key.ki = key
self.sim.update_binary(ef.encode())
print(" * %s: Key programmed." % gen)
elif ef.algo is SYSMO_ISIMSJA5_ALGO_TUAK:
ef.algo_key.key = key
ef.algo_pars.use_256_bit_key = False
if len(key) > 16:
ef.algo_pars.use_256_bit_key = True
self.sim.update_binary(ef.encode())
print(" * %s: Key programmed." % gen)
else:
print(" * %s: Key not applicable for selected algorithm." % gen)
def write_key_params(self, key):
"""
Program new Key value
"""
print("Writing Key value...")
self._init()
print(" * New Key setting:")
print(" Key: " + hexdump(key))
print(" * Programming...")
self.__select_xsim_auth_key(isim = False, _2G = True)
self.__program_key(key, "2g")
self.__select_xsim_auth_key(isim = False, _2G = False)
self.__program_key(key, "3g")
if self.sim.has_isim:
self.__select_xsim_auth_key(isim = True, _2G = False)
self.__program_key(key, "4g5g")
print("")
def show_auth_params(self):
"""
Show current authentication parameters
"""
print("Reading Authentication parameters...")
self._init()
print(" * Reading...")
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY_2G(res.apdu)
algo_2g = ef.algo
self.__select_xsim_auth_key(isim = False, _2G = False)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
algo_3g = ef.algo
if self.sim.has_isim:
self.__select_xsim_auth_key(isim = True, _2G = False)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
algo_4g5g = ef.algo
else:
algo_4g5g = algo_3g
print(" * Current algorithm setting:")
print(" 2g: %d=%s" % (algo_2g, id_to_str(self.algorithms, algo_2g)))
print(" 3g: %d=%s" % (algo_3g, id_to_str(self.algorithms, algo_3g)))
print(" 4g5g: %d=%s" % (algo_3g, id_to_str(self.algorithms, algo_4g5g)))
print("")
def write_auth_params(self, algo_2g_str, algo_3g_str, algo_4g5g_str = None):
"""
Write new authentication parameters
"""
print("Programming Authentication parameters...")
self._init()
if algo_2g_str.isdigit():
algo_2g = int(algo_2g_str)
else:
algo_2g = str_to_id(self.algorithms, algo_2g_str)
if algo_3g_str.isdigit():
algo_3g = int(algo_3g_str)
else:
algo_3g = str_to_id(self.algorithms, algo_3g_str)
if algo_4g5g_str:
if algo_4g5g_str.isdigit():
algo_4g5g = int(algo_4g5g_str)
else:
algo_4g5g = str_to_id(self.algorithms, algo_4g5g_str)
else:
algo_4g5g = algo_3g
print(" * New algorithm setting:")
print(" 2g: %d=%s" % (algo_2g, id_to_str(self.algorithms, algo_2g)))
print(" 3g: %d=%s" % (algo_3g, id_to_str(self.algorithms, algo_3g)))
print(" 4g5g: %d=%s" % (algo_4g5g, id_to_str(self.algorithms, algo_4g5g)))
print(" * Programming...")
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
ef.algo = algo_2g
self.sim.update_binary(ef.encode())
self.__select_xsim_auth_key(isim = False, _2G = False)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
ef.algo = algo_3g
self.sim.update_binary(ef.encode())
if self.sim.has_isim:
self.__select_xsim_auth_key(isim = True, _2G = False)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJAX_FILE_EF_USIM_AUTH_KEY(res.apdu)
ef.algo = algo_4g5g
self.sim.update_binary(ef.encode())
print("")