Skip to content

CHG: Add kryszyp lmdb-js improvements #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dependencies/lmdb/libraries/liblmdb/COPYRIGHT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2011-2020 Howard Chu, Symas Corp.
Copyright 2011-2021 Howard Chu, Symas Corp.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
30 changes: 24 additions & 6 deletions dependencies/lmdb/libraries/liblmdb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ W = -W -Wall -Wno-unused-parameter -Wbad-function-cast -Wuninitialized
THREADS = -pthread
OPT = -O2 -g
CFLAGS = $(THREADS) $(OPT) $(W) $(XCFLAGS)
LDFLAGS = $(THREADS)
LDLIBS =
SOLIBS =
SOEXT = .so
LDL = -ldl
prefix = /usr/local
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
Expand All @@ -42,7 +44,11 @@ ILIBS = liblmdb.a liblmdb$(SOEXT)
IPROGS = mdb_stat mdb_copy mdb_dump mdb_load mdb_drop
IDOCS = mdb_stat.1 mdb_copy.1 mdb_dump.1 mdb_load.1 mdb_drop.1
PROGS = $(IPROGS) mtest mtest2 mtest3 mtest4 mtest5
RPROGS = mtest_remap mtest_enc mtest_enc2

all: $(ILIBS) $(PROGS)
# Requires CPPFLAGS=-DMDB_VL32 and/or -DMDB_RPAGE_CACHE
rall: all $(RPROGS)

install: $(ILIBS) $(IPROGS) $(IHDRS)
mkdir -p $(DESTDIR)$(bindir)
Expand All @@ -55,7 +61,7 @@ install: $(ILIBS) $(IPROGS) $(IHDRS)
for f in $(IDOCS); do cp $$f $(DESTDIR)$(mandir)/man1; done

clean:
rm -rf $(PROGS) *.[ao] *.[ls]o *~ testdb
rm -rf $(PROGS) $(RPROGS) *.[ao] *.[ls]o *~ testdb

test: all
rm -rf testdb && mkdir testdb
Expand All @@ -68,17 +74,29 @@ liblmdb$(SOEXT): mdb.lo midl.lo
# $(CC) $(LDFLAGS) -pthread -shared -Wl,-Bsymbolic -o $@ mdb.o midl.o $(SOLIBS)
$(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.lo midl.lo $(SOLIBS)

mdb_stat: mdb_stat.o liblmdb.a
mdb_copy: mdb_copy.o liblmdb.a
mdb_dump: mdb_dump.o liblmdb.a
mdb_load: mdb_load.o liblmdb.a
mdb_drop: mdb_drop.o liblmdb.a
mdb_stat: mdb_stat.o module.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
mdb_copy: mdb_copy.o module.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
mdb_dump: mdb_dump.o module.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
mdb_load: mdb_load.o module.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
mdb_drop: mdb_drop.o module.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
mtest: mtest.o liblmdb.a
mtest2: mtest2.o liblmdb.a
mtest3: mtest3.o liblmdb.a
mtest4: mtest4.o liblmdb.a
mtest5: mtest5.o liblmdb.a
mtest6: mtest6.o liblmdb.a
mtest_remap: mtest_remap.o liblmdb.a
mtest_enc: mtest_enc.o chacha8.o liblmdb.a
mtest_enc2: mtest_enc2.o module.o liblmdb.a crypto.lm
$(CC) $(LDFLAGS) -pthread -o $@ mtest_enc2.o module.o liblmdb.a $(LDL)

crypto.lm: crypto.c
$(CC) -shared -o $@ -lcrypto

mdb.o: mdb.c lmdb.h midl.h
$(CC) $(CFLAGS) $(CPPFLAGS) -c mdb.c
Expand Down
183 changes: 183 additions & 0 deletions dependencies/lmdb/libraries/liblmdb/chacha8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
chacha-merged.c version 20080118
D. J. Bernstein
Public domain.
*/

#include <memory.h>
#include <stdio.h>
//#include <sys/param.h>

#include "chacha8.h"
#if 0
#include "common/int-util.h"
#include "warnings.h"
#endif

#if BYTE_ORDER == LITTLE_ENDIAN
#define SWAP32LE(x) (x)
#else
#define SWAP32LE(x) ((((uint32_t) (x) & 0x000000ff) << 24) | \
(((uint32_t) (x) & 0x0000ff00) << 8) | \
(((uint32_t) (x) & 0x00ff0000) >> 8) | \
(((uint32_t) (x) & 0xff000000) >> 24))
#endif

/*
* The following macros are used to obtain exact-width results.
*/
#define U8V(v) ((uint8_t)(v) & UINT8_C(0xFF))
#define U32V(v) ((uint32_t)(v) & UINT32_C(0xFFFFFFFF))

/*
* The following macros load words from an array of bytes with
* different types of endianness, and vice versa.
*/
#define U8TO32_LITTLE(p) SWAP32LE(((uint32_t*)(p))[0])
#define U32TO8_LITTLE(p, v) (((uint32_t*)(p))[0] = SWAP32LE(v))

