AlkantarClanX12

Your IP : 3.143.218.180


Current Path : /opt/imunify360/venv/lib/python3.11/site-packages/Crypto/PublicKey/__pycache__/
Upload File :
Current File : //opt/imunify360/venv/lib/python3.11/site-packages/Crypto/PublicKey/__pycache__/ECC.cpython-311.pyc

�

f��f����
�ddlmZddlZddlZddlZddlmZddlmZm	Z	m
Z
mZmZddl
mZmZddlmZddlmZmZmZmZddlmZmZmZmZmZmZmZdd	lm Z m!Z!m"Z"dd
l#m$Z$m%Z%ddl&m'Z'ddl(m)Z)ed
d��Z*edd��Z+edd��Z,d�Z-edd��Z.iZ/gd�a0d�Z1e1��[1gd�a2d�Z3e3��[3gd�a4d�Z5e5��[5gd�a6d�Z7e7��[7gd�a8d�Z9e9��[9d d!ga:d"�Z;e;��[;d#d$ga<d%�Z=e=��[=Gd&�d'e>��Z?Gd(�d)e@��ZAeAe/d*jBe/d*jCd*��ZDe/d*�EeD�+��ZFe/�GeH�It`eF����[D[Fb0eAe/d,jBe/d,jCd,��ZJe/d,�EeJ�+��ZKe/�GeH�ItdeK����[J[Kb2eAe/d-jBe/d-jCd-��ZLe/d-�EeL�+��ZMe/�GeH�ItheM����[L[Mb4eAe/d.jBe/d.jCd.��ZNe/d.�EeN�+��ZOe/�GeH�ItleO����[N[Ob6eAe/d/jBe/d/jCd/��ZPe/d/�EeP�+��ZQe/�GeH�ItpeQ����[P[Qb8eAe/d!jBe/d!jCd!��ZRe/d!�EeR�+��ZSe/�GeH�ItteS����[R[Sb:eAe/d$jBe/d$jCd$��ZTe/d$�EeT�+��ZUe/�GeH�ItxeU����[T[Ub<Gd0�d1e@��ZVd2�ZWd3�ZXdFd4�ZYd5�ZZdGd6�Z[d7�Z\d8�Z]d9�Z^d:�Z_d;�Z`d<�Zad=�ZbdFd>�Zcedd?kr�ddleZed@Zfe/d-jg�h��ZidAZjeeje��Zkelej��D]ZmeiefzZn�eodBeeje��ekz
ejzdCzdD��eeje��Zkelej��D]ZmenefzZn�eodEeeje��ekz
ejzdCzdD��dSdS)H�)�print_functionN)�
namedtuple)�bord�tobytes�tostr�bchr�	is_string)�
bytes_to_long�
long_to_bytes)�Integer)�DerObjectId�DerOctetString�DerSequence�DerBitString)�load_pycryptodome_raw_lib�VoidPointer�SmartPointer�c_size_t�c_uint8_ptr�c_ulonglong�null_pointer)�_expand_subject_public_key_info�_create_subject_public_key_info� _extract_subject_public_key_info)�SHA512�SHAKE256)�get_random_bytes)�getrandbitszCrypto.PublicKey._ec_wsa
typedef void EcContext;
typedef void EcPoint;
int ec_ws_new_context(EcContext **pec_ctx,
                      const uint8_t *modulus,
                      const uint8_t *b,
                      const uint8_t *order,
                      size_t len,
                      uint64_t seed);
void ec_free_context(EcContext *ec_ctx);
int ec_ws_new_point(EcPoint **pecp,
                    const uint8_t *x,
                    const uint8_t *y,
                    size_t len,
                    const EcContext *ec_ctx);
void ec_ws_free_point(EcPoint *ecp);
int ec_ws_get_xy(uint8_t *x,
                 uint8_t *y,
                 size_t len,
                 const EcPoint *ecp);
int ec_ws_double(EcPoint *p);
int ec_ws_add(EcPoint *ecpa, EcPoint *ecpb);
int ec_ws_scalar(EcPoint *ecp,
                 const uint8_t *k,
                 size_t len,
                 uint64_t seed);
int ec_ws_clone(EcPoint **pecp2, const EcPoint *ecp);
int ec_ws_cmp(const EcPoint *ecp1, const EcPoint *ecp2);
int ec_ws_neg(EcPoint *p);
zCrypto.PublicKey._ed25519ai
typedef void Point;
int ed25519_new_point(Point **out,
                      const uint8_t x[32],
                      const uint8_t y[32],
                      size_t modsize,
                      const void *context);
int ed25519_clone(Point **P, const Point *Q);
void ed25519_free_point(Point *p);
int ed25519_cmp(const Point *p1, const Point *p2);
int ed25519_neg(Point *p);
int ed25519_get_xy(uint8_t *xb, uint8_t *yb, size_t modsize, Point *p);
int ed25519_double(Point *p);
int ed25519_add(Point *P1, const Point *P2);
int ed25519_scalar(Point *P, const uint8_t *scalar, size_t scalar_len, uint64_t seed);
zCrypto.PublicKey._ed448a*
typedef void EcContext;
typedef void PointEd448;
int ed448_new_context(EcContext **pec_ctx);
void ed448_context(EcContext *ec_ctx);
void ed448_free_context(EcContext *ec_ctx);
int ed448_new_point(PointEd448 **out,
                    const uint8_t x[56],
                    const uint8_t y[56],
                    size_t len,
                    const EcContext *context);
