Skip to content

Commit 2e6c0ed

Browse files
refactor: Cipher suite (#40)
* Move cipher suite * Rename cipher suite
1 parent 371c118 commit 2e6c0ed

File tree

17 files changed

+130
-120
lines changed

17 files changed

+130
-120
lines changed

benches/amortized.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ use p384::NistP384;
33
use tokio::runtime::Runtime;
44

55
use privacypass::{
6-
PPCipherSuite,
76
amortized_tokens::{
87
AmortizedBatchTokenRequest, AmortizedBatchTokenResponse, AmortizedToken, server::Server,
98
},
109
auth::authenticate::TokenChallenge,
10+
common::private::PrivateCipherSuite,
1111
test_utils::{nonce_store::MemoryNonceStore, private_memory_store::MemoryKeyStoreVoprf},
1212
};
1313
use voprf::Ristretto255;
1414

15-
async fn create_amortized_keypair<CS: PPCipherSuite>(
15+
async fn create_amortized_keypair<CS: PrivateCipherSuite>(
1616
key_store: MemoryKeyStoreVoprf<CS>,
1717
server: Server<CS>,
1818
) {
1919
let _public_key = server.create_keypair(&key_store).await.unwrap();
2020
}
2121

22-
async fn issue_amortized_token_response<CS: PPCipherSuite>(
22+
async fn issue_amortized_token_response<CS: PrivateCipherSuite>(
2323
key_store: MemoryKeyStoreVoprf<CS>,
2424
server: Server<CS>,
2525
token_request: AmortizedBatchTokenRequest<CS>,
@@ -30,7 +30,7 @@ async fn issue_amortized_token_response<CS: PPCipherSuite>(
3030
.unwrap()
3131
}
3232

33-
async fn redeem_amortized_token<CS: PPCipherSuite>(
33+
async fn redeem_amortized_token<CS: PrivateCipherSuite>(
3434
key_store: MemoryKeyStoreVoprf<CS>,
3535
nonce_store: MemoryNonceStore,
3636
token: AmortizedToken<CS>,
@@ -50,7 +50,7 @@ pub fn criterion_amortized_ristretto255_benchmark(c: &mut Criterion) {
5050
flow::<Ristretto255>(c);
5151
}
5252

53-
pub fn flow<CS: PPCipherSuite>(c: &mut Criterion) {
53+
pub fn flow<CS: PrivateCipherSuite>(c: &mut Criterion) {
5454
const NR: u16 = 100;
5555
// Key pair generation
5656
c.bench_function(

benches/private.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ use p384::NistP384;
44
use tokio::runtime::Runtime;
55

66
use privacypass::{
7-
PPCipherSuite,
87
auth::{authenticate::TokenChallenge, authorize::Token},
8+
common::private::PrivateCipherSuite,
99
private_tokens::{TokenRequest, TokenResponse, server::Server},
1010
test_utils::{nonce_store::MemoryNonceStore, private_memory_store::MemoryKeyStoreVoprf},
1111
};
1212
use voprf::Ristretto255;
1313

14-
async fn create_private_keypair<CS: PPCipherSuite>(
14+
async fn create_private_keypair<CS: PrivateCipherSuite>(
1515
key_store: MemoryKeyStoreVoprf<CS>,
1616
server: Server<CS>,
1717
) {
1818
let _public_key = server.create_keypair(&key_store).await.unwrap();
1919
}
2020

21-
async fn issue_private_token_response<CS: PPCipherSuite>(
21+
async fn issue_private_token_response<CS: PrivateCipherSuite>(
2222
key_store: MemoryKeyStoreVoprf<CS>,
2323
server: Server<CS>,
2424
token_request: TokenRequest<CS>,
@@ -29,7 +29,7 @@ async fn issue_private_token_response<CS: PPCipherSuite>(
2929
.unwrap()
3030
}
3131

32-
async fn redeem_private_token<Nk: ArrayLength<u8>, CS: PPCipherSuite>(
32+
async fn redeem_private_token<Nk: ArrayLength<u8>, CS: PrivateCipherSuite>(
3333
key_store: MemoryKeyStoreVoprf<CS>,
3434
nonce_store: MemoryNonceStore,
3535
token: Token<Nk>,
@@ -49,7 +49,7 @@ pub fn criterion_private_ristretto255_benchmark(c: &mut Criterion) {
4949
flow::<Ristretto255>(c);
5050
}
5151

52-
pub fn flow<CS: PPCipherSuite>(c: &mut Criterion) {
52+
pub fn flow<CS: PrivateCipherSuite>(c: &mut Criterion) {
5353
// Key pair generation
5454
c.bench_function(
5555
&format!("PRIVATE SERVER ({}): Generate key pair", CS::ID),

src/amortized_tokens/request.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ use typenum::Unsigned;
77
use voprf::{Group, Result, VoprfClient};
88

99
use crate::{
10-
ChallengeDigest, Nonce, PPCipherSuite, TokenInput, TokenType, TruncatedTokenKeyId,
10+
ChallengeDigest, Nonce, TokenInput, TokenType, TruncatedTokenKeyId,
1111
auth::authenticate::TokenChallenge,
1212
common::{
1313
errors::IssueTokenRequestError,
14-
private::{PublicKey, public_key_to_token_key_id},
14+
private::{PrivateCipherSuite, PublicKey, public_key_to_token_key_id},
1515
},
1616
truncate_token_key_id,
1717
};
1818

1919
/// State that is kept between the token requests and token responses.
20-
pub struct TokenState<CS: PPCipherSuite> {
20+
pub struct TokenState<CS: PrivateCipherSuite> {
2121
pub(crate) clients: Vec<VoprfClient<CS>>,
2222
pub(crate) token_inputs: Vec<TokenInput>,
2323
pub(crate) challenge_digest: ChallengeDigest,
2424
pub(crate) public_key: PublicKey<CS>,
2525
}
2626

27-
impl<CS: PPCipherSuite> std::fmt::Debug for TokenState<CS> {
27+
impl<CS: PrivateCipherSuite> std::fmt::Debug for TokenState<CS> {
2828
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2929
f.debug_struct("TokenState")
3030
.field("clients", &self.clients.len())
@@ -43,7 +43,7 @@ impl<CS: PPCipherSuite> std::fmt::Debug for TokenState<CS> {
4343
/// } BlindedElement;
4444
/// ```
4545
#[derive(Debug)]
46-
pub struct BlindedElement<CS: PPCipherSuite> {
46+
pub struct BlindedElement<CS: PrivateCipherSuite> {
4747
pub(crate) _marker: std::marker::PhantomData<CS>,
4848
pub(crate) blinded_element: Vec<u8>,
4949
}
@@ -58,21 +58,21 @@ pub struct BlindedElement<CS: PPCipherSuite> {
5858
/// } AmortizedBatchTokenRequest;
5959
/// ```
6060
#[derive(Debug, TlsDeserialize, TlsSerialize, TlsSize)]
61-
pub struct AmortizedBatchTokenRequest<CS: PPCipherSuite> {
61+
pub struct AmortizedBatchTokenRequest<CS: PrivateCipherSuite> {
6262
pub(crate) token_type: TokenType,
6363
pub(crate) truncated_token_key_id: TruncatedTokenKeyId,
6464
pub(crate) blinded_elements: Vec<BlindedElement<CS>>,
6565
}
6666

67-
impl<CS: PPCipherSuite> AmortizedBatchTokenRequest<CS> {
67+
impl<CS: PrivateCipherSuite> AmortizedBatchTokenRequest<CS> {
6868
/// Returns the number of blinded elements
6969
#[must_use]
7070
pub fn nr(&self) -> usize {
7171
self.blinded_elements.len()
7272
}
7373
}
7474

75-
impl<CS: PPCipherSuite> AmortizedBatchTokenRequest<CS> {
75+
impl<CS: PrivateCipherSuite> AmortizedBatchTokenRequest<CS> {
7676
/// Issue a new token request.
7777
///
7878
/// # Errors
@@ -178,13 +178,13 @@ impl<CS: PPCipherSuite> AmortizedBatchTokenRequest<CS> {
178178
}
179179
}
180180

181-
impl<CS: PPCipherSuite> Size for BlindedElement<CS> {
181+
impl<CS: PrivateCipherSuite> Size for BlindedElement<CS> {
182182
fn tls_serialized_len(&self) -> usize {
183183
<<CS::Group as Group>::ElemLen as Unsigned>::USIZE
184184
}
185185
}
186186

187-
impl<CS: PPCipherSuite> Serialize for BlindedElement<CS> {
187+
impl<CS: PrivateCipherSuite> Serialize for BlindedElement<CS> {
188188
fn tls_serialize<W: std::io::Write>(
189189
&self,
190190
writer: &mut W,
@@ -194,7 +194,7 @@ impl<CS: PPCipherSuite> Serialize for BlindedElement<CS> {
194194
}
195195
}
196196

197-
impl<CS: PPCipherSuite> Deserialize for BlindedElement<CS> {
197+
impl<CS: PrivateCipherSuite> Deserialize for BlindedElement<CS> {
198198
fn tls_deserialize<R: std::io::Read>(
199199
bytes: &mut R,
200200
) -> std::result::Result<Self, tls_codec::Error>

src/amortized_tokens/response.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ use typenum::Unsigned;
55
use voprf::{EvaluationElement, Group, Proof, Result, VoprfClient};
66

77
use crate::{
8-
PPCipherSuite,
98
auth::authorize::Token,
10-
common::errors::{IssueTokenError, SerializationError},
9+
common::{
10+
errors::{IssueTokenError, SerializationError},
11+
private::PrivateCipherSuite,
12+
},
1113
};
1214

1315
use super::{AmortizedToken, TokenState};
@@ -21,7 +23,7 @@ use super::{AmortizedToken, TokenState};
2123
/// ```
2224
2325
#[derive(Debug, PartialEq)]
24-
pub struct EvaluatedElement<CS: PPCipherSuite> {
26+
pub struct EvaluatedElement<CS: PrivateCipherSuite> {
2527
pub(crate) _marker: std::marker::PhantomData<CS>,
2628
pub(crate) evaluated_element: Vec<u8>,
2729
}
@@ -35,13 +37,13 @@ pub struct EvaluatedElement<CS: PPCipherSuite> {
3537
/// } AmortizedBatchTokenResponse;
3638
/// ```
3739
#[derive(Debug)]
38-
pub struct AmortizedBatchTokenResponse<CS: PPCipherSuite> {
40+
pub struct AmortizedBatchTokenResponse<CS: PrivateCipherSuite> {
3941
pub(crate) _marker: std::marker::PhantomData<CS>,
4042
pub(crate) evaluated_elements: Vec<EvaluatedElement<CS>>,
4143
pub(crate) evaluated_proof: Vec<u8>,
4244
}
4345

44-
impl<CS: PPCipherSuite> AmortizedBatchTokenResponse<CS> {
46+
impl<CS: PrivateCipherSuite> AmortizedBatchTokenResponse<CS> {
4547
/// Create a new `TokenResponse` from a byte slice.
4648
///
4749
/// # Errors
@@ -113,13 +115,13 @@ impl<CS: PPCipherSuite> AmortizedBatchTokenResponse<CS> {
113115
}
114116
}
115117

116-
impl<CS: PPCipherSuite> Size for EvaluatedElement<CS> {
118+
impl<CS: PrivateCipherSuite> Size for EvaluatedElement<CS> {
117119
fn tls_serialized_len(&self) -> usize {
118120
<<CS::Group as Group>::ElemLen as Unsigned>::USIZE
119121
}
120122
}
121123

122-
impl<CS: PPCipherSuite> Serialize for EvaluatedElement<CS> {
124+
impl<CS: PrivateCipherSuite> Serialize for EvaluatedElement<CS> {
123125
fn tls_serialize<W: std::io::Write>(
124126
&self,
125127
writer: &mut W,
@@ -129,7 +131,7 @@ impl<CS: PPCipherSuite> Serialize for EvaluatedElement<CS> {
129131
}
130132
}
131133

132-
impl<CS: PPCipherSuite> Deserialize for EvaluatedElement<CS> {
134+
impl<CS: PrivateCipherSuite> Deserialize for EvaluatedElement<CS> {
133135
fn tls_deserialize<R: std::io::Read>(
134136
bytes: &mut R,
135137
) -> std::result::Result<Self, tls_codec::Error>
@@ -145,14 +147,14 @@ impl<CS: PPCipherSuite> Deserialize for EvaluatedElement<CS> {
145147
}
146148
}
147149

148-
impl<CS: PPCipherSuite> Size for AmortizedBatchTokenResponse<CS> {
150+
impl<CS: PrivateCipherSuite> Size for AmortizedBatchTokenResponse<CS> {
149151
fn tls_serialized_len(&self) -> usize {
150152
let len = 2 * <<CS::Group as Group>::ScalarLen as Unsigned>::USIZE;
151153
self.evaluated_elements.tls_serialized_len() + len
152154
}
153155
}
154156

155-
impl<CS: PPCipherSuite> Deserialize for AmortizedBatchTokenResponse<CS> {
157+
impl<CS: PrivateCipherSuite> Deserialize for AmortizedBatchTokenResponse<CS> {
156158
fn tls_deserialize<R: std::io::Read>(
157159
bytes: &mut R,
158160
) -> std::result::Result<Self, tls_codec::Error>
@@ -172,7 +174,7 @@ impl<CS: PPCipherSuite> Deserialize for AmortizedBatchTokenResponse<CS> {
172174
}
173175
}
174176

175-
impl<CS: PPCipherSuite> Serialize for AmortizedBatchTokenResponse<CS> {
177+
impl<CS: PrivateCipherSuite> Serialize for AmortizedBatchTokenResponse<CS> {
176178
fn tls_serialize<W: std::io::Write>(
177179
&self,
178180
writer: &mut W,

src/amortized_tokens/server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use typenum::Unsigned;
77
use voprf::{BlindedElement, Group, Result, VoprfServer, VoprfServerBatchEvaluateFinishResult};
88

99
use crate::{
10-
NonceStore, PPCipherSuite, TokenInput,
10+
NonceStore, TokenInput,
1111
common::{
1212
errors::{CreateKeypairError, IssueTokenResponseError, RedeemTokenError},
13-
private::{PublicKey, public_key_to_token_key_id},
13+
private::{PrivateCipherSuite, PublicKey, public_key_to_token_key_id},
1414
store::PrivateKeyStore,
1515
},
1616
truncate_token_key_id,
@@ -20,11 +20,11 @@ use super::{AmortizedBatchTokenRequest, AmortizedBatchTokenResponse, AmortizedTo
2020

2121
/// Server-side component of the batched token issuance protocol.
2222
#[derive(Default, Debug)]
23-
pub struct Server<CS: PPCipherSuite> {
23+
pub struct Server<CS: PrivateCipherSuite> {
2424
_marker: std::marker::PhantomData<CS>,
2525
}
2626

27-
impl<CS: PPCipherSuite> Server<CS> {
27+
impl<CS: PrivateCipherSuite> Server<CS> {
2828
/// Create a new server. The new server does not contain any key material.
2929
#[must_use]
3030
pub const fn new() -> Self {
@@ -199,7 +199,7 @@ impl<CS: PPCipherSuite> Server<CS> {
199199

200200
#[cfg(test)]
201201
mod tests {
202-
use crate::PPCipherSuite;
202+
use crate::common::private::PrivateCipherSuite;
203203
use p384::NistP384;
204204
use voprf::{Group, Ristretto255};
205205

@@ -215,7 +215,7 @@ mod tests {
215215
}
216216

217217
#[cfg(test)]
218-
fn key_serialization_cs<CS: PPCipherSuite>(pk: <CS::Group as Group>::Elem)
218+
fn key_serialization_cs<CS: PrivateCipherSuite>(pk: <CS::Group as Group>::Elem)
219219
where
220220
<<CS as voprf::CipherSuite>::Group as voprf::Group>::Elem: std::cmp::PartialEq,
221221
<<CS as voprf::CipherSuite>::Group as voprf::Group>::Elem: std::fmt::Debug,

src/common/private.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
//! Types that used by private tokens
22
33
use sha2::{Digest, Sha256};
4+
use std::fmt::Debug;
45
use voprf::{CipherSuite, Error, Group};
56

6-
use crate::{PPCipherSuite, TokenKeyId, TruncatedTokenKeyId, truncate_token_key_id};
7+
use crate::{TokenKeyId, TokenType, TruncatedTokenKeyId, truncate_token_key_id};
8+
9+
/// Trait for a cipher suite that can be used with the Privacy Pass protocol.
10+
pub trait PrivateCipherSuite:
11+
CipherSuite<Group: Group<Elem: Send + Sync, Scalar: Send + Sync>>
12+
+ PartialEq
13+
+ Debug
14+
+ Clone
15+
+ Send
16+
+ Sync
17+
{
18+
/// Returns the token type for the cipher suite.
19+
fn token_type() -> TokenType {
20+
match Self::ID {
21+
"P384-SHA384" => TokenType::PrivateP384,
22+
"ristretto255-SHA512" => TokenType::PrivateRistretto255,
23+
_ => panic!("Unsupported token type"),
24+
}
25+
}
26+
}
27+
28+
impl<C> PrivateCipherSuite for C where
29+
C: CipherSuite<Group: Group<Elem: Send + Sync, Scalar: Send + Sync>>
30+
+ PartialEq
31+
+ Debug
32+
+ Clone
33+
+ Send
34+
+ Sync
35+
{
36+
}
737

838
/// Public key alias
939
pub type PublicKey<CS> = <<CS as CipherSuite>::Group as Group>::Elem;
1040

1141
/// Convert a public key to a token key ID.
12-
pub fn public_key_to_truncated_token_key_id<CS: PPCipherSuite>(
42+
pub fn public_key_to_truncated_token_key_id<CS: PrivateCipherSuite>(
1343
public_key: &<CS::Group as Group>::Elem,
1444
) -> TruncatedTokenKeyId {
1545
truncate_token_key_id(&public_key_to_token_key_id::<CS>(public_key))
1646
}
1747

18-
pub(crate) fn public_key_to_token_key_id<CS: PPCipherSuite>(
48+
pub(crate) fn public_key_to_token_key_id<CS: PrivateCipherSuite>(
1949
public_key: &<CS::Group as Group>::Elem,
2050
) -> TokenKeyId {
2151
let public_key = serialize_public_key::<CS>(*public_key);
@@ -25,7 +55,9 @@ pub(crate) fn public_key_to_token_key_id<CS: PPCipherSuite>(
2555

2656
/// Serializes a public key.
2757
#[must_use]
28-
pub fn serialize_public_key<CS: PPCipherSuite>(public_key: <CS::Group as Group>::Elem) -> Vec<u8> {
58+
pub fn serialize_public_key<CS: PrivateCipherSuite>(
59+
public_key: <CS::Group as Group>::Elem,
60+
) -> Vec<u8> {
2961
<CS::Group as Group>::serialize_elem(public_key).to_vec()
3062
}
3163

@@ -34,6 +66,8 @@ pub fn serialize_public_key<CS: PPCipherSuite>(public_key: <CS::Group as Group>:
3466
/// # Errors
3567
///
3668
/// This function will return an error if the slice is not a valid public key.
37-
pub fn deserialize_public_key<CS: PPCipherSuite>(slice: &[u8]) -> Result<PublicKey<CS>, Error> {
69+
pub fn deserialize_public_key<CS: PrivateCipherSuite>(
70+
slice: &[u8],
71+
) -> Result<PublicKey<CS>, Error> {
3872
<CS::Group as Group>::deserialize_elem(slice)
3973
}

0 commit comments

Comments
 (0)