crypto_curve25519.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Wrapper code for a curve25519 implementation. */
  4. #define CRYPTO_CURVE25519_PRIVATE
  5. #include "orconfig.h"
  6. #ifdef HAVE_SYS_STAT_H
  7. #include <sys/stat.h>
  8. #endif
  9. #include "crypto.h"
  10. #include "crypto_curve25519.h"
  11. #include "util.h"
  12. #include "torlog.h"
  13. /* ==============================
  14. Part 1: wrap a suitable curve25519 implementation as curve25519_impl
  15. ============================== */
  16. #ifdef USE_CURVE25519_DONNA
  17. int curve25519_donna(uint8_t *mypublic,
  18. const uint8_t *secret, const uint8_t *basepoint);
  19. #endif
  20. #ifdef USE_CURVE25519_NACL
  21. #include <crypto_scalarmult_curve25519.h>
  22. #endif
  23. int
  24. curve25519_impl(uint8_t *output, const uint8_t *secret,
  25. const uint8_t *basepoint)
  26. {
  27. #ifdef USE_CURVE25519_DONNA
  28. return curve25519_donna(output, secret, basepoint);
  29. #elif defined(USE_CURVE25519_NACL)
  30. return crypto_scalarmult_curve25519(output, secret, basepoint);
  31. #else
  32. #error "No implementation of curve25519 is available."
  33. #endif
  34. }
  35. /* ==============================
  36. Part 2: Wrap curve25519_impl with some convenience types and functions.
  37. ============================== */
  38. /**
  39. * Return true iff a curve25519_public_key_t seems valid. (It's not necessary
  40. * to see if the point is on the curve, since the twist is also secure, but we
  41. * do need to make sure that it isn't the point at infinity.) */
  42. int
  43. curve25519_public_key_is_ok(const curve25519_public_key_t *key)
  44. {
  45. return !safe_mem_is_zero(key->public_key, CURVE25519_PUBKEY_LEN);
  46. }
  47. /** Generate a new keypair and return the secret key. If <b>extra_strong</b>
  48. * is true, this key is possibly going to get used more than once, so
  49. * use a better-than-usual RNG. Return 0 on success, -1 on failure. */
  50. int
  51. curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  52. int extra_strong)
  53. {
  54. uint8_t k_tmp[CURVE25519_SECKEY_LEN];
  55. if (crypto_rand((char*)key_out->secret_key, CURVE25519_SECKEY_LEN) < 0)
  56. return -1;
  57. if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) {
  58. /* If they asked for extra-strong entropy and we have some, use it as an
  59. * HMAC key to improve not-so-good entopy rather than using it directly,
  60. * just in case the extra-strong entropy is less amazing than we hoped. */
  61. crypto_hmac_sha256((char *)key_out->secret_key,
  62. (const char *)k_tmp, sizeof(k_tmp),
  63. (const char *)key_out->secret_key, CURVE25519_SECKEY_LEN);
  64. }
  65. memwipe(k_tmp, 0, sizeof(k_tmp));
  66. key_out->secret_key[0] &= 248;
  67. key_out->secret_key[31] &= 127;
  68. key_out->secret_key[31] |= 64;
  69. return 0;
  70. }
  71. void
  72. curve25519_public_key_generate(curve25519_public_key_t *key_out,
  73. const curve25519_secret_key_t *seckey)
  74. {
  75. static const uint8_t basepoint[32] = {9};
  76. curve25519_impl(key_out->public_key, seckey->secret_key, basepoint);
  77. }
  78. int
  79. curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  80. int extra_strong)
  81. {
  82. if (curve25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  83. return -1;
  84. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  85. return 0;
  86. }
  87. int
  88. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  89. const char *fname,
  90. const char *tag)
  91. {
  92. char contents[32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  93. int r;
  94. memset(contents, 0, sizeof(contents));
  95. tor_snprintf(contents, sizeof(contents), "== c25519v1: %s ==", tag);
  96. tor_assert(strlen(contents) <= 32);
  97. memcpy(contents+32, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  98. memcpy(contents+32+CURVE25519_SECKEY_LEN,
  99. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  100. r = write_bytes_to_file(fname, contents, sizeof(contents), 1);
  101. memwipe(contents, 0, sizeof(contents));
  102. return r;
  103. }
  104. int
  105. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  106. char **tag_out,
  107. const char *fname)
  108. {
  109. char prefix[33];
  110. char *content;
  111. struct stat st;
  112. int r = -1;
  113. *tag_out = NULL;
  114. st.st_size = 0;
  115. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  116. if (! content)
  117. goto end;
  118. if (st.st_size != 32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN)
  119. goto end;
  120. memcpy(prefix, content, 32);
  121. prefix[32] = '\0';
  122. if (strcmpstart(prefix, "== c25519v1: ") ||
  123. strcmpend(prefix, " =="))
  124. goto end;
  125. *tag_out = tor_strndup(prefix+strlen("== c25519v1: "),
  126. strlen(prefix) - strlen("== c25519v1: =="));
  127. memcpy(keypair_out->seckey.secret_key, content+32, CURVE25519_SECKEY_LEN);
  128. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  129. if (tor_memneq(keypair_out->pubkey.public_key,
  130. content + 32 + CURVE25519_SECKEY_LEN,
  131. CURVE25519_PUBKEY_LEN))
  132. goto end;
  133. r = 0;
  134. end:
  135. if (content) {
  136. memwipe(content, 0, st.st_size);
  137. tor_free(content);
  138. }
  139. if (r != 0) {
  140. memset(keypair_out, 0, sizeof(*keypair_out));
  141. tor_free(*tag_out);
  142. }
  143. return r;
  144. }
  145. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  146. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  147. void
  148. curve25519_handshake(uint8_t *output,
  149. const curve25519_secret_key_t *skey,
  150. const curve25519_public_key_t *pkey)
  151. {
  152. curve25519_impl(output, skey->secret_key, pkey->public_key);
  153. }