Skip to content

Commit

Permalink
feat: add uncheck_modulus_size feature
Browse files Browse the repository at this point in the history
  • Loading branch information
shuai132 committed Oct 9, 2024
1 parent 324c567 commit 2fa8773
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pem = ["pkcs1/pem", "pkcs8/pem"]
pkcs5 = ["pkcs8/encryption"]
u64_digit = ["num-bigint/u64_digit"]
std = ["digest/std", "pkcs1/std", "pkcs8/std", "rand_core/std", "signature/std"]
uncheck_modulus_size = []

[package.metadata.docs.rs]
features = ["std", "pem", "serde", "hazmat", "sha2"]
Expand Down
19 changes: 10 additions & 9 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ pub fn check_public(public_key: &impl PublicKeyParts) -> Result<()> {
/// Check that the public key is well formed and has an exponent within acceptable bounds.
#[inline]
fn check_public_with_max_size(public_key: &impl PublicKeyParts, max_size: usize) -> Result<()> {
#[cfg(not(feature = "uncheck_modulus_size"))]
if public_key.n().bits() > max_size {
return Err(Error::ModulusTooLarge);
}
Expand Down Expand Up @@ -674,7 +675,7 @@ mod tests {
BigUint::from_bytes_le(&[235, 65, 160, 134, 32, 136, 6, 241]),
],
)
.unwrap();
.unwrap();

for _ in 0..1000 {
test_key_basics(&private_key);
Expand Down Expand Up @@ -723,7 +724,7 @@ mod tests {
cUOEnsXNQ+DrZpLKxdtsD/qNV/j1hfeyBoPllC3cV+6bcGOFcVGbjYqb+Kw1b0+j\
L69RSKQqgmS+qYqr8c48nDRxyq3QXhR8qtzUwBFSLVk=",
)
.unwrap();
.unwrap();
let e = Base64::decode_vec("AQAB").unwrap();
let d = Base64::decode_vec(
"qQazSQ+FRN7nVK1bRsROMRB8AmsDwLVEHivlz1V3Td2Dr+oW3YUMgxedhztML1Id\
Expand All @@ -738,7 +739,7 @@ mod tests {
BYjcY7QFRm/9nupXMTH5hZ2qrHfCJIp0KK4tNBdQqmnHapFl5l6Le1s4qBS5bEIz\
jitobLvAFm9abPlDGfxmY6mlrMK4+nytwF9Ct7wc1AE=",
)
.unwrap();
.unwrap();
let primes = [
Base64::decode_vec(
"9kQWEAzsbzOcdPa+s5wFfw4XDd7bB1q9foZ31b1+TNjGNxbSBCFlDF1q98vwpV6n\
Expand All @@ -748,7 +749,7 @@ mod tests {
NC5zZOk7WzZatnCkN5H5WzalWtZuu0oVL205KPOa3R8V2yv5e6fm0v5fTmqSuvjm\
aMJLXCN4QJkmIzojO99ckQ==",
)
.unwrap(),
.unwrap(),
Base64::decode_vec(
"x8exdMjVA2CiI+Thx7loHtVcevoeE2sZ7btRVAvmBqo+lkHwxb7FHRnWvuj6eJSl\
D2f0T50EewIhhiW3R9BmktCk7hXjbSCnC1u9Oxc1IAUm/7azRqyfCMx43XhLxpD+\
Expand All @@ -757,7 +758,7 @@ mod tests {
bUIIRqr9Ii7Eswf9Vk8xp2O1Nt8nzcYS9PFD12M5eyaeFEkEYfpNMNGuTzp/31oq\
VjbpoCxS6vuWAZyADxhISQ==",
)
.unwrap(),
.unwrap(),
Base64::decode_vec(
"is7d0LY4HoXszlC2NO7gejkq7XqL4p1W6hZJPYTNx+r37t1CC2n3Vvzg6kNdpRix\
DhIpXVTLjN9O7UO/XuqSumYKJIKoP52eb4Tg+a3hw5Iz2Zsb5lUTNSLgkQSBPAf7\
Expand All @@ -766,7 +767,7 @@ mod tests {
3HbCPELqXgNJJkRfi6MP9kXa9lSfnZmoT081RMvqonB/FUa4HOcKyCrw9XZEtnbN\
CIdbitfDVEX+pSSD7596wQ==",
)
.unwrap(),
.unwrap(),
Base64::decode_vec(
"GPs0injugfycacaeIP5jMa/WX55VEnKLDHom4k6WlfDF4L4gIGoJdekcPEUfxOI5\
faKvHyFwRP1wObkPoRBDM0qZxRfBl4zEtpvjHrd5MibSyJkM8+J0BIKk/nSjbRIG\
Expand All @@ -775,7 +776,7 @@ mod tests {
RDdEl6mb+6FDgWuXVyqR9+904oanEIkbJ7vfkthagLbEf57dyG6nJlqh5FBZWxGI\
R72YGypPuAh7qnnqXXjY2Q==",
)
.unwrap(),
.unwrap(),
Base64::decode_vec(
"CUWC+hRWOT421kwRllgVjy6FYv6jQUcgDNHeAiYZnf5HjS9iK2ki7v8G5dL/0f+Y\
f+NhE/4q8w4m8go51hACrVpP1p8GJDjiT09+RsOzITsHwl+ceEKoe56ZW6iDHBLl\
Expand All @@ -784,7 +785,7 @@ mod tests {
Fqxj3N1+HrYLe/zs7LOaK0++F9Ul3tLelhrhsvLxei3oCZkF9A/foD3on3luYA+1\
cRcxWpSY3h2J4/22+yo4+Q==",
)
.unwrap(),
.unwrap(),
];

RsaPrivateKey::from_components(
Expand All @@ -793,7 +794,7 @@ mod tests {
BigUint::from_bytes_be(&d),
primes.iter().map(|p| BigUint::from_bytes_be(p)).collect(),
)
.unwrap();
.unwrap();
}

#[test]
Expand Down

0 comments on commit 2fa8773

Please sign in to comment.