summaryrefslogtreecommitdiffstats
path: root/sys/crypto/aesni/aesni.c
blob: fc6bbf4c167039817ba2b002cad5a33d1eab65f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*-
 * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@FreeBSD.org>
 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/libkern.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rwlock.h>
#include <sys/bus.h>
#include <sys/uio.h>
#include <sys/mbuf.h>
#include <crypto/aesni/aesni.h>
#include <cryptodev_if.h>
#include <opencrypto/gmac.h>

struct aesni_softc {
	int32_t cid;
	volatile uint32_t nsessions;
	struct aesni_session *sessions;
};

static int aesni_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
static int aesni_freesession(device_t, uint64_t tid);
static void aesni_freesession_locked(struct aesni_softc *sc,
    struct aesni_session *ses);
static int aesni_cipher_setup(struct aesni_session *ses,
    struct cryptoini *encini);
static int aesni_cipher_process(struct aesni_session *ses,
    struct cryptodesc *enccrd, struct cryptodesc *authcrd, struct cryptop *crp);

MALLOC_DEFINE(M_AESNI, "aesni_data", "AESNI Data");

static void
aesni_identify(driver_t *drv, device_t parent)
{

	/* NB: order 10 is so we get attached after h/w devices */
	if (device_find_child(parent, "aesni", -1) == NULL &&
	    BUS_ADD_CHILD(parent, 10, "aesni", -1) == 0)
		panic("aesni: could not attach");
}

static int
aesni_probe(device_t dev)
{

	if ((cpu_feature2 & CPUID2_AESNI) == 0) {
		device_printf(dev, "No AESNI support.\n");
		return (EINVAL);
	}

	if ((cpu_feature & CPUID2_SSE41) == 0 && (cpu_feature2 & CPUID2_SSE41) == 0) {
		device_printf(dev, "No SSE4.1 support.\n");
		return (EINVAL);
	}

	device_set_desc_copy(dev, "AES-CBC,AES-XTS,AES-GCM");
	return (0);
}

static int
aesni_attach(device_t dev)
{
	struct aesni_softc *sc;

	sc = device_get_softc(dev);
	sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE |
	    CRYPTOCAP_F_SYNC);
	if (sc->cid < 0) {
		device_printf(dev, "Could not get crypto driver id.\n");
		return (ENOMEM);
	}

	sc->nsessions = 32;
	sc->sessions = malloc(sc->nsessions * sizeof(struct aesni_session),
			M_AESNI, M_WAITOK | M_ZERO);

	crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
	crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
	crypto_register(sc->cid, CRYPTO_AES_RFC4106_GCM_16, 0, 0);
	crypto_register(sc->cid, CRYPTO_AES_128_GMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_AES_192_GMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_AES_256_GMAC, 0, 0);

	crypto_register(sc->cid, CRYPTO_NULL_HMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_MD5_HMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_SHA1_HMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_SHA2_256_HMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_SHA2_384_HMAC, 0, 0);
	crypto_register(sc->cid, CRYPTO_SHA2_512_HMAC, 0, 0);

	return (0);
}

static int
aesni_detach(device_t dev)
{
	struct aesni_softc *sc;
	struct aesni_session *ses;
	int i;

	sc = device_get_softc(dev);
	for (i = 0; i < sc->nsessions; i++) {
		ses = &sc->sessions[i];
		if (ses->used) {
			device_printf(dev,
			    "Cannot detach, sessions still active.\n");
			return (EBUSY);
		}
	}
	crypto_unregister_all(sc->cid);
	for (i = 0; i < sc->nsessions; i++) {
		ses = &sc->sessions[i];
		if (ses->fpu_ctx != NULL)
			fpu_kern_free_ctx(ses->fpu_ctx);
	}
	free(sc->sessions, M_AESNI);
	return (0);
}

