o
    CZh}                      @  sf  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 dejd	ZG d
d deejd	ZG dd deejd	ZG dd deejd	ZG dd deejd	Zd4ddZd5ddZd6d d!Zd7d"d#ZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+eZG d,d- d-eZG d.d/ d/eZG d0d1 d1eZG d2d3 d3eeZdS )8    )annotationsN)utils)UnsupportedAlgorithm_Reasons)BlockCipherAlgorithmCipherAlgorithm)
algorithmsc                   @  s0   e Zd ZeejdddZejddd	Zd
S )Modereturnstrc                 C     dS )z@
        A string naming this mode (e.g. "ECB", "CBC").
        N selfr   r   c/var/www/html/lang_env/lib/python3.10/site-packages/cryptography/hazmat/primitives/ciphers/modes.pyname       z	Mode.name	algorithmr   Nonec                 C  r   )zq
        Checks that all the necessary invariants of this (mode, algorithm)
        combination are met.
        Nr   r   r   r   r   r   validate_for_algorithm   r   zMode.validate_for_algorithmN)r
   r   r   r   r
   r   )__name__
__module____qualname__propertyabcabstractmethodr   r   r   r   r   r   r	      s    r	   )	metaclassc                   @      e Zd ZeejdddZdS )ModeWithInitializationVectorr
   bytesc                 C  r   )zP
        The value of the initialization vector for this mode as bytes.
        Nr   r   r   r   r   initialization_vector#   r   z2ModeWithInitializationVector.initialization_vectorNr
   r!   )r   r   r   r   r   r   r"   r   r   r   r   r    "       r    c                   @  r   )ModeWithTweakr
   r!   c                 C  r   )z@
        The value of the tweak for this mode as bytes.
        Nr   r   r   r   r   tweak,   r   zModeWithTweak.tweakNr#   )r   r   r   r   r   r   r&   r   r   r   r   r%   +   r$   r%   c                   @  r   )ModeWithNoncer
   r!   c                 C  r   )z@
        The value of the nonce for this mode as bytes.
        Nr   r   r   r   r   nonce5   r   zModeWithNonce.nonceNr#   )r   r   r   r   r   r   r(   r   r   r   r   r'   4   r$   r'   c                   @  r   )ModeWithAuthenticationTagr
   bytes | Nonec                 C  r   )zP
        The value of the tag supplied to the constructor of this mode.
        Nr   r   r   r   r   tag>   r   zModeWithAuthenticationTag.tagNr
   r*   )r   r   r   r   r   r   r+   r   r   r   r   r)   =   r$   r)   r   r   r   r
   r   c                 C  s$   |j dkr|jdkrtdd S d S )N   ZAESz=Only 128, 192, and 256 bit keys are allowed for this AES mode)key_sizer   
ValueErrorr   r   r   r   _check_aes_key_lengthF   s
   r0   r   c                 C  s0   t | jd |jkrtdt | j| jd S )N   zInvalid IV size ({}) for {}.)lenr"   
block_sizer/   formatr   r   r   r   r   _check_iv_lengthM   s   r5   r(   r!   r   r   c                 C  sL   t |tst| dtjt| d |jkr$tdt|  d| dd S )N" requires a block cipher algorithmr1   zInvalid nonce size (z) for .)
isinstancer   r   r   UNSUPPORTED_CIPHERr2   r3   r/   )r(   r   r   r   r   r   _check_nonce_lengthX   s   
r:   c                 C  s4   t |tst|  dtjt| | t| | d S )Nr6   )r8   r   r   r   r9   r0   r5   r   r   r   r   _check_iv_and_key_lengthd   s   

r;   c                   @  ,   e Zd Zd Zd	ddZed
ddZeZdS )CBCr"   r!   c                 C     t d| || _d S Nr"   r   _check_byteslike_initialization_vectorr   r"   r   r   r   __init__s      
zCBC.__init__r
   c                 C     | j S NrB   r   r   r   r   r"   w      zCBC.initialization_vectorNr"   r!   r#   	r   r   r   r   rD   r   r"   r;   r   r   r   r   r   r=   p       
