xref: /illumos-gate/usr/src/common/crypto/modes/modes.c (revision eb633035)
123c57df7Smcpowers /*
223c57df7Smcpowers  * CDDL HEADER START
323c57df7Smcpowers  *
423c57df7Smcpowers  * The contents of this file are subject to the terms of the
523c57df7Smcpowers  * Common Development and Distribution License (the "License").
623c57df7Smcpowers  * You may not use this file except in compliance with the License.
723c57df7Smcpowers  *
823c57df7Smcpowers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
923c57df7Smcpowers  * or http://www.opensolaris.org/os/licensing.
1023c57df7Smcpowers  * See the License for the specific language governing permissions
1123c57df7Smcpowers  * and limitations under the License.
1223c57df7Smcpowers  *
1323c57df7Smcpowers  * When distributing Covered Code, include this CDDL HEADER in each
1423c57df7Smcpowers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1523c57df7Smcpowers  * If applicable, add the following below this CDDL HEADER, with the
1623c57df7Smcpowers  * fields enclosed by brackets "[]" replaced with your own identifying
1723c57df7Smcpowers  * information: Portions Copyright [yyyy] [name of copyright owner]
1823c57df7Smcpowers  *
1923c57df7Smcpowers  * CDDL HEADER END
2023c57df7Smcpowers  */
2123c57df7Smcpowers /*
22983a1033SMark Powers  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2323c57df7Smcpowers  * Use is subject to license terms.
24cd964fceSMatt Barden  *
25cd964fceSMatt Barden  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
2623c57df7Smcpowers  */
2723c57df7Smcpowers 
2823c57df7Smcpowers #ifndef _KERNEL
2923c57df7Smcpowers #include <stdlib.h>
30cd964fceSMatt Barden #include <assert.h>
31cd964fceSMatt Barden #include <strings.h>
3223c57df7Smcpowers #endif
3323c57df7Smcpowers 
3423c57df7Smcpowers #include <sys/strsun.h>
3523c57df7Smcpowers #include <sys/types.h>
3623c57df7Smcpowers #include <modes/modes.h>
3723c57df7Smcpowers #include <sys/crypto/common.h>
3823c57df7Smcpowers #include <sys/crypto/impl.h>
3923c57df7Smcpowers 
4023c57df7Smcpowers /*
4123c57df7Smcpowers  * Initialize by setting iov_or_mp to point to the current iovec or mp,
4223c57df7Smcpowers  * and by setting current_offset to an offset within the current iovec or mp.
4323c57df7Smcpowers  */
4423c57df7Smcpowers void
crypto_init_ptrs(crypto_data_t * out,void ** iov_or_mp,offset_t * current_offset)4523c57df7Smcpowers crypto_init_ptrs(crypto_data_t *out, void **iov_or_mp, offset_t *current_offset)
4623c57df7Smcpowers {
4723c57df7Smcpowers 	offset_t offset;
4823c57df7Smcpowers 
4923c57df7Smcpowers 	switch (out->cd_format) {
5023c57df7Smcpowers 	case CRYPTO_DATA_RAW:
5123c57df7Smcpowers 		*current_offset = out->cd_offset;
5223c57df7Smcpowers 		break;
5323c57df7Smcpowers 
5423c57df7Smcpowers 	case CRYPTO_DATA_UIO: {
5523c57df7Smcpowers 		uio_t *uiop = out->cd_uio;
5623c57df7Smcpowers 		uintptr_t vec_idx;
5723c57df7Smcpowers 
5823c57df7Smcpowers 		offset = out->cd_offset;
5923c57df7Smcpowers 		for (vec_idx = 0; vec_idx < uiop->uio_iovcnt &&
6023c57df7Smcpowers 		    offset >= uiop->uio_iov[vec_idx].iov_len;
6123c57df7Smcpowers 		    offset -= uiop->uio_iov[vec_idx++].iov_len)
6223c57df7Smcpowers 			;
6323c57df7Smcpowers 
6423c57df7Smcpowers 		*current_offset = offset;
6523c57df7Smcpowers 		*iov_or_mp = (void *)vec_idx;
6623c57df7Smcpowers 		break;
6723c57df7Smcpowers 	}
6823c57df7Smcpowers 
6923c57df7Smcpowers 	case CRYPTO_DATA_MBLK: {
7023c57df7Smcpowers 		mblk_t *mp;
7123c57df7Smcpowers 
7223c57df7Smcpowers 		offset = out->cd_offset;
7323c57df7Smcpowers 		for (mp = out->cd_mp; mp != NULL && offset >= MBLKL(mp);
7423c57df7Smcpowers 		    offset -= MBLKL(mp), mp = mp->b_cont)
7523c57df7Smcpowers 			;
7623c57df7Smcpowers 
7723c57df7Smcpowers 		*current_offset = offset;
7823c57df7Smcpowers 		*iov_or_mp = mp;
7923c57df7Smcpowers 		break;
8023c57df7Smcpowers 
8123c57df7Smcpowers 	}
8223c57df7Smcpowers 	} /* end switch */
8323c57df7Smcpowers }
8423c57df7Smcpowers 
8523c57df7Smcpowers /*
8623c57df7Smcpowers  * Get pointers for where in the output to copy a block of encrypted or
8723c57df7Smcpowers  * decrypted data.  The iov_or_mp argument stores a pointer to the current
8823c57df7Smcpowers  * iovec or mp, and offset stores an offset into the current iovec or mp.
8923c57df7Smcpowers  */
9023c57df7Smcpowers void
crypto_get_ptrs(crypto_data_t * out,void ** iov_or_mp,offset_t * current_offset,uint8_t ** out_data_1,size_t * out_data_1_len,uint8_t ** out_data_2,size_t amt)9123c57df7Smcpowers crypto_get_ptrs(crypto_data_t *out, void **iov_or_mp, offset_t *current_offset,
9223c57df7Smcpowers     uint8_t **out_data_1, size_t *out_data_1_len, uint8_t **out_data_2,
9323c57df7Smcpowers     size_t amt)
9423c57df7Smcpowers {
9523c57df7Smcpowers 	offset_t offset;
9623c57df7Smcpowers 
9723c57df7Smcpowers 	switch (out->cd_format) {
9823c57df7Smcpowers 	case CRYPTO_DATA_RAW: {
9923c57df7Smcpowers 		iovec_t *iov;
10023c57df7Smcpowers 
10123c57df7Smcpowers 		offset = *current_offset;
10223c57df7Smcpowers 		iov = &out->cd_raw;
10323c57df7Smcpowers 		if ((offset + amt) <= iov->iov_len) {
10423c57df7Smcpowers 			/* one block fits */
10523c57df7Smcpowers 			*out_data_1 = (uint8_t *)iov->iov_base + offset;
10623c57df7Smcpowers 			*out_data_1_len = amt;
10723c57df7Smcpowers 			*out_data_2 = NULL;
10823c57df7Smcpowers 			*current_offset = offset + amt;
10923c57df7Smcpowers 		}
11023c57df7Smcpowers 		break;
11123c57df7Smcpowers 	}
11223c57df7Smcpowers 
11323c57df7Smcpowers 	case CRYPTO_DATA_UIO: {
11423c57df7Smcpowers 		uio_t *uio = out->cd_uio;
11523c57df7Smcpowers 		iovec_t *iov;
11623c57df7Smcpowers 		offset_t offset;
11723c57df7Smcpowers 		uintptr_t vec_idx;
11823c57df7Smcpowers 		uint8_t *p;
11923c57df7Smcpowers 
12023c57df7Smcpowers 		offset = *current_offset;
12123c57df7Smcpowers 		vec_idx = (uintptr_t)(*iov_or_mp);
12223c57df7Smcpowers 		iov = &uio->uio_iov[vec_idx];
12323c57df7Smcpowers 		p = (uint8_t *)iov->iov_base + offset;
12423c57df7Smcpowers 		*out_data_1 = p;
12523c57df7Smcpowers 
12623c57df7Smcpowers 		if (offset + amt <= iov->iov_len) {
12723c57df7Smcpowers 			/* can fit one block into this iov */
12823c57df7Smcpowers 			*out_data_1_len = amt;
12923c57df7Smcpowers 			*out_data_2 = NULL;
13023c57df7Smcpowers 			*current_offset = offset + amt;
13123c57df7Smcpowers 		} else {
13223c57df7Smcpowers 			/* one block spans two iovecs */
13323c57df7Smcpowers 			*out_data_1_len = iov->iov_len - offset;
13423c57df7Smcpowers 			if (vec_idx == uio->uio_iovcnt)
13523c57df7Smcpowers 				return;
13623c57df7Smcpowers 			vec_idx++;
13723c57df7Smcpowers 			iov = &uio->uio_iov[vec_idx];
13823c57df7Smcpowers 			*out_data_2 = (uint8_t *)iov->iov_base;
13923c57df7Smcpowers 			*current_offset = amt - *out_data_1_len;
14023c57df7Smcpowers 		}
14123c57df7Smcpowers 		*iov_or_mp = (void *)vec_idx;
14223c57df7Smcpowers 		break;
14323c57df7Smcpowers 	}
14423c57df7Smcpowers 
14523c57df7Smcpowers 	case CRYPTO_DATA_MBLK: {
14623c57df7Smcpowers 		mblk_t *mp;
14723c57df7Smcpowers 		uint8_t *p;
14823c57df7Smcpowers 
14923c57df7Smcpowers 		offset = *current_offset;
15023c57df7Smcpowers 		mp = (mblk_t *)*iov_or_mp;
15123c57df7Smcpowers 		p = mp->b_rptr + offset;
15223c57df7Smcpowers 		*out_data_1 = p;
15323c57df7Smcpowers 		if ((p + amt) <= mp->b_wptr) {
15423c57df7Smcpowers 			/* can fit one block into this mblk */
15523c57df7Smcpowers 			*out_data_1_len = amt;
15623c57df7Smcpowers 			*out_data_2 = NULL;
15723c57df7Smcpowers 			*current_offset = offset + amt;
15823c57df7Smcpowers 		} else {
15923c57df7Smcpowers 			/* one block spans two mblks */
160104d3bdeSDan OpenSolaris Anderson 			*out_data_1_len = _PTRDIFF(mp->b_wptr, p);
16123c57df7Smcpowers 			if ((mp = mp->b_cont) == NULL)
16223c57df7Smcpowers 				return;
16323c57df7Smcpowers 			*out_data_2 = mp->b_rptr;
16423c57df7Smcpowers 			*current_offset = (amt - *out_data_1_len);
16523c57df7Smcpowers 		}
16623c57df7Smcpowers 		*iov_or_mp = mp;
16723c57df7Smcpowers 		break;
16823c57df7Smcpowers 	}
16923c57df7Smcpowers 	} /* end switch */
17023c57df7Smcpowers }
17123c57df7Smcpowers 
17223c57df7Smcpowers void
crypto_free_mode_ctx(void * ctx)17323c57df7Smcpowers crypto_free_mode_ctx(void *ctx)
17423c57df7Smcpowers {
17523c57df7Smcpowers 	common_ctx_t *common_ctx = (common_ctx_t *)ctx;
17623c57df7Smcpowers 
177cd964fceSMatt Barden 	switch (common_ctx->cc_flags & (ECB_MODE|CBC_MODE|CMAC_MODE|CTR_MODE|
178cd964fceSMatt Barden 	    CCM_MODE|GCM_MODE|GMAC_MODE)) {
1794d703b5cSMark Powers 	case ECB_MODE:
18023c57df7Smcpowers #ifdef _KERNEL
18123c57df7Smcpowers 		kmem_free(common_ctx, sizeof (ecb_ctx_t));
18223c57df7Smcpowers #else
18323c57df7Smcpowers 		free(common_ctx);
18423c57df7Smcpowers #endif
1854d703b5cSMark Powers 		break;
1864d703b5cSMark Powers 
1874d703b5cSMark Powers 	case CBC_MODE:
188cd964fceSMatt Barden 	case CMAC_MODE:
18923c57df7Smcpowers #ifdef _KERNEL
19023c57df7Smcpowers 		kmem_free(common_ctx, sizeof (cbc_ctx_t));
19123c57df7Smcpowers #else
19223c57df7Smcpowers 		free(common_ctx);
19323c57df7Smcpowers #endif
1944d703b5cSMark Powers 		break;
1954d703b5cSMark Powers 
1964d703b5cSMark Powers 	case CTR_MODE:
19723c57df7Smcpowers #ifdef _KERNEL
19823c57df7Smcpowers 		kmem_free(common_ctx, sizeof (ctr_ctx_t));
19923c57df7Smcpowers #else
20023c57df7Smcpowers 		free(common_ctx);
20123c57df7Smcpowers #endif
2024d703b5cSMark Powers 		break;
2034d703b5cSMark Powers 
2044d703b5cSMark Powers 	case CCM_MODE:
20523c57df7Smcpowers #ifdef _KERNEL
20623c57df7Smcpowers 		if (((ccm_ctx_t *)ctx)->ccm_pt_buf != NULL)
20723c57df7Smcpowers 			kmem_free(((ccm_ctx_t *)ctx)->ccm_pt_buf,
20823c57df7Smcpowers 			    ((ccm_ctx_t *)ctx)->ccm_data_len);
20923c57df7Smcpowers 
21023c57df7Smcpowers 		kmem_free(ctx, sizeof (ccm_ctx_t));
21123c57df7Smcpowers #else
21223c57df7Smcpowers 		if (((ccm_ctx_t *)ctx)->ccm_pt_buf != NULL)
21323c57df7Smcpowers 			free(((ccm_ctx_t *)ctx)->ccm_pt_buf);
21423c57df7Smcpowers 		free(ctx);
2154d703b5cSMark Powers #endif
2164d703b5cSMark Powers 		break;
2174d703b5cSMark Powers 
2184d703b5cSMark Powers 	case GCM_MODE:
219983a1033SMark Powers 	case GMAC_MODE:
2204d703b5cSMark Powers #ifdef _KERNEL
2214d703b5cSMark Powers 		if (((gcm_ctx_t *)ctx)->gcm_pt_buf != NULL)
2224d703b5cSMark Powers 			kmem_free(((gcm_ctx_t *)ctx)->gcm_pt_buf,
2234d703b5cSMark Powers 			    ((gcm_ctx_t *)ctx)->gcm_pt_buf_len);
2244d703b5cSMark Powers 
2254d703b5cSMark Powers 		kmem_free(ctx, sizeof (gcm_ctx_t));
2264d703b5cSMark Powers #else
2274d703b5cSMark Powers 		if (((gcm_ctx_t *)ctx)->gcm_pt_buf != NULL)
2284d703b5cSMark Powers 			free(((gcm_ctx_t *)ctx)->gcm_pt_buf);
2294d703b5cSMark Powers 		free(ctx);
23023c57df7Smcpowers #endif
23123c57df7Smcpowers 	}
23223c57df7Smcpowers }
233cd964fceSMatt Barden 
234cd964fceSMatt Barden /*
235cd964fceSMatt Barden  * Utility routine to apply the command, 'cmd', to the
236cd964fceSMatt Barden  * data in the uio structure.
237cd964fceSMatt Barden  */
238cd964fceSMatt Barden int
crypto_uio_data(crypto_data_t * data,uchar_t * buf,int len,cmd_type_t cmd,void * digest_ctx,void (* update)())239cd964fceSMatt Barden crypto_uio_data(crypto_data_t *data, uchar_t *buf, int len, cmd_type_t cmd,
240cd964fceSMatt Barden     void *digest_ctx, void (*update)())
241cd964fceSMatt Barden {
242cd964fceSMatt Barden 	uio_t *uiop = data->cd_uio;
243cd964fceSMatt Barden 	off_t offset = data->cd_offset;
244cd964fceSMatt Barden 	size_t length = len;
245cd964fceSMatt Barden 	uint_t vec_idx;
246cd964fceSMatt Barden 	size_t cur_len;
247cd964fceSMatt Barden 	uchar_t *datap;
248cd964fceSMatt Barden 
249cd964fceSMatt Barden #ifdef _KERNEL
250cd964fceSMatt Barden 	ASSERT3U(data->cd_format, ==, CRYPTO_DATA_UIO);
251cd964fceSMatt Barden #else
252cd964fceSMatt Barden 	assert(data->cd_format == CRYPTO_DATA_UIO);
253cd964fceSMatt Barden #endif
254cd964fceSMatt Barden 	if (uiop->uio_segflg != UIO_SYSSPACE) {
255cd964fceSMatt Barden 		return (CRYPTO_ARGUMENTS_BAD);
256cd964fceSMatt Barden 	}
257cd964fceSMatt Barden 
258cd964fceSMatt Barden 	/*
259cd964fceSMatt Barden 	 * Jump to the first iovec containing data to be
260cd964fceSMatt Barden 	 * processed.
261cd964fceSMatt Barden 	 */
262cd964fceSMatt Barden 	for (vec_idx = 0; vec_idx < uiop->uio_iovcnt &&
263cd964fceSMatt Barden 	    offset >= uiop->uio_iov[vec_idx].iov_len;
264cd964fceSMatt Barden 	    offset -= uiop->uio_iov[vec_idx++].iov_len)
265cd964fceSMatt Barden 		;
266cd964fceSMatt Barden 
267*eb633035STom Caputi 	if (vec_idx == uiop->uio_iovcnt && length > 0) {
268cd964fceSMatt Barden 		/*
269cd964fceSMatt Barden 		 * The caller specified an offset that is larger than
270cd964fceSMatt Barden 		 * the total size of the buffers it provided.
271cd964fceSMatt Barden 		 */
272cd964fceSMatt Barden 		return (CRYPTO_DATA_LEN_RANGE);
273cd964fceSMatt Barden 	}
274cd964fceSMatt Barden 
275cd964fceSMatt Barden 	while (vec_idx < uiop->uio_iovcnt && length > 0) {
276cd964fceSMatt Barden 		cur_len = MIN(uiop->uio_iov[vec_idx].iov_len -
277cd964fceSMatt Barden 		    offset, length);
278cd964fceSMatt Barden 
279cd964fceSMatt Barden 		datap = (uchar_t *)(uiop->uio_iov[vec_idx].iov_base +
280cd964fceSMatt Barden 		    offset);
281cd964fceSMatt Barden 		switch (cmd) {
282cd964fceSMatt Barden 		case COPY_FROM_DATA:
283cd964fceSMatt Barden 			bcopy(datap, buf, cur_len);
284cd964fceSMatt Barden 			buf += cur_len;
285cd964fceSMatt Barden 			break;
286cd964fceSMatt Barden 		case COPY_TO_DATA:
287cd964fceSMatt Barden 			bcopy(buf, datap, cur_len);
288cd964fceSMatt Barden 			buf += cur_len;
289cd964fceSMatt Barden 			break;
290cd964fceSMatt Barden 		case COMPARE_TO_DATA:
291cd964fceSMatt Barden 			if (bcmp(datap, buf, cur_len))
292cd964fceSMatt Barden 				return (CRYPTO_SIGNATURE_INVALID);
293cd964fceSMatt Barden 			buf += cur_len;
294cd964fceSMatt Barden 			break;
295cd964fceSMatt Barden 		case MD5_DIGEST_DATA:
296cd964fceSMatt Barden 		case SHA1_DIGEST_DATA:
297cd964fceSMatt Barden 		case SHA2_DIGEST_DATA:
298cd964fceSMatt Barden 		case GHASH_DATA:
299cd964fceSMatt Barden 			update(digest_ctx, datap, cur_len);
300cd964fceSMatt Barden 			break;
301cd964fceSMatt Barden 		}
302cd964fceSMatt Barden 
303cd964fceSMatt Barden 		length -= cur_len;
304cd964fceSMatt Barden 		vec_idx++;
305cd964fceSMatt Barden 		offset = 0;
306cd964fceSMatt Barden 	}
307cd964fceSMatt Barden 
308cd964fceSMatt Barden 	if (vec_idx == uiop->uio_iovcnt && length > 0) {
309cd964fceSMatt Barden 		/*
310cd964fceSMatt Barden 		 * The end of the specified iovec's was reached but
311cd964fceSMatt Barden 		 * the length requested could not be processed.
312cd964fceSMatt Barden 		 */
313cd964fceSMatt Barden 		switch (cmd) {
314cd964fceSMatt Barden 		case COPY_TO_DATA:
315cd964fceSMatt Barden 			data->cd_length = len;
316cd964fceSMatt Barden 			return (CRYPTO_BUFFER_TOO_SMALL);
317cd964fceSMatt Barden 		default:
318cd964fceSMatt Barden 			return (CRYPTO_DATA_LEN_RANGE);
319cd964fceSMatt Barden 		}
320cd964fceSMatt Barden 	}
321cd964fceSMatt Barden 
322cd964fceSMatt Barden 	return (CRYPTO_SUCCESS);
323cd964fceSMatt Barden }
324cd964fceSMatt Barden 
325cd964fceSMatt Barden /*
326cd964fceSMatt Barden  * Utility routine to apply the command, 'cmd', to the
327cd964fceSMatt Barden  * data in the mblk structure.
328cd964fceSMatt Barden  */
329cd964fceSMatt Barden int
crypto_mblk_data(crypto_data_t * data,uchar_t * buf,int len,cmd_type_t cmd,void * digest_ctx,void (* update)())330cd964fceSMatt Barden crypto_mblk_data(crypto_data_t *data, uchar_t *buf, int len, cmd_type_t cmd,
331cd964fceSMatt Barden     void *digest_ctx, void (*update)())
332cd964fceSMatt Barden {
333cd964fceSMatt Barden 	off_t offset = data->cd_offset;
334cd964fceSMatt Barden 	size_t length = len;
335cd964fceSMatt Barden 	mblk_t *mp;
336cd964fceSMatt Barden 	size_t cur_len;
337cd964fceSMatt Barden 	uchar_t *datap;
338cd964fceSMatt Barden 
339cd964fceSMatt Barden #ifdef _KERNEL
340cd964fceSMatt Barden 	ASSERT3U(data->cd_format, ==, CRYPTO_DATA_MBLK);
341cd964fceSMatt Barden #else
342cd964fceSMatt Barden 	assert(data->cd_format == CRYPTO_DATA_MBLK);
343cd964fceSMatt Barden #endif
344cd964fceSMatt Barden 	/*
345cd964fceSMatt Barden 	 * Jump to the first mblk_t containing data to be processed.
346cd964fceSMatt Barden 	 */
347cd964fceSMatt Barden 	for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
348cd964fceSMatt Barden 	    offset -= MBLKL(mp), mp = mp->b_cont)
349cd964fceSMatt Barden 		;
350cd964fceSMatt Barden 	if (mp == NULL) {
351cd964fceSMatt Barden 		/*
352cd964fceSMatt Barden 		 * The caller specified an offset that is larger
353cd964fceSMatt Barden 		 * than the total size of the buffers it provided.
354cd964fceSMatt Barden 		 */
355cd964fceSMatt Barden 		return (CRYPTO_DATA_LEN_RANGE);
356cd964fceSMatt Barden 	}
357cd964fceSMatt Barden 
358cd964fceSMatt Barden 	/*
359cd964fceSMatt Barden 	 * Now do the processing on the mblk chain.
360cd964fceSMatt Barden 	 */
361cd964fceSMatt Barden 	while (mp != NULL && length > 0) {
362cd964fceSMatt Barden 		cur_len = MIN(MBLKL(mp) - offset, length);
363cd964fceSMatt Barden 
364cd964fceSMatt Barden 		datap = (uchar_t *)(mp->b_rptr + offset);
365cd964fceSMatt Barden 		switch (cmd) {
366cd964fceSMatt Barden 		case COPY_FROM_DATA:
367cd964fceSMatt Barden 			bcopy(datap, buf, cur_len);
368cd964fceSMatt Barden 			buf += cur_len;
369cd964fceSMatt Barden 			break;
370cd964fceSMatt Barden 		case COPY_TO_DATA:
371cd964fceSMatt Barden 			bcopy(buf, datap, cur_len);
372cd964fceSMatt Barden 			buf += cur_len;
373cd964fceSMatt Barden 			break;
374cd964fceSMatt Barden 		case COMPARE_TO_DATA:
375cd964fceSMatt Barden 			if (bcmp(datap, buf, cur_len))
376cd964fceSMatt Barden 				return (CRYPTO_SIGNATURE_INVALID);
377cd964fceSMatt Barden 			buf += cur_len;
378cd964fceSMatt Barden 			break;
379cd964fceSMatt Barden 		case MD5_DIGEST_DATA:
380cd964fceSMatt Barden 		case SHA1_DIGEST_DATA:
381cd964fceSMatt Barden 		case SHA2_DIGEST_DATA:
382cd964fceSMatt Barden 		case GHASH_DATA:
383cd964fceSMatt Barden 			update(digest_ctx, datap, cur_len);
384cd964fceSMatt Barden 			break;
385cd964fceSMatt Barden 		}
386cd964fceSMatt Barden 
387cd964fceSMatt Barden 		length -= cur_len;
388cd964fceSMatt Barden 		offset = 0;
389cd964fceSMatt Barden 		mp = mp->b_cont;
390cd964fceSMatt Barden 	}
391cd964fceSMatt Barden 
392cd964fceSMatt Barden 	if (mp == NULL && length > 0) {
393cd964fceSMatt Barden 		/*
394cd964fceSMatt Barden 		 * The end of the mblk was reached but the length
395cd964fceSMatt Barden 		 * requested could not be processed.
396cd964fceSMatt Barden 		 */
397cd964fceSMatt Barden 		switch (cmd) {
398cd964fceSMatt Barden 		case COPY_TO_DATA:
399cd964fceSMatt Barden 			data->cd_length = len;
400cd964fceSMatt Barden 			return (CRYPTO_BUFFER_TOO_SMALL);
401cd964fceSMatt Barden 		default:
402cd964fceSMatt Barden 			return (CRYPTO_DATA_LEN_RANGE);
403cd964fceSMatt Barden 		}
404cd964fceSMatt Barden 	}
405cd964fceSMatt Barden 
406cd964fceSMatt Barden 	return (CRYPTO_SUCCESS);
407cd964fceSMatt Barden }
408cd964fceSMatt Barden 
409cd964fceSMatt Barden /*
410cd964fceSMatt Barden  * Utility routine to copy a buffer to a crypto_data structure.
411cd964fceSMatt Barden  */
412cd964fceSMatt Barden int
crypto_put_output_data(uchar_t * buf,crypto_data_t * output,int len)413cd964fceSMatt Barden crypto_put_output_data(uchar_t *buf, crypto_data_t *output, int len)
414cd964fceSMatt Barden {
415cd964fceSMatt Barden 	switch (output->cd_format) {
416cd964fceSMatt Barden 	case CRYPTO_DATA_RAW:
417cd964fceSMatt Barden 		if (MAXOFF_T - output->cd_offset < (off_t)len) {
418cd964fceSMatt Barden 			return (CRYPTO_ARGUMENTS_BAD);
419cd964fceSMatt Barden 		}
420cd964fceSMatt Barden 		if (output->cd_raw.iov_len < len + output->cd_offset) {
421cd964fceSMatt Barden 			output->cd_length = len;
422cd964fceSMatt Barden 			return (CRYPTO_BUFFER_TOO_SMALL);
423cd964fceSMatt Barden 		}
424cd964fceSMatt Barden 		bcopy(buf, (uchar_t *)(output->cd_raw.iov_base +
425cd964fceSMatt Barden 		    output->cd_offset), len);
426cd964fceSMatt Barden 		break;
427cd964fceSMatt Barden 
428cd964fceSMatt Barden 	case CRYPTO_DATA_UIO:
429cd964fceSMatt Barden 		return (crypto_uio_data(output, buf, len,
430cd964fceSMatt Barden 		    COPY_TO_DATA, NULL, NULL));
431cd964fceSMatt Barden 
432cd964fceSMatt Barden 	case CRYPTO_DATA_MBLK:
433cd964fceSMatt Barden 		return (crypto_mblk_data(output, buf, len,
434cd964fceSMatt Barden 		    COPY_TO_DATA, NULL, NULL));
435cd964fceSMatt Barden 
436cd964fceSMatt Barden 	default:
437cd964fceSMatt Barden 		return (CRYPTO_ARGUMENTS_BAD);
438cd964fceSMatt Barden 	}
439cd964fceSMatt Barden 
440cd964fceSMatt Barden 	return (CRYPTO_SUCCESS);
441cd964fceSMatt Barden }
442