aes.c revision ca5b6649a57edb4ee6f28de6362b5ba157494773
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* AES provider for the Kernel Cryptographic Framework (KCF)
*/
#include <sys/sysmacros.h>
#include <aes_impl.h>
#include <aes_cbc_crypt.h>
extern struct mod_ops mod_cryptoops;
/*
* Module linkage information for the kernel.
*/
static struct modlcrypto modlcrypto = {
"AES Kernel SW Provider %I%"
};
static struct modlinkage modlinkage = {
(void *)&modlcrypto,
};
/*
* CSPI information (entry points, provider info, etc.)
*/
typedef enum aes_mech_type {
AES_ECB_MECH_INFO_TYPE, /* SUN_CKM_AES_ECB */
AES_CBC_MECH_INFO_TYPE, /* SUN_CKM_AES_CBC */
AES_CBC_PAD_MECH_INFO_TYPE, /* SUN_CKM_AES_CBC_PAD */
AES_CTR_MECH_INFO_TYPE /* SUN_CKM_AES_CTR */
/*
* The following definitions are to keep EXPORT_SRC happy.
*/
#ifndef AES_MIN_KEY_LEN
#define AES_MIN_KEY_LEN 0
#endif
#ifndef AES_MAX_KEY_LEN
#define AES_MAX_KEY_LEN 0
#endif
/*
* Mechanism info structure passed to KCF during registration.
*/
static crypto_mech_info_t aes_mech_info_tab[] = {
/* AES_ECB */
/* AES_CBC */
/* AES_CTR */
};
/* operations are in-place if the output buffer is NULL */
static crypto_control_ops_t aes_control_ops = {
};
crypto_mechanism_t *, crypto_key_t *, int);
static crypto_cipher_ops_t aes_cipher_ops = {
};
static int aes_create_ctx_template(crypto_provider_handle_t,
static int aes_free_context(crypto_ctx_t *);
static crypto_ctx_ops_t aes_ctx_ops = {
};
static crypto_ops_t aes_crypto_ops = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
static crypto_provider_info_t aes_prov_info = {
"AES Software Provider",
{&modlinkage},
NULL,
sizeof (aes_mech_info_tab)/sizeof (crypto_mech_info_t),
};
int
_init(void)
{
int ret;
/*
* Register with KCF. If the registration fails, return error.
*/
&aes_prov_handle)) != CRYPTO_SUCCESS) {
return (EACCES);
}
int rv;
/* We should not return if the unregister returns busy. */
== CRYPTO_BUSY) {
"%s _init: crypto_unregister_provider() "
"failed (0x%x). Retrying.",
/* wait 10 seconds and try again. */
}
}
return (ret);
}
int
_fini(void)
{
int ret;
/*
* Unregister from KCF if previous registration succeeded.
*/
if (aes_prov_handle != NULL) {
"%s _fini: crypto_unregister_provider() "
return (EBUSY);
}
}
return (mod_remove(&modlinkage));
}
int
{
}
static int
{
int rv = CRYPTO_SUCCESS;
case AES_ECB_MECH_INFO_TYPE:
/* no parameter */
break;
case AES_CBC_MECH_INFO_TYPE:
break;
case AES_CTR_MECH_INFO_TYPE:
break;
default:
}
return (rv);
}
/* EXPORT DELETE START */
/*
* Initialize key schedules for AES
*/
static int
{
/*
* Only keys by value are supported by this module.
*/
case CRYPTO_KEY_RAW:
return (CRYPTO_KEY_SIZE_RANGE);
}
/* key length must be either 128, 192, or 256 */
return (CRYPTO_KEY_SIZE_RANGE);
break;
default:
return (CRYPTO_KEY_TYPE_INCONSISTENT);
}
return (CRYPTO_SUCCESS);
}
/* EXPORT DELETE END */
/*
* KCF software provider control entry points.
*/
/* ARGSUSED */
static void
{
}
/*
* KCF software provider encrypt entry points.
*/
static int
{
/* EXPORT DELETE START */
int rv;
int kmflag;
/*
* Only keys by value are supported by this module.
*/
return (CRYPTO_KEY_TYPE_INCONSISTENT);
}
return (rv);
/*
* Allocate an AES context.
*/
return (CRYPTO_HOST_MEMORY);
if (rv != CRYPTO_SUCCESS) {
return (rv);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/*
* Helper AES encrypt update function for iov input data.
*/
static int
crypto_data_t *))
{
int rv;
/* EXPORT DELETE START */
/* LINTED: pointer alignment */
/* LINTED: pointer alignment */
} else {
}
}
return (CRYPTO_ARGUMENTS_BAD);
/* EXPORT DELETE END */
return (rv);
}
/*
* Helper AES encrypt update function for uio input data.
*/
static int
crypto_data_t *))
{
/* EXPORT DELETE START */
/* LINTED: pointer alignment */
/* LINTED: pointer alignment */
} else {
}
}
return (CRYPTO_ARGUMENTS_BAD);
}
/*
* Jump to the first iovec containing data to be
* processed.
*/
/*
* The caller specified an offset that is larger than the
* total size of the buffers it provided.
*/
return (CRYPTO_DATA_LEN_RANGE);
}
/*
* Now process the iovecs.
*/
vec_idx++;
offset = 0;
}
/*
* The end of the specified iovec's was reached but
* the length requested could not be processed, i.e.
* The caller requested to digest more data than it provided.
*/
return (CRYPTO_DATA_LEN_RANGE);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/*
* Helper AES encrypt update function for mblk input data.
*/
static int
crypto_data_t *))
{
/* EXPORT DELETE START */
/* LINTED: pointer alignment */
/* LINTED: pointer alignment */
} else {
}
}
/*
* Jump to the first mblk_t containing data to be processed.
*/
/*
* The caller specified an offset that is larger than the
* total size of the buffers it provided.
*/
return (CRYPTO_DATA_LEN_RANGE);
}
/*
* Now do the processing on the mblk chain.
*/
offset = 0;
}
/*
* The end of the mblk was reached but the length requested
* could not be processed, i.e. The caller requested
* to digest more data than it provided.
*/
return (CRYPTO_DATA_LEN_RANGE);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
int ret = CRYPTO_FAILED;
/* EXPORT DELETE START */
/*
* For block ciphers, plaintext must be a multiple of AES block size.
* This test is only valid for ciphers whose blocksize is a power of 2.
*/
return (CRYPTO_DATA_LEN_RANGE);
/*
* We need to just return the length needed to store the output.
* We should not destroy the context for the following case.
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do an update on the specified input data.
*/
(void) aes_free_context(ctx);
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/* ARGSUSED */
static int
{
int ret = CRYPTO_FAILED;
/* EXPORT DELETE START */
/*
* For block ciphers, ciphertext must be a multiple of AES block size.
* This test is only valid for ciphers whose blocksize is a power of 2.
*/
return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
/*
* We need to just return the length needed to store the output.
* We should not destroy the context for the following case.
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do an update on the specified input data.
*/
(void) aes_free_context(ctx);
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/* ARGSUSED */
static int
{
int ret = CRYPTO_SUCCESS;
/* compute number of bytes that will hold the ciphertext */
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do the AES update on the specified input data.
*/
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
/*
* Since AES counter mode is a stream cipher, we call
* aes_counter_final() to pick up any remaining bytes.
* It is an internal function that does not destroy
* the context like *normal* final routines.
*/
(aes_ctx->ac_remainder_len > 0)) {
}
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext)
} else {
}
return (ret);
}
/* ARGSUSED */
static int
{
int ret = CRYPTO_SUCCESS;
/* compute number of bytes that will hold the plaintext */
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do the AES update on the specified input data.
*/
switch (ciphertext->cd_format) {
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
/*
* Since AES counter mode is a stream cipher, we call
* aes_counter_final() to pick up any remaining bytes.
* It is an internal function that does not destroy
* the context like *normal* final routines.
*/
(aes_ctx->ac_remainder_len > 0)) {
}
if (ret == CRYPTO_SUCCESS) {
if (ciphertext != plaintext)
} else {
}
return (ret);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
int ret;
return (CRYPTO_ARGUMENTS_BAD);
}
/*
* There must be no unprocessed plaintext.
* This happens if the length of the last data is
* not a multiple of the AES block length.
*/
if (aes_ctx->ac_remainder_len > 0) {
return (CRYPTO_DATA_LEN_RANGE);
else {
if (ret != CRYPTO_SUCCESS)
return (ret);
}
}
(void) aes_free_context(ctx);
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
int ret;
return (CRYPTO_ARGUMENTS_BAD);
}
/*
* There must be no unprocessed ciphertext.
* This happens if the length of the last ciphertext is
* not a multiple of the AES block length.
*/
if (aes_ctx->ac_remainder_len > 0) {
return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
else {
if (ret != CRYPTO_SUCCESS)
return (ret);
}
}
(void) aes_free_context(ctx);
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
int ret;
/*
* Plaintext must be a multiple of AES block size.
* This test only works for non-padded mechanisms
* when blocksize is 2^N.
*/
return (CRYPTO_DATA_LEN_RANGE);
}
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
return (ret);
crypto_kmflag(req));
if (ret != CRYPTO_SUCCESS)
return (ret);
/*
* Do an update on the specified input data.
*/
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext)
} else {
if (aes_ctx.ac_remainder_len > 0) {
if (ret != CRYPTO_SUCCESS)
goto out;
}
if (plaintext != ciphertext)
}
} else {
}
out:
}
return (ret);
}
/* ARGSUSED */
static int
{
int ret;
/*
* Ciphertext must be a multiple of AES block size.
* This test only works for non-padded mechanisms
* when blocksize is 2^N.
*/
return (CRYPTO_DATA_LEN_RANGE);
}
/* return length needed to store the output */
return (CRYPTO_BUFFER_TOO_SMALL);
}
return (ret);
crypto_kmflag(req));
if (ret != CRYPTO_SUCCESS)
return (ret);
/*
* Do an update on the specified input data.
*/
switch (ciphertext->cd_format) {
case CRYPTO_DATA_RAW:
break;
case CRYPTO_DATA_UIO:
break;
case CRYPTO_DATA_MBLK:
break;
default:
}
if (ret == CRYPTO_SUCCESS) {
if (ciphertext != plaintext)
} else {
if (aes_ctx.ac_remainder_len > 0) {
if (ret != CRYPTO_SUCCESS)
goto out;
}
if (ciphertext != plaintext)
}
} else {
}
out:
}
return (ret);
}
/*
* KCF software provider context template entry points.
*/
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
void *keysched;
int rv;
return (CRYPTO_MECHANISM_INVALID);
return (CRYPTO_HOST_MEMORY);
}
/*
* Initialize key schedule. Key length information is stored
* in the key.
*/
return (rv);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
}
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
int rv = CRYPTO_SUCCESS;
/* EXPORT DELETE START */
void *keysched;
/*
* Copy 128-bit IV into context.
*
* If cm_param == NULL then the IV comes from the
* cd_miscdata field in the crypto_data structure.
*/
sizeof (uint64_t))) {
} else {
}
}
/* XXX what to do about miscdata */
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
while (count-- > 0)
} else {
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
} else {
}
return (CRYPTO_HOST_MEMORY);
/*
* Initialize key schedule.
* Key length is stored in the key.
*/
} else {
}
/* EXPORT DELETE END */
return (rv);
}