AESCBC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file 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 divisibly 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 encryped. 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 decryting 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, key blob), the CryptoKey must be
121  * initialized differently. The AESCBC API can handle all types of CryptoKey.
122  * However, not all device-specific implementions support all types of CryptoKey.
123  * Devices without a key store will not support CryptoKeys with keying material
124  * stored in a key store for example.
125  * All devices support plaintext CryptoKeys.
126  * - Initialise the #AESCBC_Operation using #AESCBC_Operation_init() and set all
127  * length, key, and buffer fields.
128  *
129  * ## Starting a CBC operation #
130  *
131  * The #AESCBC_oneStepEncrypt and #AESCBC_oneStepDecrypt functions perform a CBC operation
132  * in a single call. They will always be the most highly optimized routines with the
133  * least overhead and the fastest runtime. However, they require all plaintext
134  * or ciphertext to be available to the function at the start of the call.
135  * All devices support single call operations.
136  *
137  * ## After the CBC operation completes #
138  *
139  * After the CBC operation completes, the application should either start
140  * another operation or close the driver by calling #AESCBC_close().
141  *
142  * @anchor ti_drivers_AESCBC_Synopsis
143  * ## Synopsis
144  * @anchor ti_drivers_AESCBC_Synopsis_Code
145  * @code
146  * // Import AESCBC Driver definitions
147  * #include <ti/drivers/AESCBC.h>
148  *
149  * // Define name for AESCBC channel index
150  * #define AESCBC_INSTANCE 0
151  *
152  * AESCBC_init();
153  *
154  * handle = AESCBC_open(AESCBC_INSTANCE, NULL);
155  *
156  * // Initialize symmetric key
157  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
158  *
159  * // Set up AESCBC_Operation
160  * AESCBC_Operation_init(&operation);
161  * operation.key = &cryptoKey;
162  * operation.input = plaintext;
163  * operation.output = ciphertext;
164  * operation.inputLength = sizeof(plaintext);
165  * operation.iv = iv;
166  *
167  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
168  *
169  * AESCBC_close(handle);
170  * @endcode
171  *
172  * @anchor ti_drivers_AESCBC_Examples
173  * ## Examples
174  *
175  * ### Single call CBC encryption with plaintext CryptoKey in blocking return mode #
176  * @code
177  *
178  * #include <ti/drivers/AESCBC.h>
179  * #include <ti/drivers/types/cryptoKey/CryptoKey_Plaintext.h>
180  *
181  * ...
182  *
183  * AESCBC_Handle handle;
184  * CryptoKey cryptoKey;
185  * int_fast16_t encryptionResult;
186  *
187  * // For example purposes only. Generate IVs in a non-static way in practice.
188  * // Test vector 0 from NIST CAPV set CBCMMT128
189  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
190  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
191  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
192  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
193  * uint8_t ciphertext[sizeof(plaintext)];
194  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
195  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
196  *
197  * // The ciphertext should be the following after the encryption operation:
198  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
199  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
200  *
201  *
202  * handle = AESCBC_open(0, NULL);
203  *
204  * if (handle == NULL) {
205  * // handle error
206  * }
207  *
208  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
209  *
210  * AESCBC_Operation operation;
211  * AESCBC_Operation_init(&operation);
212  *
213  * operation.key = &cryptoKey;
214  * operation.input = plaintext;
215  * operation.output = ciphertext;
216  * operation.inputLength = sizeof(plaintext);
217  * operation.iv = iv;
218  *
219  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
220  *
221  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
222  * // handle error
223  * }
224  *
225  * AESCBC_close(handle);
226  *
227  * @endcode
228  *
229  * ### Single call CBC decryption with plaintext CryptoKey in callback return mode #
230  * @code
231  *
232  * #include <ti/drivers/AESCBC.h>
233  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
234  *
235  * ...
236  *
237  * // Test vector 0 from NIST CAPV set CBCMMT256
238  *
239  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
240  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
241  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
242  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
243  * uint8_t keyingMaterial[] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
244  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
245  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
246  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
247  * uint8_t plaintext[sizeof(ciphertext)];
248  *
249  * // The plaintext should be the following after the decryption operation:
250  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
251  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
252  *
253  *
254  * void cbcCallback(AESCBC_Handle handle,
255  * int_fast16_t returnValue,
256  * AESCBC_Operation *operation,
257  * AESCBC_OperationType operationType) {
258  *
259  * if (returnValue != AESCBC_STATUS_SUCCESS) {
260  * // handle error
261  * }
262  * }
263  *
264  * AESCBC_Operation operation;
265  *
266  * void cbcStartFunction(void) {
267  * AESCBC_Handle handle;
268  * AESCBC_Params params;
269  * CryptoKey cryptoKey;
270  * int_fast16_t decryptionResult;
271  *
272  * AESCBC_Params_init(&params);
273  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
274  * params.callbackFxn = cbcCallback;
275  *
276  * handle = AESCBC_open(0, &params);
277  *
278  * if (handle == NULL) {
279  * // handle error
280  * }
281  *
282  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
283  *
284  * AESCBC_Operation_init(&operation);
285  *
286  * operation.key = &cryptoKey;
287  * operation.input = plaintext;
288  * operation.output = ciphertext;
289  * operation.inputLength = sizeof(plaintext);
290  * operation.iv = iv;
291  *
292  * decryptionResult = AESCBC_oneStepDecrypt(handle, &operation);
293  *
294  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
295  * // handle error
296  * }
297  *
298  * // do other things while CBC operation completes in the background
299  * }
300  *
301  * @endcode
302  */
303 
304 #ifndef ti_drivers_AESCBC__include
305 #define ti_drivers_AESCBC__include
306 
307 #include <stdbool.h>
308 #include <stddef.h>
309 #include <stdint.h>
310 
312 
313 #ifdef __cplusplus
314 extern "C" {
315 #endif
316 
329 #define AESCBC_STATUS_RESERVED (-32)
330 
337 #define AESCBC_STATUS_SUCCESS (0)
338 
345 #define AESCBC_STATUS_ERROR (-1)
346 
355 #define AESCBC_STATUS_RESOURCE_UNAVAILABLE (-2)
356 
360 #define AESCBC_STATUS_CANCELED (-3)
361 
373 typedef struct AESCBC_Config_ {
375  void *object;
376 
378  void const *hwAttrs;
379 } AESCBC_Config;
380 
385 
407 typedef enum {
423 
427 typedef enum {
430 } AESCBC_Mode;
431 
436 typedef struct {
438  uint8_t *input;
443  uint8_t *output;
449  uint8_t *iv;
455  size_t inputLength;
461 
465 typedef enum {
469 
485 typedef void (*AESCBC_CallbackFxn) (AESCBC_Handle handle,
486  int_fast16_t returnValue,
487  AESCBC_Operation *operation,
488  AESCBC_OperationType operationType);
489 
498 typedef struct {
501  uint32_t timeout;
504  void *custom;
507 } AESCBC_Params;
508 
515 
524 void AESCBC_init(void);
525 
539 
557 AESCBC_Handle AESCBC_open(uint_least8_t index, const AESCBC_Params *params);
558 
568 void AESCBC_close(AESCBC_Handle handle);
569 
578 void AESCBC_Operation_init(AESCBC_Operation *operationStruct);
579 
599 int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_Operation *operationStruct);
600 
620 int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_Operation *operationStruct);
621 
635 int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle);
636 
660 AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params);
661 
679 int_fast16_t AESCBC_getNextIv(AESCBC_Handle handle, uint8_t *iv);
680 
681 #ifdef __cplusplus
682 }
683 #endif
684 
685 #endif /* ti_drivers_AESCBC__include */
uint8_t * input
Definition: AESCBC.h:438
ADC_Params params
Definition: Driver_Init.h:11
const AESCBC_Params AESCBC_defaultParams
Default AESCBC_Params structure.
Definition: AESCBC.h:418
The CryptoKey type is an opaque representation of a cryptographic key.
Definition: AESCBC.h:414
AESCBC_ReturnBehavior returnBehavior
Definition: AESCBC.h:499
void(* AESCBC_CallbackFxn)(AESCBC_Handle handle, int_fast16_t returnValue, AESCBC_Operation *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:485
int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle)
Cancels an ongoing AESCBC operation.
Struct containing the parameters required for encrypting/decrypting a message.
Definition: AESCBC.h:436
struct AESCBC_Config_ AESCBC_Config
AESCBC Global configuration.
Definition: AESCBC.h:466
CryptoKey datastructure.
Definition: CryptoKey.h:209
AESCBC_CallbackFxn callbackFxn
Definition: AESCBC.h:500
AESCBC_Mode
Enum for the direction of the CBC operation.
Definition: AESCBC.h:427
CryptoKey * key
Definition: AESCBC.h:437
AESCBC_ReturnBehavior
The way in which CBC function calls return after performing an encryption or decryption operation...
Definition: AESCBC.h:407
int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_Operation *operationStruct)
Function to perform an AESCBC decryption operation in one call.
void AESCBC_close(AESCBC_Handle handle)
Function to close a CBC peripheral specified by the CBC handle.
AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params)
Constructs a new AESCBC object.
Definition: AESCBC.h:429
void AESCBC_init(void)
This function initializes the CBC module.
AESCBC Global configuration.
Definition: AESCBC.h:373
void const * hwAttrs
Definition: AESCBC.h:378
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.
int_fast16_t AESCBC_getNextIv(AESCBC_Handle handle, uint8_t *iv)
Returns the IV for the next block to encrypt or decrypt.
void * custom
Definition: AESCBC.h:504
Definition: AESCBC.h:428
Definition: AESCBC.h:408
AESCBC_OperationType
Enum for the operation types supported by the driver.
Definition: AESCBC.h:465
uint8_t * iv
Definition: AESCBC.h:449
Definition: AESCBC.h:467
AESCBC_Config * AESCBC_Handle
A handle that is returned from an AESCBC_open() call.
Definition: AESCBC.h:384
int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_Operation *operationStruct)
Function to perform an AESCBC encryption operation in one call.
CBC Parameters.
Definition: AESCBC.h:498
bool ivInternallyGenerated
Definition: AESCBC.h:456
uint8_t * output
Definition: AESCBC.h:443
void * object
Definition: AESCBC.h:375
uint32_t timeout
Definition: AESCBC.h:501
void AESCBC_Params_init(AESCBC_Params *params)
Function to initialize the AESCBC_Params struct to its defaults.
size_t inputLength
Definition: AESCBC.h:455
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale