evp_libctx_test.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * Copyright 2020-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. /*
  10. * These tests are setup to load null into the default library context.
  11. * Any tests are expected to use the created 'libctx' to find algorithms.
  12. * The framework runs the tests twice using the 'default' provider or
  13. * 'fips' provider as inputs.
  14. */
  15. /*
  16. * DSA/DH low level APIs are deprecated for public use, but still ok for
  17. * internal use.
  18. */
  19. #include "internal/deprecated.h"
  20. #include <assert.h>
  21. #include <openssl/evp.h>
  22. #include <openssl/provider.h>
  23. #include <openssl/dsa.h>
  24. #include <openssl/dh.h>
  25. #include <openssl/safestack.h>
  26. #include <openssl/core_dispatch.h>
  27. #include <openssl/core_names.h>
  28. #include <openssl/x509.h>
  29. #include <openssl/encoder.h>
  30. #include "testutil.h"
  31. #include "internal/nelem.h"
  32. #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */
  33. static OSSL_LIB_CTX *libctx = NULL;
  34. static OSSL_PROVIDER *nullprov = NULL;
  35. static OSSL_PROVIDER *libprov = NULL;
  36. static STACK_OF(OPENSSL_STRING) *cipher_names = NULL;
  37. static int is_fips = 0;
  38. static int is_fips_lt_3_5 = 0;
  39. typedef enum OPTION_choice {
  40. OPT_ERR = -1,
  41. OPT_EOF = 0,
  42. OPT_CONFIG_FILE,
  43. OPT_PROVIDER_NAME,
  44. OPT_TEST_ENUM
  45. } OPTION_CHOICE;
  46. const OPTIONS *test_get_options(void)
  47. {
  48. static const OPTIONS test_options[] = {
  49. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  50. { "config", OPT_CONFIG_FILE, '<',
  51. "The configuration file to use for the libctx" },
  52. { "provider", OPT_PROVIDER_NAME, 's',
  53. "The provider to load (The default value is 'default')" },
  54. { NULL }
  55. };
  56. return test_options;
  57. }
  58. #ifndef OPENSSL_NO_DH
  59. static const char *getname(int id)
  60. {
  61. const char *name[] = {"p", "q", "g" };
  62. if (id >= 0 && id < 3)
  63. return name[id];
  64. return "?";
  65. }
  66. #endif
  67. static int test_evp_cipher_api_safety(void)
  68. {
  69. int ret = 0;
  70. EVP_CIPHER_CTX *ctx = NULL;
  71. ctx = EVP_CIPHER_CTX_new();
  72. if (!TEST_ptr(ctx))
  73. goto err;
  74. /*
  75. * Ensure that EVP_CIPHER_get_block_size returns 0
  76. * if we haven't initialized the cipher in this context
  77. */
  78. if (!TEST_int_eq(EVP_CIPHER_CTX_get_block_size(ctx), 0))
  79. goto err_free;
  80. /*
  81. * Ensure that EVP_CIPHER_get_iv_length returns 0
  82. * if we haven't initialized the cipher in this context
  83. */
  84. if (!TEST_int_eq(EVP_CIPHER_CTX_get_iv_length(ctx), 0))
  85. goto err_free;
  86. ret = 1;
  87. err_free:
  88. EVP_CIPHER_CTX_free(ctx);
  89. err:
  90. return ret;
  91. }
  92. /*
  93. * We're using some DH specific values in this test, so we skip compilation if
  94. * we're in a no-dh build.
  95. */
  96. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH)
  97. static int test_dsa_param_keygen(int tstid)
  98. {
  99. int ret = 0;
  100. int expected;
  101. EVP_PKEY_CTX *gen_ctx = NULL;
  102. EVP_PKEY *pkey_parm = NULL;
  103. EVP_PKEY *pkey = NULL, *dup_pk = NULL;
  104. DSA *dsa = NULL;
  105. int pind, qind, gind;
  106. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  107. /*
  108. * Just grab some fixed dh p, q, g values for testing,
  109. * these 'safe primes' should not be used normally for dsa *.
  110. */
  111. static const BIGNUM *bn[] = {
  112. &ossl_bignum_dh2048_256_p, &ossl_bignum_dh2048_256_q,
  113. &ossl_bignum_dh2048_256_g
  114. };
  115. /*
  116. * These tests are using bad values for p, q, g by reusing the values.
  117. * A value of 0 uses p, 1 uses q and 2 uses g.
  118. * There are 27 different combinations, with only the 1 valid combination.
  119. */
  120. pind = tstid / 9;
  121. qind = (tstid / 3) % 3;
  122. gind = tstid % 3;
  123. expected = (pind == 0 && qind == 1 && gind == 2);
  124. TEST_note("Testing with (p, q, g) = (%s, %s, %s)\n", getname(pind),
  125. getname(qind), getname(gind));
  126. if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
  127. || !TEST_ptr(dsa = DSA_new())
  128. || !TEST_ptr(p = BN_dup(bn[pind]))
  129. || !TEST_ptr(q = BN_dup(bn[qind]))
  130. || !TEST_ptr(g = BN_dup(bn[gind]))
  131. || !TEST_true(DSA_set0_pqg(dsa, p, q, g)))
  132. goto err;
  133. p = q = g = NULL;
  134. if (!TEST_true(EVP_PKEY_assign_DSA(pkey_parm, dsa)))
  135. goto err;
  136. dsa = NULL;
  137. if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
  138. || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
  139. || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
  140. goto err;
  141. if (expected) {
  142. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
  143. || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
  144. goto err;
  145. }
  146. ret = 1;
  147. err:
  148. EVP_PKEY_free(pkey);
  149. EVP_PKEY_free(dup_pk);
  150. EVP_PKEY_CTX_free(gen_ctx);
  151. EVP_PKEY_free(pkey_parm);
  152. DSA_free(dsa);
  153. BN_free(g);
  154. BN_free(q);
  155. BN_free(p);
  156. return ret;
  157. }
  158. #endif /* OPENSSL_NO_DSA */
  159. #ifndef OPENSSL_NO_DH
  160. static int do_dh_param_keygen(int tstid, const BIGNUM **bn)
  161. {
  162. int ret = 0;
  163. int expected;
  164. EVP_PKEY_CTX *gen_ctx = NULL;
  165. EVP_PKEY *pkey_parm = NULL;
  166. EVP_PKEY *pkey = NULL, *dup_pk = NULL;
  167. DH *dh = NULL;
  168. int pind, qind, gind;
  169. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  170. /*
  171. * These tests are using bad values for p, q, g by reusing the values.
  172. * A value of 0 uses p, 1 uses q and 2 uses g.
  173. * There are 27 different combinations, with only the 1 valid combination.
  174. */
  175. pind = tstid / 9;
  176. qind = (tstid / 3) % 3;
  177. gind = tstid % 3;
  178. expected = (pind == 0 && qind == 1 && gind == 2);
  179. TEST_note("Testing with (p, q, g) = (%s, %s, %s)", getname(pind),
  180. getname(qind), getname(gind));
  181. if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
  182. || !TEST_ptr(dh = DH_new())
  183. || !TEST_ptr(p = BN_dup(bn[pind]))
  184. || !TEST_ptr(q = BN_dup(bn[qind]))
  185. || !TEST_ptr(g = BN_dup(bn[gind]))
  186. || !TEST_true(DH_set0_pqg(dh, p, q, g)))
  187. goto err;
  188. p = q = g = NULL;
  189. if (!TEST_true(EVP_PKEY_assign_DH(pkey_parm, dh)))
  190. goto err;
  191. dh = NULL;
  192. if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
  193. || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
  194. || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
  195. goto err;
  196. if (expected) {
  197. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey))
  198. || !TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), 1))
  199. goto err;
  200. }
  201. ret = 1;
  202. err:
  203. EVP_PKEY_free(pkey);
  204. EVP_PKEY_free(dup_pk);
  205. EVP_PKEY_CTX_free(gen_ctx);
  206. EVP_PKEY_free(pkey_parm);
  207. DH_free(dh);
  208. BN_free(g);
  209. BN_free(q);
  210. BN_free(p);
  211. return ret;
  212. }
  213. /*
  214. * Note that we get the fips186-4 path being run for most of these cases since
  215. * the internal code will detect that the p, q, g does not match a safe prime
  216. * group (Except for when tstid = 5, which sets the correct p, q, g)
  217. */
  218. static int test_dh_safeprime_param_keygen(int tstid)
  219. {
  220. static const BIGNUM *bn[] = {
  221. &ossl_bignum_ffdhe2048_p, &ossl_bignum_ffdhe2048_q,
  222. &ossl_bignum_const_2
  223. };
  224. return do_dh_param_keygen(tstid, bn);
  225. }
  226. static int dhx_cert_load(void)
  227. {
  228. int ret = 0;
  229. X509 *cert = NULL;
  230. BIO *bio = NULL;
  231. static const unsigned char dhx_cert[] = {
  232. 0x30,0x82,0x03,0xff,0x30,0x82,0x02,0xe7,0xa0,0x03,0x02,0x01,0x02,0x02,0x09,0x00,
  233. 0xdb,0xf5,0x4d,0x22,0xa0,0x7a,0x67,0xa6,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,
  234. 0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x30,0x44,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,
  235. 0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x0a,0x0c,
  236. 0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47,0x72,0x6f,0x75,0x70,0x31,0x1d,
  237. 0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54,0x65,0x73,0x74,0x20,0x53,0x2f,
  238. 0x4d,0x49,0x4d,0x45,0x20,0x52,0x53,0x41,0x20,0x52,0x6f,0x6f,0x74,0x30,0x1e,0x17,
  239. 0x0d,0x31,0x33,0x30,0x38,0x30,0x32,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x17,0x0d,
  240. 0x32,0x33,0x30,0x36,0x31,0x31,0x31,0x34,0x34,0x39,0x32,0x39,0x5a,0x30,0x44,0x31,
  241. 0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x4b,0x31,0x16,0x30,0x14,
  242. 0x06,0x03,0x55,0x04,0x0a,0x0c,0x0d,0x4f,0x70,0x65,0x6e,0x53,0x53,0x4c,0x20,0x47,
  243. 0x72,0x6f,0x75,0x70,0x31,0x1d,0x30,0x1b,0x06,0x03,0x55,0x04,0x03,0x0c,0x14,0x54,
  244. 0x65,0x73,0x74,0x20,0x53,0x2f,0x4d,0x49,0x4d,0x45,0x20,0x45,0x45,0x20,0x44,0x48,
  245. 0x20,0x23,0x31,0x30,0x82,0x01,0xb6,0x30,0x82,0x01,0x2b,0x06,0x07,0x2a,0x86,0x48,
  246. 0xce,0x3e,0x02,0x01,0x30,0x82,0x01,0x1e,0x02,0x81,0x81,0x00,0xd4,0x0c,0x4a,0x0c,
  247. 0x04,0x72,0x71,0x19,0xdf,0x59,0x19,0xc5,0xaf,0x44,0x7f,0xca,0x8e,0x2b,0xf0,0x09,
  248. 0xf5,0xd3,0x25,0xb1,0x73,0x16,0x55,0x89,0xdf,0xfd,0x07,0xaf,0x19,0xd3,0x7f,0xd0,
  249. 0x07,0xa2,0xfe,0x3f,0x5a,0xf1,0x01,0xc6,0xf8,0x2b,0xef,0x4e,0x6d,0x03,0x38,0x42,
  250. 0xa1,0x37,0xd4,0x14,0xb4,0x00,0x4a,0xb1,0x86,0x5a,0x83,0xce,0xb9,0x08,0x0e,0xc1,
  251. 0x99,0x27,0x47,0x8d,0x0b,0x85,0xa8,0x82,0xed,0xcc,0x0d,0xb9,0xb0,0x32,0x7e,0xdf,
  252. 0xe8,0xe4,0xf6,0xf6,0xec,0xb3,0xee,0x7a,0x11,0x34,0x65,0x97,0xfc,0x1a,0xb0,0x95,
  253. 0x4b,0x19,0xb9,0xa6,0x1c,0xd9,0x01,0x32,0xf7,0x35,0x7c,0x2d,0x5d,0xfe,0xc1,0x85,
  254. 0x70,0x49,0xf8,0xcc,0x99,0xd0,0xbe,0xf1,0x5a,0x78,0xc8,0x03,0x02,0x81,0x80,0x69,
  255. 0x00,0xfd,0x66,0xf2,0xfc,0x15,0x8b,0x09,0xb8,0xdc,0x4d,0xea,0xaa,0x79,0x55,0xf9,
  256. 0xdf,0x46,0xa6,0x2f,0xca,0x2d,0x8f,0x59,0x2a,0xad,0x44,0xa3,0xc6,0x18,0x2f,0x95,
  257. 0xb6,0x16,0x20,0xe3,0xd3,0xd1,0x8f,0x03,0xce,0x71,0x7c,0xef,0x3a,0xc7,0x44,0x39,
  258. 0x0e,0xe2,0x1f,0xd8,0xd3,0x89,0x2b,0xe7,0x51,0xdc,0x12,0x48,0x4c,0x18,0x4d,0x99,
  259. 0x12,0x06,0xe4,0x17,0x02,0x03,0x8c,0x24,0x05,0x8e,0xa6,0x85,0xf2,0x69,0x1b,0xe1,
  260. 0x6a,0xdc,0xe2,0x04,0x3a,0x01,0x9d,0x64,0xbe,0xfe,0x45,0xf9,0x44,0x18,0x71,0xbd,
  261. 0x2d,0x3e,0x7a,0x6f,0x72,0x7d,0x1a,0x80,0x42,0x57,0xae,0x18,0x6f,0x91,0xd6,0x61,
  262. 0x03,0x8a,0x1c,0x89,0x73,0xc7,0x56,0x41,0x03,0xd3,0xf8,0xed,0x65,0xe2,0x85,0x02,
  263. 0x15,0x00,0x89,0x94,0xab,0x10,0x67,0x45,0x41,0xad,0x63,0xc6,0x71,0x40,0x8d,0x6b,
  264. 0x9e,0x19,0x5b,0xa4,0xc7,0xf5,0x03,0x81,0x84,0x00,0x02,0x81,0x80,0x2f,0x5b,0xde,
  265. 0x72,0x02,0x36,0x6b,0x00,0x5e,0x24,0x7f,0x14,0x2c,0x18,0x52,0x42,0x97,0x4b,0xdb,
  266. 0x6e,0x15,0x50,0x3c,0x45,0x3e,0x25,0xf3,0xb7,0xc5,0x6e,0xe5,0x52,0xe7,0xc4,0xfb,
  267. 0xf4,0xa5,0xf0,0x39,0x12,0x7f,0xbc,0x54,0x1c,0x93,0xb9,0x5e,0xee,0xe9,0x14,0xb0,
  268. 0xdf,0xfe,0xfc,0x36,0xe4,0xf2,0xaf,0xfb,0x13,0xc8,0xdf,0x18,0x94,0x1d,0x40,0xb9,
  269. 0x71,0xdd,0x4c,0x9c,0xa7,0x03,0x52,0x02,0xb5,0xed,0x71,0x80,0x3e,0x23,0xda,0x28,
  270. 0xe5,0xab,0xe7,0x6f,0xf2,0x0a,0x0e,0x00,0x5b,0x7d,0xc6,0x4b,0xd7,0xc7,0xb2,0xc3,
  271. 0xba,0x62,0x7f,0x70,0x28,0xa0,0x9d,0x71,0x13,0x70,0xd1,0x9f,0x32,0x2f,0x3e,0xd2,
  272. 0xcd,0x1b,0xa4,0xc6,0x72,0xa0,0x74,0x5d,0x71,0xef,0x03,0x43,0x6e,0xa3,0x60,0x30,
  273. 0x5e,0x30,0x0c,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x02,0x30,0x00,0x30,
  274. 0x0e,0x06,0x03,0x55,0x1d,0x0f,0x01,0x01,0xff,0x04,0x04,0x03,0x02,0x05,0xe0,0x30,
  275. 0x1d,0x06,0x03,0x55,0x1d,0x0e,0x04,0x16,0x04,0x14,0x0b,0x5a,0x4d,0x5f,0x7d,0x25,
  276. 0xc7,0xf2,0x9d,0xc1,0xaa,0xb7,0x63,0x82,0x2f,0xfa,0x8f,0x32,0xe7,0xc0,0x30,0x1f,
  277. 0x06,0x03,0x55,0x1d,0x23,0x04,0x18,0x30,0x16,0x80,0x14,0xdf,0x7e,0x5e,0x88,0x05,
  278. 0x24,0x33,0x08,0xdd,0x22,0x81,0x02,0x97,0xcc,0x9a,0xb7,0xb1,0x33,0x27,0x30,0x30,
  279. 0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x05,0x05,0x00,0x03,0x82,
  280. 0x01,0x01,0x00,0x5a,0xf2,0x63,0xef,0xd3,0x16,0xd7,0xf5,0xaa,0xdd,0x12,0x00,0x36,
  281. 0x00,0x21,0xa2,0x7b,0x08,0xd6,0x3b,0x9f,0x62,0xac,0x53,0x1f,0xed,0x4c,0xd1,0x15,
  282. 0x34,0x65,0x71,0xee,0x96,0x07,0xa6,0xef,0xb2,0xde,0xd8,0xbb,0x35,0x6e,0x2c,0xe2,
  283. 0xd1,0x26,0xef,0x7e,0x94,0xe2,0x88,0x51,0xa4,0x6c,0xaa,0x27,0x2a,0xd3,0xb6,0xc2,
  284. 0xf7,0xea,0xc3,0x0b,0xa9,0xb5,0x28,0x37,0xa2,0x63,0x08,0xe4,0x88,0xc0,0x1b,0x16,
  285. 0x1b,0xca,0xfd,0x8a,0x07,0x32,0x29,0xa7,0x53,0xb5,0x2d,0x30,0xe4,0xf5,0x16,0xc3,
  286. 0xe3,0xc2,0x4c,0x30,0x5d,0x35,0x80,0x1c,0xa2,0xdb,0xe3,0x4b,0x51,0x0d,0x4c,0x60,
  287. 0x5f,0xb9,0x46,0xac,0xa8,0x46,0xa7,0x32,0xa7,0x9c,0x76,0xf8,0xe9,0xb5,0x19,0xe2,
  288. 0x0c,0xe1,0x0f,0xc6,0x46,0xe2,0x38,0xa7,0x87,0x72,0x6d,0x6c,0xbc,0x88,0x2f,0x9d,
  289. 0x2d,0xe5,0xd0,0x7d,0x1e,0xc7,0x5d,0xf8,0x7e,0xb4,0x0b,0xa6,0xf9,0x6c,0xe3,0x7c,
  290. 0xb2,0x70,0x6e,0x75,0x9b,0x1e,0x63,0xe1,0x4d,0xb2,0x81,0xd3,0x55,0x38,0x94,0x1a,
  291. 0x7a,0xfa,0xbf,0x01,0x18,0x70,0x2d,0x35,0xd3,0xe3,0x10,0x7a,0x9a,0xa7,0x8f,0xf3,
  292. 0xbd,0x56,0x55,0x5e,0xd8,0xbd,0x4e,0x16,0x76,0xd0,0x48,0x4c,0xf9,0x51,0x54,0xdf,
  293. 0x2d,0xb0,0xc9,0xaa,0x5e,0x42,0x38,0x50,0xbf,0x0f,0xc0,0xd9,0x84,0x44,0x4b,0x42,
  294. 0x24,0xec,0x14,0xa3,0xde,0x11,0xdf,0x58,0x7f,0xc2,0x4d,0xb2,0xd5,0x42,0x78,0x6e,
  295. 0x52,0x3e,0xad,0xc3,0x5f,0x04,0xc4,0xe6,0x31,0xaa,0x81,0x06,0x8b,0x13,0x4b,0x3c,
  296. 0x0e,0x6a,0xb1
  297. };
  298. if (!TEST_ptr(bio = BIO_new_mem_buf(dhx_cert, sizeof(dhx_cert)))
  299. || !TEST_ptr(cert = X509_new_ex(libctx, NULL))
  300. || !TEST_ptr(d2i_X509_bio(bio, &cert)))
  301. goto err;
  302. ret = 1;
  303. err:
  304. X509_free(cert);
  305. BIO_free(bio);
  306. return ret;
  307. }
  308. #endif /* OPENSSL_NO_DH */
  309. static int test_cipher_reinit(int test_id)
  310. {
  311. int ret = 0, diff, ccm, siv, no_null_key;
  312. int out1_len = 0, out2_len = 0, out3_len = 0;
  313. EVP_CIPHER *cipher = NULL;
  314. EVP_CIPHER_CTX *ctx = NULL;
  315. unsigned char out1[256];
  316. unsigned char out2[256];
  317. unsigned char out3[256];
  318. unsigned char in[16] = {
  319. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  320. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
  321. };
  322. unsigned char key[64] = {
  323. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  324. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  325. 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  326. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  327. 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  328. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  329. 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  330. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  331. };
  332. unsigned char iv[16] = {
  333. 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
  334. 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
  335. };
  336. const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id);
  337. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  338. goto err;
  339. TEST_note("Fetching %s\n", name);
  340. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL)))
  341. goto err;
  342. /* ccm fails on the second update - this matches OpenSSL 1_1_1 behaviour */
  343. ccm = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE);
  344. /* siv cannot be called with NULL key as the iv is irrelevant */
  345. siv = (EVP_CIPHER_get_mode(cipher) == EVP_CIPH_SIV_MODE);
  346. /*
  347. * Skip init call with a null key for RC4 as the stream cipher does not
  348. * handle reinit (1.1.1 behaviour).
  349. */
  350. no_null_key = EVP_CIPHER_is_a(cipher, "RC4")
  351. || EVP_CIPHER_is_a(cipher, "RC4-40")
  352. || EVP_CIPHER_is_a(cipher, "RC4-HMAC-MD5");
  353. /* DES3-WRAP uses random every update - so it will give a different value */
  354. diff = EVP_CIPHER_is_a(cipher, "DES3-WRAP");
  355. if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
  356. || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, sizeof(in)))
  357. || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
  358. || !TEST_int_eq(EVP_EncryptUpdate(ctx, out2, &out2_len, in, sizeof(in)),
  359. ccm ? 0 : 1)
  360. || (!no_null_key
  361. && (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
  362. || !TEST_int_eq(EVP_EncryptUpdate(ctx, out3, &out3_len, in, sizeof(in)),
  363. ccm || siv ? 0 : 1))))
  364. goto err;
  365. if (ccm == 0) {
  366. if (diff) {
  367. if (!TEST_mem_ne(out1, out1_len, out2, out2_len)
  368. || !TEST_mem_ne(out1, out1_len, out3, out3_len)
  369. || !TEST_mem_ne(out2, out2_len, out3, out3_len))
  370. goto err;
  371. } else {
  372. if (!TEST_mem_eq(out1, out1_len, out2, out2_len)
  373. || (!siv && !no_null_key && !TEST_mem_eq(out1, out1_len, out3, out3_len)))
  374. goto err;
  375. }
  376. }
  377. ret = 1;
  378. err:
  379. EVP_CIPHER_free(cipher);
  380. EVP_CIPHER_CTX_free(ctx);
  381. return ret;
  382. }
  383. /*
  384. * This test only uses a partial block (half the block size) of input for each
  385. * EVP_EncryptUpdate() in order to test that the second init/update is not using
  386. * a leftover buffer from the first init/update.
  387. * Note: some ciphers don't need a full block to produce output.
  388. */
  389. static int test_cipher_reinit_partialupdate(int test_id)
  390. {
  391. int ret = 0, in_len;
  392. int out1_len = 0, out2_len = 0, out3_len = 0;
  393. EVP_CIPHER *cipher = NULL;
  394. EVP_CIPHER_CTX *ctx = NULL;
  395. unsigned char out1[256];
  396. unsigned char out2[256];
  397. unsigned char out3[256];
  398. static const unsigned char in[32] = {
  399. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  400. 0xba, 0xbe, 0xba, 0xbe, 0x00, 0x00, 0xba, 0xbe,
  401. 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  402. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  403. };
  404. static const unsigned char key[64] = {
  405. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  406. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  407. 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  408. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  409. 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  410. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  411. 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  412. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  413. };
  414. static const unsigned char iv[16] = {
  415. 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
  416. 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
  417. };
  418. const char *name = sk_OPENSSL_STRING_value(cipher_names, test_id);
  419. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  420. goto err;
  421. TEST_note("Fetching %s\n", name);
  422. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL)))
  423. goto err;
  424. in_len = EVP_CIPHER_get_block_size(cipher);
  425. if (!TEST_int_gt(in_len, 0))
  426. goto err;
  427. if (in_len > 1)
  428. in_len /= 2;
  429. /* skip any ciphers that don't allow partial updates */
  430. if (((EVP_CIPHER_get_flags(cipher)
  431. & (EVP_CIPH_FLAG_CTS | EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) != 0)
  432. || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_CCM_MODE
  433. || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE
  434. || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_WRAP_MODE) {
  435. ret = 1;
  436. goto err;
  437. }
  438. if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
  439. || !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, in_len))
  440. || !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
  441. || !TEST_true(EVP_EncryptUpdate(ctx, out2, &out2_len, in, in_len)))
  442. goto err;
  443. if (EVP_CIPHER_get_iv_length(cipher) != 0)
  444. if (!TEST_mem_eq(out1, out1_len, out2, out2_len))
  445. goto err;
  446. if (EVP_CIPHER_get_mode(cipher) != EVP_CIPH_SIV_MODE) {
  447. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv))
  448. || !TEST_true(EVP_EncryptUpdate(ctx, out3, &out3_len, in, in_len)))
  449. goto err;
  450. if (EVP_CIPHER_get_iv_length(cipher) != 0)
  451. if (!TEST_mem_eq(out1, out1_len, out3, out3_len))
  452. goto err;
  453. }
  454. ret = 1;
  455. err:
  456. EVP_CIPHER_free(cipher);
  457. EVP_CIPHER_CTX_free(ctx);
  458. return ret;
  459. }
  460. static int name_cmp(const char * const *a, const char * const *b)
  461. {
  462. return OPENSSL_strcasecmp(*a, *b);
  463. }
  464. static void collect_cipher_names(EVP_CIPHER *cipher, void *cipher_names_list)
  465. {
  466. STACK_OF(OPENSSL_STRING) *names = cipher_names_list;
  467. const char *name = EVP_CIPHER_get0_name(cipher);
  468. char *namedup = NULL;
  469. assert(name != NULL);
  470. /* the cipher will be freed after returning, strdup is needed */
  471. if ((namedup = OPENSSL_strdup(name)) != NULL
  472. && !sk_OPENSSL_STRING_push(names, namedup))
  473. OPENSSL_free(namedup);
  474. }
  475. static int rsa_keygen(int bits, EVP_PKEY **pub, EVP_PKEY **priv)
  476. {
  477. int ret = 0;
  478. unsigned char *pub_der = NULL;
  479. const unsigned char *pp = NULL;
  480. size_t len = 0;
  481. OSSL_ENCODER_CTX *ectx = NULL;
  482. if (!TEST_ptr(*priv = EVP_PKEY_Q_keygen(libctx, NULL, "RSA", (size_t)bits))
  483. || !TEST_ptr(ectx =
  484. OSSL_ENCODER_CTX_new_for_pkey(*priv,
  485. EVP_PKEY_PUBLIC_KEY,
  486. "DER", "type-specific",
  487. NULL))
  488. || !TEST_true(OSSL_ENCODER_to_data(ectx, &pub_der, &len)))
  489. goto err;
  490. pp = pub_der;
  491. if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_RSA, pub, &pp, len)))
  492. goto err;
  493. ret = 1;
  494. err:
  495. OSSL_ENCODER_CTX_free(ectx);
  496. OPENSSL_free(pub_der);
  497. return ret;
  498. }
  499. static int kem_rsa_gen_recover(void)
  500. {
  501. int ret = 0;
  502. EVP_PKEY *pub = NULL;
  503. EVP_PKEY *priv = NULL;
  504. EVP_PKEY_CTX *sctx = NULL, *rctx = NULL, *dctx = NULL;
  505. unsigned char secret[256] = { 0, };
  506. unsigned char ct[256] = { 0, };
  507. unsigned char unwrap[256] = { 0, };
  508. size_t ctlen = 0, unwraplen = 0, secretlen = 0;
  509. int bits = 2048;
  510. ret = TEST_true(rsa_keygen(bits, &pub, &priv))
  511. && TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL))
  512. && TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), 1)
  513. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(sctx, "RSASVE"), 1)
  514. && TEST_ptr(dctx = EVP_PKEY_CTX_dup(sctx))
  515. /* Test that providing a NULL wrappedlen fails */
  516. && TEST_int_eq(EVP_PKEY_encapsulate(dctx, NULL, NULL, NULL, NULL), 0)
  517. && TEST_int_eq(EVP_PKEY_encapsulate(dctx, NULL, &ctlen, NULL,
  518. &secretlen), 1)
  519. && TEST_int_eq(ctlen, secretlen)
  520. && TEST_int_eq(ctlen, bits / 8)
  521. && TEST_int_eq(EVP_PKEY_encapsulate(dctx, ct, &ctlen, secret,
  522. &secretlen), 1)
  523. && TEST_ptr(rctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL))
  524. && TEST_int_eq(EVP_PKEY_decapsulate_init(rctx, NULL), 1)
  525. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(rctx, "RSASVE"), 1)
  526. /* Test that providing a NULL unwrappedlen fails */
  527. && TEST_int_eq(EVP_PKEY_decapsulate(rctx, NULL, NULL, ct, ctlen), 0)
  528. && TEST_int_eq(EVP_PKEY_decapsulate(rctx, NULL, &unwraplen,
  529. ct, ctlen), 1)
  530. && TEST_int_eq(EVP_PKEY_decapsulate(rctx, unwrap, &unwraplen,
  531. ct, ctlen), 1)
  532. && TEST_mem_eq(unwrap, unwraplen, secret, secretlen);
  533. /* Test that providing a too short unwrapped/ctlen fails */
  534. if (fips_provider_version_match(libctx, ">=3.4.0")) {
  535. ctlen = 1;
  536. if (!TEST_int_eq(EVP_PKEY_encapsulate(dctx, ct, &ctlen, secret,
  537. &secretlen), 0))
  538. ret = 0;
  539. unwraplen = 1;
  540. if (!TEST_int_eq(EVP_PKEY_decapsulate(rctx, unwrap, &unwraplen, ct,
  541. ctlen), 0))
  542. ret = 0;
  543. }
  544. EVP_PKEY_free(pub);
  545. EVP_PKEY_free(priv);
  546. EVP_PKEY_CTX_free(rctx);
  547. EVP_PKEY_CTX_free(dctx);
  548. EVP_PKEY_CTX_free(sctx);
  549. return ret;
  550. }
  551. #ifndef OPENSSL_NO_DES
  552. /*
  553. * This test makes sure that EVP_CIPHER_CTX_rand_key() works correctly
  554. * For fips mode this code would produce an error if the flag is not set.
  555. */
  556. static int test_cipher_tdes_randkey(void)
  557. {
  558. int ret;
  559. EVP_CIPHER_CTX *ctx = NULL;
  560. EVP_CIPHER *tdes_cipher = NULL, *aes_cipher = NULL;
  561. unsigned char key[24] = { 0 };
  562. ret = TEST_ptr(aes_cipher = EVP_CIPHER_fetch(libctx, "AES-256-CBC", NULL))
  563. && TEST_int_eq(EVP_CIPHER_get_flags(aes_cipher) & EVP_CIPH_RAND_KEY, 0)
  564. && TEST_ptr(tdes_cipher = EVP_CIPHER_fetch(libctx, "DES-EDE3-CBC", NULL))
  565. && TEST_int_ne(EVP_CIPHER_get_flags(tdes_cipher) & EVP_CIPH_RAND_KEY, 0)
  566. && TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  567. && TEST_true(EVP_CipherInit_ex(ctx, tdes_cipher, NULL, NULL, NULL, 1))
  568. && TEST_int_gt(EVP_CIPHER_CTX_rand_key(ctx, key), 0);
  569. EVP_CIPHER_CTX_free(ctx);
  570. EVP_CIPHER_free(tdes_cipher);
  571. EVP_CIPHER_free(aes_cipher);
  572. return ret;
  573. }
  574. #endif /* OPENSSL_NO_DES */
  575. static int kem_rsa_params(void)
  576. {
  577. int ret = 0;
  578. EVP_PKEY *pub = NULL;
  579. EVP_PKEY *priv = NULL;
  580. EVP_PKEY_CTX *pubctx = NULL, *privctx = NULL;
  581. unsigned char secret[256] = { 0, };
  582. unsigned char ct[256] = { 0, };
  583. size_t ctlen = 0, secretlen = 0;
  584. ret = TEST_true(rsa_keygen(2048, &pub, &priv))
  585. && TEST_ptr(pubctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, NULL))
  586. && TEST_ptr(privctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, NULL))
  587. /* Test setting kem op before the init fails */
  588. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), -2)
  589. /* Test NULL ctx passed */
  590. && TEST_int_eq(EVP_PKEY_encapsulate_init(NULL, NULL), 0)
  591. && TEST_int_eq(EVP_PKEY_encapsulate(NULL, NULL, NULL, NULL, NULL), 0)
  592. && TEST_int_eq(EVP_PKEY_decapsulate_init(NULL, NULL), 0)
  593. && TEST_int_eq(EVP_PKEY_decapsulate(NULL, NULL, NULL, NULL, 0), 0)
  594. /* Test Invalid operation */
  595. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), -1)
  596. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, NULL, 0), 0)
  597. /* Wrong key component - no secret should be returned on failure */
  598. && TEST_int_eq(EVP_PKEY_decapsulate_init(pubctx, NULL), 1)
  599. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1)
  600. && TEST_int_eq(EVP_PKEY_decapsulate(pubctx, secret, &secretlen, ct,
  601. sizeof(ct)), 0)
  602. && TEST_uchar_eq(secret[0], 0)
  603. /* Unless newer FIPS, test encapsulate fails when the mode is not set. */
  604. && TEST_int_eq(EVP_PKEY_encapsulate_init(pubctx, NULL), 1)
  605. && (!is_fips_lt_3_5 ||
  606. TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, &secretlen), -2))
  607. /* Test setting a bad kem ops fail */
  608. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSA"), 0)
  609. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, NULL), 0)
  610. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, "RSASVE"), 0)
  611. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(NULL, NULL), 0)
  612. /* Test secretlen is optional */
  613. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(pubctx, "RSASVE"), 1)
  614. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1)
  615. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, secret, NULL), 1)
  616. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1)
  617. /* Test outlen is optional */
  618. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1)
  619. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, &secretlen), 1)
  620. /* test that either len must be set if out is NULL */
  621. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, NULL), 0)
  622. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, NULL), 1)
  623. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, NULL, NULL, &secretlen), 1)
  624. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, NULL, &ctlen, NULL, &secretlen), 1)
  625. /* Secret buffer should be set if there is an output buffer */
  626. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, &ctlen, NULL, NULL), 0)
  627. /* Test that lengths are optional if ct is not NULL */
  628. && TEST_int_eq(EVP_PKEY_encapsulate(pubctx, ct, NULL, secret, NULL), 1)
  629. /* Pass if secret or secret length are not NULL */
  630. && TEST_int_eq(EVP_PKEY_decapsulate_init(privctx, NULL), 1)
  631. && TEST_int_eq(EVP_PKEY_CTX_set_kem_op(privctx, "RSASVE"), 1)
  632. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, NULL, ct, sizeof(ct)), 1)
  633. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, &secretlen, ct, sizeof(ct)), 1)
  634. && TEST_int_eq(secretlen, 256)
  635. /* Fail if passed NULL arguments */
  636. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, NULL, NULL, ct, sizeof(ct)), 0)
  637. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, 0), 0)
  638. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, NULL, sizeof(ct)), 0)
  639. && TEST_int_eq(EVP_PKEY_decapsulate(privctx, secret, &secretlen, ct, 0), 0);
  640. EVP_PKEY_free(pub);
  641. EVP_PKEY_free(priv);
  642. EVP_PKEY_CTX_free(pubctx);
  643. EVP_PKEY_CTX_free(privctx);
  644. return ret;
  645. }
  646. #ifndef OPENSSL_NO_DH
  647. static EVP_PKEY *gen_dh_key(void)
  648. {
  649. EVP_PKEY_CTX *gctx = NULL;
  650. EVP_PKEY *pkey = NULL;
  651. OSSL_PARAM params[2];
  652. params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
  653. params[1] = OSSL_PARAM_construct_end();
  654. if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL))
  655. || !TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
  656. || !TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
  657. || !TEST_true(EVP_PKEY_keygen(gctx, &pkey)))
  658. goto err;
  659. err:
  660. EVP_PKEY_CTX_free(gctx);
  661. return pkey;
  662. }
  663. /* Fail if we try to use a dh key */
  664. static int kem_invalid_keytype(void)
  665. {
  666. int ret = 0;
  667. EVP_PKEY *key = NULL;
  668. EVP_PKEY_CTX *sctx = NULL;
  669. if (!TEST_ptr(key = gen_dh_key()))
  670. goto done;
  671. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL)))
  672. goto done;
  673. if (!TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, NULL), -2))
  674. goto done;
  675. ret = 1;
  676. done:
  677. EVP_PKEY_free(key);
  678. EVP_PKEY_CTX_free(sctx);
  679. return ret;
  680. }
  681. #endif /* OPENSSL_NO_DH */
  682. int setup_tests(void)
  683. {
  684. const char *prov_name = "default";
  685. char *config_file = NULL;
  686. OPTION_CHOICE o;
  687. while ((o = opt_next()) != OPT_EOF) {
  688. switch (o) {
  689. case OPT_PROVIDER_NAME:
  690. prov_name = opt_arg();
  691. break;
  692. case OPT_CONFIG_FILE:
  693. config_file = opt_arg();
  694. break;
  695. case OPT_TEST_CASES:
  696. break;
  697. default:
  698. case OPT_ERR:
  699. return 0;
  700. }
  701. }
  702. if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name))
  703. return 0;
  704. ADD_TEST(test_evp_cipher_api_safety);
  705. if (strcmp(prov_name, "fips") == 0)
  706. is_fips = 1;
  707. is_fips_lt_3_5 = is_fips && fips_provider_version_lt(libctx, 3, 5, 0);
  708. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DH)
  709. if (!is_fips || fips_provider_version_lt(libctx, 3, 4, 0))
  710. ADD_ALL_TESTS(test_dsa_param_keygen, 3 * 3 * 3);
  711. #endif
  712. #ifndef OPENSSL_NO_DH
  713. ADD_ALL_TESTS(test_dh_safeprime_param_keygen, 3 * 3 * 3);
  714. ADD_TEST(dhx_cert_load);
  715. #endif
  716. if (!TEST_ptr(cipher_names = sk_OPENSSL_STRING_new(name_cmp)))
  717. return 0;
  718. EVP_CIPHER_do_all_provided(libctx, collect_cipher_names, cipher_names);
  719. ADD_ALL_TESTS(test_cipher_reinit, sk_OPENSSL_STRING_num(cipher_names));
  720. ADD_ALL_TESTS(test_cipher_reinit_partialupdate,
  721. sk_OPENSSL_STRING_num(cipher_names));
  722. ADD_TEST(kem_rsa_gen_recover);
  723. ADD_TEST(kem_rsa_params);
  724. #ifndef OPENSSL_NO_DH
  725. ADD_TEST(kem_invalid_keytype);
  726. #endif
  727. #ifndef OPENSSL_NO_DES
  728. ADD_TEST(test_cipher_tdes_randkey);
  729. #endif
  730. return 1;
  731. }
  732. /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
  733. static void string_free(char *m)
  734. {
  735. OPENSSL_free(m);
  736. }
  737. void cleanup_tests(void)
  738. {
  739. sk_OPENSSL_STRING_pop_free(cipher_names, string_free);
  740. OSSL_PROVIDER_unload(libprov);
  741. OSSL_LIB_CTX_free(libctx);
  742. OSSL_PROVIDER_unload(nullprov);
  743. }