unbreak des-test
[heimdal.git] / lib / hcrypto / test_cipher.c
blob106b9933354432cf73517f32c7b97812e62391e1
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;
64 struct tests aes_tests[] = {
65 { "aes-256",
66 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
67 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
68 32,
69 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
70 16,
71 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
72 "\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87"
76 struct tests rc2_40_tests[] = {
77 { "rc2-40",
78 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
79 16,
80 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
81 16,
82 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
83 "\xc0\xb8\xff\xa5\xd6\xeb\xc9\x62\xcc\x52\x5f\xfe\x9a\x3c\x97\xe6"
87 struct tests des_ede3_tests[] = {
88 { "des-ede3",
89 "\x19\x17\xff\xe6\xbb\x77\x2e\xfc"
90 "\x29\x76\x43\xbc\x63\x56\x7e\x9a"
91 "\x00\x2e\x4d\x43\x1d\x5f\xfd\x58",
92 24,
93 "\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
94 16,
95 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
96 "\x55\x95\x97\x76\xa9\x6c\x66\x40\x64\xc7\xf4\x1c\x21\xb7\x14\x1b"
100 struct tests camellia128_tests[] = {
101 { "camellia128",
102 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
104 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
106 "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
107 "\x07\x92\x3A\x39\xEB\x0A\x81\x7D\x1C\x4D\x87\xBD\xB8\x2D\x1F\x1C"
112 static int
113 test_cipher(const EVP_CIPHER *c, struct tests *t)
115 EVP_CIPHER_CTX ectx;
116 EVP_CIPHER_CTX dctx;
117 void *d;
119 EVP_CIPHER_CTX_init(&ectx);
120 EVP_CIPHER_CTX_init(&dctx);
122 if (!EVP_CipherInit_ex(&ectx, c, NULL, t->key, t->iv, 1))
123 errx(1, "%s: EVP_CipherInit_ex encrypt", t->name);
124 if (!EVP_CipherInit_ex(&dctx, c, NULL, t->key, t->iv, 0))
125 errx(1, "%s: EVP_CipherInit_ex decrypt", t->name);
127 d = emalloc(t->datasize);
129 if (!EVP_Cipher(&ectx, d, t->indata, t->datasize))
130 return 1;
132 if (memcmp(d, t->outdata, t->datasize) != 0) {
133 char *s;
134 hex_encode(d, t->datasize, &s);
135 errx(1, "%s: encrypt not the same: %s", t->name, s);
138 if (!EVP_Cipher(&dctx, d, d, t->datasize))
139 return 1;
141 if (memcmp(d, t->indata, t->datasize) != 0) {
142 char *s;
143 hex_encode(d, t->datasize, &s);
144 errx(1, "%s: decrypt not the same: %s", t->name, s);
147 EVP_CIPHER_CTX_cleanup(&ectx);
148 EVP_CIPHER_CTX_cleanup(&dctx);
149 free(d);
151 return 0;
154 static int version_flag;
155 static int help_flag;
157 static struct getargs args[] = {
158 { "version", 0, arg_flag, &version_flag,
159 "print version", NULL },
160 { "help", 0, arg_flag, &help_flag,
161 NULL, NULL }
164 static void
165 usage (int ret)
167 arg_printusage (args,
168 sizeof(args)/sizeof(*args),
169 NULL,
170 "");
171 exit (ret);
175 main(int argc, char **argv)
177 int ret = 0;
178 int i, idx = 0;
180 setprogname(argv[0]);
182 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
183 usage(1);
185 if (help_flag)
186 usage(0);
188 if(version_flag){
189 print_version(NULL);
190 exit(0);
193 argc -= idx;
194 argv += idx;
196 for (i = 0; i < sizeof(aes_tests)/sizeof(aes_tests[0]); i++)
197 ret += test_cipher(EVP_aes_256_cbc(), &aes_tests[i]);
198 for (i = 0; i < sizeof(rc2_40_tests)/sizeof(rc2_40_tests[0]); i++)
199 ret += test_cipher(EVP_rc2_40_cbc(), &rc2_40_tests[i]);
200 for (i = 0; i < sizeof(des_ede3_tests)/sizeof(des_ede3_tests[0]); i++)
201 ret += test_cipher(EVP_des_ede3_cbc(), &des_ede3_tests[i]);
202 for (i = 0; i < sizeof(camellia128_tests)/sizeof(camellia128_tests[0]); i++)
203 ret += test_cipher(EVP_camellia_128_cbc(), &camellia128_tests[i]);
205 return ret;