o
    �)%au  ã                   @   sF   d dl mZ d dlmZmZmZ d dlmZmZ G dd„ dej	ƒZ
dS )é    )Úutils)ÚInvalidSignatureÚUnsupportedAlgorithmÚ_Reasons)Úconstant_timeÚhashesc                   @   sj   e Zd Z	ddedejfdd„Ze d¡Z	ddd	„Z
d
eddfdd„Zdefdd„Zdeddfdd„ZdS )Ú_HMACContextNÚkeyÚ	algorithmc                 C   sÈ   || _ || _|d u r\| jj ¡ }| j || jjjk¡ | jj || jjj¡}| j 	|¡}|| jjjkr=t
d |j¡tjƒ‚| jj |¡}| jj ||t|ƒ|| jjj¡}| j |dk¡ || _|| _d S )Nz*{} is not a supported hash on this backendr   )Ú
_algorithmÚ_backendÚ_libÚHMAC_CTX_newÚopenssl_assertÚ_ffiÚNULLÚgcÚHMAC_CTX_freeÚ_evp_md_from_algorithmr   ÚformatÚnamer   ÚUNSUPPORTED_HASHÚfrom_bufferÚHMAC_Init_exÚlenÚ_ctxÚ_key)ÚselfÚbackendr	   r
   ÚctxÚevp_mdÚkey_ptrÚres© r#   úK/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/hmac.pyÚ__init__   s*   ÿüÿ
z_HMACContext.__init__r   Úreturnc                 C   sp   | j j ¡ }| j  || j jjk¡ | j j || j jj¡}| j j || j	¡}| j  |dk¡ t
| j | j| j|d�S )Nr   )r   )r   r   r   r   r   r   r   r   ÚHMAC_CTX_copyr   r   r   r
   )r   Ú
copied_ctxr"   r#   r#   r$   Úcopy-   s   
ÿÿz_HMACContext.copyÚdatac                 C   s:   | j j |¡}| j j | j|t|ƒ¡}| j  |dk¡ d S )Nr   )r   r   r   r   ÚHMAC_Updater   r   r   )r   r*   Údata_ptrr"   r#   r#   r$   Úupdate9   s   z_HMACContext.updatec                 C   sz   | j j d| j jj¡}| j j d¡}| j j | j||¡}| j  |dk¡ | j  |d | jj	k¡ | j j 
|¡d |d … S )Nzunsigned char[]zunsigned int *r   )r   r   Únewr   ÚEVP_MAX_MD_SIZEÚ
HMAC_Finalr   r   r
   Údigest_sizeÚbuffer)r   ÚbufÚoutlenr"   r#   r#   r$   Úfinalize>   s   
ÿz_HMACContext.finalizeÚ	signaturec                 C   s    |   ¡ }t ||¡stdƒ‚d S )NzSignature did not match digest.)r5   r   Úbytes_eqr   )r   r6   Údigestr#   r#   r$   ÚverifyH   s   ÿz_HMACContext.verify)N)r&   r   )Ú__name__Ú
__module__Ú__qualname__Úbytesr   ÚHashAlgorithmr%   r   Úread_only_propertyr
   r)   r-   r5   r9   r#   r#   r#   r$   r      s    ÿÿ
ÿ


r   N)Úcryptographyr   Úcryptography.exceptionsr   r   r   Úcryptography.hazmat.primitivesr   r   ÚHashContextr   r#   r#   r#   r$   Ú<module>   s   