xref: /freebsd/sys/crypto/via/padlock.c (revision d0b2dbfa)
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 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/lock.h>
33 #include <sys/rwlock.h>
34 #include <sys/malloc.h>
35 #include <sys/libkern.h>
36 #if defined(__amd64__) || defined(__i386__)
37 #include <machine/cpufunc.h>
38 #include <machine/cputypes.h>
39 #include <machine/md_var.h>
40 #include <machine/specialreg.h>
41 #endif
42 
43 #include <opencrypto/cryptodev.h>
44 
45 #include <crypto/via/padlock.h>
46 
47 #include <sys/kobj.h>
48 #include <sys/bus.h>
49 #include "cryptodev_if.h"
50 
51 /*
52  * Technical documentation about the PadLock engine can be found here:
53  *
54  * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
55  */
56 
57 struct padlock_softc {
58 	int32_t		sc_cid;
59 };
60 
61 static int padlock_probesession(device_t, const struct crypto_session_params *);
62 static int padlock_newsession(device_t, crypto_session_t cses,
63     const struct crypto_session_params *);
64 static void padlock_freesession(device_t, crypto_session_t cses);
65 static void padlock_freesession_one(struct padlock_softc *sc,
66     struct padlock_session *ses);
67 static int padlock_process(device_t, struct cryptop *crp, int hint __unused);
68 
69 MALLOC_DEFINE(M_PADLOCK, "padlock_data", "PadLock Data");
70 
71 static void
72 padlock_identify(driver_t *drv, device_t parent)
73 {
74 	/* NB: order 10 is so we get attached after h/w devices */
75 	if (device_find_child(parent, "padlock", -1) == NULL &&
76 	    BUS_ADD_CHILD(parent, 10, "padlock", -1) == 0)
77 		panic("padlock: could not attach");
78 }
79 
80 static int
81 padlock_probe(device_t dev)
82 {
83 	char capp[256];
84 
85 #if defined(__amd64__) || defined(__i386__)
86 	/* If there is no AES support, we has nothing to do here. */
87 	if (!(via_feature_xcrypt & VIA_HAS_AES)) {
88 		device_printf(dev, "No ACE support.\n");
89 		return (EINVAL);
90 	}
91 	strlcpy(capp, "AES-CBC", sizeof(capp));
92 #if 0
93 	strlcat(capp, ",AES-EBC", sizeof(capp));
94 	strlcat(capp, ",AES-CFB", sizeof(capp));
95 	strlcat(capp, ",AES-OFB", sizeof(capp));
96 #endif
97 	if (via_feature_xcrypt & VIA_HAS_SHA) {
98 		strlcat(capp, ",SHA1", sizeof(capp));
99 		strlcat(capp, ",SHA256", sizeof(capp));
100 	}
101 #if 0
102 	if (via_feature_xcrypt & VIA_HAS_AESCTR)
103 		strlcat(capp, ",AES-CTR", sizeof(capp));
104 	if (via_feature_xcrypt & VIA_HAS_MM)
105 		strlcat(capp, ",RSA", sizeof(capp));
106 #endif
107 	device_set_desc_copy(dev, capp);
108 	return (0);
109 #else
110 	return (EINVAL);
111 #endif
112 }
113 
114 static int
115 padlock_attach(device_t dev)
116 {
117 	struct padlock_softc *sc = device_get_softc(dev);
118 
119 	sc->sc_cid = crypto_get_driverid(dev, sizeof(struct padlock_session),
120 	    CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC |
121 	    CRYPTOCAP_F_ACCEL_SOFTWARE);
122 	if (sc->sc_cid < 0) {
123 		device_printf(dev, "Could not get crypto driver id.\n");
124 		return (ENOMEM);
125 	}
126 
127 	return (0);
128 }
129 
130 static int
131 padlock_detach(device_t dev)
132 {
133 	struct padlock_softc *sc = device_get_softc(dev);
134 
135 	crypto_unregister_all(sc->sc_cid);
136 	return (0);
137 }
138 
139 static int
140 padlock_probesession(device_t dev, const struct crypto_session_params *csp)
141 {
142 
143 	if (csp->csp_flags != 0)
144 		return (EINVAL);
145 
146 	/*
147 	 * We only support HMAC algorithms to be able to work with
148 	 * ipsec(4), so if we are asked only for authentication without
149 	 * encryption, don't pretend we can accelerate it.
150 	 *
151 	 * XXX: For CPUs with SHA instructions we should probably
152 	 * permit CSP_MODE_DIGEST so that those can be tested.
153 	 */
154 	switch (csp->csp_mode) {
155 	case CSP_MODE_ETA:
156 		if (!padlock_hash_check(csp))
157 			return (EINVAL);
158 		/* FALLTHROUGH */
159 	case CSP_MODE_CIPHER:
160 		switch (csp->csp_cipher_alg) {
161 		case CRYPTO_AES_CBC:
162 			if (csp->csp_ivlen != AES_BLOCK_LEN)
163 				return (EINVAL);
164 			break;
165 		default:
166 			return (EINVAL);
167 		}
168 		break;
169 	default:
170 		return (EINVAL);
171 	}
172 
173 	return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
174 }
175 
176 static int
177 padlock_newsession(device_t dev, crypto_session_t cses,
178     const struct crypto_session_params *csp)
179 {
180 	struct padlock_softc *sc = device_get_softc(dev);
181 	struct padlock_session *ses = NULL;
182 	struct thread *td;
183 	int error;
184 
185 	ses = crypto_get_driver_session(cses);
186 	ses->ses_fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
187 
188 	error = padlock_cipher_setup(ses, csp);
189 	if (error != 0) {
190 		padlock_freesession_one(sc, ses);
191 		return (error);
192 	}
193 
194 	if (csp->csp_mode == CSP_MODE_ETA) {
195 		td = curthread;
196 		fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL |
197 		    FPU_KERN_KTHR);
198 		error = padlock_hash_setup(ses, csp);
199 		fpu_kern_leave(td, ses->ses_fpu_ctx);
200 		if (error != 0) {
201 			padlock_freesession_one(sc, ses);
202 			return (error);
203 		}
204 	}
205 
206 	return (0);
207 }
208 
209 static void
210 padlock_freesession_one(struct padlock_softc *sc, struct padlock_session *ses)
211 {
212 
213 	padlock_hash_free(ses);
214 	fpu_kern_free_ctx(ses->ses_fpu_ctx);
215 }
216 
217 static void
218 padlock_freesession(device_t dev, crypto_session_t cses)
219 {
220 	struct padlock_softc *sc = device_get_softc(dev);
221 	struct padlock_session *ses;
222 
223 	ses = crypto_get_driver_session(cses);
224 	padlock_freesession_one(sc, ses);
225 }
226 
227 static int
228 padlock_process(device_t dev, struct cryptop *crp, int hint __unused)
229 {
230 	const struct crypto_session_params *csp;
231 	struct padlock_session *ses;
232 	int error;
233 
234 	if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
235 		error = EINVAL;
236 		goto out;
237 	}
238 
239 	ses = crypto_get_driver_session(crp->crp_session);
240 	csp = crypto_get_params(crp->crp_session);
241 
242 	/* Perform data authentication if requested before decryption. */
243 	if (csp->csp_mode == CSP_MODE_ETA &&
244 	    !CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
245 		error = padlock_hash_process(ses, crp, csp);
246 		if (error != 0)
247 			goto out;
248 	}
249 
250 	error = padlock_cipher_process(ses, crp, csp);
251 	if (error != 0)
252 		goto out;
253 
254 	/* Perform data authentication if requested after encryption. */
255 	if (csp->csp_mode == CSP_MODE_ETA &&
256 	    CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
257 		error = padlock_hash_process(ses, crp, csp);
258 		if (error != 0)
259 			goto out;
260 	}
261 
262 out:
263 #if 0
264 	/*
265 	 * This code is not necessary, because contexts will be freed on next
266 	 * padlock_setup_mackey() call or at padlock_freesession() call.
267 	 */
268 	if (ses != NULL && maccrd != NULL &&
269 	    (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
270 		padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
271 		padlock_free_ctx(ses->ses_axf, ses->ses_octx);
272 	}
273 #endif
274 	crp->crp_etype = error;
275 	crypto_done(crp);
276 	return (0);
277 }
278 
279 static device_method_t padlock_methods[] = {
280 	DEVMETHOD(device_identify,	padlock_identify),
281 	DEVMETHOD(device_probe,		padlock_probe),
282 	DEVMETHOD(device_attach,	padlock_attach),
283 	DEVMETHOD(device_detach,	padlock_detach),
284 
285 	DEVMETHOD(cryptodev_probesession, padlock_probesession),
286 	DEVMETHOD(cryptodev_newsession,	padlock_newsession),
287 	DEVMETHOD(cryptodev_freesession,padlock_freesession),
288 	DEVMETHOD(cryptodev_process,	padlock_process),
289 
290 	{0, 0},
291 };
292 
293 static driver_t padlock_driver = {
294 	"padlock",
295 	padlock_methods,
296 	sizeof(struct padlock_softc),
297 };
298 
299 /* XXX where to attach */
300 DRIVER_MODULE(padlock, nexus, padlock_driver, 0, 0);
301 MODULE_VERSION(padlock, 1);
302 MODULE_DEPEND(padlock, crypto, 1, 1, 1);
303