Debian Codegraph @

lib/Crypto/Hash/SHA3_384.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # ===================================================================
4 # The contents of this file are dedicated to the public domain. To
5 # the extent that dedication to the public domain is not available,
6 # everyone is granted a worldwide, perpetual, royalty-free,
7 # non-exclusive license to exercise all rights associated with the
8 # contents of this file for any purpose whatsoever.
9 # No rights are reserved.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
15 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 # SOFTWARE.
19 # ===================================================================
20
21 from Crypto.Util.py3compat import bord
22
23 from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
24 VoidPointer, SmartPointer,
25 create_string_buffer,
26 get_raw_buffer, c_size_t,
27 c_uint8_ptr, c_ubyte)
28
29 from Crypto.Hash.keccak import _raw_keccak_lib
30
31 class SHA3_384_Hash(object):
32 """A SHA3-384 hash object.
33 Do not instantiate directly.
34 Use the :func:`new` function.
35
36 :ivar oid: ASN.1 Object ID
37 :vartype oid: string
38
39 :ivar digest_size: the size in bytes of the resulting hash
40 :vartype digest_size: integer
41 """
42
43 # The size of the resulting hash in bytes.
44 digest_size = 48
45
46 # ASN.1 Object ID
47 oid = "2.16.840.1.101.3.4.2.9"
48
49 # Input block size for HMAC
50 block_size = 104
51
52 def __init__(self, data, update_after_digest):
53 self._update_after_digest = update_after_digest
54 self._digest_done = False
55 self._padding = 0x06
56
57 state = VoidPointer()
58 result = _raw_keccak_lib.keccak_init(state.address_of(),
59 c_size_t(self.digest_size * 2),
60 c_ubyte(24))
61 if result:
62 raise ValueError("Error %d while instantiating SHA-3/384"
63 % result)
64 self._state = SmartPointer(state.get(),
65 _raw_keccak_lib.keccak_destroy)
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._digest_done and not self._update_after_digest:
77 raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
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 SHA-3/384"
84 % result)
85 return self
86
87 def digest(self):
88 """Return the **binary** (non-printable) digest of the message that has been hashed so far.
89
90 :return: The hash digest, computed over the data processed so far.
91 Binary form.
92 :rtype: byte string
93 """
94
95 self._digest_done = True
96
97 bfr = create_string_buffer(self.digest_size)
98 result = _raw_keccak_lib.keccak_digest(self._state.get(),
99 bfr,
100 c_size_t(self.digest_size),
101 c_ubyte(self._padding))
102 if result:
103 raise ValueError("Error %d while instantiating SHA-3/384"
104 % result)
105
106 self._digest_value = get_raw_buffer(bfr)
107 return self._digest_value
108
109 def hexdigest(self):
110 """Return the **printable** digest of the message that has been hashed so far.
111
112 :return: The hash digest, computed over the data processed so far.
113 Hexadecimal encoded.
114 :rtype: string
115 """
116
117 return "".join(["%02x" % bord(x) for x in self.digest()])
118
119 def copy(self):
120 """Return a copy ("clone") of the hash object.
121
122 The copy will have the same internal state as the original hash
123 object.
124 This can be used to efficiently compute the digests of strings that
125 share a common initial substring.
126
127 :return: A hash object of the same type
128 """
129
130 clone = self.new()
131 result = _raw_keccak_lib.keccak_copy(self._state.get(),
132 clone._state.get())
133 if result:
134 raise ValueError("Error %d while copying SHA3-384" % result)
135 return clone
136
137 def new(self, data=None):
138 """Create a fresh SHA3-256 hash object."""
139
140 return type(self)(data, self._update_after_digest)
141
142
143 def new(self, data=None):
144 """Create a fresh SHA3-384 hash object."""
145
146 return type(self)(data, self._update_after_digest)
147
148
149 def new(*args, **kwargs):
150 """Create a new hash object.
151
152 Args:
153 data (byte string/byte array/memoryview):
154 The very first chunk of the message to hash.
155 It is equivalent to an early call to :meth:`update`.
156 update_after_digest (boolean):
157 Whether :meth:`digest` can be followed by another :meth:`update`
158 (default: ``False``).
159
160 :Return: A :class:`SHA3_384_Hash` hash object
161 """
162
163 data = kwargs.pop("data", None)
164 update_after_digest = kwargs.pop("update_after_digest", False)
165 if len(args) == 1:
166 if data:
167 raise ValueError("Initial data for hash specified twice")
168 data = args[0]
169
170 if kwargs:
171 raise TypeError("Unknown parameters: " + str(kwargs))
172
173 return SHA3_384_Hash(data, update_after_digest)
174
175 # The size of the resulting hash in bytes.
176 digest_size = SHA3_384_Hash.digest_size
177
178 # Input block size for HMAC
179 block_size = 104