summaryrefslogtreecommitdiff
path: root/src/mts_fpga_hash.c
blob: 7bd4da4a75b256ff237701351b80d45a191c04fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
}