1 #include <stdio.h>
2 #include "nx_crypto_drbg.h"
3 #include "nx_crypto_method_self_test.h"
4 #include "tls_test_utility.h"
5
6 extern NX_CRYPTO_METHOD crypto_method_drbg;
7
8 /* SHA context. */
9 static NX_CRYPTO_DRBG drbg_ctx;
10
11 #ifndef NX_CRYPTO_STANDALONE_ENABLE
12 static TX_THREAD thread_0;
13 #endif
14
15 static VOID thread_0_entry(ULONG thread_input);
16
test_nx_crypto_init_failed(NX_CRYPTO_METHOD * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,VOID ** handler,VOID * crypto_metadata,ULONG crypto_metadata_size)17 static UINT test_nx_crypto_init_failed(NX_CRYPTO_METHOD *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, VOID **handler, VOID *crypto_metadata, ULONG crypto_metadata_size)
18 {
19 return 233;
20 }
21
test_nx_crypto_operation_failed(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))22 static UINT test_nx_crypto_operation_failed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
23 {
24 return 233;
25 }
26
test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_INSTANTIATE(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))27 static UINT test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_INSTANTIATE(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
28 {
29 if (op == NX_CRYPTO_DRBG_INSTANTIATE)
30 return 232;
31
32 return 0;
33 }
34
test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_GENERATE(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))35 static UINT test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_GENERATE(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
36 {
37 if (op == NX_CRYPTO_DRBG_GENERATE)
38 return 231;
39
40 return 0;
41 }
42
43 static UINT count = 0;
test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_GENERATE_0(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))44 static UINT test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_GENERATE_0(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
45 {
46 if (op == NX_CRYPTO_DRBG_GENERATE)
47 {
48 count++;
49 if (count == 2)
50 return 231;
51 }
52
53 return 0;
54 }
55
test_nx_crypto_operation_succeed(UINT op,VOID * handler,struct NX_CRYPTO_METHOD_STRUCT * method,UCHAR * key,NX_CRYPTO_KEY_SIZE key_size_in_bits,UCHAR * input,ULONG input_length_in_byte,UCHAR * iv_ptr,UCHAR * output,ULONG output_length_in_byte,VOID * crypto_metadata,ULONG crypto_metadata_size,VOID * packet_ptr,VOID (* nx_crypto_hw_process_callback)(VOID * packet_ptr,UINT status))56 static UINT test_nx_crypto_operation_succeed(UINT op, VOID *handler, struct NX_CRYPTO_METHOD_STRUCT *method, UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, UCHAR *input, ULONG input_length_in_byte, UCHAR *iv_ptr, UCHAR *output, ULONG output_length_in_byte, VOID *crypto_metadata, ULONG crypto_metadata_size, VOID *packet_ptr, VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status))
57 {
58 return 0;
59 }
60
61 #ifdef CTEST
62 void test_application_define(void *first_unused_memory);
test_application_define(void * first_unused_memory)63 void test_application_define(void *first_unused_memory)
64 #else
65 void nx_secure_drbg_test_application_define(void *first_unused_memory)
66 #endif
67 {
68 #ifndef NX_CRYPTO_STANDALONE_ENABLE
69 tx_thread_create(&thread_0, "Thread 0", thread_0_entry, 0,
70 first_unused_memory, 4096,
71 16, 16, 4, TX_AUTO_START);
72 #else
73 thread_0_entry(0);
74 #endif
75 }
76
thread_0_entry(ULONG thread_input)77 static VOID thread_0_entry(ULONG thread_input)
78 {
79 UINT i, status, backup;
80 NX_CRYPTO_METHOD test_method;
81
82 /* Print out test information banner. */
83 printf("NetX Secure Test: DRBG Test..........................................");
84
85 /* NULL method pointer. */
86 status = _nx_crypto_method_drbg_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
87 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
88
89 /* NULL metadata pointer. */
90 status = _nx_crypto_method_drbg_init(&crypto_method_drbg, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
91 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
92
93 /* Metadata address is not 4-byte aligned. */
94 status = _nx_crypto_method_drbg_init(&crypto_method_drbg, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, (VOID *)0x03, 0);
95 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
96
97 /* Metadata size is not enough. */
98 status = _nx_crypto_method_drbg_init(&crypto_method_drbg, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, &drbg_ctx, 0);
99 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
100
101 /* NULL metadata pointer. */
102 status = _nx_crypto_method_drbg_cleanup(NX_CRYPTO_NULL);
103 EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
104
105 /* NULL method pointer. */
106 status = _nx_crypto_method_drbg_operation(0, NX_CRYPTO_NULL,
107 NX_CRYPTO_NULL, /* method */
108 NX_CRYPTO_NULL, 0, /* key */
109 NX_CRYPTO_NULL, 0, /* input */
110 NX_CRYPTO_NULL, /* iv */
111 NX_CRYPTO_NULL, 0, /* output */
112 NX_CRYPTO_NULL, 0, /* crypto metadata */
113 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
114 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
115
116 /* NULL metadata pointer. */
117 status = _nx_crypto_method_drbg_operation(0, NX_CRYPTO_NULL,
118 &crypto_method_drbg, /* method */
119 NX_CRYPTO_NULL, 0, /* key */
120 NX_CRYPTO_NULL, 0, /* input */
121 NX_CRYPTO_NULL, /* iv */
122 NX_CRYPTO_NULL, 0, /* output */
123 NX_CRYPTO_NULL, 0, /* crypto metadata */
124 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
125 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
126
127 /* Metadata address is not 4-byte aligned. */
128 status = _nx_crypto_method_drbg_operation(0, NX_CRYPTO_NULL,
129 &crypto_method_drbg, /* method */
130 NX_CRYPTO_NULL, 0, /* key */
131 NX_CRYPTO_NULL, 0, /* input */
132 NX_CRYPTO_NULL, /* iv */
133 NX_CRYPTO_NULL, 0, /* output */
134 (VOID *)0x03, 0, /* crypto metadata */
135 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
136 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
137
138 /* Metadata size is not enough. */
139 status = _nx_crypto_method_drbg_operation(0, NX_CRYPTO_NULL,
140 &crypto_method_drbg, /* method */
141 NX_CRYPTO_NULL, 0, /* key */
142 NX_CRYPTO_NULL, 0, /* input */
143 NX_CRYPTO_NULL, /* iv */
144 NX_CRYPTO_NULL, 0, /* output */
145 &drbg_ctx, 0, /* crypto metadata */
146 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
147 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
148
149 /* NULL key pointer. */
150 status = _nx_crypto_method_drbg_operation(NX_CRYPTO_DRBG_INSTANTIATE, NX_CRYPTO_NULL,
151 &crypto_method_drbg, /* method */
152 NX_CRYPTO_NULL, 0, /* key */
153 NX_CRYPTO_NULL, 0, /* input */
154 NX_CRYPTO_NULL, /* iv */
155 NX_CRYPTO_NULL, 0, /* output */
156 &drbg_ctx, sizeof(drbg_ctx), /* crypto metadata */
157 NX_CRYPTO_NULL, NX_CRYPTO_NULL);
158 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
159
160 /* For NX_CRYPTO_STATE_CHECK. */
161 #ifdef NX_CRYPTO_SELF_TEST
162 backup = _nx_crypto_library_state;
163 _nx_crypto_library_state = 0;
164
165 status = _nx_crypto_method_drbg_init(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
166 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
167
168 status = _nx_crypto_method_drbg_cleanup(NX_CRYPTO_NULL);
169 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
170
171 status = _nx_crypto_method_drbg_operation(0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, 0, NX_CRYPTO_NULL, NX_CRYPTO_NULL);
172 EXPECT_EQ(NX_CRYPTO_INVALID_LIBRARY, status);
173
174 status = _nx_crypto_module_state_get();
175
176 _nx_crypto_library_state = backup;
177
178 /* Tests for _nx_crypto_method_self_test_drbg. */
179
180 /* NULL method pointer. */
181 status = _nx_crypto_method_self_test_drbg(NX_CRYPTO_NULL, NX_CRYPTO_NULL, 0);
182 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
183
184 /* nx_crypto_init and nx_crypto_operation are both NULL. */
185 test_method.nx_crypto_init = NX_CRYPTO_NULL;
186 test_method.nx_crypto_operation = NX_CRYPTO_NULL;
187 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
188 EXPECT_EQ(NX_CRYPTO_PTR_ERROR, status);
189
190 /* nx_crypto_init failed. */
191 test_method.nx_crypto_init = test_nx_crypto_init_failed;
192 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
193 EXPECT_EQ(233, status);
194
195 /* nx_crypto_operation failed. */
196 test_method.nx_crypto_init = NX_CRYPTO_NULL;
197 test_method.nx_crypto_operation = test_nx_crypto_operation_failed;
198 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
199 EXPECT_EQ(233, status);
200
201 /* nx_crypto_operation NX_CRYPTO_DRBG_INSTANTIATE failed. */
202 test_method.nx_crypto_init = NX_CRYPTO_NULL;
203 test_method.nx_crypto_operation = test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_INSTANTIATE;
204 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
205 EXPECT_EQ(232, status);
206
207 /* nx_crypto_operation NX_CRYPTO_DRBG_GENERATE failed. */
208 test_method.nx_crypto_init = NX_CRYPTO_NULL;
209 test_method.nx_crypto_operation = test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_GENERATE;
210 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
211 EXPECT_EQ(231, status);
212
213 /* nx_crypto_operation NX_CRYPTO_DRBG_GENERATE failed at the second time. */
214 test_method.nx_crypto_init = NX_CRYPTO_NULL;
215 test_method.nx_crypto_operation = test_nx_crypto_operation_failed_NX_CRYPTO_DRBG_GENERATE_0;
216 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
217 EXPECT_EQ(231, status);
218
219 /* NX_CRYPTO_MEMCMP failed. */
220 test_method.nx_crypto_init = NX_CRYPTO_NULL;
221 test_method.nx_crypto_operation = test_nx_crypto_operation_succeed;
222 status = _nx_crypto_method_self_test_drbg(&test_method, NX_CRYPTO_NULL, 0);
223 EXPECT_EQ(NX_CRYPTO_NOT_SUCCESSFUL, status);
224
225 /* nx_crypto_cleanup is NULL. */
226 test_method = crypto_method_drbg;
227 test_method.nx_crypto_cleanup = NX_CRYPTO_NULL;
228 status = _nx_crypto_method_self_test_drbg(&test_method, &drbg_ctx, sizeof(drbg_ctx));
229 EXPECT_EQ(NX_CRYPTO_SUCCESS, status);
230
231 #endif /* NX_CRYPTO_SELF_TEST */
232
233 printf("SUCCESS!\n");
234 test_control_return(0);
235 }
236