Skip to content

Commit 90757c3

Browse files
authored
dsa: make pkcs8 feature optional (#1014)
It's not needed in applications which define their own key encodings, for example SSH. The feature is still enabled by-default.
1 parent 36a3faf commit 90757c3

File tree

11 files changed

+73
-41
lines changed

11 files changed

+73
-41
lines changed

dsa/Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ keywords = ["crypto", "nist", "signature"]
1616
rust-version = "1.85"
1717

1818
[dependencies]
19+
der = { version = "0.8.0-rc.7", features = ["alloc"] }
1920
digest = "0.11.0-rc.0"
2021
crypto-bigint = { version = "=0.7.0-pre.6", default-features = false, features = ["alloc", "zeroize"] }
2122
crypto-primes = { version = "=0.7.0-pre.1", default-features = false }
22-
pkcs8 = { version = "0.11.0-rc.5", default-features = false, features = ["alloc"] }
2323
rfc6979 = { version = "0.5.0-rc.0" }
2424
sha2 = { version = "0.11.0-rc.0", default-features = false }
2525
signature = { version = "3.0.0-rc.1", default-features = false, features = ["alloc", "digest", "rand_core"] }
26-
zeroize = { version = "1", default-features = false }
26+
zeroize = { version = "1", default-features = false, features = ["alloc"] }
27+
28+
# optional dependencies
29+
pkcs8 = { version = "0.11.0-rc.5", optional = true, default-features = false, features = ["alloc"] }
2730

2831
[dev-dependencies]
2932
hex = "0.4.3"
@@ -36,19 +39,20 @@ sha1 = "0.11.0-rc.0"
3639
der = { version = "0.8.0-rc.6", features = ["derive"] }
3740

3841
[features]
42+
default = ["pkcs8"]
3943
hazmat = []
4044

4145
[package.metadata.docs.rs]
4246
all-features = true
4347

4448
[[example]]
4549
name = "sign"
46-
required-features = ["hazmat"]
50+
required-features = ["hazmat", "pkcs8"]
4751

4852
[[example]]
4953
name = "generate"
5054
required-features = ["hazmat"]
5155

5256
[[example]]
5357
name = "export"
54-
required-features = ["hazmat"]
58+
required-features = ["hazmat", "pkcs8"]

dsa/examples/sign.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(feature = "hazmat")]
2-
31
use digest::Digest;
42
use dsa::{Components, KeySize, SigningKey};
53
use pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};

