xref: /freebsd/sys/crypto/via/padlock.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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/fpu.h>
40 #include <machine/md_var.h>
41 #include <machine/specialreg.h>
42 #endif
43 
44 #include <opencrypto/cryptodev.h>
45 
46 #include <crypto/via/padlock.h>
47 
48 #include <sys/kobj.h>
49 #include <sys/bus.h>
50 #include "cryptodev_if.h"
51 
52 /*
53  * Technical documentation about the PadLock engine can be found here:
54  *
55  * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
56  */
57 
58 struct padlock_softc {
59 	int32_t		sc_cid;
60 };
61 
62 static int padlock_probesession(device_t, const struct crypto_session_params *);
63 static int padlock_newsession(device_t, crypto_session_t cses,
64     const struct crypto_session_params *);
65 static void padlock_freesession(device_t, crypto_session_t cses);
66 static void padlock_freesession_one(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_session *ses;
181 	struct thread *td;
182 	int error;
183 
184 	ses = crypto_get_driver_session(cses);
185 
186 	error = padlock_cipher_setup(ses, csp);
187 	if (error != 0) {
188 		padlock_freesession_one(ses);
189 		return (error);
190 	}
191 
192 	if (csp->csp_mode == CSP_MODE_ETA) {
193 		td = curthread;
194 		fpu_kern_enter(td, NULL, FPU_KERN_NORMAL | FPU_KERN_NOCTX);
195 		error = padlock_hash_setup(ses, csp);
196 		fpu_kern_leave(td, NULL);
197 		if (error != 0) {
198 			padlock_freesession_one(ses);
199 			return (error);
200 		}
201 	}
202 
203 	return (0);
204 }
205 
206 static void
207 padlock_freesession_one(struct padlock_session *ses)
208 {
209 
210 	padlock_hash_free(ses);
211 }
212 
213 static void
214 padlock_freesession(device_t dev, crypto_session_t cses)
215 {
216 	struct padlock_session *ses;
217 
218 	ses = crypto_get_driver_session(cses);
219 	padlock_freesession_one(ses);
220 }
221 
222 static int
223 padlock_process(device_t dev, struct cryptop *crp, int hint __unused)
224 {
225 	const struct crypto_session_params *csp;
226 	struct padlock_session *ses;
227 	int error;
228 
229 	if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
230 		error = EINVAL;
231 		goto out;
232 	}
233 
234 	ses = crypto_get_driver_session(crp->crp_session);
235 	csp = crypto_get_params(crp->crp_session);
236 
237 	/* Perform data authentication if requested before decryption. */
238 	if (csp->csp_mode == CSP_MODE_ETA &&
239 	    !CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
240 		error = padlock_hash_process(ses, crp, csp);
241 		if (error != 0)
242 			goto out;
243 	}
244 
245 	error = padlock_cipher_process(ses, crp, csp);
246 	if (error != 0)
247 		goto out;
248 
249 	/* Perform data authentication if requested after encryption. */
250 	if (csp->csp_mode == CSP_MODE_ETA &&
251 	    CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
252 		error = padlock_hash_process(ses, crp, csp);
253 		if (error != 0)
254 			goto out;
255 	}
256 
257 out:
258 #if 0
259 	/*
260 	 * This code is not necessary, because contexts will be freed on next
261 	 * padlock_setup_mackey() call or at padlock_freesession() call.
262 	 */
263 	if (ses != NULL && maccrd != NULL &&
264 	    (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
265 		padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
266 		padlock_free_ctx(ses->ses_axf, ses->ses_octx);
267 	}
268 #endif
269 	crp->crp_etype = error;
270 	crypto_done(crp);
271 	return (0);
272 }
273 
274 static device_method_t padlock_methods[] = {
275 	DEVMETHOD(device_identify,	padlock_identify),
276 	DEVMETHOD(device_probe,		padlock_probe),
277 	DEVMETHOD(device_attach,	padlock_attach),
278 	DEVMETHOD(device_detach,	padlock_detach),
279 
280 	DEVMETHOD(cryptodev_probesession, padlock_probesession),
281 	DEVMETHOD(cryptodev_newsession,	padlock_newsession),
282 	DEVMETHOD(cryptodev_freesession,padlock_freesession),
283 	DEVMETHOD(cryptodev_process,	padlock_process),
284 
285 	{0, 0},
286 };
287 
288 static driver_t padlock_driver = {
289 	"padlock",
290 	padlock_methods,
291 	sizeof(struct padlock_softc),
292 };
293 
294 /* XXX where to attach */
295 DRIVER_MODULE(padlock, nexus, padlock_driver, 0, 0);
296 MODULE_VERSION(padlock, 1);
297 MODULE_DEPEND(padlock, crypto, 1, 1, 1);
298