o
    6h3                     @   s<  zd dl ZW n ey   d dlZY nw d dlZd dlZddlmZmZmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZ G dd dejZG d	d
 d
ejZejjdedd eD ddd Zejjdedd eD ddd Zejjdedd eD ddd Zejjdedd eD ddd ZdS )    N   )CurveNIST256pcurvesUnknownCurveErrorPRIME_FIELD_OIDcurve_by_name)CurveFpPointJacobi	CurveEdTw)der)number_to_stringc                   @   s   e Z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%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1S )2TestParameterEncodingc                 C   s
   d| _ d S )Na0  MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP///////////////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDydwN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=)base64_params)cls r   e/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/test_curves.py
setUpClass   s   z TestParameterEncoding.setUpClassc                 C   s   d}t |}| |t d S )No  -----BEGIN EC PARAMETERS-----
MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP/////////
//////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12K
o6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDyd
wN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1
AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=
-----END EC PARAMETERS-----
)r   from_pemassertIsr   )self
pem_paramscurver   r   r   test_from_pem"   s   
	z#TestParameterEncoding.test_from_pemc                 C   T   d}|  tj}t|dg W d    n1 sw   Y  | dt|j d S )Nr   named_curvezexplicit curve parameters notassertRaisesr   UnexpectedDERr   r   assertInstr	exceptionr   r   er   r   r   2test_from_pem_with_explicit_when_explicit_disabled0   s   	zHTestParameterEncoding.test_from_pem_with_explicit_when_explicit_disabledc                 C   r   )NzK-----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
explicitz named_curve curve parameters notr   r#   r   r   r   8test_from_pem_with_named_curve_with_named_curve_disabled?   s   zNTestParameterEncoding.test_from_pem_with_named_curve_with_named_curve_disabledc                 C   sP   d}|  tj}t| W d    n1 sw   Y  | dt|j d S )Nai  -----BEGIN PARAMETERS-----
MIHgAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP/////////
//////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12K
o6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEQQRrF9Hy4SxCR/i85uVjpEDyd
wN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1
AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQE=
-----END PARAMETERS-----
zPARAMETERS PEM headerr   r#   r   r   r   test_from_pem_with_wrong_headerJ   s   	z5TestParameterEncoding.test_from_pem_with_wrong_headerc                 C   s   d}t  }| || d S )NsK   -----BEGIN EC PARAMETERS-----
BggqhkjOPQMBBw==
-----END EC PARAMETERS-----
)r   to_pemassertEqual)r   r   encodingr   r   r   test_to_pemY   s   z!TestParameterEncoding.test_to_pemc                 C   s   |  td d S )N   )assertNotEqualr   )r   r   r   r   "test_compare_with_different_objectc   s   z8TestParameterEncoding.test_compare_with_different_objectc                 C   s   t  }| d| d S )Ns
   *H=r   to_derr*   r   encodedr   r   r   test_named_curve_params_derf   s   z1TestParameterEncoding.test_named_curve_params_derc                 C   s"   t  }t d}| || d S Nr   r0   )r   encoded_defaultencoded_namedr   r   r   +test_verify_that_default_is_named_curve_derl   s   
