mirror of https://gerrit.osmocom.org/pysim
SJA2: Implement DF.SYSTEM/EF.0348_KEY using construct
This implicitly adds support for JSON->binary encoding, not just decoding (previous code predating construct support). Change-Id: I0994d9f66a504dd3c60b43ed5cf6645515dcbc6a
This commit is contained in:
parent
ba6d6ab64f
commit
954ce95a16
|
@ -87,6 +87,26 @@ class Rpad(Adapter):
|
||||||
len(obj), self.sizeof()))
|
len(obj), self.sizeof()))
|
||||||
return obj + self.pattern * (self.sizeof() - len(obj))
|
return obj + self.pattern * (self.sizeof() - len(obj))
|
||||||
|
|
||||||
|
class MultiplyAdapter(Adapter):
|
||||||
|
"""
|
||||||
|
Decoder multiplies by multiplicator
|
||||||
|
Encoder divides by multiplicator
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
subcon: Subconstruct as defined by construct library
|
||||||
|
multiplier: Multiplier to apply to raw encoded value
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, subcon, multiplicator):
|
||||||
|
super().__init__(subcon)
|
||||||
|
self.multiplicator = multiplicator
|
||||||
|
|
||||||
|
def _decode(self, obj, context, path):
|
||||||
|
return obj * 8
|
||||||
|
|
||||||
|
def _encode(self, obj, context, path):
|
||||||
|
return obj // 8
|
||||||
|
|
||||||
|
|
||||||
class GsmStringAdapter(Adapter):
|
class GsmStringAdapter(Adapter):
|
||||||
"""Convert GSM 03.38 encoded bytes to a string."""
|
"""Convert GSM 03.38 encoded bytes to a string."""
|
||||||
|
|
|
@ -86,19 +86,15 @@ class EF_MILENAGE_CFG(TransparentEF):
|
||||||
class EF_0348_KEY(LinFixedEF):
|
class EF_0348_KEY(LinFixedEF):
|
||||||
def __init__(self, fid='6f22', name='EF.0348_KEY', desc='TS 03.48 OTA Keys'):
|
def __init__(self, fid='6f22', name='EF.0348_KEY', desc='TS 03.48 OTA Keys'):
|
||||||
super().__init__(fid, name=name, desc=desc, rec_len=(27, 35))
|
super().__init__(fid, name=name, desc=desc, rec_len=(27, 35))
|
||||||
|
KeyLenAndType = BitStruct('mac_length'/Mapping(Bit, {8:0, 4:1}),
|
||||||
def _decode_record_bin(self, raw_bin_data, **kwargs):
|
'algorithm'/Enum(Bit, des=0, aes=1),
|
||||||
u = unpack('!BBB', raw_bin_data[0:3])
|
'key_length'/MultiplyAdapter(BitsInteger(3), 8),
|
||||||
key_algo = (u[2] >> 6) & 1
|
'_rfu'/BitsRFU(1),
|
||||||
key_length = ((u[2] >> 3) & 3) * 8
|
'key_type'/Enum(BitsInteger(2), kic=0, kid=1, kik=2, any=3))
|
||||||
return {'sec_domain': u[0],
|
self._construct = Struct('security_domain'/Int8ub,
|
||||||
'key_set_version': u[1],
|
'key_set_version'/Int8ub,
|
||||||
'key_type': key_type2str[u[2] & 3],
|
'key_len_and_type'/KeyLenAndType,
|
||||||
'key_length': key_length,
|
'key'/HexAdapter(Bytes(this.key_len_and_type.key_length)))
|
||||||
'algorithm': key_algo2str[key_algo],
|
|
||||||
'mac_length': mac_length[(u[2] >> 7)],
|
|
||||||
'key': raw_bin_data[3:key_length].hex()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class EF_0348_COUNT(LinFixedEF):
|
class EF_0348_COUNT(LinFixedEF):
|
||||||
|
|
Loading…
Reference in New Issue