Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
...
#include "nx_crypto_md5.h"
#define F
#define G
#define H
#define I
#define LEFT_SHIFT_CIRCULAR
_nx_crypto_md5_padding
...
...
_nx_crypto_md5_initialize(NX_CRYPTO_MD5 *, UINT)
...
...
_nx_crypto_md5_update(NX_CRYPTO_MD5 *, UCHAR *, UINT)
...
...
_nx_crypto_md5_digest_calculate(NX_CRYPTO_MD5 *, UCHAR *, UINT)
...
...
_nx_crypto_md5_process_buffer(NX_CRYPTO_MD5 *, UCHAR *)
...
...
_nx_crypto_method_md5_init(struct NX_CRYPTO_METHOD_STRUCT *, UCHAR *, NX_CRYPTO_KEY_SIZE, void **, void *, ULONG)
...
...
_nx_crypto_method_md5_cleanup(void *)
...
...
_nx_crypto_method_md5_operation(UINT, void *, struct NX_CRYPTO_METHOD_STRUCT *, UCHAR *, NX_CRYPTO_KEY_SIZE, UCHAR *, ULONG, UCHAR *, UCHAR *, ULONG, void *, ULONG, void *, void (*)(void *, UINT))
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocrypto_libraries/src/nx_crypto_md5.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ /**************************************************************************/ /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. License to copy and use this software is granted provided that it is identified as the ""RSA Data Security, Inc. MD5 Message-Digest Algorithm"" in all material mentioning or referencing this software or this function. License is also granted to make and use derivative works provided that such works are identified as ""derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm"" in all material mentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided ""as is"" without express or implied warranty of any kind. These notices must be retained in any copies of any part of this documentation and/or software. *//* ... */ .../**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Crypto Component */ /** */ /** MD5 Digest Algorithm (MD5) */ /** */... /**************************************************************************/ /**************************************************************************/ #include "nx_crypto_md5.h" /* Define macros for the MD5 transform function. */ /* Define the MD5 basic F, G, H and I functions. */ #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~z))) /* Define the MD5 left shift circular function. */ #define LEFT_SHIFT_CIRCULAR(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) /* Define the MD5 complex FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. */ #define FF(a, b, c, d, x, s, ac) { \ (a) += F((b), (c), (d)) + (x) + (ULONG)(ac); \ (a) = LEFT_SHIFT_CIRCULAR((a), (s)); \ (a) += (b); \ ...}... #define GG(a, b, c, d, x, s, ac) { \ (a) += G((b), (c), (d)) + (x) + (ULONG)(ac); \ (a) = LEFT_SHIFT_CIRCULAR((a), (s)); \ (a) += (b); \ ...}... #define HH(a, b, c, d, x, s, ac) { \ (a) += H((b), (c), (d)) + (x) + (ULONG)(ac); \ (a) = LEFT_SHIFT_CIRCULAR((a), (s)); \ (a) += (b); \ ...}... #define II(a, b, c, d, x, s, ac) { \ (a) += I((b), (c), (d)) + (x) + (ULONG)(ac); \ (a) = LEFT_SHIFT_CIRCULAR((a), (s)); \ (a) += (b); \ ...}... 9 defines /* Define the padding array. This is used to pad the message such that its length is 64 bits shy of being a multiple of 512 bits long. *//* ... */ const UCHAR _nx_crypto_md5_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_md5_initialize PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function initializes the MD5 context. It must be called prior */ /* to creating the MD5 digest. */ /* */ /* INPUT */ /* */ /* context MD5 context pointer */ /* algorithm Algorithm type */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_md5_operation Handle MD5 operation */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* updated constants, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_md5_initialize(NX_CRYPTO_MD5 *context, UINT algorithm) { NX_CRYPTO_PARAMETER_NOT_USED(algorithm); /* Determine if the context is non-null. */ if (context == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (context == NX_CRYPTO_NULL) { ... } /* First, clear the bit count for this context. */ context -> nx_md5_bit_count[0] = 0; /* Clear the lower 32-bits of the count */ context -> nx_md5_bit_count[1] = 0; /* Clear the upper 32-bits of the count */ /* Finally, setup the context states. */ context -> nx_md5_states[0] = 0x67452301UL; /* Setup state A */ context -> nx_md5_states[1] = 0xEFCDAB89UL; /* Setup state B */ context -> nx_md5_states[2] = 0x98BADCFEUL; /* Setup state C */ context -> nx_md5_states[3] = 0x10325476UL; /* Setup state D */ /* Return success. */ return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_md5_update PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function updates the digest calculation with new input from */ /* the caller. */ /* */ /* INPUT */ /* */ /* context MD5 context pointer */ /* input_ptr Pointer to byte(s) of input */ /* input_length Length of bytes of input */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_crypto_md5_process_buffer Process complete buffer, */ /* which is 64-bytes in size */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_md5_digest_calculate Perform calculation of the */ /* MD5 digest */ /* _nx_crypto_method_md5_operation Handle MD5 operation */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* verified memcpy use cases, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_md5_update(NX_CRYPTO_MD5 *context, UCHAR *input_ptr, UINT input_length) { ULONG current_bytes; ULONG needed_fill_bytes; /* Determine if the context is non-null. */ if (context == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (context == NX_CRYPTO_NULL) { ... } /* Determine if there is a length. */ if (input_length == 0) { return(NX_CRYPTO_SUCCESS); }if (input_length == 0) { ... } /* Calculate the current byte count mod 64. Note the reason for the shift by 3 is to account for the 8 bits per byte. *//* ... */ current_bytes = (context -> nx_md5_bit_count[0] >> 3) & 0x3F; /* Calculate the current number of bytes needed to be filled. */ needed_fill_bytes = 64 - current_bytes; /* Update the total bit count based on the input length. */ context -> nx_md5_bit_count[0] += (input_length << 3); /* Determine if there is roll-over of the bit count into the MSW. */ if (context -> nx_md5_bit_count[0] < (input_length << 3)) { /* Yes, increment the MSW of the bit count. */ context -> nx_md5_bit_count[1]++; }if (context -> nx_md5_bit_count[0] < (input_length << 3)) { ... } /* Update upper total bit count word. */ context -> nx_md5_bit_count[1] += (input_length >> 29); /* Check for a partial buffer that needs to be transformed. */ if ((current_bytes) && (input_length >= needed_fill_bytes)) { /* Yes, we can complete the buffer and transform it. */ /* Copy the appropriate portion of the input buffer into the internal buffer of the context. *//* ... */ NX_CRYPTO_MEMCPY((void *)&(context -> nx_md5_buffer[current_bytes]), (void *)input_ptr, needed_fill_bytes); /* Use case of memcpy is verified. */ /* Process the 64-byte (512 bit) buffer. */ _nx_crypto_md5_process_buffer(context, context -> nx_md5_buffer); /* Adjust the pointers and length accordingly. */ input_length = input_length - needed_fill_bytes; input_ptr = input_ptr + needed_fill_bytes; /* Clear the remaining bits, since the buffer was processed. */ current_bytes = 0; }if ((current_bytes) && (input_length >= needed_fill_bytes)) { ... } /* Process any and all whole blocks of input. */ while (input_length >= 64) { /* Process this 64-byte (512 bit) buffer. */ _nx_crypto_md5_process_buffer(context, input_ptr); /* Adjust the pointers and length accordingly. */ input_length = input_length - 64; input_ptr = input_ptr + 64; }while (input_length >= 64) { ... } /* Determine if there is anything left. */ if (input_length) { /* Save the remaining bytes in the internal buffer after any remaining bytes that it is processed later. *//* ... */ NX_CRYPTO_MEMCPY((void *)&(context -> nx_md5_buffer[current_bytes]), (void *)input_ptr, input_length); /* Use case of memcpy is verified. */ }if (input_length) { ... } /* Return success. */ return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_md5_digest_calculate PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function finishes calculation of the MD5 digest. It is called */ /* where there is no further input needed for the digest. The resulting*/ /* 16-byte (128-bit) MD5 digest is returned to the caller. */ /* */ /* INPUT */ /* */ /* context MD5 context pointer */ /* digest 16-byte (128-bit) digest */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* algorithm Algorithm type */ /* */ /* CALLS */ /* */ /* _nx_crypto_md5_update Update the digest with padding*/ /* and length of digest */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_method_md5_operation Handle MD5 operation */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* updated constants, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_md5_digest_calculate(NX_CRYPTO_MD5 *context, UCHAR digest[16], UINT algorithm) { UCHAR bit_count_string[8]; ULONG current_byte_count; ULONG padding_bytes; NX_CRYPTO_PARAMETER_NOT_USED(algorithm); /* Move the lower portion of the bit count into the array. */ bit_count_string[0] = (UCHAR)context -> nx_md5_bit_count[0]; bit_count_string[1] = (UCHAR)(context -> nx_md5_bit_count[0] >> 8); bit_count_string[2] = (UCHAR)(context -> nx_md5_bit_count[0] >> 16); bit_count_string[3] = (UCHAR)(context -> nx_md5_bit_count[0] >> 24); bit_count_string[4] = (UCHAR)context -> nx_md5_bit_count[1]; bit_count_string[5] = (UCHAR)(context -> nx_md5_bit_count[1] >> 8); bit_count_string[6] = (UCHAR)(context -> nx_md5_bit_count[1] >> 16); bit_count_string[7] = (UCHAR)(context -> nx_md5_bit_count[1] >> 24); /* Calculate the current byte count. */ current_byte_count = (context -> nx_md5_bit_count[0] >> 3) & 0x3F; /* Calculate the padding bytes needed. */ padding_bytes = (current_byte_count < 56) ? (56 - current_byte_count) : (120 - current_byte_count); /* Add any padding required. */ _nx_crypto_md5_update(context, (UCHAR*)_nx_crypto_md5_padding, padding_bytes); /* Add the in the length. */ _nx_crypto_md5_update(context, bit_count_string, 8); /* Now store the digest in the caller specified destination. */ digest[0] = (UCHAR)context -> nx_md5_states[0]; digest[1] = (UCHAR)(context -> nx_md5_states[0] >> 8); digest[2] = (UCHAR)(context -> nx_md5_states[0] >> 16); digest[3] = (UCHAR)(context -> nx_md5_states[0] >> 24); digest[4] = (UCHAR)context -> nx_md5_states[1]; digest[5] = (UCHAR)(context -> nx_md5_states[1] >> 8); digest[6] = (UCHAR)(context -> nx_md5_states[1] >> 16); digest[7] = (UCHAR)(context -> nx_md5_states[1] >> 24); digest[8] = (UCHAR)context -> nx_md5_states[2]; digest[9] = (UCHAR)(context -> nx_md5_states[2] >> 8); digest[10] = (UCHAR)(context -> nx_md5_states[2] >> 16); digest[11] = (UCHAR)(context -> nx_md5_states[2] >> 24); digest[12] = (UCHAR)context -> nx_md5_states[3]; digest[13] = (UCHAR)(context -> nx_md5_states[3] >> 8); digest[14] = (UCHAR)(context -> nx_md5_states[3] >> 16); digest[15] = (UCHAR)(context -> nx_md5_states[3] >> 24); #ifdef NX_SECURE_KEY_CLEAR NX_CRYPTO_MEMSET(bit_count_string, 0, sizeof(bit_count_string)); #endif /* NX_SECURE_KEY_CLEAR */ /* Return successful completion. */ return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_md5_process_buffer PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function actually uses the MD5 algorithm to process a 64-byte */ /* (512 bit) buffer. */ /* */ /* INPUT */ /* */ /* context MD5 context pointer */ /* buffer 64-byte buffer */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_crypto_md5_update Update the digest with padding*/ /* and length of digest */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP VOID _nx_crypto_md5_process_buffer(NX_CRYPTO_MD5 *context, UCHAR buffer[64]) { UINT i, j; ULONG a, b, c, d; ULONG x[16]; /* Initialize the state variables. */ a = context -> nx_md5_states[0]; b = context -> nx_md5_states[1]; c = context -> nx_md5_states[2]; d = context -> nx_md5_states[3]; /* Now, setup the x array of ULONGs for fast processing. */ j = 0; for (i = 0; i < 16; i++) { /* Convert 4 bytes into one 32-bit word. */ x[i] = ((ULONG)buffer[j]) | (((ULONG)buffer[j + 1]) << 8) | (((ULONG)buffer[j + 2]) << 16) | (((ULONG)buffer[j + 3]) << 24); /* Move to next position in source array. */ j = j + 4; }for (i = 0; i < 16; i++) { ... } /* Process Round 1 of MD5 calculation. */ FF(a, b, c, d, x[0], 7, 0xd76aa478UL); FF(d, a, b, c, x[1], 12, 0xe8c7b756UL); FF(c, d, a, b, x[2], 17, 0x242070dbUL); FF(b, c, d, a, x[3], 22, 0xc1bdceeeUL); FF(a, b, c, d, x[4], 7, 0xf57c0fafUL); FF(d, a, b, c, x[5], 12, 0x4787c62aUL); FF(c, d, a, b, x[6], 17, 0xa8304613UL); FF(b, c, d, a, x[7], 22, 0xfd469501UL); FF(a, b, c, d, x[8], 7, 0x698098d8UL); FF(d, a, b, c, x[9], 12, 0x8b44f7afUL); FF(c, d, a, b, x[10], 17, 0xffff5bb1UL); FF(b, c, d, a, x[11], 22, 0x895cd7beUL); FF(a, b, c, d, x[12], 7, 0x6b901122UL); FF(d, a, b, c, x[13], 12, 0xfd987193UL); FF(c, d, a, b, x[14], 17, 0xa679438eUL); FF(b, c, d, a, x[15], 22, 0x49b40821UL); /* Process Round 2 of MD5 calculation. */ GG(a, b, c, d, x[1], 5, 0xf61e2562UL); GG(d, a, b, c, x[6], 9, 0xc040b340UL); GG(c, d, a, b, x[11], 14, 0x265e5a51UL); GG(b, c, d, a, x[0], 20, 0xe9b6c7aaUL); GG(a, b, c, d, x[5], 5, 0xd62f105dUL); GG(d, a, b, c, x[10], 9, 0x02441453UL); GG(c, d, a, b, x[15], 14, 0xd8a1e681UL); GG(b, c, d, a, x[4], 20, 0xe7d3fbc8UL); GG(a, b, c, d, x[9], 5, 0x21e1cde6UL); GG(d, a, b, c, x[14], 9, 0xc33707d6UL); GG(c, d, a, b, x[3], 14, 0xf4d50d87UL); GG(b, c, d, a, x[8], 20, 0x455a14edUL); GG(a, b, c, d, x[13], 5, 0xa9e3e905UL); GG(d, a, b, c, x[2], 9, 0xfcefa3f8UL); GG(c, d, a, b, x[7], 14, 0x676f02d9UL); GG(b, c, d, a, x[12], 20, 0x8d2a4c8aUL); /* Process Round 3 of MD5 calculation. */ HH(a, b, c, d, x[5], 4, 0xfffa3942UL); HH(d, a, b, c, x[8], 11, 0x8771f681UL); HH(c, d, a, b, x[11], 16, 0x6d9d6122UL); HH(b, c, d, a, x[14], 23, 0xfde5380cUL); HH(a, b, c, d, x[1], 4, 0xa4beea44UL); HH(d, a, b, c, x[4], 11, 0x4bdecfa9UL); HH(c, d, a, b, x[7], 16, 0xf6bb4b60UL); HH(b, c, d, a, x[10], 23, 0xbebfbc70UL); HH(a, b, c, d, x[13], 4, 0x289b7ec6UL); HH(d, a, b, c, x[0], 11, 0xeaa127faUL); HH(c, d, a, b, x[3], 16, 0xd4ef3085UL); HH(b, c, d, a, x[6], 23, 0x04881d05UL); HH(a, b, c, d, x[9], 4, 0xd9d4d039UL); HH(d, a, b, c, x[12], 11, 0xe6db99e5UL); HH(c, d, a, b, x[15], 16, 0x1fa27cf8UL); HH(b, c, d, a, x[2], 23, 0xc4ac5665UL); /* Process Round 4 of MD5 calculation. */ II(a, b, c, d, x[0], 6, 0xf4292244UL); II(d, a, b, c, x[7], 10, 0x432aff97UL); II(c, d, a, b, x[14], 15, 0xab9423a7UL); II(b, c, d, a, x[5], 21, 0xfc93a039UL); II(a, b, c, d, x[12], 6, 0x655b59c3UL); II(d, a, b, c, x[3], 10, 0x8f0ccc92UL); II(c, d, a, b, x[10], 15, 0xffeff47dUL); II(b, c, d, a, x[1], 21, 0x85845dd1UL); II(a, b, c, d, x[8], 6, 0x6fa87e4fUL); II(d, a, b, c, x[15], 10, 0xfe2ce6e0UL); II(c, d, a, b, x[6], 15, 0xa3014314UL); II(b, c, d, a, x[13], 21, 0x4e0811a1UL); II(a, b, c, d, x[4], 6, 0xf7537e82UL); II(d, a, b, c, x[11], 10, 0xbd3af235UL); II(c, d, a, b, x[2], 15, 0x2ad7d2bbUL); II(b, c, d, a, x[9], 21, 0xeb86d391UL); /* Save the resulting in this MD5 context. */ context -> nx_md5_states[0] += a; context -> nx_md5_states[1] += b; context -> nx_md5_states[2] += c; context -> nx_md5_states[3] += d; #ifdef NX_SECURE_KEY_CLEAR a = 0; b = 0; c = 0; d = 0; NX_CRYPTO_MEMSET(x, 0, sizeof(x));/* ... */ #endif /* NX_SECURE_KEY_CLEAR */ }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_md5_init PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function is the common crypto method init callback for */ /* Microsoft supported MD5 cryptographic algorithm. */ /* */ /* INPUT */ /* */ /* method Pointer to crypto method */ /* key Pointer to key */ /* key_size_in_bits Length of key size in bits */ /* handler Returned crypto handler */ /* crypto_metadata Metadata area */ /* crypto_metadata_size Size of the metadata area */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_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) { NX_CRYPTO_PARAMETER_NOT_USED(key); NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); NX_CRYPTO_PARAMETER_NOT_USED(handle); NX_CRYPTO_STATE_CHECK if (method == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (method == NX_CRYPTO_NULL) { ... } if (crypto_metadata == NX_CRYPTO_NULL) { /* metadata is not passed by IPsec. */ #ifndef NX_IPSEC_ENABLE return(NX_CRYPTO_PTR_ERROR); #endif }if (crypto_metadata == NX_CRYPTO_NULL) { ... } else if (((((ULONG)crypto_metadata) & 0x3) != 0) || (crypto_metadata_size < sizeof(NX_CRYPTO_MD5))) { return(NX_CRYPTO_PTR_ERROR); }else if (((((ULONG)crypto_metadata) & 0x3) != 0) || (crypto_metadata_size < sizeof(NX_CRYPTO_MD5))) { ... } return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_md5_cleanup PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function cleans up the crypto metadata. */ /* */ /* INPUT */ /* */ /* crypto_metadata Crypto metadata */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* NX_CRYPTO_MEMSET Set the memory */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_md5_cleanup(VOID *crypto_metadata) { NX_CRYPTO_STATE_CHECK #ifdef NX_SECURE_KEY_CLEAR if (!crypto_metadata) return (NX_CRYPTO_SUCCESS); /* Clean up the crypto metadata. */ NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_MD5));/* ... */ #else NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); #endif/* NX_SECURE_KEY_CLEAR */ return(NX_CRYPTO_SUCCESS); }{ ... } ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_crypto_method_md5_operation PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Timothy Stapko, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function encrypts and decrypts a message using */ /* the MD5 algorithm. */ /* */ /* INPUT */ /* */ /* op MD5 operation */ /* handle Crypto handle */ /* method Cryption Method Object */ /* key Encryption Key */ /* key_size_in_bits Key size in bits */ /* input Input data */ /* input_length_in_byte Input data size */ /* iv_ptr Initial vector */ /* output Output buffer */ /* output_length_in_byte Output buffer size */ /* crypto_metadata Metadata area */ /* crypto_metadata_size Metadata area size */ /* packet_ptr Pointer to packet */ /* nx_crypto_hw_process_callback Callback function pointer */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_crypto_md5_initialize Initialize the MD5 context */ /* _nx_crypto_md5_update Update the digest with padding*/ /* and length of digest */ /* _nx_crypto_md5_digest_calculate Perform calculation of the */ /* MD5 digest */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Timothy Stapko Initial Version 6.0 */ /* 09-30-2020 Timothy Stapko Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ NX_CRYPTO_KEEP UINT _nx_crypto_method_md5_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ VOID *handle, /* Crypto handler */ 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)) { NX_CRYPTO_MD5 *ctx = (NX_CRYPTO_MD5 *)crypto_metadata; #ifdef NX_IPSEC_ENABLE NX_CRYPTO_MD5 metadata; #endif NX_CRYPTO_PARAMETER_NOT_USED(handle); NX_CRYPTO_PARAMETER_NOT_USED(key); NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); NX_CRYPTO_STATE_CHECK if (method == NX_CRYPTO_NULL) { return(NX_CRYPTO_PTR_ERROR); }if (method == NX_CRYPTO_NULL) { ... } /* Verify the metadata addrsss is 4-byte aligned. */ if (crypto_metadata == NX_CRYPTO_NULL) { #ifdef NX_IPSEC_ENABLE /* metadata is not passed by IPsec. */ ctx = &metadata;/* ... */ #else return(NX_CRYPTO_PTR_ERROR); #endif }if (crypto_metadata == NX_CRYPTO_NULL) { ... } else if (((((ULONG)crypto_metadata) & 0x3) != 0) || (crypto_metadata_size < sizeof(NX_CRYPTO_MD5))) { return(NX_CRYPTO_PTR_ERROR); }else if (((((ULONG)crypto_metadata) & 0x3) != 0) || (crypto_metadata_size < sizeof(NX_CRYPTO_MD5))) { ... } switch (op) { case NX_CRYPTO_HASH_INITIALIZE: _nx_crypto_md5_initialize(ctx, method -> nx_crypto_algorithm); break; case NX_CRYPTO_HASH_INITIALIZE: case NX_CRYPTO_HASH_UPDATE: _nx_crypto_md5_update(ctx, input, input_length_in_byte); break; case NX_CRYPTO_HASH_UPDATE: case NX_CRYPTO_HASH_CALCULATE: if(output_length_in_byte < 16) return(NX_CRYPTO_INVALID_BUFFER_SIZE); _nx_crypto_md5_digest_calculate(ctx, output, method -> nx_crypto_algorithm); break; case NX_CRYPTO_HASH_CALCULATE: default: if(output_length_in_byte < 16) return(NX_CRYPTO_INVALID_BUFFER_SIZE); _nx_crypto_md5_initialize(ctx, method -> nx_crypto_algorithm); _nx_crypto_md5_update(ctx, input, input_length_in_byte); _nx_crypto_md5_digest_calculate(ctx, output, method -> nx_crypto_algorithm); #if defined(NX_SECURE_KEY_CLEAR) && defined(NX_IPSEC_ENABLE) if (crypto_metadata == NX_CRYPTO_NULL) { memset(ctx, 0, sizeof(NX_CRYPTO_MD5)); }if (crypto_metadata == NX_CRYPTO_NULL) { ... } /* ... */#endif break;default }switch (op) { ... } return NX_CRYPTO_SUCCESS; }{ ... }