tls1_prf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #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_MAC_CTX *ctx = NULL, *ctx_tmp = NULL, *ctx_init = NULL;
  154. unsigned char A1[EVP_MAX_MD_SIZE];
  155. size_t A1_len;
  156. int ret = 0;
  157. chunk = EVP_MD_size(md);
  158. if (!ossl_assert(chunk > 0))
  159. goto err;
  160. ctx = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
  161. ctx_tmp = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
  162. ctx_init = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
  163. if (ctx == NULL || ctx_tmp == NULL || ctx_init == NULL)
  164. goto err;
  165. if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_FLAGS, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW) != 1)
  166. goto err;
  167. if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_MD, md) != 1)
  168. goto err;
  169. if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_KEY, sec, sec_len) != 1)
  170. goto err;
  171. if (!EVP_MAC_init(ctx_init))
  172. goto err;
  173. if (!EVP_MAC_CTX_copy(ctx, ctx_init))
  174. goto err;
  175. if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
  176. goto err;
  177. if (!EVP_MAC_final(ctx, A1, &A1_len))
  178. goto err;
  179. for (;;) {
  180. /* Reinit mac contexts */
  181. if (!EVP_MAC_CTX_copy(ctx, ctx_init))
  182. goto err;
  183. if (!EVP_MAC_update(ctx, A1, A1_len))
  184. goto err;
  185. if (olen > (size_t)chunk && !EVP_MAC_CTX_copy(ctx_tmp, ctx))
  186. goto err;
  187. if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
  188. goto err;
  189. if (olen > (size_t)chunk) {
  190. size_t mac_len;
  191. if (!EVP_MAC_final(ctx, out, &mac_len))
  192. goto err;
  193. out += mac_len;
  194. olen -= mac_len;
  195. /* calc the next A1 value */
  196. if (!EVP_MAC_final(ctx_tmp, A1, &A1_len))
  197. goto err;
  198. } else { /* last one */
  199. if (!EVP_MAC_final(ctx, A1, &A1_len))
  200. goto err;
  201. memcpy(out, A1, olen);
  202. break;
  203. }
  204. }
  205. ret = 1;
  206. err:
  207. EVP_MAC_CTX_free(ctx);
  208. EVP_MAC_CTX_free(ctx_tmp);
  209. EVP_MAC_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. }