tor_proto/crypto/cell/
bench_utils.rs

1//! Benchmark utilities for the `cell` module.
2#[cfg(feature = "counter-galois-onion")]
3#[cfg_attr(docsrs, doc(cfg(feature = "counter-galois-onion")))]
4pub use super::cgo::bench_utils as cgo;
5pub use super::tor1::bench_utils as tor1;
6use super::*;
7
8/// Public wrapper around the `InboundClientLayer` trait object.
9#[repr(transparent)]
10pub struct InboundClientLayerWrapper(pub(in crate::crypto) Box<dyn InboundClientLayer + Send>);
11
12/// Public wrapper around the `InboundClientCrypt` struct.
13#[repr(transparent)]
14pub struct InboundClientCryptWrapper(pub(in crate::crypto) InboundClientCrypt);
15
16impl InboundClientCryptWrapper {
17    /// Create a new `InboundClientCryptWrapper`.
18    pub fn new() -> Self {
19        Default::default()
20    }
21
22    /// Add a new layer to the `InboundClientCryptWrapper` based on a seed.
23    pub fn add_layer(&mut self, crypt_state: impl Into<InboundClientLayerWrapper>) {
24        let layer: InboundClientLayerWrapper = crypt_state.into();
25        self.0.add_layer(layer.0);
26    }
27
28    /// Public wrapper around the `InboundClientCrypt::decrypt` method
29    /// for benchmarking purposes.
30    pub fn decrypt(&mut self, cell: &mut RelayBody) -> Result<()> {
31        let cell = &mut cell.0;
32        self.0.decrypt(ChanCmd::RELAY, cell)?;
33
34        Ok(())
35    }
36}
37
38impl Default for InboundClientCryptWrapper {
39    fn default() -> Self {
40        Self(InboundClientCrypt::new())
41    }
42}
43
44/// Public wrapper around the `OutboundClientLayer` trait object.
45#[repr(transparent)]
46pub struct OutboundClientLayerWrapper(pub(in crate::crypto) Box<dyn OutboundClientLayer + Send>);
47
48/// Public wrapper around the `OutboundClientCrypt` struct.
49#[repr(transparent)]
50pub struct OutboundClientCryptWrapper(pub(in crate::crypto) OutboundClientCrypt);
51
52impl OutboundClientCryptWrapper {
53    /// Create a new `OutboundClientCryptWrapper`.
54    pub fn new() -> Self {
55        Default::default()
56    }
57
58    /// Add a new layer to the `OutboundClientCryptWrapper` based on a seed.
59    pub fn add_layer(&mut self, crypt_state: impl Into<OutboundClientLayerWrapper>) {
60        let layer: OutboundClientLayerWrapper = crypt_state.into();
61        self.0.add_layer(layer.0);
62    }
63
64    /// Public wrapper around the `OutboundClientCrypt::encrypt` method
65    /// for benchmarking purposes.
66    pub fn encrypt(&mut self, cell: &mut RelayBody, hop_num: u8) -> Result<()> {
67        let cell = &mut cell.0;
68        self.0.encrypt(ChanCmd::RELAY, cell, hop_num.into())?;
69
70        Ok(())
71    }
72}
73
74impl Default for OutboundClientCryptWrapper {
75    fn default() -> Self {
76        Self(OutboundClientCrypt::new())
77    }
78}
79
80/// Public wrapper around the `RelayCellBody` struct.
81#[repr(transparent)]
82pub struct RelayBody(pub(in crate::crypto) RelayCellBody);
83
84impl From<[u8; 509]> for RelayBody {
85    fn from(body: [u8; 509]) -> Self {
86        let body = Box::new(body);
87        Self(body.into())
88    }
89}
90
91/// Public trait to define the interface of a wrapper around a relay cryptographic state.
92pub trait RelayCryptState {
93    /// Public wrapper around the `InboundRelayLayer::originate` method.
94    fn originate(&mut self, cell: &mut RelayBody);
95    /// Public wrapper around the `InboundRelayLayer::encrypt_inbound` method.
96    fn encrypt(&mut self, cell: &mut RelayBody);
97    /// Public wrapper around the `OutboundRelayLayer::decrypt_outbound` method.
98    fn decrypt(&mut self, cell: &mut RelayBody);
99}
100
101/// Encrypts the given `RelayCell` in the inbound direction by all the relays in a circuit.
102pub fn circuit_encrypt_inbound(cell: &mut RelayBody, relay_states: &mut [impl RelayCryptState]) {
103    for (i, state) in relay_states.iter_mut().rev().enumerate() {
104        if i == 0 {
105            state.originate(cell);
106        } else {
107            state.encrypt(cell);
108        }
109    }
110}