3
¬ �eãM  ã               @   sþ   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d	d
„ ZG dd„ dejƒZG dd„ dejƒZdd„ ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZi fdd„Zedkrúdd„ Zejdd� dS )é    N)Ú	unhexlify)Úlist_test_cases)Úload_test_vectors_wycheproof)ÚtobytesÚbchr)ÚAES)ÚSHAKE128)Ústrxorc             C   s   t jt| ƒd�j|ƒS )N)Údata)r   Únewr   Úread)ÚtagÚlength© r   úf/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/SelfTest/Cipher/test_SIV.pyÚget_tag_random-   s    r   c               @   sÆ   e Zd ZeddƒZeddƒZeddƒZeddƒZed	d
ƒZdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-S ).ÚSivTestsÚkey_256é    Úkey_384é0   Úkey_512é@   Z	nonce_128é   r
   é€   c             C   sv   xp| j | j| jfD ]\}tj|tj| jd�}tddƒ}|j|ƒ\}}tj|tj| jd�}|j	||ƒ}| j
||ƒ qW d S )N)ÚnonceZ	plaintexté   éd   i@  )r   r   r   r   r   ÚMODE_SIVÚnonce_96r   Úencrypt_and_digestÚdecrypt_and_verifyÚassertEqual)ÚselfÚkeyÚcipherÚptÚctÚmacÚpt2r   r   r   Útest_loopback_1289   s    
zSivTests.test_loopback_128c             C   sr   t j| jt jƒ t j| jt j| jƒ}|j| jƒ\}}t j| jt j| jd�}|j| jƒ\}}| j|| || ƒ d S )N)r   )r   r   r   r   r   r    r
   r"   )r#   r%   Zct1Ztag1Úct2Ztag2r   r   r   Ú
test_nonceC   s    zSivTests.test_noncec             C   s   | j ttj| jtjdd� d S )NZtest12345678)r   )ÚassertRaisesÚ	TypeErrorr   r   r   r   )r#   r   r   r   Útest_nonce_must_be_bytesN   s    z!SivTests.test_nonce_must_be_bytesc             C   sX   | j ttj| jtjdd� x8tddƒD ]*}tj| jtjtdƒ| d�}|jdƒ q&W d S )Nó    )r   é   r   ó   )	r-   Ú
ValueErrorr   r   r   r   Úranger   r    )r#   Úxr%   r   r   r   Útest_nonce_lengthR   s
    zSivTests.test_nonce_lengthc             C   s*   t j| jt j| jd�}| j|jt jƒ d S )N)r   )r   r   r   r   r   r"   Ú
block_size)r#   r%   r   r   r   Útest_block_size_128[   s    zSivTests.test_block_size_128c             C   sF   t j| jt j| jd�}| j|j| jƒ | jtt j| jt jƒdƒƒ d S )N)r   r   )	r   r   r   r   r   r"   r   ZassertFalseÚhasattr)r#   r%   r   r   r   Útest_nonce_attribute_   s    zSivTests.test_nonce_attributec             C   sV   | j ttj| jtj| jdƒ | j ttj| jtj| jdd� tj| jtj| jdd� d S )Né   )r   ÚunknownF)r   Z	use_aesni)r-   r.   r   r   r   r   r   )r#   r   r   r   Útest_unknown_parametersf   s    
z SivTests.test_unknown_parametersc             C   sp   t j| jt j| jd�}|j| jƒ | jt|j	| jƒ t j| jt j| jd�}|j| jƒ | jt|j
| j| jƒ d S )N)r   )r   r   r   r   r   r    r
   r-   r.   Údecryptr!   )r#   r%   r   r   r   Útest_encrypt_excludes_decryptq   s    
z&SivTests.test_encrypt_excludes_decryptc             C   sR   t j| jt j| jd�}| jt|jdƒ t j| jt j| jd�}| jt|jddƒ d S )N)r   ztest1234567890-*s   xxxx)	r   r   r   r   r   r-   r.   Úencryptr!   )r#   r%   r   r   r   Útest_data_must_be_bytes{   s
    
