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