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