SHA2 driver header.
============================================================================
SHA2 (Secure Hash Algorithm 2) is a cryptographic hashing algorithm that maps an input of arbitrary length to a fixed-length output with negligible probability of collision. A collision would occur when two different inputs map to the same output.
It is not currently technologicaly feasible to derive an input from the hash digest (output) itself.
Hashes are often used to ensure the integrity of messages. They are also used to as constituent parts of more complicated cyptographic schemes. HMAC is a message authentication code that is based on hash functions such as SHA2 rather than a block cipher. Hashes may themselves be used as or form a part of key derivation functions used to derive symmetric keys from sources of entropy such as an Elliptic Curve Diffie-Helman key exchange (ECDH).
SHA2 is not actually a single algorithms but a suite of similar algorithms that produce hash digests of different lengths. 224, 256, 384, and 512-bit outputs are available.
"Hash" may refer to either the process of hashing when used as a verb and the output digest when used as a noun.
Before starting a SHA2 operation, the application must do the following:
There are two general ways to execute a SHA2 operation. In one call or multiple.
The SHA2_oneStepHash function performs a SHA2 operation in a single call. It will always be the most highly optimized routine with the least overhead and the fastest runtime. However, it requires that the entire input message be available to the function in a contiguous location at the start of the call. When trying to operate on data that is too large to fit into available memory, partial processing is more advisable. The single call operation is required when hashing a message with a lenth smaller than or equal to one hash-block length. All devices support single call operations.
After the SHA2 operation completes, the application should either start another operation or close the driver by calling SHA2_close().
In order to use the SHA2 APIs, the application is required to provide device-specific SHA2 configuration in the Board.c file. The SHA2 driver interface defines a configuration data structure:
The application must declare an array of SHA2_Config elements, named SHA2_config[]. Each element of SHA2_config[] must be populated with pointers to a device specific SHA2 driver implementation's driver object and hardware attributes. The hardware attributes define properties such as the SHA2 peripheral's base address. Each element in SHA2_config[] corresponds to an SHA2 instance and none of the elements should have NULL pointers. There is no correlation between the index and the peripheral designation (such as SHA20 or SHA21). For example, it is possible to use SHA2_config[0] for SHA21. Multiple drivers and driver instances may all access the same underlying hardware. This is transparent to the application. Mutual exclusion is performed automatically by the drivers as necessary.
Because the SHA2 configuration is very device dependent, you will need to check the doxygen for the device specific SHA2 implementation. There, you will find a description of the SHA2 hardware attributes. Please also refer to the Board.c file of any of your examples to see the SHA2 configuration.
The SHA2_Params structure is passed to the SHA2_open() call. If NULL is passed for the parameters, SHA2_open() uses default parameters. A SHA2_Params structure is initialized with default values by passing it to SHA2_Params_init(). Some of the SHA2 parameters are described below. To see brief descriptions of all the parameters, see SHA2_Params.
### Single call SHA2 256-bit hash in blocking mode #
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ti/drivers/cryptoutils/cryptokey/CryptoKey.h>
Go to the source code of this file.
Data Structures | |
struct | SHA2_OperationOneStep_ |
Struct containing the parameters required to hash a message in one go. More... | |
struct | SHA2_OperationStartHash_ |
Struct containing the parameters required to start hashing a message. More... | |
struct | SHA2_OperationProcessHash_ |
Struct containing the parameters required to continue hashing a message. More... | |
struct | SHA2_OperationFinishlHash_ |
Struct containing the parameters required to process the last blocks of a message and finalize the hash. More... | |
union | SHA2_Operation_ |
Union containing pointers to all supported operation structs. More... | |
struct | SHA2_Config_ |
SHA2 Global configuration. More... | |
struct | SHA2_Params_ |
SHA2 Parameters. More... | |
Macros | |
#define | SHA2_CMD_RESERVED (32) |
#define | SHA2_STATUS_RESERVED (-32) |
#define | SHA2_STATUS_SUCCESS (0) |
Successful status code. More... | |
#define | SHA2_STATUS_ERROR (-1) |
Generic error status code. More... | |
#define | SHA2_STATUS_UNDEFINEDCMD (-2) |
An error status code returned by SHA2_control() for undefined command codes. More... | |
#define | SHA2_STATUS_RESOURCE_UNAVAILABLE (-3) |
An error status code returned if the hardware or software resource is currently unavailable. More... | |
Typedefs | |
typedef struct SHA2_Config_ * | SHA2_Handle |
A handle that is returned from an SHA2_open() call. More... | |
typedef enum SHA2_ReturnBehavior_ | SHA2_ReturnBehavior |
The way in which SHA2 function calls return after performing an operation. More... | |
typedef enum SHA2_HashSize_ | SHA2_HashSize |
Enum for the hash digest sizes supported by the driver. More... | |
typedef enum SHA2_DigestLengthBytes_ | SHA2_DigestLengthBytes |
Enum for the hash digest lengths in bytes supported by the driver. More... | |
typedef enum SHA2_BlockSizeBytes_ | SHA2_BlockSizeBytes |
Enum for the block sizes of the algorithms. More... | |
typedef struct SHA2_OperationOneStep_ | SHA2_OperationOneStepHash |
Struct containing the parameters required to hash a message in one go. More... | |
typedef struct SHA2_OperationStartHash_ | SHA2_OperationStartHash |
Struct containing the parameters required to start hashing a message. More... | |
typedef struct SHA2_OperationProcessHash_ | SHA2_OperationProcessHash |
Struct containing the parameters required to continue hashing a message. More... | |
typedef struct SHA2_OperationFinishlHash_ | SHA2_OperationFinishHash |
Struct containing the parameters required to process the last blocks of a message and finalize the hash. More... | |
typedef union SHA2_Operation_ | SHA2_Operation |
Union containing pointers to all supported operation structs. More... | |
typedef enum SHA2_OperationType_ | SHA2_OperationType |
Enum for the operation types supported by the driver. More... | |
typedef struct SHA2_Config_ | SHA2_Config |
SHA2 Global configuration. More... | |
typedef void(* | SHA2_CallbackFxn) (SHA2_Handle handle, int_fast16_t returnStatus, SHA2_Operation operation, SHA2_OperationType operationType) |
The definition of a callback function used by the SHA2 driver when used in SHA2_RETURN_BEHAVIOR_CALLBACK. More... | |
typedef struct SHA2_Params_ | SHA2_Params |
SHA2 Parameters. More... | |
Functions | |
void | SHA2_init (void) |
This function initializes the SHA2 module. More... | |
void | SHA2_Params_init (SHA2_Params *params) |
Function to initialize the SHA2_Params struct to its defaults. More... | |
SHA2_Handle | SHA2_open (uint_least8_t index, SHA2_Params *params) |
This function opens a given SHA2 peripheral. More... | |
void | SHA2_close (SHA2_Handle handle) |
Function to close a SHA2 peripheral specified by the SHA2 handle. More... | |
int_fast16_t | SHA2_control (SHA2_Handle handle, uint32_t cmd, void *args) |
Function performs implementation specific features on a given SHA2_Handle. More... | |
void | SHA2_OperationStartHash_init (SHA2_OperationStartHash *operation) |
Function to initialize a SHA2_OperationStartHash struct to its defaults. More... | |
void | SHA2_OperationProcessHash_init (SHA2_OperationProcessHash *operation) |
Function to initialize a SHA2_OperationProcessHash struct to its defaults. More... | |
void | SHA2_OperationFinishHash_init (SHA2_OperationFinishHash *operation) |
Function to initialize a SHA2_OperationFinishHash struct to its defaults. More... | |
void | SHA2_OperationOneStepHash_init (SHA2_OperationOneStepHash *operation) |
Function to initialize a SHA2_OperationOneStepHash struct to its defaults. More... | |
int_fast16_t | SHA2_startHash (SHA2_Handle handle, SHA2_OperationStartHash *operation) |
Sets up a SHA2 hash and hashes the first message segment. More... | |
int_fast16_t | SHA2_processHash (SHA2_Handle handle, SHA2_OperationProcessHash *operation) |
Performs the SHA2 hash on a segment of the message. More... | |
int_fast16_t | SHA2_finishHash (SHA2_Handle handle, SHA2_OperationFinishHash *operation) |
Completes final block(s) of hash and returns a final output hash value. More... | |
int_fast16_t | SHA2_oneStepHash (SHA2_Handle handle, SHA2_OperationOneStepHash *operation) |
Hashes an input message. More... | |
Variables | |
const SHA2_Params | SHA2_defaultParams |
Default SHA2_Params structure. More... | |
typedef struct SHA2_Config_* SHA2_Handle |
A handle that is returned from an SHA2_open() call.
typedef enum SHA2_ReturnBehavior_ SHA2_ReturnBehavior |
The way in which SHA2 function calls return after performing an operation.
Not all SHA2 operations exhibit the specified return behavor. Functions that do not require significant computation and cannot offload that computation to a background thread behave like regular functions. Which functions exhibit the specfied return behavior is not implementation dependent. Specifically, a software-backed implementation run on the same CPU as the application will emulate the return behavior while not actually offloading the computation to the background thread.
SHA2 functions exhibiting the specified return behavior have restrictions on the context from which they may be called.
Task | Hwi | Swi | |
---|---|---|---|
SHA2_RETURN_BEHAVIOR_CALLBACK | X | X | X |
SHA2_RETURN_BEHAVIOR_BLOCKING | X | ||
SHA2_RETURN_BEHAVIOR_POLLING | X | X | X |
typedef enum SHA2_HashSize_ SHA2_HashSize |
Enum for the hash digest sizes supported by the driver.
typedef enum SHA2_DigestLengthBytes_ SHA2_DigestLengthBytes |
Enum for the hash digest lengths in bytes supported by the driver.
typedef enum SHA2_BlockSizeBytes_ SHA2_BlockSizeBytes |
Enum for the block sizes of the algorithms.
SHA2 iteratively consumes segments of the block size and computes intermediate digests which are fed back into the algorithm together with the next segment to compute the next intermediate or final digest. The block sizes of the algorithms differ from their digest lengths. When performing partial hashes, the segment lengths for all but the last segment must be multiples of the relevant block size.
typedef struct SHA2_OperationOneStep_ SHA2_OperationOneStepHash |
Struct containing the parameters required to hash a message in one go.
typedef struct SHA2_OperationStartHash_ SHA2_OperationStartHash |
Struct containing the parameters required to start hashing a message.
typedef struct SHA2_OperationProcessHash_ SHA2_OperationProcessHash |
Struct containing the parameters required to continue hashing a message.
typedef struct SHA2_OperationFinishlHash_ SHA2_OperationFinishHash |
Struct containing the parameters required to process the last blocks of a message and finalize the hash.
typedef union SHA2_Operation_ SHA2_Operation |
Union containing pointers to all supported operation structs.
typedef enum SHA2_OperationType_ SHA2_OperationType |
Enum for the operation types supported by the driver.
typedef struct SHA2_Config_ SHA2_Config |
SHA2 Global configuration.
The SHA2_Config structure contains a set of pointers used to characterize the SHA2 driver implementation.
This structure needs to be defined before calling SHA2_init() and it must not be changed thereafter.
typedef void(* SHA2_CallbackFxn) (SHA2_Handle handle, int_fast16_t returnStatus, SHA2_Operation operation, SHA2_OperationType operationType) |
The definition of a callback function used by the SHA2 driver when used in SHA2_RETURN_BEHAVIOR_CALLBACK.
handle | Handle of the client that started the SHA2 operation. |
returnStatus | The result of the SHA2 operation. May contain an error code. Informs the application of why the callback function was called. |
operation | A union of pointers to operation structs. Only one type of pointer is valid per call to the callback function. Which type is currently valid is determined by /c operationType. The union allows easier access to the struct's fields without the need to typecast the result. |
operationType | This parameter determines which operation the callback refers to and which type to access through /c operation. |
typedef struct SHA2_Params_ SHA2_Params |
SHA2 Parameters.
SHA2 Parameters are used to with the SHA2_open() call. Default values for these parameters are set using SHA2_Params_init().
enum SHA2_ReturnBehavior_ |
The way in which SHA2 function calls return after performing an operation.
Not all SHA2 operations exhibit the specified return behavor. Functions that do not require significant computation and cannot offload that computation to a background thread behave like regular functions. Which functions exhibit the specfied return behavior is not implementation dependent. Specifically, a software-backed implementation run on the same CPU as the application will emulate the return behavior while not actually offloading the computation to the background thread.
SHA2 functions exhibiting the specified return behavior have restrictions on the context from which they may be called.
Task | Hwi | Swi | |
---|---|---|---|
SHA2_RETURN_BEHAVIOR_CALLBACK | X | X | X |
SHA2_RETURN_BEHAVIOR_BLOCKING | X | ||
SHA2_RETURN_BEHAVIOR_POLLING | X | X | X |
enum SHA2_HashSize_ |
enum SHA2_BlockSizeBytes_ |
Enum for the block sizes of the algorithms.
SHA2 iteratively consumes segments of the block size and computes intermediate digests which are fed back into the algorithm together with the next segment to compute the next intermediate or final digest. The block sizes of the algorithms differ from their digest lengths. When performing partial hashes, the segment lengths for all but the last segment must be multiples of the relevant block size.
Enumerator | |
---|---|
SHA2_BLOCK_SIZE_BYTES_224 | |
SHA2_BLOCK_SIZE_BYTES_256 | |
SHA2_BLOCK_SIZE_BYTES_384 | |
SHA2_BLOCK_SIZE_BYTES_512 |
enum SHA2_OperationType_ |
void SHA2_init | ( | void | ) |
This function initializes the SHA2 module.
void SHA2_Params_init | ( | SHA2_Params * | params | ) |
Function to initialize the SHA2_Params struct to its defaults.
params | A pointer to SHA2_Params structure for initialization |
Defaults values are: returnBehavior = SHA2_RETURN_BEHAVIOR_BLOCKING callbackFxn = NULL timeout = SemaphoreP_WAIT_FOREVER custom = NULL
SHA2_Handle SHA2_open | ( | uint_least8_t | index, |
SHA2_Params * | params | ||
) |
This function opens a given SHA2 peripheral.
index | Logical peripheral number for the SHA2 indexed into the SHA2_config table |
params | Pointer to an parameter block, if NULL it will use default values. |
void SHA2_close | ( | SHA2_Handle | handle | ) |
Function to close a SHA2 peripheral specified by the SHA2 handle.
handle | A SHA2 handle returned from SHA2_open() |
int_fast16_t SHA2_control | ( | SHA2_Handle | handle, |
uint32_t | cmd, | ||
void * | args | ||
) |
Function performs implementation specific features on a given SHA2_Handle.
Commands for SHA2_control can originate from SHA2.h or from implementation specific SHA2*.h (SHA2CC26XX.h, SHA2MSP432.h, etc.. ) files. While commands from SHA2.h are API portable across driver implementations, not all implementations may support all these commands. Conversely, commands from driver implementation specific SHA2*.h files add unique driver capabilities but are not API portable across all SHA2 driver implementations.
Commands supported by SHA2.h follow an SHA2_CMD_<cmd> naming convention.
Commands supported by SHA2*.h follow an SHA2*_CMD_<cmd> naming convention.
Each control command defines arg differently. The types of arg are documented with each command.
See SHA2_control command codes for command codes.
See SHA2_control return status codes for status codes.
handle | A SHA2 handle returned from SHA2_open() |
cmd | SHA2.h or SHA2*.h commands. |
args | An optional R/W (read/write) command argument accompanied with cmd |
void SHA2_OperationStartHash_init | ( | SHA2_OperationStartHash * | operation | ) |
Function to initialize a SHA2_OperationStartHash struct to its defaults.
operation | A pointer to SHA2_OperationStartHash structure for initialization |
Defaults values are all zeros.
void SHA2_OperationProcessHash_init | ( | SHA2_OperationProcessHash * | operation | ) |
Function to initialize a SHA2_OperationProcessHash struct to its defaults.
operation | A pointer to SHA2_OperationProcessHash structure for initialization |
Defaults values are all zeros.
void SHA2_OperationFinishHash_init | ( | SHA2_OperationFinishHash * | operation | ) |
Function to initialize a SHA2_OperationFinishHash struct to its defaults.
operation | A pointer to SHA2_OperationFinishHash structure for initialization |
Defaults values are all zeros.
void SHA2_OperationOneStepHash_init | ( | SHA2_OperationOneStepHash * | operation | ) |
Function to initialize a SHA2_OperationOneStepHash struct to its defaults.
operation | A pointer to SHA2_OperationOneStepHash structure for initialization |
Defaults values are all zeros.
int_fast16_t SHA2_startHash | ( | SHA2_Handle | handle, |
SHA2_OperationStartHash * | operation | ||
) |
Sets up a SHA2 hash and hashes the first message segment.
handle | A SHA2 handle returned from SHA2_open() |
operation | A pointer to a struct containing the requisite parameters to execute the function. |
int_fast16_t SHA2_processHash | ( | SHA2_Handle | handle, |
SHA2_OperationProcessHash * | operation | ||
) |
Performs the SHA2 hash on a segment of the message.
SHA2_processHash() should be used when only part of a message is available for hashing.
handle | A SHA2 handle returned from SHA2_open() |
operation | A pointer to a struct containing the requisite parameters to execute the function. |
int_fast16_t SHA2_finishHash | ( | SHA2_Handle | handle, |
SHA2_OperationFinishHash * | operation | ||
) |
Completes final block(s) of hash and returns a final output hash value.
SHA2_finishHash() must be used to process the final block of the hash, and may also be used to process some or all preceding blocks of the message.
handle | A SHA2 handle returned from SHA2_open(). |
operation | A pointer to a struct containing the requisite parameters to execute the function. |
int_fast16_t SHA2_oneStepHash | ( | SHA2_Handle | handle, |
SHA2_OperationOneStepHash * | operation | ||
) |
Hashes an input message.
SHA2_oneStepHash() hashes a message stored contiguously. Use this function if you have access to the entire message when starting to hash.
handle | A SHA2 handle returned from SHA2_open(). |
operation | A pointer to a struct containing the requisite parameters to execute the function. |
const SHA2_Params SHA2_defaultParams |
Default SHA2_Params structure.