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 MD5 Digest Algorithm (MD5) */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #include "nx_crypto_md5.h"
24 #include "nx_crypto_hmac_md5.h"
25 #include "nx_crypto_hmac.h"
26
27
28 /**************************************************************************/
29 /* */
30 /* FUNCTION RELEASE */
31 /* */
32 /* _nx_crypto_method_hmac_md5_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 MD5 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_md5_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_md5_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_STATE_CHECK
83
84 NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits);
85 NX_CRYPTO_PARAMETER_NOT_USED(handle);
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_MD5_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_md5_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_md5_cleanup(VOID * crypto_metadata)146 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_md5_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_MD5_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_md5_operation PORTABLE C */
170 /* 6.3.0 */
171 /* AUTHOR */
172 /* */
173 /* Timothy Stapko, Microsoft Corporation */
174 /* */
175 /* DESCRIPTION */
176 /* */
177 /* This function handles HMAC MD5 Authentication operation. */
178 /* */
179 /* INPUT */
180 /* */
181 /* op Operation Type */
182 /* Encrypt, Decrypt, Authenticate*/
183 /* handler Pointer to crypto context */
184 /* method Pointer to crypto method */
185 /* key Pointer to key */
186 /* key_size_in_bits Length of key size in bits */
187 /* input Input Stream */
188 /* input_length_in_byte Input Stream Length */
189 /* iv_ptr Initialized Vector */
190 /* output Output Stream */
191 /* output_length_in_byte Output Stream Length */
192 /* crypto_metadata Metadata area */
193 /* crypto_metadata_size Size of the metadata area */
194 /* packet_ptr Pointer to packet */
195 /* nx_crypto_hw_process_callback Callback function pointer */
196 /* */
197 /* OUTPUT */
198 /* */
199 /* status Completion status */
200 /* */
201 /* CALLS */
202 /* */
203 /* _nx_crypto_hmac Calculate the HMAC */
204 /* _nx_crypto_hmac_metadata_set Set HMAC metadata */
205 /* _nx_crypto_hmac_initialize Perform HMAC initialization */
206 /* _nx_crypto_hmac_update Perform HMAC update */
207 /* _nx_crypto_hmac_digest_calculate Calculate HMAC digest */
208 /* */
209 /* CALLED BY */
210 /* */
211 /* Application Code */
212 /* */
213 /* RELEASE HISTORY */
214 /* */
215 /* DATE NAME DESCRIPTION */
216 /* */
217 /* 05-19-2020 Timothy Stapko Initial Version 6.0 */
218 /* 09-30-2020 Timothy Stapko Modified comment(s), */
219 /* resulting in version 6.1 */
220 /* 10-31-2023 Yanwu Cai Modified comment(s), */
221 /* resulting in version 6.3.0 */
222 /* */
223 /**************************************************************************/
_nx_crypto_method_hmac_md5_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))224 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_md5_operation(UINT op, /* Encrypt, Decrypt, Authenticate */
225 VOID *handle, /* Crypto handler */
226 struct NX_CRYPTO_METHOD_STRUCT *method,
227 UCHAR *key,
228 NX_CRYPTO_KEY_SIZE key_size_in_bits,
229 UCHAR *input,
230 ULONG input_length_in_byte,
231 UCHAR *iv_ptr,
232 UCHAR *output,
233 ULONG output_length_in_byte,
234 VOID *crypto_metadata,
235 ULONG crypto_metadata_size,
236 VOID *packet_ptr,
237 VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
238 {
239 NX_CRYPTO_MD5_HMAC *ctx;
240 NX_CRYPTO_HMAC *hmac_metadata;
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_MD5_HMAC))
256 {
257 return(NX_CRYPTO_PTR_ERROR);
258 }
259
260 ctx = (NX_CRYPTO_MD5_HMAC *)crypto_metadata;
261 hmac_metadata = &ctx->nx_md5_hmac_metadata;
262
263 _nx_crypto_hmac_metadata_set(hmac_metadata,
264 &(ctx -> nx_md5_hmac_context),
265 method -> nx_crypto_algorithm,
266 NX_CRYPTO_MD5_BLOCK_SIZE_IN_BYTES,
267 NX_CRYPTO_HMAC_MD5_ICV_FULL_LEN_IN_BITS >> 3,
268 (UINT (*)(VOID *, UINT))_nx_crypto_md5_initialize,
269 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_md5_update,
270 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_md5_digest_calculate);
271
272 switch (op)
273 {
274 case NX_CRYPTO_HASH_INITIALIZE:
275 if(key == NX_CRYPTO_NULL)
276 {
277 return(NX_CRYPTO_PTR_ERROR);
278 }
279
280 _nx_crypto_hmac_initialize(hmac_metadata, key, key_size_in_bits >> 3);
281 break;
282
283 case NX_CRYPTO_HASH_UPDATE:
284 _nx_crypto_hmac_update(hmac_metadata, input, input_length_in_byte);
285 break;
286
287 case NX_CRYPTO_HASH_CALCULATE:
288 if(output_length_in_byte == 0)
289 {
290 return(NX_CRYPTO_INVALID_BUFFER_SIZE);
291 }
292
293 _nx_crypto_hmac_digest_calculate(hmac_metadata, output,
294 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
295 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
296 break;
297
298 default:
299 if(key == NX_CRYPTO_NULL)
300 {
301 return(NX_CRYPTO_PTR_ERROR);
302 }
303
304 if(output_length_in_byte == 0)
305 {
306 return(NX_CRYPTO_INVALID_BUFFER_SIZE);
307 }
308
309 _nx_crypto_hmac(hmac_metadata, input, input_length_in_byte, key, (key_size_in_bits >> 3), output,
310 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
311 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
312 break;
313 }
314
315 return NX_CRYPTO_SUCCESS;
316 }
317
318