o
    ™¨ÊhÍ  ã                   @   sd   d dl mZmZmZ d dlmZ d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 )é    )Úabsolute_importÚdivisionÚprint_function)Ú
InvalidTagé   c                 C   sn   ddl m}m}m} t| |ƒrdS t| |ƒr#d t| jƒd ¡ d¡S t| |ƒs*J ‚d t| jƒd ¡ d¡S )Nr   )ÚAESCCMÚAESGCMÚChaCha20Poly1305s   chacha20-poly1305zaes-{0}-ccmé   Úasciizaes-{0}-gcm)	Ú+cryptography.hazmat.primitives.ciphers.aeadr   r   r	   Ú
isinstanceÚformatÚlenÚ_keyÚencode)Úcipherr   r   r	   © r   ú[/var/www/html/env/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/aead.pyÚ_aead_cipher_name   s   

r   c           
   
   C   sB  | j  |¡}|  || jjk¡ | j  ¡ }| j || j j¡}| j  ||| jj| jj| jjt	|t
kƒ¡}	|  |	dk¡ | j  |t|ƒ¡}	|  |	dk¡ | j  || j jt|ƒ| jj¡}	|  |	dk¡ |tkrx| j  || j jt|ƒ|¡}	|  |	dk¡ n| j  || j j|| jj¡}	| j  || jj| jj||t	|t
kƒ¡}	|  |	dk¡ |S )Nr   )Ú_libÚEVP_get_cipherbynameÚopenssl_assertÚ_ffiÚNULLÚEVP_CIPHER_CTX_newÚgcÚEVP_CIPHER_CTX_freeÚEVP_CipherInit_exÚintÚ_ENCRYPTÚEVP_CIPHER_CTX_set_key_lengthr   ÚEVP_CIPHER_CTX_ctrlÚEVP_CTRL_AEAD_SET_IVLENÚ_DECRYPTÚEVP_CTRL_AEAD_SET_TAG)
ÚbackendÚcipher_nameÚkeyÚnonceÚtagÚtag_lenÚ	operationÚ
evp_cipherÚctxÚresr   r   r   Ú_aead_setup   sJ   

ûþÿÿ
úr0   c                 C   s:   | j  d¡}| j || j j|| j j|¡}|  |dk¡ d S ©Núint *r   )r   Únewr   ÚEVP_CipherUpdater   r   )r&   r.   Údata_lenÚintptrr/   r   r   r   Ú_set_lengthE   s   ûr7   c                 C   s:   | j  d¡}| j || j j||t|ƒ¡}|  |dk¡ d S r1   )r   r3   r   r4   r   r   r   )r&   r.   Úassociated_dataÚoutlenr/   r   r   r   Ú_process_aadQ   s
   ÿr:   c                 C   s^   | j  d¡}| j  dt|ƒ¡}| j ||||t|ƒ¡}|  |dk¡ | j  ||d ¡d d … S )Nr2   úunsigned char[]r   )r   r3   r   r   r4   r   Úbuffer)r&   r.   Údatar9   Úbufr/   r   r   r   Ú_process_dataY   s
   r?   c                 C   sê   ddl m} t|ƒ}t| ||j|d |tƒ}t||ƒr"t| |t|ƒƒ t	| ||ƒ t
| ||ƒ}	| j d¡}
| j || jj|
¡}|  |dk¡ |  |
d dk¡ | j d|¡}| j || jj||¡}|  |dk¡ | j |¡d d … }|	| S )Nr   ©r   r2   r;   )r   r   r   r0   r   r    r   r7   r   r:   r?   r   r3   r   ÚEVP_CipherFinal_exr   r   r"   ÚEVP_CTRL_AEAD_GET_TAGr<   )r&   r   r)   r=   r8   Ú
tag_lengthr   r'   r.   Úprocessed_datar9   r/   Útag_bufr*   r   r   r   Ú_encrypta   s(   ÿ
ÿrF   c                 C   s2  ddl m} t|ƒ|k rt‚|| d … }|d | … }t|ƒ}t| ||j|||tƒ}	t||ƒr8t	| |	t|ƒƒ t
| |	|ƒ t||ƒrw| j d¡}
| j dt|ƒ¡}| j |	||
|t|ƒ¡}|dkrh|  ¡  t‚| j ||
d ¡d d … }|S t| |	|ƒ}| j d¡}
| j |	| jj|
¡}|dkr—|  ¡  t‚|S )Nr   r@   r2   r;   r   )r   r   r   r   r   r0   r   r$   r   r7   r:   r   r3   r   r4   Ú_consume_errorsr<   r?   rA   r   )r&   r   r)   r=   r8   rC   r   r*   r'   r.   r9   r>   r/   rD   r   r   r   Ú_decrypt|   s8   ÿ

	ùrH   N)Ú
__future__r   r   r   Úcryptography.exceptionsr   r    r$   r   r0   r7   r:   r?   rF   rH   r   r   r   r   Ú<module>   s   *