crypto_ed25519.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. int
  16. ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
  17. int extra_strong)
  18. {
  19. int r;
  20. uint8_t seed[32];
  21. if (! extra_strong || crypto_strongest_rand(seed, sizeof(seed)) < 0)
  22. crypto_rand((char*)seed, sizeof(seed));
  23. r = ed25519_ref10_seckey_expand(seckey_out->seckey, seed);
  24. memwipe(seed, 0, sizeof(seed));
  25. return r < 0 ? -1 : 0;
  26. }
  27. int
  28. ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
  29. const uint8_t *seed)
  30. {
  31. if (ed25519_ref10_seckey_expand(seckey_out->seckey, seed) < 0)
  32. return -1;
  33. return 0;
  34. }
  35. int
  36. ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
  37. const ed25519_secret_key_t *seckey)
  38. {
  39. if (ed25519_ref10_pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
  40. return -1;
  41. return 0;
  42. }
  43. /** Generate a new ed25519 keypair in <b>keypair_out</b>. If
  44. * <b>extra_strong</b> is set, try to mix some system entropy into the key
  45. * generation process. Return 0 on success, -1 on failure. */
  46. int
  47. ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
  48. {
  49. if (ed25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  50. return -1;
  51. if (ed25519_public_key_generate(&keypair_out->pubkey,
  52. &keypair_out->seckey)<0)
  53. return -1;
  54. return 0;
  55. }
  56. /**
  57. * Set <b>signature_out</b> to a signature of the <b>len</b>-byte message
  58. * <b>msg</b>, using the secret and public key in <b>keypair</b>.
  59. */
  60. int
  61. ed25519_sign(ed25519_signature_t *signature_out,
  62. const uint8_t *msg, size_t len,
  63. const ed25519_keypair_t *keypair)
  64. {
  65. if (ed25519_ref10_sign(signature_out->sig, msg, len,
  66. keypair->seckey.seckey,
  67. keypair->pubkey.pubkey) < 0) {
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. /**
  73. * Check whether if <b>signature</b> is a valid signature for the
  74. * <b>len</b>-byte message in <b>msg</b> made with the key <b>pubkey</b>.
  75. *
  76. * Return 0 if the signature is valid; -1 if it isn't.
  77. */
  78. int
  79. ed25519_checksig(const ed25519_signature_t *signature,
  80. const uint8_t *msg, size_t len,
  81. const ed25519_public_key_t *pubkey)
  82. {
  83. return
  84. ed25519_ref10_open(signature->sig, msg, len, pubkey->pubkey) < 0 ? -1 : 0;
  85. }
  86. /** Validate every signature among those in <b>checkable</b>, which contains
  87. * exactly <b>n_checkable</b> elements. If <b>okay_out</b> is non-NULL, set
  88. * the i'th element of <b>okay_out</b> to 1 if the i'th element of
  89. * <b>checkable</b> is valid, and to 0 otherwise. Return 0 if every signature
  90. * was valid. Otherwise return -N, where N is the number of invalid
  91. * signatures.
  92. */
  93. int
  94. ed25519_checksig_batch(int *okay_out,
  95. const ed25519_checkable_t *checkable,
  96. int n_checkable)
  97. {
  98. int res, i;
  99. res = 0;
  100. for (i = 0; i < n_checkable; ++i) {
  101. const ed25519_checkable_t *ch = &checkable[i];
  102. int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
  103. if (r < 0)
  104. --res;
  105. if (okay_out)
  106. okay_out[i] = (r == 0);
  107. }
  108. #if 0
  109. const uint8_t **ms;
  110. size_t *lens;
  111. const uint8_t **pks;
  112. const uint8_t **sigs;
  113. int *oks;
  114. ms = tor_malloc(sizeof(uint8_t*)*n_checkable);
  115. lens = tor_malloc(sizeof(size_t)*n_checkable);
  116. pks = tor_malloc(sizeof(uint8_t*)*n_checkable);
  117. sigs = tor_malloc(sizeof(uint8_t*)*n_checkable);
  118. oks = okay_out ? okay_out : tor_malloc(sizeof(int)*n_checkable);
  119. for (i = 0; i < n_checkable; ++i) {
  120. ms[i] = checkable[i].msg;
  121. lens[i] = checkable[i].len;
  122. pks[i] = checkable[i].pubkey->pubkey;
  123. sigs[i] = checkable[i].signature.sig;
  124. oks[i] = 0;
  125. }
  126. ed25519_sign_open_batch_donna_fb(ms, lens, pks, sigs, n_checkable, oks);
  127. res = 0;
  128. for (i = 0; i < n_checkable; ++i) {
  129. if (!oks[i])
  130. --res;
  131. }
  132. tor_free(ms);
  133. tor_free(lens);
  134. tor_free(pks);
  135. if (! okay_out)
  136. tor_free(oks);
  137. #endif
  138. return res;
  139. }
  140. /**
  141. * Given a curve25519 keypair in <b>inp</b>, generate a corresponding
  142. * ed25519 keypair in <b>out</b>, and set <b>signbit_out</b> to the
  143. * sign bit of the X coordinate of the ed25519 key.
  144. *
  145. * NOTE THAT IT IS PROBABLY NOT SAFE TO USE THE GENERATED KEY FOR ANYTHING
  146. * OUTSIDE OF WHAT'S PRESENTED IN PROPOSAL 228. In particular, it's probably
  147. * not a great idea to use it to sign attacker-supplied anything.
  148. */
  149. int
  150. ed25519_keypair_from_curve25519_keypair(ed25519_keypair_t *out,
  151. int *signbit_out,
  152. const curve25519_keypair_t *inp)
  153. {
  154. const char string[] = "Derive high part of ed25519 key from curve25519 key";
  155. ed25519_public_key_t pubkey_check;
  156. SHA512_CTX ctx;
  157. uint8_t sha512_output[64];
  158. memcpy(out->seckey.seckey, inp->seckey.secret_key, 32);
  159. SHA512_Init(&ctx);
  160. SHA512_Update(&ctx, out->seckey.seckey, 32);
  161. SHA512_Update(&ctx, string, sizeof(string));
  162. SHA512_Final(sha512_output, &ctx);
  163. memcpy(out->seckey.seckey + 32, sha512_output, 32);
  164. ed25519_public_key_generate(&out->pubkey, &out->seckey);
  165. *signbit_out = out->pubkey.pubkey[31] >> 7;
  166. ed25519_public_key_from_curve25519_public_key(&pubkey_check, &inp->pubkey,
  167. *signbit_out);
  168. tor_assert(fast_memeq(pubkey_check.pubkey, out->pubkey.pubkey, 32));
  169. memwipe(&pubkey_check, 0, sizeof(pubkey_check));
  170. memwipe(&ctx, 0, sizeof(ctx));
  171. memwipe(sha512_output, 0, sizeof(sha512_output));
  172. return 0;
  173. }
  174. /**
  175. * Given a curve25519 public key and sign bit of X coordinate of the ed25519
  176. * public key, generate the corresponding ed25519 public key.
  177. */
  178. int
  179. ed25519_public_key_from_curve25519_public_key(ed25519_public_key_t *pubkey,
  180. const curve25519_public_key_t *pubkey_in,
  181. int signbit)
  182. {
  183. return ed25519_ref10_pubkey_from_curve25519_pubkey(pubkey->pubkey,
  184. pubkey_in->public_key,
  185. signbit);
  186. }
  187. /** DOCDOC */
  188. int
  189. ed25519_seckey_write_to_file(const ed25519_secret_key_t *seckey,
  190. const char *filename,
  191. const char *tag)
  192. {
  193. return crypto_write_tagged_contents_to_file(filename,
  194. "ed25519v1-secret",
  195. tag,
  196. seckey->seckey,
  197. sizeof(seckey->seckey));
  198. }
  199. /** DOCDOC */
  200. int
  201. ed25519_seckey_read_from_file(ed25519_secret_key_t *seckey_out,
  202. char **tag_out,
  203. const char *filename)
  204. {
  205. ssize_t len;
  206. len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-secret",
  207. tag_out, seckey_out->seckey,
  208. sizeof(seckey_out->seckey));
  209. if (len != sizeof(seckey_out->seckey))
  210. return -1;
  211. return 0;
  212. }
  213. /** DOCDOC */
  214. int
  215. ed25519_pubkey_write_to_file(const ed25519_public_key_t *pubkey,
  216. const char *filename,
  217. const char *tag)
  218. {
  219. return crypto_write_tagged_contents_to_file(filename,
  220. "ed25519v1-public",
  221. tag,
  222. pubkey->pubkey,
  223. sizeof(pubkey->pubkey));
  224. }
  225. /** DOCDOC */
  226. int
  227. ed25519_pubkey_read_from_file(ed25519_public_key_t *pubkey_out,
  228. char **tag_out,
  229. const char *filename)
  230. {
  231. ssize_t len;
  232. len = crypto_read_tagged_contents_from_file(filename, "ed25519v1-public",
  233. tag_out, pubkey_out->pubkey,
  234. sizeof(pubkey_out->pubkey));
  235. if (len != sizeof(pubkey_out->pubkey))
  236. return -1;
  237. return 0;
  238. }