-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsha256test.c
89 lines (87 loc) · 2.3 KB
/
sha256test.c
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
#include <stdio.h>
#include <string.h>
#include "sha256.h"
#include "sha256test.h"
int test(char *hex, char *value)
{
sha256context ctx;
sha256_init(ctx);
size_t len = strlen(hex) / 2;
unsigned char data[len];
hextobs(data, hex);
sha256_write(ctx, data, len);
char hash[32];
sha256_finish(ctx, hash);
char hstr[64];
sprint_h(hstr, hash, 32);
int ret = strcmp(hstr, value);
if (ret != 0)
{
printf("%d %s %s\n", ret, hash, value);
}
return ret;
}
int main()
{
int size, ret;
size = sizeof(TEST_CASE_HMAC_SHA256) / sizeof(TEST_CASE_HMAC_SHA256[0]);
for (int i = 0; i < size; i += 3)
{
unsigned char hmac[32];
char *Key = TEST_CASE_HMAC_SHA256[i];
char *Data = TEST_CASE_HMAC_SHA256[i + 1];
char *HMAC = TEST_CASE_HMAC_SHA256[i + 2];
unsigned char key[strlen(Key) / 2];
unsigned char data[strlen(Data) / 2];
hextobs(key, Key);
hextobs(data, Data);
hmac_sha256(hmac, key, strlen(Key) / 2, data, strlen(Data) / 2);
unsigned char hmachex[64];
sprint_h(hmachex, hmac, 32);
ret = strcmp(hmachex, HMAC);
if (ret != 0)
{
printf("%d %s\n", i, TEST_CASE_HMAC_SHA256[i]);
break;
}
}
if (ret != 0)
{
printf("TEST_CASE_HMAC_SHA256 Fail.\n");
return -1;
}
printf("TEST_CASE_HMAC_SHA256 Complete!\n");
size = sizeof(TEST_SHA256_SHORT) / sizeof(TEST_SHA256_SHORT[0]);
for (int i = 0; i < size; i += 2)
{
ret = test(TEST_SHA256_SHORT[i], TEST_SHA256_SHORT[i + 1]);
if (ret != 0)
{
printf("%d %s\n", i, TEST_SHA256_SHORT[i]);
break;
}
}
if (ret != 0)
{
printf("TEST_SHA256_SHORT Fail.\n");
return -1;
}
printf("TEST_SHA256_SHORT Complete!\n");
size = sizeof(TEST_SHA256_LONG) / sizeof(TEST_SHA256_LONG[0]);
for (int i = 0; i < size; i += 2)
{
ret = test(TEST_SHA256_LONG[i], TEST_SHA256_LONG[i + 1]);
if (ret != 0)
{
printf("%d %s\n", i, TEST_SHA256_LONG[i]);
break;
}
}
if (ret != 0)
{
printf("TEST_SHA256_LONG Fail.\n");
return -1;
}
printf("TEST_SHA256_LONG Complete!\n");
return 0;
}