ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups 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.finalized = false;
73  state.length = 0;
74 }
75 
76 void SHA1::update(const void *data, size_t len)
77 {
78  // Reset the hashing process if finalize() was called previously.
79  if (state.finalized)
80  reset();
81 
82  // Update the total length (in bits, not bytes).
83  state.length += ((uint64_t)len) << 3;
84 
85  // Break the input up into 512-bit chunks and process each in turn.
86  const uint8_t *d = (const uint8_t *)data;
87  while (len > 0) {
88  uint8_t size = 64 - state.chunkSize;
89  if (size > len)
90  size = len;
91  memcpy(((uint8_t *)state.w) + state.chunkSize, d, size);
92  state.chunkSize += size;
93  len -= size;
94  d += size;
95  if (state.chunkSize == 64) {
96  processChunk();
97  state.chunkSize = 0;
98  }
99  }
100 }
101 
102 void SHA1::finalize(void *hash, size_t len)
103 {
104  // Finalize the hash if necessary.
105  if (!state.finalized) {
106  // Pad the last chunk. We may need two padding chunks if there
107  // isn't enough room in the first for the padding and length.
108  uint8_t *wbytes = (uint8_t *)state.w;
109  if (state.chunkSize <= (64 - 9)) {
110  wbytes[state.chunkSize] = 0x80;
111  memset(wbytes + state.chunkSize + 1, 0x00, 64 - 8 - (state.chunkSize + 1));
112  state.w[14] = htobe32((uint32_t)(state.length >> 32));
113  state.w[15] = htobe32((uint32_t)state.length);
114  processChunk();
115  } else {
116  wbytes[state.chunkSize] = 0x80;
117  memset(wbytes + state.chunkSize + 1, 0x00, 64 - (state.chunkSize + 1));
118  processChunk();
119  memset(wbytes, 0x00, 64 - 8);
120  state.w[14] = htobe32((uint32_t)(state.length >> 32));
121  state.w[15] = htobe32((uint32_t)state.length);
122  processChunk();
123  }
124 
125  // Convert the result into big endian and return it.
126  for (uint8_t posn = 0; posn < 5; ++posn)
127  state.w[posn] = htobe32(state.h[posn]);
128  state.finalized = true;
129  }
130 
131  // Copy the hash to the caller's return buffer.
132  if (len > 20)
133  len = 20;
134  memcpy(hash, state.w, len);
135 }
136 
138 {
139  clean(state);
140  reset();
141 }
142 
148 void SHA1::processChunk()
149 {
150  uint8_t index;
151 
152  // Convert the first 16 words from big endian to host byte order.
153  for (index = 0; index < 16; ++index)
154  state.w[index] = be32toh(state.w[index]);
155 
156  // Initialize the hash value for this chunk.
157  uint32_t a = state.h[0];
158  uint32_t b = state.h[1];
159  uint32_t c = state.h[2];
160  uint32_t d = state.h[3];
161  uint32_t e = state.h[4];
162 
163  // Perform the first 16 rounds of the compression function main loop.
164  uint32_t temp;
165  for (index = 0; index < 16; ++index) {
166  temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
167  e = d;
168  d = c;
169  c = leftRotate30(b);
170  b = a;
171  a = temp;
172  }
173 
174  // Perform the 64 remaining rounds. We expand the first 16 words to
175  // 80 in-place in the "w" array. This saves 256 bytes of memory
176  // that would have otherwise need to be allocated to the "w" array.
177  for (; index < 20; ++index) {
178  temp = state.w[index & 0x0F] = leftRotate1
179  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
180  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
181  temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
182  e = d;
183  d = c;
184  c = leftRotate30(b);
185  b = a;
186  a = temp;
187  }
188  for (; index < 40; ++index) {
189  temp = state.w[index & 0x0F] = leftRotate1
190  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
191  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
192  temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
193  e = d;
194  d = c;
195  c = leftRotate30(b);
196  b = a;
197  a = temp;
198  }
199  for (; index < 60; ++index) {
200  temp = state.w[index & 0x0F] = leftRotate1
201  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
202  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
203  temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
204  e = d;
205  d = c;
206  c = leftRotate30(b);
207  b = a;
208  a = temp;
209  }
210  for (; index < 80; ++index) {
211  temp = state.w[index & 0x0F] = leftRotate1
212  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
213  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
214  temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
215  e = d;
216  d = c;
217  c = leftRotate30(b);
218  b = a;
219  a = temp;
220  }
221 
222  // Add this chunk's hash to the result so far.
223  state.h[0] += a;
224  state.h[1] += b;
225  state.h[2] += c;
226  state.h[3] += d;
227  state.h[4] += e;
228 
229  // Attempt to clean up the stack.
230  a = b = c = d = e = temp = 0;
231 }
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:76
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
Definition: SHA1.cpp:137
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:102
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