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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 /*
26 * Copyright 2015 by Saso Kiselkov. All rights reserved.
27 */
28
29 #ifndef _KERNEL
30 #include <stdlib.h>
31 #endif
32
33 #include <sys/strsun.h>
34 #include <sys/types.h>
35 #include <modes/modes.h>
36 #include <sys/crypto/common.h>
37 #include <sys/crypto/impl.h>
38
39 /*
40 * Initialize by setting iov_or_mp to point to the current iovec or mp,
41 * and by setting current_offset to an offset within the current iovec or mp.
42 */
43 void
44 crypto_init_ptrs(crypto_data_t *out, void **iov_or_mp, offset_t *current_offset)
45 {
46 offset_t offset;
47
48 switch (out->cd_format) {
49 case CRYPTO_DATA_RAW:
50 *current_offset = out->cd_offset;
51 break;
52
53 case CRYPTO_DATA_UIO: {
54 uio_t *uiop = out->cd_uio;
55 uintptr_t vec_idx;
56
57 offset = out->cd_offset;
58 for (vec_idx = 0; vec_idx < uiop->uio_iovcnt &&
59 offset >= uiop->uio_iov[vec_idx].iov_len;
60 offset -= uiop->uio_iov[vec_idx++].iov_len)
61 ;
62
63 *current_offset = offset;
64 *iov_or_mp = (void *)vec_idx;
65 break;
66 }
67
68 case CRYPTO_DATA_MBLK: {
69 mblk_t *mp;
70
71 offset = out->cd_offset;
72 for (mp = out->cd_mp; mp != NULL && offset >= MBLKL(mp);
73 offset -= MBLKL(mp), mp = mp->b_cont)
74 ;
75
76 *current_offset = offset;
77 *iov_or_mp = mp;
78 break;
79
80 }
81 } /* end switch */
82 }
83
84 void
85 crypto_free_mode_ctx(void *ctx)
86 {
87 common_ctx_t *common_ctx = (common_ctx_t *)ctx;
88
89 switch (common_ctx->cc_flags &
90 (ECB_MODE|CBC_MODE|CTR_MODE|CCM_MODE|GCM_MODE|GMAC_MODE)) {
91 case ECB_MODE:
92 #ifdef _KERNEL
93 kmem_free(common_ctx, sizeof (ecb_ctx_t));
94 #else
95 free(common_ctx);
96 #endif
97 break;
98
99 case CBC_MODE:
100 #ifdef _KERNEL
101 kmem_free(common_ctx, sizeof (cbc_ctx_t));
102 #else
103 free(common_ctx);
104 #endif
105 break;
106
107 case CTR_MODE:
108 #ifdef _KERNEL
109 kmem_free(common_ctx, sizeof (ctr_ctx_t));
110 #else
111 free(common_ctx);
112 #endif
113 break;
114
115 case CCM_MODE:
116 #ifdef _KERNEL
117 if (((ccm_ctx_t *)ctx)->ccm_pt_buf != NULL)
118 kmem_free(((ccm_ctx_t *)ctx)->ccm_pt_buf,
119 ((ccm_ctx_t *)ctx)->ccm_data_len);
120
121 kmem_free(ctx, sizeof (ccm_ctx_t));
122 #else
123 if (((ccm_ctx_t *)ctx)->ccm_pt_buf != NULL)
124 free(((ccm_ctx_t *)ctx)->ccm_pt_buf);
125 free(ctx);
126 #endif
127 break;
128
129 case GCM_MODE:
130 case GMAC_MODE:
131 #ifdef _KERNEL
132 kmem_free(ctx, sizeof (gcm_ctx_t));
133 #else
134 free(ctx);
135 #endif
136 }
137 }