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 * Assertion based test of the CBC implementation. 31 * 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. 38 */ 39 40 #include <stdio.h> 41 #include <strings.h> 42 43 #include "cbc.h" 44 #include "des3.h" 45 #include "aes.h" 46 #include "cbc_test.h" 47 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 52 53 #define DES3_KEY "01234567" 54 #define AES_128_KEY "0123456789ABCDEF" 55 #define AES_192_KEY "0123456789ABCDEFHIJKLMNO" 56 #define AES_256_KEY "0123456789ABCDEFHIJKLMNOPQRSTUVW" 57 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." 61 62 int 63 cbctest(int type) 64 { 65 unsigned char test_string[TEST_SIZE]; 66 char iv[CBC_MAX_IV_SIZE]; 67 68 cbc_handle_t ch; 69 void *eh; 70 int ret; 71 int i; 72 73 switch (type) { 74 case CBC_DES3_TYPE: 75 ret = des3_init(&eh); 76 break; 77 case CBC_AES_128_TYPE: 78 ret = aes_init(&eh); 79 break; 80 case CBC_AES_192_TYPE: 81 ret = aes_init(&eh); 82 break; 83 case CBC_AES_256_TYPE: 84 ret = aes_init(&eh); 85 break; 86 default: 87 (void) printf("Illegal encryption type\n"); 88 return (-1); 89 } 90 91 if (ret != 0) { 92 (void) printf("Error initializing encryption algorithm\n"); 93 return (-1); 94 } 95 96 bzero(iv, CBC_MAX_IV_SIZE); 97 98 switch (type) { 99 case CBC_DES3_TYPE: 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); 103 break; 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); 108 break; 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); 113 break; 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); 118 break; 119 default: 120 /* Should not happen */ 121 (void) printf("Illegal encryption type\n"); 122 return (-1); 123 } 124 125 (void) strcpy((char *)test_string, TEST); 126 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); 130 } 131 132 if (strcmp((char *)test_string, TEST) == 0) { 133 (void) printf("FAILED [Encryption]\n"); 134 goto out; 135 } 136 137 bzero(iv, CBC_MAX_IV_SIZE); 138 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); 142 } 143 144 if (strcmp((char *)test_string, TEST) == 0) { 145 (void) printf("PASSED\n"); 146 } else { 147 (void) printf("FAILED [Decryption]\n"); 148 } 149 150 out: 151 switch (type) { 152 case CBC_DES3_TYPE: 153 des3_fini(eh); 154 break; 155 case CBC_AES_128_TYPE: 156 case CBC_AES_192_TYPE: 157 case CBC_AES_256_TYPE: 158 aes_fini(eh); 159 break; 160 default: 161 /* Should not happen */ 162 (void) printf("Illegal encryption type\n"); 163 return (-1); 164 } 165 166 return (0); 167 } 168