15f270659SKonstantin Belousov /*-
25f270659SKonstantin Belousov * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
35f270659SKonstantin Belousov * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
48e6af6adSJohn Baldwin * Copyright (c) 2014-2021 The FreeBSD Foundation
5fe182ba1SConrad Meyer * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
65f270659SKonstantin Belousov * All rights reserved.
75f270659SKonstantin Belousov *
808fca7a5SJohn-Mark Gurney * Portions of this software were developed by John-Mark Gurney
908fca7a5SJohn-Mark Gurney * under sponsorship of the FreeBSD Foundation and
1008fca7a5SJohn-Mark Gurney * Rubicon Communications, LLC (Netgate).
1108fca7a5SJohn-Mark Gurney *
128e6af6adSJohn Baldwin * Portions of this software were developed by Ararat River
138e6af6adSJohn Baldwin * Consulting, LLC under sponsorship of the FreeBSD Foundation.
148e6af6adSJohn Baldwin *
155f270659SKonstantin Belousov * Redistribution and use in source and binary forms, with or without
165f270659SKonstantin Belousov * modification, are permitted provided that the following conditions
175f270659SKonstantin Belousov * are met:
185f270659SKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
195f270659SKonstantin Belousov * notice, this list of conditions and the following disclaimer.
205f270659SKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
215f270659SKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
225f270659SKonstantin Belousov * documentation and/or other materials provided with the distribution.
235f270659SKonstantin Belousov *
245f270659SKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
255f270659SKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
265f270659SKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
275f270659SKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
285f270659SKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
295f270659SKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
305f270659SKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
315f270659SKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
325f270659SKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
335f270659SKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
345f270659SKonstantin Belousov * SUCH DAMAGE.
355f270659SKonstantin Belousov */
365f270659SKonstantin Belousov
375f270659SKonstantin Belousov #include <sys/param.h>
38e2e050c8SConrad Meyer #include <sys/bus.h>
395f270659SKonstantin Belousov #include <sys/kernel.h>
405f270659SKonstantin Belousov #include <sys/kobj.h>
415f270659SKonstantin Belousov #include <sys/libkern.h>
425f270659SKonstantin Belousov #include <sys/malloc.h>
4308fca7a5SJohn-Mark Gurney #include <sys/mbuf.h>
44e2e050c8SConrad Meyer #include <sys/module.h>
459d38fd07SJohn-Mark Gurney #include <sys/smp.h>
46e2e050c8SConrad Meyer #include <sys/systm.h>
47e2e050c8SConrad Meyer #include <sys/uio.h>
48fe182ba1SConrad Meyer
495f270659SKonstantin Belousov #include <crypto/aesni/aesni.h>
50fe182ba1SConrad Meyer #include <crypto/aesni/sha_sse.h>
51fe182ba1SConrad Meyer #include <crypto/sha1.h>
522ec76e3eSConrad Meyer #include <crypto/sha2/sha224.h>
53fe182ba1SConrad Meyer #include <crypto/sha2/sha256.h>
54fe182ba1SConrad Meyer
55fe182ba1SConrad Meyer #include <opencrypto/cryptodev.h>
5608fca7a5SJohn-Mark Gurney #include <opencrypto/gmac.h>
57fe182ba1SConrad Meyer #include <cryptodev_if.h>
58fe182ba1SConrad Meyer
59fe182ba1SConrad Meyer #include <machine/md_var.h>
60fe182ba1SConrad Meyer #include <machine/specialreg.h>
61fe182ba1SConrad Meyer #include <machine/fpu.h>
625f270659SKonstantin Belousov
635f270659SKonstantin Belousov struct aesni_softc {
645f270659SKonstantin Belousov int32_t cid;
65fe182ba1SConrad Meyer bool has_aes;
66fe182ba1SConrad Meyer bool has_sha;
675f270659SKonstantin Belousov };
685f270659SKonstantin Belousov
6927007c65SKonstantin Belousov static int aesni_cipher_setup(struct aesni_session *ses,
70c0341432SJohn Baldwin const struct crypto_session_params *csp);
71c0341432SJohn Baldwin static int aesni_cipher_process(struct aesni_session *ses, struct cryptop *crp);
72c0341432SJohn Baldwin static int aesni_cipher_crypt(struct aesni_session *ses, struct cryptop *crp,
73c0341432SJohn Baldwin const struct crypto_session_params *csp);
74c0341432SJohn Baldwin static int aesni_cipher_mac(struct aesni_session *ses, struct cryptop *crp,
75c0341432SJohn Baldwin const struct crypto_session_params *csp);
765f270659SKonstantin Belousov
775f270659SKonstantin Belousov MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
785f270659SKonstantin Belousov
795f270659SKonstantin Belousov static void
aesni_identify(driver_t * drv,device_t parent)805f270659SKonstantin Belousov aesni_identify(driver_t *drv, device_t parent)
815f270659SKonstantin Belousov {
825f270659SKonstantin Belousov
835f270659SKonstantin Belousov /* NB: order 10 is so we get attached after h/w devices */
84*b670c9baSAhmad Khalifa if (device_find_child(parent, "aesni", DEVICE_UNIT_ANY) == NULL &&
85*b670c9baSAhmad Khalifa BUS_ADD_CHILD(parent, 10, "aesni", DEVICE_UNIT_ANY) == 0)
865f270659SKonstantin Belousov panic("aesni: could not attach");
875f270659SKonstantin Belousov }
885f270659SKonstantin Belousov
89fe182ba1SConrad Meyer static void
detect_cpu_features(bool * has_aes,bool * has_sha)90fe182ba1SConrad Meyer detect_cpu_features(bool *has_aes, bool *has_sha)
91fe182ba1SConrad Meyer {
92fe182ba1SConrad Meyer
93fe182ba1SConrad Meyer *has_aes = ((cpu_feature2 & CPUID2_AESNI) != 0 &&
94fe182ba1SConrad Meyer (cpu_feature2 & CPUID2_SSE41) != 0);
95fe182ba1SConrad Meyer *has_sha = ((cpu_stdext_feature & CPUID_STDEXT_SHA) != 0 &&
96fe182ba1SConrad Meyer (cpu_feature2 & CPUID2_SSSE3) != 0);
97fe182ba1SConrad Meyer }
98fe182ba1SConrad Meyer
995f270659SKonstantin Belousov static int
aesni_probe(device_t dev)1005f270659SKonstantin Belousov aesni_probe(device_t dev)
1015f270659SKonstantin Belousov {
102fe182ba1SConrad Meyer bool has_aes, has_sha;
1035f270659SKonstantin Belousov
104fe182ba1SConrad Meyer detect_cpu_features(&has_aes, &has_sha);
105fe182ba1SConrad Meyer if (!has_aes && !has_sha) {
106fe182ba1SConrad Meyer device_printf(dev, "No AES or SHA support.\n");
1075f270659SKonstantin Belousov return (EINVAL);
108fe182ba1SConrad Meyer } else if (has_aes && has_sha)
109fe182ba1SConrad Meyer device_set_desc(dev,
1107cff9f37SSean Eric Fagan "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS,SHA1,SHA256");
111fe182ba1SConrad Meyer else if (has_aes)
1127cff9f37SSean Eric Fagan device_set_desc(dev,
1137cff9f37SSean Eric Fagan "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS");
114fe182ba1SConrad Meyer else
115fe182ba1SConrad Meyer device_set_desc(dev, "SHA1,SHA256");
116ff6c7bf5SJohn-Mark Gurney
1175f270659SKonstantin Belousov return (0);
1185f270659SKonstantin Belousov }
1195f270659SKonstantin Belousov
1205f270659SKonstantin Belousov static int
aesni_attach(device_t dev)1215f270659SKonstantin Belousov aesni_attach(device_t dev)
1225f270659SKonstantin Belousov {
1235f270659SKonstantin Belousov struct aesni_softc *sc;
1245f270659SKonstantin Belousov
1255f270659SKonstantin Belousov sc = device_get_softc(dev);
1269d38fd07SJohn-Mark Gurney
1271b0909d5SConrad Meyer sc->cid = crypto_get_driverid(dev, sizeof(struct aesni_session),
128a3d565a1SJohn Baldwin CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_SYNC |
129a3d565a1SJohn Baldwin CRYPTOCAP_F_ACCEL_SOFTWARE);
1305f270659SKonstantin Belousov if (sc->cid < 0) {
1315f270659SKonstantin Belousov device_printf(dev, "Could not get crypto driver id.\n");
1325f270659SKonstantin Belousov return (ENOMEM);
1335f270659SKonstantin Belousov }
1345f270659SKonstantin Belousov
135fe182ba1SConrad Meyer detect_cpu_features(&sc->has_aes, &sc->has_sha);
1365f270659SKonstantin Belousov return (0);
1375f270659SKonstantin Belousov }
1385f270659SKonstantin Belousov
1395f270659SKonstantin Belousov static int
aesni_detach(device_t dev)1405f270659SKonstantin Belousov aesni_detach(device_t dev)
1415f270659SKonstantin Belousov {
1425f270659SKonstantin Belousov struct aesni_softc *sc;
1435f270659SKonstantin Belousov
1445f270659SKonstantin Belousov sc = device_get_softc(dev);
1459d38fd07SJohn-Mark Gurney
1465f270659SKonstantin Belousov crypto_unregister_all(sc->cid);
1479d38fd07SJohn-Mark Gurney
1485f270659SKonstantin Belousov return (0);
1495f270659SKonstantin Belousov }
1505f270659SKonstantin Belousov
151c0341432SJohn Baldwin static bool
aesni_auth_supported(struct aesni_softc * sc,const struct crypto_session_params * csp)152c0341432SJohn Baldwin aesni_auth_supported(struct aesni_softc *sc,
153c0341432SJohn Baldwin const struct crypto_session_params *csp)
154c0341432SJohn Baldwin {
155c0341432SJohn Baldwin
156c0341432SJohn Baldwin if (!sc->has_sha)
157c0341432SJohn Baldwin return (false);
158c0341432SJohn Baldwin
159c0341432SJohn Baldwin switch (csp->csp_auth_alg) {
160c0341432SJohn Baldwin case CRYPTO_SHA1:
161c0341432SJohn Baldwin case CRYPTO_SHA2_224:
162c0341432SJohn Baldwin case CRYPTO_SHA2_256:
163c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC:
164c0341432SJohn Baldwin case CRYPTO_SHA2_224_HMAC:
165c0341432SJohn Baldwin case CRYPTO_SHA2_256_HMAC:
166c0341432SJohn Baldwin break;
167c0341432SJohn Baldwin default:
168c0341432SJohn Baldwin return (false);
169c0341432SJohn Baldwin }
170c0341432SJohn Baldwin
171c0341432SJohn Baldwin return (true);
172c0341432SJohn Baldwin }
173c0341432SJohn Baldwin
174c0341432SJohn Baldwin static bool
aesni_cipher_supported(struct aesni_softc * sc,const struct crypto_session_params * csp)175c0341432SJohn Baldwin aesni_cipher_supported(struct aesni_softc *sc,
176c0341432SJohn Baldwin const struct crypto_session_params *csp)
177c0341432SJohn Baldwin {
178c0341432SJohn Baldwin
179c0341432SJohn Baldwin if (!sc->has_aes)
180c0341432SJohn Baldwin return (false);
181c0341432SJohn Baldwin
182c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
183c0341432SJohn Baldwin case CRYPTO_AES_CBC:
184c0341432SJohn Baldwin case CRYPTO_AES_ICM:
18519510525SJohn Baldwin switch (csp->csp_cipher_klen * 8) {
18619510525SJohn Baldwin case 128:
18719510525SJohn Baldwin case 192:
18819510525SJohn Baldwin case 256:
18919510525SJohn Baldwin break;
19019510525SJohn Baldwin default:
19119510525SJohn Baldwin CRYPTDEB("invalid CBC/ICM key length");
19219510525SJohn Baldwin return (false);
19319510525SJohn Baldwin }
194c0341432SJohn Baldwin if (csp->csp_ivlen != AES_BLOCK_LEN)
195c0341432SJohn Baldwin return (false);
19619510525SJohn Baldwin break;
197c0341432SJohn Baldwin case CRYPTO_AES_XTS:
19819510525SJohn Baldwin switch (csp->csp_cipher_klen * 8) {
19919510525SJohn Baldwin case 256:
20019510525SJohn Baldwin case 512:
20119510525SJohn Baldwin break;
20219510525SJohn Baldwin default:
20319510525SJohn Baldwin CRYPTDEB("invalid XTS key length");
20419510525SJohn Baldwin return (false);
20519510525SJohn Baldwin }
206c0341432SJohn Baldwin if (csp->csp_ivlen != AES_XTS_IV_LEN)
207c0341432SJohn Baldwin return (false);
20819510525SJohn Baldwin break;
209c0341432SJohn Baldwin default:
210c0341432SJohn Baldwin return (false);
211c0341432SJohn Baldwin }
21219510525SJohn Baldwin
21319510525SJohn Baldwin return (true);
214c0341432SJohn Baldwin }
215c0341432SJohn Baldwin
216efac54cbSMarcin Wojtas #define SUPPORTED_SES (CSP_F_SEPARATE_OUTPUT | CSP_F_SEPARATE_AAD | CSP_F_ESN)
217efac54cbSMarcin Wojtas
2185f270659SKonstantin Belousov static int
aesni_probesession(device_t dev,const struct crypto_session_params * csp)219c0341432SJohn Baldwin aesni_probesession(device_t dev, const struct crypto_session_params *csp)
220c0341432SJohn Baldwin {
221c0341432SJohn Baldwin struct aesni_softc *sc;
222c0341432SJohn Baldwin
223c0341432SJohn Baldwin sc = device_get_softc(dev);
224efac54cbSMarcin Wojtas if ((csp->csp_flags & ~(SUPPORTED_SES)) != 0)
225c0341432SJohn Baldwin return (EINVAL);
226c0341432SJohn Baldwin switch (csp->csp_mode) {
227c0341432SJohn Baldwin case CSP_MODE_DIGEST:
228c0341432SJohn Baldwin if (!aesni_auth_supported(sc, csp))
229c0341432SJohn Baldwin return (EINVAL);
230c0341432SJohn Baldwin break;
231c0341432SJohn Baldwin case CSP_MODE_CIPHER:
232c0341432SJohn Baldwin if (!aesni_cipher_supported(sc, csp))
233c0341432SJohn Baldwin return (EINVAL);
234c0341432SJohn Baldwin break;
235c0341432SJohn Baldwin case CSP_MODE_AEAD:
236c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
237c0341432SJohn Baldwin case CRYPTO_AES_NIST_GCM_16:
23819510525SJohn Baldwin switch (csp->csp_cipher_klen * 8) {
23919510525SJohn Baldwin case 128:
24019510525SJohn Baldwin case 192:
24119510525SJohn Baldwin case 256:
24219510525SJohn Baldwin break;
24319510525SJohn Baldwin default:
24419510525SJohn Baldwin CRYPTDEB("invalid GCM key length");
24519510525SJohn Baldwin return (EINVAL);
24619510525SJohn Baldwin }
247c0341432SJohn Baldwin if (csp->csp_auth_mlen != 0 &&
248c0341432SJohn Baldwin csp->csp_auth_mlen != GMAC_DIGEST_LEN)
249c0341432SJohn Baldwin return (EINVAL);
2506e17a2e0SJohn Baldwin if (!sc->has_aes)
251c0341432SJohn Baldwin return (EINVAL);
252c0341432SJohn Baldwin break;
253c0341432SJohn Baldwin case CRYPTO_AES_CCM_16:
25419510525SJohn Baldwin switch (csp->csp_cipher_klen * 8) {
25519510525SJohn Baldwin case 128:
25619510525SJohn Baldwin case 192:
25719510525SJohn Baldwin case 256:
25819510525SJohn Baldwin break;
25919510525SJohn Baldwin default:
26019510525SJohn Baldwin CRYPTDEB("invalid CCM key length");
26119510525SJohn Baldwin return (EINVAL);
26219510525SJohn Baldwin }
2638e6af6adSJohn Baldwin if (!sc->has_aes)
264c0341432SJohn Baldwin return (EINVAL);
265c0341432SJohn Baldwin break;
266c0341432SJohn Baldwin default:
267c0341432SJohn Baldwin return (EINVAL);
268c0341432SJohn Baldwin }
269c0341432SJohn Baldwin break;
270c0341432SJohn Baldwin case CSP_MODE_ETA:
271c0341432SJohn Baldwin if (!aesni_auth_supported(sc, csp) ||
272c0341432SJohn Baldwin !aesni_cipher_supported(sc, csp))
273c0341432SJohn Baldwin return (EINVAL);
274c0341432SJohn Baldwin break;
275c0341432SJohn Baldwin default:
276c0341432SJohn Baldwin return (EINVAL);
277c0341432SJohn Baldwin }
278c0341432SJohn Baldwin
279c0341432SJohn Baldwin return (CRYPTODEV_PROBE_ACCEL_SOFTWARE);
280c0341432SJohn Baldwin }
281c0341432SJohn Baldwin
282c0341432SJohn Baldwin static int
aesni_newsession(device_t dev,crypto_session_t cses,const struct crypto_session_params * csp)283c0341432SJohn Baldwin aesni_newsession(device_t dev, crypto_session_t cses,
284c0341432SJohn Baldwin const struct crypto_session_params *csp)
2855f270659SKonstantin Belousov {
2865f270659SKonstantin Belousov struct aesni_session *ses;
2875f270659SKonstantin Belousov int error;
2885f270659SKonstantin Belousov
2891b0909d5SConrad Meyer ses = crypto_get_driver_session(cses);
2901b0909d5SConrad Meyer
291c0341432SJohn Baldwin switch (csp->csp_mode) {
292c0341432SJohn Baldwin case CSP_MODE_DIGEST:
293c0341432SJohn Baldwin case CSP_MODE_CIPHER:
294c0341432SJohn Baldwin case CSP_MODE_AEAD:
295c0341432SJohn Baldwin case CSP_MODE_ETA:
29608fca7a5SJohn-Mark Gurney break;
2975f270659SKonstantin Belousov default:
2985f270659SKonstantin Belousov return (EINVAL);
2995f270659SKonstantin Belousov }
300c0341432SJohn Baldwin error = aesni_cipher_setup(ses, csp);
3015f270659SKonstantin Belousov if (error != 0) {
30208fca7a5SJohn-Mark Gurney CRYPTDEB("setup failed");
3035f270659SKonstantin Belousov return (error);
3045f270659SKonstantin Belousov }
3055f270659SKonstantin Belousov
3065f270659SKonstantin Belousov return (0);
3075f270659SKonstantin Belousov }
3085f270659SKonstantin Belousov
3095f270659SKonstantin Belousov static int
aesni_process(device_t dev,struct cryptop * crp,int hint __unused)3105f270659SKonstantin Belousov aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
3115f270659SKonstantin Belousov {
3128475a417SConrad Meyer struct aesni_session *ses;
313c0341432SJohn Baldwin int error;
3145f270659SKonstantin Belousov
3151b0909d5SConrad Meyer ses = crypto_get_driver_session(crp->crp_session);
3165f270659SKonstantin Belousov
317c0341432SJohn Baldwin error = aesni_cipher_process(ses, crp);
3185f270659SKonstantin Belousov
3195f270659SKonstantin Belousov crp->crp_etype = error;
3205f270659SKonstantin Belousov crypto_done(crp);
321c0341432SJohn Baldwin return (0);
3225f270659SKonstantin Belousov }
3235f270659SKonstantin Belousov
324fe182ba1SConrad Meyer static uint8_t *
aesni_cipher_alloc(struct cryptop * crp,int start,int length,bool * allocated)325c0341432SJohn Baldwin aesni_cipher_alloc(struct cryptop *crp, int start, int length, bool *allocated)
3265f270659SKonstantin Belousov {
3275f270659SKonstantin Belousov uint8_t *addr;
3285f270659SKonstantin Belousov
329c0341432SJohn Baldwin addr = crypto_contiguous_subsegment(crp, start, length);
330ff2038a9SMatt Macy if (addr != NULL) {
331fe182ba1SConrad Meyer *allocated = false;
3325f270659SKonstantin Belousov return (addr);
333ff2038a9SMatt Macy }
334c0341432SJohn Baldwin addr = malloc(length, M_AESNI, M_NOWAIT);
3355f270659SKonstantin Belousov if (addr != NULL) {
336fe182ba1SConrad Meyer *allocated = true;
337c0341432SJohn Baldwin crypto_copydata(crp, start, length, addr);
3385f270659SKonstantin Belousov } else
339fe182ba1SConrad Meyer *allocated = false;
3405f270659SKonstantin Belousov return (addr);
3415f270659SKonstantin Belousov }
3425f270659SKonstantin Belousov
3435f270659SKonstantin Belousov static device_method_t aesni_methods[] = {
3445f270659SKonstantin Belousov DEVMETHOD(device_identify, aesni_identify),
3455f270659SKonstantin Belousov DEVMETHOD(device_probe, aesni_probe),
3465f270659SKonstantin Belousov DEVMETHOD(device_attach, aesni_attach),
3475f270659SKonstantin Belousov DEVMETHOD(device_detach, aesni_detach),
3485f270659SKonstantin Belousov
349c0341432SJohn Baldwin DEVMETHOD(cryptodev_probesession, aesni_probesession),
3505f270659SKonstantin Belousov DEVMETHOD(cryptodev_newsession, aesni_newsession),
3515f270659SKonstantin Belousov DEVMETHOD(cryptodev_process, aesni_process),
3525f270659SKonstantin Belousov
3538475a417SConrad Meyer DEVMETHOD_END
3545f270659SKonstantin Belousov };
3555f270659SKonstantin Belousov
3565f270659SKonstantin Belousov static driver_t aesni_driver = {
3575f270659SKonstantin Belousov "aesni",
3585f270659SKonstantin Belousov aesni_methods,
3595f270659SKonstantin Belousov sizeof(struct aesni_softc),
3605f270659SKonstantin Belousov };
3615f270659SKonstantin Belousov
362ab050b2bSJohn Baldwin DRIVER_MODULE(aesni, nexus, aesni_driver, 0, 0);
3635f270659SKonstantin Belousov MODULE_VERSION(aesni, 1);
3645f270659SKonstantin Belousov MODULE_DEPEND(aesni, crypto, 1, 1, 1);
36527007c65SKonstantin Belousov
366ccbaa67dSMarcin Wojtas static int
intel_sha1_update(void * vctx,const void * vdata,u_int datalen)3679b6b2f86SJohn Baldwin intel_sha1_update(void *vctx, const void *vdata, u_int datalen)
368fe182ba1SConrad Meyer {
369fe182ba1SConrad Meyer struct sha1_ctxt *ctx = vctx;
370fe182ba1SConrad Meyer const char *data = vdata;
371fe182ba1SConrad Meyer size_t gaplen;
372fe182ba1SConrad Meyer size_t gapstart;
373fe182ba1SConrad Meyer size_t off;
374fe182ba1SConrad Meyer size_t copysiz;
375fe182ba1SConrad Meyer u_int blocks;
376fe182ba1SConrad Meyer
377fe182ba1SConrad Meyer off = 0;
378fe182ba1SConrad Meyer /* Do any aligned blocks without redundant copying. */
379fe182ba1SConrad Meyer if (datalen >= 64 && ctx->count % 64 == 0) {
380fe182ba1SConrad Meyer blocks = datalen / 64;
381fe182ba1SConrad Meyer ctx->c.b64[0] += blocks * 64 * 8;
382fe182ba1SConrad Meyer intel_sha1_step(ctx->h.b32, data + off, blocks);
383fe182ba1SConrad Meyer off += blocks * 64;
384fe182ba1SConrad Meyer }
385fe182ba1SConrad Meyer
386fe182ba1SConrad Meyer while (off < datalen) {
387fe182ba1SConrad Meyer gapstart = ctx->count % 64;
388fe182ba1SConrad Meyer gaplen = 64 - gapstart;
389fe182ba1SConrad Meyer
390fe182ba1SConrad Meyer copysiz = (gaplen < datalen - off) ? gaplen : datalen - off;
391fe182ba1SConrad Meyer bcopy(&data[off], &ctx->m.b8[gapstart], copysiz);
392fe182ba1SConrad Meyer ctx->count += copysiz;
393fe182ba1SConrad Meyer ctx->count %= 64;
394fe182ba1SConrad Meyer ctx->c.b64[0] += copysiz * 8;
395fe182ba1SConrad Meyer if (ctx->count % 64 == 0)
396fe182ba1SConrad Meyer intel_sha1_step(ctx->h.b32, (void *)ctx->m.b8, 1);
397fe182ba1SConrad Meyer off += copysiz;
398fe182ba1SConrad Meyer }
399ccbaa67dSMarcin Wojtas
400ccbaa67dSMarcin Wojtas return (0);
401fe182ba1SConrad Meyer }
402fe182ba1SConrad Meyer
403fe182ba1SConrad Meyer static void
SHA1_Init_fn(void * ctx)404e2982a3eSConrad Meyer SHA1_Init_fn(void *ctx)
405e2982a3eSConrad Meyer {
406e2982a3eSConrad Meyer sha1_init(ctx);
407e2982a3eSConrad Meyer }
408e2982a3eSConrad Meyer
409e2982a3eSConrad Meyer static void
SHA1_Finalize_fn(void * digest,void * ctx)410fe182ba1SConrad Meyer SHA1_Finalize_fn(void *digest, void *ctx)
411fe182ba1SConrad Meyer {
412fe182ba1SConrad Meyer sha1_result(ctx, digest);
413fe182ba1SConrad Meyer }
414fe182ba1SConrad Meyer
415ccbaa67dSMarcin Wojtas static int
intel_sha256_update(void * vctx,const void * vdata,u_int len)4169b6b2f86SJohn Baldwin intel_sha256_update(void *vctx, const void *vdata, u_int len)
417fe182ba1SConrad Meyer {
418fe182ba1SConrad Meyer SHA256_CTX *ctx = vctx;
419fe182ba1SConrad Meyer uint64_t bitlen;
420fe182ba1SConrad Meyer uint32_t r;
421fe182ba1SConrad Meyer u_int blocks;
422fe182ba1SConrad Meyer const unsigned char *src = vdata;
423fe182ba1SConrad Meyer
424fe182ba1SConrad Meyer /* Number of bytes left in the buffer from previous updates */
425fe182ba1SConrad Meyer r = (ctx->count >> 3) & 0x3f;
426fe182ba1SConrad Meyer
427fe182ba1SConrad Meyer /* Convert the length into a number of bits */
428fe182ba1SConrad Meyer bitlen = len << 3;
429fe182ba1SConrad Meyer
430fe182ba1SConrad Meyer /* Update number of bits */
431fe182ba1SConrad Meyer ctx->count += bitlen;
432fe182ba1SConrad Meyer
433fe182ba1SConrad Meyer /* Handle the case where we don't need to perform any transforms */
434fe182ba1SConrad Meyer if (len < 64 - r) {
435fe182ba1SConrad Meyer memcpy(&ctx->buf[r], src, len);
436ccbaa67dSMarcin Wojtas return (0);
437fe182ba1SConrad Meyer }
438fe182ba1SConrad Meyer
439fe182ba1SConrad Meyer /* Finish the current block */
440fe182ba1SConrad Meyer memcpy(&ctx->buf[r], src, 64 - r);
441fe182ba1SConrad Meyer intel_sha256_step(ctx->state, ctx->buf, 1);
442fe182ba1SConrad Meyer src += 64 - r;
443fe182ba1SConrad Meyer len -= 64 - r;
444fe182ba1SConrad Meyer
445fe182ba1SConrad Meyer /* Perform complete blocks */
446fe182ba1SConrad Meyer if (len >= 64) {
447fe182ba1SConrad Meyer blocks = len / 64;
448fe182ba1SConrad Meyer intel_sha256_step(ctx->state, src, blocks);
449fe182ba1SConrad Meyer src += blocks * 64;
450fe182ba1SConrad Meyer len -= blocks * 64;
451fe182ba1SConrad Meyer }
452fe182ba1SConrad Meyer
453fe182ba1SConrad Meyer /* Copy left over data into buffer */
454fe182ba1SConrad Meyer memcpy(ctx->buf, src, len);
455ccbaa67dSMarcin Wojtas
456ccbaa67dSMarcin Wojtas return (0);
457fe182ba1SConrad Meyer }
458fe182ba1SConrad Meyer
459fe182ba1SConrad Meyer static void
SHA224_Init_fn(void * ctx)4602ec76e3eSConrad Meyer SHA224_Init_fn(void *ctx)
4612ec76e3eSConrad Meyer {
4622ec76e3eSConrad Meyer SHA224_Init(ctx);
4632ec76e3eSConrad Meyer }
4642ec76e3eSConrad Meyer
4652ec76e3eSConrad Meyer static void
SHA224_Finalize_fn(void * digest,void * ctx)4662ec76e3eSConrad Meyer SHA224_Finalize_fn(void *digest, void *ctx)
4672ec76e3eSConrad Meyer {
4682ec76e3eSConrad Meyer SHA224_Final(digest, ctx);
4692ec76e3eSConrad Meyer }
4702ec76e3eSConrad Meyer
4712ec76e3eSConrad Meyer static void
SHA256_Init_fn(void * ctx)472e2982a3eSConrad Meyer SHA256_Init_fn(void *ctx)
473e2982a3eSConrad Meyer {
474e2982a3eSConrad Meyer SHA256_Init(ctx);
475e2982a3eSConrad Meyer }
476e2982a3eSConrad Meyer
477e2982a3eSConrad Meyer static void
SHA256_Finalize_fn(void * digest,void * ctx)478fe182ba1SConrad Meyer SHA256_Finalize_fn(void *digest, void *ctx)
479fe182ba1SConrad Meyer {
480fe182ba1SConrad Meyer SHA256_Final(digest, ctx);
481fe182ba1SConrad Meyer }
482fe182ba1SConrad Meyer
483c0341432SJohn Baldwin static int
aesni_authprepare(struct aesni_session * ses,int klen)484c0341432SJohn Baldwin aesni_authprepare(struct aesni_session *ses, int klen)
485fe182ba1SConrad Meyer {
486fe182ba1SConrad Meyer
487c0341432SJohn Baldwin if (klen > SHA1_BLOCK_LEN)
488c0341432SJohn Baldwin return (EINVAL);
489c0341432SJohn Baldwin if ((ses->hmac && klen == 0) || (!ses->hmac && klen != 0))
490c0341432SJohn Baldwin return (EINVAL);
491c0341432SJohn Baldwin return (0);
492fe182ba1SConrad Meyer }
493fe182ba1SConrad Meyer
49427007c65SKonstantin Belousov static int
aesni_cipher_setup(struct aesni_session * ses,const struct crypto_session_params * csp)495c0341432SJohn Baldwin aesni_cipher_setup(struct aesni_session *ses,
496c0341432SJohn Baldwin const struct crypto_session_params *csp)
497c0341432SJohn Baldwin {
498098c902bSMark Johnston uint8_t *schedbase;
499937b4473SJohn Baldwin int error;
500937b4473SJohn Baldwin bool kt;
501c0341432SJohn Baldwin
502098c902bSMark Johnston schedbase = (uint8_t *)roundup2((uintptr_t)ses->schedules,
503098c902bSMark Johnston AES_SCHED_ALIGN);
504098c902bSMark Johnston ses->enc_schedule = schedbase;
505098c902bSMark Johnston ses->dec_schedule = schedbase + AES_SCHED_LEN;
506098c902bSMark Johnston ses->xts_schedule = schedbase + AES_SCHED_LEN * 2;
507098c902bSMark Johnston
508c0341432SJohn Baldwin switch (csp->csp_auth_alg) {
509c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC:
510c0341432SJohn Baldwin ses->hmac = true;
511c0341432SJohn Baldwin /* FALLTHROUGH */
512c0341432SJohn Baldwin case CRYPTO_SHA1:
513c0341432SJohn Baldwin ses->hash_len = SHA1_HASH_LEN;
514c0341432SJohn Baldwin ses->hash_init = SHA1_Init_fn;
515c0341432SJohn Baldwin ses->hash_update = intel_sha1_update;
516c0341432SJohn Baldwin ses->hash_finalize = SHA1_Finalize_fn;
517c0341432SJohn Baldwin break;
518c0341432SJohn Baldwin case CRYPTO_SHA2_224_HMAC:
519c0341432SJohn Baldwin ses->hmac = true;
520c0341432SJohn Baldwin /* FALLTHROUGH */
521c0341432SJohn Baldwin case CRYPTO_SHA2_224:
522c0341432SJohn Baldwin ses->hash_len = SHA2_224_HASH_LEN;
523c0341432SJohn Baldwin ses->hash_init = SHA224_Init_fn;
524c0341432SJohn Baldwin ses->hash_update = intel_sha256_update;
525c0341432SJohn Baldwin ses->hash_finalize = SHA224_Finalize_fn;
526c0341432SJohn Baldwin break;
527c0341432SJohn Baldwin case CRYPTO_SHA2_256_HMAC:
528c0341432SJohn Baldwin ses->hmac = true;
529c0341432SJohn Baldwin /* FALLTHROUGH */
530c0341432SJohn Baldwin case CRYPTO_SHA2_256:
531c0341432SJohn Baldwin ses->hash_len = SHA2_256_HASH_LEN;
532c0341432SJohn Baldwin ses->hash_init = SHA256_Init_fn;
533c0341432SJohn Baldwin ses->hash_update = intel_sha256_update;
534c0341432SJohn Baldwin ses->hash_finalize = SHA256_Finalize_fn;
535c0341432SJohn Baldwin break;
536c0341432SJohn Baldwin }
537c0341432SJohn Baldwin
538c0341432SJohn Baldwin if (ses->hash_len != 0) {
539c0341432SJohn Baldwin if (csp->csp_auth_mlen == 0)
540c0341432SJohn Baldwin ses->mlen = ses->hash_len;
541c0341432SJohn Baldwin else
542c0341432SJohn Baldwin ses->mlen = csp->csp_auth_mlen;
543c0341432SJohn Baldwin
544c0341432SJohn Baldwin error = aesni_authprepare(ses, csp->csp_auth_klen);
545c0341432SJohn Baldwin if (error != 0)
546c0341432SJohn Baldwin return (error);
547655eb762SJohn Baldwin } else if (csp->csp_cipher_alg == CRYPTO_AES_CCM_16) {
548655eb762SJohn Baldwin if (csp->csp_auth_mlen == 0)
549655eb762SJohn Baldwin ses->mlen = AES_CBC_MAC_HASH_LEN;
550655eb762SJohn Baldwin else
551655eb762SJohn Baldwin ses->mlen = csp->csp_auth_mlen;
552c0341432SJohn Baldwin }
553c0341432SJohn Baldwin
554937b4473SJohn Baldwin kt = (csp->csp_cipher_alg == 0);
555c0341432SJohn Baldwin if (!kt) {
556937b4473SJohn Baldwin fpu_kern_enter(curthread, NULL,
557937b4473SJohn Baldwin FPU_KERN_NORMAL | FPU_KERN_NOCTX);
558c0341432SJohn Baldwin }
559c0341432SJohn Baldwin
560c0341432SJohn Baldwin error = 0;
561c0341432SJohn Baldwin if (csp->csp_cipher_key != NULL)
562c0341432SJohn Baldwin aesni_cipher_setup_common(ses, csp, csp->csp_cipher_key,
563c0341432SJohn Baldwin csp->csp_cipher_klen);
564c0341432SJohn Baldwin
565c0341432SJohn Baldwin if (!kt) {
566937b4473SJohn Baldwin fpu_kern_leave(curthread, NULL);
567c0341432SJohn Baldwin }
568c0341432SJohn Baldwin return (error);
569c0341432SJohn Baldwin }
570c0341432SJohn Baldwin
571c0341432SJohn Baldwin static int
aesni_cipher_process(struct aesni_session * ses,struct cryptop * crp)572c0341432SJohn Baldwin aesni_cipher_process(struct aesni_session *ses, struct cryptop *crp)
573c0341432SJohn Baldwin {
574c0341432SJohn Baldwin const struct crypto_session_params *csp;
575937b4473SJohn Baldwin int error;
57608fca7a5SJohn-Mark Gurney
577c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session);
578c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
5798e6af6adSJohn Baldwin case CRYPTO_AES_CCM_16:
5808e6af6adSJohn Baldwin if (crp->crp_payload_length > ccm_max_payload_length(csp))
5818e6af6adSJohn Baldwin return (EMSGSIZE);
5828e6af6adSJohn Baldwin /* FALLTHROUGH */
583c0341432SJohn Baldwin case CRYPTO_AES_ICM:
584c0341432SJohn Baldwin case CRYPTO_AES_NIST_GCM_16:
585c0341432SJohn Baldwin if ((crp->crp_flags & CRYPTO_F_IV_SEPARATE) == 0)
58608fca7a5SJohn-Mark Gurney return (EINVAL);
587c0341432SJohn Baldwin break;
588c0341432SJohn Baldwin case CRYPTO_AES_CBC:
589c0341432SJohn Baldwin case CRYPTO_AES_XTS:
590c0341432SJohn Baldwin /* CBC & XTS can only handle full blocks for now */
591c0341432SJohn Baldwin if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0)
592c0341432SJohn Baldwin return (EINVAL);
593c0341432SJohn Baldwin break;
594fe182ba1SConrad Meyer }
59527007c65SKonstantin Belousov
596fe182ba1SConrad Meyer /* Do work */
597c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_ETA) {
598c0341432SJohn Baldwin if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {
599c0341432SJohn Baldwin error = aesni_cipher_crypt(ses, crp, csp);
600c0341432SJohn Baldwin if (error == 0)
601c0341432SJohn Baldwin error = aesni_cipher_mac(ses, crp, csp);
602c0341432SJohn Baldwin } else {
603c0341432SJohn Baldwin error = aesni_cipher_mac(ses, crp, csp);
604c0341432SJohn Baldwin if (error == 0)
605c0341432SJohn Baldwin error = aesni_cipher_crypt(ses, crp, csp);
606c0341432SJohn Baldwin }
607c0341432SJohn Baldwin } else if (csp->csp_mode == CSP_MODE_DIGEST)
608c0341432SJohn Baldwin error = aesni_cipher_mac(ses, crp, csp);
609fe182ba1SConrad Meyer else
610c0341432SJohn Baldwin error = aesni_cipher_crypt(ses, crp, csp);
611fe182ba1SConrad Meyer
612fe182ba1SConrad Meyer return (error);
613fe182ba1SConrad Meyer }
614fe182ba1SConrad Meyer
615fe182ba1SConrad Meyer static int
aesni_cipher_crypt(struct aesni_session * ses,struct cryptop * crp,const struct crypto_session_params * csp)616c0341432SJohn Baldwin aesni_cipher_crypt(struct aesni_session *ses, struct cryptop *crp,
617c0341432SJohn Baldwin const struct crypto_session_params *csp)
618fe182ba1SConrad Meyer {
619a639f937SJohn Baldwin uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN];
620a639f937SJohn Baldwin uint8_t *authbuf, *buf, *outbuf;
621c0341432SJohn Baldwin int error;
622a639f937SJohn Baldwin bool encflag, allocated, authallocated, outallocated, outcopy;
623fe182ba1SConrad Meyer
624d718c2d3SJohn Baldwin if (crp->crp_payload_length == 0) {
625d718c2d3SJohn Baldwin buf = NULL;
626d718c2d3SJohn Baldwin allocated = false;
627d718c2d3SJohn Baldwin } else {
628c0341432SJohn Baldwin buf = aesni_cipher_alloc(crp, crp->crp_payload_start,
629c0341432SJohn Baldwin crp->crp_payload_length, &allocated);
630fe182ba1SConrad Meyer if (buf == NULL)
631fe182ba1SConrad Meyer return (ENOMEM);
632d718c2d3SJohn Baldwin }
633fe182ba1SConrad Meyer
634a639f937SJohn Baldwin outallocated = false;
635fe182ba1SConrad Meyer authallocated = false;
636c0341432SJohn Baldwin authbuf = NULL;
637c0341432SJohn Baldwin if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16 ||
638c0341432SJohn Baldwin csp->csp_cipher_alg == CRYPTO_AES_CCM_16) {
639d718c2d3SJohn Baldwin if (crp->crp_aad_length == 0) {
640d718c2d3SJohn Baldwin authbuf = NULL;
641d718c2d3SJohn Baldwin } else if (crp->crp_aad != NULL) {
642604b0217SJohn Baldwin authbuf = crp->crp_aad;
643d718c2d3SJohn Baldwin } else {
644c0341432SJohn Baldwin authbuf = aesni_cipher_alloc(crp, crp->crp_aad_start,
645c0341432SJohn Baldwin crp->crp_aad_length, &authallocated);
646fe182ba1SConrad Meyer if (authbuf == NULL) {
647fe182ba1SConrad Meyer error = ENOMEM;
648fe182ba1SConrad Meyer goto out;
649fe182ba1SConrad Meyer }
650fe182ba1SConrad Meyer }
651d718c2d3SJohn Baldwin }
652fe182ba1SConrad Meyer
653d718c2d3SJohn Baldwin if (CRYPTO_HAS_OUTPUT_BUFFER(crp) && crp->crp_payload_length > 0) {
654a639f937SJohn Baldwin outbuf = crypto_buffer_contiguous_subsegment(&crp->crp_obuf,
655a639f937SJohn Baldwin crp->crp_payload_output_start, crp->crp_payload_length);
656a639f937SJohn Baldwin if (outbuf == NULL) {
657a639f937SJohn Baldwin outcopy = true;
658a639f937SJohn Baldwin if (allocated)
659a639f937SJohn Baldwin outbuf = buf;
660a639f937SJohn Baldwin else {
661a639f937SJohn Baldwin outbuf = malloc(crp->crp_payload_length,
662a639f937SJohn Baldwin M_AESNI, M_NOWAIT);
663a639f937SJohn Baldwin if (outbuf == NULL) {
664a639f937SJohn Baldwin error = ENOMEM;
665a639f937SJohn Baldwin goto out;
666a639f937SJohn Baldwin }
667a639f937SJohn Baldwin outallocated = true;
668a639f937SJohn Baldwin }
669a639f937SJohn Baldwin } else
670a639f937SJohn Baldwin outcopy = false;
671a639f937SJohn Baldwin } else {
672a639f937SJohn Baldwin outbuf = buf;
673a639f937SJohn Baldwin outcopy = allocated;
674a639f937SJohn Baldwin }
675a639f937SJohn Baldwin
6766b635c74SMark Johnston fpu_kern_enter(curthread, NULL, FPU_KERN_NORMAL | FPU_KERN_NOCTX);
6776b635c74SMark Johnston
678fe182ba1SConrad Meyer error = 0;
679c0341432SJohn Baldwin encflag = CRYPTO_OP_IS_ENCRYPT(crp->crp_op);
680c0341432SJohn Baldwin if (crp->crp_cipher_key != NULL)
681c0341432SJohn Baldwin aesni_cipher_setup_common(ses, csp, crp->crp_cipher_key,
682c0341432SJohn Baldwin csp->csp_cipher_klen);
68308fca7a5SJohn-Mark Gurney
68429fe41ddSJohn Baldwin crypto_read_iv(crp, iv);
6855a550ccaSJohn-Mark Gurney
686c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
68708fca7a5SJohn-Mark Gurney case CRYPTO_AES_CBC:
68808fca7a5SJohn-Mark Gurney if (encflag)
68927007c65SKonstantin Belousov aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
690a639f937SJohn Baldwin crp->crp_payload_length, buf, outbuf, iv);
691a639f937SJohn Baldwin else {
692a639f937SJohn Baldwin if (buf != outbuf)
693a639f937SJohn Baldwin memcpy(outbuf, buf, crp->crp_payload_length);
69408fca7a5SJohn-Mark Gurney aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
695a639f937SJohn Baldwin crp->crp_payload_length, outbuf, iv);
696a639f937SJohn Baldwin }
69708fca7a5SJohn-Mark Gurney break;
69808fca7a5SJohn-Mark Gurney case CRYPTO_AES_ICM:
69908fca7a5SJohn-Mark Gurney /* encryption & decryption are the same */
70008fca7a5SJohn-Mark Gurney aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
701a639f937SJohn Baldwin crp->crp_payload_length, buf, outbuf, iv);
70208fca7a5SJohn-Mark Gurney break;
70308fca7a5SJohn-Mark Gurney case CRYPTO_AES_XTS:
70408fca7a5SJohn-Mark Gurney if (encflag)
70527007c65SKonstantin Belousov aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
706c0341432SJohn Baldwin ses->xts_schedule, crp->crp_payload_length, buf,
707a639f937SJohn Baldwin outbuf, iv);
70827007c65SKonstantin Belousov else
70927007c65SKonstantin Belousov aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
710c0341432SJohn Baldwin ses->xts_schedule, crp->crp_payload_length, buf,
711a639f937SJohn Baldwin outbuf, iv);
71208fca7a5SJohn-Mark Gurney break;
71308fca7a5SJohn-Mark Gurney case CRYPTO_AES_NIST_GCM_16:
714fe182ba1SConrad Meyer if (encflag) {
715c0341432SJohn Baldwin memset(tag, 0, sizeof(tag));
716a639f937SJohn Baldwin AES_GCM_encrypt(buf, outbuf, authbuf, iv, tag,
717c0341432SJohn Baldwin crp->crp_payload_length, crp->crp_aad_length,
718c0341432SJohn Baldwin csp->csp_ivlen, ses->enc_schedule, ses->rounds);
719c0341432SJohn Baldwin crypto_copyback(crp, crp->crp_digest_start, sizeof(tag),
720c0341432SJohn Baldwin tag);
721fe182ba1SConrad Meyer } else {
722c0341432SJohn Baldwin crypto_copydata(crp, crp->crp_digest_start, sizeof(tag),
723c0341432SJohn Baldwin tag);
724a639f937SJohn Baldwin if (!AES_GCM_decrypt(buf, outbuf, authbuf, iv, tag,
725c0341432SJohn Baldwin crp->crp_payload_length, crp->crp_aad_length,
726c0341432SJohn Baldwin csp->csp_ivlen, ses->enc_schedule, ses->rounds))
72708fca7a5SJohn-Mark Gurney error = EBADMSG;
72827007c65SKonstantin Belousov }
72908fca7a5SJohn-Mark Gurney break;
7307cff9f37SSean Eric Fagan case CRYPTO_AES_CCM_16:
7317cff9f37SSean Eric Fagan if (encflag) {
732c0341432SJohn Baldwin memset(tag, 0, sizeof(tag));
733a639f937SJohn Baldwin AES_CCM_encrypt(buf, outbuf, authbuf, iv, tag,
734c0341432SJohn Baldwin crp->crp_payload_length, crp->crp_aad_length,
735655eb762SJohn Baldwin csp->csp_ivlen, ses->mlen, ses->enc_schedule,
736655eb762SJohn Baldwin ses->rounds);
737655eb762SJohn Baldwin crypto_copyback(crp, crp->crp_digest_start, ses->mlen,
738c0341432SJohn Baldwin tag);
7397cff9f37SSean Eric Fagan } else {
740655eb762SJohn Baldwin crypto_copydata(crp, crp->crp_digest_start, ses->mlen,
741c0341432SJohn Baldwin tag);
742a639f937SJohn Baldwin if (!AES_CCM_decrypt(buf, outbuf, authbuf, iv, tag,
743c0341432SJohn Baldwin crp->crp_payload_length, crp->crp_aad_length,
744655eb762SJohn Baldwin csp->csp_ivlen, ses->mlen, ses->enc_schedule,
745655eb762SJohn Baldwin ses->rounds))
7467cff9f37SSean Eric Fagan error = EBADMSG;
74727007c65SKonstantin Belousov }
7487cff9f37SSean Eric Fagan break;
7497cff9f37SSean Eric Fagan }
7506b635c74SMark Johnston
7516b635c74SMark Johnston fpu_kern_leave(curthread, NULL);
7526b635c74SMark Johnston
753a639f937SJohn Baldwin if (outcopy && error == 0)
754a639f937SJohn Baldwin crypto_copyback(crp, CRYPTO_HAS_OUTPUT_BUFFER(crp) ?
755a639f937SJohn Baldwin crp->crp_payload_output_start : crp->crp_payload_start,
756a639f937SJohn Baldwin crp->crp_payload_length, outbuf);
757e50f10b5SConrad Meyer
75827007c65SKonstantin Belousov out:
7594a711b8dSJohn Baldwin if (allocated)
7604a711b8dSJohn Baldwin zfree(buf, M_AESNI);
7614a711b8dSJohn Baldwin if (authallocated)
7624a711b8dSJohn Baldwin zfree(authbuf, M_AESNI);
7634a711b8dSJohn Baldwin if (outallocated)
7644a711b8dSJohn Baldwin zfree(outbuf, M_AESNI);
76566f2e4b6SJohn Baldwin explicit_bzero(iv, sizeof(iv));
76666f2e4b6SJohn Baldwin explicit_bzero(tag, sizeof(tag));
76727007c65SKonstantin Belousov return (error);
76827007c65SKonstantin Belousov }
769fe182ba1SConrad Meyer
770fe182ba1SConrad Meyer static int
aesni_cipher_mac(struct aesni_session * ses,struct cryptop * crp,const struct crypto_session_params * csp)771c0341432SJohn Baldwin aesni_cipher_mac(struct aesni_session *ses, struct cryptop *crp,
772c0341432SJohn Baldwin const struct crypto_session_params *csp)
773fe182ba1SConrad Meyer {
774fe182ba1SConrad Meyer union {
775fe182ba1SConrad Meyer struct SHA256Context sha2 __aligned(16);
776fe182ba1SConrad Meyer struct sha1_ctxt sha1 __aligned(16);
777fe182ba1SConrad Meyer } sctx;
778fe182ba1SConrad Meyer uint32_t res[SHA2_256_HASH_LEN / sizeof(uint32_t)];
779c0341432SJohn Baldwin const uint8_t *key;
780c0341432SJohn Baldwin int i, keylen;
781e2982a3eSConrad Meyer
782c0341432SJohn Baldwin if (crp->crp_auth_key != NULL)
783c0341432SJohn Baldwin key = crp->crp_auth_key;
784c0341432SJohn Baldwin else
785c0341432SJohn Baldwin key = csp->csp_auth_key;
786c0341432SJohn Baldwin keylen = csp->csp_auth_klen;
787fe182ba1SConrad Meyer
7886b635c74SMark Johnston fpu_kern_enter(curthread, NULL, FPU_KERN_NORMAL | FPU_KERN_NOCTX);
7896b635c74SMark Johnston
790c0341432SJohn Baldwin if (ses->hmac) {
79166f2e4b6SJohn Baldwin uint8_t hmac_key[SHA1_BLOCK_LEN] __aligned(16);
79266f2e4b6SJohn Baldwin
793e2982a3eSConrad Meyer /* Inner hash: (K ^ IPAD) || data */
794c0341432SJohn Baldwin ses->hash_init(&sctx);
795c0341432SJohn Baldwin for (i = 0; i < keylen; i++)
796c0341432SJohn Baldwin hmac_key[i] = key[i] ^ HMAC_IPAD_VAL;
797c0341432SJohn Baldwin for (i = keylen; i < sizeof(hmac_key); i++)
798c0341432SJohn Baldwin hmac_key[i] = 0 ^ HMAC_IPAD_VAL;
799c0341432SJohn Baldwin ses->hash_update(&sctx, hmac_key, sizeof(hmac_key));
800c0341432SJohn Baldwin
801604b0217SJohn Baldwin if (crp->crp_aad != NULL)
802604b0217SJohn Baldwin ses->hash_update(&sctx, crp->crp_aad,
803604b0217SJohn Baldwin crp->crp_aad_length);
804604b0217SJohn Baldwin else
805604b0217SJohn Baldwin crypto_apply(crp, crp->crp_aad_start,
806604b0217SJohn Baldwin crp->crp_aad_length, ses->hash_update, &sctx);
807a639f937SJohn Baldwin if (CRYPTO_HAS_OUTPUT_BUFFER(crp) &&
808a639f937SJohn Baldwin CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
809a639f937SJohn Baldwin crypto_apply_buf(&crp->crp_obuf,
810a639f937SJohn Baldwin crp->crp_payload_output_start,
811a639f937SJohn Baldwin crp->crp_payload_length,
812ccbaa67dSMarcin Wojtas ses->hash_update, &sctx);
813a639f937SJohn Baldwin else
814c0341432SJohn Baldwin crypto_apply(crp, crp->crp_payload_start,
815ccbaa67dSMarcin Wojtas crp->crp_payload_length, ses->hash_update, &sctx);
816efac54cbSMarcin Wojtas
817efac54cbSMarcin Wojtas if (csp->csp_flags & CSP_F_ESN)
818efac54cbSMarcin Wojtas ses->hash_update(&sctx, crp->crp_esn, 4);
819efac54cbSMarcin Wojtas
820c0341432SJohn Baldwin ses->hash_finalize(res, &sctx);
821c0341432SJohn Baldwin
822e2982a3eSConrad Meyer /* Outer hash: (K ^ OPAD) || inner hash */
823c0341432SJohn Baldwin ses->hash_init(&sctx);
824c0341432SJohn Baldwin for (i = 0; i < keylen; i++)
825c0341432SJohn Baldwin hmac_key[i] = key[i] ^ HMAC_OPAD_VAL;
826c0341432SJohn Baldwin for (i = keylen; i < sizeof(hmac_key); i++)
827c0341432SJohn Baldwin hmac_key[i] = 0 ^ HMAC_OPAD_VAL;
828c0341432SJohn Baldwin ses->hash_update(&sctx, hmac_key, sizeof(hmac_key));
829c0341432SJohn Baldwin ses->hash_update(&sctx, res, ses->hash_len);
830c0341432SJohn Baldwin ses->hash_finalize(res, &sctx);
83166f2e4b6SJohn Baldwin explicit_bzero(hmac_key, sizeof(hmac_key));
832e2982a3eSConrad Meyer } else {
833c0341432SJohn Baldwin ses->hash_init(&sctx);
834c0341432SJohn Baldwin
835604b0217SJohn Baldwin if (crp->crp_aad != NULL)
836604b0217SJohn Baldwin ses->hash_update(&sctx, crp->crp_aad,
837604b0217SJohn Baldwin crp->crp_aad_length);
838604b0217SJohn Baldwin else
839604b0217SJohn Baldwin crypto_apply(crp, crp->crp_aad_start,
840604b0217SJohn Baldwin crp->crp_aad_length, ses->hash_update, &sctx);
841a639f937SJohn Baldwin if (CRYPTO_HAS_OUTPUT_BUFFER(crp) &&
842a639f937SJohn Baldwin CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
843a639f937SJohn Baldwin crypto_apply_buf(&crp->crp_obuf,
844a639f937SJohn Baldwin crp->crp_payload_output_start,
845a639f937SJohn Baldwin crp->crp_payload_length,
846ccbaa67dSMarcin Wojtas ses->hash_update, &sctx);
847a639f937SJohn Baldwin else
848c0341432SJohn Baldwin crypto_apply(crp, crp->crp_payload_start,
849c0341432SJohn Baldwin crp->crp_payload_length,
850ccbaa67dSMarcin Wojtas ses->hash_update, &sctx);
851c0341432SJohn Baldwin
852c0341432SJohn Baldwin ses->hash_finalize(res, &sctx);
853e2982a3eSConrad Meyer }
854e2982a3eSConrad Meyer
8556b635c74SMark Johnston fpu_kern_leave(curthread, NULL);
8566b635c74SMark Johnston
857c0341432SJohn Baldwin if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
85866f2e4b6SJohn Baldwin uint32_t res2[SHA2_256_HASH_LEN / sizeof(uint32_t)];
85966f2e4b6SJohn Baldwin
860c0341432SJohn Baldwin crypto_copydata(crp, crp->crp_digest_start, ses->mlen, res2);
861c0341432SJohn Baldwin if (timingsafe_bcmp(res, res2, ses->mlen) != 0)
862c0341432SJohn Baldwin return (EBADMSG);
86366f2e4b6SJohn Baldwin explicit_bzero(res2, sizeof(res2));
864c0341432SJohn Baldwin } else
865c0341432SJohn Baldwin crypto_copyback(crp, crp->crp_digest_start, ses->mlen, res);
86666f2e4b6SJohn Baldwin explicit_bzero(res, sizeof(res));
867fe182ba1SConrad Meyer return (0);
868fe182ba1SConrad Meyer }
869