wec@sNdZddlZddlmZmZmZddlmZddlm Z dddYZ e dZ d ej fd YZ d e fd YZd ej fdYZdej fdYZdej fdYZdej fdYZdej fdYZdej fdYZedZdZdS(s&Self-testing for PyCrypto hash modulesiN(ta2b_hextb2a_hexthexlify(tb(tstrxor_ct _NoDefaultcBseZRS((t__name__t __module__(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR!scCs@y||}Wn$tk r4|tkr0n|SX||=|S(sAGet an item from a dictionary, and remove it from the dictionary.(tKeyErrorR(tdtktdefaulttretval((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyt_extract"s  tCipherSelfTestcBs5eZdZdZdZdZdZRS(cCstjj|||_|j}t|d|_tt|d|_tt|d|_ tt|d|_ t|dd|_ t|dd|_ t|dd|_|j rt|j|_nt|dd}t||_|dk rt|jd ||_t|d d|_|jdkr^t|d d|_n|jdk rt|j|_qnBd|_t|d d|_|jdk rt|j|_n||_dS( Nt descriptiontkeyt plaintextt ciphertextt module_namet assoc_datatmactmodetMODE_tivtnonce(tunittesttTestCaset__init__tmoduletcopyR RRRRRtNoneRRRtstrt mode_nametgetattrRRt extra_params(tselfRtparamsR((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR0s4     cCs|jS(N(R(R$((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pytshortDescriptionVscCs|jj}t|j}g}|jdk rB|jg}n|jdk rj|t|jg7}n|jj|||S(N( R#RRRRRRRtnew(R$R%Rt old_style((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyt_newYscCs7t|jd|stS|jt|jd|kS(NR(thasattrRtFalseRR"(R$tname((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pytisModeescCst|j}t|j}g}|jrXg|jD]}tt|^q7}nd}d}xtdD]}|j}|j} x(|D] } |j| | j| qWt |j |} t | j |} |r |j || |j || n| | }}qqW|j |j||j |j||j rt |j} |j |j | | jt|j ndS(Ni(RRRRRRtrangeR)tupdateRtencrypttdecryptt assertEqualRtdigesttverify(R$RRRtxtcttpttitciphertdeciphertcomptctXtptXR((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pytrunTestjs2 +     (RRRR&R)R-R>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR.s  &  tCipherStreamingSelfTestcBseZdZdZRS(cCs:|j}|jdk r/|d|jf7}nd|fS(Ns in %s modes%%s should behave like a stream cipher(RRRR!(R$tdesc((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR&s cCs*t|j}t|j}g}|j}xAtdt|dD]'}|j|j|||d!qIWtt dj |}|j |j|g}|j}xAtdt|dD]'}|j|j|||d!qWtt dj |}|j |j|dS(Niit( RRRR)R.tlentappendR0RRtjoinR2(R$RRtct3R9R8tpt3((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>s % %(RRR&R>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR?s t RoundtripTestcBs#eZdZdZdZRS(cCsddlm}tjj|||_|j|j|_t |d|_ dt |d|_ |j dd|_dS(Ni(tRandomRidRR(tCryptoRHRRRRtget_random_bytest block_sizeRRRRtgetRR(R$RR%RH((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRs cCsd|jfS(Ns8%s .decrypt() output of .encrypt() should not be garbled(R(R$((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR&scCs|jj}|jjt|j|}|j|j}|jjt|j|}|j|}|j|j|dS(N( RtMODE_ECBR'RRR0RR1R2(R$Rtencryption_cipherRtdecryption_ciphertdecrypted_plaintext((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>s  (RRRR&R>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRGs t IVLengthTestcBs,eZdZdZdZdZRS(cCs0tjj|||_t|d|_dS(NR(RRRRRR(R$RR%((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRs cCsdS(NsTCheck that all modes except MODE_ECB and MODE_CTR require an IV of the proper length((R$((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR&scCs8|jt|jjt|j|jjtddS(NRA(t assertRaisest TypeErrorRR'RRRMR(R$((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>scCsd|jjS(Nt(RRK(R$((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyt_dummy_counters(RRRR&R>RU(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRQs   tNoDefaultECBTestcBseZdZdZRS(cCs0tjj|||_t|d|_dS(NR(RRRRRR(R$RR%((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRs cCs&|jt|jjt|jdS(N(RRRSRR'RR(R$((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>s(RRRR>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRVs t BlockSizeTestcBseZdZdZRS(cCs6tjj|||_tt|d|_dS(NR(RRRRRRR(R$RR%((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRs cCs;|jj|j|jj}|j|j|jjdS(N(RR'RRMR2RK(R$R9((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>s(RRRR>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRWs t ByteArrayTestcBs)eZdZdZdZdZRS(s;Verify we can use bytearray's for encrypting and decryptingcCstjj|||_|j}t|d|_tt|d|_tt|d|_ tt|d|_ t|dd|_ t|dd|_ t|dd|_|j rt|j|_nt|dd}t||_|dk rt|jd ||_t|d d|_|jdkr^t|d d|_n|jdk rt|j|_qnBd|_t|d d|_|jdk rt|j|_n||_dS( NRRRRRRRRRRR(RRRRRR RRRRRRRRRR R!R"RRR#(R$RR%R((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRs4     cCs|jj}t|j}g}|jdk rB|jg}n|jdk rj|t|jg7}n|jj|||S(N( R#RRRRRRRR'(R$R%RR(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR)sc CsNt|j}t|j}g}|jr^g|jD]}ttt|^q7}n|j}|j}x(|D] }|j||j|q}Wt|j t|}t|j t|} |j |j||j |j| |j rJt|j } |j |j | |jtt|j ndS(N(RRRRt bytearrayRR)R/RR0R1R2RR3R4( R$RRRR5R9R:R;R6R7R((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>#s$ 1     (RRt__doc__RR)R>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRXs % tMemoryviewTestcBs)eZdZdZdZdZRS(s;Verify we can use memoryviews for encrypting and decryptingcCstjj|||_|j}t|d|_tt|d|_tt|d|_ tt|d|_ t|dd|_ t|dd|_ t|dd|_|j rt|j|_nt|dd}t||_|dk rt|jd ||_t|d d|_|jdkr^t|d d|_n|jdk rt|j|_qnBd|_t|d d|_|jdk rt|j|_n||_dS( NRRRRRRRRRRR(RRRRRR RRRRRRRRRR R!R"RRR#(R$RR%R((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyRBs4     cCs|jj}t|j}g}|jdk rB|jg}n|jdk rj|t|jg7}n|jj|||S(N( R#RRRRRRRR'(R$R%RR(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR)gsc CsNt|j}t|j}g}|jr^g|jD]}ttt|^q7}n|j}|j}x(|D] }|j||j|q}Wt|j t|}t|j t|} |j |j||j |j| |j rJt|j } |j |j | |jtt|j ndS(N(RRRRt memoryviewRR)R/RR0R1R2RR3R4( R$RRRR5R9R:R;R6R7R((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR>ss$ 1     (RRRZRR)R>(((sC/usr/lib64/python2.7/site-packages/Crypto/SelfTest/Cipher/common.pyR[?s % c Cshg}t}xUtt|D]A}||}i}t|dkre|\|d<|d<|ds   c   PP>