SHA-256 hash library

nRF5 SDK v12.1.0

This module calculates SHA-256 (SHA-2, FIPS-180) hashes. More...

Data Structures

struct sha256_context_t
Current state of a hash operation. More...

Functions

ret_code_t sha256_init ( sha256_context_t *ctx)
Function for initializing a sha256_context_t instance. More...
ret_code_t sha256_update ( sha256_context_t *ctx, const uint8_t *data, const size_t len)
Function for calculating the hash of an array of uint8_t data. More...
ret_code_t sha256_final ( sha256_context_t *ctx, uint8_t *hash, uint8_t le)
Function for extracting the hash value from a hash instance. More...

Detailed Description

This module calculates SHA-256 (SHA-2, FIPS-180) hashes.

To use this module, first call sha256_init on a sha256_context_t instance. Then call sha256_update with the data to be hashed. This step can optionally be done with multiple calls to sha256_update , each with a section of the data (in the correct order). After all data has been passed to sha256_update , call sha256_final to finalize and extract the hash value.

This code is adapted from code by Brad Conte, retrieved from https://github.com/B-Con/crypto-algorithms .

Function Documentation

ret_code_t sha256_final ( sha256_context_t * ctx ,
uint8_t * hash ,
uint8_t le
)

Function for extracting the hash value from a hash instance.

This function should be called after all data to be hashed has been passed to the hash instance (by one or more calls to sha256_update ).

Do not call sha256_update again after sha256_final has been called.

Parameters
[in,out] ctx Hash instance.
[out] hash Array to hold the extracted hash value (assumed to be 32 bytes long).
[in] le Store the hash in little-endian.
Return values
NRF_SUCCESS If the has value was successfully extracted.
NRF_ERROR_NULL If a parameter was NULL.
ret_code_t sha256_init ( sha256_context_t * ctx )

Function for initializing a sha256_context_t instance.

Parameters
[out] ctx Context instance to be initialized.
Return values
NRF_SUCCESS If the instance was successfully initialized.
NRF_ERROR_NULL If the parameter was NULL.
ret_code_t sha256_update ( sha256_context_t * ctx ,
const uint8_t * data ,
const size_t len
)

Function for calculating the hash of an array of uint8_t data.

This function can be called multiple times in sequence. This is equivalent to calling the function once on a concatenation of the data from the different calls.

Parameters
[in,out] ctx Hash instance.
[in] data Data to be hashed.
[in] len Length of the data to be hashed.
Return values
NRF_SUCCESS If the data was successfully hashed.
NRF_ERROR_NULL If the ctx parameter was NULL or the data parameter was NULL, while the len parameter was not zero.