zATestParameterEncoding.test_verify_that_default_is_named_curve_derc                 C   s&   t d}| |tt| j d S Nr&   )r   r1   r*   bytesbase64	b64decoder   r2   r   r   r    test_encoding_to_explicit_paramsr   s   
z6TestParameterEncoding.test_encoding_to_explicit_paramsc                 C   sJ   |  t}td W d    n1 sw   Y  | dt|j d S )NunsupportedzOnly 'named_curve')r   
ValueErrorr   r1   r    r!   r"   r   r$   r   r   r   !test_encoding_to_unsupported_typew   s   z7TestParameterEncoding.test_encoding_to_unsupported_typec                 C   s*   t dd}d}| |tt| d S )Nr&   
compresseda  MIHAAgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP///////////////zBEBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsEIQNrF9Hy4SxCR/i85uVjpEDydwN9gS3rM6D0oTlF2JjClgIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8YyVRAgEB)r   r1   r*   r:   r;   r<   )r   r3   compressed_base_pointr   r   r   +test_encoding_to_explicit_compressed_params}   s   zATestParameterEncoding.test_encoding_to_explicit_compressed_paramsc                 C   s(   d}t tt|}| t| d S )NaP  MIH3AgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP///////////////zBbBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsDFQDEnTYIhucEk2pmeOETnSa3gZ9+kARBBGsX0fLhLEJH+Lzm5WOkQPJ3A32BLeszoPShOUXYmMKWT+NC4v4af5uO5+tKfA+eFivOM1drMV7Oy7ZAaDe/UfUCIQD/////AAAAAP//////////vOb6racXnoTzucrC/GMlUQIBAQ==)r   from_derr:   r;   r<   r*   r   )r   p256_explicitdecodedr   r   r   #test_decoding_explicit_from_openssl   s   	z9TestParameterEncoding.test_decoding_explicit_from_opensslc                 C   s&   t tt| j}| |t d S N)r   rE   r:   r;   r<   r   r   r   )r   r   r   r   r   -test_decoding_well_known_from_explicit_params   s   zCTestParameterEncoding.test_decoding_well_known_from_explicit_paramsc                 C   sN   |  t}tddg W d    n1 sw   Y  | dt|j d S )N    
explicitCAzOnly named_curve)r   r?   r   rE   r    r!   r"   r@   r   r   r   ,test_decoding_with_incorrect_valid_encodings   s   zBTestParameterEncoding.test_decoding_with_incorrect_valid_encodingsc                 C   s`   t ddd}t|dddddd}t|dd	dddd}td
||d }td
||d }| || d S )N   r            	   T	generator   unknown)r	   r
   r   r.   )r   curve_fpbase_abase_bcurve_acurve_br   r   r   -test_compare_curves_with_different_generators   s   zCTestParameterEncoding.test_compare_curves_with_different_generatorsc                 C   sj   t ddd}t|dddddd}td	||d }| }t|}| || d
}| |tt| d S )NrN   r   rO   rP   rQ   rR   TrS   rV   z0MCECAQEwDAYHKoZIzj0BAQIBFzAGBAEBBAEHBAMEDQMCAQk=)	r	   r
   r   r1   rE   r*   r:   r;   r<   )r   rW   
base_pointr   r3   rG   expectedr   r   r   $test_default_encode_for_custom_curve   s   
z:TestParameterEncoding.test_default_encode_for_custom_curvec                 C   sx   t ddd}t|dddddd}td	||d }| t}|d
 W d    n1 s,w   Y  | dt|j d S )NrN   r   rO   rP   rQ   rR   TrS   rV   r   zCan't encode curve)	r	   r
   r   r   r   r1   r    r!   r"   )r   rW   r]   r   r$   r   r   r   (test_named_curve_encode_for_custom_curve   s   z>TestParameterEncoding.test_named_curve_encode_for_custom_curvec                 C   sT   d}|  t}tt| W d    n1 sw   Y  | dt|j d S )NzMIGRAgEBMBwGByqGSM49AQIwEQIBcQYJKoZIzj0BAgMCAgEJMDkEDwAwiCUMpufH/mSc6Fgg9wQPAOi+5NPiJgdEGIvg6ccjAxUAEOcjqxTWluZ2h1YVF1b+v4/LSakEHwQAnXNhbzX0qxQH1zViwQ8ApSgwJ3lY7oTRMV7TGIYCDwEAAAAAAAAA2czsijnlbwIBAg==z#Characteristic 2 curves unsupported)	r   r   r   rE   r;   r<   r    r!   r"   )r   sect113r1_explicitr$   r   r   r   !test_try_decoding_binary_explicit   s   z7TestParameterEncoding.test_try_decoding_binary_explicitc                 C   sb   t jtj t d }| t j}t| W d    n1 s!w   Y  | 	dt
|j d S )Nr   zUnexpected data after OID)r   
encode_oidr   oidencode_integerr   r   r   rE   r    r!   r"   r   bad_derr$   r   r   r   !test_decode_malformed_named_curve   s
   z7TestParameterEncoding.test_decode_malformed_named_curvec                 C   sf   t t| jtd }| tj}t	| W d    n1 s#w   Y  | 
dt|j d S )Nr   z"Unexpected data after ECParameters)r:   r;   r<   r   r   re   r   r   r   rE   r    r!   r"   rf   r   r   r   4test_decode_malformed_explicit_garbage_after_ECParam   s   
zJTestParameterEncoding.test_decode_malformed_explicit_garbage_after_ECParamc                 C   s\   t t d}| t j}t| W d    n1 sw   Y  | dt|j	 d S )N   z!Unknown parameter encoding format)
r   encode_sequencere   r   r   r   rE   r    r!   r"   rf   r   r   r   ,test_decode_malformed_unknown_version_number   s
   zBTestParameterEncoding.test_decode_malformed_unknown_version_numberc                 C   s   t j }ttdttdddt|tttt j	 | |ttt j
 |tt jdtt j }| t}t| W d    n1 sWw   Y  | dt|j d S )Nr   rj   rQ   uncompressedzUnknown field type: (1, 2, 3))r   r   pr   rk   re   rc   encode_octet_stringr   abrT   to_bytesorderr   r   r   rE   r    r!   r"   r   curve_prg   r$   r   r   r   (test_decode_malformed_unknown_field_type   s.   

z>TestParameterEncoding.test_decode_malformed_unknown_field_typec                 C   s   t j }ttdttjt t|tdttt	t j
 | |tt	t j |tt jdtt j }| tj}t| W d    n1 sZw   Y  | dt|j d S )Nr   rm   zPrime-p element)r   r   rn   r   rk   re   rc   r   ro   r   rp   rq   rT   rr   rs   r   r   r   rE   r    r!   r"   rt   r   r   r   )test_decode_malformed_garbage_after_prime  s2   

z?TestParameterEncoding.test_decode_malformed_garbage_after_primeN)__name__
__module____qualname__classmethodr   r   r%   r'   r(   r,   r/   r4   r8   r=   rA   rD   rH   rJ   rM   r\   r_   r`   rb   rh   ri   rl   rv   rw   r   r   r   r   r      s4    



r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestCurveSearchingc                 C      t d}| |t d S )Nr   r   r   r   r   cr   r   r   test_correct_name+     z$TestCurveSearching.test_correct_namec                 C   r}   )N
prime256v1r~   r   r   r   r   test_openssl_name/  r   z$TestCurveSearching.test_openssl_namec                 C   sH   |  t}td W d    n1 sw   Y  | dt|j d S )Nzfoo barzFname 'foo bar' unknown, only curves supported: ['NIST192p', 'NIST224p'r   r   r   r    r!   r"   r@   r   r   r   test_unknown_curve3     
z%TestCurveSearching.test_unknown_curvec                 C   sH   |  t}td  W d    n1 sw   Y  | dt|j d S )NzAname None unknown, only curves supported: ['NIST192p', 'NIST224p'r   r@   r   r   r   test_with_None_as_parameter=  r   z.TestCurveSearching.test_with_None_as_parameterN)rx   ry   rz   r   r   r   r   r   r   r   r   r|   *  s
    
r|   r   c                 C      g | ]}|j qS r   name.0ir   r   r   
<listcomp>H      r   )idsc                 C   s    t | d}| |ksJ d S r5   r   rE   r1   r   retr   r   r   %test_curve_params_encode_decode_namedH  s   r   c                 C   r   r   r   r   r   r   r   r   O  r   c                 C   sf   t | jtr#tt | d W d    d S 1 sw   Y  d S t| d}| |ks1J d S r9   	
isinstancer   r   pytestraisesr   r1   r   rE   r   r   r   r   (test_curve_params_encode_decode_explicitO  s   "r   c                 C   r   r   r   r   r   r   r   r   Z  r   c                 C   s   t |  }| |ksJ d S rI   r   r   r   r   r   'test_curve_params_encode_decode_defaultZ  s   r   c                 C   r   r   r   r   r   r   r   r   a  r   c                 C   sj   t | jtr$tt | dd W d    d S 1 sw   Y  d S t| dd}| |ks3J d S )Nr&   rB   r   r   r   r   r   3test_curve_params_encode_decode_explicit_compresseda  s   "r   )	unittest2unittestImportErrorr;   r   r   r   r   r   r   r   ellipticcurver	   r
   r    r   utilr   TestCaser   r|   markparametrizer   r   r   r   r   r   r   r   <module>   s.       



