crypto_curve25519.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Copyright (c) 2012-2013, 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. #ifdef HAVE_CRYPTO_SCALARMULT_CURVE25519_H
  22. #include <crypto_scalarmult_curve25519.h>
  23. #elif defined(HAVE_NACL_CRYPTO_SCALARMULT_CURVE25519_H)
  24. #include <nacl/crypto_scalarmult_curve25519.h>
  25. #endif
  26. #endif
  27. STATIC int
  28. curve25519_impl(uint8_t *output, const uint8_t *secret,
  29. const uint8_t *basepoint)
  30. {
  31. uint8_t bp[CURVE25519_PUBKEY_LEN];
  32. int r;
  33. memcpy(bp, basepoint, CURVE25519_PUBKEY_LEN);
  34. /* Clear the high bit, in case our backend foolishly looks at it. */
  35. bp[31] &= 0x7f;
  36. #ifdef USE_CURVE25519_DONNA
  37. r = curve25519_donna(output, secret, bp);
  38. #elif defined(USE_CURVE25519_NACL)
  39. r = crypto_scalarmult_curve25519(output, secret, bp);
  40. #else
  41. #error "No implementation of curve25519 is available."
  42. #endif
  43. memwipe(bp, 0, sizeof(bp));
  44. return r;
  45. }
  46. /* ==============================
  47. Part 2: Wrap curve25519_impl with some convenience types and functions.
  48. ============================== */
  49. /**
  50. * Return true iff a curve25519_public_key_t seems valid. (It's not necessary
  51. * to see if the point is on the curve, since the twist is also secure, but we
  52. * do need to make sure that it isn't the point at infinity.) */
  53. int
  54. curve25519_public_key_is_ok(const curve25519_public_key_t *key)
  55. {
  56. return !safe_mem_is_zero(key->public_key, CURVE25519_PUBKEY_LEN);
  57. }
  58. /**
  59. * Generate CURVE25519_SECKEY_LEN random bytes in <b>out</b>. If
  60. * <b>extra_strong</b> is true, this key is possibly going to get used more
  61. * than once, so use a better-than-usual RNG. Return 0 on success, -1 on
  62. * failure.
  63. *
  64. * This function does not adjust the output of the RNG at all; the will caller
  65. * will need to clear or set the appropriate bits to make curve25519 work.
  66. */
  67. int
  68. curve25519_rand_seckey_bytes(uint8_t *out, int extra_strong)
  69. {
  70. uint8_t k_tmp[CURVE25519_SECKEY_LEN];
  71. if (crypto_rand((char*)out, CURVE25519_SECKEY_LEN) < 0)
  72. return -1;
  73. if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) {
  74. /* If they asked for extra-strong entropy and we have some, use it as an
  75. * HMAC key to improve not-so-good entropy rather than using it directly,
  76. * just in case the extra-strong entropy is less amazing than we hoped. */
  77. crypto_hmac_sha256((char*) out,
  78. (const char *)k_tmp, sizeof(k_tmp),
  79. (const char *)out, CURVE25519_SECKEY_LEN);
  80. }
  81. memwipe(k_tmp, 0, sizeof(k_tmp));
  82. return 0;
  83. }
  84. /** Generate a new keypair and return the secret key. If <b>extra_strong</b>
  85. * is true, this key is possibly going to get used more than once, so
  86. * use a better-than-usual RNG. Return 0 on success, -1 on failure. */
  87. int
  88. curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  89. int extra_strong)
  90. {
  91. if (curve25519_rand_seckey_bytes(key_out->secret_key, extra_strong) < 0)
  92. return -1;
  93. key_out->secret_key[0] &= 248;
  94. key_out->secret_key[31] &= 127;
  95. key_out->secret_key[31] |= 64;
  96. return 0;
  97. }
  98. void
  99. curve25519_public_key_generate(curve25519_public_key_t *key_out,
  100. const curve25519_secret_key_t *seckey)
  101. {
  102. static const uint8_t basepoint[32] = {9};
  103. curve25519_impl(key_out->public_key, seckey->secret_key, basepoint);
  104. }
  105. int
  106. curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  107. int extra_strong)
  108. {
  109. if (curve25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  110. return -1;
  111. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  112. return 0;
  113. }
  114. int
  115. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  116. const char *fname,
  117. const char *tag)
  118. {
  119. char contents[32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  120. int r;
  121. memset(contents, 0, sizeof(contents));
  122. tor_snprintf(contents, sizeof(contents), "== c25519v1: %s ==", tag);
  123. tor_assert(strlen(contents) <= 32);
  124. memcpy(contents+32, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  125. memcpy(contents+32+CURVE25519_SECKEY_LEN,
  126. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  127. r = write_bytes_to_file(fname, contents, sizeof(contents), 1);
  128. memwipe(contents, 0, sizeof(contents));
  129. return r;
  130. }
  131. int
  132. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  133. char **tag_out,
  134. const char *fname)
  135. {
  136. char prefix[33];
  137. char *content;
  138. struct stat st;
  139. int r = -1;
  140. *tag_out = NULL;
  141. st.st_size = 0;
  142. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  143. if (! content)
  144. goto end;
  145. if (st.st_size != 32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN)
  146. goto end;
  147. memcpy(prefix, content, 32);
  148. prefix[32] = '\0';
  149. if (strcmpstart(prefix, "== c25519v1: ") ||
  150. strcmpend(prefix, " =="))
  151. goto end;
  152. *tag_out = tor_strndup(prefix+strlen("== c25519v1: "),
  153. strlen(prefix) - strlen("== c25519v1: =="));
  154. memcpy(keypair_out->seckey.secret_key, content+32, CURVE25519_SECKEY_LEN);
  155. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  156. if (tor_memneq(keypair_out->pubkey.public_key,
  157. content + 32 + CURVE25519_SECKEY_LEN,
  158. CURVE25519_PUBKEY_LEN))
  159. goto end;
  160. r = 0;
  161. end:
  162. if (content) {
  163. memwipe(content, 0, (size_t) st.st_size);
  164. tor_free(content);
  165. }
  166. if (r != 0) {
  167. memset(keypair_out, 0, sizeof(*keypair_out));
  168. tor_free(*tag_out);
  169. }
  170. return r;
  171. }
  172. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  173. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  174. void
  175. curve25519_handshake(uint8_t *output,
  176. const curve25519_secret_key_t *skey,
  177. const curve25519_public_key_t *pkey)
  178. {
  179. curve25519_impl(output, skey->secret_key, pkey->public_key);
  180. }