U
    *}fU                     @   s  d dl mZmZmZ 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 eejG dd	 d	eZeejG d
d deZeejG dd deZeejG dd deZeejG dd deZeejG dd de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 )    )absolute_importdivisionprint_functionN)utils)AlreadyFinalizedAlreadyUpdatedNotYetFinalizedUnsupportedAlgorithm_Reasons)_get_backend)CipherBackend)modesc                   @   s(   e Zd Zejdd Zejdd ZdS )CipherAlgorithmc                 C   s   dS )zE
        A string naming this mode (e.g. "AES", "Camellia").
        N selfr   r   O/tmp/pip-unpacked-wheel-x36vw73o/cryptography/hazmat/primitives/ciphers/base.pyname   s    zCipherAlgorithm.namec                 C   s   dS )zW
        The size of the key being used as an integer in bits (e.g. 128, 256).
        Nr   r   r   r   r   key_size    s    zCipherAlgorithm.key_sizeN)__name__
__module____qualname__abcabstractpropertyr   r   r   r   r   r   r      s   
r   c                   @   s   e Zd Zejdd ZdS )BlockCipherAlgorithmc                 C   s   dS )zK
        The size of a block as an integer in bits (e.g. 64, 128).
        Nr   r   r   r   r   
block_size)   s    zBlockCipherAlgorithm.block_sizeN)r   r   r   r   r   r   r   r   r   r   r   '   s   r   c                   @   s6   e Zd Zejdd Zejdd Zejdd ZdS )CipherContextc                 C   s   dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        Nr   r   datar   r   r   update2   s    zCipherContext.updatec                 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   bufr   r   r   update_into9   s    zCipherContext.update_intoc                 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)r   r   r   r   abstractmethodr   r"   r#   r   r   r   r   r   0   s   

r   c                   @   s   e Zd Zejdd ZdS )AEADCipherContextc                 C   s   dS )z3
        Authenticates the provided bytes.
        Nr   r   r   r   r   authenticate_additional_dataI   s    z.AEADCipherContext.authenticate_additional_dataN)r   r   r   r   r$   r&   r   r   r   r   r%   G   s   r%   c                   @   s   e Zd Zejdd ZdS )AEADDecryptionContextc                 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   tagr   r   r   finalize_with_tagR   s    z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r$   r)   r   r   r   r   r'   P   s   r'   c                   @   s   e Zd Zejdd ZdS )AEADEncryptionContextc                 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   r   r   r(   r   r   r   r   r*   Z   s   r*   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )CipherNc                 C   sX   t |}t|tstdtjt|ts0td|d k	rB|| || _	|| _
|| _d S )Nz0Backend object does not implement CipherBackend.z&Expected interface of CipherAlgorithm.)r   
isinstancer   r	   r
   ZBACKEND_MISSING_INTERFACEr   	TypeErrorZvalidate_for_algorithm	algorithmmode_backend)r   r.   r/   backendr   r   r   __init__e   s    


zCipher.__init__c                 C   sB   t | jtjr"| jjd k	r"td| j| j| j}| j	|ddS )Nz0Authentication tag must be None when encrypting.Tencrypt)
r,   r/   r   ModeWithAuthenticationTagr(   
ValueErrorr0   Zcreate_symmetric_encryption_ctxr.   	_wrap_ctxr   ctxr   r   r   	encryptorw   s     zCipher.encryptorc                 C   s    | j | j| j}| j|ddS )NFr3   )r0   Zcreate_symmetric_decryption_ctxr.   r/   r7   r8   r   r   r   	decryptor   s
     zCipher.decryptorc                 C   s0   t | jtjr$|rt|S t|S nt|S d S N)r,   r/   r   r5   _AEADEncryptionContext_AEADCipherContext_CipherContext)r   r9   r4   r   r   r   r7      s
    
zCipher._wrap_ctx)N)r   r   r   r2   r:   r;   r7   r   r   r   r   r+   d   s   
r+   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r?   c                 C   s
   || _ d S r<   )_ctxr8   r   r   r   r2      s    z_CipherContext.__init__c                 C   s   | j d krtd| j |S NContext was already finalized.)r@   r   r   r   r   r   r   r      s    
z_CipherContext.updatec                 C   s    | j d krtd| j ||S rA   )r@   r   r"   r    r   r   r   r"      s    
z_CipherContext.update_intoc                 C   s&   | j d krtd| j  }d | _ |S rA   )r@   r   r#   r   r   r   r   r#      s
    

z_CipherContext.finalizeN)r   r   r   r2   r   r"   r#   r   r   r   r   r?      s   r?   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )r>   c                 C   s"   || _ d| _d| _d | _d| _d S )Nr   F)r@   _bytes_processed_aad_bytes_processed_tag_updatedr8   r   r   r   r2      s
    z_AEADCipherContext.__init__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 )NrB   Tz+{} has a maximum encrypted byte limit of {})	r@   r   rF   rC   _modeZ_MAX_ENCRYPTED_BYTESr6   formatr   )r   	data_sizer   r   r   _check_limit   s    
 z_AEADCipherContext._check_limitc                 C   s   |  t| | j|S r<   )rJ   lenr@   r   r   r   r   r   r      s    z_AEADCipherContext.updatec                 C   s   |  t| | j||S r<   )rJ   rK   r@   r"   r    r   r   r   r"      s    z_AEADCipherContext.update_intoc                 C   s0   | j d krtd| j  }| j j| _d | _ |S rA   )r@   r   r#   r(   rE   r   r   r   r   r#      s    


z_AEADCipherContext.finalizec                 C   s2   | j d krtd| j |}| j j| _d | _ |S rA   )r@   r   r)   r(   rE   )r   r(   r   r   r   r   r)      s    

z$_AEADCipherContext.finalize_with_tagc                 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 )NrB   z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})r@   r   rF   r   rD   rK   rG   Z_MAX_AAD_BYTESr6   rH   r   r&   r   r   r   r   r&      s    
 z/_AEADCipherContext.authenticate_additional_dataN)
r   r   r   r2   rJ   r   r"   r#   r)   r&   r   r   r   r   r>      s   r>   c                   @   s   e Zd Zedd ZdS )r=   c                 C   s   | j d k	rtd| jS )Nz4You must finalize encryption before getting the tag.)r@   r   rE   r   r   r   r   r(      s
    
z_AEADEncryptionContext.tagN)r   r   r   propertyr(   r   r   r   r   r=      s   r=   )"
__future__r   r   r   r   sixZcryptographyr   Zcryptography.exceptionsr   r   r   r	   r
   Zcryptography.hazmat.backendsr   Z'cryptography.hazmat.backends.interfacesr   Z&cryptography.hazmat.primitives.ciphersr   add_metaclassABCMetaobjectr   r   r   r%   r'   r*   r+   Zregister_interfacer?   r>   r=   r   r   r   r   <module>   s8   




	
	.=