1 /* Written by Ben Laurie, 2001 */
3 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 #include <openssl/opensslconf.h>
56 #include <openssl/evp.h>
57 #ifndef OPENSSL_NO_ENGINE
58 # include <openssl/engine.h>
60 #include <openssl/err.h>
61 #include <openssl/conf.h>
63 static void hexdump(FILE *f, const char *title, const unsigned char *s, int l)
67 fprintf(f, "%s", title);
70 fprintf(f, "\n%04x", n);
71 fprintf(f, " %02x", s[n]);
76 static int convert(unsigned char *s)
81 for (d = s; *s; s += 2, ++d) {
85 fprintf(stderr, "Odd number of hex digits!");
88 sscanf((char *)s, "%2x", &n);
89 *d = (unsigned char)n;
95 static char *sstrsep(char **string, const char *delim)
98 char *token = *string;
103 memset(isdelim, 0, 256);
107 isdelim[(unsigned char)(*delim)] = 1;
111 while (!isdelim[(unsigned char)(**string)]) {
123 static unsigned char *ustrsep(char **p, const char *sep)
125 return (unsigned char *)sstrsep(p, sep);
128 static int test1_exit(int ec)
131 return (0); /* To keep some compilers quiet */
134 static void test1(const EVP_CIPHER *c, const unsigned char *key, int kn,
135 const unsigned char *iv, int in,
136 const unsigned char *plaintext, int pn,
137 const unsigned char *ciphertext, int cn,
138 const unsigned char *aad, int an,
139 const unsigned char *tag, int tn, int encdec)
142 unsigned char out[4096];
143 int outl, outl2, mode;
145 printf("Testing cipher %s%s\n", EVP_CIPHER_name(c),
147 1 ? "(encrypt)" : (encdec ==
148 0 ? "(decrypt)" : "(encrypt/decrypt)")));
149 hexdump(stdout, "Key", key, kn);
151 hexdump(stdout, "IV", iv, in);
152 hexdump(stdout, "Plaintext", plaintext, pn);
153 hexdump(stdout, "Ciphertext", ciphertext, cn);
155 hexdump(stdout, "AAD", aad, an);
157 hexdump(stdout, "Tag", tag, tn);
158 mode = EVP_CIPHER_mode(c);
159 if (kn != EVP_CIPHER_key_length(c)) {
160 fprintf(stderr, "Key length doesn't match, got %d expected %lu\n", kn,
161 (unsigned long)EVP_CIPHER_key_length(c));
164 EVP_CIPHER_CTX_init(&ctx);
165 EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
167 if (mode == EVP_CIPH_GCM_MODE) {
168 if (!EVP_EncryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
169 fprintf(stderr, "EncryptInit failed\n");
170 ERR_print_errors_fp(stderr);
173 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
174 fprintf(stderr, "IV length set failed\n");
175 ERR_print_errors_fp(stderr);
178 if (!EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv)) {
179 fprintf(stderr, "Key/IV set failed\n");
180 ERR_print_errors_fp(stderr);
183 if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
184 fprintf(stderr, "AAD set failed\n");
185 ERR_print_errors_fp(stderr);
188 } else if (mode == EVP_CIPH_CCM_MODE) {
189 if (!EVP_EncryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
190 fprintf(stderr, "EncryptInit failed\n");
191 ERR_print_errors_fp(stderr);
194 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL)) {
195 fprintf(stderr, "IV length set failed\n");
196 ERR_print_errors_fp(stderr);
199 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, tn, NULL)) {
200 fprintf(stderr, "Tag length set failed\n");
201 ERR_print_errors_fp(stderr);
204 if (!EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv)) {
205 fprintf(stderr, "Key/IV set failed\n");
206 ERR_print_errors_fp(stderr);
209 if (!EVP_EncryptUpdate(&ctx, NULL, &outl, NULL, pn)) {
210 fprintf(stderr, "Plaintext length set failed\n");
211 ERR_print_errors_fp(stderr);
214 if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
215 fprintf(stderr, "AAD set failed\n");
216 ERR_print_errors_fp(stderr);
219 } else if (mode == EVP_CIPH_WRAP_MODE) {
220 if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, in ? iv : NULL)) {
221 fprintf(stderr, "EncryptInit failed\n");
222 ERR_print_errors_fp(stderr);
225 } else if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, iv)) {
226 fprintf(stderr, "EncryptInit failed\n");
227 ERR_print_errors_fp(stderr);
230 EVP_CIPHER_CTX_set_padding(&ctx, 0);
232 if (!EVP_EncryptUpdate(&ctx, out, &outl, plaintext, pn)) {
233 fprintf(stderr, "Encrypt failed\n");
234 ERR_print_errors_fp(stderr);
237 if (!EVP_EncryptFinal_ex(&ctx, out + outl, &outl2)) {
238 fprintf(stderr, "EncryptFinal failed\n");
239 ERR_print_errors_fp(stderr);
243 if (outl + outl2 != cn) {
244 fprintf(stderr, "Ciphertext length mismatch got %d expected %d\n",
249 if (memcmp(out, ciphertext, cn)) {
250 fprintf(stderr, "Ciphertext mismatch\n");
251 hexdump(stderr, "Got", out, cn);
252 hexdump(stderr, "Expected", ciphertext, cn);
255 if (mode == EVP_CIPH_GCM_MODE || mode == EVP_CIPH_CCM_MODE) {
256 unsigned char rtag[16];
258 * Note: EVP_CTRL_CCM_GET_TAG has same value as
259 * EVP_CTRL_GCM_GET_TAG
261 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, tn, rtag)) {
262 fprintf(stderr, "Get tag failed\n");
263 ERR_print_errors_fp(stderr);
266 if (memcmp(rtag, tag, tn)) {
267 fprintf(stderr, "Tag mismatch\n");
268 hexdump(stderr, "Got", rtag, tn);
269 hexdump(stderr, "Expected", tag, tn);
276 if (mode == EVP_CIPH_GCM_MODE) {
277 if (!EVP_DecryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
278 fprintf(stderr, "EncryptInit failed\n");
279 ERR_print_errors_fp(stderr);
282 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
283 fprintf(stderr, "IV length set failed\n");
284 ERR_print_errors_fp(stderr);
287 if (!EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv)) {
288 fprintf(stderr, "Key/IV set failed\n");
289 ERR_print_errors_fp(stderr);
292 if (!EVP_CIPHER_CTX_ctrl
293 (&ctx, EVP_CTRL_GCM_SET_TAG, tn, (void *)tag)) {
294 fprintf(stderr, "Set tag failed\n");
295 ERR_print_errors_fp(stderr);
298 if (an && !EVP_DecryptUpdate(&ctx, NULL, &outl, aad, an)) {
299 fprintf(stderr, "AAD set failed\n");
300 ERR_print_errors_fp(stderr);
303 } else if (mode == EVP_CIPH_CCM_MODE) {
304 if (!EVP_DecryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
305 fprintf(stderr, "DecryptInit failed\n");
306 ERR_print_errors_fp(stderr);
309 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL)) {
310 fprintf(stderr, "IV length set failed\n");
311 ERR_print_errors_fp(stderr);
314 if (!EVP_CIPHER_CTX_ctrl
315 (&ctx, EVP_CTRL_CCM_SET_TAG, tn, (void *)tag)) {
316 fprintf(stderr, "Tag length set failed\n");
317 ERR_print_errors_fp(stderr);
320 if (!EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv)) {
321 fprintf(stderr, "Key/Nonce set failed\n");
322 ERR_print_errors_fp(stderr);
325 if (!EVP_DecryptUpdate(&ctx, NULL, &outl, NULL, pn)) {
326 fprintf(stderr, "Plaintext length set failed\n");
327 ERR_print_errors_fp(stderr);
330 if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
331 fprintf(stderr, "AAD set failed\n");
332 ERR_print_errors_fp(stderr);
335 } else if (mode == EVP_CIPH_WRAP_MODE) {
336 if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, in ? iv : NULL)) {
337 fprintf(stderr, "EncryptInit failed\n");
338 ERR_print_errors_fp(stderr);
341 } else if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, iv)) {
342 fprintf(stderr, "DecryptInit failed\n");
343 ERR_print_errors_fp(stderr);
346 EVP_CIPHER_CTX_set_padding(&ctx, 0);
348 if (!EVP_DecryptUpdate(&ctx, out, &outl, ciphertext, cn)) {
349 fprintf(stderr, "Decrypt failed\n");
350 ERR_print_errors_fp(stderr);
353 if (mode != EVP_CIPH_CCM_MODE
354 && !EVP_DecryptFinal_ex(&ctx, out + outl, &outl2)) {
355 fprintf(stderr, "DecryptFinal failed\n");
356 ERR_print_errors_fp(stderr);
360 if (outl + outl2 != pn) {
361 fprintf(stderr, "Plaintext length mismatch got %d expected %d\n",
366 if (memcmp(out, plaintext, pn)) {
367 fprintf(stderr, "Plaintext mismatch\n");
368 hexdump(stderr, "Got", out, pn);
369 hexdump(stderr, "Expected", plaintext, pn);
374 EVP_CIPHER_CTX_cleanup(&ctx);
379 static int test_cipher(const char *cipher, const unsigned char *key, int kn,
380 const unsigned char *iv, int in,
381 const unsigned char *plaintext, int pn,
382 const unsigned char *ciphertext, int cn,
383 const unsigned char *aad, int an,
384 const unsigned char *tag, int tn, int encdec)
388 c = EVP_get_cipherbyname(cipher);
392 test1(c, key, kn, iv, in, plaintext, pn, ciphertext, cn, aad, an, tag, tn,
398 static int test_digest(const char *digest,
399 const unsigned char *plaintext, int pn,
400 const unsigned char *ciphertext, unsigned int cn)
404 unsigned char md[EVP_MAX_MD_SIZE];
407 d = EVP_get_digestbyname(digest);
411 printf("Testing digest %s\n", EVP_MD_name(d));
412 hexdump(stdout, "Plaintext", plaintext, pn);
413 hexdump(stdout, "Digest", ciphertext, cn);
415 EVP_MD_CTX_init(&ctx);
416 if (!EVP_DigestInit_ex(&ctx, d, NULL)) {
417 fprintf(stderr, "DigestInit failed\n");
418 ERR_print_errors_fp(stderr);
421 if (!EVP_DigestUpdate(&ctx, plaintext, pn)) {
422 fprintf(stderr, "DigestUpdate failed\n");
423 ERR_print_errors_fp(stderr);
426 if (!EVP_DigestFinal_ex(&ctx, md, &mdn)) {
427 fprintf(stderr, "DigestFinal failed\n");
428 ERR_print_errors_fp(stderr);
431 EVP_MD_CTX_cleanup(&ctx);
434 fprintf(stderr, "Digest length mismatch, got %d expected %d\n", mdn,
439 if (memcmp(md, ciphertext, cn)) {
440 fprintf(stderr, "Digest mismatch\n");
441 hexdump(stderr, "Got", md, cn);
442 hexdump(stderr, "Expected", ciphertext, cn);
448 EVP_MD_CTX_cleanup(&ctx);
453 int main(int argc, char **argv)
455 const char *szTestFile;
459 fprintf(stderr, "%s <test file>\n", argv[0]);
462 CRYPTO_malloc_debug_init();
463 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
464 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
466 szTestFile = argv[1];
468 f = fopen(szTestFile, "r");
473 ERR_load_crypto_strings();
474 /* Load up the software EVP_CIPHER and EVP_MD definitions */
475 OpenSSL_add_all_ciphers();
476 OpenSSL_add_all_digests();
477 #ifndef OPENSSL_NO_ENGINE
478 /* Load all compiled-in ENGINEs */
479 ENGINE_load_builtin_engines();
484 #ifndef OPENSSL_NO_ENGINE
486 * Register all available ENGINE implementations of ciphers and digests.
487 * This could perhaps be changed to "ENGINE_register_all_complete()"?
489 ENGINE_register_all_ciphers();
490 ENGINE_register_all_digests();
492 * If we add command-line options, this statement should be switchable.
493 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use
494 * if they weren't already initialised.
496 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
503 unsigned char *iv, *key, *plaintext, *ciphertext, *aad, *tag;
509 if (!fgets((char *)line, sizeof(line), f))
511 if (line[0] == '#' || line[0] == '\n')
514 cipher = sstrsep(&p, ":");
515 key = ustrsep(&p, ":");
516 iv = ustrsep(&p, ":");
517 plaintext = ustrsep(&p, ":");
518 ciphertext = ustrsep(&p, ":");
525 aad = ustrsep(&p, ":");
526 tag = ustrsep(&p, ":");
536 encdec = atoi(sstrsep(&p, "\n"));
541 pn = convert(plaintext);
542 cn = convert(ciphertext);
549 (cipher, key, kn, iv, in, plaintext, pn, ciphertext, cn, aad, an,
551 && !test_digest(cipher, plaintext, pn, ciphertext, cn)) {
552 #ifdef OPENSSL_NO_AES
553 if (strstr(cipher, "AES") == cipher) {
554 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
558 #ifdef OPENSSL_NO_DES
559 if (strstr(cipher, "DES") == cipher) {
560 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
564 #ifdef OPENSSL_NO_RC4
565 if (strstr(cipher, "RC4") == cipher) {
566 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
570 #ifdef OPENSSL_NO_CAMELLIA
571 if (strstr(cipher, "CAMELLIA") == cipher) {
572 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
576 #ifdef OPENSSL_NO_SEED
577 if (strstr(cipher, "SEED") == cipher) {
578 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
582 fprintf(stderr, "Can't find %s\n", cipher);
588 #ifndef OPENSSL_NO_ENGINE
592 CRYPTO_cleanup_all_ex_data();
593 ERR_remove_thread_state(NULL);
595 CRYPTO_mem_leaks_fp(stderr);