
    5i                        S SK Jr  S SKrS SKrS SKrS SKJrJrJrJ	r	J
r
  S SKJr  S SKJrJrJrJr  S SKJrJrJr  S SKJrJr  S SKJr  S	S
KJrJrJr  S	SKJr    " S S\!5      r" " S S\#5      r$S r%S r&S'S jr'S r(S(S jr)S r*S r+S r,S r-S r.S r/S r0S r1S r2S'S jr3\4S:X  a  S SK5r5S r6\S!   Rn                  Rq                  5       r9S"r:\5Rj                  " 5       r;\<" \:5       H  r=\9\6-  r>M
     \?" S#\5Rj                  " 5       \;-
  \:-  S$-  S%5        \5Rj                  " 5       r;\<" \:5       H  r=\>\6-  r>M
     \?" S&\5Rj                  " 5       \;-
  \:-  S$-  S%5        gg))    )print_functionN)bordtobytestostrbchr	is_string)Integer)DerObjectIdDerOctetStringDerSequenceDerBitString)_expand_subject_public_key_info_create_subject_public_key_info _extract_subject_public_key_info)SHA512SHAKE256)get_random_bytes   )EccPoint	EccXPoint_curves)CurveIDc                       \ rS rSrSrg)UnsupportedEccFeature7    N)__name__
__module____qualname____firstlineno____static_attributes__r       b/var/www/html/BTCUSD/btcusdt_trading_app/venv/lib/python3.13/site-packages/Crypto/PublicKey/ECC.pyr   r   7   s    r"   r   c                       \ rS rSrSrS rS rS rS rS r	S r
\S	 5       r\S
 5       r\S 5       rS rS rS rS rS rSS jrS rS rS rS rS rS rS rSrg)EccKey;   a  Class defining an ECC key.
Do not instantiate directly.
Use :func:`generate`, :func:`construct` or :func:`import_key` instead.

:ivar curve: The **canonical** name of the curve as defined in the `ECC table`_.
:vartype curve: string

:ivar pointQ: an ECC point representing the public component.
:vartype pointQ: :class:`EccPoint` or :class:`EccXPoint`

:ivar d: A scalar that represents the private component
         in NIST P curves. It is smaller than the
         order of the generator point.
:vartype d: integer

:ivar seed: A seed that represents the private component
            in Ed22519 (32 bytes), Curve25519 (32 bytes),
            Curve448 (56 bytes), Ed448 (57 bytes).
