Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / sys / crypto / rijndael / rijndaeltest.c
blobfe5bc06ebe05111b25bfa59aab9fa06d9067c07a
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 $ */
4 /*
5 * Copyright (C) 2000 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <err.h>
41 #include <crypto/rijndael/rijndael.h>
42 #include <crypto/rijndael/rijndael-api-fst.h>
44 /* decrypt test */
45 struct {
46 const char *key;
47 const char *ct;
48 const char *pt;
49 } dvector[] = {
51 "00000000000000000000000000000000",
52 "00000000000000000000000000000000",
53 "44416AC2D1F53C583303917E6BE9EBE0",
56 "DE11FF0A429E1CD3DE016DAC294F771187463793E21C29525A3B282CDCAD6270",
57 "E1268BA8A1473DEDE6CA64DDF2C8B805",
58 "4DE0C6DF7CB1697284604D60271BC59A",
61 NULL, NULL, NULL,
65 /* encrypt test */
66 struct {
67 const char *key;
68 const char *pt;
69 const char *ct;
70 } evector[] = {
72 "00000000000000000000000000000000",
73 "00000000000000000000000000000000",
74 "C34C052CC0DA8D73451AFE5F03BE297F",
77 "982D617A0F737342E99123A5A573D266F4961915B32DCA4118AD5CF1DCB6ED00",
78 "6F8606BBA6CC03A5D0A64FE21E277B60",
79 "1F6763DF807A7E70960D4CD3118E601A",
82 NULL, NULL, NULL,
86 static void hex2key(u_int8_t *, size_t, const char *);
87 int main(int, char **);
89 static void
90 hex2key(p, l, s)
91 u_int8_t *p;
92 size_t l;
93 const char *s;
95 int i;
96 u_int v;
98 for (i = 0; i < l && *s; i++) {
99 sscanf(s, "%02x", &v);
100 *p++ = v & 0xff;
101 s += 2;
104 if (*s) {
105 errx(1, "hex2key overrun");
106 /*NOTREACHED*/
111 main(argc, argv)
112 int argc;
113 char **argv;
115 int i, j;
116 keyInstance k;
117 cipherInstance c;
118 int error;
119 const char *test;
120 u_int8_t key[32], input[16], output[16], answer[16];
121 int nrounds, rounds;
123 if (argc > 1)
124 nrounds = atoi(argv[1]);
125 else
126 nrounds = 1;
128 error = 0;
130 rounds = nrounds;
131 again1:
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);
143 error++;
144 continue;
146 if (rijndael_cipherInit(&c, MODE_ECB, NULL) < 0) {
147 printf("cipherInit failed for %s %d\n", test, i);
148 error++;
149 continue;
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",
156 test, i, j);
157 error++;
158 goto next1;
161 memcpy(input, output, sizeof(input));
164 if (memcmp(output, answer, sizeof(output)) != 0) {
165 printf("result mismatch for %s %d\n", test, i);
166 error++;
169 if (nrounds == 1)
170 printf("%s %d successful\n", test, i);
171 next1:;
173 if (--rounds)
174 goto again1;
176 rounds = nrounds;
177 again2:
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);
189 error++;
190 continue;
192 if (rijndael_cipherInit(&c, MODE_ECB, NULL) < 0) {
193 printf("cipherInit failed for %s %d\n", test, i);
194 error++;
195 continue;
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",
202 test, i, j);
203 error++;
204 goto next2;
207 memcpy(input, output, sizeof(input));
210 if (memcmp(output, answer, sizeof(output)) != 0) {
211 printf("result mismatch for %s %d\n", test, i);
212 error++;
213 continue;
216 if (nrounds == 1)
217 printf("%s %d successful\n", test, i);
218 next2:;
220 if (--rounds)
221 goto again2;
223 exit(error);