summaryrefslogtreecommitdiff
path: root/crypto_libraries/src/nx_crypto_hmac.c
blob: 26a397674fa23b0923d05cc5ba3bbbb4d8b79232 (plain)
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Crypto Component                                                 */
/**                                                                       */
/**   HMAC Mode                                                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#include "nx_crypto_hmac.h"

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac                                     PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function calculates the HMAC.                                  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hmac_metadata                         pointer to HMAC metadata      */
/*    input_ptr                             input byte stream             */
/*    input_length                          input byte stream length      */
/*    key_ptr                               key stream                    */
/*    key_length                            key stream length             */
/*    digest_ptr                            generated crypto digest       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_crypto_hmac_initialize            Perform HMAC initialization   */
/*    _nx_crypto_hmac_update                Perform HMAC update           */
/*    _nx_crypto_hmac_digest_calculate      Calculate HMAC digest         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*    _nx_crypto_method_hmac_md5_operation  Handle HMAC-MD5 operation     */
/*    _nx_crypto_method_hmac_sha1_operation Handle HMAC-SHA1 operation    */
/*    _nx_crypto_method_hmac_sha256_operation Handle HMAC-SHA256 operation*/
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_hmac(NX_CRYPTO_HMAC *hmac_metadata,
                                    UCHAR *input_ptr, UINT input_length,
                                    UCHAR *key_ptr, UINT key_length,
                                    UCHAR *digest_ptr, UINT digest_length)
{

    /* Initialize, update and calculate.  */
    _nx_crypto_hmac_initialize(hmac_metadata, key_ptr, key_length);
    _nx_crypto_hmac_update(hmac_metadata, input_ptr, input_length);
    _nx_crypto_hmac_digest_calculate(hmac_metadata, digest_ptr, digest_length);

    /* Return success.  */
    return(NX_CRYPTO_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_initialize                          PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function performs HMAC initialization.                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hmac_metadata                         pointer to HMAC metadata      */
/*    key_ptr                               key stream                    */
/*    key_length                            key stream length             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    [crypto_digest_calculate]             Calculate crypto digest       */
/*    [crypto_initialize]                   Perform crypto initialization */
/*    [crypto_update]                       Perform crypto update         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*    _nx_crypto_hmac                       Calculate the HMAC            */
/*    _nx_crypto_method_hmac_md5_operation  Handle HMAC-MD5 operation     */
/*    _nx_crypto_method_hmac_sha1_operation Handle HMAC-SHA1 operation    */
/*    _nx_crypto_method_hmac_sha256_operation Handle HMAC-SHA256 operation*/
/*    _nx_crypto_method_hmac_sha512_operation Handle HMAC-SHA512 operation*/
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_hmac_initialize(NX_CRYPTO_HMAC *hmac_metadata, UCHAR *key_ptr, UINT key_length)
{
UCHAR temp_key[128];
UINT  i;

    /* If key is longer than block size, reset it to key=CRYPTO(key). */
    if (key_length > hmac_metadata -> block_size)
    {

        hmac_metadata -> crypto_initialize(hmac_metadata -> context, hmac_metadata -> algorithm);

        hmac_metadata -> crypto_update(hmac_metadata -> context, key_ptr, key_length);

        hmac_metadata -> crypto_digest_calculate(hmac_metadata -> context, temp_key, hmac_metadata -> algorithm);

        key_ptr = temp_key;

        key_length = hmac_metadata -> output_length;
    }

    hmac_metadata -> crypto_initialize(hmac_metadata -> context, hmac_metadata -> algorithm);

    /* The HMAC_CRYPTO transform looks like:

       CRYPTO(K XOR opad, CRYPTO(K XOR ipad, text))

       where K is an n byte key,
       ipad is the byte 0x36 repeated block_size times,
       opad is the byte 0x5c repeated block_size times,
       and text is the data being protected.      */

    NX_CRYPTO_MEMSET(hmac_metadata -> k_ipad, 0, hmac_metadata -> block_size);

    NX_CRYPTO_MEMSET(hmac_metadata -> k_opad, 0, hmac_metadata -> block_size);

    NX_CRYPTO_MEMCPY(hmac_metadata -> k_ipad, key_ptr, key_length); /* Use case of memcpy is verified. */

    NX_CRYPTO_MEMCPY(hmac_metadata -> k_opad, key_ptr, key_length); /* Use case of memcpy is verified. */


    /* XOR key with ipad and opad values. */
    for (i = 0; i < hmac_metadata -> block_size; i++)
    {
        hmac_metadata -> k_ipad[i] ^= 0x36;
        hmac_metadata -> k_opad[i] ^= 0x5c;
    }

    /* Kick off the inner hash with our padded key. */
    hmac_metadata -> crypto_update(hmac_metadata -> context, hmac_metadata -> k_ipad, hmac_metadata -> block_size);

#ifdef NX_SECURE_KEY_CLEAR
    NX_CRYPTO_MEMSET(temp_key, 0, sizeof(temp_key));
#endif /* NX_SECURE_KEY_CLEAR  */

    /* Return success.  */
    return(NX_CRYPTO_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_update                              PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function performs HMAC update.                                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hmac_metadata                         pointer to HMAC metadata      */
/*    input_ptr                             input byte stream             */
/*    input_length                          input byte stream length      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    [crypto_update]                       Perform crypto update         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*    _nx_crypto_hmac                       Calculate the HMAC            */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_hmac_update(NX_CRYPTO_HMAC *hmac_metadata, UCHAR *input_ptr, UINT input_length)
{

    /* Update inner CRYPTO. */
    hmac_metadata -> crypto_update(hmac_metadata -> context, input_ptr, input_length);

    /* Return success.  */
    return(NX_CRYPTO_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_digest_calculate                    PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function performs HMAC digest calculation.                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hmac_metadata                         pointer to HMAC metadata      */
/*    digest_ptr                            generated crypto digest       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    [crypto_digest_calculate]             Calculate crypto digest       */
/*    [crypto_initialize]                   Perform crypto initialization */
/*    [crypto_update]                       Perform crypto update         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*    _nx_crypto_hmac                       Calculate the HMAC            */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_hmac_digest_calculate(NX_CRYPTO_HMAC *hmac_metadata, UCHAR *digest_ptr, UINT digest_length)
{
UCHAR icv_ptr[64];

    /* Perform outer CRYPTO. */

    hmac_metadata -> crypto_digest_calculate(hmac_metadata -> context, icv_ptr, hmac_metadata -> algorithm);

    hmac_metadata -> crypto_initialize(hmac_metadata -> context, hmac_metadata -> algorithm);

    hmac_metadata -> crypto_update(hmac_metadata -> context, hmac_metadata -> k_opad, hmac_metadata -> block_size);

    hmac_metadata -> crypto_update(hmac_metadata -> context, icv_ptr, hmac_metadata -> output_length);

    hmac_metadata -> crypto_digest_calculate(hmac_metadata -> context, icv_ptr, hmac_metadata -> algorithm);

    NX_CRYPTO_MEMCPY(digest_ptr, icv_ptr,  (digest_length > hmac_metadata -> output_length ? hmac_metadata -> output_length : digest_length)); /* Use case of memcpy is verified. */

#ifdef NX_SECURE_KEY_CLEAR
    NX_CRYPTO_MEMSET(icv_ptr, 0, sizeof(icv_ptr));
#endif /* NX_SECURE_KEY_CLEAR  */

    /* Return success.  */
    return(NX_CRYPTO_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_metadata_set                        PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sets HMAC metadata.                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hmac_metadata                         pointer to HMAC metadata      */
/*    context                               crypto context                */
/*    k_ipad                                ipad key                      */
/*    k_opad                                opad key                      */
/*    algorithm                             algorithm                     */
/*    block_size                            block size                    */
/*    output_length                         output length                 */
/*    crypto_intitialize                    initializtion function        */
/*    crypto_update                         update function               */
/*    crypto_digest_calculate               digest calculation function   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*    _nx_crypto_method_hmac_md5_operation  Handle HMAC-MD5 operation     */
/*    _nx_crypto_method_hmac_sha1_operation Handle HMAC-SHA1 operation    */
/*    _nx_crypto_method_hmac_sha256_operation Handle HMAC-SHA256 operation*/
/*    _nx_crypto_method_hmac_sha512_operation Handle HMAC-SHA512 operation*/
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP VOID _nx_crypto_hmac_metadata_set(NX_CRYPTO_HMAC *hmac_metadata,
                                                 VOID *context,
                                                 UINT algorithm, UINT block_size, UINT output_length,
                                                 UINT (*crypto_initialize)(VOID *, UINT),
                                                 UINT (*crypto_update)(VOID *, UCHAR *, UINT),
                                                 UINT (*crypto_digest_calculate)(VOID *, UCHAR *, UINT))
{

    hmac_metadata -> context = context;
    hmac_metadata -> algorithm = algorithm;
    hmac_metadata -> block_size = block_size;
    hmac_metadata -> output_length = output_length;
    hmac_metadata -> crypto_initialize = crypto_initialize;
    hmac_metadata -> crypto_update = crypto_update;
    hmac_metadata -> crypto_digest_calculate = crypto_digest_calculate;
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_hash_initialize                     PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is a build-in wrappers for hash initialization.       */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    context                               Crypto context                */
/*    algorithm                             Hash algorithm                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/**************************************************************************/
UINT _nx_crypto_hmac_hash_initialize(VOID *context, UINT algorithm)
{
NX_CRYPTO_HMAC *hmac;
NX_CRYPTO_METHOD *hash;
UINT   metadata_size;
UINT   status;

    NX_CRYPTO_PARAMETER_NOT_USED(algorithm);

    /* Get the HMAC context. */
    hmac = (NX_CRYPTO_HMAC*)context;

    /* Get the hash method and it's metadata. */
    hash = hmac->hash_method;
    metadata_size = hash->nx_crypto_metadata_area_size;

    status = hash->nx_crypto_operation(NX_CRYPTO_HASH_INITIALIZE,
                                       NX_CRYPTO_NULL,
                                       hash,
                                       NX_CRYPTO_NULL,
                                       0,
                                       NX_CRYPTO_NULL,
                                       0,
                                       NX_CRYPTO_NULL,
                                       NX_CRYPTO_NULL,
                                       0,
                                       hmac->hash_context,
                                       metadata_size,
                                       NX_CRYPTO_NULL,
                                       NX_CRYPTO_NULL);


    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_hash_update                         PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is a build-in wrappers for hash data update.          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    context                               Crypto context                */
/*    input                                 Input Stream                  */
/*    input_length                          Input Stream Length           */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/**************************************************************************/
UINT   _nx_crypto_hmac_hash_update(VOID *context, UCHAR *input, UINT input_length)
{
NX_CRYPTO_HMAC *hmac;
NX_CRYPTO_METHOD *hash;
UINT   metadata_size;
UINT   status;

    /* Get the HMAC context. */
    hmac = (NX_CRYPTO_HMAC*)context;

    /* Get the hash method and it's metadata. */
    hash = hmac->hash_method;
    metadata_size = hash->nx_crypto_metadata_area_size;

    /* Perform an update using the generic crypto API. */
    status = hash->nx_crypto_operation(NX_CRYPTO_HASH_UPDATE,
                                       NX_CRYPTO_NULL,
                                       hash,
                                       NX_CRYPTO_NULL,
                                       0,
                                       input,
                                       input_length,
                                       NX_CRYPTO_NULL,
                                       NX_CRYPTO_NULL,
                                       0,
                                       hmac->hash_context,
                                       metadata_size,
                                       NX_CRYPTO_NULL,
                                       NX_CRYPTO_NULL);

    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_hmac_hash_digest_calculate               PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is a build-in wrappers for hash digest calculate.     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    context                               Crypto context                */
/*    digest                                Digest for output             */
/*    algorithm                             Hash algorithm                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/**************************************************************************/
UINT   _nx_crypto_hmac_hash_digest_calculate(VOID *context, UCHAR *digest, UINT algorithm)
{
NX_CRYPTO_HMAC *hmac;
NX_CRYPTO_METHOD *hash;
UINT   metadata_size;
UINT   status;

    NX_CRYPTO_PARAMETER_NOT_USED(algorithm);

    /* Get the HMAC context. */
    hmac = (NX_CRYPTO_HMAC*)context;

    /* Get the hash method and it's metadata. */
    hash = hmac->hash_method;
    metadata_size = hash->nx_crypto_metadata_area_size;

    /* Perform a digest calculation using the generic crypto API. */
    status = hash->nx_crypto_operation(NX_CRYPTO_HASH_CALCULATE,
                                       NX_CRYPTO_NULL,
                                       hash,
                                       NX_CRYPTO_NULL,
                                       0,
                                       NX_CRYPTO_NULL,
                                       0,
                                       NX_CRYPTO_NULL,
                                       digest,
                                       (hash->nx_crypto_ICV_size_in_bits >> 3),
                                       hmac->hash_context,
                                       metadata_size,
                                       NX_CRYPTO_NULL,
                                       NX_CRYPTO_NULL);

    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_method_hmac_init                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the common crypto method initialization routine    */
/*    for the Microsoft implementation of the HMAC 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                                                    */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT  _nx_crypto_method_hmac_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_HMAC       *hmac;*/

    NX_CRYPTO_PARAMETER_NOT_USED(handle);
    NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits);

    NX_CRYPTO_STATE_CHECK

    if ((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL))
    {
        return(NX_CRYPTO_PTR_ERROR);
    }

    /* Verify the metadata address is 4-byte aligned. */
    if((((ULONG)crypto_metadata) & 0x3) != 0)
    {
        return(NX_CRYPTO_PTR_ERROR);
    }

    if(crypto_metadata_size < sizeof(NX_CRYPTO_HMAC))
    {
        return(NX_CRYPTO_PTR_ERROR);
    }

#if 0
    hmac = (NX_CRYPTO_HMAC *)crypto_metadata;

    /* Set the method for later. */
    hmac->hash_method = method;
#endif

    return(NX_CRYPTO_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_method_hmac_cleanup                      PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function cleans up the crypto metadata for the HMAC operation. */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    crypto_metadata                       Crypto metadata               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    NX_CRYPTO_MEMSET                      Set the memory                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT  _nx_crypto_method_hmac_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_HMAC));
#else
    NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata);
#endif/* NX_SECURE_KEY_CLEAR  */

    return(NX_CRYPTO_SUCCESS);
}





/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_crypto_method_hmac_operation                    PORTABLE C      */
/*                                                           6.4.3        */
/*                                                                        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the generic operation for HMAC. HMAC does not care */
/*    what hash is used as long as the hash size is known. Therefore, this*/
/*    method may be used with an arbitrary hash routine as long as the    */
/*    hash has an NX_CRYPTO_METHOD instance properly filled out.          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hmac_metadata                         pointer to HMAC metadata      */
/*    context                               crypto context                */
/*    k_ipad                                ipad key                      */
/*    k_opad                                opad key                      */
/*    algorithm                             algorithm                     */
/*    block_size                            block size                    */
/*    output_length                         output length                 */
/*    crypto_intitialize                    initializtion function        */
/*    crypto_update                         update function               */
/*    crypto_digest_calculate               digest calculation function   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
NX_CRYPTO_KEEP UINT _nx_crypto_method_hmac_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_HMAC       *hmac;
UINT                 status;

    NX_CRYPTO_PARAMETER_NOT_USED(handle);
    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

    /* Verify the metadata address is 4-byte aligned. */
    if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0))
    {
        return(NX_CRYPTO_PTR_ERROR);
    }

    if(crypto_metadata_size < sizeof(NX_CRYPTO_HMAC))
    {
        return(NX_CRYPTO_PTR_ERROR);
    }

    /* Get our control block for HKDF. */
    hmac = (NX_CRYPTO_HMAC *)(crypto_metadata);

    /* Get the metadata for our hash routine. */
    hmac->context = hmac;
    hmac->hash_context = ((UCHAR*)(crypto_metadata)) + sizeof(NX_CRYPTO_HMAC);

    status = NX_CRYPTO_SUCCESS;
    switch (op)
    {
    case NX_CRYPTO_HMAC_SET_HASH:
        hmac->hash_method = method;

        /* Set up the HMAC metadata using the HMAC context for the hash context - this is because our built-in routines
         * (_nx_crypto_hmac_hash_initialize/update/digest_calculate) need the full HMAC context, unlike when HMAC routines
         * are built around HMAC (as opposed to when HMAC is used as a hash wrapper). */
        _nx_crypto_hmac_metadata_set(hmac, hmac,
                                     method->nx_crypto_algorithm, method->nx_crypto_block_size_in_bytes, method->nx_crypto_ICV_size_in_bits >> 3,
                                     _nx_crypto_hmac_hash_initialize,
                                     _nx_crypto_hmac_hash_update,
                                     _nx_crypto_hmac_hash_digest_calculate);

        /* Initialize the hash routine. */
        status = method->nx_crypto_init(method, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, hmac->hash_context, method->nx_crypto_metadata_area_size);
        break;

    case NX_CRYPTO_HASH_INITIALIZE:
        /* Initialize the hash method. */
        if(key == NX_CRYPTO_NULL)
        {
            return(NX_CRYPTO_PTR_ERROR);
        }

        status = _nx_crypto_hmac_initialize(hmac, key, key_size_in_bits >> 3);
        break;

    case NX_CRYPTO_HASH_UPDATE:
        status = _nx_crypto_hmac_update(hmac, input, input_length_in_byte);
        break;

    case NX_CRYPTO_HASH_CALCULATE:
        if(output_length_in_byte == 0)
        {
            return(NX_CRYPTO_INVALID_BUFFER_SIZE);
        }
        status = _nx_crypto_hmac_digest_calculate(hmac, output,
                                                  (output_length_in_byte > (ULONG)((hmac-> hash_method -> nx_crypto_ICV_size_in_bits) >> 3) ?
                                                  ((hmac-> hash_method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
        break;

    default:
        /* Do entire HMAC operation in one pass (init, update, calculate). */
        if(key == NX_CRYPTO_NULL)
        {
            return(NX_CRYPTO_PTR_ERROR);
        }

        if(output_length_in_byte == 0)
        {
            return(NX_CRYPTO_INVALID_BUFFER_SIZE);
        }
        _nx_crypto_hmac(hmac, input, input_length_in_byte, key, (key_size_in_bits >> 3), output,
                        (output_length_in_byte > (ULONG)((hmac -> hash_method -> nx_crypto_ICV_size_in_bits) >> 3) ?
                        ((hmac -> hash_method -> nx_crypto_ICV_size_in_bits) >> 3) : output_length_in_byte));
        break;
    }

    return(status);



}