Compare commits

...

5 Commits

Author SHA1 Message Date
Philipp Maier 12ce2512be sysmo_isim_sja2: Add comments to explain sysmo_isimsjax_algorithms
Related: OS#6473
2023-06-23 09:56:29 +02:00
Philipp Maier 0a2aa28f4e sysmo_isim_sja2: define return types for __str__ and encode methods
Related: OS#6473
2023-06-23 09:56:16 +02:00
Philipp Maier e3d250b805 sysmo_isim_sja2: fix file naming in comment
Related: SYS#6473
2023-06-23 09:55:52 +02:00
Philipp Maier 93facf5eda sysmo_isim_sja2: update copyright header
Related: SYS#6473
2023-06-23 09:55:35 +02:00
Philipp Maier 359d8ef645 sysmo_isim_sja2: cosmetic: remove unnecessary new lines
Related: SYS#6473
2023-06-23 09:55:24 +02:00
1 changed files with 18 additions and 49 deletions

View File

@ -2,9 +2,9 @@
# -*- coding: utf-8 -*-
"""
Gadgets to modify sysmoISIM-SJA2 parameters
Gadgets to modify sysmoISIM-SJA2/sysmoISIM-SJA5 parameters
(C) 2017-2022 by sysmocom - s.f.m.c. GmbH
(C) 2017-2023 by sysmocom - s.f.m.c. GmbH
All Rights Reserved
Author: Philipp Maier
@ -40,15 +40,15 @@ import math
# |
# +--[ADF_USIM]
# | |
# | +--[USIM_AUTH_KEY 0xAF20] (regular file)
# | +--[EF_USIM_AUTH_KEY 0xAF20] (regular file)
# | |
# | +--[EF_USIM_AUTH_KEY_2G 0xAF22] (link to DF_SYSTEM/EF_SIM_AUTH_KEY)
# |
# +--[ADF_ISIM]
# |
# +--[USIM_AUTH_KEY 0xAF20] (regular file)
# +--[EF_ISIM_AUTH_KEY 0xAF20] (regular file)
# |
# +--[EF_USIM_AUTH_KEY_2G 0xAF22] (link to DF_SYSTEM/EF_SIM_AUTH_KEY)
# +--[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.
@ -74,6 +74,7 @@ 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'),
@ -83,6 +84,8 @@ sysmo_isimsja2_algorithms = [
(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'),
@ -102,18 +105,15 @@ class SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY:
def __init__(self, content = None):
if content == None:
return
header = content[0]
self.algo = header & 0x0F
self.use_opc = bool((header >> 4) & 1)
if (header >> 5) & 1:
self.sres_dev_func = 2
else:
self.sres_dev_func = 1
def __str__(self):
def __str__(self) -> str:
dump = ""
pfx = " "
@ -130,8 +130,7 @@ class SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY:
return dump
def encode(self):
def encode(self) -> list:
out = [0x00]
out[0] = self.algo & 0x0F
if self.use_opc == True:
@ -153,8 +152,7 @@ class SYSMO_ISIMSJA2_FILE_EF_SIM_AUTH_KEY(SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY):
self.key = content[1:17]
self.opc = content[17:33]
def __str__(self):
def __str__(self) -> str:
dump = ""
pfx = " "
@ -176,7 +174,7 @@ class SYSMO_ISIMSJA2_FILE_EF_SIM_AUTH_KEY(SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY):
return dump
def encode(self):
def encode(self) -> list:
out = SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY.encode(self)
out += self.key + self.opc
return out
@ -204,8 +202,7 @@ class SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY(SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY)
if len(content) > 17:
self.opc = content[17:33]
def __str__(self):
def __str__(self) -> str:
dump = ""
pfx = " "
@ -237,7 +234,7 @@ class SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY(SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY)
return dump
def encode(self):
def encode(self) -> list:
out = SYSMO_ISIMSJA2_FILE_EF_XSIM_AUTH_KEY.encode(self)
if self.full_res == True:
out[0] |= 1 << 6
@ -278,7 +275,6 @@ class SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG:
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
@ -295,8 +291,7 @@ class SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG:
self.C4 = content[53:69]
self.C5 = content[69:85]
def __str__(self):
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"
@ -309,8 +304,7 @@ class SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG:
dump += " C5: " + hexdump(self.C5)
return dump
def encode(self):
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
@ -376,11 +370,9 @@ class SYSMO_ISIMSJA2_FILE_EF_USIM_SQN:
self.age_limit = list_to_int(content[8:14])
self.freshness_data = content[15:(6*2**self.ind_size_bits)]
def __str__(self):
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
@ -413,11 +405,9 @@ class SYSMO_ISIMSJA2_FILE_EF_USIM_SQN:
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):
def encode(self) -> list:
out = [0x00, 0x00]
# Flag1:
@ -445,12 +435,10 @@ class SYSMO_ISIMSJA2_FILE_EF_USIM_SQN:
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
@ -469,7 +457,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
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"
@ -494,12 +481,10 @@ class Sysmo_isim_sja2(Sysmo_usim):
if card_detected == True:
return
# Exit when we are not able to detect the card
if card_detected != True:
sys.exit(1)
# Show current milenage parameters
def show_milenage_params(self):
print("Reading Milenage parameters...")
@ -515,10 +500,8 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(str(ef))
print("")
# Write new milenage parameters
def write_milenage_params(self, params):
print("Programming Milenage parameters...")
if (len(params) < 85):
@ -545,14 +528,12 @@ class Sysmo_isim_sja2(Sysmo_usim):
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):
@ -567,7 +548,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
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
@ -644,7 +624,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" * ADF_ISIM/EF_ISIM_SQN:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu))
def show_key_params(self):
"""
Show current Key value
@ -662,7 +641,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" Key: " + hexdump(ef.key))
print("")
def write_key_params(self, key):
"""
Program new Key value
@ -672,7 +650,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" * New Key setting:")
print(" Key: " + hexdump(key))
print(" * Programming...")
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY_2G(res.apdu)
@ -694,7 +671,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print("")
# Show current athentication parameters
# (Which algorithim is used for which rat?)
def show_auth_params(self):
@ -717,7 +693,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" 3G: %d=%s" % (algo_3g, id_to_str(self.algorithms, algo_3g)))
print("")
# Program new authentication parameters
def write_auth_params(self, algo_2g_str, algo_3g_str):
print("Programming Authentication parameters...")
@ -760,7 +735,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print("")
# Show current OPc value
def show_opc_params(self):
print("Reading OP/c value...")
@ -781,7 +755,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" %s: %s" % (mode_str, hexdump(ef.opc)))
print("")
# Program new OPc value
def write_opc_params(self, select, op):
if select:
@ -796,7 +769,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" %s: %s" % (mode_str, hexdump(op)))
print(" * Programming...")
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
ef = SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY_2G(res.apdu)
@ -821,7 +793,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print("")
# Show current milenage SQN parameters
def show_milenage_sqn_params(self):
print("Reading Milenage Sequence parameters...")
@ -844,7 +815,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print("")
# Reset milenage SQN configuration
def reset_milenage_sqn_params(self):
print(" * Resetting SQN Configuration to defaults...")
@ -866,7 +836,6 @@ class Sysmo_isim_sja2(Sysmo_usim):
print("")
class Sysmo_isim_sja5(Sysmo_isim_sja2):
algorithms = sysmo_isimsja5_algorithms