crypto_hkdf.c 6.5 KB

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