
    ~h                        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
 ddl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y)zauthlib.jose.rfc7518.
~~~~~~~~~~~~~~~~~~~~

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

.. _`Section 5`: https://tools.ietf.org/html/rfc7518#section-5
    N)
InvalidTag)default_backend)Cipher)AES)CBC)GCM)PKCS7   )JWEEncAlgorithm   )
encode_intc                   (    e Zd ZdZd Zd Zd Zd Zy)CBCHS2EncAlgorithm   c                     d| d| | _         d}|j                  ||      | _        || _        |dz  | _        |dz  | _        t        t        d|       | _        y )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       Y/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/authlib/jose/rfc7518/jwe_encs.py__init__zCBCHS2EncAlgorithm.__init__   sb    z	{3	I::h	: !1} 13yk):;    c                     t        t        |      dz  d      }||z   |z   |z   }t        j                  ||| j                        j                         }|d | j                   S )Nr   @   )r   lenhmacnewr   digestr   )r   
ciphertextaadivkeyalmsgds           r!   _hmaczCBCHS2EncAlgorithm._hmac+   sY    C1b)Bh#b(HHS#t}}-4464<<  r#   c                    | j                  |       |d| j                   }|| j                  d }t        t        j                        j                         }|j                  |      |j                         z   }t        t        |      t        |      t                     }	|	j                         }
|
j                  |      |
j                         z   }| j                  ||||      }||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.encrypt1   s     	b>T\\"4<<>"CNN#**,jjo6D	3r7O4EF ZZ,s||~=
jjS"d33r#   c                    | j                  |       |d| j                   }|| j                  d }| j                  ||||      }t        j                  ||      s
t               t        t        |      t        |      t                     }	|	j                         }
|
j                  |      |
j                         z   }t        t        j                        j                         }|j                  |      |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.decryptG   s     	b>T\\"4<<>"zz*c2t4""4-,D	3r7O4EFxx
#ajjl2cnn%..0||D!ENN$444r#   N)__name__
__module____qualname__IV_SIZEr"   r1   rB   rK    r#   r!   r   r      s     G<!,5r#   r   c                   "    e Zd ZdZd Zd Zd Zy)GCMEncAlgorithm`   c                 L    d| d| _         d| d| _        || _        || _        y )Nr   r   zAES GCM using z-bit key)r   r   r   r   )r   r   s     r!   r"   zGCMEncAlgorithm.__init__e   s1    z%	+H:X>  r#   c                    | j                  |       t        t        |      t        |      t	                     }|j                         }|j                  |       |j                  |      |j                         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.encryptk   sl     	bC#b'?3DE ((-ZZ_s||~5
377""r#   c                     | j                  |       t        t        |      t        ||      t	                     }|j                         }|j                  |       |j                  |      |j                         z   S )a6  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.decrypt{   sa     	bC#b#,8IJ	&&s+xx
#ajjl22r#   N)rL   rM   rN   rO   r"   rB   rK   rP   r#   r!   rR   rR   `   s     G!# 3r#   rR   r         i  i   )__doc__r   r'   cryptography.exceptionsr   cryptography.hazmat.backendsr   &cryptography.hazmat.primitives.ciphersr   1cryptography.hazmat.primitives.ciphers.algorithmsr   ,cryptography.hazmat.primitives.ciphers.modesr   r   &cryptography.hazmat.primitives.paddingr	   rfc7516r   utilr   r   rR   JWE_ENC_ALGORITHMSrP   r#   r!   <module>rd      s      . 8 9 A < < 8 % D5 D5N)3o )3Z sC sC sC CCC r#   