x942kdf.c 23 KB

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