3
ãde†   ã               @   s„  d dl 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ee	ddœdd„Zeeddœdd„Zeee	ddœdd„Zee	ddœd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 d'd(„ d(eƒZG d)d*„ d*eeƒZdS )+é    N)Úutils)ÚUnsupportedAlgorithmÚ_Reasons)ÚBlockCipherAlgorithmÚCipherAlgorithm)Ú
algorithmsc               @   s:   e Zd Ze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
   úc/var/www/agendate/envp3/lib/python3.6/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__ÚpropertyÚabcÚabstractmethodÚstrr   r   r   r
   r
   r
   r   r      s
   r   )Ú	metaclassc               @   s$   e Zd Ze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   r   Úbytesr   r
   r
   r
   r   r   "   s   r   c               @   s$   e Zd Ze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   r   +   s   r   c               @   s$   e Zd Ze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   Únonce5   s    zModeWithNonce.nonceN)r   r   r   r   r   r   r   r   r
   r
   r
   r   r   4   s   r   c               @   s*   e Zd Zeej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   Útag>   s    zModeWithAuthenticationTag.tagN)
r   r   r   r   r   r   ÚtypingÚOptionalr   r    r
   r
   r
   r   r   =   s   r   )r   r   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_lengthF   s    r&   c             C   s0   t | jƒd |jkr,tdjt | jƒ| jƒƒ‚d S )Né   zInvalid IV size ({}) for {}.)Úlenr   Ú
block_sizer%   Úformatr   )r   r   r
   r
   r   Ú_check_iv_lengthM   s    r+   )r   r   r   r	   c             C   sL   t |tƒst|› d�tjƒ‚t| ƒd |jkrHtdt| ƒ› d|› d�ƒ‚d S )Nz" requires a block cipher algorithmr'   zInvalid nonce size (z) for Ú.)Ú
isinstancer   r   r   ÚUNSUPPORTED_CIPHERr(   r)   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 )Nz" requires a block cipher algorithm)r-   r   r   r   r.   r&   r+   )r   r   r
   r
   r   Ú_check_iv_and_key_lengthd   s    

r0   c               @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚCBC)r   c             C   s   t jd|ƒ || _d S )Nr   )r   Ú_check_byteslikeÚ_initialization_vector)r   r   r
   r
   r   Ú__init__s   s    zCBC.__init__)r	   c             C   s   | j S )N)r3   )r   r
   r
   r   r   w   s    zCBC.initialization_vectorN)
r   r   r   r   r   r4   r   r   r0   r   r
   r
   r
   r   r1   p   s
   r1   c               @   s@   e Zd Zd Zedœdd„Zeedœdd„ƒZeddœd	d
„Z	dS )ÚXTS)r   c             C   s*   t jd|ƒ t|ƒdkr tdƒ‚|| _d S )Nr   é   z!tweak must be 128-bits (16 bytes))r   r2   r(   r%   Ú_tweak)r   r   r
   r
   r   r4   �   s    zXTS.__init__)r	   c             C   s   | j S )N)r7   )r   r
   r
   r   r   ‰   s    z	XTS.tweakN)r   r	   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.r#   é   z\The XTS specification requires a 256-bit key for AES-128-XTS and 512-bit key for AES-256-XTS)r#   r8   )r-   r   ZAES128ZAES256Ú	TypeErrorr$   r%   )r   r   r
   r
   r   r   �   s    
zXTS.validate_for_algorithm)
r   r   r   r   r   r4   r   r   r   r   r
   r
   r
   r   r5   ~   s
   r5   c               @   s   e Zd Zd ZeZdS )ÚECBN)r   r   r   r   r&   r   r
   r
   r
   r   r:   ›   s   r:   c               @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚOFB)r   c             C   s   t jd|ƒ || _d S )Nr   )r   r2   r3   )r   r   r
   r
   r   r4   ¤   s    zOFB.__init__)r	   c             C   s   | j S )N)r3   )r   r
   r
   r   r   ¨   s    zOFB.initialization_vectorN)
r   r   r   r   r   r4   r   r   r0   r   r
   r
   r
   r   r;   ¡   s
   r;   c               @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚCFB)r   c             C   s   t jd|ƒ || _d S )Nr   )r   r2   r3   )r   r   r
   r
   r   r4   ²   s    zCFB.__init__)r	   c             C   s   | j S )N)r3   )r   r
   r
   r   r   ¶   s    zCFB.initialization_vectorN)
r   r   r   r   r   r4   r   r   r0   r   r
   r
   r
   r   r<   ¯   s
   r<   c               @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚCFB8)r   c             C   s   t jd|ƒ || _d S )Nr   )r   r2   r3   )r   r   r
   r
   r   r4   À   s    zCFB8.__init__)r	   c             C   s   | j S )N)r3   )r   r
   r
   r   r   Ä   s    zCFB8.initialization_vectorN)
r   r   r   r   r   r4   r   r   r0   r   r
   r
   r
   r   r=   ½   s
   r=   c               @   s@   e Zd Zd Zedœdd„Zeedœdd„ƒZeddœd	d
„Z	dS )ÚCTR)r   c             C   s   t jd|ƒ || _d S )Nr   )r   r2   Ú_nonce)r   r   r
   r
   r   r4   Î   s    zCTR.__init__)r	   c             C   s   | j S )N)r?   )r   r
   r
   r   r   Ò   s    z	CTR.nonceN)r   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_algorithm)
r   r   r   r   r   r4   r   r   r   r   r
   r
   r
   r   r>   Ë   s
   r>   c               @   sl   e Zd Zd ZdZdZdeeje e	dœd	d
„Z
eeje dœdd„ƒZeedœdd„ƒZeddœdd„ZdS )ÚGCMé   é'   r#   r'   é@   Nr6   )r   r    Úmin_tag_lengthc             C   s€   t jd|ƒ t|ƒdk s$t|ƒdkr,tdƒ‚|| _|d k	rpt jd|ƒ |dk rVtdƒ‚t|ƒ|k rptdj|ƒƒ‚|| _|| _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   r2   r(   r%   r3   Ú_check_bytesr*   Ú_tagZ_min_tag_length)r   r   r    rD   r
   r
   r   r4   à   s    zGCM.__init__)r	   c             C   s   | j S )N)rH   )r   r
   r
   r   r    ü   s    zGCM.tagc             C   s   | j S )N)r3   )r   r
   r
   r   r      s    zGCM.initialization_vector)r   r	   c             C   sT   t | |ƒ t|tƒs tdtjƒ‚|jd }| jd k	rPt| jƒ|krPt	dj
|ƒƒ‚d S )Nz%GCM requires a block cipher algorithmr'   z0Authentication tag cannot be more than {} bytes.)r&   r-   r   r   r   r.   r)   rH   r(   r%   r*   )r   r   Zblock_size_bytesr
   r
   r   r     s    


zGCM.validate_for_algorithml        l    ÿÿl   àÿ? l            l            )Nr6   )r   r   r   r   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr   r!   r"   Úintr4   r   r    r   r   r   r
   r
   r
   r   r@   Û   s    r@   ) r   r!   Zcryptographyr   Zcryptography.exceptionsr   r   Z/cryptography.hazmat.primitives._cipheralgorithmr   r   Z&cryptography.hazmat.primitives.ciphersr   ÚABCMetar   r   r   r   r   r&   r+   r   r   r/   r0   r1   r5   r:   r;   r<   r=   r>   r@   r
   r
   r
   r   Ú<module>   s2   				