int ed448_clone(PointEd448 **P, const PointEd448 *Q);
void ed448_free_point(PointEd448 *p);
int ed448_cmp(const PointEd448 *p1, const PointEd448 *p2);
int ed448_neg(PointEd448 *p);
int ed448_get_xy(uint8_t *xb, uint8_t *yb, size_t len, const PointEd448 *p);
int ed448_double(PointEd448 *p);
int ed448_add(PointEd448 *P1, const PointEd448 *P2);
int ed448_scalar(PointEd448 *P, const uint8_t *scalar, size_t scalar_len, uint64_t seed);
c���|jjdkrttd|z��}nA|jjdkrttd|z��}ntt
d|z��}|S)N�Ed25519�ed25519_�Ed448�ed448_�ec_ws_)�_curve�desc�getattr�_ed25519_lib�
_ed448_lib�_ec_lib)�ecc_obj�	func_name�results   �v/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/PublicKey/ECC.py�lib_funcr/�sg���~��i�'�'���z�I�'=�>�>���	��	��	'�	'���X�	�%9�:�:�����(�Y�"6�7�7���M��_Curvez<p b order Gx Gy G modulus_bits oid context desc openssh name)�p192�
NIST P-192zP-192�
prime192v1�	secp192r1�nistp192c�T�d}d}d}d}d}t|d��}t|d��}t|d��}t��}t�|���t|��t|��t|��t
t|����ttd������}	|	rtd|	z���t|���tj
��}
tt|��t|��t|��t|��t|��dd	d
|
ddd
��}t �t$�t(|����dS)Nl
�������������l
�9��{�u��D�j�Sg9�g(Bl
1(�i�&^#a�;�������l
��+�'1t�:�_|v!a�:@m�l
H�<�^�W]�dZ{cx��W\Iq��@z#Error %d initializing P-192 context�z1.2.840.10045.3.1.1r3zecdsa-sha2-nistp192r2)rrr*�ec_ws_new_context�
address_ofrr�lenrr�ImportErrorr�get�ec_free_contextr1r�_curves�update�dict�fromkeys�
p192_names)�p�b�order�Gx�Gy�p192_modulus�p192_b�
p192_order�ec_p192_contextr-�contextr2s            r.�	init_p192rP�sn��:�A�:�A�>�E�	;�B�	;�B� ��B�'�'�L�
�1�b�
!�
!�F��u�b�)�)�J�!�m�m�O�
�
&�
&��'A�'A�'C�'C�'2�<�'@�'@�'2�6�':�':�'2�:�'>�'>�'/��L�0A�0A�'B�'B�'2�;�r�?�?�'C�'C�)�)�F��J��?�&�H�I�I�I��?�.�.�0�0�'�2I�J�J�G��'�!�*�*��!�*�*��%�.�.��"�+�+��"�+�+���'���'����D��N�N�4�=�=��T�2�2�3�3�3�3�3r0)�p224�
NIST P-224zP-224�
prime224v1�	secp224r1�nistp224c�T�d}d}d}d}d}t|d��}t|d��}t|d��}t��}t�|���t|��t|��t|��t
t|����ttd������}	|	rtd|	z���t|���tj
��}
tt|��t|��t|��t|��t|��dd	d
|
ddd
��}t �t$�t(|����dS)Nl���������?l��FeY8��w�-X"PVd�/�%�PP!-l=*�8%���(��������?l!�"X�!#BX�t�J9!'�|�%�V�A�-l4~
f�&Dv�@h�!�f�E0m9_qlM/�r9z#Error %d initializing P-224 context��z1.3.132.0.33rRzecdsa-sha2-nistp224rQ)rrr*r;r<rrr=rrr>rr?r@r1rrArBrCrD�
p224_names)rFrGrHrIrJ�p224_modulus�p224_b�
p224_order�ec_p224_contextr-rOrQs            r.�	init_p224r^�sn��B�A�B�A�F�E�	C�B�	C�B� ��B�'�'�L�
�1�b�
!�
!�F��u�b�)�)�J�!�m�m�O�
�
&�
&��'A�'A�'C�'C�'2�<�'@�'@�'2�6�':�':�'2�:�'>�'>�'/��L�0A�0A�'B�'B�'2�;�r�?�?�'C�'C�)�)�F��J��?�&�H�I�I�I��?�.�.�0�0�'�2I�J�J�G��'�!�*�*��!�*�*��%�.�.��"�+�+��"�+�+��� ���'����D��N�N�4�=�=��T�2�2�3�3�3�3�3r0)�p256�
NIST P-256zP-256�
prime256v1�	secp256r1�nistp256c�T�d}d}d}d}d}t|d��}t|d��}t|d��}t��}t�|���t|��t|��t|��t
t|����ttd������}	|	rtd|	z���t|���tj
��}
tt|��t|��t|��t|��t|��dd	d
|
ddd
��}t �t$�t(|����dS)Nl������?@��lK`�O�pq^cv
3,�e<
1�U�]>{|R�*��ZlQ%�x+�Oh�bi+}s�����@��l�B11e	%:f=K`�wr�H�7g�HK8�hkl�Q~o��]�l+f�Ug+<�)Z?�8�O�?q!�O� r9z#Error %d initializing P-256 context�z1.2.840.10045.3.1.7r`zecdsa-sha2-nistp256r_)rrr*r;r<rrr=rrr>rr?r@r1rrArBrCrD�
p256_names)rFrGrHrIrJ�p256_modulus�p256_b�
p256_order�ec_p256_contextr-rOr_s            r.�	init_p256rlsn��J�A�J�A�N�E�	K�B�	K�B� ��B�'�'�L�
�1�b�
!�
!�F��u�b�)�)�J�!�m�m�O�
�
&�
&��'A�'A�'C�'C�'2�<�'@�'@�'2�6�':�':�'2�:�'>�'>�'/��L�0A�0A�'B�'B�'2�;�r�?�?�'C�'C�)�)�F��J��?�&�H�I�I�I��?�.�.�0�0�'�2I�J�J�G��'�!�*�*��!�*�*��%�.�.��"�+�+��"�+�+���'���'����D��N�N�4�=�=��T�2�2�3�3�3�3�3r0)�p384�
NIST P-384zP-384�
prime384v1�	secp384r1�nistp384c�T�d}d}d}d}d}t|d��}t|d��}t|d��}t��}t�|���t|��t|��t|��t
t|����ttd������}	|	rtd|	z���t|���tj
��}
tt|��t|��t|��t|��t|��dd	d
|
ddd
��}t �t$�t(|����dS)Nl�����~�����������������l�*�'�#.T��Ebc+�Z'@=D 1 "(�?7N2Z�_+��|��S/1fls)��e`g�w�l
X_[n�lv|�������������l�
�d�x�R��joyU8T(	:�ss�"�n�Z�L�8k�&�"_�Ul_�!�uR��/�sX0
@qa�N�Q�N�B&J��xS8KJ�E�Y�
�	K%�l�0r9z#Error %d initializing P-384 contexti�z1.3.132.0.34rnzecdsa-sha2-nistp384rm)rrr*r;r<rrr=rrr>rr?r@r1rrArBrCrD�
p384_names)rFrGrHrIrJ�p384_modulus�p384_b�
p384_order�ec_p384_contextr-rOrms            r.�	init_p384ry0sn��j�A�j�A�n�E�	k�B�	k�B� ��B�'�'�L�
�1�b�
!�
!�F��u�b�)�)�J�!�m�m�O�
�
&�
&��'A�'A�'C�'C�'2�<�'@�'@�'2�6�':�':�'2�:�'>�'>�'/��L�0A�0A�'B�'B�'2�;�r�?�?�'C�'C�)�)�F��J��?�&�H�I�I�I��?�.�.�0�0�'�2I�J�J�G��'�!�*�*��!�*�*��%�.�.��"�+�+��"�+�+��� ���'����D��N�N�4�=�=��T�2�2�3�3�3�3�3r0)�p521�
NIST P-521zP-521�
prime521v1�	secp521r1�nistp521c�T�d}d}d}d}d}t|d��}t|d��}t|d��}t��}t�|���t|��t|��t|��t
t|����ttd������}	|	rtd|	z���t|���tj
��}
tt|��t|��t|��t|��t|��dd	d
|
ddd
��}t �t$�t(|����dS)Nl#�����������������������������������l#?�VQ(zO�%b�95~c�te1oR{�V��;�LHw>�l�-rZE]�"Sr�&Ga9}*Fl#	dp"z\}[�z�3"n�Z;PK�#
`�7�r�o�C�Q������������������l#f=��x�K�)H-apY$3^Q�	n�%k�{;/K!u{4-{�?$�O��d8�V1�l�3s:�l#Pf�?�Q�E$XN�!85aZ��U��WL9�YL��h�z
�f�$Du13otc!�%�pMxjRr`�Br9z#Error %d initializing P-521 contexti	z1.3.132.0.35r{zecdsa-sha2-nistp521rz)rrr*r;r<rrr=rrr>rr?r@r1rrArBrCrD�
p521_names)rFrGrHrIrJ�p521_modulus�p521_b�
p521_order�ec_p521_contextr-rOrzs            r.�	init_p521r�_s}��	S�A�	S�A�
W�E�
T�B�
T�B� ��B�'�'�L�
�1�b�
!�
!�F��u�b�)�)�J�!�m�m�O�
�
&�
&��'A�'A�'C�'C�'2�<�'@�'@�'2�6�':�':�'2�:�'>�'>�'/��L�0A�0A�'B�'B�'2�;�r�?�?�'C�'C�)�)�F��J��?�&�H�I�I�I��?�.�.�0�0�'�2I�J�J�G��'�!�*�*��!�*�*��%�.�.��"�+�+��"�+�+��� ���'����D��N�N�4�=�=��T�2�2�3�3�3�3�3r0�ed25519r c�"�d}d}d}d}tt|��dt|��t|��t|��ddddddd	��}t�t�t|����dS)
N������������������l�S�9i�@eM�^�w|olUK�5�J,{�$%Xci\�-�G'
lJ[sii!lXf�L�33ff�L�33ff�L�33ff�L�33ff��1.3.101.112r �ssh-ed25519r�)r1rrArBrCrD�
ed25519_names)rFrHrIrJr�s     r.�init_ed25519r��s���J�A�N�E�	K�B�	K�B��W�Q�Z�Z���U�^�^��R�[�[��R�[�[���"���"�� � �G��N�N�4�=�=���8�8�9�9�9�9�9r0�ed448r"c��d}d}d}d}t��}t�|�����}|rt	d|z���t|���tj��}tt|��dt|��t|��t|��ddd|ddd	��}t�t�
t|����dS)
N����������������?���������������l�D�VJ
�Ru�8�a6!m,�&�vD}D2_���������������l^@� 51�8`b8�C�l�\�p*A�t�(�qmj�.<�+FaS[�/SDZ74_3�	lzadoeC@�
�ZK�^�Dsx�s��sZhNx�0�2��>Ilq2� vI�Zugt'
z#Error %d initializing Ed448 contexti��1.3.101.113r"r�)rr)�ed448_new_contextr<r>rr?�ed448_free_contextr1rrArBrCrD�ed448_names)rFrHrIrJ�
ed448_contextr-rOr�s        r.�
init_ed448r��s���z�A�~�E�	{�B�	{�B��M�M�M�
�
)�
)�-�*B�*B�*D�*D�
E�
E�F�
�J��?�&�H�I�I�I��=�,�,�.�.�
�0M�N�N�G��7�1�:�:���5�>�>��2�;�;��2�;�;��� �����
�
�E��N�N�4�=�=��e�4�4�5�5�5�5�5r0c��eZdZdS)�UnsupportedEccFeatureN)�__name__�
__module__�__qualname__�r0r.r�r��s�������Dr0r�c��eZdZdZdd�Zd�Zd�Zd�Zd�Zd�Z	d	�Z
d
�Zd�Ze
d���Ze
d
���Ze
d���Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�EccPointaPA class to model a point on an Elliptic Curve.

    The class supports operators for:

    * Adding two points: ``R = S + T``
    * In-place addition: ``S += T``
    * Negating a point: ``R = -T``
    * Comparing two points: ``if S == T: ...`` or ``if S != T: ...``
    * Multiplying a point by a scalar: ``R = S*k``
    * In-place multiplication by a scalar: ``T *= k``

    :ivar x: The affine X-coordinate of the ECC point
    :vartype x: integer

    :ivar y: The affine Y-coordinate of the ECC point
    :vartype y: integer

    :ivar xy: The tuple with affine X- and Y- coordinates
    r_c��	t||_n-#t$r tdt	|��z���wxYw||_|���}t||��}t||��}t|��|kst|��|krtd���t|d��}t|d��}t��|_	|jj�
��}	n#t$r
t}	YnwxYw||j���t#|��t#|��t%|��|	��}
|
r'|
dkrtd���td|
z���t'|j�
��|��|_dS)NzUnknown curve name %szIncorrect coordinate length�	new_point�
free_point�z)The EC point does not belong to the curvez(Error %d while instantiating an EC point)rAr%�KeyError�
ValueError�str�_curve_name�
size_in_bytesrr=r/r�_pointrOr?�AttributeErrorrr<rrr)�self�x�y�curve�
modulus_bytes�xb�ybr��	free_funcrOr-s           r.�__init__zEccPoint.__init__�s���	C�!�%�.�D�K�K���	C�	C�	C��4�s�5�z�z�A�B�B�B�	C���� ����*�*�,�,�
�
�1�m�
,�
,��
�1�m�
,�
,���r�7�7�m�#�#�s�2�w�w�-�'?�'?��:�;�;�;��T�;�/�/�	��T�<�0�0�	�!�m�m���	#��k�)�-�-�/�/�G�G���	#�	#�	#�"�G�G�G�	#������4�;�1�1�3�3�&�r�?�?�&�r�?�?�#�M�2�2�"�	$�$���	R���|�|� �!L�M�M�M��G�&�P�Q�Q�Q�#�4�;�?�?�#4�#4�i�@�@����s��*?�&D�D�Dc�`�t|d��}t|d��}t��|_||j���|j�����}|rtd|z���t
|j���|��|_|S)N�cloner�z"Error %d while cloning an EC point)r/rr�r<r?r�r)r��pointr�r�r-s     r.�setzEccPoint.set
s�����w�'�'���T�<�0�0�	�!�m�m�����t�{�-�-�/�/��|�'�'�)�)�+�+���	L��A�F�J�K�K�K�"�4�;�?�?�#4�#4�i�@�@����r0c���t|t��sdSt|d��}d||j���|j�����kS)NF�cmpr)�
isinstancer�r/r�r?)r�r��cmp_funcs   r.�__eq__zEccPoint.__eq__sY���%��*�*�	��5��D�%�(�(���H�H�T�[�_�_�.�.���0@�0@�0B�0B�C�C�C�Cr0c��||kS�Nr�)r�r�s  r.�__ne__zEccPoint.__ne__ s���5�=� � r0c��t|d��}|���}||j�����}|rt	d|z���|S)N�negz$Error %d while inverting an EC point)r/�copyr�r?r�)r��neg_func�npr-s    r.�__neg__zEccPoint.__neg__#sY���D�%�(�(��
�Y�Y�[�[����"�)�-�-�/�/�*�*���	N��C�f�L�M�M�M��	r0c�F�|j\}}t|||j��}|S)zReturn a copy of this point.)�xyr�r�)r�r�r�r�s    r.r�z
EccPoint.copy+s&���w���1�
�a��D�,�
-�
-���	r0c��|jjdvS)N�r�r�)r%�name�r�s r.�	_is_eddsazEccPoint._is_eddsa1����{��#7�7�7r0c�V�|���r|jdkS|jdkS)z,``True`` if this is the *point-at-infinity*.r)rr)r�r�r�r�s r.�is_point_at_infinityzEccPoint.is_point_at_infinity4s/���>�>���	%��6�Q�;���7�f�$�$r0c��|���rtdd|j��Stdd|j��S)z-Return the *point-at-infinity* for the curve.r�)r�r�r�r�s r.�point_at_infinityzEccPoint.point_at_infinity<s@���>�>���	4��A�q�$�"2�3�3�3��A�q�$�"2�3�3�3r0c��|jdS)Nr�r�r�s r.r�z
EccPoint.xD����w�q�z�r0c��|jdS)Nr�r�r�s r.r�z
EccPoint.yHr�r0c��|���}t|��}t|��}t|d��}|t|��t|��t	|��|j�����}|rtd|z���tt|����tt|����fS)N�get_xyz#Error %d while encoding an EC point)
r��	bytearrayr/rrr�r?r�rr
)r�r�r�r�r�r-s      r.r�zEccPoint.xyLs����*�*�,�,�
�
�}�
%�
%��
�}�
%�
%���$��)�)�����B���#�B��� ��/�/�����)�)�+�+���	M��B�V�K�L�L�L��
�b�)�)�*�*�G�M�"�4E�4E�,F�,F�G�Gr0c�6�|���dzdzS)z"Size of each coordinate, in bytes.��)�size_in_bitsr�s r.r�zEccPoint.size_in_bytes[s���!�!�#�#�a�'�A�-�-r0c��|jjS)z!Size of each coordinate, in bits.)r%�modulus_bitsr�s r.r�zEccPoint.size_in_bits_s
���{�'�'r0c��t|d��}||j�����}|rtd|z���|S)zuDouble this point (in-place operation).

        Returns:
            This same object (to enable chaining).
        �doublez#Error %d while doubling an EC point�r/r�r?r�)r��double_funcr-s   r.r�zEccPoint.doublecsP���t�X�.�.����T�[�_�_�.�.�/�/���	M��B�V�K�L�L�L��r0c���t|d��}||j���|j�����}|r'|dkrtd���td|z���|S)zAdd a second point to this one�add�z#EC points are not on the same curvez#Error %d while adding two EC pointsr�)r�r��add_funcr-s    r.�__iadd__zEccPoint.__iadd__psx���D�%�(�(����$�+�/�/�+�+�U�\�-=�-=�-?�-?�@�@���	M���|�|� �!F�G�G�G��B�V�K�L�L�L��r0c�8�|���}||z
}|S)z8Return a new point, the addition of this one and another�r�)r�r�r�s   r.�__add__zEccPoint.__add__{s���Y�Y�[�[��
�e����	r0c
�b�t|d��}|dkrtd���t|��}||j���t|��t
t|����ttd������}|rtd|z���|S)zMultiply this point by a scalar�scalarrz?Scalar multiplication is only defined for non-negative integersr9z%Error %d during scalar multiplication)
r/r�rr�r?rrr=rr)r�r��scalar_func�sbr-s     r.�__imul__zEccPoint.__imul__�s����t�X�.�.���A�:�:��^�_�_�_�
�6�
"�
"����T�[�_�_�.�.�(��_�_�%�c�"�g�g�.�.�(��R���9�9�;�;���	O��D�v�M�N�N�N��r0c�8�|���}||z}|S)z2Return a new point, the scalar product of this oner�)r�r�r�s   r.�__mul__zEccPoint.__mul__�s���Y�Y�[�[��
�f����	r0c�,�|�|��Sr�)r�)r��	left_hands  r.�__rmul__zEccPoint.__rmul__�s���|�|�I�&�&�&r0N)r_)r�r�r��__doc__r�r�r�r�r�r�r�r�r��propertyr�r�r�r�r�r�r�r�r�r�r�r�r0r.r�r��s~��������($A�$A�$A�$A�L���D�D�D�!�!�!�������8�8�8�%�%�%�4�4�4�����X������X���H�H��X�H�.�.�.�(�(�(����	�	�	����
�
�
����'�'�'�'�'r0r�r2)�GrQr_rmrzc���eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
ed	���Zed
���Z
ed���Zd�Zd
�Zd�Zd�Zdd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�ZdS)�EccKeya�Class defining an ECC key.
    Do not instantiate directly.
    Use :func:`generate`, :func:`construct` or :func:`import_key` instead.

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

    :ivar pointQ: an ECC point representating the public component.
    :vartype pointQ: :class:`EccPoint`

    :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 representats the private component
                in EdDSA curves
                (Ed25519, 32 bytes; Ed448, 57 bytes).
    :vartype seed: bytes
    c��t|��}|�dd��}|�dd��|_|�dd��|_|�dd��|_|�|jr|jj}|rt
