kdf.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ====================================================================
  2. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <assert.h>
  53. #include <openssl/digest.h>
  54. #include <openssl/hmac.h>
  55. #include <openssl/mem.h>
  56. #include "internal.h"
  57. #include "../../internal.h"
  58. // tls1_P_hash computes the TLS P_<hash> function as described in RFC 5246,
  59. // section 5. It XORs |out_len| bytes to |out|, using |md| as the hash and
  60. // |secret| as the secret. |label|, |seed1|, and |seed2| are concatenated to
  61. // form the seed parameter. It returns true on success and false on failure.
  62. static int tls1_P_hash(uint8_t *out, size_t out_len,
  63. const EVP_MD *md,
  64. const uint8_t *secret, size_t secret_len,
  65. const char *label, size_t label_len,
  66. const uint8_t *seed1, size_t seed1_len,
  67. const uint8_t *seed2, size_t seed2_len) {
  68. HMAC_CTX ctx, ctx_tmp, ctx_init;
  69. uint8_t A1[EVP_MAX_MD_SIZE];
  70. unsigned A1_len;
  71. int ret = 0;
  72. const size_t chunk = EVP_MD_size(md);
  73. HMAC_CTX_init(&ctx);
  74. HMAC_CTX_init(&ctx_tmp);
  75. HMAC_CTX_init(&ctx_init);
  76. if (!HMAC_Init_ex(&ctx_init, secret, secret_len, md, NULL) ||
  77. !HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
  78. !HMAC_Update(&ctx, (const uint8_t *) label, label_len) ||
  79. !HMAC_Update(&ctx, seed1, seed1_len) ||
  80. !HMAC_Update(&ctx, seed2, seed2_len) ||
  81. !HMAC_Final(&ctx, A1, &A1_len)) {
  82. goto err;
  83. }
  84. for (;;) {
  85. unsigned len;
  86. uint8_t hmac[EVP_MAX_MD_SIZE];
  87. if (!HMAC_CTX_copy_ex(&ctx, &ctx_init) ||
  88. !HMAC_Update(&ctx, A1, A1_len) ||
  89. // Save a copy of |ctx| to compute the next A1 value below.
  90. (out_len > chunk && !HMAC_CTX_copy_ex(&ctx_tmp, &ctx)) ||
  91. !HMAC_Update(&ctx, (const uint8_t *) label, label_len) ||
  92. !HMAC_Update(&ctx, seed1, seed1_len) ||
  93. !HMAC_Update(&ctx, seed2, seed2_len) ||
  94. !HMAC_Final(&ctx, hmac, &len)) {
  95. goto err;
  96. }
  97. assert(len == chunk);
  98. // XOR the result into |out|.
  99. if (len > out_len) {
  100. len = out_len;
  101. }
  102. for (unsigned i = 0; i < len; i++) {
  103. out[i] ^= hmac[i];
  104. }
  105. out += len;
  106. out_len -= len;
  107. if (out_len == 0) {
  108. break;
  109. }
  110. // Calculate the next A1 value.
  111. if (!HMAC_Final(&ctx_tmp, A1, &A1_len)) {
  112. goto err;
  113. }
  114. }
  115. ret = 1;
  116. err:
  117. OPENSSL_cleanse(A1, sizeof(A1));
  118. HMAC_CTX_cleanup(&ctx);
  119. HMAC_CTX_cleanup(&ctx_tmp);
  120. HMAC_CTX_cleanup(&ctx_init);
  121. return ret;
  122. }
  123. int CRYPTO_tls1_prf(const EVP_MD *digest,
  124. uint8_t *out, size_t out_len,
  125. const uint8_t *secret, size_t secret_len,
  126. const char *label, size_t label_len,
  127. const uint8_t *seed1, size_t seed1_len,
  128. const uint8_t *seed2, size_t seed2_len) {
  129. if (out_len == 0) {
  130. return 1;
  131. }
  132. OPENSSL_memset(out, 0, out_len);
  133. if (digest == EVP_md5_sha1()) {
  134. // If using the MD5/SHA1 PRF, |secret| is partitioned between MD5 and SHA-1.
  135. size_t secret_half = secret_len - (secret_len / 2);
  136. if (!tls1_P_hash(out, out_len, EVP_md5(), secret, secret_half, label,
  137. label_len, seed1, seed1_len, seed2, seed2_len)) {
  138. return 0;
  139. }
  140. // Note that, if |secret_len| is odd, the two halves share a byte.
  141. secret += secret_len - secret_half;
  142. secret_len = secret_half;
  143. digest = EVP_sha1();
  144. }
  145. return tls1_P_hash(out, out_len, digest, secret, secret_len, label, label_len,
  146. seed1, seed1_len, seed2, seed2_len);
  147. }