Arduino Cryptography Library
SHA3.cpp
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "SHA3.h"
24 #include "Crypto.h"
25 
49 {
50  core.setCapacity(512);
51 }
52 
57 {
58  // The destructor for the KeccakCore object will do most of the work.
59 }
60 
61 size_t SHA3_256::hashSize() const
62 {
63  return 32;
64 }
65 
66 size_t SHA3_256::blockSize() const
67 {
68  return core.blockSize();
69 }
70 
72 {
73  core.reset();
74 }
75 
76 void SHA3_256::update(const void *data, size_t len)
77 {
78  core.update(data, len);
79 }
80 
81 void SHA3_256::finalize(void *hash, size_t len)
82 {
83  // Pad the final block and then extract the hash value.
84  core.pad(0x06);
85  core.extract(hash, len);
86 }
87 
89 {
90  core.clear();
91 }
92 
93 void SHA3_256::resetHMAC(const void *key, size_t keyLen)
94 {
95  core.setHMACKey(key, keyLen, 0x36, 32);
96 }
97 
98 void SHA3_256::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
99 {
100  uint8_t temp[32];
101  finalize(temp, sizeof(temp));
102  core.setHMACKey(key, keyLen, 0x5C, 32);
103  core.update(temp, sizeof(temp));
104  finalize(hash, hashLen);
105  clean(temp);
106 }
107 
131 {
132  core.setCapacity(1024);
133 }
134 
139 {
140  // The destructor for the KeccakCore object will do most of the work.
141 }
142 
143 size_t SHA3_512::hashSize() const
144 {
145  return 64;
146 }
147 
148 size_t SHA3_512::blockSize() const
149 {
150  return core.blockSize();
151 }
152 
154 {
155  core.reset();
156 }
157 
158 void SHA3_512::update(const void *data, size_t len)
159 {
160  core.update(data, len);
161 }
162 
163 void SHA3_512::finalize(void *hash, size_t len)
164 {
165  // Pad the final block and then extract the hash value.
166  core.pad(0x06);
167  core.extract(hash, len);
168 }
169 
171 {
172  core.clear();
173 }
174 
175 void SHA3_512::resetHMAC(const void *key, size_t keyLen)
176 {
177  core.setHMACKey(key, keyLen, 0x36, 64);
178 }
179 
180 void SHA3_512::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
181 {
182  uint8_t temp[64];
183  finalize(temp, sizeof(temp));
184  core.setHMACKey(key, keyLen, 0x5C, 64);
185  core.update(temp, sizeof(temp));
186  finalize(hash, hashLen);
187  clean(temp);
188 }
size_t blockSize() const
Returns the input block size for the sponge function in bytes.
Definition: KeccakCore.h:38
void reset()
Resets the Keccak sponge function ready for a new session.
Definition: KeccakCore.cpp:114
void pad(uint8_t tag)
Pads the last block of input data to blockSize().
Definition: KeccakCore.cpp:167
void update(const void *data, size_t size)
Updates the Keccak sponge function with more input data.
Definition: KeccakCore.cpp:133
void extract(void *data, size_t size)
Extracts data from the Keccak sponge function.
Definition: KeccakCore.cpp:194
void setCapacity(size_t capacity)
Sets the capacity of the Keccak sponge function in bits.
Definition: KeccakCore.cpp:94
void clear()
Clears all sensitive data from this object.
Definition: KeccakCore.cpp:275
void setHMACKey(const void *key, size_t len, uint8_t pad, size_t hashSize)
Sets a HMAC key for a Keccak-based hash algorithm.
Definition: KeccakCore.cpp:293
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.
Definition: SHA3.cpp:98
size_t blockSize() const
Size of the internal block used by the hash algorithm.
Definition: SHA3.cpp:66
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
Definition: SHA3.cpp:93
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
Definition: SHA3.cpp:88
void reset()
Resets the hash ready for a new hashing process.
Definition: SHA3.cpp:71
void update(const void *data, size_t len)
Updates the hash with more data.
Definition: SHA3.cpp:76
virtual ~SHA3_256()
Destroys this hash object after clearing sensitive information.
Definition: SHA3.cpp:56
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
Definition: SHA3.cpp:81
SHA3_256()
Constructs a new SHA3-256 hash object.
Definition: SHA3.cpp:48
size_t hashSize() const
Size of the hash result from finalize().
Definition: SHA3.cpp:61
void update(const void *data, size_t len)
Updates the hash with more data.
Definition: SHA3.cpp:158
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.
Definition: SHA3.cpp:180
virtual ~SHA3_512()
Destroys this hash object after clearing sensitive information.
Definition: SHA3.cpp:138
void reset()
Resets the hash ready for a new hashing process.
Definition: SHA3.cpp:153
SHA3_512()
Constructs a new SHA3-512 hash object.
Definition: SHA3.cpp:130
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
Definition: SHA3.cpp:175
size_t hashSize() const
Size of the hash result from finalize().
Definition: SHA3.cpp:143
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
Definition: SHA3.cpp:163
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
Definition: SHA3.cpp:170
size_t blockSize() const
Size of the internal block used by the hash algorithm.
Definition: SHA3.cpp:148