crypto_dh.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_dh.c
  8. * \brief Block of functions related with DH utilities and operations.
  9. * over Z_p. We aren't using this for any new crypto -- EC is more
  10. * efficient.
  11. **/
  12. #include "lib/crypt_ops/compat_openssl.h"
  13. #include "lib/crypt_ops/crypto_dh.h"
  14. #include "lib/crypt_ops/crypto_digest.h"
  15. #include "lib/crypt_ops/crypto_hkdf.h"
  16. #include "lib/crypt_ops/crypto_util.h"
  17. #include "lib/log/log.h"
  18. #include "lib/log/util_bug.h"
  19. /** Our DH 'g' parameter */
  20. const unsigned DH_GENERATOR = 2;
  21. /** This is the 1024-bit safe prime that Apache uses for its DH stuff; see
  22. * modules/ssl/ssl_engine_dh.c; Apache also uses a generator of 2 with this
  23. * prime.
  24. */
  25. const char TLS_DH_PRIME[] =
  26. "D67DE440CBBBDC1936D693D34AFD0AD50C84D239A45F520BB88174CB98"
  27. "BCE951849F912E639C72FB13B4B4D7177E16D55AC179BA420B2A29FE324A"
  28. "467A635E81FF5901377BEDDCFD33168A461AAD3B72DAE8860078045B07A7"
  29. "DBCA7874087D1510EA9FCC9DDD330507DD62DB88AEAA747DE0F4D6E2BD68"
  30. "B0E7393E0F24218EB3";
  31. /**
  32. * This is from rfc2409, section 6.2. It's a safe prime, and
  33. * supposedly it equals:
  34. * 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 }.
  35. */
  36. const char OAKLEY_PRIME_2[] =
  37. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
  38. "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
  39. "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
  40. "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
  41. "49286651ECE65381FFFFFFFFFFFFFFFF";
  42. void
  43. crypto_dh_init(void)
  44. {
  45. #ifdef ENABLE_OPENSSL
  46. crypto_dh_init_openssl();
  47. #endif
  48. #ifdef ENABLE_NSS
  49. crypto_dh_init_nss();
  50. #endif
  51. }
  52. void
  53. crypto_dh_free_all(void)
  54. {
  55. #ifdef ENABLE_OPENSSL
  56. crypto_dh_free_all_openssl();
  57. #endif
  58. #ifdef ENABLE_NSS
  59. crypto_dh_free_all_nss();
  60. #endif
  61. }
  62. /** Given a DH key exchange object, and our peer's value of g^y (as a
  63. * <b>pubkey_len</b>-byte value in <b>pubkey</b>) generate
  64. * <b>secret_bytes_out</b> bytes of shared key material and write them
  65. * to <b>secret_out</b>. Return the number of bytes generated on success,
  66. * or -1 on failure.
  67. *
  68. * (We generate key material by computing
  69. * SHA1( g^xy || "\x00" ) || SHA1( g^xy || "\x01" ) || ...
  70. * where || is concatenation.)
  71. */
  72. ssize_t
  73. crypto_dh_compute_secret(int severity, crypto_dh_t *dh,
  74. const char *pubkey, size_t pubkey_len,
  75. char *secret_out, size_t secret_bytes_out)
  76. {
  77. tor_assert(secret_bytes_out/DIGEST_LEN <= 255);
  78. unsigned char *secret_tmp = NULL;
  79. size_t secret_len=0, secret_tmp_len=0;
  80. secret_tmp_len = crypto_dh_get_bytes(dh);
  81. secret_tmp = tor_malloc(secret_tmp_len);
  82. ssize_t result = crypto_dh_handshake(severity, dh, pubkey, pubkey_len,
  83. secret_tmp, secret_tmp_len);
  84. if (result < 0)
  85. goto error;
  86. secret_len = result;
  87. if (crypto_expand_key_material_TAP(secret_tmp, secret_len,
  88. (uint8_t*)secret_out, secret_bytes_out)<0)
  89. goto error;
  90. secret_len = secret_bytes_out;
  91. goto done;
  92. error:
  93. result = -1;
  94. done:
  95. if (secret_tmp) {
  96. memwipe(secret_tmp, 0, secret_tmp_len);
  97. tor_free(secret_tmp);
  98. }
  99. if (result < 0)
  100. return result;
  101. else
  102. return secret_len;
  103. }