-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestkeycrypto.c
218 lines (165 loc) · 6.88 KB
/
testkeycrypto.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
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
/*--------------------------------------------------------------------*/
/* testkeycrypto.c */
/* Author: Gerry Wan */
/*--------------------------------------------------------------------*/
#include "keycrypto.h"
#include "sha256.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#define ASSURE(i) assure(i, __LINE__)
#define INTBUFLEN (sizeof(int) * 8 + 1)
#define ARRBUFLEN (sizeof(unsigned char) * 16 + 1)
/* If !iSuccessful, print a message to stdout indicating that the
test failed. */
static void assure(int iSuccessful, int iLineNum)
{
if (! iSuccessful)
{
printf("Test at line %d failed.\n", iLineNum);
fflush(stdout);
}
}
/*--------------------------------------------------------------------*/
static void print_hash(unsigned char hash[])
{
int idx;
for (idx=0; idx < 32; idx++)
printf("%02x",hash[idx]);
printf("\n");
}
/*--------------------------------------------------------------------*/
static void phex(unsigned char *str)
{
unsigned char i;
for (i = 0; i < 4; i++)
printf("%.2x", str[i]);
printf("\n");
}
/*--------------------------------------------------------------------*/
static void testBasics()
{
unsigned char aucKey0[] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
unsigned char aucKey1[] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01};
unsigned char aucKey2[] = {0x04, 0x00, 0x20, 0xff,
0x6d, 0x22, 0xa5, 0x1d};
unsigned char aucInput0[] = {0x00, 0x00, 0x00, 0x00,
0x72, 0x3d, 0x38, 0xae};
unsigned char aucInput1[] = {0xa5, 0x51, 0x0f, 0x58,
0xda, 0x91, 0xcd, 0xc2,
0x4b, 0x40, 0xd7, 0xff,
0x00, 0xe0, 0x8b, 0x68};
unsigned char aucInput2[] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x05};
unsigned char aucOutput00[] = {0x00, 0x00, 0x00, 0x00,
0x72, 0x3d, 0x38, 0xae};
unsigned char aucOutput01[] = {0xa5, 0x51, 0x0f, 0x58,
0xda, 0x91, 0xcd, 0xc2,
0x4b, 0x40, 0xd7, 0xff,
0x00, 0xe0, 0x8b, 0x68};
unsigned char aucOutput10[] = {0x00, 0x00, 0x00, 0x00,
0x72, 0x3d, 0x38, 0xaf};
unsigned char aucOutput11[] = {0xa5, 0x51, 0x0f, 0x58,
0xda, 0x91, 0xcd, 0xc3,
0x4b, 0x40, 0xd7, 0xff,
0x00, 0xe0, 0x8b, 0x69};
unsigned char aucOutput20[] = {0x04, 0x00, 0x20, 0xff,
0x1f, 0x1f, 0x9d, 0xb3};
unsigned char aucOutput21[] = {0xa1, 0x51, 0x2f, 0xa7,
0xb7, 0xb3, 0x68, 0xdf,
0x4f, 0x40, 0xf7, 0x00,
0x6d, 0xc2, 0x2e, 0x75};
unsigned char aucBufferShort[8];
unsigned char aucBufferLong[16];
printf("------------------------------------------------------\n");
printf("Testing XOR Encrypt and Decrypt.\n");
printf("No output should appear here:\n");
fflush(stdout);
memset(aucBufferShort, 0, 8);
memset(aucBufferLong, 0, 16);
xor_encrypt(aucInput0, aucBufferShort, 8, aucKey0);
ASSURE(memcmp(aucBufferShort, aucOutput00, 8) == 0);
xor_decrypt(aucOutput00, aucBufferShort, 8, aucKey0);
ASSURE(memcmp(aucBufferShort, aucInput0, 8) == 0);
xor_encrypt(aucInput1, aucBufferLong, 16, aucKey0);
ASSURE(memcmp(aucBufferLong, aucOutput01, 16) == 0);
xor_decrypt(aucOutput01, aucBufferLong, 16, aucKey0);
ASSURE(memcmp(aucBufferLong, aucInput1, 16) == 0);
xor_encrypt(aucInput0, aucBufferShort, 8, aucKey1);
ASSURE(memcmp(aucBufferShort, aucOutput10, 8) == 0);
xor_decrypt(aucOutput10, aucBufferShort, 8, aucKey1);
ASSURE(memcmp(aucBufferShort, aucInput0, 8) == 0);
xor_encrypt(aucInput1, aucBufferLong, 16, aucKey1);
ASSURE(memcmp(aucBufferLong, aucOutput11, 16) == 0);
xor_decrypt(aucOutput11, aucBufferLong, 16, aucKey1);
ASSURE(memcmp(aucBufferLong, aucInput1, 16) == 0);
xor_encrypt(aucInput0, aucBufferShort, 8, aucKey2);
ASSURE(memcmp(aucBufferShort, aucOutput20, 8) == 0);
xor_encrypt(aucOutput20, aucBufferShort, 8, aucKey2);
ASSURE(memcmp(aucBufferShort, aucInput0, 8) == 0);
xor_encrypt(aucInput1, aucBufferLong, 16, aucKey2);
ASSURE(memcmp(aucBufferLong, aucOutput21, 16) == 0);
xor_decrypt(aucOutput21, aucBufferLong, 16, aucKey2);
ASSURE(memcmp(aucBufferLong, aucInput1, 16) == 0);
}
/*--------------------------------------------------------------------*/
static void testHash()
{
char aucKeyID_0[] = "0";
char aucKeyID_00[] = "00";
char aucKeyID_000[] = "000";
int i = 5;
int j = 2147483647;
int k = 5214748;
unsigned char aucKey0[] = {0x01, 0x23, 0x45, 0x67};
unsigned char aucKey1[] = {0x01, 0x23, 0x00, 0x67};
int iter;
char intBuf[INTBUFLEN];
char ucBuf[ARRBUFLEN];
unsigned char hash[32];
SHA256_CTX ctx;
printf("------------------------------------------------------\n");
printf("Testing Hashing.\n");
printf("Some 32 byte hash outputs should appear here:\n");
fflush(stdout);
sha256_init(&ctx);
sha256_update(&ctx,aucKeyID_0,strlen(aucKeyID_0));
sha256_update(&ctx,aucKeyID_00,strlen(aucKeyID_00));
sha256_final(&ctx,hash);
print_hash(hash);
sha256_init(&ctx);
sha256_update(&ctx,aucKeyID_000,strlen(aucKeyID_000));
sha256_final(&ctx,hash);
print_hash(hash);
sha256_init(&ctx);
intToString(i, intBuf);
sha256_update(&ctx,intBuf,strlen(intBuf));
intToString(j, intBuf);
// printf("String: %s\n", intBuf);
sha256_update(&ctx,intBuf,strlen(intBuf));
sha256_final(&ctx,hash);
print_hash(hash);
sha256_init(&ctx);
intToString(k, intBuf);
// printf("String: %s\n", intBuf);
sha256_update(&ctx,intBuf,strlen(intBuf));
sha256_final(&ctx,hash);
print_hash(hash);
sha256_init(&ctx);
arrToString(aucKey0, ucBuf, ARRBUFLEN);
// printf("String: %s, %d\n", ucBuf, strlen(ucBuf));
sha256_update(&ctx,ucBuf,strlen(ucBuf));
sha256_final(&ctx,hash);
print_hash(hash);
}
/*--------------------------------------------------------------------*/
int main(void)
{
testBasics();
testHash();
printf("------------------------------------------------------\n");
printf("End of tests\n");
}