crypto_ed25519.c 17 KB

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