static int
aesni_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
{
	struct aesni_softc *sc;
	struct aesni_session *ses;
	struct cryptoini *encini, *authini;
	int error, sessn;

	if (sidp == NULL || cri == NULL) {
		printf("no sidp or cri");
		return (EINVAL);
	}

	sc = device_get_softc(dev);
	ses = NULL;
	encini = NULL;
	authini = NULL;
	for (; cri != NULL; cri = cri->cri_next) {
		switch (cri->cri_alg) {
		case CRYPTO_AES_CBC:
			if (encini != NULL) {
				printf("encini already set");
				return (EINVAL);
			}
                        encini = cri;
			break;
		case CRYPTO_AES_XTS:
		case CRYPTO_AES_RFC4106_GCM_16:
			if (encini != NULL) {
				printf("encini already set");
				return (EINVAL);
			}
                        encini = cri;
                        break;
		case CRYPTO_AES_128_GMAC:
		case CRYPTO_AES_192_GMAC:
		case CRYPTO_AES_256_GMAC:
		/*
		 * nothing to do here, maybe in the future cache some
		 * values for GHASH
		 */
			break;
		case CRYPTO_NULL_HMAC:
		case CRYPTO_MD5_HMAC:
		case CRYPTO_SHA1_HMAC:
		case CRYPTO_SHA2_256_HMAC:
		case CRYPTO_SHA2_384_HMAC:
		case CRYPTO_SHA2_512_HMAC:
			if (authini != NULL) {
				printf("authini already set");
				return (EINVAL);
			}
                        authini = cri;
			break;
		default:
			printf("unhandled algorithm");
			return (EINVAL);
		}
	}

	for (sessn = 1; sessn < sc->nsessions; sessn++) {
		if (!sc->sessions[sessn].used) {
			ses = &sc->sessions[sessn];
			break;
		}
	}
	if (ses == NULL) {
		ses = malloc(sizeof(*ses) * sc->nsessions * 2, M_AESNI, M_NOWAIT | M_ZERO);
		if (ses == NULL) {
			sc->sessions = ses;
			return (ENOMEM);
		}
		bcopy((void *)sc->sessions, (void *)ses, sc->nsessions * sizeof(*ses));
		atomic_set_ptr((u_long *)sc->sessions, (u_long)ses);
		bzero((void *)ses, sc->nsessions * sizeof(*ses));
		ses = &sc->sessions[sc->nsessions];
		ses->id = sc->nsessions;
		atomic_add_int(&sc->nsessions, 1);
	} else if (ses->id == 0)
		ses->id = sessn;

	if (encini != NULL) {
		if (ses->fpu_ctx == NULL) {
			ses->fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NORMAL |
			    FPU_KERN_NOWAIT);
			if (ses->fpu_ctx == NULL)
				return (ENOMEM);
		}
		ses->algo = encini->cri_alg;

		error = aesni_cipher_setup(ses, encini);
		if (error != 0) {
			printf("setup failed");
			aesni_freesession_locked(sc, ses);
			return (error);
		}
	}

	if (authini != NULL) {
		error = aesni_hash_setup(ses, authini);
		if (error != 0) {
			printf("setup failed");
			aesni_freesession_locked(sc, ses);
			return (error);
		}
	}

	ses->used = 1;
	*sidp = ses->id;

	return (0);
}

static void
aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
{
	struct fpu_kern_ctx *ctx;
	uint32_t sid;

	sid = ses->id;
	ctx = ses->fpu_ctx;
	bzero(ses, sizeof(*ses));
	ses->id = sid;
	ses->fpu_ctx = ctx;
	aesni_hash_free(ses);
}

static int
aesni_freesession(device_t dev, uint64_t tid)
{
	struct aesni_softc *sc;
	struct aesni_session *ses;
	uint32_t sid;

	sc = device_get_softc(dev);
	sid = ((uint32_t)tid) & 0xffffffff;
	if (sid >= sc->nsessions)
		return (EINVAL);

	ses = &sc->sessions[sid];
	if (ses == NULL)
		return (EINVAL);

	aesni_freesession_locked(sc, ses);
	return (0);
}