dsa/src/components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use crate::{size::KeySize, two};
66
use crypto_bigint::{BoxedUint, NonZero, Odd};
7-
use pkcs8::der::{
7+
use der::{
88
self, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Tag, Writer,
99
asn1::UintRef,
1010
};

dsa/src/generate.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ use signature::rand_core::CryptoRng;
44

55
mod components;
66
mod keypair;
7+
#[cfg(feature = "hazmat")]
78
mod secret_number;
89

9-
pub use self::components::{common as common_components, public as public_component};
10+
pub use self::components::common as common_components;
11+
#[cfg(feature = "hazmat")]
1012
pub use self::secret_number::{secret_number, secret_number_rfc6979};
1113

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

17+
#[cfg(all(feature = "hazmat", feature = "pkcs8"))]
18+
pub use self::components::public as public_component;
19+
1520
/// Calculate the upper and lower bounds for generating values like p or q
1621
#[inline]
1722
fn calculate_bounds(size: u32) -> (NonZero<BoxedUint>, NonZero<BoxedUint>) {

dsa/src/generate/components.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
//!
44
55
use crate::{
6-
Components,
76
generate::{calculate_bounds, generate_prime},
87
size::KeySize,
98
two,
109
};
1110
use crypto_bigint::{
1211
BoxedUint, NonZero, Odd, RandomBits, Resize,
1312
modular::{BoxedMontyForm, BoxedMontyParams},
14-
subtle::CtOption,
1513
};
1614
use crypto_primes::{Flavor, is_prime};
1715
use signature::rand_core::CryptoRng;
1816

17+
#[cfg(feature = "hazmat")]
18+
use {crate::Components, crypto_bigint::subtle::CtOption};
19+
1920
/// Generate the common components p, q, and g
2021
///
2122
/// # Returns
@@ -85,6 +86,7 @@ pub fn common<R: CryptoRng + ?Sized>(
8586
}
8687

8788
/// Calculate the public component from the common components and the private component
89+
#[cfg(feature = "hazmat")]
8890
#[inline]
8991
pub fn public(components: &Components, x: &NonZero<BoxedUint>) -> CtOption<NonZero<BoxedUint>> {
9092
let p = components.p();

dsa/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,35 @@ pub use crate::signing_key::SigningKey;
5858
pub use crate::{components::Components, size::KeySize, verifying_key::VerifyingKey};
5959

6060
pub use crypto_bigint::BoxedUint;
61-
pub use pkcs8;
6261
pub use signature;
6362

63+
#[cfg(feature = "pkcs8")]
64+
pub use pkcs8;
65+
6466
use crypto_bigint::NonZero;
65-
use pkcs8::spki::ObjectIdentifier;
6667

6768
mod components;
6869
mod generate;
6970
mod signing_key;
7071
mod size;
7172
mod verifying_key;
7273

74+
use alloc::{boxed::Box, vec::Vec};
75+
use der::{
76+
Decode, DecodeValue, Encode, EncodeValue, FixedTag, Length, Reader, Sequence, Writer,
77+
asn1::UintRef,
78+
};
79+
use signature::SignatureEncoding;
80+
81+
#[cfg(feature = "pkcs8")]
82+
use pkcs8::ObjectIdentifier;
83+
7384
/// DSA object identifier as defined by [RFC3279 § 2.3.2].
7485
///
7586
/// [RFC3279 2.3.2]: https://www.rfc-editor.org/rfc/rfc3279#section-2.3.2
87+
#[cfg(feature = "pkcs8")]
7688
pub const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10040.4.1");
7789

78-
use alloc::{boxed::Box, vec::Vec};
79-
use pkcs8::der::{
80-
self, Decode, DecodeValue, Encode, EncodeValue, FixedTag, Header, Length, Reader, Sequence,
81-
Writer, asn1::UintRef,
82-
};
83-
use signature::SignatureEncoding;
84-
8590
/// Container of the DSA signature
8691
#[derive(Clone, Debug)]
8792
#[must_use]
@@ -117,7 +122,7 @@ impl Signature {
117122
impl<'a> DecodeValue<'a> for Signature {
118123
type Error = der::Error;
119124

120-
fn decode_value<R: Reader<'a>>(reader: &mut R, _header: Header) -> der::Result<Self> {
125+
fn decode_value<R: Reader<'a>>(reader: &mut R, _header: der::Header) -> der::Result<Self> {
121126
let r = UintRef::decode(reader)?;
122127
let s = UintRef::decode(reader)?;
123128

dsa/src/signing_key.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//! Module containing the definition of the private key container
33
//!
44
5-
use crate::{Components, OID, Signature, VerifyingKey};
5+
#![cfg(feature = "hazmat")]
6+
7+
use crate::{Signature, VerifyingKey};
68
use core::{
79
cmp::min,
810
fmt::{self, Debug},
@@ -12,21 +14,27 @@ use crypto_bigint::{
1214
modular::{BoxedMontyForm, BoxedMontyParams},
1315
};
1416
use digest::{Digest, FixedOutputReset, block_api::BlockSizeUser};
15-
use pkcs8::{
16-
AlgorithmIdentifierRef, EncodePrivateKey, PrivateKeyInfoRef, SecretDocument,
17-
der::{
18-
AnyRef, Decode, Encode,
19-
asn1::{OctetStringRef, UintRef},
20-
},
21-
};
22-
#[cfg(feature = "hazmat")]
23-
use signature::rand_core::CryptoRng;
2417
use signature::{
2518
DigestSigner, MultipartSigner, RandomizedDigestSigner, Signer,
2619
hazmat::{PrehashSigner, RandomizedPrehashSigner},
2720
rand_core::TryCryptoRng,
2821
};
29-
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
22+
use zeroize::{ZeroizeOnDrop, Zeroizing};
23+
24+
#[cfg(feature = "hazmat")]
25+
use {crate::Components, signature::rand_core::CryptoRng};
26+
#[cfg(feature = "pkcs8")]
27+
use {
28+
crate::OID,
29+
pkcs8::{
30+
AlgorithmIdentifierRef, EncodePrivateKey, PrivateKeyInfoRef, SecretDocument,
31+
der::{
32+
AnyRef, Decode, Encode,
33+
asn1::{OctetStringRef, UintRef},
34+
},
35+
},
36+
zeroize::Zeroize,
37+
};
3038

3139
/// DSA private key.
3240
///
@@ -208,6 +216,7 @@ where
208216
}
209217
}
210218

219+
#[cfg(feature = "pkcs8")]
211220
impl EncodePrivateKey for SigningKey {
212221
fn to_pkcs8_der(&self) -> pkcs8::Result<SecretDocument> {
213222
let parameters = self.verifying_key().components().to_der()?;
@@ -232,6 +241,7 @@ impl EncodePrivateKey for SigningKey {
232241
}
233242
}
234243

244+
#[cfg(feature = "pkcs8")]
235245
impl<'a> TryFrom<PrivateKeyInfoRef<'a>> for SigningKey {
236246
type Error = pkcs8::Error;
237247

dsa/src/verifying_key.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22
//! Module containing the definition of the public key container
33
//!
44
5-
use crate::{Components, OID, Signature, two};
5+
use crate::{Components, Signature, two};
66
use core::cmp::min;
77
use crypto_bigint::{
88
BoxedUint, NonZero, Resize,
99
modular::{BoxedMontyForm, BoxedMontyParams},
1010
};
1111
use digest::Digest;
12-
use pkcs8::{
13-
AlgorithmIdentifierRef, EncodePublicKey, SubjectPublicKeyInfoRef,
14-
der::{
15-
AnyRef, Decode, Encode,
16-
asn1::{BitStringRef, UintRef},
12+
use signature::{DigestVerifier, MultipartVerifier, Verifier, hazmat::PrehashVerifier};
13+
14+
#[cfg(feature = "pkcs8")]
15+
use {
16+
crate::OID,
17+
pkcs8::{
18+
AlgorithmIdentifierRef, EncodePublicKey, SubjectPublicKeyInfoRef,
19+
der::{
20+
AnyRef, Decode, Encode,
21+
asn1::{BitStringRef, UintRef},
22+
},
23+
spki,
1724
},
18-
spki,
1925
};
20-
use signature::{DigestVerifier, MultipartVerifier, Verifier, hazmat::PrehashVerifier};
2126

2227
/// DSA public key.
2328
#[derive(Clone, Debug, PartialEq, PartialOrd)]
@@ -158,6 +163,7 @@ where
158163
}
159164
}
160165

166+
#[cfg(feature = "pkcs8")]
161167
impl EncodePublicKey for VerifyingKey {
162168
fn to_public_key_der(&self) -> spki::Result<spki::Document> {
163169
let parameters = self.components.to_der()?;
@@ -179,6 +185,7 @@ impl EncodePublicKey for VerifyingKey {
179185
}
180186
}
181187

188+
#[cfg(feature = "pkcs8")]
182189
impl<'a> TryFrom<SubjectPublicKeyInfoRef<'a>> for VerifyingKey {
183190
type Error = spki::Error;
184191

dsa/tests/proptest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "hazmat")]
1+
#![cfg(all(feature = "hazmat", feature = "pkcs8"))]
22
//! Property-based tests.
33
44
use der::{Decode, Encode, Sequence, asn1::Uint};

dsa/tests/signing_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(feature = "hazmat")]
1+
#![cfg(all(feature = "hazmat", feature = "pkcs8"))]
22
// We abused the deprecated attribute for unsecure key sizes
33
// But we want to use those small key sizes for fast tests
44
#![allow(deprecated)]

0 commit comments

Comments
 (0)