o
    ™¨ÊhÈ"  ã                   @   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                   @   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 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}z|t|ƒt|ƒf }W n tyY   td |j|rS|jn|¡tjƒ‚w || j ||ƒ}|| j jjkrxtd |j|rr|jn|¡tjƒ‚t|tjƒr‚|j}	n#t|tjƒrŒ|j}	nt|tjƒr–|j}	nt|tjƒr |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$ƒ�r1| 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'| _n| j| j)k�r1| j j
j*�r1| j j
j+�s1t,dƒ‚| j j
 || j jj| j jj|j#|	|¡}
| j   |
dk¡ | j j
 -|d¡ || _.d S )Né   r   z8cipher {0} in {1} mode is not supported by this backend.r   z_delayed passing of GCM tag requires OpenSSL >= 1.0.2. To use this feature please update OpenSSL)/Ú_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ÚNULLr
   ÚModeWithInitializationVectorÚ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Ú_DECRYPTÚ"CRYPTOGRAPHY_OPENSSL_LESS_THAN_102ÚCRYPTOGRAPHY_IS_LIBRESSLÚNotImplementedErrorÚEVP_CIPHER_CTX_set_paddingÚ_ctx)ÚselfÚbackendÚcipherÚmodeÚ	operationÚctxÚregistryÚadapterÚ
evp_cipherÚiv_nonceÚres© rD   ú^/var/www/html/env/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)r9   ÚdataÚbufÚnrD   rD   rE   Úupdatev   s   z_CipherContext.updatec                 C   s’   t |ƒt |ƒ| j d k rtd t |ƒ| j d ¡ƒ‚| jj d| jj |¡¡}| jj d¡}| jj	 
| j|||t |ƒ¡}| j |dk¡ |d S )Nr   z2buffer must be at least {0} bytes for this payloadzunsigned char *úint *r   )r,   r   Ú
ValueErrorr   r   r   ÚcastÚfrom_bufferÚnewr   ÚEVP_CipherUpdater8   r*   )r9   rJ   rK   ÚoutlenrC   rD   rD   rE   rH   {   s   þÿÿz_CipherContext.update_intoc                 C   sx  t | jtjƒr|  d¡ | 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rf| j
 ¡ }|sPt | jtjƒrPt‚| j
 |d  | j
jj| j
jj¡¡ 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 )Nó    z4Authentication tag must be provided when decrypting.zunsigned char[]rN   r   zFThe length of the provided data is not a multiple of the block length.r   )r   r   r
   r.   rM   r   r3   ÚModeWithAuthenticationTagr1   rO   r   r   rR   r   r   ÚEVP_CipherFinal_exr8   Ú_consume_errorsr   r*   Ú_lib_reason_matchÚERR_LIB_EVPÚ'EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTHÚ_ENCRYPTr/   ÚEVP_CTRL_AEAD_GET_TAGÚbufferr   ÚEVP_CIPHER_CTX_cleanup)r9   rK   rT   rC   ÚerrorsÚtag_bufrD   rD   rE   Úfinalize‹   sN   
ÿ
ÿ
þÿÿÿþz_CipherContext.finalizec                 C   s|   | j jjr| j jjstdƒ‚t|ƒ| jjk rtd 	| jj¡ƒ‚| j j 
| j| j jjt|ƒ|¡}| j  |dk¡ || _|  ¡ S )NzUfinalize_with_tag requires OpenSSL >= 1.0.2. To use this method please update OpenSSLz/Authentication tag must be {0} bytes or longer.r   )r   r   r4   r5   r6   r,   r   Ú_min_tag_lengthrO   r   r/   r8   r2   r*   r   rb   )r9   r1   rC   rD   rD   rE   Úfinalize_with_tagÁ   s(   ÿþÿÿÿþz _CipherContext.finalize_with_tagc                 C   sD   | j j d¡}| j j | j| j jj||t|ƒ¡}| j  |dk¡ d S )NrN   r   )	r   r   rR   r   rS   r8   r"   r,   r*   )r9   rJ   rT   rC   rD   rD   rE   Úauthenticate_additional_data×   s
   ÿz+_CipherContext.authenticate_additional_datar   N)Ú__name__Ú
__module__Ú__qualname__r\   r3   rF   rM   rH   rb   rd   re   r   Úread_only_propertyr1   rD   rD   rD   rE   r      s    a6r   N)Ú
__future__r   r   r   Úcryptographyr   Úcryptography.exceptionsr   r   r   Úcryptography.hazmat.primitivesr	   Ú&cryptography.hazmat.primitives.ciphersr
   Úregister_interfaceÚCipherContextÚAEADCipherContextÚAEADEncryptionContextÚAEADDecryptionContextÚobjectr   rD   rD   rD   rE   Ú<module>   s   