dt|��z���|tvrtd|z���t||_
|j
j|_t|jdu��t|jdu��z}|dkr|j�td	���dS|d
krtd���|���s^|j�td���t|j��|_d
|jcxkr|j
jksntd���dS|j�td���|j
jdkr�t%|j��dkrtd���t'j|j�����}|dd�|_t/|dd���}|dxxdzcc<|ddzdz|d<n�|j
jdkr�t%|j��dkrtd���t1j|j���d��}|dd�|_t/|dd���}|dxxdzcc<|dxxdzcc<d|d<tj|d� ��|_dS)!aiCreate 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 the Ed25519 (32 bytes)
            or Ed448 (57 bytes) curve.
          point : EccPoint
            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.
        r�N�d�seedr��Unknown parameters: zUnsupported curve (%s)rzGAt lest one between parameters 'point', 'd' or 'seed' must be specified�z,Parameters d and seed are mutually exclusivez7Parameter 'seed' can only be used with Ed25519 or Ed448r�z;Parameter d must be an integer smaller than the curve orderz/Parameter d can only be used with NIST P curvesr�rez0Parameter seed must be 32 bytes long for Ed25519���r9r��9z.Parameter seed must be 57 bytes long for Ed448�r��7��8�little��	byteorder)rC�pop�_d�_seedr�r��	TypeErrorr�rAr�r%r&r��intr�rrHr�r=r�new�digest�_prefixr�r�read�
