a
    _mgg>                     @   s  d dl mZ d dlmZ ddlmZmZmZmZ ddl	m
Z
mZmZ ddlmZmZ g dZdZd	ZG d
d deZG dd dZedejejddZedejejddZedejejddZedejej ddZ!edej"ej#ddZ$edej%ej&ddZ'ed ej(ej)d!d"Z*ed#ej+ej,d$d%Z-ed&ej.ej/d'd(Z0ed)ej1ej2d*d+Z3ed,ej4ej5d-d.Z6ed/ej7ej8d0d1Z9ed2ej:ej;d3d4Z<ed5ej=ej>d6d7Z?ed8ej@ejAd9d:ZBed;ejCejDd<d=ZEed>ejFejGd?d@ZHedAejIejJdBdCZKedDejLejMdEdFZNedGejOejPdHdIZQedJejRejSdKdLZTedMejUejVdNdOZWedPejXejYdQdRZZedSej[ej\dTdUZ]edVej^ej_dWZ`edXejaejbdYZce$e'e*e-e0e3e6e<eBeHeNeTeZeeee!e`ece9e?eEeKeQeWe]gZddZd[ Zed\d] Zfd^S )_    )division)PY2   )derecdsaellipticcurveeddsa)orderlennumber_to_stringstring_to_number)normalise_bytes
bit_length)"UnknownCurveErrorr	   Curve	SECP112r1	SECP112r2	SECP128r1	SECP160r1NIST192pNIST224pNIST256pNIST384pNIST521pcurves
find_curvecurve_by_name	SECP256k1BRAINPOOLP160r1BRAINPOOLP160t1BRAINPOOLP192r1BRAINPOOLP192t1BRAINPOOLP224r1BRAINPOOLP224t1BRAINPOOLP256r1BRAINPOOLP256t1BRAINPOOLP320r1BRAINPOOLP320t1BRAINPOOLP384r1BRAINPOOLP384t1BRAINPOOLP512r1BRAINPOOLP512t1PRIME_FIELD_OIDCHARACTERISTIC_TWO_FIELD_OIDEd25519Ed448)r      H  ='  r   r   )r   r/   r0   r1   r   r/   c                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r5   r5   C/var/www/html/idle/venv/lib/python3.9/site-packages/ecdsa/curves.pyr   5   s   r   c                   @   s^   e Zd ZdddZdd Zdd Zdd	 ZdddZdddZe	dddZ
edddZdS )r   Nc                 C   s   || _ || _|| _|| _| | _t|tjrRt|	 d d d | _
| j
| _nt| j| _
dt|	  | _d| j
 | _|| _|rtj| | _d S )Nr         r/   )nameopenssl_namecurve	generatororder
isinstancer   	CurveEdTwr   pZbaselenZverifying_key_lengthr	   Zsignature_lengthoidr   
encode_oidZencoded_oid)selfr9   r;   r<   rA   r:   r5   r5   r6   __init__:   s    

zCurve.__init__c                 C   s&   t |tr"| j|jko | j|jkS tS N)r>   r   r;   r<   NotImplementedrC   otherr5   r5   r6   __eq__O   s    
zCurve.__eq__c                 C   s
   | |k S rE   r5   rG   r5   r5   r6   __ne__V   s    zCurve.__ne__c                 C   s   | j S rE   r9   )rC   r5   r5   r6   __repr__Y   s    zCurve.__repr__uncompressedc                 C   s0  |du r| j rd}nd}|dvr(td|dkrJ| j s>tdtj| j  S t| jtjrl|dksdJ td| j	 }t
d}ttjt t
|}ttt| j | |tt| j | |}t| j|}t
| j }|||||g}	| j r&t
| j }
|	|
 tj|	 S )	a  Serialise the curve parameters to binary string.

        :param str encoding: the format to save the curve parameters in.
            Default is ``named_curve``, with fallback being the ``explicit``
            if the OID is not set for the curve.
        :param str point_encoding: the point encoding of the generator when
            explicit curve encoding is used. Ignored for ``named_curve``
            format.

        :return: DER encoded ECParameters structure
        :rtype: bytes
        Nnamed_curveexplicitrN   rO   z5Only 'named_curve' and 'explicit' encodings supportedzJCan't encode curve using named_curve encoding without associated curve OIDz6Twisted Edwards curves don't support explicit encodingr   )rA   
ValueErrorr   r   rB   r>   r;   r   r?   r@   Zencode_integerZencode_sequencer+   Zencode_octet_stringr
   abr<   to_bytesr=   cofactorappend)rC   encodingpoint_encodingZcurve_pversionfield_idr;   baser=   Zseq_elementsrU   r5   r5   r6   to_der\   sN    


