summaryrefslogtreecommitdiffstats
path: root/crypto/provider_core.c
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2020-05-15 15:56:05 +0200
committerRichard Levitte <levitte@openssl.org>2020-05-19 11:02:41 +0200
commit5a29b6286f8ccafc2ed9a026b0e8d4bd6d0396e6 (patch)
tree7addeebb9956f0e747bb0c1175e0e79eecb7b762 /crypto/provider_core.c
parentc0ec5ce0bf97c358bea29c81d3d16047244a9a7e (diff)
CORE: query for operations only once per provider (unless no_store is true)
When a desired algorithm wasn't available, we didn't register anywhere that an attempt had been made, with the result that next time the same attempt was made, the whole process would be done again. To avoid this churn, we register a bit for each operation that has been queried in the libcrypto provider object, and test it before trying the same query and method construction loop again. If course, if the provider has told us not to cache, we don't register this bit. Fixes #11814 Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/11842)
Diffstat (limited to 'crypto/provider_core.c')
-rw-r--r--crypto/provider_core.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/crypto/provider_core.c b/crypto/provider_core.c
index 662576cd7b..0c21660080 100644
--- a/crypto/provider_core.c
+++ b/crypto/provider_core.c
@@ -71,6 +71,13 @@ struct ossl_provider_st {
OSSL_provider_get_params_fn *get_params;
OSSL_provider_query_operation_fn *query_operation;
+ /*
+ * Cache of bit to indicate of query_operation() has been called on
+ * a specific operation or not.
+ */
+ unsigned char *operation_bits;
+ size_t operation_bits_sz;
+
/* Provider side data */
void *provctx;
};
@@ -317,6 +324,9 @@ void ossl_provider_free(OSSL_PROVIDER *prov)
}
# endif
#endif
+ OPENSSL_free(prov->operation_bits);
+ prov->operation_bits = NULL;
+ prov->operation_bits_sz = 0;
prov->flag_initialized = 0;
}
@@ -782,6 +792,42 @@ const OSSL_ALGORITHM *ossl_provider_query_operation(const OSSL_PROVIDER *prov,
return prov->query_operation(prov->provctx, operation_id, no_cache);
}
+int ossl_provider_set_operation_bit(OSSL_PROVIDER *provider, size_t bitnum)
+{
+ size_t byte = bitnum / 8;
+ unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
+
+ if (provider->operation_bits_sz <= byte) {
+ provider->operation_bits = OPENSSL_realloc(provider->operation_bits,
+ byte + 1);
+ if (provider->operation_bits == NULL) {
+ ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ memset(provider->operation_bits + provider->operation_bits_sz,
+ '\0', byte + 1 - provider->operation_bits_sz);
+ }
+ provider->operation_bits[byte] |= bit;
+ return 1;
+}
+
+int ossl_provider_test_operation_bit(OSSL_PROVIDER *provider, size_t bitnum,
+ int *result)
+{
+ size_t byte = bitnum / 8;
+ unsigned char bit = (1 << (bitnum % 8)) & 0xFF;
+
+ if (!ossl_assert(result != NULL)) {
+ ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
+ return 0;
+ }
+
+ *result = 0;
+ if (provider->operation_bits_sz > byte)
+ *result = ((provider->operation_bits[byte] & bit) != 0);
+ return 1;
+}
+
/*-
* Core functions for the provider
* ===============================