o
    �)%a•  ã                   @   sJ  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dedef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                   @   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   úN/usr/lib/python3/dist-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
   Únonce-   r   zModeWithNonce.nonceN)r   r   r   r   r   r   r    r   r   r   r
   r   ,   r   r   c                   @   r   )ÚModeWithAuthenticationTagr   c                 C   r   )zP
        The value of the tag supplied to the constructor of this mode.
        Nr   r   r   r   r
   Útag5   r   zModeWithAuthenticationTag.tagN)r   r   r   r   r   r   r"   r   r   r   r
   r!   4   r   r!   c                 C   s$   |j dkr|jdkrtdƒ‚d S 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_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
   Ú_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
   Ú_check_iv_and_key_lengthS   s   
r/   c                   @   ó,   e Zd Zd Zdefdd„Ze 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__[   ó   
zCBC.__init__r6   N©r   r   r   r   r   r8   r   Úread_only_propertyr   r/   r   r   r   r   r
   r1   X   ó
    
r1   c                   @   ó6   e Zd Zd Zdefdd„Ze d¡Zde	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   r5   r)   r&   Ú_tweak)r	   r   r   r   r
   r8   f   s   
zXTS.__init__r@   r   c                 C   s   |j dv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   p   s
   
ÿÿzXTS.validate_for_algorithmN)r   r   r   r   r   r8   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
   rA   x   s    rA   c                   @   r0   )ÚOFBr   c                 C   r2   r3   r4   r7   r   r   r
   r8   �   r9   zOFB.__init__r6   Nr:   r   r   r   r
   rB   ~   r<   rB   c                   @   r0   )ÚCFBr   c                 C   r2   r3   r4   r7   r   r   r
   r8   Œ   r9   zCFB.__init__r6   Nr:   r   r   r   r
   rC   ‰   r<   rC   c                   @   r0   )ÚCFB8r   c                 C   r2   r3   r4   r7   r   r   r
   r8   —   r9   zCFB8.__init__r6   Nr:   r   r   r   r
   rD   ”   r<   rD   c                   @   r=   )	ÚCTRr    c                 C   r2   )Nr    )r   r5   Ú_nonce)r	   r    r   r   r
   r8   ¢   r9   zCTR.__init__rF   r   c                 C   s   t | |ƒ t| j| j|ƒ d S r.   )r'   r-   r    r   r   r   r   r
   r   ¨   s   
zCTR.validate_for_algorithmN)r   r   r   r   r   r8   r   r;   r    r   r   r   r   r   r
   rE   Ÿ   s
    
rE   c                   @   s\   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
¡Ze d¡Zdef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 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   r5   r)   r&   r6   Ú_check_bytesr+   Ú_tagÚ_min_tag_length)r	   r   r"   rH   r   r   r
   r8   ²   s$   ÿÿÿ
zGCM.__init__rL   r6   r   c                 C   s   t | |ƒ d S r.   )r'   r   r   r   r
   r   Ñ   s   zGCM.validate_for_algorithm)Nr?   )r   r   r   r   Ú_MAX_ENCRYPTED_BYTESÚ_MAX_AAD_BYTESr   ÚtypingÚOptionalÚintr8   r   r;   r"   r   r   r   r   r   r   r
   rG   ­   s     üþý
ü

rG   )r   rP   Úcryptographyr   Ú/cryptography.hazmat.primitives._cipheralgorithmr   ÚABCMetar   r   r   r   r!   r'   r,   r   r   r-   r/   r1   r>   rA   rB   rC   rD   rE   rG   r   r   r   r
   Ú<module>   s*   	