U
    ÒZjj   ã                   @   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                   @   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   úo/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/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 )ÚModeWithInitializationVectorr	   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 )ÚModeWithTweakr	   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 )ÚModeWithNoncer	   c                 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   r!   1   s   r!   c                   @   s&   e Zd Zejeje dœdd„ƒZdS )ÚModeWithAuthenticationTagr	   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   ÚtypingÚOptionalr   r$   r   r   r   r   r#   9   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   Ú
ValueErrorr   r   r   r   Ú_check_aes_key_lengthA   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_lengthH   s     ÿÿr/   )r"   r   r   r
   c                 C   sF   t |tƒst|› d�tjƒ‚t| ƒd |jkrBtd t| ƒ|¡ƒ‚d S )Nú" requires a block cipher algorithmr+   zInvalid nonce size ({}) for {}.)	Ú
isinstancer   r   r   ÚUNSUPPORTED_CIPHERr,   r-   r)   r.   )r"   r   r   r   r   r   Ú_check_nonce_lengthS   s    
þÿr3   c                 C   s4   t |tƒst| › d�tjƒ‚t| |ƒ t| |ƒ d S )Nr0   )r1   r   r   r   r2   r*   r/   r   r   r   r   Ú_check_iv_and_key_lengtha   s    
þ
r4   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  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<   Úpropertyr   r4   r   r   r   r   r   r5   m   s
   r5   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  d|¡ t|ƒdkr tdƒ‚|| _d S )Nr    é   z!tweak must be 128-bits (16 bytes))r   r9   r,   r)   Ú_tweak)r   r    r   r   r   r<   ~   s    zXTS.__init__r	   c                 C   s   | j S r=   )rC   r   r   r   r   r    †   s    z	XTS.tweakNr   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)r1   r   ZAES128ZAES256Ú	TypeErrorr(   r)   r   r   r   r   r   Š   s    ÿ
ÿzXTS.validate_for_algorithm)
r   r   r   r   r   r<   r@   r    r   r   r   r   r   r   rA   {   s
   rA   c                   @   s   e Zd Zd ZeZdS )ÚECBN)r   r   r   r   r*   r   r   r   r   r   rE   ˜   s   rE   c                   @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚOFBr6   c                 C   s   t  d|¡ || _d S r7   r8   r;   r   r   r   r<   ¡   s    zOFB.__init__r	   c                 C   s   | j S r=   r>   r   r   r   r   r   ¥   s    zOFB.initialization_vectorNr?   r   r   r   r   rF   ž   s
   rF   c                   @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚCFBr6   c                 C   s   t  d|¡ || _d S r7   r8   r;   r   r   r   r<   ¯   s    zCFB.__init__r	   c                 C   s   | j S r=   r>   r   r   r   r   r   ³   s    zCFB.initialization_vectorNr?   r   r   r   r   rG   ¬   s
   rG   c                   @   s4   e Zd Zd Zedœdd„Zeedœdd„ƒZeZ	dS )ÚCFB8r6   c                 C   s   t  d|¡ || _d S r7   r8   r;   r   r   r   r<   ½   s    zCFB8.__init__r	   c                 C   s   | j S r=   r>   r   r   r   r   r   Á   s    zCFB8.initialization_vectorNr?   r   r   r   r   rH   º   s
   rH   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  d|¡ || _d S )Nr"   )r   r9   Ú_nonce)r   r"   r   r   r   r<   Ë   s    zCTR.__init__r	   c                 C   s   | j S r=   )rJ   r   r   r   r   r"   Ï   s    z	CTR.nonceNr   c                 C   s   t | |ƒ t| j| j|ƒ d S r=   )r*   r3   r"   r   r   r   r   r   r   Ó   s    
zCTR.validate_for_algorithm)
r   r   r   r   r   r<   r@   r"   r   r   r   r   r   r   rI   È   s
   rI   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 )ÚGCMl   àÿ? l            NrB   )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   r9   r,   r)   r:   Ú_check_bytesr.   Ú_tagZ_min_tag_length)r   r   r$   rL   r   r   r   r<   Ý   s$    ÿÿÿzGCM.__init__r	   c                 C   s   | j S r=   )rP   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   c                 C   sT   t | |ƒ t|tƒs tdtjƒ‚|jd }| jd k	rPt| jƒ|krPt	d 
|¡ƒ‚d S )Nz%GCM requires a block cipher algorithmr+   z0Authentication tag cannot be more than {} bytes.)r*   r1   r   r   r   r2   r-   rP   r,   r)   r.   )r   r   Zblock_size_bytesr   r   r   r     s    

þ
ÿÿzGCM.validate_for_algorithm)NrB   )r   r   r   r   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr   r%   r&   Úintr<   r@   r$   r   r   r   r   r   r   r   rK   Ø   s     üürK   ) 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   r3   r4   r5   rA   rE   rF   rG   rH   rI   rK   r   r   r   r   Ú<module>   s@    þ  þ þ