U
    ʹh~                      @  s  d dl m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ddddddZddddddZdddddddZdd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/d0 d0eeZdS )1    )annotationsN)utils)UnsupportedAlgorithm_Reasons)BlockCipherAlgorithmCipherAlgorithm)
algorithmsc                   @  s:   e Zd ZeejddddZejddddd	Zd
S )Modestrreturnc                 C  s   dS )z@
        A string naming this mode (e.g. "ECB", "CBC").
        N selfr   r   p/var/www/html/timesheet/backend/venv/lib/python3.8/site-packages/cryptography/hazmat/primitives/ciphers/modes.pyname   s    z	Mode.namer   None	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_algorithmN)__name__
__module____qualname__propertyabcabstractmethodr   r   r   r   r   r   r	      s
   r	   )	metaclassc                   @  s$   e Zd ZeejddddZdS )ModeWithInitializationVectorutils.Bufferr   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   r    r   r   r   r   r   "   s   r   c                   @  s$   e Zd ZeejddddZdS )ModeWithTweakr   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ejddddZdS )ModeWithNoncer   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#   4   s   r#   c                   @  s$   e Zd ZeejddddZdS )ModeWithAuthenticationTagbytes | Noner   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   r'   r   r   r   r   r%   =   s   r%   r   r   )r   r   r   c                 C  s    |j dkr|jdkrtdd S )N   AESz=Only 128, 192, and 256 bit keys are allowed for this AES mode)key_sizer   
ValueErrorr   r   r   r   _check_aes_key_lengthF   s    r,   r   c                 C  s4   t | j}|d |jkr0td| d| j dd S )N   zInvalid IV size () for .)lenr    
block_sizer+   r   )r   r   iv_lenr   r   r   _check_iv_lengthM   s    
r3   r   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 )N" requires a block cipher algorithmr-   zInvalid nonce size (r.   r/   )
isinstancer   r   r   UNSUPPORTED_CIPHERr0   r1   r+   )r$   r   r   r   r   r   _check_nonce_lengthU   s    
r7   c                 C  s4   t |tst|  dtjt| | t| | d S )Nr4   )r5   r   r   r   r6   r,   r3   r   r   r   r   _check_iv_and_key_lengtha   s    

r8   c                   @  s4   e Zd Zd ZddddZeddddZeZdS )	CBCr   r    c                 C  s   t d| || _d S Nr    r   _check_byteslike_initialization_vectorr   r    r   r   r   __init__p   s    zCBC.__init__r   c                 C  s   | j S Nr>   r   r   r   r   r    t   s    zCBC.initialization_vectorN	r   r   r   r   r@   r   r    r8   r   r   r   r   r   r9   m   s
   r9   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   )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=   r0   r+   _tweak)r   r"   r   r   r   r@   ~   s    zXTS.__init__r   c                 C  s   | j S rA   )rF   r   r   r   r   r"      s    z	XTS.tweakr   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(   i   z\The XTS specification requires a 256-bit key for AES-128-XTS and 512-bit key for AES-256-XTS)r5   r   ZAES128ZAES256	TypeErrorr*   r+   r   r   r   r   r      s    
zXTS.validate_for_algorithmN)r   r   r   r   r@   r   r"   r   r   r   r   r   rD   {   s
   rD   c                   @  s   e Zd Zd ZeZdS )ECBN)r   r   r   r   r,   r   r   r   r   r   rH      s   rH   c                   @  s4   e Zd Zd ZddddZeddddZeZdS )	OFBr   r:   c                 C  s   t d| || _d S r;   r<   r?   r   r   r   r@      s    zOFB.__init__r   c                 C  s   | j S rA   rB   r   r   r   r   r       s    zOFB.initialization_vectorNrC   r   r   r   r   rI      s
   rI   c                   @  s4   e Zd Zd ZddddZeddddZeZdS )	CFBr   r:   c                 C  s   t d| || _d S r;   r<   r?   r   r   r   r@      s    zCFB.__init__r   c                 C  s   | j S rA   rB   r   r   r   r   r       s    zCFB.initialization_vectorNrC   r   r   r   r   rJ      s
   rJ   c                   @  s4   e Zd Zd ZddddZeddddZeZdS )	CFB8r   r:   c                 C  s   t d| || _d S r;   r<   r?   r   r   r   r@      s    zCFB8.__init__r   c                 C  s   | j S rA   rB   r   r   r   r   r       s    zCFB8.initialization_vectorNrC   r   r   r   r   rK      s
   rK   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   )r$   c                 C  s   t d| || _d S )Nr$   )r   r=   _nonce)r   r$   r   r   r   r@      s    zCTR.__init__r   c                 C  s   | j S rA   )rM   r   r   r   r   r$      s    z	CTR.noncer   r   r   c                 C  s   t | | t| j| j| d S rA   )r,   r7   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   rL      s
   rL   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            NrE   r   r&   int)r    r'   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    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 z bytes or longer.)r   r=   r0   r+   r>   _check_bytes_tagZ_min_tag_length)r   r    r'   rP   r   r   r   r@      s     
zGCM.__init__r   c                 C  s   | j S rA   )rT   r   r   r   r   r'      s    zGCM.tagc                 C  s   | j S rA   rB   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,   r5   r   r   r   r6   r1   rT   r0   r+   )r   r   Zblock_size_bytesr   r   r   r      s    



zGCM.validate_for_algorithm)NrE   )r   r   r   r   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr@   r   r'   r    r   r   r   r   r   rN      s     rN   )
__future__r   r   cryptographyr   cryptography.exceptionsr   r   /cryptography.hazmat.primitives._cipheralgorithmr   r   &cryptography.hazmat.primitives.ciphersr   ABCMetar	   r   r!   r#   r%   r,   r3   r7   r8   r9   rD   rH   rI   rJ   rK   rL   rN   r   r   r   r   <module>   s,   				