4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2002-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/sysmacros.h>
30 #if defined(_KERNEL) && !defined(_BOOT)
31 #include <sys/systm.h>
37 #define CBC_MAX_BLOCK_SIZE 64
40 cbc_xorblock(uint8_t *lastp
, uint8_t *thisp
, int blocksize
)
46 if (IS_P2ALIGNED(thisp
, sizeof (uint32_t)) &&
47 IS_P2ALIGNED(lastp
, sizeof (uint32_t)) &&
48 IS_P2ALIGNED(blocksize
, sizeof (uint32_t))) {
50 this32p
= (uint32_t *)thisp
;
52 last32p
= (uint32_t *)lastp
;
53 for (i
= 0; i
< blocksize
; i
+= 4) {
59 for (i
= 0; i
< blocksize
; i
++) {
66 cbc_encrypt(cbc_handle_t
*ch
, uint8_t *data
, size_t datalen
,
73 if (!IS_P2ALIGNED(datalen
, ch
->blocklen
)) {
80 for (i
= 0; i
< datalen
; i
+= ch
->blocklen
) {
81 cbc_xorblock(lastp
, thisp
, ch
->blocklen
);
82 /* Encrypt the current block. */
83 ch
->encrypt(ch
->ks
, thisp
);
85 thisp
+= ch
->blocklen
;
88 bcopy(lastp
, IV
, ch
->blocklen
);
93 cbc_decrypt(cbc_handle_t
*ch
, uint8_t *data
, size_t datalen
,
96 uint8_t cbcblock
[CBC_MAX_BLOCK_SIZE
];
101 if (!IS_P2ALIGNED(datalen
, ch
->blocklen
)) {
108 for (i
= 0; i
< datalen
; i
+= ch
->blocklen
) {
110 /* Copy the current ciphertext block. */
111 bcopy(thisp
, cbcblock
, ch
->blocklen
);
113 /* Decrypt the current block. */
114 ch
->decrypt(ch
->ks
, thisp
);
116 cbc_xorblock(lastp
, thisp
, ch
->blocklen
);
118 /* Save the last ciphertext block. */
119 bcopy(cbcblock
, lastp
, ch
->blocklen
);
120 thisp
+= ch
->blocklen
;
127 cbc_makehandle(cbc_handle_t
*ch
, void *cookie
, uint32_t keysize
,
128 uint32_t blocksize
, uint32_t ivsize
,
129 void (*encrypt
)(void *, uint8_t *),
130 void (*decrypt
)(void *, uint8_t *))
133 ch
->keylen
= keysize
;
134 ch
->blocklen
= blocksize
;
136 ch
->encrypt
= encrypt
;
137 ch
->decrypt
= decrypt
;