self_test_kats.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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/rand_drbg.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/param_build.h>
  15. #include "internal/cryptlib.h"
  16. #include "internal/nelem.h"
  17. #include "self_test.h"
  18. #include "self_test_data.inc"
  19. #include "../../crypto/rand/rand_local.h"
  20. #define DRBG_PARAM_ENTROPY "DRBG-ENTROPY"
  21. #define DRBG_PARAM_NONCE "DRBG-NONCE"
  22. static int self_test_digest(const ST_KAT_DIGEST *t, OSSL_SELF_TEST *st,
  23. OPENSSL_CTX *libctx)
  24. {
  25. int ok = 0;
  26. unsigned char out[EVP_MAX_MD_SIZE];
  27. unsigned int out_len = 0;
  28. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  29. EVP_MD *md = EVP_MD_fetch(libctx, t->algorithm, NULL);
  30. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_DIGEST, t->desc);
  31. if (ctx == NULL
  32. || md == NULL
  33. || !EVP_DigestInit_ex(ctx, md, NULL)
  34. || !EVP_DigestUpdate(ctx, t->pt, t->pt_len)
  35. || !EVP_DigestFinal(ctx, out, &out_len))
  36. goto err;
  37. /* Optional corruption */
  38. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  39. if (out_len != t->expected_len
  40. || memcmp(out, t->expected, out_len) != 0)
  41. goto err;
  42. ok = 1;
  43. err:
  44. EVP_MD_free(md);
  45. EVP_MD_CTX_free(ctx);
  46. OSSL_SELF_TEST_onend(st, ok);
  47. return ok;
  48. }
  49. /*
  50. * Helper function to setup a EVP_CipherInit
  51. * Used to hide the complexity of Authenticated ciphers.
  52. */
  53. static int cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  54. const ST_KAT_CIPHER *t, int enc)
  55. {
  56. unsigned char *in_tag = NULL;
  57. int pad = 0, tmp;
  58. /* Flag required for Key wrapping */
  59. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  60. if (t->tag == NULL) {
  61. /* Use a normal cipher init */
  62. return EVP_CipherInit_ex(ctx, cipher, NULL, t->key, t->iv, enc)
  63. && EVP_CIPHER_CTX_set_padding(ctx, pad);
  64. }
  65. /* The authenticated cipher init */
  66. if (!enc)
  67. in_tag = (unsigned char *)t->tag;
  68. return EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)
  69. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, t->iv_len, NULL)
  70. && (in_tag == NULL
  71. || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, t->tag_len,
  72. in_tag))
  73. && EVP_CipherInit_ex(ctx, NULL, NULL, t->key, t->iv, enc)
  74. && EVP_CIPHER_CTX_set_padding(ctx, pad)
  75. && EVP_CipherUpdate(ctx, NULL, &tmp, t->aad, t->aad_len);
  76. }
  77. /* Test a single KAT for encrypt/decrypt */
  78. static int self_test_cipher(const ST_KAT_CIPHER *t, OSSL_SELF_TEST *st,
  79. OPENSSL_CTX *libctx)
  80. {
  81. int ret = 0, encrypt = 1, len, ct_len = 0, pt_len = 0;
  82. EVP_CIPHER_CTX *ctx = NULL;
  83. EVP_CIPHER *cipher = NULL;
  84. unsigned char ct_buf[256] = { 0 };
  85. unsigned char pt_buf[256] = { 0 };
  86. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_CIPHER, t->base.desc);
  87. ctx = EVP_CIPHER_CTX_new();
  88. if (ctx == NULL)
  89. goto err;
  90. cipher = EVP_CIPHER_fetch(libctx, t->base.algorithm, "");
  91. if (cipher == NULL)
  92. goto err;
  93. /* Encrypt plain text message */
  94. if (!cipher_init(ctx, cipher, t, encrypt)
  95. || !EVP_CipherUpdate(ctx, ct_buf, &len, t->base.pt, t->base.pt_len)
  96. || !EVP_CipherFinal_ex(ctx, ct_buf + len, &ct_len))
  97. goto err;
  98. OSSL_SELF_TEST_oncorrupt_byte(st, ct_buf);
  99. ct_len += len;
  100. if (ct_len != (int)t->base.expected_len
  101. || memcmp(t->base.expected, ct_buf, ct_len) != 0)
  102. goto err;
  103. if (t->tag != NULL) {
  104. unsigned char tag[16] = { 0 };
  105. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, t->tag_len, tag)
  106. || memcmp(tag, t->tag, t->tag_len) != 0)
  107. goto err;
  108. }
  109. if (!(cipher_init(ctx, cipher, t, !encrypt)
  110. && EVP_CipherUpdate(ctx, pt_buf, &len, ct_buf, ct_len)
  111. && EVP_CipherFinal_ex(ctx, pt_buf + len, &pt_len)))
  112. goto err;
  113. pt_len += len;
  114. if (pt_len != (int)t->base.pt_len
  115. || memcmp(pt_buf, t->base.pt, pt_len) != 0)
  116. goto err;
  117. ret = 1;
  118. err:
  119. EVP_CIPHER_free(cipher);
  120. EVP_CIPHER_CTX_free(ctx);
  121. OSSL_SELF_TEST_onend(st, ret);
  122. return ret;
  123. }
  124. static int add_params(OSSL_PARAM_BLD *bld, const ST_KAT_PARAM *params,
  125. BN_CTX *ctx)
  126. {
  127. int ret = 0;
  128. const ST_KAT_PARAM *p;
  129. if (params == NULL)
  130. return 1;
  131. for (p = params; p->data != NULL; ++p)
  132. {
  133. switch (p->type) {
  134. case OSSL_PARAM_UNSIGNED_INTEGER: {
  135. BIGNUM *bn = BN_CTX_get(ctx);
  136. if (bn == NULL
  137. || (BN_bin2bn(p->data, p->data_len, bn) == NULL)
  138. || !OSSL_PARAM_BLD_push_BN(bld, p->name, bn))
  139. goto err;
  140. break;
  141. }
  142. case OSSL_PARAM_UTF8_STRING: {
  143. if (!OSSL_PARAM_BLD_push_utf8_string(bld, p->name, p->data, 0))
  144. goto err;
  145. break;
  146. }
  147. case OSSL_PARAM_OCTET_STRING: {
  148. if (!OSSL_PARAM_BLD_push_octet_string(bld, p->name, p->data,
  149. p->data_len))
  150. goto err;
  151. break;
  152. }
  153. default:
  154. break;
  155. }
  156. }
  157. ret = 1;
  158. err:
  159. return ret;
  160. }
  161. static int self_test_kdf(const ST_KAT_KDF *t, OSSL_SELF_TEST *st,
  162. OPENSSL_CTX *libctx)
  163. {
  164. int ret = 0;
  165. unsigned char out[64];
  166. EVP_KDF *kdf = NULL;
  167. EVP_KDF_CTX *ctx = NULL;
  168. BN_CTX *bnctx = NULL;
  169. OSSL_PARAM *params = NULL;
  170. OSSL_PARAM_BLD *bld = NULL;
  171. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KDF, t->desc);
  172. bld = OSSL_PARAM_BLD_new();
  173. if (bld == NULL)
  174. goto err;
  175. kdf = EVP_KDF_fetch(libctx, t->algorithm, "");
  176. if (kdf == NULL)
  177. goto err;
  178. ctx = EVP_KDF_new_ctx(kdf);
  179. if (ctx == NULL)
  180. goto err;
  181. bnctx = BN_CTX_new_ex(libctx);
  182. if (bnctx == NULL)
  183. goto err;
  184. if (!add_params(bld, t->params, bnctx))
  185. goto err;
  186. params = OSSL_PARAM_BLD_to_param(bld);
  187. if (params == NULL)
  188. goto err;
  189. if (!EVP_KDF_set_ctx_params(ctx, params))
  190. goto err;
  191. if (t->expected_len > sizeof(out))
  192. goto err;
  193. if (EVP_KDF_derive(ctx, out, t->expected_len) <= 0)
  194. goto err;
  195. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  196. if (memcmp(out, t->expected, t->expected_len) != 0)
  197. goto err;
  198. ret = 1;
  199. err:
  200. EVP_KDF_free(kdf);
  201. EVP_KDF_free_ctx(ctx);
  202. BN_CTX_free(bnctx);
  203. OSSL_PARAM_BLD_free_params(params);
  204. OSSL_PARAM_BLD_free(bld);
  205. OSSL_SELF_TEST_onend(st, ret);
  206. return ret;
  207. }
  208. static size_t drbg_kat_entropy_cb(RAND_DRBG *drbg, unsigned char **pout,
  209. int entropy, size_t min_len, size_t max_len,
  210. int prediction_resistance)
  211. {
  212. OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
  213. OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_ENTROPY);
  214. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  215. return 0;
  216. *pout = (unsigned char *)p->data;
  217. return p->data_size;
  218. }
  219. static size_t drbg_kat_nonce_cb(RAND_DRBG *drbg, unsigned char **pout,
  220. int entropy, size_t min_len, size_t max_len)
  221. {
  222. OSSL_PARAM *drbg_params = RAND_DRBG_get_callback_data(drbg);
  223. OSSL_PARAM *p = OSSL_PARAM_locate(drbg_params, DRBG_PARAM_NONCE);
  224. if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING)
  225. return 0;
  226. *pout = (unsigned char *)p->data;
  227. return p->data_size;
  228. }
  229. static int self_test_drbg(const ST_KAT_DRBG *t, OSSL_SELF_TEST *st,
  230. OPENSSL_CTX *libctx)
  231. {
  232. int ret = 0;
  233. unsigned char out[256];
  234. RAND_DRBG *drbg = NULL;
  235. unsigned int flags = 0;
  236. int prediction_resistance = 1; /* Causes a reseed */
  237. OSSL_PARAM drbg_params[3] = {
  238. OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
  239. };
  240. static const unsigned char zero[sizeof(drbg->data)] = { 0 };
  241. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_DRBG, t->desc);
  242. if (strcmp(t->desc, OSSL_SELF_TEST_DESC_DRBG_HMAC) == 0)
  243. flags |= RAND_DRBG_FLAG_HMAC;
  244. drbg = RAND_DRBG_new_ex(libctx, t->nid, flags, NULL);
  245. if (drbg == NULL)
  246. goto err;
  247. if (!RAND_DRBG_set_callback_data(drbg, drbg_params))
  248. goto err;
  249. if (!RAND_DRBG_set_callbacks(drbg, drbg_kat_entropy_cb, NULL,
  250. drbg_kat_nonce_cb, NULL))
  251. goto err;
  252. drbg_params[0] =
  253. OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
  254. (void *)t->entropyin, t->entropyinlen);
  255. drbg_params[1] =
  256. OSSL_PARAM_construct_octet_string(DRBG_PARAM_NONCE,
  257. (void *)t->nonce, t->noncelen);
  258. if (!RAND_DRBG_instantiate(drbg, t->persstr, t->persstrlen))
  259. goto err;
  260. drbg_params[0] =
  261. OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
  262. (void *)t->entropyinpr1,
  263. t->entropyinpr1len);
  264. if (!RAND_DRBG_generate(drbg, out, t->expectedlen, prediction_resistance,
  265. t->entropyaddin1, t->entropyaddin1len))
  266. goto err;
  267. drbg_params[0] =
  268. OSSL_PARAM_construct_octet_string(DRBG_PARAM_ENTROPY,
  269. (void *)t->entropyinpr2,
  270. t->entropyinpr2len);
  271. /* This calls RAND_DRBG_reseed() internally when prediction_resistance = 1 */
  272. if (!RAND_DRBG_generate(drbg, out, t->expectedlen, prediction_resistance,
  273. t->entropyaddin2, t->entropyaddin2len))
  274. goto err;
  275. OSSL_SELF_TEST_oncorrupt_byte(st, out);
  276. if (memcmp(out, t->expected, t->expectedlen) != 0)
  277. goto err;
  278. if (!RAND_DRBG_uninstantiate(drbg))
  279. goto err;
  280. /*
  281. * Check that the DRBG data has been zeroized after RAND_DRBG_uninstantiate.
  282. */
  283. if (memcmp((unsigned char *)&drbg->data, zero, sizeof(drbg->data)) != 0)
  284. goto err;
  285. ret = 1;
  286. err:
  287. RAND_DRBG_free(drbg);
  288. OSSL_SELF_TEST_onend(st, ret);
  289. return ret;
  290. }
  291. static int self_test_ka(const ST_KAT_KAS *t,
  292. OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  293. {
  294. int ret = 0;
  295. EVP_PKEY_CTX *kactx = NULL, *dctx = NULL;
  296. EVP_PKEY *pkey = NULL, *peerkey = NULL;
  297. OSSL_PARAM *params = NULL;
  298. OSSL_PARAM *params_peer = NULL;
  299. unsigned char secret[256];
  300. size_t secret_len;
  301. OSSL_PARAM_BLD *bld = NULL;
  302. BN_CTX *bnctx = NULL;
  303. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_KA, t->desc);
  304. bnctx = BN_CTX_new_ex(libctx);
  305. if (bnctx == NULL)
  306. goto err;
  307. bld = OSSL_PARAM_BLD_new();
  308. if (bld == NULL)
  309. goto err;
  310. if (!add_params(bld, t->key_group, bnctx)
  311. || !add_params(bld, t->key_host_data, bnctx))
  312. goto err;
  313. params = OSSL_PARAM_BLD_to_param(bld);
  314. if (!add_params(bld, t->key_group, bnctx)
  315. || !add_params(bld, t->key_peer_data, bnctx))
  316. goto err;
  317. params_peer = OSSL_PARAM_BLD_to_param(bld);
  318. if (params == NULL || params_peer == NULL)
  319. goto err;
  320. /* Create a EVP_PKEY_CTX to load the DH keys into */
  321. kactx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
  322. if (kactx == NULL)
  323. goto err;
  324. if (EVP_PKEY_key_fromdata_init(kactx) <= 0
  325. || EVP_PKEY_fromdata(kactx, &pkey, params) <= 0)
  326. goto err;
  327. if (EVP_PKEY_key_fromdata_init(kactx) <= 0
  328. || EVP_PKEY_fromdata(kactx, &peerkey, params_peer) <= 0)
  329. goto err;
  330. /* Create a EVP_PKEY_CTX to perform key derivation */
  331. dctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
  332. if (dctx == NULL)
  333. goto err;
  334. if (EVP_PKEY_derive_init(dctx) <= 0
  335. || EVP_PKEY_derive_set_peer(dctx, peerkey) <= 0
  336. || EVP_PKEY_derive(dctx, secret, &secret_len) <= 0)
  337. goto err;
  338. OSSL_SELF_TEST_oncorrupt_byte(st, secret);
  339. if (secret_len != t->expected_len
  340. || memcmp(secret, t->expected, t->expected_len) != 0)
  341. goto err;
  342. ret = 1;
  343. err:
  344. BN_CTX_free(bnctx);
  345. EVP_PKEY_free(pkey);
  346. EVP_PKEY_free(peerkey);
  347. EVP_PKEY_CTX_free(kactx);
  348. EVP_PKEY_CTX_free(dctx);
  349. OSSL_PARAM_BLD_free_params(params_peer);
  350. OSSL_PARAM_BLD_free_params(params);
  351. OSSL_PARAM_BLD_free(bld);
  352. OSSL_SELF_TEST_onend(st, ret);
  353. return ret;
  354. }
  355. static int self_test_sign(const ST_KAT_SIGN *t,
  356. OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  357. {
  358. int ret = 0;
  359. OSSL_PARAM *params = NULL, *params_sig = NULL;
  360. OSSL_PARAM_BLD *bld = NULL;
  361. EVP_PKEY_CTX *sctx = NULL, *kctx = NULL;
  362. EVP_PKEY *pkey = NULL;
  363. unsigned char sig[256];
  364. BN_CTX *bnctx = NULL;
  365. size_t siglen = 0;
  366. static const unsigned char dgst[] = {
  367. 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
  368. 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
  369. 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
  370. };
  371. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_KAT_SIGNATURE, t->desc);
  372. bnctx = BN_CTX_new_ex(libctx);
  373. if (bnctx == NULL)
  374. goto err;
  375. bld = OSSL_PARAM_BLD_new();
  376. if (bld == NULL)
  377. goto err;
  378. if (!add_params(bld, t->key, bnctx))
  379. goto err;
  380. params = OSSL_PARAM_BLD_to_param(bld);
  381. /* Create a EVP_PKEY_CTX to load the DSA key into */
  382. kctx = EVP_PKEY_CTX_new_from_name(libctx, t->algorithm, "");
  383. if (kctx == NULL || params == NULL)
  384. goto err;
  385. if (EVP_PKEY_key_fromdata_init(kctx) <= 0
  386. || EVP_PKEY_fromdata(kctx, &pkey, params) <= 0)
  387. goto err;
  388. /* Create a EVP_PKEY_CTX to use for the signing operation */
  389. sctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, NULL);
  390. if (sctx == NULL
  391. || EVP_PKEY_sign_init(sctx) <= 0)
  392. goto err;
  393. /* set signature parameters */
  394. if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_SIGNATURE_PARAM_DIGEST,
  395. t->mdalgorithm,
  396. strlen(t->mdalgorithm) + 1))
  397. goto err;
  398. params_sig = OSSL_PARAM_BLD_to_param(bld);
  399. if (EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
  400. goto err;
  401. if (EVP_PKEY_sign(sctx, sig, &siglen, dgst, sizeof(dgst)) <= 0
  402. || EVP_PKEY_verify_init(sctx) <= 0
  403. || EVP_PKEY_CTX_set_params(sctx, params_sig) <= 0)
  404. goto err;
  405. /*
  406. * Used by RSA, for other key types where the signature changes, we
  407. * can only use the verify.
  408. */
  409. if (t->sig_expected != NULL
  410. && (siglen != t->sig_expected_len
  411. || memcmp(sig, t->sig_expected, t->sig_expected_len) != 0))
  412. goto err;
  413. OSSL_SELF_TEST_oncorrupt_byte(st, sig);
  414. if (EVP_PKEY_verify(sctx, sig, siglen, dgst, sizeof(dgst)) <= 0)
  415. goto err;
  416. ret = 1;
  417. err:
  418. BN_CTX_free(bnctx);
  419. EVP_PKEY_free(pkey);
  420. EVP_PKEY_CTX_free(kctx);
  421. EVP_PKEY_CTX_free(sctx);
  422. OSSL_PARAM_BLD_free_params(params);
  423. OSSL_PARAM_BLD_free_params(params_sig);
  424. OSSL_PARAM_BLD_free(bld);
  425. OSSL_SELF_TEST_onend(st, ret);
  426. return ret;
  427. }
  428. /*
  429. * Test a data driven list of KAT's for digest algorithms.
  430. * All tests are run regardless of if they fail or not.
  431. * Return 0 if any test fails.
  432. */
  433. static int self_test_digests(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  434. {
  435. int i, ret = 1;
  436. for (i = 0; i < (int)OSSL_NELEM(st_kat_digest_tests); ++i) {
  437. if (!self_test_digest(&st_kat_digest_tests[i], st, libctx))
  438. ret = 0;
  439. }
  440. return ret;
  441. }
  442. static int self_test_ciphers(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  443. {
  444. int i, ret = 1;
  445. for (i = 0; i < (int)OSSL_NELEM(st_kat_cipher_tests); ++i) {
  446. if (!self_test_cipher(&st_kat_cipher_tests[i], st, libctx))
  447. ret = 0;
  448. }
  449. return ret;
  450. }
  451. static int self_test_kdfs(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  452. {
  453. int i, ret = 1;
  454. for (i = 0; i < (int)OSSL_NELEM(st_kat_kdf_tests); ++i) {
  455. if (!self_test_kdf(&st_kat_kdf_tests[i], st, libctx))
  456. ret = 0;
  457. }
  458. return ret;
  459. }
  460. static int self_test_drbgs(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  461. {
  462. int i, ret = 1;
  463. for (i = 0; i < (int)OSSL_NELEM(st_kat_drbg_tests); ++i) {
  464. if (!self_test_drbg(&st_kat_drbg_tests[i], st, libctx))
  465. ret = 0;
  466. }
  467. return ret;
  468. }
  469. static int self_test_kas(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  470. {
  471. int i, ret = 1;
  472. for (i = 0; i < (int)OSSL_NELEM(st_kat_kas_tests); ++i) {
  473. if (!self_test_ka(&st_kat_kas_tests[i], st, libctx))
  474. ret = 0;
  475. }
  476. return ret;
  477. }
  478. static int self_test_signatures(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  479. {
  480. int i, ret = 1;
  481. for (i = 0; i < (int)OSSL_NELEM(st_kat_sign_tests); ++i) {
  482. if (!self_test_sign(&st_kat_sign_tests[i], st, libctx))
  483. ret = 0;
  484. }
  485. return ret;
  486. }
  487. /*
  488. * Run the algorithm KAT's.
  489. * Return 1 is successful, otherwise return 0.
  490. * This runs all the tests regardless of if any fail.
  491. */
  492. int SELF_TEST_kats(OSSL_SELF_TEST *st, OPENSSL_CTX *libctx)
  493. {
  494. int ret = 1;
  495. if (!self_test_digests(st, libctx))
  496. ret = 0;
  497. if (!self_test_ciphers(st, libctx))
  498. ret = 0;
  499. if (!self_test_signatures(st, libctx))
  500. ret = 0;
  501. if (!self_test_kdfs(st, libctx))
  502. ret = 0;
  503. if (!self_test_drbgs(st, libctx))
  504. ret = 0;
  505. if (!self_test_kas(st, libctx))
  506. ret = 0;
  507. return ret;
  508. }