1 |
#include "../lib/mcrypt-nm.h" |
2 |
#include <stdio.h> |
3 |
#include <strings.h> |
4 |
#include <stdlib.h> |
5 |
#include <string.h> |
6 |
|
7 |
/* Prints plaintext and ciphertext in hex for all the algorithms */ |
8 |
|
9 |
/* #define DEBUG */ |
10 |
|
11 |
/* $Id: cipher_test.c,v 1.7 1999/11/06 00:29:22 nmav Exp $ */ |
12 |
|
13 |
char test0[200][200]; |
14 |
|
15 |
int main() |
16 |
{ |
17 |
|
18 |
MCRYPT td, td2; |
19 |
int i; |
20 |
int j, x = 0; |
21 |
unsigned char *keyword = NULL, *password; |
22 |
unsigned char *plaintext; |
23 |
unsigned char *ciphertext; |
24 |
unsigned char cipher_tmp[200]; |
25 |
char *name; |
26 |
int blocksize; |
27 |
|
28 |
|
29 |
printf |
30 |
("Checking whether the algorithm's encryption/decryption work normally\n"); |
31 |
|
32 |
/* Test vectors */ |
33 |
|
34 |
strcpy(test0[MCRYPT_BLOWFISH], "dec5fcfb5feb111b"); |
35 |
strcpy(test0[MCRYPT_DES], "a6a0623c9acd9a69"); |
36 |
strcpy(test0[MCRYPT_TRIPLEDES], "21c6b0ca2c2d37e3"); |
37 |
strcpy(test0[MCRYPT_THREEWAY], "6efbd64c1aa44091caf7109c"); |
38 |
strcpy(test0[MCRYPT_GOST], "cc99cd11b48378d4"); |
39 |
strcpy(test0[MCRYPT_SAFER_SK64], "c8a1490249dd916b"); |
40 |
strcpy(test0[MCRYPT_SAFER_SK128], "e394643fa1dbc74b"); |
41 |
strcpy(test0[MCRYPT_CAST_128], "b5bad40c18a3a133"); |
42 |
strcpy(test0[MCRYPT_XTEA], "2a6c2f9db5b776a4"); |
43 |
strcpy(test0[MCRYPT_RC2], "1180baf6f36a21e3"); |
44 |
strcpy(test0[MCRYPT_TWOFISH], |
45 |
"717669e1ca4cd3d4ba2e73adfb05d68c"); |
46 |
strcpy(test0[MCRYPT_CAST_256], "9a1eeb06e48614338e008ca30e30494c"); |
47 |
strcpy(test0[MCRYPT_SAFERPLUS], |
48 |
"96cd24a706ed2c54f3b9edbca1df8143"); |
49 |
strcpy(test0[MCRYPT_LOKI97], "b2913deee25f25dd6e31d727bdd77d4f"); |
50 |
strcpy(test0[MCRYPT_SERPENT], |
51 |
"3fc472889c60e2ba9e35d311646dad9c"); |
52 |
strcpy(test0[MCRYPT_RIJNDAEL_128], |
53 |
"621a5598eb65170f411d959377a0d510"); |
54 |
strcpy(test0[MCRYPT_ARCFOUR], |
55 |
"371e6d430e7bf997dd96aeb5d2b7a01a42525e60ea778730ac5ea11f99d1116efc0fa4512801eea15ab1f484bf883dfdabd6"); |
56 |
strcpy(test0[MCRYPT_ENIGMA], |
57 |
"3569e0b24f2e974540a2097a4a3788dfa4abd55527d3c771a6d9d1ab52c4a263915ef870ecdce8fa6d2c51edf73e0fc7b92a"); |
58 |
|
59 |
for (i = 0; i <= 150; i++) { |
60 |
|
61 |
if (mcrypt_is_ok_algorithm(i) != 0) |
62 |
continue; |
63 |
|
64 |
password = malloc(mcrypt_get_key_size(i)); |
65 |
keyword = calloc(1, mcrypt_get_key_size(i)); |
66 |
memset(password, '\2', mcrypt_get_key_size(i)); |
67 |
memset(password, '\3', mcrypt_get_key_size(i) / 2); |
68 |
memset(password, '\5', mcrypt_get_key_size(i) / 4); |
69 |
memmove(keyword, password, mcrypt_get_key_size(i)); |
70 |
|
71 |
|
72 |
if (mcrypt_is_block_algorithm(i) != 0) { |
73 |
bzero(cipher_tmp, sizeof(cipher_tmp)); |
74 |
td = |
75 |
mcrypt_generic_init(MCRYPT_ECB, i, keyword, |
76 |
mcrypt_get_key_size(i), |
77 |
NULL); |
78 |
td2 = |
79 |
mcrypt_generic_init(MCRYPT_ECB, i, keyword, |
80 |
mcrypt_get_key_size(i), |
81 |
NULL); |
82 |
} else { |
83 |
bzero(cipher_tmp, sizeof(cipher_tmp)); |
84 |
td = |
85 |
mcrypt_generic_init(MCRYPT_STREAM, i, keyword, |
86 |
mcrypt_get_key_size(i), |
87 |
NULL); |
88 |
td2 = |
89 |
mcrypt_generic_init(MCRYPT_STREAM, i, keyword, |
90 |
mcrypt_get_key_size(i), |
91 |
NULL); |
92 |
} |
93 |
|
94 |
if (td == MCRYPT_FAILED || td2 == MCRYPT_FAILED) { |
95 |
continue; |
96 |
} |
97 |
|
98 |
name = mcrypt_get_algorithms_name(i); |
99 |
if (name == NULL) |
100 |
continue; |
101 |
|
102 |
printf("Algorithm: %s... ", name); |
103 |
|
104 |
blocksize = mcrypt_get_block_size(i); |
105 |
if (blocksize == 1) { /* stream */ |
106 |
blocksize = 50; |
107 |
} |
108 |
|
109 |
ciphertext = malloc(blocksize); |
110 |
plaintext = malloc(blocksize); |
111 |
bzero(plaintext, blocksize); |
112 |
|
113 |
for (j = 0; j < blocksize; j++) { |
114 |
plaintext[j] = j % 256; |
115 |
} |
116 |
memcpy(ciphertext, plaintext, blocksize); |
117 |
|
118 |
#ifdef DEBUG |
119 |
printf("plaintext: "); |
120 |
for (j = 0; j < blocksize; j++) { |
121 |
printf("%.2x", ciphertext[j]); |
122 |
} |
123 |
#endif |
124 |
|
125 |
#ifdef DEBUG |
126 |
printf("\nkeyword: "); |
127 |
for (j = 0; j < mcrypt_get_key_size(i); j++) { |
128 |
printf("%.2x", keyword[j]); |
129 |
} |
130 |
#endif |
131 |
|
132 |
mcrypt_generic(td, ciphertext, blocksize); |
133 |
|
134 |
#ifdef DEBUG |
135 |
printf("\nciphertext: "); |
136 |
#endif |
137 |
for (j = 0; j < blocksize; j++) { |
138 |
sprintf(&((char *) cipher_tmp)[2 * j], "%.2x", |
139 |
ciphertext[j]); |
140 |
} |
141 |
|
142 |
#ifdef DEBUG |
143 |
printf("%s\n", cipher_tmp); |
144 |
#endif |
145 |
|
146 |
|
147 |
#ifndef DEBUG |
148 |
if (strcmp((char *) cipher_tmp, test0[i]) != 0) { |
149 |
printf("failed compatibility\n"); |
150 |
x = 1; |
151 |
mcrypt_generic_end(td); |
152 |
mcrypt_generic_end(td2); |
153 |
free(name); |
154 |
free(keyword); |
155 |
free(ciphertext); |
156 |
free(plaintext); |
157 |
free(password); |
158 |
continue; |
159 |
} |
160 |
#endif |
161 |
|
162 |
#ifdef DEBUG |
163 |
printf("\n"); |
164 |
#endif |
165 |
|
166 |
#ifndef DEBUG |
167 |
mdecrypt_generic(td2, ciphertext, blocksize); |
168 |
if (memcmp(ciphertext, plaintext, blocksize) |
169 |
!= 0) { |
170 |
printf("failed internally\n"); |
171 |
x = 1; |
172 |
mcrypt_generic_end(td); |
173 |
mcrypt_generic_end(td2); |
174 |
free(name); |
175 |
free(keyword); |
176 |
free(ciphertext); |
177 |
free(plaintext); |
178 |
free(password); |
179 |
continue; |
180 |
} |
181 |
|
182 |
printf("ok\n"); |
183 |
#endif |
184 |
|
185 |
mcrypt_generic_end(td); |
186 |
mcrypt_generic_end(td2); |
187 |
free(name); |
188 |
free(keyword); |
189 |
free(ciphertext); |
190 |
free(plaintext); |
191 |
free(password); |
192 |
} |
193 |
|
194 |
return x; |
195 |
|
196 |
} |