Browse Source

More cleanup, Linux build fixes.

Adam Ierymenko 8 years ago
parent
commit
6fc70f7c16
7 changed files with 59 additions and 324 deletions
  1. 18 272
      node/C25519.cpp
  2. 0 1
      node/Poly1305.cpp
  3. 15 40
      node/SharedPtr.hpp
  4. 18 4
      node/Utils.hpp
  5. 2 2
      osdep/Binder.hpp
  6. 5 4
      osdep/ManagedRoute.cpp
  7. 1 1
      selftest.cpp

+ 18 - 272
node/C25519.cpp

@@ -268,9 +268,7 @@ static void recip(unsigned int out[32],const unsigned int z[32])
   /* 2^255 - 21 */ mult(out,t1,z11);
 }
 
-static inline int crypto_scalarmult(unsigned char *q,
-  const unsigned char *n,
-  const unsigned char *p)
+static inline int crypto_scalarmult(unsigned char *q,const unsigned char *n,const unsigned char *p)
 {
   unsigned int work[96];
   unsigned char e[32];
@@ -288,12 +286,24 @@ static inline int crypto_scalarmult(unsigned char *q,
   return 0;
 }
 
-static const unsigned char base[32] = {9};
-
-static inline int crypto_scalarmult_base(unsigned char *q,
-  const unsigned char *n)
+//static const unsigned char base[32] = {9};
+static inline int crypto_scalarmult_base(unsigned char *q,const unsigned char *n)
 {
-  return crypto_scalarmult(q,n,base);
+  //return crypto_scalarmult(q,n,base);
+  unsigned int work[96];
+  unsigned char e[32];
+  unsigned int i;
+  for (i = 0;i < 32;++i) e[i] = n[i];
+  e[0] &= 248;
+  e[31] &= 127;
+  e[31] |= 64;
+  for (i = 0;i < 32;++i) work[i] = 9;
+  mainloop(work,e);
+  recip(work + 32,work + 32);
+  mult(work + 64,work,work + 32);
+  freeze(work + 64);
+  for (i = 0;i < 32;++i) q[i] = work[64 + i];
+  return 0;
 }
 
 //////////////////////////////////////////////////////////////////////////////
@@ -413,20 +423,6 @@ static inline void fe25519_pack(unsigned char r[32], const fe25519 *x)
     r[i] = y.v[i];
 }
 
-#if 0
-static int fe25519_iszero(const fe25519 *x)
-{
-  int i;
-  int r;
-  fe25519 t = *x;
-  fe25519_freeze(&t);
-  r = equal(t.v[0],0);
-  for(i=1;i<32;i++) 
-    r &= equal(t.v[i],0);
-  return r;
-}
-#endif
-
 static inline int fe25519_iseq_vartime(const fe25519 *x, const fe25519 *y)
 {
   int i;
@@ -745,14 +741,6 @@ static inline void sc25519_from32bytes(sc25519 *r, const unsigned char x[32])
   barrett_reduce(r, t);
 }
 
-#if 0
-static void shortsc25519_from16bytes(shortsc25519 *r, const unsigned char x[16])
-{
-  int i;
-  for(i=0;i<16;i++) r->v[i] = x[i];
-}
-#endif
-
 static inline void sc25519_from64bytes(sc25519 *r, const unsigned char x[64])
 {
   int i;
@@ -761,56 +749,12 @@ static inline void sc25519_from64bytes(sc25519 *r, const unsigned char x[64])
   barrett_reduce(r, t);
 }
 
-#if 0
-static void sc25519_from_shortsc(sc25519 *r, const shortsc25519 *x)
-{
-  int i;
-  for(i=0;i<16;i++)
-    r->v[i] = x->v[i];
-  for(i=0;i<16;i++)
-    r->v[16+i] = 0;
-}
-#endif
-
 static inline void sc25519_to32bytes(unsigned char r[32], const sc25519 *x)
 {
   int i;
   for(i=0;i<32;i++) r[i] = x->v[i];
 }
 
