Debian Codegraph @

lib/Crypto/Hash/SHAKE128.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 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 # ASN.1 Object ID
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)