AESCBC.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file AESCBC.h
34  *
35  * @brief AESCBC driver header
36  *
37  * @warning This is a beta API. It may change in future releases.
38  *
39  * @anchor ti_drivers_AESCBC_Overview
40  * # Overview #
41  * The Cipher Block Chaining (CBC) mode of operation is a generic
42  * block cipher mode of operation. It can be used with any block cipher
43  * including AES.
44  *
45  * CBC mode encrypts messages of any practical length that have a length
46  * evenly divisibly by the block size. Unlike ECB, it guarantees
47  * confidentiality of the entire message when the message is larger than
48  * one block.
49  *
50  * ## Operation #
51  * In CBC encryption, the initialization vector (IV) is XOR'd with a block of
52  * plaintext and then encrypted. The output ciphertext block is then XOR'd with
53  * the next plaintext block and the result is encryped. This process is repeated
54  * until the final block of plaintext has been encrypted.
55  *
56  * To decrypt the message, decrypt the first block of ciphertext and XOR the result
57  * with the IV. The result is the first plaintext block. For subsequent ciphertext
58  * blocks, decrypt each block and XOR the previous block of the encrypted message
59  * into the result.
60  *
61  * ## Padding #
62  * CBC operates on entire blocks of ciphertext and plaintext at a time. This
63  * means that message lengths must be a multiple of the block cipher block size.
64  * AES has a block size of 16 bytes no matter the key size. Since messages do
65  * not necessarily always have a length that is a multiple of 16 bytes, it may
66  * be necessary to pad the message to a 16-byte boundary. Padding requires
67  * the sender and receiver to implicitly agree on the padding convention.
68  * Improperly designed or implemented padding schemes may leak information
69  * to an attacker through a padding oracle attack for example.
70  *
71  * ## Initialization Vectors #
72  * The IV is generated by the party performing the encryption operation.
73  * Within the scope of any encryption key, the IV value must be unique.
74  * The IV does not need to be kept secret and is usually transmitted together
75  * with the ciphertext to the decryting party.
76  * In CBC mode, the IVs must not be predictable. Two recommended ways to
77  * generate IVs is to either:
78  *
79  * - Apply the block cipher (AESCBC), using the same key used with CBC,
80  * to a nonce. This nonce must be unique for each key-message pair.
81  * A counter will usually suffice. If the same symmetric key is used
82  * by both parties to encrypt messages, they should agree to use a
83  * nonce scheme that avoids generating the same nonce and thus IV twice.
84  * Incrementing the counter by two and making one party use even numbers
85  * and the other odd numbers is a common method to avoid such collisions.
86  * - Use a TRNG (True Random Number Generator) or PRNG
87  * (Pseudo-Random Number Generator) to generate a random number for use
88  * as IV.
89  *
90  * ## Drawbacks #
91  * CBC mode has several drawbacks. Unless interfacing with legacy devices,
92  * it is recommended to use an AEAD (Authenticated Encryption with Associated Data)
93  * mode such as CCM or GCM. Below is a non-exhaustive list of reasons to use
94  * a different block cipher mode of operation.
95  *
96  * - CBC mode does not offer authentication or integrity guarantees. In practice,
97  * this means that attackers can intercept the encrypted message and manipulate
98  * the ciphertext before sending the message on to the receiver. While this
99  * does not break confidentiality and reveal the plaintext, it has enabled several
100  * attacks in the past. This is especially problematic given that changing the
101  * ciphertext of a block will only corrupt the block itself and the subsequent
102  * block of resultant plaintext. This property may be used to manipulate only
103  * certain parts of the message.
104  *
105  * - CBC mode requires message lengths to be evenly divisible by the block size.
106  * This necessitates a padding scheme. Improperly implemented padding schemes
107  * may lead to vulnerabilities that can be exploited by attackers. It often
108  * makes more sense to use a dedicated stream cipher such as CTR (Counter) that
109  * does not have this restriction. CCM and GCM both use CTR for encryption.
110  *
111  * @anchor ti_drivers_AESCBC_Usage
112  * # Usage #
113  * ## Before starting a CBC operation #
114  *
115  * Before starting a CBC operation, the application must do the following:
116  * - Call #AESCBC_init() to initialize the driver
117  * - Call #AESCBC_Params_init() to initialize the #AESCBC_Params to default values.
118  * - Modify the #AESCBC_Params as desired
119  * - Call #AESCBC_open() to open an instance of the driver
120  * - Initialize a CryptoKey. These opaque data structures are representations
121  * of keying material and its storage. Depending on how the keying material
122  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
123  * initialized differently. The AESCBC API can handle all types of CryptoKey.
124  * However, not all device-specific implementions support all types of CryptoKey.
125  * Devices without a key store will not support CryptoKeys with keying material
126  * stored in a key store for example.
127  * All devices support plaintext CryptoKeys.
128  * - Initialise the #AESCBC_Operation using #AESCBC_Operation_init() and set all
129  * length, key, and buffer fields.
130  *
131  * ## Starting a CBC operation #
132  *
133  * The #AESCBC_oneStepEncrypt and #AESCBC_oneStepDecrypt functions perform a CBC operation
134  * in a single call. They will always be the most highly optimized routines with the
135  * least overhead and the fastest runtime. However, they require all plaintext
136  * or ciphertext to be available to the function at the start of the call.
137  * All devices support single call operations.
138  *
139  * ## After the CBC operation completes #
140  *
141  * After the CBC operation completes, the application should either start
142  * another operation or close the driver by calling #AESCBC_close().
143  *
144  * @anchor ti_drivers_AESCBC_Synopsis
145  * ## Synopsis
146  * @anchor ti_drivers_AESCBC_Synopsis_Code
147  * @code
148  * // Import AESCBC Driver definitions
149  * #include <ti/drivers/AESCBC.h>
150  *
151  * // Define name for AESCBC channel index
152  * #define AESCBC_INSTANCE 0
153  *
154  * AESCBC_init();
155  *
156  * handle = AESCBC_open(AESCBC_INSTANCE, NULL);
157  *
158  * // Initialize symmetric key
159  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
160  *
161  * // Set up AESCBC_Operation
162  * AESCBC_Operation_init(&operation);
163  * operation.key = &cryptoKey;
164  * operation.input = plaintext;
165  * operation.output = ciphertext;
166  * operation.inputLength = sizeof(plaintext);
167  * operation.iv = iv;
168  *
169  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
170  *
171  * AESCBC_close(handle);
172  * @endcode
173  *
174  * @anchor ti_drivers_AESCBC_Examples
175  * ## Examples
176  *
177  * ### Single call CBC encryption with plaintext CryptoKey in blocking return mode #
178  * @code
179  *
180  * #include <ti/drivers/AESCBC.h>
181  * #include <ti/drivers/types/cryptoKey/CryptoKey_Plaintext.h>
182  *
183  * ...
184  *
185  * AESCBC_Handle handle;
186  * CryptoKey cryptoKey;
187  * int_fast16_t encryptionResult;
188  *
189  * // For example purposes only. Generate IVs in a non-static way in practice.
190  * // Test vector 0 from NIST CAPV set CBCMMT128
191  * uint8_t iv[16] = {0x2f, 0xe2, 0xb3, 0x33, 0xce, 0xda, 0x8f, 0x98,
192  * 0xf4, 0xa9, 0x9b, 0x40, 0xd2, 0xcd, 0x34, 0xa8};
193  * uint8_t plaintext[16] = {0x45, 0xcf, 0x12, 0x96, 0x4f, 0xc8, 0x24, 0xab,
194  * 0x76, 0x61, 0x6a, 0xe2, 0xf4, 0xbf, 0x08, 0x22};
195  * uint8_t ciphertext[sizeof(plaintext)];
196  * uint8_t keyingMaterial[16] = {0x1f, 0x8e, 0x49, 0x73, 0x95, 0x3f, 0x3f, 0xb0,
197  * 0xbd, 0x6b, 0x16, 0x66, 0x2e, 0x9a, 0x3c, 0x17};
198  *
199  * // The ciphertext should be the following after the encryption operation:
200  * // 0x0f, 0x61, 0xc4, 0xd4, 0x4c, 0x51, 0x47, 0xc0
201  * // 0x3c, 0x19, 0x5a, 0xd7, 0xe2, 0xcc, 0x12, 0xb2
202  *
203  *
204  * handle = AESCBC_open(0, NULL);
205  *
206  * if (handle == NULL) {
207  * // handle error
208  * }
209  *
210  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
211  *
212  * AESCBC_Operation operation;
213  * AESCBC_Operation_init(&operation);
214  *
215  * operation.key = &cryptoKey;
216  * operation.input = plaintext;
217  * operation.output = ciphertext;
218  * operation.inputLength = sizeof(plaintext);
219  * operation.iv = iv;
220  *
221  * encryptionResult = AESCBC_oneStepEncrypt(handle, &operation);
222  *
223  * if (encryptionResult != AESCBC_STATUS_SUCCESS) {
224  * // handle error
225  * }
226  *
227  * AESCBC_close(handle);
228  *
229  * @endcode
230  *
231  * ### Single call CBC decryption with plaintext CryptoKey in callback return mode #
232  * @code
233  *
234  * #include <ti/drivers/AESCBC.h>
235  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
236  *
237  * ...
238  *
239  * // Test vector 0 from NIST CAPV set CBCMMT256
240  *
241  * uint8_t iv[16] = {0xdd, 0xbb, 0xb0, 0x17, 0x3f, 0x1e, 0x2d, 0xeb,
242  * 0x23, 0x94, 0xa6, 0x2a, 0xa2, 0xa0, 0x24, 0x0e};
243  * uint8_t ciphertext[16] = {0xd5, 0x1d, 0x19, 0xde, 0xd5, 0xca, 0x4a, 0xe1,
244  * 0x4b, 0x2b, 0x20, 0xb0, 0x27, 0xff, 0xb0, 0x20};
245  * uint8_t keyingMaterial[] = {0x43, 0xe9, 0x53, 0xb2, 0xae, 0xa0, 0x8a, 0x3a,
246  * 0xd5, 0x2d, 0x18, 0x2f, 0x58, 0xc7, 0x2b, 0x9c,
247  * 0x60, 0xfb, 0xe4, 0xa9, 0xca, 0x46, 0xa3, 0xcb,
248  * 0x89, 0xe3, 0x86, 0x38, 0x45, 0xe2, 0x2c, 0x9e};
249  * uint8_t plaintext[sizeof(ciphertext)];
250  *
251  * // The plaintext should be the following after the decryption operation:
252  * // 0x07, 0x27, 0x0d, 0x0e, 0x63, 0xaa, 0x36, 0xda
253  * // 0xed, 0x8c, 0x6a, 0xde, 0x13, 0xac, 0x1a, 0xf1
254  *
255  *
256  * void cbcCallback(AESCBC_Handle handle,
257  * int_fast16_t returnValue,
258  * AESCBC_Operation *operation,
259  * AESCBC_OperationType operationType) {
260  *
261  * if (returnValue != AESCBC_STATUS_SUCCESS) {
262  * // handle error
263  * }
264  * }
265  *
266  * AESCBC_Operation operation;
267  *
268  * void cbcStartFunction(void) {
269  * AESCBC_Handle handle;
270  * AESCBC_Params params;
271  * CryptoKey cryptoKey;
272  * int_fast16_t decryptionResult;
273  *
274  * AESCBC_Params_init(&params);
275  * params.returnBehavior = AESCBC_RETURN_BEHAVIOR_CALLBACK;
276  * params.callbackFxn = cbcCallback;
277  *
278  * handle = AESCBC_open(0, &params);
279  *
280  * if (handle == NULL) {
281  * // handle error
282  * }
283  *
284  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
285  *
286  * AESCBC_Operation_init(&operation);
287  *
288  * operation.key = &cryptoKey;
289  * operation.input = plaintext;
290  * operation.output = ciphertext;
291  * operation.inputLength = sizeof(plaintext);
292  * operation.iv = iv;
293  *
294  * decryptionResult = AESCBC_oneStepDecrypt(handle, &operation);
295  *
296  * if (decryptionResult != AESCBC_STATUS_SUCCESS) {
297  * // handle error
298  * }
299  *
300  * // do other things while CBC operation completes in the background
301  * }
302  *
303  * @endcode
304  */
305 
306 #ifndef ti_drivers_AESCBC__include
307 #define ti_drivers_AESCBC__include
308 
309 #include <stdbool.h>
310 #include <stddef.h>
311 #include <stdint.h>
312 
314 
315 #ifdef __cplusplus
316 extern "C" {
317 #endif
318 
331 #define AESCBC_STATUS_RESERVED (-32)
332 
339 #define AESCBC_STATUS_SUCCESS (0)
340 
347 #define AESCBC_STATUS_ERROR (-1)
348 
357 #define AESCBC_STATUS_RESOURCE_UNAVAILABLE (-2)
358 
362 #define AESCBC_STATUS_CANCELED (-3)
363 
375 typedef struct AESCBC_Config_ {
377  void *object;
378 
380  void const *hwAttrs;
381 } AESCBC_Config;
382 
387 
409 typedef enum {
425 
429 typedef enum {
432 } AESCBC_Mode;
433 
438 typedef struct {
440  const uint8_t *input;
445  uint8_t *output;
451  uint8_t *iv;
457  size_t inputLength;
463 
467 typedef enum {
471 
487 typedef void (*AESCBC_CallbackFxn) (AESCBC_Handle handle,
488  int_fast16_t returnValue,
489  AESCBC_Operation *operation,
490  AESCBC_OperationType operationType);
491 
500 typedef struct {
503  uint32_t timeout;
506  void *custom;
509 } AESCBC_Params;
510 
517 
526 void AESCBC_init(void);
527 
540 void AESCBC_Params_init(AESCBC_Params *params);
541 
559 AESCBC_Handle AESCBC_open(uint_least8_t index, const AESCBC_Params *params);
560 
570 void AESCBC_close(AESCBC_Handle handle);
571 
580 void AESCBC_Operation_init(AESCBC_Operation *operationStruct);
581 
601 int_fast16_t AESCBC_oneStepEncrypt(AESCBC_Handle handle, AESCBC_Operation *operationStruct);
602 
622 int_fast16_t AESCBC_oneStepDecrypt(AESCBC_Handle handle, AESCBC_Operation *operationStruct);
623 
637 int_fast16_t AESCBC_cancelOperation(AESCBC_Handle handle);
638 
662 AESCBC_Handle AESCBC_construct(AESCBC_Config *config, const AESCBC_Params *params);
663 
681 int_fast16_t AESCBC_getNextIv(AESCBC_Handle handle, uint8_t *iv);
682 
683 #ifdef __cplusplus
684 }
685 #endif
686 
687 #endif /* ti_drivers_AESCBC__include */
const AESCBC_Params AESCBC_defaultParams
Default AESCBC_Params structure.
Definition: AESCBC.h:420
The CryptoKey type is an opaque representation of a cryptographic key.
Definition: AESCBC.h:416
AESCBC_ReturnBehavior returnBehavior
Definition: AESCBC.h:501
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:487
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:438
struct AESCBC_Config_ AESCBC_Config
AESCBC Global configuration.
Definition: AESCBC.h:468
CryptoKey datastructure.
Definition: CryptoKey.h:209
AESCBC_CallbackFxn callbackFxn
Definition: AESCBC.h:502
AESCBC_Mode
Enum for the direction of the CBC operation.
Definition: AESCBC.h:429
CryptoKey * key
Definition: AESCBC.h:439
AESCBC_ReturnBehavior
The way in which CBC function calls return after performing an encryption or decryption operation...
Definition: AESCBC.h:409
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:431
void AESCBC_init(void)
This function initializes the CBC module.
AESCBC Global configuration.
Definition: AESCBC.h:375
void const * hwAttrs
Definition: AESCBC.h:380
const uint8_t * input
Definition: AESCBC.h:440
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:506
Definition: AESCBC.h:430
Definition: AESCBC.h:410
AESCBC_OperationType
Enum for the operation types supported by the driver.
Definition: AESCBC.h:467
uint8_t * iv
Definition: AESCBC.h:451
Definition: AESCBC.h:469
AESCBC_Config * AESCBC_Handle
A handle that is returned from an AESCBC_open() call.
Definition: AESCBC.h:386
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:500
bool ivInternallyGenerated
Definition: AESCBC.h:458
uint8_t * output
Definition: AESCBC.h:445
void * object
Definition: AESCBC.h:377
uint32_t timeout
Definition: AESCBC.h:503
void AESCBC_Params_init(AESCBC_Params *params)
Function to initialize the AESCBC_Params struct to its defaults.
size_t inputLength
Definition: AESCBC.h:457
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale