crypto_ed25519.c 25 KB

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