ed25519_tor.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. Public domain by Andrew M. <liquidsun@gmail.com>
  3. Ed25519 reference implementation using Ed25519-donna
  4. */
  5. /*
  6. Tor specific notes:
  7. This file is used by Tor instead of `ed25519.c` as the number of
  8. changes/additions is non-trivial.
  9. Tor modifications to `ed25519.c`:
  10. * 'Tab` -> ' '.
  11. * Include `ed25519_donna_tor.h` instead of `ed25519.h`.
  12. * The external interface has been reworked to match that provided
  13. by Tor's copy of the SUPERCOP `ref10` code.
  14. * The secret (aka private) key is now stored/used in expanded form.
  15. * The internal math tests from `test-internals.c` have been wrapped
  16. in a function and the entire file is included to allow for
  17. runtime validation.
  18. */
  19. /* define ED25519_SUFFIX to have it appended to the end of each public function */
  20. #if !defined(ED25519_SUFFIX)
  21. #define ED25519_SUFFIX
  22. #endif
  23. #define ED25519_FN3(fn,suffix) fn##suffix
  24. #define ED25519_FN2(fn,suffix) ED25519_FN3(fn,suffix)
  25. #define ED25519_FN(fn) ED25519_FN2(fn,ED25519_SUFFIX)
  26. #include "orconfig.h"
  27. #include "ed25519-donna.h"
  28. #include "ed25519_donna_tor.h"
  29. #include "ed25519-randombytes.h"
  30. #include "ed25519-hash.h"
  31. typedef unsigned char ed25519_signature[64];
  32. typedef unsigned char ed25519_public_key[32];
  33. typedef unsigned char ed25519_secret_key[32];
  34. static void gettweak(unsigned char *out, const unsigned char *param);
  35. static int ED25519_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen,
  36. const ed25519_public_key pk, const ed25519_signature RS);
  37. /*
  38. Generates a (extsk[0..31]) and aExt (extsk[32..63])
  39. */
  40. DONNA_INLINE static void
  41. ed25519_extsk(hash_512bits extsk, const ed25519_secret_key sk) {
  42. ed25519_hash(extsk, sk, 32);
  43. extsk[0] &= 248;
  44. extsk[31] &= 127;
  45. extsk[31] |= 64;
  46. }
  47. static void
  48. ed25519_hram(hash_512bits hram, const ed25519_signature RS, const ed25519_public_key pk, const unsigned char *m, size_t mlen) {
  49. ed25519_hash_context ctx;
  50. ed25519_hash_init(&ctx);
  51. ed25519_hash_update(&ctx, RS, 32);
  52. ed25519_hash_update(&ctx, pk, 32);
  53. ed25519_hash_update(&ctx, m, mlen);
  54. ed25519_hash_final(&ctx, hram);
  55. }
  56. static int
  57. ED25519_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS) {
  58. ge25519 ALIGN(16) R, A;
  59. hash_512bits hash;
  60. bignum256modm hram, S;
  61. unsigned char checkR[32];
  62. if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
  63. return -1;
  64. /* hram = H(R,A,m) */
  65. ed25519_hram(hash, RS, pk, m, mlen);
  66. expand256_modm(hram, hash, 64);
  67. /* S */
  68. expand256_modm(S, RS + 32, 32);
  69. /* SB - H(R,A,m)A */
  70. ge25519_double_scalarmult_vartime(&R, &A, hram, S);
  71. ge25519_pack(checkR, &R);
  72. /* check that R = SB - H(R,A,m)A */
  73. return ed25519_verify(RS, checkR, 32) ? 0 : -1;
  74. }
  75. #include "ed25519-donna-batchverify.h"
  76. /*
  77. Fast Curve25519 basepoint scalar multiplication
  78. */
  79. void
  80. ED25519_FN(curved25519_scalarmult_basepoint) (curved25519_key pk, const curved25519_key e) {
  81. curved25519_key ec;
  82. bignum256modm s;
  83. bignum25519 ALIGN(16) yplusz, zminusy;
  84. ge25519 ALIGN(16) p;
  85. size_t i;
  86. /* clamp */
  87. for (i = 0; i < 32; i++) ec[i] = e[i];
  88. ec[0] &= 248;
  89. ec[31] &= 127;
  90. ec[31] |= 64;
  91. expand_raw256_modm(s, ec);
  92. /* scalar * basepoint */
  93. ge25519_scalarmult_base_niels(&p, ge25519_niels_base_multiples, s);
  94. /* u = (y + z) / (z - y) */
  95. curve25519_add(yplusz, p.y, p.z);
  96. curve25519_sub(zminusy, p.z, p.y);
  97. curve25519_recip(zminusy, zminusy);
  98. curve25519_mul(yplusz, yplusz, zminusy);
  99. curve25519_contract(pk, yplusz);
  100. }
  101. /*
  102. Tor has a specific idea of how an Ed25519 implementaion should behave.
  103. Implement such a beast using the ed25519-donna primitives/internals.
  104. * Private key generation using Tor's CSPRNG.
  105. * Routines that deal with the private key now use the expanded form.
  106. * Support for multiplicative key blinding has been added.
  107. * Support for converting a Curve25519 key to an Ed25519 key has been added.
  108. */
  109. int
  110. ed25519_donna_seckey(unsigned char *sk)
  111. {
  112. ed25519_secret_key seed;
  113. crypto_strongest_rand(seed, 32);
  114. ed25519_extsk(sk, seed);
  115. memwipe(seed, 0, sizeof(seed));
  116. return 0;
  117. }
  118. int
  119. ed25519_donna_seckey_expand(unsigned char *sk, const unsigned char *skseed)
  120. {
  121. ed25519_extsk(sk, skseed);
  122. return 0;
  123. }
  124. int
  125. ed25519_donna_pubkey(unsigned char *pk, const unsigned char *sk)
  126. {
  127. bignum256modm a = {0};
  128. ge25519 ALIGN(16) A = {{0}, {0}, {0}, {0}};
  129. /* A = aB */
  130. expand256_modm(a, sk, 32);
  131. ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
  132. ge25519_pack(pk, &A);
  133. return 0;
  134. }
  135. int
  136. ed25519_donna_keygen(unsigned char *pk, unsigned char *sk)
  137. {
  138. int ok;
  139. ok = ed25519_donna_seckey(sk);
  140. ed25519_donna_pubkey(pk, sk);
  141. return ok;
  142. }
  143. int
  144. ed25519_donna_open(const unsigned char *signature, const unsigned char *m,
  145. size_t mlen, const unsigned char *pk)
  146. {
  147. /* Wrap the ed25519-donna routine, since it is also used by the batch
  148. * verification code.
  149. */
  150. return ED25519_FN(ed25519_sign_open)(m, mlen, pk, signature);
  151. }
  152. int
  153. ed25519_donna_sign(unsigned char *sig, const unsigned char *m, size_t mlen,
  154. const unsigned char *sk, const unsigned char *pk)
  155. {
  156. ed25519_hash_context ctx;
  157. bignum256modm r = {0}, S, a;
  158. ge25519 ALIGN(16) R = {{0}, {0}, {0}, {0}};
  159. hash_512bits hashr, hram;
  160. /* This is equivalent to the removed `ED25519_FN(ed25519_sign)` routine,
  161. * except that the key expansion step is omitted as sk already is in expanded
  162. * form.
  163. */
  164. /* r = H(aExt[32..64], m) */
  165. ed25519_hash_init(&ctx);
  166. ed25519_hash_update(&ctx, sk + 32, 32);
  167. ed25519_hash_update(&ctx, m, mlen);
  168. ed25519_hash_final(&ctx, hashr);
  169. expand256_modm(r, hashr, 64);
  170. /* R = rB */
  171. ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
  172. ge25519_pack(sig, &R);
  173. /* S = H(R,A,m).. */
  174. ed25519_hram(hram, sig, pk, m, mlen);
  175. expand256_modm(S, hram, 64);
  176. /* S = H(R,A,m)a */
  177. expand256_modm(a, sk, 32);
  178. mul256_modm(S, S, a);
  179. /* S = (r + H(R,A,m)a) */
  180. add256_modm(S, S, r);
  181. /* S = (r + H(R,A,m)a) mod L */
  182. contract256_modm(sig + 32, S);
  183. return 0;
  184. }
  185. static void
  186. gettweak(unsigned char *out, const unsigned char *param)
  187. {
  188. static const char str[] = "Derive temporary signing key";
  189. ed25519_hash_context ctx;
  190. ed25519_hash_init(&ctx);
  191. ed25519_hash_update(&ctx, (const unsigned char*)str, strlen(str));
  192. ed25519_hash_update(&ctx, param, 32);
  193. ed25519_hash_final(&ctx, out);
  194. out[0] &= 248; /* Is this necessary ? */
  195. out[31] &= 63;
  196. out[31] |= 64;
  197. }
  198. int
  199. ed25519_donna_blind_secret_key(unsigned char *out, const unsigned char *inp,
  200. const unsigned char *param)
  201. {
  202. static const char str[] = "Derive temporary signing key hash input";
  203. unsigned char tweak[64];
  204. ed25519_hash_context ctx;
  205. bignum256modm ALIGN(16) sk, t;
  206. gettweak(tweak, param);
  207. expand256_modm(t, tweak, 32);
  208. expand256_modm(sk, inp, 32);
  209. mul256_modm(sk, sk, t);
  210. contract256_modm(out, sk);
  211. ed25519_hash_init(&ctx);
  212. ed25519_hash_update(&ctx, (const unsigned char*)str, strlen(str));
  213. ed25519_hash_update(&ctx, inp + 32, 32);
  214. ed25519_hash_final(&ctx, tweak);
  215. memcpy(out + 32, tweak, 32);
  216. memwipe(sk, 0, sizeof(sk));
  217. memwipe(t, 0, sizeof(t));
  218. memwipe(tweak, 0, sizeof(tweak));
  219. return 0;
  220. }
  221. int
  222. ed25519_donna_blind_public_key(unsigned char *out, const unsigned char *inp,
  223. const unsigned char *param)
  224. {
  225. static const bignum256modm zero = { 0 };
  226. unsigned char tweak[64];
  227. unsigned char pkcopy[32];
  228. ge25519 ALIGN(16) A, Aprime;
  229. bignum256modm ALIGN(16) t;
  230. gettweak(tweak, param);
  231. expand256_modm(t, tweak, 32);
  232. /* No "ge25519_unpack", negate the public key. */
  233. memcpy(pkcopy, inp, 32);
  234. pkcopy[31] ^= (1<<7);
  235. ge25519_unpack_negative_vartime(&A, pkcopy);
  236. /* A' = [tweak] * A + [0] * basepoint. */
  237. ge25519_double_scalarmult_vartime(&Aprime, &A, t, zero);
  238. ge25519_pack(out, &Aprime);
  239. memwipe(tweak, 0, sizeof(tweak));
  240. memwipe(pkcopy, 0, sizeof(pkcopy));
  241. memwipe(&A, 0, sizeof(A));
  242. memwipe(&Aprime, 0, sizeof(Aprime));
  243. memwipe(t, 0, sizeof(t));
  244. return 0;
  245. }
  246. int
  247. ed25519_donna_pubkey_from_curve25519_pubkey(unsigned char *out,
  248. const unsigned char *inp, int signbit)
  249. {
  250. static const bignum25519 ALIGN(16) one = { 1 };
  251. bignum25519 ALIGN(16) u, uminus1, uplus1, inv_uplus1, y;
  252. /* Prop228: y = (u-1)/(u+1) */
  253. curve25519_expand(u, inp);
  254. curve25519_sub(uminus1, u, one);
  255. curve25519_add(uplus1, u, one);
  256. curve25519_recip(inv_uplus1, uplus1);
  257. curve25519_mul(y, uminus1, inv_uplus1);
  258. curve25519_contract(out, y);
  259. /* Propagate sign. */
  260. out[31] |= (!!signbit) << 7;
  261. return 0;
  262. }
  263. #include "test-internals.c"