AESCBC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2022, 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  * ### Single call CBC decryption with plaintext CryptoKey in callback return mode #
235  *
236  * @note The following code example presented uses a 256-bit key. However,
237  * CC13x1/CC26x1 and CC23x0 only support a maximum key size of 128-bits,
238  * so reduction of keyingMaterial would be required.
239  *
240  * @code
241  *
242  * #include <ti/drivers/AESCBC.h>
243  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
244  *
245  * ...
246  *
247  * // Test vector 0 from NIST CAPV set CBCMMT256
248  *
249  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
250  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
251  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
252  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
253  * uint8_t keyingMaterial[32] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
254  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
255  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
256  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
257  * uint8_t plaintext[sizeof(ciphertext)];
258  *
259  * // The plaintext should be the following after the decryption operation:
260  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
261  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
262  *
263  *
264  * void cbcCallback(AESCBC_Handle handle,
265  * int_fast16_t returnValue,
266  * AESCBC_OperationUnion *operation,
267  * AESCBC_OperationType operationType) {
268  *
269  * if (returnValue != AESCBC_STATUS_SUCCESS) {
270  * // handle error
271  * }
272  *
273  * if (operationType == AESCBC_OPERATION_TYPE_DECRYPT ||
274  * operationType == AESCBC_OPERATION_TYPE_ENCRYPT) {
275  * // do something with operation->oneStepOperation
276  * } else {
277  * // do something with operation->segmentedOperation
278  * }
279  * }
280  *
281  * AESCBC_OneStepOperation operation;
282  *
283  * void cbcStartFunction(void) {
284  * AESCBC_Handle handle;
285  * AESCBC_Params params;
286  * CryptoKey cryptoKey;
287  * int_fast16_t decryptionResult;
288  *
289  * AESCBC_Params_init(&params);
290  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
291  * params.callbackFxn = cbcCallback;
292  *
293  * handle = AESCBC_open(0, &params);
294  *
295  * if (handle == NULL) {
296  * // handle error
297  * }
298  *
299  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
300  *
301  * AESCBC_OneStepOperation_init(&operation);
302  *
303  * operation.key = &cryptoKey;
304  * operation.input = ciphertext;
305  * operation.output = plaintext;
306  * operation.inputLength = sizeof(ciphertext);
307  * operation.iv = iv;
308  *
309  * decryptionResult = AESCBC_oneStepDecrypt(handle, &operation);
310  *
311  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
312  * // handle error
313  * }
314  *
315  * // do other things while CBC operation completes in the background
316  * }
317  *
318  * @endcode
319  *
320  * ### Multi-step CBC encryption with plaintext CryptoKey in blocking return mode #
321  * @code
322  *
323  * #include <ti/drivers/AESCBC.h>
324  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
325  *
326  * ...
327  *
328  * #define AES_BLOCK_SIZE 16 // bytes
329  *
330  * AESCBC_Handle handle;
331  * CryptoKey cryptoKey;
332  * int_fast16_t encryptionResult;
333  *
334  * // For example purposes only. Generate IVs in a non-static way in practice.
335  * // Test vector 0 from NIST CAPV set CBCMMT128
336  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
337  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
338  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
339  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
340  * uint8_t ciphertext[sizeof(plaintext)];
341  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
342  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
343  *
344  * // The ciphertext should be the following after the encryption operation:
345  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
346  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
347  *
348  *
349  * handle = AESCBC_open(0, NULL);
350  *
351  * if (handle == NULL) {
352  * // handle error
353  * }
354  *
355  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
356  *
357  * AESCBC_SegmentedOperation operation;
358  * AESCBC_SegmentedOperation_init(&operation);
359  *
360  * operation.input = plaintext;
361  * operation.output = ciphertext;
362  * operation.inputLength = sizeof(plaintext);
363  *
364  * encryptionResult = AESCBC_setupEncrypt(handle, &cryptoKey);
365  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
366  * // handle error
367  * }
368  *
369  * encryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
370  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
371  * // handle error
372  * }
373  *
374  * encryptionResult = AESCBC_addData(handle, &operation);
375  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
376  * // handle error
377  * }
378  *
379  * operation.inputLength = 0;
380  * encryptionResult = AESCBC_finalize(handle, &operation);
381  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
382  * // handle error
383  * }
384  *
385  * AESCBC_close(handle);
386  *
387  * @endcode
388  *
389  * ### Multi-step CBC decryption with plaintext CryptoKey in callback return mode #
390  *
391  * @note The following code example presented uses a 256-bit key. However,
392  * CC13x1/CC26x1 and CC23x0 only support a maximum key size of 128-bits,
393  * so reduction of keyingMaterial would be required.
394  *
395  * @code
396  *
397  * #include <ti/drivers/AESCBC.h>
398  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
399  *
400  * ...
401  *
402  * #define AES_BLOCK_SIZE 16 // bytes
403  *
404  * // Test vector 0 from NIST CAPV set CBCMMT256
405  *
406  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
407  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
408  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
409  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
410  * uint8_t keyingMaterial[32] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
411  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
412  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
413  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
414  * uint8_t plaintext[sizeof(ciphertext)];
415  *
416  * // The plaintext should be the following after the decryption operation:
417  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
418  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
419  *
420  *
421  * void cbcCallback(AESCBC_Handle handle,
422  * int_fast16_t returnValue,
423  * AESCBC_OperationUnion *operation,
424  * AESCBC_OperationType operationType) {
425  *
426  * if (returnValue != AESCBC_STATUS_SUCCESS) {
427  * // handle error
428  * }
429  *
430  * if (operationType == AESCBC_OPERATION_TYPE_DECRYPT ||
431  * operationType == AESCBC_OPERATION_TYPE_ENCRYPT) {
432  * // do something with operation->oneStepOperation
433  * } else {
434  * // do something with operation->segmentedOperation
435  * }
436  * }
437  *
438  * AESCBC_SegmentedOperation operation;
439  *
440  * void cbcStartFunction(void) {
441  * AESCBC_Handle handle;
442  * AESCBC_Params params;
443  * CryptoKey cryptoKey;
444  * int_fast16_t decryptionResult;
445  *
446  * AESCBC_Params_init(&params);
447  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
448  * params.callbackFxn = cbcCallback;
449  *
450  * handle = AESCBC_open(0, &params);
451  *
452  * if (handle == NULL) {
453  * // handle error
454  * }
455  *
456  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
457  *
458  * AESCBC_SegmentedOperation_init(&operation);
459  *
460  * operation.input = ciphertext;
461  * operation.output = plaintext;
462  * operation.inputLength = sizeof(ciphertext);
463  *
464  * decryptionResult = AESCBC_setupDecrypt(handle, &cryptoKey);
465  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
466  * // handle error
467  * }
468  *
469  * decryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
470  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
471  * // handle error
472  * }
473  *
474  * decryptionResult = AESCBC_addData(handle, &operation);
475  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
476  * // handle error
477  * }
478  *
479  * // do other things while CBC operation completes in the background
480  *
481  * operation.inputLength = 0;
482  * decryptionResult = AESCBC_finalize(handle, &operation);
483  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
484  * // handle error
485  * }
486  *
487  * }
488  *
489  * @endcode
490  *
491  * ### Multi-step CBC encryption with plaintext CryptoKey and non-empty finalize in blocking return mode #
492  * @code
493  *
494  * #include <ti/drivers/AESCBC.h>
495  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
496  *
497  * ...
498  *
499  * #define AES_BLOCK_SIZE 16 // bytes
500  *
501  * AESCBC_Handle handle;
502  * CryptoKey cryptoKey;
503  * int_fast16_t encryptionResult;
504  *
505  * // For example purposes only. Generate IVs in a non-static way in practice.
506  * // Test vector 1 from NIST CAPV set CBCMMT128
507  * uint8_t iv[16] = {0xaa, 0xd1, 0x58, 0x3c, 0xd9, 0x13, 0x65, 0xe3,
508  * 0xbb, 0x2f, 0x0c, 0x34, 0x30, 0xd0, 0x65, 0xbb};
509  * uint8_t plaintext[32] = {0x06, 0x8b, 0x25, 0xc7, 0xbf, 0xb1, 0xf8, 0xbd,
510  * 0xd4, 0xcf, 0xc9, 0x08, 0xf6, 0x9d, 0xff, 0xc5,
511  * 0xdd, 0xc7, 0x26, 0xa1, 0x97, 0xf0, 0xe5, 0xf7,
512  * 0x20, 0xf7, 0x30, 0x39, 0x32, 0x79, 0xbe, 0x91};
513  * uint8_t ciphertext[sizeof(plaintext)];
514  * uint8_t keyingMaterial[16] = {0x07, 0x00, 0xd6, 0x03, 0xa1, 0xc5, 0x14, 0xe4,
515  * 0x6b, 0x61, 0x91, 0xba, 0x43, 0x0a, 0x3a, 0x0c};
516  *
517  * // The ciphertext should be the following after the encryption operation:
518  * // 0xc4, 0xdc, 0x61, 0xd9, 0x72, 0x59, 0x67, 0xa3
519  * // 0x02, 0x01, 0x04, 0xa9, 0x73, 0x8f, 0x23, 0x86
520  * // 0x85, 0x27, 0xce, 0x83, 0x9a, 0xab, 0x17, 0x52
521  * // 0xfd, 0x8b, 0xdb, 0x95, 0xa8, 0x2c, 0x4d, 0x00
522  *
523  *
524  * handle = AESCBC_open(0, NULL);
525  *
526  * if (handle == NULL) {
527  * // handle error
528  * }
529  *
530  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
531  *
532  * AESCBC_SegmentedOperation operation;
533  * AESCBC_SegmentedOperation_init(&operation);
534  *
535  * operation.input = plaintext;
536  * operation.output = ciphertext;
537  * // One should pass in data that is a block-sized multiple length (16 bytes)
538  * operation.inputLength = AES_BLOCK_SIZE;
539  *
540  * encryptionResult = AESCBC_setupEncrypt(handle, &cryptoKey);
541  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
542  * // handle error
543  * }
544  *
545  * encryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
546  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
547  * // handle error
548  * }
549  *
550  * encryptionResult = AESCBC_addData(handle, &operation);
551  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
552  * // handle error
553  * }
554  *
555  * operation.input = plaintext + AES_BLOCK_SIZE;
556  * operation.output = ciphertext + AES_BLOCK_SIZE;
557  *
558  * // You can also finalize with more data (non-zero inputLength)
559  * operation.inputLength = sizeof(plaintext) - AES_BLOCK_SIZE;
560  * encryptionResult = AESCBC_finalize(handle, &operation);
561  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
562  * // handle error
563  * }
564  *
565  * AESCBC_close(handle);
566  *
567  * @endcode
568  */
569 
570 
571 #ifndef ti_drivers_AESCBC__include
572 #define ti_drivers_AESCBC__include
573 
574 #include <stdbool.h>
575 #include <stddef.h>
576 #include <stdint.h>
577 
578 #include <ti/drivers/AESCommon.h>
580 
581 #ifdef __cplusplus
582 extern "C" {
583 #endif
584 
597 #define AESCBC_STATUS_RESERVED AES_STATUS_RESERVED
598 
605 #define AESCBC_STATUS_SUCCESS AES_STATUS_SUCCESS
606 
613 #define AESCBC_STATUS_ERROR AES_STATUS_ERROR
614 
623 #define AESCBC_STATUS_RESOURCE_UNAVAILABLE AES_STATUS_RESOURCE_UNAVAILABLE
624 
628 #define AESCBC_STATUS_CANCELED AES_STATUS_CANCELED
629 
637 #define AESCBC_STATUS_FEATURE_NOT_SUPPORTED AES_STATUS_FEATURE_NOT_SUPPORTED
638 
642 #define AESCBC_STATUS_KEYSTORE_INVALID_ID AES_STATUS_KEYSTORE_INVALID_ID
643 
648 #define AESCBC_STATUS_KEYSTORE_GENERIC_ERROR AES_STATUS_KEYSTORE_GENERIC_ERROR
649 
656 #define AESCBC_STATUS_UNALIGNED_IO_NOT_SUPPORTED AES_STATUS_UNALIGNED_IO_NOT_SUPPORTED
657 
670 
674 typedef AESCBC_Config *AESCBC_Handle;
675 
697 typedef enum {
716 
720 typedef enum {
723 } AESCBC_Mode;
724 
729 typedef struct {
731  uint8_t *input;
736  uint8_t *output;
742  uint8_t *iv;
748  size_t inputLength;
762 
768 typedef struct {
769  uint8_t *input;
774  uint8_t *output;
780  size_t inputLength;
789 
798 
799 
805 {
806  AESCBC_OneStepOperation oneStepOperation; /* One-step operation element of the operation union */
807  AESCBC_SegmentedOperation segmentedOperation; /* Segmented operation element of the operation union */
809 
810 
814 typedef enum {
815  AESCBC_OPERATION_TYPE_ENCRYPT = 1, /* Fields 1 and 2 are for backward compatibility */
817  AESCBC_OP_TYPE_ONESTEP_ENCRYPT = 1, /* Names changed to _OP_TYPE_ to avoid MISRA deviation from first 31 chars not being unique */
824 
840 typedef void (*AESCBC_CallbackFxn) (AESCBC_Handle handle,
841  int_fast16_t returnValue,
842  AESCBC_OperationUnion *operation,
843  AESCBC_OperationType operationType);
844 
853 typedef struct {
854  AESCBC_ReturnBehavior returnBehavior;
856  uint32_t timeout;
859  void *custom;
862 } AESCBC_Params;
863 
870 
879 void AESCBC_init(void);
880 
894 
912 AESCBC_Handle AESCBC_open(uint_least8_t index, const AESCBC_Params *params);
913 
923 void AESCBC_close(AESCBC_Handle handle);
924 
936 void AESCBC_Operation_init(AESCBC_Operation *operationStruct);
937 
947 
957 
977 int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct);
978 
998 int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct);
999 
1017 int_fast16_t AESCBC_setupEncrypt(AESCBC_Handle handle, const CryptoKey *key);
1018 
1036 int_fast16_t AESCBC_setupDecrypt(AESCBC_Handle handle, const CryptoKey *key);
1037 
1059 int_fast16_t AESCBC_setIV(AESCBC_Handle handle, const uint8_t *iv, size_t ivLength);
1060 
1087 int_fast16_t AESCBC_generateIV(AESCBC_Handle handle, uint8_t *iv, size_t ivSize, size_t *ivLength);
1088 
1112 int_fast16_t AESCBC_addData(AESCBC_Handle handle,
1113  AESCBC_SegmentedOperation *operation);
1114 
1137 int_fast16_t AESCBC_finalize(AESCBC_Handle handle,
1138  AESCBC_SegmentedOperation *operation);
1139 
1153 int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle);
1154 
1178 AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params);
1179 
1180 #ifdef __cplusplus
1181 }
1182 #endif
1183 
1184 #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:710
uint8_t * input
Definition: AESCBC.h:731
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:705
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:854
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:669
Definition: AESCBC.h:820
AES Global configuration.
Definition: AESCommon.h:154
Definition: AESCBC.h:815
uint8_t * output
Definition: AESCBC.h:774
CryptoKey datastructure.
Definition: CryptoKey.h:192
Definition: AESCommon.h:184
AESCBC_CallbackFxn callbackFxn
Definition: AESCBC.h:855
Definition: AESCBC.h:818
AESCBC_Mode
Enum for the direction of the CBC operation.
Definition: AESCBC.h:720
Definition: AESCommon.h:194
AESCBC_ReturnBehavior
The way in which CBC function calls return after performing an encryption or decryption operation...
Definition: AESCBC.h:697
uint8_t * input
Definition: AESCBC.h:769
Struct containing the parameters required for encrypting/decrypting a message in a segmented operatio...
Definition: AESCBC.h:768
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:797
AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params)
Constructs a new AESCBC object.
Definition: AESCommon.h:190
uint8_t * output
Definition: AESCBC.h:736
Struct containing the parameters required for encrypting/decrypting a message in a single-step operat...
Definition: AESCBC.h:729
AESCBC_SegmentedOperation segmentedOperation
Definition: AESCBC.h:807
Definition: AESCBC.h:722
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:730
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:757
void * custom
Definition: AESCBC.h:859
AESCBC_OneStepOperation oneStepOperation
Definition: AESCBC.h:806
void AESCBC_SegmentedOperation_init(AESCBC_SegmentedOperation *operationStruct)
Function to initialize an AESCBC_SegmentedOperation struct to its defaults.
Definition: AESCBC.h:721
Definition: AESCBC.h:698
AESCBC_OperationType
Enum for the operation types supported by the driver.
Definition: AESCBC.h:814
size_t inputLength
Definition: AESCBC.h:748
Definition: AESCBC.h:816
Union containing a reference to a one step or segmented operation.
Definition: AESCBC.h:804
uint8_t * iv
Definition: AESCBC.h:742
AESCBC_Config * AESCBC_Handle
A handle that is returned from an AESCBC_open() call.
Definition: AESCBC.h:674
AES common module header for all devices.
CBC Parameters.
Definition: AESCBC.h:853
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:780
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:856
Definition: AESCBC.h:819
Definition: AESCBC.h:817
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:840
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale