diff options
Diffstat (limited to 'src')
-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); |