hkdf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright 2016-2023 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. /*
  10. * HMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <openssl/hmac.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/kdf.h>
  20. #include <openssl/core_names.h>
  21. #include <openssl/proverr.h>
  22. #include "internal/cryptlib.h"
  23. #include "internal/numbers.h"
  24. #include "internal/packet.h"
  25. #include "crypto/evp.h"
  26. #include "prov/provider_ctx.h"
  27. #include "prov/providercommon.h"
  28. #include "prov/implementations.h"
  29. #include "prov/provider_util.h"
  30. #include "internal/e_os.h"
  31. #include "internal/params.h"
  32. #define HKDF_MAXBUF 2048
  33. #define HKDF_MAXINFO (32*1024)
  34. static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_new;
  35. static OSSL_FUNC_kdf_dupctx_fn kdf_hkdf_dup;
  36. static OSSL_FUNC_kdf_freectx_fn kdf_hkdf_free;
  37. static OSSL_FUNC_kdf_reset_fn kdf_hkdf_reset;
  38. static OSSL_FUNC_kdf_derive_fn kdf_hkdf_derive;
  39. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
  40. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_hkdf_set_ctx_params;
  41. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_hkdf_gettable_ctx_params;
  42. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_hkdf_get_ctx_params;
  43. static OSSL_FUNC_kdf_derive_fn kdf_tls1_3_derive;
  44. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_3_settable_ctx_params;
  45. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_3_set_ctx_params;
  46. static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,
  47. const unsigned char *salt, size_t salt_len,
  48. const unsigned char *key, size_t key_len,
  49. const unsigned char *info, size_t info_len,
  50. unsigned char *okm, size_t okm_len);
  51. static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,
  52. const unsigned char *salt, size_t salt_len,
  53. const unsigned char *ikm, size_t ikm_len,
  54. unsigned char *prk, size_t prk_len);
  55. static int HKDF_Expand(const EVP_MD *evp_md,
  56. const unsigned char *prk, size_t prk_len,
  57. const unsigned char *info, size_t info_len,
  58. unsigned char *okm, size_t okm_len);
  59. /* Settable context parameters that are common across HKDF and the TLS KDF */
  60. #define HKDF_COMMON_SETTABLES \
  61. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0), \
  62. OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, NULL), \
  63. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), \
  64. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), \
  65. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), \
  66. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0)
  67. typedef struct {
  68. void *provctx;
  69. int mode;
  70. PROV_DIGEST digest;
  71. unsigned char *salt;
  72. size_t salt_len;
  73. unsigned char *key;
  74. size_t key_len;
  75. unsigned char *prefix;
  76. size_t prefix_len;
  77. unsigned char *label;
  78. size_t label_len;
  79. unsigned char *data;
  80. size_t data_len;
  81. unsigned char *info;
  82. size_t info_len;
  83. } KDF_HKDF;
  84. static void *kdf_hkdf_new(void *provctx)
  85. {
  86. KDF_HKDF *ctx;
  87. if (!ossl_prov_is_running())
  88. return NULL;
  89. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL)
  90. ctx->provctx = provctx;
  91. return ctx;
  92. }
  93. static void kdf_hkdf_free(void *vctx)
  94. {
  95. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  96. if (ctx != NULL) {
  97. kdf_hkdf_reset(ctx);
  98. OPENSSL_free(ctx);
  99. }
  100. }
  101. static void kdf_hkdf_reset(void *vctx)
  102. {
  103. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  104. void *provctx = ctx->provctx;
  105. ossl_prov_digest_reset(&ctx->digest);
  106. #ifdef FIPS_MODULE
  107. OPENSSL_clear_free(ctx->salt, ctx->salt_len);
  108. #else
  109. OPENSSL_free(ctx->salt);
  110. #endif
  111. OPENSSL_free(ctx->prefix);
  112. OPENSSL_free(ctx->label);
  113. OPENSSL_clear_free(ctx->data, ctx->data_len);
  114. OPENSSL_clear_free(ctx->key, ctx->key_len);
  115. OPENSSL_clear_free(ctx->info, ctx->info_len);
  116. memset(ctx, 0, sizeof(*ctx));
  117. ctx->provctx = provctx;
  118. }
  119. static void *kdf_hkdf_dup(void *vctx)
  120. {
  121. const KDF_HKDF *src = (const KDF_HKDF *)vctx;
  122. KDF_HKDF *dest;
  123. dest = kdf_hkdf_new(src->provctx);
  124. if (dest != NULL) {
  125. if (!ossl_prov_memdup(src->salt, src->salt_len, &dest->salt,
  126. &dest->salt_len)
  127. || !ossl_prov_memdup(src->key, src->key_len,
  128. &dest->key , &dest->key_len)
  129. || !ossl_prov_memdup(src->prefix, src->prefix_len,
  130. &dest->prefix, &dest->prefix_len)
  131. || !ossl_prov_memdup(src->label, src->label_len,
  132. &dest->label, &dest->label_len)
  133. || !ossl_prov_memdup(src->data, src->data_len,
  134. &dest->data, &dest->data_len)
  135. || !ossl_prov_memdup(src->info, src->info_len,
  136. &dest->info, &dest->info_len)
  137. || !ossl_prov_digest_copy(&dest->digest, &src->digest))
  138. goto err;
  139. dest->mode = src->mode;
  140. }
  141. return dest;
  142. err:
  143. kdf_hkdf_free(dest);
  144. return NULL;
  145. }
  146. static size_t kdf_hkdf_size(KDF_HKDF *ctx)
  147. {
  148. int sz;
  149. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  150. if (ctx->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY)
  151. return SIZE_MAX;
  152. if (md == NULL) {
  153. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  154. return 0;
  155. }
  156. sz = EVP_MD_get_size(md);
  157. if (sz < 0)
  158. return 0;
  159. return sz;
  160. }
  161. static int kdf_hkdf_derive(void *vctx, unsigned char *key, size_t keylen,
  162. const OSSL_PARAM params[])
  163. {
  164. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  165. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  166. const EVP_MD *md;
  167. if (!ossl_prov_is_running() || !kdf_hkdf_set_ctx_params(ctx, params))
  168. return 0;
  169. md = ossl_prov_digest_md(&ctx->digest);
  170. if (md == NULL) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  172. return 0;
  173. }
  174. if (ctx->key == NULL) {
  175. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  176. return 0;
  177. }
  178. if (keylen == 0) {
  179. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  180. return 0;
  181. }
  182. switch (ctx->mode) {
  183. case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:
  184. default:
  185. return HKDF(libctx, md, ctx->salt, ctx->salt_len,
  186. ctx->key, ctx->key_len, ctx->info, ctx->info_len, key, keylen);
  187. case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:
  188. return HKDF_Extract(libctx, md, ctx->salt, ctx->salt_len,
  189. ctx->key, ctx->key_len, key, keylen);
  190. case EVP_KDF_HKDF_MODE_EXPAND_ONLY:
  191. return HKDF_Expand(md, ctx->key, ctx->key_len, ctx->info,
  192. ctx->info_len, key, keylen);
  193. }
  194. }
  195. static int hkdf_common_set_ctx_params(KDF_HKDF *ctx, const OSSL_PARAM params[])
  196. {
  197. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  198. const OSSL_PARAM *p;
  199. int n;
  200. if (params == NULL)
  201. return 1;
  202. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
  203. return 0;
  204. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE)) != NULL) {
  205. if (p->data_type == OSSL_PARAM_UTF8_STRING) {
  206. if (OPENSSL_strcasecmp(p->data, "EXTRACT_AND_EXPAND") == 0) {
  207. ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;
  208. } else if (OPENSSL_strcasecmp(p->data, "EXTRACT_ONLY") == 0) {
  209. ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
  210. } else if (OPENSSL_strcasecmp(p->data, "EXPAND_ONLY") == 0) {
  211. ctx->mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
  212. } else {
  213. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  214. return 0;
  215. }
  216. } else if (OSSL_PARAM_get_int(p, &n)) {
  217. if (n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
  218. && n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY
  219. && n != EVP_KDF_HKDF_MODE_EXPAND_ONLY) {
  220. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  221. return 0;
  222. }
  223. ctx->mode = n;
  224. } else {
  225. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  226. return 0;
  227. }
  228. }
  229. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL) {
  230. OPENSSL_clear_free(ctx->key, ctx->key_len);
  231. ctx->key = NULL;
  232. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->key, 0,
  233. &ctx->key_len))
  234. return 0;
  235. }
  236. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
  237. if (p->data_size != 0 && p->data != NULL) {
  238. OPENSSL_free(ctx->salt);
  239. ctx->salt = NULL;
  240. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->salt, 0,
  241. &ctx->salt_len))
  242. return 0;
  243. }
  244. }
  245. return 1;
  246. }
  247. static int kdf_hkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  248. {
  249. KDF_HKDF *ctx = vctx;
  250. if (params == NULL)
  251. return 1;
  252. if (!hkdf_common_set_ctx_params(ctx, params))
  253. return 0;
  254. if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO,
  255. &ctx->info, &ctx->info_len,
  256. HKDF_MAXINFO) == 0)
  257. return 0;
  258. return 1;
  259. }
  260. static const OSSL_PARAM *kdf_hkdf_settable_ctx_params(ossl_unused void *ctx,
  261. ossl_unused void *provctx)
  262. {
  263. static const OSSL_PARAM known_settable_ctx_params[] = {
  264. HKDF_COMMON_SETTABLES,
  265. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
  266. OSSL_PARAM_END
  267. };
  268. return known_settable_ctx_params;
  269. }
  270. static int kdf_hkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  271. {
  272. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  273. OSSL_PARAM *p;
  274. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) {
  275. size_t sz = kdf_hkdf_size(ctx);
  276. if (sz == 0)
  277. return 0;
  278. return OSSL_PARAM_set_size_t(p, sz);
  279. }
  280. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_INFO)) != NULL) {
  281. if (ctx->info == NULL || ctx->info_len == 0) {
  282. p->return_size = 0;
  283. return 1;
  284. }
  285. return OSSL_PARAM_set_octet_string(p, ctx->info, ctx->info_len);
  286. }
  287. return -2;
  288. }
  289. static const OSSL_PARAM *kdf_hkdf_gettable_ctx_params(ossl_unused void *ctx,
  290. ossl_unused void *provctx)
  291. {
  292. static const OSSL_PARAM known_gettable_ctx_params[] = {
  293. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  294. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
  295. OSSL_PARAM_END
  296. };
  297. return known_gettable_ctx_params;
  298. }
  299. const OSSL_DISPATCH ossl_kdf_hkdf_functions[] = {
  300. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new },
  301. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup },
  302. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free },
  303. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset },
  304. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive },
  305. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  306. (void(*)(void))kdf_hkdf_settable_ctx_params },
  307. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_set_ctx_params },
  308. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  309. (void(*)(void))kdf_hkdf_gettable_ctx_params },
  310. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_hkdf_get_ctx_params },
  311. OSSL_DISPATCH_END
  312. };
  313. /*
  314. * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
  315. * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and
  316. * "Cryptographic Extraction and Key Derivation: The HKDF Scheme"
  317. * Section 4.2 (https://eprint.iacr.org/2010/264.pdf).
  318. *
  319. * From the paper:
  320. * The scheme HKDF is specified as:
  321. * HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t)
  322. *
  323. * where:
  324. * SKM is source key material
  325. * XTS is extractor salt (which may be null or constant)
  326. * CTXinfo is context information (may be null)
  327. * L is the number of key bits to be produced by KDF
  328. * k is the output length in bits of the hash function used with HMAC
  329. * t = ceil(L/k)
  330. * the value K(t) is truncated to its first d = L mod k bits.
  331. *
  332. * From RFC 5869:
  333. * 2.2. Step 1: Extract
  334. * HKDF-Extract(salt, IKM) -> PRK
  335. * 2.3. Step 2: Expand
  336. * HKDF-Expand(PRK, info, L) -> OKM
  337. */
  338. static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,
  339. const unsigned char *salt, size_t salt_len,
  340. const unsigned char *ikm, size_t ikm_len,
  341. const unsigned char *info, size_t info_len,
  342. unsigned char *okm, size_t okm_len)
  343. {
  344. unsigned char prk[EVP_MAX_MD_SIZE];
  345. int ret, sz;
  346. size_t prk_len;
  347. sz = EVP_MD_get_size(evp_md);
  348. if (sz < 0)
  349. return 0;
  350. prk_len = (size_t)sz;
  351. /* Step 1: HKDF-Extract(salt, IKM) -> PRK */
  352. if (!HKDF_Extract(libctx, evp_md,
  353. salt, salt_len, ikm, ikm_len, prk, prk_len))
  354. return 0;
  355. /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */
  356. ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
  357. OPENSSL_cleanse(prk, sizeof(prk));
  358. return ret;
  359. }
  360. /*
  361. * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
  362. * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2).
  363. *
  364. * 2.2. Step 1: Extract
  365. *
  366. * HKDF-Extract(salt, IKM) -> PRK
  367. *
  368. * Options:
  369. * Hash a hash function; HashLen denotes the length of the
  370. * hash function output in octets
  371. *
  372. * Inputs:
  373. * salt optional salt value (a non-secret random value);
  374. * if not provided, it is set to a string of HashLen zeros.
  375. * IKM input keying material
  376. *
  377. * Output:
  378. * PRK a pseudorandom key (of HashLen octets)
  379. *
  380. * The output PRK is calculated as follows:
  381. *
  382. * PRK = HMAC-Hash(salt, IKM)
  383. */
  384. static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,
  385. const unsigned char *salt, size_t salt_len,
  386. const unsigned char *ikm, size_t ikm_len,
  387. unsigned char *prk, size_t prk_len)
  388. {
  389. int sz = EVP_MD_get_size(evp_md);
  390. if (sz < 0)
  391. return 0;
  392. if (prk_len != (size_t)sz) {
  393. ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
  394. return 0;
  395. }
  396. /* calc: PRK = HMAC-Hash(salt, IKM) */
  397. return
  398. EVP_Q_mac(libctx, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL, salt,
  399. salt_len, ikm, ikm_len, prk, EVP_MD_get_size(evp_md), NULL)
  400. != NULL;
  401. }
  402. /*
  403. * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
  404. * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3).
  405. *
  406. * 2.3. Step 2: Expand
  407. *
  408. * HKDF-Expand(PRK, info, L) -> OKM
  409. *
  410. * Options:
  411. * Hash a hash function; HashLen denotes the length of the
  412. * hash function output in octets
  413. *
  414. * Inputs:
  415. * PRK a pseudorandom key of at least HashLen octets
  416. * (usually, the output from the extract step)
  417. * info optional context and application specific information
  418. * (can be a zero-length string)
  419. * L length of output keying material in octets
  420. * (<= 255*HashLen)
  421. *
  422. * Output:
  423. * OKM output keying material (of L octets)
  424. *
  425. * The output OKM is calculated as follows:
  426. *
  427. * N = ceil(L/HashLen)
  428. * T = T(1) | T(2) | T(3) | ... | T(N)
  429. * OKM = first L octets of T
  430. *
  431. * where:
  432. * T(0) = empty string (zero length)
  433. * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
  434. * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
  435. * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
  436. * ...
  437. *
  438. * (where the constant concatenated to the end of each T(n) is a
  439. * single octet.)
  440. */
  441. static int HKDF_Expand(const EVP_MD *evp_md,
  442. const unsigned char *prk, size_t prk_len,
  443. const unsigned char *info, size_t info_len,
  444. unsigned char *okm, size_t okm_len)
  445. {
  446. HMAC_CTX *hmac;
  447. int ret = 0, sz;
  448. unsigned int i;
  449. unsigned char prev[EVP_MAX_MD_SIZE];
  450. size_t done_len = 0, dig_len, n;
  451. sz = EVP_MD_get_size(evp_md);
  452. if (sz <= 0)
  453. return 0;
  454. dig_len = (size_t)sz;
  455. /* calc: N = ceil(L/HashLen) */
  456. n = okm_len / dig_len;
  457. if (okm_len % dig_len)
  458. n++;
  459. if (n > 255 || okm == NULL)
  460. return 0;
  461. if ((hmac = HMAC_CTX_new()) == NULL)
  462. return 0;
  463. if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
  464. goto err;
  465. for (i = 1; i <= n; i++) {
  466. size_t copy_len;
  467. const unsigned char ctr = i;
  468. /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */
  469. if (i > 1) {
  470. if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
  471. goto err;
  472. if (!HMAC_Update(hmac, prev, dig_len))
  473. goto err;
  474. }
  475. if (!HMAC_Update(hmac, info, info_len))
  476. goto err;
  477. if (!HMAC_Update(hmac, &ctr, 1))
  478. goto err;
  479. if (!HMAC_Final(hmac, prev, NULL))
  480. goto err;
  481. copy_len = (dig_len > okm_len - done_len) ?
  482. okm_len - done_len :
  483. dig_len;
  484. memcpy(okm + done_len, prev, copy_len);
  485. done_len += copy_len;
  486. }
  487. ret = 1;
  488. err:
  489. OPENSSL_cleanse(prev, sizeof(prev));
  490. HMAC_CTX_free(hmac);
  491. return ret;
  492. }
  493. /*
  494. * TLS uses slight variations of the above and for FIPS validation purposes,
  495. * they need to be present here.
  496. * Refer to RFC 8446 section 7 for specific details.
  497. */
  498. /*
  499. * Given a |secret|; a |label| of length |labellen|; and |data| of length
  500. * |datalen| (e.g. typically a hash of the handshake messages), derive a new
  501. * secret |outlen| bytes long and store it in the location pointed to be |out|.
  502. * The |data| value may be zero length. Returns 1 on success and 0 on failure.
  503. */
  504. static int prov_tls13_hkdf_expand(const EVP_MD *md,
  505. const unsigned char *key, size_t keylen,
  506. const unsigned char *prefix, size_t prefixlen,
  507. const unsigned char *label, size_t labellen,
  508. const unsigned char *data, size_t datalen,
  509. unsigned char *out, size_t outlen)
  510. {
  511. size_t hkdflabellen;
  512. unsigned char hkdflabel[HKDF_MAXBUF];
  513. WPACKET pkt;
  514. /*
  515. * 2 bytes for length of derived secret + 1 byte for length of combined
  516. * prefix and label + bytes for the label itself + 1 byte length of hash
  517. * + bytes for the hash itself. We've got the maximum the KDF can handle
  518. * which should always be sufficient.
  519. */
  520. if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
  521. || !WPACKET_put_bytes_u16(&pkt, outlen)
  522. || !WPACKET_start_sub_packet_u8(&pkt)
  523. || !WPACKET_memcpy(&pkt, prefix, prefixlen)
  524. || !WPACKET_memcpy(&pkt, label, labellen)
  525. || !WPACKET_close(&pkt)
  526. || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
  527. || !WPACKET_get_total_written(&pkt, &hkdflabellen)
  528. || !WPACKET_finish(&pkt)) {
  529. WPACKET_cleanup(&pkt);
  530. return 0;
  531. }
  532. return HKDF_Expand(md, key, keylen, hkdflabel, hkdflabellen,
  533. out, outlen);
  534. }
  535. static int prov_tls13_hkdf_generate_secret(OSSL_LIB_CTX *libctx,
  536. const EVP_MD *md,
  537. const unsigned char *prevsecret,
  538. size_t prevsecretlen,
  539. const unsigned char *insecret,
  540. size_t insecretlen,
  541. const unsigned char *prefix,
  542. size_t prefixlen,
  543. const unsigned char *label,
  544. size_t labellen,
  545. unsigned char *out, size_t outlen)
  546. {
  547. size_t mdlen;
  548. int ret;
  549. unsigned char preextractsec[EVP_MAX_MD_SIZE];
  550. /* Always filled with zeros */
  551. static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
  552. ret = EVP_MD_get_size(md);
  553. /* Ensure cast to size_t is safe */
  554. if (ret <= 0)
  555. return 0;
  556. mdlen = (size_t)ret;
  557. if (insecret == NULL) {
  558. insecret = default_zeros;
  559. insecretlen = mdlen;
  560. }
  561. if (prevsecret == NULL) {
  562. prevsecret = default_zeros;
  563. prevsecretlen = mdlen;
  564. } else {
  565. EVP_MD_CTX *mctx = EVP_MD_CTX_new();
  566. unsigned char hash[EVP_MAX_MD_SIZE];
  567. /* The pre-extract derive step uses a hash of no messages */
  568. if (mctx == NULL
  569. || EVP_DigestInit_ex(mctx, md, NULL) <= 0
  570. || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
  571. EVP_MD_CTX_free(mctx);
  572. return 0;
  573. }
  574. EVP_MD_CTX_free(mctx);
  575. /* Generate the pre-extract secret */
  576. if (!prov_tls13_hkdf_expand(md, prevsecret, mdlen,
  577. prefix, prefixlen, label, labellen,
  578. hash, mdlen, preextractsec, mdlen))
  579. return 0;
  580. prevsecret = preextractsec;
  581. prevsecretlen = mdlen;
  582. }
  583. ret = HKDF_Extract(libctx, md, prevsecret, prevsecretlen,
  584. insecret, insecretlen, out, outlen);
  585. if (prevsecret == preextractsec)
  586. OPENSSL_cleanse(preextractsec, mdlen);
  587. return ret;
  588. }
  589. static int kdf_tls1_3_derive(void *vctx, unsigned char *key, size_t keylen,
  590. const OSSL_PARAM params[])
  591. {
  592. KDF_HKDF *ctx = (KDF_HKDF *)vctx;
  593. const EVP_MD *md;
  594. if (!ossl_prov_is_running() || !kdf_tls1_3_set_ctx_params(ctx, params))
  595. return 0;
  596. md = ossl_prov_digest_md(&ctx->digest);
  597. if (md == NULL) {
  598. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  599. return 0;
  600. }
  601. switch (ctx->mode) {
  602. default:
  603. return 0;
  604. case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:
  605. return prov_tls13_hkdf_generate_secret(PROV_LIBCTX_OF(ctx->provctx),
  606. md,
  607. ctx->salt, ctx->salt_len,
  608. ctx->key, ctx->key_len,
  609. ctx->prefix, ctx->prefix_len,
  610. ctx->label, ctx->label_len,
  611. key, keylen);
  612. case EVP_KDF_HKDF_MODE_EXPAND_ONLY:
  613. return prov_tls13_hkdf_expand(md, ctx->key, ctx->key_len,
  614. ctx->prefix, ctx->prefix_len,
  615. ctx->label, ctx->label_len,
  616. ctx->data, ctx->data_len,
  617. key, keylen);
  618. }
  619. }
  620. static int kdf_tls1_3_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  621. {
  622. const OSSL_PARAM *p;
  623. KDF_HKDF *ctx = vctx;
  624. if (params == NULL)
  625. return 1;
  626. if (!hkdf_common_set_ctx_params(ctx, params))
  627. return 0;
  628. if (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND) {
  629. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  630. return 0;
  631. }
  632. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PREFIX)) != NULL) {
  633. OPENSSL_free(ctx->prefix);
  634. ctx->prefix = NULL;
  635. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->prefix, 0,
  636. &ctx->prefix_len))
  637. return 0;
  638. }
  639. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_LABEL)) != NULL) {
  640. OPENSSL_free(ctx->label);
  641. ctx->label = NULL;
  642. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->label, 0,
  643. &ctx->label_len))
  644. return 0;
  645. }
  646. OPENSSL_clear_free(ctx->data, ctx->data_len);
  647. ctx->data = NULL;
  648. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DATA)) != NULL
  649. && !OSSL_PARAM_get_octet_string(p, (void **)&ctx->data, 0,
  650. &ctx->data_len))
  651. return 0;
  652. return 1;
  653. }
  654. static const OSSL_PARAM *kdf_tls1_3_settable_ctx_params(ossl_unused void *ctx,
  655. ossl_unused void *provctx)
  656. {
  657. static const OSSL_PARAM known_settable_ctx_params[] = {
  658. HKDF_COMMON_SETTABLES,
  659. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PREFIX, NULL, 0),
  660. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_LABEL, NULL, 0),
  661. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_DATA, NULL, 0),
  662. OSSL_PARAM_END
  663. };
  664. return known_settable_ctx_params;
  665. }
  666. const OSSL_DISPATCH ossl_kdf_tls1_3_kdf_functions[] = {
  667. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new },
  668. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup },
  669. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free },
  670. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset },
  671. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_3_derive },
  672. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  673. (void(*)(void))kdf_tls1_3_settable_ctx_params },
  674. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_tls1_3_set_ctx_params },
  675. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  676. (void(*)(void))kdf_hkdf_gettable_ctx_params },
  677. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_hkdf_get_ctx_params },
  678. OSSL_DISPATCH_END
  679. };