SHA2.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file SHA2.h
34  *
35  * @brief SHA2 driver header
36  *
37  * @anchor ti_drivers_SHA2_Overview
38  * # Overview #
39  *
40  * SHA2 (Secure Hash Algorithm 2) is a cryptographic hashing algorithm that
41  * maps an input of arbitrary length to a fixed-length output with negligible
42  * probability of collision. A collision would occur when two different inputs
43  * map to the same output.
44  *
45  * It is not currently technologicaly feasible to derive an input from
46  * the hash digest (output) itself.
47  *
48  * Hashes are often used to ensure the integrity of messages. They are also
49  * used to as constituent parts of more complicated cyptographic schemes.
50  * HMAC is a message authentication code that is based on hash functions such
51  * as SHA2 rather than a block cipher.
52  * Hashes may themselves be used as or form a part of key derivation functions
53  * used to derive symmetric keys from sources of entropy such as an Elliptic
54  * Curve Diffie-Helman key exchange (ECDH).
55  *
56  * SHA2 is not actually a single algorithm, but a suite of similar algorithms
57  * that produce hash digests of different lengths. 224, 256, 384, and 512-bit
58  * outputs are available.
59  *
60  * "Hash" may refer to either the process of hashing when used as a verb and
61  * the output digest when used as a noun.
62  *
63  * @anchor ti_drivers_SHA2_Usage
64  * # Usage #
65  *
66  * Before starting a SHA2 operation, the application must do the following:
67  * - Call #SHA2_init() to initialize the driver
68  * - Call #SHA2_Params_init() to initialize the SHA2_Params to default values.
69  * - Modify the #SHA2_Params as desired
70  * - Call #SHA2_open() to open an instance of the driver
71  *
72  * There are two general ways to execute a SHA2 operation:
73  *
74  * - one-step (in one operation)
75  * - multi-step (multiple partial operations)
76 
77  * @anchor ti_drivers_SHA2_Synopsis
78  * # Synopsis
79  *
80  * @anchor ti_drivers_SHA2_Synopsis_Code
81  * @code
82  *
83  * // Import SHA2 Driver definitions
84  * #include <ti/drivers/SHA2.h>
85  *
86  * // Define name for SHA2 channel index
87  * #define SHA2_INSTANCE 0
88  *
89  * SHA2_init();
90  *
91  * handle = SHA2_open(SHA2_INSTANCE, NULL);
92  *
93  * result = SHA2_hashData(handle, message, strlen(message), actualDigest);
94  *
95  * SHA2_close(handle);
96  * @endcode
97  *
98  * @anchor ti_drivers_SHA2_Examples
99  * # Examples #
100  *
101  * ## One-step hash operation #
102  *
103  * The #SHA2_hashData() function can perform a SHA2 operation in a single call.
104  * It will always use the most highly optimized routine with the least overhead and
105  * the fastest runtime. However, it requires that the entire input message is
106  * available to the function in a contiguous location at the start of the call.
107  * The single call operation is required when hashing a message with a length smaller
108  * than or equal to one hash-block length. All devices support single call operations.
109  *
110  * After a SHA2 operation completes, the application may either start
111  * another operation or close the driver by calling #SHA2_close().
112  *
113  * @code
114  * SHA2_Params params;
115  * SHA2_Handle handle;
116  * int_fast16_t result;
117  *
118  * char message[] = "A Ferengi without profit is no Ferengi at all.";
119  *
120  * uint8_t actualDigest[SHA2_DIGEST_LENGTH_BYTES_256];
121  * uint8_t expectedDigest[] = {
122  * 0x93, 0xD6, 0x5C, 0x07,
123  * 0xA6, 0x26, 0x88, 0x9C,
124  * 0x87, 0xCC, 0x82, 0x24,
125  * 0x47, 0xC6, 0xE4, 0x28,
126  * 0xC0, 0xBD, 0xC6, 0xED,
127  * 0xAA, 0x8C, 0xD2, 0x53,
128  * 0x77, 0xAA, 0x73, 0x14,
129  * 0xA3, 0xE2, 0xDE, 0x43
130  * };
131  *
132  * SHA2_init();
133  *
134  * SHA2_Params_init(&params);
135  * params.returnBehavior = SHA2_RETURN_BEHAVIOR_BLOCKING;
136  * handle = SHA2_open(0, &params);
137  * assert(handle != NULL);
138  *
139  * result = SHA2_hashData(handle, message, strlen(message), actualDigest);
140  * assert(result == SHA2_STATUS_SUCCESS);
141  *
142  * result = memcmp(actualDigest, expectedDigest, SHA2_DIGEST_LENGTH_BYTES_256);
143  * assert(result == 0);
144  *
145  * SHA2_close(handle);
146  * @endcode
147  *
148  * ## Partial hash operation #
149  *
150  * When trying to operate on data that is too large to fit into available memory,
151  * partial processing is more advisable. The segments are processed with
152  * #SHA2_addData() whereas the final digest is computed by #SHA2_finalize().
153  *
154  * @code
155  * SHA2_Handle handle;
156  * int_fast16_t result;
157  * SHA2_Params params;
158  *
159  * const char message[] =
160  * "Premature optimization is the root of all evil (or at least most of it) in programming.";
161  *
162  * uint8_t actualDigest[SHA2_DIGEST_LENGTH_BYTES_256];
163  * uint8_t expectedDigest[] = {
164  * 0xF2, 0x6A, 0xFF, 0x01,
165  * 0x11, 0x6B, 0xF6, 0x77,
166  * 0x63, 0x91, 0xFE, 0xD9,
167  * 0x47, 0x56, 0x99, 0xB2,
168  * 0xAD, 0x7D, 0x64, 0x16,
169  * 0xF7, 0x40, 0x1A, 0x5B,
170  * 0xCC, 0xC7, 0x08, 0x3D,
171  * 0xE8, 0x6B, 0x35, 0x6D,
172  * };
173  *
174  * SHA2_init();
175  *
176  * SHA2_Params_init(&params);
177  * params.returnBehavior = SHA2_RETURN_BEHAVIOR_BLOCKING;
178  * handle = SHA2_open(0, &params);
179  * assert(handle != NULL);
180  *
181  * // We can configure the driver even after SHA2_open()
182  * result = SHA2_setHashType(handle, SHA2_HASH_TYPE_256);
183  * assert(result == SHA2_STATUS_SUCCESS);
184  *
185  * // Process data in chunks. The driver buffers incomplete blocks internally.
186  * result = SHA2_addData(handle, &message[0], 17);
187  * assert(result == SHA2_STATUS_SUCCESS);
188  *
189  * result = SHA2_addData(handle, &message[17], strlen(message) - 17);
190  * assert(result == SHA2_STATUS_SUCCESS);
191  *
192  * // Compute the resulting digest
193  * result = SHA2_finalize(handle, actualDigest);
194  * assert(result == SHA2_STATUS_SUCCESS);
195  *
196  * // Verify
197  * result = memcmp(actualDigest, expectedDigest, SHA2_DIGEST_LENGTH_BYTES_256);
198  * assert(result == 0);
199  *
200  * SHA2_close(handle);
201  * @endcode
202  *
203  */
204 
205 #ifndef ti_drivers_SHA2__include
206 #define ti_drivers_SHA2__include
207 
208 #include <stdbool.h>
209 #include <stddef.h>
210 #include <stdint.h>
211 
212 #ifdef __cplusplus
213 extern "C" {
214 #endif
215 
228 #define SHA2_STATUS_RESERVED (-32)
229 
236 #define SHA2_STATUS_SUCCESS (0)
237 
244 #define SHA2_STATUS_ERROR (-1)
245 
254 #define SHA2_STATUS_RESOURCE_UNAVAILABLE (-2)
255 
259 #define SHA2_STATUS_CANCELED (-3)
260 
282 typedef enum {
298 
302 typedef enum {
307 } SHA2_HashType;
308 
312 typedef enum {
318 
332 typedef enum {
338 
350 typedef struct {
352  void *object;
353 
355  void const *hwAttrs;
356 } SHA2_Config;
357 
362 
373 typedef void (*SHA2_CallbackFxn) (SHA2_Handle handle, int_fast16_t returnStatus);
374 
383 typedef struct {
389  uint32_t timeout;
392 } SHA2_Params;
393 
402 extern const SHA2_Config SHA2_config[];
403 
411 extern const uint_least8_t SHA2_count;
412 
418 extern const SHA2_Params SHA2_defaultParams;
419 
420 
429 void SHA2_init(void);
430 
443 void SHA2_Params_init(SHA2_Params *params);
444 
461 SHA2_Handle SHA2_open(uint_least8_t index, const SHA2_Params *params);
462 
472 void SHA2_close(SHA2_Handle handle);
473 
500 int_fast16_t SHA2_addData(SHA2_Handle handle, const void* data, size_t length);
501 
531 int_fast16_t SHA2_hashData(SHA2_Handle handle, const void* data, size_t size, void *digest);
532 
555 int_fast16_t SHA2_finalize(SHA2_Handle handle, void *digest);
556 
568 void SHA2_reset(SHA2_Handle handle);
569 
585 int_fast16_t SHA2_cancelOperation(SHA2_Handle handle);
586 
606 int_fast16_t SHA2_setHashType(SHA2_Handle handle, SHA2_HashType type);
607 
631 SHA2_Handle SHA2_construct(SHA2_Config *config, const SHA2_Params *params);
632 
633 #ifdef __cplusplus
634 }
635 #endif
636 
637 #endif /* ti_drivers_SHA2__include */
Definition: SHA2.h:316
SHA2_Config * SHA2_Handle
A handle that is returned from an SHA2_open() call.
Definition: SHA2.h:361
SHA2_Handle SHA2_construct(SHA2_Config *config, const SHA2_Params *params)
Constructs a new SHA2 object.
SHA2_HashType hashType
Definition: SHA2.h:384
Definition: SHA2.h:314
const SHA2_Config SHA2_config[]
Global SHA2 configuration struct.
void SHA2_reset(SHA2_Handle handle)
Clears internal buffers and aborts an ongoing SHA2 operation.
Definition: SHA2.h:333
const uint_least8_t SHA2_count
Global SHA2 configuration count.
Definition: SHA2.h:283
int_fast16_t SHA2_hashData(SHA2_Handle handle, const void *data, size_t size, void *digest)
Hashes a segment of data with a size in bytes and writes the resulting hash to digest.
const SHA2_Params SHA2_defaultParams
Default SHA2_Params structure.
Definition: SHA2.h:304
Definition: SHA2.h:315
SHA2 Global configuration.
Definition: SHA2.h:350
SHA2_ReturnBehavior returnBehavior
Definition: SHA2.h:387
void SHA2_init(void)
Initializes the SHA2 driver module.
Definition: SHA2.h:289
int_fast16_t SHA2_addData(SHA2_Handle handle, const void *data, size_t length)
Adds a segment of data with a length in bytes to the cryptographic hash.
int_fast16_t SHA2_setHashType(SHA2_Handle handle, SHA2_HashType type)
Selects a new hash algorithm type.
Definition: SHA2.h:293
Definition: SHA2.h:335
void SHA2_close(SHA2_Handle handle)
Closes a SHA2 peripheral specified by handle.
Definition: SHA2.h:313
uint32_t timeout
Definition: SHA2.h:389
SHA2_Handle SHA2_open(uint_least8_t index, const SHA2_Params *params)
Initializes a SHA2 driver instance and returns a handle.
SHA2_ReturnBehavior
The way in which SHA2 function calls return after performing an operation.
Definition: SHA2.h:282
SHA2_HashType
Enum for the hash types supported by the driver.
Definition: SHA2.h:302
Definition: SHA2.h:334
SHA2_DigestLengthBytes
Enum for the hash digest lengths in bytes supported by the driver.
Definition: SHA2.h:312
int_fast16_t SHA2_finalize(SHA2_Handle handle, void *digest)
Finishes hash a operation and writes the result to digest.
Definition: SHA2.h:305
void * object
Definition: SHA2.h:352
Definition: SHA2.h:306
int_fast16_t SHA2_cancelOperation(SHA2_Handle handle)
Aborts an ongoing SHA2 operation and clears internal buffers.
Definition: SHA2.h:336
void const * hwAttrs
Definition: SHA2.h:355
SHA2_CallbackFxn callbackFxn
Definition: SHA2.h:388
SHA2 Parameters.
Definition: SHA2.h:383
void(* SHA2_CallbackFxn)(SHA2_Handle handle, int_fast16_t returnStatus)
The definition of a callback function used by the SHA2 driver when used in SHA2_RETURN_BEHAVIOR_CALLB...
Definition: SHA2.h:373
void SHA2_Params_init(SHA2_Params *params)
Initializes params with default values.
Definition: SHA2.h:303
SHA2_BlockSizeBytes
Enum for the block sizes of the algorithms.
Definition: SHA2.h:332
© Copyright 1995-2020, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale