o
    5hC                     @   s   d Z ddlZddlmZmZmZ ddlmZ ddlm	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e fddZdd ZdS )z&Self-testing for PyCrypto hash modules    N)a2b_hexb2a_hexhexlify)b)strxor_cc                   @   s   e Zd ZdS )
_NoDefaultN)__name__
__module____qualname__ r   r   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/Crypto/SelfTest/Cipher/common.pyr   !   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                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )CipherSelfTestc                 C   4  t j|  || _| }t|d| _tt|d| _tt|d| _	tt|d| _
t|dd | _t|dd | _t|dd | _| jrIt| j| _t|dd }t|| _|d urt| jd	| | _t|d
d | _| jd u rtt|dd | _| jd urt| j| _nd | _t|d
d | _| jd urt| j| _|| _d S Ndescriptionkey	plaintext
ciphertextmodule_name
assoc_datamacmodeMODE_ivnonceunittestTestCase__init__modulecopyr   r   r   r   r   r   r   r   r   str	mode_namegetattrr   r   extra_paramsselfr%   paramsr   r   r   r   r$   0   s6   




zCipherSelfTest.__init__c                 C   s   | j S N)r   r,   r   r   r   shortDescriptionV   s   zCipherSelfTest.shortDescriptionc                 C   ^   | j  }t| j}g }| jd ur| jg}| jd ur"|t| jg7 }| jj|g|R i |S r.   r*   r&   r   r   r   r   r%   newr,   r-   r   	old_styler   r   r   _newY      



zCipherSelfTest._newc                 C   s*   t | jd| s
dS | jt| jd| kS )Nr   F)hasattrr%   r   r)   )r,   namer   r   r   isModee   s   zCipherSelfTest.isModec                 C   s  t | j}t | j}g }| jrdd | jD }d }d }tdD ]:}|  }|  }|D ]}	||	 ||	 q+t||}
t|	|}|rT| 
||
 | 
|| |
|}}q| 
| j| | 
| j| | jrt| }| 
| j| |t | j d S d S )Nc                 S   s   g | ]}t t|qS r   )r   r   .0xr   r   r   
<listcomp>o   s    z*CipherSelfTest.runTest.<locals>.<listcomp>   )r   r   r   r   ranger6   updater   encryptdecryptassertEqualr   digestverify)r,   r   r   r   ctpticipherdeciphercompctXptXr   r   r   r   runTestj   s4   


zCipherSelfTest.runTestN)r   r	   r
   r$   r0   r6   r:   rO   r   r   r   r   r   .   s    &r   c                   @      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   r0      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       )r   r   r   r6   r@   lenappendrB   r   r   joinrD   )r,   r   r   ct3rJ   rI   pt3r   r   r   rO      s   

zCipherStreamingSelfTest.runTestN)r   r	   r
   r0   rO   r   r   r   r   rQ      s    rQ   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   )Cryptor[   r"   r#   r$   r%   get_random_bytes
block_sizer   r   r   r   getr   )r,   r%   r-   r[   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   r0         zRoundtripTest.shortDescriptionc                 C   sX   | j j}| j t| j|}|| j}| j t| j|}||}| | j| d S r.   )	r%   MODE_ECBr3   r   r   rB   r   rC   rD   )r,   r   encryption_cipherr   decryption_cipherdecrypted_plaintextr   r   r   rO      s   
zRoundtripTest.runTestN)r   r	   r
   r$   r0   rO   r   r   r   r   rZ      s    	rZ   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
IVLengthTestc                 C   $   t j|  || _t|d | _d S Nr   r"   r#   r$   r%   r   r   r,   r%   r-   r   r   r   r$         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   r0      s   zIVLengthTest.shortDescriptionc                 C   s(   |  t| jjt| j| jjtd d S )NrT   )assertRaises	TypeErrorr%   r3   r   r   rb   r   r/   r   r   r   rO      s   zIVLengthTest.runTestc                 C   s   d| j j S )N )r%   r_   r/   r   r   r   _dummy_counter   ra   zIVLengthTest._dummy_counterN)r   r	   r
   r$   r0   rO   ro   r   r   r   r   rf      s
    rf   c                   @   rP   )NoDefaultECBTestc                 C   rg   rh   ri   rj   r   r   r   r$      rk   zNoDefaultECBTest.__init__c                 C   s   |  t| jjt| j d S r.   )rl   rm   r%   r3   r   r   r/   r   r   r   rO      s   zNoDefaultECBTest.runTestNr   r	   r
   r$   rO   r   r   r   r   rp          rp   c                   @   rP   )BlockSizeTestc                 C   s(   t j|  || _tt|d | _d S rh   )r"   r#   r$   r%   r   r   r   rj   r   r   r   r$      s   zBlockSizeTest.__init__c                 C   s*   | j | j| j j}| |j| j j d S r.   )r%   r3   r   rb   rD   r_   )r,   rJ   r   r   r   rO      s   zBlockSizeTest.runTestNrq   r   r   r   r   rs      rr   rs   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	ByteArrayTestz;Verify we can use bytearray's for encrypting and decryptingc                 C   r   r   r!   r+   r   r   r   r$      6   




