
    k7i                        S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  S
SKJr  SSKJr   " S S\5      r " S S\5      r\" SS5      \" SS5      \" SS5      \" S5      \" S5      \" S5      /rg)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                   0    \ rS rSrSrS rS rS rS rSr	g)	CBCHS2EncAlgorithm      c                     SU SU 3U l         SnUR                  X5      U l        Xl        US-  U l        US-  U l        [        [        SU 35      U l        g )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       W/home/james-whalen/.local/lib/python3.13/site-packages/authlib/jose/rfc7518/jwe_encs.py__init__CBCHS2EncAlgorithm.__init__   s^    z	{3	I::h: !1} 13yk):;    c                     [        [        U5      S-  S5      nX#-   U-   U-   n[        R                  " XFU R                  5      R                  5       nUS U R                   $ )Nr   @   )r   lenhmacnewr   digestr   )r   
ciphertextaadivkeyalmsgds           r"   _hmacCBCHS2EncAlgorithm._hmac+   sU    C1b)h#b(HHSt}}-4464<<  r%   c                    U R                  U5        USU R                   nX@R                  S n[        [        R                  5      R                  5       nUR                  U5      UR                  5       -   n[        [        U5      [        U5      [        5       S9n	U	R                  5       n
U
R                  U5      U
R                  5       -   nU R                  XX55      nX4$ )zKey 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   	encryptorr3   )r   r1   r-   r.   r/   hkeyekeypadpadded_datacipherencr,   tags                r"   encryptCBCHS2EncAlgorithm.encrypt1   s     	b>T\\"<<>"CNN#**,jjo6D	3r7O4EF ZZ,s||~=
jj"3r%   c                    U R                  U5        USU R                   nXPR                  S nU R                  XX65      n[        R                  " X5      (       d
  [        5       e[        [        U5      [        U5      [        5       S9n	U	R                  5       n
U
R                  U5      U
R                  5       -   n[        [        R                  5      R                  5       nUR                  U5      UR                  5       -   $ )a  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
Nr6   )r8   r   r3   r)   compare_digestr   r   r   r   r   	decryptorr;   r<   r	   r9   unpadder)r   r,   r-   r.   rD   r/   r>   dkey_tagrB   r2   dataunpads                r"   decryptCBCHS2EncAlgorithm.decryptG   s     	b>T\\"<<>"zz*24""4--,D	3r7O4EFxx
#ajjl2cnn%..0||D!ENN$444r%   )r   r   r   r   r   r   N)
__name__
__module____qualname____firstlineno__IV_SIZEr#   r3   rE   rO   __static_attributes__ r%   r"   r   r      s     G<!,5r%   r   c                   *    \ rS rSrSrS rS rS rSrg)GCMEncAlgorithm`   c                 H    SU S3U l         SU S3U l        Xl        Xl        g )Nr   r   zAES GCM using z-bit key)r   r   r   r   )r   r   s     r"   r#   GCMEncAlgorithm.__init__e   s-    z%	+H:X>  r%   c                    U R                  U5        [        [        U5      [        U5      [	        5       S9nUR                  5       nUR                  U5        UR                  U5      UR                  5       -   nXvR                  4$ )zKey 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)
r6   )
r8   r   r   r   r   r=   authenticate_additional_datar;   r<   rD   )r   r1   r-   r.   r/   rB   rC   r,   s           r"   rE   GCMEncAlgorithm.encryptk   sj     	bC#b'?3DE ((-ZZ_s||~5
77""r%   c                     U R                  U5        [        [        U5      [        X45      [	        5       S9nUR                  5       nUR                  U5        UR                  U5      UR                  5       -   $ )zKey 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
r6   )	r8   r   r   r   r   rI   r^   r;   r<   )r   r,   r-   r.   rD   r/   rB   r2   s           r"   rO   GCMEncAlgorithm.decrypt{   s_     	bC#b,8IJ	&&s+xx
#ajjl22r%   )r   r   r   r   N)	rQ   rR   rS   rT   rU   r#   rE   rO   rV   rW   r%   r"   rY   rY   `   s     G!# 3r%   rY   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   rY   JWE_ENC_ALGORITHMSrW   r%   r"   <module>rn      s      . 8 9 A < < 8 % D5 D5N)3o )3Z sC sC sC CCC r%   