No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / hcrypto / test_cipher.c
blobb7111c747b7d7332a99f1f1e256d7688f08ba74d
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("$Heimdal: test_cipher.c 21923 2007-08-16 14:44:55Z lha $"
40 "$NetBSD$");
41 #endif
43 #include <sys/types.h>
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <getarg.h>
49 #include <roken.h>
51 #include <evp.h>
52 #include <hex.h>
53 #include <err.h>
55 struct tests {
56 const char *name;
57 void *key;
58 size_t keysize;
59 void *iv;
60 size_t datasize;
61 void *indata;
62 void *outdata;
63 size_t outdatasize;
66 struct tests aes_tests[] = {
67 { "aes-256",
68 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
69 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
70 32,
71 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
72 16,
73 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
74 "\xdc\x95\xc0\x78\xa2\x40\x89\x89\xad\x48\xa2\x14\x92\x84\x20\x87"
78 struct tests rc2_40_tests[] = {
79 { "rc2-40",
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 16,
84 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
85 "\xc0\xb8\xff\xa5\xd6\xeb\xc9\x62\xcc\x52\x5f\xfe\x9a\x3c\x97\xe6"
89 struct tests des_ede3_tests[] = {
90 { "des-ede3",
91 "1917ffe6bb772efc297643bc63567e9a002e4d431d5ffd58",
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 "\x9d\x50\xf4\xc6\x01\xdb\x45\x49\x11\x8f\x36\x06\x06\x08\x2e\xe5"
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: decrypt not the same: %s\n", 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\n", 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;