AESCBC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2024, 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 AESCBC.h
34  *
35  * @brief AESCBC driver header
36  *
37  * @anchor ti_drivers_AESCBC_Overview
38  * # Overview #
39  * The Cipher Block Chaining (CBC) mode of operation is a generic
40  * block cipher mode of operation. It can be used with any block cipher
41  * including AES.
42  *
43  * CBC mode encrypts messages of any practical length that have a length
44  * evenly divisible by the block size. Unlike ECB, it guarantees
45  * confidentiality of the entire message when the message is larger than
46  * one block.
47  *
48  * ## Operation #
49  * In CBC encryption, the initialization vector (IV) is XOR'd with a block of
50  * plaintext and then encrypted. The output ciphertext block is then XOR'd with
51  * the next plaintext block and the result is encrypted. This process is repeated
52  * until the final block of plaintext has been encrypted.
53  *
54  * To decrypt the message, decrypt the first block of ciphertext and XOR the result
55  * with the IV. The result is the first plaintext block. For subsequent ciphertext
56  * blocks, decrypt each block and XOR the previous block of the encrypted message
57  * into the result.
58  *
59  * ## Padding #
60  * CBC operates on entire blocks of ciphertext and plaintext at a time. This
61  * means that message lengths must be a multiple of the block cipher block size.
62  * AES has a block size of 16 bytes no matter the key size. Since messages do
63  * not necessarily always have a length that is a multiple of 16 bytes, it may
64  * be necessary to pad the message to a 16-byte boundary. Padding requires
65  * the sender and receiver to implicitly agree on the padding convention.
66  * Improperly designed or implemented padding schemes may leak information
67  * to an attacker through a padding oracle attack for example.
68  *
69  * ## Initialization Vectors #
70  * The IV is generated by the party performing the encryption operation.
71  * Within the scope of any encryption key, the IV value must be unique.
72  * The IV does not need to be kept secret and is usually transmitted together
73  * with the ciphertext to the decrypting party.
74  * In CBC mode, the IVs must not be predictable. Two recommended ways to
75  * generate IVs is to either:
76  *
77  * - Apply the block cipher (AESCBC), using the same key used with CBC,
78  * to a nonce. This nonce must be unique for each key-message pair.
79  * A counter will usually suffice. If the same symmetric key is used
80  * by both parties to encrypt messages, they should agree to use a
81  * nonce scheme that avoids generating the same nonce and thus IV twice.
82  * Incrementing the counter by two and making one party use even numbers
83  * and the other odd numbers is a common method to avoid such collisions.
84  * - Use a TRNG (True Random Number Generator) or PRNG
85  * (Pseudo-Random Number Generator) to generate a random number for use
86  * as IV.
87  *
88  * ## Drawbacks #
89  * CBC mode has several drawbacks. Unless interfacing with legacy devices,
90  * it is recommended to use an AEAD (Authenticated Encryption with Associated Data)
91  * mode such as CCM or GCM. Below is a non-exhaustive list of reasons to use
92  * a different block cipher mode of operation.
93  *
94  * - CBC mode does not offer authentication or integrity guarantees. In practice,
95  * this means that attackers can intercept the encrypted message and manipulate
96  * the ciphertext before sending the message on to the receiver. While this
97  * does not break confidentiality and reveal the plaintext, it has enabled several
98  * attacks in the past. This is especially problematic given that changing the
99  * ciphertext of a block will only corrupt the block itself and the subsequent
100  * block of resultant plaintext. This property may be used to manipulate only
101  * certain parts of the message.
102  *
103  * - CBC mode requires message lengths to be evenly divisible by the block size.
104  * This necessitates a padding scheme. Improperly implemented padding schemes
105  * may lead to vulnerabilities that can be exploited by attackers. It often
106  * makes more sense to use a dedicated stream cipher such as CTR (Counter) that
107  * does not have this restriction. CCM and GCM both use CTR for encryption.
108  *
109  * @anchor ti_drivers_AESCBC_Usage
110  * # Usage #
111  * ## Before starting a CBC operation #
112  *
113  * Before starting a CBC operation, the application must do the following:
114  * - Call #AESCBC_init() to initialize the driver
115  * - Call #AESCBC_Params_init() to initialize the #AESCBC_Params to default values.
116  * - Modify the #AESCBC_Params as desired
117  * - Call #AESCBC_open() to open an instance of the driver
118  * - Initialize a CryptoKey. These opaque data structures are representations
119  * of keying material and its storage. Depending on how the keying material
120  * is stored (RAM or flash, key store), the CryptoKey must be
121  * initialized differently. The AESCBC API can handle all types of CryptoKey.
122  * However, not all device-specific implementations 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  * - Initialize the appropriate AESCBC operation struct using the relevant
127  * operation init functions and set all fields. For example, one-step (one-shot
128  * or single call) operations should initialize AESCBC_Operation or
129  * AESCBC_OneStepOperation using AESCBC_Operation_init() or
130  * AESCBC_OneStepOperation_init(). For multi-step (segmented or multiple call)
131  * operations, AESCBC_SegmentedOperation must be initialized and set.
132  *
133  * ## Starting a CBC operation #
134  *
135  * The #AESCBC_oneStepEncrypt and #AESCBC_oneStepDecrypt functions perform a CBC operation
136  * in a single call. They will always be the most highly optimized routines with the
137  * least overhead and the fastest runtime. However, they require all plaintext
138  * or ciphertext to be available to the function at the start of the call.
139  * All devices support single call operations.
140  *
141  * ## After the CBC operation completes #
142  *
143  * After the CBC operation completes, the application should either start
144  * another operation or close the driver by calling #AESCBC_close().
145  *
146  * @anchor ti_drivers_AESCBC_Synopsis
147  * ## Synopsis
148  * @anchor ti_drivers_AESCBC_Synopsis_Code
149  * @code
150  * // Import AESCBC Driver definitions
151  * #include <ti/drivers/AESCBC.h>
152  *
153  * // Define name for AESCBC channel index
154  * #define AESCBC_INSTANCE 0
155  *
156  * AESCBC_init();
157  *
158  * handle = AESCBC_open(AESCBC_INSTANCE, NULL);
159  *
160  * // Initialize symmetric key
161  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
162  *
163  * // Set up AESCBC_Operation
164  * AESCBC_OneStepOperation operation;
165  * AESCBC_OneStepOperation_init(&operation);
166  * operation.key = &cryptoKey;
167  * operation.input = plaintext;
168  * operation.output = ciphertext;
169  * operation.inputLength = sizeof(plaintext);
170  * operation.iv = iv;
171  *
172  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
173  *
174  * AESCBC_close(handle);
175  * @endcode
176  *
177  * @anchor ti_drivers_AESCBC_Examples
178  * ## Examples
179  *
180  * ### Single call CBC encryption with plaintext CryptoKey in blocking return mode #
181  * @code
182  *
183  * #include <ti/drivers/AESCBC.h>
184  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
185  *
186  * ...
187  *
188  * AESCBC_Handle handle;
189  * CryptoKey cryptoKey;
190  * int_fast16_t encryptionResult;
191  *
192  * // For example purposes only. Generate IVs in a non-static way in practice.
193  * // Test vector 0 from NIST CAPV set CBCMMT128
194  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
195  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
196  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
197  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
198  * uint8_t ciphertext[sizeof(plaintext)];
199  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
200  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
201  *
202  * // The ciphertext should be the following after the encryption operation:
203  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
204  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
205  *
206  *
207  * handle = AESCBC_open(0, NULL);
208  *
209  * if (handle == NULL) {
210  * // handle error
211  * }
212  *
213  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
214  *
215  * AESCBC_OneStepOperation operation;
216  * AESCBC_OneStepOperation_init(&operation);
217  *
218  * operation.key = &cryptoKey;
219  * operation.input = plaintext;
220  * operation.output = ciphertext;
221  * operation.inputLength = sizeof(plaintext);
222  * operation.iv = iv;
223  *
224  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
225  *
226  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
227  * // handle error
228  * }
229  *
230  * AESCBC_close(handle);
231  *
232  * @endcode
233  *
234  * <h4> The following code snippet is for CC27XX devices only and leverages the HSM which is a seperate Hardware
235  * Accelerator </h4>
236  *
237  * ### Single call CBC encryption with plaintext CryptoKey in blocking return mode using the HSM accelerator #
238  * @code
239  *
240  * #include <ti/drivers/AESCBC.h>
241  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
242  *
243  * ...
244  *
245  * AESCBC_Handle handle;
246  * CryptoKey cryptoKey;
247  * int_fast16_t encryptionResult;
248  *
249  * // For example purposes only. Generate IVs in a non-static way in practice.
250  * // Test vector 0 from NIST CAPV set CBCMMT128
251  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
252  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
253  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
254  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
255  * uint8_t ciphertext[sizeof(plaintext)];
256  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
257  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
258  *
259  * // The ciphertext should be the following after the encryption operation:
260  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
261  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
262  *
263  *
264  * handle = AESCBC_open(0, NULL);
265  *
266  * if (handle == NULL) {
267  * // handle error
268  * }
269  *
270  * CryptoKeyPlaintextHSM_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
271  *
272  * AESCBC_OneStepOperation operation;
273  * AESCBC_OneStepOperation_init(&operation);
274  *
275  * operation.key = &cryptoKey;
276  * operation.input = plaintext;
277  * operation.output = ciphertext;
278  * operation.inputLength = sizeof(plaintext);
279  * operation.iv = iv;
280  *
281  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
282  *
283  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
284  * // handle error
285  * }
286  *
287  * AESCBC_close(handle);
288  *
289  * @endcode
290  *
291  * ### Single call CBC decryption with plaintext CryptoKey in callback return mode #
292  *
293  * @note The following code example presented uses a 256-bit key. However,
294  * CC13x1/CC26x1 and CC23x0 only support a maximum key size of 128-bits,
295  * so reduction of keyingMaterial would be required.
296  *
297  * @code
298  *
299  * #include <ti/drivers/AESCBC.h>
300  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
301  *
302  * ...
303  *
304  * // Test vector 0 from NIST CAPV set CBCMMT256
305  *
306  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
307  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
308  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
309  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
310  * uint8_t keyingMaterial[32] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
311  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
312  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
313  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
314  * uint8_t plaintext[sizeof(ciphertext)];
315  *
316  * // The plaintext should be the following after the decryption operation:
317  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
318  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
319  *
320  *
321  * void cbcCallback(AESCBC_Handle handle,
322  * int_fast16_t returnValue,
323  * AESCBC_OperationUnion *operation,
324  * AESCBC_OperationType operationType) {
325  *
326  * if (returnValue != AESCBC_STATUS_SUCCESS) {
327  * // handle error
328  * }
329  *
330  * if (operationType == AESCBC_OPERATION_TYPE_DECRYPT ||
331  * operationType == AESCBC_OPERATION_TYPE_ENCRYPT) {
332  * // do something with operation->oneStepOperation
333  * } else {
334  * // do something with operation->segmentedOperation
335  * }
336  * }
337  *
338  * AESCBC_OneStepOperation operation;
339  *
340  * void cbcStartFunction(void) {
341  * AESCBC_Handle handle;
342  * AESCBC_Params params;
343  * CryptoKey cryptoKey;
344  * int_fast16_t decryptionResult;
345  *
346  * AESCBC_Params_init(&params);
347  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
348  * params.callbackFxn = cbcCallback;
349  *
350  * handle = AESCBC_open(0, &params);
351  *
352  * if (handle == NULL) {
353  * // handle error
354  * }
355  *
356  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
357  *
358  * AESCBC_OneStepOperation_init(&operation);
359  *
360  * operation.key = &cryptoKey;
361  * operation.input = ciphertext;
362  * operation.output = plaintext;
363  * operation.inputLength = sizeof(ciphertext);
364  * operation.iv = iv;
365  *
366  * decryptionResult = AESCBC_oneStepDecrypt(handle, &operation);
367  *
368  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
369  * // handle error
370  * }
371  *
372  * // do other things while CBC operation completes in the background
373  * }
374  *
375  * @endcode
376  *
377  * ### Multi-step CBC encryption with plaintext CryptoKey in blocking return mode #
378  * @code
379  *
380  * #include <ti/drivers/AESCBC.h>
381  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
382  *
383  * ...
384  *
385  * #define AES_BLOCK_SIZE 16 // bytes
386  *
387  * AESCBC_Handle handle;
388  * CryptoKey cryptoKey;
389  * int_fast16_t encryptionResult;
390  *
391  * // For example purposes only. Generate IVs in a non-static way in practice.
392  * // Test vector 0 from NIST CAPV set CBCMMT128
393  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
394  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
395  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
396  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
397  * uint8_t ciphertext[sizeof(plaintext)];
398  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
399  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
400  *
401  * // The ciphertext should be the following after the encryption operation:
402  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
403  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
404  *
405  *
406  * handle = AESCBC_open(0, NULL);
407  *
408  * if (handle == NULL) {
409  * // handle error
410  * }
411  *
412  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
413  *
414  * AESCBC_SegmentedOperation operation;
415  * AESCBC_SegmentedOperation_init(&operation);
416  *
417  * operation.input = plaintext;
418  * operation.output = ciphertext;
419  * operation.inputLength = sizeof(plaintext);
420  *
421  * encryptionResult = AESCBC_setupEncrypt(handle, &cryptoKey);
422  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
423  * // handle error
424  * }
425  *
426  * encryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
427  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
428  * // handle error
429  * }
430  *
431  * encryptionResult = AESCBC_addData(handle, &operation);
432  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
433  * // handle error
434  * }
435  *
436  * operation.inputLength = 0;
437  * encryptionResult = AESCBC_finalize(handle, &operation);
438  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
439  * // handle error
440  * }
441  *
442  * AESCBC_close(handle);
443  *
444  * @endcode
445  *
446  * ### Multi-step CBC decryption with plaintext CryptoKey in callback return mode #
447  *
448  * @note The following code example presented uses a 256-bit key. However,
449  * CC13x1/CC26x1 and CC23x0 only support a maximum key size of 128-bits,
450  * so reduction of keyingMaterial would be required.
451  *
452  * @code
453  *
454  * #include <ti/drivers/AESCBC.h>
455  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
456  *
457  * ...
458  *
459  * #define AES_BLOCK_SIZE 16 // bytes
460  *
461  * // Test vector 0 from NIST CAPV set CBCMMT256
462  *
463  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
464  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
465  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
466  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
467  * uint8_t keyingMaterial[32] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
468  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
469  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
470  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
471  * uint8_t plaintext[sizeof(ciphertext)];
472  *
473  * // The plaintext should be the following after the decryption operation:
474  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
475  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
476  *
477  *
478  * void cbcCallback(AESCBC_Handle handle,
479  * int_fast16_t returnValue,
480  * AESCBC_OperationUnion *operation,
481  * AESCBC_OperationType operationType) {
482  *
483  * if (returnValue != AESCBC_STATUS_SUCCESS) {
484  * // handle error
485  * }
486  *
487  * if (operationType == AESCBC_OPERATION_TYPE_DECRYPT ||
488  * operationType == AESCBC_OPERATION_TYPE_ENCRYPT) {
489  * // do something with operation->oneStepOperation
490  * } else {
491  * // do something with operation->segmentedOperation
492  * }
493  * }
494  *
495  * AESCBC_SegmentedOperation operation;
496  *
497  * void cbcStartFunction(void) {
498  * AESCBC_Handle handle;
499  * AESCBC_Params params;
500  * CryptoKey cryptoKey;
501  * int_fast16_t decryptionResult;
502  *
503  * AESCBC_Params_init(&params);
504  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
505  * params.callbackFxn = cbcCallback;
506  *
507  * handle = AESCBC_open(0, &params);
508  *
509  * if (handle == NULL) {
510  * // handle error
511  * }
512  *
513  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
514  *
515  * AESCBC_SegmentedOperation_init(&operation);
516  *
517  * operation.input = ciphertext;
518  * operation.output = plaintext;
519  * operation.inputLength = sizeof(ciphertext);
520  *
521  * decryptionResult = AESCBC_setupDecrypt(handle, &cryptoKey);
522  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
523  * // handle error
524  * }
525  *
526  * decryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
527  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
528  * // handle error
529  * }
530  *
531  * decryptionResult = AESCBC_addData(handle, &operation);
532  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
533  * // handle error
534  * }
535  *
536  * // do other things while CBC operation completes in the background
537  *
538  * operation.inputLength = 0;
539  * decryptionResult = AESCBC_finalize(handle, &operation);
540  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
541  * // handle error
542  * }
543  *
544  * }
545  *
546  * @endcode
547  *
548  * ### Multi-step CBC encryption with plaintext CryptoKey and non-empty finalize in blocking return mode #
549  * @code
550  *
551  * #include <ti/drivers/AESCBC.h>
552  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
553  *
554  * ...
555  *
556  * #define AES_BLOCK_SIZE 16 // bytes
557  *
558  * AESCBC_Handle handle;
559  * CryptoKey cryptoKey;
560  * int_fast16_t encryptionResult;
561  *
562  * // For example purposes only. Generate IVs in a non-static way in practice.
563  * // Test vector 1 from NIST CAPV set CBCMMT128
564  * uint8_t iv[16] = {0xaa, 0xd1, 0x58, 0x3c, 0xd9, 0x13, 0x65, 0xe3,
565  * 0xbb, 0x2f, 0x0c, 0x34, 0x30, 0xd0, 0x65, 0xbb};
566  * uint8_t plaintext[32] = {0x06, 0x8b, 0x25, 0xc7, 0xbf, 0xb1, 0xf8, 0xbd,
567  * 0xd4, 0xcf, 0xc9, 0x08, 0xf6, 0x9d, 0xff, 0xc5,
568  * 0xdd, 0xc7, 0x26, 0xa1, 0x97, 0xf0, 0xe5, 0xf7,
569  * 0x20, 0xf7, 0x30, 0x39, 0x32, 0x79, 0xbe, 0x91};
570  * uint8_t ciphertext[sizeof(plaintext)];
571  * uint8_t keyingMaterial[16] = {0x07, 0x00, 0xd6, 0x03, 0xa1, 0xc5, 0x14, 0xe4,
572  * 0x6b, 0x61, 0x91, 0xba, 0x43, 0x0a, 0x3a, 0x0c};
573  *
574  * // The ciphertext should be the following after the encryption operation:
575  * // 0xc4, 0xdc, 0x61, 0xd9, 0x72, 0x59, 0x67, 0xa3
576  * // 0x02, 0x01, 0x04, 0xa9, 0x73, 0x8f, 0x23, 0x86
577  * // 0x85, 0x27, 0xce, 0x83, 0x9a, 0xab, 0x17, 0x52
578  * // 0xfd, 0x8b, 0xdb, 0x95, 0xa8, 0x2c, 0x4d, 0x00
579  *
580  *
581  * handle = AESCBC_open(0, NULL);
582  *
583  * if (handle == NULL) {
584  * // handle error
585  * }
586  *
587  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
588  *
589  * AESCBC_SegmentedOperation operation;
590  * AESCBC_SegmentedOperation_init(&operation);
591  *
592  * operation.input = plaintext;
593  * operation.output = ciphertext;
594  * // One should pass in data that is a block-sized multiple length (16 bytes)
595  * operation.inputLength = AES_BLOCK_SIZE;
596  *
597  * encryptionResult = AESCBC_setupEncrypt(handle, &cryptoKey);
598  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
599  * // handle error
600  * }
601  *
602  * encryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
603  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
604  * // handle error
605  * }
606  *
607  * encryptionResult = AESCBC_addData(handle, &operation);
608  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
609  * // handle error
610  * }
611  *
612  * operation.input = plaintext + AES_BLOCK_SIZE;
613  * operation.output = ciphertext + AES_BLOCK_SIZE;
614  *
615  * // You can also finalize with more data (non-zero inputLength)
616  * operation.inputLength = sizeof(plaintext) - AES_BLOCK_SIZE;
617  * encryptionResult = AESCBC_finalize(handle, &operation);
618  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
619  * // handle error
620  * }
621  *
622  * AESCBC_close(handle);
623  *
624  * @endcode
625  */
626 
627 #ifndef ti_drivers_AESCBC__include
628 #define ti_drivers_AESCBC__include
629 
630 #include <stdbool.h>
631 #include <stddef.h>
632 #include <stdint.h>
633 
634 #include <ti/drivers/AESCommon.h>
636 
637 #ifdef __cplusplus
638 extern "C" {
639 #endif
640 
653 #define AESCBC_STATUS_RESERVED AES_STATUS_RESERVED
654 
661 #define AESCBC_STATUS_SUCCESS AES_STATUS_SUCCESS
662 
669 #define AESCBC_STATUS_ERROR AES_STATUS_ERROR
670 
679 #define AESCBC_STATUS_RESOURCE_UNAVAILABLE AES_STATUS_RESOURCE_UNAVAILABLE
680 
684 #define AESCBC_STATUS_CANCELED AES_STATUS_CANCELED
685 
693 #define AESCBC_STATUS_FEATURE_NOT_SUPPORTED AES_STATUS_FEATURE_NOT_SUPPORTED
694 
698 #define AESCBC_STATUS_KEYSTORE_INVALID_ID AES_STATUS_KEYSTORE_INVALID_ID
699 
704 #define AESCBC_STATUS_KEYSTORE_GENERIC_ERROR AES_STATUS_KEYSTORE_GENERIC_ERROR
705 
712 #define AESCBC_STATUS_UNALIGNED_IO_NOT_SUPPORTED AES_STATUS_UNALIGNED_IO_NOT_SUPPORTED
713 
726 
730 typedef AESCBC_Config *AESCBC_Handle;
731 
753 typedef enum
754 {
773 
777 typedef enum
778 {
781 } AESCBC_Mode;
782 
787 typedef struct
788 {
790  uint8_t *input;
795  uint8_t *output;
801  uint8_t *iv;
807  size_t inputLength;
821 
827 typedef struct
828 {
829  uint8_t *input;
834  uint8_t *output;
840  size_t inputLength;
849 
858 
864 {
865  AESCBC_OneStepOperation oneStepOperation; /* One-step operation element of the operation union */
866  AESCBC_SegmentedOperation segmentedOperation; /* Segmented operation element of the operation union */
868 
872 typedef enum
873 {
874  AESCBC_OPERATION_TYPE_ENCRYPT = 1, /* Fields 1 and 2 are for backward compatibility */
876  AESCBC_OP_TYPE_ONESTEP_ENCRYPT = 1, /* Names changed to _OP_TYPE_ to avoid MISRA deviation from first 31 chars not
877  being unique */
884 
900 typedef void (*AESCBC_CallbackFxn)(AESCBC_Handle handle,
901  int_fast16_t returnValue,
902  AESCBC_OperationUnion *operation,
903  AESCBC_OperationType operationType);
904 
913 typedef struct
914 {
915  AESCBC_ReturnBehavior returnBehavior;
917  uint32_t timeout;
920  void *custom;
923 } AESCBC_Params;
924 
931 
940 void AESCBC_init(void);
941 
955 
973 AESCBC_Handle AESCBC_open(uint_least8_t index, const AESCBC_Params *params);
974 
984 void AESCBC_close(AESCBC_Handle handle);
985 
997 void AESCBC_Operation_init(AESCBC_Operation *operationStruct);
998 
1008 
1018 
1039 int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct);
1040 
1061 int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct);
1062 
1080 int_fast16_t AESCBC_setupEncrypt(AESCBC_Handle handle, const CryptoKey *key);
1081 
1099 int_fast16_t AESCBC_setupDecrypt(AESCBC_Handle handle, const CryptoKey *key);
1100 
1122 int_fast16_t AESCBC_setIV(AESCBC_Handle handle, const uint8_t *iv, size_t ivLength);
1123 
1150 int_fast16_t AESCBC_generateIV(AESCBC_Handle handle, uint8_t *iv, size_t ivSize, size_t *ivLength);
1151 
1175 int_fast16_t AESCBC_addData(AESCBC_Handle handle, AESCBC_SegmentedOperation *operation);
1176 
1199 int_fast16_t AESCBC_finalize(AESCBC_Handle handle, AESCBC_SegmentedOperation *operation);
1200 
1214 int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle);
1215 
1239 AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params);
1240 
1241 #ifdef __cplusplus
1242 }
1243 #endif
1244 
1245 #endif /* ti_drivers_AESCBC__include */
ADC_Params params
Definition: Driver_Init.h:11
const AESCBC_Params AESCBC_defaultParams
Default AESCBC_Params structure.
int_fast16_t AESCBC_setupEncrypt(AESCBC_Handle handle, const CryptoKey *key)
Function to prepare a segmented AESCBC encryption operation.
Definition: AESCBC.h:767
uint8_t * input
Definition: AESCBC.h:790
The CryptoKey type is an opaque representation of a cryptographic key.
int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct)
Function to perform an AESCBC encryption operation in one call.
Definition: AESCBC.h:762
int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct)
Function to perform an AESCBC decryption operation in one call.
AESCBC_ReturnBehavior returnBehavior
Definition: AESCBC.h:915
union AESCBC_OperationUnion AESCBC_OperationUnion
Union containing a reference to a one step or segmented operation.
int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle)
Cancels an ongoing AESCBC operation.
AESCommon_Config AESCBC_Config
AESCBC Global configuration.
Definition: AESCBC.h:725
Definition: AESCBC.h:880
AES Global configuration.
Definition: AESCommon.h:154
Definition: AESCBC.h:874
uint8_t * output
Definition: AESCBC.h:834
CryptoKey datastructure.
Definition: CryptoKey.h:208
Definition: AESCommon.h:186
AESCBC_CallbackFxn callbackFxn
Definition: AESCBC.h:916
Definition: AESCBC.h:878
AESCBC_Mode
Enum for the direction of the CBC operation.
Definition: AESCBC.h:777
Definition: AESCommon.h:196
AESCBC_ReturnBehavior
The way in which CBC function calls return after performing an encryption or decryption operation...
Definition: AESCBC.h:753
uint8_t * input
Definition: AESCBC.h:829
Struct containing the parameters required for encrypting/decrypting a message in a segmented operatio...
Definition: AESCBC.h:827
void AESCBC_close(AESCBC_Handle handle)
Function to close a CBC peripheral specified by the CBC handle.
int_fast16_t AESCBC_generateIV(AESCBC_Handle handle, uint8_t *iv, size_t ivSize, size_t *ivLength)
Function to generate an initialization vector for an AES CBC segmented encryption operation...
int_fast16_t AESCBC_finalize(AESCBC_Handle handle, AESCBC_SegmentedOperation *operation)
Finalize the AES operation. If new data needs to be added, inputLength will be used to govern how man...
AESCBC_OneStepOperation AESCBC_Operation
Definition: AESCBC.h:857
AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params)
Constructs a new AESCBC object.
Definition: AESCommon.h:192
uint8_t * output
Definition: AESCBC.h:795
Struct containing the parameters required for encrypting/decrypting a message in a single-step operat...
Definition: AESCBC.h:787
AESCBC_SegmentedOperation segmentedOperation
Definition: AESCBC.h:866
Definition: AESCBC.h:780
int_fast16_t AESCBC_addData(AESCBC_Handle handle, AESCBC_SegmentedOperation *operation)
Encrypts or decrypts a segment of data defined by the AESCBC_SegmentedOperation struct.
void AESCBC_OneStepOperation_init(AESCBC_OneStepOperation *operationStruct)
Function to initialize an AESCBC_OneStepOperation struct to its defaults.
CryptoKey * key
Definition: AESCBC.h:789
void AESCBC_init(void)
This function initializes the CBC module.
void AESCBC_Operation_init(AESCBC_Operation *operationStruct)
Function to initialize an AESCBC_Operation struct to its defaults.
AESCBC_Handle AESCBC_open(uint_least8_t index, const AESCBC_Params *params)
This function opens a given CBC peripheral.
bool ivInternallyGenerated
Definition: AESCBC.h:816
void * custom
Definition: AESCBC.h:920
AESCBC_OneStepOperation oneStepOperation
Definition: AESCBC.h:865
void AESCBC_SegmentedOperation_init(AESCBC_SegmentedOperation *operationStruct)
Function to initialize an AESCBC_SegmentedOperation struct to its defaults.
Definition: AESCBC.h:779
Definition: AESCBC.h:755
AESCBC_OperationType
Enum for the operation types supported by the driver.
Definition: AESCBC.h:872
size_t inputLength
Definition: AESCBC.h:807
Definition: AESCBC.h:875
Union containing a reference to a one step or segmented operation.
Definition: AESCBC.h:863
uint8_t * iv
Definition: AESCBC.h:801
AESCBC_Config * AESCBC_Handle
A handle that is returned from an AESCBC_open() call.
Definition: AESCBC.h:730
AES common module header for all devices.
CBC Parameters.
Definition: AESCBC.h:913
int_fast16_t AESCBC_setupDecrypt(AESCBC_Handle handle, const CryptoKey *key)
Function to prepare a segmented AESCBC decryption operation.
size_t inputLength
Definition: AESCBC.h:840
int_fast16_t AESCBC_setIV(AESCBC_Handle handle, const uint8_t *iv, size_t ivLength)
Function to set an initialization vector for an AES CBC segmented operation.
uint32_t timeout
Definition: AESCBC.h:917
Definition: AESCBC.h:879
Definition: AESCBC.h:876
void AESCBC_Params_init(AESCBC_Params *params)
Function to initialize the AESCBC_Params struct to its defaults.
void(* AESCBC_CallbackFxn)(AESCBC_Handle handle, int_fast16_t returnValue, AESCBC_OperationUnion *operation, AESCBC_OperationType operationType)
The definition of a callback function used by the AESCBC driver when used in AESCBC_RETURN_BEHAVIOR_C...
Definition: AESCBC.h:900
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale