2 * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
4 * Copyright (c) 2014 The FreeBSD Foundation
5 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
8 * Portions of this software were developed by John-Mark Gurney
9 * under sponsorship of the FreeBSD Foundation and
10 * Rubicon Communications, LLC (Netgate).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/libkern.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
50 #include <crypto/aesni/aesni.h>
51 #include <crypto/aesni/sha_sse.h>
52 #include <crypto/sha1.h>
53 #include <crypto/sha2/sha224.h>
54 #include <crypto/sha2/sha256.h>
56 #include <opencrypto/cryptodev.h>
57 #include <opencrypto/gmac.h>
58 #include <cryptodev_if.h>
60 #include <machine/md_var.h>
61 #include <machine/specialreg.h>
63 #include <machine/npx.h>
64 #elif defined(__amd64__)
65 #include <machine/fpu.h>
68 static struct mtx_padalign *ctx_mtx;
69 static struct fpu_kern_ctx **ctx_fpu;
77 #define ACQUIRE_CTX(i, ctx) \
79 (i) = PCPU_GET(cpuid); \
80 mtx_lock(&ctx_mtx[(i)]); \
81 (ctx) = ctx_fpu[(i)]; \
83 #define RELEASE_CTX(i, ctx) \
85 mtx_unlock(&ctx_mtx[(i)]); \
90 static int aesni_newsession(device_t, crypto_session_t cses,
91 struct cryptoini *cri);
92 static int aesni_cipher_setup(struct aesni_session *ses,
93 struct cryptoini *encini, struct cryptoini *authini);
94 static int aesni_cipher_process(struct aesni_session *ses,
95 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
96 static int aesni_cipher_crypt(struct aesni_session *ses,
97 struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);
98 static int aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
101 MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");
104 aesni_identify(driver_t *drv, device_t parent)
107 /* NB: order 10 is so we get attached after h/w devices */
108 if (device_find_child(parent, "aesni", -1) == NULL &&
109 BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
110 panic("aesni: could not attach");
114 detect_cpu_features(bool *has_aes, bool *has_sha)
117 *has_aes = ((cpu_feature2 & CPUID2_AESNI) != 0 &&
118 (cpu_feature2 & CPUID2_SSE41) != 0);
119 *has_sha = ((cpu_stdext_feature & CPUID_STDEXT_SHA) != 0 &&
120 (cpu_feature2 & CPUID2_SSSE3) != 0);
124 aesni_probe(device_t dev)
126 bool has_aes, has_sha;
128 detect_cpu_features(&has_aes, &has_sha);
129 if (!has_aes && !has_sha) {
130 device_printf(dev, "No AES or SHA support.\n");
132 } else if (has_aes && has_sha)
134 "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS,SHA1,SHA256");
137 "AES-CBC,AES-CCM,AES-GCM,AES-ICM,AES-XTS");
139 device_set_desc(dev, "SHA1,SHA256");
149 /* XXX - no way to return driverid */
151 if (ctx_fpu[i] != NULL) {
152 mtx_destroy(&ctx_mtx[i]);
153 fpu_kern_free_ctx(ctx_fpu[i]);
157 free(ctx_mtx, M_AESNI);
159 free(ctx_fpu, M_AESNI);
164 aesni_attach(device_t dev)
166 struct aesni_softc *sc;
169 sc = device_get_softc(dev);
171 sc->cid = crypto_get_driverid(dev, sizeof(struct aesni_session),
172 CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SYNC);
174 device_printf(dev, "Could not get crypto driver id.\n");
178 ctx_mtx = malloc(sizeof *ctx_mtx * (mp_maxid + 1), M_AESNI,
180 ctx_fpu = malloc(sizeof *ctx_fpu * (mp_maxid + 1), M_AESNI,
184 ctx_fpu[i] = fpu_kern_alloc_ctx(0);
185 mtx_init(&ctx_mtx[i], "anifpumtx", NULL, MTX_DEF|MTX_NEW);
188 detect_cpu_features(&sc->has_aes, &sc->has_sha);
190 crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
191 crypto_register(sc->cid, CRYPTO_AES_ICM, 0, 0);
192 crypto_register(sc->cid, CRYPTO_AES_NIST_GCM_16, 0, 0);
193 crypto_register(sc->cid, CRYPTO_AES_128_NIST_GMAC, 0, 0);
194 crypto_register(sc->cid, CRYPTO_AES_192_NIST_GMAC, 0, 0);
195 crypto_register(sc->cid, CRYPTO_AES_256_NIST_GMAC, 0, 0);
196 crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
197 crypto_register(sc->cid, CRYPTO_AES_CCM_16, 0, 0);
198 crypto_register(sc->cid, CRYPTO_AES_CCM_CBC_MAC, 0, 0);
201 crypto_register(sc->cid, CRYPTO_SHA1, 0, 0);
202 crypto_register(sc->cid, CRYPTO_SHA1_HMAC, 0, 0);
203 crypto_register(sc->cid, CRYPTO_SHA2_224, 0, 0);
204 crypto_register(sc->cid, CRYPTO_SHA2_224_HMAC, 0, 0);
205 crypto_register(sc->cid, CRYPTO_SHA2_256, 0, 0);
206 crypto_register(sc->cid, CRYPTO_SHA2_256_HMAC, 0, 0);
212 aesni_detach(device_t dev)
214 struct aesni_softc *sc;
216 sc = device_get_softc(dev);
218 crypto_unregister_all(sc->cid);
226 aesni_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri)
228 struct aesni_softc *sc;
229 struct aesni_session *ses;
230 struct cryptoini *encini, *authini;
235 KASSERT(cses != NULL, ("EDOOFUS"));
241 sc = device_get_softc(dev);
243 ses = crypto_get_driver_session(cses);
249 ccm = cbc_hash = false;
251 for (; cri != NULL; cri = cri->cri_next) {
252 switch (cri->cri_alg) {
253 case CRYPTO_AES_NIST_GCM_16:
254 case CRYPTO_AES_CCM_16:
255 if (cri->cri_alg == CRYPTO_AES_NIST_GCM_16) {
257 } else if (cri->cri_alg == CRYPTO_AES_CCM_16) {
266 if (encini != NULL) {
267 CRYPTDEB("encini already set");
272 case CRYPTO_AES_CCM_CBC_MAC:
276 case CRYPTO_AES_128_NIST_GMAC:
277 case CRYPTO_AES_192_NIST_GMAC:
278 case CRYPTO_AES_256_NIST_GMAC:
280 * nothing to do here, maybe in the future cache some
283 if (authini != NULL) {
284 CRYPTDEB("authini already set");
291 case CRYPTO_SHA1_HMAC:
292 case CRYPTO_SHA2_224:
293 case CRYPTO_SHA2_224_HMAC:
294 case CRYPTO_SHA2_256:
295 case CRYPTO_SHA2_256_HMAC:
298 if (authini != NULL) {
299 CRYPTDEB("authini already set");
306 CRYPTDEB("unhandled algorithm");
310 if (encini == NULL && authini == NULL) {
311 CRYPTDEB("no cipher");
315 * GMAC algorithms are only supported with simultaneous GCM. Likewise
316 * GCM is not supported without GMAC.
318 if (gcm_hash != gcm) {
319 CRYPTDEB("gcm_hash != gcm");
323 if (cbc_hash != ccm) {
324 CRYPTDEB("cbc_hash != ccm");
329 ses->algo = encini->cri_alg;
331 ses->auth_algo = authini->cri_alg;
333 error = aesni_cipher_setup(ses, encini, authini);
335 CRYPTDEB("setup failed");
343 aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
345 struct aesni_session *ses;
346 struct cryptodesc *crd, *enccrd, *authcrd;
359 if (crp->crp_callback == NULL || crp->crp_desc == NULL ||
360 crp->crp_session == NULL) {
365 for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
366 switch (crd->crd_alg) {
367 case CRYPTO_AES_NIST_GCM_16:
368 case CRYPTO_AES_CCM_16:
374 if (enccrd != NULL) {
381 case CRYPTO_AES_128_NIST_GMAC:
382 case CRYPTO_AES_192_NIST_GMAC:
383 case CRYPTO_AES_256_NIST_GMAC:
384 case CRYPTO_AES_CCM_CBC_MAC:
386 case CRYPTO_SHA1_HMAC:
387 case CRYPTO_SHA2_224:
388 case CRYPTO_SHA2_224_HMAC:
389 case CRYPTO_SHA2_256:
390 case CRYPTO_SHA2_256_HMAC:
391 if (authcrd != NULL) {
404 if ((enccrd == NULL && authcrd == NULL) ||
405 (needauth && authcrd == NULL)) {
410 /* CBC & XTS can only handle full blocks for now */
411 if (enccrd != NULL && (enccrd->crd_alg == CRYPTO_AES_CBC ||
412 enccrd->crd_alg == CRYPTO_AES_XTS) &&
413 (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
418 ses = crypto_get_driver_session(crp->crp_session);
419 KASSERT(ses != NULL, ("EDOOFUS"));
421 error = aesni_cipher_process(ses, enccrd, authcrd, crp);
426 crp->crp_etype = error;
432 aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
437 addr = crypto_contiguous_subsegment(crp->crp_flags,
438 crp->crp_buf, enccrd->crd_skip, enccrd->crd_len);
443 addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
446 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
447 enccrd->crd_len, addr);
453 static device_method_t aesni_methods[] = {
454 DEVMETHOD(device_identify, aesni_identify),
455 DEVMETHOD(device_probe, aesni_probe),
456 DEVMETHOD(device_attach, aesni_attach),
457 DEVMETHOD(device_detach, aesni_detach),
459 DEVMETHOD(cryptodev_newsession, aesni_newsession),
460 DEVMETHOD(cryptodev_process, aesni_process),
465 static driver_t aesni_driver = {
468 sizeof(struct aesni_softc),
470 static devclass_t aesni_devclass;
472 DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
473 MODULE_VERSION(aesni, 1);
474 MODULE_DEPEND(aesni, crypto, 1, 1, 1);
477 aesni_authprepare(struct aesni_session *ses, int klen, const void *cri_key)
484 if (keylen > sizeof(ses->hmac_key))
486 if (ses->auth_algo == CRYPTO_SHA1 && keylen > 0)
488 memcpy(ses->hmac_key, cri_key, keylen);
493 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini,
494 struct cryptoini *authini)
496 struct fpu_kern_ctx *ctx;
497 int kt, ctxidx, error;
499 switch (ses->auth_algo) {
501 case CRYPTO_SHA1_HMAC:
502 case CRYPTO_SHA2_224:
503 case CRYPTO_SHA2_224_HMAC:
504 case CRYPTO_SHA2_256:
505 case CRYPTO_SHA2_256_HMAC:
506 error = aesni_authprepare(ses, authini->cri_klen,
510 ses->mlen = authini->cri_mlen;
513 kt = is_fpu_kern_thread(0) || (encini == NULL);
515 ACQUIRE_CTX(ctxidx, ctx);
516 fpu_kern_enter(curthread, ctx,
517 FPU_KERN_NORMAL | FPU_KERN_KTHR);
522 error = aesni_cipher_setup_common(ses, encini->cri_key,
526 fpu_kern_leave(curthread, ctx);
527 RELEASE_CTX(ctxidx, ctx);
533 intel_sha1_update(void *vctx, const void *vdata, u_int datalen)
535 struct sha1_ctxt *ctx = vctx;
536 const char *data = vdata;
544 /* Do any aligned blocks without redundant copying. */
545 if (datalen >= 64 && ctx->count % 64 == 0) {
546 blocks = datalen / 64;
547 ctx->c.b64[0] += blocks * 64 * 8;
548 intel_sha1_step(ctx->h.b32, data + off, blocks);
552 while (off < datalen) {
553 gapstart = ctx->count % 64;
554 gaplen = 64 - gapstart;
556 copysiz = (gaplen < datalen - off) ? gaplen : datalen - off;
557 bcopy(&data[off], &ctx->m.b8[gapstart], copysiz);
558 ctx->count += copysiz;
560 ctx->c.b64[0] += copysiz * 8;
561 if (ctx->count % 64 == 0)
562 intel_sha1_step(ctx->h.b32, (void *)ctx->m.b8, 1);
569 SHA1_Init_fn(void *ctx)
575 SHA1_Finalize_fn(void *digest, void *ctx)
577 sha1_result(ctx, digest);
581 intel_sha256_update(void *vctx, const void *vdata, u_int len)
583 SHA256_CTX *ctx = vctx;
587 const unsigned char *src = vdata;
589 /* Number of bytes left in the buffer from previous updates */
590 r = (ctx->count >> 3) & 0x3f;
592 /* Convert the length into a number of bits */
595 /* Update number of bits */
596 ctx->count += bitlen;
598 /* Handle the case where we don't need to perform any transforms */
600 memcpy(&ctx->buf[r], src, len);
604 /* Finish the current block */
605 memcpy(&ctx->buf[r], src, 64 - r);
606 intel_sha256_step(ctx->state, ctx->buf, 1);
610 /* Perform complete blocks */
613 intel_sha256_step(ctx->state, src, blocks);
618 /* Copy left over data into buffer */
619 memcpy(ctx->buf, src, len);
624 SHA224_Init_fn(void *ctx)
630 SHA224_Finalize_fn(void *digest, void *ctx)
632 SHA224_Final(digest, ctx);
636 SHA256_Init_fn(void *ctx)
642 SHA256_Finalize_fn(void *digest, void *ctx)
644 SHA256_Final(digest, ctx);
648 * Compute the HASH( (key ^ xorbyte) || buf )
651 hmac_internal(void *ctx, uint32_t *res,
652 int (*update)(void *, const void *, u_int),
653 void (*finalize)(void *, void *), uint8_t *key, uint8_t xorbyte,
654 const void *buf, size_t off, size_t buflen, int crpflags)
658 for (i = 0; i < 64; i++)
660 update(ctx, key, 64);
661 for (i = 0; i < 64; i++)
664 crypto_apply(crpflags, __DECONST(void *, buf), off, buflen,
665 __DECONST(int (*)(void *, void *, u_int), update), ctx);
670 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
671 struct cryptodesc *authcrd, struct cryptop *crp)
673 struct fpu_kern_ctx *ctx;
677 if (enccrd != NULL) {
678 if ((enccrd->crd_alg == CRYPTO_AES_ICM ||
679 enccrd->crd_alg == CRYPTO_AES_CCM_16 ||
680 enccrd->crd_alg == CRYPTO_AES_NIST_GCM_16) &&
681 (enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0)
688 kt = is_fpu_kern_thread(0);
690 ACQUIRE_CTX(ctxidx, ctx);
691 fpu_kern_enter(curthread, ctx,
692 FPU_KERN_NORMAL | FPU_KERN_KTHR);
696 if (enccrd != NULL && authcrd != NULL) {
697 /* Perform the first operation */
698 if (crp->crp_desc == enccrd)
699 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
701 error = aesni_cipher_mac(ses, authcrd, crp);
704 /* Perform the second operation */
705 if (crp->crp_desc == enccrd)
706 error = aesni_cipher_mac(ses, authcrd, crp);
708 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
709 } else if (enccrd != NULL)
710 error = aesni_cipher_crypt(ses, enccrd, authcrd, crp);
712 error = aesni_cipher_mac(ses, authcrd, crp);
719 fpu_kern_leave(curthread, ctx);
720 RELEASE_CTX(ctxidx, ctx);
726 aesni_cipher_crypt(struct aesni_session *ses, struct cryptodesc *enccrd,
727 struct cryptodesc *authcrd, struct cryptop *crp)
729 uint8_t iv[AES_BLOCK_LEN], tag[GMAC_DIGEST_LEN], *buf, *authbuf;
731 bool encflag, allocated, authallocated;
733 KASSERT((ses->algo != CRYPTO_AES_NIST_GCM_16 &&
734 ses->algo != CRYPTO_AES_CCM_16) || authcrd != NULL,
735 ("AES_NIST_GCM_16/AES_CCM_16 must include MAC descriptor"));
740 buf = aesni_cipher_alloc(enccrd, crp, &allocated);
744 authallocated = false;
745 if (ses->algo == CRYPTO_AES_NIST_GCM_16 ||
746 ses->algo == CRYPTO_AES_CCM_16) {
747 authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
748 if (authbuf == NULL) {
755 encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;
756 if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
757 error = aesni_cipher_setup_common(ses, enccrd->crd_key,
763 switch (enccrd->crd_alg) {
766 ivlen = AES_BLOCK_LEN;
771 case CRYPTO_AES_NIST_GCM_16:
772 case CRYPTO_AES_CCM_16:
773 ivlen = 12; /* should support arbitarily larger */
779 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
780 bcopy(enccrd->crd_iv, iv, ivlen);
782 arc4rand(iv, ivlen, 0);
784 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
785 crypto_copyback(crp->crp_flags, crp->crp_buf,
786 enccrd->crd_inject, ivlen, iv);
788 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
789 bcopy(enccrd->crd_iv, iv, ivlen);
791 crypto_copydata(crp->crp_flags, crp->crp_buf,
792 enccrd->crd_inject, ivlen, iv);
798 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
799 enccrd->crd_len, buf, buf, iv);
801 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
802 enccrd->crd_len, buf, iv);
805 /* encryption & decryption are the same */
806 aesni_encrypt_icm(ses->rounds, ses->enc_schedule,
807 enccrd->crd_len, buf, buf, iv);
811 aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
812 ses->xts_schedule, enccrd->crd_len, buf, buf,
815 aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
816 ses->xts_schedule, enccrd->crd_len, buf, buf,
819 case CRYPTO_AES_NIST_GCM_16:
821 crypto_copydata(crp->crp_flags, crp->crp_buf,
822 authcrd->crd_inject, sizeof(tag), tag);
824 bzero(tag, sizeof tag);
827 AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
828 enccrd->crd_len, authcrd->crd_len, ivlen,
829 ses->enc_schedule, ses->rounds);
832 crypto_copyback(crp->crp_flags, crp->crp_buf,
833 authcrd->crd_inject, sizeof(tag), tag);
835 if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
836 enccrd->crd_len, authcrd->crd_len, ivlen,
837 ses->enc_schedule, ses->rounds))
841 case CRYPTO_AES_CCM_16:
843 crypto_copydata(crp->crp_flags, crp->crp_buf,
844 authcrd->crd_inject, sizeof(tag), tag);
846 bzero(tag, sizeof tag);
848 AES_CCM_encrypt(buf, buf, authbuf, iv, tag,
849 enccrd->crd_len, authcrd->crd_len, ivlen,
850 ses->enc_schedule, ses->rounds);
852 crypto_copyback(crp->crp_flags, crp->crp_buf,
853 authcrd->crd_inject, sizeof(tag), tag);
855 if (!AES_CCM_decrypt(buf, buf, authbuf, iv, tag,
856 enccrd->crd_len, authcrd->crd_len, ivlen,
857 ses->enc_schedule, ses->rounds))
862 if (allocated && error == 0)
863 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
864 enccrd->crd_len, buf);
868 explicit_bzero(buf, enccrd->crd_len);
872 explicit_bzero(authbuf, authcrd->crd_len);
873 free(authbuf, M_AESNI);
879 aesni_cipher_mac(struct aesni_session *ses, struct cryptodesc *crd,
883 struct SHA256Context sha2 __aligned(16);
884 struct sha1_ctxt sha1 __aligned(16);
886 uint32_t res[SHA2_256_HASH_LEN / sizeof(uint32_t)];
889 void (*InitFn)(void *);
890 int (*UpdateFn)(void *, const void *, unsigned);
891 void (*FinalizeFn)(void *, void *);
895 if ((crd->crd_flags & ~CRD_F_KEY_EXPLICIT) != 0) {
896 CRYPTDEB("%s: Unsupported MAC flags: 0x%x", __func__,
897 (crd->crd_flags & ~CRD_F_KEY_EXPLICIT));
900 if ((crd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
901 error = aesni_authprepare(ses, crd->crd_klen, crd->crd_key);
907 switch (ses->auth_algo) {
908 case CRYPTO_SHA1_HMAC:
912 hashlen = SHA1_HASH_LEN;
913 InitFn = SHA1_Init_fn;
914 UpdateFn = intel_sha1_update;
915 FinalizeFn = SHA1_Finalize_fn;
919 case CRYPTO_SHA2_256_HMAC:
922 case CRYPTO_SHA2_256:
923 hashlen = SHA2_256_HASH_LEN;
924 InitFn = SHA256_Init_fn;
925 UpdateFn = intel_sha256_update;
926 FinalizeFn = SHA256_Finalize_fn;
930 case CRYPTO_SHA2_224_HMAC:
933 case CRYPTO_SHA2_224:
934 hashlen = SHA2_224_HASH_LEN;
935 InitFn = SHA224_Init_fn;
936 UpdateFn = intel_sha256_update;
937 FinalizeFn = SHA224_Finalize_fn;
942 * AES-GMAC authentication is verified while processing the
949 /* Inner hash: (K ^ IPAD) || data */
951 hmac_internal(ctx, res, UpdateFn, FinalizeFn, ses->hmac_key,
952 0x36, crp->crp_buf, crd->crd_skip, crd->crd_len,
954 /* Outer hash: (K ^ OPAD) || inner hash */
956 hmac_internal(ctx, res, UpdateFn, FinalizeFn, ses->hmac_key,
957 0x5C, res, 0, hashlen, 0);
960 crypto_apply(crp->crp_flags, crp->crp_buf, crd->crd_skip,
961 crd->crd_len, __DECONST(int (*)(void *, void *, u_int),
963 FinalizeFn(res, ctx);
966 if (ses->mlen != 0 && ses->mlen < hashlen)
969 crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject, hashlen,