1*7188Smcpowers /*
2*7188Smcpowers  * CDDL HEADER START
3*7188Smcpowers  *
4*7188Smcpowers  * The contents of this file are subject to the terms of the
5*7188Smcpowers  * Common Development and Distribution License (the "License").
6*7188Smcpowers  * You may not use this file except in compliance with the License.
7*7188Smcpowers  *
8*7188Smcpowers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7188Smcpowers  * or http://www.opensolaris.org/os/licensing.
10*7188Smcpowers  * See the License for the specific language governing permissions
11*7188Smcpowers  * and limitations under the License.
12*7188Smcpowers  *
13*7188Smcpowers  * When distributing Covered Code, include this CDDL HEADER in each
14*7188Smcpowers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7188Smcpowers  * If applicable, add the following below this CDDL HEADER, with the
16*7188Smcpowers  * fields enclosed by brackets "[]" replaced with your own identifying
17*7188Smcpowers  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7188Smcpowers  *
19*7188Smcpowers  * CDDL HEADER END
20*7188Smcpowers  */
21*7188Smcpowers /*
22*7188Smcpowers  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7188Smcpowers  * Use is subject to license terms.
24*7188Smcpowers  */
25*7188Smcpowers 
26*7188Smcpowers #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7188Smcpowers 
28*7188Smcpowers #ifndef _KERNEL
29*7188Smcpowers #include <strings.h>
30*7188Smcpowers #include <limits.h>
31*7188Smcpowers #include <assert.h>
32*7188Smcpowers #include <security/cryptoki.h>
33*7188Smcpowers #endif
34*7188Smcpowers 
35*7188Smcpowers #include <sys/types.h>
36*7188Smcpowers #include <modes/modes.h>
37*7188Smcpowers #include <sys/crypto/common.h>
38*7188Smcpowers #include <sys/crypto/impl.h>
39*7188Smcpowers 
40*7188Smcpowers /*
41*7188Smcpowers  * Algorithm independent ECB functions.
42*7188Smcpowers  */
43*7188Smcpowers int
44*7188Smcpowers ecb_cipher_contiguous_blocks(ecb_ctx_t *ctx, char *data, size_t length,
45*7188Smcpowers     crypto_data_t *out, size_t block_size,
46*7188Smcpowers     int (*cipher)(const void *ks, const uint8_t *pt, uint8_t *ct))
47*7188Smcpowers {
48*7188Smcpowers 	size_t remainder = length;
49*7188Smcpowers 	size_t need;
50*7188Smcpowers 	uint8_t *datap = (uint8_t *)data;
51*7188Smcpowers 	uint8_t *blockp;
52*7188Smcpowers 	uint8_t *lastp;
53*7188Smcpowers 	void *iov_or_mp;
54*7188Smcpowers 	offset_t offset;
55*7188Smcpowers 	uint8_t *out_data_1;
56*7188Smcpowers 	uint8_t *out_data_2;
57*7188Smcpowers 	size_t out_data_1_len;
58*7188Smcpowers 
59*7188Smcpowers 	if (length + ctx->cc_remainder_len < block_size) {
60*7188Smcpowers 		/* accumulate bytes here and return */
61*7188Smcpowers 		bcopy(datap,
62*7188Smcpowers 		    (uint8_t *)ctx->cc_remainder + ctx->cc_remainder_len,
63*7188Smcpowers 		    length);
64*7188Smcpowers 		ctx->cc_remainder_len += length;
65*7188Smcpowers 		ctx->cc_copy_to = datap;
66*7188Smcpowers 		return (CRYPTO_SUCCESS);
67*7188Smcpowers 	}
68*7188Smcpowers 
69*7188Smcpowers 	lastp = (uint8_t *)ctx->cc_iv;
70*7188Smcpowers 	if (out != NULL)
71*7188Smcpowers 		crypto_init_ptrs(out, &iov_or_mp, &offset);
72*7188Smcpowers 
73*7188Smcpowers 	do {
74*7188Smcpowers 		/* Unprocessed data from last call. */
75*7188Smcpowers 		if (ctx->cc_remainder_len > 0) {
76*7188Smcpowers 			need = block_size - ctx->cc_remainder_len;
77*7188Smcpowers 
78*7188Smcpowers 			if (need > remainder)
79*7188Smcpowers 				return (CRYPTO_DATA_LEN_RANGE);
80*7188Smcpowers 
81*7188Smcpowers 			bcopy(datap, &((uint8_t *)ctx->cc_remainder)
82*7188Smcpowers 			    [ctx->cc_remainder_len], need);
83*7188Smcpowers 
84*7188Smcpowers 			blockp = (uint8_t *)ctx->cc_remainder;
85*7188Smcpowers 		} else {
86*7188Smcpowers 			blockp = datap;
87*7188Smcpowers 		}
88*7188Smcpowers 
89*7188Smcpowers 		if (out == NULL) {
90*7188Smcpowers 			cipher(ctx->cc_keysched, blockp, blockp);
91*7188Smcpowers 
92*7188Smcpowers 			ctx->cc_lastp = blockp;
93*7188Smcpowers 			lastp = blockp;
94*7188Smcpowers 
95*7188Smcpowers 			if (ctx->cc_remainder_len > 0) {
96*7188Smcpowers 				bcopy(blockp, ctx->cc_copy_to,
97*7188Smcpowers 				    ctx->cc_remainder_len);
98*7188Smcpowers 				bcopy(blockp + ctx->cc_remainder_len, datap,
99*7188Smcpowers 				    need);
100*7188Smcpowers 			}
101*7188Smcpowers 		} else {
102*7188Smcpowers 			cipher(ctx->cc_keysched, blockp, lastp);
103*7188Smcpowers 			crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
104*7188Smcpowers 			    &out_data_1_len, &out_data_2, block_size);
105*7188Smcpowers 
106*7188Smcpowers 			/* copy block to where it belongs */
107*7188Smcpowers 			bcopy(lastp, out_data_1, out_data_1_len);
108*7188Smcpowers 			if (out_data_2 != NULL) {
109*7188Smcpowers 				bcopy(lastp + out_data_1_len, out_data_2,
110*7188Smcpowers 				    block_size - out_data_1_len);
111*7188Smcpowers 			}
112*7188Smcpowers 			/* update offset */
113*7188Smcpowers 			out->cd_offset += block_size;
114*7188Smcpowers 		}
115*7188Smcpowers 
116*7188Smcpowers 		/* Update pointer to next block of data to be processed. */
117*7188Smcpowers 		if (ctx->cc_remainder_len != 0) {
118*7188Smcpowers 			datap += need;
119*7188Smcpowers 			ctx->cc_remainder_len = 0;
120*7188Smcpowers 		} else {
121*7188Smcpowers 			datap += block_size;
122*7188Smcpowers 		}
123*7188Smcpowers 
124*7188Smcpowers 		remainder = (size_t)&data[length] - (size_t)datap;
125*7188Smcpowers 
126*7188Smcpowers 		/* Incomplete last block. */
127*7188Smcpowers 		if (remainder > 0 && remainder < block_size) {
128*7188Smcpowers 			bcopy(datap, ctx->cc_remainder, remainder);
129*7188Smcpowers 			ctx->cc_remainder_len = remainder;
130*7188Smcpowers 			ctx->cc_copy_to = datap;
131*7188Smcpowers 			goto out;
132*7188Smcpowers 		}
133*7188Smcpowers 		ctx->cc_copy_to = NULL;
134*7188Smcpowers 
135*7188Smcpowers 	} while (remainder > 0);
136*7188Smcpowers 
137*7188Smcpowers out:
138*7188Smcpowers 	return (CRYPTO_SUCCESS);
139*7188Smcpowers }
140*7188Smcpowers 
141*7188Smcpowers /* ARGSUSED */
142*7188Smcpowers void *
143*7188Smcpowers ecb_alloc_ctx(int kmflag)
144*7188Smcpowers {
145*7188Smcpowers 	ecb_ctx_t *ecb_ctx;
146*7188Smcpowers 
147*7188Smcpowers #ifdef _KERNEL
148*7188Smcpowers 	if ((ecb_ctx = kmem_zalloc(sizeof (ecb_ctx_t), kmflag)) == NULL)
149*7188Smcpowers #else
150*7188Smcpowers 	if ((ecb_ctx = calloc(1, sizeof (ecb_ctx_t))) == NULL)
151*7188Smcpowers #endif
152*7188Smcpowers 		return (NULL);
153*7188Smcpowers 
154*7188Smcpowers 	ecb_ctx->cc_flags = ECB_MODE;
155*7188Smcpowers 	return (ecb_ctx);
156*7188Smcpowers }
157