U
    Nf                      @  sF  d dl mZ 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rZd 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 )    )annotationsN)AlreadyFinalizedAlreadyUpdatedNotYetFinalized)CipherAlgorithm)modes)_CipherContextc                   @  sN   e Zd ZejdddddZejddddddZejdd	d
dZdS )CipherContextbytesdatareturnc                 C  s   dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        N selfr   r   r   O/tmp/pip-unpacked-wheel-bjwtc2fy/cryptography/hazmat/primitives/ciphers/base.pyupdate   s    zCipherContext.updateintr   bufr   c                 C  s   dS )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    s    zCipherContext.update_intor   c                 C  s   dS )zM
        Returns the results of processing the final block as bytes.
        Nr   r   r   r   r   finalize'   s    zCipherContext.finalizeN)__name__
__module____qualname__abcabstractmethodr   r   r   r   r   r   r   r	      s   r	   )	metaclassc                   @  s"   e Zd ZejdddddZdS )AEADCipherContextr
   Noner   c                 C  s   dS )z3
        Authenticates the provided bytes.
        Nr   r   r   r   r   authenticate_additional_data/   s    z.AEADCipherContext.authenticate_additional_dataN)r   r   r   r   r   r#   r   r   r   r   r!   .   s   r!   c                   @  s"   e Zd ZejdddddZdS )AEADDecryptionContextr
   tagr   c                 C  s   dS )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_tag7   s    z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r   r'   r   r   r   r   r$   6   s   r$   c                   @  s$   e Zd ZeejddddZdS )AEADEncryptionContextr
   r   c                 C  s   dS )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   r   r   r   r   r&   @   s    zAEADEncryptionContext.tagN)r   r   r   propertyr   r   r&   r   r   r   r   r(   ?   s   r(   ModeT)bound	covariantc                   @  s   e Zd ZddddddddZejd	d
dddZejdddddZdd Zejd	ddddZejdddddZdd ZddddddZdS )CipherNr   r*   z
typing.Anyr"   )	algorithmmodebackendr   c                 C  sD   t |tstd|d k	r4t |tjs*t|| || _|| _d S )Nz&Expected interface of CipherAlgorithm.)	
isinstancer   	TypeErrorr   r*   AssertionErrorZvalidate_for_algorithmr.   r/   )r   r.   r/   r0   r   r   r   __init__O   s    

zCipher.__init__z'Cipher[modes.ModeWithAuthenticationTag]r(   )r   r   c                 C  s   d S Nr   r   r   r   r   	encryptora   s    zCipher.encryptor_CIPHER_TYPEr	   c                 C  s   d S r5   r   r   r   r   r   r6   g   s    c                 C  sL   t | jtjr"| jjd k	r"tdddlm} || j	| j}| j
|ddS )Nz0Authentication tag must be None when encrypting.r   r0   Tencrypt)r1   r/   r   ModeWithAuthenticationTagr&   
ValueError,cryptography.hazmat.backends.openssl.backendr0   Zcreate_symmetric_encryption_ctxr.   	_wrap_ctxr   r0   ctxr   r   r   r6   m   s     r$   c                 C  s   d S r5   r   r   r   r   r   	decryptorz   s    zCipher.decryptorc                 C  s   d S r5   r   r   r   r   r   rA      s    c                 C  s*   ddl m} || j| j}| j|ddS )Nr   r8   Fr9   )r=   r0   Zcreate_symmetric_decryption_ctxr.   r/   r>   r?   r   r   r   rA      s     _BackendCipherContextboolz=AEADEncryptionContext | AEADDecryptionContext | CipherContext)r@   r:   r   c                 C  s0   t | jtjr$|rt|S t|S nt|S d S r5   )r1   r/   r   r;   _AEADEncryptionContext_AEADDecryptionContextr   )r   r@   r:   r   r   r   r>      s
    
