o
    ]¨Êh¸@  ã                   @   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 r"d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   úO/var/www/html/env/lib/python3.10/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   s8   z| | }W n t y   |tu r‚ | Y S w | |= |S )zAGet an item from a dictionary, and remove it from the dictionary.)ÚKeyErrorr   )ÚdÚkÚdefaultÚ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 urot| jd| ƒ| _t|dd ƒ| _| jd ur\t| jƒ| _t|d	d ƒ| _| jd urnt| jƒ| _nd | _d | _|| _d S )
NÚdescriptionÚkeyÚ	plaintextÚ
ciphertextÚmodule_nameÚmodeÚ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   ó   | j S r   ©r   ©r(   r   r   r	   ÚshortDescriptionY   ó   zCipherSelfTest.shortDescriptionr   c                 C   sz  | j  ¡ }t| jdƒrh| j| jjkrhddlm} t|d|j	ƒ}t|di ƒ ¡ }d|v r5t
t|d ƒƒ|d< d|v rCt
t|d ƒƒ|d< d|vr_d	| jjt| dd
¡ƒ t| dd
¡ƒ  |d< |di |¤Ž|d< | jd u rz| jj	t
| jƒfi |¤ŽS | jd u rŽ| jj	t
| jƒ| jfi |¤ŽS |rª| j| jjkrª| jj	t
| jƒ| jt
| jƒfi |¤ŽS | jj	t
| jƒ| jt
| jƒfi |¤ŽS )NÚMODE_CTRr   ©ÚCounterÚ	ctr_classÚ
ctr_paramsÚprefixÚsuffixÚnbitsé   Ú Úcounterr   )r'   r   Úhasattrr"   r   r/   ÚCrypto.Utilr1   r   Únewr   r#   Ú
block_sizeÚlenÚgetr   r   ÚMODE_OPENPGPr   )r(   Údo_decryptionr)   r1   r2   r3   r   r   r	   Ú_new\   s"   
0

&&zCipherSelfTest._newc                 C   s  t | jƒ}t | jƒ}t|  ¡  |¡ƒ}t|  d¡ |¡ƒ}t|  ¡  |¡ƒ}t|  d¡ |¡ƒ}t| jdƒrd| j	| jj
krdt| jƒ}|  | j|d |… ¡ |  | j|d |… ¡ ||d … }||d … }|  | j|¡ |  | j|¡ |  | j|¡ |  | j|¡ d S )Né   r@   )r   r   r   r   rB   ÚencryptÚdecryptr:   r"   r   r@   r>   r   ÚassertEqual)r(   r   r   Úct1Úpt1Úct2Úpt2Úeilenr   r   r	   ÚrunTestx   s    


zCipherSelfTest.runTestN)r   )r   r   r   r!   r-   rB   rL   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 ur|d| jf 7 }d|f S )Nz in %s modez%%s should behave like a stream cipher)r   r   r%   )r(   Údescr   r   r	   r-   ’   s   

z(CipherStreamingSelfTest.shortDescriptionc              	   C   sÔ   t | jƒ}t | jƒ}g }|  ¡ }tdt|ƒdƒD ]}| | |||d … ¡¡ qtt	dƒ 
|¡ƒ}|  | j|¡ g }|  ¡ }tdt|ƒdƒD ]}| | |||d … ¡¡ qGtt	dƒ 
|¡ƒ}|  | j|¡ d S )Nr   é   r8   )r   r   r   rB   Úranger>   ÚappendrD   r   r#   ÚjoinrF   )r(   r   r   Úct3ÚcipherÚiÚpt3r   r   r	   rL   ˜   s   

