drop CHECK_SYMBOLS
[heimdal.git] / lib / hcrypto / test_cipher.c
blobebf81453aaa906f2f3b9d9516267a9411f068c72
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #ifdef RCSID
39 RCSID("$Id$");
40 #endif
42 #include <sys/types.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <getarg.h>
48 #include <roken.h>
50 #include <evp.h>
51 #include <hex.h>
52 #include <err.h>
54 struct tests {
55 const char *name;
56 void *key;
57 size_t keysize;
58 void *iv;
59 size_t datasize;
60 void *indata;
61 void *outdata;
62 size_t outdatasize;
65 struct tests aes_tests[] = {
66 { "aes-256",
67 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
68 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
69 32,
70 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
71 16,
72 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
73 "\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87"
77 struct tests rc2_40_tests[] = {
78 { "rc2-40",
79 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
80 16,
81 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
82 16,
83 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
84 "\xc0\xb8\xff\xa5\xd6\xeb\xc9\x62\xcc\x52\x5f\xfe\x9a\x3c\x97\xe6"
88 struct tests des_ede3_tests[] = {
89 { "des-ede3",
90 "1917ffe6bb772efc297643bc63567e9a002e4d431d5ffd58",
91 24,
92 "\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
93 16,
94 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
95 "\x9d\x50\xf4\xc6\x01\xdb\x45\x49\x11\x8f\x36\x06\x06\x08\x2e\xe5"
99 struct tests camellia128_tests[] = {
100 { "camellia128",
101 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
103 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
105 "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
106 "\x07\x92\x3A\x39\xEB\x0A\x81\x7D\x1C\x4D\x87\xBD\xB8\x2D\x1F\x1C"
111 static int
112 test_cipher(const EVP_CIPHER *c, struct tests *t)
114 EVP_CIPHER_CTX ectx;
115 EVP_CIPHER_CTX dctx;
116 void *d;
118 EVP_CIPHER_CTX_init(&ectx);
119 EVP_CIPHER_CTX_init(&dctx);
121 if (!EVP_CipherInit_ex(&ectx, c, NULL, t->key, t->iv, 1))
122 errx(1, "%s: EVP_CipherInit_ex encrypt", t->name);
123 if (!EVP_CipherInit_ex(&dctx, c, NULL, t->key, t->iv, 0))
124 errx(1, "%s: EVP_CipherInit_ex decrypt", t->name);
126 d = emalloc(t->datasize);
128 if (!EVP_Cipher(&ectx, d, t->indata, t->datasize))
129 return 1;
131 if (memcmp(d, t->outdata, t->datasize) != 0) {
132 char *s;
133 hex_encode(d, t->datasize, &s);
134 errx(1, "%s: decrypt not the same: %s\n", t->name, s);
137 if (!EVP_Cipher(&dctx, d, d, t->datasize))
138 return 1;
140 if (memcmp(d, t->indata, t->datasize) != 0) {
141 char *s;
142 hex_encode(d, t->datasize, &s);
143 errx(1, "%s: decrypt not the same: %s\n", t->name, s);
146 EVP_CIPHER_CTX_cleanup(&ectx);
147 EVP_CIPHER_CTX_cleanup(&dctx);
148 free(d);
150 return 0;
153 static int version_flag;
154 static int help_flag;
156 static struct getargs args[] = {
157 { "version", 0, arg_flag, &version_flag,
158 "print version", NULL },
159 { "help", 0, arg_flag, &help_flag,
160 NULL, NULL }
163 static void
164 usage (int ret)
166 arg_printusage (args,
167 sizeof(args)/sizeof(*args),
168 NULL,
169 "");
170 exit (ret);
174 main(int argc, char **argv)
176 int ret = 0;
177 int i, idx = 0;
179 setprogname(argv[0]);
181 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
182 usage(1);
184 if (help_flag)
185 usage(0);
187 if(version_flag){
188 print_version(NULL);
189 exit(0);
192 argc -= idx;
193 argv += idx;
195 for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++)
196 ret += test_cipher(EVP_aes_256_cbc(), &aes_tests[i]);
197 for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++)
198 ret += test_cipher(EVP_rc2_40_cbc(), &rc2_40_tests[i]);
199 for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++)
200 ret += test_cipher(EVP_des_ede3_cbc(), &des_ede3_tests[i]);
201 for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++)
202 ret += test_cipher(EVP_camellia_128_cbc(), &camellia128_tests[i]);
204 return ret;