U
    *}fJ                     @   s  d dl mZmZmZ d dlZd dlZd dlmZ eej	G dd de
Zeej	G dd de
Zeej	G dd	 d	e
Zeej	G d
d de
Zeej	G dd de
Zdd Zdd Zdd ZeeeeG dd de
ZeeeeG dd de
ZeeG dd de
ZeeeeG dd de
ZeeeeG dd de
ZeeeeG dd de
ZeeeeG d d! d!e
ZeeeeeeG d"d# d#e
ZdS )$    )absolute_importdivisionprint_functionN)utilsc                   @   s(   e Zd Zejdd Zejdd ZdS )Modec                 C   s   dS )z@
        A string naming this mode (e.g. "ECB", "CBC").
        N selfr   r   P/tmp/pip-unpacked-wheel-x36vw73o/cryptography/hazmat/primitives/ciphers/modes.pyname   s    z	Mode.namec                 C   s   dS )zq
        Checks that all the necessary invariants of this (mode, algorithm)
        combination are met.
        Nr   r	   	algorithmr   r   r
   validate_for_algorithm   s    zMode.validate_for_algorithmN)__name__
__module____qualname__abcabstractpropertyr   abstractmethodr   r   r   r   r
   r      s   
r   c                   @   s   e Zd Zejdd ZdS )ModeWithInitializationVectorc                 C   s   dS )zP
        The value of the initialization vector for this mode as bytes.
        Nr   r   r   r   r
   initialization_vector    s    z2ModeWithInitializationVector.initialization_vectorN)r   r   r   r   r   r   r   r   r   r
   r      s   r   c                   @   s   e Zd Zejdd ZdS )ModeWithTweakc                 C   s   dS )z@
        The value of the tweak for this mode as bytes.
        Nr   r   r   r   r
   tweak)   s    zModeWithTweak.tweakN)r   r   r   r   r   r   r   r   r   r
   r   '   s   r   c                   @   s   e Zd Zejdd ZdS )ModeWithNoncec                 C   s   dS )z@
        The value of the nonce for this mode as bytes.
        Nr   r   r   r   r
   nonce2   s    zModeWithNonce.nonceN)r   r   r   r   r   r   r   r   r   r
   r   0   s   r   c                   @   s   e Zd Zejdd ZdS )ModeWithAuthenticationTagc                 C   s   dS )zP
        The value of the tag supplied to the constructor of this mode.
        Nr   r   r   r   r
   tag;   s    zModeWithAuthenticationTag.tagN)r   r   r   r   r   r   r   r   r   r
   r   9   s   r   c                 C   s    |j dkr|jdkrtd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_lengthB   s    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_lengthI   s     r%   c                 C   s   t | | t| | d S N)r    r%   r   r   r   r
   _check_iv_and_key_lengthR   s    
r'   c                   @   s&   e Zd Zd Zdd ZedZeZ	dS )CBCc                 C   s   t d| || _d S Nr   r   _check_byteslike_initialization_vectorr	   r   r   r   r
   __init__\   s    zCBC.__init__r,   N
r   r   r   r   r.   r   read_only_propertyr   r'   r   r   r   r   r
   r(   W   s   
r(   c                   @   s*   e Zd Zd Zdd ZedZdd ZdS )XTSc                 C   s*   t d| t|dkr td|| _d S )Nr      z!tweak must be 128-bits (16 bytes))r   r+   r"   r   _tweak)r	   r   r   r   r
   r.   i   s    zXTS.__init__r3   c                 C   s   |j dkrtdd S )N)r   i   z\The XTS specification requires a 256-bit key for AES-128-XTS and 512-bit key for AES-256-XTS)r   r   r   r   r   r
   r   s   s    
zXTS.validate_for_algorithmN)	r   r   r   r   r.   r   r0   r   r   r   r   r   r
   r1   d   s   
r1   c                   @   s   e Zd Zd ZeZdS )ECBN)r   r   r   r   r    r   r   r   r   r
   r4   {   s   r4   c                   @   s&   e Zd Zd Zdd ZedZeZ	dS )OFBc                 C   s   t d| || _d S r)   r*   r-   r   r   r
   r.      s    zOFB.__init__r,   Nr/   r   r   r   r
   r5      s   
r5   c                   @   s&   e Zd Zd Zdd ZedZeZ	dS )CFBc                 C   s   t d| || _d S r)   r*   r-   r   r   r
   r.      s    zCFB.__init__r,   Nr/   r   r   r   r
   r6      s   
r6   c                   @   s&   e Zd Zd Zdd ZedZeZ	dS )CFB8c                 C   s   t d| || _d S r)   r*   r-   r   r   r
   r.      s    zCFB8.__init__r,   Nr/   r   r   r   r
   r7      s   
r7   c                   @   s*   e Zd Zd Zdd ZedZdd ZdS )CTRc                 C   s   t d| || _d S )Nr   )r   r+   _nonce)r	   r   r   r   r
   r.      s    zCTR.__init__r9   c                 C   s:   t | | t| jd |jkr6tdt| j| jd S )Nr!   zInvalid nonce size ({}) for {}.)r    r"   r   r#   r   r$   r   r   r   r   r
   r      s    
 zCTR.validate_for_algorithmN)	r   r   r   r   r.   r   r0   r   r   r   r   r   r
   r8      s   
r8   c                   @   s>   e Zd Zd ZdZdZdddZedZ	edZ
d	d
 ZdS )GCMl   ? l            Nr2   c                 C   st   t d| t|dkr td|| _|d k	rdt d| |dk rJtdt||k rdtd||| _|| _d S )Nr   r   z-initialization_vector must be at least 1 byter      zmin_tag_length must be >= 4z.Authentication tag must be {} bytes or longer.)	r   r+   r"   r   r,   _check_bytesr$   _tagZ_min_tag_length)r	   r   r   Zmin_tag_lengthr   r   r
   r.      s     zGCM.__init__r=   r,   c                 C   s   t | | d S r&   )r    r   r   r   r
   r      s    zGCM.validate_for_algorithm)Nr2   )r   r   r   r   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr.   r   r0   r   r   r   r   r   r   r
   r:      s   


r:   )
__future__r   r   r   r   sixZcryptographyr   add_metaclassABCMetaobjectr   r   r   r   r   r    r%   r'   Zregister_interfacer(   r1   r4   r5   r6   r7   r8   r:   r   r   r   r
   <module>   sP   




	