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
|
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "aes.h"
#include "des.h"
#include "blowfish.h"
#include "twofish.h"
#include "md4.h"
#include "md5.h"
#include "sha1.h"
#include "sha256.h"
#include "sha512.h"
int main(void)
{
int i,j;
struct blowfish_ctx bf_ctx;
struct twofish_ctx tf_ctx;
struct aes_ctx a_ctx;
struct des_ctx d_ctx;
struct des3_ede_ctx d3_ctx;
struct md4_ctx md4;
struct md5_ctx md5;
struct sha1_ctx sha1;
struct sha256_ctx sha256;
struct sha512_ctx sha512;
u32 flags;
u8 key[]="9_&pass/#word%_0";
const u8 phrase[64] = "this#is/only|a?test,@nothing%more¬hing~less 0123456789 voila";
u8 encrypt[64] = "";
u8 decrypt[64] = "";
u8 md4_hash[MD4_DIGEST_SIZE];
u8 md5_hash[MD5_DIGEST_SIZE];
u8 sha1_hash[SHA1_DIGEST_SIZE];
u8 sha256_hash[SHA256_DIGEST_SIZE];
u8 sha512_hash[SHA512_DIGEST_SIZE];
const u8 T1[64] = "123456";
const u8 T2[64] = "789101";
printf("crypto api tests\n");
printf("\n\tused phrase : '%s'\n\tlength : %zu\n\n",phrase,strlen((char*)phrase));
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
/* test blowfish */
printf("_Blwofish: A 64-Bit Block Cipher_ by Bruce Schneier...\n");
if(blowfish_setkey(&bf_ctx, key, strlen((char*)key), &flags)!=0){
fprintf(stderr,"bf_setkey error.\n");
_exit(1);
}
for(i=0; i<sizeof(phrase)/BF_BLOCK_SIZE; i++){
blowfish_encrypt(&bf_ctx, encrypt, phrase+i*BF_BLOCK_SIZE);
blowfish_decrypt(&bf_ctx, decrypt+i*BF_BLOCK_SIZE, encrypt);
printf("\tencrypted block => ");
for(j=0; j<BF_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
}
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(phrase,decrypt,64)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
printf("\twith non-full blocks\n");
printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2);
blowfish_encrypt(&bf_ctx, encrypt, T1);
blowfish_encrypt(&bf_ctx, encrypt+BF_BLOCK_SIZE, T2);
printf("\tencrypted block => ");
for(j=0; j<BF_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
printf("\tencrypted block => ");
for(j=0; j<BF_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j+BF_BLOCK_SIZE]);
printf("\n");
blowfish_decrypt(&bf_ctx, decrypt, encrypt);
blowfish_decrypt(&bf_ctx, decrypt+strlen((char*)T1), encrypt+BF_BLOCK_SIZE);
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(decrypt,"123456789101",12)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
/* test twofish */
printf("_Twofish: A 128-Bit Block Cipher_ by Bruce Schneier...\n");
if(twofish_setkey(&tf_ctx, key, strlen((char*)key),&flags)!=0){
fprintf(stderr,"twofish_setkey error.\n");
_exit(1);
}
for(i=0; i<sizeof(phrase)/TF_BLOCK_SIZE; i++){
twofish_encrypt(&tf_ctx, encrypt, phrase+i*TF_BLOCK_SIZE);
twofish_decrypt(&tf_ctx, decrypt+i*TF_BLOCK_SIZE, encrypt);
printf("\tencrypted block => ");
for(j=0; j<TF_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
}
printf("\n");
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(phrase,decrypt,64)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
printf("\twith non-full blocks\n");
printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2);
twofish_encrypt(&bf_ctx, encrypt, T1);
twofish_encrypt(&bf_ctx, encrypt+TF_BLOCK_SIZE, T2);
printf("\tencrypted block => ");
for(j=0; j<TF_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
printf("\tencrypted block => ");
for(j=0; j<TF_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j+TF_BLOCK_SIZE]);
printf("\n");
twofish_decrypt(&bf_ctx, decrypt, encrypt);
twofish_decrypt(&bf_ctx, decrypt+strlen((char*)T1), encrypt+TF_BLOCK_SIZE);
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(decrypt,"123456789101",12)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
/* test aes */
printf("_aes \n");
if(aes_setkey(&a_ctx, key, strlen((char*)key),&flags)!=0){
fprintf(stderr,"aes_setkey error.\n");
_exit(1);
}
for(i=0; i<sizeof(phrase)/AES_BLOCK_SIZE; i++){
aes_encrypt(&a_ctx, encrypt, phrase+i*AES_BLOCK_SIZE);
aes_decrypt(&a_ctx, decrypt+i*AES_BLOCK_SIZE, encrypt);
printf("\tencrypted block => ");
for(j=0; j<AES_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
}
printf("\n");
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(phrase,decrypt,64)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
printf("\twith non-full blocks\n");
printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2);
aes_encrypt(&a_ctx, encrypt, T1);
aes_encrypt(&a_ctx, encrypt+AES_BLOCK_SIZE, T2);
printf("\tencrypted block => ");
for(j=0; j<AES_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
printf("\tencrypted block => ");
for(j=0; j<AES_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j+AES_BLOCK_SIZE]);
printf("\n");
aes_decrypt(&a_ctx, decrypt, encrypt);
aes_decrypt(&a_ctx, decrypt+strlen((char*)T1), encrypt+AES_BLOCK_SIZE);
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(decrypt,"123456789101",12)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
/* test des */
printf("_des \n");
if(des_setkey(&d_ctx, key, strlen((char*)key),&flags)!=0){
fprintf(stderr,"des_setkey error.\n");
_exit(1);
}
for(i=0; i<sizeof(phrase)/DES_BLOCK_SIZE; i++){
des_encrypt(&d_ctx, encrypt, phrase+i*DES_BLOCK_SIZE);
des_decrypt(&d_ctx, decrypt+i*DES_BLOCK_SIZE, encrypt);
printf("\tencrypted block => ");
for(j=0; j<DES_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
}
printf("\n");
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(phrase,decrypt,64)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
printf("\twith non-full blocks\n");
printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2);
des_encrypt(&d_ctx, encrypt, T1);
des_encrypt(&d_ctx, encrypt+DES_BLOCK_SIZE, T2);
printf("\tencrypted block => ");
for(j=0; j<DES_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
printf("\tencrypted block => ");
for(j=0; j<DES_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j+DES_BLOCK_SIZE]);
printf("\n");
des_decrypt(&d_ctx, decrypt, encrypt);
des_decrypt(&d_ctx, decrypt+strlen((char*)T1), encrypt+DES_BLOCK_SIZE);
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(decrypt,"123456789101",12)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
/* test des3_ede */
printf("_des3_ede \n");
if(des3_ede_setkey(&d3_ctx, key, strlen((char*)key),&flags)!=0){
fprintf(stderr,"des_setkey error.\n");
_exit(1);
}
for(i=0; i<sizeof(phrase)/DES3_EDE_BLOCK_SIZE; i++){
des3_ede_encrypt(&d3_ctx, encrypt, phrase+i*DES3_EDE_BLOCK_SIZE);
des3_ede_decrypt(&d3_ctx, decrypt+i*DES3_EDE_BLOCK_SIZE, encrypt);
printf("\tencrypted block => ");
for(j=0; j<DES3_EDE_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
}
printf("\n");
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(phrase,decrypt,64)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
printf("\twith non-full blocks\n");
printf("\tfirst string : '%s'\n\tsecond string : '%s'\n",T1,T2);
des3_ede_encrypt(&d3_ctx, encrypt, T1);
des3_ede_encrypt(&d3_ctx, encrypt+DES3_EDE_BLOCK_SIZE, T2);
printf("\tencrypted block => ");
for(j=0; j<DES3_EDE_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j]);
printf("\n");
printf("\tencrypted block => ");
for(j=0; j<DES3_EDE_BLOCK_SIZE; j++)
printf("%02x ",encrypt[j+DES3_EDE_BLOCK_SIZE]);
printf("\n");
des3_ede_decrypt(&d3_ctx, decrypt, encrypt);
des3_ede_decrypt(&d3_ctx, decrypt+strlen((char*)T1), encrypt+DES3_EDE_BLOCK_SIZE);
printf("\n\tdecrypted => '%s'\n\n",decrypt);
assert(memcmp(decrypt,"123456789101",12)==0);
memset(encrypt,0,sizeof(encrypt));
memset(encrypt,0,sizeof(decrypt));
/* md4 */
printf("MD4: one way hash with a %d bits output digest.\n",MD4_DIGEST_SIZE);
md4_init(&md4);
md4_update(&md4,(u8*)phrase,32);
md4_update(&md4,(u8*)phrase+32,strlen((char*)phrase)-32);
md4_final(&md4,md4_hash);
printf("\n\tdigest : ");
for(i=0; i<MD4_DIGEST_SIZE; i++)
printf("%02x",md4_hash[i]);
printf("\n\n");
/* md5 */
printf("MD5: one way hash with a %d bits output digest.\n",MD5_DIGEST_SIZE);
md5_init(&md5);
md5_update(&md5,(u8*)phrase,32);
md5_update(&md5,(u8*)phrase+32,strlen((char*)phrase)-32);
md5_final(&md5,md5_hash);
printf("\n\tdigest : ");
for(i=0; i<MD5_DIGEST_SIZE; i++)
printf("%02x",md5_hash[i]);
printf("\n\n");
/* sha1 */
printf("SHA1: one way hash with a %d bits output digest.\n",SHA1_DIGEST_SIZE);
sha1_init(&sha1);
sha1_update(&sha1,(u8*)phrase,32);
sha1_update(&sha1,(u8*)phrase+32,strlen((char*)phrase)-32);
sha1_final(&sha1,sha1_hash);
printf("\n\tdigest : ");
for(i=0; i<SHA1_DIGEST_SIZE; i++)
printf("%02x",sha1_hash[i]);
printf("\n\n");
/* sha256 */
printf("SHA256: one way hash with a %d bits output digest.\n",SHA256_DIGEST_SIZE);
sha256_init(&sha256);
sha256_update(&sha256,(u8*)phrase,32);
sha256_update(&sha256,(u8*)phrase+32,strlen((char*)phrase)-32);
sha256_final(&sha256,sha256_hash);
printf("\n\tdigest : ");
for(i=0; i<SHA256_DIGEST_SIZE; i++)
printf("%02x",sha256_hash[i]);
printf("\n\n");
/* sha512 */
printf("SHA512: one way hash with a %d bits output digest.\n",SHA512_DIGEST_SIZE);
sha512_init(&sha512);
sha512_update(&sha512,(u8*)phrase,32);
sha512_update(&sha512,(u8*)phrase+32,strlen((char*)phrase)-32);
sha512_final(&sha512,sha512_hash);
printf("\n\tdigest : ");
for(i=0; i<SHA512_DIGEST_SIZE/2; i++)
printf("%02x",sha512_hash[i]);
printf("\n\t ");
for(; i<SHA512_DIGEST_SIZE; i++)
printf("%02x",sha512_hash[i]);
printf("\n\n");
printf("tests ok\n");
return 0;
}
|