AESCCM.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 AESCCM.h
34  *
35  * @brief AESCCM driver header
36  *
37  * @anchor ti_drivers_AESCCM_Overview
38  * # Overview #
39  * The Counter with CBC-MAC (CCM) mode of operation is a generic
40  * authenticated encryption block cipher mode. It can be used with
41  * any block cipher.
42  * AESCCM combines CBC-MAC with an AES block cipher in CTR mode of operation.
43  *
44  * This combination of block cipher modes enables CCM to encrypt messages of any
45  * length and not only multiples of the block cipher block size.
46  *
47  * CTR provides confidentiality. The defined application of CBC-MAC provides
48  * message integrity and authentication.
49  *
50  * AESCCM has the following inputs and outputs:
51  *
52  * <table>
53  * <caption id="AESCCM_multi_row">AES-CCM input and output parameters</caption>
54  * <tr><th>Encryption</th><th>Decryption</th></tr>
55  * <tr><th colspan=2>Input</th></tr>
56  * <tr><td>Shared AES key</td><td> Shared AES key</td></tr>
57  * <tr><td>Nonce</td><td>Nonce</td></tr>
58  * <tr><td>Cleartext</td><td>Ciphertext</td></tr>
59  * <tr><td></td><td>MAC</td></tr>
60  * <tr><td>AAD (optional)</td><td>AAD (optional)</td></tr>
61  * <tr><th colspan=2>Output</th></tr>
62  * <tr><td>Ciphertext</td><td>Cleartext</td></tr>
63  * <tr><td>MAC</td><td></td></tr>
64  * </table>
65  *
66  * The AES key is a shared secret between the two parties and has a length
67  * between 128, 192, or 256 bits.
68  *
69  * The nonce is generated by the party performing the authenticated
70  * encryption operation. Within the scope of any authenticated
71  * encryption key, the nonce value must be unique. That is, the set of
72  * nonce values used with any given key must not contain any duplicate
73  * values. Using the same nonce for two different messages encrypted
74  * with the same key destroys the security properties.
75  *
76  * The length of the nonce determines the maximum number of messages that may
77  * be encrypted and authenticated before you must regenerate the key.
78  * Reasonable session key rotation schemes will regenerate the key before reaching
79  * this limit.
80  * There is a trade-off between the nonce-length and the maximum length of
81  * the plaintext to encrypt and authenticate per nonce. This is because
82  * CTR concatenates the nonce and an internal counter into one 16-byte
83  * IV. The counter is incremented after generating an AES-block-sized
84  * pseudo-random bitstream. This bitstream is XOR'd with the plaintext.
85  * The counter would eventually roll over for a sufficiently long message.
86  * This is must not happen. Hence, the longer the nonce and the more messages
87  * you can send before needing to rotate the key, the shorter the
88  * lengths of invidual messages sent may be. The minimum and maximum
89  * nonce length defined by the CCM standard provide for both a reasonable
90  * number of messages before key rotation and a reasonable maximum message length.
91  * Check NIST SP 800-38C for details.
92  *
93  * The optional additional authentication data (AAD) is authenticated
94  * but not encrypted. Thus, the AAD is not included in the AES-CCM output.
95  * It can be used to authenticate packet headers.
96  *
97  * After the encryption operation, the ciphertext contains the encrypted
98  * data. The message authentication code (MAC) is also provided.
99  *
100  * # CCM Variations #
101  * The AESCCM driver supports both classic CCM as defined by NIST SP 800-38C and
102  * the CCM* variant used in IEEE 802.15.4.
103  * CCM* allows for unauthenticated encryption using CCM by permitting a MAC length
104  * of 0. It also imposes the requirement that the MAC length be embedded in
105  * the nonce used for each message if the MAC length varies within the protocol
106  * using CCM*.
107  *
108  * @anchor ti_drivers_AESCCM_Usage
109  * # Usage #
110  *
111  * ## Before starting a CCM operation #
112  *
113  * Before starting a CCM operation, the application must do the following:
114  * - Call AESCCM_init() to initialize the driver
115  * - Call AESCCM_Params_init() to initialize the AESCCM_Params to default values.
116  * - Modify the AESCCM_Params as desired
117  * - Call AESCCM_open() to open an instance of the driver
118  * - Initialize a CryptoKey. These opaque datastructures are representations
119  * of keying material and its storage. Depending on how the keying material
120  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
121  * initialized differently. The AESCCM API can handle all types of CryptoKey.
122  * However, not all device-specific implementions support all types of CryptoKey.
123  * Devices without a key store will not support CryptoKeys with keying material
124  * stored in a key store for example.
125  * All devices support plaintext CryptoKeys.
126  * - Initialise the AESCCM_Operation using AESCCM_Operation_init() and set all
127  * length, key, and buffer fields.
128  *
129  * ## Starting a CCM operation #
130  *
131  * The AESCCM_oneStepEncrypt and AESCCM_oneStepDecrypt functions do a CCM operation in a single call.
132  * They will always be the most highly optimized routines with the least overhead and the fastest
133  * runtime. However, they require all AAD and plaintext or ciphertext data to be
134  * available to the function at the start of the call.
135  * All devices support single call operations.
136  *
137  * When performing a decryption operation with AESCCM_oneStepDecrypt(), the MAC is
138  * automatically verified.
139  *
140  * ## After the CCM operation completes #
141  *
142  * After the CCM operation completes, the application should either start another operation
143  * or close the driver by calling AESCCM_close()
144  *
145  * @anchor ti_drivers_AESCCM_Synopsis
146  * ## Synopsis
147  *
148  * @anchor ti_drivers_AESCCM_Synopsis_Code
149  * @code
150  *
151  * // Import AESCCM Driver definitions
152  * #include <ti/drivers/AESCCM.h>
153  *
154  * // Define name for AESCCM channel index
155  * #define AESCCM_INSTANCE 0
156  *
157  * AESCCM_init();
158  *
159  * handle = AESCCM_open(AESCCM_INSTANCE, NULL);
160  *
161  * // Initialize symmetric key
162  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
163  *
164  * // Set up AESCCM_Operation
165  * AESCCM_Operation_init(&operation);
166  * operation.key = &cryptoKey;
167  * operation.aad = aad;
168  * operation.aadLength = sizeof(aad);
169  * operation.input = plaintext;
170  * operation.output = ciphertext;
171  * operation.inputLength = sizeof(plaintext);
172  * operation.nonce = nonce;
173  * operation.nonceLength = sizeof(nonce);
174  * operation.mac = mac;
175  * operation.macLength = sizeof(mac);
176  *
177  * encryptionResult = AESCCM_oneStepEncrypt(handle, &operation);
178  *
179  * AESCCM_close(handle);
180  * @endcode
181  *
182  * @anchor ti_drivers_AESCCM_Examples
183  * ## Examples
184  *
185  * ### Single call CCM encryption + authentication with plaintext CryptoKey in blocking return mode #
186  * @code
187  *
188  * #include <ti/drivers/AESCCM.h>
189  * #include <ti/drivers/types/cryptoKey/CryptoKey_Plaintext.h>
190  *
191  * ...
192  *
193  * AESCCM_Handle handle;
194  * CryptoKey cryptoKey;
195  * int_fast16_t encryptionResult;
196  * uint8_t nonce[] = "Thisisanonce";
197  * uint8_t aad[] = "This string will be authenticated but not encrypted.";
198  * uint8_t plaintext[] = "This string will be encrypted and authenticated.";
199  * uint8_t mac[16];
200  * uint8_t ciphertext[sizeof(plaintext)];
201  * uint8_t keyingMaterial[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
202  * 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
203  * 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
204  * 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}
205  *
206  * handle = AESCCM_open(0, NULL);
207  *
208  * if (handle == NULL) {
209  * // handle error
210  * }
211  *
212  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
213  *
214  * AESCCM_Operation operation;
215  * AESCCM_Operation_init(&operation);
216  *
217  * operation.key = &cryptoKey;
218  * operation.aad = aad;
219  * operation.aadLength = sizeof(aad);
220  * operation.input = plaintext;
221  * operation.output = ciphertext;
222  * operation.inputLength = sizeof(plaintext);
223  * operation.nonce = nonce;
224  * operation.nonceLength = sizeof(nonce);
225  * operation.mac = mac;
226  * operation.macLength = sizeof(mac);
227  *
228  * encryptionResult = AESCCM_oneStepEncrypt(handle, &operation);
229  *
230  * if (encryptionResult != AESCCM_STATUS_SUCCESS) {
231  * // handle error
232  * }
233  *
234  * AESCCM_close(handle);
235  *
236  * @endcode
237  *
238  * ### Single call CCM decryption + verification with plaintext CryptoKey in callback return mode #
239  * @code
240  *
241  * #include <ti/drivers/AESCCM.h>
242  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
243  *
244  * ...
245  *
246  * // The following test vector is Packet Vector 1 from RFC 3610 of the IETF.
247  *
248  * uint8_t nonce[] = {0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xA0,
249  * 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
250  * uint8_t aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
251  * uint8_t mac[] = {0x17, 0xE8, 0xD1, 0x2C, 0xFD, 0xF9, 0x26, 0xE0};
252  * uint8_t ciphertext[] = {0x58, 0x8C, 0x97, 0x9A, 0x61, 0xC6, 0x63, 0xD2,
253  * 0xF0, 0x66, 0xD0, 0xC2, 0xC0, 0xF9, 0x89, 0x80,
254  * 0x6D, 0x5F, 0x6B, 0x61, 0xDA, 0xC3, 0x84};
255  * uint8_t keyingMaterial[] = {0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
256  * 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF};
257  * uint8_t plaintext[sizeof(ciphertext)];
258  *
259  * // The plaintext should be the following after the decryption operation:
260  * // {0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
261  * // 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
262  * // 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E}
263  *
264  *
265  * void ccmCallback(AESCCM_Handle handle,
266  * int_fast16_t returnValue,
267  * AESCCM_Operation *operation,
268  * AESCCM_OperationType operationType) {
269  *
270  * if (returnValue != AESCCM_STATUS_SUCCESS) {
271  * // handle error
272  * }
273  * }
274  *
275  * AESCCM_Operation operation;
276  *
277  * void ccmStartFunction(void) {
278  * AESCCM_Handle handle;
279  * AESCCM_Params params;
280  * CryptoKey cryptoKey;
281  * int_fast16_t decryptionResult;
282  *
283  * AESCCM_Params_init(&params);
284  * params.returnBehavior = AESCCM_RETURN_BEHAVIOR_CALLBACK;
285  * params.callbackFxn = ccmCallback;
286  *
287  * handle = AESCCM_open(0, &params);
288  *
289  * if (handle == NULL) {
290  * // handle error
291  * }
292  *
293  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
294  *
295  * AESCCM_Operation_init(&operation);
296  *
297  * operation.key = &cryptoKey;
298  * operation.aad = aad;
299  * operation.aadLength = sizeof(aad);
300  * operation.input = plaintext;
301  * operation.output = ciphertext;
302  * operation.inputLength = sizeof(plaintext);
303  * operation.nonce = nonce;
304  * operation.nonceLength = sizeof(nonce);
305  * operation.mac = mac;
306  * operation.macLength = sizeof(mac);
307  *
308  * decryptionResult = AESCCM_oneStepDecrypt(handle, &operation);
309  *
310  * if (decryptionResult != AESCCM_STATUS_SUCCESS) {
311  * // handle error
312  * }
313  *
314  * // do other things while CCM operation completes in the background
315  *
316  * }
317  *
318  *
319  * @endcode
320  */
321 
322 #ifndef ti_drivers_AESCCM__include
323 #define ti_drivers_AESCCM__include
324 
325 #include <stdbool.h>
326 #include <stddef.h>
327 #include <stdint.h>
328 
330 
331 #ifdef __cplusplus
332 extern "C" {
333 #endif
334 
347 #define AESCCM_STATUS_RESERVED (-32)
348 
355 #define AESCCM_STATUS_SUCCESS (0)
356 
363 #define AESCCM_STATUS_ERROR (-1)
364 
373 #define AESCCM_STATUS_RESOURCE_UNAVAILABLE (-2)
374 
382 #define AESCCM_STATUS_MAC_INVALID (-3)
383 
387 #define AESCCM_STATUS_CANCELED (-4)
388 
400 typedef struct {
402  void *object;
403 
405  void const *hwAttrs;
406 } AESCCM_Config;
407 
412 
434 typedef enum {
450 
454 typedef enum {
457 } AESCCM_Mode;
458 
463 typedef struct {
465  uint8_t *aad;
469  uint8_t *input;
474  uint8_t *output;
480  uint8_t *nonce;
486  uint8_t *mac;
492  size_t aadLength;
496  size_t inputLength;
500  uint8_t nonceLength;
503  uint8_t macLength;
513 
517 typedef enum {
521 
537 typedef void (*AESCCM_CallbackFxn) (AESCCM_Handle handle,
538  int_fast16_t returnValue,
539  AESCCM_Operation *operation,
540  AESCCM_OperationType operationType);
541 
550 typedef struct {
553  uint32_t timeout;
556  void *custom;
559 } AESCCM_Params;
560 
567 
576 void AESCCM_init(void);
577 
591 
609 AESCCM_Handle AESCCM_open(uint_least8_t index, const AESCCM_Params *params);
610 
620 void AESCCM_close(AESCCM_Handle handle);
621 
630 void AESCCM_Operation_init(AESCCM_Operation *operationStruct);
631 
651 int_fast16_t AESCCM_oneStepEncrypt(AESCCM_Handle handle, AESCCM_Operation *operationStruct);
652 
673 int_fast16_t AESCCM_oneStepDecrypt(AESCCM_Handle handle, AESCCM_Operation *operationStruct);
674 
688 int_fast16_t AESCCM_cancelOperation(AESCCM_Handle handle);
689 
713 AESCCM_Handle AESCCM_construct(AESCCM_Config *config, const AESCCM_Params *params);
714 
715 #ifdef __cplusplus
716 }
717 #endif
718 
719 #endif /* ti_drivers_AESCCM__include */
CryptoKey * key
Definition: AESCCM.h:464
ADC_Params params
Definition: Driver_Init.h:11
The CryptoKey type is an opaque representation of a cryptographic key.
Struct containing the parameters required for encrypting/decrypting and authenticating/verifying a me...
Definition: AESCCM.h:463
uint8_t macLength
Definition: AESCCM.h:503
void * object
Definition: AESCCM.h:402
int_fast16_t AESCCM_oneStepDecrypt(AESCCM_Handle handle, AESCCM_Operation *operationStruct)
Function to perform an AESCCM decryption + verification operation in one call.
uint32_t timeout
Definition: AESCCM.h:553
void * custom
Definition: AESCCM.h:556
Definition: AESCCM.h:441
AESCCM_Handle AESCCM_open(uint_least8_t index, const AESCCM_Params *params)
This function opens a given CCM peripheral.
const AESCCM_Params AESCCM_defaultParams
Default AESCCM_Params structure.
Definition: AESCCM.h:456
size_t aadLength
Definition: AESCCM.h:492
uint8_t * output
Definition: AESCCM.h:474
bool nonceInternallyGenerated
Definition: AESCCM.h:508
Definition: AESCCM.h:519
CCM Parameters.
Definition: AESCCM.h:550
CryptoKey datastructure.
Definition: CryptoKey.h:209
int_fast16_t AESCCM_oneStepEncrypt(AESCCM_Handle handle, AESCCM_Operation *operationStruct)
Function to perform an AESCCM encryption + authentication operation in one call.
int_fast16_t AESCCM_cancelOperation(AESCCM_Handle handle)
Cancels an ongoing AESCCM operation.
uint8_t * input
Definition: AESCCM.h:469
Definition: AESCCM.h:435
void AESCCM_Params_init(AESCCM_Params *params)
Function to initialize the AESCCM_Params struct to its defaults.
uint8_t * aad
Definition: AESCCM.h:465
Definition: AESCCM.h:445
void const * hwAttrs
Definition: AESCCM.h:405
uint8_t * nonce
Definition: AESCCM.h:480
AESCCM_Handle AESCCM_construct(AESCCM_Config *config, const AESCCM_Params *params)
Constructs a new AESCCM object.
AESCCM_ReturnBehavior returnBehavior
Definition: AESCCM.h:551
void AESCCM_Operation_init(AESCCM_Operation *operationStruct)
Function to initialize an AESCCM_Operation struct to its defaults.
AESCCM Global configuration.
Definition: AESCCM.h:400
void(* AESCCM_CallbackFxn)(AESCCM_Handle handle, int_fast16_t returnValue, AESCCM_Operation *operation, AESCCM_OperationType operationType)
The definition of a callback function used by the AESCCM driver when used in AESCCM_RETURN_BEHAVIOR_C...
Definition: AESCCM.h:537
void AESCCM_close(AESCCM_Handle handle)
Function to close a CCM peripheral specified by the CCM handle.
AESCCM_CallbackFxn callbackFxn
Definition: AESCCM.h:552
AESCCM_Mode
Enum for the direction of the CCM operation.
Definition: AESCCM.h:454
AESCCM_OperationType
Enum for the operation types supported by the driver.
Definition: AESCCM.h:517
uint8_t nonceLength
Definition: AESCCM.h:500
AESCCM_Config * AESCCM_Handle
A handle that is returned from an AESCCM_open() call.
Definition: AESCCM.h:411
AESCCM_ReturnBehavior
The way in which CCM function calls return after performing an encryption + authentication or decrypt...
Definition: AESCCM.h:434
Definition: AESCCM.h:455
void AESCCM_init(void)
This function initializes the CCM module.
Definition: AESCCM.h:518
size_t inputLength
Definition: AESCCM.h:496
uint8_t * mac
Definition: AESCCM.h:486
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale