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-512 Digest Algorithm (SHA5)                                     */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  COMPONENT DEFINITION                                   RELEASE        */
25 /*                                                                        */
26 /*    nx_crypto_sha5.h                                    PORTABLE C      */
27 /*                                                           6.1.8        */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    Timothy Stapko, Microsoft Corporation                               */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    This file defines the NetX SHA512 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 /*  08-02-2021     Timothy Stapko           Modified comment(s), and      */
48 /*                                            used ULONG64_DEFINED macro, */
49 /*                                            resulting in version 6.1.8  */
50 /*                                                                        */
51 /**************************************************************************/
52 
53 
54 
55 #ifndef SRC_NX_CRYPTO_SHA5_H_
56 #define SRC_NX_CRYPTO_SHA5_H_
57 
58 /* Determine if a C++ compiler is being used.  If so, ensure that standard
59    C is used to process the API information.  */
60 #ifdef __cplusplus
61 
62 /* Yes, C++ compiler is present.  Use standard C.  */
63 extern   "C" {
64 
65 #endif
66 
67 
68 #include "nx_crypto.h"
69 
70 #ifndef ULONG64_DEFINED
71 #define ULONG64_DEFINED
72 #define ULONG64                              unsigned long long
73 #endif /* ULONG64 */
74 
75 
76 #define NX_CRYPTO_SHA512_BLOCK_SIZE_IN_BYTES    128
77 #define NX_CRYPTO_SHA384_ICV_LEN_IN_BITS        384
78 #define NX_CRYPTO_SHA512_ICV_LEN_IN_BITS        512
79 #define NX_CRYPTO_SHA512_224_ICV_LEN_IN_BITS    224
80 #define NX_CRYPTO_SHA512_256_ICV_LEN_IN_BITS    256
81 
82 /* Define the control block structure for backward compatibility. */
83 #define NX_SHA512                               NX_CRYPTO_SHA512
84 
85 typedef struct NX_CRYPTO_SHA512_STRUCT
86 {
87 
88     ULONG64 nx_sha512_states[8];                        /* Contains each state (A,B,C,D,E,F,G,H).   */
89     ULONG64 nx_sha512_bit_count[2];                     /* Contains the 128-bit total bit            */
90                                                         /*   count, where index 0 holds the         */
91                                                         /*   least significant bit count and        */
92                                                         /*   index 1 contains the most              */
93                                                         /*   significant portion of the bit         */
94                                                         /*   count.                                 */
95     UCHAR nx_sha512_buffer[NX_CRYPTO_SHA512_BLOCK_SIZE_IN_BYTES];
96                                                         /* Working buffer for SHA512 algorithm      */
97                                                         /*   where partial buffers are              */
98                                                         /*   accumulated until a full block         */
99                                                         /*   can be processed.                      */
100     ULONG64 nx_sha512_word_array[128];                  /* Working 64 word array.                   */
101 } NX_CRYPTO_SHA512;
102 
103 
104 UINT _nx_crypto_sha512_initialize(NX_CRYPTO_SHA512 *context, UINT algorithm);
105 UINT _nx_crypto_sha512_update(NX_CRYPTO_SHA512 *context, UCHAR *input_ptr, UINT input_length);
106 UINT _nx_crypto_sha512_digest_calculate(NX_CRYPTO_SHA512 *context, UCHAR *digest, UINT algorithm);
107 VOID _nx_crypto_sha512_process_buffer(NX_CRYPTO_SHA512 *context, UCHAR *buffer);
108 
109 UINT _nx_crypto_method_sha512_init(struct  NX_CRYPTO_METHOD_STRUCT *method,
110                                    UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
111                                    VOID  **handle,
112                                    VOID  *crypto_metadata,
113                                    ULONG crypto_metadata_size);
114 
115 UINT _nx_crypto_method_sha512_cleanup(VOID *crypto_metadata);
116 
117 UINT _nx_crypto_method_sha512_operation(UINT op,      /* Encrypt, Decrypt, Authenticate */
118                                         VOID *handle, /* Crypto handler */
119                                         struct NX_CRYPTO_METHOD_STRUCT *method,
120                                         UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits,
121                                         UCHAR *input, ULONG input_length_in_byte,
122                                         UCHAR *iv_ptr,
123                                         UCHAR *output, ULONG output_length_in_byte,
124                                         VOID *crypto_metadata, ULONG crypto_metadata_size,
125                                         VOID *packet_ptr,
126                                         VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status));
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* SRC_NX_CRYPTO_SHA5_H_ */
133 
134