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