aes.c revision efcbd7a0389435d8e3218fef080cc88afeec61a3
/*
* 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 2007 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 */
AES_CCM_MECH_INFO_TYPE /* SUN_CKM_AES_CCM */
/*
* 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 */
};
/* 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
{
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;
case AES_CCM_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
{
}
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);
}
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.
* 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
*/
/*
* aes_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) {
}
}
(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);
}
/*
* Do an update on the specified input data.
*/
if (ret != CRYPTO_SUCCESS) {
goto cleanup;
}
== aes_ctx->ac_ccm_data_len);
== aes_ctx->ac_ccm_mac_len);
/* order of following 2 lines MUST not be reversed */
if (ret == CRYPTO_SUCCESS) {
if (plaintext != ciphertext) {
}
} else {
}
}
if (aes_ctx->ac_ccm_pt_buf) {
}
(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);
}
if (aes_ctx->ac_remainder_len > 0) {
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_SUCCESS)
return (ret);
}
}
/*
* This is where all the plaintext is returned, make sure
* the plaintext buffer is big enough
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
== aes_ctx->ac_ccm_mac_len);
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;
/*
* 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);
if (ret != CRYPTO_SUCCESS)
return (ret);
return (CRYPTO_BUFFER_TOO_SMALL);
}
}
/*
* 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 (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;
/*
* For block ciphers, ciphertext must be a multiple of AES block size.
* This test is only valid for non-padded mechanisms
* when blocksize is 2^N
* 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 will be checked
* at init time
*/
return (CRYPTO_DATA_LEN_RANGE);
/*
* return length needed to store the output, length requirement
* for AES CCM mode can not be determined until later
*/
return (CRYPTO_BUFFER_TOO_SMALL);
}
return (ret);
if (ret != CRYPTO_SUCCESS)
return (ret);
/* check length requirement for AES CCM mode now */
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_ccm_data_len);
== aes_ctx.ac_ccm_mac_len);
if ((ret == CRYPTO_SUCCESS) &&
(ciphertext != plaintext)) {
} else {
}
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)
#ifdef _LITTLE_ENDIAN
#endif
} else {
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
int rc;
is_encrypt_init)) != 0) {
return (rc);
}
if (is_encrypt_init) {
= ccm_param->ulDataSize;
} else {
}
} else {
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
} else {
}
return (CRYPTO_HOST_MEMORY);
/*
* Initialize key schedule.
* Key length is stored in the key.
*/
return (rv);
}
} else {
}
/* process the nonce and associated data if it is AES CCM mode */
ccm_param->ulAuthDataSize) != 0) {
return (CRYPTO_MECHANISM_PARAM_INVALID);
}
if (!is_encrypt_init) {
/* allocate buffer for storing decrypted plaintext */
return (CRYPTO_HOST_MEMORY);
}
}
}
/* EXPORT DELETE END */
return (rv);
}