hkdf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <openssl/hmac.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/core_names.h>
  16. #include "internal/cryptlib.h"
  17. #include "internal/numbers.h"
  18. #include "crypto/evp.h"
  19. #include "internal/provider_ctx.h"
  20. #include "internal/providercommonerr.h"
  21. #include "internal/provider_algs.h"
  22. #include "internal/provider_util.h"
  23. #include "e_os.h"
  24. #define HKDF_MAXBUF 1024
  25. static OSSL_OP_kdf_newctx_fn kdf_hkdf_new;
  26. static OSSL_OP_kdf_freectx_fn kdf_hkdf_free;
  27. static OSSL_OP_kdf_reset_fn kdf_hkdf_reset;
  28. static OSSL_OP_kdf_derive_fn kdf_hkdf_derive;
  29. static OSSL_OP_kdf_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
  30. static OSSL_OP_kdf_set_ctx_params_fn kdf_hkdf_set_ctx_params;
  31. static OSSL_OP_kdf_gettable_ctx_params_fn kdf_hkdf_gettable_ctx_params;
  32. static OSSL_OP_kdf_get_ctx_params_fn kdf_hkdf_get_ctx_params;
  33. static int HKDF(const EVP_MD *evp_md,
  34. const unsigned char *salt, size_t salt_len,
  35. const unsigned char *key, size_t key_len,
  36. const unsigned char *info, size_t info_len,
  37. unsigned char *okm, size_t okm_len);
  38. static int HKDF_Extract(const EVP_MD *evp_md,
  39. const unsigned char *salt, size_t salt_len,
  40. const unsigned char *ikm, size_t ikm_len,
  41. unsigned char *prk, size_t prk_len);
  42. static int HKDF_Expand(const EVP_MD *evp_md,
  43. const unsigned char *prk, size_t prk_len,
  44. const unsigned char *info, size_t info_len,
  45. unsigned char *okm, size_t okm_len);
  46. typedef struct {
  47. void *provctx;
  48. int mode;
  49. PROV_DIGEST digest;
  50. unsigned char *salt;
  51. size_t salt_len;
  52. unsigned char *key;
  53. size_t key_len;
  54. unsigned char info[HKDF_MAXBUF];
  55. size_t info_len;
  56. } KDF_HKDF;
  57. static void *kdf_hkdf_new(void *provctx)
  58. {
  59. KDF_HKDF *ctx;
  60. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  61. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  62. else
  63. ctx->provctx = provctx;
  64. return ctx;
  65. }
  66. static void kdf_hkdf_free(void *vctx)
  67. {
  68. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  69. kdf_hkdf_reset(ctx);
  70. OPENSSL_free(ctx);
  71. }
  72. static void kdf_hkdf_reset(void *vctx)
  73. {
  74. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  75. ossl_prov_digest_reset(&ctx->digest);
  76. OPENSSL_free(ctx->salt);
  77. OPENSSL_clear_free(ctx->key, ctx->key_len);
  78. OPENSSL_cleanse(ctx->info, ctx->info_len);
  79. memset(ctx, 0, sizeof(*ctx));
  80. }
  81. static size_t kdf_hkdf_size(KDF_HKDF *ctx)
  82. {
  83. int sz;
  84. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  85. if (ctx->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY)
  86. return SIZE_MAX;
  87. if (md == NULL) {
  88. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  89. return 0;
  90. }
  91. sz = EVP_MD_size(md);
  92. if (sz < 0)
  93. return 0;
  94. return sz;
  95. }
  96. static int kdf_hkdf_derive(void *vctx, unsigned char *key, size_t keylen)
  97. {
  98. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  99. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  100. if (md == NULL) {
  101. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  102. return 0;
  103. }
  104. if (ctx->key == NULL) {
  105. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  106. return 0;
  107. }
  108. switch (ctx->mode) {
  109. case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:
  110. return HKDF(md, ctx->salt, ctx->salt_len, ctx->key,
  111. ctx->key_len, ctx->info, ctx->info_len, key,
  112. keylen);
  113. case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:
  114. return HKDF_Extract(md, ctx->salt, ctx->salt_len, ctx->key,
  115. ctx->key_len, key, keylen);
  116. case EVP_KDF_HKDF_MODE_EXPAND_ONLY:
  117. return HKDF_Expand(md, ctx->key, ctx->key_len, ctx->info,
  118. ctx->info_len, key, keylen);
  119. default:
  120. return 0;
  121. }
  122. }
  123. static int kdf_hkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  124. {
  125. const OSSL_PARAM *p;
  126. KDF_HKDF *ctx = vctx;
  127. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
  128. int n;
  129. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  130. return 0;
  131. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE)) != NULL) {
  132. if (p->data_type == OSSL_PARAM_UTF8_STRING) {
  133. if (strcasecmp(p->data, "EXTRACT_AND_EXPAND") == 0) {
  134. ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;
  135. } else if (strcasecmp(p->data, "EXTRACT_ONLY") == 0) {
  136. ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
  137. } else if (strcasecmp(p->data, "EXPAND_ONLY") == 0) {
  138. ctx->mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
  139. } else {
  140. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  141. return 0;
  142. }
  143. } else if (OSSL_PARAM_get_int(p, &n)) {
  144. if (n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
  145. && n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY
  146. && n != EVP_KDF_HKDF_MODE_EXPAND_ONLY) {
  147. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  148. return 0;
  149. }
  150. ctx->mode = n;
  151. } else {
  152. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  153. return 0;
  154. }
  155. }
  156. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL) {
  157. OPENSSL_clear_free(ctx->key, ctx->key_len);
  158. ctx->key = NULL;
  159. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->key, 0,
  160. &ctx->key_len))
  161. return 0;
  162. }
  163. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
  164. if (p->data_size != 0 && p->data != NULL) {
  165. OPENSSL_free(ctx->salt);
  166. ctx->salt = NULL;
  167. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->salt, 0,
  168. &ctx->salt_len))
  169. return 0;
  170. }
  171. }
  172. /* The info fields concatenate, so process them all */
  173. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO)) != NULL) {
  174. ctx->info_len = 0;
  175. for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
  176. OSSL_KDF_PARAM_INFO)) {
  177. const void *q = ctx->info + ctx->info_len;
  178. size_t sz = 0;
  179. if (p->data_size != 0
  180. && p->data != NULL
  181. && !OSSL_PARAM_get_octet_string(p, (void **)&q,
  182. HKDF_MAXBUF - ctx->info_len,
  183. &sz))
  184. return 0;
  185. ctx->info_len += sz;
  186. }
  187. }
  188. return 1;
  189. }
  190. static const OSSL_PARAM *kdf_hkdf_settable_ctx_params(void)
  191. {
  192. static const OSSL_PARAM known_settable_ctx_params[] = {
  193. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
  194. OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, NULL),
  195. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  196. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  197. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
  198. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  199. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
  200. OSSL_PARAM_END
  201. };
  202. return known_settable_ctx_params;
  203. }
  204. static int kdf_hkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  205. {
  206. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  207. OSSL_PARAM *p;
  208. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  209. return OSSL_PARAM_set_size_t(p, kdf_hkdf_size(ctx));
  210. return -2;
  211. }
  212. static const OSSL_PARAM *kdf_hkdf_gettable_ctx_params(void)
  213. {
  214. static const OSSL_PARAM known_gettable_ctx_params[] = {
  215. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  216. OSSL_PARAM_END
  217. };
  218. return known_gettable_ctx_params;
  219. }
  220. const OSSL_DISPATCH kdf_hkdf_functions[] = {
  221. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new },
  222. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free },
  223. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset },
  224. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive },
  225. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  226. (void(*)(void))kdf_hkdf_settable_ctx_params },
  227. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_set_ctx_params },
  228. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  229. (void(*)(void))kdf_hkdf_gettable_ctx_params },
  230. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_hkdf_get_ctx_params },
  231. { 0, NULL }
  232. };
  233. /*
  234. * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
  235. * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and
  236. * "Cryptographic Extraction and Key Derivation: The HKDF Scheme"
  237. * Section 4.2 (https://eprint.iacr.org/2010/264.pdf).
  238. *
  239. * From the paper:
  240. * The scheme HKDF is specified as:
  241. * HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t)
  242. *
  243. * where:
  244. * SKM is source key material
  245. * XTS is extractor salt (which may be null or constant)
  246. * CTXinfo is context information (may be null)
  247. * L is the number of key bits to be produced by KDF
  248. * k is the output length in bits of the hash function used with HMAC
  249. * t = ceil(L/k)
  250. * the value K(t) is truncated to its first d = L mod k bits.
  251. *
  252. * From RFC 5869:
  253. * 2.2. Step 1: Extract
  254. * HKDF-Extract(salt, IKM) -> PRK
  255. * 2.3. Step 2: Expand
  256. * HKDF-Expand(PRK, info, L) -> OKM
  257. */
  258. static int HKDF(const EVP_MD *evp_md,
  259. const unsigned char *salt, size_t salt_len,
  260. const unsigned char *ikm, size_t ikm_len,
  261. const unsigned char *info, size_t info_len,
  262. unsigned char *okm, size_t okm_len)
  263. {
  264. unsigned char prk[EVP_MAX_MD_SIZE];
  265. int ret, sz;
  266. size_t prk_len;
  267. sz = EVP_MD_size(evp_md);
  268. if (sz < 0)
  269. return 0;
  270. prk_len = (size_t)sz;
  271. /* Step 1: HKDF-Extract(salt, IKM) -> PRK */
  272. if (!HKDF_Extract(evp_md, salt, salt_len, ikm, ikm_len, prk, prk_len))
  273. return 0;
  274. /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */
  275. ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
  276. OPENSSL_cleanse(prk, sizeof(prk));
  277. return ret;
  278. }
  279. /*
  280. * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
  281. * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2).
  282. *
  283. * 2.2. Step 1: Extract
  284. *
  285. * HKDF-Extract(salt, IKM) -> PRK
  286. *
  287. * Options:
  288. * Hash a hash function; HashLen denotes the length of the
  289. * hash function output in octets
  290. *
  291. * Inputs:
  292. * salt optional salt value (a non-secret random value);
  293. * if not provided, it is set to a string of HashLen zeros.
  294. * IKM input keying material
  295. *
  296. * Output:
  297. * PRK a pseudorandom key (of HashLen octets)
  298. *
  299. * The output PRK is calculated as follows:
  300. *
  301. * PRK = HMAC-Hash(salt, IKM)
  302. */
  303. static int HKDF_Extract(const EVP_MD *evp_md,
  304. const unsigned char *salt, size_t salt_len,
  305. const unsigned char *ikm, size_t ikm_len,
  306. unsigned char *prk, size_t prk_len)
  307. {
  308. int sz = EVP_MD_size(evp_md);
  309. if (sz < 0)
  310. return 0;
  311. if (prk_len != (size_t)sz) {
  312. ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
  313. return 0;
  314. }
  315. /* calc: PRK = HMAC-Hash(salt, IKM) */
  316. return HMAC(evp_md, salt, salt_len, ikm, ikm_len, prk, NULL) != NULL;
  317. }
  318. /*
  319. * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
  320. * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3).
  321. *
  322. * 2.3. Step 2: Expand
  323. *
  324. * HKDF-Expand(PRK, info, L) -> OKM
  325. *
  326. * Options:
  327. * Hash a hash function; HashLen denotes the length of the
  328. * hash function output in octets
  329. *
  330. * Inputs:
  331. * PRK a pseudorandom key of at least HashLen octets
  332. * (usually, the output from the extract step)
  333. * info optional context and application specific information
  334. * (can be a zero-length string)
  335. * L length of output keying material in octets
  336. * (<= 255*HashLen)
  337. *
  338. * Output:
  339. * OKM output keying material (of L octets)
  340. *
  341. * The output OKM is calculated as follows:
  342. *
  343. * N = ceil(L/HashLen)
  344. * T = T(1) | T(2) | T(3) | ... | T(N)
  345. * OKM = first L octets of T
  346. *
  347. * where:
  348. * T(0) = empty string (zero length)
  349. * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
  350. * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
  351. * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
  352. * ...
  353. *
  354. * (where the constant concatenated to the end of each T(n) is a
  355. * single octet.)
  356. */
  357. static int HKDF_Expand(const EVP_MD *evp_md,
  358. const unsigned char *prk, size_t prk_len,
  359. const unsigned char *info, size_t info_len,
  360. unsigned char *okm, size_t okm_len)
  361. {
  362. HMAC_CTX *hmac;
  363. int ret = 0, sz;
  364. unsigned int i;
  365. unsigned char prev[EVP_MAX_MD_SIZE];
  366. size_t done_len = 0, dig_len, n;
  367. sz = EVP_MD_size(evp_md);
  368. if (sz <= 0)
  369. return 0;
  370. dig_len = (size_t)sz;
  371. /* calc: N = ceil(L/HashLen) */
  372. n = okm_len / dig_len;
  373. if (okm_len % dig_len)
  374. n++;
  375. if (n > 255 || okm == NULL)
  376. return 0;
  377. if ((hmac = HMAC_CTX_new()) == NULL)
  378. return 0;
  379. if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
  380. goto err;
  381. for (i = 1; i <= n; i++) {
  382. size_t copy_len;
  383. const unsigned char ctr = i;
  384. /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */
  385. if (i > 1) {
  386. if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
  387. goto err;
  388. if (!HMAC_Update(hmac, prev, dig_len))
  389. goto err;
  390. }
  391. if (!HMAC_Update(hmac, info, info_len))
  392. goto err;
  393. if (!HMAC_Update(hmac, &ctr, 1))
  394. goto err;
  395. if (!HMAC_Final(hmac, prev, NULL))
  396. goto err;
  397. copy_len = (done_len + dig_len > okm_len) ?
  398. okm_len - done_len :
  399. dig_len;
  400. memcpy(okm + done_len, prev, copy_len);
  401. done_len += copy_len;
  402. }
  403. ret = 1;
  404. err:
  405. OPENSSL_cleanse(prev, sizeof(prev));
  406. HMAC_CTX_free(hmac);
  407. return ret;
  408. }