botan
botan copied to clipboard
Improve API of asymmetric algorithms
This is a collection of potential API improvements found in #3609.
- [ ] Parameter consistency for generic key handling functions (
pk_algs.h) The generic functioncreate_private_key()takes string-typedalgo_nameandalgo_paramsto identify the algorithm (see also #3275). In contrastload_{public/private}_key()requires anAlgorithmIdentifierobject. This is inconvenient, as it requires the user to find a way to translate between the two. - [x] New method:
Public_Key::raw_public_key_bits()(https://github.com/randombit/botan/pull/3985) Currently, we providepublic_key_bits()("BER encoded public key bits") andsubject_public_key()("X.509 subject key encoding"). Especially the new PQC algorithms seem to converge on their own (concat-based) encodings. Key agreement public keys could make this an alias forPK_Key_Agreement_Key::public_value(). Other keys might treatraw_public_key_bits()as an alias for the existingpublic_key_bits(). - [x] New method:
std::unique_ptr<Private_Key> Public_Key::generate_another(RNG&)(https://github.com/randombit/botan/pull/3770) ... to generically generate an equivalent new key pair with the same algorithm (and configuration) as the public key at hand. Use case: Mapping a KEM interface using a Key Agreement algorithm. The abstract "Encaps()" function needs::generate_another()to conveniently create an ephemeral keypair without knowledge about the exact underlying algorithm. - [x] New method:
std::optional<uint64_t> Private_Key::remaining_operations() const(https://github.com/randombit/botan/pull/3821) LMS and XMSS both have stateful operation, but there is no generic way to detect how many operations remain on a key. The proposed method should returnnulloptfor non-stateful algorithms, otherwise the remaining number of valid usages. [1] - [ ] (to be discussed) Internal static access to algorithm parameters Allow accessing algorithm parameters (such as "shared key length", ...) statically. This would simplify some internal implementations that statically know which algorithm they are going to deal with. That might be useful not just for asym algos but in general (1, 2). For instance to avoid magic number such as: https://github.com/randombit/botan/pull/3933#discussion_r1522881729
Another one: LMS and XMSS both have stateful operation, but there is no generic way to detect how many operations remain on a key. I think the fix here is fairly easy, add std::optional<uint64_t> remaining_operations() const on Private_Key which returns nullopt for non-stateful algorithms.
For the record, here's an example of the hoops one needs to jump through to load a private key that is not wrapped in PKCS#8: https://github.com/randombit/botan/discussions/3902#discussioncomment-8476815.