U af]O@s<ddlmZmZmZmZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZmZddlmZmZmZmZmZddlmZddlmZdd lmZmZmZejd kreZ e!Z"nd d Z e Z#d ddddddgZ$ddZ%dd Z&ddZ'ddZ(ddZ)ddZ*ddZ+ddZ,dd Z-d!dZ.d"dZ/dS)#)unicode_literalsdivisionabsolute_importprint_functionN)backend)constant_compare rand_bytes) Certificateint_from_bytes int_to_bytesPrivateKeyInfo PublicKeyInfo)pretty_message) fill_width) type_namebyte_cls int_types)cCs t|gS)N)bytes)numr@/opt/nydus/tmp/pip-target-53d1vnqk/lib/python/oscrypto/_pkcs1.pychr_clssradd_pss_paddingadd_pkcs1v15_signature_paddingraw_rsa_private_cryptraw_rsa_public_crypt"remove_pkcs1v15_encryption_padding!remove_pkcs1v15_signature_paddingverify_pss_paddingcCs:tjdkrdStd}ttt|ddddkS)zM :return: A bool if the current machine is running OS X 10.7 darwinFr.) )sysplatformmac_vertuplemapintsplit)versionrrr _is_osx_107/s  r.cCstdkrtjdkrttdt|ts:ttdt|t|t sVttdt||dkrpt tdt |t|t sttdt||d krt td t ||t d d d ddgkrt tdt |t t|}|d}tt|d}||}t|}|||dkr$t td|dkr:t|} nd} d|| } || } d|||d} | d| } t|| ||d}tt| t|A}t|t|}d||}d|dd|}t|d}|dkrt|t|dd@|dd}|| dS)a5 Pads a byte string using the EMSA-PSS-Encode operation described in PKCS#1 v2.2. :param hash_algorithm: The string name of the hash algorithm to use: "sha1", "sha224", "sha256", "sha384", "sha512" :param salt_length: The length of the salt as an integer - typically the same as the length of the output from the hash_algorithm :param key_length: The length of the RSA key, in bits :param message: A byte string of the message to pad :return: The encoded (passed) message winlegacyr!z~ Pure-python RSA PSS signature padding addition code is only for Windows XP/2003 and OS X ? message must be a byte string, not %s @ salt_length must be an integer, not %s r? salt_length must be 0 or more - is %s ? key_length must be an integer, not %s iz@ key_length must be 512 or more - is %s sha1sha224sha256sha384sha512z hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384", "sha512", not %s rr#zq Key is not long enough to use with specified hash_algorithm and salt_length 01N)_backendr&r' SystemErrorr isinstancer TypeErrorrr ValueErrorreprsetgetattrhashlibr+mathceildigestlenosurandom_mgf1r r rrord)hash_algorithm salt_length key_lengthmessage hash_funcem_bitsem_lenmessage_digest hash_lengthsaltm_primem_prime_digestpaddingdbdb_mask masked_db zero_bits left_bit_mask left_int_maskrrrr;sv             $cCstdkrtjdkrttdt|ts:ttdt|t|tsVttdt|t|t srttdt||dkrt tdt ||t d d d d d gkrt tdt |t t|}|d}tt|d}||}t|} || |dkrdS|dddkrdSd||} || d} |d| } t| dd} | d| ?}|dkrfdS|| | | }t|||| d}d| dd| }t|d}|dkrt|t|dd@|dd}tt| t|A}t|t| krdt| t||}|| |d}d|}t|d||sDdS|||ddkr^dS|d|d}d||}||}t||S)aZ Verifies the PSS padding on an encoded message :param hash_algorithm: The string name of the hash algorithm to use: "sha1", "sha224", "sha256", "sha384", "sha512" :param salt_length: The length of the salt as an integer - typically the same as the length of the output from the hash_algorithm :param key_length: The length of the RSA key, in bits :param message: A byte string of the message to pad :param signature: The signature to verify :return: A boolean indicating if the signature is invalid r/r!z Pure-python RSA PSS signature padding verification code is only for Windows XP/2003 and OS X r0zA signature must be a byte string, not %s r1rr2r4r5r6r7r8r9rr:r#FNrBr?r@rAr=r>r<)rCr&r'rDrrErrFrrrGrHrIrJrKr+rLrMrNrOrSrRrr r r)rTrUrVrW signaturerXrYrZr[r\rdZmasked_db_lengthrcZ first_byteZbits_that_should_be_zeror_rbrerfraZ zero_lengthZ zero_stringr]r^Zh_primerrrr s            $  c Cst|tsttdt|t|ts8ttdt||dkrRttdt||tddddd gkrzttd t|d }d d dddd|}t t ||}t dj}tt|}td|D] }||} |||| 7}q|d|S)a| The PKCS#1 MGF1 mask generation algorithm :param hash_algorithm: The string name of the hash algorithm to use: "sha1", "sha224", "sha256", "sha384", "sha512" :param seed: A byte string to use as the seed for the mask :param mask_length: The desired mask length, as an integer :return: A byte string of the mask z< seed must be a byte string, not %s z@ mask_length must be an integer, not %s rzD mask_length must be greater than 0 - is %s r4r5r6r7r8r9r; 0@)r4r5r6r7r8s>Ir)rErrFrrrrGrHrIr+rLrMstructStructpackrJrKrangerN) rTseedZ mask_lengthoutputr\Z iterationsrprXcounterbrrrrR:sH    rRcCs tdkrttdt||dS)z Adds PKCS#1 v1.5 padding to a message to be signed :param key_length: An integer of the number of bytes in the key :param data: A byte string to pad :return: The padded data as a byte string r/zz Pure-python RSA PKCSv1.5 signature padding addition code is only for Windows XP/2003 Zsigning)rCrDr_add_pkcs1v15_paddingrVdatarrrrs cCs tdkrttdt||dS)a Removes PKCS#1 v1.5 padding from a signed message using constant time operations :param key_length: An integer of the number of bytes in the key :param data: A byte string to unpad :return: The unpadded data as a byte string r/zy Pure-python RSA PKCSv1.5 signature padding removal code is only for Windows XP/2003 Z verifying)rCrDr_remove_pkcs1v15_paddingrwrrrrs cCststtdt||dS)a Removes PKCS#1 v1.5 padding from a decrypted message using constant time operations :param key_length: An integer of the number of bytes in the key :param data: A byte string to unpad :return: The unpadded data as a byte string zt Pure-python RSA PKCSv1.5 encryption padding removal code is only for OS X 10.7 decrypting)r.rDrryrwrrrrs cCs|dkrd}nd}t|ts.ttdt|t|tsJttdt||dkrdttdt|t||dkrttd |dt||d t|}d }|d krt |}d | d }||7}|t|8}qd ||d |S)a1 Adds PKCS#1 v1.5 padding to a message :param key_length: An integer of the number of bytes in the key :param data: A byte string to unpad :param operation: A unicode string of "encrypting" or "signing" :return: The padded data as a byte string Z encryptingr>< data must be a byte string, not %s r3rm? key_length must be 64 or more - is %s zJ data must be between 1 and %s bytes long - is %s rr;rr=) rErrFrrrrGrHrOr joinr,)rVrx operation second_byteZrequired_bytesr`Z temp_paddingrrrrvs@  rvc Csn|dkrd}nd}t|ts.ttdt|t|tsJttdt||dkrdttdt|t||kr|td|d }d }d }t d t|D]}|||d}t |} |d kr|| O}q|dkr|t | |B|kO}q|d kr|t | d Ad kO}q| d B} |d kr.| r$||O}n||O}q| r>||O}q||O}q|d kr^td|||dd S) aY Removes PKCS#1 v1.5 padding from a message using constant time operations :param key_length: An integer of the number of bytes in the key :param data: A byte string to unpad :param operation: A unicode string of "decrypting" or "verifying" :return: The unpadded data as a byte string rzr#rr|r3rmr}zError %srr$N) rErrFrrrrGrHrOrqrSr+) rVrxrrerrorZtrashZ padding_endibyteZbyte_numZnon_zerorrrrysV             rycCstdkrtdt|dr&t|jts8ttdt||jddj }|dkrj|dkrjt td | t|t sttd t||jd j }tt||d j |d j }t||jjdS)aa Performs a raw RSA algorithm in a byte string using a private key. This is a low-level primitive and is prone to disastrous results if used incorrectly. :param private_key: An oscrypto.asymmetric.PrivateKey object :param data: A byte string of the plaintext to be signed or ciphertext to be decrypted. Must be less than or equal to the length of the private key. In the case of signing, padding must already be applied. In the case of decryption, padding must be removed afterward. :return: A byte string of the transformed data r/1Pure-python RSA crypt is only for Windows XP/2003asn1zy private_key must be an instance of the oscrypto.asymmetric.PrivateKey class, not %s Zprivate_key_algorithm algorithmrsa rsassa_pssz@ private_key must be an RSA key, not %s r| private_keyZprivate_exponentmoduluswidth)rCrDhasattrrErr rFrrnativerGupperrparsedpowr r byte_size)rrxalgoZrsa_private_keytransformed_intrrrrns2  cCstdkrtdt|d}ttf}|r2t|j|sDttdt ||jddj }|dkrv|dkrvt td| t|t sttd t ||jd j}tt||d j |d j }t||jjd S)a Performs a raw RSA algorithm in a byte string using a certificate or public key. This is a low-level primitive and is prone to disastrous results if used incorrectly. :param certificate_or_public_key: An oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate object :param data: A byte string of the signature when verifying, or padded plaintext when encrypting. Must be less than or equal to the length of the public key. When verifying, padding will need to be removed afterwards. When encrypting, padding must be applied before. :return: A byte string of the transformed data r/rrz certificate_or_public_key must be an instance of the oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate classes, not %s rrrzN certificate_or_public_key must be an RSA key, not %s r|Z public_keyZpublic_exponentrr)rCrDrrr rErrFrrrrGrrrrr r r)Zcertificate_or_public_keyrxZhas_asn1Z valid_typesrZrsa_public_keyrrrrrs<    )0 __future__rrrrr&rKrLr'rnrPrutilrr Z_asn1r r r r r_errorsr_intr_typesrrr version_infochrrxrangerqrC__all__r.rr rRrrrrvryrrrrrrsJ      ~IC[9