self_test_kats.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * Copyright 2019-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. #include <string.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/kdf.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/param_build.h>
  14. #include "internal/cryptlib.h"
  15. #include "internal/nelem.h"
  16. #include "self_test.h"
  17. #include "self_test_data.inc"
  18. static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_SELF_TEST *st,
  19. OSSL_LIB_CTX *libctx)
  20. {
  21. int ok = 0;
  22. unsigned char out[EVP_MAX_MD_SIZE];
  23. unsigned int out_len = 0;
  24. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  25. EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
  26. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
  27. if (ctx == NULL
  28. || md == NULL
  29. || !EVP_DigestInit_ex(ctx, md, NULL)
  30. || !EVP_DigestUpdate(ctx, t->pt, t->pt_len)
  31. || !EVP_DigestFinal(ctx, out, &out_len))
  32. goto err;
  33. /* Optional corruption */
  34. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  35. if (out_len != t->expected_len
  36. || memcmp(out, t->expected, out_len) != 0)
  37. goto err;
  38. ok = 1;
  39. err:
  40. EVP_MD_free(md);
  41. EVP_MD_CTX_free(ctx);
  42. OSSL_SELF_TEST_onend(st, ok);
  43. return ok;
  44. }
  45. /*
  46. * Helper function to setup a EVP_CipherInit
  47. * Used to hide the complexity of Authenticated ciphers.
  48. */
  49. static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  50. const ST_KAT_CIPHER *t, int enc)
  51. {
  52. unsigned char *in_tag = NULL;
  53. int pad = 0, tmp;
  54. /* Flag required for Key wrapping */
  55. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  56. if (t->tag == NULL) {
  57. /* Use a normal cipher init */
  58. return EVP_CipherInit_ex(ctx, cipher, NULL, t->key, t->iv, enc)
  59. && EVP_CIPHER_CTX_set_padding(ctx, pad);
  60. }
  61. /* The authenticated cipher init */
  62. if (!enc)
  63. in_tag = (unsigned char *)t->tag;
  64. return EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)
  65. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, t->iv_len, NULL)
  66. && (in_tag == NULL
  67. || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->tag_len,
  68. in_tag))
  69. && EVP_CipherInit_ex(ctx, NULL, NULL, t->key, t->iv, enc)
  70. && EVP_CIPHER_CTX_set_padding(ctx, pad)
  71. && EVP_CipherUpdate(ctx, NULL, &tmp, t->aad, t->aad_len);
  72. }
  73. /* Test a single KAT for encrypt/decrypt */
  74. static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_SELF_TEST *st,
  75. OSSL_LIB_CTX *libctx)
  76. {
  77. int ret = 0, encrypt = 1, len, ct_len = 0, pt_len = 0;
  78. EVP_CIPHER_CTX *ctx = NULL;
  79. EVP_CIPHER *cipher = NULL;
  80. unsigned char ct_buf[256] = { 0 };
  81. unsigned char pt_buf[256] = { 0 };
  82. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
  83. ctx = EVP_CIPHER_CTX_new();
  84. if (ctx == NULL)
  85. goto err;
  86. cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, "");
  87. if (cipher == NULL)
  88. goto err;
  89. /* Encrypt plain text message */
  90. if (!cipher_init(ctx, cipher, t, encrypt)
  91. || !EVP_CipherUpdate(ctx, ct_buf, &len, t->base.pt, t->base.pt_len)
  92. || !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
  93. goto err;
  94. OSSL_SELF_TEST_oncorrupt_byte(st, ct_buf);
  95. ct_len += len;
  96. if (ct_len != (int)t->base.expected_len
  97. || memcmp(t->base.expected, ct_buf, ct_len) != 0)
  98. goto err;
  99. if (t->tag != NULL) {
  100. unsigned char tag[16] = { 0 };
  101. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, t->tag_len, tag)
  102. || memcmp(tag, t->tag, t->tag_len) != 0)
  103. goto err;
  104. }
  105. if (!(cipher_init(ctx, cipher, t, !encrypt)
  106. && EVP_CipherUpdate(ctx, pt_buf, &len, ct_buf, ct_len)
  107. && EVP_CipherFinal_ex(ctx, pt_buf + len, &pt_len)))
  108. goto err;
  109. pt_len += len;
  110. if (pt_len != (int)t->base.pt_len
  111. || memcmp(pt_buf, t->base.pt, pt_len) != 0)
  112. goto err;
  113. ret = 1;
  114. err:
  115. EVP_CIPHER_free(cipher);
  116. EVP_CIPHER_CTX_free(ctx);
  117. OSSL_SELF_TEST_onend(st, ret);
  118. return ret;
  119. }
  120. static int add_params(OSSL_PARAM_BLD *bld, const ST_KAT_PARAM *params,
  121. BN_CTX *ctx)
  122. {
  123. int ret = 0;
  124. const ST_KAT_PARAM *p;
  125. if (params == NULL)
  126. return 1;
  127. for (p = params; p->data != NULL; ++p)
  128. {
  129. switch (p->type) {
  130. case OSSL_PARAM_UNSIGNED_INTEGER: {
  131. BIGNUM *bn = BN_CTX_get(ctx);
  132. if (bn == NULL
  133. || (BN_bin2bn(p->data, p->data_len, bn) == NULL)
  134. || !OSSL_PARAM_BLD_push_BN(bld, p->name, bn))
  135. goto err;
  136. break;
  137. }
  138. case OSSL_PARAM_UTF8_STRING: {
  139. if (!OSSL_PARAM_BLD_push_utf8_string(bld, p->name, p->data, 0))
  140. goto err;
  141. break;
  142. }
  143. case OSSL_PARAM_OCTET_STRING: {
  144. if (!OSSL_PARAM_BLD_push_octet_string(bld, p->name, p->data,
  145. p->data_len))
  146. goto err;
  147. break;
  148. }
  149. case OSSL_PARAM_INTEGER: {
  150. if (!OSSL_PARAM_BLD_push_int(bld, p->name, *(int *)p->data))
  151. goto err;
  152. break;
  153. }
  154. default:
  155. break;
  156. }
  157. }
  158. ret = 1;
  159. err:
  160. return ret;
  161. }
  162. static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
  163. OSSL_LIB_CTX *libctx)
  164. {
  165. int ret = 0;
  166. unsigned char out[128];
  167. EVP_KDF *kdf = NULL;
  168. EVP_KDF_CTX *ctx = NULL;
  169. BN_CTX *bnctx = NULL;
  170. OSSL_PARAM *params = NULL;
  171. OSSL_PARAM_BLD *bld = NULL;
  172. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
  173. bld = OSSL_PARAM_BLD_new();
  174. if (bld == NULL)
  175. goto err;
  176. kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
  177. if (kdf == NULL)
  178. goto err;
  179. ctx = EVP_KDF_CTX_new(kdf);
  180. if (ctx == NULL)
  181. goto err;
  182. bnctx = BN_CTX_new_ex(libctx);
  183. if (bnctx == NULL)
  184. goto err;
  185. if (!add_params(bld, t->params, bnctx))
  186. goto err;
  187. params = OSSL_PARAM_BLD_to_param(bld);
  188. if (params == NULL)
  189. goto err;
  190. if (!EVP_KDF_CTX_set_params(ctx, params))
  191. goto err;
  192. if (t->expected_len > sizeof(out))
  193. goto err;
  194. if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
  195. goto err;
  196. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  197. if (memcmp(out, t->expected, t->expected_len) != 0)
  198. goto err;
  199. ret = 1;
  200. err:
  201. EVP_KDF_free(kdf);
  202. EVP_KDF_CTX_free(ctx);
  203. BN_CTX_free(bnctx);
  204. OSSL_PARAM_BLD_free_params(params);
  205. OSSL_PARAM_BLD_free(bld);
  206. OSSL_SELF_TEST_onend(st, ret);
  207. return ret;
  208. }
  209. static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
  210. OSSL_LIB_CTX *libctx)
  211. {
  212. int ret = 0;
  213. unsigned char out[256];
  214. EVP_RAND *rand;
  215. EVP_RAND_CTX *test = NULL, *drbg = NULL;
  216. unsigned int strength = 256;
  217. int prediction_resistance = 1; /* Causes a reseed */
  218. OSSL_PARAM drbg_params[3] = {
  219. OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
  220. };
  221. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
  222. rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
  223. if (rand == NULL)
  224. goto err;
  225. test = EVP_RAND_CTX_new(rand, NULL);
  226. EVP_RAND_free(rand);
  227. if (test == NULL)
  228. goto err;
  229. drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
  230. &strength);
  231. if (!EVP_RAND_set_ctx_params(test, drbg_params))
  232. goto err;
  233. rand = EVP_RAND_fetch(libctx, t->algorithm, NULL);
  234. if (rand == NULL)
  235. goto err;
  236. drbg = EVP_RAND_CTX_new(rand, test);
  237. EVP_RAND_free(rand);
  238. if (drbg == NULL)
  239. goto err;
  240. strength = EVP_RAND_strength(drbg);
  241. drbg_params[0] = OSSL_PARAM_construct_utf8_string(t->param_name,
  242. t->param_value, 0);
  243. /* This is only used by HMAC-DRBG but it is ignored by the others */
  244. drbg_params[1] =
  245. OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
  246. if (!EVP_RAND_set_ctx_params(drbg, drbg_params))
  247. goto err;
  248. drbg_params[0] =
  249. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  250. (void *)t->entropyin,
  251. t->entropyinlen);
  252. drbg_params[1] =
  253. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
  254. (void *)t->nonce, t->noncelen);
  255. if (!EVP_RAND_set_ctx_params(test, drbg_params)
  256. || !EVP_RAND_instantiate(test, strength, 0, NULL, 0))
  257. goto err;
  258. if (!EVP_RAND_instantiate(drbg, strength, 0, t->persstr, t->persstrlen))
  259. goto err;
  260. drbg_params[0] =
  261. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  262. (void *)t->entropyinpr1,
  263. t->entropyinpr1len);
  264. if (!EVP_RAND_set_ctx_params(test, drbg_params))
  265. goto err;
  266. if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
  267. prediction_resistance,
  268. t->entropyaddin1, t->entropyaddin1len))
  269. goto err;
  270. drbg_params[0] =
  271. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  272. (void *)t->entropyinpr2,
  273. t->entropyinpr2len);
  274. if (!EVP_RAND_set_ctx_params(test, drbg_params))
  275. goto err;
  276. /*
  277. * This calls ossl_prov_drbg_reseed() internally when
  278. * prediction_resistance = 1
  279. */
  280. if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
  281. prediction_resistance,
  282. t->entropyaddin2, t->entropyaddin2len))
  283. goto err;
  284. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  285. if (memcmp(out, t->expected, t->expectedlen) != 0)
  286. goto err;
  287. if (!EVP_RAND_uninstantiate(drbg))
  288. goto err;
  289. /*
  290. * Check that the DRBG data has been zeroized after
  291. * ossl_prov_drbg_uninstantiate.
  292. */
  293. if (!EVP_RAND_verify_zeroization(drbg))
  294. goto err;
  295. ret = 1;
  296. err:
  297. EVP_RAND_CTX_free(drbg);
  298. EVP_RAND_CTX_free(test);
  299. OSSL_SELF_TEST_onend(st, ret);
  300. return ret;
  301. }
  302. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  303. static int self_test_ka(const ST_KAT_KAS *t,
  304. OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  305. {
  306. int ret = 0;
  307. EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
  308. EVP_PKEY *pkey = NULL, *peerkey = NULL;
  309. OSSL_PARAM *params = NULL;
  310. OSSL_PARAM *params_peer = NULL;
  311. unsigned char secret[256];
  312. size_t secret_len;
  313. OSSL_PARAM_BLD *bld = NULL;
  314. BN_CTX *bnctx = NULL;
  315. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
  316. bnctx = BN_CTX_new_ex(libctx);
  317. if (bnctx == NULL)
  318. goto err;
  319. bld = OSSL_PARAM_BLD_new();
  320. if (bld == NULL)
  321. goto err;
  322. if (!add_params(bld, t->key_group, bnctx)
  323. || !add_params(bld, t->key_host_data, bnctx))
  324. goto err;
  325. params = OSSL_PARAM_BLD_to_param(bld);
  326. if (!add_params(bld, t->key_group, bnctx)
  327. || !add_params(bld, t->key_peer_data, bnctx))
  328. goto err;
  329. params_peer = OSSL_PARAM_BLD_to_param(bld);
  330. if (params == NULL || params_peer == NULL)
  331. goto err;
  332. /* Create a EVP_PKEY_CTX to load the DH keys into */
  333. kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
  334. if (kactx == NULL)
  335. goto err;
  336. if (EVP_PKEY_key_fromdata_init(kactx) <= 0
  337. || EVP_PKEY_fromdata(kactx, &pkey, params) <= 0)
  338. goto err;
  339. if (EVP_PKEY_key_fromdata_init(kactx) <= 0
  340. || EVP_PKEY_fromdata(kactx, &peerkey, params_peer) <= 0)
  341. goto err;
  342. /* Create a EVP_PKEY_CTX to perform key derivation */
  343. dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
  344. if (dctx == NULL)
  345. goto err;
  346. if (EVP_PKEY_derive_init(dctx) <= 0
  347. || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
  348. || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
  349. goto err;
  350. OSSL_SELF_TEST_oncorrupt_byte(st, secret);
  351. if (secret_len != t->expected_len
  352. || memcmp(secret, t->expected, t->expected_len) != 0)
  353. goto err;
  354. ret = 1;
  355. err:
  356. BN_CTX_free(bnctx);
  357. EVP_PKEY_free(pkey);
  358. EVP_PKEY_free(peerkey);
  359. EVP_PKEY_CTX_free(kactx);
  360. EVP_PKEY_CTX_free(dctx);
  361. OSSL_PARAM_BLD_free_params(params_peer);
  362. OSSL_PARAM_BLD_free_params(params);
  363. OSSL_PARAM_BLD_free(bld);
  364. OSSL_SELF_TEST_onend(st, ret);
  365. return ret;
  366. }
  367. #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
  368. static int self_test_sign(const ST_KAT_SIGN *t,
  369. OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  370. {
  371. int ret = 0;
  372. OSSL_PARAM *params = NULL, *params_sig = NULL;
  373. OSSL_PARAM_BLD *bld = NULL;
  374. EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
  375. EVP_PKEY *pkey = NULL;
  376. unsigned char sig[256];
  377. BN_CTX *bnctx = NULL;
  378. size_t siglen = 0;
  379. static const unsigned char dgst[] = {
  380. 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
  381. 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
  382. 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
  383. };
  384. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE, t->desc);
  385. bnctx = BN_CTX_new_ex(libctx);
  386. if (bnctx == NULL)
  387. goto err;
  388. bld = OSSL_PARAM_BLD_new();
  389. if (bld == NULL)
  390. goto err;
  391. if (!add_params(bld, t->key, bnctx))
  392. goto err;
  393. params = OSSL_PARAM_BLD_to_param(bld);
  394. /* Create a EVP_PKEY_CTX to load the DSA key into */
  395. kctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
  396. if (kctx == NULL || params == NULL)
  397. goto err;
  398. if (EVP_PKEY_key_fromdata_init(kctx) <= 0
  399. || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
  400. goto err;
  401. /* Create a EVP_PKEY_CTX to use for the signing operation */
  402. sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
  403. if (sctx == NULL
  404. || EVP_PKEY_sign_init(sctx) <= 0)
  405. goto err;
  406. /* set signature parameters */
  407. if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_SIGNATURE_PARAM_DIGEST,
  408. t->mdalgorithm,
  409. strlen(t->mdalgorithm) + 1))
  410. goto err;
  411. params_sig = OSSL_PARAM_BLD_to_param(bld);
  412. if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
  413. goto err;
  414. if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
  415. || EVP_PKEY_verify_init(sctx) <= 0
  416. || EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
  417. goto err;
  418. /*
  419. * Used by RSA, for other key types where the signature changes, we
  420. * can only use the verify.
  421. */
  422. if (t->sig_expected != NULL
  423. && (siglen != t->sig_expected_len
  424. || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
  425. goto err;
  426. OSSL_SELF_TEST_oncorrupt_byte(st, sig);
  427. if (EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
  428. goto err;
  429. ret = 1;
  430. err:
  431. BN_CTX_free(bnctx);
  432. EVP_PKEY_free(pkey);
  433. EVP_PKEY_CTX_free(kctx);
  434. EVP_PKEY_CTX_free(sctx);
  435. OSSL_PARAM_BLD_free_params(params);
  436. OSSL_PARAM_BLD_free_params(params_sig);
  437. OSSL_PARAM_BLD_free(bld);
  438. OSSL_SELF_TEST_onend(st, ret);
  439. return ret;
  440. }
  441. /*
  442. * Test an encrypt or decrypt KAT..
  443. *
  444. * FIPS 140-2 IG D.9 states that separate KAT tests are needed for encrypt
  445. * and decrypt..
  446. */
  447. static int self_test_asym_cipher(const ST_KAT_ASYM_CIPHER *t, OSSL_SELF_TEST *st,
  448. OSSL_LIB_CTX *libctx)
  449. {
  450. int ret = 0;
  451. OSSL_PARAM *keyparams = NULL, *initparams = NULL;
  452. OSSL_PARAM_BLD *keybld = NULL, *initbld = NULL;
  453. EVP_PKEY_CTX *encctx = NULL, *keyctx = NULL;
  454. EVP_PKEY *key = NULL;
  455. BN_CTX *bnctx = NULL;
  456. unsigned char out[256];
  457. size_t outlen = sizeof(out);
  458. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER, t->desc);
  459. bnctx = BN_CTX_new_ex(libctx);
  460. if (bnctx == NULL)
  461. goto err;
  462. /* Load a public or private key from data */
  463. keybld = OSSL_PARAM_BLD_new();
  464. if (keybld == NULL
  465. || !add_params(keybld, t->key, bnctx))
  466. goto err;
  467. keyparams = OSSL_PARAM_BLD_to_param(keybld);
  468. keyctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, NULL);
  469. if (keyctx == NULL || keyparams == NULL)
  470. goto err;
  471. if (EVP_PKEY_key_fromdata_init(keyctx) <= 0
  472. || EVP_PKEY_fromdata(keyctx, &key, keyparams) <= 0)
  473. goto err;
  474. /* Create a EVP_PKEY_CTX to use for the encrypt or decrypt operation */
  475. encctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL);
  476. if (encctx == NULL
  477. || (t->encrypt && EVP_PKEY_encrypt_init(encctx) <= 0)
  478. || (!t->encrypt && EVP_PKEY_decrypt_init(encctx) <= 0))
  479. goto err;
  480. /* Add any additional parameters such as padding */
  481. if (t->postinit != NULL) {
  482. initbld = OSSL_PARAM_BLD_new();
  483. if (initbld == NULL)
  484. goto err;
  485. if (!add_params(initbld, t->postinit, bnctx))
  486. goto err;
  487. initparams = OSSL_PARAM_BLD_to_param(initbld);
  488. if (initparams == NULL)
  489. goto err;
  490. if (EVP_PKEY_CTX_set_params(encctx, initparams) <= 0)
  491. goto err;
  492. }
  493. if (t->encrypt) {
  494. if (EVP_PKEY_encrypt(encctx, out, &outlen,
  495. t->in, t->in_len) <= 0)
  496. goto err;
  497. } else {
  498. if (EVP_PKEY_decrypt(encctx, out, &outlen,
  499. t->in, t->in_len) <= 0)
  500. goto err;
  501. }
  502. /* Check the KAT */
  503. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  504. if (outlen != t->expected_len
  505. || memcmp(out, t->expected, t->expected_len) != 0)
  506. goto err;
  507. ret = 1;
  508. err:
  509. BN_CTX_free(bnctx);
  510. EVP_PKEY_free(key);
  511. EVP_PKEY_CTX_free(encctx);
  512. EVP_PKEY_CTX_free(keyctx);
  513. OSSL_PARAM_BLD_free_params(keyparams);
  514. OSSL_PARAM_BLD_free(keybld);
  515. OSSL_PARAM_BLD_free_params(initparams);
  516. OSSL_PARAM_BLD_free(initbld);
  517. OSSL_SELF_TEST_onend(st, ret);
  518. return ret;
  519. }
  520. /*
  521. * Test a data driven list of KAT's for digest algorithms.
  522. * All tests are run regardless of if they fail or not.
  523. * Return 0 if any test fails.
  524. */
  525. static int self_test_digests(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  526. {
  527. int i, ret = 1;
  528. for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
  529. if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
  530. ret = 0;
  531. }
  532. return ret;
  533. }
  534. static int self_test_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  535. {
  536. int i, ret = 1;
  537. for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
  538. if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
  539. ret = 0;
  540. }
  541. return ret;
  542. }
  543. static int self_test_asym_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  544. {
  545. int i, ret = 1;
  546. for (i = 0; i < (int)OSSL_NELEM(st_kat_asym_cipher_tests); ++i) {
  547. if (!self_test_asym_cipher(&st_kat_asym_cipher_tests[i], st, libctx))
  548. ret = 0;
  549. }
  550. return ret;
  551. }
  552. static int self_test_kdfs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  553. {
  554. int i, ret = 1;
  555. for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
  556. if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
  557. ret = 0;
  558. }
  559. return ret;
  560. }
  561. static int self_test_drbgs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  562. {
  563. int i, ret = 1;
  564. for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
  565. if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
  566. ret = 0;
  567. }
  568. return ret;
  569. }
  570. static int self_test_kas(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  571. {
  572. int ret = 1;
  573. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
  574. int i;
  575. for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
  576. if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
  577. ret = 0;
  578. }
  579. #endif
  580. return ret;
  581. }
  582. static int self_test_signatures(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  583. {
  584. int i, ret = 1;
  585. for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
  586. if (!self_test_sign(&st_kat_sign_tests[i], st, libctx))
  587. ret = 0;
  588. }
  589. return ret;
  590. }
  591. /*
  592. * Run the algorithm KAT's.
  593. * Return 1 is successful, otherwise return 0.
  594. * This runs all the tests regardless of if any fail.
  595. */
  596. int SELF_TEST_kats(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  597. {
  598. int ret = 1;
  599. if (!self_test_digests(st, libctx))
  600. ret = 0;
  601. if (!self_test_ciphers(st, libctx))
  602. ret = 0;
  603. if (!self_test_signatures(st, libctx))
  604. ret = 0;
  605. if (!self_test_kdfs(st, libctx))
  606. ret = 0;
  607. if (!self_test_drbgs(st, libctx))
  608. ret = 0;
  609. if (!self_test_kas(st, libctx))
  610. ret = 0;
  611. if (!self_test_asym_ciphers(st, libctx))
  612. ret = 0;
  613. return ret;
  614. }