1 /* $NetBSD: rijndaeltest.c,v 1.6 2005/02/06 06:05:20 perry Exp $ */
2 /* $KAME: rijndaeltest.c,v 1.7 2001/05/27 01:56:45 itojun Exp $ */
5 * Copyright (C) 2000 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * 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.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
41 #include <crypto/rijndael/rijndael.h>
42 #include <crypto/rijndael/rijndael-api-fst.h>
51 "00000000000000000000000000000000",
52 "00000000000000000000000000000000",
53 "44416AC2D1F53C583303917E6BE9EBE0",
56 "DE11FF0A429E1CD3DE016DAC294F771187463793E21C29525A3B282CDCAD6270",
57 "E1268BA8A1473DEDE6CA64DDF2C8B805",
58 "4DE0C6DF7CB1697284604D60271BC59A",
72 "00000000000000000000000000000000",
73 "00000000000000000000000000000000",
74 "C34C052CC0DA8D73451AFE5F03BE297F",
77 "982D617A0F737342E99123A5A573D266F4961915B32DCA4118AD5CF1DCB6ED00",
78 "6F8606BBA6CC03A5D0A64FE21E277B60",
79 "1F6763DF807A7E70960D4CD3118E601A",
86 static void hex2key(u_int8_t
*, size_t, const char *);
87 int main(int, char **);
98 for (i
= 0; i
< l
&& *s
; i
++) {
99 sscanf(s
, "%02x", &v
);
105 errx(1, "hex2key overrun");
120 u_int8_t key
[32], input
[16], output
[16], answer
[16];
124 nrounds
= atoi(argv
[1]);
132 test
= "decrypt test";
133 for (i
= 0; dvector
[i
].key
; i
++) {
134 hex2key(key
, sizeof(key
), dvector
[i
].key
);
135 hex2key(input
, sizeof(input
), dvector
[i
].ct
);
136 memset(output
, 0, sizeof(output
));
137 hex2key(answer
, sizeof(answer
), dvector
[i
].pt
);
139 /* LINTED const cast */
140 if (rijndael_makeKey(&k
, DIR_DECRYPT
,
141 strlen(dvector
[i
].key
) * 4, (char *)key
) < 0) {
142 printf("makeKey failed for %s %d\n", test
, i
);
146 if (rijndael_cipherInit(&c
, MODE_ECB
, NULL
) < 0) {
147 printf("cipherInit failed for %s %d\n", test
, i
);
152 for (j
= 0; j
< 10000; j
++) {
153 if (rijndael_blockDecrypt(&c
, &k
, input
,
154 sizeof(input
) * 8, output
) < 0) {
155 printf("blockDecrypt failed for %s %d/%d\n",
161 memcpy(input
, output
, sizeof(input
));
164 if (memcmp(output
, answer
, sizeof(output
)) != 0) {
165 printf("result mismatch for %s %d\n", test
, i
);
170 printf("%s %d successful\n", test
, i
);
178 test
= "encrypt test";
179 for (i
= 0; evector
[i
].key
; i
++) {
180 hex2key(key
, sizeof(key
), evector
[i
].key
);
181 hex2key(input
, sizeof(input
), evector
[i
].pt
);
182 memset(output
, 0, sizeof(output
));
183 hex2key(answer
, sizeof(answer
), evector
[i
].ct
);
185 /* LINTED const cast */
186 if (rijndael_makeKey(&k
, DIR_ENCRYPT
,
187 strlen(evector
[i
].key
) * 4, (char *)key
) < 0) {
188 printf("makeKey failed for %s %d\n", test
, i
);
192 if (rijndael_cipherInit(&c
, MODE_ECB
, NULL
) < 0) {
193 printf("cipherInit failed for %s %d\n", test
, i
);
198 for (j
= 0; j
< 10000; j
++) {
199 if (rijndael_blockEncrypt(&c
, &k
, input
,
200 sizeof(input
) * 8, output
) < 0) {
201 printf("blockEncrypt failed for %s %d/%d\n",
207 memcpy(input
, output
, sizeof(input
));
210 if (memcmp(output
, answer
, sizeof(output
)) != 0) {
211 printf("result mismatch for %s %d\n", test
, i
);
217 printf("%s %d successful\n", test
, i
);