static int
aesni_process(device_t dev, struct cryptop *crp, int hint __unused)
{
	struct aesni_softc *sc = device_get_softc(dev);
	struct aesni_session *ses = NULL;
	struct cryptodesc *crd, *enccrd, *authcrd;
	uint32_t sid;
	int error, needauth;

	error = 0;
	enccrd = NULL;
	authcrd = NULL;
	needauth = 0;

	/* Sanity check. */
	if (crp == NULL)
		return (EINVAL);

	if (crp->crp_callback == NULL || crp->crp_desc == NULL)
		return (EINVAL);

	sid = ((uint32_t)crp->crp_sid) & 0xffffffff;
	if (sid >= sc->nsessions)
		return (EINVAL);

	for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
		switch (crd->crd_alg) {
		case CRYPTO_AES_CBC:
		case CRYPTO_AES_XTS:
			if (enccrd != NULL) {
				error = EINVAL;
				goto out;
			}
			enccrd = crd;
			break;

		case CRYPTO_AES_RFC4106_GCM_16:
			if (enccrd != NULL) {
				error = EINVAL;
				goto out;
			}
			enccrd = crd;
			needauth = 1;
			break;

		case CRYPTO_AES_128_GMAC:
		case CRYPTO_AES_192_GMAC:
		case CRYPTO_AES_256_GMAC:
			if (authcrd != NULL) {
				error = EINVAL;
				goto out;
			}
			authcrd = crd;
			needauth = 1;
			break;

		case CRYPTO_NULL_HMAC:
		case CRYPTO_MD5_HMAC:
		case CRYPTO_SHA1_HMAC:
		case CRYPTO_SHA2_256_HMAC:
		case CRYPTO_SHA2_384_HMAC:
		case CRYPTO_SHA2_512_HMAC:
			if (authcrd != NULL) {
				error = EINVAL;
				goto out;
			}
			authcrd = crd;
			needauth = 1;
			break;

		default:
			return (EINVAL);
		}
	}

	if (needauth && authcrd == NULL) {
		error = EINVAL;
		goto out;
	}

	/* CBC & XTS can only handle full blocks for now */
	if (enccrd != NULL && ((enccrd->crd_len == CRYPTO_AES_CBC || enccrd->crd_len ==
	    CRYPTO_AES_XTS) && (enccrd->crd_len % AES_BLOCK_LEN) != 0)) {
		error = EINVAL;
		goto out;
	}

	ses = &sc->sessions[sid];
	if (ses == NULL) {
		error = EINVAL;
		goto out;
	}

	error = aesni_cipher_process(ses, enccrd, authcrd, crp);
	if (error != 0)
		goto out;

out:
	crp->crp_etype = error;
	crypto_done(crp);
	return (error);
}

uint8_t *
aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
    int *allocated)
{
	struct mbuf *m;
	struct uio *uio;
	struct iovec *iov;
	uint8_t *addr;

	if (crp->crp_flags & CRYPTO_F_IMBUF) {
		m = (struct mbuf *)crp->crp_buf;
		if (m->m_next != NULL)
			goto alloc;
		addr = mtod(m, uint8_t *);
	} else if (crp->crp_flags & CRYPTO_F_IOV) {
		uio = (struct uio *)crp->crp_buf;
		if (uio->uio_iovcnt != 1)
			goto alloc;
		iov = uio->uio_iov;
		addr = (u_char *)iov->iov_base + enccrd->crd_skip;
	} else
		addr = (u_char *)crp->crp_buf;
	*allocated = 0;
	addr += enccrd->crd_skip;
	return (addr);

alloc:
	addr = malloc(enccrd->crd_len, M_AESNI, M_NOWAIT);
	if (addr != NULL) {
		*allocated = 1;
		crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
		    enccrd->crd_len, addr);
	} else
		*allocated = 0;
	return (addr);
}