z SivTests.test_data_must_be_bytesc             C   s:   t j| jt j| jd�}|j| jƒ\}}| jt|ƒdƒ d S )N)r   r   )	r   r   r   r   r   r    r
   r"   Úlen)r#   r%   Ú_r(   r   r   r   Útest_mac_lenƒ   s    zSivTests.test_mac_lenc             C   sh   ddl m} tj| jtj| jd�}|j| jƒ\}}||dƒ}tj| jtj| jd�}| j	t
|j||ƒ d S )Nr   )Ústrxor_c)r   r1   )ÚCrypto.Util.strxorrE   r   r   r   r   r   r    r
   r-   r3   r!   )r#   rE   r%   r'   r(   Zinvalid_macr   r   r   Útest_invalid_macˆ   s    
zSivTests.test_invalid_macc             C   sV   t j| jt j| jd�}|jƒ }| j|jƒ t|ƒƒ t j| jt j| jd�}|j	|ƒ d S )N)r   )
r   r   r   r   r   Ú	hexdigestr"   Údigestr   Z	hexverify)r#   r%   Zmac_hexr   r   r   Útest_hex_mac“   s
    zSivTests.test_hex_macc             C   s\  t | jƒ}t | jƒ}t | jƒ}t | jƒ}tj| jtj| jd�}|j| jƒ |j| jƒ\}}tj|tj|d�}d|d d…< d|d d…< |j|ƒ d|d d…< |j|ƒ\}	}
| j	||	ƒ | j	||
ƒ | j	|j
|j
ƒ t | jƒ}t | jƒ}t | jƒ}t |ƒ}t |ƒ}tj|tj|d�}d|d d…< d|d d…< |j|ƒ d|d d…< |j||ƒ}| j	| j|ƒ d S )N)r   s   ÿÿÿé   )Ú	bytearrayr   r   r
   r   r   r   Úupdater    r"   r   r!   )r#   r$   r   r
   ÚheaderÚcipher1r'   r   Úcipher2Úct_testÚtag_testÚct_baÚtag_baÚcipher3Úpt_testr   r   r   Útest_bytearray›   sD    









zSivTests.test_bytearrayc             C   s€  t t| jƒƒ}t t| jƒƒ}t t| jƒƒ}t t| jƒƒ}tj| jtj| jd�}|j| jƒ |j	| jƒ\}}tj|tj|d�}d|d d…< d|d d…< |j|ƒ d|d d…< |j	|ƒ\}	}
| j
||	ƒ | j
||
ƒ | j
|j|jƒ t t| jƒƒ}t t| jƒƒ}t t| jƒƒ}t t|ƒƒ}t t|ƒƒ}tj|tj|d�}d|d d…< d|d d…< |j|ƒ d|d d…< |j||ƒ}| j
| j|ƒ d S )N)r   s   ÿÿÿrK   )Ú
memoryviewrL   r   r   r
   r   r   r   rM   r    r"   r   r!   )r#   r$   r   r
   rN   rO   r'   r   rP   rQ   rR   rS   rT   rU   rV   r   r   r   Útest_memoryviewÈ   sD    


