From cdcebdc1e2c59f445c4fa2557913dfc0461908de Mon Sep 17 00:00:00 2001 From: Hosam Elkoulak Date: Wed, 17 Jun 2026 13:45:28 +0200 Subject: [PATCH] move var declarations to the beginning of the block --- crypto/ascon/ascon-file-encrypt.c | 9 +++++++-- hash/Ascon-Hash256.c | 23 +++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/crypto/ascon/ascon-file-encrypt.c b/crypto/ascon/ascon-file-encrypt.c index e74d350c..022589a1 100644 --- a/crypto/ascon/ascon-file-encrypt.c +++ b/crypto/ascon/ascon-file-encrypt.c @@ -137,6 +137,7 @@ int AsconEncrypt(wc_AsconCtx* ctx) byte salt[SALT_SIZE] = {0}; byte tag[ASCON_AEAD128_TAG_SZ] = {0}; int chunk_read = BLOCK_SIZE; + long j; if (fseek(ctx->inFile, 0, SEEK_END) != SUCCESS) { @@ -204,7 +205,7 @@ int AsconEncrypt(wc_AsconCtx* ctx) } /* Loop reading a block at a time */ - for (long j = 0; j <= ctx->inFileLength; j += BLOCK_SIZE) { + for (j = 0; j <= ctx->inFileLength; j += BLOCK_SIZE) { if (chunk_read > ctx->inFileLength - j) { chunk_read = ctx->inFileLength - j; } @@ -302,6 +303,7 @@ int AsconDecrypt(wc_AsconCtx* ctx) if (ctx->inFileLength >= FILE_HEADER_SIZE) { int chunk_read = BLOCK_SIZE; int i = 0; + long j; /* read the file header and extract salt, nonce, and tag */ if (fread(ctx->cipherText, 1, FILE_HEADER_SIZE, ctx->inFile) != FILE_HEADER_SIZE) { @@ -351,7 +353,7 @@ int AsconDecrypt(wc_AsconCtx* ctx) /* Start decrypting ciphertext */ ctx->inFileLength -= FILE_HEADER_SIZE; - for (long j = 0; j <= ctx->inFileLength; j += BLOCK_SIZE) { + for (j = 0; j <= ctx->inFileLength; j += BLOCK_SIZE) { if (chunk_read > ctx->inFileLength - j) { chunk_read = ctx->inFileLength - j; } @@ -466,6 +468,9 @@ int main(int argc, char** argv) printf("Memory allocation for ctx failed.\n"); return ret; } + + memset(ctx, 0, sizeof(*ctx)); + ctx->ascon = NULL; ctx->key = NULL; ctx->password = NULL; diff --git a/hash/Ascon-Hash256.c b/hash/Ascon-Hash256.c index 41de5fba..5617bd0a 100644 --- a/hash/Ascon-Hash256.c +++ b/hash/Ascon-Hash256.c @@ -56,12 +56,15 @@ int main(int argc, char** argv) int ret = 1; #ifdef HAVE_ASCON wc_AsconHash256* asconHash = NULL; - byte hash[ASCON_HASH256_SZ] = {0}; - byte* rawInput = NULL; - FILE* inputStream = NULL; - char* fName = NULL; - long fileLength = 0; - int chunkRead = BLOCK_SIZE; + byte hash[ASCON_HASH256_SZ] = {0}; + byte* rawInput = NULL; + FILE* inputStream = NULL; + char* fName = NULL; + long fileLength = 0; + int chunkRead = BLOCK_SIZE; + long i; + int j; + size_t read; if (argc < 2) usage(); @@ -105,12 +108,12 @@ int main(int argc, char** argv) goto cleanup; } - for (long i = 0; i < fileLength; i += BLOCK_SIZE) { + for (i = 0; i < fileLength; i += BLOCK_SIZE) { if (chunkRead > fileLength - i) chunkRead = fileLength - i; /* Read blocks from input file into a byte array*/ - size_t read = fread(rawInput, 1, chunkRead, inputStream); + read = fread(rawInput, 1, chunkRead, inputStream); if (read != chunkRead) { printf("ERROR: Failed to read the size of input file\n"); goto cleanup; @@ -132,8 +135,8 @@ int main(int argc, char** argv) } printf("Hash result is: "); - for (int i = 0; i < ASCON_HASH256_SZ; i++) - printf("%02x", hash[i]); + for (j = 0; j < ASCON_HASH256_SZ; j++) + printf("%02x", hash[j]); printf("\n"); cleanup: