From 10bdb32c45cc659a7e93d71fbdc5fc3f59a724f8 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Thu, 2 Dec 2021 13:13:28 +0100 Subject: [PATCH] Refactor determine_rsa_attrs() to be more easily reusable for key generation. --- openpgp-card/src/keys.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/openpgp-card/src/keys.rs b/openpgp-card/src/keys.rs index 940541b..d823f1b 100644 --- a/openpgp-card/src/keys.rs +++ b/openpgp-card/src/keys.rs @@ -187,8 +187,12 @@ pub(crate) fn key_import( let (algo, key_cmd) = match key.private_key()? { PrivateKeyMaterial::R(rsa_key) => { + // RSA bitsize + // (round up to 4-bytes, in case the key has 8+ leading zero bits) + let rsa_bits = (((rsa_key.n().len() * 8 + 31) / 32) * 32) as u16; + let rsa_attrs = - determine_rsa_attrs(&ard, &*rsa_key, key_type, algo_list)?; + determine_rsa_attrs(rsa_bits, key_type, &ard, algo_list)?; let key_cmd = rsa_key_import_cmd(key_type, rsa_key, &rsa_attrs)?; @@ -221,21 +225,17 @@ pub(crate) fn key_import( Ok(()) } -/// Derive RsaAttrs for `rsa_key`. +/// Determine RsaAttrs for the current card, for an `rsa_bits` sized key. /// /// If available, via lookup in `algo_list`, otherwise the current -/// algorithm attributes are loaded and checked. If neither method yields a +/// algorithm attributes are checked. If neither method yields a /// result, we 'guess' the RsaAttrs setting. fn determine_rsa_attrs( - ard: &ApplicationRelatedData, - rsa_key: &dyn RSAKey, + rsa_bits: u16, key_type: KeyType, + ard: &ApplicationRelatedData, algo_list: Option, ) -> Result { - // RSA bitsize - // (round up to 4-bytes, in case the key has 8+ leading zeros) - let rsa_bits = (((rsa_key.n().len() * 8 + 31) / 32) * 32) as u16; - // Figure out suitable RSA algorithm parameters: // Does the card offer a list of algorithms?