Skip to content

Commit

Permalink
use '__builtin_expect' to improve EVP_EncryptUpdate performance for g…
Browse files Browse the repository at this point in the history
…cc/clang.

Signed-off-by: Liu-ErMeng <liuermeng2@huawei.com>

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #21425)
  • Loading branch information
Liu-ErMeng authored and paulidale committed Jul 13, 2023
1 parent 5be8233 commit ed6dfd1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
24 changes: 12 additions & 12 deletions crypto/evp/evp_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,30 +621,30 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
size_t soutl, inl_ = (size_t)inl;
int blocksize;

if (outl != NULL) {
if (likely(outl != NULL)) {
*outl = 0;
} else {
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}

/* Prevent accidental use of decryption context when encrypting */
if (!ctx->encrypt) {
if (unlikely(!ctx->encrypt)) {
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
return 0;
}

if (ctx->cipher == NULL) {
if (unlikely(ctx->cipher == NULL)) {
ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
return 0;
}

if (ctx->cipher->prov == NULL)
if (unlikely(ctx->cipher->prov == NULL))
goto legacy;

blocksize = ctx->cipher->block_size;

if (ctx->cipher->cupdate == NULL || blocksize < 1) {
if (unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
return 0;
}
Expand All @@ -653,7 +653,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
in, inl_);

if (ret) {
if (likely(ret)) {
if (soutl > INT_MAX) {
ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
return 0;
Expand Down Expand Up @@ -770,37 +770,37 @@ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
size_t soutl, inl_ = (size_t)inl;
int blocksize;

if (outl != NULL) {
if (likely(outl != NULL)) {
*outl = 0;
} else {
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}

/* Prevent accidental use of encryption context when decrypting */
if (ctx->encrypt) {
if (unlikely(ctx->encrypt)) {
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
return 0;
}

if (ctx->cipher == NULL) {
if (unlikely(ctx->cipher == NULL)) {
ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
return 0;
}
if (ctx->cipher->prov == NULL)
if (unlikely(ctx->cipher->prov == NULL))
goto legacy;

blocksize = EVP_CIPHER_CTX_get_block_size(ctx);

if (ctx->cipher->cupdate == NULL || blocksize < 1) {
if (unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
return 0;
}
ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
in, inl_);

if (ret) {
if (likely(ret)) {
if (soutl > INT_MAX) {
ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
return 0;
Expand Down
8 changes: 8 additions & 0 deletions include/internal/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
# include "internal/e_os.h" /* ossl_inline in many files */
# include "internal/nelem.h"

#if defined(__GNUC__) || defined(__clang__)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) x
#define unlikely(x) x
#endif

#ifdef NDEBUG
# define ossl_assert(x) ((x) != 0)
#else
Expand Down

0 comments on commit ed6dfd1

Please sign in to comment.