Skip to content

Commit

Permalink
Basic serde support for non-secret types
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Sep 1, 2021
1 parent 849f468 commit 51e56c5
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 0 deletions.
4 changes: 4 additions & 0 deletions umbral-pre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ sha2 = { version = "0.9", default-features = false }
chacha20poly1305 = { version = "0.8", features = ["xchacha20poly1305"] }
hkdf = { version = "0.11", default-features = false }
hex = { version = "0.4", default-features = false }
serde = "1"
base64 = "0.13"

# These packages are among the dependencies of the packages above.
# Their versions should be updated when the main packages above are updated.
Expand All @@ -32,6 +34,8 @@ zeroize = "1.3"

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
serde_json = "1"
rmp-serde = "0.15"

[features]
default = ["default-rng"]
Expand Down
31 changes: 31 additions & 0 deletions umbral-pre/src/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::fmt;
use generic_array::sequence::Concat;
use generic_array::GenericArray;
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use typenum::op;

use crate::capsule_frag::CapsuleFrag;
Expand All @@ -12,6 +13,7 @@ use crate::hashing_ds::{hash_capsule_points, hash_to_polynomial_arg, hash_to_sha
use crate::keys::{PublicKey, SecretKey};
use crate::params::Parameters;
use crate::secret_box::SecretBox;
use crate::serde::{serde_deserialize, serde_serialize};
use crate::traits::{
fmt_public, ConstructionError, DeserializableFromArray, HasTypeName, RepresentableAsArray,
SerializableToArray,
Expand Down Expand Up @@ -84,6 +86,24 @@ impl DeserializableFromArray for Capsule {
}
}

impl Serialize for Capsule {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl<'de> Deserialize<'de> for Capsule {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer)
}
}

impl HasTypeName for Capsule {
fn type_name() -> &'static str {
"Capsule"
Expand Down Expand Up @@ -243,6 +263,7 @@ mod tests {
use rand_core::OsRng;

use super::{Capsule, OpenReencryptedError};
use crate::serde::tests::{check_deserialization, check_serialization};
use crate::{
encrypt, generate_kfrags, reencrypt, DeserializableFromArray, SecretKey,
SerializableToArray, Signer,
Expand Down Expand Up @@ -324,4 +345,14 @@ mod tests {
Err(OpenReencryptedError::ValidationFailed)
);
}

#[test]
fn test_serde_serialization() {
let delegating_sk = SecretKey::random();
let delegating_pk = delegating_sk.public_key();
let (capsule, _key_seed) = Capsule::from_public_key(&mut OsRng, &delegating_pk);

check_serialization(&capsule);
check_deserialization(&capsule);
}
}
44 changes: 44 additions & 0 deletions umbral-pre/src/capsule_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use core::fmt;
use generic_array::sequence::Concat;
use generic_array::GenericArray;
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use typenum::op;

use crate::capsule::Capsule;
use crate::curve::{CurvePoint, CurveScalar};
use crate::hashing_ds::{hash_to_cfrag_verification, kfrag_signature_message};
use crate::key_frag::{KeyFrag, KeyFragID};
use crate::keys::{PublicKey, Signature};
use crate::serde::{serde_deserialize, serde_serialize};
use crate::traits::{
fmt_public, ConstructionError, DeserializableFromArray, DeserializationError, HasTypeName,
RepresentableAsArray, SerializableToArray,
Expand Down Expand Up @@ -154,6 +156,24 @@ impl DeserializableFromArray for CapsuleFrag {
}
}

impl Serialize for CapsuleFrag {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl<'de> Deserialize<'de> for CapsuleFrag {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer)
}
}

impl HasTypeName for CapsuleFrag {
fn type_name() -> &'static str {
"CapsuleFrag"
Expand Down Expand Up @@ -289,6 +309,15 @@ impl SerializableToArray for VerifiedCapsuleFrag {
}
}

