ml_kem_internal_test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2024-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 <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/core_names.h>
  13. #ifndef OPENSSL_NO_STDIO
  14. # include <stdio.h>
  15. #endif
  16. #include <crypto/ml_kem.h>
  17. #include "testutil.h"
  18. #include "testutil/output.h"
  19. static uint8_t ml_kem_private_entropy[] = {
  20. /* Seed for genkey */
  21. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa, 0x0c, 0x6d, 0x10, 0xe4,
  22. 0xdb, 0x6b, 0x1a, 0xdd, 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  23. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d, 0x86, 0x26, 0xed, 0x79,
  24. 0xd4, 0x51, 0x14, 0x08, 0x00, 0xe0, 0x3b, 0x59, 0xb9, 0x56, 0xf8, 0x21,
  25. 0x0e, 0x55, 0x60, 0x67, 0x40, 0x7d, 0x13, 0xdc, 0x90, 0xfa, 0x9e, 0x8b,
  26. 0x87, 0x2b, 0xfb, 0x8f
  27. };
  28. static uint8_t ml_kem_public_entropy[] = {
  29. /* Seed for encap */
  30. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4, 0x06, 0xc8, 0xfa, 0xe1,
  31. 0x87, 0x4d, 0x7f, 0x13, 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  32. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15,
  33. /* Seed for decap on length error */
  34. 0x4e, 0x6f, 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x64, 0x72, 0x6f, 0x69,
  35. 0x64, 0x73, 0x20, 0x79, 0x6f, 0x75, 0x27, 0x72, 0x65, 0x20, 0x6c, 0x6f,
  36. 0x6f, 0x6b, 0x69, 0x6e, 0x67, 0x20, 0x66, 0x6f
  37. };
  38. static uint8_t ml_kem_expected_rho[3][ML_KEM_RANDOM_BYTES] = {
  39. {
  40. 0x7e, 0xfb, 0x9e, 0x40, 0xc3, 0xbf, 0x0f, 0xf0, 0x43, 0x29, 0x86, 0xae,
  41. 0x4b, 0xc1, 0xa2, 0x42, 0xce, 0x99, 0x21, 0xaa, 0x9e, 0x22, 0x44, 0x88,
  42. 0x19, 0x58, 0x5d, 0xea, 0x30, 0x8e, 0xb0, 0x39
  43. },
  44. {
  45. 0x16, 0x2e, 0xc0, 0x98, 0xa9, 0x00, 0xb1, 0x2d, 0xd8, 0xfa, 0xbb, 0xfb,
  46. 0x3f, 0xe8, 0xcb, 0x1d, 0xc4, 0xe8, 0x31, 0x5f, 0x2a, 0xf0, 0xd3, 0x2f,
  47. 0x00, 0x17, 0xae, 0x13, 0x6e, 0x19, 0xf0, 0x28
  48. },
  49. {
  50. 0x29, 0xb4, 0xf9, 0xf8, 0xcf, 0xba, 0xdf, 0x2e, 0x41, 0x86, 0x9a, 0xbf,
  51. 0xba, 0xd1, 0x07, 0x38, 0xad, 0x04, 0xcc, 0x75, 0x2b, 0xc2, 0x0c, 0x39,
  52. 0x47, 0x46, 0x85, 0x0e, 0x0c, 0x48, 0x47, 0xdb
  53. }
  54. };
  55. static uint8_t ml_kem_expected_ctext_sha256[3][32] = {
  56. {
  57. 0xbc, 0x29, 0xd7, 0xdf, 0x8b, 0xc5, 0x46, 0x5d, 0x98, 0x06, 0x01, 0xd8,
  58. 0x00, 0x25, 0x97, 0x93, 0xe2, 0x60, 0x38, 0x25, 0xa5, 0x72, 0xda, 0x6c,
  59. 0xd1, 0x98, 0xa5, 0x12, 0xcc, 0x6d, 0x1a, 0x34
  60. },
  61. {
  62. 0x36, 0x82, 0x9a, 0x2f, 0x35, 0xcb, 0xf4, 0xde, 0xb6, 0x2c, 0x0a, 0x12,
  63. 0xa1, 0x5c, 0x22, 0xda, 0xe9, 0xf8, 0xd2, 0xc2, 0x52, 0x56, 0x6f, 0xc2,
  64. 0x4f, 0x88, 0xab, 0xe8, 0x05, 0xcb, 0x57, 0x5e
  65. },
  66. {
  67. 0x50, 0x81, 0x36, 0xa1, 0x3f, 0x8a, 0x79, 0x20, 0xe3, 0x43, 0x44, 0x98,
  68. 0xc6, 0x97, 0x5c, 0xbb, 0xab, 0x45, 0x7d, 0x80, 0x93, 0x09, 0xeb, 0x2f,
  69. 0x92, 0x45, 0x3e, 0x74, 0x09, 0x73, 0x82, 0x10
  70. }
  71. };
  72. static uint8_t ml_kem_expected_shared_secret[3][32] = {
  73. {
  74. 0x31, 0x98, 0x39, 0xe8, 0x2a, 0xb6, 0xb2, 0x22, 0xde, 0x7b, 0x61, 0x9e,
  75. 0x80, 0xda, 0x83, 0x91, 0x52, 0x2b, 0xbb, 0x37, 0x67, 0x70, 0x18, 0x49,
  76. 0x4a, 0x47, 0x42, 0xc5, 0x3f, 0x9a, 0xbf, 0xdf
  77. },
  78. {
  79. 0xe7, 0x18, 0x4a, 0x09, 0x75, 0xee, 0x34, 0x70, 0x87, 0x8d, 0x2d, 0x15,
  80. 0x9e, 0xc8, 0x31, 0x29, 0xc8, 0xae, 0xc2, 0x53, 0xd4, 0xee, 0x17, 0xb4,
  81. 0x81, 0x03, 0x11, 0xd1, 0x98, 0xcd, 0x03, 0x68
  82. },
  83. {
  84. 0x48, 0x9d, 0xd1, 0xe9, 0xc2, 0xbe, 0x4a, 0xf3, 0x48, 0x2b, 0xdb, 0x35,
  85. 0xbb, 0x26, 0xce, 0x76, 0x0e, 0x6e, 0x41, 0x4d, 0xa6, 0xec, 0xbe, 0x48,
  86. 0x99, 0x85, 0x74, 0x8a, 0x82, 0x5f, 0x1c, 0xd6
  87. },
  88. };
  89. static int sanity_test(void)
  90. {
  91. static const int alg[3] = {
  92. EVP_PKEY_ML_KEM_512,
  93. EVP_PKEY_ML_KEM_768,
  94. EVP_PKEY_ML_KEM_1024
  95. };
  96. EVP_RAND_CTX *privctx;
  97. EVP_RAND_CTX *pubctx;
  98. EVP_MD *sha256 = EVP_MD_fetch(NULL, "sha256", NULL);
  99. uint8_t *decap_entropy;
  100. int i, ret = 0;
  101. if (!TEST_ptr(sha256))
  102. return 0;
  103. if (!TEST_ptr(privctx = RAND_get0_private(NULL))
  104. || !TEST_ptr(pubctx = RAND_get0_public(NULL))) {
  105. ret = -1;
  106. goto err;
  107. }
  108. decap_entropy = ml_kem_public_entropy + ML_KEM_RANDOM_BYTES;
  109. for (i = 0; i < (int) OSSL_NELEM(alg); ++i) {
  110. OSSL_PARAM params[3];
  111. uint8_t hash[32];
  112. uint8_t shared_secret[ML_KEM_SHARED_SECRET_BYTES];
  113. uint8_t shared_secret2[ML_KEM_SHARED_SECRET_BYTES];
  114. uint8_t *encoded_public_key = NULL;
  115. uint8_t *ciphertext = NULL;
  116. ML_KEM_KEY *private_key = NULL;
  117. ML_KEM_KEY *public_key = NULL;
  118. int ret2 = -1;
  119. unsigned char c;
  120. unsigned int strength = 256;
  121. const ML_KEM_VINFO *v;
  122. /* Configure the private RNG to output just the keygen seed */
  123. params[0] =
  124. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  125. ml_kem_private_entropy,
  126. sizeof(ml_kem_private_entropy));
  127. params[1] =
  128. OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
  129. params[2] = OSSL_PARAM_construct_end();
  130. if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params))) {
  131. ret = -1;
  132. goto err;
  133. }
  134. public_key = ossl_ml_kem_key_new(NULL, NULL, alg[i]);
  135. private_key = ossl_ml_kem_key_new(NULL, NULL, alg[i]);
  136. if (private_key == NULL || public_key == NULL
  137. || (v = ossl_ml_kem_key_vinfo(public_key)) == NULL)
  138. goto done;
  139. encoded_public_key = OPENSSL_malloc(v->pubkey_bytes);
  140. ciphertext = OPENSSL_malloc(v->ctext_bytes);
  141. if (encoded_public_key == NULL || ciphertext == NULL)
  142. goto done;
  143. ret2 = -2;
  144. /* Generate a private key */
  145. if (!ossl_ml_kem_genkey(encoded_public_key, v->pubkey_bytes,
  146. private_key))
  147. goto done;
  148. /* Check that no more entropy is available! */
  149. if (!TEST_int_le(RAND_priv_bytes(&c, 1), 0))
  150. goto done;
  151. ret2 = -3;
  152. /* Check that we got the expected 'rho' value in the ciphertext */
  153. if (!TEST_mem_eq(encoded_public_key + v->vector_bytes,
  154. ML_KEM_RANDOM_BYTES,
  155. ml_kem_expected_rho[i],
  156. ML_KEM_RANDOM_BYTES))
  157. goto done;
  158. ret2 = -4;
  159. /* Create the expected associated public key */
  160. if (!ossl_ml_kem_parse_public_key(encoded_public_key, v->pubkey_bytes,
  161. public_key))
  162. goto done;
  163. /* Configure the public RNG to output the encap and decap seeds */
  164. params[0] =
  165. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  166. ml_kem_public_entropy,
  167. sizeof(ml_kem_public_entropy));
  168. if (!TEST_true(EVP_RAND_CTX_set_params(pubctx, params)))
  169. goto done;
  170. /* encaps - decaps test: validate shared secret equality */
  171. ret2 = -5;
  172. if (!ossl_ml_kem_encap_rand(ciphertext, v->ctext_bytes,
  173. shared_secret, sizeof(shared_secret),
  174. public_key))
  175. goto done;
  176. ret2 = -6;
  177. /* Check the ciphertext hash */
  178. if (!TEST_true(EVP_Digest(ciphertext, v->ctext_bytes,
  179. hash, NULL, sha256, NULL))
  180. || !TEST_mem_eq(hash, sizeof(hash),
  181. ml_kem_expected_ctext_sha256[i],
  182. sizeof(ml_kem_expected_ctext_sha256[i])))
  183. goto done;
  184. /* Check for the expected shared secret */
  185. if (!TEST_mem_eq(shared_secret, sizeof(shared_secret),
  186. ml_kem_expected_shared_secret[i],
  187. ML_KEM_SHARED_SECRET_BYTES))
  188. goto done;
  189. /* Now decapsulate the ciphertext */
  190. ret2 = -7;
  191. if (!ossl_ml_kem_decap(shared_secret2, sizeof(shared_secret2),
  192. ciphertext, v->ctext_bytes, private_key))
  193. goto done;
  194. /* Check for the same shared secret */
  195. if (!TEST_mem_eq(shared_secret, sizeof(shared_secret),
  196. shared_secret2, sizeof(shared_secret2)))
  197. goto done;
  198. ret2 = -8;
  199. /* Now a quick negative test by zeroing the ciphertext */
  200. memset(ciphertext, 0, v->ctext_bytes);
  201. if (!TEST_true(ossl_ml_kem_decap(shared_secret2, sizeof(shared_secret2),
  202. ciphertext, v->ctext_bytes,
  203. private_key)))
  204. goto done;
  205. /* Ensure we have a mismatch */
  206. if (!TEST_mem_ne(shared_secret, sizeof(shared_secret),
  207. shared_secret2, sizeof(shared_secret2)))
  208. goto done;
  209. ret2 = -9;
  210. /*
  211. * Change the ciphertext length, decap should fail, but and consume the
  212. * last batch of entropy to return a fake shared secret, just in case.
  213. */
  214. if (!TEST_false(ossl_ml_kem_decap(shared_secret2, sizeof(shared_secret2),
  215. ciphertext, v->ctext_bytes - 1,
  216. private_key)))
  217. goto done;
  218. if (!TEST_mem_eq(shared_secret2, sizeof(shared_secret2),
  219. decap_entropy, ML_KEM_SHARED_SECRET_BYTES))
  220. goto done;
  221. /* Check that no more entropy is available! */
  222. if (!TEST_int_le(RAND_bytes(&c, 1), 0))
  223. goto done;
  224. ret2 = 0;
  225. done:
  226. if (ret2 != 0)
  227. ret = ret2;
  228. ossl_ml_kem_key_free(private_key);
  229. ossl_ml_kem_key_free(public_key);
  230. OPENSSL_free(encoded_public_key);
  231. OPENSSL_free(ciphertext);
  232. }
  233. err:
  234. EVP_MD_free(sha256);
  235. return ret == 0;
  236. }
  237. int setup_tests(void)
  238. {
  239. if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", "fips=no", NULL, NULL)))
  240. return 0;
  241. ADD_TEST(sanity_test);
  242. return 1;
  243. }