xref: /freebsd/sys/crypto/via/padlock_cipher.c (revision 652a9748855320619e075c4e83aef2f5294412d2)
1 /*-
2  * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2004 Mark R V Murray
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*	$OpenBSD: via.c,v 1.3 2004/06/15 23:36:55 deraadt Exp $	*/
29 /*-
30  * Copyright (c) 2003 Jason Wright
31  * Copyright (c) 2003, 2004 Theo de Raadt
32  * All rights reserved.
33  *
34  * Permission to use, copy, modify, and distribute this software for any
35  * purpose with or without fee is hereby granted, provided that the above
36  * copyright notice and this permission notice appear in all copies.
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
39  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
40  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
41  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
42  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
43  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
44  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45  */
46 
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/module.h>
54 #include <sys/malloc.h>
55 #include <sys/libkern.h>
56 #include <sys/pcpu.h>
57 #include <sys/uio.h>
58 
59 #include <opencrypto/cryptodev.h>
60 #include <crypto/rijndael/rijndael.h>
61 
62 #include <crypto/via/padlock.h>
63 
64 #define	PADLOCK_ROUND_COUNT_AES128	10
65 #define	PADLOCK_ROUND_COUNT_AES192	12
66 #define	PADLOCK_ROUND_COUNT_AES256	14
67 
68 #define	PADLOCK_ALGORITHM_TYPE_AES	0
69 
70 #define	PADLOCK_KEY_GENERATION_HW	0
71 #define	PADLOCK_KEY_GENERATION_SW	1
72 
73 #define	PADLOCK_DIRECTION_ENCRYPT	0
74 #define	PADLOCK_DIRECTION_DECRYPT	1
75 
76 #define	PADLOCK_KEY_SIZE_128	0
77 #define	PADLOCK_KEY_SIZE_192	1
78 #define	PADLOCK_KEY_SIZE_256	2
79 
80 MALLOC_DECLARE(M_PADLOCK);
81 
82 static __inline void
83 padlock_cbc(void *in, void *out, size_t count, void *key, union padlock_cw *cw,
84     void *iv)
85 {
86 #ifdef __GNUCLIKE_ASM
87 	/* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
88 	__asm __volatile(
89 		"pushf				\n\t"
90 		"popf				\n\t"
91 		"rep				\n\t"
92 		".byte	0x0f, 0xa7, 0xd0"
93 			: "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
94 			: "b" (key), "d" (cw)
95 			: "cc", "memory"
96 		);
97 #endif
98 }
99 
100 static void
101 padlock_cipher_key_setup(struct padlock_session *ses, const void *key, int klen)
102 {
103 	union padlock_cw *cw;
104 	int i;
105 
106 	cw = &ses->ses_cw;
107 	if (cw->cw_key_generation == PADLOCK_KEY_GENERATION_SW) {
108 		/* Build expanded keys for both directions */
109 		rijndaelKeySetupEnc(ses->ses_ekey, key, klen * 8);
110 		rijndaelKeySetupDec(ses->ses_dkey, key, klen * 8);
111 		for (i = 0; i < 4 * (RIJNDAEL_MAXNR + 1); i++) {
112 			ses->ses_ekey[i] = ntohl(ses->ses_ekey[i]);
113 			ses->ses_dkey[i] = ntohl(ses->ses_dkey[i]);
114 		}
115 	} else {
116 		bcopy(key, ses->ses_ekey, klen);
117 		bcopy(key, ses->ses_dkey, klen);
118 	}
119 }
120 
121 int
122 padlock_cipher_setup(struct padlock_session *ses,
123     const struct crypto_session_params *csp)
124 {
125 	union padlock_cw *cw;
126 
127 	if (csp->csp_cipher_klen != 16 && csp->csp_cipher_klen != 25 &&
128 	    csp->csp_cipher_klen != 32) {
129 		return (EINVAL);
130 	}
131 
132 	cw = &ses->ses_cw;
133 	bzero(cw, sizeof(*cw));
134 	cw->cw_algorithm_type = PADLOCK_ALGORITHM_TYPE_AES;
135 	cw->cw_key_generation = PADLOCK_KEY_GENERATION_SW;
136 	cw->cw_intermediate = 0;
137 	switch (csp->csp_cipher_klen * 8) {
138 	case 128:
139 		cw->cw_round_count = PADLOCK_ROUND_COUNT_AES128;
140 		cw->cw_key_size = PADLOCK_KEY_SIZE_128;
141 #ifdef HW_KEY_GENERATION
142 		/* This doesn't buy us much, that's why it is commented out. */
143 		cw->cw_key_generation = PADLOCK_KEY_GENERATION_HW;
144 #endif
145 		break;
146 	case 192:
147 		cw->cw_round_count = PADLOCK_ROUND_COUNT_AES192;
148 		cw->cw_key_size = PADLOCK_KEY_SIZE_192;
149 		break;
150 	case 256:
151 		cw->cw_round_count = PADLOCK_ROUND_COUNT_AES256;
152 		cw->cw_key_size = PADLOCK_KEY_SIZE_256;
153 		break;
154 	}
155 	if (csp->csp_cipher_key != NULL) {
156 		padlock_cipher_key_setup(ses, csp->csp_cipher_key,
157 		    csp->csp_cipher_klen);
158 	}
159 	return (0);
160 }
161 
162 /*
163  * Function checks if the given buffer is already 16 bytes aligned.
164  * If it is there is no need to allocate new buffer.
165  * If it isn't, new buffer is allocated.
166  */
167 static u_char *
168 padlock_cipher_alloc(struct cryptop *crp, int *allocated)
169 {
170 	u_char *addr;
171 
172 	switch (crp->crp_buf_type) {
173 	case CRYPTO_BUF_MBUF:
174 		break;
175 	case CRYPTO_BUF_UIO: {
176 		struct uio *uio;
177 		struct iovec *iov;
178 
179 		uio = crp->crp_uio;
180 		if (uio->uio_iovcnt != 1)
181 			break;
182 		iov = uio->uio_iov;
183 		addr = (u_char *)iov->iov_base + crp->crp_payload_start;
184 		if (((uintptr_t)addr & 0xf) != 0) /* 16 bytes aligned? */
185 			break;
186 		*allocated = 0;
187 		return (addr);
188 	}
189 	case CRYPTO_BUF_CONTIG:
190 		addr = (u_char *)crp->crp_buf + crp->crp_payload_start;
191 		if (((uintptr_t)addr & 0xf) != 0) /* 16 bytes aligned? */
192 			break;
193 		*allocated = 0;
194 		return (addr);
195 	}
196 
197 	*allocated = 1;
198 	addr = malloc(crp->crp_payload_length + 16, M_PADLOCK, M_NOWAIT);
199 	return (addr);
200 }
201 
202 int
203 padlock_cipher_process(struct padlock_session *ses, struct cryptop *crp,
204     const struct crypto_session_params *csp)
205 {
206 	union padlock_cw *cw;
207 	struct thread *td;
208 	u_char *buf, *abuf;
209 	uint32_t *key;
210 	uint8_t iv[AES_BLOCK_LEN] __aligned(16);
211 	int allocated;
212 
213 	buf = padlock_cipher_alloc(crp, &allocated);
214 	if (buf == NULL)
215 		return (ENOMEM);
216 	/* Buffer has to be 16 bytes aligned. */
217 	abuf = PADLOCK_ALIGN(buf);
218 
219 	if (crp->crp_cipher_key != NULL) {
220 		padlock_cipher_key_setup(ses, crp->crp_cipher_key,
221 		    csp->csp_cipher_klen);
222 	}
223 
224 	cw = &ses->ses_cw;
225 	cw->cw_filler0 = 0;
226 	cw->cw_filler1 = 0;
227 	cw->cw_filler2 = 0;
228 	cw->cw_filler3 = 0;
229 
230 	if (crp->crp_flags & CRYPTO_F_IV_GENERATE) {
231 		arc4rand(iv, AES_BLOCK_LEN, 0);
232 		crypto_copyback(crp, crp->crp_iv_start, AES_BLOCK_LEN, iv);
233 	} else if (crp->crp_flags & CRYPTO_F_IV_SEPARATE)
234 		memcpy(iv, crp->crp_iv, AES_BLOCK_LEN);
235 	else
236 		crypto_copydata(crp, crp->crp_iv_start, AES_BLOCK_LEN, iv);
237 
238 	if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
239 		cw->cw_direction = PADLOCK_DIRECTION_ENCRYPT;
240 		key = ses->ses_ekey;
241 	} else {
242 		cw->cw_direction = PADLOCK_DIRECTION_DECRYPT;
243 		key = ses->ses_dkey;
244 	}
245 
246 	if (allocated) {
247 		crypto_copydata(crp, crp->crp_payload_start,
248 		    crp->crp_payload_length, abuf);
249 	}
250 
251 	td = curthread;
252 	fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL | FPU_KERN_KTHR);
253 	padlock_cbc(abuf, abuf, crp->crp_payload_length / AES_BLOCK_LEN, key,
254 	    cw, iv);
255 	fpu_kern_leave(td, ses->ses_fpu_ctx);
256 
257 	if (allocated) {
258 		crypto_copyback(crp, crp->crp_payload_start,
259 		    crp->crp_payload_length, abuf);
260 
261 		explicit_bzero(buf, crp->crp_payload_length + 16);
262 		free(buf, M_PADLOCK);
263 	}
264 	return (0);
265 }
266