1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/sysmacros.h>
30*0Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
31*0Sstevel@tonic-gate #include <sys/systm.h>
32*0Sstevel@tonic-gate #else
33*0Sstevel@tonic-gate #include <strings.h>
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate #include "cbc.h"
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #define CBC_MAX_BLOCK_SIZE 64
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate static void
cbc_xorblock(uint8_t * lastp,uint8_t * thisp,int blocksize)40*0Sstevel@tonic-gate cbc_xorblock(uint8_t *lastp, uint8_t *thisp, int blocksize)
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate uint32_t *this32p;
43*0Sstevel@tonic-gate uint32_t *last32p;
44*0Sstevel@tonic-gate int i;
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate if (IS_P2ALIGNED(thisp, sizeof (uint32_t)) &&
47*0Sstevel@tonic-gate IS_P2ALIGNED(lastp, sizeof (uint32_t)) &&
48*0Sstevel@tonic-gate IS_P2ALIGNED(blocksize, sizeof (uint32_t))) {
49*0Sstevel@tonic-gate /* LINTED */
50*0Sstevel@tonic-gate this32p = (uint32_t *)thisp;
51*0Sstevel@tonic-gate /* LINTED */
52*0Sstevel@tonic-gate last32p = (uint32_t *)lastp;
53*0Sstevel@tonic-gate for (i = 0; i < blocksize; i += 4) {
54*0Sstevel@tonic-gate *this32p ^= *last32p;
55*0Sstevel@tonic-gate this32p++;
56*0Sstevel@tonic-gate last32p++;
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate } else {
59*0Sstevel@tonic-gate for (i = 0; i < blocksize; i++) {
60*0Sstevel@tonic-gate thisp[i] ^= lastp[i];
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate boolean_t
cbc_encrypt(cbc_handle_t * ch,uint8_t * data,size_t datalen,uint8_t * IV)66*0Sstevel@tonic-gate cbc_encrypt(cbc_handle_t *ch, uint8_t *data, size_t datalen,
67*0Sstevel@tonic-gate uint8_t *IV)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate uint8_t *lastp;
70*0Sstevel@tonic-gate uint8_t *thisp;
71*0Sstevel@tonic-gate size_t i;
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate if (!IS_P2ALIGNED(datalen, ch->blocklen)) {
74*0Sstevel@tonic-gate return (B_FALSE);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate thisp = data;
78*0Sstevel@tonic-gate lastp = IV;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate for (i = 0; i < datalen; i += ch->blocklen) {
81*0Sstevel@tonic-gate cbc_xorblock(lastp, thisp, ch->blocklen);
82*0Sstevel@tonic-gate /* Encrypt the current block. */
83*0Sstevel@tonic-gate ch->encrypt(ch->ks, thisp);
84*0Sstevel@tonic-gate lastp = thisp;
85*0Sstevel@tonic-gate thisp += ch->blocklen;
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate bcopy(lastp, IV, ch->blocklen);
89*0Sstevel@tonic-gate return (B_TRUE);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate boolean_t
cbc_decrypt(cbc_handle_t * ch,uint8_t * data,size_t datalen,uint8_t * IV)93*0Sstevel@tonic-gate cbc_decrypt(cbc_handle_t *ch, uint8_t *data, size_t datalen,
94*0Sstevel@tonic-gate uint8_t *IV)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate uint8_t cbcblock[CBC_MAX_BLOCK_SIZE];
97*0Sstevel@tonic-gate uint8_t *lastp;
98*0Sstevel@tonic-gate uint8_t *thisp;
99*0Sstevel@tonic-gate size_t i;
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate if (!IS_P2ALIGNED(datalen, ch->blocklen)) {
102*0Sstevel@tonic-gate return (B_FALSE);
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate thisp = data;
106*0Sstevel@tonic-gate lastp = IV;
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate for (i = 0; i < datalen; i += ch->blocklen) {
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate /* Copy the current ciphertext block. */
111*0Sstevel@tonic-gate bcopy(thisp, cbcblock, ch->blocklen);
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate /* Decrypt the current block. */
114*0Sstevel@tonic-gate ch->decrypt(ch->ks, thisp);
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate cbc_xorblock(lastp, thisp, ch->blocklen);
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /* Save the last ciphertext block. */
119*0Sstevel@tonic-gate bcopy(cbcblock, lastp, ch->blocklen);
120*0Sstevel@tonic-gate thisp += ch->blocklen;
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate return (B_TRUE);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate void
cbc_makehandle(cbc_handle_t * ch,void * cookie,uint32_t keysize,uint32_t blocksize,uint32_t ivsize,void (* encrypt)(void *,uint8_t *),void (* decrypt)(void *,uint8_t *))127*0Sstevel@tonic-gate cbc_makehandle(cbc_handle_t *ch, void *cookie, uint32_t keysize,
128*0Sstevel@tonic-gate uint32_t blocksize, uint32_t ivsize,
129*0Sstevel@tonic-gate void (*encrypt)(void *, uint8_t *),
130*0Sstevel@tonic-gate void (*decrypt)(void *, uint8_t *))
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate ch->ks = cookie;
133*0Sstevel@tonic-gate ch->keylen = keysize;
134*0Sstevel@tonic-gate ch->blocklen = blocksize;
135*0Sstevel@tonic-gate ch->ivlen = ivsize;
136*0Sstevel@tonic-gate ch->encrypt = encrypt;
137*0Sstevel@tonic-gate ch->decrypt = decrypt;
138*0Sstevel@tonic-gate }
139