2
0

tls1_prf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2016.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/kdf.h>
  61. #include <openssl/evp.h>
  62. #include "internal/evp_int.h"
  63. static int tls1_prf_alg(const EVP_MD *md,
  64. const unsigned char *sec, size_t slen,
  65. const unsigned char *seed, size_t seed_len,
  66. unsigned char *out, size_t olen);
  67. #define TLS1_PRF_MAXBUF 1024
  68. /* TLS KDF pkey context structure */
  69. typedef struct {
  70. /* Digest to use for PRF */
  71. const EVP_MD *md;
  72. /* Secret value to use for PRF */
  73. unsigned char *sec;
  74. size_t seclen;
  75. /* Buffer of concatenated seed data */
  76. unsigned char seed[TLS1_PRF_MAXBUF];
  77. size_t seedlen;
  78. } TLS1_PRF_PKEY_CTX;
  79. static int pkey_tls1_prf_init(EVP_PKEY_CTX *ctx)
  80. {
  81. TLS1_PRF_PKEY_CTX *kctx;
  82. kctx = OPENSSL_zalloc(sizeof(*kctx));
  83. if (kctx == NULL)
  84. return 0;
  85. ctx->data = kctx;
  86. return 1;
  87. }
  88. static void pkey_tls1_prf_cleanup(EVP_PKEY_CTX *ctx)
  89. {
  90. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  91. OPENSSL_clear_free(kctx->sec, kctx->seclen);
  92. OPENSSL_cleanse(kctx->seed, kctx->seedlen);
  93. OPENSSL_free(kctx);
  94. }
  95. static int pkey_tls1_prf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  96. {
  97. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  98. switch (type) {
  99. case EVP_PKEY_CTRL_TLS_MD:
  100. kctx->md = p2;
  101. return 1;
  102. case EVP_PKEY_CTRL_TLS_SECRET:
  103. if (p1 < 0)
  104. return 0;
  105. if (kctx->sec != NULL)
  106. OPENSSL_clear_free(kctx->sec, kctx->seclen);
  107. OPENSSL_cleanse(kctx->seed, kctx->seedlen);
  108. kctx->seedlen = 0;
  109. kctx->sec = OPENSSL_memdup(p2, p1);
  110. if (kctx->sec == NULL)
  111. return 0;
  112. kctx->seclen = p1;
  113. return 1;
  114. case EVP_PKEY_CTRL_TLS_SEED:
  115. if (p1 == 0 || p2 == NULL)
  116. return 1;
  117. if (p1 < 0 || p1 > (int)(TLS1_PRF_MAXBUF - kctx->seedlen))
  118. return 0;
  119. memcpy(kctx->seed + kctx->seedlen, p2, p1);
  120. kctx->seedlen += p1;
  121. return 1;
  122. default:
  123. return -2;
  124. }
  125. }
  126. static int pkey_tls1_prf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  127. size_t *keylen)
  128. {
  129. TLS1_PRF_PKEY_CTX *kctx = ctx->data;
  130. if (kctx->md == NULL || kctx->sec == NULL || kctx->seedlen == 0)
  131. return 0;
  132. return tls1_prf_alg(kctx->md, kctx->sec, kctx->seclen,
  133. kctx->seed, kctx->seedlen,
  134. key, *keylen);
  135. }
  136. const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
  137. EVP_PKEY_TLS1_PRF,
  138. 0,
  139. pkey_tls1_prf_init,
  140. 0,
  141. pkey_tls1_prf_cleanup,
  142. 0, 0,
  143. 0, 0,
  144. 0,
  145. 0,
  146. 0,
  147. 0,
  148. 0, 0,
  149. 0, 0, 0, 0,
  150. 0, 0,
  151. 0, 0,
  152. 0,
  153. pkey_tls1_prf_derive,
  154. pkey_tls1_prf_ctrl,
  155. 0
  156. };
  157. static int tls1_prf_P_hash(const EVP_MD *md,
  158. const unsigned char *sec, size_t sec_len,
  159. const unsigned char *seed, size_t seed_len,
  160. unsigned char *out, size_t olen)
  161. {
  162. int chunk;
  163. EVP_MD_CTX *ctx = NULL, *ctx_tmp = NULL, *ctx_init = NULL;
  164. EVP_PKEY *mac_key = NULL;
  165. unsigned char A1[EVP_MAX_MD_SIZE];
  166. size_t A1_len;
  167. int ret = 0;
  168. chunk = EVP_MD_size(md);
  169. OPENSSL_assert(chunk >= 0);
  170. ctx = EVP_MD_CTX_new();
  171. ctx_tmp = EVP_MD_CTX_new();
  172. ctx_init = EVP_MD_CTX_new();
  173. if (ctx == NULL || ctx_tmp == NULL || ctx_init == NULL)
  174. goto err;
  175. EVP_MD_CTX_set_flags(ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  176. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
  177. if (mac_key == NULL)
  178. goto err;
  179. if (!EVP_DigestSignInit(ctx_init, NULL, md, NULL, mac_key))
  180. goto err;
  181. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
  182. goto err;
  183. if (seed != NULL && !EVP_DigestSignUpdate(ctx, seed, seed_len))
  184. goto err;
  185. if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
  186. goto err;
  187. for (;;) {
  188. /* Reinit mac contexts */
  189. if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
  190. goto err;
  191. if (!EVP_DigestSignUpdate(ctx, A1, A1_len))
  192. goto err;
  193. if (olen > (size_t)chunk && !EVP_MD_CTX_copy_ex(ctx_tmp, ctx))
  194. goto err;
  195. if (seed && !EVP_DigestSignUpdate(ctx, seed, seed_len))
  196. goto err;
  197. if (olen > (size_t)chunk) {
  198. size_t mac_len;
  199. if (!EVP_DigestSignFinal(ctx, out, &mac_len))
  200. goto err;
  201. out += mac_len;
  202. olen -= mac_len;
  203. /* calc the next A1 value */
  204. if (!EVP_DigestSignFinal(ctx_tmp, A1, &A1_len))
  205. goto err;
  206. } else { /* last one */
  207. if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
  208. goto err;
  209. memcpy(out, A1, olen);
  210. break;
  211. }
  212. }
  213. ret = 1;
  214. err:
  215. EVP_PKEY_free(mac_key);
  216. EVP_MD_CTX_free(ctx);
  217. EVP_MD_CTX_free(ctx_tmp);
  218. EVP_MD_CTX_free(ctx_init);
  219. OPENSSL_cleanse(A1, sizeof(A1));
  220. return ret;
  221. }
  222. static int tls1_prf_alg(const EVP_MD *md,
  223. const unsigned char *sec, size_t slen,
  224. const unsigned char *seed, size_t seed_len,
  225. unsigned char *out, size_t olen)
  226. {
  227. if (EVP_MD_type(md) == NID_md5_sha1) {
  228. size_t i;
  229. unsigned char *tmp;
  230. if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),
  231. seed, seed_len, out, olen))
  232. return 0;
  233. tmp = OPENSSL_malloc(olen);
  234. if (tmp == NULL)
  235. return 0;
  236. if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),
  237. seed, seed_len, tmp, olen)) {
  238. OPENSSL_clear_free(tmp, olen);
  239. return 0;
  240. }
  241. for (i = 0; i < olen; i++)
  242. out[i] ^= tmp[i];
  243. OPENSSL_clear_free(tmp, olen);
  244. return 1;
  245. }
  246. if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
  247. return 0;
  248. return 1;
  249. }