crypto_ed25519.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Copyright (c) 2013-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file crypto_ed25519.c
  5. *
  6. * \brief Wrapper code for an ed25519 implementation.
  7. *
  8. * Ed25519 is a Schnorr signature on a Twisted Edwards curve, defined
  9. * by Dan Bernstein. For more information, see https://ed25519.cr.yp.to/
  10. *
  11. * This module wraps our choice of Ed25519 backend, and provides a few
  12. * convenience functions for checking and generating signatures. It also
  13. * provides Tor-specific tools for key blinding and for converting Ed25519
  14. * keys to and from the corresponding Curve25519 keys.
  15. */
  16. #define CRYPTO_ED25519_PRIVATE
  17. #include "orconfig.h"
  18. #ifdef HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #include "common/crypto_curve25519.h"
  22. #include "common/crypto_digest.h"
  23. #include "common/crypto_ed25519.h"
  24. #include "common/crypto_format.h"
  25. #include "common/crypto_rand.h"
  26. #include "common/crypto_util.h"
  27. #include "common/torlog.h"
  28. #include "common/util.h"
  29. #include "common/util_format.h"
  30. #include "ed25519/ref10/ed25519_ref10.h"
  31. #include "ed25519/donna/ed25519_donna_tor.h"
  32. static void pick_ed25519_impl(void);
  33. /** An Ed25519 implementation, as a set of function pointers. */
  34. typedef struct {
  35. int (*selftest)(void);
  36. int (*seckey)(unsigned char *);
  37. int (*seckey_expand)(unsigned char *, const unsigned char *);
  38. int (*pubkey)(unsigned char *, const unsigned char *);
  39. int (*keygen)(unsigned char *, unsigned char *);
  40. int (*open)(const unsigned char *, const unsigned char *, size_t, const
  41. unsigned char *);
  42. int (*sign)(unsigned char *, const unsigned char *, size_t,
  43. const unsigned char *, const unsigned char *);
  44. int (*open_batch)(const unsigned char **, size_t *, const unsigned char **,
  45. const unsigned char **, size_t, int *);
  46. int (*blind_secret_key)(unsigned char *, const unsigned char *,
  47. const unsigned char *);
  48. int (*blind_public_key)(unsigned char *, const unsigned char *,
  49. const unsigned char *);
  50. int (*pubkey_from_curve25519_pubkey)(unsigned char *, const unsigned char *,
  51. int);
  52. int (*ed25519_scalarmult_with_group_order)(unsigned char *,
  53. const unsigned char *);
  54. } ed25519_impl_t;
  55. /** The Ref10 Ed25519 implementation. This one is pure C and lightly
  56. * optimized. */
  57. static const ed25519_impl_t impl_ref10 = {
  58. NULL,
  59. ed25519_ref10_seckey,
  60. ed25519_ref10_seckey_expand,
  61. ed25519_ref10_pubkey,
  62. ed25519_ref10_keygen,
  63. ed25519_ref10_open,
  64. ed25519_ref10_sign,
  65. NULL,
  66. ed25519_ref10_blind_secret_key,
  67. ed25519_ref10_blind_public_key,
  68. ed25519_ref10_pubkey_from_curve25519_pubkey,
  69. ed25519_ref10_scalarmult_with_group_order,
  70. };
  71. /** The Ref10 Ed25519 implementation. This one is heavily optimized, but still
  72. * mostly C. The C still tends to be heavily platform-specific. */
  73. static const ed25519_impl_t impl_donna = {
  74. ed25519_donna_selftest,
  75. ed25519_donna_seckey,
  76. ed25519_donna_seckey_expand,
  77. ed25519_donna_pubkey,
  78. ed25519_donna_keygen,
  79. ed25519_donna_open,
  80. ed25519_donna_sign,
  81. ed25519_sign_open_batch_donna,
  82. ed25519_donna_blind_secret_key,
  83. ed25519_donna_blind_public_key,
  84. ed25519_donna_pubkey_from_curve25519_pubkey,
  85. ed25519_donna_scalarmult_with_group_order,
  86. };
  87. /** Which Ed25519 implementation are we using? NULL if we haven't decided
  88. * yet. */
  89. static const ed25519_impl_t *ed25519_impl = NULL;
  90. /** Helper: Return our chosen Ed25519 implementation.
  91. *
  92. * This should only be called after we've picked an implementation, but
  93. * it _does_ recover if you forget this.
  94. **/
  95. static inline const ed25519_impl_t *
  96. get_ed_impl(void)
  97. {
  98. if (BUG(ed25519_impl == NULL)) {
  99. pick_ed25519_impl(); // LCOV_EXCL_LINE - We always call ed25519_init().
  100. }
  101. return ed25519_impl;
  102. }
  103. #ifdef TOR_UNIT_TESTS
  104. /** For testing: used to remember our actual choice of Ed25519
  105. * implementation */
  106. static const ed25519_impl_t *saved_ed25519_impl = NULL;
  107. /** For testing: Use the Ed25519 implementation called <b>name</b> until
  108. * crypto_ed25519_testing_restore_impl is called. Recognized names are
  109. * "donna" and "ref10". */
  110. void
  111. crypto_ed25519_testing_force_impl(const char *name)
  112. {
  113. tor_assert(saved_ed25519_impl == NULL);
  114. saved_ed25519_impl = ed25519_impl;
  115. if (! strcmp(name, "donna")) {
  116. ed25519_impl = &impl_donna;
  117. } else {
  118. tor_assert(!strcmp(name, "ref10"));
  119. ed25519_impl = &impl_ref10;
  120. }
  121. }
  122. /** For testing: go back to whatever Ed25519 implementation we had picked
  123. * before crypto_ed25519_testing_force_impl was called.
  124. */
  125. void
  126. crypto_ed25519_testing_restore_impl(void)
  127. {
  128. ed25519_impl = saved_ed25519_impl;
  129. saved_ed25519_impl = NULL;
  130. }
  131. #endif /* defined(TOR_UNIT_TESTS) */
  132. /**
  133. * Initialize a new ed25519 secret key in <b>seckey_out</b>. If
  134. * <b>extra_strong</b>, take the RNG inputs directly from the operating
  135. * system. Return 0 on success, -1 on failure.
  136. */
  137. int
  138. ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
  139. int extra_strong)
  140. {
  141. int r;
  142. uint8_t seed[32];
  143. if (extra_strong)
  144. crypto_strongest_rand(seed, sizeof(seed));
  145. else
  146. crypto_rand((char*)seed, sizeof(seed));
  147. r = get_ed_impl()->seckey_expand(seckey_out->seckey, seed);
  148. memwipe(seed, 0, sizeof(seed));
  149. return r < 0 ? -1 : 0;
  150. }
  151. /**
  152. * Given a 32-byte random seed in <b>seed</b>, expand it into an ed25519
  153. * secret key in <b>seckey_out</b>. Return 0 on success, -1 on failure.
  154. */
  155. int
  156. ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
  157. const uint8_t *seed)
  158. {
  159. if (get_ed_impl()->seckey_expand(seckey_out->seckey, seed) < 0)
  160. return -1;
  161. return 0;
  162. }
  163. /**
  164. * Given a secret key in <b>seckey</b>, expand it into an
  165. * ed25519 public key. Return 0 on success, -1 on failure.
  166. */
  167. int
  168. ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
  169. const ed25519_secret_key_t *seckey)
  170. {
  171. if (get_ed_impl()->pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
  172. return -1;
  173. return 0;
  174. }
  175. /** Generate a new ed25519 keypair in <b>keypair_out</b>. If
  176. * <b>extra_strong</b> is set, try to mix some system entropy into the key
  177. * generation process. Return 0 on success, -1 on failure. */
  178. int
  179. ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
  180. {
  181. if (ed25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  182. return -1;
  183. if (ed25519_public_key_generate(&keypair_out->pubkey,
  184. &keypair_out->seckey)<0)
  185. return -1;
  186. return 0;
  187. }
  188. /** Return true iff 'pubkey' is set to zero (eg to indicate that it is not
  189. * set). */
  190. int
  191. ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
  192. {
  193. return tor_mem_is_zero((char*)pubkey->pubkey, ED25519_PUBKEY_LEN);
  194. }
  195. /* Return a heap-allocated array that contains <b>msg</b> prefixed by the
  196. * string <b>prefix_str</b>. Set <b>final_msg_len_out</b> to the size of the
  197. * final array. If an error occurred, return NULL. It's the responsibility of
  198. * the caller to free the returned array. */
  199. static uint8_t *
  200. get_prefixed_msg(const uint8_t *msg, size_t msg_len,
  201. const char *prefix_str,
  202. size_t *final_msg_len_out)
  203. {
  204. size_t prefixed_msg_len, prefix_len;
  205. uint8_t *prefixed_msg;
  206. tor_assert(prefix_str);
  207. tor_assert(final_msg_len_out);
  208. prefix_len = strlen(prefix_str);
  209. /* msg_len + strlen(prefix_str) must not overflow. */
  210. if (msg_len > SIZE_T_CEILING - prefix_len) {
  211. return NULL;
  212. }
  213. prefixed_msg_len = msg_len + prefix_len;
  214. prefixed_msg = tor_malloc_zero(prefixed_msg_len);
  215. memcpy(prefixed_msg, prefix_str, prefix_len);
  216. memcpy(prefixed_msg + prefix_len, msg, msg_len);
  217. *final_msg_len_out = prefixed_msg_len;
  218. return prefixed_msg;
  219. }
  220. /**
  221. * Set <b>signature_out</b> to a signature of the <b>len</b>-byte message
  222. * <b>msg</b>, using the secret and public key in <b>keypair</b>.
  223. *
  224. * Return 0 if we successfully signed the message, otherwise return -1.
  225. */
  226. int
  227. ed25519_sign(ed25519_signature_t *signature_out,
  228. const uint8_t *msg, size_t len,
  229. const ed25519_keypair_t *keypair)
  230. {
  231. if (get_ed_impl()->sign(signature_out->sig, msg, len,
  232. keypair->seckey.seckey,
  233. keypair->pubkey.pubkey) < 0) {
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. /**
  239. * Like ed25519_sign(), but also prefix <b>msg</b> with <b>prefix_str</b>
  240. * before signing. <b>prefix_str</b> must be a NUL-terminated string.
  241. */
  242. MOCK_IMPL(int,
  243. ed25519_sign_prefixed,(ed25519_signature_t *signature_out,
  244. const uint8_t *msg, size_t msg_len,
  245. const char *prefix_str,
  246. const ed25519_keypair_t *keypair))
  247. {
  248. int retval;
  249. size_t prefixed_msg_len;
  250. uint8_t *prefixed_msg;
  251. tor_assert(prefix_str);
  252. prefixed_msg = get_prefixed_msg(msg, msg_len, prefix_str,
  253. &prefixed_msg_len);
  254. if (BUG(!prefixed_msg)) {
  255. /* LCOV_EXCL_START -- only possible when the message and prefix are
  256. * ridiculously huge */
  257. log_warn(LD_GENERAL, "Failed to get prefixed msg.");
  258. return -1;
  259. /* LCOV_EXCL_STOP */
  260. }
  261. retval = ed25519_sign(signature_out,
  262. prefixed_msg, prefixed_msg_len,
  263. keypair);
  264. tor_free(prefixed_msg);
  265. return retval;
  266. }
  267. /**
  268. * Check whether if <b>signature</b> is a valid signature for the
  269. * <b>len</b>-byte message in <b>msg</b> made with the key <b>pubkey</b>.
  270. *
  271. * Return 0 if the signature is valid; -1 if it isn't.
  272. */
  273. MOCK_IMPL(int,
  274. ed25519_checksig,(const ed25519_signature_t *signature,
  275. const uint8_t *msg, size_t len,
  276. const ed25519_public_key_t *pubkey))
  277. {
  278. return
  279. get_ed_impl()->open(signature->sig, msg, len, pubkey->pubkey) < 0 ? -1 : 0;
  280. }
  281. /**
  282. * Like ed2519_checksig(), but also prefix <b>msg</b> with <b>prefix_str</b>
  283. * before verifying signature. <b>prefix_str</b> must be a NUL-terminated
  284. * string.
  285. */
  286. int
  287. ed25519_checksig_prefixed(const ed25519_signature_t *signature,
  288. const uint8_t *msg, size_t msg_len,
  289. const char *prefix_str,
  290. const ed25519_public_key_t *pubkey)
  291. {
  292. int retval;
  293. size_t prefixed_msg_len;
  294. uint8_t *prefixed_msg;
  295. prefixed_msg = get_prefixed_msg(msg, msg_len, prefix_str,
  296. &prefixed_msg_len);
  297. if (BUG(!prefixed_msg)) {
  298. /* LCOV_EXCL_START -- only possible when the message and prefix are
  299. * ridiculously huge */
  300. log_warn(LD_GENERAL, "Failed to get prefixed msg.");
  301. return -1;
  302. /* LCOV_EXCL_STOP */
  303. }
  304. retval = ed25519_checksig(signature,
  305. prefixed_msg, prefixed_msg_len,
  306. pubkey);
  307. tor_free(prefixed_msg);
  308. return retval;
  309. }
  310. /** Validate every signature among those in <b>checkable</b>, which contains
  311. * exactly <b>n_checkable</b> elements. If <b>okay_out</b> is non-NULL, set
  312. * the i'th element of <b>okay_out</b> to 1 if the i'th element of
  313. * <b>checkable</b> is valid, and to 0 otherwise. Return 0 if every signature
  314. * was valid. Otherwise return -N, where N is the number of invalid
  315. * signatures.
  316. */
  317. MOCK_IMPL(int,
  318. ed25519_checksig_batch,(int *okay_out,
  319. const ed25519_checkable_t *checkable,
  320. int n_checkable))
  321. {
  322. int i, res;
  323. const ed25519_impl_t *impl = get_ed_impl();
  324. if (impl->open_batch == NULL) {
  325. /* No batch verification implementation available, fake it by checking the
  326. * each signature individually.
  327. */
  328. res = 0;
  329. for (i = 0; i < n_checkable; ++i) {
  330. const ed25519_checkable_t *ch = &checkable[i];
  331. int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
  332. if (r < 0)
  333. --res;
  334. if (okay_out)
  335. okay_out[i] = (r == 0);
  336. }
  337. } else {
  338. /* ed25519-donna style batch verification available.
  339. *
  340. * Theoretically, this should only be called if n_checkable >= 3, since
  341. * that's the threshold where the batch verification actually kicks in,
  342. * but the only difference is a few mallocs/frees.
  343. */
  344. const uint8_t **ms;
  345. size_t *lens;
  346. const uint8_t **pks;
  347. const uint8_t **sigs;
  348. int *oks;
  349. int all_ok;
  350. ms = tor_calloc(n_checkable, sizeof(uint8_t*));
  351. lens = tor_calloc(n_checkable, sizeof(size_t));
  352. pks = tor_calloc(n_checkable, sizeof(uint8_t*));
  353. sigs = tor_calloc(n_checkable, sizeof(uint8_t*));
  354. oks = okay_out ? okay_out : tor_calloc(n_checkable, sizeof(int));
  355. for (i = 0; i < n_checkable; ++i) {
  356. ms[i] = checkable[i].msg;
  357. lens[i] = checkable[i].len;
  358. pks[i] = checkable[i].pubkey->pubkey;
  359. sigs[i] = checkable[i].signature.sig;
  360. oks[i] = 0;
  361. }
  362. res = 0;
  363. all_ok = impl->open_batch(ms, lens, pks, sigs, n_checkable, oks);
  364. for (i = 0; i < n_checkable; ++i) {
  365. if (!oks[i])
  366. --res;
  367. }
  368. /* XXX: For now sanity check oks with the return value. Once we have
  369. * more confidence in the code, if `all_ok == 0` we can skip iterating
  370. * over oks since all the signatures were found to be valid.
  371. */
  372. tor_assert(((res == 0) && !all_ok) || ((res < 0) && all_ok));
  373. tor_free(ms);
  374. tor_free(lens);
  375. tor_free(pks);
  376. tor_free(sigs);
  377. if (! okay_out)
  378. tor_free(oks);
  379. }
  380. return res;
  381. }
  382. /**
  383. * Given a curve25519 keypair in <b>inp</b>, generate a corresponding
  384. * ed25519 keypair in <b>out</b>, and set <b>signbit_out</b> to the
  385. * sign bit of the X coordinate of the ed25519 key.
  386. *
  387. * NOTE THAT IT IS PROBABLY NOT SAFE TO USE THE GENERATED KEY FOR ANYTHING
  388. * OUTSIDE OF WHAT'S PRESENTED IN PROPOSAL 228. In particular, it's probably
  389. * not a great idea to use it to sign attacker-supplied anything.
  390. */
  391. int
  392. ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
  393. int *signbit_out,
  394. const curve25519_keypair_t *inp)
  395. {
  396. const char string[] = "Derive high part of ed25519 key from curve25519 key";
  397. ed25519_public_key_t pubkey_check;
  398. crypto_digest_t *ctx;
  399. uint8_t sha512_output[DIGEST512_LEN];
  400. memcpy(out->seckey.seckey, inp->seckey.secret_key, 32);
  401. ctx = crypto_digest512_new(DIGEST_SHA512);
  402. crypto_digest_add_bytes(ctx, (const char*)out->seckey.seckey, 32);
  403. crypto_digest_add_bytes(ctx, (const char*)string, sizeof(string));
  404. crypto_digest_get_digest(ctx, (char *)sha512_output, sizeof(sha512_output));
  405. crypto_digest_free(ctx);
  406. memcpy(out->seckey.seckey + 32, sha512_output, 32);
  407. ed25519_public_key_generate(&out->pubkey, &out->seckey);
  408. *signbit_out = out->pubkey.pubkey[31] >> 7;
  409. ed25519_public_key_from_curve25519_public_key(&pubkey_check, &inp->pubkey,
  410. *signbit_out);
  411. tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
  412. memwipe(&pubkey_check, 0, sizeof(pubkey_check));
  413. memwipe(sha512_output, 0, sizeof(sha512_output));
  414. return 0;
  415. }
  416. /**
  417. * Given a curve25519 public key and sign bit of X coordinate of the ed25519
  418. * public key, generate the corresponding ed25519 public key.
  419. */
  420. int
  421. ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey,
  422. const curve25519_public_key_t *pubkey_in,
  423. int signbit)
  424. {
  425. return get_ed_impl()->pubkey_from_curve25519_pubkey(pubkey->pubkey,
  426. pubkey_in->public_key,
  427. signbit);
  428. }
  429. /**
  430. * Given an ed25519 keypair in <b>inp</b>, generate a corresponding
  431. * ed25519 keypair in <b>out</b>, blinded by the corresponding 32-byte input
  432. * in 'param'.
  433. *
  434. * Tor uses key blinding for the "next-generation" hidden services design:
  435. * service descriptors are encrypted with a key derived from the service's
  436. * long-term public key, and then signed with (and stored at a position
  437. * indexed by) a short-term key derived by blinding the long-term keys.
  438. *
  439. * Return 0 if blinding was successful, else return -1. */
  440. int
  441. ed25519_keypair_blind(ed25519_keypair_t *out,
  442. const ed25519_keypair_t *inp,
  443. const uint8_t *param)
  444. {
  445. ed25519_public_key_t pubkey_check;
  446. get_ed_impl()->blind_secret_key(out->seckey.seckey,
  447. inp->seckey.seckey, param);
  448. if (ed25519_public_blind(&pubkey_check, &inp->pubkey, param) < 0) {
  449. return -1;
  450. }
  451. ed25519_public_key_generate(&out->pubkey, &out->seckey);
  452. tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
  453. memwipe(&pubkey_check, 0, sizeof(pubkey_check));
  454. return 0;
  455. }
  456. /**
  457. * Given an ed25519 public key in <b>inp</b>, generate a corresponding blinded
  458. * public key in <b>out</b>, blinded with the 32-byte parameter in
  459. * <b>param</b>. Return 0 on success, -1 on railure.
  460. */
  461. int
  462. ed25519_public_blind(ed25519_public_key_t *out,
  463. const ed25519_public_key_t *inp,
  464. const uint8_t *param)
  465. {
  466. return get_ed_impl()->blind_public_key(out->pubkey, inp->pubkey, param);
  467. }
  468. /**
  469. * Store seckey unencrypted to <b>filename</b>, marking it with <b>tag</b>.
  470. * Return 0 on success, -1 on failure.
  471. */
  472. int
  473. ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey,
  474. const char *filename,
  475. const char *tag)
  476. {
  477. return crypto_write_tagged_contents_to_file(filename,
  478. "ed25519v1-secret",
  479. tag,
  480. seckey->seckey,
  481. sizeof(seckey->seckey));
  482. }
  483. /**
  484. * Read seckey unencrypted from <b>filename</b>, storing it into
  485. * <b>seckey_out</b>. Set *<b>tag_out</b> to the tag it was marked with.
  486. * Return 0 on success, -1 on failure.
  487. */
  488. int
  489. ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out,
  490. char **tag_out,
  491. const char *filename)
  492. {
  493. ssize_t len;
  494. len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-secret",
  495. tag_out, seckey_out->seckey,
  496. sizeof(seckey_out->seckey));
  497. if (len == sizeof(seckey_out->seckey)) {
  498. return 0;
  499. } else if (len >= 0) {
  500. errno = EINVAL;
  501. }
  502. tor_free(*tag_out);
  503. return -1;
  504. }
  505. /**
  506. * Store pubkey unencrypted to <b>filename</b>, marking it with <b>tag</b>.
  507. * Return 0 on success, -1 on failure.
  508. */
  509. int
  510. ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey,
  511. const char *filename,
  512. const char *tag)
  513. {
  514. return crypto_write_tagged_contents_to_file(filename,
  515. "ed25519v1-public",
  516. tag,
  517. pubkey->pubkey,
  518. sizeof(pubkey->pubkey));
  519. }
  520. /**
  521. * Store pubkey unencrypted to <b>filename</b>, marking it with <b>tag</b>.
  522. * Return 0 on success, -1 on failure.
  523. */
  524. int
  525. ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
  526. char **tag_out,
  527. const char *filename)
  528. {
  529. ssize_t len;
  530. len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-public",
  531. tag_out, pubkey_out->pubkey,
  532. sizeof(pubkey_out->pubkey));
  533. if (len == sizeof(pubkey_out->pubkey)) {
  534. return 0;
  535. } else if (len >= 0) {
  536. errno = EINVAL;
  537. }
  538. tor_free(*tag_out);
  539. return -1;
  540. }
  541. /** Release all storage held for <b>kp</b>. */
  542. void
  543. ed25519_keypair_free_(ed25519_keypair_t *kp)
  544. {
  545. if (! kp)
  546. return;
  547. memwipe(kp, 0, sizeof(*kp));
  548. tor_free(kp);
  549. }
  550. /** Return true iff <b>key1</b> and <b>key2</b> are the same public key. */
  551. int
  552. ed25519_pubkey_eq(const ed25519_public_key_t *key1,
  553. const ed25519_public_key_t *key2)
  554. {
  555. tor_assert(key1);
  556. tor_assert(key2);
  557. return tor_memeq(key1->pubkey, key2->pubkey, ED25519_PUBKEY_LEN);
  558. }
  559. /**
  560. * Set <b>dest</b> to contain the same key as <b>src</b>.
  561. */
  562. void
  563. ed25519_pubkey_copy(ed25519_public_key_t *dest,
  564. const ed25519_public_key_t *src)
  565. {
  566. tor_assert(dest);
  567. tor_assert(src);
  568. memcpy(dest, src, sizeof(ed25519_public_key_t));
  569. }
  570. /** Check whether the given Ed25519 implementation seems to be working.
  571. * If so, return 0; otherwise return -1. */
  572. MOCK_IMPL(STATIC int,
  573. ed25519_impl_spot_check,(void))
  574. {
  575. static const uint8_t alicesk[32] = {
  576. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  577. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  578. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  579. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  580. };
  581. static const uint8_t alicepk[32] = {
  582. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  583. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  584. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  585. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  586. };
  587. static const uint8_t alicemsg[2] = { 0xaf, 0x82 };
  588. static const uint8_t alicesig[64] = {
  589. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  590. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  591. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  592. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  593. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  594. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  595. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  596. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  597. };
  598. const ed25519_impl_t *impl = get_ed_impl();
  599. uint8_t sk[ED25519_SECKEY_LEN];
  600. uint8_t pk[ED25519_PUBKEY_LEN];
  601. uint8_t sig[ED25519_SIG_LEN];
  602. int r = 0;
  603. /* Some implementations (eg: The modified Ed25519-donna) have handy self-test
  604. * code that sanity-checks the internals. If present, use that to screen out
  605. * catastrophic errors like massive compiler failure.
  606. */
  607. if (impl->selftest && impl->selftest() != 0)
  608. goto fail;
  609. /* Validate results versus known answer tests. People really should be
  610. * running "make test" instead of relying on this, but it's better than
  611. * nothing.
  612. *
  613. * Test vectors taken from "EdDSA & Ed25519 - 6. Test Vectors for Ed25519
  614. * (TEST3)" (draft-josefsson-eddsa-ed25519-03).
  615. */
  616. /* Key expansion, public key derivation. */
  617. if (impl->seckey_expand(sk, alicesk) < 0)
  618. goto fail;
  619. if (impl->pubkey(pk, sk) < 0)
  620. goto fail;
  621. if (fast_memneq(pk, alicepk, ED25519_PUBKEY_LEN))
  622. goto fail;
  623. /* Signing, verification. */
  624. if (impl->sign(sig, alicemsg, sizeof(alicemsg), sk, pk) < 0)
  625. return -1;
  626. if (fast_memneq(sig, alicesig, ED25519_SIG_LEN))
  627. return -1;
  628. if (impl->open(sig, alicemsg, sizeof(alicemsg), pk) < 0)
  629. return -1;
  630. /* XXX/yawning: Someone that's more paranoid than I am, can write "Assume
  631. * ref0 is canonical, and fuzz impl against it" if they want, but I doubt
  632. * that will catch anything that the known answer tests won't.
  633. */
  634. goto end;
  635. // LCOV_EXCL_START -- We can only reach this if our ed25519 implementation is
  636. // broken.
  637. fail:
  638. r = -1;
  639. // LCOV_EXCL_STOP
  640. end:
  641. return r;
  642. }
  643. /** Force the Ed25519 implementation to a given one, without sanity checking
  644. * the output. Used for testing.
  645. */
  646. void
  647. ed25519_set_impl_params(int use_donna)
  648. {
  649. if (use_donna)
  650. ed25519_impl = &impl_donna;
  651. else
  652. ed25519_impl = &impl_ref10;
  653. }
  654. /** Choose whether to use the Ed25519-donna implementation. */
  655. static void
  656. pick_ed25519_impl(void)
  657. {
  658. ed25519_impl = &impl_donna;
  659. if (ed25519_impl_spot_check() == 0)
  660. return;
  661. /* LCOV_EXCL_START
  662. * unreachable unless ed25519_donna is broken */
  663. log_warn(LD_CRYPTO, "The Ed25519-donna implementation seems broken; using "
  664. "the ref10 implementation.");
  665. ed25519_impl = &impl_ref10;
  666. /* LCOV_EXCL_STOP */
  667. }
  668. /* Initialize the Ed25519 implementation. This is necessary if you're
  669. * going to use them in a multithreaded setting, and not otherwise. */
  670. void
  671. ed25519_init(void)
  672. {
  673. pick_ed25519_impl();
  674. }
  675. /* Return true if <b>point</b> is the identity element of the ed25519 group. */
  676. static int
  677. ed25519_point_is_identity_element(const uint8_t *point)
  678. {
  679. /* The identity element in ed25159 is the point with coordinates (0,1). */
  680. static const uint8_t ed25519_identity[32] = {
  681. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  682. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  683. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  684. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  685. tor_assert(sizeof(ed25519_identity) == ED25519_PUBKEY_LEN);
  686. return tor_memeq(point, ed25519_identity, sizeof(ed25519_identity));
  687. }
  688. /** Validate <b>pubkey</b> to ensure that it has no torsion component.
  689. * Return 0 if <b>pubkey</b> is valid, else return -1. */
  690. int
  691. ed25519_validate_pubkey(const ed25519_public_key_t *pubkey)
  692. {
  693. uint8_t result[32] = {9};
  694. /* First check that we were not given the identity element */
  695. if (ed25519_point_is_identity_element(pubkey->pubkey)) {
  696. log_warn(LD_CRYPTO, "ed25519 pubkey is the identity");
  697. return -1;
  698. }
  699. /* For any point on the curve, doing l*point should give the identity element
  700. * (where l is the group order). Do the computation and check that the
  701. * identity element is returned. */
  702. if (get_ed_impl()->ed25519_scalarmult_with_group_order(result,
  703. pubkey->pubkey) < 0) {
  704. log_warn(LD_CRYPTO, "ed25519 group order scalarmult failed");
  705. return -1;
  706. }
  707. if (!ed25519_point_is_identity_element(result)) {
  708. log_warn(LD_CRYPTO, "ed25519 validation failed");
  709. return -1;
  710. }
  711. return 0;
  712. }