Add disable encryption to cmake for forseeable future and remove the header check for client requests

Rename lokinet_identity to lokid_key and update other references to lokinet key

Load the lokid key and generate snode address from it
This commit is contained in:
Beaudan 2019-04-08 13:31:21 +10:00
parent 08ab6d7483
commit 571722e281
16 changed files with 2024 additions and 113 deletions

View file

@ -6,8 +6,8 @@ set(CMAKE_CXX_STANDARD 11)
set(CXX_STANDARD_REQUIRED ON)
set(SOURCES
src/lokinet_identity.cpp
include/lokinet_identity.hpp
src/lokid_key.cpp
include/lokid_key.h
include/channel_encryption.hpp
src/channel_encryption.cpp
)

View file

@ -7,7 +7,7 @@
template <typename T>
class ChannelEncryption {
public:
ChannelEncryption(const std::string& identityPrivatePath = "");
ChannelEncryption(const std::string& key_path = "");
~ChannelEncryption() = default;
T encrypt(const T& plainText, const std::string& pubKey) const;
@ -17,5 +17,8 @@ class ChannelEncryption {
private:
std::vector<uint8_t>
calculateSharedSecret(const std::vector<uint8_t>& pubKey) const;
std::vector<uint8_t> privateKey;
std::vector<uint8_t> private_key;
public:
std::vector<uint8_t> public_key;
};

View file

@ -0,0 +1,9 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
std::vector<uint8_t> parseLokidKey(const std::string& path);
std::vector<uint8_t> calcPublicKey(const std::vector<uint8_t>& private_key);

View file

@ -1,9 +0,0 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
std::vector<uint8_t> parseLokinetIdentityPrivate(const std::string& path);
std::vector<uint8_t> parseLokinetIdentityPublic(const std::string& path);

View file

@ -0,0 +1,246 @@
#ifndef common_H
#define common_H 1
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
#ifdef HAVE_TI_MODE
# if defined(__SIZEOF_INT128__)
typedef unsigned __int128 uint128_t;
# else
typedef unsigned uint128_t __attribute__((mode(TI)));
# endif
#endif
#define ROTL32(X, B) rotl32((X), (B))
static inline uint32_t
rotl32(const uint32_t x, const int b)
{
return (x << b) | (x >> (32 - b));
}
#define ROTL64(X, B) rotl64((X), (B))
static inline uint64_t
rotl64(const uint64_t x, const int b)
{
return (x << b) | (x >> (64 - b));
}
#define ROTR32(X, B) rotr32((X), (B))
static inline uint32_t
rotr32(const uint32_t x, const int b)
{
return (x >> b) | (x << (32 - b));
}
#define ROTR64(X, B) rotr64((X), (B))
static inline uint64_t
rotr64(const uint64_t x, const int b)
{
return (x >> b) | (x << (64 - b));
}
#define LOAD64_LE(SRC) load64_le(SRC)
static inline uint64_t
load64_le(const uint8_t src[8])
{
#ifdef NATIVE_LITTLE_ENDIAN
uint64_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint64_t w = (uint64_t) src[0];
w |= (uint64_t) src[1] << 8;
w |= (uint64_t) src[2] << 16;
w |= (uint64_t) src[3] << 24;
w |= (uint64_t) src[4] << 32;
w |= (uint64_t) src[5] << 40;
w |= (uint64_t) src[6] << 48;
w |= (uint64_t) src[7] << 56;
return w;
#endif
}
#define STORE64_LE(DST, W) store64_le((DST), (W))
static inline void
store64_le(uint8_t dst[8], uint64_t w)
{
#ifdef NATIVE_LITTLE_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[0] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[3] = (uint8_t) w; w >>= 8;
dst[4] = (uint8_t) w; w >>= 8;
dst[5] = (uint8_t) w; w >>= 8;
dst[6] = (uint8_t) w; w >>= 8;
dst[7] = (uint8_t) w;
#endif
}
#define LOAD32_LE(SRC) load32_le(SRC)
static inline uint32_t
load32_le(const uint8_t src[4])
{
#ifdef NATIVE_LITTLE_ENDIAN
uint32_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint32_t w = (uint32_t) src[0];
w |= (uint32_t) src[1] << 8;
w |= (uint32_t) src[2] << 16;
w |= (uint32_t) src[3] << 24;
return w;
#endif
}
#define STORE32_LE(DST, W) store32_le((DST), (W))
static inline void
store32_le(uint8_t dst[4], uint32_t w)
{
#ifdef NATIVE_LITTLE_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[0] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[3] = (uint8_t) w;
#endif
}
/* ----- */
#define LOAD64_BE(SRC) load64_be(SRC)
static inline uint64_t
load64_be(const uint8_t src[8])
{
#ifdef NATIVE_BIG_ENDIAN
uint64_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint64_t w = (uint64_t) src[7];
w |= (uint64_t) src[6] << 8;
w |= (uint64_t) src[5] << 16;
w |= (uint64_t) src[4] << 24;
w |= (uint64_t) src[3] << 32;
w |= (uint64_t) src[2] << 40;
w |= (uint64_t) src[1] << 48;
w |= (uint64_t) src[0] << 56;
return w;
#endif
}
#define STORE64_BE(DST, W) store64_be((DST), (W))
static inline void
store64_be(uint8_t dst[8], uint64_t w)
{
#ifdef NATIVE_BIG_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[7] = (uint8_t) w; w >>= 8;
dst[6] = (uint8_t) w; w >>= 8;
dst[5] = (uint8_t) w; w >>= 8;
dst[4] = (uint8_t) w; w >>= 8;
dst[3] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[0] = (uint8_t) w;
#endif
}
#define LOAD32_BE(SRC) load32_be(SRC)
static inline uint32_t
load32_be(const uint8_t src[4])
{
#ifdef NATIVE_BIG_ENDIAN
uint32_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint32_t w = (uint32_t) src[3];
w |= (uint32_t) src[2] << 8;
w |= (uint32_t) src[1] << 16;
w |= (uint32_t) src[0] << 24;
return w;
#endif
}
#define STORE32_BE(DST, W) store32_be((DST), (W))
static inline void
store32_be(uint8_t dst[4], uint32_t w)
{
#ifdef NATIVE_BIG_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[3] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[0] = (uint8_t) w;
#endif
}
#define XOR_BUF(OUT, IN, N) xor_buf((OUT), (IN), (N))
static inline void
xor_buf(unsigned char *out, const unsigned char *in, size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
out[i] ^= in[i];
}
}
#if !defined(__clang__) && !defined(__GNUC__)
# ifdef __attribute__
# undef __attribute__
# endif
# define __attribute__(a)
#endif
#ifndef CRYPTO_ALIGN
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
# define CRYPTO_ALIGN(x) __declspec(align(x))
# else
# define CRYPTO_ALIGN(x) __attribute__ ((aligned(x)))
# endif
#endif
#if defined(_MSC_VER) && \
(defined(_M_X64) || defined(_M_AMD64) || defined(_M_IX86))
# include <intrin.h>
# define HAVE_INTRIN_H 1
# define HAVE_MMINTRIN_H 1
# define HAVE_EMMINTRIN_H 1
# define HAVE_PMMINTRIN_H 1
# define HAVE_TMMINTRIN_H 1
# define HAVE_SMMINTRIN_H 1
# define HAVE_AVXINTRIN_H 1
# if _MSC_VER >= 1600
# define HAVE_WMMINTRIN_H 1
# endif
# if _MSC_VER >= 1700 && defined(_M_X64)
# define HAVE_AVX2INTRIN_H 1
# endif
#elif defined(HAVE_INTRIN_H)
# include <intrin.h>
#endif
#ifdef HAVE_LIBCTGRIND
extern void ct_poison (const void *, size_t);
extern void ct_unpoison(const void *, size_t);
# define POISON(X, L) ct_poison((X), (L))
# define UNPOISON(X, L) ct_unpoison((X), (L))
#else
# define POISON(X, L) (void) 0
# define UNPOISON(X, L) (void) 0
#endif
#endif

