o
    ^¨Êh³@  ã                   @   sÂ   d Z dZddlZddlZejd dkrejd dkrddlT ddlT ddlZddlm	Z	m
Z
mZ G dd	„ d	ejƒZG d
d„ deƒZG dd„ deƒZi fdd„Zedkr_dd„ Zejdd� dS dS )z(Self-test suite for Crypto.PublicKey.RSAz$Id$é    Né   é   )Ú*)Úlist_test_casesÚa2b_hexÚb2a_hexc                   @   s°   e Zd ZdZdZdZdZ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(d)„ Zd*S )+ÚRSATestaÅ  
           eb 7a 19 ac e9 e3 00 63 50 e3 29 50 4b 45 e2
        ca 82 31 0b 26 dc d8 7d 5c 68 f1 ee a8 f5 52 67
        c3 1b 2e 8b b4 25 1f 84 d7 e0 b2 c0 46 26 f5 af
        f9 3e dc fb 25 c9 c2 b3 ff 8a e1 0e 83 9a 2d db
        4c dc fe 4f f4 77 28 b4 a1 b7 c1 36 2b aa d2 9a
        b4 8d 28 69 d5 02 41 21 43 58 11 59 1b e3 92 f9
        82 fb 3e 87 d0 95 ae b4 04 48 db 97 2f 3a c1 4f
        7b c2 75 19 52 81 ce 32 d2 f1 b7 6d 4d 35 3e 2d
    aÅ  
        12 53 e0 4d c0 a5 39 7b b4 4a 7a b8 7e 9b f2 a0
        39 a3 3d 1e 99 6f c8 2a 94 cc d3 00 74 c9 5d f7
        63 72 20 17 06 9e 52 68 da 5d 1c 0b 4f 87 2c f6
        53 c1 1d f8 23 14 a6 79 68 df ea e2 8d ef 04 bb
        6d 84 b1 c3 1d 65 4a 19 70 e5 78 3b d6 eb 96 a0
        24 c2 ca 2f 4a 90 fe 9f 2e f5 c9 c1 40 e5 bb 48
        da 95 36 ad 87 00 c8 4f c9 13 0a de a7 4e 55 8d
        51 a7 4d df 85 d8 b5 0d e9 68 38 d6 06 3e 09 55
    aÅ  
        bb f8 2f 09 06 82 ce 9c 23 38 ac 2b 9d a8 71 f7
        36 8d 07 ee d4 10 43 a4 40 d6 b6 f0 74 54 f5 1f
        b8 df ba af 03 5c 02 ab 61 ea 48 ce eb 6f cd 48
        76 ed 52 0d 60 e1 ec 46 19 71 9d 8a 5b 8b 80 7f
        af b8 e0 a3 df c7 37 72 3e e6 b4 b7 d9 3a 25 84
        ee 6a 64 9d 06 09 53 74 88 34 b2 45 45 98 39 4e
        e0 aa b1 2d 7b 61 a5 1f 52 7a 9a 41 f6 c1 68 7f
        e2 53 72 98 ca 2a 8f 59 46 f8 e5 fd 09 1d bd cb
    é   zå
        c9 7f b1 f0 27 f4 53 f6 34 12 33 ea aa d1 d9 35
        3f 6c 42 d0 88 66 b1 d0 5a 0f 20 35 02 8b 9d 86
        98 40 b4 16 66 b4 2e 92 ea 0d a3 b4 32 04 b5 cf
        ce 33 52 52 4d 04 16 a5 a4 41 e7 00 af 46 15 03
    c                 C   s”   ddl ma ddlma ddlmam} tt| jƒƒ| _	tt| j
ƒƒ| _t| j	| jƒd | _|| j| jd | jd  ƒ| _|| j| jƒ| _t| _d S )Nr   )ÚRSA)ÚRandom)Úbytes_to_longÚinverser   )ÚCrypto.PublicKeyr
   ÚCryptor   ÚCrypto.Util.numberr   r   r   ÚmodulusÚnÚprime_factorÚpÚdivmodÚqÚeÚdÚuÚrsa)Úselfr   © r   úT/var/www/html/env/lib/python3.10/site-packages/Crypto/SelfTest/PublicKey/test_RSA.pyÚsetUpZ   s   