from_bytes)r��kwargs�kwargs_�
curve_name�count�	seed_hash�tmps       r.r�zEccKey.__init__�s-��&�v�,�,���[�[��$�/�/�
��+�+�c�4�(�(����[�[���.�.��
��k�k�'�4�0�0�����$�+����0�J��	C��2�S��\�\�A�B�B�B��W�$�$��5�
�B�C�C�C��j�)����[�%��
��D�G�4�'�(�(�3�t�z��/E�+F�+F�F���A�:�:��{�"� �!j�k�k�k��F��A�:�:��K�L�L�L��~�~���	B��z�%� �!Z�[�[�[��d�g�&�&�D�G����3�3�3�3�$�+�"3�3�3�3�3� �!^�_�_�_�4�3��w�"� �!R�S�S�S��{��9�,�,��t�z�?�?�b�(�(�$�%W�X�X�X�"�J�t�z�2�2�9�9�;�;�	�(����~����	�#�2�#��/�/���A����$������r�7�T�>�T�1��B�����!�W�,�,��t�z�?�?�b�(�(�$�%U�V�V�V�$�L���4�4�9�9�#�>�>�	�(����~����	�#�2�#��/�/���A����$������B����4�������B���(���A�A�A�D�G�G�Gr0c��|jjdvS)N)r r")r%r&r�s r.r�zEccKey._is_eddsa#r�r0c��t|t��sdS|���|���krdS|j|jkS)NF)r�r��has_private�pointQ)r��others  r.r�z
EccKey.__eq__&sP���%��(�(�	��5������$�"2�"2�"4�"4�4�4��5��|�t�{�*�*r0c��|���rV|���r*dttj|j����z}ndt
|j��z}nd}|jj	\}}d|j
j|||fzS)Nz	, seed=%sz, d=%d�z,EccKey(curve='%s', point_x=%d, point_y=%d%s))rr�r�binascii�hexlifyrrr
rr�r%r&)r��extrar�r�s    r.�__repr__zEccKey.__repr__/s��������	��~�~���
0�#�e�H�,<�T�Z�,H�,H�&I�&I�I��� �3�t�w�<�<�/����E��{�~���1�=���AQ�ST�VW�Y^�@_�_�_r0c��|jduS)zJ``True`` if this key can be used for making signatures or decrypting data.N)r
r�s r.rzEccKey.has_private:s���w�d�"�"r0c��d|cxkr|jjksnJ�|jj}tjd|���}|j|z}||z�|��}|jj|zj|z}|||z||zzz|z}||fS)Nrr�)�
min_inclusive�
max_exclusive)r%rHr�random_ranger
�inverser�r�)	r��z�krH�blind�blind_d�inv_blind_k�r�ss	         r.�_signzEccKey._sign@s����1�(�(�(�(�t�{�(�(�(�(�(�(�(���!���$�1�38�:�:�:���'�E�/���q�y�)�)�%�0�0��
�[�]�Q�
�!�E�)���5�1�9�w��{�2�3�e�;���1�v�
r0c���|jj}|d�|��}|jj||z|zz}|j||dz|zz}||zj|dkS)Nr�r)r%rHr,r�rr�)r�r-�rsrH�sinv�point1�point2s       r.�_verifyzEccKey._verifyOsg����!���!�u�}�}�U�#�#�����4�!�8�u�"4�5������1���� 6�7�����"�b��e�+�+r0c�V�|���std���|jS�NzThis is not a private ECC key)rr�r
r�s r.r�zEccKey.dVs,�����!�!�	>��<�=�=�=��w�r0c�V�|���std���|jSr<)rr�rr�s r.r�zEccKey.seed\s-�����!�!�	>��<�=�=�=��z�r0c�P�|j�|jj|jz|_|jSr�)r�r%r�r
r�s r.rz
EccKey.pointQbs$���;���+�-�$�'�1�D�K��{�r0c�B�t|jj|j���S)z^A matching ECC public key.

        Returns:
            a new :class:`EccKey` object
        )r�r�)r�r%r&rr�s r.�
