U
    /jw                     @  sP  d dl mZ d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZmZmZ d dlm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eZeje
edej dd ejeedej dd eje	edej dd d S )!    )annotations)utils)UnsupportedAlgorithm_Reasons)CFB)CFB8)OFB)BlockCipherAlgorithmCipherAlgorithm)Mode)ModeWithAuthenticationTag)ModeWithInitializationVector)ModeWithNonce)ModeWithTweak)_check_aes_key_length_check_iv_and_key_length_check_nonce_length)
algorithmsc                   @  s4   e Zd Zd ZddddZeddddZeZdS )	CBCutils.Buffer)initialization_vectorc                 C  s   t d| || _d S )Nr   )r   _check_byteslike_initialization_vector)selfr    r   P/tmp/pip-unpacked-wheel-qzbw3lpx/cryptography/hazmat/primitives/ciphers/modes.py__init__*   s    zCBC.__init__returnc                 C  s   | j S Nr   r   r   r   r   r   .   s    zCBC.initialization_vectorN)	__name__
__module____qualname__namer   propertyr   r   validate_for_algorithmr   r   r   r   r   '   s
   r   c                   @  s@   e Zd Zd ZddddZeddddZdd	d
ddZdS )XTSr   )tweakc                 C  s*   t d| t|dkr td|| _d S )Nr)      z!tweak must be 128-bits (16 bytes))r   r   len
ValueError_tweak)r   r)   r   r   r   r   8   s    zXTS.__init__r   c                 C  s   | j S r   )r-   r!   r   r   r   r)   @   s    z	XTS.tweakr
   None	algorithmr   c                 C  s0   t |tjtjfrtd|jdkr,tdd S )Nz\The AES128 and AES256 classes do not support XTS, please use the standard AES class instead.)   i   z\The XTS specification requires a 256-bit key for AES-128-XTS and 512-bit key for AES-256-XTS)
isinstancer   ZAES128ZAES256	TypeErrorZkey_sizer,   r   r0   r   r   r   r'   D   s    
zXTS.validate_for_algorithmN)r"   r#   r$   r%   r   r&   r)   r'   r   r   r   r   r(   5   s
   r(   c                   @  s   e Zd Zd ZeZdS )ECBN)r"   r#   r$   r%   r   r'   r   r   r   r   r5   R   s   r5   c                   @  s@   e Zd Zd ZddddZeddddZdd	d
ddZdS )CTRr   )noncec                 C  s   t d| || _d S )Nr7   )r   r   _nonce)r   r7   r   r   r   r   [   s    zCTR.__init__r   c                 C  s   | j S r   )r8   r!   r   r   r   r7   _   s    z	CTR.noncer
   r.   r/   c                 C  s   t | | t| j| j| d S r   )r   r   r7   r%   r4   r   r   r   r'   c   s    
zCTR.validate_for_algorithmN)r"   r#   r$   r%   r   r&   r7   r'   r   r   r   r   r6   X   s
   r6   c                   @  s`   e Zd Zd ZdZdZdddddd	d
ZeddddZeddddZ	dddddZ
dS )GCMl   ? l            Nr*   r   zbytes | Noneint)r   tagmin_tag_lengthc                 C  s   t d| t|dk s$t|dkr,td|| _|d k	rrt d| |dk rVtdt||k rrtd| d	|| _|| _d S )
Nr         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 z bytes or longer.)r   r   r+   r,   r   _check_bytes_tagZ_min_tag_length)r   r   r;   r<   r   r   r   r   m   s     
zGCM.__init__r   c                 C  s   | j S r   )rA   r!   r   r   r   r;      s    zGCM.tagc                 C  s   | j S r   r    r!   r   r   r   r      s    zGCM.initialization_vectorr
   r.   r/   c                 C  sV   t | | t|ts tdtj|jd }| jd k	rRt| j|krRt	d| dd S )Nz%GCM requires a block cipher algorithmr=   z'Authentication tag cannot be more than z bytes.)
r   r2   r	   r   r   ZUNSUPPORTED_CIPHER
block_sizerA   r+   r,   )r   r0   Zblock_size_bytesr   r   r   r'      s    



zGCM.validate_for_algorithm)Nr*   )r"   r#   r$   r%   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr   r&   r;   r   r'   r   r   r   r   r9   h   s     r9   zOFB has been moved to cryptography.hazmat.decrepit.ciphers.modes.OFB and will be removed from cryptography.hazmat.primitives.ciphers.modes in 49.0.0.r   )r%   zCFB has been moved to cryptography.hazmat.decrepit.ciphers.modes.CFB and will be removed from cryptography.hazmat.primitives.ciphers.modes in 49.0.0.r   zCFB8 has been moved to cryptography.hazmat.decrepit.ciphers.modes.CFB8 and will be removed from cryptography.hazmat.primitives.ciphers.modes in 49.0.0.r   N)!
__future__r   Zcryptographyr   Zcryptography.exceptionsr   r   Z*cryptography.hazmat.decrepit.ciphers.modesr   r   r   Z/cryptography.hazmat.primitives._cipheralgorithmr	   r
   Z%cryptography.hazmat.primitives._modesr   r   r   r   r   r   r   r   Z&cryptography.hazmat.primitives.ciphersr   r   r(   r5   r6   r9   
deprecatedr"   ZDeprecatedIn47r   r   r   r   <module>   sN   7