B
    ?[,C                 @   s  d Z dZddlZddlZddlmZmZ ddlT ejdk rDdd Z	ne	Z	G d	d
 d
Z
e
fddZG dd dejZG dd deZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZG dd dejZdd Zdd  ZdS )!z&Self-testing for PyCrypto hash modulesz$Id$    N)a2b_hexb2a_hex)*i  c              K   s   |   S )N)copy)kwargs r   <lib/python3.7/site-packages/Crypto/SelfTest/Cipher/common.pydict&   s    r	   c               @   s   e Zd ZdS )
_NoDefaultN)__name__
__module____qualname__r   r   r   r   r
   +   s    r
   c             C   s6   y| | }W n t k
r*   |tkr& |S X | |= |S )zAGet an item from a dictionary, and remove it from the dictionary.)KeyErrorr
   )dkdefaultZretvalr   r   r   _extract,   s    r   c               @   s.   e Zd Zdd Zdd ZdddZdd	 Zd
S )CipherSelfTestc             C   s   t j|  || _| }t|d| _tt|d| _tt|d| _	tt|d| _
t|dd | _t|dd }t|| _|d k	rt| jd| | _t|dd | _| jd k	rt| j| _t|d	d | _| jd k	rt| j| _nd | _d | _|| _d S )
Ndescriptionkey	plaintext
ciphertextmodule_namemodeZMODE_ivencrypted_iv)unittestTestCase__init__moduler   r   r   br   r   r   r   str	mode_namegetattrr   r   r   extra_params)selfr   paramsr   r   r   r   r   :   s*    

 
zCipherSelfTest.__init__c             C   s   | j S )N)r   )r%   r   r   r   shortDescriptionY   s    zCipherSelfTest.shortDescriptionr   c             C   sp  | j  }t| jdr| j| jjkrddlm} t|d|j	}t|di  }d|krjt
t|d |d< d|krt
t|d |d< d|krd	| jjt|dd
 t|dd
  |d< |f ||d< | jd kr| jj	t
| jf|S | jd kr| jj	t
| j| jf|S |rJ| j| jjkrJ| jj	t
| j| jt
| jf|S | jj	t
| j| jt
| jf|S d S )NMODE_CTRr   )Counter	ctr_class
ctr_paramsprefixsuffixZnbits    counter)r$   r   hasattrr   r   r(   Crypto.Utilr)   r   newr   r    
block_sizelengetr   r   MODE_OPENPGPr   )r%   Zdo_decryptionr&   r)   r*   r+   r   r   r   _new\   s&    
  0
"zCipherSelfTest._newc             C   s  t | j}t | j}t|  |}t| d|}t|  |}t| d|}t| jdr| j	| jj
krt| j}| | j|d |  | | j|d |  ||d  }||d  }| | j| | | j| | | j| | | j| d S )N   r7   )r   r   r   r   r8   encryptdecryptr1   r   r   r7   r5   r   assertEqual)r%   r   r   Zct1Zpt1Zct2Zpt2Zeilenr   r   r   runTestx   s     


