From 9761e0e66467a77628f4db9c4b15dd35a9ce3a6b Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Tue, 29 Aug 2023 15:07:31 +0200 Subject: [PATCH] openpgp-card: internal API cleanup, continued --- openpgp-card/src/algorithm.rs | 19 ++++++++++--------- openpgp-card/src/lib.rs | 9 +++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/openpgp-card/src/algorithm.rs b/openpgp-card/src/algorithm.rs index 590723d..9be5a35 100644 --- a/openpgp-card/src/algorithm.rs +++ b/openpgp-card/src/algorithm.rs @@ -12,7 +12,6 @@ use std::convert::TryFrom; use std::fmt; -use crate::card_do::ApplicationRelatedData; use crate::crypto_data::EccType; use crate::{keys, oid, Error, KeyType}; @@ -77,38 +76,40 @@ impl AlgoSimple { /// Return the appropriate Algo for this AlgoSimple. /// - /// This mapping differs between cards, based on `ard` and `algo_info` - /// (e.g. the exact Algo variant can have a different size for e, in RSA; - /// also, the import_format can differ). + /// This mapping depends on the actual card in use + /// (e.g.: the size of "e", in RSA can differ; + /// or a different `import_format` can be selected). + /// + /// These card-specific settings are derived from `algorithm_attributes` and `algo_info`. pub(crate) fn determine_algo_attributes( &self, key_type: KeyType, - ard: &ApplicationRelatedData, + algorithm_attributes: AlgorithmAttributes, algo_info: Option, ) -> Result { let algo = match self { Self::RSA1k => AlgorithmAttributes::Rsa(keys::determine_rsa_attrs( 1024, key_type, - ard.algorithm_attributes(key_type)?, + algorithm_attributes, algo_info, )?), Self::RSA2k => AlgorithmAttributes::Rsa(keys::determine_rsa_attrs( 2048, key_type, - ard.algorithm_attributes(key_type)?, + algorithm_attributes, algo_info, )?), Self::RSA3k => AlgorithmAttributes::Rsa(keys::determine_rsa_attrs( 3072, key_type, - ard.algorithm_attributes(key_type)?, + algorithm_attributes, algo_info, )?), Self::RSA4k => AlgorithmAttributes::Rsa(keys::determine_rsa_attrs( 4096, key_type, - ard.algorithm_attributes(key_type)?, + algorithm_attributes, algo_info, )?), Self::NIST256 => AlgorithmAttributes::Ecc(keys::determine_ecc_attrs( diff --git a/openpgp-card/src/lib.rs b/openpgp-card/src/lib.rs index 9a84926..fcb3844 100644 --- a/openpgp-card/src/lib.rs +++ b/openpgp-card/src/lib.rs @@ -1242,14 +1242,11 @@ impl<'a> Transaction<'a> { simple: AlgoSimple, ) -> Result<(PublicKeyMaterial, KeyGenerationTime), Error> { let ard = self.application_related_data()?; - let algo_info = if let Ok(ai) = self.algorithm_information() { - ai - } else { - None - }; + let algorithm_attributes = ard.algorithm_attributes(key_type)?; - let algo = simple.determine_algo_attributes(key_type, &ard, algo_info)?; + let algo_info = self.algorithm_information().ok().flatten(); + let algo = simple.determine_algo_attributes(key_type, algorithm_attributes, algo_info)?; Self::generate_key(self, fp_from_pub, key_type, Some(&algo)) }