-
Notifications
You must be signed in to change notification settings - Fork 50
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
[PM-5693] CryptoService using memfd_secret on Linux #979
Closed
Closed
Changes from 24 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
d7c7c3e
Initial CryptoService impl
dani-garcia 6068e84
More work
dani-garcia 50dc1b4
Refactor keystore to also handle resizes
dani-garcia 216eb25
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 4b846ed
Fix
dani-garcia 7a01168
Paralelization plus alternative to locatekey
dani-garcia c649bf0
WASM support
dani-garcia b901ef7
Merge branch 'main' into ps/secure-crypto-service
dani-garcia e2129ad
Remove unnecessary trait
dani-garcia f708fcc
Inline cryptoengine
dani-garcia 30854f7
Impl KeyStore in Slice struct directly
dani-garcia bed894a
Reset the values to None after munlock has zeroized them
dani-garcia 709dfce
Merge branch 'main' into ps/secure-crypto-service
dani-garcia f7eda88
Fmt
dani-garcia ce2343e
Add benchmark
dani-garcia bcd712f
Respect no-memory-hardening flag
dani-garcia 38343d2
Fix cfg flags, silence warnings
dani-garcia f34ce02
Export memfd correctly
dani-garcia cc27320
Fix memtest
dani-garcia 281619d
Merge branch 'main' into ps/secure-crypto-service
dani-garcia 32d298f
Try fat LTO to fix windows
dani-garcia c43aa08
Disable memsec on windows to fix it
dani-garcia dd37d1d
Incorrect optional dep
dani-garcia 45f3d32
Try updating windows in memsec
dani-garcia 1f70169
Remove unnecessary bound
dani-garcia 22a8b17
Move store impl around a bit
dani-garcia c63f656
Make KeyStore pub
dani-garcia db3f8d4
Merge branch 'main' into ps/secure-crypto-service-impl
dani-garcia eb81684
Some renames/simplifications
dani-garcia d279626
Add some missing implementations
dani-garcia d5f1ede
Rename
dani-garcia 8652d79
Introduce mutable context
dani-garcia fdb0263
Refactor keyref/encryptable locations
dani-garcia 6ea6267
Some additions needed to migrate the code
dani-garcia 32088c7
Merge branch 'main' into ps/secure-crypto-service
dani-garcia f882fb2
Remove bench
dani-garcia bec4786
Remove using
dani-garcia c2ffea6
Fix TODO
dani-garcia c0d2b63
Comment
dani-garcia cacf4db
Merge branch 'main' into ps/secure-crypto-service
dani-garcia f4ca816
Fmt
dani-garcia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; | ||
|
||
pub fn criterion_benchmark(c: &mut Criterion) { | ||
let user_key = SymmetricCryptoKey::generate(rand::thread_rng()); | ||
|
||
let org_id = uuid::Uuid::parse_str("91b000b6-81ce-47f4-9802-3390e0b895ed").expect(""); | ||
let org_key = SymmetricCryptoKey::generate(rand::thread_rng()); | ||
|
||
let cipher_key = SymmetricCryptoKey::generate(rand::thread_rng()); | ||
let cipher_key_user_enc = cipher_key.to_vec().encrypt_with_key(&user_key).expect(""); | ||
let cipher_view = CipherView { | ||
key: Some(cipher_key_user_enc.clone()), | ||
name: "test".to_string(), | ||
}; | ||
|
||
let service: CryptoService<MySymmKeyRef, MyAsymmKeyRef> = CryptoService::new(); | ||
#[allow(deprecated)] | ||
{ | ||
service.insert_symmetric_key(MySymmKeyRef::User, user_key.clone()); | ||
service.insert_symmetric_key(MySymmKeyRef::Organization(org_id), org_key.clone()); | ||
} | ||
|
||
let cipher_views = vec![cipher_view.clone(); 10_000]; | ||
|
||
{ | ||
let mut group = c.benchmark_group("New encryptable"); | ||
|
||
for size in [1, 10, 100, 1_000, 10_000].iter() { | ||
group.throughput(Throughput::Elements(*size as u64)); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("encrypt X ciphers individually", size), | ||
size, | ||
|b, &size| { | ||
b.iter(|| { | ||
for c in cipher_views.iter().take(size).cloned() { | ||
service.encrypt(black_box(c)).expect(""); | ||
} | ||
}); | ||
}, | ||
); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("encrypt X ciphers batch", size), | ||
size, | ||
|b, &size| { | ||
b.iter(|| service.encrypt_list(black_box(&cipher_views[..size]))); | ||
}, | ||
); | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
{ | ||
let mut group = c.benchmark_group("Old encryptable"); | ||
|
||
for size in [1, 10, 100, 1_000, 10_000].iter() { | ||
group.throughput(Throughput::Elements(*size as u64)); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("encrypt X ciphers individually", size), | ||
size, | ||
|b, &size| { | ||
b.iter(|| { | ||
for c in cipher_views.iter().take(size).cloned() { | ||
black_box(c).encrypt_with_key(&user_key).expect(""); | ||
} | ||
}); | ||
}, | ||
); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("encrypt X ciphers batch", size), | ||
size, | ||
|b, &size| { | ||
b.iter(|| { | ||
black_box(cipher_views[0..size].to_vec()) | ||
.encrypt_with_key(&user_key) | ||
.expect(""); | ||
}); | ||
}, | ||
); | ||
} | ||
|
||
group.finish(); | ||
} | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); | ||
|
||
use bitwarden_crypto::{ | ||
key_refs, service::*, CryptoError, EncString, KeyDecryptable, KeyEncryptable, | ||
SymmetricCryptoKey, | ||
}; | ||
|
||
key_refs! { | ||
#[symmetric] | ||
pub enum MySymmKeyRef { | ||
User, | ||
Organization(uuid::Uuid), | ||
#[local] | ||
Local(&'static str), | ||
} | ||
|
||
#[asymmetric] | ||
pub enum MyAsymmKeyRef { | ||
UserPrivateKey, | ||
#[local] | ||
Local(&'static str), | ||
} | ||
} | ||
|
||
#[allow(unused)] | ||
struct Cipher { | ||
key: Option<EncString>, | ||
name: EncString, | ||
} | ||
|
||
#[derive(Clone)] | ||
struct CipherView { | ||
key: Option<EncString>, | ||
name: String, | ||
} | ||
|
||
impl UsesKey<MySymmKeyRef> for Cipher { | ||
fn uses_key(&self) -> MySymmKeyRef { | ||
MySymmKeyRef::User | ||
} | ||
} | ||
impl UsesKey<MySymmKeyRef> for CipherView { | ||
fn uses_key(&self) -> MySymmKeyRef { | ||
MySymmKeyRef::User | ||
} | ||
} | ||
|
||
const CIPHER_KEY: MySymmKeyRef = MySymmKeyRef::Local("cipher_key"); | ||
|
||
// New encryptable implementations | ||
|
||
impl Encryptable<MySymmKeyRef, MyAsymmKeyRef, MySymmKeyRef, Cipher> for CipherView { | ||
fn encrypt( | ||
&self, | ||
ctx: &mut CryptoServiceContext<MySymmKeyRef, MyAsymmKeyRef>, | ||
key: MySymmKeyRef, | ||
) -> Result<Cipher, bitwarden_crypto::CryptoError> { | ||
let cipher_key = match &self.key { | ||
Some(cipher_key) => ctx.decrypt_and_store_symmetric_key(key, CIPHER_KEY, cipher_key)?, | ||
None => key, | ||
}; | ||
|
||
Ok(Cipher { | ||
key: self.key.clone(), | ||
name: self.name.as_str().encrypt(ctx, cipher_key)?, | ||
}) | ||
} | ||
} | ||
|
||
// Old encryptable implementations | ||
|
||
impl KeyEncryptable<SymmetricCryptoKey, Cipher> for CipherView { | ||
fn encrypt_with_key(self, key: &SymmetricCryptoKey) -> Result<Cipher, CryptoError> { | ||
let cipher_key = self | ||
.key | ||
.as_ref() | ||
.map(|k| { | ||
let mut kk: Vec<u8> = k.decrypt_with_key(key)?; | ||
SymmetricCryptoKey::try_from(kk.as_mut_slice()) | ||
}) | ||
.transpose()?; | ||
|
||
let key = cipher_key.as_ref().unwrap_or(key); | ||
|
||
Ok(Cipher { | ||
key: self.key.clone(), | ||
name: self.name.encrypt_with_key(key)?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ pub enum CryptoError { | |
MissingKey(Uuid), | ||
#[error("The item was missing a required field: {0}")] | ||
MissingField(&'static str), | ||
#[error("Missing Key for Ref. {0}")] | ||
MissingKey2(String), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't change the |
||
|
||
#[error("EncString error, {0}")] | ||
EncString(#[from] EncStringParseError), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memsec
is usingwindows-sys
0.45, which was causing build errors similar to the ones fixed by #1053.Using the latest
windows-sys
0.59 seems to solve the problem for me. TODO: Open a PR upstream?