o
    )%a                     @   sB  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZ d dlm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G dd deZeeG dd deZeeeeeeG dd deZeeG dd deZdS )    N)utils)AlreadyFinalizedAlreadyUpdatedNotYetFinalizedUnsupportedAlgorithm_Reasons)_get_backend)CipherBackend)CipherAlgorithm)modesc                   @       e Zd ZejdefddZdS )BlockCipherAlgorithmreturnc                 C      dS )zK
        The size of a block as an integer in bits (e.g. 64, 128).
        N selfr   r   M/usr/lib/python3/dist-packages/cryptography/hazmat/primitives/ciphers/base.py
block_size       zBlockCipherAlgorithm.block_sizeN)__name__
__module____qualname__abcabstractpropertyintr   r   r   r   r   r          r   )	metaclassc                   @   sP   e Zd ZejdedefddZejdedefddZejdefddZ	d	S )
CipherContextdatar   c                 C   r   )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        Nr   r   r   r   r   r   update    r   zCipherContext.updatec                 C   r   )z
        Processes the provided bytes and writes the resulting data into the
        provided buffer. Returns the number of bytes written.
        Nr   r   r   bufr   r   r   update_into'   r   zCipherContext.update_intoc                 C   r   )zM
        Returns the results of processing the final block as bytes.
        Nr   r   r   r   r   finalize.   r   zCipherContext.finalizeN)
r   r   r   r   abstractmethodbytesr!   r   r$   r%   r   r   r   r   r      s    r   c                   @   s$   e Zd ZejdeddfddZdS )AEADCipherContextr   r   Nc                 C   r   )z3
        Authenticates the provided bytes.
        Nr   r    r   r   r   authenticate_additional_data6   r   z.AEADCipherContext.authenticate_additional_data)r   r   r   r   r&   r'   r)   r   r   r   r   r(   5       r(   c                   @   s$   e Zd ZejdedefddZdS )AEADDecryptionContexttagr   c                 C   r   )z
        Returns the results of processing the final block as bytes and allows
        delayed passing of the authentication tag.
        Nr   )r   r,   r   r   r   finalize_with_tag>   r   z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r&   r'   r-   r   r   r   r   r+   =   r*   r+   c                   @   r   )AEADEncryptionContextr   c                 C   r   )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r,   G   r   zAEADEncryptionContext.tagN)r   r   r   r   r   r'   r,   r   r   r   r   r.   F   r   r.   c                   @   sB   e Zd Z	ddedejej fddZdd Z	dd	 Z
d
d ZdS )CipherN	algorithmmodec                 C   sX   t |}t|tstdtjt|tstd|d ur!|| || _	|| _
|| _d S )Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.)r   
isinstancer	   r   r   BACKEND_MISSING_INTERFACEr
   	TypeErrorvalidate_for_algorithmr0   r1   _backend)r   r0   r1   backendr   r   r   __init__P   s   



zCipher.__init__c                 C   sB   t | jtjr| jjd urtd| j| j| j}| j	|ddS )Nz0Authentication tag must be None when encrypting.Tencrypt)
r2   r1   r   ModeWithAuthenticationTagr,   
ValueErrorr6   create_symmetric_encryption_ctxr0   	_wrap_ctxr   ctxr   r   r   	encryptorg   s   zCipher.encryptorc                 C   s    | j | j| j}| j|ddS )NFr9   )r6   create_symmetric_decryption_ctxr0   r1   r>   r?   r   r   r   	decryptorr   s   zCipher.decryptorc                 C   s*   t | jtjr|rt|S t|S t|S N)r2   r1   r   r;   _AEADEncryptionContext_AEADCipherContext_CipherContext)r   r@   r:   r   r   r   r>   x   s
   zCipher._wrap_ctxrD   )r   r   r   r
   typingOptionalr   Moder8   rA   rC   r>   r   r   r   r   r/   O   s    

r/   c                   @   sF   e Zd Zdd ZdedefddZdedefddZdefd	d
ZdS )rG   c                 C   s
   || _ d S rD   )_ctxr?   r   r   r   r8      s   
z_CipherContext.__init__r   r   c                 C   s   | j d u r	td| j |S NContext was already finalized.)rK   r   r!   r    r   r   r   r!      s   
z_CipherContext.updatec                 C   s    | j d u r	td| j ||S rL   )rK   r   r$   r"   r   r   r   r$      s   
z_CipherContext.update_intoc                 C   s&   | j d u r	td| j  }d | _ |S rL   )rK   r   r%   r    r   r   r   r%      s
   

z_CipherContext.finalizeN)	r   r   r   r8   r'   r!   r   r$   r%   r   r   r   r   rG      s
    rG   c                   @   sx   e Zd Zdd ZdefddZdedefdd	Zdedefd
dZdefddZ	dedefddZ
deddfddZdS )rF   c                 C   s"   || _ d| _d| _d | _d| _d S )Nr   F)rK   _bytes_processed_aad_bytes_processed_tag_updatedr?   r   r   r   r8      s
   
z_AEADCipherContext.__init__	data_sizec                 C   sV   | j d u r	tdd| _|  j|7  _| j| j jjkr)td| j jj| j jjd S )NrM   Tz+{} has a maximum encrypted byte limit of {})	rK   r   rQ   rN   _mode_MAX_ENCRYPTED_BYTESr<   formatname)r   rR   r   r   r   _check_limit   s   
z_AEADCipherContext._check_limitr   r   c                 C   s   |  t| | j|S rD   )rW   lenrK   r!   r    r   r   r   r!      s   z_AEADCipherContext.updatec                 C   s   |  t| | j||S rD   )rW   rX   rK   r$   r"   r   r   r   r$      s   z_AEADCipherContext.update_intoc                 C   s0   | j d u r	td| j  }| j j| _d | _ |S rL   )rK   r   r%   r,   rP   r    r   r   r   r%      s   


z_AEADCipherContext.finalizer,   c                 C   s2   | j d u r	td| j |}| j j| _d | _ |S rL   )rK   r   r-   r,   rP   )r   r,   r   r   r   r   r-      s   

z$_AEADCipherContext.finalize_with_tagNc                 C   sn   | j d u r	td| jrtd|  jt|7  _| j| j jjkr/td	| j jj
| j jj| j | d S )NrM   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})rK   r   rQ   r   rO   rX   rS   _MAX_AAD_BYTESr<   rU   rV   r)   r    r   r   r   r)      s   
z/_AEADCipherContext.authenticate_additional_data)r   r   r   r8   r   rW   r'   r!   r$   r%   r-   r)   r   r   r   r   rF      s    rF   c                   @   s   e Zd ZedefddZdS )rE   r   c                 C   s&   | j d ur	td| jd usJ | jS )Nz4You must finalize encryption before getting the tag.)rK   r   rP   r   r   r   r   r,      s   
z_AEADEncryptionContext.tagN)r   r   r   propertyr'   r,   r   r   r   r   rE      s    rE   )r   rH   cryptographyr   cryptography.exceptionsr   r   r   r   r   cryptography.hazmat.backendsr   'cryptography.hazmat.backends.interfacesr	   /cryptography.hazmat.primitives._cipheralgorithmr
   &cryptography.hazmat.primitives.ciphersr   ABCMetar   r   r(   r+   r.   objectr/   register_interfacerG   rF   rE   r   r   r   r   <module>   s,   		3=