LCOV - code coverage report
Current view: top level - home/h/core/forks/m4-libzmq/src - tweetnacl.c (source / functions) Hit Total Coverage
Test: zeromq-4.2.0 Code Coverage Lines: 243 438 55.5 %
Date: 2016-05-09 Functions: 28 44 63.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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      829440 : static u32 L32(u32 x,int c) { return (x << c) | ((x&0xffffffff) >> (32 - c)); }
      62             : 
      63             : static u32 ld32(const u8 *x)
      64             : {
      65       50952 :   u32 u = x[3];
      66       50952 :   u = (u<<8)|x[2];
      67       50952 :   u = (u<<8)|x[1];
      68       50952 :   return (u<<8)|x[0];
      69             : }
      70             : 
      71             : static u64 dl64(const u8 *x)
      72             : {
      73           0 :   u64 i,u=0;
      74           0 :   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       31992 :   FOR(i,4) { x[i] = u; u >>= 8; }
      82             : }
      83             : 
      84             : sv ts64(u8 *x,u64 u)
      85             : {
      86             :   int i;
      87           0 :   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         297 :   u32 i,d = 0;
      93         297 :   FOR(i,n) d |= x[i]^y[i];
      94         297 :   return (1 & ((d - 1) >> 8)) - 1;
      95             : }
      96             : 
      97         297 : int crypto_verify_16(const u8 *x,const u8 *y)
      98             : {
      99         297 :   return vn(x,y,16);
     100             : }
     101             : 
     102           0 : int crypto_verify_32(const u8 *x,const u8 *y)
     103             : {
     104           0 :   return vn(x,y,32);
     105             : }
     106             : 
     107        2592 : 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       12960 :   FOR(i,4) {
     113       20736 :     x[5*i] = ld32(c+4*i);
     114       20736 :     x[1+i] = ld32(k+4*i);
     115       20736 :     x[6+i] = ld32(in+4*i);
     116       20736 :     x[11+i] = ld32(k+16+4*i);
     117             :   }
     118             : 
     119       41472 :   FOR(i,16) y[i] = x[i];
     120             : 
     121       51840 :   FOR(i,20) {
     122      207360 :     FOR(j,4) {
     123      829440 :       FOR(m,4) t[m] = x[(5*j+4*m)%16];
     124      414720 :       t[1] ^= L32(t[0]+t[3], 7);
     125      414720 :       t[2] ^= L32(t[1]+t[0], 9);
     126      414720 :       t[3] ^= L32(t[2]+t[1],13);
     127      414720 :       t[0] ^= L32(t[3]+t[2],18);
     128      207360 :       FOR(m,4) w[4*j+(j+m)%4] = t[m];
     129             :     }
     130      829440 :     FOR(m,16) x[m] = w[m];
     131             :   }
     132             : 
     133        2592 :   if (h) {
     134       18960 :     FOR(i,16) x[i] += y[i];
     135        4740 :     FOR(i,4) {
     136        9480 :       x[5*i] -= ld32(c+4*i);
     137        9480 :       x[6+i] -= ld32(in+4*i);
     138             :     }
     139        4740 :     FOR(i,4) {
     140        4740 :       st32(out+4*i,x[5*i]);
     141        4740 :       st32(out+16+4*i,x[6+i]);
     142             :     }
     143             :   } else
     144       45024 :     FOR(i,16) st32(out + 4 * i,x[i] + y[i]);
     145        2592 : }
     146             : 
     147        1407 : int crypto_core_salsa20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
     148             : {
     149        1407 :   core(out,in,k,c,0);
     150        1407 :   return 0;
     151             : }
     152             : 
     153        1185 : int crypto_core_hsalsa20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
     154             : {
     155        1185 :   core(out,in,k,c,1);
     156        1185 :   return 0;
     157             : }
     158             : 
     159             : static const u8 sigma[16] = "expand 32-byte k";
     160             : 
     161         855 : 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         855 :   if (!b) return 0;
     166       13680 :   FOR(i,16) z[i] = 0;
     167        6840 :   FOR(i,8) z[i] = n[i];
     168        1407 :   while (b >= 64) {
     169         552 :     crypto_core_salsa20(x,z,k,sigma);
     170         552 :     FOR(i,64) c[i] = (m?m[i]:0) ^ x[i];
     171             :     u = 1;
     172        4416 :     for (i = 8;i < 16;++i) {
     173        4416 :       u += (u32) z[i];
     174        4416 :       z[i] = u;
     175        4416 :       u >>= 8;
     176             :     }
     177         552 :     b -= 64;
     178         552 :     c += 64;
     179         552 :     if (m) m += 64;
     180             :   }
     181         855 :   if (b) {
     182         855 :     crypto_core_salsa20(x,z,k,sigma);
     183         855 :     FOR(i,b) c[i] = (m?m[i]:0) ^ x[i];
     184             :   }
     185             :   return 0;
     186             : }
     187             : 
     188         297 : int crypto_stream_salsa20(u8 *c,u64 d,const u8 *n,const u8 *k)
     189             : {
     190         297 :   return crypto_stream_salsa20_xor(c,0,d,n,k);
     191             : }
     192             : 
     193         297 : int crypto_stream(u8 *c,u64 d,const u8 *n,const u8 *k)
     194             : {
     195             :   u8 s[32];
     196         297 :   crypto_core_hsalsa20(s,n,k,sigma);
     197         297 :   return crypto_stream_salsa20(c,d,n+16,s);
     198             : }
     199             : 
     200         558 : int crypto_stream_xor(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *k)
     201             : {
     202             :   u8 s[32];
     203         558 :   crypto_core_hsalsa20(s,n,k,sigma);
     204         558 :   return crypto_stream_salsa20_xor(c,m,d,n+16,s);
     205             : }
     206             : 
     207             : sv add1305(u32 *h,const u32 *c)
     208             : {
     209        3048 :   u32 j,u = 0;
     210       64962 :   FOR(j,17) {
     211       61914 :     u += h[j] + c[j];
     212       61914 :     h[j] = u & 255;
     213       61914 :     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         594 : 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         594 :   FOR(j,17) r[j]=h[j]=0;
     226        9504 :   FOR(j,16) r[j]=k[j];
     227         594 :   r[3]&=15;
     228         594 :   r[4]&=252;
     229         594 :   r[7]&=15;
     230         594 :   r[8]&=252;
     231         594 :   r[11]&=15;
     232         594 :   r[12]&=252;
     233         594 :   r[15]&=15;
     234             : 
     235        3642 :   while (n > 0) {
     236       41718 :     FOR(j,17) c[j] = 0;
     237       36090 :     for (j = 0;(j < 16) && (j < n);++j) c[j] = m[j];
     238        2454 :     c[j] = 1;
     239        2454 :     m += j; n -= j;
     240             :     add1305(h,c);
     241       41718 :     FOR(i,17) {
     242       41718 :       x[i] = 0;
     243       41718 :       FOR(j,17) x[i] += h[j] * ((j <= i) ? r[i - j] : 320 * r[i + 17 - j]);
     244             :     }
     245       41718 :     FOR(i,17) h[i] = x[i];
     246             :     u = 0;
     247       39264 :     FOR(j,16) {
     248       39264 :       u += h[j];
     249       39264 :       h[j] = u & 255;
     250       39264 :       u >>= 8;
     251             :     }
     252        2454 :     u += h[16]; h[16] = u & 3;
     253        2454 :     u = 5 * (u >> 2);
     254       41718 :     FOR(j,16) {
     255       39264 :       u += h[j];
     256       39264 :       h[j] = u & 255;
     257       39264 :       u >>= 8;
     258             :     }
     259        2454 :     u += h[16]; h[16] = u;
     260             :   }
     261             : 
     262       10098 :   FOR(j,17) g[j] = h[j];
     263             :   add1305(h,minusp);
     264         594 :   s = -(h[16] >> 7);
     265         594 :   FOR(j,17) h[j] ^= s & (g[j] ^ h[j]);
     266             : 
     267        9504 :   FOR(j,16) c[j] = k[j + 16];
     268         594 :   c[16] = 0;
     269             :   add1305(h,c);
     270        9504 :   FOR(j,16) out[j] = h[j];
     271         594 :   return 0;
     272             : }
     273             : 
     274         297 : int crypto_onetimeauth_verify(const u8 *h,const u8 *m,u64 n,const u8 *k)
     275             : {
     276             :   u8 x[16];
     277         297 :   crypto_onetimeauth(x,m,n,k);
     278         297 :   return crypto_verify_16(h,x);
     279             : }
     280             : 
     281         297 : int crypto_secretbox(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *k)
     282             : {
     283             :   int i;
     284         297 :   if (d < 32) return -1;
     285         297 :   crypto_stream_xor(c,m,d,n,k);
     286         297 :   crypto_onetimeauth(c + 16,c + 32,d - 32,c);
     287         297 :   FOR(i,16) c[i] = 0;
     288             :   return 0;
     289             : }
     290             : 
     291         297 : 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         297 :   if (d < 32) return -1;
     296         297 :   crypto_stream(x,32,n,k);
     297         297 :   if (crypto_onetimeauth_verify(c + 16,c + 32,d - 32,x) != 0) return -1;
     298         261 :   crypto_stream_xor(m,c,d,n,k);
     299         261 :   FOR(i,32) m[i] = 0;
     300             :   return 0;
     301             : }
     302             : 
     303             : sv set25519(gf r, const gf a)
     304             : {
     305             :   int i;
     306           0 :   FOR(i,16) r[i]=a[i];
     307             : }
     308             : 
     309     2624193 : sv car25519(gf o)
     310             : {
     311             :   int i;
     312             :   i64 c;
     313    44611281 :   FOR(i,16) {
     314    41987088 :     o[i]+=(1LL<<16);
     315    41987088 :     c=o[i]>>16;
     316    41987088 :     o[(i+1)*(i<15)]+=c-1+37*(c-1)*(i==15);
     317    41987088 :     o[i]-=c<<16;
     318             :   }
     319     2624193 : }
     320             : 
     321      438438 : sv sel25519(gf p,gf q,int b)
     322             : {
     323      438438 :   i64 t,i,c=~(b-1);
     324     7453446 :   FOR(i,16) {
     325     7015008 :     t= c&(p[i]^q[i]);
     326     7015008 :     p[i]^=t;
     327     7015008 :     q[i]^=t;
     328             :   }
     329      438438 : }
     330             : 
     331         429 : sv pack25519(u8 *o,const gf n)
     332             : {
     333             :   int i,j,b;
     334             :   gf m,t;
     335         429 :   FOR(i,16) t[i]=n[i];
     336         429 :   car25519(t);
     337         429 :   car25519(t);
     338         429 :   car25519(t);
     339        1287 :   FOR(j,2) {
     340         858 :     m[0]=t[0]-0xffed;
     341       12870 :     for(i=1;i<15;i++) {
     342       12012 :       m[i]=t[i]-0xffff-((m[i-1]>>16)&1);
     343       12012 :       m[i-1]&=0xffff;
     344             :     }
     345         858 :     m[15]=t[15]-0x7fff-((m[14]>>16)&1);
     346         858 :     b=(m[15]>>16)&1;
     347         858 :     m[14]&=0xffff;
     348         858 :     sel25519(t,m,1-b);
     349             :   }
     350        6864 :   FOR(i,16) {
     351        6864 :     o[2*i]=t[i]&0xff;
     352        6864 :     o[2*i+1]=t[i]>>8;
     353             :   }
     354         429 : }
     355             : 
     356           0 : static int neq25519(const gf a, const gf b)
     357             : {
     358             :   u8 c[32],d[32];
     359           0 :   pack25519(c,a);
     360           0 :   pack25519(d,b);
     361           0 :   return crypto_verify_32(c,d);
     362             : }
     363             : 
     364             : static u8 par25519(const gf a)
     365             : {
     366             :   u8 d[32];
     367           0 :   pack25519(d,a);
     368           0 :   return d[0]&1;
     369             : }
     370             : 
     371         429 : sv unpack25519(gf o, const u8 *n)
     372             : {
     373             :   int i;
     374         429 :   FOR(i,16) o[i]=n[2*i]+((i64)n[2*i+1]<<8);
     375         429 :   o[15]&=0x7fff;
     376         429 : }
     377             : 
     378             : sv A(gf o,const gf a,const gf b)
     379             : {
     380             :   int i;
     381     2078505 :   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     5360355 :   FOR(i,16) o[i]=a[i]-b[i];
     388             : }
     389             : 
     390     1311453 : sv M(gf o,const gf a,const gf b)
     391             : {
     392             :   i64 i,j,t[31];
     393     1311453 :   FOR(i,31) t[i]=0;
     394   335731968 :   FOR(i,16) FOR(j,16) t[i+j]+=a[i]*b[j];
     395    19671795 :   FOR(i,15) t[i]+=38*t[i+16];
     396    20983248 :   FOR(i,16) o[i]=t[i];
     397     1311453 :   car25519(o);
     398     1311453 :   car25519(o);
     399     1311453 : }
     400             : 
     401             : sv S(gf o,const gf a)
     402             : {
     403      437151 :   M(o,a,a);
     404             : }
     405             : 
     406         429 : sv inv25519(gf o,const gf i)
     407             : {
     408             :   gf c;
     409             :   int a;
     410         429 :   FOR(a,16) c[a]=i[a];
     411      108966 :   for(a=253;a>=0;a--) {
     412             :     S(c,c);
     413      108966 :     if(a!=2&&a!=4) M(c,c,i);
     414             :   }
     415        6864 :   FOR(a,16) o[a]=c[a];
     416         429 : }
     417             : 
     418           0 : sv pow2523(gf o,const gf i)
     419             : {
     420             :   gf c;
     421             :   int a;
     422           0 :   FOR(a,16) c[a]=i[a];
     423           0 :   for(a=250;a>=0;a--) {
     424             :     S(c,c);
     425           0 :     if(a!=1) M(c,c,i);
     426             :   }
     427           0 :   FOR(a,16) o[a]=c[a];
     428           0 : }
     429             : 
     430         429 : 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         429 :   FOR(i,31) z[i]=n[i];
     436         429 :   z[31]=(n[31]&127)|64;
     437         429 :   z[0]&=248;
     438         429 :   unpack25519(x,p);
     439        7293 :   FOR(i,16) {
     440        6864 :     b[i]=x[i];
     441        6864 :     d[i]=a[i]=c[i]=0;
     442             :   }
     443         429 :   a[0]=d[0]=1;
     444      109824 :   for(i=254;i>=0;--i) {
     445      109395 :     r=(z[i>>3]>>(i&7))&1;
     446      109395 :     sel25519(a,b,r);
     447      109395 :     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      109395 :     M(a,c,a);
     455      109395 :     M(c,b,e);
     456             :     A(e,a,c);
     457             :     Z(a,a,c);
     458             :     S(b,a);
     459             :     Z(c,d,f);
     460      109395 :     M(a,c,_121665);
     461             :     A(a,a,d);
     462      109395 :     M(c,c,a);
     463      109395 :     M(a,d,f);
     464      109395 :     M(d,b,x);
     465             :     S(b,e);
     466      109395 :     sel25519(a,b,r);
     467      109395 :     sel25519(c,d,r);
     468             :   }
     469        6864 :   FOR(i,16) {
     470        6864 :     x[i+16]=a[i];
     471        6864 :     x[i+32]=c[i];
     472        6864 :     x[i+48]=b[i];
     473        6864 :     x[i+64]=d[i];
     474             :   }
     475         429 :   inv25519(x+32,x+32);
     476         429 :   M(x+16,x+16,x+32);
     477         429 :   pack25519(q,x+16);
     478         429 :   return 0;
     479             : }
     480             : 
     481          99 : int crypto_scalarmult_base(u8 *q,const u8 *n)
     482             : {
     483          99 :   return crypto_scalarmult(q,n,_9);
     484             : }
     485             : 
     486          99 : int crypto_box_keypair(u8 *y,u8 *x)
     487             : {
     488          99 :   randombytes(x,32);
     489          99 :   return crypto_scalarmult_base(y,x);
     490             : }
     491             : 
     492         330 : int crypto_box_beforenm(u8 *k,const u8 *y,const u8 *x)
     493             : {
     494             :   u8 s[32];
     495         330 :   crypto_scalarmult(s,x,y);
     496         330 :   return crypto_core_hsalsa20(k,_0,s,sigma);
     497             : }
     498             : 
     499         264 : int crypto_box_afternm(u8 *c,const u8 *m,u64 d,const u8 *n,const u8 *k)
     500             : {
     501         264 :   return crypto_secretbox(c,m,d,n,k);
     502             : }
     503             : 
     504         264 : int crypto_box_open_afternm(u8 *m,const u8 *c,u64 d,const u8 *n,const u8 *k)
     505             : {
     506         264 :   return crypto_secretbox_open(m,c,d,n,k);
     507             : }
     508             : 
     509         144 : 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         144 :   crypto_box_beforenm(k,y,x);
     513         144 :   return crypto_box_afternm(c,m,d,n,k);
     514             : }
     515             : 
     516         144 : 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         144 :   crypto_box_beforenm(k,y,x);
     520         144 :   return crypto_box_open_afternm(m,c,d,n,k);
     521             : }
     522             : 
     523           0 : static u64 R(u64 x,int c) { return (x >> c) | (x << (64 - c)); }
     524           0 : static u64 Ch(u64 x,u64 y,u64 z) { return (x & y) ^ (~x & z); }
     525           0 : static u64 Maj(u64 x,u64 y,u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
     526           0 : static u64 Sigma0(u64 x) { return R(x,28) ^ R(x,34) ^ R(x,39); }
     527           0 : static u64 Sigma1(u64 x) { return R(x,14) ^ R(x,18) ^ R(x,41); }
     528           0 : static u64 sigma0(u64 x) { return R(x, 1) ^ R(x, 8) ^ (x >> 7); }
     529           0 : 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           0 : 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           0 :   FOR(i,8) z[i] = a[i] = dl64(x + 8 * i);
     561             : 
     562           0 :   while (n >= 128) {
     563           0 :     FOR(i,16) w[i] = dl64(m + 8 * i);
     564             : 
     565           0 :     FOR(i,80) {
     566           0 :       FOR(j,8) b[j] = a[j];
     567           0 :       t = a[7] + Sigma1(a[4]) + Ch(a[4],a[5],a[6]) + K[i] + w[i%16];
     568           0 :       b[7] = t + Sigma0(a[0]) + Maj(a[0],a[1],a[2]);
     569           0 :       b[3] += t;
     570           0 :       FOR(j,8) a[(j+1)%8] = b[j];
     571           0 :       if (i%16 == 15)
     572           0 :         FOR(j,16)
     573           0 :           w[j] += w[(j+9)%16] + sigma0(w[(j+1)%16]) + sigma1(w[(j+14)%16]);
     574             :     }
     575             : 
     576           0 :     FOR(i,8) { a[i] += z[i]; z[i] = a[i]; }
     577             : 
     578           0 :     m += 128;
     579           0 :     n -= 128;
     580             :   }
     581             : 
     582           0 :   FOR(i,8) ts64(x+8*i,z[i]);
     583             : 
     584           0 :   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           0 : int crypto_hash(u8 *out,const u8 *m,u64 n)
     599             : {
     600             :   u8 h[64],x[256];
     601           0 :   u64 i,b = n;
     602             : 
     603           0 :   FOR(i,64) h[i] = iv[i];
     604             : 
     605           0 :   crypto_hashblocks(h,m,n);
     606           0 :   m += n;
     607           0 :   n &= 127;
     608           0 :   m -= n;
     609             : 
     610           0 :   FOR(i,256) x[i] = 0;
     611           0 :   FOR(i,n) x[i] = m[i];
     612           0 :   x[n] = 128;
     613             : 
     614           0 :   n = 256-128*(n<112);
     615           0 :   x[n-9] = b >> 61;
     616           0 :   ts64(x+n-8,b<<3);
     617           0 :   crypto_hashblocks(h,x,n);
     618             : 
     619           0 :   FOR(i,64) out[i] = h[i];
     620             : 
     621           0 :   return 0;
     622             : }
     623             : 
     624           0 : sv add(gf p[4],gf q[4])
     625             : {
     626             :   gf a,b,c,d,t,e,f,g,h;
     627             : 
     628           0 :   Z(a, p[1], p[0]);
     629           0 :   Z(t, q[1], q[0]);
     630           0 :   M(a, a, t);
     631           0 :   A(b, p[0], p[1]);
     632             :   A(t, q[0], q[1]);
     633           0 :   M(b, b, t);
     634           0 :   M(c, p[3], q[3]);
     635           0 :   M(c, c, D2);
     636           0 :   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           0 :   M(p[0], e, f);
     644           0 :   M(p[1], h, g);
     645           0 :   M(p[2], g, f);
     646           0 :   M(p[3], e, h);
     647           0 : }
     648             : 
     649           0 : sv cswap(gf p[4],gf q[4],u8 b)
     650             : {
     651             :   int i;
     652           0 :   FOR(i,4)
     653           0 :     sel25519(p[i],q[i],b);
     654           0 : }
     655             : 
     656           0 : sv pack(u8 *r,gf p[4])
     657             : {
     658             :   gf tx, ty, zi;
     659           0 :   inv25519(zi, p[2]);
     660           0 :   M(tx, p[0], zi);
     661           0 :   M(ty, p[1], zi);
     662           0 :   pack25519(r, ty);
     663           0 :   r[31] ^= par25519(tx) << 7;
     664           0 : }
     665             : 
     666           0 : sv scalarmult(gf p[4],gf q[4],const u8 *s)
     667             : {
     668             :   int i;
     669             :   set25519(p[0],gf0);
     670           0 :   set25519(p[1],gf1);
     671           0 :   set25519(p[2],gf1);
     672           0 :   set25519(p[3],gf0);
     673           0 :   for (i = 255;i >= 0;--i) {
     674           0 :     u8 b = (s[i/8]>>(i&7))&1;
     675           0 :     cswap(p,q,b);
     676           0 :     add(q,p);
     677           0 :     add(p,p);
     678           0 :     cswap(p,q,b);
     679             :   }
     680           0 : }
     681             : 
     682           0 : 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           0 :   M(q[3],X,Y);
     689           0 :   scalarmult(p,q,s);
     690           0 : }
     691             : 
     692           0 : int crypto_sign_keypair(u8 *pk, u8 *sk)
     693             : {
     694             :   u8 d[64];
     695             :   gf p[4];
     696             :   int i;
     697             : 
     698           0 :   randombytes(sk, 32);
     699           0 :   crypto_hash(d, sk, 32);
     700           0 :   d[0] &= 248;
     701           0 :   d[31] &= 127;
     702           0 :   d[31] |= 64;
     703             : 
     704           0 :   scalarbase(p,d);
     705           0 :   pack(pk,p);
     706             : 
     707           0 :   FOR(i,32) sk[32 + i] = pk[i];
     708           0 :   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           0 : sv modL(u8 *r,i64 x[64])
     714             : {
     715             :   i64 carry,i,j;
     716           0 :   for (i = 63;i >= 32;--i) {
     717           0 :     carry = 0;
     718           0 :     for (j = i - 32;j < i - 12;++j) {
     719           0 :       x[j] += carry - 16 * x[i] * L[j - (i - 32)];
     720           0 :       carry = (x[j] + 128) >> 8;
     721           0 :       x[j] -= carry << 8;
     722             :     }
     723           0 :     x[j] += carry;
     724           0 :     x[i] = 0;
     725             :   }
     726             :   carry = 0;
     727           0 :   FOR(j,32) {
     728           0 :     x[j] += carry - (x[31] >> 4) * L[j];
     729           0 :     carry = x[j] >> 8;
     730           0 :     x[j] &= 255;
     731             :   }
     732           0 :   FOR(j,32) x[j] -= carry * L[j];
     733           0 :   FOR(i,32) {
     734           0 :     x[i+1] += x[i] >> 8;
     735           0 :     r[i] = x[i] & 255;
     736             :   }
     737           0 : }
     738             : 
     739           0 : sv reduce(u8 *r)
     740             : {
     741             :   i64 x[64],i;
     742           0 :   FOR(i,64) x[i] = (u64) r[i];
     743           0 :   FOR(i,64) r[i] = 0;
     744           0 :   modL(r,x);
     745           0 : }
     746             : 
     747           0 : 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           0 :   crypto_hash(d, sk, 32);
     754           0 :   d[0] &= 248;
     755           0 :   d[31] &= 127;
     756           0 :   d[31] |= 64;
     757             : 
     758           0 :   *smlen = n+64;
     759           0 :   FOR(i,n) sm[64 + i] = m[i];
     760           0 :   FOR(i,32) sm[32 + i] = d[32 + i];
     761             : 
     762           0 :   crypto_hash(r, sm+32, n+32);
     763           0 :   reduce(r);
     764           0 :   scalarbase(p,r);
     765           0 :   pack(sm,p);
     766             : 
     767           0 :   FOR(i,32) sm[i+32] = sk[i+32];
     768           0 :   crypto_hash(h,sm,n + 64);
     769           0 :   reduce(h);
     770             : 
     771           0 :   FOR(i,64) x[i] = 0;
     772           0 :   FOR(i,32) x[i] = (u64) r[i];
     773           0 :   FOR(i,32) FOR(j,32) x[i+j] += h[i] * (u64) d[j];
     774           0 :   modL(sm + 32,x);
     775             : 
     776           0 :   return 0;
     777             : }
     778             : 
     779           0 : static int unpackneg(gf r[4],const u8 p[32])
     780             : {
     781             :   gf t, chk, num, den, den2, den4, den6;
     782           0 :   set25519(r[2],gf1);
     783           0 :   unpack25519(r[1],p);
     784           0 :   S(num,r[1]);
     785           0 :   M(den,num,D);
     786           0 :   Z(num,num,r[2]);
     787             :   A(den,r[2],den);
     788             : 
     789             :   S(den2,den);
     790             :   S(den4,den2);
     791           0 :   M(den6,den4,den2);
     792           0 :   M(t,den6,num);
     793           0 :   M(t,t,den);
     794             : 
     795           0 :   pow2523(t,t);
     796           0 :   M(t,t,num);
     797           0 :   M(t,t,den);
     798           0 :   M(t,t,den);
     799           0 :   M(r[0],t,den);
     800             : 
     801             :   S(chk,r[0]);
     802           0 :   M(chk,chk,den);
     803           0 :   if (neq25519(chk, num)) M(r[0],r[0],I);
     804             : 
     805             :   S(chk,r[0]);
     806           0 :   M(chk,chk,den);
     807           0 :   if (neq25519(chk, num)) return -1;
     808             : 
     809           0 :   if (par25519(r[0]) == (p[31]>>7)) Z(r[0],gf0,r[0]);
     810             : 
     811           0 :   M(r[3],r[0],r[1]);
     812           0 :   return 0;
     813             : }
     814             : 
     815           0 : 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           0 :   *mlen = -1;
     822           0 :   if (n < 64) return -1;
     823             : 
     824           0 :   if (unpackneg(q,pk)) return -1;
     825             : 
     826           0 :   FOR(i,n) m[i] = sm[i];
     827           0 :   FOR(i,32) m[i+32] = pk[i];
     828           0 :   crypto_hash(h,m,n);
     829           0 :   reduce(h);
     830           0 :   scalarmult(p,q,h);
     831             : 
     832           0 :   scalarbase(q,sm + 32);
     833           0 :   add(p,q);
     834           0 :   pack(t,p);
     835             : 
     836           0 :   n -= 64;
     837           0 :   if (crypto_verify_32(sm, t)) {
     838           0 :     FOR(i,n) m[i] = 0;
     839             :     return -1;
     840             :   }
     841             : 
     842           0 :   FOR(i,n) m[i] = sm[i + 64];
     843           0 :   *mlen = n;
     844           0 :   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         654 : void randombytes (unsigned char *x,unsigned long long xlen)
     907             : {
     908             :     int i;
     909         654 :     if (fd == -1) {
     910             :         for (;;) {
     911         420 :             fd = open("/dev/urandom",O_RDONLY);
     912         420 :             if (fd != -1) break;
     913           0 :                 sleep (1);
     914           0 :         }
     915             :     }
     916        1308 :     while (xlen > 0) {
     917         654 :         if (xlen < 1048576)
     918         654 :             i = xlen;
     919             :         else
     920             :             i = 1048576;
     921             : 
     922        1308 :         i = read(fd,x,i);
     923         654 :         if (i < 1) {
     924           0 :             sleep (1);
     925           0 :             continue;
     926             :         }
     927         654 :         x += i;
     928         654 :         xlen -= i;
     929             :     }
     930         654 : }
     931             : 
     932         423 : int randombytes_close (void)
     933             : {
     934         423 :     int rc = -1;
     935         423 :     if (fd != -1 && close(fd) == 0) {
     936         420 :         fd = -1;
     937         420 :         rc = 0;
     938             :     }
     939         423 :     return rc;
     940             : }
     941             : 
     942             : #endif
     943             : 
     944             : #endif

Generated by: LCOV version 1.10