libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
tweetnacl.c
Go to the documentation of this file.
1 /*
2  Copyright (c) 2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "platform.hpp"
31 #if defined (ZMQ_USE_TWEETNACL)
32 
33 /*
34  Disable warnings for this source only, rather than for the whole
35  codebase when building with C99 (gcc >= 4.2) or with Microsoft's compiler
36 */
37 #if defined __GNUC__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && __STDC_VERSION__ < 201112L
38 # pragma GCC diagnostic ignored "-Wsign-compare"
39 #elif defined _MSC_VER
40 # pragma warning (disable:4018 4244 4146)
41 #endif
42 
43 #include "tweetnacl.h"
44 
45 #define FOR(i,n) for (i = 0;i < n;++i)
46 #define sv static void
47 
48 static const u8
49  _0[16],
50  _9[32] = {9};
51 static const gf
52  gf0,
53  gf1 = {1},
54  _121665 = {0xDB41,1},
55  D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203},
56  D2 = {0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406},
57  X = {0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169},
58  Y = {0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666},
59  I = {0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83};
60 
61 static u32 L32(u32 x,int c) { return (x << c) | ((x&0xffffffff) >> (32 - c)); }
62 
63 static u32 ld32(const u8 *x)
64 {
65  u32 u = x[3];
66  u = (u<<8)|x[2];
67  u = (u<<8)|x[1];
68  return (u<<8)|x[0];
69 }
70 
71 static u64 dl64(const u8 *x)
72 {
73  u64 i,u=0;
74  FOR(i,8) u=(u<<8)|x[i];
75  return u;
76 }
77 
78 sv st32(u8 *x,u32 u)
79 {
80  int i;
81  FOR(i,4) { x[i] = u; u >>= 8; }
82 }
83 
84 sv ts64(u8 *x,u64 u)
85 {
86  int i;
87  for (i = 7;i >= 0;--i) { x[i] = u; u >>= 8; }
88 }
89 
90 static int vn(const u8 *x,const u8 *y,int n)
91 {
92  u32 i,d = 0;
93  FOR(i,n) d |= x[i]^y[i];
94  return (1 & ((d - 1) >> 8)) - 1;
95 }
96 
97 int crypto_verify_16(const u8 *x,const u8 *y)
98 {
99  return vn(x,y,16);
100 }
101 
102 int crypto_verify_32(const u8 *x,const u8 *y)
103 {
104  return vn(x,y,32);
105 }
106 
107 sv core(u8 *out,const u8 *in,const u8 *k,const u8 *c,int h)
108 {
109  u32 w[16],x[16],y[16],t[4];
110  int i,j,m;
111 
112  FOR(i,4) {
113  x[5*i] = ld32(c+4*i);
114  x[1+i] = ld32(k+4*i);
115  x[6+i] = ld32(in+4*i);
116  x[11+i] = ld32(k+16+4*i);
117  }
118 
119  FOR(i,16) y[i] = x[i];
120 
121  FOR(i,20) {
122  FOR(j,4) {
123  FOR(m,4) t[m] = x[(5*j+4*m)%16];
124  t[1] ^= L32(t[0]+t[3], 7);
125  t[2] ^= L32(t[1]+t[0], 9);
126  t[3] ^= L32(t[2]+t[1],13);
127  t[0] ^= L32(t[3]+t[2],18);
128  FOR(m,4) w[4*j+(j+m)%4] = t[m];
129  }
130  FOR(m,16) x[m] = w[m];
131  }
132 
133  if (h) {
134  FOR(i,16) x[i] += y[i];
135  FOR(i,4) {
136  x[5*i] -= ld32(c+4*i);
137  x[6+i] -= ld32(in+4*i);
138  }
139  FOR(i,4) {
140  st32(out+4*i,x[5*i]);
141  st32(out+16+4*i,x[6+i]);
142  }
143  } else
144  FOR(i,16) st32(out + 4 * i,x[i] + y[i]);
145 }
146 
147 int crypto_core_salsa20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
148 {
149  core(out,in,k,c,0);
150  return 0;
151 }
152 
153 int crypto_core_hsalsa20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
154 {
155  core(out,in,k,c,1);
156  return 0;
157 }
158 
159 static const u8 sigma[16] = "expand 32-byte k";
160 
161 int crypto_stream_salsa20_xor(u8 *c,const u8 *m,u64 b,const u8 *n,const u8 *k)
162 {
163  u8 z[16],x[64];
164  u32 u,i;
165  if (!b) return 0;
166  FOR(i,16) z[i] = 0;
167  FOR(i,8) z[i] = n[i];
168  while (b >= 64) {
169  crypto_core_salsa20(x,z,k,sigma);
170  FOR(i,64) c[i] = (m?m[i]:0) ^ x[i];
171  u = 1;
172  for (i = 8;i < 16;++i) {
173  u += (u32) z[i];
174  z[i] = u;
175  u >>= 8;
176  }
177  b -= 64;
178  c += 64;
179  if (m) m += 64;
180  }
181  if (b) {
182  crypto_core_salsa20(x,z,k,sigma);
183  FOR(i,b) c[i] = (m?m[i]:0) ^ x[i];
184  }
185  return 0;
186 }
187 
188 int crypto_stream_salsa20(u8 *c,u64 d,const u8 *n,const u8 *k)
189 {
190  return crypto_stream_salsa20_xor(c,0,d,n,k);
191 }
192 
193 int crypto_stream(u8 *c,u64 d,const u8 *n,const u8 *k)
194 {
195  u8 s[32];
196  crypto_core_hsalsa20(s,n,k,sigma);
197  return crypto_stream_salsa20(c,d,n+16,s);
198 }
199 
200 int crypto_stream_xor(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *k)
201 {
202  u8 s[32];
203  crypto_core_hsalsa20(s,n,k,sigma);
204  return crypto_stream_salsa20_xor(c,m,d,n+16,s);
205 }
206 
207 sv add1305(u32 *h,const u32 *c)
208 {
209  u32 j,u = 0;
210  FOR(j,17) {
211  u += h[j] + c[j];
212  h[j] = u & 255;
213  u >>= 8;
214  }
215 }
216 
217 static const u32 minusp[17] = {
218  5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252
219 } ;
220 
221 int crypto_onetimeauth(u8 *out,const u8 *m,u64 n,const u8 *k)
222 {
223  u32 s,i,j,u,x[17],r[17],h[17],c[17],g[17];
224 
225  FOR(j,17) r[j]=h[j]=0;
226  FOR(j,16) r[j]=k[j];
227  r[3]&=15;
228  r[4]&=252;
229  r[7]&=15;
230  r[8]&=252;
231  r[11]&=15;
232  r[12]&=252;
233  r[15]&=15;
234 
235  while (n > 0) {
236  FOR(j,17) c[j] = 0;
237  for (j = 0;(j < 16) && (j < n);++j) c[j] = m[j];
238  c[j] = 1;
239  m += j; n -= j;
240  add1305(h,c);
241  FOR(i,17) {
242  x[i] = 0;
243  FOR(j,17) x[i] += h[j] * ((j <= i) ? r[i - j] : 320 * r[i + 17 - j]);
244  }
245  FOR(i,17) h[i] = x[i];
246  u = 0;
247  FOR(j,16) {
248  u += h[j];
249  h[j] = u & 255;
250  u >>= 8;
251  }
252  u += h[16]; h[16] = u & 3;
253  u = 5 * (u >> 2);
254  FOR(j,16) {
255  u += h[j];
256  h[j] = u & 255;
257  u >>= 8;
258  }
259  u += h[16]; h[16] = u;
260  }
261 
262  FOR(j,17) g[j] = h[j];
263  add1305(h,minusp);
264  s = -(h[16] >> 7);
265  FOR(j,17) h[j] ^= s & (g[j] ^ h[j]);
266 
267  FOR(j,16) c[j] = k[j + 16];
268  c[16] = 0;
269  add1305(h,c);
270  FOR(j,16) out[j] = h[j];
271  return 0;
272 }
273 
274 int crypto_onetimeauth_verify(const u8 *h,const u8 *m,u64 n,const u8 *k)
275 {
276  u8 x[16];
277  crypto_onetimeauth(x,m,n,k);
278  return crypto_verify_16(h,x);
279 }
280 
281 int crypto_secretbox(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *k)
282 {
283  int i;
284  if (d < 32) return -1;
285  crypto_stream_xor(c,m,d,n,k);
286  crypto_onetimeauth(c + 16,c + 32,d - 32,c);
287  FOR(i,16) c[i] = 0;
288  return 0;
289 }
290 
291 int crypto_secretbox_open(u8 *m,const u8 *c,u64 d,const u8 *n,const u8 *k)
292 {
293  int i;
294  u8 x[32];
295  if (d < 32) return -1;
296  crypto_stream(x,32,n,k);
297  if (crypto_onetimeauth_verify(c + 16,c + 32,d - 32,x) != 0) return -1;
298  crypto_stream_xor(m,c,d,n,k);
299  FOR(i,32) m[i] = 0;
300  return 0;
301 }
302 
303 sv set25519(gf r, const gf a)
304 {
305  int i;
306  FOR(i,16) r[i]=a[i];
307 }
308 
309 sv car25519(gf o)
310 {
311  int i;
312  i64 c;
313  FOR(i,16) {
314  o[i]+=(1LL<<16);
315  c=o[i]>>16;
316  o[(i+1)*(i<15)]+=c-1+37*(c-1)*(i==15);
317  o[i]-=c<<16;
318  }
319 }
320 
321 sv sel25519(gf p,gf q,int b)
322 {
323  i64 t,i,c=~(b-1);
324  FOR(i,16) {
325  t= c&(p[i]^q[i]);
326  p[i]^=t;
327  q[i]^=t;
328  }
329 }
330 
331 sv pack25519(u8 *o,const gf n)
332 {
333  int i,j,b;
334  gf m,t;
335  FOR(i,16) t[i]=n[i];
336  car25519(t);
337  car25519(t);
338  car25519(t);
339  FOR(j,2) {
340  m[0]=t[0]-0xffed;
341  for(i=1;i<15;i++) {
342  m[i]=t[i]-0xffff-((m[i-1]>>16)&1);
343  m[i-1]&=0xffff;
344  }
345  m[15]=t[15]-0x7fff-((m[14]>>16)&1);
346  b=(m[15]>>16)&1;
347  m[14]&=0xffff;
348  sel25519(t,m,1-b);
349  }
350  FOR(i,16) {
351  o[2*i]=t[i]&0xff;
352  o[2*i+1]=t[i]>>8;
353  }
354 }
355 
356 static int neq25519(const gf a, const gf b)
357 {
358  u8 c[32],d[32];
359  pack25519(c,a);
360  pack25519(d,b);
361  return crypto_verify_32(c,d);
362 }
363 
364 static u8 par25519(const gf a)
365 {
366  u8 d[32];
367  pack25519(d,a);
368  return d[0]&1;
369 }
370 
371 sv unpack25519(gf o, const u8 *n)
372 {
373  int i;
374  FOR(i,16) o[i]=n[2*i]+((i64)n[2*i+1]<<8);
375  o[15]&=0x7fff;
376 }
377 
378 sv A(gf o,const gf a,const gf b)
379 {
380  int i;
381  FOR(i,16) o[i]=a[i]+b[i];
382 }
383 
384 sv Z(gf o,const gf a,const gf b)
385 {
386  int i;
387  FOR(i,16) o[i]=a[i]-b[i];
388 }
389 
390 sv M(gf o,const gf a,const gf b)
391 {
392  i64 i,j,t[31];
393  FOR(i,31) t[i]=0;
394  FOR(i,16) FOR(j,16) t[i+j]+=a[i]*b[j];
395  FOR(i,15) t[i]+=38*t[i+16];
396  FOR(i,16) o[i]=t[i];
397  car25519(o);
398  car25519(o);
399 }
400 
401 sv S(gf o,const gf a)
402 {
403  M(o,a,a);
404 }
405 
406 sv inv25519(gf o,const gf i)
407 {
408  gf c;
409  int a;
410  FOR(a,16) c[a]=i[a];
411  for(a=253;a>=0;a--) {
412  S(c,c);
413  if(a!=2&&a!=4) M(c,c,i);
414  }
415  FOR(a,16) o[a]=c[a];
416 }
417 
418 sv pow2523(gf o,const gf i)
419 {
420  gf c;
421  int a;
422  FOR(a,16) c[a]=i[a];
423  for(a=250;a>=0;a--) {
424  S(c,c);
425  if(a!=1) M(c,c,i);
426  }
427  FOR(a,16) o[a]=c[a];
428 }
429 
430 int crypto_scalarmult(u8 *q,const u8 *n,const u8 *p)
431 {
432  u8 z[32];
433  i64 x[80],r,i;
434  gf a,b,c,d,e,f;
435  FOR(i,31) z[i]=n[i];
436  z[31]=(n[31]&127)|64;
437  z[0]&=248;
438  unpack25519(x,p);
439  FOR(i,16) {
440  b[i]=x[i];
441  d[i]=a[i]=c[i]=0;
442  }
443  a[0]=d[0]=1;
444  for(i=254;i>=0;--i) {
445  r=(z[i>>3]>>(i&7))&1;
446  sel25519(a,b,r);
447  sel25519(c,d,r);
448  A(e,a,c);
449  Z(a,a,c);
450  A(c,b,d);
451  Z(b,b,d);
452  S(d,e);
453  S(f,a);
454  M(a,c,a);
455  M(c,b,e);
456  A(e,a,c);
457  Z(a,a,c);
458  S(b,a);
459  Z(c,d,f);
460  M(a,c,_121665);
461  A(a,a,d);
462  M(c,c,a);
463  M(a,d,f);
464  M(d,b,x);
465  S(b,e);
466  sel25519(a,b,r);
467  sel25519(c,d,r);
468  }
469  FOR(i,16) {
470  x[i+16]=a[i];
471  x[i+32]=c[i];
472  x[i+48]=b[i];
473  x[i+64]=d[i];
474  }
475  inv25519(x+32,x+32);
476  M(x+16,x+16,x+32);
477  pack25519(q,x+16);
478  return 0;
479 }
480 
481 int crypto_scalarmult_base(u8 *q,const u8 *n)
482 {
483  return crypto_scalarmult(q,n,_9);
484 }
485 
486 int crypto_box_keypair(u8 *y,u8 *x)
487 {
488  randombytes(x,32);
489  return crypto_scalarmult_base(y,x);
490 }
491 
492 int crypto_box_beforenm(u8 *k,const u8 *y,const u8 *x)
493 {
494  u8 s[32];
495  crypto_scalarmult(s,x,y);
496  return crypto_core_hsalsa20(k,_0,s,sigma);
497 }
498 
499 int crypto_box_afternm(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *k)
500 {
501  return crypto_secretbox(c,m,d,n,k);
502 }
503 
504 int crypto_box_open_afternm(u8 *m,const u8 *c,u64 d,const u8 *n,const u8 *k)
505 {
506  return crypto_secretbox_open(m,c,d,n,k);
507 }
508 
509 int crypto_box(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *y,const u8 *x)
510 {
511  u8 k[32];
512  crypto_box_beforenm(k,y,x);
513  return crypto_box_afternm(c,m,d,n,k);
514 }
515 
516 int crypto_box_open(u8 *m,const u8 *c,u64 d,const u8 *n,const u8 *y,const u8 *x)
517 {
518  u8 k[32];
519  crypto_box_beforenm(k,y,x);
520  return crypto_box_open_afternm(m,c,d,n,k);
521 }
522 
523 static u64 R(u64 x,int c) { return (x >> c) | (x << (64 - c)); }
524 static u64 Ch(u64 x,u64 y,u64 z) { return (x & y) ^ (~x & z); }
525 static u64 Maj(u64 x,u64 y,u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
526 static u64 Sigma0(u64 x) { return R(x,28) ^ R(x,34) ^ R(x,39); }
527 static u64 Sigma1(u64 x) { return R(x,14) ^ R(x,18) ^ R(x,41); }
528 static u64 sigma0(u64 x) { return R(x, 1) ^ R(x, 8) ^ (x >> 7); }
529 static u64 sigma1(u64 x) { return R(x,19) ^ R(x,61) ^ (x >> 6); }
530 
531 static const u64 K[80] =
532 {
533  0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
534  0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
535  0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
536  0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
537  0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
538  0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
539  0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
540  0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
541  0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
542  0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
543  0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
544  0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
545  0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
546  0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
547  0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
548  0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
549  0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
550  0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
551  0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
552  0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
553 };
554 
555 int crypto_hashblocks(u8 *x,const u8 *m,u64 n)
556 {
557  u64 z[8],b[8],a[8],w[16],t;
558  int i,j;
559 
560  FOR(i,8) z[i] = a[i] = dl64(x + 8 * i);
561 
562  while (n >= 128) {
563  FOR(i,16) w[i] = dl64(m + 8 * i);
564 
565  FOR(i,80) {
566  FOR(j,8) b[j] = a[j];
567  t = a[7] + Sigma1(a[4]) + Ch(a[4],a[5],a[6]) + K[i] + w[i%16];
568  b[7] = t + Sigma0(a[0]) + Maj(a[0],a[1],a[2]);
569  b[3] += t;
570  FOR(j,8) a[(j+1)%8] = b[j];
571  if (i%16 == 15)
572  FOR(j,16)
573  w[j] += w[(j+9)%16] + sigma0(w[(j+1)%16]) + sigma1(w[(j+14)%16]);
574  }
575 
576  FOR(i,8) { a[i] += z[i]; z[i] = a[i]; }
577 
578  m += 128;
579  n -= 128;
580  }
581 
582  FOR(i,8) ts64(x+8*i,z[i]);
583 
584  return n;
585 }
586 
587 static const u8 iv[64] = {
588  0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
589  0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
590  0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
591  0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
592  0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
593  0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
594  0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
595  0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
596 } ;
597 
598 int crypto_hash(u8 *out,const u8 *m,u64 n)
599 {
600  u8 h[64],x[256];
601  u64 i,b = n;
602 
603  FOR(i,64) h[i] = iv[i];
604 
605  crypto_hashblocks(h,m,n);
606  m += n;
607  n &= 127;
608  m -= n;
609 
610  FOR(i,256) x[i] = 0;
611  FOR(i,n) x[i] = m[i];
612  x[n] = 128;
613 
614  n = 256-128*(n<112);
615  x[n-9] = b >> 61;
616  ts64(x+n-8,b<<3);
617  crypto_hashblocks(h,x,n);
618 
619  FOR(i,64) out[i] = h[i];
620 
621  return 0;
622 }
623 
624 sv add(gf p[4],gf q[4])
625 {
626  gf a,b,c,d,t,e,f,g,h;
627 
628  Z(a, p[1], p[0]);
629  Z(t, q[1], q[0]);
630  M(a, a, t);
631  A(b, p[0], p[1]);
632  A(t, q[0], q[1]);
633  M(b, b, t);
634  M(c, p[3], q[3]);
635  M(c, c, D2);
636  M(d, p[2], q[2]);
637  A(d, d, d);
638  Z(e, b, a);
639  Z(f, d, c);
640  A(g, d, c);
641  A(h, b, a);
642 
643  M(p[0], e, f);
644  M(p[1], h, g);
645  M(p[2], g, f);
646  M(p[3], e, h);
647 }
648 
649 sv cswap(gf p[4],gf q[4],u8 b)
650 {
651  int i;
652  FOR(i,4)
653  sel25519(p[i],q[i],b);
654 }
655 
656 sv pack(u8 *r,gf p[4])
657 {
658  gf tx, ty, zi;
659  inv25519(zi, p[2]);
660  M(tx, p[0], zi);
661  M(ty, p[1], zi);
662  pack25519(r, ty);
663  r[31] ^= par25519(tx) << 7;
664 }
665 
666 sv scalarmult(gf p[4],gf q[4],const u8 *s)
667 {
668  int i;
669  set25519(p[0],gf0);
670  set25519(p[1],gf1);
671  set25519(p[2],gf1);
672  set25519(p[3],gf0);
673  for (i = 255;i >= 0;--i) {
674  u8 b = (s[i/8]>>(i&7))&1;
675  cswap(p,q,b);
676  add(q,p);
677  add(p,p);
678  cswap(p,q,b);
679  }
680 }
681 
682 sv scalarbase(gf p[4],const u8 *s)
683 {
684  gf q[4];
685  set25519(q[0],X);
686  set25519(q[1],Y);
687  set25519(q[2],gf1);
688  M(q[3],X,Y);
689  scalarmult(p,q,s);
690 }
691 
692 int crypto_sign_keypair(u8 *pk, u8 *sk)
693 {
694  u8 d[64];
695  gf p[4];
696  int i;
697 
698  randombytes(sk, 32);
699  crypto_hash(d, sk, 32);
700  d[0] &= 248;
701  d[31] &= 127;
702  d[31] |= 64;
703 
704  scalarbase(p,d);
705  pack(pk,p);
706 
707  FOR(i,32) sk[32 + i] = pk[i];
708  return 0;
709 }
710 
711 static const u64 L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10};
712 
713 sv modL(u8 *r,i64 x[64])
714 {
715  i64 carry,i,j;
716  for (i = 63;i >= 32;--i) {
717  carry = 0;
718  for (j = i - 32;j < i - 12;++j) {
719  x[j] += carry - 16 * x[i] * L[j - (i - 32)];
720  carry = (x[j] + 128) >> 8;
721  x[j] -= carry << 8;
722  }
723  x[j] += carry;
724  x[i] = 0;
725  }
726  carry = 0;
727  FOR(j,32) {
728  x[j] += carry - (x[31] >> 4) * L[j];
729  carry = x[j] >> 8;
730  x[j] &= 255;
731  }
732  FOR(j,32) x[j] -= carry * L[j];
733  FOR(i,32) {
734  x[i+1] += x[i] >> 8;
735  r[i] = x[i] & 255;
736  }
737 }
738 
739 sv reduce(u8 *r)
740 {
741  i64 x[64],i;
742  FOR(i,64) x[i] = (u64) r[i];
743  FOR(i,64) r[i] = 0;
744  modL(r,x);
745 }
746 
747 int crypto_sign(u8 *sm,u64 *smlen,const u8 *m,u64 n,const u8 *sk)
748 {
749  u8 d[64],h[64],r[64];
750  i64 i,j,x[64];
751  gf p[4];
752 
753  crypto_hash(d, sk, 32);
754  d[0] &= 248;
755  d[31] &= 127;
756  d[31] |= 64;
757 
758  *smlen = n+64;
759  FOR(i,n) sm[64 + i] = m[i];
760  FOR(i,32) sm[32 + i] = d[32 + i];
761 
762  crypto_hash(r, sm+32, n+32);
763  reduce(r);
764  scalarbase(p,r);
765  pack(sm,p);
766 
767  FOR(i,32) sm[i+32] = sk[i+32];
768  crypto_hash(h,sm,n + 64);
769  reduce(h);
770 
771  FOR(i,64) x[i] = 0;
772  FOR(i,32) x[i] = (u64) r[i];
773  FOR(i,32) FOR(j,32) x[i+j] += h[i] * (u64) d[j];
774  modL(sm + 32,x);
775 
776  return 0;
777 }
778 
779 static int unpackneg(gf r[4],const u8 p[32])
780 {
781  gf t, chk, num, den, den2, den4, den6;
782  set25519(r[2],gf1);
783  unpack25519(r[1],p);
784  S(num,r[1]);
785  M(den,num,D);
786  Z(num,num,r[2]);
787  A(den,r[2],den);
788 
789  S(den2,den);
790  S(den4,den2);
791  M(den6,den4,den2);
792  M(t,den6,num);
793  M(t,t,den);
794 
795  pow2523(t,t);
796  M(t,t,num);
797  M(t,t,den);
798  M(t,t,den);
799  M(r[0],t,den);
800 
801  S(chk,r[0]);
802  M(chk,chk,den);
803  if (neq25519(chk, num)) M(r[0],r[0],I);
804 
805  S(chk,r[0]);
806  M(chk,chk,den);
807  if (neq25519(chk, num)) return -1;
808 
809  if (par25519(r[0]) == (p[31]>>7)) Z(r[0],gf0,r[0]);
810 
811  M(r[3],r[0],r[1]);
812  return 0;
813 }
814 
815 int crypto_sign_open(u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
816 {
817  int i;
818  u8 t[32],h[64];
819  gf p[4],q[4];
820 
821  *mlen = -1;
822  if (n < 64) return -1;
823 
824  if (unpackneg(q,pk)) return -1;
825 
826  FOR(i,n) m[i] = sm[i];
827  FOR(i,32) m[i+32] = pk[i];
828  crypto_hash(h,m,n);
829  reduce(h);
830  scalarmult(p,q,h);
831 
832  scalarbase(q,sm + 32);
833  add(p,q);
834  pack(t,p);
835 
836  n -= 64;
837  if (crypto_verify_32(sm, t)) {
838  FOR(i,n) m[i] = 0;
839  return -1;
840  }
841 
842  FOR(i,n) m[i] = sm[i + 64];
843  *mlen = n;
844  return 0;
845 }
846 
847 
848 #ifdef ZMQ_HAVE_WINDOWS
849 
850 #include <windows.h>
851 #include <WinCrypt.h>
852 
853 #define NCP ((HCRYPTPROV) 0)
854 
855 HCRYPTPROV hProvider = NCP;
856 
857 void randombytes(unsigned char *x,unsigned long long xlen)
858 {
859  unsigned i;
860  BOOL ret;
861 
862  if (hProvider == NCP) {
863  for (;;) {
864  ret = CryptAcquireContext(&hProvider, NULL, NULL,
865  PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
866  if (ret != FALSE)
867  break;
868  Sleep (1);
869  }
870  }
871  while (xlen > 0) {
872  if (xlen < 1048576)
873  i = (unsigned) xlen;
874  else
875  i = 1048576;
876 
877  ret = CryptGenRandom(hProvider, i, x);
878  if (ret == FALSE) {
879  Sleep(1);
880  continue;
881  }
882  x += i;
883  xlen -= i;
884  }
885 }
886 
887 int randombytes_close(void)
888 {
889  int rc = -1;
890  if ((hProvider != NCP) && (CryptReleaseContext(hProvider, 0) != FALSE)) {
891  hProvider = NCP;
892  rc = 0;
893  }
894  return rc;
895 }
896 
897 #else
898 
899 #include <sys/types.h>
900 #include <sys/stat.h>
901 #include <fcntl.h>
902 #include <unistd.h>
903 
904 static int fd = -1;
905 
906 void randombytes (unsigned char *x,unsigned long long xlen)
907 {
908  int i;
909  if (fd == -1) {
910  for (;;) {
911  fd = open("/dev/urandom",O_RDONLY);
912  if (fd != -1) break;
913  sleep (1);
914  }
915  }
916  while (xlen > 0) {
917  if (xlen < 1048576)
918  i = xlen;
919  else
920  i = 1048576;
921 
922  i = read(fd,x,i);
923  if (i < 1) {
924  sleep (1);
925  continue;
926  }
927  x += i;
928  xlen -= i;
929  }
930 }
931 
932 int randombytes_close (void)
933 {
934  int rc = -1;
935  if (fd != -1 && close(fd) == 0) {
936  fd = -1;
937  rc = 0;
938  }
939  return rc;
940 }
941 
942 #endif
943 
944 #endif