Edit File: _mode_ocb.cpython-311.pyc
� ������Fh�M�����������������������������d�Z�ddlZddlmZ�ddlmZmZmZ�ddlm Z m Z �ddlmZ�ddl mZ�ddlmZ�dd lmZmZmZmZmZmZmZmZ��ed d������������Z�G�d��d e������������Zd��ZdS�)ah�� Offset Codebook (OCB) mode. OCB is Authenticated Encryption with Associated Data (AEAD) cipher mode designed by Prof. Phillip Rogaway and specified in `RFC7253`_. The algorithm provides both authenticity and privacy, it is very efficient, it uses only one key and it can be used in online mode (so that encryption or decryption can start before the end of the message is available). This module implements the third and last variant of OCB (OCB3) and it only works in combination with a 128-bit block symmetric cipher, like AES. OCB is patented in US but `free licenses`_ exist for software implementations meant for non-military purposes. Example: >>> from Crypto.Cipher import AES >>> from Crypto.Random import get_random_bytes >>> >>> key = get_random_bytes(32) >>> cipher = AES.new(key, AES.MODE_OCB) >>> plaintext = b"Attack at dawn" >>> ciphertext, mac = cipher.encrypt_and_digest(plaintext) >>> # Deliver cipher.nonce, ciphertext and mac ... >>> cipher = AES.new(key, AES.MODE_OCB, nonce=nonce) >>> try: >>> plaintext = cipher.decrypt_and_verify(ciphertext, mac) >>> except ValueError: >>> print "Invalid message" >>> else: >>> print plaintext :undocumented: __package__ .. _RFC7253: http://www.rfc-editor.org/info/rfc7253 .. _free licenses: http://web.cs.ucdavis.edu/~rogaway/ocb/license.htm �����N)� unhexlify)�bord�_copy_bytes�bchr)� long_to_bytes� bytes_to_long)�strxor)�BLAKE2s)�get_random_bytes)�load_pycryptodome_raw_lib�VoidPointer�create_string_buffer�get_raw_buffer�SmartPointer�c_size_t�c_uint8_ptr� is_bufferzCrypto.Cipher._raw_ocba��� int OCB_start_operation(void *cipher, const uint8_t *offset_0, size_t offset_0_len, void **pState); int OCB_encrypt(void *state, const uint8_t *in, uint8_t *out, size_t data_len); int OCB_decrypt(void *state, const uint8_t *in, uint8_t *out, size_t data_len); int OCB_update(void *state, const uint8_t *in, size_t data_len); int OCB_digest(void *state, uint8_t *tag, size_t tag_len); int OCB_stop_operation(void *state); c��������������������j�����e�Zd�ZdZd��Zd��Zd��Zd��Zd��Zdd�Z dd �Z d ��Zd��Zd��Z d ��Zd��Zd��Zd��ZdS�)�OcbModez=Offset Codebook (OCB) mode. :undocumented: __init__ c����������� �������^����|j���������dk����rt����������d�������������d|�_��������� �t����������d�d�|������������|�_�������� �t ����������|������������t����������dd������������vrt����������d�������������t ����������|������������st����������d�������������||�_��������d|cxk����rdk����sn�t����������d�������������d�|�_ ��������d|�_ ��������d|�_��������g�d �|�_��������t����������|������������}|���������������������d ������������}|�j��������dz��dz��}t ����������|�j��������������������dk�����r4t����������|dz��������������d dt ����������|������������z ��z��z���dz���|�j��������z���}nt����������|dz��dz��������������|�j��������z���}t!����������|d���������������������dz��}t!����������|d���������������������dz��} �|j��������||j��������fi�|��} | ���������������������t)����������j��������d|d�d����������| ������������������������}|t-����������|d�d����������|dd����������������������z���}t/����������t1����������|������������d|z ��z ��d������������dd�����������} |���������������������|������������}|rt����������dt5����������|������������z����������������t7������������������������|�_��������t:�������������������������������|�����������������������������������| tA����������t ����������| ������������������������|�j���������!����������������������������������������������}|rt����������d|z���������������tE����������|�j�������������������������������������������t:����������j#��������������������|�_��������|�$�����������������������������������d�S�)N����zFOCB mode is only available for ciphers that operate on 128 bits blocks����z#Nonce must be at most 15 bytes longz,Nonce must be bytes, bytearray or memoryview����z+MAC tag must be between 8 and 16 bytes long�����)�update�encrypt�decrypt�digest�verify�key����������������������?��������15sB� ����@�������zUnknown keywords: z)Error %d while instantiating the OCB mode)%� block_size� ValueErrorr����nonce�len�ranger���� TypeError�_mac_len�_mac_tag�_cache_A�_cache_P�_next�dict�popr���r����new�MODE_ECBr����struct�packr ���r���r����_create_base_cipher�strr ����_state�_raw_ocb_lib�OCB_start_operation�getr���� address_ofr����OCB_stop_operation�release)�self�factoryr.����mac_len� cipher_params�params_without_keyr ���� taglen_mod128�bottom_bits�top_bits�ktop_cipher�ktop�stretch�offset_0� raw_cipher�results��� �y/builddir/build/BUILD/imunify360-venv-2.5.3/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Cipher/_mode_ocb.py�__init__zOcbMode.__init__s���sz��������#�#���@��A��A�� A�����@� ��t�U�3�3�� �*��u�:�:�U�1�b�\�\�)�)��B�C�C�C������ L��J�K�K�K��� ��G�!�!�!�!�r�!�!�!�!��J�K�K�K���� ���� ���� �*��*��*�� ��"�-�0�0�� �$�$�U�+�+�����*�c�1� ��t�z�?�?�R�����!�+�,�,��r�C��J�J��/�0�����J��E�E� ��-�1�,��4�5�5��J��E���5��9�o�o��,����b� �?�?�T�)��!�g�k�#�")�"2�8��8�$6�8��8����"�"�6�;�v�/4�S�b�S�z�/7�$9��$9��:��:������R�a�R��$�q��s�)�4�4�4�� ��w�!7�!7�"$�{�"2�"4�57�9��9�9:���=����0�0��?�?� ��� G��0�3�}�3E�3E�E�F�F�F�!�m�m����1�1�*�.�.�2B�2B�2:�2:�3�x�=�=�2I�2I�26�+�2H�2H�2J�2J�L��L����� '��H�%�&��'��'�� '� �#�4�;�?�?�#4�#4�#/�#B�D��D��� � �������r���c������������������������t��������������������������������|�j�������������������������������������������t ����������|������������t����������|������������������������}|rt ����������d|z���������������d�S�)Nz(Error %d while computing MAC in OCB mode)r@���� OCB_updater?���rB���r���r���r-���)rF���� assoc_data�assoc_data_lenrS���s��� rT����_updatezOcbMode._update����sh�������(�(�����):�):�)4�Z�)@�)@�)1�.�)A�)A�C��C����� R��G�&�P�Q�Q�Q� R�� Rr���c������������������,����d|�j���������vrt����������d�������������g�d�|�_���������t����������|�j��������������������dk����r�t ����������dt����������|�j��������������������z ��t����������|������������������������}|�xj��������t����������d||������������z ��c_��������||d����������}t����������|�j��������������������dk�����r|�S�d|�j��������c|�_��������}|����������������������|�������������t����������|������������dz��dz��}t����������|d|������������|�_��������|����������������������||�������������|�S�)a���Process the associated data. If there is any associated data, the caller has to invoke this method one or more times, before using ``decrypt`` or ``encrypt``. By *associated data* it is meant any data (e.g. packet headers) that will not be encrypted and will be transmitted in the clear. However, the receiver shall still able to detect modifications. If there is no associated data, this method must not be called. The caller may split associated data in segments of any size, and invoke this method multiple times, each time with the next segment. :Parameters: assoc_data : bytes/bytearray/memoryview A piece of associated data. r���z<update() can only be called immediately after initialization)r���r���r���r���r���r���r���Nr���)r6���r1���r/���r4����minr���r���rZ���)rF���rX����filler�seg� update_lens��� rT���r���zOcbMode.update����s#�����*��4�:�%�%���@��A��A�� A�*��*��*�� ���t�}����!�!���c�$�-�0�0�0�#�j�/�/�B�B�F��M�M�[��v�z�B�B�B�M�M�#�F�G�G�,�J��4�=�!�!�B�&�&����"%�d�m��D�M�3��K�K�������_�_��*�R�/� �#�J��j�A�A�� ����Z��,�,�,��r���c������������������������t����������|������������}�||�j�������������������������������������������||t����������|������������������������}|rt ����������d||fz���������������t����������|������������S�)Nz Error %d while %sing in OCB mode)r���r?���rB���r���r-���r���)rF����in_data�in_data_len� trans_func� trans_desc�out_datarS���s��� rT����_transcrypt_alignedzOcbMode._transcrypt_aligned����s{�������(��4�4����D�K�O�O�-�-�#�$�$�[�1�1�3��3����� 5��?� &� �3�4��5��5�� 5��h�'�'�'r���c����������������������|�8|�����������������������|�j��������t����������|�j��������������������||������������}d|�_��������|S�d}t����������|�j��������������������dk����r�t����������dt����������|�j��������������������z ��t����������|������������������������}|�xj��������t ����������d�||������������z ��c_��������||d�����������}t����������|�j��������������������dk�����rdS�|�����������������������|�j��������t����������|�j��������������������||������������}d|�_��������t����������|������������dz��dz��}|�����������������������t����������|������������|||������������}|r||z���}t ����������|d�|������������|�_��������|S�)Nr���r���r���)rf���r5���r/���r\���r���r���) rF���ra���rc���rd���re����prefixr]���� trans_lenrS���s ��� rT����_transcryptzOcbMode._transcrypt��sj������?��/�/�� �03�D�M�0B�0B�0:�0:�<��<�H�� �D�M��O�����t�}����!�!���c�$�-�0�0�0�#�g�,�,�?�?�F��M�M�[��v�w�?�?�?�M�M��f�g�g�&�G��4�=�!�!�B�&�&���s���-�-�d�m�.1�$�-�.@�.@�.8�.8�:��:�F�� �D�M����L�L�B�&��+� ��)�)�+�g�*>�*>�*3�*4�*4�6��6����� %��f�_�F��$�I�t�W�=�=�� �� r���Nc�����������������������d|�j���������vrt����������d�������������|� dg|�_���������ndg|�_���������|����������������������|t����������j��������d������������S�)a���Encrypt the next piece of plaintext. After the entire plaintext has been passed (but before `digest`), you **must** call this method one last time with no arguments to collect the final piece of ciphertext. If possible, use the method `encrypt_and_digest` instead. :Parameters: plaintext : bytes/bytearray/memoryview The next piece of data to encrypt or ``None`` to signify that encryption has finished and that any remaining ciphertext has to be produced. :Return: the ciphertext, as a byte string. Its length may not match the length of the *plaintext*. r���z@encrypt() can only be called after initialization or an update()Nr���)r6���r1���rj���r@����OCB_encrypt�rF���� plaintexts��� rT���r���zOcbMode.encrypt3��s_������&��D�J�&�&���=��>��>�� >����"��D�J�J�#��D�J���� �<�+C�Y�O�O�Or���c�����������������������d|�j���������vrt����������d�������������|� dg|�_���������ndg|�_���������|����������������������|t����������j��������d������������S�)a���Decrypt the next piece of ciphertext. After the entire ciphertext has been passed (but before `verify`), you **must** call this method one last time with no arguments to collect the remaining piece of plaintext. If possible, use the method `decrypt_and_verify` instead. :Parameters: ciphertext : bytes/bytearray/memoryview The next piece of data to decrypt or ``None`` to signify that decryption has finished and that any remaining plaintext has to be produced. :Return: the plaintext, as a byte string. Its length may not match the length of the *ciphertext*. r���z@decrypt() can only be called after initialization or an update()Nr���)r6���r1���rj���r@����OCB_decrypt)rF���� ciphertexts��� rT���r���zOcbMode.decryptP��sg������&��D�J�&�&���=��>��>�� >����"��D�J�J�#��D�J���� � ,� 8� )�+��+�� +r���c����������� �����������|�j����������d�S�|�j��������r4|����������������������|�j��������t����������|�j���������������������������������d|�_��������t ����������d������������}t �������������������������������|�j�������������������������������������������|t����������t����������|������������������������������������}|rt����������d|z���������������t����������|������������d�|�j������������������|�_���������d�S�)Nr���r���z+Error %d while computing digest in OCB mode) r3���r4���rZ���r/���r���r@���� OCB_digestr?���rB���r���r-���r���r2���)rF����mac_tagrS���s��� rT����_compute_mac_tagzOcbMode._compute_mac_tago��s��������=�$��F��=�� ��L�L����D�M�(:�(:�;�;�;��D�M�&�r�*�*���(�(�����):�):�)0�)1�#�g�,�,�)?�)?�+��+����� '��J�%�&��'��'�� '�&�w�/�/�����?�� � � r���c�����������������������d|�j���������vrt����������d�������������t����������|�j��������������������dk����sJ��dg|�_���������|�j���������|�������������������������������������|�j��������S�)a#��Compute the *binary* MAC tag. Call this method after the final `encrypt` (the one with no arguments) to obtain the MAC tag. The MAC tag is needed by the receiver to determine authenticity of the message. :Return: the MAC, as a byte string. r���z-digest() cannot be called now for this cipherr���)r6���r1���r/���r5���r3���ru����rF���s��� rT���r���zOcbMode.digest���sf��������4�:�%�%��K�L�L�L��4�=�!�!�Q�&�&�&�&��Z�� ��=� ��!�!�#�#�#��}�r���c������������������d�����d����������������������d��|������������������������������������D���������������������������S�)z�Compute the *printable* MAC tag. This method is like `digest`. :Return: the MAC, as a hexadecimal string. ��c������������������2�����g�|�]}d�t����������|������������z����S�)z%02x)r���)�.0�xs��� rT���� <listcomp>z%OcbMode.hexdigest.<locals>.<listcomp>���s#������@�@�@�Q���a���(�@�@�@r���)�joinr���rw���s��� rT���� hexdigestzOcbMode.hexdigest���s-��������w�w�@�@�$�+�+�-�-�@�@�@�A�A�Ar���c����������������������d|�j���������vrt����������d�������������t����������|�j��������������������dk����sJ��dg|�_���������|�j���������|�������������������������������������t ����������d������������}t����������j��������d||�j���������������������}t����������j��������d||�������������}|� ����������������������������������|� ����������������������������������k����rt����������d�������������dS�) a���Validate the *binary* MAC tag. Call this method after the final `decrypt` (the one with no arguments) to check if the message is authentic and valid. :Parameters: received_mac_tag : bytes/bytearray/memoryview This is the *binary* MAC, as received from the sender. :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. r���z-verify() cannot be called now for this cipherr���Nr�������)�digest_bitsr ����datazMAC check failed)r6���r1���r/���r5���r3���ru���r���r ���r9���r���r-���)rF����received_mac_tag�secret�mac1�mac2s��� rT���r���zOcbMode.verify���s���������4�:�%�%��K�L�L�L��4�=�!�!�Q�&�&�&�&��Z�� ��=� ��!�!�#�#�#�!�"�%�%���{�s��T�]�K�K�K���{�s��=M�N�N�N���;�;�=�=�D�K�K�M�M�)�)��/�0�0�0��*�)r���c������������������J�����|�����������������������t����������|�������������������������dS�)aW��Validate the *printable* MAC tag. This method is like `verify`. :Parameters: hex_mac_tag : string This is the *printable* MAC, as received from the sender. :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. N)r���r���)rF����hex_mac_tags��� rT���� hexverifyzOcbMode.hexverify���s$������� ���I�k�*�*�+�+�+�+�+r���c������������������~�����|�����������������������|������������|�������������������������������������z���|������������������������������������fS�)a&��Encrypt the message and create the MAC tag in one step. :Parameters: plaintext : bytes/bytearray/memoryview The entire message to encrypt. :Return: a tuple with two byte strings: - the encrypted data - the MAC )r���r���rm���s��� rT����encrypt_and_digestzOcbMode.encrypt_and_digest���s0��������|�|�I�&�&������7������F�Fr���c�����������������������|�����������������������|������������|�������������������������������������z���}|����������������������|�������������|S�)a���Decrypted the message and verify its authenticity in one step. :Parameters: ciphertext : bytes/bytearray/memoryview The entire message to decrypt. received_mac_tag : byte string This is the *binary* MAC, as received from the sender. :Return: the decrypted data (byte string). :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. )r���r���)rF���rq���r����rn���s��� rT����decrypt_and_verifyzOcbMode.decrypt_and_verify���s;��������L�L��,�,�t�|�|�~�~�=� ����$�%�%�%��r���)N)�__name__� __module__�__qualname__�__doc__rU���rZ���r���rf���rj���r���r���ru���r���r���r���r����r����r������r���rT���r���r���m���s���������������� R��R��R�hR��R��R�+��+��+�Z(��(��(�)��)��)�VP��P��P��P�:+��+��+��+�>@��@��@�&�����0B��B��B�1��1��1�> ,�� ,�� ,� G�� G�� G���������r���r���c���������������������� �|����������������������dd������������}|�t����������d������������}|����������������������dd������������}n1#�t����������$�r$}t����������dt ����������|������������z����������������d}~ww�xY�wt����������|�|||������������S�)a���Create a new block cipher, configured in OCB mode. :Parameters: factory : module A symmetric cipher module from `Crypto.Cipher` (like `Crypto.Cipher.AES`). :Keywords: nonce : bytes/bytearray/memoryview A value that must never be reused for any other encryption. Its length can vary from 1 to 15 bytes. If not specified, a random 15 bytes long nonce is generated. mac_len : integer Length of the MAC, in bytes. It must be in the range ``[8..16]``. The default is 16 (128 bits). Any other keyword will be passed to the underlying block cipher. See the relevant documentation for details (at least ``key`` will need to be present). r.���Nr"���rH���r���zKeyword missing: )r8���r����KeyErrorr1���r>���r���)rG����kwargsr.���rH����es��� rT����_create_ocb_cipherr�������s�������06�� � �7�D�)�)���=�$�R�(�(�E��*�*�Y��+�+������6��6��6��+�c�!�f�f�4�5�5�5�����6������7�E�7�F�3�3�3s����=A���� A.� A)�)A.)r����r;����binasciir����Crypto.Util.py3compatr���r���r����Crypto.Util.numberr���r����Crypto.Util.strxorr ����Crypto.Hashr ���� Crypto.Randomr����Crypto.Util._raw_apir���r ���r���r���r���r���r���r���r@����objectr���r����r����r���rT����<module>r�������sf����>&��&�P�� � � ��������������9��9��9��9��9��9��9��9��9��9��;��;��;��;��;��;��;��;��%��%��%��%��%��%��������������*��*��*��*��*��*�-��-��-��-��-��-��-��-��-��-��-��-��-��-��-��-��-��-��-��-� �)�(�)A��D(��)��)��.D��D��D��D��D�f��D��D��D�N 4�� 4�� 4�� 4�� 4r���