/* -------------------------------------------------------------------------- */ /* --- DEPENDANCIES --------------------------------------------------------- */ #include #include /* open */ #include /* generate hash */ #include /* printf fprintf */ #include /* malloc free */ #include /* C99 types */ #include /* memset */ #include "mts_error_codes.h" /* error codes*/ #include "mts_fpga_reg.h" /* register functions*/ /* -------------------------------------------------------------------------- */ /* --- PRIVATE MACROS ------------------------------------------------------- */ #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #if DEBUG_HASH == 1 #define DEBUG_MSG(str) fprintf(stderr, str) #define DEBUG_PRINTF(fmt, args...) \ fprintf(stderr, "%s:%d: " fmt, __FUNCTION__, __LINE__, args) #define CHECK_NULL(a) \ if (a == NULL) { \ fprintf(stderr, "%s:%d: ERROR: NULL POINTER AS ARGUMENT\n", __FUNCTION__, \ __LINE__); \ return MTAC_REG_ERROR; \ } #else #define DEBUG_MSG(str) #define DEBUG_PRINTF(fmt, args...) #define CHECK_NULL(a) \ if (a == NULL) { \ return MTAC_REG_ERROR; \ } #endif /* -------------------------------------------------------------------------- */ /* --- PRIVATE CONSTANTS ---------------------------------------------------- */ static const char *valid_mtcdt_hashes[] = { "d9f811fcab57947db3c2323242885a32a7f095a069d3386a148466e7f3da5353", /* v28*/ "903c1199df46d38683b1aa9fc88310abe2f317c01c3aefa77987990874aba420", /* v31*/ "7c190506b969aea6198daffb6c9b47685f3a4dc3ce18565c66542bac27d6f24e", /* v33*/ "72bcdfda72bf8677d585330caa3d609615d08d4ca6d7951f0ebbcb5a93306b3c" /* v35*/ }; static const char *valid_mtcdt3_hashes[] = { "54e41b186b2c91f1bcf249648c50357165d361101fc4fe20ee9b8f0c40dce25d" /* v35*/ }; static const char *valid_mtcap_hashes[] = { "07317fe9ca59393c074215c9d923d8d01025654883291a5e89b27d21668e2263", /* v28*/ "f208ef5cae03e703951bb8799172a5eaadb74ddb90bf3e65c32030c008a88e75", /* v31*/ "aaecd468b187703dbbf76022b00268dba2a5f25300da6486d420f476c836385c", /* v33*/ "876cc5683f612c09f96bacb27fff170358c90f3bd76a5c61ec41504eabba8313" /* v35*/ }; /* -------------------------------------------------------------------------- */ /* --- PRIVATE FUNCTIONS DEFINITION ------------------------------------------ */ /* hash outputBuffer */ static void sha256_hash_string(char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65]) { int i = 0; for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { sprintf(outputBuffer + (i * 2), "%02x", (unsigned char)hash[i]); } outputBuffer[64] = 0; DEBUG_PRINTF("OutputBuffer hash: %s\n", outputBuffer); } /* Initialize, update and finalize sha256 */ static void sha256(char *string, char outputBuffer[65]) { unsigned char hash[SHA256_DIGEST_LENGTH]; int len; SHA256_CTX sha256; SHA256_Init(&sha256); 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]); } outputBuffer[64] = 0; DEBUG_PRINTF("OutputBuffer finalized: %s\n", outputBuffer); } /* -------------------------------------------------------------------------- */ /* --- PUBLIC FUNCTIONS DEFINITION ------------------------------------------ */ /* Open input file and verify sha256 with verified list */ int sha256_file(char* hw, char *path) { DEBUG_PRINTF("Checking hash on input file: %s\n", path); FILE *file = fopen(path, "rb"); if (!file) { printf("File %s not found\n", path); return MTAC_FILE_NOT_FOUND; } else { printf("Checking file %s\n", path); } char file_hash[65]; unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); const int bufSize = 32768; unsigned char *buffer = malloc(bufSize); int bytesRead = 0; if (!buffer) return ENOMEM; while ((bytesRead = fread(buffer, 1, bufSize, file))) { SHA256_Update(&sha256, buffer, bytesRead); } SHA256_Final(hash, &sha256); sha256_hash_string(hash, file_hash); fclose(file); free(buffer); DEBUG_PRINTF("Calculated input file hash: %s\n", file_hash); int i; if (strstr(hw, MTCAP)) { for(i = 0; i < sizeof(valid_mtcap_hashes)/sizeof(valid_mtcap_hashes[0]); ++i) { if(!strcmp(valid_mtcap_hashes[i], file_hash)) { printf("File verified\n"); return MTAC_SUCCESS; } } } else if (strstr(hw, MTCDT3)) { for(i = 0; i < sizeof(valid_mtcdt3_hashes)/sizeof(valid_mtcdt3_hashes[0]); ++i) { if(!strcmp(valid_mtcdt3_hashes[i], file_hash)) { printf("File verified\n"); return MTAC_SUCCESS; } } } else if (strstr(hw, MTCDT)) { for(i = 0; i < sizeof(valid_mtcdt_hashes)/sizeof(valid_mtcdt_hashes[0]); ++i) { if(!strcmp(valid_mtcdt_hashes[i], file_hash)) { printf("File verified\n"); return MTAC_SUCCESS; } } } printf("Invalid input file.\n"); return MTAC_HASH_VERIFICATION_FAILURE; }