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.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 
139 void SHA1::processChunk()
140 {
141  uint8_t index;
142 
143  // Convert the first 16 words from big endian to host byte order.
144  for (index = 0; index < 16; ++index)
145  state.w[index] = be32toh(state.w[index]);
146 
147  // Initialize the hash value for this chunk.
148  uint32_t a = state.h[0];
149  uint32_t b = state.h[1];
150  uint32_t c = state.h[2];
151  uint32_t d = state.h[3];
152  uint32_t e = state.h[4];
153 
154  // Perform the first 16 rounds of the compression function main loop.
155  uint32_t temp;
156  for (index = 0; index < 16; ++index) {
157  temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
158  e = d;
159  d = c;
160  c = leftRotate30(b);
161  b = a;
162  a = temp;
163  }
164 
165  // Perform the 64 remaining rounds. We expand the first 16 words to
166  // 80 in-place in the "w" array. This saves 256 bytes of memory
167  // that would have otherwise need to be allocated to the "w" array.
168  for (; index < 20; ++index) {
169  temp = state.w[index & 0x0F] = leftRotate1
170  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
171  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
172  temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
173  e = d;
174  d = c;
175  c = leftRotate30(b);
176  b = a;
177  a = temp;
178  }
179  for (; index < 40; ++index) {
180  temp = state.w[index & 0x0F] = leftRotate1
181  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
182  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
183  temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
184  e = d;
185  d = c;
186  c = leftRotate30(b);
187  b = a;
188  a = temp;
189  }
190  for (; index < 60; ++index) {
191  temp = state.w[index & 0x0F] = leftRotate1
192  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
193  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
194  temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
195  e = d;
196  d = c;
197  c = leftRotate30(b);
198  b = a;
199  a = temp;
200  }
201  for (; index < 80; ++index) {
202  temp = state.w[index & 0x0F] = leftRotate1
203  (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
204  state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
205  temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
206  e = d;
207  d = c;
208  c = leftRotate30(b);
209  b = a;
210  a = temp;
211  }
212 
213  // Add this chunk's hash to the result so far.
214  state.h[0] += a;
215  state.h[1] += b;
216  state.h[2] += c;
217  state.h[3] += d;
218  state.h[4] += e;
219 
220  // Attempt to clean up the stack.
221  a = b = c = d = e = temp = 0;
222 }
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
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