]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - zfs/module/icp/algs/modes/ecb.c
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
27 #include <modes/modes.h>
28 #include <sys/crypto/common.h>
29 #include <sys/crypto/impl.h>
32 * Algorithm independent ECB functions.
35 ecb_cipher_contiguous_blocks(ecb_ctx_t
*ctx
, char *data
, size_t length
,
36 crypto_data_t
*out
, size_t block_size
,
37 int (*cipher
)(const void *ks
, const uint8_t *pt
, uint8_t *ct
))
39 size_t remainder
= length
;
41 uint8_t *datap
= (uint8_t *)data
;
48 size_t out_data_1_len
;
50 if (length
+ ctx
->ecb_remainder_len
< block_size
) {
51 /* accumulate bytes here and return */
53 (uint8_t *)ctx
->ecb_remainder
+ ctx
->ecb_remainder_len
,
55 ctx
->ecb_remainder_len
+= length
;
56 ctx
->ecb_copy_to
= datap
;
57 return (CRYPTO_SUCCESS
);
60 lastp
= (uint8_t *)ctx
->ecb_iv
;
62 crypto_init_ptrs(out
, &iov_or_mp
, &offset
);
65 /* Unprocessed data from last call. */
66 if (ctx
->ecb_remainder_len
> 0) {
67 need
= block_size
- ctx
->ecb_remainder_len
;
70 return (CRYPTO_DATA_LEN_RANGE
);
72 bcopy(datap
, &((uint8_t *)ctx
->ecb_remainder
)
73 [ctx
->ecb_remainder_len
], need
);
75 blockp
= (uint8_t *)ctx
->ecb_remainder
;
81 cipher(ctx
->ecb_keysched
, blockp
, blockp
);
83 ctx
->ecb_lastp
= blockp
;
86 if (ctx
->ecb_remainder_len
> 0) {
87 bcopy(blockp
, ctx
->ecb_copy_to
,
88 ctx
->ecb_remainder_len
);
89 bcopy(blockp
+ ctx
->ecb_remainder_len
, datap
,
93 cipher(ctx
->ecb_keysched
, blockp
, lastp
);
94 crypto_get_ptrs(out
, &iov_or_mp
, &offset
, &out_data_1
,
95 &out_data_1_len
, &out_data_2
, block_size
);
97 /* copy block to where it belongs */
98 bcopy(lastp
, out_data_1
, out_data_1_len
);
99 if (out_data_2
!= NULL
) {
100 bcopy(lastp
+ out_data_1_len
, out_data_2
,
101 block_size
- out_data_1_len
);
104 out
->cd_offset
+= block_size
;
107 /* Update pointer to next block of data to be processed. */
108 if (ctx
->ecb_remainder_len
!= 0) {
110 ctx
->ecb_remainder_len
= 0;
115 remainder
= (size_t)&data
[length
] - (size_t)datap
;
117 /* Incomplete last block. */
118 if (remainder
> 0 && remainder
< block_size
) {
119 bcopy(datap
, ctx
->ecb_remainder
, remainder
);
120 ctx
->ecb_remainder_len
= remainder
;
121 ctx
->ecb_copy_to
= datap
;
124 ctx
->ecb_copy_to
= NULL
;
126 } while (remainder
> 0);
129 return (CRYPTO_SUCCESS
);
134 ecb_alloc_ctx(int kmflag
)
138 if ((ecb_ctx
= kmem_zalloc(sizeof (ecb_ctx_t
), kmflag
)) == NULL
)
141 ecb_ctx
->ecb_flags
= ECB_MODE
;