From 0906436e33bdd8dabf07d494e3d6d5184cc02246 Mon Sep 17 00:00:00 2001 From: Nikolay Morozov Date: Wed, 26 Feb 2020 12:38:27 +0300 Subject: [PATCH] cppcheck: The scope of the variable can be reduced. --- getopt.h | 6 +++--- gost_crypt.c | 6 +++--- gost_grasshopper_cipher.c | 12 ++++-------- gost_grasshopper_core.c | 16 +++++++--------- gost_keywrap.c | 11 ++++++----- gost_pmeth.c | 14 +++++++------- gosthash.c | 3 +-- gosthash2012.c | 3 +-- 8 files changed, 32 insertions(+), 39 deletions(-) diff --git a/getopt.h b/getopt.h index bcbff17..aea89a0 100644 --- a/getopt.h +++ b/getopt.h @@ -181,7 +181,7 @@ static void permute_args(int panonopt_start, int panonopt_end, int opt_end, char * const *nargv) { - int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + int cyclelen, i, j, ncycle, nnonopts, nopts; char *swap; /* @@ -193,8 +193,8 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end, cyclelen = (opt_end - panonopt_start) / ncycle; for (i = 0; i < ncycle; i++) { - cstart = panonopt_end+i; - pos = cstart; + int cstart = panonopt_end+i; + int pos = cstart; for (j = 0; j < cyclelen; j++) { if (pos >= panonopt_end) pos -= nnonopts; diff --git a/gost_crypt.c b/gost_crypt.c index a649e72..4e22145 100644 --- a/gost_crypt.c +++ b/gost_crypt.c @@ -1001,7 +1001,6 @@ int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params) /* Store parameters into ASN1 structure */ int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params) { - int ret = -1; int len; GOST_CIPHER_PARAMS *gcp = NULL; unsigned char *p; @@ -1009,7 +1008,7 @@ int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params) int nid; if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) { - return ret; + return -1; } p = params->value.sequence->data; @@ -1088,12 +1087,13 @@ int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count) { struct ossl_gost_imit_ctx *c = EVP_MD_CTX_md_data(ctx); const unsigned char *p = data; - size_t bytes = count, i; + size_t bytes = count; if (!(c->key_set)) { GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET); return 0; } if (c->bytes_left) { + size_t i; for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) { c->partial_block[i] = *p; } diff --git a/gost_grasshopper_cipher.c b/gost_grasshopper_cipher.c index 2955534..777ca0a 100644 --- a/gost_grasshopper_cipher.c +++ b/gost_grasshopper_cipher.c @@ -331,8 +331,6 @@ int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx); const unsigned char *current_in = in; unsigned char *current_out = out; - grasshopper_w128_t *currentInputBlock; - grasshopper_w128_t *currentOutputBlock; size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE; size_t i; grasshopper_w128_t *currentBlock; @@ -342,8 +340,8 @@ int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) { - currentInputBlock = (grasshopper_w128_t *) current_in; - currentOutputBlock = (grasshopper_w128_t *) current_out; + grasshopper_w128_t *currentInputBlock = (grasshopper_w128_t *) current_in; + grasshopper_w128_t *currentOutputBlock = (grasshopper_w128_t *) current_out; if (encrypting) { grasshopper_append128(currentBlock, currentInputBlock); grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock, @@ -366,10 +364,10 @@ int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out, void inc_counter(unsigned char *counter, size_t counter_bytes) { - unsigned char c; unsigned int n = counter_bytes; do { + unsigned char c; --n; c = counter[n]; ++c; @@ -715,10 +713,8 @@ GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params) { - int ret = -1; - if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) { - return ret; + return -1; } return 1; diff --git a/gost_grasshopper_core.c b/gost_grasshopper_core.c index ed41459..2b14640 100644 --- a/gost_grasshopper_core.c +++ b/gost_grasshopper_core.c @@ -13,12 +13,12 @@ extern "C" { #include "gost_grasshopper_defines.h" static GRASSHOPPER_INLINE void grasshopper_l(grasshopper_w128_t* w) { - uint8_t x; - unsigned int j; - int i; + unsigned int j; + int i; // 16 rounds for (j = 0; j < sizeof(grasshopper_lvec) / sizeof(grasshopper_lvec[0]); j++) { + uint8_t x; // An LFSR with 16 elements from GF(2^8) x = w->b[15]; // since lvec[15] = 1 @@ -32,14 +32,12 @@ static GRASSHOPPER_INLINE void grasshopper_l(grasshopper_w128_t* w) { } static GRASSHOPPER_INLINE void grasshopper_l_inv(grasshopper_w128_t* w) { - uint8_t x; - unsigned int j; - int i; + unsigned int j; + int i; // 16 rounds for (j = 0; j < sizeof(grasshopper_lvec) / sizeof(grasshopper_lvec[0]); j++) { - - x = w->b[0]; + uint8_t x = w->b[0]; for (i = 0; i < 15; i++) { w->b[i] = w->b[i + 1]; x ^= grasshopper_galois_mul(w->b[i], grasshopper_lvec[i]); @@ -52,7 +50,7 @@ static GRASSHOPPER_INLINE void grasshopper_l_inv(grasshopper_w128_t* w) { void grasshopper_set_encrypt_key(grasshopper_round_keys_t* subkeys, const grasshopper_key_t* key) { grasshopper_w128_t c, x, y, z; - int i; + int i; for (i = 0; i < 16; i++) { // this will be have to changed for little-endian systems diff --git a/gost_keywrap.c b/gost_keywrap.c index 502a83c..a2134c2 100644 --- a/gost_keywrap.c +++ b/gost_keywrap.c @@ -23,16 +23,17 @@ void keyDiversifyCryptoPro(gost_ctx * ctx, const unsigned char *inputKey, const unsigned char *ukm, unsigned char *outputKey) { - - u4 k, s1, s2; - int i, j, mask; - unsigned char S[8]; + int i; memcpy(outputKey, inputKey, 32); for (i = 0; i < 8; i++) { + u4 s1 = 0; + u4 s2 = 0; + int j, mask; + unsigned char S[8]; /* Make array of integers from key */ /* Compute IV S */ - s1 = 0, s2 = 0; for (j = 0, mask = 1; j < 8; j++, mask <<= 1) { + u4 k; k = ((u4) outputKey[4 * j]) | (outputKey[4 * j + 1] << 8) | (outputKey[4 * j + 2] << 16) | (outputKey[4 * j + 3] << 24); if (mask & ukm[i]) { diff --git a/gost_pmeth.c b/gost_pmeth.c index b6f4543..4713e3c 100644 --- a/gost_pmeth.c +++ b/gost_pmeth.c @@ -180,9 +180,9 @@ static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx, const char *type, const char *value) { - int param_nid = 0; - if (strcmp(type, param_ctrl_string) == 0) { + int param_nid = 0; + if (!value) { return 0; } @@ -215,9 +215,9 @@ static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx, default: return 0; } - } else if ((strlen(value) == 3) - && (toupper((unsigned char)value[0]) == 'T') - && (toupper((unsigned char)value[1]) == 'C')) { + } else if ((strlen(value) == 3) + && (toupper((unsigned char)value[0]) == 'T') + && (toupper((unsigned char)value[1]) == 'C')) { switch (toupper((unsigned char)value[2])) { case 'A': param_nid = NID_id_tc26_gost_3410_2012_256_paramSetA; @@ -631,8 +631,8 @@ static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) case EVP_PKEY_CTRL_DIGESTINIT: { EVP_MD_CTX *mctx = p2; - struct gost_mac_key *key; if (!data->key_set) { + struct gost_mac_key *key; EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); if (!pkey) { GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, @@ -764,8 +764,8 @@ static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, si case EVP_PKEY_CTRL_DIGESTINIT: { EVP_MD_CTX *mctx = p2; - struct gost_mac_key *key; if (!data->key_set) { + struct gost_mac_key *key; EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx); if (!pkey) { GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, diff --git a/gosthash.c b/gosthash.c index 036a2b3..7fd3cb0 100644 --- a/gosthash.c +++ b/gosthash.c @@ -65,9 +65,8 @@ static int add_blocks(int n, byte * left, const byte * right) { int i; int carry = 0; - int sum; for (i = 0; i < n; i++) { - sum = (int)left[i] + (int)right[i] + carry; + int sum = (int)left[i] + (int)right[i] + carry; left[i] = sum & 0xff; carry = sum >> 8; } diff --git a/gosthash2012.c b/gosthash2012.c index 60fa436..9548bfb 100644 --- a/gosthash2012.c +++ b/gosthash2012.c @@ -195,7 +195,6 @@ static INLINE void stage3(gost2012_hash_ctx * CTX) void gost2012_hash_block(gost2012_hash_ctx * CTX, const unsigned char *data, size_t len) { - register size_t chunksize; register size_t bufsize = CTX->bufsize; if (bufsize == 0) { @@ -208,7 +207,7 @@ void gost2012_hash_block(gost2012_hash_ctx * CTX, } while (len) { - chunksize = 64 - bufsize; + register size_t chunksize = 64 - bufsize; if (chunksize > len) chunksize = len; -- 2.39.2