crypto_curve25519.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Copyright (c) 2012-2015, 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 "container.h"
  10. #include "crypto.h"
  11. #include "crypto_curve25519.h"
  12. #include "crypto_format.h"
  13. #include "util.h"
  14. #include "torlog.h"
  15. #include "ed25519/donna/ed25519_donna_tor.h"
  16. /* ==============================
  17. Part 1: wrap a suitable curve25519 implementation as curve25519_impl
  18. ============================== */
  19. #ifdef USE_CURVE25519_DONNA
  20. int curve25519_donna(uint8_t *mypublic,
  21. const uint8_t *secret, const uint8_t *basepoint);
  22. #endif
  23. #ifdef USE_CURVE25519_NACL
  24. #ifdef HAVE_CRYPTO_SCALARMULT_CURVE25519_H
  25. #include <crypto_scalarmult_curve25519.h>
  26. #elif defined(HAVE_NACL_CRYPTO_SCALARMULT_CURVE25519_H)
  27. #include <nacl/crypto_scalarmult_curve25519.h>
  28. #endif
  29. #endif
  30. static void pick_curve25519_basepoint_impl(void);
  31. static int curve25519_use_ed = -1;
  32. STATIC int
  33. curve25519_impl(uint8_t *output, const uint8_t *secret,
  34. const uint8_t *basepoint)
  35. {
  36. uint8_t bp[CURVE25519_PUBKEY_LEN];
  37. int r;
  38. memcpy(bp, basepoint, CURVE25519_PUBKEY_LEN);
  39. /* Clear the high bit, in case our backend foolishly looks at it. */
  40. bp[31] &= 0x7f;
  41. #ifdef USE_CURVE25519_DONNA
  42. r = curve25519_donna(output, secret, bp);
  43. #elif defined(USE_CURVE25519_NACL)
  44. r = crypto_scalarmult_curve25519(output, secret, bp);
  45. #else
  46. #error "No implementation of curve25519 is available."
  47. #endif
  48. memwipe(bp, 0, sizeof(bp));
  49. return r;
  50. }
  51. STATIC int
  52. curve25519_basepoint_impl(uint8_t *output, const uint8_t *secret)
  53. {
  54. int r = 0;
  55. if (PREDICT_UNLIKELY(curve25519_use_ed == -1)) {
  56. pick_curve25519_basepoint_impl();
  57. }
  58. /* TODO: Someone should benchmark curved25519_scalarmult_basepoint versus
  59. * an optimized NaCl build to see which should be used when compiled with
  60. * NaCl available. I suspected that the ed25519 optimization always wins.
  61. */
  62. if (PREDICT_LIKELY(curve25519_use_ed == 1)) {
  63. curved25519_scalarmult_basepoint_donna(output, secret);
  64. r = 0;
  65. } else {
  66. static const uint8_t basepoint[32] = {9};
  67. r = curve25519_impl(output, secret, basepoint);
  68. }
  69. return r;
  70. }
  71. void
  72. curve25519_set_impl_params(int use_ed)
  73. {
  74. curve25519_use_ed = use_ed;
  75. }
  76. /* ==============================
  77. Part 2: Wrap curve25519_impl with some convenience types and functions.
  78. ============================== */
  79. /**
  80. * Return true iff a curve25519_public_key_t seems valid. (It's not necessary
  81. * to see if the point is on the curve, since the twist is also secure, but we
  82. * do need to make sure that it isn't the point at infinity.) */
  83. int
  84. curve25519_public_key_is_ok(const curve25519_public_key_t *key)
  85. {
  86. return !safe_mem_is_zero(key->public_key, CURVE25519_PUBKEY_LEN);
  87. }
  88. /**
  89. * Generate CURVE25519_SECKEY_LEN random bytes in <b>out</b>. If
  90. * <b>extra_strong</b> is true, this key is possibly going to get used more
  91. * than once, so use a better-than-usual RNG. Return 0 on success, -1 on
  92. * failure.
  93. *
  94. * This function does not adjust the output of the RNG at all; the will caller
  95. * will need to clear or set the appropriate bits to make curve25519 work.
  96. */
  97. int
  98. curve25519_rand_seckey_bytes(uint8_t *out, int extra_strong)
  99. {
  100. uint8_t k_tmp[CURVE25519_SECKEY_LEN];
  101. if (crypto_rand((char*)out, CURVE25519_SECKEY_LEN) < 0)
  102. return -1;
  103. if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) {
  104. /* If they asked for extra-strong entropy and we have some, use it as an
  105. * HMAC key to improve not-so-good entropy rather than using it directly,
  106. * just in case the extra-strong entropy is less amazing than we hoped. */
  107. crypto_hmac_sha256((char*) out,
  108. (const char *)k_tmp, sizeof(k_tmp),
  109. (const char *)out, CURVE25519_SECKEY_LEN);
  110. }
  111. memwipe(k_tmp, 0, sizeof(k_tmp));
  112. return 0;
  113. }
  114. /** Generate a new keypair and return the secret key. If <b>extra_strong</b>
  115. * is true, this key is possibly going to get used more than once, so
  116. * use a better-than-usual RNG. Return 0 on success, -1 on failure. */
  117. int
  118. curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  119. int extra_strong)
  120. {
  121. if (curve25519_rand_seckey_bytes(key_out->secret_key, extra_strong) < 0)
  122. return -1;
  123. key_out->secret_key[0] &= 248;
  124. key_out->secret_key[31] &= 127;
  125. key_out->secret_key[31] |= 64;
  126. return 0;
  127. }
  128. void
  129. curve25519_public_key_generate(curve25519_public_key_t *key_out,
  130. const curve25519_secret_key_t *seckey)
  131. {
  132. curve25519_basepoint_impl(key_out->public_key, seckey->secret_key);
  133. }
  134. int
  135. curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  136. int extra_strong)
  137. {
  138. if (curve25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  139. return -1;
  140. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  141. return 0;
  142. }
  143. /** DOCDOC */
  144. int
  145. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  146. const char *fname,
  147. const char *tag)
  148. {
  149. uint8_t contents[CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  150. int r;
  151. memcpy(contents, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  152. memcpy(contents+CURVE25519_SECKEY_LEN,
  153. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  154. r = crypto_write_tagged_contents_to_file(fname,
  155. "c25519v1",
  156. tag,
  157. contents,
  158. sizeof(contents));
  159. memwipe(contents, 0, sizeof(contents));
  160. return r;
  161. }
  162. /** DOCDOC */
  163. int
  164. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  165. char **tag_out,
  166. const char *fname)
  167. {
  168. uint8_t content[CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  169. ssize_t len;
  170. int r = -1;
  171. len = crypto_read_tagged_contents_from_file(fname, "c25519v1", tag_out,
  172. content, sizeof(content));
  173. if (len != sizeof(content))
  174. goto end;
  175. memcpy(keypair_out->seckey.secret_key, content, CURVE25519_SECKEY_LEN);
  176. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  177. if (tor_memneq(keypair_out->pubkey.public_key,
  178. content + CURVE25519_SECKEY_LEN,
  179. CURVE25519_PUBKEY_LEN))
  180. goto end;
  181. r = 0;
  182. end:
  183. memwipe(content, 0, sizeof(content));
  184. if (r != 0) {
  185. memset(keypair_out, 0, sizeof(*keypair_out));
  186. tor_free(*tag_out);
  187. }
  188. return r;
  189. }
  190. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  191. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  192. void
  193. curve25519_handshake(uint8_t *output,
  194. const curve25519_secret_key_t *skey,
  195. const curve25519_public_key_t *pkey)
  196. {
  197. curve25519_impl(output, skey->secret_key, pkey->public_key);
  198. }
  199. /** Check whether the ed25519-based curve25519 basepoint optimization seems to
  200. * be working. If so, return 0; otherwise return -1. */
  201. static int
  202. curve25519_basepoint_spot_check(void)
  203. {
  204. static const uint8_t alicesk[32] = {
  205. 0x77,0x07,0x6d,0x0a,0x73,0x18,0xa5,0x7d,
  206. 0x3c,0x16,0xc1,0x72,0x51,0xb2,0x66,0x45,
  207. 0xdf,0x4c,0x2f,0x87,0xeb,0xc0,0x99,0x2a,
  208. 0xb1,0x77,0xfb,0xa5,0x1d,0xb9,0x2c,0x2a
  209. };
  210. static const uint8_t alicepk[32] = {
  211. 0x85,0x20,0xf0,0x09,0x89,0x30,0xa7,0x54,
  212. 0x74,0x8b,0x7d,0xdc,0xb4,0x3e,0xf7,0x5a,
  213. 0x0d,0xbf,0x3a,0x0d,0x26,0x38,0x1a,0xf4,
  214. 0xeb,0xa4,0xa9,0x8e,0xaa,0x9b,0x4e,0x6a
  215. };
  216. const int loop_max=200;
  217. int save_use_ed = curve25519_use_ed;
  218. unsigned char e1[32] = { 5 };
  219. unsigned char e2[32] = { 5 };
  220. unsigned char x[32],y[32];
  221. int i;
  222. int r=0;
  223. /* Check the most basic possible sanity via the test secret/public key pair
  224. * used in "Cryptography in NaCl - 2. Secret keys and public keys". This
  225. * may catch catastrophic failures on systems where Curve25519 is expensive,
  226. * without requiring a ton of key generation.
  227. */
  228. curve25519_use_ed = 1;
  229. r |= curve25519_basepoint_impl(x, alicesk);
  230. if (fast_memneq(x, alicepk, 32))
  231. goto fail;
  232. /* Ok, the optimization appears to produce passable results, try a few more
  233. * values, maybe there's something subtle wrong.
  234. */
  235. for (i = 0; i < loop_max; ++i) {
  236. curve25519_use_ed = 0;
  237. r |= curve25519_basepoint_impl(x, e1);
  238. curve25519_use_ed = 1;
  239. r |= curve25519_basepoint_impl(y, e2);
  240. if (fast_memneq(x,y,32))
  241. goto fail;
  242. memcpy(e1, x, 32);
  243. memcpy(e2, x, 32);
  244. }
  245. goto end;
  246. fail:
  247. r = -1;
  248. end:
  249. curve25519_use_ed = save_use_ed;
  250. return r;
  251. }
  252. /** Choose whether to use the ed25519-based curve25519-basepoint
  253. * implementation. */
  254. static void
  255. pick_curve25519_basepoint_impl(void)
  256. {
  257. curve25519_use_ed = 1;
  258. if (curve25519_basepoint_spot_check() == 0)
  259. return;
  260. log_warn(LD_CRYPTO, "The ed25519-based curve25519 basepoint "
  261. "multiplication seems broken; using the curve25519 "
  262. "implementation.");
  263. curve25519_use_ed = 0;
  264. }
  265. /** Initialize the curve25519 implementations. This is necessary if you're
  266. * going to use them in a multithreaded setting, and not otherwise. */
  267. void
  268. curve25519_init(void)
  269. {
  270. pick_curve25519_basepoint_impl();
  271. }