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/module.h>
36 #include <sys/malloc.h>
37 #include <sys/rwlock.h>
42 #include <opencrypto/cryptodev.h>
43 #include <cryptodev_if.h>
45 #if defined(__amd64__)
46 #include <machine/fpu.h>
47 #elif defined(__i386__)
48 #include <machine/npx.h>
51 struct blake2_session {
55 uint8_t key[BLAKE2B_KEYBYTES];
57 CTASSERT((size_t)BLAKE2B_KEYBYTES > (size_t)BLAKE2S_KEYBYTES);
65 static struct mtx_padalign *ctx_mtx;
66 static struct fpu_kern_ctx **ctx_fpu;
68 #define ACQUIRE_CTX(i, ctx) \
70 (i) = PCPU_GET(cpuid); \
71 mtx_lock(&ctx_mtx[(i)]); \
72 (ctx) = ctx_fpu[(i)]; \
74 #define RELEASE_CTX(i, ctx) \
76 mtx_unlock(&ctx_mtx[(i)]); \
81 static int blake2_newsession(device_t, crypto_session_t cses,
82 struct cryptoini *cri);
83 static int blake2_cipher_setup(struct blake2_session *ses,
84 struct cryptoini *authini);
85 static int blake2_cipher_process(struct blake2_session *ses,
88 MALLOC_DEFINE(M_BLAKE2, "blake2_data", "Blake2 Data");
91 blake2_identify(driver_t *drv, device_t parent)
94 /* NB: order 10 is so we get attached after h/w devices */
95 if (device_find_child(parent, "blaketwo", -1) == NULL &&
96 BUS_ADD_CHILD(parent, 10, "blaketwo", -1) == 0)
97 panic("blaketwo: could not attach");
101 blake2_probe(device_t dev)
103 device_set_desc(dev, "Blake2");
108 blake2_cleanctx(void)
112 /* XXX - no way to return driverid */
114 if (ctx_fpu[i] != NULL) {
115 mtx_destroy(&ctx_mtx[i]);
116 fpu_kern_free_ctx(ctx_fpu[i]);
120 free(ctx_mtx, M_BLAKE2);
122 free(ctx_fpu, M_BLAKE2);
127 blake2_attach(device_t dev)
129 struct blake2_softc *sc;
132 sc = device_get_softc(dev);
135 sc->cid = crypto_get_driverid(dev, sizeof(struct blake2_session),
136 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
138 device_printf(dev, "Could not get crypto driver id.\n");
142 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_BLAKE2,
144 ctx_fpu = malloc(sizeof(*ctx_fpu) * (mp_maxid + 1), M_BLAKE2,
148 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
149 mtx_init(&ctx_mtx[i], "bl2fpumtx", NULL, MTX_DEF | MTX_NEW);
152 rw_init(&sc->lock, "blake2_lock");
154 crypto_register(sc->cid, CRYPTO_BLAKE2B, 0, 0);
155 crypto_register(sc->cid, CRYPTO_BLAKE2S, 0, 0);
160 blake2_detach(device_t dev)
162 struct blake2_softc *sc;
164 sc = device_get_softc(dev);
168 rw_wunlock(&sc->lock);
169 crypto_unregister_all(sc->cid);
171 rw_destroy(&sc->lock);
179 blake2_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
181 struct blake2_softc *sc;
182 struct blake2_session *ses;
183 struct cryptoini *authini;
191 sc = device_get_softc(dev);
194 for (; cri != NULL; cri = cri->cri_next) {
195 switch (cri->cri_alg) {
198 if (authini != NULL) {
199 CRYPTDEB("authini already set");
205 CRYPTDEB("unhandled algorithm");
209 if (authini == NULL) {
210 CRYPTDEB("no cipher");
216 rw_wunlock(&sc->lock);
219 rw_wunlock(&sc->lock);
221 ses = crypto_get_driver_session(cses);
223 ses->algo = authini->cri_alg;
224 error = blake2_cipher_setup(ses, authini);
226 CRYPTDEB("setup failed");
234 blake2_process(device_t dev, struct cryptop *crp, int hint __unused)
236 struct blake2_session *ses;
237 struct cryptodesc *crd, *authcrd;
248 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
253 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
254 switch (crd->crd_alg) {
257 if (authcrd != NULL) {
270 ses = crypto_get_driver_session(crp->crp_session);
271 error = blake2_cipher_process(ses, crp);
276 crp->crp_etype = error;
281 static device_method_t blake2_methods[] = {
282 DEVMETHOD(device_identify, blake2_identify),
283 DEVMETHOD(device_probe, blake2_probe),
284 DEVMETHOD(device_attach, blake2_attach),
285 DEVMETHOD(device_detach, blake2_detach),
287 DEVMETHOD(cryptodev_newsession, blake2_newsession),
288 DEVMETHOD(cryptodev_process, blake2_process),
293 static driver_t blake2_driver = {
296 sizeof(struct blake2_softc),
298 static devclass_t blake2_devclass;
300 DRIVER_MODULE(blake2, nexus, blake2_driver, blake2_devclass, 0, 0);
301 MODULE_VERSION(blake2, 1);
302 MODULE_DEPEND(blake2, crypto, 1, 1, 1);
305 blake2_cipher_setup(struct blake2_session *ses, struct cryptoini *authini)
309 CTASSERT((size_t)BLAKE2S_OUTBYTES <= (size_t)BLAKE2B_OUTBYTES);
311 if (authini->cri_mlen < 0)
316 if (authini->cri_mlen != 0 &&
317 authini->cri_mlen > BLAKE2S_OUTBYTES)
321 if (authini->cri_mlen != 0 &&
322 authini->cri_mlen > BLAKE2B_OUTBYTES)
325 if (authini->cri_klen % 8 != 0)
327 keylen = authini->cri_klen / 8;
328 if (keylen > sizeof(ses->key) ||
329 (ses->algo == CRYPTO_BLAKE2S && keylen > BLAKE2S_KEYBYTES))
332 memcpy(ses->key, authini->cri_key, keylen);
333 ses->mlen = authini->cri_mlen;
339 blake2b_applicator(void *state, void *buf, u_int len)
343 rc = blake2b_update(state, buf, len);
350 blake2s_applicator(void *state, void *buf, u_int len)
354 rc = blake2s_update(state, buf, len);
361 blake2_cipher_process(struct blake2_session *ses, struct cryptop *crp)
367 char res[BLAKE2B_OUTBYTES];
368 struct fpu_kern_ctx *ctx;
371 struct cryptodesc *crd;
380 kt = is_fpu_kern_thread(0);
382 ACQUIRE_CTX(ctxidx, ctx);
383 fpu_kern_enter(curthread, ctx,
384 FPU_KERN_NORMAL | FPU_KERN_KTHR);
387 if (crd->crd_flags != 0)
395 hashlen = BLAKE2B_OUTBYTES;
397 rc = blake2b_init_key(&bctx.sb, hashlen, ses->key, ses->klen);
399 rc = blake2b_init(&bctx.sb, hashlen);
402 error = crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
403 crd->crd_len, blake2b_applicator, &bctx.sb);
406 rc = blake2b_final(&bctx.sb, res, hashlen);
416 hashlen = BLAKE2S_OUTBYTES;
418 rc = blake2s_init_key(&bctx.ss, hashlen, ses->key, ses->klen);
420 rc = blake2s_init(&bctx.ss, hashlen);
423 error = crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
424 crd->crd_len, blake2s_applicator, &bctx.ss);
427 rc = blake2s_final(&bctx.ss, res, hashlen);
434 panic("unreachable");
437 crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,
442 fpu_kern_leave(curthread, ctx);
443 RELEASE_CTX(ctxidx, ctx);