tls1_prf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright 2016-2018 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 "internal/cryptlib.h"
  51. #include <openssl/evp.h>
  52. #include <openssl/kdf.h>
  53. #include "internal/evp_int.h"
  54. #include "kdf_local.h"
  55. static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl);
  56. static int tls1_prf_alg(const EVP_MD *md,
  57. const unsigned char *sec, size_t slen,
  58. const unsigned char *seed, size_t seed_len,
  59. unsigned char *out, size_t olen);
  60. #define TLS1_PRF_MAXBUF 1024
  61. /* TLS KDF kdf context structure */
  62. struct evp_kdf_impl_st {
  63. /* Digest to use for PRF */
  64. const EVP_MD *md;
  65. /* Secret value to use for PRF */
  66. unsigned char *sec;
  67. size_t seclen;
  68. /* Buffer of concatenated seed data */
  69. unsigned char seed[TLS1_PRF_MAXBUF];
  70. size_t seedlen;
  71. };
  72. static EVP_KDF_IMPL *kdf_tls1_prf_new(void)
  73. {
  74. EVP_KDF_IMPL *impl;
  75. if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
  76. KDFerr(KDF_F_KDF_TLS1_PRF_NEW, ERR_R_MALLOC_FAILURE);
  77. return impl;
  78. }
  79. static void kdf_tls1_prf_free(EVP_KDF_IMPL *impl)
  80. {
  81. kdf_tls1_prf_reset(impl);
  82. OPENSSL_free(impl);
  83. }
  84. static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl)
  85. {
  86. OPENSSL_clear_free(impl->sec, impl->seclen);
  87. OPENSSL_cleanse(impl->seed, impl->seedlen);
  88. memset(impl, 0, sizeof(*impl));
  89. }
  90. static int kdf_tls1_prf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
  91. {
  92. const unsigned char *p;
  93. size_t len;
  94. const EVP_MD *md;
  95. switch (cmd) {
  96. case EVP_KDF_CTRL_SET_MD:
  97. md = va_arg(args, const EVP_MD *);
  98. if (md == NULL)
  99. return 0;
  100. impl->md = md;
  101. return 1;
  102. case EVP_KDF_CTRL_SET_TLS_SECRET:
  103. p = va_arg(args, const unsigned char *);
  104. len = va_arg(args, size_t);
  105. OPENSSL_clear_free(impl->sec, impl->seclen);
  106. impl->sec = OPENSSL_memdup(p, len);
  107. if (impl->sec == NULL)
  108. return 0;
  109. impl->seclen = len;
  110. return 1;
  111. case EVP_KDF_CTRL_RESET_TLS_SEED:
  112. OPENSSL_cleanse(impl->seed, impl->seedlen);
  113. impl->seedlen = 0;
  114. return 1;
  115. case EVP_KDF_CTRL_ADD_TLS_SEED:
  116. p = va_arg(args, const unsigned char *);
  117. len = va_arg(args, size_t);
  118. if (len == 0 || p == NULL)
  119. return 1;
  120. if (len > (TLS1_PRF_MAXBUF - impl->seedlen))
  121. return 0;
  122. memcpy(impl->seed + impl->seedlen, p, len);
  123. impl->seedlen += len;
  124. return 1;
  125. default:
  126. return -2;
  127. }
  128. }
  129. static int kdf_tls1_prf_ctrl_str(EVP_KDF_IMPL *impl,
  130. const char *type, const char *value)
  131. {
  132. if (value == NULL) {
  133. KDFerr(KDF_F_KDF_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
  134. return 0;
  135. }
  136. if (strcmp(type, "digest") == 0)
  137. return kdf_md2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_SET_MD, value);
  138. if (strcmp(type, "secret") == 0)
  139. return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl,
  140. EVP_KDF_CTRL_SET_TLS_SECRET, value);
  141. if (strcmp(type, "hexsecret") == 0)
  142. return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl,
  143. EVP_KDF_CTRL_SET_TLS_SECRET, value);
  144. if (strcmp(type, "seed") == 0)
  145. return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
  146. value);
  147. if (strcmp(type, "hexseed") == 0)
  148. return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
  149. value);
  150. return -2;
  151. }
  152. static int kdf_tls1_prf_derive(EVP_KDF_IMPL *impl, unsigned char *key,
  153. size_t keylen)
  154. {
  155. if (impl->md == NULL) {
  156. KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
  157. return 0;
  158. }
  159. if (impl->sec == NULL) {
  160. KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SECRET);
  161. return 0;
  162. }
  163. if (impl->seedlen == 0) {
  164. KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SEED);
  165. return 0;
  166. }
  167. return tls1_prf_alg(impl->md, impl->sec, impl->seclen,
  168. impl->seed, impl->seedlen,
  169. key, keylen);
  170. }
  171. const EVP_KDF tls1_prf_kdf_meth = {
  172. EVP_KDF_TLS1_PRF,
  173. kdf_tls1_prf_new,
  174. kdf_tls1_prf_free,
  175. kdf_tls1_prf_reset,
  176. kdf_tls1_prf_ctrl,
  177. kdf_tls1_prf_ctrl_str,
  178. NULL,
  179. kdf_tls1_prf_derive
  180. };
  181. /*
  182. * Refer to "The TLS Protocol Version 1.0" Section 5
  183. * (https://tools.ietf.org/html/rfc2246#section-5) and
  184. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  185. * (https://tools.ietf.org/html/rfc5246#section-5).
  186. *
  187. * P_<hash> is an expansion function that uses a single hash function to expand
  188. * a secret and seed into an arbitrary quantity of output:
  189. *
  190. * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
  191. * HMAC_<hash>(secret, A(2) + seed) +
  192. * HMAC_<hash>(secret, A(3) + seed) + ...
  193. *
  194. * where + indicates concatenation. P_<hash> can be iterated as many times as
  195. * is necessary to produce the required quantity of data.
  196. *
  197. * A(i) is defined as:
  198. * A(0) = seed
  199. * A(i) = HMAC_<hash>(secret, A(i-1))
  200. */
  201. static int tls1_prf_P_hash(const EVP_MD *md,
  202. const unsigned char *sec, size_t sec_len,
  203. const unsigned char *seed, size_t seed_len,
  204. unsigned char *out, size_t olen)
  205. {
  206. size_t chunk;
  207. EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL, *ctx_init = NULL;
  208. unsigned char Ai[EVP_MAX_MD_SIZE];
  209. size_t Ai_len;
  210. int ret = 0;
  211. ctx_init = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
  212. if (ctx_init == NULL)
  213. goto err;
  214. if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_FLAGS, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW) != 1)
  215. goto err;
  216. if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_MD, md) != 1)
  217. goto err;
  218. if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_KEY, sec, sec_len) != 1)
  219. goto err;
  220. if (!EVP_MAC_init(ctx_init))
  221. goto err;
  222. chunk = EVP_MAC_size(ctx_init);
  223. if (chunk == 0)
  224. goto err;
  225. /* A(0) = seed */
  226. ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
  227. if (ctx_Ai == NULL)
  228. goto err;
  229. if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
  230. goto err;
  231. for (;;) {
  232. /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
  233. if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len))
  234. goto err;
  235. EVP_MAC_CTX_free(ctx_Ai);
  236. ctx_Ai = NULL;
  237. /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
  238. ctx = EVP_MAC_CTX_dup(ctx_init);
  239. if (ctx == NULL)
  240. goto err;
  241. if (!EVP_MAC_update(ctx, Ai, Ai_len))
  242. goto err;
  243. /* save state for calculating next A(i) value */
  244. if (olen > chunk) {
  245. ctx_Ai = EVP_MAC_CTX_dup(ctx);
  246. if (ctx_Ai == NULL)
  247. goto err;
  248. }
  249. if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
  250. goto err;
  251. if (olen <= chunk) {
  252. /* last chunk - use Ai as temp bounce buffer */
  253. if (!EVP_MAC_final(ctx, Ai, &Ai_len))
  254. goto err;
  255. memcpy(out, Ai, olen);
  256. break;
  257. }
  258. if (!EVP_MAC_final(ctx, out, NULL))
  259. goto err;
  260. EVP_MAC_CTX_free(ctx);
  261. ctx = NULL;
  262. out += chunk;
  263. olen -= chunk;
  264. }
  265. ret = 1;
  266. err:
  267. EVP_MAC_CTX_free(ctx);
  268. EVP_MAC_CTX_free(ctx_Ai);
  269. EVP_MAC_CTX_free(ctx_init);
  270. OPENSSL_cleanse(Ai, sizeof(Ai));
  271. return ret;
  272. }
  273. /*
  274. * Refer to "The TLS Protocol Version 1.0" Section 5
  275. * (https://tools.ietf.org/html/rfc2246#section-5) and
  276. * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
  277. * (https://tools.ietf.org/html/rfc5246#section-5).
  278. *
  279. * For TLS v1.0 and TLS v1.1:
  280. *
  281. * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
  282. * P_SHA-1(S2, label + seed)
  283. *
  284. * S1 is taken from the first half of the secret, S2 from the second half.
  285. *
  286. * L_S = length in bytes of secret;
  287. * L_S1 = L_S2 = ceil(L_S / 2);
  288. *
  289. * For TLS v1.2:
  290. *
  291. * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
  292. */
  293. static int tls1_prf_alg(const EVP_MD *md,
  294. const unsigned char *sec, size_t slen,
  295. const unsigned char *seed, size_t seed_len,
  296. unsigned char *out, size_t olen)
  297. {
  298. if (EVP_MD_type(md) == NID_md5_sha1) {
  299. /* TLS v1.0 and TLS v1.1 */
  300. size_t i;
  301. unsigned char *tmp;
  302. /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
  303. size_t L_S1 = (slen + 1) / 2;
  304. size_t L_S2 = L_S1;
  305. if (!tls1_prf_P_hash(EVP_md5(), sec, L_S1,
  306. seed, seed_len, out, olen))
  307. return 0;
  308. if ((tmp = OPENSSL_malloc(olen)) == NULL) {
  309. KDFerr(KDF_F_TLS1_PRF_ALG, ERR_R_MALLOC_FAILURE);
  310. return 0;
  311. }
  312. if (!tls1_prf_P_hash(EVP_sha1(), sec + slen - L_S2, L_S2,
  313. seed, seed_len, tmp, olen)) {
  314. OPENSSL_clear_free(tmp, olen);
  315. return 0;
  316. }
  317. for (i = 0; i < olen; i++)
  318. out[i] ^= tmp[i];
  319. OPENSSL_clear_free(tmp, olen);
  320. return 1;
  321. }
  322. /* TLS v1.2 */
  323. if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
  324. return 0;
  325. return 1;
  326. }