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