
    Bwg"Y                     N   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k7  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:y)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    F/var/www/horilla/myenv/lib/python3.12/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kD  r||j                  k  rn.t        dt        dt        d|       d	      t        d
|d      d      }t        |      }|j!                         |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
            F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   s   " C?@@ K	
  	 "%(O))) 	 &7():5IQ?5E5K5K#K  &!4,6
 " $&*%
 '  ++;<H;C==?]#**<8x/1ABBr   c                     t        | t              st        t        dt	        |                   | j
                  \  }}|dk(  rt        t        d            |dk(  rt        t        d            |dk(  rB|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 k"
  	 &++J]"
  	 [ 
  	 
w	#EFF^ W	   .--
 	
 M : A A- P W WWL''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#      sk    " C?@@ K	
  	 ',)
  '	 	 	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*                  }
 ||      j-                         }t/        |      }t1        |d	
      |
z  }d|z  }d|z  }t3        j4                  ||dz   |z   |z   |      j-                         }t3        j4                  |||      j-                         }t3        j4                  ||dz   |z   |z   |      j-                         }t3        j4                  |||      j-                         }d}d}	 d}t/        |      |k  r9t3        j4                  |||      j-                         }||z  }t/        |      |k  r9t1        |d| d	
      }|dk(  s||
k\  re|	|z  j6                  |
z  }|dk(  r}t9        ||
      |||z  |
z  z   z  |
z  }|dk(  r	 t;        ||d      j=                         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   r   )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+:J:JN/[ k"
  	 ""JDEE
  	 dH% dO	
  	 S!QRR  
  	 0I %%m4;;N&}5>>$]3::O!*-O))) 	 	A
 t_##%Ff+Kve,q0A 	+A 	+A 	AK"33f<iHOOQA 	Ay!((*A 	AK"33f<iHOOQA 	Ay!((*A 	
A	A
 !f&Ay)002AFA !f&
 1Q/>6Q!V !$$q(6A!':a&?"?@AE6aa()..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#                         \  }}	|j$                  }
t'        |j                  ||	|
      }	 t)        j*                  |      }|d
   j,                  }|d   j,                  }d}||dk  z  }|||
k\  z  }||dk  z  }|||
k\  z  }|rt/        d      t1        t2        |      } ||      j5                         }t7        |d      |
z  }t9        ||
      }||z  |
z  }||z  |
z  }||z  ||z  z   }||j:                  |
z  k7  rt/        d      y# t        $ r t/        d      w xY w)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&9H:&?&D&D}VaFbc
 /0
  	 +00JDEE
  	 i* i 	
  	 dH% dO	
  	 S!QRR  
  	 %))D$$ *)) 	 '')DAqA ""2"8"8!QB5 %%i0	cN!!cN!! G q1uGqAvG q1uGqAvG3440It_##%Fve,q0AAqA
a%1B
a%1B"R',<r,ABJZ\\A344 3  53445s   =3I I/c                     | dk  s|| k  r| |z  } | |}}d\  }}}}|dk7  r1t        ||      |fz   \  }}}|||z  z
  |||z  z
  ||f\  }}}}|dk7  r1|dk(  sJ |dkD  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     	1uQE aqANBB
q&A,!%1aa"fb1r6k2r9BB q& 6M6	Av	Avr   c                       e Zd ZdZd Zd Zy)
PrimeCurvezc
    Elliptic curve over a prime field. Characteristic two field curves are not
    supported.
    c                 .    || _         || _        || _        y)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?  se     WWuwwWWuww(b466EGG++dff45?1DDr   N)__name__
__module____qualname____doc__r   r    r   r   r   r   '  s    
$Er   r   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
r   z1
    A point on a prime-field elliptic curve
    Nc                     || _         || _        || _        || _        | j                   r&| j                   j	                  |       st        d      | j                  r"| | j                  z  t        k7  rt        d      yy)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  su     

 ::::&&t, !344::djj H, !344 - r   c                     | j                   |j                   k(  r3| j                  |j                  k(  r| j                  |j                  k(  ryy)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  s8     ::$577):tvv?Pr   c                    |t         k(  r| S | t         k(  r|S | j                  |j                  k(  sJ | j                  |j                  k(  rI| j                  |j                  z   | j                  j                  z  dk(  rt         S | j                         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zzU[[(((66UWW DJJLL0A5{{}$JJLLww;uww/?#CCqH2g(A-DFFRK 466)Q.$**b"--r   c                    d }|}| j                   r|| j                   z  }|dk(  rt        S | t        k(  rt        S |dkD  sJ d|z  }t        | j                  | j                  | j
                   | j                         } ||      dz  }| }|dkD  rE|j                         }||z  dk7  r||z  dk(  r|| z   }||z  dk(  r||z  dk7  r||z   }|dz  }|dkD  rE|S )
        :param other:
            An integer to multiple the Point by

        :return:
            A PrimePoint object
        c                 >    | dkD  sJ d}|| k  rd|z  }|| k  r|dz  S )Nr   r      r   )rW   results     r   leftmost_bitz(PrimePoint.__mul__.<locals>.leftmost_bit  s5    q5L5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    	 ::DJJA6O8O1uu U"4::tvvw

K!!e]]_FQ1}!a%A$Q1}!a%A-/QA !e r   c                     | |z  S )r   r   r   s     r   __rmul__zPrimePoint.__rmul__  s     e|r   c                 t   | 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     JJLLJJLL466zDFF"Q&+a$&&j!*DDI2gDFF
"a'DFFRK 466)Q.$**b"--r   r   )
r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   M  s&    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   
    $ '  "dGE 9 { J
KK   9Cx8Hv"J~1Br5j,^ F#E #ELT. T.p dD$' >6
 "66>	  H>
 ">>H	  RF
 "FFR	  xf
 "ffx	   b J
 " I J b	 r   