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 {
55 CTASSERT((size_t)BLAKE2B_KEYBYTES > (size_t)BLAKE2S_KEYBYTES);
63 static struct mtx_padalign *ctx_mtx;
64 static struct fpu_kern_ctx **ctx_fpu;
66 #define ACQUIRE_CTX(i, ctx) \
68 (i) = PCPU_GET(cpuid); \
69 mtx_lock(&ctx_mtx[(i)]); \
70 (ctx) = ctx_fpu[(i)]; \
72 #define RELEASE_CTX(i, ctx) \
74 mtx_unlock(&ctx_mtx[(i)]); \
79 static int blake2_cipher_setup(struct blake2_session *ses,
80 const struct crypto_session_params *csp);
81 static int blake2_cipher_process(struct blake2_session *ses,
84 MALLOC_DEFINE(M_BLAKE2, "blake2_data", "Blake2 Data");
87 blake2_identify(driver_t *drv, device_t parent)
90 /* NB: order 10 is so we get attached after h/w devices */
91 if (device_find_child(parent, "blaketwo", -1) == NULL &&
92 BUS_ADD_CHILD(parent, 10, "blaketwo", -1) == 0)
93 panic("blaketwo: could not attach");
97 blake2_probe(device_t dev)
99 device_set_desc(dev, "Blake2");
104 blake2_cleanctx(void)
108 /* XXX - no way to return driverid */
110 if (ctx_fpu[i] != NULL) {
111 mtx_destroy(&ctx_mtx[i]);
112 fpu_kern_free_ctx(ctx_fpu[i]);
116 free(ctx_mtx, M_BLAKE2);
118 free(ctx_fpu, M_BLAKE2);
123 blake2_attach(device_t dev)
125 struct blake2_softc *sc;
128 sc = device_get_softc(dev);
131 sc->cid = crypto_get_driverid(dev, sizeof(struct blake2_session),
132 CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC |
133 CRYPTOCAP_F_ACCEL_SOFTWARE);
135 device_printf(dev, "Could not get crypto driver id.\n");
139 ctx_mtx = malloc(sizeof(*ctx_mtx) * (mp_maxid + 1), M_BLAKE2,
141 ctx_fpu = malloc(sizeof(*ctx_fpu) * (mp_maxid + 1), M_BLAKE2,
145 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
146 mtx_init(&ctx_mtx[i], "bl2fpumtx", NULL, MTX_DEF | MTX_NEW);
149 rw_init(&sc->lock, "blake2_lock");
155 blake2_detach(device_t dev)
157 struct blake2_softc *sc;
159 sc = device_get_softc(dev);
163 rw_wunlock(&sc->lock);
164 crypto_unregister_all(sc->cid);
166 rw_destroy(&sc->lock);
174 blake2_probesession(device_t dev, const struct crypto_session_params *csp)
177 if (csp->csp_flags != 0)
179 switch (csp->csp_mode) {
180 case CSP_MODE_DIGEST:
181 switch (csp->csp_auth_alg) {
192 return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
196 blake2_newsession(device_t dev, crypto_session_t cses,
197 const struct crypto_session_params *csp)
199 struct blake2_softc *sc;
200 struct blake2_session *ses;
203 sc = device_get_softc(dev);
205 ses = crypto_get_driver_session(cses);
209 rw_runlock(&sc->lock);
212 rw_runlock(&sc->lock);
214 error = blake2_cipher_setup(ses, csp);
216 CRYPTDEB("setup failed");
224 blake2_process(device_t dev, struct cryptop *crp, int hint __unused)
226 struct blake2_session *ses;
229 ses = crypto_get_driver_session(crp->crp_session);
230 error = blake2_cipher_process(ses, crp);
232 crp->crp_etype = error;
237 static device_method_t blake2_methods[] = {
238 DEVMETHOD(device_identify, blake2_identify),
239 DEVMETHOD(device_probe, blake2_probe),
240 DEVMETHOD(device_attach, blake2_attach),
241 DEVMETHOD(device_detach, blake2_detach),
243 DEVMETHOD(cryptodev_probesession, blake2_probesession),
244 DEVMETHOD(cryptodev_newsession, blake2_newsession),
245 DEVMETHOD(cryptodev_process, blake2_process),
250 static driver_t blake2_driver = {
253 sizeof(struct blake2_softc),
255 static devclass_t blake2_devclass;
257 DRIVER_MODULE(blake2, nexus, blake2_driver, blake2_devclass, 0, 0);
258 MODULE_VERSION(blake2, 1);
259 MODULE_DEPEND(blake2, crypto, 1, 1, 1);
262 blake2_check_klen(const struct crypto_session_params *csp, unsigned klen)
265 if (csp->csp_auth_alg == CRYPTO_BLAKE2S)
266 return (klen <= BLAKE2S_KEYBYTES);
268 return (klen <= BLAKE2B_KEYBYTES);
272 blake2_cipher_setup(struct blake2_session *ses,
273 const struct crypto_session_params *csp)
277 CTASSERT((size_t)BLAKE2S_OUTBYTES <= (size_t)BLAKE2B_OUTBYTES);
279 if (!blake2_check_klen(csp, csp->csp_auth_klen))
282 if (csp->csp_auth_mlen < 0)
285 switch (csp->csp_auth_alg) {
287 hashlen = BLAKE2S_OUTBYTES;
290 hashlen = BLAKE2B_OUTBYTES;
296 if (csp->csp_auth_mlen > hashlen)
299 if (csp->csp_auth_mlen == 0)
302 ses->mlen = csp->csp_auth_mlen;
307 blake2b_applicator(void *state, void *buf, u_int len)
311 rc = blake2b_update(state, buf, len);
318 blake2s_applicator(void *state, void *buf, u_int len)
322 rc = blake2s_update(state, buf, len);
329 blake2_cipher_process(struct blake2_session *ses, struct cryptop *crp)
335 char res[BLAKE2B_OUTBYTES], res2[BLAKE2B_OUTBYTES];
336 const struct crypto_session_params *csp;
337 struct fpu_kern_ctx *ctx;
348 kt = is_fpu_kern_thread(0);
350 ACQUIRE_CTX(ctxidx, ctx);
351 fpu_kern_enter(curthread, ctx,
352 FPU_KERN_NORMAL | FPU_KERN_KTHR);
355 csp = crypto_get_params(crp->crp_session);
356 if (crp->crp_auth_key != NULL)
357 key = crp->crp_auth_key;
359 key = csp->csp_auth_key;
360 klen = csp->csp_auth_klen;
361 switch (csp->csp_auth_alg) {
364 rc = blake2b_init_key(&bctx.sb, ses->mlen, key, klen);
366 rc = blake2b_init(&bctx.sb, ses->mlen);
369 error = crypto_apply(crp, crp->crp_payload_start,
370 crp->crp_payload_length, blake2b_applicator, &bctx.sb);
373 rc = blake2b_final(&bctx.sb, res, ses->mlen);
381 rc = blake2s_init_key(&bctx.ss, ses->mlen, key, klen);
383 rc = blake2s_init(&bctx.ss, ses->mlen);
386 error = crypto_apply(crp, crp->crp_payload_start,
387 crp->crp_payload_length, blake2s_applicator, &bctx.ss);
390 rc = blake2s_final(&bctx.ss, res, ses->mlen);
397 panic("unreachable");
400 if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
401 crypto_copydata(crp, crp->crp_digest_start, ses->mlen, res2);
402 if (timingsafe_bcmp(res, res2, ses->mlen) != 0)
405 crypto_copyback(crp, crp->crp_digest_start, ses->mlen, res);
409 fpu_kern_leave(curthread, ctx);
410 RELEASE_CTX(ctxidx, ctx);