public_keyzEccKey.public_keyhs���D�K�,�D�K�@�@�@�@r0c��|���rtd���|j���}|rF|jj���rd}nd}||jj�|��z}nBd|jj�|��z|jj�|��z}|S)Nz+SEC1 format is unsupported for EdDSA curves���)r�r�rr�r��is_oddr��to_bytes)r��compressr��
first_byter@s     r.�_export_SEC1zEccKey._export_SEC1qs����>�>���	L��J�K�K�K���1�1�3�3�
��
	A��{�}�#�#�%�%�
%�$�
�
�$�
�$��+�-�0�0��?�?�@�J�J�"��+�-�0�0��?�?�@��+�-�0�0��?�?�@�J��r0c�n�|jj\}}|jjdkr9t	|�dd�����}|dzdz|dz|d<nO|jjdkr0t	|�d	d�����}|dzdz|d
<nt
d���t|��S)Nr�rer	r
r�r�rr�rrzNot an EdDSA key to export)rr�r%r�r�rFr��bytes)r�r�r�r-s    r.�
_export_eddsazEccKey._export_eddsa�s����{�~���1��;��y�(�(��q�z�z�"��z�A�A�B�B�F��q�5�Q�,�&��*�4�F�2�J�J�
�[�
��
(�
(��q�z�z�"��z�A�A�B�B�F��a�%�A��F�2�J�J��9�:�:�:��V�}�}�r0c���|���r#|jj}|���}d}n0d}|�|��}t|jj��}t
|||��S)N�1.2.840.10045.2.1)r�r%�oidrLrIr
r)r�rGrOr@�paramss     r.�_export_subjectPublicKeyInfoz#EccKey._export_subjectPublicKeyInfo�sx���>�>���	2��+�/�C��+�+�-�-�J��F�F�%�C��*�*�8�4�4�J� ����1�1�F�.�s�/9�/5�7�7�	7r0Tc���|���sJ�|j���}d|jj�|��z|jj�|��z}dt
|j�|����t|j	j
d���t|d���g}|s|d=t|���
��S)NrDr�r��explicitr�)rrr�r�rFr�rr�r
r%rOrr�encode)r��include_ec_paramsr�r@�seqs     r.�_export_rfc5915_private_derz"EccKey._export_rfc5915_private_der�s������!�!�!�!�!���1�1�3�3�
���k�m�,�,�]�;�;�<��k�m�,�,�]�;�;�<�
���d�f�o�o�m�<�<�=�=��4�;�?�Q�7�7�7��J��3�3�3�5��
!�	��A���3���&�&�(�(�(r0c�z�ddlm}|�dd���d|vrtd���|���r5|jj}t|j���	��}d}n1d}|�
d���}t|jj��}|j||fd	|i|��}|S)
Nr��PKCS8�
passphrase�
protection�5At least the 'protection' parameter should be presentrNF)rV�
key_params)
�	Crypto.IOr[r?r�r�r%rOrrrUrXr
�wrap)r�rr[rO�private_keyrPr-s       r.�
_export_pkcs8zEccKey._export_pkcs8�s���#�#�#�#�#�#��:�:�l�D�)�)�5�,�f�:T�:T��T�U�U�U��>�>���	2��+�/�C�(���4�4�;�;�=�=�K��F�F�%�C��:�:�U�:�S�S�K� ����1�1�F����K��&�&�'-�&�%�&�&���
r0c�d�ddlm}|�|��}|�|d��S)Nr��PEMz
PUBLIC KEY)r`rfrQrU)r�rGrf�encoded_ders    r.�_export_public_pemzEccKey._export_public_pem�s;��!�!�!�!�!�!��7�7��A�A���z�z�+�|�4�4�4r0c�V�ddlm}|���}|j|d|fi|��S)NrrezEC PRIVATE KEY)r`rfrXrU�r�r\rrfrgs     r.�_export_private_pemzEccKey._export_private_pem�sD��!�!�!�!�!�!��6�6�8�8���s�z�+�'7��N�N�v�N�N�Nr0c�b�ddlm}|���}|�|d��S)NrrezPRIVATE KEY)r`rfrcrU)r�rfrgs   r.�(_export_private_clear_pkcs8_in_clear_pemz/EccKey._export_private_clear_pkcs8_in_clear_pem�s9��!�!�!�!�!�!��(�(�*�*���z�z�+�}�5�5�5r0c��ddlm}|sJ�d|vrtd���|jdd|i|��}|�|d��S)Nrrer]r^r\zENCRYPTED PRIVATE KEYr�)r`rfr�rcrUrjs     r.�,_export_private_encrypted_pkcs8_in_clear_pemz3EccKey._export_private_encrypted_pkcs8_in_clear_pem�sk��!�!�!�!�!�!����z��v�%�%��T�U�U�U�(�d�(�I�I�J�I�&�I�I���z�z�+�'>�?�?�?r0c�j�|���rtd���|jj}|�td|jjz���|dkr3|���}t
|��t
|��f}n�|j���}|rQd|jj	�
��z}t|��|jj�
|��z}nBd|jj�
|��z|jj	�
|��z}|�d��d}t
|��t
|��|f}d�d�|D����}|d	zt!t#j|����zS)
Nz"Cannot export OpenSSH private keysz Cannot export %s keys as OpenSSHr�r�rD�-r0c�X�g|]'}tjdt|����|z��(S)�>I)�struct�packr=)�.0r�s  r.�
<listcomp>z*EccKey._export_openssh.<locals>.<listcomp>s/��F�F�F�1���T�3�q�6�6�2�2�Q�6�F�F�Fr0� )rr�r%�opensshr�rLrrr�r�rErr�rF�split�joinrr#�
b2a_base64)	r�rGr&r@�compsr�rH�middle�blobs	         r.�_export_opensshzEccKey._export_openssh�s��������	C��A�B�B�B��{�"���<��?�$�+�BR�R�S�S�S�
�]�
"�
"��+�+�-�-�J��T�]�]�G�J�$7�$7�8�E�E� �K�5�5�7�7�M��
E�����!5�!5�!7�!7�7�
�"�:�.�.�"�k�m�4�4�]�C�C�D�
�
�&�"�k�m�4�4�]�C�C�D�"�k�m�4�4�]�C�C�D�
��Z�Z��_�_�Q�'�F��T�]�]�G�F�O�O�Z�@�E��x�x�F�F��F�F�F�G�G���c�z�E�(�"5�d�";�";�<�<�<�<r0c�X�|���}|�d��}|dvrtd|z���|�dd��}|����r
|�dd��}t	|��r t|��}|std���|�d	d
��}|s#|���rtd���|dkr4|r$|r|j|fi|��S|���S|j	|fi|��S|d
kr8|r|std���|r|j
dd|i|��S|���Std|z���|rtd|z���|dkr|�|��S|d
kr|�
|��S|dkr|�|��S|dkr7|jjdvr|���S|�|��S|�|��S)aExport this ECC key.

        Args:
          format (string):
            The format to use for encoding the key:

            - ``'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 EdDSA curves: ``bytes`` in the format defined in `RFC8032`_.

          passphrase (byte string or string):
            The passphrase to use for protecting the private key.

          use_pkcs8 (boolean):
            Only relevant for private keys.

            If ``True`` (default and recommended), the `PKCS#8`_ representation
            will be used. It must be ``True`` for EdDSA curves.

          protection (string):
            When a private key is exported with password-protection
            and PKCS#8 (both ``DER`` and ``PEM`` formats), this parameter MUST be
            present and be a valid algorithm supported by :mod:`Crypto.IO.PKCS8`.
            It is recommended to use ``PBKDF2WithHMAC-SHA1AndAES128-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 EdDSA curves, as compression is
            mandatory.

        .. 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

        Returns:
            A multi-line string (for ``'PEM'`` and ``'OpenSSH'``) or
            ``bytes`` (for ``'DER'``, ``'SEC1'``, and ``'raw'``) with the encoded key.
        �format)rf�DER�OpenSSH�SEC1�rawzUnknown format '%s'rGFr\NzEmpty passphrase�	use_pkcs8Tz%'pkcs8' must be True for EdDSA curvesrfr�z8Private keys can only be encrpyted with DER using PKCS#8z2Private keys cannot be exported in the '%s' formatzUnexpected parameters: '%s'r�r�r�r�)r�rr�rr	rr�rormrkrcrXrhrQrIr%r�rLr�)r�r�args�
