1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
use cipher::{KeyIvInit, StreamCipher};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use digest::Digest;
use rand::prelude::*;
use tor_bytes::SecretBuf;
use tor_cell::relaycell::{RelayCellFormatTrait, RelayCellFormatV0};
use tor_llcrypto::{
cipher::aes::{Aes128Ctr, Aes256Ctr},
d::{Sha1, Sha256},
};
use tor_proto::bench_utils::{
client_decrypt, encrypt_inbound, HopCryptState, InboundCryptWrapper, RelayBody,
};
mod cpu_time;
use cpu_time::*;
// Helper macro to setup a full circuit decryption benchmark.
macro_rules! full_circuit_inbound_setup {
($sc:ty, $d:ty, $f:ty) => {{
let seed1: SecretBuf = b"hidden we are free".to_vec().into();
let seed2: SecretBuf = b"free to speak, to free ourselves".to_vec().into();
let seed3: SecretBuf = b"free to hide no more".to_vec().into();
let mut rng = thread_rng();
let mut circuit_sates = [
HopCryptState::construct(seed1.clone()).unwrap(),
HopCryptState::construct(seed2.clone()).unwrap(),
HopCryptState::construct(seed3.clone()).unwrap(),
];
let mut cc_in = InboundCryptWrapper::new();
cc_in.add_layer_from_seed::<$sc, $d, $f>(seed1).unwrap();
cc_in.add_layer_from_seed::<$sc, $d, $f>(seed2).unwrap();
cc_in.add_layer_from_seed::<$sc, $d, $f>(seed3).unwrap();
let cell = create_inbound_cell::<$sc, $d, $f>(&mut rng, &mut circuit_sates);
(cell, cc_in)
}};
}
/// Encrypt a random cell using the given circuit crypt states
/// as if it were an inbound cell encrypted by each router in the circuit.
fn create_inbound_cell<
SC: StreamCipher + KeyIvInit,
D: Digest + Clone,
RCF: RelayCellFormatTrait,
>(
rng: &mut ThreadRng,
circuit_crypt_states: &mut [HopCryptState<SC, D, RCF>],
) -> RelayBody {
let mut cell = [0u8; 509];
rng.fill(&mut cell[..]);
let mut cell: RelayBody = cell.into();
encrypt_inbound(&mut cell, circuit_crypt_states);
cell
}
/// Benchmark the `client_decrypt` function.
pub fn cell_decrypt_benchmark(c: &mut Criterion<CpuTime>) {
let mut group = c.benchmark_group("cell_decrypt");
group.throughput(Throughput::Bytes(509));
group.bench_function("cell_decrypt_Tor1RelayCrypto", |b| {
b.iter_batched_ref(
|| full_circuit_inbound_setup!(Aes128Ctr, Sha1, RelayCellFormatV0),
|(cell, cc_in)| {
client_decrypt(cell, cc_in).unwrap();
},
criterion::BatchSize::SmallInput,
);
});
group.bench_function("cell_decrypt_Tor1Hsv3RelayCrypto", |b| {
b.iter_batched_ref(
|| full_circuit_inbound_setup!(Aes256Ctr, Sha256, RelayCellFormatV0),
|(cell, cc_in)| {
client_decrypt(cell, cc_in).unwrap();
},
criterion::BatchSize::SmallInput,
);
});
group.finish();
}
criterion_group!(
name = cell_decrypt;
config = Criterion::default()
.with_measurement(CpuTime)
.sample_size(5000);
targets = cell_decrypt_benchmark);
criterion_main!(cell_decrypt);
|