crypto_ed25519.c 16 KB

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