ext_formatrGr\r�s       r.�
export_keyzEccKey.export_keys���L�{�{�}�}���X�X�h�'�'�
��E�E�E��2�Z�?�@�@�@��8�8�J��.�.�������-	6����,��5�5�J���$�$�
9�$�Z�0�0�
�!�9�$�%7�8�8�8�����d�3�3�I��
J����!1�!1�
J� �!H�I�I�I��U�"�"��H�!�O�P�t�P�Q[�d�d�_c�d�d�d�#�L�L�N�N�N�3�4�3�J�G�G�$�G�G�G��u�$�$��a�i�a�$�%_�`�`�`��>�-�4�-�L�L��L�t�L�L�L��;�;�=�=�=� �"6�8B�"C�D�D�D��
G� �!>��!E�F�F�F��U�"�"��.�.�x�8�8�8��u�$�$��8�8��B�B�B��v�%�%��(�(��2�2�2��u�$�$��;�#�';�;�;��-�-�/�/�/��,�,�X�6�6�6��+�+�H�5�5�5r0N)T)r�r�r�r�r�r�r�r&rr4r:r�r�r�rr@rIrLrQrXrcrhrkrmror�r�r�r0r.r�r��s���������*JB�JB�JB�X8�8�8�+�+�+�	`�	`�	`�#�#�#����,�,�,�����X��
����X��
����X��
A�A�A����8
�
�
�7�7�7�)�)�)�)�6���*5�5�5�O�O�O�6�6�6�@�@�@�=�=�=�:z6�z6�z6�z6�z6r0r�c���|�d��}t|}|�dt��}|rtdt	|��z���t|jdkr|d��}t
||���}n`t|jdkr|d��}t
||���}n-tjd	|j	|�
��}t
||���}|S)a1Generate 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�re�r�r�r�rr�)r)r*r�)r�r�)
rrArrr�r�r�rr+rH)rrr�r�r��new_keyr�s       r.�generater��s������G�$�$�J��J��E��z�z�*�&6�7�7�H�
�>��.��V���<�=�=�=��z���9�,�,��x��|�|���z��5�5�5���	��	�	!�W�	,�	,��x��|�|���z��5�5�5���� �q�/4�{�*2�
4�
4�
4���z�Q�/�/�/���Nr0c��|d}t|}|�dd��}|�dd��}d|vrtd���d||fvrt|||��|d<t	di|��}|���r/d|vr+|j|jz}|j||fkrtd���|S)	a�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):
        the integer in the range ``[1..order-1]`` that represents the key.

      seed (bytes):
        Mandatory for a private key and an EdDSA curve.
        It must be 32 bytes for Ed25519, and 57 bytes for Ed448.

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

      point_y (integer):
        Mandatory for a public key: the Y coordinate (affine) of the ECC point.

    Returns:
      :class:`EccKey` : a new ECC key object
    r��point_xN�point_yr�zUnknown keyword: pointz(Private and public ECC keys do not matchr�)
rArrr�r�rr�r�r�r�)rrr�r�r�r��pub_keys       r.�	constructr��s���<���J��J��E��j�j��D�)�)�G��j�j��D�)�)�G��&����0�1�1�1��G�W�%�%�%�"�7�G�Z�@�@��w�����v���G������I��F�!2�!2��'�G�I�%���:�'�7�+�+�+��G�H�H�H��Nr0c��t���D]\}}|r
|j|krn/||krn'�|rtd|z���td|z���|j���}t
|d��}|dkrgt|��dd|zzkrtd���tj
|d|dz���}tj
||dzd���}n�|d	vr�t|��d|zkrtd���tj
|dd���}|d
z|d
zz
|jz�|j��}|dkr|�
��r
|j|z
}|d
kr|���r
|j|z
}ntd���t|||���S)
aConvert 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)zUnsupported ECC curve (%s)r�r�r�zIncorrect EC point lengthN�r��r�zIncorrect EC point encoding�r�r�r�)rA�itemsrOr�rFr�rr=r�rrrG�sqrtrE�is_evenr�)	�ec_point�	curve_oidrr�r�r��
point_typer�r�s	         r.�_import_public_derr��s���&�m�m�o�o�	S�	S���U��	���i�/�/��E���$�$��E�%��	S�'�(I�I�(U�V�V�V�'�(D�z�(Q�R�R�R��G�)�)�+�+�M��h�q�k�"�"�J��T����x�=�=�Q��]�!2�2�3�3��8�9�9�9���x��-��/�(9�:�;�;����x�
�a��(8�(8�9�:�:���	�|�	#�	#��x�=�=�Q��.�/�/��8�9�9�9���x����|�,�,��
��T�A�a�C�Z�%�'�
!�'�'���0�0������!�(�(�*�*����!��A�����!�)�)�+�+����!��A���6�7�7�7��;��1�=�=�=�=r0c���t|��\}}}d}dtfdtfd�}||vrj|std|z���	t	���|��j}n#t$rtd���wxYwt||���S||vr?||\}}	|rtd|z���|	|��\}
}t|
||�	��Std
|z���)z4Convert a subjectPublicKeyInfo into an EccKey object�rNz1.3.132.1.12z1.3.132.1.13r r"�r�r�z%Missing ECC parameters for ECC OID %szError decoding namedCurve�r�z(Unexpected ECC parameters for ECC OID %s)r�r�r�zUnsupported ECC OID: %s)
r�_import_ed25519_public_key�_import_ed448_public_keyr�r
�decode�valuer�r�r�)�encodedrrOr�rP�nist_p_oids�
eddsa_oidsr�r�import_eddsa_public_keyr�r�s            r.�_import_subjectPublicKeyInfor�sA��<�G�D�D��C��6��K�"�#=�>��#;�<���J�
�k����	L��D�s�J�K�K�K�	:�#�
�
�,�,�V�4�4�:�I�I���	:�	:�	:��8�9�9�9�	:����"�(�i�@�@�@�@�	�
�	�	�.8��o�+�
�+��	O��G�#�M�N�N�N�&�&�x�0�0���1���A�Z�@�@�@�@�#�$=��$C�D�D�Ds�&A)�)Bc���t���|d���}|ddkrtd���	td����|d��j}|�||krtd���|}n#t$rYnwxYw|�td	���t
���D]\}}|j|krn�td
|z���t���|d��j
}|j���}t|��|krtd���tj|��}	t|��dkrXt!d����|d��j}
t#|
|�
��}|jj}|jj}
ndx}}
t+||	||
���S)N)r�r�)�nr_elementsrr�z!Incorrect ECC private key versionrSr�zCurve mismatchzNo curve foundr�zPrivate key is too small���r�)r�r�r�r�)rr�r�r
r�rAr�rOr�r�payloadrFr�r=rrrr�rr�r�r�)r�r\r�rb�
parametersrr��scalar_bytesr�r��public_key_encr@r�r�s              r.�_import_rfc5915_derr�Ps����-�-�&�&�w�F�&�C�C�K��1�~�����<�=�=�=�
� �!�,�,�,�3�3�K��N�C�C�I�
�� �Z�9�%<�%<��-�.�.�.��	�	���
�
�
���
�������)�*�*�*�$�]�]�_�_�S�S��
�E��9�	�!�!��E�"�$�$E�	�$Q�R�R�R�!�#�#�*�*�;�q�>�:�:�B�L��G�)�)�+�+�M�
�<���M�)�)��3�4�4�4���<�(�(�A��;���!���%�q�1�1�1�8�8��R��I�I�O��'��)�L�L�L�
��#�%���#�%��� � ��'��:��G�W�M�M�M�Ms�AB�
B�Bc��ddlm}|�||��\}}}d}ddd�}||vr7t���|��j}t
|||��S||vrP|�td���d}t���|��j	}	t|||	���Std	|z���)
NrrZr�r r"r�z.EdDSA ECC private key must not have parametersr�z!Unsupported ECC purpose (OID: %s))r`r[�unwrapr
r�r�r�r�rr�r�r�)
r�r\r[�algo_oidrbrPr�r�r�r�s
          r.�
_import_pkcs8r��s���������$)�L�L��*�$E�$E�!�H�k�6��K�!����J�
�;����M�M�(�(��0�0�6�	�"�;�
�I�F�F�F�	�Z�	�	����M�N�N�N��	����&�&�{�3�3�;���z�(�3�$�?�?�?�?�#�$G�(�$R�S�S�Sr0c�>�t|��}t|��Sr�)rr�)r�r�sp_infos   r.�_import_x509_certr��s��.�w�7�7�G�'��0�0�0r0c��	t||��S#t$r}|�d}~wtttf$rYnwxYw	t||��S#t$r}|�d}~wtttf$rYnwxYw	t
||��S#t$r}|�d}~wtttf$rYnwxYw	t||��S#t$r}|�d}~wtttf$rYnwxYwtd���)NzNot an ECC DER key)r�r�r�r�
IndexErrorr�r�r�)r�r\�errs   r.�_import_derr��so��
�+�G�Z�@�@�@�� �����	������	�:�.�
�
�
���
����
� ��*�5�5�5�� �����	������	�:�.�
�
�
���
����
�"�7�J�7�7�7�� �����	������	�:�.�
�
�
���
����
��W�j�1�1�1�� �����	������	�:�.�
�
�
���
�����)�
*�
*�*sm��
;��;�;�A�
A8�A�A8�7A8�<B�
B5�B�B5�4B5�9C	�	
C2�C�C2�1C2c�v�|�d��}t|��dvrtd���	tj|d��}g}t|��dkrctjd|dd���d}|�|dd|z���|d|zd�}t|��dk�c|d|dkrtd���|d�d	��r�t�
��D]c\}}|j��
|j�d
��s�(t|j�d��d��}|d|krn�dtd
|z���t|d|j���}nO|ddkr+t|d��\}	}
t!d|	|
���}ntd|dz���n8#t"t$tjf$rtd|dz���wxYw|S)N� r�zNot an openssh public keyr�r�rsrzMismatch in openssh public key�ecdsa-sha2-�
ecdsa-sha2rqr�zUnsupported ECC curve: r��ssh-ed25519r r�zUnsupported SSH key type: zError parsing SSH key type: )rzr=r�r#�
a2b_base64rt�unpack�append�
startswithrAr�ryrr�rOr�r�r�r�Error)r��parts�	keystring�keyparts�lkrr�r~�ecc_keyr�r�s           r.�_import_openssh_publicr��sQ���M�M�$���E�
�5�z�z�����4�5�5�5�$D��'��a��1�1�	����)�n�n�q� � ���t�Y�r��r�]�3�3�A�6�B��O�O�I�a��B��h�/�0�0�0�!�!�b�&�'�'�*�I��)�n�n�q� � �
��8�x��{�"�"��=�>�>�>���8���~�.�.�	F�%,�]�]�_�_�	
E�	
E�!�
�E��=�(���}�/�/��=�=��� ���!4�!4�S�!9�!9�!�!<�=�=���A�;�&�(�(��E�)�!�!:�V�!C�D�D�D�(��!���	�J�J�J�G�G��1�X��
'�
'�-�h�q�k�:�:�D�A�q��i��A�F�F�F�G�G��9�E�!�H�D�E�E�E����	�8�>�2�D�D�D��7�%��(�B�C�C�C�D�����Ns�G	H�5H6c��ddlm}m}m}m}|||��\}}ddt
dfi}|�d���r||��\}	}|	tvrtd|	z���t|	}
|
j	dzd	z}||��\}}t|d
��dkrtd���t|��d
|zdzkrtd���tj|dd|z���}
tj|d|zd���}||��\}}tj|��}||	d�}n\||vrF||\}}}||��\}}||��\}
}||��\}}|d|�}||d�}ntd|z���||��\}}||��td|
|d�|��S)Nr�)�import_openssh_private_generic�
read_bytes�read_string�
check_paddingr�r rer�zUnsupported ECC curve %sr�r�rr�z/Only uncompressed OpenSSH EC keys are supportedr�zIncorrect public key length)r�r�)r�r�zUnsupport SSH agent key type:)r�r�r�)�_opensshr�r�r�r�r�r�rAr�r�rr�r=rrr�)�data�passwordr�r�r�r��key_type�	decrypted�
eddsa_keys�ecdsa_curve_namer�r�r@r�r�rbr�rPrr��seed_len�private_public_keyr��_�paddeds                         r.�_import_openssh_private_eccr��s���C�C�C�C�C�C�C�C�C�C�C�C�9�8��x�H�H��H�i�	�	�#=�r�B��J�
���<�(�(�#E�&1�k�)�&<�&<�#��)��7�*�*�'�(B�EU�(U�V�V�V��(�)���+�a�/�A�5�
� *�
�9� 5� 5��
�I��
�1�
���!�#�#��N�O�O�O��z�?�?�a�-�/�!�3�3�3��:�;�;�;��$�Z��!�M�/�0A�%B�C�C���$�Z��-��0@�0@�%A�B�B��!+��I�!6�!6���Y���{�+�+���#3�4�4���	�Z�	�	�8B�8�8L�5�
�+�X� *�
�9� 5� 5��
�I�2�2�:�>�>����(2�
�9�(=�(=�%��I�!�)�8�)�,����4�4����8�8�C�D�D�D���I�&�&�I�A�v��M�&�����@�W�g�@�@��@�@�@r0c� �t|��dkrtd���td��}d}t|��}|ddz	}|dxxdzcc<tj|d�	��}||krtd
���|dkrdS|d
zdz
|z}|d
z|z|zdz|z}	|�|��}||z|z}	tj|	|��}
|
dz|kr||
z
}
n#t$rtd���wxYw|
|fS)a~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:
      :class:`EccKey` : a new ECC key object

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

    .. _RFC8032: https://datatracker.ietf.org/doc/html/rfc8032
    rez9Incorrect length. Only Ed25519 public keys are supported.r�l�x�&(7Z/�
;(P8�s�e:�8��
w6Rrr�rr	r
zInvalid Ed25519 key (y)r��rr�r�zInvalid Ed25519 public key)r=r�rr�rr,�_tonelli_shanks�r�rFr�r��x_lsbr��u�v�v_inv�x2r�s           r.r�r�*sP��"�7�|�|�r����T�U�U�U��R�S�S�A�U�A��'���A�
�b�E�Q�J�E��b�E�E�E�T�M�E�E�E�� ��h�7�7�7�G��!�|�|��2�3�3�3��!�|�|��t�	�!��a��1��A�
�1�*�q�.�A�	��	!�Q�&�A�7��	�	�!�����%�i�1�_���)�"�a�0�0���a�K�E�!�!��'�k�G����7�7�7��5�6�6�6�7�����G��s
�.AC/�/D	c��t|��dkrtd���td��}d}|dd�}t|d��dz	}tj|d�	��}||krtd
���|dkrdS|d
zdz
|z}|d
z|z|zdz
|z}	|�|��}||z|z}	tj|	|��}
|
dz|kr||
z
}
n#t$rtd���wxYw|
|fS)azImport 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:
      :class:`EccKey` : a new ECC key object

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

    .. _RFC8032: https://datatracker.ietf.org/doc/html/rfc8032
    rz7Incorrect length. Only Ed448 public keys are supported.r�lVg��������������?���������������Nrr�r	r
