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