Skip to content

Commit

Permalink
Merge pull request #30 from raphaelrobert/align-base-spec
Browse files Browse the repository at this point in the history
Align token_key_id/truncated_token_key_id with the base spec
  • Loading branch information
raphaelrobert authored Apr 8, 2024
2 parents b88130a + d148e76 commit 13a3db6
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 167 deletions.
4 changes: 2 additions & 2 deletions benches/public.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use privacypass::public_tokens::{public_key_to_token_key_id, server::OriginKeyStore};
use privacypass::public_tokens::{public_key_to_truncated_token_key_id, server::OriginKeyStore};
#[path = "../tests/public_memory_stores.rs"]
mod public_memory_stores;

Expand Down Expand Up @@ -165,7 +165,7 @@ pub fn criterion_public_benchmark(c: &mut Criterion) {
.unwrap();
origin_key_store
.insert(
public_key_to_token_key_id(&key_pair.pk),
public_key_to_truncated_token_key_id(&key_pair.pk),
key_pair.pk.clone(),
)
.await;
Expand Down
10 changes: 5 additions & 5 deletions src/auth/authorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::io::Write;
use thiserror::Error;
use tls_codec::{Deserialize, Error, Serialize, Size};

use crate::{ChallengeDigest, KeyId, Nonce, TokenType};
use crate::{ChallengeDigest, Nonce, TokenKeyId, TokenType};

use super::{base64_char, key_name, opt_spaces, space};

Expand All @@ -34,7 +34,7 @@ pub struct Token<Nk: ArrayLength<u8>> {
token_type: TokenType,
nonce: Nonce,
challenge_digest: ChallengeDigest,
token_key_id: KeyId,
token_key_id: TokenKeyId,
authenticator: GenericArray<u8, Nk>,
}

Expand Down Expand Up @@ -66,7 +66,7 @@ impl<Nk: ArrayLength<u8>> Deserialize for Token<Nk> {
let token_type = TokenType::tls_deserialize(bytes)?;
let nonce = Nonce::tls_deserialize(bytes)?;
let challenge_digest = ChallengeDigest::tls_deserialize(bytes)?;
let token_key_id = KeyId::tls_deserialize(bytes)?;
let token_key_id = TokenKeyId::tls_deserialize(bytes)?;
let mut authenticator = vec![0u8; Nk::to_usize()];
let len = bytes.read(authenticator.as_mut_slice())?;
if len != Nk::to_usize() {
Expand All @@ -88,7 +88,7 @@ impl<Nk: ArrayLength<u8>> Token<Nk> {
token_type: TokenType,
nonce: Nonce,
challenge_digest: ChallengeDigest,
token_key_id: KeyId,
token_key_id: TokenKeyId,
authenticator: GenericArray<u8, Nk>,
) -> Self {
Self {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<Nk: ArrayLength<u8>> Token<Nk> {
}

/// Returns the token key ID.
pub const fn token_key_id(&self) -> &KeyId {
pub const fn token_key_id(&self) -> &TokenKeyId {
&self.token_key_id
}

Expand Down
23 changes: 13 additions & 10 deletions src/batched_tokens_p384/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use voprf::{EvaluationElement, Proof, Result, VoprfClient};

use crate::{
auth::{authenticate::TokenChallenge, authorize::Token},
ChallengeDigest, KeyId, TokenInput, TokenType,
ChallengeDigest, TokenInput, TokenKeyId, TokenType,
};

use super::{
key_id_to_token_key_id, public_key_to_key_id, BatchedToken, Nonce, PublicKey, TokenRequest,
TokenResponse,
public_key_to_token_key_id, truncate_token_key_id, BatchedToken, Nonce, PublicKey,
TokenRequest, TokenResponse,
};

/// Client-side state that is kept between the token requests and token responses.
Expand Down Expand Up @@ -45,17 +45,20 @@ pub enum IssueTokenError {
/// The client side of the batched token issuance protocol.
#[derive(Debug)]
pub struct Client {
key_id: KeyId,
token_key_id: TokenKeyId,
public_key: PublicKey,
}

impl Client {
/// Create a new client from a public key.
#[must_use]
pub fn new(public_key: PublicKey) -> Self {
let key_id = public_key_to_key_id(&public_key);
let token_key_id = public_key_to_token_key_id(&public_key);

Self { key_id, public_key }
Self {
token_key_id,
public_key,
}
}

/// Issue a token request.
Expand Down Expand Up @@ -97,14 +100,14 @@ impl Client {
for nonce in nonces {
// nonce = random(32)
// challenge_digest = SHA256(challenge)
// token_input = concat(0xF901, nonce, challenge_digest, key_id)
// token_input = concat(0xF901, nonce, challenge_digest, token_key_id)
// blind, blinded_element = client_context.Blind(token_input)

let token_input = TokenInput::new(
TokenType::BatchedTokenP384,
nonce,
challenge_digest,
self.key_id,
self.token_key_id,
);

let blinded_element =
Expand Down Expand Up @@ -138,7 +141,7 @@ impl Client {

let token_request = TokenRequest {
token_type: TokenType::BatchedTokenP384,
token_key_id: key_id_to_token_key_id(&self.key_id),
truncated_token_key_id: truncate_token_key_id(&self.token_key_id),
blinded_elements: blinded_elements.into(),
};

Expand Down Expand Up @@ -202,7 +205,7 @@ impl Client {
TokenType::BatchedTokenP384,
token_state.token_input.nonce,
token_state.challenge_digest,
token_state.token_input.key_id,
token_state.token_input.token_key_id,
*authenticator,
);
tokens.push(token);
Expand Down
12 changes: 6 additions & 6 deletions src/batched_tokens_p384/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tls_codec_derive::{TlsDeserialize, TlsSerialize, TlsSize};
use typenum::U48;
pub use voprf::*;

use crate::{auth::authorize::Token, KeyId, Nonce, TokenKeyId, TokenType};
use crate::{auth::authorize::Token, Nonce, TokenKeyId, TokenType, TruncatedTokenKeyId};

use self::server::serialize_public_key;

Expand All @@ -27,14 +27,14 @@ pub type BatchedToken = Token<U48>;
/// Public key alias
pub type PublicKey = <NistP384 as Group>::Elem;

fn public_key_to_key_id(public_key: &PublicKey) -> KeyId {
fn public_key_to_token_key_id(public_key: &PublicKey) -> TokenKeyId {
let public_key = serialize_public_key(*public_key);

Sha256::digest(public_key).into()
}

fn key_id_to_token_key_id(key_id: &KeyId) -> TokenKeyId {
*key_id.iter().last().unwrap_or(&0)
fn truncate_token_key_id(token_key_id: &TokenKeyId) -> TruncatedTokenKeyId {
*token_key_id.iter().last().unwrap_or(&0)
}

/// Serialization error
Expand Down Expand Up @@ -62,14 +62,14 @@ pub struct BlindedElement {
/// ```c
/// struct {
/// uint16_t token_type = 0xF901;
/// uint8_t token_key_id;
/// uint8_t truncated_token_key_id;
/// BlindedElement blinded_element[Nr];
/// } TokenRequest;
/// ```
#[derive(Debug, TlsDeserialize, TlsSerialize, TlsSize)]
pub struct TokenRequest {
token_type: TokenType,
token_key_id: TokenKeyId,
truncated_token_key_id: TruncatedTokenKeyId,
blinded_elements: TlsVecU16<BlindedElement>,
}

Expand Down
36 changes: 23 additions & 13 deletions src/batched_tokens_p384/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use voprf::{
BlindedElement, Error, Group, Result, VoprfServer, VoprfServerBatchEvaluateFinishResult,
};

use crate::{NonceStore, TokenInput, TokenKeyId, TokenType};
use crate::{NonceStore, TokenInput, TokenType, TruncatedTokenKeyId};

use super::{
key_id_to_token_key_id, public_key_to_key_id, BatchedToken, PublicKey, TokenRequest,
public_key_to_token_key_id, truncate_token_key_id, BatchedToken, PublicKey, TokenRequest,
TokenResponse, NK, NS,
};

Expand Down Expand Up @@ -56,10 +56,17 @@ pub enum RedeemTokenError {
/// that the store requires inner mutability.
#[async_trait]
pub trait BatchedKeyStore: Send + Sync {
/// Inserts a keypair with a given `token_key_id` into the key store.
async fn insert(&self, token_key_id: TokenKeyId, server: VoprfServer<NistP384>);
/// Returns a keypair with a given `token_key_id` from the key store.
async fn get(&self, token_key_id: &TokenKeyId) -> Option<VoprfServer<NistP384>>;
/// Inserts a keypair with a given `truncated_token_key_id` into the key store.
async fn insert(
&self,
truncated_token_key_id: TruncatedTokenKeyId,
server: VoprfServer<NistP384>,
);
/// Returns a keypair with a given `truncated_token_key_id` from the key store.
async fn get(
&self,
truncated_token_key_id: &TruncatedTokenKeyId,
) -> Option<VoprfServer<NistP384>>;
}

/// Serializes a public key.
Expand Down Expand Up @@ -111,8 +118,9 @@ impl Server {
let server = VoprfServer::<NistP384>::new_from_seed(seed, info)
.map_err(|_| CreateKeypairError::SeedError)?;
let public_key = server.get_public_key();
let token_key_id = key_id_to_token_key_id(&public_key_to_key_id(&server.get_public_key()));
key_store.insert(token_key_id, server).await;
let truncated_token_key_id =
truncate_token_key_id(&public_key_to_token_key_id(&server.get_public_key()));
key_store.insert(truncated_token_key_id, server).await;
Ok(public_key)
}

Expand Down Expand Up @@ -141,7 +149,7 @@ impl Server {
return Err(IssueTokenResponseError::InvalidTokenType);
}
let server = key_store
.get(&token_request.token_key_id)
.get(&token_request.truncated_token_key_id)
.await
.ok_or(IssueTokenResponseError::KeyIdNotFound)?;

Expand Down Expand Up @@ -196,10 +204,10 @@ impl Server {
token_type: token.token_type(),
nonce: token.nonce(),
challenge_digest: *token.challenge_digest(),
key_id: *token.token_key_id(),
token_key_id: *token.token_key_id(),
};
let server = key_store
.get(&key_id_to_token_key_id(token.token_key_id()))
.get(&truncate_token_key_id(token.token_key_id()))
.await
.ok_or(RedeemTokenError::KeyIdNotFound)?;
let token_authenticator = server
Expand All @@ -224,8 +232,10 @@ impl Server {
let server = VoprfServer::<NistP384>::new_with_key(private_key)
.map_err(|_| CreateKeypairError::SeedError)?;
let public_key = server.get_public_key();
let token_key_id = key_id_to_token_key_id(&public_key_to_key_id(&server.get_public_key()));
key_store.insert(token_key_id, server).await;
let token_key_id = public_key_to_token_key_id(&server.get_public_key());
key_store
.insert(truncate_token_key_id(&token_key_id), server)
.await;
Ok(public_key)
}
}
Expand Down
23 changes: 13 additions & 10 deletions src/batched_tokens_ristretto255/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use voprf::{EvaluationElement, Proof, Result, Ristretto255, VoprfClient};

use crate::{
auth::{authenticate::TokenChallenge, authorize::Token},
ChallengeDigest, KeyId, TokenInput, TokenType,
ChallengeDigest, TokenInput, TokenKeyId, TokenType,
};

use super::{
key_id_to_token_key_id, public_key_to_key_id, BatchedToken, Nonce, PublicKey, TokenRequest,
TokenResponse,
public_key_to_token_key_id, truncate_token_key_id, BatchedToken, Nonce, PublicKey,
TokenRequest, TokenResponse,
};

/// Client-side state that is kept between the token requests and token responses.
Expand Down Expand Up @@ -44,17 +44,20 @@ pub enum IssueTokenError {
/// The client side of the batched token issuance protocol.
#[derive(Debug)]
pub struct Client {
key_id: KeyId,
token_key_id: TokenKeyId,
public_key: PublicKey,
}

impl Client {
/// Create a new client from a public key.
#[must_use]
pub fn new(public_key: PublicKey) -> Self {
let key_id = public_key_to_key_id(&public_key);
let token_key_id = public_key_to_token_key_id(&public_key);

Self { key_id, public_key }
Self {
token_key_id,
public_key,
}
}

/// Issue a token request.
Expand Down Expand Up @@ -96,14 +99,14 @@ impl Client {
for nonce in nonces {
// nonce = random(32)
// challenge_digest = SHA256(challenge)
// token_input = concat(0xF91A, nonce, challenge_digest, key_id)
// token_input = concat(0xF91A, nonce, challenge_digest, token_key_id)
// blind, blinded_element = client_context.Blind(token_input)

let token_input = TokenInput::new(
TokenType::BatchedTokenRistretto255,
nonce,
challenge_digest,
self.key_id,
self.token_key_id,
);

let blinded_element =
Expand Down Expand Up @@ -137,7 +140,7 @@ impl Client {

let token_request = TokenRequest {
token_type: TokenType::BatchedTokenRistretto255,
token_key_id: key_id_to_token_key_id(&self.key_id),
truncated_token_key_id: truncate_token_key_id(&self.token_key_id),
blinded_elements: blinded_elements.into(),
};

Expand Down Expand Up @@ -201,7 +204,7 @@ impl Client {
TokenType::BatchedTokenRistretto255,
token_state.token_input.nonce,
token_state.challenge_digest,
token_state.token_input.key_id,
token_state.token_input.token_key_id,
*authenticator,
);
tokens.push(token);
Expand Down
12 changes: 6 additions & 6 deletions src/batched_tokens_ristretto255/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tls_codec_derive::{TlsDeserialize, TlsSerialize, TlsSize};
use typenum::U64;
pub use voprf::*;

use crate::{auth::authorize::Token, KeyId, Nonce, TokenKeyId, TokenType};
use crate::{auth::authorize::Token, Nonce, TokenKeyId, TokenType, TruncatedTokenKeyId};

use self::server::serialize_public_key;

Expand All @@ -26,14 +26,14 @@ pub type BatchedToken = Token<U64>;
/// Public key alias
pub type PublicKey = <Ristretto255 as Group>::Elem;

fn public_key_to_key_id(public_key: &PublicKey) -> KeyId {
fn public_key_to_token_key_id(public_key: &PublicKey) -> TokenKeyId {
let public_key = serialize_public_key(*public_key);

Sha256::digest(public_key).into()
}

fn key_id_to_token_key_id(key_id: &KeyId) -> TokenKeyId {
*key_id.iter().last().unwrap_or(&0)
fn truncate_token_key_id(token_key_id: &TokenKeyId) -> TruncatedTokenKeyId {
*token_key_id.iter().last().unwrap_or(&0)
}

/// Serialization error
Expand Down Expand Up @@ -61,14 +61,14 @@ pub struct BlindedElement {
/// ```c
/// struct {
/// uint16_t token_type = 0xF91A;
/// uint8_t token_key_id;
/// uint8_t truncated_token_key_id;
/// BlindedElement blinded_element[Nr];
/// } TokenRequest;
/// ```
#[derive(Debug, TlsDeserialize, TlsSerialize, TlsSize)]
pub struct TokenRequest {
token_type: TokenType,
token_key_id: TokenKeyId,
truncated_token_key_id: TruncatedTokenKeyId,
blinded_elements: TlsVecU16<BlindedElement>,
}

Expand Down
Loading

0 comments on commit 13a3db6

Please sign in to comment.