curve25519-jni.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema Java Client
  7. * Copyright (c) 2015-2020 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <string.h>
  22. #include <jni.h>
  23. int crypto_scalarmult(unsigned char *q,
  24. const unsigned char *n,
  25. const unsigned char *p);
  26. JNIEXPORT jint JNICALL Java_com_neilalexander_jnacl_crypto_curve25519_crypto_1scalarmult_1native(JNIEnv* env, jclass cls,
  27. jbyteArray qarr, jbyteArray narr, jbyteArray parr) {
  28. jbyte q[32], n[32], p[32];
  29. int res;
  30. (*env)->GetByteArrayRegion(env, narr, 0, 32, n);
  31. (*env)->GetByteArrayRegion(env, parr, 0, 32, p);
  32. res = crypto_scalarmult((unsigned char *)q, (unsigned char *)n, (unsigned char *)p);
  33. (*env)->SetByteArrayRegion(env, qarr, 0, 32, q);
  34. return res;
  35. }
  36. /* Public Domain code copied verbatim from NaCl below */
  37. static void add(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  38. {
  39. unsigned int j;
  40. unsigned int u;
  41. u = 0;
  42. for (j = 0;j < 31;++j) { u += a[j] + b[j]; out[j] = u & 255; u >>= 8; }
  43. u += a[31] + b[31]; out[31] = u;
  44. }
  45. static void sub(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  46. {
  47. unsigned int j;
  48. unsigned int u;
  49. u = 218;
  50. for (j = 0;j < 31;++j) {
  51. u += a[j] + 65280 - b[j];
  52. out[j] = u & 255;
  53. u >>= 8;
  54. }
  55. u += a[31] - b[31];
  56. out[31] = u;
  57. }
  58. static void squeeze(unsigned int a[32])
  59. {
  60. unsigned int j;
  61. unsigned int u;
  62. u = 0;
  63. for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
  64. u += a[31]; a[31] = u & 127;
  65. u = 19 * (u >> 7);
  66. for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
  67. u += a[31]; a[31] = u;
  68. }
  69. static const unsigned int minusp[32] = {
  70. 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128
  71. } ;
  72. static void freeze(unsigned int a[32])
  73. {
  74. unsigned int aorig[32];
  75. unsigned int j;
  76. unsigned int negative;
  77. for (j = 0;j < 32;++j) aorig[j] = a[j];
  78. add(a,a,minusp);
  79. negative = -((a[31] >> 7) & 1);
  80. for (j = 0;j < 32;++j) a[j] ^= negative & (aorig[j] ^ a[j]);
  81. }
  82. static void mult(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  83. {
  84. unsigned int i;
  85. unsigned int j;
  86. unsigned int u;
  87. for (i = 0;i < 32;++i) {
  88. u = 0;
  89. for (j = 0;j <= i;++j) u += a[j] * b[i - j];
  90. for (j = i + 1;j < 32;++j) u += 38 * a[j] * b[i + 32 - j];
  91. out[i] = u;
  92. }
  93. squeeze(out);
  94. }
  95. static void mult121665(unsigned int out[32],const unsigned int a[32])
  96. {
  97. unsigned int j;
  98. unsigned int u;
  99. u = 0;
  100. for (j = 0;j < 31;++j) { u += 121665 * a[j]; out[j] = u & 255; u >>= 8; }
  101. u += 121665 * a[31]; out[31] = u & 127;
  102. u = 19 * (u >> 7);
  103. for (j = 0;j < 31;++j) { u += out[j]; out[j] = u & 255; u >>= 8; }
  104. u += out[j]; out[j] = u;
  105. }
  106. static void square(unsigned int out[32],const unsigned int a[32])
  107. {
  108. unsigned int i;
  109. unsigned int j;
  110. unsigned int u;
  111. for (i = 0;i < 32;++i) {
  112. u = 0;
  113. for (j = 0;j < i - j;++j) u += a[j] * a[i - j];
  114. for (j = i + 1;j < i + 32 - j;++j) u += 38 * a[j] * a[i + 32 - j];
  115. u *= 2;
  116. if ((i & 1) == 0) {
  117. u += a[i / 2] * a[i / 2];
  118. u += 38 * a[i / 2 + 16] * a[i / 2 + 16];
  119. }
  120. out[i] = u;
  121. }
  122. squeeze(out);
  123. }
  124. static void select(unsigned int p[64],unsigned int q[64],const unsigned int r[64],const unsigned int s[64],unsigned int b)
  125. {
  126. unsigned int j;
  127. unsigned int t;
  128. unsigned int bminus1;
  129. bminus1 = b - 1;
  130. for (j = 0;j < 64;++j) {
  131. t = bminus1 & (r[j] ^ s[j]);
  132. p[j] = s[j] ^ t;
  133. q[j] = r[j] ^ t;
  134. }
  135. }
  136. static void mainloop(unsigned int work[64],const unsigned char e[32])
  137. {
  138. unsigned int xzm1[64];
  139. unsigned int xzm[64];
  140. unsigned int xzmb[64];
  141. unsigned int xzm1b[64];
  142. unsigned int xznb[64];
  143. unsigned int xzn1b[64];
  144. unsigned int a0[64];
  145. unsigned int a1[64];
  146. unsigned int b0[64];
  147. unsigned int b1[64];
  148. unsigned int c1[64];
  149. unsigned int r[32];
  150. unsigned int s[32];
  151. unsigned int t[32];
  152. unsigned int u[32];
  153. unsigned int i;
  154. unsigned int j;
  155. unsigned int b;
  156. int pos;
  157. for (j = 0;j < 32;++j) xzm1[j] = work[j];
  158. xzm1[32] = 1;
  159. for (j = 33;j < 64;++j) xzm1[j] = 0;
  160. xzm[0] = 1;
  161. for (j = 1;j < 64;++j) xzm[j] = 0;
  162. for (pos = 254;pos >= 0;--pos) {
  163. b = e[pos / 8] >> (pos & 7);
  164. b &= 1;
  165. select(xzmb,xzm1b,xzm,xzm1,b);
  166. add(a0,xzmb,xzmb + 32);
  167. sub(a0 + 32,xzmb,xzmb + 32);
  168. add(a1,xzm1b,xzm1b + 32);
  169. sub(a1 + 32,xzm1b,xzm1b + 32);
  170. square(b0,a0);
  171. square(b0 + 32,a0 + 32);
  172. mult(b1,a1,a0 + 32);
  173. mult(b1 + 32,a1 + 32,a0);
  174. add(c1,b1,b1 + 32);
  175. sub(c1 + 32,b1,b1 + 32);
  176. square(r,c1 + 32);
  177. sub(s,b0,b0 + 32);
  178. mult121665(t,s);
  179. add(u,t,b0);
  180. mult(xznb,b0,b0 + 32);
  181. mult(xznb + 32,s,u);
  182. square(xzn1b,c1);
  183. mult(xzn1b + 32,r,work);
  184. select(xzm,xzm1,xznb,xzn1b,b);
  185. }
  186. for (j = 0;j < 64;++j) work[j] = xzm[j];
  187. }
  188. static void recip(unsigned int out[32],const unsigned int z[32])
  189. {
  190. unsigned int z2[32];
  191. unsigned int z9[32];
  192. unsigned int z11[32];
  193. unsigned int z2_5_0[32];
  194. unsigned int z2_10_0[32];
  195. unsigned int z2_20_0[32];
  196. unsigned int z2_50_0[32];
  197. unsigned int z2_100_0[32];
  198. unsigned int t0[32];
  199. unsigned int t1[32];
  200. int i;
  201. /* 2 */ square(z2,z);
  202. /* 4 */ square(t1,z2);
  203. /* 8 */ square(t0,t1);
  204. /* 9 */ mult(z9,t0,z);
  205. /* 11 */ mult(z11,z9,z2);
  206. /* 22 */ square(t0,z11);
  207. /* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
  208. /* 2^6 - 2^1 */ square(t0,z2_5_0);
  209. /* 2^7 - 2^2 */ square(t1,t0);
  210. /* 2^8 - 2^3 */ square(t0,t1);
  211. /* 2^9 - 2^4 */ square(t1,t0);
  212. /* 2^10 - 2^5 */ square(t0,t1);
  213. /* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
  214. /* 2^11 - 2^1 */ square(t0,z2_10_0);
  215. /* 2^12 - 2^2 */ square(t1,t0);
  216. /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
  217. /* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
  218. /* 2^21 - 2^1 */ square(t0,z2_20_0);
  219. /* 2^22 - 2^2 */ square(t1,t0);
  220. /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
  221. /* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
  222. /* 2^41 - 2^1 */ square(t1,t0);
  223. /* 2^42 - 2^2 */ square(t0,t1);
  224. /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
  225. /* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
  226. /* 2^51 - 2^1 */ square(t0,z2_50_0);
  227. /* 2^52 - 2^2 */ square(t1,t0);
  228. /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  229. /* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
  230. /* 2^101 - 2^1 */ square(t1,z2_100_0);
  231. /* 2^102 - 2^2 */ square(t0,t1);
  232. /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
  233. /* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
  234. /* 2^201 - 2^1 */ square(t0,t1);
  235. /* 2^202 - 2^2 */ square(t1,t0);
  236. /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  237. /* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
  238. /* 2^251 - 2^1 */ square(t1,t0);
  239. /* 2^252 - 2^2 */ square(t0,t1);
  240. /* 2^253 - 2^3 */ square(t1,t0);
  241. /* 2^254 - 2^4 */ square(t0,t1);
  242. /* 2^255 - 2^5 */ square(t1,t0);
  243. /* 2^255 - 21 */ mult(out,t1,z11);
  244. }
  245. int crypto_scalarmult(unsigned char *q,
  246. const unsigned char *n,
  247. const unsigned char *p)
  248. {
  249. unsigned int work[96];
  250. unsigned char e[32];
  251. unsigned int i;
  252. for (i = 0;i < 32;++i) e[i] = n[i];
  253. e[0] &= 248;
  254. e[31] &= 127;
  255. e[31] |= 64;
  256. for (i = 0;i < 32;++i) work[i] = p[i];
  257. mainloop(work,e);
  258. recip(work + 32,work + 32);
  259. mult(work + 64,work,work + 32);
  260. freeze(work + 64);
  261. for (i = 0;i < 32;++i) q[i] = work[64 + i];
  262. return 0;
  263. }