2 * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/rwlock.h>
43 #include <opencrypto/cryptodev.h>
44 #include <cryptodev_if.h>
46 #if defined(__amd64__)
47 #include <machine/fpu.h>
48 #elif defined(__i386__)
49 #include <machine/npx.h>
52 struct blake2_session {
56 uint8_t key[BLAKE2B_KEYBYTES];
58 CTASSERT((size_t)BLAKE2B_KEYBYTES > (size_t)BLAKE2S_KEYBYTES);
66 static struct mtx_padalign *ctx_mtx;
67 static struct fpu_kern_ctx **ctx_fpu;
69 #define ACQUIRE_CTX(i, ctx) \
71 (i) = PCPU_GET(cpuid); \
72 mtx_lock(&ctx_mtx[(i)]); \
73 (ctx) = ctx_fpu[(i)]; \
75 #define RELEASE_CTX(i, ctx) \
77 mtx_unlock(&ctx_mtx[(i)]); \
82 static int blake2_newsession(device_t, crypto_session_t cses,
83 struct cryptoini *cri);
84 static int blake2_cipher_setup(struct blake2_session *ses,
85 struct cryptoini *authini);
86 static int blake2_cipher_process(struct blake2_session *ses,
89 MALLOC_DEFINE(M_BLAKE2, "blake2_data", "Blake2 Data");
92 blake2_identify(driver_t *drv, device_t parent)
95 /* NB: order 10 is so we get attached after h/w devices */
96 if (device_find_child(parent, "blaketwo", -1) == NULL &&
97 BUS_ADD_CHILD(parent, 10, "blaketwo", -1) == 0)
98 panic("blaketwo: could not attach");
102 blake2_probe(device_t dev)
104 device_set_desc(dev, "Blake2");
109 blake2_cleanctx(void)
113 /* XXX - no way to return driverid */
115 if (ctx_fpu[i] != NULL) {
116 mtx_destroy(&ctx_mtx[i]);
117 fpu_kern_free_ctx(ctx_fpu[i]);
121 free(ctx_mtx, M_BLAKE2);
123 free(ctx_fpu, M_BLAKE2);
128 blake2_attach(device_t dev)
130 struct blake2_softc *sc;
133 sc = device_get_softc(dev);
136 sc->cid = crypto_get_driverid(dev, sizeof(struct blake2_session),
137 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
139 device_printf(dev, "Could not get crypto driver id.\n");
143 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_BLAKE2,
145 ctx_fpu = malloc(sizeof(*ctx_fpu) * (mp_maxid + 1), M_BLAKE2,
149 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
150 mtx_init(&ctx_mtx[i], "bl2fpumtx", NULL, MTX_DEF | MTX_NEW);
153 rw_init(&sc->lock, "blake2_lock");
155 crypto_register(sc->cid, CRYPTO_BLAKE2B, 0, 0);
156 crypto_register(sc->cid, CRYPTO_BLAKE2S, 0, 0);
161 blake2_detach(device_t dev)
163 struct blake2_softc *sc;
165 sc = device_get_softc(dev);
169 rw_wunlock(&sc->lock);
170 crypto_unregister_all(sc->cid);
172 rw_destroy(&sc->lock);
180 blake2_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
182 struct blake2_softc *sc;
183 struct blake2_session *ses;
184 struct cryptoini *authini;
192 sc = device_get_softc(dev);
195 for (; cri != NULL; cri = cri->cri_next) {
196 switch (cri->cri_alg) {
199 if (authini != NULL) {
200 CRYPTDEB("authini already set");
206 CRYPTDEB("unhandled algorithm");
210 if (authini == NULL) {
211 CRYPTDEB("no cipher");
217 rw_wunlock(&sc->lock);
220 rw_wunlock(&sc->lock);
222 ses = crypto_get_driver_session(cses);
224 ses->algo = authini->cri_alg;
225 error = blake2_cipher_setup(ses, authini);
227 CRYPTDEB("setup failed");
235 blake2_process(device_t dev, struct cryptop *crp, int hint __unused)
237 struct blake2_session *ses;
238 struct cryptodesc *crd, *authcrd;
249 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
254 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
255 switch (crd->crd_alg) {
258 if (authcrd != NULL) {
271 ses = crypto_get_driver_session(crp->crp_session);
272 error = blake2_cipher_process(ses, crp);
277 crp->crp_etype = error;
282 static device_method_t blake2_methods[] = {
283 DEVMETHOD(device_identify, blake2_identify),
284 DEVMETHOD(device_probe, blake2_probe),
285 DEVMETHOD(device_attach, blake2_attach),
286 DEVMETHOD(device_detach, blake2_detach),
288 DEVMETHOD(cryptodev_newsession, blake2_newsession),
289 DEVMETHOD(cryptodev_process, blake2_process),
294 static driver_t blake2_driver = {
297 sizeof(struct blake2_softc),
299 static devclass_t blake2_devclass;
301 DRIVER_MODULE(blake2, nexus, blake2_driver, blake2_devclass, 0, 0);
302 MODULE_VERSION(blake2, 1);
303 MODULE_DEPEND(blake2, crypto, 1, 1, 1);
306 blake2_cipher_setup(struct blake2_session *ses, struct cryptoini *authini)
310 CTASSERT((size_t)BLAKE2S_OUTBYTES <= (size_t)BLAKE2B_OUTBYTES);
312 if (authini->cri_mlen < 0)
317 if (authini->cri_mlen != 0 &&
318 authini->cri_mlen > BLAKE2S_OUTBYTES)
322 if (authini->cri_mlen != 0 &&
323 authini->cri_mlen > BLAKE2B_OUTBYTES)
326 if (authini->cri_klen % 8 != 0)
328 keylen = authini->cri_klen / 8;
329 if (keylen > sizeof(ses->key) ||
330 (ses->algo == CRYPTO_BLAKE2S && keylen > BLAKE2S_KEYBYTES))
333 memcpy(ses->key, authini->cri_key, keylen);
334 ses->mlen = authini->cri_mlen;
340 blake2b_applicator(void *state, void *buf, u_int len)
344 rc = blake2b_update(state, buf, len);
351 blake2s_applicator(void *state, void *buf, u_int len)
355 rc = blake2s_update(state, buf, len);
362 blake2_cipher_process(struct blake2_session *ses, struct cryptop *crp)
368 char res[BLAKE2B_OUTBYTES];
369 struct fpu_kern_ctx *ctx;
372 struct cryptodesc *crd;
381 kt = is_fpu_kern_thread(0);
383 ACQUIRE_CTX(ctxidx, ctx);
384 fpu_kern_enter(curthread, ctx,
385 FPU_KERN_NORMAL | FPU_KERN_KTHR);
388 if (crd->crd_flags != 0)
396 hashlen = BLAKE2B_OUTBYTES;
398 rc = blake2b_init_key(&bctx.sb, hashlen, ses->key, ses->klen);
400 rc = blake2b_init(&bctx.sb, hashlen);
403 error = crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
404 crd->crd_len, blake2b_applicator, &bctx.sb);
407 rc = blake2b_final(&bctx.sb, res, hashlen);
417 hashlen = BLAKE2S_OUTBYTES;
419 rc = blake2s_init_key(&bctx.ss, hashlen, ses->key, ses->klen);
421 rc = blake2s_init(&bctx.ss, hashlen);
424 error = crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
425 crd->crd_len, blake2s_applicator, &bctx.ss);
428 rc = blake2s_final(&bctx.ss, res, hashlen);
435 panic("unreachable");
438 crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,
443 fpu_kern_leave(curthread, ctx);
444 RELEASE_CTX(ctxidx, ctx);