test_ctr_prng.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* test_ctr_prng.c - TinyCrypt implementation of some CTR-PRNG tests */
  2. /*
  3. * Copyright (c) 2016, Chris Morrison, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. DESCRIPTION
  29. This module tests the CTR-PRNG routines
  30. */
  31. #include <tinycrypt/ctr_prng.h>
  32. #include <tinycrypt/aes.h>
  33. #include <tinycrypt/constants.h>
  34. #include <test_utils.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. /* utility function to convert hex character representation to their nibble (4 bit) values */
  39. static uint8_t nibbleFromChar(char c)
  40. {
  41. if(c >= '0' && c <= '9') return c - '0';
  42. if(c >= 'a' && c <= 'f') return c - 'a' + 10U;
  43. if(c >= 'A' && c <= 'F') return c - 'A' + 10U;
  44. return 255U;
  45. }
  46. /*
  47. * Convert a string of characters representing a hex buffer into a series of
  48. * bytes of that real value
  49. */
  50. uint8_t *hexStringToBytes(char *inhex)
  51. {
  52. uint8_t *retval;
  53. uint8_t *p;
  54. int len, i;
  55. len = strlen(inhex) / 2;
  56. retval = (uint8_t *)malloc(len+1);
  57. for(i=0, p = (uint8_t *) inhex; i<len; i++) {
  58. retval[i] = (nibbleFromChar(*p) << 4) | nibbleFromChar(*(p+1));
  59. p += 2;
  60. }
  61. retval[len] = 0;
  62. return retval;
  63. }
  64. typedef struct {
  65. char * entropyString;
  66. char * personalizationString; /* may be null */
  67. char * additionalInputString1; /* may be null */
  68. char * additionalInputString2; /* may be null */
  69. char * expectedString;
  70. } PRNG_Vector;
  71. /* vectors taken from NIST CAVS 14.3 CTR_DRBG.rsp */
  72. PRNG_Vector vectors[] = {
  73. /*
  74. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  75. * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 0,
  76. * ReturnedBitsLen = 512
  77. */
  78. { /* Count 0 */
  79. "ce50f33da5d4c1d3d4004eb35244b7f2cd7f2e5076fbf6780a7ff634b249a5fc",
  80. 0,
  81. 0,
  82. 0,
  83. "6545c0529d372443b392ceb3ae3a99a30f963eaf313280f1d1a1e87f9db373d361e75d18018266499cccd64d9bbb8de0185f213383080faddec46bae1f784e5a",
  84. },
  85. { /* Count 1 */
  86. "a385f70a4d450321dfd18d8379ef8e7736fee5fbf0a0aea53b76696094e8aa93",
  87. 0,
  88. 0,
  89. 0,
  90. "1a062553ab60457ed1f1c52f5aca5a3be564a27545358c112ed92c6eae2cb7597cfcc2e0a5dd81c5bfecc941da5e8152a9010d4845170734676c8c1b6b3073a5",
  91. },
  92. /*
  93. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  94. * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 256,
  95. * ReturnedBitsLen = 512
  96. */
  97. { /* Count 0 */
  98. "6bd4f2ae649fc99350951ff0c5d460c1a9214154e7384975ee54b34b7cae0704",
  99. 0,
  100. "ecd4893b979ac92db1894ae3724518a2f78cf2dbe2f6bbc6fda596df87c7a4ae",
  101. "b23e9188687c88768b26738862c4791fa52f92502e1f94bf66af017c4228a0dc",
  102. "5b2bf7a5c60d8ab6591110cbd61cd387b02de19784f496d1a109123d8b3562a5de2dd6d5d1aef957a6c4f371cecd93c15799d82e34d6a0dba7e915a27d8e65f3",
  103. },
  104. { /* Count 1 */
  105. "e2addbde2a76e769fc7aa3f45b31402f482b73bbe7067ad6254621f06d3ef68b",
  106. 0,
  107. "ad11643b019e31245e4ea41f18f7680458310580fa6efad275c5833e7f800dae",
  108. "b5d849616b3123c9725d188cd0005003220768d1200f9e7cc29ef6d88afb7b9a",
  109. "132d0d50c8477a400bb8935be5928f916a85da9ffcf1a8f6e9f9a14cca861036cda14cf66d8953dab456b632cf687cd539b4b807926561d0b3562b9d3334fb61",
  110. },
  111. /*
  112. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  113. * NonceLen = 0, PersonalizationStringLen = 256, AdditionalInputLen = 0,
  114. * ReturnedBitsLen = 512
  115. */
  116. { /* Count 0 */
  117. "cee23de86a69c7ef57f6e1e12bd16e35e51624226fa19597bf93ec476a44b0f2",
  118. "a2ef16f226ea324f23abd59d5e3c660561c25e73638fe21c87566e86a9e04c3e",
  119. 0,
  120. 0,
  121. "2a76d71b329f449c98dc08fff1d205a2fbd9e4ade120c7611c225c984eac8531288dd3049f3dc3bb3671501ab8fbf9ad49c86cce307653bd8caf29cb0cf07764",
  122. },
  123. { /* Count 1 */
  124. "b09eb4a82a39066ec945bb7c6aef6a0682a62c3e674bd900297d4271a5f25b49",
  125. "a3b768adcfe76d61c972d900da8dffeeb2a42e740247aa719ed1c924d2d10bd4",
  126. 0,
  127. 0,
  128. "5a1c26803f3ffd4daf32042fdcc32c3812bb5ef13bc208cef82ea047d2890a6f5dcecf32bcc32a2585775ac5e1ffaa8de00664c54fe00a7674b985619e953c3a",
  129. },
  130. /*
  131. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  132. * NonceLen = 0, PersonalizationStringLen = 256, AdditionalInputLen = 256,
  133. * ReturnedBitsLen = 512
  134. */
  135. { /* Count 0 */
  136. "50b96542a1f2b8b05074051fe8fb0e45adbbd5560e3594e12d485fe1bfcb741f",
  137. "820c3030f97b3ead81a93b88b871937278fd3d711d2085d9280cba394673b17e",
  138. "1f1632058806d6d8e231288f3b15a3c324e90ccef4891bd595f09c3e80e27469",
  139. "5cadc8bfd86d2a5d44f921f64c7d153001b9bdd7caa6618639b948ebfad5cb8a",
  140. "02b76a66f103e98d450e25e09c35337747d987471d2b3d81e03be24c7e985417a32acd72bc0a6eddd9871410dacb921c659249b4e2b368c4ac8580fb5db559bc",
  141. },
  142. { /* Count 1 */
  143. "ff5f4b754e8b364f6df0c5effba5f1c036de49c4b38cd8d230ee1f14d7234ef5",
  144. "994eb339f64034005d2e18352899e77df446e285c3430631d557498aac4f4280",
  145. "e1824832d5fc2a6dea544cac2ab73306d6566bde98cc8f9425d064b860a9b218",
  146. "c08b42433a78fd393a34ffc24724d479af08c36882799c134165d98b2866dc0a",
  147. "1efa34aed07dd57bde9741b8d1907d28e8c1ac71601df37ef4295e6ffb67f6a1c4c13e5def65d505e2408aeb82948999ca1f9c9113b99a6b59ff7f0cc3dc6e92",
  148. },
  149. /*
  150. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  151. * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 0,
  152. * ReturnedBitsLen = 512
  153. */
  154. { /* Count 0 */
  155. "69a09f6bf5dda15cd4af29e14cf5e0cddd7d07ac39bba587f8bc331104f9c448",
  156. 0,
  157. 0,
  158. 0,
  159. "f78a4919a6ec899f7b6c69381febbbe083315f3d289e70346db0e4ec4360473ae0b3d916e9b6b964309f753ed66ae59de48da316cc1944bc8dfd0e2575d0ff6d",
  160. },
  161. { /* Count 1 */
  162. "80bfbd340d79888f34f043ed6807a9f28b72b6644d9d9e9d777109482b80788a",
  163. 0,
  164. 0,
  165. 0,
  166. "80db048d2f130d864b19bfc547c92503e580cb1a8e1f74f3d97fdda6501fb1aa81fcedac0dd18b6ccfdc183ca28a44fc9f3a08834ba8751a2f4495367c54a185",
  167. },
  168. /*
  169. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  170. * NonceLen = 0, PersonalizationStringLen = 0, AdditionalInputLen = 256,
  171. * ReturnedBitsLen = 512
  172. */
  173. { /* Count 0 */
  174. "7f40804693552e317523fda6935a5bc814353b1fbb7d334964ac4d1d12ddccce",
  175. 0,
  176. "95c04259f64fcd1fe00c183aa3fb76b8a73b4d1243b800d770e38515bc41143c",
  177. "5523102dbd7fe1228436b91a765b165ae6405eb0236e237afad4759cf0888941",
  178. "1abf6bccb4c2d64e5187b1e2e34e493eca204ee4eef0d964267e38228f5f20efba376430a266f3832916d0a45b2703f46401dfd145e447a0a1667ebd8b6ee748",
  179. },
  180. { /* Count 1 */
  181. "350df677409a1dc297d01d3716a2abdfa6272cd030ab75f76839648582b47113",
  182. 0,
  183. "ba5709a12ae6634a5436b7ea06838b48f7b847a237f6654a0e27c776ebee9511",
  184. "f1b2c717c5e3a934127e10471d67accc65f4a45010ca53b35f54c88833dbd8e7",
  185. "1ef1ea279812e8abe54f7ffd12d04c80ae40741f4ccfe232a5fba3a78dfd3e2ed419b88ee9188df724160cbb3aea0f276e84a3c0ff01e3b89fe30ebcfa64cb86",
  186. },
  187. /*
  188. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  189. * NonceLen = 0, PersonalizationStringLen = 256, AdditionalInputLen = 0,
  190. * ReturnedBitsLen = 512
  191. */
  192. { /* Count 0 */
  193. "3fef762f0aa0677f61c65d749eeb10b013ff68ccc6314f150cfee752dcd8f987",
  194. "f56db099240c7590dac396372b8737404d418b2864a3df96a8a397967245735f",
  195. 0,
  196. 0,
  197. "af0afe0837442136fbb1959a1c91a9291c1d8188ede07c67d0e4dd6541303415e7a67999c302ba0df555324c26077514592a9b6db6be2f153fad2250161164e4",
  198. },
  199. { /* Count 1 */
  200. "3eebe77db4631862e3eb7e39370515b8baa1cdd71a5b1b0cda79c14d0b5f48ea",
  201. "4be56a9b9c21242739c985ef12aa4d98e8c7da07c4c1dc6829f2e06833cfa148",
  202. 0,
  203. 0,
  204. "be9e18a753df261927473c8bb5fb7c3ea6e821df5ab49adc566a4ebf44f75fa825b1f9d8c154bcd469134c0bb688e07e3c3e45407ca350d540e1528cc2e64068",
  205. },
  206. /*
  207. * AES-128 no df, PredictionResistance = False, EntropyInputLen = 256,
  208. * NonceLen = 0, PersonalizationStringLen = 256, AdditionalInputLen = 256,
  209. * ReturnedBitsLen = 512
  210. */
  211. { /* Count 0 */
  212. "c129c2732003bbf1d1dec244a933cd04cb47199bbce98fe080a1be880afb2155",
  213. "64e2b9ac5c20642e3e3ee454b7463861a7e93e0dd1bbf8c4a0c28a6cb3d811ba",
  214. "f94f0975760d52f47bd490d1623a9907e4df701f601cf2d573aba803a29d2b51",
  215. "6f99720b186e2028a5fcc586b3ea518458e437ff449c7c5a318e6d13f75b5db7",
  216. "7b8b3378b9031ab3101cec8af5b8ba5a9ca2a9af41432cd5f2e5e19716140bb219ed7f4ba88fc37b2d7e146037d2cac1128ffe14131c8691e581067a29cacf80",
  217. },
  218. { /* Count 1 */
  219. "7667643670254b3530e80a17b16b22406e84efa6a4b5ceef3ebc877495fc6048",
  220. "40b92969953acde756747005117e46eff6893d7132a8311ffb1062280367326b",
  221. "797a02ffbe8ff2c94ed0e5d39ebdc7847adaa762a88238242ed8f71f5635b194",
  222. "d617f0f0e609e90d814192ba2e5214293d485402cdf9f789cc78b05e8c374f18",
  223. "e8d6f89dca9825aed8927b43187492a98ca8648db30f0ac709556d401a8ac2b959c81350fc64332c4c0deb559a286a72e65dbb462bd872f9b28c0728f353dc10",
  224. }
  225. };
  226. static unsigned int executePRNG_TestVector(PRNG_Vector vector, unsigned int idx)
  227. {
  228. unsigned int result = TC_PASS;
  229. uint8_t * entropy = hexStringToBytes(vector.entropyString);
  230. unsigned int entropylen = strlen(vector.entropyString) / 2U;
  231. uint8_t * expected = hexStringToBytes(vector.expectedString);
  232. unsigned int expectedlen = strlen(vector.expectedString) / 2U;
  233. uint8_t * personalization = 0;
  234. unsigned int plen = 0U;
  235. uint8_t * additional_input1 = 0;
  236. unsigned int additionallen1 = 0U;
  237. uint8_t * additional_input2 = 0;
  238. unsigned int additionallen2 = 0U;
  239. uint8_t * output = (uint8_t *)malloc(expectedlen);
  240. unsigned int i;
  241. TCCtrPrng_t ctx;
  242. if (0 != vector.personalizationString) {
  243. personalization = hexStringToBytes(vector.personalizationString);
  244. plen = strlen(vector.personalizationString) / 2U;
  245. }
  246. if (0 != vector.additionalInputString1) {
  247. additional_input1 = hexStringToBytes(vector.additionalInputString1);
  248. additionallen1 = strlen(vector.additionalInputString1) / 2U;
  249. }
  250. if (0 != vector.additionalInputString2) {
  251. additional_input2 = hexStringToBytes(vector.additionalInputString2);
  252. additionallen2 = strlen(vector.additionalInputString2) / 2U;
  253. }
  254. (void)tc_ctr_prng_init(&ctx, entropy, entropylen, personalization, plen);
  255. (void)tc_ctr_prng_generate(&ctx, additional_input1, additionallen1, output, expectedlen);
  256. (void)tc_ctr_prng_generate(&ctx, additional_input2, additionallen2, output, expectedlen);
  257. for (i = 0U; i < expectedlen; i++) {
  258. if (output[i] != expected[i]) {
  259. TC_ERROR("CTR PRNG test #%d failed\n", idx);
  260. result = TC_FAIL;
  261. break;
  262. }
  263. }
  264. free(entropy);
  265. free(expected);
  266. free(personalization);
  267. free(additional_input1);
  268. free(additional_input2);
  269. free(output);
  270. return result;
  271. }
  272. static int test_reseed(void)
  273. {
  274. int result = TC_PASS;
  275. uint8_t entropy[32U] = {0U}; /* value not important */
  276. uint8_t additional_input[32] = {0U};
  277. uint8_t output[32];
  278. TCCtrPrng_t ctx;
  279. int ret;
  280. unsigned int i;
  281. (void)tc_ctr_prng_init(&ctx, entropy, sizeof entropy, 0, 0U);
  282. /* force internal state to max allowed count */
  283. ctx.reseedCount = 0x1000000000000ULL;
  284. ret = tc_ctr_prng_generate(&ctx, 0, 0, output, sizeof output);
  285. if (1 != ret) {
  286. result = TC_FAIL;
  287. goto exitTest;
  288. }
  289. /* expect further attempts to fail due to reaching reseed threshold */
  290. ret = tc_ctr_prng_generate(&ctx, 0, 0, output, sizeof output);
  291. if (-1 != ret) {
  292. result = TC_FAIL;
  293. goto exitTest;
  294. }
  295. /* reseed and confirm generate works again */
  296. /* make entropy different from original value - not really important for the purpose of this test */
  297. memset(entropy, 0xFF, sizeof entropy);
  298. ret = tc_ctr_prng_reseed(&ctx, entropy, sizeof entropy, additional_input, sizeof additional_input);
  299. if (1 != ret) {
  300. result = TC_FAIL;
  301. goto exitTest;
  302. }
  303. ret = tc_ctr_prng_generate(&ctx, 0, 0, output, sizeof output);
  304. if (1 != ret) {
  305. result = TC_FAIL;
  306. goto exitTest;
  307. }
  308. /* confirm entropy and additional_input are being used correctly */
  309. /* first, entropy only */
  310. memset(&ctx, 0x0, sizeof ctx);
  311. for (i = 0U; i < sizeof entropy; i++) {
  312. entropy[i] = i;
  313. }
  314. ret = tc_ctr_prng_reseed(&ctx, entropy, sizeof entropy, 0, 0U);
  315. if (1 != ret) {
  316. result = TC_FAIL;
  317. goto exitTest;
  318. }
  319. {
  320. uint8_t expectedV[] =
  321. {0x7EU, 0xE3U, 0xA0U, 0xCBU, 0x6DU, 0x5CU, 0x4BU, 0xC2U,
  322. 0x4BU, 0x7EU, 0x3CU, 0x48U, 0x88U, 0xC3U, 0x69U, 0x70U};
  323. for (i = 0U; i < sizeof expectedV; i++) {
  324. if (ctx.V[i] != expectedV[i]) {
  325. result = TC_FAIL;
  326. goto exitTest;
  327. }
  328. }
  329. }
  330. /* now, entropy and additional_input */
  331. memset(&ctx, 0x0, sizeof ctx);
  332. for (i = 0U; i < sizeof additional_input; i++) {
  333. additional_input[i] = i * 2U;
  334. }
  335. ret = tc_ctr_prng_reseed(&ctx, entropy, sizeof entropy, additional_input, sizeof additional_input);
  336. if (1 != ret) {
  337. result = TC_FAIL;
  338. goto exitTest;
  339. }
  340. {
  341. uint8_t expectedV[] =
  342. {0x5EU, 0xC1U, 0x84U, 0xEDU, 0x45U, 0x76U, 0x67U, 0xECU,
  343. 0x7BU, 0x4CU, 0x08U, 0x7EU, 0xB0U, 0xF9U, 0x55U, 0x4EU};
  344. for (i = 0U; i < sizeof expectedV; i++) {
  345. if (ctx.V[i] != expectedV[i]) {
  346. result = TC_FAIL;
  347. goto exitTest;
  348. }
  349. }
  350. }
  351. exitTest:
  352. if (TC_FAIL == result) {
  353. TC_ERROR("CTR PRNG reseed tests failed\n");
  354. }
  355. return result;
  356. }
  357. static int test_uninstantiate(void)
  358. {
  359. unsigned int i;
  360. int result = TC_PASS;
  361. uint8_t entropy[32U] = {0U}; /* value not important */
  362. TCCtrPrng_t ctx;
  363. (void)tc_ctr_prng_init(&ctx, entropy, sizeof entropy, 0, 0U);
  364. tc_ctr_prng_uninstantiate(&ctx);
  365. /* show that state has been zeroised */
  366. for (i = 0U; i < sizeof ctx.V; i++) {
  367. if (0U != ctx.V[i]) {
  368. TC_ERROR("CTR PRNG uninstantiate tests failed\n");
  369. result = TC_FAIL;
  370. break;
  371. }
  372. }
  373. for (i = 0U; i < sizeof ctx.key.words / sizeof ctx.key.words[0]; i++) {
  374. if (0U != ctx.key.words[i]) {
  375. TC_ERROR("CTR PRNG uninstantiate tests failed\n");
  376. result = TC_FAIL;
  377. break;
  378. }
  379. }
  380. if (0U != ctx.reseedCount) {
  381. TC_ERROR("CTR PRNG uninstantiate tests failed\n");
  382. result = TC_FAIL;
  383. }
  384. return result;
  385. }
  386. static int test_robustness(void)
  387. {
  388. int result = TC_PASS;
  389. int ret;
  390. uint8_t entropy[32U] = {0U}; /* value not important */
  391. uint8_t output[32];
  392. TCCtrPrng_t ctx;
  393. /* show that the CTR PRNG is robust to invalid inputs */
  394. tc_ctr_prng_uninstantiate(0);
  395. ret = tc_ctr_prng_generate(&ctx, 0, 0, 0, 0);
  396. if (0 != ret) {
  397. result = TC_FAIL;
  398. goto exitTest;
  399. }
  400. ret = tc_ctr_prng_generate(0, 0, 0, output, sizeof output);
  401. if (0 != ret) {
  402. result = TC_FAIL;
  403. goto exitTest;
  404. }
  405. ret = tc_ctr_prng_generate(0, 0, 0, 0, 0);
  406. if (0 != ret) {
  407. result = TC_FAIL;
  408. goto exitTest;
  409. }
  410. ret = tc_ctr_prng_reseed(&ctx, 0, 0, 0, 0);
  411. if (0 != ret) {
  412. result = TC_FAIL;
  413. goto exitTest;
  414. }
  415. /* too little entropy */
  416. ret = tc_ctr_prng_reseed(&ctx, entropy, (sizeof entropy) - 1U, 0, 0);
  417. if (0 != ret) {
  418. result = TC_FAIL;
  419. goto exitTest;
  420. }
  421. ret = tc_ctr_prng_reseed(0, entropy, sizeof entropy, 0, 0);
  422. if (0 != ret) {
  423. result = TC_FAIL;
  424. goto exitTest;
  425. }
  426. ret = tc_ctr_prng_reseed(0, 0, 0, 0, 0);
  427. if (0 != ret) {
  428. result = TC_FAIL;
  429. goto exitTest;
  430. }
  431. ret = tc_ctr_prng_init(&ctx, 0, 0, 0, 0);
  432. if (0 != ret) {
  433. result = TC_FAIL;
  434. goto exitTest;
  435. }
  436. /* too little entropy */
  437. ret = tc_ctr_prng_init(&ctx, entropy, (sizeof entropy) - 1U, 0, 0);
  438. if (0 != ret) {
  439. result = TC_FAIL;
  440. goto exitTest;
  441. }
  442. ret = tc_ctr_prng_init(0, entropy, sizeof entropy, 0, 0);
  443. if (0 != ret) {
  444. result = TC_FAIL;
  445. goto exitTest;
  446. }
  447. ret = tc_ctr_prng_init(0, 0, 0, 0, 0);
  448. if (0 != ret) {
  449. result = TC_FAIL;
  450. goto exitTest;
  451. }
  452. exitTest:
  453. if (TC_FAIL == result) {
  454. TC_ERROR("CTR PRNG reseed tests failed\n");
  455. }
  456. return result;
  457. }
  458. /*
  459. * Main task to test CTR PRNG
  460. */
  461. int main(void)
  462. {
  463. int result = TC_PASS;
  464. unsigned int i;
  465. TC_START("Performing CTR-PRNG tests:");
  466. for (i = 0U; i < sizeof vectors / sizeof vectors[0]; i++) {
  467. result = executePRNG_TestVector(vectors[i], i);
  468. if (TC_PASS != result) {
  469. goto exitTest;
  470. }
  471. }
  472. if (TC_PASS != test_reseed()) {
  473. goto exitTest;
  474. }
  475. if (TC_PASS != test_uninstantiate()) {
  476. goto exitTest;
  477. }
  478. if (TC_PASS != test_robustness()) {
  479. goto exitTest;
  480. }
  481. TC_PRINT("All CTR PRNG tests succeeded!\n");
  482. exitTest:
  483. TC_END_RESULT(result);
  484. TC_END_REPORT(result);
  485. }