self_test_kats.c 25 KB

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