ECIES.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, 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 ECIES.h
34  *
35  * @brief ECIES driver header
36  *
37  * @anchor ti_drivers_ECIES_Overview
38  * # Overview #
39  *
40  * The Elliptic Curve Integrated Encryption Scheme (ECIES) driver provides a
41  * public-key encryption scheme based on Elliptic Curve Cryptography. This
42  * driver supports the ECIES implementation defined in the SEC-1 v2.0 standard
43  * https://www.secg.org/sec1-v2.pdf except the cipher and MAC functions were
44  * changed to use AES-128-GCM:
45  *
46  * | Function | Implementation |
47  * |----------------|-----------------|
48  * | Key Agreement | ECDH NIST P-256 |
49  * | KDF | ANSI X9.63 |
50  * | Hash | SHA-256 |
51  * | Cipher | AES-128-GCM |
52  * | MAC | AES-128-GCM |
53  *
54  * @anchor ti_drivers_ECIES_Usage
55  * # Usage #
56  *
57  * Before starting a ECIES operation, the application must do the following:
58  * - Call #ECIES_init() to initialize the driver.
59  * - Call #ECIES_Params_init() to initialize the ECIES_Params to default values.
60  * - Modify the #ECIES_Params as desired.
61  * - Call #ECIES_open() to open an instance of the driver.
62  *
63  * @anchor ti_drivers_ECIES_Synopsis
64  * # Synopsis
65  *
66  * @anchor ti_drivers_ECIES_Synopsis_Code
67  * @code
68  *
69  * // Import ECIES Driver definitions
70  * #include <ti/drivers/ECIES.h>
71  *
72  * // Import driver configuration
73  * #include "ti_drivers_config.h"
74  *
75  * // Initialize driver
76  * ECIES_init();
77  *
78  * // Open driver instance
79  * handle = ECIES_open(CONFIG_ECIES_0, NULL);
80  *
81  * // Perform ECIES encryption
82  * result = ECIES_encrypt(handle, &publicKey, input, sizeof(input), output, sizeof(output));
83  *
84  * // Close driver instance
85  * ECIES_close(handle);
86  * @endcode
87  *
88  * @anchor ti_drivers_ECIES_Example
89  * # Example #
90  *
91  * The #ECIES_encrypt() function performs an ECIES encryption operation in
92  * a single call.
93  *
94  * After an ECIES operation completes, the application may either start
95  * another operation or close the driver by calling #ECIES_close().
96  *
97  * @code
98  * #define INPUT_SIZE 24
99  *
100  * CryptoKey publicKey;
101  * ECIES_Params params;
102  * ECIES_Handle handle;
103  * int_fast16_t result;
104  * uint8_t sharedInfo[] = {0x75, 0xee, 0xf8, 0x1a, 0xa3, 0x04, 0x1e, 0x33,
105  * 0xb8, 0x09, 0x71, 0x20, 0x3d, 0x2c, 0x0c, 0x52};
106  * uint8_t input[INPUT_SIZE] = {0x22, 0x51, 0x8b, 0x10, 0xe7, 0x0f, 0x2a, 0x3f,
107  * 0x24, 0x38, 0x10, 0xae, 0x32, 0x54, 0x13, 0x9e,
108  * 0xfb, 0xee, 0x04, 0xaa, 0x57, 0xc7, 0xaf, 0x7d};
109  * uint8_t output[ECIES_PADDING_BYTES + ECIES_PUBLIC_KEY_SIZE + INPUT_SIZE + ECIES_TAG_SIZE];
110  * uint8_t publicKeyMaterial[] = {0x04,
111  * // X coordinate
112  * 0x33,0x91,0x50,0x84,0x4E,0xC1,0x52,0x34,
113  * 0x80,0x7F,0xE8,0x62,0xA8,0x6B,0xE7,0x79,
114  * 0x77,0xDB,0xFB,0x3A,0xE3,0xD9,0x6F,0x4C,
115  * 0x22,0x79,0x55,0x13,0xAE,0xAA,0xB8,0x2F,
116  * // Y coordinate
117  * 0xB1,0xC1,0x4D,0xDF,0xDC,0x8E,0xC1,0xB2,
118  * 0x58,0x3F,0x51,0xE8,0x5A,0x5E,0xB3,0xA1,
119  * 0x55,0x84,0x0F,0x20,0x34,0x73,0x0E,0x9B,
120  * 0x5A,0xDA,0x38,0xB6,0x74,0x33,0x6A,0x21};
121  *
122  * // RNG initialization should be handled by the application after the RNG
123  * // driver is seeded with the noise input from Radio Control Layer.
124  *
125  * ECIES_init();
126  *
127  * ECIES_Params_init(&params);
128  * params.returnBehavior = ECIES_RETURN_BEHAVIOR_POLLING;
129  *
130  * handle = ECIES_open(CONFIG_ECIES_0, &params);
131  * assert(handle != NULL);
132  *
133  * CryptoKeyPlaintext_initKey(&publicKey, publicKeyMaterial, sizeof(publicKeyMaterial));
134  *
135  * result = ECIES_encrypt(handle, &publicKey, input, sizeof(input), output, sizeof(output));
136  * assert(result == ECIES_STATUS_SUCCESS);
137  *
138  * ECIES_close(handle);
139  * @endcode
140  */
141 
142 #ifndef ti_drivers_ECIES__include
143 #define ti_drivers_ECIES__include
144 
145 #include <stddef.h>
146 #include <stdint.h>
147 
148 #include <ti/drivers/ANSIX936KDF.h>
150 
151 #ifdef __cplusplus
152 extern "C" {
153 #endif
154 
167 #define ECIES_STATUS_RESERVED ((int_fast16_t)-32)
168 
175 #define ECIES_STATUS_SUCCESS ((int_fast16_t)0)
176 
183 #define ECIES_STATUS_ERROR ((int_fast16_t)-1)
184 
194 #define ECIES_STATUS_RESOURCE_UNAVAILABLE ((int_fast16_t)-2)
195 
199 #define ECIES_STATUS_INSUFFICIENT_OUTPUT_LENGTH ((int_fast16_t)-3)
200 
207 #define ECIES_STATUS_MAC_INVALID ((int_fast16_t)-4)
208 
215 #define ECIES_STATUS_UNALIGNED_IO_NOT_SUPPORTED AES_STATUS_UNALIGNED_IO_NOT_SUPPORTED
216 
238 typedef enum
239 {
245 
252 
256 #define ECIES_TAG_SIZE 16U
257 
258 #ifndef ECCParams_NISTP256_LENGTH
259 
262  #define ECCParams_NISTP256_LENGTH 32U
263 #endif
264 
270 #define ECIES_PUBLIC_KEY_SIZE (1U + (2U * ECCParams_NISTP256_LENGTH))
271 
279 #define ECIES_PADDING_BYTES 3U
280 
292 typedef struct
293 {
295  void *object;
296 
298  void const *hwAttrs;
299 } ECIES_Config;
300 
305 
314 typedef struct
315 {
320  uint32_t timeout;
323 } ECIES_Params;
324 
333 extern const ECIES_Config ECIES_config[];
334 
342 extern const uint_least8_t ECIES_count;
343 
349 extern const ECIES_Params ECIES_defaultParams;
350 
359 void ECIES_init(void);
360 
372 
393 ECIES_Handle ECIES_open(uint_least8_t index, const ECIES_Params *params);
394 
404 void ECIES_close(ECIES_Handle handle);
405 
449 int_fast16_t ECIES_encrypt(ECIES_Handle handle,
450  const CryptoKey *publicKey,
451  const void *input,
452  size_t inputLen,
453  void *paddedOutput,
454  size_t paddedOutputLen);
455 
499 int_fast16_t ECIES_decrypt(ECIES_Handle handle,
500  const CryptoKey *privateKey,
501  const void *paddedInput,
502  size_t paddedInputLen,
503  void *output,
504  size_t outputLen);
505 
534 ECIES_Handle ECIES_construct(ECIES_Config *config, const ECIES_Params *params);
535 
536 #ifdef __cplusplus
537 }
538 #endif
539 
540 #endif /* ti_drivers_ECIES__include */
ECIES_Handle ECIES_construct(ECIES_Config *config, const ECIES_Params *params)
Constructs a new ECIES object.
ADC_Params params
Definition: Driver_Init.h:11
ECIES_Handle ECIES_open(uint_least8_t index, const ECIES_Params *params)
Initializes a ECIES driver instance and returns a handle.
ECIES_ReturnBehavior returnBehavior
Definition: ECIES.h:316
Definition: ECIES.h:250
const ECIES_Params ECIES_defaultParams
Default ECIES_Params structure.
void ECIES_init(void)
Initializes the ECIES driver module.
const uint_least8_t ECIES_count
Global ECIES configuration count.
CryptoKey datastructure.
Definition: CryptoKey.h:208
int_fast16_t ECIES_encrypt(ECIES_Handle handle, const CryptoKey *publicKey, const void *input, size_t inputLen, void *paddedOutput, size_t paddedOutputLen)
Performs ECIES encryption of a message.
void ECIES_Params_init(ECIES_Params *params)
Initializes params with default values.
Definition: ECIES.h:244
void ECIES_close(ECIES_Handle handle)
Closes a ECIES peripheral specified by handle.
ECIES Global configuration.
Definition: ECIES.h:292
ANSIX936KDF driver header.
uint32_t timeout
Definition: ECIES.h:320
ECIES Parameters.
Definition: ECIES.h:314
int_fast16_t ECIES_decrypt(ECIES_Handle handle, const CryptoKey *privateKey, const void *paddedInput, size_t paddedInputLen, void *output, size_t outputLen)
Performs ECIES decryption of a message.
const ECIES_Config ECIES_config[]
Global ECIES configuration struct.
void * object
Definition: ECIES.h:295
ECIES_Config * ECIES_Handle
A handle that is returned from an ECIES_open() call.
Definition: ECIES.h:304
ECIES_ReturnBehavior
The way in which ECIES function calls return after performing an operation.
Definition: ECIES.h:238
void const * hwAttrs
Definition: ECIES.h:298
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale