
     h"Y                        d dl mZmZmZm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mZmZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ ej        d
k     reZ e!Z"nd Z  e	            Z#e#dk    r e$d          g dZ%ddddZ&d d ddZ'd Z(d Z)d Z*d Z+d Z,	 d Z- G d d          Z. G d d          Z/ e/ddd          Z0 e.ddd           Z1 e/e1d!d"d#          Z2 e.d$dd%          Z3 e/e3d&d'd(          Z4 e.d)dd*          Z5 e/e5d+d,d-          Z6 e.d.dd/          Z7 e/e7d0d1d2          Z8 e.d3dd4          Z9 e/e9d5d6d7          Z:dS )8    )unicode_literalsdivisionabsolute_importprint_functionN   )backend)
CertificateDSASignatureECDomainParametersECPointBitStringECPrivateKeyint_from_bytesPrivateKeyAlgorithmPrivateKeyInfoPublicKeyAlgorithmPublicKeyInfo)pretty_message)	type_namebyte_cls)
rand_bytes)SignatureError)   c                 "    t          | g          S N)bytes)nums    K/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/oscrypto/_ecdsa.pychr_clsr      s    cU||    	winlegacyz2Pure-python ECDSA code is only for Windows XP/2003)ec_generate_pairec_compute_public_key_pointec_public_key_info
ecdsa_signecdsa_verify    0   B   	secp256r1	secp384r1	secp521r1   c           
      >   | t          g d          vr*t          t          dt          |                               t          |          }t
          t          t          d|          }	 t          |          }t          |d          }|dk    r||j
        k     rn3t          dt          dt          d| 	          d
          t          d|d          d          }t          |          }|                                |d         j        d<   t%          ||           |fS )a  
    Generates a EC public/private key pair

    :param curve:
        A unicode string. Valid values include "secp256r1", "secp384r1" and
        "secp521r1".

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type

    :return:
        A 2-element tuple of (asn1crypto.keys.PublicKeyInfo,
        asn1crypto.keys.PrivateKeyInfo)
    r)   \
            curve must be one of "secp256r1", "secp384r1", "secp521r1", not %s
            TFsignedr   ecnamednamevalue	algorithm
parametersecPrivkeyVer1)versionprivate_key)r;   private_key_algorithmr<   r<   
public_key)set
ValueErrorr   reprCURVE_BYTESSECP256R1_BASE_POINTSECP384R1_BASE_POINTSECP521R1_BASE_POINTr   r   orderr   r   r   r   r"   copyparsedr#   )curvecurve_num_bytescurve_base_pointprivate_key_bytesprivate_key_intprivate_key_infoec_points          r   r!   r!   C   sp   " C???@@@@ KK	
 
   	 "%(O)))  	&77():5IIIQ?5E5K#K#K &!4,  6
 6
 " " $&*%
 %
  ' '   ++;<<H;C==??]#*<8x//1ABBr   c                 L   t          | t                    s*t          t          dt	          |                               | j        \  }}|dk    rt          t          d                    |dk    rt          t          d                    |dk    rX|t          g d          vr*t          t          dt          |                              t          t          t          d|         }|| d	         j        d	         j        z  }t          j        |j        |j                  S )
a  
    Constructs the PublicKeyInfo for a PrivateKeyInfo

    :param private_key:
        An asn1crypto.keys.PrivateKeyInfo object

    :raises:
        ValueError - when any of the parameters contain an invalid value

    :return:
        An asn1crypto.keys.ECPointBitString object
    zy
            private_key must be an instance of the
            asn1crypto.keys.PrivateKeyInfo class, not %s
            implicit_cazj
            Unable to compute public key for EC key using Implicit CA
            parameters
            	specifiedzX
            Unable to compute public key for EC key over a specified field
            r3   r)   zj
                Named curve must be one of "secp256r1", "secp384r1", "secp521r1", not %s
                r<   )
isinstancer   	TypeErrorr   r   rI   r@   r?   rA   rC   rD   rE   rH   nativer   from_coordsxy)r<   
curve_typedetails
base_pointpublic_points        r   r"   r"      s\    k>22  k""
 
   	 &+J]""
 
   	 [  
 
   	 
w		#EEEFFFF^ W	     .--
 
 	
 M : A- P WWL'GGGr   c           	          |t          g d          vr*t          t          dt          |                              t	          t          dt          d|          d          | d          S )a  
    Constructs the PublicKeyInfo for an ECPointBitString

    :param private_key:
        An asn1crypto.keys.ECPointBitString object

    :param curve:
        A unicode string of the curve name - one of secp256r1, secp384r1 or secp521r1

    :raises:
        ValueError - when any of the parameters contain an invalid value

    :return:
        An asn1crypto.keys.PublicKeyInfo object
    r)   r/   r2   r3   r4   r7   )r8   r>   )r?   r@   r   rA   r   r   r   )public_key_pointrI   s     r   r#   r#      s    " C???@@@@ KK	
 
   	 ',  )
 )
   '	 	 	 	 	r   c                    t          | d          rt          | j        t                    s*t	          t          dt          |                               | j        }|t          g d          vrt          t          d                    t          |t                    s*t	          t          dt          |                              |t          g d          vr*t          t          dt          |                              t          t          |          }| j        d         j        }|d         j        }|d         j        }t"          |         }t$          t&          t(          d|         }	|	j        }
 ||                                          }t/          |          }t1          |d	
          |
z  }d|z  }d|z  }t3          j        ||dz   |z   |z   |                                          }t3          j        |||                                          }t3          j        ||dz   |z   |z   |                                          }t3          j        |||                                          }d}d}	 d}t/          |          |k     r@t3          j        |||                                          }||z  }t/          |          |k     @t1          |d|         d	
          }|dk    s||
k    r||	|z  j        |
z  }|dk    rt9          ||
          |||z  |
z  z   z  |
z  }|dk    r	 t;          ||d                                          S )aN  
    Generates an ECDSA signature in pure Python (thus slow)

    :param private_key:
        The PrivateKey to generate the signature with

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "sha1", "sha256", "sha384" or "sha512"

    :raises:
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library

    :return:
        A byte string of the signature
    asn1zy
            private_key must be an instance of the
            oscrypto.asymmetric.PrivateKey class, not %s
            r)   zx
            private_key does not use one of the named curves secp256r1,
            secp384r1 or secp521r1
            <
            data must be a byte string, not %s
            sha1sha224sha256sha384sha512z
            hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
            "sha512", not %s
            r<   Fr0          r   Tr   )rs)hasattrrS   r`   r   rT   r   r   rI   r?   r@   r   rA   getattrhashlibrH   contentsrU   rB   rC   rD   rE   rF   digestlenr   hmacnewrW   inverse_modr
   dump)r<   datahash_algorithm
curve_name	hash_funcec_private_keyrL   rM   rJ   rK   nrq   hash_lengthhVKrk   rl   Tks                       r   r$   r$      s   , ;'' z+:JN/[/[  k""
 
   	 "JDDDEEEE
 
   	 dH%%  dOO	
 
   	 S!Q!Q!QRRRR   
 
   	 00I %m4;N&}5>$]3:O!*-O)))  	 	A
 Yt__##%%Ff++Kve,,,q0A 	+A 	+A 	AK"33f<iHHOOQQA 	Ay!!((**A 	AK"33f<iHHOOQQA 	Ay!!((**A 	
A	A !ff&&Ay))0022AFA !ff&&
 1Q./>>>66Q!VV !$q(66A!':a&?"?@AE66aa(())..000r   c                    t          | d          }|r!t          | j        t          t          f          s*t          t          dt          |                               | j        }|t          g d          vrt          t          d                    t          |t                    s*t          t          dt          |                              t          |t                    s*t          t          dt          |                              |t          g d          vr*t          t          dt          |                              | j        }t          |t                    r|j        }t          t          t           d|         }|d	                                         \  }}	|j        }
t'          |j        ||	|
          }	 t)          j        |          }|d
         j        }|d         j        }n# t          $ r t/          d          w xY wd}||dk     z  }|||
k    z  }||dk     z  }|||
k    z  }|rt/          d          t1          t2          |          } ||                                          }t7          |d          |
z  }t9          ||
          }||z  |
z  }||z  |
z  }||z  ||z  z   }||j        |
z  k    rt/          d          dS )a  
    Verifies an ECDSA signature in pure Python (thus slow)

    :param certificate_or_public_key:
        A Certificate or PublicKey instance to verify the signature with

    :param signature:
        A byte string of the signature to verify

    :param data:
        A byte string of the data the signature is for

    :param hash_algorithm:
        A unicode string of "md5", "sha1", "sha256", "sha384" or "sha512"

    :raises:
        oscrypto.errors.SignatureError - when the signature is determined to be invalid
        ValueError - when any of the parameters contain an invalid value
        TypeError - when any of the parameters are of the wrong type
        OSError - when an error is returned by the OS crypto library
    r`   z
            certificate_or_public_key must be an instance of the
            oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate
            classes, not %s
            r)   z
            certificate_or_public_key does not use one of the named curves
            secp256r1, secp384r1 or secp521r1
            zA
            signature must be a byte string, not %s
            ra   rb   rh   r>   rk   rl   zSignature is invalidr   r   Fr0   N)rm   rS   r`   r   r	   rT   r   r   rI   r?   r@   r   rA   r>   rC   rD   rE   	to_coordsrF   
PrimePointr
   loadrU   r   rn   ro   rq   r   ru   rW   )certificate_or_public_key	signaturerw   rx   has_asn1ry   r`   rK   rW   rX   r|   r^   rk   rl   invalidrz   rq   zwu1u2
