1*9392Sopensolaris@drydog.com /*
2*9392Sopensolaris@drydog.com * CDDL HEADER START
3*9392Sopensolaris@drydog.com *
4*9392Sopensolaris@drydog.com * The contents of this file are subject to the terms of the
5*9392Sopensolaris@drydog.com * Common Development and Distribution License (the "License").
6*9392Sopensolaris@drydog.com * You may not use this file except in compliance with the License.
7*9392Sopensolaris@drydog.com *
8*9392Sopensolaris@drydog.com * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9392Sopensolaris@drydog.com * or http://www.opensolaris.org/os/licensing.
10*9392Sopensolaris@drydog.com * See the License for the specific language governing permissions
11*9392Sopensolaris@drydog.com * and limitations under the License.
12*9392Sopensolaris@drydog.com *
13*9392Sopensolaris@drydog.com * When distributing Covered Code, include this CDDL HEADER in each
14*9392Sopensolaris@drydog.com * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9392Sopensolaris@drydog.com * If applicable, add the following below this CDDL HEADER, with the
16*9392Sopensolaris@drydog.com * fields enclosed by brackets "[]" replaced with your own identifying
17*9392Sopensolaris@drydog.com * information: Portions Copyright [yyyy] [name of copyright owner]
18*9392Sopensolaris@drydog.com *
19*9392Sopensolaris@drydog.com * CDDL HEADER END
20*9392Sopensolaris@drydog.com */
21*9392Sopensolaris@drydog.com /*
22*9392Sopensolaris@drydog.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23*9392Sopensolaris@drydog.com * Use is subject to license terms.
24*9392Sopensolaris@drydog.com */
25*9392Sopensolaris@drydog.com
26*9392Sopensolaris@drydog.com #include <sys/types.h>
27*9392Sopensolaris@drydog.com #include <sys/sysmacros.h>
28*9392Sopensolaris@drydog.com #include <modes/modes.h>
29*9392Sopensolaris@drydog.com #include "aes_impl.h"
30*9392Sopensolaris@drydog.com #ifndef _KERNEL
31*9392Sopensolaris@drydog.com #include <stdlib.h>
32*9392Sopensolaris@drydog.com #endif /* !_KERNEL */
33*9392Sopensolaris@drydog.com
34*9392Sopensolaris@drydog.com
35*9392Sopensolaris@drydog.com /* Copy a 16-byte AES block from "in" to "out" */
36*9392Sopensolaris@drydog.com void
aes_copy_block(uint8_t * in,uint8_t * out)37*9392Sopensolaris@drydog.com aes_copy_block(uint8_t *in, uint8_t *out)
38*9392Sopensolaris@drydog.com {
39*9392Sopensolaris@drydog.com if (IS_P2ALIGNED2(in, out, sizeof (uint32_t))) {
40*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
41*9392Sopensolaris@drydog.com *(uint32_t *)&out[0] = *(uint32_t *)&in[0];
42*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
43*9392Sopensolaris@drydog.com *(uint32_t *)&out[4] = *(uint32_t *)&in[4];
44*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
45*9392Sopensolaris@drydog.com *(uint32_t *)&out[8] = *(uint32_t *)&in[8];
46*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
47*9392Sopensolaris@drydog.com *(uint32_t *)&out[12] = *(uint32_t *)&in[12];
48*9392Sopensolaris@drydog.com } else {
49*9392Sopensolaris@drydog.com AES_COPY_BLOCK(in, out);
50*9392Sopensolaris@drydog.com }
51*9392Sopensolaris@drydog.com }
52*9392Sopensolaris@drydog.com
53*9392Sopensolaris@drydog.com
54*9392Sopensolaris@drydog.com /* XOR a 16-byte AES block of data into dst */
55*9392Sopensolaris@drydog.com void
aes_xor_block(uint8_t * data,uint8_t * dst)56*9392Sopensolaris@drydog.com aes_xor_block(uint8_t *data, uint8_t *dst)
57*9392Sopensolaris@drydog.com {
58*9392Sopensolaris@drydog.com if (IS_P2ALIGNED2(dst, data, sizeof (uint32_t))) {
59*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
60*9392Sopensolaris@drydog.com *(uint32_t *)&dst[0] ^= *(uint32_t *)&data[0];
61*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
62*9392Sopensolaris@drydog.com *(uint32_t *)&dst[4] ^= *(uint32_t *)&data[4];
63*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
64*9392Sopensolaris@drydog.com *(uint32_t *)&dst[8] ^= *(uint32_t *)&data[8];
65*9392Sopensolaris@drydog.com /* LINTED: pointer alignment */
66*9392Sopensolaris@drydog.com *(uint32_t *)&dst[12] ^= *(uint32_t *)&data[12];
67*9392Sopensolaris@drydog.com } else {
68*9392Sopensolaris@drydog.com AES_XOR_BLOCK(data, dst);
69*9392Sopensolaris@drydog.com }
70*9392Sopensolaris@drydog.com }
71*9392Sopensolaris@drydog.com
72*9392Sopensolaris@drydog.com
73*9392Sopensolaris@drydog.com /*
74*9392Sopensolaris@drydog.com * Encrypt multiple blocks of data according to mode.
75*9392Sopensolaris@drydog.com */
76*9392Sopensolaris@drydog.com int
aes_encrypt_contiguous_blocks(void * ctx,char * data,size_t length,crypto_data_t * out)77*9392Sopensolaris@drydog.com aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length,
78*9392Sopensolaris@drydog.com crypto_data_t *out)
79*9392Sopensolaris@drydog.com {
80*9392Sopensolaris@drydog.com aes_ctx_t *aes_ctx = ctx;
81*9392Sopensolaris@drydog.com int rv;
82*9392Sopensolaris@drydog.com
83*9392Sopensolaris@drydog.com if (aes_ctx->ac_flags & CTR_MODE) {
84*9392Sopensolaris@drydog.com rv = ctr_mode_contiguous_blocks(ctx, data, length, out,
85*9392Sopensolaris@drydog.com AES_BLOCK_LEN, aes_encrypt_block, aes_xor_block);
86*9392Sopensolaris@drydog.com #ifdef _KERNEL
87*9392Sopensolaris@drydog.com } else if (aes_ctx->ac_flags & CCM_MODE) {
88*9392Sopensolaris@drydog.com rv = ccm_mode_encrypt_contiguous_blocks(ctx, data, length,
89*9392Sopensolaris@drydog.com out, AES_BLOCK_LEN, aes_encrypt_block, aes_copy_block,
90*9392Sopensolaris@drydog.com aes_xor_block);
91*9392Sopensolaris@drydog.com } else if (aes_ctx->ac_flags & (GCM_MODE|GMAC_MODE)) {
92*9392Sopensolaris@drydog.com rv = gcm_mode_encrypt_contiguous_blocks(ctx, data, length,
93*9392Sopensolaris@drydog.com out, AES_BLOCK_LEN, aes_encrypt_block, aes_copy_block,
94*9392Sopensolaris@drydog.com aes_xor_block);
95*9392Sopensolaris@drydog.com #endif
96*9392Sopensolaris@drydog.com } else if (aes_ctx->ac_flags & CBC_MODE) {
97*9392Sopensolaris@drydog.com rv = cbc_encrypt_contiguous_blocks(ctx,
98*9392Sopensolaris@drydog.com data, length, out, AES_BLOCK_LEN, aes_encrypt_block,
99*9392Sopensolaris@drydog.com aes_copy_block, aes_xor_block);
100*9392Sopensolaris@drydog.com } else {
101*9392Sopensolaris@drydog.com rv = ecb_cipher_contiguous_blocks(ctx, data, length, out,
102*9392Sopensolaris@drydog.com AES_BLOCK_LEN, aes_encrypt_block);
103*9392Sopensolaris@drydog.com }
104*9392Sopensolaris@drydog.com return (rv);
105*9392Sopensolaris@drydog.com }
106*9392Sopensolaris@drydog.com
107*9392Sopensolaris@drydog.com
108*9392Sopensolaris@drydog.com /*
109*9392Sopensolaris@drydog.com * Decrypt multiple blocks of data according to mode.
110*9392Sopensolaris@drydog.com */
111*9392Sopensolaris@drydog.com int
aes_decrypt_contiguous_blocks(void * ctx,char * data,size_t length,crypto_data_t * out)112*9392Sopensolaris@drydog.com aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length,
113*9392Sopensolaris@drydog.com crypto_data_t *out)
114*9392Sopensolaris@drydog.com {
115*9392Sopensolaris@drydog.com aes_ctx_t *aes_ctx = ctx;
116*9392Sopensolaris@drydog.com int rv;
117*9392Sopensolaris@drydog.com
118*9392Sopensolaris@drydog.com if (aes_ctx->ac_flags & CTR_MODE) {
119*9392Sopensolaris@drydog.com rv = ctr_mode_contiguous_blocks(ctx, data, length, out,
120*9392Sopensolaris@drydog.com AES_BLOCK_LEN, aes_encrypt_block, aes_xor_block);
121*9392Sopensolaris@drydog.com if (rv == CRYPTO_DATA_LEN_RANGE)
122*9392Sopensolaris@drydog.com rv = CRYPTO_ENCRYPTED_DATA_LEN_RANGE;
123*9392Sopensolaris@drydog.com #ifdef _KERNEL
124*9392Sopensolaris@drydog.com } else if (aes_ctx->ac_flags & CCM_MODE) {
125*9392Sopensolaris@drydog.com rv = ccm_mode_decrypt_contiguous_blocks(ctx, data, length,
126*9392Sopensolaris@drydog.com out, AES_BLOCK_LEN, aes_encrypt_block, aes_copy_block,
127*9392Sopensolaris@drydog.com aes_xor_block);
128*9392Sopensolaris@drydog.com } else if (aes_ctx->ac_flags & (GCM_MODE|GMAC_MODE)) {
129*9392Sopensolaris@drydog.com rv = gcm_mode_decrypt_contiguous_blocks(ctx, data, length,
130*9392Sopensolaris@drydog.com out, AES_BLOCK_LEN, aes_encrypt_block, aes_copy_block,
131*9392Sopensolaris@drydog.com aes_xor_block);
132*9392Sopensolaris@drydog.com #endif
133*9392Sopensolaris@drydog.com } else if (aes_ctx->ac_flags & CBC_MODE) {
134*9392Sopensolaris@drydog.com rv = cbc_decrypt_contiguous_blocks(ctx, data, length, out,
135*9392Sopensolaris@drydog.com AES_BLOCK_LEN, aes_decrypt_block, aes_copy_block,
136*9392Sopensolaris@drydog.com aes_xor_block);
137*9392Sopensolaris@drydog.com } else {
138*9392Sopensolaris@drydog.com rv = ecb_cipher_contiguous_blocks(ctx, data, length, out,
139*9392Sopensolaris@drydog.com AES_BLOCK_LEN, aes_decrypt_block);
140*9392Sopensolaris@drydog.com if (rv == CRYPTO_DATA_LEN_RANGE)
141*9392Sopensolaris@drydog.com rv = CRYPTO_ENCRYPTED_DATA_LEN_RANGE;
142*9392Sopensolaris@drydog.com }
143*9392Sopensolaris@drydog.com return (rv);
144*9392Sopensolaris@drydog.com }
145