Skip to content

Commit

Permalink
adds support for rsa in put_asymmetric_key
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Nov 22, 2023
1 parent e14de97 commit 576b1fb
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/mockhsm/object/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
use crate::{algorithm::Algorithm, asymmetric, authentication, hmac, opaque, wrap};
use ecdsa::elliptic_curve::sec1::ToEncodedPoint;
use ed25519_dalek as ed25519;
use num_traits::cast::FromPrimitive;
use rand_core::{OsRng, RngCore};
use rsa::traits::PublicKeyParts;
use rsa::{traits::PublicKeyParts, BigUint};

/// Loaded instances of a cryptographic primitives in the MockHsm
#[derive(Debug)]
Expand Down Expand Up @@ -53,6 +54,17 @@ impl Payload {
assert_eq!(data.len(), ed25519::SECRET_KEY_LENGTH);
Payload::Ed25519Key(ed25519::SigningKey::try_from(data).unwrap())
}
asymmetric::Algorithm::Rsa2048
| asymmetric::Algorithm::Rsa3072
| asymmetric::Algorithm::Rsa4096 => {
assert_eq!(data.len(), asymmetric_alg.key_len());
let exp = BigUint::from_u64(65537).expect("invalid static exponent");
let p = BigUint::from_bytes_be(&data[..asymmetric_alg.key_len() / 2]);
let q = BigUint::from_bytes_be(&data[asymmetric_alg.key_len() / 2..]);

let key = rsa::RsaPrivateKey::from_p_q(p, q, exp).unwrap();
Payload::RsaKey(key)
}
_ => {
panic!("MockHsm doesn't support this asymmetric algorithm: {asymmetric_alg:?}")
}
Expand Down

0 comments on commit 576b1fb

Please sign in to comment.