xref: /titanic_50/usr/src/common/net/wanboot/crypt/aes_test.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * AES tests as defined by FIPS 197.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Encrypts plain text with the defined key and verifies that the result
33*7c478bd9Sstevel@tonic-gate  * is the expected cipher. Then decrypts the cipher and verifies that the
34*7c478bd9Sstevel@tonic-gate  * result is the original plain text. One test is run for each AES128,
35*7c478bd9Sstevel@tonic-gate  * AES192 and AES256.
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <strings.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include "aes.h"
42*7c478bd9Sstevel@tonic-gate #include "cmn_test.h"
43*7c478bd9Sstevel@tonic-gate #include "aes_test.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate typedef struct test_data {
46*7c478bd9Sstevel@tonic-gate 	char key[AES_256_KEY_SIZE * 2];
47*7c478bd9Sstevel@tonic-gate 	char plain[AES_BLOCK_SIZE * 2];
48*7c478bd9Sstevel@tonic-gate 	char cipher[AES_BLOCK_SIZE * 2];
49*7c478bd9Sstevel@tonic-gate 	uint32_t keysize;
50*7c478bd9Sstevel@tonic-gate } test_data_t;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static test_data_t td[] = {
53*7c478bd9Sstevel@tonic-gate 	{ "000102030405060708090a0b0c0d0e0f",
54*7c478bd9Sstevel@tonic-gate 	    "00112233445566778899aabbccddeeff",
55*7c478bd9Sstevel@tonic-gate 	    "69c4e0d86a7b0430d8cdb78070b4c55a", AES_128_KEY_SIZE },
56*7c478bd9Sstevel@tonic-gate 	{ "000102030405060708090a0b0c0d0e0f1011121314151617",
57*7c478bd9Sstevel@tonic-gate 	    "00112233445566778899aabbccddeeff",
58*7c478bd9Sstevel@tonic-gate 	    "dda97ca4864cdfe06eaf70a0ec0d7191", AES_192_KEY_SIZE },
59*7c478bd9Sstevel@tonic-gate 	{ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
60*7c478bd9Sstevel@tonic-gate 	    "00112233445566778899aabbccddeeff",
61*7c478bd9Sstevel@tonic-gate 	    "8ea2b7ca516745bfeafc49904b496089", AES_256_KEY_SIZE }
62*7c478bd9Sstevel@tonic-gate };
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate int
aestest(void)65*7c478bd9Sstevel@tonic-gate aestest(void)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	void *ah;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	unsigned char key[AES_256_KEY_SIZE];
70*7c478bd9Sstevel@tonic-gate 	unsigned char plain[AES_BLOCK_SIZE];
71*7c478bd9Sstevel@tonic-gate 	unsigned char cipher[AES_BLOCK_SIZE];
72*7c478bd9Sstevel@tonic-gate 	unsigned char work[AES_BLOCK_SIZE];
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	int fail;
75*7c478bd9Sstevel@tonic-gate 	int num;
76*7c478bd9Sstevel@tonic-gate 	int i;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if (aes_init(&ah) != 0) {
79*7c478bd9Sstevel@tonic-gate 		(void) printf("Error initializing AES\n");
80*7c478bd9Sstevel@tonic-gate 		return (-1);
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	num = sizeof (td) / sizeof (test_data_t);
84*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
85*7c478bd9Sstevel@tonic-gate 		fail = 0;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 		(void) printf("Test #%d [AES%d] ", i, td[i].keysize * 8);
88*7c478bd9Sstevel@tonic-gate 		getxdata(key, td[i].key, td[i].keysize);
89*7c478bd9Sstevel@tonic-gate 		aes_key(ah, key, td[i].keysize);
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 		getxdata(plain, td[i].plain, AES_BLOCK_SIZE);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 		getxdata(cipher, td[i].cipher, AES_BLOCK_SIZE);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 		bcopy(plain, work, AES_BLOCK_SIZE);
96*7c478bd9Sstevel@tonic-gate 		aes_encrypt(ah, work);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		if (bcmp(work, cipher, AES_BLOCK_SIZE) != 0) {
99*7c478bd9Sstevel@tonic-gate 			(void) printf("FAILED [Encrypt]");
100*7c478bd9Sstevel@tonic-gate 			fail++;
101*7c478bd9Sstevel@tonic-gate 		}
102*7c478bd9Sstevel@tonic-gate 		aes_decrypt(ah, work);
103*7c478bd9Sstevel@tonic-gate 		if (bcmp(work, plain, AES_BLOCK_SIZE) != 0) {
104*7c478bd9Sstevel@tonic-gate 			(void) printf("FAILED [Decrypt]");
105*7c478bd9Sstevel@tonic-gate 			fail++;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 		if (fail == 0)
108*7c478bd9Sstevel@tonic-gate 			(void) printf("PASSED");
109*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	aes_fini(ah);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	return (fail);
115*7c478bd9Sstevel@tonic-gate }
116