:vartype seed: bytes
c                 	   [        U5      nUR                  SS5      nUR                  SS5      U l        UR                  SS5      U l        UR                  SS5      U l        Uc'  U R                  (       a  U R                  R
                  nU(       a  [        S[        U5      -   5      eU[        ;  a  [        SU-  5      e[        U   U l
        U R                  R                  U l        [        U R                  SL5      [        U R                  SL5      -   nUS:X  a  U R                  c  [        S	5      egUS
:X  a  [        S5      eU R                  R                  [        R                  :X  a  U R                  b  [        S5      e[!        U R                  5      S:w  a  [        S5      e["        R$                  " U R                  5      R'                  5       nUSS U l        [+        USS 5      nUS==   S-  ss'   US   S-  S-  US'   [,        R.                  " USS9U l        gU R                  R                  [        R0                  :X  a  U R                  b  [        S5      e[!        U R                  5      S:w  a  [        S5      e[2        R$                  " U R                  5      R5                  S5      nUSS U l        [+        USS 5      nUS==   S-  ss'   US==   S-  ss'   SUS'   [,        R.                  " USS9U l        gU R                  R                  [        R6                  :X  a  U R                  b  [        S5      e[!        U R                  5      S:w  a  [        S5      e[+        U R                  5      nUS==   S-  ss'   US   S-  S-  US'   [,        R.                  " USS9U l        gU R                  R                  [        R8                  :X  a  U R                  b  [        S5      e[!        U R                  5      S:w  a  [        S5      e[+        U R                  5      nUS==   S-  ss'   US==   S-  ss'   [,        R.                  " USS9U l        gU R                  b  [        S5      e[-        U R                  5      U l        SU R                  s=::  a  U R                  R:                  :  d  O  [        S 5      eg)!a(  Create a new ECC key

Keywords:
  curve : string
    The name of the curve.
  d : integer
    Mandatory for a private key one NIST P curves.
    It must be in the range ``[1..order-1]``.
  seed : bytes
    Mandatory for a private key on Ed25519 (32 bytes),
    Curve25519 (32 bytes), Curve448 (56 bytes) or Ed448 (57 bytes).
  point : EccPoint or EccXPoint
    Mandatory for a public key. If provided for a private key,
    the implementation will NOT check whether it matches ``d``.

Only one parameter among ``d``, ``seed`` or ``point`` may be used.
curveNdseedpointUnknown parameters: zUnsupported curve (%s)r   zGAt lest one between parameters 'point', 'd' or 'seed' must be specified   z,Parameters d and seed are mutually exclusivez/Parameter d can only be used with NIST P curves    z0Parameter seed must be 32 bytes long for Ed25519         @   little	byteorder9   z.Parameter seed must be 57 bytes long for Ed448r      r      8   z3Parameter seed must be 32 bytes long for Curve25519z1Parameter seed must be 56 bytes long for Curve448z2Parameter 'seed' cannot be used with NIST P-curvesr   z;Parameter d must be an integer smaller than the curve order)dictpop_d_seed_pointr(   	TypeErrorstrr   
ValueError_curve	canonicalintid_CurveIDED25519lenr   newdigest_prefix	bytearrayr	   
from_bytesED448r   read
CURVE25519CURVE448order)selfkwargskwargs_
curve_namecount	seed_hashtmps          r#   __init__EccKey.__init__Q   s   & v,[[$/
++c4([[.
kk'40$++**J2S\ABBW$5
BCCj)[[**
DGG4'(3tzz/E+FFA:{{" !jkkA:KLL
 ;;>>X---ww" !RSS4::"$ !STT

4::.557I$RS>DLIcrN+CFdNF2w~-CG((ADG[[^^x~~-ww" !RSS4::"$ !QRR TZZ055c:I$RS>DLIcrN+CFdNFGtOGCG((ADG[[^^x222ww" !RSS4::"$ !VWWDJJ'CFdNF2w~-CG((ADG[[^^x000ww" !RSS4::"$ !TUUDJJ'CFdNFGtOG((ADG zz% !UVVdgg&DG3$++"3"33 !^__ 4r"   c                     [        U[        5      (       d  gUR                  5       U R                  5       :w  a  gUR                  U R                  :H  $ )NF)
isinstancer%   has_privatepointQ)rT   others     r#   __eq__EccKey.__eq__   sB    %(($"2"2"44||t{{**r"   c                 4   U R                  5       (       aa  U R                  R                  (       a-  S[        [        R
                  " U R                  5      5      -  nOS[        U R                  5      -  nOSnU R                  R                  [        R                  [        R                  4;   a3  U R                  R                  nSU R                  R                  X!4-  nU$ U R                  R                   u  p$SU R                  R                  X$U4-  nU$ )Nz	, seed=%sz, d=%d z EccKey(curve='%s', point_x=%d%s)z,EccKey(curve='%s', point_x=%d, point_y=%d%s))r_   rC   
is_edwardsr   binasciihexlifyr>   rE   r=   rF   rG   rQ   rR   r`   xrD   xy)rT   extrari   resultys        r#   __repr__EccKey.__repr__   s    {{%%#eH,<,<TZZ,H&II 3tww</E;;>>h11&//1 1A74;;;P;PRS:[[F  ;;>>DACt{{G\G\^_diFjjFr"   c                     U R                   SL$ )zJ``True`` if this key can be used for making signatures or decrypting data.N)r=   rT   s    r#   r_   EccKey.has_private   s     wwd""r"   c                 X   SUs=:  a  U R                   R                  :  d   e   eU R                   R                  n[        R                  " SUS9nU R                  U-  nXB-  R                  U5      nU R                   R                  U-  R                  U-  nXdU-  XW-  -   -  U-  nXx4$ )Nr   r   )min_inclusivemax_exclusive)rC   rS   r	   random_ranger=   inverseGri   )	rT   zkrS   blindblind_dinv_blind_krss	            r#   _signEccKey._sign   s    1(t{{(((((((!!$$138: ''E/y))%0[[]]Q!!E)19w{23e;vr"   c                     U R                   R                  nUS   R                  U5      nU R                   R                  XA-  U-  -  nU R                  XBS   -  U-  -  nXV-   R
                  US   :H  $ )Nr   r   )rC   rS   rw   rx   r`   ri   )rT   ry   rsrS   sinvpoint1point2s          r#   _verifyEccKey._verify   sl    !!!u}}U#48u"451 67""be++r"   c                 Z    U R                  5       (       d  [        S5      eU R                  $ NzThis is not a private ECC key)r_   rB   r=   rq   s    r#   r)   EccKey.d   s&    !!<==wwr"   c                 Z    U R                  5       (       d  [        S5      eU R                  $ r   )r_   rB   r>   rq   s    r#   r*   EccKey.seed   s'    !!<==zzr"   c                     U R                   c(  U R                  R                  U R                  -  U l         U R                   $ N)r?   rC   rx   r=   rq   s    r#   r`   EccKey.pointQ   s.    ;;++--$''1DK{{r"   c                 R    [        U R                  R                  U R                  S9$ )zFA matching ECC public key.

Returns:
    a new :class:`EccKey` object
)r(   r+   )r%   rC   rD   r`   rq   s    r#   
public_keyEccKey.public_key   s     DKK11EEr"   c                    U R                   R                  (       d  [        S5      eU R                  R	                  5       nU(       aX  U R                  R
                  R                  5       (       a  SnOSnUU R                  R                  R                  U5      -   nU$ SU R                  R                  R                  U5      -   U R                  R
                  R                  U5      -   nU$ )Nz/SEC1 format is only supported for NIST P curves         )	rC   is_weierstrassrB   r`   size_in_bytesrm   is_oddri   to_bytes)rT   compressmodulus_bytes
first_byter   s        r#   _export_SEC1EccKey._export_SEC1  s    {{))NOO 113{{}}##%%$
$
$++--00?@J  "++--00?@++--00?@J r"   c                    U R                   R                  u  pU R                  R                  [        R
                  :X  a+  [        UR                  SSS95      nUS-  S-  US   -  US'   OXU R                  R                  [        R                  :X  a%  [        UR                  SSS95      nUS-  S-  US'   O[        S	5      e[        U5      $ )
Nr.   r3   r4   r      r0   r6   r:   zNot an EdDSA key to export)r`   rj   rC   rF   rG   rH   rM   r   rO   rB   bytes)rT   ri   rm   rl   s       r#   _export_eddsa_publicEccKey._export_eddsa_public   s    {{~~;;>>X---qzz"zABFq5Q,&*4F2J[[^^x~~-qzz"zABFa%AF2J9::V}r"   c                     U R                   R                  (       d  [        S5      eU R                  R                  nU R                  R                  5       n[        UR                  USS95      n[        U5      $ )NzNot a Montgomery key to exportr3   r4   )	rC   is_montgomeryrB   r`   ri   r   rM   r   r   )rT   ri   
field_sizerl   s       r#   _export_montgomery_public EccKey._export_montgomery_public,  s[    {{((=>>KKMM[[..0
1::jH:EFV}r"   c                    U R                   R                  (       a)  U R                   R                  nU R                  5       nS nOvU R                   R                  (       a)  U R                   R                  nU R                  5       nS nO2SnU R                  U5      n[        U R                   R                  5      n[        UUU5      $ )N1.2.840.10045.2.1)	rC   rf   oidr   r   r   r   r
   r   )rT   r   r   r   paramss        r#   _export_subjectPublicKeyInfo#EccKey._export_subjectPublicKeyInfo4  s    ;;!!++//C224JF[[&&++//C779JF%C**84J 1F.s/9/57 	7r"   c                    U R                  5       (       d   eU R                  R                  5       nSU R                  R                  R	                  U5      -   U R                  R
                  R	                  U5      -   nS[        U R                  R	                  U5      5      [        U R                  R                  SS9[        USS9/nU(       d  US	 [        U5      R                  5       $ )Nr   r   r   explicitr-   )r_   r`   r   ri   r   rm   r   r)   r
   rC   r   r   r   encode)rT   include_ec_paramsr   r   seqs        r#   _export_rfc5915_private_der"EccKey._export_rfc5915_private_derF  s    !!!! 113kkmm,,];<kkmm,,];<
 dffoom<=4;;??Q7J35
 !A3&&((r"   c                 x   SSK Jn  UR                  SS 5      b  SU;  a  [        S5      eU R                  b<  U R
                  R                  n[        U R                  5      R                  5       nS nO0SnU R                  SS9n[        U R
                  R                  5      nUR                  " UU4S	U0UD6nU$ )
Nr   PKCS8
passphrase
protectionz3At least the 'protection' parameter must be presentr   F)r   
key_params)	Crypto.IOr   getrB   r>   rC   r   r   r   r   r
   wrap)rT   rU   r   r   private_keyr   rl   s          r#   _export_pkcs8EccKey._export_pkcs8a  s    #::lD)5,f:TRSS::!++//C(4;;=KF%C::U:SK 1FK&'-& %& r"   c                 T    SSK Jn  U R                  U5      nUR                  US5      $ )Nr   PEMz
PUBLIC KEY)r   r   r   r   )rT   r   r   encoded_ders       r#   _export_public_pemEccKey._export_public_pemv  s&    !77Azz+|44r"   c                 X    SSK Jn  U R                  5       nUR                  " USU40 UD6$ )Nr   r   zEC PRIVATE KEY)r   r   r   r   rT   r   rU   r   r   s        r#   _export_private_pemEccKey._export_private_pem|  s,    !668zz+'7NvNNr"   c                 R    SSK Jn  U R                  5       nUR                  US5      $ )Nr   r   zPRIVATE KEY)r   r   r   r   )rT   r   r   s      r#   (_export_private_clear_pkcs8_in_clear_pem/EccKey._export_private_clear_pkcs8_in_clear_pem  s$    !((*zz+}55r"   c                     SSK Jn  U(       d   eSU;  a  [        S5      eU R                  " SSU0UD6nUR	                  US5      $ )Nr   r   r   z5At least the 'protection' parameter should be presentr   zENCRYPTED PRIVATE KEYr   )r   r   rB   r   r   r   s        r#   ,_export_private_encrypted_pkcs8_in_clear_pem3EccKey._export_private_encrypted_pkcs8_in_clear_pem  sK    !zv%TUU((IJI&Izz+'>??r"   c                    U R                  5       (       a  [        S5      eU R                  R                  nUc  [        SU R                  -  5      eUS:X  a'  U R                  5       n[        U5      [        U5      4nOU R                  R                  5       nU(       aY  SU R                  R                  R                  5       -   n[        U5      U R                  R                  R                  U5      -   nONSU R                  R                  R                  U5      -   U R                  R                  R                  U5      -   nUR                  S5      S   n[        U5      [        U5      U4nSR                  U Vs/ s H&  n[         R"                  " S[%        U5      5      U-   PM(     sn5      n	US	-   ['        [(        R*                  " U	5      5      -   $ s  snf )
Nz"Cannot export OpenSSH private keysz Cannot export %s keys as OpenSSHssh-ed25519r-   r   -r"   >I )r_   rB   rC   opensshr(   r   r   r`   r   rm   r   r   ri   r   splitjoinstructpackrI   r   rg   
b2a_base64)
rT   r   descr   compsr   r   middleri   blobs
             r#   _export_opensshEccKey._export_openssh  s   ABB{{""<?$**LMM]"224JT]GJ$78E KK557M!5!5!77
":."kkmm44]CD
 &"kkmm44]CD"kkmm44]CD
 ZZ_Q'FT]GFOZ@ExxF1T3q62Q6FGczE("5"5d";<<< Gs   -Gc                    UR                  5       nUR                  S5      nUS;  a  [        SU-  5      eUR                  SS5      nU R                  5       (       GaU  UR                  SS5      n[	        U5      (       a  [        U5      nU(       d  [        S5      eUR                  S	S
5      nUSL a]  U R                  R                  (       a  [        S5      eU R                  R                  (       a  [        S5      eSU;   a  [        S5      eUS:X  aD  U(       a*  U(       a  U R                  " U40 UD6$ U R                  5       $ U R                  " U40 UD6$ US:X  aD  U(       a  U(       d  [        S5      eU(       a  U R                  " SSU0UD6$ U R                  5       $ [        SU-  5      eU(       a  [        SU-  5      eUS:X  a  U R                  U5      $ US:X  a  U R                  U5      $ US:X  a  U R!                  U5      $ US:X  ag  U R                  R                  (       a  U R#                  5       $ U R                  R                  (       a  U R%                  5       $ U R!                  U5      $ U R'                  U5      $ )a  Export this ECC key.

Args:
  format (string):
    The output format:

    - ``'DER'``. The key will be encoded in ASN.1 DER format (binary).
      For a public key, the ASN.1 ``subjectPublicKeyInfo`` structure
      defined in `RFC5480`_ will be used.
      For a private key, the ASN.1 ``ECPrivateKey`` structure defined
      in `RFC5915`_ is used instead (possibly within a PKCS#8 envelope,
      see the ``use_pkcs8`` flag below).
    - ``'PEM'``. The key will be encoded in a PEM_ envelope (ASCII).
    - ``'OpenSSH'``. The key will be encoded in the OpenSSH_ format
      (ASCII, public keys only).
    - ``'SEC1'``. The public key (i.e., the EC point) will be encoded
      into ``bytes`` according to Section 2.3.3 of `SEC1`_
      (which is a subset of the older X9.62 ITU standard).
      Only for NIST P-curves.
    - ``'raw'``. The public key will be encoded as ``bytes``,
      without any metadata.

      * For NIST P-curves: equivalent to ``'SEC1'``.
      * For Ed25519 and Ed448: ``bytes`` in the format
        defined in `RFC8032`_.
      * For Curve25519 and Curve448: ``bytes`` in the format
        defined in `RFC7748`_.

  passphrase (bytes or string):
    (*Private keys only*) The passphrase to protect the
    private key.

  use_pkcs8 (boolean):
    (*Private keys only*)
    If ``True`` (default and recommended), the `PKCS#8`_ representation
    will be used.
    It must be ``True`` for Ed25519, Ed448, Curve25519, and Curve448.

    If ``False`` and a passphrase is present, the obsolete PEM
    encryption will be used.

  protection (string):
    When a private key is exported with password-protection
    and PKCS#8 (both ``DER`` and ``PEM`` formats), this parameter MUST be
    present,
    For all possible protection schemes,
    refer to :ref:`the encryption parameters of PKCS#8<enc_params>`.
    It is recommended to use ``'PBKDF2WithHMAC-SHA512AndAES128-CBC'``.

  compress (boolean):
    If ``True``, the method returns a more compact representation
    of the public key, with the X-coordinate only.

    If ``False`` (default), the method returns the full public key.

    This parameter is ignored for Ed25519/Ed448/Curve25519/Curve448,
    as compression is mandatory.

  prot_params (dict):
    When a private key is exported with password-protection
    and PKCS#8 (both ``DER`` and ``PEM`` formats), this dictionary
    contains the  parameters to use to derive the encryption key
    from the passphrase.
    For all possible values,
    refer to :ref:`the encryption parameters of PKCS#8<enc_params>`.
    The recommendation is to use ``{'iteration_count':21000}`` for PBKDF2,
    and ``{'iteration_count':131072}`` for scrypt.

.. warning::
    If you don't provide a passphrase, the private key will be
    exported in the clear!

.. note::
    When exporting a private key with password-protection and `PKCS#8`_
    (both ``DER`` and ``PEM`` formats), any extra parameters
    to ``export_key()`` will be passed to :mod:`Crypto.IO.PKCS8`.

.. _PEM:        http://www.ietf.org/rfc/rfc1421.txt
.. _`PEM encryption`: http://www.ietf.org/rfc/rfc1423.txt
.. _OpenSSH:    http://www.openssh.com/txt/rfc5656.txt
.. _RFC5480:    https://tools.ietf.org/html/rfc5480
.. _SEC1:       https://www.secg.org/sec1-v2.pdf
.. _RFC7748:    https://tools.ietf.org/html/rfc7748

Returns:
    A multi-line string (for ``'PEM'`` and ``'OpenSSH'``) or
    ``bytes`` (for ``'DER'``, ``'SEC1'``, and ``'raw'``) with the encoded key.
format)r   DEROpenSSHSEC1rawzUnknown format '%s'r   Fr   NzEmpty passphrase	use_pkcs8Tz%'pkcs8' must be True for EdDSA curvesz#'pkcs8' must be True for Curve25519r   z)'protection' is only supported for PKCS#8r   r   z8Private keys can only be encrpyted with DER using PKCS#8z2Private keys cannot be exported in the '%s' formatzUnexpected parameters: '%s'r   r   r   )copyr<   rB   r_   r   r   rC   rf   r   r   r   r   r   r   r   r   r   r   r   r   )rT   rU   args
ext_formatr   r   r   s          r#   
export_keyEccKey.export_key  sX   t {{}XXh'
EE2Z?@@88J.,5J$$$Z0
!$%788d3IE!;;))$%LMM;;,,$%JKK4'$%PQQU"!#PPQ[d_cdd#LLNN33JG$GGu$i$%_``--LLtLL;;==  "68B"C D D  !>!EFFU"..x88u$88BBv%((22u$;;))4466[[..99;;,,X66++H55r"   )rC   r=   r?   rL   r>   r(   N)T)r   r   r   r    __doc__r[   rb   rn   r_   r   r   propertyr)   r*   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r"   r#   r%   r%   ;   s    *``D+"#,  
  
  
F8
7$)6*5O6@=:U6r"   r%   c                  <   U R                  S5      n[        U   nU R                  S[        5      nU (       a  [        S[	        U 5      -   5      e[        U   R
                  [        R                  :X  a  U" S5      n[        XS9nU$ [        U   R
                  [        R                  :X  a  U" S5      n[        XS9nU$ [        U   R
                  [        R                  :X  a5  U" S5      n[        XS9n[        U   R                  UR                  5        U$ [        U   R
                  [        R                  :X  a5  U" S5      n[        XS9n[        U   R                  UR                  5        U$ [        R                  " SUR                   US	9n[        XS
9nU$ )a  Generate a new private key on the given curve.

Args:

  curve (string):
    Mandatory. It must be a curve name defined in the `ECC table`_.

  randfunc (callable):
    Optional. The RNG to read randomness from.
    If ``None``, :func:`Crypto.Random.get_random_bytes` is used.
r(   randfuncr,   r.   r(   r*   r6   r:   r   )rt   ru   r   )r(   r)   )r<   r   r   r@   rA   rF   rG   rH   r%   rO   rQ   validater`   rR   r	   rv   rS   )rU   rW   r(   r   r*   new_keyr)   s          r#   generater   F  sr    G$JJEzz*&67H.V<==z!1!11|z5$ N# 
			8>>	1|z5 N 
			8#6#6	6|z5
$$W^^4 N 
			8#4#4	4|z5
$$W^^4 N   q/4{{*24 z/Nr"   c                     U S   n[         U   nU R                  SS5      nU R                  SS5      nSU ;   a  [        S5      eUR                  [        R
                  :X  a9  Ub  [        X15      U S'   [        S0 U D6nUR                  UR                  5        U$ UR                  [        R                  :X  a9  Ub  [        X15      U S'   [        S0 U D6nUR                  UR                  5        U$ SX44;  a  [        X4U5      U S'   [        S0 U D6nUR                  5       (       a;  SU ;   a5  UR                  UR                  -  nUR                  X44:w  a  [!        S5      eU$ )	ay  Build a new ECC key (private or public) starting
from some base components.

In most cases, you will already have an existing key
which you can read in with :func:`import_key` instead
of this function.

Args:
  curve (string):
    Mandatory. The name of the elliptic curve, as defined in the `ECC table`_.

  d (integer):
    Mandatory for a private key and a NIST P-curve (e.g., P-256).
    It must be an integer in the range ``[1..order-1]``.

  seed (bytes):
    Mandatory for a private key and curves Ed25519 (32 bytes),
    Curve25519 (32 bytes), Curve448 (56 bytes) and Ed448 (57 bytes).

  point_x (integer):
    The X coordinate (affine) of the ECC point.
    Mandatory for a public key.

  point_y (integer):
    The Y coordinate (affine) of the ECC point.
    Mandatory for a public key,
    except for Curve25519 and Curve448.

Returns:
  :class:`EccKey` : a new ECC key object
r(   point_xNpoint_yr+   zUnknown keyword: pointz(Private and public ECC keys do not matchr   )r   r<   r@   rF   rG   rQ   r   r%   r   r`   rR   r   r_   rx   r)   rj   rB   )rU   rW   r(   r   r   r   pub_keys          r#   	constructr   p  sV   B JJEjjD)GjjD)G&011xx8&&&'<F7O"6"w~~&, N) 
X&&	&'<F7O"6"w~~& N ))&wDF7O"6"   W%6gg		)Gzzg// !KLLNr"   c                 ~   [         R                  " 5        H%  u  p4U(       a  UR                  U:X  a    O.X#:X  d  M%    O%   U(       a  [        SU-  5      e[        SU-  5      eUR                  R                  5       n[        U S   5      nUS:X  aX  [        U 5      SSU-  -   :w  a  [        S5      e[        R                  " U SUS-    5      n[        R                  " XS-   S 5      nOUS	;   a  [        U 5      SU-   :w  a  [        S5      e[        R                  " U SS 5      nUS
-  US
-  -
  UR                  -   R                  UR                  5      nUS:X  a$  UR                  5       (       a  UR                  U-
  nUS
:X  a$  UR                  5       (       a  UR                  U-
  nO[        S5      e[        X7US9$ )zConvert an encoded EC point into an EccKey object

ec_point: byte string with the EC point (SEC1-encoded)
curve_oid: string with the name the curve
curve_name: string with the OID of the curve

Either curve_id or curve_name must be specified

Unsupported ECC curve (OID: %s)zUnsupported ECC curve (%s)r      r   r-   zIncorrect EC point lengthNr-      r  zIncorrect EC point encodingr(   r   r   )r   itemsr   r   pr   r   rI   rB   r	   rN   bsqrtr   is_evenr   )	ec_point	curve_oidrW   _curve_namer(   r   
point_typeri   rm   s	            r#   _import_public_derr    s    &mmoi/$	 . '(II(UVV'(Dz(QRR GG))+Mhqk"J Tx=Q]!223899x-/:;xa(89:	|	#x=Q./899x|,TAaCZ%''!''0!((**!A!))++!A677;1==r"   c                    [        U 5      u  p#nSnS[        4S[        4S.nS[        4S[        4S.nX%;   aB  U(       d  [        SU-  5      e [        5       R                  U5      R                  n[        X8S
9$ X&;   a/  Xb   u  pU(       a  [        SU-  5      eU
" U5      u  p[        XU	S9$ X';   a,  Xr   u  pU(       a  [        SU-  5      eU" U5      n[        XS9$ [        SU-  5      e! [
         a    [        S	5      ef = f)z4Convert a subjectPublicKeyInfo into an EccKey objectr   z1.3.132.1.12z1.3.132.1.13Ed25519Ed448z1.3.101.112z1.3.101.113
Curve25519Curve448z1.3.101.110z1.3.101.111z%Missing ECC parameters for ECC OID %szError decoding namedCurver  z(Unexpected ECC parameters for ECC OID %s)r   r   r(   )r   r(   zUnsupported ECC OID: %s)r   _import_ed25519_public_key_import_ed448_public_key_import_curve25519_public_key_import_curve448_public_keyrB   r
   decodevaluer  r   r   )encodedrU   r   r
  r   nist_p_oids
eddsa_oidsxdh_oidsr  rW   import_eddsa_public_keyri   rm   import_xdh_public_keys                 r#   _import_subjectPublicKeyInfor$    sA    <GDC6K "#=>#;<J
 %&CD"$?@H
  DsJKK	:#,,V4::I
 "(@@		.8o+
 G#MNN&x0Z@@	,4M)
 G#MNN!(+55 $$=$CDD9  	:899	:s   #C- -Dc                    [        5       R                  U SS9nUS   S:w  a  [        S5      e[        5       R                  US   5      R                  nSnU[        U5      :  a?   [        SS9R                  X5   5      R                  nUb  Xb:w  a  [        S5      eUnUS-  nUc  [        S	5      e[        R                  " 5        H  u  pxUR                  U:X  d  M    O   [        S
U-  5      eUR                  R                  5       n	[        U5      U	:w  a  [        S5      eS =pU[        U5      :  a_   [        SS9R                  X5   5      R                  n[        XS9nUR                   R"                  n
UR                   R$                  nUS-  n[&        R(                  " U5      n[+        X~XS9$ ! [         a     GNf = f! [         a     N=f = f)Nr-   r  r  )nr_elementsr   r   z!Incorrect ECC private key versionr-   r   zCurve mismatchzNo curve foundr   zPrivate key is too smallr  )r(   r)   r   r   )r   r  rB   r   payloadrI   r
   r  r   r  r   r   r  r   r   r  r`   ri   rm   r	   rN   r   )r  r   r  ec_private_keyscalar_bytesnext_element
parametersrW   r(   r   r   r   public_key_encr   r)   s                  r#   _import_rfc5915_derr.  2  s    !]))'y)INaA<==!#**>!+<=EELL c.))	$a0778TU[[J$)@ !122"IAL )**$]]_
99	! - $$E	$QRRGG))+M
<M)344 Gc.))	)15<<^=YZ``N+NPJ ''))G ''))GAL 	<(A:GMM;  		2  		s%   %>F4 6AG 4
GG
GGc                    SSK Jn  UR                  X5      u  p4nSnSSS.nSSS	.nX6;   a/  [        5       R	                  U5      R
                  n	[        XAU	5      $ X7;   a?  Ub  [        S
5      eS n	[        5       R	                  U5      R                  n
[        Xs   U
S9$ X8;   aF  X   nUb  [        SU-  5      eS n	[        5       R	                  U5      R                  n
[        X   U
S9$ [        SU-  5      e)Nr   r   r  r  r  r  r  r  r  z.EdDSA ECC private key must not have parametersr   z+%s ECC private key must not have parametersz!Unsupported ECC purpose (OID: %s))r   r   unwrapr
   r  r  r.  rB   r   r(  r   r   )r  r   r   algo_oidr   r   r  r   r!  r  r*   rW   s               r#   _import_pkcs8r2  m  s   $)LL$E!H6K !J
 $!H
 M((066	";IFF		MNN	&&{3;;z3$??		'
J'( ) )	&&{3;;x1==#$G($RSSr"   c                 .    [        U 5      n[        U5      $ r   )r   r$  )r  rU   sp_infos      r#   _import_x509_certr5    s    .w7G'00r"   c                     [        X5      $ ! [         a  nUeS nAf[        [        [        4 a     Of = f [        X5      $ ! [         a  nUeS nAf[        [        [        4 a     Of = f [        X5      $ ! [         a  nUeS nAf[        [        [        4 a     Of = f [        X5      $ ! [         a  nUeS nAf[        [        [        4 a     Of = f[        S5      e)NzNot an ECC DER key)r$  r   rB   r@   
IndexErrorr5  r.  r2  )r  r   errs      r#   _import_derr9    s    +G@@  		:.  55  		:. "777  		:. W11  		:.  )
**sm   
 
444
A 
A*AA*)A*.
A9 9
B BB B $
B/ /
C9B;;CCc                    U R                  S5      n[        U5      S;  a  [        S5      e [        R                  " US   5      n/ n[        U5      S:  aM  [
        R                  " SUS S 5      S   nUR                  USSU-    5        USU-   S  n[        U5      S:  a  MM  US   US   :w  a  [        S5      eUS   R                  S	5      (       a  [        R                  " 5        Hh  u  pVUR                  c  M  UR                  R                  S
5      (       d  M6  [        UR                  R                  S5      S   5      nUS   U:X  d  Mh    O   [        SW-   5      e[        US   UR                  S9nU$ US   S:X  a  [        US   5      u  p[!        SXS9nU$ [        SUS   -   5      e! ["        [$        [        R&                  4 a    [        SUS   -   5      ef = f)N    r  zNot an openssh public keyr   r  r   r   zMismatch in openssh public key   ecdsa-sha2-
ecdsa-sha2r   r-   zUnsupported ECC curve: r     ssh-ed25519r  r  zUnsupported SSH key type: zError parsing SSH key type: )r   rI   rB   rg   
a2b_base64r   unpackappend
startswithr   r  r   r   r  r   r  r   r7  r@   Error)r  parts	keystringkeypartslkrW   r(   r   ecc_keyri   rm   s              r#   _import_openssh_publicrI    s   MM$E
5z455$D''a1	)nq tYr]3A6BOOIaB/0!!b&'*I )nq 
 8x{"=>> 8~..%,]]_!
==(}}//== !4!4S!9!!<=A;&( &5 !!:V!CDD(!		JG N 1X'-hqk:DAiFG N 9E!HDEE	8>>2 D7%(BCCDs%   A5F2 $B)F2 )F2 <#F2 !F2 21G#c                    SSK JnJnJnJn  U" X5      u  pgSS[
        S40nUR                  S5      (       a  U" U5      u  pU	[        ;  a  [        SU	-  5      e[        U	   n
U
R                  S-   S	-  nU" U5      u  p[        US
   5      S:w  a  [        S5      e[        U5      SU-  S-   :w  a  [        S5      e[        R                  " USSU-    5      n[        R                  " USU-   S  5      nU" U5      u  p[        R                  " U5      nUU	S.nOEXh;   a2  X   u  nnnU" U5      u  pU" U5      u  pU" U5      u  nnUS U nUUS.nO[        SU-   5      eU" U5      u  nnU" U5        [        SXS.UD6$ )Nr   )import_openssh_private_generic
read_bytesread_stringcheck_paddingr   r  r.   r=  zUnsupported ECC curve %sr      r   r  z/Only uncompressed OpenSSH EC keys are supportedr-   zIncorrect public key length)r)   r(   )r*   r(   zUnsupport SSH agent key type:)r   r   r   )_opensshrK  rL  rM  rN  r  rB  r   r   modulus_bitsr   rB   rI   r	   rN   r   )datapasswordrK  rL  rM  rN  key_type	decrypted
eddsa_keysecdsa_curve_namer(   r   r   r   r   r   r)   r   rW   r"  seed_lenprivate_public_keyr*   _paddeds                            r#   _import_openssh_private_eccr\    s   C C 9HH 		#=rBJ
 <((&1)&<#7*'(BEU(UVV()++a/A5 *9 5

1!#NOOz?a-/!33:;;$$Z!M/%BC$$Z-0@%AB!+I!6{+#34		8B8L5
+X *9 5
2:>(29(=%I!)8,488CDDI&IAv&@W@@@r"   c                    [        U 5      S:w  a  [        S5      e[        S5      nSn[        U 5      nUS   S-	  nUS==   S-  ss'   [        R                  " USS	9nXQ:  a  [        S
5      eUS:X  a  gUS-  S-
  U-  nUS-  U-  U-  S-   U-  n UR                  U5      nXh-  U-  n	[        R                  " X5      n
U
S-  U:w  a  X-
  n
X4$ ! [         a    [        S5      ef = f)aA  Import an Ed25519 ECC public key, encoded as raw bytes as described
in RFC8032_.

Args:
  encoded (bytes):
    The Ed25519 public key to import. It must be 32 bytes long.

Returns:
  x and y (integer)

Raises:
  ValueError: when the given key cannot be parsed.

.. _RFC8032: https://datatracker.ietf.org/doc/html/rfc8032
r.   z9Incorrect length. Only Ed25519 public keys are supported.l   l   x&(7Z/
;(P8 se:8
w6Rr0   r   r1   r3   r4   zInvalid Ed25519 key (y)r   r   r   r-   zInvalid Ed25519 public key)rI   rB   r	   rM   rN   rw   _tonelli_shanksr  r  r)   rm   x_lsbr   uvv_invx2r   s              r#   r  r  !  s   " 7|rTUURSAUA'AbEQJEbETME  h7G|233!|	!a1A
1*q.A		!Q&A7		!i1_))"0aKE!kG   75667s   ;C C%c                     [        U 5      S:w  a  [        S5      e[        U 5      nUS==   S-  ss'   [        R                  " USS9nU$ )a@  Import a Curve25519 ECC public key,
encoded as raw bytes as described in RFC7748_.

Args:
  encoded (bytes):
    The Curve25519 public key to import. It must be 32 bytes long.

Returns:
  x (integer)

Raises:
  ValueError: when the given key cannot be parsed.

.. _RFC7748: https://datatracker.ietf.org/doc/html/rfc7748
r.   zIncorrect Curve25519 key lengthr0   r1   r3   r4   )rI   rB   rM   r	   rN   )r  ri   r   s      r#   r  r  N  sJ    " 7|r:;;'AbETME  h7GNr"   c                 d    [        U 5      S:w  a  [        S5      e[        R                  " U SS9nU$ )a<  Import a Curve448 ECC public key,
encoded as raw bytes as described in RFC7748_.

Args:
  encoded (bytes):
    The Curve448 public key to import. It must be 56 bytes long.

Returns:
  x (integer)

Raises:
  ValueError: when the given key cannot be parsed.

.. _RFC7748: https://datatracker.ietf.org/doc/html/rfc7748
r:   zIncorrect Curve448 key lengthr3   r4   )rI   rB   r	   rN   )r  r   s     r#   r  r  j  s3    " 7|r899  H=GNr"   c                    [        U 5      S:w  a  [        S5      e[        S   R                  nUS-
  nU SS n[	        U S   5      S-	  n[
        R                  " USS	9nXQ:  a  [        S
5      eUS:X  a  gUS-  S-
  U-  nUS-  U-  U-  S-
  U-  n UR                  U5      nXh-  U-  n	[
        R                  " X5      n
U
S-  U:w  a  X-
  n
X4$ ! [         a    [        S5      ef = f)a?  Import an Ed448 ECC public key, encoded as raw bytes as described
in RFC8032_.

Args:
  encoded (bytes):
    The Ed448 public key to import. It must be 57 bytes long.

Returns:
    x and y (integer)

Raises:
  ValueError: when the given key cannot be parsed.

.. _RFC8032: https://datatracker.ietf.org/doc/html/rfc8032
r6   z7Incorrect length. Only Ed448 public keys are supported.curve448i  Nr:   r   r3   r4   zInvalid Ed448 key (y)r   r^  r-   zInvalid Ed448 public key)	rI   rB   r   r  r   r	   rN   rw   r_  r`  s              r#   r  r    s   " 7|rRSS
A	E	AA"E  h7G|011!|	!a1A
1*q.A		!Q&A5		!i1_))"0aKE!kG   53445s   ;C C&c                 *   SSK Jn  [        U 5      n Ub  [        U5      nU R                  S5      (       a,  [	        U 5      nUR                  XA5      u  pVn[        XQ5      nU$ U R                  S5      (       ae  [	        U 5      nSn	Sn
[        R                  " U	S-   U
-   S	U[        R                  S
9nUR                  XA5      u  pnU(       a  Sn [        X5      nU$ U R                  S5      (       a  [        U 5      $ [        U 5      S:  a  [        U S   5      S:X  a  [        X5      $ [        U 5      S:  a)  [        U S   5      S;   a  Uc  [        S5      e[!        XS9$ [        S5      e! [         a  nUeSnAf[         a    [        S5      ef = f)a
  Import an ECC key (public or private).

Args:
  encoded (bytes or multi-line string):
    The ECC key to import.
    The function will try to automatically detect the right format.

    Supported formats for an ECC **public** key:

    * X.509 certificate: binary (DER) or ASCII (PEM).
    * X.509 ``subjectPublicKeyInfo``: binary (DER) or ASCII (PEM).
    * SEC1_ (or X9.62), as ``bytes``. NIST P curves only.
      You must also provide the ``curve_name`` (with a value from the `ECC table`_)
    * OpenSSH line, defined in RFC5656_ and RFC8709_ (ASCII).
      This is normally the content of files like ``~/.ssh/id_ecdsa.pub``.

    Supported formats for an ECC **private** key:

    * A binary ``ECPrivateKey`` structure, as defined in `RFC5915`_ (DER).
      NIST P curves only.
    * A `PKCS#8`_ structure (or the more recent Asymmetric Key
      Package, RFC5958_): binary (DER) or ASCII (PEM).
    * `OpenSSH 6.5`_ and newer versions (ASCII).

    Private keys can be in the clear or password-protected.

    For details about the PEM encoding, see `RFC1421`_/`RFC1423`_.

  passphrase (byte string):
    The passphrase to use for decrypting a private key.
    Encryption may be applied protected at the PEM level (not recommended)
    or at the PKCS#8 level (recommended).
    This parameter is ignored if the key in input is not encrypted.

  curve_name (string):
    For a SEC1 encoding only. This is the name of the curve,
    as defined in the `ECC table`_.

.. note::

    To import EdDSA private and public keys, when encoded as raw ``bytes``, use:

    * :func:`Crypto.Signature.eddsa.import_public_key`, or
    * :func:`Crypto.Signature.eddsa.import_private_key`.

.. note::

    To import X25519/X448 private and public keys, when encoded as raw ``bytes``, use:

    * :func:`Crypto.Protocol.DH.import_x25519_public_key`
    * :func:`Crypto.Protocol.DH.import_x25519_private_key`
    * :func:`Crypto.Protocol.DH.import_x448_public_key`
    * :func:`Crypto.Protocol.DH.import_x448_private_key`

Returns:
  :class:`EccKey` : a new ECC key object

Raises:
  ValueError: when the given key cannot be parsed (possibly because
    the pass phrase is wrong).

.. _RFC1421: https://datatracker.ietf.org/doc/html/rfc1421
.. _RFC1423: https://datatracker.ietf.org/doc/html/rfc1423
.. _RFC5915: https://datatracker.ietf.org/doc/html/rfc5915
.. _RFC5656: https://datatracker.ietf.org/doc/html/rfc5656
.. _RFC8709: https://datatracker.ietf.org/doc/html/rfc8709
.. _RFC5958: https://datatracker.ietf.org/doc/html/rfc5958
.. _`PKCS#8`: https://datatracker.ietf.org/doc/html/rfc5208
.. _`OpenSSH 6.5`: https://flak.tedunangst.com/post/new-openssh-key-format-and-bcrypt-pbkdf
.. _SEC1: https://www.secg.org/sec1-v2.pdf
r   r   Ns   -----BEGIN OPENSSH PRIVATE KEYs   -----z-----BEGIN EC PARAMETERS-----z-----END EC PARAMETERS-----z.*?re   )flagsz(Invalid DER encoding inside the PEM file)r<  r>  0   r&  zNo curve name was provided)rW   zECC key format is not supported)r   r   r   rB  r   r  r\  resubDOTALLr9  r   rB   rI  rI   r   r  )r  r   rW   r   text_encodedopenssh_encodedmarkerenc_flagrl   ecparams_startecparams_endder_encodeduefs                r#   
import_keyrx    s   R gGZ(
 ;<<W~,/JJ|,P),_I			H	%	%W~ 94vvnu4|CR*$&II/ ),

<(L%XJ	I 9F
  :;;%g.. 7|aD,47// 7|aD,0BB9::!'AA
6
77) % 	I 	IGHH	Is   E- -
F7E99F__main__l   _,)N$chKf-5lk<Xk#E p256i  z	(P-256 G)i  msz(P-256 arbitrary point))NNr   )@
__future__r   rm  r   rg   Crypto.Util.py3compatr   r   r   r   r   Crypto.Math.Numbersr	   Crypto.Util.asn1r
   r   r   r   Crypto.PublicKeyr   r   r   Crypto.Hashr   r   Crypto.Randomr   r?   r   r   r   r   rG   rB   r   objectr%   r   r   r  r$  r.  r2  r5  r9  rI  r\  r  r  r  r  rx  r   timer)   rx   r   r+   rX   startrangeri   pointXprintr   r"   r#   <module>r     sw  > & 	   G G ', ,@ @ ) * 0 0 '	J 	H6V H6V'TDN5>p@EF8Nv%TP1+B+\4An*Z82)X{8| zJAFO""$EEIIKE5\ 	+		e+u4t;TBIIKE5\! 	
#diikE&9U%BT%I4P# r"   