a752c7ab
elopes
add first test an...
|
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright (C) 2016 Martin Landsmann <martin.landsmann@haw-hamburg.de>
* Copyright 2016 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "embUnit/embUnit.h"
#include "hashes/sha256.h"
#include "tests-hashes.h"
static int compare_str_vs_digest(const char *str,
const uint8_t hash[SHA256_DIGEST_LENGTH])
{
char ch[3] = { 0, 0, 0 };
size_t iter_hash = 0;
size_t str_length = strlen((char*)str);
for (size_t i = 0; i < str_length; i += 2) {
ch[0] = str[i];
ch[1] = str[i + 1];
if (hash[iter_hash++] != strtol(ch, NULL, 16)) {
return 0;
}
}
return 1;
}
static void test_hashes_hmac_sha256_hash_sequence(void)
{
unsigned char key[64];
/* prepare an empty key */
memset((void*)key, 0x0, 64);
static uint8_t hmac[SHA256_DIGEST_LENGTH];
/* use an empty message */
const unsigned char *m = NULL;
hmac_sha256(key, sizeof(key), m, 0, hmac);
TEST_ASSERT(compare_str_vs_digest(
"b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", hmac));
/* use a real message */
const unsigned char str[] = "The quick brown fox jumps over the lazy dog";
key[0] = 'k';
key[1] = 'e';
key[2] = 'y';
hmac_sha256(key, sizeof(key), str, strlen((char*)str), hmac);
TEST_ASSERT(compare_str_vs_digest(
"f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8", hmac));
}
/*
The followig testcases are taken from:
https://tools.ietf.org/html/rfc4868#section-2.7.1
*/
static void test_hashes_hmac_sha256_hash_PRF1(void)
{
/* Test Case PRF-1: */
const unsigned char strPRF1[] = "Hi There";
unsigned char key[20];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(key, 0x0b, sizeof(key));
hmac_sha256(key, sizeof(key), strPRF1, strlen((char*)strPRF1), hmac);
TEST_ASSERT(compare_str_vs_digest(
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", hmac));
}
static void test_hashes_hmac_sha256_hash_PRF2(void)
{
/* Test Case PRF-2: */
const unsigned char strPRF2[] = "what do ya want for nothing?";
unsigned char key[4] = {'J', 'e', 'f', 'e'};
static unsigned char hmac[SHA256_DIGEST_LENGTH];
hmac_sha256(key, sizeof(key), strPRF2, strlen((char*)strPRF2), hmac);
TEST_ASSERT(compare_str_vs_digest(
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", hmac));
}
static void test_hashes_hmac_sha256_hash_PRF3(void)
{
/* Test Case PRF-3: */
char unsigned strPRF3[50];
unsigned char key[20];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(strPRF3, 0xdd, sizeof(strPRF3));
memset(key, 0xaa, sizeof(key));
hmac_sha256(key, sizeof(key), strPRF3, sizeof(strPRF3), hmac);
TEST_ASSERT(compare_str_vs_digest(
"773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", hmac));
}
static void test_hashes_hmac_sha256_hash_PRF4(void)
{
/* Test Case PRF-4: */
char unsigned strPRF4[50];
unsigned char key[25];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(strPRF4, 0xcd, sizeof(strPRF4));
/*
* set key to: 0102030405060708090a0b0c0d0e0f10111213141516171819
*/
for (size_t i = 0; i < sizeof(key); ++i) {
key[i] = i+1;
}
hmac_sha256(key, sizeof(key), strPRF4, sizeof(strPRF4), hmac);
TEST_ASSERT(compare_str_vs_digest(
"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b", hmac));
}
static void test_hashes_hmac_sha256_hash_PRF5(void)
{
/* Test Case PRF-5: */
const unsigned char strPRF5[] = "Test Using Larger Than Block-Size Key - Hash Key First";
unsigned char longKey[131];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(longKey, 0xaa, sizeof(longKey));
hmac_sha256(longKey, sizeof(longKey), strPRF5, strlen((char*)strPRF5), hmac);
TEST_ASSERT(compare_str_vs_digest(
"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", hmac));
}
static void test_hashes_hmac_sha256_hash_PRF6(void)
{
/* Test Case PRF-6: */
const unsigned char strPRF6[] = "This is a test using a larger than block-size key and a "
"larger than block-size data. The key needs to be hashed "
"before being used by the HMAC algorithm.";
unsigned char longKey[131];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(longKey, 0xaa, sizeof(longKey));
/* the same key is used as above: 131 x 0xa */
hmac_sha256(longKey, sizeof(longKey), strPRF6, strlen((char*)strPRF6), hmac);
TEST_ASSERT(compare_str_vs_digest(
"9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_sequence(void)
{
unsigned char key[64];
hmac_context_t ctx;
/* prepare an empty key */
memset((void*)key, 0x0, 64);
static uint8_t hmac[SHA256_DIGEST_LENGTH];
/* use an empty message */
const unsigned char *m = NULL;
hmac_sha256_init(&ctx, key, sizeof(key));
hmac_sha256_update(&ctx, m, 0);
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", hmac));
/* use a real message */
const unsigned char str[] = "The quick brown fox jumps over the lazy dog";
key[0] = 'k';
key[1] = 'e';
key[2] = 'y';
hmac_sha256_init(&ctx, key, sizeof(key));
hmac_sha256_update(&ctx, str, strlen((char*)str));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8", hmac));
}
/*
The followig testcases are taken from:
https://tools.ietf.org/html/rfc4868#section-2.7.1
*/
static void test_hashes_hmac_sha256_ite_hash_PRF1(void)
{
/* Test Case PRF-1: */
hmac_context_t ctx;
const unsigned char strPRF1[] = "Hi There";
unsigned char key[20];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(key, 0x0b, sizeof(key));
hmac_sha256_init(&ctx, key, sizeof(key));
hmac_sha256_update(&ctx, strPRF1, strlen((char*)strPRF1));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_PRF2(void)
{
/* Test Case PRF-2: */
hmac_context_t ctx;
const unsigned char strPRF2[] = "what do ya want for nothing?";
unsigned char key[4] = {'J', 'e', 'f', 'e'};
static unsigned char hmac[SHA256_DIGEST_LENGTH];
hmac_sha256_init(&ctx, key, sizeof(key));
hmac_sha256_update(&ctx, strPRF2, strlen((char*)strPRF2));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_PRF3(void)
{
/* Test Case PRF-3: */
hmac_context_t ctx;
char unsigned strPRF3[50];
unsigned char key[20];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(strPRF3, 0xdd, sizeof(strPRF3));
memset(key, 0xaa, sizeof(key));
hmac_sha256_init(&ctx, key, sizeof(key));
hmac_sha256_update(&ctx, strPRF3, sizeof(strPRF3));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_PRF4(void)
{
/* Test Case PRF-4: */
hmac_context_t ctx;
char unsigned strPRF4[50];
unsigned char key[25];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(strPRF4, 0xcd, sizeof(strPRF4));
/*
* set key to: 0102030405060708090a0b0c0d0e0f10111213141516171819
*/
for (size_t i = 0; i < sizeof(key); ++i) {
key[i] = i+1;
}
hmac_sha256_init(&ctx, key, sizeof(key));
hmac_sha256_update(&ctx, strPRF4, sizeof(strPRF4));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_PRF5(void)
{
/* Test Case PRF-5: */
hmac_context_t ctx;
const unsigned char strPRF5[] = "Test Using Larger Than Block-Size Key - Hash Key First";
unsigned char longKey[131];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(longKey, 0xaa, sizeof(longKey));
hmac_sha256_init(&ctx, longKey, sizeof(longKey));
hmac_sha256_update(&ctx, strPRF5, strlen((char*)strPRF5));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_PRF6(void)
{
/* Test Case PRF-6: */
hmac_context_t ctx;
const unsigned char strPRF6[] = "This is a test using a larger than block-size key and a "
"larger than block-size data. The key needs to be hashed "
"before being used by the HMAC algorithm.";
unsigned char longKey[131];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(longKey, 0xaa, sizeof(longKey));
/* the same key is used as above: 131 x 0xa */
hmac_sha256_init(&ctx, longKey, sizeof(longKey));
hmac_sha256_update(&ctx, strPRF6, strlen((char*)strPRF6));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2", hmac));
}
static void test_hashes_hmac_sha256_ite_hash_PRF6_split(void)
{
/* Test Case PRF-6: */
hmac_context_t ctx;
const unsigned char strPRF6_1[] = "This is a test using a larger than block-size key and a ";
const unsigned char strPRF6_2[] = "larger than block-size data. The key needs to be hashed ";
const unsigned char strPRF6_3[] = "before being used by the HMAC algorithm.";
unsigned char longKey[131];
static unsigned char hmac[SHA256_DIGEST_LENGTH];
memset(longKey, 0xaa, sizeof(longKey));
/* the same key is used as above: 131 x 0xa */
hmac_sha256_init(&ctx, longKey, sizeof(longKey));
hmac_sha256_update(&ctx, strPRF6_1, strlen((char*)strPRF6_1));
hmac_sha256_update(&ctx, strPRF6_2, strlen((char*)strPRF6_2));
hmac_sha256_update(&ctx, strPRF6_3, strlen((char*)strPRF6_3));
hmac_sha256_final(&ctx, hmac);
TEST_ASSERT(compare_str_vs_digest(
"9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2", hmac));
}
Test *tests_hashes_sha256_hmac_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_hashes_hmac_sha256_hash_sequence),
new_TestFixture(test_hashes_hmac_sha256_hash_PRF1),
new_TestFixture(test_hashes_hmac_sha256_hash_PRF2),
new_TestFixture(test_hashes_hmac_sha256_hash_PRF3),
new_TestFixture(test_hashes_hmac_sha256_hash_PRF4),
new_TestFixture(test_hashes_hmac_sha256_hash_PRF5),
new_TestFixture(test_hashes_hmac_sha256_hash_PRF6),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_sequence),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF1),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF2),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF3),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF4),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF5),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF6),
new_TestFixture(test_hashes_hmac_sha256_ite_hash_PRF6_split),
};
EMB_UNIT_TESTCALLER(hashes_sha256_tests, NULL, NULL,
fixtures);
return (Test *)&hashes_sha256_tests;
}
|