Skip to content

Commit 7123e0f

Browse files
committed
dsa: Implement signature traits directly on the structures
1 parent 6bc6cc1 commit 7123e0f

File tree

8 files changed

+113
-230
lines changed

8 files changed

+113
-230
lines changed

dsa/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ num-bigint = { package = "num-bigint-dig", version = "0.8.1", default-features =
1414
num-traits = { version = "0.2.15", default-features = false }
1515
opaque-debug = "0.3.0"
1616
paste = "1.0.7"
17-
pkcs8 = { version = "0.9.0", default-features = false, features = ["alloc"] }
17+
pkcs8 = { version = "0.9.0", default-features = false }
1818
rand = { version = "0.8.5", default-features = false }
19-
signature = { version = "1.5.0", optional = true }
19+
signature = { version = ">= 1.5.0, < 1.6.0", default-features = false, features = ["digest-preview", "rand-preview"] }
2020
zeroize = { version = "1.5.5", default-features = false }
2121

2222
[features]
23-
default = ["signature-compat"]
24-
signature-compat = ["signature", "pkcs8/std", "rand/std", "rand/std_rng"]
23+
default = []
2524

2625
[dev-dependencies]
2726
pkcs8 = { version = "0.9.0", default-features = false, features = ["pem"] }

dsa/examples/export.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ fn main() {
88
let private_key = PrivateKey::generate(&mut rng, components);
99
let public_key = private_key.public_key();
1010

11+
let private_key_bytes = private_key.to_pkcs8_pem(LineEnding::LF).unwrap();
12+
let public_key_bytes = public_key.to_public_key_pem(LineEnding::LF).unwrap();
13+
1114
let mut file = File::create("public.pem").unwrap();
12-
file.write_all(
13-
public_key
14-
.to_public_key_pem(LineEnding::LF)
15-
.unwrap()
16-
.as_bytes(),
17-
)
18-
.unwrap();
15+
file.write_all(public_key_bytes.as_bytes()).unwrap();
1916
file.flush().unwrap();
2017

2118
let mut file = File::create("private.pem").unwrap();
22-
file.write_all(private_key.to_pkcs8_pem(LineEnding::LF).unwrap().as_bytes())
23-
.unwrap();
19+
file.write_all(private_key_bytes.as_bytes()).unwrap();
2420
file.flush().unwrap();
2521
}

dsa/examples/sign.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1+
use digest::Digest;
12
use dsa::{consts::DSA_2048_256, Components, PrivateKey};
2-
use pkcs8::{der::Encode, EncodePrivateKey, EncodePublicKey, LineEnding};
3+
use pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding};
34
use sha1::Sha1;
5+
use signature::{RandomizedDigestSigner, Signature};
46
use std::{fs::File, io::Write};
57

68
fn main() {
79
let mut rng = rand::thread_rng();
810
let components = Components::generate(&mut rng, DSA_2048_256);
911
let private_key = PrivateKey::generate(&mut rng, components);
1012
let public_key = private_key.public_key();
13+
1114
let signature = private_key
12-
.sign::<_, Sha1>(&mut rng, b"hello world")
13-
.unwrap();
15+
.sign_digest_with_rng(rand::thread_rng(), Sha1::new().chain_update(b"hello world"));
16+
17+
let private_key_bytes = private_key.to_pkcs8_pem(LineEnding::LF).unwrap();
18+
let public_key_bytes = public_key.to_public_key_pem(LineEnding::LF).unwrap();
1419

1520
let mut file = File::create("public.pem").unwrap();
16-
file.write_all(
17-
public_key
18-
.to_public_key_pem(LineEnding::LF)
19-
.unwrap()
20-
.as_bytes(),
21-
)
22-
.unwrap();
21+
file.write_all(public_key_bytes.as_bytes()).unwrap();
2322
file.flush().unwrap();
2423

2524
let mut file = File::create("signature.der").unwrap();
26-
file.write_all(signature.to_vec().unwrap().as_ref())
27-
.unwrap();
25+
file.write_all(signature.as_bytes()).unwrap();
2826
file.flush().unwrap();
2927

3028
let mut file = File::create("private.pem").unwrap();
31-
file.write_all(private_key.to_pkcs8_pem(LineEnding::LF).unwrap().as_bytes())
32-
.unwrap();
29+
file.write_all(private_key_bytes.as_bytes()).unwrap();
3330
file.flush().unwrap();
3431
}

dsa/src/compat.rs

-169
This file was deleted.

dsa/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@
5454
//! ```
5555
//!
5656
57-
#![cfg_attr(not(feature = "signature-compat"), no_std)]
57+
#![no_std]
5858
#![forbid(missing_docs, unsafe_code)]
5959
#![deny(rust_2018_idioms)]
6060

61+
extern crate alloc;
62+
6163
/// DSA object identifier as defined by RFC-3279, section 2.3.2
6264
const DSA_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10040.4.1");
6365

6466
pub use self::components::Components;
6567
pub use self::privatekey::PrivateKey;
6668
pub use self::publickey::PublicKey;
67-
pub use self::signature::Signature;
69+
pub use self::sig::Signature;
6870

6971
// Re-export the types needed for de-/serialising keys to DER and PEM
7072
pub use pkcs8;
73+
pub use signature;
7174

72-
#[cfg(feature = "signature-compat")]
73-
#[cfg_attr(doc_cfg, doc(cfg(feature = "signature-compat")))]
74-
pub mod compat;
7575
pub mod consts;
7676

7777
use num_bigint::BigUint;
@@ -81,7 +81,7 @@ mod components;
8181
mod generate;
8282
mod privatekey;
8383
mod publickey;
84-
mod signature;
84+
mod sig;
8585

8686
/// Returns a `BigUint` with the value 2
8787
#[inline]

0 commit comments

Comments
 (0)