o
    îRµi$  ã                   @   s|   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 )é    )Úutils)Ú
InvalidTagÚUnsupportedAlgorithmÚ_Reasons)Úciphers)Úmodesc                   @   s€   e Zd ZdZdZdZdd„ Zdedefdd	„Zdede	fd
d„Z
defdd„Zdedefdd„Zdeddf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rˆd |¡}	|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   )-Ú_backendÚ_cipherÚ_modeÚ
_operationÚ_tagÚ
isinstancer   ÚBlockCipherAlgorithmÚ
block_sizeÚ_block_size_bytesÚ_libÚEVP_CIPHER_CTX_newÚ_ffiÚgcÚEVP_CIPHER_CTX_freeÚ_cipher_registryÚtypeÚKeyErrorr   ÚformatÚnamer   ÚUNSUPPORTED_CIPHERÚNULLÚopenssl_version_textr   ÚModeWithInitializationVectorÚfrom_bufferÚinitialization_vectorÚModeWithTweakÚtweakÚModeWithNonceÚnonceÚEVP_CipherInit_exÚopenssl_assertÚEVP_CIPHER_CTX_set_key_lengthÚlenÚkeyÚGCMÚEVP_CIPHER_CTX_ctrlÚEVP_CTRL_AEAD_SET_IVLENÚtagÚEVP_CTRL_AEAD_SET_TAGÚEVP_CIPHER_CTX_set_paddingÚ_ctx)ÚselfÚbackendÚcipherÚmodeÚ	operationÚctxÚregistryÚadapterÚ
evp_cipherÚmsgÚiv_nonceÚres© r@   úN/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/ciphers.pyÚ__init__   s¬   
ÿÿûÿ	
ýÿ
ú
ÿüüú
z_CipherContext.__init__ÚdataÚreturnc                 C   s2   t t|ƒ| j d ƒ}|  ||¡}t|d |… ƒS )Nr	   )Ú	bytearrayr+   r   Úupdate_intoÚbytes)r4   rC   ÚbufÚnr@   r@   rA   Úupdatey   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|dd�}| jj |¡}||kr}|| }	|| }
t| j	|| ƒ}| jj
 | j|	||
|¡}|dkrgt| jtjƒrgtdƒ‚| j |dk¡ ||7 }||d 7 }||ks<|S )Nr	   z1buffer must be at least {} bytes for this payloadr   úint *T)Úrequire_writablezeIn XTS mode you must supply at least a full block in the first update call. For AES this is 16 bytes.)r+   r   Ú
ValueErrorr   r   r   Únewr"   ÚminÚ_MAX_CHUNK_SIZEr   ÚEVP_CipherUpdater3   r   r   r   ÚXTSr)   )r4   rC   rH   Útotal_data_lenÚdata_processedÚ	total_outÚoutlenÚ
baseoutbufÚ	baseinbufÚoutbufÚinbufÚinlenr?   r@   r@   rA   rF   ~   s6   þÿÿðz_CipherContext.update_intoc                 C   sŠ  | 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ro| j ¡ }|sDt| jtjƒrDt‚| jj|d  | jjj| jjj¡pg| jjjog|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[]rK   r   )ÚerrorszFThe length of the provided data is not a multiple of the block length.r	   )r   Ú_DECRYPTr   r   r   ÚModeWithAuthenticationTagr0   rM   r   r   rN   r   r   ÚEVP_CipherFinal_exr3   Ú_consume_errorsr-   r   r)   Ú_lib_reason_matchÚERR_LIB_EVPÚ'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHÚCryptography_HAS_PROVIDERSÚERR_LIB_PROVÚPROV_R_WRONG_FINAL_BLOCK_LENGTHÚ_ENCRYPTr.   ÚEVP_CTRL_AEAD_GET_TAGÚbufferr   ÚEVP_CIPHER_CTX_reset)r4   rH   rV   r?   r\   Útag_bufr@   r@   rA   Úfinalize    s\   ÿ
ÿ
þ
þôÿÿÿüz_CipherContext.finalizer0   c                 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   Ú_min_tag_lengthrM   r   r   r   r.   r3   r1   r)   r   rl   )r4   r0   r?   r@   r@   rA   Úfinalize_with_tagÚ   s   ÿÿÿz _CipherContext.finalize_with_tagNc                 C   sN   | j j d¡}| j j | j| j jj|| j j |¡t|ƒ¡}| j  	|dk¡ d S )NrK   r   )
r   r   rN   r   rQ   r3   r   r"   r+   r)   )r4   rC   rV   r?   r@   r@   rA   Úauthenticate_additional_dataè   s   ûz+_CipherContext.authenticate_additional_datar   )Ú__name__Ú
__module__Ú__qualname__rg   r]   rP   rB   rG   rJ   ÚintrF   rl   rn   ro   r   Úread_only_propertyr0   r@   r@   r@   rA   r      s    d":r   N)Úcryptographyr   Úcryptography.exceptionsr   r   r   Úcryptography.hazmat.primitivesr   Ú&cryptography.hazmat.primitives.ciphersr   Úregister_interfaceÚCipherContextÚAEADCipherContextÚAEADEncryptionContextÚAEADDecryptionContextÚobjectr   r@   r@   r@   rA   Ú<module>   s   



