AESGCM.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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  *
34  * @file AESGCM.h
35  *
36  * @brief AESGCM driver header
37  *
38  * @anchor ti_drivers_AESGCM_Overview
39  * ### Overview #
40  *
41  * The Galois Counter Mode (GCM) mode of operation is a generic
42  * authenticated encryption with associated data (AEAD) block cipher mode.
43  * It can be implemented with any block cipher.
44  * AESGCM combines GHASH with the AES block cipher in CTR mode of operation.
45  *
46  * This combination of block cipher modes enables GCM to encrypt messages of any
47  * length and not only multiples of the block cipher block size.
48  *
49  * CTR provides confidentiality. The using GHASH and encrypting the result provides
50  * message integrity and authentication.
51  *
52  * The AES key is a shared secret between the two parties and has a length
53  * of 128, 192, or 256 bits.
54  *
55  * The IV is generated by the party performing the authenticated
56  * encryption operation. Within the scope of any authenticated
57  * encryption key, the IV value must be unique. That is, the set of
58  * IV values used with any given key must not contain any duplicate
59  * values. Using the same IV for two different messages encrypted
60  * with the same key destroys the security properties of GCM.
61  *
62  * The optional additional authentication data (AAD) is authenticated
63  * but not encrypted. Thus, the AAD is not included in the AES-GCM output.
64  * It can be used to authenticate packet headers, timestamps and other
65  * metadata.
66  *
67  * After the encryption operation, the ciphertext contains the encrypted
68  * data and the message authentication code (MAC).
69  *
70  * GCM is highly performant for an AEAD mode. Counter with CBC-MAC requires
71  * one invocation per block of AAD and two invocations of the block cipher
72  * per proccessed block of input; one to compute the CBC-MAC and one to
73  * perform CTR. GCM substitutes the block cipher invocation during CBC-MAC
74  * computation with computing GHASH over the same input. GHASH is significantly
75  * faster per block than AES. In turn, this gives GCM a performance edge
76  * over CCM.
77  *
78  * ### Security Considerations
79  *
80  * In each operation, GCM limits the length of the input and AAD to guarantee
81  * its security properties:
82  * - inputLength <= 2^36 - 32 bytes
83  * - aadLength <= 2^61 - 1 bytes
84  *
85  * The security properties of GCM rely on the MAC size. While MAC lengths of
86  * [4, 8, 12, 13, 14, 15, 16] bytes are permitted, it is recommended to
87  * use the full 16-byte MAC.
88  *
89  * See NIST SP 800-38D for more a more detailed discussion of security
90  * considerations.
91  *
92  * @anchor ti_drivers_AESGCM_Usage
93  * ### Usage #
94  *
95  * #### Before starting a GCM operation #
96  *
97  * Before starting a GCM operation, the application must do the following:
98  * - Call AESGCM_init() to initialize the driver
99  * - Call AESGCM_Params_init() to initialize the #AESGCM_Params to default values.
100  * - Modify the #AESGCM_Params as desired
101  * - Call AESGCM_open() to open an instance of the driver
102  * - Initialize a CryptoKey. These opaque datastructures are representations
103  * of keying material and its storage. Depending on how the keying material
104  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
105  * initialized differently. The AESGCM API can handle all types of CryptoKey.
106  * However, not all device-specific implementions support all types of CryptoKey.
107  * Devices without a key store will not support CryptoKeys with keying material
108  * stored in a key store for example.
109  * All devices support plaintext CryptoKeys.
110  * - Initialise the #AESGCM_Operation using AESGCM_Operation_init() and set all
111  * length, key, and buffer fields.
112  *
113  * #### Starting a GCM operation #
114  *
115  * The AESGCM_oneStepEncrypt() and AESGCM_oneStepDecrypt() functions perform a GCM operation in a single call.
116  *
117  * When performing a decryption operation with AESGCM_oneStepDecrypt(), the MAC is
118  * automatically verified.
119  *
120  * #### After the GCM operation completes #
121  *
122  * After the GCM operation completes, the application should either start another operation
123  * or close the driver by calling AESGCM_close()
124  *
125  * @anchor ti_drivers_AESGCM_Synopsis
126  * ## Synopsis
127  *
128  * @anchor ti_drivers_AESGCM_Synopsis_Code
129  * @code
130  *
131  * // Import AESGCM Driver definitions
132  * #include <ti/drivers/AESGCM.h>
133  *
134  * // Define name for AESGCM channel index
135  * #define AESGCM_INSTANCE 0
136  *
137  * AESGCM_init();
138  *
139  * handle = AESGCM_open(AESGCM_INSTANCE, NULL);
140  *
141  * // Initialize symmetric key
142  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
143  *
144  * // Set up AESGCM_Operation
145  * AESGCM_Operation_init(&operation);
146  * operation.key = &cryptoKey;
147  * operation.aad = aad;
148  * operation.aadLength = sizeof(aad);
149  * operation.input = plaintext;
150  * operation.output = ciphertext;
151  * operation.inputLength = sizeof(plaintext);
152  * operation.iv = iv;
153  * operation.ivLength = sizeof(iv);
154  * operation.mac = mac;
155  * operation.macLength = sizeof(mac);
156  *
157  * encryptionResult = AESGCM_oneStepEncrypt(handle, &operation);
158  *
159  * AESGCM_close(handle);
160  * @endcode
161  *
162  * @anchor ti_drivers_AESGCM_Examples
163  * #### Examples
164  *
165  * ##### Single call GCM encryption + authentication with plaintext CryptoKey in blocking return mode #
166  *
167  * @code
168  *
169  * #include <ti/drivers/AESGCM.h>
170  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
171  *
172  * ...
173  *
174  * AESGCM_Handle handle;
175  * CryptoKey cryptoKey;
176  * int_fast16_t encryptionResult;
177  * uint8_t iv[12] = "12-byte IV ";
178  * uint8_t aad[] = "This string will be authenticated but not encrypted.";
179  * uint8_t plaintext[] = "This string will be encrypted and authenticated.";
180  * uint8_t mac[16];
181  * uint8_t ciphertext[sizeof(plaintext)];
182  * uint8_t keyingMaterial[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
183  * 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
184  * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
185  * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
186  *
187  * handle = AESGCM_open(0, NULL);
188  *
189  * if (handle == NULL) {
190  * // handle error
191  * }
192  *
193  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
194  *
195  * AESGCM_Operation operation;
196  * AESGCM_Operation_init(&operation);
197  *
198  * operation.key = &cryptoKey;
199  * operation.aad = aad;
200  * operation.aadLength = sizeof(aad);
201  * operation.input = plaintext;
202  * operation.output = ciphertext;
203  * operation.inputLength = sizeof(plaintext);
204  * operation.iv = iv;
205  * operation.ivLength = 12;
206  * operation.mac = mac;
207  * operation.macLength = sizeof(mac);
208  *
209  * encryptionResult = AESGCM_oneStepEncrypt(handle, &operation);
210  *
211  * if (encryptionResult != AESGCM_STATUS_SUCCESS) {
212  * // handle error
213  * }
214  *
215  * AESGCM_close(handle);
216  *
217  * @endcode
218  *
219  * ##### Single call GCM decryption + verification with plaintext CryptoKey in callback return mode #
220  *
221  * @code
222  *
223  * #include <ti/drivers/AESGCM.h>
224  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
225  *
226  * ...
227  *
228  * // The following test vector is Packet Vector 1 from RFC 3610 of the IETF.
229  *
230  * uint8_t iv[] = {0x1f, 0x80, 0x3c, 0x52, 0xca, 0xc4, 0x97, 0xe1,
231  * 0x55, 0xaa, 0x55, 0x2d};
232  * uint8_t aad[] = {0x3b, 0xba, 0x31, 0x28, 0x9d, 0x05, 0xf5, 0x0f,
233  * 0xed, 0x6c, 0x53, 0x35, 0x3c, 0x1f, 0x74, 0xd8,
234  * 0x28, 0xa9, 0x96, 0xb8, 0xd6, 0x84, 0xfe, 0x64,
235  * 0x7f, 0x7c, 0x40, 0xc0, 0xd5, 0x68, 0x8c, 0x89,
236  * 0x68, 0x1a, 0x33, 0xb1, 0x0c, 0xb7, 0x14, 0xb6,
237  * 0x49, 0x0b, 0xdf, 0x1f, 0x16, 0x60, 0x60, 0xa7};
238  * uint8_t mac[] = {0x39, 0x03, 0xe4, 0xdc, 0xa4, 0xe7, 0xc8, 0x21,
239  * 0x62, 0x1a, 0xbb, 0xb2, 0x37, 0x2c, 0x97};
240  * uint8_t ciphertext[] = {0xf8, 0x7e, 0xf7, 0x99, 0x4a, 0x86, 0xf3, 0xe9,
241  * 0xa3, 0xab, 0x6a, 0x6f, 0x2d, 0x34, 0x3b, 0xbd};
242  * uint8_t keyingMaterial[] = {0x4f, 0xd7, 0xf2, 0x09, 0xdf, 0xb0, 0xdf, 0xbd,
243  * 0xd9, 0x8d, 0x2d, 0xb4, 0x98, 0x66, 0x4c, 0x88};
244  * uint8_t plaintext[sizeof(ciphertext)];
245  *
246  * // The plaintext should be the following after the decryption operation:
247  * // 0x17, 0x9d, 0xcb, 0x79, 0x5c, 0x09, 0x8f, 0xc5, 0x31, 0x4b, 0xde, 0x0d, 0x39, 0x9d, 0x7a, 0x10
248  *
249  *
250  * void gcmCallback(AESGCM_Handle handle,
251  * int_fast16_t returnValue,
252  * AESGCM_Operation *operation,
253  * AESGCM_OperationType operationType) {
254  *
255  * if (returnValue != AESGCM_STATUS_SUCCESS) {
256  * // handle error
257  * }
258  * }
259  *
260  * AESGCM_Operation operation;
261  * CryptoKey cryptoKey;
262  *
263  * void gcmStartFunction(void) {
264  * AESGCM_Handle handle;
265  * AESGCM_Params params;
266  * int_fast16_t decryptionResult;
267  *
268  * AESGCM_Params_init(&params);
269  * params.returnBehavior = AESGCM_RETURN_BEHAVIOR_CALLBACK;
270  * params.callbackFxn = gcmCallback;
271  *
272  * handle = AESGCM_open(0, &params);
273  *
274  * if (handle == NULL) {
275  * // handle error
276  * }
277  *
278  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
279  *
280  * AESGCM_Operation_init(&operation);
281  *
282  * operation.key = &cryptoKey;
283  * operation.aad = aad;
284  * operation.aadLength = sizeof(aad);
285  * operation.input = ciphertext;
286  * operation.output = plaintext;
287  * operation.inputLength = sizeof(ciphertext);
288  * operation.iv = iv;
289  * operation.mac = mac;
290  * operation.macLength = sizeof(mac);
291  *
292  * decryptionResult = AESGCM_oneStepDecrypt(handle, &operation);
293  *
294  * if (decryptionResult != AESGCM_STATUS_SUCCESS) {
295  * // handle error
296  * }
297  *
298  * // do other things while GCM operation completes in the background
299  *
300  * }
301  * @endcode
302  */
303 
304 #ifndef ti_drivers_AESGCM__include
305 #define ti_drivers_AESGCM__include
306 
307 #include <stdbool.h>
308 #include <stddef.h>
309 #include <stdint.h>
310 
312 
313 #ifdef __cplusplus
314 extern "C" {
315 #endif
316 
329 #define AESGCM_STATUS_RESERVED (-32)
330 
337 #define AESGCM_STATUS_SUCCESS (0)
338 
345 #define AESGCM_STATUS_ERROR (-1)
346 
355 #define AESGCM_STATUS_RESOURCE_UNAVAILABLE (-2)
356 
364 #define AESGCM_STATUS_MAC_INVALID (-3)
365 
369 #define AESGCM_STATUS_CANCELED (-4)
370 
382 typedef struct {
384  void *object;
385 
387  void const *hwAttrs;
388 } AESGCM_Config;
389 
394 
416 typedef enum {
432 
436 typedef enum {
439 } AESGCM_Mode;
440 
445 typedef struct {
447  uint8_t *aad;
451  uint8_t *input;
456  uint8_t *output;
462  uint8_t *iv;
468  uint8_t *mac;
474  size_t aadLength;
478  size_t inputLength;
482  uint8_t ivLength;
485  uint8_t macLength;
493 
497 typedef enum {
501 
517 typedef void (*AESGCM_CallbackFxn) (AESGCM_Handle handle,
518  int_fast16_t returnValue,
519  AESGCM_Operation *operation,
520  AESGCM_OperationType operationType);
521 
530 typedef struct {
533  uint32_t timeout;
536  void *custom;
539 } AESGCM_Params;
540 
547 
556 void AESGCM_init(void);
557 
571 
589 AESGCM_Handle AESGCM_open(uint_least8_t index, const AESGCM_Params *params);
590 
600 void AESGCM_close(AESGCM_Handle handle);
601 
610 void AESGCM_Operation_init(AESGCM_Operation *operationStruct);
611 
631 int_fast16_t AESGCM_oneStepEncrypt(AESGCM_Handle handle, AESGCM_Operation *operationStruct);
632 
653 int_fast16_t AESGCM_oneStepDecrypt(AESGCM_Handle handle, AESGCM_Operation *operationStruct);
654 
668 int_fast16_t AESGCM_cancelOperation(AESGCM_Handle handle);
669 
693 AESGCM_Handle AESGCM_construct(AESGCM_Config *config, const AESGCM_Params *params);
694 
695 #ifdef __cplusplus
696 }
697 #endif
698 
699 #endif /* ti_drivers_AESGCM__include */
ADC_Params params
Definition: Driver_Init.h:11
void * custom
Definition: AESGCM.h:536
The CryptoKey type is an opaque representation of a cryptographic key.
uint8_t * input
Definition: AESGCM.h:451
uint8_t macLength
Definition: AESGCM.h:485
CryptoKey * key
Definition: AESGCM.h:446
AESGCM_OperationType
Enum for the operation types supported by the driver.
Definition: AESGCM.h:497
int_fast16_t AESGCM_cancelOperation(AESGCM_Handle handle)
Cancels an ongoing AESGCM operation.
const AESGCM_Params AESGCM_defaultParams
Default AESGCM_Params structure.
uint8_t ivLength
Definition: AESGCM.h:482
Definition: AESGCM.h:417
void * object
Definition: AESGCM.h:384
CryptoKey datastructure.
Definition: CryptoKey.h:209
int_fast16_t AESGCM_oneStepEncrypt(AESGCM_Handle handle, AESGCM_Operation *operationStruct)
Function to perform an AESGCM encryption + authentication operation in one call.
void AESGCM_Operation_init(AESGCM_Operation *operationStruct)
Function to initialize an AESGCM_Operation struct to its defaults.
AESGCM_ReturnBehavior returnBehavior
Definition: AESGCM.h:531
void AESGCM_init(void)
This function initializes the GCM module.
uint8_t * mac
Definition: AESGCM.h:468
int_fast16_t AESGCM_oneStepDecrypt(AESGCM_Handle handle, AESGCM_Operation *operationStruct)
Function to perform an AESGCM decryption + verification operation in one call.
AESGCM Global configuration.
Definition: AESGCM.h:382
size_t aadLength
Definition: AESGCM.h:474
void(* AESGCM_CallbackFxn)(AESGCM_Handle handle, int_fast16_t returnValue, AESGCM_Operation *operation, AESGCM_OperationType operationType)
The definition of a callback function used by the AESGCM driver when used in AESGCM_RETURN_BEHAVIOR_C...
Definition: AESGCM.h:517
Definition: AESGCM.h:499
AESGCM_ReturnBehavior
The way in which GCM function calls return after performing an encryption + authentication or decrypt...
Definition: AESGCM.h:416
Definition: AESGCM.h:438
Definition: AESGCM.h:427
uint8_t * output
Definition: AESGCM.h:456
Definition: AESGCM.h:498
uint32_t timeout
Definition: AESGCM.h:533
void AESGCM_close(AESGCM_Handle handle)
Function to close a GCM peripheral specified by the GCM handle.
AESGCM_Handle AESGCM_construct(AESGCM_Config *config, const AESGCM_Params *params)
Constructs a new AESGCM object.
AESGCM_Config * AESGCM_Handle
A handle that is returned from an AESGCM_open() call.
Definition: AESGCM.h:393
void AESGCM_Params_init(AESGCM_Params *params)
Function to initialize the AESGCM_Params struct to its defaults.
uint8_t * aad
Definition: AESGCM.h:447
AESGCM_Handle AESGCM_open(uint_least8_t index, const AESGCM_Params *params)
This function opens a given GCM peripheral.
AESGCM_Mode
Enum for the direction of the GCM operation.
Definition: AESGCM.h:436
GCM Parameters.
Definition: AESGCM.h:530
size_t inputLength
Definition: AESGCM.h:478
Definition: AESGCM.h:423
bool ivInternallyGenerated
Definition: AESGCM.h:488
uint8_t * iv
Definition: AESGCM.h:462
Definition: AESGCM.h:437
AESGCM_CallbackFxn callbackFxn
Definition: AESGCM.h:532
void const * hwAttrs
Definition: AESGCM.h:387
Struct containing the parameters required for encrypting/decrypting and authenticating/verifying a me...
Definition: AESGCM.h:445
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale