o
    e                     @  sp   d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
 ejr/d dlmZ d dlmZ G dd	 d	ZdS )
    )annotationsN)InvalidSignatureUnsupportedAlgorithm_Reasons)constant_time)CBC)Backend)ciphersc                   @  sB   e Zd Z	dddd	ZdddZdddZdddZdddZdS )_CMACContextNbackendr   	algorithmciphers.BlockCipherAlgorithmreturnNonec           	      C  s   | |stdtj|| _|j| _|| _|jd | _	|d u ro| jj
}|t|tf }|| j|t}| jj }| j|| jjjk | jj|| jjj}| jj| j}| jj||t| j|| jjj}| j|dk || _d S )Nz#This backend does not support CMAC.      )cmac_algorithm_supportedr   r   UNSUPPORTED_CIPHER_backendkey_key
_algorithm
block_size_output_length_cipher_registrytyper   _libCMAC_CTX_newopenssl_assert_ffiNULLgcCMAC_CTX_freefrom_buffer	CMAC_Initlen_ctx)	selfr   r   ctxregistryadapter
evp_cipherkey_ptrres r.   \/var/www/html/venv/lib/python3.10/site-packages/cryptography/hazmat/backends/openssl/cmac.py__init__   s4   

z_CMACContext.__init__databytesc                 C  s,   | j j| j|t|}| j |dk d S )Nr   )r   r   CMAC_Updater&   r%   r   )r'   r1   r-   r.   r.   r/   update?   s   z_CMACContext.updatec                 C  sd   | j jd| j}| j jd| j}| j j| j||}| j |dk d | _| j j|d d  S )Nzunsigned char[]zsize_t *r   )	r   r   newr   r   
CMAC_Finalr&   r   buffer)r'   buflengthr-   r.   r.   r/   finalizeC   s   z_CMACContext.finalizec                 C  sV   | j j }| j j|| j jj}| j j|| j}| j |dk t	| j | j
|dS )Nr   )r(   )r   r   r   r   r!   r"   CMAC_CTX_copyr&   r   r
   r   )r'   
copied_ctxr-   r.   r.   r/   copyM   s   
z_CMACContext.copy	signaturec                 C  s    |   }t||stdd S )NzSignature did not match digest.)r:   r   bytes_eqr   )r'   r>   digestr.   r.   r/   verifyV   s   z_CMACContext.verify)N)r   r   r   r   r   r   )r1   r2   r   r   )r   r2   )r   r
   )r>   r2   r   r   )__name__
__module____qualname__r0   r4   r:   r=   rA   r.   r.   r.   r/   r
      s    
(


	r
   )
__future__r   typingcryptography.exceptionsr   r   r   cryptography.hazmat.primitivesr   ,cryptography.hazmat.primitives.ciphers.modesr   TYPE_CHECKING,cryptography.hazmat.backends.openssl.backendr   r	   r
   r.   r.   r.   r/   <module>   s   