
    Bwg]O                     6   d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
ddlmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZmZ ej<                  d
k  reZ e!Z"nd Z  e       Z#g dZ$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/y)    )unicode_literalsdivisionabsolute_importprint_functionN   )backend)constant_compare
rand_bytes)Certificateint_from_bytesint_to_bytesPrivateKeyInfoPublicKeyInfo)pretty_message)
fill_width)	type_namebyte_cls	int_types)   c                     t        | g      S )N)bytes)nums    F/var/www/horilla/myenv/lib/python3.12/site-packages/oscrypto/_pkcs1.pychr_clsr      s    cU|    )add_pss_paddingadd_pkcs1v15_signature_paddingraw_rsa_private_cryptraw_rsa_public_crypt"remove_pkcs1v15_encryption_padding!remove_pkcs1v15_signature_paddingverify_pss_paddingc                      t         j                  dk7  ryt        j                         d   } t        t	        t
        | j                  d                  dd dk(  S )zM
    :return:
        A bool if the current machine is running OS X 10.7
    darwinFr   .   )
      )sysplatformmac_vertuplemapintsplit)versions    r   _is_osx_107r1   /   sM     ||x #GS'--,-.q3w>>r   c                    t         dk7  r't        j                  dk7  rt        t	        d            t        |t              st        t	        dt        |                  t        |t              st        t	        dt        |                  |dk  rt        t	        dt        |                  t        |t              st        t	        dt        |                  |d	k  rt        t	        d
t        |                  | t        g d      vrt        t	        dt        |                   t        t        |       }|dz
  }t        t!        j"                  |dz              } ||      j%                         }t'        |      }|||z   dz   k  rt        t	        d            |dkD  rt)        j*                  |      }	nd}	d|z   |	z   }
 ||
      j%                         }d||z
  |z
  dz
  z  }|dz   |	z   }t-        | |||z
  dz
        }t/        t1        |      t1        |      z        }t3        |t'        |            }d|z  |z
  }d|z  dd|z
  z  z   }t        |d      }|dk7  r t5        |t7        |dd       z        |dd z   }||z   dz   S )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
            i   z@
            key_length must be 512 or more - is %s
            sha1sha224sha256sha384sha512z
            hash_algorithm must be one of "sha1", "sha224", "sha256", "sha384",
            "sha512", not %s
            r      r&   zq
            Key is not long enough to use with specified hash_algorithm and
            salt_length
            r                     01   N   )_backendr)   r*   SystemErrorr   
isinstancer   	TypeErrorr   r   
ValueErrorreprsetgetattrhashlibr.   mathceildigestlenosurandom_mgf1r   r   r   r   ord)hash_algorithmsalt_length
key_lengthmessage	hash_funcem_bitsem_lenmessage_digesthash_lengthsaltm_primem_prime_digestpaddingdbdb_mask	masked_db	zero_bitsleft_bit_maskleft_int_masks                      r   r   r   ;   s   . ;3<<8#;.
  	 gx( g	
  	 k9- k"	
  	 Q 	
  	 j), j!	
  	 C 	
  	 S!QRR  
  	 0I 1nG7Q;'(Fw'..0Nn%Kk)A--
  	 Qzz+&n,t3Gw'..0N+-;a?@G	7	T	!BNNF[4H14LMG^B/.2IIJI9c'l3IVw&I9_I)>?Mq)MMC	!A,??@9QR=P	~%//r   c                 h   t         dk7  r't        j                  dk7  rt        t	        d            t        |t              st        t	        dt        |                  t        |t              st        t	        dt        |                  t        |t              st        t	        dt        |                  |dk  rt        t	        dt        |                  | t        g d	      vrt        t	        d
t        |                   t        t        |       }|dz
  }t        t!        j"                  |dz              } ||      j%                         }t'        |      }	||	|z   dz   k  ry|dd dk7  ryd|z  |z
  }
||	z
  dz
  }|d| }t)        |dd       }|d|
z
  z	  }|dk7  ry||||	z    }t+        | |||	z
  dz
        }d|