zRSATest.setUpc                 C   s@   | j  d¡}|  |¡ |  |¡ | ¡ }|  |¡ |  |¡ dS )z7RSA (default implementation) generated key (1 argument)é   N)r   ÚgenerateÚ_check_private_keyÚ_exercise_primitiveÚ	publickeyÚ_check_public_keyÚ_exercise_public_primitive©r   ÚrsaObjÚpubr   r   r   Útest_generate_1argi   s   


zRSATest.test_generate_1argc                 C   sH   | j  dt ¡ j¡}|  |¡ |  |¡ | ¡ }|  |¡ |  	|¡ dS )z8RSA (default implementation) generated key (2 arguments)r   N)
r   r    r   ÚnewÚreadr!   r"   r#   r$   r%   r&   r   r   r   Útest_generate_2argr   s   


zRSATest.test_generate_2argc                 C   sZ   | j jdt ¡ jdd�}|  |¡ |  |¡ | ¡ }|  |¡ |  	|¡ |  
d|j¡ d S )Nr   i  )r   )r   r    r   r*   r+   r!   r"   r#   r$   r%   ÚassertEqualr   r&   r   r   r   Útest_generate_3args{   s   



zRSATest.test_generate_3argsc                 C   s6   | j  | j| jf¡}|  |¡ |  |¡ |  |¡ dS )z6RSA (default implementation) constructed key (2-tuple)N)r   Ú	constructr   r   r$   Ú_check_encryptionÚ_check_verification)r   r(   r   r   r   Útest_construct_2tuple„   s   

zRSATest.test_construct_2tuplec                 C   sD   | j  | j| j| jf¡}|  |¡ |  |¡ |  |¡ |  |¡ dS )z6RSA (default implementation) constructed key (3-tuple)N)	r   r/   r   r   r   r0   Ú_check_decryptionÚ_check_signingr1   ©r   r'   r   r   r   Útest_construct_3tuple‹   s
   


zRSATest.test_construct_3tuplec                 C   sH   | j  | j| j| j| jf¡}|  |¡ |  |¡ |  |¡ |  	|¡ dS )z6RSA (default implementation) constructed key (4-tuple)N)
r   r/   r   r   r   r   r0   r3   r4   r1   r5   r   r   r   Útest_construct_4tuple“   s
   


zRSATest.test_construct_4tuplec                 C   sV   | j  | j| j| j| j| jf¡}|  |¡ |  |¡ |  	|¡ |  
|¡ |  |¡ dS )z6RSA (default implementation) constructed key (5-tuple)N)r   r/   r   r   r   r   r   r!   r0   r3   r4   r1   r5   r   r   r   Útest_construct_5tuple›   s    



zRSATest.test_construct_5tuplec                 C   sZ   | j  | j| j| j| j| j| jf¡}|  |¡ |  	|¡ |  
|¡ |  |¡ |  |¡ dS )z6RSA (default implementation) constructed key (6-tuple)N)r   r/   r   r   r   r   r   r   r!   r0   r3   r4   r1   r5   r   r   r   Útest_construct_6tuple¤   s   $



zRSATest.test_construct_6tuplec                 C   s’   | j  | j| j| jg¡}|  |j| jkp|j| jk¡ |  |j| jkp(|j| jk¡ |  |j|j | jk¡ |  t	| j j| j| j| jd g¡ d S ©Nr   )
r   r/   r   r   r   Ú
assertTruer   r   ÚassertRaisesÚ
ValueErrorr5   r   r   r   Útest_factoring­   s
   &zRSATest.test_factoringc                 C   sP  |   d| ¡ ¡ |   d| ¡ ¡ |   d| ¡ ¡ |   d| ¡ ¡ |   |j|jj¡ |   |j|jj¡ |   |j|jj¡ |   |j	|jj	¡ |   |j
|jj
¡ |   |j|jj¡ |   |j|j	|j
 ¡ |   d|j|j |j	d |j
d   ¡ |   d|j	|j |j
 ¡ |   d|j	dk¡ |   d|j
