2016-05-12 14:51:24 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2022-07-11 21:16:13 +00:00
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
2016-05-12 14:51:24 +00:00
|
|
|
* 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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AES provider for the Kernel Cryptographic Framework (KCF)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/zfs_context.h>
|
|
|
|
#include <sys/crypto/common.h>
|
|
|
|
#include <sys/crypto/impl.h>
|
|
|
|
#include <sys/crypto/spi.h>
|
|
|
|
#include <sys/crypto/icp.h>
|
|
|
|
#include <modes/modes.h>
|
|
|
|
#define _AES_IMPL
|
|
|
|
#include <aes/aes_impl.h>
|
2018-08-02 18:59:24 +00:00
|
|
|
#include <modes/gcm_impl.h>
|
2016-05-12 14:51:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mechanism info structure passed to KCF during registration.
|
|
|
|
*/
|
2022-01-14 23:37:55 +00:00
|
|
|
static const crypto_mech_info_t aes_mech_info_tab[] = {
|
2016-05-12 14:51:24 +00:00
|
|
|
/* AES_CCM */
|
|
|
|
{SUN_CKM_AES_CCM, AES_CCM_MECH_INFO_TYPE,
|
2024-05-18 12:17:36 +00:00
|
|
|
CRYPTO_FG_ENCRYPT_ATOMIC | CRYPTO_FG_DECRYPT_ATOMIC},
|
2016-05-12 14:51:24 +00:00
|
|
|
/* AES_GCM */
|
|
|
|
{SUN_CKM_AES_GCM, AES_GCM_MECH_INFO_TYPE,
|
2024-05-18 12:17:36 +00:00
|
|
|
CRYPTO_FG_ENCRYPT_ATOMIC | CRYPTO_FG_DECRYPT_ATOMIC},
|
2016-05-12 14:51:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int aes_common_init_ctx(aes_ctx_t *, crypto_spi_ctx_template_t *,
|
|
|
|
crypto_mechanism_t *, crypto_key_t *, int, boolean_t);
|
|
|
|
|
2021-12-27 01:53:32 +00:00
|
|
|
static int aes_encrypt_atomic(crypto_mechanism_t *, crypto_key_t *,
|
|
|
|
crypto_data_t *, crypto_data_t *, crypto_spi_ctx_template_t);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2021-12-27 01:53:32 +00:00
|
|
|
static int aes_decrypt_atomic(crypto_mechanism_t *, crypto_key_t *,
|
|
|
|
crypto_data_t *, crypto_data_t *, crypto_spi_ctx_template_t);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2022-01-14 23:37:55 +00:00
|
|
|
static const crypto_cipher_ops_t aes_cipher_ops = {
|
2017-11-28 23:33:48 +00:00
|
|
|
.encrypt_atomic = aes_encrypt_atomic,
|
|
|
|
.decrypt_atomic = aes_decrypt_atomic
|
2016-05-12 14:51:24 +00:00
|
|
|
};
|
|
|
|
|
2021-12-27 01:32:37 +00:00
|
|
|
static int aes_create_ctx_template(crypto_mechanism_t *, crypto_key_t *,
|
|
|
|
crypto_spi_ctx_template_t *, size_t *);
|
2016-05-12 14:51:24 +00:00
|
|
|
static int aes_free_context(crypto_ctx_t *);
|
|
|
|
|
2022-01-14 23:37:55 +00:00
|
|
|
static const crypto_ctx_ops_t aes_ctx_ops = {
|
2017-11-28 23:33:48 +00:00
|
|
|
.create_ctx_template = aes_create_ctx_template,
|
|
|
|
.free_context = aes_free_context
|
2016-05-12 14:51:24 +00:00
|
|
|
};
|
|
|
|
|
2021-12-22 21:09:28 +00:00
|
|
|
static const crypto_ops_t aes_crypto_ops = {
|
2016-05-12 14:51:24 +00:00
|
|
|
&aes_cipher_ops,
|
2024-05-18 11:57:36 +00:00
|
|
|
NULL,
|
2021-12-22 22:29:25 +00:00
|
|
|
&aes_ctx_ops,
|
2021-12-22 21:09:28 +00:00
|
|
|
};
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2021-12-22 21:09:28 +00:00
|
|
|
static const crypto_provider_info_t aes_prov_info = {
|
2016-05-12 14:51:24 +00:00
|
|
|
"AES Software Provider",
|
|
|
|
&aes_crypto_ops,
|
2022-01-14 23:37:55 +00:00
|
|
|
sizeof (aes_mech_info_tab) / sizeof (crypto_mech_info_t),
|
2016-05-12 14:51:24 +00:00
|
|
|
aes_mech_info_tab
|
2021-12-22 21:09:28 +00:00
|
|
|
};
|
2016-05-12 14:51:24 +00:00
|
|
|
|
|
|
|
static crypto_kcf_provider_handle_t aes_prov_handle = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
aes_mod_init(void)
|
|
|
|
{
|
2019-10-24 17:17:33 +00:00
|
|
|
/* Determine the fastest available implementation. */
|
|
|
|
aes_impl_init();
|
|
|
|
gcm_impl_init();
|
2018-08-02 18:59:24 +00:00
|
|
|
|
2016-05-12 14:51:24 +00:00
|
|
|
/* Register with KCF. If the registration fails, remove the module. */
|
2021-12-22 14:27:43 +00:00
|
|
|
if (crypto_register_provider(&aes_prov_info, &aes_prov_handle))
|
2016-05-12 14:51:24 +00:00
|
|
|
return (EACCES);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
aes_mod_fini(void)
|
|
|
|
{
|
|
|
|
/* Unregister from KCF if module is registered */
|
|
|
|
if (aes_prov_handle != 0) {
|
|
|
|
if (crypto_unregister_provider(aes_prov_handle))
|
|
|
|
return (EBUSY);
|
|
|
|
|
|
|
|
aes_prov_handle = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 14:27:43 +00:00
|
|
|
return (0);
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-25 03:34:29 +00:00
|
|
|
aes_check_mech_param(crypto_mechanism_t *mechanism, aes_ctx_t **ctx)
|
2016-05-12 14:51:24 +00:00
|
|
|
{
|
|
|
|
void *p = NULL;
|
|
|
|
boolean_t param_required = B_TRUE;
|
|
|
|
size_t param_len;
|
|
|
|
void *(*alloc_fun)(int);
|
|
|
|
int rv = CRYPTO_SUCCESS;
|
|
|
|
|
|
|
|
switch (mechanism->cm_type) {
|
|
|
|
case AES_CCM_MECH_INFO_TYPE:
|
|
|
|
param_len = sizeof (CK_AES_CCM_PARAMS);
|
|
|
|
alloc_fun = ccm_alloc_ctx;
|
|
|
|
break;
|
|
|
|
case AES_GCM_MECH_INFO_TYPE:
|
|
|
|
param_len = sizeof (CK_AES_GCM_PARAMS);
|
|
|
|
alloc_fun = gcm_alloc_ctx;
|
|
|
|
break;
|
|
|
|
default:
|
2024-05-18 11:57:36 +00:00
|
|
|
__builtin_unreachable();
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
if (param_required && mechanism->cm_param != NULL &&
|
|
|
|
mechanism->cm_param_len != param_len) {
|
|
|
|
rv = CRYPTO_MECHANISM_PARAM_INVALID;
|
|
|
|
}
|
|
|
|
if (ctx != NULL) {
|
2021-12-25 03:34:29 +00:00
|
|
|
p = (alloc_fun)(KM_SLEEP);
|
2016-05-12 14:51:24 +00:00
|
|
|
*ctx = p;
|
|
|
|
}
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize key schedules for AES
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
init_keysched(crypto_key_t *key, void *newbie)
|
|
|
|
{
|
2021-12-25 02:23:07 +00:00
|
|
|
if (key->ck_length < AES_MINBITS ||
|
|
|
|
key->ck_length > AES_MAXBITS) {
|
|
|
|
return (CRYPTO_KEY_SIZE_RANGE);
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 02:23:07 +00:00
|
|
|
/* key length must be either 128, 192, or 256 */
|
|
|
|
if ((key->ck_length & 63) != 0)
|
|
|
|
return (CRYPTO_KEY_SIZE_RANGE);
|
|
|
|
|
2016-05-12 14:51:24 +00:00
|
|
|
aes_init_keysched(key->ck_data, key->ck_length, newbie);
|
|
|
|
return (CRYPTO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* KCF software provider encrypt entry points.
|
|
|
|
*/
|
|
|
|
static int
|
2021-12-27 01:53:32 +00:00
|
|
|
aes_encrypt_atomic(crypto_mechanism_t *mechanism,
|
2016-05-12 14:51:24 +00:00
|
|
|
crypto_key_t *key, crypto_data_t *plaintext, crypto_data_t *ciphertext,
|
2021-12-25 03:34:29 +00:00
|
|
|
crypto_spi_ctx_template_t template)
|
2016-05-12 14:51:24 +00:00
|
|
|
{
|
Use memset to zero stack allocations containing unions
C99 6.7.8.17 says that when an undesignated initialiser is used, only
the first element of a union is initialised. If the first element is not
the largest within the union, how the remaining space is initialised is
up to the compiler.
GCC extends the initialiser to the entire union, while Clang treats the
remainder as padding, and so initialises according to whatever
automatic/implicit initialisation rules are currently active.
When Linux is compiled with CONFIG_INIT_STACK_ALL_PATTERN,
-ftrivial-auto-var-init=pattern is added to the kernel CFLAGS. This flag
sets the policy for automatic/implicit initialisation of variables on
the stack.
Taken together, this means that when compiling under
CONFIG_INIT_STACK_ALL_PATTERN on Clang, the "zero" initialiser will only
zero the first element in a union, and the rest will be filled with a
pattern. This is significant for aes_ctx_t, which in
aes_encrypt_atomic() and aes_decrypt_atomic() is initialised to zero,
but then used as a gcm_ctx_t, which is the fifth element in the union,
and thus gets pattern initialisation. Later, it's assumed to be zero,
resulting in a hang.
As confusing and undiscoverable as it is, by the spec, we are at fault
when we initialise a structure containing a union with the zero
initializer. As such, this commit replaces these uses with an explicit
memset(0).
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
Closes #16135
Closes #16206
2024-05-25 02:00:29 +00:00
|
|
|
aes_ctx_t aes_ctx;
|
2016-05-12 14:51:24 +00:00
|
|
|
off_t saved_offset;
|
|
|
|
size_t saved_length;
|
|
|
|
size_t length_needed;
|
|
|
|
int ret;
|
|
|
|
|
Use memset to zero stack allocations containing unions
C99 6.7.8.17 says that when an undesignated initialiser is used, only
the first element of a union is initialised. If the first element is not
the largest within the union, how the remaining space is initialised is
up to the compiler.
GCC extends the initialiser to the entire union, while Clang treats the
remainder as padding, and so initialises according to whatever
automatic/implicit initialisation rules are currently active.
When Linux is compiled with CONFIG_INIT_STACK_ALL_PATTERN,
-ftrivial-auto-var-init=pattern is added to the kernel CFLAGS. This flag
sets the policy for automatic/implicit initialisation of variables on
the stack.
Taken together, this means that when compiling under
CONFIG_INIT_STACK_ALL_PATTERN on Clang, the "zero" initialiser will only
zero the first element in a union, and the rest will be filled with a
pattern. This is significant for aes_ctx_t, which in
aes_encrypt_atomic() and aes_decrypt_atomic() is initialised to zero,
but then used as a gcm_ctx_t, which is the fifth element in the union,
and thus gets pattern initialisation. Later, it's assumed to be zero,
resulting in a hang.
As confusing and undiscoverable as it is, by the spec, we are at fault
when we initialise a structure containing a union with the zero
initializer. As such, this commit replaces these uses with an explicit
memset(0).
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
Closes #16135
Closes #16206
2024-05-25 02:00:29 +00:00
|
|
|
memset(&aes_ctx, 0, sizeof (aes_ctx_t));
|
|
|
|
|
2020-03-26 17:41:57 +00:00
|
|
|
ASSERT(ciphertext != NULL);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2021-12-25 03:34:29 +00:00
|
|
|
if ((ret = aes_check_mech_param(mechanism, NULL)) != CRYPTO_SUCCESS)
|
2016-05-12 14:51:24 +00:00
|
|
|
return (ret);
|
|
|
|
|
|
|
|
ret = aes_common_init_ctx(&aes_ctx, template, mechanism, key,
|
2021-12-25 03:34:29 +00:00
|
|
|
KM_SLEEP, B_TRUE);
|
2016-05-12 14:51:24 +00:00
|
|
|
if (ret != CRYPTO_SUCCESS)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
switch (mechanism->cm_type) {
|
|
|
|
case AES_CCM_MECH_INFO_TYPE:
|
|
|
|
length_needed = plaintext->cd_length + aes_ctx.ac_mac_len;
|
|
|
|
break;
|
|
|
|
case AES_GCM_MECH_INFO_TYPE:
|
|
|
|
length_needed = plaintext->cd_length + aes_ctx.ac_tag_len;
|
|
|
|
break;
|
|
|
|
default:
|
2024-05-18 11:57:36 +00:00
|
|
|
__builtin_unreachable();
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return size of buffer needed to store output */
|
|
|
|
if (ciphertext->cd_length < length_needed) {
|
|
|
|
ciphertext->cd_length = length_needed;
|
|
|
|
ret = CRYPTO_BUFFER_TOO_SMALL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_offset = ciphertext->cd_offset;
|
|
|
|
saved_length = ciphertext->cd_length;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do an update on the specified input data.
|
|
|
|
*/
|
|
|
|
switch (plaintext->cd_format) {
|
|
|
|
case CRYPTO_DATA_RAW:
|
|
|
|
ret = crypto_update_iov(&aes_ctx, plaintext, ciphertext,
|
2021-12-25 02:33:19 +00:00
|
|
|
aes_encrypt_contiguous_blocks);
|
2016-05-12 14:51:24 +00:00
|
|
|
break;
|
|
|
|
case CRYPTO_DATA_UIO:
|
|
|
|
ret = crypto_update_uio(&aes_ctx, plaintext, ciphertext,
|
2021-12-25 02:33:19 +00:00
|
|
|
aes_encrypt_contiguous_blocks);
|
2016-05-12 14:51:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = CRYPTO_ARGUMENTS_BAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == CRYPTO_SUCCESS) {
|
|
|
|
if (mechanism->cm_type == AES_CCM_MECH_INFO_TYPE) {
|
|
|
|
ret = ccm_encrypt_final((ccm_ctx_t *)&aes_ctx,
|
|
|
|
ciphertext, AES_BLOCK_LEN, aes_encrypt_block,
|
|
|
|
aes_xor_block);
|
|
|
|
if (ret != CRYPTO_SUCCESS)
|
|
|
|
goto out;
|
|
|
|
ASSERT(aes_ctx.ac_remainder_len == 0);
|
2024-05-18 11:57:36 +00:00
|
|
|
} else if (mechanism->cm_type == AES_GCM_MECH_INFO_TYPE) {
|
2016-05-12 14:51:24 +00:00
|
|
|
ret = gcm_encrypt_final((gcm_ctx_t *)&aes_ctx,
|
|
|
|
ciphertext, AES_BLOCK_LEN, aes_encrypt_block,
|
|
|
|
aes_copy_block, aes_xor_block);
|
|
|
|
if (ret != CRYPTO_SUCCESS)
|
|
|
|
goto out;
|
|
|
|
ASSERT(aes_ctx.ac_remainder_len == 0);
|
|
|
|
} else {
|
|
|
|
ASSERT(aes_ctx.ac_remainder_len == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plaintext != ciphertext) {
|
|
|
|
ciphertext->cd_length =
|
|
|
|
ciphertext->cd_offset - saved_offset;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ciphertext->cd_length = saved_length;
|
|
|
|
}
|
|
|
|
ciphertext->cd_offset = saved_offset;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (aes_ctx.ac_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
|
2022-02-25 13:26:54 +00:00
|
|
|
memset(aes_ctx.ac_keysched, 0, aes_ctx.ac_keysched_len);
|
2016-05-12 14:51:24 +00:00
|
|
|
kmem_free(aes_ctx.ac_keysched, aes_ctx.ac_keysched_len);
|
|
|
|
}
|
2024-05-18 11:57:36 +00:00
|
|
|
if (aes_ctx.ac_flags & GCM_MODE) {
|
2023-02-27 22:38:12 +00:00
|
|
|
gcm_clear_ctx((gcm_ctx_t *)&aes_ctx);
|
2020-10-30 22:24:21 +00:00
|
|
|
}
|
2016-05-12 14:51:24 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-12-27 01:53:32 +00:00
|
|
|
aes_decrypt_atomic(crypto_mechanism_t *mechanism,
|
2016-05-12 14:51:24 +00:00
|
|
|
crypto_key_t *key, crypto_data_t *ciphertext, crypto_data_t *plaintext,
|
2021-12-25 03:34:29 +00:00
|
|
|
crypto_spi_ctx_template_t template)
|
2016-05-12 14:51:24 +00:00
|
|
|
{
|
Use memset to zero stack allocations containing unions
C99 6.7.8.17 says that when an undesignated initialiser is used, only
the first element of a union is initialised. If the first element is not
the largest within the union, how the remaining space is initialised is
up to the compiler.
GCC extends the initialiser to the entire union, while Clang treats the
remainder as padding, and so initialises according to whatever
automatic/implicit initialisation rules are currently active.
When Linux is compiled with CONFIG_INIT_STACK_ALL_PATTERN,
-ftrivial-auto-var-init=pattern is added to the kernel CFLAGS. This flag
sets the policy for automatic/implicit initialisation of variables on
the stack.
Taken together, this means that when compiling under
CONFIG_INIT_STACK_ALL_PATTERN on Clang, the "zero" initialiser will only
zero the first element in a union, and the rest will be filled with a
pattern. This is significant for aes_ctx_t, which in
aes_encrypt_atomic() and aes_decrypt_atomic() is initialised to zero,
but then used as a gcm_ctx_t, which is the fifth element in the union,
and thus gets pattern initialisation. Later, it's assumed to be zero,
resulting in a hang.
As confusing and undiscoverable as it is, by the spec, we are at fault
when we initialise a structure containing a union with the zero
initializer. As such, this commit replaces these uses with an explicit
memset(0).
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
Closes #16135
Closes #16206
2024-05-25 02:00:29 +00:00
|
|
|
aes_ctx_t aes_ctx;
|
2016-05-12 14:51:24 +00:00
|
|
|
off_t saved_offset;
|
|
|
|
size_t saved_length;
|
|
|
|
size_t length_needed;
|
|
|
|
int ret;
|
|
|
|
|
Use memset to zero stack allocations containing unions
C99 6.7.8.17 says that when an undesignated initialiser is used, only
the first element of a union is initialised. If the first element is not
the largest within the union, how the remaining space is initialised is
up to the compiler.
GCC extends the initialiser to the entire union, while Clang treats the
remainder as padding, and so initialises according to whatever
automatic/implicit initialisation rules are currently active.
When Linux is compiled with CONFIG_INIT_STACK_ALL_PATTERN,
-ftrivial-auto-var-init=pattern is added to the kernel CFLAGS. This flag
sets the policy for automatic/implicit initialisation of variables on
the stack.
Taken together, this means that when compiling under
CONFIG_INIT_STACK_ALL_PATTERN on Clang, the "zero" initialiser will only
zero the first element in a union, and the rest will be filled with a
pattern. This is significant for aes_ctx_t, which in
aes_encrypt_atomic() and aes_decrypt_atomic() is initialised to zero,
but then used as a gcm_ctx_t, which is the fifth element in the union,
and thus gets pattern initialisation. Later, it's assumed to be zero,
resulting in a hang.
As confusing and undiscoverable as it is, by the spec, we are at fault
when we initialise a structure containing a union with the zero
initializer. As such, this commit replaces these uses with an explicit
memset(0).
Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Reviewed-by: Tino Reichardt <milky-zfs@mcmilk.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
Closes #16135
Closes #16206
2024-05-25 02:00:29 +00:00
|
|
|
memset(&aes_ctx, 0, sizeof (aes_ctx_t));
|
|
|
|
|
2020-03-26 17:41:57 +00:00
|
|
|
ASSERT(plaintext != NULL);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2021-12-25 03:34:29 +00:00
|
|
|
if ((ret = aes_check_mech_param(mechanism, NULL)) != CRYPTO_SUCCESS)
|
2016-05-12 14:51:24 +00:00
|
|
|
return (ret);
|
|
|
|
|
|
|
|
ret = aes_common_init_ctx(&aes_ctx, template, mechanism, key,
|
2021-12-25 03:34:29 +00:00
|
|
|
KM_SLEEP, B_FALSE);
|
2016-05-12 14:51:24 +00:00
|
|
|
if (ret != CRYPTO_SUCCESS)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
switch (mechanism->cm_type) {
|
|
|
|
case AES_CCM_MECH_INFO_TYPE:
|
|
|
|
length_needed = aes_ctx.ac_data_len;
|
|
|
|
break;
|
|
|
|
case AES_GCM_MECH_INFO_TYPE:
|
|
|
|
length_needed = ciphertext->cd_length - aes_ctx.ac_tag_len;
|
|
|
|
break;
|
|
|
|
default:
|
2024-05-18 11:57:36 +00:00
|
|
|
__builtin_unreachable();
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return size of buffer needed to store output */
|
|
|
|
if (plaintext->cd_length < length_needed) {
|
|
|
|
plaintext->cd_length = length_needed;
|
|
|
|
ret = CRYPTO_BUFFER_TOO_SMALL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_offset = plaintext->cd_offset;
|
|
|
|
saved_length = plaintext->cd_length;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do an update on the specified input data.
|
|
|
|
*/
|
|
|
|
switch (ciphertext->cd_format) {
|
|
|
|
case CRYPTO_DATA_RAW:
|
|
|
|
ret = crypto_update_iov(&aes_ctx, ciphertext, plaintext,
|
2021-12-25 02:33:19 +00:00
|
|
|
aes_decrypt_contiguous_blocks);
|
2016-05-12 14:51:24 +00:00
|
|
|
break;
|
|
|
|
case CRYPTO_DATA_UIO:
|
|
|
|
ret = crypto_update_uio(&aes_ctx, ciphertext, plaintext,
|
2021-12-25 02:33:19 +00:00
|
|
|
aes_decrypt_contiguous_blocks);
|
2016-05-12 14:51:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = CRYPTO_ARGUMENTS_BAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == CRYPTO_SUCCESS) {
|
|
|
|
if (mechanism->cm_type == AES_CCM_MECH_INFO_TYPE) {
|
|
|
|
ASSERT(aes_ctx.ac_processed_data_len
|
|
|
|
== aes_ctx.ac_data_len);
|
|
|
|
ASSERT(aes_ctx.ac_processed_mac_len
|
|
|
|
== aes_ctx.ac_mac_len);
|
|
|
|
ret = ccm_decrypt_final((ccm_ctx_t *)&aes_ctx,
|
|
|
|
plaintext, AES_BLOCK_LEN, aes_encrypt_block,
|
|
|
|
aes_copy_block, aes_xor_block);
|
|
|
|
ASSERT(aes_ctx.ac_remainder_len == 0);
|
|
|
|
if ((ret == CRYPTO_SUCCESS) &&
|
|
|
|
(ciphertext != plaintext)) {
|
|
|
|
plaintext->cd_length =
|
|
|
|
plaintext->cd_offset - saved_offset;
|
|
|
|
} else {
|
|
|
|
plaintext->cd_length = saved_length;
|
|
|
|
}
|
2024-05-18 11:57:36 +00:00
|
|
|
} else if (mechanism->cm_type == AES_GCM_MECH_INFO_TYPE) {
|
2016-05-12 14:51:24 +00:00
|
|
|
ret = gcm_decrypt_final((gcm_ctx_t *)&aes_ctx,
|
|
|
|
plaintext, AES_BLOCK_LEN, aes_encrypt_block,
|
|
|
|
aes_xor_block);
|
|
|
|
ASSERT(aes_ctx.ac_remainder_len == 0);
|
|
|
|
if ((ret == CRYPTO_SUCCESS) &&
|
|
|
|
(ciphertext != plaintext)) {
|
|
|
|
plaintext->cd_length =
|
|
|
|
plaintext->cd_offset - saved_offset;
|
|
|
|
} else {
|
|
|
|
plaintext->cd_length = saved_length;
|
|
|
|
}
|
2024-05-18 11:57:36 +00:00
|
|
|
} else
|
|
|
|
__builtin_unreachable();
|
2016-05-12 14:51:24 +00:00
|
|
|
} else {
|
|
|
|
plaintext->cd_length = saved_length;
|
|
|
|
}
|
|
|
|
plaintext->cd_offset = saved_offset;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (aes_ctx.ac_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
|
2022-02-25 13:26:54 +00:00
|
|
|
memset(aes_ctx.ac_keysched, 0, aes_ctx.ac_keysched_len);
|
2016-05-12 14:51:24 +00:00
|
|
|
kmem_free(aes_ctx.ac_keysched, aes_ctx.ac_keysched_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aes_ctx.ac_flags & CCM_MODE) {
|
|
|
|
if (aes_ctx.ac_pt_buf != NULL) {
|
|
|
|
vmem_free(aes_ctx.ac_pt_buf, aes_ctx.ac_data_len);
|
|
|
|
}
|
2024-05-18 11:57:36 +00:00
|
|
|
} else if (aes_ctx.ac_flags & GCM_MODE) {
|
2023-02-27 22:38:12 +00:00
|
|
|
gcm_clear_ctx((gcm_ctx_t *)&aes_ctx);
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* KCF software provider context template entry points.
|
|
|
|
*/
|
|
|
|
static int
|
2021-12-27 01:32:37 +00:00
|
|
|
aes_create_ctx_template(crypto_mechanism_t *mechanism, crypto_key_t *key,
|
2021-12-25 03:34:29 +00:00
|
|
|
crypto_spi_ctx_template_t *tmpl, size_t *tmpl_size)
|
2016-05-12 14:51:24 +00:00
|
|
|
{
|
|
|
|
void *keysched;
|
|
|
|
size_t size;
|
|
|
|
int rv;
|
|
|
|
|
2024-05-18 11:57:36 +00:00
|
|
|
if (mechanism->cm_type != AES_CCM_MECH_INFO_TYPE &&
|
|
|
|
mechanism->cm_type != AES_GCM_MECH_INFO_TYPE)
|
2016-05-12 14:51:24 +00:00
|
|
|
return (CRYPTO_MECHANISM_INVALID);
|
|
|
|
|
2021-12-25 03:34:29 +00:00
|
|
|
if ((keysched = aes_alloc_keysched(&size, KM_SLEEP)) == NULL) {
|
2016-05-12 14:51:24 +00:00
|
|
|
return (CRYPTO_HOST_MEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize key schedule. Key length information is stored
|
|
|
|
* in the key.
|
|
|
|
*/
|
|
|
|
if ((rv = init_keysched(key, keysched)) != CRYPTO_SUCCESS) {
|
2022-02-25 13:26:54 +00:00
|
|
|
memset(keysched, 0, size);
|
2016-05-12 14:51:24 +00:00
|
|
|
kmem_free(keysched, size);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
*tmpl = keysched;
|
|
|
|
*tmpl_size = size;
|
|
|
|
|
|
|
|
return (CRYPTO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
aes_free_context(crypto_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
aes_ctx_t *aes_ctx = ctx->cc_provider_private;
|
|
|
|
|
|
|
|
if (aes_ctx != NULL) {
|
|
|
|
if (aes_ctx->ac_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
|
|
|
|
ASSERT(aes_ctx->ac_keysched_len != 0);
|
2022-02-25 13:26:54 +00:00
|
|
|
memset(aes_ctx->ac_keysched, 0,
|
|
|
|
aes_ctx->ac_keysched_len);
|
2016-05-12 14:51:24 +00:00
|
|
|
kmem_free(aes_ctx->ac_keysched,
|
|
|
|
aes_ctx->ac_keysched_len);
|
|
|
|
}
|
|
|
|
crypto_free_mode_ctx(aes_ctx);
|
|
|
|
ctx->cc_provider_private = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (CRYPTO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
aes_common_init_ctx(aes_ctx_t *aes_ctx, crypto_spi_ctx_template_t *template,
|
|
|
|
crypto_mechanism_t *mechanism, crypto_key_t *key, int kmflag,
|
|
|
|
boolean_t is_encrypt_init)
|
|
|
|
{
|
|
|
|
int rv = CRYPTO_SUCCESS;
|
|
|
|
void *keysched;
|
2016-10-05 01:15:57 +00:00
|
|
|
size_t size = 0;
|
2016-05-12 14:51:24 +00:00
|
|
|
|
|
|
|
if (template == NULL) {
|
|
|
|
if ((keysched = aes_alloc_keysched(&size, kmflag)) == NULL)
|
|
|
|
return (CRYPTO_HOST_MEMORY);
|
|
|
|
/*
|
|
|
|
* Initialize key schedule.
|
|
|
|
* Key length is stored in the key.
|
|
|
|
*/
|
|
|
|
if ((rv = init_keysched(key, keysched)) != CRYPTO_SUCCESS) {
|
|
|
|
kmem_free(keysched, size);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
aes_ctx->ac_flags |= PROVIDER_OWNS_KEY_SCHEDULE;
|
|
|
|
aes_ctx->ac_keysched_len = size;
|
|
|
|
} else {
|
|
|
|
keysched = template;
|
|
|
|
}
|
|
|
|
aes_ctx->ac_keysched = keysched;
|
|
|
|
|
|
|
|
switch (mechanism->cm_type) {
|
|
|
|
case AES_CCM_MECH_INFO_TYPE:
|
|
|
|
if (mechanism->cm_param == NULL ||
|
|
|
|
mechanism->cm_param_len != sizeof (CK_AES_CCM_PARAMS)) {
|
|
|
|
return (CRYPTO_MECHANISM_PARAM_INVALID);
|
|
|
|
}
|
|
|
|
rv = ccm_init_ctx((ccm_ctx_t *)aes_ctx, mechanism->cm_param,
|
|
|
|
kmflag, is_encrypt_init, AES_BLOCK_LEN, aes_encrypt_block,
|
|
|
|
aes_xor_block);
|
|
|
|
break;
|
|
|
|
case AES_GCM_MECH_INFO_TYPE:
|
|
|
|
if (mechanism->cm_param == NULL ||
|
|
|
|
mechanism->cm_param_len != sizeof (CK_AES_GCM_PARAMS)) {
|
|
|
|
return (CRYPTO_MECHANISM_PARAM_INVALID);
|
|
|
|
}
|
|
|
|
rv = gcm_init_ctx((gcm_ctx_t *)aes_ctx, mechanism->cm_param,
|
|
|
|
AES_BLOCK_LEN, aes_encrypt_block, aes_copy_block,
|
|
|
|
aes_xor_block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != CRYPTO_SUCCESS) {
|
|
|
|
if (aes_ctx->ac_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
|
2022-02-25 13:26:54 +00:00
|
|
|
memset(keysched, 0, size);
|
2016-05-12 14:51:24 +00:00
|
|
|
kmem_free(keysched, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|