zCipherStreamingSelfTest.runTestN)r   r   r   r-   rL   r   r   r   r	   rM   �   s    rM   c                   @   ó$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚCTRSegfaultTestc                 C   ó2   t j | ¡ || _t|d ƒ| _| dd ¡| _d S ©Nr   r   ©r   r    r!   r"   r#   r   r?   r   ©r(   r"   r)   r   r   r	   r!   ±   ó   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   ó"   |   t| jjt| jƒ| jj¡ d S r   )ÚassertRaisesÚ	TypeErrorr"   r<   r   r   r/   r,   r   r   r	   rL   º   s   "zCTRSegfaultTest.runTestN©r   r   r   r!   r-   rL   r   r   r   r	   rX   ¯   ó    rX   c                   @   rW   )ÚCTRWraparoundTestc                 C   rY   rZ   r[   r\   r   r   r	   r!   ¿   r]   zCTRWraparoundTest.__init__c                 C   ó   d| j f S )Nz]Regression test: %s with MODE_CTR should raise OverflowError on wraparound when shortcut usedr^   r,   r   r   r	   r-   Å   ó   z"CTRWraparoundTest.shortDescriptionc                 C   s”   ddl m} dD ]?}dD ]:}|jd| jj dd| jj  d ||d�}| jjt| jƒ| jj|d�}td	ƒ| jj }| 	|¡ |  
t|j	|¡ qqd S )
Nr   r0   )r   rC   r7   é   rC   )Úinitial_valueÚlittle_endianÚdisable_shortcut©r9   ú )r;   r1   r<   r"   r=   r   r   r/   r#   rD   r`   ÚOverflowError)r(   r1   rj   ri   ÚctrrT   Úblockr   r   r	   rL   È   s   *
ûÿzCTRWraparoundTest.runTestNrb   r   r   r   r	   rd   ½   rc   rd   c                   @   rW   )ÚCFBSegmentSizeTestc                 C   s.   t j | ¡ || _t|d ƒ| _|d | _d S )Nr   r   )r   r    r!   r"   r#   r   r   r\   r   r   r	   r!   Õ   s   zCFBSegmentSizeTest.__init__c                 C   r*   r   r+   r,   r   r   r	   r-   Û   r.   z#CFBSegmentSizeTest.shortDescriptionc                 C   s\   t ddƒD ]}| jt| jjt| jƒ| jj|d� q| 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 bitsrC   r7   )Úsegment_sizerl   N)	rP   r`   Ú
ValueErrorr"   r<   r   r   ÚMODE_CFBr=   )r(   rU   r   r   r	   rL   Þ   s   $*zCFBSegmentSizeTest.runTestNrb   r   r   r   r	   rp   Ó   rc   rp   c                   @   rW   )ÚRoundtripTestc                 C   s^   ddl m} tj | ¡ || _| |j¡| _t	|d ƒ| _
dt	|d ƒ | _| dd ¡| _d S )Nr   )ÚRandomr   éd   r   r   )ÚCryptoru   r   r    r!   r"   Úget_random_bytesr=   r   r#   r   r   r?   r   )r(   r"   r)   ru   r   r   r	   r!   å   s   zRoundtripTest.__init__c                 C   re   )Nz8%s .decrypt() output of .encrypt() should not be garbledr^   r,   r   r   r	   r-   î   rf   zRoundtripTest.shortDescriptionc                 C   sÌ   | j j| j j| j j| j j| j jfD ]R}| j  t| jƒ|| j	¡}| 
| j¡}|| j jkr8| j  t| jƒ|| j	¡}n|d | j jd … }|| j jd d … }| j  t| jƒ||¡}| |¡}|  | j|¡ qd S )Nrg   )r"   ÚMODE_ECBÚMODE_CBCrs   ÚMODE_OFBr@   r<   r   r   r   rD   r   r=   rE   rF   )r(   r   Úencryption_cipherr   Údecryption_cipherÚeivÚdecrypted_plaintextr   r   r	   rL   ñ   s   &
õzRoundtripTest.runTestNrb   r   r   r   r	   rt   ä   s    	rt   c                   @   rW   )ÚPGPTestc                 C   ó$   t j | ¡ || _t|d ƒ| _d S ©Nr   ©r   r    r!   r"   r#   r   r\   r   r   r	   r!      ó   zPGPTest.__init__c                 C   ó   dS )NzRMODE_PGP was implemented incorrectly and insecurely. It's completely banished now.r   r,   r   r   r	   r-     ó   zPGPTest.shortDescriptionc                 C   r_   r   )r`   rr   r"   r<   r   r   ÚMODE_PGPr,   r   r   r	   rL     s   ÿzPGPTest.runTestNrb   r   r   r   r	   r€   ÿ   s    r€   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚIVLengthTestc                 C   r�   r‚   rƒ   r\   r   r   r	   r!     r„   zIVLengthTest.__init__c                 C   r…   )NzTCheck that all modes except MODE_ECB and MODE_CTR require an IV of the proper lengthr   r,   r   r   r	   r-     r†   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 )Nr8   rk   )r`   rr   r"   r<   r   r   rz   rs   r{   r@   ry   r/   Ú_dummy_counterr,   r   r   r	   rL     s   ÿÿÿÿ$zIVLengthTest.runTestc                 C   s   d| j j S )Nrl   )r"   r=   r,   r   r   r	   r‰   !  rf   zIVLengthTest._dummy_counterN)r   r   r   r!   r-   rL   r‰   r   r   r   r	   rˆ     s
    rˆ   c              
   C   sF  g }d}t t|ƒƒD �]}|| }ddi}t|ƒdkr'|\|d< |d< |d< n9t|ƒdkr<|\|d< |d< |d< |d	< n$t|ƒd
krW|\|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 ur�|}n|dkr�|	s�d||
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|v�r | ¡ }|d	  d7  < | di ¡ ¡ }||d< d|d v�rd|d d< | t| |ƒ¡ q
|S )Nr   r   ÚECBrO   r   r   r   é   r   é   úUnsupported tuple size %dú
p=%s, k=%súp=%s, k=%s, %rú
%s #%d: %srC   r   ÚCTRr2   z (shortcut disabled)r3   rj   )rP   r>   ÚupdateÚAssertionErrorr   r   r?   rX   rd   rp   rt   r€   rˆ   rQ   r   rM   )r"   r   Ú	test_dataÚtestsÚextra_tests_addedrU   Úrowr)   r'   Úp2Úp_keyÚp_plaintextÚp_ciphertextÚp_descriptionÚp_moder   ÚnameÚparams2Úctr_params2r   r   r	   Úmake_block_tests$  sh   


ú€r¡   c                 C   s^  g }t t|ƒƒD ]¤}|| }i }t|ƒdkr"|\|d< |d< |d< n9t|ƒdkr7|\|d< |d< |d< |d< n$t|ƒdkrR|\|d< |d< |d< |d< }| |¡ n	tdt|ƒf ƒ‚| ¡ }t|dƒ}	t|dƒ}
t|dƒ}t|dd ƒ}|d ur{|}n|s„d	|
|	f }nd
|
|	|f }d||d |f }||d< ||d< | t| |ƒ¡ | t| |ƒ¡ q|S )NrO   r   r   r   r‹   r   rŒ   r�   rŽ   r�   r�   rC   r   )	rP   r>   r’   r“   r   r   rQ   r   rM   )r"   r   r”   r•   rU   r—   r)   r'   r˜   r™   rš   r›   rœ   r   rž   r   r   r	   Úmake_stream_testsh  s8   


r¢   )Ú__doc__Ú__revision__Úsysr   Úbinasciir   r   ÚCrypto.Util.py3compatÚ
hexversionr
   r   r   r    r   rM   rX   rd   rp   rt   r€   rˆ   r¡   r¢   r   r   r   r	   Ú<module>   s*   

XD