1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

encryption benchmark & test

This commit is contained in:
mat 2022-08-28 15:08:43 -05:00
parent b8228a0360
commit b44f1c84de
4 changed files with 78 additions and 0 deletions

1
Cargo.lock generated
View file

@ -163,6 +163,7 @@ dependencies = [
"aes",
"azalea-buf",
"cfb8",
"criterion",
"num-bigint",
"rand",
"rsa_public_encrypt_pkcs1",

View file

@ -14,3 +14,10 @@ rand = {version = "^0.8.4", features = ["getrandom"]}
rsa_public_encrypt_pkcs1 = "0.4.0"
sha-1 = "^0.10.0"
uuid = "^1.1.2"
[dev-dependencies]
criterion = {version = "^0.3.5", features = ["html_reports"]}
[[bench]]
harness = false
name = "my_benchmark"

View file

@ -0,0 +1,18 @@
use azalea_crypto::{create_cipher, encrypt_packet};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
fn bench(c: &mut Criterion) {
let (enc, dec) = create_cipher(b"0123456789abcdef");
let mut packet = [0u8; 65536];
for i in 0..packet.len() {
packet[i] = i as u8;
}
c.bench_function("Encrypt", |b| {
b.iter(|| encrypt_packet(&mut enc.clone(), &mut packet.clone()))
});
}
criterion_group!(benches, bench);
criterion_main!(benches);

View file

@ -100,4 +100,56 @@ mod tests {
let digest = hex_digest(&digest_data(b"simon", &[], &[]));
assert_eq!(digest, "88e16a1019277b15d58faf0541e11910eb756f6");
}
#[test]
fn encode_packet_twice() {
let mut packet = vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
let (mut enc_cipher, _dec_cipher) = create_cipher(b"1234567890123456");
encrypt_packet(&mut enc_cipher, &mut packet);
assert_eq!(packet, vec![117, 151, 183, 45, 229, 232, 43, 181, 121, 16]);
let mut packet = vec![0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13];
encrypt_packet(&mut enc_cipher, &mut packet);
assert_eq!(
packet,
vec![185, 223, 129, 153, 173, 140, 133, 239, 59, 168]
);
}
#[test]
fn encode_packet_long() {
let mut packet = (0..=255).collect::<Vec<u8>>();
let (mut enc_cipher, _dec_cipher) = create_cipher(b"1234567890123456");
encrypt_packet(&mut enc_cipher, &mut packet);
assert_eq!(
packet,
vec![
117, 151, 183, 45, 229, 232, 43, 181, 121, 16, 185, 223, 129, 153, 173, 140, 133,
239, 59, 168, 148, 39, 97, 19, 22, 219, 78, 70, 116, 143, 21, 223, 155, 99, 201,
62, 133, 77, 244, 152, 254, 36, 135, 147, 45, 25, 66, 236, 2, 12, 101, 39, 140, 62,
7, 57, 19, 101, 217, 91, 142, 243, 0, 3, 100, 142, 160, 21, 219, 145, 151, 37, 11,
30, 190, 176, 26, 90, 143, 63, 255, 188, 254, 40, 41, 92, 163, 197, 8, 8, 111, 175,
49, 234, 82, 34, 5, 96, 162, 7, 217, 42, 77, 38, 127, 213, 207, 251, 34, 173, 34,
132, 23, 12, 118, 59, 51, 216, 173, 199, 137, 95, 132, 222, 243, 195, 81, 60, 205,
52, 65, 209, 125, 137, 5, 52, 219, 165, 248, 35, 173, 57, 200, 182, 162, 148, 70,
62, 102, 21, 220, 158, 71, 98, 47, 231, 196, 58, 8, 70, 160, 177, 159, 50, 20, 187,
31, 249, 68, 26, 142, 171, 239, 193, 10, 174, 14, 80, 238, 114, 124, 185, 253, 246,
47, 67, 37, 69, 70, 9, 69, 135, 13, 195, 253, 8, 241, 175, 170, 75, 231, 7, 92, 18,
38, 132, 65, 146, 202, 130, 238, 224, 30, 113, 168, 241, 159, 131, 238, 67, 1, 244,
74, 172, 86, 95, 192, 236, 198, 188, 81, 67, 49, 230, 166, 52, 224, 238, 11, 252,
0, 179, 56, 209, 231, 62, 146, 106, 18, 217, 138, 89, 110, 240, 255, 192
]
);
}
#[test]
fn encode_decode_packet() {
let mut packet = vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
let (mut enc_cipher, mut dec_cipher) = create_cipher(b"1234567890123456");
encrypt_packet(&mut enc_cipher, &mut packet);
decrypt_packet(&mut dec_cipher, &mut packet);
assert_eq!(
packet,
vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09]
);
}
}