3
K^b$                 @   s   d dl mZmZmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ ejejejejejejejejG dd deZdS )	    )absolute_importdivisionprint_function)utils)
InvalidTagUnsupportedAlgorithm_Reasons)ciphers)modesc               @   sN   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
ejdZdS )_CipherContext   r   c             C   s  || _ || _|| _|| _d | _t| jtjr<| jjd | _	nd| _	| j j
j }| j jj|| j j
j}| j j}y|t|t|f }W n4 tk
r   tdj|j|r|jn|tjY nX || j ||}|| j jjkrdj|}	|d k	r|	dj|7 }	|	dj| j j 7 }	t|	tjt|tjr8| j jj|j}
njt|tjrX| j jj|j}
nJt|tjrx| j jj|j }
n*t|tjr| j jj|j }
n
| j jj}
| j j
j!||| j jj| j jj| j jj|}| j j"|dk | j j
j#|t$|j%}| j j"|dk t|tj&r| j j
j'|| j j
j(t$|
| j jj}| j j"|dk |j)d k	r| j j
j'|| j j
j*t$|j)|j)}| j j"|dk |j)| _n0| j| j+kr| j j
j,r| j j
j- rt.d| j j
j!|| j jj| j jj| j jj|j%|
|}| j j"|dk | j j
j/|d || _0d S )	N   r   z6cipher {} in {} mode is not supported by this backend.zcipher {0.name} zin {0.name} mode z_is not supported by this backend (Your version of OpenSSL may be too old. Current version: {}.)r   z_delayed passing of GCM tag requires OpenSSL >= 1.0.2. To use this feature please update OpenSSL)1_backendZ_cipher_mode
_operation_tag
isinstancer	   ZBlockCipherAlgorithm
block_size_block_size_bytes_libZEVP_CIPHER_CTX_new_ffigcZEVP_CIPHER_CTX_freeZ_cipher_registrytypeKeyErrorr   formatnamer   ZUNSUPPORTED_CIPHERNULLZopenssl_version_textr
   ZModeWithInitializationVectorfrom_bufferZinitialization_vectorZModeWithTweakZtweakZModeWithNoncenonceZEVP_CipherInit_exopenssl_assertZEVP_CIPHER_CTX_set_key_lengthlenkeyGCMEVP_CIPHER_CTX_ctrlZEVP_CTRL_AEAD_SET_IVLENtagEVP_CTRL_AEAD_SET_TAG_DECRYPT"CRYPTOGRAPHY_OPENSSL_LESS_THAN_102CRYPTOGRAPHY_IS_LIBRESSLNotImplementedErrorZEVP_CIPHER_CTX_set_padding_ctx)selfbackendciphermodeZ	operationctxregistryadapterZ
evp_ciphermsgZiv_nonceres r4   P/tmp/pip-unpacked-wheel-vvkwn1hz/cryptography/hazmat/backends/openssl/ciphers.py__init__   s    





z_CipherContext.__init__c             C   s2   t t|| j d }| j||}t|d | S )Nr   )	bytearrayr    r   update_intobytes)r+   databufnr4   r4   r5   updatez   s    z_CipherContext.updatec             C   s   t |t || j d k r6tdjt || j d | jjjd| jjj|}| jjjd}| jj	j
| j||| jjj|t |}| jj|dk |d S )Nr   z1buffer must be at least {} bytes for this payloadzunsigned char *zint *r   )r    r   
ValueErrorr   r   r   castr   newr   EVP_CipherUpdater*   r   )r+   r:   r;   outlenr3   r4   r4   r5   r8      s    z_CipherContext.update_intoc             C   s|  t | jtjr| jd | j| jkrDt | jtjrD| jd krDt	d| j
jjd| j}| j
jjd}| j
jj| j||}|dkr| j
j }| rt | jtjrt| j
j|d j| j
jj| j
jj t	dt | jtjo| j| jkrB| j
jjd| j}| j
jj| j| j
jj| j|}| j
j|dk | j
jj|d d  | _| j
jj| j}| j
j|dk | j
jj|d |d  S )N    z4Authentication tag must be provided when decrypting.zunsigned char[]zint *r   zFThe length of the provided data is not a multiple of the block length.r   )r   r   r
   r"   r=   r   r&   ZModeWithAuthenticationTagr$   r>   r   r   r@   r   r   ZEVP_CipherFinal_exr*   Z_consume_errorsr   r   Z_lib_reason_matchZERR_LIB_EVPZ'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH_ENCRYPTr#   ZEVP_CTRL_AEAD_GET_TAGbufferr   ZEVP_CIPHER_CTX_cleanup)r+   r;   rB   r3   errorsZtag_bufr4   r4   r5   finalize   s@    




z_CipherContext.finalizec             C   s~   | j jjr| j jj rtdt|| jjk r@tdj	| jj| j jj
| j| j jjt||}| j j|dk || _| j S )NzUfinalize_with_tag requires OpenSSL >= 1.0.2. To use this method please update OpenSSLz.Authentication tag must be {} bytes or longer.r   )r   r   r'   r(   r)   r    r   Z_min_tag_lengthr>   r   r#   r*   r%   r   r   rG   )r+   r$   r3   r4   r4   r5   finalize_with_tag   s    
z _CipherContext.finalize_with_tagc             C   sN   | j jjd}| j jj| j| j jj|| j jj|t|}| j j	|dk d S )Nzint *r   )
r   r   r@   r   rA   r*   r   r   r    r   )r+   r:   rB   r3   r4   r4   r5   authenticate_additional_data   s
    z+_CipherContext.authenticate_additional_datar   N)__name__
__module____qualname__rD   r&   r6   r=   r8   rG   rH   rI   r   Zread_only_propertyr$   r4   r4   r4   r5   r      s   e6r   N)
__future__r   r   r   Zcryptographyr   Zcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr	   Z&cryptography.hazmat.primitives.ciphersr
   Zregister_interfaceZCipherContextZAEADCipherContextZAEADEncryptionContextZAEADDecryptionContextobjectr   r4   r4   r4   r5   <module>   s   


