Debian Codegraph @

lib/Crypto/Hash/SHAKE256.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2015, Legrandin <[email protected]>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
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 SHAKE256_XOF(object):
42 """A SHAKE256 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 # ASN.1 Object ID
51 oid = "2.16.840.1.101.3.4.2.12"
52
53 def __init__(self, data=None):
54 state = VoidPointer()
55 result = _raw_keccak_lib.keccak_init(state.address_of(),
56 c_size_t(64),
57 c_ubyte(24))
58 if result:
59 raise ValueError("Error %d while instantiating SHAKE256"
60 % result)
61 self._state = SmartPointer(state.get(),
62 _raw_keccak_lib.keccak_destroy)
63 self._is_squeezing = False
64 self._padding = 0x1F
65
66 if data:
67 self.update(data)
68
69 def update(self, data):
70 """Continue hashing of a message by consuming the next chunk of data.
71
72 Args:
73 data (byte string/byte array/memoryview): The next chunk of the message being hashed.
74 """
75
76 if self._is_squeezing:
77 raise TypeError("You cannot call 'update' after the first 'read'")
78
79 result = _raw_keccak_lib.keccak_absorb(self._state.get(),
80 c_uint8_ptr(data),
81 c_size_t(len(data)))
82 if result:
83 raise ValueError("Error %d while updating SHAKE256 state"
84 % result)
85 return self
86
87 def read(self, length):
88 """
89 Compute the next piece of XOF output.
90
91 .. note::
92 You cannot use :meth:`update` anymore after the first call to
93 :meth:`read`.
94
95 Args:
96 length (integer): the amount of bytes this method must return
97
98 :return: the next piece of XOF output (of the given length)
99 :rtype: byte string
100 """
101
102 self._is_squeezing = True
103 bfr = create_string_buffer(length)
104 result = _raw_keccak_lib.keccak_squeeze(self._state.get(),
105 bfr,
106 c_size_t(length),
107 c_ubyte(self._padding))
108 if result:
109 raise ValueError("Error %d while extracting from SHAKE256"
110 % result)
111
112 return get_raw_buffer(bfr)
113
114 def new(self, data=None):
115 return type(self)(data=data)
116
117
118 def new(data=None):
119 """Return a fresh instance of a SHAKE256 object.
120
121 Args:
122 data (bytes/bytearray/memoryview):
123 The very first chunk of the message to hash.
124 It is equivalent to an early call to :meth:`update`.
125 Optional.
126
127 :Return: A :class:`SHAKE256_XOF` object
128 """
129
130 return SHAKE256_XOF(data=data)