AESECB.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 AESECB.h
34  *
35  * @brief AESECB driver header
36  *
37  * @anchor ti_drivers_AESECB_Overview
38  * # Overview #
39  * The Electronic Code Book (ECB) mode of operation is a generic
40  * encryption block cipher mode. It can be used with any block cipher.
41  * AESECB encrypts or decrypts one or multiple blocks of plaintext or ciphertext
42  * using the Advanced Encryption Standard (AES) block cipher.
43  * Each input block is individually encrypted or decrypted. This means that
44  * blocks of ciphertext can be decrypted individually and out of order.
45  * Encrypting the same plaintext using the same key yields identical ciphertext.
46  * This raises several security issues. For this reason, it is not recommended
47  * that ECB be used unless interfacing with unupdatable legacy systems
48  * or where a standard specifies its use. Better alternatives would be an
49  * authenticated encryption with associated data (AEAD) mode such as
50  * CCM or GCM.
51  *
52  * The AES key is a shared secret between the two parties and has a length
53  * of 128, 192, or 256 bits.
54  *
55  * @anchor ti_drivers_AESECB_Usage
56  * # Usage #
57  *
58  * ## Before starting an ECB operation #
59  *
60  * Before starting an ECB operation, the application must do the following:
61  * - Call AESECB_init() to initialize the driver
62  * - Call AESECB_Params_init() to initialize the AESECB_Params to default values.
63  * - Modify the AESECB_Params as desired
64  * - Call AESECB_open() to open an instance of the driver
65  * - Initialize a CryptoKey. These opaque datastructures are representations
66  * of keying material and its storage. Depending on how the keying material
67  * is stored (RAM or flash, key store, key blob), the CryptoKey must be
68  * initialized differently. The AESECB API can handle all types of CryptoKey.
69  * However, not all device-specific implementions support all types of CryptoKey.
70  * Devices without a key store will not support CryptoKeys with keying material
71  * stored in a key store for example.
72  * All devices support plaintext CryptoKeys.
73  * - Initialize the AESECB_Operation using AESECB_Operation_init() and set all
74  * length, key, and buffer fields.
75  *
76  * ## Starting an ECB operation #
77  *
78  * The AESECB_oneStepEncrypt and AESECB_oneStepDecrypt functions do an ECB operation in a single call.
79  * They will always be the most highly optimized routines with the least overhead and the fastest
80  * runtime. Since ECB plaintext blocks are simply encrypted with the block cipher block by block,
81  * there is no difference in the ciphertext between encrypting two blocks in one go or encypting
82  * each block individually.
83  *
84  * ## After the ECB operation completes #
85  *
86  * After the ECB operation completes, the application should either start another operation
87  * or close the driver by calling AESECB_close()
88  *
89  * @anchor ti_drivers_AESECB_Synopsis
90  * ## Synopsis
91  * @anchor ti_drivers_AESECB_Synopsis_Code
92  * @code
93  * // Import AESECB Driver definitions
94  * #include <ti/drivers/AESECB.h>
95  *
96  * AESECB_init();
97  *
98  * // Define name for AESECB channel index
99  * #define AESECB_INSTANCE 0
100  *
101  * handle = AESECB_open(AESECB_INSTANCE, NULL);
102  *
103  * // Initialize symmetric key
104  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
105  *
106  * // Set up AESECB_Operation
107  * AESECB_Operation_init(&operation);
108  * operation.key = &cryptoKey;
109  * operation.input = plaintext;
110  * operation.output = ciphertext;
111  * operation.inputLength = sizeof(plaintext);
112  * operation.iv = iv;
113  *
114  * encryptionResult = AESECB_oneStepEncrypt(handle, &operation);
115  *
116  * AESECB_close(handle);
117  * @endcode
118  *
119  * @anchor ti_drivers_AESECB_Examples
120  *
121  * ## Examples
122  *
123  * ### Encyption of multiple plaintext blocks in blocking mode #
124  * @code
125  *
126  * #include <ti/drivers/AESECB.h>
127  * #include <ti/drivers/types/cryptoKey/CryptoKey_Plaintext.h>
128  *
129  * ...
130  *
131  * AESECB_Handle handle;
132  * CryptoKey cryptoKey;
133  * int_fast16_t encryptionResult;
134  * uint8_t plaintext[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
135  * 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
136  * 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
137  * 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51};
138  * uint8_t ciphertext[sizof(plaintext)];
139  * uint8_t keyingMaterial[16] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
140  * 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}
141  *
142  * handle = AESECB_open(0, NULL);
143  *
144  * if (handle == NULL) {
145  * // handle error
146  * }
147  *
148  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
149  *
150  * AESECB_Operation operation;
151  * AESECB_Operation_init(&operation);
152  *
153  * operation.key = &cryptoKey;
154  * operation.input = plaintext;
155  * operation.output = ciphertext;
156  * operation.inputLength = sizeof(plaintext);
157  *
158  * encryptionResult = AESECB_oneStepEncrypt(handle, &operation);
159  *
160  * if (encryptionResult != AESECB_STATUS_SUCCESS) {
161  * // handle error
162  * }
163  *
164  * // The resultant ciphertext should be:
165  * // 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60,
166  * // 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97,
167  * // 0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d,
168  * // 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf
169  *
170  *
171  * AESECB_close(handle);
172  *
173  * @endcode
174  *
175  * ### Single call ECB decryption in callback mode #
176  * @code
177  *
178  * #include <ti/drivers/AESECB.h>
179  * #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
180  *
181  * ...
182  *
183  * uint8_t ciphertext[] = {0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
184  * 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8};
185  * uint8_t keyingMaterial[32] = {0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
186  * 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
187  * 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
188  * 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4};
189  * uint8_t plaintext[sizeof(ciphertext)];
190  *
191  * // The plaintext should be the following after the decryption operation:
192  * // 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
193  * // 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
194  *
195  *
196  * void ecbCallback(AESECB_Handle handle,
197  * int_fast16_t returnValue,
198  * AESECB_Operation *operation,
199  * AESECB_OperationType operationType) {
200  *
201  * if (returnValue != AESECB_STATUS_SUCCESS) {
202  * // handle error
203  * }
204  * }
205  *
206  * AESECB_Operation operation;
207  *
208  * void ecbStartFunction(void) {
209  * AESECB_Handle handle;
210  * AESECB_Params params;
211  * CryptoKey cryptoKey;
212  * int_fast16_t decryptionResult;
213  *
214  * AESECB_Params_init(&params);
215  * params.returnBehavior = AESECB_RETURN_BEHAVIOR_CALLBACK;
216  * params.callbackFxn = ecbCallback;
217  *
218  * handle = AESECB_open(0, &params);
219  *
220  * if (handle == NULL) {
221  * // handle error
222  * }
223  *
224  * CryptoKeyPlaintext_initKey(&cryptoKey, keyingMaterial, sizeof(keyingMaterial));
225  *
226  * AESECB_Operation_init(&operation);
227  *
228  * operation.key = &cryptoKey;
229  * operation.input = plaintext;
230  * operation.output = ciphertext;
231  * operation.inputLength = sizeof(plaintext);
232  *
233  * decryptionResult = AESECB_oneStepDecrypt(handle, &operation);
234  *
235  * if (decryptionResult != AESECB_STATUS_SUCCESS) {
236  * // handle error
237  * }
238  *
239  * // do other things while ECB operation completes in the background
240  *
241  * }
242  *
243  *
244  * @endcode
245  */
246 
247 #ifndef ti_drivers_AESECB__include
248 #define ti_drivers_AESECB__include
249 
250 #include <stdbool.h>
251 #include <stddef.h>
252 #include <stdint.h>
253 
255 
256 #ifdef __cplusplus
257 extern "C" {
258 #endif
259 
272 #define AESECB_STATUS_RESERVED (-32)
273 
280 #define AESECB_STATUS_SUCCESS (0)
281 
288 #define AESECB_STATUS_ERROR (-1)
289 
298 #define AESECB_STATUS_RESOURCE_UNAVAILABLE (-2)
299 
303 #define AESECB_STATUS_CANCELED (-3)
304 
316 typedef struct {
318  void *object;
319 
321  void const *hwAttrs;
322 } AESECB_Config;
323 
328 
350 typedef enum {
366 
370 typedef enum {
373 } AESECB_Mode;
374 
379 typedef struct {
381  uint8_t *input;
386  uint8_t *output;
392  size_t inputLength;
396 
400 typedef enum {
404 
420 typedef void (*AESECB_CallbackFxn) (AESECB_Handle handle,
421  int_fast16_t returnValue,
422  AESECB_Operation *operation,
423  AESECB_OperationType operationType);
424 
433 typedef struct {
436  uint32_t timeout;
439  void *custom;
442 } AESECB_Params;
443 
450 
459 void AESECB_init(void);
460 
474 
492 AESECB_Handle AESECB_open(uint_least8_t index, const AESECB_Params *params);
493 
503 void AESECB_close(AESECB_Handle handle);
504 
513 void AESECB_Operation_init(AESECB_Operation *operationStruct);
514 
534 int_fast16_t AESECB_oneStepEncrypt(AESECB_Handle handle, AESECB_Operation *operation);
535 
555 int_fast16_t AESECB_oneStepDecrypt(AESECB_Handle handle, AESECB_Operation *operation);
556 
570 int_fast16_t AESECB_cancelOperation(AESECB_Handle handle);
571 
595 AESECB_Handle AESECB_construct(AESECB_Config *config, const AESECB_Params *params);
596 
597 #ifdef __cplusplus
598 }
599 #endif
600 
601 #endif /* ti_drivers_AESECB__include */
void * object
Definition: AESECB.h:318
ADC_Params params
Definition: Driver_Init.h:11
AESECB_ReturnBehavior returnBehavior
Definition: AESECB.h:434
The CryptoKey type is an opaque representation of a cryptographic key.
void AESECB_close(AESECB_Handle handle)
Function to close an ECB peripheral specified by the ECB handle.
int_fast16_t AESECB_cancelOperation(AESECB_Handle handle)
Cancels an ongoing AESECB operation.
AESECB_Handle AESECB_open(uint_least8_t index, const AESECB_Params *params)
This function opens a given ECB peripheral.
size_t inputLength
Definition: AESECB.h:392
Definition: AESECB.h:371
int_fast16_t AESECB_oneStepEncrypt(AESECB_Handle handle, AESECB_Operation *operation)
Function to perform an AESECB encryption operation in one call.
CryptoKey datastructure.
Definition: CryptoKey.h:209
Definition: AESECB.h:402
Definition: AESECB.h:361
ECB Parameters.
Definition: AESECB.h:433
void AESECB_init(void)
This function initializes the ECB module.
uint8_t * output
Definition: AESECB.h:386
void AESECB_Params_init(AESECB_Params *params)
Function to initialize the AESECB_Params struct to its defaults.
AESECB_ReturnBehavior
The way in which ECB function calls return after performing an encryption + authentication or decrypt...
Definition: AESECB.h:350
CryptoKey * key
Definition: AESECB.h:380
Struct containing the parameters required for encrypting/decrypting and a message.
Definition: AESECB.h:379
uint8_t * input
Definition: AESECB.h:381
int_fast16_t AESECB_oneStepDecrypt(AESECB_Handle handle, AESECB_Operation *operation)
Function to perform an AESECB decryption in one call.
uint32_t timeout
Definition: AESECB.h:436
void AESECB_Operation_init(AESECB_Operation *operationStruct)
Function to initialize an AESECB_Operation struct to its defaults.
Definition: AESECB.h:401
void(* AESECB_CallbackFxn)(AESECB_Handle handle, int_fast16_t returnValue, AESECB_Operation *operation, AESECB_OperationType operationType)
The definition of a callback function used by the AESECB driver when used in AESECB_RETURN_BEHAVIOR_C...
Definition: AESECB.h:420
Definition: AESECB.h:351
AESECB_Mode
Enum for the direction of the ECB operation.
Definition: AESECB.h:370
const AESECB_Params AESECB_defaultParams
Default AESECB_Params structure.
void * custom
Definition: AESECB.h:439
Definition: AESECB.h:357
AESECB_OperationType
Enum for the operation types supported by the driver.
Definition: AESECB.h:400
AESECB_Config * AESECB_Handle
A handle that is returned from an AESECB_open() call.
Definition: AESECB.h:327
Definition: AESECB.h:372
AESECB_CallbackFxn callbackFxn
Definition: AESECB.h:435
void const * hwAttrs
Definition: AESECB.h:321
AESECB_Handle AESECB_construct(AESECB_Config *config, const AESECB_Params *params)
Constructs a new AESECB object.
AESECB Global configuration.
Definition: AESECB.h:316
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale