]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/crypto/via/padlock.c
MFV r362990:
[FreeBSD/FreeBSD.git] / sys / crypto / via / padlock.c
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_SOFTWARE | CRYPTOCAP_F_SYNC |
123             CRYPTOCAP_F_ACCEL_SOFTWARE);
124         if (sc->sc_cid < 0) {
125                 device_printf(dev, "Could not get crypto driver id.\n");
126                 return (ENOMEM);
127         }
128
129         return (0);
130 }
131
132 static int
133 padlock_detach(device_t dev)
134 {
135         struct padlock_softc *sc = device_get_softc(dev);
136
137         crypto_unregister_all(sc->sc_cid);
138         return (0);
139 }
140
141 static int
142 padlock_probesession(device_t dev, const struct crypto_session_params *csp)
143 {
144
145         if (csp->csp_flags != 0)
146                 return (EINVAL);
147
148         /*
149          * We only support HMAC algorithms to be able to work with
150          * ipsec(4), so if we are asked only for authentication without
151          * encryption, don't pretend we can accelerate it.
152          *
153          * XXX: For CPUs with SHA instructions we should probably
154          * permit CSP_MODE_DIGEST so that those can be tested.
155          */
156         switch (csp->csp_mode) {
157         case CSP_MODE_ETA:
158                 if (!padlock_hash_check(csp))
159                         return (EINVAL);
160                 /* FALLTHROUGH */
161         case CSP_MODE_CIPHER:
162                 switch (csp->csp_cipher_alg) {
163                 case CRYPTO_AES_CBC:
164                         if (csp->csp_ivlen != AES_BLOCK_LEN)
165                                 return (EINVAL);
166                         break;
167                 default:
168                         return (EINVAL);
169                 }
170                 break;
171         default:
172                 return (EINVAL);
173         }
174
175         return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
176 }
177
178 static int
179 padlock_newsession(device_t dev, crypto_session_t cses,
180     const struct crypto_session_params *csp)
181 {
182         struct padlock_softc *sc = device_get_softc(dev);
183         struct padlock_session *ses = NULL;
184         struct thread *td;
185         int error;
186
187         ses = crypto_get_driver_session(cses);
188         ses->ses_fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
189
190         error = padlock_cipher_setup(ses, csp);
191         if (error != 0) {
192                 padlock_freesession_one(sc, ses);
193                 return (error);
194         }
195
196         if (csp->csp_mode == CSP_MODE_ETA) {
197                 td = curthread;
198                 fpu_kern_enter(td, ses->ses_fpu_ctx, FPU_KERN_NORMAL |
199                     FPU_KERN_KTHR);
200                 error = padlock_hash_setup(ses, csp);
201                 fpu_kern_leave(td, ses->ses_fpu_ctx);
202                 if (error != 0) {
203                         padlock_freesession_one(sc, ses);
204                         return (error);
205                 }
206         }
207
208         return (0);
209 }
210
211 static void
212 padlock_freesession_one(struct padlock_softc *sc, struct padlock_session *ses)
213 {
214
215         padlock_hash_free(ses);
216         fpu_kern_free_ctx(ses->ses_fpu_ctx);
217 }
218
219 static void
220 padlock_freesession(device_t dev, crypto_session_t cses)
221 {
222         struct padlock_softc *sc = device_get_softc(dev);
223         struct padlock_session *ses;
224
225         ses = crypto_get_driver_session(cses);
226         padlock_freesession_one(sc, ses);
227 }
228
229 static int
230 padlock_process(device_t dev, struct cryptop *crp, int hint __unused)
231 {
232         const struct crypto_session_params *csp;
233         struct padlock_session *ses;
234         int error;
235
236         if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
237                 error = EINVAL;
238                 goto out;
239         }
240
241         ses = crypto_get_driver_session(crp->crp_session);
242         csp = crypto_get_params(crp->crp_session);
243
244         /* Perform data authentication if requested before decryption. */
245         if (csp->csp_mode == CSP_MODE_ETA &&
246             !CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
247                 error = padlock_hash_process(ses, crp, csp);
248                 if (error != 0)
249                         goto out;
250         }
251
252         error = padlock_cipher_process(ses, crp, csp);
253         if (error != 0)
254                 goto out;
255
256         /* Perform data authentication if requested after encryption. */
257         if (csp->csp_mode == CSP_MODE_ETA &&
258             CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
259                 error = padlock_hash_process(ses, crp, csp);
260                 if (error != 0)
261                         goto out;
262         }
263
264 out:
265 #if 0
266         /*
267          * This code is not necessary, because contexts will be freed on next
268          * padlock_setup_mackey() call or at padlock_freesession() call.
269          */
270         if (ses != NULL && maccrd != NULL &&
271             (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
272                 padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
273                 padlock_free_ctx(ses->ses_axf, ses->ses_octx);
274         }
275 #endif
276         crp->crp_etype = error;
277         crypto_done(crp);
278         return (error);
279 }
280
281 static device_method_t padlock_methods[] = {
282         DEVMETHOD(device_identify,      padlock_identify),
283         DEVMETHOD(device_probe,         padlock_probe),
284         DEVMETHOD(device_attach,        padlock_attach),
285         DEVMETHOD(device_detach,        padlock_detach),
286
287         DEVMETHOD(cryptodev_probesession, padlock_probesession),
288         DEVMETHOD(cryptodev_newsession, padlock_newsession),
289         DEVMETHOD(cryptodev_freesession,padlock_freesession),
290         DEVMETHOD(cryptodev_process,    padlock_process),
291
292         {0, 0},
293 };
294
295 static driver_t padlock_driver = {
296         "padlock",
297         padlock_methods,
298         sizeof(struct padlock_softc),
299 };
300 static devclass_t padlock_devclass;
301
302 /* XXX where to attach */
303 DRIVER_MODULE(padlock, nexus, padlock_driver, padlock_devclass, 0, 0);
304 MODULE_VERSION(padlock, 1);
305 MODULE_DEPEND(padlock, crypto, 1, 1, 1);