blob: 4c38b867a4c836f30029619ceb2d93f74027c80f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** NetX Secure Component */
/** */
/** Transport Layer Security (TLS) */
/** */
/**************************************************************************/
/**************************************************************************/
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_tls.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_secure_tls_metadata_size_calculate PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Timothy Stapko, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function can be used to determine the size of the buffer */
/* needed by TLS for encryption metadata for a given ciphersuite */
/* table. */
/* */
/* INPUT */
/* */
/* ciphersuite_table Ciphersuite table to analyze */
/* metadata_size Size needed to support the */
/* given ciphersuite table */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nx_secure_tls_metadata_size_calculate(const NX_SECURE_TLS_CRYPTO *crypto_table,
ULONG *metadata_size)
{
UINT i;
UINT max_public_cipher_metadata_size = 0;
UINT max_public_auth_metadata_size = 0;
UINT max_session_cipher_metadata_size = 0;
UINT max_hash_mac_metadata_size = 0;
UINT max_tls_prf_metadata_size = 0;
UINT max_handshake_hash_metadata_size = 0;
UINT max_handshake_hash_scratch_size = 0;
NX_SECURE_TLS_CIPHERSUITE_INFO *ciphersuite_table;
USHORT ciphersuite_table_size;
ULONG max_total_metadata_size;
#ifndef NX_SECURE_DISABLE_X509
NX_SECURE_X509_CRYPTO *cert_crypto;
USHORT cert_crypto_size;
#endif
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
const NX_CRYPTO_METHOD *crypto_method_md5;
const NX_CRYPTO_METHOD *crypto_method_sha1;
#endif
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
const NX_CRYPTO_METHOD *crypto_method_sha256;
#endif
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
crypto_method_md5 = crypto_table -> nx_secure_tls_handshake_hash_md5_method;
crypto_method_sha1 = crypto_table -> nx_secure_tls_handshake_hash_sha1_method;
#endif
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
crypto_method_sha256 = crypto_table -> nx_secure_tls_handshake_hash_sha256_method;
#endif
/* Get working pointers to our tables. */
ciphersuite_table = crypto_table -> nx_secure_tls_ciphersuite_lookup_table;
ciphersuite_table_size = crypto_table -> nx_secure_tls_ciphersuite_lookup_table_size;
#ifndef NX_SECURE_DISABLE_X509
cert_crypto = crypto_table -> nx_secure_tls_x509_cipher_table;
cert_crypto_size = crypto_table -> nx_secure_tls_x509_cipher_table_size;
#endif
/* Loop through the ciphersuite table and find the largest metadata for each type of cipher. */
for (i = 0; i < ciphersuite_table_size; ++i)
{
if (max_public_cipher_metadata_size < ciphersuite_table[i].nx_secure_tls_public_cipher -> nx_crypto_metadata_area_size)
{
max_public_cipher_metadata_size = ciphersuite_table[i].nx_secure_tls_public_cipher -> nx_crypto_metadata_area_size;
}
if (max_public_auth_metadata_size < ciphersuite_table[i].nx_secure_tls_public_auth -> nx_crypto_metadata_area_size)
{
max_public_auth_metadata_size = ciphersuite_table[i].nx_secure_tls_public_auth -> nx_crypto_metadata_area_size;
}
if (max_session_cipher_metadata_size < ciphersuite_table[i].nx_secure_tls_session_cipher -> nx_crypto_metadata_area_size)
{
max_session_cipher_metadata_size = ciphersuite_table[i].nx_secure_tls_session_cipher -> nx_crypto_metadata_area_size;
}
if (max_tls_prf_metadata_size < ciphersuite_table[i].nx_secure_tls_prf -> nx_crypto_metadata_area_size)
{
max_tls_prf_metadata_size = ciphersuite_table[i].nx_secure_tls_prf -> nx_crypto_metadata_area_size;
}
if (max_hash_mac_metadata_size < ciphersuite_table[i].nx_secure_tls_hash -> nx_crypto_metadata_area_size)
{
max_hash_mac_metadata_size = ciphersuite_table[i].nx_secure_tls_hash -> nx_crypto_metadata_area_size;
}
}
#ifndef NX_SECURE_DISABLE_X509
/* Loop through the certificate cipher table as well. */
for (i = 0; i < cert_crypto_size; ++i)
{
if (max_public_auth_metadata_size < cert_crypto[i].nx_secure_x509_public_cipher_method -> nx_crypto_metadata_area_size)
{
max_public_auth_metadata_size = cert_crypto[i].nx_secure_x509_public_cipher_method -> nx_crypto_metadata_area_size;
}
if (max_hash_mac_metadata_size < cert_crypto[i].nx_secure_x509_hash_method -> nx_crypto_metadata_area_size)
{
max_hash_mac_metadata_size = cert_crypto[i].nx_secure_x509_hash_method -> nx_crypto_metadata_area_size;
}
if (max_handshake_hash_scratch_size < cert_crypto[i].nx_secure_x509_hash_method -> nx_crypto_metadata_area_size)
{
max_handshake_hash_scratch_size = cert_crypto[i].nx_secure_x509_hash_method -> nx_crypto_metadata_area_size;
}
}
#endif
/* We also need metadata space for the TLS handshake hash, so add that into the total.
We need some scratch space to copy the handshake hash metadata during final hash generation
so figure out the largest metadata between SHA-1+MD5 (TLSv1.0, 1.1) and SHA256 (TLSv1.2). */
#if (NX_SECURE_TLS_TLS_1_0_ENABLED || NX_SECURE_TLS_TLS_1_1_ENABLED)
if (crypto_method_md5 != NX_NULL && crypto_method_sha1 != NX_NULL)
{
max_handshake_hash_metadata_size += (crypto_method_md5 -> nx_crypto_metadata_area_size +
crypto_method_sha1 -> nx_crypto_metadata_area_size);
if (max_handshake_hash_scratch_size < crypto_method_md5 -> nx_crypto_metadata_area_size + crypto_method_sha1 -> nx_crypto_metadata_area_size)
{
max_handshake_hash_scratch_size = crypto_method_md5 -> nx_crypto_metadata_area_size + crypto_method_sha1 -> nx_crypto_metadata_area_size;
}
if (max_tls_prf_metadata_size < crypto_table -> nx_secure_tls_prf_1_method -> nx_crypto_metadata_area_size)
{
max_tls_prf_metadata_size = crypto_table -> nx_secure_tls_prf_1_method -> nx_crypto_metadata_area_size;
}
}
#endif
#if (NX_SECURE_TLS_TLS_1_2_ENABLED)
max_handshake_hash_metadata_size += crypto_method_sha256 -> nx_crypto_metadata_area_size;
/* See if the scratch size from above is bigger. */
if (max_handshake_hash_scratch_size < crypto_method_sha256 -> nx_crypto_metadata_area_size)
{
max_handshake_hash_scratch_size = crypto_method_sha256 -> nx_crypto_metadata_area_size;
}
if (max_tls_prf_metadata_size < crypto_table -> nx_secure_tls_prf_sha256_method -> nx_crypto_metadata_area_size)
{
max_tls_prf_metadata_size = crypto_table -> nx_secure_tls_prf_sha256_method -> nx_crypto_metadata_area_size;
}
#endif
#if (NX_SECURE_TLS_TLS_1_3_ENABLED)
max_handshake_hash_scratch_size += crypto_table -> nx_secure_tls_hmac_method -> nx_crypto_metadata_area_size;
if (max_tls_prf_metadata_size < crypto_table -> nx_secure_tls_hkdf_method -> nx_crypto_metadata_area_size)
{
max_tls_prf_metadata_size = crypto_table -> nx_secure_tls_hkdf_method -> nx_crypto_metadata_area_size;
}
#endif
/* The public cipher and authentication should never be used simultaneously, so it should be OK
to share their metadata areas. */
if (max_public_cipher_metadata_size < max_public_auth_metadata_size)
{
max_public_cipher_metadata_size = max_public_auth_metadata_size;
}
/* The Total metadata size needed is the sum of all the maximums calculated above.
We need to keep track of two separate session cipher states, one for the server and one for the client,
so account for that extra space. */
max_total_metadata_size = max_public_cipher_metadata_size +
(2 * max_session_cipher_metadata_size) +
max_hash_mac_metadata_size +
max_tls_prf_metadata_size +
max_handshake_hash_metadata_size +
max_handshake_hash_scratch_size;
*metadata_size = max_total_metadata_size;
return(NX_SUCCESS);
}
|