aboutsummaryrefslogtreecommitdiff
path: root/tests/test-hash.c
blob: c13e49c56f70afca7a9376ce74ad676bbdf1691c (plain) (blame)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Test code for hash cores.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include <cryptech.h>

/* Usual NIST sample messages. */

static const uint8_t nist_512_single[] = { /* 3 bytes */
  0x61, 0x62, 0x63
};

static const uint8_t sha1_single_digest[] = { /* 40 bytes */
  0x61, 0x39, 0x39, 0x39, 0x33, 0x65, 0x33, 0x36, 0x34, 0x37, 0x30, 0x36,
  0x38, 0x31, 0x36, 0x61, 0x62, 0x61, 0x33, 0x65, 0x32, 0x35, 0x37, 0x31,
  0x37, 0x38, 0x35, 0x30, 0x63, 0x32, 0x36, 0x63, 0x39, 0x63, 0x64, 0x30,
  0x64, 0x38, 0x39, 0x64
};

static const uint8_t sha256_single_digest[] = { /* 64 bytes */
  0x62, 0x61, 0x37, 0x38, 0x31, 0x36, 0x62, 0x66, 0x38, 0x66, 0x30, 0x31,
  0x63, 0x66, 0x65, 0x61, 0x34, 0x31, 0x34, 0x31, 0x34, 0x30, 0x64, 0x65,
  0x35, 0x64, 0x61, 0x65, 0x32, 0x32, 0x32, 0x33, 0x62, 0x30, 0x30, 0x33,
  0x36, 0x31, 0x61, 0x33, 0x39, 0x36, 0x31, 0x37, 0x37, 0x61, 0x39, 0x63,
  0x62, 0x34, 0x31, 0x30, 0x66, 0x66, 0x36, 0x31, 0x66, 0x32, 0x30, 0x30,
  0x31, 0x35, 0x61, 0x64
};

static const uint8_t nist_512_double[] = { /* 56 bytes */
  0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65, 0x63, 0x64, 0x65, 0x66,
  0x64, 0x65, 0x66, 0x67, 0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
  0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b, 0x69, 0x6a, 0x6b, 0x6c,
  0x6a, 0x6b, 0x6c, 0x6d, 0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
  0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
};

static const uint8_t sha1_double_digest[] = { /* 40 bytes */
  0x38, 0x34, 0x39, 0x38, 0x33, 0x65, 0x34, 0x34, 0x31, 0x63, 0x33, 0x62,
  0x64, 0x32, 0x36, 0x65, 0x62, 0x61, 0x61, 0x65, 0x34, 0x61, 0x61, 0x31,
  0x66, 0x39, 0x35, 0x31, 0x32, 0x39, 0x65, 0x35, 0x65, 0x35, 0x34, 0x36,
  0x37, 0x30, 0x66, 0x31
};

static const uint8_t sha256_double_digest[] = { /* 64 bytes */
  0x32, 0x34, 0x38, 0x64, 0x36, 0x61, 0x36, 0x31, 0x64, 0x32, 0x30, 0x36,
  0x33, 0x38, 0x62, 0x38, 0x65, 0x35, 0x63, 0x30, 0x32, 0x36, 0x39, 0x33,
  0x30, 0x63, 0x33, 0x65, 0x36, 0x30, 0x33, 0x39, 0x61, 0x33, 0x33, 0x63,
  0x65, 0x34, 0x35, 0x39, 0x36, 0x34, 0x66, 0x66, 0x32, 0x31, 0x36, 0x37,
  0x66, 0x36, 0x65, 0x63, 0x65, 0x64, 0x64, 0x34, 0x31, 0x39, 0x64, 0x62,
  0x30, 0x36, 0x63, 0x31
};

static const uint8_t nist_1024_single[] = { /* 3 bytes */
  0x61, 0x62, 0x63
};

static const uint8_t sha384_single_digest[] = { /* 96 bytes */
  0x63, 0x62, 0x30, 0x30, 0x37, 0x35, 0x33, 0x66, 0x34, 0x35, 0x61, 0x33,
  0x35, 0x65, 0x38, 0x62, 0x62, 0x35, 0x61, 0x30, 0x33, 0x64, 0x36, 0x39,
  0x39, 0x61, 0x63, 0x36, 0x35, 0x30, 0x30, 0x37, 0x32, 0x37, 0x32, 0x63,
  0x33, 0x32, 0x61, 0x62, 0x30, 0x65, 0x64, 0x65, 0x64, 0x31, 0x36, 0x33,
  0x31, 0x61, 0x38, 0x62, 0x36, 0x30, 0x35, 0x61, 0x34, 0x33, 0x66, 0x66,
  0x35, 0x62, 0x65, 0x64, 0x38, 0x30, 0x38, 0x36, 0x30, 0x37, 0x32, 0x62,
  0x61, 0x31, 0x65, 0x37, 0x63, 0x63, 0x32, 0x33, 0x35, 0x38, 0x62, 0x61,
  0x65, 0x63, 0x61, 0x31, 0x33, 0x34, 0x63, 0x38, 0x32, 0x35, 0x61, 0x37
};

