acvp_test.c 51 KB

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