static device_method_t aesni_methods[] = {
	DEVMETHOD(device_identify, aesni_identify),
	DEVMETHOD(device_probe, aesni_probe),
	DEVMETHOD(device_attach, aesni_attach),
	DEVMETHOD(device_detach, aesni_detach),

	DEVMETHOD(cryptodev_newsession, aesni_newsession),
	DEVMETHOD(cryptodev_freesession, aesni_freesession),
	DEVMETHOD(cryptodev_process, aesni_process),

	{0, 0},
};

static driver_t aesni_driver = {
	"aesni",
	aesni_methods,
	sizeof(struct aesni_softc),
};
static devclass_t aesni_devclass;

DRIVER_MODULE(aesni, nexus, aesni_driver, aesni_devclass, 0, 0);
MODULE_VERSION(aesni, 1);
MODULE_DEPEND(aesni, crypto, 1, 1, 1);

static int
aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini)
{
	struct thread *td;
	int error;

	td = curthread;
	critical_enter();
	error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL |
	    FPU_KERN_KTHR);
	if (error != 0) {
		critical_exit();
		return (error);
	}
	error = aesni_cipher_setup_common(ses, encini->cri_key,
	    encini->cri_klen);
	fpu_kern_leave(td, ses->fpu_ctx);
	critical_exit();
	return (error);
}

#ifdef AESNI_DEBUG
static void
aesni_printhexstr(uint8_t *ptr, int len)
{
	int i;

	for (i = 0; i < len; i++)
		printf("%02hhx", ptr[i]);
}
#endif

static int
aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
    struct cryptodesc *authcrd, struct cryptop *crp)
{
	uint8_t *tag;
	uint8_t *iv;
	struct thread *td;
	uint8_t *buf, *authbuf;
	int error, allocated, authallocated;
	int ivlen, encflag, i;

	allocated = authallocated = 0;
	encflag = error = 0;
	authbuf = NULL;

	if (enccrd != NULL) {
		encflag = (enccrd->crd_flags & CRD_F_ENCRYPT) == CRD_F_ENCRYPT;

		buf = aesni_cipher_alloc(enccrd, crp, &allocated);
		if (buf == NULL)
			return (ENOMEM);

		iv = enccrd->crd_iv;
		/* XXX - validate that enccrd and authcrd have/use same key? */
		switch (enccrd->crd_alg) {
		case CRYPTO_AES_CBC:
			ivlen = 16;
			break;
		case CRYPTO_AES_XTS:
			ivlen = 8;
			break;
		case CRYPTO_AES_RFC4106_GCM_16:
			/* Be smart at determining the ivlen until better ways are present */
			ivlen = enccrd->crd_skip - enccrd->crd_inject;
			ivlen += 4;
			break;
		}

		/* Setup ses->iv */
		if (encflag) {
			if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0) {
				if (enccrd->crd_alg == CRYPTO_AES_RFC4106_GCM_16) {
					for (i = 0; i < AESCTR_NONCESIZE; i++)
						iv[i] = ses->nonce[i];
					/* XXX: Is this enough? */
					u_long counter = atomic_fetchadd_long(&ses->aesgcmcounter, 1);
					bcopy((void *)&counter, iv + AESCTR_NONCESIZE, sizeof(uint64_t));
					crypto_copyback(crp->crp_flags, crp->crp_buf,
					    enccrd->crd_inject, AESCTR_IVSIZE, iv + AESCTR_NONCESIZE);
				} else {
					arc4rand(iv, AES_BLOCK_LEN, 0);
					crypto_copyback(crp->crp_flags, crp->crp_buf,
					    enccrd->crd_inject, ivlen, iv);
				}
			}
		} else {
			if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) == 0) {
				if (enccrd->crd_alg == CRYPTO_AES_RFC4106_GCM_16) {
					for (i = 0; i < AESCTR_NONCESIZE; i++)
						iv[i] = ses->nonce[i];
					crypto_copydata(crp->crp_flags, crp->crp_buf,
					    enccrd->crd_inject, AESCTR_IVSIZE, iv + AESCTR_NONCESIZE);
				} else
					crypto_copydata(crp->crp_flags, crp->crp_buf,
					    enccrd->crd_inject, ivlen, iv);
			}
		}
