U af"Y@sddlmZmZmZmZddlZddlZddlZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZddlmZmZddlmZdd lmZejd kreZ e!Z"nd d Z e Z#e#d kre$ddddddgZ%ddddZ&ddddZ'ddZ(ddZ)ddZ*ddZ+ddZ,ddZ-Gd d!d!Z.Gd"d#d#Z/e/dddZ0e.d$d%d&Z1e/e1d'd(d)Z2e.d*d%d+Z3e/e3d,d-d.Z4e.d/d%d0Z5e/e5d1d2d3Z6e.d4d%d5Z7e/e7d6d7d8Z8e.d9d%d:Z9e/e9d;d)unicode_literalsdivisionabsolute_importprint_functionN)backend) Certificate DSASignatureECDomainParametersECPointBitString ECPrivateKeyint_from_bytesPrivateKeyAlgorithmPrivateKeyInfoPublicKeyAlgorithm PublicKeyInfo)pretty_message) type_namebyte_cls) rand_bytes)SignatureError)cCs t|gS)N)bytes)numr@/opt/nydus/tmp/pip-target-53d1vnqk/lib/python/oscrypto/_ecdsa.pychr_clssrZ winlegacyz2Pure-python ECDSA code is only for Windows XP/2003ec_generate_pairec_compute_public_key_pointec_public_key_info ecdsa_sign ecdsa_verify 0B secp256r1 secp384r1 secp521r1cCs|tdddgkr$ttdt|t|}tttd|}t|}t |dd}|dkr<||j krrAnativer Z from_coordsxy)r6Z curve_typedetailsZ base_pointZ public_pointrrrrs8  cCsD|tdddgkr$ttdt|ttdtd|dd|d S) a Constructs the PublicKeyInfo for an ECPointBitString :param private_key: An asn1crypto.keys.ECPointBitString object :param curve: A unicode string of the curve name - one of secp256r1, secp384r1 or secp521r1 :raises: ValueError - when any of the parameters contain an invalid value :return: An asn1crypto.keys.PublicKeyInfo object r&r'r(r*r-r.r/r2)r3r7)r8r9rr:rrr )public_key_pointrBrrrrscCs2t|drt|jts(ttdt||j}|tdddgkrLt tdt|t shttdt||tdd d d d gkrt td t |t t |}|jdj}|dj}|dj}t|}tttd|} | j} ||} t| } t| dd| } d| }d| }t||d|| |}t|||}t||d|| |}t|||}d}d}d}t||krt|||}||7}qxt|d|dd}|dkst|| krАqt| |j| }|dkrqtt|| | ||| | }|dkr qtq qtt||dS)aN Generates an ECDSA signature in pure Python (thus slow) :param private_key: The PrivateKey to generate the signature with :param data: A byte string of the data the signature is for :param hash_algorithm: A unicode string of "sha1", "sha256", "sha384" or "sha512" :raises: ValueError - when any of the parameters contain an invalid value TypeError - when any of the parameters are of the wrong type OSError - when an error is returned by the OS crypto library :return: A byte string of the signature asn1zy private_key must be an instance of the oscrypto.asymmetric.PrivateKey class, not %s r&r'r(zx private_key does not use one of the named curves secp256r1, secp384r1 or secp521r1 < data must be a byte string, not %s sha1sha224sha256sha384sha512z hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384", "sha512", not %s r6r%Fr+r)rs)hasattrrGrNrrHrrrBr8r9rr:getattrhashlibrAcontentsrIr;r<r=r>r?digestlenr hmacnewrJ inverse_modr dump)r6datahash_algorithm curve_name hash_funcZec_private_keyrErFrCrDnr_Z hash_lengthhVKrYrZTkrrrr st         cCst|d}|rt|jttfs0ttdt||j}|t dddgkrTt tdt|t spttdt|t|t sttdt||t d d d d d gkrt tdt ||j}t|tr|j }tttd|}|d\}} |j} t|j|| | } z"t|}|dj} |dj} Wnt k r@tdYnXd}|| dkO}|| | kO}|| dkO}|| | kO}|rtdtt|}||}t|dd| }t| | }||| }| || }||| |}| |j| krtddS)a Verifies an ECDSA signature in pure Python (thus slow) :param certificate_or_public_key: A Certificate or PublicKey instance to verify the signature with :param signature: A byte string of the signature to verify :param data: A byte string of the data the signature is for :param hash_algorithm: A unicode string of "md5", "sha1", "sha256", "sha384" or "sha512" :raises: oscrypto.errors.SignatureError - when the signature is determined to be invalid ValueError - when any of the parameters contain an invalid value TypeError - when any of the parameters are of the wrong type OSError - when an error is returned by the OS crypto library rNz certificate_or_public_key must be an instance of the oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate classes, not %s r&r'r(z certificate_or_public_key does not use one of the named curves secp256r1, secp384r1 or secp521r1 zA signature must be a byte string, not %s rOrPrQrRrSrTrUr%r7rYrZzSignature is invalidrrFr+N)r[rGrNrrrHrrrBr8r9rr:r7r<r=r>Z to_coordsr? PrimePointr loadrIrr\r]r_r rcrJ)Zcertificate_or_public_key signaturererfZhas_asn1rgrNrDrJrKrirMrYrZinvalidrhr_zwu1u2Z hash_pointrrrr!`sx                c Cs|dks||kr||}||}}d\}}}}|dkrrt|||f\}}}||||||||f\}}}}q.|dks~t|dkr|S||SdS)z Compute the modular inverse of a (mod p) :param a: An integer :param p: An integer :return: An integer r)rrrrrN)divmodAssertionError) apcdZucvcZudZvdqrrrrcs  & rcc@s eZdZdZddZddZdS) PrimeCurvezc Elliptic curve over a prime field. Characteristic two field curves are not supported. cCs||_||_||_dS)a The curve of points satisfying y^2 = x^3 + a*x + b (mod p) :param p: The prime number as an integer :param a: The component a as an integer :param b: The component b as an integer N)rzryb)selfrzryrrrr__init__-szPrimeCurve.__init__cCsB|j|j}|j|j|j}|||j|j|j|jdkS)z~ :param point: A Point object :return: Boolean if the point is on this curve r)rKrJryrrz)rZpointy2x3rrrcontains?s zPrimeCurve.containsN)__name__ __module__ __qualname____doc__rrrrrrr'src@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)roz1 A point on a prime-field elliptic curve NcCsR||_||_||_||_|jr2|j|s2td|jrN||jtkrNtddS)a) :param curve: A PrimeCurve object :param x: The x coordinate of the point as an integer :param y: The y coordinate of the point as an integer :param order: The order of the point, as an integer - optional zInvalid EC pointN)rBrJrKr?rr9INFINITY)rrBrJrKr?rrrrRs zPrimePoint.__init__cCs0|j|jkr(|j|jkr(|j|jkr(dSdSdS)zy :param other: A PrimePoint object :return: 0 if identical, 1 otherwise rrN)rBrJrKrotherrrr__cmp__os$zPrimePoint.__cmp__cCs|tkr |S|tkr|S|j|jks(t|j|jkrX|j|j|jjdkrPtS|S|jj}|j|jt|j|j||}|||j|j|}||j||j|}t|j||S)zq :param other: A PrimePoint object :return: A PrimePoint object r) rrBrxrJrKrzdoublercro)rrrzl_ry3rrr__add__|s  "zPrimePoint.__add__cCsdd}|}|jr||j}|dkr(tS|tkr4tS|dks@td|}t|j|j|j |j}||d}|}|dkr|}||@dkr||@dkr||}||@dkr||@dkr||}|d}qp|S) :param other: An integer to multiple the Point by :return: A PrimePoint object cSs*|dks td}||kr"d|}q|dS)Nrr)rx)rJresultrrr leftmost_bits   z(PrimePoint.__mul__..leftmost_bitrrrr)r?rrxrorBrJrKr)rrreZe3Z negative_selfirrrr__mul__s*     zPrimePoint.__mul__cCs||S)rrrrrr__rmul__s zPrimePoint.__rmul__cCst|jj}|jj}d|j|j|td|j||}||d|j|}||j||j|}t|j||S)zS :return: A PrimePoint object that is twice this point rr)rBrzryrJrcrKro)rrzryrrrrrrrs (zPrimePoint.double)N) rrrrrrrrrrrrrrroMs  + rol l 9{uDjSg9g(Bl +'1t:_|v!a:@ml H<^W]dZ{cxW\Iql 1(i&^#a;l?lF eY8 w-X"PVd/%PP!-l!"X!#BXtJ9!'|%VA-l4~ f&Dv@h!fE0m9_ qlM/l=*8%(?l?@lK`Opq^cv 3,e< 1U]>{|R*ZlB11e %:f=K`wrH7gHK8hklQ~o]l+fUg+<)Z?8O?q!OlQ%x +Ohbi+}s@l~l*'#.TEbc+Z'@=D 1 "(?7N2Z_+|S/1fl dxRjoyU8T( :ss"nZL8k&"_Ul_!uR/sX0 @qaNQNB&JxS8KJEY K%lls)e`gwl X_[nlv|l#l#?VQ(zO%b95~cte1oR{V;LH w>l-rZE]"Sr&Ga9}*Fl#f=xK)H-apY$3^Q n%k{;/K!u{4-{?$Od8V1l3s: l#Pf?QE$XN!85aZU WL9YLhz f$Du13otc!% pMxjRr`l# dp"z\}[z3"nZ;PK# `7roCQ); __future__rrrrr]rasysrZ_asn1rr r r r r rrrr_errorsr_typesrrutilrerrorsr version_infochrrxrangerangeZ_backend SystemError__all__r;ZCURVE_EXTRA_BITSrrrr r!rcrrorZSECP192R1_CURVEZSECP192R1_BASE_POINTZSECP224R1_CURVEZSECP224R1_BASE_POINTZSECP256R1_CURVEr<ZSECP384R1_CURVEr=ZSECP521R1_CURVEr>rrrrs 0     <;%%#&