*gEdZddlmZddlmZmZmZmZmZm Z m Z m Z m Z ddlm Z mZmZmZmZmZmZmZddlmZddlTddlTddlTddlmZddlZd d d d d ZdddZdddZGdde Z!Gdde Z"Gdde#Z$Gdde#Z%Gdde Z&Gdde#Z'Gdd e#Z(Gd!d"e Z)Gd#d$e*Z+Gd%d&e Z,Gd'd(e Z-Gd)d*e Z.Gd+d,e Z/Gd-d.e Z0Gd/d0e#Z1Gd1d2e2Z3Gd3d4e2Z4dS)5aUtilities / Functions related to sysmocom SJA2/SJA5 cards (C) 2021-2023 by Harald Welte 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 . )unpack) FlagsEnumByteStructInt8ubBytesMappingEnumPadding BitsInteger)BitthisInt32ubInt16ubNibble BytesInteger GreedyRangeConst)Optional)*) RuntimeStateNkickidkikany)rdesaes)rrc leZdZddddddddddddddd d fd dddddddddd d d fgZdfd ZxZS)EF_PIN*f1030331323334ffffffff0a0a3132333435363738TF)valid change_able unblock_able disable_ablenot_initializeddisabledr31323334 3132333435363738)attempts_remainingmaximum_attemptspuk)stater/r0pinr1f0030399999999999999999999999999999999N6f01EF.CHV1c t||d|zttdddddd }t d t z d t z d t ttd z }t d|z d t z d t z dt ttd z d t|z |_ dS)Nz %s PIN filenamedescrr @)r+r*r)r(r'r&r/r0r1r!r2r3) super__init__rrrr HexAdapterRpadr COptional _construct)selffidr: StateByte PukStruct __class__s A/home/jenkins/workspace/simtester-sanitize/pySim/sysmocom_sja2.pyrAzEF_PIN.__init__?s 4md.BCCCdQPTcg04DBBB /6-f4 DqNN!;!;;== !!2!5f!rr=` 00000000000000000000000000000000 00000000000000000000000000000001 00000000000000000000000000000002 00000000000000000000000000000004 00000000000000000000000000000008) r1r2r3r4r5c1c2c3c4c56f21EF.MILENAGE_CFGMilenage connfigurationct|||tdtz dtz dtz dtz dtz dt t dz d t t dz d t t dz d t t dz d t t dz |_dS) Nr9r]r^r_r`rarbr<rcrdrerfr@rArrrBrrErFrGr:r;rJs rKrAzEF_MILENAGE_CFG.__init__Ts 4d333 fd6k4;V UYZ`U`!%jr&;&;!;!%jr&;&;!;!%jr&;&;!;!%jr&;&;!;!%jr&;&;!; ==rL)rgrhrirMrSs@rKrUrUMsv w1BbBd -5W -5W Y Y [O ==========rLrUc eZdZdfd ZxZS) EF_0348_KEY6f22 EF.0348_KEYTS 03.48 OTA Keysc&t|||dtdttdddz dt tddz d t td d z d tdz d t tddddd z }tdtz dtz d|z dtttjjz |_dS)N)#r:r;rec_len mac_lengthrr)r!r" algorithm)rr key_lengthrr!_rfukey_typer)rrrrsecurity_domainkey_set_versionkey_len_and_typekey)r@rA BitStructr r r MultiplyAdapterr BitsRFUrrrBrrr~ryrE)rFrGr:r; KeyLenAndTyperJs rKrAzEF_0348_KEY.__init___s 4dHEEE!,wsqAJJ/G/G"G"-d3A1.E.E.E"E".{1~~q/Q/Q"Q"("3",T+a..aQTU[\-]-]-]"] __ !!26!9!26!9!3M!A!&z%8M8X2Y2Y'Z'Z!Z\\rL)rorprqrNrOrPrArRrSs@rKrnrn^sG \ \ \ \ \ \ \ \ \ \rLrnc2eZdZdddddfgZd fd ZxZS) EF_0348_COUNTfe010000000000r 0000000000) sec_domainr}counter6f23 EF.0348_COUNTTS 03.48 OTA Countersc t|||dtdtz dtz dt t dz |_dS)N)rrurr}rrkrls rKrAzEF_0348_COUNT.__init__ps` 4dFCCC f!4!26!9!*:eAhh+?+?!?AArL)rrrrMrSs@rKrrlsa 31Q]^^`OAAAAAAAAAArLrc eZdZdfd ZxZS)EF_SIM_AUTH_COUNTERaf24EF.SIM_AUTH_COUNTERct||dtdtz |_dS)Nz0Number of remaining RUN GSM ALGORITHM executionsr9num_run_gsm_algo_remain)r@rArrrErFrGr:rJs rKrAzEF_SIM_AUTH_COUNTER.__init__xs< 4.`aaa !:7!BCCrL)rrrrSs@rKrrwsGDDDDDDDDDDrLrc4eZdZddddddfgZd fd ZxZS) EF_GP_COUNT 0070000000rp)rr}rrfu6f26 EF.GP_COUNTGP SCP02 Countersct|||dtdtz dtz dtz dtz |_dS)N)rrrurr}rr)r@rArrrrErls rKrAzEF_GP_COUNT.__init__sW 4dFCCC f!4!26!9!*7!2!&v//rL)rrrrMrSs@rKrr}sY qSQWXYY[O//////////rLrc&eZdZdfd ZdZxZS)EF_GP_DIV_DATA6f27EF.GP_DIV_DATA!GP SCP02 key diversification datacRt|||ddS)N) rru)r@rArls rKrAzEF_GP_DIV_DATA.__init__s* 4dHEEEEErLc vtd|}|d|d|ddS)Nz!BB8srrr)rr} key_div_data)rhex)rF raw_bin_datakwargsus rK_decode_record_binz!EF_GP_DIV_DATA._decode_record_bins5 7L ) )dqtQqTXXZZXXXrL)rrr)rNrOrPrArrRrSs@rKrrsYFFFFFFYYYYYYYrLrc:eZdZddddddddfgZd fd ZxZS) EF_SIM_AUTH_KEYB14000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1frTmilenage)sres_deriv_funcuse_opc_instead_of_oprx 000102030405060708090a0b0c0d0e0f 101112131415161718191a1b1c1d1e1fcfgrop_opc6f20EF.SIM_AUTH_KEYc t||dttddt t dddz dt z d ttd ddd z }td |z dttdz dttdz |_ dS)NUSIM authentication keyr9rrrrrrrrxr"r)r comp128v1 comp128v2 comp128v3rrr<r) r@rArr r r Flagr rrrBrrErFrGr:CfgByterJs rKrAzEF_SIM_AUTH_KEY.__init__s 4.GHHHGAJJ-gca1::.F.FF3D8'Va1XYef(g(g(ggii!w!&z%))'<' > @O K K K K K K K K K KrLrceZdZddddddddd fd d d ddd d d d dddddfgZdfd ZdedefdZdedefdZxZ S)EF_USIM_AUTH_KEYB141898d827f70120d33b3e7462ee5fd6fe6ca53d7a0a804561646816d7b0c702fbFrTronly_4bytes_res_in_3gsres_deriv_func_in_2grrx 1898d827f70120d33b3e7462ee5fd6fe 6ca53d7a0a804561646816d7b0c702fbrf160a04101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f000102030405060708090a0b0c0d0e0ftuakr?)rxryrr)ck_and_ik_sizemac_sizeres_sizer"r@101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f)rtuak_cfgnum_of_keccak_iterationskraf20EF.USIM_AUTH_KEYct||dttdddd}t t dd t z d ttd dd z d ttd ddz d|z }td|z dttdz dttdz |_ t t ddttd ddz d ttd dd z d ttd ddz d|z }t t ddttd ddz dttdd dddz dttdd ddddz }td|z d|z dtz dttdz d tttjjd!zz |_dS)"Nrr9r"rr)rsha1_akarxorrrrrrr)FTrxrrr<rry)r?rrrr)r>r?rr)r=r>r?rrrr=rr!)r@rAr rrr rr r rrBrrEr rrrry _constr_tuak)rFrGr: Algorithmr CfgByteTuak TuakCfgByterJs rKrAzEF_USIM_AUTH_KEY.__init__s/ 4.GHHH!aaRHHH GAJJ(?(D3GCA4L4LL3GCPQARAR4S4SS' 133!w!&z%))'<'>qaUVCWCW+X+X X *7;q>>qQTU[\C]C]+^+^ ^`` #5#4#-k#9#=f#D#+JuRyy,A,A#A#&z%8KQ8N2O2O'P'P#P RRrLrreturnct|ddzdkrt|j|St|j|S)Nrrr)parse_constructrrE)rFrs rK _decode_binzEF_USIM_AUTH_KEY._decode_bins; ?T !T ) )"4#4lCC C"4?LAA ArL abstract_datac z|dddkrt|j|St|j|S)Nrrxr)build_constructrrE)rFr rs rK _encode_binzEF_USIM_AUTH_KEY._encode_bins<   , 6 6"4#4mDD D"4?MBB BrL)rr) rNrOrPrQrA bytearraydictr rrRrSs@rKrrs N.3ajn~HJJ5Ac f f h s#)#YZvz||-0sQTVV()3X    ORRRRRR8B BdBBBB CCICCCCCCCCrLrc<eZdZddddddddd fgZd fd ZxZS)EF_USIM_AUTH_KEY_2GrFrTrrrrraf22EF.USIM_AUTH_KEY_2Gct||dttddtz dt t dddz dtz d ttd dd d d z }td|z dttdz dttdz |_ dS)Nz%USIM authentication key in 2G contextr9rrrrrrrxr"rr)rrrrrrrr<r) r@rArr rr r r rrrBrrErs rKrAzEF_USIM_AUTH_KEY_2G.__init__ s 4.UVVVGAJJ(?(D3GCA4L4LL3D8'Va1XYefln(o(o(ooqq!w!&z%))'<'???///N   ~ . . . . . 2 1rLNrNrOrP_atrs classmethodrrrLrKr-r-,O R R RE/<///[///rLr-c2eZdZgdZedefdZdS) SysmocomSJA5)zA3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 35 75 30 35 02 51 CCzA3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 35 75 30 35 02 65 F8zA3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 35 75 30 35 02 59 C4r.c(|jtd|jjvrw|jjd}t t t tttg}| |d|jjvrX|jjd}t dt dtdg}| |dSdSr0r3r7s rKrzSysmocomSJA5.add_filesPr=rLNr>rArLrKrDrDKrBrLrD)5__doc__structr constructrrrrrr r r r r rrrrrrrrrD osmocom.utilsosmocom.constructpySim.filesystem pySim.runtimerr key_type2str key_algo2strrw TransparentEFr$rU LinFixedEFrnrrrrrCardDFrrrrrr#r( CardModelr-rDrArLrKrSsQ$aaaaaaaaaaaaaaaaaaaaaa[[[[[[[[[[[[[[[[[[[[++++++&&&&&&           =====]===8=====m===" \ \ \ \ \* \ \ \AAAAAJAAADDDDD-DDD / / / / /* / / /YYYYYZYYYBBBBBmBBB"QQQQQQQQ0KKKKK-KKK,3C3C3C3C3C}3C3C3ClBBBBB-BBB$&&&&& &&& .....m...&&&&&Z&&&/////9///>/////9/////rL