#ifdef AESNI_DEBUG
		aesni_printhexstr(iv, ivlen);
		printf("\n");
#endif
	}

	if (authcrd != NULL) {
		authbuf = aesni_cipher_alloc(authcrd, crp, &authallocated);
		if (authbuf == NULL) {
			error = ENOMEM;
			goto out1;
		}

		if (ses->algo == CRYPTO_AES_RFC4106_GCM_16) {
			/* NOTE: GMAC_DIGEST_LEN == AES_BLOCK_LEN */
			tag = authcrd->crd_iv;

			if (!encflag) {
				crypto_copydata(crp->crp_flags, crp->crp_buf,
				    authcrd->crd_inject, GMAC_DIGEST_LEN, tag);
			} else {
#ifdef AESNI_DEBUG
				printf("ptag: ");
				aesni_printhexstr(tag, sizeof tag);
				printf("\n");
#endif
				bzero(tag, sizeof tag);
			}
		}
	}
	/* Called by stack only for HASH operation? */
	if (enccrd == NULL)
		goto out1;

	td = curthread;

	critical_enter();
	error = fpu_kern_enter(td, ses->fpu_ctx, FPU_KERN_NORMAL |
	    FPU_KERN_KTHR);
	if (error != 0) {
		critical_exit();
		goto out1;
	}
	/* Do work */
	switch (ses->algo) {
	case CRYPTO_AES_CBC:
		if (encflag)
			aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
			    enccrd->crd_len, buf, buf, iv);
		else
			aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
			    enccrd->crd_len, buf, iv);
		break;
	case CRYPTO_AES_XTS:
		if (encflag)
			aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
			    ses->xts_schedule, enccrd->crd_len, buf, buf,
			    iv);
		else
			aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
			    ses->xts_schedule, enccrd->crd_len, buf, buf,
			    iv);
		break;
	case CRYPTO_AES_RFC4106_GCM_16:
#ifdef AESNI_DEBUG
		printf("GCM: %d\n", encflag);
		printf("buf(%d): ", enccrd->crd_len);
		aesni_printhexstr(buf, enccrd->crd_len);
		printf("\nauthbuf(%d): ", authcrd->crd_len);
		aesni_printhexstr(authbuf, authcrd->crd_len);
		printf("\niv: ");
		aesni_printhexstr(iv, ivlen);
		printf("\ntag: ");
		aesni_printhexstr(tag, 16);
		printf("\nsched: ");
		aesni_printhexstr(ses->enc_schedule, 16 * (ses->rounds + 1));
		printf("\n");
#endif
		if (encflag)
			AES_GCM_encrypt(buf, buf, authbuf, iv, tag,
			    enccrd->crd_len, authcrd->crd_len, ivlen,
			    ses->enc_schedule, ses->rounds);
		else {
			if (!AES_GCM_decrypt(buf, buf, authbuf, iv, tag,
			    enccrd->crd_len, authcrd->crd_len, ivlen,
			    ses->enc_schedule, ses->rounds))
				error = EBADMSG;
		}
		break;
	}
	fpu_kern_leave(td, ses->fpu_ctx);
	critical_exit();

	if (allocated)
		crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
		    enccrd->crd_len, buf);

	if (!error && authcrd != NULL) {
		if (ses->algo == CRYPTO_AES_RFC4106_GCM_16) {
			crypto_copyback(crp->crp_flags, crp->crp_buf,
			    authcrd->crd_inject, crp->crp_ilen - authcrd->crd_inject, tag);
		} else if (enccrd->crd_next == authcrd) {
			error = aesni_hash_process(ses, authcrd, crp);
		}
	}

out1:
	if (allocated) {
		bzero(buf, enccrd->crd_len);
		free(buf, M_AESNI);
	}
	if (authallocated)
		free(authbuf, M_AESNI);

	return (error);
}
OpenPOWER on IntegriCloud