dk¡ |   d|jdk¡ |   d|jdk¡ d S r:   )r-   Úhas_privateÚcan_signÚcan_encryptÚ	can_blindr   Úkeyr   r   r   r   r   r5   r   r   r   r!   µ   s"   (zRSATest._check_private_keyc                 C   sr  t | jƒ}|  d| ¡ ¡ |  d| ¡ ¡ |  d| ¡ ¡ |  d| ¡ ¡ |  |j|jj¡ |  |j	|jj	¡ |  dt
|dƒ¡ |  dt
|dƒ¡ |  dt
|dƒ¡ |  dt
|dƒ¡ |  dt
|jdƒ¡ |  dt
|jdƒ¡ |  dt
|jdƒ¡ |  dt
|jdƒ¡ |  d|j	dk¡ |  t|j|tdƒ¡ |  t|j|¡ |  | ¡ | ¡ kd¡ |  | ¡ | ¡ kd	¡ d S )
Nr   r   r   r   r   r   Ú TF)r   Ú
ciphertextr-   r?   r@   rA   rB   r   rC   r   Úhasattrr<   Ú	TypeErrorÚsignÚbÚdecryptr#   )r   r'   rE   r   r   r   r$   Í   s(   
zRSATest._check_public_keyc           
      C   sÊ   t | jƒ}| |f¡}| |tdƒ¡\}|  t|ƒt|ƒ¡ t ¡  	t
|ƒd ¡}| ||¡}| |f¡}| ||¡}|  t|ƒt|ƒ¡ | |tdƒ¡}	|  t|ƒf|	¡ |  d| |t|ƒf¡¡ d S )NrD   r   )r   rE   rJ   ÚencryptrI   r-   r   r   r*   r+   ÚlenÚblindÚunblindrH   r   Úverify)
r   r'   rE   Ú	plaintextÚnew_ciphertext2Úblinding_factorÚblinded_ctextÚblinded_ptextÚunblinded_plaintextÚ
signature2r   r   r   r"   ï   s   
zRSATest._exercise_primitivec                 C   s2   t | jƒ}| |tdƒ¡\}| |t|ƒf¡ d S ©NrD   )r   rP   rK   rI   rO   r   )r   r'   rP   rQ   r   r   r   r%   
  s   
z"RSATest._exercise_public_primitivec                 C   s>   t | jƒ}t | jƒ}| |tdƒ¡\}|  t|ƒt|ƒ¡ d S rW   )r   rP   rE   rK   rI   r-   r   )r   r'   rP   rE   rQ   r   r   r   r0     s   

zRSATest._check_encryptionc           	      C   s†   t | jƒ}t | jƒ}| |f¡}|  t|ƒt|ƒ¡ t ¡  t	|ƒd ¡}| 
||¡}| |f¡}| ||¡}|  t|ƒt|ƒ¡ d S r:   )r   rP   rE   rJ   r-   r   r   r*   r+   rL   rM   rN   )	r   r'   rP   rE   Únew_plaintextrR   rS   rT   rU   r   r   r   r3     s   

zRSATest._check_decryptionc                 C   sR   t t| jƒƒ}t| jƒ}|f}|  d| ||¡¡ |df}|  d| ||¡¡ d S )Nr   rD   )r   r   rP   rE   r-   rO   )r   r'   Ú	signatureÚmessageÚtÚt2r   r   r   r1   *  s   
zRSATest._check_verificationc                 C   s6   t t| jƒƒ}t| jƒ}|  |f| |tdƒ¡¡ d S rW   )r   r   rP   rE   r-   rH   rI   )r   r'   rY   rZ   r   r   r   r4   8  s   
zRSATest._check_signingN)Ú__name__Ú
__module__Ú__qualname__rP   rE   r   r   r   r   r)   r,   r.   r2   r6   r7   r8   r9   r>   r!   r$   r"   r%   r0   r3   r1   r4   r   r   r   r   r   &   s0    
					"	r   c                   @   óT   e Z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 )ÚRSAFastMathTestc                 C   ó   t  | ¡ tjdd�| _d S )NT©Úuse_fast_math©r   r   r
   ÚRSAImplementationr   ©r   r   r   r   r   @  ó   