z  dd|
z
  z  z   }t        |d      }|dk7  r t-        |t)        |dd       z        |dd z   }t/        t1        |      t1        |      z        }t'        |      t'        |      k  rdt'        |      t'        |      z
  z  |z   }||	z
  |z
  dz
  }d|z  }t3        |d| |      sy|||dz    dk7  ry|d|z
  d }d|z   |z   } ||      j%                         }t3        ||      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
    r3   r$   z
            Pure-python RSA PSS signature padding verification code is only for
            Windows XP/2003 and OS X
            r4   zA
            signature must be a byte string, not %s
            r5   r   r6   r8   r>   r   r?   r&   FNrF   rC   rD   rE   rA   rB   r@   )rG   r)   r*   rH   r   rI   r   rJ   r   r   rK   rL   rM   rN   rO   r.   rP   rQ   rR   rS   rW   rV   r   r   r   r	   )rX   rY   rZ   r[   	signaturer\   r]   r^   r_   r`   rh   masked_db_lengthrg   
first_bytebits_that_should_be_zerorc   rf   ri   rj   re   zero_lengthzero_stringra   rb   h_primes                            r   r"   r"      s*   2 ;3<<8#;.
  	 gx( g	
  	 i* i 	
  	 k9- k"	
  	 Q 	
  	 S!QRR  
  	 0I1nG7Q;'(Fw'..0Nn%Kk)A--~ Vw&I+a/!,-IYq^$J)a)m<1$/0@;0NONNNF[4H14LMG9_I)>?Mq)M-#gal*;;<wqr{J	nY/.2II	JB
2wYY#b'12b8;&4q8KK'KBq-{;	+kAo&'1a+oDn,t3G '')GNG44r   c                    t        |t              st        t        dt	        |                  t        |t
              st        t        dt	        |                  |dk  rt        t        dt        |                  | t        g d      vrt        t        dt        |                   d}dd	d
ddd|    }t        t        j                  ||z              }t        j                  d      j                  }t        t         |       }t#        d|      D ]&  } ||      }	| |||	z         j%                         z  }( |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
            r   zD
            mask_length must be greater than 0 - is %s
            r8   r>   r             0   @   s   >Ir   )rI   r   rJ   r   r   r   rK   rL   rM   r.   rP   rQ   structStructpackrN   rO   rangerR   )
rX   seedmask_lengthoutputr`   
iterationsr|   r\   counterbs
             r   rV   rV   :  sk   $ dH% dO	
  	 k9- k"	
  	 Q 	
  	 S!QRR  
  	 F  K TYY{[89:J==$$D0IJ' /M)D1H%,,../ !K  r   c                 V    t         dk7  rt        t        d            t        | |d      S )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
    r3   zz
            Pure-python RSA PKCSv1.5 signature padding addition code is only
            for Windows XP/2003
            signing)rG   rH   r   _add_pkcs1v15_paddingrZ   datas     r   r   r     s6     ;.
  	 !T9==r   c                 V    t         dk7  rt        t        d            t        | |d      S )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
    r3   zy
            Pure-python RSA PKCSv1.5 signature padding removal code is only for
            Windows XP/2003
            	verifying)rG   rH   r   _remove_pkcs1v15_paddingr   s     r   r!   r!     s6     ;.
  	 $JkBBr   c                 X    t               st        t        d            t        | |d      S )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)r1   rH   r   r   r   s     r   r    r      s3     =.
  	 $JlCCr   c           	      N   |dk(  rd}nd}t        |t              st        t        dt	        |                  t        | t
              st        t        dt	        |                   | dk  rt        t        dt        |                   t        |      | dz
  kD  r"t        t        d	| dz
  t        |                  | d
z
  t        |      z
  }d}|dkD  rDt        |      }dj                  |j                  d            }||z  }|t        |      z  }|dkD  rDd|z   |z   dz   |z   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
    
encrypting   rB   <
            data must be a byte string, not %s
            r7   ry   ?
            key_length must be 64 or more - is %s
               zJ
            data must be between 1 and %s bytes long - is %s
            r   r   r   rA   )rI   r   rJ   r   r   r   rK   rL   rS   r
   joinr/   )rZ   r   	operationsecond_byterequired_bytesrd   temp_paddings          r   r   r     s[   " L dH% dO	
  	 j), j!	
  	 B 	
  	 4y:?" OI
  	  !^c$i/NG
