1 /*	$OpenBSD: evptest.c,v 1.9 2020/01/26 02:46:26 tb Exp $	*/
2 /* Written by Ben Laurie, 2001 */
3 /*
4  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. All advertising materials mentioning features or use of this
19  *    software must display the following acknowledgment:
20  *    "This product includes software developed by the OpenSSL Project
21  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22  *
23  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24  *    endorse or promote products derived from this software without
25  *    prior written permission. For written permission, please contact
26  *    openssl-core@openssl.org.
27  *
28  * 5. Products derived from this software may not be called "OpenSSL"
29  *    nor may "OpenSSL" appear in their names without prior written
30  *    permission of the OpenSSL Project.
31  *
32  * 6. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by the OpenSSL Project
35  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48  * OF THE POSSIBILITY OF SUCH DAMAGE.
49  */
50 
51 #include <stdio.h>
52 #include <string.h>
53 
54 #include <openssl/opensslconf.h>
55 #include <openssl/evp.h>
56 #ifndef OPENSSL_NO_ENGINE
57 #include <openssl/engine.h>
58 #endif
59 #include <openssl/err.h>
60 #include <openssl/conf.h>
61 
62 static void
hexdump(FILE * f,const char * title,const unsigned char * s,int l)63 hexdump(FILE *f, const char *title, const unsigned char *s, int l)
64 {
65 	int n = 0;
66 
67 	fprintf(f, "%s",title);
68 	for (; n < l; ++n) {
69 		if ((n % 16) == 0)
70 			fprintf(f, "\n%04x",n);
71 		fprintf(f, " %02x",s[n]);
72 	}
73 	fprintf(f, "\n");
74 }
75 
76 static int
convert(unsigned char * s)77 convert(unsigned char *s)
78 {
79 	unsigned char *d;
80 
81 	for (d = s; *s; s += 2,++d) {
82 		unsigned int n;
83 
84 		if (!s[1]) {
85 			fprintf(stderr, "Odd number of hex digits!\n");
86 			exit(4);
87 		}
88 		if (sscanf((char *)s, "%2x", &n) != 1) {
89 			fprintf(stderr, "Invalid hex value at %s\n", s);
90 			exit(4);
91 		}
92 
93 		*d = (unsigned char)n;
94 	}
95 	return s - d;
96 }
97 
98 static char *
sstrsep(char ** string,const char * delim)99 sstrsep(char **string, const char *delim)
100 {
101 	char isdelim[256];
102 	char *token = *string;
103 
104 	if (**string == 0)
105 		return NULL;
106 
107 	memset(isdelim, 0, 256);
108 	isdelim[0] = 1;
109 
110 	while (*delim) {
111 		isdelim[(unsigned char)(*delim)] = 1;
112 		delim++;
113 	}
114 
115 	while (!isdelim[(unsigned char)(**string)]) {
116 		(*string)++;
117 	}
118 
119 	if (**string) {
120 		**string = 0;
121 		(*string)++;
122 	}
123 
124 	return token;
125 }
126 
127 static unsigned char *
ustrsep(char ** p,const char * sep)128 ustrsep(char **p, const char *sep)
129 {
130 	return (unsigned char *)sstrsep(p, sep);
131 }
132 
133 static int
test1_exit(int ec)134 test1_exit(int ec)
135 {
136 	exit(ec);
137 	return(0);		/* To keep some compilers quiet */
138 }
139 
140 static void
test1(const EVP_CIPHER * c,const unsigned char * key,int kn,const unsigned char * iv,int in,const unsigned char * plaintext,int pn,const unsigned char * ciphertext,int cn,int encdec)141 test1(const EVP_CIPHER *c, const unsigned char *key, int kn,
142     const unsigned char *iv, int in, const unsigned char *plaintext, int pn,
143     const unsigned char *ciphertext, int cn, int encdec)
144 {
145 	EVP_CIPHER_CTX ctx;
146 	unsigned char out[4096];
147 	const unsigned char *eiv;
148 	int outl, outl2;
149 
150 	printf("Testing cipher %s%s\n", EVP_CIPHER_name(c),
151 	    (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
152 	hexdump(stdout, "Key",key,kn);
153 	if (in)
154 		hexdump(stdout, "IV",iv,in);
155 	hexdump(stdout, "Plaintext",plaintext,pn);
156 	hexdump(stdout, "Ciphertext",ciphertext,cn);
157 
158 	if (kn != c->key_len) {
159 		fprintf(stderr, "Key length doesn't match, got %d expected %lu\n",kn,
160 		    (unsigned long)c->key_len);
161 		test1_exit(5);
162 	}
163 	EVP_CIPHER_CTX_init(&ctx);
164 	EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
165 	if (encdec != 0) {
166 		eiv = iv;
167 		if (EVP_CIPHER_mode(c) == EVP_CIPH_WRAP_MODE && in == 0)
168 			eiv = NULL;
169 		if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, eiv)) {
170 			fprintf(stderr, "EncryptInit failed\n");
171 			ERR_print_errors_fp(stderr);
172 			test1_exit(10);
173 		}
174 		EVP_CIPHER_CTX_set_padding(&ctx, 0);
175 
176 		if (!EVP_EncryptUpdate(&ctx, out, &outl, plaintext, pn)) {
177 			fprintf(stderr, "Encrypt failed\n");
178 			ERR_print_errors_fp(stderr);
179 			test1_exit(6);
180 		}
181 		if (!EVP_EncryptFinal_ex(&ctx, out + outl, &outl2)) {
182 			fprintf(stderr, "EncryptFinal failed\n");
183 			ERR_print_errors_fp(stderr);
184 			test1_exit(7);
185 		}
186 
187 		if (outl + outl2 != cn) {
188 			fprintf(stderr, "Ciphertext length mismatch got %d expected %d\n",
189 			    outl + outl2, cn);
190 			test1_exit(8);
191 		}
192 
193 		if (memcmp(out, ciphertext, cn)) {
194 			fprintf(stderr, "Ciphertext mismatch\n");
195 			hexdump(stderr, "Got",out,cn);
196 			hexdump(stderr, "Expected",ciphertext,cn);
197 			test1_exit(9);
198 		}
199 	}
200 
201 	if (encdec <= 0) {
202 		eiv = iv;
203 		if (EVP_CIPHER_mode(c) == EVP_CIPH_WRAP_MODE && in == 0)
204 			eiv = NULL;
205 		if (!EVP_DecryptInit_ex(&ctx, c,NULL, key, eiv)) {
206 			fprintf(stderr, "DecryptInit failed\n");
207 			ERR_print_errors_fp(stderr);
208 			test1_exit(11);
209 		}
210 		EVP_CIPHER_CTX_set_padding(&ctx, 0);
211 
212 		if (!EVP_DecryptUpdate(&ctx, out, &outl, ciphertext, cn)) {
213 			fprintf(stderr, "Decrypt failed\n");
214 			ERR_print_errors_fp(stderr);
215 			test1_exit(6);
216 		}
217 		if (!EVP_DecryptFinal_ex(&ctx, out + outl, &outl2)) {
218 			fprintf(stderr, "DecryptFinal failed\n");
219 			ERR_print_errors_fp(stderr);
220 			test1_exit(7);
221 		}
222 
223 		if (outl + outl2 != pn) {
224 			fprintf(stderr, "Plaintext length mismatch got %d expected %d\n",
225 			    outl + outl2, pn);
226 			test1_exit(8);
227 		}
228 
229 		if (memcmp(out, plaintext, pn)) {
230 			fprintf(stderr, "Plaintext mismatch\n");
231 			hexdump(stderr, "Got",out,pn);
232 			hexdump(stderr, "Expected",plaintext,pn);
233 			test1_exit(9);
234 		}
235 	}
236 
237 	EVP_CIPHER_CTX_cleanup(&ctx);
238 
239 	printf("\n");
240 }
241 
242 static int
test_cipher(const char * cipher,const unsigned char * key,int kn,const unsigned char * iv,int in,const unsigned char * plaintext,int pn,const unsigned char * ciphertext,int cn,int encdec)243 test_cipher(const char *cipher, const unsigned char *key, int kn,
244     const unsigned char *iv, int in, const unsigned char *plaintext, int pn,
245     const unsigned char *ciphertext, int cn, int encdec)
246 {
247 	const EVP_CIPHER *c;
248 
249 	c = EVP_get_cipherbyname(cipher);
250 	if (!c)
251 		return 0;
252 
253 	test1(c, key, kn, iv, in, plaintext, pn, ciphertext, cn, encdec);
254 
255 	return 1;
256 }
257 
258 static int
test_digest(const char * digest,const unsigned char * plaintext,int pn,const unsigned char * ciphertext,unsigned int cn)259 test_digest(const char *digest, const unsigned char *plaintext, int pn,
260     const unsigned char *ciphertext, unsigned int cn)
261 {
262 	const EVP_MD *d;
263 	EVP_MD_CTX ctx;
264 	unsigned char md[EVP_MAX_MD_SIZE];
265 	unsigned int mdn;
266 
267 	d = EVP_get_digestbyname(digest);
268 	if (!d)
269 		return 0;
270 
271 	printf("Testing digest %s\n",EVP_MD_name(d));
272 	hexdump(stdout, "Plaintext",plaintext,pn);
273 	hexdump(stdout, "Digest",ciphertext,cn);
274 
275 	EVP_MD_CTX_init(&ctx);
276 	if (!EVP_DigestInit_ex(&ctx, d, NULL)) {
277 		fprintf(stderr, "DigestInit failed\n");
278 		ERR_print_errors_fp(stderr);
279 		exit(100);
280 	}
281 	if (!EVP_DigestUpdate(&ctx, plaintext, pn)) {
282 		fprintf(stderr, "DigestUpdate failed\n");
283 		ERR_print_errors_fp(stderr);
284 		exit(101);
285 	}
286 	if (!EVP_DigestFinal_ex(&ctx, md, &mdn)) {
287 		fprintf(stderr, "DigestFinal failed\n");
288 		ERR_print_errors_fp(stderr);
289 		exit(101);
290 	}
291 	EVP_MD_CTX_cleanup(&ctx);
292 
293 	if (mdn != cn) {
294 		fprintf(stderr, "Digest length mismatch, got %d expected %d\n",mdn,cn);
295 		exit(102);
296 	}
297 
298 	if (memcmp(md, ciphertext, cn)) {
299 		fprintf(stderr, "Digest mismatch\n");
300 		hexdump(stderr, "Got",md,cn);
301 		hexdump(stderr, "Expected",ciphertext,cn);
302 		exit(103);
303 	}
304 
305 	printf("\n");
306 
307 	EVP_MD_CTX_cleanup(&ctx);
308 
309 	return 1;
310 }
311 
312 int
main(int argc,char ** argv)313 main(int argc, char **argv)
314 {
315 	const char *szTestFile;
316 	FILE *f;
317 
318 	if (argc != 2) {
319 		fprintf(stderr, "%s <test file>\n",argv[0]);
320 		exit(1);
321 	}
322 
323 	szTestFile = argv[1];
324 
325 	f=fopen(szTestFile, "r");
326 	if (!f) {
327 		perror(szTestFile);
328 		exit(2);
329 	}
330 
331 	/* Load up the software EVP_CIPHER and EVP_MD definitions */
332 	OpenSSL_add_all_ciphers();
333 	OpenSSL_add_all_digests();
334 #ifndef OPENSSL_NO_ENGINE
335 	/* Load all compiled-in ENGINEs */
336 	ENGINE_load_builtin_engines();
337 #endif
338 #if 0
339 	OPENSSL_config();
340 #endif
341 #ifndef OPENSSL_NO_ENGINE
342     /* Register all available ENGINE implementations of ciphers and digests.
343      * This could perhaps be changed to "ENGINE_register_all_complete()"? */
344 	ENGINE_register_all_ciphers();
345 	ENGINE_register_all_digests();
346     /* If we add command-line options, this statement should be switchable.
347      * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
348      * they weren't already initialised. */
349 	/* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
350 #endif
351 
352 	for (;;) {
353 		char line[8 * 1024];
354 		char *p;
355 		char *cipher;
356 		unsigned char *iv, *key, *plaintext, *ciphertext;
357 		int encdec;
358 		int kn, in, pn, cn;
359 
360 		if (!fgets((char *)line, sizeof line, f))
361 			break;
362 		if (line[0] == '#' || line[0] == '\n')
363 			continue;
364 		p = line;
365 		cipher=sstrsep(&p, ":");
366 		key=ustrsep(&p, ":");
367 		iv=ustrsep(&p, ":");
368 		plaintext=ustrsep(&p, ":");
369 		ciphertext=ustrsep(&p, ":");
370 		if (p[-1] == '\n') {
371 			p[-1] = '\0';
372 			encdec = -1;
373 		} else {
374 			encdec = atoi(sstrsep(&p, "\n"));
375 		}
376 
377 
378 		kn = convert(key);
379 		in = convert(iv);
380 		pn = convert(plaintext);
381 		cn = convert(ciphertext);
382 
383 		if (!test_cipher(cipher, key, kn, iv, in, plaintext, pn, ciphertext, cn, encdec) &&
384 		    !test_digest(cipher, plaintext, pn, ciphertext, cn)) {
385 #ifdef OPENSSL_NO_AES
386 			if (strstr(cipher, "AES") == cipher) {
387 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
388 				continue;
389 			}
390 #endif
391 #ifdef OPENSSL_NO_DES
392 			if (strstr(cipher, "DES") == cipher) {
393 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
394 				continue;
395 			}
396 #endif
397 #ifdef OPENSSL_NO_RC4
398 			if (strstr(cipher, "RC4") == cipher) {
399 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
400 				continue;
401 			}
402 #endif
403 #ifdef OPENSSL_NO_CAMELLIA
404 			if (strstr(cipher, "CAMELLIA") == cipher) {
405 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
406 				continue;
407 			}
408 #endif
409 #ifdef OPENSSL_NO_SEED
410 			if (strstr(cipher, "SEED") == cipher) {
411 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
412 				continue;
413 			}
414 #endif
415 #ifdef OPENSSL_NO_CHACHA
416 			if (strstr(cipher, "ChaCha") == cipher) {
417 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
418 				continue;
419 			}
420 #endif
421 #ifdef OPENSSL_NO_GOST
422 			if (strstr(cipher, "md_gost") == cipher ||
423 			    strstr(cipher, "streebog") == cipher) {
424 				fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
425 				continue;
426 			}
427 #endif
428 			fprintf(stderr, "Can't find %s\n",cipher);
429 			exit(3);
430 		}
431 	}
432 	fclose(f);
433 
434 #ifndef OPENSSL_NO_ENGINE
435 	ENGINE_cleanup();
436 #endif
437 	EVP_cleanup();
438 	CRYPTO_cleanup_all_ex_data();
439 	ERR_remove_thread_state(NULL);
440 	ERR_free_strings();
441 	CRYPTO_mem_leaks_fp(stderr);
442 
443 	return 0;
444 }
445