crypto_ed25519.c 23 KB

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