Skip to content

Commit

Permalink
dsa: gate signing under hazmat feature (#859)
Browse files Browse the repository at this point in the history
Resolves #858
  • Loading branch information
msedzins authored Jan 19, 2025
1 parent e658b96 commit 4064907
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 10 deletions.
13 changes: 13 additions & 0 deletions dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ sha1 = "=0.11.0-pre.4"

[features]
std = []
hazmat = []

[[example]]
name = "sign"
required-features = ["hazmat"]

[[example]]
name = "generate"
required-features = ["hazmat"]

[[example]]
name = "export"
required-features = ["hazmat"]
2 changes: 2 additions & 0 deletions dsa/examples/export.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "hazmat")]

use dsa::{Components, KeySize, SigningKey};
use pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};
use std::{fs::File, io::Write};
Expand Down
2 changes: 2 additions & 0 deletions dsa/examples/generate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "hazmat")]

use dsa::{Components, KeySize, SigningKey};

fn main() {
Expand Down
2 changes: 2 additions & 0 deletions dsa/examples/sign.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "hazmat")]

use digest::Digest;
use dsa::{Components, KeySize, SigningKey};
use pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};
Expand Down
4 changes: 3 additions & 1 deletion dsa/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ mod keypair;
mod secret_number;

pub use self::components::{common as common_components, public as public_component};
pub use self::keypair::keypair;
pub use self::secret_number::{secret_number, secret_number_rfc6979};

#[cfg(feature = "hazmat")]
pub use self::keypair::keypair;

/// Calculate the upper and lower bounds for generating values like p or q
#[inline]
fn calculate_bounds(size: u32) -> (BigUint, BigUint) {
Expand Down
3 changes: 2 additions & 1 deletion dsa/src/generate/keypair.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![cfg(feature = "hazmat")]
//!
//! Generate a DSA keypair
//!
use crate::{generate::components, Components, SigningKey, VerifyingKey};
use crate::{generate::components, signing_key::SigningKey, Components, VerifyingKey};
use num_bigint::{BigUint, RandBigInt};
use num_traits::One;
use signature::rand_core::CryptoRngCore;
Expand Down
2 changes: 1 addition & 1 deletion dsa/src/generate/secret_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! Generate a per-message secret number
//!
use crate::{Components, SigningKey};
use crate::{signing_key::SigningKey, Components};
use alloc::{vec, vec::Vec};
use core::cmp::min;
use digest::{core_api::BlockSizeUser, Digest, FixedOutputReset};
Expand Down
13 changes: 8 additions & 5 deletions dsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
//!
//! Generate a DSA keypair
//!
//! ```
#![cfg_attr(feature = "hazmat", doc = "```")]
#![cfg_attr(not(feature = "hazmat"), doc = "```ignore")]
//! # use dsa::{KeySize, Components, SigningKey};
//! let mut csprng = rand::thread_rng();
//! let components = Components::generate(&mut csprng, KeySize::DSA_2048_256);
Expand All @@ -22,7 +23,8 @@
//!
//! Create keypair from existing components
//!
//! ```
#![cfg_attr(feature = "hazmat", doc = "```")]
#![cfg_attr(not(feature = "hazmat"), doc = "```ignore")]
//! # use dsa::{Components, SigningKey, VerifyingKey};
//! # use num_bigint::BigUint;
//! # use num_traits::One;
Expand All @@ -46,9 +48,10 @@
extern crate alloc;

pub use crate::{
components::Components, signing_key::SigningKey, size::KeySize, verifying_key::VerifyingKey,
};
#[cfg(feature = "hazmat")]
pub use crate::signing_key::SigningKey;

pub use crate::{components::Components, size::KeySize, verifying_key::VerifyingKey};

pub use num_bigint::BigUint;
pub use pkcs8;
Expand Down
2 changes: 2 additions & 0 deletions dsa/src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl SigningKey {
})
}

#[cfg(feature = "hazmat")]
/// Generate a new DSA keypair
#[inline]
pub fn generate(rng: &mut impl CryptoRngCore, components: Components) -> SigningKey {
Expand All @@ -70,6 +71,7 @@ impl SigningKey {
&self.x
}

#[cfg(feature = "hazmat")]
/// Try to sign the given message digest deterministically with a prehashed digest.
/// The parameter `D` must match the hash function used to sign the digest.
///
Expand Down
2 changes: 2 additions & 0 deletions dsa/tests/deterministic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "hazmat")]

use digest::{core_api::BlockSizeUser, Digest, FixedOutputReset};
use dsa::{Components, Signature, SigningKey, VerifyingKey};
use num_bigint::BigUint;
Expand Down
1 change: 1 addition & 0 deletions dsa/tests/signature.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "hazmat")]
#![allow(deprecated)]

use digest::Digest;
Expand Down
1 change: 1 addition & 0 deletions dsa/tests/signing_key.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "hazmat")]
// We abused the deprecated attribute for unsecure key sizes
// But we want to use those small key sizes for fast tests
#![allow(deprecated)]
Expand Down
12 changes: 10 additions & 2 deletions dsa/tests/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
// But we want to use those small key sizes for fast tests
#![allow(deprecated)]

use dsa::{Components, KeySize, SigningKey, VerifyingKey};
use dsa::VerifyingKey;
use pkcs8::{DecodePublicKey, EncodePublicKey, LineEnding};

#[cfg(feature = "hazmat")]
use dsa::{Components, KeySize, SigningKey};
#[cfg(feature = "hazmat")]
use num_bigint::BigUint;
#[cfg(feature = "hazmat")]
use num_traits::One;
use pkcs8::{DecodePublicKey, EncodePublicKey, LineEnding};

const OPENSSL_PEM_PUBLIC_KEY: &str = include_str!("pems/public.pem");

#[cfg(feature = "hazmat")]
fn generate_verifying_key() -> VerifyingKey {
let mut rng = rand::thread_rng();
let components = Components::generate(&mut rng, KeySize::DSA_1024_160);
Expand All @@ -29,6 +35,7 @@ fn decode_encode_openssl_verifying_key() {
assert_eq!(reencoded_verifying_key, OPENSSL_PEM_PUBLIC_KEY);
}

#[cfg(feature = "hazmat")]
#[test]
fn encode_decode_verifying_key() {
let verifying_key = generate_verifying_key();
Expand All @@ -38,6 +45,7 @@ fn encode_decode_verifying_key() {
assert_eq!(verifying_key, decoded_verifying_key);
}

#[cfg(feature = "hazmat")]
#[test]
fn validate_verifying_key() {
let verifying_key = generate_verifying_key();
Expand Down

0 comments on commit 4064907

Please sign in to comment.