zCipherSelfTest.runTestN)r   )r   r   r   r   r'   r8   r=   r   r   r   r   r   8   s   
r   c               @   s   e Zd Zdd Zdd ZdS )CipherStreamingSelfTestc             C   s*   | j }| jd k	r |d| jf 7 }d|f S )Nz in %s modez%%s should behave like a stream cipher)r   r   r"   )r%   Zdescr   r   r   r'      s    
z(CipherStreamingSelfTest.shortDescriptionc          	   C   s   t | j}t | j}g }|  }x4tdt|dD ] }|||||d   q2W tt	d
|}| | j| g }|  }x4tdt|dD ] }|||||d   qW tt	d
|}| | j| d S )Nr      r/   )r   r   r   r8   ranger5   appendr:   r   r    joinr<   )r%   r   r   Zct3cipheriZpt3r   r   r   r=      s    

  zCipherStreamingSelfTest.runTestN)r   r   r   r'   r=   r   r   r   r   r>      s   r>   c               @   s$   e Zd Zdd Zdd Zdd ZdS )CTRSegfaultTestc             C   s2   t j|  || _t|d | _|dd | _d S )Nr   r   )r   r   r   r   r    r   r6   r   )r%   r   r&   r   r   r   r      s    zCTRSegfaultTest.__init__c             C   s   d| j | j f S )NzNRegression test: %s.new(key, %s.MODE_CTR) should raise TypeError, not segfault)r   )r%   r   r   r   r'      s    z CTRSegfaultTest.shortDescriptionc             C   s"   |  t| jjt| j| jj d S )N)assertRaises	TypeErrorr   r3   r   r   r(   )r%   r   r   r   r=      s    zCTRSegfaultTest.runTestN)r   r   r   r   r'   r=   r   r   r   r   rE      s   rE   c               @   s$   e Zd Zdd Zdd Zdd ZdS )CTRWraparoundTestc             C   s2   t j|  || _t|d | _|dd | _d S )Nr   r   )r   r   r   r   r    r   r6   r   )r%   r   r&   r   r   r   r      s    zCTRWraparoundTest.__init__c             C   s   d| j f S )Nz]Regression test: %s with MODE_CTR should raise OverflowError on wraparound when shortcut used)r   )r%   r   r   r   r'      s    z"CTRWraparoundTest.shortDescriptionc             C   s   ddl m} xdD ]}x|dD ]t}|jd| jj dd| jj  d ||d}| jjt| j| jj|d}td	| jj }|	| | 
t|j	| qW qW d S )
Nr   )r)   )r   r9   r.      r9   )Zinitial_valuelittle_endiandisable_shortcut)r0    )r2   r)   r3   r   r4   r   r   r(   r    r:   rF   OverflowError)r%   r)   rK   rJ   ZctrrC   blockr   r   r   r=      s    

*
zCTRWraparoundTest.runTestN)r   r   r   r   r'   r=   r   r   r   r   rH      s   rH   c               @   s$   e Zd Zdd Zdd Zdd ZdS )CFBSegmentSizeTestc             C   s.   t j|  || _t|d | _|d | _d S )Nr   r   )r   r   r   r   r    r   r   )r%   r   r&   r   r   r   r      s    zCFBSegmentSizeTest.__init__c             C   s   | j S )N)r   )r%   r   r   r   r'      s    z#CFBSegmentSizeTest.shortDescriptionc             C   s`   x4t ddD ]&}| jt| jjt| j| jj|d qW | jjt| j| jjd| jj dd dS )znRegression test: m.new(key, m.MODE_CFB, segment_size=N) should require segment_size to be a multiple of 8 bitsr9   r.   )Zsegment_sizerL   N)	r@   rF   
ValueErrorr   r3   r   r   MODE_CFBr4   )r%   rD   r   r   r   r=      s    &zCFBSegmentSizeTest.runTestN)r   r   r   r   r'   r=   r   r   r   r   rO      s   rO   c               @   s$   e Zd Zdd Zdd Zdd ZdS )RoundtripTestc             C   s^   ddl m} tj|  || _||j| _t	|d | _
dt	|d  | _|dd | _d S )Nr   )Randomr   d   r   r   )ZCryptorS   r   r   r   r   Zget_random_bytesr4   r   r    r   r   r6   r   )r%   r   r&   rS   r   r   r   r      s    zRoundtripTest.__init__c             C   s   d| j f S )Nz8%s .decrypt() output of .encrypt() should not be garbled)r   )r%   r   r   r   r'      s    zRoundtripTest.shortDescriptionc             C   sN  | j j}| j t| j|}|| j}| j t| j|}||}| | j| | j j	}| j t| j|| j
}|| j}|d | j jd  }|| j jd d  }| j t| j||}||}| | j| xr| j j| j j| j jfD ]X}| j t| j|| j
}|| j}| j t| j|| j
}||}| | j| qW d S )NrI   )r   MODE_ECBr3   r   r   r:   r   r;   r<   r7   r   r4   MODE_CBCrQ   MODE_OFB)r%   r   Zencryption_cipherr   Zdecryption_cipherZdecrypted_plaintextZeiv_ciphertextZeivr   r   r   r=      s(    


zRoundtripTest.runTestN)r   r   r   r   r'   r=   r   r   r   r   rR      s   	rR   c               @   s$   e Zd Zdd Zdd Zdd ZdS )PGPTestc             C   s$   t j|  || _t|d | _d S )Nr   )r   r   r   r   r    r   )r%   r   r&   r   r   r   r     s    zPGPTest.__init__c             C   s   dS )NzRMODE_PGP was implemented incorrectly and insecurely. It's completely banished now.r   )r%   r   r   r   r'     s    zPGPTest.shortDescriptionc             C   s"   |  t| jjt| j| jj d S )N)rF   rP   r   r3   r   r   ZMODE_PGP)r%   r   r   r   r=     s    zPGPTest.runTestN)r   r   r   r   r'   r=   r   r   r   r   rX     s   rX   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
IVLengthTestc             C   s$   t j|  || _t|d | _d S )Nr   )r   r   r   r   r    r   )r%   r   r&   r   r   r   r     s    zIVLengthTest.__init__c             C   s   dS )NzTCheck that all modes except MODE_ECB and MODE_CTR require an IV of the proper lengthr   )r%   r   r   r   r'   !  s    zIVLengthTest.shortDescriptionc             C   s   |  t| jjt| j| jjd |  t| jjt| j| jjd |  t| jjt| j| jjd |  t| jjt| j| jj	d | jt| j| jj
d | jjt| j| jjd| jd d S )Nr/   )r0   )rF   rP   r   r3   r   r   rV   rQ   rW   r7   rU   r(   _dummy_counter)r%   r   r   r   r=   $  s    zIVLengthTest.runTestc             C   s   d| j j S )NrL   )r   r4   )r%   r   r   r   rZ   0  s    zIVLengthTest._dummy_counterN)r   r   r   r   r'   r=   rZ   r   r   r   r   rY     s   rY   c          
   C   sT  g }d}xDt t|D ]2}|| }ddi}t|dkrR|\|d< |d< |d< nrt|dkr||\|d< |d< |d< |d	< nHt|d
