59
59
60
60
#define MSG_TRNG_READY " ready"
61
61
#define MSG_TRNG_BUFFER " buffer"
62
+ #define MSG_TRNG_EXIT " exit"
62
63
63
64
#define MSG_TRNG_TEST_STEP1 " check_step1"
64
65
#define MSG_TRNG_TEST_STEP2 " check_step2"
@@ -102,11 +103,19 @@ static int fill_buffer_trng(uint8_t *buffer, trng_t *trng_obj, size_t trng_len)
102
103
static void compress_and_compare (char *key, char *value)
103
104
{
104
105
trng_t trng_obj;
105
- uint8_t out_comp_buf[(BUFFER_LEN * 5 ) + 32 ] = { 0 }, buffer[BUFFER_LEN] = { 0 } ;
106
- uint8_t input_buf[BUFFER_LEN * 4 ] = { 0 }, temp_buf[BUFFER_LEN * 2 ] = { 0 } ;
106
+ uint8_t *out_comp_buf, * buffer;
107
+ uint8_t *input_buf, *temp_buf ;
107
108
size_t comp_sz = 0 ;
108
109
unsigned int result = 0 ;
109
110
111
+ #define OUT_COMP_BUF_SIZE ((BUFFER_LEN * 5 ) + 32 )
112
+ #define TEMP_BUF_SIZE (BUFFER_LEN * 2 )
113
+
114
+ out_comp_buf = new uint8_t [OUT_COMP_BUF_SIZE];
115
+ buffer = new uint8_t [BUFFER_LEN];
116
+ input_buf = new uint8_t [BUFFER_LEN * 4 ];
117
+ temp_buf = new uint8_t [BUFFER_LEN * 2 ];
118
+
110
119
#if NVSTORE_RESET
111
120
NVStore& nvstore = NVStore::get_instance ();
112
121
#endif
@@ -115,7 +124,7 @@ static void compress_and_compare(char *key, char *value)
115
124
if (strcmp (key, MSG_TRNG_TEST_STEP2) == 0 ) {
116
125
#if NVSTORE_RESET
117
126
uint16_t actual = 0 ;
118
- result = nvstore.get (NVKEY, sizeof (buffer) , buffer, actual);
127
+ result = nvstore.get (NVKEY, BUFFER_LEN , buffer, actual);
119
128
TEST_ASSERT_EQUAL (RESULT_SUCCESS, result);
120
129
#else
121
130
/* Using base64 to decode data sent from host*/
@@ -130,105 +139,104 @@ static void compress_and_compare(char *key, char *value)
130
139
&charsProcessed);
131
140
TEST_ASSERT_EQUAL (0 , result);
132
141
#endif
133
- memcpy (input_buf, buffer, sizeof (buffer) );
142
+ memcpy (input_buf, buffer, BUFFER_LEN );
134
143
}
135
144
136
145
if (strcmp (key, MSG_TRNG_TEST_STEP1) == 0 ) {
137
146
/* Fill buffer with trng values*/
138
- result = fill_buffer_trng (buffer, &trng_obj, sizeof (buffer) );
147
+ result = fill_buffer_trng (buffer, &trng_obj, BUFFER_LEN );
139
148
TEST_ASSERT_EQUAL (0 , result);
140
- memcpy (input_buf, buffer, sizeof (buffer) );
149
+ memcpy (input_buf, buffer, BUFFER_LEN );
141
150
}
142
151
/* pithy_Compress will try to compress the random data, if it succeeded it means the data is not really random*/
143
152
else if (strcmp (key, MSG_TRNG_TEST_STEP2) == 0 ) {
144
153
145
154
comp_sz = pithy_Compress ((char *)buffer,
146
- sizeof (buffer) ,
155
+ BUFFER_LEN ,
147
156
(char *)out_comp_buf,
148
- sizeof (out_comp_buf) ,
157
+ OUT_COMP_BUF_SIZE ,
149
158
9 );
150
- TEST_ASSERT_MESSAGE (comp_sz > sizeof (buffer) ,
159
+ TEST_ASSERT_MESSAGE (comp_sz > BUFFER_LEN ,
151
160
" TRNG_TEST_STEP1: trng_get_bytes was able to compress thus not random" );
152
161
153
162
/* pithy_Compress will try to compress the random data with a different buffer sizem*/
154
- result = fill_buffer_trng (temp_buf, &trng_obj, sizeof (temp_buf) );
163
+ result = fill_buffer_trng (temp_buf, &trng_obj, TEMP_BUF_SIZE );
155
164
TEST_ASSERT_EQUAL (0 , result);
156
165
157
166
comp_sz = pithy_Compress ((char *)temp_buf,
158
- sizeof (temp_buf) ,
167
+ TEMP_BUF_SIZE ,
159
168
(char *)out_comp_buf,
160
- sizeof (out_comp_buf) ,
169
+ OUT_COMP_BUF_SIZE ,
161
170
9 );
162
- TEST_ASSERT_MESSAGE (comp_sz > sizeof (temp_buf) ,
171
+ TEST_ASSERT_MESSAGE (comp_sz > TEMP_BUF_SIZE ,
163
172
" TRNG_TEST_STEP2: trng_get_bytes was able to compress thus not random" );
164
173
165
- memcpy (input_buf + sizeof (buffer) , temp_buf, sizeof (temp_buf) );
174
+ memcpy (input_buf + BUFFER_LEN , temp_buf, TEMP_BUF_SIZE );
166
175
/* pithy_Compress will try to compress the random data from before reset concatenated with new random data*/
167
176
comp_sz = pithy_Compress ((char *)input_buf,
168
- sizeof (temp_buf) + sizeof (buffer) ,
177
+ TEMP_BUF_SIZE + BUFFER_LEN ,
169
178
(char *)out_comp_buf,
170
- sizeof (out_comp_buf) ,
179
+ OUT_COMP_BUF_SIZE ,
171
180
9 );
172
- TEST_ASSERT_MESSAGE (comp_sz > sizeof (temp_buf) + sizeof (buffer) ,
181
+ TEST_ASSERT_MESSAGE (comp_sz > TEMP_BUF_SIZE + BUFFER_LEN ,
173
182
" TRNG_TEST_STEP3: concatenated buffer after reset was able to compress thus not random" );
183
+ greentea_send_kv (MSG_TRNG_TEST_SUITE_ENDED, MSG_VALUE_DUMMY);
174
184
}
175
185
176
186
/* At the end of step 1 store trng buffer and reset the device*/
177
187
if (strcmp (key, MSG_TRNG_TEST_STEP1) == 0 ) {
178
188
int result = 0 ;
179
189
#if NVSTORE_RESET
180
- result = nvstore.set (NVKEY, sizeof (buffer) , buffer);
190
+ result = nvstore.set (NVKEY, BUFFER_LEN , buffer);
181
191
TEST_ASSERT_EQUAL (RESULT_SUCCESS, result);
182
192
#else
183
193
/* Using base64 to encode data sending from host*/
184
194
result = trng_EncodeBase64 (buffer,
185
195
BUFFER_LEN,
186
196
(char *)out_comp_buf,
187
- sizeof (out_comp_buf) );
197
+ OUT_COMP_BUF_SIZE );
188
198
TEST_ASSERT_EQUAL (RESULT_SUCCESS, result);
189
199
190
200
greentea_send_kv (MSG_TRNG_BUFFER, (const char *)out_comp_buf);
191
201
#endif
192
- system_reset ();
193
- TEST_ASSERT_MESSAGE (false , " system_reset() did not reset the device as expected." );
194
202
}
195
203
196
- return ;
204
+ delete[] out_comp_buf;
205
+ delete[] buffer;
206
+ delete[] input_buf;
207
+ delete[] temp_buf;
197
208
}
198
209
199
210
/* This method call first and second steps, it directs by the key received from the host*/
200
211
void trng_test ()
201
212
{
202
213
greentea_send_kv (MSG_TRNG_READY, MSG_VALUE_DUMMY);
203
214
204
- static char key[MSG_KEY_LEN + 1 ] = { };
205
- static char value[MSG_VALUE_LEN + 1 ] = { };
206
- memset (key, 0 , MSG_KEY_LEN + 1 );
207
- memset (value, 0 , MSG_VALUE_LEN + 1 );
215
+ char key[MSG_KEY_LEN + 1 ] = { };
216
+ char *value = new char [MSG_VALUE_LEN + 1 ];
217
+ do {
218
+ memset (key, 0 , MSG_KEY_LEN + 1 );
219
+ memset (value, 0 , MSG_VALUE_LEN + 1 );
208
220
209
- greentea_parse_kv (key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
221
+ greentea_parse_kv (key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
210
222
211
- if (strcmp (key, MSG_TRNG_TEST_STEP1) == 0 ) {
212
- /* create trng data buffer and try to compress it, store it for later checks*/
213
- compress_and_compare (key, value);
214
- return trng_test ();
215
- }
223
+ if (strcmp (key, MSG_TRNG_TEST_STEP1) == 0 ) {
224
+ /* create trng data buffer and try to compress it, store it for later checks*/
225
+ compress_and_compare (key, value);
226
+ }
216
227
217
- if (strcmp (key, MSG_TRNG_TEST_STEP2) == 0 ) {
218
- /* create another trng data buffer and concatenate it to the stored trng data buffer
219
- try to compress them both*/
220
- compress_and_compare (key, value);
221
- }
222
- }
228
+ if (strcmp (key, MSG_TRNG_TEST_STEP2) == 0 ) {
229
+ /* create another trng data buffer and concatenate it to the stored trng data buffer
230
+ try to compress them both*/
231
+ compress_and_compare (key, value);
232
+ }
233
+ } while ( strcmp (key, MSG_TRNG_EXIT) != 0 );
223
234
224
- utest::v1::status_t greentea_failure_handler (const Case *const source, const failure_t reason)
225
- {
226
- greentea_case_failure_abort_handler (source, reason);
227
- return STATUS_CONTINUE;
235
+ delete[] value;
228
236
}
229
237
230
238
Case cases[] = {
231
- Case (" TRNG: trng_test" , trng_test, greentea_failure_handler ),
239
+ Case (" TRNG: trng_test" , trng_test),
232
240
};
233
241
234
242
utest::v1::status_t greentea_test_setup (const size_t number_of_cases)
@@ -242,7 +250,6 @@ Specification specification(greentea_test_setup, cases, greentea_test_teardown_h
242
250
int main ()
243
251
{
244
252
bool ret = !Harness::run (specification);
245
- greentea_send_kv (MSG_TRNG_TEST_SUITE_ENDED, MSG_VALUE_DUMMY);
246
253
247
254
return ret;
248
255
}
0 commit comments