o
     `!                     @   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ejeejeejeejG dd deZdS )	    )absolute_importdivisionprint_function)utils)
InvalidTagUnsupportedAlgorithm_Reasons)ciphers)modesc                   @   sR   e Zd 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dZdS )_CipherContext   r   i?c                 C   s  || _ || _|| _|| _d | _t| jtjr| jjd | _	nd| _	| j j
 }| j j|| j j
j}| j j}z|t|t|f }W n tyY   td|j|rS|jn|tjw || j ||}|| j jjkrd|}	|d urx|	d|7 }	|	d| j  7 }	t|	tjt|tjr| j j|j}
n2t|tjr| j j|j}
n#t|tjr| j j|j }
nt|tjr| j j|j }
n| j jj}
| j j
!||| j jj| j jj| j jj|}| j "|dk | j j
#|t$|j%}| j "|dk t|tj&r=| j j
'|| j j
j(t$|
| j jj}| j "|dk |j)d ur=| j j
'|| j j
j*t$|j)|j)}| j "|dk |j)| _| j j
!|| j jj| j jj| j j|j%|
|}| j "|dk | j j
+|d || _,d 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   )-_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ModeWithNonceZ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_TAGZEVP_CIPHER_CTX_set_padding_ctx)selfbackendZciphermodeZ	operationctxregistryadapterZ
evp_ciphermsgZiv_nonceres r.   home/ych/rk3568/buildroot/output/rockchip_rk3568/host/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/ciphers.py__init__   s   
	



z_CipherContext.__init__c                 C   s2   t t|| j d }| ||}t|d | S )Nr   )	bytearrayr   r   update_intobytes)r&   databufnr.   r.   r/   updatez   s   z_CipherContext.updatec                 C   s   t |}t ||| j d k rtdt || j d d}d}| jjd}| jj|}| jj|}||krl|| }	|| }
t| j	|| }| jj
| j|	||
|}| j|dk ||7 }||d 7 }||ks:|S )Nr   z1buffer must be at least {} bytes for this payloadr   int *)r   r   
ValueErrorr   r   r   newr   min_MAX_CHUNK_SIZEr   EVP_CipherUpdater%   r   )r&   r4   r5   Ztotal_data_lenZdata_processedZ	total_outoutlenZ
baseoutbufZ	baseinbufZoutbufZinbufZinlenr-   r.   r.   r/   r2      s.   z_CipherContext.update_intoc                 C   sd  | j | jkrt| jtjr| jd u rtd| jj	
d| j}| jj	
d}| jj| j||}|dkr\| j }|sDt| jtjrDt| jj|d | jjj| jjj|d tdt| jtjr| j | jkr| jj	
d| j}| jj| j| jjj| j|}| j|dk | jj	|d d  | _| jj| j}| j|dk | jj	|d |d  S )Nz4Authentication tag must be provided when decrypting.zunsigned char[]r8   r   )errorszFThe length of the provided data is not a multiple of the block length.r   )r   _DECRYPTr   r   r
   ZModeWithAuthenticationTagr#   r9   r   r   r:   r   r   ZEVP_CipherFinal_exr%   Z_consume_errorsr!   r   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&   r5   r>   r-   r?   Ztag_bufr.   r.   r/   finalize   sX   


z_CipherContext.finalizec                 C   s`   t || jjk rtd| jj| jj| j| jjj	t ||}| j
|dk || _|  S )Nz.Authentication tag must be {} bytes or longer.r   )r   r   Z_min_tag_lengthr9   r   r   r   r"   r%   r$   r   r   rC   )r&   r#   r-   r.   r.   r/   finalize_with_tag   s   z _CipherContext.finalize_with_tagc                 C   sN   | j jd}| j j| j| j jj|| j j|t|}| j 	|dk d S )Nr8   r   )
r   r   r:   r   r=   r%   r   r   r   r   )r&   r4   r>   r-   r.   r.   r/   authenticate_additional_data   s   z+_CipherContext.authenticate_additional_datar   N)__name__
__module____qualname__rA   r@   r<   r0   r7   r2   rC   rD   rE   r   Zread_only_propertyr#   r.   r.   r.   r/   r      s    d3r   N)
__future__r   r   r   cryptographyr   Zcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr	   Z&cryptography.hazmat.primitives.ciphersr
   Zregister_interfaceZCipherContextZAEADCipherContextZAEADEncryptionContextZAEADDecryptionContextobjectr   r.   r.   r.   r/   <module>   s   