static const uint8_t sha512_single_digest[] = { /* 128 bytes */
  0x64, 0x64, 0x61, 0x66, 0x33, 0x35, 0x61, 0x31, 0x39, 0x33, 0x36, 0x31,
  0x37, 0x61, 0x62, 0x61, 0x63, 0x63, 0x34, 0x31, 0x37, 0x33, 0x34, 0x39,
  0x61, 0x65, 0x32, 0x30, 0x34, 0x31, 0x33, 0x31, 0x31, 0x32, 0x65, 0x36,
  0x66, 0x61, 0x34, 0x65, 0x38, 0x39, 0x61, 0x39, 0x37, 0x65, 0x61, 0x32,
  0x30, 0x61, 0x39, 0x65, 0x65, 0x65, 0x65, 0x36, 0x34, 0x62, 0x35, 0x35,
  0x64, 0x33, 0x39, 0x61, 0x32, 0x31, 0x39, 0x32, 0x39, 0x39, 0x32, 0x61,
  0x32, 0x37, 0x34, 0x66, 0x63, 0x31, 0x61, 0x38, 0x33, 0x36, 0x62, 0x61,
  0x33, 0x63, 0x32, 0x33, 0x61, 0x33, 0x66, 0x65, 0x65, 0x62, 0x62, 0x64,
  0x34, 0x35, 0x34, 0x64, 0x34, 0x34, 0x32, 0x33, 0x36, 0x34, 0x33, 0x63,
  0x65, 0x38, 0x30, 0x65, 0x32, 0x61, 0x39, 0x61, 0x63, 0x39, 0x34, 0x66,
  0x61, 0x35, 0x34, 0x63, 0x61, 0x34, 0x39, 0x66
};

static const uint8_t nist_1024_double[] = { /* 112 bytes */
  0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x62, 0x63, 0x64, 0x65,
  0x66, 0x67, 0x68, 0x69, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
  0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x65, 0x66, 0x67, 0x68,
  0x69, 0x6a, 0x6b, 0x6c, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
  0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x68, 0x69, 0x6a, 0x6b,
  0x6c, 0x6d, 0x6e, 0x6f, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
  0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x6b, 0x6c, 0x6d, 0x6e,
  0x6f, 0x70, 0x71, 0x72, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
  0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x6e, 0x6f, 0x70, 0x71,
  0x72, 0x73, 0x74, 0x75
};

static const uint8_t sha384_double_digest[] = { /* 96 bytes */
  0x30, 0x39, 0x33, 0x33, 0x30, 0x63, 0x33, 0x33, 0x66, 0x37, 0x31, 0x31,
  0x34, 0x37, 0x65, 0x38, 0x33, 0x64, 0x31, 0x39, 0x32, 0x66, 0x63, 0x37,
  0x38, 0x32, 0x63, 0x64, 0x31, 0x62, 0x34, 0x37, 0x35, 0x33, 0x31, 0x31,
  0x31, 0x62, 0x31, 0x37, 0x33, 0x62, 0x33, 0x62, 0x30, 0x35, 0x64, 0x32,
  0x32, 0x66, 0x61, 0x30, 0x38, 0x30, 0x38, 0x36, 0x65, 0x33, 0x62, 0x30,
  0x66, 0x37, 0x31, 0x32, 0x66, 0x63, 0x63, 0x37, 0x63, 0x37, 0x31, 0x61,
  0x35, 0x35, 0x37, 0x65, 0x32, 0x64, 0x62, 0x39, 0x36, 0x36, 0x63, 0x33,
  0x65, 0x39, 0x66, 0x61, 0x39, 0x31, 0x37, 0x34, 0x36, 0x30, 0x33, 0x39
};