zSivTests.test_memoryviewc             C   sÂ   dd }t j| jt j| jd�}|j|ƒ\}}tdƒ}t j| jt j| jd�}|j||d�\}}| j||ƒ | j|d ƒ | j||ƒ t j| jt j| jd�}|j|||d�}| j||ƒ | j|d ƒ d S )Nó   5r   )r   )Úoutput)	r   r   r   r   r   r    rL   r"   r!   )r#   r&   r%   r'   r   r[   ÚresZtag_outr   r   r   Útest_output_paramõ   s    zSivTests.test_output_paramc             C   sž   dd }t j| jt j| jd�}|j|ƒ\}}ttdƒƒ}t j| jt j| jd�}|j||d� | j||ƒ t j| jt j| jd�}|j	|||d� | j||ƒ d S )NrZ   r   )r   )r[   )
r   r   r   r   r   r    rX   rL   r"   r!   )r#   r&   r%   r'   r   r[   r   r   r   Útest_output_param_memoryview  s    z%SivTests.test_output_param_memoryviewc             C   sô   d}d| }t j| jt j| jd�}|j|ƒ\}}t j| jt j| jd�}| jt|j|d| d� t j| jt j| jd�}| jt|j||d| d� t	|d ƒ}t j| jt j| jd�}| jt
|j||d� t j| jt j| jd�}| jt
|j|||d� d S )Nr   rZ   )r   ó   0)r[   r1   )r   r   r   r   r   r    r-   r.   r!   rL   r3   )r#   ZLEN_PTr&   r%   r'   r   Zshorter_outputr   r   r   Útest_output_param_neg  s    zSivTests.test_output_param_negN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r
   r*   r,   r/   r6   r8   r:   r=   r?   rA   rD   rG   rJ   rW   rY   r]   r^   r`   r   r   r   r   r   1   s,   





	
--r   c               @   sr   e Zd ZeddƒZeddƒZeddƒZdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSivFSMTestsr   r   r   r   r
   r   c             C   s*   t j| jt j| jd�}| jt|jdƒ d S )N)r   s   xxx)r   r   r   r   r   r-   r.   r@   )r#   r%   r   r   r   Útest_invalid_init_encrypt0  s    
z%SivFSMTests.test_invalid_init_encryptc             C   s*   t j| jt j| jd�}| jt|jdƒ d S )N)r   s   xxx)r   r   r   r   r   r-   r.   r>   )r#   r%   r   r   r   Útest_invalid_init_decrypt6  s    
z%SivFSMTests.test_invalid_init_decryptc             C   sZ   t j| jt j| jd�}|j| jƒ |jƒ }t j| jt j| jd�}|j| jƒ |j|ƒ d S )N)r   )	r   r   r   r   r   rM   r
   rI   Úverify)r#   r%   r(   r   r   r   Ú$test_valid_init_update_digest_verify<  s    

z0SivFSMTests.test_valid_init_update_digest_verifyc             C   s"   t j| jt j| jd�}|jƒ  d S )N)r   )r   r   r   r   r   rI   )r#   r%   r   r   r   Útest_valid_init_digestJ  s    z"SivFSMTests.test_valid_init_digestc             C   sB   t j| jt j| jd�}|jƒ }t j| jt j| jd�}|j|ƒ d S )N)r   )r   r   r   r   r   rI   rg   )r#   r%   r(   r   r   r   Útest_valid_init_verifyO  s    z"SivFSMTests.test_valid_init_verifyc             C   sŽ   t j| jt j| jd�}|j| jƒ |jƒ }x tdƒD ]}| j	||jƒ ƒ q4W t j| jt j| jd�}|j| jƒ xtdƒD ]}|j
|ƒ qxW d S )N)r   é   é   )r   r   r   r   r   rM   r
   rI   r4   r"   rg   )r#   r%   Z	first_macr5   r   r   r   Ú$test_valid_multiple_digest_or_verifyW  s    z0SivFSMTests.test_valid_multiple_digest_or_verifyc             C   sr   t j| jt j| jd�}|j| jƒ |j| jƒ\}}t j| jt j| jd�}|j| jƒ |j||ƒ}| j	| j|ƒ d S )N)r   )
r   r   r   r   r   rM   r
   r    r!   r"   )r#   r%   r'   r(   r&   r   r   r   Ú0test_valid_encrypt_and_digest_decrypt_and_verifye  s    z<SivFSMTests.test_valid_encrypt_and_digest_decrypt_and_verifyc             C   s:   t j| jt j| jd�}|j| jƒ\}}| jt|jdƒ d S )N)r   r0   )	r   r   r   r   r   r    r
   r-   r.   )r#   r%   r'   r   r   r   r   Ú(test_invalid_multiple_encrypt_and_digestq  s    z4SivFSMTests.test_invalid_multiple_encrypt_and_digestc             C   s^   t j| jt j| jd�}|j| jƒ\}}t j| jt j| jd�}|j||ƒ | jt	|j||ƒ d S )N)r   )
r   r   r   r   r   r    r
   r!   r-   r.   )r#   r%   r'   r   r   r   r   Ú(test_invalid_multiple_decrypt_and_verifyv  s
    z4SivFSMTests.test_invalid_multiple_decrypt_and_verifyN)ra   rb   rc   r   r   r   r
   re   rf   rh   ri   rj   rm   rn   ro   rp   r   r   r   r   rd   *  s   


rd   c             C   s\   dd„ | d j dƒD ƒg}|dd„ | dd… D ƒ7 }| d rJt| d ƒ}nd }||g7 }|S )Nc             S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0r5   r   r   r   ú
<listcomp>€  s    ztransform.<locals>.<listcomp>r   ú-c             S   s   g | ]}t |ƒ‘qS r   )r   )rq   r5   r   r   r   rr   �  s    r1   rl   )Úsplitr   )ÚtvZnew_tvr   r   r   r   Ú	transform  s    
rv   c               @   s.   e Zd ZdZddgZdd„ eD ƒZdd„ ZdS )ÚTestVectorsz6Class exercising the SIV test vectors found in RFC5297Ú0101112131415161718191a1b1c1d1e1f2021222324252627Ú112233445566778899aabbccddeeÚ40c02b9690c4dc04daef7f6afe5cÚ 85632d07c6e8f37f950acd320a2ecc93Ú@fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeffNZ@00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa9988z%7766554433221100-102030405060708090a0Z@7468697320697320736f6d6520706c61696e7465787420746f20656e63727970Z74207573696e67205349562d414553Z@cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829Zea64ad544a272e9c485b62a3fd5c0dÚ 7bdb6e3b432667eb06f4d14bff2fbd0fÚ@7f7e7d7c7b7a79787776757473727170404142434445464748494a4b4c4d4e4fÚ 09f911029d74e35bd84156c5635688c0c             C   s   g | ]}t |ƒ‘qS r   )rv   )rq   ru   r   r   r   rr   ®  s    zTestVectors.<listcomp>c             C   s²   x¬| j D ]¢\}}}}}}tj|tj|d�}x|D ]}|j|ƒ q0W |j|ƒ\}	}
| j||	ƒ | j||
ƒ tj|tj|d�}x|D ]}|j|ƒ q€W |j||ƒ}| j||ƒ qW d S )N)r   )Útest_vectorsr   r   r   rM   r    r"   r!   )r#   Z
assoc_datar&   r'   r(   r$   r   r%   r5   r+   Zmac2r)   r   r   r   ÚrunTest°  s    

zTestVectors.runTest)rx   ry   rz   r{   r|   Núe00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa99887766554433221100-102030405060708090a0Ú^7468697320697320736f6d6520706c61696e7465787420746f20656e6372797074207573696e67205349562d414553Ú^cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829ea64ad544a272e9c485b62a3fd5c0d)r‚   rƒ   r„   r}   r~   r   )ra   rb   rc   Ú__doc__Ztest_vectors_hexr€   r�   r   r   r   r   rw   Š  s$                rw   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTestVectorsWycheproofc             C   s   t jj| ƒ d| _d S )NÚNone)ÚunittestÚTestCaseÚ__init__Ú_id)r#   r   r   r   rŠ   Å  s    zTestVectorsWycheproof.__init__c             C   s   t dddƒ| _d S )NÚCipherÚ
wycheproofzaes_siv_cmac_test.jsonzWycheproof AES SIV)rŒ   r�   )r   ru   )r#   r   r   r   ÚsetUpÉ  s    zTestVectorsWycheproof.setUpc             C   s   | j S )N)r‹   )r#   r   r   r   ÚshortDescriptionÎ  s    z&TestVectorsWycheproof.shortDescriptionc             C   sX   dt |jƒ | _tj|jtjƒ}|j|jƒ |j	|j
ƒ\}}|jrT| j|| |jƒ d S )Nz!Wycheproof Encrypt AES-SIV Test #)ÚstrÚidr‹   r   r   r$   r   rM   Úaadr    ÚmsgÚvalidr"   r'   )r#   ru   r%   r'   r   r   r   r   Útest_encryptÑ  s    z"TestVectorsWycheproof.test_encryptc             C   sŽ   dt |jƒ | _tj|jtjƒ}|j|jƒ y$|j	|j
dd … |j
d d… ƒ}W n  tk
rp   |j slt‚Y nX |js|t‚| j||jƒ d S )Nz!Wycheproof Decrypt AES_SIV Test #r   )r�   r‘   r‹   r   r   r$   r   rM   r’   r!   r'   r3   r”   ÚAssertionErrorr"   r“   )r#   ru   r%   r&   r   r   r   Útest_decryptÚ  s    $
z"TestVectorsWycheproof.test_decryptc             C   s(   x"| j D ]}| j|ƒ | j|ƒ qW d S )N)ru   r•   r—   )r#   ru   r   r   r   r�   ç  s    
zTestVectorsWycheproof.runTestN)	ra   rb   rc   rŠ   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d„ Zd	d
„ Zdd„ ZdS )ÚTestVectorsWycheproof2c             C   s   t jj| ƒ d| _d S )Nr‡   )rˆ   r‰   rŠ   r‹   )r#   r   r   r   rŠ   ð  s    zTestVectorsWycheproof2.__init__c             C   s   t dddƒ| _d S )NrŒ   r�   zaead_aes_siv_cmac_test.jsonzWycheproof AEAD SIV)rŒ   r�   )r   ru   )r#   r   r   r   rŽ   ô  s    zTestVectorsWycheproof2.setUpc             C   s   | j S )N)r‹   )r#   r   r   r   r�   ù  s    z'TestVectorsWycheproof2.shortDescriptionc             C   sh   dt |jƒ | _tj|jtj|jd�}|j|j	ƒ |j
|jƒ\}}|jrd| j||jƒ | j||jƒ d S )Nz&Wycheproof Encrypt AEAD-AES-SIV Test #)r   )r�   r‘   r‹   r   r   r$   r   ÚivrM   r’   r    r“   r”   r"   r'   r   )r#   ru   r%   r'   r   r   r   r   r•   ü  s    z#TestVectorsWycheproof2.test_encryptc             C   s„   dt |jƒ | _tj|jtj|jd�}|j|j	ƒ y|j
|j|jƒ}W n  tk
rf   |j sbt‚Y nX |jsrt‚| j||jƒ d S )Nz&Wycheproof Decrypt AEAD-AES-SIV Test #)r   )r�   r‘   r‹   r   r   r$   r   r™   rM   r’   r!   r'   r   r3   r”   r–   r"   r“   )r#   ru   r%   r&   r   r   r   r—     s    
z#TestVectorsWycheproof2.test_decryptc             C   s(   x"| j D ]}| j|ƒ | j|ƒ qW d S )N)ru   r•   r—   )r#   ru   r   r   r   r�     s    
zTestVectorsWycheproof2.runTestN)	ra   rb   rc   rŠ   rŽ   r�   r•   r—   r�   r   r   r   r   r˜   î  s   
r˜   c             C   sN   | j dƒ}g }|ttƒ7 }|ttƒ7 }|tƒ g7 }|tƒ g7 }|tƒ g7 }|S )NÚwycheproof_warnings)Úgetr   r   rd   rw   r†   r˜   )Úconfigrš   Útestsr   r   r   Ú	get_tests  s    
rž   Ú__main__c               C   s   t jtƒ ƒS )N)rˆ   Z	TestSuiterž   r   r   r   r   Ú<lambda>'  s    r    Úsuite)ZdefaultTest)Zjsonrˆ   Úbinasciir   ZCrypto.SelfTest.st_commonr   ZCrypto.SelfTest.loaderr   ZCrypto.Util.py3compatr   r   ZCrypto.Cipherr   ZCrypto.Hashr   rF   r	   r   r‰   r   rd   rv   rw   r†   r˜   rž   ra   r¡   Úmainr   r   r   r   Ú<module>   s(    zU9+,