U
    *}f'                     @   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 G dd deZdS )	    )absolute_importdivisionprint_function)utils)InvalidSignatureUnsupportedAlgorithm_Reasons)constant_time)CBCc                   @   s@   e Zd ZdddZedZdd Zdd Zd	d
 Z	dd Z
dS )_CMACContextNc           	      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_freefrom_bufferZ	CMAC_Initlen_ctx)	selfbackend	algorithmctxregistryadapterZ
evp_cipherZkey_ptrres r%   M/tmp/pip-unpacked-wheel-x36vw73o/cryptography/hazmat/backends/openssl/cmac.py__init__   s4    
z_CMACContext.__init__r   c                 C   s,   | j j| j|t|}| j |dk d S )Nr   )r   r   ZCMAC_Updater   r   r   )r   datar$   r%   r%   r&   update8   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   Z
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   ZCMAC_CTX_copyr   r   r   r   )r   Z
copied_ctxr$   r%   r%   r&   copyF   s     z_CMACContext.copyc                 C   s    |   }t||stdd S )NzSignature did not match digest.)r.   r	   Zbytes_eqr   )r   	signaturedigestr%   r%   r&   verifyO   s    z_CMACContext.verify)N)__name__
__module____qualname__r'   r   Zread_only_propertyr    r)   r.   r/   r2   r%   r%   r%   r&   r      s   
#

	r   N)
__future__r   r   r   Zcryptographyr   Zcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr	   Z,cryptography.hazmat.primitives.ciphers.modesr
   objectr   r%   r%   r%   r&   <module>   s
   