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
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* 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 2009 Sun Microsystems, Inc. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/zfs_context.h>
|
|
|
|
#include <modes/modes.h>
|
|
|
|
#include <sys/crypto/common.h>
|
|
|
|
#include <sys/crypto/impl.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Utility routine to copy a buffer to a crypto_data structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-12-25 02:23:07 +00:00
|
|
|
* Utility routine to apply the command COPY_TO_DATA to the
|
2016-05-12 14:51:24 +00:00
|
|
|
* data in the uio structure.
|
|
|
|
*/
|
2021-12-25 02:23:07 +00:00
|
|
|
static int
|
|
|
|
crypto_uio_copy_to_data(crypto_data_t *data, uchar_t *buf, int len)
|
2016-05-12 14:51:24 +00:00
|
|
|
{
|
2021-01-21 05:27:30 +00:00
|
|
|
zfs_uio_t *uiop = data->cd_uio;
|
2016-05-12 14:51:24 +00:00
|
|
|
off_t offset = data->cd_offset;
|
|
|
|
size_t length = len;
|
|
|
|
uint_t vec_idx;
|
|
|
|
size_t cur_len;
|
|
|
|
uchar_t *datap;
|
|
|
|
|
|
|
|
ASSERT(data->cd_format == CRYPTO_DATA_UIO);
|
2021-01-21 05:27:30 +00:00
|
|
|
if (zfs_uio_segflg(uiop) != UIO_SYSSPACE) {
|
2016-05-12 14:51:24 +00:00
|
|
|
return (CRYPTO_ARGUMENTS_BAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to the first iovec containing data to be
|
|
|
|
* processed.
|
|
|
|
*/
|
2021-01-21 05:27:30 +00:00
|
|
|
offset = zfs_uio_index_at_offset(uiop, offset, &vec_idx);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
if (vec_idx == zfs_uio_iovcnt(uiop) && length > 0) {
|
2016-05-12 14:51:24 +00:00
|
|
|
/*
|
|
|
|
* The caller specified an offset that is larger than
|
|
|
|
* the total size of the buffers it provided.
|
|
|
|
*/
|
|
|
|
return (CRYPTO_DATA_LEN_RANGE);
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
while (vec_idx < zfs_uio_iovcnt(uiop) && length > 0) {
|
|
|
|
cur_len = MIN(zfs_uio_iovlen(uiop, vec_idx) -
|
2016-05-12 14:51:24 +00:00
|
|
|
offset, length);
|
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
datap = (uchar_t *)(zfs_uio_iovbase(uiop, vec_idx) + offset);
|
2021-12-25 02:23:07 +00:00
|
|
|
bcopy(buf, datap, cur_len);
|
|
|
|
buf += cur_len;
|
2016-05-12 14:51:24 +00:00
|
|
|
|
|
|
|
length -= cur_len;
|
|
|
|
vec_idx++;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
if (vec_idx == zfs_uio_iovcnt(uiop) && length > 0) {
|
2016-05-12 14:51:24 +00:00
|
|
|
/*
|
2021-12-25 02:23:07 +00:00
|
|
|
* The end of the specified iovecs was reached but
|
2016-05-12 14:51:24 +00:00
|
|
|
* the length requested could not be processed.
|
|
|
|
*/
|
2021-12-25 02:23:07 +00:00
|
|
|
data->cd_length = len;
|
|
|
|
return (CRYPTO_BUFFER_TOO_SMALL);
|
2016-05-12 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (CRYPTO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
crypto_put_output_data(uchar_t *buf, crypto_data_t *output, int len)
|
|
|
|
{
|
|
|
|
switch (output->cd_format) {
|
|
|
|
case CRYPTO_DATA_RAW:
|
|
|
|
if (output->cd_raw.iov_len < len) {
|
|
|
|
output->cd_length = len;
|
|
|
|
return (CRYPTO_BUFFER_TOO_SMALL);
|
|
|
|
}
|
|
|
|
bcopy(buf, (uchar_t *)(output->cd_raw.iov_base +
|
|
|
|
output->cd_offset), len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CRYPTO_DATA_UIO:
|
2021-12-25 02:23:07 +00:00
|
|
|
return (crypto_uio_copy_to_data(output, buf, len));
|
2016-05-12 14:51:24 +00:00
|
|
|
default:
|
|
|
|
return (CRYPTO_ARGUMENTS_BAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (CRYPTO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
crypto_update_iov(void *ctx, crypto_data_t *input, crypto_data_t *output,
|
|
|
|
int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
|
|
|
|
void (*copy_block)(uint8_t *, uint64_t *))
|
|
|
|
{
|
|
|
|
common_ctx_t *common_ctx = ctx;
|
|
|
|
int rv;
|
|
|
|
|
2020-03-26 17:41:57 +00:00
|
|
|
ASSERT(input != output);
|
2016-05-12 14:51:24 +00:00
|
|
|
if (input->cd_miscdata != NULL) {
|
|
|
|
copy_block((uint8_t *)input->cd_miscdata,
|
|
|
|
&common_ctx->cc_iv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input->cd_raw.iov_len < input->cd_length)
|
|
|
|
return (CRYPTO_ARGUMENTS_BAD);
|
|
|
|
|
|
|
|
rv = (cipher)(ctx, input->cd_raw.iov_base + input->cd_offset,
|
2020-03-26 17:41:57 +00:00
|
|
|
input->cd_length, output);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
crypto_update_uio(void *ctx, crypto_data_t *input, crypto_data_t *output,
|
|
|
|
int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
|
|
|
|
void (*copy_block)(uint8_t *, uint64_t *))
|
|
|
|
{
|
|
|
|
common_ctx_t *common_ctx = ctx;
|
2021-01-21 05:27:30 +00:00
|
|
|
zfs_uio_t *uiop = input->cd_uio;
|
2016-05-12 14:51:24 +00:00
|
|
|
off_t offset = input->cd_offset;
|
|
|
|
size_t length = input->cd_length;
|
|
|
|
uint_t vec_idx;
|
|
|
|
size_t cur_len;
|
|
|
|
|
2020-03-26 17:41:57 +00:00
|
|
|
ASSERT(input != output);
|
2016-05-12 14:51:24 +00:00
|
|
|
if (input->cd_miscdata != NULL) {
|
|
|
|
copy_block((uint8_t *)input->cd_miscdata,
|
|
|
|
&common_ctx->cc_iv[0]);
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
if (zfs_uio_segflg(input->cd_uio) != UIO_SYSSPACE) {
|
2016-05-12 14:51:24 +00:00
|
|
|
return (CRYPTO_ARGUMENTS_BAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to the first iovec containing data to be
|
|
|
|
* processed.
|
|
|
|
*/
|
2021-01-21 05:27:30 +00:00
|
|
|
offset = zfs_uio_index_at_offset(uiop, offset, &vec_idx);
|
|
|
|
if (vec_idx == zfs_uio_iovcnt(uiop) && length > 0) {
|
2016-05-12 14:51:24 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-01-21 05:27:30 +00:00
|
|
|
while (vec_idx < zfs_uio_iovcnt(uiop) && length > 0) {
|
|
|
|
cur_len = MIN(zfs_uio_iovlen(uiop, vec_idx) -
|
2016-05-12 14:51:24 +00:00
|
|
|
offset, length);
|
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
int rv = (cipher)(ctx, zfs_uio_iovbase(uiop, vec_idx) + offset,
|
2020-03-26 17:41:57 +00:00
|
|
|
cur_len, output);
|
2016-05-12 14:51:24 +00:00
|
|
|
|
2019-12-03 18:28:48 +00:00
|
|
|
if (rv != CRYPTO_SUCCESS) {
|
|
|
|
return (rv);
|
|
|
|
}
|
2016-05-12 14:51:24 +00:00
|
|
|
length -= cur_len;
|
|
|
|
vec_idx++;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:27:30 +00:00
|
|
|
if (vec_idx == zfs_uio_iovcnt(uiop) && length > 0) {
|
2016-05-12 14:51:24 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (CRYPTO_SUCCESS);
|
|
|
|
}
|