§
    (ê[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Ñ)Ô)ˆØ�B‰h˜Ñ# bÑ(ˆÝŒH�S˜#˜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ÑÔÐØ�=�D”L�=Ô!ˆØ�4”<�=�=Ô!ˆå•C”NÑ#Ô#×*Ò*Ñ,Ô,ˆØ—j’j ‘o”o¨¯ª©¬Ñ6ˆå�˜D™	œ	¥3 r¡7¤7µOÑ4EÔ4EÐFÑFÔFˆØ×ÒÑ Ô ˆØ—Z’Z Ñ,Ô,¨s¯|ª|©~¬~Ñ=ˆ
Ø�jŠj˜ 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ÑÔÐØ�=�D”L�=Ô!ˆØ�4”<�=�=Ô!ˆà�zŠz˜* c¨2¨tÑ4Ô4ˆÝÔ" 4¨Ñ-Ô-ð 	Ý‘,”,Ðå�˜D™	œ	¥3 r¡7¤7µOÑ4EÔ4EÐFÑFÔFˆØ×ÒÑÔˆØ�xŠx˜
Ñ#Ô# a§j¢j¡l¤lÑ2ˆÝ•c”nÑ%Ô%×.Ò.Ñ0Ô0ˆØ�|Š|˜DÑ!Ô! E§N¢NÑ$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¡'¤'µ?Ñ3DÔ3DÐEÑEÔEˆØ×ÒÑ Ô ˆØ×(Ò(¨Ñ-Ô-Ð-Ø—Z’Z ‘_”_ s§|¢|¡~¤~Ñ5ˆ
Ø˜3œ7Ð"Ð"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¨#¡,¤,½Ñ8IÔ8IÐJÑJÔJˆØ×ÒÑÔˆØ	×&Ò& sÑ+Ô+Ð+Ø�xŠx˜
Ñ#Ô# a§j¢j¡l¤lÑ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5ðN)3ð )3ð )3ð )3ð )3�oñ )3ô )3ð )3ðZ Ð�s˜CÑ Ô ØÐ�s˜CÑ Ô ØÐ�s˜CÑ Ô Ø€O�CÑÔØ€O�CÑÔØ€O�CÑÔðÐ Ð Ð r#   