관리-도구
편집 파일: _mode_ctr.cpython-311.pyc
� O�Dg�= � � � d Z dgZddlZddlmZmZmZmZmZm Z m Z mZ ddlm Z ddlmZmZ ddlmZ edd � � Z G d � de� � Zd� ZdS )z Counter (CTR) mode. �CtrMode� N)�load_pycryptodome_raw_lib�VoidPointer�create_string_buffer�get_raw_buffer�SmartPointer�c_size_t�c_uint8_ptr�is_writeable_buffer)�get_random_bytes)�_copy_bytes� is_native_int)� long_to_byteszCrypto.Cipher._raw_ctra� int CTR_start_operation(void *cipher, uint8_t initialCounterBlock[], size_t initialCounterBlock_len, size_t prefix_len, unsigned counter_len, unsigned littleEndian, void **pResult); int CTR_encrypt(void *ctrState, const uint8_t *in, uint8_t *out, size_t data_len); int CTR_decrypt(void *ctrState, const uint8_t *in, uint8_t *out, size_t data_len); int CTR_stop_operation(void *ctrState);c �( � e Zd ZdZd� Zdd�Zdd�ZdS )r a* *CounTeR (CTR)* mode. This mode is very similar to ECB, in that encryption of one block is done independently of all other blocks. Unlike ECB, the block *position* contributes to the encryption and no information leaks about symbol frequency. Each message block is associated to a *counter* which must be unique across all messages that get encrypted with the same key (not just within the same message). The counter is as big as the block size. Counters can be generated in several ways. The most straightword one is to choose an *initial counter block* (which can be made public, similarly to the *IV* for the other modes) and increment its lowest **m** bits by one (modulo *2^m*) for each block. In most cases, **m** is chosen to be half the block size. See `NIST SP800-38A`_, Section 6.5 (for the mode) and Appendix B (for how to manage the *initial counter block*). .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf :undocumented: __init__ c �x � t |� � ||z k rt d||� � | _ t � � | _ t � |� � � t |� � t t |� � � � t |� � ||| j � � � � � }|rt d|z � � �t | j � � � t j � � | _ |� � � t |� � | _ ddg| _ dS )aB Create a new block cipher, configured in CTR mode. :Parameters: block_cipher : C pointer A smart pointer to the low-level block cipher instance. initial_counter_block : bytes/bytearray/memoryview The initial plaintext to use to generate the key stream. It is as large as the cipher block, and it embeds the initial value of the counter. This value must not be reused. It shall contain a nonce or a random component. Reusing the *initial counter block* for encryptions performed with the same key compromises confidentiality. prefix_len : integer The amount of bytes at the beginning of the counter block that never change. counter_len : integer The length in bytes of the counter embedded in the counter block. little_endian : boolean True if the counter in the counter block is an integer encoded in little endian mode. If False, it is big endian. Nz)Error %X while instantiating the CTR mode�encrypt�decrypt)�lenr �noncer �_state�raw_ctr_lib�CTR_start_operation�getr r � address_of� ValueErrorr �CTR_stop_operation�release� block_size�_next)�self�block_cipher�initial_counter_block� prefix_len�counter_len� little_endian�results �y/builddir/build/BUILD/imunify360-venv-2.4.0/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Cipher/_mode_ctr.py�__init__zCtrMode.__init__Y s. � �@ �$�%�%��k�)A�A�A�$�T�:�7L�M�M�D�J�A�!�m�m����0�0��1A�1A�1C�1C�1<�=R�1S�1S�19�#�>S�:T�:T�1U�1U�19�*�1E�1E�1<�1>�15��1G�1G�1I�1I� K� K�� � '��H�%�&� '� '� '� #�4�;�?�?�#4�#4�#.�#A�C� C��� �������3�4�4���@���+�� � � � Nc � � d| j vrt d� � �dg| _ |�t t |� � � � }n_|}t |� � st d� � �t |� � t |� � k rt dt |� � z � � �t � | j � � � t |� � t |� � t t |� � � � � � }|r'|dk rt d� � �t d|z � � �|�t |� � S dS ) a� Encrypt data with the key and the parameters set at initialization. A cipher object is stateful: once you have encrypted a message you cannot encrypt (or decrypt) another message using the same object. The data to encrypt can be broken up in two or more pieces and `encrypt` can be called multiple times. That is, the statement: >>> c.encrypt(a) + c.encrypt(b) is equivalent to: >>> c.encrypt(a+b) This function does not add any padding to the plaintext. :Parameters: plaintext : bytes/bytearray/memoryview The piece of data to encrypt. It can be of any length. :Keywords: output : bytearray/memoryview The location where the ciphertext must be written to. If ``None``, the ciphertext is returned. :Return: If ``output`` is ``None``, the ciphertext is returned as ``bytes``. Otherwise, ``None``. r z*encrypt() cannot be called after decrypt()N�4output must be a bytearray or a writeable memoryview�9output must have the same length as the input (%d bytes)� �*The counter has wrapped around in CTR modez%Error %X while encrypting in CTR mode)r � TypeErrorr r r r r �CTR_encryptr r r r � OverflowErrorr )r � plaintext�output� ciphertextr&