Skip to content

Commit 0e3dd5b

Browse files
bjayanaxYogaraj-Alamenda
authored andcommitted
Remove legacy CRYPTO_THREAD_lock usage and update ECX key management for OpenSSL 3.x
- Eliminated conditional code for CRYPTO_THREAD_lock allocation and release in ECX key structures and related functions, aligning with updated OpenSSL 3.x threading and reference counting. - Refactored key management logic in qat_prov_kmgmt_ecx.c to implement direct parameter handling for X25519/X448 key types, including get/set/export functionality. - Introduced utility macros and constants for X25519/X448 bits and security levels. - Updated the OSSL_DISPATCH function tables to use new parameter and context management functions. - Improved memory handling and property query support for ECX keys and context structures. - Minor code cleanup and removal of compatibility indirection for OpenSSL <3.2.0. Signed-off-by: Raveesh Vemula <[email protected]>
1 parent 2871717 commit 0e3dd5b

File tree

6 files changed

+256
-171
lines changed

6 files changed

+256
-171
lines changed

e_qat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,13 +941,13 @@ static inline int QAT_CRYPTO_GET_REF(QAT_CRYPTO_REF_COUNT *refcnt, int *ret)
941941
return 1;
942942
}
943943

944-
static inline int QAT_CRYPTO_UP_REF(QAT_CRYPTO_REF_COUNT* refcnt, int* ret)
944+
static inline int QAT_CRYPTO_UP_REF(QAT_CRYPTO_REF_COUNT *refcnt, int *ret)
945945
{
946946
*ret = atomic_fetch_add_explicit(&refcnt->val, 1, memory_order_relaxed) + 1;
947947
return 1;
948948
}
949949

950-
static inline int QAT_CRYPTO_DOWN_REF(QAT_CRYPTO_REF_COUNT* refcnt, int* ret)
950+
static inline int QAT_CRYPTO_DOWN_REF(QAT_CRYPTO_REF_COUNT *refcnt, int *ret)
951951
{
952952
*ret = atomic_fetch_sub_explicit(&refcnt->val, 1, memory_order_release) - 1;
953953
if (*ret == 0)

qat_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454

5555
# define MAX_KEYLEN 57
5656

57+
# define X25519_BITS 253
58+
# define X25519_SECURITY_BITS 128
59+
60+
# define X448_BITS 448
61+
# define X448_SECURITY_BITS 224
62+
5763
typedef struct {
5864
_Atomic int val;
5965
}QAT_CRYPTO_REF_COUNT;

qat_hw_ecx.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ static int qat_pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey, int type)
215215
* count accordingly, otherwise it will trigger openssl's panic.
216216
*/
217217
#if defined(QAT_OPENSSL_3) && defined(QAT_OPENSSL_PROVIDER)
218-
# if OPENSSL_VERSION_NUMBER < 0x30200000
219-
key->lock = CRYPTO_THREAD_lock_new();
220-
# endif
221218
key->references.val = 1;
222219
# ifdef QAT_OPENSSL_PROVIDER
223220
key->type = gctx->type;
@@ -265,22 +262,12 @@ static int qat_pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey, int type)
265262
default:
266263
WARN("Unsupported NID: %d\n", gctx->type);
267264
QATerr(QAT_F_QAT_PKEY_ECX_KEYGEN, ERR_R_INTERNAL_ERROR);
268-
#ifdef QAT_OPENSSL_3
269-
# if OPENSSL_VERSION_NUMBER < 0x30200000
270-
CRYPTO_THREAD_lock_free(key->lock);
271-
# endif
272-
#endif
273265
OPENSSL_free(key);
274266
return 0;
275267
}
276268

