1fd86ae68SMitchell Horne /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fd86ae68SMitchell Horne * 4ba610be9SJohn Baldwin * Copyright (c) 2020 Netflix, Inc 5ba610be9SJohn Baldwin * 6ba610be9SJohn Baldwin * Redistribution and use in source and binary forms, with or without 7ba610be9SJohn Baldwin * modification, are permitted provided that the following conditions 8ba610be9SJohn Baldwin * are met: 9ba610be9SJohn Baldwin * 1. Redistributions of source code must retain the above copyright 10ba610be9SJohn Baldwin * notice, this list of conditions and the following disclaimer, 11ba610be9SJohn Baldwin * without modification. 12ba610be9SJohn Baldwin * 2. Redistributions in binary form must reproduce at minimum a disclaimer 13ba610be9SJohn Baldwin * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 14ba610be9SJohn Baldwin * redistribution must be conditioned upon including a substantially 15ba610be9SJohn Baldwin * similar Disclaimer requirement for further binary redistribution. 16ba610be9SJohn Baldwin * 17ba610be9SJohn Baldwin * NO WARRANTY 18ba610be9SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19ba610be9SJohn Baldwin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20ba610be9SJohn Baldwin * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 21ba610be9SJohn Baldwin * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22ba610be9SJohn Baldwin * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 23ba610be9SJohn Baldwin * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24ba610be9SJohn Baldwin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25ba610be9SJohn Baldwin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 26ba610be9SJohn Baldwin * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27ba610be9SJohn Baldwin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28ba610be9SJohn Baldwin * THE POSSIBILITY OF SUCH DAMAGES. 29ba610be9SJohn Baldwin */ 30ba610be9SJohn Baldwin 31ba610be9SJohn Baldwin /* 32ba610be9SJohn Baldwin * A driver for the OpenCrypto framework which uses assembly routines 33ba610be9SJohn Baldwin * from OpenSSL. 34ba610be9SJohn Baldwin */ 35ba610be9SJohn Baldwin 36ba610be9SJohn Baldwin #include <sys/cdefs.h> 37ba610be9SJohn Baldwin __FBSDID("$FreeBSD$"); 38ba610be9SJohn Baldwin 39ba610be9SJohn Baldwin #include <sys/types.h> 40ba610be9SJohn Baldwin #include <sys/bus.h> 41ba610be9SJohn Baldwin #include <sys/kernel.h> 42ba610be9SJohn Baldwin #include <sys/malloc.h> 43ba610be9SJohn Baldwin #include <sys/module.h> 44fd86ae68SMitchell Horne 45ba610be9SJohn Baldwin #include <machine/fpu.h> 46ba610be9SJohn Baldwin 47ba610be9SJohn Baldwin #include <opencrypto/cryptodev.h> 48ba610be9SJohn Baldwin #include <opencrypto/xform_auth.h> 49ba610be9SJohn Baldwin 50ba610be9SJohn Baldwin #include <crypto/openssl/ossl.h> 5192aecd1eSJohn Baldwin #include <crypto/openssl/ossl_chacha.h> 52197ff4c3SKornel Duleba #include <crypto/openssl/ossl_cipher.h> 53ba610be9SJohn Baldwin 54ba610be9SJohn Baldwin #include "cryptodev_if.h" 55ba610be9SJohn Baldwin 56ba610be9SJohn Baldwin static MALLOC_DEFINE(M_OSSL, "ossl", "OpenSSL crypto"); 57ba610be9SJohn Baldwin 58ba610be9SJohn Baldwin static void 59ba610be9SJohn Baldwin ossl_identify(driver_t *driver, device_t parent) 60ba610be9SJohn Baldwin { 61ba610be9SJohn Baldwin 62ba610be9SJohn Baldwin if (device_find_child(parent, "ossl", -1) == NULL) 63ba610be9SJohn Baldwin BUS_ADD_CHILD(parent, 10, "ossl", -1); 64ba610be9SJohn Baldwin } 65ba610be9SJohn Baldwin 66ba610be9SJohn Baldwin static int 67ba610be9SJohn Baldwin ossl_probe(device_t dev) 68ba610be9SJohn Baldwin { 69ba610be9SJohn Baldwin 70ba610be9SJohn Baldwin device_set_desc(dev, "OpenSSL crypto"); 71ba610be9SJohn Baldwin return (BUS_PROBE_DEFAULT); 72ba610be9SJohn Baldwin } 73ba610be9SJohn Baldwin 74ba610be9SJohn Baldwin static int 75ba610be9SJohn Baldwin ossl_attach(device_t dev) 76ba610be9SJohn Baldwin { 77ba610be9SJohn Baldwin struct ossl_softc *sc; 78ba610be9SJohn Baldwin 79ba610be9SJohn Baldwin sc = device_get_softc(dev); 80ba610be9SJohn Baldwin 81197ff4c3SKornel Duleba ossl_cpuid(sc); 82ba610be9SJohn Baldwin sc->sc_cid = crypto_get_driverid(dev, sizeof(struct ossl_session), 83ba610be9SJohn Baldwin CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC | 84ba610be9SJohn Baldwin CRYPTOCAP_F_ACCEL_SOFTWARE); 85ba610be9SJohn Baldwin if (sc->sc_cid < 0) { 86ba610be9SJohn Baldwin device_printf(dev, "failed to allocate crypto driver id\n"); 87ba610be9SJohn Baldwin return (ENXIO); 88ba610be9SJohn Baldwin } 89ba610be9SJohn Baldwin 90ba610be9SJohn Baldwin return (0); 91ba610be9SJohn Baldwin } 92ba610be9SJohn Baldwin 93ba610be9SJohn Baldwin static int 94ba610be9SJohn Baldwin ossl_detach(device_t dev) 95ba610be9SJohn Baldwin { 96ba610be9SJohn Baldwin struct ossl_softc *sc; 97ba610be9SJohn Baldwin 98ba610be9SJohn Baldwin sc = device_get_softc(dev); 99ba610be9SJohn Baldwin 100ba610be9SJohn Baldwin crypto_unregister_all(sc->sc_cid); 101ba610be9SJohn Baldwin 102ba610be9SJohn Baldwin return (0); 103ba610be9SJohn Baldwin } 104ba610be9SJohn Baldwin 105ba610be9SJohn Baldwin static struct auth_hash * 106ba610be9SJohn Baldwin ossl_lookup_hash(const struct crypto_session_params *csp) 107ba610be9SJohn Baldwin { 108ba610be9SJohn Baldwin 109ba610be9SJohn Baldwin switch (csp->csp_auth_alg) { 110ba610be9SJohn Baldwin case CRYPTO_SHA1: 111ba610be9SJohn Baldwin case CRYPTO_SHA1_HMAC: 112ba610be9SJohn Baldwin return (&ossl_hash_sha1); 113ba610be9SJohn Baldwin case CRYPTO_SHA2_224: 114ba610be9SJohn Baldwin case CRYPTO_SHA2_224_HMAC: 115ba610be9SJohn Baldwin return (&ossl_hash_sha224); 116ba610be9SJohn Baldwin case CRYPTO_SHA2_256: 117ba610be9SJohn Baldwin case CRYPTO_SHA2_256_HMAC: 118ba610be9SJohn Baldwin return (&ossl_hash_sha256); 119ba610be9SJohn Baldwin case CRYPTO_SHA2_384: 120ba610be9SJohn Baldwin case CRYPTO_SHA2_384_HMAC: 121ba610be9SJohn Baldwin return (&ossl_hash_sha384); 122ba610be9SJohn Baldwin case CRYPTO_SHA2_512: 123ba610be9SJohn Baldwin case CRYPTO_SHA2_512_HMAC: 124ba610be9SJohn Baldwin return (&ossl_hash_sha512); 125a079e38bSJohn Baldwin case CRYPTO_POLY1305: 126a079e38bSJohn Baldwin return (&ossl_hash_poly1305); 127ba610be9SJohn Baldwin default: 128ba610be9SJohn Baldwin return (NULL); 129ba610be9SJohn Baldwin } 130ba610be9SJohn Baldwin } 131ba610be9SJohn Baldwin 132197ff4c3SKornel Duleba static struct ossl_cipher* 133197ff4c3SKornel Duleba ossl_lookup_cipher(const struct crypto_session_params *csp) 134197ff4c3SKornel Duleba { 135197ff4c3SKornel Duleba 136197ff4c3SKornel Duleba switch (csp->csp_cipher_alg) { 137197ff4c3SKornel Duleba case CRYPTO_AES_CBC: 138197ff4c3SKornel Duleba switch (csp->csp_cipher_klen * 8) { 139197ff4c3SKornel Duleba case 128: 140197ff4c3SKornel Duleba case 192: 141197ff4c3SKornel Duleba case 256: 142197ff4c3SKornel Duleba break; 143197ff4c3SKornel Duleba default: 144197ff4c3SKornel Duleba return (NULL); 145197ff4c3SKornel Duleba } 146197ff4c3SKornel Duleba return (&ossl_cipher_aes_cbc); 147197ff4c3SKornel Duleba case CRYPTO_CHACHA20: 148197ff4c3SKornel Duleba if (csp->csp_cipher_klen != CHACHA_KEY_SIZE) 149197ff4c3SKornel Duleba return (NULL); 150197ff4c3SKornel Duleba return (&ossl_cipher_chacha20); 151197ff4c3SKornel Duleba default: 152197ff4c3SKornel Duleba return (NULL); 153197ff4c3SKornel Duleba } 154197ff4c3SKornel Duleba } 155197ff4c3SKornel Duleba 156ba610be9SJohn Baldwin static int 157ba610be9SJohn Baldwin ossl_probesession(device_t dev, const struct crypto_session_params *csp) 158ba610be9SJohn Baldwin { 159197ff4c3SKornel Duleba struct ossl_softc *sc = device_get_softc(dev); 160ba610be9SJohn Baldwin 161ba610be9SJohn Baldwin if ((csp->csp_flags & ~(CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD)) != 162ba610be9SJohn Baldwin 0) 163ba610be9SJohn Baldwin return (EINVAL); 164ba610be9SJohn Baldwin switch (csp->csp_mode) { 165ba610be9SJohn Baldwin case CSP_MODE_DIGEST: 166ba610be9SJohn Baldwin if (ossl_lookup_hash(csp) == NULL) 167ba610be9SJohn Baldwin return (EINVAL); 168ba610be9SJohn Baldwin break; 16992aecd1eSJohn Baldwin case CSP_MODE_CIPHER: 170197ff4c3SKornel Duleba if (csp->csp_cipher_alg != CRYPTO_CHACHA20 && !sc->has_aes) 17192aecd1eSJohn Baldwin return (EINVAL); 172197ff4c3SKornel Duleba if (ossl_lookup_cipher(csp) == NULL) 17392aecd1eSJohn Baldwin return (EINVAL); 17492aecd1eSJohn Baldwin break; 175c4026909SKornel Duleba case CSP_MODE_ETA: 176c4026909SKornel Duleba if (!sc->has_aes || 177c4026909SKornel Duleba csp->csp_cipher_alg == CRYPTO_CHACHA20 || 178c4026909SKornel Duleba ossl_lookup_hash(csp) == NULL || 179c4026909SKornel Duleba ossl_lookup_cipher(csp) == NULL) 180c4026909SKornel Duleba return (EINVAL); 181c4026909SKornel Duleba break; 18278991a93SJohn Baldwin case CSP_MODE_AEAD: 18378991a93SJohn Baldwin switch (csp->csp_cipher_alg) { 18478991a93SJohn Baldwin case CRYPTO_CHACHA20_POLY1305: 18578991a93SJohn Baldwin break; 18678991a93SJohn Baldwin default: 18778991a93SJohn Baldwin return (EINVAL); 18878991a93SJohn Baldwin } 18978991a93SJohn Baldwin break; 190ba610be9SJohn Baldwin default: 191ba610be9SJohn Baldwin return (EINVAL); 192ba610be9SJohn Baldwin } 193ba610be9SJohn Baldwin 194ba610be9SJohn Baldwin return (CRYPTODEV_PROBE_ACCEL_SOFTWARE); 195ba610be9SJohn Baldwin } 196ba610be9SJohn Baldwin 19792aecd1eSJohn Baldwin static void 19892aecd1eSJohn Baldwin ossl_newsession_hash(struct ossl_session *s, 199ba610be9SJohn Baldwin const struct crypto_session_params *csp) 200ba610be9SJohn Baldwin { 201ba610be9SJohn Baldwin struct auth_hash *axf; 202ba610be9SJohn Baldwin 203ba610be9SJohn Baldwin axf = ossl_lookup_hash(csp); 204ba610be9SJohn Baldwin s->hash.axf = axf; 205ba610be9SJohn Baldwin if (csp->csp_auth_mlen == 0) 206ba610be9SJohn Baldwin s->hash.mlen = axf->hashsize; 207ba610be9SJohn Baldwin else 208ba610be9SJohn Baldwin s->hash.mlen = csp->csp_auth_mlen; 209ba610be9SJohn Baldwin 210ba610be9SJohn Baldwin if (csp->csp_auth_klen == 0) { 211ba610be9SJohn Baldwin axf->Init(&s->hash.ictx); 212ba610be9SJohn Baldwin } else { 213ba610be9SJohn Baldwin if (csp->csp_auth_key != NULL) { 214ba610be9SJohn Baldwin fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); 215a079e38bSJohn Baldwin if (axf->Setkey != NULL) { 216a079e38bSJohn Baldwin axf->Init(&s->hash.ictx); 217a079e38bSJohn Baldwin axf->Setkey(&s->hash.ictx, csp->csp_auth_key, 218ba610be9SJohn Baldwin csp->csp_auth_klen); 219a079e38bSJohn Baldwin } else { 220a079e38bSJohn Baldwin hmac_init_ipad(axf, csp->csp_auth_key, 221a079e38bSJohn Baldwin csp->csp_auth_klen, &s->hash.ictx); 222a079e38bSJohn Baldwin hmac_init_opad(axf, csp->csp_auth_key, 223a079e38bSJohn Baldwin csp->csp_auth_klen, &s->hash.octx); 224a079e38bSJohn Baldwin } 225ba610be9SJohn Baldwin fpu_kern_leave(curthread, NULL); 226ba610be9SJohn Baldwin } 227ba610be9SJohn Baldwin } 22892aecd1eSJohn Baldwin } 22992aecd1eSJohn Baldwin 23092aecd1eSJohn Baldwin static int 231197ff4c3SKornel Duleba ossl_newsession_cipher(struct ossl_session *s, 232197ff4c3SKornel Duleba const struct crypto_session_params *csp) 233197ff4c3SKornel Duleba { 234197ff4c3SKornel Duleba struct ossl_cipher *cipher; 235197ff4c3SKornel Duleba int error = 0; 236197ff4c3SKornel Duleba 237197ff4c3SKornel Duleba cipher = ossl_lookup_cipher(csp); 238197ff4c3SKornel Duleba if (cipher == NULL) 239197ff4c3SKornel Duleba return (EINVAL); 240197ff4c3SKornel Duleba 241197ff4c3SKornel Duleba s->cipher.cipher = cipher; 242197ff4c3SKornel Duleba 243197ff4c3SKornel Duleba if (csp->csp_cipher_key == NULL) 244197ff4c3SKornel Duleba return (0); 245197ff4c3SKornel Duleba 246197ff4c3SKornel Duleba fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); 247197ff4c3SKornel Duleba if (cipher->set_encrypt_key != NULL) { 248197ff4c3SKornel Duleba error = cipher->set_encrypt_key(csp->csp_cipher_key, 249197ff4c3SKornel Duleba 8 * csp->csp_cipher_klen, &s->cipher.enc_ctx); 250197ff4c3SKornel Duleba if (error != 0) { 251197ff4c3SKornel Duleba fpu_kern_leave(curthread, NULL); 252197ff4c3SKornel Duleba return (error); 253197ff4c3SKornel Duleba } 254197ff4c3SKornel Duleba } 255197ff4c3SKornel Duleba if (cipher->set_decrypt_key != NULL) 256197ff4c3SKornel Duleba error = cipher->set_decrypt_key(csp->csp_cipher_key, 257197ff4c3SKornel Duleba 8 * csp->csp_cipher_klen, &s->cipher.dec_ctx); 258197ff4c3SKornel Duleba fpu_kern_leave(curthread, NULL); 259197ff4c3SKornel Duleba 260197ff4c3SKornel Duleba return (error); 261197ff4c3SKornel Duleba } 262197ff4c3SKornel Duleba 263197ff4c3SKornel Duleba static int 26492aecd1eSJohn Baldwin ossl_newsession(device_t dev, crypto_session_t cses, 26592aecd1eSJohn Baldwin const struct crypto_session_params *csp) 26692aecd1eSJohn Baldwin { 26792aecd1eSJohn Baldwin struct ossl_session *s; 268197ff4c3SKornel Duleba int error = 0; 26992aecd1eSJohn Baldwin 27092aecd1eSJohn Baldwin s = crypto_get_driver_session(cses); 27192aecd1eSJohn Baldwin switch (csp->csp_mode) { 27292aecd1eSJohn Baldwin case CSP_MODE_DIGEST: 27392aecd1eSJohn Baldwin ossl_newsession_hash(s, csp); 27492aecd1eSJohn Baldwin break; 275197ff4c3SKornel Duleba case CSP_MODE_CIPHER: 276197ff4c3SKornel Duleba error = ossl_newsession_cipher(s, csp); 277197ff4c3SKornel Duleba break; 278c4026909SKornel Duleba case CSP_MODE_ETA: 279c4026909SKornel Duleba ossl_newsession_hash(s, csp); 280c4026909SKornel Duleba error = ossl_newsession_cipher(s, csp); 281c4026909SKornel Duleba break; 28292aecd1eSJohn Baldwin } 28392aecd1eSJohn Baldwin 284197ff4c3SKornel Duleba return (error); 285ba610be9SJohn Baldwin } 286ba610be9SJohn Baldwin 287ba610be9SJohn Baldwin static int 28892aecd1eSJohn Baldwin ossl_process_hash(struct ossl_session *s, struct cryptop *crp, 28992aecd1eSJohn Baldwin const struct crypto_session_params *csp) 290ba610be9SJohn Baldwin { 291ba610be9SJohn Baldwin struct ossl_hash_context ctx; 292ba610be9SJohn Baldwin char digest[HASH_MAX_LEN]; 293ba610be9SJohn Baldwin struct auth_hash *axf; 294ba610be9SJohn Baldwin int error; 295ba610be9SJohn Baldwin 296ba610be9SJohn Baldwin axf = s->hash.axf; 297ba610be9SJohn Baldwin 298a079e38bSJohn Baldwin if (crp->crp_auth_key == NULL) { 299ba610be9SJohn Baldwin ctx = s->hash.ictx; 300a079e38bSJohn Baldwin } else { 301a079e38bSJohn Baldwin if (axf->Setkey != NULL) { 302a079e38bSJohn Baldwin axf->Init(&ctx); 303a079e38bSJohn Baldwin axf->Setkey(&ctx, crp->crp_auth_key, 304a079e38bSJohn Baldwin csp->csp_auth_klen); 305a079e38bSJohn Baldwin } else { 306a079e38bSJohn Baldwin hmac_init_ipad(axf, crp->crp_auth_key, 307a079e38bSJohn Baldwin csp->csp_auth_klen, &ctx); 308a079e38bSJohn Baldwin } 309a079e38bSJohn Baldwin } 310ba610be9SJohn Baldwin 311ba610be9SJohn Baldwin if (crp->crp_aad != NULL) 312ba610be9SJohn Baldwin error = axf->Update(&ctx, crp->crp_aad, crp->crp_aad_length); 313ba610be9SJohn Baldwin else 314ba610be9SJohn Baldwin error = crypto_apply(crp, crp->crp_aad_start, 315ba610be9SJohn Baldwin crp->crp_aad_length, axf->Update, &ctx); 316ba610be9SJohn Baldwin if (error) 317ba610be9SJohn Baldwin goto out; 318ba610be9SJohn Baldwin 319ba610be9SJohn Baldwin error = crypto_apply(crp, crp->crp_payload_start, 320ba610be9SJohn Baldwin crp->crp_payload_length, axf->Update, &ctx); 321ba610be9SJohn Baldwin if (error) 322ba610be9SJohn Baldwin goto out; 323ba610be9SJohn Baldwin 324ba610be9SJohn Baldwin axf->Final(digest, &ctx); 325ba610be9SJohn Baldwin 326a079e38bSJohn Baldwin if (csp->csp_auth_klen != 0 && axf->Setkey == NULL) { 327a079e38bSJohn Baldwin if (crp->crp_auth_key == NULL) 328ba610be9SJohn Baldwin ctx = s->hash.octx; 329a079e38bSJohn Baldwin else 330a079e38bSJohn Baldwin hmac_init_opad(axf, crp->crp_auth_key, 331a079e38bSJohn Baldwin csp->csp_auth_klen, &ctx); 332ba610be9SJohn Baldwin axf->Update(&ctx, digest, axf->hashsize); 333ba610be9SJohn Baldwin axf->Final(digest, &ctx); 334ba610be9SJohn Baldwin } 335ba610be9SJohn Baldwin 336ba610be9SJohn Baldwin if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) { 337ba610be9SJohn Baldwin char digest2[HASH_MAX_LEN]; 338ba610be9SJohn Baldwin 339ba610be9SJohn Baldwin crypto_copydata(crp, crp->crp_digest_start, s->hash.mlen, 340ba610be9SJohn Baldwin digest2); 341ba610be9SJohn Baldwin if (timingsafe_bcmp(digest, digest2, s->hash.mlen) != 0) 342ba610be9SJohn Baldwin error = EBADMSG; 343ba610be9SJohn Baldwin explicit_bzero(digest2, sizeof(digest2)); 344ba610be9SJohn Baldwin } else { 345ba610be9SJohn Baldwin crypto_copyback(crp, crp->crp_digest_start, s->hash.mlen, 346ba610be9SJohn Baldwin digest); 347ba610be9SJohn Baldwin } 348ba610be9SJohn Baldwin explicit_bzero(digest, sizeof(digest)); 349ba610be9SJohn Baldwin 350ba610be9SJohn Baldwin out: 35192aecd1eSJohn Baldwin explicit_bzero(&ctx, sizeof(ctx)); 35292aecd1eSJohn Baldwin return (error); 35392aecd1eSJohn Baldwin } 35492aecd1eSJohn Baldwin 35592aecd1eSJohn Baldwin static int 356c4026909SKornel Duleba ossl_process_eta(struct ossl_session *s, struct cryptop *crp, 357c4026909SKornel Duleba const struct crypto_session_params *csp) 358c4026909SKornel Duleba { 359c4026909SKornel Duleba int error; 360c4026909SKornel Duleba 361c4026909SKornel Duleba if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { 362c4026909SKornel Duleba error = s->cipher.cipher->process(&s->cipher, crp, csp); 363c4026909SKornel Duleba if (error == 0) 364c4026909SKornel Duleba error = ossl_process_hash(s, crp, csp); 365c4026909SKornel Duleba } else { 366c4026909SKornel Duleba error = ossl_process_hash(s, crp, csp); 367c4026909SKornel Duleba if (error == 0) 368c4026909SKornel Duleba error = s->cipher.cipher->process(&s->cipher, crp, csp); 369c4026909SKornel Duleba } 370c4026909SKornel Duleba 371c4026909SKornel Duleba return (error); 372c4026909SKornel Duleba } 373c4026909SKornel Duleba 374c4026909SKornel Duleba static int 37592aecd1eSJohn Baldwin ossl_process(device_t dev, struct cryptop *crp, int hint) 37692aecd1eSJohn Baldwin { 37792aecd1eSJohn Baldwin const struct crypto_session_params *csp; 37892aecd1eSJohn Baldwin struct ossl_session *s; 37992aecd1eSJohn Baldwin int error; 38092aecd1eSJohn Baldwin bool fpu_entered; 38192aecd1eSJohn Baldwin 38292aecd1eSJohn Baldwin s = crypto_get_driver_session(crp->crp_session); 38392aecd1eSJohn Baldwin csp = crypto_get_params(crp->crp_session); 38492aecd1eSJohn Baldwin 38592aecd1eSJohn Baldwin if (is_fpu_kern_thread(0)) { 38692aecd1eSJohn Baldwin fpu_entered = false; 38792aecd1eSJohn Baldwin } else { 38892aecd1eSJohn Baldwin fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); 38992aecd1eSJohn Baldwin fpu_entered = true; 39092aecd1eSJohn Baldwin } 39192aecd1eSJohn Baldwin 39292aecd1eSJohn Baldwin switch (csp->csp_mode) { 39392aecd1eSJohn Baldwin case CSP_MODE_DIGEST: 39492aecd1eSJohn Baldwin error = ossl_process_hash(s, crp, csp); 39592aecd1eSJohn Baldwin break; 39692aecd1eSJohn Baldwin case CSP_MODE_CIPHER: 397197ff4c3SKornel Duleba error = s->cipher.cipher->process(&s->cipher, crp, csp); 39892aecd1eSJohn Baldwin break; 399c4026909SKornel Duleba case CSP_MODE_ETA: 400c4026909SKornel Duleba error = ossl_process_eta(s, crp, csp); 401c4026909SKornel Duleba break; 40278991a93SJohn Baldwin case CSP_MODE_AEAD: 40378991a93SJohn Baldwin if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) 40478991a93SJohn Baldwin error = ossl_chacha20_poly1305_encrypt(crp, csp); 40578991a93SJohn Baldwin else 40678991a93SJohn Baldwin error = ossl_chacha20_poly1305_decrypt(crp, csp); 40778991a93SJohn Baldwin break; 40892aecd1eSJohn Baldwin default: 40992aecd1eSJohn Baldwin __assert_unreachable(); 41092aecd1eSJohn Baldwin } 41192aecd1eSJohn Baldwin 412ba610be9SJohn Baldwin if (fpu_entered) 413ba610be9SJohn Baldwin fpu_kern_leave(curthread, NULL); 414ba610be9SJohn Baldwin 415ba610be9SJohn Baldwin crp->crp_etype = error; 416ba610be9SJohn Baldwin crypto_done(crp); 417ba610be9SJohn Baldwin 418ba610be9SJohn Baldwin return (0); 419ba610be9SJohn Baldwin } 420ba610be9SJohn Baldwin 421ba610be9SJohn Baldwin static device_method_t ossl_methods[] = { 422ba610be9SJohn Baldwin DEVMETHOD(device_identify, ossl_identify), 423ba610be9SJohn Baldwin DEVMETHOD(device_probe, ossl_probe), 424ba610be9SJohn Baldwin DEVMETHOD(device_attach, ossl_attach), 425ba610be9SJohn Baldwin DEVMETHOD(device_detach, ossl_detach), 426ba610be9SJohn Baldwin 427ba610be9SJohn Baldwin DEVMETHOD(cryptodev_probesession, ossl_probesession), 428ba610be9SJohn Baldwin DEVMETHOD(cryptodev_newsession, ossl_newsession), 429ba610be9SJohn Baldwin DEVMETHOD(cryptodev_process, ossl_process), 430ba610be9SJohn Baldwin 431ba610be9SJohn Baldwin DEVMETHOD_END 432ba610be9SJohn Baldwin }; 433ba610be9SJohn Baldwin 434ba610be9SJohn Baldwin static driver_t ossl_driver = { 435ba610be9SJohn Baldwin "ossl", 436ba610be9SJohn Baldwin ossl_methods, 437ba610be9SJohn Baldwin sizeof(struct ossl_softc) 438ba610be9SJohn Baldwin }; 439ba610be9SJohn Baldwin 440ab050b2bSJohn Baldwin DRIVER_MODULE(ossl, nexus, ossl_driver, NULL, NULL); 441ba610be9SJohn Baldwin MODULE_VERSION(ossl, 1); 442ba610be9SJohn Baldwin MODULE_DEPEND(ossl, crypto, 1, 1, 1); 443