o
    
cN                      @   s:  d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z	 ej
r'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ejdeje	j ddZG dd deje Zeeje	je	jde	je	jf  ZG dd deZG dd deZG dd deeZG dd deeZ dS )    N)AlreadyFinalizedAlreadyUpdatedNotYetFinalized)CipherAlgorithm)modes)_CipherContextc                   @   sT   e Zd ZejdedefddZejdededefddZejdefdd	Z	d
S )CipherContextdatareturnc                 C      dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        N selfr	   r   r   X/opt/certbot/lib/python3.10/site-packages/cryptography/hazmat/primitives/ciphers/base.pyupdate       zCipherContext.updatebufc                 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	   r   r   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)
__name__
__module____qualname__abcabstractmethodbytesr   intr   r   r   r   r   r   r      s    r   )	metaclassc                   @   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_data.   r   z.AEADCipherContext.authenticate_additional_data)r   r   r   r   r   r   r    r   r   r   r   r   -       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_tag6   r   z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r   r   r$   r   r   r   r   r"   5   r!   r"   c                   @   s$   e Zd ZeejdefddZdS )AEADEncryptionContextr
   c                 C   r   )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r#   ?   r   zAEADEncryptionContext.tagN)r   r   r   propertyr   r   r   r#   r   r   r   r   r%   >   s    r%   ModeT)bound	covariantc                	   @   s   e Zd Z	ddededejddfddZejdd	de	fd
dZ
ejdddefddZ
dd Z
ejdd	defddZejdddefddZdd Zdddedeje	eef fddZdS )CipherN	algorithmmodebackendr
   c                 C   sD   t |ts	td|d urt |tjsJ || || _|| _d S )Nz&Expected interface of CipherAlgorithm.)
isinstancer   	TypeErrorr   r'   validate_for_algorithmr+   r,   )r   r+   r,   r-   r   r   r   __init__N   s   


zCipher.__init__r   z'Cipher[modes.ModeWithAuthenticationTag]c                 C      d S Nr   r   r   r   r   	encryptora      zCipher.encryptor_CIPHER_TYPEc                 C   r2   r3   r   r   r   r   r   r4   g   r5   c                 C   sL   t | jtjr| jjd urtdddlm} || j	| j}| j
|ddS )Nz0Authentication tag must be None when encrypting.r   r-   Tencrypt)r.   r,   r   ModeWithAuthenticationTagr#   
ValueError,cryptography.hazmat.backends.openssl.backendr-   create_symmetric_encryption_ctxr+   	_wrap_ctxr   r-   ctxr   r   r   r4   m   s   c                 C   r2   r3   r   r   r   r   r   	decryptorz   r5   zCipher.decryptorc                 C   r2   r3   r   r   r   r   r   rA      r5   c                 C   s*   ddl m} || j| j}| j|ddS )Nr   r7   Fr8   )r<   r-   create_symmetric_decryption_ctxr+   r,   r>   r?   r   r   r   rA      s
   r@   _BackendCipherContextr9   c                 C   s*   t | jtjr|rt|S t|S t|S r3   )r.   r,   r   r:   _AEADEncryptionContext_AEADDecryptionContextr   )r   r@   r9   r   r   r   r>      s
   zCipher._wrap_ctxr3   )r   r   r   r   r'   typingAnyr1   overloadr%   r4   r   r"   rA   boolUnionr>   r   r   r   r   r*   M   s\    
r*   c                   @   s\   e Zd ZU ejd ed< dddZdedefd	d
Zdedede	fddZ
defddZdS )r   rC   _ctxr@   r
   Nc                 C   s
   || _ d S r3   )rK   r   r@   r   r   r   r1      s   
z_CipherContext.__init__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.updater   c                 C   s    | j d u r	td| j ||S rM   )rK   r   r   r   r   r   r   r      s   
z_CipherContext.update_intoc                 C   s&   | j d u r	td| j  }d | _ |S rM   )rK   r   r   r   r   r   r   r      s
   

z_CipherContext.finalizer@   rC   r
   N)r   r   r   rF   Optional__annotations__r1   r   r   r   r   r   r   r   r   r   r      s   
 
r   c                   @   s   e Zd ZU ejd ed< eje ed< dddZd	eddfd
dZ	dedefddZ
dededefddZdefddZdeddfddZdS )_AEADCipherContextrC   rK   _tagr@   r
   Nc                 C   s"   || _ d| _d| _d | _d| _d S )Nr   F)rK   _bytes_processed_aad_bytes_processedrS   _updatedrL   r   r   r   r1      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 )NrN   Tz+{} has a maximum encrypted byte limit of {})	rK   r   rV   rT   _mode_MAX_ENCRYPTED_BYTESr;   formatname)r   rW   r   r   r   _check_limit   s   
z_AEADCipherContext._check_limitr	   c                 C   s(   |  t| | jd usJ | j|S r3   )r\   lenrK   r   r   r   r   r   r      s   z_AEADCipherContext.updater   c                 C   s*   |  t| | jd usJ | j||S r3   )r\   r]   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 rM   )rK   r   r   r#   rS   r   r   r   r   r      s   


z_AEADCipherContext.finalizec                 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 )NrN   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})rK   r   rV   r   rU   r]   rX   _MAX_AAD_BYTESr;   rZ   r[   r    r   r   r   r   r       s   
z/_AEADCipherContext.authenticate_additional_datarO   )r   r   r   rF   rP   rQ   r   r1   r   r\   r   r   r   r    r   r   r   r   rR      s   
 
rR   c                   @   s   e Zd ZdedefddZdS )rE   r#   r
   c                 C   s2   | j d u r	td| j |}| j j| _d | _ |S rM   )rK   r   r$   r#   rS   )r   r#   r	   r   r   r   r$      s   

z(_AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r$   r   r   r   r   rE      s    rE   c                   @   s   e Zd ZedefddZdS )rD   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   rS   r   r   r   r   r#     s   
z_AEADEncryptionContext.tagN)r   r   r   r&   r   r#   r   r   r   r   rD     s    rD   )!r   rF   cryptography.exceptionsr   r   r   /cryptography.hazmat.primitives._cipheralgorithmr   &cryptography.hazmat.primitives.ciphersr   TYPE_CHECKING,cryptography.hazmat.backends.openssl.ciphersr   rC   ABCMetar   r   r"   r%   TypeVarrP   r'   Genericr*   rJ   ModeWithNonceModeWithTweakECBModeWithInitializationVectorr6   rR   rE   rD   r   r   r   r   <module>   s:   	
O<
