diff --git a/wolfcrypt/src/aes.c b/wolfcrypt/src/aes.c index 87db090536..61f8e9e311 100644 --- a/wolfcrypt/src/aes.c +++ b/wolfcrypt/src/aes.c @@ -16504,7 +16504,7 @@ static WARN_UNUSED_RESULT int wc_AesFeedbackCFB1( } if (ret == 0) { - if (bit >= 0 && bit < 7) { + if (bit < 7) { out[0] = cur; } } diff --git a/wolfcrypt/src/coding.c b/wolfcrypt/src/coding.c index 860df6b0f2..46a86f2061 100644 --- a/wolfcrypt/src/coding.c +++ b/wolfcrypt/src/coding.c @@ -134,7 +134,7 @@ int Base64_SkipNewline(const byte* in, word32 *inLen, curChar = in[++j]; len--; } - if (len && (curChar == '\r' || curChar == '\n')) { + if (curChar == '\r' || curChar == '\n') { j++; len--; if (curChar == '\r') { @@ -278,7 +278,7 @@ int Base64_Decode_nonCT(const byte* in, word32 inLen, byte* out, word32* outLen) } /* If the output buffer has a room for an extra byte, add a null terminator */ - if (out && *outLen > i) + if (*outLen > i) out[i]= '\0'; /* Note, *outLen won't reflect the optional terminating null. */ @@ -392,7 +392,7 @@ int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen) } /* If the output buffer has a room for an extra byte, add a null terminator */ - if (out && *outLen > i) + if (*outLen > i) out[i]= '\0'; /* Note, *outLen won't reflect the optional terminating null. */ @@ -660,7 +660,7 @@ int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen) if (in == NULL || out == NULL || outLen == NULL) return BAD_FUNC_ARG; - if (inLen == 1 && *outLen && in) { + if (inLen == 1 && *outLen) { byte b = (byte)(in[inIdx++] - BASE16_MIN); /* 0 starts at 0x30 */ /* sanity check */ diff --git a/wolfcrypt/src/ed448.c b/wolfcrypt/src/ed448.c index 08374ba446..a60497f910 100644 --- a/wolfcrypt/src/ed448.c +++ b/wolfcrypt/src/ed448.c @@ -1521,41 +1521,29 @@ int wc_ed448_check_key(ed448_key* key) } } /* No private key, check Y is valid. */ - else if ((ret == 0) && (!key->privKeySet)) { + else if (ret == 0) { /* Verify that xQ and yQ are integers in the interval [0, p - 1]. * Only have yQ so check that ordinate. * p = 2^448-2^224-1 = 0xff..fe..ff */ - if (ret == 0) { - int i; - ret = PUBLIC_KEY_E; + int i; + ret = PUBLIC_KEY_E; - /* Check top part before 0xFE. */ - for (i = ED448_PUB_KEY_SIZE - 1; i > ED448_PUB_KEY_SIZE/2; i--) { - if (key->p[i] < 0xff) { - ret = 0; - break; - } + /* Check top part before 0xFE. */ + for (i = ED448_PUB_KEY_SIZE - 1; i > ED448_PUB_KEY_SIZE/2; i--) { + if (key->p[i] < 0xff) { + ret = 0; + break; } - if (ret == WC_NO_ERR_TRACE(PUBLIC_KEY_E)) { - /* Check against 0xFE. */ - if (key->p[ED448_PUB_KEY_SIZE/2] < 0xfe) { - ret = 0; - } - else if (key->p[ED448_PUB_KEY_SIZE/2] == 0xfe) { - /* Check bottom part before last byte. */ - for (i = ED448_PUB_KEY_SIZE/2 - 1; i > 0; i--) { - if (key->p[i] != 0xff) { - ret = 0; - break; - } - } - /* Check last byte. */ - if ((ret == WC_NO_ERR_TRACE(PUBLIC_KEY_E)) && - (key->p[0] < 0xff)) { - ret = 0; - } - } + } + if (ret == WC_NO_ERR_TRACE(PUBLIC_KEY_E)) { + /* Every byte above the 0xFE position is 0xff here, so y is out of + * range only if this byte is 0xff too. The two encodings that are + * in range by this test but still invalid, y == p and y == p - 1, + * are already rejected by ed448_is_small_order() above, so no + * further check of the low bytes is needed. */ + if (key->p[ED448_PUB_KEY_SIZE/2] <= 0xfe) { + ret = 0; } } diff --git a/wolfcrypt/src/integer.c b/wolfcrypt/src/integer.c index 894a679a6f..d6cd075455 100644 --- a/wolfcrypt/src/integer.c +++ b/wolfcrypt/src/integer.c @@ -102,8 +102,6 @@ word32 CheckRunTimeSettings(void) int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, mp_int* f) { - int res = MP_OKAY; - if (a) XMEMSET(a, 0, sizeof(mp_int)); if (b) XMEMSET(b, 0, sizeof(mp_int)); if (c) XMEMSET(c, 0, sizeof(mp_int)); @@ -111,35 +109,15 @@ int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e, if (e) XMEMSET(e, 0, sizeof(mp_int)); if (f) XMEMSET(f, 0, sizeof(mp_int)); - if (a && ((res = mp_init(a)) != MP_OKAY)) - return res; - - if (b && ((res = mp_init(b)) != MP_OKAY)) { - mp_clear(a); - return res; - } - - if (c && ((res = mp_init(c)) != MP_OKAY)) { - mp_clear(a); mp_clear(b); - return res; - } + /* mp_init() only fails on a NULL argument, already excluded here. */ + if (a) (void)mp_init(a); + if (b) (void)mp_init(b); + if (c) (void)mp_init(c); + if (d) (void)mp_init(d); + if (e) (void)mp_init(e); + if (f) (void)mp_init(f); - if (d && ((res = mp_init(d)) != MP_OKAY)) { - mp_clear(a); mp_clear(b); mp_clear(c); - return res; - } - - if (e && ((res = mp_init(e)) != MP_OKAY)) { - mp_clear(a); mp_clear(b); mp_clear(c); mp_clear(d); - return res; - } - - if (f && ((res = mp_init(f)) != MP_OKAY)) { - mp_clear(a); mp_clear(b); mp_clear(c); mp_clear(d); mp_clear(e); - return res; - } - - return res; + return MP_OKAY; } @@ -1792,8 +1770,7 @@ int s_mp_add (mp_int * a, mp_int * b, mp_int * c) tmpc = c->dp; /* sanity-check dp pointers. */ - if ((min_ab > 0) && - ((tmpa == NULL) || (tmpb == NULL) || (tmpc == NULL))) + if ((min_ab > 0) && ((tmpa == NULL) || (tmpb == NULL))) { return MP_VAL; } diff --git a/wolfcrypt/src/kdf.c b/wolfcrypt/src/kdf.c index e9d10166c3..4c7498768d 100644 --- a/wolfcrypt/src/kdf.c +++ b/wolfcrypt/src/kdf.c @@ -1633,7 +1633,7 @@ int wc_KDA_KDF_PRF_cmac(const byte* Kin, word32 KinSz, } #endif - while (ret == 0 && len_rem >= WC_AES_BLOCK_SIZE) { + while (len_rem >= WC_AES_BLOCK_SIZE) { /* cmac in place in block size increments */ c32toa(counter, counterBuf); #ifdef WOLFSSL_DEBUG_KDF diff --git a/wolfcrypt/src/md5.c b/wolfcrypt/src/md5.c index 9e6c22cbd3..f6c72d3118 100644 --- a/wolfcrypt/src/md5.c +++ b/wolfcrypt/src/md5.c @@ -358,8 +358,8 @@ int wc_Md5Update(wc_Md5* md5, const byte* data, word32 len) if (md5->buffLen >= WC_MD5_BLOCK_SIZE) return BUFFER_E; - if (data == NULL && len == 0) { - /* valid, but do nothing */ + if (data == NULL) { + /* len is 0 here: valid, but do nothing */ return 0; } diff --git a/wolfcrypt/src/memory.c b/wolfcrypt/src/memory.c index 4b06cb9dee..26766d1d34 100644 --- a/wolfcrypt/src/memory.c +++ b/wolfcrypt/src/memory.c @@ -1411,7 +1411,7 @@ void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type) } } - if (pt != NULL && res == NULL) { + if (pt != NULL) { word32 prvSz; res = pt->buffer; diff --git a/wolfcrypt/src/pwdbased.c b/wolfcrypt/src/pwdbased.c index 9b54464138..8fbf64c330 100644 --- a/wolfcrypt/src/pwdbased.c +++ b/wolfcrypt/src/pwdbased.c @@ -373,10 +373,6 @@ static int DoPKCS12Hash(enum wc_HashType hashT, byte* buffer, word32 totalLen, int ret = 0; WC_DECLARE_VAR(hash, wc_HashAlg, 1, 0); - if ((buffer == NULL) || (Ai == NULL)) { - return BAD_FUNC_ARG; - } - /* initialize hash */ WC_ALLOC_VAR_EX(hash, wc_HashAlg, 1, NULL, DYNAMIC_TYPE_HASHCTX, return MEMORY_E); diff --git a/wolfcrypt/src/sha3.c b/wolfcrypt/src/sha3.c index 1ead204fb5..7f4276955f 100644 --- a/wolfcrypt/src/sha3.c +++ b/wolfcrypt/src/sha3.c @@ -1186,8 +1186,8 @@ static int wc_Sha3Update(wc_Sha3* sha3, const byte* data, word32 len, word32 p) return BAD_FUNC_ARG; } - if (data == NULL && len == 0) { - /* valid, but do nothing */ + if (data == NULL) { + /* len is 0 here: valid, but do nothing */ return 0; } @@ -1865,8 +1865,8 @@ int wc_Shake128_Update(wc_Shake* shake, const byte* data, word32 len) return BAD_FUNC_ARG; } - if (data == NULL && len == 0) { - /* valid, but do nothing */ + if (data == NULL) { + /* len is 0 here: valid, but do nothing */ return 0; } @@ -2163,8 +2163,8 @@ int wc_Shake256_Update(wc_Shake* shake, const byte* data, word32 len) return BAD_FUNC_ARG; } - if (data == NULL && len == 0) { - /* valid, but do nothing */ + if (data == NULL) { + /* len is 0 here: valid, but do nothing */ return 0; } diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 4400d6f845..83017ae29f 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -18906,13 +18906,11 @@ int sp_todecimal(const sp_int* a, char* str) /* Terminate string. */ str[i] = '\0'; - if (err == MP_OKAY) { - /* Reverse string to big endian. */ - for (j = 0; j <= (i - 1) / 2; j++) { - int c = (unsigned char)str[j]; - str[j] = str[i - 1 - j]; - str[i - 1 - j] = (char)c; - } + /* Reverse string to big endian. */ + for (j = 0; j <= (i - 1) / 2; j++) { + int c = (unsigned char)str[j]; + str[j] = str[i - 1 - j]; + str[i - 1 - j] = (char)c; } } @@ -19950,10 +19948,8 @@ static int _sp_lcm(const sp_int* a, const sp_int* b, sp_int* r) _sp_init_size(t[0], used); _sp_init_size(t[1], used); - if (err == MP_OKAY) { - /* 1. t0 = gcd(a, b) */ - err = sp_gcd(a, b, t[0]); - } + /* 1. t0 = gcd(a, b) */ + err = sp_gcd(a, b, t[0]); if (err == MP_OKAY) { /* Divide the greater by the common divisor and multiply by other diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index bb5440c99d..c8c7e0f618 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -5511,23 +5511,15 @@ int fp_gcd(fp_int *a, fp_int *b, fp_int *c) } /* either zero than gcd is the largest */ - if (fp_iszero (a) == FP_YES && fp_iszero (b) == FP_NO) { + if (fp_iszero (a) == FP_YES) { fp_abs (b, c); return FP_OKAY; } - if (fp_iszero (a) == FP_NO && fp_iszero (b) == FP_YES) { + if (fp_iszero (b) == FP_YES) { fp_abs (a, c); return FP_OKAY; } - /* optimized. At this point if a == 0 then - * b must equal zero too - */ - if (fp_iszero (a) == FP_YES) { - fp_zero(c); - return FP_OKAY; - } - #ifdef WOLFSSL_SMALL_STACK u = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT); if (u == NULL) { diff --git a/wolfcrypt/src/wc_lms_impl.c b/wolfcrypt/src/wc_lms_impl.c index 908fe97555..2b01541139 100644 --- a/wolfcrypt/src/wc_lms_impl.c +++ b/wolfcrypt/src/wc_lms_impl.c @@ -3589,9 +3589,6 @@ int wc_hss_reload_key(LmsState* state, const byte* priv_raw, #endif wc_hss_priv_data_load(state->params, priv_key, priv_data); -#ifndef WOLFSSL_WC_LMS_SMALL - priv_key->inited = 0; -#endif #ifdef WOLFSSL_WC_LMS_SERIALIZE_STATE if (pub_root != NULL) @@ -3600,7 +3597,7 @@ int wc_hss_reload_key(LmsState* state, const byte* priv_raw, /* Expand the raw private key into the private key data. */ ret = wc_hss_expand_private_key(state, priv_key->priv, priv_raw, 0); #ifndef WOLFSSL_WC_LMS_SMALL - if ((ret == 0) && (!priv_key->inited)) { + if (ret == 0) { /* Initialize the authentication paths and caches for all trees. */ ret = wc_hss_init_auth_path(state, priv_key, pub_root); #ifndef WOLFSSL_LMS_NO_SIGN_SMOOTHING diff --git a/wolfcrypt/src/wc_mldsa.c b/wolfcrypt/src/wc_mldsa.c index f1e9d0496c..689a25ccd8 100644 --- a/wolfcrypt/src/wc_mldsa.c +++ b/wolfcrypt/src/wc_mldsa.c @@ -5063,7 +5063,7 @@ static int mldsa_vec_check_low_c(const sword32* a, byte l, sword32 hi) unsigned int i; /* For each polynomial of vector. */ - for (i = 0; (ret == 1) && (i < l); i++) { + for (i = 0; i < l; i++) { ret = mldsa_check_low(a, hi); if (ret == 0) { break; @@ -8100,7 +8100,7 @@ static int mldsa_make_key_from_seed(wc_MlDsaKey* key, const byte* seed) /* Put r/i into buffer to be hashed. */ aseed[MLDSA_PUB_SEED_SZ + 1] = (byte)r; - for (s = 0; (ret == 0) && (s < params->l); s++) { + for (s = 0; s < params->l; s++) { /* Put s into buffer to be hashed. */ aseed[MLDSA_PUB_SEED_SZ + 0] = (byte)s; /* Step 3: Expand public seed into a matrix of polynomials. */ @@ -8727,7 +8727,7 @@ static int mldsa_sign_with_seed_mu(wc_MlDsaKey* key, sizeof(priv_rand_seed)); #endif /* Check the signature buffer isn't too small. */ - if ((ret == 0) && (*sigLen < params->sigSz)) { + if (*sigLen < params->sigSz) { ret = BUFFER_E; } if (ret == 0) { @@ -8892,7 +8892,7 @@ static int mldsa_sign_with_seed_mu(wc_MlDsaKey* key, /* Put r/i into buffer to be hashed. */ aseed[MLDSA_PUB_SEED_SZ + 1] = r; /* Alg 26. Step 2: Loop over second dimension of matrix. */ - for (s = 0; (ret == 0) && (s < params->l); s++) { + for (s = 0; s < params->l; s++) { /* Put s into buffer to be hashed. */ aseed[MLDSA_PUB_SEED_SZ + 0] = s; /* Alg 26. Step 3: Create polynomial from hashing seed. */ @@ -11586,7 +11586,7 @@ int wc_MlDsaKey_CheckKey(wc_MlDsaKey* key) x |= key->p[i] ^ key->k[i]; } - if ((ret == 0) && (x != 0)) { + if (x != 0) { ret = PUBLIC_KEY_E; } } diff --git a/wolfcrypt/src/wc_mlkem.c b/wolfcrypt/src/wc_mlkem.c index 52b9ef2cc8..b5f2d79f1a 100644 --- a/wolfcrypt/src/wc_mlkem.c +++ b/wolfcrypt/src/wc_mlkem.c @@ -1425,10 +1425,6 @@ static int wc_mlkemkey_check_h(MlKemKey* key) XFREE(pubKey, key->heap, DYNAMIC_TYPE_TMP_BUFFER); #endif } - if ((ret == 0) && ((key->flags & MLKEM_FLAG_H_SET) == 0)) { - /* Implementation issue if h not cached and flag not set. */ - ret = BAD_STATE_E; - } return ret; }