View file

@ -0,0 +1,125 @@
#ifndef ed25519_ref10_H
#define ed25519_ref10_H
#include <stddef.h>
#include <stdint.h>
/*
fe means field element.
Here the field is \Z/(2^255-19).
*/
#ifdef HAVE_TI_MODE
typedef uint64_t fe25519[5];
#else
typedef int32_t fe25519[10];
#endif
void fe25519_invert(fe25519 out, const fe25519 z);
void fe25519_frombytes(fe25519 h, const unsigned char *s);
void fe25519_tobytes(unsigned char *s, const fe25519 h);
#ifdef HAVE_TI_MODE
# include "ed25519_ref10_fe_51.h"
#else
# include "ed25519_ref10_fe_25_5.h"
#endif
/*
ge means group element.
Here the group is the set of pairs (x,y) of field elements
satisfying -x^2 + y^2 = 1 + d x^2y^2
where d = -121665/121666.
Representations:
ge25519_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
ge25519_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
ge25519_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
ge25519_precomp (Duif): (y+x,y-x,2dxy)
*/
typedef struct {
fe25519 X;
fe25519 Y;
fe25519 Z;
} ge25519_p2;
typedef struct {
fe25519 X;
fe25519 Y;
fe25519 Z;
fe25519 T;
} ge25519_p3;
typedef struct {
fe25519 X;
fe25519 Y;
fe25519 Z;
fe25519 T;
} ge25519_p1p1;
typedef struct {
fe25519 yplusx;
fe25519 yminusx;
fe25519 xy2d;
} ge25519_precomp;
typedef struct {
fe25519 YplusX;
fe25519 YminusX;
fe25519 Z;
fe25519 T2d;
} ge25519_cached;
void ge25519_tobytes(unsigned char *s, const ge25519_p2 *h);
void ge25519_p3_tobytes(unsigned char *s, const ge25519_p3 *h);
int ge25519_frombytes(ge25519_p3 *h, const unsigned char *s);
int ge25519_frombytes_negate_vartime(ge25519_p3 *h, const unsigned char *s);
void ge25519_p3_to_cached(ge25519_cached *r, const ge25519_p3 *p);
void ge25519_p1p1_to_p2(ge25519_p2 *r, const ge25519_p1p1 *p);
void ge25519_p1p1_to_p3(ge25519_p3 *r, const ge25519_p1p1 *p);
void ge25519_add(ge25519_p1p1 *r, const ge25519_p3 *p, const ge25519_cached *q);
void ge25519_sub(ge25519_p1p1 *r, const ge25519_p3 *p, const ge25519_cached *q);
void ge25519_scalarmult_base(ge25519_p3 *h, const unsigned char *a);
void ge25519_double_scalarmult_vartime(ge25519_p2 *r, const unsigned char *a,
const ge25519_p3 *A,
const unsigned char *b);
void ge25519_scalarmult(ge25519_p3 *h, const unsigned char *a,
const ge25519_p3 *p);
int ge25519_is_canonical(const unsigned char *s);
int ge25519_is_on_curve(const ge25519_p3 *p);
int ge25519_is_on_main_subgroup(const ge25519_p3 *p);
int ge25519_has_small_order(const unsigned char s[32]);
void ge25519_from_uniform(unsigned char s[32], const unsigned char r[32]);
/*
The set of scalars is \Z/l
where l = 2^252 + 27742317777372353535851937790883648493.
*/
void sc25519_reduce(unsigned char *s);
void sc25519_muladd(unsigned char *s, const unsigned char *a,
const unsigned char *b, const unsigned char *c);
int sc25519_is_canonical(const unsigned char *s);
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,518 @@
#include <string.h>
#include "private/common.h"
#include "utils.h"
/*
h = 0
*/
static inline void
fe25519_0(fe25519 h)
{
memset(&h[0], 0, 5 * sizeof h[0]);
}
/*
h = 1
*/
static inline void
fe25519_1(fe25519 h)
{
h[0] = 1;
memset(&h[1], 0, 4 * sizeof h[0]);
}
/*
h = f + g
Can overlap h with f or g.
*/
static inline void
fe25519_add(fe25519 h, const fe25519 f, const fe25519 g)
{
uint64_t h0 = f[0] + g[0];
uint64_t h1 = f[1] + g[1];
uint64_t h2 = f[2] + g[2];
uint64_t h3 = f[3] + g[3];
uint64_t h4 = f[4] + g[4];
h[0] = h0;
h[1] = h1;
h[2] = h2;
h[3] = h3;
h[4] = h4;
}
/*
h = f - g
*/
static void
fe25519_sub(fe25519 h, const fe25519 f, const fe25519 g)
{
const uint64_t mask = 0x7ffffffffffffULL;
uint64_t h0, h1, h2, h3, h4;
h0 = g[0];
h1 = g[1];
h2 = g[2];
h3 = g[3];
h4 = g[4];
h1 += h0 >> 51;
h0 &= mask;
h2 += h1 >> 51;
h1 &= mask;
h3 += h2 >> 51;
h2 &= mask;
h4 += h3 >> 51;
h3 &= mask;
h0 += 19ULL * (h4 >> 51);
h4 &= mask;
h0 = (f[0] + 0xfffffffffffdaULL) - h0;
h1 = (f[1] + 0xffffffffffffeULL) - h1;
h2 = (f[2] + 0xffffffffffffeULL) - h2;
h3 = (f[3] + 0xffffffffffffeULL) - h3;
h4 = (f[4] + 0xffffffffffffeULL) - h4;
h[0] = h0;
h[1] = h1;
h[2] = h2;
h[3] = h3;
h[4] = h4;
}
/*
h = -f
*/
static inline void
fe25519_neg(fe25519 h, const fe25519 f)
{
fe25519 zero;
fe25519_0(zero);
fe25519_sub(h, zero, f);
}
/*
Replace (f,g) with (g,g) if b == 1;
replace (f,g) with (f,g) if b == 0.
*
Preconditions: b in {0,1}.
*/
static void
fe25519_cmov(fe25519 f, const fe25519 g, unsigned int b)
{
const uint64_t mask = (uint64_t) (-(int64_t) b);
uint64_t f0 = f[0];
uint64_t f1 = f[1];
uint64_t f2 = f[2];
uint64_t f3 = f[3];
uint64_t f4 = f[4];
uint64_t x0 = f0 ^ g[0];
uint64_t x1 = f1 ^ g[1];
uint64_t x2 = f2 ^ g[2];
uint64_t x3 = f3 ^ g[3];
uint64_t x4 = f4 ^ g[4];
x0 &= mask;
x1 &= mask;
x2 &= mask;
x3 &= mask;
x4 &= mask;
f[0] = f0 ^ x0;
f[1] = f1 ^ x1;
f[2] = f2 ^ x2;
f[3] = f3 ^ x3;
f[4] = f4 ^ x4;
}
/*
Replace (f,g) with (g,f) if b == 1;
replace (f,g) with (f,g) if b == 0.
Preconditions: b in {0,1}.
*/
static void
fe25519_cswap(fe25519 f, fe25519 g, unsigned int b)
{
const uint64_t mask = (uint64_t) (-(int64_t) b);
uint64_t f0 = f[0];
uint64_t f1 = f[1];
uint64_t f2 = f[2];
uint64_t f3 = f[3];
uint64_t f4 = f[4];
uint64_t g0 = g[0];
uint64_t g1 = g[1];
uint64_t g2 = g[2];
uint64_t g3 = g[3];
uint64_t g4 = g[4];
uint64_t x0 = f0 ^ g0;
uint64_t x1 = f1 ^ g1;
uint64_t x2 = f2 ^ g2;
uint64_t x3 = f3 ^ g3;
uint64_t x4 = f4 ^ g4;
x0 &= mask;
x1 &= mask;
x2 &= mask;
x3 &= mask;
x4 &= mask;
f[0] = f0 ^ x0;
f[1] = f1 ^ x1;
f[2] = f2 ^ x2;
f[3] = f3 ^ x3;
f[4] = f4 ^ x4;
g[0] = g0 ^ x0;
g[1] = g1 ^ x1;
g[2] = g2 ^ x2;
g[3] = g3 ^ x3;
g[4] = g4 ^ x4;
}
/*
h = f
*/
static inline void
fe25519_copy(fe25519 h, const fe25519 f)
{
uint64_t f0 = f[0];
uint64_t f1 = f[1];
uint64_t f2 = f[2];
uint64_t f3 = f[3];
uint64_t f4 = f[4];
h[0] = f0;
h[1] = f1;
h[2] = f2;
h[3] = f3;
h[4] = f4;
}
/*
return 1 if f is in {1,3,5,...,q-2}
return 0 if f is in {0,2,4,...,q-1}
*/
static inline int
fe25519_isnegative(const fe25519 f)
{
unsigned char s[32];
fe25519_tobytes(s, f);
return s[0] & 1;
}
/*
return 1 if f == 0
return 0 if f != 0
*/
static inline int
fe25519_iszero(const fe25519 f)
{
unsigned char s[32];
fe25519_tobytes(s, f);
return sodium_is_zero(s, 32);
}
/*
h = f * g
Can overlap h with f or g.
*/
static void
fe25519_mul(fe25519 h, const fe25519 f, const fe25519 g)
{
const uint64_t mask = 0x7ffffffffffffULL;
uint128_t r0, r1, r2, r3, r4, carry;
uint64_t f0, f1, f2, f3, f4;
uint64_t f1_19, f2_19, f3_19, f4_19;
uint64_t g0, g1, g2, g3, g4;
uint64_t r00, r01, r02, r03, r04;
f0 = f[0];
f1 = f[1];
f2 = f[2];
f3 = f[3];
f4 = f[4];
g0 = g[0];
g1 = g[1];
g2 = g[2];
g3 = g[3];
g4 = g[4];
f1_19 = 19ULL * f1;
f2_19 = 19ULL * f2;
f3_19 = 19ULL * f3;
f4_19 = 19ULL * f4;
r0 = ((uint128_t) f0 ) * ((uint128_t) g0);
r0 += ((uint128_t) f1_19) * ((uint128_t) g4);
r0 += ((uint128_t) f2_19) * ((uint128_t) g3);
r0 += ((uint128_t) f3_19) * ((uint128_t) g2);
r0 += ((uint128_t) f4_19) * ((uint128_t) g1);
r1 = ((uint128_t) f0 ) * ((uint128_t) g1);
r1 += ((uint128_t) f1 ) * ((uint128_t) g0);
r1 += ((uint128_t) f2_19) * ((uint128_t) g4);
r1 += ((uint128_t) f3_19) * ((uint128_t) g3);
r1 += ((uint128_t) f4_19) * ((uint128_t) g2);
r2 = ((uint128_t) f0 ) * ((uint128_t) g2);
r2 += ((uint128_t) f1 ) * ((uint128_t) g1);
r2 += ((uint128_t) f2 ) * ((uint128_t) g0);
r2 += ((uint128_t) f3_19) * ((uint128_t) g4);
r2 += ((uint128_t) f4_19) * ((uint128_t) g3);
r3 = ((uint128_t) f0 ) * ((uint128_t) g3);
r3 += ((uint128_t) f1 ) * ((uint128_t) g2);
r3 += ((uint128_t) f2 ) * ((uint128_t) g1);
r3 += ((uint128_t) f3 ) * ((uint128_t) g0);
r3 += ((uint128_t) f4_19) * ((uint128_t) g4);
r4 = ((uint128_t) f0 ) * ((uint128_t) g4);
r4 += ((uint128_t) f1 ) * ((uint128_t) g3);
r4 += ((uint128_t) f2 ) * ((uint128_t) g2);
r4 += ((uint128_t) f3 ) * ((uint128_t) g1);
r4 += ((uint128_t) f4 ) * ((uint128_t) g0);
r00 = ((uint64_t) r0) & mask;
carry = r0 >> 51;
r1 += carry;
r01 = ((uint64_t) r1) & mask;
carry = r1 >> 51;
r2 += carry;
r02 = ((uint64_t) r2) & mask;
carry = r2 >> 51;
r3 += carry;
r03 = ((uint64_t) r3) & mask;
carry = r3 >> 51;
r4 += carry;
r04 = ((uint64_t) r4) & mask;
carry = r4 >> 51;
r00 += 19ULL * (uint64_t) carry;
carry = r00 >> 51;
r00 &= mask;
r01 += (uint64_t) carry;
carry = r01 >> 51;
r01 &= mask;
r02 += (uint64_t) carry;
h[0] = r00;
h[1] = r01;
h[2] = r02;
h[3] = r03;
h[4] = r04;
}
/*
h = f * f
Can overlap h with f.
*/
static void
fe25519_sq(fe25519 h, const fe25519 f)
{
const uint64_t mask = 0x7ffffffffffffULL;
uint128_t r0, r1, r2, r3, r4, carry;
uint64_t f0, f1, f2, f3, f4;
uint64_t f0_2, f1_2, f1_38, f2_38, f3_38, f3_19, f4_19;
uint64_t r00, r01, r02, r03, r04;
f0 = f[0];
f1 = f[1];
f2 = f[2];
f3 = f[3];
f4 = f[4];
f0_2 = f0 << 1;
f1_2 = f1 << 1;
f1_38 = 38ULL * f1;
f2_38 = 38ULL * f2;
f3_38 = 38ULL * f3;
f3_19 = 19ULL * f3;
f4_19 = 19ULL * f4;
r0 = ((uint128_t) f0 ) * ((uint128_t) f0);
r0 += ((uint128_t) f1_38) * ((uint128_t) f4);
r0 += ((uint128_t) f2_38) * ((uint128_t) f3);
r1 = ((uint128_t) f0_2 ) * ((uint128_t) f1);
r1 += ((uint128_t) f2_38) * ((uint128_t) f4);
r1 += ((uint128_t) f3_19) * ((uint128_t) f3);
r2 = ((uint128_t) f0_2 ) * ((uint128_t) f2);
r2 += ((uint128_t) f1 ) * ((uint128_t) f1);
r2 += ((uint128_t) f3_38) * ((uint128_t) f4);
r3 = ((uint128_t) f0_2 ) * ((uint128_t) f3);
r3 += ((uint128_t) f1_2 ) * ((uint128_t) f2);
r3 += ((uint128_t) f4_19) * ((uint128_t) f4);
r4 = ((uint128_t) f0_2 ) * ((uint128_t) f4);
r4 += ((uint128_t) f1_2 ) * ((uint128_t) f3);
r4 += ((uint128_t) f2 ) * ((uint128_t) f2);
r00 = ((uint64_t) r0) & mask;
carry = r0 >> 51;
r1 += carry;
r01 = ((uint64_t) r1) & mask;
carry = r1 >> 51;
r2 += carry;
r02 = ((uint64_t) r2) & mask;
carry = r2 >> 51;
r3 += carry;
r03 = ((uint64_t) r3) & mask;
carry = r3 >> 51;
r4 += carry;
r04 = ((uint64_t) r4) & mask;
carry = r4 >> 51;
r00 += 19ULL * (uint64_t) carry;
carry = r00 >> 51;
r00 &= mask;
r01 += (uint64_t) carry;
carry = r01 >> 51;
r01 &= mask;
r02 += (uint64_t) carry;
h[0] = r00;
h[1] = r01;
h[2] = r02;
h[3] = r03;
h[4] = r04;
}
/*
h = 2 * f * f
Can overlap h with f.
*/
static void
fe25519_sq2(fe25519 h, const fe25519 f)
{
const uint64_t mask = 0x7ffffffffffffULL;
uint128_t r0, r1, r2, r3, r4, carry;
uint64_t f0, f1, f2, f3, f4;
uint64_t f0_2, f1_2, f1_38, f2_38, f3_38, f3_19, f4_19;
uint64_t r00, r01, r02, r03, r04;
f0 = f[0];
f1 = f[1];
f2 = f[2];
f3 = f[3];
f4 = f[4];
f0_2 = f0 << 1;
f1_2 = f1 << 1;
f1_38 = 38ULL * f1;
f2_38 = 38ULL * f2;
f3_38 = 38ULL * f3;
f3_19 = 19ULL * f3;
f4_19 = 19ULL * f4;
r0 = ((uint128_t) f0 ) * ((uint128_t) f0);
r0 += ((uint128_t) f1_38) * ((uint128_t) f4);
r0 += ((uint128_t) f2_38) * ((uint128_t) f3);
r1 = ((uint128_t) f0_2 ) * ((uint128_t) f1);
r1 += ((uint128_t) f2_38) * ((uint128_t) f4);
r1 += ((uint128_t) f3_19) * ((uint128_t) f3);
r2 = ((uint128_t) f0_2 ) * ((uint128_t) f2);
r2 += ((uint128_t) f1 ) * ((uint128_t) f1);
r2 += ((uint128_t) f3_38) * ((uint128_t) f4);
r3 = ((uint128_t) f0_2 ) * ((uint128_t) f3);
r3 += ((uint128_t) f1_2 ) * ((uint128_t) f2);
r3 += ((uint128_t) f4_19) * ((uint128_t) f4);
r4 = ((uint128_t) f0_2 ) * ((uint128_t) f4);
r4 += ((uint128_t) f1_2 ) * ((uint128_t) f3);
r4 += ((uint128_t) f2 ) * ((uint128_t) f2);
r0 <<= 1;
r1 <<= 1;
r2 <<= 1;
r3 <<= 1;
r4 <<= 1;
r00 = ((uint64_t) r0) & mask;
carry = r0 >> 51;
r1 += carry;
r01 = ((uint64_t) r1) & mask;
carry = r1 >> 51;
r2 += carry;
r02 = ((uint64_t) r2) & mask;
carry = r2 >> 51;
r3 += carry;
r03 = ((uint64_t) r3) & mask;
carry = r3 >> 51;
r4 += carry;
r04 = ((uint64_t) r4) & mask;
carry = r4 >> 51;
r00 += 19ULL * (uint64_t) carry;
carry = r00 >> 51;
r00 &= mask;
r01 += (uint64_t) carry;
carry = r01 >> 51;
r01 &= mask;
r02 += (uint64_t) carry;
h[0] = r00;
h[1] = r01;
h[2] = r02;
h[3] = r03;
h[4] = r04;
}
static void
fe25519_scalar_product(fe25519 h, const fe25519 f, uint32_t n)
{
const uint64_t mask = 0x7ffffffffffffULL;
uint128_t a;
uint128_t sn = (uint128_t) n;
uint64_t h0, h1, h2, h3, h4;
a = f[0] * sn;
h0 = ((uint64_t) a) & mask;
a = f[1] * sn + ((uint64_t) (a >> 51));
h1 = ((uint64_t) a) & mask;
a = f[2] * sn + ((uint64_t) (a >> 51));
h2 = ((uint64_t) a) & mask;
a = f[3] * sn + ((uint64_t) (a >> 51));
h3 = ((uint64_t) a) & mask;
a = f[4] * sn + ((uint64_t) (a >> 51));
h4 = ((uint64_t) a) & mask;
h0 += (a >> 51) * 19ULL;
h[0] = h0;
h[1] = h1;
h[2] = h2;
h[3] = h3;
h[4] = h4;
}

