xref: /freebsd/sys/crypto/via/padlock.c (revision c0341432)
1 /*-
2  * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/lock.h>
35 #include <sys/rwlock.h>
36 #include <sys/malloc.h>
37 #include <sys/libkern.h>
38 #if defined(__amd64__) || defined(__i386__)
39 #include <machine/cpufunc.h>
40 #include <machine/cputypes.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
43 #endif
44 
45 #include <opencrypto/cryptodev.h>
46 
47 #include <crypto/via/padlock.h>
48 
49 #include <sys/kobj.h>
50 #include <sys/bus.h>
51 #include "cryptodev_if.h"
52 
53 /*
54  * Technical documentation about the PadLock engine can be found here:
55  *
56  * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
57  */
58 
59 struct padlock_softc {
60 	int32_t		sc_cid;
61 };
62 
63 static int padlock_probesession(device_t, const struct crypto_session_params *);
64 static int padlock_newsession(device_t, crypto_session_t cses,
65     const struct crypto_session_params *);
66 static void padlock_freesession(device_t, crypto_session_t cses);
67 static void padlock_freesession_one(struct padlock_softc *sc,
68     struct padlock_session *ses);
69 static int padlock_process(device_t, struct cryptop *crp, int hint __unused);
70 
71 MALLOC_DEFINE(M_PADLOCK, "padlock_data", "PadLock Data");
72 
73 static void
74 padlock_identify(driver_t *drv, device_t parent)
75 {
76 	/* NB: order 10 is so we get attached after h/w devices */
77 	if (device_find_child(parent, "padlock", -1) == NULL &&
78 	    BUS_ADD_CHILD(parent, 10, "padlock", -1) == 0)
79 		panic("padlock: could not attach");
80 }
81 
82 static int
83 padlock_probe(device_t dev)
84 {
85 	char capp[256];
86 
87 #if defined(__amd64__) || defined(__i386__)
88 	/* If there is no AES support, we has nothing to do here. */
89 	if (!(via_feature_xcrypt & VIA_HAS_AES)) {
90 		device_printf(dev, "No ACE support.\n");
91 		return (EINVAL);
92 	}
93 	strlcpy(capp, "AES-CBC", sizeof(capp));
94 #if 0
95 	strlcat(capp, ",AES-EBC", sizeof(capp));
96 	strlcat(capp, ",AES-CFB", sizeof(capp));
97 	strlcat(capp, ",AES-OFB", sizeof(capp));
98 #endif
99 	if (via_feature_xcrypt & VIA_HAS_SHA) {
100 		strlcat(capp, ",SHA1", sizeof(capp));
101 		strlcat(capp, ",SHA256", sizeof(capp));
102 	}
103 #if 0
104 	if (via_feature_xcrypt & VIA_HAS_AESCTR)
105 		strlcat(capp, ",AES-CTR", sizeof(capp));
106 	if (via_feature_xcrypt & VIA_HAS_MM)
107 		strlcat(capp, ",RSA", sizeof(capp));
108 #endif
109 	device_set_desc_copy(dev, capp);
110 	return (0);
111 #else
112 	return (EINVAL);
113 #endif
114 }
115 
116 static int
117 padlock_attach(device_t dev)
118 {
119 	struct padlock_softc *sc = device_get_softc(dev);
120 
121 	sc->sc_cid = crypto_get_driverid(dev, sizeof(struct padlock_session),
122 	    CRYPTOCAP_F_HARDWARE);
123 	if (sc->sc_cid < 0) {
124 		device_printf(dev, "Could not get crypto driver id.\n");
125 		return (ENOMEM);
126 	}
127 
128 	return (0);
129 }
130 
131 static int
132 padlock_detach(device_t dev)
133 {
134 	struct padlock_softc *sc = device_get_softc(dev);
135 
136 	crypto_unregister_all(sc->sc_cid);
137 	return (0);
138 }
139 
140 static int
141 padlock_probesession(device_t dev, const struct crypto_session_params *csp)
142 {
143 
144 	if (csp->csp_flags != 0)
145 		return (EINVAL);
146 
147 	/*
148 	 * We only support HMAC algorithms to be able to work with
149 	 * ipsec(4), so if we are asked only for authentication without
150 	 * encryption, don't pretend we can accelerate it.
151 	 *
152 	 * XXX: For CPUs with SHA instructions we should probably
153 	 * permit CSP_MODE_DIGEST so that those can be tested.
154 	 */
155 	switch (csp->csp_mode) {
156 	case CSP_MODE_ETA:
157 		if (!padlock_hash_check(csp))
158 			return (EINVAL);
159 		/* FALLTHROUGH */
160 	case CSP_MODE_CIPHER:
161 		switch (csp->csp_cipher_alg) {
162 		case CRYPTO_AES_CBC:
163 			if (csp->csp_ivlen != AES_BLOCK_LEN)
164 				return (EINVAL);
165 			break;
166 		default:
167 			return (EINVAL);
168 		}
169 		break;
170 	default:
171 		return (EINVAL);
172 	}
173 
174 	return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
175 }
176 
177 static int
178 padlock_newsession(device_t dev, crypto_session_t cses,
179     const struct crypto_session_params *csp)
180 {
181 	struct padlock_softc *sc = device_get_softc(dev);
182 	struct padlock_session *ses = NULL;
183 	struct thread *td;
184 	int error;
185 
186 	ses = crypto_get_driver_session(cses);
187 	ses->ses_fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
188 
189 	error = padlock_cipher_setup(ses, csp);
190 	if (error != 0) {
191 		padlock_freesession_one(sc, ses);
192 		return (error);
193 	}
194 
195 	if (csp->csp_mode == CSP_MODE_ETA) {
196 		td = curthread;
197 		fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL |
198 		    FPU_KERN_KTHR);
199 		error = padlock_hash_setup(ses, csp);
200 		fpu_kern_leave(td, ses->ses_fpu_ctx);
201 		if (error != 0) {
202 			padlock_freesession_one(sc, ses);
203 			return (error);
204 		}
205 	}
206 
207 	return (0);
208 }
209 
210 static void
211 padlock_freesession_one(struct padlock_softc *sc, struct padlock_session *ses)
212 {
213 
214 	padlock_hash_free(ses);
215 	fpu_kern_free_ctx(ses->ses_fpu_ctx);
216 }
217 
218 static void
219 padlock_freesession(device_t dev, crypto_session_t cses)
220 {
221 	struct padlock_softc *sc = device_get_softc(dev);
222 	struct padlock_session *ses;
223 
224 	ses = crypto_get_driver_session(cses);
225 	padlock_freesession_one(sc, ses);
226 }
227 
228 static int
229 padlock_process(device_t dev, struct cryptop *crp, int hint __unused)
230 {
231 	const struct crypto_session_params *csp;
232 	struct padlock_session *ses;
233 	int error;
234 
235 	if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
236 		error = EINVAL;
237 		goto out;
238 	}
239 
240 	ses = crypto_get_driver_session(crp->crp_session);
241 	csp = crypto_get_params(crp->crp_session);
242 
243 	/* Perform data authentication if requested before decryption. */
244 	if (csp->csp_mode == CSP_MODE_ETA &&
245 	    !CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
246 		error = padlock_hash_process(ses, crp, csp);
247 		if (error != 0)
248 			goto out;
249 	}
250 
251 	error = padlock_cipher_process(ses, crp, csp);
252 	if (error != 0)
253 		goto out;
254 
255 	/* Perform data authentication if requested after encryption. */
256 	if (csp->csp_mode == CSP_MODE_ETA &&
257 	    CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
258 		error = padlock_hash_process(ses, crp, csp);
259 		if (error != 0)
260 			goto out;
261 	}
262 
263 out:
264 #if 0
265 	/*
266 	 * This code is not necessary, because contexts will be freed on next
267 	 * padlock_setup_mackey() call or at padlock_freesession() call.
268 	 */
269 	if (ses != NULL && maccrd != NULL &&
270 	    (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
271 		padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
272 		padlock_free_ctx(ses->ses_axf, ses->ses_octx);
273 	}
274 #endif
275 	crp->crp_etype = error;
276 	crypto_done(crp);
277 	return (error);
278 }
279 
280 static device_method_t padlock_methods[] = {
281 	DEVMETHOD(device_identify,	padlock_identify),
282 	DEVMETHOD(device_probe,		padlock_probe),
283 	DEVMETHOD(device_attach,	padlock_attach),
284 	DEVMETHOD(device_detach,	padlock_detach),
285 
286 	DEVMETHOD(cryptodev_probesession, padlock_probesession),
287 	DEVMETHOD(cryptodev_newsession,	padlock_newsession),
288 	DEVMETHOD(cryptodev_freesession,padlock_freesession),
289 	DEVMETHOD(cryptodev_process,	padlock_process),
290 
291 	{0, 0},
292 };
293 
294 static driver_t padlock_driver = {
295 	"padlock",
296 	padlock_methods,
297 	sizeof(struct padlock_softc),
298 };
299 static devclass_t padlock_devclass;
300 
301 /* XXX where to attach */
302 DRIVER_MODULE(padlock, nexus, padlock_driver, padlock_devclass, 0, 0);
303 MODULE_VERSION(padlock, 1);
304 MODULE_DEPEND(padlock, crypto, 1, 1, 1);
305