10Sstevel@tonic-gate /* ====================================================================
20Sstevel@tonic-gate * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate * are met:
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
90Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
130Sstevel@tonic-gate * the documentation and/or other materials provided with the
140Sstevel@tonic-gate * distribution.
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
170Sstevel@tonic-gate * software must display the following acknowledgment:
180Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
190Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
200Sstevel@tonic-gate *
210Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
220Sstevel@tonic-gate * endorse or promote products derived from this software without
230Sstevel@tonic-gate * prior written permission. For written permission, please contact
240Sstevel@tonic-gate * openssl-core@openssl.org.
250Sstevel@tonic-gate *
260Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
270Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
280Sstevel@tonic-gate * permission of the OpenSSL Project.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
310Sstevel@tonic-gate * acknowledgment:
320Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
330Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
360Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
370Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
380Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
390Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
400Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
410Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
420Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
430Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
440Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
450Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
460Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
470Sstevel@tonic-gate * ====================================================================
480Sstevel@tonic-gate *
490Sstevel@tonic-gate */
500Sstevel@tonic-gate
51*2139Sjp161948 #include <openssl/opensslconf.h>
520Sstevel@tonic-gate #ifndef OPENSSL_NO_AES
530Sstevel@tonic-gate #include <openssl/evp.h>
540Sstevel@tonic-gate #include <openssl/err.h>
550Sstevel@tonic-gate #include <string.h>
56*2139Sjp161948 #include <assert.h>
570Sstevel@tonic-gate #include <openssl/aes.h>
580Sstevel@tonic-gate #include "evp_locl.h"
590Sstevel@tonic-gate
600Sstevel@tonic-gate static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
610Sstevel@tonic-gate const unsigned char *iv, int enc);
620Sstevel@tonic-gate
630Sstevel@tonic-gate typedef struct
640Sstevel@tonic-gate {
650Sstevel@tonic-gate AES_KEY ks;
660Sstevel@tonic-gate } EVP_AES_KEY;
670Sstevel@tonic-gate
680Sstevel@tonic-gate #define data(ctx) EVP_C_DATA(EVP_AES_KEY,ctx)
690Sstevel@tonic-gate
700Sstevel@tonic-gate IMPLEMENT_BLOCK_CIPHER(aes_128, ks, AES, EVP_AES_KEY,
710Sstevel@tonic-gate NID_aes_128, 16, 16, 16, 128,
720Sstevel@tonic-gate 0, aes_init_key, NULL,
730Sstevel@tonic-gate EVP_CIPHER_set_asn1_iv,
740Sstevel@tonic-gate EVP_CIPHER_get_asn1_iv,
750Sstevel@tonic-gate NULL)
760Sstevel@tonic-gate IMPLEMENT_BLOCK_CIPHER(aes_192, ks, AES, EVP_AES_KEY,
770Sstevel@tonic-gate NID_aes_192, 16, 24, 16, 128,
780Sstevel@tonic-gate 0, aes_init_key, NULL,
790Sstevel@tonic-gate EVP_CIPHER_set_asn1_iv,
800Sstevel@tonic-gate EVP_CIPHER_get_asn1_iv,
810Sstevel@tonic-gate NULL)
820Sstevel@tonic-gate IMPLEMENT_BLOCK_CIPHER(aes_256, ks, AES, EVP_AES_KEY,
830Sstevel@tonic-gate NID_aes_256, 16, 32, 16, 128,
840Sstevel@tonic-gate 0, aes_init_key, NULL,
850Sstevel@tonic-gate EVP_CIPHER_set_asn1_iv,
860Sstevel@tonic-gate EVP_CIPHER_get_asn1_iv,
870Sstevel@tonic-gate NULL)
880Sstevel@tonic-gate
89*2139Sjp161948 #define IMPLEMENT_AES_CFBR(ksize,cbits) IMPLEMENT_CFBR(aes,AES,EVP_AES_KEY,ks,ksize,cbits,16)
90*2139Sjp161948
91*2139Sjp161948 IMPLEMENT_AES_CFBR(128,1)
92*2139Sjp161948 IMPLEMENT_AES_CFBR(192,1)
93*2139Sjp161948 IMPLEMENT_AES_CFBR(256,1)
94*2139Sjp161948
95*2139Sjp161948 IMPLEMENT_AES_CFBR(128,8)
96*2139Sjp161948 IMPLEMENT_AES_CFBR(192,8)
97*2139Sjp161948 IMPLEMENT_AES_CFBR(256,8)
98*2139Sjp161948
aes_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)990Sstevel@tonic-gate static int aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
100*2139Sjp161948 const unsigned char *iv, int enc)
101*2139Sjp161948 {
102*2139Sjp161948 int ret;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if ((ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_CFB_MODE
1050Sstevel@tonic-gate || (ctx->cipher->flags & EVP_CIPH_MODE) == EVP_CIPH_OFB_MODE
1060Sstevel@tonic-gate || enc)
107*2139Sjp161948 ret=AES_set_encrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
1080Sstevel@tonic-gate else
109*2139Sjp161948 ret=AES_set_decrypt_key(key, ctx->key_len * 8, ctx->cipher_data);
110*2139Sjp161948
111*2139Sjp161948 if(ret < 0)
112*2139Sjp161948 {
113*2139Sjp161948 EVPerr(EVP_F_AES_INIT_KEY,EVP_R_AES_KEY_SETUP_FAILED);
114*2139Sjp161948 return 0;
115*2139Sjp161948 }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate return 1;
118*2139Sjp161948 }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate #endif
121