diff --git a/cmn_mem_drv_inf.c b/cmn_mem_drv_inf.c index 54fafa7e..a6172ec3 100644 --- a/cmn_mem_drv_inf.c +++ b/cmn_mem_drv_inf.c @@ -51,6 +51,8 @@ #include "cmn_mem_drv_inf.h" #include "qae_mem.h" +#define unlikely(x) __builtin_expect (!!(x), 0) + static pthread_mutex_t mem_mutex = PTHREAD_MUTEX_INITIALIZER; static int crypto_inited = 0; @@ -70,7 +72,7 @@ void qaeCryptoMemFree(void *ptr) MEM_DEBUG("Address: %p\n", ptr); - if (NULL == ptr) { + if (unlikely(NULL == ptr)) { MEM_WARN("qaeCryptoMemFree trying to free NULL pointer.\n"); return; } @@ -92,6 +94,7 @@ void qaeCryptoMemFree(void *ptr) void *qaeCryptoMemAlloc(size_t memsize, const char *file, int line) { + /* Input params should already have been sanity-checked by calling function. */ int rc; void *pAddress = NULL; @@ -119,6 +122,7 @@ void *qaeCryptoMemRealloc(void *ptr, size_t memsize, const char *file, { void *nptr; + /* copyAllocPinnedMemory() will check the input params. */ nptr = copyAllocPinnedMemory(ptr, memsize, file, line); if (nptr) { qaeCryptoMemFree(ptr); @@ -132,11 +136,7 @@ void *qaeCryptoMemReallocClean(void *ptr, size_t memsize, { void *nptr; - if (original_size > memsize) { - MEM_WARN("original_size : %zd > memsize : %zd", original_size, memsize); - return NULL; - } - + /* copyAllocPinnedMemoryClean() checks the input params. */ nptr = copyAllocPinnedMemoryClean(ptr, memsize, original_size, file, line); if (nptr) { @@ -150,8 +150,11 @@ void *copyAllocPinnedMemory(void *ptr, size_t size, const char *file, { void *nptr; - if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { - MEM_WARN("pinned memory allocation failure\n"); + if (unlikely((ptr == NULL) || + (size == 0) || + (file == NULL) || + ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL))) { + MEM_WARN("Pinned memory allocation failure\n"); return NULL; } memcpy(nptr, ptr, size); @@ -163,19 +166,31 @@ void *copyAllocPinnedMemoryClean(void *ptr, size_t size, size_t original_size, { void *nptr; + if (unlikely((ptr == NULL) || + (size == 0) || + (original_size == 0) || + (file == NULL))) { + MEM_WARN("Invalid input params.\n"); + return NULL; + } + if (original_size > size) { + MEM_WARN("original_size : %zd > size : %zd", original_size, size); + return NULL; + } if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { - MEM_WARN("pinned memory allocation failure\n"); + MEM_WARN("Clean pinned memory allocation failure\n"); return NULL; } + memcpy(nptr, ptr, original_size); return nptr; } int copyFreePinnedMemory(void *uptr, void *kptr, int size) { - if (uptr == NULL || kptr == NULL) { - MEM_WARN("Input pointers uptr or kptr are NULL\n"); - return 0; + if (uptr == NULL || kptr == NULL || size <= 0) { + MEM_WARN("Input pointers uptr or kptr are NULL, or size invalid.\n"); + return 0; } memcpy(uptr, kptr, size); @@ -185,6 +200,10 @@ int copyFreePinnedMemory(void *uptr, void *kptr, int size) CpaPhysicalAddr qaeCryptoMemV2P(void *v) { + if (v == NULL) { + MEM_WARN("NULL address passed to function\n"); + return (CpaPhysicalAddr)0; + } return qaeVirtToPhysNUMA(v); } diff --git a/e_qat.c b/e_qat.c index 40b19bfc..d2505b86 100644 --- a/e_qat.c +++ b/e_qat.c @@ -767,7 +767,7 @@ qat_engine_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) case QAT_CMD_SET_CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD: #ifndef OPENSSL_ENABLE_QAT_SMALL_PACKET_CIPHER_OFFLOADS - if (p) { + if (p != NULL) { char *token; char str_p[QAT_MAX_INPUT_STRING_LENGTH]; char *itr = str_p; @@ -924,7 +924,7 @@ int qat_engine_finish_int(ENGINE *e, int reset_globals) * if requested, i.e. when we are not re-initializing the engine after * forking */ - if (reset_globals) { + if (reset_globals == 1) { enable_external_polling = 0; enable_inline_polling = 0; enable_event_driven_polling = 0; diff --git a/multi_thread_qaememutils.c b/multi_thread_qaememutils.c index 25fec9da..fbb46a60 100644 --- a/multi_thread_qaememutils.c +++ b/multi_thread_qaememutils.c @@ -115,6 +115,8 @@ static int slot_sizes_available[] = { SLOT_32_KILOBYTES }; +#define unlikely(x) __builtin_expect (!!(x), 0) + typedef struct _qae_slot { struct _qae_slot *next; int sig; @@ -247,11 +249,13 @@ void *copyAllocPinnedMemory(void *ptr, size_t size, const char *file, { void *nptr; - if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { + if (unlikely((ptr == NULL) || + (size == 0) || + (file == NULL) || + ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL))) { MEM_WARN("pinned memory allocation failure\n"); return NULL; } - memcpy(nptr, ptr, size); return nptr; } @@ -280,10 +284,21 @@ void *copyAllocPinnedMemoryClean(void *ptr, size_t size, size_t original_size, { void *nptr; - if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { + if (unlikely((ptr == NULL) || + (size == 0) || + (original_size == 0) || + (file == NULL))) { MEM_WARN("pinned memory allocation failure\n"); return NULL; } + if (original_size > size) { + MEM_WARN("original_size : %zd > size : %zd", original_size, size); + return NULL; + } + if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { + MEM_WARN("Clean pinned memory allocation failure\n"); + return NULL; + } memcpy(nptr, ptr, original_size); return nptr; @@ -304,16 +319,14 @@ void *copyAllocPinnedMemoryClean(void *ptr, size_t size, size_t original_size, ******************************************************************************/ int copyFreePinnedMemory(void *uptr, void *kptr, int size) { - if (uptr == NULL || kptr == NULL) { - MEM_WARN("Input pointers uptr or kptr are NULL\n"); + if (unlikely(uptr == NULL || kptr == NULL || size <= 0)) { + MEM_WARN("Input pointers uptr or kptr are NULL, or size invalid.\n"); return 0; } - if (size > MAX_ALLOC) { MEM_WARN("Size greater than MAX_ALLOC\n"); return 0; } - memcpy(uptr, kptr, size); qaeCryptoMemFree(kptr); return 1; @@ -924,9 +937,9 @@ CpaPhysicalAddr qaeCryptoMemV2P(void *v) qat_contig_mem_config *memCfg = NULL; void *pVirtPageAddress = NULL; ptrdiff_t offset = 0; - if(v == NULL) { + if (unlikely(v == NULL)) { MEM_WARN("NULL address passed to function\n"); - return (CpaPhysicalAddr) 0; + return (CpaPhysicalAddr)0; } /* Get the physical address contained in the slab @@ -942,7 +955,7 @@ CpaPhysicalAddr qaeCryptoMemV2P(void *v) if(memCfg->signature == QAT_CONTIG_MEM_ALLOC_SIG) return (CpaPhysicalAddr)(memCfg->physicalAddress + offset); MEM_WARN("Virtual to Physical memory lookup failure\n"); - return (CpaPhysicalAddr) 0; + return (CpaPhysicalAddr)0; } /************************************** @@ -964,9 +977,10 @@ CpaPhysicalAddr qaeCryptoMemV2P(void *v) ******************************************************************************/ void *qaeCryptoMemAlloc(size_t memsize, const char *file, int line) { + /* Input params should already have been sanity-checked by calling function. */ void *pAddress = crypto_alloc_from_slab(memsize, file, line); - MEM_DEBUG("Address: %p Size: %lu File: %s:%d\n", pAddress, - memsize, file, line); + MEM_DEBUG("Address: %p Size: %lu File: %s:%d\n", + pAddress, memsize, file, line); return pAddress; } @@ -983,8 +997,13 @@ void *qaeCryptoMemAlloc(size_t memsize, const char *file, int line) void qaeCryptoMemFree(void *ptr) { MEM_DEBUG("Address: %p\n", ptr); - if (NULL != ptr) - crypto_free_to_slab(ptr); + { + if (NULL != ptr) + crypto_free_to_slab(ptr); + else { + MEM_WARN("qaeCryptoMemFree trying to free NULL pointer.\n"); + } + } } /****************************************************************************** @@ -1006,6 +1025,13 @@ void qaeCryptoMemFree(void *ptr) void *qaeCryptoMemRealloc(void *ptr, size_t memsize, const char *file, int line) { + if (unlikely((ptr == NULL) || + (memsize == 0) || + (file == NULL))) { + MEM_WARN("Input parameter invalid.\n"); + return NULL; + } + int copy = crypto_slot_get_size(ptr); void *n = crypto_alloc_from_slab(memsize, file, line); if (n == NULL) { @@ -1046,6 +1072,18 @@ void *qaeCryptoMemReallocClean(void *ptr, size_t memsize, size_t original_size, const char *file, int line) { + if (unlikely((ptr == NULL) || + (memsize == 0) || + (original_size == 0) || + (file == NULL))) { + MEM_WARN("Input param. invalid.\n"); + return NULL; + } + if (original_size > memsize) { + MEM_WARN("original_size : %zd > memsize : %zd", original_size, memsize); + return NULL; + } + int copy = crypto_slot_get_size(ptr); void *n = crypto_alloc_from_slab(memsize, file, line); if (n == NULL) { diff --git a/qae_mem_utils.c b/qae_mem_utils.c index aa92999f..1bc1395f 100644 --- a/qae_mem_utils.c +++ b/qae_mem_utils.c @@ -68,6 +68,7 @@ * Error from file descriptor operation */ #define FD_ERROR -1 +#define unlikely(x) __builtin_expect (!!(x), 0) /* flag for mutex lock */ static int crypto_inited = 0; @@ -91,7 +92,7 @@ static pthread_mutex_t crypto_bsal = PTHREAD_MUTEX_INITIALIZER; * fragmentation and also to reduce cost of allocation There are nine * predefined slot sizes: 128 bytes, 256 bytes, 512 bytes, 1024 bytes, * 2048 bytes, 4096 bytes, 8192 bytes, 16384 bytes and 32768 bytes. - * Slabs are 128KB in size. Each slot has an overhead of a qae_slot + * Slabs are 128KB in size. Each slot has an overhead of a qae_slot * structure plus QAE_BYTE_ALIGNMENT bytes. The slab also has an * overhead of a qae_slab structure plus QAE_BYTE_ALIGNMENT bytes * so the full 128KB is not available for allocation or splitting into @@ -261,16 +262,13 @@ void *copyAllocPinnedMemory(void *ptr, size_t size, const char *file, { void *nptr; - if (ptr == NULL) { - MEM_WARN("Input pointer is NULL\n"); - return NULL; - } - - if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { + if (unlikely((ptr == NULL) || + (size == 0) || + (file == NULL) || + ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL))) { MEM_WARN("pinned memory allocation failure\n"); return NULL; } - memcpy(nptr, ptr, size); return nptr; } @@ -299,16 +297,17 @@ void *copyAllocPinnedMemoryClean(void *ptr, size_t size, size_t original_size, { void *nptr; - if (ptr == NULL) { - MEM_WARN("Input pointer is NULL\n"); + if (unlikely(ptr == NULL || + size == 0 || + original_size == 0 || + file == NULL)) { + MEM_WARN("Invalid input params.\n"); return NULL; } - if (original_size > size) { MEM_WARN("original_size : %zd > size : %zd", original_size, size); return NULL; } - if ((nptr = qaeCryptoMemAlloc(size, file, line)) == NULL) { MEM_WARN("pinned memory allocation failure\n"); return NULL; @@ -333,16 +332,14 @@ void *copyAllocPinnedMemoryClean(void *ptr, size_t size, size_t original_size, ******************************************************************************/ int copyFreePinnedMemory(void *uptr, void *kptr, int size) { - if (uptr == NULL || kptr == NULL) { - MEM_WARN("Input pointers uptr or kptr are NULL\n"); + if (unlikely(uptr == NULL || kptr == NULL || size <= 0)) { + MEM_WARN("Input pointers uptr or kptr are NULL, or size invalid.\n"); return 0; } - if (size > MAX_ALLOC) { MEM_WARN("Size greater than MAX_ALLOC\n"); return 0; } - memcpy(uptr, kptr, size); qaeCryptoMemFree(kptr); return 1; @@ -970,9 +967,9 @@ CpaPhysicalAddr qaeCryptoMemV2P(void *v) qat_contig_mem_config *memCfg = NULL; void *pVirtPageAddress = NULL; ptrdiff_t offset = 0; - if(v == NULL) { + if (unlikely(v == NULL)) { MEM_WARN("NULL address passed to function\n"); - return (CpaPhysicalAddr) 0; + return (CpaPhysicalAddr)0; } /* Get the physical address contained in the slab @@ -988,7 +985,7 @@ CpaPhysicalAddr qaeCryptoMemV2P(void *v) if(memCfg->signature == QAT_CONTIG_MEM_ALLOC_SIG) return (CpaPhysicalAddr)(memCfg->physicalAddress + offset); MEM_WARN("Virtual to Physical memory lookup failure\n"); - return (CpaPhysicalAddr) 0; + return (CpaPhysicalAddr)0; } /************************************** @@ -1010,9 +1007,10 @@ CpaPhysicalAddr qaeCryptoMemV2P(void *v) ******************************************************************************/ void *qaeCryptoMemAlloc(size_t memsize, const char *file, int line) { + /* Input params should already have been sanity-checked by calling function. */ void *pAddress = crypto_alloc_from_slab(memsize, file, line); - MEM_DEBUG("Address: %p Size: %lu File: %s:%d\n", pAddress, - memsize, file, line); + MEM_DEBUG("Address: %p Size: %lu File: %s:%d\n", + pAddress, memsize, file, line); return pAddress; } @@ -1029,8 +1027,13 @@ void *qaeCryptoMemAlloc(size_t memsize, const char *file, int line) void qaeCryptoMemFree(void *ptr) { MEM_DEBUG("Address: %p\n", ptr); - if (NULL != ptr) - crypto_free_to_slab(ptr); + { + if (NULL != ptr) + crypto_free_to_slab(ptr); + else { + MEM_WARN("qaeCryptoMemFree trying to free NULL pointer.\n"); + } + } } /****************************************************************************** @@ -1052,8 +1055,10 @@ void qaeCryptoMemFree(void *ptr) void *qaeCryptoMemRealloc(void *ptr, size_t memsize, const char *file, int line) { - if (ptr == NULL) { - MEM_WARN("Input pointer is NULL\n"); + if (unlikely((ptr == NULL) || + (memsize == 0) || + (file == NULL))) { + MEM_WARN("Input parameter invalid.\n"); return NULL; } @@ -1097,8 +1102,15 @@ void *qaeCryptoMemReallocClean(void *ptr, size_t memsize, size_t original_size, const char *file, int line) { - if (ptr == NULL) { - MEM_WARN("Input pointer is NULL\n"); + if (unlikely((ptr == NULL) || + (memsize == 0) || + (original_size == 0) || + (file == NULL))) { + MEM_WARN("Input param. invalid.\n"); + return NULL; + } + if (original_size > memsize) { + MEM_WARN("original_size : %zd > memsize : %zd", original_size, memsize); return NULL; } diff --git a/qat_asym_common.c b/qat_asym_common.c index f4371ea9..c7054975 100644 --- a/qat_asym_common.c +++ b/qat_asym_common.c @@ -94,6 +94,11 @@ ******************************************************************************/ int qat_BN_to_FB(CpaFlatBuffer * fb, const BIGNUM *bn) { + if (unlikely((fb == NULL || + bn == NULL ))) { + WARN("Invalid input params.\n"); + return 0; + } /* Memory allocate for flat buffer */ fb->dataLenInBytes = (Cpa32U) BN_num_bytes(bn); if (0 == fb->dataLenInBytes) { diff --git a/qat_callback.c b/qat_callback.c index dce7909b..ba409836 100644 --- a/qat_callback.c +++ b/qat_callback.c @@ -81,7 +81,7 @@ void qat_init_op_done(op_done_t *opDone) { - if (opDone == NULL) { + if (unlikely(opDone == NULL)) { WARN("opDone is NULL\n"); QATerr(QAT_F_QAT_INIT_OP_DONE, QAT_R_OPDONE_NULL); return; @@ -96,8 +96,8 @@ void qat_init_op_done(op_done_t *opDone) int qat_init_op_done_pipe(op_done_pipe_t *opdpipe, unsigned int npipes) { - if (opdpipe == NULL) { - WARN("opdpipe is NULL\n"); + if (unlikely((opdpipe == NULL) || (npipes == 0))) { + WARN("opdpipe is NULL or npipes is 0.\n"); QATerr(QAT_F_QAT_INIT_OP_DONE_PIPE, QAT_R_OPDPIPE_NULL); return 0; } @@ -124,7 +124,7 @@ int qat_init_op_done_pipe(op_done_pipe_t *opdpipe, unsigned int npipes) int qat_init_op_done_rsa_crt(op_done_rsa_crt_t *opdcrt) { - if (opdcrt == NULL) { + if (unlikely(opdcrt == NULL)) { WARN("opdcrt is NULL\n"); QATerr(QAT_F_QAT_INIT_OP_DONE_RSA_CRT, QAT_R_OPDCRT_NULL); return 0; @@ -143,7 +143,7 @@ int qat_init_op_done_rsa_crt(op_done_rsa_crt_t *opdcrt) void qat_cleanup_op_done(op_done_t *opDone) { - if (opDone == NULL) { + if (unlikely(opDone == NULL)) { WARN("opDone is NULL\n"); return; } @@ -157,7 +157,7 @@ void qat_cleanup_op_done(op_done_t *opDone) void qat_cleanup_op_done_pipe(op_done_pipe_t *opdone) { - if (opdone == NULL) { + if (unlikely(opdone == NULL)) { WARN("opdone is NULL\n"); return; } @@ -170,7 +170,7 @@ void qat_cleanup_op_done_pipe(op_done_pipe_t *opdone) void qat_cleanup_op_done_rsa_crt(op_done_rsa_crt_t *opdcrt) { - if (opdcrt == NULL) { + if (unlikely(opdcrt == NULL)) { WARN("opdcrt is NULL\n"); return; } @@ -187,7 +187,7 @@ void qat_crypto_callbackFn(void *callbackTag, CpaStatus status, { op_done_t *opDone = (op_done_t *)callbackTag; - if (opDone == NULL) { + if (unlikely(opDone == NULL)) { WARN("opDone is NULL\n"); QATerr(QAT_F_QAT_CRYPTO_CALLBACKFN, QAT_R_OPDONE_NULL); return; diff --git a/qat_ciphers.c b/qat_ciphers.c index b4249baf..7ba4d4ea 100644 --- a/qat_ciphers.c +++ b/qat_ciphers.c @@ -479,6 +479,13 @@ int qat_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid) { int i; + if (unlikely((nids == NULL) && ((cipher == NULL) || (nid < 0)))) { + WARN("Invalid input param.\n"); + if (cipher != NULL) + *cipher = NULL; + return 0; + } + /* No specific cipher => return a list of supported nids ... */ if (cipher == NULL) { *nids = qat_cipher_nids; diff --git a/qat_dh.c b/qat_dh.c index 01d992e4..febdb45b 100644 --- a/qat_dh.c +++ b/qat_dh.c @@ -496,6 +496,11 @@ int qat_dh_compute_key(unsigned char *key, const BIGNUM *in_pub_key, DH *dh) DEBUG("- Started\n"); + if (unlikely(key == NULL)) { + WARN("Invalid variable key is NULL.\n"); + QATerr(QAT_F_QAT_DH_COMPUTE_KEY, QAT_R_KEY_NULL); + return -1; + } if (!dh) { WARN("Input variable dh is null\n"); QATerr(QAT_F_QAT_DH_COMPUTE_KEY, QAT_R_DH_NULL); diff --git a/qat_dsa.c b/qat_dsa.c index 4abe27e1..5db49254 100644 --- a/qat_dsa.c +++ b/qat_dsa.c @@ -257,10 +257,15 @@ DSA_SIG *qat_dsa_do_sign(const unsigned char *dgst, int dlen, DEBUG("- Started\n"); - if (dsa == NULL || dgst == NULL) { - WARN("Either dsa %p or dgst %p are NULL\n", dsa, dgst); - QATerr(QAT_F_QAT_DSA_DO_SIGN, QAT_R_DSA_DGST_NULL); - return NULL; + if (unlikely(dlen <= 0)) { + WARN("Invalid input param.\n"); + QATerr(QAT_F_QAT_DSA_DO_SIGN, QAT_R_DLEN_INVALID); + return NULL; + } + if (unlikely(dsa == NULL || dgst == NULL)) { + WARN("Either dsa %p or dgst %p are NULL\n", dsa, dgst); + QATerr(QAT_F_QAT_DSA_DO_SIGN, QAT_R_DSA_DGST_NULL); + return NULL; } DSA_get0_pqg(dsa, &p, &q, &g); @@ -580,6 +585,11 @@ int qat_dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) const DSA_METHOD *default_dsa_method = DSA_OpenSSL(); DEBUG("%s been called \n", __func__); + if (unlikely(dsa == NULL || ctx_in == NULL || kinvp == NULL || rp == NULL)) { + WARN("Invalid input param.\n"); + QATerr(QAT_F_QAT_DSA_SIGN_SETUP, QAT_R_INPUT_PARAM_INVALID); + return 0; + } return DSA_meth_get_sign_setup(default_dsa_method)(dsa, ctx_in, kinvp, rp); } @@ -614,6 +624,11 @@ int qat_dsa_do_verify(const unsigned char *dgst, int dgst_len, DEBUG("- Started\n"); + if (unlikely(dgst_len <= 0)) { + WARN("Invalid input param.\n"); + QATerr(QAT_F_QAT_DSA_DO_VERIFY, QAT_R_DGSTLEN_INVALID); + return -1; + } if (dsa == NULL || dgst == NULL || sig == NULL) { WARN("Either dsa = %p, dgst = %p or sig = %p are NULL\n", dsa, dgst, sig); QATerr(QAT_F_QAT_DSA_DO_VERIFY, QAT_R_DSA_DGST_SIG_NULL); diff --git a/qat_ec.c b/qat_ec.c index 50149dea..e0c1c08a 100644 --- a/qat_ec.c +++ b/qat_ec.c @@ -279,9 +279,11 @@ int qat_ecdh_compute_key(unsigned char **outX, size_t *outlenX, DEBUG("- Started\n"); - if (ecdh == NULL || (priv_key = EC_KEY_get0_private_key(ecdh)) == NULL) { - WARN("Either ecdh or priv_key is NULL\n"); - QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_ECDH_PRIVATE_KEY_NULL); + if (unlikely(ecdh == NULL || + ((priv_key = EC_KEY_get0_private_key(ecdh)) == NULL) + || pub_key == NULL)) { + WARN("Either ecdh or priv_key or pub_key is NULL\n"); + QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_ECDH_PRIV_KEY_PUB_KEY_NULL); return ret; } @@ -629,7 +631,7 @@ int qat_ecdh_generate_key(EC_KEY *ecdh) size_t temp_yfield_size = 0; PFUNC_GEN_KEY gen_key_pfunc = NULL; - if (ecdh == NULL || ((group = EC_KEY_get0_group(ecdh)) == NULL)) { + if (unlikely(ecdh == NULL || ((group = EC_KEY_get0_group(ecdh)) == NULL))) { WARN("Either ecdh or group are NULL\n"); QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_ECDH_GROUP_NULL); return 0; @@ -848,15 +850,29 @@ static void qat_ecdsaVerifyCallbackFn(void *pCallbackTag, CpaStatus status, int qat_ecdsa_sign(int type, const unsigned char *dgst, int dlen, - unsigned char *sig, unsigned int *siglen, - const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) + unsigned char *sig, unsigned int *siglen, + const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey) { ECDSA_SIG *s; + + if (unlikely(dgst == NULL || + dlen <= 0)) { /* Check these input params before passing to + * RAND_seed(). Rest of the input params. are + * checked by qat_ecdsa_do_sign(). + */ + WARN("Invalid input param.\n"); + if (siglen != NULL) + *siglen = 0; + QATerr(QAT_F_QAT_ECDSA_SIGN, QAT_R_INPUT_PARAM_INVALID); + return 0; + } RAND_seed(dgst, dlen); s = qat_ecdsa_do_sign(dgst, dlen, kinv, r, eckey); if (s == NULL) { WARN("Error ECDSA Sign Operation Failed\n"); - *siglen = 0; + if (siglen != NULL) + *siglen = 0; + QATerr(QAT_F_QAT_ECDSA_SIGN, QAT_R_QAT_ECDSA_DO_SIGN_FAIL); return 0; } *siglen = i2d_ECDSA_SIG(s, &sig); @@ -866,8 +882,8 @@ int qat_ecdsa_sign(int type, const unsigned char *dgst, int dlen, ECDSA_SIG *qat_ecdsa_do_sign(const unsigned char *dgst, int dgst_len, - const BIGNUM *in_kinv, const BIGNUM *in_r, - EC_KEY *eckey) + const BIGNUM *in_kinv, const BIGNUM *in_r, + EC_KEY *eckey) { int ok = 0, i, job_ret = 0; BIGNUM *m = NULL, *order = NULL; @@ -896,6 +912,14 @@ ECDSA_SIG *qat_ecdsa_do_sign(const unsigned char *dgst, int dgst_len, DEBUG("- Started\n"); + if (unlikely(dgst == NULL || + dgst_len <= 0 || + eckey == NULL)) { + WARN("Invalid input param.\n"); + QATerr(QAT_F_QAT_ECDSA_DO_SIGN, QAT_R_INPUT_PARAM_INVALID); + return NULL; + } + group = EC_KEY_get0_group(eckey); priv_key = EC_KEY_get0_private_key(eckey); pub_key = EC_KEY_get0_public_key(eckey); @@ -1269,7 +1293,7 @@ ECDSA_SIG *qat_ecdsa_do_sign(const unsigned char *dgst, int dgst_len, * -1: error */ int qat_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len, - const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) + const unsigned char *sigbuf, int sig_len, EC_KEY *eckey) { ECDSA_SIG *s; const unsigned char *p = sigbuf; @@ -1327,6 +1351,11 @@ int qat_ecdsa_do_verify(const unsigned char *dgst, int dgst_len, thread_local_variables_t *tlv = NULL; DEBUG("- Started\n"); + if (unlikely(dgst == NULL || dgst_len <= 0)) { + WARN("Invalid input param.\n"); + QATerr(QAT_F_QAT_ECDSA_DO_VERIFY, QAT_R_INPUT_PARAM_INVALID); + return ret; + } /* check input values */ if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL || diff --git a/qat_fork.c b/qat_fork.c index 89ac60c7..cadb0c53 100644 --- a/qat_fork.c +++ b/qat_fork.c @@ -116,7 +116,9 @@ int qat_set_instance_for_thread(long instanceNum) { thread_local_variables_t *tlv = NULL; tlv = qat_check_create_local_variables(); - if (NULL == tlv || 0 == qat_num_instances) { + if (NULL == tlv || + 0 == qat_num_instances || + instanceNum < 0) { WARN("could not create local variables or no instances available\n"); QATerr(QAT_F_QAT_SET_INSTANCE_FOR_THREAD, QAT_R_SET_INSTANCE_FAILURE); return 0; diff --git a/qat_prf.c b/qat_prf.c index 2bf83f76..1674bed3 100644 --- a/qat_prf.c +++ b/qat_prf.c @@ -170,13 +170,15 @@ int qat_PRF_pkey_methods(ENGINE *e, EVP_PKEY_METHOD **pmeth, const int **nids, int nid) { if (pmeth == NULL) { + if (unlikely(nids == NULL)) { + WARN("Invalid input params.\n"); + return 0; + } *nids = qat_prf_nids; return 1; } - if (pmeth) - *pmeth = qat_prf_pmeth(); - + *pmeth = qat_prf_pmeth(); return 1; } @@ -196,6 +198,11 @@ int qat_tls1_prf_init(EVP_PKEY_CTX *ctx) { QAT_TLS1_PRF_CTX *qat_prf_ctx = NULL; + if (unlikely(ctx == NULL)) { + WARN("Invalid input param.\n"); + return 0; + } + qat_prf_ctx = OPENSSL_zalloc(sizeof(*qat_prf_ctx)); if (qat_prf_ctx == NULL) { WARN("Cannot allocate qat_prf_ctx\n"); @@ -219,7 +226,7 @@ void qat_prf_cleanup(EVP_PKEY_CTX *ctx) { QAT_TLS1_PRF_CTX *qat_prf_ctx = NULL; - if (ctx == NULL) { + if (unlikely(ctx == NULL)) { WARN("ctx (type EVP_PKEY_CTX) is NULL \n"); return; } @@ -261,14 +268,23 @@ void qat_prf_cleanup(EVP_PKEY_CTX *ctx) ******************************************************************************/ int qat_tls1_prf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { + if (unlikely(ctx == NULL)) { + WARN("Invalid input param.\n"); + return 0; + } + QAT_TLS1_PRF_CTX *qat_prf_ctx = (QAT_TLS1_PRF_CTX *) EVP_PKEY_CTX_get_data(ctx); - if (qat_prf_ctx == NULL) { + if (unlikely(qat_prf_ctx == NULL)) { WARN("qat_prf_ctx cannot be NULL\n"); return 0; } switch (type) { case EVP_PKEY_CTRL_TLS_MD: + if (unlikely(p2 == NULL)) { + WARN("Invalid input param.\n"); + return 0; + } qat_prf_ctx->md = p2; return 1; @@ -534,7 +550,7 @@ int qat_prf_tls_derive(EVP_PKEY_CTX *ctx, unsigned char *key, memset(&prf_op_data, 0, sizeof(CpaCyKeyGenTlsOpData)); - if (NULL == ctx || NULL == key || NULL == olen) { + if (unlikely(NULL == ctx || NULL == key || NULL == olen)) { WARN("Either ctx %p, key %p or olen %p is NULL\n", ctx, key, olen); QATerr(QAT_F_QAT_PRF_TLS_DERIVE, ERR_R_PASSED_NULL_PARAMETER); return ret; diff --git a/qat_rsa.c b/qat_rsa.c index d9f34628..95ea199e 100644 --- a/qat_rsa.c +++ b/qat_rsa.c @@ -838,9 +838,8 @@ build_encrypt_op_buf(int flen, const unsigned char *from, unsigned char *to, * description: Perform an RSA private encrypt (RSA Sign) * We use the decrypt implementation to achieve this. ******************************************************************************/ -int -qat_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, - RSA *rsa, int padding) +int qat_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) { int rsa_len = 0; CpaCyRsaDecryptOpData *dec_op_data = NULL; @@ -860,11 +859,11 @@ qat_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, * The input message length should be less than or equal to RSA size and also have * minimum space of at least 11 bytes of padding if using PKCS1 padding. */ - if (rsa == NULL || from == NULL || to == NULL || flen == 0) { + if (unlikely(rsa == NULL || from == NULL || to == NULL || flen <= 0)) { WARN("RSA key, input or output is NULL or invalid length, \ flen = %d\n", flen); QATerr(QAT_F_QAT_RSA_PRIV_ENC, QAT_R_RSA_FROM_TO_NULL); - goto exit; + return 0; } rsa_len = RSA_size(rsa); @@ -973,8 +972,8 @@ int qat_rsa_priv_dec(int flen, const unsigned char *from, DEBUG("- Started.\n"); /* parameter checks */ - if (rsa == NULL || from == NULL || to == NULL || - (flen != (rsa_len = RSA_size(rsa)))) { + if (unlikely(rsa == NULL || from == NULL || to == NULL || + (flen != (rsa_len = RSA_size(rsa))))) { WARN("RSA key, input or output is NULL or invalid length, \ flen = %d, rsa_len = %d\n", flen, rsa_len); QATerr(QAT_F_QAT_RSA_PRIV_DEC, QAT_R_RSA_FROM_TO_NULL); @@ -1088,7 +1087,7 @@ int qat_rsa_priv_dec(int flen, const unsigned char *from, rsa_decrypt_op_buf_free(dec_op_data, output_buffer); /* set output all 0xff if failed */ - if (!sts && to) + if (!sts) memset(to, 0xff, rsa_len); /* Return an error */ @@ -1126,11 +1125,11 @@ int qat_rsa_pub_enc(int flen, const unsigned char *from, DEBUG("- Started\n"); /* parameter checks */ - if (rsa == NULL || from == NULL || to == NULL) { - WARN("RSA key %p, input %p or output %p are NULL\n", - rsa, from, to); + if (unlikely(rsa == NULL || from == NULL || to == NULL || flen < 0)) { + WARN("RSA key %p, input %p or output %p are NULL, or flen invalid length.\n", + rsa, from, to); QATerr(QAT_F_QAT_RSA_PUB_ENC, QAT_R_RSA_FROM_TO_NULL); - goto exit; + return 0; } rsa_len = RSA_size(rsa); @@ -1196,9 +1195,8 @@ int qat_rsa_pub_enc(int flen, const unsigned char *from, * The function returns the RSA recovered message output. * We use the encrypt implementation to achieve this. ******************************************************************************/ -int -qat_rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, - RSA *rsa, int padding) +int qat_rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) { int rsa_len = 0; int output_len = -1; @@ -1214,7 +1212,7 @@ qat_rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, WARN("RSA key %p, input %p or output %p are NULL or invalid length, \ flen = %d, rsa_len = %d\n", rsa, from, to, flen, rsa_len); QATerr(QAT_F_QAT_RSA_PUB_DEC, QAT_R_RSA_FROM_TO_NULL); - goto exit; + return 0; } /* diff --git a/qat_rsa_crt.c b/qat_rsa_crt.c index 9aaa584d..b64ec453 100644 --- a/qat_rsa_crt.c +++ b/qat_rsa_crt.c @@ -335,9 +335,9 @@ CRT_combine(CpaFlatBuffer *crt_out1, CpaFlatBuffer *crt_out2, int rsa_len, return ret; } -int -qat_rsa_decrypt_CRT(CpaCyRsaDecryptOpData * dec_op_data, int rsa_len, - CpaFlatBuffer * output_buf) + +int qat_rsa_decrypt_CRT(CpaCyRsaDecryptOpData * dec_op_data, int rsa_len, + CpaFlatBuffer * output_buf) { CpaCyLnModExpOpData crt_op1_data = {{0}}, crt_op2_data = {{0}}; CpaFlatBuffer crt_out1 = {0}, crt_out2 = {0}; @@ -353,7 +353,14 @@ qat_rsa_decrypt_CRT(CpaCyRsaDecryptOpData * dec_op_data, int rsa_len, DEBUG("- Started\n"); - if(qat_init_op_done_rsa_crt(&op_done) != 1){ + if (unlikely(rsa_len < 0)) { /* dec_op_data and output_buf are + * already checked by calling function. + */ + WARN("Invalid input param.\n"); + QATerr(QAT_F_QAT_RSA_DECRYPT_CRT, QAT_R_INPUT_PARAM_INVALID); + return 0; + } + if (qat_init_op_done_rsa_crt(&op_done) != 1) { WARN("failed to init opdone for rsa crt\n"); return 0; } diff --git a/qat_utils.c b/qat_utils.c index e966fa78..eec3f24d 100644 --- a/qat_utils.c +++ b/qat_utils.c @@ -75,7 +75,7 @@ void crypto_qat_debug_init_log() WARN("unable to open %s\n", STR(QAT_DEBUG_FILE_PATH)); } else { - debug_file_ref_count++; + debug_file_ref_count++; } } pthread_mutex_unlock(&debug_file_mutex); @@ -138,7 +138,7 @@ void qat_hex_dump(const char *func, const char *var, const unsigned char p[], int i; fprintf(qatDebugLogFile, "%s: %s: Length %d, Address %p", func, var, l, p); - if (NULL != p && l != 0) { + if (NULL != p && l > 0) { for (i = 0; i < l; i++) { if (i % 16 == 0) fputc('\n', qatDebugLogFile);