B
    `                 @   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               @   s    e Zd ZejedddZdS )BlockCipherAlgorithm)returnc             C   s   dS )zK
        The size of a block as an integer in bits (e.g. 64, 128).
        N )selfr   r   Z/home/dcms/DCMS/lib/python3.7/site-packages/cryptography/hazmat/primitives/ciphers/base.py
block_size   s    zBlockCipherAlgorithm.block_sizeN)__name__
__module____qualname__abcabstractpropertyintr   r   r   r   r   r      s   r   )	metaclassc               @   sL   e Zd ZejeedddZejeedddZejedddZ	d	S )
CipherContext)datar   c             C   s   dS )zk
        Processes the provided bytes through the cipher and returns the results
        as bytes.
        Nr   )r   r   r   r   r   update    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_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)
r   r   r   r   abstractmethodbytesr   r   r   r   r   r   r   r   r      s   r   c               @   s"   e Zd ZejeddddZdS )AEADCipherContextN)r   r   c             C   s   dS )z3
        Authenticates the provided bytes.
        Nr   )r   r   r   r   r   authenticate_additional_data6   s    z.AEADCipherContext.authenticate_additional_data)r   r   r   r   r   r    r"   r   r   r   r   r!   5   s   r!   c               @   s"   e Zd ZejeedddZdS )AEADDecryptionContext)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_tag>   s    z'AEADDecryptionContext.finalize_with_tagN)r   r   r   r   r   r    r%   r   r   r   r   r#   =   s   r#   c               @   s    e Zd ZejedddZdS )AEADEncryptionContext)r   c             C   s   dS )zb
        Returns tag bytes. This is only available after encryption is
        finalized.
        Nr   )r   r   r   r   r$   G   s    zAEADEncryptionContext.tagN)r   r   r   r   r   r    r$   r   r   r   r   r&   F   s   r&   c               @   s>   e Zd Zdeejej d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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_algorithmr(   r)   _backend)r   r(   r)   backendr   r   r   __init__P   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$   
ValueErrorr,   Zcreate_symmetric_encryption_ctxr(   	_wrap_ctx)r   ctxr   r   r   	encryptorg   s    zCipher.encryptorc             C   s    | j | j| j}| j|ddS )NF)r/   )r,   Zcreate_symmetric_decryption_ctxr(   r)   r2   )r   r3   r   r   r   	decryptorr   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   r0   _AEADEncryptionContext_AEADCipherContext_CipherContext)r   r3   r/   r   r   r   r2   x   s
    
zCipher._wrap_ctx)N)r   r   r   r
   typingOptionalr   ZModer.   r4   r5   r2   r   r   r   r   r'   O   s   r'   c               @   sB   e Zd Zdd ZeedddZeedddZedd	d
ZdS )r8   c             C   s
   || _ d S )N)_ctx)r   r3   r   r   r   r.      s    z_CipherContext.__init__)r   r   c             C   s   | j d krtd| j |S )NzContext was already finalized.)r;   r   r   )r   r   r   r   r   r      s    
z_CipherContext.updatec             C   s    | j d krtd| j ||S )NzContext was already finalized.)r;   r   r   )r   r   r   r   r   r   r      s    
z_CipherContext.update_into)r   c             C   s&   | j d krtd| j  }d | _ |S )NzContext was already finalized.)r;   r   r   )r   r   r   r   r   r      s
    

z_CipherContext.finalizeN)	r   r   r   r.   r    r   r   r   r   r   r   r   r   r8      s   r8   c               @   sp   e Zd Zdd ZedddZeedddZeedd	d
ZedddZ	eedddZ
eddddZdS )r7   c             C   s"   || _ d| _d| _d | _d| _d S )Nr   F)r;   _bytes_processed_aad_bytes_processed_tag_updated)r   r3   r   r   r   r.      s
    z_AEADCipherContext.__init__)	data_sizec             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 )NzContext was already finalized.Tz+{} has a maximum encrypted byte limit of {})	r;   r   r?   r<   _modeZ_MAX_ENCRYPTED_BYTESr1   formatname)r   r@   r   r   r   _check_limit   s    
z_AEADCipherContext._check_limit)r   r   c             C   s   |  t| | j|S )N)rD   lenr;   r   )r   r   r   r   r   r      s    z_AEADCipherContext.updatec             C   s   |  t| | j||S )N)rD   rE   r;   r   )r   r   r   r   r   r   r      s    z_AEADCipherContext.update_into)r   c             C   s0   | j d krtd| j  }| j j| _d | _ |S )NzContext was already finalized.)r;   r   r   r$   r>   )r   r   r   r   r   r      s    


z_AEADCipherContext.finalize)r$   r   c             C   s2   | j d krtd| j |}| j j| _d | _ |S )NzContext was already finalized.)r;   r   r%   r$   r>   )r   r$   r   r   r   r   r%      s    

z$_AEADCipherContext.finalize_with_tagNc             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 )NzContext was already finalized.z'Update has been called on this context.z%{} has a maximum AAD byte limit of {})r;   r   r?   r   r=   rE   rA   Z_MAX_AAD_BYTESr1   rB   rC   r"   )r   r   r   r   r   r"      s    
z/_AEADCipherContext.authenticate_additional_data)r   r   r   r.   r   rD   r    r   r   r   r%   r"   r   r   r   r   r7      s   r7   c               @   s   e Zd ZeedddZdS )r6   )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.)r;   r   r>   AssertionError)r   r   r   r   r$      s
    
z_AEADEncryptionContext.tagN)r   r   r   propertyr    r$   r   r   r   r   r6      s   r6   )r   r9   Zcryptographyr   Zcryptography.exceptionsr   r   r   r   r   Zcryptography.hazmat.backendsr   Z'cryptography.hazmat.backends.interfacesr	   Z/cryptography.hazmat.primitives._cipheralgorithmr
   Z&cryptography.hazmat.primitives.ciphersr   ABCMetar   r   r!   r#   r&   objectr'   Zregister_interfacer8   r7   r6   r   r   r   r   <module>   s*   		3=