
    ([f                     8   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e          Z G 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                   *    e Zd ZdZd Zd Zd Zd ZdS )CBCHS2EncAlgorithm   c                     d| d| | _         d}|                    ||          | _        || _        |dz  | _        |dz  | _        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tpls       Q/var/www/piapp/venv/lib/python3.11/site-packages/authlib/jose/rfc7518/jwe_encs.py__init__zCBCHS2EncAlgorithm.__init__   sq    333	33	I::h	:: !1} 1):y):):;;    c                     t          t          |          dz  d          }||z   |z   |z   }t          j        ||| j                                                  }|d | j                 S )Nr   @   )r   lenhmacnewr   digestr   )r   
ciphertextaadivkeyalmsgds           r!   _hmaczCBCHS2EncAlgorithm._hmac(   s`    C1b))Bh#b(HS#t}--4466$,r#   c                 6   |                      |           |d| j                 }|| j        d         }t          t          j                                                  }|                    |          |                                z   }t          t          |          t          |          t                                }	|	                                }
|
                    |          |
                                z   }|                     ||||          }||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   	encryptorr1   )r   r/   r+   r,   r-   hkeyekeypadpadded_datacipherencr*   tags                r!   encryptzCBCHS2EncAlgorithm.encrypt.   s     	b=DL=!4<==!CN##**,,jjoo6D		3r77O4E4EFFF  ZZ,,s||~~=
jjS"d333r#   c                 t   |                      |           |d| j                 }|| j        d         }|                     ||||          }t          j        ||          st                      t          t          |          t          |          t                                }	|	
                                }
|
                    |          |
                                z   }t          t          j                                                  }|                    |          |                                z   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
        Nr3   )r5   r   r1   r'   compare_digestr	   r   r   r   r   	decryptorr8   r9   r   r6   unpadder)r   r*   r+   r,   rA   r-   r;   dkey_tagr?   r0   dataunpads                r!   decryptzCBCHS2EncAlgorithm.decryptD   s     	b=DL=!4<==!zz*c2t44"4-- 	,,D		3r77O4E4EFFFxx
##ajjll2cn%%..00||D!!ENN$4$444r#   N)__name__
__module____qualname__IV_SIZEr"   r1   rB   rK    r#   r!   r   r      sW         G< < <       ,5 5 5 5 5r#   r   c                   $    e Zd ZdZd Zd Zd ZdS )GCMEncAlgorithm`   c                 N    d| d| _         d| d| _        || _        || _        d S )Nr   r   zAES GCM using z-bit key)r   r   r   r   )r   r   s     r!   r"   zGCMEncAlgorithm.__init__b   s9    %%%%	>H>>>  r#   c                 T   |                      |           t          t          |          t          |          t	                                }|                                }|                    |           |                    |          |                                z   }||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)
        r3   )
r5   r   r   r   r   r:   authenticate_additional_datar8   r9   rA   )r   r/   r+   r,   r-   r?   r@   r*   s           r!   rB   zGCMEncAlgorithm.encrypth   s     	bC#b''?3D3DEEE  ((---ZZ__s||~~5
37""r#   c                 D   |                      |           t          t          |          t          ||          t	                                }|                                }|                    |           |                    |          |                                z   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
        r3   )	r5   r   r   r   r   rE   rV   r8   r9   )r   r*   r+   r,   rA   r-   r?   r0   s           r!   rK   zGCMEncAlgorithm.decryptx   s     	bC#b#,,8I8IJJJ	&&s+++xx
##ajjll22r#   N)rL   rM   rN   rO   r"   rB   rK   rP   r#   r!   rR   rR   ]   sH         G! ! !# # # 3 3 3 3 3r#   rR   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   rR   JWE_ENC_ALGORITHMSrP   r#   r!   <module>rd      s~      8 8 8 8 8 8 9 9 9 9 9 9 A A A A A A A A A A A A A A 8 8 8 8 8 8 . . . . . . % % % % % %      D5 D5 D5 D5 D5 D5 D5 D5N)3 )3 )3 )3 )3o )3 )3 )3Z sC  sC  sC  OCOCOC   r#   