277269
if (qat_get_qat_offload_disabled()) {
278270
DEBUG("- Switched to software mode.\n");
279-
#ifdef QAT_OPENSSL_3
280-
# if OPENSSL_VERSION_NUMBER < 0x30200000
281-
CRYPTO_THREAD_lock_free(key->lock);
282-
# endif
283-
#endif
284271
OPENSSL_free(key);
285272

286273
if (!is_ecx_448) {
@@ -303,11 +290,6 @@ static int qat_pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey, int type)
303290
goto err;
304291
} else {
305292
QATerr(QAT_F_QAT_PKEY_ECX_KEYGEN, ERR_R_INTERNAL_ERROR);
306-
#if defined(QAT_OPENSSL_3) && defined(QAT_OPENSSL_PROVIDER)
307-
# if OPENSSL_VERSION_NUMBER < 0x30200000
308-
CRYPTO_THREAD_lock_free(key->lock);
309-
# endif
310-
#endif
311293
OPENSSL_free(key);
312294
return 0;
313295
}
@@ -321,11 +303,6 @@ static int qat_pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey, int type)
321303
if (NULL == qat_ecx_op_data) {
322304
WARN("Failed to allocate memory for qat_ecx_op_data\n");
323305
QATerr(QAT_F_QAT_PKEY_ECX_KEYGEN, ERR_R_MALLOC_FAILURE);
324-
#if defined(QAT_OPENSSL_3) && defined(QAT_OPENSSL_PROVIDER)
325-
# if OPENSSL_VERSION_NUMBER < 0x30200000
326-
CRYPTO_THREAD_lock_free(key->lock);
327-
# endif
328-
#endif
329306
OPENSSL_free(key);
330307
return 0;
331308
}
@@ -572,11 +549,6 @@ static int qat_pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey, int type)
572549
privkey = NULL;
573550
}
574551
if (NULL != key) {
575-
#if defined(QAT_OPENSSL_3) && defined(QAT_OPENSSL_PROVIDER)
576-
# if OPENSSL_VERSION_NUMBER < 0x30200000
577-
CRYPTO_THREAD_lock_free(key->lock);
578-
# endif
579-
#endif
580552
OPENSSL_free(key);
581553
key = NULL;
582554
}

qat_prov_ecx.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
# define ED448_KEYLEN 57
5656
# define MAX_KEYLEN 57
5757

58+
#define QAT_ECX_KEY_TYPES() \
59+
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), \
60+
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
61+
5862
typedef struct{
5963
int id; /* libcrypto internal */
6064
int name_id;
@@ -145,9 +149,6 @@ typedef struct qat_ecx_key_st {
145149
size_t keylen;
146150
ECX_KEY_TYPE type;
147151
QAT_CRYPTO_REF_COUNT references;
148-
#if OPENSSL_VERSION_NUMBER < 0x30200000
149-
CRYPTO_RWLOCK *lock;
150-
#endif
151152
}ECX_KEY;
152153

153154
typedef struct {

qat_prov_exch_ecx.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ int qat_ecx_key_up_ref(ECX_KEY *key)
166166
if (QAT_CRYPTO_UP_REF(&key->references, &i) <= 0)
167167
return 0;
168168

169-
if(i < 2) {
169+
if (i < 2) {
170170
WARN("refcount error");
171171
return 0;
172172
}
173173

174-
return ((i >1) ? 1 : 0);
174+
return ((i > 1) ? 1 : 0);
175175
}
176176

177177
void qat_ecx_key_free(ECX_KEY *key)
@@ -195,9 +195,6 @@ void qat_ecx_key_free(ECX_KEY *key)
195195

196196
OPENSSL_free(key->propq);
197197
OPENSSL_secure_clear_free(key->privkey, key->keylen);
198-
#if OPENSSL_VERSION_NUMBER < 0x30200000
199-
CRYPTO_THREAD_lock_free(key->lock);
200-
#endif
201198
OPENSSL_free(key);
202199
#ifdef ENABLE_QAT_FIPS
203200
qat_fips_key_zeroize = 1;
@@ -260,11 +257,31 @@ static void qat_ecx_freectx(void *vecxctx)
260257

261258
static void *qat_ecx_dupctx(void *vecxctx)
262259
{
263-
typedef void * (*fun_ptr)(void *vecxctx);
264-
fun_ptr fun = get_default_x25519_keyexch().dupctx;
265-
if (!fun)
260+
QAT_ECX_CTX *srcctx = (QAT_ECX_CTX *)vecxctx;
261+
QAT_ECX_CTX *dstctx;
262+
263+
if (!qat_prov_is_running())
264+
return NULL;
265+
266+
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
267+
if (dstctx == NULL)
268+
return NULL;
269+
270+
*dstctx = *srcctx;
271+
if (dstctx->key != NULL && !qat_ecx_key_up_ref(dstctx->key)) {
272+
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
273+
OPENSSL_free(dstctx);
274+
return NULL;
275+
}
276+
277+
if (dstctx->peerkey != NULL && !qat_ecx_key_up_ref(dstctx->peerkey)) {
278+
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
279+
qat_ecx_key_free(dstctx->key);
280+
OPENSSL_free(dstctx);
266281
return NULL;
267-
return fun(vecxctx);
282+
}
283+
284+
return dstctx;
268285
}
269286

270287
const OSSL_DISPATCH qat_X25519_keyexch_functions[] = {

0 commit comments

Comments
 (0)