U
    2ÜŸ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-mmhsgb0a/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   z•OFB 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%   z•CFB 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   z—CFB8 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øøø