tls1_prf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/kdf.h>
  12. #include <openssl/evp.h>
  13. #include "internal/evp_int.h"
  14. static int tls1_prf_alg(const EVP_MD *md,
  15. const unsigned char *sec, size_t slen,
  16. const unsigned char *seed, size_t seed_len,
  17. unsigned char *out, size_t olen);
  18. #define TLS1_PRF_MAXBUF 1024
  19. /* TLS KDF pkey context structure */
  20. typedef struct {
  21. /* Digest to use for PRF */
  22. const EVP_MD *md;
  23. /* Secret value to use for PRF */
  24. unsigned char *sec;
  25. size_t seclen;
  26. /* Buffer of concatenated seed data */
  27. unsigned char seed[TLS1_PRF_MAXBUF];
  28. size_t seedlen;
  29. } TLS1_PRF_PKEY_CTX;
  30. static int pkey_tls1_prf_init(EVP_PKEY_CTX *ctx)
  31. {
  32. TLS1_PRF_PKEY_CTX *kctx;
  33. kctx = OPENSSL_zalloc(sizeof(*kctx));
  34. if (kctx == NULL)
  35. return 0;
  36. ctx->data = kctx;
  37. return 1;
  38. }
  39. static void pkey_tls1_prf_cleanup(EVP_PKEY_CTX *ctx)
  40. {
  41. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  42. OPENSSL_clear_free(kctx->sec, kctx->seclen);
  43. OPENSSL_cleanse(kctx->seed, kctx->seedlen);
  44. OPENSSL_free(kctx);
  45. }
  46. static int pkey_tls1_prf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  47. {
  48. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  49. switch (type) {
  50. case EVP_PKEY_CTRL_TLS_MD:
  51. kctx->md = p2;
  52. return 1;
  53. case EVP_PKEY_CTRL_TLS_SECRET:
  54. if (p1 < 0)
  55. return 0;
  56. if (kctx->sec != NULL)
  57. OPENSSL_clear_free(kctx->sec, kctx->seclen);
  58. OPENSSL_cleanse(kctx->seed, kctx->seedlen);
  59. kctx->seedlen = 0;
  60. kctx->sec = OPENSSL_memdup(p2, p1);
  61. if (kctx->sec == NULL)
  62. return 0;
  63. kctx->seclen = p1;
  64. return 1;
  65. case EVP_PKEY_CTRL_TLS_SEED:
  66. if (p1 == 0 || p2 == NULL)
  67. return 1;
  68. if (p1 < 0 || p1 > (int)(TLS1_PRF_MAXBUF - kctx->seedlen))
  69. return 0;
  70. memcpy(kctx->seed + kctx->seedlen, p2, p1);
  71. kctx->seedlen += p1;
  72. return 1;
  73. default:
  74. return -2;
  75. }
  76. }
  77. static int pkey_tls1_prf_ctrl_str(EVP_PKEY_CTX *ctx,
  78. const char *type, const char *value)
  79. {
  80. if (value == NULL) {
  81. KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
  82. return 0;
  83. }
  84. if (strcmp(type, "md") == 0) {
  85. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  86. const EVP_MD *md = EVP_get_digestbyname(value);
  87. if (md == NULL) {
  88. KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_INVALID_DIGEST);
  89. return 0;
  90. }
  91. kctx->md = md;
  92. return 1;
  93. }
  94. if (strcmp(type, "secret") == 0)
  95. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
  96. if (strcmp(type, "hexsecret") == 0)
  97. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
  98. if (strcmp(type, "seed") == 0)
  99. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_TLS_SEED, value);
  100. if (strcmp(type, "hexseed") == 0)
  101. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_TLS_SEED, value);
  102. return -2;
  103. }
  104. static int pkey_tls1_prf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  105. size_t *keylen)
  106. {
  107. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  108. if (kctx->md == NULL || kctx->sec == NULL || kctx->seedlen == 0) {
  109. KDFerr(KDF_F_PKEY_TLS1_PRF_DERIVE, KDF_R_MISSING_PARAMETER);
  110. return 0;
  111. }
  112. return tls1_prf_alg(kctx->md, kctx->sec, kctx->seclen,
  113. kctx->seed, kctx->seedlen,
  114. key, *keylen);
  115. }
  116. const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
  117. EVP_PKEY_TLS1_PRF,
  118. 0,
  119. pkey_tls1_prf_init,
  120. 0,
  121. pkey_tls1_prf_cleanup,
  122. 0, 0,
  123. 0, 0,
  124. 0,
  125. 0,
  126. 0,
  127. 0,
  128. 0, 0,
  129. 0, 0, 0, 0,
  130. 0, 0,
  131. 0, 0,
  132. 0,
  133. pkey_tls1_prf_derive,
  134. pkey_tls1_prf_ctrl,
  135. pkey_tls1_prf_ctrl_str
  136. };
  137. static int tls1_prf_P_hash(const EVP_MD *md,
  138. const unsigned char *sec, size_t sec_len,
  139. const unsigned char *seed, size_t seed_len,
  140. unsigned char *out, size_t olen)
  141. {
  142. int chunk;
  143. EVP_MD_CTX *ctx = NULL, *ctx_tmp = NULL, *ctx_init = NULL;
  144. EVP_PKEY *mac_key = NULL;
  145. unsigned char A1[EVP_MAX_MD_SIZE];
  146. size_t A1_len;
  147. int ret = 0;
  148. chunk = EVP_MD_size(md);
  149. OPENSSL_assert(chunk >= 0);
  150. ctx = EVP_MD_CTX_new();
  151. ctx_tmp = EVP_MD_CTX_new();
  152. ctx_init = EVP_MD_CTX_new();
  153. if (ctx == NULL || ctx_tmp == NULL || ctx_init == NULL)
  154. goto err;
  155. EVP_MD_CTX_set_flags(ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  156. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
  157. if (mac_key == NULL)
  158. goto err;
  159. if (!EVP_DigestSignInit(ctx_init, NULL, md, NULL, mac_key))
  160. goto err;
  161. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
  162. goto err;
  163. if (seed != NULL && !EVP_DigestSignUpdate(ctx, seed, seed_len))
  164. goto err;
  165. if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
  166. goto err;
  167. for (;;) {
  168. /* Reinit mac contexts */
  169. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
  170. goto err;
  171. if (!EVP_DigestSignUpdate(ctx, A1, A1_len))
  172. goto err;
  173. if (olen > (size_t)chunk && !EVP_MD_CTX_copy_ex(ctx_tmp, ctx))
  174. goto err;
  175. if (seed && !EVP_DigestSignUpdate(ctx, seed, seed_len))
  176. goto err;
  177. if (olen > (size_t)chunk) {
  178. size_t mac_len;
  179. if (!EVP_DigestSignFinal(ctx, out, &mac_len))
  180. goto err;
  181. out += mac_len;
  182. olen -= mac_len;
  183. /* calc the next A1 value */
  184. if (!EVP_DigestSignFinal(ctx_tmp, A1, &A1_len))
  185. goto err;
  186. } else { /* last one */
  187. if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
  188. goto err;
  189. memcpy(out, A1, olen);
  190. break;
  191. }
  192. }
  193. ret = 1;
  194. err:
  195. EVP_PKEY_free(mac_key);
  196. EVP_MD_CTX_free(ctx);
  197. EVP_MD_CTX_free(ctx_tmp);
  198. EVP_MD_CTX_free(ctx_init);
  199. OPENSSL_cleanse(A1, sizeof(A1));
  200. return ret;
  201. }
  202. static int tls1_prf_alg(const EVP_MD *md,
  203. const unsigned char *sec, size_t slen,
  204. const unsigned char *seed, size_t seed_len,
  205. unsigned char *out, size_t olen)
  206. {
  207. if (EVP_MD_type(md) == NID_md5_sha1) {
  208. size_t i;
  209. unsigned char *tmp;
  210. if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),
  211. seed, seed_len, out, olen))
  212. return 0;
  213. tmp = OPENSSL_malloc(olen);
  214. if (tmp == NULL)
  215. return 0;
  216. if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),
  217. seed, seed_len, tmp, olen)) {
  218. OPENSSL_clear_free(tmp, olen);
  219. return 0;
  220. }
  221. for (i = 0; i < olen; i++)
  222. out[i] ^= tmp[i];
  223. OPENSSL_clear_free(tmp, olen);
  224. return 1;
  225. }
  226. if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
  227. return 0;
  228. return 1;
  229. }