
    Wwg                      .   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
 d dlmZ  G d dej                  	      Z G d
 deej                  	      Z G d deej                  	      Z G d deej                  	      Z G d deej                  	      Zd&dZ	 	 	 	 	 	 d'dZ	 	 	 	 	 	 	 	 d(dZ	 	 	 	 	 	 d)dZ G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d  d!e      Z G d" d#e      Z G d$ d%ee      Zy)*    )annotationsN)utils)UnsupportedAlgorithm_Reasons)BlockCipherAlgorithmCipherAlgorithm)
algorithmsc                  b    e Zd Zeej
                  dd              Zej
                  dd       Zy)Modec                     y)z@
        A string naming this mode (e.g. "ECB", "CBC").
        N selfs    c/var/www/horilla/myenv/lib/python3.12/site-packages/cryptography/hazmat/primitives/ciphers/modes.pynamez	Mode.name           c                     y)zq
        Checks that all the necessary invariants of this (mode, algorithm)
        combination are met.
        Nr   r   	algorithms     r   validate_for_algorithmzMode.validate_for_algorithm   r   r   N)returnstrr   r   r   None)__name__
__module____qualname__propertyabcabstractmethodr   r   r   r   r   r   r      s<      
 	 r   r   )	metaclassc                  <    e Zd Zeej
                  dd              Zy)ModeWithInitializationVectorc                     y)zP
        The value of the initialization vector for this mode as bytes.
        Nr   r   s    r   initialization_vectorz2ModeWithInitializationVector.initialization_vector#   r   r   Nr   bytes)r   r   r   r   r    r!   r&   r   r   r   r$   r$   "   "      r   r$   c                  <    e Zd Zeej
                  dd              Zy)ModeWithTweakc                     y)z@
        The value of the tweak for this mode as bytes.
        Nr   r   s    r   tweakzModeWithTweak.tweak,   r   r   Nr'   )r   r   r   r   r    r!   r-   r   r   r   r+   r+   +   r)   r   r+   c                  <    e Zd Zeej
                  dd              Zy)ModeWithNoncec                     y)z@
        The value of the nonce for this mode as bytes.
        Nr   r   s    r   noncezModeWithNonce.nonce5   r   r   Nr'   )r   r   r   r   r    r!   r1   r   r   r   r/   r/   4   r)   r   r/   c                  <    e Zd Zeej
                  dd              Zy)ModeWithAuthenticationTagc                     y)zP
        The value of the tag supplied to the constructor of this mode.
        Nr   r   s    r   tagzModeWithAuthenticationTag.tag>   r   r   Nr   bytes | None)r   r   r   r   r    r!   r5   r   r   r   r3   r3   =   r)   r   r3   c                X    |j                   dkD  r|j                  dk(  rt        d      y y )N   AESz=Only 128, 192, and 256 bit keys are allowed for this AES mode)key_sizer   
ValueErrorr   s     r   _check_aes_key_lengthr=   F   s3    CINNe$;K
 	
 %<r   c                    t        | j                        }|dz  |j                  k7  rt        d| d| j                   d      y )N   zInvalid IV size () for .)lenr&   
block_sizer<   r   )r   r   iv_lens      r   _check_iv_lengthrE   M   sJ     ++,FzY))),VHF499+QGHH *r   c                    t        |t              st        | dt        j                        t        |       dz  |j                  k7  rt        dt        |        d| d      y )N" requires a block cipher algorithmr?   zInvalid nonce size (r@   rA   )
isinstancer   r   r   UNSUPPORTED_CIPHERrB   rC   r<   )r1   r   r   s      r   _check_nonce_lengthrJ   U   sl     i!56"f67''
 	
 5zA~---/E
