crypto_ed25519.c 25 KB

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