zRSAFastMathTest.setUpc                 C   ó   t  | ¡ dS )z9RSA (_fastmath implementation) generated key (1 argument)N©r   r)   rg   r   r   r   r)   D  ó   z"RSAFastMathTest.test_generate_1argc                 C   ri   )z:RSA (_fastmath implementation) generated key (2 arguments)N©r   r,   rg   r   r   r   r,   H  rk   z"RSAFastMathTest.test_generate_2argc                 C   ri   )z8RSA (_fastmath implementation) constructed key (2-tuple)N©r   r2   rg   r   r   r   r2   L  rk   z%RSAFastMathTest.test_construct_2tuplec                 C   ri   )z8RSA (_fastmath implementation) constructed key (3-tuple)N©r   r6   rg   r   r   r   r6   P  rk   z%RSAFastMathTest.test_construct_3tuplec                 C   ri   )z8RSA (_fastmath implementation) constructed key (4-tuple)N©r   r7   rg   r   r   r   r7   T  rk   z%RSAFastMathTest.test_construct_4tuplec                 C   ri   )z8RSA (_fastmath implementation) constructed key (5-tuple)N©r   r8   rg   r   r   r   r8   X  rk   z%RSAFastMathTest.test_construct_5tuplec                 C   ri   )z8RSA (_fastmath implementation) constructed key (6-tuple)N©r   r9   rg   r   r   r   r9   \  rk   z%RSAFastMathTest.test_construct_6tuplec                 C   ó   t  | ¡ d S ©N©r   r>   rg   r   r   r   r>   `  ó   zRSAFastMathTest.test_factoringN©r]   r^   r_   r   r)   r,   r2   r6   r7   r8   r9   r>   r   r   r   r   ra   ?  ó    ra   c                   @   r`   )ÚRSASlowMathTestc                 C   rb   )NFrc   re   rg   r   r   r   r   d  rh   zRSASlowMathTest.setUpc                 C   ri   )z9RSA (_slowmath implementation) generated key (1 argument)Nrj   rg   r   r   r   r)   h  rk   z"RSASlowMathTest.test_generate_1argc                 C   ri   )z:RSA (_slowmath implementation) generated key (2 arguments)Nrl   rg   r   r   r   r,   l  rk   z"RSASlowMathTest.test_generate_2argc                 C   ri   )z8RSA (_slowmath implementation) constructed key (2-tuple)Nrm   rg   r   r   r   r2   p  rk   z%RSASlowMathTest.test_construct_2tuplec                 C   ri   )z8RSA (_slowmath implementation) constructed key (3-tuple)Nrn   rg   r   r   r   r6   t  rk   z%RSASlowMathTest.test_construct_3tuplec                 C   ri   )z8RSA (_slowmath implementation) constructed key (4-tuple)Nro   rg   r   r   r   r7   x  rk   z%RSASlowMathTest.test_construct_4tuplec                 C   ri   )z8RSA (_slowmath implementation) constructed key (5-tuple)Nrp   rg   r   r   r   r8   |  rk   z%RSASlowMathTest.test_construct_5tuplec                 C   ri   )z8RSA (_slowmath implementation) constructed key (6-tuple)Nrq   rg   r   r   r   r9   €  rk   z%RSASlowMathTest.test_construct_6tuplec                 C   rr   rs   rt   rg   r   r   r   r>   „  ru   zRSASlowMathTest.test_factoringNrv   r   r   r   r   rx   c  rw   rx   c                 C   sº   g }|t tƒ7 }zddlm} |t tƒ7 }W n8 tyN   ddlm} dd l}t	j
 t	j
 t	j
 | | ¡ ¡¡¡d |dƒ ¡}t	j
 |¡rLtd| ƒ‚Y nw |  dd¡r[|t tƒ7 }|S )	Nr   )Ú	_fastmath)Úget_config_varz/../../PublicKey/_fastmathÚSOz—While the _fastmath module exists, importing it failed. This may point to the gmp or mpir shared library not being in the path. _fastmath was found at Ú
slow_testsr   )r   r   r   ry   ra   ÚImportErrorÚdistutils.sysconfigrz   ÚinspectÚosÚpathÚnormpathÚdirnameÚabspathÚgetfileÚcurrentframeÚexistsÚgetrx   )ÚconfigÚtestsry   rz   r   Ú_fm_pathr   r   r   Ú	get_tests‡  s0   ÿþþþÿú
rŒ   Ú__main__c                   C   s   t  tƒ ¡S rs   )ÚunittestÚ	TestSuiterŒ   r   r   r   r   Ú<lambda>œ  s    r�   Úsuite)ÚdefaultTest)Ú__doc__Ú__revision__Úsysr€   Úversion_infoÚCrypto.Util.py21compatÚCrypto.Util.py3compatrŽ   ÚCrypto.SelfTest.st_commonr   r   r   ÚTestCaser   ra   rx   rŒ   r]   r‘   Úmainr   r   r   r   Ú<module>   s&     $$þ