Skip to content
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

Expose the LMDB Encrypt/Decrypt feature #134

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "lmdb-master-sys/lmdb"]
path = lmdb-master-sys/lmdb
[submodule "lmdb-master3-sys/lmdb"]
path = lmdb-master3-sys/lmdb
url = https://github.com/LMDB/lmdb
branch = mdb.master
branch = mdb.master3
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["lmdb-master-sys", "heed", "heed-traits", "heed-types"]
members = ["lmdb-master3-sys", "heed", "heed-traits", "heed-types"]
resolver = "2"
12 changes: 8 additions & 4 deletions heed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ readme = "../README.md"
edition = "2021"

[dependencies]
aead = { version = "0.5.1", default-features = false }
bitflags = { version = "2.3.3", features = ["serde"] }
bytemuck = "1.12.3"
byteorder = { version = "1.4.3", default-features = false }
generic-array = { version = "0.14.6", features = ["serde"] }
heed-traits = { version = "0.20.0-alpha.8", path = "../heed-traits" }
heed-types = { version = "0.20.0-alpha.8", default-features = false, path = "../heed-types" }
libc = "0.2.139"
lmdb-master-sys = { version = "0.1.0", path = "../lmdb-master-sys" }
lmdb-master3-sys = { version = "0.1.0", path = "../lmdb-master3-sys" }
once_cell = "1.16.0"
page_size = "0.6.0"
serde = { version = "1.0.151", features = ["derive"], optional = true }
synchronoise = "1.0.1"

[dev-dependencies]
serde = { version = "1.0.151", features = ["derive"] }
bytemuck = { version = "1.12.3", features = ["derive"] }
argon2 = { version = "0.4.1", features = ["std"] }
bytemuck = { version = "1.12.1", features = ["derive"] }
chacha20poly1305 = "0.10.1"
serde = { version = "1.0.144", features = ["derive"] }
tempfile = "3.3.0"

[target.'cfg(windows)'.dependencies]
Expand Down Expand Up @@ -69,7 +73,7 @@ unbounded_depth = ["heed-types/unbounded_depth"]
# There are tradeoffs for both POSIX and SysV semaphores; which you
# should look into before enabling this feature. Also, see here:
# <https://github.com/LMDB/lmdb/blob/3947014aed7ffe39a79991fa7fb5b234da47ad1a/libraries/liblmdb/lmdb.h#L46-L69>
posix-sem = ["lmdb-master-sys/posix-sem"]
posix-sem = ["lmdb-master3-sys/posix-sem"]

[[example]]
name = "rmp-serde"
Expand Down
57 changes: 57 additions & 0 deletions heed/examples/encrypt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::error::Error;
use std::fs;
use std::path::Path;

use argon2::Argon2;
use chacha20poly1305::{ChaCha20Poly1305, Key};
use heed::types::*;
use heed::{Database, EnvOpenOptions};

fn main() -> Result<(), Box<dyn Error>> {
let env_path = Path::new("target").join("encrypt.mdb");
let password = "This is the password that will be hashed by the argon2 algorithm";
let salt = "The salt added to the password hashes to add more security when stored";

let _ = fs::remove_dir_all(&env_path);
fs::create_dir_all(&env_path)?;

// We choose to use argon2 as our Key Derivation Function, but you can choose whatever you want.
// <https://github.com/RustCrypto/traits/tree/master/password-hash#supported-crates>
let mut key = Key::default();
Argon2::default().hash_password_into(password.as_bytes(), salt.as_bytes(), &mut key)?;

// We open the environment
let mut options = EnvOpenOptions::new().encrypt_with::<ChaCha20Poly1305>(key);
let env = options
.map_size(10 * 1024 * 1024) // 10MB
.max_dbs(3)
.open(&env_path)?;

let key1 = "first-key";
let val1 = "this is a secret info";
let key2 = "second-key";
let val2 = "this is another secret info";

// We create database and write secret values in it
let mut wtxn = env.write_txn()?;
let db: Database<Str, Str> = env.create_database(&mut wtxn, Some("first"))?;
db.put(&mut wtxn, key1, val1)?;
db.put(&mut wtxn, key2, val2)?;
wtxn.commit()?;
env.prepare_for_closing().wait();

// We reopen the environment now
let env = options.open(&env_path)?;

// We check that the secret entries are correctly decrypted
let rtxn = env.read_txn()?;
let db: Database<Str, Str> = env.open_database(&rtxn, Some("first"))?.unwrap();
let mut iter = db.iter(&rtxn)?;
assert_eq!(iter.next().transpose()?, Some((key1, val1)));
assert_eq!(iter.next().transpose()?, Some((key2, val2)));
assert_eq!(iter.next().transpose()?, None);

eprintln!("Successful test!");

Ok(())
}
Loading
Loading