SHA2.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019, 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  * @warning This is a beta API. It may change in future releases.
38  *
39  * @anchor ti_drivers_SHA2_Overview
40  * # Overview #
41  *
42  * SHA2 (Secure Hash Algorithm 2) is a cryptographic hashing algorithm that
43  * maps an input of arbitrary length to a fixed-length output with negligible
44  * probability of collision. A collision would occur when two different inputs
45  * map to the same output.
46  *
47  * It is not currently technologicaly feasible to derive an input from
48  * the hash digest (output) itself.
49  *
50  * Hashes are often used to ensure the integrity of messages. They are also
51  * used to as constituent parts of more complicated cyptographic schemes.
52  * HMAC is a message authentication code that is based on hash functions such
53  * as SHA2 rather than a block cipher.
54  * Hashes may themselves be used as or form a part of key derivation functions
55  * used to derive symmetric keys from sources of entropy such as an Elliptic
56  * Curve Diffie-Helman key exchange (ECDH).
57  *
58  * SHA2 is not actually a single algorithm, but a suite of similar algorithms
59  * that produce hash digests of different lengths. 224, 256, 384, and 512-bit
60  * outputs are available.
61  *
62  * "Hash" may refer to either the process of hashing when used as a verb and
63  * the output digest when used as a noun.
64  *
65  * @anchor ti_drivers_SHA2_Usage
66  * # Usage #
67  *
68  * Before starting a SHA2 operation, the application must do the following:
69  * - Call #SHA2_init() to initialize the driver
70  * - Call #SHA2_Params_init() to initialize the SHA2_Params to default values.
71  * - Modify the #SHA2_Params as desired
72  * - Call #SHA2_open() to open an instance of the driver
73  *
74  * There are two general ways to execute a SHA2 operation:
75  *
76  * - one-step (in one operation)
77  * - multi-step (multiple partial operations)
78 
79  * @anchor ti_drivers_SHA2_Synopsis
80  * # Synopsis
81  *
82  * @anchor ti_drivers_SHA2_Synopsis_Code
83  * @code
84  *
85  * // Import SHA2 Driver definitions
86  * #include <ti/drivers/SHA2.h>
87  *
88  * // Define name for SHA2 channel index
89  * #define SHA2_INSTANCE 0
90  *
91  * SHA2_init();
92  *
93  * handle = SHA2_open(SHA2_INSTANCE, NULL);
94  *
95  * result = SHA2_hashData(handle, message, strlen(message), actualDigest);
96  *
97  * SHA2_close(handle);
98  * @endcode
99  *
100  * @anchor ti_drivers_SHA2_Examples
101  * # Examples #
102  *
103  * ## One-step hash operation #
104  *
105  * The #SHA2_hashData() function can perform a SHA2 operation in a single call.
106  * It will always use the most highly optimized routine with the least overhead and
107  * the fastest runtime. However, it requires that the entire input message is
108  * available to the function in a contiguous location at the start of the call.
109  * The single call operation is required when hashing a message with a length smaller
110  * than or equal to one hash-block length. All devices support single call operations.
111  *
112  * After a SHA2 operation completes, the application may either start
113  * another operation or close the driver by calling #SHA2_close().
114  *
115  * @code
116  * SHA2_Params params;
117  * SHA2_Handle handle;
118  * int_fast16_t result;
119  *
120  * char message[] = "A Ferengi without profit is no Ferengi at all.";
121  *
122  * uint8_t actualDigest[SHA2_DIGEST_LENGTH_BYTES_256];
123  * uint8_t expectedDigest[] = {
124  * 0x93, 0xD6, 0x5C, 0x07,
125  * 0xA6, 0x26, 0x88, 0x9C,
126  * 0x87, 0xCC, 0x82, 0x24,
127  * 0x47, 0xC6, 0xE4, 0x28,
128  * 0xC0, 0xBD, 0xC6, 0xED,
129  * 0xAA, 0x8C, 0xD2, 0x53,
130  * 0x77, 0xAA, 0x73, 0x14,
131  * 0xA3, 0xE2, 0xDE, 0x43
132  * };
133  *
134  * SHA2_init();
135  *
136  * SHA2_Params_init(&params);
137  * params.returnBehavior = SHA2_RETURN_BEHAVIOR_BLOCKING;
138  * handle = SHA2_open(0, &params);
139  * assert(handle != NULL);
140  *
141  * result = SHA2_hashData(handle, message, strlen(message), actualDigest);
142  * assert(result == SHA2_STATUS_SUCCESS);
143  *
144  * result = memcmp(actualDigest, expectedDigest, SHA2_DIGEST_LENGTH_BYTES_256);
145  * assert(result == 0);
146  *
147  * SHA2_close(handle);
148  * @endcode
149  *
150  * ## Partial hash operation #
151  *
152  * When trying to operate on data that is too large to fit into available memory,
153  * partial processing is more advisable. The segments are processed with
154  * #SHA2_addData() whereas the final digest is computed by #SHA2_finalize().
155  *
156  * @code
157  * SHA2_Handle handle;
158  * int_fast16_t result;
159  * SHA2_Params params;
160  *
161  * const char message[] =
162  * "Premature optimization is the root of all evil (or at least most of it) in programming.";
163  *
164  * uint8_t actualDigest[SHA2_DIGEST_LENGTH_BYTES_256];
165  * uint8_t expectedDigest[] = {
166  * 0xF2, 0x6A, 0xFF, 0x01,
167  * 0x11, 0x6B, 0xF6, 0x77,
168  * 0x63, 0x91, 0xFE, 0xD9,
169  * 0x47, 0x56, 0x99, 0xB2,
170  * 0xAD, 0x7D, 0x64, 0x16,
171  * 0xF7, 0x40, 0x1A, 0x5B,
172  * 0xCC, 0xC7, 0x08, 0x3D,
173  * 0xE8, 0x6B, 0x35, 0x6D,
174  * };
175  *
176  * SHA2_init();
177  *
178  * SHA2_Params_init(&params);
179  * params.returnBehavior = SHA2_RETURN_BEHAVIOR_BLOCKING;
180  * handle = SHA2_open(0, &params);
181  * assert(handle != NULL);
182  *
183  * // We can configure the driver even after SHA2_open()
184  * result = SHA2_setHashType(handle, SHA2_HASH_TYPE_256);
185  * assert(result == SHA2_STATUS_SUCCESS);
186  *
187  * // Process data in chunks. The driver buffers incomplete blocks internally.
188  * result = SHA2_addData(handle, &message[0], 17);
189  * assert(result == SHA2_STATUS_SUCCESS);
190  *
191  * result = SHA2_addData(handle, &message[17], strlen(message) - 17);
192  * assert(result == SHA2_STATUS_SUCCESS);
193  *
194  * // Compute the resulting digest
195  * result = SHA2_finalize(handle, actualDigest);
196  * assert(result == SHA2_STATUS_SUCCESS);
197  *
198  * // Verify
199  * result = memcmp(actualDigest, expectedDigest, SHA2_DIGEST_LENGTH_BYTES_256);
200  * assert(result == 0);
201  *
202  * SHA2_close(handle);
203  * @endcode
204  *
205  */
206 
207 #ifndef ti_drivers_SHA2__include
208 #define ti_drivers_SHA2__include
209 
210 #include <stdbool.h>
211 #include <stddef.h>
212 #include <stdint.h>
213 
214 #ifdef __cplusplus
215 extern "C" {
216 #endif
217 
230 #define SHA2_STATUS_RESERVED (-32)
231 
238 #define SHA2_STATUS_SUCCESS (0)
239 
246 #define SHA2_STATUS_ERROR (-1)
247 
256 #define SHA2_STATUS_RESOURCE_UNAVAILABLE (-2)
257 
261 #define SHA2_STATUS_CANCELED (-3)
262 
284 typedef enum {
300 
304 typedef enum {
309 } SHA2_HashType;
310 
314 typedef enum {
320 
334 typedef enum {
340 
352 typedef struct {
354  void *object;
355 
357  void const *hwAttrs;
358 } SHA2_Config;
359 
364 
375 typedef void (*SHA2_CallbackFxn) (SHA2_Handle handle, int_fast16_t returnStatus);
376 
385 typedef struct {
391  uint32_t timeout;
394 } SHA2_Params;
395 
404 extern const SHA2_Config SHA2_config[];
405 
413 extern const uint_least8_t SHA2_count;
414 
420 extern const SHA2_Params SHA2_defaultParams;
421 
422 
431 void SHA2_init(void);
432 
445 void SHA2_Params_init(SHA2_Params *params);
446 
463 SHA2_Handle SHA2_open(uint_least8_t index, const SHA2_Params *params);
464 
474 void SHA2_close(SHA2_Handle handle);
475 
502 int_fast16_t SHA2_addData(SHA2_Handle handle, const void* data, size_t length);
503 
533 int_fast16_t SHA2_hashData(SHA2_Handle handle, const void* data, size_t size, void *digest);
534 
557 int_fast16_t SHA2_finalize(SHA2_Handle handle, void *digest);
558 
570 void SHA2_reset(SHA2_Handle handle);
571 
584 int_fast16_t SHA2_cancelOperation(SHA2_Handle handle);
585 
605 int_fast16_t SHA2_setHashType(SHA2_Handle handle, SHA2_HashType type);
606 
630 SHA2_Handle SHA2_construct(SHA2_Config *config, const SHA2_Params *params);
631 
632 #ifdef __cplusplus
633 }
634 #endif
635 
636 #endif /* ti_drivers_SHA2__include */
Definition: SHA2.h:318
SHA2_Config * SHA2_Handle
A handle that is returned from an SHA2_open() call.
Definition: SHA2.h:363
SHA2_Handle SHA2_construct(SHA2_Config *config, const SHA2_Params *params)
Constructs a new SHA2 object.
SHA2_HashType hashType
Definition: SHA2.h:386
Definition: SHA2.h:316
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:335
const uint_least8_t SHA2_count
Global SHA2 configuration count.
Definition: SHA2.h:285
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:306
Definition: SHA2.h:317
SHA2 Global configuration.
Definition: SHA2.h:352
SHA2_ReturnBehavior returnBehavior
Definition: SHA2.h:389
void SHA2_init(void)
Initializes the SHA2 driver module.
Definition: SHA2.h:291
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:295
Definition: SHA2.h:337
void SHA2_close(SHA2_Handle handle)
Closes a SHA2 peripheral specified by handle.
Definition: SHA2.h:315
uint32_t timeout
Definition: SHA2.h:391
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:284
SHA2_HashType
Enum for the hash types supported by the driver.
Definition: SHA2.h:304
Definition: SHA2.h:336
SHA2_DigestLengthBytes
Enum for the hash digest lengths in bytes supported by the driver.
Definition: SHA2.h:314
int_fast16_t SHA2_finalize(SHA2_Handle handle, void *digest)
Finishes hash a operation and writes the result to digest.
Definition: SHA2.h:307
void * object
Definition: SHA2.h:354
Definition: SHA2.h:308
int_fast16_t SHA2_cancelOperation(SHA2_Handle handle)
Aborts an ongoing SHA2 operation and clears internal buffers.
Definition: SHA2.h:338
void const * hwAttrs
Definition: SHA2.h:357
SHA2_CallbackFxn callbackFxn
Definition: SHA2.h:390
SHA2 Parameters.
Definition: SHA2.h:385
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:375
void SHA2_Params_init(SHA2_Params *params)
Initializes params with default values.
Definition: SHA2.h:305
SHA2_BlockSizeBytes
Enum for the block sizes of the algorithms.
Definition: SHA2.h:334
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale