o
    6h,                  
   @   s  d dl mZmZ d dlZzd dlmZ W n ey!   g dZY nw dd eD Zd dlmZ d dlZd dl	Z	d dl
mZ d dlmZmZmZmZmZ d	d
lmZ d	dlmZ d	dlmZmZ d	dlmZmZ d	dlmZmZmZ d	dlmZm Z m!Z!m"Z"m#Z#m$Z$ d	dl%m&Z& dZ'	 dd eD Z(	 de	j)v reegZg Z*	 e+edd dD ]/Z,dd e+e(dd dD D ]Z-ej.e,eej/e-dZ0e*1d2e,e-e0j3e0j4e'edf qqej56ddd e*D dd Z7ej8d d! Z9i Z:e	j;d"kr	d d#lm<Z< d$e:d%< e<j=e<j>e<j?ge:d&< de	j)v rd'e:d(< e@e:ZAde	j)v r"d	eAd(< nd)eAd(< edHi eAee9e*d*d+ ZBej8d,d- ZCedHi eAeeC d.d/ ZDd0d1 ZEej8d2d3 ZFd4d5 ZGd6d7 ZHej8d8d9 ZId:d; ZJedHi eAeeKe*eJ d<d= ZLedHi eAeeKe*ejMd>d?ee*d  e#ed ed ee*d  e#ed	ed	d@ ee*d  e#ed	gdA  dBdC ZNdDd e*D ZO	 eOdEd e*D 7 ZOedHi eAee9eOdFdG ZPdS )I    )with_statementdivisionN)algorithms_available)md5sha1sha224sha256sha384sha512c                 C   s   g | ]}|d vr|qS ))mdc2md2md4	whirlpool	ripemd160 ).0ir   r   m/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/ecdsa/test_malformed_sigs.py
<listcomp>   s
    r   )partial)noteassumegivensettingsexample   )
SigningKey)BadSignatureError)sigencode_dersigencode_string)sigdecode_dersigdecode_string)curves	SECP112r2	SECP128r1)encode_integerencode_bitstringencode_octet_string
encode_oidencode_sequenceencode_constructed)	CurveEdTws   some data to signc                 C   s   g | ]
}|t |jfqS r   )hashlibnewdigest_size)r   namer   r   r   r   2   s    z--fastc                 C   s   | j S N)baselenxr   r   r   <lambda>C   s    r4   )keyc                 C   s.   g | ]\}}d |  k rt jkrn n|qS )r   )curver1   )r   r/   sizer   r   r   r   D   s
    c                 C   s   | d S )Nr   r   r2   r   r   r   r4   F       )hashfuncz{0} {1})	sigencodezverifying_key,signaturec                 C   s"   g | ]\}}}t j|||d qS ))id)pytestparam)r   r/   vksigr   r   r   r   Y   s   " c                 C   s   | j |ttds
J d S N	sigdecode)verifyexample_datar    )verifying_key	signaturer   r   r   test_signaturesW   s   rG   c                 C   sZ  | t |\}}}td| t|}| t jt jdt|d ddd}|  t	|D ]}||= q/td| d}|rn| t 
t jdt|d dt jdd	d}| D ]\}}	||  |	N  < qZtd
| | t jdt|d}
| t jdd}|d|
 | ||
d  }td|
| t|}t|p|p| t||k ||fS )z
    Hypothesis strategy that generates pairs of VerifyingKey and malformed
    signatures created by fuzzing of a valid signature.
    Configuration: {0}r   r   	min_value	max_valueT)uniquezRemove bytes: {0}N   z	xors: {0}   max_sizez%Inserted at position {0} bytes: {1!r})stsampled_fromr   format	bytearraylistsintegerslensortreverseddictionariesitemsbinarybytesr   )drawkeys_and_sigsr/   rE   old_sigr?   	to_remover   xorsval
insert_posinsert_datar   r   r   st_fuzzed_siga   s>   
rf   )      )HealthChecki  deadlinesuppress_health_check   max_examples
   c                 C   H   | \}}t t |j|ttd W d    d S 1 sw   Y  d S r@   r<   raisesr   rC   rD   r    )argsrE   r?   r   r   r   test_fuzzed_der_signatures      "rs   c                 C   s   | t t\}}}td| t|jj}| t jd|d> dt j|d? |d dB }| t jd|d> dt j|d? |d dB }t	t
