3 l�_�6�&@s�ddlmZmZmZddlZddlZddlZddlmZddl m Z ddl m Z Gdd�de �Zejej�Gdd �d e ��Zejej�Gd d �d e ��Zejej�Gd d �d e ��Zejej�Gdd�de��Zejej�Gdd�de ��ZeZeje�Gdd�de ��Zeje�Gdd�de ��Zeje�Gdd�de ��Zeje�Gdd�de ��Zeje�Gdd�de ��Zeje�Gdd�de ��Zeje�Gdd�de ��Zeje�Gd d!�d!e ��Zeje�Gd"d#�d#e ��Z eje�Gd$d%�d%e ��Z!eje�Gd&d'�d'e ��Z"eje�Gd(d)�d)e ��Z#eje�Gd*d+�d+e ��Z$eje�Gd,d-�d-e ��Z%eje�Gd.d/�d/e ��Z&eje�Gd0d1�d1e ��Z'eje�Gd2d3�d3e ��Z(eje�Gd4d5�d5e ��Z)eje�Gd6d7�d7e ��Z*e'e$e'e&e$e#e"e%e!e eeeeeeeee(e)e*d8�Z+eje�Gd9d:�d:e ��Z,dGd;d<�Z-dHd=d>�Z.Gd?d@�d@e �Z/GdAdB�dBe �Z0GdCdD�dDe �Z1ej'e'ej&e&ej%e%ej$e$ej#e#ej"e"ej2e(ej3e)ej4e*ej!e!ejeej e ejeejeejeejeejeejeejeiZ5dEdF�Z6dS)I�)�absolute_import�division�print_functionN)�utils)�ObjectIdentifier)� _get_backendc@s�eZdZed�Zed�Zed�Zed�Zed�Zed�Z ed�Z ed�Z ed �Z ed �Z ed �Zed �Zed �Zed�Zed�Zed�Zed�Zed�Zed�ZdS)�EllipticCurveOIDz1.2.840.10045.3.1.1z 1.3.132.0.33z 1.3.132.0.10z1.2.840.10045.3.1.7z 1.3.132.0.34z 1.3.132.0.35z1.3.36.3.3.2.8.1.1.7z1.3.36.3.3.2.8.1.1.11z1.3.36.3.3.2.8.1.1.13z 1.3.132.0.1z 1.3.132.0.15z 1.3.132.0.26z 1.3.132.0.27z 1.3.132.0.16z 1.3.132.0.17z 1.3.132.0.36z 1.3.132.0.37z 1.3.132.0.38z 1.3.132.0.39N)�__name__� __module__� __qualname__r� SECP192R1� SECP224R1� SECP256K1� SECP256R1� SECP384R1� SECP521R1�BRAINPOOLP256R1�BRAINPOOLP384R1�BRAINPOOLP512R1� SECT163K1� SECT163R2� SECT233K1� SECT233R1� SECT283K1� SECT283R1� SECT409K1� SECT409R1� SECT571K1� SECT571R1�rr�/usr/lib64/python3.6/ec.pyrs&rc@s(eZdZejdd��Zejdd��ZdS)� EllipticCurvecCsdS)z8 The name of the curve. e.g. secp256r1. Nr)�selfrrr �name)szEllipticCurve.namecCsdS)z< Bit size of a secret scalar for the curve. Nr)r"rrr �key_size/szEllipticCurve.key_sizeN)r r r �abc�abstractpropertyr#r$rrrr r!'sr!c@seZdZejdd��ZdS)�EllipticCurveSignatureAlgorithmcCsdS)z@ The digest algorithm used with this signature. Nr)r"rrr � algorithm8sz)EllipticCurveSignatureAlgorithm.algorithmN)r r r r%r&r(rrrr r'6sr'c@s`eZdZejdd��Zejdd��Zejdd��Zejdd��Z ejd d ��Z ejd d ��Z d S)�EllipticCurvePrivateKeycCsdS)zN Returns an AsymmetricSignatureContext used for signing data. Nr)r"�signature_algorithmrrr �signerAszEllipticCurvePrivateKey.signercCsdS)z} Performs a key exchange operation using the provided algorithm with the provided peer's public key. Nr)r"r(Zpeer_public_keyrrr �exchangeGsz EllipticCurvePrivateKey.exchangecCsdS)zB The EllipticCurvePublicKey for this private key. Nr)r"rrr � public_keyNsz"EllipticCurvePrivateKey.public_keycCsdS)z8 The EllipticCurve that this key is on. Nr)r"rrr �curveTszEllipticCurvePrivateKey.curvecCsdS)z< Bit size of a secret scalar for the curve. Nr)r"rrr r$Zsz EllipticCurvePrivateKey.key_sizecCsdS)z Signs the data Nr)r"�datar*rrr �sign`szEllipticCurvePrivateKey.signN) r r r r%�abstractmethodr+r,r-r&r.r$r0rrrr r)?s r)c@s(eZdZejdd��Zejdd��ZdS)�(EllipticCurvePrivateKeyWithSerializationcCsdS)z9 Returns an EllipticCurvePrivateNumbers. Nr)r"rrr �private_numbersisz8EllipticCurvePrivateKeyWithSerialization.private_numberscCsdS)z6 Returns the key serialized as bytes. Nr)r"�encoding�formatZencryption_algorithmrrr � private_bytesosz6EllipticCurvePrivateKeyWithSerialization.private_bytesN)r r r r%r1r3r6rrrr r2gsr2c@sleZdZejdd��Zejdd��Zejdd��Zejdd��Z ejd d ��Z ejd d ��Z e d d��Z dS)�EllipticCurvePublicKeycCsdS)zQ Returns an AsymmetricVerificationContext used for signing data. Nr)r"� signaturer*rrr �verifierxszEllipticCurvePublicKey.verifiercCsdS)z8 The EllipticCurve that this key is on. Nr)r"rrr r.~szEllipticCurvePublicKey.curvecCsdS)z< Bit size of a secret scalar for the curve. Nr)r"rrr r$�szEllipticCurvePublicKey.key_sizecCsdS)z8 Returns an EllipticCurvePublicNumbers. Nr)r"rrr �public_numbers�sz%EllipticCurvePublicKey.public_numberscCsdS)z6 Returns the key serialized as bytes. Nr)r"r4r5rrr � public_bytes�sz#EllipticCurvePublicKey.public_bytescCsdS)z5 Verifies the signature of the data. Nr)r"r8r/r*rrr �verify�szEllipticCurvePublicKey.verifycCsbtjd|�t|t�std��t|�dkr2td��tj|d�d krJtd��dd l m }|j ||�S) Nr/z'curve must be an EllipticCurve instancerz%data must not be an empty byte string���z%Unsupported elliptic curve point type)�backend)r=r>r?) r� _check_bytes� isinstancer!� TypeError�len� ValueError�sixZ indexbytesZ,cryptography.hazmat.backends.openssl.backendr@Z load_elliptic_curve_public_bytes)�clsr.r/r@rrr �from_encoded_point�s    z)EllipticCurvePublicKey.from_encoded_pointN)r r r r%r1r9r&r.r$r:r;r<� classmethodrHrrrr r7vsr7c@seZdZdZdZdS)r� sect571r1i:N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect409r1i�N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect283r1iN)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect233r1��N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect163r2�N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect571k1i;N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect409k1i�N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect283k1iN)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect233k1rNN)r r r r#r$rrrr r�src@seZdZdZdZdS)r� sect163k1rPN)r r r r#r$rrrr r�src@seZdZdZdZdS)r� secp521r1i N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� secp384r1i�N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� secp256r1�N)r r r r#r$rrrr r�src@seZdZdZdZdS)r� secp256k1rYN)r r r r#r$rrrr r�src@seZdZdZdZdS)r � secp224r1��N)r r r r#r$rrrr r sr c@seZdZdZdZdS)r � secp192r1��N)r r r r#r$rrrr r sr c@seZdZdZdZdS)�BrainpoolP256R1�brainpoolP256r1rYN)r r r r#r$rrrr r_sr_c@seZdZdZdZdS)�BrainpoolP384R1�brainpoolP384r1i�N)r r r r#r$rrrr rasrac@seZdZdZdZdS)�BrainpoolP512R1�brainpoolP512r1iN)r r r r#r$rrrr rcsrc)Z prime192v1Z prime256v1r]r[rXrWrVrZrUrTrSrRrQrOrMrLrKrJr`rbrdc@seZdZdd�Zejd�ZdS)�ECDSAcCs ||_dS)N)� _algorithm)r"r(rrr �__init__>szECDSA.__init__rfN)r r r rgr�read_only_propertyr(rrrr re<srecCst|�}|j|�S)N)rZ#generate_elliptic_curve_private_key)r.r@rrr �generate_private_keyDsricCsJt|�}t|tj�std��|dkr,td��t|t�s>td��|j||�S)Nz&private_value must be an integer type.rz)private_value must be a positive integer.z/curve must provide the EllipticCurve interface.)rrBrF� integer_typesrCrEr!Z!derive_elliptic_curve_private_key)� private_valuer.r@rrr �derive_private_keyIs  rlc@speZdZdd�Zddd�Zdd�Zedd ��Zej d �Z ej d �Z ej d �Z d d�Z dd�Zdd�Zdd�ZdS)�EllipticCurvePublicNumberscCsLt|tj� st|tj� r$td��t|t�s6td��||_||_||_dS)Nzx and y must be integers.z/curve must provide the EllipticCurve interface.)rBrFrjrCr!�_y�_x�_curve)r"�x�yr.rrr rgXs  z#EllipticCurvePublicNumbers.__init__NcCst|�}|j|�S)N)rZ"load_elliptic_curve_public_numbers)r"r@rrr r-esz%EllipticCurvePublicNumbers.public_keycCsBtjdtjdd�|jjdd}dtj|j|�tj|j|�S)Nz�encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.r=)� stacklevel���) �warnings�warnr�PersistentlyDeprecated2019r.r$Z int_to_bytesrqrr)r"� byte_lengthrrr � encode_pointis z'EllipticCurvePublicNumbers.encode_pointcCs�t|t�std��tjdtjdd�|jd�r�|jdd}t |�d|dkr�tj |d|d�d �}tj ||dd�d �}||||�St d ��nt d ��dS) Nz'curve must be an EllipticCurve instancez�Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_pointr=)rsrvrtru�Zbigz(Invalid elliptic curve point data lengthz%Unsupported elliptic curve point type) rBr!rCrwrxrry� startswithr$rDZint_from_bytesrE)rGr.r/rzrqrrrrr rHzs    z-EllipticCurvePublicNumbers.from_encoded_pointrprorncCsFt|t�stS|j|jkoD|j|jkoD|jj|jjkoD|jj|jjkS)N)rBrm�NotImplementedrqrrr.r#r$)r"�otherrrr �__eq__�s    z!EllipticCurvePublicNumbers.__eq__cCs ||k S)Nr)r"rrrr �__ne__�sz!EllipticCurvePublicNumbers.__ne__cCst|j|j|jj|jjf�S)N)�hashrqrrr.r#r$)r"rrr �__hash__�sz#EllipticCurvePublicNumbers.__hash__cCs dj|�S)NzC)r5)r"rrr �__repr__�sz#EllipticCurvePublicNumbers.__repr__)N)r r r rgr-r{rIrHrrhr.rqrrr�r�r�r�rrrr rmWs      rmc@sJeZdZdd�Zddd�Zejd�Zejd�Zdd �Z d d �Z d d �Z dS)�EllipticCurvePrivateNumberscCs6t|tj�std��t|t�s&td��||_||_dS)Nz!private_value must be an integer.z>public_numbers must be an EllipticCurvePublicNumbers instance.)rBrFrjrCrm�_private_value�_public_numbers)r"rkr:rrr rg�s  z$EllipticCurvePrivateNumbers.__init__NcCst|�}|j|�S)N)rZ#load_elliptic_curve_private_numbers)r"r@rrr � private_key�sz'EllipticCurvePrivateNumbers.private_keyr�r�cCs&t|t�stS|j|jko$|j|jkS)N)rBr�r~rkr:)r"rrrr r��s  z"EllipticCurvePrivateNumbers.__eq__cCs ||k S)Nr)r"rrrr r��sz"EllipticCurvePrivateNumbers.__ne__cCst|j|jf�S)N)r�rkr:)r"rrr r��sz$EllipticCurvePrivateNumbers.__hash__)N) r r r rgr�rrhrkr:r�r�r�rrrr r��s    r�c@s eZdZdS)�ECDHN)r r r rrrr r��sr�c Cs*yt|Stk r$td��YnXdS)NzCThe provided object identifier has no matching elliptic curve class)� _OID_TO_CURVE�KeyError� LookupError)Zoidrrr �get_curve_for_oid�s r�)N)N)7Z __future__rrrr%rwrFZ cryptographyrZcryptography.hazmat._oidrZcryptography.hazmat.backendsr�objectrZ add_metaclass�ABCMetar!r'r)r2r7Z'EllipticCurvePublicKeyWithSerializationZregister_interfacerrrrrrrrrrrrrrr r r_rarcZ _CURVE_TYPESrerirlrmr�r�rrrr�r�rrrr �s�   '7  X%