Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ p384 = { version = "0.13.0", default-features = false, features = [
"hash2curve",
"voprf",
] }
blind-rsa-signatures = "0.15.0"
blind-rsa-signatures = "0.17"
http = "1"
typenum = "1"
nom = "8"
Expand Down
14 changes: 7 additions & 7 deletions benches/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use privacypass::{
},
};

use blind_rsa_signatures::reexports::rand::CryptoRng;
use criterion::{Criterion, async_executor::FuturesExecutor};
use generic_array::ArrayLength;
use rand::{CryptoRng, RngCore};
use tokio::runtime::Runtime;

use privacypass::{TokenType, auth::authenticate::TokenChallenge};

async fn create_public_keypair<R: RngCore + CryptoRng>(
async fn create_public_keypair<R: CryptoRng>(
rng: &mut R,
key_store: public_memory_store::IssuerMemoryKeyStore,
server: privacypass::public_tokens::server::IssuerServer,
Expand Down Expand Up @@ -52,7 +52,7 @@ pub fn criterion_public_benchmark(c: &mut Criterion) {
c.bench_function("PUBLIC SERVER: Generate key pair", move |b| {
b.to_async(FuturesExecutor).iter_with_setup(
|| {
let rng = rand::thread_rng();
let rng = blind_rsa_signatures::reexports::rand::rng();
let key_store = IssuerMemoryKeyStore::default();
let server = IssuerServer::new();
(rng, key_store, server)
Expand All @@ -67,7 +67,7 @@ pub fn criterion_public_benchmark(c: &mut Criterion) {
c.bench_function("PUBLIC CLIENT: Issue token request", move |b| {
b.iter_with_setup(
|| {
let mut rng = rand::thread_rng();
let mut rng = blind_rsa_signatures::reexports::rand::rng();
let key_store = IssuerMemoryKeyStore::default();
let server = IssuerServer::new();

Expand All @@ -92,7 +92,7 @@ pub fn criterion_public_benchmark(c: &mut Criterion) {
c.bench_function("PUBLIC SERVER: Issue token response", move |b| {
b.to_async(FuturesExecutor).iter_with_setup(
|| {
let rng = &mut rand::thread_rng();
let rng = &mut blind_rsa_signatures::reexports::rand::rng();
let key_store = IssuerMemoryKeyStore::default();
let server = IssuerServer::new();

Expand All @@ -119,7 +119,7 @@ pub fn criterion_public_benchmark(c: &mut Criterion) {
c.bench_function("PUBLIC CLIENT: Issue token", move |b| {
b.iter_with_setup(
|| {
let rng = &mut rand::thread_rng();
let rng = &mut blind_rsa_signatures::reexports::rand::rng();
let key_store = IssuerMemoryKeyStore::default();
let server = IssuerServer::new();

Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn criterion_public_benchmark(c: &mut Criterion) {
c.bench_function("PUBLIC SERVER: Redeem token", move |b| {
b.to_async(FuturesExecutor).iter_with_setup(
|| {
let rng = &mut rand::thread_rng();
let rng = &mut blind_rsa_signatures::reexports::rand::rng();
let issuer_key_store = IssuerMemoryKeyStore::default();
let origin_key_store = OriginMemoryKeyStore::default();

Expand Down
3 changes: 1 addition & 2 deletions src/amortized_tokens/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ impl<CS: PrivateCipherSuite> AmortizedBatchTokenResponse<CS> {
.iter()
.zip(token_state.token_inputs.iter())
{
let authenticator =
GenericArray::from_slice(authenticator.as_ref()).clone();
let authenticator = GenericArray::from_slice(authenticator.as_ref()).clone();
let token = Token::new(
token_input.token_type,
token_input.nonce,
Expand Down
28 changes: 14 additions & 14 deletions src/public_tokens/det_rng.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Helper RNG that returns the same set of values for each call to (try_)fill_bytes.

use rand::{CryptoRng, Error, RngCore, rngs::OsRng};
use blind_rsa_signatures::DefaultRng;
use blind_rsa_signatures::reexports::rand::{TryCryptoRng, TryRng};
use std::convert::Infallible;

/// This RNG step is used to generate deterministic values for the nonce, salt,
/// and blind.
Expand Down Expand Up @@ -44,7 +46,7 @@ impl DeterministicRng {
self.additional_blind.as_deref()
}

fn fill_with_data(&mut self, dest: &mut [u8]) {
fn fill_with_data(&mut self, dest: &mut [u8]) -> Result<(), Infallible> {
match self.step {
RngStep::Nonce => {
dest.copy_from_slice(&self.nonce);
Expand All @@ -60,32 +62,30 @@ impl DeterministicRng {
}
RngStep::AdditionalBlind => {
let mut ab = [0u8; 256];
OsRng.fill_bytes(&mut ab);
DefaultRng.try_fill_bytes(&mut ab)?;
dest.copy_from_slice(&ab);
self.additional_blind = Some(ab.to_vec());
self.step = RngStep::AdditionalBlind;
}
}
Ok(())
}
}

impl RngCore for DeterministicRng {
fn next_u32(&mut self) -> u32 {
unimplemented!()
}
impl TryRng for DeterministicRng {
type Error = Infallible;

fn next_u64(&mut self) -> u64 {
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
unimplemented!()
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
self.fill_with_data(dest);
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
unimplemented!()
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_with_data(dest);
Ok(())
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible> {
self.fill_with_data(dest)
}
}

impl CryptoRng for DeterministicRng {}
impl TryCryptoRng for DeterministicRng {}
4 changes: 3 additions & 1 deletion src/public_tokens/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! # Publicly Verifiable Tokens

use blind_rsa_signatures::{Deterministic, PSS, Sha384};
use sha2::{Digest, Sha256};
use typenum::U256;

Expand All @@ -17,7 +18,8 @@ pub use response::*;

/// Publicly Verifiable Token alias
pub type PublicToken = Token<U256>;
pub use blind_rsa_signatures::PublicKey;
/// Publicly Verifiable Token public key type alias (SHA-384, PSS, Deterministic).
pub type PublicKey = blind_rsa_signatures::PublicKey<Sha384, PSS, Deterministic>;

use self::server::serialize_public_key;

Expand Down
15 changes: 8 additions & 7 deletions src/public_tokens/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Request implementation of the Publicly Verifiable Token protocol.

use blind_rsa_signatures::{BlindingResult, Options, PublicKey};
use rand::{CryptoRng, RngCore};
use blind_rsa_signatures::BlindingResult;
use blind_rsa_signatures::reexports::rand::CryptoRng;

use super::PublicKey;
use tls_codec_derive::{TlsDeserialize, TlsSerialize, TlsSize};

use crate::{
Expand Down Expand Up @@ -41,7 +43,7 @@ impl TokenRequest {
///
/// # Errors
/// Returns an error if the challenge is invalid.
pub fn new<R: RngCore + CryptoRng>(
pub fn new<R: CryptoRng>(
rng: &mut R,
public_key: PublicKey,
challenge: &TokenChallenge,
Expand All @@ -62,16 +64,15 @@ impl TokenRequest {

let token_input = TokenInput::new(TokenType::Public, nonce, challenge_digest, token_key_id);

let options = Options::default();
let blinding_result = public_key
.blind(rng, token_input.serialize(), false, &options)
.blind(rng, token_input.serialize())
.map_err(|source| IssueTokenRequestError::BlindingError {
source: source.into(),
})?;

debug_assert!(blinding_result.blind_msg.len() == NK);
debug_assert!(blinding_result.blind_message.len() == NK);
let mut blinded_msg = [0u8; NK];
blinded_msg.copy_from_slice(blinding_result.blind_msg.as_slice());
blinded_msg.copy_from_slice(blinding_result.blind_message.as_slice());

let token_request = TokenRequest {
token_type: TokenType::Public,
Expand Down
11 changes: 2 additions & 9 deletions src/public_tokens/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Response implementation of the Publicly Verifiable Token protocol.

use blind_rsa_signatures::{BlindSignature, Options};
use blind_rsa_signatures::BlindSignature;
use generic_array::{GenericArray, typenum::U256};
use tls_codec_derive::{TlsDeserialize, TlsSerialize, TlsSize};

Expand Down Expand Up @@ -28,18 +28,11 @@ impl TokenResponse {
pub fn issue_token(self, token_state: &TokenState) -> Result<PublicToken, IssueTokenError> {
// authenticator = rsabssa_finalize(pkI, nonce, blind_sig, blind_inv)
let token_input = token_state.token_input.serialize();
let options = Options::default();
let token_type = TokenType::Public;
let blind_sig = BlindSignature(self.blind_sig.to_vec());
let signature = token_state
.public_key
.finalize(
&blind_sig,
&token_state.blinding_result.secret,
None,
token_input,
&options,
)
.finalize(&blind_sig, &token_state.blinding_result, token_input)
.map_err(|source| IssueTokenError::SignatureFinalizationFailed {
token_type,
source,
Expand Down
22 changes: 12 additions & 10 deletions src/public_tokens/server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
//! Server-side implementation of Publicly Verifiable Token protocol.

use async_trait::async_trait;
use blind_rsa_signatures::{KeyPair, Options, PublicKey, Signature};
use blind_rsa_signatures::reexports::rand::CryptoRng;
use blind_rsa_signatures::{
Deterministic, KeyPair as GenericKeyPair, PSS, PublicKey as GenericPublicKey, Sha384, Signature,
};
use generic_array::ArrayLength;
use rand::{CryptoRng, RngCore, rngs::OsRng};

type KeyPair = GenericKeyPair<Sha384, PSS, Deterministic>;
type PublicKey = GenericPublicKey<Sha384, PSS, Deterministic>;

use crate::{
COLLISION_AVOIDANCE_ATTEMPTS, NonceStore, TokenInput, TokenType, TruncatedTokenKeyId,
Expand Down Expand Up @@ -41,7 +46,7 @@ pub trait OriginKeyStore {
/// Serializes a keypair into a DER-encoded PKCS#8 document.
#[must_use]
pub fn serialize_public_key(public_key: &PublicKey) -> Vec<u8> {
public_key.to_spki(Some(&Options::default())).unwrap()
public_key.to_spki().unwrap()
}

const KEYSIZE_IN_BITS: usize = 2048;
Expand All @@ -63,7 +68,7 @@ impl IssuerServer {
///
/// # Errors
/// Returns an error if creating the keypair fails.
pub async fn create_keypair<IKS: IssuerKeyStore, R: RngCore + CryptoRng>(
pub async fn create_keypair<IKS: IssuerKeyStore, R: CryptoRng>(
&self,
rng: &mut R,
key_store: &IKS,
Expand Down Expand Up @@ -96,7 +101,6 @@ impl IssuerServer {
key_store: &IKS,
token_request: TokenRequest,
) -> Result<TokenResponse, IssueTokenResponseError> {
let rng = &mut OsRng;
if token_request.token_type != TokenType::Public {
return Err(IssueTokenResponseError::InvalidTokenType {
expected: TokenType::Public,
Expand All @@ -109,10 +113,9 @@ impl IssuerServer {
.ok_or(IssueTokenResponseError::KeyIdNotFound)?;

// blind_sig = rsabssa_blind_sign(skI, TokenRequest.blinded_msg)
let options = Options::default();
let blind_signature = key_pair
.sk
.blind_sign(rng, token_request.blinded_msg, &options)
.blind_sign(token_request.blinded_msg)
.map_err(|source| IssueTokenResponseError::BlindSignatureFailed { source })?;

debug_assert!(blind_signature.len() == NK);
Expand Down Expand Up @@ -182,13 +185,12 @@ impl OriginServer {
return Err(RedeemTokenError::KeyIdNotFound);
}

let options = Options::default();
let signature = Signature(token.authenticator().to_vec());
let token_input_bytes = token_input.serialize();

let verified = public_keys.iter().any(|public_key| {
signature
.verify(public_key, None, &token_input_bytes, &options)
public_key
.verify(&signature, None, &token_input_bytes)
.is_ok()
});

Expand Down
5 changes: 4 additions & 1 deletion src/test_utils/public_memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//! `OriginKeyStore` traits.
use crate::{TruncatedTokenKeyId, public_tokens::server::*};
use async_trait::async_trait;
use blind_rsa_signatures::{KeyPair, PublicKey};
use blind_rsa_signatures::{Deterministic, PSS, Sha384};

type KeyPair = blind_rsa_signatures::KeyPair<Sha384, PSS, Deterministic>;
type PublicKey = blind_rsa_signatures::PublicKey<Sha384, PSS, Deterministic>;
use std::collections::{HashMap, hash_map::Entry};
use tokio::sync::Mutex;

Expand Down
4 changes: 2 additions & 2 deletions tests/generic_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use blind_rsa_signatures::reexports::rand::rng;
use p384::NistP384;
use privacypass::{
TokenType,
Expand All @@ -14,7 +15,6 @@ use privacypass::{
public_memory_store::{IssuerMemoryKeyStore, OriginMemoryKeyStore},
},
};
use rand::thread_rng;
use voprf::Ristretto255;

#[tokio::test]
Expand Down Expand Up @@ -45,7 +45,7 @@ async fn generic_tokens_cycle() {

// === Set up the public token server ===

let rng = &mut thread_rng();
let rng = &mut rng();

// Server: Instantiate in-memory keystore and nonce store.
let issuer_key_store = IssuerMemoryKeyStore::default();
Expand Down
2 changes: 1 addition & 1 deletion tests/kat_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub(crate) async fn evaluate_vector<CS: PrivateCipherSuite>(vector: PrivateToken
// Convert parameters
let token_challenge = TokenChallenge::deserialize(vector.token_challenge.as_slice()).unwrap();
let challenge_digest: [u8; 32] = token_challenge.digest().unwrap();
let nonce: [u8; 32] = <[u8; 32]>::try_from(vector.nonce.as_ref()).unwrap();
let nonce: [u8; 32] = <[u8; 32]>::try_from(vector.nonce.as_slice()).unwrap();
let blind = <CS::Group as Group>::deserialize_scalar(&vector.blind).unwrap();

// Client: Prepare a TokenRequest after having received a challenge
Expand Down
Loading