Arduino Cryptography Library
 All Classes Files Functions Variables Enumerations Enumerator Pages
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 
39 {
40  core.setCapacity(512);
41 }
42 
47 {
48  // The destructor for the KeccakCore object will do most of the work.
49 }
50 
51 size_t SHA3_256::hashSize() const
52 {
53  return 32;
54 }
55 
56 size_t SHA3_256::blockSize() const
57 {
58  return core.blockSize();
59 }
60 
62 {
63  core.reset();
64 }
65 
66 void SHA3_256::update(const void *data, size_t len)
67 {
68  core.update(data, len);
69 }
70 
71 void SHA3_256::finalize(void *hash, size_t len)
72 {
73  // Pad the final block and then extract the hash value.
74  core.pad(0x06);
75  core.extract(hash, len);
76 }
77 
79 {
80  core.clear();
81 }
82 
83 void SHA3_256::resetHMAC(const void *key, size_t keyLen)
84 {
85  core.setHMACKey(key, keyLen, 0x36, 32);
86 }
87 
88 void SHA3_256::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
89 {
90  uint8_t temp[32];
91  finalize(temp, sizeof(temp));
92  core.setHMACKey(key, keyLen, 0x5C, 32);
93  core.update(temp, sizeof(temp));
94  finalize(hash, hashLen);
95  clean(temp);
96 }
97 
111 {
112  core.setCapacity(1024);
113 }
114 
119 {
120  // The destructor for the KeccakCore object will do most of the work.
121 }
122 
123 size_t SHA3_512::hashSize() const
124 {
125  return 64;
126 }
127 
128 size_t SHA3_512::blockSize() const
129 {
130  return core.blockSize();
131 }
132 
134 {
135  core.reset();
136 }
137 
138 void SHA3_512::update(const void *data, size_t len)
139 {
140  core.update(data, len);
141 }
142 
143 void SHA3_512::finalize(void *hash, size_t len)
144 {
145  // Pad the final block and then extract the hash value.
146  core.pad(0x06);
147  core.extract(hash, len);
148 }
149 
151 {
152  core.clear();
153 }
154 
155 void SHA3_512::resetHMAC(const void *key, size_t keyLen)
156 {
157  core.setHMACKey(key, keyLen, 0x36, 64);
158 }
159 
160 void SHA3_512::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
161 {
162  uint8_t temp[64];
163  finalize(temp, sizeof(temp));
164  core.setHMACKey(key, keyLen, 0x5C, 64);
165  core.update(temp, sizeof(temp));
166  finalize(hash, hashLen);
167  clean(temp);
168 }
SHA3_512()
Constructs a new SHA3-512 hash object.
Definition: SHA3.cpp:110
void reset()
Resets the hash ready for a new hashing process.
Definition: SHA3.cpp:61
size_t blockSize() const
Returns the input block size for the sponge function in bytes.
Definition: KeccakCore.h:38
virtual ~SHA3_256()
Destroys this hash object after clearing sensitive information.
Definition: SHA3.cpp:46
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
Definition: SHA3.cpp:83
size_t hashSize() const
Size of the hash result from finalize().
Definition: SHA3.cpp:123
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
SHA3_256()
Constructs a new SHA3-256 hash object.
Definition: SHA3.cpp:38
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:88
virtual ~SHA3_512()
Destroys this hash object after clearing sensitive information.
Definition: SHA3.cpp:118
size_t hashSize() const
Size of the hash result from finalize().
Definition: SHA3.cpp:51
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
Definition: SHA3.cpp:143
void reset()
Resets the hash ready for a new hashing process.
Definition: SHA3.cpp:133
void setCapacity(size_t capacity)
Sets the capacity of the Keccak sponge function in bits.
Definition: KeccakCore.cpp:94
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:160
size_t blockSize() const
Size of the internal block used by the hash algorithm.
Definition: SHA3.cpp:56
void extract(void *data, size_t size)
Extracts data from the Keccak sponge function.
Definition: KeccakCore.cpp:194
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
Definition: SHA3.cpp:155
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
Definition: SHA3.cpp:150
void pad(uint8_t tag)
Pads the last block of input data to blockSize().
Definition: KeccakCore.cpp:167
size_t blockSize() const
Size of the internal block used by the hash algorithm.
Definition: SHA3.cpp:128
void update(const void *data, size_t len)
Updates the hash with more data.
Definition: SHA3.cpp:138
void update(const void *data, size_t size)
Updates the Keccak sponge function with more input data.
Definition: KeccakCore.cpp:133
void clear()
Clears all sensitive data from this object.
Definition: KeccakCore.cpp:275
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
Definition: SHA3.cpp:71
void reset()
Resets the Keccak sponge function ready for a new session.
Definition: KeccakCore.cpp:114
void update(const void *data, size_t len)
Updates the hash with more data.
Definition: SHA3.cpp:66
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
Definition: SHA3.cpp:78