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  * ## One-step HMAC operation #
204  *
205  * The #SHA2_hmac() function can perform a SHA2 operation in a single call.
206  * It will always use the most highly optimized routine with the least overhead
207  * and the fastest runtime. It requires that the entire input message is
208  * available to the function in a contiguous location at the start of the call.
209  *
210  * After a SHA2 operation completes, the application may either start
211  * another operation or close the driver by calling #SHA2_close().
212  *
213  * @code
214  * SHA2_Params params;
215  * SHA2_Handle handle;
216  * int_fast16_t result;
217  * CryptoKey hmacKey;
218  *
219  * uint8_t message[] = {
220  * 0xb1, 0x68, 0x9c, 0x25, 0x91, 0xea, 0xf3, 0xc9,
221  * 0xe6, 0x60, 0x70, 0xf8, 0xa7, 0x79, 0x54, 0xff,
222  * 0xb8, 0x17, 0x49, 0xf1, 0xb0, 0x03, 0x46, 0xf9,
223  * 0xdf, 0xe0, 0xb2, 0xee, 0x90, 0x5d, 0xcc, 0x28,
224  * 0x8b, 0xaf, 0x4a, 0x92, 0xde, 0x3f, 0x40, 0x01,
225  * 0xdd, 0x9f, 0x44, 0xc4, 0x68, 0xc3, 0xd0, 0x7d,
226  * 0x6c, 0x6e, 0xe8, 0x2f, 0xac, 0xea, 0xfc, 0x97,
227  * 0xc2, 0xfc, 0x0f, 0xc0, 0x60, 0x17, 0x19, 0xd2,
228  * 0xdc, 0xd0, 0xaa, 0x2a, 0xec, 0x92, 0xd1, 0xb0,
229  * 0xae, 0x93, 0x3c, 0x65, 0xeb, 0x06, 0xa0, 0x3c,
230  * 0x9c, 0x93, 0x5c, 0x2b, 0xad, 0x04, 0x59, 0x81,
231  * 0x02, 0x41, 0x34, 0x7a, 0xb8, 0x7e, 0x9f, 0x11,
232  * 0xad, 0xb3, 0x04, 0x15, 0x42, 0x4c, 0x6c, 0x7f,
233  * 0x5f, 0x22, 0xa0, 0x03, 0xb8, 0xab, 0x8d, 0xe5,
234  * 0x4f, 0x6d, 0xed, 0x0e, 0x3a, 0xb9, 0x24, 0x5f,
235  * 0xa7, 0x95, 0x68, 0x45, 0x1d, 0xfa, 0x25, 0x8e};
236  *
237  * // In this case, keyingMaterial is 40 bytes long. It could also be
238  * // any other length.
239  * uint8_t keyingMaterial[] = {
240  * 0x97, 0x79, 0xd9, 0x12, 0x06, 0x42, 0x79, 0x7f,
241  * 0x17, 0x47, 0x02, 0x5d, 0x5b, 0x22, 0xb7, 0xac,
242  * 0x60, 0x7c, 0xab, 0x08, 0xe1, 0x75, 0x8f, 0x2f,
243  * 0x3a, 0x46, 0xc8, 0xbe, 0x1e, 0x25, 0xc5, 0x3b,
244  * 0x8c, 0x6a, 0x8f, 0x58, 0xff, 0xef, 0xa1, 0x76};
245  *
246  *
247  *
248  * uint8_t actualHmac[SHA2_DIGEST_LENGTH_BYTES_256];
249  * uint8_t expectedHmac[] = {
250  * 0x76, 0x9f, 0x00, 0xd3, 0xe6, 0xa6, 0xcc, 0x1f,
251  * 0xb4, 0x26, 0xa1, 0x4a, 0x4f, 0x76, 0xc6, 0x46,
252  * 0x2e, 0x61, 0x49, 0x72, 0x6e, 0x0d, 0xee, 0x0e,
253  * 0xc0, 0xcf, 0x97, 0xa1, 0x66, 0x05, 0xac, 0x8b
254  * };
255  *
256  * SHA2_init();
257  *
258  * SHA2_Params_init(&params);
259  * params.returnBehavior = SHA2_RETURN_BEHAVIOR_BLOCKING;
260  * handle = SHA2_open(0, &params);
261  * assert(handle != NULL);
262  *
263  * CryptoKeyPlaintext_initKey(&hmacKey,
264  * keyingMaterial,
265  * sizeof(keyingMaterial));
266  *
267  * result = SHA2_hmac(handle,
268  * &hmacKey,
269  * message,
270  * sizeof(message),
271  * actualHmac);
272  * assert(result == SHA2_STATUS_SUCCESS);
273  *
274  * result = memcmp(actualHmac, expectedHmac, SHA2_DIGEST_LENGTH_BYTES_256);
275  * assert(result == 0);
276  *
277  * SHA2_close(handle);
278  * @endcode
279  */
280 
281 #ifndef ti_drivers_SHA2__include
282 #define ti_drivers_SHA2__include
283 
284 #include <stdbool.h>
285 #include <stddef.h>
286 #include <stdint.h>
287 
289 
290 #ifdef __cplusplus
291 extern "C" {
292 #endif
293 
306 #define SHA2_STATUS_RESERVED (-32)
307 
314 #define SHA2_STATUS_SUCCESS (0)
315 
322 #define SHA2_STATUS_ERROR (-1)
323 
332 #define SHA2_STATUS_RESOURCE_UNAVAILABLE (-2)
333 
337 #define SHA2_STATUS_CANCELED (-3)
338 
360 typedef enum {
376 
380 typedef enum {
385 } SHA2_HashType;
386 
390 typedef enum {
396 
410 typedef enum {
416 
428 typedef struct {
430  void *object;
431 
433  void const *hwAttrs;
434 } SHA2_Config;
435 
440 
451 typedef void (*SHA2_CallbackFxn) (SHA2_Handle handle, int_fast16_t returnStatus);
452 
461 typedef struct {
467  uint32_t timeout;
470 } SHA2_Params;
471 
480 extern const SHA2_Config SHA2_config[];
481 
489 extern const uint_least8_t SHA2_count;
490 
496 extern const SHA2_Params SHA2_defaultParams;
497 
498 
507 void SHA2_init(void);
508 
522 
539 SHA2_Handle SHA2_open(uint_least8_t index, const SHA2_Params *params);
540 
550 void SHA2_close(SHA2_Handle handle);
551 
578 int_fast16_t SHA2_setupHmac(SHA2_Handle handle, CryptoKey *key);
579 
611 int_fast16_t SHA2_addData(SHA2_Handle handle, const void* data, size_t length);
612 
637 int_fast16_t SHA2_finalize(SHA2_Handle handle, void *digest);
638 
664 int_fast16_t SHA2_finalizeHmac(SHA2_Handle handle, void *hmac);
665 
699 int_fast16_t SHA2_hashData(SHA2_Handle handle,
700  const void* data,
701  size_t dataLength,
702  void *digest);
703 
740 int_fast16_t SHA2_hmac(SHA2_Handle handle,
741  CryptoKey *key,
742  const void *data,
743  size_t dataLength,
744  void *hmac);
745 
757 void SHA2_reset(SHA2_Handle handle);
758 
771 int_fast16_t SHA2_cancelOperation(SHA2_Handle handle);
772 
792 int_fast16_t SHA2_setHashType(SHA2_Handle handle, SHA2_HashType type);
793 
817 SHA2_Handle SHA2_construct(SHA2_Config *config, const SHA2_Params *params);
818 
819 #ifdef __cplusplus
820 }
821 #endif
822 
823 #endif /* ti_drivers_SHA2__include */
int_fast16_t SHA2_finalizeHmac(SHA2_Handle handle, void *hmac)
Finishes an HMAC operation and writes the result to hmac.
Definition: SHA2.h:394
SHA2_Config * SHA2_Handle
A handle that is returned from an SHA2_open() call.
Definition: SHA2.h:439
ADC_Params params
Definition: Driver_Init.h:11
The CryptoKey type is an opaque representation of a cryptographic key.
SHA2_Handle SHA2_construct(SHA2_Config *config, const SHA2_Params *params)
Constructs a new SHA2 object.
int_fast16_t SHA2_hmac(SHA2_Handle handle, CryptoKey *key, const void *data, size_t dataLength, void *hmac)
Creates a keyed hash of data with key.
SHA2_HashType hashType
Definition: SHA2.h:462
Definition: SHA2.h:392
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:411
const uint_least8_t SHA2_count
Global SHA2 configuration count.
Definition: SHA2.h:361
const SHA2_Params SHA2_defaultParams
Default SHA2_Params structure.
Definition: SHA2.h:382
Definition: SHA2.h:393
SHA2 Global configuration.
Definition: SHA2.h:428
SHA2_ReturnBehavior returnBehavior
Definition: SHA2.h:465
CryptoKey datastructure.
Definition: CryptoKey.h:209
void SHA2_init(void)
Initializes the SHA2 driver module.
Definition: SHA2.h:367
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 or HMAC.
int_fast16_t SHA2_setHashType(SHA2_Handle handle, SHA2_HashType type)
Selects a new hash algorithm type.
Definition: SHA2.h:371
Definition: SHA2.h:413
void SHA2_close(SHA2_Handle handle)
Closes a SHA2 peripheral specified by handle.
Definition: SHA2.h:391
uint32_t timeout
Definition: SHA2.h:467
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:360
SHA2_HashType
Enum for the hash types supported by the driver.
Definition: SHA2.h:380
Definition: SHA2.h:412
SHA2_DigestLengthBytes
Enum for the hash digest lengths in bytes supported by the driver.
Definition: SHA2.h:390
int_fast16_t SHA2_setupHmac(SHA2_Handle handle, CryptoKey *key)
Starts an HMAC operation on segmented data.
int_fast16_t SHA2_finalize(SHA2_Handle handle, void *digest)
Finishes a hash operation and writes the result to digest.
Definition: SHA2.h:383
void * object
Definition: SHA2.h:430
Definition: SHA2.h:384
int_fast16_t SHA2_hashData(SHA2_Handle handle, const void *data, size_t dataLength, void *digest)
Hashes a segment of data with a size in bytes and writes the resulting hash to digest.
int_fast16_t SHA2_cancelOperation(SHA2_Handle handle)
Aborts an ongoing SHA2 operation and clears internal buffers.
Definition: SHA2.h:414
void const * hwAttrs
Definition: SHA2.h:433
SHA2_CallbackFxn callbackFxn
Definition: SHA2.h:466
SHA2 Parameters.
Definition: SHA2.h:461
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:451
void SHA2_Params_init(SHA2_Params *params)
Initializes params with default values.
Definition: SHA2.h:381
SHA2_BlockSizeBytes
Enum for the block sizes of the algorithms.
Definition: SHA2.h:410
© Copyright 1995-2020, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale