-
Notifications
You must be signed in to change notification settings - Fork 143
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
Benchmark passphrase API of Secure Cell #582
Merged
Merged
Changes from all commits
Commits
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
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
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,161 @@ | ||
// Copyright 2020 Cossack Labs Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; | ||
|
||
use libthemis_sys::{ | ||
themis_secure_cell_decrypt_seal_with_passphrase, | ||
themis_secure_cell_encrypt_seal_with_passphrase, THEMIS_BUFFER_TOO_SMALL, THEMIS_SUCCESS, | ||
}; | ||
|
||
const PASSPHRASE: &str = "correct horse battery staple"; | ||
const CONTEXT: &[u8] = b"Themis Core benchmark"; | ||
const MIN_AUTH_TOKEN: usize = 128; | ||
|
||
const KB: usize = 1024; | ||
const MB: usize = 1024 * KB; | ||
#[allow(clippy::identity_op)] | ||
const MESSAGE_SIZES: &[usize] = &[ | ||
// There is no significant difference between those due to KDF | ||
64, // cache line (and close to EcdsaPrivateKey) | ||
4 * KB, // memory page | ||
1 * MB, // L2 cache | ||
]; | ||
|
||
pub fn encryption(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Secure Cell encryption - Seal, passphrase"); | ||
for message_size in MESSAGE_SIZES { | ||
group.throughput(Throughput::Bytes(*message_size as u64)); | ||
group.bench_with_input( | ||
BenchmarkId::from_parameter(pretty(*message_size)), | ||
message_size, | ||
|b, &size| { | ||
let message = vec![0; size]; | ||
|
||
let mut encrypted = vec![0; size + MIN_AUTH_TOKEN]; | ||
b.iter(|| { | ||
let mut encrypted_size = 0; | ||
let res = unsafe { | ||
themis_secure_cell_encrypt_seal_with_passphrase( | ||
PASSPHRASE.as_ptr(), | ||
PASSPHRASE.len(), | ||
CONTEXT.as_ptr(), | ||
CONTEXT.len(), | ||
message.as_ptr(), | ||
message.len(), | ||
std::ptr::null_mut(), | ||
&mut encrypted_size, | ||
) | ||
}; | ||
assert_eq!(res, THEMIS_BUFFER_TOO_SMALL as i32); | ||
assert!(encrypted_size <= encrypted.len()); | ||
|
||
let res = unsafe { | ||
themis_secure_cell_encrypt_seal_with_passphrase( | ||
PASSPHRASE.as_ptr(), | ||
PASSPHRASE.len(), | ||
CONTEXT.as_ptr(), | ||
CONTEXT.len(), | ||
message.as_ptr(), | ||
message.len(), | ||
encrypted.as_mut_ptr(), | ||
&mut encrypted_size, | ||
) | ||
}; | ||
assert_eq!(res, THEMIS_SUCCESS as i32); | ||
}); | ||
}, | ||
); | ||
} | ||
group.finish(); | ||
} | ||
|
||
pub fn decryption(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Secure Cell decryption - Seal, passphrase"); | ||
for message_size in MESSAGE_SIZES { | ||
group.throughput(Throughput::Bytes(*message_size as u64)); | ||
group.bench_with_input( | ||
BenchmarkId::from_parameter(pretty(*message_size)), | ||
message_size, | ||
|b, &size| { | ||
let message = vec![0; size]; | ||
// TODO: replace with themis::SecureCell when passphrase API is available | ||
let mut encrypted = vec![0; size + MIN_AUTH_TOKEN]; | ||
let mut encrypted_size = encrypted.len(); | ||
let res = unsafe { | ||
themis_secure_cell_encrypt_seal_with_passphrase( | ||
PASSPHRASE.as_ptr(), | ||
PASSPHRASE.len(), | ||
CONTEXT.as_ptr(), | ||
CONTEXT.len(), | ||
message.as_ptr(), | ||
message.len(), | ||
encrypted.as_mut_ptr(), | ||
&mut encrypted_size, | ||
) | ||
}; | ||
assert_eq!(res, THEMIS_SUCCESS as i32); | ||
encrypted.truncate(encrypted_size); | ||
let encrypted = encrypted; | ||
|
||
let mut decrypted = vec![0; size]; | ||
b.iter(|| { | ||
let mut decrypted_size = 0; | ||
let res = unsafe { | ||
themis_secure_cell_decrypt_seal_with_passphrase( | ||
PASSPHRASE.as_ptr(), | ||
PASSPHRASE.len(), | ||
CONTEXT.as_ptr(), | ||
CONTEXT.len(), | ||
encrypted.as_ptr(), | ||
encrypted.len(), | ||
std::ptr::null_mut(), | ||
&mut decrypted_size, | ||
) | ||
}; | ||
assert_eq!(res, THEMIS_BUFFER_TOO_SMALL as i32); | ||
assert!(decrypted_size <= decrypted.len()); | ||
|
||
let res = unsafe { | ||
themis_secure_cell_decrypt_seal_with_passphrase( | ||
PASSPHRASE.as_ptr(), | ||
PASSPHRASE.len(), | ||
CONTEXT.as_ptr(), | ||
CONTEXT.len(), | ||
encrypted.as_ptr(), | ||
encrypted.len(), | ||
decrypted.as_mut_ptr(), | ||
&mut decrypted_size, | ||
) | ||
}; | ||
assert_eq!(res, THEMIS_SUCCESS as i32); | ||
}); | ||
}, | ||
); | ||
} | ||
group.finish(); | ||
} | ||
|
||
fn pretty(size: usize) -> String { | ||
if size >= MB { | ||
format!("{} MB", size / MB) | ||
} else if size >= KB { | ||
format!("{} KB", size / KB) | ||
} else { | ||
format!("{}", size) | ||
} | ||
} | ||
|
||
criterion_group!(benches, encryption, decryption); | ||
criterion_main!(benches); |
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.
😂