From 878b6f4183763acb2e7b157986a8cace8886044d Mon Sep 17 00:00:00 2001 From: Aidan Keefe Date: Thu, 1 Oct 2026 16:09:31 -0600 Subject: [PATCH 1/2] starting to change --- src/crypto/clu_encrypt.c | 185 +++++++++++++++++++-------------------- 1 file changed, 90 insertions(+), 95 deletions(-) diff --git a/src/crypto/clu_encrypt.c b/src/crypto/clu_encrypt.c index 8f21d0a4..57a8b80c 100644 --- a/src/crypto/clu_encrypt.c +++ b/src/crypto/clu_encrypt.c @@ -28,6 +28,41 @@ #define MAX_LEN 1024 +static int turnUserStdinputToFile(char* in) +{ + word32 inputLength = 0; + char* userInputBuffer = NULL; + XFILE tempInFile = NULL; + WOLFCLU_LOG(WOLFCLU_L0, "file did not exist, encrypting string " + "following \"-i\" instead."); + + /* use user entered data to encrypt */ + inputLength = (int) XSTRLEN(in); + userInputBuffer = (char*) XMALLOC(inputLength, HEAP_HINT, + DYNAMIC_TYPE_TMP_BUFFER); + if (userInputBuffer == NULL) + return MEMORY_E; + + /* writes the entered text to the input buffer */ + XMEMCPY(userInputBuffer, in, inputLength); + + /* open the file to write */ + tempInFile = XFOPEN(in, "wb"); + if (tempInFile == NULL) { + wolfCLU_LogError("unable to open file %s", in); + XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + return BAD_FUNC_ARG; + } + else { + XFWRITE(userInputBuffer, 1, inputLength, tempInFile); + XFCLOSE(tempInFile); + } + + /* free buffer */ + XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + return WOLFCLU_SUCCESS; +} + /* return WOLFCLU_SUCCESS on success */ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, char* in, char* out, byte* iv, int block, int ivCheck, int inputHex) @@ -36,7 +71,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, Camellia camellia; /* camellia declaration */ #endif - XFILE tempInFile = NULL; /* if user not provide a file */ XFILE inFile = NULL; /* input file */ XFILE outFile = NULL; /* output file */ @@ -46,7 +80,7 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, byte* output = NULL; /* output buffer */ byte salt[SALT_SIZE] = {0}; /* salt variable */ - int ret = 0; /* return variable */ + int ret = WOLFCLU_SUCCESS; /* return variable */ int inputLength = 0; /* length of input */ int length = 0; /* total length */ int padCounter = 0; /* number of padded bytes */ @@ -57,134 +91,102 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, word32 tempMax = MAX_LEN; /* controls encryption amount */ char inputString[MAX_LEN]; /* the input string */ - char* userInputBuffer = NULL; /* buffer when input is not a file */ - - if (access (in, F_OK) == -1) { - WOLFCLU_LOG(WOLFCLU_L0, "file did not exist, encrypting string following \"-i\"" - "instead."); - - /* use user entered data to encrypt */ - inputLength = (int) XSTRLEN(in); - userInputBuffer = (char*) XMALLOC(inputLength, HEAP_HINT, - DYNAMIC_TYPE_TMP_BUFFER); - if (userInputBuffer == NULL) - return MEMORY_E; - - /* writes the entered text to the input buffer */ - XMEMCPY(userInputBuffer, in, inputLength); - - /* open the file to write */ - tempInFile = XFOPEN(in, "wb"); - if (tempInFile == NULL) { - wolfCLU_LogError("unable to open file %s", in); - XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - return BAD_FUNC_ARG; - } - else { - XFWRITE(userInputBuffer, 1, inputLength, tempInFile); - XFCLOSE(tempInFile); + if (ret == WOLFCLU_SUCCESS) { + /* Start up the random number generator */ + ret = (int) wc_InitRng(&rng); + if (ret != 0) { + wolfCLU_LogError("Random Number Generator failed to start."); } + } - /* free buffer */ - XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (access (in, F_OK) == -1) { + ret = turnUserStdinputToFile(in); } - /* open the inFile in read mode */ - inFile = XFOPEN(in, "rb"); - if (inFile == NULL) { - wolfCLU_LogError("unable to open file %s", in); - return WOLFCLU_FATAL_ERROR; + if (ret == WOLFCLU_SUCCESS) { + /* open the inFile in read mode */ + inFile = XFOPEN(in, "rb"); + if (inFile == NULL) { + ret = WOLFCLU_FATAL_ERROR; + } } - /* find length */ - XFSEEK(inFile, 0, SEEK_END); - inputLength = (int)XFTELL(inFile); - XFSEEK(inFile, 0, SEEK_SET); + if (ret == WOLFCLU_SUCCESS) { + /* find length */ + XFSEEK(inFile, 0, SEEK_END); + inputLength = (int)XFTELL(inFile); + XFSEEK(inFile, 0, SEEK_SET); - length = inputLength; + length = inputLength; - /* Start up the random number generator */ - ret = (int) wc_InitRng(&rng); - if (ret != 0) { - wolfCLU_LogError("Random Number Generator failed to start."); - XFCLOSE(inFile); - return ret; - } - - /* pads the length until it matches a block, - * and increases pad number - */ - while (length % block != 0) { - length++; - padCounter++; + /* pads the length until it matches a block, + * and increases pad number + */ + while (length % block != 0) { + length++; + padCounter++; + } } /* if the iv was not explicitly set, * generate an iv and use the pwdKey */ - if (ivCheck == 0) { + if (ret == WOLFCLU_SUCCESS && ivCheck == 0) { /* IV not set, generate it */ ret = wc_RNG_GenerateBlock(&rng, iv, block); - if (ret != 0) { - XFCLOSE(inFile); - wc_FreeRng(&rng); - return ret; - } - /* stretches pwdKey to fit size based on wolfCLU_getAlgo() */ - ret = wolfCLU_genKey_PWDBASED(&rng, pwdKey, size, salt, padCounter); - if (ret != WOLFCLU_SUCCESS) { - wolfCLU_LogError("failed to set pwdKey."); - XFCLOSE(inFile); - wc_FreeRng(&rng); - return ret; + if (ret == WOLFCLU_SUCCESS) { + ret = wolfCLU_genKey_PWDBASED(&rng, pwdKey, size, salt, padCounter); + } + else { + wolfCLU_LogError("Failed to create ."); + } + if (ret == WOLFCLU_SUCCESS) { + /* move the generated pwdKey to "key" for encrypting */ + for (i = 0; i < size; i++) { + key[i] = pwdKey[i]; + } } - /* move the generated pwdKey to "key" for encrypting */ - for (i = 0; i < size; i++) { - key[i] = pwdKey[i]; + else { + wolfCLU_LogError("failed to set pwdKey."); } } - /* open the outFile in write mode */ - outFile = XFOPEN(out, "wb"); - if (outFile == NULL) { - wolfCLU_LogError("unable to open output file %s", out); - XFCLOSE(inFile); - wc_FreeRng(&rng); - return WOLFCLU_FATAL_ERROR; - } - XFWRITE(salt, 1, SALT_SIZE, outFile); - XFWRITE(iv, 1, block, outFile); - XFCLOSE(outFile); /* MALLOC 1kB buffers */ input = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); if (input == NULL) { XFCLOSE(inFile); - wc_FreeRng(&rng); return MEMORY_E; } output = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); if (output == NULL) { XFCLOSE(inFile); wolfCLU_freeBins(input, NULL, NULL, NULL, NULL); - wc_FreeRng(&rng); return MEMORY_E; } + /* open the outFile in write mode */ + outFile = XFOPEN(out, "wb"); + if (outFile == NULL) { + wolfCLU_LogError("unable to open output file %s", out); + XFCLOSE(inFile); + return WOLFCLU_FATAL_ERROR; + } + XFWRITE(salt, 1, SALT_SIZE, outFile); + XFWRITE(iv, 1, block, outFile); + XFCLOSE(outFile); + /* loop, encrypt 1kB at a time till length <= 0 */ while (length > 0) { /* Read in 1kB to input[] */ if (feof(inFile)) { - ret = 0; + ret = WOLFCLU_SUCCESS; } else { - if (inputHex == 1) - ret = (int) fread(inputString, 1, MAX_LEN, inFile); - else - ret = (int) fread(input, 1, MAX_LEN, inFile); + ret = (int) XFREAD(inputString, 1, MAX_LEN, inFile); } if (ret != MAX_LEN) { @@ -202,7 +204,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, wolfCLU_LogError("failed during conversion of input," " ret = %d", hexRet); XFCLOSE(inFile); - wc_FreeRng(&rng); return hexRet; } }/* end hex or ascii */ @@ -217,7 +218,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, else { /* otherwise we got a file read error */ wolfCLU_freeBins(input, output, NULL, NULL, NULL); XFCLOSE(inFile); - wc_FreeRng(&rng); return FREAD_ERROR; }/* End feof check */ }/* End fread check */ @@ -230,7 +230,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, XFCLOSE(inFile); wolfCLU_LogError("CamelliaSetKey failed."); wolfCLU_freeBins(input, output, NULL, NULL, NULL); - wc_FreeRng(&rng); return ret; } if (XSTRNCMP(mode, "cbc", 3) == 0) { @@ -240,7 +239,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, XFCLOSE(inFile); wolfCLU_LogError("Incompatible mode while using Camellia."); wolfCLU_freeBins(input, output, NULL, NULL, NULL); - wc_FreeRng(&rng); return FATAL_ERROR; } } @@ -268,7 +266,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, XFCLOSE(inFile); wolfCLU_LogError("failed to open file."); wolfCLU_freeBins(input, output, NULL, NULL, NULL); - wc_FreeRng(&rng); return FWRITE_ERROR; } @@ -279,7 +276,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, XFCLOSE(inFile); wolfCLU_LogError("failed to write to file."); wolfCLU_freeBins(input, output, NULL, NULL, NULL); - wc_FreeRng(&rng); return FWRITE_ERROR; } if (ret > MAX_LEN) { @@ -287,7 +283,6 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, XFCLOSE(inFile); wolfCLU_LogError("Wrote too much to file."); wolfCLU_freeBins(input, output, NULL, NULL, NULL); - wc_FreeRng(&rng); return FWRITE_ERROR; } /* close the outFile */ From 8920c5c9b67582c8ea82eb24cd199b9152715e52 Mon Sep 17 00:00:00 2001 From: Aidan Keefe Date: Fri, 2 Oct 2026 10:00:05 -0600 Subject: [PATCH 2/2] rewrote encrypt to remove bugs and old code branches --- src/crypto/clu_crypto_setup.c | 8 +- src/crypto/clu_encrypt.c | 245 ++++++++++++-------------------- src/tools/clu_io.c | 62 +++++++- tests/testEncDec/encdec-test.py | 34 +++++ wolfclu/clu_header_main.h | 5 +- wolfclu/clu_io.h | 17 ++- 6 files changed, 205 insertions(+), 166 deletions(-) diff --git a/src/crypto/clu_crypto_setup.c b/src/crypto/clu_crypto_setup.c index d375a78a..5415de96 100644 --- a/src/crypto/clu_crypto_setup.c +++ b/src/crypto/clu_crypto_setup.c @@ -240,7 +240,6 @@ int wolfCLU_setup(int argc, char** argv, char action) int outCheck = 0; /* if output has been provided */ int encCheck = 0; /* if user is encrypting data */ int decCheck = 0; /* if user is decrypting data */ - int inputHex = 0; /* if user is encrypting hexidecimal data */ int keyType = WOLFCLU_KEYTYPE_NONE; /* tells Decrypt which key it will be using; * one of the WOLFCLU_KEYTYPE_* values from @@ -420,11 +419,6 @@ int wolfCLU_setup(int argc, char** argv, char action) case WOLFCLU_SIGN: break; - case WOLFCLU_VERIFY: /* Verify results, used with -iv and -key */ - /* using hexidecimal format */ - inputHex = 1; - break; - case WOLFCLU_INFORM: case WOLFCLU_OUTFORM: case WOLFCLU_OUTPUT: @@ -704,7 +698,7 @@ int wolfCLU_setup(int argc, char** argv, char action) } if (ret == WOLFCLU_SUCCESS) { ret = wolfCLU_encrypt(alg, mode, pwdKey, key, keySize, in, out, - iv, block, ivCheck, inputHex); + iv, block, ivCheck); } } } diff --git a/src/crypto/clu_encrypt.c b/src/crypto/clu_encrypt.c index 57a8b80c..64831d47 100644 --- a/src/crypto/clu_encrypt.c +++ b/src/crypto/clu_encrypt.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #ifndef WOLFCLU_NO_FILESYSTEM @@ -30,49 +31,50 @@ static int turnUserStdinputToFile(char* in) { + int ret = 0; word32 inputLength = 0; - char* userInputBuffer = NULL; - XFILE tempInFile = NULL; + byte* userInputBuffer = NULL; + WOLFCLU_IO tempIo = {0}; WOLFCLU_LOG(WOLFCLU_L0, "file did not exist, encrypting string " "following \"-i\" instead."); + /* use user entered data to encrypt */ inputLength = (int) XSTRLEN(in); - userInputBuffer = (char*) XMALLOC(inputLength, HEAP_HINT, + userInputBuffer = (byte*) XMALLOC(inputLength, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); if (userInputBuffer == NULL) return MEMORY_E; + + /* writes the entered text to the input buffer */ XMEMCPY(userInputBuffer, in, inputLength); - /* open the file to write */ - tempInFile = XFOPEN(in, "wb"); - if (tempInFile == NULL) { - wolfCLU_LogError("unable to open file %s", in); - XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - return BAD_FUNC_ARG; + tempIo = wolfCLU_IO_OpenFile(in, WOLFCLU_IO_WRITABLE_FILE); + if (tempIo.type > 0) { + ret = wolfCLU_IO_Write(&tempIo, userInputBuffer, inputLength); + wolfCLU_IO_Close(&tempIo); } else { - XFWRITE(userInputBuffer, 1, inputLength, tempInFile); - XFCLOSE(tempInFile); + ret = WOLFCLU_FATAL_ERROR; } /* free buffer */ XFREE(userInputBuffer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - return WOLFCLU_SUCCESS; + return ret; } /* return WOLFCLU_SUCCESS on success */ -int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, - char* in, char* out, byte* iv, int block, int ivCheck, int inputHex) +int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, word32 size, + char* in, char* out, byte* iv, int block, int ivCheck) { #ifdef HAVE_CAMELLIA Camellia camellia; /* camellia declaration */ #endif - XFILE inFile = NULL; /* input file */ - XFILE outFile = NULL; /* output file */ + WOLFCLU_IO inIo = {0}; /* input file */ + WOLFCLU_IO outIo = {0}; /* output file */ WC_RNG rng; /* random number generator declaration */ @@ -84,39 +86,36 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, int inputLength = 0; /* length of input */ int length = 0; /* total length */ int padCounter = 0; /* number of padded bytes */ - int i = 0; /* loop variable */ - int hexRet = 0; /* hex -> bin return*/ + word32 i = 0; /* loop variable */ + word32 readSz = 0; /* bytes read from inIo this pass */ - word32 tempInputL = 0; /* temporary input Length */ word32 tempMax = MAX_LEN; /* controls encryption amount */ - char inputString[MAX_LEN]; /* the input string */ + XMEMSET(&rng, 0, sizeof(rng)); - if (ret == WOLFCLU_SUCCESS) { - /* Start up the random number generator */ - ret = (int) wc_InitRng(&rng); - if (ret != 0) { - wolfCLU_LogError("Random Number Generator failed to start."); - } + /* Start up the random number generator */ + if (wc_InitRng(&rng) != 0) { + wolfCLU_LogError("Random Number Generator failed to start."); + ret = WOLFCLU_FATAL_ERROR; } - if (access (in, F_OK) == -1) { + if (ret == WOLFCLU_SUCCESS && access(in, F_OK) == -1) { ret = turnUserStdinputToFile(in); } if (ret == WOLFCLU_SUCCESS) { /* open the inFile in read mode */ - inFile = XFOPEN(in, "rb"); - if (inFile == NULL) { + inIo = wolfCLU_IO_OpenFile(in, WOLFCLU_IO_READABLE_FILE); + if (inIo.type <= 0) { ret = WOLFCLU_FATAL_ERROR; } } if (ret == WOLFCLU_SUCCESS) { /* find length */ - XFSEEK(inFile, 0, SEEK_END); - inputLength = (int)XFTELL(inFile); - XFSEEK(inFile, 0, SEEK_SET); + XFSEEK(inIo.fp, 0, SEEK_END); + inputLength = (int)XFTELL(inIo.fp); + XFSEEK(inIo.fp, 0, SEEK_SET); length = inputLength; @@ -134,14 +133,17 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, */ if (ret == WOLFCLU_SUCCESS && ivCheck == 0) { /* IV not set, generate it */ - ret = wc_RNG_GenerateBlock(&rng, iv, block); + if (wc_RNG_GenerateBlock(&rng, iv, block) != 0) { + wolfCLU_LogError("Failed to create IV."); + ret = WOLFCLU_FATAL_ERROR; + } /* stretches pwdKey to fit size based on wolfCLU_getAlgo() */ if (ret == WOLFCLU_SUCCESS) { ret = wolfCLU_genKey_PWDBASED(&rng, pwdKey, size, salt, padCounter); - } - else { - wolfCLU_LogError("Failed to create ."); + if (ret != WOLFCLU_SUCCESS) { + wolfCLU_LogError("failed to set pwdKey."); + } } if (ret == WOLFCLU_SUCCESS) { /* move the generated pwdKey to "key" for encrypting */ @@ -149,144 +151,82 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, key[i] = pwdKey[i]; } } - else { - wolfCLU_LogError("failed to set pwdKey."); - } } - /* MALLOC 1kB buffers */ - input = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - if (input == NULL) { - XFCLOSE(inFile); - return MEMORY_E; + if (ret == WOLFCLU_SUCCESS) { + input = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (input == NULL) { + ret = WOLFCLU_FATAL_ERROR; + } } - output = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - if (output == NULL) { - XFCLOSE(inFile); - wolfCLU_freeBins(input, NULL, NULL, NULL, NULL); - return MEMORY_E; + if (ret == WOLFCLU_SUCCESS) { + output = (byte*) XMALLOC(MAX_LEN, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (output == NULL) { + ret = WOLFCLU_FATAL_ERROR; + } } /* open the outFile in write mode */ - outFile = XFOPEN(out, "wb"); - if (outFile == NULL) { - wolfCLU_LogError("unable to open output file %s", out); - XFCLOSE(inFile); - return WOLFCLU_FATAL_ERROR; + if (ret == WOLFCLU_SUCCESS) { + outIo = wolfCLU_IO_OpenFile(out, WOLFCLU_IO_WRITABLE_FILE); + if (outIo.type <= 0) { + wolfCLU_LogError("unable to open output file %s", out); + ret = WOLFCLU_FATAL_ERROR; + } } - XFWRITE(salt, 1, SALT_SIZE, outFile); - XFWRITE(iv, 1, block, outFile); - XFCLOSE(outFile); + + if (ret == WOLFCLU_SUCCESS) + ret = wolfCLU_IO_Write(&outIo, salt, SALT_SIZE); + if (ret == WOLFCLU_SUCCESS) + ret = wolfCLU_IO_Write(&outIo, iv, block); /* loop, encrypt 1kB at a time till length <= 0 */ - while (length > 0) { - /* Read in 1kB to input[] */ - if (feof(inFile)) { - ret = WOLFCLU_SUCCESS; - } - else { - ret = (int) XFREAD(inputString, 1, MAX_LEN, inFile); + while (ret == WOLFCLU_SUCCESS && length > 0) { + readSz = MAX_LEN; + + ret = wolfCLU_IO_ReadBlock(&inIo, (byte*)input, + &readSz); + if (ret != WOLFCLU_SUCCESS) { + wolfCLU_LogError("Error while reading from stream"); + ret = WOLFCLU_FATAL_ERROR; + break; } - if (ret != MAX_LEN) { - /* check for end of file */ - if (feof(inFile)) { - - /* hex or ascii */ - if (inputHex == 1) { - hexRet = wolfCLU_hexToBin(inputString, &input, - &tempInputL, - NULL, NULL, NULL, - NULL, NULL, NULL, - NULL, NULL, NULL); - if (hexRet != WOLFCLU_SUCCESS) { - wolfCLU_LogError("failed during conversion of input," - " ret = %d", hexRet); - XFCLOSE(inFile); - return hexRet; - } - }/* end hex or ascii */ - - /* pad to end of block */ - for (i = ret ; i < (ret + padCounter); i++) { - input[i] = padCounter; - } - /* adjust tempMax for less than 1kB encryption */ - tempMax = ret + padCounter; + if (readSz != MAX_LEN) { + /* pad to end of block */ + for (i = readSz; i < (readSz + padCounter); i++) { + input[i] = padCounter; } - else { /* otherwise we got a file read error */ - wolfCLU_freeBins(input, output, NULL, NULL, NULL); - XFCLOSE(inFile); - return FREAD_ERROR; - }/* End feof check */ - }/* End fread check */ + /* adjust tempMax for less than 1kB encryption */ + tempMax = readSz + padCounter; + } #ifdef HAVE_CAMELLIA if (alg == WOLFCLU_CAMELLIA128CBC || alg == WOLFCLU_CAMELLIA192CBC || alg == WOLFCLU_CAMELLIA256CBC) { - ret = wc_CamelliaSetKey(&camellia, key, size / 8, iv); - if (ret != 0) { - XFCLOSE(inFile); + if (wc_CamelliaSetKey(&camellia, key, size / 8, iv) != 0) { wolfCLU_LogError("CamelliaSetKey failed."); - wolfCLU_freeBins(input, output, NULL, NULL, NULL); - return ret; + ret = WOLFCLU_FATAL_ERROR; + break; } if (XSTRNCMP(mode, "cbc", 3) == 0) { - wc_CamelliaCbcEncrypt(&camellia, output, input, tempMax); + if (wc_CamelliaCbcEncrypt(&camellia, output, input, + tempMax) != 0) { + wolfCLU_LogError("CamelliaCbcEncrypt failed."); + ret = WOLFCLU_FATAL_ERROR; + break; + } } else { - XFCLOSE(inFile); wolfCLU_LogError("Incompatible mode while using Camellia."); - wolfCLU_freeBins(input, output, NULL, NULL, NULL); - return FATAL_ERROR; + ret = WOLFCLU_FATAL_ERROR; + break; } } #endif /* HAVE_CAMELLIA */ - /* this method added for visual confirmation of nist test vectors, - * automated tests to come soon - */ - - /* something in the output buffer and using hex */ - if (output != NULL && inputHex == 1) { - int tempi; - - WOLFCLU_LOG(WOLFCLU_L0, "\nUser specified hex input this is a representation of " - "what\nis being written to file in hex form.\n\n[ "); - for (tempi = 0; tempi < block; tempi++ ) { - WOLFCLU_LOG(WOLFCLU_L0, "%02x", output[tempi]); - } - WOLFCLU_LOG(WOLFCLU_L0, " ]\n"); - } /* end visual confirmation */ - - /* Open the outFile in append mode */ - outFile = XFOPEN(out, "ab"); - if (outFile == NULL) { - XFCLOSE(inFile); - wolfCLU_LogError("failed to open file."); - wolfCLU_freeBins(input, output, NULL, NULL, NULL); - return FWRITE_ERROR; - } - - ret = (int)XFWRITE(output, 1, tempMax, outFile); - - if (ferror(outFile)) { - XFCLOSE(outFile); - XFCLOSE(inFile); - wolfCLU_LogError("failed to write to file."); - wolfCLU_freeBins(input, output, NULL, NULL, NULL); - return FWRITE_ERROR; - } - if (ret > MAX_LEN) { - XFCLOSE(outFile); - XFCLOSE(inFile); - wolfCLU_LogError("Wrote too much to file."); - wolfCLU_freeBins(input, output, NULL, NULL, NULL); - return FWRITE_ERROR; - } - /* close the outFile */ - XFCLOSE(outFile); + ret = wolfCLU_IO_Write(&outIo, output, tempMax); length -= tempMax; if (length < 0) @@ -294,16 +234,19 @@ int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, } /* closes the opened files and frees the memory */ - XFCLOSE(inFile); - XMEMSET(key, 0, size); - XMEMSET(iv, 0 , block); + wolfCLU_ForceZero(key, size); + wolfCLU_ForceZero(iv, block); + + wolfCLU_IO_Close(&inIo); + wolfCLU_IO_Close(&outIo); /* Use the wolfssl free for rng */ wc_FreeRng(&rng); + wc_ForceZero(input, MAX_LEN); wolfCLU_freeBins(input, output, NULL, NULL, NULL); (void)mode; (void)alg; - return WOLFCLU_SUCCESS; + return ret; } #endif diff --git a/src/tools/clu_io.c b/src/tools/clu_io.c index 26386f52..ce8f8397 100644 --- a/src/tools/clu_io.c +++ b/src/tools/clu_io.c @@ -187,6 +187,53 @@ static int FileRead(XFILE fp, WOLFCLU_IO_BUFFER* buffer, word32 limit) } #endif +/* read in data upto len of memory buffer passed in. Can be called recursivly + * to stream data + * + * @param io io state struct + * @param buf caller own buffer to read data from stream to + * @param len max number of bytes that can be read into buf + * @returns num bytes read on success and negative value on failure + */ +int wolfCLU_IO_ReadBlock(WOLFCLU_IO* io, byte* buf, word32* len) +{ + int ret = WOLFCLU_SUCCESS; +#ifndef WOLFCLU_NO_FILESYSTEM + + if (io == NULL || io->type <= 0 || len == NULL || + (buf == NULL && *len > 0) || io->fp == NULL) { + wolfCLU_LogError("Bad arg passed to wolfCLU_IO_ReadBlock"); + if (len != NULL) { + *len = 0; + } + return BAD_FUNC_ARG; + } + + if (!(io->type & WOLFCLU_IO_READABLE)) { + wolfCLU_LogError("Invalid ioType passed to wolfCLU_IO_Read"); + *len = 0; + return BAD_FUNC_ARG; + } + + ret = (int)XFREAD(buf, 1, *len, io->fp); + if (ret < 0 || XFERROR(io->fp)) { + wolfCLU_LogError("Error while reading io"); + *len = 0; + return WOLFCLU_FATAL_ERROR; + } + else { + *len = ret; + return WOLFCLU_SUCCESS; + } +#else + (void)io; + (void)buf; + (void)len; + ret = NOT_COMPILED_IN; + return ret; +#endif +} + int wolfCLU_IO_Read(WOLFCLU_IO* io, byte** buf, word32* len, word32 limit) { int ret = WOLFCLU_SUCCESS; @@ -212,9 +259,13 @@ int wolfCLU_IO_Read(WOLFCLU_IO* io, byte** buf, word32* len, word32 limit) if (io->type & WOLFCLU_IO_READABLE_FILE) { ret = FileRead(io->fp, &buffer, limit); } - else { + else if (io->type & WOLFCLU_IO_READABLE_STREAM) { ret = StreamRead(io->fp, &buffer, limit); } + else { + wolfCLU_LogError("IO type was not able to be processed"); + ret = WOLFCLU_FATAL_ERROR; + } if (ret != WOLFCLU_SUCCESS) { if (buffer.outBuf != NULL) { @@ -261,6 +312,10 @@ int wolfCLU_IO_Write(WOLFCLU_IO* io, const byte* buf, word32 len) ret = WOLFCLU_FATAL_ERROR; } #ifdef XFFLUSH + /* do not flush let the XFILE buffered write do its thing */ + if (io->type & WOLFCLU_IO_NOFLUSH) + return ret; + /* the write may only be buffered, flush so errors like a full disk are * caught here */ if (ret == WOLFCLU_SUCCESS && XFFLUSH(io->fp) != 0) { @@ -391,6 +446,7 @@ WOLFCLU_IO wolfCLU_IO_OpenFile(const char* fileName, enum WOLFCLU_IO_TYPE type) else { wolfCLU_LogError("Bad type passed to wolfCLU_IO_OpenFile"); io.type = WOLFCLU_IO_ERROR; + return io; } if (fp == XBADFILE) { wolfCLU_LogError("Could not open file pointer from file name: %s", @@ -421,8 +477,8 @@ int wolfCLU_IO_Close(WOLFCLU_IO* io) { #ifndef WOLFCLU_NO_FILESYSTEM int ret = WOLFCLU_SUCCESS; - if (io == NULL || io->type <= 0) - return BAD_FUNC_ARG; + if (io == NULL || io->type <= 0 || io->fp == XBADFILE) + return WOLFCLU_SUCCESS; /* the stream is gone even when fclose fails */ if (!(io->type & WOLFCLU_IO_NOCLOSE) && XFCLOSE(io->fp) != 0) { diff --git a/tests/testEncDec/encdec-test.py b/tests/testEncDec/encdec-test.py index 435c6c1b..1a017b2c 100644 --- a/tests/testEncDec/encdec-test.py +++ b/tests/testEncDec/encdec-test.py @@ -119,5 +119,39 @@ def test_camellia_cbc_256(self): self._roundtrip("camellia-cbc-256", "hello256") +@unittest.skipUnless("camellia-cbc-128" in _ALGOS, + "Camellia-CBC not available") +class CamelliaEdgeSizeTest(unittest.TestCase): + """Non-EVP path reads in 1024-byte chunks and pads to a 16-byte block; + round trip sizes around both boundaries.""" + + SIZES = [1, 15, 16, 17, 1023, 1024, 1025, 2048] + + def test_camellia_edge_sizes(self): + for size in self.SIZES: + with self.subTest(size=size): + in_file = f"camellia_edge_{size}.in" + enc_file = f"camellia_edge_{size}.enc" + dec_file = f"camellia_edge_{size}.dec" + for f in (in_file, enc_file, dec_file): + self.addCleanup(lambda p=f: os.remove(p) + if os.path.exists(p) else None) + + with open(in_file, "wb") as f: + f.write(bytes(i % 251 for i in range(size))) + + r = run_wolfssl("-encrypt", "camellia-cbc-128", "-pwd", + "edgepwd", "-in", in_file, "-out", enc_file) + self.assertEqual(r.returncode, 0, + f"encrypt size {size} failed: {r.stderr}") + + r = run_wolfssl("-decrypt", "camellia-cbc-128", "-pwd", + "edgepwd", "-in", enc_file, "-out", dec_file) + self.assertEqual(r.returncode, 0, + f"decrypt size {size} failed: {r.stderr}") + self.assertTrue(filecmp.cmp(in_file, dec_file, shallow=False), + f"size {size} round trip mismatch") + + if __name__ == "__main__": test_main() diff --git a/wolfclu/clu_header_main.h b/wolfclu/clu_header_main.h index b3fa9c93..e6c487a5 100644 --- a/wolfclu/clu_header_main.h +++ b/wolfclu/clu_header_main.h @@ -302,11 +302,10 @@ void wolfCLU_stats(double start, int blockSize, int64_t blocks); * @param iv if entered must be in hex otherwise generated at run time * @param block size of block as determined by the algorithm being used * @param ivCheck a flag if user inputs a specific IV - * @param inputHex a flag to specify encrypting hex data, instead of byte data */ -int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, int size, +int wolfCLU_encrypt(int alg, char* mode, byte* pwdKey, byte* key, word32 size, char* in, char* out, byte* iv, int block, - int ivCheck, int inputHex); + int ivCheck); /* decryption function * diff --git a/wolfclu/clu_io.h b/wolfclu/clu_io.h index df515a9d..45204e19 100644 --- a/wolfclu/clu_io.h +++ b/wolfclu/clu_io.h @@ -46,7 +46,8 @@ enum WOLFCLU_IO_TYPE { WOLFCLU_IO_WRITABLE_FILE = 1 << 3, /*behaviors*/ - WOLFCLU_IO_NOCLOSE = 1 << 10, + WOLFCLU_IO_NOCLOSE = 1 << 10, + WOLFCLU_IO_NOFLUSH = 1 << 11, /* type groups */ WOLFCLU_IO_RW_STREAM = @@ -101,6 +102,17 @@ WOLFCLU_IO wolfCLU_IO_OpenFile(const char* fileName, enum WOLFCLU_IO_TYPE type); */ int wolfCLU_IO_Close(WOLFCLU_IO* io); +/** + * @brief read in data upto len of memory buffer passed in. + * Can be called contiuously to stream data + * + * @param io io state struct with readable type + * @param buf caller own buffer to read data from stream to + * @param len max number of bytes that can be read into buf also the number of + * bytes written + */ +int wolfCLU_IO_ReadBlock(WOLFCLU_IO* io, byte* buf, word32* len); + /** * @brief read file/stream data in to an allocated buffer. If limit is * 0 it will default to UINT_MAX otherwise it will be the max number of @@ -116,7 +128,8 @@ int wolfCLU_IO_Close(WOLFCLU_IO* io); int wolfCLU_IO_Read(WOLFCLU_IO* io, byte** buf, word32* len, word32 limit); /** - * @brief write len bytes of buf to io and flush it + * @brief write len bytes of buf to io and flush it can be used to stream data + * to stream. * @param io open IO with a writable type * @param buf buffer to write, may be NULL when len is 0 * @param len number of bytes to write