tls1_prf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright 2016-2022 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. * Refer to "The TLS Protocol Version 1.0" Section 5
  11. * (https://tools.ietf.org/html/rfc2246#section-5) and
  12. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  13. * (https://tools.ietf.org/html/rfc5246#section-5).
  14. *
  15. * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
  16. *
  17. * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
  18. * P_SHA-1(S2, label + seed)
  19. *
  20. * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
  21. * two halves of the secret (with the possibility of one shared byte, in the
  22. * case where the length of the original secret is odd). S1 is taken from the
  23. * first half of the secret, S2 from the second half.
  24. *
  25. * For TLS v1.2 the TLS PRF algorithm is given by:
  26. *
  27. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  28. *
  29. * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
  30. * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
  31. * unless defined otherwise by the cipher suite.
  32. *
  33. * P_<hash> is an expansion function that uses a single hash function to expand
  34. * a secret and seed into an arbitrary quantity of output:
  35. *
  36. * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
  37. * HMAC_<hash>(secret, A(2) + seed) +
  38. * HMAC_<hash>(secret, A(3) + seed) + ...
  39. *
  40. * where + indicates concatenation. P_<hash> can be iterated as many times as
  41. * is necessary to produce the required quantity of data.
  42. *
  43. * A(i) is defined as:
  44. * A(0) = seed
  45. * A(i) = HMAC_<hash>(secret, A(i-1))
  46. */
  47. #include <stdio.h>
  48. #include <stdarg.h>
  49. #include <string.h>
  50. #include <openssl/evp.h>
  51. #include <openssl/kdf.h>
  52. #include <openssl/core_names.h>
  53. #include <openssl/params.h>
  54. #include <openssl/proverr.h>
  55. #include "internal/cryptlib.h"
  56. #include "internal/numbers.h"
  57. #include "crypto/evp.h"
  58. #include "prov/provider_ctx.h"
  59. #include "prov/providercommon.h"
  60. #include "prov/implementations.h"
  61. #include "prov/provider_util.h"
  62. #include "internal/e_os.h"
  63. static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
  64. static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup;
  65. static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
  66. static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
  67. static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
  68. static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
  69. static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
  70. static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
  71. static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
  72. static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
  73. const unsigned char *sec, size_t slen,
  74. const unsigned char *seed, size_t seed_len,
  75. unsigned char *out, size_t olen);
  76. #define TLS1_PRF_MAXBUF 1024
  77. /* TLS KDF kdf context structure */
  78. typedef struct {
  79. void *provctx;
  80. /* MAC context for the main digest */
  81. EVP_MAC_CTX *P_hash;
  82. /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
  83. EVP_MAC_CTX *P_sha1;
  84. /* Secret value to use for PRF */
  85. unsigned char *sec;
  86. size_t seclen;
  87. /* Buffer of concatenated seed data */
  88. unsigned char seed[TLS1_PRF_MAXBUF];
  89. size_t seedlen;
  90. } TLS1_PRF;
  91. static void *kdf_tls1_prf_new(void *provctx)
  92. {
  93. TLS1_PRF *ctx;
  94. if (!ossl_prov_is_running())
  95. return NULL;
  96. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  97. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  98. return NULL;
  99. }
  100. ctx->provctx = provctx;
  101. return ctx;
  102. }
  103. static void kdf_tls1_prf_free(void *vctx)
  104. {
  105. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  106. if (ctx != NULL) {
  107. kdf_tls1_prf_reset(ctx);
  108. OPENSSL_free(ctx);
  109. }
  110. }
  111. static void kdf_tls1_prf_reset(void *vctx)
  112. {
  113. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  114. void *provctx = ctx->provctx;
  115. EVP_MAC_CTX_free(ctx->P_hash);
  116. EVP_MAC_CTX_free(ctx->P_sha1);
  117. OPENSSL_clear_free(ctx->sec, ctx->seclen);
  118. OPENSSL_cleanse(ctx->seed, ctx->seedlen);
  119. memset(ctx, 0, sizeof(*ctx));
  120. ctx->provctx = provctx;
  121. }
  122. static void *kdf_tls1_prf_dup(void *vctx)
  123. {
  124. const TLS1_PRF *src = (const TLS1_PRF *)vctx;
  125. TLS1_PRF *dest;
  126. dest = kdf_tls1_prf_new(src->provctx);
  127. if (dest != NULL) {
  128. if (src->P_hash != NULL
  129. && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
  130. goto err;
  131. if (src->P_sha1 != NULL
  132. && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
  133. goto err;
  134. if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
  135. goto err;
  136. memcpy(dest->seed, src->seed, src->seedlen);
  137. dest->seedlen = src->seedlen;
  138. }
  139. return dest;
  140. err:
  141. kdf_tls1_prf_free(dest);
  142. return NULL;
  143. }
  144. static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
  145. const OSSL_PARAM params[])
  146. {
  147. TLS1_PRF *ctx = (TLS1_PRF *)vctx;
  148. if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
  149. return 0;
  150. if (ctx->P_hash == NULL) {
  151. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  152. return 0;
  153. }
  154. if (ctx->sec == NULL) {
  155. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
  156. return 0;
  157. }
  158. if (ctx->seedlen == 0) {
  159. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
  160. return 0;
  161. }
  162. if (keylen == 0) {
  163. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  164. return 0;
  165. }
  166. return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
  167. ctx->sec, ctx->seclen,
  168. ctx->seed, ctx->seedlen,
  169. key, keylen);
  170. }
  171. static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  172. {
  173. const OSSL_PARAM *p;
  174. TLS1_PRF *ctx = vctx;
  175. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  176. if (params == NULL)
  177. return 1;
  178. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
  179. if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
  180. if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
  181. OSSL_MAC_NAME_HMAC,
  182. NULL, SN_md5, libctx)
  183. || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
  184. OSSL_MAC_NAME_HMAC,
  185. NULL, SN_sha1, libctx))
  186. return 0;
  187. } else {
  188. EVP_MAC_CTX_free(ctx->P_sha1);
  189. if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
  190. OSSL_MAC_NAME_HMAC,
  191. NULL, NULL, libctx))
  192. return 0;
  193. }
  194. }
  195. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
  196. OPENSSL_clear_free(ctx->sec, ctx->seclen);
  197. ctx->sec = NULL;
  198. if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
  199. return 0;
  200. }
  201. /* The seed fields concatenate, so process them all */
  202. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
  203. for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
  204. OSSL_KDF_PARAM_SEED)) {
  205. const void *q = ctx->seed + ctx->seedlen;
  206. size_t sz = 0;
  207. if (p->data_size != 0
  208. && p->data != NULL
  209. && !OSSL_PARAM_get_octet_string(p, (void **)&q,
  210. TLS1_PRF_MAXBUF - ctx->seedlen,
  211. &sz))
  212. return 0;
  213. ctx->seedlen += sz;
  214. }
  215. }
  216. return 1;
  217. }
  218. static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
  219. ossl_unused void *ctx, ossl_unused void *provctx)
  220. {
  221. static const OSSL_PARAM known_settable_ctx_params[] = {
  222. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  223. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  224. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
  225. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
  226. OSSL_PARAM_END
  227. };
  228. return known_settable_ctx_params;
  229. }
  230. static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  231. {
  232. OSSL_PARAM *p;
  233. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  234. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  235. return -2;
  236. }
  237. static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
  238. ossl_unused void *ctx, ossl_unused void *provctx)
  239. {
  240. static const OSSL_PARAM known_gettable_ctx_params[] = {
  241. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  242. OSSL_PARAM_END
  243. };
  244. return known_gettable_ctx_params;
  245. }
  246. const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
  247. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
  248. { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup },
  249. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
  250. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
  251. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
  252. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  253. (void(*)(void))kdf_tls1_prf_settable_ctx_params },
  254. { OSSL_FUNC_KDF_SET_CTX_PARAMS,
  255. (void(*)(void))kdf_tls1_prf_set_ctx_params },
  256. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  257. (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
  258. { OSSL_FUNC_KDF_GET_CTX_PARAMS,
  259. (void(*)(void))kdf_tls1_prf_get_ctx_params },
  260. { 0, NULL }
  261. };
  262. /*
  263. * Refer to "The TLS Protocol Version 1.0" Section 5
  264. * (https://tools.ietf.org/html/rfc2246#section-5) and
  265. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  266. * (https://tools.ietf.org/html/rfc5246#section-5).
  267. *
  268. * P_<hash> is an expansion function that uses a single hash function to expand
  269. * a secret and seed into an arbitrary quantity of output:
  270. *
  271. * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
  272. * HMAC_<hash>(secret, A(2) + seed) +
  273. * HMAC_<hash>(secret, A(3) + seed) + ...
  274. *
  275. * where + indicates concatenation. P_<hash> can be iterated as many times as
  276. * is necessary to produce the required quantity of data.
  277. *
  278. * A(i) is defined as:
  279. * A(0) = seed
  280. * A(i) = HMAC_<hash>(secret, A(i-1))
  281. */
  282. static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
  283. const unsigned char *sec, size_t sec_len,
  284. const unsigned char *seed, size_t seed_len,
  285. unsigned char *out, size_t olen)
  286. {
  287. size_t chunk;
  288. EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
  289. unsigned char Ai[EVP_MAX_MD_SIZE];
  290. size_t Ai_len;
  291. int ret = 0;
  292. if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
  293. goto err;
  294. chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
  295. if (chunk == 0)
  296. goto err;
  297. /* A(0) = seed */
  298. ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
  299. if (ctx_Ai == NULL)
  300. goto err;
  301. if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
  302. goto err;
  303. for (;;) {
  304. /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
  305. if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
  306. goto err;
  307. EVP_MAC_CTX_free(ctx_Ai);
  308. ctx_Ai = NULL;
  309. /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
  310. ctx = EVP_MAC_CTX_dup(ctx_init);
  311. if (ctx == NULL)
  312. goto err;
  313. if (!EVP_MAC_update(ctx, Ai, Ai_len))
  314. goto err;
  315. /* save state for calculating next A(i) value */
  316. if (olen > chunk) {
  317. ctx_Ai = EVP_MAC_CTX_dup(ctx);
  318. if (ctx_Ai == NULL)
  319. goto err;
  320. }
  321. if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
  322. goto err;
  323. if (olen <= chunk) {
  324. /* last chunk - use Ai as temp bounce buffer */
  325. if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
  326. goto err;
  327. memcpy(out, Ai, olen);
  328. break;
  329. }
  330. if (!EVP_MAC_final(ctx, out, NULL, olen))
  331. goto err;
  332. EVP_MAC_CTX_free(ctx);
  333. ctx = NULL;
  334. out += chunk;
  335. olen -= chunk;
  336. }
  337. ret = 1;
  338. err:
  339. EVP_MAC_CTX_free(ctx);
  340. EVP_MAC_CTX_free(ctx_Ai);
  341. OPENSSL_cleanse(Ai, sizeof(Ai));
  342. return ret;
  343. }
  344. /*
  345. * Refer to "The TLS Protocol Version 1.0" Section 5
  346. * (https://tools.ietf.org/html/rfc2246#section-5) and
  347. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  348. * (https://tools.ietf.org/html/rfc5246#section-5).
  349. *
  350. * For TLS v1.0 and TLS v1.1:
  351. *
  352. * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
  353. * P_SHA-1(S2, label + seed)
  354. *
  355. * S1 is taken from the first half of the secret, S2 from the second half.
  356. *
  357. * L_S = length in bytes of secret;
  358. * L_S1 = L_S2 = ceil(L_S / 2);
  359. *
  360. * For TLS v1.2:
  361. *
  362. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  363. */
  364. static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
  365. const unsigned char *sec, size_t slen,
  366. const unsigned char *seed, size_t seed_len,
  367. unsigned char *out, size_t olen)
  368. {
  369. if (sha1ctx != NULL) {
  370. /* TLS v1.0 and TLS v1.1 */
  371. size_t i;
  372. unsigned char *tmp;
  373. /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
  374. size_t L_S1 = (slen + 1) / 2;
  375. size_t L_S2 = L_S1;
  376. if (!tls1_prf_P_hash(mdctx, sec, L_S1,
  377. seed, seed_len, out, olen))
  378. return 0;
  379. if ((tmp = OPENSSL_malloc(olen)) == NULL) {
  380. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  381. return 0;
  382. }
  383. if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
  384. seed, seed_len, tmp, olen)) {
  385. OPENSSL_clear_free(tmp, olen);
  386. return 0;
  387. }
  388. for (i = 0; i < olen; i++)
  389. out[i] ^= tmp[i];
  390. OPENSSL_clear_free(tmp, olen);
  391. return 1;
  392. }
  393. /* TLS v1.2 */
  394. if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
  395. return 0;
  396. return 1;
  397. }