#define ROTATE(v,c) (rol32(v,c))
#define XOR(v,w) ((v) ^ (w))
#define PLUS(v,w) (U32V((v) + (w)))
#define PLUSONE(v) (PLUS((v),1))

#define QUARTERROUND(a,b,c,d) \
a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);

static const char sigma[] = "expand 32-byte k";

static uint32_t rol32(uint32_t x, int r) {
return (x << (r & 31)) | (x >> (-r & 31));
}

void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher) {
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
char* ctarget = 0;
char tmp[64];
int i;

if (!length) return;

j0 = U8TO32_LITTLE(sigma + 0);
j1 = U8TO32_LITTLE(sigma + 4);
j2 = U8TO32_LITTLE(sigma + 8);
j3 = U8TO32_LITTLE(sigma + 12);
j4 = U8TO32_LITTLE(key + 0);
j5 = U8TO32_LITTLE(key + 4);
j6 = U8TO32_LITTLE(key + 8);
j7 = U8TO32_LITTLE(key + 12);
j8 = U8TO32_LITTLE(key + 16);
j9 = U8TO32_LITTLE(key + 20);
j10 = U8TO32_LITTLE(key + 24);
j11 = U8TO32_LITTLE(key + 28);
j12 = 0;
j13 = 0;
j14 = U8TO32_LITTLE(iv + 0);
j15 = U8TO32_LITTLE(iv + 4);

for (;;) {
if (length < 64) {
memcpy(tmp, data, length);
data = tmp;
ctarget = cipher;
cipher = tmp;
}
x0 = j0;
x1 = j1;
x2 = j2;
x3 = j3;
x4 = j4;
x5 = j5;
x6 = j6;
x7 = j7;
x8 = j8;
x9 = j9;
x10 = j10;
x11 = j11;
x12 = j12;
x13 = j13;
x14 = j14;
x15 = j15;
for (i = 8;i > 0;i -= 2) {
QUARTERROUND( x0, x4, x8,x12)
QUARTERROUND( x1, x5, x9,x13)
QUARTERROUND( x2, x6,x10,x14)
QUARTERROUND( x3, x7,x11,x15)
QUARTERROUND( x0, x5,x10,x15)
QUARTERROUND( x1, x6,x11,x12)
QUARTERROUND( x2, x7, x8,x13)
QUARTERROUND( x3, x4, x9,x14)
}
x0 = PLUS( x0, j0);
x1 = PLUS( x1, j1);
x2 = PLUS( x2, j2);
x3 = PLUS( x3, j3);
x4 = PLUS( x4, j4);
x5 = PLUS( x5, j5);
x6 = PLUS( x6, j6);
x7 = PLUS( x7, j7);
x8 = PLUS( x8, j8);
x9 = PLUS( x9, j9);
x10 = PLUS(x10,j10);
x11 = PLUS(x11,j11);
x12 = PLUS(x12,j12);
x13 = PLUS(x13,j13);
x14 = PLUS(x14,j14);
x15 = PLUS(x15,j15);

x0 = XOR( x0,U8TO32_LITTLE((uint8_t*)data + 0));
x1 = XOR( x1,U8TO32_LITTLE((uint8_t*)data + 4));
x2 = XOR( x2,U8TO32_LITTLE((uint8_t*)data + 8));
x3 = XOR( x3,U8TO32_LITTLE((uint8_t*)data + 12));
x4 = XOR( x4,U8TO32_LITTLE((uint8_t*)data + 16));
x5 = XOR( x5,U8TO32_LITTLE((uint8_t*)data + 20));
x6 = XOR( x6,U8TO32_LITTLE((uint8_t*)data + 24));
x7 = XOR( x7,U8TO32_LITTLE((uint8_t*)data + 28));
x8 = XOR( x8,U8TO32_LITTLE((uint8_t*)data + 32));
x9 = XOR( x9,U8TO32_LITTLE((uint8_t*)data + 36));
x10 = XOR(x10,U8TO32_LITTLE((uint8_t*)data + 40));
x11 = XOR(x11,U8TO32_LITTLE((uint8_t*)data + 44));
x12 = XOR(x12,U8TO32_LITTLE((uint8_t*)data + 48));
x13 = XOR(x13,U8TO32_LITTLE((uint8_t*)data + 52));
x14 = XOR(x14,U8TO32_LITTLE((uint8_t*)data + 56));
x15 = XOR(x15,U8TO32_LITTLE((uint8_t*)data + 60));

j12 = PLUSONE(j12);
if (!j12)
{
j13 = PLUSONE(j13);
/* stopping at 2^70 bytes per iv is user's responsibility */
}

U32TO8_LITTLE(cipher + 0,x0);
U32TO8_LITTLE(cipher + 4,x1);
U32TO8_LITTLE(cipher + 8,x2);
U32TO8_LITTLE(cipher + 12,x3);
U32TO8_LITTLE(cipher + 16,x4);
U32TO8_LITTLE(cipher + 20,x5);
U32TO8_LITTLE(cipher + 24,x6);
U32TO8_LITTLE(cipher + 28,x7);
U32TO8_LITTLE(cipher + 32,x8);
U32TO8_LITTLE(cipher + 36,x9);
U32TO8_LITTLE(cipher + 40,x10);
U32TO8_LITTLE(cipher + 44,x11);
U32TO8_LITTLE(cipher + 48,x12);
U32TO8_LITTLE(cipher + 52,x13);
U32TO8_LITTLE(cipher + 56,x14);
U32TO8_LITTLE(cipher + 60,x15);

if (length <= 64) {
if (length < 64) {
memcpy(ctarget, cipher, length);
}
return;
}
length -= 64;
cipher += 64;
data = (uint8_t*)data + 64;
}
}
14 changes: 14 additions & 0 deletions dependencies/lmdb/libraries/liblmdb/chacha8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
void chacha8(const void* data, size_t length, const uint8_t* key, const uint8_t* iv, char* cipher);

