B
    `                 @   sH  d dl Z d dlZd dlmZ d dlmZ G dd de jdZG dd de jdZG d	d
 d
e jdZ	G dd de jdZ
G dd de jdZdd Zdd ZeedddZdd ZG dd deeZG dd dee	ZG dd deZG dd deeZG d d! d!eeZG d"d# d#eeZG d$d% d%ee
ZG d&d' d'eeeZdS )(    N)utils)CipherAlgorithmc               @   s6   e Zd ZejedddZejeddddZ	dS )Mode)returnc             C   s   dS )z@
        A string naming this mode (e.g. "ECB", "CBC").
        N )selfr   r   [/home/dcms/DCMS/lib/python3.7/site-packages/cryptography/hazmat/primitives/ciphers/modes.pyname   s    z	Mode.nameN)	algorithmr   c             C   s   dS )zq
        Checks that all the necessary invariants of this (mode, algorithm)
        combination are met.
        Nr   )r   r
   r   r   r   validate_for_algorithm   s    zMode.validate_for_algorithm)
__name__
__module____qualname__abcabstractpropertystrr	   abstractmethodr   r   r   r   r   r   r      s   r   )	metaclassc               @   s    e Zd ZejedddZdS )ModeWithInitializationVector)r   c             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   bytesr   r   r   r   r   r      s   r   c               @   s    e Zd ZejedddZdS )ModeWithTweak)r   c             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   r   $   s   r   c               @   s    e Zd ZejedddZdS )ModeWithNonce)r   c             C   s   dS )z@
        The value of the nonce for this mode as bytes.
        Nr   )r   r   r   r   nonce-   s    zModeWithNonce.nonceN)r   r   r   r   r   r   r   r   r   r   r   r   ,   s   r   c               @   s    e Zd ZejedddZdS )ModeWithAuthenticationTag)r   c             C   s   dS )zP
        The value of the tag supplied to the constructor of this mode.
        Nr   )r   r   r   r   tag5   s    zModeWithAuthenticationTag.tagN)r   r   r   r   r   r   r   r   r   r   r   r   4   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	   
ValueError)r   r
   r   r   r   _check_aes_key_length<   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   r   _check_iv_lengthC   s    r%   )r   r	   c             C   s*   t | d |jkr&tdt | |d S )Nr!   zInvalid nonce size ({}) for {}.)r"   r#   r   r$   )r   r	   r
   r   r   r   _check_nonce_lengthL   s    r&   c             C   s   t | | t| | d S )N)r    r%   )r   r
   r   r   r   _check_iv_and_key_lengthS   s    
r'   c               @   s,   e Zd Zd ZedddZedZe	Z
dS )CBC)r   c             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+   r   read_only_propertyr   r'   r   r   r   r   r   r(   X   s   
r(   c               @   s6   e Zd Zd ZedddZedZe	dddZ
dS )	XTS)r   c             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+   f   s    zXTS.__init__r/   )r
   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   r   p   s    
zXTS.validate_for_algorithmN)r   r   r   r	   r   r+   r   r,   r   r   r   r   r   r   r   r-   c   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   r0   x   s   r0   c               @   s,   e Zd Zd ZedddZedZe	Z
dS )OFB)r   c             C   s   t d| || _d S )Nr   )r   r)   r*   )r   r   r   r   r   r+      s    zOFB.__init__r*   N)r   r   r   r	   r   r+   r   r,   r   r'   r   r   r   r   r   r1   ~   s   
r1   c               @   s,   e Zd Zd ZedddZedZe	Z
dS )CFB)r   c             C   s   t d| || _d S )Nr   )r   r)   r*   )r   r   r   r   r   r+      s    zCFB.__init__r*   N)r   r   r   r	   r   r+   r   r,   r   r'   r   r   r   r   r   r2      s   
r2   c               @   s,   e Zd Zd ZedddZedZe	Z
dS )CFB8)r   c             C   s   t d| || _d S )Nr   )r   r)   r*   )r   r   r   r   r   r+      s    zCFB8.__init__r*   N)r   r   r   r	   r   r+   r   r,   r   r'   r   r   r   r   r   r3      s   
r3   c               @   s6   e Zd Zd ZedddZedZe	dddZ
dS )	CTR)r   c             C   s   t d| || _d S )Nr   )r   r)   _nonce)r   r   r   r   r   r+      s    zCTR.__init__r5   )r
   c             C   s   t | | t| j| j| d S )N)r    r&   r   r	   )r   r
   r   r   r   r      s    
zCTR.validate_for_algorithmN)r   r   r   r	   r   r+   r   r,   r   r   r   r   r   r   r   r4      s   
r4   c               @   sT   e Zd Zd ZdZdZdeeje e	dddZ
edZed	Zed
ddZdS )GCMl   ? l            Nr.   )r   r   min_tag_lengthc             C   s   t d| t|dk s$t|dkr,td|| _|d k	rpt d| |dk rVtdt||k rptd||| _|| _d S )	Nr   r!      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   r)   r"   r   r*   _check_bytesr$   _tagZ_min_tag_length)r   r   r   r7   r   r   r   r+      s    zGCM.__init__r;   r*   )r
   c             C   s   t | | d S )N)r    )r   r
   r   r   r   r      s    zGCM.validate_for_algorithm)Nr.   )r   r   r   r	   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr   typingOptionalintr+   r   r,   r   r   r   r   r   r   r   r   r6      s    

r6   )r   r<   Zcryptographyr   Z/cryptography.hazmat.primitives._cipheralgorithmr   ABCMetar   r   r   r   r   r    r%   r   r   r&   r'   r(   r-   r0   r1   r2   r3   r4   r6   r   r   r   r   <module>   s(   	