diff --git a/src/distributed.rs b/src/distributed.rs index 070e8c7..1120617 100644 --- a/src/distributed.rs +++ b/src/distributed.rs @@ -9,7 +9,7 @@ use serde::de::{Error, Visitor}; /// GLOBAL KEY BLINDING #[derive(Copy, Clone, Debug, From)] -pub struct BlindingFactor(ScalarNonZero); +pub struct BlindingFactor(pub(crate) ScalarNonZero); impl BlindingFactor { pub fn random(rng: &mut R) -> Self { let scalar = ScalarNonZero::random(rng); @@ -40,7 +40,7 @@ impl BlindingFactor { } #[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] -pub struct BlindedGlobalSecretKey(ScalarNonZero); +pub struct BlindedGlobalSecretKey(pub(crate) ScalarNonZero); impl Serialize for BlindedGlobalSecretKey { fn serialize(&self, serializer: S) -> Result where @@ -87,7 +87,7 @@ pub fn make_blinded_global_secret_key( } #[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] -pub struct SessionKeyShare(ScalarNonZero); +pub struct SessionKeyShare(pub(crate) ScalarNonZero); impl Serialize for SessionKeyShare { fn serialize(&self, serializer: S) -> Result where @@ -147,7 +147,7 @@ impl PEPSystem { } pub fn session_key_share(&self, context: &EncryptionContext) -> SessionKeyShare { let k = make_rekey_factor(&self.rekeying_secret, &context); - make_session_key_share(&k, &self.blinding_factor) + make_session_key_share(&k.0, &self.blinding_factor) } pub fn rekey_info( &self, diff --git a/src/high_level.rs b/src/high_level.rs index 2b50d64..cc1c450 100644 --- a/src/high_level.rs +++ b/src/high_level.rs @@ -7,7 +7,7 @@ use rand_core::{CryptoRng, RngCore}; use serde::{Deserialize, Serialize}; /// GLOBAL KEYS -#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] +#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From, Serialize, Deserialize)] pub struct GlobalPublicKey(pub GroupElement); #[derive(Copy, Clone, Debug, From)] pub struct GlobalSecretKey(pub(crate) ScalarNonZero); @@ -38,8 +38,8 @@ impl EncryptionSecret { } /// SESSION KEYS -#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] -pub struct SessionPublicKey(GroupElement); +#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From, Serialize, Deserialize)] +pub struct SessionPublicKey(pub GroupElement); #[derive(Copy, Clone, Debug, From)] pub struct SessionSecretKey(pub(crate) ScalarNonZero); /// Generate a subkey from a global secret key, a context, and an encryption secret @@ -49,7 +49,7 @@ pub fn make_session_keys( encryption_secret: &EncryptionSecret, ) -> (SessionPublicKey, SessionSecretKey) { let k = make_rekey_factor(encryption_secret, context); - let sk = *k * global.0; + let sk = k.0 * global.0; let pk = sk * G; (SessionPublicKey(pk), SessionSecretKey(sk)) } @@ -57,11 +57,11 @@ pub fn make_session_keys( /// PSEUDONYMS AND DATA #[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] pub struct Pseudonym { - value: GroupElement, + pub(crate) value: GroupElement, } #[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] pub struct DataPoint { - value: GroupElement, + pub(crate) value: GroupElement, } impl Pseudonym { pub fn from_point(value: GroupElement) -> Self { @@ -168,7 +168,7 @@ pub fn decrypt_data(data: &EncryptedDataPoint, sk: &SessionSecretKey) -> DataPoi DataPoint::from_point(decrypt(&data, &sk.0)) } -#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] +#[derive(Copy, Clone, Eq, PartialEq, Debug, From)] pub struct RerandomizeFactor(ScalarNonZero); #[cfg(not(feature = "elgamal2"))] /// Rerandomize the ciphertext of an encrypted pseudonym @@ -247,10 +247,10 @@ pub enum AudienceType { Unknown = 0x00, } -#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] -pub struct ReshuffleFactor(ScalarNonZero); -#[derive(Copy, Clone, Eq, PartialEq, Debug, Deref, From)] -pub struct RekeyFactor(ScalarNonZero); +#[derive(Copy, Clone, Eq, PartialEq, Debug, From)] +pub struct ReshuffleFactor(pub(crate) ScalarNonZero); +#[derive(Copy, Clone, Eq, PartialEq, Debug, From)] +pub struct RekeyFactor(pub(crate) ScalarNonZero); #[derive(Eq, PartialEq, Clone, Copy, Debug, From)] pub struct Reshuffle2Factors { @@ -344,10 +344,10 @@ pub fn pseudonymize( ) -> EncryptedPseudonym { EncryptedPseudonym::from(rsk2( &p.value, - &pseudonymization_info.s.from, - &pseudonymization_info.s.to, - &pseudonymization_info.k.from, - &pseudonymization_info.k.to, + &pseudonymization_info.s.from.0, + &pseudonymization_info.s.to.0, + &pseudonymization_info.k.from.0, + &pseudonymization_info.k.to.0, )) } @@ -359,20 +359,20 @@ pub fn pseudonymize_from_global( ) -> EncryptedPseudonym { EncryptedPseudonym::from(rsk2( &p.value, - &reshuffle_factors.from, - &reshuffle_factors.to, + &reshuffle_factors.from.0, + &reshuffle_factors.to.0, &ScalarNonZero::one(), - &rekey_to, + &rekey_to.0, )) } /// Rekey an encrypted data point, encrypted with one session key, to be decrypted by another session key pub fn rekey(p: &EncryptedDataPoint, rekey_info: &RekeyInfo) -> EncryptedDataPoint { - EncryptedDataPoint::from(rekey2(&p.value, &rekey_info.from, &rekey_info.to)) + EncryptedDataPoint::from(rekey2(&p.value, &rekey_info.from.0, &rekey_info.to.0)) } /// Rekey an encrypted data point, encrypted for a global key, to be decrypted by a session key pub fn rekey_from_global(p: &EncryptedDataPoint, rekey_to: RekeyFactor) -> EncryptedDataPoint { - EncryptedDataPoint::from(crate::primitives::rekey(&p.value, &rekey_to)) + EncryptedDataPoint::from(crate::primitives::rekey(&p.value, &rekey_to.0)) } diff --git a/src/high_level_proved.rs b/src/high_level_proved.rs index 29217f9..0027db2 100644 --- a/src/high_level_proved.rs +++ b/src/high_level_proved.rs @@ -20,7 +20,7 @@ impl PseudonymizationContextVerifiers { rng: &mut R, ) -> (Self, PseudonymizationFactorVerifiersProof) { let factor = make_pseudonymisation_factor(secret, context); - let (verifiers, proof) = PseudonymizationFactorVerifiers::new(&*factor, rng); + let (verifiers, proof) = PseudonymizationFactorVerifiers::new(&factor.0, rng); (PseudonymizationContextVerifiers(verifiers), proof) } } @@ -31,7 +31,7 @@ impl EncryptionContextVerifiers { rng: &mut R, ) -> (Self, RekeyFactorVerifiersProof) { let factor = make_rekey_factor(secret, context); - let (verifiers, proof) = RekeyFactorVerifiers::new(&*factor, rng); + let (verifiers, proof) = RekeyFactorVerifiers::new(&factor.0, rng); (EncryptionContextVerifiers(verifiers), proof) } } @@ -46,13 +46,13 @@ pub struct PseudonymizationInfoProof { pub struct RekeyInfoProof(pub Rekey2FactorsProof); impl PseudonymizationInfoProof { pub fn new(factors: &PseudonymizationInfo, rng: &mut R) -> Self { - let reshuffle_proof = Reshuffle2FactorsProof::new(&factors.s.from, &factors.s.to, rng); - let rekey_proof = Rekey2FactorsProof::new(&factors.k.from, &factors.k.to, rng); + let reshuffle_proof = Reshuffle2FactorsProof::new(&factors.s.from.0, &factors.s.to.0, rng); + let rekey_proof = Rekey2FactorsProof::new(&factors.k.from.0, &factors.k.to.0, rng); let rsk_proof = RSK2FactorsProof::new( - &factors.s.from, - &factors.s.to, - &factors.k.from, - &factors.k.to, + &factors.s.from.0, + &factors.s.to.0, + &factors.k.from.0, + &factors.k.to.0, rng, ); PseudonymizationInfoProof { @@ -81,7 +81,7 @@ impl PseudonymizationInfoProof { } impl RekeyInfoProof { pub fn new(factors: &RekeyInfo, rng: &mut R) -> Self { - let rekey_proof = Rekey2FactorsProof::new(&factors.from, &factors.to, rng); + let rekey_proof = Rekey2FactorsProof::new(&factors.from.0, &factors.to.0, rng); RekeyInfoProof(rekey_proof) } #[must_use] @@ -141,10 +141,10 @@ pub fn proved_pseudonymize( ) -> ProvedEncryptedPseudonym { ProvedEncryptedPseudonym::new(prove_rsk2( &p, - &pseudonymization_info.s.from, - &pseudonymization_info.s.to, - &pseudonymization_info.k.from, - &pseudonymization_info.k.to, + &pseudonymization_info.s.from.0, + &pseudonymization_info.s.to.0, + &pseudonymization_info.k.from.0, + &pseudonymization_info.k.to.0, rng, )) } @@ -155,7 +155,7 @@ pub fn proved_rekey( rekey_info: &RekeyInfo, rng: &mut R, ) -> ProvedEncryptedDataPoint { - ProvedEncryptedDataPoint::new(prove_rekey2(&p, &rekey_info.from, &rekey_info.to, rng)) + ProvedEncryptedDataPoint::new(prove_rekey2(&p, &rekey_info.from.0, &rekey_info.to.0, rng)) } #[must_use] diff --git a/src/wasm/arithmetic.rs b/src/wasm/arithmetic.rs index 278a298..0f561bc 100644 --- a/src/wasm/arithmetic.rs +++ b/src/wasm/arithmetic.rs @@ -33,12 +33,12 @@ impl WASMGroupElement { } #[wasm_bindgen(js_name = toHex)] pub fn to_hex(&self) -> String { - self.0.encode_to_hex() + self.0.encode_hex() } #[wasm_bindgen(js_name = toBase64)] pub fn to_base_64(&self) -> String { - self.0.encode_to_base64() + self.0.encode_base64() } #[wasm_bindgen(js_name = fromBase64)] pub fn from_base_64(s: &str) -> Option { diff --git a/src/wasm/distributed.rs b/src/wasm/distributed.rs index b7f2cfe..0c4dae3 100644 --- a/src/wasm/distributed.rs +++ b/src/wasm/distributed.rs @@ -75,8 +75,7 @@ pub fn wasm_make_blinded_global_secret_key( make_blinded_global_secret_key( &GlobalSecretKey::from(ScalarNonZero::from(global_secret_key.0)), &bs, - ) - .0, + ).unwrap().0, )) } @@ -93,8 +92,8 @@ impl WASMPEPSystem { blinding_factor: &WASMBlindingFactor, ) -> Self { Self(PEPSystem::new( - PseudonymizationSecret(pseudonymisation_secret.to_string()), - EncryptionSecret(rekeying_secret.to_string()), + PseudonymizationSecret::from(pseudonymisation_secret.as_bytes().into()), + EncryptionSecret::from(rekeying_secret.as_bytes().into()), BlindingFactor::from(ScalarNonZero::from(blinding_factor.0)), )) } @@ -102,7 +101,7 @@ impl WASMPEPSystem { #[wasm_bindgen(js_name = sessionKeyShare)] pub fn wasm_session_key_share(&self, context: &str) -> WASMSessionKeyShare { WASMSessionKeyShare::from(WASMScalarNonZero::from( - self.session_key_share(&EncryptionContext(context.to_string())) + self.session_key_share(&EncryptionContext::from(context.to_string())) .0, )) } @@ -110,8 +109,8 @@ impl WASMPEPSystem { #[wasm_bindgen(js_name = rekeyInfo)] pub fn wasm_rekey_info(&self, from_enc: &str, to_enc: &str) -> WASMRekeyInfo { WASMRekeyInfo::from(self.rekey_info( - &EncryptionContext(from_enc.to_string()), - &EncryptionContext(to_enc.to_string()), + &EncryptionContext::from(from_enc.to_string()), + &EncryptionContext::from(to_enc.to_string()), )) } @@ -124,10 +123,10 @@ impl WASMPEPSystem { to_enc: &str, ) -> WASMPseudonymizationInfo { WASMPseudonymizationInfo::from(self.pseudonymization_info( - &PseudonymizationContext(from_pseudo.to_string()), - &PseudonymizationContext(to_pseudo.to_string()), - &EncryptionContext(from_enc.to_string()), - &EncryptionContext(to_enc.to_string()), + &PseudonymizationContext::from(from_pseudo.to_string()), + &PseudonymizationContext::from(to_pseudo.to_string()), + &EncryptionContext::from(from_enc.to_string()), + &EncryptionContext::from(to_enc.to_string()), )) } diff --git a/src/wasm/high_level.rs b/src/wasm/high_level.rs index 8e04b07..1fbf454 100644 --- a/src/wasm/high_level.rs +++ b/src/wasm/high_level.rs @@ -117,8 +117,8 @@ pub fn wasm_make_session_keys( ) -> WASMSessionKeyPair { let (public, secret) = make_session_keys( &GlobalSecretKey(***global), - &EncryptionContext(context.to_string()), - &EncryptionSecret(encryption_secret.to_string()), + &EncryptionContext::from(context.to_string()), + &EncryptionSecret::from(encryption_secret.into()), ); WASMSessionKeyPair { public: WASMSessionPublicKey::from(WASMGroupElement::from(public.0)), @@ -153,8 +153,7 @@ pub fn wasm_decrypt_pseudonym( decrypt_pseudonym( &EncryptedPseudonym::from(ElGamal::from(p.value)), &SessionSecretKey::from(ScalarNonZero::from(sk.0)), - ) - .value, + ).value, )) } @@ -185,8 +184,7 @@ pub fn wasm_decrypt_data( decrypt_data( &EncryptedDataPoint::from(ElGamal::from(data.value)), &SessionSecretKey::from(ScalarNonZero::from(sk.0)), - ) - .value, + ).value, )) } @@ -287,12 +285,12 @@ impl WASMPseudonymizationInfo { encryption_secret: &str, ) -> Self { let x = PseudonymizationInfo::new( - &PseudonymizationContext(from_pseudo_context.to_string()), - &PseudonymizationContext(to_pseudo_context.to_string()), - &EncryptionContext(from_enc_context.to_string()), - &EncryptionContext(to_enc_context.to_string()), - &PseudonymizationSecret(pseudonymization_secret.to_string()), - &EncryptionSecret(encryption_secret.to_string()), + &PseudonymizationContext::from(from_pseudo_context.to_string()), + &PseudonymizationContext::from(to_pseudo_context.to_string()), + &EncryptionContext::from(from_enc_context.to_string()), + &EncryptionContext::from(to_enc_context.to_string()), + &PseudonymizationSecret::from(pseudonymization_secret.as_bytes().to_vec()), + &EncryptionSecret::from(encryption_secret.as_bytes().to_vec()), ); let k = WASMRekey2Factors { from: WASMRekeyFactor(WASMScalarNonZero::from(x.k.from.0)), @@ -319,9 +317,9 @@ impl WASMRekeyInfo { #[wasm_bindgen(constructor)] pub fn new(from_enc_context: &str, to_enc_context: &str, encryption_secret: &str) -> Self { let x = RekeyInfo::new( - &EncryptionContext(from_enc_context.to_string()), - &EncryptionContext(to_enc_context.to_string()), - &EncryptionSecret(encryption_secret.to_string()), + &EncryptionContext::from(from_enc_context.to_string()), + &EncryptionContext::from(to_enc_context.to_string()), + &EncryptionSecret::from(encryption_secret.as_bytes().into()), ); let k = WASMRekey2Factors { from: WASMRekeyFactor(WASMScalarNonZero::from(x.from.0)),