tor_proto/crypto/cell/
bench_utils.rs
1#[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#[repr(transparent)]
10pub struct InboundClientLayerWrapper(pub(in crate::crypto) Box<dyn InboundClientLayer + Send>);
11
12#[repr(transparent)]
14pub struct InboundClientCryptWrapper(pub(in crate::crypto) InboundClientCrypt);
15
16impl InboundClientCryptWrapper {
17 pub fn new() -> Self {
19 Default::default()
20 }
21
22 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 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#[repr(transparent)]
46pub struct OutboundClientLayerWrapper(pub(in crate::crypto) Box<dyn OutboundClientLayer + Send>);
47
48#[repr(transparent)]
50pub struct OutboundClientCryptWrapper(pub(in crate::crypto) OutboundClientCrypt);
51
52impl OutboundClientCryptWrapper {
53 pub fn new() -> Self {
55 Default::default()
56 }
57
58 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 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#[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
91pub trait RelayCryptState {
93 fn originate(&mut self, cell: &mut RelayBody);
95 fn encrypt(&mut self, cell: &mut RelayBody);
97 fn decrypt(&mut self, cell: &mut RelayBody);
99}
100
101pub 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}