|t
|}||fS )z
    Hypothesis strategy for selecting random values and encoding them
    to ECDSA-Sig-Value object::

        ECDSA-Sig-Value ::= SEQUENCE {
            r INTEGER,
            s INTEGER
        }
    rH   r      rI   rg   r   )rQ   rR   r_   r   rS   intr6   orderrV   r)   r%   )r^   r/   rE   _rw   rsr?   r   r   r   st_random_der_ecdsa_sig_value   s   r{   c                 C   sH   | \}}t t |j|ttd W d   dS 1 sw   Y  dS )zd
    Check if random values encoded in ECDSA-Sig-Value structure are rejected
    as signature.
    rA   Nrp   paramsrE   r?   r   r   r   test_random_der_ecdsa_sig_value   s   "r~   c                  O   s(   d|vrd|d< t tt j| i |S )z
    Hypothesis strategy that returns a random positive integer as DER
    INTEGER.
    Parameters are passed to hypothesis.strategy.integer.
    rJ   r   )rQ   buildsr%   rV   rr   kwargsr   r   r   st_der_integer   s   r   c                 O   s`   | t j|i |}|r)| t jddd}t|}|d  d|  M  < t|}nd}t||S )z|
    Hypothesis strategy that returns a random DER BIT STRING.
    Parameters are passed to hypothesis.strategy.binary.
    r   rh   rI   rg   )rQ   r\   rV   rT   r]   r&   )r^   rr   r   dataunusedr   r   r   st_der_bit_string   s   

r   c                  O   s   t tt j| i |S )z
    Hypothesis strategy that returns a random DER OCTET STRING object.
    Parameters are passed to hypothesis.strategy.binary
    )rQ   r   r'   r\   r   r   r   r   st_der_octet_string   s   r   c                   C   s
   t dS )z;
    Hypothesis strategy that returns DER NULL object.
    s    )rQ   justr   r   r   r   st_der_null  s   
r   c                 C   sv   | t jddd}|dk r| t jddd}n| t jddd d}| t jt jddd ddd}t||g|R  S )zI
    Hypothesis strategy that returns DER OBJECT IDENTIFIER objects.
    r   rg   rI   '   i   2   rO   )rQ   rV   rU   r(   )r^   firstsecondrestr   r   r   
st_der_oid  s   r   c                   C   sH   t jt dtdd dB tddB tddB t B t B dd d	d
S )z
    Hypothesis strategy that returns random DER structures.

    A valid DER structure is any primitive object, an octet encoding
    of a valid DER structure, sequence of valid DER objects or a constructed
    encoding of any of the above.
        rg   i   )rK      rO   c                 S   s`   t tt | t dd t | B t dd t j| ddB t tt jdddt | B S )	Nc                 S   s
   t | dS )Nr   )r&   r2   r   r   r   r4   .  s   
 z*st_der.<locals>.<lambda>.<locals>.<lambda>c                 S   s   t |  S r0   )r)   r2   r   r   r   r4   0  r8      rO   r   ?   rI   )rQ   r   r'   one_ofrU   r*   rV   )childrenr   r   r   r4   -  s    zst_der.<locals>.<lambda>(   )
max_leaves)rQ   	recursiver   r   r   r   r   r   r   r   r   r   st_der  s   
r   c                 C   J   | \}}}t t |j|ttd W d   dS 1 sw   Y  dS )z8Check if random DER structures are rejected as signaturerA   Nrp   r}   derr/   rE   rx   r   r   r   test_random_der_as_signature;  s   
"r   r   rO          c                 C   r   )z/Check if random bytes are rejected as signaturerA   Nrp   r   r   r   r   test_random_bytes_as_signatureE  s   
"r   c                 C   sB   g | ]\}}}t |jjts||tt||jjd |jjifqS )rw   )
isinstancer6   r+   r   r    rw   r   r/   rE   r?   r   r   r   r   W  s    	c                 C   s*   g | ]\}}}t |jjtr|||fqS r   )r   r6   r+   r   r   r   r   r   i  s    c                 C   ro   r@   )r<   rq   r   rC   rD   r!   r|   r   r   r   test_fuzzed_string_signaturest  rt   r   r   )Q
__future__r   r   r,   r   ImportError	functoolsr   r<   syshypothesis.strategies
strategiesrQ   
hypothesisr   r   r   r   r   keysr   r   utilr   r   r    r!   r"   r#   r$   r   r%   r&   r'   r(   r)   r*   ellipticcurver+   rD   hash_and_sizeargvr_   sortedr6   hash_alggenerater-   skappendrS   rE   signmarkparametrizerG   	compositerf   r}   version_infori   data_too_largefilter_too_muchtoo_slowdictslow_paramsrs   r{   r~   r   r   r   r   r   r   rR   r   r\   r   keys_and_string_sigsr   r   r   r   r   <module>   s     




3


!

	
