o
    cj                      @   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dede	ddfddZdededdfddZdedede	ddfddZdede	ddf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                   @   s8   e Zd ZejdefddZejdeddfddZ	dS )Modereturnc                 C      dS )z@
        A string naming this mode (e.g. "ECB", "CBC").
        N selfr   r   Pusr/lib/python3.10/site-packages/cryptography/hazmat/primitives/ciphers/modes.pyname       z	Mode.name	algorithmNc                 C   r
   )zq
        Checks that all the necessary invariants of this (mode, algorithm)
        combination are met.
        Nr   r   r   r   r   r   validate_for_algorithm   r   zMode.validate_for_algorithm)
__name__
__module____qualname__abcabstractpropertystrr   abstractmethodr   r   r   r   r   r   r      s
    r   )	metaclassc                   @       e Zd ZejdefddZdS )ModeWithInitializationVectorr	   c                 C   r
   )zP
        The value of the initialization vector for this mode as bytes.
        Nr   r   r   r   r   initialization_vector"   r   z2ModeWithInitializationVector.initialization_vectorN)r   r   r   r   r   bytesr   r   r   r   r   r   !       r   c                   @   r   )ModeWithTweakr	   c                 C   r
   )z@
        The value of the tweak for this mode as bytes.
        Nr   r   r   r   r   tweak*   r   zModeWithTweak.tweakN)r   r   r   r   r   r   r"   r   r   r   r   r!   )   r    r!   c                   @   r   )ModeWithNoncer	   c                 C   r
   )z@
        The value of the nonce for this mode as bytes.
        Nr   r   r   r   r   nonce2   r   zModeWithNonce.nonceN)r   r   r   r   r   r   r$   r   r   r   r   r#   1   r    r#   c                   @   s&   e Zd Zejdeje fddZdS )ModeWithAuthenticationTagr	   c                 C   r
   )zP
        The value of the tag supplied to the constructor of this mode.
        Nr   r   r   r   r   tag:   r   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 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   r1   r$   r   c                 C   sF   t |tst| dtjt| d |jkr!tdt| |d S )N" requires a block cipher algorithmr-   zInvalid nonce size ({}) for {}.)	
isinstancer   r   r   UNSUPPORTED_CIPHERr.   r/   r+   r0   )r$   r   r   r   r   r   _check_nonce_lengthS   s   
r5   c                 C   s4   t |tst|  dtjt| | t| | d S )Nr2   )r3   r   r   r   r4   r,   r1   r   r   r   r   _check_iv_and_key_lengtha   s   

r6   c                   @   4   e Zd Zd ZdefddZedefddZeZ	dS )CBCr   c                 C      t d| || _d S Nr   r   _check_byteslike_initialization_vectorr   r   r   r   r   __init__p      
zCBC.__init__r	   c                 C      | j S Nr=   r   r   r   r   r   t      zCBC.initialization_vectorN
r   r   r   r   r   r?   propertyr   r6   r   r   r   r   r   r8   m       r8   c                   @   B   e Zd Zd ZdefddZedefddZdeddfd	d
Z	dS )XTSr"   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?   ~   s   
zXTS.__init__r	   c                 C   rA   rB   )rK   r   r   r   r   r"      rD   z	XTS.tweakr   Nc                 C   s0   t |tjtjfrtd|jdv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)r3   r   ZAES128ZAES256	TypeErrorr*   r+   r   r   r   r   r      s   
zXTS.validate_for_algorithm)
r   r   r   r   r   r?   rF   r"   r   r   r   r   r   r   rI   {   s    rI   c                   @   s   e Zd Zd ZeZdS )ECBN)r   r   r   r   r,   r   r   r   r   r   rM      s    rM   c                   @   r7   )OFBr   c                 C   r9   r:   r;   r>   r   r   r   r?      r@   zOFB.__init__r	   c                 C   rA   rB   rC   r   r   r   r   r      rD   zOFB.initialization_vectorNrE   r   r   r   r   rN      rG   rN   c                   @   r7   )CFBr   c                 C   r9   r:   r;   r>   r   r   r   r?      r@   zCFB.__init__r	   c                 C   rA   rB   rC   r   r   r   r   r      rD   zCFB.initialization_vectorNrE   r   r   r   r   rO      rG   rO   c                   @   r7   )CFB8r   c                 C   r9   r:   r;   r>   r   r   r   r?      r@   zCFB8.__init__r	   c                 C   rA   rB   rC   r   r   r   r   r      rD   zCFB8.initialization_vectorNrE   r   r   r   r   rP      rG   rP   c                   @   rH   )CTRr$   c                 C   r9   )Nr$   )r   r<   _nonce)r   r$   r   r   r   r?      r@   zCTR.__init__r	   c                 C   rA   rB   )rR   r   r   r   r   r$      rD   z	CTR.noncer   Nc                 C   s   t | | t| j| j| d S rB   )r,   r5   r$   r   r   r   r   r   r      s   
zCTR.validate_for_algorithm)
r   r   r   r   r   r?   rF   r$   r   r   r   r   r   r   rQ      s    rQ   c                   @   sv   e Zd Zd ZdZdZ		ddedeje de	fdd	Z
ed
eje fddZed
efddZded
dfddZdS )GCMl   ? l            NrJ   r   r&   min_tag_lengthc                 C   s   t d| t|dk st|dkrtd|| _|d ur8t d| |dk r+tdt||k r8t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_bytesr0   _tagZ_min_tag_length)r   r   r&   rT   r   r   r   r?      s$   
zGCM.__init__r	   c                 C   rA   rB   )rX   r   r   r   r   r&      rD   zGCM.tagc                 C   rA   rB   rC   r   r   r   r   r      rD   zGCM.initialization_vectorr   c                 C   sX   t | | t|tstdtj|jd }| jd ur(t| j|kr*t	d
|d S d S )Nz%GCM requires a block cipher algorithmr-   z0Authentication tag cannot be more than {} bytes.)r,   r3   r   r   r   r4   r/   rX   r.   r+   r0   )r   r   Zblock_size_bytesr   r   r   r     s   


zGCM.validate_for_algorithm)NrJ   )r   r   r   r   Z_MAX_ENCRYPTED_BYTESZ_MAX_AAD_BYTESr   r'   r(   intr?   rF   r&   r   r   r   r   r   r   r   rS      s$    
rS   ) r   r'   cryptographyr   Zcryptography.exceptionsr   r   Z/cryptography.hazmat.primitives._cipheralgorithmr   r   Z&cryptography.hazmat.primitives.ciphersr   ABCMetar   r   r!   r#   r%   r,   r1   r   r   r5   r6   r8   rI   rM   rN   rO   rP   rQ   rS   r   r   r   r   <module>   sV   


