AESCTR.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 AESCTR.h
34  *
35  * @brief AESCTR driver header
36  *
37  * @anchor ti_drivers_AESCTR_Overview
38  * <h3> Overview </h3>
39  * The Counter (CTR) mode of operation is a generic block cipher mode of operation
40  * that can be used with any block cipher including AES.
41  *
42  * CTR mode encrypts and decrypts messages. It is not required for the message
43  * length to be evenly divisible by the cipher block size. This also means
44  * that padding the message is not required.
45  *
46  * <h3> Operation </h3>
47  * CTR encryption and decryption perform the following steps:
48  * -# Set the counter value to the initial counter value
49  * -# Encrypt the counter value under the symmetric key
50  * -# XOR the encrypted counter value with the input block (plaintext or ciphertext)
51  * -# Increment the counter value. Interpret the byte array as a big-endian number.
52  * -# Repeat steps 2 to 4 until the input is completely processed. If the
53  * input is not evenly divisible by the block size, XOR the last
54  * (u = input length % block size) input bytes with the most significant
55  * u bytes of the last encrypted counter value.
56  *
57  * CTR performs the same steps regardless of whether it is used to
58  * encrypt or decrypt a message. The input merely changes.
59  *
60  * <h3> Choosing Initial Counter Values </h3>
61  * CTR requires that each counter value used to encrypt a block of a message
62  * is unique for each key used. If this requirement is not kept, the
63  * confidentiality of that message block may be compromised.
64  *
65  * There are two general strategies when choosing the initial counter value
66  * of a CTR operation to ensure this requirement holds.
67  *
68  * The first is to choose an initial counter value for the first message
69  * and increment the initial counter value for a subsequent message by
70  * by message length % block length (16-bytes for AES). This effectively
71  * turns a sequence of messages into one long message. If 0 is chosen
72  * as the initial counter value, up to 2^128 - 1 blocks may be encrypted before
73  * key rotation is mandatory.
74  *
75  * The second is to split the initial counter value into a nonce and
76  * counter section. The nonce of length n bits must be unique per message.
77  * This allows for up to 2^n - 1 messages to be encrypted before
78  * key rotation is required. The counter section of length c is incremented
79  * as usual. This limits messages to a length of at most 2^c - 1 blocks.
80  * n and c must be chosen such that n + c = block length in bits
81  * (128 bits for AES) holds.
82  *
83  * @anchor ti_drivers_AESCTR_Usage
84  * <h3> Usage </h3>
85  * <h4> Before starting a CTR operation </h4>
86  *
87  * Before starting a CTR operation, the application must do the following:
88  * - Call #AESCTR_init() to initialize the driver
89  * - Call #AESCTR_Params_init() to initialize the #AESCTR_Params to default values.
90  * - Modify the #AESCTR_Params as desired
91  * - Call #AESCTR_open() to open an instance of the driver
92  * - Initialize a CryptoKey. These opaque data structures are representations
93  * of keying material and its storage. Depending on how the keying material
94  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
95  * initialized differently. The AESCTR API can handle all types of CryptoKey.
96  * However, not all device-specific implementions support all types of CryptoKey.
97  * Devices without a key store will not support CryptoKeys with keying material
98  * stored in a key store for example.
99  * All devices support plaintext CryptoKeys.
100  * - Initialize the #AESCTR_Operation using #AESCTR_Operation_init() and set all
101  * length, key, and buffer fields.
102  *
103  * <h4> Starting a CTR operation </h4>
104  *
105  * The AESCTR_oneStepEncrypt() and AESCTR_oneStepDecrypt() functions perform a CTR operation
106  * in a single call.
107  *
108  * <h4> After the CTR operation completes </h4>
109  *
110  * After the CTR operation completes, the application should either start
111  * another operation or close the driver by calling #AESCTR_close().
112  *
113  * @anchor ti_drivers_AESCTR_Synopsis
114  * ## Synopsis
115  *
116  * @anchor ti_drivers_AESCTR_Synopsis_Code
117  * @code
118  *
119  * // Import AESCTR Driver definitions
120  * #include <ti/drivers/AESCTR.h>
121  *
122  * // Define name for AESCTR channel index
123  * #define AESCTR_INSTANCE 0
124  *
125  * AESCTR_init();
126  *
127  * handle = AESCTR_open(AESCTR_INSTANCE, NULL);
128  *
129  * // Initialize symmetric key
130  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
131  *
132  * // Set up AESCTR_Operation
133  * AESCTR_Operation_init(&operation);
134  * operation.key = &cryptoKey;
135  * operation.input = plaintext;
136  * operation.output = ciphertext;
137  * operation.inputLength = sizeof(plaintext);
138  * operation.iv = iv;
139  *
140  * encryptionResult = AESCTR_oneStepEncrypt(handle, &operation);
141  *
142  * AESCTR_close(handle);
143  * @endcode
144  *
145  * @anchor ti_drivers_AESCTR_Examples
146  * <h4> Examples </h4>
147  *
148  * <h5> Single call CTR encryption with plaintext CryptoKey in blocking return mode </h5>
149  * @code
150  *
151  * #include <ti/drivers/AESCTR.h>
152  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
153  *
154  * ...
155  *
156  * AESCTR_Handle handle;
157  * CryptoKey cryptoKey;
158  * int_fast16_t encryptionResult;
159  *
160  * // For example purposes only. Generate IVs in a non-static way in practice.
161  * // Test vector from NIST SP 800-38A
162  * uint8_t initialCounter[16] = {0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
163  * 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff};
164  * uint8_t plaintext[64] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
165  * 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
166  * 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
167  * 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
168  * 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
169  * 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
170  * 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
171  * 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10};
172  * ruint8_t ciphertext[sizeof(plaintext)];
173  * uint8_t keyingMaterial[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
174  * 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
175  *
176  * handle = AESCTR_open(0, NULL);
177  *
178  * if (handle == NULL) {
179  * // handle error
180  * while(1);
181  * }
182  *
183  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
184  *
185  * AESCTR_Operation operation;
186  * AESCTR_Operation_init(&operation);
187  *
188  * operation.key = &cryptoKey;
189  * operation.input = plaintext;
190  * operation.output = ciphertext;
191  * operation.inputLength = sizeof(plaintext);
192  * operation.initialCounter = initialCounter;
193  *
194  * encryptionResult = AESCTR_oneStepEncrypt(handle, &operation);
195  *
196  * if (encryptionResult != AESCTR_STATUS_SUCCESS) {
197  * // handle error
198  * while(1);
199  * }
200  *
201  * // The ciphertext should be the following after the encryption operation:
202  * // 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26,
203  * // 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce,
204  * // 0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff,
205  * // 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff,
206  * // 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e,
207  * // 0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab,
208  * // 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1,
209  * // 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee
210  *
211  * AESCTR_close(handle);
212  *
213  * @endcode
214  *
215  * <h5> Single call CTR decryption with plaintext CryptoKey in callback return mode </h5>
216  * @code
217  *
218  * #include <ti/drivers/AESCTR.h>
219  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
220  *
221  * ...
222  *
223  *
224  * void ctrCallback(AESCTR_Handle handle,
225  * int_fast16_t returnValue,
226  * AESCTR_Operation *operation,
227  * AESCTR_OperationType operationType) {
228  *
229  * if (returnValue != AESCTR_STATUS_SUCCESS) {
230  * // handle error
231  * while(1);
232  * }
233  * }
234  * AESCTR_Operation operation;
235  *
236  * void ctrStartFunction(void) {
237  * uint8_t initialCounter[16] = {0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F, 0x3F,
238  * 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00, 0x01};
239  * uint8_t ciphertext[] = {0xC1, 0xCF, 0x48, 0xA8, 0x9F, 0x2F, 0xFD, 0xD9,
240  * 0xCF, 0x46, 0x52, 0xE9, 0xEF, 0xDB, 0x72, 0xD7,
241  * 0x45, 0x40, 0xA4, 0x2B, 0xDE, 0x6D, 0x78, 0x36,
242  * 0xD5, 0x9A, 0x5C, 0xEA, 0xAE, 0xF3, 0x10, 0x53,
243  * 0x25, 0xB2, 0x07, 0x2F};
244  * uint8_t keyingMaterial[] = {0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
245  * 0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC};
246  * uint8_t plaintext[sizeof(ciphertext)];
247  *
248  * AESCTR_Handle handle;
249  * AESCTR_Params params;
250  * CryptoKey cryptoKey;
251  * int_fast16_t decryptionResult;
252  *
253  * AESCTR_Operation operation;
254  *
255  * AESCTR_Params_init(&params);
256  * params.returnBehavior = AESCTR_RETURN_BEHAVIOR_CALLBACK;
257  * params.callbackFxn = ctrCallback;
258  *
259  * handle = AESCTR_open(0, &params);
260  *
261  * if (handle == NULL) {
262  * // handle error
263  * while(1);
264  * }
265  *
266  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
267  *
268  * AESCTR_Operation_init(&operation);
269  *
270  * operation.key = &cryptoKey;
271  * operation.input = ciphertext;
272  * operation.output = plaintext;
273  * operation.inputLength = sizeof(ciphertext);
274  * operation.initialCounter = initialCounter;
275  *
276  * decryptionResult = AESCTR_oneStepDecrypt(handle, &operation);
277  *
278  * if (decryptionResult != AESCTR_STATUS_SUCCESS) {
279  * // handle error
280  * while(1);
281  * }
282  *
283  * // do other things while CTR operation completes in the background
284  *
285  * // After the operation completes and the callback is invoked, the resultant
286  * // plaintext should be
287  * // 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
288  * // 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
289  * // 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
290  * // 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
291  * // 0x20, 0x21, 0x22, 0x23
292  * }
293  *
294  * @endcode
295  */
296 
297 #ifndef ti_drivers_AESCTR__include
298 #define ti_drivers_AESCTR__include
299 
300 #include <stdbool.h>
301 #include <stddef.h>
302 #include <stdint.h>
303 
305 
306 #ifdef __cplusplus
307 extern "C" {
308 #endif
309 
310 
323 #define AESCTR_STATUS_RESERVED (-32)
324 
331 #define AESCTR_STATUS_SUCCESS (0)
332 
339 #define AESCTR_STATUS_ERROR (-1)
340 
349 #define AESCTR_STATUS_RESOURCE_UNAVAILABLE (-2)
350 
354 #define AESCTR_STATUS_CANCELED (-3)
355 
356 
378 typedef enum {
394 
398 typedef enum {
401 } AESCTR_Mode;
402 
410 typedef struct {
411  const CryptoKey *key;
412  uint8_t *input;
417  uint8_t *output;
423  const uint8_t *initialCounter;
428  size_t inputLength;
430 
434 typedef enum {
438 
450 typedef struct {
452  void *object;
453 
455  void const *hwAttrs;
456 } AESCTR_Config;
457 
462 
478 typedef void (*AESCTR_CallbackFxn) (AESCTR_Handle handle,
479  int_fast16_t returnValue,
480  AESCTR_Operation *operation,
481  AESCTR_OperationType operationType);
482 
491 typedef struct {
494  uint32_t timeout;
497  void *custom;
500 } AESCTR_Params;
501 
508 
517 void AESCTR_init(void);
518 
532 
550 AESCTR_Handle AESCTR_open(uint_least8_t index, const AESCTR_Params *params);
551 
561 void AESCTR_close(AESCTR_Handle handle);
562 
571 void AESCTR_Operation_init(AESCTR_Operation *operationStruct);
572 
592 int_fast16_t AESCTR_oneStepEncrypt(AESCTR_Handle handle, AESCTR_Operation *operationStruct);
593 
613 int_fast16_t AESCTR_oneStepDecrypt(AESCTR_Handle handle, AESCTR_Operation *operationStruct);
614 
628 int_fast16_t AESCTR_cancelOperation(AESCTR_Handle handle);
629 
653 AESCTR_Handle AESCTR_construct(AESCTR_Config *config, const AESCTR_Params *params);
654 
655 #ifdef __cplusplus
656 }
657 #endif
658 
659 #endif /* ti_drivers_AESCTR__include */
const AESCTR_Params AESCTR_defaultParams
Default AESCTR_Params structure.
Definition: AESCTR.h:399
ADC_Params params
Definition: Driver_Init.h:11
The CryptoKey type is an opaque representation of a cryptographic key.
AESCTR_Config * AESCTR_Handle
A handle that is returned from an AESCTR_open() call.
Definition: AESCTR.h:461
void AESCTR_init(void)
This function initializes the CTR module.
CTR Parameters.
Definition: AESCTR.h:491
AESCTR Global configuration.
Definition: AESCTR.h:450
void * object
Definition: AESCTR.h:452
const CryptoKey * key
Definition: AESCTR.h:411
AESCTR_OperationType
Enum for the operation types supported by the driver.
Definition: AESCTR.h:434
CryptoKey datastructure.
Definition: CryptoKey.h:209
AESCTR_ReturnBehavior
The way in which CTR function calls return after performing an encryption or decryption operation...
Definition: AESCTR.h:378
uint8_t * output
Definition: AESCTR.h:417
Definition: AESCTR.h:389
Definition: AESCTR.h:435
AESCTR_Mode
Enum for the direction of the CTR operation.
Definition: AESCTR.h:398
Struct containing the parameters required for encrypting/decrypting a message.
Definition: AESCTR.h:410
void AESCTR_Params_init(AESCTR_Params *params)
Function to initialize the AESCTR_Params struct to its defaults.
int_fast16_t AESCTR_oneStepDecrypt(AESCTR_Handle handle, AESCTR_Operation *operationStruct)
Function to perform an AESCTR decryption operation in one call.
Definition: AESCTR.h:400
Definition: AESCTR.h:379
void const * hwAttrs
Definition: AESCTR.h:455
AESCTR_CallbackFxn callbackFxn
Definition: AESCTR.h:493
int_fast16_t AESCTR_oneStepEncrypt(AESCTR_Handle handle, AESCTR_Operation *operationStruct)
Function to perform an AESCTR encryption operation in one call.
void * custom
Definition: AESCTR.h:497
int_fast16_t AESCTR_cancelOperation(AESCTR_Handle handle)
Cancels an ongoing AESCTR operation.
AESCTR_ReturnBehavior returnBehavior
Definition: AESCTR.h:492
Definition: AESCTR.h:385
AESCTR_Handle AESCTR_open(uint_least8_t index, const AESCTR_Params *params)
This function opens a given AESCTR peripheral.
void AESCTR_Operation_init(AESCTR_Operation *operationStruct)
Function to initialize an AESCTR_Operation struct to its defaults.
void(* AESCTR_CallbackFxn)(AESCTR_Handle handle, int_fast16_t returnValue, AESCTR_Operation *operation, AESCTR_OperationType operationType)
The definition of a callback function used by the AESCTR driver when used in AESCTR_RETURN_BEHAVIOR_C...
Definition: AESCTR.h:478
uint32_t timeout
Definition: AESCTR.h:494
AESCTR_Handle AESCTR_construct(AESCTR_Config *config, const AESCTR_Params *params)
Constructs a new AESCTR object.
const uint8_t * initialCounter
Definition: AESCTR.h:423
Definition: AESCTR.h:436
uint8_t * input
Definition: AESCTR.h:412
void AESCTR_close(AESCTR_Handle handle)
Function to close a CTR peripheral specified by the CTR handle.
size_t inputLength
Definition: AESCTR.h:428
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale