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
226
227
228
229
230
231
232
|
/***************************************************************************
* 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 */
/** */
/** Datagram Transport Layer Security (DTLS) */
/** */
/**************************************************************************/
/**************************************************************************/
#define NX_SECURE_SOURCE_CODE
#include "nx_secure_dtls.h"
#ifdef NX_SECURE_ENABLE_DTLS
static UCHAR adjusted_sequence_num[8];
static UCHAR adjusted_header[5];
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_secure_dtls_hash_record PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Timothy Stapko, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function hashes an outgoing DTLS record to generate the Message*/
/* Authentication Code (MAC) value that is placed at the end of all */
/* encrypted DTLS messages. */
/* */
/* INPUT */
/* */
/* dtls_session DTLS control block */
/* sequence_num Record sequence number */
/* header Record header */
/* header_length Length of record header */
/* data Record payload */
/* length Length of record payload */
/* record_hash Pointer to output hash buffer */
/* hash_length Length of hash */
/* mac_secret Key used for MAC generation */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* [nx_crypto_operation] Hash functions */
/* */
/* CALLED BY */
/* */
/* _nx_secure_dtls_send_record Send DTLS records */
/* _nx_secure_dtls_verify_mac Verify record MAC checksum */
/* */
/**************************************************************************/
UINT _nx_secure_dtls_hash_record(NX_SECURE_DTLS_SESSION *dtls_session,
ULONG sequence_num[NX_SECURE_TLS_SEQUENCE_NUMBER_SIZE],
UCHAR *header, UINT header_length, UCHAR *data, UINT length,
UCHAR *record_hash, UINT *hash_length, UCHAR *mac_secret)
{
UINT hash_size;
UINT status = NX_SECURE_TLS_SUCCESS;
const NX_CRYPTO_METHOD *authentication_method;
NX_SECURE_TLS_SESSION *tls_session;
NX_PARAMETER_NOT_USED(sequence_num);
NX_PARAMETER_NOT_USED(header_length);
/* We need to generate a Message Authentication Code (MAC) for each record during an "active" TLS session
(following a ChangeCipherSpec message). The hash algorithm is determined by the ciphersuite, and HMAC
is used with that hash algorithm to protect the TLS record contents from tampering.
The MAC is generated as:
HMAC_hash(MAC_write_secret, seq_num + type + version + length + data);
*/
/* Get a reference to the internal TLS session. */
tls_session = &dtls_session -> nx_secure_dtls_tls_session;
if (tls_session -> nx_secure_tls_session_ciphersuite == NX_NULL)
{
/* Likely internal error since at this point ciphersuite negotiation was theoretically completed. */
return(NX_SECURE_TLS_UNKNOWN_CIPHERSUITE);
}
/* Get our authentication method from the ciphersuite. */
authentication_method = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash;
/* Get the hash size and MAC secret for our current session. */
hash_size = tls_session -> nx_secure_tls_session_ciphersuite -> nx_secure_tls_hash_size;
/* In DTLS, use the sequence number + epoch from the header. */
NX_SECURE_MEMCPY(adjusted_sequence_num, &header[3], 8); /* Use case of memcpy is verified. */
/* We need to extract the type, version, and length from the DTLS header. */
adjusted_header[0] = header[0]; /* Type. */
adjusted_header[1] = header[1]; /* Version. */
adjusted_header[2] = header[2];
adjusted_header[3] = header[11]; /* Length. */
adjusted_header[4] = header[12];
if (authentication_method -> nx_crypto_operation != NX_NULL)
{
status = authentication_method -> nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE,
NX_NULL,
(NX_CRYPTO_METHOD*)authentication_method,
mac_secret,
(NX_CRYPTO_KEY_SIZE)(hash_size << 3),
NX_NULL,
0,
NX_NULL,
NX_NULL,
0,
tls_session -> nx_secure_hash_mac_metadata_area,
tls_session -> nx_secure_hash_mac_metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}
status = authentication_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
NX_NULL,
(NX_CRYPTO_METHOD*)authentication_method,
NX_NULL,
0,
adjusted_sequence_num,
8,
NX_NULL,
NX_NULL,
0,
tls_session -> nx_secure_hash_mac_metadata_area,
tls_session -> nx_secure_hash_mac_metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}
status = authentication_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
NX_NULL,
(NX_CRYPTO_METHOD*)authentication_method,
NX_NULL,
0,
adjusted_header,
5,
NX_NULL,
NX_NULL,
0,
tls_session -> nx_secure_hash_mac_metadata_area,
tls_session -> nx_secure_hash_mac_metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}
status = authentication_method -> nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
NX_NULL,
(NX_CRYPTO_METHOD*)authentication_method,
NX_NULL,
0,
data,
length,
NX_NULL,
NX_NULL,
0,
tls_session -> nx_secure_hash_mac_metadata_area,
tls_session -> nx_secure_hash_mac_metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}
status = authentication_method -> nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
NX_NULL,
(NX_CRYPTO_METHOD*)authentication_method,
NX_NULL,
0,
NX_NULL,
0,
NX_NULL,
record_hash,
NX_SECURE_TLS_MAX_HASH_SIZE,
tls_session -> nx_secure_hash_mac_metadata_area,
tls_session -> nx_secure_hash_mac_metadata_size,
NX_NULL,
NX_NULL);
if(status != NX_CRYPTO_SUCCESS)
{
return(status);
}
}
/* Return how many bytes our hash is since the caller doesn't necessarily know. */
*hash_length = hash_size;
return(status);
}
#endif /* NX_SECURE_ENABLE_DTLS */
|