crypto_ed25519.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright (c) 2013, 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 <openssl/sha.h>
  15. /**
  16. * Initialize a new ed25519 secret key in <b>seckey_out</b>. If
  17. * <b>extra_strong</b>, take the RNG inputs directly from the operating
  18. * system. Return 0 on success, -1 on failure.
  19. */
  20. int
  21. ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
  22. int extra_strong)
  23. {
  24. int r;
  25. uint8_t seed[32];
  26. if (! extra_strong || crypto_strongest_rand(seed, sizeof(seed)) < 0)
  27. crypto_rand((char*)seed, sizeof(seed));
  28. r = ed25519_ref10_seckey_expand(seckey_out->seckey, seed);
  29. memwipe(seed, 0, sizeof(seed));
  30. return r < 0 ? -1 : 0;
  31. }
  32. /**
  33. * Given a 32-byte random seed in <b>seed</b>, expand it into an ed25519
  34. * secret key in <b>seckey_out</b>. Return 0 on success, -1 on failure.
  35. */
  36. int
  37. ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
  38. const uint8_t *seed)
  39. {
  40. if (ed25519_ref10_seckey_expand(seckey_out->seckey, seed) < 0)
  41. return -1;
  42. return 0;
  43. }
  44. /**
  45. * Given a secret key in <b>seckey</b>, expand it into an
  46. * ed25519 public key. Return 0 on success, -1 on failure.
  47. */
  48. int
  49. ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
  50. const ed25519_secret_key_t *seckey)
  51. {
  52. if (ed25519_ref10_pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
  53. return -1;
  54. return 0;
  55. }
  56. /** Generate a new ed25519 keypair in <b>keypair_out</b>. If
  57. * <b>extra_strong</b> is set, try to mix some system entropy into the key
  58. * generation process. Return 0 on success, -1 on failure. */
  59. int
  60. ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
  61. {
  62. if (ed25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  63. return -1;
  64. if (ed25519_public_key_generate(&keypair_out->pubkey,
  65. &keypair_out->seckey)<0)
  66. return -1;
  67. return 0;
  68. }
  69. /**
  70. * Set <b>signature_out</b> to a signature of the <b>len</b>-byte message
  71. * <b>msg</b>, using the secret and public key in <b>keypair</b>.
  72. */
  73. int
  74. ed25519_sign(ed25519_signature_t *signature_out,
  75. const uint8_t *msg, size_t len,
  76. const ed25519_keypair_t *keypair)
  77. {
  78. if (ed25519_ref10_sign(signature_out->sig, msg, len,
  79. keypair->seckey.seckey,
  80. keypair->pubkey.pubkey) < 0) {
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. /**
  86. * Check whether if <b>signature</b> is a valid signature for the
  87. * <b>len</b>-byte message in <b>msg</b> made with the key <b>pubkey</b>.
  88. *
  89. * Return 0 if the signature is valid; -1 if it isn't.
  90. */
  91. int
  92. ed25519_checksig(const ed25519_signature_t *signature,
  93. const uint8_t *msg, size_t len,
  94. const ed25519_public_key_t *pubkey)
  95. {
  96. return
  97. ed25519_ref10_open(signature->sig, msg, len, pubkey->pubkey) < 0 ? -1 : 0;
  98. }
  99. /** Validate every signature among those in <b>checkable</b>, which contains
  100. * exactly <b>n_checkable</b> elements. If <b>okay_out</b> is non-NULL, set
  101. * the i'th element of <b>okay_out</b> to 1 if the i'th element of
  102. * <b>checkable</b> is valid, and to 0 otherwise. Return 0 if every signature
  103. * was valid. Otherwise return -N, where N is the number of invalid
  104. * signatures.
  105. */
  106. int
  107. ed25519_checksig_batch(int *okay_out,
  108. const ed25519_checkable_t *checkable,
  109. int n_checkable)
  110. {
  111. int res, i;
  112. res = 0;
  113. for (i = 0; i < n_checkable; ++i) {
  114. const ed25519_checkable_t *ch = &checkable[i];
  115. int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
  116. if (r < 0)
  117. --res;
  118. if (okay_out)
  119. okay_out[i] = (r == 0);
  120. }
  121. #if 0
  122. const uint8_t **ms;
  123. size_t *lens;
  124. const uint8_t **pks;
  125. const uint8_t **sigs;
  126. int *oks;
  127. ms = tor_malloc(sizeof(uint8_t*)*n_checkable);
  128. lens = tor_malloc(sizeof(size_t)*n_checkable);
  129. pks = tor_malloc(sizeof(uint8_t*)*n_checkable);
  130. sigs = tor_malloc(sizeof(uint8_t*)*n_checkable);
  131. oks = okay_out ? okay_out : tor_malloc(sizeof(int)*n_checkable);
  132. for (i = 0; i < n_checkable; ++i) {
  133. ms[i] = checkable[i].msg;
  134. lens[i] = checkable[i].len;
  135. pks[i] = checkable[i].pubkey->pubkey;
  136. sigs[i] = checkable[i].signature.sig;
  137. oks[i] = 0;
  138. }
  139. ed25519_sign_open_batch_donna_fb(ms, lens, pks, sigs, n_checkable, oks);
  140. res = 0;
  141. for (i = 0; i < n_checkable; ++i) {
  142. if (!oks[i])
  143. --res;
  144. }
  145. tor_free(ms);
  146. tor_free(lens);
  147. tor_free(pks);
  148. if (! okay_out)
  149. tor_free(oks);
  150. #endif
  151. return res;
  152. }
  153. /**
  154. * Given a curve25519 keypair in <b>inp</b>, generate a corresponding
  155. * ed25519 keypair in <b>out</b>, and set <b>signbit_out</b> to the
  156. * sign bit of the X coordinate of the ed25519 key.
  157. *
  158. * NOTE THAT IT IS PROBABLY NOT SAFE TO USE THE GENERATED KEY FOR ANYTHING
  159. * OUTSIDE OF WHAT'S PRESENTED IN PROPOSAL 228. In particular, it's probably
  160. * not a great idea to use it to sign attacker-supplied anything.
  161. */
  162. int
  163. ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
  164. int *signbit_out,
  165. const curve25519_keypair_t *inp)
  166. {
  167. const char string[] = "Derive high part of ed25519 key from curve25519 key";
  168. ed25519_public_key_t pubkey_check;
  169. SHA512_CTX ctx;
  170. uint8_t sha512_output[64];
  171. memcpy(out->seckey.seckey, inp->seckey.secret_key, 32);
  172. SHA512_Init(&ctx);
  173. SHA512_Update(&ctx, out->seckey.seckey, 32);
  174. SHA512_Update(&ctx, string, sizeof(string));
  175. SHA512_Final(sha512_output, &ctx);
  176. memcpy(out->seckey.seckey + 32, sha512_output, 32);
  177. ed25519_public_key_generate(&out->pubkey, &out->seckey);
  178. *signbit_out = out->pubkey.pubkey[31] >> 7;
  179. ed25519_public_key_from_curve25519_public_key(&pubkey_check, &inp->pubkey,
  180. *signbit_out);
  181. tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
  182. memwipe(&pubkey_check, 0, sizeof(pubkey_check));
  183. memwipe(&ctx, 0, sizeof(ctx));
  184. memwipe(sha512_output, 0, sizeof(sha512_output));
  185. return 0;
  186. }
  187. /**
  188. * Given a curve25519 public key and sign bit of X coordinate of the ed25519
  189. * public key, generate the corresponding ed25519 public key.
  190. */
  191. int
  192. ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey,
  193. const curve25519_public_key_t *pubkey_in,
  194. int signbit)
  195. {
  196. return ed25519_ref10_pubkey_from_curve25519_pubkey(pubkey->pubkey,
  197. pubkey_in->public_key,
  198. signbit);
  199. }
  200. /**
  201. * Given an ed25519 keypair in <b>inp</b>, generate a corresponding
  202. * ed25519 keypair in <b>out</b>, blinded by the corresponding 32-byte input
  203. * in 'param'.
  204. *
  205. * Tor uses key blinding for the "next-generation" hidden services design:
  206. * service descriptors are encrypted with a key derived from the service's
  207. * long-term public key, and then signed with (and stored at a position
  208. * indexed by) a short-term key derived by blinding the long-term keys.
  209. */
  210. int
  211. ed25519_keypair_blind(ed25519_keypair_t *out,
  212. const ed25519_keypair_t *inp,
  213. const uint8_t *param)
  214. {
  215. ed25519_public_key_t pubkey_check;
  216. ed25519_ref10_derive_secret_key(out->seckey.seckey,
  217. inp->seckey.seckey, param);
  218. ed25519_public_blind(&pubkey_check, &inp->pubkey, param);
  219. ed25519_public_key_generate(&out->pubkey, &out->seckey);
  220. tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
  221. memwipe(&pubkey_check, 0, sizeof(pubkey_check));
  222. return 0;
  223. }
  224. /**
  225. * Given an ed25519 public key in <b>inp</b>, generate a corresponding blinded
  226. * public key in <b>out</b>, blinded with the 32-byte parameter in
  227. * <b>param</b>. Return 0 on sucess, -1 on railure.
  228. */
  229. int
  230. ed25519_public_blind(ed25519_public_key_t *out,
  231. const ed25519_public_key_t *inp,
  232. const uint8_t *param)
  233. {
  234. ed25519_ref10_derive_public_key(out->pubkey, inp->pubkey, param);
  235. return 0;
  236. }
  237. /**
  238. * Store seckey unencrypted to <b>filename</b>, marking it with <b>tag</b>.
  239. * Return 0 on success, -1 on failure.
  240. */
  241. int
  242. ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey,
  243. const char *filename,
  244. const char *tag)
  245. {
  246. return crypto_write_tagged_contents_to_file(filename,
  247. "ed25519v1-secret",
  248. tag,
  249. seckey->seckey,
  250. sizeof(seckey->seckey));
  251. }
  252. /**
  253. * Read seckey unencrypted from <b>filename</b>, storing it into
  254. * <b>seckey_out</b>. Set *<b>tag_out</> to the tag it was marked with.
  255. * Return 0 on success, -1 on failure.
  256. */
  257. int
  258. ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out,
  259. char **tag_out,
  260. const char *filename)
  261. {
  262. ssize_t len;
  263. len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-secret",
  264. tag_out, seckey_out->seckey,
  265. sizeof(seckey_out->seckey));
  266. if (len != sizeof(seckey_out->seckey))
  267. return -1;
  268. return 0;
  269. }
  270. /**
  271. * Store pubkey unencrypted to <b>filename</b>, marking it with <b>tag</b>.
  272. * Return 0 on success, -1 on failure.
  273. */
  274. int
  275. ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey,
  276. const char *filename,
  277. const char *tag)
  278. {
  279. return crypto_write_tagged_contents_to_file(filename,
  280. "ed25519v1-public",
  281. tag,
  282. pubkey->pubkey,
  283. sizeof(pubkey->pubkey));
  284. }
  285. /**
  286. * Store pubkey unencrypted to <b>filename</b>, marking it with <b>tag</b>.
  287. * Return 0 on success, -1 on failure.
  288. */
  289. int
  290. ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
  291. char **tag_out,
  292. const char *filename)
  293. {
  294. ssize_t len;
  295. len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-public",
  296. tag_out, pubkey_out->pubkey,
  297. sizeof(pubkey_out->pubkey));
  298. if (len != sizeof(pubkey_out->pubkey))
  299. return -1;
  300. return 0;
  301. }