crypto_curve25519.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /** Generate a new keypair and return the secret key. If <b>extra_strong</b>
  59. * is true, this key is possibly going to get used more than once, so
  60. * use a better-than-usual RNG. Return 0 on success, -1 on failure. */
  61. int
  62. curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  63. int extra_strong)
  64. {
  65. uint8_t k_tmp[CURVE25519_SECKEY_LEN];
  66. if (crypto_rand((char*)key_out->secret_key, CURVE25519_SECKEY_LEN) < 0)
  67. return -1;
  68. if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) {
  69. /* If they asked for extra-strong entropy and we have some, use it as an
  70. * HMAC key to improve not-so-good entropy rather than using it directly,
  71. * just in case the extra-strong entropy is less amazing than we hoped. */
  72. crypto_hmac_sha256((char *)key_out->secret_key,
  73. (const char *)k_tmp, sizeof(k_tmp),
  74. (const char *)key_out->secret_key, CURVE25519_SECKEY_LEN);
  75. }
  76. memwipe(k_tmp, 0, sizeof(k_tmp));
  77. key_out->secret_key[0] &= 248;
  78. key_out->secret_key[31] &= 127;
  79. key_out->secret_key[31] |= 64;
  80. return 0;
  81. }
  82. void
  83. curve25519_public_key_generate(curve25519_public_key_t *key_out,
  84. const curve25519_secret_key_t *seckey)
  85. {
  86. static const uint8_t basepoint[32] = {9};
  87. curve25519_impl(key_out->public_key, seckey->secret_key, basepoint);
  88. }
  89. int
  90. curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  91. int extra_strong)
  92. {
  93. if (curve25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  94. return -1;
  95. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  96. return 0;
  97. }
  98. int
  99. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  100. const char *fname,
  101. const char *tag)
  102. {
  103. char contents[32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  104. int r;
  105. memset(contents, 0, sizeof(contents));
  106. tor_snprintf(contents, sizeof(contents), "== c25519v1: %s ==", tag);
  107. tor_assert(strlen(contents) <= 32);
  108. memcpy(contents+32, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  109. memcpy(contents+32+CURVE25519_SECKEY_LEN,
  110. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  111. r = write_bytes_to_file(fname, contents, sizeof(contents), 1);
  112. memwipe(contents, 0, sizeof(contents));
  113. return r;
  114. }
  115. int
  116. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  117. char **tag_out,
  118. const char *fname)
  119. {
  120. char prefix[33];
  121. char *content;
  122. struct stat st;
  123. int r = -1;
  124. *tag_out = NULL;
  125. st.st_size = 0;
  126. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  127. if (! content)
  128. goto end;
  129. if (st.st_size != 32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN)
  130. goto end;
  131. memcpy(prefix, content, 32);
  132. prefix[32] = '\0';
  133. if (strcmpstart(prefix, "== c25519v1: ") ||
  134. strcmpend(prefix, " =="))
  135. goto end;
  136. *tag_out = tor_strndup(prefix+strlen("== c25519v1: "),
  137. strlen(prefix) - strlen("== c25519v1: =="));
  138. memcpy(keypair_out->seckey.secret_key, content+32, CURVE25519_SECKEY_LEN);
  139. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  140. if (tor_memneq(keypair_out->pubkey.public_key,
  141. content + 32 + CURVE25519_SECKEY_LEN,
  142. CURVE25519_PUBKEY_LEN))
  143. goto end;
  144. r = 0;
  145. end:
  146. if (content) {
  147. memwipe(content, 0, (size_t) st.st_size);
  148. tor_free(content);
  149. }
  150. if (r != 0) {
  151. memset(keypair_out, 0, sizeof(*keypair_out));
  152. tor_free(*tag_out);
  153. }
  154. return r;
  155. }
  156. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  157. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  158. void
  159. curve25519_handshake(uint8_t *output,
  160. const curve25519_secret_key_t *skey,
  161. const curve25519_public_key_t *pkey)
  162. {
  163. curve25519_impl(output, skey->secret_key, pkey->public_key);
  164. }