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
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.
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]
23 * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Assertion based test of the CBC implementation.
32 * This test can be used to the CBC implementation using either
33 * 3DES, AES128, AES192 or AES256. The test string above is encrypted
34 * and then decrypted using one of the algorithms and keys below. The test
35 * passes if the decrypted string is the same as the original. Note,
36 * that this test should not be used to test the underlying algorithms
37 * and relies on the correctness of those algorithms.
48 #define CBC_MAX_KEY_SIZE AES_256_KEY_SIZE
49 #define CBC_MAX_BLOCK_SIZE AES_BLOCK_SIZE
50 #define CBC_MIN_BLOCK_SIZE DES3_BLOCK_SIZE
51 #define CBC_MAX_IV_SIZE AES_IV_SIZE
53 #define DES3_KEY "01234567"
54 #define AES_128_KEY "0123456789ABCDEF"
55 #define AES_192_KEY "0123456789ABCDEFHIJKLMNO"
56 #define AES_256_KEY "0123456789ABCDEFHIJKLMNOPQRSTUVW"
58 #define TEST_BLOCK_SIZE (CBC_MAX_BLOCK_SIZE * 2)
59 #define TEST_SIZE (TEST_BLOCK_SIZE * 2)
60 #define TEST "This test is successful if this string has a period at the end."
65 unsigned char test_string
[TEST_SIZE
];
66 char iv
[CBC_MAX_IV_SIZE
];
77 case CBC_AES_128_TYPE
:
80 case CBC_AES_192_TYPE
:
83 case CBC_AES_256_TYPE
:
87 (void) printf("Illegal encryption type\n");
92 (void) printf("Error initializing encryption algorithm\n");
96 bzero(iv
, CBC_MAX_IV_SIZE
);
100 des3_key(eh
, (uint8_t *)DES3_KEY
);
101 cbc_makehandle(&ch
, eh
, DES3_KEY_SIZE
, DES3_BLOCK_SIZE
,
102 DES3_IV_SIZE
, des3_encrypt
, des3_decrypt
);
104 case CBC_AES_128_TYPE
:
105 aes_key(eh
, (uint8_t *)AES_128_KEY
, AES_128_KEY_SIZE
);
106 cbc_makehandle(&ch
, eh
, AES_128_KEY_SIZE
, AES_BLOCK_SIZE
,
107 AES_IV_SIZE
, aes_encrypt
, aes_decrypt
);
109 case CBC_AES_192_TYPE
:
110 aes_key(eh
, (uint8_t *)AES_192_KEY
, AES_192_KEY_SIZE
);
111 cbc_makehandle(&ch
, eh
, AES_192_KEY_SIZE
, AES_BLOCK_SIZE
,
112 AES_IV_SIZE
, aes_encrypt
, aes_decrypt
);
114 case CBC_AES_256_TYPE
:
115 aes_key(eh
, (uint8_t *)AES_256_KEY
, AES_256_KEY_SIZE
);
116 cbc_makehandle(&ch
, eh
, AES_256_KEY_SIZE
, AES_BLOCK_SIZE
,
117 AES_IV_SIZE
, aes_encrypt
, aes_decrypt
);
120 /* Should not happen */
121 (void) printf("Illegal encryption type\n");
125 (void) strcpy((char *)test_string
, TEST
);
127 for (i
= 0; i
< TEST_SIZE
; i
+= TEST_BLOCK_SIZE
) {
128 (void) cbc_encrypt(&ch
, (uint8_t *)&test_string
[i
],
129 TEST_BLOCK_SIZE
, (uint8_t *)iv
);
132 if (strcmp((char *)test_string
, TEST
) == 0) {
133 (void) printf("FAILED [Encryption]\n");
137 bzero(iv
, CBC_MAX_IV_SIZE
);
139 for (i
= 0; i
< TEST_SIZE
; i
+= TEST_BLOCK_SIZE
) {
140 (void) cbc_decrypt(&ch
, (uint8_t *)&test_string
[i
],
141 TEST_BLOCK_SIZE
, (uint8_t *)iv
);
144 if (strcmp((char *)test_string
, TEST
) == 0) {
145 (void) printf("PASSED\n");
147 (void) printf("FAILED [Decryption]\n");
155 case CBC_AES_128_TYPE
:
156 case CBC_AES_192_TYPE
:
157 case CBC_AES_256_TYPE
:
161 /* Should not happen */
162 (void) printf("Illegal encryption type\n");