From fefd049be7f5b9c2d6e80efc0c4d877b80e1ca6c Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Tue, 27 Feb 2024 22:21:12 -0800 Subject: [PATCH] wrap: add support for ecdsa wrapping --- Cargo.lock | 2 +- src/wrap/message.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ea6d4e2..903a1648 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -991,7 +991,7 @@ dependencies = [ [[package]] name = "yubihsm" -version = "0.42.1" +version = "0.43.0-pre" dependencies = [ "aes", "bitflags", diff --git a/src/wrap/message.rs b/src/wrap/message.rs index 9005acc0..f2ffefa2 100644 --- a/src/wrap/message.rs +++ b/src/wrap/message.rs @@ -147,6 +147,42 @@ impl Plaintext { } } + /// Build a [`Plaintext`] from an [`RsaPrivateKey`]. + pub fn from_ecdsa( + algorithm: Algorithm, + object_id: object::Id, + capabilities: Capability, + domains: Domain, + label: object::Label, + key: SecretKey, + ) -> Result + where + C: PrimeCurve + CurveAlgorithm, + FieldBytesSize: ModulusSize + Unsigned, + { + let asym_algorithm = C::asymmetric_algorithm(); + + let object_info = wrap::Info { + capabilities, + object_id, + length: 0, + domains, + object_type: object::Type::AsymmetricKey, + algorithm: algorithm::Algorithm::Asymmetric(asym_algorithm), + sequence: 0, + origin: object::Origin::Imported, + label, + }; + + let data = key.to_bytes().as_slice().to_vec(); + + Ok(Self { + algorithm, + object_info, + data, + }) + } + /// Return the rsa key of this [`Plaintext`] if it was an RSA key. pub fn rsa(&self) -> Option { let (component_size, modulus_size) = match self.object_info.algorithm { @@ -187,7 +223,10 @@ impl Plaintext { length: 0, domains, object_type: object::Type::AsymmetricKey, - algorithm: algorithm::Algorithm::Asymmetric(asymmetric::Algorithm::Rsa2048), + algorithm: algorithm::Algorithm::Asymmetric( + // This is rewritten a couple lines below + asymmetric::Algorithm::Rsa2048, + ), sequence: 0, origin: object::Origin::Imported, label,