evp_xof_test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/evp.h>
  10. #include <openssl/rand.h>
  11. #include <openssl/core_names.h>
  12. #include "testutil.h"
  13. #include "internal/nelem.h"
  14. static const unsigned char shake256_input[] = {
  15. 0x8d, 0x80, 0x01, 0xe2, 0xc0, 0x96, 0xf1, 0xb8,
  16. 0x8e, 0x7c, 0x92, 0x24, 0xa0, 0x86, 0xef, 0xd4,
  17. 0x79, 0x7f, 0xbf, 0x74, 0xa8, 0x03, 0x3a, 0x2d,
  18. 0x42, 0x2a, 0x2b, 0x6b, 0x8f, 0x67, 0x47, 0xe4
  19. };
  20. /*
  21. * This KAT output is 250 bytes, which is more than
  22. * the SHAKE256 block size (136 bytes).
  23. */
  24. static const unsigned char shake256_output[] = {
  25. 0x2e, 0x97, 0x5f, 0x6a, 0x8a, 0x14, 0xf0, 0x70,
  26. 0x4d, 0x51, 0xb1, 0x36, 0x67, 0xd8, 0x19, 0x5c,
  27. 0x21, 0x9f, 0x71, 0xe6, 0x34, 0x56, 0x96, 0xc4,
  28. 0x9f, 0xa4, 0xb9, 0xd0, 0x8e, 0x92, 0x25, 0xd3,
  29. 0xd3, 0x93, 0x93, 0x42, 0x51, 0x52, 0xc9, 0x7e,
  30. 0x71, 0xdd, 0x24, 0x60, 0x1c, 0x11, 0xab, 0xcf,
  31. 0xa0, 0xf1, 0x2f, 0x53, 0xc6, 0x80, 0xbd, 0x3a,
  32. 0xe7, 0x57, 0xb8, 0x13, 0x4a, 0x9c, 0x10, 0xd4,
  33. 0x29, 0x61, 0x58, 0x69, 0x21, 0x7f, 0xdd, 0x58,
  34. 0x85, 0xc4, 0xdb, 0x17, 0x49, 0x85, 0x70, 0x3a,
  35. 0x6d, 0x6d, 0xe9, 0x4a, 0x66, 0x7e, 0xac, 0x30,
  36. 0x23, 0x44, 0x3a, 0x83, 0x37, 0xae, 0x1b, 0xc6,
  37. 0x01, 0xb7, 0x6d, 0x7d, 0x38, 0xec, 0x3c, 0x34,
  38. 0x46, 0x31, 0x05, 0xf0, 0xd3, 0x94, 0x9d, 0x78,
  39. 0xe5, 0x62, 0xa0, 0x39, 0xe4, 0x46, 0x95, 0x48,
  40. 0xb6, 0x09, 0x39, 0x5d, 0xe5, 0xa4, 0xfd, 0x43,
  41. 0xc4, 0x6c, 0xa9, 0xfd, 0x6e, 0xe2, 0x9a, 0xda,
  42. 0x5e, 0xfc, 0x07, 0xd8, 0x4d, 0x55, 0x32, 0x49,
  43. 0x45, 0x0d, 0xab, 0x4a, 0x49, 0xc4, 0x83, 0xde,
  44. 0xd2, 0x50, 0xc9, 0x33, 0x8f, 0x85, 0xcd, 0x93,
  45. 0x7a, 0xe6, 0x6b, 0xb4, 0x36, 0xf3, 0xb4, 0x02,
  46. 0x6e, 0x85, 0x9f, 0xda, 0x1c, 0xa5, 0x71, 0x43,
  47. 0x2f, 0x3b, 0xfc, 0x09, 0xe7, 0xc0, 0x3c, 0xa4,
  48. 0xd1, 0x83, 0xb7, 0x41, 0x11, 0x1c, 0xa0, 0x48,
  49. 0x3d, 0x0e, 0xda, 0xbc, 0x03, 0xfe, 0xb2, 0x3b,
  50. 0x17, 0xee, 0x48, 0xe8, 0x44, 0xba, 0x24, 0x08,
  51. 0xd9, 0xdc, 0xfd, 0x01, 0x39, 0xd2, 0xe8, 0xc7,
  52. 0x31, 0x01, 0x25, 0xae, 0xe8, 0x01, 0xc6, 0x1a,
  53. 0xb7, 0x90, 0x0d, 0x1e, 0xfc, 0x47, 0xc0, 0x78,
  54. 0x28, 0x17, 0x66, 0xf3, 0x61, 0xc5, 0xe6, 0x11,
  55. 0x13, 0x46, 0x23, 0x5e, 0x1d, 0xc3, 0x83, 0x25,
  56. 0x66, 0x6c
  57. };
  58. static const unsigned char shake256_largemsg_input[] = {
  59. 0xb2, 0xd2, 0x38, 0x65, 0xaf, 0x8f, 0x25, 0x6e,
  60. 0x64, 0x40, 0xe2, 0x0d, 0x49, 0x8e, 0x3e, 0x64,
  61. 0x46, 0xd2, 0x03, 0xa4, 0x19, 0xe3, 0x7b, 0x80,
  62. 0xf7, 0x2b, 0x32, 0xe2, 0x76, 0x01, 0xfe, 0xdd,
  63. 0xaa, 0x33, 0x3d, 0xe4, 0x8e, 0xe1, 0x5e, 0x39,
  64. 0xa6, 0x92, 0xa3, 0xa7, 0xe3, 0x81, 0x24, 0x74,
  65. 0xc7, 0x38, 0x18, 0x92, 0xc9, 0x60, 0x50, 0x15,
  66. 0xfb, 0xd8, 0x04, 0xea, 0xea, 0x04, 0xd2, 0xc5,
  67. 0xc6, 0x68, 0x04, 0x5b, 0xc3, 0x75, 0x12, 0xd2,
  68. 0xbe, 0xa2, 0x67, 0x75, 0x24, 0xbf, 0x68, 0xad,
  69. 0x10, 0x86, 0xb3, 0x2c, 0xb3, 0x74, 0xa4, 0x6c,
  70. 0xf9, 0xd7, 0x1e, 0x58, 0x69, 0x27, 0x88, 0x49,
  71. 0x4e, 0x99, 0x15, 0x33, 0x14, 0xf2, 0x49, 0x21,
  72. 0xf4, 0x99, 0xb9, 0xde, 0xd4, 0xf1, 0x12, 0xf5,
  73. 0x68, 0xe5, 0x5c, 0xdc, 0x9e, 0xc5, 0x80, 0x6d,
  74. 0x39, 0x50, 0x08, 0x95, 0xbb, 0x12, 0x27, 0x50,
  75. 0x89, 0xf0, 0xf9, 0xd5, 0x4a, 0x01, 0x0b, 0x0d,
  76. 0x90, 0x9f, 0x1e, 0x4a, 0xba, 0xbe, 0x28, 0x36,
  77. 0x19, 0x7d, 0x9c, 0x0a, 0x51, 0xfb, 0xeb, 0x00,
  78. 0x02, 0x6c, 0x4b, 0x0a, 0xa8, 0x6c, 0xb7, 0xc4,
  79. 0xc0, 0x92, 0x37, 0xa7, 0x2d, 0x49, 0x61, 0x80,
  80. 0xd9, 0xdb, 0x20, 0x21, 0x9f, 0xcf, 0xb4, 0x57,
  81. 0x69, 0x75, 0xfa, 0x1c, 0x95, 0xbf, 0xee, 0x0d,
  82. 0x9e, 0x52, 0x6e, 0x1e, 0xf8, 0xdd, 0x41, 0x8c,
  83. 0x3b, 0xaa, 0x57, 0x13, 0x84, 0x73, 0x52, 0x62,
  84. 0x18, 0x76, 0x46, 0xcc, 0x4b, 0xcb, 0xbd, 0x40,
  85. 0xa1, 0xf6, 0xff, 0x7b, 0x32, 0xb9, 0x90, 0x7c,
  86. 0x53, 0x2c, 0xf9, 0x38, 0x72, 0x0f, 0xcb, 0x90,
  87. 0x42, 0x5e, 0xe2, 0x80, 0x19, 0x26, 0xe7, 0x99,
  88. 0x96, 0x98, 0x18, 0xb1, 0x86, 0x5b, 0x4c, 0xd9,
  89. 0x08, 0x27, 0x31, 0x8f, 0xf0, 0x90, 0xd9, 0x35,
  90. 0x6a, 0x1f, 0x75, 0xc2, 0xe0, 0xa7, 0x60, 0xb8,
  91. 0x1d, 0xd6, 0x5f, 0x56, 0xb2, 0x0b, 0x27, 0x0e,
  92. 0x98, 0x67, 0x1f, 0x39, 0x18, 0x27, 0x68, 0x0a,
  93. 0xe8, 0x31, 0x1b, 0xc0, 0x97, 0xec, 0xd1, 0x20,
  94. 0x2a, 0x55, 0x69, 0x23, 0x08, 0x50, 0x05, 0xec,
  95. 0x13, 0x3b, 0x56, 0xfc, 0x18, 0xc9, 0x1a, 0xa9,
  96. 0x69, 0x0e, 0xe2, 0xcc, 0xc8, 0xd6, 0x19, 0xbb,
  97. 0x87, 0x3b, 0x42, 0x77, 0xee, 0x77, 0x81, 0x26,
  98. 0xdd, 0xf6, 0x5d, 0xc3, 0xb2, 0xb0, 0xc4, 0x14,
  99. 0x6d, 0xb5, 0x4f, 0xdc, 0x13, 0x09, 0xc8, 0x53,
  100. 0x50, 0xb3, 0xea, 0xd3, 0x5f, 0x11, 0x67, 0xd4,
  101. 0x2f, 0x6e, 0x30, 0x1a, 0xbe, 0xd6, 0xf0, 0x2d,
  102. 0xc9, 0x29, 0xd9, 0x0a, 0xa8, 0x6f, 0xa4, 0x18,
  103. 0x74, 0x6b, 0xd3, 0x5d, 0x6a, 0x73, 0x3a, 0xf2,
  104. 0x94, 0x7f, 0xbd, 0xb4, 0xa6, 0x7f, 0x5b, 0x3d,
  105. 0x26, 0xf2, 0x6c, 0x13, 0xcf, 0xb4, 0x26, 0x1e,
  106. 0x38, 0x17, 0x66, 0x60, 0xb1, 0x36, 0xae, 0xe0,
  107. 0x6d, 0x86, 0x69, 0xe7, 0xe7, 0xae, 0x77, 0x6f,
  108. 0x7e, 0x99, 0xe5, 0xd9, 0x62, 0xc9, 0xfc, 0xde,
  109. 0xb4, 0xee, 0x7e, 0xc8, 0xe9, 0xb7, 0x2c, 0xe2,
  110. 0x70, 0xe8, 0x8b, 0x2d, 0x94, 0xad, 0xe8, 0x54,
  111. 0xa3, 0x2d, 0x9a, 0xe2, 0x50, 0x63, 0x87, 0xb3,
  112. 0x56, 0x29, 0xea, 0xa8, 0x5e, 0x96, 0x53, 0x9f,
  113. 0x23, 0x8a, 0xef, 0xa3, 0xd4, 0x87, 0x09, 0x5f,
  114. 0xba, 0xc3, 0xd1, 0xd9, 0x1a, 0x7b, 0x5c, 0x5d,
  115. 0x5d, 0x89, 0xed, 0xb6, 0x6e, 0x39, 0x73, 0xa5,
  116. 0x64, 0x59, 0x52, 0x8b, 0x61, 0x8f, 0x66, 0x69,
  117. 0xb9, 0xf0, 0x45, 0x0a, 0x57, 0xcd, 0xc5, 0x7f,
  118. 0x5d, 0xd0, 0xbf, 0xcc, 0x0b, 0x48, 0x12, 0xe1,
  119. 0xe2, 0xc2, 0xea, 0xcc, 0x09, 0xd9, 0x42, 0x2c,
  120. 0xef, 0x4f, 0xa7, 0xe9, 0x32, 0x5c, 0x3f, 0x22,
  121. 0xc0, 0x45, 0x0b, 0x67, 0x3c, 0x31, 0x69, 0x29,
  122. 0xa3, 0x39, 0xdd, 0x6e, 0x2f, 0xbe, 0x10, 0xc9,
  123. 0x7b, 0xff, 0x19, 0x8a, 0xe9, 0xea, 0xfc, 0x32,
  124. 0x41, 0x33, 0x70, 0x2a, 0x9a, 0xa4, 0xe6, 0xb4,
  125. 0x7e, 0xb4, 0xc6, 0x21, 0x49, 0x5a, 0xfc, 0x45,
  126. 0xd2, 0x23, 0xb3, 0x28, 0x4d, 0x83, 0x60, 0xfe,
  127. 0x70, 0x68, 0x03, 0x59, 0xd5, 0x15, 0xaa, 0x9e,
  128. 0xa0, 0x2e, 0x36, 0xb5, 0x61, 0x0f, 0x61, 0x05,
  129. 0x3c, 0x62, 0x00, 0xa0, 0x47, 0xf1, 0x86, 0xba,
  130. 0x33, 0xb8, 0xca, 0x60, 0x2f, 0x3f, 0x0a, 0x67,
  131. 0x09, 0x27, 0x2f, 0xa2, 0x96, 0x02, 0x52, 0x58,
  132. 0x55, 0x68, 0x80, 0xf4, 0x4f, 0x47, 0xba, 0xff,
  133. 0x41, 0x7a, 0x40, 0x4c, 0xfd, 0x9d, 0x10, 0x72,
  134. 0x0e, 0x20, 0xa9, 0x7f, 0x9b, 0x9b, 0x14, 0xeb,
  135. 0x8e, 0x61, 0x25, 0xcb, 0xf4, 0x58, 0xff, 0x47,
  136. 0xa7, 0x08, 0xd6, 0x4e, 0x2b, 0xf1, 0xf9, 0x89,
  137. 0xd7, 0x22, 0x0f, 0x8d, 0x35, 0x07, 0xa0, 0x54,
  138. 0xab, 0x83, 0xd8, 0xee, 0x5a, 0x3e, 0x88, 0x74,
  139. 0x46, 0x41, 0x6e, 0x3e, 0xb7, 0xc0, 0xb6, 0x55,
  140. 0xe0, 0x36, 0xc0, 0x2b, 0xbf, 0xb8, 0x24, 0x8a,
  141. 0x44, 0x82, 0xf4, 0xcb, 0xb5, 0xd7, 0x41, 0x48,
  142. 0x51, 0x08, 0xe0, 0x14, 0x34, 0xd2, 0x6d, 0xe9,
  143. 0x7a, 0xec, 0x91, 0x61, 0xa7, 0xe1, 0x81, 0x69,
  144. 0x47, 0x1c, 0xc7, 0xf3
  145. };
  146. static const unsigned char shake256_largemsg_output[] = {
  147. 0x64, 0xea, 0x24, 0x6a, 0xab, 0x80, 0x37, 0x9e,
  148. 0x08, 0xe2, 0x19, 0x9e, 0x09, 0x69, 0xe2, 0xee,
  149. 0x1a, 0x5d, 0xd1, 0x68, 0x68, 0xec, 0x8d, 0x42,
  150. 0xd0, 0xf8, 0xb8, 0x44, 0x74, 0x54, 0x87, 0x3e,
  151. };
  152. static EVP_MD_CTX *shake_setup(const char *name)
  153. {
  154. EVP_MD_CTX *ctx = NULL;
  155. EVP_MD *md = NULL;
  156. if (!TEST_ptr(md = EVP_MD_fetch(NULL, name, NULL)))
  157. return NULL;
  158. if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
  159. goto err;
  160. if (!TEST_true(EVP_DigestInit_ex2(ctx, md, NULL)))
  161. goto err;
  162. EVP_MD_free(md);
  163. return ctx;
  164. err:
  165. EVP_MD_free(md);
  166. EVP_MD_CTX_free(ctx);
  167. return NULL;
  168. }
  169. static int shake_kat_test(void)
  170. {
  171. int ret = 0;
  172. EVP_MD_CTX *ctx = NULL;
  173. unsigned char out[sizeof(shake256_output)];
  174. if (!TEST_ptr(ctx = shake_setup("SHAKE256")))
  175. return 0;
  176. if (!TEST_true(EVP_DigestUpdate(ctx, shake256_input,
  177. sizeof(shake256_input)))
  178. || !TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out)))
  179. || !TEST_mem_eq(out, sizeof(out),
  180. shake256_output,sizeof(shake256_output))
  181. /* Test that a second call to EVP_DigestFinalXOF fails */
  182. || !TEST_false(EVP_DigestFinalXOF(ctx, out, sizeof(out)))
  183. /* Test that a call to EVP_DigestSqueeze fails */
  184. || !TEST_false(EVP_DigestSqueeze(ctx, out, sizeof(out))))
  185. goto err;
  186. ret = 1;
  187. err:
  188. EVP_MD_CTX_free(ctx);
  189. return ret;
  190. }
  191. static int shake_kat_digestfinal_test(void)
  192. {
  193. int ret = 0;
  194. unsigned int digest_length = 0;
  195. EVP_MD_CTX *ctx = NULL;
  196. unsigned char out[sizeof(shake256_output)];
  197. if (!TEST_ptr(ctx = shake_setup("SHAKE256")))
  198. return 0;
  199. if (!TEST_true(EVP_DigestUpdate(ctx, shake256_input,
  200. sizeof(shake256_input)))
  201. || !TEST_true(EVP_DigestFinal(ctx, out, &digest_length))
  202. || !TEST_uint_eq(digest_length, 32)
  203. || !TEST_mem_eq(out, digest_length,
  204. shake256_output, digest_length)
  205. || !TEST_false(EVP_DigestFinalXOF(ctx, out, sizeof(out))))
  206. goto err;
  207. ret = 1;
  208. err:
  209. EVP_MD_CTX_free(ctx);
  210. return ret;
  211. }
  212. /*
  213. * Test that EVP_DigestFinal() returns the output length
  214. * set by the OSSL_DIGEST_PARAM_XOFLEN param.
  215. */
  216. static int shake_kat_digestfinal_xoflen_test(void)
  217. {
  218. int ret = 0;
  219. unsigned int digest_length = 0;
  220. EVP_MD_CTX *ctx = NULL;
  221. unsigned char out[sizeof(shake256_output)];
  222. OSSL_PARAM params[2];
  223. size_t sz = 12;
  224. if (!TEST_ptr(ctx = shake_setup("SHAKE256")))
  225. return 0;
  226. memset(out, 0, sizeof(out));
  227. params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_XOFLEN, &sz);
  228. params[1] = OSSL_PARAM_construct_end();
  229. if (!TEST_int_eq(EVP_MD_CTX_set_params(ctx, params), 1)
  230. || !TEST_true(EVP_DigestUpdate(ctx, shake256_input,
  231. sizeof(shake256_input)))
  232. || !TEST_true(EVP_DigestFinal(ctx, out, &digest_length))
  233. || !TEST_uint_eq(digest_length, (unsigned int)sz)
  234. || !TEST_mem_eq(out, digest_length,
  235. shake256_output, digest_length)
  236. || !TEST_uchar_eq(out[digest_length], 0))
  237. goto err;
  238. ret = 1;
  239. err:
  240. EVP_MD_CTX_free(ctx);
  241. return ret;
  242. }
  243. /*
  244. * Test that multiple absorb calls gives the expected result.
  245. * This is a nested test that uses multiple strides for the input.
  246. */
  247. static int shake_absorb_test(void)
  248. {
  249. int ret = 0;
  250. EVP_MD_CTX *ctx = NULL;
  251. unsigned char out[sizeof(shake256_largemsg_output)];
  252. size_t total = sizeof(shake256_largemsg_input);
  253. size_t i, stride, sz;
  254. if (!TEST_ptr(ctx = shake_setup("SHAKE256")))
  255. return 0;
  256. for (stride = 1; stride < total; ++stride) {
  257. sz = 0;
  258. for (i = 0; i < total; i += sz) {
  259. sz += stride;
  260. if ((i + sz) > total)
  261. sz = total - i;
  262. if (!TEST_true(EVP_DigestUpdate(ctx, shake256_largemsg_input + i,
  263. sz)))
  264. goto err;
  265. }
  266. if (!TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out)))
  267. || !TEST_mem_eq(out, sizeof(out),
  268. shake256_largemsg_output,
  269. sizeof(shake256_largemsg_output)))
  270. goto err;
  271. if (!TEST_true(EVP_DigestInit_ex2(ctx, NULL, NULL)))
  272. goto err;
  273. }
  274. ret = 1;
  275. err:
  276. EVP_MD_CTX_free(ctx);
  277. return ret;
  278. }
  279. /*
  280. * Table containing the size of the output to squeeze for the
  281. * initially call, followed by a size for each subsequent call.
  282. */
  283. static const struct {
  284. size_t startsz, incsz;
  285. } stride_tests[] = {
  286. { 1, 1 },
  287. { 1, 136 },
  288. { 1, 136/2 },
  289. { 1, 136/2-1 },
  290. { 1, 136/2+1 },
  291. { 1, 136*3 },
  292. { 8, 8 },
  293. { 9, 9 },
  294. { 10, 10 },
  295. { 136/2 - 1, 136 },
  296. { 136/2 - 1, 136-1 },
  297. { 136/2 - 1, 136+1 },
  298. { 136/2, 136 },
  299. { 136/2, 136-1 },
  300. { 136/2, 136+1 },
  301. { 136/2 + 1, 136 },
  302. { 136/2 + 1, 136-1 },
  303. { 136/2 + 1, 136+1 },
  304. { 136, 2 },
  305. { 136, 136 },
  306. { 136-1, 136 },
  307. { 136-1, 136-1 },
  308. { 136-1, 136+1 },
  309. { 136+1, 136 },
  310. { 136+1, 136-1 },
  311. { 136+1, 136+1 },
  312. { 136*3, 136 },
  313. { 136*3, 136 + 1 },
  314. { 136*3, 136 - 1 },
  315. { 136*3, 136/2 },
  316. { 136*3, 136/2 + 1 },
  317. { 136*3, 136/2 - 1 },
  318. };
  319. /*
  320. * Helper to do multiple squeezes of output data using SHAKE256.
  321. * tst is an index into the stride_tests[] containing an initial starting
  322. * output length, followed by a second output length to use for all remaining
  323. * squeezes. expected_outlen contains the total number of bytes to squeeze.
  324. * in and inlen represent the input to absorb. expected_out and expected_outlen
  325. * represent the expected output.
  326. */
  327. static int do_shake_squeeze_test(int tst,
  328. const unsigned char *in, size_t inlen,
  329. const unsigned char *expected_out,
  330. size_t expected_outlen)
  331. {
  332. int ret = 0;
  333. EVP_MD_CTX *ctx = NULL;
  334. unsigned char *out = NULL;
  335. size_t i = 0, sz = stride_tests[tst].startsz;
  336. if (!TEST_ptr(ctx = shake_setup("SHAKE256")))
  337. return 0;
  338. if (!TEST_ptr(out = OPENSSL_malloc(expected_outlen)))
  339. goto err;
  340. if (!TEST_true(EVP_DigestUpdate(ctx, in, inlen)))
  341. goto err;
  342. while (i < expected_outlen) {
  343. if ((i + sz) > expected_outlen)
  344. sz = expected_outlen - i;
  345. if (!TEST_true(EVP_DigestSqueeze(ctx, out + i, sz)))
  346. goto err;
  347. i += sz;
  348. sz = stride_tests[tst].incsz;
  349. }
  350. if (!TEST_mem_eq(out, expected_outlen, expected_out, expected_outlen))
  351. goto err;
  352. ret = 1;
  353. err:
  354. OPENSSL_free(out);
  355. EVP_MD_CTX_free(ctx);
  356. return ret;
  357. }
  358. static int shake_squeeze_kat_test(int tst)
  359. {
  360. return do_shake_squeeze_test(tst, shake256_input, sizeof(shake256_input),
  361. shake256_output, sizeof(shake256_output));
  362. }
  363. /*
  364. * Generate some random input to absorb, and then
  365. * squeeze it out in one operation to get a expected
  366. * output. Use this to test that multiple squeeze calls
  367. * on the same input gives the same output.
  368. */
  369. static int shake_squeeze_large_test(int tst)
  370. {
  371. int ret = 0;
  372. EVP_MD_CTX *ctx = NULL;
  373. unsigned char msg[16];
  374. unsigned char out[2000];
  375. if (!TEST_int_gt(RAND_bytes(msg, sizeof(msg)), 0)
  376. || !TEST_ptr(ctx = shake_setup("SHAKE256"))
  377. || !TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg)))
  378. || !TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out))))
  379. goto err;
  380. ret = do_shake_squeeze_test(tst, msg, sizeof(msg), out, sizeof(out));
  381. err:
  382. EVP_MD_CTX_free(ctx);
  383. return ret;
  384. }
  385. static const size_t dupoffset_tests[] = {
  386. 1, 135, 136, 137, 136*3-1, 136*3, 136*3+1
  387. };
  388. /* Helper function to test that EVP_MD_CTX_dup() copies the internal state */
  389. static int do_shake_squeeze_dup_test(int tst, const char *alg,
  390. const unsigned char *in, size_t inlen,
  391. const unsigned char *expected_out,
  392. size_t expected_outlen)
  393. {
  394. int ret = 0;
  395. EVP_MD_CTX *cur, *ctx = NULL, *dupctx = NULL;
  396. unsigned char *out = NULL;
  397. size_t i = 0, sz = 10;
  398. size_t dupoffset = dupoffset_tests[tst];
  399. if (!TEST_ptr(ctx = shake_setup(alg)))
  400. return 0;
  401. cur = ctx;
  402. if (!TEST_ptr(out = OPENSSL_malloc(expected_outlen)))
  403. goto err;
  404. if (!TEST_true(EVP_DigestUpdate(ctx, in, inlen)))
  405. goto err;
  406. while (i < expected_outlen) {
  407. if ((i + sz) > expected_outlen)
  408. sz = expected_outlen - i;
  409. if (!TEST_true(EVP_DigestSqueeze(cur, out + i, sz)))
  410. goto err;
  411. i += sz;
  412. /* At a certain offset we swap to a new ctx that copies the state */
  413. if (dupctx == NULL && i >= dupoffset) {
  414. if (!TEST_ptr(dupctx = EVP_MD_CTX_dup(ctx)))
  415. goto err;
  416. cur = dupctx;
  417. }
  418. }
  419. if (!TEST_mem_eq(out, expected_outlen, expected_out, expected_outlen))
  420. goto err;
  421. ret = 1;
  422. err:
  423. OPENSSL_free(out);
  424. EVP_MD_CTX_free(ctx);
  425. EVP_MD_CTX_free(dupctx);
  426. return ret;
  427. }
  428. /* Test that the internal state can be copied */
  429. static int shake_squeeze_dup_test(int tst)
  430. {
  431. int ret = 0;
  432. EVP_MD_CTX *ctx = NULL;
  433. unsigned char msg[16];
  434. unsigned char out[1000];
  435. const char *alg = "SHAKE128";
  436. if (!TEST_int_gt(RAND_bytes(msg, sizeof(msg)), 0)
  437. || !TEST_ptr(ctx = shake_setup(alg))
  438. || !TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg)))
  439. || !TEST_true(EVP_DigestFinalXOF(ctx, out, sizeof(out))))
  440. goto err;
  441. ret = do_shake_squeeze_dup_test(tst, alg, msg, sizeof(msg),
  442. out, sizeof(out));
  443. err:
  444. EVP_MD_CTX_free(ctx);
  445. return ret;
  446. }
  447. int setup_tests(void)
  448. {
  449. ADD_TEST(shake_kat_test);
  450. ADD_TEST(shake_kat_digestfinal_test);
  451. ADD_TEST(shake_kat_digestfinal_xoflen_test);
  452. ADD_TEST(shake_absorb_test);
  453. ADD_ALL_TESTS(shake_squeeze_kat_test, OSSL_NELEM(stride_tests));
  454. ADD_ALL_TESTS(shake_squeeze_large_test, OSSL_NELEM(stride_tests));
  455. ADD_ALL_TESTS(shake_squeeze_dup_test, OSSL_NELEM(dupoffset_tests));
  456. return 1;
  457. }