xref: /titanic_52/usr/src/common/net/wanboot/crypt/cbc_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  * Assertion based test of the CBC implementation.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * This test can be used to the CBC implementation using either
33*7c478bd9Sstevel@tonic-gate  * 3DES, AES128, AES192 or AES256. The test string above is encrypted
34*7c478bd9Sstevel@tonic-gate  * and then decrypted using one of the algorithms and keys below. The test
35*7c478bd9Sstevel@tonic-gate  * passes if the decrypted string is the same as the original. Note,
36*7c478bd9Sstevel@tonic-gate  * that this test should not be used to test the underlying algorithms
37*7c478bd9Sstevel@tonic-gate  * and relies on the correctness of those algorithms.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <strings.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include "cbc.h"
44*7c478bd9Sstevel@tonic-gate #include "des3.h"
45*7c478bd9Sstevel@tonic-gate #include "aes.h"
46*7c478bd9Sstevel@tonic-gate #include "cbc_test.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	CBC_MAX_KEY_SIZE	AES_256_KEY_SIZE
49*7c478bd9Sstevel@tonic-gate #define	CBC_MAX_BLOCK_SIZE	AES_BLOCK_SIZE
50*7c478bd9Sstevel@tonic-gate #define	CBC_MIN_BLOCK_SIZE	DES3_BLOCK_SIZE
51*7c478bd9Sstevel@tonic-gate #define	CBC_MAX_IV_SIZE		AES_IV_SIZE
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #define	DES3_KEY	"01234567"
54*7c478bd9Sstevel@tonic-gate #define	AES_128_KEY	"0123456789ABCDEF"
55*7c478bd9Sstevel@tonic-gate #define	AES_192_KEY	"0123456789ABCDEFHIJKLMNO"
56*7c478bd9Sstevel@tonic-gate #define	AES_256_KEY	"0123456789ABCDEFHIJKLMNOPQRSTUVW"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	TEST_BLOCK_SIZE	(CBC_MAX_BLOCK_SIZE * 2)
59*7c478bd9Sstevel@tonic-gate #define	TEST_SIZE	(TEST_BLOCK_SIZE * 2)
60*7c478bd9Sstevel@tonic-gate #define	TEST "This test is successful if this string has a period at the end."
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate int
63*7c478bd9Sstevel@tonic-gate cbctest(int type)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	unsigned char test_string[TEST_SIZE];
66*7c478bd9Sstevel@tonic-gate 	char iv[CBC_MAX_IV_SIZE];
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	cbc_handle_t ch;
69*7c478bd9Sstevel@tonic-gate 	void *eh;
70*7c478bd9Sstevel@tonic-gate 	int ret;
71*7c478bd9Sstevel@tonic-gate 	int i;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	switch (type) {
74*7c478bd9Sstevel@tonic-gate 	case CBC_DES3_TYPE:
75*7c478bd9Sstevel@tonic-gate 		ret = des3_init(&eh);
76*7c478bd9Sstevel@tonic-gate 		break;
77*7c478bd9Sstevel@tonic-gate 	case CBC_AES_128_TYPE:
78*7c478bd9Sstevel@tonic-gate 		ret = aes_init(&eh);
79*7c478bd9Sstevel@tonic-gate 		break;
80*7c478bd9Sstevel@tonic-gate 	case CBC_AES_192_TYPE:
81*7c478bd9Sstevel@tonic-gate 		ret = aes_init(&eh);
82*7c478bd9Sstevel@tonic-gate 		break;
83*7c478bd9Sstevel@tonic-gate 	case CBC_AES_256_TYPE:
84*7c478bd9Sstevel@tonic-gate 		ret = aes_init(&eh);
85*7c478bd9Sstevel@tonic-gate 		break;
86*7c478bd9Sstevel@tonic-gate 	default:
87*7c478bd9Sstevel@tonic-gate 		(void) printf("Illegal encryption type\n");
88*7c478bd9Sstevel@tonic-gate 		return (-1);
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if (ret != 0) {
92*7c478bd9Sstevel@tonic-gate 		(void) printf("Error initializing encryption algorithm\n");
93*7c478bd9Sstevel@tonic-gate 		return (-1);
94*7c478bd9Sstevel@tonic-gate 	}
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	bzero(iv, CBC_MAX_IV_SIZE);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	switch (type) {
99*7c478bd9Sstevel@tonic-gate 	case CBC_DES3_TYPE:
100*7c478bd9Sstevel@tonic-gate 		des3_key(eh, (uint8_t *)DES3_KEY);
101*7c478bd9Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, DES3_KEY_SIZE, DES3_BLOCK_SIZE,
102*7c478bd9Sstevel@tonic-gate 		    DES3_IV_SIZE, des3_encrypt, des3_decrypt);
103*7c478bd9Sstevel@tonic-gate 		break;
104*7c478bd9Sstevel@tonic-gate 	case CBC_AES_128_TYPE:
105*7c478bd9Sstevel@tonic-gate 		aes_key(eh, (uint8_t *)AES_128_KEY, AES_128_KEY_SIZE);
106*7c478bd9Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, AES_128_KEY_SIZE, AES_BLOCK_SIZE,
107*7c478bd9Sstevel@tonic-gate 		    AES_IV_SIZE, aes_encrypt, aes_decrypt);
108*7c478bd9Sstevel@tonic-gate 		break;
109*7c478bd9Sstevel@tonic-gate 	case CBC_AES_192_TYPE:
110*7c478bd9Sstevel@tonic-gate 		aes_key(eh, (uint8_t *)AES_192_KEY, AES_192_KEY_SIZE);
111*7c478bd9Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, AES_192_KEY_SIZE, AES_BLOCK_SIZE,
112*7c478bd9Sstevel@tonic-gate 		    AES_IV_SIZE, aes_encrypt, aes_decrypt);
113*7c478bd9Sstevel@tonic-gate 		break;
114*7c478bd9Sstevel@tonic-gate 	case CBC_AES_256_TYPE:
115*7c478bd9Sstevel@tonic-gate 		aes_key(eh, (uint8_t *)AES_256_KEY, AES_256_KEY_SIZE);
116*7c478bd9Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, AES_256_KEY_SIZE, AES_BLOCK_SIZE,
117*7c478bd9Sstevel@tonic-gate 		    AES_IV_SIZE, aes_encrypt, aes_decrypt);
118*7c478bd9Sstevel@tonic-gate 		break;
119*7c478bd9Sstevel@tonic-gate 	default:
120*7c478bd9Sstevel@tonic-gate 		/* Should not happen */
121*7c478bd9Sstevel@tonic-gate 		(void) printf("Illegal encryption type\n");
122*7c478bd9Sstevel@tonic-gate 		return (-1);
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	(void) strcpy((char *)test_string, TEST);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TEST_SIZE; i += TEST_BLOCK_SIZE) {
128*7c478bd9Sstevel@tonic-gate 		(void) cbc_encrypt(&ch, (uint8_t *)&test_string[i],
129*7c478bd9Sstevel@tonic-gate 		    TEST_BLOCK_SIZE, (uint8_t *)iv);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (strcmp((char *)test_string, TEST) == 0) {
133*7c478bd9Sstevel@tonic-gate 		(void) printf("FAILED [Encryption]\n");
134*7c478bd9Sstevel@tonic-gate 		goto out;
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	bzero(iv, CBC_MAX_IV_SIZE);
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TEST_SIZE; i += TEST_BLOCK_SIZE) {
140*7c478bd9Sstevel@tonic-gate 		(void) cbc_decrypt(&ch, (uint8_t *)&test_string[i],
141*7c478bd9Sstevel@tonic-gate 		    TEST_BLOCK_SIZE, (uint8_t *)iv);
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (strcmp((char *)test_string, TEST) == 0) {
145*7c478bd9Sstevel@tonic-gate 		(void) printf("PASSED\n");
146*7c478bd9Sstevel@tonic-gate 	} else {
147*7c478bd9Sstevel@tonic-gate 		(void) printf("FAILED [Decryption]\n");
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate out:
151*7c478bd9Sstevel@tonic-gate 	switch (type) {
152*7c478bd9Sstevel@tonic-gate 	case CBC_DES3_TYPE:
153*7c478bd9Sstevel@tonic-gate 		des3_fini(eh);
154*7c478bd9Sstevel@tonic-gate 		break;
155*7c478bd9Sstevel@tonic-gate 	case CBC_AES_128_TYPE:
156*7c478bd9Sstevel@tonic-gate 	case CBC_AES_192_TYPE:
157*7c478bd9Sstevel@tonic-gate 	case CBC_AES_256_TYPE:
158*7c478bd9Sstevel@tonic-gate 		aes_fini(eh);
159*7c478bd9Sstevel@tonic-gate 		break;
160*7c478bd9Sstevel@tonic-gate 	default:
161*7c478bd9Sstevel@tonic-gate 		/* Should not happen */
162*7c478bd9Sstevel@tonic-gate 		(void) printf("Illegal encryption type\n");
163*7c478bd9Sstevel@tonic-gate 		return (-1);
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	return (0);
167*7c478bd9Sstevel@tonic-gate }
168