1 
2 #include <stdio.h>
3 #include "nx_secure_tls.h"
4 
5 #include "nx_crypto_des.h"
6 #include "nx_crypto_3des.h"
7 #include "nx_crypto_aes.h"
8 #include "nx_crypto_hmac_sha1.h"
9 #include "nx_crypto_hmac_sha5.h"
10 #include "nx_crypto_hmac_md5.h"
11 
12 #include "tls_test_utility.h"
13 #include "nx_secure_crypto_table_self_test.h"
14 
15 /* Metadata buffer. */
16 static UCHAR metadata[10240];
17 
18 static TX_THREAD thread_0;
19 
20 static VOID thread_0_entry(ULONG thread_input);
21 
22 extern const NX_SECURE_TLS_CRYPTO nx_crypto_tls_ciphers;
23 
24 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
25 static NX_CRYPTO_METHOD crypto_method_des_cbc =
26 {
27     NX_CRYPTO_ENCRYPTION_DES_CBC,             /* DES crypto algorithm filled at runtime*/
28     0,                                        /* Key size in bits                       */
29     NX_CRYPTO_DES_IV_LEN_IN_BITS,             /* IV size in bits                        */
30     0,                                        /* ICV size in bits, not used.            */
31     NX_CRYPTO_DES_BLOCK_SIZE_IN_BITS,         /* Block size in bytes.                   */
32     sizeof(NX_CRYPTO_DES),                    /* Metadata size in bytes                 */
33     _nx_crypto_method_des_init,               /* DES initialization routine.            */
34     NX_NULL,                                  /* DES cleanup routine, not used.         */
35     _nx_crypto_method_des_operation           /* DES operation                          */
36 
37 };
38 static NX_CRYPTO_METHOD crypto_method_3des_cbc =
39 {
40     NX_CRYPTO_ENCRYPTION_3DES_CBC,            /* 3DES crypto algorithm filled at runtime*/
41     0,                                        /* Key size in bits                       */
42     NX_CRYPTO_3DES_IV_LEN_IN_BITS,            /* IV size in bits                        */
43     0,                                        /* ICV size in bits, not used.            */
44     NX_CRYPTO_3DES_BLOCK_SIZE_IN_BITS,        /* Block size in bytes.                   */
45     sizeof(NX_CRYPTO_3DES),                   /* Metadata size in bytes                 */
46     _nx_crypto_method_3des_init,              /* 3DES initialization routine.            */
47     NX_NULL,                                  /* 3DES cleanup routine, not used.         */
48     _nx_crypto_method_3des_operation          /* 3DES operation                          */
49 
50 };
51 static NX_CRYPTO_METHOD crypto_method_aes_cbc_192 =
52 {
53     NX_CRYPTO_ENCRYPTION_AES_CBC,                /* AES crypto algorithm                   */
54     NX_CRYPTO_AES_192_KEY_LEN_IN_BITS,           /* Key size in bits                       */
55     NX_CRYPTO_AES_IV_LEN_IN_BITS,                /* IV size in bits                        */
56     0,                                           /* ICV size in bits, not used.            */
57     (NX_CRYPTO_AES_BLOCK_SIZE_IN_BITS >> 3),     /* Block size in bytes.                   */
58     sizeof(NX_AES),                              /* Metadata size in bytes                 */
59     _nx_crypto_method_aes_init,                  /* AES-CBC initialization routine.        */
60     NX_NULL,                                     /* AES-CBC cleanup routine, not used.     */
61     _nx_crypto_method_aes_cbc_operation          /* AES-CBC operation                      */
62 };
63 static NX_CRYPTO_METHOD crypto_method_aes_ctr_128 =
64 {
65     NX_CRYPTO_ENCRYPTION_AES_CTR,             /* AES crypto algorithm                   */
66     NX_CRYPTO_AES_128_KEY_LEN_IN_BITS,        /* Key size in bits                       */
67     64,                                       /* IV size in bits                        */
68     0,                                        /* ICV size in bits, not used             */
69     (NX_CRYPTO_AES_BLOCK_SIZE_IN_BITS >> 3),  /* Block size in bytes.                   */
70     sizeof(NX_AES),                           /* Metadata size in bytes                 */
71     _nx_crypto_method_aes_init,               /* AES-CTR initialization routine.        */
72     NX_NULL,                                  /* AES-CTR cleanup routine, not used.     */
73     _nx_crypto_method_aes_ctr_operation       /* AES-CTR operation                      */
74 };
75 static NX_CRYPTO_METHOD crypto_method_aes_ctr_192 =
76 {
77     NX_CRYPTO_ENCRYPTION_AES_CTR,             /* AES crypto algorithm                   */
78     NX_CRYPTO_AES_192_KEY_LEN_IN_BITS,        /* Key size in bits                       */
79     64,                                       /* IV size in bits                        */
80     0,                                        /* ICV size in bits, not used             */
81     (NX_CRYPTO_AES_BLOCK_SIZE_IN_BITS >> 3),  /* Block size in bytes.                   */
82     sizeof(NX_AES),                           /* Metadata size in bytes                 */
83     _nx_crypto_method_aes_init,               /* AES-CTR initialization routine.        */
84     NX_NULL,                                  /* AES-CTR cleanup routine, not used.     */
85     _nx_crypto_method_aes_ctr_operation       /* AES-CTR operation                      */
86 };
87 static NX_CRYPTO_METHOD crypto_method_aes_ctr_256 =
88 {
89     NX_CRYPTO_ENCRYPTION_AES_CTR,             /* AES crypto algorithm                   */
90     NX_CRYPTO_AES_256_KEY_LEN_IN_BITS,        /* Key size in bits                       */
91     64,                                       /* IV size in bits                        */
92     0,                                        /* ICV size in bits, not used             */
93     (NX_CRYPTO_AES_BLOCK_SIZE_IN_BITS >> 3),  /* Block size in bytes.                   */
94     sizeof(NX_AES),                           /* Metadata size in bytes                 */
95     _nx_crypto_method_aes_init,               /* AES-CTR initialization routine.        */
96     NX_NULL,                                  /* AES-CTR cleanup routine, not used.     */
97     _nx_crypto_method_aes_ctr_operation       /* AES-CTR operation                      */
98 };
99 static NX_CRYPTO_METHOD crypto_method_hmac_sha1_160 =
100 {
101     NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_160,            /* HMAC SHA1 algorithm                   */
102     0,                                                 /* Key size in bits                      */
103     0,                                                 /* IV size in bits, not used             */
104     160,                                               /* Transmitted ICV size in bits          */
105     0,                                                 /* Block size in bytes, not used         */
106     sizeof(NX_SHA1_HMAC),                              /* Metadata size in bytes                */
107     NX_NULL,                                           /* Initialization routine, not used      */
108     NX_NULL,                                           /* Cleanup routine, not used             */
109     _nx_crypto_method_hmac_sha1_operation              /* HMAC SHA1 operation                   */
110 };
111 static NX_CRYPTO_METHOD crypto_method_hmac_sha384 =
112 {
113     NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_384,            /* HMAC SHA384 algorithm                 */
114     0,                                                 /* Key size in bits                      */
115     0,                                                 /* IV size in bits, not used             */
116     384,                                               /* Transmitted ICV size in bits          */
117     0,                                                 /* Block size in bytes, not used         */
118     sizeof(NX_CRYPTO_SHA512_HMAC),                     /* Metadata size in bytes                */
119     NX_NULL,                                           /* Initialization routine, not used      */
120     NX_NULL,                                           /* Cleanup routine, not used             */
121     _nx_crypto_method_hmac_sha512_operation            /* HMAC SHA384 operation                 */
122 };
123 static NX_CRYPTO_METHOD crypto_method_hmac_sha512 =
124 {
125     NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512,            /* HMAC SHA384 algorithm                 */
126     0,                                                 /* Key size in bits                      */
127     0,                                                 /* IV size in bits, not used             */
128     512,                                               /* Transmitted ICV size in bits          */
129     0,                                                 /* Block size in bytes, not used         */
130     sizeof(NX_SHA512_HMAC),                            /* Metadata size in bytes                */
131     NX_NULL,                                           /* Initialization routine, not used      */
132     NX_NULL,                                           /* Cleanup routine, not used             */
133     _nx_crypto_method_hmac_sha512_operation            /* HMAC SHA512 operation                 */
134 };
135 static NX_CRYPTO_METHOD crypto_method_hmac_md5_128 =
136 {
137     NX_CRYPTO_AUTHENTICATION_HMAC_MD5_128,            /* HMAC MD5 algorithm                    */
138     NX_CRYPTO_HMAC_MD5_KEY_LEN_IN_BITS,               /* Key size in bits                      */
139     0,                                                /* IV size in bits, not used             */
140     128,                                              /* Transmitted ICV size in bits          */
141     0,                                                /* Block size in bytes, not used         */
142     sizeof(NX_MD5_HMAC),                              /* Metadata size in bytes                */
143     NX_NULL,                                          /* Initialization routine, not used      */
144     NX_NULL,                                          /* Cleanup routine, not used             */
145     _nx_crypto_method_hmac_md5_operation              /* HMAC MD5 operation                    */
146 };
147 
148 extern NX_CRYPTO_METHOD crypto_method_rsa;
149 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_128;
150 extern NX_CRYPTO_METHOD crypto_method_aes_cbc_256;
151 extern NX_CRYPTO_METHOD crypto_method_sha1;
152 extern NX_CRYPTO_METHOD crypto_method_sha256;
153 extern NX_CRYPTO_METHOD crypto_method_sha384;
154 extern NX_CRYPTO_METHOD crypto_method_sha512;
155 extern NX_CRYPTO_METHOD crypto_method_md5;
156 extern NX_CRYPTO_METHOD crypto_method_hmac_sha1;
157 extern NX_CRYPTO_METHOD crypto_method_hmac_sha256;
158 extern NX_CRYPTO_METHOD crypto_method_hmac_md5;
159 extern NX_CRYPTO_METHOD crypto_method_tls_prf_1;
160 extern NX_CRYPTO_METHOD crypto_method_tls_prf_sha256;
161 
162 /* For now we shall be able to test the following algorithms, based on the value in
163    nx_crypto_algorithm:
164    NX_CRYPTO_KEY_EXCHANGE_RSA (1024/2048/4096 bit key)
165    NX_CRYPTO_ENCRYPTION_DES_CBC
166    NX_CRYPTO_ENCRYPTION_3DES_CBC
167    NX_CRYPTO_ENCRYPTION_AES_CBC (check key_size field)
168    NX_CRYPTO_ENCRYPTION_AES_CTR
169    NX_CRYPTO_HASH_SHA1
170    NX_CRYPTO_HASH_SHA256
171    NX_CRYPTO_HASH_SHA384
172    NX_CRYPTO_HASH_SHA512
173    NX_CRYPTO_HASH_MD5
174    NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_96
175    NX_CRYPTO_AUTHENTICATION_HMAC_SHA1_160
176    NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256
177    NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_384
178    NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_512
179    NX_CRYPTO_AUTHENTICATION_HMAC_MD5_96
180    NX_CRYPTO_AUTHENTICATION_HMAC_MD5_128
181    NX_CRYPTO_PRF_HMAC_SHA1
182    NX_CRYPTO_PRF_HMAC_SHA2_256
183 */
184 static NX_CRYPTO_METHOD *test_crypto_method[] = {
185 &crypto_method_rsa,
186 &crypto_method_des_cbc,
187 &crypto_method_3des_cbc,
188 &crypto_method_aes_cbc_128,
189 &crypto_method_aes_cbc_192,
190 &crypto_method_aes_cbc_256,
191 &crypto_method_aes_ctr_128,
192 &crypto_method_aes_ctr_192,
193 &crypto_method_aes_ctr_256,
194 &crypto_method_sha1,
195 &crypto_method_sha256,
196 &crypto_method_sha384,
197 &crypto_method_sha512,
198 &crypto_method_md5,
199 &crypto_method_hmac_sha1,
200 &crypto_method_hmac_sha1_160,
201 &crypto_method_hmac_sha256,
202 &crypto_method_hmac_sha384,
203 &crypto_method_hmac_sha512,
204 &crypto_method_hmac_md5,
205 &crypto_method_hmac_md5_128,
206 &crypto_method_tls_prf_1,
207 &crypto_method_tls_prf_sha256,
208 };
209 static UINT test_crypto_method_size = sizeof(test_crypto_method) / sizeof(NX_CRYPTO_METHOD *);
210 #endif /* NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK */
211 
212 extern UINT _nx_secure_crypto_method_self_test(const NX_CRYPTO_METHOD *crypto_method,
213                                                VOID *metadata, UINT metadata_size);
214 
215 #ifdef CTEST
216 void test_application_define(void *first_unused_memory);
test_application_define(void * first_unused_memory)217 void test_application_define(void *first_unused_memory)
218 #else
219 void nx_secure_crypto_self_test_application_define(void *first_unused_memory)
220 #endif
221 {
222     tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
223                      first_unused_memory, 4096,
224                      16, 16, 4, TX_AUTO_START);
225 }
226 
thread_0_entry(ULONG thread_input)227 static VOID thread_0_entry(ULONG thread_input)
228 {
229 UINT i;
230 UINT status;
231 
232     /* Print out test information banner.  */
233     printf("NetX Secure Test:   Crypto Table Self Test.............................");
234 
235     status = nx_secure_crypto_table_self_test(&nx_crypto_tls_ciphers, metadata, sizeof(metadata));
236 
237     EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
238 
239 #ifdef NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK
240     for (i = 0; i < test_crypto_method_size; i++)
241     {
242         status = _nx_secure_crypto_method_self_test(test_crypto_method[i], metadata, sizeof(metadata));
243 
244         EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
245     }
246 #endif /* NX_SECURE_POWER_ON_SELF_TEST_MODULE_INTEGRITY_CHECK */
247 
248     status = nx_secure_crypto_rng_self_test();
249 
250     EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
251 
252     printf("SUCCESS!\n");
253 
254     test_control_return(0);
255 }
256