2
0

kbkdf.c 14 KB

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