1
0

ml_kem_evp_extra_test.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright 2015-2025 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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/conf.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/provider.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/params.h>
  19. #include <openssl/param_build.h>
  20. #include <openssl/rand.h>
  21. #include <crypto/ml_kem.h>
  22. #include "testutil.h"
  23. static OSSL_LIB_CTX *testctx = NULL;
  24. typedef enum OPTION_choice {
  25. OPT_ERR = -1,
  26. OPT_EOF = 0,
  27. OPT_CONFIG_FILE,
  28. OPT_TEST_RAND,
  29. OPT_TEST_ENUM
  30. } OPTION_CHOICE;
  31. const OPTIONS *test_get_options(void)
  32. {
  33. static const OPTIONS options[] = {
  34. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  35. { "test-rand", OPT_TEST_RAND, '-', "Test non-derandomised ML-KEM" },
  36. { NULL }
  37. };
  38. return options;
  39. }
  40. static uint8_t gen_seed[64] = {
  41. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa, 0x0c, 0x6d, 0x10, 0xe4,
  42. 0xdb, 0x6b, 0x1a, 0xdd, 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  43. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d, 0x86, 0x26, 0xed, 0x79,
  44. 0xd4, 0x51, 0x14, 0x08, 0x00, 0xe0, 0x3b, 0x59, 0xb9, 0x56, 0xf8, 0x21,
  45. 0x0e, 0x55, 0x60, 0x67, 0x40, 0x7d, 0x13, 0xdc, 0x90, 0xfa, 0x9e, 0x8b,
  46. 0x87, 0x2b, 0xfb, 0x8f
  47. };
  48. static uint8_t enc_seed[32] = {
  49. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4, 0x06, 0xc8, 0xfa, 0xe1,
  50. 0x87, 0x4d, 0x7f, 0x13, 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  51. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  52. };
  53. static uint8_t dec_seed[32] = {
  54. 0x4e, 0x6f, 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x64, 0x72, 0x6f, 0x69,
  55. 0x64, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x27, 0x72, 0x65, 0x20, 0x6c, 0x6f,
  56. 0x6f, 0x6b, 0x69, 0x6e, 0x67, 0x20, 0x66, 0x6f
  57. };
  58. static uint8_t expected_rho[3][32] = {
  59. {
  60. 0x7e, 0xfb, 0x9e, 0x40, 0xc3, 0xbf, 0x0f, 0xf0, 0x43, 0x29, 0x86, 0xae,
  61. 0x4b, 0xc1, 0xa2, 0x42, 0xce, 0x99, 0x21, 0xaa, 0x9e, 0x22, 0x44, 0x88,
  62. 0x19, 0x58, 0x5d, 0xea, 0x30, 0x8e, 0xb0, 0x39
  63. },
  64. {
  65. 0x16, 0x2e, 0xc0, 0x98, 0xa9, 0x00, 0xb1, 0x2d, 0xd8, 0xfa, 0xbb, 0xfb,
  66. 0x3f, 0xe8, 0xcb, 0x1d, 0xc4, 0xe8, 0x31, 0x5f, 0x2a, 0xf0, 0xd3, 0x2f,
  67. 0x00, 0x17, 0xae, 0x13, 0x6e, 0x19, 0xf0, 0x28
  68. },
  69. {
  70. 0x29, 0xb4, 0xf9, 0xf8, 0xcf, 0xba, 0xdf, 0x2e, 0x41, 0x86, 0x9a, 0xbf,
  71. 0xba, 0xd1, 0x07, 0x38, 0xad, 0x04, 0xcc, 0x75, 0x2b, 0xc2, 0x0c, 0x39,
  72. 0x47, 0x46, 0x85, 0x0e, 0x0c, 0x48, 0x47, 0xdb
  73. }
  74. };
  75. static uint8_t expected_ctext_sha256[3][32] = {
  76. {
  77. 0xbc, 0x29, 0xd7, 0xdf, 0x8b, 0xc5, 0x46, 0x5d, 0x98, 0x06, 0x01, 0xd8,
  78. 0x00, 0x25, 0x97, 0x93, 0xe2, 0x60, 0x38, 0x25, 0xa5, 0x72, 0xda, 0x6c,
  79. 0xd1, 0x98, 0xa5, 0x12, 0xcc, 0x6d, 0x1a, 0x34
  80. },
  81. {
  82. 0x36, 0x82, 0x9a, 0x2f, 0x35, 0xcb, 0xf4, 0xde, 0xb6, 0x2c, 0x0a, 0x12,
  83. 0xa1, 0x5c, 0x22, 0xda, 0xe9, 0xf8, 0xd2, 0xc2, 0x52, 0x56, 0x6f, 0xc2,
  84. 0x4f, 0x88, 0xab, 0xe8, 0x05, 0xcb, 0x57, 0x5e
  85. },
  86. {
  87. 0x50, 0x81, 0x36, 0xa1, 0x3f, 0x8a, 0x79, 0x20, 0xe3, 0x43, 0x44, 0x98,
  88. 0xc6, 0x97, 0x5c, 0xbb, 0xab, 0x45, 0x7d, 0x80, 0x93, 0x09, 0xeb, 0x2f,
  89. 0x92, 0x45, 0x3e, 0x74, 0x09, 0x73, 0x82, 0x10
  90. }
  91. };
  92. static uint8_t expected_shared_secret[3][32] = {
  93. {
  94. 0x31, 0x98, 0x39, 0xe8, 0x2a, 0xb6, 0xb2, 0x22, 0xde, 0x7b, 0x61, 0x9e,
  95. 0x80, 0xda, 0x83, 0x91, 0x52, 0x2b, 0xbb, 0x37, 0x67, 0x70, 0x18, 0x49,
  96. 0x4a, 0x47, 0x42, 0xc5, 0x3f, 0x9a, 0xbf, 0xdf
  97. },
  98. {
  99. 0xe7, 0x18, 0x4a, 0x09, 0x75, 0xee, 0x34, 0x70, 0x87, 0x8d, 0x2d, 0x15,
  100. 0x9e, 0xc8, 0x31, 0x29, 0xc8, 0xae, 0xc2, 0x53, 0xd4, 0xee, 0x17, 0xb4,
  101. 0x81, 0x03, 0x11, 0xd1, 0x98, 0xcd, 0x03, 0x68
  102. },
  103. {
  104. 0x48, 0x9d, 0xd1, 0xe9, 0xc2, 0xbe, 0x4a, 0xf3, 0x48, 0x2b, 0xdb, 0x35,
  105. 0xbb, 0x26, 0xce, 0x76, 0x0e, 0x6e, 0x41, 0x4d, 0xa6, 0xec, 0xbe, 0x48,
  106. 0x99, 0x85, 0x74, 0x8a, 0x82, 0x5f, 0x1c, 0xd6
  107. },
  108. };
  109. static int test_ml_kem(void)
  110. {
  111. EVP_PKEY *akey, *bkey = NULL;
  112. int res = 0;
  113. size_t publen;
  114. unsigned char *rawpub = NULL;
  115. EVP_PKEY_CTX *ctx = NULL;
  116. unsigned char *wrpkey = NULL, *agenkey = NULL, *bgenkey = NULL;
  117. size_t wrpkeylen, agenkeylen, bgenkeylen, i;
  118. /* Generate Alice's key */
  119. akey = EVP_PKEY_Q_keygen(testctx, NULL, "ML-KEM-768");
  120. if (!TEST_ptr(akey))
  121. goto err;
  122. /* Get the raw public key */
  123. publen = EVP_PKEY_get1_encoded_public_key(akey, &rawpub);
  124. if (!TEST_size_t_gt(publen, 0))
  125. goto err;
  126. /* Create Bob's key and populate it with Alice's public key data */
  127. bkey = EVP_PKEY_new();
  128. if (!TEST_ptr(bkey))
  129. goto err;
  130. if (!TEST_int_gt(EVP_PKEY_copy_parameters(bkey, akey), 0))
  131. goto err;
  132. /* Bob's empty key is not equal to Alice's */
  133. if (!TEST_false(EVP_PKEY_eq(akey, bkey))
  134. || !TEST_false(EVP_PKEY_eq(bkey, akey)))
  135. goto err;
  136. if (!TEST_true(EVP_PKEY_set1_encoded_public_key(bkey, rawpub, publen)))
  137. goto err;
  138. /* Bob's copy of Alice's public key makes the two equal */
  139. if (!TEST_true(EVP_PKEY_eq(akey, bkey))
  140. || !TEST_true(EVP_PKEY_eq(bkey, akey)))
  141. goto err;
  142. /* Encapsulate Bob's key */
  143. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, bkey, NULL);
  144. if (!TEST_ptr(ctx))
  145. goto err;
  146. if (!TEST_int_gt(EVP_PKEY_encapsulate_init(ctx, NULL), 0))
  147. goto err;
  148. if (!TEST_int_gt(EVP_PKEY_encapsulate(ctx, NULL, &wrpkeylen, NULL,
  149. &bgenkeylen), 0))
  150. goto err;
  151. if (!TEST_size_t_gt(wrpkeylen, 0) || !TEST_size_t_gt(bgenkeylen, 0))
  152. goto err;
  153. wrpkey = OPENSSL_zalloc(wrpkeylen);
  154. bgenkey = OPENSSL_zalloc(bgenkeylen);
  155. if (!TEST_ptr(wrpkey) || !TEST_ptr(bgenkey))
  156. goto err;
  157. if (!TEST_int_gt(EVP_PKEY_encapsulate(ctx, wrpkey, &wrpkeylen, bgenkey,
  158. &bgenkeylen), 0))
  159. goto err;
  160. EVP_PKEY_CTX_free(ctx);
  161. /* Alice now decapsulates Bob's key */
  162. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, akey, NULL);
  163. if (!TEST_ptr(ctx))
  164. goto err;
  165. if (!TEST_int_gt(EVP_PKEY_decapsulate_init(ctx, NULL), 0))
  166. goto err;
  167. if (!TEST_int_gt(EVP_PKEY_decapsulate(ctx, NULL, &agenkeylen, wrpkey,
  168. wrpkeylen), 0))
  169. goto err;
  170. if (!TEST_size_t_gt(agenkeylen, 0))
  171. goto err;
  172. agenkey = OPENSSL_zalloc(agenkeylen);
  173. if (!TEST_ptr(agenkey))
  174. goto err;
  175. if (!TEST_int_gt(EVP_PKEY_decapsulate(ctx, agenkey, &agenkeylen, wrpkey,
  176. wrpkeylen), 0))
  177. goto err;
  178. /* Hopefully we ended up with a shared key */
  179. if (!TEST_mem_eq(agenkey, agenkeylen, bgenkey, bgenkeylen))
  180. goto err;
  181. /* Verify we generated a non-zero shared key */
  182. for (i = 0; i < agenkeylen; i++)
  183. if (agenkey[i] != 0)
  184. break;
  185. if (!TEST_size_t_ne(i, agenkeylen))
  186. goto err;
  187. res = 1;
  188. err:
  189. EVP_PKEY_CTX_free(ctx);
  190. EVP_PKEY_free(akey);
  191. EVP_PKEY_free(bkey);
  192. OPENSSL_free(rawpub);
  193. OPENSSL_free(wrpkey);
  194. OPENSSL_free(agenkey);
  195. OPENSSL_free(bgenkey);
  196. return res;
  197. }
  198. static int test_non_derandomised_ml_kem(void)
  199. {
  200. static const int alg[3] = {
  201. EVP_PKEY_ML_KEM_512,
  202. EVP_PKEY_ML_KEM_768,
  203. EVP_PKEY_ML_KEM_1024
  204. };
  205. EVP_RAND_CTX *privctx;
  206. EVP_RAND_CTX *pubctx;
  207. EVP_MD *sha256;
  208. int i, ret = 0;
  209. if (!TEST_ptr(privctx = RAND_get0_private(NULL))
  210. || !TEST_ptr(pubctx = RAND_get0_public(NULL)))
  211. return 0;
  212. if (!TEST_ptr(sha256 = EVP_MD_fetch(NULL, "sha256", NULL)))
  213. return 0;
  214. for (i = 0; i < (int) OSSL_NELEM(alg); ++i) {
  215. const ML_KEM_VINFO *v;
  216. OSSL_PARAM params[3], *p;
  217. uint8_t hash[32];
  218. EVP_PKEY *akey = NULL, *bkey = NULL;
  219. size_t publen;
  220. unsigned char *rawpub = NULL;
  221. EVP_PKEY_CTX *ctx = NULL;
  222. unsigned char *wrpkey = NULL, *agenkey = NULL, *bgenkey = NULL;
  223. size_t wrpkeylen, agenkeylen, bgenkeylen;
  224. unsigned int strength = 256;
  225. unsigned char c;
  226. int res = -1;
  227. if ((v = ossl_ml_kem_get_vinfo(alg[i])) == NULL)
  228. goto done;
  229. /* Configure the private RNG to output just the keygen seed */
  230. p = params;
  231. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  232. gen_seed, sizeof(gen_seed));
  233. *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
  234. *p = OSSL_PARAM_construct_end();
  235. if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params)))
  236. goto done;
  237. res = -2;
  238. /* Generate Alice's key */
  239. akey = EVP_PKEY_Q_keygen(testctx, NULL, v->algorithm_name);
  240. if (!TEST_ptr(akey))
  241. goto done;
  242. /* Check that no more entropy is available! */
  243. if (!TEST_int_le(RAND_priv_bytes(&c, 1), 0))
  244. goto done;
  245. /* Get the raw public key */
  246. publen = EVP_PKEY_get1_encoded_public_key(akey, &rawpub);
  247. if (!TEST_size_t_eq(publen, v->pubkey_bytes))
  248. goto done;
  249. res = -3;
  250. /* Check that we got the expected 'rho' value in the ciphertext */
  251. if (!TEST_mem_eq(rawpub + v->vector_bytes, ML_KEM_RANDOM_BYTES,
  252. expected_rho[i], ML_KEM_RANDOM_BYTES))
  253. goto done;
  254. res = -4;
  255. /* Create Bob's key and populate it with Alice's public key data */
  256. bkey = EVP_PKEY_new();
  257. if (!TEST_ptr(bkey))
  258. goto done;
  259. if (!TEST_int_gt(EVP_PKEY_copy_parameters(bkey, akey), 0))
  260. goto done;
  261. if (!TEST_true(EVP_PKEY_set1_encoded_public_key(bkey, rawpub, publen)))
  262. goto done;
  263. /* Configure the public RNG to output just the encap seed */
  264. p = params;
  265. *p = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  266. enc_seed, sizeof(enc_seed));
  267. if (!TEST_true(EVP_RAND_CTX_set_params(pubctx, params)))
  268. goto done;
  269. /* Encapsulate Bob's key */
  270. res = -5;
  271. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, bkey, NULL);
  272. if (!TEST_ptr(ctx))
  273. goto done;
  274. if (!TEST_int_gt(EVP_PKEY_encapsulate_init(ctx, NULL), 0))
  275. goto done;
  276. if (!TEST_int_gt(EVP_PKEY_encapsulate(ctx, NULL, &wrpkeylen, NULL,
  277. &bgenkeylen), 0))
  278. goto done;
  279. if (!TEST_size_t_eq(wrpkeylen, v->ctext_bytes)
  280. || !TEST_size_t_eq(bgenkeylen, ML_KEM_SHARED_SECRET_BYTES))
  281. goto done;
  282. wrpkey = OPENSSL_zalloc(wrpkeylen);
  283. bgenkey = OPENSSL_zalloc(bgenkeylen);
  284. if (!TEST_ptr(wrpkey) || !TEST_ptr(bgenkey))
  285. goto done;
  286. if (!TEST_true(EVP_PKEY_encapsulate(ctx, wrpkey, &wrpkeylen, bgenkey,
  287. &bgenkeylen)))
  288. goto done;
  289. EVP_PKEY_CTX_free(ctx);
  290. ctx = NULL;
  291. /* Check that no more public entropy is available! */
  292. if (!TEST_int_le(RAND_bytes(&c, 1), 0))
  293. goto done;
  294. res = -6;
  295. /* Check the ciphertext hash */
  296. if (!TEST_true(EVP_Digest(wrpkey, v->ctext_bytes,
  297. hash, NULL, sha256, NULL))
  298. || !TEST_mem_eq(hash, sizeof(hash),
  299. expected_ctext_sha256[i],
  300. sizeof(expected_ctext_sha256[i])))
  301. goto done;
  302. /* Check for the expected shared secret */
  303. if (!TEST_mem_eq(bgenkey, bgenkeylen,
  304. expected_shared_secret[i], ML_KEM_SHARED_SECRET_BYTES))
  305. goto done;
  306. /*
  307. * Alice now decapsulates Bob's key. Decap should not need a seed if
  308. * the ciphertext length is good.
  309. */
  310. res = -7;
  311. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, akey, NULL);
  312. if (!TEST_ptr(ctx))
  313. goto done;
  314. if (!TEST_int_gt(EVP_PKEY_decapsulate_init(ctx, NULL), 0))
  315. goto done;
  316. if (!TEST_true(EVP_PKEY_decapsulate(ctx, NULL, &agenkeylen, wrpkey,
  317. wrpkeylen)))
  318. goto done;
  319. if (!TEST_size_t_eq(agenkeylen, ML_KEM_SHARED_SECRET_BYTES))
  320. goto done;
  321. agenkey = OPENSSL_zalloc(agenkeylen);
  322. if (!TEST_ptr(agenkey))
  323. goto done;
  324. if (!TEST_true(EVP_PKEY_decapsulate(ctx, agenkey, &agenkeylen, wrpkey,
  325. wrpkeylen)))
  326. goto done;
  327. /* Hopefully we ended up with a shared key */
  328. if (!TEST_mem_eq(agenkey, agenkeylen, bgenkey, bgenkeylen))
  329. goto done;
  330. res = -8;
  331. /* Now a quick negative test by zeroing the ciphertext */
  332. memset(wrpkey, 0, v->ctext_bytes);
  333. if (!TEST_true(EVP_PKEY_decapsulate(ctx, agenkey, &agenkeylen, wrpkey,
  334. wrpkeylen)))
  335. goto done;
  336. if (!TEST_mem_ne(agenkey, agenkeylen, bgenkey, bgenkeylen))
  337. goto done;
  338. res = -9;
  339. /* Configure decap entropy for a bad ciphertext length */
  340. p = params;
  341. *p = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  342. dec_seed, sizeof(dec_seed));
  343. if (!TEST_true(EVP_RAND_CTX_set_params(pubctx, params)))
  344. goto done;
  345. /* This time decap should fail, and return the decap entropy */
  346. if (!TEST_false(EVP_PKEY_decapsulate(ctx, agenkey, &agenkeylen, wrpkey,
  347. wrpkeylen - 1)))
  348. goto done;
  349. if (!TEST_mem_eq(agenkey, agenkeylen, dec_seed, sizeof(dec_seed)))
  350. goto done;
  351. res = 0;
  352. done:
  353. EVP_PKEY_CTX_free(ctx);
  354. EVP_PKEY_free(akey);
  355. EVP_PKEY_free(bkey);
  356. OPENSSL_free(rawpub);
  357. OPENSSL_free(wrpkey);
  358. OPENSSL_free(agenkey);
  359. OPENSSL_free(bgenkey);
  360. if (res != 0)
  361. ret = res;
  362. }
  363. EVP_MD_free(sha256);
  364. return ret == 0;
  365. }
  366. int setup_tests(void)
  367. {
  368. int test_rand = 0;
  369. OPTION_CHOICE o;
  370. while ((o = opt_next()) != OPT_EOF) {
  371. switch (o) {
  372. case OPT_TEST_RAND:
  373. test_rand = 1;
  374. break;
  375. case OPT_TEST_CASES:
  376. break;
  377. default:
  378. return 0;
  379. }
  380. }
  381. if (test_rand != 0) {
  382. /* Cargo-culted from test/rand_test.c, this may need changes */
  383. if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", "fips=no",
  384. NULL, NULL)))
  385. return 0;
  386. ADD_TEST(test_non_derandomised_ml_kem);
  387. return 1;
  388. }
  389. ADD_TEST(test_ml_kem);
  390. return 1;
  391. }