MeterLogger
Data Structures | Macros | Functions
sha256.h File Reference
#include <esp8266.h>
#include <stdint.h>
#include "crypto/crypto.h"
Include dependency graph for sha256.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  sha256_ctx_t
 

Macros

#define SHA256_BLOCK_LENGTH   64
 
#define SHA256_DIGEST_LENGTH   32
 
#define SHA256_DIGEST_STRING_LENGTH   (SHA256_DIGEST_LENGTH * 2 + 1)
 

Functions

ICACHE_FLASH_ATTR void sha256_init (sha256_ctx_t *context)
 
ICACHE_FLASH_ATTR void sha256_update (sha256_ctx_t *context, uint8_t *data, size_t len)
 
ICACHE_FLASH_ATTR void sha256_final (sha256_ctx_t *context, uint8_t digest[SHA256_DIGEST_LENGTH])
 
ICACHE_FLASH_ATTR char * sha256_end (sha256_ctx_t *context, char buffer[SHA256_DIGEST_STRING_LENGTH])
 
ICACHE_FLASH_ATTR void sha256_raw (uint8_t *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
 
ICACHE_FLASH_ATTR char * sha256_data (uint8_t *data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH])
 

Macro Definition Documentation

◆ SHA256_BLOCK_LENGTH

#define SHA256_BLOCK_LENGTH   64

Copyright (c) 2000-2001 Aaron D. Gifford Copyright (c) 2013-2014 Pavol Rusnak All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition at line 38 of file sha256.h.

Referenced by hmac_sha256_final(), hmac_sha256_init(), sha256_final(), sha256_init(), and sha256_update().

◆ SHA256_DIGEST_LENGTH

#define SHA256_DIGEST_LENGTH   32

◆ SHA256_DIGEST_STRING_LENGTH

#define SHA256_DIGEST_STRING_LENGTH   (SHA256_DIGEST_LENGTH * 2 + 1)

Definition at line 40 of file sha256.h.

Function Documentation

◆ sha256_data()

ICACHE_FLASH_ATTR char* sha256_data ( uint8_t *  data,
size_t  len,
char  digest[SHA256_DIGEST_STRING_LENGTH] 
)

Definition at line 430 of file sha256.c.

References sha256_end(), sha256_init(), and sha256_update().

430  {
431  sha256_ctx_t context;
432 
433  sha256_init(&context);
434  sha256_update(&context, data, len);
435  return sha256_end(&context, digest);
436 }
ICACHE_FLASH_ATTR char * sha256_end(sha256_ctx_t *context, char buffer[SHA256_DIGEST_STRING_LENGTH])
Definition: sha256.c:399
ICACHE_FLASH_ATTR void sha256_init(sha256_ctx_t *context)
Definition: sha256.c:196
ICACHE_FLASH_ATTR void sha256_update(sha256_ctx_t *context, uint8_t *data, size_t len)
Definition: sha256.c:288
Here is the call graph for this function:

◆ sha256_end()

ICACHE_FLASH_ATTR char* sha256_end ( sha256_ctx_t context,
char  buffer[SHA256_DIGEST_STRING_LENGTH] 
)

Definition at line 399 of file sha256.c.

References ICACHE_FLASH_ATTR, memset, SHA256_DIGEST_LENGTH, sha256_final(), and sha2_hex_digits.

Referenced by sha256_data().

