Skip to content

Commit c01f1bc

Browse files
committed
Uses conditional compilation to make sure postprocessing is only available in tests
See rust-lang/rust#64010
1 parent 3954cd2 commit c01f1bc

File tree

3 files changed

+62
-71
lines changed

3 files changed

+62
-71
lines changed

src/opaque.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -577,37 +577,25 @@ impl<CS: CipherSuite> ClientRegistration<CS> {
577577
&Vec::new(),
578578
password,
579579
blinding_factor_rng,
580-
)
581-
}
582-
583-
/// Same as ClientRegistration::start, but also accepts a username and server name as input
584-
pub fn start_with_user_and_server_name<R: RngCore + CryptoRng>(
585-
user_name: &[u8],
586-
server_name: &[u8],
587-
password: &[u8],
588-
blinding_factor_rng: &mut R,
589-
) -> Result<(RegisterFirstMessage<CS::Group>, Self), ProtocolError> {
590-
Self::start_with_user_and_server_name_and_postprocessing(
591-
user_name,
592-
server_name,
593-
password,
594-
blinding_factor_rng,
580+
#[cfg(test)]
595581
std::convert::identity,
596582
)
597583
}
598584

599-
/// Same as ClientRegistration::start, but also accepts a username and server name as input as well as
600-
/// an optional postprocessing function for the blinding factor
601-
pub fn start_with_user_and_server_name_and_postprocessing<R: RngCore + CryptoRng>(
585+
/// Same as ClientRegistration::start, but also accepts a username and
586+
/// server name as input
587+
/// as well as an optional postprocessing function for the blinding factor(used in tests)
588+
pub fn start_with_user_and_server_name<R: RngCore + CryptoRng>(
602589
user_name: &[u8],
603590
server_name: &[u8],
604591
password: &[u8],
605592
blinding_factor_rng: &mut R,
606-
postprocess: fn(<CS::Group as Group>::Scalar) -> <CS::Group as Group>::Scalar,
593+
#[cfg(test)] postprocess: fn(<CS::Group as Group>::Scalar) -> <CS::Group as Group>::Scalar,
607594
) -> Result<(RegisterFirstMessage<CS::Group>, Self), ProtocolError> {
608-
let (token, alpha) = oprf::blind_with_postprocessing::<R, CS::Group>(
595+
let (token, alpha) = oprf::blind::<R, CS::Group>(
609596
&password,
610597
blinding_factor_rng,
598+
#[cfg(test)]
611599
postprocess,
612600
)?;
613601

@@ -1037,35 +1025,31 @@ impl<CS: CipherSuite> ClientLogin<CS> {
10371025
password: &[u8],
10381026
rng: &mut R,
10391027
) -> Result<(LoginFirstMessage<CS>, Self), ProtocolError> {
1040-
Self::start_with_user_and_server_name(&Vec::new(), &Vec::new(), password, rng)
1041-
}
1042-
1043-
/// Same as start, but allows the user to supply a username and server name
1044-
pub fn start_with_user_and_server_name<R: RngCore + CryptoRng>(
1045-
user_name: &[u8],
1046-
server_name: &[u8],
1047-
password: &[u8],
1048-
rng: &mut R,
1049-
) -> Result<(LoginFirstMessage<CS>, Self), ProtocolError> {
1050-
Self::start_with_user_and_server_name_and_postprocessing(
1051-
user_name,
1052-
server_name,
1028+
Self::start_with_user_and_server_name(
1029+
&Vec::new(),
1030+
&Vec::new(),
10531031
password,
10541032
rng,
1033+
#[cfg(test)]
10551034
std::convert::identity,
10561035
)
10571036
}
10581037

1059-
/// Same as start, but allows the user to supply a username and server name and postprocessing function
1060-
pub fn start_with_user_and_server_name_and_postprocessing<R: RngCore + CryptoRng>(
1038+
/// Same as start, but allows the user to supply a username and server name
1039+
/// and, in tests, a postprocessing function
1040+
pub fn start_with_user_and_server_name<R: RngCore + CryptoRng>(
10611041
user_name: &[u8],
10621042
server_name: &[u8],
10631043
password: &[u8],
10641044
rng: &mut R,
1065-
postprocess: fn(<CS::Group as Group>::Scalar) -> <CS::Group as Group>::Scalar,
1045+
#[cfg(test)] postprocess: fn(<CS::Group as Group>::Scalar) -> <CS::Group as Group>::Scalar,
10661046
) -> Result<(LoginFirstMessage<CS>, Self), ProtocolError> {
1067-
let (token, alpha) =
1068-
oprf::blind_with_postprocessing::<R, CS::Group>(&password, rng, postprocess)?;
1047+
let (token, alpha) = oprf::blind::<R, CS::Group>(
1048+
&password,
1049+
rng,
1050+
#[cfg(test)]
1051+
postprocess,
1052+
)?;
10691053

10701054
let (ke1_state, ke1_message) = CS::KeyExchange::generate_ke1(alpha.to_arr().to_vec(), rng)?;
10711055

src/oprf.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ static STR_VOPRF: &[u8] = b"VOPRF05";
2323
/// message is sent from the client (who holds the input) to the server (who holds the OPRF key).
2424
/// The client can also pass in an optional "pepper" string to be mixed in with the input through
2525
/// an HKDF computation.
26-
pub(crate) fn blind_with_postprocessing<R: RngCore + CryptoRng, G: GroupWithMapToCurve>(
26+
pub(crate) fn blind<R: RngCore + CryptoRng, G: GroupWithMapToCurve>(
2727
input: &[u8],
2828
blinding_factor_rng: &mut R,
29-
postprocess: fn(G::Scalar) -> G::Scalar,
29+
#[cfg(test)] postprocess: fn(G::Scalar) -> G::Scalar,
3030
) -> Result<(Token<G>, G), InternalPakeError> {
3131
let mapped_point = G::map_to_curve(input, Some(STR_VOPRF)); // TODO: add contextString from RFC
3232
let blinding_factor = G::random_scalar(blinding_factor_rng);
33+
#[cfg(test)]
3334
let blind = postprocess(blinding_factor);
35+
#[cfg(not(test))]
36+
let blind = blinding_factor;
37+
3438
let blind_token = mapped_point * &blind;
3539
Ok((
3640
Token {
@@ -60,8 +64,12 @@ pub(crate) fn unblind_and_finalize<G: Group, H: Hash>(
6064
Ok(prk)
6165
}
6266

63-
// Benchmarking shims
67+
////////////////////////
68+
// Benchmarking shims //
69+
////////////////////////
70+
6471
#[cfg(feature = "bench")]
72+
#[doc(hidden)]
6573
#[inline]
6674
pub fn blind_shim<R: RngCore + CryptoRng, G: GroupWithMapToCurve>(
6775
input: &[u8],
@@ -71,12 +79,14 @@ pub fn blind_shim<R: RngCore + CryptoRng, G: GroupWithMapToCurve>(
7179
}
7280

7381
#[cfg(feature = "bench")]
82+
#[doc(hidden)]
7483
#[inline]
7584
pub fn evaluate_shim<G: Group>(point: G, oprf_key: &G::Scalar) -> Result<G, InternalPakeError> {
7685
evaluate(point, oprf_key)
7786
}
7887

7988
#[cfg(feature = "bench")]
89+
#[doc(hidden)]
8090
#[inline]
8191
pub fn unblind_and_finalize_shim<G: Group, H: Hash>(
8292
token: &Token<G>,
@@ -85,8 +95,10 @@ pub fn unblind_and_finalize_shim<G: Group, H: Hash>(
8595
unblind_and_finalize::<G, H>(token, point)
8696
}
8797

88-
// Tests
89-
// =====
98+
///////////
99+
// Tests //
100+
// ===== //
101+
///////////
90102

91103
#[cfg(test)]
92104
mod tests {
@@ -117,11 +129,8 @@ mod tests {
117129
fn oprf_retrieval() -> Result<(), InternalPakeError> {
118130
let input = b"hunter2";
119131
let mut rng = OsRng;
120-
let (token, alpha) = blind_with_postprocessing::<_, RistrettoPoint>(
121-
&input[..],
122-
&mut rng,
123-
std::convert::identity,
124-
)?;
132+
let (token, alpha) =
133+
blind::<_, RistrettoPoint>(&input[..], &mut rng, std::convert::identity)?;
125134
let oprf_key_bytes = arr![
126135
u8; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
127136
24, 25, 26, 27, 28, 29, 30, 31, 32,
@@ -139,12 +148,8 @@ mod tests {
139148
let mut rng = OsRng;
140149
let mut input = vec![0u8; 64];
141150
rng.fill_bytes(&mut input);
142-
let (token, alpha) = blind_with_postprocessing::<_, RistrettoPoint>(
143-
&input,
144-
&mut rng,
145-
std::convert::identity,
146-
)
147-
.unwrap();
151+
let (token, alpha) =
152+
blind::<_, RistrettoPoint>(&input, &mut rng, std::convert::identity).unwrap();
148153
let res = unblind_and_finalize::<RistrettoPoint, sha2::Sha256>(&token, alpha).unwrap();
149154

150155
let (hashed_input, _) = Hkdf::<Sha512>::extract(Some(STR_VOPRF), &input);

src/tests/opaque_ke_test.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ where
254254
id_s,
255255
password,
256256
&mut blinding_factor_registration_rng,
257+
std::convert::identity,
257258
)
258259
.unwrap();
259260
let r1_bytes = r1.serialize().to_vec();
@@ -291,6 +292,7 @@ where
291292
id_s,
292293
password,
293294
&mut client_login_start_rng,
295+
std::convert::identity,
294296
)
295297
.unwrap();
296298
let l1_bytes = l1.serialize().to_vec();
@@ -362,14 +364,15 @@ fn postprocess_blinding_factor<G: Group>(_: G::Scalar) -> G::Scalar {
362364
fn test_r1() -> Result<(), PakeError> {
363365
let parameters = populate_test_vectors(&serde_json::from_str(TEST_VECTOR).unwrap());
364366
let mut rng = OsRng;
365-
let (r1, client_registration) = ClientRegistration::<X255193dhNoSlowHash>::start_with_user_and_server_name_and_postprocessing(
366-
&parameters.id_u,
367-
&parameters.id_s,
368-
&parameters.password,
369-
&mut rng,
370-
postprocess_blinding_factor::<<X255193dhNoSlowHash as CipherSuite>::Group>,
371-
)
372-
.unwrap();
367+
let (r1, client_registration) =
368+
ClientRegistration::<X255193dhNoSlowHash>::start_with_user_and_server_name(
369+
&parameters.id_u,
370+
&parameters.id_s,
371+
&parameters.password,
372+
&mut rng,
373+
postprocess_blinding_factor::<<X255193dhNoSlowHash as CipherSuite>::Group>,
374+
)
375+
.unwrap();
373376
assert_eq!(hex::encode(&parameters.r1), hex::encode(r1.serialize()));
374377
assert_eq!(
375378
hex::encode(&parameters.client_registration_state),
@@ -452,15 +455,14 @@ fn test_l1() -> Result<(), PakeError> {
452455
]
453456
.concat();
454457
let mut client_login_start_rng = CycleRng::new(client_login_start);
455-
let (l1, client_login) =
456-
ClientLogin::<X255193dhNoSlowHash>::start_with_user_and_server_name_and_postprocessing(
457-
&parameters.id_u,
458-
&parameters.id_s,
459-
&parameters.password,
460-
&mut client_login_start_rng,
461-
postprocess_blinding_factor::<<X255193dhNoSlowHash as CipherSuite>::Group>,
462-
)
463-
.unwrap();
458+
let (l1, client_login) = ClientLogin::<X255193dhNoSlowHash>::start_with_user_and_server_name(
459+
&parameters.id_u,
460+
&parameters.id_s,
461+
&parameters.password,
462+
&mut client_login_start_rng,
463+
postprocess_blinding_factor::<<X255193dhNoSlowHash as CipherSuite>::Group>,
464+
)
465+
.unwrap();
464466
assert_eq!(hex::encode(&parameters.l1), hex::encode(l1.serialize()));
465467
assert_eq!(
466468
hex::encode(&parameters.client_login_state),

0 commit comments

Comments
 (0)