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