summaryrefslogtreecommitdiff
path: root/src/mts_fpga_hash.c
diff options
context:
space:
mode:
authorJohn Klug <john.klug@multitech.com>2017-08-18 15:14:17 -0500
committerJohn Klug <john.klug@multitech.com>2017-08-18 15:14:17 -0500
commit3ff432faedee2ec0b93d163e4808f391d65f1ba2 (patch)
tree59df462ea8586a664a59d4cf62f1a58af6507f7e /src/mts_fpga_hash.c
parentc2da95882ea7a4218a4432ee5da8165aa61addfc (diff)
downloadmts-id-eeprom-3ff432faedee2ec0b93d163e4808f391d65f1ba2.tar.gz
mts-id-eeprom-3ff432faedee2ec0b93d163e4808f391d65f1ba2.tar.bz2
mts-id-eeprom-3ff432faedee2ec0b93d163e4808f391d65f1ba2.zip
mts-fpga-loader
Diffstat (limited to 'src/mts_fpga_hash.c')
-rw-r--r--src/mts_fpga_hash.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/mts_fpga_hash.c b/src/mts_fpga_hash.c
new file mode 100644
index 0000000..7bd4da4
--- /dev/null
+++ b/src/mts_fpga_hash.c
@@ -0,0 +1,127 @@
+/* -------------------------------------------------------------------------- */
+/* --- DEPENDANCIES --------------------------------------------------------- */
+#include <errno.h>
+#include <fcntl.h> /* open */
+#include <openssl/sha.h> /* generate hash */
+#include <stdio.h> /* printf fprintf */
+#include <stdlib.h> /* malloc free */
+#include <stdint.h> /* C99 types */
+#include <string.h> /* 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*/
+static const char *valid_mtcap_hashes[] = {
+ "07317fe9ca59393c074215c9d923d8d01025654883291a5e89b27d21668e2263", /* v28*/
+ "f208ef5cae03e703951bb8799172a5eaadb74ddb90bf3e65c32030c008a88e75", /* v31*/
+ "aaecd468b187703dbbf76022b00268dba2a5f25300da6486d420f476c836385c"};/* v33*/
+/* -------------------------------------------------------------------------- */
+/* --- 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, 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;
+} \ No newline at end of file