|
19 | 19 | #include <cstring> |
20 | 20 | #if OPENSSL_VERSION_MAJOR >= 3 |
21 | 21 | #include <openssl/provider.h> |
| 22 | +#endif |
| 23 | +#if OPENSSL_WITH_PQC |
| 24 | +struct PQCMapping { |
| 25 | + const char* name; |
| 26 | + int nid; |
| 27 | +}; |
| 28 | + |
| 29 | +constexpr static PQCMapping pqc_mappings[] = { |
| 30 | + {"ML-DSA-44", EVP_PKEY_ML_DSA_44}, |
| 31 | + {"ML-DSA-65", EVP_PKEY_ML_DSA_65}, |
| 32 | + {"ML-DSA-87", EVP_PKEY_ML_DSA_87}, |
| 33 | + {"ML-KEM-512", EVP_PKEY_ML_KEM_512}, |
| 34 | + {"ML-KEM-768", EVP_PKEY_ML_KEM_768}, |
| 35 | + {"ML-KEM-1024", EVP_PKEY_ML_KEM_1024}, |
| 36 | +}; |
| 37 | + |
22 | 38 | #endif |
23 | 39 |
|
24 | 40 | // EVP_PKEY_CTX_set_dsa_paramgen_q_bits was added in OpenSSL 1.1.1e. |
@@ -2119,11 +2135,21 @@ int EVPKeyPointer::id(const EVP_PKEY* key) { |
2119 | 2135 | if (key == nullptr) return 0; |
2120 | 2136 | int type = EVP_PKEY_id(key); |
2121 | 2137 | #if OPENSSL_WITH_PQC |
| 2138 | + // EVP_PKEY_id returns -1 when EVP_PKEY_* is only implemented in a provider |
| 2139 | + // which is the case for all post-quantum NIST algorithms |
| 2140 | + // one suggested way would be to use a chain of `EVP_PKEY_is_a` |
2122 | 2141 | // https://github.com/openssl/openssl/issues/27738#issuecomment-3013215870 |
| 2142 | + // or, this way there are less calls to the OpenSSL provider, just |
| 2143 | + // getting the name once |
2123 | 2144 | if (type == -1) { |
2124 | | - if (EVP_PKEY_is_a(key, "ML-DSA-44")) return EVP_PKEY_ML_DSA_44; |
2125 | | - if (EVP_PKEY_is_a(key, "ML-DSA-65")) return EVP_PKEY_ML_DSA_65; |
2126 | | - if (EVP_PKEY_is_a(key, "ML-DSA-87")) return EVP_PKEY_ML_DSA_87; |
| 2145 | + const char* type_name = EVP_PKEY_get0_type_name(key); |
| 2146 | + if (type_name == nullptr) return -1; |
| 2147 | + |
| 2148 | + for (const auto& mapping : pqc_mappings) { |
| 2149 | + if (strcmp(type_name, mapping.name) == 0) { |
| 2150 | + return mapping.nid; |
| 2151 | + } |
| 2152 | + } |
2127 | 2153 | } |
2128 | 2154 | #endif |
2129 | 2155 | return type; |
|
0 commit comments