MeterLogger
sha256.c
Go to the documentation of this file.
1 /**
2  * Copyright (c) 2000-2001 Aaron D. Gifford
3  * Copyright (c) 2013-2014 Pavol Rusnak
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <esp8266.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include "crypto/crypto.h"
35 #include "crypto/sha256.h"
36 
37 
38 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
39 /*
40  * BYTE_ORDER NOTE:
41  *
42  * Please make sure that your system defines BYTE_ORDER. If your
43  * architecture is little-endian, make sure it also defines
44  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
45  * equivilent.
46  *
47  * If your system does not define the above, then you can do so by
48  * hand like this:
49  *
50  * #define LITTLE_ENDIAN 1234
51  * #define BIG_ENDIAN 4321
52  *
53  * And for little-endian machines, add:
54  *
55  * #define BYTE_ORDER LITTLE_ENDIAN
56  *
57  * Or for big-endian machines:
58  *
59  * #define BYTE_ORDER BIG_ENDIAN
60  *
61  * The FreeBSD machine this was written on defines BYTE_ORDER
62  * appropriately by including <sys/types.h> (which in turn includes
63  * <machine/endian.h> where the appropriate definitions are actually
64  * made).
65  */
66 
67 #ifndef LITTLE_ENDIAN
68 #define LITTLE_ENDIAN 1234
69 #define BIG_ENDIAN 4321
70 #endif
71 
72 #ifndef BYTE_ORDER
73 #define BYTE_ORDER LITTLE_ENDIAN
74 #endif
75 
76 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
77 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
78 #endif
79 
80 /*** SHA-256/384/512 Various Length Definitions ***********************/
81 /* NOTE: Most of these are in sha256.h */
82 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
83 
84 
85 /*** ENDIAN REVERSAL MACROS *******************************************/
86 #if BYTE_ORDER == LITTLE_ENDIAN
87 #define REVERSE32(w,x) { \
88  uint32_t tmp = (w); \
89  tmp = (tmp >> 16) | (tmp << 16); \
90  (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
91 }
92 #define REVERSE64(w,x) { \
93  uint64_t tmp = (w); \
94  tmp = (tmp >> 32) | (tmp << 32); \
95  tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \
96  ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
97  (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \
98  ((tmp & 0x0000ffff0000ffffULL) << 16); \
99 }
100 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
101 
102 /*
103  * Macro for incrementally adding the unsigned 64-bit integer n to the
104  * unsigned 128-bit integer (represented using a two-element array of
105  * 64-bit words):
106  */
107 #define ADDINC128(w,n) { \
108  (w)[0] += (uint64_t)(n); \
109  if ((w)[0] < (n)) { \
110  (w)[1]++; \
111  } \
112 }
113 
114 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
115 /*
116  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
117  *
118  * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
119  * S is a ROTATION) because the SHA-256/384/512 description document
120  * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
121  * same "backwards" definition.
122  */
123 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
124 #define R(b,x) ((x) >> (b))
125 /* 32-bit Rotate-right (used in SHA-256): */
126 #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
127 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
128 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
129 
130 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
131 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
132 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
133 
134 /* Four of six logical functions used in SHA-256: */
135 #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
136 #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
137 #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
138 #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
139 
140 /* Four of six logical functions used in SHA-384 and SHA-512: */
141 #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
142 #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
143 #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
144 #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
145 
146 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
147 /* NOTE: These should not be accessed directly from outside this
148  * library -- they are intended for private internal visibility/use
149  * only.
150  */
151 void sha256_transform(sha256_ctx_t*, uint8_t*);
152 
153 
154 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
155 /* Hash constant words K for SHA-256: */
156 static const uint32_t K256[64] = {
157  0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
158  0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
159  0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
160  0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
161  0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
162  0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
163  0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
164  0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
165  0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
166  0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
167  0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
168  0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
169  0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
170  0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
171  0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
172  0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
173 };
174 
175 /* Initial hash value H for SHA-256: */
176 static const uint32_t sha256_initial_hash_value[8] = {
177  0x6a09e667UL,
178  0xbb67ae85UL,
179  0x3c6ef372UL,
180  0xa54ff53aUL,
181  0x510e527fUL,
182  0x9b05688cUL,
183  0x1f83d9abUL,
184  0x5be0cd19UL
185 };
186 
187 /*
188  * Constant used by SHA256/384/512_End() functions for converting the
189  * digest to a readable hexadecimal character string:
190  */
191 static const char *sha2_hex_digits = "0123456789abcdef";
192 
193 
194 /*** SHA-256: *********************************************************/
196 void sha256_init(sha256_ctx_t *context) {
197  if (context == (sha256_ctx_t*)0) {
198  return;
199  }
201  memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
202  context->bitcount = 0;
203 }
204 
206 void sha256_transform(sha256_ctx_t* context, uint8_t *data) {
207  uint32_t a, b, c, d, e, f, g, h, s0, s1;
208  uint32_t T1, T2, *W256;
209  int j;
210 
211  W256 = (uint32_t*)(void*)context->buffer;
212 
213  /* Initialize registers with the prev. intermediate value */
214  a = context->state[0];
215  b = context->state[1];
216  c = context->state[2];
217  d = context->state[3];
218  e = context->state[4];
219  f = context->state[5];
220  g = context->state[6];
221  h = context->state[7];
222 
223  j = 0;
224  do {
225 #if BYTE_ORDER == LITTLE_ENDIAN
226  /* Copy data while converting to host byte order */
227  uint32_t u32;
228  u32 = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
229  W256[j] = u32;
230  // REVERSE32(u32, W256[j]);
231  data += sizeof(uint32_t);
232  /* Apply the SHA-256 compression function to update a..h */
233  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
234 #else /* BYTE_ORDER == LITTLE_ENDIAN */
235  /* Apply the SHA-256 compression function to update a..h with copy */
236  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
237 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
238  T2 = Sigma0_256(a) + Maj(a, b, c);
239  h = g;
240  g = f;
241  f = e;
242  e = d + T1;
243  d = c;
244  c = b;
245  b = a;
246  a = T1 + T2;
247  j++;
248  } while (j < 16);
249 
250  do {
251  /* Part of the message block expansion: */
252  s0 = W256[(j+1)&0x0f];
253  s0 = sigma0_256(s0);
254  s1 = W256[(j+14)&0x0f];
255  s1 = sigma1_256(s1);
256 
257  /* Apply the SHA-256 compression function to update a..h */
258  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
259  (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
260  T2 = Sigma0_256(a) + Maj(a, b, c);
261  h = g;
262  g = f;
263  f = e;
264  e = d + T1;
265  d = c;
266  c = b;
267  b = a;
268  a = T1 + T2;
269 
270  j++;
271  } while (j < 64);
272 
273  /* Compute the current intermediate hash value */
274  context->state[0] += a;
275  context->state[1] += b;
276  context->state[2] += c;
277  context->state[3] += d;
278  context->state[4] += e;
279  context->state[5] += f;
280  context->state[6] += g;
281  context->state[7] += h;
282 
283  /* Clean up */
284  a = b = c = d = e = f = g = h = T1 = T2 = 0;
285 }
286 
288 void sha256_update(sha256_ctx_t *context, uint8_t *data, size_t len) {
289  unsigned int freespace, usedspace;
290 
291  if (len == 0) {
292  // Calling with no data is valid - we do nothing
293  return;
294  }
295 
296  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
297  if (usedspace > 0) {
298  // Calculate how much free space is available in the buffer
299  freespace = SHA256_BLOCK_LENGTH - usedspace;
300 
301  if (len >= freespace) {
302  // Fill the buffer completely and process it
303  memcpy(&context->buffer[usedspace], data, freespace);
304  context->bitcount += freespace << 3;
305  len -= freespace;
306  data += freespace;
307  sha256_transform(context, context->buffer);
308  } else {
309  // The buffer is not yet full
310  memcpy(&context->buffer[usedspace], data, len);
311  context->bitcount += len << 3;
312  // Clean up:
313  usedspace = freespace = 0;
314  return;
315  }
316  }
317  while (len >= SHA256_BLOCK_LENGTH) {
318  // Process as many complete blocks as we can
319  sha256_transform(context, data);
320  context->bitcount += SHA256_BLOCK_LENGTH << 3;
321  len -= SHA256_BLOCK_LENGTH;
322  data += SHA256_BLOCK_LENGTH;
323  }
324  if (len > 0) {
325  // There's left-overs, so save 'em
326  memcpy(context->buffer, data, len);
327  context->bitcount += len << 3;
328  }
329  // Clean up:
330  usedspace = freespace = 0;
331 }
332 
334 void sha256_final(sha256_ctx_t *context, uint8_t digest[SHA256_DIGEST_LENGTH]) {
335  unsigned int usedspace;
336 
337  /* If no digest buffer is passed, we don't bother doing this: */
338  if (digest != (uint8_t*)0) {
339  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
340 #if BYTE_ORDER == LITTLE_ENDIAN
341  /* Convert FROM host byte order */
342  REVERSE64(context->bitcount,context->bitcount);
343 #endif
344  if (usedspace > 0) {
345  /* Begin padding with a 1 bit: */
346  context->buffer[usedspace++] = 0x80;
347 
348  if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
349  /* Set-up for the last transform: */
350  memset(&context->buffer[usedspace], 0, SHA256_SHORT_BLOCK_LENGTH - usedspace);
351  } else {
352  if (usedspace < SHA256_BLOCK_LENGTH) {
353  memset(&context->buffer[usedspace], 0, SHA256_BLOCK_LENGTH - usedspace);
354  }
355  /* Do second-to-last transform: */
356  sha256_transform(context, context->buffer);
357 
358  /* And set-up for the last transform: */
360  }
361  } else {
362  /* Set-up for the last transform: */
364 
365  /* Begin padding with a 1 bit: */
366  *context->buffer = 0x80;
367  }
368  /* Set the bit count: */
369  uint64_t *t = (uint64_t *)(void*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH];
370  *t = context->bitcount;
371 
372  /* Final transform: */
373  sha256_transform(context, context->buffer);
374 
375 #if BYTE_ORDER == LITTLE_ENDIAN
376  {
377  /* Convert TO host byte order */
378  int j;
379  for (j = 0; j < 8; j++) {
380  REVERSE32(context->state[j],context->state[j]);
381  digest[0] = ((context->state[j] >> 0) & 0xff);
382  digest[1] = ((context->state[j] >> 8) & 0xff);
383  digest[2] = ((context->state[j] >> 16) & 0xff);
384  digest[3] = ((context->state[j] >> 24) & 0xff);
385  digest += sizeof(uint32_t);
386  }
387  }
388 #else
389  memcpy(d, context->state, SHA256_DIGEST_LENGTH);
390 #endif
391  }
392 
393  /* Clean up state data: */
394  memset(context, 0, sizeof(sha256_ctx_t));
395  usedspace = 0;
396 }
397 
399 char *sha256_end(sha256_ctx_t *context, char buffer[SHA256_DIGEST_STRING_LENGTH]) {
400  uint8_t digest[SHA256_DIGEST_LENGTH];
401  uint8_t *d = digest;
402  int i;
403 
404  if (buffer != (char*)0) {
405  sha256_final(context, digest);
406 
407  for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
408  *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
409  *buffer++ = sha2_hex_digits[*d & 0x0f];
410  d++;
411  }
412  *buffer = (char)0;
413  } else {
414  memset(context, 0, sizeof(sha256_ctx_t));
415  }
416  memset(digest, 0, SHA256_DIGEST_LENGTH);
417  return buffer;
418 }
419 
421 void sha256_raw(uint8_t* data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH]) {
422  sha256_ctx_t context;
423 
424  sha256_init(&context);
425  sha256_update(&context, data, len);
426  sha256_final(&context, digest);
427 }
428 
430 char* sha256_data(uint8_t* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
431  sha256_ctx_t context;
432 
433  sha256_init(&context);
434  sha256_update(&context, data, len);
435  return sha256_end(&context, digest);
436 }
437 
#define sigma1_256(x)
Definition: sha256.c:138
#define sigma0_256(x)
Definition: sha256.c:137
#define memset(x, a, b)
Definition: platform.h:21
#define REVERSE32(w, x)
Definition: sha256.c:87
#define SHA256_DIGEST_STRING_LENGTH
Definition: sha256.h:40
#define Ch(x, y, z)
Definition: sha256.c:131
#define SHA256_SHORT_BLOCK_LENGTH
Definition: sha256.c:82
#define SHA256_BLOCK_LENGTH
Definition: sha256.h:38
uint32_t state[8]
Definition: sha256.h:43
ICACHE_FLASH_ATTR void sha256_raw(uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha256.c:421
uint64_t bitcount
Definition: sha256.h:44
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
unsigned int u32
Definition: c_types.h:56
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha256.h:45
ICACHE_FLASH_ATTR char * sha256_end(sha256_ctx_t *context, char buffer[SHA256_DIGEST_STRING_LENGTH])
Definition: sha256.c:399
#define Sigma1_256(x)
Definition: sha256.c:136
static const uint32_t sha256_initial_hash_value[8]
Definition: sha256.c:176
ICACHE_FLASH_ATTR void sha256_final(sha256_ctx_t *context, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha256.c:334
#define Sigma0_256(x)
Definition: sha256.c:135
ICACHE_FLASH_ATTR void sha256_init(sha256_ctx_t *context)
Definition: sha256.c:196
#define Maj(x, y, z)
Definition: sha256.c:132
static const char * sha2_hex_digits
Definition: sha256.c:191
#define memcpy(x, a, b)
Definition: platform.h:22
void sha256_transform(sha256_ctx_t *, uint8_t *)
Definition: sha256.c:206
#define REVERSE64(w, x)
Definition: sha256.c:92
ICACHE_FLASH_ATTR char * sha256_data(uint8_t *data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH])
Definition: sha256.c:430
#define SHA256_DIGEST_LENGTH
Definition: sha256.h:39
ICACHE_FLASH_ATTR void sha256_update(sha256_ctx_t *context, uint8_t *data, size_t len)
Definition: sha256.c:288
static const uint32_t K256[64]
Definition: sha256.c:156