zByteArrayTest.__init__c                 C   r1   r.   r2   r4   r   r   r   r6     r7   zByteArrayTest._newc           
      C      t | j}t | j}g }| jrdd | jD }|  }|  }|D ]}|| || q!t|t|}t|	t|}| 
| j| | 
| j| | jrjt| }	| 
| j|	 |tt | j d S d S )Nc                 S      g | ]
}t tt|qS r   )	bytearrayr   r   r;   r   r   r   r>   )      z)ByteArrayTest.runTest.<locals>.<listcomp>)r   r   r   r   r6   rA   r   rB   ry   rC   rD   r   rE   rF   
r,   r   r   r   rJ   rK   rL   rG   rH   r   r   r   r   rO   #  &   


zByteArrayTest.runTestNr   r	   r
   __doc__r$   r6   rO   r   r   r   r   ru      
    %ru   c                   @   rt   )	MemoryviewTestz;Verify we can use memoryviews for encrypting and decryptingc                 C   r   r   r!   r+   r   r   r   r$   B  rv   zMemoryviewTest.__init__c                 C   r1   r.   r2   r4   r   r   r   r6   g  r7   zMemoryviewTest._newc           
      C   rw   )Nc                 S   rx   r   )
memoryviewr   r   r;   r   r   r   r>   y  rz   z*MemoryviewTest.runTest.<locals>.<listcomp>)r   r   r   r   r6   rA   r   rB   r   rC   rD   r   rE   rF   r{   r   r   r   rO   s  r|   zMemoryviewTest.runTestNr}   r   r   r   r   r   ?  r   r   c              	   C   s  g }d}t t|D ]}|| }i }t|dkr$|\|d< |d< |d< n9t|dkr9|\|d< |d< |d< |d< n$t|dkrT|\|d< |d< |d< |d< }	||	 n	td	t|f d
|vred|d
< | }
t|
d}t|
d}t|
d}t|
d
}t|
dd }|d ur|}n|dkr|
sd||f }nd|||
f }d||d |f }||d< ||d< || |s|t| |t| |t| |t	| |t
| |g7 }d}|t| | q
|S )NFrS   r   r   r      r      Unsupported tuple size %dr   ECB
p=%s, k=%sp=%s, k=%s, %r
%s #%d: %s   r   T)r@   rU   rA   AssertionErrorr&   r   rZ   rf   rp   ru   rs   rV   r   )r%   r   	test_dataadditional_paramstestsextra_tests_addedrI   rowr-   r*   p2p_keyp_plaintextp_ciphertextp_modep_descriptionr   r9   r   r   r   make_block_tests  sR   	




r   c                 C   s  g }d}t t|D ]}|| }i }t|dkr$|\|d< |d< |d< n9t|dkr9|\|d< |d< |d< |d< n$t|dkrT|\|d< |d< |d< |d< }|| n	td	t|f | }	t|	d}
t|	d}t|	d}t|	dd }|d ur}|}n|	sd
||
f }nd||
|	f }d||d |f }||d< ||d< |s|t| |g7 }|t| | d}|t	| | |t
| | q
|S )NFrS   r   r   r   r   r   r   r   r   r   r   r   r   T)r@   rU   rA   r   r&   r   ru   rV   r   r   rQ   )r%   r   r   r   r   rI   r   r-   r*   r   r   r   r   r   r   r9   r   r   r   make_stream_tests  sF   


r   )r~   r"   binasciir   r   r   Crypto.Util.py3compatr   Crypto.Util.strxorr   r   r   r#   r   rQ   rZ   rf   rp   rs   ru   r   dictr   r   r   r   r   r   <module>   s"   c 
PP>