From 1681d947100a219579febf4b882fb126d8e3a860 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Thu, 31 Aug 2023 22:43:29 +0200 Subject: [PATCH] openpgp-card-sequoia: add set_algorithm() (and remove algorithm setting from generate_key) Also add set_algorithm_attributes(). --- card-functionality/src/tests.rs | 9 ++++++--- openpgp-card-sequoia/src/lib.rs | 35 +++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/card-functionality/src/tests.rs b/card-functionality/src/tests.rs index 631f2e2..e7561e6 100644 --- a/card-functionality/src/tests.rs +++ b/card-functionality/src/tests.rs @@ -218,11 +218,13 @@ pub fn test_keygen(tx: &mut Card, param: &[&str]) -> Result, param: &[&str]) -> Result> { ) } - pub fn generate_key_simple( + /// Configure the key slot `key_type` to `algorithm_attributes`. + /// This can be useful in preparation for [`Self::generate_key`]. + /// + /// Note that legal values for [`AlgorithmAttributes`] are card-specific. + /// Different OpenPGP card implementations may support different + /// algorithms, sometimes with differing requirements for the encoding + /// (e.g. field sizes) + pub fn set_algorithm_attributes( &mut self, key_type: KeyType, - algo: Option, - ) -> Result<(PublicKeyMaterial, KeyGenerationTime), Error> { - if let Some(algo) = algo { - // set algorithm attributes - let attr = algo.matching_algorithm_attributes(self.card(), key_type)?; - self.card().set_algorithm_attributes(key_type, &attr)?; - } + algorithm_attributes: &AlgorithmAttributes, + ) -> Result<(), Error> { + self.card() + .set_algorithm_attributes(key_type, algorithm_attributes) + } + /// Configure the key slot `key_type` to the algorithm `algo`. + /// This can be useful in preparation for [`Self::generate_key`]. + pub fn set_algorithm(&mut self, key_type: KeyType, algo: AlgoSimple) -> Result<(), Error> { + let attr = algo.matching_algorithm_attributes(self.card(), key_type)?; + self.set_algorithm_attributes(key_type, &attr) + } + + /// Generate a new cryptographic key in slot `key_type`, with the currently + /// configured cryptographic algorithm + /// (see [`Self::set_algorithm`] for changing the algorithm setting). + pub fn generate_key( + &mut self, + key_type: KeyType, + ) -> Result<(PublicKeyMaterial, KeyGenerationTime), Error> { self.card().generate_key(Self::ptf, key_type) } }