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_crypto_table_self_test.h"
29 
30 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
31 
32 static UCHAR secret_sha1[] = { 0x86, 0xec, 0x88, 0xbb };
33 static UCHAR label_sha1[] = { 0xc8, 0x37, 0xaf, 0x7d };
34 static UCHAR seed_sha1[] = { 0x36, 0x54, 0xf1, 0x6f };
35 static UCHAR result_sha1[] = {
36 0xab, 0xe3, 0x77, 0xa6, 0x58, 0x4c, 0x97, 0x03, 0x98, 0xe9, 0xe4, 0x62, 0xe6, 0x44, 0xe4, 0x2d,
37 0x21, 0x16, 0xdb, 0x4f, 0x0e, 0x70, 0xc9, 0x83, 0xe5, 0x31, 0x61, 0x95, 0x17, 0xcd, 0xc2, 0xd0,
38 0x7e, 0x9a, 0xdf, 0xf6, 0xe2, 0x44, 0x01, 0x05, 0xa9, 0xb0, 0x7a, 0xbe, 0xc3, 0x9a, 0x47, 0x9b,
39 0xd7, 0xd9, 0x2c, 0xba, 0xb7, 0x8e, 0x90, 0x1d, 0x4f, 0x21, 0xae, 0x4e, 0x0f, 0x60, 0xcf, 0x3b,
40 0xdf, 0xe5, 0x77, 0x79, 0xff, 0x23, 0x23, 0x2d, 0x62, 0x48, 0xc6, 0x72, 0xb3, 0xf9, 0xce, 0x4f,
41 0x46, 0x66, 0x2f, 0xc5, 0x0e, 0xbc, 0x2a, 0x34, 0xd0, 0xc5, 0x37, 0xa2, 0x2f, 0x69, 0x43, 0x74,
42 0x6d, 0x11, 0x3c, 0x1c, 0x75, 0xa5, 0x12, 0x61, 0x1a, 0xc7, 0x8f, 0x41, 0xab, 0xa8, 0x45, 0xd5,
43 0xf3, 0xb3, 0xb4, 0xbd, 0xe3, 0x7c, 0x8b, 0xbb, 0x0f, 0x0d, 0xcb, 0x57, 0xc8, 0x6c, 0x13, 0x32,
44 0x8b, 0xe1, 0xd0, 0x2e, 0x2c, 0x2d, 0xb2, 0xd5, 0x67, 0x1c, 0xb0, 0x61, 0x3d, 0x77, 0x96, 0x68,
45 0x1b, 0x47, 0xdf, 0x3a, 0x50, 0x62, 0x31, 0x4b, 0x30, 0x09, 0xe9, 0x3b, 0xdd, 0xfb, 0x34, 0x1d,
46 };
47 
48 static UCHAR secret_sha256[] = { 0xbc, 0xd6, 0x2b, 0x3f };
49 static UCHAR label_sha256[] = { 0xad, 0x16, 0x21, 0xba };
50 static UCHAR seed_sha256[] = { 0x73, 0x21, 0xe1, 0x4c };
51 static UCHAR result_sha256[] = {
52 0x05, 0xab, 0x98, 0x15, 0xd8, 0x70, 0xd6, 0xdb, 0x27, 0xde, 0x5c, 0xd6, 0x8b, 0xd6, 0xbd, 0xfe,
53 0x0c, 0xee, 0xde, 0x1b, 0xd8, 0x9e, 0x80, 0x4b, 0xf4, 0x71, 0xb2, 0x5f, 0x8a, 0xb6, 0x60, 0x83,
54 0x95, 0xe3, 0x96, 0x36, 0x5d, 0xa6, 0xc0, 0x42, 0x57, 0x02, 0x57, 0x49, 0x5e, 0xff, 0x52, 0xf2,
55 0xe2, 0x96, 0x9a, 0x26, 0x71, 0x87, 0x6d, 0xb7, 0x21, 0x90, 0x4e, 0x82, 0xbb, 0xd1, 0x77, 0xbf,
56 0x8f, 0xd9, 0x89, 0x1f, 0x6d, 0xda, 0xbe, 0xb4, 0x35, 0xb3, 0x0d, 0x3e, 0xdf, 0xcb, 0x18, 0x5b,
57 };
58 
59 /* Output. */
60 static UCHAR output[256];
61 
62 /**************************************************************************/
63 /*                                                                        */
64 /*  FUNCTION                                               RELEASE        */
65 /*                                                                        */
66 /*    nx_secure_crypto_method_self_test_prf              PORTABLE C       */
67 /*                                                           6.1          */
68 /*  AUTHOR                                                                */
69 /*                                                                        */
70 /*    Timothy Stapko, Microsoft Corporation                               */
71 /*                                                                        */
72 /*  DESCRIPTION                                                           */
73 /*                                                                        */
74 /*    This function performs the Known Answer Test for PRF crypto method. */
75 /*                                                                        */
76 /*  INPUT                                                                 */
77 /*                                                                        */
78 /*    method_ptr                            Pointer to the crypto method  */
79 /*                                            to be tested.               */
80 /*                                                                        */
81 /*  OUTPUT                                                                */
82 /*                                                                        */
83 /*    status                                Completion status             */
84 /*                                                                        */
85 /*  CALLS                                                                 */
86 /*                                                                        */
87 /*    None                                                                */
88 /*                                                                        */
89 /*  CALLED BY                                                             */
90 /*                                                                        */
91 /*    Application Code                                                    */
92 /*                                                                        */
93 /*  RELEASE HISTORY                                                       */
94 /*                                                                        */
95 /*    DATE              NAME                      DESCRIPTION             */
96 /*                                                                        */
97 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
98 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
99 /*                                            resulting in version 6.1    */
100 /*                                                                        */
101 /**************************************************************************/
_nx_secure_crypto_method_self_test_prf(NX_CRYPTO_METHOD * crypto_method_prf,VOID * metadata,UINT metadata_size)102 UINT _nx_secure_crypto_method_self_test_prf(NX_CRYPTO_METHOD *crypto_method_prf,
103                                             VOID *metadata, UINT metadata_size)
104 {
105 UCHAR  *secret;
106 UCHAR  *label;
107 UCHAR  *seed;
108 UCHAR  *result;
109 UINT    secret_length;
110 UINT    label_length;
111 UINT    seed_length;
112 UINT    result_length;
113 UINT    status;
114 VOID   *handler = NX_NULL;
115 
116 
117     /* Validate the crypto method */
118     if(crypto_method_prf == NX_NULL)
119         return(NX_PTR_ERROR);
120 
121     /* Set the test data.  */
122     switch (crypto_method_prf -> nx_crypto_algorithm)
123     {
124     case NX_CRYPTO_PRF_HMAC_SHA1:
125         secret = secret_sha1;
126         secret_length = sizeof(secret_sha1);
127         label = label_sha1;
128         label_length = sizeof(label_sha1);
129         seed = seed_sha1;
130         seed_length = sizeof(seed_sha1);
131         result = result_sha1;
132         result_length = sizeof(result_sha1);
133         break;
134     case NX_CRYPTO_PRF_HMAC_SHA2_256:
135         secret = secret_sha256;
136         secret_length = sizeof(secret_sha256);
137         label = label_sha256;
138         label_length = sizeof(label_sha256);
139         seed = seed_sha256;
140         seed_length = sizeof(seed_sha256);
141         result = result_sha256;
142         result_length = sizeof(result_sha256);
143         break;
144     default:
145         return(1);
146     }
147 
148     /* Clear the output buffer.  */
149     NX_SECURE_MEMSET(output, 0, sizeof(output));
150 
151     /* Call the crypto initialization function.  */
152     if (crypto_method_prf -> nx_crypto_init)
153     {
154         status = crypto_method_prf -> nx_crypto_init(crypto_method_prf,
155                                                      secret,
156                                                      secret_length,
157                                                      &handler,
158                                                      metadata,
159                                                      metadata_size);
160 
161         if (status != NX_CRYPTO_SUCCESS)
162         {
163             return(status);
164         }
165     }
166 
167     if (crypto_method_prf -> nx_crypto_operation == NX_NULL)
168     {
169         return(NX_PTR_ERROR);
170     }
171 
172     /* Call the crypto operation function.  */
173     status = crypto_method_prf -> nx_crypto_operation(NX_CRYPTO_PRF,
174                                                       handler,
175                                                       crypto_method_prf,
176                                                       label,
177                                                       label_length,
178                                                       seed,
179                                                       seed_length,
180                                                       NX_NULL,
181                                                       (UCHAR *)output,
182                                                       result_length,
183                                                       metadata,
184                                                       metadata_size,
185                                                       NX_NULL, NX_NULL);
186 
187     /* Check the status.  */
188     if(status != NX_CRYPTO_SUCCESS)
189     {
190         return(status);
191     }
192 
193     /* Validate the output.  */
194     if(NX_SECURE_MEMCMP(output, result, result_length) != 0)
195     {
196         return(NX_NOT_SUCCESSFUL);
197     }
198 
199     if (crypto_method_prf -> nx_crypto_cleanup)
200     {
201         status = crypto_method_prf -> nx_crypto_cleanup(metadata);
202     }
203 
204     return(status);
205 }
206 #endif
207