관리-도구
편집 파일: _mode_ocb.cpython-311.pyc
� O�Dg�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.4.0/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_ � �&