xref: /titanic_52/usr/src/common/crypto/modes/modes.c (revision 4b56a00321e0ce508e55cc5e43e3ad7b00005a39)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #ifndef _KERNEL
29 #include <stdlib.h>
30 #endif
31 
32 #include <sys/strsun.h>
33 #include <sys/types.h>
34 #include <modes/modes.h>
35 #include <sys/crypto/common.h>
36 #include <sys/crypto/impl.h>
37 
38 /*
39  * Initialize by setting iov_or_mp to point to the current iovec or mp,
40  * and by setting current_offset to an offset within the current iovec or mp.
41  */
42 void
43 crypto_init_ptrs(crypto_data_t *out, void **iov_or_mp, offset_t *current_offset)
44 {
45 	offset_t offset;
46 
47 	switch (out->cd_format) {
48 	case CRYPTO_DATA_RAW:
49 		*current_offset = out->cd_offset;
50 		break;
51 
52 	case CRYPTO_DATA_UIO: {
53 		uio_t *uiop = out->cd_uio;
54 		uintptr_t vec_idx;
55 
56 		offset = out->cd_offset;
57 		for (vec_idx = 0; vec_idx < uiop->uio_iovcnt &&
58 		    offset >= uiop->uio_iov[vec_idx].iov_len;
59 		    offset -= uiop->uio_iov[vec_idx++].iov_len)
60 			;
61 
62 		*current_offset = offset;
63 		*iov_or_mp = (void *)vec_idx;
64 		break;
65 	}
66 
67 	case CRYPTO_DATA_MBLK: {
68 		mblk_t *mp;
69 
70 		offset = out->cd_offset;
71 		for (mp = out->cd_mp; mp != NULL && offset >= MBLKL(mp);
72 		    offset -= MBLKL(mp), mp = mp->b_cont)
73 			;
74 
75 		*current_offset = offset;
76 		*iov_or_mp = mp;
77 		break;
78 
79 	}
80 	} /* end switch */
81 }
82 
83 /*
84  * Get pointers for where in the output to copy a block of encrypted or
85  * decrypted data.  The iov_or_mp argument stores a pointer to the current
86  * iovec or mp, and offset stores an offset into the current iovec or mp.
87  */
88 void
89 crypto_get_ptrs(crypto_data_t *out, void **iov_or_mp, offset_t *current_offset,
90     uint8_t **out_data_1, size_t *out_data_1_len, uint8_t **out_data_2,
91     size_t amt)
92 {
93 	offset_t offset;
94 
95 	switch (out->cd_format) {
96 	case CRYPTO_DATA_RAW: {
97 		iovec_t *iov;
98 
99 		offset = *current_offset;
100 		iov = &out->cd_raw;
101 		if ((offset + amt) <= iov->iov_len) {
102 			/* one block fits */
103 			*out_data_1 = (uint8_t *)iov->iov_base + offset;
104 			*out_data_1_len = amt;
105 			*out_data_2 = NULL;
106 			*current_offset = offset + amt;
107 		}
108 		break;
109 	}
110 
111 	case CRYPTO_DATA_UIO: {
112 		uio_t *uio = out->cd_uio;
113 		iovec_t *iov;
114 		offset_t offset;
115 		uintptr_t vec_idx;
116 		uint8_t *p;
117 
118 		offset = *current_offset;
119 		vec_idx = (uintptr_t)(*iov_or_mp);
120 		iov = &uio->uio_iov[vec_idx];
121 		p = (uint8_t *)iov->iov_base + offset;
122 		*out_data_1 = p;
123 
124 		if (offset + amt <= iov->iov_len) {
125 			/* can fit one block into this iov */
126 			*out_data_1_len = amt;
127 			*out_data_2 = NULL;
128 			*current_offset = offset + amt;
129 		} else {
130 			/* one block spans two iovecs */
131 			*out_data_1_len = iov->iov_len - offset;
132 			if (vec_idx == uio->uio_iovcnt)
133 				return;
134 			vec_idx++;
135 			iov = &uio->uio_iov[vec_idx];
136 			*out_data_2 = (uint8_t *)iov->iov_base;
137 			*current_offset = amt - *out_data_1_len;
138 		}
139 		*iov_or_mp = (void *)vec_idx;
140 		break;
141 	}
142 
143 	case CRYPTO_DATA_MBLK: {
144 		mblk_t *mp;
145 		uint8_t *p;
146 
147 		offset = *current_offset;
148 		mp = (mblk_t *)*iov_or_mp;
149 		p = mp->b_rptr + offset;
150 		*out_data_1 = p;
151 		if ((p + amt) <= mp->b_wptr) {
152 			/* can fit one block into this mblk */
153 			*out_data_1_len = amt;
154 			*out_data_2 = NULL;
155 			*current_offset = offset + amt;
156 		} else {
157 			/* one block spans two mblks */
158 			*out_data_1_len = mp->b_wptr - p;
159 			if ((mp = mp->b_cont) == NULL)
160 				return;
161 			*out_data_2 = mp->b_rptr;
162 			*current_offset = (amt - *out_data_1_len);
163 		}
164 		*iov_or_mp = mp;
165 		break;
166 	}
167 	} /* end switch */
168 }
169 
170 void
171 crypto_free_mode_ctx(void *ctx)
172 {
173 	common_ctx_t *common_ctx = (common_ctx_t *)ctx;
174 
175 	if (common_ctx->cc_flags & ECB_MODE)
176 #ifdef _KERNEL
177 		kmem_free(common_ctx, sizeof (ecb_ctx_t));
178 #else
179 		free(common_ctx);
180 #endif
181 	else if (common_ctx->cc_flags & CBC_MODE)
182 #ifdef _KERNEL
183 		kmem_free(common_ctx, sizeof (cbc_ctx_t));
184 #else
185 		free(common_ctx);
186 #endif
187 	else if (common_ctx->cc_flags & CTR_MODE)
188 #ifdef _KERNEL
189 		kmem_free(common_ctx, sizeof (ctr_ctx_t));
190 #else
191 		free(common_ctx);
192 #endif
193 	else if (common_ctx->cc_flags & CCM_MODE) {
194 #ifdef _KERNEL
195 		if (((ccm_ctx_t *)ctx)->ccm_pt_buf != NULL)
196 			kmem_free(((ccm_ctx_t *)ctx)->ccm_pt_buf,
197 			    ((ccm_ctx_t *)ctx)->ccm_data_len);
198 
199 		kmem_free(ctx, sizeof (ccm_ctx_t));
200 #else
201 		if (((ccm_ctx_t *)ctx)->ccm_pt_buf != NULL)
202 			free(((ccm_ctx_t *)ctx)->ccm_pt_buf);
203 		free(ctx);
204 #endif
205 	}
206 }
207