x942kdf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "internal/e_os.h"
  11. #include <openssl/core_names.h>
  12. #include <openssl/core_dispatch.h>
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/params.h>
  16. #include <openssl/proverr.h>
  17. #include "internal/packet.h"
  18. #include "internal/der.h"
  19. #include "internal/nelem.h"
  20. #include "prov/provider_ctx.h"
  21. #include "prov/providercommon.h"
  22. #include "prov/implementations.h"
  23. #include "prov/provider_util.h"
  24. #include "prov/securitycheck.h"
  25. #include "prov/der_wrap.h"
  26. #define X942KDF_MAX_INLEN (1 << 30)
  27. static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
  28. static OSSL_FUNC_kdf_dupctx_fn x942kdf_dup;
  29. static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
  30. static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
  31. static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
  32. static OSSL_FUNC_kdf_settable_ctx_params_fn x942kdf_settable_ctx_params;
  33. static OSSL_FUNC_kdf_set_ctx_params_fn x942kdf_set_ctx_params;
  34. static OSSL_FUNC_kdf_gettable_ctx_params_fn x942kdf_gettable_ctx_params;
  35. static OSSL_FUNC_kdf_get_ctx_params_fn x942kdf_get_ctx_params;
  36. typedef struct {
  37. void *provctx;
  38. PROV_DIGEST digest;
  39. unsigned char *secret;
  40. size_t secret_len;
  41. unsigned char *acvpinfo;
  42. size_t acvpinfo_len;
  43. unsigned char *partyuinfo, *partyvinfo, *supp_pubinfo, *supp_privinfo;
  44. size_t partyuinfo_len, partyvinfo_len, supp_pubinfo_len, supp_privinfo_len;
  45. size_t dkm_len;
  46. const unsigned char *cek_oid;
  47. size_t cek_oid_len;
  48. int use_keybits;
  49. OSSL_FIPS_IND_DECLARE
  50. } KDF_X942;
  51. /*
  52. * A table of allowed wrapping algorithms, oids and the associated output
  53. * lengths.
  54. * NOTE: RC2wrap and camellia128_wrap have been removed as there are no
  55. * corresponding ciphers for these operations.
  56. */
  57. static const struct {
  58. const char *name;
  59. const unsigned char *oid;
  60. size_t oid_len;
  61. size_t keklen; /* size in bytes */
  62. } kek_algs[] = {
  63. { "AES-128-WRAP", ossl_der_oid_id_aes128_wrap, DER_OID_SZ_id_aes128_wrap,
  64. 16 },
  65. { "AES-192-WRAP", ossl_der_oid_id_aes192_wrap, DER_OID_SZ_id_aes192_wrap,
  66. 24 },
  67. { "AES-256-WRAP", ossl_der_oid_id_aes256_wrap, DER_OID_SZ_id_aes256_wrap,
  68. 32 },
  69. #ifndef FIPS_MODULE
  70. { "DES3-WRAP", ossl_der_oid_id_alg_CMS3DESwrap,
  71. DER_OID_SZ_id_alg_CMS3DESwrap, 24 },
  72. #endif
  73. };
  74. static int find_alg_id(OSSL_LIB_CTX *libctx, const char *algname,
  75. const char *propq, size_t *id)
  76. {
  77. int ret = 1;
  78. size_t i;
  79. EVP_CIPHER *cipher;
  80. cipher = EVP_CIPHER_fetch(libctx, algname, propq);
  81. if (cipher != NULL) {
  82. for (i = 0; i < OSSL_NELEM(kek_algs); i++) {
  83. if (EVP_CIPHER_is_a(cipher, kek_algs[i].name)) {
  84. *id = i;
  85. goto end;
  86. }
  87. }
  88. }
  89. ret = 0;
  90. ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_CEK_ALG);
  91. end:
  92. EVP_CIPHER_free(cipher);
  93. return ret;
  94. }
  95. static int DER_w_keyinfo(WPACKET *pkt,
  96. const unsigned char *der_oid, size_t der_oidlen,
  97. unsigned char **pcounter)
  98. {
  99. return ossl_DER_w_begin_sequence(pkt, -1)
  100. /* Store the initial value of 1 into the counter */
  101. && ossl_DER_w_octet_string_uint32(pkt, -1, 1)
  102. /* Remember where we stored the counter in the buffer */
  103. && (pcounter == NULL
  104. || (*pcounter = WPACKET_get_curr(pkt)) != NULL)
  105. && ossl_DER_w_precompiled(pkt, -1, der_oid, der_oidlen)
  106. && ossl_DER_w_end_sequence(pkt, -1);
  107. }
  108. static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen,
  109. const unsigned char *der_oid, size_t der_oidlen,
  110. const unsigned char *acvp, size_t acvplen,
  111. const unsigned char *partyu, size_t partyulen,
  112. const unsigned char *partyv, size_t partyvlen,
  113. const unsigned char *supp_pub, size_t supp_publen,
  114. const unsigned char *supp_priv, size_t supp_privlen,
  115. uint32_t keylen_bits, unsigned char **pcounter)
  116. {
  117. return (buf != NULL ? WPACKET_init_der(pkt, buf, buflen) :
  118. WPACKET_init_null_der(pkt))
  119. && ossl_DER_w_begin_sequence(pkt, -1)
  120. && (supp_priv == NULL
  121. || ossl_DER_w_octet_string(pkt, 3, supp_priv, supp_privlen))
  122. && (supp_pub == NULL
  123. || ossl_DER_w_octet_string(pkt, 2, supp_pub, supp_publen))
  124. && (keylen_bits == 0
  125. || ossl_DER_w_octet_string_uint32(pkt, 2, keylen_bits))
  126. && (partyv == NULL || ossl_DER_w_octet_string(pkt, 1, partyv, partyvlen))
  127. && (partyu == NULL || ossl_DER_w_octet_string(pkt, 0, partyu, partyulen))
  128. && (acvp == NULL || ossl_DER_w_precompiled(pkt, -1, acvp, acvplen))
  129. && DER_w_keyinfo(pkt, der_oid, der_oidlen, pcounter)
  130. && ossl_DER_w_end_sequence(pkt, -1)
  131. && WPACKET_finish(pkt);
  132. }
  133. /*
  134. * Encode the other info structure.
  135. *
  136. * The ANS X9.42-2003 standard uses OtherInfo:
  137. *
  138. * OtherInfo ::= SEQUENCE {
  139. * keyInfo KeySpecificInfo,
  140. * partyUInfo [0] OCTET STRING OPTIONAL,
  141. * partyVInfo [1] OCTET STRING OPTIONAL,
  142. * suppPubInfo [2] OCTET STRING OPTIONAL,
  143. * suppPrivInfo [3] OCTET STRING OPTIONAL
  144. * }
  145. *
  146. * KeySpecificInfo ::= SEQUENCE {
  147. * algorithm OBJECT IDENTIFIER,
  148. * counter OCTET STRING SIZE (4..4)
  149. * }
  150. *
  151. * RFC2631 Section 2.1.2 Contains the following definition for OtherInfo
  152. *
  153. * OtherInfo ::= SEQUENCE {
  154. * keyInfo KeySpecificInfo,
  155. * partyAInfo [0] OCTET STRING OPTIONAL,
  156. * suppPubInfo [2] OCTET STRING
  157. * }
  158. * Where suppPubInfo is the key length (in bits) (stored into 4 bytes)
  159. *
  160. * |keylen| is the length (in bytes) of the generated KEK. It is stored into
  161. * suppPubInfo (in bits). It is ignored if the value is 0.
  162. * |cek_oid| The oid of the key wrapping algorithm.
  163. * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
  164. * |acvp| is the optional blob of DER data representing one or more of the
  165. * OtherInfo fields related to |partyu|, |partyv|, |supp_pub| and |supp_priv|.
  166. * This field should normally be NULL. If |acvp| is non NULL then |partyu|,
  167. * |partyv|, |supp_pub| and |supp_priv| should all be NULL.
  168. * |acvp_len| is the |acvp| length (in bytes).
  169. * |partyu| is the optional public info contributed by the initiator.
  170. * It can be NULL. (It is also used as the ukm by CMS).
  171. * |partyu_len| is the |partyu| length (in bytes).
  172. * |partyv| is the optional public info contributed by the responder.
  173. * It can be NULL.
  174. * |partyv_len| is the |partyv| length (in bytes).
  175. * |supp_pub| is the optional additional, mutually-known public information.
  176. * It can be NULL. |keylen| should be 0 if this is not NULL.
  177. * |supp_pub_len| is the |supp_pub| length (in bytes).
  178. * |supp_priv| is the optional additional, mutually-known private information.
  179. * It can be NULL.
  180. * |supp_priv_len| is the |supp_priv| length (in bytes).
  181. * |der| is the returned encoded data. It must be freed by the caller.
  182. * |der_len| is the returned size of the encoded data.
  183. * |out_ctr| returns a pointer to the counter data which is embedded inside the
  184. * encoded data. This allows the counter bytes to be updated without
  185. * re-encoding.
  186. *
  187. * Returns: 1 if successfully encoded, or 0 otherwise.
  188. * Assumptions: |der|, |der_len| & |out_ctr| are not NULL.
  189. */
  190. static int
  191. x942_encode_otherinfo(size_t keylen,
  192. const unsigned char *cek_oid, size_t cek_oid_len,
  193. const unsigned char *acvp, size_t acvp_len,
  194. const unsigned char *partyu, size_t partyu_len,
  195. const unsigned char *partyv, size_t partyv_len,
  196. const unsigned char *supp_pub, size_t supp_pub_len,
  197. const unsigned char *supp_priv, size_t supp_priv_len,
  198. unsigned char **der, size_t *der_len,
  199. unsigned char **out_ctr)
  200. {
  201. int ret = 0;
  202. unsigned char *pcounter = NULL, *der_buf = NULL;
  203. size_t der_buflen = 0;
  204. WPACKET pkt;
  205. uint32_t keylen_bits;
  206. /* keylenbits must fit into 4 bytes */
  207. if (keylen > 0xFFFFFF)
  208. return 0;
  209. keylen_bits = 8 * keylen;
  210. /* Calculate the size of the buffer */
  211. if (!der_encode_sharedinfo(&pkt, NULL, 0, cek_oid, cek_oid_len,
  212. acvp, acvp_len,
  213. partyu, partyu_len, partyv, partyv_len,
  214. supp_pub, supp_pub_len, supp_priv, supp_priv_len,
  215. keylen_bits, NULL)
  216. || !WPACKET_get_total_written(&pkt, &der_buflen))
  217. goto err;
  218. WPACKET_cleanup(&pkt);
  219. /* Alloc the buffer */
  220. der_buf = OPENSSL_zalloc(der_buflen);
  221. if (der_buf == NULL)
  222. goto err;
  223. /* Encode into the buffer */
  224. if (!der_encode_sharedinfo(&pkt, der_buf, der_buflen, cek_oid, cek_oid_len,
  225. acvp, acvp_len,
  226. partyu, partyu_len, partyv, partyv_len,
  227. supp_pub, supp_pub_len, supp_priv, supp_priv_len,
  228. keylen_bits, &pcounter))
  229. goto err;
  230. /*
  231. * Since we allocated the exact size required, the buffer should point to the
  232. * start of the allocated buffer at this point.
  233. */
  234. if (WPACKET_get_curr(&pkt) != der_buf)
  235. goto err;
  236. /*
  237. * The data for the DER encoded octet string of a 32 bit counter = 1
  238. * should be 04 04 00 00 00 01
  239. * So just check the header is correct and skip over it.
  240. * This counter will be incremented in the kdf update loop.
  241. */
  242. if (pcounter == NULL
  243. || pcounter[0] != 0x04
  244. || pcounter[1] != 0x04)
  245. goto err;
  246. *out_ctr = (pcounter + 2);
  247. *der = der_buf;
  248. *der_len = der_buflen;
  249. ret = 1;
  250. err:
  251. WPACKET_cleanup(&pkt);
  252. return ret;
  253. }
  254. static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
  255. const unsigned char *z, size_t z_len,
  256. const unsigned char *other, size_t other_len,
  257. unsigned char *ctr,
  258. unsigned char *derived_key, size_t derived_key_len)
  259. {
  260. int ret = 0, hlen;
  261. size_t counter, out_len, len = derived_key_len;
  262. unsigned char mac[EVP_MAX_MD_SIZE];
  263. unsigned char *out = derived_key;
  264. EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
  265. if (z_len > X942KDF_MAX_INLEN
  266. || other_len > X942KDF_MAX_INLEN
  267. || derived_key_len > X942KDF_MAX_INLEN
  268. || derived_key_len == 0) {
  269. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
  270. return 0;
  271. }
  272. hlen = EVP_MD_get_size(kdf_md);
  273. if (hlen <= 0)
  274. return 0;
  275. out_len = (size_t)hlen;
  276. ctx = EVP_MD_CTX_create();
  277. ctx_init = EVP_MD_CTX_create();
  278. if (ctx == NULL || ctx_init == NULL)
  279. goto end;
  280. if (!EVP_DigestInit(ctx_init, kdf_md))
  281. goto end;
  282. for (counter = 1;; counter++) {
  283. /* updating the ctr modifies 4 bytes in the 'other' buffer */
  284. ctr[0] = (unsigned char)((counter >> 24) & 0xff);
  285. ctr[1] = (unsigned char)((counter >> 16) & 0xff);
  286. ctr[2] = (unsigned char)((counter >> 8) & 0xff);
  287. ctr[3] = (unsigned char)(counter & 0xff);
  288. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)
  289. || !EVP_DigestUpdate(ctx, z, z_len)
  290. || !EVP_DigestUpdate(ctx, other, other_len))
  291. goto end;
  292. if (len >= out_len) {
  293. if (!EVP_DigestFinal_ex(ctx, out, NULL))
  294. goto end;
  295. out += out_len;
  296. len -= out_len;
  297. if (len == 0)
  298. break;
  299. } else {
  300. if (!EVP_DigestFinal_ex(ctx, mac, NULL))
  301. goto end;
  302. memcpy(out, mac, len);
  303. break;
  304. }
  305. }
  306. ret = 1;
  307. end:
  308. EVP_MD_CTX_free(ctx);
  309. EVP_MD_CTX_free(ctx_init);
  310. OPENSSL_cleanse(mac, sizeof(mac));
  311. return ret;
  312. }
  313. static void *x942kdf_new(void *provctx)
  314. {
  315. KDF_X942 *ctx;
  316. if (!ossl_prov_is_running())
  317. return NULL;
  318. ctx = OPENSSL_zalloc(sizeof(*ctx));
  319. if (ctx == NULL)
  320. return NULL;
  321. ctx->provctx = provctx;
  322. OSSL_FIPS_IND_INIT(ctx)
  323. ctx->use_keybits = 1;
  324. return ctx;
  325. }
  326. static void x942kdf_reset(void *vctx)
  327. {
  328. KDF_X942 *ctx = (KDF_X942 *)vctx;
  329. void *provctx = ctx->provctx;
  330. ossl_prov_digest_reset(&ctx->digest);
  331. OPENSSL_clear_free(ctx->secret, ctx->secret_len);
  332. OPENSSL_clear_free(ctx->acvpinfo, ctx->acvpinfo_len);
  333. OPENSSL_clear_free(ctx->partyuinfo, ctx->partyuinfo_len);
  334. OPENSSL_clear_free(ctx->partyvinfo, ctx->partyvinfo_len);
  335. OPENSSL_clear_free(ctx->supp_pubinfo, ctx->supp_pubinfo_len);
  336. OPENSSL_clear_free(ctx->supp_privinfo, ctx->supp_privinfo_len);
  337. memset(ctx, 0, sizeof(*ctx));
  338. ctx->provctx = provctx;
  339. ctx->use_keybits = 1;
  340. }
  341. static void x942kdf_free(void *vctx)
  342. {
  343. KDF_X942 *ctx = (KDF_X942 *)vctx;
  344. if (ctx != NULL) {
  345. x942kdf_reset(ctx);
  346. OPENSSL_free(ctx);
  347. }
  348. }
  349. static void *x942kdf_dup(void *vctx)
  350. {
  351. const KDF_X942 *src = (const KDF_X942 *)vctx;
  352. KDF_X942 *dest;
  353. dest = x942kdf_new(src->provctx);
  354. if (dest != NULL) {
  355. if (!ossl_prov_memdup(src->secret, src->secret_len,
  356. &dest->secret , &dest->secret_len)
  357. || !ossl_prov_memdup(src->acvpinfo, src->acvpinfo_len,
  358. &dest->acvpinfo , &dest->acvpinfo_len)
  359. || !ossl_prov_memdup(src->partyuinfo, src->partyuinfo_len,
  360. &dest->partyuinfo , &dest->partyuinfo_len)
  361. || !ossl_prov_memdup(src->partyvinfo, src->partyvinfo_len,
  362. &dest->partyvinfo , &dest->partyvinfo_len)
  363. || !ossl_prov_memdup(src->supp_pubinfo, src->supp_pubinfo_len,
  364. &dest->supp_pubinfo,
  365. &dest->supp_pubinfo_len)
  366. || !ossl_prov_memdup(src->supp_privinfo, src->supp_privinfo_len,
  367. &dest->supp_privinfo,
  368. &dest->supp_privinfo_len)
  369. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  370. goto err;
  371. dest->cek_oid = src->cek_oid;
  372. dest->cek_oid_len = src->cek_oid_len;
  373. dest->dkm_len = src->dkm_len;
  374. dest->use_keybits = src->use_keybits;
  375. OSSL_FIPS_IND_COPY(dest, src)
  376. }
  377. return dest;
  378. err:
  379. x942kdf_free(dest);
  380. return NULL;
  381. }
  382. static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
  383. const OSSL_PARAM *p)
  384. {
  385. if (p->data_size == 0 || p->data == NULL)
  386. return 1;
  387. OPENSSL_free(*out);
  388. *out = NULL;
  389. return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
  390. }
  391. static size_t x942kdf_size(KDF_X942 *ctx)
  392. {
  393. int len;
  394. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  395. if (md == NULL) {
  396. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  397. return 0;
  398. }
  399. len = EVP_MD_get_size(md);
  400. return (len <= 0) ? 0 : (size_t)len;
  401. }
  402. #ifdef FIPS_MODULE
  403. static int fips_x942kdf_key_check_passed(KDF_X942 *ctx)
  404. {
  405. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  406. int key_approved = ossl_kdf_check_key_size(ctx->secret_len);
  407. if (!key_approved) {
  408. if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
  409. libctx, "X942KDF", "Key size",
  410. ossl_fips_config_x942kdf_key_check)) {
  411. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  412. return 0;
  413. }
  414. }
  415. return 1;
  416. }
  417. #endif
  418. static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen,
  419. const OSSL_PARAM params[])
  420. {
  421. KDF_X942 *ctx = (KDF_X942 *)vctx;
  422. const EVP_MD *md;
  423. int ret = 0;
  424. unsigned char *ctr;
  425. unsigned char *der = NULL;
  426. size_t der_len = 0;
  427. if (!ossl_prov_is_running() || !x942kdf_set_ctx_params(ctx, params))
  428. return 0;
  429. /*
  430. * These 2 options encode to the same field so only one of them should be
  431. * active at once.
  432. */
  433. if (ctx->use_keybits && ctx->supp_pubinfo != NULL) {
  434. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PUBINFO);
  435. return 0;
  436. }
  437. /*
  438. * If the blob of acvp data is used then the individual info fields that it
  439. * replaces should not also be defined.
  440. */
  441. if (ctx->acvpinfo != NULL
  442. && (ctx->partyuinfo != NULL
  443. || ctx->partyvinfo != NULL
  444. || ctx->supp_pubinfo != NULL
  445. || ctx->supp_privinfo != NULL)) {
  446. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
  447. return 0;
  448. }
  449. if (ctx->secret == NULL) {
  450. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
  451. return 0;
  452. }
  453. md = ossl_prov_digest_md(&ctx->digest);
  454. if (md == NULL) {
  455. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  456. return 0;
  457. }
  458. if (ctx->cek_oid == NULL || ctx->cek_oid_len == 0) {
  459. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CEK_ALG);
  460. return 0;
  461. }
  462. if (ctx->partyuinfo != NULL && ctx->partyuinfo_len >= X942KDF_MAX_INLEN) {
  463. /*
  464. * Note the ukm length MUST be 512 bits if it is used.
  465. * For backwards compatibility the old check is being done.
  466. */
  467. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_UKM_LENGTH);
  468. return 0;
  469. }
  470. /* generate the otherinfo der */
  471. if (!x942_encode_otherinfo(ctx->use_keybits ? ctx->dkm_len : 0,
  472. ctx->cek_oid, ctx->cek_oid_len,
  473. ctx->acvpinfo, ctx->acvpinfo_len,
  474. ctx->partyuinfo, ctx->partyuinfo_len,
  475. ctx->partyvinfo, ctx->partyvinfo_len,
  476. ctx->supp_pubinfo, ctx->supp_pubinfo_len,
  477. ctx->supp_privinfo, ctx->supp_privinfo_len,
  478. &der, &der_len, &ctr)) {
  479. ERR_raise(ERR_LIB_PROV, PROV_R_BAD_ENCODING);
  480. return 0;
  481. }
  482. ret = x942kdf_hash_kdm(md, ctx->secret, ctx->secret_len,
  483. der, der_len, ctr, key, keylen);
  484. OPENSSL_free(der);
  485. return ret;
  486. }
  487. static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  488. {
  489. const OSSL_PARAM *p, *pq;
  490. KDF_X942 *ctx = vctx;
  491. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
  492. const char *propq = NULL;
  493. const EVP_MD *md;
  494. size_t id;
  495. if (ossl_param_is_empty(params))
  496. return 1;
  497. if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
  498. OSSL_KDF_PARAM_FIPS_KEY_CHECK))
  499. return 0;
  500. if (OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST) != NULL) {
  501. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  502. return 0;
  503. md = ossl_prov_digest_md(&ctx->digest);
  504. if (EVP_MD_xof(md)) {
  505. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  506. return 0;
  507. }
  508. }
  509. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET);
  510. if (p == NULL)
  511. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
  512. if (p != NULL) {
  513. if (!x942kdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
  514. return 0;
  515. #ifdef FIPS_MODULE
  516. if (!fips_x942kdf_key_check_passed(ctx))
  517. return 0;
  518. #endif
  519. }
  520. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_ACVPINFO);
  521. if (p != NULL
  522. && !x942kdf_set_buffer(&ctx->acvpinfo, &ctx->acvpinfo_len, p))
  523. return 0;
  524. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_PARTYUINFO);
  525. if (p == NULL)
  526. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_UKM);
  527. if (p != NULL
  528. && !x942kdf_set_buffer(&ctx->partyuinfo, &ctx->partyuinfo_len, p))
  529. return 0;
  530. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_PARTYVINFO);
  531. if (p != NULL
  532. && !x942kdf_set_buffer(&ctx->partyvinfo, &ctx->partyvinfo_len, p))
  533. return 0;
  534. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_USE_KEYBITS);
  535. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_keybits))
  536. return 0;
  537. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_SUPP_PUBINFO);
  538. if (p != NULL) {
  539. if (!x942kdf_set_buffer(&ctx->supp_pubinfo, &ctx->supp_pubinfo_len, p))
  540. return 0;
  541. ctx->use_keybits = 0;
  542. }
  543. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_X942_SUPP_PRIVINFO);
  544. if (p != NULL
  545. && !x942kdf_set_buffer(&ctx->supp_privinfo, &ctx->supp_privinfo_len, p))
  546. return 0;
  547. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_CEK_ALG);
  548. if (p != NULL) {
  549. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  550. return 0;
  551. pq = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
  552. /*
  553. * We already grab the properties during ossl_prov_digest_load_from_params()
  554. * so there is no need to check the validity again..
  555. */
  556. if (pq != NULL)
  557. propq = p->data;
  558. if (find_alg_id(provctx, p->data, propq, &id) == 0)
  559. return 0;
  560. ctx->cek_oid = kek_algs[id].oid;
  561. ctx->cek_oid_len = kek_algs[id].oid_len;
  562. ctx->dkm_len = kek_algs[id].keklen;
  563. }
  564. return 1;
  565. }
  566. static const OSSL_PARAM *x942kdf_settable_ctx_params(ossl_unused void *ctx,
  567. ossl_unused void *provctx)
  568. {
  569. static const OSSL_PARAM known_settable_ctx_params[] = {
  570. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  571. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  572. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  573. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  574. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_UKM, NULL, 0),
  575. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_ACVPINFO, NULL, 0),
  576. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_PARTYUINFO, NULL, 0),
  577. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_PARTYVINFO, NULL, 0),
  578. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_SUPP_PUBINFO, NULL, 0),
  579. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_X942_SUPP_PRIVINFO, NULL, 0),
  580. OSSL_PARAM_int(OSSL_KDF_PARAM_X942_USE_KEYBITS, NULL),
  581. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CEK_ALG, NULL, 0),
  582. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)
  583. OSSL_PARAM_END
  584. };
  585. return known_settable_ctx_params;
  586. }
  587. static int x942kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  588. {
  589. KDF_X942 *ctx = (KDF_X942 *)vctx;
  590. OSSL_PARAM *p;
  591. p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
  592. if (p != NULL && !OSSL_PARAM_set_size_t(p, x942kdf_size(ctx)))
  593. return 0;
  594. if (!OSSL_FIPS_IND_GET_CTX_PARAM(ctx, params))
  595. return 0;
  596. return 1;
  597. }
  598. static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *ctx,
  599. ossl_unused void *provctx)
  600. {
  601. static const OSSL_PARAM known_gettable_ctx_params[] = {
  602. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  603. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  604. OSSL_PARAM_END
  605. };
  606. return known_gettable_ctx_params;
  607. }
  608. const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = {
  609. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
  610. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))x942kdf_dup },
  611. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
  612. { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
  613. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
  614. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  615. (void(*)(void))x942kdf_settable_ctx_params },
  616. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))x942kdf_set_ctx_params },
  617. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  618. (void(*)(void))x942kdf_gettable_ctx_params },
  619. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
  620. OSSL_DISPATCH_END
  621. };