
    df9                        d dl mZ d dlZd dl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 ddlmZ ddlmZ dZ ej$                  e Zd	Zd
Zej,                  dk\  rd Znd Zej,                  dk  rd Znd Zd Zd*dZ G d d      Zd Zd Zd ZefdZefdZ d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(d" Z)d# Z*d$ Z+ G d% d&e,      Z-d' Z.d( Z/d) Z0y)+    )divisionN)sha256)PY2int2bytebnext   )der)normalise_bytes)r	      iH  i='  r   r	   )r	         r	      )r	   r   r   r	      )r   c                 |    t        t        j                  | d            dd j                  t	        |       dz        S )-Convert a bytestring to string of 0's and 1'sbigr   N   )binint
from_byteszfilllenent_256s    C/home/api-vastappli/venv/lib/python3.12/site-packages/ecdsa/util.pyentropy_to_bitsr   &   s3    3>>'512126<<S\A=MNN    c                 2    dj                  d | D              S )r    c              3   h   K   | ]*  }t        t        |            d d j                  d       , yw)r   Nr   )r   ordr   ).0xs     r   	<genexpr>z"entropy_to_bits.<locals>.<genexpr>.   s)     AAs3q6{12,,Q/As   02)joinr   s    r   r   r   ,   s    wwAAAAr   )r      c                 0    t        t        |             dz
  S Nr   )r   r   r$   s    r   
bit_lengthr+   3   s    3q6{Qr   c                 *    | j                         xs dS Nr	   )r+   r*   s    r   r+   r+   8   s    ||~""r   c                 *    dt        d| z        z   dz  S )Nr	   z%xr   )r   )orders    r   orderlenr0   <   s    D5L!!a''r   c                     | dkD  sJ |t         j                  }t        | dz
        }|dz  dz   }	  ||      }t        |      }t	        |d| d      dz   }d|cxk  r| k  r|S  5)a  Return a random integer k such that 1 <= k < order, uniformly
    distributed across that range. Worst case should be a mean of 2 loops at
    (2**k)+2.

    Note that this function is not declared to be forwards-compatible: we may
    change the behavior in future releases. The entropy= argument (which
    should get a callable that behaves like os.urandom) can be used to
    achieve stability within a given release (for repeatable unit tests), but
    should not be used as a long-term-compatible key generation algorithm.
    r	   Nr   r   )baser   )osurandomr+   r   r   )r/   entropyupper_2	upper_256r   ent_2rand_nums          r   	randranger:   @   s     19**#G1q I
)$(uXgQ/!3x%O  	 r   c                       e Zd Zd Zd Zd Zy)PRNGc                 0    | j                  |      | _        y N)block_generator	generator)selfseeds     r   __init__zPRNG.__init__^   s    --d3r   c                     t        |      D cg c]  }t        | j                         }}t        rdj	                  |      S t        |      S c c}w )Nr    )ranger   r@   r   r&   bytes)rA   numbytesias       r   __call__zPRNG.__call__a   sA    +0?;aT$..!;;771:8O <s   Ac              #      K   d}	 t        d||fz  j                               j                         D ]  }|  |dz  };w)Nr   z
prng-%d-%sr	   )r   encodedigest)rA   rB   counterbytes       r   r?   zPRNG.block_generatori   sU     $/779fh 
 qLG s   ?AN)__name__