1
!.1xx 2 27 ;<<#l++ 1
 [ 7*W4t;;r   c                    |dk(  rd}nd}t        |t              st        t        dt	        |                  t        | t
              st        t        dt	        |                   | dk  rt        t        dt        |                   t        |      | k7  rt        d|z        d	}d	}d	}t        d	t        |            D ]y  }|||dz    }t        |      }	|d	k(  r||	z  }!|dk(  r|t        |	|z  |k7        z  };|d
k  r|t        |	d	z  d	k(        z  }U|	d	z  }
|d	k(  r|
r||z  }g||z  }m|
r||z  }u||z  }{ |d	k7  rt        d|z        ||dz   d 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
    r   r&   r   r   r7   ry   r   zError %sr   r'   N)rI   r   rJ   r   r   r   rK   rL   rS   r}   rW   r.   )rZ   r   r   r   errortrashpadding_endibytebyte_numnon_zeros              r   r   r     s   " L dH% dO	
  	 j), j!	
  	 B 	
  	 4yJi/00EEK 1c$i  Aa!e}t9 6XE !VS([0[@AAE VS(Q,1,--E  !|HaQJE1$KQJEQJE9< zi/00a !!r   c                 t   t         dk7  rt        d      t        | d      rt        | j                  t
              st        t        dt        |                   | j                  d   d   j                  }|dk7  r(|dk7  r#t        t        d	|j                                     t        |t              st        t        d
t        |                  | j                  d   j                  }t        t        |      |d   j                  |d   j                        }t!        || j                  j"                        S )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
    r3   1Pure-python RSA crypt is only for Windows XP/2003asn1zy
            private_key must be an instance of the
            oscrypto.asymmetric.PrivateKey class, not %s
            private_key_algorithm	algorithmrsa
rsassa_pssz@
            private_key must be an RSA key, not %s
            r   private_keyprivate_exponentmoduluswidth)rG   rH   hasattrrI   r   r   rJ   r   r   nativerK   upperr   parsedpowr   r   	byte_size)r   r   algorsa_private_keytransformed_ints        r   r   r   n  s4   & ;MNN;'z+:J:JN/[ k"
  	 34[AHHDu}- JJL	
  	 dH% dO	
  	 "&&}5<<Ot*+22	"))O
 {/?/?/I/IJJr   c                    t         dk7  rt        d      t        | d      }t        t        f}|rt        | j                  |      st        t        dt        |                   | j                  d   d   j                  }|dk7  r(|dk7  r#t        t        d|j                                     t        |t              st        t        d	t        |                  | j                  d
   j                  }t        t!        |      |d   j                  |d   j                        }t#        || j                  j$                        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
    r3   r   r   z
            certificate_or_public_key must be an instance of the
            oscrypto.asymmetric.PublicKey or oscrypto.asymmetric.Certificate
            classes, not %s
            r   r   r   zN
            certificate_or_public_key must be an RSA key, not %s
            r   
public_keypublic_exponentr   r   )rG   rH   r   r   r   rI   r   rJ   r   r   r   rK   r   r   r   r   r   r   r   )certificate_or_public_keyr   has_asn1valid_typesr   rsa_public_keyr   s          r   r   r     sK   ( ;MNN0&9H +.K:&?&D&DkR
 /0
  	 %))+6{CJJDu}- JJL	
  	 dH% dO	
  	 /33LAHHNt()00y!((O
 ',,66 r   )0
__future__r   r   r   r   r)   rO   rP   r*   rz   rT    r   utilr	   r
   _asn1r   r   r   r   r   _errorsr   _intr   _typesr   r   r   version_infochrr   xranger}   rG   __all__r1   r   r"   rV   r   r!   r    r   r   r   r    r   r   <module>r      s    R R 
     	  .  $  2 2dGE 9	?{0|~5BF!R>2C4D4@<FX"v6Kr=r   