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 /**   ECJPAKE                                                             */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
27 /*                                                                        */
28 /*    nx_crypto_ecjpake.h                                 PORTABLE C      */
29 /*                                                           6.1          */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Timothy Stapko, Microsoft Corporation                               */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the symbols, structures and operations for        */
37 /*    Elliptic Curve J-PAKE.                                              */
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 /*                                                                        */
47 /**************************************************************************/
48 
49 #ifndef NX_CRYPTO_ECJPAKE_H
50 #define NX_CRYPTO_ECJPAKE_H
51 
52 /* Determine if a C++ compiler is being used.  If so, ensure that standard
53    C is used to process the API information.  */
54 #ifdef __cplusplus
55 
56 /* Yes, C++ compiler is present.  Use standard C.  */
57 extern   "C" {
58 
59 #endif
60 
61 
62 #include "nx_crypto.h"
63 #include "nx_crypto_ec.h"
64 
65 #define NX_CRYPTO_ECJPAKE_CLIENT_ID           "client"
66 #define NX_CRYPTO_ECJPAKE_SERVER_ID           "server"
67 
68 #ifndef NX_CRYPTO_ECJPAKE_SCRATCH_BUFFER_SIZE
69 #define NX_CRYPTO_ECJPAKE_SCRATCH_BUFFER_SIZE 4096
70 #endif /* NX_CRYPTO_ECJPAKE_SCRATCH_BUFFER_SIZE */
71 
72 typedef struct
73 {
74     UCHAR *nx_crypto_ecjpake_zkp_x;
75     UINT   nx_crypto_ecjpake_zkp_x_len;
76     UCHAR *nx_crypto_ecjpake_zkp_v;
77     UINT   nx_crypto_ecjpake_zkp_v_len;
78     UCHAR *nx_crypto_ecjpake_zkp_r;
79     UINT   nx_crypto_ecjpake_zkp_r_len;
80 } NX_CRYPTO_ECJPAKE_ZKP;
81 
82 typedef struct
83 {
84     NX_CRYPTO_EC         *nx_crypto_ecjpake_curve;
85 
86     NX_CRYPTO_EC_POINT    nx_crypto_ecjpake_public_x1;   /* x1 and x2 are always local public keys */
87     NX_CRYPTO_EC_POINT    nx_crypto_ecjpake_public_x2;
88     NX_CRYPTO_EC_POINT    nx_crypto_ecjpake_public_x3;   /* x3 and x4 are always peer public keys */
89     NX_CRYPTO_EC_POINT    nx_crypto_ecjpake_public_x4;
90 
91     NX_CRYPTO_HUGE_NUMBER nx_crypto_ecjpake_private_x2;
92 
93     NX_CRYPTO_METHOD     *nx_crypto_ecjpake_hash_method;
94     VOID                 *nx_crypto_ecjpake_hash_metadata;
95     ULONG                 nx_crypto_ecjpake_hash_metadata_size;
96 
97     UCHAR                *nx_crypto_ecjpake_psk;
98     UINT                  nx_crypto_ecjpake_psk_length;
99 
100     UCHAR                 nx_crypto_ecjpake_scratch_buffer[NX_CRYPTO_ECJPAKE_SCRATCH_BUFFER_SIZE];
101     HN_UBASE             *nx_crypto_ecjpake_scratch_ptr;
102 } NX_CRYPTO_ECJPAKE;
103 
104 VOID _nx_crypto_ecjpake_init(NX_CRYPTO_ECJPAKE *ecjpake,
105                              NX_CRYPTO_EC *curve,
106                              NX_CRYPTO_METHOD *hash_method,
107                              VOID *hash_metadata,
108                              ULONG hash_metadata_size,
109                              HN_UBASE **scratch_pptr);
110 
111 UINT _nx_crypto_ecjpake_hello_generate(NX_CRYPTO_ECJPAKE *ecjpake,
112                                        CHAR *id, UINT id_len,
113                                        UCHAR *output, ULONG output_length,
114                                        ULONG *actual_size,
115                                        HN_UBASE *scratch);
116 
117 UINT _nx_crypto_ecjpake_hello_process(NX_CRYPTO_ECJPAKE *ecjpake,
118                                       CHAR *id, UINT id_len,
119                                       UCHAR *input, UINT input_length,
120                                       HN_UBASE *scratch);
121 
122 UINT _nx_crypto_ecjpake_key_exchange_generate(NX_CRYPTO_ECJPAKE *ecjpake,
123                                               UCHAR *shared_secret,
124                                               UINT shared_secret_len,
125                                               CHAR *id, UINT id_len,
126                                               UCHAR *output, ULONG output_length,
127                                               ULONG *actual_size,
128                                               HN_UBASE *scratch);
129 
130 UINT _nx_crypto_ecjpake_key_exchange_process(NX_CRYPTO_ECJPAKE *ecjpake,
131                                              UCHAR *shared_secret,
132                                              UINT shared_secret_len,
133                                              CHAR *id, UINT id_len,
134                                              UCHAR *input, UINT input_length,
135                                              UCHAR *pms,
136                                              HN_UBASE *scratch);
137 
138 UINT _nx_crypto_ecjpake_schnorr_zkp_hash(NX_CRYPTO_METHOD *hash_method,
139                                          VOID *hash_metadata,
140                                          NX_CRYPTO_EC *curve,
141                                          NX_CRYPTO_EC_POINT *g,
142                                          NX_CRYPTO_EC_POINT *v,
143                                          NX_CRYPTO_EC_POINT *x,
144                                          NX_CRYPTO_HUGE_NUMBER *h,
145                                          CHAR *id,
146                                          UINT id_len,
147                                          HN_UBASE *scratch);
148 
149 UINT _nx_crypto_ecjpake_schnorr_zkp_generate(NX_CRYPTO_METHOD *hash_method,
150                                              VOID *hash_metadata,
151                                              NX_CRYPTO_EC *curve,
152                                              NX_CRYPTO_EC_POINT *g,
153                                              NX_CRYPTO_EC_POINT *v,
154                                              NX_CRYPTO_EC_POINT *public_key,
155                                              CHAR *id,
156                                              UINT id_len,
157                                              NX_CRYPTO_HUGE_NUMBER *private_key,
158                                              NX_CRYPTO_HUGE_NUMBER *r,
159                                              HN_UBASE *scratch);
160 
161 UINT _nx_crypto_ecjpake_schnorr_zkp_verify(NX_CRYPTO_METHOD *hash_method,
162                                            VOID *hash_metadata,
163                                            NX_CRYPTO_EC *curve,
164                                            NX_CRYPTO_EC_POINT *g,
165                                            NX_CRYPTO_EC_POINT *v,
166                                            NX_CRYPTO_EC_POINT *public_key,
167                                            CHAR *id,
168                                            UINT id_len,
169                                            NX_CRYPTO_HUGE_NUMBER *r,
170                                            HN_UBASE *scratch);
171 
172 VOID _nx_crypto_ecjpake_public_key_generate(NX_CRYPTO_EC *curve,
173                                             NX_CRYPTO_EC_POINT *x1,
174                                             NX_CRYPTO_EC_POINT *x3,
175                                             NX_CRYPTO_EC_POINT *x4,
176                                             NX_CRYPTO_HUGE_NUMBER *x2,
177                                             NX_CRYPTO_HUGE_NUMBER *s,
178                                             NX_CRYPTO_EC_POINT *g,
179                                             NX_CRYPTO_EC_POINT *public_key,
180                                             NX_CRYPTO_HUGE_NUMBER *private_key,
181                                             HN_UBASE *scratch);
182 
183 UINT _nx_crypto_ecjpake_pre_master_secret_generate(NX_CRYPTO_METHOD *hash_method,
184                                                    VOID *hash_metadata,
185                                                    NX_CRYPTO_EC *curve,
186                                                    NX_CRYPTO_EC_POINT *x4,
187                                                    NX_CRYPTO_HUGE_NUMBER *s,
188                                                    NX_CRYPTO_EC_POINT *public_key,
189                                                    NX_CRYPTO_HUGE_NUMBER *x2,
190                                                    UCHAR *pms,
191                                                    HN_UBASE *scratch);
192 
193 UINT _nx_crypto_ecjpake_key_encryption_key_generate(NX_CRYPTO_METHOD *hash_method,
194                                                     VOID *hash_metadata,
195                                                     UCHAR *key_expansion,
196                                                     UINT key_expansion_len,
197                                                     UCHAR *key_encryption_key);
198 
199 UINT _nx_crypto_method_ecjpake_init(struct NX_CRYPTO_METHOD_STRUCT *method,
200                                     UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
201                                     VOID **handle,
202                                     VOID *crypto_metadata,
203                                     ULONG crypto_metadata_size);
204 
205 UINT _nx_crypto_method_ecjpake_cleanup(VOID *crypto_metadata);
206 
207 UINT _nx_crypto_method_ecjpake_operation(UINT op,
208                                          VOID *handle,
209                                          struct NX_CRYPTO_METHOD_STRUCT *method,
210                                          UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
211                                          UCHAR *input, ULONG input_length_in_byte,
212                                          UCHAR *iv_ptr,
213                                          UCHAR *output, ULONG output_length_in_byte,
214                                          VOID *crypto_metadata, ULONG crypto_metadata_size,
215                                          VOID *packet_ptr,
216                                          VOID (*nx_crypto_hw_process_callback)(VOID *, UINT));
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif /* NX_CRYPTO_ECJPAKE_H */
222 
223