nRF5 SDK v12.1.0
Data Structures | Functions
SHA-256 hash library

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]ctxHash instance.
[out]hashArray to hold the extracted hash value (assumed to be 32 bytes long).
[in]leStore the hash in little-endian.
Return values
NRF_SUCCESSIf the has value was successfully extracted.
NRF_ERROR_NULLIf a parameter was NULL.
ret_code_t sha256_init ( sha256_context_t ctx)

Function for initializing a sha256_context_t instance.

Parameters
[out]ctxContext instance to be initialized.
Return values
NRF_SUCCESSIf the instance was successfully initialized.
NRF_ERROR_NULLIf 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]ctxHash instance.
[in]dataData to be hashed.
[in]lenLength of the data to be hashed.
Return values
NRF_SUCCESSIf the data was successfully hashed.
NRF_ERROR_NULLIf the ctx parameter was NULL or the data parameter was NULL, while the len parameter was not zero.

Documentation feedback | Developer Zone | Subscribe | Updated