__module____qualname__rC   rJ   r?    r   r   r<   r<   X   s    4r   r<   c                      t        |       dt        |      z        }t        t        j                  |      d      |dz
  z  dz   }d|cxk  r|k  sn J d||f       |S )Nr      r	   )r<   r0   r   binasciihexlify)rB   r/   r2   numbers       r   %randrange_from_seed__overshoot_modulorY   s   sc     4:a(5/)*D(""4("-;q@F2FE 22Mr   c                     d| z  dz
  S r-   rS   )numbitss    r   lsb_of_onesr\      s    LAr   c                 j    t        t        j                  | dz
  d      dz         }|dz  }|dz  }|||fS )Nr	   r   r   )r   mathlog)r/   bitsrF   	extrabitss       r   bits_and_bytesrb      s?    txx	1%)*DAIEqI	!!r   c                     t        |      \  }}}|r|dz  } ||       j                         d | }d|t        |      z
  z  |z   }dt        t	        j
                  |      d      z   }d|cxk  r|k  sJ  J |S )Nr	    rU   )rb   rM   r   r   rV   rW   )rB   r/   hashmodr`   _bytesra   r2   rX   s           r   #randrange_from_seed__truncate_bytesrg      s     -U3D&)!4=!'6*DVc$i'(4/DX%%d+R00FMr   c                    t        t        j                  |dz
  d      dz         }|dz   dz  } ||       j                         d | }d|t	        |      z
  z  |z   }d|z  |z
  }|r)t        t        |d         t        |      z        |dd  z   }dt        t        j                  |      d      z   }d|cxk  r|k  sJ  J |S )Nr	   r   r'   r   rd   r   rU   )
r   r^   r_   rM   r   r   r"   r\   rV   rW   )rB   r/   re   r`   maxbytesr2   topbitsrX   s           r   "randrange_from_seed__truncate_bitsrk      s     txx	1%)*DqQH4=!)8,DXD	)*T1D(lT!GDG{7';;<tABxGX%%d+R00FMr   c                     |dkD  sJ t        |      \  }}}t        |       }	 t        d      }|r&t        t	         |d            t        |      z        }t        | ||      z         dz   }d|cxk  r|k  r|S  Y)Nr	   r    )rb   r<   r   r   r"   r\   string_to_number)rB   r/   r`   rF   ra   generate	extrabyteguesss           r    randrange_from_seed__trytryagainrq      s     19+E2D%DzH
bE	 Xa[!1K	4J!JKI Xe_!<=AL  r   c                     t        |      }dt        d|z        z   dz   }t        j                  || z  j	                               }t        |      |k(  sJ t        |      |f       |S Nz%0r   r$   )r0   strrV   	unhexlifyrL   r   numr/   lfmt_strstrings        r   number_to_stringr{      sa    ASQZ#%G3 6 6 89Fv;!-c&k1--Mr   c                     t        |      }dt        d|z        z   dz   }t        j                  || z  j	                               }|d | S rs   )r0   rt   rV   ru   rL   rv   s        r   number_to_string_cropr}      sK    ASQZ#%G3 6 6 89F"1:r   c                 @    t        t        j                  |       d      S NrU   )r   rV   rW   )rz   s    r   rm   rm      s    x',,r   c                     t        |      }t        |       |k(  sJ t        |       |f       t        t        j                  |       d      S r   )r0   r   r   rV   rW   )rz   r/   rx   s      r   string_to_number_fixedlenr      s@    Av;!-c&k1--x',,r   c                 :    t        | |      }t        ||      }||fS r>   )r{   rsr/   r_strs_strs        r   sigencode_stringsr      s$    Q&EQ&E5>r   c                 ,    t        | ||      \  }}||z   S )a  
    Encode the signature to raw format (:term:`raw encoding`)

    It's expected that this function will be used as a `sigencode=` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: bytes
    r   r   s        r   sigencode_stringr      s     " %Q51LE55=r   c                 z    t        j                  t        j                  |       t        j                  |            S )a  
    Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.

    Encodes the signature to the following :term:`ASN.1` structure::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as a `sigencode=` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: DER encoding of ECDSA signature
    :rtype: bytes
    )r
   encode_sequenceencode_integerr   r   r/   s      r   sigencode_derr      s-    , s11!4c6H6H6KLLr   c                 6    ||dz  kD  r||z
  }t        | ||      S r)   r   r   s      r   sigencode_strings_canonizer     s%    519}AIQ5))r   c                 6    ||dz  kD  r||z
  }t        | ||      S r)   )r   r   s      r   sigencode_string_canonizer   #  s%    519}AIAq%((r   c                 6    ||dz  kD  r||z
  }t        | ||      S r)   )r   r   s      r   sigencode_der_canonizer   )  s%    519}AIAu%%r   c                       e Zd ZdZy)MalformedSignatureaB  
    Raised by decoding functions when the signature is malformed.

    Malformed in this context means that the relevant strings or integers
    do not match what a signature over provided curve would create. Either
    because the byte strings have incorrect lengths or because the encoded
    values are too large.
    N)rP   rQ   rR   __doc__rS   r   r   r   r   /  s     	r   r   c                     t        |       } t        |      }t        |       d|z  k(  s't        dj	                  d|z  t        |                   t        | d| |      }t        | |d |      }||fS )a  
    Decoder for :term:`raw encoding`  of ECDSA signatures.

    raw encoding is a simple concatenation of the two integers that comprise
    the signature, with each encoded using the same amount of bytes depending
    on curve size/order.

    It's expected that this function will be used as the `sigdecode=`
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param signature: encoded signature
    :type signature: bytes like object
    :param order: order of the curve over which the signature was computed
    :type order: int

    :raises MalformedSignature: when the encoding of the signature is invalid

    :return: tuple with decoded 'r' and 's' values of signature
    :rtype: tuple of ints
    r   zWInvalid length of signature, expected {0} bytes long, provided string is {1} bytes longN)r   r0   r   r   formatr   )	signaturer/   rx   r   r   s        r   sigdecode_stringr   <  s|    *  	*IAy>QU" 006q1uc)n0M
 	
 	")BQ-7A!)AB-7Aa4Kr   c                    t        |       dk(  s#t        dj                  t        |                   | \  }}t        |      }t        |      }t	        |      }t        |      |k(  s$t        dj                  |t        |                  t        |      |k(  s$t        dj                  |t        |                  t        ||      }t        ||      }||fS )a  
    Decode the signature from two strings.

    First string needs to be a big endian encoding of 'r', second needs to
    be a big endian encoding of the 's' parameter of an ECDSA signature.

    It's expected that this function will be used as the `sigdecode=`
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param list rs_strings: list of two bytes-like objects, each encoding one
        parameter of signature
    :param int order: order of the curve over which the signature was computed

    :raises MalformedSignature: when the encoding of the signature is invalid

    :return: tuple with decoded 'r' and 's' values of signature
    :rtype: tuple of ints
    r   z3Invalid number of strings provided: {0}, expected 2zjInvalid length of first string ('r' parameter), expected {0} bytes long, provided string is {1} bytes longzkInvalid length of second string ('s' parameter), expected {0} bytes long, provided string is {1} bytes long)r   r   r   r   r0   r   )
rs_stringsr/   r   r   rx   r   r   s          r   sigdecode_stringsr   ]  s    & z?a AHHJ
 	

  NUEE"EE"EAu:? 3u:.
 	

 u:? 3u:.
 	

 	"%/A!%/Aa4Kr   c                 p   t        |       } t        j                  |       \  }}|dk7  r+t        j                  dt	        j
                  |      z        t        j                  |      \  }}t        j                  |      \  }}|dk7  r+t        j                  dt	        j
                  |      z        ||fS )a  
    Decoder for DER format of ECDSA signatures.

    DER format of signature is one that uses the :term:`ASN.1` :term:`DER`
    rules to encode it as a sequence of two integers::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as as the `sigdecode=`
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param sig_der: encoded signature
    :type sig_der: bytes like object
    :param order: order of the curve over which the signature was computed
    :type order: int

    :raises UnexpectedDER: when the encoding of signature is invalid

    :return: tuple with decoded 'r' and 's' values of signature
    :rtype: tuple of ints
    r   ztrailing junk after DER sig: %sz#trailing junk after DER numbers: %s)r   r
   remove_sequenceUnexpectedDERrV   rW   remove_integer)sig_derr/   r   emptyr   restr   s          r   sigdecode_derr     s    2 g&G++G4J|-0@0@0GG
 	
   ,GAt!!$'HAu|1H4D4DU4KK
 	
 a4Kr   r>   )1
__future__r   r3   r^   rV   syshashlibr   sixr   r   r   r   r    r
   _compatr   oid_ecPublicKey
encode_oidencoded_oid_ecPublicKeyoid_ecDH	oid_ecMQVversion_infor   r+   r0   r:   r<   rY   r\   rb   rg   rk   rq   r{   r}   rm   r   r   r   r   r   r   r   	Exceptionr   r   r   r   rS   r   r   <module>r      s    	   
  & &  $ +(#../:   	tOB
 f
#(0 6
"& >D  =C &--*M:*)&
	 
	B+\&r   