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];
58 TAILQ_ENTRY(blake2_session) next;
60 CTASSERT((size_t)BLAKE2B_KEYBYTES > (size_t)BLAKE2S_KEYBYTES);
66 TAILQ_HEAD(blake2_sessions_head, blake2_session) sessions;
70 static struct mtx_padalign *ctx_mtx;
71 static struct fpu_kern_ctx **ctx_fpu;
73 #define ACQUIRE_CTX(i, ctx) \
75 (i) = PCPU_GET(cpuid); \
76 mtx_lock(&ctx_mtx[(i)]); \
77 (ctx) = ctx_fpu[(i)]; \
79 #define RELEASE_CTX(i, ctx) \
81 mtx_unlock(&ctx_mtx[(i)]); \
86 static int blake2_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
87 static int blake2_freesession(device_t, uint64_t tid);
88 static void blake2_freesession_locked(struct blake2_softc *sc,
89 struct blake2_session *ses);
90 static int blake2_cipher_setup(struct blake2_session *ses,
91 struct cryptoini *authini);
92 static int blake2_cipher_process(struct blake2_session *ses,
95 MALLOC_DEFINE(M_BLAKE2, "blake2_data", "Blake2 Data");
98 blake2_identify(driver_t *drv, device_t parent)
101 /* NB: order 10 is so we get attached after h/w devices */
102 if (device_find_child(parent, "blaketwo", -1) == NULL &&
103 BUS_ADD_CHILD(parent, 10, "blaketwo", -1) == 0)
104 panic("blaketwo: could not attach");
108 blake2_probe(device_t dev)
110 device_set_desc(dev, "Blake2");
115 blake2_cleanctx(void)
119 /* XXX - no way to return driverid */
121 if (ctx_fpu[i] != NULL) {
122 mtx_destroy(&ctx_mtx[i]);
123 fpu_kern_free_ctx(ctx_fpu[i]);
127 free(ctx_mtx, M_BLAKE2);
129 free(ctx_fpu, M_BLAKE2);
134 blake2_attach(device_t dev)
136 struct blake2_softc *sc;
139 sc = device_get_softc(dev);
141 TAILQ_INIT(&sc->sessions);
144 sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE |
147 device_printf(dev, "Could not get crypto driver id.\n");
151 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_BLAKE2,
153 ctx_fpu = malloc(sizeof(*ctx_fpu) * (mp_maxid + 1), M_BLAKE2,
157 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
158 mtx_init(&ctx_mtx[i], "bl2fpumtx", NULL, MTX_DEF | MTX_NEW);
161 rw_init(&sc->lock, "blake2_lock");
163 crypto_register(sc->cid, CRYPTO_BLAKE2B, 0, 0);
164 crypto_register(sc->cid, CRYPTO_BLAKE2S, 0, 0);
169 blake2_detach(device_t dev)
171 struct blake2_softc *sc;
172 struct blake2_session *ses;
174 sc = device_get_softc(dev);
177 TAILQ_FOREACH(ses, &sc->sessions, next) {
179 rw_wunlock(&sc->lock);
181 "Cannot detach, sessions still active.\n");
186 while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
187 TAILQ_REMOVE(&sc->sessions, ses, next);
190 rw_wunlock(&sc->lock);
191 crypto_unregister_all(sc->cid);
193 rw_destroy(&sc->lock);
201 blake2_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
203 struct blake2_softc *sc;
204 struct blake2_session *ses;
205 struct cryptoini *authini;
208 if (sidp == NULL || cri == NULL) {
209 CRYPTDEB("no sidp or cri");
213 sc = device_get_softc(dev);
217 for (; cri != NULL; cri = cri->cri_next) {
218 switch (cri->cri_alg) {
221 if (authini != NULL) {
222 CRYPTDEB("authini already set");
228 CRYPTDEB("unhandled algorithm");
232 if (authini == NULL) {
233 CRYPTDEB("no cipher");
239 rw_wunlock(&sc->lock);
243 * Free sessions are inserted at the head of the list. So if the first
244 * session is used, none are free and we must allocate a new one.
246 ses = TAILQ_FIRST(&sc->sessions);
247 if (ses == NULL || ses->used) {
248 ses = malloc(sizeof(*ses), M_BLAKE2, M_NOWAIT | M_ZERO);
250 rw_wunlock(&sc->lock);
255 TAILQ_REMOVE(&sc->sessions, ses, next);
258 TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
259 rw_wunlock(&sc->lock);
261 ses->algo = authini->cri_alg;
262 error = blake2_cipher_setup(ses, authini);
264 CRYPTDEB("setup failed");
266 blake2_freesession_locked(sc, ses);
267 rw_wunlock(&sc->lock);
276 blake2_freesession_locked(struct blake2_softc *sc, struct blake2_session *ses)
280 rw_assert(&sc->lock, RA_WLOCKED);
283 TAILQ_REMOVE(&sc->sessions, ses, next);
284 explicit_bzero(ses, sizeof(*ses));
286 TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
290 blake2_freesession(device_t dev, uint64_t tid)
292 struct blake2_softc *sc;
293 struct blake2_session *ses;
296 sc = device_get_softc(dev);
297 sid = ((uint32_t)tid) & 0xffffffff;
299 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, blake2_sessions_head, next) {
304 rw_wunlock(&sc->lock);
307 blake2_freesession_locked(sc, ses);
308 rw_wunlock(&sc->lock);
313 blake2_process(device_t dev, struct cryptop *crp, int hint __unused)
315 struct blake2_softc *sc;
316 struct blake2_session *ses;
317 struct cryptodesc *crd, *authcrd;
320 sc = device_get_softc(dev);
329 if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
334 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
335 switch (crd->crd_alg) {
338 if (authcrd != NULL) {
352 TAILQ_FOREACH_REVERSE(ses, &sc->sessions, blake2_sessions_head, next) {
353 if (ses->id == (crp->crp_sid & 0xffffffff))
356 rw_runlock(&sc->lock);
362 error = blake2_cipher_process(ses, crp);
367 crp->crp_etype = error;
372 static device_method_t blake2_methods[] = {
373 DEVMETHOD(device_identify, blake2_identify),
374 DEVMETHOD(device_probe, blake2_probe),
375 DEVMETHOD(device_attach, blake2_attach),
376 DEVMETHOD(device_detach, blake2_detach),
378 DEVMETHOD(cryptodev_newsession, blake2_newsession),
379 DEVMETHOD(cryptodev_freesession, blake2_freesession),
380 DEVMETHOD(cryptodev_process, blake2_process),
385 static driver_t blake2_driver = {
388 sizeof(struct blake2_softc),
390 static devclass_t blake2_devclass;
392 DRIVER_MODULE(blake2, nexus, blake2_driver, blake2_devclass, 0, 0);
393 MODULE_VERSION(blake2, 1);
394 MODULE_DEPEND(blake2, crypto, 1, 1, 1);
397 blake2_cipher_setup(struct blake2_session *ses, struct cryptoini *authini)
401 CTASSERT((size_t)BLAKE2S_OUTBYTES <= (size_t)BLAKE2B_OUTBYTES);
403 if (authini->cri_mlen < 0)
408 if (authini->cri_mlen != 0 &&
409 authini->cri_mlen > BLAKE2S_OUTBYTES)
413 if (authini->cri_mlen != 0 &&
414 authini->cri_mlen > BLAKE2B_OUTBYTES)
417 if (authini->cri_klen % 8 != 0)
419 keylen = authini->cri_klen / 8;
420 if (keylen > sizeof(ses->key) ||
421 (ses->algo == CRYPTO_BLAKE2S && keylen > BLAKE2S_KEYBYTES))
424 memcpy(ses->key, authini->cri_key, keylen);
425 ses->mlen = authini->cri_mlen;
431 blake2b_applicator(void *state, void *buf, u_int len)
435 rc = blake2b_update(state, buf, len);
442 blake2s_applicator(void *state, void *buf, u_int len)
446 rc = blake2s_update(state, buf, len);
453 blake2_cipher_process(struct blake2_session *ses, struct cryptop *crp)
459 char res[BLAKE2B_OUTBYTES];
460 struct fpu_kern_ctx *ctx;
463 struct cryptodesc *crd;
472 kt = is_fpu_kern_thread(0);
474 ACQUIRE_CTX(ctxidx, ctx);
475 fpu_kern_enter(curthread, ctx,
476 FPU_KERN_NORMAL | FPU_KERN_KTHR);
479 if (crd->crd_flags != 0)
487 hashlen = BLAKE2B_OUTBYTES;
489 rc = blake2b_init_key(&bctx.sb, hashlen, ses->key, ses->klen);
491 rc = blake2b_init(&bctx.sb, hashlen);
494 error = crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
495 crd->crd_len, blake2b_applicator, &bctx.sb);
498 rc = blake2b_final(&bctx.sb, res, hashlen);
508 hashlen = BLAKE2S_OUTBYTES;
510 rc = blake2s_init_key(&bctx.ss, hashlen, ses->key, ses->klen);
512 rc = blake2s_init(&bctx.ss, hashlen);
515 error = crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
516 crd->crd_len, blake2s_applicator, &bctx.ss);
519 rc = blake2s_final(&bctx.ss, res, hashlen);
526 panic("unreachable");
529 crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,
534 fpu_kern_leave(curthread, ctx);
535 RELEASE_CTX(ctxidx, ctx);