crypto_hkdf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_hkdf.c
  8. * \brief Block of functions related with HKDF utilities and operations.
  9. **/
  10. #include "lib/crypt_ops/crypto_hkdf.h"
  11. #include "lib/crypt_ops/crypto_util.h"
  12. #include "lib/crypt_ops/crypto_digest.h"
  13. #include "lib/crypt_ops/crypto_openssl_mgt.h"
  14. #include "lib/intmath/cmp.h"
  15. #include "lib/log/util_bug.h"
  16. #include <openssl/opensslv.h>
  17. #if defined(HAVE_ERR_LOAD_KDF_STRINGS)
  18. #include <openssl/kdf.h>
  19. #define HAVE_OPENSSL_HKDF 1
  20. #endif
  21. #include <string.h>
  22. /** Given <b>key_in_len</b> bytes of negotiated randomness in <b>key_in</b>
  23. * ("K"), expand it into <b>key_out_len</b> bytes of negotiated key material in
  24. * <b>key_out</b> by taking the first <b>key_out_len</b> bytes of
  25. * H(K | [00]) | H(K | [01]) | ....
  26. *
  27. * This is the key expansion algorithm used in the "TAP" circuit extension
  28. * mechanism; it shouldn't be used for new protocols.
  29. *
  30. * Return 0 on success, -1 on failure.
  31. */
  32. int
  33. crypto_expand_key_material_TAP(const uint8_t *key_in, size_t key_in_len,
  34. uint8_t *key_out, size_t key_out_len)
  35. {
  36. int i, r = -1;
  37. uint8_t *cp, *tmp = tor_malloc(key_in_len+1);
  38. uint8_t digest[DIGEST_LEN];
  39. /* If we try to get more than this amount of key data, we'll repeat blocks.*/
  40. tor_assert(key_out_len <= DIGEST_LEN*256);
  41. memcpy(tmp, key_in, key_in_len);
  42. for (cp = key_out, i=0; cp < key_out+key_out_len;
  43. ++i, cp += DIGEST_LEN) {
  44. tmp[key_in_len] = i;
  45. if (crypto_digest((char*)digest, (const char *)tmp, key_in_len+1) < 0)
  46. goto exit;
  47. memcpy(cp, digest, MIN(DIGEST_LEN, key_out_len-(cp-key_out)));
  48. }
  49. r = 0;
  50. exit:
  51. memwipe(tmp, 0, key_in_len+1);
  52. tor_free(tmp);
  53. memwipe(digest, 0, sizeof(digest));
  54. return r;
  55. }
  56. #ifdef HAVE_OPENSSL_HKDF
  57. /**
  58. * Perform RFC5869 HKDF computation using OpenSSL (only to be called from
  59. * crypto_expand_key_material_rfc5869_sha256_openssl). Note that OpenSSL
  60. * requires input key to be nonempty and salt length to be equal or less
  61. * than 1024.
  62. */
  63. static int
  64. crypto_expand_key_material_rfc5869_sha256_openssl(
  65. const uint8_t *key_in, size_t key_in_len,
  66. const uint8_t *salt_in, size_t salt_in_len,
  67. const uint8_t *info_in, size_t info_in_len,
  68. uint8_t *key_out, size_t key_out_len)
  69. {
  70. int r;
  71. EVP_PKEY_CTX *evp_pkey_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  72. tor_assert(evp_pkey_ctx);
  73. tor_assert(key_in_len != 0);
  74. tor_assert(salt_in_len <= 1024);
  75. r = EVP_PKEY_derive_init(evp_pkey_ctx);
  76. tor_assert(r == 1);
  77. r = EVP_PKEY_CTX_set_hkdf_md(evp_pkey_ctx, EVP_sha256());
  78. tor_assert(r == 1);
  79. r = EVP_PKEY_CTX_set1_hkdf_salt(evp_pkey_ctx, salt_in, (int)salt_in_len);
  80. tor_assert(r == 1);
  81. r = EVP_PKEY_CTX_set1_hkdf_key(evp_pkey_ctx, key_in, (int)key_in_len);
  82. tor_assert(r == 1);
  83. r = EVP_PKEY_CTX_add1_hkdf_info(evp_pkey_ctx, info_in, (int)info_in_len);
  84. tor_assert(r == 1);
  85. r = EVP_PKEY_derive(evp_pkey_ctx, key_out, &key_out_len);
  86. tor_assert(r == 1);
  87. EVP_PKEY_CTX_free(evp_pkey_ctx);
  88. return 0;
  89. }
  90. #else
  91. /**
  92. * Perform RFC5869 HKDF computation using our own legacy implementation.
  93. * Only to be called from crypto_expand_key_material_rfc5869_sha256_openssl.
  94. */
  95. static int
  96. crypto_expand_key_material_rfc5869_sha256_legacy(
  97. const uint8_t *key_in, size_t key_in_len,
  98. const uint8_t *salt_in, size_t salt_in_len,
  99. const uint8_t *info_in, size_t info_in_len,
  100. uint8_t *key_out, size_t key_out_len)
  101. {
  102. uint8_t prk[DIGEST256_LEN];
  103. uint8_t tmp[DIGEST256_LEN + 128 + 1];
  104. uint8_t mac[DIGEST256_LEN];
  105. int i;
  106. uint8_t *outp;
  107. size_t tmp_len;
  108. crypto_hmac_sha256((char*)prk,
  109. (const char*)salt_in, salt_in_len,
  110. (const char*)key_in, key_in_len);
  111. /* If we try to get more than this amount of key data, we'll repeat blocks.*/
  112. tor_assert(key_out_len <= DIGEST256_LEN * 256);
  113. tor_assert(info_in_len <= 128);
  114. memset(tmp, 0, sizeof(tmp));
  115. outp = key_out;
  116. i = 1;
  117. while (key_out_len) {
  118. size_t n;
  119. if (i > 1) {
  120. memcpy(tmp, mac, DIGEST256_LEN);
  121. memcpy(tmp+DIGEST256_LEN, info_in, info_in_len);
  122. tmp[DIGEST256_LEN+info_in_len] = i;
  123. tmp_len = DIGEST256_LEN + info_in_len + 1;
  124. } else {
  125. memcpy(tmp, info_in, info_in_len);
  126. tmp[info_in_len] = i;
  127. tmp_len = info_in_len + 1;
  128. }
  129. crypto_hmac_sha256((char*)mac,
  130. (const char*)prk, DIGEST256_LEN,
  131. (const char*)tmp, tmp_len);
  132. n = key_out_len < DIGEST256_LEN ? key_out_len : DIGEST256_LEN;
  133. memcpy(outp, mac, n);
  134. key_out_len -= n;
  135. outp += n;
  136. ++i;
  137. }
  138. memwipe(tmp, 0, sizeof(tmp));
  139. memwipe(mac, 0, sizeof(mac));
  140. return 0;
  141. }
  142. #endif
  143. /** Expand some secret key material according to RFC5869, using SHA256 as the
  144. * underlying hash. The <b>key_in_len</b> bytes at <b>key_in</b> are the
  145. * secret key material; the <b>salt_in_len</b> bytes at <b>salt_in</b> and the
  146. * <b>info_in_len</b> bytes in <b>info_in_len</b> are the algorithm's "salt"
  147. * and "info" parameters respectively. On success, write <b>key_out_len</b>
  148. * bytes to <b>key_out</b> and return 0. Assert on failure.
  149. */
  150. int
  151. crypto_expand_key_material_rfc5869_sha256(
  152. const uint8_t *key_in, size_t key_in_len,
  153. const uint8_t *salt_in, size_t salt_in_len,
  154. const uint8_t *info_in, size_t info_in_len,
  155. uint8_t *key_out, size_t key_out_len)
  156. {
  157. tor_assert(key_in);
  158. tor_assert(key_in_len > 0);
  159. #ifdef HAVE_OPENSSL_HKDF
  160. return crypto_expand_key_material_rfc5869_sha256_openssl(key_in,
  161. key_in_len, salt_in,
  162. salt_in_len, info_in,
  163. info_in_len,
  164. key_out, key_out_len);
  165. #else
  166. return crypto_expand_key_material_rfc5869_sha256_legacy(key_in,
  167. key_in_len, salt_in,
  168. salt_in_len, info_in,
  169. info_in_len,
  170. key_out, key_out_len);
  171. #endif
  172. }