[BACK]Return to rijndaeltest.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / regress / sys / crypto / rijndael

File: [cvs.NetBSD.org] / src / regress / sys / crypto / rijndael / Attic / rijndaeltest.c (download)

Revision 1.7, Fri May 12 01:56:39 2006 UTC (16 years, 3 months ago) by mrg
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, mjf-devfs2-base, mjf-devfs2, matt-premerge-20091211, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, abandoned-netbsd-4-base, abandoned-netbsd-4
Changes since 1.6: +3 -3 lines

sprinkle some (char *) to pacify GCC4.

/*	$NetBSD: rijndaeltest.c,v 1.7 2006/05/12 01:56:39 mrg Exp $	*/
/*	$KAME: rijndaeltest.c,v 1.7 2001/05/27 01:56:45 itojun Exp $	*/

/*
 * Copyright (C) 2000 WIDE Project.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include <sys/types.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <err.h>

#include <crypto/rijndael/rijndael.h>
#include <crypto/rijndael/rijndael-api-fst.h>

/* decrypt test */
struct {
	const char *key;
	const char *ct;
	const char *pt;
} dvector[] = {
    {
	"00000000000000000000000000000000",
	"00000000000000000000000000000000",
	"44416AC2D1F53C583303917E6BE9EBE0",
    },
    {
	"DE11FF0A429E1CD3DE016DAC294F771187463793E21C29525A3B282CDCAD6270",
	"E1268BA8A1473DEDE6CA64DDF2C8B805",
	"4DE0C6DF7CB1697284604D60271BC59A",
    },
    {
	NULL, NULL, NULL,
    },
};

/* encrypt test */
struct {
	const char *key;
	const char *pt;
	const char *ct;
} evector[] = {
    {
	"00000000000000000000000000000000",
	"00000000000000000000000000000000",
	"C34C052CC0DA8D73451AFE5F03BE297F",
    },
    {
	"982D617A0F737342E99123A5A573D266F4961915B32DCA4118AD5CF1DCB6ED00",
	"6F8606BBA6CC03A5D0A64FE21E277B60",
	"1F6763DF807A7E70960D4CD3118E601A",
    },
    {
	NULL, NULL, NULL,
    },
};

static void hex2key(u_int8_t *, size_t, const char *);
int main(int, char **);

static void
hex2key(p, l, s)
	u_int8_t *p;
	size_t l;
	const char *s;
{
	int i;
	u_int v;

	for (i = 0; i < l && *s; i++) {
		sscanf(s, "%02x", &v);
		*p++ = v & 0xff;
		s += 2;
	}

	if (*s) {
		errx(1, "hex2key overrun");
		/*NOTREACHED*/
	}
}

int
main(argc, argv)
	int argc;
	char **argv;
{
	int i, j;
	keyInstance k;
	cipherInstance c;
	int error;
	const char *test;
	u_int8_t key[32], input[16], output[16], answer[16];
	int nrounds, rounds;

	if (argc > 1)
		nrounds = atoi(argv[1]);
	else
		nrounds = 1;

	error = 0;

	rounds = nrounds;
again1:
	test = "decrypt test";
	for (i = 0; dvector[i].key; i++) {
		hex2key(key, sizeof(key), dvector[i].key);
		hex2key(input, sizeof(input), dvector[i].ct);
		memset(output, 0, sizeof(output));
		hex2key(answer, sizeof(answer), dvector[i].pt);

		/* LINTED const cast */
		if (rijndael_makeKey(&k, DIR_DECRYPT,
		    strlen(dvector[i].key) * 4, (char *)key) < 0) {
			printf("makeKey failed for %s %d\n", test, i);
			error++;
			continue;
		}
		if (rijndael_cipherInit(&c, MODE_ECB, NULL) < 0) {
			printf("cipherInit failed for %s %d\n", test, i);
			error++;
			continue;
		}

		for (j = 0; j < 10000; j++) {
			if (rijndael_blockDecrypt(&c, &k, input,
			    sizeof(input) * 8, output) < 0) {
				printf("blockDecrypt failed for %s %d/%d\n",
				    test, i, j);
				error++;
				goto next1;
			}

			memcpy(input, output, sizeof(input));
		}

		if (memcmp(output, answer, sizeof(output)) != 0) {
			printf("result mismatch for %s %d\n", test, i);
			error++;
		}

		if (nrounds == 1)
			printf("%s %d successful\n", test, i);
next1:;
	}
	if (--rounds)
		goto again1;

	rounds = nrounds;
again2:
	test = "encrypt test";
	for (i = 0; evector[i].key; i++) {
		hex2key(key, sizeof(key), evector[i].key);
		hex2key(input, sizeof(input), evector[i].pt);
		memset(output, 0, sizeof(output));
		hex2key(answer, sizeof(answer), evector[i].ct);

		/* LINTED const cast */
		if (rijndael_makeKey(&k, DIR_ENCRYPT,
		    strlen(evector[i].key) * 4, (char *)key) < 0) {
			printf("makeKey failed for %s %d\n", test, i);
			error++;
			continue;
		}
		if (rijndael_cipherInit(&c, MODE_ECB, NULL) < 0) {
			printf("cipherInit failed for %s %d\n", test, i);
			error++;
			continue;
		}

		for (j = 0; j < 10000; j++) {
			if (rijndael_blockEncrypt(&c, &k, input,
			    sizeof(input) * 8, output) < 0) {
				printf("blockEncrypt failed for %s %d/%d\n",
				    test, i, j);
				error++;
				goto next2;
			}

			memcpy(input, output, sizeof(input));
		}

		if (memcmp(output, answer, sizeof(output)) != 0) {
			printf("result mismatch for %s %d\n", test, i);
			error++;
			continue;
		}

		if (nrounds == 1)
			printf("%s %d successful\n", test, i);
next2:;
	}
	if (--rounds)
		goto again2;

	exit(error);
}