crypto_hkdf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "crypto_hkdf.h"
  11. #include "crypto_util.h"
  12. #include "crypto_digest.h"
  13. /** Given <b>key_in_len</b> bytes of negotiated randomness in <b>key_in</b>
  14. * ("K"), expand it into <b>key_out_len</b> bytes of negotiated key material in
  15. * <b>key_out</b> by taking the first <b>key_out_len</b> bytes of
  16. * H(K | [00]) | H(K | [01]) | ....
  17. *
  18. * This is the key expansion algorithm used in the "TAP" circuit extension
  19. * mechanism; it shouldn't be used for new protocols.
  20. *
  21. * Return 0 on success, -1 on failure.
  22. */
  23. int
  24. crypto_expand_key_material_TAP(const uint8_t *key_in, size_t key_in_len,
  25. uint8_t *key_out, size_t key_out_len)
  26. {
  27. int i, r = -1;
  28. uint8_t *cp, *tmp = tor_malloc(key_in_len+1);
  29. uint8_t digest[DIGEST_LEN];
  30. /* If we try to get more than this amount of key data, we'll repeat blocks.*/
  31. tor_assert(key_out_len <= DIGEST_LEN*256);
  32. memcpy(tmp, key_in, key_in_len);
  33. for (cp = key_out, i=0; cp < key_out+key_out_len;
  34. ++i, cp += DIGEST_LEN) {
  35. tmp[key_in_len] = i;
  36. if (crypto_digest((char*)digest, (const char *)tmp, key_in_len+1) < 0)
  37. goto exit;
  38. memcpy(cp, digest, MIN(DIGEST_LEN, key_out_len-(cp-key_out)));
  39. }
  40. r = 0;
  41. exit:
  42. memwipe(tmp, 0, key_in_len+1);
  43. tor_free(tmp);
  44. memwipe(digest, 0, sizeof(digest));
  45. return r;
  46. }
  47. /** Expand some secret key material according to RFC5869, using SHA256 as the
  48. * underlying hash. The <b>key_in_len</b> bytes at <b>key_in</b> are the
  49. * secret key material; the <b>salt_in_len</b> bytes at <b>salt_in</b> and the
  50. * <b>info_in_len</b> bytes in <b>info_in_len</b> are the algorithm's "salt"
  51. * and "info" parameters respectively. On success, write <b>key_out_len</b>
  52. * bytes to <b>key_out</b> and return 0. Assert on failure.
  53. */
  54. int
  55. crypto_expand_key_material_rfc5869_sha256(
  56. const uint8_t *key_in, size_t key_in_len,
  57. const uint8_t *salt_in, size_t salt_in_len,
  58. const uint8_t *info_in, size_t info_in_len,
  59. uint8_t *key_out, size_t key_out_len)
  60. {
  61. uint8_t prk[DIGEST256_LEN];
  62. uint8_t tmp[DIGEST256_LEN + 128 + 1];
  63. uint8_t mac[DIGEST256_LEN];
  64. int i;
  65. uint8_t *outp;
  66. size_t tmp_len;
  67. crypto_hmac_sha256((char*)prk,
  68. (const char*)salt_in, salt_in_len,
  69. (const char*)key_in, key_in_len);
  70. /* If we try to get more than this amount of key data, we'll repeat blocks.*/
  71. tor_assert(key_out_len <= DIGEST256_LEN * 256);
  72. tor_assert(info_in_len <= 128);
  73. memset(tmp, 0, sizeof(tmp));
  74. outp = key_out;
  75. i = 1;
  76. while (key_out_len) {
  77. size_t n;
  78. if (i > 1) {
  79. memcpy(tmp, mac, DIGEST256_LEN);
  80. memcpy(tmp+DIGEST256_LEN, info_in, info_in_len);
  81. tmp[DIGEST256_LEN+info_in_len] = i;
  82. tmp_len = DIGEST256_LEN + info_in_len + 1;
  83. } else {
  84. memcpy(tmp, info_in, info_in_len);
  85. tmp[info_in_len] = i;
  86. tmp_len = info_in_len + 1;
  87. }
  88. crypto_hmac_sha256((char*)mac,
  89. (const char*)prk, DIGEST256_LEN,
  90. (const char*)tmp, tmp_len);
  91. n = key_out_len < DIGEST256_LEN ? key_out_len : DIGEST256_LEN;
  92. memcpy(outp, mac, n);
  93. key_out_len -= n;
  94. outp += n;
  95. ++i;
  96. }
  97. memwipe(tmp, 0, sizeof(tmp));
  98. memwipe(mac, 0, sizeof(mac));
  99. return 0;
  100. }