
    Ld                     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z  | _        |dz  | _        t          t          d                     |                    | _        d S )NzA{}CBC-HS{}z9AES_{}_CBC_HMAC_SHA_{} authenticated encryption algorithm   r
   zsha{})	formatnamedescriptionkey_sizekey_lenCEK_SIZEgetattrhashlibhash_alg)selfr   	hash_typetpls       CF:\djangOuth\env\Lib\site-packages\authlib/jose/rfc7518/jwe_encs.py__init__zCBCHS2EncAlgorithm.__init__   st    !((9==	I::h	:: !1} 1	)B)BCC    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   	encryptorr/   )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
        Nr1   )r3   r   r/   r%   compare_digestr	   r   r   r   r   	decryptorr6   r7   r   r4   unpadder)r   r(   r)   r*   r?   r+   r9   dkey_tagr=   r.   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    r/   r@   rI    r!   r   r   r      sZ         GD D D       ,5 5 5 5 5r!   r   c                   $    e Zd ZdZd Zd Zd ZdS )GCMEncAlgorithm`   c                     d                     |          | _        d                     |          | _        || _        || _        d S )NzA{}GCMzAES GCM using {}-bit key)r   r   r   r   r   )r   r   s     r   r    zGCMEncAlgorithm.__init__b   s=    OOH--	5<<XFF  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)
        r1   )
r3   r   r   r   r   r8   authenticate_additional_datar6   r7   r?   )r   r-   r)   r*   r+   r=   r>   r(   s           r   r@   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
        r1   )	r3   r   r   r   r   rC   rT   r6   r7   )r   r(   r)   r*   r?   r+   r=   r.   s           r   rI   zGCMEncAlgorithm.decryptx   s     	bC#b#,,8I8IJJJ	&&s+++xx
##ajjll22r!   N)rJ   rK   rL   rM   r    r@   rI   rN   r!   r   rP   rP   ]   sH         G! ! !# # # 3 3 3 3 3r!   rP   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   rP   JWE_ENC_ALGORITHMSrN   r!   r   <module>rb      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!   