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. #include <openssl/sha.h>
  30. static void pick_ed25519_impl(void);
  31. /** An Ed25519 implementation, as a set of function pointers. */
  32. typedef struct {
  33. int (*selftest)(void);
  34. int (*seckey)(unsigned char *);
  35. int (*seckey_expand)(unsigned char *, const unsigned char *);
  36. int (*pubkey)(unsigned char *, const unsigned char *);
  37. int (*keygen)(unsigned char *, unsigned char *);
  38. int (*open)(const unsigned char *, const unsigned char *, size_t, const
  39. unsigned char *);
  40. int (*sign)(unsigned char *, const unsigned char *, size_t,
  41. const unsigned char *, const unsigned char *);
  42. int (*open_batch)(const unsigned char **, size_t *, const unsigned char **,
  43. const unsigned char **, size_t, int *);
  44. int (*blind_secret_key)(unsigned char *, const unsigned char *,
  45. const unsigned char *);
  46. int (*blind_public_key)(unsigned char *, const unsigned char *,
  47. const unsigned char *);
  48. int (*pubkey_from_curve25519_pubkey)(unsigned char *, const unsigned char *,
  49. int);
  50. } ed25519_impl_t;
  51. /** The Ref10 Ed25519 implementation. This one is pure C and lightly
  52. * optimized. */
  53. static const ed25519_impl_t impl_ref10 = {
  54. NULL,
  55. ed25519_ref10_seckey,
  56. ed25519_ref10_seckey_expand,
  57. ed25519_ref10_pubkey,
  58. ed25519_ref10_keygen,
  59. ed25519_ref10_open,
  60. ed25519_ref10_sign,
  61. NULL,
  62. ed25519_ref10_blind_secret_key,
  63. ed25519_ref10_blind_public_key,
  64. ed25519_ref10_pubkey_from_curve25519_pubkey,
  65. };
  66. /** The Ref10 Ed25519 implementation. This one is heavily optimized, but still
  67. * mostly C. The C still tends to be heavily platform-specific. */
  68. static const ed25519_impl_t impl_donna = {
  69. ed25519_donna_selftest,
  70. ed25519_donna_seckey,
  71. ed25519_donna_seckey_expand,
  72. ed25519_donna_pubkey,
  73. ed25519_donna_keygen,
  74. ed25519_donna_open,
  75. ed25519_donna_sign,
  76. ed25519_sign_open_batch_donna,
  77. ed25519_donna_blind_secret_key,
  78. ed25519_donna_blind_public_key,
  79. ed25519_donna_pubkey_from_curve25519_pubkey,
  80. };
  81. /** Which Ed25519 implementation are we using? NULL if we haven't decided
  82. * yet. */
  83. static const ed25519_impl_t *ed25519_impl = NULL;
  84. /** Helper: Return our chosen Ed25519 implementation.
  85. *
  86. * This should only be called after we've picked an implementation, but
  87. * it _does_ recover if you forget this.
  88. **/
  89. static inline const ed25519_impl_t *
  90. get_ed_impl(void)
  91. {
  92. if (BUG(ed25519_impl == NULL)) {
  93. pick_ed25519_impl(); // LCOV_EXCL_LINE - We always call ed25519_init().
  94. }
  95. return ed25519_impl;
  96. }
  97. #ifdef TOR_UNIT_TESTS
  98. /** For testing: used to remember our actual choice of Ed25519
  99. * implementation */
  100. static const ed25519_impl_t *saved_ed25519_impl = NULL;
  101. /** For testing: Use the Ed25519 implementation called <b>name</b> until
  102. * crypto_ed25519_testing_restore_impl is called. Recognized names are
  103. * "donna" and "ref10". */
  104. void
  105. crypto_ed25519_testing_force_impl(const char *name)
  106. {
  107. tor_assert(saved_ed25519_impl == NULL);
  108. saved_ed25519_impl = ed25519_impl;
  109. if (! strcmp(name, "donna")) {
  110. ed25519_impl = &impl_donna;
  111. } else {
  112. tor_assert(!strcmp(name, "ref10"));
  113. ed25519_impl = &impl_ref10;
  114. }
  115. }
  116. /** For testing: go back to whatever Ed25519 implementation we had picked
  117. * before crypto_ed25519_testing_force_impl was called.
  118. */
  119. void
  120. crypto_ed25519_testing_restore_impl(void)
  121. {
  122. ed25519_impl = saved_ed25519_impl;
  123. saved_ed25519_impl = NULL;
  124. }
  125. #endif
  126. /**
  127. * Initialize a new ed25519 secret key in <b>seckey_out</b>. If
  128. * <b>extra_strong</b>, take the RNG inputs directly from the operating
  129. * system. Return 0 on success, -1 on failure.
  130. */
  131. int
  132. ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
  133. int extra_strong)
  134. {
  135. int r;
  136. uint8_t seed[32];
  137. if (extra_strong)
  138. crypto_strongest_rand(seed, sizeof(seed));
  139. else
  140. crypto_rand((char*)seed, sizeof(seed));
  141. r = get_ed_impl()->seckey_expand(seckey_out->seckey, seed);
  142. memwipe(seed, 0, sizeof(seed));
  143. return r < 0 ? -1 : 0;
  144. }
  145. /**
  146. * Given a 32-byte random seed in <b>seed</b>, expand it into an ed25519
  147. * secret key in <b>seckey_out</b>. Return 0 on success, -1 on failure.
  148. */
  149. int
  150. ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
  151. const uint8_t *seed)
  152. {
  153. if (get_ed_impl()->seckey_expand(seckey_out->seckey, seed) < 0)
  154. return -1;
  155. return 0;
  156. }
  157. /**
  158. * Given a secret key in <b>seckey</b>, expand it into an
  159. * ed25519 public key. Return 0 on success, -1 on failure.
  160. */
  161. int
  162. ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
  163. const ed25519_secret_key_t *seckey)
  164. {
  165. if (get_ed_impl()->pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
  166. return -1;
  167. return 0;
  168. }
  169. /** Generate a new ed25519 keypair in <b>keypair_out</b>. If
  170. * <b>extra_strong</b> is set, try to mix some system entropy into the key
  171. * generation process. Return 0 on success, -1 on failure. */
  172. int
  173. ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
  174. {
  175. if (ed25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  176. return -1;
  177. if (ed25519_public_key_generate(&keypair_out->pubkey,
  178. &keypair_out->seckey)<0)
  179. return -1;
  180. return 0;
  181. }
  182. /** Return true iff 'pubkey' is set to zero (eg to indicate that it is not
  183. * set). */
  184. int
  185. ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
  186. {
  187. return tor_mem_is_zero((char*)pubkey->pubkey, ED25519_PUBKEY_LEN);
  188. }
  189. /* Return a heap-allocated array that contains <b>msg</b> prefixed by the
  190. * string <b>prefix_str</b>. Set <b>final_msg_len_out</b> to the size of the
  191. * final array. If an error occured, return NULL. It's the resonsibility of the
  192. * caller to free the returned array. */
  193. static uint8_t *
  194. get_prefixed_msg(const uint8_t *msg, size_t msg_len,
  195. const char *prefix_str,
  196. size_t *final_msg_len_out)
  197. {
  198. size_t prefixed_msg_len, prefix_len;
  199. uint8_t *prefixed_msg;
  200. tor_assert(prefix_str);
  201. tor_assert(final_msg_len_out);
  202. prefix_len = strlen(prefix_str);
  203. /* msg_len + strlen(prefix_str) must not overflow. */
  204. if (msg_len > SIZE_T_CEILING - prefix_len) {
  205. return NULL;
  206. }
  207. prefixed_msg_len = msg_len + prefix_len;
  208. prefixed_msg = tor_malloc_zero(prefixed_msg_len);
  209. memcpy(prefixed_msg, prefix_str, prefix_len);
  210. memcpy(prefixed_msg + prefix_len, msg, msg_len);
  211. *final_msg_len_out = prefixed_msg_len;
  212. return prefixed_msg;
  213. }
  214. /**
  215. * Set <b>signature_out</b> to a signature of the <b>len</b>-byte message
  216. * <b>msg</b>, using the secret and public key in <b>keypair</b>.
  217. *
  218. * Return 0 if we successfuly signed the message, otherwise return -1.
  219. */
  220. int
  221. ed25519_sign(ed25519_signature_t *signature_out,
  222. const uint8_t *msg, size_t len,
  223. const ed25519_keypair_t *keypair)
  224. {
  225. if (get_ed_impl()->sign(signature_out->sig, msg, len,
  226. keypair->seckey.seckey,
  227. keypair->pubkey.pubkey) < 0) {
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. /**
  233. * Like ed25519_sign(), but also prefix <b>msg</b> with <b>prefix_str</b>
  234. * before signing. <b>prefix_str</b> must be a NUL-terminated string.
  235. */
  236. MOCK_IMPL(int,
  237. ed25519_sign_prefixed,(ed25519_signature_t *signature_out,
  238. const uint8_t *msg, size_t msg_len,
  239. const char *prefix_str,
  240. const ed25519_keypair_t *keypair))
  241. {
  242. int retval;
  243. size_t prefixed_msg_len;
  244. uint8_t *prefixed_msg;
  245. tor_assert(prefix_str);
  246. prefixed_msg = get_prefixed_msg(msg, msg_len, prefix_str,
  247. &prefixed_msg_len);
  248. if (!prefixed_msg) {
  249. log_warn(LD_GENERAL, "Failed to get prefixed msg.");
  250. return -1;
  251. }
  252. retval = ed25519_sign(signature_out,
  253. prefixed_msg, prefixed_msg_len,
  254. keypair);
  255. tor_free(prefixed_msg);
  256. return retval;
  257. }
  258. /**
  259. * Check whether if <b>signature</b> is a valid signature for the
  260. * <b>len</b>-byte message in <b>msg</b> made with the key <b>pubkey</b>.
  261. *
  262. * Return 0 if the signature is valid; -1 if it isn't.
  263. */
  264. MOCK_IMPL(int,
  265. ed25519_checksig,(const ed25519_signature_t *signature,
  266. const uint8_t *msg, size_t len,
  267. const ed25519_public_key_t *pubkey))
  268. {
  269. return
  270. get_ed_impl()->open(signature->sig, msg, len, pubkey->pubkey) < 0 ? -1 : 0;
  271. }
  272. /**
  273. * Like ed2519_checksig(), but also prefix <b>msg</b> with <b>prefix_str</b>
  274. * before verifying signature. <b>prefix_str</b> must be a NUL-terminated
  275. * string.
  276. */
  277. int
  278. ed25519_checksig_prefixed(const ed25519_signature_t *signature,
  279. const uint8_t *msg, size_t msg_len,
  280. const char *prefix_str,
  281. const ed25519_public_key_t *pubkey)
  282. {
  283. int retval;
  284. size_t prefixed_msg_len;
  285. uint8_t *prefixed_msg;
  286. prefixed_msg = get_prefixed_msg(msg, msg_len, prefix_str,
  287. &prefixed_msg_len);
  288. if (!prefixed_msg) {
  289. log_warn(LD_GENERAL, "Failed to get prefixed msg.");
  290. return -1;
  291. }
  292. retval = ed25519_checksig(signature,
  293. prefixed_msg, prefixed_msg_len,
  294. pubkey);
  295. tor_free(prefixed_msg);
  296. return retval;
  297. }
  298. /** Validate every signature among those in <b>checkable</b>, which contains
  299. * exactly <b>n_checkable</b> elements. If <b>okay_out</b> is non-NULL, set
  300. * the i'th element of <b>okay_out</b> to 1 if the i'th element of
  301. * <b>checkable</b> is valid, and to 0 otherwise. Return 0 if every signature
  302. * was valid. Otherwise return -N, where N is the number of invalid
  303. * signatures.
  304. */
  305. MOCK_IMPL(int,
  306. ed25519_checksig_batch,(int *okay_out,
  307. const ed25519_checkable_t *checkable,
  308. int n_checkable))
  309. {
  310. int i, res;
  311. const ed25519_impl_t *impl = get_ed_impl();
  312. if (impl->open_batch == NULL) {
  313. /* No batch verification implementation available, fake it by checking the
  314. * each signature individually.
  315. */
  316. res = 0;
  317. for (i = 0; i < n_checkable; ++i) {
  318. const ed25519_checkable_t *ch = &checkable[i];
  319. int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
  320. if (r < 0)
  321. --res;
  322. if (okay_out)
  323. okay_out[i] = (r == 0);
  324. }
  325. } else {
  326. /* ed25519-donna style batch verification available.
  327. *
  328. * Theoretically, this should only be called if n_checkable >= 3, since
  329. * that's the threshold where the batch verification actually kicks in,
  330. * but the only difference is a few mallocs/frees.
  331. */
  332. const uint8_t **ms;
  333. size_t *lens;
  334. const uint8_t **pks;
  335. const uint8_t **sigs;
  336. int *oks;
  337. int all_ok;
  338. ms = tor_calloc(n_checkable, sizeof(uint8_t*));
  339. lens = tor_calloc(n_checkable, sizeof(size_t));
  340. pks = tor_calloc(n_checkable, sizeof(uint8_t*));
  341. sigs = tor_calloc(n_checkable, sizeof(uint8_t*));
  342. oks = okay_out ? okay_out : tor_calloc(n_checkable, sizeof(int));
  343. for (i = 0; i < n_checkable; ++i) {
  344. ms[i] = checkable[i].msg;
  345. lens[i] = checkable[i].len;
  346. pks[i] = checkable[i].pubkey->pubkey;
  347. sigs[i] = checkable[i].signature.sig;
  348. oks[i] = 0;
  349. }
  350. res = 0;
  351. all_ok = impl->open_batch(ms, lens, pks, sigs, n_checkable, oks);
  352. for (i = 0; i < n_checkable; ++i) {
  353. if (!oks[i])
  354. --res;
  355. }
  356. /* XXX: For now sanity check oks with the return value. Once we have
  357. * more confidence in the code, if `all_ok == 0` we can skip iterating
  358. * over oks since all the signatures were found to be valid.
  359. */
  360. tor_assert(((res == 0) && !all_ok) || ((res < 0) && all_ok));
  361. tor_free(ms);
  362. tor_free(lens);
  363. tor_free(pks);
  364. tor_free(sigs);
  365. if (! okay_out)
  366. tor_free(oks);
  367. }
  368. return res;
  369. }
  370. /**
  371. * Given a curve25519 keypair in <b>inp</b>, generate a corresponding
  372. * ed25519 keypair in <b>out</b>, and set <b>signbit_out</b> to the
  373. * sign bit of the X coordinate of the ed25519 key.
  374. *
  375. * NOTE THAT IT IS PROBABLY NOT SAFE TO USE THE GENERATED KEY FOR ANYTHING
  376. * OUTSIDE OF WHAT'S PRESENTED IN PROPOSAL 228. In particular, it's probably
  377. * not a great idea to use it to sign attacker-supplied anything.
  378. */
  379. int
  380. ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
  381. int *signbit_out,
  382. const curve25519_keypair_t *inp)
  383. {
  384. const char string[] = "Derive high part of ed25519 key from curve25519 key";
  385. ed25519_public_key_t pubkey_check;
  386. SHA512_CTX ctx;
  387. uint8_t sha512_output[64];
  388. memcpy(out->seckey.seckey, inp->seckey.secret_key, 32);
  389. SHA512_Init(&ctx);
  390. SHA512_Update(&ctx, out->seckey.seckey, 32);
  391. SHA512_Update(&ctx, string, sizeof(string));
  392. SHA512_Final(sha512_output, &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. }