| 1 |
from Crypto.Util._raw_api import (VoidPointer, SmartPointer, |
| 2 |
create_string_buffer, |
| 3 |
get_raw_buffer, c_size_t, |
| 4 |
c_uint8_ptr, c_ubyte) |
| 5 |
|
| 6 |
from Crypto.Util.number import long_to_bytes |
| 7 |
from Crypto.Util.py3compat import bchr |
| 8 |
|
| 9 |
from .keccak import _raw_keccak_lib |
| 10 |
|
| 11 |
|
| 12 |
class TurboSHAKE(object): |
| 13 |
"""A TurboSHAKE hash object. |
| 14 |
Do not instantiate directly. |
| 15 |
Use the :func:`new` function. |
| 16 |
""" |
| 17 |
|
| 18 |
def __init__(self, capacity, domain_separation, data): |
| 19 |
|
| 20 |
state = VoidPointer() |
| 21 |
result = _raw_keccak_lib.keccak_init(state.address_of(), |
| 22 |
c_size_t(capacity), |
| 23 |
c_ubyte(12)) |
| 24 |
if result: |
| 25 |
raise ValueError("Error %d while instantiating TurboSHAKE" |
| 26 |
% result) |
| 27 |
self._state = SmartPointer(state.get(), _raw_keccak_lib.keccak_destroy) |
| 28 |
|
| 29 |
self._is_squeezing = False |
| 30 |
self._capacity = capacity |
| 31 |
self._domain = domain_separation |
| 32 |
|
| 33 |
if data: |
| 34 |
self.update(data) |
| 35 |
|
| 36 |
|
| 37 |
def update(self, data): |
| 38 |
"""Continue hashing of a message by consuming the next chunk of data. |
| 39 |
|
| 40 |
Args: |
| 41 |
data (byte string/byte array/memoryview): The next chunk of the message being hashed. |
| 42 |
""" |
| 43 |
|
| 44 |
if self._is_squeezing: |
| 45 |
raise TypeError("You cannot call 'update' after the first 'read'") |
| 46 |
|
| 47 |
result = _raw_keccak_lib.keccak_absorb(self._state.get(), |
| 48 |
c_uint8_ptr(data), |
| 49 |
c_size_t(len(data))) |
| 50 |
if result: |
| 51 |
raise ValueError("Error %d while updating TurboSHAKE state" |
| 52 |
% result) |
| 53 |
return self |
| 54 |
|
| 55 |
def read(self, length): |
| 56 |
""" |
| 57 |
Compute the next piece of XOF output. |
| 58 |
|
| 59 |
.. note:: |
| 60 |
You cannot use :meth:`update` anymore after the first call to |
| 61 |
:meth:`read`. |
| 62 |
|
| 63 |
Args: |
| 64 |
length (integer): the amount of bytes this method must return |
| 65 |
|
| 66 |
:return: the next piece of XOF output (of the given length) |
| 67 |
:rtype: byte string |
| 68 |
""" |
| 69 |
|
| 70 |
self._is_squeezing = True |
| 71 |
bfr = create_string_buffer(length) |
| 72 |
result = _raw_keccak_lib.keccak_squeeze(self._state.get(), |
| 73 |
bfr, |
| 74 |
c_size_t(length), |
| 75 |
c_ubyte(self._domain)) |
| 76 |
if result: |
| 77 |
raise ValueError("Error %d while extracting from TurboSHAKE" |
| 78 |
% result) |
| 79 |
|
| 80 |
return get_raw_buffer(bfr) |
| 81 |
|
| 82 |
def new(self, data=None): |
| 83 |
return type(self)(self._capacity, self._domain, data) |
| 84 |
|
| 85 |
def _reset(self): |
| 86 |
result = _raw_keccak_lib.keccak_reset(self._state.get()) |
| 87 |
if result: |
| 88 |
raise ValueError("Error %d while resetting TurboSHAKE state" |
| 89 |
% result) |
| 90 |
self._is_squeezing = False |
| 91 |
|
| 92 |
|
| 93 |
def new(**kwargs): |
| 94 |
"""Create a new TurboSHAKE128 object. |
| 95 |
|
| 96 |
Args: |
| 97 |
domain (integer): |
| 98 |
Optional - A domain separation byte, between 0x01 and 0x7F. |
| 99 |
The default value is 0x1F. |
| 100 |
data (bytes/bytearray/memoryview): |
| 101 |
Optional - The very first chunk of the message to hash. |
| 102 |
It is equivalent to an early call to :meth:`update`. |
| 103 |
|
| 104 |
:Return: A :class:`TurboSHAKE` object |
| 105 |
""" |
| 106 |
|
| 107 |
domain_separation = kwargs.get('domain', 0x1F) |
| 108 |
if not (0x01 <= domain_separation <= 0x7F): |
| 109 |
raise ValueError("Incorrect domain separation value (%d)" % |
| 110 |
domain_separation) |
| 111 |
data = kwargs.get('data') |
| 112 |
return TurboSHAKE(32, domain_separation, data=data) |