C25519.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "Utils.hpp"
  31. #include "C25519.hpp"
  32. #include "SHA512.hpp"
  33. namespace ZeroTier {
  34. //////////////////////////////////////////////////////////////////////////////
  35. //////////////////////////////////////////////////////////////////////////////
  36. // Code taken from NaCl by D. J. Bernstein and others
  37. /*
  38. Matthew Dempsky
  39. Public domain.
  40. Derived from public domain code by D. J. Bernstein.
  41. */
  42. static void add(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  43. {
  44. unsigned int j;
  45. unsigned int u;
  46. u = 0;
  47. for (j = 0;j < 31;++j) { u += a[j] + b[j]; out[j] = u & 255; u >>= 8; }
  48. u += a[31] + b[31]; out[31] = u;
  49. }
  50. static void sub(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  51. {
  52. unsigned int j;
  53. unsigned int u;
  54. u = 218;
  55. for (j = 0;j < 31;++j) {
  56. u += a[j] + 65280 - b[j];
  57. out[j] = u & 255;
  58. u >>= 8;
  59. }
  60. u += a[31] - b[31];
  61. out[31] = u;
  62. }
  63. static void squeeze(unsigned int a[32])
  64. {
  65. unsigned int j;
  66. unsigned int u;
  67. u = 0;
  68. for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
  69. u += a[31]; a[31] = u & 127;
  70. u = 19 * (u >> 7);
  71. for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
  72. u += a[31]; a[31] = u;
  73. }
  74. static const unsigned int minusp[32] = {
  75. 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
  76. } ;
  77. static void freeze(unsigned int a[32])
  78. {
  79. unsigned int aorig[32];
  80. unsigned int j;
  81. unsigned int negative;
  82. for (j = 0;j < 32;++j) aorig[j] = a[j];
  83. add(a,a,minusp);
  84. negative = -((a[31] >> 7) & 1);
  85. for (j = 0;j < 32;++j) a[j] ^= negative & (aorig[j] ^ a[j]);
  86. }
  87. static void mult(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
  88. {
  89. unsigned int i;
  90. unsigned int j;
  91. unsigned int u;
  92. for (i = 0;i < 32;++i) {
  93. u = 0;
  94. for (j = 0;j <= i;++j) u += a[j] * b[i - j];
  95. for (j = i + 1;j < 32;++j) u += 38 * a[j] * b[i + 32 - j];
  96. out[i] = u;
  97. }
  98. squeeze(out);
  99. }
  100. static void mult121665(unsigned int out[32],const unsigned int a[32])
  101. {
  102. unsigned int j;
  103. unsigned int u;
  104. u = 0;
  105. for (j = 0;j < 31;++j) { u += 121665 * a[j]; out[j] = u & 255; u >>= 8; }
  106. u += 121665 * a[31]; out[31] = u & 127;
  107. u = 19 * (u >> 7);
  108. for (j = 0;j < 31;++j) { u += out[j]; out[j] = u & 255; u >>= 8; }
  109. u += out[j]; out[j] = u;
  110. }
  111. static void square(unsigned int out[32],const unsigned int a[32])
  112. {
  113. unsigned int i;
  114. unsigned int j;
  115. unsigned int u;
  116. for (i = 0;i < 32;++i) {
  117. u = 0;
  118. for (j = 0;j < i - j;++j) u += a[j] * a[i - j];
  119. for (j = i + 1;j < i + 32 - j;++j) u += 38 * a[j] * a[i + 32 - j];
  120. u *= 2;
  121. if ((i & 1) == 0) {
  122. u += a[i / 2] * a[i / 2];
  123. u += 38 * a[i / 2 + 16] * a[i / 2 + 16];
  124. }
  125. out[i] = u;
  126. }
  127. squeeze(out);
  128. }
  129. static void select(unsigned int p[64],unsigned int q[64],const unsigned int r[64],const unsigned int s[64],unsigned int b)
  130. {
  131. unsigned int j;
  132. unsigned int t;
  133. unsigned int bminus1;
  134. bminus1 = b - 1;
  135. for (j = 0;j < 64;++j) {
  136. t = bminus1 & (r[j] ^ s[j]);
  137. p[j] = s[j] ^ t;
  138. q[j] = r[j] ^ t;
  139. }
  140. }
  141. static void mainloop(unsigned int work[64],const unsigned char e[32])
  142. {
  143. unsigned int xzm1[64];
  144. unsigned int xzm[64];
  145. unsigned int xzmb[64];
  146. unsigned int xzm1b[64];
  147. unsigned int xznb[64];
  148. unsigned int xzn1b[64];
  149. unsigned int a0[64];
  150. unsigned int a1[64];
  151. unsigned int b0[64];
  152. unsigned int b1[64];
  153. unsigned int c1[64];
  154. unsigned int r[32];
  155. unsigned int s[32];
  156. unsigned int t[32];
  157. unsigned int u[32];
  158. //unsigned int i;
  159. unsigned int j;
  160. unsigned int b;
  161. int pos;
  162. for (j = 0;j < 32;++j) xzm1[j] = work[j];
  163. xzm1[32] = 1;
  164. for (j = 33;j < 64;++j) xzm1[j] = 0;
  165. xzm[0] = 1;
  166. for (j = 1;j < 64;++j) xzm[j] = 0;
  167. for (pos = 254;pos >= 0;--pos) {
  168. b = e[pos / 8] >> (pos & 7);
  169. b &= 1;
  170. select(xzmb,xzm1b,xzm,xzm1,b);
  171. add(a0,xzmb,xzmb + 32);
  172. sub(a0 + 32,xzmb,xzmb + 32);
  173. add(a1,xzm1b,xzm1b + 32);
  174. sub(a1 + 32,xzm1b,xzm1b + 32);
  175. square(b0,a0);
  176. square(b0 + 32,a0 + 32);
  177. mult(b1,a1,a0 + 32);
  178. mult(b1 + 32,a1 + 32,a0);
  179. add(c1,b1,b1 + 32);
  180. sub(c1 + 32,b1,b1 + 32);
  181. square(r,c1 + 32);
  182. sub(s,b0,b0 + 32);
  183. mult121665(t,s);
  184. add(u,t,b0);
  185. mult(xznb,b0,b0 + 32);
  186. mult(xznb + 32,s,u);
  187. square(xzn1b,c1);
  188. mult(xzn1b + 32,r,work);
  189. select(xzm,xzm1,xznb,xzn1b,b);
  190. }
  191. for (j = 0;j < 64;++j) work[j] = xzm[j];
  192. }
  193. static void recip(unsigned int out[32],const unsigned int z[32])
  194. {
  195. unsigned int z2[32];
  196. unsigned int z9[32];
  197. unsigned int z11[32];
  198. unsigned int z2_5_0[32];
  199. unsigned int z2_10_0[32];
  200. unsigned int z2_20_0[32];
  201. unsigned int z2_50_0[32];
  202. unsigned int z2_100_0[32];
  203. unsigned int t0[32];
  204. unsigned int t1[32];
  205. int i;
  206. /* 2 */ square(z2,z);
  207. /* 4 */ square(t1,z2);
  208. /* 8 */ square(t0,t1);
  209. /* 9 */ mult(z9,t0,z);
  210. /* 11 */ mult(z11,z9,z2);
  211. /* 22 */ square(t0,z11);
  212. /* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
  213. /* 2^6 - 2^1 */ square(t0,z2_5_0);
  214. /* 2^7 - 2^2 */ square(t1,t0);
  215. /* 2^8 - 2^3 */ square(t0,t1);
  216. /* 2^9 - 2^4 */ square(t1,t0);
  217. /* 2^10 - 2^5 */ square(t0,t1);
  218. /* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
  219. /* 2^11 - 2^1 */ square(t0,z2_10_0);
  220. /* 2^12 - 2^2 */ square(t1,t0);
  221. /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
  222. /* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
  223. /* 2^21 - 2^1 */ square(t0,z2_20_0);
  224. /* 2^22 - 2^2 */ square(t1,t0);
  225. /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
  226. /* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
  227. /* 2^41 - 2^1 */ square(t1,t0);
  228. /* 2^42 - 2^2 */ square(t0,t1);
  229. /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
  230. /* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
  231. /* 2^51 - 2^1 */ square(t0,z2_50_0);
  232. /* 2^52 - 2^2 */ square(t1,t0);
  233. /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  234. /* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
  235. /* 2^101 - 2^1 */ square(t1,z2_100_0);
  236. /* 2^102 - 2^2 */ square(t0,t1);
  237. /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
  238. /* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
  239. /* 2^201 - 2^1 */ square(t0,t1);
  240. /* 2^202 - 2^2 */ square(t1,t0);
  241. /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
  242. /* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
  243. /* 2^251 - 2^1 */ square(t1,t0);
  244. /* 2^252 - 2^2 */ square(t0,t1);
  245. /* 2^253 - 2^3 */ square(t1,t0);
  246. /* 2^254 - 2^4 */ square(t0,t1);
  247. /* 2^255 - 2^5 */ square(t1,t0);
  248. /* 2^255 - 21 */ mult(out,t1,z11);
  249. }
  250. static int crypto_scalarmult(unsigned char *q,
  251. const unsigned char *n,
  252. const unsigned char *p)
  253. {
  254. unsigned int work[96];
  255. unsigned char e[32];
  256. unsigned int i;
  257. for (i = 0;i < 32;++i) e[i] = n[i];
  258. e[0] &= 248;
  259. e[31] &= 127;
  260. e[31] |= 64;
  261. for (i = 0;i < 32;++i) work[i] = p[i];
  262. mainloop(work,e);
  263. recip(work + 32,work + 32);
  264. mult(work + 64,work,work + 32);
  265. freeze(work + 64);
  266. for (i = 0;i < 32;++i) q[i] = work[64 + i];
  267. return 0;
  268. }
  269. static const unsigned char base[32] = {9};
  270. static int crypto_scalarmult_base(unsigned char *q,
  271. const unsigned char *n)
  272. {
  273. return crypto_scalarmult(q,n,base);
  274. }
  275. //////////////////////////////////////////////////////////////////////////////
  276. //////////////////////////////////////////////////////////////////////////////
  277. C25519::Pair C25519::generate()
  278. {
  279. Pair kp;
  280. Utils::getSecureRandom(kp.priv.data,kp.priv.size());
  281. // First 32 bytes of pub and priv are the keys for C25519 key
  282. // agreement. This generates the public portion from the private.
  283. crypto_scalarmult_base(kp.pub.data,kp.priv.data);
  284. return kp;
  285. }
  286. void C25519::agree(const C25519::Pair &mine,const C25519::Public &their,void *keybuf,unsigned int keylen)
  287. {
  288. unsigned char rawkey[32];
  289. unsigned char digest[64];
  290. crypto_scalarmult(rawkey,mine.priv.data,their.data);
  291. SHA512::hash(digest,rawkey,32);
  292. for(unsigned int i=0,k=0;i<keylen;) {
  293. if (k == 64) {
  294. k = 0;
  295. SHA512::hash(digest,digest,64);
  296. }
  297. ((unsigned char *)keybuf)[i++] = digest[k++];
  298. }
  299. }
  300. } // namespace ZeroTier