zCipher._wrap_ctx)N)	r   r   r   r4   typingoverloadr6   rA   r>   r   r   r   r   r-   N   s    r-   c                   @  sV   e Zd ZU ded< dddddZddd	d
dZddddddZddddZdS )r   _BackendCipherContext | None_ctxrB   r"   r@   r   c                 C  s
   || _ d S r5   )rI   r   r@   r   r   r   r4      s    z_CipherContext.__init__r
   r   c                 C  s   | j d krtd| j |S NContext was already finalized.)rI   r   r   r   r   r   r   r      s    
z_CipherContext.updater   r   c                 C  s    | j d krtd| j ||S rL   )rI   r   r   r   r   r   r   r      s    
z_CipherContext.update_intor   c                 C  s&   | j d krtd| j  }d | _ |S rL   )rI   r   r   r   r   r   r   r      s
    

z_CipherContext.finalizeN)r   r   r   __annotations__r4   r   r   r   r   r   r   r   r      s
   
r   c                   @  s~   e Zd ZU ded< ded< ddddd	Zd
ddddZdddddZddd
dddZddddZdddddZ	dS )_AEADCipherContextrH   rI   zbytes | None_tagrB   r"   rJ   c                 C  s"   || _ d| _d| _d | _d| _d S )Nr   F)rI   _bytes_processed_aad_bytes_processedrP   _updatedrK   r   r   r   r4      s
    z_AEADCipherContext.__init__r   )	data_sizer   c                 C  sV   | j d krtdd| _|  j|7  _| j| j jjkrRtd| j jj| j jjd S )NrM   Tz+{} has a maximum encrypted byte limit of {})	rI   r   rS   rQ   _modeZ_MAX_ENCRYPTED_BYTESr<   formatname)r   rT   r   r   r   _check_limit   s    
 z_AEADCipherContext._check_limitr
   r   c                 C  s(   |  t| | jd k	st| j|S r5   )rX   lenrI   r3   r   r   r   r   r   r      s    z_AEADCipherContext.updater   c                 C  s*   |  t| | jd k	st| j||S r5   )rX   rY   rI   r3   r   r   r   r   r   r      s    z_AEADCipherContext.update_intor   c                 C  s0   | j d krtd| j  }| j j| _d | _ |S rL   )rI   r   r   r&   rP   r   r   r   r   r      s    


z_AEADCipherContext.finalizec                 C  sn   | j d k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 {})rI   r   rS   r   rR   rY   rU   Z_MAX_AAD_BYTESr<   rV   rW   r#   r   r   r   r   r#      s    
 z/_AEADCipherContext.authenticate_additional_dataN)
r   r   r   rN   r4   rX   r   r   r   r#   r   r   r   r   rO      s   
rO   c                   @  s   e Zd ZdddddZdS )rE   r
   r%   c                 C  sF   | j d krtd| j jd k	r&td| j |}| j j| _d | _ |S )NrM   z^tag provided both in mode and in call with finalize_with_tag: tag should only be provided once)rI   r   rP   r<   r'   r&   )r   r&   r   r   r   r   r'      s    

z(_AEADDecryptionContext.finalize_with_tagN)r   r   r   r'   r   r   r   r   rE      s   rE   c                   @  s   e Zd ZeddddZdS )rD   r
   r   c                 C  s&   | j d k	rtd| jd k	s t| jS )Nz4You must finalize encryption before getting the tag.)rI   r   rP   r3   r   r   r   r   r&   	  s    
z_AEADEncryptionContext.tagN)r   r   r   r)   r&   r   r   r   r   rD     s   rD   )#
__future__r   r   rF   Zcryptography.exceptionsr   r   r   Z/cryptography.hazmat.primitives._cipheralgorithmr   Z&cryptography.hazmat.primitives.ciphersr   TYPE_CHECKINGZ,cryptography.hazmat.backends.openssl.ciphersr   rB   ABCMetar	   r!   r$   r(   TypeVarOptionalr*   Genericr-   UnionZModeWithNonceZModeWithTweakZECBZModeWithInitializationVectorr7   rO   rE   rD   r   r   r   r   <module>   s>   	
 
 L<