2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
42 #include <sys/types.h>
64 struct tests aes_tests
[] = {
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",
69 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
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
[] = {
78 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
80 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
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
[] = {
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",
93 "\xbf\x9a\x12\xb7\x26\x69\xfd\x05",
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
[] = {
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"
113 test_cipher(const EVP_CIPHER
*c
, struct tests
*t
)
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
))
132 if (memcmp(d
, t
->outdata
, t
->datasize
) != 0) {
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
))
141 if (memcmp(d
, t
->indata
, t
->datasize
) != 0) {
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
);
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
,
167 arg_printusage (args
,
168 sizeof(args
)/sizeof(*args
),
175 main(int argc
, char **argv
)
180 setprogname(argv
[0]);
182 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, 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
]);