implement mdb_env_init_crypto with memory management

lofire0_1_0
Niko 2 years ago
parent a9e0d9dfca
commit 6156b10356
  1. 2
      .gitignore
  2. 10
      libraries/liblmdb/Makefile
  3. 2
      libraries/liblmdb/lmdb.h
  4. 31
      libraries/liblmdb/mdb.c

2
.gitignore vendored

@ -0,0 +1,2 @@
.DS_Store

@ -67,12 +67,12 @@ test: all
rm -rf testdb && mkdir testdb
./mtest && ./mdb_stat testdb
liblmdb.a: mdb.o midl.o
$(AR) rs $@ mdb.o midl.o
liblmdb.a: mdb.o midl.o chacha8.o
$(AR) rs $@ mdb.o midl.o chacha8.o
liblmdb$(SOEXT): mdb.lo midl.lo
liblmdb$(SOEXT): mdb.lo midl.lo chacha8.o
# $(CC) $(LDFLAGS) -pthread -shared -Wl,-Bsymbolic -o $@ mdb.o midl.o $(SOLIBS)
$(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.lo midl.lo $(SOLIBS)
$(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.lo midl.lo chacha8.o $(SOLIBS)
mdb_stat: mdb_stat.o module.o liblmdb.a
$(CC) $(LDFLAGS) -o $@ $^ $(LDL)
@ -91,7 +91,7 @@ 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_enc: mtest_enc.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)

@ -1409,6 +1409,8 @@ int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
* <li>EINVAL - an invalid parameter was specified.
* </ul>
*/
int mdb_env_init_crypto(MDB_env *env, void *key);
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
/** @brief Store items into a database.

@ -6298,7 +6298,7 @@ mdb_env_close(MDB_env *env)
mdb_env_close_active(env, 0);
#if MDB_RPAGE_CACHE
free(env->me_enckey.mv_data);
//free(env->me_enckey.mv_data);
#endif
free(env);
}
@ -10726,6 +10726,25 @@ done:
return rc;
}
#include "chacha8.h"
static int encfunc(const MDB_val *src, MDB_val *dst, const MDB_val *key, int encdec)
{
chacha8(src->mv_data, src->mv_size, key[0].mv_data, key[1].mv_data, dst->mv_data);
return 0;
}
int
mdb_env_init_crypto(MDB_env *env, void *key) {
MDB_val enckey;
enckey.mv_data = key;
enckey.mv_size = 32;
int ret = mdb_env_set_encrypt(env, encfunc, &enckey, 0);
//fprintf(stderr,"return of mdb_env_init_crypto %d\n",ret);
return ret;
}
int
mdb_put(MDB_txn *txn, MDB_dbi dbi,
MDB_val *key, MDB_val *data, unsigned int flags)
@ -11361,18 +11380,18 @@ mdb_env_set_assert(MDB_env *env, MDB_assert_func *func)
int ESECT
mdb_env_set_encrypt(MDB_env *env, MDB_enc_func *func, const MDB_val *key, unsigned int size)
{
char *kdata;
//char *kdata;
if (!env || !func || !key)
return EINVAL;
if (env->me_flags & MDB_ENV_ACTIVE)
return EINVAL;
if (! (kdata = malloc(key[0].mv_size)))
return ENOMEM;
//if (! (kdata = malloc(key[0].mv_size)))
// return ENOMEM;
memcpy(kdata, key->mv_data, key->mv_size);
//memcpy(kdata, key->mv_data, key->mv_size);
free(env->me_enckey.mv_data);
env->me_enckey.mv_data = kdata;
env->me_enckey.mv_data = key->mv_data;//kdata;
env->me_enckey.mv_size = key->mv_size;
env->me_encfunc = func;
if (size)

Loading…
Cancel
Save