kbkdf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2019 Red Hat, Inc.
  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. /*
  11. * This implements https://csrc.nist.gov/publications/detail/sp/800-108/final
  12. * section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC
  13. * and CMAC. That document does not name the KDFs it defines; the name is
  14. * derived from
  15. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
  16. *
  17. * Note that section 5.3 ("double-pipeline mode") is not implemented, though
  18. * it would be possible to do so in the future.
  19. *
  20. * These versions all assume the counter is used. It would be relatively
  21. * straightforward to expose a configuration handle should the need arise.
  22. *
  23. * Variable names attempt to match those of SP800-108.
  24. */
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <openssl/core_names.h>
  29. #include <openssl/evp.h>
  30. #include <openssl/hmac.h>
  31. #include <openssl/kdf.h>
  32. #include <openssl/params.h>
  33. #include <openssl/proverr.h>
  34. #include "internal/cryptlib.h"
  35. #include "crypto/evp.h"
  36. #include "internal/numbers.h"
  37. #include "internal/endian.h"
  38. #include "prov/implementations.h"
  39. #include "prov/provider_ctx.h"
  40. #include "prov/provider_util.h"
  41. #include "prov/providercommon.h"
  42. #include "internal/e_os.h"
  43. #include "internal/params.h"
  44. #define ossl_min(a, b) ((a) < (b)) ? (a) : (b)
  45. typedef enum {
  46. COUNTER = 0,
  47. FEEDBACK
  48. } kbkdf_mode;
  49. /* Our context structure. */
  50. typedef struct {
  51. void *provctx;
  52. kbkdf_mode mode;
  53. EVP_MAC_CTX *ctx_init;
  54. /* Names are lowercased versions of those found in SP800-108. */
  55. int r;
  56. unsigned char *ki;
  57. size_t ki_len;
  58. unsigned char *label;
  59. size_t label_len;
  60. unsigned char *context;
  61. size_t context_len;
  62. unsigned char *iv;
  63. size_t iv_len;
  64. int use_l;
  65. int is_kmac;
  66. int use_separator;
  67. } KBKDF;
  68. /* Definitions needed for typechecking. */
  69. static OSSL_FUNC_kdf_newctx_fn kbkdf_new;
  70. static OSSL_FUNC_kdf_dupctx_fn kbkdf_dup;
  71. static OSSL_FUNC_kdf_freectx_fn kbkdf_free;
  72. static OSSL_FUNC_kdf_reset_fn kbkdf_reset;
  73. static OSSL_FUNC_kdf_derive_fn kbkdf_derive;
  74. static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
  75. static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
  76. static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;
  77. static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;
  78. /* Not all platforms have htobe32(). */
  79. static uint32_t be32(uint32_t host)
  80. {
  81. uint32_t big = 0;
  82. DECLARE_IS_ENDIAN;
  83. if (!IS_LITTLE_ENDIAN)
  84. return host;
  85. big |= (host & 0xff000000) >> 24;
  86. big |= (host & 0x00ff0000) >> 8;
  87. big |= (host & 0x0000ff00) << 8;
  88. big |= (host & 0x000000ff) << 24;
  89. return big;
  90. }
  91. static void init(KBKDF *ctx)
  92. {
  93. ctx->r = 32;
  94. ctx->use_l = 1;
  95. ctx->use_separator = 1;
  96. ctx->is_kmac = 0;
  97. }
  98. static void *kbkdf_new(void *provctx)
  99. {
  100. KBKDF *ctx;
  101. if (!ossl_prov_is_running())
  102. return NULL;
  103. ctx = OPENSSL_zalloc(sizeof(*ctx));
  104. if (ctx == NULL)
  105. return NULL;
  106. ctx->provctx = provctx;
  107. init(ctx);
  108. return ctx;
  109. }
  110. static void kbkdf_free(void *vctx)
  111. {
  112. KBKDF *ctx = (KBKDF *)vctx;
  113. if (ctx != NULL) {
  114. kbkdf_reset(ctx);
  115. OPENSSL_free(ctx);
  116. }
  117. }
  118. static void kbkdf_reset(void *vctx)
  119. {
  120. KBKDF *ctx = (KBKDF *)vctx;
  121. void *provctx = ctx->provctx;
  122. EVP_MAC_CTX_free(ctx->ctx_init);
  123. OPENSSL_clear_free(ctx->context, ctx->context_len);
  124. OPENSSL_clear_free(ctx->label, ctx->label_len);
  125. OPENSSL_clear_free(ctx->ki, ctx->ki_len);
  126. OPENSSL_clear_free(ctx->iv, ctx->iv_len);
  127. memset(ctx, 0, sizeof(*ctx));
  128. ctx->provctx = provctx;
  129. init(ctx);
  130. }
  131. static void *kbkdf_dup(void *vctx)
  132. {
  133. const KBKDF *src = (const KBKDF *)vctx;
  134. KBKDF *dest;
  135. dest = kbkdf_new(src->provctx);
  136. if (dest != NULL) {
  137. dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);
  138. if (dest->ctx_init == NULL
  139. || !ossl_prov_memdup(src->ki, src->ki_len,
  140. &dest->ki, &dest->ki_len)
  141. || !ossl_prov_memdup(src->label, src->label_len,
  142. &dest->label, &dest->label_len)
  143. || !ossl_prov_memdup(src->context, src->context_len,
  144. &dest->context, &dest->context_len)
  145. || !ossl_prov_memdup(src->iv, src->iv_len,
  146. &dest->iv, &dest->iv_len))
  147. goto err;
  148. dest->mode = src->mode;
  149. dest->r = src->r;
  150. dest->use_l = src->use_l;
  151. dest->use_separator = src->use_separator;
  152. dest->is_kmac = src->is_kmac;
  153. }
  154. return dest;
  155. err:
  156. kbkdf_free(dest);
  157. return NULL;
  158. }
  159. /* SP800-108 section 5.1 or section 5.2 depending on mode. */
  160. static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
  161. size_t iv_len, unsigned char *label, size_t label_len,
  162. unsigned char *context, size_t context_len,
  163. unsigned char *k_i, size_t h, uint32_t l, int has_separator,
  164. unsigned char *ko, size_t ko_len, int r)
  165. {
  166. int ret = 0;
  167. EVP_MAC_CTX *ctx = NULL;
  168. size_t written = 0, to_write, k_i_len = iv_len;
  169. const unsigned char zero = 0;
  170. uint32_t counter, i;
  171. /*
  172. * From SP800-108:
  173. * The fixed input data is a concatenation of a Label,
  174. * a separation indicator 0x00, the Context, and L.
  175. * One or more of these fixed input data fields may be omitted.
  176. *
  177. * has_separator == 0 means that the separator is omitted.
  178. * Passing a value of l == 0 means that L is omitted.
  179. * The Context and L are omitted automatically if a NULL buffer is passed.
  180. */
  181. int has_l = (l != 0);
  182. /* Setup K(0) for feedback mode. */
  183. if (iv_len > 0)
  184. memcpy(k_i, iv, iv_len);
  185. for (counter = 1; written < ko_len; counter++) {
  186. i = be32(counter);
  187. ctx = EVP_MAC_CTX_dup(ctx_init);
  188. if (ctx == NULL)
  189. goto done;
  190. /* Perform feedback, if appropriate. */
  191. if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
  192. goto done;
  193. if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
  194. || !EVP_MAC_update(ctx, label, label_len)
  195. || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
  196. || !EVP_MAC_update(ctx, context, context_len)
  197. || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
  198. || !EVP_MAC_final(ctx, k_i, NULL, h))
  199. goto done;
  200. to_write = ko_len - written;
  201. memcpy(ko + written, k_i, ossl_min(to_write, h));
  202. written += h;
  203. k_i_len = h;
  204. EVP_MAC_CTX_free(ctx);
  205. ctx = NULL;
  206. }
  207. ret = 1;
  208. done:
  209. EVP_MAC_CTX_free(ctx);
  210. return ret;
  211. }
  212. /* This must be run before the key is set */
  213. static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, size_t customlen)
  214. {
  215. OSSL_PARAM params[2];
  216. if (custom == NULL || customlen == 0)
  217. return 1;
  218. params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
  219. (void *)custom, customlen);
  220. params[1] = OSSL_PARAM_construct_end();
  221. return EVP_MAC_CTX_set_params(ctx, params) > 0;
  222. }
  223. static int kmac_derive(EVP_MAC_CTX *ctx, unsigned char *out, size_t outlen,
  224. const unsigned char *context, size_t contextlen)
  225. {
  226. OSSL_PARAM params[2];
  227. params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &outlen);
  228. params[1] = OSSL_PARAM_construct_end();
  229. return EVP_MAC_CTX_set_params(ctx, params) > 0
  230. && EVP_MAC_update(ctx, context, contextlen)
  231. && EVP_MAC_final(ctx, out, NULL, outlen);
  232. }
  233. static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
  234. const OSSL_PARAM params[])
  235. {
  236. KBKDF *ctx = (KBKDF *)vctx;
  237. int ret = 0;
  238. unsigned char *k_i = NULL;
  239. uint32_t l = 0;
  240. size_t h = 0;
  241. uint64_t counter_max;
  242. if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))
  243. return 0;
  244. /* label, context, and iv are permitted to be empty. Check everything
  245. * else. */
  246. if (ctx->ctx_init == NULL) {
  247. if (ctx->ki_len == 0 || ctx->ki == NULL) {
  248. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  249. return 0;
  250. }
  251. /* Could either be missing MAC or missing message digest or missing
  252. * cipher - arbitrarily, I pick this one. */
  253. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
  254. return 0;
  255. }
  256. /* Fail if the output length is zero */
  257. if (keylen == 0) {
  258. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  259. return 0;
  260. }
  261. if (ctx->is_kmac) {
  262. ret = kmac_derive(ctx->ctx_init, key, keylen,
  263. ctx->context, ctx->context_len);
  264. goto done;
  265. }
  266. h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
  267. if (h == 0)
  268. goto done;
  269. if (ctx->iv_len != 0 && ctx->iv_len != h) {
  270. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
  271. goto done;
  272. }
  273. if (ctx->mode == COUNTER) {
  274. /* Fail if keylen is too large for r */
  275. counter_max = (uint64_t)1 << (uint64_t)ctx->r;
  276. if ((uint64_t)(keylen / h) >= counter_max) {
  277. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  278. goto done;
  279. }
  280. }
  281. if (ctx->use_l != 0)
  282. l = be32(keylen * 8);
  283. k_i = OPENSSL_zalloc(h);
  284. if (k_i == NULL)
  285. goto done;
  286. ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
  287. ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
  288. ctx->use_separator, key, keylen, ctx->r);
  289. done:
  290. if (ret != 1)
  291. OPENSSL_cleanse(key, keylen);
  292. OPENSSL_clear_free(k_i, h);
  293. return ret;
  294. }
  295. static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  296. {
  297. KBKDF *ctx = (KBKDF *)vctx;
  298. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  299. const OSSL_PARAM *p;
  300. if (params == NULL)
  301. return 1;
  302. if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,
  303. NULL, NULL, libctx))
  304. return 0;
  305. if (ctx->ctx_init != NULL) {
  306. ctx->is_kmac = 0;
  307. if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
  308. OSSL_MAC_NAME_KMAC128)
  309. || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
  310. OSSL_MAC_NAME_KMAC256)) {
  311. ctx->is_kmac = 1;
  312. } else if (!EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
  313. OSSL_MAC_NAME_HMAC)
  314. && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
  315. OSSL_MAC_NAME_CMAC)) {
  316. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
  317. return 0;
  318. }
  319. }
  320. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);
  321. if (p != NULL
  322. && OPENSSL_strncasecmp("counter", p->data, p->data_size) == 0) {
  323. ctx->mode = COUNTER;
  324. } else if (p != NULL
  325. && OPENSSL_strncasecmp("feedback", p->data, p->data_size) == 0) {
  326. ctx->mode = FEEDBACK;
  327. } else if (p != NULL) {
  328. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  329. return 0;
  330. }
  331. if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_KEY,
  332. &ctx->ki, &ctx->ki_len) == 0)
  333. return 0;
  334. if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SALT,
  335. &ctx->label, &ctx->label_len) == 0)
  336. return 0;
  337. if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO,
  338. &ctx->context, &ctx->context_len,
  339. 0) == 0)
  340. return 0;
  341. if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SEED,
  342. &ctx->iv, &ctx->iv_len) == 0)
  343. return 0;
  344. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_L);
  345. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_l))
  346. return 0;
  347. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_R);
  348. if (p != NULL) {
  349. int new_r = 0;
  350. if (!OSSL_PARAM_get_int(p, &new_r))
  351. return 0;
  352. if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
  353. return 0;
  354. ctx->r = new_r;
  355. }
  356. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR);
  357. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_separator))
  358. return 0;
  359. /* Set up digest context, if we can. */
  360. if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
  361. if ((ctx->is_kmac && !kmac_init(ctx->ctx_init, ctx->label, ctx->label_len))
  362. || !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
  363. return 0;
  364. }
  365. return 1;
  366. }
  367. static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
  368. ossl_unused void *provctx)
  369. {
  370. static const OSSL_PARAM known_settable_ctx_params[] = {
  371. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
  372. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  373. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  374. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
  375. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  376. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
  377. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
  378. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
  379. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  380. OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L, NULL),
  381. OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, NULL),
  382. OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_R, NULL),
  383. OSSL_PARAM_END,
  384. };
  385. return known_settable_ctx_params;
  386. }
  387. static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  388. {
  389. OSSL_PARAM *p;
  390. p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
  391. if (p == NULL)
  392. return -2;
  393. /* KBKDF can produce results as large as you like. */
  394. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  395. }
  396. static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
  397. ossl_unused void *provctx)
  398. {
  399. static const OSSL_PARAM known_gettable_ctx_params[] =
  400. { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), OSSL_PARAM_END };
  401. return known_gettable_ctx_params;
  402. }
  403. const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
  404. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
  405. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup },
  406. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
  407. { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
  408. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
  409. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  410. (void(*)(void))kbkdf_settable_ctx_params },
  411. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
  412. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  413. (void(*)(void))kbkdf_gettable_ctx_params },
  414. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },
  415. OSSL_DISPATCH_END,
  416. };