tls13_misc.h
Go to the documentation of this file.
1 /**
2  * @file tls13_misc.h
3  * @brief TLS 1.3 helper functions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSL Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 #ifndef _TLS13_MISC_H
32 #define _TLS13_MISC_H
33 
34 //DHE key establishment
35 #ifndef TLS13_DHE_KE_SUPPORT
36  #define TLS13_DHE_KE_SUPPORT ENABLED
37 #elif (TLS13_DHE_KE_SUPPORT != ENABLED && TLS13_DHE_KE_SUPPORT != DISABLED)
38  #error TLS13_DHE_KE_SUPPORT parameter is not valid
39 #endif
40 
41 //ECDHE key exchange support
42 #ifndef TLS13_ECDHE_KE_SUPPORT
43  #define TLS13_ECDHE_KE_SUPPORT ENABLED
44 #elif (TLS13_ECDHE_KE_SUPPORT != ENABLED && TLS13_ECDHE_KE_SUPPORT != DISABLED)
45  #error TLS13_ECDHE_KE_SUPPORT parameter is not valid
46 #endif
47 
48 //ML-KEM key exchange support
49 #ifndef TLS13_MLKEM_KE_SUPPORT
50  #define TLS13_MLKEM_KE_SUPPORT DISABLED
51 #elif (TLS13_MLKEM_KE_SUPPORT != ENABLED && TLS13_MLKEM_KE_SUPPORT != DISABLED)
52  #error TLS13_MLKEM_KE_SUPPORT parameter is not valid
53 #endif
54 
55 //Hybrid key exchange support
56 #ifndef TLS13_HYBRID_KE_SUPPORT
57  #define TLS13_HYBRID_KE_SUPPORT DISABLED
58 #elif (TLS13_HYBRID_KE_SUPPORT != ENABLED && TLS13_HYBRID_KE_SUPPORT != DISABLED)
59  #error TLS13_HYBRID_KE_SUPPORT parameter is not valid
60 #endif
61 
62 //PSK-only key exchange support
63 #ifndef TLS13_PSK_KE_SUPPORT
64  #define TLS13_PSK_KE_SUPPORT DISABLED
65 #elif (TLS13_PSK_KE_SUPPORT != ENABLED && TLS13_PSK_KE_SUPPORT != DISABLED)
66  #error TLS13_PSK_KE_SUPPORT parameter is not valid
67 #endif
68 
69 //PSK with DHE key exchange support
70 #ifndef TLS13_PSK_DHE_KE_SUPPORT
71  #define TLS13_PSK_DHE_KE_SUPPORT ENABLED
72 #elif (TLS13_PSK_DHE_KE_SUPPORT != ENABLED && TLS13_PSK_DHE_KE_SUPPORT != DISABLED)
73  #error TLS13_PSK_DHE_KE_SUPPORT parameter is not valid
74 #endif
75 
76 //PSK with ECDHE key exchange support
77 #ifndef TLS13_PSK_ECDHE_KE_SUPPORT
78  #define TLS13_PSK_ECDHE_KE_SUPPORT ENABLED
79 #elif (TLS13_PSK_ECDHE_KE_SUPPORT != ENABLED && TLS13_PSK_ECDHE_KE_SUPPORT != DISABLED)
80  #error TLS13_PSK_ECDHE_KE_SUPPORT parameter is not valid
81 #endif
82 
83 //PSK with ML-KEM key exchange support
84 #ifndef TLS13_PSK_MLKEM_KE_SUPPORT
85  #define TLS13_PSK_MLKEM_KE_SUPPORT DISABLED
86 #elif (TLS13_PSK_MLKEM_KE_SUPPORT != ENABLED && TLS13_PSK_MLKEM_KE_SUPPORT != DISABLED)
87  #error TLS13_PSK_MLKEM_KE_SUPPORT parameter is not valid
88 #endif
89 
90 //PSK with hybrid key exchange support
91 #ifndef TLS13_PSK_HYBRID_KE_SUPPORT
92  #define TLS13_PSK_HYBRID_KE_SUPPORT DISABLED
93 #elif (TLS13_PSK_HYBRID_KE_SUPPORT != ENABLED && TLS13_PSK_HYBRID_KE_SUPPORT != DISABLED)
94  #error TLS13_PSK_HYBRID_KE_SUPPORT parameter is not valid
95 #endif
96 
97 //Early data support
98 #ifndef TLS13_EARLY_DATA_SUPPORT
99  #define TLS13_EARLY_DATA_SUPPORT DISABLED
100 #elif (TLS13_EARLY_DATA_SUPPORT != ENABLED && TLS13_EARLY_DATA_SUPPORT != DISABLED)
101  #error TLS13_EARLY_DATA_SUPPORT parameter is not valid
102 #endif
103 
104 //Middlebox compatibility mode
105 #ifndef TLS13_MIDDLEBOX_COMPAT_SUPPORT
106  #define TLS13_MIDDLEBOX_COMPAT_SUPPORT ENABLED
107 #elif (TLS13_MIDDLEBOX_COMPAT_SUPPORT != ENABLED && TLS13_MIDDLEBOX_COMPAT_SUPPORT != DISABLED)
108  #error TLS13_MIDDLEBOX_COMPAT_SUPPORT parameter is not valid
109 #endif
110 
111 //Maximum size for cookies
112 #ifndef TLS13_MAX_COOKIE_SIZE
113  #define TLS13_MAX_COOKIE_SIZE 256
114 #elif (TLS13_MAX_COOKIE_SIZE < 32)
115  #error TLS13_MAX_COOKIE_SIZE parameter is not valid
116 #endif
117 
118 //Maximum size for session tickets
119 #ifndef TLS13_MAX_TICKET_SIZE
120  #define TLS13_MAX_TICKET_SIZE 1024
121 #elif (TLS13_MAX_TICKET_SIZE < 32)
122  #error TLS13_MAX_TICKET_SIZE parameter is not valid
123 #endif
124 
125 //Maximum lifetime of session tickets
126 #ifndef TLS13_MAX_TICKET_LIFETIME
127  #define TLS13_MAX_TICKET_LIFETIME 604800
128 #elif (TLS13_MAX_TICKET_LIFETIME < 0)
129  #error TLS13_MAX_TICKET_LIFETIME parameter is not valid
130 #endif
131 
132 //Age tolerance for tickets, in milliseconds
133 #ifndef TLS13_TICKET_AGE_TOLERANCE
134  #define TLS13_TICKET_AGE_TOLERANCE 5000
135 #elif (TLS13_TICKET_AGE_TOLERANCE < 0)
136  #error TLS13_TICKET_AGE_TOLERANCE parameter is not valid
137 #endif
138 
139 //Number of NewSessionTicket message sent by the server
140 #ifndef TLS13_NEW_SESSION_TICKET_COUNT
141  #define TLS13_NEW_SESSION_TICKET_COUNT 2
142 #elif (TLS13_NEW_SESSION_TICKET_COUNT < 0)
143  #error TLS13_NEW_SESSION_TICKET_COUNT parameter is not valid
144 #endif
145 
146 //Maximum size for HKDF digests
147 #if (TLS_SHA384_SUPPORT == ENABLED)
148  #define TLS13_MAX_HKDF_DIGEST_SIZE 48
149 #else
150  #define TLS13_MAX_HKDF_DIGEST_SIZE 32
151 #endif
152 
153 //C++ guard
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 
159 /**
160  * @brief PSK key exchange modes
161  **/
162 
163 typedef enum
164 {
168 
169 
170 /**
171  * @brief Key update requests
172  **/
173 
174 typedef enum
175 {
179 
180 
181 //CC-RX, CodeWarrior or Win32 compiler?
182 #if defined(__CCRX__)
183  #pragma pack
184 #elif defined(__CWCC__) || defined(_WIN32)
185  #pragma pack(push, 1)
186 #endif
187 
188 
189 /**
190  * @brief Cookie
191  **/
192 
194 {
195  uint16_t length; //0-1
196  uint8_t value[]; //2
198 
199 
200 /**
201  * @brief Key share entry
202  **/
203 
204 typedef __packed_struct
205 {
206  uint16_t group; //0
207  uint16_t length; //1
208  uint8_t keyExchange[]; //2
210 
211 
212 /**
213  * @brief List of key shares
214  **/
215 
216 typedef __packed_struct
217 {
218  uint16_t length; //0
219  uint8_t value[]; //1
221 
222 
223 /**
224  * @brief List of PSK key exchange modes
225  **/
226 
227 typedef __packed_struct
228 {
229  uint8_t length; //0
230  uint8_t value[]; //1
232 
233 
234 /**
235  * @brief PSK identity
236  **/
237 
238 typedef __packed_struct
239 {
240  uint16_t length; //0-1
241  uint8_t value[]; //2
243 
244 
245 /**
246  * @brief List of PSK identities
247  **/
248 
249 typedef __packed_struct
250 {
251  uint16_t length; //0-1
252  uint8_t value[]; //2
254 
255 
256 /**
257  * @brief PSK binder
258  **/
259 
260 typedef __packed_struct
261 {
262  uint8_t length; //0
263  uint8_t value[]; //1
265 
266 
267 /**
268  * @brief List of PSK binders
269  **/
270 
271 typedef __packed_struct
272 {
273  uint16_t length; //0-1
274  uint8_t value[]; //2
276 
277 
278 /**
279  * @brief Certificate request context
280  **/
281 
282 typedef __packed_struct
283 {
284  uint8_t length; //0
285  uint8_t value[]; //1
287 
288 
289 /**
290  * @brief Digitally-signed element (TLS 1.3)
291  **/
292 
293 typedef __packed_struct
294 {
295  uint16_t algorithm; //0-1
296  uint16_t length; //2-3
297  uint8_t value[]; //4
299 
300 
301 /**
302  * @brief HelloRetryRequest message
303  **/
304 
305 typedef __packed_struct
306 {
307  uint16_t serverVersion; //0-1
308  uint8_t random[32]; //2-33
309  uint8_t sessionIdLen; //34
310  uint8_t sessionId[]; //35
312 
313 
314 /**
315  * @brief EndOfEarlyData message
316  **/
317 
318 typedef void *Tls13EndOfEarlyData;
319 
320 
321 /**
322  * @brief EncryptedExtensions message
323  **/
324 
325 typedef __packed_struct
326 {
327  uint16_t extensionsLen; //0-1
328  uint8_t extensions[]; //2
330 
331 
332 /**
333  * @brief NewSessionTicket message (TLS 1.3)
334  **/
335 
336 typedef __packed_struct
337 {
338  uint32_t ticketLifetime; //0-3
339  uint32_t ticketAgeAdd; //4-7
340  uint8_t ticketNonceLen; //8
341  uint8_t ticketNonce[]; //9
343 
344 
345 /**
346  * @brief KeyUpdate message
347  **/
348 
349 typedef __packed_struct
350 {
351  uint8_t requestUpdate; //0
353 
354 
355 /**
356  * @brief Session ticket
357  **/
358 
359 typedef __packed_struct
360 {
361  uint16_t length; //0-1
362  uint8_t data[]; //2
364 
365 
366 /**
367  * @brief Session state information
368  **/
369 
370 typedef __packed_struct
371 {
372  uint16_t version; ///<Protocol version
373  uint16_t cipherSuite; ///<Cipher suite identifier
374  systime_t ticketTimestamp; ///<Timestamp to manage ticket lifetime
375  uint32_t ticketLifetime; ///<Lifetime of the ticket
376  uint32_t ticketAgeAdd; ///<Random value used to obscure the age of the ticket
377  uint8_t ticketNonce[4]; ///<A per-ticket value that is unique across all tickets issued
378  size_t ticketPskLen; ///<Length of the PSK associated with the ticket
379  uint8_t ticketPsk[TLS13_MAX_HKDF_DIGEST_SIZE]; ///<PSK associated with the ticket
381 
382 
383 //CC-RX, CodeWarrior or Win32 compiler?
384 #if defined(__CCRX__)
385  #pragma unpack
386 #elif defined(__CWCC__) || defined(_WIN32)
387  #pragma pack(pop)
388 #endif
389 
390 //TLS 1.3 related constants
391 extern const uint8_t tls11DowngradeRandom[8];
392 extern const uint8_t tls12DowngradeRandom[8];
393 extern const uint8_t tls13HelloRetryRequestRandom[32];
394 
395 //TLS 1.3 related functions
396 error_t tls13ComputePskBinder(TlsContext *context, const void *clientHello,
397  size_t clientHelloLen, size_t truncatedClientHelloLen,
398  const Tls13PskIdentity *identity, uint8_t *binder, size_t binderLen);
399 
400 error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup);
401 
402 error_t tls13GenerateSharedSecret(TlsContext *context, const uint8_t *keyShare,
403  size_t length);
404 
405 error_t tls13Encapsulate(TlsContext *context, uint16_t namedGroup,
406  const uint8_t *keyShare, size_t length);
407 
408 error_t tls13Decapsulate(TlsContext *context, const uint8_t *keyShare,
409  size_t length);
410 
411 error_t tls13ComputeMac(TlsContext *context, TlsEncryptionEngine *encryptionEngine,
412  void *record, const uint8_t *data, size_t dataLen, uint8_t *mac);
413 
415 
417 
418 bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup);
419 bool_t tls13IsFfdheGroupSupported(TlsContext *context, uint16_t namedGroup);
420 bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup);
421 bool_t tls13IsMlkemGroupSupported(TlsContext *context, uint16_t namedGroup);
422 bool_t tls13IsHybridGroupSupported(TlsContext *context, uint16_t namedGroup);
423 
424 const KemAlgo *tls13GetMlkemAlgo(TlsContext *context, uint16_t namedGroup);
425 
427  uint16_t namedGroup);
428 
429 const KemAlgo *tls13GetNextGenAlgo(TlsContext *context, uint16_t namedGroup);
430 
431 error_t tls13CheckDuplicateKeyShare(uint16_t namedGroup, const uint8_t *p,
432  size_t length);
433 
434 error_t tls13FormatCertExtensions(uint8_t *p, size_t *written);
435 
436 error_t tls13ParseCertExtensions(const uint8_t *p, size_t length,
437  size_t *consumed);
438 
439 //C++ guard
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif
uint8_t sessionId[]
Definition: tls13_misc.h:310
uint32_t ticketLifetime
Lifetime of the ticket.
Definition: tls13_misc.h:375
Tls13PskBinderList
Definition: tls13_misc.h:275
#define TLS13_MAX_HKDF_DIGEST_SIZE
Definition: tls13_misc.h:148
error_t tls13ComputeMac(TlsContext *context, TlsEncryptionEngine *encryptionEngine, void *record, const uint8_t *data, size_t dataLen, uint8_t *mac)
Compute message authentication code.
Definition: tls13_misc.c:846
uint8_t keyExchange[]
Definition: tls13_misc.h:208
int bool_t
Definition: compiler_port.h:61
Tls13Cookie
Definition: tls13_misc.h:197
Tls13KeyUpdate
Definition: tls13_misc.h:352
error_t tls13GenerateKeyShare(TlsContext *context, uint16_t namedGroup)
Key share generation.
Definition: tls13_misc.c:260
uint8_t ticketNonce[]
A per-ticket value that is unique across all tickets issued.
Definition: tls13_misc.h:341
uint8_t algorithm
uint8_t p
Definition: ndp.h:300
uint16_t length
Definition: tls13_misc.h:207
error_t tls13DigestClientHello1(TlsContext *context)
Hash ClientHello1 in the transcript when HelloRetryRequest is used.
Definition: tls13_misc.c:888
typedef __packed_struct
Cookie.
Definition: tls13_misc.h:194
uint8_t data[]
Definition: tls13_misc.h:362
error_t tls13ParseCertExtensions(const uint8_t *p, size_t length, size_t *consumed)
Parse certificate extensions.
Definition: tls13_misc.c:1469
size_t ticketPskLen
Length of the PSK associated with the ticket.
Definition: tls13_misc.h:378
uint8_t version
Definition: coap_common.h:177
Tls13KeyShareEntry
Definition: tls13_misc.h:209
uint8_t ticketNonceLen
Definition: tls13_misc.h:340
error_t tls13Encapsulate(TlsContext *context, uint16_t namedGroup, const uint8_t *keyShare, size_t length)
Encapsulation algorithm.
Definition: tls13_misc.c:521
Tls13KeyUpdateRequest
Key update requests.
Definition: tls13_misc.h:175
bool_t tls13IsHybridGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given hybrid key exchange method is supported.
Definition: tls13_misc.c:1151
uint8_t ticketPsk[TLS13_MAX_HKDF_DIGEST_SIZE]
PSK associated with the ticket.
Definition: tls13_misc.h:379
@ TLS_PSK_KEY_EXCH_MODE_PSK_KE
Definition: tls13_misc.h:165
@ TLS_PSK_KEY_EXCH_MODE_PSK_DHE_KE
Definition: tls13_misc.h:166
bool_t tls13IsGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given named group is supported.
Definition: tls13_misc.c:971
Tls13PskKeyExchMode
PSK key exchange modes.
Definition: tls13_misc.h:164
#define TlsContext
Definition: tls.h:36
const KemAlgo * tls13GetMlkemAlgo(TlsContext *context, uint16_t namedGroup)
Get the ML-KEM algorithm that matches the specified named group.
Definition: tls13_misc.c:1193
error_t
Error codes.
Definition: error.h:43
Tls13PlaintextSessionState
Definition: tls13_misc.h:380
Tls13HelloRetryRequest
Definition: tls13_misc.h:311
systime_t ticketTimestamp
Timestamp to manage ticket lifetime.
Definition: tls13_misc.h:374
const KemAlgo * tls13GetNextGenAlgo(TlsContext *context, uint16_t namedGroup)
Get the next-gen algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:1330
error_t tls13Decapsulate(TlsContext *context, const uint8_t *keyShare, size_t length)
Decapsulation algorithm.
Definition: tls13_misc.c:696
Tls13PskBinder
Definition: tls13_misc.h:264
uint32_t dataLen
Definition: sftp_common.h:229
Tls13KeyShareList
Definition: tls13_misc.h:220
Tls13Ticket
Definition: tls13_misc.h:363
uint32_t systime_t
System time.
error_t tls13GenerateSharedSecret(TlsContext *context, const uint8_t *keyShare, size_t length)
(EC)DHE shared secret generation
Definition: tls13_misc.c:423
error_t tls13ComputePskBinder(TlsContext *context, const void *clientHello, size_t clientHelloLen, size_t truncatedClientHelloLen, const Tls13PskIdentity *identity, uint8_t *binder, size_t binderLen)
Compute PSK binder value.
Definition: tls13_misc.c:86
bool_t tls13IsEcdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given ECDHE group is supported.
Definition: tls13_misc.c:1052
uint8_t sessionIdLen
Definition: tls13_misc.h:309
uint8_t extensions[]
Definition: tls13_misc.h:328
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls13_misc.h:373
uint8_t value[]
Definition: tls13_misc.h:196
uint8_t random[32]
Definition: tls13_misc.h:308
Tls13NewSessionTicket
Definition: tls13_misc.h:342
bool_t tls13IsPskValid(TlsContext *context)
Check whether an externally established PSK is valid.
Definition: tls13_misc.c:933
bool_t tls13IsFfdheGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given FFDHE group is supported.
Definition: tls13_misc.c:1012
@ TLS_KEY_UPDATE_REQUESTED
Definition: tls13_misc.h:177
Tls13DigitalSignature
Definition: tls13_misc.h:298
Common interface for key encapsulation mechanisms (KEM)
Definition: crypto.h:1123
Tls13PskKeModeList
Definition: tls13_misc.h:231
bool_t tls13IsMlkemGroupSupported(TlsContext *context, uint16_t namedGroup)
Check whether a given ML-KEM exchange method is supported.
Definition: tls13_misc.c:1110
const uint8_t tls11DowngradeRandom[8]
Definition: tls13_misc.c:53
const uint8_t tls12DowngradeRandom[8]
Definition: tls13_misc.c:59
void * Tls13EndOfEarlyData
EndOfEarlyData message.
Definition: tls13_misc.h:318
#define EcCurve
Definition: ec.h:346
Tls13CertRequestContext
Definition: tls13_misc.h:286
@ TLS_KEY_UPDATE_NOT_REQUESTED
Definition: tls13_misc.h:176
Tls13PskIdentity
Definition: tls13_misc.h:242
Tls13PskIdentityList
Definition: tls13_misc.h:253
uint32_t ticketAgeAdd
Random value used to obscure the age of the ticket.
Definition: tls13_misc.h:339
#define TlsEncryptionEngine
Definition: tls.h:40
error_t tls13CheckDuplicateKeyShare(uint16_t namedGroup, const uint8_t *p, size_t length)
Check whether the specified key share group is a duplicate.
Definition: tls13_misc.c:1394
error_t tls13FormatCertExtensions(uint8_t *p, size_t *written)
Format certificate extensions.
Definition: tls13_misc.c:1440
const uint8_t tls13HelloRetryRequestRandom[32]
Definition: tls13_misc.c:65
const EcCurve * tls13GetTraditionalAlgo(TlsContext *context, uint16_t namedGroup)
Get the traditional algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:1261
Tls13EncryptedExtensions
Definition: tls13_misc.h:329