xref: /onnv-gate/usr/src/common/net/wanboot/crypt/cbc_test.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Assertion based test of the CBC implementation.
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * This test can be used to the CBC implementation using either
33*0Sstevel@tonic-gate  * 3DES, AES128, AES192 or AES256. The test string above is encrypted
34*0Sstevel@tonic-gate  * and then decrypted using one of the algorithms and keys below. The test
35*0Sstevel@tonic-gate  * passes if the decrypted string is the same as the original. Note,
36*0Sstevel@tonic-gate  * that this test should not be used to test the underlying algorithms
37*0Sstevel@tonic-gate  * and relies on the correctness of those algorithms.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <strings.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "cbc.h"
44*0Sstevel@tonic-gate #include "des3.h"
45*0Sstevel@tonic-gate #include "aes.h"
46*0Sstevel@tonic-gate #include "cbc_test.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	CBC_MAX_KEY_SIZE	AES_256_KEY_SIZE
49*0Sstevel@tonic-gate #define	CBC_MAX_BLOCK_SIZE	AES_BLOCK_SIZE
50*0Sstevel@tonic-gate #define	CBC_MIN_BLOCK_SIZE	DES3_BLOCK_SIZE
51*0Sstevel@tonic-gate #define	CBC_MAX_IV_SIZE		AES_IV_SIZE
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #define	DES3_KEY	"01234567"
54*0Sstevel@tonic-gate #define	AES_128_KEY	"0123456789ABCDEF"
55*0Sstevel@tonic-gate #define	AES_192_KEY	"0123456789ABCDEFHIJKLMNO"
56*0Sstevel@tonic-gate #define	AES_256_KEY	"0123456789ABCDEFHIJKLMNOPQRSTUVW"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define	TEST_BLOCK_SIZE	(CBC_MAX_BLOCK_SIZE * 2)
59*0Sstevel@tonic-gate #define	TEST_SIZE	(TEST_BLOCK_SIZE * 2)
60*0Sstevel@tonic-gate #define	TEST "This test is successful if this string has a period at the end."
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate int
cbctest(int type)63*0Sstevel@tonic-gate cbctest(int type)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	unsigned char test_string[TEST_SIZE];
66*0Sstevel@tonic-gate 	char iv[CBC_MAX_IV_SIZE];
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	cbc_handle_t ch;
69*0Sstevel@tonic-gate 	void *eh;
70*0Sstevel@tonic-gate 	int ret;
71*0Sstevel@tonic-gate 	int i;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	switch (type) {
74*0Sstevel@tonic-gate 	case CBC_DES3_TYPE:
75*0Sstevel@tonic-gate 		ret = des3_init(&eh);
76*0Sstevel@tonic-gate 		break;
77*0Sstevel@tonic-gate 	case CBC_AES_128_TYPE:
78*0Sstevel@tonic-gate 		ret = aes_init(&eh);
79*0Sstevel@tonic-gate 		break;
80*0Sstevel@tonic-gate 	case CBC_AES_192_TYPE:
81*0Sstevel@tonic-gate 		ret = aes_init(&eh);
82*0Sstevel@tonic-gate 		break;
83*0Sstevel@tonic-gate 	case CBC_AES_256_TYPE:
84*0Sstevel@tonic-gate 		ret = aes_init(&eh);
85*0Sstevel@tonic-gate 		break;
86*0Sstevel@tonic-gate 	default:
87*0Sstevel@tonic-gate 		(void) printf("Illegal encryption type\n");
88*0Sstevel@tonic-gate 		return (-1);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (ret != 0) {
92*0Sstevel@tonic-gate 		(void) printf("Error initializing encryption algorithm\n");
93*0Sstevel@tonic-gate 		return (-1);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	bzero(iv, CBC_MAX_IV_SIZE);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	switch (type) {
99*0Sstevel@tonic-gate 	case CBC_DES3_TYPE:
100*0Sstevel@tonic-gate 		des3_key(eh, (uint8_t *)DES3_KEY);
101*0Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, DES3_KEY_SIZE, DES3_BLOCK_SIZE,
102*0Sstevel@tonic-gate 		    DES3_IV_SIZE, des3_encrypt, des3_decrypt);
103*0Sstevel@tonic-gate 		break;
104*0Sstevel@tonic-gate 	case CBC_AES_128_TYPE:
105*0Sstevel@tonic-gate 		aes_key(eh, (uint8_t *)AES_128_KEY, AES_128_KEY_SIZE);
106*0Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, AES_128_KEY_SIZE, AES_BLOCK_SIZE,
107*0Sstevel@tonic-gate 		    AES_IV_SIZE, aes_encrypt, aes_decrypt);
108*0Sstevel@tonic-gate 		break;
109*0Sstevel@tonic-gate 	case CBC_AES_192_TYPE:
110*0Sstevel@tonic-gate 		aes_key(eh, (uint8_t *)AES_192_KEY, AES_192_KEY_SIZE);
111*0Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, AES_192_KEY_SIZE, AES_BLOCK_SIZE,
112*0Sstevel@tonic-gate 		    AES_IV_SIZE, aes_encrypt, aes_decrypt);
113*0Sstevel@tonic-gate 		break;
114*0Sstevel@tonic-gate 	case CBC_AES_256_TYPE:
115*0Sstevel@tonic-gate 		aes_key(eh, (uint8_t *)AES_256_KEY, AES_256_KEY_SIZE);
116*0Sstevel@tonic-gate 		cbc_makehandle(&ch, eh, AES_256_KEY_SIZE, AES_BLOCK_SIZE,
117*0Sstevel@tonic-gate 		    AES_IV_SIZE, aes_encrypt, aes_decrypt);
118*0Sstevel@tonic-gate 		break;
119*0Sstevel@tonic-gate 	default:
120*0Sstevel@tonic-gate 		/* Should not happen */
121*0Sstevel@tonic-gate 		(void) printf("Illegal encryption type\n");
122*0Sstevel@tonic-gate 		return (-1);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	(void) strcpy((char *)test_string, TEST);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	for (i = 0; i < TEST_SIZE; i += TEST_BLOCK_SIZE) {
128*0Sstevel@tonic-gate 		(void) cbc_encrypt(&ch, (uint8_t *)&test_string[i],
129*0Sstevel@tonic-gate 		    TEST_BLOCK_SIZE, (uint8_t *)iv);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (strcmp((char *)test_string, TEST) == 0) {
133*0Sstevel@tonic-gate 		(void) printf("FAILED [Encryption]\n");
134*0Sstevel@tonic-gate 		goto out;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	bzero(iv, CBC_MAX_IV_SIZE);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	for (i = 0; i < TEST_SIZE; i += TEST_BLOCK_SIZE) {
140*0Sstevel@tonic-gate 		(void) cbc_decrypt(&ch, (uint8_t *)&test_string[i],
141*0Sstevel@tonic-gate 		    TEST_BLOCK_SIZE, (uint8_t *)iv);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if (strcmp((char *)test_string, TEST) == 0) {
145*0Sstevel@tonic-gate 		(void) printf("PASSED\n");
146*0Sstevel@tonic-gate 	} else {
147*0Sstevel@tonic-gate 		(void) printf("FAILED [Decryption]\n");
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate out:
151*0Sstevel@tonic-gate 	switch (type) {
152*0Sstevel@tonic-gate 	case CBC_DES3_TYPE:
153*0Sstevel@tonic-gate 		des3_fini(eh);
154*0Sstevel@tonic-gate 		break;
155*0Sstevel@tonic-gate 	case CBC_AES_128_TYPE:
156*0Sstevel@tonic-gate 	case CBC_AES_192_TYPE:
157*0Sstevel@tonic-gate 	case CBC_AES_256_TYPE:
158*0Sstevel@tonic-gate 		aes_fini(eh);
159*0Sstevel@tonic-gate 		break;
160*0Sstevel@tonic-gate 	default:
161*0Sstevel@tonic-gate 		/* Should not happen */
162*0Sstevel@tonic-gate 		(void) printf("Illegal encryption type\n");
163*0Sstevel@tonic-gate 		return (-1);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	return (0);
167*0Sstevel@tonic-gate }
168