kr|\|d< |d< |d< |d	< }|| ntdt|f | }	t|	d}
t|	d}t|	d}t|	d	d }|	dd}|dkrt|	dd |d k	r(|}n,|dkrF|	sFd||
f }nd||
|	f }d||d |f }||d	< ||d< |s|t| |t| |t	| |t
| |t| |t| |g7 }d}|t| | |dkr|t| | |dkrd|kr| }|d	  d7  < |di  }||d< d|d kr<d|d d< |t| | qW |S )Nr   r   ZECBr?   r   r   r      r      zUnsupported tuple size %dz
p=%s, k=%szp=%s, k=%s, %rz
%s #%d: %sr9   r   ZCTRr*   z (shortcut disabled)r+   rK   )r@   r5   updateAssertionErrorr   r   r6   rE   rH   rO   rR   rX   rY   rA   r   r>   )r   r   	test_datatestsZextra_tests_addedrD   rowr&   r$   p2p_keyp_plaintextp_ciphertextp_descriptionZp_moder   nameZparams2Zctr_params2r   r   r   make_block_tests3  sd    





rh   c             C   sh  g }x\t t|D ]J}|| }i }t|dkrJ|\|d< |d< |d< nrt|dkrt|\|d< |d< |d< |d< nHt|dkr|\|d< |d< |d< |d< }|| ntdt|f | }t|d}	t|d}
t|d}t|dd }|d k	r|}n"|sd	|
|	f }nd
|
|	|f }d||d |f }||d< ||d< |t| | |t| | qW |S )Nr?   r   r   r   r[   r   r\   zUnsupported tuple size %dz
p=%s, k=%szp=%s, k=%s, %rz
%s #%d: %sr9   r   )	r@   r5   r]   r^   r   r   rA   r   r>   )r   r   r_   r`   rD   ra   r&   r$   rb   rc   rd   re   rf   r   rg   r   r   r   make_stream_testsw  s8    


ri   )__doc__Z__revision__sysr   Zbinasciir   r   ZCrypto.Util.py3compat
hexversionr	   r
   r   r   r   r>   rE   rH   rO   rR   rX   rY   rh   ri   r   r   r   r   <module>   s(   

X*D