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)
80 for (d = s; *s; s += 2, ++d) {
84 fprintf(stderr, "Odd number of hex digits!");
87 sscanf((char *)s, "%2x", &n);
88 *d = (unsigned char)n;
93 static char *sstrsep(char **string, const char *delim)
96 char *token = *string;
101 memset(isdelim, 0, 256);
105 isdelim[(unsigned char)(*delim)] = 1;
109 while (!isdelim[(unsigned char)(**string)]) {
121 static unsigned char *ustrsep(char **p, const char *sep)
123 return (unsigned char *)sstrsep(p, sep);
126 static int test1_exit(int ec)
129 return (0); /* To keep some compilers quiet */
132 static void test1(const EVP_CIPHER *c, const unsigned char *key, int kn,
133 const unsigned char *iv, int in,
134 const unsigned char *plaintext, int pn,
135 const unsigned char *ciphertext, int cn,
136 const unsigned char *aad, int an,
137 const unsigned char *tag, int tn, int encdec)
140 unsigned char out[4096];
141 int outl, outl2, mode;
143 printf("Testing cipher %s%s\n", EVP_CIPHER_name(c),
145 1 ? "(encrypt)" : (encdec ==
146 0 ? "(decrypt)" : "(encrypt/decrypt)")));
147 hexdump(stdout, "Key", key, kn);
149 hexdump(stdout, "IV", iv, in);
150 hexdump(stdout, "Plaintext", plaintext, pn);
151 hexdump(stdout, "Ciphertext", ciphertext, cn);
153 hexdump(stdout, "AAD", aad, an);
155 hexdump(stdout, "Tag", tag, tn);
156 mode = EVP_CIPHER_mode(c);
157 if (kn != EVP_CIPHER_key_length(c)) {
158 fprintf(stderr, "Key length doesn't match, got %d expected %lu\n", kn,
159 (unsigned long)EVP_CIPHER_key_length(c));
162 EVP_CIPHER_CTX_init(&ctx);
163 EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
165 if (mode == EVP_CIPH_GCM_MODE) {
166 if (!EVP_EncryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
167 fprintf(stderr, "EncryptInit failed\n");
168 ERR_print_errors_fp(stderr);
171 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
172 fprintf(stderr, "IV length set failed\n");
173 ERR_print_errors_fp(stderr);
176 if (!EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv)) {
177 fprintf(stderr, "Key/IV set failed\n");
178 ERR_print_errors_fp(stderr);
181 if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
182 fprintf(stderr, "AAD set failed\n");
183 ERR_print_errors_fp(stderr);
186 } else if (mode == EVP_CIPH_CCM_MODE) {
187 if (!EVP_EncryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
188 fprintf(stderr, "EncryptInit failed\n");
189 ERR_print_errors_fp(stderr);
192 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL)) {
193 fprintf(stderr, "IV length set failed\n");
194 ERR_print_errors_fp(stderr);
197 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, tn, NULL)) {
198 fprintf(stderr, "Tag length set failed\n");
199 ERR_print_errors_fp(stderr);
202 if (!EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv)) {
203 fprintf(stderr, "Key/IV set failed\n");
204 ERR_print_errors_fp(stderr);
207 if (!EVP_EncryptUpdate(&ctx, NULL, &outl, NULL, pn)) {
208 fprintf(stderr, "Plaintext length set failed\n");
209 ERR_print_errors_fp(stderr);
212 if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
213 fprintf(stderr, "AAD set failed\n");
214 ERR_print_errors_fp(stderr);
217 } else if (mode == EVP_CIPH_WRAP_MODE) {
218 if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, in ? iv : NULL)) {
219 fprintf(stderr, "EncryptInit failed\n");
220 ERR_print_errors_fp(stderr);
223 } else if (!EVP_EncryptInit_ex(&ctx, c, NULL, key, iv)) {
224 fprintf(stderr, "EncryptInit failed\n");
225 ERR_print_errors_fp(stderr);
228 EVP_CIPHER_CTX_set_padding(&ctx, 0);
230 if (!EVP_EncryptUpdate(&ctx, out, &outl, plaintext, pn)) {
231 fprintf(stderr, "Encrypt failed\n");
232 ERR_print_errors_fp(stderr);
235 if (!EVP_EncryptFinal_ex(&ctx, out + outl, &outl2)) {
236 fprintf(stderr, "EncryptFinal failed\n");
237 ERR_print_errors_fp(stderr);
241 if (outl + outl2 != cn) {
242 fprintf(stderr, "Ciphertext length mismatch got %d expected %d\n",
247 if (memcmp(out, ciphertext, cn)) {
248 fprintf(stderr, "Ciphertext mismatch\n");
249 hexdump(stderr, "Got", out, cn);
250 hexdump(stderr, "Expected", ciphertext, cn);
253 if (mode == EVP_CIPH_GCM_MODE || mode == EVP_CIPH_CCM_MODE) {
254 unsigned char rtag[16];
256 * Note: EVP_CTRL_CCM_GET_TAG has same value as
257 * EVP_CTRL_GCM_GET_TAG
259 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, tn, rtag)) {
260 fprintf(stderr, "Get tag failed\n");
261 ERR_print_errors_fp(stderr);
264 if (memcmp(rtag, tag, tn)) {
265 fprintf(stderr, "Tag mismatch\n");
266 hexdump(stderr, "Got", rtag, tn);
267 hexdump(stderr, "Expected", tag, tn);
274 if (mode == EVP_CIPH_GCM_MODE) {
275 if (!EVP_DecryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
276 fprintf(stderr, "EncryptInit failed\n");
277 ERR_print_errors_fp(stderr);
280 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, in, NULL)) {
281 fprintf(stderr, "IV length set failed\n");
282 ERR_print_errors_fp(stderr);
285 if (!EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv)) {
286 fprintf(stderr, "Key/IV set failed\n");
287 ERR_print_errors_fp(stderr);
290 if (!EVP_CIPHER_CTX_ctrl
291 (&ctx, EVP_CTRL_GCM_SET_TAG, tn, (void *)tag)) {
292 fprintf(stderr, "Set tag failed\n");
293 ERR_print_errors_fp(stderr);
296 if (an && !EVP_DecryptUpdate(&ctx, NULL, &outl, aad, an)) {
297 fprintf(stderr, "AAD set failed\n");
298 ERR_print_errors_fp(stderr);
301 } else if (mode == EVP_CIPH_CCM_MODE) {
302 if (!EVP_DecryptInit_ex(&ctx, c, NULL, NULL, NULL)) {
303 fprintf(stderr, "DecryptInit failed\n");
304 ERR_print_errors_fp(stderr);
307 if (!EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, in, NULL)) {
308 fprintf(stderr, "IV length set failed\n");
309 ERR_print_errors_fp(stderr);
312 if (!EVP_CIPHER_CTX_ctrl
313 (&ctx, EVP_CTRL_CCM_SET_TAG, tn, (void *)tag)) {
314 fprintf(stderr, "Tag length set failed\n");
315 ERR_print_errors_fp(stderr);
318 if (!EVP_DecryptInit_ex(&ctx, NULL, NULL, key, iv)) {
319 fprintf(stderr, "Key/Nonce set failed\n");
320 ERR_print_errors_fp(stderr);
323 if (!EVP_DecryptUpdate(&ctx, NULL, &outl, NULL, pn)) {
324 fprintf(stderr, "Plaintext length set failed\n");
325 ERR_print_errors_fp(stderr);
328 if (an && !EVP_EncryptUpdate(&ctx, NULL, &outl, aad, an)) {
329 fprintf(stderr, "AAD set failed\n");
330 ERR_print_errors_fp(stderr);
333 } else if (mode == EVP_CIPH_WRAP_MODE) {
334 if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, in ? iv : NULL)) {
335 fprintf(stderr, "EncryptInit failed\n");
336 ERR_print_errors_fp(stderr);
339 } else if (!EVP_DecryptInit_ex(&ctx, c, NULL, key, iv)) {
340 fprintf(stderr, "DecryptInit failed\n");
341 ERR_print_errors_fp(stderr);
344 EVP_CIPHER_CTX_set_padding(&ctx, 0);
346 if (!EVP_DecryptUpdate(&ctx, out, &outl, ciphertext, cn)) {
347 fprintf(stderr, "Decrypt failed\n");
348 ERR_print_errors_fp(stderr);
351 if (mode != EVP_CIPH_CCM_MODE
352 && !EVP_DecryptFinal_ex(&ctx, out + outl, &outl2)) {
353 fprintf(stderr, "DecryptFinal failed\n");
354 ERR_print_errors_fp(stderr);
358 if (outl + outl2 != pn) {
359 fprintf(stderr, "Plaintext length mismatch got %d expected %d\n",
364 if (memcmp(out, plaintext, pn)) {
365 fprintf(stderr, "Plaintext mismatch\n");
366 hexdump(stderr, "Got", out, pn);
367 hexdump(stderr, "Expected", plaintext, pn);
372 EVP_CIPHER_CTX_cleanup(&ctx);
377 static int test_cipher(const char *cipher, const unsigned char *key, int kn,
378 const unsigned char *iv, int in,
379 const unsigned char *plaintext, int pn,
380 const unsigned char *ciphertext, int cn,
381 const unsigned char *aad, int an,
382 const unsigned char *tag, int tn, int encdec)
386 c = EVP_get_cipherbyname(cipher);
390 test1(c, key, kn, iv, in, plaintext, pn, ciphertext, cn, aad, an, tag, tn,
396 static int test_digest(const char *digest,
397 const unsigned char *plaintext, int pn,
398 const unsigned char *ciphertext, unsigned int cn)
402 unsigned char md[EVP_MAX_MD_SIZE];
405 d = EVP_get_digestbyname(digest);
409 printf("Testing digest %s\n", EVP_MD_name(d));
410 hexdump(stdout, "Plaintext", plaintext, pn);
411 hexdump(stdout, "Digest", ciphertext, cn);
413 EVP_MD_CTX_init(&ctx);
414 if (!EVP_DigestInit_ex(&ctx, d, NULL)) {
415 fprintf(stderr, "DigestInit failed\n");
416 ERR_print_errors_fp(stderr);
419 if (!EVP_DigestUpdate(&ctx, plaintext, pn)) {
420 fprintf(stderr, "DigestUpdate failed\n");
421 ERR_print_errors_fp(stderr);
424 if (!EVP_DigestFinal_ex(&ctx, md, &mdn)) {
425 fprintf(stderr, "DigestFinal failed\n");
426 ERR_print_errors_fp(stderr);
429 EVP_MD_CTX_cleanup(&ctx);
432 fprintf(stderr, "Digest length mismatch, got %d expected %d\n", mdn,
437 if (memcmp(md, ciphertext, cn)) {
438 fprintf(stderr, "Digest mismatch\n");
439 hexdump(stderr, "Got", md, cn);
440 hexdump(stderr, "Expected", ciphertext, cn);
446 EVP_MD_CTX_cleanup(&ctx);
451 int main(int argc, char **argv)
453 const char *szTestFile;
457 fprintf(stderr, "%s <test file>\n", argv[0]);
460 CRYPTO_malloc_debug_init();
461 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
462 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
464 szTestFile = argv[1];
466 f = fopen(szTestFile, "r");
471 ERR_load_crypto_strings();
472 /* Load up the software EVP_CIPHER and EVP_MD definitions */
473 OpenSSL_add_all_ciphers();
474 OpenSSL_add_all_digests();
475 #ifndef OPENSSL_NO_ENGINE
476 /* Load all compiled-in ENGINEs */
477 ENGINE_load_builtin_engines();
482 #ifndef OPENSSL_NO_ENGINE
484 * Register all available ENGINE implementations of ciphers and digests.
485 * This could perhaps be changed to "ENGINE_register_all_complete()"?
487 ENGINE_register_all_ciphers();
488 ENGINE_register_all_digests();
490 * If we add command-line options, this statement should be switchable.
491 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use
492 * if they weren't already initialised.
494 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
501 unsigned char *iv, *key, *plaintext, *ciphertext, *aad, *tag;
507 if (!fgets((char *)line, sizeof line, f))
509 if (line[0] == '#' || line[0] == '\n')
512 cipher = sstrsep(&p, ":");
513 key = ustrsep(&p, ":");
514 iv = ustrsep(&p, ":");
515 plaintext = ustrsep(&p, ":");
516 ciphertext = ustrsep(&p, ":");
523 aad = ustrsep(&p, ":");
524 tag = ustrsep(&p, ":");
534 encdec = atoi(sstrsep(&p, "\n"));
539 pn = convert(plaintext);
540 cn = convert(ciphertext);
547 (cipher, key, kn, iv, in, plaintext, pn, ciphertext, cn, aad, an,
549 && !test_digest(cipher, plaintext, pn, ciphertext, cn)) {
550 #ifdef OPENSSL_NO_AES
551 if (strstr(cipher, "AES") == cipher) {
552 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
556 #ifdef OPENSSL_NO_DES
557 if (strstr(cipher, "DES") == cipher) {
558 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
562 #ifdef OPENSSL_NO_RC4
563 if (strstr(cipher, "RC4") == cipher) {
564 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
568 #ifdef OPENSSL_NO_CAMELLIA
569 if (strstr(cipher, "CAMELLIA") == cipher) {
570 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
574 #ifdef OPENSSL_NO_SEED
575 if (strstr(cipher, "SEED") == cipher) {
576 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
580 fprintf(stderr, "Can't find %s\n", cipher);
586 #ifndef OPENSSL_NO_ENGINE
590 CRYPTO_cleanup_all_ex_data();
591 ERR_remove_thread_state(NULL);
593 CRYPTO_mem_leaks_fp(stderr);