r=   c                   @  2   e Zd Zd ZdddZedddZdddZdS )XTSr&   r!   c                 C  s*   t d| t|dkrtd|| _d S )Nr&      z!tweak must be 128-bits (16 bytes))r   rA   r2   r/   _tweak)r   r&   r   r   r   rD      s   
zXTS.__init__r
   c                 C  rF   rG   )rP   r   r   r   r   r&      rI   z	XTS.tweakr   r   r   c                 C  s0   t |tjtjfrtd|jdvrtdd S )Nz\The AES128 and AES256 classes do not support XTS, please use the standard AES class instead.)r-   i   z\The XTS specification requires a 256-bit key for AES-128-XTS and 512-bit key for AES-256-XTS)r8   r   ZAES128ZAES256	TypeErrorr.   r/   r   r   r   r   r      s   
zXTS.validate_for_algorithmN)r&   r!   r#   r   )r   r   r   r   rD   r   r&   r   r   r   r   r   rN   ~   s    
rN   c                   @  s   e Zd Zd ZeZdS )ECBN)r   r   r   r   r0   r   r   r   r   r   rR      s    rR   c                   @  r<   )OFBr"   r!   c                 C  r>   r?   r@   rC   r   r   r   rD      rE   zOFB.__init__r
   c                 C  rF   rG   rH   r   r   r   r   r"      rI   zOFB.initialization_vectorNrJ   r#   rK   r   r   r   r   rS      rL   rS   c                   @  r<   )CFBr"   r!   c                 C  r>   r?   r@   rC   r   r   r   rD      rE   zCFB.__init__r
   c                 C  rF   rG   rH   r   r   r   r   r"      rI   zCFB.initialization_vectorNrJ   r#   rK   r   r   r   r   rT      rL   rT   c                   @  r<   )CFB8r"   r!   c                 C  r>   r?   r@   rC   r   r   r   rD      rE   zCFB8.__init__r
   c                 C  rF   rG   rH   r   r   r   r   r"      rI   zCFB8.initialization_vectorNrJ   r#   rK   r   r   r   r   rU      rL   rU   c                   @  rM   )CTRr(   r!   c                 C  r>   )Nr(   )r   rA   _nonce)r   r(   r   r   r   rD      rE   zCTR.__init__r
   c                 C  rF   rG   )rW   r   r   r   r   r(      rI   z	CTR.noncer   r   r   c                 C  s   t | | t| j| j| d S rG   )r0   r:   r(   r   r   r   r   r   r      s   
zCTR.validate_for_algorithmN)r(   r!   r#   r   )r   r   r   r   rD   r   r(   r   r   r   r   r   rV      s    
rV   c                   @  sN   e Zd Zd ZdZdZ		ddddZedddZedddZ	dddZ
dS )GCMl   ? l            NrO   r"   r!   r+   r*   min_tag_lengthintc                 C  s   t d| t|dk st|dkrtd|| _|d ur8t d| |dk r+tdt||k r8td||| _|| _d S )	Nr"   r1      zIinitialization_vector must be between 8 and 128 bytes (64 and 1024 bits).r+      zmin_tag_length must be >= 4z.Authentication tag must be {} bytes or longer.)	r   rA   r2   r/   rB   _check_bytesr4   _tagZ_min_tag_length)r   r"   r+   rY   r   r   r   rD      s$   
zGCM.__init__r
   c                 C  rF   rG   )r^   r   r   r   r   r+      rI   zGCM.tagc                 C  rF   rG   rH   r   r   r   r   r"      rI   zGCM.initialization_vectorr   r   r   c                 C  sX   t | | t|tstdtj|jd }| jd ur(t| j|kr*t	d
|d S d S )Nz%GCM requires a block cipher algorithmr1   z0Authentication tag cannot be more than {} bytes.)r0   r8   r   r   r   r9   r3   r^   r2   r/   r4   )r   r   Zblock_size_bytesr   r   r   r     s   


zGCM.validate_for_algorithm)NrO   )r"   r!   r+   r*   rY   rZ   r,   r#   r   )r   r   r   r   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESrD   r   r+   r"   r   r   r   r   r   rX      s    rX   )r   r	   r   r   r
   r   )r   r    r   r   r
   r   )r(   r!   r   r   r   r   r
   r   )r   r    r   r   r
   r   )
__future__r   r   Zcryptographyr   Zcryptography.exceptionsr   r   Z/cryptography.hazmat.primitives._cipheralgorithmr   r   Z&cryptography.hazmat.primitives.ciphersr   ABCMetar	   r    r%   r'   r)   r0   r5   r:   r;   r=   rN   rR   rS   rT   rU   rV   rX   r   r   r   r   <module>   s.   			
	


