aes.c revision 1dcbfafd968b0b71d294f13dbacb0e4b44b044ca
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* AES provider for the Kernel Cryptographic Framework (KCF)
*/
#include <sys/sysmacros.h>
#include <aes/aes_impl.h>
extern struct mod_ops mod_cryptoops;
/*
* Module linkage information for the kernel.
*/
static struct modlcrypto modlcrypto = {
"AES Kernel SW Provider"
};
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 */
AES_CCM_MECH_INFO_TYPE, /* SUN_CKM_AES_CCM */
AES_GCM_MECH_INFO_TYPE /* SUN_CKM_AES_GCM */
/*
* The following definitions are to keep EXPORT_SRC happy.
*/
#ifndef AES_MIN_KEY_BYTES
#define AES_MIN_KEY_BYTES 0
#endif
#ifndef AES_MAX_KEY_BYTES
#define AES_MAX_KEY_BYTES 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 */
/* AES_CCM */
/* AES_GCM */
};
/* operations are in-place if the output buffer is NULL */
static crypto_control_ops_t aes_control_ops = {
};
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
{
void *p = NULL;
int rv = CRYPTO_SUCCESS;
case AES_ECB_MECH_INFO_TYPE:
/* no parameter */
p = ecb_alloc_ctx(kmflag);
break;
case AES_CBC_MECH_INFO_TYPE:
break;
}
p = cbc_alloc_ctx(kmflag);
break;
case AES_CTR_MECH_INFO_TYPE:
break;
}
p = ctr_alloc_ctx(kmflag);
break;
case AES_CCM_MECH_INFO_TYPE:
break;
}
p = ccm_alloc_ctx(kmflag);
break;
case AES_GCM_MECH_INFO_TYPE:
break;
}
p = gcm_alloc_ctx(kmflag);
break;
default:
}
*ctx = p;
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
{
}
static int
}
static int
}
/*
* 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);
}
!= CRYPTO_SUCCESS)
return (rv);
if (rv != CRYPTO_SUCCESS) {
return (rv);
}
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
static void
{
/* LINTED: pointer alignment */
/* LINTED: pointer alignment */
} else {
}
}
/* 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.
* Even though AES CCM mode is a block cipher, it does not
* require the plaintext to be a multiple of AES block size.
* The length requirement for AES CCM mode has already been checked
* at init time
*/
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.
*/
} else {
}
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do an update on the specified input data.
*/
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
/*
* For CCM mode, aes_ccm_encrypt_final() will take care of any
* left-over unprocessed data, and compute the MAC
*/
/*
* ccm_encrypt_final() will compute the MAC and append
* it to existing ciphertext. So, need to adjust the left over
* length value accordingly
*/
/* order of following 2 lines MUST not be reversed */
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
if (plaintext != ciphertext) {
}
/*
* gcm_encrypt_final() will compute the MAC and append
* it to existing ciphertext. So, need to adjust the left over
* length value accordingly
*/
/* order of following 2 lines MUST not be reversed */
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
if (plaintext != ciphertext) {
}
}
(void) aes_free_context(ctx);
/* EXPORT DELETE END */
/* LINTED */
return (ret);
}
/* 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.
* Even though AES CCM mode is a block cipher, it does not
* require the plaintext to be a multiple of AES block size.
* The length requirement for AES CCM mode has already been checked
* at init time
*/
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.
*
* For AES CCM mode, size of the plaintext will be MAC_SIZE
* smaller than size of the cipher text.
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
return (CRYPTO_BUFFER_TOO_SMALL);
}
return (CRYPTO_BUFFER_TOO_SMALL);
}
/*
* Do an update on the specified input data.
*/
if (ret != CRYPTO_SUCCESS) {
goto cleanup;
}
/* order of following 2 lines MUST not be reversed */
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext) {
}
} else {
}
/* order of following 2 lines MUST not be reversed */
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext) {
}
} else {
}
}
(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
* ctr_mode_final() to pick up any remaining bytes.
* It is an internal function that does not destroy
* the context like *normal* final routines.
*/
}
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.
* This is not necessary for CCM and GCM since these mechanisms
* never return plaintext for update operations.
*/
/* 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
* ctr_mode_final() to pick up any remaining bytes.
* It is an internal function that does not destroy
* the context like *normal* final routines.
*/
if (ret == CRYPTO_DATA_LEN_RANGE)
}
if (ret == CRYPTO_SUCCESS) {
if (ciphertext != plaintext)
} else {
}
return (ret);
}
/* ARGSUSED */
static int
{
/* EXPORT DELETE START */
int ret;
return (CRYPTO_ARGUMENTS_BAD);
}
if (aes_ctx->ac_remainder_len > 0) {
if (ret != CRYPTO_SUCCESS)
return (ret);
}
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
} else {
/*
* 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);
}
}
(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_DATA_LEN_RANGE)
if (ret != CRYPTO_SUCCESS)
return (ret);
}
}
/*
* This is where all the plaintext is returned, make sure
* the plaintext buffer is big enough
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
if (ret == CRYPTO_SUCCESS) {
} else {
}
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
/*
* This is where all the plaintext is returned, make sure
* the plaintext buffer is big enough
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
if (ret == CRYPTO_SUCCESS) {
} else {
}
if (ret != CRYPTO_SUCCESS) {
return (ret);
}
}
}
(void) aes_free_context(ctx);
/* EXPORT DELETE END */
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
int ret;
/*
* CTR, CCM, and GCM modes do not require that ciphertext
* be a multiple of AES block size.
*/
case AES_CTR_MECH_INFO_TYPE:
case AES_CCM_MECH_INFO_TYPE:
case AES_GCM_MECH_INFO_TYPE:
break;
default:
return (CRYPTO_DATA_LEN_RANGE);
}
return (ret);
if (ret != CRYPTO_SUCCESS)
return (ret);
case AES_CCM_MECH_INFO_TYPE:
break;
case AES_GCM_MECH_INFO_TYPE:
break;
default:
}
/* return size of buffer needed to store output */
goto out;
}
/*
* 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 (ret != CRYPTO_SUCCESS)
goto out;
if (ret != CRYPTO_SUCCESS)
goto out;
if (aes_ctx.ac_remainder_len > 0) {
if (ret != CRYPTO_SUCCESS)
goto out;
}
} else {
}
if (plaintext != ciphertext) {
}
} else {
}
out:
}
return (ret);
}
/* ARGSUSED */
static int
{
int ret;
/*
* CCM, GCM, and CTR modes do not require that ciphertext
* be a multiple of AES block size.
*/
case AES_CTR_MECH_INFO_TYPE:
case AES_CCM_MECH_INFO_TYPE:
case AES_GCM_MECH_INFO_TYPE:
break;
default:
return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
}
return (ret);
if (ret != CRYPTO_SUCCESS)
return (ret);
case AES_CCM_MECH_INFO_TYPE:
break;
case AES_GCM_MECH_INFO_TYPE:
break;
default:
}
/* return size of buffer needed to store output */
goto out;
}
/*
* 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) {
== aes_ctx.ac_data_len);
== aes_ctx.ac_mac_len);
if ((ret == CRYPTO_SUCCESS) &&
(ciphertext != plaintext)) {
} else {
}
if ((ret == CRYPTO_SUCCESS) &&
(ciphertext != plaintext)) {
} else {
}
if (ciphertext != plaintext)
} else {
if (aes_ctx.ac_remainder_len > 0) {
if (ret == CRYPTO_DATA_LEN_RANGE)
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;
return (CRYPTO_HOST_MEMORY);
/*
* Initialize key schedule.
* Key length is stored in the key.
*/
return (rv);
}
} else {
}
case AES_CBC_MECH_INFO_TYPE:
break;
case AES_CTR_MECH_INFO_TYPE: {
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
break;
}
case AES_CCM_MECH_INFO_TYPE:
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
break;
case AES_GCM_MECH_INFO_TYPE:
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
break;
case AES_ECB_MECH_INFO_TYPE:
}
if (rv != CRYPTO_SUCCESS) {
}
}
/* EXPORT DELETE END */
return (rv);
}