summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/evp/evptest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/evp/evptest.c')
-rw-r--r--src/regress/lib/libcrypto/evp/evptest.c436
1 files changed, 0 insertions, 436 deletions
diff --git a/src/regress/lib/libcrypto/evp/evptest.c b/src/regress/lib/libcrypto/evp/evptest.c
deleted file mode 100644
index 1aa11d7b1e..0000000000
--- a/src/regress/lib/libcrypto/evp/evptest.c
+++ /dev/null
@@ -1,436 +0,0 @@
1/* $OpenBSD: evptest.c,v 1.7 2018/07/17 17:06:49 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
62static void
63hexdump(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
76static int
77convert(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
98static char *
99sstrsep(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
127static unsigned char *
128ustrsep(char **p, const char *sep)
129{
130 return (unsigned char *)sstrsep(p, sep);
131}
132
133static int
134test1_exit(int ec)
135{
136 exit(ec);
137 return(0); /* To keep some compilers quiet */
138}
139
140static void
141test1(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 int outl, outl2;
148
149 printf("Testing cipher %s%s\n", EVP_CIPHER_name(c),
150 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
151 hexdump(stdout, "Key",key,kn);
152 if (in)
153 hexdump(stdout, "IV",iv,in);
154 hexdump(stdout, "Plaintext",plaintext,pn);
155 hexdump(stdout, "Ciphertext",ciphertext,cn);
156
157 if (kn != c->key_len) {
158 fprintf(stderr, "Key length doesn't match, got %d expected %lu\n",kn,
159 (unsigned long)c->key_len);
160 test1_exit(5);
161 }
162 EVP_CIPHER_CTX_init(&ctx);
163 if (encdec != 0) {
164 if (!EVP_EncryptInit_ex(&ctx, c,NULL, key, iv)) {
165 fprintf(stderr, "EncryptInit failed\n");
166 ERR_print_errors_fp(stderr);
167 test1_exit(10);
168 }
169 EVP_CIPHER_CTX_set_padding(&ctx, 0);
170
171 if (!EVP_EncryptUpdate(&ctx, out, &outl, plaintext, pn)) {
172 fprintf(stderr, "Encrypt failed\n");
173 ERR_print_errors_fp(stderr);
174 test1_exit(6);
175 }
176 if (!EVP_EncryptFinal_ex(&ctx, out + outl, &outl2)) {
177 fprintf(stderr, "EncryptFinal failed\n");
178 ERR_print_errors_fp(stderr);
179 test1_exit(7);
180 }
181
182 if (outl + outl2 != cn) {
183 fprintf(stderr, "Ciphertext length mismatch got %d expected %d\n",
184 outl + outl2, cn);
185 test1_exit(8);
186 }
187
188 if (memcmp(out, ciphertext, cn)) {
189 fprintf(stderr, "Ciphertext mismatch\n");
190 hexdump(stderr, "Got",out,cn);
191 hexdump(stderr, "Expected",ciphertext,cn);
192 test1_exit(9);
193 }
194 }
195
196 if (encdec <= 0) {
197 if (!EVP_DecryptInit_ex(&ctx, c,NULL, key, iv)) {
198 fprintf(stderr, "DecryptInit failed\n");
199 ERR_print_errors_fp(stderr);
200 test1_exit(11);
201 }
202 EVP_CIPHER_CTX_set_padding(&ctx, 0);
203
204 if (!EVP_DecryptUpdate(&ctx, out, &outl, ciphertext, cn)) {
205 fprintf(stderr, "Decrypt failed\n");
206 ERR_print_errors_fp(stderr);
207 test1_exit(6);
208 }
209 if (!EVP_DecryptFinal_ex(&ctx, out + outl, &outl2)) {
210 fprintf(stderr, "DecryptFinal failed\n");
211 ERR_print_errors_fp(stderr);
212 test1_exit(7);
213 }
214
215 if (outl + outl2 != pn) {
216 fprintf(stderr, "Plaintext length mismatch got %d expected %d\n",
217 outl + outl2, pn);
218 test1_exit(8);
219 }
220
221 if (memcmp(out, plaintext, pn)) {
222 fprintf(stderr, "Plaintext mismatch\n");
223 hexdump(stderr, "Got",out,pn);
224 hexdump(stderr, "Expected",plaintext,pn);
225 test1_exit(9);
226 }
227 }
228
229 EVP_CIPHER_CTX_cleanup(&ctx);
230
231 printf("\n");
232}
233
234static int
235test_cipher(const char *cipher, const unsigned char *key, int kn,
236 const unsigned char *iv, int in, const unsigned char *plaintext, int pn,
237 const unsigned char *ciphertext, int cn, int encdec)
238{
239 const EVP_CIPHER *c;
240
241 c = EVP_get_cipherbyname(cipher);
242 if (!c)
243 return 0;
244
245 test1(c, key, kn, iv, in, plaintext, pn, ciphertext, cn, encdec);
246
247 return 1;
248}
249
250static int
251test_digest(const char *digest, const unsigned char *plaintext, int pn,
252 const unsigned char *ciphertext, unsigned int cn)
253{
254 const EVP_MD *d;
255 EVP_MD_CTX ctx;
256 unsigned char md[EVP_MAX_MD_SIZE];
257 unsigned int mdn;
258
259 d = EVP_get_digestbyname(digest);
260 if (!d)
261 return 0;
262
263 printf("Testing digest %s\n",EVP_MD_name(d));
264 hexdump(stdout, "Plaintext",plaintext,pn);
265 hexdump(stdout, "Digest",ciphertext,cn);
266
267 EVP_MD_CTX_init(&ctx);
268 if (!EVP_DigestInit_ex(&ctx, d, NULL)) {
269 fprintf(stderr, "DigestInit failed\n");
270 ERR_print_errors_fp(stderr);
271 exit(100);
272 }
273 if (!EVP_DigestUpdate(&ctx, plaintext, pn)) {
274 fprintf(stderr, "DigestUpdate failed\n");
275 ERR_print_errors_fp(stderr);
276 exit(101);
277 }
278 if (!EVP_DigestFinal_ex(&ctx, md, &mdn)) {
279 fprintf(stderr, "DigestFinal failed\n");
280 ERR_print_errors_fp(stderr);
281 exit(101);
282 }
283 EVP_MD_CTX_cleanup(&ctx);
284
285 if (mdn != cn) {
286 fprintf(stderr, "Digest length mismatch, got %d expected %d\n",mdn,cn);
287 exit(102);
288 }
289
290 if (memcmp(md, ciphertext, cn)) {
291 fprintf(stderr, "Digest mismatch\n");
292 hexdump(stderr, "Got",md,cn);
293 hexdump(stderr, "Expected",ciphertext,cn);
294 exit(103);
295 }
296
297 printf("\n");
298
299 EVP_MD_CTX_cleanup(&ctx);
300
301 return 1;
302}
303
304int
305main(int argc, char **argv)
306{
307 const char *szTestFile;
308 FILE *f;
309
310 if (argc != 2) {
311 fprintf(stderr, "%s <test file>\n",argv[0]);
312 exit(1);
313 }
314
315 szTestFile = argv[1];
316
317 f=fopen(szTestFile, "r");
318 if (!f) {
319 perror(szTestFile);
320 exit(2);
321 }
322
323 /* Load up the software EVP_CIPHER and EVP_MD definitions */
324 OpenSSL_add_all_ciphers();
325 OpenSSL_add_all_digests();
326#ifndef OPENSSL_NO_ENGINE
327 /* Load all compiled-in ENGINEs */
328 ENGINE_load_builtin_engines();
329#endif
330#if 0
331 OPENSSL_config();
332#endif
333#ifndef OPENSSL_NO_ENGINE
334 /* Register all available ENGINE implementations of ciphers and digests.
335 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
336 ENGINE_register_all_ciphers();
337 ENGINE_register_all_digests();
338 /* If we add command-line options, this statement should be switchable.
339 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
340 * they weren't already initialised. */
341 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
342#endif
343
344 for (;;) {
345 char line[4096];
346 char *p;
347 char *cipher;
348 unsigned char *iv, *key, *plaintext, *ciphertext;
349 int encdec;
350 int kn, in, pn, cn;
351
352 if (!fgets((char *)line, sizeof line, f))
353 break;
354 if (line[0] == '#' || line[0] == '\n')
355 continue;
356 p = line;
357 cipher=sstrsep(&p, ":");
358 key=ustrsep(&p, ":");
359 iv=ustrsep(&p, ":");
360 plaintext=ustrsep(&p, ":");
361 ciphertext=ustrsep(&p, ":");
362 if (p[-1] == '\n') {
363 p[-1] = '\0';
364 encdec = -1;
365 } else {
366 encdec = atoi(sstrsep(&p, "\n"));
367 }
368
369
370 kn = convert(key);
371 in = convert(iv);
372 pn = convert(plaintext);
373 cn = convert(ciphertext);
374
375 if (!test_cipher(cipher, key, kn, iv, in, plaintext, pn, ciphertext, cn, encdec) &&
376 !test_digest(cipher, plaintext, pn, ciphertext, cn)) {
377#ifdef OPENSSL_NO_AES
378 if (strstr(cipher, "AES") == cipher) {
379 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
380 continue;
381 }
382#endif
383#ifdef OPENSSL_NO_DES
384 if (strstr(cipher, "DES") == cipher) {
385 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
386 continue;
387 }
388#endif
389#ifdef OPENSSL_NO_RC4
390 if (strstr(cipher, "RC4") == cipher) {
391 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
392 continue;
393 }
394#endif
395#ifdef OPENSSL_NO_CAMELLIA
396 if (strstr(cipher, "CAMELLIA") == cipher) {
397 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
398 continue;
399 }
400#endif
401#ifdef OPENSSL_NO_SEED
402 if (strstr(cipher, "SEED") == cipher) {
403 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
404 continue;
405 }
406#endif
407#ifdef OPENSSL_NO_CHACHA
408 if (strstr(cipher, "ChaCha") == cipher) {
409 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
410 continue;
411 }
412#endif
413#ifdef OPENSSL_NO_GOST
414 if (strstr(cipher, "md_gost") == cipher ||
415 strstr(cipher, "streebog") == cipher) {
416 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
417 continue;
418 }
419#endif
420 fprintf(stderr, "Can't find %s\n",cipher);
421 exit(3);
422 }
423 }
424 fclose(f);
425
426#ifndef OPENSSL_NO_ENGINE
427 ENGINE_cleanup();
428#endif
429 EVP_cleanup();
430 CRYPTO_cleanup_all_ex_data();
431 ERR_remove_thread_state(NULL);
432 ERR_free_strings();
433 CRYPTO_mem_leaks_fp(stderr);
434
435 return 0;
436}