zInvalid Ed448 key (y)r�r�r�zInvalid Ed448 public key)r=r�rrrr,r�r�s           r.r�r�WsM��"�7�|�|�r����R�S�S�S��C�	D�	D�A�z�A������A��������"�E�� ��h�7�7�7�G��!�|�|��0�1�1�1��!�|�|��t�	�!��a��1��A�
�1�*�q�.�A�	��	!�Q�&�A�5��	�	�!�����%�i�1�_���)�"�a�0�0���a�K�E�!�!��'�k�G����5�5�5��3�4�4�4�5�����G��s
�&AC'�'Dc��ddlm}t|��}|�t|��}|�d��r;t	|��}|�||��\}}}t
||��}|S|�d��r�t	|��}d}	d}
tj|	dz|
zd	|tj	�
��}|�||��\}}}|rd}	t||��}n,#t$r}|�d}~wt$rtd���wxYw|S|�d��rt|��St|��dkr)t|d��d
krt||��St|��dkr9t|d��dvr"|�td���t!||���Std���)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`.

    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
    rreNs-----BEGIN OPENSSH PRIVATE KEYs-----z-----BEGIN EC PARAMETERS-----z-----END EC PARAMETERS-----z.*?r")�flagsz(Invalid DER encoding inside the PEM file)r�r�rsszNo curve name was provided)rzECC key format is not supported)r`rfrr�rr�r��re�sub�DOTALLr�r�r�r�r=rr�)
