ffu8dZdgZddlZddlmZddlmZmZmZddl m Z ddl m Z ddl mZmZdd lmZmZdd lmZGd deZd ZdS) z EAX mode. EaxModeN) unhexlify) byte_stringbord _copy_bytes) is_buffer)strxor) long_to_bytes bytes_to_long)CMACBLAKE2s)get_random_bytescVeZdZdZdZdZd dZd dZdZdZ d Z d Z d d Z d d Z dS)ra*EAX* mode. 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*. This mode is only available for ciphers that operate on 64 or 128 bits blocks. There are no official standards defining EAX. The implementation is based on `a proposal`__ that was presented to NIST. .. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html .. __: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/eax/eax-spec.pdf :undocumented: __init__ cj_ tdd|_ |_d_gd_djcxkr jksnt djztjdkrt dt|stdfdtdd D_ j d jj d _ tj d}jjf|d d _dS) zEAX cipher modeNupdateencryptdecryptdigestverifyz3'mac_len' must be at least 2 and not larger than %drz!Nonce cannot be empty in EAX modez,nonce must be bytes, bytearray or memoryviewc g|];}tjdjdz ztjd|zz$EaxMode.__init__..ksd   DOa$786;sA;N;NN#*'4666rr)) initial_valuenonce)rrr,_mac_len_mac_tag_next ValueErrorlenr TypeErrorrange_omacr_signerr rrMODE_CTR_cipher)r&r$r%r,mac_lenr counter_ints``` ` r'__init__zEaxMode.__init__Ps",@ tU33 =  *** T]5555do5555R#/00 0 tz??a  @AA A LJKK K q!   1 TZ(((z!} $DJqM$8$8$:$:;; "w{3#*#341<),44&3 44 r)c|d|jvrtdgd|_|j||S)aProtect associated data If there is any associated data, the caller has to invoke this function 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 is still able to detect any modification to it. 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. There are no restrictions on its size. rz>> 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``. rz@encrypt() can only be called after initialization or an update()routputNr)r/r2r7rr4r)r& plaintextr?cts r'rzEaxMode.encryptsB DJ & &=>> >* \ ! !)F ! ; ; > JqM  $ $ $ $ JqM  ( ( ( r)cd|jvrtdddg|_|jd||j||S)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 as ``bytes``. Otherwise, ``None``. rz@decrypt() can only be called after initialization or an update()rrr>)r/r2r4rr7r)r& ciphertextr?s r'rzEaxMode.decryptsnB DJ & &CDD D*  1 Z(((|##Jv#>>>r)cd|jvrtddg|_|js]d|jz}t dD]/}t ||j|}0|d|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 messagerr*N) r/r2r.rr3r r4rr-)r&tagr#s r'rzEaxMode.digests 4: % %;<< <Z } 0DO+C1XX : :S$*Q-"6"6"8"899/DM}r)cddd|DS)zCompute the *printable* MAC tag. This method is like `digest`. :Return: the MAC, as a hexadecimal string. c2g|]}dt|zS)z%02x)r)r"xs r'r(z%EaxMode.hexdigest..s#@@@Qa(@@@r))joinr)r&s r' hexdigestzEaxMode.hexdigest s-ww@@$++--@@@AAAr)cd|jvrtddg|_|js]d|jz}t dD]/}t ||j|}0|d|j|_td}tj d||j}tj d||}||krtd dS) 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 MacMismatchError: if the MAC does not match. The message has been tampered with or the key is incorrect. rz3verify() cannot be called when encrypting a messagerr*N) digest_bitsr%datazMAC check failed) r/r2r.rr3r r4rr-rr rr0)r&received_mac_tagrEr#secretmac1mac2s r'rzEaxMode.verifys" 4: % %=>> >Z } 0DO+C1XX : :S$*Q-"6"6"8"899/DM!"%%{sT]KKK{s=MNNN ;;==DKKMM ) )/00 0 * )r)cJ|t|dS)a]Validate 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 MacMismatchError: if the MAC does not match. The message has been tampered with or the key is incorrect. N)rr)r& hex_mac_tags r' hexverifyzEaxMode.hexverify7s$ Ik**+++++r)cX||||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. r>)rr)r&r@r?s r'encrypt_and_digestzEaxMode.encrypt_and_digestFs'(||If|55t{{}}DDr)c^|||}|||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 MacMismatchError: if the MAC does not match. The message has been tampered with or the key is incorrect. r>)rr)r&rCrQr?pts r'decrypt_and_verifyzEaxMode.decrypt_and_verify\s1&\\*V\ 4 4 $%%% r))N)__name__ __module__ __qualname____doc__r:rrrrrKrrWrYr\r)r'rr3s8-4-4-4^>****X&?&?&?&?P0BBB"1"1"1H , , ,EEEE,r)c < |d}|dd}|td}|d|j}n1#t$r$}t dt |zd}~wwxYwt |||||S)aYCreate a new block cipher, configured in EAX 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. There are no restrictions on its length, but it is recommended to use at least 16 bytes. The nonce shall never repeat for two different messages encrypted with the same key, but it does not need to be random. If not specified, a 16 byte long random string is used. mac_len : integer Length of the MAC, in bytes. It must be no larger than the cipher block bytes (which is the default). r%r,NrMr8zMissing parameter: )poprrKeyErrorr2strr)r$kwargsr%r,r8es r'_create_eax_cipherrhts68jj 7D)) =$R((E**Y(:;; 888-A67778 7C 8 88sAA B$BB)r`__all__r binasciirCrypto.Util.py3compatrrrCrypto.Util._raw_apirCrypto.Util.strxorr Crypto.Util.numberr r Crypto.Hashr r Crypto.Randomrobjectrrhrar)r'rrs> + @@@@@@@@@@******%%%%%%;;;;;;;;%%%%%%%%******~~~~~f~~~B $9$9$9$9$9r)