KDF

Struct KDF 

Source
pub struct KDF {
    pub key_hash: [u8; 20],
    pub forward_digest: [u8; 20],
    pub backward_digest: [u8; 20],
    pub forward_key: [u8; 16],
    pub backward_key: [u8; 16],
}
Expand description

KDF-TOR derived key material.

Contains the cryptographic keys and digests derived from shared key material during circuit creation. This implements the KDF-TOR key derivation function as defined in tor-spec section 5.2.1.

The derivation uses SHA-1 in a counter mode:

K = H(K0 | [00]) | H(K0 | [01]) | H(K0 | [02]) | ...

Where K0 is the input key material and H is SHA-1.

§Fields

The derived key material is split into five parts:

FieldSizePurpose
key_hash20 bytesProves knowledge of shared key
forward_digest20 bytesForward digest hash seed
backward_digest20 bytesBackward digest hash seed
forward_key16 bytesForward encryption key (AES-128)
backward_key16 bytesBackward encryption key (AES-128)

§Example

use stem_rs::client::datatype::KDF;

// Derive keys from shared secret (e.g., from CREATE_FAST handshake)
let key_material = b"shared_secret_from_handshake____";
let kdf = KDF::from_value(key_material);

// Use the derived keys for encryption
assert_eq!(kdf.forward_key.len(), 16);
assert_eq!(kdf.backward_key.len(), 16);

§Security

This KDF is used with the TAP and CREATE_FAST handshakes. Modern Tor circuits use the ntor handshake with a different KDF (HKDF-SHA256).

Fields§

§key_hash: [u8; 20]

Hash that proves knowledge of the shared key.

This is compared with the value sent by the relay to verify both parties derived the same key material.

§forward_digest: [u8; 20]

Forward digest hash seed.

Used to initialize the running digest for cells sent from client to relay.

§backward_digest: [u8; 20]

Backward digest hash seed.

Used to initialize the running digest for cells sent from relay to client.

§forward_key: [u8; 16]

Forward encryption key (AES-128-CTR).

Used to encrypt relay cells sent from client to relay.

§backward_key: [u8; 16]

Backward encryption key (AES-128-CTR).

Used to decrypt relay cells received from relay.

Implementations§

Source§

impl KDF

Source

pub fn from_value(key_material: &[u8]) -> Self

Derives key material from a shared secret.

Implements the KDF-TOR key derivation function from tor-spec section 5.2.1. The input key material is expanded using SHA-1 in counter mode to produce the required key material.

§Arguments
  • key_material - The shared secret from the circuit handshake
§Returns

A KDF struct containing all derived keys and digests.

§Algorithm
derived = H(key_material | 0x00) | H(key_material | 0x01) | ...
key_hash       = derived[0..20]
forward_digest = derived[20..40]
backward_digest= derived[40..60]
forward_key    = derived[60..76]
backward_key   = derived[76..92]
§Example
use stem_rs::client::datatype::KDF;

let shared_secret = b"example_shared_secret___________";
let kdf = KDF::from_value(shared_secret);

// All fields are populated
assert_eq!(kdf.key_hash.len(), 20);
assert_eq!(kdf.forward_key.len(), 16);

Trait Implementations§

Source§

impl Clone for KDF

Source§

fn clone(&self) -> KDF

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for KDF

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for KDF

Source§

fn eq(&self, other: &KDF) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for KDF

Source§

impl StructuralPartialEq for KDF

Auto Trait Implementations§

§

impl Freeze for KDF

§

impl RefUnwindSafe for KDF

§

impl Send for KDF

§

impl Sync for KDF

§

impl Unpin for KDF

§

impl UnwindSafe for KDF

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, 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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.