1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Crypto Component                                                 */
16 /**                                                                       */
17 /**   Transport Layer Security (TLS)                                      */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #ifndef NX_CRYPTO_STANDALONE_ENABLE
23 #include "nx_secure_tls.h"
24 
25 
26 /**************************************************************************/
27 /*                                                                        */
28 /*  FUNCTION                                               RELEASE        */
29 /*                                                                        */
30 /*    nx_crypto_generic_ciphersuites                      PORTABLE C      */
31 /*                                                           6.2.1       */
32 /*  AUTHOR                                                                */
33 /*                                                                        */
34 /*    Timothy Stapko, Microsoft Corporation                               */
35 /*                                                                        */
36 /*  DESCRIPTION                                                           */
37 /*                                                                        */
38 /*     This table of function pointers provides a mapping from TLS        */
39 /*     ciphersuites to the necessary cryptographic methods for a given    */
40 /*     platform. It can be used as a model to develop a hardware-specific */
41 /*     cryptography table for TLS.                                        */
42 /*                                                                        */
43 /*  INPUT                                                                 */
44 /*                                                                        */
45 /*    None                                                                */
46 /*                                                                        */
47 /*  OUTPUT                                                                */
48 /*                                                                        */
49 /*    None                                                                */
50 /*                                                                        */
51 /*  CALLS                                                                 */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLED BY                                                             */
56 /*                                                                        */
57 /*    Application Code                                                    */
58 /*                                                                        */
59 /*  RELEASE HISTORY                                                       */
60 /*                                                                        */
61 /*    DATE              NAME                      DESCRIPTION             */
62 /*                                                                        */
63 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
64 /*  09-30-2020     Timothy Stapko           Modified comment(s), added    */
65 /*                                            curves in the crypto array, */
66 /*                                            added TLS ciphersuite entry,*/
67 /*                                            resulting in version 6.1    */
68 /*  04-25-2022     Yuxin Zhou               Modified comment(s), added    */
69 /*                                            x25519 and x448 curves,     */
70 /*                                            resulting in version 6.1.11 */
71 /*  07-29-2022     Yuxin Zhou               Modified comment(s),          */
72 /*                                            added x448 curves,          */
73 /*                                            resulting in version 6.1.12 */
74 /*  10-31-2022     Yanwu Cai                Modified comment(s),          */
75 /*                                            resulting in version 6.2.0  */
76 /*  03-08-2023     Yanwu Cai                Modified comment(s),          */
77 /*                                            fixed compiler errors when  */
78 /*                                            x509 is disabled,           */
79 /*                                            resulting in version 6.2.1  */
80 /*                                                                        */
81 /**************************************************************************/
82 
83 /* Define cryptographic methods for use with TLS. */
84 
85 extern NX_CRYPTO_METHOD crypto_method_none;
86 extern NX_CRYPTO_METHOD crypto_method_null;
87 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_128;
88 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_256;
89 extern NX_CRYPTO_METHOD crypto_method_aes_ccm_8;
90 extern NX_CRYPTO_METHOD crypto_method_aes_ccm_16;
91 extern NX_CRYPTO_METHOD crypto_method_aes_128_gcm_16;
92 extern NX_CRYPTO_METHOD crypto_method_aes_256_gcm_16;
93 extern NX_CRYPTO_METHOD crypto_method_ecdsa;
94 extern NX_CRYPTO_METHOD crypto_method_ecdhe;
95 extern NX_CRYPTO_METHOD crypto_method_hmac_sha1;
96 extern NX_CRYPTO_METHOD crypto_method_hmac_sha256;
97 extern NX_CRYPTO_METHOD crypto_method_hmac_md5;
98 extern NX_CRYPTO_METHOD crypto_method_rsa;
99 extern NX_CRYPTO_METHOD crypto_method_pkcs1;
100 extern NX_CRYPTO_METHOD crypto_method_auth_psk;
101 extern NX_CRYPTO_METHOD crypto_method_ec_secp256;
102 extern NX_CRYPTO_METHOD crypto_method_ec_secp384;
103 extern NX_CRYPTO_METHOD crypto_method_ec_secp521;
104 extern NX_CRYPTO_METHOD crypto_method_ec_x25519;
105 extern NX_CRYPTO_METHOD crypto_method_ec_x448;
106 extern NX_CRYPTO_METHOD crypto_method_md5;
107 extern NX_CRYPTO_METHOD crypto_method_sha1;
108 extern NX_CRYPTO_METHOD crypto_method_sha224;
109 extern NX_CRYPTO_METHOD crypto_method_sha256;
110 extern NX_CRYPTO_METHOD crypto_method_sha384;
111 extern NX_CRYPTO_METHOD crypto_method_sha512;
112 extern NX_CRYPTO_METHOD crypto_method_hkdf_sha1;
113 extern NX_CRYPTO_METHOD crypto_method_hkdf_sha256;
114 extern NX_CRYPTO_METHOD crypto_method_tls_prf_1;
115 extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha256;
116 extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha384;
117 extern NX_CRYPTO_METHOD crypto_method_hkdf;
118 extern NX_CRYPTO_METHOD crypto_method_hmac;
119 
120 
121 /* Ciphersuite table without ECC. */
122 /* Lookup table used to map ciphersuites to cryptographic routines. */
123 /* For TLS Web servers, define NX_SECURE_ENABLE_AEAD_CIPHER to allow web browsers to connect using AES_128_GCM cipher suites. */
124 NX_SECURE_TLS_CIPHERSUITE_INFO _nx_crypto_ciphersuite_lookup_table[] =
125 {
126     /* Ciphersuite,                           public cipher,            public_auth,              session cipher & cipher mode,   iv size, key size,  hash method,                    hash size, TLS PRF */
127 #ifndef NX_SECURE_DISABLE_X509
128 #ifdef NX_SECURE_ENABLE_AEAD_CIPHER
129     {TLS_RSA_WITH_AES_128_GCM_SHA256,         &crypto_method_rsa,       &crypto_method_rsa,       &crypto_method_aes_128_gcm_16,  16,      16,        &crypto_method_null,            0,         &crypto_method_tls_prf_sha256},
130 #endif /* NX_SECURE_ENABLE_AEAD_CIPHER */
131     {TLS_RSA_WITH_AES_256_CBC_SHA256,         &crypto_method_rsa,       &crypto_method_rsa,       &crypto_method_aes_cbc_256,     16,      32,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
132     {TLS_RSA_WITH_AES_128_CBC_SHA256,         &crypto_method_rsa,       &crypto_method_rsa,       &crypto_method_aes_cbc_128,     16,      16,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
133 #endif /* NX_SECURE_DISABLE_X509 */
134 
135 #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
136     {TLS_PSK_WITH_AES_128_CBC_SHA256,         &crypto_method_null,      &crypto_method_auth_psk,  &crypto_method_aes_cbc_128,     16,      16,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
137 #ifdef NX_SECURE_ENABLE_AEAD_CIPHER
138     {TLS_PSK_WITH_AES_128_CCM_8,              &crypto_method_null,      &crypto_method_auth_psk,  &crypto_method_aes_ccm_8,       16,      16,        &crypto_method_null,            0,         &crypto_method_tls_prf_sha256},
139 #endif
140 #endif /* NX_SECURE_ENABLE_PSK_CIPHERSUITES */
141 };
142 
143 const UINT _nx_crypto_ciphersuite_lookup_table_size = sizeof(_nx_crypto_ciphersuite_lookup_table) / sizeof(NX_SECURE_TLS_CIPHERSUITE_INFO);
144 
145 #ifndef NX_SECURE_DISABLE_X509
146 /* Lookup table for X.509 digital certificates - they need a public-key algorithm and a hash routine for verification. */
147 NX_SECURE_X509_CRYPTO _nx_crypto_x509_cipher_lookup_table[] =
148 {
149     /* OID identifier,                        public cipher,            hash method */
150     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_256,    &crypto_method_rsa,       &crypto_method_sha256},
151     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_384,    &crypto_method_rsa,       &crypto_method_sha384},
152     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_512,    &crypto_method_rsa,       &crypto_method_sha512},
153     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_1,      &crypto_method_rsa,       &crypto_method_sha1},
154     {NX_SECURE_TLS_X509_TYPE_RSA_MD5,        &crypto_method_rsa,       &crypto_method_md5},
155 };
156 
157 const UINT _nx_crypto_x509_cipher_lookup_table_size = sizeof(_nx_crypto_x509_cipher_lookup_table) / sizeof(NX_SECURE_X509_CRYPTO);
158 #endif /* NX_SECURE_DISABLE_X509 */
159 
160 /* Define the object we can pass into TLS. */
161 NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers =
162 {
163     /* Ciphersuite lookup table and size. */
164     _nx_crypto_ciphersuite_lookup_table,
165     sizeof(_nx_crypto_ciphersuite_lookup_table) / sizeof(NX_SECURE_TLS_CIPHERSUITE_INFO),
166 
167 #ifndef NX_SECURE_DISABLE_X509
168     /* X.509 certificate cipher table and size. */
169     _nx_crypto_x509_cipher_lookup_table,
170     sizeof(_nx_crypto_x509_cipher_lookup_table) / sizeof(NX_SECURE_X509_CRYPTO),
171 #endif
172 
173     /* TLS version-specific methods. */
174 #if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
175     &crypto_method_md5,
176     &crypto_method_sha1,
177     &crypto_method_tls_prf_1,
178 #endif
179 
180 #if (NX_SECURE_TLS_TLS_1_2_ENABLED)
181     &crypto_method_sha256,
182     &crypto_method_tls_prf_sha256,
183 #endif
184 
185 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
186     &crypto_method_hkdf,
187     &crypto_method_hmac,
188     &crypto_method_ecdhe,
189 #endif
190 };
191 
192 
193 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
194 
195 #ifndef NX_SECURE_DISABLE_X509
196 
197 /* Lookup table for X.509 digital certificates - they need a public-key algorithm and a hash routine for verification. */
198 NX_SECURE_X509_CRYPTO _nx_crypto_x509_cipher_lookup_table_ecc[] =
199 {
200     /* OID identifier,                        public cipher,            hash method */
201     {NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256,  &crypto_method_ecdsa,     &crypto_method_sha256},
202     {NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_384,  &crypto_method_ecdsa,     &crypto_method_sha384},
203     {NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_512,  &crypto_method_ecdsa,     &crypto_method_sha512},
204     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_256,    &crypto_method_rsa,       &crypto_method_sha256},
205     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_384,    &crypto_method_rsa,       &crypto_method_sha384},
206     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_512,    &crypto_method_rsa,       &crypto_method_sha512},
207     {NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_224,  &crypto_method_ecdsa,     &crypto_method_sha224},
208     {NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_1,    &crypto_method_ecdsa,     &crypto_method_sha1},
209     {NX_SECURE_TLS_X509_TYPE_RSA_SHA_1,      &crypto_method_rsa,       &crypto_method_sha1},
210     {NX_SECURE_TLS_X509_TYPE_RSA_MD5,        &crypto_method_rsa,       &crypto_method_md5},
211 };
212 
213 const UINT _nx_crypto_x509_cipher_lookup_table_ecc_size = sizeof(_nx_crypto_x509_cipher_lookup_table_ecc) / sizeof(NX_SECURE_X509_CRYPTO);
214 
215 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
216 NX_SECURE_TLS_CIPHERSUITE_INFO _nx_crypto_ciphersuite_lookup_table_tls_1_3[] =
217 {
218 #ifdef NX_SECURE_ENABLE_AEAD_CIPHER
219     {TLS_AES_128_GCM_SHA256,                  &crypto_method_ecdhe,      &crypto_method_ecdsa,     &crypto_method_aes_128_gcm_16,  96,      16,        &crypto_method_sha256,         32,         &crypto_method_hkdf},
220     /* SHA-384 ciphersuites not yet supported... {TLS_AES_256_GCM_SHA384,                  &crypto_method_ecdhe,      &crypto_method_rsa,     &crypto_method_aes_256_gcm_16,  16,      16,        &crypto_method_sha384,         48,         &crypto_method_hkdf},*/
221     {TLS_AES_128_CCM_SHA256,                  &crypto_method_ecdhe,      &crypto_method_ecdsa,     &crypto_method_aes_ccm_16,       96,      16,        &crypto_method_sha256,         32,         &crypto_method_hkdf},
222     {TLS_AES_128_CCM_8_SHA256,                &crypto_method_ecdhe,      &crypto_method_ecdsa,     &crypto_method_aes_ccm_8,       96,      16,        &crypto_method_sha256,         32,         &crypto_method_hkdf},
223 #endif
224 };
225 
226 const UINT _nx_crypto_ciphersuite_lookup_table_tls_1_3_size = sizeof(_nx_crypto_ciphersuite_lookup_table_tls_1_3) / sizeof(NX_SECURE_TLS_CIPHERSUITE_INFO);
227 #endif
228 #endif
229 
230 /* Ciphersuite table with ECC. */
231 /* Lookup table used to map ciphersuites to cryptographic routines. */
232 /* Ciphersuites are negotiated IN ORDER - top priority first. Ciphersuites lower in the list are considered less secure. */
233 /* For TLS Web servers, define NX_SECURE_ENABLE_AEAD_CIPHER to allow web browsers to connect using AES_128_GCM cipher suites. */
234 NX_SECURE_TLS_CIPHERSUITE_INFO _nx_crypto_ciphersuite_lookup_table_ecc[] =
235 {
236     /* Ciphersuite,                           public cipher,            public_auth,              session cipher & cipher mode,   iv size, key size,  hash method,                    hash size, TLS PRF */
237 #ifndef NX_SECURE_DISABLE_X509
238 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
239     {TLS_AES_128_GCM_SHA256,                  &crypto_method_ecdhe,     &crypto_method_ecdsa,     &crypto_method_aes_128_gcm_16,  96,      16,        &crypto_method_sha256,         32,         &crypto_method_hkdf},
240     {TLS_AES_128_CCM_SHA256,                  &crypto_method_ecdhe,     &crypto_method_ecdsa,     &crypto_method_aes_ccm_16,      96,      16,        &crypto_method_sha256,         32,         &crypto_method_hkdf},
241     {TLS_AES_128_CCM_8_SHA256,                &crypto_method_ecdhe,     &crypto_method_ecdsa,     &crypto_method_aes_ccm_8,       96,      16,        &crypto_method_sha256,         32,         &crypto_method_hkdf},
242 #endif
243 
244 #ifdef NX_SECURE_ENABLE_AEAD_CIPHER
245     {TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, &crypto_method_ecdhe,     &crypto_method_ecdsa,     &crypto_method_aes_128_gcm_16,  16,      16,        &crypto_method_null,            0,         &crypto_method_tls_prf_sha256},
246     {TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,   &crypto_method_ecdhe,     &crypto_method_rsa,       &crypto_method_aes_128_gcm_16,  16,      16,        &crypto_method_null,            0,         &crypto_method_tls_prf_sha256},
247 #endif /* NX_SECURE_ENABLE_AEAD_CIPHER */
248 
249     {TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, &crypto_method_ecdhe,     &crypto_method_ecdsa,     &crypto_method_aes_cbc_128,     16,      16,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
250     {TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,   &crypto_method_ecdhe,     &crypto_method_rsa,       &crypto_method_aes_cbc_128,     16,      16,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
251 
252 #ifdef NX_SECURE_ENABLE_AEAD_CIPHER
253     {TLS_RSA_WITH_AES_128_GCM_SHA256,         &crypto_method_rsa,       &crypto_method_rsa,       &crypto_method_aes_128_gcm_16,  16,      16,        &crypto_method_null,            0,         &crypto_method_tls_prf_sha256},
254 #endif /* NX_SECURE_ENABLE_AEAD_CIPHER */
255 
256     {TLS_RSA_WITH_AES_256_CBC_SHA256,         &crypto_method_rsa,       &crypto_method_rsa,       &crypto_method_aes_cbc_256,     16,      32,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
257     {TLS_RSA_WITH_AES_128_CBC_SHA256,         &crypto_method_rsa,       &crypto_method_rsa,       &crypto_method_aes_cbc_128,     16,      16,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
258 #endif
259 
260 #ifdef NX_SECURE_ENABLE_PSK_CIPHERSUITES
261     {TLS_PSK_WITH_AES_128_CBC_SHA256,         &crypto_method_null,      &crypto_method_auth_psk,  &crypto_method_aes_cbc_128,     16,      16,        &crypto_method_hmac_sha256,     32,        &crypto_method_tls_prf_sha256},
262 #ifdef NX_SECURE_ENABLE_AEAD_CIPHER
263     {TLS_PSK_WITH_AES_128_CCM_8,              &crypto_method_null,      &crypto_method_auth_psk,  &crypto_method_aes_ccm_8,       16,      16,        &crypto_method_null,            0,         &crypto_method_tls_prf_sha256},
264 #endif
265 #endif /* NX_SECURE_ENABLE_PSK_CIPHERSUITES */
266 
267 
268 };
269 
270 const UINT _nx_crypto_ciphersuite_lookup_table_ecc_size = sizeof(_nx_crypto_ciphersuite_lookup_table_ecc) / sizeof(NX_SECURE_TLS_CIPHERSUITE_INFO);
271 
272 
273 /* Define the object we can pass into TLS. */
274 const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers_ecc =
275 {
276     /* Ciphersuite lookup table and size. */
277     _nx_crypto_ciphersuite_lookup_table_ecc,
278     sizeof(_nx_crypto_ciphersuite_lookup_table_ecc) / sizeof(NX_SECURE_TLS_CIPHERSUITE_INFO),
279 
280 #ifndef NX_SECURE_DISABLE_X509
281     /* X.509 certificate cipher table and size. */
282     _nx_crypto_x509_cipher_lookup_table_ecc,
283     sizeof(_nx_crypto_x509_cipher_lookup_table_ecc) / sizeof(NX_SECURE_X509_CRYPTO),
284 #endif
285 
286     /* TLS version-specific methods. */
287 #if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
288     &crypto_method_md5,
289     &crypto_method_sha1,
290     &crypto_method_tls_prf_1,
291 #endif
292 
293 #if (NX_SECURE_TLS_TLS_1_2_ENABLED)
294     &crypto_method_sha256,
295     &crypto_method_tls_prf_sha256,
296 #endif
297 
298 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
299     &crypto_method_hkdf,
300     &crypto_method_hmac,
301     &crypto_method_ecdhe,
302 #endif
303 
304 
305 };
306 
307 const USHORT nx_crypto_ecc_supported_groups[] =
308 {
309     (USHORT)NX_CRYPTO_EC_SECP256R1,
310 #ifdef NX_CRYPTO_ENABLE_CURVE25519_448
311     (USHORT)NX_CRYPTO_EC_X25519,
312     (USHORT)NX_CRYPTO_EC_X448,
313 #endif /* NX_CRYPTO_ENABLE_CURVE25519_448 */
314     (USHORT)NX_CRYPTO_EC_SECP384R1,
315     (USHORT)NX_CRYPTO_EC_SECP521R1,
316 };
317 
318 const NX_CRYPTO_METHOD *nx_crypto_ecc_curves[] =
319 {
320     &crypto_method_ec_secp256,
321 #ifdef NX_CRYPTO_ENABLE_CURVE25519_448
322     &crypto_method_ec_x25519,
323     &crypto_method_ec_x448,
324 #endif /* NX_CRYPTO_ENABLE_CURVE25519_448 */
325     &crypto_method_ec_secp384,
326     &crypto_method_ec_secp521,
327 };
328 
329 const UINT nx_crypto_ecc_supported_groups_size = sizeof(nx_crypto_ecc_supported_groups) / sizeof(USHORT);
330 #endif /* NX_SECURE_ENABLE_ECC_CIPHERSUITE */
331 
332 
333 
334 #if 0  /* This ciphersuite is provided for reference only. It can be used to construct legacy ciphersuites
335           for use with TLS 1.0 or TLS 1.1 (SHA-1 based ciphersuites are not currently supported in TLS 1.2). */
336 const NX_CRYPTO_CIPHERSUITE nx_crypto_tls_rsa_with_aes_128_cbc_sha =
337 /* TLS ciphersuite entry. */
338 {   TLS_RSA_WITH_AES_128_CBC_SHA,       /* Ciphersuite ID. */
339     NX_SECURE_APPLICATION_TLS,          /* Internal application label. */
340     16,                                 /* Symmetric key size. */
341     {   /* Cipher role array. */
342         {NX_CRYPTO_KEY_EXCHANGE_RSA,             NX_CRYPTO_ROLE_KEY_EXCHANGE},
343         {NX_CRYPTO_DIGITAL_SIGNATURE_RSA,        NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
344         {NX_CRYPTO_ENCRYPTION_AES_CBC,           NX_CRYPTO_ROLE_SYMMETRIC},
345         {NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_160, NX_CRYPTO_ROLE_MAC_HASH},
346         {NX_CRYPTO_HASH_SHA1,                    NX_CRYPTO_ROLE_RAW_HASH},
347         {NX_CRYPTO_HASH_HMAC,                    NX_CRYPTO_ROLE_HMAC},
348         {NX_CRYPTO_PRF_HMAC_SHA2_256,            NX_CRYPTO_ROLE_PRF},
349         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_NONE}
350     },
351     /* TLS/DTLS Versions supported. */
352     (NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3)
353 };
354 #endif
355 
356 const NX_CRYPTO_CIPHERSUITE nx_crypto_tls_rsa_with_aes_128_cbc_sha256 =
357 /* TLS ciphersuite entry. */
358 {   TLS_RSA_WITH_AES_128_CBC_SHA256,    /* Ciphersuite ID. */
359     NX_SECURE_APPLICATION_TLS,          /* Internal application label. */
360     16,                                 /* Symmetric key size. */
361     {   /* Cipher role array. */
362         {NX_CRYPTO_KEY_EXCHANGE_RSA,             NX_CRYPTO_ROLE_KEY_EXCHANGE},
363         {NX_CRYPTO_KEY_EXCHANGE_RSA,             NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
364         {NX_CRYPTO_ENCRYPTION_AES_CBC,           NX_CRYPTO_ROLE_SYMMETRIC},
365         {NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256, NX_CRYPTO_ROLE_MAC_HASH},
366         {NX_CRYPTO_HASH_SHA256,                  NX_CRYPTO_ROLE_RAW_HASH},
367         {NX_CRYPTO_HASH_HMAC,                    NX_CRYPTO_ROLE_HMAC},
368         {NX_CRYPTO_PRF_HMAC_SHA2_256,            NX_CRYPTO_ROLE_PRF},
369         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_NONE}
370     },
371     /* TLS/DTLS Versions supported. */
372     (NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3)
373 };
374 
375 const NX_CRYPTO_CIPHERSUITE nx_crypto_tls_ecdhe_rsa_with_aes_128_cbc_sha256 =
376 /* TLS ciphersuite entry. */
377 {   TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,   /* Ciphersuite ID. */
378     NX_SECURE_APPLICATION_TLS,               /* Internal application label. */
379     16,                                      /* Symmetric key size. */
380     {   /* Cipher role array. */
381         {NX_CRYPTO_KEY_EXCHANGE_ECDHE,           NX_CRYPTO_ROLE_KEY_EXCHANGE},
382         {NX_CRYPTO_KEY_EXCHANGE_RSA,             NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
383         {NX_CRYPTO_ENCRYPTION_AES_CBC,           NX_CRYPTO_ROLE_SYMMETRIC},
384         {NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256, NX_CRYPTO_ROLE_MAC_HASH},
385         {NX_CRYPTO_HASH_SHA256,                  NX_CRYPTO_ROLE_RAW_HASH},
386         {NX_CRYPTO_HASH_HMAC,                    NX_CRYPTO_ROLE_HMAC},
387         {NX_CRYPTO_PRF_HMAC_SHA2_256,            NX_CRYPTO_ROLE_PRF},
388         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_NONE}
389     },
390     /* TLS/DTLS Versions supported. */
391     (NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3)
392 };
393 
394 const NX_CRYPTO_CIPHERSUITE nx_crypto_tls_ecdhe_rsa_with_aes_128_gcm_sha256 =
395 /* TLS ciphersuite entry. */
396 {   TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,   /* Ciphersuite ID. */
397     NX_SECURE_APPLICATION_TLS,               /* Internal application label. */
398     16,                                      /* Symmetric key size. */
399     {   /* Cipher role array. */
400         {NX_CRYPTO_KEY_EXCHANGE_ECDHE,           NX_CRYPTO_ROLE_KEY_EXCHANGE},
401         {NX_CRYPTO_KEY_EXCHANGE_RSA,             NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
402         {NX_CRYPTO_ENCRYPTION_AES_GCM_16,        NX_CRYPTO_ROLE_SYMMETRIC},
403         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_MAC_HASH},
404         {NX_CRYPTO_HASH_SHA256,                  NX_CRYPTO_ROLE_RAW_HASH},
405         {NX_CRYPTO_HASH_HMAC,                    NX_CRYPTO_ROLE_HMAC},
406         {NX_CRYPTO_PRF_HMAC_SHA2_256,            NX_CRYPTO_ROLE_PRF},
407         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_NONE}
408     },
409     /* TLS/DTLS Versions supported. */
410     (NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3)
411 };
412 
413 const NX_CRYPTO_CIPHERSUITE nx_crypto_tls_ecdhe_ecdsa_with_aes_128_gcm_sha256 =
414 /* TLS ciphersuite entry. */
415 {   TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* Ciphersuite ID. */
416     NX_SECURE_APPLICATION_TLS,               /* Internal application label. */
417     16,                                      /* Symmetric key size. */
418     {   /* Cipher role array. */
419         {NX_CRYPTO_KEY_EXCHANGE_ECDHE,           NX_CRYPTO_ROLE_KEY_EXCHANGE},
420         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,      NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
421         {NX_CRYPTO_ENCRYPTION_AES_GCM_16,        NX_CRYPTO_ROLE_SYMMETRIC},
422         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_MAC_HASH},
423         {NX_CRYPTO_HASH_SHA256,                  NX_CRYPTO_ROLE_RAW_HASH},
424         {NX_CRYPTO_HASH_HMAC,                    NX_CRYPTO_ROLE_HMAC},
425         {NX_CRYPTO_PRF_HMAC_SHA2_256,            NX_CRYPTO_ROLE_PRF},
426         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_NONE}
427     },
428     /* TLS/DTLS Versions supported. */
429     (NX_SECURE_TLS_BITFIELD_VERSIONS_PRE_1_3 | NX_SECURE_DTLS_BITFIELD_VERSIONS_PRE_1_3)
430 };
431 
432 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
433 const NX_CRYPTO_CIPHERSUITE nx_crypto_tls_aes_128_gcm_sha256 =
434 /* TLS ciphersuite entry. */
435 {   TLS_AES_128_GCM_SHA256,             /* Ciphersuite ID. */
436     NX_SECURE_APPLICATION_TLS,          /* Internal application label. */
437     16,                                 /* Symmetric key size. */
438     {   /* Cipher role array. */
439         {NX_CRYPTO_KEY_EXCHANGE_ECDHE,           NX_CRYPTO_ROLE_KEY_EXCHANGE},
440         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,      NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
441         {NX_CRYPTO_ENCRYPTION_AES_GCM_16,        NX_CRYPTO_ROLE_SYMMETRIC},
442         {NX_CRYPTO_HASH_SHA256,                  NX_CRYPTO_ROLE_MAC_HASH},
443         {NX_CRYPTO_HASH_SHA256,                  NX_CRYPTO_ROLE_RAW_HASH},
444         {NX_CRYPTO_HKDF_METHOD,                  NX_CRYPTO_ROLE_PRF},
445         {NX_CRYPTO_NONE,                         NX_CRYPTO_ROLE_NONE}
446     },
447     /* TLS/DTLS Versions supported. */
448     (NX_SECURE_TLS_BITFIELD_VERSION_1_3 | NX_SECURE_DTLS_BITFIELD_VERSION_1_3)
449 };
450 #endif
451 
452 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_rsa_md5 =
453 /* X.509 ciphersuite entry. */
454 {
455     NX_SECURE_TLS_X509_TYPE_RSA_MD5,
456     NX_SECURE_APPLICATION_X509,
457     0,                                 /* Symmetric key size. */
458     {
459         {NX_CRYPTO_KEY_EXCHANGE_RSA,         NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
460         {NX_CRYPTO_HASH_MD5,                 NX_CRYPTO_ROLE_SIGNATURE_HASH},
461         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
462     },
463     /* Versions supported. */
464     NX_SECURE_X509_BITFIELD_VERSION_3
465 };
466 
467 
468 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_rsa_sha_1 =
469 /* X.509 ciphersuite entry. */
470 {
471     NX_SECURE_TLS_X509_TYPE_RSA_SHA_1,
472     NX_SECURE_APPLICATION_X509,
473     0,                                 /* Symmetric key size. */
474     {
475         {NX_CRYPTO_KEY_EXCHANGE_RSA,         NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
476         {NX_CRYPTO_HASH_SHA1,                NX_CRYPTO_ROLE_SIGNATURE_HASH},
477         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
478     },
479     /* Versions supported. */
480     NX_SECURE_X509_BITFIELD_VERSION_3
481 };
482 
483 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_rsa_sha_256 =
484 /* X.509 ciphersuite entry. */
485 {
486     NX_SECURE_TLS_X509_TYPE_RSA_SHA_256,
487     NX_SECURE_APPLICATION_X509,
488     0,                                 /* Symmetric key size. */
489     {
490         {NX_CRYPTO_KEY_EXCHANGE_RSA,         NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
491         {NX_CRYPTO_HASH_SHA256,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
492         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
493     },
494     /* Versions supported. */
495     NX_SECURE_X509_BITFIELD_VERSION_3
496 };
497 
498 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_rsa_sha_384 =
499 /* X.509 ciphersuite entry. */
500 {
501     NX_SECURE_TLS_X509_TYPE_RSA_SHA_384,
502     NX_SECURE_APPLICATION_X509,
503     0,                                 /* Symmetric key size. */
504     {
505         {NX_CRYPTO_KEY_EXCHANGE_RSA,         NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
506         {NX_CRYPTO_HASH_SHA384,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
507         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
508     },
509     /* Versions supported. */
510     NX_SECURE_X509_BITFIELD_VERSION_3
511 };
512 
513 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_rsa_sha_512 =
514 /* X.509 ciphersuite entry. */
515 {
516     NX_SECURE_TLS_X509_TYPE_RSA_SHA_512,
517     NX_SECURE_APPLICATION_X509,
518     0,                                 /* Symmetric key size. */
519     {
520         {NX_CRYPTO_KEY_EXCHANGE_RSA,         NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
521         {NX_CRYPTO_HASH_SHA512,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
522         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
523     },
524     /* Versions supported. */
525     NX_SECURE_X509_BITFIELD_VERSION_3
526 };
527 
528 
529 
530 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_ecdsa_sha_1 =
531 /* X.509 ciphersuite entry. */
532 {
533     NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_1,
534     NX_SECURE_APPLICATION_X509,
535     0,                                 /* Symmetric key size. */
536     {
537         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,  NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
538         {NX_CRYPTO_HASH_SHA1,                NX_CRYPTO_ROLE_SIGNATURE_HASH},
539         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
540     },
541     /* Versions supported. */
542     NX_SECURE_X509_BITFIELD_VERSION_3
543 };
544 
545 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_ecdsa_sha_224 =
546 /* X.509 ciphersuite entry. */
547 {
548     NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_224,
549     NX_SECURE_APPLICATION_X509,
550     0,                                 /* Symmetric key size. */
551     {
552         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,  NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
553         {NX_CRYPTO_HASH_SHA224,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
554         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
555     },
556     /* Versions supported. */
557     NX_SECURE_X509_BITFIELD_VERSION_3
558 };
559 
560 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_ecdsa_sha_256 =
561 /* X.509 ciphersuite entry. */
562 {
563     NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_256,
564     NX_SECURE_APPLICATION_X509,
565     0,                                 /* Symmetric key size. */
566     {
567         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,  NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
568         {NX_CRYPTO_HASH_SHA256,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
569         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
570     },
571     /* Versions supported. */
572     NX_SECURE_X509_BITFIELD_VERSION_3
573 };
574 
575 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_ecdsa_sha_384 =
576 /* X.509 ciphersuite entry. */
577 {
578     NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_384,
579     NX_SECURE_APPLICATION_X509,
580     0,                                 /* Symmetric key size. */
581     {
582         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,  NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
583         {NX_CRYPTO_HASH_SHA384,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
584         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
585     },
586     /* Versions supported. */
587     NX_SECURE_X509_BITFIELD_VERSION_3
588 };
589 
590 const NX_CRYPTO_CIPHERSUITE nx_crypto_x509_ecdsa_sha_512 =
591 /* X.509 ciphersuite entry. */
592 {
593     NX_SECURE_TLS_X509_TYPE_ECDSA_SHA_512,
594     NX_SECURE_APPLICATION_X509,
595     0,                                 /* Symmetric key size. */
596     {
597         {NX_CRYPTO_DIGITAL_SIGNATURE_ECDSA,  NX_CRYPTO_ROLE_SIGNATURE_CRYPTO},
598         {NX_CRYPTO_HASH_SHA512,              NX_CRYPTO_ROLE_SIGNATURE_HASH},
599         {NX_CRYPTO_NONE,                     NX_CRYPTO_ROLE_NONE}
600     },
601     /* Versions supported. */
602     NX_SECURE_X509_BITFIELD_VERSION_3
603 };
604 
605 
606 const NX_CRYPTO_METHOD *supported_crypto[] =
607 {
608     &crypto_method_none,
609     &crypto_method_rsa,
610     &crypto_method_pkcs1,
611     &crypto_method_ecdhe,
612     &crypto_method_ecdsa,
613     &crypto_method_aes_ccm_8,
614     &crypto_method_aes_cbc_128,
615     &crypto_method_aes_cbc_256,
616     &crypto_method_aes_128_gcm_16,
617     &crypto_method_aes_256_gcm_16,
618     &crypto_method_hmac,
619     &crypto_method_hmac_md5,
620     &crypto_method_hmac_sha1,
621     &crypto_method_hmac_sha256,
622     &crypto_method_md5,
623     &crypto_method_sha1,
624     &crypto_method_sha224,
625     &crypto_method_sha256,
626     &crypto_method_sha384,
627     &crypto_method_sha512,
628     &crypto_method_tls_prf_1,
629     &crypto_method_tls_prf_sha256,
630     &crypto_method_hkdf,
631     &crypto_method_ec_secp256,
632 #ifdef NX_CRYPTO_ENABLE_CURVE25519_448
633     &crypto_method_ec_x25519,
634     &crypto_method_ec_x448,
635 #endif /* NX_CRYPTO_ENABLE_CURVE25519_448 */
636     &crypto_method_ec_secp384,
637     &crypto_method_ec_secp521,
638 };
639 
640 const UINT supported_crypto_size = sizeof(supported_crypto) / sizeof(NX_CRYPTO_METHOD*);
641 
642 const NX_CRYPTO_CIPHERSUITE *ciphersuite_map[] =
643 {
644     /* TLS ciphersuites. */
645 #if (NX_SECURE_TLS_TLS_1_3_ENABLED)
646     &nx_crypto_tls_aes_128_gcm_sha256,
647 #endif
648     &nx_crypto_tls_ecdhe_rsa_with_aes_128_gcm_sha256,
649     &nx_crypto_tls_ecdhe_ecdsa_with_aes_128_gcm_sha256,
650     &nx_crypto_tls_rsa_with_aes_128_cbc_sha256,
651 
652     /* X.509 ciphersuites. */
653     &nx_crypto_x509_ecdsa_sha_256,
654     &nx_crypto_x509_ecdsa_sha_384,
655     &nx_crypto_x509_ecdsa_sha_512,
656     &nx_crypto_x509_rsa_sha_256,
657     &nx_crypto_x509_rsa_sha_384,
658     &nx_crypto_x509_rsa_sha_512,
659     &nx_crypto_x509_ecdsa_sha_224,
660     &nx_crypto_x509_ecdsa_sha_1,
661     &nx_crypto_x509_rsa_sha_1,
662     &nx_crypto_x509_rsa_md5,
663 };
664 
665 const UINT ciphersuite_map_size = sizeof(ciphersuite_map) / sizeof(NX_CRYPTO_CIPHERSUITE*);
666 
667 #endif /* NX_CRYPTO_STANDALONE_ENABLE */
668 
669 
670 
671 
672