Skip to content

Commit

Permalink
pkcs1v15: make *_with_prefix methods the default (#290)
Browse files Browse the repository at this point in the history
Renames the following:

- `SigningKey::new` => `SigningKey::new_unprefixed`
- `SigningKey::new_with_prefix` => `SigningKey::new`
- `VerifyingKey::new` => `VerifyingKey::new_unprefixed`
- `VerifyingKey::new_with_prefix` => `VerifyingKey::new`

The `*_with_prefix` methods are preserved with a deprecation warning,
which should help people migrate to the new versions.

Closes #238
  • Loading branch information
tarcieri authored Apr 11, 2023
1 parent 5546279 commit bf1defd
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions src/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,17 +423,20 @@ where
///
/// ## Note: unprefixed signatures are uncommon
///
/// In most cases you'll want to use [`SigningKey::new_with_prefix`].
pub fn new(key: RsaPrivateKey) -> Self {
/// In most cases you'll want to use [`SigningKey::new`].
pub fn new_unprefixed(key: RsaPrivateKey) -> Self {
Self {
inner: key,
prefix: Vec::new(),
phantom: Default::default(),
}
}

/// Generate a new signing key.
pub fn random<R: CryptoRngCore + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self> {
/// Generate a new signing key with an empty prefix.
pub fn random_unprefixed<R: CryptoRngCore + ?Sized>(
rng: &mut R,
bit_size: usize,
) -> Result<Self> {
Ok(Self {
inner: RsaPrivateKey::new(rng, bit_size)?,
prefix: Vec::new(),
Expand Down Expand Up @@ -469,7 +472,7 @@ where
D: Digest,
{
fn from(key: RsaPrivateKey) -> Self {
Self::new(key)
Self::new_unprefixed(key)
}
}

Expand All @@ -487,7 +490,7 @@ where
D: Digest + AssociatedOid,
{
/// Create a new signing key with a prefix for the digest `D`.
pub fn new_with_prefix(key: RsaPrivateKey) -> Self {
pub fn new(key: RsaPrivateKey) -> Self {
Self {
inner: key,
prefix: generate_prefix::<D>(),
Expand All @@ -496,16 +499,28 @@ where
}

/// Generate a new signing key with a prefix for the digest `D`.
pub fn random_with_prefix<R: CryptoRngCore + ?Sized>(
rng: &mut R,
bit_size: usize,
) -> Result<Self> {
pub fn random<R: CryptoRngCore + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self> {
Ok(Self {
inner: RsaPrivateKey::new(rng, bit_size)?,
prefix: generate_prefix::<D>(),
phantom: Default::default(),
})
}

/// Create a new signing key with a prefix for the digest `D`.
#[deprecated(since = "0.9.0", note = "use SigningKey::new instead")]
pub fn new_with_prefix(key: RsaPrivateKey) -> Self {
Self::new(key)
}

/// Generate a new signing key with a prefix for the digest `D`.
#[deprecated(since = "0.9.0", note = "use SigningKey::random instead")]
pub fn random_with_prefix<R: CryptoRngCore + ?Sized>(
rng: &mut R,
bit_size: usize,
) -> Result<Self> {
Self::random(rng, bit_size)
}
}

impl<D> AsRef<RsaPrivateKey> for SigningKey<D>
Expand Down Expand Up @@ -624,8 +639,8 @@ where
///
/// ## Note: unprefixed signatures are uncommon
///
/// In most cases you'll want to use [`VerifyingKey::new_with_prefix`].
pub fn new(key: RsaPublicKey) -> Self {
/// In most cases you'll want to use [`VerifyingKey::new`] instead.
pub fn new_unprefixed(key: RsaPublicKey) -> Self {
Self {
inner: key,
prefix: Vec::new(),
Expand Down Expand Up @@ -661,7 +676,7 @@ where
D: Digest,
{
fn from(key: RsaPublicKey) -> Self {
Self::new(key)
Self::new_unprefixed(key)
}
}

Expand All @@ -679,13 +694,19 @@ where
D: Digest + AssociatedOid,
{
/// Create a new verifying key with a prefix for the digest `D`.
pub fn new_with_prefix(key: RsaPublicKey) -> Self {
pub fn new(key: RsaPublicKey) -> Self {
Self {
inner: key,
prefix: generate_prefix::<D>(),
phantom: Default::default(),
}
}

/// Create a new verifying key with a prefix for the digest `D`.
#[deprecated(since = "0.9.0", note = "use VerifyingKey::new instead")]
pub fn new_with_prefix(key: RsaPublicKey) -> Self {
Self::new(key)
}
}

impl<D> AsRef<RsaPublicKey> for VerifyingKey<D>
Expand Down Expand Up @@ -1064,7 +1085,7 @@ mod tests {
),
)];

let signing_key = SigningKey::<Sha1>::new_with_prefix(priv_key);
let signing_key = SigningKey::<Sha1>::new(priv_key);

for (text, expected) in &tests {
let out = signing_key.sign(text.as_bytes());
Expand All @@ -1090,7 +1111,7 @@ mod tests {
),
)];

let signing_key = SigningKey::<Sha256>::new_with_prefix(priv_key);
let signing_key = SigningKey::<Sha256>::new(priv_key);

for (text, expected) in &tests {
let out = signing_key.sign(text.as_bytes());
Expand All @@ -1115,7 +1136,7 @@ mod tests {
),
)];

let signing_key = SigningKey::<Sha3_256>::new_with_prefix(priv_key);
let signing_key = SigningKey::<Sha3_256>::new(priv_key);

for (text, expected) in &tests {
let out = signing_key.sign(text.as_bytes());
Expand All @@ -1140,7 +1161,7 @@ mod tests {
),
)];

let signing_key = SigningKey::new_with_prefix(priv_key);
let signing_key = SigningKey::new(priv_key);

for (text, expected) in &tests {
let mut digest = Sha1::new();
Expand Down Expand Up @@ -1218,7 +1239,7 @@ mod tests {
),
];
let pub_key: RsaPublicKey = priv_key.into();
let verifying_key = VerifyingKey::<Sha1>::new_with_prefix(pub_key);
let verifying_key = VerifyingKey::<Sha1>::new(pub_key);

for (text, sig, expected) in &tests {
let result = verifying_key.verify(
Expand Down Expand Up @@ -1257,7 +1278,7 @@ mod tests {
),
];
let pub_key: RsaPublicKey = priv_key.into();
let verifying_key = VerifyingKey::new_with_prefix(pub_key);
let verifying_key = VerifyingKey::new(pub_key);

for (text, sig, expected) in &tests {
let mut digest = Sha1::new();
Expand All @@ -1272,6 +1293,7 @@ mod tests {
}
}
}

#[test]
fn test_unpadded_signature() {
let msg = b"Thu Dec 19 18:06:16 EST 2013\n";
Expand All @@ -1293,7 +1315,7 @@ mod tests {
let expected_sig = Base64::decode_vec("pX4DR8azytjdQ1rtUiC040FjkepuQut5q2ZFX1pTjBrOVKNjgsCDyiJDGZTCNoh9qpXYbhl7iEym30BWWwuiZg==").unwrap();
let priv_key = get_private_key();

let signing_key = SigningKey::<Sha1>::new(priv_key);
let signing_key = SigningKey::<Sha1>::new_unprefixed(priv_key);
let sig = signing_key.sign_prehash(msg).expect("Failure during sign");
assert_eq!(sig.as_ref(), expected_sig);

Expand Down

0 comments on commit bf1defd

Please sign in to comment.