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