B c@@sdZdZddlZddlZddlmZmZddlTejdkrDddZ ne Z Gd d d Z e fd d Z Gd ddej Z Gddde ZGdddej ZGdddej ZGdddej ZGdddej ZGdddej ZGdddej ZddZdd ZdS)!z&Self-testing for PyCrypto hash modulesz$Id$N)a2b_hexb2a_hex)*icKs|S)N)copy)kwargsrP/opt/alt/python37/lib64/python3.7/site-packages/Crypto/SelfTest/Cipher/common.pydict&sr c@s eZdZdS) _NoDefaultN)__name__ __module__ __qualname__rrrrr +sr cCs6y ||}Wntk r*|tkr&|SX||=|S)zAGet an item from a dictionary, and remove it from the dictionary.)KeyErrorr )dkdefaultZretvalrrr_extract,s rc@s.eZdZddZddZd ddZdd Zd S) CipherSelfTestcCstj|||_|}t|d|_tt|d|_tt|d|_ tt|d|_ t|dd|_ t|dd}t ||_ |dk rt|jd||_t|dd|_|jdk rt|j|_t|d d|_|jdk rt|j|_n d|_d|_||_dS) N descriptionkey plaintext ciphertext module_namemodeZMODE_iv encrypted_iv)unittestTestCase__init__modulerrrbrrrrstr mode_namegetattrrrr extra_params)selfrparamsrrrrr:s*      zCipherSelfTest.__init__cCs|jS)N)r)r%rrrshortDescriptionYszCipherSelfTest.shortDescriptionrcCsp|j}t|jdr|j|jjkrddlm}t|d|j }t|di}d|krjt t |d|d<d|krt t |d|d<d|krd |jj t |dd t |dd |d<|f||d <|jdkr|jj t |jf|S|jdkr|jj t |j|jf|S|rJ|j|jjkrJ|jj t |j|jt |jf|S|jj t |j|jt |jf|SdS) NMODE_CTRr)Counter ctr_class ctr_paramsprefixsuffixZnbitscounter)r$rhasattrrrr( Crypto.Utilr)rnewrr block_sizelengetrr MODE_OPENPGPr)r%Z do_decryptionr&r)r*r+rrr_new\s&  0  "zCipherSelfTest._newcCst|j}t|j}t||}t|d|}t||}t|d|}t|jdr|j |jj krt |j }| |j |d|| |j |d|||d}||d}| |j|| |j|| |j|| |j|dS)Nr7)rrrrr8encryptdecryptr1rrr7r5r assertEqual)r%rrZct1Zpt1Zct2Zpt2ZeilenrrrrunTestxs      zCipherSelfTest.runTestN)r)r r r rr'r8r=rrrrr8s rc@seZdZddZddZdS)CipherStreamingSelfTestcCs*|j}|jdk r |d|jf7}d|fS)Nz in %s modez%%s should behave like a stream cipher)rrr")r%Zdescrrrr's z(CipherStreamingSelfTest.shortDescriptionc Cst|j}t|j}g}|}x4tdt|dD] }|||||dq2Wtt d |}| |j|g}|}x4tdt|dD] }|||||dqWtt d |}| |j|dS)Nrr/) rrrr8ranger5appendr:rr joinr<)r%rrZct3cipheriZpt3rrrr=s    zCipherStreamingSelfTest.runTestN)r r r r'r=rrrrr>sr>c@s$eZdZddZddZddZdS)CTRSegfaultTestcCs2tj|||_t|d|_|dd|_dS)Nrr)rrrrr rr6r)r%rr&rrrrs zCTRSegfaultTest.__init__cCsd|j|jfS)NzNRegression test: %s.new(key, %s.MODE_CTR) should raise TypeError, not segfault)r)r%rrrr'sz CTRSegfaultTest.shortDescriptioncCs"|t|jjt|j|jjdS)N) assertRaises TypeErrorrr3rrr()r%rrrr=szCTRSegfaultTest.runTestN)r r r rr'r=rrrrrEsrEc@s$eZdZddZddZddZdS)CTRWraparoundTestcCs2tj|||_t|d|_|dd|_dS)Nrr)rrrrr rr6r)r%rr&rrrrs zCTRWraparoundTest.__init__cCs d|jfS)Nz]Regression test: %s with MODE_CTR should raise OverflowError on wraparound when shortcut used)r)r%rrrr'sz"CTRWraparoundTest.shortDescriptioncCsddlm}xdD]}x|dD]t}|jd|jjdd|jjd||d}|jjt|j|jj|d}td |jj}| || t |j |qWqWdS) Nr)r))rr9r.r9)Z initial_value little_endiandisable_shortcut)r0) r2r)r3rr4rrr(r r:rF OverflowError)r%r)rKrJZctrrCblockrrrr=s   * zCTRWraparoundTest.runTestN)r r r rr'r=rrrrrHsrHc@s$eZdZddZddZddZdS)CFBSegmentSizeTestcCs.tj|||_t|d|_|d|_dS)Nrr)rrrrr rr)r%rr&rrrrs zCFBSegmentSizeTest.__init__cCs|jS)N)r)r%rrrr'sz#CFBSegmentSizeTest.shortDescriptioncCs`x4tddD]&}|jt|jjt|j|jj|dq W|jjt|j|jjd|jjdddS)znRegression test: m.new(key, m.MODE_CFB, segment_size=N) should require segment_size to be a multiple of 8 bitsr9r.)Z segment_sizerLN) r@rF ValueErrorrr3rrMODE_CFBr4)r%rDrrrr=s&zCFBSegmentSizeTest.runTestN)r r r rr'r=rrrrrOsrOc@s$eZdZddZddZddZdS) RoundtripTestcCs^ddlm}tj|||_||j|_t |d|_ dt |d|_ | dd|_ dS)Nr)Randomrdrr)ZCryptorSrrrrZget_random_bytesr4rr rrr6r)r%rr&rSrrrrs  zRoundtripTest.__init__cCs d|jfS)Nz8%s .decrypt() output of .encrypt() should not be garbled)r)r%rrrr'szRoundtripTest.shortDescriptioncCsx|jj|jj|jj|jj|jjfD]}|jt|j||j }| |j }||jjkrr|jt|j||j }n>|d|jj d}||jj dd}|jt|j||}| |}||j |q$WdS)NrI)rMODE_ECBMODE_CBCrQMODE_OFBr7r3rrrr:rr4r;r<)r%rZencryption_cipherrZdecryption_cipherZeivZdecrypted_plaintextrrrr=s(   zRoundtripTest.runTestN)r r r rr'r=rrrrrRs rRc@s$eZdZddZddZddZdS)PGPTestcCs$tj|||_t|d|_dS)Nr)rrrrr r)r%rr&rrrrs zPGPTest.__init__cCsdS)NzRMODE_PGP was implemented incorrectly and insecurely. It's completely banished now.r)r%rrrr'szPGPTest.shortDescriptioncCs"|t|jjt|j|jjdS)N)rFrPrr3rrZMODE_PGP)r%rrrr=szPGPTest.runTestN)r r r rr'r=rrrrrXsrXc@s,eZdZddZddZddZddZd S) IVLengthTestcCs$tj|||_t|d|_dS)Nr)rrrrr r)r%rr&rrrr s zIVLengthTest.__init__cCsdS)NzTCheck that all modes except MODE_ECB and MODE_CTR require an IV of the proper lengthr)r%rrrr'szIVLengthTest.shortDescriptioncCs|t|jjt|j|jjd|t|jjt|j|jjd|t|jjt|j|jjd|t|jjt|j|jj d|jt|j|jj d|jjt|j|jj d|j ddS)Nr/)r0) rFrPrr3rrrVrQrWr7rUr(_dummy_counter)r%rrrr=s    zIVLengthTest.runTestcCs d|jjS)NrL)rr4)r%rrrrZ!szIVLengthTest._dummy_counterN)r r r rr'r=rZrrrrrY s rYc CsTg}d}xDtt|D]2}||}ddi}t|dkrR|\|d<|d<|d<nrt|dkr||\|d<|d<|d<|d <nHt|d kr|\|d<|d<|d<|d <}||ntd t|f|} t| d} t| d} t| d} t| d d} | dd}|dkrt| dd| dk r(| }n,|dkrF| sFd | | f}nd | | | f}d||d|f}||d <||d<|s|t||t||t ||t ||t ||t ||g7}d}| t|||dkr| t|||dkrd|kr|}|d d7<|di}||d<d|dkr)rr test_datatestsZextra_tests_addedrDrowr&r$p2p_key p_plaintext p_ciphertext p_descriptionZp_modernameZparams2Z ctr_params2rrrmake_block_tests$sd              rhcCshg}x\tt|D]J}||}i}t|dkrJ|\|d<|d<|d<nrt|dkrt|\|d<|d<|d<|d<nHt|dkr|\|d<|d<|d<|d<}||ntdt|f|}t|d} t|d} t|d} t|dd} | dk r| } n"|sd | | f} nd | | |f} d ||d | f}||d<||d <|t|||t||qW|S)Nr?rrrr[rr\zUnsupported tuple size %dz p=%s, k=%szp=%s, k=%s, %rz %s #%d: %sr9r) r@r5r]r^rrrArr>)rrr_r`rDrar&r$rbrcrdrerfrrgrrrmake_stream_testshs8        ri)__doc__Z __revision__sysrZbinasciirrZCrypto.Util.py3compat hexversionr r rrrr>rErHrOrRrXrYrhrirrrrs(   X D