-#if 0
-static int sc25519_iszero_vartime(const sc25519 *x)
-{
-  int i;
-  for(i=0;i<32;i++)
-    if(x->v[i] != 0) return 0;
-  return 1;
-}
-#endif
-
-#if 0
-static int sc25519_isshort_vartime(const sc25519 *x)
-{
-  int i;
-  for(i=31;i>15;i--)
-    if(x->v[i] != 0) return 0;
-  return 1;
-}
-#endif
-
-#if 0
-static int sc25519_lt_vartime(const sc25519 *x, const sc25519 *y)
-{
-  int i;
-  for(i=31;i>=0;i--)
-  {
-    if(x->v[i] < y->v[i]) return 1;
-    if(x->v[i] > y->v[i]) return 0;
-  }
-  return 0;
-}
-#endif
-
 static inline void sc25519_add(sc25519 *r, const sc25519 *x, const sc25519 *y)
 {
   int i, carry;
@@ -824,21 +768,6 @@ static inline void sc25519_add(sc25519 *r, const sc25519 *x, const sc25519 *y)
   reduce_add_sub(r);
 }
 
-#if 0
-static void sc25519_sub_nored(sc25519 *r, const sc25519 *x, const sc25519 *y)
-{
-  crypto_uint32 b = 0;
-  crypto_uint32 t;
-  int i;
-  for(i=0;i<32;i++)
-  {
-    t = x->v[i] - y->v[i] - b;
-    r->v[i] = t & 255;
-    b = (t >> 8) & 1;
-  }
-}
-#endif
-
 static inline void sc25519_mul(sc25519 *r, const sc25519 *x, const sc25519 *y)
 {
   int i,j,carry;
@@ -860,15 +789,6 @@ static inline void sc25519_mul(sc25519 *r, const sc25519 *x, const sc25519 *y)
   barrett_reduce(r, t);
 }
 
-#if 0
-static void sc25519_mul_shortsc(sc25519 *r, const sc25519 *x, const shortsc25519 *y)
-{
-  sc25519 t;
-  sc25519_from_shortsc(&t, y);
-  sc25519_mul(r, x, &t);
-}
-#endif
-
 static inline void sc25519_window3(signed char r[85], const sc25519 *s)
 {
   char carry;
@@ -906,45 +826,6 @@ static inline void sc25519_window3(signed char r[85], const sc25519 *s)
   r[84] += carry;
 }
 
-#if 0
-static void sc25519_window5(signed char r[51], const sc25519 *s)
-{
-  char carry;
-  int i;
-  for(i=0;i<6;i++)
-  {
-    r[8*i+0]  =  s->v[5*i+0]       & 31;
-    r[8*i+1]  = (s->v[5*i+0] >> 5) & 31;
-    r[8*i+1] ^= (s->v[5*i+1] << 3) & 31;
-    r[8*i+2]  = (s->v[5*i+1] >> 2) & 31;
-    r[8*i+3]  = (s->v[5*i+1] >> 7) & 31;
-    r[8*i+3] ^= (s->v[5*i+2] << 1) & 31;
-    r[8*i+4]  = (s->v[5*i+2] >> 4) & 31;
-    r[8*i+4] ^= (s->v[5*i+3] << 4) & 31;
-    r[8*i+5]  = (s->v[5*i+3] >> 1) & 31;
-    r[8*i+6]  = (s->v[5*i+3] >> 6) & 31;
-    r[8*i+6] ^= (s->v[5*i+4] << 2) & 31;
-    r[8*i+7]  = (s->v[5*i+4] >> 3) & 31;
-  }
-  r[8*i+0]  =  s->v[5*i+0]       & 31;
-  r[8*i+1]  = (s->v[5*i+0] >> 5) & 31;
-  r[8*i+1] ^= (s->v[5*i+1] << 3) & 31;
-  r[8*i+2]  = (s->v[5*i+1] >> 2) & 31;
-
-  /* Making it signed */
-  carry = 0;
-  for(i=0;i<50;i++)
-  {
-    r[i] += carry;
-    r[i+1] += r[i] >> 5;
-    r[i] &= 31;
-    carry = r[i] >> 4;
-    r[i] -= carry<<5;
-  }
-  r[50] += carry;
-}
-#endif
-
 static inline void sc25519_2interleave2(unsigned char r[127], const sc25519 *s1, const sc25519 *s2)
 {
   int i;
@@ -2052,16 +1933,6 @@ static inline void ge25519_pack(unsigned char r[32], const ge25519_p3 *p)
   r[31] ^= fe25519_getparity(&tx) << 7;
 }
 
