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=