Skip to content

Commit

Permalink
refactor BLindingFactor into StaticSecret
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwicky committed Apr 29, 2024
1 parent 8069c51 commit 6899145
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
19 changes: 8 additions & 11 deletions src/header/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::convert::TryInto;
use std::fmt;

use crate::constants::{
Expand All @@ -30,14 +31,13 @@ pub type HeaderIntegrityMacKey = [u8; INTEGRITY_MAC_KEY_SIZE];
// TODO: perhaps change PayloadKey to a Vec considering it's almost 200 bytes long?
// we will lose length assertions but won't need to copy all that data every single function call
pub type PayloadKey = [u8; PAYLOAD_KEY_SIZE];
pub type BlindingFactor = [u8; BLINDING_FACTOR_SIZE];

#[derive(Clone)]
pub struct RoutingKeys {
pub stream_cipher_key: StreamCipherKey,
pub header_integrity_hmac_key: HeaderIntegrityMacKey,
pub payload_key: PayloadKey,
pub blinding_factor: BlindingFactor,
pub blinding_factor: StaticSecret,
}

impl RoutingKeys {
Expand All @@ -63,11 +63,10 @@ impl RoutingKeys {
payload_key.copy_from_slice(&output[i..i + PAYLOAD_KEY_SIZE]);
i += PAYLOAD_KEY_SIZE;

// TODO: we later treat blinding factor as a Scalar, the question is, should it be clamped
// and/or go through montgomery reduction? We kinda need somebody with good ECC knowledge
// to answer this question (and other related ones).
let mut blinding_factor: [u8; BLINDING_FACTOR_SIZE] = Default::default();
blinding_factor.copy_from_slice(&output[i..i + BLINDING_FACTOR_SIZE]);
//Safety, converting a slice of size BLINDING_FACTOR_SIZE into an array of type [u8; BLINDING_FACTOR_SIZE], hence unwrap is fine
let blinding_factor_bytes: [u8; BLINDING_FACTOR_SIZE] =
output[i..i + BLINDING_FACTOR_SIZE].try_into().unwrap();
let blinding_factor = StaticSecret::from(blinding_factor_bytes);

Self {
stream_cipher_key,
Expand Down Expand Up @@ -121,8 +120,7 @@ impl KeyMaterial {

// it's not the last iteration
if i != route.len() + 1 {
let next_blinding_factor = StaticSecret::from(node_routing_keys.blinding_factor);
blinding_factors.push(next_blinding_factor);
blinding_factors.push(node_routing_keys.blinding_factor.clone());
}

routing_keys.push(node_routing_keys);
Expand Down Expand Up @@ -203,8 +201,7 @@ mod deriving_key_material {

let expected_routing_keys = RoutingKeys::derive(expected_shared_key);

expected_accumulator
.push(StaticSecret::from(expected_routing_keys.blinding_factor));
expected_accumulator.push(expected_routing_keys.blinding_factor);
let expected_routing_keys = RoutingKeys::derive(expected_shared_key);
assert_eq!(expected_routing_keys, key_material.routing_keys[i])
}
Expand Down
5 changes: 2 additions & 3 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use crate::constants::HEADER_INTEGRITY_MAC_SIZE;
use crate::header::delays::Delay;
use crate::header::filler::Filler;
use crate::header::keys::{BlindingFactor, PayloadKey};
use crate::header::keys::PayloadKey;
use crate::header::routing::nodes::ParsedRawRoutingInformation;
use crate::header::routing::{EncapsulatedRoutingInformation, ENCRYPTED_ROUTING_INFO_SIZE};
use crate::route::{Destination, DestinationAddressBytes, Node, NodeAddressBytes, SURBIdentifier};
Expand Down Expand Up @@ -233,9 +233,8 @@ impl SphinxHeader {

fn blind_the_shared_secret(
shared_secret: PublicKey,
blinding_factor: BlindingFactor,
blinding_factor: StaticSecret,
) -> PublicKey {
let blinding_factor = StaticSecret::from(blinding_factor);
// shared_secret * blinding_factor
let new_shared_secret = blinding_factor.diffie_hellman(&shared_secret);
PublicKey::from(new_shared_secret.to_bytes())
Expand Down
2 changes: 1 addition & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub mod fixtures {
stream_cipher_key: [1u8; crypto::STREAM_CIPHER_KEY_SIZE],
header_integrity_hmac_key: [2u8; INTEGRITY_MAC_KEY_SIZE],
payload_key: [3u8; PAYLOAD_KEY_SIZE],
blinding_factor: [4u8; BLINDING_FACTOR_SIZE],
blinding_factor: [4u8; BLINDING_FACTOR_SIZE].into(),
}
}

Expand Down

0 comments on commit 6899145

Please sign in to comment.