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 /** HMAC SHA1 Digest Algorithm (SHA1) */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #include "nx_crypto_sha2.h"
23 #include "nx_crypto_hmac_sha2.h"
24 #include "nx_crypto_hmac.h"
25
26
27 /**************************************************************************/
28 /* */
29 /* FUNCTION RELEASE */
30 /* */
31 /* _nx_crypto_method_hmac_sha256_init PORTABLE C */
32 /* 6.3.0 */
33 /* AUTHOR */
34 /* */
35 /* Timothy Stapko, Microsoft Corporation */
36 /* */
37 /* DESCRIPTION */
38 /* */
39 /* This function is the common crypto method init callback for */
40 /* Microsoft supported HMAC SHA256 cryptographic algorithm. */
41 /* */
42 /* INPUT */
43 /* */
44 /* method Pointer to crypto method */
45 /* key Pointer to key */
46 /* key_size_in_bits Length of key size in bits */
47 /* handler Returned crypto handler */
48 /* crypto_metadata Metadata area */
49 /* crypto_metadata_size Size of the metadata area */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* status Completion status */
54 /* */
55 /* CALLS */
56 /* */
57 /* None */
58 /* */
59 /* CALLED BY */
60 /* */
61 /* Application Code */
62 /* */
63 /* RELEASE HISTORY */
64 /* */
65 /* DATE NAME DESCRIPTION */
66 /* */
67 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
68 /* 09-30-2020 Timothy Stapko Modified comment(s), */
69 /* resulting in version 6.1 */
70 /* 10-31-2023 Yanwu Cai Modified comment(s), */
71 /* resulting in version 6.3.0 */
72 /* */
73 /**************************************************************************/
_nx_crypto_method_hmac_sha256_init(struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,VOID ** handle,VOID * crypto_metadata,ULONG crypto_metadata_size)74 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_sha256_init(struct NX_CRYPTO_METHOD_STRUCT *method,
75 UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
76 VOID **handle,
77 VOID *crypto_metadata,
78 ULONG crypto_metadata_size)
79 {
80
81 NX_CRYPTO_PARAMETER_NOT_USED(key);
82 NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits);
83 NX_CRYPTO_PARAMETER_NOT_USED(handle);
84
85 NX_CRYPTO_STATE_CHECK
86
87 if ((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL))
88 {
89 return(NX_CRYPTO_PTR_ERROR);
90 }
91
92 /* Verify the metadata address is 4-byte aligned. */
93 if((((ULONG)crypto_metadata) & 0x3) != 0)
94 {
95 return(NX_CRYPTO_PTR_ERROR);
96 }
97
98 if(crypto_metadata_size < sizeof(NX_CRYPTO_SHA256_HMAC))
99 {
100 return(NX_CRYPTO_PTR_ERROR);
101 }
102
103 return(NX_CRYPTO_SUCCESS);
104 }
105
106
107 /**************************************************************************/
108 /* */
109 /* FUNCTION RELEASE */
110 /* */
111 /* _nx_crypto_method_hmac_sha256_cleanup PORTABLE C */
112 /* 6.1 */
113 /* AUTHOR */
114 /* */
115 /* Timothy Stapko, Microsoft Corporation */
116 /* */
117 /* DESCRIPTION */
118 /* */
119 /* This function cleans up the crypto metadata. */
120 /* */
121 /* INPUT */
122 /* */
123 /* crypto_metadata Crypto metadata */
124 /* */
125 /* OUTPUT */
126 /* */
127 /* status Completion status */
128 /* */
129 /* CALLS */
130 /* */
131 /* NX_CRYPTO_MEMSET Set the memory */
132 /* */
133 /* CALLED BY */
134 /* */
135 /* Application Code */
136 /* */
137 /* RELEASE HISTORY */
138 /* */
139 /* DATE NAME DESCRIPTION */
140 /* */
141 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
142 /* 09-30-2020 Timothy Stapko Modified comment(s), */
143 /* resulting in version 6.1 */
144 /* */
145 /**************************************************************************/
_nx_crypto_method_hmac_sha256_cleanup(VOID * crypto_metadata)146 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_sha256_cleanup(VOID *crypto_metadata)
147 {
148
149 NX_CRYPTO_STATE_CHECK
150
151 #ifdef NX_SECURE_KEY_CLEAR
152 if (!crypto_metadata)
153 return (NX_CRYPTO_SUCCESS);
154
155 /* Clean up the crypto metadata. */
156 NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_SHA256_HMAC));
157 #else
158 NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata);
159 #endif/* NX_SECURE_KEY_CLEAR */
160
161 return(NX_CRYPTO_SUCCESS);
162 }
163
164
165 /**************************************************************************/
166 /* */
167 /* FUNCTION RELEASE */
168 /* */
169 /* _nx_crypto_method_hmac_sha256_operation PORTABLE C */
170 /* 6.3.0 */
171 /* AUTHOR */
172 /* */
173 /* Timothy Stapko, Microsoft Corporation */
174 /* */
175 /* DESCRIPTION */
176 /* */
177 /* This function handles HMAC SHA256 Authentication operation. */
178 /* */
179 /* INPUT */
180 /* */
181 /* op Operation Type */
182 /* Encrypt, Decrypt, Authenticate*/
183 /* handler Pointer to crypto context */
184 /* key Pointer to key */
185 /* key_size_in_bits Length of key size in bits */
186 /* input Input Stream */
187 /* input_length_in_byte Input Stream Length */
188 /* iv_ptr Initialized Vector */
189 /* output Output Stream */
190 /* output_length_in_byte Output Stream Length */
191 /* crypto_metadata Metadata area */
192 /* crypto_metadata_size Size of the metadata area */
193 /* packet_ptr Pointer to packet */
194 /* nx_crypto_hw_process_callback Callback function pointer */
195 /* */
196 /* OUTPUT */
197 /* */
198 /* status Completion status */
199 /* */
200 /* CALLS */
201 /* */
202 /* _nx_crypto_hmac Calculate the HMAC */
203 /* _nx_crypto_hmac_metadata_set Set HMAC metadata */
204 /* _nx_crypto_hmac_initialize Perform HMAC initialization */
205 /* _nx_crypto_hmac_update Perform HMAC update */
206 /* _nx_crypto_hmac_digest_calculate Calculate HMAC digest */
207 /* */
208 /* CALLED BY */
209 /* */
210 /* Application Code */
211 /* */
212 /* RELEASE HISTORY */
213 /* */
214 /* DATE NAME DESCRIPTION */
215 /* */
216 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
217 /* 09-30-2020 Timothy Stapko Modified comment(s), */
218 /* resulting in version 6.1 */
219 /* 10-31-2023 Yanwu Cai Modified comment(s), */
220 /* resulting in version 6.3.0 */
221 /* */
222 /**************************************************************************/
_nx_crypto_method_hmac_sha256_operation(UINT op,VOID * handle,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))223 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_sha256_operation(UINT op, /* Encrypt, Decrypt, Authenticate */
224 VOID *handle, /* Crypto handler */
225 struct NX_CRYPTO_METHOD_STRUCT *method,
226 UCHAR *key,
227 NX_CRYPTO_KEY_SIZE key_size_in_bits,
228 UCHAR *input,
229 ULONG input_length_in_byte,
230 UCHAR *iv_ptr,
231 UCHAR *output,
232 ULONG output_length_in_byte,
233 VOID *crypto_metadata,
234 ULONG crypto_metadata_size,
235 VOID *packet_ptr,
236 VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
237 {
238 NX_CRYPTO_SHA256_HMAC *ctx;
239 NX_CRYPTO_HMAC *hmac_metadata;
240 UINT icv_length;
241
242 NX_CRYPTO_PARAMETER_NOT_USED(handle);
243 NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr);
244 NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr);
245 NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback);
246
247 NX_CRYPTO_STATE_CHECK
248
249 /* Verify the metadata address is 4-byte aligned. */
250 if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0))
251 {
252 return(NX_CRYPTO_PTR_ERROR);
253 }
254
255 if(crypto_metadata_size < sizeof(NX_CRYPTO_SHA256_HMAC))
256 {
257 return(NX_CRYPTO_PTR_ERROR);
258 }
259
260 if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_224)
261 {
262 icv_length = NX_CRYPTO_HMAC_SHA224_ICV_FULL_LEN_IN_BITS;
263 }
264 else if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256)
265 {
266 icv_length = NX_CRYPTO_HMAC_SHA256_ICV_FULL_LEN_IN_BITS;
267 }
268 else
269 {
270
271 /* Incorrect method. */
272 return(NX_CRYPTO_NOT_SUCCESSFUL);
273 }
274
275 ctx = (NX_CRYPTO_SHA256_HMAC *)crypto_metadata;
276 hmac_metadata = &ctx->nx_sha256_hmac_metadata;
277
278 _nx_crypto_hmac_metadata_set(hmac_metadata,
279 &(ctx -> nx_sha256_hmac_context),
280 method -> nx_crypto_algorithm,
281 NX_CRYPTO_SHA2_BLOCK_SIZE_IN_BYTES,
282 icv_length >> 3,
283 (UINT (*)(VOID *, UINT))_nx_crypto_sha256_initialize,
284 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_sha256_update,
285 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_sha256_digest_calculate);
286
287
288 switch (op)
289 {
290 case NX_CRYPTO_HASH_INITIALIZE:
291 if(key == NX_CRYPTO_NULL)
292 {
293 return(NX_CRYPTO_PTR_ERROR);
294 }
295
296 _nx_crypto_hmac_initialize(hmac_metadata, key, key_size_in_bits >> 3);
297 break;
298
299 case NX_CRYPTO_HASH_UPDATE:
300 _nx_crypto_hmac_update(hmac_metadata, input, input_length_in_byte);
301 break;
302
303 case NX_CRYPTO_HASH_CALCULATE:
304 if(output_length_in_byte == 0)
305 {
306 return(NX_CRYPTO_INVALID_BUFFER_SIZE);
307 }
308 _nx_crypto_hmac_digest_calculate(hmac_metadata, output,
309 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
310 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
311 break;
312
313 default:
314 if(key == NX_CRYPTO_NULL)
315 {
316 return(NX_CRYPTO_PTR_ERROR);
317 }
318
319 if(output_length_in_byte == 0)
320 {
321 return(NX_CRYPTO_INVALID_BUFFER_SIZE);
322 }
323 _nx_crypto_hmac(hmac_metadata, input, input_length_in_byte, key, (key_size_in_bits >> 3), output,
324 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
325 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
326 break;
327 }
328
329 return NX_CRYPTO_SUCCESS;
330 }
331
332