kbkdf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2019-2022 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. #define ossl_min(a, b) ((a) < (b)) ? (a) : (b)
  44. typedef enum {
  45. COUNTER = 0,
  46. FEEDBACK
  47. } kbkdf_mode;
  48. /* Our context structure. */
  49. typedef struct {
  50. void *provctx;
  51. kbkdf_mode mode;
  52. EVP_MAC_CTX *ctx_init;
  53. /* Names are lowercased versions of those found in SP800-108. */
  54. int r;
  55. unsigned char *ki;
  56. size_t ki_len;
  57. unsigned char *label;
  58. size_t label_len;
  59. unsigned char *context;
  60. size_t context_len;
  61. unsigned char *iv;
  62. size_t iv_len;
  63. int use_l;
  64. int use_separator;
  65. } KBKDF;
  66. /* Definitions needed for typechecking. */
  67. static OSSL_FUNC_kdf_newctx_fn kbkdf_new;
  68. static OSSL_FUNC_kdf_newctx_fn kbkdf_dup;
  69. static OSSL_FUNC_kdf_freectx_fn kbkdf_free;
  70. static OSSL_FUNC_kdf_reset_fn kbkdf_reset;
  71. static OSSL_FUNC_kdf_derive_fn kbkdf_derive;
  72. static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
  73. static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
  74. static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;
  75. static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;
  76. /* Not all platforms have htobe32(). */
  77. static uint32_t be32(uint32_t host)
  78. {
  79. uint32_t big = 0;
  80. DECLARE_IS_ENDIAN;
  81. if (!IS_LITTLE_ENDIAN)
  82. return host;
  83. big |= (host & 0xff000000) >> 24;
  84. big |= (host & 0x00ff0000) >> 8;
  85. big |= (host & 0x0000ff00) << 8;
  86. big |= (host & 0x000000ff) << 24;
  87. return big;
  88. }
  89. static void init(KBKDF *ctx)
  90. {
  91. ctx->r = 32;
  92. ctx->use_l = 1;
  93. ctx->use_separator = 1;
  94. }
  95. static void *kbkdf_new(void *provctx)
  96. {
  97. KBKDF *ctx;
  98. if (!ossl_prov_is_running())
  99. return NULL;
  100. ctx = OPENSSL_zalloc(sizeof(*ctx));
  101. if (ctx == NULL)
  102. return NULL;
  103. ctx->provctx = provctx;
  104. init(ctx);
  105. return ctx;
  106. }
  107. static void kbkdf_free(void *vctx)
  108. {
  109. KBKDF *ctx = (KBKDF *)vctx;
  110. if (ctx != NULL) {
  111. kbkdf_reset(ctx);
  112. OPENSSL_free(ctx);
  113. }
  114. }
  115. static void kbkdf_reset(void *vctx)
  116. {
  117. KBKDF *ctx = (KBKDF *)vctx;
  118. void *provctx = ctx->provctx;
  119. EVP_MAC_CTX_free(ctx->ctx_init);
  120. OPENSSL_clear_free(ctx->context, ctx->context_len);
  121. OPENSSL_clear_free(ctx->label, ctx->label_len);
  122. OPENSSL_clear_free(ctx->ki, ctx->ki_len);
  123. OPENSSL_clear_free(ctx->iv, ctx->iv_len);
  124. memset(ctx, 0, sizeof(*ctx));
  125. ctx->provctx = provctx;
  126. init(ctx);
  127. }
  128. static void *kbkdf_dup(void *vctx)
  129. {
  130. const KBKDF *src = (const KBKDF *)vctx;
  131. KBKDF *dest;
  132. dest = kbkdf_new(src->provctx);
  133. if (dest != NULL) {
  134. dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);
  135. if (dest->ctx_init == NULL
  136. || !ossl_prov_memdup(src->ki, src->ki_len,
  137. &dest->ki, &dest->ki_len)
  138. || !ossl_prov_memdup(src->label, src->label_len,
  139. &dest->label, &dest->label_len)
  140. || !ossl_prov_memdup(src->context, src->context_len,
  141. &dest->context, &dest->context_len)
  142. || !ossl_prov_memdup(src->iv, src->iv_len,
  143. &dest->iv, &dest->iv_len))
  144. goto err;
  145. dest->mode = src->mode;
  146. dest->r = src->r;
  147. dest->use_l = src->use_l;
  148. dest->use_separator = src->use_separator;
  149. }
  150. return dest;
  151. err:
  152. kbkdf_free(dest);
  153. return NULL;
  154. }
  155. /* SP800-108 section 5.1 or section 5.2 depending on mode. */
  156. static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
  157. size_t iv_len, unsigned char *label, size_t label_len,
  158. unsigned char *context, size_t context_len,
  159. unsigned char *k_i, size_t h, uint32_t l, int has_separator,
  160. unsigned char *ko, size_t ko_len, int r)
  161. {
  162. int ret = 0;
  163. EVP_MAC_CTX *ctx = NULL;
  164. size_t written = 0, to_write, k_i_len = iv_len;
  165. const unsigned char zero = 0;
  166. uint32_t counter, i;
  167. /*
  168. * From SP800-108:
  169. * The fixed input data is a concatenation of a Label,
  170. * a separation indicator 0x00, the Context, and L.
  171. * One or more of these fixed input data fields may be omitted.
  172. *
  173. * has_separator == 0 means that the separator is omitted.
  174. * Passing a value of l == 0 means that L is omitted.
  175. * The Context and L are omitted automatically if a NULL buffer is passed.
  176. */
  177. int has_l = (l != 0);
  178. /* Setup K(0) for feedback mode. */
  179. if (iv_len > 0)
  180. memcpy(k_i, iv, iv_len);
  181. for (counter = 1; written < ko_len; counter++) {
  182. i = be32(counter);
  183. ctx = EVP_MAC_CTX_dup(ctx_init);
  184. if (ctx == NULL)
  185. goto done;
  186. /* Perform feedback, if appropriate. */
  187. if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
  188. goto done;
  189. if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
  190. || !EVP_MAC_update(ctx, label, label_len)
  191. || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
  192. || !EVP_MAC_update(ctx, context, context_len)
  193. || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
  194. || !EVP_MAC_final(ctx, k_i, NULL, h))
  195. goto done;
  196. to_write = ko_len - written;
  197. memcpy(ko + written, k_i, ossl_min(to_write, h));
  198. written += h;
  199. k_i_len = h;
  200. EVP_MAC_CTX_free(ctx);
  201. ctx = NULL;
  202. }
  203. ret = 1;
  204. done:
  205. EVP_MAC_CTX_free(ctx);
  206. return ret;
  207. }
  208. static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
  209. const OSSL_PARAM params[])
  210. {
  211. KBKDF *ctx = (KBKDF *)vctx;
  212. int ret = 0;
  213. unsigned char *k_i = NULL;
  214. uint32_t l = 0;
  215. size_t h = 0;
  216. uint64_t counter_max;
  217. if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))
  218. return 0;
  219. /* label, context, and iv are permitted to be empty. Check everything
  220. * else. */
  221. if (ctx->ctx_init == NULL) {
  222. if (ctx->ki_len == 0 || ctx->ki == NULL) {
  223. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  224. return 0;
  225. }
  226. /* Could either be missing MAC or missing message digest or missing
  227. * cipher - arbitrarily, I pick this one. */
  228. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
  229. return 0;
  230. }
  231. /* Fail if the output length is zero */
  232. if (keylen == 0) {
  233. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  234. return 0;
  235. }
  236. h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
  237. if (h == 0)
  238. goto done;
  239. if (ctx->iv_len != 0 && ctx->iv_len != h) {
  240. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
  241. goto done;
  242. }
  243. if (ctx->mode == COUNTER) {
  244. /* Fail if keylen is too large for r */
  245. counter_max = (uint64_t)1 << (uint64_t)ctx->r;
  246. if ((uint64_t)(keylen / h) >= counter_max) {
  247. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  248. goto done;
  249. }
  250. }
  251. if (ctx->use_l != 0)
  252. l = be32(keylen * 8);
  253. k_i = OPENSSL_zalloc(h);
  254. if (k_i == NULL)
  255. goto done;
  256. ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
  257. ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
  258. ctx->use_separator, key, keylen, ctx->r);
  259. done:
  260. if (ret != 1)
  261. OPENSSL_cleanse(key, keylen);
  262. OPENSSL_clear_free(k_i, h);
  263. return ret;
  264. }
  265. static int kbkdf_set_buffer(unsigned char **out, size_t *out_len,
  266. const OSSL_PARAM *p)
  267. {
  268. if (p->data == NULL || p->data_size == 0)
  269. return 1;
  270. OPENSSL_clear_free(*out, *out_len);
  271. *out = NULL;
  272. return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
  273. }
  274. static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  275. {
  276. KBKDF *ctx = (KBKDF *)vctx;
  277. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  278. const OSSL_PARAM *p;
  279. if (params == NULL)
  280. return 1;
  281. if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,
  282. NULL, NULL, libctx))
  283. return 0;
  284. else if (ctx->ctx_init != NULL
  285. && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
  286. OSSL_MAC_NAME_HMAC)
  287. && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
  288. OSSL_MAC_NAME_CMAC)) {
  289. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
  290. return 0;
  291. }
  292. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);
  293. if (p != NULL
  294. && OPENSSL_strncasecmp("counter", p->data, p->data_size) == 0) {
  295. ctx->mode = COUNTER;
  296. } else if (p != NULL
  297. && OPENSSL_strncasecmp("feedback", p->data, p->data_size) == 0) {
  298. ctx->mode = FEEDBACK;
  299. } else if (p != NULL) {
  300. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  301. return 0;
  302. }
  303. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
  304. if (p != NULL && !kbkdf_set_buffer(&ctx->ki, &ctx->ki_len, p))
  305. return 0;
  306. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT);
  307. if (p != NULL && !kbkdf_set_buffer(&ctx->label, &ctx->label_len, p))
  308. return 0;
  309. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO);
  310. if (p != NULL && !kbkdf_set_buffer(&ctx->context, &ctx->context_len, p))
  311. return 0;
  312. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED);
  313. if (p != NULL && !kbkdf_set_buffer(&ctx->iv, &ctx->iv_len, p))
  314. return 0;
  315. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_L);
  316. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_l))
  317. return 0;
  318. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_R);
  319. if (p != NULL) {
  320. int new_r = 0;
  321. if (!OSSL_PARAM_get_int(p, &new_r))
  322. return 0;
  323. if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
  324. return 0;
  325. ctx->r = new_r;
  326. }
  327. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR);
  328. if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_separator))
  329. return 0;
  330. /* Set up digest context, if we can. */
  331. if (ctx->ctx_init != NULL && ctx->ki_len != 0
  332. && !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
  333. return 0;
  334. return 1;
  335. }
  336. static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
  337. ossl_unused void *provctx)
  338. {
  339. static const OSSL_PARAM known_settable_ctx_params[] = {
  340. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
  341. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  342. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  343. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
  344. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  345. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
  346. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
  347. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
  348. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  349. OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L, NULL),
  350. OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, NULL),
  351. OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_R, NULL),
  352. OSSL_PARAM_END,
  353. };
  354. return known_settable_ctx_params;
  355. }
  356. static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  357. {
  358. OSSL_PARAM *p;
  359. p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
  360. if (p == NULL)
  361. return -2;
  362. /* KBKDF can produce results as large as you like. */
  363. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  364. }
  365. static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
  366. ossl_unused void *provctx)
  367. {
  368. static const OSSL_PARAM known_gettable_ctx_params[] =
  369. { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), OSSL_PARAM_END };
  370. return known_gettable_ctx_params;
  371. }
  372. const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
  373. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
  374. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup },
  375. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
  376. { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
  377. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
  378. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  379. (void(*)(void))kbkdf_settable_ctx_params },
  380. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
  381. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  382. (void(*)(void))kbkdf_gettable_ctx_params },
  383. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },
  384. { 0, NULL },
  385. };