#define CHACHA8_KEY_SIZE 32
#define CHACHA8_IV_SIZE 8

#ifdef __cplusplus
}
#endif

121 changes: 121 additions & 0 deletions dependencies/lmdb/libraries/liblmdb/crypto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* crypto.c - LMDB encryption helper module */
/*
* Copyright 2020-2021 Howard Chu, Symas Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the Symas
* Dual-Use License.
*
* A copy of this license is available in the file LICENSE in the
* source distribution.
*/
#include <string.h>

#include <openssl/engine.h>

#include "lmdb.h"

MDB_crypto_hooks MDB_crypto;

static EVP_CIPHER *cipher;

static int mcf_str2key(const char *passwd, MDB_val *key)
{
unsigned int size;
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
EVP_DigestUpdate(mdctx, "Just a Constant", sizeof("Just a Constant"));
EVP_DigestUpdate(mdctx, passwd, strlen(passwd));
EVP_DigestFinal_ex(mdctx, key->mv_data, &size);
EVP_MD_CTX_free(mdctx);
return 0;
}

/* cheats - internal OpenSSL 1.1 structures */
typedef struct evp_cipher_ctx_st {
const EVP_CIPHER *cipher;
ENGINE *engine; /* functional reference if 'cipher' is
* ENGINE-provided */
int encrypt; /* encrypt or decrypt */
int buf_len; /* number we have left */
unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
int num; /* used by cfb/ofb/ctr mode */
/* FIXME: Should this even exist? It appears unused */
void *app_data; /* application stuff */
int key_len; /* May change for variable length cipher */
unsigned long flags; /* Various flags */
void *cipher_data; /* per EVP data */
int final_used;
int block_mask;
unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
} EVP_CIPHER_CTX;

#define CHACHA_KEY_SIZE 32
#define CHACHA_CTR_SIZE 16
#define CHACHA_BLK_SIZE 64
#define POLY1305_BLOCK_SIZE 16

typedef struct {
union {
double align; /* this ensures even sizeof(EVP_CHACHA_KEY)%8==0 */
unsigned int d[CHACHA_KEY_SIZE / 4];
} key;
unsigned int counter[CHACHA_CTR_SIZE / 4];
unsigned char buf[CHACHA_BLK_SIZE];
unsigned int partial_len;
} EVP_CHACHA_KEY;

typedef struct {
EVP_CHACHA_KEY key;
unsigned int nonce[12/4];
unsigned char tag[POLY1305_BLOCK_SIZE];
unsigned char tls_aad[POLY1305_BLOCK_SIZE];
struct { uint64_t aad, text; } len;
int aad, mac_inited, tag_len, nonce_len;
size_t tls_payload_length;
} EVP_CHACHA_AEAD_CTX;

static int mcf_encfunc(const MDB_val *src, MDB_val *dst, const MDB_val *key, int encdec)
{
unsigned char iv[12];
int ivl, outl, rc;
mdb_size_t *ptr;
EVP_CIPHER_CTX ctx = {0};
EVP_CHACHA_AEAD_CTX cactx;

ctx.cipher_data = &cactx;
ptr = key[1].mv_data;
ivl = ptr[0] & 0xffffffff;
memcpy(iv, &ivl, 4);
memcpy(iv+4, ptr+1, sizeof(mdb_size_t));
EVP_CipherInit_ex(&ctx, cipher, NULL, key[0].mv_data, iv, encdec);
EVP_CIPHER_CTX_set_padding(&ctx, 0);
if (!encdec) {
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_TAG, key[2].mv_size, key[2].mv_data);
}
rc = EVP_CipherUpdate(&ctx, dst->mv_data, &outl, src->mv_data, src->mv_size);
if (rc)
rc = EVP_CipherFinal_ex(&ctx, key[2].mv_data, &outl);
if (rc && encdec) {
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_GET_TAG, key[2].mv_size, key[2].mv_data);
}
return rc == 0;
}

static const MDB_crypto_funcs mcf_table = {
mcf_str2key,
mcf_encfunc,
NULL,
CHACHA_KEY_SIZE,
POLY1305_BLOCK_SIZE,
0
};

MDB_crypto_funcs *MDB_crypto()
{
cipher = (EVP_CIPHER *)EVP_chacha20_poly1305();
return (MDB_crypto_funcs *)&mcf_table;
}
Loading