static const uint8_t sha512_double_digest[] = { /* 128 bytes */
  0x38, 0x65, 0x39, 0x35, 0x39, 0x62, 0x37, 0x35, 0x64, 0x61, 0x65, 0x33,
  0x31, 0x33, 0x64, 0x61, 0x38, 0x63, 0x66, 0x34, 0x66, 0x37, 0x32, 0x38,
  0x31, 0x34, 0x66, 0x63, 0x31, 0x34, 0x33, 0x66, 0x38, 0x66, 0x37, 0x37,
  0x37, 0x39, 0x63, 0x36, 0x65, 0x62, 0x39, 0x66, 0x37, 0x66, 0x61, 0x31,
  0x37, 0x32, 0x39, 0x39, 0x61, 0x65, 0x61, 0x64, 0x62, 0x36, 0x38, 0x38,
  0x39, 0x30, 0x31, 0x38, 0x35, 0x30, 0x31, 0x64, 0x32, 0x38, 0x39, 0x65,
  0x34, 0x39, 0x30, 0x30, 0x66, 0x37, 0x65, 0x34, 0x33, 0x33, 0x31, 0x62,
  0x39, 0x39, 0x64, 0x65, 0x63, 0x34, 0x62, 0x35, 0x34, 0x33, 0x33, 0x61,
  0x63, 0x37, 0x64, 0x33, 0x32, 0x39, 0x65, 0x65, 0x62, 0x36, 0x64, 0x64,
  0x32, 0x36, 0x35, 0x34, 0x35, 0x65, 0x39, 0x36, 0x65, 0x35, 0x35, 0x62,
  0x38, 0x37, 0x34, 0x62, 0x65, 0x39, 0x30, 0x39
};

static int test_hash(hal_error_t (*hash)(void *, const uint8_t *, const size_t, uint8_t *, const size_t),
		     const uint8_t * const data, const size_t data_len,
		     const uint8_t * const result, const size_t result_len,
		     const char * const comment)
{
  uint8_t state[512], digest[512];
  hal_error_t err;

  assert(hash != NULL && data != NULL && result != NULL);

  assert(result_len <= sizeof(digest) && hal_hash_state_size() <= sizeof(state));

  if (comment != NULL)
    printf("%s\n", comment);

  printf("Initializing state\n");
  hal_hash_state_initialize(state);

  printf("Hashing data %lu bytes of data\n", (unsigned long) data_len);
  if ((err = hash(state, data, data_len, NULL, 0)) != HAL_OK) {
    printf("Failed: %s\n", hal_error_string(err));
    return 0;
  }

  printf("Finalizing hash and reading %lu bytes of result\n", (unsigned long) result_len);
  if ((err = hash(state, NULL, 0, digest, sizeof(digest))) != HAL_OK) {
    printf("Failed: %s\n", hal_error_string(err));
    return 0;
  }

  printf("Comparing result with known value");
  if (memcmp(result, digest, result_len)) {
    printf("MISMATCH\n");
    return 0;
  }

  printf("OK\n");
    return 1;
}

int main (int argc, char *argv[])
{
  int ok = 1;

  /*
   * Missing some tests here because I started from the Cryptlib test
   * script, which skips the 224 and 256 options of the SHA-512 core.
   */

  ok &= test_hash(hal_hash_sha1,   nist_512_single,  sizeof(nist_512_single),  sha1_single_digest,   sizeof(sha1_single_digest),
		  "SHA-1 single block test");
  ok &= test_hash(hal_hash_sha1,   nist_512_double,  sizeof(nist_512_double),  sha1_double_digest,   sizeof(sha1_double_digest),
		  "SHA-1 double block test");

  ok &= test_hash(hal_hash_sha256, nist_512_single,  sizeof(nist_512_single),  sha256_single_digest, sizeof(sha256_single_digest),
		  "SHA-256 single block test");
  ok &= test_hash(hal_hash_sha256, nist_512_double,  sizeof(nist_512_double),  sha256_double_digest, sizeof(sha256_double_digest),
		  "SHA-256 double block test");

  ok &= test_hash(hal_hash_sha384, nist_1024_single, sizeof(nist_1024_single), sha384_single_digest, sizeof(sha384_single_digest),
		  "SHA-384 single block test");
  ok &= test_hash(hal_hash_sha384, nist_1024_double, sizeof(nist_1024_double), sha384_double_digest, sizeof(sha384_double_digest),
		  "SHA-384 double block test");

  ok &= test_hash(hal_hash_sha512, nist_1024_single, sizeof(nist_1024_single), sha512_single_digest, sizeof(sha512_single_digest),
		  "SHA-512 single block test");
  ok &= test_hash(hal_hash_sha512, nist_1024_double, sizeof(nist_1024_double), sha512_double_digest, sizeof(sha512_double_digest),
		  "SHA-512 double block test");

  return !ok;
}