r�r\rrf�text_encoded�openssh_encoded�marker�enc_flagr-�ecparams_start�ecparams_end�der_encoded�uefs
             r.�
import_keyr��s0��~�������g���G����Z�(�(�
����;�<�<���W�~�~��,/�J�J�|�Z�,P�,P�)����,�_�j�I�I���
�	�	�	�H�	%�	%���W�~�~��9��4���v�n�u�4�|�C�R�*�$&�I�/�/�/��),�
�
�<��(L�(L�%��V�X��	��J�	I� ��j�9�9�F�F��$�	�	�	��I������	I�	I�	I��G�H�H�H�	I�����
����:�;�;�/�%�g�.�.�.��7�|�|�a���D����,�,��4�4��7�J�/�/�/��7�|�|�a���D����,�,��?�?����9�:�:�:�!�'�j�A�A�A�A�
�6�
7�
7�7s�&C7�7
D �D�D �__main__l��_,)�N�$�c�
hKf-5lk<X�k�#Ei�z	(P-256 G)i��msz(P-256 arbitrary point))NNr�)p�
__future__rr�rtr#�collectionsr�Crypto.Util.py3compatrrrrr	�Crypto.Util.numberr
r�Crypto.Math.Numbersr�Crypto.Util.asn1r
rrr�Crypto.Util._raw_apirrrrrrr�Crypto.PublicKeyrrr�Crypto.Hashrr�
Crypto.Randomr�Crypto.Random.randomrr*r(r)r/r1rArErPrYr^rgrlrtryr�r�r�r�r�r�r�r��objectr�rIrJ�p192_G�_replacer2rBrCrD�p224_GrQ�p256_Gr_�p384_Grm�p521_Grz�	ed25519_Gr��ed448_Gr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r��timer�r�r�r�r�start�ranger��pointX�printr�r0r.�<module>rs���>&�%�%�%�%�%�	�	�	�	�
�
�
�
�����"�"�"�"�"�"�G�G�G�G�G�G�G�G�G�G�G�G�G�G�;�;�;�;�;�;�;�;�'�'�'�'�'�'�,�,�,�,�,�,�,�,�,�,�,�,�=�=�=�=�=�=�=�=�=�=�=�=�=�=�=�=�=�=�@�@�@�@�@�@�@�@�@�@�)�(�(�(�(�(�(�(�*�*�*�*�*�*�,�,�,�,�,�,�$�
#�$=�@����>)�(�)D�G����"'�
&�'@�C���
�,���4
��H�\�	]�	]��
�����
�$4�$4�$4�N
�	����
����
�$4�$4�$4�N
�	����
����
�$4�$4�$4�N
�	����
����
�$4�$4�$4�N
�	����
����
�$4�$4�$4�N
�	����
��I�&�
�:�:�:�,
�������� ��6�6�6�:�
�����	�	�	�	�	�J�	�	�	�J'�J'�J'�J'�J'�v�J'�J'�J'�\
��'�&�/�$�g�f�o�&8�&�	A�	A���v����&��)�)�����t�}�}�Z��.�.�/�/�/�
�D�*�	��'�&�/�$�g�f�o�&8�&�	A�	A���v����&��)�)�����t�}�}�Z��.�.�/�/�/�
�D�*�	��'�&�/�$�g�f�o�&8�&�	A�	A���v����&��)�)�����t�}�}�Z��.�.�/�/�/�
�D�*�	��'�&�/�$�g�f�o�&8�&�	A�	A���v����&��)�)�����t�}�}�Z��.�.�/�/�/�
�D�*�	��'�&�/�$�g�f�o�&8�&�	A�	A���v����&��)�)�����t�}�}�Z��.�.�/�/�/�
�D�*��H�W�Y�'�*�G�I�,>�,A�9�M�M�	�
�)�
�
%�
%�	�
%�
2�
2�����t�}�}�]�G�4�4�5�5�5�
�w�
�
�(�7�7�#�&���(8�(;�W�
E�
E�����!�!�G�!�,�,�����t�}�}�[�%�0�0�1�1�1��U�K�H6�H6�H6�H6�H6�V�H6�H6�H6�V���D3�3�3�l5>�5>�5>�5>�p1E�1E�1E�h/N�/N�/N�/N�dT�T�T�81�1�1�+�+�+�B+�+�+�\4A�4A�4A�n*�*�*�Z)�)�)�Xq8�q8�q8�q8�h�z����K�K�K�J�A��F�O��"�"�$�$�E��E��D�I�K�K�E�
�U�5�\�\���������	�E�+�	��	���e�+�u�4�t�;�T�B�B�B��D�I�K�K�E�
�U�5�\�\�����!����	�E�
#�i�d�i�k�k�E�&9�U�%B�T�%I�4�P�P�P�P�P�#�r0