-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.cpp
180 lines (167 loc) · 5.29 KB
/
Hash.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (C) 2015 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "Hash.h"
#include <string.h>
/**
* \class Hash Hash.h <Hash.h>
* \brief Abstract base class for cryptographic hash algorithms.
*
* \sa SHA256, SHA3_256, BLAKE2s
*/
/**
* \brief Constructs a new hash object.
*/
Hash::Hash()
{
}
/**
* \brief Destroys this hash object.
*
* \note Subclasses are responsible for clearing any sensitive data
* that remains in the hash object when it is destroyed.
*
* \sa clear()
*/
Hash::~Hash()
{
}
/**
* \fn size_t Hash::hashSize() const
* \brief Size of the hash result from finalize().
*
* \sa finalize(), blockSize()
*/
/**
* \fn size_t Hash::blockSize() const
* \brief Size of the internal block used by the hash algorithm.
*
* \sa update(), hashSize()
*/
/**
* \fn void Hash::reset()
* \brief Resets the hash ready for a new hashing process.
*
* \sa update(), finalize(), resetHMAC()
*/
/**
* \fn void Hash::update(const void *data, size_t len)
* \brief Updates the hash with more data.
*
* \param data Data to be hashed.
* \param len Number of bytes of data to be hashed.
*
* If finalize() has already been called, then the behavior of update() will
* be undefined. Call reset() first to start a new hashing process.
*
* \sa reset(), finalize()
*/
/**
* \fn void Hash::finalize(void *hash, size_t len)
* \brief Finalizes the hashing process and returns the hash.
*
* \param hash The buffer to return the hash value in.
* \param len The length of the \a hash buffer, normally hashSize().
*
* If \a len is less than hashSize(), then the hash value will be
* truncated to the first \a len bytes. If \a len is greater than
* hashSize(), then the remaining bytes will left unchanged.
*
* If finalize() is called again, then the returned \a hash value is
* undefined. Call reset() first to start a new hashing process.
*
* \sa reset(), update(), finalizeHMAC()
*/
/**
* \fn void Hash::clear()
* \brief Clears the hash state, removing all sensitive data, and then
* resets the hash ready for a new hashing process.
*
* \sa reset()
*/
/**
* \fn void Hash::resetHMAC(const void *key, size_t keyLen)
* \brief Resets the hash ready for a new HMAC hashing process.
*
* \param key Points to the HMAC key for the hashing process.
* \param keyLen Size of the HMAC \a key in bytes.
*
* The following example computes a HMAC over a series of data blocks
* with a specific key:
*
* \code
* hash.resetHMAC(key, sizeof(key));
* hash.update(data1, sizeof(data1));
* hash.update(data2, sizeof(data2));
* ...
* hash.update(dataN, sizeof(dataN));
* hash.finalizeHMAC(key, sizeof(key), hmac, sizeof(hmac));
* \endcode
*
* The same key must be passed to both resetHMAC() and finalizeHMAC().
*
* \sa finalizeHMAC(), reset()
*/
/**
* \fn void Hash::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
* \brief Finalizes the HMAC hashing process and returns the hash.
*
* \param key Points to the HMAC key for the hashing process. The contents
* of this array must be identical to the value passed to resetHMAC().
* \param keyLen Size of the HMAC \a key in bytes.
* \param hash The buffer to return the hash value in.
* \param hashLen The length of the \a hash buffer, normally hashSize().
*
* \sa resetHMAC(), finalize()
*/
/**
* \brief Formats a HMAC key into a block.
*
* \param block The block to format the key into. Must be at least
* blockSize() bytes in length.
* \param key Points to the HMAC key for the hashing process.
* \param len Length of the HMAC \a key in bytes.
* \param pad Inner (0x36) or outer (0x5C) padding value to XOR with
* the formatted HMAC key.
*
* This function is intended to help subclasses implement resetHMAC() and
* finalizeHMAC() by directly formatting the HMAC key into the subclass's
* internal block buffer and resetting the hash.
*/
void Hash::formatHMACKey(void *block, const void *key, size_t len, uint8_t pad)
{
size_t size = blockSize();
reset();
if (len <= size) {
memcpy(block, key, len);
} else {
update(key, len);
len = hashSize();
finalize(block, len);
reset();
}
uint8_t *b = (uint8_t *)block;
memset(b + len, pad, size - len);
while (len > 0) {
*b++ ^= pad;
--len;
}
}