U +¥Mb=ã@slddlmZddlmZmZmZddlmZddlm Z m Z ddl m Z m Z m Z Gdd„deƒZd d „Zd S) é)Ú unhexlify)ÚbordÚtobytesÚis_bytes)Úget_random_bytesé)Ú cSHAKE128ÚSHA3_256)Ú_bytepadÚ _encode_strÚ _right_encodec@sHeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)Ú KMAC_Hashz[A KMAC hash object. Do not instantiate directly. Use the :func:`new` function. c CsLd||_||_d|_ttt|ƒƒ|ƒ}| ||d¡|_|rH|j |¡dS)Nz2.16.840.1.101.3.4.2.sKMAC) ÚoidÚ digest_sizeÚ_macr r rÚ_newÚ_cshakeÚupdate) ÚselfÚdataÚkeyÚmac_lenÚcustomZ oid_variantZcshakeZrateZ partial_newX©rú:/tmp/pip-target-t616c12r/lib/python/Crypto/Hash/KMAC128.pyÚ__init__.s zKMAC_Hash.__init__cCs|jrtdƒ‚|j |¡|S)z©Authenticate the next chunk of message. Args: data (bytes/bytearray/memoryview): The next chunk of the message to authenticate. z8You can only call 'digest' or 'hexdigest' on this object)rÚ TypeErrorrr)rrrrrr=s zKMAC_Hash.updatecCs2|js,|j t|jdƒ¡|j |j¡|_|jS)zŽReturn the **binary** (non-printable) MAC tag of the message. :return: The MAC tag. Binary form. :rtype: byte string é)rrrr rÚread©rrrrÚdigestKszKMAC_Hash.digestcCsd dd„t| ¡ƒDƒ¡S)z„Return the **printable** MAC tag of the message. :return: The MAC tag. Hexadecimal encoded. :rtype: string ÚcSsg|]}dt|ƒ‘qS)z%02x)r)Ú.0ÚxrrrÚ _sz'KMAC_Hash.hexdigest..)ÚjoinÚtupler rrrrÚ hexdigestXszKMAC_Hash.hexdigestcCsDtdƒ}t ||¡}t || ¡¡}| ¡| ¡kr@tdƒ‚dS)a`Verify that a given **binary** MAC (computed by another party) is valid. Args: mac_tag (bytes/bytearray/memoryview): the expected MAC of the message. Raises: ValueError: if the MAC does not match. It means that the message has been tampered with or that the MAC key is incorrect. ézMAC check failedN)rr Únewr Ú ValueError)rZmac_tagÚsecretZmac1Zmac2rrrÚverifyas zKMAC_Hash.verifycCs| tt|ƒƒ¡dS)anVerify that a given **printable** MAC (computed by another party) is valid. Args: hex_mac_tag (string): the expected MAC of the message, as a hexadecimal string. Raises: ValueError: if the MAC does not match. It means that the message has been tampered with or that the MAC key is incorrect. N)r,rr)rZ hex_mac_tagrrrÚ hexverifyus zKMAC_Hash.hexverifycKsd|kr|j|d<tf|ŽS)zNReturn a new instance of a KMAC hash object. See :func:`new`. r)rr))rÚkwargsrrrr)ƒs z KMAC_Hash.newN) Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr r'r,r-r)rrrrr (s  r cKsŒ| dd¡}t|ƒstdƒ‚t|ƒdkr0tdƒ‚| dd¡}| dd¡}|d krXtd ƒ‚| d d ¡}|rxtd t|ƒƒ‚t||||dtdƒS)aµCreate a new KMAC128 object. Args: key (bytes/bytearray/memoryview): The key to use to compute the MAC. It must be at least 128 bits long (16 bytes). data (bytes/bytearray/memoryview): Optional. The very first chunk of the message to authenticate. It is equivalent to an early call to :meth:`KMAC_Hash.update`. mac_len (integer): Optional. The size of the authentication tag, in bytes. Default is 64. Minimum is 8. custom (bytes/bytearray/memoryview): Optional. A customization byte string (``S`` in SP 800-185). Returns: A :class:`KMAC_Hash` hash object rNzYou must pass a key to KMAC128r(z1The key must be at least 128 bits long (16 bytes)rré@rz!'mac_len' must be 8 bytes or morerózUnknown parameters: Z19é¨)ÚpoprrÚlenr*Ústrr r)r.rrrrrrrr)Žs     r)N)ÚbinasciirZCrypto.Util.py3compatrrrZ Crypto.Randomrr!rr r r r Úobjectr r)rrrrÚs  f