impl Serialize for VerifiedCapsuleFrag {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl HasTypeName for VerifiedCapsuleFrag {
fn type_name() -> &'static str {
"VerifiedCapsuleFrag"
Expand Down Expand Up @@ -329,6 +358,7 @@ mod tests {
use alloc::vec::Vec;

use super::{CapsuleFrag, VerifiedCapsuleFrag};
use crate::serde::tests::{check_deserialization, check_serialization};
use crate::{
encrypt, generate_kfrags, reencrypt, Capsule, DeserializableFromArray, PublicKey,
SecretKey, SerializableToArray, Signer,
Expand Down Expand Up @@ -388,4 +418,18 @@ mod tests {
assert_eq!(verified_cfrag_back, verified_cfrag);
}
}

#[test]
fn test_serde_serialization() {
let (_delegating_pk, _receiving_pk, _verifying_pk, _capsule, verified_cfrags) =
prepare_cfrags();

let vcfrag = verified_cfrags[0].clone();
let cfrag = CapsuleFrag::from_array(&vcfrag.to_array()).unwrap();

check_serialization(&vcfrag);

check_serialization(&cfrag);
check_deserialization(&cfrag);
}
}
44 changes: 44 additions & 0 deletions umbral-pre/src/key_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use core::fmt;
use generic_array::sequence::Concat;
use generic_array::GenericArray;
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use typenum::{op, U32};

use crate::curve::{CurvePoint, CurveScalar};
use crate::hashing_ds::{hash_to_polynomial_arg, hash_to_shared_secret, kfrag_signature_message};
use crate::keys::{PublicKey, SecretKey, Signature, Signer};
use crate::params::Parameters;
use crate::serde::{serde_deserialize, serde_serialize};
use crate::traits::{
fmt_public, ConstructionError, DeserializableFromArray, DeserializationError, HasTypeName,
RepresentableAsArray, SerializableToArray,
Expand Down Expand Up @@ -201,6 +203,24 @@ impl DeserializableFromArray for KeyFrag {
}
}

impl Serialize for KeyFrag {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl<'de> Deserialize<'de> for KeyFrag {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer)
}
}

impl HasTypeName for KeyFrag {
fn type_name() -> &'static str {
"KeyFrag"
Expand Down Expand Up @@ -355,6 +375,15 @@ impl SerializableToArray for VerifiedKeyFrag {
}
}

impl Serialize for VerifiedKeyFrag {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl HasTypeName for VerifiedKeyFrag {
fn type_name() -> &'static str {
"VerifiedKeyFrag"
Expand Down Expand Up @@ -471,6 +500,7 @@ mod tests {
use rand_core::OsRng;

use super::{KeyFrag, KeyFragBase, KeyFragVerificationError, VerifiedKeyFrag};
use crate::serde::tests::{check_deserialization, check_serialization};
use crate::{DeserializableFromArray, PublicKey, SecretKey, SerializableToArray, Signer};

fn prepare_kfrags(
Expand Down Expand Up @@ -539,4 +569,18 @@ mod tests {
}
}
}

#[test]
fn test_serde_serialization() {
let (_delegating_pk, _receiving_pk, _verifying_pk, verified_kfrags) =
prepare_kfrags(true, true);

let vkfrag = verified_kfrags[0].clone();
let kfrag = KeyFrag::from_array(&vkfrag.to_array()).unwrap();

check_serialization(&vkfrag);

check_serialization(&kfrag);
check_deserialization(&kfrag);
}
}
54 changes: 54 additions & 0 deletions umbral-pre/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ecdsa::{Signature as BackendSignature, SignatureSize, SigningKey, VerifyingK
use elliptic_curve::{PublicKey as BackendPublicKey, SecretKey as BackendSecretKey};
use generic_array::GenericArray;
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use signature::{DigestVerifier, RandomizedDigestSigner, Signature as SignatureTrait};
use typenum::{Unsigned, U32, U64};

Expand All @@ -18,6 +19,7 @@ use crate::curve::{BackendNonZeroScalar, CurvePoint, CurveScalar, CurveType};
use crate::dem::kdf;
use crate::hashing::{BackendDigest, Hash, ScalarDigest};
use crate::secret_box::{CanBeZeroizedOnDrop, SecretBox};
use crate::serde::{serde_deserialize, serde_serialize};
use crate::traits::{
fmt_public, fmt_secret, ConstructionError, DeserializableFromArray, HasTypeName,
RepresentableAsArray, SerializableToArray, SerializableToSecretArray, SizeMismatchError,
Expand Down Expand Up @@ -47,6 +49,24 @@ impl DeserializableFromArray for Signature {
}
}

impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer)
}
}

impl Signature {
/// Verifies that the given message was signed with the secret counterpart of the given key.
/// The message is hashed internally.
Expand Down Expand Up @@ -238,6 +258,24 @@ impl DeserializableFromArray for PublicKey {
}
}

impl Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serde_serialize(self, serializer)
}
}

impl<'de> Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
serde_deserialize(deserializer)
}
}

impl HasTypeName for PublicKey {
fn type_name() -> &'static str {
"PublicKey"
Expand Down Expand Up @@ -382,6 +420,7 @@ impl fmt::Display for SecretKeyFactory {
mod tests {

use super::{PublicKey, SecretKey, SecretKeyFactory, Signer};
use crate::serde::tests::{check_deserialization, check_serialization};
use crate::{DeserializableFromArray, SerializableToArray, SerializableToSecretArray};

#[test]
Expand Down Expand Up @@ -433,4 +472,19 @@ mod tests {
assert_eq!(pk, vk);
assert!(signature.verify(&vk, message));
}

#[test]
fn test_serde_serialization() {
let sk = SecretKey::random();
let pk = sk.public_key();
let message = b"asdafdahsfdasdfasd";
let signer = Signer::new(&sk);
let signature = signer.sign(message);

check_serialization(&pk);
check_deserialization(&pk);

check_serialization(&signature);
check_deserialization(&signature);
}
}
1 change: 1 addition & 0 deletions umbral-pre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ mod keys;
mod params;
mod pre;
mod secret_box;
mod serde;
mod traits;

pub use capsule::{Capsule, OpenReencryptedError};
Expand Down
Loading

0 comments on commit 51e56c5

Please sign in to comment.