2
0

tls1_prf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2016-2018 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. if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL) {
  34. KDFerr(KDF_F_PKEY_TLS1_PRF_INIT, ERR_R_MALLOC_FAILURE);
  35. return 0;
  36. }
  37. ctx->data = kctx;
  38. return 1;
  39. }
  40. static void pkey_tls1_prf_cleanup(EVP_PKEY_CTX *ctx)
  41. {
  42. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  43. OPENSSL_clear_free(kctx->sec, kctx->seclen);
  44. OPENSSL_cleanse(kctx->seed, kctx->seedlen);
  45. OPENSSL_free(kctx);
  46. }
  47. static int pkey_tls1_prf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  48. {
  49. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  50. switch (type) {
  51. case EVP_PKEY_CTRL_TLS_MD:
  52. kctx->md = p2;
  53. return 1;
  54. case EVP_PKEY_CTRL_TLS_SECRET:
  55. if (p1 < 0)
  56. return 0;
  57. if (kctx->sec != NULL)
  58. OPENSSL_clear_free(kctx->sec, kctx->seclen);
  59. OPENSSL_cleanse(kctx->seed, kctx->seedlen);
  60. kctx->seedlen = 0;
  61. kctx->sec = OPENSSL_memdup(p2, p1);
  62. if (kctx->sec == NULL)
  63. return 0;
  64. kctx->seclen = p1;
  65. return 1;
  66. case EVP_PKEY_CTRL_TLS_SEED:
  67. if (p1 == 0 || p2 == NULL)
  68. return 1;
  69. if (p1 < 0 || p1 > (int)(TLS1_PRF_MAXBUF - kctx->seedlen))
  70. return 0;
  71. memcpy(kctx->seed + kctx->seedlen, p2, p1);
  72. kctx->seedlen += p1;
  73. return 1;
  74. default:
  75. return -2;
  76. }
  77. }
  78. static int pkey_tls1_prf_ctrl_str(EVP_PKEY_CTX *ctx,
  79. const char *type, const char *value)
  80. {
  81. if (value == NULL) {
  82. KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
  83. return 0;
  84. }
  85. if (strcmp(type, "md") == 0) {
  86. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  87. const EVP_MD *md = EVP_get_digestbyname(value);
  88. if (md == NULL) {
  89. KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_INVALID_DIGEST);
  90. return 0;
  91. }
  92. kctx->md = md;
  93. return 1;
  94. }
  95. if (strcmp(type, "secret") == 0)
  96. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
  97. if (strcmp(type, "hexsecret") == 0)
  98. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_TLS_SECRET, value);
  99. if (strcmp(type, "seed") == 0)
  100. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_TLS_SEED, value);
  101. if (strcmp(type, "hexseed") == 0)
  102. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_TLS_SEED, value);
  103. KDFerr(KDF_F_PKEY_TLS1_PRF_CTRL_STR, KDF_R_UNKNOWN_PARAMETER_TYPE);
  104. return -2;
  105. }
  106. static int pkey_tls1_prf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  107. size_t *keylen)
  108. {
  109. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  110. if (kctx->md == NULL) {
  111. KDFerr(KDF_F_PKEY_TLS1_PRF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
  112. return 0;
  113. }
  114. if (kctx->sec == NULL) {
  115. KDFerr(KDF_F_PKEY_TLS1_PRF_DERIVE, KDF_R_MISSING_SECRET);
  116. return 0;
  117. }
  118. if (kctx->seedlen == 0) {
  119. KDFerr(KDF_F_PKEY_TLS1_PRF_DERIVE, KDF_R_MISSING_SEED);
  120. return 0;
  121. }
  122. return tls1_prf_alg(kctx->md, kctx->sec, kctx->seclen,
  123. kctx->seed, kctx->seedlen,
  124. key, *keylen);
  125. }
  126. const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
  127. EVP_PKEY_TLS1_PRF,
  128. 0,
  129. pkey_tls1_prf_init,
  130. 0,
  131. pkey_tls1_prf_cleanup,
  132. 0, 0,
  133. 0, 0,
  134. 0,
  135. 0,
  136. 0,
  137. 0,
  138. 0, 0,
  139. 0, 0, 0, 0,
  140. 0, 0,
  141. 0, 0,
  142. 0,
  143. pkey_tls1_prf_derive,
  144. pkey_tls1_prf_ctrl,
  145. pkey_tls1_prf_ctrl_str
  146. };
  147. static int tls1_prf_P_hash(const EVP_MD *md,
  148. const unsigned char *sec, size_t sec_len,
  149. const unsigned char *seed, size_t seed_len,
  150. unsigned char *out, size_t olen)
  151. {
  152. int chunk;
  153. EVP_MD_CTX *ctx = NULL, *ctx_tmp = NULL, *ctx_init = NULL;
  154. EVP_PKEY *mac_key = NULL;
  155. unsigned char A1[EVP_MAX_MD_SIZE];
  156. size_t A1_len;
  157. int ret = 0;
  158. chunk = EVP_MD_size(md);
  159. if (!ossl_assert(chunk > 0))
  160. goto err;
  161. ctx = EVP_MD_CTX_new();
  162. ctx_tmp = EVP_MD_CTX_new();
  163. ctx_init = EVP_MD_CTX_new();
  164. if (ctx == NULL || ctx_tmp == NULL || ctx_init == NULL)
  165. goto err;
  166. EVP_MD_CTX_set_flags(ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  167. mac_key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
  168. if (mac_key == NULL)
  169. goto err;
  170. if (!EVP_DigestSignInit(ctx_init, NULL, md, NULL, mac_key))
  171. goto err;
  172. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
  173. goto err;
  174. if (seed != NULL && !EVP_DigestSignUpdate(ctx, seed, seed_len))
  175. goto err;
  176. if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
  177. goto err;
  178. for (;;) {
  179. /* Reinit mac contexts */
  180. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
  181. goto err;
  182. if (!EVP_DigestSignUpdate(ctx, A1, A1_len))
  183. goto err;
  184. if (olen > (size_t)chunk && !EVP_MD_CTX_copy_ex(ctx_tmp, ctx))
  185. goto err;
  186. if (seed && !EVP_DigestSignUpdate(ctx, seed, seed_len))
  187. goto err;
  188. if (olen > (size_t)chunk) {
  189. size_t mac_len;
  190. if (!EVP_DigestSignFinal(ctx, out, &mac_len))
  191. goto err;
  192. out += mac_len;
  193. olen -= mac_len;
  194. /* calc the next A1 value */
  195. if (!EVP_DigestSignFinal(ctx_tmp, A1, &A1_len))
  196. goto err;
  197. } else { /* last one */
  198. if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
  199. goto err;
  200. memcpy(out, A1, olen);
  201. break;
  202. }
  203. }
  204. ret = 1;
  205. err:
  206. EVP_PKEY_free(mac_key);
  207. EVP_MD_CTX_free(ctx);
  208. EVP_MD_CTX_free(ctx_tmp);
  209. EVP_MD_CTX_free(ctx_init);
  210. OPENSSL_cleanse(A1, sizeof(A1));
  211. return ret;
  212. }
  213. static int tls1_prf_alg(const EVP_MD *md,
  214. const unsigned char *sec, size_t slen,
  215. const unsigned char *seed, size_t seed_len,
  216. unsigned char *out, size_t olen)
  217. {
  218. if (EVP_MD_type(md) == NID_md5_sha1) {
  219. size_t i;
  220. unsigned char *tmp;
  221. if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),
  222. seed, seed_len, out, olen))
  223. return 0;
  224. if ((tmp = OPENSSL_malloc(olen)) == NULL) {
  225. KDFerr(KDF_F_TLS1_PRF_ALG, ERR_R_MALLOC_FAILURE);
  226. return 0;
  227. }
  228. if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),
  229. seed, seed_len, tmp, olen)) {
  230. OPENSSL_clear_free(tmp, olen);
  231. return 0;
  232. }
  233. for (i = 0; i < olen; i++)
  234. out[i] ^= tmp[i];
  235. OPENSSL_clear_free(tmp, olen);
  236. return 1;
  237. }
  238. if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
  239. return 0;
  240. return 1;
  241. }