|6$qIJJ .r   c                    t        |t              st        |  dt        j                        t        | |       t        | |       y )NrG   )rH   r   r   r   rI   r=   rE   r   s     r   _check_iv_and_key_lengthrL   a   sF     i!56"f67''
 	
 $	*T9%r   c                  .    e Zd Zd ZddZedd       ZeZy)CBCc                >    t        j                  d|       || _        y Nr&   r   _check_byteslike_initialization_vectorr   r&   s     r   __init__zCBC.__init__p       68MN&;#r   c                    | j                   S NrS   r   s    r   r&   zCBC.initialization_vectort       ***r   Nr&   r(   r'   	r   r   r   r   rU   r   r&   rL   r   r   r   r   rN   rN   m   (    D< + + 6r   rN   c                  2    e Zd Zd ZddZedd       ZddZy)XTSc                p    t        j                  d|       t        |      dk7  rt        d      || _        y )Nr-      z!tweak must be 128-bits (16 bytes))r   rR   rB   r<   _tweak)r   r-   s     r   rU   zXTS.__init__~   s1    w.u:@AAr   c                    | j                   S rX   )rb   r   s    r   r-   z	XTS.tweak       {{r   c                    t        |t        j                  t        j                  f      rt	        d      |j
                  dvrt        d      y )Nz\The AES128 and AES256 classes do not support XTS, please use the standard AES class instead.)r9   i   z\The XTS specification requires a 256-bit key for AES-128-XTS and 512-bit key for AES-256-XTS)rH   r	   AES128AES256	TypeErrorr;   r<   r   s     r   r   zXTS.validate_for_algorithm   sV    i*"3"3Z5F5F!GH2 
 Z/3  0r   N)r-   r(   r'   r   )r   r   r   r   rU   r   r-   r   r   r   r   r_   r_   {   s%    D  r   r_   c                      e Zd Zd ZeZy)ECBN)r   r   r   r   r=   r   r   r   r   rj   rj      s    D2r   rj   c                  .    e Zd Zd ZddZedd       ZeZy)OFBc                >    t        j                  d|       || _        y rP   rQ   rT   s     r   rU   zOFB.__init__   rV   r   c                    | j                   S rX   rY   r   s    r   r&   zOFB.initialization_vector   rZ   r   Nr[   r'   r\   r   r   r   rl   rl      r]   r   rl   c                  .    e Zd Zd ZddZedd       ZeZy)CFBc                >    t        j                  d|       || _        y rP   rQ   rT   s     r   rU   zCFB.__init__   rV   r   c                    | j                   S rX   rY   r   s    r   r&   zCFB.initialization_vector   rZ   r   Nr[   r'   r\   r   r   r   rp   rp      r]   r   rp   c                  .    e Zd Zd ZddZedd       ZeZy)CFB8c                >    t        j                  d|       || _        y rP   rQ   rT   s     r   rU   zCFB8.__init__   rV   r   c                    | j                   S rX   rY   r   s    r   r&   zCFB8.initialization_vector   rZ   r   Nr[   r'   r\   r   r   r   rt   rt      s(    D< + + 6r   rt   c                  2    e Zd Zd ZddZedd       ZddZy)CTRc                >    t        j                  d|       || _        y )Nr1   )r   rR   _nonce)r   r1   s     r   rU   zCTR.__init__   s    w.r   c                    | j                   S rX   )rz   r   s    r   r1   z	CTR.nonce   rd   r   c                ^    t        | |       t        | j                  | j                  |       y rX   )r=   rJ   r1   r   r   s     r   r   zCTR.validate_for_algorithm   s     dI.DJJ		9=r   N)r1   r(   r'   r   )r   r   r   r   rU   r   r1   r   r   r   r   rx   rx      s%    D  >r   rx   c                  \    e Zd Zd ZdZdZ	 	 d	 	 	 	 	 d	dZed
d       Zedd       Z	ddZ
y)GCMl   ? l            Nc                2   t        j                  d|       t        |      dk  st        |      dkD  rt        d      || _        |Ct        j
                  d|       |dk  rt        d      t        |      |k  rt        d| d	      || _        || _        y )
Nr&   r?      zIinitialization_vector must be between 8 and 128 bytes (64 and 1024 bits).r5      zmin_tag_length must be >= 4zAuthentication tag must be z bytes or longer.)r   rR   rB   r<   rS   _check_bytes_tag_min_tag_length)r   r&   r5   min_tag_lengths       r   rU   zGCM.__init__   s     	68MN$%)S1F-G#-M"  '<#?uc*! !>??3x.( 1.1A B   	-r   c                    | j                   S rX   )r   r   s    r   r5   zGCM.tag   s    yyr   c                    | j                   S rX   rY   r   s    r   r&   zGCM.initialization_vector   rZ   r   c                    t        | |       t        |t              st        dt        j
                        |j                  dz  }| j                  (t        | j                        |kD  rt        d| d      y y )Nz%GCM requires a block cipher algorithmr?   z'Authentication tag cannot be more than z bytes.)
r=   rH   r   r   r   rI   rC   r   rB   r<   )r   r   block_size_bytess      r   r   zGCM.validate_for_algorithm   s    dI.)%9:&7++  %//1499 S^6F%F9:J9K L   &G r   )Nra   )r&   r(   r5   r7   r   intr6   r'   r   )r   r   r   r   _MAX_ENCRYPTED_BYTES_MAX_AAD_BYTESrU   r   r5   r&   r   r   r   r   r~   r~      sh    D-!N
 ! 	.$. . 	.6   + +r   r~   )r   r   r   r   r   r   )r   r$   r   r   r   r   )r1   r(   r   r   r   r   r   r   )r   r$   r   r   r   r   )
__future__r   r    cryptographyr   cryptography.exceptionsr   r   /cryptography.hazmat.primitives._cipheralgorithmr   r   &cryptography.hazmat.primitives.ciphersr	   ABCMetar   r$   r+   r/   r3   r=   rE   rJ   rL   rN   r_   rj   rl   rp   rt   rx   r~   r   r   r   <module>r      sJ  
 # 
  B >S[[  43;; DCKK DCKK  
I
&I3GI	I	K	K	K(7	K		K	&
&	&3B	&		&6
& 6- :3$ 36
& 66
& 66' 6>- > 4
&(A 4r   