Skip to main content

DataCodec

Struct DataCodec 

Source
pub struct DataCodec { /* private fields */ }
Expand description

Negotiated key material for exactly one endpoint-to-relay connection. Debug output intentionally never includes the secret.

Implementations§

Source§

impl DataCodec

Source

pub fn legacy(key: AesKeyType) -> Self

Wrap an existing legacy key without changing its wire encoding.

Examples found in repository?
examples/data_codec_bench.rs (line 13)
9fn measure(v2: bool, size: usize, iterations: usize) -> Result<f64, Box<dyn Error>> {
10    let codec = if v2 {
11        DataCodec::negotiate([42; 32], Some(2), HeaderProtocol::V2)?
12    } else {
13        DataCodec::legacy([42; 32])
14    };
15    if size == 0 {
16        let start = Instant::now();
17        for _ in 0..iterations {
18            black_box(black_box(codec).endpoint_codecs()?);
19        }
20        return Ok(start.elapsed().as_secs_f64() * 1e9 / iterations as f64);
21    }
22    let (_, mut writer) = codec.endpoint_codecs()?;
23    let (mut reader, _) = codec.relay_codecs()?;
24    let mut bytes = vec![42; size + 16];
25    let start = Instant::now();
26    for _ in 0..iterations {
27        let tag = writer
28            .encrypt(black_box(&mut bytes[..size]))
29            .map_err(|_| "encrypt failed")?;
30        bytes[size..].copy_from_slice(tag.as_ref());
31        black_box(
32            reader
33                .decrypt(black_box(&mut bytes))
34                .map_err(|_| "decrypt failed")?,
35        );
36    }
37    let elapsed = start.elapsed().as_secs_f64();
38    assert!(bytes[..size].iter().all(|&byte| byte == 42));
39    Ok(elapsed * 1e9 / iterations as f64)
40}
Source

pub fn from_response( key: Option<AesKeyType>, protocol: Option<u16>, offered_v2: bool, ) -> Result<Option<Self>>

Validate an authenticated response. Unknown selections are errors and must never cause another connection in a weaker mode.

Source

pub fn negotiate( key: AesKeyType, offer: Option<u16>, header: HeaderProtocol, ) -> Result<Self>

Select only a capability received through authenticated control-v2.

Examples found in repository?
examples/data_codec_bench.rs (line 11)
9fn measure(v2: bool, size: usize, iterations: usize) -> Result<f64, Box<dyn Error>> {
10    let codec = if v2 {
11        DataCodec::negotiate([42; 32], Some(2), HeaderProtocol::V2)?
12    } else {
13        DataCodec::legacy([42; 32])
14    };
15    if size == 0 {
16        let start = Instant::now();
17        for _ in 0..iterations {
18            black_box(black_box(codec).endpoint_codecs()?);
19        }
20        return Ok(start.elapsed().as_secs_f64() * 1e9 / iterations as f64);
21    }
22    let (_, mut writer) = codec.endpoint_codecs()?;
23    let (mut reader, _) = codec.relay_codecs()?;
24    let mut bytes = vec![42; size + 16];
25    let start = Instant::now();
26    for _ in 0..iterations {
27        let tag = writer
28            .encrypt(black_box(&mut bytes[..size]))
29            .map_err(|_| "encrypt failed")?;
30        bytes[size..].copy_from_slice(tag.as_ref());
31        black_box(
32            reader
33                .decrypt(black_box(&mut bytes))
34                .map_err(|_| "decrypt failed")?,
35        );
36    }
37    let elapsed = start.elapsed().as_secs_f64();
38    assert!(bytes[..size].iter().all(|&byte| byte == 42));
39    Ok(elapsed * 1e9 / iterations as f64)
40}
Source

pub fn validate_offer(offer: Option<u16>, header: HeaderProtocol) -> Result<()>

Reject unknown or unauthenticated selections before any setup response.

Source

pub fn key(self) -> AesKeyType

The per-leg secret sent inside the authenticated setup response.

Source

pub fn protocol(self) -> Option<u16>

Omit the field completely when interoperating with a legacy peer.

Source

pub fn endpoint_codecs(self) -> Result<(Aes256GcmDeCodec, Aes256GcmEnCodec)>

Receiving and sending codecs from the local endpoint’s perspective.

Examples found in repository?
examples/data_codec_bench.rs (line 18)
9fn measure(v2: bool, size: usize, iterations: usize) -> Result<f64, Box<dyn Error>> {
10    let codec = if v2 {
11        DataCodec::negotiate([42; 32], Some(2), HeaderProtocol::V2)?
12    } else {
13        DataCodec::legacy([42; 32])
14    };
15    if size == 0 {
16        let start = Instant::now();
17        for _ in 0..iterations {
18            black_box(black_box(codec).endpoint_codecs()?);
19        }
20        return Ok(start.elapsed().as_secs_f64() * 1e9 / iterations as f64);
21    }
22    let (_, mut writer) = codec.endpoint_codecs()?;
23    let (mut reader, _) = codec.relay_codecs()?;
24    let mut bytes = vec![42; size + 16];
25    let start = Instant::now();
26    for _ in 0..iterations {
27        let tag = writer
28            .encrypt(black_box(&mut bytes[..size]))
29            .map_err(|_| "encrypt failed")?;
30        bytes[size..].copy_from_slice(tag.as_ref());
31        black_box(
32            reader
33                .decrypt(black_box(&mut bytes))
34                .map_err(|_| "decrypt failed")?,
35        );
36    }
37    let elapsed = start.elapsed().as_secs_f64();
38    assert!(bytes[..size].iter().all(|&byte| byte == 42));
39    Ok(elapsed * 1e9 / iterations as f64)
40}
Source

pub fn relay_codecs(self) -> Result<(Aes256GcmDeCodec, Aes256GcmEnCodec)>

Receiving and sending codecs from the relay’s perspective.

Examples found in repository?
examples/data_codec_bench.rs (line 23)
9fn measure(v2: bool, size: usize, iterations: usize) -> Result<f64, Box<dyn Error>> {
10    let codec = if v2 {
11        DataCodec::negotiate([42; 32], Some(2), HeaderProtocol::V2)?
12    } else {
13        DataCodec::legacy([42; 32])
14    };
15    if size == 0 {
16        let start = Instant::now();
17        for _ in 0..iterations {
18            black_box(black_box(codec).endpoint_codecs()?);
19        }
20        return Ok(start.elapsed().as_secs_f64() * 1e9 / iterations as f64);
21    }
22    let (_, mut writer) = codec.endpoint_codecs()?;
23    let (mut reader, _) = codec.relay_codecs()?;
24    let mut bytes = vec![42; size + 16];
25    let start = Instant::now();
26    for _ in 0..iterations {
27        let tag = writer
28            .encrypt(black_box(&mut bytes[..size]))
29            .map_err(|_| "encrypt failed")?;
30        bytes[size..].copy_from_slice(tag.as_ref());
31        black_box(
32            reader
33                .decrypt(black_box(&mut bytes))
34                .map_err(|_| "decrypt failed")?,
35        );
36    }
37    let elapsed = start.elapsed().as_secs_f64();
38    assert!(bytes[..size].iter().all(|&byte| byte == 42));
39    Ok(elapsed * 1e9 / iterations as f64)
40}

Trait Implementations§

Source§

impl Clone for DataCodec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DataCodec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more