Skip to content

Commit

Permalink
Merge pull request #67 from fjarri/serde
Browse files Browse the repository at this point in the history
`serde` support
  • Loading branch information
fjarri authored Sep 12, 2021
2 parents e90188c + 3e5e1e1 commit 3d3adf3
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `VerifiedCapsuleFrag::from_verified_bytes()`. ([#63])
- Added `SecretKeyFactory::secret_key_factory_by_label()`. ([#64])
- Added `SecretKeyFactory::from_secure_randomness()` and `seed_size()`. ([#64])
- `serde` support for `Capsule`, `CapsuleFrag`, `KeyFrag`, `PublicKey`, and `Signature`. ([#67])


### Fixed
Expand All @@ -40,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#63]: https://github.com/nucypher/rust-umbral/pull/63
[#64]: https://github.com/nucypher/rust-umbral/pull/64
[#65]: https://github.com/nucypher/rust-umbral/pull/65
[#67]: https://github.com/nucypher/rust-umbral/pull/67


## [0.2.0] - 2021-06-14
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ members = [
"umbral-pre-wasm",
"umbral-pre-python",
]

# Prevents feature conflicts between [dependencies] and [dev-dependencies]
# Will be the default in 2021 edition.
resolver = "2"
6 changes: 5 additions & 1 deletion umbral-pre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ k256 = { version = "0.9", default-features = false, features = ["ecdsa", "arithm
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 }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
serde = { version = "1", default-features = false }
base64 = { version = "0.13", default-features = false, features = ["alloc"] }

# 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
32 changes: 32 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, Representation};
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, Representation::Base64)
}
}

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

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

use super::{Capsule, OpenReencryptedError};
use crate::serde::tests::{check_deserialization, check_serialization};
use crate::serde::Representation;
use crate::{
encrypt, generate_kfrags, reencrypt, DeserializableFromArray, SecretKey,
SerializableToArray, Signer,
Expand Down Expand Up @@ -324,4 +346,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, Representation::Base64);
check_deserialization(&capsule);
}
}
34 changes: 34 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, Representation};
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, Representation::Base64)
}
}

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

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

use super::{CapsuleFrag, VerifiedCapsuleFrag};
use crate::serde::tests::{check_deserialization, check_serialization};
use crate::serde::Representation;
use crate::{
encrypt, generate_kfrags, reencrypt, Capsule, DeserializableFromArray, PublicKey,
SecretKey, SerializableToArray, Signer,
Expand Down Expand Up @@ -388,4 +410,16 @@ 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(&cfrag, Representation::Base64);
check_deserialization(&cfrag);
}
}
34 changes: 34 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, Representation};
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, Representation::Base64)
}
}

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

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

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

fn prepare_kfrags(
Expand Down Expand Up @@ -539,4 +561,16 @@ 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(&kfrag, Representation::Base64);
check_deserialization(&kfrag);
}
}
55 changes: 55 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, Representation};
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, Representation::Base64)
}
}

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

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, Representation::Hex)
}
}

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

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

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

#[test]
Expand Down Expand Up @@ -433,4 +473,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, Representation::Hex);
check_deserialization(&pk);

check_serialization(&signature, Representation::Base64);
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 3d3adf3

Please sign in to comment.