crypto_curve25519.c 6.5 KB

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