ff=dZdgZddlZddlmZmZmZmZmZm Z m Z m Z ddl m Z ddlmZmZddlmZedd ZGd deZd ZdS) z Counter (CTR) mode. CtrModeN)load_pycryptodome_raw_lib VoidPointercreate_string_bufferget_raw_buffer SmartPointerc_size_t c_uint8_ptris_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(eZdZdZdZddZddZdS)ra**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 xt|||zkrtd|||_ t|_t |t|tt|t||||j }|rtd|zt|jt j |_|t||_ ddg|_dS)aBCreate 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 modeencryptdecrypt)lenr noncer_state raw_ctr_libCTR_start_operationgetr r address_of ValueErrorrCTR_stop_operationrelease block_size_next)self block_cipherinitial_counter_block prefix_len counter_len little_endianresults y/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Cipher/_mode_ctr.py__init__zCtrMode.__init__Ys.@ $ % %k)A A A$T:7LMMDJ A!mm 001A1A1C1C1<=R1S1S19#>S:T:T1U1U19*1E1E1<1>151G1G1I1I KK  'H%&'' ' #4;??#4#4#.#ACC 344@+ Nc d|jvrtddg|_|tt|}n_|}t |stdt|t|krt dt|zt |j t|t|tt|}|r'|dkrtdt d|z|t|SdS) aEncrypt 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``. rz*encrypt() cannot be called after decrypt()N4output must be a bytearray or a writeable memoryview9output 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 TypeErrorrrr rr CTR_encryptrrr r OverflowErrorr)r plaintextoutput ciphertextr&s r'rzCtrMode.encryptsPB DJ & &HII I[ >-c)nn==JJJ&v.. X VWWW9~~V,, "025i.."ABBB(():):)4Y)?)?)4Z)@)@)1#i..)A)ACC  O  #%0111DvMNN N >!*-- -4r)c d|jvrtddg|_|tt|}n_|}t |stdt|t|krt dt|zt |j t|t|tt|}|r'|dkrtdt d|z|t|SdS) aDecrypt data with the key and the parameters set at initialization. A cipher object is stateful: once you have decrypted a message you cannot decrypt (or encrypt) another message with the same object. The data to decrypt can be broken up in two or more pieces and `decrypt` can be called multiple times. That is, the statement: >>> c.decrypt(a) + c.decrypt(b) is equivalent to: >>> c.decrypt(a+b) This function does not remove any padding from the plaintext. :Parameters: ciphertext : bytes/bytearray/memoryview The piece of data to decrypt. It can be of any length. :Keywords: output : bytearray/memoryview The location where the plaintext must be written to. If ``None``, the plaintext is returned. :Return: If ``output`` is ``None``, the plaintext is returned as ``bytes``. Otherwise, ``None``. rz*decrypt() cannot be called after encrypt()Nr+r,r-r.z%Error %X while decrypting in CTR mode)rr/rrr rr CTR_decryptrrr r r1r)r r4r3r2r&s r'rzCtrMode.decryptsPB DJ & &HII I[ >,S__==III&v.. X VWWW:#f++-- "025i.."ABBB(():):)4Z)@)@)4Y)?)?)1#j//)B)BDD  O  #%0111DvMNN N >!),, ,4r))N)__name__ __module__ __qualname____doc__r(rrr)r'rr<sW8<,<,<,|>>>>@>>>>>>r)c `||}|dd}|dd}|dd}|rtdt|z|||fdkrtd||2|jdkrtd t |jd z}n't ||jkrtd |jt |z }|d }t|r2d |dzzd z |krtd|t||z}n9t ||kr!tdt ||fz||z}t||t ||dSt|} |d}|d} |d} |d}|d} n#t$rtdwxYwg} |d kr6| tjd|dz|dz}|d k6| dgt!d |t | z zz } | s| | d| z| z}t ||jkr&tdt ||jfzt||t | || S)a4Instantiate a cipher object that performs CTR encryption/decryption. :Parameters: factory : module The underlying block cipher, a module from ``Crypto.Cipher``. :Keywords: nonce : bytes/bytearray/memoryview The fixed part at the beginning of the counter block - the rest is the counter number that gets increased when processing the next block. The nonce must be such that no two messages are encrypted under the same key and the same nonce. The nonce must be shorter than the block size (it can have zero length; the counter is then as long as the block). If this parameter is not present, a random nonce will be created with length equal to half the block size. No random nonce shorter than 64 bits will be created though - you must really think through all security consequences of using such a short block size. initial_value : posive integer or bytes/bytearray/memoryview The initial value for the counter. If not present, the cipher will start counting from 0. The value is incremented by one for each block. The counter number is encoded in big endian mode. counter : object Instance of ``Crypto.Util.Counter``, which allows full customization of the counter block. This parameter is incompatible to both ``nonce`` and ``initial_value``. 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). counterNr initial_valuez#Invalid parameters for CTR mode: %s)NNz<'counter' and 'nonce'/'initial_value' are mutually exclusivez7Impossible to create a safe nonce for short block sizeszNonce is too longrz"Initial counter value is too largez@Incorrect length for counter byte string (%d bytes, expected %d)Fr$prefixsuffixr%z6Incorrect counter object (use Crypto.Util.Counter.new)Br)z?Size of the counter block (%d bytes) must match block size (%d))_create_base_cipherpopr/strrr rrrrrdictKeyErrorappendstructpackmaxreversejoin) factorykwargs cipher_stater=rr>r$r"_counterrCrDr%wordss r'_create_ctr_cipherrXsJ..v66LjjD))G JJw % %EJJ55M M=F KLLL}5EE233 3 =!B&&!/000$W%71%<==EE5zzW/// !4555(3u::5  M  ' ' :kAo&!+m;; !EFFF$)M-,U,U$U ! !=!![00 !c"%m"4"4k!B"CDDD$)M$9 !|,5zz"  G}}H:ll=11 h''h'' _55  _55 :::9:: :: E !   V[mc&9::;;;!  !   gYQ c%jj 899 99E  "SXXe__4v= !!W%777,/23H/I/I/6/A/CCDD D r_s/. + 77777777777777777777 +*****<<<<<<<<,,,,,,''(@C?** (YYYYYfYYYxq<q<q<q<q