AESCBC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021, 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  * @code
236  *
237  * #include <ti/drivers/AESCBC.h>
238  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
239  *
240  * ...
241  *
242  * // Test vector 0 from NIST CAPV set CBCMMT256
243  *
244  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
245  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
246  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
247  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
248  * uint8_t keyingMaterial[32] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
249  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
250  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
251  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
252  * uint8_t plaintext[sizeof(ciphertext)];
253  *
254  * // The plaintext should be the following after the decryption operation:
255  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
256  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
257  *
258  *
259  * void cbcCallback(AESCBC_Handle handle,
260  * int_fast16_t returnValue,
261  * AESCBC_OperationUnion *operation,
262  * AESCBC_OperationType operationType) {
263  *
264  * if (returnValue != AESCBC_STATUS_SUCCESS) {
265  * // handle error
266  * }
267  *
268  * if (operationType == AESCBC_OPERATION_TYPE_DECRYPT ||
269  * operationType == AESCBC_OPERATION_TYPE_ENCRYPT) {
270  * // do something with operation->oneStepOperation
271  * } else {
272  * // do something with operation->segmentedOperation
273  * }
274  * }
275  *
276  * AESCBC_OneStepOperation operation;
277  *
278  * void cbcStartFunction(void) {
279  * AESCBC_Handle handle;
280  * AESCBC_Params params;
281  * CryptoKey cryptoKey;
282  * int_fast16_t decryptionResult;
283  *
284  * AESCBC_Params_init(&params);
285  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
286  * params.callbackFxn = cbcCallback;
287  *
288  * handle = AESCBC_open(0, &params);
289  *
290  * if (handle == NULL) {
291  * // handle error
292  * }
293  *
294  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
295  *
296  * AESCBC_OneStepOperation_init(&operation);
297  *
298  * operation.key = &cryptoKey;
299  * operation.input = plaintext;
300  * operation.output = ciphertext;
301  * operation.inputLength = sizeof(plaintext);
302  * operation.iv = iv;
303  *
304  * decryptionResult = AESCBC_oneStepDecrypt(handle, &operation);
305  *
306  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
307  * // handle error
308  * }
309  *
310  * // do other things while CBC operation completes in the background
311  * }
312  *
313  * @endcode
314  *
315  * ### Multi-step CBC encryption with plaintext CryptoKey in blocking return mode #
316  * @code
317  *
318  * #include <ti/drivers/AESCBC.h>
319  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
320  *
321  * ...
322  *
323  * #define AES_BLOCK_SIZE 16 // bytes
324  *
325  * AESCBC_Handle handle;
326  * CryptoKey cryptoKey;
327  * int_fast16_t encryptionResult;
328  *
329  * // For example purposes only. Generate IVs in a non-static way in practice.
330  * // Test vector 0 from NIST CAPV set CBCMMT128
331  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
332  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
333  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
334  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
335  * uint8_t ciphertext[sizeof(plaintext)];
336  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
337  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
338  *
339  * // The ciphertext should be the following after the encryption operation:
340  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
341  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
342  *
343  *
344  * handle = AESCBC_open(0, NULL);
345  *
346  * if (handle == NULL) {
347  * // handle error
348  * }
349  *
350  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
351  *
352  * AESCBC_SegmentedOperation operation;
353  * AESCBC_SegmentedOperation_init(&operation);
354  *
355  * operation.input = plaintext;
356  * operation.output = ciphertext;
357  * operation.inputLength = sizeof(plaintext);
358  *
359  * encryptionResult = AESCBC_setupEncrypt(handle, &cryptoKey);
360  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
361  * // handle error
362  * }
363  *
364  * encryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
365  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
366  * // handle error
367  * }
368  *
369  * encryptionResult = AESCBC_addData(handle, &operation);
370  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
371  * // handle error
372  * }
373  *
374  * operation.inputLength = 0;
375  * encryptionResult = AESCBC_finalize(handle, &operation);
376  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
377  * // handle error
378  * }
379  *
380  * AESCBC_close(handle);
381  *
382  * @endcode
383  *
384  * ### Multi-step CBC decryption with plaintext CryptoKey in callback return mode #
385  * @code
386  *
387  * #include <ti/drivers/AESCBC.h>
388  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
389  *
390  * ...
391  *
392  * #define AES_BLOCK_SIZE 16 // bytes
393  *
394  * // Test vector 0 from NIST CAPV set CBCMMT256
395  *
396  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
397  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
398  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
399  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
400  * uint8_t keyingMaterial[32] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
401  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
402  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
403  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
404  * uint8_t plaintext[sizeof(ciphertext)];
405  *
406  * // The plaintext should be the following after the decryption operation:
407  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
408  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
409  *
410  *
411  * void cbcCallback(AESCBC_Handle handle,
412  * int_fast16_t returnValue,
413  * AESCBC_OperationUnion *operation,
414  * AESCBC_OperationType operationType) {
415  *
416  * if (returnValue != AESCBC_STATUS_SUCCESS) {
417  * // handle error
418  * }
419  *
420  * if (operationType == AESCBC_OPERATION_TYPE_DECRYPT ||
421  * operationType == AESCBC_OPERATION_TYPE_ENCRYPT) {
422  * // do something with operation->oneStepOperation
423  * } else {
424  * // do something with operation->segmentedOperation
425  * }
426  * }
427  *
428  * AESCBC_SegmentedOperation operation;
429  *
430  * void cbcStartFunction(void) {
431  * AESCBC_Handle handle;
432  * AESCBC_Params params;
433  * CryptoKey cryptoKey;
434  * int_fast16_t decryptionResult;
435  *
436  * AESCBC_Params_init(&params);
437  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
438  * params.callbackFxn = cbcCallback;
439  *
440  * handle = AESCBC_open(0, &params);
441  *
442  * if (handle == NULL) {
443  * // handle error
444  * }
445  *
446  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
447  *
448  * AESCBC_SegmentedOperation_init(&operation);
449  *
450  * operation.input = ciphertext;
451  * operation.output = plaintext;
452  * operation.inputLength = sizeof(ciphertext);
453  *
454  * decryptionResult = AESCBC_setupDecrypt(handle, &cryptoKey);
455  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
456  * // handle error
457  * }
458  *
459  * decryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
460  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
461  * // handle error
462  * }
463  *
464  * decryptionResult = AESCBC_addData(handle, &operation);
465  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
466  * // handle error
467  * }
468  *
469  * // do other things while CBC operation completes in the background
470  *
471  * operation.inputLength = 0;
472  * decryptionResult = AESCBC_finalize(handle, &operation);
473  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
474  * // handle error
475  * }
476  *
477  * }
478  *
479  * @endcode
480  *
481  * ### Multi-step CBC encryption with plaintext CryptoKey and non-empty finalize in blocking return mode #
482  * @code
483  *
484  * #include <ti/drivers/AESCBC.h>
485  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
486  *
487  * ...
488  *
489  * #define AES_BLOCK_SIZE 16 // bytes
490  *
491  * AESCBC_Handle handle;
492  * CryptoKey cryptoKey;
493  * int_fast16_t encryptionResult;
494  *
495  * // For example purposes only. Generate IVs in a non-static way in practice.
496  * // Test vector 1 from NIST CAPV set CBCMMT128
497  * uint8_t iv[16] = {0xaa, 0xd1, 0x58, 0x3c, 0xd9, 0x13, 0x65, 0xe3,
498  * 0xbb, 0x2f, 0x0c, 0x34, 0x30, 0xd0, 0x65, 0xbb};
499  * uint8_t plaintext[32] = {0x06, 0x8b, 0x25, 0xc7, 0xbf, 0xb1, 0xf8, 0xbd,
500  * 0xd4, 0xcf, 0xc9, 0x08, 0xf6, 0x9d, 0xff, 0xc5,
501  * 0xdd, 0xc7, 0x26, 0xa1, 0x97, 0xf0, 0xe5, 0xf7,
502  * 0x20, 0xf7, 0x30, 0x39, 0x32, 0x79, 0xbe, 0x91};
503  * uint8_t ciphertext[sizeof(plaintext)];
504  * uint8_t keyingMaterial[16] = {0x07, 0x00, 0xd6, 0x03, 0xa1, 0xc5, 0x14, 0xe4,
505  * 0x6b, 0x61, 0x91, 0xba, 0x43, 0x0a, 0x3a, 0x0c};
506  *
507  * // The ciphertext should be the following after the encryption operation:
508  * // 0xc4, 0xdc, 0x61, 0xd9, 0x72, 0x59, 0x67, 0xa3
509  * // 0x02, 0x01, 0x04, 0xa9, 0x73, 0x8f, 0x23, 0x86
510  * // 0x85, 0x27, 0xce, 0x83, 0x9a, 0xab, 0x17, 0x52
511  * // 0xfd, 0x8b, 0xdb, 0x95, 0xa8, 0x2c, 0x4d, 0x00
512  *
513  *
514  * handle = AESCBC_open(0, NULL);
515  *
516  * if (handle == NULL) {
517  * // handle error
518  * }
519  *
520  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
521  *
522  * AESCBC_SegmentedOperation operation;
523  * AESCBC_SegmentedOperation_init(&operation);
524  *
525  * operation.input = plaintext;
526  * operation.output = ciphertext;
527  * // One should pass in data that is a block-sized multiple length (16 bytes)
528  * operation.inputLength = AES_BLOCK_SIZE;
529  *
530  * encryptionResult = AESCBC_setupEncrypt(handle, &cryptoKey);
531  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
532  * // handle error
533  * }
534  *
535  * encryptionResult = AESCBC_setIV(handle, iv, AES_BLOCK_SIZE);
536  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
537  * // handle error
538  * }
539  *
540  * encryptionResult = AESCBC_addData(handle, &operation);
541  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
542  * // handle error
543  * }
544  *
545  * operation.input = plaintext + AES_BLOCK_SIZE;
546  * operation.output = ciphertext + AES_BLOCK_SIZE;
547  *
548  * // You can also finalize with more data (non-zero inputLength)
549  * operation.inputLength = sizeof(plaintext) - AES_BLOCK_SIZE;
550  * encryptionResult = AESCBC_finalize(handle, &operation);
551  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
552  * // handle error
553  * }
554  *
555  * AESCBC_close(handle);
556  *
557  * @endcode
558  */
559 
560 
561 #ifndef ti_drivers_AESCBC__include
562 #define ti_drivers_AESCBC__include
563 
564 #include <stdbool.h>
565 #include <stddef.h>
566 #include <stdint.h>
567 
569 
570 #ifdef __cplusplus
571 extern "C" {
572 #endif
573 
586 #define AESCBC_STATUS_RESERVED (-32)
587 
594 #define AESCBC_STATUS_SUCCESS (0)
595 
602 #define AESCBC_STATUS_ERROR (-1)
603 
612 #define AESCBC_STATUS_RESOURCE_UNAVAILABLE (-2)
613 
617 #define AESCBC_STATUS_CANCELED (-3)
618 
626  #define AESCBC_STATUS_FEATURE_NOT_SUPPORTED (-4)
627 
639 typedef struct AESCBC_Config_ {
641  void *object;
642 
644  void const *hwAttrs;
645 } AESCBC_Config;
646 
651 
673 typedef enum {
689 
693 typedef enum {
696 } AESCBC_Mode;
697 
702 typedef struct {
704  uint8_t *input;
709  uint8_t *output;
715  uint8_t *iv;
721  size_t inputLength;
734 
740 typedef struct {
741  uint8_t *input;
746  uint8_t *output;
752  size_t inputLength;
760 
769 
770 
776 {
777  AESCBC_OneStepOperation oneStepOperation; /* One-step operation element of the operation union */
778  AESCBC_SegmentedOperation segmentedOperation; /* Segmented operation element of the operation union */
780 
781 
785 typedef enum {
786  AESCBC_OPERATION_TYPE_ENCRYPT = 1, /* Fields 1 and 2 are for backward compatibility */
788  AESCBC_OP_TYPE_ONESTEP_ENCRYPT = 1, /* Names changed to _OP_TYPE_ to avoid MISRA deviation from first 31 chars not being unique */
795 
811 typedef void (*AESCBC_CallbackFxn) (AESCBC_Handle handle,
812  int_fast16_t returnValue,
813  AESCBC_OperationUnion *operation,
814  AESCBC_OperationType operationType);
815 
824 typedef struct {
827  uint32_t timeout;
830  void *custom;
833 } AESCBC_Params;
834 
841 
850 void AESCBC_init(void);
851 
865 
883 AESCBC_Handle AESCBC_open(uint_least8_t index, const AESCBC_Params *params);
884 
894 void AESCBC_close(AESCBC_Handle handle);
895 
907 void AESCBC_Operation_init(AESCBC_Operation *operationStruct);
908 
918 
928 
947 int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct);
948 
967 int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_OneStepOperation *operationStruct);
968 
986 int_fast16_t AESCBC_setupEncrypt(AESCBC_Handle handle, const CryptoKey *key);
987 
1005 int_fast16_t AESCBC_setupDecrypt(AESCBC_Handle handle, const CryptoKey *key);
1006 
1028 int_fast16_t AESCBC_setIV(AESCBC_Handle handle, const uint8_t *iv, size_t ivLength);
1029 
1056 int_fast16_t AESCBC_generateIV(AESCBC_Handle handle, uint8_t *iv, size_t ivSize, size_t *ivLength);
1057 
1080 int_fast16_t AESCBC_addData(AESCBC_Handle handle,
1081  AESCBC_SegmentedOperation *operation);
1082 
1104 int_fast16_t AESCBC_finalize(AESCBC_Handle handle,
1105  AESCBC_SegmentedOperation *operation);
1106 
1120 int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle);
1121 
1145 AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params);
1146 
1147 #ifdef __cplusplus
1148 }
1149 #endif
1150 
1151 #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:684
uint8_t * input
Definition: AESCBC.h:704
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:680
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:825
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.
Definition: AESCBC.h:791
struct AESCBC_Config_ AESCBC_Config
AESCBC Global configuration.
Definition: AESCBC.h:786
uint8_t * output
Definition: AESCBC.h:746
CryptoKey datastructure.
Definition: CryptoKey.h:192
AESCBC_CallbackFxn callbackFxn
Definition: AESCBC.h:826
Definition: AESCBC.h:789
AESCBC_Mode
Enum for the direction of the CBC operation.
Definition: AESCBC.h:693
AESCBC_ReturnBehavior
The way in which CBC function calls return after performing an encryption or decryption operation...
Definition: AESCBC.h:673
uint8_t * input
Definition: AESCBC.h:741
Struct containing the parameters required for encrypting/decrypting a message in a segmented operatio...
Definition: AESCBC.h:740
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:768
AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params)
Constructs a new AESCBC object.
uint8_t * output
Definition: AESCBC.h:709
Struct containing the parameters required for encrypting/decrypting a message in a single-step operat...
Definition: AESCBC.h:702
AESCBC_SegmentedOperation segmentedOperation
Definition: AESCBC.h:778
Definition: AESCBC.h:695
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:703
void AESCBC_init(void)
This function initializes the CBC module.
AESCBC Global configuration.
Definition: AESCBC.h:639
void const * hwAttrs
Definition: AESCBC.h:644
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:729
void * custom
Definition: AESCBC.h:830
AESCBC_OneStepOperation oneStepOperation
Definition: AESCBC.h:777
void AESCBC_SegmentedOperation_init(AESCBC_SegmentedOperation *operationStruct)
Function to initialize an AESCBC_SegmentedOperation struct to its defaults.
Definition: AESCBC.h:694
Definition: AESCBC.h:674
AESCBC_OperationType
Enum for the operation types supported by the driver.
Definition: AESCBC.h:785
size_t inputLength
Definition: AESCBC.h:721
Definition: AESCBC.h:787
Union containing a reference to a one step or segmented operation.
Definition: AESCBC.h:775
uint8_t * iv
Definition: AESCBC.h:715
AESCBC_Config * AESCBC_Handle
A handle that is returned from an AESCBC_open() call.
Definition: AESCBC.h:650
CBC Parameters.
Definition: AESCBC.h:824
int_fast16_t AESCBC_setupDecrypt(AESCBC_Handle handle, const CryptoKey *key)
Function to prepare a segmented AESCBC decryption operation.
void * object
Definition: AESCBC.h:641
size_t inputLength
Definition: AESCBC.h:752
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:827
Definition: AESCBC.h:790
Definition: AESCBC.h:788
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:811
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale