diff options
author | John Klug <john.klug@multitech.com> | 2023-09-20 17:12:27 -0500 |
---|---|---|
committer | John Klug <john.klug@multitech.com> | 2023-09-20 17:12:27 -0500 |
commit | 40a09c25bf1dd08db8b526b274c2f24c22458206 (patch) | |
tree | a3548cb0c979046a628a5da939d9fb3dd9e10b2e | |
parent | 6130937e60b8eb8388b7043a4325abbb55052e2a (diff) | |
download | mts-io-sysfs-40a09c25bf1dd08db8b526b274c2f24c22458206.tar.gz mts-io-sysfs-40a09c25bf1dd08db8b526b274c2f24c22458206.tar.bz2 mts-io-sysfs-40a09c25bf1dd08db8b526b274c2f24c22458206.zip |
Move Mtac15Fpga.cpp to OpenSSL 3
-rw-r--r-- | src/AccessoryCards/Mtac15Fpga.cpp | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/src/AccessoryCards/Mtac15Fpga.cpp b/src/AccessoryCards/Mtac15Fpga.cpp index ef3b7bd..a6ac838 100644 --- a/src/AccessoryCards/Mtac15Fpga.cpp +++ b/src/AccessoryCards/Mtac15Fpga.cpp @@ -14,7 +14,7 @@ * WRITTEN AGREEMENT SIGNED BY MULTI-TECH SYSTEMS, INC. IS PROHIBITED. * ***********************************************************************/ - +#include <openssl/evp.h> #include "Fpga.h" /* -------------------------------------------------------------------------- */ @@ -85,24 +85,45 @@ void Mtac15Fpga::sha256_hash_string(unsigned char hash[SHA256_DIGEST_LENGTH], } /* Initialize, update and finalize sha256 */ -void Mtac15Fpga::sha256(char *string, char outputBuffer[65]) { - unsigned char hash[SHA256_DIGEST_LENGTH]; - int len; - SHA256_CTX sha256; - SHA256_Init(&sha256); +#define OUTPUTBUFFERSZ 65 +void Mtac15Fpga::sha256(char *string, char outputBuffer[OUTPUTBUFFERSZ]) { + unsigned int len; + EVP_MD_CTX *mdctx; + unsigned char *digest; + + mdctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); + len = strlen(string); - SHA256_Update(&sha256, string, len); - SHA256_Final(hash, &sha256); - int i = 0; - for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { - sprintf(outputBuffer + (i * 2), "%02x", (unsigned char)hash[i]); + EVP_DigestUpdate(mdctx, string, len); + + digest = (unsigned char *)OPENSSL_malloc(EVP_MD_size(EVP_sha256())); + + EVP_DigestFinal_ex(mdctx, digest, &len); + + int total = 0; + for (unsigned int i = 0; i < len; i++) { + if(total + 2 <= OUTPUTBUFFERSZ - 1) + total += sprintf(outputBuffer + (i * 2), "%02x", (unsigned char)digest[i]); + else { + printError("%s:%s: overflow of outputBuffer %d/%d\n", + __FILE__,__FUNCTION__,total+2,EVP_MD_size(EVP_sha256())); + abort(); + } } outputBuffer[64] = 0; printInfo("OutputBuffer finalized: %s", outputBuffer); + EVP_MD_CTX_free(mdctx); + OPENSSL_free(digest); } /* Open input file and verify sha256 with verified list */ int Mtac15Fpga::sha256_file(const char *path) { + unsigned int len; + EVP_MD_CTX *mdctx; + unsigned char *digest; + + printInfo("Checking hash on input file: %s", path); FILE *file = fopen(path, "rb"); @@ -115,9 +136,10 @@ int Mtac15Fpga::sha256_file(const char *path) { unsigned int i; char file_hash[65]; - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_CTX sha256; - SHA256_Init(&sha256); + + mdctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); + const int bufSize = 32768; unsigned char *buffer = (unsigned char *)malloc(bufSize); int bytesRead = 0; @@ -125,10 +147,11 @@ int Mtac15Fpga::sha256_file(const char *path) { if (!buffer) return ENOMEM; while ((bytesRead = fread(buffer, 1, bufSize, file))) { - SHA256_Update(&sha256, buffer, bytesRead); + EVP_DigestUpdate(mdctx, buffer, bytesRead); } - SHA256_Final(hash, &sha256); - sha256_hash_string(hash, file_hash); + digest = (unsigned char *)OPENSSL_malloc(EVP_MD_size(EVP_sha256())); + EVP_DigestFinal_ex(mdctx, digest, &len); + sha256_hash_string(digest, file_hash); fclose(file); free(buffer); |