curve25519-donna-c64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* Copyright 2008, Google Inc.
  2. * All rights reserved.
  3. *
  4. * Code released into the public domain.
  5. *
  6. * curve25519-donna: Curve25519 elliptic curve, public key function
  7. *
  8. * http://code.google.com/p/curve25519-donna/
  9. *
  10. * Adam Langley <agl@imperialviolet.org>
  11. *
  12. * Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
  13. *
  14. * More information about curve25519 can be found here
  15. * http://cr.yp.to/ecdh.html
  16. *
  17. * djb's sample implementation of curve25519 is written in a special assembly
  18. * language called qhasm and uses the floating point registers.
  19. *
  20. * This is, almost, a clean room reimplementation from the curve25519 paper. It
  21. * uses many of the tricks described therein. Only the crecip function is taken
  22. * from the sample implementation.
  23. */
  24. #include <string.h>
  25. #include <stdint.h>
  26. typedef uint8_t u8;
  27. typedef uint64_t limb;
  28. typedef limb felem[5];
  29. // This is a special gcc mode for 128-bit integers. It's implemented on 64-bit
  30. // platforms only as far as I know.
  31. typedef unsigned uint128_t __attribute__((mode(TI)));
  32. #undef force_inline
  33. #define force_inline __attribute__((always_inline))
  34. /* Sum two numbers: output += in */
  35. static void force_inline
  36. fsum(limb *output, const limb *in) {
  37. output[0] += in[0];
  38. output[1] += in[1];
  39. output[2] += in[2];
  40. output[3] += in[3];
  41. output[4] += in[4];
  42. }
  43. /* Find the difference of two numbers: output = in - output
  44. * (note the order of the arguments!)
  45. *
  46. * Assumes that out[i] < 2**52
  47. * On return, out[i] < 2**55
  48. */
  49. static void force_inline
  50. fdifference_backwards(felem out, const felem in) {
  51. /* 152 is 19 << 3 */
  52. static const limb two54m152 = (((limb)1) << 54) - 152;
  53. static const limb two54m8 = (((limb)1) << 54) - 8;
  54. out[0] = in[0] + two54m152 - out[0];
  55. out[1] = in[1] + two54m8 - out[1];
  56. out[2] = in[2] + two54m8 - out[2];
  57. out[3] = in[3] + two54m8 - out[3];
  58. out[4] = in[4] + two54m8 - out[4];
  59. }
  60. /* Multiply a number by a scalar: output = in * scalar */
  61. static void force_inline
  62. fscalar_product(felem output, const felem in, const limb scalar) {
  63. uint128_t a;
  64. a = ((uint128_t) in[0]) * scalar;
  65. output[0] = ((limb)a) & 0x7ffffffffffff;
  66. a = ((uint128_t) in[1]) * scalar + ((limb) (a >> 51));
  67. output[1] = ((limb)a) & 0x7ffffffffffff;
  68. a = ((uint128_t) in[2]) * scalar + ((limb) (a >> 51));
  69. output[2] = ((limb)a) & 0x7ffffffffffff;
  70. a = ((uint128_t) in[3]) * scalar + ((limb) (a >> 51));
  71. output[3] = ((limb)a) & 0x7ffffffffffff;
  72. a = ((uint128_t) in[4]) * scalar + ((limb) (a >> 51));
  73. output[4] = ((limb)a) & 0x7ffffffffffff;
  74. output[0] += (a >> 51) * 19;
  75. }
  76. /* Multiply two numbers: output = in2 * in
  77. *
  78. * output must be distinct to both inputs. The inputs are reduced coefficient
  79. * form, the output is not.
  80. *
  81. * Assumes that in[i] < 2**55 and likewise for in2.
  82. * On return, output[i] < 2**52
  83. */
  84. static void force_inline
  85. fmul(felem output, const felem in2, const felem in) {
  86. uint128_t t[5];
  87. limb r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c;
  88. r0 = in[0];
  89. r1 = in[1];
  90. r2 = in[2];
  91. r3 = in[3];
  92. r4 = in[4];
  93. s0 = in2[0];
  94. s1 = in2[1];
  95. s2 = in2[2];
  96. s3 = in2[3];
  97. s4 = in2[4];
  98. t[0] = ((uint128_t) r0) * s0;
  99. t[1] = ((uint128_t) r0) * s1 + ((uint128_t) r1) * s0;
  100. t[2] = ((uint128_t) r0) * s2 + ((uint128_t) r2) * s0 + ((uint128_t) r1) * s1;
  101. t[3] = ((uint128_t) r0) * s3 + ((uint128_t) r3) * s0 + ((uint128_t) r1) * s2 + ((uint128_t) r2) * s1;
  102. t[4] = ((uint128_t) r0) * s4 + ((uint128_t) r4) * s0 + ((uint128_t) r3) * s1 + ((uint128_t) r1) * s3 + ((uint128_t) r2) * s2;
  103. r4 *= 19;
  104. r1 *= 19;
  105. r2 *= 19;
  106. r3 *= 19;
  107. t[0] += ((uint128_t) r4) * s1 + ((uint128_t) r1) * s4 + ((uint128_t) r2) * s3 + ((uint128_t) r3) * s2;
  108. t[1] += ((uint128_t) r4) * s2 + ((uint128_t) r2) * s4 + ((uint128_t) r3) * s3;
  109. t[2] += ((uint128_t) r4) * s3 + ((uint128_t) r3) * s4;
  110. t[3] += ((uint128_t) r4) * s4;
  111. r0 = (limb)t[0] & 0x7ffffffffffff; c = (limb)(t[0] >> 51);
  112. t[1] += c; r1 = (limb)t[1] & 0x7ffffffffffff; c = (limb)(t[1] >> 51);
  113. t[2] += c; r2 = (limb)t[2] & 0x7ffffffffffff; c = (limb)(t[2] >> 51);
  114. t[3] += c; r3 = (limb)t[3] & 0x7ffffffffffff; c = (limb)(t[3] >> 51);
  115. t[4] += c; r4 = (limb)t[4] & 0x7ffffffffffff; c = (limb)(t[4] >> 51);
  116. r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffff;
  117. r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffff;
  118. r2 += c;
  119. output[0] = r0;
  120. output[1] = r1;
  121. output[2] = r2;
  122. output[3] = r3;
  123. output[4] = r4;
  124. }
  125. static void force_inline
  126. fsquare_times(felem output, const felem in, limb count) {
  127. uint128_t t[5];
  128. limb r0,r1,r2,r3,r4,c;
  129. limb d0,d1,d2,d4,d419;
  130. r0 = in[0];
  131. r1 = in[1];
  132. r2 = in[2];
  133. r3 = in[3];
  134. r4 = in[4];
  135. do {
  136. d0 = r0 * 2;
  137. d1 = r1 * 2;
  138. d2 = r2 * 2 * 19;
  139. d419 = r4 * 19;
  140. d4 = d419 * 2;
  141. t[0] = ((uint128_t) r0) * r0 + ((uint128_t) d4) * r1 + (((uint128_t) d2) * (r3 ));
  142. t[1] = ((uint128_t) d0) * r1 + ((uint128_t) d4) * r2 + (((uint128_t) r3) * (r3 * 19));
  143. t[2] = ((uint128_t) d0) * r2 + ((uint128_t) r1) * r1 + (((uint128_t) d4) * (r3 ));
  144. t[3] = ((uint128_t) d0) * r3 + ((uint128_t) d1) * r2 + (((uint128_t) r4) * (d419 ));
  145. t[4] = ((uint128_t) d0) * r4 + ((uint128_t) d1) * r3 + (((uint128_t) r2) * (r2 ));
  146. r0 = (limb)t[0] & 0x7ffffffffffff; c = (limb)(t[0] >> 51);
  147. t[1] += c; r1 = (limb)t[1] & 0x7ffffffffffff; c = (limb)(t[1] >> 51);
  148. t[2] += c; r2 = (limb)t[2] & 0x7ffffffffffff; c = (limb)(t[2] >> 51);
  149. t[3] += c; r3 = (limb)t[3] & 0x7ffffffffffff; c = (limb)(t[3] >> 51);
  150. t[4] += c; r4 = (limb)t[4] & 0x7ffffffffffff; c = (limb)(t[4] >> 51);
  151. r0 += c * 19; c = r0 >> 51; r0 = r0 & 0x7ffffffffffff;
  152. r1 += c; c = r1 >> 51; r1 = r1 & 0x7ffffffffffff;
  153. r2 += c;
  154. } while(--count);
  155. output[0] = r0;
  156. output[1] = r1;
  157. output[2] = r2;
  158. output[3] = r3;
  159. output[4] = r4;
  160. }
  161. /* Take a little-endian, 32-byte number and expand it into polynomial form */
  162. static void
  163. fexpand(limb *output, const u8 *in) {
  164. output[0] = *((const uint64_t *)(in)) & 0x7ffffffffffff;
  165. output[1] = (*((const uint64_t *)(in+6)) >> 3) & 0x7ffffffffffff;
  166. output[2] = (*((const uint64_t *)(in+12)) >> 6) & 0x7ffffffffffff;
  167. output[3] = (*((const uint64_t *)(in+19)) >> 1) & 0x7ffffffffffff;
  168. output[4] = (*((const uint64_t *)(in+24)) >> 12) & 0x7ffffffffffff;
  169. }
  170. /* Take a fully reduced polynomial form number and contract it into a
  171. * little-endian, 32-byte array
  172. */
  173. static void
  174. fcontract(u8 *output, const felem input) {
  175. uint128_t t[5];
  176. t[0] = input[0];
  177. t[1] = input[1];
  178. t[2] = input[2];
  179. t[3] = input[3];
  180. t[4] = input[4];
  181. t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
  182. t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
  183. t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
  184. t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
  185. t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff;
  186. t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
  187. t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
  188. t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
  189. t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
  190. t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff;
  191. /* now t is between 0 and 2^255-1, properly carried. */
  192. /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
  193. t[0] += 19;
  194. t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
  195. t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
  196. t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
  197. t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
  198. t[0] += 19 * (t[4] >> 51); t[4] &= 0x7ffffffffffff;
  199. /* now between 19 and 2^255-1 in both cases, and offset by 19. */
  200. t[0] += 0x8000000000000 - 19;
  201. t[1] += 0x8000000000000 - 1;
  202. t[2] += 0x8000000000000 - 1;
  203. t[3] += 0x8000000000000 - 1;
  204. t[4] += 0x8000000000000 - 1;
  205. /* now between 2^255 and 2^256-20, and offset by 2^255. */
  206. t[1] += t[0] >> 51; t[0] &= 0x7ffffffffffff;
  207. t[2] += t[1] >> 51; t[1] &= 0x7ffffffffffff;
  208. t[3] += t[2] >> 51; t[2] &= 0x7ffffffffffff;
  209. t[4] += t[3] >> 51; t[3] &= 0x7ffffffffffff;
  210. t[4] &= 0x7ffffffffffff;
  211. *((uint64_t *)(output)) = t[0] | (t[1] << 51);
  212. *((uint64_t *)(output+8)) = (t[1] >> 13) | (t[2] << 38);
  213. *((uint64_t *)(output+16)) = (t[2] >> 26) | (t[3] << 25);
  214. *((uint64_t *)(output+24)) = (t[3] >> 39) | (t[4] << 12);
  215. }
  216. /* Input: Q, Q', Q-Q'
  217. * Output: 2Q, Q+Q'
  218. *
  219. * x2 z3: long form
  220. * x3 z3: long form
  221. * x z: short form, destroyed
  222. * xprime zprime: short form, destroyed
  223. * qmqp: short form, preserved
  224. */
  225. static void
  226. fmonty(limb *x2, limb *z2, /* output 2Q */
  227. limb *x3, limb *z3, /* output Q + Q' */
  228. limb *x, limb *z, /* input Q */
  229. limb *xprime, limb *zprime, /* input Q' */
  230. const limb *qmqp /* input Q - Q' */) {
  231. limb origx[5], origxprime[5], zzz[5], xx[5], zz[5], xxprime[5],
  232. zzprime[5], zzzprime[5];
  233. memcpy(origx, x, 5 * sizeof(limb));
  234. fsum(x, z);
  235. fdifference_backwards(z, origx); // does x - z
  236. memcpy(origxprime, xprime, sizeof(limb) * 5);
  237. fsum(xprime, zprime);
  238. fdifference_backwards(zprime, origxprime);
  239. fmul(xxprime, xprime, z);
  240. fmul(zzprime, x, zprime);
  241. memcpy(origxprime, xxprime, sizeof(limb) * 5);
  242. fsum(xxprime, zzprime);
  243. fdifference_backwards(zzprime, origxprime);
  244. fsquare_times(x3, xxprime, 1);
  245. fsquare_times(zzzprime, zzprime, 1);
  246. fmul(z3, zzzprime, qmqp);
  247. fsquare_times(xx, x, 1);
  248. fsquare_times(zz, z, 1);
  249. fmul(x2, xx, zz);
  250. fdifference_backwards(zz, xx); // does zz = xx - zz
  251. fscalar_product(zzz, zz, 121665);
  252. fsum(zzz, xx);
  253. fmul(z2, zz, zzz);
  254. }
  255. // -----------------------------------------------------------------------------
  256. // Maybe swap the contents of two limb arrays (@a and @b), each @len elements
  257. // long. Perform the swap iff @swap is non-zero.
  258. //
  259. // This function performs the swap without leaking any side-channel
  260. // information.
  261. // -----------------------------------------------------------------------------
  262. static void
  263. swap_conditional(limb a[5], limb b[5], limb iswap) {
  264. unsigned i;
  265. const limb swap = -iswap;
  266. for (i = 0; i < 5; ++i) {
  267. const limb x = swap & (a[i] ^ b[i]);
  268. a[i] ^= x;
  269. b[i] ^= x;
  270. }
  271. }
  272. /* Calculates nQ where Q is the x-coordinate of a point on the curve
  273. *
  274. * resultx/resultz: the x coordinate of the resulting curve point (short form)
  275. * n: a little endian, 32-byte number
  276. * q: a point of the curve (short form)
  277. */
  278. static void
  279. cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q) {
  280. limb a[5] = {0}, b[5] = {1}, c[5] = {1}, d[5] = {0};
  281. limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t;
  282. limb e[5] = {0}, f[5] = {1}, g[5] = {0}, h[5] = {1};
  283. limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h;
  284. unsigned i, j;
  285. memcpy(nqpqx, q, sizeof(limb) * 5);
  286. for (i = 0; i < 32; ++i) {
  287. u8 byte = n[31 - i];
  288. for (j = 0; j < 8; ++j) {
  289. const limb bit = byte >> 7;
  290. swap_conditional(nqx, nqpqx, bit);
  291. swap_conditional(nqz, nqpqz, bit);
  292. fmonty(nqx2, nqz2,
  293. nqpqx2, nqpqz2,
  294. nqx, nqz,
  295. nqpqx, nqpqz,
  296. q);
  297. swap_conditional(nqx2, nqpqx2, bit);
  298. swap_conditional(nqz2, nqpqz2, bit);
  299. t = nqx;
  300. nqx = nqx2;
  301. nqx2 = t;
  302. t = nqz;
  303. nqz = nqz2;
  304. nqz2 = t;
  305. t = nqpqx;
  306. nqpqx = nqpqx2;
  307. nqpqx2 = t;
  308. t = nqpqz;
  309. nqpqz = nqpqz2;
  310. nqpqz2 = t;
  311. byte <<= 1;
  312. }
  313. }
  314. memcpy(resultx, nqx, sizeof(limb) * 5);
  315. memcpy(resultz, nqz, sizeof(limb) * 5);
  316. }
  317. // -----------------------------------------------------------------------------
  318. // Shamelessly copied from djb's code, tightened a little
  319. // -----------------------------------------------------------------------------
  320. static void
  321. crecip(felem out, const felem z) {
  322. felem a,t0,b,c;
  323. /* 2 */ fsquare_times(a, z, 1); // a = 2
  324. /* 8 */ fsquare_times(t0, a, 2);
  325. /* 9 */ fmul(b, t0, z); // b = 9
  326. /* 11 */ fmul(a, b, a); // a = 11
  327. /* 22 */ fsquare_times(t0, a, 1);
  328. /* 2^5 - 2^0 = 31 */ fmul(b, t0, b);
  329. /* 2^10 - 2^5 */ fsquare_times(t0, b, 5);
  330. /* 2^10 - 2^0 */ fmul(b, t0, b);
  331. /* 2^20 - 2^10 */ fsquare_times(t0, b, 10);
  332. /* 2^20 - 2^0 */ fmul(c, t0, b);
  333. /* 2^40 - 2^20 */ fsquare_times(t0, c, 20);
  334. /* 2^40 - 2^0 */ fmul(t0, t0, c);
  335. /* 2^50 - 2^10 */ fsquare_times(t0, t0, 10);
  336. /* 2^50 - 2^0 */ fmul(b, t0, b);
  337. /* 2^100 - 2^50 */ fsquare_times(t0, b, 50);
  338. /* 2^100 - 2^0 */ fmul(c, t0, b);
  339. /* 2^200 - 2^100 */ fsquare_times(t0, c, 100);
  340. /* 2^200 - 2^0 */ fmul(t0, t0, c);
  341. /* 2^250 - 2^50 */ fsquare_times(t0, t0, 50);
  342. /* 2^250 - 2^0 */ fmul(t0, t0, b);
  343. /* 2^255 - 2^5 */ fsquare_times(t0, t0, 5);
  344. /* 2^255 - 21 */ fmul(out, t0, a);
  345. }
  346. int
  347. curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) {
  348. limb bp[5], x[5], z[5], zmone[5];
  349. uint8_t e[32];
  350. int i;
  351. for (i = 0;i < 32;++i) e[i] = secret[i];
  352. e[0] &= 248;
  353. e[31] &= 127;
  354. e[31] |= 64;
  355. fexpand(bp, basepoint);
  356. cmult(x, z, e, bp);
  357. crecip(zmone, z);
  358. fmul(z, x, zmone);
  359. fcontract(mypublic, z);
  360. return 0;
  361. }