sysmo_usimsjs1.py: define algos as tuple, print numbers with the names

Collapse two functions of if-cascades into a data structure matching algo
numbers with string names. Use dicts produced from the initial tuple defining
the relation, to re-implement the functions.

When printing out algos, also print out the numbers set on the USIM.

This prepares for printing a list of available algos by commandline switch.
This commit is contained in:
Neels Hofmeyr 2017-12-14 05:22:49 +01:00
parent 9cf40ef497
commit bb7e45f436
1 changed files with 27 additions and 42 deletions

View File

@ -250,45 +250,28 @@ def sysmo_usim_admin_auth(sim, adm1, force = False):
return rc return rc
def sysmo_usim_algo_to_str(alg): sysmo_usim_algorithms = (
if alg == 1: (1, 'MILENAGE'),
return 'MILENAGE' (3, 'COMP128v1'),
elif alg == 3: (4, 'XOR-2G'),
return 'COMP128v1' (5, 'GBA'),
elif alg == 4: (6, 'COMP128v2'),
return 'XOR-2G' (7, 'COMP128v3'),
elif alg == 5: (8, 'XOR-3G'),
return 'GBA' (9, 'CIS-B'),
elif alg == 6: )
return 'COMP128v2'
elif alg == 7:
return 'COMP128v3'
elif alg == 8:
return 'XOR-3G'
elif alg == 9:
return 'CIS-B'
else:
return 'INVALID'
def sysmo_usim_str_to_algo(alg): sysmo_usim_algorithms_dict_by_nr = dict(sysmo_usim_algorithms)
if alg == 'MILENAGE' or alg == '1': sysmo_usim_algorithms_dict_by_name = dict([(name.upper(), nr) for nr, name in sysmo_usim_algorithms])
return 1
elif alg == 'COMP128v1' or alg == '3': def sysmo_usim_algo_to_str(alg_nr):
return 3 return sysmo_usim_algorithms_dict_by_nr.get(alg_nr) or 'INVALID'
elif alg == 'XOR-2G' or alg == '4':
return 4 def sysmo_usim_str_to_algo(alg_str):
elif alg == 'GBA' or alg == '5': alg_nr = sysmo_usim_algorithms_dict_by_name.get(alg_str.upper())
return 5 if alg_nr is None:
elif alg == 'COMP128v2' or alg == '6': raise ValueError('Unknown Algorithm %s' % alg_str)
return 6 return alg_nr
elif alg == 'COMP128v3' or alg == '7':
return 7
elif alg == 'XOR-3G' or alg == '8':
return 8
elif alg == 'CIS-B' or alg == '9':
return 9
else:
raise ValueError('Unknown Algorithm %s', alg)
# Show current athentication parameters # Show current athentication parameters
# (Which algorithim is used for which rat?) # (Which algorithim is used for which rat?)
@ -300,9 +283,11 @@ def sysmo_usim_show_auth_params(sim):
sim.select(SYSMO_USIMSJS1_EF_AUTH) sim.select(SYSMO_USIMSJS1_EF_AUTH)
res = sim.read_binary(0x02) res = sim.read_binary(0x02)
algo_2g, algo_3g = res.apdu[:2]
print " * Current algorithm setting:" print " * Current algorithm setting:"
print " 2G: " + sysmo_usim_algo_to_str(res.apdu[0]) print " 2G: %d=%s" % (algo_2g, sysmo_usim_algo_to_str(algo_2g))
print " 3G: " + sysmo_usim_algo_to_str(res.apdu[1]) print " 3G: %d=%s" % (algo_3g, sysmo_usim_algo_to_str(algo_3g))
# Program new authentication parameters # Program new authentication parameters
@ -311,8 +296,8 @@ def sysmo_usim_write_auth_params(sim, algo_2g_str, algo_3g_str):
algo_3g = sysmo_usim_str_to_algo(algo_3g_str) algo_3g = sysmo_usim_str_to_algo(algo_3g_str)
print " * New algorithm setting:" print " * New algorithm setting:"
print " 2G: " + sysmo_usim_algo_to_str(algo_2g) print " 2G: %d=%s" % (algo_2g, sysmo_usim_algo_to_str(algo_2g))
print " 3G: " + sysmo_usim_algo_to_str(algo_3g) print " 3G: %d=%s" % (algo_3g, sysmo_usim_algo_to_str(algo_3g))
sysmo_usim_init(sim) sysmo_usim_init(sim)