|
20 | 20 | #include <cstring> |
21 | 21 | #if OPENSSL_VERSION_MAJOR >= 3 |
22 | 22 | #include <openssl/provider.h> |
| 23 | +#endif |
| 24 | +#if OPENSSL_WITH_PQC |
| 25 | +struct PQCMapping { |
| 26 | + const char* name; |
| 27 | + int nid; |
| 28 | +}; |
| 29 | + |
| 30 | +constexpr static PQCMapping pqc_mappings[] = { |
| 31 | + {"ML-DSA-44", EVP_PKEY_ML_DSA_44}, |
| 32 | + {"ML-DSA-65", EVP_PKEY_ML_DSA_65}, |
| 33 | + {"ML-DSA-87", EVP_PKEY_ML_DSA_87}, |
| 34 | + {"ML-KEM-512", EVP_PKEY_ML_KEM_512}, |
| 35 | + {"ML-KEM-768", EVP_PKEY_ML_KEM_768}, |
| 36 | + {"ML-KEM-1024", EVP_PKEY_ML_KEM_1024}, |
| 37 | +}; |
| 38 | + |
23 | 39 | #endif |
24 | 40 |
|
25 | 41 | // EVP_PKEY_CTX_set_dsa_paramgen_q_bits was added in OpenSSL 1.1.1e. |
@@ -2067,11 +2083,21 @@ int EVPKeyPointer::id(const EVP_PKEY* key) { |
2067 | 2083 | if (key == nullptr) return 0; |
2068 | 2084 | int type = EVP_PKEY_id(key); |
2069 | 2085 | #if OPENSSL_WITH_PQC |
| 2086 | + // EVP_PKEY_id returns -1 when EVP_PKEY_* is only implemented in a provider |
| 2087 | + // which is the case for all post-quantum NIST algorithms |
| 2088 | + // one suggested way would be to use a chain of `EVP_PKEY_is_a` |
2070 | 2089 | // https://github.com/openssl/openssl/issues/27738#issuecomment-3013215870 |
| 2090 | + // or, this way there are less calls to the OpenSSL provider, just |
| 2091 | + // getting the name once |
2071 | 2092 | if (type == -1) { |
2072 | | - if (EVP_PKEY_is_a(key, "ML-DSA-44")) return EVP_PKEY_ML_DSA_44; |
2073 | | - if (EVP_PKEY_is_a(key, "ML-DSA-65")) return EVP_PKEY_ML_DSA_65; |
2074 | | - if (EVP_PKEY_is_a(key, "ML-DSA-87")) return EVP_PKEY_ML_DSA_87; |
| 2093 | + const char* type_name = EVP_PKEY_get0_type_name(key); |
| 2094 | + if (type_name == nullptr) return -1; |
| 2095 | + |
| 2096 | + for (const auto& mapping : pqc_mappings) { |
| 2097 | + if (strcmp(type_name, mapping.name) == 0) { |
| 2098 | + return mapping.nid; |
| 2099 | + } |
| 2100 | + } |
2075 | 2101 | } |
2076 | 2102 | #endif |
2077 | 2103 | return type; |
|
0 commit comments