View file

@ -1,5 +1,5 @@
#include "channel_encryption.hpp"
#include "lokinet_identity.hpp"
#include "lokid_key.h"
#include <boost/algorithm/hex.hpp>
#include <openssl/evp.h>
@ -16,19 +16,10 @@ std::vector<uint8_t> hexToBytes(const std::string& hex) {
}
template <typename T>
ChannelEncryption<T>::ChannelEncryption(
const std::string& identityPrivatePath) {
// Lokinet identity uses ed25519
const std::vector<uint8_t> privateEd25519Key =
parseLokinetIdentityPrivate(identityPrivatePath);
this->privateKey.resize(crypto_scalarmult_curve25519_BYTES);
// Convert to curve25519
if (crypto_sign_ed25519_sk_to_curve25519(this->privateKey.data(),
privateEd25519Key.data()) != 0) {
throw std::runtime_error(
"Could not convert lokinet private key from ed25519 to curve25519");
}
ChannelEncryption<T>::ChannelEncryption(const std::string& key_path) {
// Lokid uses ed25519
this->private_key = parseLokidKey(key_path);
this->public_key = calcPublicKey(this->private_key);
}
template <typename T>
@ -38,7 +29,7 @@ std::vector<uint8_t> ChannelEncryption<T>::calculateSharedSecret(
if (pubKey.size() != crypto_scalarmult_curve25519_BYTES) {
throw std::runtime_error("Bad pubKey size");
}
if (crypto_scalarmult(sharedSecret.data(), this->privateKey.data(),
if (crypto_scalarmult(sharedSecret.data(), this->private_key.data(),
pubKey.data()) != 0) {
throw std::runtime_error(
"Shared key derivation failed (crypto_scalarmult)");

46
crypto/src/lokid_key.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "lokid_key.h"
extern "C" {
#include "sodium/private/ed25519_ref10.h"
}
#include <boost/filesystem.hpp>
#include <exception>
#include <fstream>
#include <iterator>
namespace fs = boost::filesystem;
constexpr size_t KEY_LENGTH = 32;
std::vector<uint8_t> parseLokidKey(const std::string& path) {
fs::path p(path);
if (p.empty()) {
#ifdef _WIN32
const fs::path homedir = fs::pathpath(getenv("APPDATA"));
#else
const fs::path homedir = fs::path(getenv("HOME"));
#endif
const fs::path basepath = homedir / fs::path(".loki");
p = basepath / "key";
}
if (!fs::exists(p)) {
throw std::runtime_error(
"Lokid key file could not be found");
}
std::ifstream input(p.c_str(), std::ios::binary);
const std::vector<uint8_t> privateKey(std::istreambuf_iterator<char>(input), {});
return privateKey;
}
std::vector<uint8_t> calcPublicKey(const std::vector<uint8_t>& private_key) {
ge25519_p3 A;
ge25519_scalarmult_base(&A, private_key.data());
std::vector<uint8_t> publicKey(KEY_LENGTH);
ge25519_p3_tobytes(publicKey.data(), &A);
return publicKey;
}

View file

@ -1,65 +0,0 @@
#include "lokinet_identity.hpp"
#include <boost/filesystem.hpp>
#include <exception>
#include <fstream>
#include <iterator>
namespace fs = boost::filesystem;
constexpr size_t PRIVATE_KEY_OFFSET = 3;
constexpr size_t KEY_LENGTH = 32;
constexpr size_t PUBLIC_KEY_OFFSET = PRIVATE_KEY_OFFSET + KEY_LENGTH;
std::vector<uint8_t> parseLokinetIdentityPrivate(const std::string& path) {
fs::path p(path);
if (p.empty()) {
#ifdef _WIN32
const fs::path homedir = fs::pathpath(getenv("APPDATA"));
#else
const fs::path homedir = fs::path(getenv("HOME"));
#endif
const fs::path basepath = homedir / fs::path(".lokinet");
p = basepath / "identity.private";
}
if (!fs::exists(p)) {
throw std::runtime_error(
"Lokinet identity.private file could not be found");
}
std::ifstream input(p.c_str(), std::ios::binary);
const std::vector<uint8_t> bytes(std::istreambuf_iterator<char>(input), {});
const std::vector<uint8_t> privateKey(bytes.begin() + PRIVATE_KEY_OFFSET,
bytes.begin() + PRIVATE_KEY_OFFSET +
KEY_LENGTH);
return privateKey;
}
std::vector<uint8_t> parseLokinetIdentityPublic(const std::string& path) {
fs::path p(path);
if (p.empty()) {
#ifdef _WIN32
const fs::path homedir = fs::pathpath(getenv("APPDATA"));
#else
const fs::path homedir = fs::path(getenv("HOME"));
#endif
const fs::path basepath = homedir / fs::path(".lokinet");
p = basepath / "identity.private";
}
if (!fs::exists(p)) {
throw std::runtime_error(
"Lokinet identity.private file could not be found");
}
std::ifstream input(p.c_str(), std::ios::binary);
const std::vector<uint8_t> bytes(std::istreambuf_iterator<char>(input), {});
const std::vector<uint8_t> publicKey(bytes.begin() + PUBLIC_KEY_OFFSET,
bytes.begin() + PUBLIC_KEY_OFFSET +
KEY_LENGTH);
return publicKey;
}

View file

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 2.8.0)
add_definitions(-DBOOST_LOG_DYN_LINK)
add_definitions(-DDISABLE_ENCRYPTION)
project(httpserver)

View file

@ -645,15 +645,15 @@ void connection_t::process_retrieve(const json& params) {
}
void connection_t::process_client_req() {
std::string plainText = request_.body();
#ifndef DISABLE_ENCRYPTION
const std::vector<std::string> keys = {LOKI_EPHEMKEY_HEADER};
if (!parse_header(keys)) {
BOOST_LOG_TRIVIAL(error) << "Could not parse headers\n";
return;
}
std::string plainText = request_.body();
#ifndef DISABLE_ENCRYPTION
try {
const std::string decoded =
boost::beast::detail::base64_decode(plainText);

View file

@ -34,7 +34,7 @@ static const LogLevelMap logLevelMap{
void usage(char* argv[]) {
std::cerr << "Usage: " << argv[0]
<< " <address> <port> [--lokinet-identity path] [--db-location "
<< " <address> <port> [--lokid-key path] [--db-location "
"path] [--log-level level]\n";
std::cerr << " For IPv4, try:\n";
std::cerr << " receiver 0.0.0.0 80\n";
@ -67,7 +67,7 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE;
}
std::string lokinetIdentityPath;
std::string lokidKeyPath;
std::string dbLocation(".");
std::string logLocation;
std::string logLevelString("info");
@ -76,7 +76,7 @@ int main(int argc, char* argv[]) {
std::string ip = argv[1];
po::options_description desc;
desc.add_options()("lokinet-identity", po::value(&lokinetIdentityPath),
desc.add_options()("lokid-key", po::value(&lokidKeyPath),
"")("db-location", po::value(&dbLocation),
"")("output-log", po::value(&logLocation), "")(
"log-level", po::value(&logLevelString), "");
@ -103,9 +103,9 @@ int main(int argc, char* argv[]) {
logLevel);
BOOST_LOG_TRIVIAL(info) << "Setting log level to " << logLevelString;
if (vm.count("lokinet-identity")) {
if (vm.count("lokid-key")) {
BOOST_LOG_TRIVIAL(info)
<< "Setting identity.private path to " << lokinetIdentityPath;
<< "Setting Lokid key path to " << lokidKeyPath;
}
if (vm.count("db-location")) {
@ -118,9 +118,9 @@ int main(int argc, char* argv[]) {
boost::asio::io_context ioc{1};
loki::ServiceNode service_node(ioc, port, lokinetIdentityPath,
ChannelEncryption<std::string> channelEncryption(lokidKeyPath);
loki::ServiceNode service_node(ioc, port, channelEncryption.public_key,
dbLocation);
ChannelEncryption<std::string> channelEncryption(lokinetIdentityPath);
/// Should run http server
loki::http_server::run(ioc, ip, port, service_node, channelEncryption);

View file

@ -1,7 +1,7 @@
#include "service_node.h"
#include "Database.hpp"
#include "lokinet_identity.hpp"
#include "lokid_key.h"
#include "utils.hpp"
#include "Item.hpp"
@ -64,24 +64,22 @@ std::string hash_data(std::string data) {
}
ServiceNode::ServiceNode(boost::asio::io_context& ioc, uint16_t port,
const std::string& identityPath,
const std::vector<uint8_t>& public_key,
const std::string& dbLocation)
: ioc_(ioc), db_(std::make_unique<Database>(dbLocation)), our_port_(port),
: ioc_(ioc), db_(std::make_unique<Database>(dbLocation)),
update_timer_(ioc, std::chrono::milliseconds(100)) {
#ifndef INTEGRATION_TEST
const std::vector<uint8_t> publicKey =
parseLokinetIdentityPublic(identityPath);
char buf[64] = {0};
std::string our_address;
if (char const* dest = util::base32z_encode(publicKey, buf)) {
if (char const* dest = util::base32z_encode(public_key, buf)) {
our_address.append(dest);
our_address.append(".snode");
}
BOOST_LOG_TRIVIAL(info) << "Read snode address " << our_address;
our_address_.address = our_address;
#else
our_address_.port = port;
#endif
our_address_.port = port;
swarm_timer_tick();
}

View file

@ -53,8 +53,6 @@ class ServiceNode {
std::unique_ptr<Swarm> swarm_;
std::unique_ptr<Database> db_;
uint16_t our_port_;
sn_record_t our_address_;
boost::asio::steady_timer update_timer_;
@ -84,7 +82,7 @@ class ServiceNode {
public:
ServiceNode(boost::asio::io_context& ioc, uint16_t port,
const std::string& identityPath, const std::string& dbLocation);
const std::vector<uint8_t>& public_key, const std::string& dbLocation);
~ServiceNode();