Arduino Cryptography Library
SHA1.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 "SHA1.h"
24 #include "Crypto.h"
25 #include "utility/RotateUtil.h"
26 #include "utility/EndianUtil.h"
27 #include <string.h>
28 
52 {
53  reset();
54 }
55 
60 {
61  clean(state);
62 }
63 
64 size_t SHA1::hashSize() const
65 {
66  return 20;
67 }
68 
69 size_t SHA1::blockSize() const
70 {
71  return 64;
72 }
73 
75 {
76  state.h[0] = 0x67452301;
77  state.h[1] = 0xEFCDAB89;
78  state.h[2] = 0x98BADCFE;
79  state.h[3] = 0x10325476;
80  state.h[4] = 0xC3D2E1F0;
81  state.chunkSize = 0;
82  state.length = 0;
83 }
84 
85 void SHA1::update(const void *data, size_t len)
86 {
87  // Update the total length (in bits, not bytes).
88  state.length += ((uint64_t)len) << 3;
89 
90  // Break the input up into 512-bit chunks and process each in turn.
91  const uint8_t *d = (const uint8_t *)data;
92  while (len > 0) {
93  uint8_t size = 64 - state.chunkSize;
94  if (size > len)
95  size = len;
96  memcpy(((uint8_t *)state.w) + state.chunkSize, d, size);
97  state.chunkSize += size;
98  len -= size;
99  d += size;
100  if (state.chunkSize == 64) {
101  processChunk();
102  state.chunkSize = 0;
103  }
104  }
105 }
106 
107 void SHA1::finalize(void *hash, size_t len)
108 {
109  // Pad the last chunk. We may need two padding chunks if there
110  // isn't enough room in the first for the padding and length.
111  uint8_t *wbytes = (uint8_t *)state.w;
112  if (state.chunkSize <= (64 - 9)) {
113  wbytes[state.chunkSize] = 0x80;
114  memset(wbytes + state.chunkSize + 1, 0x00, 64 - 8 - (state.chunkSize + 1));
115  state.w[14] = htobe32((uint32_t)(state.length >> 32));
116  state.w[15] = htobe32((uint32_t)state.length);
117  processChunk();
118  } else {
119  wbytes[state.chunkSize] = 0x80;
120  memset(wbytes + state.chunkSize + 1, 0x00, 64 - (state.chunkSize + 1));
121  processChunk();
122  memset(wbytes, 0x00, 64 - 8);
123  state.w[14] = htobe32((uint32_t)(state.length >> 32));
124  state.w[15] = htobe32((uint32_t)state.length);
125  processChunk();
126  }
127 
128  // Convert the result into big endian and return it.
129  for (uint8_t posn = 0; posn < 5; ++posn)
130  state.w[posn] = htobe32(state.h[posn]);
131 
132  // Copy the hash to the caller's return buffer.
133  if (len > 20)
134  len = 20;
135  memcpy(hash, state.w, len);
136 }
137 
139 {
140  clean(state);
141  reset();
142 }
143 
144 void SHA1::resetHMAC(const void *key, size_t keyLen)
145 {
146  formatHMACKey(state.w, key, keyLen, 0x36);
147  state.length += 64 * 8;
148  processChunk();
149 }
150 
151 void SHA1::finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
152 {
153  uint8_t temp[20];
154  finalize(temp, sizeof(temp));
155  formatHMACKey(state.w, key, keyLen, 0x5C);
156  state.length += 64 * 8;
157  processChunk();
158  update(temp, sizeof(temp));
159  finalize(hash, hashLen);
160  clean(temp);
161 }
162 
168 void SHA1::processChunk()
169 {
170  uint8_t index;
171 
172  // Convert the first 16 words from big endian to host byte order.
173  for (index = 0; index < 16; ++index)
174  state.w[index] = be32toh(state.w[index]);
175 
176  // Initialize the hash value for this chunk.
177  uint32_t a = state.h[0];
178  uint32_t b = state.h[1];
179  uint32_t c = state.h[2];
180  uint32_t d = state.h[3];
181  uint32_t e = state.h[4];
182 
183  // Perform the first 16 rounds of the compression function main loop.
184  uint32_t temp;
185  for (index = 0; index < 16; ++index) {
186  temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
187  e = d;
188  d = c;
189  c = leftRotate30(b);
190  b = a;
191  a = temp;
192  }
193 
194  // Perform the 64 remaining rounds. We expand the first 16 words to
195  // 80 in-place in the "w" array. This saves 256 bytes of memory
196  // that would have otherwise need to be allocated to the "w" array.
197  for (; index < 20; ++index) {
198  temp = state.w[index & 0x0F] = leftRotate1
199  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
200  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
201  temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
202  e = d;
203  d = c;
204  c = leftRotate30(b);
205  b = a;
206  a = temp;
207  }
208  for (; index < 40; ++index) {
209  temp = state.w[index & 0x0F] = leftRotate1
210  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
211  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
212  temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
213  e = d;
214  d = c;
215  c = leftRotate30(b);
216  b = a;
217  a = temp;
218  }
219  for (; index < 60; ++index) {
220  temp = state.w[index & 0x0F] = leftRotate1
221  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
222  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
223  temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
224  e = d;
225  d = c;
226  c = leftRotate30(b);
227  b = a;
228  a = temp;
229  }
230  for (; index < 80; ++index) {
231  temp = state.w[index & 0x0F] = leftRotate1
232  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
233  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
234  temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
235  e = d;
236  d = c;
237  c = leftRotate30(b);
238  b = a;
239  a = temp;
240  }
241 
242  // Add this chunk's hash to the result so far.
243  state.h[0] += a;
244  state.h[1] += b;
245  state.h[2] += c;
246  state.h[3] += d;
247  state.h[4] += e;
248 
249  // Attempt to clean up the stack.
250  a = b = c = d = e = temp = 0;
251 }
void formatHMACKey(void *block, const void *key, size_t len, uint8_t pad)
Formats a HMAC key into a block.
Definition: Hash.cpp:162
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
Definition: SHA1.cpp:138
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
Definition: SHA1.cpp:107
size_t hashSize() const
Size of the hash result from finalize().
Definition: SHA1.cpp:64
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.
Definition: SHA1.cpp:151
virtual ~SHA1()
Destroys this SHA-1 hash object after clearing sensitive information.
Definition: SHA1.cpp:59
size_t blockSize() const
Size of the internal block used by the hash algorithm.
Definition: SHA1.cpp:69
void reset()
Resets the hash ready for a new hashing process.
Definition: SHA1.cpp:74
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
Definition: SHA1.cpp:144
SHA1()
Constructs a SHA-1 hash object.
Definition: SHA1.cpp:51
void update(const void *data, size_t len)
Updates the hash with more data.
Definition: SHA1.cpp:85