1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Crypto Component                                                 */
17 /**                                                                       */
18 /**   Elliptic-curve Diffie-Hellman (ECDH)                                */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
27 /*                                                                        */
28 /*    nx_crypto_ecdh.h                                    PORTABLE C      */
29 /*                                                           6.1.11       */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Timothy Stapko, Microsoft Corporation                               */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the basic Application Interface (API) to the      */
37 /*    NetX Crypto ECDH module.                                            */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
44 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
45 /*                                            resulting in version 6.1    */
46 /*  04-25-2022     Yuxin Zhou               Modified comment(s), supported*/
47 /*                                            x25519 and x448 curves,     */
48 /*                                            resulting in version 6.1.11 */
49 /*                                                                        */
50 /**************************************************************************/
51 
52 #ifndef NX_CRYPTO_ECDH_H
53 #define NX_CRYPTO_ECDH_H
54 
55 /* Determine if a C++ compiler is being used.  If so, ensure that standard
56    C is used to process the API information.  */
57 #ifdef __cplusplus
58 
59 /* Yes, C++ compiler is present.  Use standard C.  */
60 extern   "C" {
61 
62 #endif
63 
64 #include "nx_crypto_ec.h"
65 
66 
67 /* Max Elliptic-curve Diffie-Hellman key size. Buffer size for calculations is 4X the key size */
68 #define NX_CRYPTO_ECDH_MAX_KEY_SIZE     (68)
69 #ifndef NX_CRYPTO_ECDH_SCRATCH_BUFFER_SIZE
70 #define NX_CRYPTO_ECDH_SCRATCH_BUFFER_SIZE 2464
71 #endif /* NX_CRYPTO_ECDSA_SCRATCH_BUFFER_SIZE */
72 
73 /* Diffie-Hellman Key-exchange control structure. */
74 typedef struct NX_CRYPTO_ECDH_STRUCT
75 {
76     /* The size of the key being used. This is primarily for testing, but also allows for future expansion.
77        The value is assigned in _nx_crypto_dh_setup depending on the chosen group. */
78     UINT nx_crypto_ecdh_key_size;
79 
80     /* The private key is generated by nx_crypto_dh_setup and is a random number.
81        Make the array in units of UINT to make sure the starting address is 4-byte aligned. */
82     HN_UBASE nx_crypto_ecdh_private_key_buffer[NX_CRYPTO_ECDH_MAX_KEY_SIZE >> HN_SIZE_SHIFT];
83 
84     /* The elliptic curve selected in the call to nx_crypto_ecdh_setup.  */
85     NX_CRYPTO_EC *nx_crypto_ecdh_curve;
86 
87     HN_UBASE      nx_crypto_ecdh_scratch_buffer[NX_CRYPTO_ECDH_SCRATCH_BUFFER_SIZE >> HN_SIZE_SHIFT];
88 } NX_CRYPTO_ECDH;
89 
90 /* Function prototypes */
91 
92 
93 UINT _nx_crypto_ecdh_key_pair_import(NX_CRYPTO_ECDH  *ecdh_ptr,
94                                      NX_CRYPTO_EC *curve,
95                                      UCHAR  *local_private_key_ptr,
96                                      ULONG   local_private_key_len,
97                                      UCHAR  *local_public_key_ptr,
98                                      ULONG   local_public_key_len);
99 
100 UINT _nx_crypto_ecdh_private_key_export(NX_CRYPTO_ECDH  *ecdh_ptr,
101                                         UCHAR  *local_private_key_ptr,
102                                         ULONG   local_private_key_len,
103                                         ULONG  *actual_local_private_key_len);
104 
105 UINT _nx_crypto_ecdh_setup(NX_CRYPTO_ECDH  *ecdh_ptr,
106                            UCHAR  *local_public_key_ptr,
107                            ULONG   local_public_key_len_ptr,
108                            ULONG  *actual_local_public_key_len,
109                            NX_CRYPTO_EC *curve,
110                            HN_UBASE *scratch_buf_ptr);
111 
112 UINT _nx_crypto_ecdh_compute_secret(NX_CRYPTO_ECDH  *ecdh_ptr,
113                                     UCHAR  *share_secret_key_ptr,
114                                     ULONG   share_secret_key_len_ptr,
115                                     ULONG  *actual_share_secret_key_len,
116                                     UCHAR  *remote_public_key,
117                                     ULONG   remote_public_key_len,
118                                     HN_UBASE *scratch_buf_ptr);
119 
120 UINT _nx_crypto_method_ecdh_init(struct  NX_CRYPTO_METHOD_STRUCT *method,
121                                  UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
122                                  VOID  **handle,
123                                  VOID  *crypto_metadata,
124                                  ULONG crypto_metadata_size);
125 
126 UINT _nx_crypto_method_ecdh_cleanup(VOID *crypto_metadata);
127 
128 UINT _nx_crypto_method_ecdh_operation(UINT op,
129                                       VOID *handle,
130                                       struct NX_CRYPTO_METHOD_STRUCT *method,
131                                       UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
132                                       UCHAR *input, ULONG input_length_in_byte,
133                                       UCHAR *iv_ptr,
134                                       UCHAR *output, ULONG output_length_in_byte,
135                                       VOID *crypto_metadata, ULONG crypto_metadata_size,
136                                       VOID *packet_ptr,
137                                       VOID (*nx_crypto_hw_process_callback)(VOID *, UINT));
138 
139 #ifdef NX_CRYPTO_ENABLE_CURVE25519_448
140 UINT _nx_crypto_ecdh_key_pair_import_x25519_448(NX_CRYPTO_ECDH *ecdh_ptr,
141                                                 NX_CRYPTO_EC *curve,
142                                                 UCHAR *local_private_key_ptr,
143                                                 ULONG  local_private_key_len,
144                                                 UCHAR *local_public_key_ptr,
145                                                 ULONG  local_public_key_len);
146 UINT _nx_crypto_ecdh_private_key_export_x25519_448(NX_CRYPTO_ECDH *ecdh_ptr,
147                                                    UCHAR *local_private_key_ptr,
148                                                    ULONG  local_private_key_len,
149                                                    ULONG *actual_local_private_key_len);
150 UINT _nx_crypto_ecdh_setup_x25519_448(NX_CRYPTO_ECDH *ecdh_ptr,
151                                       UCHAR *local_public_key_ptr,
152                                       ULONG  local_public_key_len,
153                                       ULONG *actual_local_public_key_len,
154                                       NX_CRYPTO_EC *curve,
155                                       HN_UBASE *scratch_buf_ptr);
156 UINT _nx_crypto_ecdh_compute_secret_x25519_448(NX_CRYPTO_ECDH *ecdh_ptr,
157                                                UCHAR *share_secret_key_ptr,
158                                                ULONG  share_secret_key_len_ptr,
159                                                ULONG *actual_share_secret_key_len,
160                                                UCHAR *remote_public_key,
161                                                ULONG  remote_public_key_len,
162                                                HN_UBASE *scratch_buf_ptr);
163 #endif /* NX_CRYPTO_ENABLE_CURVE25519_448 */
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif /* NX_CRYPTO_ECDH_H */
170 
171