ff7_dZdgZddlZddlmZddlmZmZmZddl m Z ddl m Z ddl mZdd lmZdd lmZd Zedd d ZGddeZdZdS)z" Counter with CBC-MAC (CCM) mode. CcmModeN) unhexlify) byte_stringbord _copy_bytes)is_writeable_buffer)strxor) long_to_bytes)BLAKE2s)get_random_bytesc $tdd|S)NEnum)type)enumss y/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Cipher/_mode_ccm.pyenumr3s E " "") NOT_STARTEDPROCESSING_AUTH_DATAPROCESSING_PLAINTEXTcpeZdZdZdZdZdZdZddZdd Z dd Z d Z d Z d Z dZdZddZddZdS)ra Counter with CBC-MAC (CCM). 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. This mode requires a nonce. The nonce shall never repeat for two different messages encrypted with the same key, but it does not need to be random. Note that there is a trade-off between the size of the nonce and the maximum size of a single message you can encrypt. It is important to use a large nonce if the key is reused across several messages and the nonce is chosen randomly. It is acceptable to us a short nonce if the key is only used a few times or if the nonce is taken from a counter. The following table shows the trade-off when the nonce is chosen at random. The column on the left shows how many messages it takes for the keystream to repeat **on average**. In practice, you will want to stop using the key way before that. +--------------------+---------------+-------------------+ | Avg. # of messages | nonce | Max. message | | before keystream | size | size | | repeats | (bytes) | (bytes) | +====================+===============+===================+ | 2^52 | 13 | 64K | +--------------------+---------------+-------------------+ | 2^48 | 12 | 16M | +--------------------+---------------+-------------------+ | 2^44 | 11 | 4G | +--------------------+---------------+-------------------+ | 2^40 | 10 | 1T | +--------------------+---------------+-------------------+ | 2^36 | 9 | 64P | +--------------------+---------------+-------------------+ | 2^32 | 8 | 16E | +--------------------+---------------+-------------------+ This mode is only available for ciphers that operate on 128 bits blocks (e.g. AES but not TDES). See `NIST SP800-38C`_ or RFC3610_. .. _`NIST SP800-38C`: http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C.pdf .. _RFC3610: https://tools.ietf.org/html/rfc3610 .. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html :undocumented: __init__ c T|j|_ tdd||_ ||_tdd||_||_||_||_||_d|_ |jdkrtd|dvrtd|z|rdt|cxkrdksntd|jj ||j fdd i||_tj|_d|_gd |_d |_d |_g|_d t|z }|jj ||jjfd t1jd|dz |jzi||_|jd |_d||fvr|dSdS)NzFCCM mode is only available for ciphers that operate on 128 bits blocks) rz@Parameter 'mac_len' must be even and in the range 4..16 (not %d) z)rI assoc_data_ptfiller update_lens rrXzCcmMode._updates  y4 4 4"=11 G +D$ F F K  } - - - F4;$/1111 t{  a  3t{+;+;;]++--F KK;tV]CC CKK'mDDM4;$/11i'' 44DGDK''4?:T_L !*dMBB >>i'' kzk(BCCCDDIDGGG >rNcd|jvrtdddg|_|j[t|jt sJt d|jD|_|j|n|j |jkrtd|j0t||_|dg|_|xj t|z c_ |j |jkrtd|j tjkr%|tj|_ |||j||S) abEncrypt data with the key 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. This method can be called only **once** if ``msg_len`` was not passed at initialization. If ``msg_len`` was given, 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 as ``bytes``. Otherwise, ``None``. r(z@encrypt() can only be called after initialization or an update()r*Nc,g|]}t|Srr8.0xs r z#CcmMode.encrypt..["?"?"?a3q66"?"?"?rAssociated data is too shortMessage is too longoutput)r?rcr4rTrBrUsumr3rHr@r7r8rAr=r<rrarrXrFr(rI plaintextrvs rr(zCcmMode.encrypt.suJ DJ & &=>> >*  ? "dk400 1 10!"?"?4;"?"?"?@@DO}(!!!$t66 !?@@@ =  NNDM OO   "DJ s9~~-   . .233 3  y= = =  & & ( ( ((=D  Y|##If#===rc6d|jvrtdddg|_|j[t|jt sJt d|jD|_|j|n|j |jkrtd|j0t||_|dg|_|xj t|z c_ |j |jkrtd|j tjkr%|tj|_ |j||}|||n|||S) adDecrypt data with the key 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. This method can be called only **once** if ``msg_len`` was not passed at initialization. If ``msg_len`` was given, 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 as ``bytes``. Otherwise, ``None``. r)z@decrypt() can only be called after initialization or an update()r+Nc,g|]}t|Srrmrns rrqz#CcmMode.decrypt..rrrrsrtru)r?rcr4rTrBrUrwr3rHr@r7r8rAr=r<rrarrFr(rX)rI ciphertextrvrys rr)zCcmMode.decryptwsJ DJ & &CDD D*  ? "dk400 1 10!"?"?4;"?"?"?@@DO}(!!!$t66 !?@@@ =  OODM OO   "DJ s:.   . .233 3  y= = =  & & ( ( ((=D L((F(CC > LL # # # # LL rcjd|jvrtddg|_|S)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. r*zAdigest() cannot be called when decrypting or validating a message)r?rc_digestrIs rr*zCcmMode.digests? 4: % %788 8Z ||~~rc0|jr|jS|j[t|jtsJt d|jD|_|j|n|j|jkrtd|jd|_||j |jkrtd| t|j |jd|j|_|jS)Nc,g|]}t|Srrmrns rrqz#CcmMode._digest..rrrrsrzMessage is too short)r6r4rTrBrUrwr3rHr@r7rArar r>rGr2rs rr~zCcmMode._digests = != ? "dk400 1 10!"?"?4;"?"?"?@@DO}(!!!$t66 !?@@@ = DM OO     $- / /344 4 ""$$$tw 22>DM>B }rcddd|DS)zCompute the *printable* MAC tag. This method is like `digest`. :Return: the MAC, as a hexadecimal string. c2g|]}dt|zS)z%02x)rrns rrqz%CcmMode.hexdigest..s#@@@Qa(@@@r)rWr*rs r hexdigestzCcmMode.hexdigests-ww@@$++--@@@AAArcdd|jvrtddg|_|td}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. r+z3verify() cannot be called when encrypting a messager) digest_bitsrKdatazMAC check failedN) r?rcr~r r r9r6r*r7)rIreceived_mac_tagsecretmac1mac2s rr+zCcmMode.verifys" 4: % %9:: :Z  !"%%{sT]KKK{s=MNNN ;;==DKKMM ) )/00 0 * )rcJ|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)r+r)rI hex_mac_tags r hexverifyzCcmMode.hexverifys$ Ik**+++++rcX||||fS)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. ru)r(r*rxs rencrypt_and_digestzCcmMode.encrypt_and_digest+s'(||If|55t{{}}DDrc^|||}|||S)aPerform decrypt() and verify() in one step. :Parameters: ciphertext : bytes/bytearray/memoryview The piece of data to decrypt. received_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. ru)r)r+)rIr|rrvrys rdecrypt_and_verifyzCcmMode.decrypt_and_verifyAs2&LLFL;;  $%%%r)r)N)__name__ __module__ __qualname____doc__rQrHrar'rXr(r)r*r~rr+rrrrrrrr9s)88tBBBH"("("(H B B B$$$LJJJJ@G>G>G>G>RLLLL\"<BBB111> , , ,EEEE,rc  |dx}}n1#t$r$}tdt|zd}~wwxYw|dd}|t d}|d|j}|dd}|dd}t |}t|||||||S) aNCreate a new block cipher, configured in CCM mode. :Parameters: factory : module 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. nonce : bytes/bytearray/memoryview A value that must never be reused for any other encryption. Its length must be in the range ``[7..13]``. 11 or 12 bytes are reasonable values in general. Bear in mind that with CCM there is a trade-off between nonce length and maximum message size. If not specified, a 11 byte long random string is used. mac_len : integer Length of the MAC, in bytes. It must be even and in the range ``[4..16]``. The default is 16. msg_len : integer Length of the message to (de)cipher. If not specified, ``encrypt`` or ``decrypt`` may only be called once. assoc_len : integer Length of the associated data. If not specified, all data is internally buffered. rKzMissing parameter: Nr- rLrMrN)popKeyErrorrcstrr r/dictr) rJkwargsrKer-rLrMrNrOs r_create_ccm_cipherrYsF8JJu%%%cc 888-A67778 JJw % %E } $$jjG$677GjjD))G ;--ILLM 7Cm - --s AAA)r__all__rDbinasciirCrypto.Util.py3compatrrrCrypto.Util._raw_apirCrypto.Util.strxorr Crypto.Util.numberr Crypto.Hashr Crypto.Randomr rr<objectrrrrrrs2> + 0000000000444444%%%%%%,,,,,,******### DQQQ O O O ]]]]]f]]]@1-1-1-1-1-r