]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/module/icp/algs/modes/ecb.c
UBUNTU: SAUCE: Update zfs to e02aaf17f15ad274fa1f24c9c826f1477911ea3f
[mirror_ubuntu-zesty-kernel.git] / zfs / module / icp / algs / modes / ecb.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <sys/zfs_context.h>
27 #include <modes/modes.h>
28 #include <sys/crypto/common.h>
29 #include <sys/crypto/impl.h>
30
31 /*
32 * Algorithm independent ECB functions.
33 */
34 int
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))
38 {
39 size_t remainder = length;
40 size_t need = 0;
41 uint8_t *datap = (uint8_t *)data;
42 uint8_t *blockp;
43 uint8_t *lastp;
44 void *iov_or_mp;
45 offset_t offset;
46 uint8_t *out_data_1;
47 uint8_t *out_data_2;
48 size_t out_data_1_len;
49
50 if (length + ctx->ecb_remainder_len < block_size) {
51 /* accumulate bytes here and return */
52 bcopy(datap,
53 (uint8_t *)ctx->ecb_remainder + ctx->ecb_remainder_len,
54 length);
55 ctx->ecb_remainder_len += length;
56 ctx->ecb_copy_to = datap;
57 return (CRYPTO_SUCCESS);
58 }
59
60 lastp = (uint8_t *)ctx->ecb_iv;
61 if (out != NULL)
62 crypto_init_ptrs(out, &iov_or_mp, &offset);
63
64 do {
65 /* Unprocessed data from last call. */
66 if (ctx->ecb_remainder_len > 0) {
67 need = block_size - ctx->ecb_remainder_len;
68
69 if (need > remainder)
70 return (CRYPTO_DATA_LEN_RANGE);
71
72 bcopy(datap, &((uint8_t *)ctx->ecb_remainder)
73 [ctx->ecb_remainder_len], need);
74
75 blockp = (uint8_t *)ctx->ecb_remainder;
76 } else {
77 blockp = datap;
78 }
79
80 if (out == NULL) {
81 cipher(ctx->ecb_keysched, blockp, blockp);
82
83 ctx->ecb_lastp = blockp;
84 lastp = blockp;
85
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,
90 need);
91 }
92 } else {
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);
96
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);
102 }
103 /* update offset */
104 out->cd_offset += block_size;
105 }
106
107 /* Update pointer to next block of data to be processed. */
108 if (ctx->ecb_remainder_len != 0) {
109 datap += need;
110 ctx->ecb_remainder_len = 0;
111 } else {
112 datap += block_size;
113 }
114
115 remainder = (size_t)&data[length] - (size_t)datap;
116
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;
122 goto out;
123 }
124 ctx->ecb_copy_to = NULL;
125
126 } while (remainder > 0);
127
128 out:
129 return (CRYPTO_SUCCESS);
130 }
131
132 /* ARGSUSED */
133 void *
134 ecb_alloc_ctx(int kmflag)
135 {
136 ecb_ctx_t *ecb_ctx;
137
138 if ((ecb_ctx = kmem_zalloc(sizeof (ecb_ctx_t), kmflag)) == NULL)
139 return (NULL);
140
141 ecb_ctx->ecb_flags = ECB_MODE;
142 return (ecb_ctx);
143 }