Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 1 | # coding=utf-8 |
Harald Welte | 0489ae6 | 2023-05-24 10:28:34 +0200 | [diff] [blame] | 2 | """Utilities / Functions related to sysmocom SJA2/SJA5 cards |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 3 | |
Harald Welte | 0489ae6 | 2023-05-24 10:28:34 +0200 | [diff] [blame] | 4 | (C) 2021-2023 by Harald Welte <laforge@osmocom.org> |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | """ |
| 19 | |
| 20 | from pytlv.TLV import * |
| 21 | from struct import pack, unpack |
| 22 | from pySim.utils import * |
| 23 | from pySim.filesystem import * |
Harald Welte | 531894d | 2023-07-11 19:11:11 +0200 | [diff] [blame] | 24 | from pySim.runtime import RuntimeState |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 25 | from pySim.ts_102_221 import CardProfileUICC |
| 26 | from pySim.construct import * |
| 27 | from construct import * |
| 28 | import pySim |
| 29 | |
| 30 | key_type2str = { |
| 31 | 0: 'kic', |
| 32 | 1: 'kid', |
| 33 | 2: 'kik', |
| 34 | 3: 'any', |
| 35 | } |
| 36 | |
| 37 | key_algo2str = { |
| 38 | 0: 'des', |
| 39 | 1: 'aes' |
| 40 | } |
| 41 | |
| 42 | mac_length = { |
| 43 | 0: 8, |
| 44 | 1: 4 |
| 45 | } |
| 46 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 47 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 48 | class EF_PIN(TransparentEF): |
| 49 | def __init__(self, fid, name): |
| 50 | super().__init__(fid, name=name, desc='%s PIN file' % name) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 51 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 52 | def _decode_bin(self, raw_bin_data): |
| 53 | u = unpack('!BBB8s', raw_bin_data[:11]) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 54 | res = {'enabled': (True, False)[u[0] & 0x01], |
| 55 | 'initialized': (True, False)[u[0] & 0x02], |
| 56 | 'disable_able': (False, True)[u[0] & 0x10], |
| 57 | 'unblock_able': (False, True)[u[0] & 0x20], |
| 58 | 'change_able': (False, True)[u[0] & 0x40], |
| 59 | 'valid': (False, True)[u[0] & 0x80], |
| 60 | 'attempts_remaining': u[1], |
| 61 | 'maximum_attempts': u[2], |
| 62 | 'pin': u[3].hex(), |
| 63 | } |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 64 | if len(raw_bin_data) == 21: |
| 65 | u2 = unpack('!BB8s', raw_bin_data[11:10]) |
| 66 | res['attempts_remaining_puk'] = u2[0] |
| 67 | res['maximum_attempts_puk'] = u2[1] |
| 68 | res['puk'] = u2[2].hex() |
| 69 | return res |
| 70 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 71 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 72 | class EF_MILENAGE_CFG(TransparentEF): |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 73 | _test_de_encode = [ |
| 74 | ( '40002040600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000020000000000000000000000000000000400000000000000000000000000000008', |
| 75 | {"r1": 64, "r2": 0, "r3": 32, "r4": 64, "r5": 96, "c1": "00000000000000000000000000000000", "c2": |
| 76 | "00000000000000000000000000000001", "c3": "00000000000000000000000000000002", "c4": |
| 77 | "00000000000000000000000000000004", "c5": "00000000000000000000000000000008"} ), |
| 78 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 79 | def __init__(self, fid='6f21', name='EF.MILENAGE_CFG', desc='Milenage connfiguration'): |
| 80 | super().__init__(fid, name=name, desc=desc) |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 81 | self._construct = Struct('r1'/Int8ub, 'r2'/Int8ub, 'r3'/Int8ub, 'r4'/Int8ub, 'r5'/Int8ub, |
| 82 | 'c1'/HexAdapter(Bytes(16)), |
| 83 | 'c2'/HexAdapter(Bytes(16)), |
| 84 | 'c3'/HexAdapter(Bytes(16)), |
| 85 | 'c4'/HexAdapter(Bytes(16)), |
| 86 | 'c5'/HexAdapter(Bytes(16))) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 87 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 88 | |
| 89 | class EF_0348_KEY(LinFixedEF): |
| 90 | def __init__(self, fid='6f22', name='EF.0348_KEY', desc='TS 03.48 OTA Keys'): |
Harald Welte | 99e4cc0 | 2022-07-21 15:25:47 +0200 | [diff] [blame] | 91 | super().__init__(fid, name=name, desc=desc, rec_len=(27, 35)) |
Harald Welte | 954ce95 | 2023-05-27 20:08:09 +0200 | [diff] [blame] | 92 | KeyLenAndType = BitStruct('mac_length'/Mapping(Bit, {8:0, 4:1}), |
| 93 | 'algorithm'/Enum(Bit, des=0, aes=1), |
| 94 | 'key_length'/MultiplyAdapter(BitsInteger(3), 8), |
| 95 | '_rfu'/BitsRFU(1), |
| 96 | 'key_type'/Enum(BitsInteger(2), kic=0, kid=1, kik=2, any=3)) |
| 97 | self._construct = Struct('security_domain'/Int8ub, |
| 98 | 'key_set_version'/Int8ub, |
| 99 | 'key_len_and_type'/KeyLenAndType, |
| 100 | 'key'/HexAdapter(Bytes(this.key_len_and_type.key_length))) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 101 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 102 | |
| 103 | class EF_0348_COUNT(LinFixedEF): |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 104 | _test_de_encode = [ |
| 105 | ( 'fe010000000000', {"sec_domain": 254, "key_set_version": 1, "counter": "0000000000"} ), |
| 106 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 107 | def __init__(self, fid='6f23', name='EF.0348_COUNT', desc='TS 03.48 OTA Counters'): |
Harald Welte | 99e4cc0 | 2022-07-21 15:25:47 +0200 | [diff] [blame] | 108 | super().__init__(fid, name=name, desc=desc, rec_len=(7, 7)) |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 109 | self._construct = Struct('sec_domain'/Int8ub, |
| 110 | 'key_set_version'/Int8ub, |
| 111 | 'counter'/HexAdapter(Bytes(5))) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 112 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 113 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 114 | class EF_SIM_AUTH_COUNTER(TransparentEF): |
| 115 | def __init__(self, fid='af24', name='EF.SIM_AUTH_COUNTER'): |
| 116 | super().__init__(fid, name=name, desc='Number of remaining RUN GSM ALGORITHM executions') |
| 117 | self._construct = Struct('num_run_gsm_algo_remain'/Int32ub) |
| 118 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 119 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 120 | class EF_GP_COUNT(LinFixedEF): |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 121 | _test_de_encode = [ |
| 122 | ( '0070000000', {"sec_domain": 0, "key_set_version": 112, "counter": 0, "rfu": 0} ), |
| 123 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 124 | def __init__(self, fid='6f26', name='EF.GP_COUNT', desc='GP SCP02 Counters'): |
Harald Welte | 99e4cc0 | 2022-07-21 15:25:47 +0200 | [diff] [blame] | 125 | super().__init__(fid, name=name, desc=desc, rec_len=(5, 5)) |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 126 | self._construct = Struct('sec_domain'/Int8ub, |
| 127 | 'key_set_version'/Int8ub, |
| 128 | 'counter'/Int16ub, |
| 129 | 'rfu'/Int8ub) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 130 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 131 | class EF_GP_DIV_DATA(LinFixedEF): |
| 132 | def __init__(self, fid='6f27', name='EF.GP_DIV_DATA', desc='GP SCP02 key diversification data'): |
Harald Welte | 99e4cc0 | 2022-07-21 15:25:47 +0200 | [diff] [blame] | 133 | super().__init__(fid, name=name, desc=desc, rec_len=(12, 12)) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 134 | |
Harald Welte | f6b37af | 2023-01-24 15:42:26 +0100 | [diff] [blame] | 135 | def _decode_record_bin(self, raw_bin_data, **kwargs): |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 136 | u = unpack('!BB8s', raw_bin_data) |
| 137 | return {'sec_domain': u[0], 'key_set_version': u[1], 'key_div_data': u[2].hex()} |
| 138 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 139 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 140 | class EF_SIM_AUTH_KEY(TransparentEF): |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 141 | _test_de_encode = [ |
| 142 | ( '14000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 143 | {"cfg": {"sres_deriv_func": 1, "use_opc_instead_of_op": True, "algorithm": "milenage"}, "key": |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 144 | "000102030405060708090a0b0c0d0e0f", "op_opc": "101112131415161718191a1b1c1d1e1f"} ), |
| 145 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 146 | def __init__(self, fid='6f20', name='EF.SIM_AUTH_KEY'): |
| 147 | super().__init__(fid, name=name, desc='USIM authentication key') |
Harald Welte | cfa3015 | 2022-07-22 17:12:27 +0200 | [diff] [blame] | 148 | CfgByte = BitStruct(Padding(2), |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 149 | 'sres_deriv_func'/Mapping(Bit, {1:0, 2:1}), |
| 150 | 'use_opc_instead_of_op'/Flag, |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 151 | 'algorithm'/Enum(Nibble, milenage=4, comp128v1=1, comp128v2=2, comp128v3=3)) |
| 152 | self._construct = Struct('cfg'/CfgByte, |
Harald Welte | 3c98d5e | 2022-07-20 07:40:05 +0200 | [diff] [blame] | 153 | 'key'/HexAdapter(Bytes(16)), |
Philipp Maier | 4f888a0 | 2022-12-02 12:30:12 +0100 | [diff] [blame] | 154 | 'op_opc' /HexAdapter(Bytes(16))) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 155 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 156 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 157 | class DF_SYSTEM(CardDF): |
| 158 | def __init__(self): |
| 159 | super().__init__(fid='a515', name='DF.SYSTEM', desc='CardOS specifics') |
| 160 | files = [ |
| 161 | EF_PIN('6f01', 'EF.CHV1'), |
| 162 | EF_PIN('6f81', 'EF.CHV2'), |
| 163 | EF_PIN('6f0a', 'EF.ADM1'), |
| 164 | EF_PIN('6f0b', 'EF.ADM2'), |
| 165 | EF_PIN('6f0c', 'EF.ADM3'), |
| 166 | EF_PIN('6f0d', 'EF.ADM4'), |
| 167 | EF_MILENAGE_CFG(), |
| 168 | EF_0348_KEY(), |
| 169 | EF_SIM_AUTH_COUNTER(), |
| 170 | EF_SIM_AUTH_KEY(), |
| 171 | EF_0348_COUNT(), |
| 172 | EF_GP_COUNT(), |
| 173 | EF_GP_DIV_DATA(), |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 174 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 175 | self.add_files(files) |
| 176 | |
| 177 | def decode_select_response(self, resp_hex): |
Philipp Maier | 5998a3a | 2021-11-16 15:16:39 +0100 | [diff] [blame] | 178 | return pySim.ts_102_221.CardProfileUICC.decode_select_response(resp_hex) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 179 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 180 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 181 | class EF_USIM_SQN(TransparentEF): |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 182 | _test_de_encode = [ |
| 183 | ( 'd503000200000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 184 | {"flag1": {"skip_next_sqn_check": True, "delta_max_check": True, "age_limit_check": False, "sqn_check": True, |
| 185 | "ind_len": 5}, "flag2": {"rfu": 0, "dont_clear_amf_for_macs": False, "aus_concealed": True, |
| 186 | "autn_concealed": True}, "delta_max": 8589934592, "age_limit": |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 187 | 8589934592, "freshness": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 188 | 0, 0, 0, 0, 0, 0, 0, 0]} ), |
| 189 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 190 | def __init__(self, fid='af30', name='EF.USIM_SQN'): |
| 191 | super().__init__(fid, name=name, desc='SQN parameters for AKA') |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 192 | Flag1 = BitStruct('skip_next_sqn_check'/Flag, 'delta_max_check'/Flag, |
| 193 | 'age_limit_check'/Flag, 'sqn_check'/Flag, |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 194 | 'ind_len'/BitsInteger(4)) |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 195 | Flag2 = BitStruct('rfu'/BitsRFU(5), 'dont_clear_amf_for_macs'/Flag, |
| 196 | 'aus_concealed'/Flag, 'autn_concealed'/Flag) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 197 | self._construct = Struct('flag1'/Flag1, 'flag2'/Flag2, |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 198 | 'delta_max' / |
| 199 | BytesInteger(6), 'age_limit'/BytesInteger(6), |
Harald Welte | eb45838 | 2021-10-16 10:44:23 +0200 | [diff] [blame] | 200 | 'freshness'/GreedyRange(BytesInteger(6))) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 201 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 202 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 203 | class EF_USIM_AUTH_KEY(TransparentEF): |
| 204 | def __init__(self, fid='af20', name='EF.USIM_AUTH_KEY'): |
| 205 | super().__init__(fid, name=name, desc='USIM authentication key') |
Harald Welte | 0365058 | 2023-05-29 21:07:24 +0200 | [diff] [blame] | 206 | Algorithm = Enum(Nibble, milenage=4, sha1_aka=5, tuak=6, xor=15) |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 207 | CfgByte = BitStruct(Padding(1), 'only_4bytes_res_in_3g'/Flag, |
| 208 | 'sres_deriv_func_in_2g'/Mapping(Bit, {1:0, 2:1}), |
Harald Welte | 0365058 | 2023-05-29 21:07:24 +0200 | [diff] [blame] | 209 | 'use_opc_instead_of_op'/Mapping(Bit, {False:0, True:1}), |
| 210 | 'algorithm'/Algorithm) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 211 | self._construct = Struct('cfg'/CfgByte, |
Harald Welte | 3c98d5e | 2022-07-20 07:40:05 +0200 | [diff] [blame] | 212 | 'key'/HexAdapter(Bytes(16)), |
Philipp Maier | 4f888a0 | 2022-12-02 12:30:12 +0100 | [diff] [blame] | 213 | 'op_opc' /HexAdapter(Bytes(16))) |
Harald Welte | 0365058 | 2023-05-29 21:07:24 +0200 | [diff] [blame] | 214 | # TUAK has a rather different layout for the data, so we define a different |
| 215 | # construct below and use explicit _{decode,encode}_bin() methods for separating |
| 216 | # the TUAK and non-TUAK situation |
| 217 | CfgByteTuak = BitStruct(Padding(1), |
| 218 | 'key_length'/Mapping(Bit, {128:0, 256:1}), |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 219 | 'sres_deriv_func_in_2g'/Mapping(Bit, {1:0, 2:1}), |
Harald Welte | 0365058 | 2023-05-29 21:07:24 +0200 | [diff] [blame] | 220 | 'use_opc_instead_of_op'/Mapping(Bit, {False:0, True:1}), |
| 221 | 'algorithm'/Algorithm) |
| 222 | TuakCfgByte = BitStruct(Padding(1), |
| 223 | 'ck_and_ik_size'/Mapping(Bit, {128:0, 256:1}), |
| 224 | 'mac_size'/Mapping(BitsInteger(3), {64:0, 128:1, 256:2}), |
| 225 | 'res_size'/Mapping(BitsInteger(3), {32:0, 64:1, 128:2, 256:3})) |
| 226 | self._constr_tuak = Struct('cfg'/CfgByteTuak, |
| 227 | 'tuak_cfg'/TuakCfgByte, |
| 228 | 'num_of_keccak_iterations'/Int8ub, |
| 229 | 'op_opc'/HexAdapter(Bytes(32)), |
Harald Welte | 19b4a97 | 2023-06-08 17:18:31 +0200 | [diff] [blame] | 230 | 'k'/HexAdapter(Bytes(this.cfg.key_length//8))) |
Harald Welte | 0365058 | 2023-05-29 21:07:24 +0200 | [diff] [blame] | 231 | |
| 232 | def _decode_bin(self, raw_bin_data: bytearray) -> dict: |
| 233 | if raw_bin_data[0] & 0x0F == 0x06: |
| 234 | return parse_construct(self._constr_tuak, raw_bin_data) |
| 235 | else: |
| 236 | return parse_construct(self._construct, raw_bin_data) |
| 237 | |
| 238 | def _encode_bin(self, abstract_data: dict) -> bytearray: |
| 239 | if abstract_data['cfg']['algorithm'] == 'tuak': |
Harald Welte | a5eb924 | 2023-12-23 10:20:20 +0100 | [diff] [blame] | 240 | return build_construct(self._constr_tuak, abstract_data) |
Harald Welte | 0365058 | 2023-05-29 21:07:24 +0200 | [diff] [blame] | 241 | else: |
Harald Welte | a5eb924 | 2023-12-23 10:20:20 +0100 | [diff] [blame] | 242 | return build_construct(self._construct, abstract_data) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 243 | |
| 244 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 245 | class EF_USIM_AUTH_KEY_2G(TransparentEF): |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 246 | _test_de_encode = [ |
| 247 | ( '14000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 248 | {"cfg": {"only_4bytes_res_in_3g": False, "sres_deriv_func_in_2g": 1, "use_opc_instead_of_op": True, |
Harald Welte | 7be68b2 | 2023-11-02 00:19:53 +0100 | [diff] [blame] | 249 | "algorithm": "milenage"}, "key": "000102030405060708090a0b0c0d0e0f", "op_opc": |
| 250 | "101112131415161718191a1b1c1d1e1f"} ), |
| 251 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 252 | def __init__(self, fid='af22', name='EF.USIM_AUTH_KEY_2G'): |
| 253 | super().__init__(fid, name=name, desc='USIM authentication key in 2G context') |
Harald Welte | 45477a7 | 2023-11-03 01:33:06 +0100 | [diff] [blame] | 254 | CfgByte = BitStruct(Padding(1), 'only_4bytes_res_in_3g'/Flag, |
| 255 | 'sres_deriv_func_in_2g'/Mapping(Bit, {1:0, 2:1}), |
| 256 | 'use_opc_instead_of_op'/Flag, |
Harald Welte | 557c136 | 2023-05-29 15:26:22 +0200 | [diff] [blame] | 257 | 'algorithm'/Enum(Nibble, milenage=4, comp128v1=1, comp128v2=2, comp128v3=3, xor=14)) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 258 | self._construct = Struct('cfg'/CfgByte, |
Harald Welte | 3c98d5e | 2022-07-20 07:40:05 +0200 | [diff] [blame] | 259 | 'key'/HexAdapter(Bytes(16)), |
Philipp Maier | 4f888a0 | 2022-12-02 12:30:12 +0100 | [diff] [blame] | 260 | 'op_opc' /HexAdapter(Bytes(16))) |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 261 | |
| 262 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 263 | class EF_GBA_SK(TransparentEF): |
| 264 | def __init__(self, fid='af31', name='EF.GBA_SK'): |
| 265 | super().__init__(fid, name=name, desc='Secret key for GBA key derivation') |
| 266 | self._construct = GreedyBytes |
| 267 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 268 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 269 | class EF_GBA_REC_LIST(TransparentEF): |
| 270 | def __init__(self, fid='af32', name='EF.GBA_REC_LIST'): |
| 271 | super().__init__(fid, name=name, desc='Secret key for GBA key derivation') |
| 272 | # integers representing record numbers in EF-GBANL |
| 273 | self._construct = GreedyRange(Int8ub) |
| 274 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 275 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 276 | class EF_GBA_INT_KEY(LinFixedEF): |
| 277 | def __init__(self, fid='af33', name='EF.GBA_INT_KEY'): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 278 | super().__init__(fid, name=name, |
Harald Welte | 99e4cc0 | 2022-07-21 15:25:47 +0200 | [diff] [blame] | 279 | desc='Secret key for GBA key derivation', rec_len=(32, 32)) |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 280 | self._construct = GreedyBytes |
| 281 | |
| 282 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 283 | class SysmocomSJA2(CardModel): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 284 | _atrs = ["3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9", |
| 285 | "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2", |
| 286 | "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5"] |
| 287 | |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 288 | @classmethod |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 289 | def add_files(cls, rs: RuntimeState): |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 290 | """Add sysmocom SJA2 specific files to given RuntimeState.""" |
| 291 | rs.mf.add_file(DF_SYSTEM()) |
| 292 | # optional USIM application |
| 293 | if 'a0000000871002' in rs.mf.applications: |
| 294 | usim_adf = rs.mf.applications['a0000000871002'] |
| 295 | files_adf_usim = [ |
| 296 | EF_USIM_AUTH_KEY(), |
| 297 | EF_USIM_AUTH_KEY_2G(), |
| 298 | EF_GBA_SK(), |
| 299 | EF_GBA_REC_LIST(), |
| 300 | EF_GBA_INT_KEY(), |
| 301 | EF_USIM_SQN(), |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 302 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 303 | usim_adf.add_files(files_adf_usim) |
| 304 | # optional ISIM application |
| 305 | if 'a0000000871004' in rs.mf.applications: |
| 306 | isim_adf = rs.mf.applications['a0000000871004'] |
| 307 | files_adf_isim = [ |
| 308 | EF_USIM_AUTH_KEY(name='EF.ISIM_AUTH_KEY'), |
| 309 | EF_USIM_AUTH_KEY_2G(name='EF.ISIM_AUTH_KEY_2G'), |
| 310 | EF_USIM_SQN(name='EF.ISIM_SQN'), |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 311 | ] |
Harald Welte | f44256c | 2021-10-14 15:53:39 +0200 | [diff] [blame] | 312 | isim_adf.add_files(files_adf_isim) |
Harald Welte | 0489ae6 | 2023-05-24 10:28:34 +0200 | [diff] [blame] | 313 | |
| 314 | class SysmocomSJA5(CardModel): |
| 315 | _atrs = ["3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 35 75 30 35 02 51 CC", |
| 316 | "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 35 75 30 35 02 65 F8", |
| 317 | "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 35 75 30 35 02 59 C4"] |
| 318 | |
| 319 | @classmethod |
| 320 | def add_files(cls, rs: RuntimeState): |
| 321 | """Add sysmocom SJA2 specific files to given RuntimeState.""" |
| 322 | rs.mf.add_file(DF_SYSTEM()) |
| 323 | # optional USIM application |
| 324 | if 'a0000000871002' in rs.mf.applications: |
| 325 | usim_adf = rs.mf.applications['a0000000871002'] |
| 326 | files_adf_usim = [ |
| 327 | EF_USIM_AUTH_KEY(), |
| 328 | EF_USIM_AUTH_KEY_2G(), |
| 329 | EF_GBA_SK(), |
| 330 | EF_GBA_REC_LIST(), |
| 331 | EF_GBA_INT_KEY(), |
| 332 | EF_USIM_SQN(), |
| 333 | ] |
| 334 | usim_adf.add_files(files_adf_usim) |
| 335 | # optional ISIM application |
| 336 | if 'a0000000871004' in rs.mf.applications: |
| 337 | isim_adf = rs.mf.applications['a0000000871004'] |
| 338 | files_adf_isim = [ |
| 339 | EF_USIM_AUTH_KEY(name='EF.ISIM_AUTH_KEY'), |
| 340 | EF_USIM_AUTH_KEY_2G(name='EF.ISIM_AUTH_KEY_2G'), |
| 341 | EF_USIM_SQN(name='EF.ISIM_SQN'), |
| 342 | ] |
| 343 | isim_adf.add_files(files_adf_isim) |