acvp_test.c 52 KB

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