399  {
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 }
#define memset(x, a, b)
Definition: platform.h:21
ICACHE_FLASH_ATTR void sha256_final(sha256_ctx_t *context, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha256.c:334
static const char * sha2_hex_digits
Definition: sha256.c:191
#define SHA256_DIGEST_LENGTH
Definition: sha256.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sha256_final()

ICACHE_FLASH_ATTR void sha256_final ( sha256_ctx_t context,
uint8_t  digest[SHA256_DIGEST_LENGTH] 
)

Definition at line 334 of file sha256.c.

References sha256_ctx_t::bitcount, sha256_ctx_t::buffer, ICACHE_FLASH_ATTR, memcpy, memset, REVERSE32, REVERSE64, SHA256_BLOCK_LENGTH, SHA256_SHORT_BLOCK_LENGTH, sha256_transform(), and sha256_ctx_t::state.

Referenced by hmac_sha256_final(), sha256_end(), and sha256_raw().

334  {
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 }
#define memset(x, a, b)
Definition: platform.h:21
#define REVERSE32(w, x)
Definition: sha256.c:87
#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
uint64_t bitcount
Definition: sha256.h:44
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha256.h:45
#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
#define SHA256_DIGEST_LENGTH
Definition: sha256.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sha256_init()

ICACHE_FLASH_ATTR void sha256_init ( sha256_ctx_t context)

Definition at line 196 of file sha256.c.

References sha256_ctx_t::bitcount, sha256_ctx_t::buffer, ICACHE_FLASH_ATTR, memcpy, memset, SHA256_BLOCK_LENGTH, SHA256_DIGEST_LENGTH, sha256_initial_hash_value, and sha256_ctx_t::state.

Referenced by hmac_sha256_final(), hmac_sha256_init(), sha256_data(), and sha256_raw().

196  {
197  if (context == (sha256_ctx_t*)0) {
198  return;
199  }
201  memset(context->buffer, 0, SHA256_BLOCK_LENGTH);
202  context->bitcount = 0;
203 }
#define memset(x, a, b)
Definition: platform.h:21
#define SHA256_BLOCK_LENGTH
Definition: sha256.h:38
uint32_t state[8]
Definition: sha256.h:43
uint64_t bitcount
Definition: sha256.h:44
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha256.h:45
static const uint32_t sha256_initial_hash_value[8]
Definition: sha256.c:176
#define memcpy(x, a, b)
Definition: platform.h:22
#define SHA256_DIGEST_LENGTH
Definition: sha256.h:39
Here is the caller graph for this function:

◆ sha256_raw()

ICACHE_FLASH_ATTR void sha256_raw ( uint8_t *  data,
size_t  len,
uint8_t  digest[SHA256_DIGEST_LENGTH] 
)

Definition at line 421 of file sha256.c.

References ICACHE_FLASH_ATTR, sha256_final(), sha256_init(), and sha256_update().

Referenced by hmac_sha256_init(), and init_aes_hmac_combined().

421  {
422  sha256_ctx_t context;
423 
424  sha256_init(&context);
425  sha256_update(&context, data, len);
426  sha256_final(&context, digest);
427 }
ICACHE_FLASH_ATTR void sha256_final(sha256_ctx_t *context, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha256.c:334
ICACHE_FLASH_ATTR void sha256_init(sha256_ctx_t *context)
Definition: sha256.c:196
ICACHE_FLASH_ATTR void sha256_update(sha256_ctx_t *context, uint8_t *data, size_t len)
Definition: sha256.c:288
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sha256_update()

ICACHE_FLASH_ATTR void sha256_update ( sha256_ctx_t context,
uint8_t *  data,
size_t  len 
)

Definition at line 288 of file sha256.c.

References sha256_ctx_t::bitcount, sha256_ctx_t::buffer, ICACHE_FLASH_ATTR, memcpy, SHA256_BLOCK_LENGTH, and sha256_transform().

Referenced by hmac_sha256_final(), hmac_sha256_init(), hmac_sha256_update(), sha256_data(), and sha256_raw().

288  {
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 }
#define SHA256_BLOCK_LENGTH
Definition: sha256.h:38
uint64_t bitcount
Definition: sha256.h:44
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha256.h:45
#define memcpy(x, a, b)
Definition: platform.h:22
void sha256_transform(sha256_ctx_t *, uint8_t *)
Definition: sha256.c:206
Here is the call graph for this function:
Here is the caller graph for this function: