crypto_ed25519.c 16 KB

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