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 https://opensource.org/licenses/CDDL-1.0.
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 2009 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 * Initialize by setting iov_or_mp to point to the current iovec or mp,
33 * and by setting current_offset to an offset within the current iovec or mp.
36 crypto_init_ptrs(crypto_data_t
*out
, void **iov_or_mp
, offset_t
*current_offset
)
40 switch (out
->cd_format
) {
42 *current_offset
= out
->cd_offset
;
45 case CRYPTO_DATA_UIO
: {
46 zfs_uio_t
*uiop
= out
->cd_uio
;
49 offset
= out
->cd_offset
;
50 offset
= zfs_uio_index_at_offset(uiop
, offset
, &vec_idx
);
52 *current_offset
= offset
;
53 *iov_or_mp
= (void *)(uintptr_t)vec_idx
;
60 * Get pointers for where in the output to copy a block of encrypted or
61 * decrypted data. The iov_or_mp argument stores a pointer to the current
62 * iovec or mp, and offset stores an offset into the current iovec or mp.
65 crypto_get_ptrs(crypto_data_t
*out
, void **iov_or_mp
, offset_t
*current_offset
,
66 uint8_t **out_data_1
, size_t *out_data_1_len
, uint8_t **out_data_2
,
71 switch (out
->cd_format
) {
72 case CRYPTO_DATA_RAW
: {
75 offset
= *current_offset
;
77 if ((offset
+ amt
) <= iov
->iov_len
) {
79 *out_data_1
= (uint8_t *)iov
->iov_base
+ offset
;
80 *out_data_1_len
= amt
;
82 *current_offset
= offset
+ amt
;
87 case CRYPTO_DATA_UIO
: {
88 zfs_uio_t
*uio
= out
->cd_uio
;
95 offset
= *current_offset
;
96 vec_idx
= (uintptr_t)(*iov_or_mp
);
97 zfs_uio_iov_at_index(uio
, vec_idx
, &iov_base
, &iov_len
);
98 p
= (uint8_t *)iov_base
+ offset
;
101 if (offset
+ amt
<= iov_len
) {
102 /* can fit one block into this iov */
103 *out_data_1_len
= amt
;
105 *current_offset
= offset
+ amt
;
107 /* one block spans two iovecs */
108 *out_data_1_len
= iov_len
- offset
;
109 if (vec_idx
== zfs_uio_iovcnt(uio
)) {
114 zfs_uio_iov_at_index(uio
, vec_idx
, &iov_base
, &iov_len
);
115 *out_data_2
= (uint8_t *)iov_base
;
116 *current_offset
= amt
- *out_data_1_len
;
118 *iov_or_mp
= (void *)(uintptr_t)vec_idx
;
125 crypto_free_mode_ctx(void *ctx
)
127 common_ctx_t
*common_ctx
= (common_ctx_t
*)ctx
;
129 switch (common_ctx
->cc_flags
& (CCM_MODE
|GCM_MODE
)) {
131 if (((ccm_ctx_t
*)ctx
)->ccm_pt_buf
!= NULL
)
132 vmem_free(((ccm_ctx_t
*)ctx
)->ccm_pt_buf
,
133 ((ccm_ctx_t
*)ctx
)->ccm_data_len
);
135 kmem_free(ctx
, sizeof (ccm_ctx_t
));
139 gcm_clear_ctx((gcm_ctx_t
*)ctx
);
140 kmem_free(ctx
, sizeof (gcm_ctx_t
));
144 __builtin_unreachable();
149 explicit_memset(void *s
, int c
, size_t n
)
152 __asm__
__volatile__("" :: "r"(s
) : "memory");
157 * Clear sensitive data in the context and free allocated memory.
159 * ctx->gcm_remainder may contain a plaintext remainder. ctx->gcm_H and
160 * ctx->gcm_Htable contain the hash sub key which protects authentication.
161 * ctx->gcm_pt_buf contains the plaintext result of decryption.
163 * Although extremely unlikely, ctx->gcm_J0 and ctx->gcm_tmp could be used for
164 * a known plaintext attack, they consist of the IV and the first and last
165 * counter respectively. If they should be cleared is debatable.
168 gcm_clear_ctx(gcm_ctx_t
*ctx
)
170 explicit_memset(ctx
->gcm_remainder
, 0, sizeof (ctx
->gcm_remainder
));
171 explicit_memset(ctx
->gcm_H
, 0, sizeof (ctx
->gcm_H
));
172 #if defined(CAN_USE_GCM_ASM)
173 if (ctx
->gcm_use_avx
== B_TRUE
) {
174 ASSERT3P(ctx
->gcm_Htable
, !=, NULL
);
175 memset(ctx
->gcm_Htable
, 0, ctx
->gcm_htab_len
);
176 kmem_free(ctx
->gcm_Htable
, ctx
->gcm_htab_len
);
179 if (ctx
->gcm_pt_buf
!= NULL
) {
180 memset(ctx
->gcm_pt_buf
, 0, ctx
->gcm_pt_buf_len
);
181 vmem_free(ctx
->gcm_pt_buf
, ctx
->gcm_pt_buf_len
);
184 explicit_memset(ctx
->gcm_J0
, 0, sizeof (ctx
->gcm_J0
));
185 explicit_memset(ctx
->gcm_tmp
, 0, sizeof (ctx
->gcm_tmp
));