o
    Df                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ d	d
lmZ ddlmZ G dd deZG dd deZeddeddeddedededgZdS )z
    authlib.jose.rfc7518
    ~~~~~~~~~~~~~~~~~~~~

    Cryptographic Algorithms for Cryptographic Algorithms for Content
    Encryption per `Section 5`_.

    .. _`Section 5`: https://tools.ietf.org/html/rfc7518#section-5
    N)default_backend)Cipher)AES)GCMCBC)PKCS7)
InvalidTag   )JWEEncAlgorithm   )
encode_intc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CBCHS2EncAlgorithm   c                 C   sT   d| d| | _ d}|||| _|| _|d | _|d | _ttd| | _d S )NAzCBC-HSz9AES_{}_CBC_HMAC_SHA_{} authenticated encryption algorithm   r	   sha)	nameformatdescriptionkey_sizekey_lenCEK_SIZEgetattrhashlibhash_alg)selfr   	hash_typetpl r   V/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/jose/rfc7518/jwe_encs.py__init__   s   

zCBCHS2EncAlgorithm.__init__c                 C   sD   t t|d d}|| | | }t||| j }|d | j S )Nr   @   )r   lenhmacnewr   digestr   )r   
ciphertextaadivkeyalmsgdr   r   r   _hmac(   s   zCBCHS2EncAlgorithm._hmacc                 C   s   |  | |d| j }|| jd }ttj }|||  }tt|t	|t
 d}	|	 }
|
||
  }| ||||}||fS )a  Key Encryption with AES_CBC_HMAC_SHA2.

        :param msg: text to be encrypt in bytes
        :param aad: additional authenticated data in bytes
        :param iv: initialization vector in bytes
        :param key: encrypted key in bytes
        :return: (ciphertext, iv, tag)
        Nbackend)check_ivr   r   r   
block_sizepadderupdatefinalizer   r   r   	encryptorr-   )r   r+   r'   r(   r)   hkeyekeypadpadded_datacipherencr&   tagr   r   r   encrypt.   s   
	zCBCHS2EncAlgorithm.encryptc                 C   s   |  | |d| j }|| jd }| ||||}t||s$t tt|t|t	 d}	|	
 }
|
||
  }ttj }|||  S )aD  Key Decryption with AES AES_CBC_HMAC_SHA2.

        :param ciphertext: ciphertext in bytes
        :param aad: additional authenticated data in bytes
        :param iv: initialization vector in bytes
        :param tag: authentication tag in bytes
        :param key: encrypted key in bytes
        :return: message
        Nr.   )r0   r   r-   r#   compare_digestr   r   r   r   r   	decryptorr3   r4   r   r1   unpadder)r   r&   r'   r(   r<   r)   r6   dkey_tagr:   r,   dataunpadr   r   r   decryptD   s   

zCBCHS2EncAlgorithm.decryptN)__name__
__module____qualname__IV_SIZEr    r-   r=   rE   r   r   r   r   r      s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	GCMEncAlgorithm`   c                 C   s,   d| d| _ d| d| _|| _|| _d S )Nr   r   zAES GCM using z-bit key)r   r   r   r   )r   r   r   r   r   r    b   s   
zGCMEncAlgorithm.__init__c                 C   sP   |  | tt|t|t d}| }|| |||  }||j	fS )a  Key Encryption with AES GCM

        :param msg: text to be encrypt in bytes
        :param aad: additional authenticated data in bytes
        :param iv: initialization vector in bytes
        :param key: encrypted key in bytes
        :return: (ciphertext, iv, tag)
        r.   )
r0   r   r   r   r   r5   authenticate_additional_datar3   r4   r<   )r   r+   r'   r(   r)   r:   r;   r&   r   r   r   r=   h   s   
	

zGCMEncAlgorithm.encryptc                 C   sH   |  | tt|t||t d}| }|| |||  S )a5  Key Decryption with AES GCM

        :param ciphertext: ciphertext in bytes
        :param aad: additional authenticated data in bytes
        :param iv: initialization vector in bytes
        :param tag: authentication tag in bytes
        :param key: encrypted key in bytes
        :return: message
        r.   )	r0   r   r   r   r   r?   rL   r3   r4   )r   r&   r'   r(   r<   r)   r:   r,   r   r   r   rE   x   s
   


zGCMEncAlgorithm.decryptN)rF   rG   rH   rI   r    r=   rE   r   r   r   r   rJ   ]   s
    rJ   r         i  i   )__doc__r#   r   cryptography.hazmat.backendsr   &cryptography.hazmat.primitives.ciphersr   1cryptography.hazmat.primitives.ciphers.algorithmsr   ,cryptography.hazmat.primitives.ciphers.modesr   r   &cryptography.hazmat.primitives.paddingr   cryptography.exceptionsr   rfc7516r
   utilr   r   rJ   JWE_ENC_ALGORITHMSr   r   r   r   <module>   s(    	G-