Skip to content

Commit 2936e1d

Browse files
committed
PublicKeyParts: provide serialization helpers
When serializing the exponent or the modulus from a key, `BoxedUint::to_be_bytes` will return a slice padded with zeroes on the left because of its inner representation. Not every implementation out there will handle leading zeroes. This provides a `PublicKeyParts::n_bytes`/`e_bytes` that will strip out those leading zeros.
1 parent b831df8 commit 2936e1d

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ pkcs8 = { git = "https://github.com/RustCrypto/formats.git" }
8585
# https://github.com/RustCrypto/password-hashes/pull/578
8686
pbkdf2 = { git = "https://github.com/baloo/password-hashes.git", branch = "baloo/hmac-0.13.0-pre.5" }
8787
scrypt = { git = "https://github.com/baloo/password-hashes.git", branch = "baloo/hmac-0.13.0-pre.5" }
88+
89+
crypto-bigint = { git = "https://github.com/baloo/crypto-bigint.git", branch = "baloo/boxeduint/trimmed" }

src/key.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ mod tests {
718718
let n_limbs: &[u64] = PublicKeyParts::n(&public_key).as_ref().as_ref();
719719
assert_eq!(n_limbs, &[101u64]);
720720
assert_eq!(PublicKeyParts::e(&public_key), &BoxedUint::from(200u64));
721+
assert_eq!(PublicKeyParts::e_bytes(&public_key), [200].into());
722+
assert_eq!(PublicKeyParts::n_bytes(&public_key), [101].into());
721723
}
722724

723725
fn test_key_basics(private_key: &RsaPrivateKey) {

src/traits/keys.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Traits related to the key components
22
3+
use alloc::boxed::Box;
34
use crypto_bigint::{
45
modular::{BoxedMontyForm, BoxedMontyParams},
56
BoxedUint, NonZero,
@@ -27,6 +28,16 @@ pub trait PublicKeyParts {
2728
fn n_bits_precision(&self) -> u32 {
2829
self.n().bits_precision()
2930
}
31+
32+
/// Returns the big endian serialization of the modulus of the key
33+
fn n_bytes(&self) -> Box<[u8]> {
34+
self.n().to_be_bytes_trimmed_vartime()
35+
}
36+
37+
/// Returns the big endian serialization of the public exponent of the key
38+
fn e_bytes(&self) -> Box<[u8]> {
39+
self.e().to_be_bytes_trimmed_vartime()
40+
}
3041
}
3142

3243
/// Components of an RSA private key.

0 commit comments

Comments
 (0)