-#if 0
-static int ge25519_isneutral_vartime(const ge25519_p3 *p)
-{
-  int ret = 1;
-  if(!fe25519_iszero(&p->x)) ret = 0;
-  if(!fe25519_iseq_vartime(&p->y, &p->z)) ret = 0;
-  return ret;
-}
-#endif
-
 /* computes [s1]p1 + [s2]p2 */
 static void ge25519_double_scalarmult_vartime(ge25519_p3 *r, const ge25519_p3 *p1, const sc25519 *s1, const ge25519_p3 *p2, const sc25519 *s2)
 {
@@ -2137,131 +2008,6 @@ static inline void get_hram(unsigned char *hram, const unsigned char *sm, const
   SHA512::hash(hram,playground,(unsigned int)smlen);
 }
 
-// This is the original sign and verify code -- the versions in sign() and
-// verify() below the fold are slightly modified in terms of how they behave
-// in relation to the message, but the algorithms are the same.
-
-#if 0
-int crypto_sign_keypair(
-    unsigned char *pk,
-    unsigned char *sk
-    )
-{
-  sc25519 scsk;
-  ge25519 gepk;
-  unsigned char extsk[64];
-  int i;
-
-  randombytes(sk, 32);
-  crypto_hash_sha512(extsk, sk, 32);
-  extsk[0] &= 248;
-  extsk[31] &= 127;
-  extsk[31] |= 64;
-
-  sc25519_from32bytes(&scsk,extsk);
-  
-  ge25519_scalarmult_base(&gepk, &scsk);
-  ge25519_pack(pk, &gepk);
-  for(i=0;i<32;i++)
-    sk[32 + i] = pk[i];
-  return 0;
-}
-
-static int crypto_sign(
-    unsigned char *sm,unsigned long long *smlen,
-    const unsigned char *m,unsigned long long mlen,
-    const unsigned char *sk
-    )
-{
-  sc25519 sck, scs, scsk;
-  ge25519 ger;
-  unsigned char r[32];
-  unsigned char s[32];
-  unsigned char extsk[64];
-  unsigned long long i;
-  unsigned char hmg[crypto_hash_sha512_BYTES];
-  unsigned char hram[crypto_hash_sha512_BYTES];
-
-  crypto_hash_sha512(extsk, sk, 32);
-  extsk[0] &= 248;
-  extsk[31] &= 127;
-  extsk[31] |= 64;
-
-  *smlen = mlen+64;
-  for(i=0;i<mlen;i++)
-    sm[64 + i] = m[i];
-  for(i=0;i<32;i++)
-    sm[32 + i] = extsk[32+i];
-
-  crypto_hash_sha512(hmg, sm+32, mlen+32); /* Generate k as h(extsk[32],...,extsk[63],m) */
-
-  /* Computation of R */
-  sc25519_from64bytes(&sck, hmg);
-  ge25519_scalarmult_base(&ger, &sck);
-  ge25519_pack(r, &ger);
-  
-  /* Computation of s */
-  for(i=0;i<32;i++)
-    sm[i] = r[i];
-
-  get_hram(hram, sm, sk+32, sm, mlen+64);
-
-  sc25519_from64bytes(&scs, hram);
-  sc25519_from32bytes(&scsk, extsk);
-  sc25519_mul(&scs, &scs, &scsk);
-  
-  sc25519_add(&scs, &scs, &sck);
-
-  sc25519_to32bytes(s,&scs); /* cat s */
-  for(i=0;i<32;i++)
-    sm[32 + i] = s[i]; 
-
-  return 0;
-}
-
-static int crypto_sign_open(
-    unsigned char *m,unsigned long long *mlen,
-    const unsigned char *sm,unsigned long long smlen,
-    const unsigned char *pk
-    )
-{
-  int i, ret;
-  unsigned char t2[32];
-  ge25519 get1, get2;
-  sc25519 schram, scs;
-  unsigned char hram[crypto_hash_sha512_BYTES];
-
-  *mlen = (unsigned long long) -1;
-  if (smlen < 64) return -1;
-
-  if (ge25519_unpackneg_vartime(&get1, pk)) return -1;
-
-  get_hram(hram,sm,pk,m,smlen);
-
-  sc25519_from64bytes(&schram, hram);
-
-  sc25519_from32bytes(&scs, sm+32);
-
-  ge25519_double_scalarmult_vartime(&get2, &get1, &schram, &ge25519_base, &scs);
-  ge25519_pack(t2, &get2);
-
-  ret = crypto_verify_32(sm, t2);
-
-  if (!ret)
-  {
-    for(i=0;i<smlen-64;i++)
-      m[i] = sm[i + 64];
-    *mlen = smlen-64;
-  }
-  else
-  {
-    for(i=0;i<smlen-64;i++)
-      m[i] = 0;
-  }
-  return ret;
-}
-#endif // 0
-
 //////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////
 

+ 0 - 1
node/Poly1305.cpp

@@ -140,7 +140,6 @@ typedef struct poly1305_context {
 //////////////////////////////////////////////////////////////////////////////
 // 128-bit implementation for MSC and GCC from Poly1305-donna
 
-
 #if defined(_MSC_VER)
   #include <intrin.h>
 

+ 15 - 40
node/SharedPtr.hpp

@@ -33,41 +33,19 @@
 namespace ZeroTier {
 
 /**
- * Simple reference counted pointer
+ * Simple zero-overhead introspective reference counted pointer
  *
  * This is an introspective shared pointer. Classes that need to be reference
  * counted must list this as a 'friend' and must have a private instance of
- * AtomicCounter called __refCount. They should also have private destructors,
- * since only this class should delete them.
- *
- * Because this is introspective, it is safe to apply to a naked pointer
- * multiple times provided there is always at least one holding SharedPtr.
- *
- * Once C++11 is ubiquitous, this and a few other things like Thread might get
- * torn out for their standard equivalents.
+ * AtomicCounter called __refCount.
  */
 template<typename T>
 class SharedPtr
 {
 public:
-	SharedPtr()
-		throw() :
-		_ptr((T *)0)
-	{
-	}
-
-	SharedPtr(T *obj)
-		throw() :
-		_ptr(obj)
-	{
-		++obj->__refCount;
-	}
-
-	SharedPtr(const SharedPtr &sp)
-		throw() :
-		_ptr(sp._getAndInc())
-	{
-	}
+	SharedPtr() : _ptr((T *)0) {}
+	SharedPtr(T *obj) : _ptr(obj) { ++obj->__refCount; }
+	SharedPtr(const SharedPtr &sp) : _ptr(sp._getAndInc()) {}
 
 	~SharedPtr()
 	{
@@ -110,21 +88,20 @@ public:
 	 * @param with Pointer to swap with
 	 */
 	inline void swap(SharedPtr &with)
-		throw()
 	{
 		T *tmp = _ptr;
 		_ptr = with._ptr;
 		with._ptr = tmp;
 	}
 
-	inline operator bool() const throw() { return (_ptr != (T *)0); }
-	inline T &operator*() const throw() { return *_ptr; }
-	inline T *operator->() const throw() { return _ptr; }
+	inline operator bool() const { return (_ptr != (T *)0); }
+	inline T &operator*() const { return *_ptr; }
+	inline T *operator->() const { return _ptr; }
 
 	/**
 	 * @return Raw pointer to held object
 	 */
-	inline T *ptr() const throw() { return _ptr; }
+	inline T *ptr() const { return _ptr; }
 
 	/**
 	 * Set this pointer to NULL
@@ -162,22 +139,20 @@ public:
 		}
 	}
 
-	inline bool operator==(const SharedPtr &sp) const throw() { return (_ptr == sp._ptr); }
-	inline bool operator!=(const SharedPtr &sp) const throw() { return (_ptr != sp._ptr); }
-	inline bool operator>(const SharedPtr &sp) const throw() { return (_ptr > sp._ptr); }
-	inline bool operator<(const SharedPtr &sp) const throw() { return (_ptr < sp._ptr); }
-	inline bool operator>=(const SharedPtr &sp) const throw() { return (_ptr >= sp._ptr); }
-	inline bool operator<=(const SharedPtr &sp) const throw() { return (_ptr <= sp._ptr); }
+	inline bool operator==(const SharedPtr &sp) const { return (_ptr == sp._ptr); }
+	inline bool operator!=(const SharedPtr &sp) const { return (_ptr != sp._ptr); }
+	inline bool operator>(const SharedPtr &sp) const { return (_ptr > sp._ptr); }
+	inline bool operator<(const SharedPtr &sp) const { return (_ptr < sp._ptr); }
+	inline bool operator>=(const SharedPtr &sp) const { return (_ptr >= sp._ptr); }
+	inline bool operator<=(const SharedPtr &sp) const { return (_ptr <= sp._ptr); }
 
 private:
 	inline T *_getAndInc() const
-		throw()
 	{
 		if (_ptr)
 			++_ptr->__refCount;
 		return _ptr;
 	}
-
 	T *_ptr;
 };
 

+ 18 - 4
node/Utils.hpp

@@ -76,7 +76,7 @@ public:
 	 */
 	static char *decimal(unsigned long n,char s[24]);
 
-	static inline char *hex(uint64_t i,char *const s)
+	static inline char *hex(uint64_t i,char s[17])
 	{
 		s[0] = HEXCHARS[(i >> 60) & 0xf];
 		s[1] = HEXCHARS[(i >> 56) & 0xf];
@@ -98,7 +98,7 @@ public:
 		return s;
 	}
 
-	static inline char *hex10(uint64_t i,char *const s)
+	static inline char *hex10(uint64_t i,char s[11])
 	{
 		s[0] = HEXCHARS[(i >> 36) & 0xf];
 		s[1] = HEXCHARS[(i >> 32) & 0xf];
@@ -114,7 +114,21 @@ public:
 		return s;
 	}
 
-	static inline char *hex(uint16_t i,char *const s)
+	static inline char *hex(uint32_t i,char s[9])
+	{
+		s[0] = HEXCHARS[(i >> 28) & 0xf];
+		s[1] = HEXCHARS[(i >> 24) & 0xf];
+		s[2] = HEXCHARS[(i >> 20) & 0xf];
+		s[3] = HEXCHARS[(i >> 16) & 0xf];
+		s[4] = HEXCHARS[(i >> 12) & 0xf];
+		s[5] = HEXCHARS[(i >> 8) & 0xf];
+		s[6] = HEXCHARS[(i >> 4) & 0xf];
+		s[7] = HEXCHARS[i & 0xf];
+		s[8] = (char)0;
+		return s;
+	}
+
+	static inline char *hex(uint16_t i,char s[5])
 	{
 		s[0] = HEXCHARS[(i >> 12) & 0xf];
 		s[1] = HEXCHARS[(i >> 8) & 0xf];
@@ -124,7 +138,7 @@ public:
 		return s;
 	}
 
-	static inline char *hex(uint8_t i,char *const s)
+	static inline char *hex(uint8_t i,char s[3])
 	{
 		s[0] = HEXCHARS[(i >> 4) & 0xf];
 		s[1] = HEXCHARS[i & 0xf];

+ 2 - 2
osdep/Binder.hpp

@@ -176,7 +176,7 @@ public:
 		const unsigned long pid = (unsigned long)getpid();
 
 		// Get all device names
-		Utils::ztsnprintf(fn,sizeof(fn),"/proc/%lu/net/dev",pid);
+		OSUtils::ztsnprintf(fn,sizeof(fn),"/proc/%lu/net/dev",pid);
 		FILE *procf = fopen(fn,"r");
 		if (procf) {
 			while (fgets(tmp,sizeof(tmp),procf)) {
@@ -192,7 +192,7 @@ public:
 		}
 
 		// Get IPv6 addresses (and any device names we don't already know)
-		Utils::ztsnprintf(fn,sizeof(fn),"/proc/%lu/net/if_inet6",pid);
+		OSUtils::ztsnprintf(fn,sizeof(fn),"/proc/%lu/net/if_inet6",pid);
 		procf = fopen(fn,"r");
 		if (procf) {
 			while (fgets(tmp,sizeof(tmp),procf)) {

+ 5 - 4
osdep/ManagedRoute.cpp

@@ -286,12 +286,13 @@ static void _routeCmd(const char *op,const InetAddress &target,const InetAddress
 	} else if (p == 0) {
 		::close(STDOUT_FILENO);
 		::close(STDERR_FILENO);
+		char ipbuf[64],ipbuf2[64];
 		if (via) {
-			::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"via",via.toIpString().c_str(),(const char *)0);
-			::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"via",via.toIpString().c_str(),(const char *)0);
+			::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"via",via.toIpString(ipbuf2),(const char *)0);
+			::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"via",via.toIpString(ipbuf2),(const char *)0);
 		} else if ((localInterface)&&(localInterface[0])) {
-			::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"dev",localInterface,(const char *)0);
-			::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString().c_str(),"dev",localInterface,(const char *)0);
+			::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"dev",localInterface,(const char *)0);
+			::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"dev",localInterface,(const char *)0);
 		}
 		::_exit(-1);
 	}

+ 1 - 1
selftest.cpp

@@ -844,7 +844,7 @@ static int testOther()
 		memset(key, 0, sizeof(key));
 		memset(value, 0, sizeof(value));
 		for(unsigned int q=0;q<32;++q) {
-			OSUtils::ztsnprintf(key[q],16,"%.8lx",(unsigned long)(rand() % 1000) + (q * 1000));
+			Utils::hex((uint32_t)((rand() % 1000) + (q * 1000)),key[q]);
 			int r = rand() % 128;
 			for(int x=0;x<r;++x)
 				value[q][x] = ("0123456789\0\t\r\n= ")[rand() % 16];