zCurve.to_derc                 C   s   t | ||dS )a  
        Serialise the curve parameters to the :term:`PEM` format.

        :param str encoding: the format to save the curve parameters in.
            Default is ``named_curve``, with fallback being the ``explicit``
            if the OID is not set for the curve.
        :param str point_encoding: the point encoding of the generator when
            explicit curve encoding is used. Ignored for ``named_curve``
            format.

        :return: PEM encoded ECParameters structure
        :rtype: str
        zEC PARAMETERS)r   Ztopemr\   )rC   rW   rX   r5   r5   r6   to_pem   s    zCurve.to_pemc                 C   s  |st d}tdd |D s&tdt| } t| snd|vrJtdt| \}}|rftdt|S d|vrtd	t	| \}}|rtd
t
|\}}|dkrtdt	|\}}t	|\}}t|\}	}t
|\}
}d}|rt
|\}}t|\}}|tkr,td|tkrDtd|t
|\}}|rbtdt|\}}t|\}}t|}t|}t||||}tjj||	d|
dd}td||d}tD ]}||kr|  S q|S )a  Decode the curve parameters from DER file.

        :param data: the binary string to decode the parameters from
        :type data: :term:`bytes-like object`
        :param valid_encodings: set of names of allowed encodings, by default
            all (set by passing ``None``), supported ones are ``named_curve``
            and ``explicit``
        :type valid_encodings: :term:`set-like object`
        rP   c                 s   s   | ]}|d v V  qdS )rP   Nr5   ).0ir5   r5   r6   	<genexpr>       z!Curve.from_der.<locals>.<genexpr>z1Only named_curve and explicit encodings supportedrN   z(named_curve curve parameters not allowedzUnexpected data after OIDrO   z%explicit curve parameters not allowedz,Unexpected data after ECParameters structurer   z!Unknown parameter encoding formatNz#Characteristic 2 curves unsupportedzUnknown field type: {0}z:Unexpected data after ECParameters.fieldID.Prime-p element)rM   
compressedZhybridT)valid_encodingsr=   r<   unknown)setallrQ   r   r   Zis_sequenceUnexpectedDERZremove_objectr   Zremove_sequenceZremove_integerZremove_octet_stringr,   r   r+   formatr   r   ZCurveFpZPointJacobi
from_bytesr   r   )datarc   rA   emptyseqrY   restrZ   r;   Z
base_bytesr=   rU   _Z
field_typeprimeZcurve_a_bytesZcurve_b_bytesZcurve_aZcurve_bZcurve_fpr[   Z	tmp_curver_   r5   r5   r6   from_der   s|    






zCurve.from_derc                 C   sL   t st|tr| }|d}|dkr2td| t||d |S )am  Decode the curve parameters from PEM file.

        :param str string: the text string to decode the parameters from
        :param valid_encodings: set of names of allowed encodings, by default
            all (set by passing ``None``), supported ones are ``named_curve``
            and ``explicit``
        :type valid_encodings: :term:`set-like object`
        s   -----BEGIN EC PARAMETERS-----z"EC PARAMETERS PEM header not foundN)	r   r>   strencodefindr   rg   rp   Zunpem)clsstringrc   Zec_param_indexr5   r5   r6   from_pem  s    


zCurve.from_pem)N)NrM   )NrM   )N)N)r2   r3   r4   rD   rI   rJ   rL   r\   r]   staticmethodrp   classmethodrw   r5   r5   r5   r6   r   9   s   

<
[r   r   )r         r      Z	secp112r1r   )r   rz   r{   r   r7   Z	secp112r2r   )r   rz   r{   r      Z	secp128r1r   )r   rz   r{   r   r8   Z	secp160r1r   )r   r/   r0   r1   rz   r   r   Z
prime192v1r   )r   rz   r{   r   !   Z	secp224r1r   )r   r/   r0   r1   rz   r   r7   Z
prime256v1r   )r   rz   r{   r   "   Z	secp384r1r   )r   rz   r{   r   #   Z	secp521r1r   )r   rz   r{   r   
   Z	secp256k1r   )
