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