*gPdZddlZddlZddlZddlmZmZddlmZm Z m Z m Z m Z m Z mZmZddlmZmZmZmZmZmZddlTddlmZddlmZee dd d d d d dddddddZe de z dee d z dee z Zeed dd d d Zeed deed dd d d z dez dez ed dez dez d ez d!eed dd d "z Zed#ed z d$eed dd d dd %z Z ed#ed z d$eed dd d dd &z Z!ed#ed z d$eed dd d d 'z Z"e d(e z d)e z d*ez d+e z d,eej#j$e!e"d-z d.e d z d/e z Z%e d0e&e e z d1e z d2e z d3e z d4ee e d5e z d6e z z d7e z d8e&e e z d9e&e ee d z Z'Gd:d;Z(Gd<d=e)Z*Gd>d?ej+Z,dd@l-m.Z.m/Z/m0Z0ddAl1m2Z2GdBdCej+Z3GdDdEe3ej+Z4GdFdGe3ej+Z5GdHdIe4Z6GdJdKe5Z7GdLdMe4Z8GdNdOe5Z9GdPdQe4Z:GdRdSe5Z;GdTdUe,ZzOtaAlgo.suaS4>%9::rmNrin_lenmultiplepaddingc2||zdkrdS|||zz }d|zS)z+Return padding bytes towards multiple of N.rrmr~)rrrpad_cnts rk _get_paddingzOtaAlgo._get_paddings3 H  ! !3fx/0  rmindatcZ|tt|||zS)z!Pad input bytes to multiple of N.)rrlen)rrrs rk_pad_to_multiplezOtaAlgo._pad_to_multiples'w++CJJ'JJJJrmc:|||j|S)z>Pad the given input data to multiple of the cipher block size.)rr)rjrrs rkpad_to_blocksizezOtaAlgo.pad_to_blocksizes$$UDNGDDDrmrc||_dSrh)r)rjrs rkrlzOtaAlgo.__init__s  rmc|jjSrh) __class__rwrqs rk__str__zOtaAlgo.__str__s ~&&rmrv)rwrxryr}ivr enum_name staticmethodr|rrirrrarlrr~rmrkrrs :: ; ;BII!!S!C!#!!!\!KKKKsKKK\KEEeEcEEEEY'''''rmrceZdZdeffd ZdedefdZdedefdZej dedefdZ ej dedefdZ e deddfd Z xZS) rtrc|j|jkrtd|jd|jt|dSNzCannot use algorithm z with key for )rrbrsuperrlrjrrs rkrlzOtaAlgoCrypt.__init__sR >T_ , ,*$...Z^ZiZijkk k rmdatarcV||}||S)zFEncrypt given input bytes using the key material given in constructor.)r_encrypt)rjr padded_datas rkencryptzOtaAlgoCrypt.encrypts'++D11 }}[)))rmc,||S)zFDecrypt given input bytes using the key material given in constructor.)_decryptrjrs rkdecryptzOtaAlgoCrypt.decrypts}}T"""rmcdSz:Actual implementation, to be implemented by derived class.Nr~rs rkrzOtaAlgoCrypt._encryptrrmcdSrr~rs rkrzOtaAlgoCrypt._decryptrrmc|D]}|j|jkr ||cS td|jz)zJResolve the class for the encryption algorithm of otak and instantiate it.z(No implementation for crypt algorithm %s)__subclasses__rrbrrdclsrsubcs rkrpzOtaAlgoCrypt.from_keysets]&&(( " "D~00tDzz!!!1CdnTUUUrm)rwrxryrarlrirrrrrr classmethodrp __classcell__rs@rkrtrts0Y *5*U**** #5#U#### IEIeIIII IEIeIIIIVyV^VVV[VVVVVrmrtceZdZdeffd ZdedefdZdedefdZej dedefdZ e deddfd Z xZ S) rorc|j|jkrtd|jd|jt |dSr)rrdrrbrrlrs rkrlzOtaAlgoAuth.__init__sR >T^ + +*$...Z^ZiZijkk k rmrrcZ||}||}|S)zaCompute the CC/CR check bytes for the input data using key material given in constructor.)r_sign)rjrrsigs rksignzOtaAlgoAuth.signs-++D11 jj%% rm cc_receivedc||}||kr0tdt|dt|ddS)zQCompute the CC/CR check bytes for the input data and compare against cc_received.z Received CC (z) != Computed CC ()N)rrr)rjrrr4s rk check_sigzOtaAlgoAuth.check_sigsW YYt__ "  -#kJZJZJZJZ\_`b\c\c\c\c dee e  rmcdSrr~rs rkrzOtaAlgoAuth._signs  rmc|D]}|j|jkr ||cS td|jz)zNResolve the class for the authentication algorithm of otak and instantiate it.z'No implementation for auth algorithm %s)rrrdrrs rkrpzOtaAlgoAuth.from_keysets]&&(( " "D~//tDzz!!!0BT^STTTrm)rwrxryrarlrirrrrrrrprrs@rkrorosY %fUfffff    5    UyU]UUU[UUUUUrmroc>eZdZdZdZdZdZdedefdZdedefdZ d S) OtaAlgoCryptDES;DES is insecure. For backwards compatibility with pre-Rel8rrGrrrctj|jjtj|j}||Srh)rnewrrRMODE_CBCrrrjrciphers rkrzOtaAlgoCryptDES._encrypt/ dg>>~~d###rmctj|jjtj|j}||Srh)rrrrRrrrrs rkrzOtaAlgoCryptDES._decrypt rrmN) rwrxryrznamerrrirrr~rmrkrrsmEE DII$E$e$$$$$E$e$$$$$$rmrc.eZdZdZdZdZdZdedefdZdS) OtaAlgoAuthDESrrrGrrrctj|jjtj|j}||}|t|dz dSNr)rrrrSrrrrrjrrciphs rkrzOtaAlgoAuthDES._signsG dg>>~~d##CIIMNN##rmN) rwrxryrzrrrrirr~rmrkrrsKEE DII$$5$$$$$$rmrc:eZdZdZdZdZdedefdZdedefdZdS) OtaAlgoCryptDES33DESrHrrrctj|jjtj|j}||Srh)rrrrRrrrrs rkrzOtaAlgoCryptDES3._encrypt/$)-@@~~d###rmctj|jjtj|j}||Srh)rrrrRrrrrs rkrzOtaAlgoCryptDES3._decrypt!rrmN rwrxryrrrrirrr~rmrkrrsg D!II$E$e$$$$$E$e$$$$$$rmrc*eZdZdZdZdZdedefdZdS)OtaAlgoAuthDES3rrHrrrctj|jjtj|j}||}|t|dz dSr)rrrrSrrrrrs rkrzOtaAlgoAuthDES3._sign)sG$)-@@~~d##CIIMNN##rmNrwrxryrrrrirr~rmrkrr%sE D!II$$5$$$$$$rmrc:eZdZdZdZdZdedefdZdedefdZdS) OtaAlgoCryptAESrrJrrctj|jjtj|j}||Srh)rrrrRrrrrs rkrzOtaAlgoCryptAES._encrypt2rrmctj|jjtj|j}||Srh)rrrrRrrrrs rkrzOtaAlgoCryptAES._decrypt6rrmNrr~rmrkrr.sg DII$E$e$$$$$E$e$$$$$$rmrc*eZdZdZdZdZdedefdZdS)OtaAlgoAuthAESrrKrrrctj|jjtd}|||}|t|dz dS)Nr) ciphermodmac_len)rrrrSrupdatedigestr)rjrcmacrs rkrzOtaAlgoAuthAES._sign>sTx a@@@ D{{}}CIIMNN##rmNrr~rmrkrr:sE DII$$5$$$$$$rmrc deZdZdZedez dez dedz dedz dez d ez d ee j d z z d e z Z ed ez de z dez dez dedz Zdededededef dZdededeeeeffdZdedededdedffdZdS) OtaDialectSmszDOTA dialect for SMS based transport, as described in 3GPP TS 31.115.rplrhlrTrrfrpcntrresponse_statuscc_rcrrUchlrQrRrSrrrcn||}d}|dr]d|zt|z}|j||jj}t|}t |}||z }|j|jd} |j|j d} d|z} |j | || | |d} |j dd| d dz} | | z|z}t||z}| d d|z}|d d kr#|j|}| |z|z}nj|d d kr4t!j|dz}| | ddz|z}n*|d dkr| |z}nt%d|d z|drH|j|}| |z}t|}| d d|z}n| |z}t|dkrt%d|S)Nrr;r)rDrE )rrQrRrSrTrbigrrr<r4r3lrr2rzDCannot encode command in a single SMS; Fragmentation not implemented)rrrurrrircrbrerd hdr_constructbuildrfto_bytesrrrzlibrMrr)rjrrTrQrlen_sigr len_cipherrrRrSr part_headpart_cnt envelope_datacplr4rMrs rkrzOtaDialectSms.encode_cmdRsg'',, {  Ws4yy0Jj--j$*:NOOG'llG;;D GODlDO<<lDN;;7l&,,S3VYad-e-eff 9%%a//'2B2B1e2L2LL"H,t3 -  7* Q..>  z?d " " ..B$rMD0MM _ $ $J}-- :E$u~~a'?'??$FMM _ - -$tOMM3c*oEFF F {  6:%%m44D%,Mm$$CLLE22]BMM% 5M }   # #cdd drmencodedcX t|ddd}|dd}|dd}|j|}|j|}|dd}t|ddd} t|ddd} |dd}|d } | d d krO|dd} |dd} |dd|z|z| z}|j|| n]| d d kr*t|dd d}t| d dkr|} ntd| d z| dt| | z } |d| | fS)z;Decode an encoded (encrypted, signed) OTA SMS Command-APDU.TNrr rrrrrQr<r4r3rr2rrT) r| from_bytesrurr parserrrrNotImplementedErrorrr)rjrrrrrrhdr_decrrfrrQr4r temp_datacrc32_rxs rk decode_cmdzOtaDialectSms.decode_cmds (..!e44C#I344=D J..t44M $**955!!$~~hrrlE22..!""u55%abb) en z?d " "rr"B $D Q..:XELI I   2 . . . . _ $ $~~mBQB&7??H& % _ - - DD3c*oEFF F&SYYw&&'u~sD((rmrzOtaDialectSms.SmsResponsePacketrct|trt|}|ddkrtd|dzt j|\}}|dstd|dd}|j|}|drk|dd}|j |} || z} |j| }|ddkr|d d|d |d <| }|d d z } |d d kr| rtd| znm|d dkrI|dd} |dd} | | z|d| zdz} |j | |dntd|d z|jdkr6t|d r!t |d }nd}||fS)NrrzUnexpected UDL=0x%02xqzRPI 0x71 not found in UDHrr>rrUrrr?r2z%No RC/CC/DS requested, but len_sig=%ur4rr rzUnknown por_rc_cc_ds: %sr") isinstancer{h2brrrhas_ieSmsResponsePacketrrurrrrrrrr)rjrrQrudhd remainder rph_rhl_tarres ciphered_partdeciphrrudhrpl_rhl_tar_cntr_pcntr_stsdecs rkrzOtaDialectSms.decode_resps3 dC  t99D 7d??4tAw>?? ?(3D99i{{4   :899 9m $**955 & ' "%abbMMZ'' 66F#f,I(..y99C7|q  &).&9.CL=.&IN#!Ie*r/ ~ - / / W#$Kg$UVVV W  D ( (rr(C)23B3 &889RZ[[;QQI I   3w< 8 8 8 8 :S=P PQQ Q  ( * *s3~3F/G/G *#))#n*=>>CCCSzrmN)rwrxryrzrrrr ResponseStatusrrr r$rKICKID_CCr rarirrrrrrr~rmrkrrFsNNuW}$V|$UU1XX~%eeAhh&v~0?&uuTXb['9'99-k9;;F5<sE#IuV|USXSXYZS[S[^\\MAyAuA4AuAQVAAAAF()y()5()U5$PUCU=V()()()()V3 33E3GhjrtGkHGI333333rmr)=rzrrstructtypingrr constructrrrrr r r r r rrrrrosmocom.construct osmocom.utilsr pySim.smsrr. HexAdapterrRC_CC_DSrr/r0KID_RCrQr<SmsCommandPacketPrefixed$SimFileAccessAndToolkitAppSpecParamsra ExceptionrABCrCryptodome.CipherrrrCryptodome.Hashrrrtrorrrrrrrr~rmrkrAsGG" """"""""____________________MMMMMMMMMMMMMMMM$$$$$$(fQAQR#$aRS*+aQ2615+/ 111F/6-jjq.B.BB/ ;0G0GGII 4 AA!a @ @ @i GAJJ dd;;q>>a!*+1FFFFx GAJJDD 8 $${{1~~a1 6 6 66   ikk!nn$ttKKNNQ1VWij$%'''' 5Q'$${{1~~aYZlm()++++   5Q'$${{1~~!YZ[[[[  6-/'.))tx'8v:W:W X XXa.(4 66(.vohhv{>[>[.[.I&.P.A&.H.Nv.U.<]]6SYSYZ^_eZeZ_`fZfThTh>i>i/i.CF.J.3HHV[4Q4Q.Q.:88FKKX]X]^_X`X`LaLa;b;b.b(d(d$........,     I   <<<<<<<<4-,,,,,,,,, '''''cg'''8VVVVV7CGVVV>UUUUU'37UUU@ $ $ $ $ $l $ $ $$$$$$[$$$ $ $ $ $ $| $ $ $$$$$$k$$$ $ $ $ $ $l $ $ $$$$$$[$$$mmmmmJmmmmmrm