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 /**   Crypto Initialization                                               */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 #define _NX_CRYPTO_INITIALIZE_
23 
24 #include "nx_crypto.h"
25 
26 
27 #ifdef NX_CRYPTO_SELF_TEST
28 
29 /* Include necessary system files.  */
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_crypto_self_test_memcpy                         PORTABLE C      */
37 /*                                                           6.1.7        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Timothy Stapko, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function performs memory copy function for the FIPS 140-2      */
45 /*    compliance build.                                                   */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    dest                                  Pointer to the destination    */
50 /*                                            memory                      */
51 /*    value                                 value (in byte) to set to the */
52 /*                                            memory location             */
53 /*    size                                  Number of bytes to copy       */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    void *                                Pointer to the destination    */
58 /*                                            memory                      */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
72 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
75 /*                                            renamed function,           */
76 /*                                            resulting in version 6.1.7  */
77 /*                                                                        */
78 /**************************************************************************/
_nx_crypto_self_test_memcpy(void * dest,const void * src,size_t size)79 NX_CRYPTO_KEEP VOID *_nx_crypto_self_test_memcpy(void *dest, const void *src, size_t size)
80 {
81     char *from, *to;
82     unsigned int i;
83 
84     from = (char*)src;
85     to = (char*)dest;
86 
87     for(i = 0; i < size; i++)
88     {
89         to[i] = from[i];
90     }
91 
92     return dest;
93 }
94 
95 /**************************************************************************/
96 /*                                                                        */
97 /*  FUNCTION                                               RELEASE        */
98 /*                                                                        */
99 /*    _nx_crypto_self_test_memset                         PORTABLE C      */
100 /*                                                           6.1.7        */
101 /*  AUTHOR                                                                */
102 /*                                                                        */
103 /*    Timothy Stapko, Microsoft Corporation                               */
104 /*                                                                        */
105 /*  DESCRIPTION                                                           */
106 /*                                                                        */
107 /*    This function performs memory set function for the FIPS 140-2       */
108 /*    compliance build.                                                   */
109 /*                                                                        */
110 /*  INPUT                                                                 */
111 /*                                                                        */
112 /*    dest                                  Pointer to the destination    */
113 /*                                            memory                      */
114 /*    value                                 value (in byte) to set to the */
115 /*                                            memory location             */
116 /*    size                                  Number of bytes to set        */
117 /*                                                                        */
118 /*  OUTPUT                                                                */
119 /*                                                                        */
120 /*    void *                                Pointer to the destination    */
121 /*                                            memory                      */
122 /*                                                                        */
123 /*  CALLS                                                                 */
124 /*                                                                        */
125 /*    None                                                                */
126 /*                                                                        */
127 /*  CALLED BY                                                             */
128 /*                                                                        */
129 /*                                                                        */
130 /*  RELEASE HISTORY                                                       */
131 /*                                                                        */
132 /*    DATE              NAME                      DESCRIPTION             */
133 /*                                                                        */
134 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
135 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
136 /*                                            resulting in version 6.1    */
137 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
138 /*                                            renamed function,           */
139 /*                                            resulting in version 6.1.7  */
140 /*                                                                        */
141 /**************************************************************************/
_nx_crypto_self_test_memset(void * dest,int value,size_t size)142 NX_CRYPTO_KEEP VOID *_nx_crypto_self_test_memset(void *dest, int value, size_t size)
143 {
144     char  *to;
145     unsigned int i;
146     char v;
147 
148     to = (char*)dest;
149     v = (char)(value & 0xFF);
150     for(i = 0; i < size; i++)
151     {
152         to[i] = v;
153     }
154 
155     return dest;
156 }
157 
158 /**************************************************************************/
159 /*                                                                        */
160 /*  FUNCTION                                               RELEASE        */
161 /*                                                                        */
162 /*    _nx_crypto_self_test_memcmp                         PORTABLE C      */
163 /*                                                           6.1.7        */
164 /*  AUTHOR                                                                */
165 /*                                                                        */
166 /*    Timothy Stapko, Microsoft Corporation                               */
167 /*                                                                        */
168 /*  DESCRIPTION                                                           */
169 /*                                                                        */
170 /*    This function performs memory comparison function for the           */
171 /*    FIPS 140-2 compliance build.                                        */
172 /*                                                                        */
173 /*  INPUT                                                                 */
174 /*                                                                        */
175 /*    str1                                  Pointer to the first string   */
176 /*                                            for the comparison.         */
177 /*    str2                                  Pointer to the second string  */
178 /*                                            for the comparison.         */
179 /*    size                                  Number of bytes to compare.   */
180 /*                                                                        */
181 /*  OUTPUT                                                                */
182 /*                                                                        */
183 /*    Result                                0:  str1 and str2 are the     */
184 /*                                              same.                     */
185 /*                                          0:  str1 and str2 are         */
186 /*                                              different.                */
187 /*                                                                        */
188 /*  CALLS                                                                 */
189 /*                                                                        */
190 /*    None                                                                */
191 /*                                                                        */
192 /*  CALLED BY                                                             */
193 /*                                                                        */
194 /*                                                                        */
195 /*  RELEASE HISTORY                                                       */
196 /*                                                                        */
197 /*    DATE              NAME                      DESCRIPTION             */
198 /*                                                                        */
199 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
200 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
201 /*                                            resulting in version 6.1    */
202 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
203 /*                                            renamed function,           */
204 /*                                            resulting in version 6.1.7  */
205 /*                                                                        */
206 /**************************************************************************/
_nx_crypto_self_test_memcmp(const void * str1,const void * str2,size_t size)207 NX_CRYPTO_KEEP int _nx_crypto_self_test_memcmp(const void *str1, const void *str2, size_t size)
208 {
209     char *string1;
210     char *string2;
211     unsigned int i;
212 
213     string1 = (char*)str1;
214     string2 = (char*)str2;
215     for(i = 0; i < size; i++)
216     {
217         if(*string1++ != *string2++)
218             return(1);
219     }
220 
221     return(0);
222 }
223 
224 /**************************************************************************/
225 /*                                                                        */
226 /*  FUNCTION                                               RELEASE        */
227 /*                                                                        */
228 /*    _nx_crypto_self_test_memmove                        PORTABLE C      */
229 /*                                                           6.1.7        */
230 /*  AUTHOR                                                                */
231 /*                                                                        */
232 /*    Timothy Stapko, Microsoft Corporation                               */
233 /*                                                                        */
234 /*  DESCRIPTION                                                           */
235 /*                                                                        */
236 /*    This function performs memory move function for the FIPS 140-2      */
237 /*    compliance build.                                                   */
238 /*                                                                        */
239 /*  INPUT                                                                 */
240 /*                                                                        */
241 /*    dest                                  Pointer to the destination    */
242 /*                                            memory                      */
243 /*    src                                   Pointer to the source memory  */
244 /*    size                                  Number of bytes to move       */
245 /*                                                                        */
246 /*  OUTPUT                                                                */
247 /*                                                                        */
248 /*    void *                                Pointer to the destination    */
249 /*                                            memory                      */
250 /*                                                                        */
251 /*  CALLS                                                                 */
252 /*                                                                        */
253 /*    None                                                                */
254 /*                                                                        */
255 /*  CALLED BY                                                             */
256 /*                                                                        */
257 /*                                                                        */
258 /*  RELEASE HISTORY                                                       */
259 /*                                                                        */
260 /*    DATE              NAME                      DESCRIPTION             */
261 /*                                                                        */
262 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
263 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
264 /*                                            resulting in version 6.1    */
265 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
266 /*                                            renamed function,           */
267 /*                                            resulting in version 6.1.7  */
268 /*                                                                        */
269 /**************************************************************************/
_nx_crypto_self_test_memmove(void * dest,const void * src,size_t size)270 NX_CRYPTO_KEEP void* _nx_crypto_self_test_memmove(void *dest, const void *src, size_t size)
271 {
272     char *from, *to;
273     unsigned int i;
274 
275     from = (char*)src;
276     to = (char*)dest;
277 
278     if((ULONG)dest < (ULONG)src)
279     {
280         for(i = 0; i < size; i++)
281         {
282             to[i] = from[i];
283         }
284     }
285     else if((ULONG)dest > (ULONG)src)
286     {
287 
288         for(i = size; i != 0; i--)
289             to[i - 1] = from[i - 1];
290     }
291     return(dest);
292 }
293 
294 
295 /**************************************************************************/
296 /*                                                                        */
297 /*  FUNCTION                                               RELEASE        */
298 /*                                                                        */
299 /*    _nx_crypto_module_state_get                         PORTABLE C      */
300 /*                                                           6.1          */
301 /*  AUTHOR                                                                */
302 /*                                                                        */
303 /*    Timothy Stapko, Microsoft Corporation                               */
304 /*                                                                        */
305 /*  DESCRIPTION                                                           */
306 /*                                                                        */
307 /*    This function retrieves the current crypto library state.           */
308 /*                                                                        */
309 /*  INPUT                                                                 */
310 /*                                                                        */
311 /*    None                                                                */
312 /*                                                                        */
313 /*  OUTPUT                                                                */
314 /*                                                                        */
315 /*    status                                The bitmap of the current     */
316 /*                                            status.  Valid bits are:    */
317 /*                        NX_CRYPTO_LIBRARY_STATE_UNINITIALIZED           */
318 /*                        NX_CRYPTO_LIBRARY_STATE_POST_IN_PROGRESS        */
319 /*                        NX_CRYPTO_LIBRARY_STATE_POST_FAILED             */
320 /*                        NX_CRYPTO_LIBRARY_STATE_OPERATIONAL             */
321 /*                                                                        */
322 /*  CALLS                                                                 */
323 /*                                                                        */
324 /*    None                                                                */
325 /*                                                                        */
326 /*  CALLED BY                                                             */
327 /*                                                                        */
328 /*                                                                        */
329 /*  RELEASE HISTORY                                                       */
330 /*                                                                        */
331 /*    DATE              NAME                      DESCRIPTION             */
332 /*                                                                        */
333 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
334 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
335 /*                                            resulting in version 6.1    */
336 /*                                                                        */
337 /**************************************************************************/
_nx_crypto_module_state_get(VOID)338 NX_CRYPTO_KEEP UINT _nx_crypto_module_state_get(VOID)
339 {
340     return(_nx_crypto_library_state);
341 
342 }
343 
344 #endif
345 
346 
347 /**************************************************************************/
348 /*                                                                        */
349 /*  FUNCTION                                               RELEASE        */
350 /*                                                                        */
351 /*    _nx_crypto_initialize                               PORTABLE C      */
352 /*                                                           6.1          */
353 /*  AUTHOR                                                                */
354 /*                                                                        */
355 /*    Timothy Stapko, Microsoft Corporation                               */
356 /*                                                                        */
357 /*  DESCRIPTION                                                           */
358 /*                                                                        */
359 /*    This function initializes the NetX Crypto module.                   */
360 /*                                                                        */
361 /*  INPUT                                                                 */
362 /*                                                                        */
363 /*    None                                                                */
364 /*                                                                        */
365 /*  OUTPUT                                                                */
366 /*                                                                        */
367 /*    status                                                              */
368 /*                                                                        */
369 /*  CALLS                                                                 */
370 /*                                                                        */
371 /*    None                                                                */
372 /*                                                                        */
373 /*  CALLED BY                                                             */
374 /*                                                                        */
375 /*                                                                        */
376 /*  RELEASE HISTORY                                                       */
377 /*                                                                        */
378 /*    DATE              NAME                      DESCRIPTION             */
379 /*                                                                        */
380 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
381 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
382 /*                                            resulting in version 6.1    */
383 /*                                                                        */
384 /**************************************************************************/
_nx_crypto_initialize(VOID)385 NX_CRYPTO_KEEP UINT _nx_crypto_initialize(VOID)
386 {
387 
388 
389     return(NX_CRYPTO_SUCCESS);
390 
391 }
392