1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Crypto Component                                                 */
16 /**                                                                       */
17 /**   SHA-256 Digest Algorithm (SHA2)                                     */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  COMPONENT DEFINITION                                   RELEASE        */
25 /*                                                                        */
26 /*    nx_crypto_sha2.h                                    PORTABLE C      */
27 /*                                                           6.1          */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    Timothy Stapko, Microsoft Corporation                               */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    This file defines the NetX SHA256 component, derived primarily      */
35 /*    from NIST FIPS PUB 180-4 (Crypto Hash Standard).                    */
36 /*                                                                        */
37 /*    It is assumed that nx_api.h and nx_port.h have already been         */
38 /*    included.                                                           */
39 /*                                                                        */
40 /*  RELEASE HISTORY                                                       */
41 /*                                                                        */
42 /*    DATE              NAME                      DESCRIPTION             */
43 /*                                                                        */
44 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
45 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
46 /*                                            resulting in version 6.1    */
47 /*                                                                        */
48 /**************************************************************************/
49 
50 
51 
52 #ifndef SRC_NX_CRYPTO_SHA2_H_
53 #define SRC_NX_CRYPTO_SHA2_H_
54 
55 /* Determine if a C++ compiler is being used.  If so, ensure that standard
56    C is used to process the API information.  */
57 #ifdef __cplusplus
58 
59 /* Yes, C++ compiler is present.  Use standard C.  */
60 extern   "C" {
61 
62 #endif
63 
64 
65 #include "nx_crypto.h"
66 
67 #define NX_CRYPTO_SHA2_BLOCK_SIZE_IN_BYTES  64
68 #define NX_CRYPTO_SHA224_ICV_LEN_IN_BITS    224
69 #define NX_CRYPTO_SHA256_ICV_LEN_IN_BITS    256
70 
71 /* Define the control block structure for backward compatibility. */
72 #define NX_SHA256                               NX_CRYPTO_SHA256
73 
74 typedef struct NX_CRYPTO_SHA256_STRUCT
75 {
76 
77     ULONG nx_sha256_states[8];                          /* Contains each state (A,B,C,D,E,F,G,H).   */
78     ULONG nx_sha256_bit_count[2];                       /* Contains the 64-bit total bit            */
79                                                         /*   count, where index 0 holds the         */
80                                                         /*   least significant bit count and        */
81                                                         /*   index 1 contains the most              */
82                                                         /*   significant portion of the bit         */
83                                                         /*   count.                                 */
84     UCHAR nx_sha256_buffer[64];                         /* Working buffer for SHA256 algorithm      */
85                                                         /*   where partial buffers are              */
86                                                         /*   accumulated until a full block         */
87                                                         /*   can be processed.                      */
88     ULONG nx_sha256_word_array[64];                     /* Working 64 word array.                   */
89 } NX_CRYPTO_SHA256;
90 
91 
92 UINT _nx_crypto_sha256_initialize(NX_CRYPTO_SHA256 *context, UINT algorithm);
93 UINT _nx_crypto_sha256_update(NX_CRYPTO_SHA256 *context, UCHAR *input_ptr, UINT input_length);
94 UINT _nx_crypto_sha256_digest_calculate(NX_CRYPTO_SHA256 *context, UCHAR *digest, UINT algorithm);
95 VOID _nx_crypto_sha256_process_buffer(NX_CRYPTO_SHA256 * context, UCHAR buffer[64]);
96 
97 UINT _nx_crypto_method_sha256_init(struct  NX_CRYPTO_METHOD_STRUCT *method,
98                                    UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
99                                    VOID  **handle,
100                                    VOID  *crypto_metadata,
101                                    ULONG crypto_metadata_size);
102 
103 UINT _nx_crypto_method_sha256_cleanup(VOID *crypto_metadata);
104 
105 UINT _nx_crypto_method_sha256_operation(UINT op,      /* Encrypt, Decrypt, Authenticate */
106                                         VOID *handle, /* Crypto handler */
107                                         struct NX_CRYPTO_METHOD_STRUCT *method,
108                                         UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
109                                         UCHAR *input, ULONG input_length_in_byte,
110                                         UCHAR *iv_ptr,
111                                         UCHAR *output, ULONG output_length_in_byte,
112                                         VOID *crypto_metadata, ULONG crypto_metadata_size,
113                                         VOID *packet_ptr,
114                                         VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status));
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif /* SRC_NX_CRYPTO_SHA2_H_ */
121 
122