sha256.h File Reference

Interface to a SHA-256 implementation. More...

#include <stddef.h>
#include <stdint.h>

Macros

#define SHA256_BLOCK_SIZE   (64)
 Block size.
 
#define SHA256_DIGEST_SIZE   (32)
 Digest size.
 
#define SHA256_STATE_BLOCKS   (SHA256_DIGEST_SIZE / 4)
 Number of blocks in state.
 

Functions

void tc_sha256_init (sha256_state *s)
 The SHA state. More...
 
void tc_sha256_update (sha256_state *s, const uint8_t *data, size_t datalen)
 SHA256 update procedure Hashes data_length bytes addressed by data into state s. More...
 
void tc_sha256_final (uint8_t *digest, sha256_state *s)
 SHA256 final procedure Inserts the completed hash computation into digest. More...
 

Detailed Description

Interface to a SHA-256 implementation.

Overview: SHA-256 is a NIST approved cryptographic hashing algorithm specified in FIPS 180. A hash algorithm maps data of arbitrary size to data of fixed length.

Security: SHA-256 provides 128 bits of security against collision attacks and 256 bits of security against pre-image attacks. SHA-256 does NOT behave like a random oracle, but it can be used as one if the string being hashed is prefix-free encoded before hashing.

Usage: 1) call tc_sha256_init to initialize a struct tc_sha256_state_struct before hashing a new string.

2) call tc_sha256_update to hash the next string segment; tc_sha256_update can be called as many times as needed to hash all of the segments of a string; the order is important.

3) call tc_sha256_final to out put the digest from a hashing operation.

Function Documentation

◆ tc_sha256_final()

void tc_sha256_final ( uint8_t *  digest,
sha256_state *  s 
)

SHA256 final procedure Inserts the completed hash computation into digest.

Returns
returns 1 returns 0 if: s == NULL, s->iv == NULL, digest == NULL
Note
Assumes: s has been initialized by tc_sha256_init digest points to at least SHA256_DIGEST_SIZE bytes
Warning
The state buffer 'leftover' is left in memory after processing If your application intends to have sensitive data in this buffer, remind to erase it after the data has been processed
Parameters
digestunsigned eight bit integer
sSha256 state struct

◆ tc_sha256_init()

void tc_sha256_init ( sha256_state *  s)

The SHA state.

SHA256 initialization procedure Initializes s

Parameters
sSha256 state struct

◆ tc_sha256_update()

void tc_sha256_update ( sha256_state *  s,
const uint8_t *  data,
size_t  datalen 
)

SHA256 update procedure Hashes data_length bytes addressed by data into state s.

Note
Assumes s has been initialized by tc_sha256_init
Warning
The state buffer 'leftover' is left in memory after processing If your application intends to have sensitive data in this buffer, remind to erase it after the data has been processed
Parameters
sSha256 state struct
datamessage to hash
datalenlength of message to hash