acvp_test.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * Copyright 2020-2021 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. * A set of tests demonstrating uses cases for CAVS/ACVP testing.
  11. *
  12. * For examples of testing KDF's, Digests, KeyAgreement & DRBG's refer to
  13. * providers/fips/self_test_kats.c
  14. */
  15. #include <string.h>
  16. #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
  17. #include <openssl/core_names.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/ec.h>
  20. #include <openssl/dh.h>
  21. #include <openssl/dsa.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/param_build.h>
  24. #include <openssl/provider.h>
  25. #include <openssl/self_test.h>
  26. #include "testutil.h"
  27. #include "testutil/output.h"
  28. #include "acvp_test.inc"
  29. #include "internal/nelem.h"
  30. typedef enum OPTION_choice {
  31. OPT_ERR = -1,
  32. OPT_EOF = 0,
  33. OPT_CONFIG_FILE,
  34. OPT_TEST_ENUM
  35. } OPTION_CHOICE;
  36. typedef struct st_args {
  37. int enable;
  38. int called;
  39. } SELF_TEST_ARGS;
  40. static OSSL_PROVIDER *prov_null = NULL;
  41. static OSSL_LIB_CTX *libctx = NULL;
  42. static SELF_TEST_ARGS self_test_args = { 0 };
  43. static OSSL_CALLBACK self_test_events;
  44. const OPTIONS *test_get_options(void)
  45. {
  46. static const OPTIONS test_options[] = {
  47. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  48. { "config", OPT_CONFIG_FILE, '<',
  49. "The configuration file to use for the libctx" },
  50. { NULL }
  51. };
  52. return test_options;
  53. }
  54. static int pkey_get_bn_bytes(EVP_PKEY *pkey, const char *name,
  55. unsigned char **out, size_t *out_len)
  56. {
  57. unsigned char *buf = NULL;
  58. BIGNUM *bn = NULL;
  59. int sz;
  60. if (!EVP_PKEY_get_bn_param(pkey, name, &bn))
  61. goto err;
  62. sz = BN_num_bytes(bn);
  63. buf = OPENSSL_zalloc(sz);
  64. if (buf == NULL)
  65. goto err;
  66. if (!BN_bn2binpad(bn, buf, sz))
  67. goto err;
  68. *out_len = sz;
  69. *out = buf;
  70. BN_free(bn);
  71. return 1;
  72. err:
  73. OPENSSL_free(buf);
  74. BN_free(bn);
  75. return 0;
  76. }
  77. static int sig_gen(EVP_PKEY *pkey, OSSL_PARAM *params, const char *digest_name,
  78. const unsigned char *msg, size_t msg_len,
  79. unsigned char **sig_out, size_t *sig_out_len)
  80. {
  81. int ret = 0;
  82. EVP_MD_CTX *md_ctx = NULL;
  83. unsigned char *sig = NULL;
  84. size_t sig_len;
  85. size_t sz = EVP_PKEY_size(pkey);
  86. if (!TEST_ptr(sig = OPENSSL_malloc(sz))
  87. || !TEST_ptr(md_ctx = EVP_MD_CTX_new())
  88. || !TEST_int_eq(EVP_DigestSignInit_ex(md_ctx, NULL, digest_name, libctx,
  89. NULL, pkey, NULL), 1)
  90. || !TEST_int_gt(EVP_DigestSign(md_ctx, sig, &sig_len, msg, msg_len), 0))
  91. goto err;
  92. *sig_out = sig;
  93. *sig_out_len = sig_len;
  94. sig = NULL;
  95. ret = 1;
  96. err:
  97. OPENSSL_free(sig);
  98. EVP_MD_CTX_free(md_ctx);
  99. return ret;
  100. }
  101. #ifndef OPENSSL_NO_EC
  102. static int ecdsa_keygen_test(int id)
  103. {
  104. int ret = 0;
  105. EVP_PKEY_CTX *ctx = NULL;
  106. EVP_PKEY *pkey = NULL;
  107. unsigned char *priv = NULL;
  108. unsigned char *pubx = NULL, *puby = NULL;
  109. size_t priv_len = 0, pubx_len = 0, puby_len = 0;
  110. const struct ecdsa_keygen_st *tst = &ecdsa_keygen_data[id];
  111. self_test_args.called = 0;
  112. self_test_args.enable = 1;
  113. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL))
  114. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  115. || !TEST_true(EVP_PKEY_CTX_set_group_name(ctx, tst->curve_name))
  116. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0)
  117. || !TEST_int_ge(self_test_args.called, 3)
  118. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &priv,
  119. &priv_len))
  120. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &pubx,
  121. &pubx_len))
  122. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &puby,
  123. &puby_len)))
  124. goto err;
  125. test_output_memory("qy", puby, puby_len);
  126. test_output_memory("qx", pubx, pubx_len);
  127. test_output_memory("d", priv, priv_len);
  128. ret = 1;
  129. err:
  130. self_test_args.enable = 0;
  131. self_test_args.called = 0;
  132. OPENSSL_clear_free(priv, priv_len);
  133. OPENSSL_free(pubx);
  134. OPENSSL_free(puby);
  135. EVP_PKEY_free(pkey);
  136. EVP_PKEY_CTX_free(ctx);
  137. return ret;
  138. }
  139. static int ecdsa_create_pkey(EVP_PKEY **pkey, const char *curve_name,
  140. const unsigned char *pub, size_t pub_len,
  141. int expected)
  142. {
  143. int ret = 0;
  144. EVP_PKEY_CTX *ctx = NULL;
  145. OSSL_PARAM_BLD *bld = NULL;
  146. OSSL_PARAM *params = NULL;
  147. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  148. || (curve_name != NULL
  149. && !TEST_true(OSSL_PARAM_BLD_push_utf8_string(
  150. bld, OSSL_PKEY_PARAM_GROUP_NAME, curve_name, 0) > 0))
  151. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  152. OSSL_PKEY_PARAM_PUB_KEY,
  153. pub, pub_len) > 0)
  154. || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  155. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL))
  156. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  157. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_PUBLIC_KEY,
  158. params), expected))
  159. goto err;
  160. ret = 1;
  161. err:
  162. OSSL_PARAM_free(params);
  163. OSSL_PARAM_BLD_free(bld);
  164. EVP_PKEY_CTX_free(ctx);
  165. return ret;
  166. }
  167. static int ecdsa_pub_verify_test(int id)
  168. {
  169. const struct ecdsa_pub_verify_st *tst = &ecdsa_pv_data[id];
  170. int ret = 0;
  171. EVP_PKEY_CTX *key_ctx = NULL;
  172. EVP_PKEY *pkey = NULL;
  173. if (!TEST_true(ecdsa_create_pkey(&pkey, tst->curve_name,
  174. tst->pub, tst->pub_len, tst->pass)))
  175. goto err;
  176. if (tst->pass) {
  177. if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, ""))
  178. || !TEST_int_eq(EVP_PKEY_public_check(key_ctx), tst->pass))
  179. goto err;
  180. }
  181. ret = 1;
  182. err:
  183. EVP_PKEY_free(pkey);
  184. EVP_PKEY_CTX_free(key_ctx);
  185. return ret;
  186. }
  187. /* Extract r and s from a ecdsa signature */
  188. static int get_ecdsa_sig_rs_bytes(const unsigned char *sig, size_t sig_len,
  189. unsigned char **r, unsigned char **s,
  190. size_t *rlen, size_t *slen)
  191. {
  192. int ret = 0;
  193. unsigned char *rbuf = NULL, *sbuf = NULL;
  194. size_t r1_len, s1_len;
  195. const BIGNUM *r1, *s1;
  196. ECDSA_SIG *sign = d2i_ECDSA_SIG(NULL, &sig, sig_len);
  197. if (sign == NULL)
  198. return 0;
  199. r1 = ECDSA_SIG_get0_r(sign);
  200. s1 = ECDSA_SIG_get0_s(sign);
  201. if (r1 == NULL || s1 == NULL)
  202. return 0;
  203. r1_len = BN_num_bytes(r1);
  204. s1_len = BN_num_bytes(s1);
  205. rbuf = OPENSSL_zalloc(r1_len);
  206. sbuf = OPENSSL_zalloc(s1_len);
  207. if (rbuf == NULL || sbuf == NULL)
  208. goto err;
  209. if (BN_bn2binpad(r1, rbuf, r1_len) <= 0)
  210. goto err;
  211. if (BN_bn2binpad(s1, sbuf, s1_len) <= 0)
  212. goto err;
  213. *r = rbuf;
  214. *s = sbuf;
  215. *rlen = r1_len;
  216. *slen = s1_len;
  217. ret = 1;
  218. err:
  219. if (ret == 0) {
  220. OPENSSL_free(rbuf);
  221. OPENSSL_free(sbuf);
  222. }
  223. ECDSA_SIG_free(sign);
  224. return ret;
  225. }
  226. static int ecdsa_siggen_test(int id)
  227. {
  228. int ret = 0;
  229. EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
  230. EVP_PKEY *pkey = NULL;
  231. size_t sig_len = 0, rlen = 0, slen = 0;
  232. unsigned char *sig = NULL;
  233. unsigned char *r = NULL, *s = NULL;
  234. const struct ecdsa_siggen_st *tst = &ecdsa_siggen_data[id];
  235. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL))
  236. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  237. || !TEST_true(EVP_PKEY_CTX_set_group_name(ctx, tst->curve_name))
  238. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
  239. goto err;
  240. if (!TEST_true(sig_gen(pkey, NULL, tst->digest_alg, tst->msg, tst->msg_len,
  241. &sig, &sig_len))
  242. || !TEST_true(get_ecdsa_sig_rs_bytes(sig, sig_len, &r, &s, &rlen, &slen)))
  243. goto err;
  244. test_output_memory("r", r, rlen);
  245. test_output_memory("s", s, slen);
  246. ret = 1;
  247. err:
  248. OPENSSL_free(r);
  249. OPENSSL_free(s);
  250. OPENSSL_free(sig);
  251. EVP_PKEY_free(pkey);
  252. EVP_PKEY_CTX_free(key_ctx);
  253. EVP_PKEY_CTX_free(ctx);
  254. return ret;
  255. }
  256. static int ecdsa_sigver_test(int id)
  257. {
  258. int ret = 0;
  259. EVP_MD_CTX *md_ctx = NULL;
  260. EVP_PKEY *pkey = NULL;
  261. ECDSA_SIG *sign = NULL;
  262. size_t sig_len;
  263. unsigned char *sig = NULL;
  264. BIGNUM *rbn = NULL, *sbn = NULL;
  265. const struct ecdsa_sigver_st *tst = &ecdsa_sigver_data[id];
  266. if (!TEST_true(ecdsa_create_pkey(&pkey, tst->curve_name,
  267. tst->pub, tst->pub_len, 1)))
  268. goto err;
  269. if (!TEST_ptr(sign = ECDSA_SIG_new())
  270. || !TEST_ptr(rbn = BN_bin2bn(tst->r, tst->r_len, NULL))
  271. || !TEST_ptr(sbn = BN_bin2bn(tst->s, tst->s_len, NULL))
  272. || !TEST_true(ECDSA_SIG_set0(sign, rbn, sbn)))
  273. goto err;
  274. rbn = sbn = NULL;
  275. ret = TEST_int_gt((sig_len = i2d_ECDSA_SIG(sign, &sig)), 0)
  276. && TEST_ptr(md_ctx = EVP_MD_CTX_new())
  277. && TEST_true(EVP_DigestVerifyInit_ex(md_ctx, NULL, tst->digest_alg,
  278. libctx, NULL, pkey, NULL)
  279. && TEST_int_eq(EVP_DigestVerify(md_ctx, sig, sig_len,
  280. tst->msg, tst->msg_len), tst->pass));
  281. err:
  282. BN_free(rbn);
  283. BN_free(sbn);
  284. OPENSSL_free(sig);
  285. ECDSA_SIG_free(sign);
  286. EVP_PKEY_free(pkey);
  287. EVP_MD_CTX_free(md_ctx);
  288. return ret;
  289. }
  290. #endif /* OPENSSL_NO_EC */
  291. #ifndef OPENSSL_NO_DSA
  292. static int pkey_get_octet_bytes(EVP_PKEY *pkey, const char *name,
  293. unsigned char **out, size_t *out_len)
  294. {
  295. size_t len = 0;
  296. unsigned char *buf = NULL;
  297. if (!EVP_PKEY_get_octet_string_param(pkey, name, NULL, 0, &len))
  298. goto err;
  299. buf = OPENSSL_zalloc(len);
  300. if (buf == NULL)
  301. goto err;
  302. if (!EVP_PKEY_get_octet_string_param(pkey, name, buf, len, out_len))
  303. goto err;
  304. *out = buf;
  305. return 1;
  306. err:
  307. OPENSSL_free(buf);
  308. return 0;
  309. }
  310. static EVP_PKEY *dsa_paramgen(int L, int N)
  311. {
  312. EVP_PKEY_CTX *paramgen_ctx = NULL;
  313. EVP_PKEY *param_key = NULL;
  314. if (!TEST_ptr(paramgen_ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", NULL))
  315. || !TEST_true(EVP_PKEY_paramgen_init(paramgen_ctx))
  316. || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_bits(paramgen_ctx, L))
  317. || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_q_bits(paramgen_ctx, N))
  318. || !TEST_true(EVP_PKEY_paramgen(paramgen_ctx, &param_key)))
  319. return NULL;
  320. EVP_PKEY_CTX_free(paramgen_ctx);
  321. return param_key;
  322. }
  323. static EVP_PKEY *dsa_keygen(int L, int N)
  324. {
  325. EVP_PKEY *param_key = NULL, *key = NULL;
  326. EVP_PKEY_CTX *keygen_ctx = NULL;
  327. if (!TEST_ptr(param_key = dsa_paramgen(L, N))
  328. || !TEST_ptr(keygen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, param_key,
  329. NULL))
  330. || !TEST_int_gt(EVP_PKEY_keygen_init(keygen_ctx), 0)
  331. || !TEST_int_gt(EVP_PKEY_keygen(keygen_ctx, &key), 0))
  332. goto err;
  333. err:
  334. EVP_PKEY_free(param_key);
  335. EVP_PKEY_CTX_free(keygen_ctx);
  336. return key;
  337. }
  338. static int dsa_keygen_test(int id)
  339. {
  340. int ret = 0, i;
  341. EVP_PKEY_CTX *paramgen_ctx = NULL, *keygen_ctx = NULL;
  342. EVP_PKEY *param_key = NULL, *key = NULL;
  343. unsigned char *priv = NULL, *pub = NULL;
  344. size_t priv_len = 0, pub_len = 0;
  345. const struct dsa_paramgen_st *tst = &dsa_keygen_data[id];
  346. if (!TEST_ptr(param_key = dsa_paramgen(tst->L, tst->N))
  347. || !TEST_ptr(keygen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, param_key,
  348. NULL))
  349. || !TEST_int_gt(EVP_PKEY_keygen_init(keygen_ctx), 0))
  350. goto err;
  351. for (i = 0; i < 2; ++i) {
  352. if (!TEST_int_gt(EVP_PKEY_keygen(keygen_ctx, &key), 0)
  353. || !TEST_true(pkey_get_bn_bytes(key, OSSL_PKEY_PARAM_PRIV_KEY,
  354. &priv, &priv_len))
  355. || !TEST_true(pkey_get_bn_bytes(key, OSSL_PKEY_PARAM_PUB_KEY,
  356. &pub, &pub_len)))
  357. goto err;
  358. test_output_memory("y", pub, pub_len);
  359. test_output_memory("x", priv, priv_len);
  360. EVP_PKEY_free(key);
  361. OPENSSL_clear_free(priv, priv_len);
  362. OPENSSL_free(pub);
  363. key = NULL;
  364. pub = priv = NULL;
  365. }
  366. ret = 1;
  367. err:
  368. OPENSSL_clear_free(priv, priv_len);
  369. OPENSSL_free(pub);
  370. EVP_PKEY_free(param_key);
  371. EVP_PKEY_free(key);
  372. EVP_PKEY_CTX_free(keygen_ctx);
  373. EVP_PKEY_CTX_free(paramgen_ctx);
  374. return ret;
  375. }
  376. static int dsa_paramgen_test(int id)
  377. {
  378. int ret = 0, counter = 0;
  379. EVP_PKEY_CTX *paramgen_ctx = NULL;
  380. EVP_PKEY *param_key = NULL;
  381. unsigned char *p = NULL, *q = NULL, *seed = NULL;
  382. size_t plen = 0, qlen = 0, seedlen = 0;
  383. const struct dsa_paramgen_st *tst = &dsa_paramgen_data[id];
  384. if (!TEST_ptr(paramgen_ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", NULL))
  385. || !TEST_true(EVP_PKEY_paramgen_init(paramgen_ctx))
  386. || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_bits(paramgen_ctx, tst->L))
  387. || !TEST_true(EVP_PKEY_CTX_set_dsa_paramgen_q_bits(paramgen_ctx, tst->N))
  388. || !TEST_true(EVP_PKEY_paramgen(paramgen_ctx, &param_key))
  389. || !TEST_true(pkey_get_bn_bytes(param_key, OSSL_PKEY_PARAM_FFC_P,
  390. &p, &plen))
  391. || !TEST_true(pkey_get_bn_bytes(param_key, OSSL_PKEY_PARAM_FFC_Q,
  392. &q, &qlen))
  393. || !TEST_true(pkey_get_octet_bytes(param_key, OSSL_PKEY_PARAM_FFC_SEED,
  394. &seed, &seedlen))
  395. || !TEST_true(EVP_PKEY_get_int_param(param_key,
  396. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  397. &counter)))
  398. goto err;
  399. test_output_memory("p", p, plen);
  400. test_output_memory("q", q, qlen);
  401. test_output_memory("domainSeed", seed, seedlen);
  402. test_printf_stderr("%s: %d\n", "counter", counter);
  403. ret = 1;
  404. err:
  405. OPENSSL_free(p);
  406. OPENSSL_free(q);
  407. OPENSSL_free(seed);
  408. EVP_PKEY_free(param_key);
  409. EVP_PKEY_CTX_free(paramgen_ctx);
  410. return ret;
  411. }
  412. static int dsa_create_pkey(EVP_PKEY **pkey,
  413. const unsigned char *p, size_t p_len,
  414. const unsigned char *q, size_t q_len,
  415. const unsigned char *g, size_t g_len,
  416. const unsigned char *seed, size_t seed_len,
  417. int counter,
  418. const char *validate_type,
  419. const unsigned char *pub, size_t pub_len,
  420. BN_CTX *bn_ctx)
  421. {
  422. int ret = 0;
  423. EVP_PKEY_CTX *ctx = NULL;
  424. OSSL_PARAM_BLD *bld = NULL;
  425. OSSL_PARAM *params = NULL;
  426. BIGNUM *p_bn = NULL, *q_bn = NULL, *g_bn = NULL, *pub_bn = NULL;
  427. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  428. || !TEST_ptr(p_bn = BN_CTX_get(bn_ctx))
  429. || !TEST_ptr(BN_bin2bn(p, p_len, p_bn))
  430. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  431. OSSL_PKEY_PARAM_FFC_VALIDATE_TYPE,
  432. validate_type, 0))
  433. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p_bn))
  434. || !TEST_ptr(q_bn = BN_CTX_get(bn_ctx))
  435. || !TEST_ptr(BN_bin2bn(q, q_len, q_bn))
  436. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q_bn)))
  437. goto err;
  438. if (g != NULL) {
  439. if (!TEST_ptr(g_bn = BN_CTX_get(bn_ctx))
  440. || !TEST_ptr(BN_bin2bn(g, g_len, g_bn))
  441. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld,
  442. OSSL_PKEY_PARAM_FFC_G, g_bn)))
  443. goto err;
  444. }
  445. if (seed != NULL) {
  446. if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  447. OSSL_PKEY_PARAM_FFC_SEED, seed, seed_len)))
  448. goto err;
  449. }
  450. if (counter != -1) {
  451. if (!TEST_true(OSSL_PARAM_BLD_push_int(bld,
  452. OSSL_PKEY_PARAM_FFC_PCOUNTER,
  453. counter)))
  454. goto err;
  455. }
  456. if (pub != NULL) {
  457. if (!TEST_ptr(pub_bn = BN_CTX_get(bn_ctx))
  458. || !TEST_ptr(BN_bin2bn(pub, pub_len, pub_bn))
  459. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld,
  460. OSSL_PKEY_PARAM_PUB_KEY,
  461. pub_bn)))
  462. goto err;
  463. }
  464. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  465. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", NULL))
  466. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  467. || !TEST_true(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_PUBLIC_KEY, params)))
  468. goto err;
  469. ret = 1;
  470. err:
  471. OSSL_PARAM_free(params);
  472. OSSL_PARAM_BLD_free(bld);
  473. EVP_PKEY_CTX_free(ctx);
  474. return ret;
  475. }
  476. static int dsa_pqver_test(int id)
  477. {
  478. int ret = 0;
  479. BN_CTX *bn_ctx = NULL;
  480. EVP_PKEY_CTX *key_ctx = NULL;
  481. EVP_PKEY *param_key = NULL;
  482. const struct dsa_pqver_st *tst = &dsa_pqver_data[id];
  483. if (!TEST_ptr(bn_ctx = BN_CTX_new_ex(libctx))
  484. || !TEST_true(dsa_create_pkey(&param_key, tst->p, tst->p_len,
  485. tst->q, tst->q_len, NULL, 0,
  486. tst->seed, tst->seed_len, tst->counter,
  487. OSSL_FFC_PARAM_VALIDATE_PQ,
  488. NULL, 0,
  489. bn_ctx))
  490. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, param_key,
  491. NULL))
  492. || !TEST_int_eq(EVP_PKEY_param_check(key_ctx), tst->pass))
  493. goto err;
  494. ret = 1;
  495. err:
  496. BN_CTX_free(bn_ctx);
  497. EVP_PKEY_free(param_key);
  498. EVP_PKEY_CTX_free(key_ctx);
  499. return ret;
  500. }
  501. /* Extract r and s from a dsa signature */
  502. static int get_dsa_sig_rs_bytes(const unsigned char *sig, size_t sig_len,
  503. unsigned char **r, unsigned char **s,
  504. size_t *r_len, size_t *s_len)
  505. {
  506. int ret = 0;
  507. unsigned char *rbuf = NULL, *sbuf = NULL;
  508. size_t r1_len, s1_len;
  509. const BIGNUM *r1, *s1;
  510. DSA_SIG *sign = d2i_DSA_SIG(NULL, &sig, sig_len);
  511. if (sign == NULL)
  512. return 0;
  513. DSA_SIG_get0(sign, &r1, &s1);
  514. if (r1 == NULL || s1 == NULL)
  515. return 0;
  516. r1_len = BN_num_bytes(r1);
  517. s1_len = BN_num_bytes(s1);
  518. rbuf = OPENSSL_zalloc(r1_len);
  519. sbuf = OPENSSL_zalloc(s1_len);
  520. if (rbuf == NULL || sbuf == NULL)
  521. goto err;
  522. if (BN_bn2binpad(r1, rbuf, r1_len) <= 0)
  523. goto err;
  524. if (BN_bn2binpad(s1, sbuf, s1_len) <= 0)
  525. goto err;
  526. *r = rbuf;
  527. *s = sbuf;
  528. *r_len = r1_len;
  529. *s_len = s1_len;
  530. ret = 1;
  531. err:
  532. if (ret == 0) {
  533. OPENSSL_free(rbuf);
  534. OPENSSL_free(sbuf);
  535. }
  536. DSA_SIG_free(sign);
  537. return ret;
  538. }
  539. static int dsa_siggen_test(int id)
  540. {
  541. int ret = 0;
  542. EVP_PKEY *pkey = NULL;
  543. unsigned char *sig = NULL, *r = NULL, *s = NULL;
  544. size_t sig_len = 0, rlen = 0, slen = 0;
  545. const struct dsa_siggen_st *tst = &dsa_siggen_data[id];
  546. if (!TEST_ptr(pkey = dsa_keygen(tst->L, tst->N)))
  547. goto err;
  548. if (!TEST_true(sig_gen(pkey, NULL, tst->digest_alg, tst->msg, tst->msg_len,
  549. &sig, &sig_len))
  550. || !TEST_true(get_dsa_sig_rs_bytes(sig, sig_len, &r, &s, &rlen, &slen)))
  551. goto err;
  552. test_output_memory("r", r, rlen);
  553. test_output_memory("s", s, slen);
  554. ret = 1;
  555. err:
  556. OPENSSL_free(r);
  557. OPENSSL_free(s);
  558. OPENSSL_free(sig);
  559. EVP_PKEY_free(pkey);
  560. return ret;
  561. }
  562. static int dsa_sigver_test(int id)
  563. {
  564. int ret = 0;
  565. EVP_PKEY_CTX *ctx = NULL;
  566. EVP_PKEY *pkey = NULL;
  567. DSA_SIG *sign = NULL;
  568. size_t sig_len;
  569. unsigned char *sig = NULL;
  570. BIGNUM *rbn = NULL, *sbn = NULL;
  571. EVP_MD *md = NULL;
  572. unsigned char digest[EVP_MAX_MD_SIZE];
  573. unsigned int digest_len;
  574. BN_CTX *bn_ctx = NULL;
  575. const struct dsa_sigver_st *tst = &dsa_sigver_data[id];
  576. if (!TEST_ptr(bn_ctx = BN_CTX_new())
  577. || !TEST_true(dsa_create_pkey(&pkey, tst->p, tst->p_len,
  578. tst->q, tst->q_len, tst->g, tst->g_len,
  579. NULL, 0, 0, "", tst->pub, tst->pub_len,
  580. bn_ctx)))
  581. goto err;
  582. if (!TEST_ptr(sign = DSA_SIG_new())
  583. || !TEST_ptr(rbn = BN_bin2bn(tst->r, tst->r_len, NULL))
  584. || !TEST_ptr(sbn = BN_bin2bn(tst->s, tst->s_len, NULL))
  585. || !TEST_true(DSA_SIG_set0(sign, rbn, sbn)))
  586. goto err;
  587. rbn = sbn = NULL;
  588. if (!TEST_ptr(md = EVP_MD_fetch(libctx, tst->digest_alg, ""))
  589. || !TEST_true(EVP_Digest(tst->msg, tst->msg_len,
  590. digest, &digest_len, md, NULL)))
  591. goto err;
  592. if (!TEST_int_gt((sig_len = i2d_DSA_SIG(sign, &sig)), 0)
  593. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, ""))
  594. || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0)
  595. || !TEST_int_eq(EVP_PKEY_verify(ctx, sig, sig_len, digest, digest_len),
  596. tst->pass))
  597. goto err;
  598. ret = 1;
  599. err:
  600. EVP_PKEY_CTX_free(ctx);
  601. OPENSSL_free(sig);
  602. EVP_MD_free(md);
  603. DSA_SIG_free(sign);
  604. EVP_PKEY_free(pkey);
  605. BN_free(rbn);
  606. BN_free(sbn);
  607. BN_CTX_free(bn_ctx);
  608. return ret;
  609. }
  610. #endif /* OPENSSL_NO_DSA */
  611. /* cipher encrypt/decrypt */
  612. static int cipher_enc(const char *alg,
  613. const unsigned char *pt, size_t pt_len,
  614. const unsigned char *key, size_t key_len,
  615. const unsigned char *iv, size_t iv_len,
  616. const unsigned char *ct, size_t ct_len,
  617. int enc)
  618. {
  619. int ret = 0, out_len = 0, len = 0;
  620. EVP_CIPHER_CTX *ctx = NULL;
  621. EVP_CIPHER *cipher = NULL;
  622. unsigned char out[256] = { 0 };
  623. TEST_note("%s : %s", alg, enc ? "encrypt" : "decrypt");
  624. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  625. || !TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, alg, ""))
  626. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc))
  627. || !TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))
  628. || !TEST_true(EVP_CipherUpdate(ctx, out, &len, pt, pt_len))
  629. || !TEST_true(EVP_CipherFinal_ex(ctx, out + len, &out_len)))
  630. goto err;
  631. out_len += len;
  632. if (!TEST_mem_eq(out, out_len, ct, ct_len))
  633. goto err;
  634. ret = 1;
  635. err:
  636. EVP_CIPHER_free(cipher);
  637. EVP_CIPHER_CTX_free(ctx);
  638. return ret;
  639. }
  640. static int cipher_enc_dec_test(int id)
  641. {
  642. const struct cipher_st *tst = &cipher_enc_data[id];
  643. const int enc = 1;
  644. return TEST_true(cipher_enc(tst->alg, tst->pt, tst->pt_len,
  645. tst->key, tst->key_len,
  646. tst->iv, tst->iv_len,
  647. tst->ct, tst->ct_len, enc))
  648. && TEST_true(cipher_enc(tst->alg, tst->ct, tst->ct_len,
  649. tst->key, tst->key_len,
  650. tst->iv, tst->iv_len,
  651. tst->pt, tst->pt_len, !enc));
  652. }
  653. static int aes_ccm_enc_dec(const char *alg,
  654. const unsigned char *pt, size_t pt_len,
  655. const unsigned char *key, size_t key_len,
  656. const unsigned char *iv, size_t iv_len,
  657. const unsigned char *aad, size_t aad_len,
  658. const unsigned char *ct, size_t ct_len,
  659. const unsigned char *tag, size_t tag_len,
  660. int enc, int pass)
  661. {
  662. int ret = 0;
  663. EVP_CIPHER_CTX *ctx;
  664. EVP_CIPHER *cipher = NULL;
  665. int out_len, len;
  666. unsigned char out[1024];
  667. TEST_note("%s : %s : expected to %s", alg, enc ? "encrypt" : "decrypt",
  668. pass ? "pass" : "fail");
  669. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  670. || !TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, alg, ""))
  671. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc))
  672. || !TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len,
  673. NULL))
  674. || !TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len,
  675. enc ? NULL : (void *)tag))
  676. || !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
  677. || !TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))
  678. || !TEST_true(EVP_CipherUpdate(ctx, NULL, &len, NULL, pt_len))
  679. || !TEST_true(EVP_CipherUpdate(ctx, NULL, &len, aad, aad_len))
  680. || !TEST_int_eq(EVP_CipherUpdate(ctx, out, &len, pt, pt_len), pass))
  681. goto err;
  682. if (!pass) {
  683. ret = 1;
  684. goto err;
  685. }
  686. if (!TEST_true(EVP_CipherFinal_ex(ctx, out + len, &out_len)))
  687. goto err;
  688. if (enc) {
  689. out_len += len;
  690. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  691. tag_len, out + out_len))
  692. || !TEST_mem_eq(out, out_len, ct, ct_len)
  693. || !TEST_mem_eq(out + out_len, tag_len, tag, tag_len))
  694. goto err;
  695. } else {
  696. if (!TEST_mem_eq(out, out_len + len, ct, ct_len))
  697. goto err;
  698. }
  699. ret = 1;
  700. err:
  701. EVP_CIPHER_free(cipher);
  702. EVP_CIPHER_CTX_free(ctx);
  703. return ret;
  704. }
  705. static int aes_ccm_enc_dec_test(int id)
  706. {
  707. const struct cipher_ccm_st *tst = &aes_ccm_enc_data[id];
  708. /* The tag is on the end of the cipher text */
  709. const size_t tag_len = tst->ct_len - tst->pt_len;
  710. const size_t ct_len = tst->ct_len - tag_len;
  711. const unsigned char *tag = tst->ct + ct_len;
  712. const int enc = 1;
  713. const int pass = 1;
  714. if (ct_len < 1)
  715. return 0;
  716. return aes_ccm_enc_dec(tst->alg, tst->pt, tst->pt_len,
  717. tst->key, tst->key_len,
  718. tst->iv, tst->iv_len, tst->aad, tst->aad_len,
  719. tst->ct, ct_len, tag, tag_len, enc, pass)
  720. && aes_ccm_enc_dec(tst->alg, tst->ct, ct_len,
  721. tst->key, tst->key_len,
  722. tst->iv, tst->iv_len, tst->aad, tst->aad_len,
  723. tst->pt, tst->pt_len, tag, tag_len, !enc, pass)
  724. /* test that it fails if the tag is incorrect */
  725. && aes_ccm_enc_dec(tst->alg, tst->ct, ct_len,
  726. tst->key, tst->key_len,
  727. tst->iv, tst->iv_len, tst->aad, tst->aad_len,
  728. tst->pt, tst->pt_len,
  729. tag - 1, tag_len, !enc, !pass);
  730. }
  731. static int aes_gcm_enc_dec(const char *alg,
  732. const unsigned char *pt, size_t pt_len,
  733. const unsigned char *key, size_t key_len,
  734. const unsigned char *iv, size_t iv_len,
  735. const unsigned char *aad, size_t aad_len,
  736. const unsigned char *ct, size_t ct_len,
  737. const unsigned char *tag, size_t tag_len,
  738. int enc, int pass)
  739. {
  740. int ret = 0;
  741. EVP_CIPHER_CTX *ctx;
  742. EVP_CIPHER *cipher = NULL;
  743. int out_len, len;
  744. unsigned char out[1024];
  745. TEST_note("%s : %s : expected to %s", alg, enc ? "encrypt" : "decrypt",
  746. pass ? "pass" : "fail");
  747. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  748. || !TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, alg, ""))
  749. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc))
  750. || !TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len,
  751. NULL)))
  752. goto err;
  753. if (!enc) {
  754. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len,
  755. (void *)tag)))
  756. goto err;
  757. }
  758. /*
  759. * For testing purposes the IV it being set here. In a compliant application
  760. * the IV would be generated internally. A fake entropy source could also
  761. * be used to feed in the random IV bytes (see fake_random.c)
  762. */
  763. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
  764. || !TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))
  765. || !TEST_true(EVP_CipherUpdate(ctx, NULL, &len, aad, aad_len))
  766. || !TEST_true(EVP_CipherUpdate(ctx, out, &len, pt, pt_len)))
  767. goto err;
  768. if (!TEST_int_eq(EVP_CipherFinal_ex(ctx, out + len, &out_len), pass))
  769. goto err;
  770. if (!pass) {
  771. ret = 1;
  772. goto err;
  773. }
  774. out_len += len;
  775. if (enc) {
  776. if (!TEST_mem_eq(out, out_len, ct, ct_len)
  777. || !TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  778. tag_len, out + out_len))
  779. || !TEST_mem_eq(out + out_len, tag_len, tag, tag_len))
  780. goto err;
  781. } else {
  782. if (!TEST_mem_eq(out, out_len, ct, ct_len))
  783. goto err;
  784. }
  785. ret = 1;
  786. err:
  787. EVP_CIPHER_free(cipher);
  788. EVP_CIPHER_CTX_free(ctx);
  789. return ret;
  790. }
  791. static int aes_gcm_enc_dec_test(int id)
  792. {
  793. const struct cipher_gcm_st *tst = &aes_gcm_enc_data[id];
  794. int enc = 1;
  795. int pass = 1;
  796. return aes_gcm_enc_dec(tst->alg, tst->pt, tst->pt_len,
  797. tst->key, tst->key_len,
  798. tst->iv, tst->iv_len, tst->aad, tst->aad_len,
  799. tst->ct, tst->ct_len, tst->tag, tst->tag_len,
  800. enc, pass)
  801. && aes_gcm_enc_dec(tst->alg, tst->ct, tst->ct_len,
  802. tst->key, tst->key_len,
  803. tst->iv, tst->iv_len, tst->aad, tst->aad_len,
  804. tst->pt, tst->pt_len, tst->tag, tst->tag_len,
  805. !enc, pass)
  806. /* Fail if incorrect tag passed to decrypt */
  807. && aes_gcm_enc_dec(tst->alg, tst->ct, tst->ct_len,
  808. tst->key, tst->key_len,
  809. tst->iv, tst->iv_len, tst->aad, tst->aad_len,
  810. tst->pt, tst->pt_len, tst->aad, tst->tag_len,
  811. !enc, !pass);
  812. }
  813. #ifndef OPENSSL_NO_DH
  814. static int dh_create_pkey(EVP_PKEY **pkey, const char *group_name,
  815. const unsigned char *pub, size_t pub_len,
  816. const unsigned char *priv, size_t priv_len,
  817. BN_CTX *bn_ctx, int pass)
  818. {
  819. int ret = 0;
  820. EVP_PKEY_CTX *ctx = NULL;
  821. OSSL_PARAM_BLD *bld = NULL;
  822. OSSL_PARAM *params = NULL;
  823. BIGNUM *pub_bn = NULL, *priv_bn = NULL;
  824. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  825. || (group_name != NULL
  826. && !TEST_int_gt(OSSL_PARAM_BLD_push_utf8_string(
  827. bld, OSSL_PKEY_PARAM_GROUP_NAME,
  828. group_name, 0), 0)))
  829. goto err;
  830. if (pub != NULL) {
  831. if (!TEST_ptr(pub_bn = BN_CTX_get(bn_ctx))
  832. || !TEST_ptr(BN_bin2bn(pub, pub_len, pub_bn))
  833. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  834. pub_bn)))
  835. goto err;
  836. }
  837. if (priv != NULL) {
  838. if (!TEST_ptr(priv_bn = BN_CTX_get(bn_ctx))
  839. || !TEST_ptr(BN_bin2bn(priv, priv_len, priv_bn))
  840. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  841. priv_bn)))
  842. goto err;
  843. }
  844. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  845. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL))
  846. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  847. || !TEST_int_eq(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_KEYPAIR, params),
  848. pass))
  849. goto err;
  850. ret = 1;
  851. err:
  852. OSSL_PARAM_free(params);
  853. OSSL_PARAM_BLD_free(bld);
  854. EVP_PKEY_CTX_free(ctx);
  855. return ret;
  856. }
  857. static int dh_safe_prime_keygen_test(int id)
  858. {
  859. int ret = 0;
  860. EVP_PKEY_CTX *ctx = NULL;
  861. EVP_PKEY *pkey = NULL;
  862. unsigned char *priv = NULL;
  863. unsigned char *pub = NULL;
  864. size_t priv_len = 0, pub_len = 0;
  865. OSSL_PARAM params[2];
  866. const struct dh_safe_prime_keygen_st *tst = &dh_safe_prime_keygen_data[id];
  867. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  868. (char *)tst->group_name, 0);
  869. params[1] = OSSL_PARAM_construct_end();
  870. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL))
  871. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  872. || !TEST_true(EVP_PKEY_CTX_set_params(ctx, params))
  873. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0)
  874. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
  875. &priv, &priv_len))
  876. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_PUB_KEY,
  877. &pub, &pub_len)))
  878. goto err;
  879. test_output_memory("x", priv, priv_len);
  880. test_output_memory("y", pub, pub_len);
  881. ret = 1;
  882. err:
  883. OPENSSL_clear_free(priv, priv_len);
  884. OPENSSL_free(pub);
  885. EVP_PKEY_free(pkey);
  886. EVP_PKEY_CTX_free(ctx);
  887. return ret;
  888. }
  889. static int dh_safe_prime_keyver_test(int id)
  890. {
  891. int ret = 0;
  892. BN_CTX *bn_ctx = NULL;
  893. EVP_PKEY_CTX *key_ctx = NULL;
  894. EVP_PKEY *pkey = NULL;
  895. const struct dh_safe_prime_keyver_st *tst = &dh_safe_prime_keyver_data[id];
  896. if (!TEST_ptr(bn_ctx = BN_CTX_new_ex(libctx))
  897. || !TEST_true(dh_create_pkey(&pkey, tst->group_name,
  898. tst->pub, tst->pub_len,
  899. tst->priv, tst->priv_len, bn_ctx, 1))
  900. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, ""))
  901. || !TEST_int_eq(EVP_PKEY_check(key_ctx), tst->pass))
  902. goto err;
  903. ret = 1;
  904. err:
  905. EVP_PKEY_free(pkey);
  906. EVP_PKEY_CTX_free(key_ctx);
  907. BN_CTX_free(bn_ctx);
  908. return ret;
  909. }
  910. #endif /* OPENSSL_NO_DH */
  911. static EVP_PKEY *rsa_keygen(int bits)
  912. {
  913. EVP_PKEY *key = NULL;
  914. EVP_PKEY_CTX *keygen_ctx = NULL;
  915. if (!TEST_ptr(keygen_ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL))
  916. || !TEST_int_gt(EVP_PKEY_keygen_init(keygen_ctx), 0)
  917. || !TEST_true(EVP_PKEY_CTX_set_rsa_keygen_bits(keygen_ctx, bits))
  918. || !TEST_int_gt(EVP_PKEY_keygen(keygen_ctx, &key), 0))
  919. goto err;
  920. err:
  921. EVP_PKEY_CTX_free(keygen_ctx);
  922. return key;
  923. }
  924. static int rsa_create_pkey(EVP_PKEY **pkey,
  925. const unsigned char *n, size_t n_len,
  926. const unsigned char *e, size_t e_len,
  927. const unsigned char *d, size_t d_len,
  928. BN_CTX *bn_ctx)
  929. {
  930. int ret = 0;
  931. EVP_PKEY_CTX *ctx = NULL;
  932. OSSL_PARAM_BLD *bld = NULL;
  933. OSSL_PARAM *params = NULL;
  934. BIGNUM *e_bn = NULL, *d_bn = NULL, *n_bn = NULL;
  935. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  936. || !TEST_ptr(n_bn = BN_CTX_get(bn_ctx))
  937. || !TEST_ptr(BN_bin2bn(n, n_len, n_bn))
  938. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n_bn)))
  939. goto err;
  940. if (e != NULL) {
  941. if (!TEST_ptr(e_bn = BN_CTX_get(bn_ctx))
  942. || !TEST_ptr(BN_bin2bn(e, e_len, e_bn))
  943. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E,
  944. e_bn)))
  945. goto err;
  946. }
  947. if (d != NULL) {
  948. if (!TEST_ptr(d_bn = BN_CTX_get(bn_ctx))
  949. || !TEST_ptr(BN_bin2bn(d, d_len, d_bn))
  950. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D,
  951. d_bn)))
  952. goto err;
  953. }
  954. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  955. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL))
  956. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  957. || !TEST_true(EVP_PKEY_fromdata(ctx, pkey, EVP_PKEY_KEYPAIR, params)))
  958. goto err;
  959. ret = 1;
  960. err:
  961. OSSL_PARAM_free(params);
  962. OSSL_PARAM_BLD_free(bld);
  963. EVP_PKEY_CTX_free(ctx);
  964. return ret;
  965. }
  966. static int rsa_keygen_test(int id)
  967. {
  968. int ret = 0;
  969. EVP_PKEY_CTX *ctx = NULL;
  970. EVP_PKEY *pkey = NULL;
  971. BIGNUM *e_bn = NULL;
  972. BIGNUM *xp1_bn = NULL, *xp2_bn = NULL, *xp_bn = NULL;
  973. BIGNUM *xq1_bn = NULL, *xq2_bn = NULL, *xq_bn = NULL;
  974. unsigned char *n = NULL, *d = NULL;
  975. unsigned char *p = NULL, *p1 = NULL, *p2 = NULL;
  976. unsigned char *q = NULL, *q1 = NULL, *q2 = NULL;
  977. size_t n_len = 0, d_len = 0;
  978. size_t p_len = 0, p1_len = 0, p2_len = 0;
  979. size_t q_len = 0, q1_len = 0, q2_len = 0;
  980. OSSL_PARAM_BLD *bld = NULL;
  981. OSSL_PARAM *params = NULL;
  982. const struct rsa_keygen_st *tst = &rsa_keygen_data[id];
  983. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  984. || !TEST_ptr(xp1_bn = BN_bin2bn(tst->xp1, tst->xp1_len, NULL))
  985. || !TEST_ptr(xp2_bn = BN_bin2bn(tst->xp2, tst->xp2_len, NULL))
  986. || !TEST_ptr(xp_bn = BN_bin2bn(tst->xp, tst->xp_len, NULL))
  987. || !TEST_ptr(xq1_bn = BN_bin2bn(tst->xq1, tst->xq1_len, NULL))
  988. || !TEST_ptr(xq2_bn = BN_bin2bn(tst->xq2, tst->xq2_len, NULL))
  989. || !TEST_ptr(xq_bn = BN_bin2bn(tst->xq, tst->xq_len, NULL))
  990. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XP1,
  991. xp1_bn))
  992. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XP2,
  993. xp2_bn))
  994. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XP,
  995. xp_bn))
  996. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XQ1,
  997. xq1_bn))
  998. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XQ2,
  999. xq2_bn))
  1000. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_TEST_XQ,
  1001. xq_bn))
  1002. || !TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  1003. goto err;
  1004. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL))
  1005. || !TEST_ptr(e_bn = BN_bin2bn(tst->e, tst->e_len, NULL))
  1006. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  1007. || !TEST_true(EVP_PKEY_CTX_set_params(ctx, params))
  1008. || !TEST_true(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, tst->mod))
  1009. || !TEST_true(EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, e_bn))
  1010. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0)
  1011. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_P1,
  1012. &p1, &p1_len))
  1013. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_P2,
  1014. &p2, &p2_len))
  1015. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_Q1,
  1016. &q1, &q1_len))
  1017. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_TEST_Q2,
  1018. &q2, &q2_len))
  1019. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_FACTOR1,
  1020. &p, &p_len))
  1021. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_FACTOR2,
  1022. &q, &q_len))
  1023. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_N,
  1024. &n, &n_len))
  1025. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_D,
  1026. &d, &d_len)))
  1027. goto err;
  1028. if (!TEST_mem_eq(tst->p1, tst->p1_len, p1, p1_len)
  1029. || !TEST_mem_eq(tst->p2, tst->p2_len, p2, p2_len)
  1030. || !TEST_mem_eq(tst->p, tst->p_len, p, p_len)
  1031. || !TEST_mem_eq(tst->q1, tst->q1_len, q1, q1_len)
  1032. || !TEST_mem_eq(tst->q2, tst->q2_len, q2, q2_len)
  1033. || !TEST_mem_eq(tst->q, tst->q_len, q, q_len)
  1034. || !TEST_mem_eq(tst->n, tst->n_len, n, n_len)
  1035. || !TEST_mem_eq(tst->d, tst->d_len, d, d_len))
  1036. goto err;
  1037. test_output_memory("p1", p1, p1_len);
  1038. test_output_memory("p2", p2, p2_len);
  1039. test_output_memory("p", p, p_len);
  1040. test_output_memory("q1", q1, q1_len);
  1041. test_output_memory("q2", q2, q2_len);
  1042. test_output_memory("q", q, q_len);
  1043. test_output_memory("n", n, n_len);
  1044. test_output_memory("d", d, d_len);
  1045. ret = 1;
  1046. err:
  1047. BN_free(xp1_bn);
  1048. BN_free(xp2_bn);
  1049. BN_free(xp_bn);
  1050. BN_free(xq1_bn);
  1051. BN_free(xq2_bn);
  1052. BN_free(xq_bn);
  1053. BN_free(e_bn);
  1054. OPENSSL_free(p1);
  1055. OPENSSL_free(p2);
  1056. OPENSSL_free(q1);
  1057. OPENSSL_free(q2);
  1058. OPENSSL_free(p);
  1059. OPENSSL_free(q);
  1060. OPENSSL_free(n);
  1061. OPENSSL_free(d);
  1062. EVP_PKEY_free(pkey);
  1063. EVP_PKEY_CTX_free(ctx);
  1064. OSSL_PARAM_free(params);
  1065. OSSL_PARAM_BLD_free(bld);
  1066. return ret;
  1067. }
  1068. static int rsa_siggen_test(int id)
  1069. {
  1070. int ret = 0;
  1071. EVP_PKEY *pkey = NULL;
  1072. unsigned char *sig = NULL, *n = NULL, *e = NULL;
  1073. size_t sig_len = 0, n_len = 0, e_len = 0;
  1074. OSSL_PARAM params[4], *p;
  1075. const struct rsa_siggen_st *tst = &rsa_siggen_data[id];
  1076. TEST_note("RSA %s signature generation", tst->sig_pad_mode);
  1077. p = params;
  1078. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,
  1079. (char *)tst->sig_pad_mode, 0);
  1080. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1081. (char *)tst->digest_alg, 0);
  1082. if (tst->pss_salt_len >= 0) {
  1083. int salt_len = tst->pss_salt_len;
  1084. *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
  1085. &salt_len);
  1086. }
  1087. *p++ = OSSL_PARAM_construct_end();
  1088. if (!TEST_ptr(pkey = rsa_keygen(tst->mod))
  1089. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_N, &n, &n_len))
  1090. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_E, &e, &e_len))
  1091. || !TEST_true(sig_gen(pkey, params, tst->digest_alg,
  1092. tst->msg, tst->msg_len,
  1093. &sig, &sig_len)))
  1094. goto err;
  1095. test_output_memory("n", n, n_len);
  1096. test_output_memory("e", e, e_len);
  1097. test_output_memory("sig", sig, sig_len);
  1098. ret = 1;
  1099. err:
  1100. OPENSSL_free(n);
  1101. OPENSSL_free(e);
  1102. OPENSSL_free(sig);
  1103. EVP_PKEY_free(pkey);
  1104. return ret;
  1105. }
  1106. static int rsa_sigver_test(int id)
  1107. {
  1108. int ret = 0;
  1109. EVP_PKEY_CTX *pkey_ctx = NULL;
  1110. EVP_PKEY *pkey = NULL;
  1111. EVP_MD_CTX *md_ctx = NULL;
  1112. BN_CTX *bn_ctx = NULL;
  1113. OSSL_PARAM params[4], *p;
  1114. const struct rsa_sigver_st *tst = &rsa_sigver_data[id];
  1115. TEST_note("RSA %s Signature Verify : expected to %s ", tst->sig_pad_mode,
  1116. tst->pass == PASS ? "pass" : "fail");
  1117. p = params;
  1118. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE,
  1119. (char *)tst->sig_pad_mode, 0);
  1120. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1121. (char *)tst->digest_alg, 0);
  1122. if (tst->pss_salt_len >= 0) {
  1123. int salt_len = tst->pss_salt_len;
  1124. *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
  1125. &salt_len);
  1126. }
  1127. *p++ = OSSL_PARAM_construct_end();
  1128. if (!TEST_ptr(bn_ctx = BN_CTX_new())
  1129. || !TEST_true(rsa_create_pkey(&pkey, tst->n, tst->n_len,
  1130. tst->e, tst->e_len, NULL, 0, bn_ctx))
  1131. || !TEST_ptr(md_ctx = EVP_MD_CTX_new())
  1132. || !TEST_true(EVP_DigestVerifyInit_ex(md_ctx, &pkey_ctx,
  1133. tst->digest_alg, libctx, NULL,
  1134. pkey, NULL)
  1135. || !TEST_true(EVP_PKEY_CTX_set_params(pkey_ctx, params))
  1136. || !TEST_int_eq(EVP_DigestVerify(md_ctx, tst->sig, tst->sig_len,
  1137. tst->msg, tst->msg_len), tst->pass)))
  1138. goto err;
  1139. ret = 1;
  1140. err:
  1141. EVP_PKEY_free(pkey);
  1142. BN_CTX_free(bn_ctx);
  1143. EVP_MD_CTX_free(md_ctx);
  1144. return ret;
  1145. }
  1146. static int rsa_decryption_primitive_test(int id)
  1147. {
  1148. int ret = 0;
  1149. EVP_PKEY_CTX *ctx = NULL;
  1150. EVP_PKEY *pkey = NULL;
  1151. unsigned char pt[2048];
  1152. size_t pt_len = sizeof(pt);
  1153. unsigned char *n = NULL, *e = NULL;
  1154. size_t n_len = 0, e_len = 0;
  1155. BN_CTX *bn_ctx = NULL;
  1156. const struct rsa_decrypt_prim_st *tst = &rsa_decrypt_prim_data[id];
  1157. if (!TEST_ptr(pkey = rsa_keygen(2048))
  1158. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_N, &n, &n_len))
  1159. || !TEST_true(pkey_get_bn_bytes(pkey, OSSL_PKEY_PARAM_RSA_E, &e, &e_len))
  1160. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, ""))
  1161. || !TEST_int_gt(EVP_PKEY_decrypt_init(ctx), 0)
  1162. || !TEST_int_gt(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING), 0))
  1163. goto err;
  1164. test_output_memory("n", n, n_len);
  1165. test_output_memory("e", e, e_len);
  1166. if (!EVP_PKEY_decrypt(ctx, pt, &pt_len, tst->ct, tst->ct_len))
  1167. TEST_note("Decryption Failed");
  1168. else
  1169. test_output_memory("pt", pt, pt_len);
  1170. ret = 1;
  1171. err:
  1172. OPENSSL_free(n);
  1173. OPENSSL_free(e);
  1174. EVP_PKEY_CTX_free(ctx);
  1175. EVP_PKEY_free(pkey);
  1176. BN_CTX_free(bn_ctx);
  1177. return ret;
  1178. }
  1179. static int self_test_events(const OSSL_PARAM params[], void *varg)
  1180. {
  1181. SELF_TEST_ARGS *args = varg;
  1182. const OSSL_PARAM *p = NULL;
  1183. const char *phase = NULL, *type = NULL, *desc = NULL;
  1184. int ret = 0;
  1185. if (!args->enable)
  1186. return 1;
  1187. args->called++;
  1188. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_PHASE);
  1189. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  1190. goto err;
  1191. phase = (const char *)p->data;
  1192. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_DESC);
  1193. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  1194. goto err;
  1195. desc = (const char *)p->data;
  1196. p = OSSL_PARAM_locate_const(params, OSSL_PROV_PARAM_SELF_TEST_TYPE);
  1197. if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING)
  1198. goto err;
  1199. type = (const char *)p->data;
  1200. BIO_printf(bio_out, "%s %s %s\n", phase, desc, type);
  1201. ret = 1;
  1202. err:
  1203. return ret;
  1204. }
  1205. static int drbg_test(int id)
  1206. {
  1207. OSSL_PARAM params[3];
  1208. EVP_RAND *rand = NULL;
  1209. EVP_RAND_CTX *ctx = NULL, *parent = NULL;
  1210. unsigned char returned_bits[64];
  1211. const size_t returned_bits_len = sizeof(returned_bits);
  1212. unsigned int strength = 256;
  1213. const struct drbg_st *tst = &drbg_data[id];
  1214. int res = 0;
  1215. /* Create the seed source */
  1216. if (!TEST_ptr(rand = EVP_RAND_fetch(libctx, "TEST-RAND", "-fips"))
  1217. || !TEST_ptr(parent = EVP_RAND_CTX_new(rand, NULL)))
  1218. goto err;
  1219. EVP_RAND_free(rand);
  1220. rand = NULL;
  1221. params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
  1222. params[1] = OSSL_PARAM_construct_end();
  1223. if (!TEST_true(EVP_RAND_set_ctx_params(parent, params)))
  1224. goto err;
  1225. /* Get the DRBG */
  1226. if (!TEST_ptr(rand = EVP_RAND_fetch(libctx, tst->drbg_name, ""))
  1227. || !TEST_ptr(ctx = EVP_RAND_CTX_new(rand, parent)))
  1228. goto err;
  1229. /* Set the DRBG up */
  1230. params[0] = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF,
  1231. (int *)&tst->use_df);
  1232. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
  1233. (char *)tst->cipher, 0);
  1234. params[2] = OSSL_PARAM_construct_end();
  1235. if (!TEST_true(EVP_RAND_set_ctx_params(ctx, params)))
  1236. goto err;
  1237. /* Feed in the entropy and nonce */
  1238. params[0] = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  1239. (void *)tst->entropy_input,
  1240. tst->entropy_input_len);
  1241. params[1] = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
  1242. (void *)tst->nonce,
  1243. tst->nonce_len);
  1244. params[2] = OSSL_PARAM_construct_end();
  1245. if (!TEST_true(EVP_RAND_set_ctx_params(parent, params)))
  1246. goto err;
  1247. /*
  1248. * Run the test
  1249. * A NULL personalisation string defaults to the built in so something
  1250. * non-NULL is needed if there is no personalisation string
  1251. */
  1252. if (!TEST_true(EVP_RAND_instantiate(ctx, 0, 0, (void *)"", 0, NULL))
  1253. || !TEST_true(EVP_RAND_generate(ctx, returned_bits, returned_bits_len,
  1254. 0, 0, NULL, 0))
  1255. || !TEST_true(EVP_RAND_generate(ctx, returned_bits, returned_bits_len,
  1256. 0, 0, NULL, 0)))
  1257. goto err;
  1258. test_output_memory("returned bits", returned_bits, returned_bits_len);
  1259. /* Clean up */
  1260. if (!TEST_true(EVP_RAND_uninstantiate(ctx))
  1261. || !TEST_true(EVP_RAND_uninstantiate(parent)))
  1262. goto err;
  1263. /* Verify the output */
  1264. if (!TEST_mem_eq(returned_bits, returned_bits_len,
  1265. tst->returned_bits, tst->returned_bits_len))
  1266. goto err;
  1267. res = 1;
  1268. err:
  1269. EVP_RAND_CTX_free(ctx);
  1270. EVP_RAND_CTX_free(parent);
  1271. EVP_RAND_free(rand);
  1272. return res;
  1273. }
  1274. int setup_tests(void)
  1275. {
  1276. char *config_file = NULL;
  1277. OPTION_CHOICE o;
  1278. while ((o = opt_next()) != OPT_EOF) {
  1279. switch (o) {
  1280. case OPT_CONFIG_FILE:
  1281. config_file = opt_arg();
  1282. break;
  1283. case OPT_TEST_CASES:
  1284. break;
  1285. default:
  1286. case OPT_ERR:
  1287. return 0;
  1288. }
  1289. }
  1290. if (!test_get_libctx(&libctx, &prov_null, config_file, NULL, NULL))
  1291. return 0;
  1292. OSSL_SELF_TEST_set_callback(libctx, self_test_events, &self_test_args);
  1293. ADD_ALL_TESTS(cipher_enc_dec_test, OSSL_NELEM(cipher_enc_data));
  1294. ADD_ALL_TESTS(aes_ccm_enc_dec_test, OSSL_NELEM(aes_ccm_enc_data));
  1295. ADD_ALL_TESTS(aes_gcm_enc_dec_test, OSSL_NELEM(aes_gcm_enc_data));
  1296. ADD_ALL_TESTS(rsa_keygen_test, OSSL_NELEM(rsa_keygen_data));
  1297. ADD_ALL_TESTS(rsa_siggen_test, OSSL_NELEM(rsa_siggen_data));
  1298. ADD_ALL_TESTS(rsa_sigver_test, OSSL_NELEM(rsa_sigver_data));
  1299. ADD_ALL_TESTS(rsa_decryption_primitive_test,
  1300. OSSL_NELEM(rsa_decrypt_prim_data));
  1301. #ifndef OPENSSL_NO_DH
  1302. ADD_ALL_TESTS(dh_safe_prime_keygen_test,
  1303. OSSL_NELEM(dh_safe_prime_keygen_data));
  1304. ADD_ALL_TESTS(dh_safe_prime_keyver_test,
  1305. OSSL_NELEM(dh_safe_prime_keyver_data));
  1306. #endif /* OPENSSL_NO_DH */
  1307. #ifndef OPENSSL_NO_DSA
  1308. ADD_ALL_TESTS(dsa_keygen_test, OSSL_NELEM(dsa_keygen_data));
  1309. ADD_ALL_TESTS(dsa_paramgen_test, OSSL_NELEM(dsa_paramgen_data));
  1310. ADD_ALL_TESTS(dsa_pqver_test, OSSL_NELEM(dsa_pqver_data));
  1311. ADD_ALL_TESTS(dsa_siggen_test, OSSL_NELEM(dsa_siggen_data));
  1312. ADD_ALL_TESTS(dsa_sigver_test, OSSL_NELEM(dsa_sigver_data));
  1313. #endif /* OPENSSL_NO_DSA */
  1314. #ifndef OPENSSL_NO_EC
  1315. ADD_ALL_TESTS(ecdsa_keygen_test, OSSL_NELEM(ecdsa_keygen_data));
  1316. ADD_ALL_TESTS(ecdsa_pub_verify_test, OSSL_NELEM(ecdsa_pv_data));
  1317. ADD_ALL_TESTS(ecdsa_siggen_test, OSSL_NELEM(ecdsa_siggen_data));
  1318. ADD_ALL_TESTS(ecdsa_sigver_test, OSSL_NELEM(ecdsa_sigver_data));
  1319. #endif /* OPENSSL_NO_EC */
  1320. ADD_ALL_TESTS(drbg_test, OSSL_NELEM(drbg_data));
  1321. return 1;
  1322. }
  1323. void cleanup_tests(void)
  1324. {
  1325. OSSL_PROVIDER_unload(prov_null);
  1326. OSSL_LIB_CTX_free(libctx);
  1327. }