crypto_ed25519.c 21 KB

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