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