crypto_ed25519.c 25 KB

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