x942kdf.c 22 KB

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