o
    -&%a  ã                   @   sL   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 G dd„ de
ƒZdS )é    )Úutils)ÚInvalidSignatureÚUnsupportedAlgorithmÚ_Reasons)Úconstant_time)ÚCBCc                   @   s\   e Zd Zddd„Ze d¡Zdeddfdd„Zdefd	d
„Z	ddd„Z
deddfdd„ZdS )Ú_CMACContextNc           	      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)	ÚselfÚbackendÚ	algorithmÚctxÚregistryÚadapterÚ
evp_cipherÚkey_ptrÚres© r)   úK/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/cmac.pyÚ__init__   s4   
þû
z_CMACContext.__init__r   ÚdataÚreturnc                 C   s,   | j j | j|t|ƒ¡}| j  |dk¡ d S )Nr
   )r   r   ÚCMAC_Updater   r   r   )r    r,   r(   r)   r)   r*   Úupdate6   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*   Úfinalize:   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*   ÚcopyD   s   
ÿz_CMACContext.copyÚ	signaturec                 C   s    |   ¡ }t ||¡stdƒ‚d S )NzSignature did not match digest.)r5   r   Úbytes_eqr   )r    r9   Údigestr)   r)   r*   ÚverifyM   s   ÿz_CMACContext.verify)N)r-   r   )Ú__name__Ú
__module__Ú__qualname__r+   r   Úread_only_propertyr"   Úbytesr/   r5   r8   r<   r)   r)   r)   r*   r      s    

#

	r   N)Úcryptographyr   Úcryptography.exceptionsr   r   r   Úcryptography.hazmat.primitivesr   Ú,cryptography.hazmat.primitives.ciphers.modesr   Úobjectr   r)   r)   r)   r*   Ú<module>   s
   