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 Secure Component                                                 */
17 /**                                                                       */
18 /**    Transport Layer Security (TLS)                                     */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SECURE_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_secure_tls.h"
29 
30 #define NX_SECURE_SOURCE_CODE
31 #include "nx_secure_tls_api.h"
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    nx_secure_module_hash_compute                       PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Timothy Stapko, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function uses user-supplied HMAC-SHA256 function (in the       */
45 /*    proper NX_CRYPTO_METHOD structure) to compute the hash value of     */
46 /*    the program memory marked by the symbols EL_SECURE_PROGRAM_BEGIN    */
47 /*    and EL_SECURE_PROGRAM_END.                                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    hmac_sha2_ptr                         Pointer to NX_CRYPTO_METHOD   */
52 /*                                            structure that contains     */
53 /*                                            HMAC-SHA256                 */
54 /*    key                                   User-specified key for        */
55 /*                                            computing the hash          */
56 /*    key_length                            Size of the key, in bytes     */
57 /*    output_buffer                         Output buffer space for       */
58 /*                                            storing the computed HMAC   */
59 /*    output_buffer_size                    Size of the output buffer     */
60 /*    actual_size                           Size of the HMAC message, in  */
61 /*                                            bytes                       */
62 /*                                                                        */
63 /*  OUTPUT                                                                */
64 /*                                                                        */
65 /*    status                                Completion status             */
66 /*                                                                        */
67 /*  CALLS                                                                 */
68 /*                                                                        */
69 /*    None                                                                */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Application Code                                                    */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
80 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
nx_secure_module_hash_compute(NX_CRYPTO_METHOD * hmac_ptr,UINT start_address,UINT end_address,UCHAR * key,UINT key_length,VOID * metadata,UINT metadata_size,UCHAR * output_buffer,UINT output_buffer_size,UINT * actual_size)84 UINT nx_secure_module_hash_compute(NX_CRYPTO_METHOD *hmac_ptr,
85                                    UINT start_address,
86                                    UINT end_address,
87                                    UCHAR *key, UINT key_length,
88                                    VOID *metadata, UINT metadata_size,
89                                    UCHAR *output_buffer, UINT output_buffer_size, UINT *actual_size)
90 {
91 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
92 VOID *handler = NX_NULL;
93 UINT status;
94 
95     if(output_buffer_size < 32)
96         return(1);
97 
98     /* Validate the crypto table. */
99     if(hmac_ptr == NX_NULL)
100         return(1);
101 
102     if(hmac_ptr -> nx_crypto_algorithm != NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256)
103         return(1);
104 
105     if (hmac_ptr -> nx_crypto_init)
106     {
107         status = hmac_ptr -> nx_crypto_init(hmac_ptr,
108                                                key,
109                                                (key_length << 3),
110                                                &handler,
111                                                metadata,
112                                                metadata_size);
113 
114         if (status != NX_CRYPTO_SUCCESS)
115         {
116             return(1);
117         }
118     }
119 
120     if (hmac_ptr -> nx_crypto_operation == NX_NULL)
121     {
122         return(1);
123     }
124 
125     /* Now compute the hash */
126     status = hmac_ptr -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE,
127                                                 handler,     /* handle, not used */
128                                                 hmac_ptr, /* Method, not used */
129                                                 key,
130                                                 (key_length << 3),
131                                                 (UCHAR*)start_address, /* Data start */
132                                                 end_address - start_address, /* Data Length */
133                                                 NX_NULL, /* iv_ptr, not used */
134                                                 output_buffer,
135                                                 output_buffer_size,
136                                                 metadata,
137                                                 metadata_size,
138                                                 NX_NULL, /* packet_ptr, not used. */
139                                                 NX_NULL);/* HW process callback, not used. */
140 
141     if (status)
142     {
143         return(1);
144     }
145 
146     if (hmac_ptr -> nx_crypto_cleanup)
147     {
148         status = hmac_ptr -> nx_crypto_cleanup(metadata);
149 
150         if (status)
151         {
152             return(1);
153         }
154     }
155 
156     *actual_size = (hmac_ptr -> nx_crypto_ICV_size_in_bits >> 3);
157 
158     return(0);
159 #else
160     NX_PARAMETER_NOT_USED(hmac_ptr);
161     NX_PARAMETER_NOT_USED(start_address);
162     NX_PARAMETER_NOT_USED(end_address);
163     NX_PARAMETER_NOT_USED(key);
164     NX_PARAMETER_NOT_USED(key_length);
165     NX_PARAMETER_NOT_USED(metadata);
166     NX_PARAMETER_NOT_USED(metadata_size);
167     NX_PARAMETER_NOT_USED(output_buffer);
168     NX_PARAMETER_NOT_USED(output_buffer_size);
169     NX_PARAMETER_NOT_USED(actual_size);
170     return(0);
171 #endif
172 }
173 
174