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