kbkdf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright 2019 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") in HMAC only. That document does not name the
  13. * KDFs it defines; the name is derived from
  14. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
  15. *
  16. * Note that sections 5.2 ("feedback mode") and 5.3 ("double-pipeline mode")
  17. * are not implemented, though it would be possible to do so in the future.
  18. * CMAC mode is also not implemented; some plumbing would be required.
  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 "internal/cryptlib.h"
  34. #include "crypto/evp.h"
  35. #include "internal/numbers.h"
  36. #include "internal/provider_algs.h"
  37. #include "internal/provider_ctx.h"
  38. #include "internal/provider_util.h"
  39. #include "internal/providercommonerr.h"
  40. #include "e_os.h"
  41. #define MIN(a, b) ((a) < (b)) ? (a) : (b)
  42. /* Our context structure. */
  43. typedef struct {
  44. void *provctx;
  45. EVP_MAC_CTX *ctx_init;
  46. /* Names are lowercased versions of those found in SP800-108. */
  47. unsigned char *ki;
  48. size_t ki_len;
  49. unsigned char *label;
  50. size_t label_len;
  51. unsigned char *context;
  52. size_t context_len;
  53. } KBKDF;
  54. /* Definitions needed for typechecking. */
  55. static OSSL_OP_kdf_newctx_fn kbkdf_new;
  56. static OSSL_OP_kdf_freectx_fn kbkdf_free;
  57. static OSSL_OP_kdf_reset_fn kbkdf_reset;
  58. static OSSL_OP_kdf_derive_fn kbkdf_derive;
  59. static OSSL_OP_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
  60. static OSSL_OP_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
  61. /* Not all platforms have htobe32(). */
  62. static uint32_t be32(uint32_t host)
  63. {
  64. uint32_t big = 0;
  65. const union {
  66. long one;
  67. char little;
  68. } is_endian = { 1 };
  69. if (!is_endian.little)
  70. return host;
  71. big |= (host & 0xff000000) >> 24;
  72. big |= (host & 0x00ff0000) >> 8;
  73. big |= (host & 0x0000ff00) << 8;
  74. big |= (host & 0x000000ff) << 24;
  75. return big;
  76. }
  77. static void *kbkdf_new(void *provctx)
  78. {
  79. KBKDF *ctx;
  80. ctx = OPENSSL_zalloc(sizeof(*ctx));
  81. if (ctx == NULL) {
  82. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  83. return NULL;
  84. }
  85. ctx->provctx = provctx;
  86. return ctx;
  87. }
  88. static void kbkdf_free(void *vctx)
  89. {
  90. KBKDF *ctx = (KBKDF *)vctx;
  91. kbkdf_reset(ctx);
  92. OPENSSL_free(ctx);
  93. }
  94. static void kbkdf_reset(void *vctx)
  95. {
  96. KBKDF *ctx = (KBKDF *)vctx;
  97. EVP_MAC_CTX_free(ctx->ctx_init);
  98. OPENSSL_clear_free(ctx->context, ctx->context_len);
  99. OPENSSL_clear_free(ctx->label, ctx->label_len);
  100. OPENSSL_clear_free(ctx->ki, ctx->ki_len);
  101. memset(ctx, 0, sizeof(*ctx));
  102. }
  103. /* SP800-108 section 5.1. */
  104. static int kbkdf_derive_counter(EVP_MAC_CTX *ctx_init,
  105. unsigned char *label, size_t label_len,
  106. unsigned char *context, size_t context_len,
  107. unsigned char *k_i, size_t h, uint32_t l,
  108. unsigned char *ko, size_t ko_len)
  109. {
  110. int ret = 0;
  111. EVP_MAC_CTX *ctx = NULL;
  112. size_t written = 0, to_write;
  113. const unsigned char zero = 0;
  114. uint32_t counter, i;
  115. for (counter = 1; written < ko_len; counter++) {
  116. i = be32(counter);
  117. ctx = EVP_MAC_CTX_dup(ctx_init);
  118. if (ctx == NULL)
  119. goto done;
  120. if (!EVP_MAC_update(ctx, (unsigned char *)&i, 4)
  121. || !EVP_MAC_update(ctx, label, label_len)
  122. || !EVP_MAC_update(ctx, &zero, 1)
  123. || !EVP_MAC_update(ctx, context, context_len)
  124. || !EVP_MAC_update(ctx, (unsigned char *)&l, 4)
  125. || !EVP_MAC_final(ctx, k_i, NULL, h))
  126. goto done;
  127. to_write = ko_len - written;
  128. memcpy(ko + written, k_i, MIN(to_write, h));
  129. written += h;
  130. EVP_MAC_CTX_free(ctx);
  131. ctx = NULL;
  132. }
  133. ret = 1;
  134. done:
  135. EVP_MAC_CTX_free(ctx);
  136. return ret;
  137. }
  138. static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen)
  139. {
  140. KBKDF *ctx = (KBKDF *)vctx;
  141. int ret = 0;
  142. unsigned char *k_i = NULL;
  143. uint32_t l = be32(keylen * 8);
  144. size_t h = 0;
  145. /* Label and Context are permitted to be empty. Check everything else. */
  146. if (ctx->ctx_init == NULL) {
  147. if (ctx->ki_len == 0 || ctx->ki == NULL) {
  148. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  149. return 0;
  150. }
  151. /* Could either be missing MAC or missing message digest -
  152. * arbitrarily, I pick this one. */
  153. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
  154. return 0;
  155. }
  156. h = EVP_MAC_size(ctx->ctx_init);
  157. if (h == 0)
  158. goto done;
  159. k_i = OPENSSL_zalloc(h);
  160. if (k_i == NULL)
  161. goto done;
  162. ret = kbkdf_derive_counter(
  163. ctx->ctx_init, ctx->label, ctx->label_len, ctx->context,
  164. ctx->context_len, k_i, h, l, key, keylen);
  165. done:
  166. if (ret != 1)
  167. OPENSSL_cleanse(key, keylen);
  168. OPENSSL_clear_free(k_i, h);
  169. return ret;
  170. }
  171. static int kbkdf_set_buffer(unsigned char **out, size_t *out_len,
  172. const OSSL_PARAM *p)
  173. {
  174. if (p->data == NULL || p->data_size == 0)
  175. return 1;
  176. OPENSSL_clear_free(*out, *out_len);
  177. *out = NULL;
  178. return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
  179. }
  180. static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  181. {
  182. KBKDF *ctx = (KBKDF *)vctx;
  183. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
  184. const OSSL_PARAM *p;
  185. OSSL_PARAM mparams[2];
  186. if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,
  187. NULL, NULL, libctx))
  188. return 0;
  189. else if (ctx->ctx_init != NULL
  190. && !EVP_MAC_is_a(EVP_MAC_CTX_mac(ctx->ctx_init),
  191. OSSL_MAC_NAME_HMAC)) {
  192. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
  193. return 0;
  194. }
  195. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
  196. if (p != NULL && !kbkdf_set_buffer(&ctx->ki, &ctx->ki_len, p))
  197. return 0;
  198. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT);
  199. if (p != NULL && !kbkdf_set_buffer(&ctx->label, &ctx->label_len, p))
  200. return 0;
  201. p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO);
  202. if (p != NULL && !kbkdf_set_buffer(&ctx->context, &ctx->context_len, p))
  203. return 0;
  204. /* Set up digest context, if we can. */
  205. if (ctx->ctx_init != NULL && ctx->ki_len != 0) {
  206. mparams[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  207. ctx->ki, ctx->ki_len);
  208. mparams[1] = OSSL_PARAM_construct_end();
  209. if (!EVP_MAC_CTX_set_params(ctx->ctx_init, mparams)
  210. || !EVP_MAC_init(ctx->ctx_init))
  211. return 0;
  212. }
  213. return 1;
  214. }
  215. static const OSSL_PARAM *kbkdf_settable_ctx_params(void)
  216. {
  217. static const OSSL_PARAM known_settable_ctx_params[] = {
  218. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
  219. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  220. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  221. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  222. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
  223. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  224. OSSL_PARAM_END,
  225. };
  226. return known_settable_ctx_params;
  227. }
  228. static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  229. {
  230. OSSL_PARAM *p;
  231. p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
  232. if (p == NULL)
  233. return -2;
  234. /* KBKDF can produce results as large as you like. */
  235. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  236. }
  237. static const OSSL_PARAM *kbkdf_gettable_ctx_params(void)
  238. {
  239. static const OSSL_PARAM known_gettable_ctx_params[] =
  240. { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), OSSL_PARAM_END };
  241. return known_gettable_ctx_params;
  242. }
  243. const OSSL_DISPATCH kdf_kbkdf_functions[] = {
  244. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
  245. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
  246. { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
  247. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
  248. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  249. (void(*)(void))kbkdf_settable_ctx_params },
  250. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
  251. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  252. (void(*)(void))kbkdf_gettable_ctx_params },
  253. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },
  254. { 0, NULL },
  255. };