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 SHA1 Digest Algorithm (SHA1) */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #include "nx_crypto_sha1.h"
24 #include "nx_crypto_hmac_sha1.h"
25 #include "nx_crypto_hmac.h"
26
27
28 /**************************************************************************/
29 /* */
30 /* FUNCTION RELEASE */
31 /* */
32 /* _nx_crypto_method_hmac_sha1_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 SHA1 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_sha1_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_sha1_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_SHA1_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_sha1_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_sha1_cleanup(VOID * crypto_metadata)146 NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_sha1_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_SHA1_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_sha1_operation PORTABLE C */
170 /* 6.3.0 */
171 /* AUTHOR */
172 /* */
173 /* Timothy Stapko, Microsoft Corporation */
174 /* */
175 /* DESCRIPTION */
176 /* */
177 /* This function handles HMAC SHA1 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_sha1_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_sha1_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_SHA1_HMAC *ctx;
239 NX_CRYPTO_HMAC *hmac_metadata;
240
241 NX_CRYPTO_PARAMETER_NOT_USED(handle);
242 NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr);
243 NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr);
244 NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback);
245
246 NX_CRYPTO_STATE_CHECK
247
248 /* Verify the metadata address is 4-byte aligned. */
249 if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0))
250 {
251 return(NX_CRYPTO_PTR_ERROR);
252 }
253
254 if(crypto_metadata_size < sizeof(NX_CRYPTO_SHA1_HMAC))
255 {
256 return(NX_CRYPTO_PTR_ERROR);
257 }
258
259 ctx = (NX_CRYPTO_SHA1_HMAC *)crypto_metadata;
260 hmac_metadata = &ctx->nx_sha1_hmac_metadata;
261
262 _nx_crypto_hmac_metadata_set(hmac_metadata,
263 &(ctx -> nx_sha1_hmac_context),
264 method -> nx_crypto_algorithm,
265 NX_CRYPTO_SHA1_BLOCK_SIZE_IN_BYTES,
266 NX_CRYPTO_HMAC_SHA1_ICV_FULL_LEN_IN_BITS >> 3,
267 (UINT (*)(VOID *, UINT))_nx_crypto_sha1_initialize,
268 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_sha1_update,
269 (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_sha1_digest_calculate);
270
271 switch (op)
272 {
273 case NX_CRYPTO_HASH_INITIALIZE:
274 if(key == NX_CRYPTO_NULL)
275 {
276 return(NX_CRYPTO_PTR_ERROR);
277 }
278
279 _nx_crypto_hmac_initialize(hmac_metadata, key, key_size_in_bits >> 3);
280 break;
281
282 case NX_CRYPTO_HASH_UPDATE:
283 _nx_crypto_hmac_update(hmac_metadata, input, input_length_in_byte);
284 break;
285
286 case NX_CRYPTO_HASH_CALCULATE:
287 if(output_length_in_byte == 0)
288 {
289 return(NX_CRYPTO_INVALID_BUFFER_SIZE);
290 }
291
292 _nx_crypto_hmac_digest_calculate(hmac_metadata, output,
293 (output_length_in_byte > (ULONG)((method -> nx_crypto_ICV_size_in_bits) >> 3) ?
294 ((method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
295 break;
296
297 default:
298 if(key == NX_CRYPTO_NULL)
299 {
300 return(NX_CRYPTO_PTR_ERROR);
301 }
302
303 if(output_length_in_byte == 0)
304 {
305 return(NX_CRYPTO_INVALID_BUFFER_SIZE);
306 }
307
308 _nx_crypto_hmac(hmac_metadata, input, input_length_in_byte, key, (key_size_in_bits >> 3), 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
314 return NX_CRYPTO_SUCCESS;
315 }
316
317