Skip to content

Commit 4631ab6

Browse files
authored
Have RsaPrivateKey::from_components convert n to Odd (#532)
Changes the `n` parameter of this function from `Odd<BoxedUint>` to just `BoxedUint`, and converts to `Odd` internally, returning an error in the event the provided `n` is even. Previously this was a conversion the API caller had to do, which is a little more onerous than just doing it for them. It should also make upgrades from v0.9 easier, since it's one less type to deal with. Closes #531
1 parent 79641e1 commit 4631ab6

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

benches/key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate test;
44

55
use base64ct::{Base64, Encoding};
6-
use crypto_bigint::{BoxedUint, Odd};
6+
use crypto_bigint::BoxedUint;
77
use hex_literal::hex;
88
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
99
use rsa::{Pkcs1v15Encrypt, Pkcs1v15Sign, RsaPrivateKey};
@@ -64,7 +64,7 @@ fn get_key() -> RsaPrivateKey {
6464
];
6565

6666
RsaPrivateKey::from_components(
67-
Odd::new(BoxedUint::from_be_slice(&n, 2048).unwrap()).unwrap(),
67+
BoxedUint::from_be_slice(&n, 2048).unwrap(),
6868
BoxedUint::from(3u32),
6969
BoxedUint::from_be_slice(&d, 2048).unwrap(),
7070
vec![

src/encoding.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
RsaPrivateKey, RsaPublicKey,
99
};
1010
use core::convert::{TryFrom, TryInto};
11-
use crypto_bigint::{BoxedUint, NonZero, Odd, Resize};
11+
use crypto_bigint::{BoxedUint, NonZero, Resize};
1212
use pkcs8::{
1313
der::{asn1::OctetStringRef, Encode},
1414
Document, EncodePrivateKey, EncodePublicKey, ObjectIdentifier, SecretDocument,
@@ -60,8 +60,6 @@ impl TryFrom<pkcs8::PrivateKeyInfoRef<'_>> for RsaPrivateKey {
6060
let bits = u32::try_from(pkcs1_key.modulus.as_bytes().len()).map_err(|_| KeyMalformed)? * 8;
6161

6262
let n = uint_from_slice(pkcs1_key.modulus.as_bytes(), bits)?;
63-
let n = Option::from(Odd::new(n)).ok_or(KeyMalformed)?;
64-
6563
let bits_e = u32::try_from(pkcs1_key.public_exponent.as_bytes().len())
6664
.map_err(|_| KeyMalformed)?
6765
* 8;

src/key.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,12 @@ impl RsaPrivateKey {
287287
exp: BoxedUint,
288288
) -> Result<RsaPrivateKey> {
289289
let components = generate_multi_prime_key_with_exp(rng, 2, bit_size, exp)?;
290-
RsaPrivateKey::from_components(components.n, components.e, components.d, components.primes)
290+
RsaPrivateKey::from_components(
291+
components.n.get(),
292+
components.e,
293+
components.d,
294+
components.primes,
295+
)
291296
}
292297

293298
/// Constructs an RSA key pair from individual components:
@@ -304,11 +309,13 @@ impl RsaPrivateKey {
304309
///
305310
/// [NIST SP 800-56B Revision 2]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br2.pdf
306311
pub fn from_components(
307-
n: Odd<BoxedUint>,
312+
n: BoxedUint,
308313
e: BoxedUint,
309314
d: BoxedUint,
310315
mut primes: Vec<BoxedUint>,
311316
) -> Result<RsaPrivateKey> {
317+
let n = Odd::new(n).into_option().ok_or(Error::InvalidModulus)?;
318+
312319
// The modulus may come in padded with zeros, shorten it
313320
// to ensure optimal performance of arithmetic operations.
314321
let n_bits = n.bits_vartime();
@@ -386,7 +393,7 @@ impl RsaPrivateKey {
386393
let primes = vec![p, q];
387394
let n = compute_modulus(&primes);
388395

389-
Self::from_components(n, public_exponent, d, primes)
396+
Self::from_components(n.get(), public_exponent, d, primes)
390397
}
391398

392399
/// Constructs an RSA key pair from its primes.
@@ -412,7 +419,7 @@ impl RsaPrivateKey {
412419
let n = compute_modulus(&primes);
413420
let d = compute_private_exponent_euler_totient(&primes, &public_exponent)?;
414421

415-
Self::from_components(n, public_exponent, d, primes)
422+
Self::from_components(n.get(), public_exponent, d, primes)
416423
}
417424

418425
/// Get the public key from the private key, cloning `n` and `e`.
@@ -755,7 +762,7 @@ mod tests {
755762
generate_multi_prime_key_with_exp(&mut rng, $multi, $size, exp.clone())
756763
.unwrap();
757764
let private_key = RsaPrivateKey::from_components(
758-
components.n,
765+
components.n.get(),
759766
components.e,
760767
components.d,
761768
components.primes,
@@ -784,14 +791,11 @@ mod tests {
784791
fn test_negative_decryption_value() {
785792
let bits = 128;
786793
let private_key = RsaPrivateKey::from_components(
787-
Odd::new(
788-
BoxedUint::from_le_slice(
789-
&[
790-
99, 192, 208, 179, 0, 220, 7, 29, 49, 151, 75, 107, 75, 73, 200, 180,
791-
],
792-
bits,
793-
)
794-
.unwrap(),
794+
BoxedUint::from_le_slice(
795+
&[
796+
99, 192, 208, 179, 0, 220, 7, 29, 49, 151, 75, 107, 75, 73, 200, 180,
797+
],
798+
bits,
795799
)
796800
.unwrap(),
797801
BoxedUint::from_le_slice(&[1, 0, 1, 0, 0, 0, 0, 0], 64).unwrap(),
@@ -923,7 +927,7 @@ mod tests {
923927
let e = BoxedUint::from_be_slice(&e, 64).unwrap();
924928

925929
let bits = 4096;
926-
let n = Odd::new(BoxedUint::from_be_slice(&n, bits).unwrap()).unwrap();
930+
let n = BoxedUint::from_be_slice(&n, bits).unwrap();
927931
let d = BoxedUint::from_be_slice(&d, bits).unwrap();
928932
let primes = primes
929933
.iter()

src/oaep.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ mod tests {
295295
use crate::traits::PublicKeyParts;
296296
use crate::traits::{Decryptor, RandomizedDecryptor, RandomizedEncryptor};
297297

298-
use crypto_bigint::{BoxedUint, Odd};
298+
use crypto_bigint::BoxedUint;
299299
use digest::{Digest, DynDigest, FixedOutputReset};
300300
use rand_chacha::{
301301
rand_core::{RngCore, SeedableRng},
@@ -335,7 +335,7 @@ mod tests {
335335
// -----END RSA PRIVATE KEY-----
336336

337337
RsaPrivateKey::from_components(
338-
Odd::new(BoxedUint::from_be_hex("d397b84d98a4c26138ed1b695a8106ead91d553bf06041b62d3fdc50a041e222b8f4529689c1b82c5e71554f5dd69fa2f4b6158cf0dbeb57811a0fc327e1f28e74fe74d3bc166c1eabdc1b8b57b934ca8be5b00b4f29975bcc99acaf415b59bb28a6782bb41a2c3c2976b3c18dbadef62f00c6bb226640095096c0cc60d22fe7ef987d75c6a81b10d96bf292028af110dc7cc1bbc43d22adab379a0cd5d8078cc780ff5cd6209dea34c922cf784f7717e428d75b5aec8ff30e5f0141510766e2e0ab8d473c84e8710b2b98227c3db095337ad3452f19e2b9bfbccdd8148abf6776fa552775e6e75956e45229ae5a9c46949bab1e622f0e48f56524a84ed3483b", 2048).unwrap()).unwrap(),
338+
BoxedUint::from_be_hex("d397b84d98a4c26138ed1b695a8106ead91d553bf06041b62d3fdc50a041e222b8f4529689c1b82c5e71554f5dd69fa2f4b6158cf0dbeb57811a0fc327e1f28e74fe74d3bc166c1eabdc1b8b57b934ca8be5b00b4f29975bcc99acaf415b59bb28a6782bb41a2c3c2976b3c18dbadef62f00c6bb226640095096c0cc60d22fe7ef987d75c6a81b10d96bf292028af110dc7cc1bbc43d22adab379a0cd5d8078cc780ff5cd6209dea34c922cf784f7717e428d75b5aec8ff30e5f0141510766e2e0ab8d473c84e8710b2b98227c3db095337ad3452f19e2b9bfbccdd8148abf6776fa552775e6e75956e45229ae5a9c46949bab1e622f0e48f56524a84ed3483b", 2048).unwrap(),
339339
BoxedUint::from(65_537u64),
340340
BoxedUint::from_be_hex("c4e70c689162c94c660828191b52b4d8392115df486a9adbe831e458d73958320dc1b755456e93701e9702d76fb0b92f90e01d1fe248153281fe79aa9763a92fae69d8d7ecd144de29fa135bd14f9573e349e45031e3b76982f583003826c552e89a397c1a06bd2163488630d92e8c2bb643d7abef700da95d685c941489a46f54b5316f62b5d2c3a7f1bbd134cb37353a44683fdc9d95d36458de22f6c44057fe74a0a436c4308f73f4da42f35c47ac16a7138d483afc91e41dc3a1127382e0c0f5119b0221b4fc639d6b9c38177a6de9b526ebd88c38d7982c07f98a0efd877d508aae275b946915c02e2e1106d175d74ec6777f5e80d12c053d9c7be1e341", 2048).unwrap(),
341341
vec![

src/pkcs1v15.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ mod tests {
253253
SignatureEncoding, Signer, Verifier,
254254
};
255255
use base64ct::{Base64, Encoding};
256-
use crypto_bigint::Odd;
257256
use hex_literal::hex;
258257
use rand_chacha::{
259258
rand_core::{RngCore, SeedableRng},
@@ -281,7 +280,7 @@ mod tests {
281280
// -----END RSA PRIVATE KEY-----
282281

283282
RsaPrivateKey::from_components(
284-
Odd::new(BoxedUint::from_be_hex("B2990F49C47DFA8CD400AE6A4D1B8A3B6A13642B23F28B003BFB97790ADE9A4CC82B8B2A81747DDEC08B6296E53A08C331687EF25C4BF4936BA1C0E6041E9D15", 512).unwrap()).unwrap(),
283+
BoxedUint::from_be_hex("B2990F49C47DFA8CD400AE6A4D1B8A3B6A13642B23F28B003BFB97790ADE9A4CC82B8B2A81747DDEC08B6296E53A08C331687EF25C4BF4936BA1C0E6041E9D15", 512).unwrap(),
285284
BoxedUint::from(65_537u64),
286285
BoxedUint::from_be_hex("8ABD6A69F4D1A4B487F0AB8D7AAEFD38609405C999984E30F567E1E8AEEFF44E8B18BDB1EC78DFA31A55E32A48D7FB131F5AF1F44D7D6B2CED2A9DF5E5AE4535", 512).unwrap(),
287286
vec![

0 commit comments

Comments
 (0)