The friendly Operating System for the Internet of Things
sha256.h File Reference

Header definitions for the SHA256 hash function. More...

Detailed Description

Author
Colin Percival
Christian Mehlis
Rene Kijewski
Hermann Lelong

Definition in file sha256.h.

#include <inttypes.h>
+ Include dependency graph for sha256.h:

Go to the source code of this file.

Data Structures

struct  sha256_context_t
 Context for ciper operations based on sha256. More...
 
struct  hmac_context_t
 Context for HMAC operations based on sha256. More...
 
struct  sha256_chain_idx_elm_t
 sha256-chain indexed element More...
 
#define SHA256_DIGEST_LENGTH   32
 
#define SHA256_INTERNAL_BLOCK_SIZE   (64)
 512 Bit (64 Byte) internally used block size for sha256
 
void sha256_init (sha256_context_t *ctx)
 SHA-256 initialization. More...
 
void sha256_update (sha256_context_t *ctx, const void *data, size_t len)
 Add bytes into the hash. More...
 
void sha256_final (sha256_context_t *ctx, void *digest)
 SHA-256 finalization. More...
 
void * sha256 (const void *data, size_t len, void *digest)
 A wrapper function to simplify the generation of a hash, this is usefull for generating sha256 for one buffer. More...
 
void hmac_sha256_init (hmac_context_t *ctx, const void *key, size_t key_length)
 hmac_sha256_init HMAC SHA-256 calculation. More...
 
void hmac_sha256_update (hmac_context_t *ctx, const void *data, size_t len)
 hmac_sha256_update Add data bytes for HMAC calculation More...
 
void hmac_sha256_final (hmac_context_t *ctx, void *digest)
 hmac_sha256_final HMAC SHA-256 finalization. More...
 
const void * hmac_sha256 (const void *key, size_t key_length, const void *data, size_t len, void *digest)
 function to compute a hmac-sha256 from a given message More...
 
void * sha256_chain (const void *seed, size_t seed_length, size_t elements, void *tail_element)
 function to produce a hash chain statring with a given seed element. More...
 
void * sha256_chain_with_waypoints (const void *seed, size_t seed_length, size_t elements, void *tail_element, sha256_chain_idx_elm_t *waypoints, size_t *waypoints_length)
 function to produce a hash chain statring with a given seed element. More...
 
int sha256_chain_verify_element (void *element, size_t element_index, void *tail_element, size_t chain_length)
 function to verify if a given chain element is part of the chain. More...
 

Function Documentation

const void* hmac_sha256 ( const void *  key,
size_t  key_length,
const void *  data,
size_t  len,
void *  digest 
)
Parameters
[in]keykey used in the hmac-sha256 computation
[in]key_lengththe size in bytes of the key
[in]datapointer to the buffer to generate the hmac-sha256
[in]lenthe length of the message in bytes
[out]digestthe computed hmac-sha256, length MUST be SHA256_DIGEST_LENGTH if digest == NULL, a static buffer is used
Returns
pointer to the resulting digest. if result == NULL the pointer points to the static buffer
void hmac_sha256_final ( hmac_context_t ctx,
void *  digest 
)

Finish HMAC calculation and export the value

Parameters
[in]ctxhmac_context_t handle to use
[out]digestthe computed hmac-sha256, length MUST be SHA256_DIGEST_LENGTH if digest == NULL, a static buffer is used
void hmac_sha256_init ( hmac_context_t ctx,
const void *  key,
size_t  key_length 
)

Initiate calculation of a HMAC

Parameters
[in]ctxhmac_context_t handle to use
[in]keykey used in the hmac-sha256 computation
[in]key_lengththe size in bytes of the key
void hmac_sha256_update ( hmac_context_t ctx,
const void *  data,
size_t  len 
)
Parameters
[in]ctxhmac_context_t handle to use
[in]datapointer to the buffer to generate hash from
[in]lenlength of the buffer
void* sha256 ( const void *  data,
size_t  len,
void *  digest 
)
Parameters
[in]datapointer to the buffer to generate hash from
[in]lenlength of the buffer
[out]digestoptional pointer to an array for the result, length must be SHA256_DIGEST_LENGTH if digest == NULL, one static buffer is used
void* sha256_chain ( const void *  seed,
size_t  seed_length,
size_t  elements,
void *  tail_element 
)

The chain is computed by taking the sha256 from the seed, hash the resulting sha256 and continuing taking sha256 from each result consecutively.

Parameters
[in]seedthe seed of the sha256-chain, i.e. the first element
[in]seed_lengththe size of seed in bytes
[in]elementsthe number of chained elements, i.e. the index of the last element is (elements-1)
[out]tail_elementthe final element of the sha256-chain
Returns
pointer to tail_element
int sha256_chain_verify_element ( void *  element,
size_t  element_index,
void *  tail_element,
size_t  chain_length 
)
Parameters
[in]elementthe chain element to be verified
[in]element_indexthe position in the chain
[in]tail_elementthe last element of the sha256-chain
[in]chain_lengththe number of elements in the chain
Returns
0 if element is verified to be part of the chain at element_index 1 if the element cannot be verified as part of the chain
void* sha256_chain_with_waypoints ( const void *  seed,
size_t  seed_length,
size_t  elements,
void *  tail_element,
sha256_chain_idx_elm_t waypoints,
size_t *  waypoints_length 
)

The chain is computed the same way as done with sha256_chain(). Additionally intermediate elements are saved while computing the chain. This slows down computation, but provides the caller with indexed "waypoint"-elements. They are supposed to shortcut computing verification elements, this comes in handy when using long chains, e.g. a chain with 232 elements.

Parameters
[in]seedthe seed of the sha256-chain, i.e. the first element
[in]seed_lengththe size of seed in bytes
[in]elementsthe number of chained elements, i.e. the index of the last element is (elements-1)
[out]tail_elementthe final element of the sha256-chain
[out]waypointsintermediate elements are stored there.
[in,out]waypoints_lengththe size of the waypoints array. If the given size is equal or greater elements, the complete chain will be stored. Otherwise every n-th element is stored where: n = floor(elements / waypoints_length); floor is implicitly used since we perform unsigned integer division. The last used waypoint index is stored in the variable after call. That is (elements - 1) if the complete chain is stored, and (*waypoints_length - 1) if we only store some waypoints.
Returns
pointer to tail_element
void sha256_final ( sha256_context_t ctx,
void *  digest 
)

Pads the input data, exports the hash value, and clears the context state.

Parameters
ctxsha256_context_t handle to use
digestresulting digest, this is the hash of all the bytes
void sha256_init ( sha256_context_t ctx)

Begins a SHA-256 operation.

Parameters
ctxsha256_context_t handle to init
void sha256_update ( sha256_context_t ctx,
const void *  data,
size_t  len 
)
Parameters
ctxsha256_context_t handle to use
[in]dataInput data
[in]lenLength of data