1 /* test_ctr_prng.c - TinyCrypt implementation of some CTR-PRNG tests */
2
3 /*
4 * Copyright (c) 2016, Chris Morrison, All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * DESCRIPTION
31 * This module tests the CTR-PRNG routines
32 */
33
34 #include <tinycrypt/ctr_prng.h>
35 #include <tinycrypt/aes.h>
36 #include <tinycrypt/constants.h>
37 #include <zephyr/test_utils.h>
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <zephyr/ztest.h>
42
43 #define MAX_EXPECTED_STRING 128
44 #define MAX_BIN_SIZE (MAX_EXPECTED_STRING / 2)
45
46 uint8_t per[MAX_BIN_SIZE];
47 uint8_t ai1[MAX_BIN_SIZE];
48 uint8_t ai2[MAX_BIN_SIZE];
49
50 struct prng_vector {
51 char *entropy;
52 char *personal; /* may be null */
53 char *extra1; /* may be null */
54 char *extra2; /* may be null */
55 char *expected;
56 };
57
58 /* vectors taken from NIST CAVS 14.3 CTR_DRBG.rsp */
59 struct prng_vector vectors[] = {
60 /*
61 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
62 * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 0,
63 * ReturnedBitsLen = 512
64 */
65 { /* Count 0 */
66 "ce50f33da5d4c1d3d4004eb35244b7f2cd7f2e5076fbf6780a7ff634b249a5fc",
67 0,
68 0,
69 0,
70 "6545c0529d372443b392ceb3ae3a99a30f963eaf313280f1d1a1e87f9db373d361e75d"
71 "18018266499cccd64d9bbb8de0185f213383080faddec46bae1f784e5a",
72 },
73
74 { /* Count 1 */
75 "a385f70a4d450321dfd18d8379ef8e7736fee5fbf0a0aea53b76696094e8aa93",
76 0,
77 0,
78 0,
79 "1a062553ab60457ed1f1c52f5aca5a3be564a27545358c112ed92c6eae2cb7597cfcc2"
80 "e0a5dd81c5bfecc941da5e8152a9010d4845170734676c8c1b6b3073a5",
81 },
82
83 /*
84 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
85 * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 256,
86 * ReturnedBitsLen = 512
87 */
88 { /* Count 0 */
89 "6bd4f2ae649fc99350951ff0c5d460c1a9214154e7384975ee54b34b7cae0704",
90 0,
91 "ecd4893b979ac92db1894ae3724518a2f78cf2dbe2f6bbc6fda596df87c7a4ae",
92 "b23e9188687c88768b26738862c4791fa52f92502e1f94bf66af017c4228a0dc",
93 "5b2bf7a5c60d8ab6591110cbd61cd387b02de19784f496d1a109123d8b3562a5de2dd6"
94 "d5d1aef957a6c4f371cecd93c15799d82e34d6a0dba7e915a27d8e65f3",
95 },
96
97 { /* Count 1 */
98 "e2addbde2a76e769fc7aa3f45b31402f482b73bbe7067ad6254621f06d3ef68b",
99 0,
100 "ad11643b019e31245e4ea41f18f7680458310580fa6efad275c5833e7f800dae",
101 "b5d849616b3123c9725d188cd0005003220768d1200f9e7cc29ef6d88afb7b9a",
102 "132d0d50c8477a400bb8935be5928f916a85da9ffcf1a8f6e9f9a14cca861036cda14c"
103 "f66d8953dab456b632cf687cd539b4b807926561d0b3562b9d3334fb61",
104 },
105
106 /*
107 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
108 * NonceLen = 0, PersonalizationStringLen = 256, AdditionalInputLen = 0,
109 * ReturnedBitsLen = 512
110 */
111 { /* Count 0 */
112 "cee23de86a69c7ef57f6e1e12bd16e35e51624226fa19597bf93ec476a44b0f2",
113 "a2ef16f226ea324f23abd59d5e3c660561c25e73638fe21c87566e86a9e04c3e",
114 0,
115 0,
116 "2a76d71b329f449c98dc08fff1d205a2fbd9e4ade120c7611c225c984eac8531288dd3"
117 "049f3dc3bb3671501ab8fbf9ad49c86cce307653bd8caf29cb0cf07764",
118 },
119
120 { /* Count 1 */
121 "b09eb4a82a39066ec945bb7c6aef6a0682a62c3e674bd900297d4271a5f25b49",
122 "a3b768adcfe76d61c972d900da8dffeeb2a42e740247aa719ed1c924d2d10bd4",
123 0,
124 0,
125 "5a1c26803f3ffd4daf32042fdcc32c3812bb5ef13bc208cef82ea047d2890a6f5dcecf"
126 "32bcc32a2585775ac5e1ffaa8de00664c54fe00a7674b985619e953c3a",
127 },
128
129 /*
130 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
131 * NonceLen = 0, PersonalizationStringLen = 256,
132 * AdditionalInputLen = 256,
133 * ReturnedBitsLen = 512
134 */
135 { /* Count 0 */
136 "50b96542a1f2b8b05074051fe8fb0e45adbbd5560e3594e12d485fe1bfcb741f",
137 "820c3030f97b3ead81a93b88b871937278fd3d711d2085d9280cba394673b17e",
138 "1f1632058806d6d8e231288f3b15a3c324e90ccef4891bd595f09c3e80e27469",
139 "5cadc8bfd86d2a5d44f921f64c7d153001b9bdd7caa6618639b948ebfad5cb8a",
140 "02b76a66f103e98d450e25e09c35337747d987471d2b3d81e03be24c7e985417a32acd"
141 "72bc0a6eddd9871410dacb921c659249b4e2b368c4ac8580fb5db559bc",
142 },
143
144 { /* Count 1 */
145 "ff5f4b754e8b364f6df0c5effba5f1c036de49c4b38cd8d230ee1f14d7234ef5",
146 "994eb339f64034005d2e18352899e77df446e285c3430631d557498aac4f4280",
147 "e1824832d5fc2a6dea544cac2ab73306d6566bde98cc8f9425d064b860a9b218",
148 "c08b42433a78fd393a34ffc24724d479af08c36882799c134165d98b2866dc0a",
149 "1efa34aed07dd57bde9741b8d1907d28e8c1ac71601df37ef4295e6ffb67f6a1c4c13e"
150 "5def65d505e2408aeb82948999ca1f9c9113b99a6b59ff7f0cc3dc6e92",
151 },
152
153 /*
154 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
155 * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 0,
156 * ReturnedBitsLen = 512
157 */
158 { /* Count 0 */
159 "69a09f6bf5dda15cd4af29e14cf5e0cddd7d07ac39bba587f8bc331104f9c448",
160 0,
161 0,
162 0,
163 "f78a4919a6ec899f7b6c69381febbbe083315f3d289e70346db0e4ec4360473ae0b3d9"
164 "16e9b6b964309f753ed66ae59de48da316cc1944bc8dfd0e2575d0ff6d",
165 },
166
167 { /* Count 1 */
168 "80bfbd340d79888f34f043ed6807a9f28b72b6644d9d9e9d777109482b80788a",
169 0,
170 0,
171 0,
172 "80db048d2f130d864b19bfc547c92503e580cb1a8e1f74f3d97fdda6501fb1aa81fced"
173 "ac0dd18b6ccfdc183ca28a44fc9f3a08834ba8751a2f4495367c54a185",
174 },
175
176 /*
177 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
178 * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 256,
179 * ReturnedBitsLen = 512
180 */
181 { /* Count 0 */
182 "7f40804693552e317523fda6935a5bc814353b1fbb7d334964ac4d1d12ddccce",
183 0,
184 "95c04259f64fcd1fe00c183aa3fb76b8a73b4d1243b800d770e38515bc41143c",
185 "5523102dbd7fe1228436b91a765b165ae6405eb0236e237afad4759cf0888941",
186 "1abf6bccb4c2d64e5187b1e2e34e493eca204ee4eef0d964267e38228f5f20efba3764"
187 "30a266f3832916d0a45b2703f46401dfd145e447a0a1667ebd8b6ee748",
188 },
189
190 { /* Count 1 */
191 "350df677409a1dc297d01d3716a2abdfa6272cd030ab75f76839648582b47113",
192 0,
193 "ba5709a12ae6634a5436b7ea06838b48f7b847a237f6654a0e27c776ebee9511",
194 "f1b2c717c5e3a934127e10471d67accc65f4a45010ca53b35f54c88833dbd8e7",
195 "1ef1ea279812e8abe54f7ffd12d04c80ae40741f4ccfe232a5fba3a78dfd3e2ed419b8"
196 "8ee9188df724160cbb3aea0f276e84a3c0ff01e3b89fe30ebcfa64cb86",
197 },
198
199 /*
200 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
201 * NonceLen = 0, PersonalizationStringLen = 256, AdditionalInputLen = 0,
202 * ReturnedBitsLen = 512
203 */
204 { /* Count 0 */
205 "3fef762f0aa0677f61c65d749eeb10b013ff68ccc6314f150cfee752dcd8f987",
206 "f56db099240c7590dac396372b8737404d418b2864a3df96a8a397967245735f",
207 0,
208 0,
209 "af0afe0837442136fbb1959a1c91a9291c1d8188ede07c67d0e4dd6541303415e7a679"
210 "99c302ba0df555324c26077514592a9b6db6be2f153fad2250161164e4",
211 },
212
213 { /* Count 1 */
214 "3eebe77db4631862e3eb7e39370515b8baa1cdd71a5b1b0cda79c14d0b5f48ea",
215 "4be56a9b9c21242739c985ef12aa4d98e8c7da07c4c1dc6829f2e06833cfa148",
216 0,
217 0,
218 "be9e18a753df261927473c8bb5fb7c3ea6e821df5ab49adc566a4ebf44f75fa825b1f9"
219 "d8c154bcd469134c0bb688e07e3c3e45407ca350d540e1528cc2e64068",
220 },
221
222 /*
223 * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
224 * NonceLen = 0, PersonalizationStringLen = 256,
225 * AdditionalInputLen = 256,
226 * ReturnedBitsLen = 512
227 */
228 { /* Count 0 */
229 "c129c2732003bbf1d1dec244a933cd04cb47199bbce98fe080a1be880afb2155",
230 "64e2b9ac5c20642e3e3ee454b7463861a7e93e0dd1bbf8c4a0c28a6cb3d811ba",
231 "f94f0975760d52f47bd490d1623a9907e4df701f601cf2d573aba803a29d2b51",
232 "6f99720b186e2028a5fcc586b3ea518458e437ff449c7c5a318e6d13f75b5db7",
233 "7b8b3378b9031ab3101cec8af5b8ba5a9ca2a9af41432cd5f2e5e19716140bb219ed7f"
234 "4ba88fc37b2d7e146037d2cac1128ffe14131c8691e581067a29cacf80",
235 },
236
237 { /* Count 1 */
238 "7667643670254b3530e80a17b16b22406e84efa6a4b5ceef3ebc877495fc6048",
239 "40b92969953acde756747005117e46eff6893d7132a8311ffb1062280367326b",
240 "797a02ffbe8ff2c94ed0e5d39ebdc7847adaa762a88238242ed8f71f5635b194",
241 "d617f0f0e609e90d814192ba2e5214293d485402cdf9f789cc78b05e8c374f18",
242 "e8d6f89dca9825aed8927b43187492a98ca8648db30f0ac709556d401a8ac2b959c813"
243 "50fc64332c4c0deb559a286a72e65dbb462bd872f9b28c0728f353dc10",
244 }
245 };
246
247 /* utility function to convert hex character representation
248 * to their nibble (4 bit) values
249 */
char_to_nibble(char c)250 static uint8_t char_to_nibble(char c)
251 {
252 if (c >= '0' && c <= '9') {
253 return c - '0';
254 }
255 if (c >= 'a' && c <= 'f') {
256 return c - 'a' + 10U;
257 }
258 if (c >= 'A' && c <= 'F') {
259 return c - 'A' + 10U;
260 }
261 return 255U;
262 }
263
264 /*
265 * Convert a string of characters representing a hex buffer into a series of
266 * bytes of that real value
267 */
hex_str_to_num(uint8_t * buf,char * in)268 static void hex_str_to_num(uint8_t *buf, char *in)
269 {
270 int len;
271 int i;
272
273 len = strlen(in) / 2;
274 for (i = 0; i < len; i++) {
275 buf[i] = (char_to_nibble(*in) << 4) | char_to_nibble(*(in+1));
276 in += 2;
277 }
278 }
279
test_prng_vector(struct prng_vector * v)280 static int test_prng_vector(struct prng_vector *v)
281 {
282 TCCtrPrng_t ctx;
283
284 uint8_t entropy[MAX_BIN_SIZE];
285 uint8_t expected[MAX_BIN_SIZE];
286 uint8_t output[MAX_BIN_SIZE];
287 uint8_t *personal = NULL;
288 uint8_t *extra1 = NULL;
289 uint8_t *extra2 = NULL;
290
291 int extra1_len = 0;
292 int extra2_len = 0;
293 int plen = 0;
294 int ent_len;
295 int exp_len;
296 int rc;
297
298 hex_str_to_num(entropy, v->entropy);
299 hex_str_to_num(expected, v->expected);
300
301 ent_len = strlen(v->entropy) / 2;
302 exp_len = strlen(v->expected) / 2;
303
304 if (v->personal != 0) {
305 personal = per;
306 hex_str_to_num(personal, v->personal);
307 plen = strlen(v->personal) / 2;
308 }
309
310 if (v->extra1 != 0) {
311 extra1 = ai1;
312 hex_str_to_num(extra1, v->extra1);
313 extra1_len = strlen(v->extra1) / 2;
314 }
315
316 if (v->extra2 != 0) {
317 extra2 = ai2;
318 hex_str_to_num(extra2, v->extra2);
319 extra2_len = strlen(v->extra2) / 2;
320 }
321
322 rc = tc_ctr_prng_init(&ctx, entropy, ent_len, personal, plen);
323
324 /**TESTPOINT: Check if init works*/
325 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG init failed");
326
327 rc = tc_ctr_prng_generate(&ctx, extra1, extra1_len, output, exp_len);
328
329 /**TESTPOINT: Check if generate works*/
330 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG generate failed");
331
332 rc = tc_ctr_prng_generate(&ctx, extra2, extra2_len, output, exp_len);
333
334 /**TESTPOINT: Check if generate works*/
335 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG generate failed");
336
337 rc = memcmp(output, expected, exp_len);
338
339 /**TESTPOINT: Check results*/
340 zassert_false(rc, "expected value different - check failed");
341
342 rc = TC_PASS;
343 return rc;
344 }
345
ZTEST(tinycrypt,test_ctr_prng_reseed)346 ZTEST(tinycrypt, test_ctr_prng_reseed)
347 {
348 uint8_t expectedV1[] = {0x7E, 0xE3, 0xA0, 0xCB, 0x6D, 0x5C, 0x4B, 0xC2,
349 0x4B, 0x7E, 0x3C, 0x48, 0x88, 0xC3, 0x69, 0x70};
350 uint8_t expectedV2[] = {0x5E, 0xC1, 0x84, 0xED, 0x45, 0x76, 0x67, 0xEC,
351 0x7B, 0x4C, 0x08, 0x7E, 0xB0, 0xF9, 0x55, 0x4E};
352 uint8_t extra_input[32] = {0};
353 uint8_t entropy[32] = {0}; /* value not important */
354 uint8_t output[32];
355 TCCtrPrng_t ctx;
356 uint32_t i;
357 int rc;
358
359 rc = tc_ctr_prng_init(&ctx, entropy, sizeof(entropy), 0, 0U);
360
361 /**TESTPOINT: Check if init works*/
362 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG init failed");
363
364 /* force internal state to max allowed count */
365 ctx.reseedCount = 0x1000000000000ULL;
366
367 rc = tc_ctr_prng_generate(&ctx, 0, 0, output, sizeof(output));
368
369 /**TESTPOINT: Check if generate works*/
370 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG generate failed");
371
372 /* expect further attempts to fail due to reaching reseed threshold */
373 rc = tc_ctr_prng_generate(&ctx, 0, 0, output, sizeof(output));
374
375 /**TESTPOINT: Check if generate works*/
376 zassert_equal(rc, TC_CTR_PRNG_RESEED_REQ, "CTR PRNG generate failed");
377
378 /* reseed and confirm generate works again
379 * make entropy different from original value - not really important
380 * for the purpose of this test
381 */
382 (void)memset(entropy, 0xFF, sizeof(entropy));
383 rc = tc_ctr_prng_reseed(&ctx, entropy, sizeof(entropy), extra_input,
384 sizeof(extra_input));
385
386 /**TESTPOINT: Recheck if the functions work*/
387 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG reseed failed");
388
389 rc = tc_ctr_prng_generate(&ctx, 0, 0, output, sizeof(output));
390
391 /**TESTPOINT: Check if generate works again*/
392 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG generate failed");
393
394 /* confirm entropy and additional_input are being used correctly
395 * first, entropy only
396 */
397 (void)memset(&ctx, 0x0, sizeof(ctx));
398 for (i = 0U; i < sizeof(entropy); i++) {
399 entropy[i] = i;
400 }
401
402 rc = tc_ctr_prng_reseed(&ctx, entropy, sizeof(entropy), 0, 0);
403
404 /**TESTPOINT: Check if reseed works*/
405 zassert_equal(rc, 1, "CTR PRNG reseed failed");
406
407 /**TESTPOINT: Check results*/
408 zassert_false(memcmp(ctx.V, expectedV1, sizeof(expectedV1)),
409 "expected value different - check failed");
410
411 /* now, entropy and additional_input */
412 (void)memset(&ctx, 0x00, sizeof(ctx));
413 for (i = 0U; i < sizeof(extra_input); i++) {
414 extra_input[i] = i * 2U;
415 }
416
417 rc = tc_ctr_prng_reseed(&ctx, entropy, sizeof(entropy),
418 extra_input, sizeof(extra_input));
419
420 /**TESTPOINT: Check if reseed works*/
421 zassert_equal(rc, 1, "CTR PRNG reseed failed");
422
423 /**TESTPOINT: Check results*/
424 zassert_false(memcmp(ctx.V, expectedV2, sizeof(expectedV2)),
425 "expected value different - check failed");
426
427 TC_PRINT("CTR PRNG reseed test succeeded\n");
428 }
429
ZTEST(tinycrypt,test_ctr_prng_uninstantiate)430 ZTEST(tinycrypt, test_ctr_prng_uninstantiate)
431 {
432 uint8_t entropy[32] = {0}; /* value not important */
433 TCCtrPrng_t ctx;
434 size_t words;
435 size_t i;
436 int rc;
437
438 rc = tc_ctr_prng_init(&ctx, entropy, sizeof(entropy), 0, 0);
439
440 /**TESTPOINT: Check if init works*/
441 zassert_equal(rc, TC_CRYPTO_SUCCESS, "CTR PRNG init failed");
442
443 tc_ctr_prng_uninstantiate(&ctx);
444 /* show that state has been zeroised */
445 for (i = 0; i < sizeof(ctx.V); i++) {
446
447 /**TESTPOINT: Check if states have been zeroised*/
448 zassert_false(ctx.V[i], "some states have not been zeroised");
449 }
450
451 words = sizeof(ctx.key.words) / sizeof(ctx.key.words[0]);
452 for (i = 0; i < words; i++) {
453
454 /**TESTPOINT: Check words*/
455 zassert_false(ctx.key.words[i],
456 "expected value wrong - check failed");
457 }
458
459 /**TESTPOINT: Check if uninstantiation passed*/
460 zassert_false(ctx.reseedCount, "CTR PRNG uninstantiate test failed");
461
462 TC_PRINT("CTR PRNG uninstantiate test succeeded\n");
463 }
464
ZTEST(tinycrypt,test_ctr_prng_robustness)465 ZTEST(tinycrypt, test_ctr_prng_robustness)
466 {
467 uint8_t entropy[32] = {0}; /* value not important */
468 uint8_t output[32];
469 TCCtrPrng_t ctx;
470 int rc;
471
472 /* show that the CTR PRNG is robust to invalid inputs */
473 tc_ctr_prng_uninstantiate(0);
474
475 rc = tc_ctr_prng_generate(&ctx, 0, 0, 0, 0);
476
477 /**TESTPOINT: Check if invalid input test works*/
478 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
479
480 rc = tc_ctr_prng_generate(0, 0, 0, output, sizeof(output));
481
482 /**TESTPOINT: Check if invalid input test works*/
483 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
484
485 rc = tc_ctr_prng_generate(0, 0, 0, 0, 0);
486
487 /**TESTPOINT: Check if invalid input test works*/
488 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
489
490 rc = tc_ctr_prng_reseed(&ctx, 0, 0, 0, 0);
491
492 /**TESTPOINT: Check if invalid input test works*/
493 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
494
495 /* too little entropy */
496 rc = tc_ctr_prng_reseed(&ctx, entropy, sizeof(entropy) - 1, 0, 0);
497
498 /**TESTPOINT: Check if invalid input test works*/
499 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
500
501
502 rc = tc_ctr_prng_reseed(0, entropy, sizeof(entropy), 0, 0);
503
504 /**TESTPOINT: Check if invalid input test works*/
505 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
506
507
508 rc = tc_ctr_prng_reseed(0, 0, 0, 0, 0);
509
510 /**TESTPOINT: Check if invalid input test works*/
511 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
512
513
514 rc = tc_ctr_prng_init(&ctx, 0, 0, 0, 0);
515
516 /**TESTPOINT: Check if invalid input test works*/
517 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
518
519 /* too little entropy */
520 rc = tc_ctr_prng_init(&ctx, entropy, sizeof(entropy) - 1, 0, 0);
521
522 /**TESTPOINT: Check if invalid input test works*/
523 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
524
525 rc = tc_ctr_prng_init(0, entropy, sizeof(entropy), 0, 0);
526
527 /**TESTPOINT: Check if invalid input test works*/
528 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
529
530 rc = tc_ctr_prng_init(0, 0, 0, 0, 0);
531
532 /**TESTPOINT: Check if invalid input test works*/
533 zassert_equal(rc, TC_CRYPTO_FAIL, "CTR PRNG invalid input test failed");
534
535 TC_PRINT("CTR PRNG robustness test succeeded\n");
536 }
537
538 /*
539 * Main task to test CTR PRNG
540 */
ZTEST(tinycrypt,test_ctr_prng_vector)541 ZTEST(tinycrypt, test_ctr_prng_vector)
542 {
543 int elements;
544 int rc;
545 int i;
546
547
548 elements = (int)sizeof(vectors) / sizeof(vectors[0]);
549 for (i = 0; i < elements; i++) {
550 rc = test_prng_vector(&vectors[i]);
551 TC_PRINT("[%s] test_prng_vector #%d\n",
552 TC_RESULT_TO_STR(rc), i);
553
554 /**TESTPOINT: Check if test passed*/
555 zassert_false(rc, "CTR PRNG vector test failed");
556 }
557 TC_PRINT("CTR PRNG vector test succeeded\n");
558 }
559