r   rz   $   rz   rz   r/   r8   r   r   r   ZbrainpoolP160r1r   )
r   rz   r   rz   rz   r/   r8   r   r   r/   ZbrainpoolP160t1r   )
r   rz   r   rz   rz   r/   r8   r   r   rz   ZbrainpoolP192r1r    )
r   rz   r   rz   rz   r/   r8   r   r      ZbrainpoolP192t1r!   )
r   rz   r   rz   rz   r/   r8   r   r      ZbrainpoolP224r1r"   )
r   rz   r   rz   rz   r/   r8   r   r   r|   ZbrainpoolP224t1r#   )
r   rz   r   rz   rz   r/   r8   r   r   r7   ZbrainpoolP256r1r$   )
r   rz   r   rz   rz   r/   r8   r   r   r8   ZbrainpoolP256t1r%   )
r   rz   r   rz   rz   r/   r8   r   r   	   ZbrainpoolP320r1r&   )
r   rz   r   rz   rz   r/   r8   r   r   r   ZbrainpoolP320t1r'   )
r   rz   r   rz   rz   r/   r8   r   r      ZbrainpoolP384r1r(   )
r   rz   r   rz   rz   r/   r8   r   r      ZbrainpoolP384t1r)   )
r   rz   r   rz   rz   r/   r8   r   r      ZbrainpoolP512r1r*   )
r   rz   r   rz   rz   r/   r8   r   r      ZbrainpoolP512t1r-   )r   rz   e   p   r.   )r   rz   r   q   c                 C   s:   t D ]}|j| kr|  S qtd| dd t D f dS )a9  Select a curve based on its OID

    :param tuple[int,...] oid_curve: ASN.1 Object Identifier of the
        curve to return, like ``(1, 2, 840, 10045, 3, 1, 7)`` for ``NIST256p``.

    :raises UnknownCurveError: When the oid doesn't match any of the supported
        curves

    :rtype: ~ecdsa.curves.Curve
    zDI don't know about the curve with oid %s.I only know about these: %sc                 S   s   g | ]
}|j qS r5   rK   r^   cr5   r5   r6   
<listcomp>5  ra   zfind_curve.<locals>.<listcomp>N)r   rA   r   )Z	oid_curver   r5   r5   r6   r   %  s    

r   c                 C   sJ   t D ]&}| |jks"|jr| |jkr|  S qtd| dd t D dS )a{  Select a curve based on its name.

    Returns a :py:class:`~ecdsa.curves.Curve` object with a ``name`` name.
    Note that ``name`` is case-sensitve.

    :param str name: Name of the curve to return, like ``NIST256p`` or
        ``prime256v1``

    :raises UnknownCurveError: When the name doesn't match any of the supported
        curves

    :rtype: ~ecdsa.curves.Curve
    z9Curve with name {0!r} unknown, only curves supported: {1}c                 S   s   g | ]
}|j qS r5   rK   r   r5   r5   r6   r   L  ra   z!curve_by_name.<locals>.<listcomp>N)r   r9   r:   r   rh   )r9   r   r5   r5   r6   r   9  s    
r   N)g
__future__r   sixr    r   r   r   r   utilr	   r
   r   Z_compatr   r   __all__r+   r,   	Exceptionr   r   Zcurve_112r1Zgenerator_112r1r   Zcurve_112r2Zgenerator_112r2r   Zcurve_128r1Zgenerator_128r1r   Zcurve_160r1Zgenerator_160r1r   Z	curve_192Zgenerator_192r   Z	curve_224Zgenerator_224r   Z	curve_256Zgenerator_256r   Z	curve_384Zgenerator_384r   Z	curve_521Zgenerator_521r   Zcurve_secp256k1Zgenerator_secp256k1r   Zcurve_brainpoolp160r1Zgenerator_brainpoolp160r1r   Zcurve_brainpoolp160t1Zgenerator_brainpoolp160t1r   Zcurve_brainpoolp192r1Zgenerator_brainpoolp192r1r   Zcurve_brainpoolp192t1Zgenerator_brainpoolp192t1r    Zcurve_brainpoolp224r1Zgenerator_brainpoolp224r1r!   Zcurve_brainpoolp224t1Zgenerator_brainpoolp224t1r"   Zcurve_brainpoolp256r1Zgenerator_brainpoolp256r1r#   Zcurve_brainpoolp256t1Zgenerator_brainpoolp256t1r$   Zcurve_brainpoolp320r1Zgenerator_brainpoolp320r1r%   Zcurve_brainpoolp320t1Zgenerator_brainpoolp320t1r&   Zcurve_brainpoolp384r1Zgenerator_brainpoolp384r1r'   Zcurve_brainpoolp384t1Zgenerator_brainpoolp384t1r(   Zcurve_brainpoolp512r1Zgenerator_brainpoolp512r1r)   Zcurve_brainpoolp512t1Zgenerator_brainpoolp512t1r*   Zcurve_ed25519Zgenerator_ed25519r-   Zcurve_ed448Zgenerator_ed448r.   r   r   r   r5   r5   r5   r6   <module>   s  & e			
																				
