ff6dZdgZddlmZmZddlmZmZddlm Z ddl m Z m Z ddl mZddlmZdd lmZGd deZd Zd S) z- Synthetic Initialization Vector (SIV) mode. SivMode)hexlify unhexlify)bord _copy_bytes) is_buffer) long_to_bytes bytes_to_long)_S2V)BLAKE2s)get_random_bytescXeZdZdZdZdZdZdZdZdZ dZ d Z d Z dd Z dd Zd S)raSynthetic Initialization Vector (SIV). This is an Authenticated Encryption with Associated Data (`AEAD`_) mode. It provides both confidentiality and authenticity. The header of the message may be left in the clear, if needed, and it will still be subject to authentication. The decryption step tells the receiver if the message comes from a source that really knowns the secret key. Additionally, decryption detects if any part of the message - including the header - has been modified or corrupted. Unlike other AEAD modes such as CCM, EAX or GCM, accidental reuse of a nonce is not catastrophic for the confidentiality of the message. The only effect is that an attacker can tell when the same plaintext (and same associated data) is protected with the same key. The length of the MAC is fixed to the block size of the underlying cipher. The key size is twice the length of the key of the underlying cipher. This mode is only available for AES ciphers. +--------------------+---------------+-------------------+ | Cipher | SIV MAC size | SIV key length | | | (bytes) | (bytes) | +====================+===============+===================+ | AES-128 | 16 | 32 | +--------------------+---------------+-------------------+ | AES-192 | 16 | 48 | +--------------------+---------------+-------------------+ | AES-256 | 16 | 64 | +--------------------+---------------+-------------------+ See `RFC5297`_ and the `original paper`__. .. _RFC5297: https://tools.ietf.org/html/rfc5297 .. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html .. __: http://www.cs.ucdavis.edu/~rogaway/papers/keywrap.pdf :undocumented: __init__ c0|j|_ ||_||_t|dvrt dt|z|Wt |st dt|dkrt dtdd||_ t|dz}d|_ t|d|||j|_ ||d|_ |j |d||jfi|gd|_dS) N) 0@zIncorrect key length (%d bytes)z?When provided, the nonce must be bytes, bytearray or memoryviewrz*When provided, the nonce must be non-empty) ciphermod cipher_paramsupdateencryptdecryptdigestverify) block_size_factory_cipher_paramslen ValueErrorr TypeErrorrnonce_mac_tagr _kdf_subkey_ciphernewMODE_ECB_next)selffactorykeyr"kwargs subkey_sizes y/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Cipher/_mode_siv.py__init__zSivMode.__init__[s;!,@ $ s88< ' '>SIJJ J  U## c abbb5zzQ !MNNN$T477DJ #hh!m  \k\*#*'+':<<< "+,,/  C  %w'7BB6BBB*** ct|}|dz}|jj|j|jjf|dd|jS)z*Create a new CTR cipher from V in SIV model r0) initial_valuer")r rr&r%MODE_CTRr)r)vv_intqs r._create_ctr_cipherzSivMode._create_ctr_ciphersba   6 6 t} 'M*+#$ ++ ) ++ +r0cxd|jvrtdgd|_|j|S)aProtect one associated data component For SIV, the associated data is a sequence (*vector*) of non-empty byte strings (*components*). This method consumes the next component. It must be called once for each of the components that constitue the associated data. Note that the components have clear boundaries, so that: >>> cipher.update(b"builtin") >>> cipher.update(b"securely") is not equivalent to: >>> cipher.update(b"built") >>> cipher.update(b"insecurely") If there is no associated data, this method must not be called. :Parameters: component : bytes/bytearray/memoryview The next associated data component. rz> >r0c td)z For SIV, decryption and verification must take place at the same point. This method shall not be used. Use `decrypt_and_verify` instead. zEdecrypt() not allowed for SIV mode. Use decrypt_and_verify() instead.r;)r) ciphertexts r.rzSivMode.decryptr=r0cd|jvrtddg|_|j|j|_|jS)zCompute the *binary* MAC tag. The caller invokes this function at the very end. This method returns the MAC that shall be sent to the receiver, together with the ciphertext. :Return: the MAC, as a byte string. rzAdigest() cannot be called when decrypting or validating a message)r(r!r#r$deriver)s r.rzSivMode.digestsV 4: % %788 8Z =  I,,..DM}r0cddd|DS)zCompute the *printable* MAC tag. This method is like `digest`. :Return: the MAC, as a hexadecimal string. c2g|]}dt|zS)z%02x)r).0xs r. z%SivMode.hexdigest..s#@@@Qa(@@@r0)joinrrBs r. hexdigestzSivMode.hexdigests-ww@@$++--@@@AAAr0cd|jvrtddg|_|j|j|_t d}t jd||j}t jd||}||krtddS)a(Validate the *binary* MAC tag. The caller invokes this function at the very end. This method checks if the decrypted message is indeed valid (that is, if the key is correct) and it has not been tampered with while in transit. :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. rz3verify() cannot be called when encrypting a messageN) digest_bitsr+datazMAC check failed) r(r!r#r$rAr r r&rr )r)received_mac_tagsecretmac1mac2s r.rzSivMode.verifys" 4: % %9:: :Z =  I,,..DM!"%%{sT]KKK{s=MNNN ;;==DKKMM ) )/00 0 * )r0cJ|t|dS)aWValidate 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)rr)r) hex_mac_tags r. hexverifyzSivMode.hexverifys$ Ik**+++++r0Ncd|jvrtddg|_t|dr|j|j|j||j|_||j}| |||jfS)a\Perform encrypt() and digest() in one step. :Parameters: plaintext : bytes/bytearray/memoryview The piece of data to encrypt. :Keywords: output : bytearray/memoryview The location where the ciphertext must be written to. If ``None``, the ciphertext is returned. :Return: a tuple with two items: - the ciphertext, as ``bytes`` - the MAC tag, as ``bytes`` The first item becomes ``None`` when the ``output`` parameter specified a location for the result. rz@encrypt() can only be called after initialization or an update()rr"output) r(r!hasattrr$rr"rAr#r7r)r)r<rYciphers r.encrypt_and_digestzSivMode.encrypt_and_digests( DJ & &=>> >Z  4 ! ! ) I  TZ ( ( ( ### ((** ((77~~i~77FFr0cvd|jvrtddg|_|||_|j||}t |dr|j|j|j||n|| ||S)aPPerform decryption and verification in one step. A cipher object is stateful: once you have decrypted a message you cannot decrypt (or encrypt) another message with the same object. You cannot reuse an object for encrypting or decrypting other data with the same key. 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. mac_tag : bytes/bytearray/memoryview This is the *binary* MAC, as received from the sender. :Keywords: output : bytearray/memoryview The location where the plaintext must be written to. If ``None``, the plaintext is returned. :Return: the plaintext as ``bytes`` or ``None`` when the ``output`` parameter specified a location for the result. :Raises ValueError: if the MAC does not match. The message has been tampered with or the key is incorrect. rz@decrypt() can only be called after initialization or an update()rrXr") r(r!r7_cipherrrZr$rr"r)r)r?mac_tagrYr<s r.decrypt_and_verifyzSivMode.decrypt_and_verify6s: DJ & &CDD DZ ..w77 L((F(CC 4 ! ! ) I  TZ ( ( ( fn&AAA Gr0)N)__name__ __module__ __qualname____doc__r/r7rrrrrJrrVr\r`r0r.rr1s''R$*$*$*L + + +!+!+!+F > > > > > >&BBB111B , , ,"G"G"G"GH,,,,,,r0c |d}n1#t$r$}tdt|zd}~wwxYw|dd}t ||||S)a-Create a new block cipher, configured in Synthetic Initializaton Vector (SIV) mode. :Parameters: factory : object A symmetric cipher module from `Crypto.Cipher` (like `Crypto.Cipher.AES`). :Keywords: key : bytes/bytearray/memoryview The secret key to use in the symmetric cipher. It must be 32, 48 or 64 bytes long. If AES is the chosen cipher, the variants *AES-128*, *AES-192* and or *AES-256* will be used internally. nonce : bytes/bytearray/memoryview For deterministic encryption, it is not present. Otherwise, it is a value that must never be reused for encrypting message under this key. There are no restrictions on its length, but it is recommended to use at least 16 bytes. r+zMissing parameter: Nr")popKeyErrorr!strr)r*r,r+er"s r._create_siv_cipherrkes|88jj 888-A67778 JJw % %E 7C / //s AAAN)rd__all__binasciirrCrypto.Util.py3compatrrCrypto.Util._raw_apirCrypto.Util.numberr r Crypto.Protocol.KDFr Crypto.Hashr Crypto.Randomr objectrrkrer0r.rus> +''''''''33333333******;;;;;;;;$$$$$$******qqqqqfqqqh #0#0#0#0#0r0