Skip to content

Commit

Permalink
dsa: introduce serde
Browse files Browse the repository at this point in the history
This commits introduces the dependency `serde` to `dsa`. It is gated
by two features: `serde` and `serde_secrets`. The first only derives
`Deserialize` and `Serialize` for non-secret types like `Signature`.
The latter additionally for `SignatureKey`, which should be used with
caution, because it could allow accidentally leaking secrets.
  • Loading branch information
sivizius committed Oct 12, 2024
1 parent c0ab70c commit 4923b18
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ num-bigint = { package = "num-bigint-dig", version = "0.8", default-features = f
num-traits = { version = "0.2", default-features = false }
pkcs8 = { version = "=0.11.0-rc.1", default-features = false, features = ["alloc"] }
rfc6979 = { version = "=0.5.0-pre.4", path = "../rfc6979" }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
sha2 = { version = "=0.11.0-pre.4", default-features = false }
signature = { version = "=2.3.0-pre.4", default-features = false, features = ["alloc", "digest", "rand_core"] }
zeroize = { version = "1", default-features = false }
Expand All @@ -32,4 +33,12 @@ rand_chacha = "0.3"
sha1 = "=0.11.0-pre.4"

[features]
serde = [
"dep:serde",
"num-bigint/serde",
]
serde_secrets = [
"serde",
"zeroize/serde",
]
std = []
4 changes: 4 additions & 0 deletions dsa/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ use pkcs8::der::{
};
use signature::rand_core::CryptoRngCore;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// The common components of an DSA keypair
///
/// (the prime p, quotient q and generator g)
#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[must_use]
pub struct Components {
/// Prime p
Expand Down
4 changes: 4 additions & 0 deletions dsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ use pkcs8::der::{
};
use signature::SignatureEncoding;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Container of the DSA signature
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[must_use]
pub struct Signature {
/// Signature part r
Expand Down
4 changes: 4 additions & 0 deletions dsa/src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ use signature::{
};
use zeroize::{Zeroize, Zeroizing};

#[cfg(feature = "serde_secrets")]
use serde::{Deserialize, Serialize};

/// DSA private key.
///
/// The [`(try_)sign_digest_with_rng`](::signature::RandomizedDigestSigner) API uses regular non-deterministic signatures,
/// while the [`(try_)sign_digest`](::signature::DigestSigner) API uses deterministic signatures as described in RFC 6979
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde_secrets", derive(Deserialize, Serialize))]
#[must_use]
pub struct SigningKey {
/// Public key
Expand Down
4 changes: 4 additions & 0 deletions dsa/src/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ use pkcs8::{
};
use signature::{hazmat::PrehashVerifier, DigestVerifier, Verifier};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// DSA public key.
#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[must_use]
pub struct VerifyingKey {
/// common components
Expand Down

0 comments on commit 4923b18

Please sign in to comment.