hash_points                         r   r%   r%   `  s]   . 0&99H :&?&D}VaFbcc 
 /00
 
   	 +0JDDDEEEE
 
   	 i**  i  	
 
   	 dH%%  dOO	
 
   	 S!Q!Q!QRRRR   
 
   	 %)D$$$  *))  	 ''))DAqA ""2"8!QBB5 %i00	cN!cN! 5 5 534445 G q1uGqAvG q1uGqAvG 5344400IYt__##%%Fve,,,q0AAqA
a%1B
a%1B"R',<r,ABJZ\A3444 s   	.G8 8Hc                     | dk     s|| k    r| |z  } | |}}d\  }}}}|dk    r5t          ||          |fz   \  }}}|||z  z
  |||z  z
  ||f\  }}}}|dk    5|dk    sJ |dk    r|S ||z   S )z
    Compute the modular inverse of a (mod p)

    :param a:
        An integer

    :param p:
        An integer

    :return:
        An integer
    r   )r   r   r   r   r   )divmod)	apcducvcudvdqs	            r   ru   ru     s     	1uuQE aqANBB
q&&A,,!%1aa"fb1r6k2r9BB q&& 6666	Avv	Avr   c                       e Zd ZdZd Zd ZdS )
PrimeCurvezc
    Elliptic curve over a prime field. Characteristic two field curves are not
    supported.
    c                 0    || _         || _        || _        dS )a  
        The curve of points satisfying y^2 = x^3 + a*x + b (mod p)

        :param p:
            The prime number as an integer

        :param a:
            The component a as an integer

        :param b:
            The component b as an integer
        N)r   r   b)selfr   r   r   s       r   __init__zPrimeCurve.__init__-  s     r   c                     |j         |j         z  }|j        |j        z  |j        z  }||| j        |j        z  z   | j        z   z
  | j        z  dk    S )z~
        :param point:
            A Point object

        :return:
            Boolean if the point is on this curve
        r   )rX   rW   r   r   r   )r   pointy2x3s       r   containszPrimeCurve.contains?  sS     WuwWuw(b46EG++df45?1DDr   N)__name__
__module____qualname____doc__r   r    r   r   r   r   '  sA         
  $E E E E Er   r   c                   8    e Zd ZdZd	dZd Zd Zd Zd Zd Z	dS )
r   z1
    A point on a prime-field elliptic curve
    Nc                     || _         || _        || _        || _        | j         r)| j                             |           st          d          | j        r"| | j        z  t          k    rt          d          dS dS )a)  
        :param curve:
            A PrimeCurve object

        :param x:
            The x coordinate of the point as an integer

        :param y:
            The y coordinate of the point as an integer

        :param order:
            The order of the point, as an integer - optional
        zInvalid EC pointN)rI   rW   rX   rF   r   r@   INFINITY)r   rI   rW   rX   rF   s        r   r   zPrimePoint.__init__R  s     

 : 	5:&&t,, 5 !3444: 	5dj H,, !3444	5 	5,,r   c                 j    | j         |j         k    r"| j        |j        k    r| j        |j        k    rdS dS )zy
        :param other:
            A PrimePoint object

        :return:
            0 if identical, 1 otherwise
        r   r   )rI   rW   rX   r   others     r   __cmp__zPrimePoint.__cmp__o  s9     :$$57):):tv?P?P11r   c                    |t           k    r| S | t           k    r|S | j        |j        k    sJ | j        |j        k    r;| j        |j        z   | j        j        z  dk    rt           S |                                 S | j        j        }|j        | j        z
  t          |j        | j        z
  |          z  |z  }||z  | j        z
  |j        z
  |z  }|| j        |z
  z  | j        z
  |z  }t          | j        ||          S )zq
        :param other:
            A PrimePoint object

        :return:
            A PrimePoint object
        r   )r   rI   rW   rX   r   doubleru   r   )r   r   r   l_r   y3s         r   __add__zPrimePoint.__add__|  s     HK8LzU[((((6UW DJL0A55{{}}$JLw;uw/?#C#CCqH2g(A-DFRK 46)Q.$*b"---r   c                    d }|}| j         r
|| j         z  }|dk    rt          S | t          k    rt          S |dk    sJ d|z  }t          | j        | j        | j         | j                   } ||          dz  }| }|dk    rM|                                }||z  dk    r||z  dk    r|| z   }||z  dk    r||z  dk    r||z   }|dz  }|dk    M|S )
        :param other:
            An integer to multiple the Point by

        :return:
            A PrimePoint object
        c                 B    | dk    sJ d}|| k    rd|z  }|| k    |dz  S )Nr   r      r   )rW   results     r   leftmost_bitz(PrimePoint.__mul__.<locals>.leftmost_bit  s;    q5555FA++V A++Q;r   r   r   r   r   )rF   r   r   rI   rW   rX   r   )r   r   r   ee3negative_selfir   s           r   __mul__zPrimePoint.__mul__  s   	 	 	 : 	DJA66O8O1uuuu U"4:tvw
KKL!!ee]]__FQ1}}!a%A$Q1}}!a%A-/QA !ee r   c                     | |z  S )r   r   r   s     r   __rmul__zPrimePoint.__rmul__  s     e|r   c                    | j         j        }| j         j        }d| j        z  | j        z  |z   t	          d| j        z  |          z  |z  }||z  d| j        z  z
  |z  }|| j        |z
  z  | j        z
  |z  }t          | j         ||          S )zS
        :return:
            A PrimePoint object that is twice this point
        r   r   )rI   r   r   rW   ru   rX   r   )r   r   r   r   r   r   s         r   r   zPrimePoint.double  s     JLJL46zDF"Q&+a$&j!*D*DDI2gDF
"a'DFRK 46)Q.$*b"---r   r   )
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   M  s~         5 5 5 5:  . . .>) ) )V	 	 	. . . . .r   r   l   l   9{uDjSg9g(Bl   +' 1t:_|v!a:@ml   H<^W]dZ{cxW\Iq l   1(i&^#a;l              ?l   FeY8w-X"PVd/%PP!-l   !"X!#BXtJ9!'|%VA-l   4~ 
f&Dv@h!fE0m9_qlM/l   =*8%(?l   ?               @ l   K`Opq^cv
3,e<1U]>{|R*Zl   B11e	%:f=K`wrH7gHK8hkl   Q~o]l+fUg+<)Z?8O?q!Ol   Q%x+Ohbi+}s   @ l          ~l   *'#.TEbc+Z'@=D 1 "(?7N2Z_+|S/1fl   
dxRjoyU8T(	:ss"nZL8k&"_Ul   _!uR/sX0
@qaNQNB&JxS8KJEY	K%l l   s)e`gwlX_[nlv|l#   l#    ?VQ(zO%b95~cte1oR{V;LHw>l-rZE]"Sr&Ga9}*Fl#   f=xK)H-apY$3^Q	n%k{;/K!u{4-{?$Od8V1l3s:l#   Pf?QE$XN!85aZUWL9YLhzf$Du13otc!%pMxjRr`l#   	dp"z\}[z3"nZ;PK#
`7roCQ);
__future__r   r   r   r   ro   rs   sys r   _asn1r	   r
   r   r   r   r   r   r   r   r   _errorsr   _typesr   r   utilr   errorsr   version_infochrr   xrangerange_backendSystemError__all__rB   CURVE_EXTRA_BITSr!   r"   r#   r$   r%   ru   r   r   r   SECP192R1_CURVESECP192R1_BASE_POINTSECP224R1_CURVESECP224R1_BASE_POINTSECP256R1_CURVErC   SECP384R1_CURVErD   SECP521R1_CURVErE   r   r   r   <module>r      s   R R R R R R R R R R R R   



                              $ # # # # # ' ' ' ' ' ' ' '       " " " " " "dGEE   799 { +J
K
KK       9C 9C 9Cx8H 8H 8Hv" " "J~1 ~1 ~1Br5 r5 r5j,^     F#E #E #E #E #E #E #E #ELT. T. T. T. T. T. T. T.p :dD$'' *>6 
 "z66>	   *H> 
 "z>>H	   *RF 
 "zFFR	   *xf 
 "zffx	   * b J 
 "z I J b	    r   