U
    ÒZj½  ã                   @   sd   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	rRd dlm
Z
 d dlmZ G dd„ dƒZdS )	é    N)ÚInvalidSignatureÚUnsupportedAlgorithmÚ_Reasons)Úconstant_time)ÚCBC)Úciphers)ÚBackendc                   @   s\   e Zd Zdddddœdd„Zeddœdd	„Zed
œdd„Zd d
œdd„Zeddœdd„ZdS )Ú_CMACContextNr   zciphers.BlockCipherAlgorithm)ÚbackendÚ	algorithmÚreturnc           	      C   sè   |  |¡stdtjƒ‚|| _|j| _|| _|jd | _	|d krÞ| 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.é   é   )Zcmac_algorithm_supportedr   r   ZUNSUPPORTED_CIPHERÚ_backendÚkeyÚ_keyÚ
_algorithmÚ
block_sizeÚ_output_lengthZ_cipher_registryÚtyper   Ú_libÚCMAC_CTX_newÚopenssl_assertÚ_ffiZNULLÚgcÚCMAC_CTX_freeZfrom_bufferZ	CMAC_InitÚlenÚ_ctx)	Úselfr
   r   ÚctxÚregistryÚadapterZ
evp_cipherZkey_ptrÚres© r#   úl/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/cmac.pyÚ__init__   s4    
þûz_CMACContext.__init__)Údatar   c                 C   s,   | j j | j|t|ƒ¡}| j  |dk¡ d S )Nr   )r   r   ZCMAC_Updater   r   r   )r   r&   r"   r#   r#   r$   Úupdate=   s    z_CMACContext.update)r   c                 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   Z
CMAC_Finalr   r   Úbuffer)r   ÚbufÚlengthr"   r#   r#   r$   ÚfinalizeA   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   ZCMAC_CTX_copyr   r   r	   r   )r   Z
copied_ctxr"   r#   r#   r$   ÚcopyK   s     ÿz_CMACContext.copy)Ú	signaturer   c                 C   s    |   ¡ }t ||¡stdƒ‚d S )NzSignature did not match digest.)r,   r   Zbytes_eqr   )r   r.   Údigestr#   r#   r$   ÚverifyT   s    z_CMACContext.verify)N)	Ú__name__Ú
__module__Ú__qualname__r%   Úbytesr'   r,   r-   r0   r#   r#   r#   r$   r	      s    üû(
	r	   )ÚtypingZcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr   Z,cryptography.hazmat.primitives.ciphers.modesr   ÚTYPE_CHECKINGr   Z,cryptography.hazmat.backends.openssl.backendr   r	   r#   r#   r#   r$   Ú<module>   s   