self_test_kats.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. default:
  150. break;
  151. }
  152. }
  153. ret = 1;
  154. err:
  155. return ret;
  156. }
  157. static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
  158. OSSL_LIB_CTX *libctx)
  159. {
  160. int ret = 0;
  161. unsigned char out[64];
  162. EVP_KDF *kdf = NULL;
  163. EVP_KDF_CTX *ctx = NULL;
  164. BN_CTX *bnctx = NULL;
  165. OSSL_PARAM *params = NULL;
  166. OSSL_PARAM_BLD *bld = NULL;
  167. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
  168. bld = OSSL_PARAM_BLD_new();
  169. if (bld == NULL)
  170. goto err;
  171. kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
  172. if (kdf == NULL)
  173. goto err;
  174. ctx = EVP_KDF_CTX_new(kdf);
  175. if (ctx == NULL)
  176. goto err;
  177. bnctx = BN_CTX_new_ex(libctx);
  178. if (bnctx == NULL)
  179. goto err;
  180. if (!add_params(bld, t->params, bnctx))
  181. goto err;
  182. params = OSSL_PARAM_BLD_to_param(bld);
  183. if (params == NULL)
  184. goto err;
  185. if (!EVP_KDF_CTX_set_params(ctx, params))
  186. goto err;
  187. if (t->expected_len > sizeof(out))
  188. goto err;
  189. if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
  190. goto err;
  191. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  192. if (memcmp(out, t->expected, t->expected_len) != 0)
  193. goto err;
  194. ret = 1;
  195. err:
  196. EVP_KDF_free(kdf);
  197. EVP_KDF_CTX_free(ctx);
  198. BN_CTX_free(bnctx);
  199. OSSL_PARAM_BLD_free_params(params);
  200. OSSL_PARAM_BLD_free(bld);
  201. OSSL_SELF_TEST_onend(st, ret);
  202. return ret;
  203. }
  204. static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
  205. OSSL_LIB_CTX *libctx)
  206. {
  207. int ret = 0;
  208. unsigned char out[256];
  209. EVP_RAND *rand;
  210. EVP_RAND_CTX *test = NULL, *drbg = NULL;
  211. unsigned int strength = 256;
  212. int prediction_resistance = 1; /* Causes a reseed */
  213. OSSL_PARAM drbg_params[3] = {
  214. OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
  215. };
  216. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
  217. rand = EVP_RAND_fetch(libctx, "TEST-RAND", NULL);
  218. if (rand == NULL)
  219. goto err;
  220. test = EVP_RAND_CTX_new(rand, NULL);
  221. EVP_RAND_free(rand);
  222. if (test == NULL)
  223. goto err;
  224. drbg_params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH,
  225. &strength);
  226. if (!EVP_RAND_set_ctx_params(test, drbg_params))
  227. goto err;
  228. rand = EVP_RAND_fetch(libctx, t->algorithm, NULL);
  229. if (rand == NULL)
  230. goto err;
  231. drbg = EVP_RAND_CTX_new(rand, test);
  232. EVP_RAND_free(rand);
  233. if (drbg == NULL)
  234. goto err;
  235. strength = EVP_RAND_strength(drbg);
  236. drbg_params[0] = OSSL_PARAM_construct_utf8_string(t->param_name,
  237. t->param_value, 0);
  238. /* This is only used by HMAC-DRBG but it is ignored by the others */
  239. drbg_params[1] =
  240. OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_MAC, "HMAC", 0);
  241. if (!EVP_RAND_set_ctx_params(drbg, drbg_params))
  242. goto err;
  243. drbg_params[0] =
  244. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  245. (void *)t->entropyin,
  246. t->entropyinlen);
  247. drbg_params[1] =
  248. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
  249. (void *)t->nonce, t->noncelen);
  250. if (!EVP_RAND_set_ctx_params(test, drbg_params)
  251. || !EVP_RAND_instantiate(test, strength, 0, NULL, 0))
  252. goto err;
  253. if (!EVP_RAND_instantiate(drbg, strength, 0, t->persstr, t->persstrlen))
  254. goto err;
  255. drbg_params[0] =
  256. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  257. (void *)t->entropyinpr1,
  258. t->entropyinpr1len);
  259. if (!EVP_RAND_set_ctx_params(test, drbg_params))
  260. goto err;
  261. if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
  262. prediction_resistance,
  263. t->entropyaddin1, t->entropyaddin1len))
  264. goto err;
  265. drbg_params[0] =
  266. OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  267. (void *)t->entropyinpr2,
  268. t->entropyinpr2len);
  269. if (!EVP_RAND_set_ctx_params(test, drbg_params))
  270. goto err;
  271. /*
  272. * This calls ossl_prov_drbg_reseed() internally when
  273. * prediction_resistance = 1
  274. */
  275. if (!EVP_RAND_generate(drbg, out, t->expectedlen, strength,
  276. prediction_resistance,
  277. t->entropyaddin2, t->entropyaddin2len))
  278. goto err;
  279. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  280. if (memcmp(out, t->expected, t->expectedlen) != 0)
  281. goto err;
  282. if (!EVP_RAND_uninstantiate(drbg))
  283. goto err;
  284. /*
  285. * Check that the DRBG data has been zeroized after
  286. * ossl_prov_drbg_uninstantiate.
  287. */
  288. if (!EVP_RAND_verify_zeroization(drbg))
  289. goto err;
  290. ret = 1;
  291. err:
  292. EVP_RAND_CTX_free(drbg);
  293. EVP_RAND_CTX_free(test);
  294. OSSL_SELF_TEST_onend(st, ret);
  295. return ret;
  296. }
  297. static int self_test_ka(const ST_KAT_KAS *t,
  298. OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  299. {
  300. int ret = 0;
  301. EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
  302. EVP_PKEY *pkey = NULL, *peerkey = NULL;
  303. OSSL_PARAM *params = NULL;
  304. OSSL_PARAM *params_peer = NULL;
  305. unsigned char secret[256];
  306. size_t secret_len;
  307. OSSL_PARAM_BLD *bld = NULL;
  308. BN_CTX *bnctx = NULL;
  309. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
  310. bnctx = BN_CTX_new_ex(libctx);
  311. if (bnctx == NULL)
  312. goto err;
  313. bld = OSSL_PARAM_BLD_new();
  314. if (bld == NULL)
  315. goto err;
  316. if (!add_params(bld, t->key_group, bnctx)
  317. || !add_params(bld, t->key_host_data, bnctx))
  318. goto err;
  319. params = OSSL_PARAM_BLD_to_param(bld);
  320. if (!add_params(bld, t->key_group, bnctx)
  321. || !add_params(bld, t->key_peer_data, bnctx))
  322. goto err;
  323. params_peer = OSSL_PARAM_BLD_to_param(bld);
  324. if (params == NULL || params_peer == NULL)
  325. goto err;
  326. /* Create a EVP_PKEY_CTX to load the DH keys into */
  327. kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
  328. if (kactx == NULL)
  329. goto err;
  330. if (EVP_PKEY_key_fromdata_init(kactx) <= 0
  331. || EVP_PKEY_fromdata(kactx, &pkey, params) <= 0)
  332. goto err;
  333. if (EVP_PKEY_key_fromdata_init(kactx) <= 0
  334. || EVP_PKEY_fromdata(kactx, &peerkey, params_peer) <= 0)
  335. goto err;
  336. /* Create a EVP_PKEY_CTX to perform key derivation */
  337. dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
  338. if (dctx == NULL)
  339. goto err;
  340. if (EVP_PKEY_derive_init(dctx) <= 0
  341. || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
  342. || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
  343. goto err;
  344. OSSL_SELF_TEST_oncorrupt_byte(st, secret);
  345. if (secret_len != t->expected_len
  346. || memcmp(secret, t->expected, t->expected_len) != 0)
  347. goto err;
  348. ret = 1;
  349. err:
  350. BN_CTX_free(bnctx);
  351. EVP_PKEY_free(pkey);
  352. EVP_PKEY_free(peerkey);
  353. EVP_PKEY_CTX_free(kactx);
  354. EVP_PKEY_CTX_free(dctx);
  355. OSSL_PARAM_BLD_free_params(params_peer);
  356. OSSL_PARAM_BLD_free_params(params);
  357. OSSL_PARAM_BLD_free(bld);
  358. OSSL_SELF_TEST_onend(st, ret);
  359. return ret;
  360. }
  361. static int self_test_sign(const ST_KAT_SIGN *t,
  362. OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  363. {
  364. int ret = 0;
  365. OSSL_PARAM *params = NULL, *params_sig = NULL;
  366. OSSL_PARAM_BLD *bld = NULL;
  367. EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
  368. EVP_PKEY *pkey = NULL;
  369. unsigned char sig[256];
  370. BN_CTX *bnctx = NULL;
  371. size_t siglen = 0;
  372. static const unsigned char dgst[] = {
  373. 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
  374. 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
  375. 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
  376. };
  377. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE, t->desc);
  378. bnctx = BN_CTX_new_ex(libctx);
  379. if (bnctx == NULL)
  380. goto err;
  381. bld = OSSL_PARAM_BLD_new();
  382. if (bld == NULL)
  383. goto err;
  384. if (!add_params(bld, t->key, bnctx))
  385. goto err;
  386. params = OSSL_PARAM_BLD_to_param(bld);
  387. /* Create a EVP_PKEY_CTX to load the DSA key into */
  388. kctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
  389. if (kctx == NULL || params == NULL)
  390. goto err;
  391. if (EVP_PKEY_key_fromdata_init(kctx) <= 0
  392. || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
  393. goto err;
  394. /* Create a EVP_PKEY_CTX to use for the signing operation */
  395. sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
  396. if (sctx == NULL
  397. || EVP_PKEY_sign_init(sctx) <= 0)
  398. goto err;
  399. /* set signature parameters */
  400. if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_SIGNATURE_PARAM_DIGEST,
  401. t->mdalgorithm,
  402. strlen(t->mdalgorithm) + 1))
  403. goto err;
  404. params_sig = OSSL_PARAM_BLD_to_param(bld);
  405. if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
  406. goto err;
  407. if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
  408. || EVP_PKEY_verify_init(sctx) <= 0
  409. || EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
  410. goto err;
  411. /*
  412. * Used by RSA, for other key types where the signature changes, we
  413. * can only use the verify.
  414. */
  415. if (t->sig_expected != NULL
  416. && (siglen != t->sig_expected_len
  417. || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
  418. goto err;
  419. OSSL_SELF_TEST_oncorrupt_byte(st, sig);
  420. if (EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
  421. goto err;
  422. ret = 1;
  423. err:
  424. BN_CTX_free(bnctx);
  425. EVP_PKEY_free(pkey);
  426. EVP_PKEY_CTX_free(kctx);
  427. EVP_PKEY_CTX_free(sctx);
  428. OSSL_PARAM_BLD_free_params(params);
  429. OSSL_PARAM_BLD_free_params(params_sig);
  430. OSSL_PARAM_BLD_free(bld);
  431. OSSL_SELF_TEST_onend(st, ret);
  432. return ret;
  433. }
  434. /*
  435. * Test an encrypt or decrypt KAT..
  436. *
  437. * FIPS 140-2 IG D.9 states that separate KAT tests are needed for encrypt
  438. * and decrypt..
  439. */
  440. static int self_test_asym_cipher(const ST_KAT_ASYM_CIPHER *t, OSSL_SELF_TEST *st,
  441. OSSL_LIB_CTX *libctx)
  442. {
  443. int ret = 0;
  444. OSSL_PARAM *keyparams = NULL, *initparams = NULL;
  445. OSSL_PARAM_BLD *keybld = NULL, *initbld = NULL;
  446. EVP_PKEY_CTX *encctx = NULL, *keyctx = NULL;
  447. EVP_PKEY *key = NULL;
  448. BN_CTX *bnctx = NULL;
  449. unsigned char out[256];
  450. size_t outlen = sizeof(out);
  451. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_ASYM_CIPHER, t->desc);
  452. bnctx = BN_CTX_new_ex(libctx);
  453. if (bnctx == NULL)
  454. goto err;
  455. /* Load a public or private key from data */
  456. keybld = OSSL_PARAM_BLD_new();
  457. if (keybld == NULL
  458. || !add_params(keybld, t->key, bnctx))
  459. goto err;
  460. keyparams = OSSL_PARAM_BLD_to_param(keybld);
  461. keyctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, NULL);
  462. if (keyctx == NULL || keyparams == NULL)
  463. goto err;
  464. if (EVP_PKEY_key_fromdata_init(keyctx) <= 0
  465. || EVP_PKEY_fromdata(keyctx, &key, keyparams) <= 0)
  466. goto err;
  467. /* Create a EVP_PKEY_CTX to use for the encrypt or decrypt operation */
  468. encctx = EVP_PKEY_CTX_new_from_pkey(libctx, key, NULL);
  469. if (encctx == NULL
  470. || (t->encrypt && EVP_PKEY_encrypt_init(encctx) <= 0)
  471. || (!t->encrypt && EVP_PKEY_decrypt_init(encctx) <= 0))
  472. goto err;
  473. /* Add any additional parameters such as padding */
  474. if (t->postinit != NULL) {
  475. initbld = OSSL_PARAM_BLD_new();
  476. if (initbld == NULL)
  477. goto err;
  478. if (!add_params(initbld, t->postinit, bnctx))
  479. goto err;
  480. initparams = OSSL_PARAM_BLD_to_param(initbld);
  481. if (initparams == NULL)
  482. goto err;
  483. if (EVP_PKEY_CTX_set_params(encctx, initparams) <= 0)
  484. goto err;
  485. }
  486. if (t->encrypt) {
  487. if (EVP_PKEY_encrypt(encctx, out, &outlen,
  488. t->in, t->in_len) <= 0)
  489. goto err;
  490. } else {
  491. if (EVP_PKEY_decrypt(encctx, out, &outlen,
  492. t->in, t->in_len) <= 0)
  493. goto err;
  494. }
  495. /* Check the KAT */
  496. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  497. if (outlen != t->expected_len
  498. || memcmp(out, t->expected, t->expected_len) != 0)
  499. goto err;
  500. ret = 1;
  501. err:
  502. BN_CTX_free(bnctx);
  503. EVP_PKEY_free(key);
  504. EVP_PKEY_CTX_free(encctx);
  505. EVP_PKEY_CTX_free(keyctx);
  506. OSSL_PARAM_BLD_free_params(keyparams);
  507. OSSL_PARAM_BLD_free(keybld);
  508. OSSL_PARAM_BLD_free_params(initparams);
  509. OSSL_PARAM_BLD_free(initbld);
  510. OSSL_SELF_TEST_onend(st, ret);
  511. return ret;
  512. }
  513. /*
  514. * Test a data driven list of KAT's for digest algorithms.
  515. * All tests are run regardless of if they fail or not.
  516. * Return 0 if any test fails.
  517. */
  518. static int self_test_digests(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  519. {
  520. int i, ret = 1;
  521. for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
  522. if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
  523. ret = 0;
  524. }
  525. return ret;
  526. }
  527. static int self_test_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  528. {
  529. int i, ret = 1;
  530. for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
  531. if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
  532. ret = 0;
  533. }
  534. return ret;
  535. }
  536. static int self_test_asym_ciphers(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  537. {
  538. int i, ret = 1;
  539. for (i = 0; i < (int)OSSL_NELEM(st_kat_asym_cipher_tests); ++i) {
  540. if (!self_test_asym_cipher(&st_kat_asym_cipher_tests[i], st, libctx))
  541. ret = 0;
  542. }
  543. return ret;
  544. }
  545. static int self_test_kdfs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  546. {
  547. int i, ret = 1;
  548. for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
  549. if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
  550. ret = 0;
  551. }
  552. return ret;
  553. }
  554. static int self_test_drbgs(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  555. {
  556. int i, ret = 1;
  557. for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
  558. if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
  559. ret = 0;
  560. }
  561. return ret;
  562. }
  563. static int self_test_kas(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  564. {
  565. int i, ret = 1;
  566. for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
  567. if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
  568. ret = 0;
  569. }
  570. return ret;
  571. }
  572. static int self_test_signatures(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  573. {
  574. int i, ret = 1;
  575. for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
  576. if (!self_test_sign(&st_kat_sign_tests[i], st, libctx))
  577. ret = 0;
  578. }
  579. return ret;
  580. }
  581. /*
  582. * Run the algorithm KAT's.
  583. * Return 1 is successful, otherwise return 0.
  584. * This runs all the tests regardless of if any fail.
  585. */
  586. int SELF_TEST_kats(OSSL_SELF_TEST *st, OSSL_LIB_CTX *libctx)
  587. {
  588. int ret = 1;
  589. if (!self_test_digests(st, libctx))
  590. ret = 0;
  591. if (!self_test_ciphers(st, libctx))
  592. ret = 0;
  593. if (!self_test_signatures(st, libctx))
  594. ret = 0;
  595. if (!self_test_kdfs(st, libctx))
  596. ret = 0;
  597. if (!self_test_drbgs(st, libctx))
  598. ret = 0;
  599. if (!self_test_kas(st, libctx))
  600. ret = 0;
  601. if (!self_test_asym_ciphers(st, libctx))
  602. ret = 0;
  603. return ret;
  604. }