1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** NetX Crypto Component */
17 /** */
18 /** HMAC SHA5 Digest Algorithm (SHA5) */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #include "nx_crypto_sha5.h"
24 #include "nx_crypto_hmac_sha5.h"
25 #include "nx_crypto_hmac.h"
26
27
28 /**************************************************************************/
29 /* */
30 /* FUNCTION RELEASE */
31 /* */
32 /* _nx_crypto_method_hmac_sha512_init PORTABLE C */
33 /* 6.3.0 */
34 /* AUTHOR */
35 /* */
36 /* Timothy Stapko, Microsoft Corporation */
37 /* */
38 /* DESCRIPTION */
39 /* */
40 /* This function is the common crypto method init callback for */
41 /* Microsoft supported HMAC SHA512 cryptographic algorithm. */
42 /* */
43 /* INPUT */
44 /* */
45 /* method Pointer to crypto method */
46 /* key Pointer to key */
47 /* key_size_in_bits Length of key size in bits */
48 /* handler Returned crypto handler */
49 /* crypto_metadata Metadata area */
50 /* crypto_metadata_size Size of the metadata area */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* status Completion status */
55 /* */
56 /* CALLS */
57 /* */
58 /* None */
59 /* */
60 /* CALLED BY */
61 /* */
62 /* Application Code */
63 /* */
64 /* RELEASE HISTORY */
65 /* */
66 /* DATE NAME DESCRIPTION */
67 /* */
68 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
69 /* 09-30-2020 Timothy Stapko Modified comment(s), */
70 /* resulting in version 6.1 */
71 /* 10-31-2023 Yanwu Cai Modified comment(s), */
72 /* resulting in version 6.3.0 */
73 /* */
74 /**************************************************************************/
_nx_crypto_method_hmac_sha512_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)75 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_sha512_init(struct NX_CRYPTO_METHOD_STRUCT *method,
76 UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
77 VOID **handle,
78 VOID *crypto_metadata,
79 ULONG crypto_metadata_size)
80 {
81
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_SHA512_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_sha512_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_sha512_cleanup(VOID * crypto_metadata)146 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_sha512_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_SHA512_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_sha512_operation PORTABLE C */
170 /* 6.3.0 */
171 /* AUTHOR */
172 /* */
173 /* Timothy Stapko, Microsoft Corporation */
174 /* */
175 /* DESCRIPTION */
176 /* */
177 /* This function handles HMAC SHA512 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_sha512_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_sha512_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 UINT status = NX_CRYPTO_NOT_SUCCESSFUL;
239 NX_CRYPTO_SHA512_HMAC *ctx;
240 NX_CRYPTO_HMAC *hmac_metadata;
241 UINT icv_full_length;
242
243 NX_CRYPTO_PARAMETER_NOT_USED(handle);
244 NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr);
245 NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte);
246 NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr);
247 NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback);
248
249 NX_CRYPTO_STATE_CHECK
250
251 /* Verify the metadata address is 4-byte aligned. */
252 if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0))
253 {
254 return(NX_CRYPTO_PTR_ERROR);
255 }
256
257 if(crypto_metadata_size < sizeof(NX_CRYPTO_SHA512_HMAC))
258 {
259 return(NX_CRYPTO_PTR_ERROR);
260 }
261
262 if (op != NX_CRYPTO_AUTHENTICATE && op != NX_CRYPTO_VERIFY && op != NX_CRYPTO_HASH_INITIALIZE &&
263 op != NX_CRYPTO_HASH_UPDATE && op != NX_CRYPTO_HASH_CALCULATE)
264 {
265 /* Incorrect Operation. */
266 return status;
267 }
268
269 if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512)
270 {
271 icv_full_length = NX_CRYPTO_HMAC_SHA512_ICV_FULL_LEN_IN_BITS;
272 }
273 else if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_384)
274 {
275 icv_full_length = NX_CRYPTO_HMAC_SHA384_ICV_FULL_LEN_IN_BITS;
276 }
277 else if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512_224)
278 {
279 icv_full_length = NX_CRYPTO_HMAC_SHA512_224_ICV_FULL_LEN_IN_BITS;
280 }
281 else if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512_256)
282 {
283 icv_full_length = NX_CRYPTO_HMAC_SHA512_256_ICV_FULL_LEN_IN_BITS;
284 }
285 else
286 {
287 return(NX_CRYPTO_NOT_SUCCESSFUL);
288 }
289
290 ctx = (NX_CRYPTO_SHA512_HMAC *)crypto_metadata;
291 hmac_metadata = &ctx->nx_sha512_hmac_metadata;
292
293 _nx_crypto_hmac_metadata_set(hmac_metadata,
294 &(ctx -> nx_sha512_hmac_context),
295 method -> nx_crypto_algorithm,
296 NX_CRYPTO_SHA512_BLOCK_SIZE_IN_BYTES,
297 icv_full_length >> 3,
298 (UINT (*)(VOID *, UINT))_nx_crypto_sha512_initialize,
299 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_sha512_update,
300 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_sha512_digest_calculate);
301
302
303 switch (op)
304 {
305 case NX_CRYPTO_HASH_INITIALIZE:
306 if(key == NX_CRYPTO_NULL)
307 {
308 return(NX_CRYPTO_PTR_ERROR);
309 }
310
311 _nx_crypto_hmac_initialize(hmac_metadata, key, key_size_in_bits >> 3);
312 break;
313
314 case NX_CRYPTO_HASH_UPDATE:
315 _nx_crypto_hmac_update(hmac_metadata, input, input_length_in_byte);
316 break;
317
318 case NX_CRYPTO_HASH_CALCULATE:
319 _nx_crypto_hmac_digest_calculate(hmac_metadata, output,
320 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
321 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
322 break;
323
324 default:
325 if(key == NX_CRYPTO_NULL)
326 {
327 return(NX_CRYPTO_PTR_ERROR);
328 }
329
330 _nx_crypto_hmac(hmac_metadata, input, input_length_in_byte, key, (key_size_in_bits >> 3), output,
331 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
332 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
333 break;
334 }
335
336 return NX_CRYPTO_SUCCESS;
337 }
338
339