From 3eda0dd861fc43d2637c61cf4fd67705e13366de Mon Sep 17 00:00:00 2001
From: Michael Neumann <mneumann@ntecs.de>
Date: Thu, 10 Oct 2024 10:55:04 +0200
Subject: [PATCH] Retire crypto(9) and tcplay(8) and refactor dm_target_crypt

* Completely remove the OpenBSD-inherited in-kernel crypto(9) framework
  that lived in sys/crypto and hardware drivers in dev/crypto. With it,
  remove /dev/crypto - the only application relying on it in DragonFly
  was tcplay...

* Remove tcplay(8). It was severly broken and could panic the kernel by
  exceeding the malloc limits; see https://bugs.dragonflybsd.org/issues/3370

* Remove hifn(4), safe(4), ubsec(4) and padlock(4) drivers. These  were
  removed in OpenBSD 7.1 and it's unlikely that they are still in use by
  someone.

* In-place of crypto(9), add a simple non-allocating API that lives in
  crypto_ciphers.{h,c}. It's only used by dm_target_crypt.

* Remove support for the following crypto algorithms:

  Blowfish, Camellia, Cast, Serpent, Skipjack, Twofish

  Mainly, because I am too lazy to test any of these with
  dm_target_crypt, but also, because there is no hardware support for
  any of these.

* Greatly refactor dm_target_crypt. It used to encrypt/decrypt blocks in
  pieces of 512 bytes and send them over to the async crypto(9) framework.
  The async-ness of crypto(9) led to overly complex code. The new
  code processes each BIO read or write request by a worker thread.
  There are ncpu kthreads to process read requests, and another
  set of ncpu kthreads to process write requests. Instead of using a
  malloc_pipe as before, and allocating memory for each BIO request
  from that mpipe, each worker kthread now simply owns the required
  memory. That being said, it is 100% non-allocating.
  The BIO requests are distributing to the worker threads round robin.
  Performance-wise it is about the same speed as before, slightly better,
  whereas interactivity is a lot better now - mainly concerning the mouse
  cursor, which keeps on moving rather smooth, even under higher load.
  Under very high I/O, the rest of the system still because very slow,
  but the same happens without dm_target_crypt.
  Note that I test using a rather old i5-8350U Panasoci laptop that
  still has SATA SSD.

* The AESNI (hardware instruction for AES as found in most Intel/AMD CPUs)
  code moved from dev/aesni to crypto/. It is no longer a device.
  We simply check for the CPU feature when "probing" for available
  crypto ciphers. Note that by default, the hardware-accelerated
  ciphers (AES-CBC, AES-XTS) are prefered over the software
  implementation. You can disable aesni by setting sysctl
  hw.aesni_disable=1. Note that, setting this will not have immediate
  effect for already "mounted" dm_target_crypt targets, as the crypto
  cipher is queried only at attach time.

* Use explicit_bzero in a few places.

* I've tested both AES-CBC and AES-XTS with and w/o AESNI. Basically, I
  formatted a device (cryptsetup luksFormat) with the old kernel, write
  some data on it, then switched to the new kernel, and tried to open it.
---
 Makefile_upgrade.inc                          |   17 +
 UPDATING                                      |   15 +
 contrib/mdocml/lib.in                         |    1 -
 initrd/etc/Makefile                           |    2 +-
 initrd/etc/rcmount_tcplay                     |    9 -
 initrd/rescue.libcrypto/Makefile              |    5 +-
 lib/Makefile                                  |    1 -
 lib/libtcplay/Makefile                        |   33 -
 lib/libtcplay/crc32.c                         |  109 -
 lib/libtcplay/crc32.h                         |    2 -
 lib/libtcplay/crypto-dev.c                    |  165 -
 lib/libtcplay/crypto.c                        |  267 --
 lib/libtcplay/generic_xts.c                   |  175 -
 lib/libtcplay/generic_xts.h                   |   49 -
 lib/libtcplay/hdr.c                           |  432 ---
 lib/libtcplay/humanize.c                      |  199 --
 lib/libtcplay/humanize.h                      |   32 -
 lib/libtcplay/io.c                            |  466 ---
 lib/libtcplay/pbkdf2-openssl.c                |   61 -
 lib/libtcplay/safe_mem.c                      |  196 --
 lib/libtcplay/tcplay.3                        |  564 ----
 lib/libtcplay/tcplay.c                        | 2215 -------------
 lib/libtcplay/tcplay.h                        |  352 --
 lib/libtcplay/tcplay.map                      |   15 -
 lib/libtcplay/tcplay_api.c                    |  806 -----
 lib/libtcplay/tcplay_api.h                    |   75 -
 lib/libtcplay/tcplay_api_internal.h           |   52 -
 sbin/Makefile                                 |    1 -
 sbin/cryptdisks/Makefile                      |    4 +-
 sbin/cryptdisks/cryptdisks.8                  |    4 +-
 sbin/cryptdisks/cryptdisks.c                  |  103 +-
 sbin/tcplay/Makefile                          |   28 -
 sbin/tcplay/main.c                            |  480 ---
 sbin/tcplay/tcplay.8                          |  570 ----
 share/man/man4/Makefile                       |    6 -
 share/man/man4/aesni.4                        |    4 +-
 share/man/man4/crypto.4                       |  135 -
 share/man/man4/dm_target_crypt.4              |    1 -
 share/man/man4/hifn.4                         |  130 -
 share/man/man4/padlock.4                      |   97 -
 share/man/man4/rdrand.4                       |    1 -
 share/man/man4/rndtest.4                      |    4 -
 share/man/man4/safe.4                         |  126 -
 share/man/man4/ubsec.4                        |  104 -
 share/man/man7/initrd.7                       |   16 +-
 share/man/man9/Makefile                       |    9 -
 share/man/man9/crypto.9                       |  329 +-
 share/mk/bsd.libnames.mk                      |    1 -
 sys/conf/files                                |   27 +-
 sys/conf/kmod.mk                              |    2 +-
 sys/conf/options                              |   15 -
 sys/config/LINT64                             |   20 +-
 sys/config/X86_64_GENERIC                     |    3 -
 sys/crypto/Makefile                           |   27 +-
 sys/{dev => }/crypto/aesni/aesencdec_x86_64.S |    0
 sys/{dev => }/crypto/aesni/aeskeys_x86_64.S   |    0
 sys/crypto/aesni/aesni.c                      |  384 +++
 sys/crypto/aesni/aesni.h                      |   71 +
 sys/crypto/blowfish/bf_enc.c                  |  155 -
 sys/crypto/blowfish/bf_locl.h                 |  173 -
 sys/crypto/blowfish/bf_pi.h                   |  328 --
 sys/crypto/blowfish/bf_skey.c                 |  118 -
 sys/crypto/blowfish/blowfish.h                |   93 -
 sys/crypto/camellia/camellia-api.c            |   56 -
 sys/crypto/camellia/camellia.c                | 1332 --------
 sys/crypto/camellia/camellia.h                |   69 -
 sys/crypto/crypto_cipher.c                    |  389 +++
 sys/crypto/crypto_cipher.h                    |   54 +
 sys/crypto/crypto_mod.c                       |   42 +
 sys/{opencrypto => crypto/rmd160}/rmd160.c    |    2 +-
 sys/{opencrypto => crypto/rmd160}/rmd160.h    |    0
 sys/crypto/serpent/serpent.c                  |  631 ----
 sys/crypto/serpent/serpent.h                  |   16 -
 sys/crypto/twofish/twofish.c                  |  514 ---
 sys/crypto/twofish/twofish.h                  |   16 -
 sys/dev/crypto/Makefile                       |    2 +-
 sys/dev/crypto/aesni/Makefile                 |    8 -
 sys/dev/crypto/aesni/aesni.c                  |  353 --
 sys/dev/crypto/aesni/aesni.h                  |  103 -
 sys/dev/crypto/aesni/aesni_wrap.c             |  331 --
 sys/dev/crypto/cryptodev/Makefile             |    7 -
 sys/dev/crypto/hifn/Makefile                  |   13 -
 sys/dev/crypto/hifn/hifn7751.c                | 2938 -----------------
 sys/dev/crypto/hifn/hifn7751reg.h             |  590 ----
 sys/dev/crypto/hifn/hifn7751var.h             |  365 --
 sys/dev/crypto/padlock/Makefile               |    8 -
 sys/dev/crypto/padlock/padlock.c              |  427 ---
 sys/dev/crypto/padlock/padlock.h              |  101 -
 sys/dev/crypto/padlock/padlock_cipher.c       |  266 --
 sys/dev/crypto/padlock/padlock_hash.c         |  388 ---
 sys/dev/crypto/padlock/padlock_rng.c          |   78 -
 sys/dev/crypto/padlock/rng_harvest_x86_64.S   |   75 -
 sys/dev/crypto/safe/Makefile                  |   39 -
 sys/dev/crypto/safe/safe.c                    | 2248 -------------
 sys/dev/crypto/safe/safereg.h                 |  412 ---
 sys/dev/crypto/safe/safevar.h                 |  219 --
 sys/dev/crypto/ubsec/Makefile                 |   13 -
 sys/dev/crypto/ubsec/ubsec.c                  | 2856 ----------------
 sys/dev/crypto/ubsec/ubsecreg.h               |  221 --
 sys/dev/crypto/ubsec/ubsecvar.h               |  256 --
 sys/dev/disk/dm/crypt/Makefile                |    4 +-
 sys/dev/disk/dm/crypt/dm_target_crypt.c       | 1379 ++++----
 sys/dev/raid/arcmsr/arcmsr.h                  |    1 +
 sys/opencrypto/cast.c                         |  242 --
 sys/opencrypto/cast.h                         |   23 -
 sys/opencrypto/castsb.h                       |  545 ---
 sys/opencrypto/criov.c                        |  197 --
 sys/opencrypto/crypto.c                       | 1651 ---------
 sys/opencrypto/cryptodev.c                    |  930 ------
 sys/opencrypto/cryptodev.h                    |  462 ---
 sys/opencrypto/cryptodev_if.m                 |   55 -
 sys/opencrypto/cryptosoft.c                   | 1433 --------
 sys/opencrypto/cryptosoft.h                   |   69 -
 sys/opencrypto/deflate.c                      |  174 -
 sys/opencrypto/deflate.h                      |   56 -
 sys/opencrypto/gmac.c                         |  159 -
 sys/opencrypto/gmac.h                         |   51 -
 sys/opencrypto/skipjack.c                     |  258 --
 sys/opencrypto/skipjack.h                     |   19 -
 sys/opencrypto/xform.c                        | 1099 ------
 sys/opencrypto/xform.h                        |  119 -
 sys/platform/pc64/conf/files                  |   13 +-
 sys/platform/vkernel64/conf/files             |    1 -
 tools/tools/crypto/Makefile                   |   63 -
 tools/tools/crypto/README                     |   45 -
 tools/tools/crypto/cryptokeytest.c            |  287 --
 tools/tools/crypto/cryptostats.c              |  105 -
 tools/tools/crypto/cryptotest.c               |  622 ----
 tools/tools/crypto/hifnstats.c                |   61 -
 tools/tools/crypto/ubsecstats.c               |   69 -
 130 files changed, 1602 insertions(+), 33912 deletions(-)
 delete mode 100644 initrd/etc/rcmount_tcplay
 delete mode 100644 lib/libtcplay/Makefile
 delete mode 100644 lib/libtcplay/crc32.c
 delete mode 100644 lib/libtcplay/crc32.h
 delete mode 100644 lib/libtcplay/crypto-dev.c
 delete mode 100644 lib/libtcplay/crypto.c
 delete mode 100644 lib/libtcplay/generic_xts.c
 delete mode 100644 lib/libtcplay/generic_xts.h
 delete mode 100644 lib/libtcplay/hdr.c
 delete mode 100644 lib/libtcplay/humanize.c
 delete mode 100644 lib/libtcplay/humanize.h
 delete mode 100644 lib/libtcplay/io.c
 delete mode 100644 lib/libtcplay/pbkdf2-openssl.c
 delete mode 100644 lib/libtcplay/safe_mem.c
 delete mode 100644 lib/libtcplay/tcplay.3
 delete mode 100644 lib/libtcplay/tcplay.c
 delete mode 100644 lib/libtcplay/tcplay.h
 delete mode 100644 lib/libtcplay/tcplay.map
 delete mode 100644 lib/libtcplay/tcplay_api.c
 delete mode 100644 lib/libtcplay/tcplay_api.h
 delete mode 100644 lib/libtcplay/tcplay_api_internal.h
 delete mode 100644 sbin/tcplay/Makefile
 delete mode 100644 sbin/tcplay/main.c
 delete mode 100644 sbin/tcplay/tcplay.8
 delete mode 100644 share/man/man4/crypto.4
 delete mode 100644 share/man/man4/hifn.4
 delete mode 100644 share/man/man4/padlock.4
 delete mode 100644 share/man/man4/safe.4
 delete mode 100644 share/man/man4/ubsec.4
 rename sys/{dev => }/crypto/aesni/aesencdec_x86_64.S (100%)
 rename sys/{dev => }/crypto/aesni/aeskeys_x86_64.S (100%)
 create mode 100644 sys/crypto/aesni/aesni.c
 create mode 100644 sys/crypto/aesni/aesni.h
 delete mode 100644 sys/crypto/blowfish/bf_enc.c
 delete mode 100644 sys/crypto/blowfish/bf_locl.h
 delete mode 100644 sys/crypto/blowfish/bf_pi.h
 delete mode 100644 sys/crypto/blowfish/bf_skey.c
 delete mode 100644 sys/crypto/blowfish/blowfish.h
 delete mode 100644 sys/crypto/camellia/camellia-api.c
 delete mode 100644 sys/crypto/camellia/camellia.c
 delete mode 100644 sys/crypto/camellia/camellia.h
 create mode 100644 sys/crypto/crypto_cipher.c
 create mode 100644 sys/crypto/crypto_cipher.h
 create mode 100644 sys/crypto/crypto_mod.c
 rename sys/{opencrypto => crypto/rmd160}/rmd160.c (99%)
 rename sys/{opencrypto => crypto/rmd160}/rmd160.h (100%)
 delete mode 100644 sys/crypto/serpent/serpent.c
 delete mode 100644 sys/crypto/serpent/serpent.h
 delete mode 100644 sys/crypto/twofish/twofish.c
 delete mode 100644 sys/crypto/twofish/twofish.h
 delete mode 100644 sys/dev/crypto/aesni/Makefile
 delete mode 100644 sys/dev/crypto/aesni/aesni.c
 delete mode 100644 sys/dev/crypto/aesni/aesni.h
 delete mode 100644 sys/dev/crypto/aesni/aesni_wrap.c
 delete mode 100644 sys/dev/crypto/cryptodev/Makefile
 delete mode 100644 sys/dev/crypto/hifn/Makefile
 delete mode 100644 sys/dev/crypto/hifn/hifn7751.c
 delete mode 100644 sys/dev/crypto/hifn/hifn7751reg.h
 delete mode 100644 sys/dev/crypto/hifn/hifn7751var.h
 delete mode 100644 sys/dev/crypto/padlock/Makefile
 delete mode 100644 sys/dev/crypto/padlock/padlock.c
 delete mode 100644 sys/dev/crypto/padlock/padlock.h
 delete mode 100644 sys/dev/crypto/padlock/padlock_cipher.c
 delete mode 100644 sys/dev/crypto/padlock/padlock_hash.c
 delete mode 100644 sys/dev/crypto/padlock/padlock_rng.c
 delete mode 100644 sys/dev/crypto/padlock/rng_harvest_x86_64.S
 delete mode 100644 sys/dev/crypto/safe/Makefile
 delete mode 100644 sys/dev/crypto/safe/safe.c
 delete mode 100644 sys/dev/crypto/safe/safereg.h
 delete mode 100644 sys/dev/crypto/safe/safevar.h
 delete mode 100644 sys/dev/crypto/ubsec/Makefile
 delete mode 100644 sys/dev/crypto/ubsec/ubsec.c
 delete mode 100644 sys/dev/crypto/ubsec/ubsecreg.h
 delete mode 100644 sys/dev/crypto/ubsec/ubsecvar.h
 delete mode 100644 sys/opencrypto/cast.c
 delete mode 100644 sys/opencrypto/cast.h
 delete mode 100644 sys/opencrypto/castsb.h
 delete mode 100644 sys/opencrypto/criov.c
 delete mode 100644 sys/opencrypto/crypto.c
 delete mode 100644 sys/opencrypto/cryptodev.c
 delete mode 100644 sys/opencrypto/cryptodev.h
 delete mode 100644 sys/opencrypto/cryptodev_if.m
 delete mode 100644 sys/opencrypto/cryptosoft.c
 delete mode 100644 sys/opencrypto/cryptosoft.h
 delete mode 100644 sys/opencrypto/deflate.c
 delete mode 100644 sys/opencrypto/deflate.h
 delete mode 100644 sys/opencrypto/gmac.c
 delete mode 100644 sys/opencrypto/gmac.h
 delete mode 100644 sys/opencrypto/skipjack.c
 delete mode 100644 sys/opencrypto/skipjack.h
 delete mode 100644 sys/opencrypto/xform.c
 delete mode 100644 sys/opencrypto/xform.h
 delete mode 100644 tools/tools/crypto/Makefile
 delete mode 100644 tools/tools/crypto/README
 delete mode 100644 tools/tools/crypto/cryptokeytest.c
 delete mode 100644 tools/tools/crypto/cryptostats.c
 delete mode 100644 tools/tools/crypto/cryptotest.c
 delete mode 100644 tools/tools/crypto/hifnstats.c
 delete mode 100644 tools/tools/crypto/ubsecstats.c

diff --git a/Makefile_upgrade.inc b/Makefile_upgrade.inc
index c28c9af808..7a1c3f1f59 100644
--- a/Makefile_upgrade.inc
+++ b/Makefile_upgrade.inc
@@ -3929,6 +3929,23 @@ TO_REMOVE+=/etc/mail/mailer.conf.dma
 TO_REMOVE+=/etc/mail/mailer.conf.opensmtpd
 TO_REMOVE+=/etc/mail/mailer.conf.postfix
 TO_REMOVE+=/etc/mail/mailer.conf.sendmail
+TO_REMOVE+=/usr/share/man/man4/hifn.4.gz
+TO_REMOVE+=/usr/share/man/man4/padlock.4.gz
+TO_REMOVE+=/usr/share/man/man4/safe.4.gz
+TO_REMOVE+=/usr/share/man/man4/ubsec.4.gz
+TO_REMOVE+=/usr/share/man/man4/crypto.4.gz
+TO_REMOVE+=/usr/share/man/man4/cryptodev.4.gz
+TO_REMOVE+=/usr/share/man/man9/crypto_find_device_byhid.9.gz
+TO_REMOVE+=/usr/share/man/man9/crypto_find_driver.9.gz
+TO_REMOVE+=/usr/share/man/man9/crypto_get_caps.9.gz
+TO_REMOVE+=/usr/share/man/man9/crypto_freereq.9.gz
+TO_REMOVE+=/usr/share/man/man9/crypto_getreq.9.gz
+TO_REMOVE+=/usr/share/man/man9/crypto_unblock.9.gz
+TO_REMOVE+=/usr/lib/libtcplay.so.3
+TO_REMOVE+=/usr/lib/libtcplay.so
+TO_REMOVE+=/usr/lib/libtcplay.a
+TO_REMOVE+=/usr/share/man/man8/tcplay.8.gz
+TO_REMOVE+=/sbin/tcplay
 
 .if !defined(WANT_INSTALLER)
 TO_REMOVE+=/usr/sbin/dfuibe_installer
diff --git a/UPDATING b/UPDATING
index 72ec730267..228f5e93e9 100644
--- a/UPDATING
+++ b/UPDATING
@@ -8,6 +8,21 @@
 # If you discover any problem, please contact the bugs@lists.dragonflybsd.org
 # mailing list with the details.
 
++-----------------------------------------------------------------------+
++         UPGRADING DRAGONFLY FROM 6.4 TO LATER VERSIONS                +
++-----------------------------------------------------------------------+
+
+PADLOCK CRYPTO DRIVER REMOVED
+-----------------------------
+
+Likely no VIA cpus in use on DragonFly.
+
+HIFN, SAFE AND UBSEC CRYPTO DRIVERS REMOVED
+-------------------------------------------
+
+These were removed in OpenBSD 7.1 and it's unlikely that they are still
+in use by someone.
+
 +-----------------------------------------------------------------------+
 +         UPGRADING DRAGONFLY FROM 6.0 TO LATER VERSIONS                +
 +-----------------------------------------------------------------------+
diff --git a/contrib/mdocml/lib.in b/contrib/mdocml/lib.in
index 07f37b7296..a8bac59a4c 100644
--- a/contrib/mdocml/lib.in
+++ b/contrib/mdocml/lib.in
@@ -124,7 +124,6 @@ LINE("libstdthreads",	"C11 Threads Library (libstdthreads, \\-lstdthreads)")
 LINE("libSystem",	"System Library (libSystem, \\-lSystem)")
 LINE("libsysdecode",	"System Argument Decoding Library (libsysdecode, \\-lsysdecode)")
 LINE("libtacplus",	"TACACS+ Client Library (libtacplus, \\-ltacplus)")
-LINE("libtcplay",	"TrueCrypt-compatible API library (libtcplay, \\-ltcplay)")
 LINE("libtermcap",	"Termcap Access Library (libtermcap, \\-ltermcap)")
 LINE("libterminfo",	"Terminal Information Library (libterminfo, \\-lterminfo)")
 LINE("libthr",		"1:1 Threading Library (libthr, \\-lthr)")
diff --git a/initrd/etc/Makefile b/initrd/etc/Makefile
index f811d68cfc..afc8ecb43f 100644
--- a/initrd/etc/Makefile
+++ b/initrd/etc/Makefile
@@ -1,4 +1,4 @@
-ETC_SCRIPTS=	rc rc.lvm2 rcmount_crypt rcmount_tcplay
+ETC_SCRIPTS=	rc rc.lvm2 rcmount_crypt
 ETC_FILES=	${.CURDIR}/motd \
 		${.CURDIR}/termcap \
 		${.CURDIR}/../../etc/group \
diff --git a/initrd/etc/rcmount_tcplay b/initrd/etc/rcmount_tcplay
deleted file mode 100644
index 03bb5736ef..0000000000
--- a/initrd/etc/rcmount_tcplay
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-FSTYPE=$2
-MOUNTFROM="/dev/${3#/dev/}"
-VOLUME=$4
-OPTIONS=$5
-
-tcplay -m $VOLUME -d $MOUNTFROM $OPTIONS || return 2
-mount -o ro -t $FSTYPE /dev/mapper/$VOLUME $NEW_ROOT || return 3
diff --git a/initrd/rescue.libcrypto/Makefile b/initrd/rescue.libcrypto/Makefile
index 350dc11676..fbcb9631b7 100644
--- a/initrd/rescue.libcrypto/Makefile
+++ b/initrd/rescue.libcrypto/Makefile
@@ -40,8 +40,7 @@ CRUNCH_PROGS_sbin=	\
 	md5		\
 	hammer		\
 	hammer2		\
-	ping6		\
-	tcplay
+	ping6
 
 CRUNCH_ALIAS_md5= rmd160 sha1 sha256 sha512
 
@@ -57,7 +56,7 @@ CRUNCH_SRCDIRS+= gnu/sbin
 CRUNCH_PATH_gnu/sbin= ${.CURDIR}/../..
 CRUNCH_PROGS_gnu/sbin= cryptsetup
 
-CRUNCH_LIBS+= -ldm -lprop  # tcplay, cryptsetup
+CRUNCH_LIBS+= -ldm -lprop  # cryptsetup
 CRUNCH_LIBS+= -lpcap  # wpa_supplicant
 CRUNCH_LIBS+= -lhammer  # hammer
 CRUNCH_LIBS+= -ldmsg  # hammer2
diff --git a/lib/Makefile b/lib/Makefile
index 4e24ed1cc1..0cafe52b47 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -87,7 +87,6 @@ SUBDIR=	${SUBDIR_ORDERED} \
 	${_libsmdb} \
 	${_libsmutil} \
 	libstdbuf \
-	libtcplay \
 	libtelnet \
 	libu4bhid \
 	libvgl \
diff --git a/lib/libtcplay/Makefile b/lib/libtcplay/Makefile
deleted file mode 100644
index 65f158f93c..0000000000
--- a/lib/libtcplay/Makefile
+++ /dev/null
@@ -1,33 +0,0 @@
-LIB=		tcplay
-MAJ_VER=	3
-MIN_VER=	3
-SHLIB_MAJOR?=	${MAJ_VER}
-SHLIB_MINOR?=	${MIN_VER}
-MAN=		tcplay.3
-
-MLINKS+=	tcplay.3	tc_api_init.3
-MLINKS+=	tcplay.3	tc_api_uninit.3
-MLINKS+=	tcplay.3	tc_api_has.3
-MLINKS+=	tcplay.3	tc_api_cipher_iterate.3
-MLINKS+=	tcplay.3	tc_api_prf_iterate.3
-MLINKS+=	tcplay.3	tc_api_task_init.3
-MLINKS+=	tcplay.3	tc_api_task_uninit.3
-MLINKS+=	tcplay.3	tc_api_task_set.3
-MLINKS+=	tcplay.3	tc_api_task_do.3
-MLINKS+=	tcplay.3	tc_api_task_info_get.3
-MLINKS+=	tcplay.3	tc_api_task_get_error.3
-
-CFLAGS+=	-DMAJ_VER=${MAJ_VER} -DMIN_VER=${MIN_VER}
-CFLAGS+=	-D_FILE_OFFSET_BITS=64
-CFLAGS+=	-I${.CURDIR}/../../crypto/libressl/include
-
-SRCS+=		tcplay.c crc32.c safe_mem.c io.c hdr.c humanize.c
-SRCS+=		crypto.c generic_xts.c
-SRCS+=		crypto-dev.c pbkdf2-openssl.c
-SRCS+=		tcplay_api.c
-
-INCS+=		tcplay_api.h
-
-LDFLAGS+=	-Wl,-version-script=${.CURDIR}/tcplay.map
-
-.include <bsd.lib.mk>
diff --git a/lib/libtcplay/crc32.c b/lib/libtcplay/crc32.c
deleted file mode 100644
index ac8f8215e3..0000000000
--- a/lib/libtcplay/crc32.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/*-
- *  COPYRIGHT (C) 1986 Gary S. Brown.  You may use this program, or
- *  code or tables extracted from it, as desired without restriction.
- *
- *  First, the polynomial itself and its table of feedback terms.  The
- *  polynomial is
- *  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
- *
- *  Note that we take it "backwards" and put the highest-order term in
- *  the lowest-order bit.  The X^32 term is "implied"; the LSB is the
- *  X^31 term, etc.  The X^0 term (usually shown as "+1") results in
- *  the MSB being 1
- *
- *  Note that the usual hardware shift register implementation, which
- *  is what we're using (we're merely optimizing it by doing eight-bit
- *  chunks at a time) shifts bits into the lowest-order term.  In our
- *  implementation, that means shifting towards the right.  Why do we
- *  do it this way?  Because the calculated CRC must be transmitted in
- *  order from highest-order term to lowest-order term.  UARTs transmit
- *  characters in order from LSB to MSB.  By storing the CRC this way
- *  we hand it to the UART in the order low-byte to high-byte; the UART
- *  sends each low-bit to hight-bit; and the result is transmission bit
- *  by bit from highest- to lowest-order term without requiring any bit
- *  shuffling on our part.  Reception works similarly
- *
- *  The feedback terms table consists of 256, 32-bit entries.  Notes
- *
- *      The table can be generated at runtime if desired; code to do so
- *      is shown later.  It might not be obvious, but the feedback
- *      terms simply represent the results of eight shift/xor opera
- *      tions for all combinations of data and CRC register values
- *
- *      The values must be right-shifted by eight bits by the "updcrc
- *      logic; the shift must be unsigned (bring in zeroes).  On some
- *      hardware you could probably optimize the shift in assembler by
- *      using byte-swap instructions
- *      polynomial $edb88320
- */
-
-#include <stdlib.h>
-#include <stdint.h>
-#include "crc32.h"
-
-uint32_t crc32_tab[] = {
-	0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
-	0xe963a535, 0x9e6495a3,	0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
-	0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
-	0xf3b97148, 0x84be41de,	0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
-	0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,	0x14015c4f, 0x63066cd9,
-	0xfa0f3d63, 0x8d080df5,	0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
-	0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,	0x35b5a8fa, 0x42b2986c,
-	0xdbbbc9d6, 0xacbcf940,	0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
-	0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
-	0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
-	0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,	0x76dc4190, 0x01db7106,
-	0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
-	0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
-	0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
-	0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
-	0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
-	0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
-	0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
-	0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
-	0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
-	0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
-	0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
-	0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
-	0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
-	0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
-	0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
-	0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
-	0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
-	0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
-	0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
-	0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
-	0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
-	0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
-	0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
-	0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
-	0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
-	0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
-	0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
-	0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
-	0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
-	0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
-	0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
-	0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
-};
-
-uint32_t
-crc32(const void *buf, size_t size)
-{
-	const uint8_t *p;
-	uint32_t crc;
-
-	p = buf;
-	crc = ~0U;
-
-	while (size--)
-		crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
-
-	return crc ^ ~0U;
-}
-
-uint32_t
-crc32_intermediate(uint32_t crc, uint8_t d)
-{
-	return crc32_tab[(crc ^ d) & 0xFF] ^ (crc >> 8);
-}
diff --git a/lib/libtcplay/crc32.h b/lib/libtcplay/crc32.h
deleted file mode 100644
index 2ad34ae9c4..0000000000
--- a/lib/libtcplay/crc32.h
+++ /dev/null
@@ -1,2 +0,0 @@
-uint32_t crc32(const void *buf, size_t size);
-uint32_t crc32_intermediate(uint32_t crc, uint8_t d);
diff --git a/lib/libtcplay/crypto-dev.c b/lib/libtcplay/crypto-dev.c
deleted file mode 100644
index b61fcc9eab..0000000000
--- a/lib/libtcplay/crypto-dev.c
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/types.h>
-#include <sys/param.h>
-#include <sys/ioctl.h>
-#include <sys/sysctl.h>
-#include <crypto/cryptodev.h>
-
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "tcplay.h"
-
-static
-int
-getallowsoft(void)
-{
-	int old;
-	size_t olen;
-
-	olen = sizeof(old);
-
-	if (sysctlbyname("kern.cryptodevallowsoft", &old, &olen, NULL, 0) < 0) {
-		perror("accessing sysctl kern.cryptodevallowsoft failed");
-	}
-
-	return old;
-}
-
-static
-void
-setallowsoft(int new)
-{
-	int old;
-	size_t olen, nlen;
-
-	olen = nlen = sizeof(new);
-
-	if (sysctlbyname("kern.cryptodevallowsoft", &old, &olen, &new, nlen) < 0) {
-		perror("accessing sysctl kern.cryptodevallowsoft failed");
-	}
-}
-
-static
-int
-get_cryptodev_cipher_id(struct tc_crypto_algo *cipher)
-{
-	if	(strcmp(cipher->name, "AES-128-XTS") == 0)
-		return CRYPTO_AES_XTS;
-	else if (strcmp(cipher->name, "AES-256-XTS") == 0)
-		return CRYPTO_AES_XTS;
-	else if (strcmp(cipher->name, "TWOFISH-128-XTS") == 0)
-		return CRYPTO_TWOFISH_XTS;
-	else if (strcmp(cipher->name, "TWOFISH-256-XTS") == 0)
-		return CRYPTO_TWOFISH_XTS;
-	else if (strcmp(cipher->name, "SERPENT-128-XTS") == 0)
-		return CRYPTO_SERPENT_XTS;
-	else if (strcmp(cipher->name, "SERPENT-256-XTS") == 0)
-		return CRYPTO_SERPENT_XTS;
-	else
-		return -1;
-}
-
-int
-syscrypt(struct tc_crypto_algo *cipher, unsigned char *key, size_t klen, unsigned char *iv,
-    unsigned char *in, unsigned char *out, size_t len, int do_encrypt)
-{
-	struct session_op session;
-	struct crypt_op cryp;
-	int cipher_id;
-	int cryptodev_fd = -1, fd = -1;
-
-	cipher_id = get_cryptodev_cipher_id(cipher);
-	if (cipher_id < 0) {
-		tc_log(1, "Cipher %s not found\n",
-		    cipher->name);
-		return ENOENT;
-	}
-
-	if ((cryptodev_fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
-		perror("Could not open /dev/crypto");
-		goto err;
-	}
-	if (ioctl(cryptodev_fd, CRIOGET, &fd) == -1) {
-		perror("CRIOGET failed");
-		goto err;
-	}
-	memset(&session, 0, sizeof(session));
-	session.cipher = cipher_id;
-	session.key = (caddr_t) key;
-	session.keylen = klen;
-	if (ioctl(fd, CIOCGSESSION, &session) == -1) {
-		perror("CIOCGSESSION failed");
-		goto err;
-	}
-	memset(&cryp, 0, sizeof(cryp));
-	cryp.ses = session.ses;
-	cryp.op = do_encrypt ? COP_ENCRYPT : COP_DECRYPT;
-	cryp.flags = 0;
-	cryp.len = len;
-	cryp.src = (caddr_t) in;
-	cryp.dst = (caddr_t) out;
-	cryp.iv = (caddr_t) iv;
-	cryp.mac = 0;
-	if (ioctl(fd, CIOCCRYPT, &cryp) == -1) {
-		perror("CIOCCRYPT failed");
-		goto err;
-	}
-	if (ioctl(fd, CIOCFSESSION, &session.ses) == -1) {
-		perror("CIOCFSESSION failed");
-		goto err;
-	}
-	close(fd);
-	close(cryptodev_fd);
-	return (0);
-
-err:
-	if (fd != -1)
-		close(fd);
-	if (cryptodev_fd != -1)
-		close(cryptodev_fd);
-	return (-1);
-}
-
-int
-tc_crypto_init(void)
-{
-	int allowed;
-
-	allowed = getallowsoft();
-	if (allowed == 0)
-		setallowsoft(1);
-
-	return 0;
-}
-
diff --git a/lib/libtcplay/crypto.c b/lib/libtcplay/crypto.c
deleted file mode 100644
index 321689e766..0000000000
--- a/lib/libtcplay/crypto.c
+++ /dev/null
@@ -1,267 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/types.h>
-#include <sys/param.h>
-#include <inttypes.h>
-
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "crc32.h"
-#include "tcplay.h"
-
-int
-tc_cipher_chain_populate_keys(struct tc_cipher_chain *cipher_chain,
-    unsigned char *key)
-{
-	int total_key_bytes, used_key_bytes;
-	struct tc_cipher_chain *dummy_chain;
-
-	/*
-	 * We need to determine the total key bytes as the key locations
-	 * depend on it.
-	 */
-	total_key_bytes = tc_cipher_chain_klen(cipher_chain);
-
-	/*
-	 * Now we need to get prepare the keys, as the keys are in
-	 * forward order with respect to the cipher cascade, but
-	 * the actual decryption is in reverse cipher cascade order.
-	 */
-	used_key_bytes = 0;
-	for (dummy_chain = cipher_chain;
-	    dummy_chain != NULL;
-	    dummy_chain = dummy_chain->next) {
-		dummy_chain->key = alloc_safe_mem(dummy_chain->cipher->klen);
-		if (dummy_chain->key == NULL) {
-			tc_log(1, "tc_decrypt: Could not allocate key "
-			    "memory\n");
-			return ENOMEM;
-		}
-
-		/* XXX: here we assume XTS operation! */
-		memcpy(dummy_chain->key,
-		    key + used_key_bytes/2,
-		    dummy_chain->cipher->klen/2);
-		memcpy(dummy_chain->key + dummy_chain->cipher->klen/2,
-		    key + (total_key_bytes/2) + used_key_bytes/2,
-		    dummy_chain->cipher->klen/2);
-
-		/* Remember how many key bytes we've seen */
-		used_key_bytes += dummy_chain->cipher->klen;
-	}
-
-	return 0;
-}
-
-int
-tc_cipher_chain_free_keys(struct tc_cipher_chain *cipher_chain)
-{
-	for (; cipher_chain != NULL; cipher_chain = cipher_chain->next) {
-		if (cipher_chain->key != NULL) {
-			free_safe_mem(cipher_chain->key);
-			cipher_chain->key = NULL;
-		}
-	}
-
-	return 0;
-}
-
-int
-tc_encrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
-    unsigned char *iv,
-    unsigned char *in, int in_len, unsigned char *out)
-{
-	struct tc_cipher_chain *chain_start;
-	int err;
-
-	chain_start = cipher_chain;
-
-	if ((err = tc_cipher_chain_populate_keys(cipher_chain, key)))
-		return err;
-
-#ifdef DEBUG
-	printf("tc_encrypt: starting chain\n");
-#endif
-
-	/*
-	 * Now process the actual decryption, in forward cascade order.
-	 */
-	for (;
-	    cipher_chain != NULL;
-	    cipher_chain = cipher_chain->next) {
-#ifdef DEBUG
-		printf("tc_encrypt: Currently using cipher %s\n",
-		    cipher_chain->cipher->name);
-#endif
-
-		err = syscrypt(cipher_chain->cipher, cipher_chain->key,
-		    cipher_chain->cipher->klen, iv, in, out, in_len, 1);
-
-		/* Deallocate this key, since we won't need it anymore */
-		free_safe_mem(cipher_chain->key);
-		cipher_chain->key = NULL;
-
-		if (err != 0) {
-			tc_cipher_chain_free_keys(chain_start);
-			return err;
-		}
-
-		/* Set next input buffer as current output buffer */
-		in = out;
-	}
-
-	tc_cipher_chain_free_keys(chain_start);
-
-	return 0;
-}
-
-int
-tc_decrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
-    unsigned char *iv,
-    unsigned char *in, int in_len, unsigned char *out)
-{
-	struct tc_cipher_chain *chain_start;
-	int err;
-
-	chain_start = cipher_chain;
-
-	if ((err = tc_cipher_chain_populate_keys(cipher_chain, key)))
-		return err;
-
-#ifdef DEBUG
-	printf("tc_decrypt: starting chain!\n");
-#endif
-
-	/*
-	 * Now process the actual decryption, in reverse cascade order; so
-	 * first find the last element in the chain.
-	 */
-	for (; cipher_chain->next != NULL; cipher_chain = cipher_chain->next)
-		;
-	for (;
-	    cipher_chain != NULL;
-	    cipher_chain = cipher_chain->prev) {
-#ifdef DEBUG
-		printf("tc_decrypt: Currently using cipher %s\n",
-		    cipher_chain->cipher->name);
-#endif
-
-		err = syscrypt(cipher_chain->cipher, cipher_chain->key,
-		    cipher_chain->cipher->klen, iv, in, out, in_len, 0);
-
-		/* Deallocate this key, since we won't need it anymore */
-		free_safe_mem(cipher_chain->key);
-		cipher_chain->key = NULL;
-
-		if (err != 0) {
-			tc_cipher_chain_free_keys(chain_start);
-			return err;
-		}
-
-		/* Set next input buffer as current output buffer */
-		in = out;
-	}
-
-	tc_cipher_chain_free_keys(chain_start);
-
-	return 0;
-}
-
-int
-apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
-    int nkeyfiles)
-{
-	int pl, k;
-	unsigned char *kpool;
-	unsigned char *kdata;
-	int kpool_idx;
-	size_t i, kdata_sz;
-	uint32_t crc;
-
-	if (pass_memsz < MAX_PASSSZ) {
-		tc_log(1, "Not enough memory for password manipulation\n");
-		return ENOMEM;
-	}
-
-	pl = strlen((char *)pass);
-	memset(pass+pl, 0, MAX_PASSSZ-pl);
-
-	if ((kpool = alloc_safe_mem(KPOOL_SZ)) == NULL) {
-		tc_log(1, "Error allocating memory for keyfile pool\n");
-		return ENOMEM;
-	}
-
-	memset(kpool, 0, KPOOL_SZ);
-
-	for (k = 0; k < nkeyfiles; k++) {
-#ifdef DEBUG
-		printf("Loading keyfile %s into kpool\n", keyfiles[k]);
-#endif
-		kpool_idx = 0;
-		crc = ~0U;
-		kdata_sz = MAX_KFILE_SZ;
-
-		if ((kdata = read_to_safe_mem(keyfiles[k], 0, &kdata_sz)) == NULL) {
-			tc_log(1, "Error reading keyfile %s content\n",
-			    keyfiles[k]);
-			free_safe_mem(kpool);
-			return EIO;
-		}
-
-		for (i = 0; i < kdata_sz; i++) {
-			crc = crc32_intermediate(crc, kdata[i]);
-
-			kpool[kpool_idx++] += (unsigned char)(crc >> 24);
-			kpool[kpool_idx++] += (unsigned char)(crc >> 16);
-			kpool[kpool_idx++] += (unsigned char)(crc >> 8);
-			kpool[kpool_idx++] += (unsigned char)(crc);
-
-			/* Wrap around */
-			if (kpool_idx == KPOOL_SZ)
-				kpool_idx = 0;
-		}
-
-		free_safe_mem(kdata);
-	}
-
-#ifdef DEBUG
-	printf("Applying kpool to passphrase\n");
-#endif
-	/* Apply keyfile pool to passphrase */
-	for (i = 0; i < KPOOL_SZ; i++)
-		pass[i] += kpool[i];
-
-	free_safe_mem(kpool);
-
-	return 0;
-}
diff --git a/lib/libtcplay/generic_xts.c b/lib/libtcplay/generic_xts.c
deleted file mode 100644
index 82020406f3..0000000000
--- a/lib/libtcplay/generic_xts.c
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- * Copyright (C) 2008, Damien Miller
- * Copyright (C) 2011, Alex Hornung
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all copies of any software which is or includes a copy or
- * modification of this software.
- * You may use this code under the GNU public license if you so wish. Please
- * contribute changes back to the authors under this freer than GPL license
- * so that we may further the use of strong encryption without limitations to
- * all.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <inttypes.h>
-
-#include "tcplay.h"
-#include "generic_xts.h"
-
-
-
-static int
-xts_reinit(struct xts_ctx *ctx, u_int64_t blocknum)
-{
-	u_int i;
-
-	/*
-	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
-	 * of a 64-bit block number which we allow to be passed in directly.
-	 */
-	for (i = 0; i < XTS_IVSIZE; i++) {
-		ctx->tweak[i] = blocknum & 0xff;
-		blocknum >>= 8;
-	}
-	/* Last 64 bits of IV are always zero */
-	bzero(ctx->tweak + XTS_IVSIZE, XTS_IVSIZE);
-
-	return ctx->encrypt_fn(ctx->ctx2, ctx->blk_sz, ctx->tweak, ctx->tweak);
-}
-
-static int
-xts_crypt(struct xts_ctx *ctx, u_int8_t *data, u_int do_encrypt)
-{
-	u_int8_t block[XTS_MAX_BLOCKSIZE];
-	u_int i, carry_in, carry_out;
-	int err;
-
-	for (i = 0; i < ctx->blk_sz; i++)
-		block[i] = data[i] ^ ctx->tweak[i];
-
-	if (do_encrypt)
-		err = ctx->encrypt_fn(ctx->ctx1, ctx->blk_sz, block, data);
-	else
-		err = ctx->decrypt_fn(ctx->ctx1, ctx->blk_sz, block, data);
-
-	if (err)
-		goto out;
-
-	for (i = 0; i < ctx->blk_sz; i++)
-		data[i] ^= ctx->tweak[i];
-
-	/* Exponentiate tweak */
-	carry_in = 0;
-	for (i = 0; i < ctx->blk_sz; i++) {
-		carry_out = ctx->tweak[i] & 0x80;
-		ctx->tweak[i] = (ctx->tweak[i] << 1) | (carry_in ? 1 : 0);
-		carry_in = carry_out;
-	}
-	if (carry_in)
-		ctx->tweak[0] ^= XTS_ALPHA;
-
-out:
-	bzero(block, sizeof(block));
-	return err;
-}
-
-int
-xts_init(struct xts_ctx *ctx, void *arg1, void *arg2, set_key_fn _set_key_fn,
-    zero_key_fn _zero_key_fn, encrypt_decrypt_fn _encrypt_fn,
-    encrypt_decrypt_fn _decrypt_fn, u_int blk_sz, u_int8_t *key, int len)
-{
-	int err;
-
-	if (len != 32 && len != 64)
-		return -1;
-
-	ctx->blk_sz = blk_sz;
-	ctx->encrypt_fn = _encrypt_fn;
-	ctx->decrypt_fn = _decrypt_fn;
-	ctx->set_key_fn = _set_key_fn;
-	ctx->zero_key_fn = _zero_key_fn;
-
-	err = ctx->set_key_fn(&ctx->ctx1, arg1, arg2, key, len * 4);
-	if (err)
-		return -1;
-
-	err = ctx->set_key_fn(&ctx->ctx2, arg1, arg2, key + (len / 2),
-	    len * 4);
-	if (err) {
-		ctx->zero_key_fn(&ctx->ctx1);
-		return -1;
-	}
-
-	return 0;
-}
-
-int
-xts_encrypt(struct xts_ctx *ctx, u_int8_t *data, size_t len, uint8_t *iv)
-{
-	uint64_t sector = *((uint64_t *)iv);
-	int err;
-
-	if ((len % ctx->blk_sz) != 0)
-		return -1;
-
-	err = xts_reinit(ctx, sector);
-	if (err)
-		return err;
-
-	while (len > 0) {
-		err = xts_crypt(ctx, data, 1);
-		if (err)
-			return -1;
-
-		data += ctx->blk_sz;
-		len -= ctx->blk_sz;
-	}
-
-	return err;
-}
-
-int
-xts_decrypt(struct xts_ctx *ctx, u_int8_t *data, size_t len, uint8_t *iv)
-{
-	uint64_t sector = *((uint64_t *)iv);
-	int err;
-
-	if ((len % ctx->blk_sz) != 0)
-		return -1;
-
-	err = xts_reinit(ctx, sector);
-	if (err)
-		return err;
-
-	while (len > 0) {
-		err = xts_crypt(ctx, data, 0);
-		if (err)
-			return -1;
-
-		data += ctx->blk_sz;
-		len -= ctx->blk_sz;
-	}
-
-	return err;
-}
-
-int
-xts_uninit(struct xts_ctx *ctx)
-{
-	ctx->zero_key_fn(&ctx->ctx1);
-	ctx->zero_key_fn(&ctx->ctx2);
-
-	return 0;
-}
-
diff --git a/lib/libtcplay/generic_xts.h b/lib/libtcplay/generic_xts.h
deleted file mode 100644
index 1c31450b80..0000000000
--- a/lib/libtcplay/generic_xts.h
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (C) 2008, Damien Miller
- * Copyright (C) 2011, Alex Hornung
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all copies of any software which is or includes a copy or
- * modification of this software.
- * You may use this code under the GNU public license if you so wish. Please
- * contribute changes back to the authors under this freer than GPL license
- * so that we may further the use of strong encryption without limitations to
- * all.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- */
-
-#include <inttypes.h>
-#define XTS_MAX_BLOCKSIZE	16
-#define XTS_IVSIZE		8
-#define XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
-
-typedef int (*encrypt_decrypt_fn)(void *, size_t, const uint8_t *, uint8_t *);
-typedef int (*set_key_fn)(void **, void *, void *, const uint8_t *, int);
-typedef int (*zero_key_fn)(void **);
-
-
-struct xts_ctx {
-	encrypt_decrypt_fn	encrypt_fn;
-	encrypt_decrypt_fn	decrypt_fn;
-	set_key_fn		set_key_fn;
-	zero_key_fn		zero_key_fn;
-
-	void		*ctx1;
-	void		*ctx2;
-	uint8_t		tweak[XTS_MAX_BLOCKSIZE];
-	uint32_t	blk_sz;
-};
-
-int xts_init(struct xts_ctx *ctxp, void *arg1, void *arg2, set_key_fn set_key_fn,
-    zero_key_fn zero_key_fn, encrypt_decrypt_fn encrypt_fn,
-    encrypt_decrypt_fn decrypt_fn, uint32_t blk_sz, uint8_t *key, int len);
-int xts_encrypt(struct xts_ctx *ctx, uint8_t *data, size_t len, uint8_t *iv);
-int xts_decrypt(struct xts_ctx *ctx, uint8_t *data, size_t len, uint8_t *iv);
-int xts_uninit(struct xts_ctx *ctxp);
-
diff --git a/lib/libtcplay/hdr.c b/lib/libtcplay/hdr.c
deleted file mode 100644
index b3d2f782bd..0000000000
--- a/lib/libtcplay/hdr.c
+++ /dev/null
@@ -1,432 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/types.h>
-
-#if defined(__DragonFly__)
-#include <sys/endian.h>
-#elif defined(__linux__)
-#include <endian.h>
-#endif
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <inttypes.h>
-#include <string.h>
-
-#include "crc32.h"
-#include "tcplay.h"
-
-/* Endianess macros */
-#define BE_TO_HOST(n, v) v = be ## n ## toh(v)
-#define LE_TO_HOST(n, v) v = le ## n ## toh(v)
-#define HOST_TO_BE(n, v) v = htobe ## n (v)
-#define HOST_TO_LE(n, v) v = htole ## n (v)
-
-struct sig_hdr_cfg {
-	const char	*sig;
-	uint16_t	min_ver;
-};
-
-struct sig_hdr_cfg sig_hdr_cfgs[] = {
-	{ TC_SIG,	0x0007 },
-	{ VC_SIG,	0x0b01 },
-	{ NULL,		0x0000 }
-};
-
-static
-const
-struct sig_hdr_cfg *hdr_cfg_from_sig(const char *sig)
-{
-	const struct sig_hdr_cfg *cfg;
-
-	for (cfg = &sig_hdr_cfgs[0]; cfg->sig != NULL; cfg++) {
-		if (strcmp(cfg->sig, sig) == 0)
-			return cfg;
-	}
-
-	return NULL;
-}
-
-struct tchdr_dec *
-decrypt_hdr(struct tchdr_enc *ehdr, struct tc_cipher_chain *cipher_chain,
-    unsigned char *key)
-{
-	struct tchdr_dec *dhdr;
-	unsigned char iv[128];
-	int error;
-
-	if ((dhdr = alloc_safe_mem(sizeof(struct tchdr_dec))) == NULL) {
-		tc_log(1, "Error allocating safe tchdr_dec memory\n");
-		return NULL;
-	}
-
-	memset(iv, 0, sizeof(iv));
-
-	error = tc_decrypt(cipher_chain, key, iv, ehdr->enc,
-	    sizeof(struct tchdr_dec), (unsigned char *)dhdr);
-	if (error) {
-		tc_log(1, "Header decryption failed\n");
-		free_safe_mem(dhdr);
-		return NULL;
-	}
-
-	BE_TO_HOST(16, dhdr->tc_ver);
-	LE_TO_HOST(16, dhdr->tc_min_ver);
-	BE_TO_HOST(32, dhdr->crc_keys);
-	BE_TO_HOST(64, dhdr->vol_ctime);
-	BE_TO_HOST(64, dhdr->hdr_ctime);
-	BE_TO_HOST(64, dhdr->sz_hidvol);
-	BE_TO_HOST(64, dhdr->sz_vol);
-	BE_TO_HOST(64, dhdr->off_mk_scope);
-	BE_TO_HOST(64, dhdr->sz_mk_scope);
-	BE_TO_HOST(32, dhdr->flags);
-	BE_TO_HOST(32, dhdr->sec_sz);
-	BE_TO_HOST(32, dhdr->crc_dhdr);
-
-	return dhdr;
-}
-
-int
-verify_hdr(struct tchdr_dec *hdr, struct pbkdf_prf_algo *prf_algo)
-{
-	uint32_t crc;
-
-	if (memcmp(hdr->tc_str, prf_algo->sig, sizeof(hdr->tc_str)) != 0) {
-#ifdef DEBUG
-		fprintf(stderr, "Signature mismatch\n");
-#endif
-		return 0;
-	}
-
-	crc = crc32((void *)&hdr->keys, 256);
-	if (crc != hdr->crc_keys) {
-#ifdef DEBUG
-		fprintf(stderr, "CRC32 mismatch (crc_keys)\n");
-#endif
-		return 0;
-	}
-
-	switch(hdr->tc_ver) {
-	case 1:
-	case 2:
-		/* Unsupported header version */
-		tc_log(1, "Header version %d unsupported\n", hdr->tc_ver);
-		return 0;
-
-	case 3:
-	case 4:
-		hdr->sec_sz = 512;
-		break;
-	}
-
-	return 1;
-}
-
-struct tchdr_enc *
-create_hdr(unsigned char *pass, int passlen, struct pbkdf_prf_algo *prf_algo,
-    struct tc_cipher_chain *cipher_chain, size_t sec_sz,
-    disksz_t total_blocks __unused,
-    off_t offset, disksz_t blocks, int hidden, int weak, struct tchdr_enc **backup_hdr)
-{
-	struct tchdr_enc *ehdr, *ehdr_backup;
-	struct tchdr_dec *dhdr;
-	unsigned char *key, *key_backup;
-	unsigned char iv[128];
-	const struct sig_hdr_cfg *hdr_cfg;
-	int error;
-
-	key = key_backup = NULL;
-	dhdr = NULL;
-	ehdr = ehdr_backup = NULL;
-
-	if (backup_hdr != NULL)
-		*backup_hdr = NULL;
-
-	if ((dhdr = (struct tchdr_dec *)alloc_safe_mem(sizeof(*dhdr))) == NULL) {
-		tc_log(1, "could not allocate safe dhdr memory\n");
-		goto error;
-	}
-
-	if ((ehdr = (struct tchdr_enc *)alloc_safe_mem(sizeof(*ehdr))) == NULL) {
-		tc_log(1, "could not allocate safe ehdr memory\n");
-		goto error;
-	}
-
-	if ((ehdr_backup = (struct tchdr_enc *)alloc_safe_mem
-	    (sizeof(*ehdr_backup))) == NULL) {
-		tc_log(1, "could not allocate safe ehdr_backup memory\n");
-		goto error;
-	}
-
-	if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
-		tc_log(1, "could not allocate safe key memory\n");
-		goto error;
-	}
-
-	if ((key_backup = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
-		tc_log(1, "could not allocate safe backup key memory\n");
-		goto error;
-	}
-
-	if ((error = get_random(ehdr->salt, sizeof(ehdr->salt), weak)) != 0) {
-		tc_log(1, "could not get salt\n");
-		goto error;
-	}
-
-	if ((error = get_random(ehdr_backup->salt, sizeof(ehdr_backup->salt), weak))
-	    != 0) {
-		tc_log(1, "could not get salt for backup header\n");
-		goto error;
-	}
-
-	error = pbkdf2(prf_algo, (char *)pass, passlen,
-	    ehdr->salt, sizeof(ehdr->salt),
-	    MAX_KEYSZ, key);
-	if (error) {
-		tc_log(1, "could not derive key\n");
-		goto error;
-	}
-
-	error = pbkdf2(prf_algo, (char *)pass, passlen,
-	    ehdr_backup->salt, sizeof(ehdr_backup->salt),
-	    MAX_KEYSZ, key_backup);
-	if (error) {
-		tc_log(1, "could not derive backup key\n");
-		goto error;
-	}
-
-	memset(dhdr, 0, sizeof(*dhdr));
-
-	if ((error = get_random(dhdr->keys, sizeof(dhdr->keys), weak)) != 0) {
-		tc_log(1, "could not get key random bits\n");
-		goto error;
-	}
-
-	if ((hdr_cfg = hdr_cfg_from_sig(prf_algo->sig)) == NULL) {
-		tc_log(1, "could not find internal header configuration\n");
-		goto error;
-	}
-
-	memcpy(dhdr->tc_str, prf_algo->sig, 4);
-	dhdr->tc_ver = 5;
-	dhdr->tc_min_ver = hdr_cfg->min_ver;
-	dhdr->crc_keys = crc32((void *)&dhdr->keys, 256);
-	dhdr->sz_vol = blocks * sec_sz;
-	if (hidden)
-		dhdr->sz_hidvol = dhdr->sz_vol;
-	dhdr->off_mk_scope = offset * sec_sz;
-	dhdr->sz_mk_scope = blocks * sec_sz;
-	dhdr->sec_sz = sec_sz;
-	dhdr->flags = 0;
-
-	HOST_TO_BE(16, dhdr->tc_ver);
-	HOST_TO_LE(16, dhdr->tc_min_ver);
-	HOST_TO_BE(32, dhdr->crc_keys);
-	HOST_TO_BE(64, dhdr->sz_vol);
-	HOST_TO_BE(64, dhdr->sz_hidvol);
-	HOST_TO_BE(64, dhdr->off_mk_scope);
-	HOST_TO_BE(64, dhdr->sz_mk_scope);
-	HOST_TO_BE(32, dhdr->sec_sz);
-	HOST_TO_BE(32, dhdr->flags);
-
-	dhdr->crc_dhdr = crc32((void *)dhdr, 188);
-	HOST_TO_BE(32, dhdr->crc_dhdr);
-
-	memset(iv, 0, sizeof(iv));
-	error = tc_encrypt(cipher_chain, key, iv, (unsigned char *)dhdr,
-	    sizeof(struct tchdr_dec), ehdr->enc);
-	if (error) {
-		tc_log(1, "Header encryption failed\n");
-		goto error;
-	}
-
-	memset(iv, 0, sizeof(iv));
-	error = tc_encrypt(cipher_chain, key_backup, iv,
-	    (unsigned char *)dhdr,
-	    sizeof(struct tchdr_dec), ehdr_backup->enc);
-	if (error) {
-		tc_log(1, "Backup header encryption failed\n");
-		goto error;
-	}
-
-	free_safe_mem(key);
-	free_safe_mem(key_backup);
-	free_safe_mem(dhdr);
-
-	if (backup_hdr != NULL)
-		*backup_hdr = ehdr_backup;
-	else
-		free_safe_mem(ehdr_backup);
-
-	return ehdr;
-	/* NOT REACHED */
-
-error:
-	if (key)
-		free_safe_mem(key);
-	if (key_backup)
-		free_safe_mem(key_backup);
-	if (dhdr)
-		free_safe_mem(dhdr);
-	if (ehdr)
-		free_safe_mem(ehdr);
-	if (ehdr_backup)
-		free_safe_mem(ehdr_backup);
-
-	return NULL;
-}
-
-struct tchdr_enc *copy_reencrypt_hdr(unsigned char *pass, int passlen,
-    struct pbkdf_prf_algo *prf_algo, int weak, struct tcplay_info *info,
-    struct tchdr_enc **backup_hdr)
-{
-	struct tchdr_enc *ehdr, *ehdr_backup;
-	unsigned char *key, *key_backup;
-	unsigned char iv[128];
-	const struct sig_hdr_cfg *hdr_cfg;
-	int error;
-
-	key = key_backup = NULL;
-	ehdr = ehdr_backup = NULL;
-
-	/* By default stick to current PRF algo */
-	if (prf_algo == NULL)
-		prf_algo = info->pbkdf_prf;
-
-	if ((ehdr = (struct tchdr_enc *)alloc_safe_mem(sizeof(*ehdr))) == NULL) {
-		tc_log(1, "could not allocate safe ehdr memory\n");
-		goto error;
-	}
-
-	if ((ehdr_backup = (struct tchdr_enc *)alloc_safe_mem
-	    (sizeof(*ehdr_backup))) == NULL) {
-		tc_log(1, "could not allocate safe ehdr_backup memory\n");
-		goto error;
-	}
-
-	if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
-		tc_log(1, "could not allocate safe key memory\n");
-		goto error;
-	}
-
-	if ((key_backup = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
-		tc_log(1, "could not allocate safe backup key memory\n");
-		goto error;
-	}
-
-	if ((error = get_random(ehdr->salt, sizeof(ehdr->salt), weak)) != 0) {
-		tc_log(1, "could not get salt\n");
-		goto error;
-	}
-
-	if ((error = get_random(ehdr_backup->salt, sizeof(ehdr_backup->salt), weak))
-	    != 0) {
-		tc_log(1, "could not get salt for backup header\n");
-		goto error;
-	}
-
-	error = pbkdf2(prf_algo, (char *)pass, passlen,
-	    ehdr->salt, sizeof(ehdr->salt),
-	    MAX_KEYSZ, key);
-	if (error) {
-		tc_log(1, "could not derive key\n");
-		goto error;
-	}
-
-	error = pbkdf2(prf_algo, (char *)pass, passlen,
-	    ehdr_backup->salt, sizeof(ehdr_backup->salt),
-	    MAX_KEYSZ, key_backup);
-	if (error) {
-		tc_log(1, "could not derive backup key\n");
-		goto error;
-	}
-
-	if ((hdr_cfg = hdr_cfg_from_sig(prf_algo->sig)) == NULL) {
-		tc_log(1, "could not find internal header configuration\n");
-		goto error;
-	}
-
-	/* Update signature and min_ver depending on selected PBKDF2 PRF algo */
-	memcpy(info->hdr->tc_str, prf_algo->sig, 4);
-	info->hdr->tc_min_ver = hdr_cfg->min_ver;
-
-	HOST_TO_BE(16, info->hdr->tc_ver);
-	HOST_TO_LE(16, info->hdr->tc_min_ver);
-	HOST_TO_BE(32, info->hdr->crc_keys);
-	HOST_TO_BE(64, info->hdr->vol_ctime);
-	HOST_TO_BE(64, info->hdr->hdr_ctime);
-	HOST_TO_BE(64, info->hdr->sz_vol);
-	HOST_TO_BE(64, info->hdr->sz_hidvol);
-	HOST_TO_BE(64, info->hdr->off_mk_scope);
-	HOST_TO_BE(64, info->hdr->sz_mk_scope);
-	HOST_TO_BE(32, info->hdr->sec_sz);
-	HOST_TO_BE(32, info->hdr->flags);
-	HOST_TO_BE(32, info->hdr->crc_dhdr);
-
-	memset(iv, 0, sizeof(iv));
-	error = tc_encrypt(info->cipher_chain, key, iv,
-	    (unsigned char *)info->hdr, sizeof(struct tchdr_dec), ehdr->enc);
-	if (error) {
-		tc_log(1, "Header encryption failed\n");
-		goto error;
-	}
-
-	memset(iv, 0, sizeof(iv));
-	error = tc_encrypt(info->cipher_chain, key_backup, iv,
-	    (unsigned char *)info->hdr,
-	    sizeof(struct tchdr_dec), ehdr_backup->enc);
-	if (error) {
-		tc_log(1, "Backup header encryption failed\n");
-		goto error;
-	}
-
-	free_safe_mem(key);
-	free_safe_mem(key_backup);
-
-	if (backup_hdr != NULL)
-		*backup_hdr = ehdr_backup;
-	else
-		free_safe_mem(ehdr_backup);
-
-	return ehdr;
-	/* NOT REACHED */
-
-error:
-	if (key)
-		free_safe_mem(key);
-	if (key_backup)
-		free_safe_mem(key_backup);
-	if (ehdr)
-		free_safe_mem(ehdr);
-	if (ehdr_backup)
-		free_safe_mem(ehdr_backup);
-
-	return NULL;
-}
diff --git a/lib/libtcplay/humanize.c b/lib/libtcplay/humanize.c
deleted file mode 100644
index 876ace41eb..0000000000
--- a/lib/libtcplay/humanize.c
+++ /dev/null
@@ -1,199 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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 <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <inttypes.h>
-#include <ctype.h>
-#include <errno.h>
-
-#include "humanize.h"
-
-static const char prefixes[] = " KMGTPE";
-int
-_humanize_number(char *buf, size_t bufsz, uint64_t num)
-{
-	const char *prefixp;
-	int ret;
-	uint64_t i, d;
-
-	prefixp = prefixes;
-	i = num;
-	d = 0;
-
-	while ((i > 1024) && (*prefixp != '\0')) {
-		d = (i % 1024)/10;
-		i /= 1024;
-		++prefixp;
-	}
-
-	if (d > 0)
-		ret = snprintf(buf, bufsz, "%"PRIu64".%"PRIu64" %c",
-		    i, d, *prefixp);
-	else
-		ret = snprintf(buf, bufsz, "%"PRIu64" %c", i, *prefixp);
-
-
-	if ((ret < 0) || ((size_t)ret >= bufsz)) {
-		errno = ENOMEM;
-		return -1;
-	} else {
-		return 0;
-	}
-}
-
-int
-_dehumanize_number(const char *buf, uint64_t *dest)
-{
-	char *endptr;
-	uint64_t n, n_check, d;
-	uint64_t multiplier;
-	size_t len;
-
-	if (*buf == '\0') {
-		errno = EINVAL;
-		return -1;
-	}
-
-	len = strlen(buf);
-	if (tolower(buf[len-1]) == 'b')
-		--len;
-
-	multiplier = 1;
-
-	switch (tolower(buf[len-1])) {
-	case 'y':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 'z':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 'e':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 'p':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 't':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 'g':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 'm':
-		multiplier *= 1024;
-		/* FALLTHROUGH */
-	case 'k':
-		multiplier *= 1024;
-		break;
-	default:
-		/*
-		 * only set error if string ends in a character that
-		 * is not a valid unit.
-		 */
-		if (isalpha(buf[len-1])) {
-			errno = EINVAL;
-			return -1;
-		}
-	}
-
-	d = 0;
-	n = n_check = strtoull(buf, &endptr, 10);
-	if (endptr) {
-		if ((*endptr != '.') && (*endptr != '\0') &&
-		    (*endptr != ' ') && (endptr != &buf[len-1])) {
-			errno = EINVAL;
-			return -1;
-		}
-
-		if (*endptr == '.') {
-			d = strtoull(endptr+1, &endptr, 10);
-			if (endptr && (*endptr != '\0') &&
-			    (*endptr != ' ') &&
-			    (endptr != &buf[len-1])) {
-				errno = EINVAL;
-				return -1;
-			}
-		}
-	}
-
-	if (d != 0) {
-		while (d < 100)
-			d *= 10;
-
-		while (d > 1000)
-			d /= 10;
-	}
-
-	d *= (multiplier/1024);
-	n *= multiplier;
-
-	if ((uint64_t)(n/multiplier) != n_check) {
-		errno = ERANGE;
-		return -1;
-	}
-
-	n += d;
-	*dest = n;
-
-	return 0;
-}
-
-#ifdef __TEST_HUMANIZE__
-
-#include <assert.h>
-int main(int argc, char *argv[])
-{
-	char buf[1024];
-	uint64_t n, out;
-	int err;
-
-	if (argc < 3)
-		return -1;
-
-	n = strtoull(argv[1], NULL, 10);
-
-	err = _humanize_number(buf, 1024, n);
-	assert(err == 0);
-
-	err = _dehumanize_number(buf, &out);
-	assert(err == 0);
-
-	printf("Converting: %"PRIu64" => %s => %"PRIu64"\n", n, buf, out);
-
-	err = _dehumanize_number(argv[2], &out);
-	assert (err == 0);
-
-	printf("Converting: %s => %"PRIu64"\n", argv[2], out);
-
-	return 0;
-}
-
-#endif
diff --git a/lib/libtcplay/humanize.h b/lib/libtcplay/humanize.h
deleted file mode 100644
index 6a45b1d008..0000000000
--- a/lib/libtcplay/humanize.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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.
- */
-
-int _humanize_number(char *buf, size_t bufsz, uint64_t num);
-int _dehumanize_number(const char *buf, uint64_t *dest);
-
diff --git a/lib/libtcplay/io.c b/lib/libtcplay/io.c
deleted file mode 100644
index 7370f0676b..0000000000
--- a/lib/libtcplay/io.c
+++ /dev/null
@@ -1,466 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/param.h>
-#if defined(__DragonFly__)
-#include <sys/diskslice.h>
-#elif defined(__linux__)
-#include <linux/fs.h>
-#include <sys/ioctl.h>
-#endif
-#include <sys/stat.h>
-#include <sys/uio.h>
-#include <sys/select.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <signal.h>
-
-#include "tcplay.h"
-
-void *
-read_to_safe_mem(const char *file, off_t offset, size_t *sz)
-{
-	void *mem = NULL;
-	ssize_t r = 0;
-	int fd;
-
-	if ((fd = open(file, O_RDONLY)) < 0) {
-		tc_log(1, "Error opening file %s\n", file);
-		return NULL;
-	}
-
-	if ((mem = alloc_safe_mem(*sz)) == NULL) {
-		tc_log(1, "Error allocating memory\n");
-		goto out;
-	}
-
-	if ((lseek(fd, offset, (offset >= 0) ? SEEK_SET : SEEK_END) < 0)) {
-		tc_log(1, "Error seeking on file %s\n", file);
-		goto m_err;
-	}
-
-	if ((r = read(fd, mem, *sz)) <= 0) {
-		tc_log(1, "Error reading from file %s\n", file);
-		goto m_err;
-	}
-
-out:
-	*sz = r;
-	close(fd);
-	return mem;
-	/* NOT REACHED */
-
-m_err:
-	free_safe_mem(mem);
-	close(fd);
-	return NULL;
-}
-
-static size_t get_random_total_bytes = 0;
-static size_t get_random_read_bytes = 0;
-
-
-float
-get_random_read_progress(void)
-{
-	return (get_random_total_bytes == 0) ? 0.0 :
-	    (1.0 * get_random_read_bytes) /
-	    (1.0 * get_random_total_bytes) * 100.0;
-}
-
-static
-void
-get_random_summary(void)
-{
-	float pct_done = get_random_read_progress();
-
-	tc_log(0, "Gathering true randomness, %.0f%% done.\n", pct_done);
-}
-
-int
-get_random(unsigned char *buf, size_t len, int weak)
-{
-	int fd;
-	ssize_t r;
-	size_t rd = 0;
-	size_t sz;
-	struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 }; /* 10 ms */
-
-
-	if ((fd = open((weak) ? "/dev/urandom" : "/dev/random", O_RDONLY)) < 0) {
-		tc_log(1, "Error opening /dev/random\n");
-		return -1;
-	}
-
-	summary_fn = get_random_summary;
-	get_random_total_bytes = len;
-	tc_internal_state = STATE_GET_RANDOM;
-
-	/* Get random data in 16-byte chunks */
-	sz = 16;
-	while (rd < len) {
-		get_random_read_bytes = rd;
-
-		if ((len - rd) < sz)
-			sz = (len - rd);
-
-		if ((r = read(fd, buf+rd, sz)) < 0) {
-			tc_log(1, "Error reading from /dev/random(%d): %s\n",
-			    fd, strerror(errno));
-			close(fd);
-			summary_fn = NULL;
-			tc_internal_state = STATE_UNKNOWN;
-			return -1;
-		}
-		rd += r;
-		nanosleep(&ts, NULL);
-	}
-
-	close(fd);
-	summary_fn = NULL;
-
-	tc_internal_state = STATE_UNKNOWN;
-	return 0;
-}
-
-static disksz_t secure_erase_total_bytes = 0;
-static disksz_t secure_erase_erased_bytes = 0;
-
-float
-get_secure_erase_progress(void)
-{
-	return (secure_erase_total_bytes == 0) ? 0.0 :
-	    (1.0 * secure_erase_erased_bytes) /
-	    (1.0 * secure_erase_total_bytes) * 100.0;
-}
-
-static
-void
-secure_erase_summary(void)
-{
-	float pct_done = get_secure_erase_progress();
-	tc_log(0, "Securely erasing, %.0f%% done.\n", pct_done);
-}
-
-int
-secure_erase(const char *dev, disksz_t bytes, size_t blksz)
-{
-	size_t erased = 0;
-	int fd_rand, fd;
-	char buf[ERASE_BUFFER_SIZE];
-	ssize_t r, w;
-	size_t sz;
-
-	if (blksz > MAX_BLKSZ) {
-		tc_log(1, "blksz > MAX_BLKSZ\n");
-		return -1;
-	}
-
-	if ((fd_rand = open("/dev/urandom", O_RDONLY)) < 0) {
-		tc_log(1, "Error opening /dev/urandom\n");
-		return -1;
-	}
-
-	if ((fd = open(dev, O_WRONLY)) < 0) {
-		close(fd_rand);
-		tc_log(1, "Error opening %s\n", dev);
-		return -1;
-	}
-
-	summary_fn = secure_erase_summary;
-	secure_erase_total_bytes = bytes;
-
-	tc_internal_state = STATE_ERASE;
-
-	sz = ERASE_BUFFER_SIZE;
-	while (erased < bytes) {
-		secure_erase_erased_bytes = erased;
-		/* Switch to block size when not much is remaining */
-		if ((bytes - erased) <= ERASE_BUFFER_SIZE)
-			sz = blksz;
-
-		if ((r = read(fd_rand, buf, sz)) < 0) {
-			tc_log(1, "Error reading from /dev/urandom\n");
-			close(fd);
-			close(fd_rand);
-			summary_fn = NULL;
-			tc_internal_state = STATE_UNKNOWN;
-			return -1;
-		}
-
-		if (r < (ssize_t)blksz)
-			continue;
-
-		if ((w = write(fd, buf, r)) < 0) {
-			tc_log(1, "Error writing to %s\n", dev);
-			close(fd);
-			close(fd_rand);
-			summary_fn = NULL;
-			tc_internal_state = STATE_UNKNOWN;
-			return -1;
-		}
-
-		erased += (size_t)w;
-	}
-
-	close(fd);
-	close(fd_rand);
-
-	summary_fn = NULL;
-
-	tc_internal_state = STATE_UNKNOWN;
-	return 0;
-}
-
-#if defined(__DragonFly__)
-int
-get_disk_info(const char *dev, disksz_t *blocks, size_t *bsize)
-{
-	struct partinfo pinfo;
-	int fd;
-
-	if ((fd = open(dev, O_RDONLY)) < 0) {
-		tc_log(1, "Error opening %s\n", dev);
-		return -1;
-	}
-
-	memset(&pinfo, 0, sizeof(struct partinfo));
-
-	if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
-		close(fd);
-		return -1;
-	}
-
-	*blocks = (disksz_t)pinfo.media_blocks;
-	*bsize = pinfo.media_blksize;
-
-	close(fd);
-	return 0;
-}
-#elif defined(__linux__)
-int
-get_disk_info(const char *dev, disksz_t *blocks, size_t *bsize)
-{
-	uint64_t nbytes;
-	int blocksz;
-	int fd;
-
-	if ((fd = open(dev, O_RDONLY)) < 0) {
-		tc_log(1, "Error opening %s\n", dev);
-		return -1;
-	}
-
-	if ((ioctl(fd, BLKSSZGET, &blocksz)) < 0) {
-		close(fd);
-		return -1;
-	}
-
-	if ((ioctl(fd, BLKGETSIZE64, &nbytes)) < 0) {
-		close(fd);
-		return -1;
-	}
-
-	*blocks = (disksz_t)(nbytes / blocksz);
-	*bsize = (size_t)(blocksz);
-
-	close(fd);
-	return 0;
-}
-#endif
-
-int
-write_to_disk(const char *dev, off_t offset, size_t blksz, void *mem,
-    size_t bytes)
-{
-	unsigned char *mem_buf = NULL;
-	ssize_t w;
-	size_t sz;
-	off_t internal_off;
-	int fd;
-
-	/* Align to block sizes */
-	internal_off = offset % blksz;
-#ifdef DEBUG
-	printf("offset: %"PRIu64", internal offset: %"PRIu64"\n",
-	    (uint64_t)offset, (uint64_t)internal_off);
-#endif
-	offset = rounddown(offset, blksz);
-
-	if ((internal_off + bytes) > blksz) {
-		tc_log(1, "This should never happen: internal_off + bytes > "
-		    "blksz (write_to_disk)\n");
-		return -1;
-	}
-
-	if ((bytes < blksz) || (internal_off != 0)) {
-		sz = blksz;
-		if ((mem_buf = read_to_safe_mem(dev, offset, &sz)) == NULL) {
-			tc_log(1, "Error buffering data on "
-			    "write_to_disk(%s)\n", dev);
-			return -1;
-		}
-
-		memcpy(mem_buf + internal_off, mem, bytes);
-	}
-
-	if ((fd = open(dev, O_WRONLY)) < 0) {
-		tc_log(1, "Error opening device %s\n", dev);
-		return -1;
-	}
-
-	if ((lseek(fd, offset, (offset >= 0) ? SEEK_SET : SEEK_END) < 0)) {
-		tc_log(1, "Error seeking on device %s\n", dev);
-		close(fd);
-		return -1;
-	}
-
-	if ((w = write(fd, (mem_buf != NULL) ? mem_buf : mem, bytes)) <= 0) {
-		tc_log(1, "Error writing to device %s\n", dev);
-		close(fd);
-		return -1;
-	}
-
-	close(fd);
-
-	if (mem_buf != NULL)
-		free_safe_mem(mem_buf);
-	return 0;
-}
-
-
-int
-write_to_file(const char *file, void *mem, size_t bytes)
-{
-	int fd;
-	ssize_t w;
-
-	if ((fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
-		tc_log(1, "Error opening file %s\n", file);
-		return -1;
-	}
-
-	if ((w = write(fd, mem, bytes)) < 0) {
-		tc_log(1, "Error writing to file %s\n", file);
-		close(fd);
-		return -1;
-	}
-
-	close(fd);
-	return 0;
-}
-
-
-static struct termios termios_old;
-static int tty_fd;
-
-static void sigint_termios(int sa)
-{
-	tcsetattr(tty_fd, TCSAFLUSH, &termios_old);
-	exit(sa);
-}
-
-int
-read_passphrase(const char *prompt, char *pass, size_t passlen, size_t bufsz, time_t timeout)
-{
-	struct termios termios_new;
-	struct timeval to;
-	fd_set fds;
-	ssize_t n;
-	int fd = STDIN_FILENO, r = 0, nready;
-	struct sigaction act, old_act;
-	int is_tty = isatty(fd);
-
-	if (is_tty == 0)
-		errno = 0;
-
-	memset(pass, 0, bufsz);
-
-	printf("%s", prompt);
-	fflush(stdout);
-
-	/* If input is being provided by something which is not a terminal, don't
-	 * change the settings. */
-	if (is_tty) {
-		tcgetattr(fd, &termios_old);
-		memcpy(&termios_new, &termios_old, sizeof(termios_new));
-		termios_new.c_lflag &= ~ECHO;
-
-		act.sa_handler = sigint_termios;
-		act.sa_flags   = SA_RESETHAND;
-		sigemptyset(&act.sa_mask);
-
-		tty_fd = fd;
-		sigaction(SIGINT, &act, &old_act);
-
-		tcsetattr(fd, TCSAFLUSH, &termios_new);
-	}
-
-	if (timeout > 0) {
-		memset(&to, 0, sizeof(to));
-		to.tv_sec = timeout;
-
-		FD_ZERO(&fds);
-		FD_SET(fd, &fds);
-		nready = select(fd + 1, &fds, NULL, NULL, &to);
-		if (nready <= 0) {
-			r = EINTR;
-			goto out;
-		}
-	}
-
-	n = read(fd, pass, bufsz-1);
-	if (n > 0) {
-		pass[n-1] = '\0'; /* Strip trailing \n */
-	} else {
-		r = EIO;
-	}
-
-	/* Warn about passphrase trimming */
-	if (strlen(pass) > MAX_PASSSZ)
-		tc_log(0, "WARNING: Passphrase is being trimmed to %zu "
-		    "characters, discarding rest.\n", passlen);
-
-	/* Cut off after passlen characters */
-	pass[passlen] = '\0';
-
-out:
-	if (is_tty) {
-		tcsetattr(fd, TCSAFLUSH, &termios_old);
-		putchar('\n');
-
-		sigaction(SIGINT, &old_act, NULL);
-	}
-
-	return r;
-}
diff --git a/lib/libtcplay/pbkdf2-openssl.c b/lib/libtcplay/pbkdf2-openssl.c
deleted file mode 100644
index e3cbcc1d55..0000000000
--- a/lib/libtcplay/pbkdf2-openssl.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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 <errno.h>
-#include <openssl/evp.h>
-
-#include "tcplay.h"
-
-
-int
-pbkdf2(struct pbkdf_prf_algo *hash, const char *pass, int passlen,
-    const unsigned char *salt, int saltlen,
-    int keylen, unsigned char *out)
-{
-	const EVP_MD *md;
-	int r;
-
-	OpenSSL_add_all_algorithms();
-
-	md = EVP_get_digestbyname(hash->algo);
-	if (md == NULL) {
-		tc_log(1, "Hash %s not found\n", hash->algo);
-		return ENOENT;
-	}
-	r = PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen,
-	    hash->iteration_count, md, keylen, out);
-
-	if (r == 0) {
-		tc_log(1, "Error in PBKDF2\n");
-		return EINVAL;
-	}
-
-	return 0;
-}
-
diff --git a/lib/libtcplay/safe_mem.c b/lib/libtcplay/safe_mem.c
deleted file mode 100644
index 73ba370f6f..0000000000
--- a/lib/libtcplay/safe_mem.c
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/types.h>
-#include <sys/mman.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "tcplay.h"
-
-struct safe_mem_hdr {
-	struct safe_mem_hdr	*prev;
-	struct safe_mem_hdr	*next;
-	struct safe_mem_tail	*tail;
-	const char	*file;
-	int		line;
-	size_t		alloc_sz;
-	char		sig[8]; /* SAFEMEM */
-};
-
-struct safe_mem_tail {
-	char sig[8]; /* SAFEMEM */
-};
-
-static struct safe_mem_hdr *safe_mem_hdr_first = NULL;
-
-void *
-_alloc_safe_mem(size_t req_sz, const char *file, int line)
-{
-	struct safe_mem_hdr *hdr, *hdrp;
-	struct safe_mem_tail *tail;
-	size_t alloc_sz;
-	char *mem, *user_mem;
-
-	alloc_sz = req_sz + sizeof(*hdr) + sizeof(*tail);
-	if ((mem = malloc(alloc_sz)) == NULL)
-		return NULL;
-
-	if (mlock(mem, alloc_sz) < 0) {
-		free(mem);
-		return NULL;
-	}
-
-	memset(mem, 0, alloc_sz);
-
-	hdr = (struct safe_mem_hdr *)mem;
-	tail = (struct safe_mem_tail *)(mem + alloc_sz - sizeof(*tail));
-	user_mem = mem + sizeof(*hdr);
-
-	strcpy(hdr->sig, "SAFEMEM");
-	strcpy(tail->sig, "SAFEMEM");
-	hdr->tail = tail;
-	hdr->alloc_sz = alloc_sz;
-	hdr->file = file;
-	hdr->line = line;
-	hdr->next = NULL;
-
-	if (safe_mem_hdr_first == NULL) {
-		safe_mem_hdr_first = hdr;
-	} else {
-		hdrp = safe_mem_hdr_first;
-		while (hdrp->next != NULL)
-			hdrp = hdrp->next;
-		hdr->prev = hdrp;
-		hdrp->next = hdr;
-	}
-
-	return user_mem;
-}
-
-void
-_free_safe_mem(void *mem_ptr, const char *file, int line)
-{
-	struct safe_mem_hdr *hdr;
-	struct safe_mem_tail *tail;
-	size_t alloc_sz;
-	char *mem = mem_ptr;
-
-	mem -= sizeof(*hdr);
-	hdr = (struct safe_mem_hdr *)mem;
-	tail = (struct safe_mem_tail *)(mem + hdr->alloc_sz - sizeof(*tail));
-
-#ifdef DEBUG
-	fprintf(stderr, "freeing safe_mem (hdr): %#lx (%s:%d)\n",
-		    (unsigned long)(void *)hdr, hdr->file, hdr->line);
-#endif
-
-	if (hdr->alloc_sz == 0) {
-		fprintf(stderr, "BUG: double-free at %s:%d !!!\n", file, line);
-		return;
-	}
-
-	/* Integrity checks */
-	if ((memcmp(hdr->sig, "SAFEMEM\0", 8) != 0) ||
-	    (memcmp(tail->sig, "SAFEMEM\0", 8) != 0)) {
-		fprintf(stderr, "BUG: safe_mem buffer under- or overflow at "
-		    "%s:%d !!!\n", file, line);
-		return;
-	}
-
-	if (safe_mem_hdr_first == NULL) {
-		fprintf(stderr, "BUG: safe_mem list should not be empty at "
-		    "%s:%d !!!\n", file, line);
-		return;
-	}
-
-	if (hdr->prev != NULL)
-		hdr->prev->next = hdr->next;
-	if (hdr->next != NULL)
-		hdr->next->prev = hdr->prev;
-	if (safe_mem_hdr_first == hdr)
-		safe_mem_hdr_first = hdr->next;
-
-	alloc_sz = hdr->alloc_sz;
-	memset(mem, 0xFF, alloc_sz);
-	memset(mem, 0, alloc_sz);
-
-	free(mem);
-}
-
-void *
-_strdup_safe_mem(const char *in, const char *file, int line)
-{
-	char *out;
-	size_t sz;
-
-	sz = strlen(in)+1;
-
-	if ((out = _alloc_safe_mem(sz, file, line)) == NULL) {
-		return NULL;
-	}
-
-	memcpy(out, in, sz);
-	out[sz-1] = '\0';
-
-	return out;
-}
-
-void
-check_and_purge_safe_mem(void)
-{
-	struct safe_mem_hdr *hdr;
-	char *mem;
-#ifdef DEBUG
-	int ok;
-#endif
-
-	if (safe_mem_hdr_first == NULL)
-		return;
-
-	hdr = safe_mem_hdr_first;
-	while ((hdr = safe_mem_hdr_first) != NULL) {
-#ifdef DEBUG
-		if ((hdr->alloc_sz > 0) &&
-		    (memcmp(hdr->sig, "SAFEMEM\0", 8) == 0) &&
-		    (memcmp(hdr->tail->sig, "SAFEMEM\0", 8) == 0))
-			ok = 1;
-		else
-			ok = 0;
-
-		fprintf(stderr, "un-freed safe_mem: %#lx (%s:%d) [integrity=%s]\n",
-		    (unsigned long)(void *)hdr, hdr->file, hdr->line,
-		    ok? "ok" : "failed");
-#endif
-		mem = (void *)hdr;
-		mem += sizeof(*hdr);
-		_free_safe_mem(mem, "check_and_purge_safe_mem", 0);
-	}
-}
diff --git a/lib/libtcplay/tcplay.3 b/lib/libtcplay/tcplay.3
deleted file mode 100644
index 9d80ba2c81..0000000000
--- a/lib/libtcplay/tcplay.3
+++ /dev/null
@@ -1,564 +0,0 @@
-.\"
-.\" Copyright (c) 2011 The DragonFly Project.  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.
-.\" 3. Neither the name of The DragonFly Project nor the names of its
-.\"    contributors may be used to endorse or promote products derived
-.\"    from this software without specific, prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
-.\" COPYRIGHT HOLDERS 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.
-.\"
-.Dd May 2, 2020
-.Dt TCPLAY 3
-.Os
-.Sh NAME
-.Nm tc_api_init ,
-.Nm tc_api_uninit ,
-.Nm tc_api_has ,
-.Nm tc_api_cipher_iterate ,
-.Nm tc_api_prf_iterate ,
-.Nm tc_api_task_init ,
-.Nm tc_api_task_uninit ,
-.Nm tc_api_task_set ,
-.Nm tc_api_task_do ,
-.Nm tc_api_task_info_get ,
-.Nm tc_api_task_get_error
-.Nd TrueCrypt volume management
-.Sh LIBRARY
-.Lb libtcplay
-.Sh SYNOPSIS
-.In tcplay_api.h
-.Ft typedef int
-.Fn (*tc_api_cipher_iterator_fn) "void *priv" "const char *name" "int key_length_in_bits" "int ciphers_in_chain"
-.Ft typedef int
-.Fn (*tc_api_prf_iterator_fn) "void *priv" "const char *name"
-.Ft typedef int
-.Fn (*tc_api_state_change_fn) "void *priv" "const char *state" "int enter_state"
-.Ft int
-.Fn tc_api_init "int verbose"
-.Ft int
-.Fn tc_api_uninit "void"
-.Ft int
-.Fn tc_api_has "const char *feature"
-.Ft int
-.Fn tc_api_cipher_iterate "tc_api_cipher_iterator_fn fn" "void *priv"
-.Ft int
-.Fn tc_api_prf_iterate "tc_api_prf_iterator_fn fn" "void *priv"
-.Ft tc_api_task
-.Fn tc_api_task_init "const char *op"
-.Ft int
-.Fn tc_api_task_uninit "tc_api_task task"
-.Ft int
-.Fn tc_api_task_set "tc_api_task task" "const char *key" "..."
-.Ft int
-.Fn tc_api_task_do "tc_api_task task"
-.Ft int
-.Fn tc_api_task_info_get "tc_api_task task" "const char *key" "..."
-.Ft const char *
-.Fn tc_api_task_get_error "tc_api_task task"
-.Sh DESCRIPTION
-The
-.Nm tcplay
-library provides an interface to create, query, map and manage
-TrueCrypt-compatible volumes.
-.Pp
-The
-.Fn tc_api_init
-function initializes the library internals and prepares it for use via
-the API.
-This function has to be called before using any other API function.
-If the
-.Fa verbose
-argument is non-zero, then the library will output information such as
-errors via stdout and stderr.
-.Pp
-The
-.Fn tc_api_uninit
-function clears up all internal secure memory, such as memory used for
-decrypted headers, passphrases, keyfiles, etc.
-.Pp
-The
-.Fn tc_api_has
-function checks whether the loaded tcplay library has the feature
-specified by the
-.Fa feature
-argument.
-The current version of the
-.Nm tcplay
-library supports the following features:
-.Bl -column "some_long_feature" "Description"
-.It Sy Feature         Ta Sy Description
-.It Li trim            Ta    Allows enabling discards/TRIM when mapping a volume
-.El
-.Pp
-The
-.Fn tc_api_cipher_iterate
-function passes every available cipher chain to the callback provided in the
-.Fa fn
-argument.
-The
-.Fa priv
-argument is passed on every call of the callback function.
-The name of the cipher chain is passed to the callback function in the
-.Fa name
-argument.
-Similarly,
-the
-.Fa ciphers_in_chain
-argument holds the number of ciphers in the current chain,
-and
-.Fa key_length_in_bits
-holds the total key length for the cipher chain,
-in bits.
-.Pp
-The
-.Fn tc_api_prf_iterate
-function passes every available cipher chain to the callback provided in the
-.Fa fn
-argument.
-The
-.Fa priv
-argument is passed on every call of the callback function.
-The name of the PKBDF2 PRF algorithm is passed to the callback function in the
-.Fa name
-argument.
-.Pp
-The
-.Fn tc_api_task_init
-function initializes and returns a
-.Ft tc_api_task
-opaque pointer that can be used to run
-.Nm tcplay
-commands.
-Each task can be used only for a single
-.Fn tc_api_task_do
-call,
-and must be deallocated using
-.Fn tc_api_task_uninit .
-The
-.Fa op
-argument can be one of the following:
-.Bl -tag -width indent
-.It Sy create
-Create a new encrypted TrueCrypt volume.
-.It Sy map
-Map an existing TrueCrypt volume.
-.It Sy info
-Request information about an encrypted TrueCrypt volume.
-.It Sy info_mapped
-Request information about a mapped TrueCrypt volume.
-.It Sy unmap
-Unmap a mapped TrueCrypt volume.
-.It Sy modify
-Modify the TrueCrypt volume by changing the passphrase, keyfiles, PRF algorithm,
-restoring from a backup header, restoring from a header file or saving to a header file.
-.It Sy restore
-Modify the TrueCrypt volume as
-.Sy modify
-does, but without changing the passphrase, keyfiles or PRF algorithm.
-.El
-.Pp
-The
-.Fn tc_api_task_set
-function allows settting a number of different options for the current task.
-The following table shows which keys are available on calls to
-.Fn tc_api_task_set
-for each of the operations.
-The letter
-.Sy M
-indicates the setting is mandatory,
-whilst
-.Sy *
-indicates the setting is optional.
-.Bl -column "hidden_header_from_filexxx" "createxxx" "infoxxx" "mapxxx" "unmapxxx" "info_mappedxxx" "modifyxxx" "restorexxx"
-.It Sy Key                      Ta Sy create Ta Sy info Ta Sy map Ta Sy unmap Ta Sy info_mapped Ta Sy modify Ta Sy restore
-.It Li dev                      Ta    "M"    Ta    "M"  Ta    "M" Ta    "*"   Ta                Ta    "M"    Ta    "M"
-.It Li map_name                 Ta           Ta         Ta    "M" Ta    "M"   Ta    "M"         Ta           Ta    ""
-.It Li interactive              Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li retries                  Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li timeout                  Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta           Ta    ""
-.It Li state_change_fn          Ta    "*"    Ta         Ta        Ta          Ta                Ta    "*"    Ta    "*"
-.It Li weak_keys_and_salt       Ta    "*"    Ta         Ta        Ta          Ta                Ta    "*"    Ta    "*"
-.It Li secure_erase             Ta    "*"    Ta         Ta        Ta          Ta                Ta           Ta    ""
-.It Li hidden_size_bytes        Ta    "*"    Ta         Ta        Ta          Ta                Ta           Ta    ""
-.It Li prf_algo                 Ta    "*"    Ta         Ta        Ta          Ta                Ta           Ta    ""
-.It Li h_prf_algo               Ta    "*"    Ta         Ta        Ta          Ta                Ta           Ta    ""
-.It Li cipher_chain             Ta    "*"    Ta         Ta        Ta          Ta                Ta           Ta    ""
-.It Li h_cipher_chain           Ta    "*"    Ta         Ta        Ta          Ta                Ta           Ta    ""
-.It Li protect_hidden           Ta           Ta    "*"  Ta    "*" Ta          Ta                Ta           Ta    ""
-.It Li fde                      Ta           Ta    "*"  Ta    "*" Ta          Ta                Ta           Ta    ""
-.It Li sys                      Ta           Ta    "*"  Ta    "*" Ta          Ta                Ta    "?"    Ta    "?"
-.It Li use_backup_header        Ta           Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li header_from_file         Ta           Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li hidden_header_from_file  Ta           Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li allow_trim               Ta           Ta         Ta    "*" Ta          Ta                Ta           Ta    ""
-.It Li save_header_to_file      Ta           Ta         Ta        Ta          Ta                Ta    "*"    Ta    ""
-.It Li passphrase               Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li h_passphrase             Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li keyfiles                 Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li h_keyfiles               Ta    "*"    Ta    "*"  Ta    "*" Ta          Ta                Ta    "*"    Ta    "*"
-.It Li new_passphrase           Ta           Ta         Ta        Ta          Ta                Ta    "*"    Ta    ""
-.It Li new_keyfiles             Ta           Ta         Ta        Ta          Ta                Ta    "*"    Ta    ""
-.It Li new_prf_algo             Ta           Ta         Ta        Ta          Ta                Ta    "*"    Ta    ""
-.El
-.Pp
-The varargs accepted by the
-.Fn tc_api_task_set
-function depend on the key being set.
-.Bl -tag -width indent
-.It dev
-The vararg is of type
-.Ft const char * .
-It sets the device that contains the TrueCrypt volume to operate on.
-.It map_name
-The vararg is of type
-.Ft const char * .
-It set the name of the mapped volume.
-.It interactive
-The vararg is of type
-.Ft int .
-It determines whether the user will be prompted for a passphrase or whether
-the settings are taken from the arguments set using
-.Fn tc_api_task_set .
-.It retries
-The vararg is of type
-.Ft int .
-It determines the number of passphrase retries if
-.Fa interactive
-is set.
-.It weak_keys_and_salt
-The vararg is of type
-.Ft int .
-It determines whether to use a weak source of entropy for key material and/or
-the salt.
-.It secure_erase
-The vararg is of type
-.Ft int .
-It determines whether a secure erase is performed as part of the volume creation.
-.It hidden_size_bytes
-The vararg is of type
-.Ft int64_t .
-If set to
-.Fa 0
-it implies no hidden volume will be created.
-A positive value implies a hidden volume of the specified size in bytes is created.
-.It prf_algo
-The vararg is of type
-.Ft const char *
-and must be a valid PBKDF2 PRF algorithm.
-It determines which PBKDF2 PRF algorithm is used for the outer volume.
-.It h_prf_algo
-The vararg is of type
-.Ft const char *
-and must be a valid PBKDF2 PRF algorithm.
-It determines which PBKDF2 PRF algorithm is used for the hidden volume.
-.It cipher_chain
-The vararg is of type
-.Ft const char *
-and must be a valid
-.Nm tcplay
-cipher chain.
-It determines which cipher chain is used to encrypt the outer volume.
-.It h_cipher_chain
-The vararg is of type
-.Ft const char *
-and must be a valid
-.Nm tcplay
-cipher chain.
-It determines which cipher chain is used to encrypt the hidden volume.
-.It protect_hidden
-The vararg is of type
-.Ft int .
-It determines whether the size of the outer volume should be adjusted to protect
-any hidden volume.
-Using this mode requires both outer and hidden passphrases and keyfiles.
-.It sys
-The vararg is of type
-.Ft const char * .
-It determines whether the volume is a system encrypted volume,
-and if so, which disk is the system disk and hence contains the header.
-If set to
-.Dv NULL
-the volume will implicitly be treated as a non-system encrypted volume.
-.It fde
-The vararg is of type
-.Ft int .
-It determines whether the disk uses full disk encryption or not.
-If specified, the device pointed to by the
-.Fa dev
-setting should be a whole disk device, not any partition.
-The device will be mapped or queried as a whole.
-For more details on full disk encryption, see
-.Xr tcplay 8 .
-.It use_backup_header
-The vararg is of type
-.Ft int .
-It determines whether the backup header should be used instead of the regular
-header to access the volume.
-.It header_from_file
-The vararg is of type
-.Ft const char * .
-If not
-.Dv NULL
-it forces
-.Nm tcplay
-to use the header in the specified file instead of the regular outer volume header.
-.It hidden_header_from_file
-The vararg is of type
-.Ft const char * .
-If not
-.Dv NULL
-it forces
-.Nm tcplay
-to use the header in the specified file instead of the regular hidden volume header.
-.It allow_trim
-The vararg is of type
-.Ft int .
-It specifies whether the mapped volume should allow discards (TRIM).
-.It save_header_to_file
-The vararg is of type
-.Ft const char * .
-If not
-.Dv NULL
-it forces
-.Nm tcplay
-to write the (modified) header to the specified file instead of replacing the volume
-headers.
-.It passphrase
-The vararg is of type
-.Ft const char * .
-It sets the passphrase that
-.Nm tcplay
-uses to access the volume.
-.It h_passphrase
-The vararg is of type
-.Ft const char * .
-It sets the passphrase that
-.Nm tcplay
-uses to unlock the hidden volume header.
-This option is only used if a hidden volume is being created or the
-.Fa protect_hidden
-setting is set.
-Otherwise
-.Nm tcplay
-will first use the regular passphrase to try to unlock the outer volume and then
-try to unlock the hidden volume header with the same passphrase without ever
-using
-.Fa h_passphrase .
-.It keyfiles
-The vararg is of type
-.Ft const char * .
-If not
-.Dv NULL
-the given keyfile will be added to the keyfile pool.
-Multiple calls to set this option with a non-
-.Dv NULL
-argument result add additional keyfiles.
-If
-.Dv NULL
-all keyfiles are cleared.
-.It h_keyfiles
-The vararg is of type
-.Ft const char * .
-If not
-.Dv NULL
-the given keyfile will be added to the keyfile pool.
-Multiple calls to set this option with a non-
-.Dv NULL
-argument result add additional keyfiles.
-If
-.Dv NULL
-all keyfiles are cleared.
-This option is only used if a hidden volume is being created or the
-.Fa protect_hidden
-setting is set.
-Otherwise
-.Nm tcplay
-will first use the regular keyfiles to try to unlock the outer volume and then
-try to unlock the hidden volume header with the same keyfiles without ever
-using
-.Fa h_keyfiles .
-.It new_passphrase
-The vararg is of type
-.Fa const char * .
-It specifies the new passphrase to use when modifying the volume header.
-.It new_keyfiles
-The vararg is of type
-.Fa const char * .
-If not
-.Dv NULL
-the given keyfile will be added to the new keyfile pool.
-Multiple calls to set this option with a non-
-.Dv NULL
-argument result add additional keyfiles.
-If
-.Dv NULL
-all new keyfiles are cleared.
-When the volume header is modified,
-it will be reencrypted using the new keyfiles.
-.It new_prf_algo
-The vararg is of type
-.Ft const char *
-and must be a valid PBKDF2 PRF algorithm.
-It determines which PBKDF2 PRF algorithm is used when reencrypting the (modified)
-volume header.
-.It state_change_fn
-The first vararg is of type
-.Fa tc_api_state_change_fn
-and the second vararg is of type
-.Fa void * .
-It allows the consumer to provide a callback function which will be called when
-starting and stopping a time-intensive sub-operation such as collecting entropy
-or erasing a volume.
-The second vararg is passed as the
-.Fa priv
-argument to the callback.
-The
-.Fa enter_state
-argument to the callback determines whether
-.Nm tcplay
-is starting or stopping the time-intensive sub-task specified in the
-.Fa state
-argument.
-.El
-.Pp
-The
-.Fn tc_api_task_do
-function runs the task specified in the
-.Fa task
-argument.
-Before running the task,
-.Fn tc_api_task_do
-performs a simple sanity check of the arguments set previously using
-.Fn tc_api_task_set
-before performing the actual operation.
-After a call to
-.Fn tc_api_task_do
-for the
-.Sy info
-or
-.Sy info_mapped
-operations,
-the queried information is available to be accessed using
-.Fn tc_api_task_info_get .
-.Pp
-The
-.Fn tc_api_task_info_get
-function can be used to query the result of a
-.Sy info
-or
-.Sy info_mapped
-operation.
-The
-.Fa task
-argument is the task used in a previous
-.Fn tc_api_task_do
-call.
-The
-.Fa key
-argument can be one of the following:
-.Bl -column "some_long_feature" "very_long_type" "Description"
-.It Sy Key             Ta Sy type       Ta Sy Description
-.It Li device          Ta Ft char *     Ta Corresponding device node
-.It Li cipher          Ta Ft char *     Ta Used cipher chain
-.It Li prf             Ta Ft char *     Ta Used PBKDF2 PRF algorithm
-.It Li key_bits        Ta Ft int *      Ta Number of key bits
-.It Li size            Ta Ft int64_t *  Ta Volume size in bytes
-.It Li iv_offset       Ta Ft int64_t *  Ta IV Offset of volume in bytes
-.It Li block_offset    Ta Ft int64_t *  Ta Block Offset of volume in bytes
-.El
-.Pp
-The second vararg argument must be of the type specified in the above table.
-The first vararg argument is always the size of the storage provided
-in the second argument.
-For a
-.Ft char *
-argument, the size corresponds to the size of the buffer at the provided
-location and must be of type
-.Ft size_t .
-For an
-.Ft int *
-or
-.Ft int64_t *
-argument, it should be the size of the underlying type.
-.Pp
-The
-.Fn tc_api_task_get_error
-function can be used to get a detailed error message after a failed
-.Fa tc_api_task_do
-call.
-The
-.Fa task
-argument is the task used in a previous
-.Fn tc_api_task_do
-call.
-.Sh NOTES
-TrueCrypt limits passphrases to 64 characters (including the terminating
-null character).
-To be compatible with it,
-.Nm tcplay
-does the same.
-All passphrases (exlcuding keyfiles) are trimmed to 64 characters.
-Similarly, keyfiles are limited to a size of 1 MB, but up to 256
-keyfiles can be used.
-.Sh RETURN VALUES
-All functions except
-.Fn tc_api_task_init
-and
-.Fn tc_api_task_get_error
-return either
-.Dv TC_OK
-to indicate that the operation completed successfully, or
-.Dv TC_ERR_UNIMPL
-to indicate that the operation is not implemented
-, or
-.Dv TC_ERR
-to indicate that any other error occurred.
-.Pp
-The
-.Fn tc_api_task_get_error
-function always return a valid, but possibly empty (or irrelevant,
-if not called after an error occurred) string.
-.Pp
-The
-.Fn tc_api_task_init
-function returns
-.Dv NULL
-if an error occurred and an opaque
-.Ft tc_api_task
-otherwise.
-.Sh COMPATIBILITY
-The
-.Nm tcplay
-library offers full compatibility with TrueCrypt volumes including
-hidden
-volumes, system encryption (map-only), keyfiles and cipher cascading.
-.Sh SEE ALSO
-.Xr tcplay 8
-.Sh HISTORY
-The
-.Nm tcplay
-library appeared in
-.Dx 2.11 .
-.Sh AUTHORS
-.An Alex Hornung
diff --git a/lib/libtcplay/tcplay.c b/lib/libtcplay/tcplay.c
deleted file mode 100644
index c268e6e4db..0000000000
--- a/lib/libtcplay/tcplay.c
+++ /dev/null
@@ -1,2215 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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.
- */
-
-#define _BSD_SOURCE
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#if defined(__DragonFly__)
-#include <sys/param.h>
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <inttypes.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <err.h>
-#include <time.h>
-#if defined(__linux__)
-#include <libdevmapper.h>
-#include <uuid/uuid.h>
-#include <sys/sysmacros.h>
-#elif defined(__DragonFly__)
-#include <libdm.h>
-#include <uuid.h>
-#endif
-
-#include <dirent.h>
-
-#include "crc32.h"
-#include "tcplay.h"
-#include "humanize.h"
-
-
-/* XXX TODO:
- *  - LRW-benbi support? needs further work in dm-crypt and even opencrypto
- *  - secure buffer review (i.e: is everything that needs it using secure mem?)
- *  - mlockall? (at least MCL_FUTURE, which is the only one we support)
- */
-
-summary_fn_t summary_fn = NULL;
-int tc_internal_verbose = 1;
-char tc_internal_log_buffer[LOG_BUFFER_SZ];
-int tc_internal_state = STATE_UNKNOWN;
-
-void
-tc_log(int is_err, const char *fmt, ...)
-{
-	va_list ap;
-	FILE *fp;
-
-	if (is_err)
-		fp = stderr;
-	else
-		fp = stdout;
-
-        va_start(ap, fmt);
-
-	vsnprintf(tc_internal_log_buffer, LOG_BUFFER_SZ, fmt, ap);
-
-	va_end(ap);
-
-	if (tc_internal_verbose)
-	    fprintf(fp, "%s", tc_internal_log_buffer);
-}
-
-/* Supported algorithms */
-struct pbkdf_prf_algo pbkdf_prf_algos[] = {
-	{ "RIPEMD160",		"RIPEMD160",	2000,	TC_SIG, 0},
-	{ "RIPEMD160",		"RIPEMD160",	1000,	TC_SIG, 1},
-	{ "SHA512",		"SHA512",	1000,	TC_SIG, 0},
-	{ "whirlpool",		"whirlpool",	1000,	TC_SIG, 0},
-	{ "RIPEMD160-VC",	"RIPEMD160",	655331,	VC_SIG, 0},
-	{ "RIPEMD160-VC",	"RIPEMD160",	327661,	VC_SIG, 1},
-	{ "SHA512-VC",		"SHA512",	500000,	VC_SIG, 0},
-	{ "whirlpool-VC",	"whirlpool",	500000,	VC_SIG, 0},
-	{ "SHA256-VC",		"SHA256",	500000,	VC_SIG, 0},
-	{ "SHA256-VC",		"SHA256",	200000,	VC_SIG, 1},
-	{ NULL,			NULL,		0,	NULL,	0}
-};
-
-struct tc_crypto_algo tc_crypto_algos[] = {
-#if 0
-	/* XXX: turns out TC doesn't support AES-128-XTS */
-	{ "AES-128-XTS",	"aes-xts-plain",	32,	8 },
-	{ "TWOFISH-128-XTS",	"twofish-xts-plain",	32,	8 },
-	{ "SERPENT-128-XTS",	"serpent-xts-plain",	32,	8 },
-#endif
-	{ "AES-256-XTS",	"aes-xts-plain64",	64,	8 },
-	{ "TWOFISH-256-XTS",	"twofish-xts-plain64",	64,	8 },
-	{ "SERPENT-256-XTS",	"serpent-xts-plain64",	64,	8 },
-	{ NULL,			NULL,			0,	0 }
-};
-
-const char *valid_cipher_chains[][MAX_CIPHER_CHAINS] = {
-	{ "AES-256-XTS", NULL },
-	{ "TWOFISH-256-XTS", NULL },
-	{ "SERPENT-256-XTS", NULL },
-	{ "AES-256-XTS", "TWOFISH-256-XTS", "SERPENT-256-XTS", NULL },
-	{ "SERPENT-256-XTS", "TWOFISH-256-XTS", "AES-256-XTS", NULL },
-#if 0
-	/* It seems that all the two-way cascades are the other way round... */
-	{ "AES-256-XTS", "TWOFISH-256-XTS", NULL },
-	{ "SERPENT-256-XTS", "AES-256-XTS", NULL },
-	{ "TWOFISH-256-XTS", "SERPENT-256-XTS", NULL },
-
-#endif
-	{ "TWOFISH-256-XTS", "AES-256-XTS", NULL },
-	{ "AES-256-XTS", "SERPENT-256-XTS", NULL },
-	{ "SERPENT-256-XTS", "TWOFISH-256-XTS", NULL },
-	{ NULL }
-};
-
-struct tc_cipher_chain *tc_cipher_chains[MAX_CIPHER_CHAINS];
-
-static
-int
-tc_build_cipher_chains(void)
-{
-	struct tc_cipher_chain *chain, *elem, *prev;
-	int i = 0;
-	int k;
-
-	while (valid_cipher_chains[i][0] != NULL) {
-		chain = NULL;
-		prev = NULL;
-		k = 0;
-
-		while (valid_cipher_chains[i][k] != NULL) {
-			if ((elem = alloc_safe_mem(sizeof(*elem))) == NULL) {
-				tc_log(1, "Error allocating memory for "
-				   "cipher chain\n");
-				return -1;
-			}
-
-			/* Initialize first element of chain */
-			if (chain == NULL) {
-				chain = elem;
-				elem->prev = NULL;
-			}
-
-			/* Populate previous element */
-			if (prev != NULL) {
-				prev->next = elem;
-				elem->prev = prev;
-			}
-
-			/* Assume we are the last element in the chain */
-			elem->next = NULL;
-
-			/* Initialize other fields */
-			elem->cipher = check_cipher(valid_cipher_chains[i][k], 0);
-			if (elem->cipher == NULL)
-				return -1;
-
-			elem->key = NULL;
-
-			prev = elem;
-			++k;
-		}
-
-		/* Store cipher chain */
-		tc_cipher_chains[i++] = chain;
-
-		/* Integrity check */
-		if (i >= MAX_CIPHER_CHAINS) {
-			tc_log(1, "FATAL: tc_cipher_chains is full!!\n");
-			return -1;
-		}
-
-		/* Make sure array is NULL terminated */
-		tc_cipher_chains[i] = NULL;
-	}
-
-	return 0;
-}
-
-static
-struct tc_cipher_chain *
-tc_dup_cipher_chain(struct tc_cipher_chain *src)
-{
-	struct tc_cipher_chain *first = NULL, *prev = NULL, *elem;
-
-	for (; src != NULL; src = src->next) {
-		if ((elem = alloc_safe_mem(sizeof(*elem))) == NULL) {
-			tc_log(1, "Error allocating memory for "
-			    "duplicate cipher chain\n");
-			return NULL;
-		}
-
-		memcpy(elem, src, sizeof(*elem));
-
-		if (src->key != NULL) {
-			if ((elem->key = alloc_safe_mem(src->cipher->klen)) == NULL) {
-				tc_log(1, "Error allocating memory for "
-				    "duplicate key in cipher chain\n");
-				return NULL;
-			}
-
-			memcpy(elem->key, src->key, src->cipher->klen);
-		}
-
-		if (first == NULL)
-			first = elem;
-
-		elem->next = NULL;
-		elem->prev = prev;
-
-		if (prev != NULL)
-			prev->next = elem;
-
-		prev = elem;
-	}
-
-	return first;
-}
-
-static
-int
-tc_free_cipher_chain(struct tc_cipher_chain *chain)
-{
-	struct tc_cipher_chain *next = chain;
-
-	while ((chain = next) != NULL) {
-		next = chain->next;
-
-		if (chain->key != NULL)
-			free_safe_mem(chain->key);
-		free_safe_mem(chain);
-	}
-
-	return 0;
-}
-
-int
-tc_cipher_chain_length(struct tc_cipher_chain *chain)
-{
-	int len = 0;
-
-	for (; chain != NULL; chain = chain->next)
-		++len;
-
-	return len;
-}
-
-int
-tc_cipher_chain_klen(struct tc_cipher_chain *chain)
-{
-	int klen_bytes = 0;
-
-	for (; chain != NULL; chain = chain->next) {
-		klen_bytes += chain->cipher->klen;
-	}
-
-	return klen_bytes;
-}
-
-char *
-tc_cipher_chain_sprint(char *buf, size_t bufsz, struct tc_cipher_chain *chain)
-{
-	static char sbuf[256];
-	int n = 0;
-
-	if (buf == NULL) {
-		buf = sbuf;
-		bufsz = sizeof(sbuf);
-	}
-
-	for (; chain != NULL; chain = chain->next) {
-		n += snprintf(buf+n, bufsz-n, "%s%s", chain->cipher->name,
-		    (chain->next != NULL) ? "," : "\0");
-	}
-
-	return buf;
-}
-
-#ifdef DEBUG
-static void
-print_hex(unsigned char *buf, off_t start, size_t len)
-{
-	size_t i;
-
-	for (i = start; i < start+len; i++)
-		printf("%02x", buf[i]);
-
-	printf("\n");
-}
-#endif
-
-void
-print_info(struct tcplay_info *info)
-{
-	printf("Device:\t\t\t%s\n", info->dev);
-
-	if (info->pbkdf_prf != NULL) {
-		printf("PBKDF2 PRF:\t\t%s\n", info->pbkdf_prf->name);
-		printf("PBKDF2 iterations:\t%d\n",
-		    info->pbkdf_prf->iteration_count);
-	}
-
-	printf("Cipher:\t\t\t%s\n",
-	    tc_cipher_chain_sprint(NULL, 0, info->cipher_chain));
-
-	printf("Key Length:\t\t%d bits\n",
-	    8*tc_cipher_chain_klen(info->cipher_chain));
-
-	if (info->hdr != NULL) {
-		printf("CRC Key Data:\t\t%#x\n", info->hdr->crc_keys);
-		printf("Sector size:\t\t%d\n", info->hdr->sec_sz);
-		printf("Signature:\t\t%c%c%c%c\n", info->hdr->tc_str[0],
-		       info->hdr->tc_str[1], info->hdr->tc_str[2],
-		       info->hdr->tc_str[3]);
-	} else {
-		printf("Sector size:\t\t512\n");
-	}
-	printf("Volume size:\t\t%"DISKSZ_FMT" sectors\n", info->size);
-#if 0
-	/* Don't print this; it's always 0 and is rather confusing */
-	printf("Volume offset:\t\t%"PRIu64"\n", (uint64_t)info->start);
-#endif
-
-#ifdef DEBUG
-	printf("Vol Flags:\t\t%d\n", info->volflags);
-#endif
-
-	printf("IV offset:\t\t%"PRIu64" sectors\n",
-	    (uint64_t)info->skip);
-	printf("Block offset:\t\t%"PRIu64" sectors\n",
-	    (uint64_t)info->offset);
-}
-
-static
-struct tcplay_info *
-new_info(const char *dev, int flags, struct tc_cipher_chain *cipher_chain,
-    struct pbkdf_prf_algo *prf, struct tchdr_dec *hdr, off_t start)
-{
-	struct tc_cipher_chain *chain_start;
-	struct tcplay_info *info;
-	int i;
-	int error;
-
-	chain_start = cipher_chain;
-
-	if ((info = (struct tcplay_info *)alloc_safe_mem(sizeof(*info))) == NULL) {
-		tc_log(1, "could not allocate safe info memory\n");
-		return NULL;
-	}
-
-	strncpy(info->dev, dev, sizeof(info->dev));
-	info->cipher_chain = cipher_chain;
-	info->pbkdf_prf = prf;
-	info->start = start;
-	info->hdr = hdr;
-	info->blk_sz = hdr->sec_sz;
-	info->size = hdr->sz_mk_scope / hdr->sec_sz;	/* volume size */
-	info->skip = hdr->off_mk_scope / hdr->sec_sz;	/* iv skip */
-
-	info->volflags = hdr->flags;
-	info->flags = flags;
-
-	if (TC_FLAG_SET(flags, SYS))
-		info->offset = 0; /* offset is 0 for system volumes */
-	else
-		info->offset = hdr->off_mk_scope / hdr->sec_sz;	/* block offset */
-
-	/* Associate a key out of the key pool with each cipher in the chain */
-	error = tc_cipher_chain_populate_keys(cipher_chain, hdr->keys);
-	if (error) {
-		tc_log(1, "could not populate keys in cipher chain\n");
-		return NULL;
-	}
-
-	for (; cipher_chain != NULL; cipher_chain = cipher_chain->next) {
-		for (i = 0; i < cipher_chain->cipher->klen; i++)
-			sprintf(&cipher_chain->dm_key[i*2], "%02x",
-			    cipher_chain->key[i]);
-	}
-
-	tc_cipher_chain_free_keys(chain_start);
-
-	return info;
-}
-
-int
-free_info(struct tcplay_info *info)
-{
-	if (info->cipher_chain)
-		tc_free_cipher_chain(info->cipher_chain);
-	if (info->hdr)
-		free_safe_mem(info->hdr);
-
-	free_safe_mem(info);
-
-	return 0;
-}
-
-int
-adjust_info(struct tcplay_info *info, struct tcplay_info *hinfo)
-{
-	if (hinfo->hdr->sz_hidvol == 0)
-		return 1;
-
-	info->size -= hinfo->hdr->sz_hidvol / hinfo->hdr->sec_sz;
-	return 0;
-}
-
-int
-process_hdr(const char *dev, int flags, unsigned char *pass, int passlen,
-    struct tchdr_enc *ehdr, struct tcplay_info **pinfo)
-{
-	struct tchdr_dec *dhdr;
-	struct tcplay_info *info;
-	struct tc_cipher_chain *cipher_chain = NULL;
-	unsigned char *key;
-	int i, j, found, error;
-
-	*pinfo = NULL;
-
-	if ((key = alloc_safe_mem(MAX_KEYSZ)) == NULL) {
-		tc_log(1, "could not allocate safe key memory\n");
-		return ENOMEM;
-	}
-
-	/* Start search for correct algorithm combination */
-	found = 0;
-	for (i = 0; !found && pbkdf_prf_algos[i].name != NULL; i++) {
-#ifdef DEBUG
-		printf("\nTrying PRF algo %s (%d)\n", pbkdf_prf_algos[i].name,
-		    pbkdf_prf_algos[i].iteration_count);
-		printf("Salt: ");
-		print_hex(ehdr->salt, 0, sizeof(ehdr->salt));
-#endif
-		error = pbkdf2(&pbkdf_prf_algos[i], (char *)pass, passlen,
-		    ehdr->salt, sizeof(ehdr->salt),
-		    MAX_KEYSZ, key);
-
-		if (error) {
-			tc_log(1, "pbkdf failed for algorithm %s\n",
-			    pbkdf_prf_algos[i].name);
-			free_safe_mem(key);
-			return EINVAL;
-		}
-
-#if 0
-		printf("Derived Key: ");
-		print_hex(key, 0, MAX_KEYSZ);
-#endif
-
-		for (j = 0; !found && tc_cipher_chains[j] != NULL; j++) {
-			cipher_chain = tc_dup_cipher_chain(tc_cipher_chains[j]);
-#ifdef DEBUG
-			printf("\nTrying cipher chain %d\n", j);
-#endif
-
-			dhdr = decrypt_hdr(ehdr, cipher_chain, key);
-			if (dhdr == NULL) {
-				tc_log(1, "hdr decryption failed for cipher "
-				    "chain %d\n", j);
-				free_safe_mem(key);
-				return EINVAL;
-			}
-
-			if (verify_hdr(dhdr, &pbkdf_prf_algos[i])) {
-#ifdef DEBUG
-				printf("tc_str: %.4s, tc_ver: %d, tc_min_ver: %d, "
-				    "crc_keys: %d, sz_vol: %"PRIu64", "
-				    "off_mk_scope: %"PRIu64", sz_mk_scope: %"PRIu64", "
-				    "flags: %d, sec_sz: %d crc_dhdr: %d\n",
-				    dhdr->tc_str, dhdr->tc_ver, dhdr->tc_min_ver,
-				    dhdr->crc_keys, dhdr->sz_vol, dhdr->off_mk_scope,
-				    dhdr->sz_mk_scope, dhdr->flags, dhdr->sec_sz,
-				    dhdr->crc_dhdr);
-#endif
-				found = 1;
-			} else {
-				free_safe_mem(dhdr);
-				tc_free_cipher_chain(cipher_chain);
-			}
-		}
-	}
-
-	free_safe_mem(key);
-
-	if (!found)
-		return EINVAL;
-
-	if ((info = new_info(dev, flags, cipher_chain,
-	    &pbkdf_prf_algos[i-1], dhdr, 0)) == NULL) {
-		free_safe_mem(dhdr);
-		return ENOMEM;
-	}
-
-	*pinfo = info;
-
-	return 0;
-}
-
-int
-create_volume(struct tcplay_opts *opts)
-{
-	char *pass, *pass_again;
-	char *h_pass = NULL;
-	char buf[1024];
-	disksz_t blocks, hidden_blocks = 0;
-	size_t blksz;
-	struct tchdr_enc *ehdr, *hehdr;
-	struct tchdr_enc *ehdr_backup, *hehdr_backup;
-	uint64_t tmp;
-	int error, r, ret;
-
-	pass = h_pass = pass_again = NULL;
-	ehdr = hehdr = NULL;
-	ehdr_backup = hehdr_backup = NULL;
-	ret = -1; /* Default to returning error */
-
-	if (opts->cipher_chain == NULL)
-		opts->cipher_chain = tc_cipher_chains[0];
-	if (opts->prf_algo == NULL)
-		opts->prf_algo = &pbkdf_prf_algos[DEFAULT_PRF_ALGO_IDX];
-	if (opts->h_cipher_chain == NULL)
-		opts->h_cipher_chain = opts->cipher_chain;
-	if (opts->h_prf_algo == NULL)
-		opts->h_prf_algo = opts->prf_algo;
-
-	if ((error = get_disk_info(opts->dev, &blocks, &blksz)) != 0) {
-		tc_log(1, "could not get disk info\n");
-		return -1;
-	}
-
-	if ((blocks*blksz) <= MIN_VOL_BYTES) {
-		tc_log(1, "Cannot create volumes on devices with less "
-		    "than %d bytes\n", MIN_VOL_BYTES);
-		return -1;
-	}
-
-	if (opts->interactive) {
-		if (((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) ||
-		   ((pass_again = alloc_safe_mem(PASS_BUFSZ)) == NULL)) {
-			tc_log(1, "could not allocate safe passphrase memory\n");
-			goto out;
-		}
-
-		if ((error = read_passphrase("Passphrase: ", pass, MAX_PASSSZ,
-		    PASS_BUFSZ, 0) ||
-		    (read_passphrase("Repeat passphrase: ", pass_again,
-		    MAX_PASSSZ, PASS_BUFSZ, 0)))) {
-			tc_log(1, "could not read passphrase\n");
-			goto out;
-		}
-
-		if (strcmp(pass, pass_again) != 0) {
-			tc_log(1, "Passphrases don't match\n");
-			goto out;
-		}
-
-		free_safe_mem(pass_again);
-		pass_again = NULL;
-	} else {
-		/* In batch mode, use provided passphrase */
-		if ((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
-			tc_log(1, "could not allocate safe "
-			    "passphrase memory");
-			goto out;
-		}
-
-		if (opts->passphrase != NULL) {
-			strncpy(pass, opts->passphrase, MAX_PASSSZ);
-			pass[MAX_PASSSZ] = '\0';
-		}
-	}
-
-	if (opts->nkeyfiles > 0) {
-		/* Apply keyfiles to 'pass' */
-		if ((error = apply_keyfiles((unsigned char *)pass, PASS_BUFSZ,
-		    opts->keyfiles, opts->nkeyfiles))) {
-			tc_log(1, "could not apply keyfiles\n");
-			goto out;
-		}
-	}
-
-	if (opts->hidden) {
-		if (opts->interactive) {
-			if (((h_pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) ||
-			   ((pass_again = alloc_safe_mem(PASS_BUFSZ)) == NULL)) {
-				tc_log(1, "could not allocate safe "
-				    "passphrase memory\n");
-				goto out;
-			}
-
-			if ((error = read_passphrase("Passphrase for hidden volume: ",
-			   h_pass, MAX_PASSSZ, PASS_BUFSZ, 0) ||
-			   (read_passphrase("Repeat passphrase: ", pass_again,
-			   MAX_PASSSZ, PASS_BUFSZ, 0)))) {
-				tc_log(1, "could not read passphrase\n");
-				goto out;
-			}
-
-			if (strcmp(h_pass, pass_again) != 0) {
-				tc_log(1, "Passphrases for hidden volume don't "
-				    "match\n");
-				goto out;
-			}
-
-			free_safe_mem(pass_again);
-			pass_again = NULL;
-		} else {
-			/* In batch mode, use provided passphrase */
-			if ((h_pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
-				tc_log(1, "could not allocate safe "
-				    "passphrase memory");
-				goto out;
-			}
-
-			if (opts->h_passphrase != NULL) {
-				strncpy(h_pass, opts->h_passphrase, MAX_PASSSZ);
-				h_pass[MAX_PASSSZ] = '\0';
-			}
-		}
-
-		if (opts->n_hkeyfiles > 0) {
-			/* Apply keyfiles to 'h_pass' */
-			if ((error = apply_keyfiles((unsigned char *)h_pass,
-			    PASS_BUFSZ, opts->h_keyfiles, opts->n_hkeyfiles))) {
-				tc_log(1, "could not apply keyfiles\n");
-				goto out;
-			}
-		}
-
-		if (opts->interactive) {
-			hidden_blocks = 0;
-		} else {
-			hidden_blocks = opts->hidden_size_bytes/blksz;
-			if (hidden_blocks == 0) {
-				tc_log(1, "hidden_blocks to create volume "
-				    "cannot be zero!\n");
-				goto out;
-			}
-
-			if (opts->hidden_size_bytes >=
-			    (blocks*blksz) - MIN_VOL_BYTES) {
-				tc_log(1, "Hidden volume needs to be "
-				    "smaller than the outer volume\n");
-				goto out;
-			}
-		}
-
-		/* This only happens in interactive mode */
-		while (hidden_blocks == 0) {
-			if ((r = _humanize_number(buf, sizeof(buf),
-			    (uint64_t)(blocks * blksz))) < 0) {
-				sprintf(buf, "%"DISKSZ_FMT" bytes", (blocks * blksz));
-			}
-
-			printf("The total volume size of %s is %s (bytes)\n", opts->dev, buf);
-			memset(buf, 0, sizeof(buf));
-			printf("Size of hidden volume (e.g. 127M):  ");
-			fflush(stdout);
-
-			if ((fgets(buf, sizeof(buf), stdin)) == NULL) {
-				tc_log(1, "Could not read from stdin\n");
-				goto out;
-			}
-
-			/* get rid of trailing newline */
-			buf[strlen(buf)-1] = '\0';
-			if ((error = _dehumanize_number(buf,
-			    &tmp)) != 0) {
-				tc_log(1, "Could not interpret input: %s\n", buf);
-				continue;
-			}
-
-			if (tmp >= (blocks*blksz) - MIN_VOL_BYTES) {
-				tc_log(1, "Hidden volume needs to be "
-				    "smaller than the outer volume\n");
-				hidden_blocks = 0;
-				continue;
-			}
-
-			hidden_blocks = (size_t)tmp;
-			hidden_blocks /= blksz;
-		}
-	}
-
-	if (opts->interactive) {
-		/* Show summary and ask for confirmation */
-		printf("Summary of actions:\n");
-		if (opts->secure_erase)
-			printf(" - Completely erase *EVERYTHING* on %s\n", opts->dev);
-		printf(" - Create %svolume on %s\n", opts->hidden?("outer "):"", opts->dev);
-		if (opts->hidden) {
-			printf(" - Create hidden volume of %"DISKSZ_FMT" bytes at end of "
-			    "outer volume\n",
-			    hidden_blocks * blksz);
-		}
-
-		printf("\n Are you sure you want to proceed? (y/n) ");
-		fflush(stdout);
-		if ((fgets(buf, sizeof(buf), stdin)) == NULL) {
-			tc_log(1, "Could not read from stdin\n");
-			goto out;
-		}
-
-		if ((buf[0] != 'y') && (buf[0] != 'Y')) {
-			tc_log(1, "User cancelled action(s)\n");
-			goto out;
-		}
-	}
-
-	/* erase volume */
-	if (opts->secure_erase) {
-		tc_log(0, "Securely erasing the volume...\nThis process may take "
-		    "some time depending on the size of the volume\n");
-
-		if (opts->state_change_fn)
-			opts->state_change_fn(opts->api_ctx, "secure_erase", 1);
-
-		if ((error = secure_erase(opts->dev, blocks * blksz, blksz)) != 0) {
-			tc_log(1, "could not securely erase device %s\n", opts->dev);
-			goto out;
-		}
-
-		if (opts->state_change_fn)
-			opts->state_change_fn(opts->api_ctx, "secure_erase", 0);
-	}
-
-	tc_log(0, "Creating volume headers...\nDepending on your system, this "
-	    "process may take a few minutes as it uses true random data which "
-	    "might take a while to refill\n");
-
-	if (opts->weak_keys_and_salt) {
-		tc_log(0, "WARNING: Using a weak random generator to get "
-		    "entropy for the key material. Odds are this is NOT "
-		    "what you want.\n");
-	}
-
-	if (opts->state_change_fn)
-		opts->state_change_fn(opts->api_ctx, "create_header", 1);
-
-	/* create encrypted headers */
-	ehdr = create_hdr((unsigned char *)pass,
-	    (opts->nkeyfiles > 0)?MAX_PASSSZ:strlen(pass),
-	    opts->prf_algo, opts->cipher_chain, blksz, blocks, VOL_RSVD_BYTES_START/blksz,
-	    blocks - (MIN_VOL_BYTES/blksz), 0, opts->weak_keys_and_salt, &ehdr_backup);
-	if (ehdr == NULL) {
-		tc_log(1, "Could not create header\n");
-		goto out;
-	}
-
-	if (opts->hidden) {
-		hehdr = create_hdr((unsigned char *)h_pass,
-		    (opts->n_hkeyfiles > 0)?MAX_PASSSZ:strlen(h_pass), opts->h_prf_algo,
-		    opts->h_cipher_chain,
-		    blksz, blocks,
-		    blocks - (VOL_RSVD_BYTES_END/blksz) - hidden_blocks,
-		    hidden_blocks, 1, opts->weak_keys_and_salt, &hehdr_backup);
-		if (hehdr == NULL) {
-			tc_log(1, "Could not create hidden volume header\n");
-			goto out;
-		}
-	}
-
-	if (opts->state_change_fn)
-		opts->state_change_fn(opts->api_ctx, "create_header", 0);
-
-	tc_log(0, "Writing volume headers to disk...\n");
-
-	if ((error = write_to_disk(opts->dev, 0, blksz, ehdr, sizeof(*ehdr))) != 0) {
-		tc_log(1, "Could not write volume header to device\n");
-		goto out;
-	}
-
-	/* Write backup header; it's offset is relative to the end */
-	if ((error = write_to_disk(opts->dev, (blocks*blksz - BACKUP_HDR_OFFSET_END),
-	    blksz, ehdr_backup, sizeof(*ehdr_backup))) != 0) {
-		tc_log(1, "Could not write backup volume header to device\n");
-		goto out;
-	}
-
-	if (opts->hidden) {
-		if ((error = write_to_disk(opts->dev, HDR_OFFSET_HIDDEN, blksz, hehdr,
-		    sizeof(*hehdr))) != 0) {
-			tc_log(1, "Could not write hidden volume header to "
-			    "device\n");
-			goto out;
-		}
-
-		/* Write backup hidden header; offset is relative to end */
-		if ((error = write_to_disk(opts->dev,
-		    (blocks*blksz - BACKUP_HDR_HIDDEN_OFFSET_END), blksz,
-		    hehdr_backup, sizeof(*hehdr_backup))) != 0) {
-			tc_log(1, "Could not write backup hidden volume "
-			    "header to device\n");
-			goto out;
-		}
-	}
-
-	/* Everything went ok */
-	tc_log(0, "All done!\n");
-
-	ret = 0;
-
-out:
-	if (pass)
-		free_safe_mem(pass);
-	if (h_pass)
-		free_safe_mem(h_pass);
-	if (pass_again)
-		free_safe_mem(pass_again);
-	if (ehdr)
-		free_safe_mem(ehdr);
-	if (hehdr)
-		free_safe_mem(hehdr);
-	if (ehdr_backup)
-		free_safe_mem(ehdr_backup);
-	if (hehdr_backup)
-		free_safe_mem(hehdr_backup);
-
-	return ret;
-}
-
-struct tcplay_info *
-info_map_common(struct tcplay_opts *opts, char *passphrase_out)
-{
-	struct tchdr_enc *ehdr, *hehdr = NULL;
-	struct tcplay_info *info, *hinfo = NULL;
-	char *pass;
-	char *h_pass;
-	int error, error2 = 0;
-	size_t sz;
-	size_t blksz;
-	disksz_t blocks;
-	int is_hidden = 0;
-	int try_empty = 0;
-	int retries;
-
-	if ((error = get_disk_info(opts->dev, &blocks, &blksz)) != 0) {
-		tc_log(1, "could not get disk information\n");
-		return NULL;
-	}
-
-	if (opts->retries < 1)
-		retries = 1;
-	else
-		retries = opts->retries;
-
-	/*
-	 * Add one retry so we can do a first try without asking for
-	 * a password if keyfiles are passed in.
-	 */
-	if (opts->interactive &&
-	    !opts->prompt_passphrase &&
-	    (opts->nkeyfiles > 0)) {
-		try_empty = 1;
-		++retries;
-	}
-
-	info = NULL;
-
-	ehdr = NULL;
-	pass = h_pass = NULL;
-
-	while ((info == NULL) && retries-- > 0)
-	{
-		pass = h_pass = NULL;
-		ehdr = hehdr = NULL;
-		info = hinfo = NULL;
-
-		if ((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
-			tc_log(1, "could not allocate safe passphrase memory\n");
-			goto out;
-		}
-
-		if (try_empty) {
-			pass[0] = '\0';
-		} else if (opts->interactive) {
-		        if ((error = read_passphrase("Passphrase: ", pass,
-			    MAX_PASSSZ, PASS_BUFSZ, opts->timeout))) {
-				tc_log(1, "could not read passphrase\n");
-				/* XXX: handle timeout differently? */
-				goto out;
-			}
-			pass[MAX_PASSSZ] = '\0';
-		} else {
-			/* In batch mode, use provided passphrase */
-			if (opts->passphrase != NULL) {
-				strncpy(pass, opts->passphrase, MAX_PASSSZ);
-				pass[MAX_PASSSZ] = '\0';
-			}
-		}
-
-		if (passphrase_out != NULL) {
-			strcpy(passphrase_out, pass);
-		}
-
-		if (opts->nkeyfiles > 0) {
-			/* Apply keyfiles to 'pass' */
-			if ((error = apply_keyfiles((unsigned char *)pass, PASS_BUFSZ,
-			    opts->keyfiles, opts->nkeyfiles))) {
-				tc_log(1, "could not apply keyfiles");
-				goto out;
-			}
-		}
-
-		if (opts->protect_hidden) {
-			if ((h_pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
-				tc_log(1, "could not allocate safe passphrase memory\n");
-				goto out;
-			}
-
-			if (opts->interactive) {
-			        if ((error = read_passphrase(
-				    "Passphrase for hidden volume: ", h_pass,
-				    MAX_PASSSZ, PASS_BUFSZ, opts->timeout))) {
-					tc_log(1, "could not read passphrase\n");
-					goto out;
-				}
-				h_pass[MAX_PASSSZ] = '\0';
-			} else {
-				/* In batch mode, use provided passphrase */
-				if (opts->h_passphrase != NULL) {
-					strncpy(h_pass, opts->h_passphrase, MAX_PASSSZ);
-					h_pass[MAX_PASSSZ] = '\0';
-				}
-			}
-
-			if (opts->n_hkeyfiles > 0) {
-				/* Apply keyfiles to 'pass' */
-				if ((error = apply_keyfiles((unsigned char *)h_pass, PASS_BUFSZ,
-				    opts->h_keyfiles, opts->n_hkeyfiles))) {
-					tc_log(1, "could not apply keyfiles");
-					goto out;
-				}
-			}
-		}
-
-		/* Always read blksz-sized chunks */
-		sz = blksz;
-
-		if (TC_FLAG_SET(opts->flags, HDR_FROM_FILE)) {
-			ehdr = (struct tchdr_enc *)read_to_safe_mem(
-			    opts->hdr_file_in, 0, &sz);
-			if (ehdr == NULL) {
-				tc_log(1, "error read hdr_enc: %s", opts->hdr_file_in);
-				goto out;
-			}
-		} else {
-			ehdr = (struct tchdr_enc *)read_to_safe_mem(
-			    (TC_FLAG_SET(opts->flags, SYS)) ? opts->sys_dev : opts->dev,
-			    (TC_FLAG_SET(opts->flags, SYS) || TC_FLAG_SET(opts->flags, FDE)) ?
-			    HDR_OFFSET_SYS :
-			    (!TC_FLAG_SET(opts->flags, BACKUP)) ? 0 : -BACKUP_HDR_OFFSET_END,
-			    &sz);
-			if (ehdr == NULL) {
-				tc_log(1, "error read hdr_enc: %s", opts->dev);
-				goto out;
-			}
-		}
-
-		if (!TC_FLAG_SET(opts->flags, SYS)) {
-			/* Always read blksz-sized chunks */
-			sz = blksz;
-
-			if (TC_FLAG_SET(opts->flags, H_HDR_FROM_FILE)) {
-				hehdr = (struct tchdr_enc *)read_to_safe_mem(
-				    opts->h_hdr_file_in, 0, &sz);
-				if (hehdr == NULL) {
-					tc_log(1, "error read hdr_enc: %s", opts->h_hdr_file_in);
-					goto out;
-				}
-			} else {
-				hehdr = (struct tchdr_enc *)read_to_safe_mem(opts->dev,
-				    (!TC_FLAG_SET(opts->flags, BACKUP)) ? HDR_OFFSET_HIDDEN :
-				    -BACKUP_HDR_HIDDEN_OFFSET_END, &sz);
-				if (hehdr == NULL) {
-					tc_log(1, "error read hdr_enc: %s", opts->dev);
-					goto out;
-				}
-			}
-		} else {
-			hehdr = NULL;
-		}
-
-		error = process_hdr(opts->dev, opts->flags, (unsigned char *)pass,
-		    (opts->nkeyfiles > 0)?MAX_PASSSZ:strlen(pass),
-		    ehdr, &info);
-
-		/*
-		 * Try to process hidden header if we have to protect the hidden
-		 * volume, or the decryption/verification of the main header
-		 * failed.
-		 */
-		if (hehdr && (error || opts->protect_hidden)) {
-			if (error) {
-				error2 = process_hdr(opts->dev, opts->flags, (unsigned char *)pass,
-				    (opts->nkeyfiles > 0)?MAX_PASSSZ:strlen(pass), hehdr,
-				    &info);
-				is_hidden = !error2;
-			} else if (opts->protect_hidden) {
-				error2 = process_hdr(opts->dev, opts->flags, (unsigned char *)h_pass,
-				    (opts->n_hkeyfiles > 0)?MAX_PASSSZ:strlen(h_pass), hehdr,
-				    &hinfo);
-			}
-		}
-
-		/* We need both to protect a hidden volume */
-		if ((opts->protect_hidden && (error || error2)) ||
-		    (error && error2)) {
-			if (!try_empty)
-				tc_log(1, "Incorrect password or not a TrueCrypt volume\n");
-
-			if (info) {
-				free_info(info);
-				info = NULL;
-			}
-			if (hinfo) {
-				free_info(hinfo);
-				hinfo = NULL;
-			}
-
-			/* Try again (or finish) */
-			free_safe_mem(pass);
-			pass = NULL;
-
-			if (h_pass) {
-				free_safe_mem(h_pass);
-				h_pass = NULL;
-			}
-			if (ehdr) {
-				free_safe_mem(ehdr);
-				ehdr = NULL;
-			}
-			if (hehdr) {
-				free_safe_mem(hehdr);
-				hehdr = NULL;
-			}
-
-			try_empty = 0;
-			continue;
-		}
-
-		if (opts->protect_hidden) {
-			if (adjust_info(info, hinfo) != 0) {
-				tc_log(1, "Could not protect hidden volume\n");
-				if (info)
-					free_info(info);
-				info = NULL;
-
-				if (hinfo)
-					free_info(hinfo);
-				hinfo = NULL;
-
-				goto out;
-			}
-
-			if (hinfo) {
-				free_info(hinfo);
-				hinfo = NULL;
-			}
-		}
-		try_empty = 0;
-        }
-
-out:
-	if (hinfo)
-		free_info(hinfo);
-	if (pass)
-		free_safe_mem(pass);
-	if (h_pass)
-		free_safe_mem(h_pass);
-	if (ehdr)
-		free_safe_mem(ehdr);
-	if (hehdr)
-		free_safe_mem(hehdr);
-
-	if (info != NULL)
-		info->hidden = is_hidden;
-
-	return info;
-}
-
-int
-info_mapped_volume(struct tcplay_opts *opts)
-{
-	struct tcplay_info *info;
-
-	info = dm_info_map(opts->map_name);
-	if (info != NULL) {
-		if (opts->interactive)
-			print_info(info);
-
-		free_info(info);
-
-		return 0;
-		/* NOT REACHED */
-	} else if (opts->interactive) {
-		tc_log(1, "Could not retrieve information about mapped "
-		    "volume %s. Does it exist?\n", opts->map_name);
-	}
-
-	return -1;
-}
-
-int
-info_volume(struct tcplay_opts *opts)
-{
-	struct tcplay_info *info;
-
-	info = info_map_common(opts, NULL);
-
-	if (info != NULL) {
-		if (opts->interactive)
-			print_info(info);
-
-		free_info(info);
-
-		return 0;
-		/* NOT REACHED */
-	}
-
-	return -1;
-}
-
-int
-map_volume(struct tcplay_opts *opts)
-{
-	struct tcplay_info *info;
-	int error;
-
-	info = info_map_common(opts, NULL);
-
-	if (info == NULL)
-		return -1;
-
-	if ((error = dm_setup(opts->map_name, info)) != 0) {
-		tc_log(1, "Could not set up mapping %s\n", opts->map_name);
-		free_info(info);
-		return -1;
-	}
-
-	if (opts->interactive)
-		printf("All ok!\n");
-
-	free_info(info);
-
-	return 0;
-}
-
-int
-modify_volume(struct tcplay_opts *opts)
-{
-	struct tcplay_info *info;
-	struct tchdr_enc *ehdr, *ehdr_backup;
-	const char *new_passphrase = opts->new_passphrase;
-	const char **new_keyfiles = opts->new_keyfiles;
-	struct pbkdf_prf_algo *new_prf_algo = opts->new_prf_algo;
-	int n_newkeyfiles = opts->n_newkeyfiles;
-	char *pass, *pass_again;
-	int ret = -1;
-	off_t offset, offset_backup = 0;
-	const char *dev;
-	size_t blksz;
-	disksz_t blocks;
-	int error;
-
-	ehdr = ehdr_backup = NULL;
-	pass = pass_again = NULL;
-	info = NULL;
-
-	if (TC_FLAG_SET(opts->flags, ONLY_RESTORE)) {
-		if (opts->interactive) {
-			if ((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
-				tc_log(1, "could not allocate safe "
-				    "passphrase memory");
-				goto out;
-			}
-		} else {
-			new_passphrase = opts->passphrase;
-		}
-		new_keyfiles = opts->keyfiles;
-		n_newkeyfiles = opts->nkeyfiles;
-		new_prf_algo = NULL;
-	}
-
-	info = info_map_common(opts, pass);
-	if (info == NULL)
-		goto out;
-
-	if (opts->interactive && !TC_FLAG_SET(opts->flags, ONLY_RESTORE)) {
-		if (((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) ||
-		   ((pass_again = alloc_safe_mem(PASS_BUFSZ)) == NULL)) {
-			tc_log(1, "could not allocate safe passphrase memory\n");
-			goto out;
-		}
-
-		if ((error = read_passphrase("New passphrase: ", pass, MAX_PASSSZ,
-		    PASS_BUFSZ, 0) ||
-		    (read_passphrase("Repeat passphrase: ", pass_again,
-		    MAX_PASSSZ, PASS_BUFSZ, 0)))) {
-			tc_log(1, "could not read passphrase\n");
-			goto out;
-		}
-
-		if (strcmp(pass, pass_again) != 0) {
-			tc_log(1, "Passphrases don't match\n");
-			goto out;
-		}
-
-		free_safe_mem(pass_again);
-		pass_again = NULL;
-	} else if (!opts->interactive) {
-		/* In batch mode, use provided passphrase */
-		if ((pass = alloc_safe_mem(PASS_BUFSZ)) == NULL) {
-			tc_log(1, "could not allocate safe "
-			    "passphrase memory");
-			goto out;
-		}
-
-		if (new_passphrase != NULL) {
-			strncpy(pass, new_passphrase, MAX_PASSSZ);
-			pass[MAX_PASSSZ] = '\0';
-		}
-	}
-
-	if (n_newkeyfiles > 0) {
-		/* Apply keyfiles to 'pass' */
-		if ((error = apply_keyfiles((unsigned char *)pass, PASS_BUFSZ,
-		    new_keyfiles, n_newkeyfiles))) {
-			tc_log(1, "could not apply keyfiles\n");
-			goto out;
-		}
-	}
-
-	ehdr = copy_reencrypt_hdr((unsigned char *)pass,
-	    (opts->n_newkeyfiles > 0)?MAX_PASSSZ:strlen(pass),
-	    new_prf_algo, opts->weak_keys_and_salt, info, &ehdr_backup);
-	if (ehdr == NULL) {
-		tc_log(1, "Could not create header\n");
-		goto out;
-	}
-
-	dev = (TC_FLAG_SET(opts->flags, SYS)) ? opts->sys_dev : opts->dev;
-	if (TC_FLAG_SET(opts->flags, SYS) || TC_FLAG_SET(opts->flags, FDE)) {
-		/* SYS and FDE don't have backup headers (as far as I understand) */
-		if (info->hidden) {
-			offset = HDR_OFFSET_HIDDEN;
-		} else {
-			offset = HDR_OFFSET_SYS;
-		}
-	} else {
-		if (info->hidden) {
-			offset = HDR_OFFSET_HIDDEN;
-			offset_backup = -BACKUP_HDR_HIDDEN_OFFSET_END;
-		} else {
-			offset = 0;
-			offset_backup = -BACKUP_HDR_OFFSET_END;
-		}
-	}
-
-	if ((error = get_disk_info(dev, &blocks, &blksz)) != 0) {
-		tc_log(1, "could not get disk information\n");
-		goto out;
-	}
-
-	tc_log(0, "Writing new volume headers to disk/file...\n");
-
-	if (TC_FLAG_SET(opts->flags, SAVE_TO_FILE)) {
-		if ((error = write_to_file(opts->hdr_file_out, ehdr, sizeof(*ehdr))) != 0) {
-			tc_log(1, "Could not write volume header to file\n");
-			goto out;
-		}
-	} else {
-		if ((error = write_to_disk(dev, offset, blksz, ehdr,
-		    sizeof(*ehdr))) != 0) {
-			tc_log(1, "Could not write volume header to device\n");
-			goto out;
-		}
-
-		if (!TC_FLAG_SET(opts->flags, SYS) && !TC_FLAG_SET(opts->flags, FDE)) {
-			if ((error = write_to_disk(dev, offset_backup, blksz,
-			    ehdr_backup, sizeof(*ehdr_backup))) != 0) {
-				tc_log(1, "Could not write backup volume header to device\n");
-				goto out;
-			}
-		}
-	}
-
-	/* Everything went ok */
-	tc_log(0, "All done!\n");
-
-	ret = 0;
-
-out:
-	if (pass)
-		free_safe_mem(pass);
-	if (pass_again)
-		free_safe_mem(pass_again);
-	if (ehdr)
-		free_safe_mem(ehdr);
-	if (ehdr_backup)
-		free_safe_mem(ehdr_backup);
-	if (info)
-		free_safe_mem(info);
-
-	return ret;
-}
-
-static
-int
-dm_get_info(const char *name, struct dm_info *dmi)
-{
-	struct dm_task *dmt = NULL;
-	int error = -1;
-
-	if ((dmt = dm_task_create(DM_DEVICE_INFO)) == NULL)
-		goto out;
-
-	if ((dm_task_set_name(dmt, name)) == 0)
-		goto out;
-
-	if ((dm_task_run(dmt)) == 0)
-		goto out;
-
-	if ((dm_task_get_info(dmt, dmi)) == 0)
-		goto out;
-
-	error = 0;
-
-out:
-	if (dmt)
-		dm_task_destroy(dmt);
-
-	return error;
-}
-
-#if defined(__DragonFly__)
-static
-int
-xlate_maj_min(const char *start_path __unused, int max_depth __unused,
-    char *buf, size_t bufsz, uint32_t maj, uint32_t min)
-{
-	dev_t dev = makedev(maj, min);
-
-	snprintf(buf, bufsz, "/dev/%s", devname(dev, S_IFCHR));
-	return 1;
-}
-#else
-static
-int
-xlate_maj_min(const char *start_path, int max_depth, char *buf, size_t bufsz,
-    uint32_t maj, uint32_t min)
-{
-	dev_t dev = makedev(maj, min);
-	char path[PATH_MAX];
-	struct stat sb;
-	struct dirent *ent;
-	DIR *dirp;
-	int found = 0;
-
-	if (max_depth <= 0)
-		return -1;
-
-	if ((dirp = opendir(start_path)) == NULL)
-		return -1;
-
-	while ((ent = readdir(dirp)) != NULL) {
-		/* d_name, d_type, DT_BLK, DT_CHR, DT_DIR, DT_LNK */
-		if (ent->d_name[0] == '.')
-			continue;
-
-		/* Linux' /dev is littered with junk, so skip over it */
-		/*
-		 * The dm-<number> devices seem to be the raw DM devices
-		 * things in mapper/ link to.
-		 */
-		if (((strcmp(ent->d_name, "block")) == 0) ||
-		    ((strcmp(ent->d_name, "fd")) == 0) ||
-                    (((strncmp(ent->d_name, "dm-", 3) == 0) && strlen(ent->d_name) <= 5)))
-			continue;
-
-		snprintf(path, PATH_MAX, "%s/%s", start_path, ent->d_name);
-
-		if ((stat(path, &sb)) < 0)
-			continue;
-
-		if (S_ISDIR(sb.st_mode)) {
-			found = !xlate_maj_min(path, max_depth-1, buf, bufsz, maj, min);
-			if (found)
-				break;
-		}
-
-		if (!S_ISBLK(sb.st_mode))
-			continue;
-
-		if (sb.st_rdev != dev)
-			continue;
-
-		snprintf(buf, bufsz, "%s", path);
-		found = 1;
-		break;
-	}
-
-	if (dirp)
-		closedir(dirp);
-
-	return found ? 0 : -ENOENT;
-}
-#endif
-
-static
-struct tcplay_dm_table *
-dm_get_table(const char *name)
-{
-	struct tcplay_dm_table *tc_table;
-	struct dm_task *dmt = NULL;
-	void *next = NULL;
-	uint64_t start, length;
-	char *target_type;
-	char *params;
-	char *p1;
-	int c = 0;
-	uint32_t maj, min;
-
-	if ((tc_table = (struct tcplay_dm_table *)alloc_safe_mem(sizeof(*tc_table))) == NULL) {
-		tc_log(1, "could not allocate safe tc_table memory\n");
-		return NULL;
-	}
-
-	if ((dmt = dm_task_create(DM_DEVICE_TABLE)) == NULL)
-		goto error;
-
-	if ((dm_task_set_name(dmt, name)) == 0)
-		goto error;
-
-	if ((dm_task_run(dmt)) == 0)
-		goto error;
-
-	tc_table->start = (off_t)0;
-	tc_table->size = (size_t)0;
-
-	do {
-		next = dm_get_next_target(dmt, next, &start, &length,
-		    &target_type, &params);
-
-		tc_table->size += (size_t)length;
-		strncpy(tc_table->target, target_type,
-		    sizeof(tc_table->target));
-
-		/* Skip any leading whitespace */
-		while (params && *params == ' ')
-			params++;
-
-		if (strcmp(target_type, "crypt") == 0) {
-			while ((p1 = strsep(&params, " ")) != NULL) {
-				/* Skip any whitespace before the next strsep */
-				while (params && *params == ' ')
-					params++;
-
-				/* Process p1 */
-				if (c == 0) {
-					/* cipher */
-					strncpy(tc_table->cipher, p1,
-					    sizeof(tc_table->cipher));
-				} else if (c == 2) {
-					/* iv offset */
-					tc_table->skip = (off_t)strtoll(p1, NULL, 10);
-				} else if (c == 3) {
-					/* major:minor */
-					maj = strtoul(p1, NULL, 10);
-					while (*p1 != ':' && *p1 != '\0')
-						p1++;
-					min = strtoul(++p1, NULL, 10);
-					if ((xlate_maj_min("/dev", 2, tc_table->device,
-					    sizeof(tc_table->device), maj, min)) != 0)
-						snprintf(tc_table->device,
-						    sizeof(tc_table->device),
-						    "%u:%u", maj, min);
-				} else if (c == 4) {
-					/* block offset */
-					tc_table->offset = (off_t)strtoll(p1,
-					    NULL, 10);
-				}
-				++c;
-			}
-
-			if (c < 5) {
-				tc_log(1, "could not get all the info required from "
-				    "the table\n");
-				goto error;
-			}
-		}
-	} while (next != NULL);
-
-	if (dmt)
-		dm_task_destroy(dmt);
-
-#ifdef DEBUG
-	printf("device: %s\n", tc_table->device);
-	printf("target: %s\n", tc_table->target);
-	printf("cipher: %s\n", tc_table->cipher);
-	printf("size:   %ju\n", tc_table->size);
-	printf("offset: %"PRId64"\n", tc_table->offset);
-	printf("skip:   %"PRId64"\n", tc_table->skip);
-#endif
-
-	return tc_table;
-
-error:
-	if (dmt)
-		dm_task_destroy(dmt);
-	if (tc_table)
-		free_safe_mem(tc_table);
-
-	return NULL;
-}
-
-struct tcplay_info *
-dm_info_map(const char *map_name)
-{
-	struct dm_task *dmt = NULL;
-	struct dm_info dmi[3];
-	struct tcplay_dm_table *dm_table[3];
-	struct tc_crypto_algo *crypto_algo;
-	struct tcplay_info *info;
-	char map[PATH_MAX];
-	char ciphers[512];
-	int i, outermost = -1;
-
-	memset(dm_table, 0, sizeof(dm_table));
-
-	if ((info = (struct tcplay_info *)alloc_safe_mem(sizeof(*info))) == NULL) {
-		tc_log(1, "could not allocate safe info memory\n");
-		return NULL;
-	}
-
-	strncpy(map, map_name, PATH_MAX);
-	for (i = 0; i < 3; i++) {
-		if ((dm_get_info(map, &dmi[i])) != 0)
-			goto error;
-
-		if (dmi[i].exists)
-			dm_table[i] = dm_get_table(map);
-
-		snprintf(map, PATH_MAX, "%s.%d", map_name, i);
-	}
-
-	if (dmt)
-		dm_task_destroy(dmt);
-
-	if (dm_table[0] == NULL)
-		goto error;
-
-	/*
-	 * Process our dmi, dm_table fun into the info structure.
-	 */
-	/* First find which cipher chain we are using */
-	ciphers[0] = '\0';
-	for (i = 0; i < 3; i++) {
-		if (dm_table[i] == NULL)
-			continue;
-
-		if (outermost < i)
-			outermost = i;
-
-		crypto_algo = &tc_crypto_algos[0];
-		while ((crypto_algo != NULL) &&
-		    (strcmp(dm_table[i]->cipher, crypto_algo->dm_crypt_str) != 0))
-			++crypto_algo;
-		if (crypto_algo == NULL) {
-			tc_log(1, "could not find corresponding cipher\n");
-			goto error;
-		}
-		strcat(ciphers, crypto_algo->name);
-		strcat(ciphers, ",");
-	}
-	ciphers[strlen(ciphers)-1] = '\0';
-
-	info->cipher_chain = check_cipher_chain(ciphers, 1);
-	if (info->cipher_chain == NULL) {
-		tc_log(1, "could not find cipher chain\n");
-		goto error;
-	}
-
-	info->cipher_chain = tc_dup_cipher_chain(info->cipher_chain);
-	if (info->cipher_chain == NULL) {
-		tc_log(1, "could not dup cipher chain\n");
-		goto error;
-	}
-
-	/* Copy over the name */
-	strncpy(info->dev, dm_table[outermost]->device, sizeof(info->dev));
-
-	/* Other fields */
-	info->hdr = NULL;
-	info->pbkdf_prf = NULL;
-	info->start = dm_table[outermost]->start;
-	info->size = dm_table[0]->size;
-	info->skip = dm_table[outermost]->skip;
-	info->offset = dm_table[outermost]->offset;
-	info->blk_sz = 512;
-
-	for (i = 0; i < 3; i++)
-		if (dm_table[i] != NULL)
-			free_safe_mem(dm_table[i]);
-
-	return info;
-
-error:
-	if (dmt)
-		dm_task_destroy(dmt);
-	if (info)
-		free_safe_mem(info);
-	for (i = 0; i < 3; i++)
-		if (dm_table[i] != NULL)
-			free_safe_mem(dm_table[i]);
-
-	return NULL;
-}
-
-static
-int
-dm_exists_device(const char *name)
-{
-	struct dm_info dmi;
-	int exists = 0;
-
-	if (dm_get_info(name, &dmi) != 0)
-		goto out;
-
-	exists = dmi.exists;
-
-out:
-	return exists;
-}
-
-static
-int
-dm_remove_device(const char *name)
-{
-	struct dm_task *dmt = NULL;
-	int ret = EINVAL;
-
-	if ((dmt = dm_task_create(DM_DEVICE_REMOVE)) == NULL)
-		goto out;
-
-	if ((dm_task_set_name(dmt, name)) == 0)
-		goto out;
-
-	if ((dm_task_run(dmt)) == 0)
-		goto out;
-
-	ret = 0;
-out:
-	if (dmt)
-		dm_task_destroy(dmt);
-
-	return ret;
-}
-
-int
-dm_setup(const char *mapname, struct tcplay_info *info)
-{
-	struct tc_cipher_chain *cipher_chain;
-	struct dm_task *dmt = NULL;
-	struct dm_info dmi;
-	char *params = NULL;
-	char *uu, *uu_temp;
-	char *uu_stack[64];
-	int uu_stack_idx;
-#if defined(__DragonFly__)
-	uint32_t status;
-#endif
-	int r, ret = 0;
-	int j, len;
-	off_t start, offset;
-	char dev[PATH_MAX];
-	char map[PATH_MAX];
-	uint32_t cookie;
-
-	dm_udev_set_sync_support(1);
-
-	if ((params = alloc_safe_mem(512)) == NULL) {
-		tc_log(1, "could not allocate safe parameters memory");
-		return ENOMEM;
-	}
-
-	strcpy(dev, info->dev);
-
-	/*
-	 * Device Mapper blocks are always 512-byte blocks, so convert
-	 * from the "native" block size to the dm block size here.
-	 */
-	start = INFO_TO_DM_BLOCKS(info, start);
-	offset = INFO_TO_DM_BLOCKS(info, offset);
-	uu_stack_idx = 0;
-
-	/*
-         * Find length of cipher chain. Could use the for below, but doesn't
-         * really matter.
-         */
-	len = tc_cipher_chain_length(info->cipher_chain);
-
-	/* Get to the end of the chain */
-	for (cipher_chain = info->cipher_chain; cipher_chain->next != NULL;
-	    cipher_chain = cipher_chain->next)
-		;
-
-	/*
-         * Start j at len-2, as we want to use .0, and the final one has no
-         * suffix.
-         */
-	for (j = len-2; cipher_chain != NULL;
-	    cipher_chain = cipher_chain->prev, j--) {
-
-		cookie = 0;
-
-		if ((dmt = dm_task_create(DM_DEVICE_CREATE)) == NULL) {
-			tc_log(1, "dm_task_create failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		/*
-		 * If this is the last element in the cipher chain, use the
-		 * final map name. Otherwise pick a secondary name...
-		 */
-		if (cipher_chain->prev == NULL)
-			strcpy(map, mapname);
-		else
-			sprintf(map, "%s.%d", mapname, j);
-
-		if ((dm_task_set_name(dmt, map)) == 0) {
-			tc_log(1, "dm_task_set_name failed\n");
-			ret = -1;
-			goto out;
-		}
-
-#if defined(__linux__)
-		uuid_generate(info->uuid);
-		if ((uu_temp = malloc(1024)) == NULL) {
-			tc_log(1, "uuid_unparse memory failed\n");
-			ret = -1;
-			goto out;
-		}
-		uuid_unparse(info->uuid, uu_temp);
-#elif defined(__DragonFly__)
-		uuid_create(&info->uuid, &status);
-		if (status != uuid_s_ok) {
-			tc_log(1, "uuid_create failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		uuid_to_string(&info->uuid, &uu_temp, &status);
-		if (uu_temp == NULL) {
-			tc_log(1, "uuid_to_string failed\n");
-			ret = -1;
-			goto out;
-		}
-#endif
-
-		if ((uu = malloc(1024)) == NULL) {
-			free(uu_temp);
-			tc_log(1, "uuid second malloc failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		snprintf(uu, 1024, "CRYPT-TCPLAY-%s", uu_temp);
-		free(uu_temp);
-
-		if ((dm_task_set_uuid(dmt, uu)) == 0) {
-			free(uu);
-			tc_log(1, "dm_task_set_uuid failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		free(uu);
-
-		if (TC_FLAG_SET(info->flags, FDE)) {
-			/*
-			 * When the full disk encryption (FDE) flag is set,
-			 * we map the first N sectors using a linear target
-			 * as they aren't encrypted.
-			 */
-
-			/*  /dev/ad0s0a              0 */
-			/* dev---^       block off --^ */
-			snprintf(params, 512, "%s 0", dev);
-
-			if ((dm_task_add_target(dmt, 0,
-				INFO_TO_DM_BLOCKS(info, offset),
-				"linear", params)) == 0) {
-				tc_log(1, "dm_task_add_target failed\n");
-				ret = -1;
-				goto out;
-			}
-
-			start = INFO_TO_DM_BLOCKS(info, offset);
-		}
-
-		/* aes-cbc-essiv:sha256 7997f8af... 0 /dev/ad0s0a 8 <opts> */
-		/*			   iv off---^  block off--^ <opts> */
-		snprintf(params, 512, "%s %s %"PRIu64 " %s %"PRIu64 " %s",
-		    cipher_chain->cipher->dm_crypt_str, cipher_chain->dm_key,
-		    (uint64_t)INFO_TO_DM_BLOCKS(info, skip), dev,
-		    (uint64_t)offset,
-		    TC_FLAG_SET(info->flags, ALLOW_TRIM) ? "1 allow_discards" : "");
-#ifdef DEBUG
-		printf("Params: %s\n", params);
-#endif
-
-		if ((dm_task_add_target(dmt, start,
-		    INFO_TO_DM_BLOCKS(info, size), "crypt", params)) == 0) {
-			tc_log(1, "dm_task_add_target failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		if ((dm_task_set_cookie(dmt, &cookie, 0)) == 0) {
-			tc_log(1, "dm_task_set_cookie failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		if ((dm_task_run(dmt)) == 0) {
-			dm_udev_wait(cookie);
-			tc_log(1, "dm_task_run failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		if ((dm_task_get_info(dmt, &dmi)) == 0) {
-			dm_udev_wait(cookie);
-			tc_log(1, "dm_task_get info failed\n");
-			ret = -1;
-			goto out;
-		}
-
-		dm_udev_wait(cookie);
-
-		if ((r = asprintf(&uu_stack[uu_stack_idx++], "%s", map)) < 0)
-			tc_log(1, "warning, asprintf failed. won't be able to "
-			    "unroll changes\n");
-
-
-		offset = 0;
-		start = 0;
-		sprintf(dev, "/dev/mapper/%s.%d", mapname, j);
-
-		dm_task_destroy(dmt);
-		dm_task_update_nodes();
-	}
-
-out:
-	/*
-	 * If an error occured, try to unroll changes made before it
-	 * happened.
-	 */
-	if (ret) {
-		j = uu_stack_idx;
-		while (j > 0) {
-#ifdef DEBUG
-			printf("Unrolling dm changes! j = %d (%s)\n", j-1,
-			    uu_stack[j-1]);
-#endif
-			if ((uu_stack[j-1] == NULL) ||
-			    ((r = dm_remove_device(uu_stack[--j])) != 0)) {
-				tc_log(1, "Tried to unroll dm changes, "
-				    "giving up.\n");
-				break;
-			}
-		}
-	}
-
-	while (uu_stack_idx > 0)
-		free(uu_stack[--uu_stack_idx]);
-
-	free_safe_mem(params);
-
-	return ret;
-}
-
-int
-dm_teardown(const char *mapname, const char *device __unused)
-{
-#if 0
-	struct dm_task *dmt = NULL;
-	struct dm_info dmi;
-#endif
-	char map[PATH_MAX];
-	int i, error;
-
-	if ((error = dm_remove_device(mapname)) != 0) {
-		tc_log(1, "Could not remove mapping %s\n", mapname);
-		return error;
-	}
-
-	/* Try to remove other cascade devices */
-	for (i = 0; i < 2; i++) {
-		sprintf(map, "%s.%d", mapname, i);
-		if (dm_exists_device(map))
-			dm_remove_device(map);
-	}
-
-	return 0;
-}
-
-struct tc_crypto_algo *
-check_cipher(const char *cipher, int quiet)
-{
-	int i, found = 0;
-
-	for (i = 0; tc_crypto_algos[i].name != NULL; i++) {
-		if (strcmp(cipher, tc_crypto_algos[i].name) == 0) {
-			found = 1;
-			break;
-		}
-	}
-
-	if (!found && !quiet) {
-		fprintf(stderr, "Valid ciphers are: ");
-		for (i = 0; tc_crypto_algos[i].name != NULL; i++)
-			fprintf(stderr, "%s ", tc_crypto_algos[i].name);
-		fprintf(stderr, "\n");
-		return NULL;
-	}
-
-	return &tc_crypto_algos[i];
-}
-
-struct tc_cipher_chain *
-check_cipher_chain(const char *cipher_chain, int quiet)
-{
-	struct tc_cipher_chain *cipher = NULL;
-	int i,k, nciphers = 0, mismatch = 0;
-	char *ciphers[8];
-	char *tmp_chain, *tmp_chain_free;
-	char *token;
-
-	if ((tmp_chain = strdup(cipher_chain)) == NULL) {
-		tc_log(1, "Could not allocate strdup memory\n");
-		return NULL;
-	}
-
-	tmp_chain_free = tmp_chain;
-
-	while ((token = strsep(&tmp_chain, ",")) != NULL)
-		ciphers[nciphers++] = token;
-
-	cipher = NULL;
-
-	for (i = 0; valid_cipher_chains[i][0] != NULL; i++) {
-		mismatch = 0;
-
-		for (k = 0; (valid_cipher_chains[i][k] != NULL); k++) {
-			/*
-			 * If there are more ciphers in the chain than in the
-			 * ciphers[] variable this is not the right chain.
-			 */
-			if (k == nciphers) {
-				mismatch = 1;
-				break;
-			}
-
-			if (strcmp(ciphers[k], valid_cipher_chains[i][k]) != 0)
-				mismatch = 1;
-		}
-
-		/*
-		 * If all ciphers matched and there are exactly nciphers,
-		 * then we found the right cipher chain.
-		 */
-		if ((k == nciphers) && !mismatch) {
-			cipher = tc_cipher_chains[i];
-			break;
-		}
-	}
-
-	if (cipher == NULL) {
-		tc_log(1, "Invalid cipher: %s\n", cipher_chain);
-		if (!quiet) {
-			fprintf(stderr, "Valid cipher chains are:\n");
-			for (i = 0; valid_cipher_chains[i][0] != NULL; i++) {
-				for (k = 0; valid_cipher_chains[i][k] != NULL;
-				    k++) {
-					fprintf(stderr, "%s%c",
-					    valid_cipher_chains[i][k],
-					    (valid_cipher_chains[i][k+1] != NULL) ?
-					    ',' : '\0');
-				}
-				fprintf(stderr, "\n");
-			}
-		}
-	}
-
-	free(tmp_chain_free);
-	return cipher;
-}
-
-struct pbkdf_prf_algo *
-check_prf_algo(const char *algo, int sys, int quiet)
-{
-	int i, found = 0;
-
-	for (i = 0; pbkdf_prf_algos[i].name != NULL; i++) {
-		if (sys != pbkdf_prf_algos[i].sys)
-			continue;
-
-		if (strcmp(algo, pbkdf_prf_algos[i].name) == 0) {
-			found = 1;
-			break;
-		}
-	}
-
-	if (!found && !quiet) {
-		fprintf(stderr, "Valid PBKDF PRF algorithms are: ");
-		for (i = 0; pbkdf_prf_algos[i].name != NULL; i++) {
-			if (sys != pbkdf_prf_algos[i].sys)
-				continue;
-			fprintf(stderr, "%s ", pbkdf_prf_algos[i].name);
-		}
-		fprintf(stderr, "\n");
-		return NULL;
-	}
-
-	return &pbkdf_prf_algos[i];
-}
-
-int
-tc_play_init(void)
-{
-	int error;
-
-	if ((error = tc_build_cipher_chains()) != 0)
-		return error;
-
-	if ((error = tc_crypto_init()) != 0)
-		return error;
-
-	return 0;
-}
-
-struct tcplay_opts *opts_init(void)
-{
-	struct tcplay_opts *opts;
-
-	if ((opts = (struct tcplay_opts *)alloc_safe_mem(sizeof(*opts))) == NULL) {
-		tc_log(1, "could not allocate safe opts memory\n");
-		return NULL;
-	}
-
-	memset(opts, 0, sizeof(*opts));
-
-	opts->retries = DEFAULT_RETRIES;
-	opts->secure_erase = 1;
-
-	return opts;
-}
-
-int
-opts_add_keyfile(struct tcplay_opts *opts, const char *keyfile)
-{
-	const char *keyf;
-
-	if (opts->nkeyfiles == MAX_KEYFILES)
-		return -1;
-
-	if ((keyf = strdup_safe_mem(keyfile)) == NULL) {
-		return -1;
-	}
-
-	opts->keyfiles[opts->nkeyfiles++] = keyf;
-
-	return 0;
-}
-
-int
-opts_add_keyfile_hidden(struct tcplay_opts *opts, const char *keyfile)
-{
-	const char *keyf;
-
-	if (opts->n_hkeyfiles == MAX_KEYFILES)
-		return -1;
-
-	if ((keyf = strdup_safe_mem(keyfile)) == NULL) {
-		return -1;
-	}
-
-	opts->h_keyfiles[opts->n_hkeyfiles++] = keyf;
-
-	return 0;
-}
-
-int
-opts_add_keyfile_new(struct tcplay_opts *opts, const char *keyfile)
-{
-	const char *keyf;
-
-	if (opts->n_newkeyfiles == MAX_KEYFILES)
-		return -1;
-
-	if ((keyf = strdup_safe_mem(keyfile)) == NULL) {
-		return -1;
-	}
-
-	opts->new_keyfiles[opts->n_newkeyfiles++] = keyf;
-
-	return 0;
-}
-
-void
-opts_clear_keyfile(struct tcplay_opts *opts)
-{
-	int i;
-
-	for (i = 0; i < opts->nkeyfiles; i++) {
-		free_safe_mem(opts->keyfiles[i]);
-	}
-
-	opts->nkeyfiles = 0;
-}
-
-void
-opts_clear_keyfile_hidden(struct tcplay_opts *opts)
-{
-	int i;
-
-	for (i = 0; i < opts->n_hkeyfiles; i++) {
-		free_safe_mem(opts->h_keyfiles[i]);
-	}
-
-	opts->n_hkeyfiles = 0;
-}
-
-
-void
-opts_clear_keyfile_new(struct tcplay_opts *opts)
-{
-	int i;
-
-	for (i = 0; i < opts->n_newkeyfiles; i++) {
-		free_safe_mem(opts->new_keyfiles[i]);
-	}
-
-	opts->n_newkeyfiles = 0;
-}
-
-
-void
-opts_free(struct tcplay_opts *opts)
-{
-	int i;
-
-	for (i = 0; i < opts->nkeyfiles; i++) {
-		free_safe_mem(opts->keyfiles[i]);
-	}
-
-	for (i = 0; i < opts->n_hkeyfiles; i++) {
-		free_safe_mem(opts->h_keyfiles[i]);
-	}
-
-	for (i = 0; i < opts->n_newkeyfiles; i++) {
-		free_safe_mem(opts->new_keyfiles[i]);
-	}
-
-	if (opts->dev)
-		free_safe_mem(opts->dev);
-	if (opts->passphrase)
-		free_safe_mem(opts->passphrase);
-	if (opts->h_passphrase)
-		free_safe_mem(opts->h_passphrase);
-	if (opts->new_passphrase)
-		free_safe_mem(opts->new_passphrase);
-	if (opts->map_name)
-		free_safe_mem(opts->map_name);
-	if (opts->sys_dev)
-		free_safe_mem(opts->sys_dev);
-	if (opts->hdr_file_in)
-		free_safe_mem(opts->hdr_file_in);
-	if (opts->h_hdr_file_in)
-		free_safe_mem(opts->h_hdr_file_in);
-	if (opts->hdr_file_out)
-		free_safe_mem(opts->hdr_file_out);
-
-	free_safe_mem(opts);
-}
diff --git a/lib/libtcplay/tcplay.h b/lib/libtcplay/tcplay.h
deleted file mode 100644
index 272361555c..0000000000
--- a/lib/libtcplay/tcplay.h
+++ /dev/null
@@ -1,352 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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.
- */
-
-/* Version of tcplay specified during build (CMakeLists.txt, Makefile.classic) */
-
-#ifndef _TCPLAY_H
-#define _TCPLAY_H
-
-#define MAX_BLKSZ		4096
-#define MAX_KEYSZ		192
-#define HDRSZ			512
-#define HDR_OFFSET_SYS		31744	/* 512 * (63 -1) */
-#define TC_SIG			"TRUE"
-#define VC_SIG			"VERA"
-#define MAX_PASSSZ		64
-#define PASS_BUFSZ		256
-#define KPOOL_SZ		64
-#define MAX_KFILE_SZ		1048576	/* 1 MB */
-#define MAX_KEYFILES		256
-#define HDR_OFFSET_HIDDEN	65536
-#define BACKUP_HDR_HIDDEN_OFFSET_END	65536
-#define BACKUP_HDR_OFFSET_END	131072
-#define SALT_LEN		64
-#define VOL_RSVD_BYTES_START	(256*512) /* Reserved bytes at vol. start */
-#define VOL_RSVD_BYTES_END	(256*512) /* Reserved bytes at vol. end */
-#define MIN_VOL_BYTES		(VOL_RSVD_BYTES_START + VOL_RSVD_BYTES_END)
-
-#define MAX_CIPHER_CHAINS	64
-#define DEFAULT_RETRIES		3
-#define ERASE_BUFFER_SIZE	4*1024*1024 /* 4 MB */
-
-/* TrueCrypt Volume flags */
-#define TC_VOLFLAG_SYSTEM	0x01	/* system encryption */
-#define TC_VOLFLAG_INPLACE	0x02	/* non-system in-place-encrypted volume */
-
-#define TC_VOLFLAG_SET(f, x)	((f & TC_VOLFLAG_##x) == TC_VOLFLAG_##x)
-
-#define LOG_BUFFER_SZ		1024
-#if 0
-#define DEBUG 1
-#endif
-
-#define TC_FLAG_SYS		0x0001
-#define TC_FLAG_FDE		0x0002
-#define TC_FLAG_BACKUP		0x0004
-#define TC_FLAG_ONLY_RESTORE	0x0008
-#define TC_FLAG_ALLOW_TRIM	0x0010
-#define TC_FLAG_SAVE_TO_FILE	0x0020
-#define TC_FLAG_HDR_FROM_FILE	0x0040
-#define TC_FLAG_H_HDR_FROM_FILE	0x0080
-
-#define TC_FLAG_SET(f, x)	((f & TC_FLAG_##x) == TC_FLAG_##x)
-
-#include <limits.h>
-#include <inttypes.h>
-
-#if defined(__DragonFly__)
-#include <uuid.h>
-#elif defined(__linux__)
-#include <uuid/uuid.h>
-#endif
-
-
-typedef uint64_t disksz_t;
-#define DISKSZ_FMT PRIu64
-
-
-struct pbkdf_prf_algo {
-	const char *name;
-	const char *algo;
-	int iteration_count;
-	const char *sig;
-	int sys;
-};
-
-#define DEFAULT_PRF_ALGO_IDX	6
-
-struct tc_crypto_algo {
-	const char *name;
-	const char *dm_crypt_str;
-	int klen;
-	int ivlen;
-};
-
-struct tc_cipher_chain {
-	struct tc_crypto_algo *cipher;
-	unsigned char *key;
-	char dm_key[MAX_KEYSZ*2 + 1];
-
-	struct tc_cipher_chain *prev;
-	struct tc_cipher_chain *next;
-};
-
-struct tchdr_enc {
-	unsigned char salt[SALT_LEN];	/* Salt for PBKDF */
-	unsigned char enc[448];		/* Encrypted part of the header */
-} __attribute__((__packed__));
-
-struct tchdr_dec {
-	char		tc_str[4];	/* ASCII string "TRUE" */
-	uint16_t	tc_ver;		/* Volume header format version */
-	uint16_t	tc_min_ver;
-	uint32_t	crc_keys;	/* CRC32 of the key section */
-	uint64_t	vol_ctime;	/* Volume creation time */
-	uint64_t	hdr_ctime;	/* Header creation time */
-	uint64_t	sz_hidvol;	/* Size of hidden volume (set to zero
-					   in non-hidden volumes) */
-	uint64_t	sz_vol;		/* Size of volume */
-	uint64_t	off_mk_scope;	/* Byte offset of the start of the
-					   master key scope */
-	uint64_t	sz_mk_scope;	/* Size of the encrypted area within
-					   the master key scope */
-	uint32_t	flags;		/* Flag bits
-					   (bit 0: system encryption;
-					   bit 1: non-system in-place-encrypted volume;
-					   bits 2–31 are reserved) */
-	uint32_t	sec_sz;		/* Sector size (in bytes) */
-	unsigned char	unused3[120];
-	uint32_t	crc_dhdr;	/* CRC32 of dec. header (except keys) */
-	unsigned char	keys[256];
-} __attribute__((__packed__));
-
-struct tcplay_info {
-	char dev[PATH_MAX];
-	struct tchdr_dec *hdr;
-	struct tc_cipher_chain *cipher_chain;
-	struct pbkdf_prf_algo *pbkdf_prf;
-	char key[MAX_KEYSZ*2 + 1];
-
-	int flags;
-	int volflags;
-
-	uint32_t blk_sz;
-
-	off_t start;	/* Logical volume offset in table (in blk_sz blocks) */
-	disksz_t size;	/* Volume size (in blk_sz blocks) */
-
-	off_t skip;	/* IV offset (in blk_sz blocks) */
-	off_t offset;	/* Block offset (in blk_sz blocks) */
-
-	/* Populated by dm_setup */
-	uuid_t uuid;
-
-	int hidden;
-};
-
-#define INFO_TO_DM_BLOCKS(info, memb) \
-    (((info)->memb * (uint64_t)((info)->blk_sz))/512)
-
-struct tcplay_dm_table {
-	char device[PATH_MAX];	/* Underlying device */
-	char target[256];	/* DM Target type */
-	off_t start;		/* Logical volume offset in table */
-	disksz_t size;		/* Volume size */
-
-	char cipher[256];	/* Cipher */
-	off_t skip;		/* IV offset */
-	off_t offset;		/* Block offset */
-};
-
-
-typedef int (*tc_state_change_fn)(void *, const char *, int);
-
-struct tcplay_opts {
-	/* (Mostly) common options */
-	const char	*dev;
-	const char	*keyfiles[MAX_KEYFILES];
-	int		nkeyfiles;
-	const char	*h_keyfiles[MAX_KEYFILES];
-	int		n_hkeyfiles;
-	struct pbkdf_prf_algo	*prf_algo;
-	struct tc_cipher_chain	*cipher_chain;
-	struct pbkdf_prf_algo	*h_prf_algo;
-	struct tc_cipher_chain	*h_cipher_chain;
-	const char	*passphrase;
-	const char	*h_passphrase;
-	int		interactive;
-	int		weak_keys_and_salt;
-
-	/* Options for create */
-	int		hidden;
-	disksz_t	hidden_size_bytes;
-	int		secure_erase; /* XXX: default to 1! */
-
-	/* Options for map, info_mapped */
-	const char	*map_name;
-
-	/* Options for info, map, modify */
-	int		flags;
-	const char	*sys_dev;
-	int		protect_hidden;
-	int		retries;	/* XXX: default to DEFAULT_RETRIES */
-	time_t		timeout;
-	int		prompt_passphrase;
-
-	const char	*hdr_file_in;
-	const char	*h_hdr_file_in;
-
-	/* Options for modify only */
-	struct pbkdf_prf_algo	*new_prf_algo;
-	const char	*new_passphrase;
-	const char	*hdr_file_out;
-	const char	*new_keyfiles[MAX_KEYFILES];
-	int		n_newkeyfiles;
-
-	void		*api_ctx;
-	tc_state_change_fn	state_change_fn;
-};
-
-
-struct tcplay_opts *opts_init(void);
-int opts_add_keyfile(struct tcplay_opts *opts, const char *keyfile);
-int opts_add_keyfile_hidden(struct tcplay_opts *opts, const char *keyfile);
-int opts_add_keyfile_new(struct tcplay_opts *opts, const char *keyfile);
-void opts_free(struct tcplay_opts *opts);
-void opts_clear_keyfile(struct tcplay_opts *opts);
-void opts_clear_keyfile_hidden(struct tcplay_opts *opts);
-void opts_clear_keyfile_new(struct tcplay_opts *opts);
-
-void *read_to_safe_mem(const char *file, off_t offset, size_t *sz);
-int get_random(unsigned char *buf, size_t len, int weak);
-int secure_erase(const char *dev, disksz_t bytes, size_t blksz);
-int get_disk_info(const char *dev, disksz_t *blocks, size_t *bsize);
-int write_to_disk(const char *dev, off_t offset, size_t blksz, void *mem,
-    size_t bytes);
-int write_to_file(const char *file, void *mem, size_t bytes);
-int read_passphrase(const char *prompt, char *pass, size_t passlen,
-    size_t bufsz, time_t timeout);
-float get_random_read_progress(void);
-float get_secure_erase_progress(void);
-
-
-int tc_crypto_init(void);
-int tc_cipher_chain_populate_keys(struct tc_cipher_chain *cipher_chain,
-    unsigned char *key);
-int tc_cipher_chain_free_keys(struct tc_cipher_chain *cipher_chain);
-int tc_encrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
-    unsigned char *iv,
-    unsigned char *in, int in_len, unsigned char *out);
-int tc_decrypt(struct tc_cipher_chain *cipher_chain, unsigned char *key,
-    unsigned char *iv,
-    unsigned char *in, int in_len, unsigned char *out);
-
-/* The following two are platform dependent */
-int syscrypt(struct tc_crypto_algo *cipher, unsigned char *key, size_t klen,
-    unsigned char *iv, unsigned char *in, unsigned char *out, size_t len,
-    int do_encrypt);
-int pbkdf2(struct pbkdf_prf_algo *hash, const char *pass, int passlen,
-    const unsigned char *salt, int saltlen,
-    int keylen, unsigned char *out);
-
-int apply_keyfiles(unsigned char *pass, size_t pass_memsz, const char *keyfiles[],
-    int nkeyfiles);
-
-struct tchdr_enc *create_hdr(unsigned char *pass, int passlen,
-    struct pbkdf_prf_algo *prf_algo, struct tc_cipher_chain *cipher_chain,
-    size_t sec_sz, disksz_t total_blocks,
-    off_t offset, disksz_t blocks, int hidden, int weak,
-    struct tchdr_enc **backup_hdr);
-struct tchdr_dec *decrypt_hdr(struct tchdr_enc *ehdr,
-    struct tc_cipher_chain *cipher_chain, unsigned char *key);
-int verify_hdr(struct tchdr_dec *hdr, struct pbkdf_prf_algo *prf_algo);
-struct tchdr_enc *copy_reencrypt_hdr(unsigned char *pass, int passlen,
-    struct pbkdf_prf_algo *prf_algo, int weak, struct tcplay_info *info,
-    struct tchdr_enc **backup_hdr);
-
-void *_alloc_safe_mem(size_t req_sz, const char *file, int line);
-void *_strdup_safe_mem(const char *in, const char *file, int line);
-void _free_safe_mem(void *mem, const char *file, int line);
-void check_and_purge_safe_mem(void);
-
-struct tc_crypto_algo *check_cipher(const char *cipher, int quiet);
-struct tc_cipher_chain *check_cipher_chain(const char *cipher_chain, int quiet);
-struct pbkdf_prf_algo *check_prf_algo(const char *algo, int sys, int quiet);
-
-int tc_play_init(void);
-void tc_log(int err, const char *fmt, ...);
-int tc_cipher_chain_klen(struct tc_cipher_chain *chain);
-int tc_cipher_chain_length(struct tc_cipher_chain *chain);
-char *tc_cipher_chain_sprint(char *buf, size_t bufsz,
-    struct tc_cipher_chain *chain);
-int free_info(struct tcplay_info *info);
-void print_info(struct tcplay_info *info);
-int adjust_info(struct tcplay_info *info, struct tcplay_info *hinfo);
-int process_hdr(const char *dev, int flags, unsigned char *pass, int passlen,
-    struct tchdr_enc *ehdr, struct tcplay_info **pinfo);
-int create_volume(struct tcplay_opts *opts);
-struct tcplay_info *info_map_common(struct tcplay_opts *opts,
-    char *passphrase_out);
-int info_mapped_volume(struct tcplay_opts *opts);
-int info_volume(struct tcplay_opts *opts);
-int map_volume(struct tcplay_opts *opts);
-int modify_volume(struct tcplay_opts *opts);
-int dm_setup(const char *mapname, struct tcplay_info *info);
-int dm_teardown(const char *mapname, const char *device);
-struct tcplay_info *dm_info_map(const char *map_name);
-
-typedef void(*summary_fn_t)(void);
-
-extern int tc_internal_verbose;
-extern char tc_internal_log_buffer[];
-extern summary_fn_t summary_fn;
-extern struct pbkdf_prf_algo pbkdf_prf_algos[];
-extern struct tc_cipher_chain *tc_cipher_chains[MAX_CIPHER_CHAINS];
-
-#define STATE_UNKNOWN		0
-#define STATE_GET_RANDOM	1
-#define STATE_ERASE		2
-
-extern int tc_internal_state;
-#ifndef	__DECONST
-#define	__DECONST(type, var)	((type)(uintptr_t)(const void *)(var))
-#endif
-
-#define alloc_safe_mem(x) \
-	_alloc_safe_mem(x, __FILE__, __LINE__)
-
-#define strdup_safe_mem(x) \
-	_strdup_safe_mem(x, __FILE__, __LINE__)
-
-#define free_safe_mem(x) \
-	_free_safe_mem(__DECONST(void *, x), __FILE__, __LINE__)
-
-#define __unused       __attribute__((__unused__))
-
-#endif
diff --git a/lib/libtcplay/tcplay.map b/lib/libtcplay/tcplay.map
deleted file mode 100644
index 6f8f377b7d..0000000000
--- a/lib/libtcplay/tcplay.map
+++ /dev/null
@@ -1,15 +0,0 @@
-{
-global:
-	tc_api_init;
-	tc_api_uninit;
-	tc_api_has;
-	tc_api_cipher_iterate;
-	tc_api_prf_iterate;
-	tc_api_task_init;
-	tc_api_task_uninit;
-	tc_api_task_set;
-	tc_api_task_do;
-	tc_api_task_info_get;
-local:
-	*;
-};
diff --git a/lib/libtcplay/tcplay_api.c b/lib/libtcplay/tcplay_api.c
deleted file mode 100644
index 0a02665d33..0000000000
--- a/lib/libtcplay/tcplay_api.c
+++ /dev/null
@@ -1,806 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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 <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdarg.h>
-
-#include "tcplay.h"
-#include "tcplay_api.h"
-#include "tcplay_api_internal.h"
-
-
-int
-tc_api_init(int verbose)
-{
-	int error;
-
-	tc_internal_verbose = verbose;
-
-	if ((error = tc_play_init()) != 0)
-		return TC_ERR;
-	else
-		return TC_OK;
-}
-
-int
-tc_api_uninit(void)
-{
-	check_and_purge_safe_mem();
-	return TC_OK;
-}
-
-
-static const char *_caps[] = {
-	"trim",
-	NULL
-};
-
-int
-tc_api_has(const char *feature)
-{
-	const char *cap;
-	int i;
-
-	for (cap = _caps[0], i = 0; cap != NULL; cap = _caps[++i]) {
-		if ((strcmp(cap, feature)) == 0)
-			return TC_OK;
-	}
-
-	return TC_ERR_UNIMPL;
-}
-
-int
-tc_api_cipher_iterate(tc_api_cipher_iterator_fn fn, void *priv)
-{
-	int i;
-	struct tc_cipher_chain *chain;
-	int klen;
-	int length;
-	char buf[1024];
-
-	if (fn == NULL) {
-		errno = EFAULT;
-		return TC_ERR;
-	}
-
-	for (i = 0, chain = tc_cipher_chains[0]; chain != NULL;
-	     chain = tc_cipher_chains[++i]) {
-		tc_cipher_chain_sprint(buf, sizeof(buf), chain);
-		klen = tc_cipher_chain_klen(chain);
-		length = tc_cipher_chain_length(chain);
-		if ((fn(priv, buf, klen, length)) < 0)
-			break;
-	}
-
-	return TC_OK;
-}
-
-int
-tc_api_prf_iterate(tc_api_prf_iterator_fn fn, void *priv)
-{
-	int i;
-
-	if (fn == NULL) {
-		errno = EFAULT;
-		return TC_ERR;
-	}
-
-	for (i = 0; pbkdf_prf_algos[i].name != NULL; i++) {
-		/* Skip over sys PRFs */
-		if (pbkdf_prf_algos[i].sys)
-			continue;
-
-		if ((fn(priv, pbkdf_prf_algos[i].name)) < 0)
-			break;
-	}
-
-	return TC_OK;
-}
-
-
-const char *
-tc_api_task_get_error(tc_api_task task __unused)
-{
-	return tc_internal_log_buffer;
-}
-
-
-#define _match(k, v) (strcmp(k, v) == 0)
-
-tc_api_task
-tc_api_task_init(const char *op)
-{
-	tc_api_task task = NULL;
-	int fail = 1;
-
-	if ((task = alloc_safe_mem(sizeof(*task))) == NULL) {
-		errno = ENOMEM;
-		goto out;
-	}
-
-	if ((task->opts = opts_init()) == NULL) {
-		errno = ENOMEM;
-		goto out;
-	}
-
-	if (_match(op, "create")) {
-		task->op = TC_OP_CREATE;
-	} else if (_match(op, "map")) {
-		task->op = TC_OP_MAP;
-	} else if (_match(op, "unmap")) {
-		task->op = TC_OP_UNMAP;
-	} else if (_match(op, "info")) {
-		task->op = TC_OP_INFO;
-	} else if (_match(op, "info_mapped")) {
-		task->op = TC_OP_INFO_MAPPED;
-	} else if (_match(op, "modify")) {
-		task->op = TC_OP_MODIFY;
-	} else if (_match(op, "restore")) {
-		task->op = TC_OP_RESTORE;
-	} else {
-		errno = EINVAL;
-		goto out;
-	}
-
-	fail = 0;
-
-out:
-	if (fail && task != NULL) {
-		if (task->opts != NULL)
-			opts_free(task->opts);
-		free_safe_mem(task);
-	}
-
-	return fail ? NULL : task;
-}
-
-int
-tc_api_task_uninit(tc_api_task task)
-{
-	if (task->last_info != NULL)
-		free_info(task->last_info);
-	opts_free(task->opts);
-	free_safe_mem(task);
-
-	return TC_OK;
-}
-
-
-#define _set_str(k) \
-	do {							\
-		if ((opts->k = strdup_safe_mem(s)) == NULL) {	\
-			errno = ENOMEM;				\
-			r = TC_ERR;				\
-			goto out;				\
-		}						\
-	} while (0)
-
-#define _clr_str(k) \
-	do {							\
-		if (opts->k)					\
-			free_safe_mem(opts->k);			\
-		opts->k = NULL;					\
-	} while (0)
-
-int
-tc_api_task_set(tc_api_task task, const char *key, ...)
-{
-	struct tcplay_opts *opts;
-	va_list ap;
-	const char *s;
-	int64_t i64;
-	int i;
-	tc_api_state_change_fn sc_fn;
-	void *vp;
-	int r = TC_OK;
-
-	if (task == NULL || key == NULL || ((opts = task->opts) == NULL)) {
-		errno = EFAULT;
-		return TC_ERR;
-	}
-
-	va_start(ap, key);
-
-	if (_match(key, "interactive")) {
-		i = va_arg(ap, int);
-		opts->interactive = i;
-	} else if (_match(key, "weak_keys_and_salt")) {
-		i = va_arg(ap, int);
-		opts->weak_keys_and_salt = i;
-	} else if (_match(key, "secure_erase")) {
-		i = va_arg(ap, int);
-		opts->secure_erase = i;
-	} else if (_match(key, "protect_hidden")) {
-		i = va_arg(ap, int);
-		opts->protect_hidden = i;
-	} else if (_match(key, "fde")) {
-		i = va_arg(ap, int);
-		if (i)
-			opts->flags |= TC_FLAG_FDE;
-		else
-			opts->flags &= ~TC_FLAG_FDE;
-	} else if (_match(key, "use_backup_header")) {
-		i = va_arg(ap, int);
-		if (i)
-			opts->flags |= TC_FLAG_BACKUP;
-		else
-			opts->flags &= ~TC_FLAG_BACKUP;
-	} else if (_match(key, "allow_trim")) {
-		i = va_arg(ap, int);
-		if (i)
-			opts->flags |= TC_FLAG_ALLOW_TRIM;
-		else
-			opts->flags &= ~TC_FLAG_ALLOW_TRIM;
-	} else if (_match(key, "hidden_size_bytes")) {
-		i64 = va_arg(ap, int64_t);
-		opts->hidden_size_bytes = (disksz_t)i64;
-		opts->hidden = (i64 > 0);
-	} else if (_match(key, "retries")) {
-		i = va_arg(ap, int);
-		opts->retries = i;
-	} else if (_match(key, "timeout")) {
-		i = va_arg(ap, int);
-		opts->timeout = (time_t)i;
-	} else if (_match(key, "save_header_to_file")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(hdr_file_out);
-			opts->flags |= TC_FLAG_SAVE_TO_FILE;
-		} else {
-			_clr_str(hdr_file_out);
-			opts->flags &= ~TC_FLAG_SAVE_TO_FILE;
-		}
-	} else if (_match(key, "header_from_file")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(hdr_file_in);
-			opts->flags |= TC_FLAG_HDR_FROM_FILE;
-		} else {
-			_clr_str(hdr_file_in);
-			opts->flags &= ~TC_FLAG_HDR_FROM_FILE;
-		}
-	} else if (_match(key, "hidden_header_from_file")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(h_hdr_file_in);
-			opts->flags |= TC_FLAG_H_HDR_FROM_FILE;
-		} else {
-			_clr_str(h_hdr_file_in);
-			opts->flags &= ~TC_FLAG_H_HDR_FROM_FILE;
-		}
-	} else if (_match(key, "sys")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(sys_dev);
-			opts->flags |= TC_FLAG_SYS;
-		} else {
-			_clr_str(sys_dev);
-			opts->flags &= ~TC_FLAG_SYS;
-		}
-	} else if (_match(key, "passphrase")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(passphrase);
-		} else {
-			_clr_str(passphrase);
-		}
-	} else if (_match(key, "h_passphrase")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(h_passphrase);
-		} else {
-			_clr_str(h_passphrase);
-		}
-	} else if (_match(key, "new_passphrase")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(new_passphrase);
-		} else {
-			_clr_str(new_passphrase);
-		}
-	} else if (_match(key, "dev")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(dev);
-		} else {
-			_clr_str(dev);
-		}
-	} else if (_match(key, "map_name")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			_set_str(map_name);
-		} else {
-			_clr_str(map_name);
-		}
-	} else if (_match(key, "keyfiles")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			opts_add_keyfile(opts, s);
-		} else {
-			opts_clear_keyfile(opts);
-		}
-	} else if (_match(key, "h_keyfiles")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			opts_add_keyfile_hidden(opts, s);
-		} else {
-			opts_clear_keyfile_hidden(opts);
-		}
-	} else if (_match(key, "new_keyfiles")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			opts_add_keyfile_new(opts, s);
-		} else {
-			opts_clear_keyfile_new(opts);
-		}
-	} else if (_match(key, "prf_algo")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			if ((opts->prf_algo = check_prf_algo(s, 0, 1)) == NULL) {
-				errno = ENOENT;
-				r = TC_ERR;
-				goto out;
-			}
-		} else {
-			opts->prf_algo = NULL;
-		}
-	} else if (_match(key, "h_prf_algo")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			if ((opts->h_prf_algo = check_prf_algo(s, 0, 1)) == NULL) {
-				errno = ENOENT;
-				r = TC_ERR;
-				goto out;
-			}
-		} else {
-			opts->h_prf_algo = NULL;
-		}
-	} else if (_match(key, "new_prf_algo")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			if ((opts->new_prf_algo = check_prf_algo(s, 0, 1)) == NULL) {
-				errno = ENOENT;
-				r = TC_ERR;
-				goto out;
-			}
-		} else {
-			opts->new_prf_algo = NULL;
-		}
-	} else if (_match(key, "cipher_chain")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			if ((opts->cipher_chain = check_cipher_chain(s, 1)) == NULL) {
-				errno = ENOENT;
-				r = TC_ERR;
-				goto out;
-			}
-		} else {
-			opts->cipher_chain = NULL;
-		}
-	} else if (_match(key, "h_cipher_chain")) {
-		s = va_arg(ap, const char *);
-		if (s != NULL) {
-			if ((opts->h_cipher_chain = check_cipher_chain(s, 1)) == NULL) {
-				errno = ENOENT;
-				r = TC_ERR;
-				goto out;
-			}
-		} else {
-			opts->h_cipher_chain = NULL;
-		}
-	} else if (_match(key, "state_change_fn")) {
-		sc_fn = va_arg(ap, tc_api_state_change_fn);
-		opts->state_change_fn = sc_fn;
-		vp = va_arg(ap, void *);
-		opts->api_ctx = vp;
-	} else {
-		r = TC_ERR_UNIMPL;
-	}
-
-out:
-	va_end(ap);
-
-	return r;
-}
-
-#define _not_null(x) \
-	if (opts->x == NULL) {	\
-		return -1;	\
-	}
-
-#define _null(x) \
-	if (opts->x != NULL) {	\
-		return -1;	\
-	}
-
-#define _zero(x) \
-	if (opts->x != 0) {	\
-		return -1;	\
-	}
-
-#define _not_set(x) \
-	if (TC_FLAG_SET(opts->flags, x)) {	\
-		return -1;			\
-	}
-
-static
-int
-_opts_check_create(struct tcplay_opts *opts)
-{
-	_not_null(dev);
-	_not_set(SYS);
-	_not_set(FDE);
-	_not_set(BACKUP);
-	_not_set(ONLY_RESTORE);
-	_not_set(ALLOW_TRIM);
-	_not_set(SAVE_TO_FILE);
-	_not_set(HDR_FROM_FILE);
-	_not_set(H_HDR_FROM_FILE);
-
-	_null(map_name);
-	_zero(protect_hidden);
-	_null(new_passphrase);
-	_null(new_prf_algo);
-	_zero(n_newkeyfiles);
-
-	if (opts->hidden_size_bytes && !opts->hidden) {
-		return -1;
-	}
-
-	return 0;
-}
-
-static
-int
-_opts_check_map(struct tcplay_opts *opts)
-{
-	_not_null(dev);
-	_not_null(map_name);
-	_not_set(ONLY_RESTORE);
-	_not_set(SAVE_TO_FILE);
-	_zero(hidden);
-	_zero(hidden_size_bytes);
-	_null(new_passphrase);
-	_null(new_prf_algo);
-	_zero(n_newkeyfiles);
-	_null(prf_algo);
-	_null(h_prf_algo);
-	_null(cipher_chain);
-	_null(h_cipher_chain);
-
-	if (!opts->protect_hidden) {
-		_zero(n_hkeyfiles);
-		//_null(h_passphrase);
-	}
-
-	return 0;
-}
-
-static
-int
-_opts_check_unmap(struct tcplay_opts *opts)
-{
-	_not_null(map_name);
-	/* XXX: _not_null(dev); ? */
-	_zero(nkeyfiles);
-	_zero(n_hkeyfiles);
-	_null(prf_algo);
-	_null(cipher_chain);
-	_null(h_prf_algo);
-	_null(h_cipher_chain);
-	_null(passphrase);
-	_null(h_passphrase);
-	_zero(hidden);
-	_zero(protect_hidden);
-	_null(new_prf_algo);
-	_null(new_passphrase);
-	_zero(n_newkeyfiles);
-	_not_set(SYS);
-	_not_set(FDE);
-	_not_set(BACKUP);
-	_not_set(ONLY_RESTORE);
-	_not_set(ALLOW_TRIM);
-	_not_set(SAVE_TO_FILE);
-	_not_set(HDR_FROM_FILE);
-	_not_set(H_HDR_FROM_FILE);
-
-	return 0;
-}
-
-static
-int
-_opts_check_info(struct tcplay_opts *opts)
-{
-	_not_null(dev);
-	_null(map_name);
-	_not_set(ONLY_RESTORE);
-	_not_set(SAVE_TO_FILE);
-	_zero(hidden);
-	_zero(hidden_size_bytes);
-	_null(new_passphrase);
-	_null(new_prf_algo);
-	_zero(n_newkeyfiles);
-	_null(prf_algo);
-	_null(h_prf_algo);
-	_null(cipher_chain);
-	_null(h_cipher_chain);
-
-	if (!opts->protect_hidden) {
-		_zero(n_hkeyfiles);
-		//_null(h_passphrase);
-	}
-
-	return 0;
-}
-
-static
-int
-_opts_check_info_mapped(struct tcplay_opts *opts)
-{
-	_not_null(map_name);
-	/* XXX: _not_null(dev); ? */
-	_zero(nkeyfiles);
-	_zero(n_hkeyfiles);
-	_null(prf_algo);
-	_null(cipher_chain);
-	_null(h_prf_algo);
-	_null(h_cipher_chain);
-	_null(passphrase);
-	_null(h_passphrase);
-	_zero(hidden);
-	_zero(protect_hidden);
-	_null(new_prf_algo);
-	_null(new_passphrase);
-	_zero(n_newkeyfiles);
-	_not_set(SYS);
-	_not_set(FDE);
-	_not_set(BACKUP);
-	_not_set(ONLY_RESTORE);
-	_not_set(ALLOW_TRIM);
-	_not_set(SAVE_TO_FILE);
-	_not_set(HDR_FROM_FILE);
-	_not_set(H_HDR_FROM_FILE);
-
-	return 0;
-}
-
-static
-int
-_opts_check_modify(struct tcplay_opts *opts)
-{
-	_not_null(dev);
-	_null(map_name);
-	_zero(hidden);
-	_zero(hidden_size_bytes);
-	_null(prf_algo);
-	_null(h_prf_algo);
-	_null(cipher_chain);
-	_null(h_cipher_chain);
-
-	if (!opts->protect_hidden) {
-		_zero(n_hkeyfiles);
-		_null(h_passphrase);
-	}
-
-	return 0;
-}
-
-
-static
-int
-_opts_check_restore(struct tcplay_opts *opts)
-{
-	if ((_opts_check_modify(opts)) < 0)
-		return -1;
-
-	_null(new_prf_algo);
-	_zero(n_newkeyfiles);
-	_null(new_passphrase);
-
-	return 0;
-}
-
-int
-tc_api_task_do(tc_api_task task)
-{
-	struct tcplay_opts *opts;
-	int r = TC_OK;
-
-	if (task == NULL || ((opts = task->opts) == NULL)) {
-		errno = EFAULT;
-		return TC_ERR;
-	}
-
-	if (task->last_info != NULL) {
-		free_info(task->last_info);
-	}
-
-	switch (task->op) {
-	case TC_OP_CREATE:
-		if ((r = _opts_check_create(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		r = create_volume(opts);
-		break;
-
-	case TC_OP_MAP:
-		if ((r = _opts_check_map(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		r = map_volume(opts);
-		break;
-
-	case TC_OP_UNMAP:
-		if ((r = _opts_check_unmap(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		r = dm_teardown(opts->map_name, opts->dev);
-		break;
-
-	case TC_OP_INFO:
-		if ((r = _opts_check_info(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		if ((task->last_info = info_map_common(opts, NULL)) == NULL) {
-			r = TC_ERR;
-		}
-		break;
-
-	case TC_OP_INFO_MAPPED:
-		if ((r = _opts_check_info_mapped(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		if ((task->last_info = dm_info_map(opts->map_name)) == NULL) {
-			r = TC_ERR;
-		}
-		break;
-
-	case TC_OP_MODIFY:
-		if ((r = _opts_check_modify(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		r = modify_volume(opts);
-		break;
-
-	case TC_OP_RESTORE:
-		if ((r = _opts_check_restore(task->opts)) != 0) {
-			errno = EINVAL;
-			return r;
-		}
-		opts->flags |= TC_FLAG_ONLY_RESTORE;
-		r = modify_volume(opts);
-		opts->flags &= ~TC_FLAG_ONLY_RESTORE;
-		break;
-	}
-
-	return r;
-}
-
-
-int
-tc_api_task_info_get(tc_api_task task, const char *key, ...)
-{
-	char buf[1024];
-	va_list ap;
-	struct tcplay_info *info;
-	char *s;
-	int *ip;
-	int64_t *i64p;
-	int r = TC_OK;
-	size_t sz;
-
-	if (task == NULL || ((info = task->last_info) == NULL)) {
-		errno = EFAULT;
-		return TC_ERR;
-	}
-
-	va_start(ap, key);
-	sz = va_arg(ap, size_t);
-	if (sz < 1) {
-		errno = EINVAL;
-		r = TC_ERR;
-		goto out;
-	}
-
-	if (_match(key, "device")) {
-		s = va_arg(ap, char *);
-		strncpy(s, info->dev, sz);
-		s[sz-1] = '\0';
-	} else if (_match(key, "cipher")) {
-		s = va_arg(ap, char *);
-		tc_cipher_chain_sprint(buf, sizeof(buf), info->cipher_chain);
-		strncpy(s, buf, sz);
-		s[sz-1] = '\0';
-	} else if (_match(key, "prf")) {
-		s = va_arg(ap, char *);
-		if (info->pbkdf_prf)
-			strncpy(s, info->pbkdf_prf->name, sz);
-		else
-			strncpy(s, "(unknown)", sz);
-		s[sz-1] = '\0';
-	} else if (_match(key, "key_bits")) {
-		if (sz != sizeof(int)) {
-			errno = EFAULT;
-			r = TC_ERR;
-			goto out;
-		}
-		ip = va_arg(ap, int *);
-		*ip = 8*tc_cipher_chain_klen(info->cipher_chain);
-	} else if (_match(key, "size")) {
-		if (sz != sizeof(int64_t)) {
-			errno = EFAULT;
-			r = TC_ERR;
-			goto out;
-		}
-		i64p = va_arg(ap, int64_t *);
-		if (info->hdr)
-			*i64p = (int64_t)info->size * (int64_t)info->hdr->sec_sz;
-		else
-			*i64p = (int64_t)info->size * (int64_t)info->blk_sz;
-	} else if (_match(key, "iv_offset")) {
-		if (sz != sizeof(int64_t)) {
-			errno = EFAULT;
-			r = TC_ERR;
-			goto out;
-		}
-		i64p = va_arg(ap, int64_t *);
-		if (info->hdr)
-			*i64p = (int64_t)info->skip * (int64_t)info->hdr->sec_sz;
-		else
-			*i64p = (int64_t)info->skip * (int64_t)info->blk_sz;
-	} else if (_match(key, "block_offset")) {
-		if (sz != sizeof(int64_t)) {
-			errno = EFAULT;
-			r = TC_ERR;
-			goto out;
-		}
-		i64p = va_arg(ap, int64_t *);
-		if (info->hdr)
-			*i64p = (int64_t)info->offset * (int64_t)info->hdr->sec_sz;
-		else
-			*i64p = (int64_t)info->offset * (int64_t)info->blk_sz;
-	} else {
-		r = TC_ERR_UNIMPL;
-	}
-
-out:
-	va_end(ap);
-
-	return r;
-}
diff --git a/lib/libtcplay/tcplay_api.h b/lib/libtcplay/tcplay_api.h
deleted file mode 100644
index 40a5ebc974..0000000000
--- a/lib/libtcplay/tcplay_api.h
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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.
- */
-
-#ifndef _TCPLAY_API_H
-#define _TCPLAY_API_H
-
-#include <stddef.h>
-
-#define TC_OK		0
-#define TC_ERR		-1
-#define TC_ERR_UNIMPL	-255
-
-#define TC_STATE_ENTER	1
-#define TC_STATE_EXIT	0
-
-struct _tc_api_task;
-
-typedef struct _tc_api_task *tc_api_task;
-
-typedef int (*tc_api_cipher_iterator_fn)(void *, const char *, int /* klen */, int /* length */);
-typedef int (*tc_api_prf_iterator_fn)(void *, const char *);
-typedef int (*tc_api_state_change_fn)(void *, const char *, int);
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-int tc_api_init(int verbose);
-int tc_api_uninit(void);
-
-int tc_api_has(const char *feature);
-
-int tc_api_cipher_iterate(tc_api_cipher_iterator_fn fn, void *priv);
-int tc_api_prf_iterate(tc_api_prf_iterator_fn fn, void *priv);
-
-
-tc_api_task tc_api_task_init(const char *op);
-int tc_api_task_uninit(tc_api_task task);
-int tc_api_task_set(tc_api_task task, const char *key, ...);
-int tc_api_task_do(tc_api_task task);
-
-int tc_api_task_info_get(tc_api_task task, const char *key, ...);
-const char *tc_api_task_get_error(tc_api_task task);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/lib/libtcplay/tcplay_api_internal.h b/lib/libtcplay/tcplay_api_internal.h
deleted file mode 100644
index fb0717c695..0000000000
--- a/lib/libtcplay/tcplay_api_internal.h
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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.
- */
-
-#ifndef _TCPLAY_API_INTERNAL_H
-#define _TCPLAY_API_INTERNAL_H
-
-#include <stddef.h>
-#include "tcplay.h"
-
-typedef enum tc_api_op {
-	TC_OP_CREATE,
-	TC_OP_MAP,
-	TC_OP_UNMAP,
-	TC_OP_INFO,
-	TC_OP_INFO_MAPPED,
-	TC_OP_MODIFY,
-	TC_OP_RESTORE
-} tc_api_op;
-
-struct _tc_api_task {
-	tc_api_op		op;
-	struct tcplay_opts	*opts;
-	struct tcplay_info	*last_info;
-};
-
-#endif
diff --git a/sbin/Makefile b/sbin/Makefile
index 7c277bb4f4..02f150423b 100644
--- a/sbin/Makefile
+++ b/sbin/Makefile
@@ -97,7 +97,6 @@ SUBDIR=	adjkerntz \
 	svc \
 	swapon \
 	sysctl \
-	tcplay \
 	tunefs \
 	udevd \
 	umount \
diff --git a/sbin/cryptdisks/Makefile b/sbin/cryptdisks/Makefile
index 3af45a0ea9..784a030617 100644
--- a/sbin/cryptdisks/Makefile
+++ b/sbin/cryptdisks/Makefile
@@ -4,8 +4,8 @@ NOSHARED=	YES
 
 SRCS+=		cryptdisks.c safe_mem.c
 
-LDADD=		-lcryptsetup -lluks -ldm -ltcplay -lprop -lutil
-DPADD=		${LIBCRYPTSETUP} ${LIBLUKS} ${LIBDM} ${LIBTCPLAY}
+LDADD=		-lcryptsetup -lluks -ldm -lprop -lutil
+DPADD=		${LIBCRYPTSETUP} ${LIBLUKS} ${LIBDM}
 DPADD+=		${LIBPROP} ${LIBUTIL}
 
 DPADD+=		${LIBCRYPTO}
diff --git a/sbin/cryptdisks/cryptdisks.8 b/sbin/cryptdisks/cryptdisks.8
index f444914a22..e999dc8628 100644
--- a/sbin/cryptdisks/cryptdisks.8
+++ b/sbin/cryptdisks/cryptdisks.8
@@ -50,8 +50,7 @@ file and sets up all crypto volumes when
 is specified and closes them when
 .Fl 0
 is specified.
-The device will be treated as a LUKS device if it has a LUKS header,
-otherwise it will be treated as a TrueCrypt/tcplay device.
+The device will be treated as a LUKS device if it has a LUKS header.
 .Sh FILES
 .Bl -tag -width ".Pa /etc/crypttab" -compact
 .It Pa /etc/crypttab
@@ -60,7 +59,6 @@ configuration file of local crypto volumes
 .Sh SEE ALSO
 .Xr crypttab 5 ,
 .Xr cryptsetup 8 ,
-.Xr tcplay 8
 .Sh HISTORY
 The
 .Nm
diff --git a/sbin/cryptdisks/cryptdisks.c b/sbin/cryptdisks/cryptdisks.c
index 573cf6547a..f7874ca5f3 100644
--- a/sbin/cryptdisks/cryptdisks.c
+++ b/sbin/cryptdisks/cryptdisks.c
@@ -41,7 +41,6 @@
 #include <err.h>
 
 #include <libcryptsetup.h>
-#include <tcplay_api.h>
 
 #include "safe_mem.h"
 
@@ -284,7 +283,6 @@ static int
 entry_parser(char **tokens, char **options, int type)
 {
 	struct crypt_options co;
-	tc_api_task tcplay_task;
 	struct generic_opts go;
 	int r, i, error, isluks;
 
@@ -311,17 +309,14 @@ entry_parser(char **tokens, char **options, int type)
 	generic_opts_to_luks(&co, &go);
 
 	/*
-	 * Check whether the device is a LUKS-formatted device; otherwise
-	 * we assume its a TrueCrypt volume.
+	 * Check whether the device is a LUKS-formatted device
 	 */
 	isluks = !crypt_isLuks(&co);
 
 	if (!isluks) {
-		if ((error = tc_api_init(0)) != 0) {
-			fprintf(stderr, "crypttab: line %d: tc_api could not "
-			    "be initialized\n", line_no);
-			return 1;
-		}
+		fprintf(stderr, "crypttab: line %d: not a LUKS-formatted device\n",
+		    line_no);
+		return 1;
 	}
 
 	if (type == CRYPTDISKS_STOP) {
@@ -335,33 +330,6 @@ entry_parser(char **tokens, char **options, int type)
 
 			/* Actually close the device */
 			crypt_remove_device(&co);
-		} else {
-			/* Assume tcplay volume */
-			if ((tcplay_task = tc_api_task_init("unmap")) == NULL) {
-				fprintf(stderr, "tc_api_task_init failed.\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "dev", go.device))) {
-				fprintf(stderr, "tc_api_task_set dev failed\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "map_name",
-			    go.map_name))) {
-				fprintf(stderr, "tc_api_task_set map_name failed\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_do(tcplay_task))) {
-				fprintf(stderr, "crypttab: line %d: device %s "
-				    "could not be unmapped: %s\n",
-				    line_no, go.device,
-				    tc_api_task_get_error(tcplay_task));
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_uninit(tcplay_task))) {
-				fprintf(stderr, "tc_api_task_uninit failed\n");
-				goto tcplay_err;
-			}
-
 		}
 	} else if (type == CRYPTDISKS_START) {
 		/* Open the device */
@@ -372,73 +340,10 @@ entry_parser(char **tokens, char **options, int type)
 				    line_no, co.device);
 				return 1;
 			}
-		} else {
-			if ((tcplay_task = tc_api_task_init("map")) == NULL) {
-				fprintf(stderr, "tc_api_task_init failed.\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "dev", go.device))) {
-				fprintf(stderr, "tc_api_task_set dev failed\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "map_name",
-			    go.map_name))) {
-				fprintf(stderr, "tc_api_task_set map_name failed\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "interactive",
-			    (go.passphrase != NULL) ? 0 : 1))) {
-				fprintf(stderr, "tc_api_task_set map_name failed\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "retries",
-			    go.ntries))) {
-				fprintf(stderr, "tc_api_task_set map_name failed\n");
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_set(tcplay_task, "timeout",
-			    go.timeout))) {
-				fprintf(stderr, "tc_api_task_set map_name failed\n");
-				goto tcplay_err;
-			}
-
-			if (go.passphrase != NULL) {
-				if ((error = tc_api_task_set(tcplay_task, "passphrase",
-				    go.passphrase))) {
-					fprintf(stderr, "tc_api_task_set map_name failed\n");
-					goto tcplay_err;
-				}
-			}
-
-			for (i = 0; i < go.nkeyfiles; i++) {
-				if ((error = tc_api_task_set(tcplay_task, "keyfiles",
-				    go.keyfiles[i]))) {
-					fprintf(stderr, "tc_api_task_set keyfile failed\n");
-					goto tcplay_err;
-				}
-			}
-			if ((error = tc_api_task_do(tcplay_task))) {
-				fprintf(stderr, "crypttab: line %d: device %s "
-				    "could not be mapped/opened: %s\n",
-				    line_no, go.device,
-				    tc_api_task_get_error(tcplay_task));
-				goto tcplay_err;
-			}
-			if ((error = tc_api_task_uninit(tcplay_task))) {
-				fprintf(stderr, "tc_api_task_uninit failed\n");
-				goto tcplay_err;
-			}
 		}
 	}
 
-	if (!isluks)
-		tc_api_uninit();
-
 	return 0;
-
-tcplay_err:
-	tc_api_uninit();
-	return 1;
 }
 
 static int
diff --git a/sbin/tcplay/Makefile b/sbin/tcplay/Makefile
deleted file mode 100644
index 17bdc89ce5..0000000000
--- a/sbin/tcplay/Makefile
+++ /dev/null
@@ -1,28 +0,0 @@
-TCPLAY_DIR=     ${.CURDIR}/../../lib/libtcplay
-
-MAJ_VER=	3
-MIN_VER=	3
-
-PROG=   tcplay
-MAN=	tcplay.8
-
-SRCS+=		tcplay.c crc32.c safe_mem.c io.c hdr.c humanize.c
-SRCS+=		crypto.c generic_xts.c
-SRCS+=		crypto-dev.c pbkdf2-openssl.c
-SRCS+=		main.c
-
-LDADD=		-ldm -lprop -lutil
-DPADD=		${LIBDM} ${LIBPROP} ${LIBUTIL}
-
-DPADD+=		${LIBCRYPTO}
-LDADD+=		-lprivate_crypto
-LDFLAGS+=	${PRIVATELIB_LDFLAGS}
-CFLAGS+=	-I${.CURDIR}/../../crypto/libressl/include
-
-CFLAGS+=	-I${TCPLAY_DIR}
-CFLAGS+=	-DMAJ_VER=${MAJ_VER} -DMIN_VER=${MIN_VER}
-CFLAGS+=	-D_FILE_OFFSET_BITS=64
-
-.PATH: ${TCPLAY_DIR}
-
-.include <bsd.prog.mk>
diff --git a/sbin/tcplay/main.c b/sbin/tcplay/main.c
deleted file mode 100644
index e210150bcf..0000000000
--- a/sbin/tcplay/main.c
+++ /dev/null
@@ -1,480 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/types.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <inttypes.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <time.h>
-
-#include "tcplay.h"
-
-#ifndef SIGINFO
-#define SIGINFO SIGUSR1
-#endif
-
-#define FLAG_LONG_FDE		0xff01
-#define FLAG_LONG_USE_BACKUP	0xff02
-#define FLAG_LONG_MOD		0xff04
-#define FLAG_LONG_MOD_KF	0xff08
-#define FLAG_LONG_MOD_PRF	0xff10
-#define FLAG_LONG_MOD_NONE	0xff20
-#define FLAG_LONG_MOD_TO_FILE	0xff40
-#define FLAG_LONG_USE_HDR_FILE	0xfe01
-#define FLAG_LONG_USE_HHDR_FILE	0xfe02
-#define FLAG_LONG_NO_RETRIES	0xfabc
-
-
-static
-void
-sig_handler(int sig)
-{
-	if ((sig == SIGUSR1 || sig == SIGINFO) && (summary_fn != NULL))
-		summary_fn();
-}
-
-static
-void
-usage(void)
-{
-	fprintf(stderr,
-	    "usage: tcplay -c -d device [-g] [-z] [-w] [-a pbkdf_hash] [-b cipher]\n"
-	    "              [-f keyfile_hidden] [-k keyfile] [-x pbkdf_hash] [-y cipher]\n"
-	    "       tcplay -i -d device [-e] [-p] [-f keyfile_hidden] [-k keyfile]\n"
-	    "              [-s system_device] [--fde] [--use-backup]\n"
-	    "              [--use-hdr-file=hdr_file] [--use-hidden-hdr-file=hdr_file]\n"
-	    "       tcplay -m mapping -d device [-e] [-p] [-f keyfile_hidden] [-k keyfile]\n"
-	    "              [-s system_device] [--fde] [--use-backup] [--allow-trim]\n"
-	    "              [--use-hdr-file=hdr_file] [--use-hidden-hdr-file=hdr_file]\n"
-	    "       tcplay --modify -d device [-k keyfile] [--new-keyfile=keyfile]\n"
-	    "              [--new-pbkdf-prf=pbkdf_hash] [-s system_device] [--fde]\n"
-	    "              [--use-backup] [--save-hdr-to-file=hdr_file] [-w]\n"
-	    "              [--use-hdr-file=hdr_file] [--use-hidden-hdr-file=hdr_file]\n"
-	    "       tcplay --modify -d device [-k keyfile] --restore-from-backup-hdr [-w]\n"
-	    "       tcplay -j mapping\n"
-	    "       tcplay -u mapping\n"
-	    "       tcplay -h | -v\n"
-	    "\n"
-	    "Valid commands are:\n"
-	    " -c, --create\n"
-	    "\t Creates a new TC volume on the device specified by -d or --device.\n"
-	    " -h, --help\n"
-	    "\t Print help message and exit.\n"
-	    " -i, --info\n"
-	    "\t Gives information about the TC volume specified by -d or --device.\n"
-	    " -j <mapping name>, --info-mapped=<mapping name>\n"
-	    "\t Gives information about the mapped TC volume under the given mapping.\n"
-	    " -m <mapping name>, --map=<mapping name>\n"
-	    "\t Creates a dm-crypt mapping with the given name for the device\n"
-	    "\t specified by -d or --device.\n"
-	    " -u <mapping name>, --unmap=<mapping name>\n"
-	    "\t Removes a dm-crypt mapping with the given name.\n"
-	    " --modify\n"
-	    "\t Changes the volume's passphrase, keyfile and optionally the hashing\n"
-	    "\t function used for the PBKDF password derivation.\n"
-	    " -v, --version\n"
-	    "\t Print version message and exit.\n"
-	    "\n"
-	    "Valid options for --create are:\n"
-	    " -a <pbkdf prf algorithm>, --pbkdf-prf=<pbkdf prf algorithm>\n"
-	    "\t Specifies which hashing function to use for the PBKDF password\n"
-	    "\t derivation when creating a new volume.\n"
-	    "\t To see valid options, specify '-a help'.\n"
-	    " -b <cipher>, --cipher=<cipher>\n"
-	    "\t Specifies which cipher to use when creating a new TC volume.\n"
-	    "\t To see valid options, specify '-b help'.\n"
-	    " -g, --hidden\n"
-	    "\t Specifies that the newly created volume will contain a hidden volume.\n"
-	    " -x <pbkdf prf algorithm>, --pbkdf-prf=<pbkdf prf algorithm>\n"
-	    "\t Specifies which hashing function to use for the PBKDF password\n"
-	    "\t derivation when creating a new hidden volume.  By default, the\n"
-	    "\t same as for the outer volume will be used.\n"
-	    "\t To see valid options, specify '-x help'.\n"
-	    " -y <cipher>, --cipher=<cipher>\n"
-	    "\t Specifies which cipher to use when creating a new hidden volume.\n"
-	    "\t By default, the same as for the outer volume will be used.\n"
-	    "\t To see valid options, specify '-y help'.\n"
-	    " -z, --insecure-erase\n"
-	    "\t Skips the erase of the disk. Possible security hazard.\n"
-	    " -w, --weak-keys\n"
-	    "\t Uses a weak source of entropy (urandom) for key material.\n"
-	    "\t WARNING: This is a REALLY REALLY bad idea for anything but\n"
-	    "\t testing.\n"
-	    "\n"
-	    "Valid options for --modify are:\n"
-	    " --new-keyfile=<key file>\n"
-	    "\t Specifies a key file to use for the password derivation, when\n"
-	    "\t re-encrypting the header, can appear multiple times.\n"
-	    " --new-pbkdf-prf=<pbkdf prf algorithm>\n"
-	    "\t Specifies which hashing function to use for the PBKDF password\n"
-	    "\t derivation when re-encrypting the header.\n"
-	    "\t To see valid options, specify '-a help'.\n"
-	    " -s <disk path>, --system-encryption=<disk path>\n"
-	    "\t Specifies that the disk (e.g. /dev/da0) is using system encryption.\n"
-	    " --fde\n"
-	    "\t Specifies that the disk (e.g. /dev/da0) is using full disk encryption.\n"
-	    " --use-backup\n"
-	    "\t Uses the backup headers (at the end of the volume) instead of the\n"
-	    "\t primary headers. Both normal and backup headers will be modified!\n"
-	    "\t This is useful when your primary headers have been corrupted.\n"
-	    " --use-hdr-file=<header file>\n"
-	    "\t Use the header in the specified file instead of the main header on the\n"
-	    "\t disk as source for the modify operation.\n"
-	    " --use-hidden-hdr-file=<header file>\n"
-	    "\t Use the header in the specified file instead of the hidden header on the\n"
-	    "\t disk as source for the modify operation.\n"
-	    " --restore-from-backup-hdr\n"
-	    "\t Implies --use-backup, no new PBKDF hashing function, no new keyfiles\n"
-	    "\t and no new passphrase.\n"
-	    "\t In other words, this will simply restore both headers from the backup\n"
-	    "\t header. This option cannot be used to restore from a backup header file.\n"
-	    " -w, --weak-keys\n"
-	    "\t Uses a weak source of entropy (urandom) for salt material. The\n"
-	    "\t key material is not affected, as the master keys are kept intact.\n"
-	    "\t WARNING: This is a bad idea for anything but testing.\n"
-	    " --save-hdr-backup=<header file>\n"
-	    "\t Saves the modified header in the specified file instead of updating\n"
-	    "\t the header files on disk.\n"
-	    "\n"
-	    "Valid options for --info and --map are:\n"
-	    " -e, --protect-hidden\n"
-	    "\t Protect a hidden volume when mounting the outer volume.\n"
-	    " -p, --prompt-passphrase\n"
-	    "\t Immediately prompt for a passphrase even if a keyfile is supplied.\n"
-	    " -s <disk path>, --system-encryption=<disk path>\n"
-	    "\t Specifies that the disk (e.g. /dev/da0) is using system encryption.\n"
-	    " -t, --allow-trim\n"
-	    "\t Allow discards (TRIM command) on mapped volume.\n"
-	    " --fde\n"
-	    "\t Specifies that the disk (e.g. /dev/da0) is using full disk encryption.\n"
-	    " --use-backup\n"
-	    "\t Uses the backup headers (at the end of the volume) instead of the\n"
-	    "\t primary headers.\n"
-	    "\t This is useful when your primary headers have been corrupted.\n"
-	    " --use-hdr-file=<header file>\n"
-	    "\t Use the header in the specified file instead of the main header on the\n"
-	    "\t disk.\n"
-	    " --use-hidden-hdr-file=<header file>\n"
-	    "\t Use the header in the specified file instead of the hidden header on the\n"
-	    "\t disk.\n"
-	    "\n"
-	    "Valid options common to all commands are:\n"
-	    " -d <device path>, --device=<device path>\n"
-	    "\t Specifies the path to the volume to operate on (e.g. /dev/da0s1).\n"
-	    " -f <key file>, --keyfile-hidden=<key file>\n"
-	    "\t Specifies a key file to use for the hidden volume password derivation.\n"
-	    "\t This option is only valid in combination with -e, --protect-hidden\n"
-	    "\t or -g, --hidden.\n"
-	    " -k <key file>, --keyfile=<key file>\n"
-	    "\t Specifies a key file to use for the password derivation, can appear\n"
-	    "\t multiple times.\n"
-	    );
-
-	exit(EXIT_FAILURE);
-}
-
-static struct option longopts[] = {
-	{ "create",		no_argument,		NULL, 'c' },
-	{ "cipher",		required_argument,	NULL, 'b' },
-	{ "cipher-hidden",	required_argument,	NULL, 'y' },
-	{ "hidden",		no_argument,		NULL, 'g' },
-	{ "pbkdf-prf",		required_argument,	NULL, 'a' },
-	{ "pbkdf-prf-hidden",	required_argument,	NULL, 'x' },
-	{ "info",		no_argument,		NULL, 'i' },
-	{ "info-mapped",	required_argument,	NULL, 'j' },
-	{ "map",		required_argument,	NULL, 'm' },
-	{ "keyfile",		required_argument,	NULL, 'k' },
-	{ "keyfile-hidden",	required_argument,	NULL, 'f' },
-	{ "protect-hidden",	no_argument,		NULL, 'e' },
-	{ "device",		required_argument,	NULL, 'd' },
-	{ "prompt-passphrase",	no_argument,		NULL, 'p' },
-	{ "system-encryption",	required_argument,	NULL, 's' },
-	{ "allow-trim",		no_argument,		NULL, 't' },
-	{ "fde",		no_argument,		NULL, FLAG_LONG_FDE },
-	{ "use-backup",		no_argument,		NULL, FLAG_LONG_USE_BACKUP },
-	{ "use-hdr-file",	required_argument,	NULL, FLAG_LONG_USE_HDR_FILE },
-	{ "use-hidden-hdr-file",required_argument,	NULL, FLAG_LONG_USE_HHDR_FILE },
-	{ "modify",		no_argument,		NULL, FLAG_LONG_MOD },
-	{ "new-keyfile",	required_argument,	NULL, FLAG_LONG_MOD_KF },
-	{ "new-pbkdf-prf",	required_argument,	NULL, FLAG_LONG_MOD_PRF },
-	{ "restore-from-backup-hdr", no_argument,	NULL, FLAG_LONG_MOD_NONE },
-	{ "save-hdr-backup",	required_argument,	NULL, FLAG_LONG_MOD_TO_FILE },
-	{ "unmap",		required_argument,	NULL, 'u' },
-	{ "version",		no_argument,		NULL, 'v' },
-	{ "weak-keys",		no_argument,		NULL, 'w' },
-	{ "insecure-erase",	no_argument,		NULL, 'z' },
-	{ "help",		no_argument,		NULL, 'h' },
-	{ "no-retries",         no_argument,            NULL, FLAG_LONG_NO_RETRIES },
-	{ NULL,			0,			NULL, 0   },
-};
-
-#define _set_str_opt(opt) \
-	do {									\
-		if ((opts->opt = strdup_safe_mem(optarg)) == NULL) {		\
-			fprintf(stderr, "Could not allocate safe mem.\n");	\
-			exit(EXIT_FAILURE);					\
-		}								\
-	} while(0)
-
-int
-main(int argc, char *argv[])
-{
-	struct tcplay_opts *opts;
-	int ch, error;
-	int info_vol = 0, map_vol = 0,
-	    unmap_vol = 0, info_map = 0,
-	    create_vol = 0, modify_vol = 0;
-
-	if ((error = tc_play_init()) != 0) {
-		fprintf(stderr, "Initialization failed, exiting.");
-		exit(EXIT_FAILURE);
-	}
-
-	atexit(check_and_purge_safe_mem);
-	signal(SIGUSR1, sig_handler);
-	signal(SIGINFO, sig_handler);
-
-	if ((opts = opts_init()) == NULL) {
-		fprintf(stderr, "Initialization failed (opts), exiting.");
-		exit(EXIT_FAILURE);
-	}
-
-	opts->interactive = 1;
-
-	while ((ch = getopt_long(argc, argv, "a:b:cd:ef:ghij:k:m:ps:tu:vwx:y:z",
-	    longopts, NULL)) != -1) {
-		switch(ch) {
-		case 'a':
-			if (opts->prf_algo != NULL)
-				usage();
-			if ((opts->prf_algo = check_prf_algo(optarg, 0, 0)) == NULL) {
-				if (strcmp(optarg, "help") == 0)
-					exit(EXIT_SUCCESS);
-				else
-					usage();
-				/* NOT REACHED */
-			}
-			break;
-		case 'b':
-			if (opts->cipher_chain != NULL)
-				usage();
-			if ((opts->cipher_chain = check_cipher_chain(optarg, 0)) == NULL) {
-				if (strcmp(optarg, "help") == 0)
-					exit(EXIT_SUCCESS);
-				else
-					usage();
-				/* NOT REACHED */
-			}
-			break;
-		case 'c':
-			create_vol = 1;
-			break;
-		case 'd':
-			_set_str_opt(dev);
-			break;
-		case 'e':
-			opts->protect_hidden = 1;
-			break;
-		case 'f':
-			if ((error = opts_add_keyfile_hidden(opts, optarg)) != 0) {
-				fprintf(stderr, "Could not add keyfile: %s\n", optarg);
-				exit(EXIT_FAILURE);
-			}
-			break;
-		case 'g':
-			opts->hidden = 1;
-			break;
-		case 'i':
-			info_vol = 1;
-			break;
-		case 'j':
-			info_map = 1;
-			_set_str_opt(map_name);
-			break;
-		case 'k':
-			if ((error = opts_add_keyfile(opts, optarg)) != 0) {
-				fprintf(stderr, "Could not add keyfile: %s\n", optarg);
-				exit(EXIT_FAILURE);
-			}
-			break;
-		case 'm':
-			map_vol = 1;
-			_set_str_opt(map_name);
-			break;
-		case 'p':
-			opts->prompt_passphrase = 1;
-			break;
-		case 's':
-			opts->flags |= TC_FLAG_SYS;
-			_set_str_opt(sys_dev);
-			break;
-		case 't':
-			opts->flags |= TC_FLAG_ALLOW_TRIM;
-			break;
-		case 'u':
-			unmap_vol = 1;
-			_set_str_opt(map_name);
-			break;
-		case 'v':
-			printf("tcplay v%d.%d\n", MAJ_VER, MIN_VER);
-			exit(EXIT_SUCCESS);
-			/* NOT REACHED */
-		case 'w':
-			fprintf(stderr, "WARNING: Using urandom as source of "
-			    "entropy for key material is a really bad idea.\n");
-			opts->weak_keys_and_salt = 1;
-			break;
-		case 'x':
-			if (opts->h_prf_algo != NULL)
-				usage();
-			if ((opts->h_prf_algo = check_prf_algo(optarg, 0, 0)) == NULL) {
-				if (strcmp(optarg, "help") == 0)
-					exit(EXIT_SUCCESS);
-				else
-					usage();
-				/* NOT REACHED */
-			}
-			break;
-		case 'y':
-			if (opts->h_cipher_chain != NULL)
-				usage();
-			if ((opts->h_cipher_chain = check_cipher_chain(optarg, 0)) == NULL) {
-				if (strcmp(optarg, "help") == 0)
-					exit(EXIT_SUCCESS);
-				else
-					usage();
-				/* NOT REACHED */
-			}
-			break;
-		case 'z':
-			opts->secure_erase = 0;
-			break;
-		case FLAG_LONG_FDE:
-			opts->flags |= TC_FLAG_FDE;
-			break;
-		case FLAG_LONG_USE_BACKUP:
-			opts->flags |= TC_FLAG_BACKUP;
-			break;
-		case FLAG_LONG_USE_HDR_FILE:
-			opts->flags |= TC_FLAG_HDR_FROM_FILE;
-			_set_str_opt(hdr_file_in);
-			break;
-		case FLAG_LONG_USE_HHDR_FILE:
-			opts->flags |= TC_FLAG_H_HDR_FROM_FILE;
-			_set_str_opt(h_hdr_file_in);
-			break;
-		case FLAG_LONG_MOD:
-			modify_vol = 1;
-			break;
-		case FLAG_LONG_MOD_KF:
-			if ((error = opts_add_keyfile_new(opts, optarg)) != 0) {
-				fprintf(stderr, "Could not add keyfile: %s\n", optarg);
-				exit(EXIT_FAILURE);
-			}
-			break;
-		case FLAG_LONG_MOD_PRF:
-			if (opts->new_prf_algo != NULL)
-				usage();
-			if ((opts->new_prf_algo = check_prf_algo(optarg, 0, 0)) == NULL) {
-				if (strcmp(optarg, "help") == 0)
-					exit(EXIT_SUCCESS);
-				else
-					usage();
-				/* NOT REACHED */
-			}
-			break;
-		case FLAG_LONG_MOD_NONE:
-			opts->new_prf_algo = NULL;
-			opts->flags |= TC_FLAG_ONLY_RESTORE;
-			opts->flags |= TC_FLAG_BACKUP;
-			break;
-		case FLAG_LONG_MOD_TO_FILE:
-			opts->flags |= TC_FLAG_SAVE_TO_FILE;
-			_set_str_opt(hdr_file_out);
-			break;
-		case FLAG_LONG_NO_RETRIES:
-			opts->retries = 1;
-			break;
-		case 'h':
-		case '?':
-		default:
-			usage();
-			/* NOT REACHED */
-		}
-	}
-
-	argc -= optind;
-	argv += optind;
-
-	/* Check arguments */
-	if (!(((map_vol || info_vol || create_vol || modify_vol) && opts->dev != NULL) ||
-	    ((unmap_vol || info_map) && opts->map_name != NULL)) ||
-	    (TC_FLAG_SET(opts->flags, SYS) && TC_FLAG_SET(opts->flags, FDE)) ||
-	    (map_vol + info_vol + create_vol + unmap_vol + info_map + modify_vol > 1) ||
-	    (opts->hidden && !create_vol) ||
-	    (TC_FLAG_SET(opts->flags, SYS) && (opts->sys_dev == NULL)) ||
-	    (TC_FLAG_SET(opts->flags, ONLY_RESTORE) && (opts->n_newkeyfiles > 0 || opts->new_prf_algo != NULL)) ||
-	    (TC_FLAG_SET(opts->flags, BACKUP) && (opts->sys_dev != NULL || TC_FLAG_SET(opts->flags, FDE))) ||
-	    (map_vol && (opts->map_name == NULL)) ||
-	    (unmap_vol && (opts->map_name == NULL)) ||
-	    (!modify_vol && opts->n_newkeyfiles > 0) ||
-	    (!modify_vol && opts->new_prf_algo != NULL) ||
-	    (!modify_vol && TC_FLAG_SET(opts->flags, ONLY_RESTORE)) ||
-	    (!modify_vol && TC_FLAG_SET(opts->flags, SAVE_TO_FILE)) ||
-	    (!(opts->protect_hidden || create_vol) && opts->n_hkeyfiles > 0)) {
-		usage();
-		/* NOT REACHED */
-	}
-
-	/* Create a new volume */
-	if (create_vol) {
-		error = create_volume(opts);
-		if (error) {
-			tc_log(1, "could not create new volume on %s\n", opts->dev);
-		}
-	} else if (info_map) {
-		error = info_mapped_volume(opts);
-	} else if (info_vol) {
-		error = info_volume(opts);
-	} else if (map_vol) {
-		error = map_volume(opts);
-	} else if (unmap_vol) {
-		error = dm_teardown(opts->map_name, NULL);
-	} else if (modify_vol) {
-		error = modify_volume(opts);
-	}
-
-	return error;
-}
-
-#undef _set_str_opt
diff --git a/sbin/tcplay/tcplay.8 b/sbin/tcplay/tcplay.8
deleted file mode 100644
index 81f4047f8c..0000000000
--- a/sbin/tcplay/tcplay.8
+++ /dev/null
@@ -1,570 +0,0 @@
-.\"
-.\" Copyright (c) 2011
-.\"	The DragonFly Project.  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.
-.\" 3. Neither the name of The DragonFly Project nor the names of its
-.\"    contributors may be used to endorse or promote products derived
-.\"    from this software without specific, prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
-.\" COPYRIGHT HOLDERS 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.
-.\"
-.Dd April 30, 2020
-.Dt TCPLAY 8
-.Os
-.Sh NAME
-.Nm tcplay
-.Nd tool to manage TrueCrypt volumes
-.Sh SYNOPSIS
-.Nm
-.Fl c
-.Fl d Ar device
-.Op Fl g
-.Op Fl z
-.Op Fl w
-.Op Fl a Ar pbkdf_hash
-.Op Fl b Ar cipher
-.Op Fl f Ar keyfile_hidden
-.Op Fl k Ar keyfile
-.Op Fl x Ar pbkdf_hash
-.Op Fl y Ar cipher
-.Op Fl -fde
-.Nm
-.Fl i
-.Fl d Ar device
-.Op Fl e
-.Op Fl p
-.Op Fl f Ar keyfile_hidden
-.Op Fl k Ar keyfile
-.Op Fl s Ar system_device
-.Op Fl -use-backup
-.Op Fl -use-hdr-file Ar hdr_file
-.Op Fl -use-hidden-hdr-file Ar hdr_file
-.Nm
-.Fl j Ar mapping
-.Nm
-.Fl m Ar mapping
-.Fl d Ar device
-.Op Fl e
-.Op Fl p
-.Op Fl f Ar keyfile_hidden
-.Op Fl k Ar keyfile
-.Op Fl s Ar system_device
-.Op Fl t
-.Op Fl -fde
-.Op Fl -use-backup
-.Op Fl -use-hdr-file Ar hdr_file
-.Op Fl -use-hidden-hdr-file Ar hdr_file
-.Nm
-.Fl -modify
-.Fl d Ar device
-.Op Fl k Ar keyfile
-.Op Fl -new-keyfile Ar new_keyfile
-.Op Fl -new-pbkdf-prf Ar pbkdf_hash
-.Op Fl s Ar system_device
-.Op Fl -fde
-.Op Fl -use-backup
-.Op Fl -use-hdr-file Ar hdr_file
-.Op Fl -use-hidden-hdr-file Ar hdr_file
-.Op Fl -save-hdr-backup Ar hdr_file
-.Op Fl w
-.Nm
-.Fl -modify
-.Fl d Ar device
-.Op Fl k Ar keyfile
-.Fl -restore-from-backup-hdr
-.Op Fl w
-.Nm
-.Fl u Ar mapping
-.Nm
-.Fl h | v
-.Sh DESCRIPTION
-The
-.Nm
-utility provides full support for creating and opening/mapping
-TrueCrypt-compatible volumes.
-It supports the following commands, each with a set of options
-detailed further below:
-.Bl -tag -width indent
-.It Fl c , Fl -create
-Create a new encrypted TrueCrypt volume on the device
-specified by
-.Fl -device .
-.It Fl h , Fl -help
-Print help message and exit.
-.It Fl i , Fl -info
-Print out information about the encrypted device specified by
-.Fl -device .
-.It Fl j Ar mapping , Fl -info-mapped Ns = Ns Ar mapping
-Print out information about the mapped tcplay volume specified
-by
-.Ar mapping .
-Information such as key CRC and the PBKDF2 PRF is not available
-via this command.
-.It Fl -modify
-Modify the volume header.
-This mode allows changing passphrase, keyfiles, PBKDF2 PRF as
-well as restoring from a backup header.
-.It Fl m Ar mapping , Fl -map Ns = Ns Ar mapping
-Map the encrypted TrueCrypt volume on the device specified by
-.Fl -device
-as a
-.Xr dm 4
-mapping called
-.Ar mapping .
-The
-.Ar mapping
-argument should not contain any spaces or special characters.
-.It Fl u Ar mapping , Fl -unmap Ns = Ns Ar mapping
-Removes (unmaps) the
-.Xr dm 4
-mapping specified by
-.Ar mapping
-as well as any related cascade mappings.
-.It Fl v , Fl -version
-Print version message and exit.
-.El
-.Pp
-Options common to all commands are:
-.Bl -tag -width indent
-.It Fl d Ar device , Fl -device Ns = Ns Ar device
-Specifies the disk
-.Ar device
-on which the TrueCrypt volume resides/will reside.
-This option is mandatory for all commands.
-.It Fl f Ar keyfile_hidden , Fl -keyfile-hidden Ns = Ns Ar keyfile_hidden
-Specifies a keyfile
-to use in addition to the passphrase when either creating a
-hidden volume or when protecting a hidden volume while mapping
-or querying the outer volume.
-If you only intend to map a hidden volume, the
-.Fl -keyfile
-option has to be used.
-This option can appear multiple times; if so, multiple
-keyfiles will be used.
-This option is not valid in the
-.Fl -modify
-mode.
-.It Fl k Ar keyfile , Fl -keyfile Ns = Ns Ar keyfile
-Specifies a
-.Ar keyfile
-to use in addition to the passphrase.
-This option can appear multiple times; if so, multiple
-keyfiles will be used.
-.El
-.Pp
-Additional options for the
-.Fl -create
-command are:
-.Bl -tag -width indent
-.It Fl a Ar pbkdf_hash , Fl -pbkdf-prf Ns = Ns Ar pbkdf_hash
-Specifies which hash algorithm to use for the PBKDF2 password
-derivation.
-To see which algorithms are supported, specify
-.Fl -pbkdf-prf Ns = Ns Cm help .
-.It Fl b Ar cipher , Fl -cipher Ns = Ns Ar cipher
-Specifies which cipher algorithm or cascade of ciphers to use
-to encrypt the new volume.
-To see which algorithms are supported, specify
-.Fl -cipher Ns = Ns Cm help .
-.It Fl g , Fl -hidden
-Specifies that the newly created volume will contain a hidden
-volume.
-The keyfiles applied to the passphrase for the hidden
-volume are those specified by
-.Fl -keyfile-hidden .
-The user will be prompted for the size of the hidden volume
-interactively.
-.It Fl w , Fl -weak-keys
-Use
-.Xr urandom 4
-for key material instead of a strong entropy source.
-This is in general a really bad idea and should only be used
-for testing.
-.It Fl x Ar pbkdf_hash , Fl -pbkdf-prf-hidden Ns = Ns Ar pbkdf_hash
-Specifies which hash algorithm to use for the PBKDF2 password
-derivation for the hidden volume.
-Only valid in conjunction with
-.Fl -hidden .
-If no algorithm is specified, the same as for the outer volume
-will be used.
-To see which algorithms are supported, specify
-.Fl -pbkdf-prf-hidden Ns = Ns Cm help .
-.It Fl y Ar cipher , Fl -cipher-hidden Ns = Ns Ar cipher
-Specifies which cipher algorithm or cascade of ciphers to use
-to encrypt the hidden volume on the new TrueCrypt volume.
-Only valid in conjunction with
-.Fl -hidden .
-If no cipher is specified, the same as for the outer volume
-will be used.
-To see which algorithms are supported, specify
-.Fl -cipher-hidden Ns = Ns Cm help .
-.It Fl z , Fl -insecure-erase
-Skips the secure erase of the disk.
-Use this option carefully as it is a security risk!
-.El
-.Pp
-Additional options for the
-.Fl -info ,
-.Fl -map
-and
-.Fl -modify
-commands are:
-.Bl -tag -width indent
-.It Fl e , Fl -protect-hidden
-Specifies that an outer volume will be queried or mapped, but
-its reported size will be adjusted accordingly to the size of
-the hidden volume contained in it.
-Both the hidden volume and outer volume passphrase and keyfiles
-will be required.
-This option only applies to the
-.Fl -info
-and
-.Fl -map
-commands.
-.It Fl p, Fl -prompt-passphrase
-This option causes
-.Nm
-to prompt for a passphrase immediately, even if a keyfile is
-provided.
-Normally, if a keyfile is supplied,
-.Nm
-will first attempt to unlock the volume using only the keyfile,
-and only prompt for a passphrase if that first unlocking attempt
-fails.
-However, since a failed unlocking attempt can take a non-trivial
-amount of time, specifying this option can reduce the total unlocking
-time if both a keyfile and passphrase are required.
-This option only makes sense if
-.Fl k
-or
-.Fl f
-are used.
-.It Fl s Ar system_device , Fl -system-encryption Ns = Ns Ar system_device
-This option is required if you are attempting to access a device
-that uses system encryption, for example an encrypted
-.Tn Windows
-system partition.
-It does not apply to disks using full disk encryption.
-The
-.Fl -device
-option will point at the actual encrypted partition, while the
-.Ar system_device
-argument will point to the parent device (i.e.\& underlying physical disk)
-of the encrypted partition.
-.It Fl -fde
-This option is intended to be used with disks using full disk encryption (FDE).
-When a disk has been encrypted using TrueCrypt's FDE, the complete disk
-is encrypted except for the first 63 sectors.
-The
-.Fl -device
-option should point to the whole disk device, not to any particular
-partition.
-The resultant mapping will cover the whole disk, and will not appear as
-separate partitions.
-.It Fl -use-backup
-This option is intended to be used when the primary headers of a volume
-have been corrupted.
-This option will force
-.Nm
-to use the backup headers, which are located at the end of the device,
-to access the volume.
-.El
-.Pp
-Additional options only for the
-.Fl -map
-command are:
-.Bl -tag -width indent
-.It Fl t , Fl -allow-trim
-This option enables TRIM (discard) support on the mapped volume.
-.El
-.Pp
-Additional options only for the
-.Fl -modify
-command are:
-.Bl -tag -width indent
-.It Fl -new-pbkdf-prf Ns = Ns Ar pbkdf_hash
-Specifies which hash algorithm to use for the PBKDF2 password
-derivation on reencrypting the volume header.
-If this option is not specified, the reencrypted header will
-use the current PRF.
-To see which algorithms are supported, specify
-.Fl -pbkdf-prf Ns = Ns Cm help .
-.It Fl -new-keyfile Ns = Ns Ar keyfile
-Specifies a
-.Ar keyfile
-to use in addition to the new passphrase on reencrypting the
-volume header.
-This option can appear multiple times; if so, multiple
-keyfiles will be used.
-.It Fl -restore-from-backup-hdr
-If this option is specified, neither
-.Fl -new-pbkdf-prf
-nor
-.Fl -new-keyfile
-should be specified.
-This option implies
-.Fl -use-backup .
-Use this option to restore the volume headers from the backup
-header.
-.El
-.Pp
-Sending a
-.Dv SIGINFO
-or
-.Dv SIGUSR1
-signal to a running
-.Nm
-process makes it print progress on slower tasks
-such as gathering entropy or wiping the volume.
-.Sh NOTES
-TrueCrypt limits passphrases to 64 characters (including the terminating
-null character).
-To be compatible with it,
-.Nm
-does the same.
-All passphrases (excluding keyfiles) are trimmed to 64 characters.
-Similarly, keyfiles are limited to a size of 1 MB, but up to
-256 keyfiles can be used.
-.Sh PLAUSIBLE DENIABILITY
-.Nm
-offers plausible deniability. Hidden volumes are created within an outer
-volume.
-Which volume is accessed solely depends on the passphrase and keyfile(s)
-used.
-If the passphrase and keyfiles for the outer volume are specified,
-no information about the existence of the hidden volume is exposed.
-Without knowledge of the passphrase and keyfile(s) of the hidden volume
-its existence remains unexposed.
-The hidden volume can be protected when mapping the outer volume by
-using the
-.Fl -protect-hidden
-option and specifying the passphrase and keyfiles for both the outer
-and hidden volumes.
-.Sh VERACRYPT SUPPORT
-.Nm
-offers both legacy TrueCrypt as well as VeraCrypt support.
-When creating a new volume, the selected PBKDF2 PRF determines whether
-the volume will use the TrueCrypt or VeraCrypt format.
-The formats are identical other than the rounds of the key derivation
-functions as well as the volume signature and minver fields in the
-header.
-Converting volumes from one format or another using
-.Nm
-is simply a matter of using the
-.Fl -modify
-option specifying a PBKDF2 PRF hash matching the intended target format
-with the
-.Fl -new-pbkdf-prf
-argument.
-.Pp
-PBKDF2 PRFs suffixed with
-.Dv -VC
-are VeraCrypt PRFs, whilst all others are legacy TrueCrypt PRFs.
-By default, new volumes are created with a VeraCrypt PRF to offer better
-security.
-.Pp
-NOTE: Failed unlocking attempts even for legacy TrueCrypt volumes now take
-significantly longer than before, as
-.Nm
-will cycle through all PRFs, including the VeraCrypt PRFs with much higher
-number of PRF iterations.
-Successful attempts should still take the same amount of time as before, as
-the legacy PRF settings are tried first.
-One notable exception is if both a keyfile and a passphrase is required.
-Normally,
-.Nm
-would first attempt an unlock attempt with just the keyfile, and only prompt
-for a passphrase after that attempt failed.
-If it is known in advance that both a keyfile and passphrase are required to
-unlock a volume, the
-.Fl p
-option to
-.Fl -info
-and
-.Fl -map
-can more than halve the time required to unlock the volume.
-.Sh EXAMPLES
-Create a new TrueCrypt volume on
-.Pa /dev/vn0
-using the cipher cascade
-of AES and Twofish and the Whirlpool hash algorithm for
-PBKDF2 password derivation and two keyfiles,
-.Pa one.key
-and
-.Pa two.key :
-.Bd -ragged -offset indent
-.Nm Fl -create
-.Fl -device Ns = Ns Cm /dev/vn0
-.Fl -cipher Ns = Ns Cm TWOFISH-256-XTS,AES-256-XTS
-.Fl -pbkdf-prf Ns = Ns Cm whirlpool
-.Fl -keyfile Ns = Ns Cm one.key
-.Fl -keyfile Ns = Ns Cm two.key
-.Ed
-.Pp
-Map the outer volume on the TrueCrypt volume on
-.Pa /dev/vn0
-as
-.Sy truecrypt1 ,
-but protect the hidden volume, using the keyfile
-.Pa hidden.key ,
-from being overwritten:
-.Bd -ragged -offset indent
-.Nm Fl -map Ns = Ns Cm truecrypt1
-.Fl -device Ns = Ns Cm /dev/vn0
-.Fl -protect-hidden
-.Fl -keyfile-hidden Ns = Ns Cm hidden.key
-.Ed
-.Pp
-Map the hidden volume on the TrueCrypt volume on
-.Pa /dev/vn0
-as
-.Sy truecrypt2 ,
-using the keyfile
-.Pa hidden.key :
-.Bd -ragged -offset indent
-.Nm Fl -map Ns = Ns Cm truecrypt2
-.Fl -device Ns = Ns Cm /dev/vn0
-.Fl -keyfile Ns = Ns Cm hidden.key
-.Ed
-.Pp
-Map and mount the volume in the file
-.Pa secvol :
-.Bd -ragged -offset indent
-.Sy vnconfig Cm vn1 Cm secvol
-.Ed
-.Bd -ragged -offset indent
-.Nm Fl -map Ns = Ns Cm secv
-.Fl -device Ns = Ns Cm /dev/vn1
-.Ed
-.Bd -ragged -offset indent
-.Sy mount Cm /dev/mapper/secv Cm /mnt
-.Ed
-.Pp
-Unmapping the volume
-.Sy truecrypt2
-after unmounting:
-.Bd -ragged -offset indent
-.Sy dmsetup Cm remove Cm truecrypt2
-.Ed
-.Pp
-Or alternatively:
-.Bd -ragged -offset indent
-.Nm Fl -unmap Ns = Ns Cm truecrypt2
-.Ed
-.Pp
-A hidden volume whose existence can be plausibly denied and its outer volume
-can for example be created with
-.Bd -ragged -offset indent
-.Nm Fl -create
-.Fl -hidden
-.Fl -device Ns = Ns Cm /dev/vn0
-.Fl -cipher Ns = Ns Cm TWOFISH-256-XTS,AES-256-XTS
-.Fl -pbkdf-prf Ns = Ns Cm whirlpool
-.Fl -keyfile Ns = Ns Cm one.key
-.Fl -cipher-hidden Ns = Ns Cm AES-256-XTS
-.Fl -pbkdf-prf-hidden Ns = Ns Cm whirlpool
-.Fl -keyfile-hidden Ns = Ns Cm hidden.key
-.Ed
-.Pp
-.Nm
-will prompt the user for the passphrase for both the outer and hidden volume
-as well as the size of the hidden volume inside the outer volume.
-The hidden volume will be created inside the area spanned by the outer volume.
-The hidden volume can optionally use a different cipher and prf function
-as specified by the
-.Fl -cipher-hidden
-and
-.Fl -pbkdf-prf-hidden
-options.
-Which volume is later accessed depends only on which passphrase and keyfile(s)
-are being used,
-so that the existence of the hidden volume remains unknown without knowledge
-of the passphrase and keyfile it is protected by since it is located within
-the outer volume.
-To map the outer volume without potentially damaging the hidden volume,
-the passphrase and keyfile(s) of the hidden volume must be known and provided
-alongside the
-.Fl -protect-hidden
-option.
-.Pp
-A disk encrypted using full disk encryption can be mapped using
-.Bd -ragged -offset indent
-.Nm Fl -map Ns = Ns Cm tcplay_da2
-.Fl -device Ns = Ns Cm /dev/da2
-.Fl -fde
-.Ed
-.Pp
-To restore the main volume header from the backup header, the following
-command can be used:
-.Bd -ragged -offset indent
-.Nm Fl -modify
-.Fl -device Ns = Ns Cm /dev/da2
-.Fl -restore-from-backup-hdr
-.Ed
-.Pp
-As with most other commands, which header is saved (used as source) depends
-on the passphrase and keyfiles used.
-.Pp
-To save a backup copy of a header, the following command can be used:
-.Bd -ragged -offset indent
-.Nm Fl -modify
-.Fl -device Ns = Ns Cm /dev/da2
-.Fl -save-hdr-backup Ns = Ns Cm /tmp/da2_backup_header.hdr
-.Ed
-.Pp
-As with most other commands, which header is saved (used as source) depends
-on the passphrase and keyfiles used.
-.Pp
-To restore a header from a backup header file, the following command can be
-used:
-.Bd -ragged -offset indent
-.Nm Fl -modify
-.Fl -device Ns = Ns Cm /dev/da2
-.Fl -use-hdr-file Ns = Ns Cm /tmp/da2_backup_header.hdr
-.Ed
-.Pp
-Similarly, to restore a hidden header from a backup header file:
-.Bd -ragged -offset indent
-.Nm Fl -modify
-.Fl -device Ns = Ns Cm /dev/da2
-.Fl -use-hidden-hdr-file Ns = Ns Cm /tmp/da2_backup_hidden_header.hdr
-.Ed
-.Pp
-Which header is used as the source of the operation will still depend on the
-passphrase and keyfiles used.
-Even if you use the
-.Fl -use-hidden-hdr-file
-option, if you specify the passphrase and keyfiles for the main header, the
-main header will be used instead.
-.Sh SEE ALSO
-.Xr crypttab 5 ,
-.Xr cryptsetup 8 ,
-.Xr dmsetup 8
-.Sh HISTORY
-The
-.Nm
-utility appeared in
-.Dx 2.11 .
-.Sh AUTHORS
-.An Alex Hornung
diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile
index 22aef48a82..71494b900b 100644
--- a/share/man/man4/Makefile
+++ b/share/man/man4/Makefile
@@ -70,7 +70,6 @@ MAN=	aac.4 \
 	corepower.4 \
 	coretemp.4 \
 	cpuctl.4 \
-	crypto.4 \
 	cue.4 \
 	cxm.4 \
 	da.4 \
@@ -108,7 +107,6 @@ MAN=	aac.4 \
 	gpio_acpi.4 \
 	gpio_intel.4 \
 	gre.4 \
-	hifn.4 \
 	hpt27xx.4 \
 	hptiop.4 \
 	hptmv.4 \
@@ -231,7 +229,6 @@ MAN=	aac.4 \
 	oce.4 \
 	ohci.4 \
 	orm.4 \
-	padlock.4 \
 	pass.4 \
 	pccard.4 \
 	pccbb.4 \
@@ -266,7 +263,6 @@ MAN=	aac.4 \
 	run.4 \
 	runfw.4 \
 	sa.4 \
-	safe.4 \
 	sbp.4 \
 	sbsh.4 \
 	screen.4 \
@@ -337,7 +333,6 @@ MAN=	aac.4 \
 	txp.4 \
 	uark.4 \
 	ubsa.4 \
-	ubsec.4 \
 	ubser.4 \
 	ubt.4 \
 	uchcom.4 \
@@ -437,7 +432,6 @@ MLINKS+=bnx.4 if_bnx.4
 MLINKS+=bridge.4 if_bridge.4
 MLINKS+=bwn.4 if_bwn.4
 MLINKS+=cdce.4 if_cdce.4
-MLINKS+=crypto.4 cryptodev.4
 MLINKS+=cue.4 if_cue.4
 MLINKS+=dc.4 if_dc.4
 MLINKS+=disc.4 if_disc.4
diff --git a/share/man/man4/aesni.4 b/share/man/man4/aesni.4
index e914440d60..02e912b09f 100644
--- a/share/man/man4/aesni.4
+++ b/share/man/man4/aesni.4
@@ -62,7 +62,7 @@ driver does not attach on systems that lack the required CPU capability.
 The
 .Nm
 driver registers itself to accelerate AES operations for
-.Xr crypto 4 .
+.Xr crypto 9 .
 Besides speed, the advantage of using the
 .Nm
 driver is that the AESNI operation
@@ -71,9 +71,7 @@ measuring cache use and timings typically present in table-driven
 implementations.
 .Sh SEE ALSO
 .Xr crypt 3 ,
-.Xr crypto 4 ,
 .Xr intro 4 ,
-.Xr padlock 4 ,
 .Xr random 4 ,
 .Xr crypto 9
 .Sh HISTORY
diff --git a/share/man/man4/crypto.4 b/share/man/man4/crypto.4
deleted file mode 100644
index 2791816c75..0000000000
--- a/share/man/man4/crypto.4
+++ /dev/null
@@ -1,135 +0,0 @@
-.\"	$OpenBSD: crypto.4,v 1.4 2002/09/12 07:15:03 deraadt Exp $
-.\"
-.\" Copyright (c) 2001 Theo de Raadt
-.\" 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.
-.\" 3. The name of the author may not be used to endorse or promote products
-.\"    derived from this software without specific prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
-.\"
-.\" $FreeBSD: src/share/man/man4/crypto.4,v 1.9 2009/03/03 07:58:01 brueffer Exp $
-.\"
-.Dd April 21, 2018
-.Dt CRYPTO 4
-.Os
-.Sh NAME
-.Nm crypto ,
-.Nm cryptodev
-.Nd hardware crypto access driver
-.Sh SYNOPSIS
-.Cd device crypto
-.Cd device cryptodev
-.Sh DESCRIPTION
-The
-.Nm
-driver provides a device-independent framework to support
-cryptographic operations in the kernel.
-The
-.Nm cryptodev
-driver provides userland applications access to this support
-through the
-.Pa /dev/crypto
-device.
-This node primarily operates in an
-.Xr ioctl 2
-based model, permitting a variety of applications to query device capabilities,
-submit transactions, and get results.
-.Pp
-If
-.Ar count
-given in the specification, and is greater than 0, a maximum of one
-.Nm
-device is created.
-.Pp
-The following
-.Xr ioctl 2
-calls apply only to the
-.Nm
-devices:
-.Bl -tag -width ".Dv CIOCGSESSION"
-.It Dv CIOCGSESSION
-Setup a new crypto session for a new type of operation.
-.It Dv CIOCFSESSION
-Free a previously established session.
-.It Dv CIOCCRYPT
-Perform a crypto operation against a previously setup session.
-.El
-.Sh FEATURES
-Depending on hardware being present, the following symmetric and
-asymmetric cryptographic features are potentially available from
-.Pa /dev/crypto :
-.Pp
-.Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -offset indent -compact
-.It Dv CRYPTO_DES_CBC
-.It Dv CRYPTO_3DES_CBC
-.It Dv CRYPTO_BLF_CBC
-.It Dv CRYPTO_CAMELLIA_CBC
-.It Dv CRYPTO_CAST_CBC
-.It Dv CRYPTO_SKIPJACK_CBC
-.It Dv CRYPTO_MD5_HMAC
-.It Dv CRYPTO_SHA1_HMAC
-.It Dv CRYPTO_RIPEMD160_HMAC
-.It Dv CRYPTO_MD5_KPDK
-.It Dv CRYPTO_SHA1_KPDK
-.It Dv CRYPTO_AES_CBC
-.It Dv CRYPTO_AES_XTS
-.It Dv CRYPTO_AES_CTR
-.It Dv CRYPTO_AES_GCM_16
-.It Dv CRYPTO_AES_GMAC
-.It Dv CRYPTO_AES_128_GMAC
-.It Dv CRYPTO_AES_192_GMAC
-.It Dv CRYPTO_AES_256_GMAC
-.It Dv CRYPTO_TWOFISH_CBC
-.It Dv CRYPTO_SERPENT_CBC
-.It Dv CRYPTO_TWOFISH_XTS
-.It Dv CRYPTO_SERPENT_XTS
-.It Dv CRYPTO_ARC4
-.It Dv CRYPTO_MD5
-.It Dv CRYPTO_SHA1
-.It Dv CRK_MOD_EXP
-.It Dv CRK_MOD_EXP_CRT
-.It Dv CRK_DSA_SIGN
-.It Dv CRK_DSA_VERIFY
-.It Dv CRK_DH_COMPUTE_KEY
-.El
-.Sh FILES
-.Bl -tag -width ".Pa /dev/crypto" -compact
-.It Pa /dev/crypto
-crypto access device
-.El
-.Sh SEE ALSO
-.Xr aesni 4 ,
-.Xr hifn 4 ,
-.Xr padlock 4 ,
-.Xr safe 4 ,
-.Xr ubsec 4 ,
-.\".Xr geli 8 ,
-.Xr crypto 9
-.Sh HISTORY
-The
-.Nm
-driver first appeared in
-.Ox 3.0 .
-The
-.Nm
-driver was imported to
-.Fx 5.0 .
diff --git a/share/man/man4/dm_target_crypt.4 b/share/man/man4/dm_target_crypt.4
index 70bc804be6..80222686e4 100644
--- a/share/man/man4/dm_target_crypt.4
+++ b/share/man/man4/dm_target_crypt.4
@@ -56,7 +56,6 @@ provides transparent disk encryption compatible with Linux
 .Xr dm 4 ,
 .Xr cryptsetup 8 ,
 .Xr dmsetup 8 ,
-.Xr tcplay 8
 .Sh HISTORY
 The
 .Nm
diff --git a/share/man/man4/hifn.4 b/share/man/man4/hifn.4
deleted file mode 100644
index b0ab104de0..0000000000
--- a/share/man/man4/hifn.4
+++ /dev/null
@@ -1,130 +0,0 @@
-.\"	$OpenBSD: hifn.4,v 1.32 2002/09/26 07:55:40 miod Exp $
-.\"	$FreeBSD: src/share/man/man4/hifn.4,v 1.9 2009/10/19 14:36:12 brueffer Exp $
-.\"
-.\" Copyright (c) 2000 Theo de Raadt
-.\" 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 AUTHOR ``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 AUTHOR 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.
-.\"
-.Dd April 21, 2018
-.Dt HIFN 4
-.Os
-.Sh NAME
-.Nm hifn
-.Nd Hifn 7751/7951/7811/7955/7956 crypto accelerator
-.Sh SYNOPSIS
-To compile this driver into the kernel,
-place the following lines in your
-kernel configuration file:
-.Bd -ragged -offset indent
-.Cd "device crypto"
-.Cd "device cryptodev"
-.Cd "device hifn"
-.Ed
-.Pp
-Alternatively, to load the driver as a
-module at boot time, place the following line in
-.Xr loader.conf 5 :
-.Bd -literal -offset indent
-hifn_load="YES"
-.Ed
-.Sh DESCRIPTION
-The
-.Nm
-driver supports various cards containing the Hifn 7751, 7951,
-7811, 7955, and 7956 chipsets.
-.Pp
-The
-.Nm
-driver registers itself to accelerate DES, Triple-DES,
-AES (7955 and 7956 only), ARC4, MD5,
-MD5-HMAC, SHA1, and SHA1-HMAC operations for
-.Xr crypto 4 .
-.Pp
-The Hifn
-.Tn 7951 ,
-.Tn 7811 ,
-.Tn 7955 ,
-and
-.Tn 7956
-will also supply data to the kernel
-.Xr random 4
-subsystem.
-.Sh HARDWARE
-The
-.Nm
-driver supports various cards containing the Hifn 7751, 7951,
-7811, 7955, and 7956
-chipsets, such as:
-.Bl -tag -width namenamenamena -offset indent
-.It Invertex AEON
-No longer being made.
-Came as 128KB SRAM model, or 2MB DRAM model.
-.It Hifn 7751
-Reference board with 512KB SRAM.
-.It PowerCrypt
-Comes with 512KB SRAM.
-.It XL-Crypt
-Only board based on 7811 (which is faster than 7751 and has
-a random number generator).
-.It NetSec 7751
-Supports the most IPsec sessions, with 1MB SRAM.
-.It Soekris Engineering vpn1201 and vpn1211
-See
-.Pa http://www.soekris.com/ .
-Contains a 7951 and supports symmetric and random number operations.
-.It Soekris Engineering vpn1401 and vpn1411
-See
-.Pa http://www.soekris.com/ .
-Contains a 7955 and supports symmetric and random number operations.
-.El
-.Sh SEE ALSO
-.Xr crypt 3 ,
-.Xr crypto 4 ,
-.Xr intro 4 ,
-.Xr random 4 ,
-.Xr crypto 9
-.Sh CAVEATS
-The Hifn 9751 shares the same PCI ID.
-This chip is basically a 7751, but with the cryptographic functions missing.
-Instead, the 9751 is only capable of doing compression.
-Since we do not currently attempt to use any of these chips to do
-compression, the 9751-based cards are not useful.
-.Pp
-Support for the 7955 and 7956 is incomplete; the asymmetric crypto
-facilities are to be added and the performance is suboptimal.
-.Sh HISTORY
-The
-.Nm
-device driver appeared in
-.Ox 2.7 .
-The
-.Nm
-device driver was imported to
-.Fx 5.0 .
-.Sh BUGS
-The 7751 chip starts out at initialization by only supporting compression.
-A proprietary algorithm, which has been reverse engineered, is required to
-unlock the cryptographic functionality of the chip.
-It is possible for vendors to make boards which have a lock ID not known
-to the driver, but all vendors currently just use the obvious ID which is
-13 bytes of 0.
diff --git a/share/man/man4/padlock.4 b/share/man/man4/padlock.4
deleted file mode 100644
index c6c9f21f2d..0000000000
--- a/share/man/man4/padlock.4
+++ /dev/null
@@ -1,97 +0,0 @@
-.\" Copyright (c) 2005 Christian Brueffer
-.\" 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 AUTHOR 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 AUTHOR 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.
-.\"
-.\" $FreeBSD: src/share/man/man4/man4.i386/padlock.4,v 1.8 2007/08/02 08:04:48 bz Exp $
-.\"
-.Dd April 21, 2018
-.Dt PADLOCK 4
-.Os
-.Sh NAME
-.Nm padlock
-.Nd "driver for the cryptographic functions and RNG in VIA C3, C7 and Eden processors"
-.Sh SYNOPSIS
-To compile this driver into the kernel,
-place the following lines in your
-kernel configuration file:
-.Bd -ragged -offset indent
-.Cd "device crypto"
-.Cd "device padlock"
-.Ed
-.Pp
-Alternatively, to load the driver as a
-module at boot time, place the following line in
-.Xr loader.conf 5 :
-.Bd -literal -offset indent
-padlock_load="YES"
-.Ed
-.Sh DESCRIPTION
-The C3 and Eden processor series from VIA include hardware acceleration for
-AES.
-The C7 series includes hardware acceleration for AES, SHA1, SHA256 and RSA.
-All of the above processor series include a hardware random number generator.
-.Pp
-The
-.Nm
-driver registers itself to accelerate AES operations and, if available, HMAC/SHA1
-and HMAC/SHA256 for
-.Xr crypto 4 .
-It also registers itself to accelerate other HMAC algorithms, although
-there is no hardware acceleration for those algorithms.
-This is due to historical reasons, so that padlock could work with
-.Em IPsec .
-.Pp
-The hardware random number generator supplies data for the kernel
-.Xr random 4
-subsystem.
-.Sh SEE ALSO
-.Xr crypt 3 ,
-.Xr crypto 4 ,
-.Xr intro 4 ,
-.Xr random 4 ,
-.Xr crypto 9
-.Sh HISTORY
-The
-.Nm
-driver first appeared in
-.Ox .
-The first
-.Fx
-release to include it was
-.Fx 6.0 .
-.Pp
-It was imported into
-.Dx 2.5 .
-.Sh AUTHORS
-.An -nosplit
-The
-.Nm
-driver with AES encryption support was written by
-.An Jason Wright Aq Mt jason@OpenBSD.org .
-It was ported to
-.Fx
-and then extended to support SHA1 and SHA256
-by
-.An Pawel Jakub Dawidek Aq Mt pjd@FreeBSD.org .
-This manual page was written by
-.An Christian Brueffer Aq Mt brueffer@FreeBSD.org .
diff --git a/share/man/man4/rdrand.4 b/share/man/man4/rdrand.4
index 989ab47174..1898c8a6dd 100644
--- a/share/man/man4/rdrand.4
+++ b/share/man/man4/rdrand.4
@@ -62,7 +62,6 @@ The
 driver continuously feeds entropy to the kernel's random number generator.
 .Sh SEE ALSO
 .Xr intro 4 ,
-.Xr padlock 4 ,
 .Xr random 4
 .Sh HISTORY
 The
diff --git a/share/man/man4/rndtest.4 b/share/man/man4/rndtest.4
index 092e5a12b4..3f27e5ee35 100644
--- a/share/man/man4/rndtest.4
+++ b/share/man/man4/rndtest.4
@@ -47,11 +47,7 @@ it is discarded and testing is continuously applied until ``good data''
 is received from the device.
 Failures are optionally reported on the console.
 .Sh SEE ALSO
-.Xr crypto 4 ,
-.Xr hifn 4 ,
 .Xr random 4 ,
-.Xr safe 4 ,
-.Xr ubsec 4 ,
 .Xr crypto 9
 .Sh HISTORY
 The idea for this and the original code came from Jason L. Wright.
diff --git a/share/man/man4/safe.4 b/share/man/man4/safe.4
deleted file mode 100644
index 0b62b36cdb..0000000000
--- a/share/man/man4/safe.4
+++ /dev/null
@@ -1,126 +0,0 @@
-.\"-
-.\" Copyright (c) 2003	Sam Leffler, Errno Consulting
-.\" 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 AUTHOR 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 AUTHOR 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.
-.\"
-.\" $FreeBSD: src/share/man/man4/safe.4,v 1.7 2006/04/01 10:56:36 brueffer Exp $
-.\"
-.Dd April 21, 2018
-.Dt SAFE 4
-.Os
-.Sh NAME
-.Nm safe
-.Nd SafeNet crypto accelerator
-.Sh SYNOPSIS
-To compile this driver into the kernel,
-place the following lines in your
-kernel configuration file:
-.Bd -ragged -offset indent
-.Cd "device crypto"
-.Cd "device cryptodev"
-.Cd "device safe"
-.Ed
-.Pp
-Alternatively, to load the driver as a
-module at boot time, place the following line in
-.Xr loader.conf 5 :
-.Bd -literal -offset indent
-safe_load="YES"
-.Ed
-.Pp
-.Nm sysctl Va hw.safe.debug
-.Nm sysctl Va hw.safe.dump
-.Nm sysctl Va hw.safe.rnginterval
-.Nm sysctl Va hw.safe.rngbufsize
-.Nm sysctl Va hw.safe.rngmaxalarm
-.Sh DESCRIPTION
-The
-.Nm
-driver supports cards containing SafeNet crypto accelerator chips.
-.Pp
-The
-.Nm
-driver registers itself to accelerate DES, Triple-DES, AES, MD5-HMAC,
-SHA1-HMAC, and NULL operations for
-.Xr crypto 4 .
-.Pp
-On all models, the driver registers itself to provide random data to the
-.Xr random 4
-subsystem.
-Periodically the driver will poll the hardware RNG and retrieve
-data for use by the system.
-If the driver detects that the hardware RNG is resonating with any local
-signal, it will reset the oscillators that generate random data.
-Three
-.Xr sysctl 8
-settings control this procedure:
-.Va hw.safe.rnginterval
-specifies the time, in seconds, between polling operations,
-.Va hw.safe.rngbufsize
-specifies the number of 32-bit words to retrieve on each poll,
-and
-.Va hw.safe.rngmaxalarm
-specifies the threshold for resetting the oscillators.
-.Pp
-When the driver is compiled with
-.Dv SAFE_DEBUG
-defined, two
-.Xr sysctl 8
-variables are provided for debugging purposes:
-.Va hw.safe.debug
-can be set to a non-zero value to enable debugging messages to be sent
-to the console for each cryptographic operation,
-.Va hw.safe.dump
-is a write-only variable that can be used to force driver state to be sent
-to the console.
-Set this variable to
-.Dq Li ring
-to dump the current state of the descriptor ring,
-to
-.Dq Li dma
-to dump the hardware DMA registers,
-or
-to
-.Dq Li int
-to dump the hardware interrupt registers.
-.Sh HARDWARE
-The
-.Nm
-driver supports cards containing any of the following chips:
-.Bl -tag -width "SafeNet 1141" -offset indent
-.It SafeNet 1141
-The original chipset.
-Supports DES, Triple-DES, AES, MD5, and SHA-1
-symmetric crypto operations, RNG, public key operations, and full IPsec
-packet processing.
-.It SafeNet 1741
-A faster version of the 1141.
-.El
-.Sh SEE ALSO
-.Xr crypt 3 ,
-.Xr crypto 4 ,
-.Xr intro 4 ,
-.Xr random 4 ,
-.Xr crypto 9
-.Sh BUGS
-Public key support is not implemented.
diff --git a/share/man/man4/ubsec.4 b/share/man/man4/ubsec.4
deleted file mode 100644
index dfb504df0c..0000000000
--- a/share/man/man4/ubsec.4
+++ /dev/null
@@ -1,104 +0,0 @@
-.\"	$OpenBSD: ubsec.4,v 1.18 2002/09/26 07:55:41 miod Exp $
-.\"	$FreeBSD: src/share/man/man4/ubsec.4,v 1.1.2.1 2002/11/21 23:57:24 sam Exp $
-.\"
-.\" Copyright (c) 2000 Jason L. Wright (jason@thought.net)
-.\" 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.
-.\" 3. All advertising materials mentioning features or use of this software
-.\"    must display the following acknowledgement:
-.\"	This product includes software developed by Jason L. Wright
-.\" 4. The name of the author may not be used to endorse or promote products
-.\"    derived from this software without specific prior written permission.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
-.\"
-.Dd April 21, 2018
-.Dt UBSEC 4
-.Os
-.Sh NAME
-.Nm ubsec
-.Nd Broadcom and BlueSteel uBsec 5x0x crypto accelerator
-.Sh SYNOPSIS
-.Cd device ubsec
-.Sh DESCRIPTION
-The
-.Nm
-driver supports cards containing any of the following chips:
-.Bl -tag -width "Broadcom BCM5821" -offset indent
-.It Bluesteel 5501
-The original chipset, no longer made.  This extremely rare unit
-was not very fast, lacked a RNG, and had a number of other bugs.
-.It Bluesteel 5601
-A faster and fixed version of the original, with a random number
-unit and large number engine added.
-.It Broadcom BCM5801
-A BCM5805 without public key engine or random number generator.
-.It Broadcom BCM5802
-A slower version of the BCM5805.
-.It Broadcom BCM5805
-Faster version of Bluesteel 5601.
-.It Broadcom BCM5820
-64 bit version of the chip, and significantly more advanced.
-.It Broadcom BCM5821
-Faster version of the BCM5820.
-.It Broadcom BCM5822
-Faster version of the BCM5820.
-.It Broadcom BCM5823
-A BCM5822 with AES capability.
-.It Broadcom BCM5825
-Faster version of the BCM5823.
-.El
-.Pp
-The
-.Nm
-driver registers itself to accelerate DES, Triple-DES, MD5-HMAC,
-and SHA1-HMAC operations for
-.Xr crypto 4 .
-.Pp
-On those models which contain a public key engine (almost all of the
-more recent ones), this feature is registered with the
-.Xr crypto 4
-subsystem.
-.Pp
-On all models except the Bluesteel 5501 and Broadcom 5801, the driver
-registers itself to provide random data to the
-.Xr random 4
-subsystem.
-.Sh SEE ALSO
-.Xr crypt 3 ,
-.Xr crypto 4 ,
-.Xr intro 4 ,
-.Xr random 4 ,
-.Xr crypto 9
-.Sh HISTORY
-The
-.Nm
-device driver appeared in
-.Ox 2.8 .
-The
-.Nm
-device driver was imported to
-.Fx
-in 5.0.
-.Sh BUGS
-The BCM5801 and BCM5802 have not actually been tested.
-The AES capability of the BCM5823 is not yet supported; it is awaiting
-public disclosure of programming information from Broadcom.
diff --git a/share/man/man7/initrd.7 b/share/man/man7/initrd.7
index 3dcd56a622..34ce5d5271 100644
--- a/share/man/man7/initrd.7
+++ b/share/man/man7/initrd.7
@@ -47,7 +47,6 @@ scripts, and many other statically linked tools, such as various
 utilities,
 .Xr lvm 8 ,
 .Xr cryptsetup 8 ,
-.Xr tcplay 8 ,
 as well as some networking tools.
 The
 .Nm
@@ -100,9 +99,8 @@ vfs.root.realroot="<type>:[arg1[:arg2[:...]]]"
 .Pp
 Currently, the supported types of real root partition are:
 .Pa local ,
-.Pa crypt ,
 and
-.Pa tcplay .
+.Pa crypt .
 The
 .Pa local
 type allows to mount any local filesystem that does not require any
@@ -116,20 +114,13 @@ vfs.root.realroot="local:<FS-TYPE>:<DEVICE-PATH>[:<OPTIONS>]"
 .Pp
 The
 .Pa crypt
-and
-.Pa tcplay
-types allow to mount volumes supported by
-.Xr cryptsetup 8
-and
-.Xr tcplay 8 ,
-respectively.
+type allows to mount volumes supported by
+.Xr cryptsetup 8 .
 The device will be set up after asking the user for the key,
 and this volume will then be mounted as the root.
 The general formats are as follows:
 .Bd -literal -offset indent
 vfs.root.realroot="crypt:<FS-TYPE>:<DEVICE-PATH>:<MAPPING-NAME>[:<OPTIONS>]"
-
-vfs.root.realroot="tcplay:<FS-TYPE>:<DEVICE-PATH>:<MAPPING-NAME>[:<OPTIONS>]"
 .Ed
 .Sh FILES
 .Bl -tag -width "/boot/kernel/initrd.img.gz"
@@ -152,7 +143,6 @@ vfs.root.realroot="crypt:hammer2:/dev/serno/XXXXXX.s1d:root[:OPTIONS]"
 .Xr build 7 ,
 .Xr cryptsetup 8 ,
 .Xr lvm 8 ,
-.Xr tcplay 8
 .Sh HISTORY
 The
 .Nm
diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile
index 13d65b234b..447eb78f4d 100644
--- a/share/man/man9/Makefile
+++ b/share/man/man9/Makefile
@@ -447,19 +447,10 @@ MLINKS+=crit_enter.9 crit_enter_gd.9 \
 	crit_enter.9 crit_exit_id.9
 MLINKS+=crypto.9 crypto_dispatch.9 \
 	crypto.9 crypto_done.9 \
-	crypto.9 crypto_find_device_byhid.9 \
-	crypto.9 crypto_find_driver.9 \
-	crypto.9 crypto_freereq.9 \
 	crypto.9 crypto_freesession.9 \
-	crypto.9 crypto_getcaps.9 \
 	crypto.9 crypto_get_driverid.9 \
-	crypto.9 crypto_getreq.9 \
-	crypto.9 crypto_kdispatch.9 \
-	crypto.9 crypto_kdone.9 \
-	crypto.9 crypto_kregister.9 \
 	crypto.9 crypto_newsession.9 \
 	crypto.9 crypto_register.9 \
-	crypto.9 crypto_unblock.9 \
 	crypto.9 crypto_unregister.9 \
 	crypto.9 crypto_unregister_all.9
 MLINKS+=DB_COMMAND.9 DB_COMMAND_FLAG.9 \
diff --git a/share/man/man9/crypto.9 b/share/man/man9/crypto.9
index 7eb6d0ea9e..9a9f0d903b 100644
--- a/share/man/man9/crypto.9
+++ b/share/man/man9/crypto.9
@@ -30,37 +30,19 @@
 .Ft int32_t
 .Fn crypto_get_driverid "device_t dev" "int flags"
 .Ft int
-.Fn crypto_find_driver "const char *match"
-.Ft device_t
-.Fn crypto_find_device_byhid "int hid"
-.Ft int
-.Fn crypto_getcaps "int hid"
-.Ft int
 .Fn crypto_register "u_int32_t driverid" "int alg" "u_int16_t maxoplen" "u_int32_t flags"
 .Ft int
-.Fn crypto_kregister "u_int32_t driverid" "int kalg" "u_int32_t flags"
-.Ft int
 .Fn crypto_unregister "u_int32_t driverid" "int alg"
 .Ft int
 .Fn crypto_unregister_all "u_int32_t driverid"
 .Ft void
 .Fn crypto_done "struct cryptop *crp"
-.Ft void
-.Fn crypto_kdone "struct cryptkop *krp"
 .Ft int
 .Fn crypto_newsession "u_int64_t *sid" "struct cryptoini *cri" "int crid"
 .Ft int
 .Fn crypto_freesession "u_int64_t sid"
 .Ft int
 .Fn crypto_dispatch "struct cryptop *crp"
-.Ft int
-.Fn crypto_kdispatch "struct cryptkop *krp"
-.Ft int
-.Fn crypto_unblock "u_int32_t driverid" "int what"
-.Ft "struct cryptop *"
-.Fn crypto_getreq "int num"
-.Ft void
-.Fn crypto_freereq "struct cryptop *crp"
 .Bd -literal
 #define	CRYPTO_SYMQ	0x1
 #define	CRYPTO_ASYMQ	0x2
@@ -70,23 +52,16 @@
 struct cryptoini {
 	int                cri_alg;
 	int                cri_klen;
-	int                cri_mlen;
 	caddr_t            cri_key;
 	u_int8_t           cri_iv[EALG_MAX_BLOCK_LEN];
-	struct cryptoini  *cri_next;
 };
 
 struct cryptodesc {
-	int                crd_skip;
 	int                crd_len;
-	int                crd_inject;
 	int                crd_flags;
 	struct cryptoini   CRD_INI;
 #define crd_iv          CRD_INI.cri_iv
-#define crd_key         CRD_INI.cri_key
 #define crd_alg         CRD_INI.cri_alg
-#define crd_klen        CRD_INI.cri_klen
-	struct cryptodesc *crd_next;
 };
 
 struct cryptop {
@@ -94,32 +69,14 @@ struct cryptop {
 	u_int64_t          crp_sid;
 	int                crp_ilen;
 	int                crp_olen;
-	int                crp_etype;
 	int                crp_flags;
 	caddr_t            crp_buf;
 	caddr_t            crp_opaque;
 	struct cryptodesc *crp_desc;
-	int              (*crp_callback) (struct cryptop *);
 	caddr_t            crp_mac;
 };
 
-struct crparam {
-        caddr_t         crp_p;
-        u_int           crp_nbits;
-};
-
 #define CRK_MAXPARAM    8
-
-struct cryptkop {
-        TAILQ_ENTRY(cryptkop) krp_next;
-        u_int              krp_op;         /* ie. CRK_MOD_EXP or other */
-        u_int              krp_status;     /* return status */
-        u_short            krp_iparams;    /* # of input parameters */
-        u_short            krp_oparams;    /* # of output parameters */
-	u_int32_t	   krp_hid;
-        struct crparam     krp_param[CRK_MAXPARAM];
-        int               (*krp_callback)(struct cryptkop *);
-};
 .Ed
 .Sh DESCRIPTION
 .Nm
@@ -149,20 +106,10 @@ Since the consumers may not be associated with a process, drivers may
 not
 .Xr sleep 9 .
 The same holds for the framework.
-Thus, a callback mechanism is used
-to notify a consumer that a request has been completed (the
-callback is specified by the consumer on an per-request basis).
-The callback is invoked by the framework whether the request was
-successfully completed or not.
-An error indication is provided in the latter case.
 A specific error code,
 .Er EAGAIN ,
 is used to indicate that a session number has changed and that the
 request may be re-submitted immediately with the new session number.
-Errors are only returned to the invoking function if not
-enough information to call the callback is available (meaning, there
-was a fatal error in verifying the arguments).
-For session initialization and teardown there is no callback mechanism used.
 .Pp
 The
 .Fn crypto_newsession
@@ -176,20 +123,15 @@ hardware driver (1) should be used or not (0).
 The various fields in the
 .Vt cryptoini
 structure are:
-.Bl -tag -width ".Va cri_next"
+.Bl -tag -width ".Va cri_alg"
 .It Va cri_alg
 Contains an algorithm identifier.
 Currently supported algorithms are:
 .Pp
-.Bl -tag -width ".Dv CRYPTO_RIPEMD160_HMAC" -compact
+.Bl -tag -width ".Dv CRYPTO_CAMELLIA_CBC" -compact
 .It Dv CRYPTO_AES_CBC
 .It Dv CRYPTO_AES_XTS
 .It Dv CRYPTO_AES_CTR
-.It Dv CRYPTO_AES_GCM_16
-.It Dv CRYPTO_AES_GMAC
-.It Dv CRYPTO_AES_128_GMAC
-.It Dv CRYPTO_AES_192_GMAC
-.It Dv CRYPTO_AES_256_GMAC
 .It Dv CRYPTO_TWOFISH_CBC
 .It Dv CRYPTO_TWOFISH_XTS
 .It Dv CRYPTO_SERPENT_CBC
@@ -201,25 +143,11 @@ Currently supported algorithms are:
 .It Dv CRYPTO_DES_CBC
 .It Dv CRYPTO_3DES_CBC
 .It Dv CRYPTO_SKIPJACK_CBC
-.It Dv CRYPTO_MD5
-.It Dv CRYPTO_MD5_HMAC
-.It Dv CRYPTO_MD5_KPDK
-.It Dv CRYPTO_RIPEMD160_HMAC
-.It Dv CRYPTO_SHA1
-.It Dv CRYPTO_SHA1_HMAC
-.It Dv CRYPTO_SHA1_KPDK
-.It Dv CRYPTO_SHA2_256_HMAC
-.It Dv CRYPTO_SHA2_384_HMAC
-.It Dv CRYPTO_SHA2_512_HMAC
-.It Dv CRYPTO_NULL_HMAC
 .It Dv CRYPTO_NULL_CBC
 .El
 .It Va cri_klen
 Specifies the length of the key in bits, for variable-size key
 algorithms.
-.It Va cri_mlen
-Specifies how many bytes from the calculated hash should be copied back.
-0 means entire hash.
 .It Va cri_key
 Contains the key to be used with the algorithm.
 .It Va cri_iv
@@ -228,11 +156,6 @@ the data.
 This field is ignored during initialization.
 If no IV is explicitly passed (see below on details), a random IV is used
 by the device driver processing the request.
-.It Va cri_next
-Contains a pointer to another
-.Vt cryptoini
-structure.
-Multiple such structures may be linked to establish multi-algorithm sessions.
 .El
 .Pp
 The
@@ -253,7 +176,7 @@ is called to process a request.
 The various fields in the
 .Vt cryptop
 structure are:
-.Bl -tag -width ".Va crp_callback"
+.Bl -tag -width ".Va crp_ilen"
 .It Va crp_sid
 Contains the SID.
 .It Va crp_ilen
@@ -263,69 +186,19 @@ On return, contains the total length of the result.
 For symmetric crypto operations, this will be the same as the input length.
 This will be used if the framework needs to allocate a new
 buffer for the result (or for re-formatting the input).
-.It Va crp_callback
-This routine is invoked upon completion of the request, whether
-successful or not.
-It is invoked through the
-.Fn crypto_done
-routine.
-If the request was not successful, an error code is set in the
-.Va crp_etype
-field.
-It is the responsibility of the callback routine to enter a critical
-section.
-.It Va crp_etype
-Contains the error type, if any errors were encountered, or zero if
-the request was successfully processed.
-If the
-.Er EAGAIN
-error code is returned, the SID has changed (and has been recorded in the
-.Va crp_sid
-field).
-The consumer should record the new SID and use it in all subsequent requests.
-In this case, the request may be re-submitted immediately.
-This mechanism is used by the framework to perform
-session migration (move a session from one driver to another, because
-of availability, performance, or other considerations).
+.Fn crypto_dispatch
+returns the error code. 
 .Pp
-Note that this field only makes sense when examined by
-the callback routine specified in
-.Va crp_callback .
-Errors are returned to the invoker of
-.Fn crypto_process
-only when enough information is not present to call the callback
-routine (i.e., if the pointer passed is
-.Dv NULL
-or if no callback routine was specified).
 .It Va crp_flags
 Is a bitmask of flags associated with this request.
 Currently defined flags are:
-.Bl -tag -width ".Dv CRYPTO_F_CBIFSYNC"
-.It Dv CRYPTO_F_IMBUF
-The buffer pointed to by
-.Va crp_buf
-is an mbuf chain.
-.It Dv CRYPTO_F_IOV
-The buffer pointed to by
-.Va crp_buf
-is an
-.Vt uio
-structure.
-.It Dv CRYPTO_F_REL
-Must return data in the same place.
-.It Dv CRYPTO_F_BATCH
-Batch operation if possible.
-.It Dv CRYPTO_F_CBIMM
-Do callback immediately instead of doing it from a dedicated kernel thread.
+.Bl -tag -width ".Dv CRYPTO_F_DONE"
 .It Dv CRYPTO_F_DONE
 Operation completed.
-.It Dv CRYPTO_F_CBIFSYNC
-Do callback immediately if operation is synchronous.
 .El
 .It Va crp_buf
 Points to the input buffer.
-On return (when the callback is invoked),
-it contains the result of the request.
+On return it contains the result of the request.
 The input buffer may be an mbuf
 chain or a contiguous buffer,
 depending on
@@ -339,85 +212,21 @@ Each descriptor provides
 information about what type of cryptographic operation should be done
 on the input buffer.
 The various fields are:
-.Bl -tag -width ".Va crd_inject"
 .It Va crd_iv
-The field where IV should be provided when the
-.Dv CRD_F_IV_EXPLICIT
-flag is given.
-.It Va crd_key
-When the
-.Dv CRD_F_KEY_EXPLICIT
-flag is given, the
-.Va crd_key
-points to a buffer with encryption or authentication key.
+The field where IV should be provided.
 .It Va crd_alg
 An algorithm to use.
 Must be the same as the one given at newsession time.
-.It Va crd_klen
-The
-.Va crd_key
-key length.
-.It Va crd_skip
-The offset in the input buffer where processing should start.
 .It Va crd_len
-How many bytes, after
-.Va crd_skip ,
-should be processed.
-.It Va crd_inject
-Offset from the beginning of the buffer to insert any results.
-For encryption algorithms, this is where the initialization vector
-(IV) will be inserted when encrypting or where it can be found when
-decrypting (subject to
-.Va crd_flags ) .
-For MAC algorithms, this is where the result of the keyed hash will be
-inserted.
+How many bytes should be processed.
 .It Va crd_flags
 The following flags are defined:
 .Bl -tag -width 3n
 .It Dv CRD_F_ENCRYPT
 For encryption algorithms, this bit is set when encryption is required
 (when not set, decryption is performed).
-.It Dv CRD_F_IV_PRESENT
-For encryption algorithms, this bit is set when the IV already
-precedes the data, so the
-.Va crd_inject
-value will be ignored and no IV will be written in the buffer.
-Otherwise, the IV used to encrypt the packet will be written
-at the location pointed to by
-.Va crd_inject .
 The IV length is assumed to be equal to the blocksize of the
 encryption algorithm.
-Some applications that do special
-.Dq "IV cooking"
-can use this flag to indicate that the IV should not be written on the packet.
-This flag is typically used in conjunction with the
-.Dv CRD_F_IV_EXPLICIT
-flag.
-.It Dv CRD_F_IV_EXPLICIT
-For encryption algorithms, this bit is set when the IV is explicitly
-provided by the consumer in the
-.Va crd_iv
-field.
-Otherwise, for encryption operations the IV is provided for by
-the driver used to perform the operation, whereas for decryption
-operations it is pointed to by the
-.Va crd_inject
-field.
-This flag is typically used when the IV is calculated
-.Dq "on the fly"
-by the consumer, and does not precede the data
-(encrypted swap being an example).
-.It Dv CRD_F_KEY_EXPLICIT
-For encryption and authentication (MAC) algorithms, this bit is set when the key
-is explicitly provided by the consumer in the
-.Va crd_key
-field for the given operation.
-Otherwise, the key is taken at newsession time from the
-.Va cri_key
-field.
-.It Dv CRD_F_COMP
-For compression algorithms, this bit is set when compression is required (when
-not set, decompression is performed).
 .El
 .It Va CRD_INI
 This
@@ -429,68 +238,12 @@ operation request, drivers may re-initialize state on-demand
 Furthermore, the cryptographic
 framework may re-route requests as a result of full queues or hardware
 failure, as described above.
-.It Va crd_next
-Point to the next descriptor.
-Linked operations are useful in protocols
-where multiple cryptographic transforms may be applied on the same
-block of data.
-.El
-.El
-.Pp
-.Fn crypto_getreq
-allocates a
-.Vt cryptop
-structure with a linked list of as many
-.Vt cryptodesc
-structures as were specified in the argument passed to it.
-.Pp
-.Fn crypto_freereq
-deallocates a structure
-.Vt cryptop
-and any
-.Vt cryptodesc
-structures linked to it.
-Note that it is the responsibility of the
-callback routine to do the necessary cleanups associated with the
-opaque field in the
-.Vt cryptop
-structure.
-.Pp
-.Fn crypto_kdispatch
-is called to perform a keying operation.
-The various fields in the
-.Vt cryptkop
-structure are:
-.Bl -tag -width ".Va krp_callback"
-.It Va krp_op
-Operation code, such as
-.Dv CRK_MOD_EXP .
-.It Va krp_status
-Return code.
-This
-.Va errno Ns -style
-variable indicates whether lower level reasons
-for operation failure.
-.It Va krp_iparams
-Number if input parameters to the specified operation.
-Note that each operation has a (typically hardwired) number of such parameters.
-.It Va krp_oparams
-Number if output parameters from the specified operation.
-Note that each operation has a (typically hardwired) number of such parameters.
-.It Va krp_kvp
-An array of kernel memory blocks containing the parameters.
-.It Va krp_hid
-Identifier specifying which low-level driver is being used.
-.It Va krp_callback
-Callback called on completion of a keying operation.
 .El
 .Sh DRIVER-SIDE API
 The
 .Fn crypto_get_driverid ,
 .Fn crypto_register ,
-.Fn crypto_kregister ,
 .Fn crypto_unregister ,
-.Fn crypto_unblock ,
 and
 .Fn crypto_done
 routines are used by drivers that provide support for cryptographic
@@ -542,9 +295,6 @@ The calling convention for the three driver-supplied routines is:
 .It
 .Ft int
 .Fn (*process) "void *" "struct cryptop *" ;
-.It
-.Ft int
-.Fn (*kprocess) "void *" "struct cryptkop *" ;
 .El
 .Pp
 On invocation, the first argument to
@@ -573,78 +323,23 @@ The
 routine is invoked with a request to perform crypto processing.
 This routine must not block, but should queue the request and return
 immediately.
-Upon processing the request, the callback routine should be invoked.
-In case of an unrecoverable error, the error indication must be placed in the
-.Va crp_etype
-field of the
-.Vt cryptop
-structure.
-When the request is completed, or an error is detected, the
-.Fn process
-routine should invoke
-.Fn crypto_done .
 Session migration may be performed, as mentioned previously.
 .Pp
-In case of a temporary resource exhaustion, the
-.Fn process
-routine may return
-.Er ERESTART
-in which case the crypto services will requeue the request, mark the driver
-as
-.Dq blocked ,
-and stop submitting requests for processing.
-The driver is then responsible for notifying the crypto services
-when it is again able to process requests through the
-.Fn crypto_unblock
-routine.
-This simple flow control mechanism should only be used for short-lived
-resource exhaustion as it causes operations to be queued in the crypto
-layer.
-Doing so is preferable to returning an error in such cases as
-it can cause network protocols to degrade performance by treating the
-failure much like a lost packet.
-.Pp
-The
-.Fn kprocess
-routine is invoked with a request to perform crypto key processing.
-This routine must not block, but should queue the request and return
-immediately.
-Upon processing the request, the callback routine should be invoked.
-In case of an unrecoverable error, the error indication must be placed in the
-.Va krp_status
-field of the
-.Vt cryptkop
-structure.
-When the request is completed, or an error is detected, the
-.Fn kprocess
-routine should invoked
-.Fn crypto_kdone .
 .Sh RETURN VALUES
 .Fn crypto_register ,
-.Fn crypto_kregister ,
 .Fn crypto_unregister ,
 .Fn crypto_newsession ,
-.Fn crypto_freesession ,
 and
-.Fn crypto_unblock
+.Fn crypto_freesession
 return 0 on success, or an error code on failure.
 .Fn crypto_get_driverid
 returns a non-negative value on error, and \-1 on failure.
-.Fn crypto_getreq
-returns a pointer to a
-.Vt cryptop
-structure and
-.Dv NULL
-on failure.
 .Fn crypto_dispatch
 returns
 .Er EINVAL
-if its argument or the callback function was
+if its argument was
 .Dv NULL ,
-and 0 otherwise.
-The callback is provided with an error code in case of failure, in the
-.Va crp_etype
-field.
+any other error code or 0 in case of success.
 .Sh FILES
 .Bl -tag -width ".Pa sys/opencrypto/crypto.c"
 .It Pa sys/opencrypto/crypto.c
diff --git a/share/mk/bsd.libnames.mk b/share/mk/bsd.libnames.mk
index 436a1d43e5..9306f44314 100644
--- a/share/mk/bsd.libnames.mk
+++ b/share/mk/bsd.libnames.mk
@@ -77,7 +77,6 @@ LIBSMB?=	${DESTDIR}${LIBDIR}/libsmb.a
 LIBSSH?=	${DESTDIR}${LIBDIR}/priv/libprivate_ssh.a
 LIBSSL?=	${DESTDIR}${LIBDIR}/priv/libprivate_ssl.a
 LIBSTDBUF?=	${DESTDIR}${LIBDIR}/libstdbuf.a
-LIBTCPLAY?=	${DESTDIR}${LIBDIR}/libtcplay.a
 LIBUSBHID?=	${DESTDIR}${LIBDIR}/libusbhid.a
 LIBUSB?=	${DESTDIR}${LIBDIR}/libusb.a
 LIBUTIL?=	${DESTDIR}${LIBDIR}/libutil.a
diff --git a/sys/conf/files b/sys/conf/files
index 5b6757b66e..82d5788fe8 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -1216,7 +1216,6 @@ dev/misc/pps/pps.c		optional pps
 dev/disk/vpo/vpo.c		optional vpo
 dev/disk/vpo/vpoio.c	optional vpo
 dev/crypto/rndtest/rndtest.c	optional rndtest
-dev/crypto/safe/safe.c		optional safe
 dev/netif/sbsh/if_sbsh.c	optional sbsh
 dev/disk/sdhci/sdhci.c		optional sdhci
 dev/disk/sdhci/sdhci_if.m	optional sdhci
@@ -2245,10 +2244,9 @@ libprop/prop_object.c			standard
 libprop/prop_stack.c			standard
 libprop/prop_string.c			standard
 libprop/prop_rb.c			standard
+crypto/crypto_mod.c			optional crypto
+crypto/crypto_cipher.c			optional crypto
 crypto/blake2/blake2s.c			optional crypto
-crypto/blowfish/bf_skey.c		optional crypto
-crypto/camellia/camellia.c		optional crypto
-crypto/camellia/camellia-api.c		optional crypto
 crypto/chacha20/chacha.c		optional crypto
 crypto/chachapoly.c			optional crypto
 crypto/curve25519/curve25519.c		optional crypto
@@ -2259,25 +2257,14 @@ crypto/rc4/rc4.c			optional crypto
 crypto/rijndael/rijndael-alg-fst.c	optional crypto
 crypto/rijndael/rijndael-api-fst.c	optional crypto
 crypto/rijndael/rijndael-api.c		optional crypto
+crypto/rmd160/rmd160.c			optional crypto
 crypto/sha1.c				optional crypto
 crypto/sha2/sha2.c			standard
 crypto/siphash/siphash.c		optional crypto
-crypto/twofish/twofish.c		optional crypto
-crypto/serpent/serpent.c		optional crypto
-net/zlib.c				optional crypto
-opencrypto/cast.c			optional crypto
-opencrypto/criov.c			optional crypto
-opencrypto/crypto.c			optional crypto
-opencrypto/cryptodev.c			optional cryptodev
-opencrypto/cryptodev_if.m		optional crypto
-opencrypto/cryptosoft.c			optional crypto
-opencrypto/deflate.c			optional crypto
-opencrypto/gmac.c			optional crypto
-opencrypto/rmd160.c			optional crypto
-opencrypto/skipjack.c			optional crypto
-opencrypto/xform.c			optional crypto
-dev/crypto/hifn/hifn7751.c		optional hifn
-dev/crypto/ubsec/ubsec.c		optional ubsec
+crypto/aesni/aesni.c			optional crypto
+crypto/aesni/aesencdec_x86_64.S		optional crypto
+crypto/aesni/aeskeys_x86_64.S		optional crypto
+
 # drm_agpsupport.c must be included if CONFIG_AGP is set
 #dev/drm/drm_agpsupport.c		optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
 dev/drm/drm_atomic.c			optional drm compile-with "${NORMAL_C} -include $S/dev/drm/kconfig.h ${WERROR}"
diff --git a/sys/conf/kmod.mk b/sys/conf/kmod.mk
index 764df1c766..3ffae6dce0 100644
--- a/sys/conf/kmod.mk
+++ b/sys/conf/kmod.mk
@@ -350,7 +350,7 @@ MFILES?= kern/bus_if.m kern/device_if.m bus/iicbus/iicbb_if.m \
     dev/sound/pcm/feeder_if.m dev/sound/pcm/mixer_if.m \
     dev/sound/midi/mpu_if.m dev/sound/midi/mpufoi_if.m \
     dev/sound/midi/synth_if.m  \
-    libiconv/iconv_converter_if.m dev/agp/agp_if.m opencrypto/cryptodev_if.m \
+    libiconv/iconv_converter_if.m dev/agp/agp_if.m \
     bus/mmc/mmcbus_if.m bus/mmc/mmcbr_if.m \
     dev/virtual/virtio/virtio/virtio_bus_if.m \
     dev/misc/backlight/backlight_if.m dev/misc/coremctl/coremctl_if.m kern/cpu_if.m \
diff --git a/sys/conf/options b/sys/conf/options
index fd3c0d6bcd..4d8dc7a748 100644
--- a/sys/conf/options
+++ b/sys/conf/options
@@ -504,21 +504,6 @@ PCFCLOCK_MAX_RETRIES	opt_pcfclock.h
 # Network device queue based polling
 IFPOLL_ENABLE		opt_ifpoll.h
 
-# options for ubsec driver
-UBSEC_DEBUG		opt_ubsec.h
-UBSEC_RNDTEST		opt_ubsec.h
-UBSEC_NO_RNG		opt_ubsec.h
-
-# options for hifn driver
-HIFN_DEBUG		opt_hifn.h
-HIFN_RNDTEST		opt_hifn.h
-HIFN_NO_RNG		opt_hifn.h
-
-# options for safenet driver
-SAFE_DEBUG		opt_safe.h
-SAFE_NO_RNG		opt_safe.h
-SAFE_RNDTEST		opt_safe.h
-
 # KTR options
 KTR				opt_global.h
 KTR_ENTRIES			opt_global.h
diff --git a/sys/config/LINT64 b/sys/config/LINT64
index e2c4484f5b..d5d84e8885 100644
--- a/sys/config/LINT64
+++ b/sys/config/LINT64
@@ -408,7 +408,7 @@ options                ACCEPT_FILTER_HTTP
 # carried in TCP option 19.
 # This is enabled on a per-socket basis using the TCP_SIGNATURE_ENABLE
 # socket option.
-# This requires the use of 'device crypto' or 'device cryptodev'.
+# This requires the use of 'device crypto'
 #
 # XXX disabled for now until building with it is fixed, which broke
 # after removing IPsec.
@@ -2022,27 +2022,9 @@ options		DCONS_FORCE_GDB=1	# force to be the gdb device
 # been fed back to openbsd (and hopefully will be included).
 
 device		crypto		# core crypto support
-device		cryptodev	# /dev/crypto for access to h/w
 
 device		rndtest		# FIPS 140-2 entropy tester
 
-device		hifn		# Hifn 7951, 7781, etc.
-options		HIFN_DEBUG	# enable debugging support: hw.hifn.debug
-#options	HIFN_NO_RNG	# for devices without RNG
-options		HIFN_RNDTEST	# enable rndtest support
-
-device		safe		# SafeNet 1141
-options 	SAFE_DEBUG	# enable debugging support: hw.safe.debug
-#options	SAFE_NO_RNG	# for devices without RNG
-options 	SAFE_RNDTEST	# enable rndtest support
-
-device		ubsec		# Broadcom 5501, 5601, 58xx
-options		UBSEC_DEBUG	# enable debugging support: hw.ubsec.debug
-#options	UBSEC_NO_RNG	# for devices without RNG
-options		UBSEC_RNDTEST	# enable rndtest support
-
-device		aesni		# hardware crypto/RNG for AES-NI
-device		padlock		# hardware crypto/RNG for VIA C3/C7/Eden
 device		rdrand		# hardware RNG for RdRand
 
 #
diff --git a/sys/config/X86_64_GENERIC b/sys/config/X86_64_GENERIC
index 92c685c7df..eec377116b 100644
--- a/sys/config/X86_64_GENERIC
+++ b/sys/config/X86_64_GENERIC
@@ -297,7 +297,6 @@ pseudo-device	carp
 pseudo-device	bpf		#Berkeley packet filter
 
 device		crypto		# core crypto support, used by wlan
-device		cryptodev	# /dev/crypto for access to h/w
 
 # USB support
 #
@@ -328,8 +327,6 @@ device		sdhci
 
 # RNG
 #
-device		aesni		# hardware crypto/RNG for AES-NI
-device		padlock		# hardware crypto/RNG for VIA C3/C7/Eden
 device		rdrand		# hardware RNG for RdRand
 
 device		gpio_acpi
diff --git a/sys/crypto/Makefile b/sys/crypto/Makefile
index 95767ae474..f13d37bfc9 100644
--- a/sys/crypto/Makefile
+++ b/sys/crypto/Makefile
@@ -2,11 +2,8 @@
 
 KMOD=	crypto
 
-.PATH:	${.CURDIR}/../opencrypto
-# excluded: cryptodev.c
-SRCS=	cast.c criov.c crypto.c cryptosoft.c deflate.c gmac.c rmd160.c \
-	skipjack.c xform.c
-SRCS+=	cryptodev_if.c cryptodev_if.h
+.PATH:	${.CURDIR}
+SRCS+=	crypto_mod.c crypto_cipher.c
 
 SRCS+=	sha1.c
 SRCS+=	chachapoly.c
@@ -14,18 +11,13 @@ SRCS+=	chachapoly.c
 .PATH:	${.CURDIR}/blake2
 SRCS+=	blake2s.c
 
-.PATH:	${.CURDIR}/blowfish
-SRCS+=	bf_enc.c bf_skey.c
-
-.PATH:	${.CURDIR}/camellia
-SRCS+=	camellia.c camellia-api.c
-
 .PATH:	${.CURDIR}/chacha20
 SRCS+=	chacha.c
 
 .PATH:	${.CURDIR}/curve25519
 SRCS+=	curve25519.c
 
+# required by: smb_crypt
 .PATH:	${.CURDIR}/des
 SRCS+=	des_ecb.c des_enc.c des_setkey.c
 
@@ -35,8 +27,8 @@ SRCS+=	poly1305.c
 .PATH:	${.CURDIR}/rijndael
 SRCS+=	rijndael-alg-fst.c rijndael-api.c
 
-.PATH:	${.CURDIR}/serpent
-SRCS+=	serpent.c
+.PATH:	${.CURDIR}/rmd160
+SRCS+=	rmd160.c
 
 .PATH:	${.CURDIR}/sha2
 SRCS+=	sha2.c
@@ -44,11 +36,12 @@ SRCS+=	sha2.c
 .PATH:	${.CURDIR}/siphash
 SRCS+=	siphash.c
 
-.PATH:	${.CURDIR}/twofish
-SRCS+=	twofish.c
-
-SRCS+=	bus_if.h device_if.h opt_ddb.h
+.PATH:	${.CURDIR}/aesni
+SRCS+=	aesni.c
+SRCS+=	aesencdec_${MACHINE_ARCH}.S
+SRCS+=	aeskeys_${MACHINE_ARCH}.S
 
+# required by: netgraph
 SUBDIR=	rc4
 
 .include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/aesni/aesencdec_x86_64.S b/sys/crypto/aesni/aesencdec_x86_64.S
similarity index 100%
rename from sys/dev/crypto/aesni/aesencdec_x86_64.S
rename to sys/crypto/aesni/aesencdec_x86_64.S
diff --git a/sys/dev/crypto/aesni/aeskeys_x86_64.S b/sys/crypto/aesni/aeskeys_x86_64.S
similarity index 100%
rename from sys/dev/crypto/aesni/aeskeys_x86_64.S
rename to sys/crypto/aesni/aeskeys_x86_64.S
diff --git a/sys/crypto/aesni/aesni.c b/sys/crypto/aesni/aesni.c
new file mode 100644
index 0000000000..17e3a0147f
--- /dev/null
+++ b/sys/crypto/aesni/aesni.c
@@ -0,0 +1,384 @@
+/*-
+ * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
+ * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
+ * Copyright (c) 2024 Michael Neumann <mneumann@ntecs.de>
+ * 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.
+ *
+ * $FreeBSD: src/sys/crypto/aesni/aesni_wrap.c,v 1.7 2010/11/27 15:41:44
+ * kib Exp $
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/sysctl.h>
+
+#include <crypto/aesni/aesni.h>
+#include <crypto/crypto_cipher.h>
+
+static int aesni_disable = 0;
+// TUNABLE_INT("hw.aesni_disable", &aesni_disable);
+SYSCTL_INT(_hw, OID_AUTO, aesni_disable, CTLFLAG_RW, &aesni_disable, 0,
+    "Disable AESNI");
+
+/*
+ * Internal functions, implemented in assembler.
+ */
+void aesni_enc(int rounds, const uint8_t *key_schedule,
+    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN],
+    const uint8_t iv[AES_BLOCK_LEN]);
+void aesni_dec(int rounds, const uint8_t *key_schedule,
+    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN],
+    const uint8_t iv[AES_BLOCK_LEN]);
+void aesni_set_enckey(const uint8_t *userkey, uint8_t *encrypt_schedule,
+    int number_of_rounds);
+void aesni_set_deckey(const uint8_t *encrypt_schedule,
+    uint8_t *decrypt_schedule, int number_of_rounds);
+void aesni_decrypt_cbc(int rounds, const void *key_schedule, size_t len,
+    const uint8_t *from, const uint8_t iv[AES_BLOCK_LEN]);
+
+static inline void
+aesni_encrypt_cbc(int rounds, const void *key_schedule, size_t len,
+    const uint8_t *from, uint8_t *to, const uint8_t iv[AES_BLOCK_LEN])
+{
+	const uint8_t *ivp;
+	size_t i;
+
+	len /= AES_BLOCK_LEN;
+	ivp = iv;
+	for (i = 0; i < len; i++) {
+		aesni_enc(rounds - 1, key_schedule, from, to, ivp);
+		ivp = to;
+		from += AES_BLOCK_LEN;
+		to += AES_BLOCK_LEN;
+	}
+}
+
+#define AESNI_CTX(ctx) (ctx->_ctx._aesni)
+#define AESNI_IV(iv)   (iv->_iv._aesni.iv)
+
+// TODO: how to improve alignment?
+#define AESNI_ALIGNED_KEY_SCHEDULES(ctx, CONST)                     \
+	((CONST struct aesni_key_schedules                          \
+		*)((((uintptr_t)((CONST uint8_t *)&(                \
+			AESNI_CTX(ctx).key_schedules.schedules))) + \
+		       (AESNI_ALIGN - 1)) &                         \
+	    (~(AESNI_ALIGN - 1))))
+
+#define AESNI_ALIGNED_ENC_SCHEDULE(ctx, CONST) \
+	(AESNI_ALIGNED_KEY_SCHEDULES(ctx, CONST)->enc_schedule)
+
+#define AESNI_ALIGNED_DEC_SCHEDULE(ctx, CONST) \
+	(AESNI_ALIGNED_KEY_SCHEDULES(ctx, CONST)->dec_schedule)
+
+#define AESNI_ALIGNED_XTS_SCHEDULE(ctx, CONST) \
+	(AESNI_ALIGNED_KEY_SCHEDULES(ctx, CONST)->xts_schedule)
+
+#define KKASSERT_AESNI_ALIGNED(ptr) \
+	KKASSERT((((uintptr_t)(const uint8_t *)ptr) % AESNI_ALIGN) == 0)
+
+static int
+cipher_aesni_cbc_probe(const char *algo_name, const char *mode_name,
+    int keysize_in_bits)
+{
+	if (aesni_disable)
+		return (-1);
+
+	if ((cpu_feature2 & CPUID2_AESNI) == 0)
+		return (EINVAL);
+
+	if ((strcmp(algo_name, "aes") == 0) &&
+	    (strcmp(mode_name, "cbc") == 0) &&
+	    (keysize_in_bits == 128 || keysize_in_bits == 192 ||
+		keysize_in_bits == 256))
+		return (0);
+
+	return (-1);
+}
+
+static int
+cipher_aesni_cbc_setkey(struct crypto_cipher_context *ctx,
+    const uint8_t *keydata, int keylen_in_bytes)
+{
+	bzero(ctx, sizeof(*ctx));
+	int rounds;
+
+	switch (keylen_in_bytes * 8) {
+	case 128:
+		rounds = AES128_ROUNDS;
+		break;
+	case 192:
+		rounds = AES192_ROUNDS;
+		break;
+	case 256:
+		rounds = AES256_ROUNDS;
+		break;
+	default:
+		return (EINVAL);
+	}
+
+	uint8_t *enc_schedule = AESNI_ALIGNED_ENC_SCHEDULE(ctx, );
+	uint8_t *dec_schedule = AESNI_ALIGNED_DEC_SCHEDULE(ctx, );
+
+	AESNI_CTX(ctx).rounds = rounds;
+
+	aesni_set_enckey(keydata, enc_schedule, rounds);
+	aesni_set_deckey(enc_schedule, dec_schedule, rounds);
+
+	return (0);
+}
+
+static int
+cipher_aesni_cbc_encrypt(const struct crypto_cipher_context *ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *iv)
+{
+	if ((datalen % AES_BLOCK_LEN) != 0)
+		return (EINVAL);
+
+	const uint8_t *enc_schedule = AESNI_ALIGNED_ENC_SCHEDULE(ctx, const);
+
+	KKASSERT_AESNI_ALIGNED(enc_schedule);
+
+	aesni_encrypt_cbc(AESNI_CTX(ctx).rounds, enc_schedule, datalen, data,
+	    data, AESNI_IV(iv));
+
+	return (0);
+}
+
+static int
+cipher_aesni_cbc_decrypt(const struct crypto_cipher_context *ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *iv)
+{
+	if ((datalen % AES_BLOCK_LEN) != 0)
+		return (EINVAL);
+
+	const uint8_t *dec_schedule = AESNI_ALIGNED_DEC_SCHEDULE(ctx, const);
+
+	KKASSERT_AESNI_ALIGNED(dec_schedule);
+
+	aesni_decrypt_cbc(AESNI_CTX(ctx).rounds, dec_schedule, datalen, data,
+	    AESNI_IV(iv));
+
+	return (0);
+}
+
+const struct crypto_cipher cipher_aesni_cbc = {
+	"aesni-cbc",
+	"AES-CBC w/ CPU AESNI instruction",
+	AES_BLOCK_LEN,
+	AES_BLOCK_LEN,
+	sizeof(aesni_ctx),
+	cipher_aesni_cbc_probe,
+	cipher_aesni_cbc_setkey,
+	cipher_aesni_cbc_encrypt,
+	cipher_aesni_cbc_decrypt,
+};
+
+/**
+ * AES-XTS
+ */
+
+#define AES_XTS_BLOCKSIZE 16
+#define AES_XTS_IVSIZE	  8
+#define AES_XTS_ALPHA	  0x87 /* GF(2^128) generator polynomial */
+
+static void
+aesni_crypt_xts_block(int rounds, const void *key_schedule, uint8_t *tweak,
+    const uint8_t *from, uint8_t *to, int do_encrypt)
+{
+	uint8_t block[AES_XTS_BLOCKSIZE];
+	u_int i, carry_in, carry_out;
+
+	for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
+		block[i] = from[i] ^ tweak[i];
+
+	if (do_encrypt)
+		aesni_enc(rounds - 1, key_schedule, block, to, NULL);
+	else
+		aesni_dec(rounds - 1, key_schedule, block, to, NULL);
+
+	for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
+		to[i] ^= tweak[i];
+
+	/* Exponentiate tweak. */
+	carry_in = 0;
+	for (i = 0; i < AES_XTS_BLOCKSIZE; i++) {
+		carry_out = tweak[i] & 0x80;
+		tweak[i] = (tweak[i] << 1) | (carry_in ? 1 : 0);
+		carry_in = carry_out;
+	}
+	if (carry_in)
+		tweak[0] ^= AES_XTS_ALPHA;
+	bzero(block, sizeof(block));
+}
+
+static void
+aesni_crypt_xts(int rounds, const void *data_schedule,
+    const void *tweak_schedule, size_t len, const uint8_t *from, uint8_t *to,
+    const uint8_t iv[AES_BLOCK_LEN], int do_encrypt)
+{
+	uint8_t tweak[AES_XTS_BLOCKSIZE];
+	uint64_t blocknum;
+	size_t i;
+
+	/*
+	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
+	 * of a 64-bit block number which we allow to be passed in directly.
+	 */
+	bcopy(iv, &blocknum, AES_XTS_IVSIZE);
+	for (i = 0; i < AES_XTS_IVSIZE; i++) {
+		tweak[i] = blocknum & 0xff;
+		blocknum >>= 8;
+	}
+	/* Last 64 bits of IV are always zero. */
+	bzero(tweak + AES_XTS_IVSIZE, AES_XTS_IVSIZE);
+	aesni_enc(rounds - 1, tweak_schedule, tweak, tweak, NULL);
+
+	len /= AES_XTS_BLOCKSIZE;
+	for (i = 0; i < len; i++) {
+		aesni_crypt_xts_block(rounds, data_schedule, tweak, from, to,
+		    do_encrypt);
+		from += AES_XTS_BLOCKSIZE;
+		to += AES_XTS_BLOCKSIZE;
+	}
+
+	bzero(tweak, sizeof(tweak));
+}
+
+static inline void
+aesni_encrypt_xts(int rounds, const void *data_schedule,
+    const void *tweak_schedule, size_t len, const uint8_t *from, uint8_t *to,
+    const uint8_t iv[AES_BLOCK_LEN])
+{
+	aesni_crypt_xts(rounds, data_schedule, tweak_schedule, len, from, to,
+	    iv, 1);
+}
+
+static inline void
+aesni_decrypt_xts(int rounds, const void *data_schedule,
+    const void *tweak_schedule, size_t len, const uint8_t *from, uint8_t *to,
+    const uint8_t iv[AES_BLOCK_LEN])
+{
+	aesni_crypt_xts(rounds, data_schedule, tweak_schedule, len, from, to,
+	    iv, 0);
+}
+
+static int
+cipher_aesni_xts_probe(const char *algo_name, const char *mode_name,
+    int keysize_in_bits)
+{
+	if (aesni_disable)
+		return (-1);
+
+	if ((cpu_feature2 & CPUID2_AESNI) == 0)
+		return (EINVAL);
+
+	if ((strcmp(algo_name, "aes") == 0) &&
+	    (strcmp(mode_name, "xts") == 0) &&
+	    (keysize_in_bits == 256 || keysize_in_bits == 512))
+		return (0);
+
+	return (-1);
+}
+
+static int
+cipher_aesni_xts_setkey(struct crypto_cipher_context *ctx,
+    const uint8_t *keydata, int keylen_in_bytes)
+{
+	bzero(ctx, sizeof(*ctx));
+	int rounds;
+
+	switch (keylen_in_bytes * 8) {
+	case 256:
+		rounds = AES128_ROUNDS;
+		break;
+	case 512:
+		rounds = AES256_ROUNDS;
+		break;
+	default:
+		return (EINVAL);
+	}
+
+	uint8_t *enc_schedule = AESNI_ALIGNED_ENC_SCHEDULE(ctx, );
+	uint8_t *dec_schedule = AESNI_ALIGNED_DEC_SCHEDULE(ctx, );
+	uint8_t *xts_schedule = AESNI_ALIGNED_XTS_SCHEDULE(ctx, );
+
+	AESNI_CTX(ctx).rounds = rounds;
+
+	aesni_set_enckey(keydata, enc_schedule, rounds);
+	aesni_set_deckey(enc_schedule, dec_schedule, rounds);
+	aesni_set_enckey(keydata + (keylen_in_bytes / 2), xts_schedule, rounds);
+
+	return (0);
+}
+
+static int
+cipher_aesni_xts_encrypt(const struct crypto_cipher_context *ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *iv)
+{
+	if ((datalen % AES_BLOCK_LEN) != 0)
+		return (EINVAL);
+
+	const uint8_t *enc_schedule = AESNI_ALIGNED_ENC_SCHEDULE(ctx, const);
+	const uint8_t *xts_schedule = AESNI_ALIGNED_XTS_SCHEDULE(ctx, const);
+
+	KKASSERT_AESNI_ALIGNED(enc_schedule);
+	KKASSERT_AESNI_ALIGNED(xts_schedule);
+
+	aesni_encrypt_xts(AESNI_CTX(ctx).rounds, enc_schedule, xts_schedule,
+	    datalen, data, data, AESNI_IV(iv));
+
+	return (0);
+}
+
+static int
+cipher_aesni_xts_decrypt(const struct crypto_cipher_context *ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *iv)
+{
+	if ((datalen % AES_BLOCK_LEN) != 0)
+		return (EINVAL);
+
+	const uint8_t *dec_schedule = AESNI_ALIGNED_DEC_SCHEDULE(ctx, const);
+	const uint8_t *xts_schedule = AESNI_ALIGNED_XTS_SCHEDULE(ctx, const);
+
+	KKASSERT_AESNI_ALIGNED(dec_schedule);
+	KKASSERT_AESNI_ALIGNED(xts_schedule);
+
+	aesni_decrypt_xts(AESNI_CTX(ctx).rounds, dec_schedule, xts_schedule,
+	    datalen, data, data, AESNI_IV(iv));
+
+	return (0);
+}
+
+const struct crypto_cipher cipher_aesni_xts = {
+	"aesni-xts",
+	"AES-XTS w/ CPU AESNI instruction",
+	AES_BLOCK_LEN,
+	AES_BLOCK_LEN,
+	sizeof(aesni_ctx),
+	cipher_aesni_xts_probe,
+	cipher_aesni_xts_setkey,
+	cipher_aesni_xts_encrypt,
+	cipher_aesni_xts_decrypt,
+};
diff --git a/sys/crypto/aesni/aesni.h b/sys/crypto/aesni/aesni.h
new file mode 100644
index 0000000000..d915e05700
--- /dev/null
+++ b/sys/crypto/aesni/aesni.h
@@ -0,0 +1,71 @@
+/*-
+ * 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.
+ *
+ * $FreeBSD: src/sys/crypto/aesni/aesni.h,v 1.2 2010/09/23 11:57:25 pjd Exp $
+ */
+
+#ifndef _AESNI_H_
+#define _AESNI_H_
+
+#if defined(__x86_64__)
+#include <machine/cpufunc.h>
+#include <machine/cputypes.h>
+#include <machine/md_var.h>
+#include <machine/specialreg.h>
+#endif
+
+#define AES_BLOCK_LEN 16
+
+#define AESNI_ALIGN   16
+
+#define AES128_ROUNDS 10
+#define AES192_ROUNDS 12
+#define AES256_ROUNDS 14
+#define AES_SCHED_LEN ((AES256_ROUNDS + 1) * AES_BLOCK_LEN)
+
+typedef struct {
+	uint8_t iv[AES_BLOCK_LEN];
+} aesni_iv;
+
+struct aesni_key_schedules {
+	uint8_t enc_schedule[AES_SCHED_LEN] __aligned(16);
+	uint8_t dec_schedule[AES_SCHED_LEN] __aligned(16);
+	uint8_t xts_schedule[AES_SCHED_LEN] __aligned(16);
+};
+
+struct aesni_key_schedules_padded {
+	struct aesni_key_schedules schedules;
+	uint8_t _padding[AESNI_ALIGN];
+};
+
+typedef struct {
+	struct aesni_key_schedules_padded key_schedules;
+	int rounds;
+} aesni_ctx;
+
+extern const struct crypto_cipher cipher_aesni_cbc;
+extern const struct crypto_cipher cipher_aesni_xts;
+
+#endif
diff --git a/sys/crypto/blowfish/bf_enc.c b/sys/crypto/blowfish/bf_enc.c
deleted file mode 100644
index 33d0460059..0000000000
--- a/sys/crypto/blowfish/bf_enc.c
+++ /dev/null
@@ -1,155 +0,0 @@
-/*	$FreeBSD: src/sys/crypto/blowfish/bf_enc.c,v 1.6 2003/06/10 21:38:38 obrien Exp $	*/
-/*	$KAME: bf_enc.c,v 1.7 2002/02/27 01:33:59 itojun Exp $	*/
-
-/* crypto/bf/bf_enc.c */
-
-/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
- * All rights reserved.
- *
- * This package is an SSL implementation written
- * by Eric Young (eay@cryptsoft.com).
- * The implementation was written so as to conform with Netscapes SSL.
- *
- * This library is free for commercial and non-commercial use as long as
- * the following conditions are aheared to.  The following conditions
- * apply to all code found in this distribution, be it the RC4, RSA,
- * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
- * included with this distribution is covered by the same copyright terms
- * except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
- * Copyright remains Eric Young's, and as such any Copyright notices in
- * the code are not to be removed.
- * If this package is used in a product, Eric Young should be given attribution
- * as the author of the parts of the library used.
- * This can be in the form of a textual message at program startup or
- * in documentation (online or textual) provided with the package.
- *
- * 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 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *    "This product includes cryptographic software written by
- *     Eric Young (eay@cryptsoft.com)"
- *    The word 'cryptographic' can be left out if the rouines from the library
- *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
- *    the apps directory (application code) you must include an acknowledgement:
- *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``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 AUTHOR 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.
- *
- * The licence and distribution terms for any publically available version or
- * derivative of this code cannot be changed.  i.e. this code cannot simply be
- * copied and put under another distribution licence
- * [including the GNU Public Licence.]
- */
-
-#include <sys/types.h>
-#include <crypto/blowfish/blowfish.h>
-#include <crypto/blowfish/bf_locl.h>
-
-/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
- * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
- * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
- */
-
-#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
-If you set BF_ROUNDS to some value other than 16 or 20, you will have
-to modify the code.
-#endif
-
-/* XXX "data" is host endian */
-void
-BF_encrypt(BF_LONG *data, BF_KEY *key)
-{
-	BF_LONG l, r, *p, *s;
-
-	p = key->P;
-	s= &key->S[0];
-	l = data[0];
-	r = data[1];
-
-	l^=p[0];
-	BF_ENC(r, l, s, p[ 1]);
-	BF_ENC(l, r, s, p[ 2]);
-	BF_ENC(r, l, s, p[ 3]);
-	BF_ENC(l, r, s, p[ 4]);
-	BF_ENC(r, l, s, p[ 5]);
-	BF_ENC(l, r, s, p[ 6]);
-	BF_ENC(r, l, s, p[ 7]);
-	BF_ENC(l, r, s, p[ 8]);
-	BF_ENC(r, l, s, p[ 9]);
-	BF_ENC(l, r, s, p[10]);
-	BF_ENC(r, l, s, p[11]);
-	BF_ENC(l, r, s, p[12]);
-	BF_ENC(r, l, s, p[13]);
-	BF_ENC(l, r, s, p[14]);
-	BF_ENC(r, l, s, p[15]);
-	BF_ENC(l, r, s, p[16]);
-#if BF_ROUNDS == 20
-	BF_ENC(r, l, s, p[17]);
-	BF_ENC(l, r, s, p[18]);
-	BF_ENC(r, l, s, p[19]);
-	BF_ENC(l, r, s, p[20]);
-#endif
-	r ^= p[BF_ROUNDS + 1];
-
-	data[1] = l & 0xffffffff;
-	data[0] = r & 0xffffffff;
-}
-
-/* XXX "data" is host endian */
-void
-BF_decrypt(BF_LONG *data, BF_KEY *key)
-{
-	BF_LONG l, r, *p, *s;
-
-	p = key->P;
-	s= &key->S[0];
-	l = data[0];
-	r = data[1];
-
-	l ^= p[BF_ROUNDS + 1];
-#if BF_ROUNDS == 20
-	BF_ENC(r, l, s, p[20]);
-	BF_ENC(l, r, s, p[19]);
-	BF_ENC(r, l, s, p[18]);
-	BF_ENC(l, r, s, p[17]);
-#endif
-	BF_ENC(r, l, s, p[16]);
-	BF_ENC(l, r, s, p[15]);
-	BF_ENC(r, l, s, p[14]);
-	BF_ENC(l, r, s, p[13]);
-	BF_ENC(r, l, s, p[12]);
-	BF_ENC(l, r, s, p[11]);
-	BF_ENC(r, l, s, p[10]);
-	BF_ENC(l, r, s, p[ 9]);
-	BF_ENC(r, l, s, p[ 8]);
-	BF_ENC(l, r, s, p[ 7]);
-	BF_ENC(r, l, s, p[ 6]);
-	BF_ENC(l, r, s, p[ 5]);
-	BF_ENC(r, l, s, p[ 4]);
-	BF_ENC(l, r, s, p[ 3]);
-	BF_ENC(r, l, s, p[ 2]);
-	BF_ENC(l, r, s, p[ 1]);
-	r ^= p[0];
-
-	data[1] = l & 0xffffffff;
-	data[0] = r & 0xffffffff;
-}
diff --git a/sys/crypto/blowfish/bf_locl.h b/sys/crypto/blowfish/bf_locl.h
deleted file mode 100644
index e2c740373b..0000000000
--- a/sys/crypto/blowfish/bf_locl.h
+++ /dev/null
@@ -1,173 +0,0 @@
-/*	$FreeBSD: src/sys/crypto/blowfish/bf_locl.h,v 1.5 2002/03/05 09:19:01 ume Exp $	*/
-/*	$KAME: bf_locl.h,v 1.6 2001/09/10 04:03:56 itojun Exp $	*/
-
-/* crypto/bf/bf_local.h */
-/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
- * All rights reserved.
- *
- * This package is an SSL implementation written
- * by Eric Young (eay@mincom.oz.au).
- * The implementation was written so as to conform with Netscapes SSL.
- *
- * This library is free for commercial and non-commercial use as long as
- * the following conditions are aheared to.  The following conditions
- * apply to all code found in this distribution, be it the RC4, RSA,
- * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
- * included with this distribution is covered by the same copyright terms
- * except that the holder is Tim Hudson (tjh@mincom.oz.au).
- *
- * Copyright remains Eric Young's, and as such any Copyright notices in
- * the code are not to be removed.
- * If this package is used in a product, Eric Young should be given attribution
- * as the author of the parts of the library used.
- * This can be in the form of a textual message at program startup or
- * in documentation (online or textual) provided with the package.
- *
- * 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 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *    "This product includes cryptographic software written by
- *     Eric Young (eay@mincom.oz.au)"
- *    The word 'cryptographic' can be left out if the rouines from the library
- *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
- *    the apps directory (application code) you must include an acknowledgement:
- *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
- *
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``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 AUTHOR 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.
- *
- * The licence and distribution terms for any publically available version or
- * derivative of this code cannot be changed.  i.e. this code cannot simply be
- * copied and put under another distribution licence
- * [including the GNU Public Licence.]
- */
-/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
- *
- * Always modify bf_locl.org since bf_locl.h is automatically generated from
- * it during SSLeay configuration.
- *
- * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
- */
-
-#undef c2l
-#define c2l(c,l)	(l =((BF_LONG)(*((c)++)))    , \
-			 l|=((BF_LONG)(*((c)++)))<< 8L, \
-			 l|=((BF_LONG)(*((c)++)))<<16L, \
-			 l|=((BF_LONG)(*((c)++)))<<24L)
-
-/* NOTE - c is not incremented as per c2l */
-#undef c2ln
-#define c2ln(c,l1,l2,n)	{ \
-			c+=n; \
-			l1=l2=0; \
-			switch (n) { \
-			case 8: l2 =((BF_LONG)(*(--(c))))<<24L; \
-			case 7: l2|=((BF_LONG)(*(--(c))))<<16L; \
-			case 6: l2|=((BF_LONG)(*(--(c))))<< 8L; \
-			case 5: l2|=((BF_LONG)(*(--(c))));     \
-			case 4: l1 =((BF_LONG)(*(--(c))))<<24L; \
-			case 3: l1|=((BF_LONG)(*(--(c))))<<16L; \
-			case 2: l1|=((BF_LONG)(*(--(c))))<< 8L; \
-			case 1: l1|=((BF_LONG)(*(--(c))));     \
-				} \
-			}
-
-#undef l2c
-#define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
-			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
-			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
-			 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
-
-/* NOTE - c is not incremented as per l2c */
-#undef l2cn
-#define l2cn(l1,l2,c,n)	{ \
-			c+=n; \
-			switch (n) { \
-			case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
-			case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
-			case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
-			case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
-			case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
-			case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
-			case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
-			case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
-				} \
-			}
-
-/* NOTE - c is not incremented as per n2l */
-#define n2ln(c,l1,l2,n)	{ \
-			c+=n; \
-			l1=l2=0; \
-			switch (n) { \
-			case 8: l2 =((BF_LONG)(*(--(c))))    ; \
-			case 7: l2|=((BF_LONG)(*(--(c))))<< 8; \
-			case 6: l2|=((BF_LONG)(*(--(c))))<<16; \
-			case 5: l2|=((BF_LONG)(*(--(c))))<<24; \
-			case 4: l1 =((BF_LONG)(*(--(c))))    ; \
-			case 3: l1|=((BF_LONG)(*(--(c))))<< 8; \
-			case 2: l1|=((BF_LONG)(*(--(c))))<<16; \
-			case 1: l1|=((BF_LONG)(*(--(c))))<<24; \
-				} \
-			}
-
-/* NOTE - c is not incremented as per l2n */
-#define l2nn(l1,l2,c,n)	{ \
-			c+=n; \
-			switch (n) { \
-			case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
-			case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
-			case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
-			case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
-			case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
-			case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
-			case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
-			case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
-				} \
-			}
-
-#undef n2l
-#define n2l(c,l)        (l =((BF_LONG)(*((c)++)))<<24L, \
-                         l|=((BF_LONG)(*((c)++)))<<16L, \
-                         l|=((BF_LONG)(*((c)++)))<< 8L, \
-                         l|=((BF_LONG)(*((c)++))))
-
-#undef l2n
-#define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
-                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
-                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
-                         *((c)++)=(unsigned char)(((l)     )&0xff))
-
-/* This is actually a big endian algorithm, the most significate byte
- * is used to lookup array 0 */
-
-#define BF_M	0x3fc
-#define BF_0	22L
-#define BF_1	14L
-#define BF_2	 6L
-#define BF_3	 2L /* left shift */
-
-/* This will always work, even on 64 bit machines */
-
-#define BF_ENC(LL,R,S,P) \
-	LL^=P; \
-	LL^=(((	S[        (R>>24L)      ] + \
-		S[0x0100+((R>>16L)&0xff)])^ \
-		S[0x0200+((R>> 8L)&0xff)])+ \
-		S[0x0300+((R     )&0xff)])&0xffffffff;
diff --git a/sys/crypto/blowfish/bf_pi.h b/sys/crypto/blowfish/bf_pi.h
deleted file mode 100644
index a2be6034d2..0000000000
--- a/sys/crypto/blowfish/bf_pi.h
+++ /dev/null
@@ -1,328 +0,0 @@
-/*	$FreeBSD: src/sys/crypto/blowfish/bf_pi.h,v 1.4 2002/03/05 09:19:01 ume Exp $	*/
-/*	$KAME: bf_pi.h,v 1.4 2001/09/10 04:03:56 itojun Exp $	*/
-
-/* crypto/bf/bf_pi.h */
-/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
- * All rights reserved.
- *
- * This package is an SSL implementation written
- * by Eric Young (eay@mincom.oz.au).
- * The implementation was written so as to conform with Netscapes SSL.
- *
- * This library is free for commercial and non-commercial use as long as
- * the following conditions are aheared to.  The following conditions
- * apply to all code found in this distribution, be it the RC4, RSA,
- * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
- * included with this distribution is covered by the same copyright terms
- * except that the holder is Tim Hudson (tjh@mincom.oz.au).
- *
- * Copyright remains Eric Young's, and as such any Copyright notices in
- * the code are not to be removed.
- * If this package is used in a product, Eric Young should be given attribution
- * as the author of the parts of the library used.
- * This can be in the form of a textual message at program startup or
- * in documentation (online or textual) provided with the package.
- *
- * 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 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *    "This product includes cryptographic software written by
- *     Eric Young (eay@mincom.oz.au)"
- *    The word 'cryptographic' can be left out if the rouines from the library
- *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
- *    the apps directory (application code) you must include an acknowledgement:
- *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
- *
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``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 AUTHOR 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.
- *
- * The licence and distribution terms for any publically available version or
- * derivative of this code cannot be changed.  i.e. this code cannot simply be
- * copied and put under another distribution licence
- * [including the GNU Public Licence.]
- */
-
-static const BF_KEY bf_init= {
-	{
-	0x243f6a88L, 0x85a308d3L, 0x13198a2eL, 0x03707344L,
-	0xa4093822L, 0x299f31d0L, 0x082efa98L, 0xec4e6c89L,
-	0x452821e6L, 0x38d01377L, 0xbe5466cfL, 0x34e90c6cL,
-	0xc0ac29b7L, 0xc97c50ddL, 0x3f84d5b5L, 0xb5470917L,
-	0x9216d5d9L, 0x8979fb1b
-	},{
-	0xd1310ba6L, 0x98dfb5acL, 0x2ffd72dbL, 0xd01adfb7L,
-	0xb8e1afedL, 0x6a267e96L, 0xba7c9045L, 0xf12c7f99L,
-	0x24a19947L, 0xb3916cf7L, 0x0801f2e2L, 0x858efc16L,
-	0x636920d8L, 0x71574e69L, 0xa458fea3L, 0xf4933d7eL,
-	0x0d95748fL, 0x728eb658L, 0x718bcd58L, 0x82154aeeL,
-	0x7b54a41dL, 0xc25a59b5L, 0x9c30d539L, 0x2af26013L,
-	0xc5d1b023L, 0x286085f0L, 0xca417918L, 0xb8db38efL,
-	0x8e79dcb0L, 0x603a180eL, 0x6c9e0e8bL, 0xb01e8a3eL,
-	0xd71577c1L, 0xbd314b27L, 0x78af2fdaL, 0x55605c60L,
-	0xe65525f3L, 0xaa55ab94L, 0x57489862L, 0x63e81440L,
-	0x55ca396aL, 0x2aab10b6L, 0xb4cc5c34L, 0x1141e8ceL,
-	0xa15486afL, 0x7c72e993L, 0xb3ee1411L, 0x636fbc2aL,
-	0x2ba9c55dL, 0x741831f6L, 0xce5c3e16L, 0x9b87931eL,
-	0xafd6ba33L, 0x6c24cf5cL, 0x7a325381L, 0x28958677L,
-	0x3b8f4898L, 0x6b4bb9afL, 0xc4bfe81bL, 0x66282193L,
-	0x61d809ccL, 0xfb21a991L, 0x487cac60L, 0x5dec8032L,
-	0xef845d5dL, 0xe98575b1L, 0xdc262302L, 0xeb651b88L,
-	0x23893e81L, 0xd396acc5L, 0x0f6d6ff3L, 0x83f44239L,
-	0x2e0b4482L, 0xa4842004L, 0x69c8f04aL, 0x9e1f9b5eL,
-	0x21c66842L, 0xf6e96c9aL, 0x670c9c61L, 0xabd388f0L,
-	0x6a51a0d2L, 0xd8542f68L, 0x960fa728L, 0xab5133a3L,
-	0x6eef0b6cL, 0x137a3be4L, 0xba3bf050L, 0x7efb2a98L,
-	0xa1f1651dL, 0x39af0176L, 0x66ca593eL, 0x82430e88L,
-	0x8cee8619L, 0x456f9fb4L, 0x7d84a5c3L, 0x3b8b5ebeL,
-	0xe06f75d8L, 0x85c12073L, 0x401a449fL, 0x56c16aa6L,
-	0x4ed3aa62L, 0x363f7706L, 0x1bfedf72L, 0x429b023dL,
-	0x37d0d724L, 0xd00a1248L, 0xdb0fead3L, 0x49f1c09bL,
-	0x075372c9L, 0x80991b7bL, 0x25d479d8L, 0xf6e8def7L,
-	0xe3fe501aL, 0xb6794c3bL, 0x976ce0bdL, 0x04c006baL,
-	0xc1a94fb6L, 0x409f60c4L, 0x5e5c9ec2L, 0x196a2463L,
-	0x68fb6fafL, 0x3e6c53b5L, 0x1339b2ebL, 0x3b52ec6fL,
-	0x6dfc511fL, 0x9b30952cL, 0xcc814544L, 0xaf5ebd09L,
-	0xbee3d004L, 0xde334afdL, 0x660f2807L, 0x192e4bb3L,
-	0xc0cba857L, 0x45c8740fL, 0xd20b5f39L, 0xb9d3fbdbL,
-	0x5579c0bdL, 0x1a60320aL, 0xd6a100c6L, 0x402c7279L,
-	0x679f25feL, 0xfb1fa3ccL, 0x8ea5e9f8L, 0xdb3222f8L,
-	0x3c7516dfL, 0xfd616b15L, 0x2f501ec8L, 0xad0552abL,
-	0x323db5faL, 0xfd238760L, 0x53317b48L, 0x3e00df82L,
-	0x9e5c57bbL, 0xca6f8ca0L, 0x1a87562eL, 0xdf1769dbL,
-	0xd542a8f6L, 0x287effc3L, 0xac6732c6L, 0x8c4f5573L,
-	0x695b27b0L, 0xbbca58c8L, 0xe1ffa35dL, 0xb8f011a0L,
-	0x10fa3d98L, 0xfd2183b8L, 0x4afcb56cL, 0x2dd1d35bL,
-	0x9a53e479L, 0xb6f84565L, 0xd28e49bcL, 0x4bfb9790L,
-	0xe1ddf2daL, 0xa4cb7e33L, 0x62fb1341L, 0xcee4c6e8L,
-	0xef20cadaL, 0x36774c01L, 0xd07e9efeL, 0x2bf11fb4L,
-	0x95dbda4dL, 0xae909198L, 0xeaad8e71L, 0x6b93d5a0L,
-	0xd08ed1d0L, 0xafc725e0L, 0x8e3c5b2fL, 0x8e7594b7L,
-	0x8ff6e2fbL, 0xf2122b64L, 0x8888b812L, 0x900df01cL,
-	0x4fad5ea0L, 0x688fc31cL, 0xd1cff191L, 0xb3a8c1adL,
-	0x2f2f2218L, 0xbe0e1777L, 0xea752dfeL, 0x8b021fa1L,
-	0xe5a0cc0fL, 0xb56f74e8L, 0x18acf3d6L, 0xce89e299L,
-	0xb4a84fe0L, 0xfd13e0b7L, 0x7cc43b81L, 0xd2ada8d9L,
-	0x165fa266L, 0x80957705L, 0x93cc7314L, 0x211a1477L,
-	0xe6ad2065L, 0x77b5fa86L, 0xc75442f5L, 0xfb9d35cfL,
-	0xebcdaf0cL, 0x7b3e89a0L, 0xd6411bd3L, 0xae1e7e49L,
-	0x00250e2dL, 0x2071b35eL, 0x226800bbL, 0x57b8e0afL,
-	0x2464369bL, 0xf009b91eL, 0x5563911dL, 0x59dfa6aaL,
-	0x78c14389L, 0xd95a537fL, 0x207d5ba2L, 0x02e5b9c5L,
-	0x83260376L, 0x6295cfa9L, 0x11c81968L, 0x4e734a41L,
-	0xb3472dcaL, 0x7b14a94aL, 0x1b510052L, 0x9a532915L,
-	0xd60f573fL, 0xbc9bc6e4L, 0x2b60a476L, 0x81e67400L,
-	0x08ba6fb5L, 0x571be91fL, 0xf296ec6bL, 0x2a0dd915L,
-	0xb6636521L, 0xe7b9f9b6L, 0xff34052eL, 0xc5855664L,
-	0x53b02d5dL, 0xa99f8fa1L, 0x08ba4799L, 0x6e85076aL,
-	0x4b7a70e9L, 0xb5b32944L, 0xdb75092eL, 0xc4192623L,
-	0xad6ea6b0L, 0x49a7df7dL, 0x9cee60b8L, 0x8fedb266L,
-	0xecaa8c71L, 0x699a17ffL, 0x5664526cL, 0xc2b19ee1L,
-	0x193602a5L, 0x75094c29L, 0xa0591340L, 0xe4183a3eL,
-	0x3f54989aL, 0x5b429d65L, 0x6b8fe4d6L, 0x99f73fd6L,
-	0xa1d29c07L, 0xefe830f5L, 0x4d2d38e6L, 0xf0255dc1L,
-	0x4cdd2086L, 0x8470eb26L, 0x6382e9c6L, 0x021ecc5eL,
-	0x09686b3fL, 0x3ebaefc9L, 0x3c971814L, 0x6b6a70a1L,
-	0x687f3584L, 0x52a0e286L, 0xb79c5305L, 0xaa500737L,
-	0x3e07841cL, 0x7fdeae5cL, 0x8e7d44ecL, 0x5716f2b8L,
-	0xb03ada37L, 0xf0500c0dL, 0xf01c1f04L, 0x0200b3ffL,
-	0xae0cf51aL, 0x3cb574b2L, 0x25837a58L, 0xdc0921bdL,
-	0xd19113f9L, 0x7ca92ff6L, 0x94324773L, 0x22f54701L,
-	0x3ae5e581L, 0x37c2dadcL, 0xc8b57634L, 0x9af3dda7L,
-	0xa9446146L, 0x0fd0030eL, 0xecc8c73eL, 0xa4751e41L,
-	0xe238cd99L, 0x3bea0e2fL, 0x3280bba1L, 0x183eb331L,
-	0x4e548b38L, 0x4f6db908L, 0x6f420d03L, 0xf60a04bfL,
-	0x2cb81290L, 0x24977c79L, 0x5679b072L, 0xbcaf89afL,
-	0xde9a771fL, 0xd9930810L, 0xb38bae12L, 0xdccf3f2eL,
-	0x5512721fL, 0x2e6b7124L, 0x501adde6L, 0x9f84cd87L,
-	0x7a584718L, 0x7408da17L, 0xbc9f9abcL, 0xe94b7d8cL,
-	0xec7aec3aL, 0xdb851dfaL, 0x63094366L, 0xc464c3d2L,
-	0xef1c1847L, 0x3215d908L, 0xdd433b37L, 0x24c2ba16L,
-	0x12a14d43L, 0x2a65c451L, 0x50940002L, 0x133ae4ddL,
-	0x71dff89eL, 0x10314e55L, 0x81ac77d6L, 0x5f11199bL,
-	0x043556f1L, 0xd7a3c76bL, 0x3c11183bL, 0x5924a509L,
-	0xf28fe6edL, 0x97f1fbfaL, 0x9ebabf2cL, 0x1e153c6eL,
-	0x86e34570L, 0xeae96fb1L, 0x860e5e0aL, 0x5a3e2ab3L,
-	0x771fe71cL, 0x4e3d06faL, 0x2965dcb9L, 0x99e71d0fL,
-	0x803e89d6L, 0x5266c825L, 0x2e4cc978L, 0x9c10b36aL,
-	0xc6150ebaL, 0x94e2ea78L, 0xa5fc3c53L, 0x1e0a2df4L,
-	0xf2f74ea7L, 0x361d2b3dL, 0x1939260fL, 0x19c27960L,
-	0x5223a708L, 0xf71312b6L, 0xebadfe6eL, 0xeac31f66L,
-	0xe3bc4595L, 0xa67bc883L, 0xb17f37d1L, 0x018cff28L,
-	0xc332ddefL, 0xbe6c5aa5L, 0x65582185L, 0x68ab9802L,
-	0xeecea50fL, 0xdb2f953bL, 0x2aef7dadL, 0x5b6e2f84L,
-	0x1521b628L, 0x29076170L, 0xecdd4775L, 0x619f1510L,
-	0x13cca830L, 0xeb61bd96L, 0x0334fe1eL, 0xaa0363cfL,
-	0xb5735c90L, 0x4c70a239L, 0xd59e9e0bL, 0xcbaade14L,
-	0xeecc86bcL, 0x60622ca7L, 0x9cab5cabL, 0xb2f3846eL,
-	0x648b1eafL, 0x19bdf0caL, 0xa02369b9L, 0x655abb50L,
-	0x40685a32L, 0x3c2ab4b3L, 0x319ee9d5L, 0xc021b8f7L,
-	0x9b540b19L, 0x875fa099L, 0x95f7997eL, 0x623d7da8L,
-	0xf837889aL, 0x97e32d77L, 0x11ed935fL, 0x16681281L,
-	0x0e358829L, 0xc7e61fd6L, 0x96dedfa1L, 0x7858ba99L,
-	0x57f584a5L, 0x1b227263L, 0x9b83c3ffL, 0x1ac24696L,
-	0xcdb30aebL, 0x532e3054L, 0x8fd948e4L, 0x6dbc3128L,
-	0x58ebf2efL, 0x34c6ffeaL, 0xfe28ed61L, 0xee7c3c73L,
-	0x5d4a14d9L, 0xe864b7e3L, 0x42105d14L, 0x203e13e0L,
-	0x45eee2b6L, 0xa3aaabeaL, 0xdb6c4f15L, 0xfacb4fd0L,
-	0xc742f442L, 0xef6abbb5L, 0x654f3b1dL, 0x41cd2105L,
-	0xd81e799eL, 0x86854dc7L, 0xe44b476aL, 0x3d816250L,
-	0xcf62a1f2L, 0x5b8d2646L, 0xfc8883a0L, 0xc1c7b6a3L,
-	0x7f1524c3L, 0x69cb7492L, 0x47848a0bL, 0x5692b285L,
-	0x095bbf00L, 0xad19489dL, 0x1462b174L, 0x23820e00L,
-	0x58428d2aL, 0x0c55f5eaL, 0x1dadf43eL, 0x233f7061L,
-	0x3372f092L, 0x8d937e41L, 0xd65fecf1L, 0x6c223bdbL,
-	0x7cde3759L, 0xcbee7460L, 0x4085f2a7L, 0xce77326eL,
-	0xa6078084L, 0x19f8509eL, 0xe8efd855L, 0x61d99735L,
-	0xa969a7aaL, 0xc50c06c2L, 0x5a04abfcL, 0x800bcadcL,
-	0x9e447a2eL, 0xc3453484L, 0xfdd56705L, 0x0e1e9ec9L,
-	0xdb73dbd3L, 0x105588cdL, 0x675fda79L, 0xe3674340L,
-	0xc5c43465L, 0x713e38d8L, 0x3d28f89eL, 0xf16dff20L,
-	0x153e21e7L, 0x8fb03d4aL, 0xe6e39f2bL, 0xdb83adf7L,
-	0xe93d5a68L, 0x948140f7L, 0xf64c261cL, 0x94692934L,
-	0x411520f7L, 0x7602d4f7L, 0xbcf46b2eL, 0xd4a20068L,
-	0xd4082471L, 0x3320f46aL, 0x43b7d4b7L, 0x500061afL,
-	0x1e39f62eL, 0x97244546L, 0x14214f74L, 0xbf8b8840L,
-	0x4d95fc1dL, 0x96b591afL, 0x70f4ddd3L, 0x66a02f45L,
-	0xbfbc09ecL, 0x03bd9785L, 0x7fac6dd0L, 0x31cb8504L,
-	0x96eb27b3L, 0x55fd3941L, 0xda2547e6L, 0xabca0a9aL,
-	0x28507825L, 0x530429f4L, 0x0a2c86daL, 0xe9b66dfbL,
-	0x68dc1462L, 0xd7486900L, 0x680ec0a4L, 0x27a18deeL,
-	0x4f3ffea2L, 0xe887ad8cL, 0xb58ce006L, 0x7af4d6b6L,
-	0xaace1e7cL, 0xd3375fecL, 0xce78a399L, 0x406b2a42L,
-	0x20fe9e35L, 0xd9f385b9L, 0xee39d7abL, 0x3b124e8bL,
-	0x1dc9faf7L, 0x4b6d1856L, 0x26a36631L, 0xeae397b2L,
-	0x3a6efa74L, 0xdd5b4332L, 0x6841e7f7L, 0xca7820fbL,
-	0xfb0af54eL, 0xd8feb397L, 0x454056acL, 0xba489527L,
-	0x55533a3aL, 0x20838d87L, 0xfe6ba9b7L, 0xd096954bL,
-	0x55a867bcL, 0xa1159a58L, 0xcca92963L, 0x99e1db33L,
-	0xa62a4a56L, 0x3f3125f9L, 0x5ef47e1cL, 0x9029317cL,
-	0xfdf8e802L, 0x04272f70L, 0x80bb155cL, 0x05282ce3L,
-	0x95c11548L, 0xe4c66d22L, 0x48c1133fL, 0xc70f86dcL,
-	0x07f9c9eeL, 0x41041f0fL, 0x404779a4L, 0x5d886e17L,
-	0x325f51ebL, 0xd59bc0d1L, 0xf2bcc18fL, 0x41113564L,
-	0x257b7834L, 0x602a9c60L, 0xdff8e8a3L, 0x1f636c1bL,
-	0x0e12b4c2L, 0x02e1329eL, 0xaf664fd1L, 0xcad18115L,
-	0x6b2395e0L, 0x333e92e1L, 0x3b240b62L, 0xeebeb922L,
-	0x85b2a20eL, 0xe6ba0d99L, 0xde720c8cL, 0x2da2f728L,
-	0xd0127845L, 0x95b794fdL, 0x647d0862L, 0xe7ccf5f0L,
-	0x5449a36fL, 0x877d48faL, 0xc39dfd27L, 0xf33e8d1eL,
-	0x0a476341L, 0x992eff74L, 0x3a6f6eabL, 0xf4f8fd37L,
-	0xa812dc60L, 0xa1ebddf8L, 0x991be14cL, 0xdb6e6b0dL,
-	0xc67b5510L, 0x6d672c37L, 0x2765d43bL, 0xdcd0e804L,
-	0xf1290dc7L, 0xcc00ffa3L, 0xb5390f92L, 0x690fed0bL,
-	0x667b9ffbL, 0xcedb7d9cL, 0xa091cf0bL, 0xd9155ea3L,
-	0xbb132f88L, 0x515bad24L, 0x7b9479bfL, 0x763bd6ebL,
-	0x37392eb3L, 0xcc115979L, 0x8026e297L, 0xf42e312dL,
-	0x6842ada7L, 0xc66a2b3bL, 0x12754cccL, 0x782ef11cL,
-	0x6a124237L, 0xb79251e7L, 0x06a1bbe6L, 0x4bfb6350L,
-	0x1a6b1018L, 0x11caedfaL, 0x3d25bdd8L, 0xe2e1c3c9L,
-	0x44421659L, 0x0a121386L, 0xd90cec6eL, 0xd5abea2aL,
-	0x64af674eL, 0xda86a85fL, 0xbebfe988L, 0x64e4c3feL,
-	0x9dbc8057L, 0xf0f7c086L, 0x60787bf8L, 0x6003604dL,
-	0xd1fd8346L, 0xf6381fb0L, 0x7745ae04L, 0xd736fcccL,
-	0x83426b33L, 0xf01eab71L, 0xb0804187L, 0x3c005e5fL,
-	0x77a057beL, 0xbde8ae24L, 0x55464299L, 0xbf582e61L,
-	0x4e58f48fL, 0xf2ddfda2L, 0xf474ef38L, 0x8789bdc2L,
-	0x5366f9c3L, 0xc8b38e74L, 0xb475f255L, 0x46fcd9b9L,
-	0x7aeb2661L, 0x8b1ddf84L, 0x846a0e79L, 0x915f95e2L,
-	0x466e598eL, 0x20b45770L, 0x8cd55591L, 0xc902de4cL,
-	0xb90bace1L, 0xbb8205d0L, 0x11a86248L, 0x7574a99eL,
-	0xb77f19b6L, 0xe0a9dc09L, 0x662d09a1L, 0xc4324633L,
-	0xe85a1f02L, 0x09f0be8cL, 0x4a99a025L, 0x1d6efe10L,
-	0x1ab93d1dL, 0x0ba5a4dfL, 0xa186f20fL, 0x2868f169L,
-	0xdcb7da83L, 0x573906feL, 0xa1e2ce9bL, 0x4fcd7f52L,
-	0x50115e01L, 0xa70683faL, 0xa002b5c4L, 0x0de6d027L,
-	0x9af88c27L, 0x773f8641L, 0xc3604c06L, 0x61a806b5L,
-	0xf0177a28L, 0xc0f586e0L, 0x006058aaL, 0x30dc7d62L,
-	0x11e69ed7L, 0x2338ea63L, 0x53c2dd94L, 0xc2c21634L,
-	0xbbcbee56L, 0x90bcb6deL, 0xebfc7da1L, 0xce591d76L,
-	0x6f05e409L, 0x4b7c0188L, 0x39720a3dL, 0x7c927c24L,
-	0x86e3725fL, 0x724d9db9L, 0x1ac15bb4L, 0xd39eb8fcL,
-	0xed545578L, 0x08fca5b5L, 0xd83d7cd3L, 0x4dad0fc4L,
-	0x1e50ef5eL, 0xb161e6f8L, 0xa28514d9L, 0x6c51133cL,
-	0x6fd5c7e7L, 0x56e14ec4L, 0x362abfceL, 0xddc6c837L,
-	0xd79a3234L, 0x92638212L, 0x670efa8eL, 0x406000e0L,
-	0x3a39ce37L, 0xd3faf5cfL, 0xabc27737L, 0x5ac52d1bL,
-	0x5cb0679eL, 0x4fa33742L, 0xd3822740L, 0x99bc9bbeL,
-	0xd5118e9dL, 0xbf0f7315L, 0xd62d1c7eL, 0xc700c47bL,
-	0xb78c1b6bL, 0x21a19045L, 0xb26eb1beL, 0x6a366eb4L,
-	0x5748ab2fL, 0xbc946e79L, 0xc6a376d2L, 0x6549c2c8L,
-	0x530ff8eeL, 0x468dde7dL, 0xd5730a1dL, 0x4cd04dc6L,
-	0x2939bbdbL, 0xa9ba4650L, 0xac9526e8L, 0xbe5ee304L,
-	0xa1fad5f0L, 0x6a2d519aL, 0x63ef8ce2L, 0x9a86ee22L,
-	0xc089c2b8L, 0x43242ef6L, 0xa51e03aaL, 0x9cf2d0a4L,
-	0x83c061baL, 0x9be96a4dL, 0x8fe51550L, 0xba645bd6L,
-	0x2826a2f9L, 0xa73a3ae1L, 0x4ba99586L, 0xef5562e9L,
-	0xc72fefd3L, 0xf752f7daL, 0x3f046f69L, 0x77fa0a59L,
-	0x80e4a915L, 0x87b08601L, 0x9b09e6adL, 0x3b3ee593L,
-	0xe990fd5aL, 0x9e34d797L, 0x2cf0b7d9L, 0x022b8b51L,
-	0x96d5ac3aL, 0x017da67dL, 0xd1cf3ed6L, 0x7c7d2d28L,
-	0x1f9f25cfL, 0xadf2b89bL, 0x5ad6b472L, 0x5a88f54cL,
-	0xe029ac71L, 0xe019a5e6L, 0x47b0acfdL, 0xed93fa9bL,
-	0xe8d3c48dL, 0x283b57ccL, 0xf8d56629L, 0x79132e28L,
-	0x785f0191L, 0xed756055L, 0xf7960e44L, 0xe3d35e8cL,
-	0x15056dd4L, 0x88f46dbaL, 0x03a16125L, 0x0564f0bdL,
-	0xc3eb9e15L, 0x3c9057a2L, 0x97271aecL, 0xa93a072aL,
-	0x1b3f6d9bL, 0x1e6321f5L, 0xf59c66fbL, 0x26dcf319L,
-	0x7533d928L, 0xb155fdf5L, 0x03563482L, 0x8aba3cbbL,
-	0x28517711L, 0xc20ad9f8L, 0xabcc5167L, 0xccad925fL,
-	0x4de81751L, 0x3830dc8eL, 0x379d5862L, 0x9320f991L,
-	0xea7a90c2L, 0xfb3e7bceL, 0x5121ce64L, 0x774fbe32L,
-	0xa8b6e37eL, 0xc3293d46L, 0x48de5369L, 0x6413e680L,
-	0xa2ae0810L, 0xdd6db224L, 0x69852dfdL, 0x09072166L,
-	0xb39a460aL, 0x6445c0ddL, 0x586cdecfL, 0x1c20c8aeL,
-	0x5bbef7ddL, 0x1b588d40L, 0xccd2017fL, 0x6bb4e3bbL,
-	0xdda26a7eL, 0x3a59ff45L, 0x3e350a44L, 0xbcb4cdd5L,
-	0x72eacea8L, 0xfa6484bbL, 0x8d6612aeL, 0xbf3c6f47L,
-	0xd29be463L, 0x542f5d9eL, 0xaec2771bL, 0xf64e6370L,
-	0x740e0d8dL, 0xe75b1357L, 0xf8721671L, 0xaf537d5dL,
-	0x4040cb08L, 0x4eb4e2ccL, 0x34d2466aL, 0x0115af84L,
-	0xe1b00428L, 0x95983a1dL, 0x06b89fb4L, 0xce6ea048L,
-	0x6f3f3b82L, 0x3520ab82L, 0x011a1d4bL, 0x277227f8L,
-	0x611560b1L, 0xe7933fdcL, 0xbb3a792bL, 0x344525bdL,
-	0xa08839e1L, 0x51ce794bL, 0x2f32c9b7L, 0xa01fbac9L,
-	0xe01cc87eL, 0xbcc7d1f6L, 0xcf0111c3L, 0xa1e8aac7L,
-	0x1a908749L, 0xd44fbd9aL, 0xd0dadecbL, 0xd50ada38L,
-	0x0339c32aL, 0xc6913667L, 0x8df9317cL, 0xe0b12b4fL,
-	0xf79e59b7L, 0x43f5bb3aL, 0xf2d519ffL, 0x27d9459cL,
-	0xbf97222cL, 0x15e6fc2aL, 0x0f91fc71L, 0x9b941525L,
-	0xfae59361L, 0xceb69cebL, 0xc2a86459L, 0x12baa8d1L,
-	0xb6c1075eL, 0xe3056a0cL, 0x10d25065L, 0xcb03a442L,
-	0xe0ec6e0eL, 0x1698db3bL, 0x4c98a0beL, 0x3278e964L,
-	0x9f1f9532L, 0xe0d392dfL, 0xd3a0342bL, 0x8971f21eL,
-	0x1b0a7441L, 0x4ba3348cL, 0xc5be7120L, 0xc37632d8L,
-	0xdf359f8dL, 0x9b992f2eL, 0xe60b6f47L, 0x0fe3f11dL,
-	0xe54cda54L, 0x1edad891L, 0xce6279cfL, 0xcd3e7e6fL,
-	0x1618b166L, 0xfd2c1d05L, 0x848fd2c5L, 0xf6fb2299L,
-	0xf523f357L, 0xa6327623L, 0x93a83531L, 0x56cccd02L,
-	0xacf08162L, 0x5a75ebb5L, 0x6e163697L, 0x88d273ccL,
-	0xde966292L, 0x81b949d0L, 0x4c50901bL, 0x71c65614L,
-	0xe6c6c7bdL, 0x327a140aL, 0x45e1d006L, 0xc3f27b9aL,
-	0xc9aa53fdL, 0x62a80f00L, 0xbb25bfe2L, 0x35bdd2f6L,
-	0x71126905L, 0xb2040222L, 0xb6cbcf7cL, 0xcd769c2bL,
-	0x53113ec0L, 0x1640e3d3L, 0x38abbd60L, 0x2547adf0L,
-	0xba38209cL, 0xf746ce76L, 0x77afa1c5L, 0x20756060L,
-	0x85cbfe4eL, 0x8ae88dd8L, 0x7aaaf9b0L, 0x4cf9aa7eL,
-	0x1948c25cL, 0x02fb8a8cL, 0x01c36ae4L, 0xd6ebe1f9L,
-	0x90d4f869L, 0xa65cdea0L, 0x3f09252dL, 0xc208e69fL,
-	0xb74e6132L, 0xce77e25bL, 0x578fdfe3L, 0x3ac372e6L,
-	}
-	};
-
diff --git a/sys/crypto/blowfish/bf_skey.c b/sys/crypto/blowfish/bf_skey.c
deleted file mode 100644
index 28b0e5a6b5..0000000000
--- a/sys/crypto/blowfish/bf_skey.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/*	$FreeBSD: src/sys/crypto/blowfish/bf_skey.c,v 1.6 2003/06/10 21:38:38 obrien Exp $	*/
-/*	$KAME: bf_skey.c,v 1.7 2002/02/27 01:33:59 itojun Exp $	*/
-
-/* crypto/bf/bf_skey.c */
-
-/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
- * All rights reserved.
- *
- * This package is an SSL implementation written
- * by Eric Young (eay@mincom.oz.au).
- * The implementation was written so as to conform with Netscapes SSL.
- *
- * This library is free for commercial and non-commercial use as long as
- * the following conditions are aheared to.  The following conditions
- * apply to all code found in this distribution, be it the RC4, RSA,
- * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
- * included with this distribution is covered by the same copyright terms
- * except that the holder is Tim Hudson (tjh@mincom.oz.au).
- *
- * Copyright remains Eric Young's, and as such any Copyright notices in
- * the code are not to be removed.
- * If this package is used in a product, Eric Young should be given attribution
- * as the author of the parts of the library used.
- * This can be in the form of a textual message at program startup or
- * in documentation (online or textual) provided with the package.
- *
- * 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 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *    "This product includes cryptographic software written by
- *     Eric Young (eay@mincom.oz.au)"
- *    The word 'cryptographic' can be left out if the rouines from the library
- *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
- *    the apps directory (application code) you must include an acknowledgement:
- *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
- *
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``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 AUTHOR 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.
- *
- * The licence and distribution terms for any publically available version or
- * derivative of this code cannot be changed.  i.e. this code cannot simply be
- * copied and put under another distribution licence
- * [including the GNU Public Licence.]
- */
-
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/systm.h>
-#include <crypto/blowfish/blowfish.h>
-#include <crypto/blowfish/bf_locl.h>
-#include <crypto/blowfish/bf_pi.h>
-
-void
-BF_set_key(BF_KEY *key, int len, unsigned char *data)
-{
-	int i;
-	BF_LONG *p, ri, in[2];
-	unsigned char *d, *end;
-
-	memcpy((char *)key, (const char *)&bf_init, sizeof(BF_KEY));
-	p = key->P;
-
-	if (len > ((BF_ROUNDS + 2) * 4))
-		len = (BF_ROUNDS + 2) * 4;
-
-	d = data;
-	end= &(data[len]);
-	for (i = 0; i < BF_ROUNDS + 2; i++) {
-		ri = *(d++);
-		if (d >= end) d = data;
-
-		ri <<= 8;
-		ri |= *(d++);
-		if (d >= end) d = data;
-
-		ri <<= 8;
-		ri |= *(d++);
-		if (d >= end) d = data;
-
-		ri <<= 8;
-		ri |= *(d++);
-		if (d >= end) d = data;
-
-		p[i] ^= ri;
-	}
-
-	in[0] = 0L;
-	in[1] = 0L;
-	for (i = 0; i < BF_ROUNDS + 2; i += 2) {
-		BF_encrypt(in, key);
-		p[i  ] = in[0];
-		p[i+1] = in[1];
-	}
-
-	p = key->S;
-	for (i = 0; i < 4 * 256; i += 2) {
-		BF_encrypt(in, key);
-		p[i  ] = in[0];
-		p[i+1] = in[1];
-	}
-}
diff --git a/sys/crypto/blowfish/blowfish.h b/sys/crypto/blowfish/blowfish.h
deleted file mode 100644
index 7964c391a5..0000000000
--- a/sys/crypto/blowfish/blowfish.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/*	$FreeBSD: src/sys/crypto/blowfish/blowfish.h,v 1.7 2003/10/13 19:26:08 ume Exp $	*/
-/*	$KAME: blowfish.h,v 1.12 2002/02/27 01:33:59 itojun Exp $	*/
-
-/* crypto/bf/blowfish.h */
-/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
- * All rights reserved.
- *
- * This package is an SSL implementation written
- * by Eric Young (eay@mincom.oz.au).
- * The implementation was written so as to conform with Netscapes SSL.
- *
- * This library is free for commercial and non-commercial use as long as
- * the following conditions are aheared to.  The following conditions
- * apply to all code found in this distribution, be it the RC4, RSA,
- * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
- * included with this distribution is covered by the same copyright terms
- * except that the holder is Tim Hudson (tjh@mincom.oz.au).
- *
- * Copyright remains Eric Young's, and as such any Copyright notices in
- * the code are not to be removed.
- * If this package is used in a product, Eric Young should be given attribution
- * as the author of the parts of the library used.
- * This can be in the form of a textual message at program startup or
- * in documentation (online or textual) provided with the package.
- *
- * 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 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *    "This product includes cryptographic software written by
- *     Eric Young (eay@mincom.oz.au)"
- *    The word 'cryptographic' can be left out if the rouines from the library
- *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
- *    the apps directory (application code) you must include an acknowledgement:
- *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
- *
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``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 AUTHOR 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.
- *
- * The licence and distribution terms for any publically available version or
- * derivative of this code cannot be changed.  i.e. this code cannot simply be
- * copied and put under another distribution licence
- * [including the GNU Public Licence.]
- */
-
-#ifndef HEADER_BLOWFISH_H
-#define HEADER_BLOWFISH_H
-
-#ifdef  __cplusplus
-extern "C" {
-#endif
-
-#define BF_ENCRYPT	1
-#define BF_DECRYPT	0
-
-/* must be 32bit quantity */
-#define BF_LONG u_int32_t
-
-#define BF_ROUNDS	16
-#define BF_BLOCK	8
-
-typedef struct bf_key_st {
-	BF_LONG P[BF_ROUNDS+2];
-	BF_LONG S[4*256];
-} BF_KEY;
-
-void BF_set_key(BF_KEY *, int, unsigned char *);
-void BF_encrypt(BF_LONG *, BF_KEY *);
-void BF_decrypt(BF_LONG *, BF_KEY *);
-void BF_ecb_encrypt(const unsigned char *, unsigned char *,
-		    BF_KEY *, int);
-
-#ifdef  __cplusplus
-}
-#endif
-
-#endif
diff --git a/sys/crypto/camellia/camellia-api.c b/sys/crypto/camellia/camellia-api.c
deleted file mode 100644
index 1be038b1ee..0000000000
--- a/sys/crypto/camellia/camellia-api.c
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- *
- * Copyright (c) 2006
- * NTT (Nippon Telegraph and Telephone Corporation) . 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 as
- *   the first lines of this file unmodified.
- * 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 NTT ``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 NTT 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.
- *
- * $FreeBSD: src/sys/crypto/camellia/camellia-api.c,v 1.1 2007/05/09 19:37:01 gnn Exp $
- */
-
-#include <sys/types.h>
-#ifdef _KERNEL
-#include <sys/systm.h>
-#endif
-#include <crypto/camellia/camellia.h>
-
-void
-camellia_set_key(camellia_ctx *ctx, const u_char *key, int bits)
-{
-
-    Camellia_Ekeygen(bits, key, ctx->subkey);
-    ctx->bits = bits;
-}
-
-void
-camellia_decrypt(const camellia_ctx *ctx, const u_char *src, u_char *dst)
-{
-
-    Camellia_DecryptBlock(ctx->bits, src, ctx->subkey, dst);
-}
-
-void
-camellia_encrypt(const camellia_ctx *ctx, const u_char *src, u_char *dst)
-{
-
-    Camellia_EncryptBlock(ctx->bits, src, ctx->subkey, dst);
-}
diff --git a/sys/crypto/camellia/camellia.c b/sys/crypto/camellia/camellia.c
deleted file mode 100644
index 240b3d3f79..0000000000
--- a/sys/crypto/camellia/camellia.c
+++ /dev/null
@@ -1,1332 +0,0 @@
-/* camellia.h ver 1.1.0
- *
- * Copyright (c) 2006
- * NTT (Nippon Telegraph and Telephone Corporation) . 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 as
- *   the first lines of this file unmodified.
- * 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 NTT ``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 NTT 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.
- *
- * $FreeBSD: src/sys/crypto/camellia/camellia.c,v 1.1 2007/05/09 19:37:01 gnn Exp $
- */
-
-/*
- * Algorithm Specification
- *  http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html
- */
-
-#include <sys/types.h>
-#include <sys/endian.h>
-#ifdef _KERNEL
-#include <sys/param.h>
-#include <sys/systm.h>
-#else
-#include <string.h>
-#include <assert.h>
-#define KASSERT(exp, msg) assert(exp)
-#endif
-
-#include <crypto/camellia/camellia.h>
-
-
-/* key constants */
-
-#define CAMELLIA_SIGMA1L (0xA09E667FL)
-#define CAMELLIA_SIGMA1R (0x3BCC908BL)
-#define CAMELLIA_SIGMA2L (0xB67AE858L)
-#define CAMELLIA_SIGMA2R (0x4CAA73B2L)
-#define CAMELLIA_SIGMA3L (0xC6EF372FL)
-#define CAMELLIA_SIGMA3R (0xE94F82BEL)
-#define CAMELLIA_SIGMA4L (0x54FF53A5L)
-#define CAMELLIA_SIGMA4R (0xF1D36F1CL)
-#define CAMELLIA_SIGMA5L (0x10E527FAL)
-#define CAMELLIA_SIGMA5R (0xDE682D1DL)
-#define CAMELLIA_SIGMA6L (0xB05688C2L)
-#define CAMELLIA_SIGMA6R (0xB3E6C1FDL)
-
-/*
- *  macros
- */
-#define GETU32(pt) (((uint32_t)(pt)[0] << 24)		\
-		     ^ ((uint32_t)(pt)[1] << 16)	\
-		     ^ ((uint32_t)(pt)[2] <<  8)	\
-		     ^ ((uint32_t)(pt)[3]))
-
-#define PUTU32(ct, st) {(ct)[0] = (uint8_t)((st) >> 24);	\
-			(ct)[1] = (uint8_t)((st) >> 16);	\
-			(ct)[2] = (uint8_t)((st) >>  8);	\
-			(ct)[3] = (uint8_t)(st);}
-
-#define SUBL(INDEX) (subkey[(INDEX)*2+1])
-#define SUBR(INDEX) (subkey[(INDEX)*2])
-
-#define CAMELLIA_RR8(x) (((x) >> 8) + ((x) << 24))
-#define CAMELLIA_RL1(x) (((x) << 1) + ((x) >> 31))
-#define CAMELLIA_RL8(x) (((x) << 8) + ((x) >> 24))
-
-#define CAMELLIA_ROLDQ(ll, lr, rl, rr, w0, w1, bits)	\
-    do {						\
-	w0 = ll;					\
-	ll = (ll << bits) + (lr >> (32 - bits));	\
-	lr = (lr << bits) + (rl >> (32 - bits));	\
-	rl = (rl << bits) + (rr >> (32 - bits));	\
-	rr = (rr << bits) + (w0 >> (32 - bits));	\
-    } while(0)
-
-#define CAMELLIA_ROLDQo32(ll, lr, rl, rr, w0, w1, bits)	\
-    do {						\
-	w0 = ll;					\
-	w1 = lr;					\
-	ll = (lr << (bits - 32)) + (rl >> (64 - bits));	\
-	lr = (rl << (bits - 32)) + (rr >> (64 - bits));	\
-	rl = (rr << (bits - 32)) + (w0 >> (64 - bits));	\
-	rr = (w0 << (bits - 32)) + (w1 >> (64 - bits));	\
-    } while(0)
-
-#define CAMELLIA_SP1110(INDEX) (camellia_sp1110[(INDEX)])
-#define CAMELLIA_SP0222(INDEX) (camellia_sp0222[(INDEX)])
-#define CAMELLIA_SP3033(INDEX) (camellia_sp3033[(INDEX)])
-#define CAMELLIA_SP4404(INDEX) (camellia_sp4404[(INDEX)])
-
-#define CAMELLIA_F(xl, xr, kl, kr, yl, yr, il, ir, t0, t1)	\
-    do {							\
-	il = xl ^ kl;						\
-	ir = xr ^ kr;						\
-	t0 = il >> 16;						\
-	t1 = ir >> 16;						\
-	yl = CAMELLIA_SP1110(ir & 0xff)				\
-	    ^ CAMELLIA_SP0222((t1 >> 8) & 0xff)			\
-	    ^ CAMELLIA_SP3033(t1 & 0xff)			\
-	    ^ CAMELLIA_SP4404((ir >> 8) & 0xff);		\
-	yr = CAMELLIA_SP1110((t0 >> 8) & 0xff)			\
-	    ^ CAMELLIA_SP0222(t0 & 0xff)			\
-	    ^ CAMELLIA_SP3033((il >> 8) & 0xff)			\
-	    ^ CAMELLIA_SP4404(il & 0xff);			\
-	yl ^= yr;						\
-	yr = CAMELLIA_RR8(yr);					\
-	yr ^= yl;						\
-    } while(0)
-
-
-#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \
-    do {								\
-	t0 = kll;							\
-	t2 = krr;							\
-	t0 &= ll;							\
-	t2 |= rr;							\
-	rl ^= t2;							\
-	lr ^= CAMELLIA_RL1(t0);						\
-	t3 = krl;							\
-	t1 = klr;							\
-	t3 &= rl;							\
-	t1 |= lr;							\
-	ll ^= t1;							\
-	rr ^= CAMELLIA_RL1(t3);						\
-    } while(0)
-
-#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir, t0, t1)	\
-    do {								\
-	ir =  CAMELLIA_SP1110(xr & 0xff);				\
-	il =  CAMELLIA_SP1110((xl>>24) & 0xff);				\
-	ir ^= CAMELLIA_SP0222((xr>>24) & 0xff);				\
-	il ^= CAMELLIA_SP0222((xl>>16) & 0xff);				\
-	ir ^= CAMELLIA_SP3033((xr>>16) & 0xff);				\
-	il ^= CAMELLIA_SP3033((xl>>8) & 0xff);				\
-	ir ^= CAMELLIA_SP4404((xr>>8) & 0xff);				\
-	il ^= CAMELLIA_SP4404(xl & 0xff);				\
-	il ^= kl;							\
-	ir ^= kr;							\
-	ir ^= il;							\
-	il = CAMELLIA_RR8(il);						\
-	il ^= ir;							\
-	yl ^= ir;							\
-	yr ^= il;							\
-    } while(0)
-
-
-static const uint32_t camellia_sp1110[256] = {
-    0x70707000,0x82828200,0x2c2c2c00,0xececec00,
-    0xb3b3b300,0x27272700,0xc0c0c000,0xe5e5e500,
-    0xe4e4e400,0x85858500,0x57575700,0x35353500,
-    0xeaeaea00,0x0c0c0c00,0xaeaeae00,0x41414100,
-    0x23232300,0xefefef00,0x6b6b6b00,0x93939300,
-    0x45454500,0x19191900,0xa5a5a500,0x21212100,
-    0xededed00,0x0e0e0e00,0x4f4f4f00,0x4e4e4e00,
-    0x1d1d1d00,0x65656500,0x92929200,0xbdbdbd00,
-    0x86868600,0xb8b8b800,0xafafaf00,0x8f8f8f00,
-    0x7c7c7c00,0xebebeb00,0x1f1f1f00,0xcecece00,
-    0x3e3e3e00,0x30303000,0xdcdcdc00,0x5f5f5f00,
-    0x5e5e5e00,0xc5c5c500,0x0b0b0b00,0x1a1a1a00,
-    0xa6a6a600,0xe1e1e100,0x39393900,0xcacaca00,
-    0xd5d5d500,0x47474700,0x5d5d5d00,0x3d3d3d00,
-    0xd9d9d900,0x01010100,0x5a5a5a00,0xd6d6d600,
-    0x51515100,0x56565600,0x6c6c6c00,0x4d4d4d00,
-    0x8b8b8b00,0x0d0d0d00,0x9a9a9a00,0x66666600,
-    0xfbfbfb00,0xcccccc00,0xb0b0b000,0x2d2d2d00,
-    0x74747400,0x12121200,0x2b2b2b00,0x20202000,
-    0xf0f0f000,0xb1b1b100,0x84848400,0x99999900,
-    0xdfdfdf00,0x4c4c4c00,0xcbcbcb00,0xc2c2c200,
-    0x34343400,0x7e7e7e00,0x76767600,0x05050500,
-    0x6d6d6d00,0xb7b7b700,0xa9a9a900,0x31313100,
-    0xd1d1d100,0x17171700,0x04040400,0xd7d7d700,
-    0x14141400,0x58585800,0x3a3a3a00,0x61616100,
-    0xdedede00,0x1b1b1b00,0x11111100,0x1c1c1c00,
-    0x32323200,0x0f0f0f00,0x9c9c9c00,0x16161600,
-    0x53535300,0x18181800,0xf2f2f200,0x22222200,
-    0xfefefe00,0x44444400,0xcfcfcf00,0xb2b2b200,
-    0xc3c3c300,0xb5b5b500,0x7a7a7a00,0x91919100,
-    0x24242400,0x08080800,0xe8e8e800,0xa8a8a800,
-    0x60606000,0xfcfcfc00,0x69696900,0x50505000,
-    0xaaaaaa00,0xd0d0d000,0xa0a0a000,0x7d7d7d00,
-    0xa1a1a100,0x89898900,0x62626200,0x97979700,
-    0x54545400,0x5b5b5b00,0x1e1e1e00,0x95959500,
-    0xe0e0e000,0xffffff00,0x64646400,0xd2d2d200,
-    0x10101000,0xc4c4c400,0x00000000,0x48484800,
-    0xa3a3a300,0xf7f7f700,0x75757500,0xdbdbdb00,
-    0x8a8a8a00,0x03030300,0xe6e6e600,0xdadada00,
-    0x09090900,0x3f3f3f00,0xdddddd00,0x94949400,
-    0x87878700,0x5c5c5c00,0x83838300,0x02020200,
-    0xcdcdcd00,0x4a4a4a00,0x90909000,0x33333300,
-    0x73737300,0x67676700,0xf6f6f600,0xf3f3f300,
-    0x9d9d9d00,0x7f7f7f00,0xbfbfbf00,0xe2e2e200,
-    0x52525200,0x9b9b9b00,0xd8d8d800,0x26262600,
-    0xc8c8c800,0x37373700,0xc6c6c600,0x3b3b3b00,
-    0x81818100,0x96969600,0x6f6f6f00,0x4b4b4b00,
-    0x13131300,0xbebebe00,0x63636300,0x2e2e2e00,
-    0xe9e9e900,0x79797900,0xa7a7a700,0x8c8c8c00,
-    0x9f9f9f00,0x6e6e6e00,0xbcbcbc00,0x8e8e8e00,
-    0x29292900,0xf5f5f500,0xf9f9f900,0xb6b6b600,
-    0x2f2f2f00,0xfdfdfd00,0xb4b4b400,0x59595900,
-    0x78787800,0x98989800,0x06060600,0x6a6a6a00,
-    0xe7e7e700,0x46464600,0x71717100,0xbababa00,
-    0xd4d4d400,0x25252500,0xababab00,0x42424200,
-    0x88888800,0xa2a2a200,0x8d8d8d00,0xfafafa00,
-    0x72727200,0x07070700,0xb9b9b900,0x55555500,
-    0xf8f8f800,0xeeeeee00,0xacacac00,0x0a0a0a00,
-    0x36363600,0x49494900,0x2a2a2a00,0x68686800,
-    0x3c3c3c00,0x38383800,0xf1f1f100,0xa4a4a400,
-    0x40404000,0x28282800,0xd3d3d300,0x7b7b7b00,
-    0xbbbbbb00,0xc9c9c900,0x43434300,0xc1c1c100,
-    0x15151500,0xe3e3e300,0xadadad00,0xf4f4f400,
-    0x77777700,0xc7c7c700,0x80808000,0x9e9e9e00,
-};
-
-static const uint32_t camellia_sp0222[256] = {
-    0x00e0e0e0,0x00050505,0x00585858,0x00d9d9d9,
-    0x00676767,0x004e4e4e,0x00818181,0x00cbcbcb,
-    0x00c9c9c9,0x000b0b0b,0x00aeaeae,0x006a6a6a,
-    0x00d5d5d5,0x00181818,0x005d5d5d,0x00828282,
-    0x00464646,0x00dfdfdf,0x00d6d6d6,0x00272727,
-    0x008a8a8a,0x00323232,0x004b4b4b,0x00424242,
-    0x00dbdbdb,0x001c1c1c,0x009e9e9e,0x009c9c9c,
-    0x003a3a3a,0x00cacaca,0x00252525,0x007b7b7b,
-    0x000d0d0d,0x00717171,0x005f5f5f,0x001f1f1f,
-    0x00f8f8f8,0x00d7d7d7,0x003e3e3e,0x009d9d9d,
-    0x007c7c7c,0x00606060,0x00b9b9b9,0x00bebebe,
-    0x00bcbcbc,0x008b8b8b,0x00161616,0x00343434,
-    0x004d4d4d,0x00c3c3c3,0x00727272,0x00959595,
-    0x00ababab,0x008e8e8e,0x00bababa,0x007a7a7a,
-    0x00b3b3b3,0x00020202,0x00b4b4b4,0x00adadad,
-    0x00a2a2a2,0x00acacac,0x00d8d8d8,0x009a9a9a,
-    0x00171717,0x001a1a1a,0x00353535,0x00cccccc,
-    0x00f7f7f7,0x00999999,0x00616161,0x005a5a5a,
-    0x00e8e8e8,0x00242424,0x00565656,0x00404040,
-    0x00e1e1e1,0x00636363,0x00090909,0x00333333,
-    0x00bfbfbf,0x00989898,0x00979797,0x00858585,
-    0x00686868,0x00fcfcfc,0x00ececec,0x000a0a0a,
-    0x00dadada,0x006f6f6f,0x00535353,0x00626262,
-    0x00a3a3a3,0x002e2e2e,0x00080808,0x00afafaf,
-    0x00282828,0x00b0b0b0,0x00747474,0x00c2c2c2,
-    0x00bdbdbd,0x00363636,0x00222222,0x00383838,
-    0x00646464,0x001e1e1e,0x00393939,0x002c2c2c,
-    0x00a6a6a6,0x00303030,0x00e5e5e5,0x00444444,
-    0x00fdfdfd,0x00888888,0x009f9f9f,0x00656565,
-    0x00878787,0x006b6b6b,0x00f4f4f4,0x00232323,
-    0x00484848,0x00101010,0x00d1d1d1,0x00515151,
-    0x00c0c0c0,0x00f9f9f9,0x00d2d2d2,0x00a0a0a0,
-    0x00555555,0x00a1a1a1,0x00414141,0x00fafafa,
-    0x00434343,0x00131313,0x00c4c4c4,0x002f2f2f,
-    0x00a8a8a8,0x00b6b6b6,0x003c3c3c,0x002b2b2b,
-    0x00c1c1c1,0x00ffffff,0x00c8c8c8,0x00a5a5a5,
-    0x00202020,0x00898989,0x00000000,0x00909090,
-    0x00474747,0x00efefef,0x00eaeaea,0x00b7b7b7,
-    0x00151515,0x00060606,0x00cdcdcd,0x00b5b5b5,
-    0x00121212,0x007e7e7e,0x00bbbbbb,0x00292929,
-    0x000f0f0f,0x00b8b8b8,0x00070707,0x00040404,
-    0x009b9b9b,0x00949494,0x00212121,0x00666666,
-    0x00e6e6e6,0x00cecece,0x00ededed,0x00e7e7e7,
-    0x003b3b3b,0x00fefefe,0x007f7f7f,0x00c5c5c5,
-    0x00a4a4a4,0x00373737,0x00b1b1b1,0x004c4c4c,
-    0x00919191,0x006e6e6e,0x008d8d8d,0x00767676,
-    0x00030303,0x002d2d2d,0x00dedede,0x00969696,
-    0x00262626,0x007d7d7d,0x00c6c6c6,0x005c5c5c,
-    0x00d3d3d3,0x00f2f2f2,0x004f4f4f,0x00191919,
-    0x003f3f3f,0x00dcdcdc,0x00797979,0x001d1d1d,
-    0x00525252,0x00ebebeb,0x00f3f3f3,0x006d6d6d,
-    0x005e5e5e,0x00fbfbfb,0x00696969,0x00b2b2b2,
-    0x00f0f0f0,0x00313131,0x000c0c0c,0x00d4d4d4,
-    0x00cfcfcf,0x008c8c8c,0x00e2e2e2,0x00757575,
-    0x00a9a9a9,0x004a4a4a,0x00575757,0x00848484,
-    0x00111111,0x00454545,0x001b1b1b,0x00f5f5f5,
-    0x00e4e4e4,0x000e0e0e,0x00737373,0x00aaaaaa,
-    0x00f1f1f1,0x00dddddd,0x00595959,0x00141414,
-    0x006c6c6c,0x00929292,0x00545454,0x00d0d0d0,
-    0x00787878,0x00707070,0x00e3e3e3,0x00494949,
-    0x00808080,0x00505050,0x00a7a7a7,0x00f6f6f6,
-    0x00777777,0x00939393,0x00868686,0x00838383,
-    0x002a2a2a,0x00c7c7c7,0x005b5b5b,0x00e9e9e9,
-    0x00eeeeee,0x008f8f8f,0x00010101,0x003d3d3d,
-};
-
-static const uint32_t camellia_sp3033[256] = {
-    0x38003838,0x41004141,0x16001616,0x76007676,
-    0xd900d9d9,0x93009393,0x60006060,0xf200f2f2,
-    0x72007272,0xc200c2c2,0xab00abab,0x9a009a9a,
-    0x75007575,0x06000606,0x57005757,0xa000a0a0,
-    0x91009191,0xf700f7f7,0xb500b5b5,0xc900c9c9,
-    0xa200a2a2,0x8c008c8c,0xd200d2d2,0x90009090,
-    0xf600f6f6,0x07000707,0xa700a7a7,0x27002727,
-    0x8e008e8e,0xb200b2b2,0x49004949,0xde00dede,
-    0x43004343,0x5c005c5c,0xd700d7d7,0xc700c7c7,
-    0x3e003e3e,0xf500f5f5,0x8f008f8f,0x67006767,
-    0x1f001f1f,0x18001818,0x6e006e6e,0xaf00afaf,
-    0x2f002f2f,0xe200e2e2,0x85008585,0x0d000d0d,
-    0x53005353,0xf000f0f0,0x9c009c9c,0x65006565,
-    0xea00eaea,0xa300a3a3,0xae00aeae,0x9e009e9e,
-    0xec00ecec,0x80008080,0x2d002d2d,0x6b006b6b,
-    0xa800a8a8,0x2b002b2b,0x36003636,0xa600a6a6,
-    0xc500c5c5,0x86008686,0x4d004d4d,0x33003333,
-    0xfd00fdfd,0x66006666,0x58005858,0x96009696,
-    0x3a003a3a,0x09000909,0x95009595,0x10001010,
-    0x78007878,0xd800d8d8,0x42004242,0xcc00cccc,
-    0xef00efef,0x26002626,0xe500e5e5,0x61006161,
-    0x1a001a1a,0x3f003f3f,0x3b003b3b,0x82008282,
-    0xb600b6b6,0xdb00dbdb,0xd400d4d4,0x98009898,
-    0xe800e8e8,0x8b008b8b,0x02000202,0xeb00ebeb,
-    0x0a000a0a,0x2c002c2c,0x1d001d1d,0xb000b0b0,
-    0x6f006f6f,0x8d008d8d,0x88008888,0x0e000e0e,
-    0x19001919,0x87008787,0x4e004e4e,0x0b000b0b,
-    0xa900a9a9,0x0c000c0c,0x79007979,0x11001111,
-    0x7f007f7f,0x22002222,0xe700e7e7,0x59005959,
-    0xe100e1e1,0xda00dada,0x3d003d3d,0xc800c8c8,
-    0x12001212,0x04000404,0x74007474,0x54005454,
-    0x30003030,0x7e007e7e,0xb400b4b4,0x28002828,
-    0x55005555,0x68006868,0x50005050,0xbe00bebe,
-    0xd000d0d0,0xc400c4c4,0x31003131,0xcb00cbcb,
-    0x2a002a2a,0xad00adad,0x0f000f0f,0xca00caca,
-    0x70007070,0xff00ffff,0x32003232,0x69006969,
-    0x08000808,0x62006262,0x00000000,0x24002424,
-    0xd100d1d1,0xfb00fbfb,0xba00baba,0xed00eded,
-    0x45004545,0x81008181,0x73007373,0x6d006d6d,
-    0x84008484,0x9f009f9f,0xee00eeee,0x4a004a4a,
-    0xc300c3c3,0x2e002e2e,0xc100c1c1,0x01000101,
-    0xe600e6e6,0x25002525,0x48004848,0x99009999,
-    0xb900b9b9,0xb300b3b3,0x7b007b7b,0xf900f9f9,
-    0xce00cece,0xbf00bfbf,0xdf00dfdf,0x71007171,
-    0x29002929,0xcd00cdcd,0x6c006c6c,0x13001313,
-    0x64006464,0x9b009b9b,0x63006363,0x9d009d9d,
-    0xc000c0c0,0x4b004b4b,0xb700b7b7,0xa500a5a5,
-    0x89008989,0x5f005f5f,0xb100b1b1,0x17001717,
-    0xf400f4f4,0xbc00bcbc,0xd300d3d3,0x46004646,
-    0xcf00cfcf,0x37003737,0x5e005e5e,0x47004747,
-    0x94009494,0xfa00fafa,0xfc00fcfc,0x5b005b5b,
-    0x97009797,0xfe00fefe,0x5a005a5a,0xac00acac,
-    0x3c003c3c,0x4c004c4c,0x03000303,0x35003535,
-    0xf300f3f3,0x23002323,0xb800b8b8,0x5d005d5d,
-    0x6a006a6a,0x92009292,0xd500d5d5,0x21002121,
-    0x44004444,0x51005151,0xc600c6c6,0x7d007d7d,
-    0x39003939,0x83008383,0xdc00dcdc,0xaa00aaaa,
-    0x7c007c7c,0x77007777,0x56005656,0x05000505,
-    0x1b001b1b,0xa400a4a4,0x15001515,0x34003434,
-    0x1e001e1e,0x1c001c1c,0xf800f8f8,0x52005252,
-    0x20002020,0x14001414,0xe900e9e9,0xbd00bdbd,
-    0xdd00dddd,0xe400e4e4,0xa100a1a1,0xe000e0e0,
-    0x8a008a8a,0xf100f1f1,0xd600d6d6,0x7a007a7a,
-    0xbb00bbbb,0xe300e3e3,0x40004040,0x4f004f4f,
-};
-
-static const uint32_t camellia_sp4404[256] = {
-    0x70700070,0x2c2c002c,0xb3b300b3,0xc0c000c0,
-    0xe4e400e4,0x57570057,0xeaea00ea,0xaeae00ae,
-    0x23230023,0x6b6b006b,0x45450045,0xa5a500a5,
-    0xeded00ed,0x4f4f004f,0x1d1d001d,0x92920092,
-    0x86860086,0xafaf00af,0x7c7c007c,0x1f1f001f,
-    0x3e3e003e,0xdcdc00dc,0x5e5e005e,0x0b0b000b,
-    0xa6a600a6,0x39390039,0xd5d500d5,0x5d5d005d,
-    0xd9d900d9,0x5a5a005a,0x51510051,0x6c6c006c,
-    0x8b8b008b,0x9a9a009a,0xfbfb00fb,0xb0b000b0,
-    0x74740074,0x2b2b002b,0xf0f000f0,0x84840084,
-    0xdfdf00df,0xcbcb00cb,0x34340034,0x76760076,
-    0x6d6d006d,0xa9a900a9,0xd1d100d1,0x04040004,
-    0x14140014,0x3a3a003a,0xdede00de,0x11110011,
-    0x32320032,0x9c9c009c,0x53530053,0xf2f200f2,
-    0xfefe00fe,0xcfcf00cf,0xc3c300c3,0x7a7a007a,
-    0x24240024,0xe8e800e8,0x60600060,0x69690069,
-    0xaaaa00aa,0xa0a000a0,0xa1a100a1,0x62620062,
-    0x54540054,0x1e1e001e,0xe0e000e0,0x64640064,
-    0x10100010,0x00000000,0xa3a300a3,0x75750075,
-    0x8a8a008a,0xe6e600e6,0x09090009,0xdddd00dd,
-    0x87870087,0x83830083,0xcdcd00cd,0x90900090,
-    0x73730073,0xf6f600f6,0x9d9d009d,0xbfbf00bf,
-    0x52520052,0xd8d800d8,0xc8c800c8,0xc6c600c6,
-    0x81810081,0x6f6f006f,0x13130013,0x63630063,
-    0xe9e900e9,0xa7a700a7,0x9f9f009f,0xbcbc00bc,
-    0x29290029,0xf9f900f9,0x2f2f002f,0xb4b400b4,
-    0x78780078,0x06060006,0xe7e700e7,0x71710071,
-    0xd4d400d4,0xabab00ab,0x88880088,0x8d8d008d,
-    0x72720072,0xb9b900b9,0xf8f800f8,0xacac00ac,
-    0x36360036,0x2a2a002a,0x3c3c003c,0xf1f100f1,
-    0x40400040,0xd3d300d3,0xbbbb00bb,0x43430043,
-    0x15150015,0xadad00ad,0x77770077,0x80800080,
-    0x82820082,0xecec00ec,0x27270027,0xe5e500e5,
-    0x85850085,0x35350035,0x0c0c000c,0x41410041,
-    0xefef00ef,0x93930093,0x19190019,0x21210021,
-    0x0e0e000e,0x4e4e004e,0x65650065,0xbdbd00bd,
-    0xb8b800b8,0x8f8f008f,0xebeb00eb,0xcece00ce,
-    0x30300030,0x5f5f005f,0xc5c500c5,0x1a1a001a,
-    0xe1e100e1,0xcaca00ca,0x47470047,0x3d3d003d,
-    0x01010001,0xd6d600d6,0x56560056,0x4d4d004d,
-    0x0d0d000d,0x66660066,0xcccc00cc,0x2d2d002d,
-    0x12120012,0x20200020,0xb1b100b1,0x99990099,
-    0x4c4c004c,0xc2c200c2,0x7e7e007e,0x05050005,
-    0xb7b700b7,0x31310031,0x17170017,0xd7d700d7,
-    0x58580058,0x61610061,0x1b1b001b,0x1c1c001c,
-    0x0f0f000f,0x16160016,0x18180018,0x22220022,
-    0x44440044,0xb2b200b2,0xb5b500b5,0x91910091,
-    0x08080008,0xa8a800a8,0xfcfc00fc,0x50500050,
-    0xd0d000d0,0x7d7d007d,0x89890089,0x97970097,
-    0x5b5b005b,0x95950095,0xffff00ff,0xd2d200d2,
-    0xc4c400c4,0x48480048,0xf7f700f7,0xdbdb00db,
-    0x03030003,0xdada00da,0x3f3f003f,0x94940094,
-    0x5c5c005c,0x02020002,0x4a4a004a,0x33330033,
-    0x67670067,0xf3f300f3,0x7f7f007f,0xe2e200e2,
-    0x9b9b009b,0x26260026,0x37370037,0x3b3b003b,
-    0x96960096,0x4b4b004b,0xbebe00be,0x2e2e002e,
-    0x79790079,0x8c8c008c,0x6e6e006e,0x8e8e008e,
-    0xf5f500f5,0xb6b600b6,0xfdfd00fd,0x59590059,
-    0x98980098,0x6a6a006a,0x46460046,0xbaba00ba,
-    0x25250025,0x42420042,0xa2a200a2,0xfafa00fa,
-    0x07070007,0x55550055,0xeeee00ee,0x0a0a000a,
-    0x49490049,0x68680068,0x38380038,0xa4a400a4,
-    0x28280028,0x7b7b007b,0xc9c900c9,0xc1c100c1,
-    0xe3e300e3,0xf4f400f4,0xc7c700c7,0x9e9e009e,
-};
-
-
-/*
- * Stuff related to the Camellia key schedule
- */
-#define subl(x) subL[(x)]
-#define subr(x) subR[(x)]
-
-void
-camellia_setup128(const unsigned char *key, uint32_t *subkey)
-{
-    uint32_t kll, klr, krl, krr;
-    uint32_t il, ir, t0, t1, w0, w1;
-    uint32_t kw4l, kw4r, dw, tl, tr;
-    uint32_t subL[26];
-    uint32_t subR[26];
-
-    /*
-     *  k == kll || klr || krl || krr (|| is concatination)
-     */
-    kll = GETU32(key     );
-    klr = GETU32(key +  4);
-    krl = GETU32(key +  8);
-    krr = GETU32(key + 12);
-    /*
-     * generate KL dependent subkeys
-     */
-    subl(0) = kll; subr(0) = klr;
-    subl(1) = krl; subr(1) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(4) = kll; subr(4) = klr;
-    subl(5) = krl; subr(5) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30);
-    subl(10) = kll; subr(10) = klr;
-    subl(11) = krl; subr(11) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(13) = krl; subr(13) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
-    subl(16) = kll; subr(16) = klr;
-    subl(17) = krl; subr(17) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
-    subl(18) = kll; subr(18) = klr;
-    subl(19) = krl; subr(19) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
-    subl(22) = kll; subr(22) = klr;
-    subl(23) = krl; subr(23) = krr;
-
-    /* generate KA */
-    kll = subl(0); klr = subr(0);
-    krl = subl(1); krr = subr(1);
-    CAMELLIA_F(kll, klr, CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R,
-	       w0, w1, il, ir, t0, t1);
-    krl ^= w0; krr ^= w1;
-    CAMELLIA_F(krl, krr, CAMELLIA_SIGMA2L, CAMELLIA_SIGMA2R,
-	       kll, klr, il, ir, t0, t1);
-    CAMELLIA_F(kll, klr, CAMELLIA_SIGMA3L, CAMELLIA_SIGMA3R,
-	       krl, krr, il, ir, t0, t1);
-    krl ^= w0; krr ^= w1;
-    CAMELLIA_F(krl, krr, CAMELLIA_SIGMA4L, CAMELLIA_SIGMA4R,
-	       w0, w1, il, ir, t0, t1);
-    kll ^= w0; klr ^= w1;
-
-    /* generate KA dependent subkeys */
-    subl(2) = kll; subr(2) = klr;
-    subl(3) = krl; subr(3) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(6) = kll; subr(6) = klr;
-    subl(7) = krl; subr(7) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(8) = kll; subr(8) = klr;
-    subl(9) = krl; subr(9) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(12) = kll; subr(12) = klr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(14) = kll; subr(14) = klr;
-    subl(15) = krl; subr(15) = krr;
-    CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34);
-    subl(20) = kll; subr(20) = klr;
-    subl(21) = krl; subr(21) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
-    subl(24) = kll; subr(24) = klr;
-    subl(25) = krl; subr(25) = krr;
-
-
-    /* absorb kw2 to other subkeys */
-    subl(3) ^= subl(1); subr(3) ^= subr(1);
-    subl(5) ^= subl(1); subr(5) ^= subr(1);
-    subl(7) ^= subl(1); subr(7) ^= subr(1);
-    subl(1) ^= subr(1) & ~subr(9);
-    dw = subl(1) & subl(9), subr(1) ^= CAMELLIA_RL1(dw);
-    subl(11) ^= subl(1); subr(11) ^= subr(1);
-    subl(13) ^= subl(1); subr(13) ^= subr(1);
-    subl(15) ^= subl(1); subr(15) ^= subr(1);
-    subl(1) ^= subr(1) & ~subr(17);
-    dw = subl(1) & subl(17), subr(1) ^= CAMELLIA_RL1(dw);
-    subl(19) ^= subl(1); subr(19) ^= subr(1);
-    subl(21) ^= subl(1); subr(21) ^= subr(1);
-    subl(23) ^= subl(1); subr(23) ^= subr(1);
-    subl(24) ^= subl(1); subr(24) ^= subr(1);
-
-    /* absorb kw4 to other subkeys */
-    kw4l = subl(25); kw4r = subr(25);
-    subl(22) ^= kw4l; subr(22) ^= kw4r;
-    subl(20) ^= kw4l; subr(20) ^= kw4r;
-    subl(18) ^= kw4l; subr(18) ^= kw4r;
-    kw4l ^= kw4r & ~subr(16);
-    dw = kw4l & subl(16), kw4r ^= CAMELLIA_RL1(dw);
-    subl(14) ^= kw4l; subr(14) ^= kw4r;
-    subl(12) ^= kw4l; subr(12) ^= kw4r;
-    subl(10) ^= kw4l; subr(10) ^= kw4r;
-    kw4l ^= kw4r & ~subr(8);
-    dw = kw4l & subl(8), kw4r ^= CAMELLIA_RL1(dw);
-    subl(6) ^= kw4l; subr(6) ^= kw4r;
-    subl(4) ^= kw4l; subr(4) ^= kw4r;
-    subl(2) ^= kw4l; subr(2) ^= kw4r;
-    subl(0) ^= kw4l; subr(0) ^= kw4r;
-
-    /* key XOR is end of F-function */
-    SUBL(0) = subl(0) ^ subl(2);
-    SUBR(0) = subr(0) ^ subr(2);
-    SUBL(2) = subl(3);
-    SUBR(2) = subr(3);
-    SUBL(3) = subl(2) ^ subl(4);
-    SUBR(3) = subr(2) ^ subr(4);
-    SUBL(4) = subl(3) ^ subl(5);
-    SUBR(4) = subr(3) ^ subr(5);
-    SUBL(5) = subl(4) ^ subl(6);
-    SUBR(5) = subr(4) ^ subr(6);
-    SUBL(6) = subl(5) ^ subl(7);
-    SUBR(6) = subr(5) ^ subr(7);
-    tl = subl(10) ^ (subr(10) & ~subr(8));
-    dw = tl & subl(8), tr = subr(10) ^ CAMELLIA_RL1(dw);
-    SUBL(7) = subl(6) ^ tl;
-    SUBR(7) = subr(6) ^ tr;
-    SUBL(8) = subl(8);
-    SUBR(8) = subr(8);
-    SUBL(9) = subl(9);
-    SUBR(9) = subr(9);
-    tl = subl(7) ^ (subr(7) & ~subr(9));
-    dw = tl & subl(9), tr = subr(7) ^ CAMELLIA_RL1(dw);
-    SUBL(10) = tl ^ subl(11);
-    SUBR(10) = tr ^ subr(11);
-    SUBL(11) = subl(10) ^ subl(12);
-    SUBR(11) = subr(10) ^ subr(12);
-    SUBL(12) = subl(11) ^ subl(13);
-    SUBR(12) = subr(11) ^ subr(13);
-    SUBL(13) = subl(12) ^ subl(14);
-    SUBR(13) = subr(12) ^ subr(14);
-    SUBL(14) = subl(13) ^ subl(15);
-    SUBR(14) = subr(13) ^ subr(15);
-    tl = subl(18) ^ (subr(18) & ~subr(16));
-    dw = tl & subl(16), tr = subr(18) ^ CAMELLIA_RL1(dw);
-    SUBL(15) = subl(14) ^ tl;
-    SUBR(15) = subr(14) ^ tr;
-    SUBL(16) = subl(16);
-    SUBR(16) = subr(16);
-    SUBL(17) = subl(17);
-    SUBR(17) = subr(17);
-    tl = subl(15) ^ (subr(15) & ~subr(17));
-    dw = tl & subl(17), tr = subr(15) ^ CAMELLIA_RL1(dw);
-    SUBL(18) = tl ^ subl(19);
-    SUBR(18) = tr ^ subr(19);
-    SUBL(19) = subl(18) ^ subl(20);
-    SUBR(19) = subr(18) ^ subr(20);
-    SUBL(20) = subl(19) ^ subl(21);
-    SUBR(20) = subr(19) ^ subr(21);
-    SUBL(21) = subl(20) ^ subl(22);
-    SUBR(21) = subr(20) ^ subr(22);
-    SUBL(22) = subl(21) ^ subl(23);
-    SUBR(22) = subr(21) ^ subr(23);
-    SUBL(23) = subl(22);
-    SUBR(23) = subr(22);
-    SUBL(24) = subl(24) ^ subl(23);
-    SUBR(24) = subr(24) ^ subr(23);
-
-    /* apply the inverse of the last half of P-function */
-    dw = SUBL(2) ^ SUBR(2), dw = CAMELLIA_RL8(dw);
-    SUBR(2) = SUBL(2) ^ dw, SUBL(2) = dw;
-    dw = SUBL(3) ^ SUBR(3), dw = CAMELLIA_RL8(dw);
-    SUBR(3) = SUBL(3) ^ dw, SUBL(3) = dw;
-    dw = SUBL(4) ^ SUBR(4), dw = CAMELLIA_RL8(dw);
-    SUBR(4) = SUBL(4) ^ dw, SUBL(4) = dw;
-    dw = SUBL(5) ^ SUBR(5), dw = CAMELLIA_RL8(dw);
-    SUBR(5) = SUBL(5) ^ dw, SUBL(5) = dw;
-    dw = SUBL(6) ^ SUBR(6), dw = CAMELLIA_RL8(dw);
-    SUBR(6) = SUBL(6) ^ dw, SUBL(6) = dw;
-    dw = SUBL(7) ^ SUBR(7), dw = CAMELLIA_RL8(dw);
-    SUBR(7) = SUBL(7) ^ dw, SUBL(7) = dw;
-    dw = SUBL(10) ^ SUBR(10), dw = CAMELLIA_RL8(dw);
-    SUBR(10) = SUBL(10) ^ dw, SUBL(10) = dw;
-    dw = SUBL(11) ^ SUBR(11), dw = CAMELLIA_RL8(dw);
-    SUBR(11) = SUBL(11) ^ dw, SUBL(11) = dw;
-    dw = SUBL(12) ^ SUBR(12), dw = CAMELLIA_RL8(dw);
-    SUBR(12) = SUBL(12) ^ dw, SUBL(12) = dw;
-    dw = SUBL(13) ^ SUBR(13), dw = CAMELLIA_RL8(dw);
-    SUBR(13) = SUBL(13) ^ dw, SUBL(13) = dw;
-    dw = SUBL(14) ^ SUBR(14), dw = CAMELLIA_RL8(dw);
-    SUBR(14) = SUBL(14) ^ dw, SUBL(14) = dw;
-    dw = SUBL(15) ^ SUBR(15), dw = CAMELLIA_RL8(dw);
-    SUBR(15) = SUBL(15) ^ dw, SUBL(15) = dw;
-    dw = SUBL(18) ^ SUBR(18), dw = CAMELLIA_RL8(dw);
-    SUBR(18) = SUBL(18) ^ dw, SUBL(18) = dw;
-    dw = SUBL(19) ^ SUBR(19), dw = CAMELLIA_RL8(dw);
-    SUBR(19) = SUBL(19) ^ dw, SUBL(19) = dw;
-    dw = SUBL(20) ^ SUBR(20), dw = CAMELLIA_RL8(dw);
-    SUBR(20) = SUBL(20) ^ dw, SUBL(20) = dw;
-    dw = SUBL(21) ^ SUBR(21), dw = CAMELLIA_RL8(dw);
-    SUBR(21) = SUBL(21) ^ dw, SUBL(21) = dw;
-    dw = SUBL(22) ^ SUBR(22), dw = CAMELLIA_RL8(dw);
-    SUBR(22) = SUBL(22) ^ dw, SUBL(22) = dw;
-    dw = SUBL(23) ^ SUBR(23), dw = CAMELLIA_RL8(dw);
-    SUBR(23) = SUBL(23) ^ dw, SUBL(23) = dw;
-}
-
-void
-camellia_setup256(const unsigned char *key, uint32_t *subkey)
-{
-    uint32_t kll,klr,krl,krr;           /* left half of key */
-    uint32_t krll,krlr,krrl,krrr;       /* right half of key */
-    uint32_t il, ir, t0, t1, w0, w1;    /* temporary variables */
-    uint32_t kw4l, kw4r, dw, tl, tr;
-    uint32_t subL[34];
-    uint32_t subR[34];
-
-    /*
-     *  key = (kll || klr || krl || krr || krll || krlr || krrl || krrr)
-     *  (|| is concatination)
-     */
-
-    kll  = GETU32(key     );
-    klr  = GETU32(key +  4);
-    krl  = GETU32(key +  8);
-    krr  = GETU32(key + 12);
-    krll = GETU32(key + 16);
-    krlr = GETU32(key + 20);
-    krrl = GETU32(key + 24);
-    krrr = GETU32(key + 28);
-
-    /* generate KL dependent subkeys */
-    subl(0) = kll; subr(0) = klr;
-    subl(1) = krl; subr(1) = krr;
-    CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 45);
-    subl(12) = kll; subr(12) = klr;
-    subl(13) = krl; subr(13) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(16) = kll; subr(16) = klr;
-    subl(17) = krl; subr(17) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
-    subl(22) = kll; subr(22) = klr;
-    subl(23) = krl; subr(23) = krr;
-    CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34);
-    subl(30) = kll; subr(30) = klr;
-    subl(31) = krl; subr(31) = krr;
-
-    /* generate KR dependent subkeys */
-    CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15);
-    subl(4) = krll; subr(4) = krlr;
-    subl(5) = krrl; subr(5) = krrr;
-    CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15);
-    subl(8) = krll; subr(8) = krlr;
-    subl(9) = krrl; subr(9) = krrr;
-    CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
-    subl(18) = krll; subr(18) = krlr;
-    subl(19) = krrl; subr(19) = krrr;
-    CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34);
-    subl(26) = krll; subr(26) = krlr;
-    subl(27) = krrl; subr(27) = krrr;
-    CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34);
-
-    /* generate KA */
-    kll = subl(0) ^ krll; klr = subr(0) ^ krlr;
-    krl = subl(1) ^ krrl; krr = subr(1) ^ krrr;
-    CAMELLIA_F(kll, klr, CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R,
-	       w0, w1, il, ir, t0, t1);
-    krl ^= w0; krr ^= w1;
-    CAMELLIA_F(krl, krr, CAMELLIA_SIGMA2L, CAMELLIA_SIGMA2R,
-	       kll, klr, il, ir, t0, t1);
-    kll ^= krll; klr ^= krlr;
-    CAMELLIA_F(kll, klr, CAMELLIA_SIGMA3L, CAMELLIA_SIGMA3R,
-	       krl, krr, il, ir, t0, t1);
-    krl ^= w0 ^ krrl; krr ^= w1 ^ krrr;
-    CAMELLIA_F(krl, krr, CAMELLIA_SIGMA4L, CAMELLIA_SIGMA4R,
-	       w0, w1, il, ir, t0, t1);
-    kll ^= w0; klr ^= w1;
-
-    /* generate KB */
-    krll ^= kll; krlr ^= klr;
-    krrl ^= krl; krrr ^= krr;
-    CAMELLIA_F(krll, krlr, CAMELLIA_SIGMA5L, CAMELLIA_SIGMA5R,
-	       w0, w1, il, ir, t0, t1);
-    krrl ^= w0; krrr ^= w1;
-    CAMELLIA_F(krrl, krrr, CAMELLIA_SIGMA6L, CAMELLIA_SIGMA6R,
-	       w0, w1, il, ir, t0, t1);
-    krll ^= w0; krlr ^= w1;
-
-    /* generate KA dependent subkeys */
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
-    subl(6) = kll; subr(6) = klr;
-    subl(7) = krl; subr(7) = krr;
-    CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30);
-    subl(14) = kll; subr(14) = klr;
-    subl(15) = krl; subr(15) = krr;
-    subl(24) = klr; subr(24) = krl;
-    subl(25) = krr; subr(25) = kll;
-    CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 49);
-    subl(28) = kll; subr(28) = klr;
-    subl(29) = krl; subr(29) = krr;
-
-    /* generate KB dependent subkeys */
-    subl(2) = krll; subr(2) = krlr;
-    subl(3) = krrl; subr(3) = krrr;
-    CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
-    subl(10) = krll; subr(10) = krlr;
-    subl(11) = krrl; subr(11) = krrr;
-    CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
-    subl(20) = krll; subr(20) = krlr;
-    subl(21) = krrl; subr(21) = krrr;
-    CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 51);
-    subl(32) = krll; subr(32) = krlr;
-    subl(33) = krrl; subr(33) = krrr;
-
-    /* absorb kw2 to other subkeys */
-    subl(3) ^= subl(1); subr(3) ^= subr(1);
-    subl(5) ^= subl(1); subr(5) ^= subr(1);
-    subl(7) ^= subl(1); subr(7) ^= subr(1);
-    subl(1) ^= subr(1) & ~subr(9);
-    dw = subl(1) & subl(9), subr(1) ^= CAMELLIA_RL1(dw);
-    subl(11) ^= subl(1); subr(11) ^= subr(1);
-    subl(13) ^= subl(1); subr(13) ^= subr(1);
-    subl(15) ^= subl(1); subr(15) ^= subr(1);
-    subl(1) ^= subr(1) & ~subr(17);
-    dw = subl(1) & subl(17), subr(1) ^= CAMELLIA_RL1(dw);
-    subl(19) ^= subl(1); subr(19) ^= subr(1);
-    subl(21) ^= subl(1); subr(21) ^= subr(1);
-    subl(23) ^= subl(1); subr(23) ^= subr(1);
-    subl(1) ^= subr(1) & ~subr(25);
-    dw = subl(1) & subl(25), subr(1) ^= CAMELLIA_RL1(dw);
-    subl(27) ^= subl(1); subr(27) ^= subr(1);
-    subl(29) ^= subl(1); subr(29) ^= subr(1);
-    subl(31) ^= subl(1); subr(31) ^= subr(1);
-    subl(32) ^= subl(1); subr(32) ^= subr(1);
-
-
-    /* absorb kw4 to other subkeys */
-    kw4l = subl(33); kw4r = subr(33);
-    subl(30) ^= kw4l; subr(30) ^= kw4r;
-    subl(28) ^= kw4l; subr(28) ^= kw4r;
-    subl(26) ^= kw4l; subr(26) ^= kw4r;
-    kw4l ^= kw4r & ~subr(24);
-    dw = kw4l & subl(24), kw4r ^= CAMELLIA_RL1(dw);
-    subl(22) ^= kw4l; subr(22) ^= kw4r;
-    subl(20) ^= kw4l; subr(20) ^= kw4r;
-    subl(18) ^= kw4l; subr(18) ^= kw4r;
-    kw4l ^= kw4r & ~subr(16);
-    dw = kw4l & subl(16), kw4r ^= CAMELLIA_RL1(dw);
-    subl(14) ^= kw4l; subr(14) ^= kw4r;
-    subl(12) ^= kw4l; subr(12) ^= kw4r;
-    subl(10) ^= kw4l; subr(10) ^= kw4r;
-    kw4l ^= kw4r & ~subr(8);
-    dw = kw4l & subl(8), kw4r ^= CAMELLIA_RL1(dw);
-    subl(6) ^= kw4l; subr(6) ^= kw4r;
-    subl(4) ^= kw4l; subr(4) ^= kw4r;
-    subl(2) ^= kw4l; subr(2) ^= kw4r;
-    subl(0) ^= kw4l; subr(0) ^= kw4r;
-
-    /* key XOR is end of F-function */
-    SUBL(0) = subl(0) ^ subl(2);
-    SUBR(0) = subr(0) ^ subr(2);
-    SUBL(2) = subl(3);
-    SUBR(2) = subr(3);
-    SUBL(3) = subl(2) ^ subl(4);
-    SUBR(3) = subr(2) ^ subr(4);
-    SUBL(4) = subl(3) ^ subl(5);
-    SUBR(4) = subr(3) ^ subr(5);
-    SUBL(5) = subl(4) ^ subl(6);
-    SUBR(5) = subr(4) ^ subr(6);
-    SUBL(6) = subl(5) ^ subl(7);
-    SUBR(6) = subr(5) ^ subr(7);
-    tl = subl(10) ^ (subr(10) & ~subr(8));
-    dw = tl & subl(8), tr = subr(10) ^ CAMELLIA_RL1(dw);
-    SUBL(7) = subl(6) ^ tl;
-    SUBR(7) = subr(6) ^ tr;
-    SUBL(8) = subl(8);
-    SUBR(8) = subr(8);
-    SUBL(9) = subl(9);
-    SUBR(9) = subr(9);
-    tl = subl(7) ^ (subr(7) & ~subr(9));
-    dw = tl & subl(9), tr = subr(7) ^ CAMELLIA_RL1(dw);
-    SUBL(10) = tl ^ subl(11);
-    SUBR(10) = tr ^ subr(11);
-    SUBL(11) = subl(10) ^ subl(12);
-    SUBR(11) = subr(10) ^ subr(12);
-    SUBL(12) = subl(11) ^ subl(13);
-    SUBR(12) = subr(11) ^ subr(13);
-    SUBL(13) = subl(12) ^ subl(14);
-    SUBR(13) = subr(12) ^ subr(14);
-    SUBL(14) = subl(13) ^ subl(15);
-    SUBR(14) = subr(13) ^ subr(15);
-    tl = subl(18) ^ (subr(18) & ~subr(16));
-    dw = tl & subl(16), tr = subr(18) ^ CAMELLIA_RL1(dw);
-    SUBL(15) = subl(14) ^ tl;
-    SUBR(15) = subr(14) ^ tr;
-    SUBL(16) = subl(16);
-    SUBR(16) = subr(16);
-    SUBL(17) = subl(17);
-    SUBR(17) = subr(17);
-    tl = subl(15) ^ (subr(15) & ~subr(17));
-    dw = tl & subl(17), tr = subr(15) ^ CAMELLIA_RL1(dw);
-    SUBL(18) = tl ^ subl(19);
-    SUBR(18) = tr ^ subr(19);
-    SUBL(19) = subl(18) ^ subl(20);
-    SUBR(19) = subr(18) ^ subr(20);
-    SUBL(20) = subl(19) ^ subl(21);
-    SUBR(20) = subr(19) ^ subr(21);
-    SUBL(21) = subl(20) ^ subl(22);
-    SUBR(21) = subr(20) ^ subr(22);
-    SUBL(22) = subl(21) ^ subl(23);
-    SUBR(22) = subr(21) ^ subr(23);
-    tl = subl(26) ^ (subr(26) & ~subr(24));
-    dw = tl & subl(24), tr = subr(26) ^ CAMELLIA_RL1(dw);
-    SUBL(23) = subl(22) ^ tl;
-    SUBR(23) = subr(22) ^ tr;
-    SUBL(24) = subl(24);
-    SUBR(24) = subr(24);
-    SUBL(25) = subl(25);
-    SUBR(25) = subr(25);
-    tl = subl(23) ^ (subr(23) & ~subr(25));
-    dw = tl & subl(25), tr = subr(23) ^ CAMELLIA_RL1(dw);
-    SUBL(26) = tl ^ subl(27);
-    SUBR(26) = tr ^ subr(27);
-    SUBL(27) = subl(26) ^ subl(28);
-    SUBR(27) = subr(26) ^ subr(28);
-    SUBL(28) = subl(27) ^ subl(29);
-    SUBR(28) = subr(27) ^ subr(29);
-    SUBL(29) = subl(28) ^ subl(30);
-    SUBR(29) = subr(28) ^ subr(30);
-    SUBL(30) = subl(29) ^ subl(31);
-    SUBR(30) = subr(29) ^ subr(31);
-    SUBL(31) = subl(30);
-    SUBR(31) = subr(30);
-    SUBL(32) = subl(32) ^ subl(31);
-    SUBR(32) = subr(32) ^ subr(31);
-
-    /* apply the inverse of the last half of P-function */
-    dw = SUBL(2) ^ SUBR(2), dw = CAMELLIA_RL8(dw);
-    SUBR(2) = SUBL(2) ^ dw, SUBL(2) = dw;
-    dw = SUBL(3) ^ SUBR(3), dw = CAMELLIA_RL8(dw);
-    SUBR(3) = SUBL(3) ^ dw, SUBL(3) = dw;
-    dw = SUBL(4) ^ SUBR(4), dw = CAMELLIA_RL8(dw);
-    SUBR(4) = SUBL(4) ^ dw, SUBL(4) = dw;
-    dw = SUBL(5) ^ SUBR(5), dw = CAMELLIA_RL8(dw);
-    SUBR(5) = SUBL(5) ^ dw, SUBL(5) = dw;
-    dw = SUBL(6) ^ SUBR(6), dw = CAMELLIA_RL8(dw);
-    SUBR(6) = SUBL(6) ^ dw, SUBL(6) = dw;
-    dw = SUBL(7) ^ SUBR(7), dw = CAMELLIA_RL8(dw);
-    SUBR(7) = SUBL(7) ^ dw, SUBL(7) = dw;
-    dw = SUBL(10) ^ SUBR(10), dw = CAMELLIA_RL8(dw);
-    SUBR(10) = SUBL(10) ^ dw, SUBL(10) = dw;
-    dw = SUBL(11) ^ SUBR(11), dw = CAMELLIA_RL8(dw);
-    SUBR(11) = SUBL(11) ^ dw, SUBL(11) = dw;
-    dw = SUBL(12) ^ SUBR(12), dw = CAMELLIA_RL8(dw);
-    SUBR(12) = SUBL(12) ^ dw, SUBL(12) = dw;
-    dw = SUBL(13) ^ SUBR(13), dw = CAMELLIA_RL8(dw);
-    SUBR(13) = SUBL(13) ^ dw, SUBL(13) = dw;
-    dw = SUBL(14) ^ SUBR(14), dw = CAMELLIA_RL8(dw);
-    SUBR(14) = SUBL(14) ^ dw, SUBL(14) = dw;
-    dw = SUBL(15) ^ SUBR(15), dw = CAMELLIA_RL8(dw);
-    SUBR(15) = SUBL(15) ^ dw, SUBL(15) = dw;
-    dw = SUBL(18) ^ SUBR(18), dw = CAMELLIA_RL8(dw);
-    SUBR(18) = SUBL(18) ^ dw, SUBL(18) = dw;
-    dw = SUBL(19) ^ SUBR(19), dw = CAMELLIA_RL8(dw);
-    SUBR(19) = SUBL(19) ^ dw, SUBL(19) = dw;
-    dw = SUBL(20) ^ SUBR(20), dw = CAMELLIA_RL8(dw);
-    SUBR(20) = SUBL(20) ^ dw, SUBL(20) = dw;
-    dw = SUBL(21) ^ SUBR(21), dw = CAMELLIA_RL8(dw);
-    SUBR(21) = SUBL(21) ^ dw, SUBL(21) = dw;
-    dw = SUBL(22) ^ SUBR(22), dw = CAMELLIA_RL8(dw);
-    SUBR(22) = SUBL(22) ^ dw, SUBL(22) = dw;
-    dw = SUBL(23) ^ SUBR(23), dw = CAMELLIA_RL8(dw);
-    SUBR(23) = SUBL(23) ^ dw, SUBL(23) = dw;
-    dw = SUBL(26) ^ SUBR(26), dw = CAMELLIA_RL8(dw);
-    SUBR(26) = SUBL(26) ^ dw, SUBL(26) = dw;
-    dw = SUBL(27) ^ SUBR(27), dw = CAMELLIA_RL8(dw);
-    SUBR(27) = SUBL(27) ^ dw, SUBL(27) = dw;
-    dw = SUBL(28) ^ SUBR(28), dw = CAMELLIA_RL8(dw);
-    SUBR(28) = SUBL(28) ^ dw, SUBL(28) = dw;
-    dw = SUBL(29) ^ SUBR(29), dw = CAMELLIA_RL8(dw);
-    SUBR(29) = SUBL(29) ^ dw, SUBL(29) = dw;
-    dw = SUBL(30) ^ SUBR(30), dw = CAMELLIA_RL8(dw);
-    SUBR(30) = SUBL(30) ^ dw, SUBL(30) = dw;
-    dw = SUBL(31) ^ SUBR(31), dw = CAMELLIA_RL8(dw);
-    SUBR(31) = SUBL(31) ^ dw, SUBL(31) = dw;
-}
-
-void
-camellia_setup192(const unsigned char *key, uint32_t *subkey)
-{
-    unsigned char kk[32];
-    uint32_t krll, krlr, krrl,krrr;
-
-    memcpy(kk, key, 24);
-    memcpy((unsigned char *)&krll, key+16,4);
-    memcpy((unsigned char *)&krlr, key+20,4);
-    krrl = ~krll;
-    krrr = ~krlr;
-    memcpy(kk+24, (unsigned char *)&krrl, 4);
-    memcpy(kk+28, (unsigned char *)&krrr, 4);
-    camellia_setup256(kk, subkey);
-}
-
-
-/**
- * Stuff related to camellia encryption/decryption
- */
-void
-camellia_encrypt128(const uint32_t *subkey, uint32_t *io)
-{
-    uint32_t il, ir, t0, t1;
-
-    /* pre whitening but absorb kw2*/
-    io[0] ^= SUBL(0);
-    io[1] ^= SUBR(0);
-    /* main iteration */
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(2),SUBR(2),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(3),SUBR(3),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(4),SUBR(4),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(5),SUBR(5),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(6),SUBR(6),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(7),SUBR(7),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(8),SUBR(8), SUBL(9),SUBR(9),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(10),SUBR(10),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(11),SUBR(11),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(12),SUBR(12),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(13),SUBR(13),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(14),SUBR(14),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(15),SUBR(15),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(16), SUBR(16), SUBL(17),SUBR(17),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(18),SUBR(18),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(19),SUBR(19),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(20),SUBR(20),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(21),SUBR(21),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(22),SUBR(22),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(23),SUBR(23),
-		     io[0],io[1],il,ir,t0,t1);
-
-    /* post whitening but kw4 */
-    io[2] ^= SUBL(24);
-    io[3] ^= SUBR(24);
-
-    t0 = io[0];
-    t1 = io[1];
-    io[0] = io[2];
-    io[1] = io[3];
-    io[2] = t0;
-    io[3] = t1;
-}
-
-void
-camellia_decrypt128(const uint32_t *subkey, uint32_t *io)
-{
-    uint32_t il,ir,t0,t1;               /* temporary valiables */
-
-    /* pre whitening but absorb kw2*/
-    io[0] ^= SUBL(24);
-    io[1] ^= SUBR(24);
-
-    /* main iteration */
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(23),SUBR(23),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(22),SUBR(22),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(21),SUBR(21),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(20),SUBR(20),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(19),SUBR(19),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(18),SUBR(18),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3],SUBL(17),SUBR(17),SUBL(16),SUBR(16),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(15),SUBR(15),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(14),SUBR(14),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(13),SUBR(13),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(12),SUBR(12),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(11),SUBR(11),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(10),SUBR(10),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(9),SUBR(9), SUBL(8),SUBR(8),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(7),SUBR(7),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(6),SUBR(6),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(5),SUBR(5),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(4),SUBR(4),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(3),SUBR(3),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(2),SUBR(2),
-		     io[0],io[1],il,ir,t0,t1);
-
-    /* post whitening but kw4 */
-    io[2] ^= SUBL(0);
-    io[3] ^= SUBR(0);
-
-    t0 = io[0];
-    t1 = io[1];
-    io[0] = io[2];
-    io[1] = io[3];
-    io[2] = t0;
-    io[3] = t1;
-}
-
-/**
- * stuff for 192 and 256bit encryption/decryption
- */
-void
-camellia_encrypt256(const uint32_t *subkey, uint32_t *io)
-{
-    uint32_t il,ir,t0,t1;           /* temporary valiables */
-
-    /* pre whitening but absorb kw2*/
-    io[0] ^= SUBL(0);
-    io[1] ^= SUBR(0);
-
-    /* main iteration */
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(2),SUBR(2),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(3),SUBR(3),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(4),SUBR(4),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(5),SUBR(5),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(6),SUBR(6),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(7),SUBR(7),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(8),SUBR(8), SUBL(9),SUBR(9),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(10),SUBR(10),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(11),SUBR(11),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(12),SUBR(12),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(13),SUBR(13),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(14),SUBR(14),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(15),SUBR(15),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(16),SUBR(16), SUBL(17),SUBR(17),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(18),SUBR(18),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(19),SUBR(19),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(20),SUBR(20),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(21),SUBR(21),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(22),SUBR(22),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(23),SUBR(23),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(24),SUBR(24), SUBL(25),SUBR(25),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(26),SUBR(26),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(27),SUBR(27),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(28),SUBR(28),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(29),SUBR(29),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(30),SUBR(30),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(31),SUBR(31),
-		     io[0],io[1],il,ir,t0,t1);
-
-    /* post whitening but kw4 */
-    io[2] ^= SUBL(32);
-    io[3] ^= SUBR(32);
-
-    t0 = io[0];
-    t1 = io[1];
-    io[0] = io[2];
-    io[1] = io[3];
-    io[2] = t0;
-    io[3] = t1;
-}
-
-void
-camellia_decrypt256(const uint32_t *subkey, uint32_t *io)
-{
-    uint32_t il,ir,t0,t1;           /* temporary valiables */
-
-    /* pre whitening but absorb kw2*/
-    io[0] ^= SUBL(32);
-    io[1] ^= SUBR(32);
-
-    /* main iteration */
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(31),SUBR(31),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(30),SUBR(30),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(29),SUBR(29),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(28),SUBR(28),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(27),SUBR(27),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(26),SUBR(26),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(25),SUBR(25), SUBL(24),SUBR(24),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(23),SUBR(23),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(22),SUBR(22),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(21),SUBR(21),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(20),SUBR(20),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(19),SUBR(19),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(18),SUBR(18),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(17),SUBR(17), SUBL(16),SUBR(16),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(15),SUBR(15),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(14),SUBR(14),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(13),SUBR(13),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(12),SUBR(12),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(11),SUBR(11),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(10),SUBR(10),
-		     io[0],io[1],il,ir,t0,t1);
-
-    CAMELLIA_FLS(io[0],io[1],io[2],io[3], SUBL(9),SUBR(9), SUBL(8),SUBR(8),
-		 t0,t1,il,ir);
-
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(7),SUBR(7),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(6),SUBR(6),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(5),SUBR(5),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(4),SUBR(4),
-		     io[0],io[1],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[0],io[1], SUBL(3),SUBR(3),
-		     io[2],io[3],il,ir,t0,t1);
-    CAMELLIA_ROUNDSM(io[2],io[3], SUBL(2),SUBR(2),
-		     io[0],io[1],il,ir,t0,t1);
-
-    /* post whitening but kw4 */
-    io[2] ^= SUBL(0);
-    io[3] ^= SUBR(0);
-
-    t0 = io[0];
-    t1 = io[1];
-    io[0] = io[2];
-    io[1] = io[3];
-    io[2] = t0;
-    io[3] = t1;
-}
-
-void
-Camellia_Ekeygen(const int keyBitLength,
-		 const unsigned char *rawKey,
-		 uint32_t *subkey)
-{
-    KASSERT(keyBitLength == 128 || keyBitLength == 192 || keyBitLength == 256,
-	    ("Invalid key size (%d).", keyBitLength));
-
-    switch(keyBitLength) {
-    case 128:
-	camellia_setup128(rawKey, subkey);
-	break;
-    case 192:
-	camellia_setup192(rawKey, subkey);
-	break;
-    case 256:
-	camellia_setup256(rawKey, subkey);
-	break;
-    default:
-	break;
-    }
-}
-void
-Camellia_EncryptBlock(const int keyBitLength,
-		      const unsigned char *plaintext,
-		      const uint32_t *subkey,
-		      unsigned char *ciphertext)
-{
-    uint32_t tmp[4];
-
-    tmp[0] = GETU32(plaintext);
-    tmp[1] = GETU32(plaintext + 4);
-    tmp[2] = GETU32(plaintext + 8);
-    tmp[3] = GETU32(plaintext + 12);
-
-    switch (keyBitLength) {
-    case 128:
-	camellia_encrypt128(subkey, tmp);
-	break;
-    case 192:
-	/* fall through */
-    case 256:
-	camellia_encrypt256(subkey, tmp);
-	break;
-    default:
-	break;
-    }
-
-    PUTU32(ciphertext,    tmp[0]);
-    PUTU32(ciphertext+4,  tmp[1]);
-    PUTU32(ciphertext+8,  tmp[2]);
-    PUTU32(ciphertext+12, tmp[3]);
-}
-
-void
-Camellia_DecryptBlock(const int keyBitLength,
-		      const unsigned char *ciphertext,
-		      const uint32_t *subkey,
-		      unsigned char *plaintext)
-{
-    uint32_t tmp[4];
-
-    tmp[0] = GETU32(ciphertext);
-    tmp[1] = GETU32(ciphertext + 4);
-    tmp[2] = GETU32(ciphertext + 8);
-    tmp[3] = GETU32(ciphertext + 12);
-
-    switch (keyBitLength) {
-    case 128:
-	camellia_decrypt128(subkey, tmp);
-	break;
-    case 192:
-	/* fall through */
-    case 256:
-	camellia_decrypt256(subkey, tmp);
-	break;
-    default:
-	break;
-    }
-
-    PUTU32(plaintext,    tmp[0]);
-    PUTU32(plaintext+4,  tmp[1]);
-    PUTU32(plaintext+8,  tmp[2]);
-    PUTU32(plaintext+12, tmp[3]);
-}
diff --git a/sys/crypto/camellia/camellia.h b/sys/crypto/camellia/camellia.h
deleted file mode 100644
index 58e56868f6..0000000000
--- a/sys/crypto/camellia/camellia.h
+++ /dev/null
@@ -1,69 +0,0 @@
-/* camellia.h ver 1.1.0
- *
- * Copyright (c) 2006
- * NTT (Nippon Telegraph and Telephone Corporation) . 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 as
- *   the first lines of this file unmodified.
- * 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 NTT ``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 NTT 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.
- *
- * $FreeBSD: src/sys/crypto/camellia/camellia.h,v 1.1 2007/05/09 19:37:01 gnn Exp $
- */
-
-#ifndef _CAMELLIA_H
-#define _CAMELLIA_H
-
-#define CAMELLIA_BLOCK_SIZE 16
-#define CAMELLIA_SUBKEYWORD 68 /* (34*8/4) */
-
-typedef struct {
-    int	bits;				      /* key-length */
-    uint32_t subkey[CAMELLIA_SUBKEYWORD]; /* encrypt/decrypt key schedule */
-} camellia_ctx;
-
-void camellia_set_key(camellia_ctx *, const u_char *, int);
-void camellia_decrypt(const camellia_ctx *, const u_char *, u_char *);
-void camellia_encrypt(const camellia_ctx *, const u_char *, u_char *);
-
-
-void Camellia_Ekeygen(const int keyBitLength,
-		      const unsigned char *rawKey,
-		      uint32_t *subkey);
-
-void Camellia_EncryptBlock(const int keyBitLength,
-			   const unsigned char *plaintext,
-			   const uint32_t *subkey,
-			   unsigned char *cipherText);
-
-void Camellia_DecryptBlock(const int keyBitLength,
-			   const unsigned char *cipherText,
-			   const uint32_t *subkey,
-			   unsigned char *plaintext);
-
-void camellia_setup128(const unsigned char *key, uint32_t *subkey);
-void camellia_setup192(const unsigned char *key, uint32_t *subkey);
-void camellia_setup256(const unsigned char *key, uint32_t *subkey);
-void camellia_encrypt128(const uint32_t *subkey, uint32_t *io);
-void camellia_encrypt256(const uint32_t *subkey, uint32_t *io);
-void camellia_decrypt128(const uint32_t *subkey, uint32_t *io);
-void camellia_decrypt256(const uint32_t *subkey, uint32_t *io);
-
-
-#endif /* _CAMELLIA_H */
diff --git a/sys/crypto/crypto_cipher.c b/sys/crypto/crypto_cipher.c
new file mode 100644
index 0000000000..d75f8a5f06
--- /dev/null
+++ b/sys/crypto/crypto_cipher.c
@@ -0,0 +1,389 @@
+/*-
+ * The authors of this code are John Ioannidis (ji@tla.org),
+ * Angelos D. Keromytis (kermit@csd.uch.gr) and
+ * Niels Provos (provos@physnet.uni-hamburg.de).
+ *
+ * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
+ * in November 1995.
+ *
+ * Ported to OpenBSD and NetBSD, with additional transforms, in December
+ * 1996, by Angelos D. Keromytis.
+ *
+ * Additional transforms and features in 1997 and 1998 by Angelos D.
+ * Keromytis and Niels Provos.
+ *
+ * Additional features in 1999 by Angelos D. Keromytis.
+ *
+ * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
+ * Angelos D. Keromytis and Niels Provos.
+ *
+ * Copyright (C) 2001, Angelos D. Keromytis.
+ *
+ * Copyright (c) 2024, Michael Neumann.
+ *
+ * Permission to use, copy, and modify this software with or without fee
+ * is hereby granted, provided that this entire notice is included in
+ * all copies of any software which is or includes a copy or
+ * modification of this software.
+ * You may use this code under the GNU public license if you so wish.
+ * Please contribute changes back to the authors under this freer than
+ * GPL license so that we may further the use of strong encryption
+ * without limitations to all.
+ *
+ * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
+ * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
+ * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
+ * PURPOSE.
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+
+#include <crypto/crypto_cipher.h>
+
+/**
+ * --------------------------------------
+ *  Utility
+ * --------------------------------------
+ */
+
+static inline void
+xor_block(uint8_t *dst, const uint8_t *src, int blocksize)
+{
+	for (int i = 0; i < blocksize; i++)
+		dst[i] ^= src[i];
+}
+
+typedef void (*block_fn_t)(const void *ctx, uint8_t *data, uint8_t *data2);
+
+/*
+ * XOR with the IV/previous block, as appropriate.
+ */
+#define ENCRYPT_DATA_CBC(block_fn, ctx, data, datalen, blocksize, iv)       \
+	for (int i = 0; i < datalen; i += blocksize) {                      \
+		xor_block(data + i, (i == 0) ? iv : (data + i - blocksize), \
+		    blocksize);                                             \
+		block_fn(ctx, data + i, data + i);                          \
+	}
+
+/*
+ * Start at the end, so we don't need to keep the
+ * encrypted block as the IV for the next block.
+ *
+ * XOR with the IV/previous block, as appropriate
+ */
+#define DECRYPT_DATA_CBC(block_fn, ctx, data, datalen, blocksize, iv)       \
+	for (int i = datalen - blocksize; i >= 0; i -= blocksize) {         \
+		block_fn(ctx, data + i, data + i);                          \
+		xor_block(data + i, (i == 0) ? iv : (data + i - blocksize), \
+		    blocksize);                                             \
+	}
+
+/**
+ * --------------------------------------
+ * Cipher null
+ * --------------------------------------
+ */
+
+static int
+cipher_null_probe(const char *algo_name, const char *mode_name __unused,
+    int keysize_in_bits __unused)
+{
+	if (strcmp(algo_name, "null") == 0)
+		return (0);
+	return (-1);
+}
+
+static int
+cipher_null_setkey(struct crypto_cipher_context *ctx __unused,
+    const uint8_t *keydata __unused, int keylen_in_bytes __unused)
+{
+	return (0);
+}
+
+static int
+cipher_null_encrypt(const struct crypto_cipher_context *ctx __unused,
+    uint8_t *data __unused, int datalen __unused,
+    struct crypto_cipher_iv *iv __unused)
+{
+	return (0);
+}
+
+static int
+cipher_null_decrypt(const struct crypto_cipher_context *ctx __unused,
+    uint8_t *data, int datalen, struct crypto_cipher_iv *iv __unused)
+{
+	return (0);
+}
+
+const struct crypto_cipher cipher_null = {
+	"null",
+	"null",
+	4,
+	0,
+	0,
+	cipher_null_probe,
+	cipher_null_setkey,
+	cipher_null_encrypt,
+	cipher_null_decrypt,
+};
+
+/**
+ * --------------------------------------
+ * AES-CBC (Rijndael-128)
+ * --------------------------------------
+ */
+
+#define AES_BLOCK_LEN 16
+
+static int
+aes_cbc_probe(const char *algo_name, const char *mode_name, int keysize_in_bits)
+{
+	if ((strcmp(algo_name, "aes") == 0) &&
+	    (strcmp(mode_name, "cbc") == 0) &&
+	    (keysize_in_bits == 128 || keysize_in_bits == 192 ||
+		keysize_in_bits == 256))
+		return (0);
+	else
+		return (-1);
+}
+
+static int
+aes_cbc_setkey(struct crypto_cipher_context *ctx, const uint8_t *keydata,
+    int keylen_in_bytes)
+{
+	switch (keylen_in_bytes * 8) {
+	case 128:
+	case 192:
+	case 256:
+		rijndael_set_key((void *)ctx, keydata, keylen_in_bytes * 8);
+		return (0);
+
+	default:
+		return (EINVAL);
+	}
+}
+
+static int
+aes_cbc_encrypt(const struct crypto_cipher_context *ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *iv)
+{
+	if ((datalen % AES_BLOCK_LEN) != 0)
+		return EINVAL;
+
+	ENCRYPT_DATA_CBC(rijndael_encrypt, (const void *)ctx, data, datalen,
+	    AES_BLOCK_LEN, (uint8_t *)iv);
+
+	return (0);
+}
+
+static int
+aes_cbc_decrypt(const struct crypto_cipher_context *ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *iv)
+{
+	if ((datalen % AES_BLOCK_LEN) != 0)
+		return EINVAL;
+
+	DECRYPT_DATA_CBC(rijndael_decrypt, (const void *)ctx, data, datalen,
+	    AES_BLOCK_LEN, (uint8_t *)iv);
+
+	return (0);
+}
+
+const struct crypto_cipher cipher_aes_cbc = {
+	"aes-cbc",
+	"AES-CBC (Rijndael-128) in software",
+	AES_BLOCK_LEN,
+	AES_BLOCK_LEN,
+	sizeof(rijndael_ctx),
+	aes_cbc_probe,
+	aes_cbc_setkey,
+	aes_cbc_encrypt,
+	aes_cbc_decrypt,
+};
+
+/**
+ * --------------------------------------
+ * AES-XTS
+ * --------------------------------------
+ */
+
+#define AES_XTS_BLOCK_LEN 16
+#define AES_XTS_IV_LEN	  8
+#define AES_XTS_ALPHA	  0x87 /* GF(2^128) generator polynomial */
+
+static void
+aes_xts_crypt_block(const struct aes_xts_ctx *ctx, uint8_t *data, uint8_t *iv,
+    bool do_encrypt)
+{
+	uint8_t block[AES_XTS_BLOCK_LEN];
+	u_int i, carry_in, carry_out;
+
+	for (i = 0; i < AES_XTS_BLOCK_LEN; i++)
+		block[i] = data[i] ^ iv[i];
+
+	if (do_encrypt)
+		rijndael_encrypt(&ctx->key1, block, data);
+	else
+		rijndael_decrypt(&ctx->key1, block, data);
+
+	for (i = 0; i < AES_XTS_BLOCK_LEN; i++)
+		data[i] ^= iv[i];
+
+	/* Exponentiate tweak */
+	carry_in = 0;
+	for (i = 0; i < AES_XTS_BLOCK_LEN; i++) {
+		carry_out = iv[i] & 0x80;
+		iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
+		carry_in = carry_out;
+	}
+	if (carry_in)
+		iv[0] ^= AES_XTS_ALPHA;
+	explicit_bzero(block, sizeof(block));
+}
+
+static bool
+aes_xts_valid_keysize_in_bits(int keysize_in_bits)
+{
+	switch (keysize_in_bits) {
+	case 256:
+	case 512:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static int
+aes_xts_probe(const char *algo_name, const char *mode_name, int keysize_in_bits)
+{
+	if (strcmp(algo_name, "aes") != 0)
+		return (-1);
+
+	if (strcmp(mode_name, "xts") != 0)
+		return (-1);
+
+	if (aes_xts_valid_keysize_in_bits(keysize_in_bits))
+		return (0);
+
+	return (-1);
+}
+
+static int
+aes_xts_setkey(struct crypto_cipher_context *ctx, const uint8_t *keydata,
+    int keylen_in_bytes)
+{
+	if (!aes_xts_valid_keysize_in_bits(keylen_in_bytes * 8))
+		return (EINVAL);
+
+	rijndael_set_key(&ctx->_ctx._aes_xts.key1, keydata,
+	    (keylen_in_bytes / 2) * 8);
+	rijndael_set_key(&ctx->_ctx._aes_xts.key2,
+	    keydata + (keylen_in_bytes / 2), (keylen_in_bytes / 2) * 8);
+
+	return (0);
+}
+
+static void
+aes_xts_reinit(const struct aes_xts_ctx *ctx, u_int8_t *iv)
+{
+#if 0
+	/*
+	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
+	 * of a 64-bit block number which we allow to be passed in directly.
+	 */
+	/* XXX: possibly use htole64? */
+#endif
+	/* Last 64 bits of IV are always zero */
+	bzero(iv + AES_XTS_IV_LEN, AES_XTS_IV_LEN);
+
+	rijndael_encrypt(&ctx->key2, iv, iv);
+}
+
+static int
+aes_xts_encrypt(const struct crypto_cipher_context *_ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *_iv)
+{
+	uint8_t *iv = _iv->_iv._aes_xts;
+	const struct aes_xts_ctx *ctx = &_ctx->_ctx._aes_xts;
+
+	if ((datalen % AES_XTS_BLOCK_LEN) != 0)
+		return EINVAL;
+
+	aes_xts_reinit(ctx, iv);
+	for (int i = 0; i < datalen; i += AES_XTS_BLOCK_LEN) {
+		aes_xts_crypt_block(ctx, data + i, iv, true);
+	}
+
+	return (0);
+}
+
+static int
+aes_xts_decrypt(const struct crypto_cipher_context *_ctx, uint8_t *data,
+    int datalen, struct crypto_cipher_iv *_iv)
+{
+	uint8_t *iv = _iv->_iv._aes_xts;
+	const struct aes_xts_ctx *ctx = &_ctx->_ctx._aes_xts;
+
+	if ((datalen % AES_XTS_BLOCK_LEN) != 0)
+		return EINVAL;
+
+	aes_xts_reinit(ctx, iv);
+	for (int i = 0; i < datalen; i += AES_XTS_BLOCK_LEN) {
+		aes_xts_crypt_block(ctx, data + i, iv, false);
+	}
+
+	return (0);
+}
+
+const struct crypto_cipher cipher_aes_xts = {
+	"aes-xts",
+	"AES-XTS (in software)",
+	AES_XTS_BLOCK_LEN,
+	16,
+	sizeof(struct aes_xts_ctx),
+	aes_xts_probe,
+	aes_xts_setkey,
+	aes_xts_encrypt,
+	aes_xts_decrypt,
+};
+
+/**
+ *
+ */
+
+const struct crypto_cipher *crypto_ciphers[6] = {
+
+	&cipher_null,
+
+	/* first probe AESNI, then fallback to software AES */
+	&cipher_aesni_cbc, &cipher_aesni_xts,
+
+	/* AES in software */
+	&cipher_aes_cbc, &cipher_aes_xts,
+
+	NULL
+};
+
+/**
+ * --------------------------------------
+ * --------------------------------------
+ */
+
+const struct crypto_cipher *
+crypto_cipher_find(const char *algo_name, const char *mode_name,
+    int keysize_in_bits)
+{
+	for (const struct crypto_cipher **cipherpp = crypto_ciphers; *cipherpp;
+	     ++cipherpp) {
+		const struct crypto_cipher *cipherp = *cipherpp;
+		if ((*cipherp->probe)(algo_name, mode_name, keysize_in_bits) ==
+		    0) {
+			return cipherp;
+		}
+	}
+
+	return NULL;
+}
diff --git a/sys/crypto/crypto_cipher.h b/sys/crypto/crypto_cipher.h
new file mode 100644
index 0000000000..0470b972f7
--- /dev/null
+++ b/sys/crypto/crypto_cipher.h
@@ -0,0 +1,54 @@
+#ifndef _CRYPTO_CIPHER_H_
+#define _CRYPTO_CIPHER_H_
+
+#include <crypto/aesni/aesni.h>
+#include <crypto/rijndael/rijndael.h>
+
+struct aes_xts_ctx {
+	rijndael_ctx key1;
+	rijndael_ctx key2;
+};
+
+struct crypto_cipher_context {
+	union {
+		rijndael_ctx _rijndael;
+		aesni_ctx _aesni;
+		struct aes_xts_ctx _aes_xts;
+	} _ctx;
+};
+
+struct crypto_cipher_iv {
+	union {
+		uint8_t _rijndael[16];
+		aesni_iv _aesni;
+		uint8_t _aes_xts[16]; /* 16 bytes are used, but the last 8 bytes
+					 are zero */
+	} _iv;
+};
+
+typedef int (*crypto_cipher_blockfn_t)(const struct crypto_cipher_context *ctx,
+    uint8_t *data, int datalen, struct crypto_cipher_iv *iv);
+
+typedef int (*crypto_cipher_probe_t)(const char *algo_name,
+    const char *mode_name, int keysize_in_bits);
+
+typedef int (*crypto_cipher_setkey_t)(struct crypto_cipher_context *ctx,
+    const uint8_t *keydata, int keylen_in_bytes);
+
+struct crypto_cipher {
+	const char *shortname;
+	const char *description;
+	uint16_t blocksize;
+	uint16_t ivsize;
+	uint16_t ctxsize;
+
+	crypto_cipher_probe_t probe;
+	crypto_cipher_setkey_t setkey;
+	crypto_cipher_blockfn_t encrypt;
+	crypto_cipher_blockfn_t decrypt;
+};
+
+const struct crypto_cipher *crypto_cipher_find(const char *algo_name,
+    const char *mode_name, int keysize_in_bits);
+
+#endif
diff --git a/sys/crypto/crypto_mod.c b/sys/crypto/crypto_mod.c
new file mode 100644
index 0000000000..bd04980c47
--- /dev/null
+++ b/sys/crypto/crypto_mod.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2024 Michael Neumann <mneumann@ntecs.de>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all
+ * copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * Crypto module stub for dynamic and static linking.
+ *
+ * The OpenBSD Crypto Framework (OCF) is gone. This is a much simpler
+ * approach.
+ */
+
+#include <sys/module.h>
+
+int crypto_modevent(module_t mod, int type, void *unused);
+
+int
+crypto_modevent(module_t mod, int type, void *unused)
+{
+	switch (type) {
+	case MOD_LOAD:
+		break;
+	case MOD_UNLOAD:
+		break;
+	}
+	return (0);
+}
+
+MODULE_VERSION(crypto, 2);
diff --git a/sys/opencrypto/rmd160.c b/sys/crypto/rmd160/rmd160.c
similarity index 99%
rename from sys/opencrypto/rmd160.c
rename to sys/crypto/rmd160/rmd160.c
index 420723d209..cc8191bd4b 100644
--- a/sys/opencrypto/rmd160.c
+++ b/sys/crypto/rmd160/rmd160.c
@@ -33,7 +33,7 @@
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/endian.h>
-#include <opencrypto/rmd160.h>
+#include <crypto/rmd160/rmd160.h>
 
 #define PUT_64BIT_LE(cp, value) do { \
 	(cp)[7] = (value) >> 56; \
diff --git a/sys/opencrypto/rmd160.h b/sys/crypto/rmd160/rmd160.h
similarity index 100%
rename from sys/opencrypto/rmd160.h
rename to sys/crypto/rmd160/rmd160.h
diff --git a/sys/crypto/serpent/serpent.c b/sys/crypto/serpent/serpent.c
deleted file mode 100644
index c24b06c2bc..0000000000
--- a/sys/crypto/serpent/serpent.c
+++ /dev/null
@@ -1,631 +0,0 @@
-
-// Copyright in this code is held by Dr B.R. Gladman but free direct or
-// derivative use is permitted subject to acknowledgement of its origin
-// and subject to any constraints placed on the use of the algorithm by
-// its designers (if such constraints may exist, this will be indicated
-// below).
-//
-// Dr. B. R. Gladman                               . 25th January 2000.
-//
-// This is an implementation of Serpent, an encryption algorithm designed
-// by Anderson, Biham and Knudsen and submitted as a candidate for the
-// Advanced Encryption Standard programme of the US National Institute of
-// Standards and Technology.
-//
-// The designers of Serpent have not placed any constraints on the use of
-// this algorithm.
-
-#include <sys/types.h>
-#include <sys/endian.h>
-
-#include <crypto/serpent/serpent.h>
-
-// Partially optimised Serpent S Box boolean functions derived
-// using a recursive descent analyser but without a full search
-// of all subtrees. This set of S boxes is the result of work
-// by Sam Simpson and Brian Gladman using the spare time on a
-// cluster of high capacity servers to search for S boxes with
-// this customised search engine.
-//
-// Copyright:   Dr B. R Gladman (gladman@seven77.demon.co.uk)
-//              and Sam Simpson (s.simpson@mia.co.uk)
-//              17th December 1998
-//
-// We hereby give permission for information in this file to be
-// used freely subject only to acknowledgement of its origin
-
-// 15 terms
-
-#define rotr(x,n)   (((x) >> ((int)((n) & 0x1f))) | ((x) << ((int)((32 - ((n) & 0x1f))))))
-#define rotl(x,n)   (((x) << ((int)((n) & 0x1f))) | ((x) >> ((int)((32 - ((n) & 0x1f))))))
-
-#define sb0(a,b,c,d,e,f,g,h)    \
-{   t1 = a ^ d;     \
-    t2 = a & d;     \
-    t3 = c ^ t1;    \
-    t6 = b & t1;    \
-    t4 = b ^ t3;    \
-    t10 = ~t3;      \
-    h = t2 ^ t4;    \
-    t7 = a ^ t6;    \
-    t14 = ~t7;      \
-    t8 = c | t7;    \
-    t11 = t3 ^ t7;  \
-    g = t4 ^ t8;    \
-    t12 = h & t11;  \
-    f = t10 ^ t12;  \
-    e = t12 ^ t14;  \
-}
-// 15 terms
-
-#define ib0(a,b,c,d,e,f,g,h)    \
-{   t1 = ~a;        \
-    t2 = a ^ b;     \
-    t3 = t1 | t2;   \
-    t4 = d ^ t3;    \
-    t7 = d & t2;    \
-    t5 = c ^ t4;    \
-    t8 = t1 ^ t7;   \
-    g = t2 ^ t5;    \
-    t11 = a & t4;   \
-    t9 = g & t8;    \
-    t14 = t5 ^ t8;  \
-    f = t4 ^ t9;    \
-    t12 = t5 | f;   \
-    h = t11 ^ t12;  \
-    e = h ^ t14;    \
-}
-
-// 14 terms!
-
-#define sb1(a,b,c,d,e,f,g,h)    \
-{   t1 = ~a;        \
-    t2 = b ^ t1;    \
-    t3 = a | t2;    \
-    t4 = d | t2;    \
-    t5 = c ^ t3;    \
-    g = d ^ t5;     \
-    t7 = b ^ t4;    \
-    t8 = t2 ^ g;    \
-    t9 = t5 & t7;   \
-    h = t8 ^ t9;    \
-    t11 = t5 ^ t7;  \
-    f = h ^ t11;    \
-    t13 = t8 & t11; \
-    e = t5 ^ t13;   \
-}
-
-// 17 terms
-
-#define ib1(a,b,c,d,e,f,g,h)    \
-{   t1 = a ^ d;     \
-    t2 = a & b;     \
-    t3 = b ^ c;     \
-    t4 = a ^ t3;    \
-    t5 = b | d;     \
-    t7 = c | t1;    \
-    h = t4 ^ t5;    \
-    t8 = b ^ t7;    \
-    t11 = ~t2;      \
-    t9 = t4 & t8;   \
-    f = t1 ^ t9;    \
-    t13 = t9 ^ t11; \
-    t12 = h & f;    \
-    g = t12 ^ t13;  \
-    t15 = a & d;    \
-    t16 = c ^ t13;  \
-    e = t15 ^ t16;  \
-}
-
-// 16 terms
-
-#define sb2(a,b,c,d,e,f,g,h)    \
-{   t1 = ~a;        \
-    t2 = b ^ d;     \
-    t3 = c & t1;    \
-    t13 = d | t1;   \
-    e = t2 ^ t3;    \
-    t5 = c ^ t1;    \
-    t6 = c ^ e;     \
-    t7 = b & t6;    \
-    t10 = e | t5;   \
-    h = t5 ^ t7;    \
-    t9 = d | t7;    \
-    t11 = t9 & t10; \
-    t14 = t2 ^ h;   \
-    g = a ^ t11;    \
-    t15 = g ^ t13;  \
-    f = t14 ^ t15;  \
-}
-
-// 16 terms
-
-#define ib2(a,b,c,d,e,f,g,h)    \
-{   t1 = b ^ d;     \
-    t2 = ~t1;       \
-    t3 = a ^ c;     \
-    t4 = c ^ t1;    \
-    t7 = a | t2;    \
-    t5 = b & t4;    \
-    t8 = d ^ t7;    \
-    t11 = ~t4;      \
-    e = t3 ^ t5;    \
-    t9 = t3 | t8;   \
-    t14 = d & t11;  \
-    h = t1 ^ t9;    \
-    t12 = e | h;    \
-    f = t11 ^ t12;  \
-    t15 = t3 ^ t12; \
-    g = t14 ^ t15;  \
-}
-
-// 17 terms
-
-#define sb3(a,b,c,d,e,f,g,h)    \
-{   t1 = a ^ c;     \
-    t2 = d ^ t1;    \
-    t3 = a & t2;    \
-    t4 = d ^ t3;    \
-    t5 = b & t4;    \
-    g = t2 ^ t5;    \
-    t7 = a | g;     \
-    t8 = b | d;     \
-    t11 = a | d;    \
-    t9 = t4 & t7;   \
-    f = t8 ^ t9;    \
-    t12 = b ^ t11;  \
-    t13 = g ^ t9;   \
-    t15 = t3 ^ t8;  \
-    h = t12 ^ t13;  \
-    t16 = c & t15;  \
-    e = t12 ^ t16;  \
-}
-
-// 16 term solution that performs less well than 17 term one
-// in my environment (PPro/PII)
-
-#if 0
-
-#define sb3(a,b,c,d,e,f,g,h)    \
-{   t1 = a ^ b;     \
-    t2 = a & c;     \
-    t3 = a | d;     \
-    t4 = c ^ d;     \
-    t5 = t1 & t3;   \
-    t6 = t2 | t5;   \
-    g = t4 ^ t6;    \
-    t8 = b ^ t3;    \
-    t9 = t6 ^ t8;   \
-    t10 = t4 & t9;  \
-    e = t1 ^ t10;   \
-    t12 = g & e;    \
-    f = t9 ^ t12;   \
-    t14 = b | d;    \
-    t15 = t4 ^ t12; \
-    h = t14 ^ t15;  \
-}
-
-#endif
-
-// 17 terms
-
-#define ib3(a,b,c,d,e,f,g,h)    \
-{   t1 = b ^ c;     \
-    t2 = b | c;     \
-    t3 = a ^ c;     \
-    t7 = a ^ d;     \
-    t4 = t2 ^ t3;   \
-    t5 = d | t4;    \
-    t9 = t2 ^ t7;   \
-    e = t1 ^ t5;    \
-    t8 = t1 | t5;   \
-    t11 = a & t4;   \
-    g = t8 ^ t9;    \
-    t12 = e | t9;   \
-    f = t11 ^ t12;  \
-    t14 = a & g;    \
-    t15 = t2 ^ t14; \
-    t16 = e & t15;  \
-    h = t4 ^ t16;   \
-}
-
-// 15 terms
-
-#define sb4(a,b,c,d,e,f,g,h)    \
-{   t1 = a ^ d;     \
-    t2 = d & t1;    \
-    t3 = c ^ t2;    \
-    t4 = b | t3;    \
-    h = t1 ^ t4;    \
-    t6 = ~b;        \
-    t7 = t1 | t6;   \
-    e = t3 ^ t7;    \
-    t9 = a & e;     \
-    t10 = t1 ^ t6;  \
-    t11 = t4 & t10; \
-    g = t9 ^ t11;   \
-    t13 = a ^ t3;   \
-    t14 = t10 & g;  \
-    f = t13 ^ t14;  \
-}
-
-// 17 terms
-
-#define ib4(a,b,c,d,e,f,g,h)    \
-{   t1 = c ^ d;     \
-    t2 = c | d;     \
-    t3 = b ^ t2;    \
-    t4 = a & t3;    \
-    f = t1 ^ t4;    \
-    t6 = a ^ d;     \
-    t7 = b | d;     \
-    t8 = t6 & t7;   \
-    h = t3 ^ t8;    \
-    t10 = ~a;       \
-    t11 = c ^ h;    \
-    t12 = t10 | t11;\
-    e = t3 ^ t12;   \
-    t14 = c | t4;   \
-    t15 = t7 ^ t14; \
-    t16 = h | t10;  \
-    g = t15 ^ t16;  \
-}
-
-// 16 terms
-
-#define sb5(a,b,c,d,e,f,g,h)    \
-{   t1 = ~a;        \
-    t2 = a ^ b;     \
-    t3 = a ^ d;     \
-    t4 = c ^ t1;    \
-    t5 = t2 | t3;   \
-    e = t4 ^ t5;    \
-    t7 = d & e;     \
-    t8 = t2 ^ e;    \
-    t10 = t1 | e;   \
-    f = t7 ^ t8;    \
-    t11 = t2 | t7;  \
-    t12 = t3 ^ t10; \
-    t14 = b ^ t7;   \
-    g = t11 ^ t12;  \
-    t15 = f & t12;  \
-    h = t14 ^ t15;  \
-}
-
-// 16 terms
-
-#define ib5(a,b,c,d,e,f,g,h)    \
-{   t1 = ~c;        \
-    t2 = b & t1;    \
-    t3 = d ^ t2;    \
-    t4 = a & t3;    \
-    t5 = b ^ t1;    \
-    h = t4 ^ t5;    \
-    t7 = b | h;     \
-    t8 = a & t7;    \
-    f = t3 ^ t8;    \
-    t10 = a | d;    \
-    t11 = t1 ^ t7;  \
-    e = t10 ^ t11;  \
-    t13 = a ^ c;    \
-    t14 = b & t10;  \
-    t15 = t4 | t13; \
-    g = t14 ^ t15;  \
-}
-
-// 15 terms
-
-#define sb6(a,b,c,d,e,f,g,h)    \
-{   t1 = ~a;        \
-    t2 = a ^ d;     \
-    t3 = b ^ t2;    \
-    t4 = t1 | t2;   \
-    t5 = c ^ t4;    \
-    f = b ^ t5;     \
-    t13 = ~t5;      \
-    t7 = t2 | f;    \
-    t8 = d ^ t7;    \
-    t9 = t5 & t8;   \
-    g = t3 ^ t9;    \
-    t11 = t5 ^ t8;  \
-    e = g ^ t11;    \
-    t14 = t3 & t11; \
-    h = t13 ^ t14;  \
-}
-
-// 15 terms
-
-#define ib6(a,b,c,d,e,f,g,h)    \
-{   t1 = ~a;        \
-    t2 = a ^ b;     \
-    t3 = c ^ t2;    \
-    t4 = c | t1;    \
-    t5 = d ^ t4;    \
-    t13 = d & t1;   \
-    f = t3 ^ t5;    \
-    t7 = t3 & t5;   \
-    t8 = t2 ^ t7;   \
-    t9 = b | t8;    \
-    h = t5 ^ t9;    \
-    t11 = b | h;    \
-    e = t8 ^ t11;   \
-    t14 = t3 ^ t11; \
-    g = t13 ^ t14;  \
-}
-
-// 17 terms
-
-#define sb7(a,b,c,d,e,f,g,h)    \
-{   t1 = ~c;        \
-    t2 = b ^ c;     \
-    t3 = b | t1;    \
-    t4 = d ^ t3;    \
-    t5 = a & t4;    \
-    t7 = a ^ d;     \
-    h = t2 ^ t5;    \
-    t8 = b ^ t5;    \
-    t9 = t2 | t8;   \
-    t11 = d & t3;   \
-    f = t7 ^ t9;    \
-    t12 = t5 ^ f;   \
-    t15 = t1 | t4;  \
-    t13 = h & t12;  \
-    g = t11 ^ t13;  \
-    t16 = t12 ^ g;  \
-    e = t15 ^ t16;  \
-}
-
-// 17 terms
-
-#define ib7(a,b,c,d,e,f,g,h)    \
-{   t1 = a & b;     \
-    t2 = a | b;     \
-    t3 = c | t1;    \
-    t4 = d & t2;    \
-    h = t3 ^ t4;    \
-    t6 = ~d;        \
-    t7 = b ^ t4;    \
-    t8 = h ^ t6;    \
-    t11 = c ^ t7;   \
-    t9 = t7 | t8;   \
-    f = a ^ t9;     \
-    t12 = d | f;    \
-    e = t11 ^ t12;  \
-    t14 = a & h;    \
-    t15 = t3 ^ f;   \
-    t16 = e ^ t14;  \
-    g = t15 ^ t16;  \
-}
-
-#define k_xor(r,a,b,c,d)             \
-{   a ^= ctx->l_key[4 * r +  8]; \
-    b ^= ctx->l_key[4 * r +  9]; \
-    c ^= ctx->l_key[4 * r + 10]; \
-    d ^= ctx->l_key[4 * r + 11]; \
-}
-
-#define k_set(r,a,b,c,d)             \
-{   a = ctx->l_key[4 * r +  8];  \
-    b = ctx->l_key[4 * r +  9];  \
-    c = ctx->l_key[4 * r + 10];  \
-    d = ctx->l_key[4 * r + 11];  \
-}
-
-#define k_get(r,a,b,c,d)    \
-{   ctx->l_key[4 * r +  8] = a;  \
-    ctx->l_key[4 * r +  9] = b;  \
-    ctx->l_key[4 * r + 10] = c;  \
-    ctx->l_key[4 * r + 11] = d;  \
-}
-
-// the linear transformation and its inverse
-
-#define rot(a,b,c,d)    \
-{   a = rotl(a, 13);    \
-    c = rotl(c, 3);     \
-    d ^= c ^ (a << 3);  \
-    b ^= a ^ c;         \
-    d = rotl(d, 7);     \
-    b = rotl(b, 1);     \
-    a ^= b ^ d;         \
-    c ^= d ^ (b << 7);  \
-    a = rotl(a, 5);     \
-    c = rotl(c, 22);    \
-}
-
-#define irot(a,b,c,d)   \
-{   c = rotr(c, 22);    \
-    a = rotr(a, 5);     \
-    c ^= d ^ (b << 7);  \
-    a ^= b ^ d;         \
-    d = rotr(d, 7);     \
-    b = rotr(b, 1);     \
-    d ^= c ^ (a << 3);  \
-    b ^= a ^ c;         \
-    c = rotr(c, 3);     \
-    a = rotr(a, 13);    \
-}
-
-// initialise the key schedule from the user supplied key
-
-void serpent_set_key(serpent_ctx *ctx, const u_int8_t in_key[], int key_len)
-{
-    u_int32_t  i,lk,a,b,c,d,e,f,g,h;
-    u_int32_t  t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;
-
-    if(key_len > 256)
-        return;
-
-    i = 0; lk = (key_len + 31) / 32;
-
-    while(i < lk)
-    {
-        ctx->l_key[i] = htole32(*((const u_int32_t *)(in_key + 4 * i))); i++;
-    }
-
-    if(key_len < 256)
-    {
-        while(i < 8)
-
-            ctx->l_key[i++] = 0;
-
-        i = key_len / 32; lk = 1 << key_len % 32;
-
-        ctx->l_key[i] = (ctx->l_key[i] & (lk - 1)) | lk;
-    }
-
-    t1 = ctx->l_key[2] ^ ctx->l_key[4] ^ ctx->l_key[6] ^ 0x9e3779b9;
-    t2 = ctx->l_key[3] ^ ctx->l_key[5] ^ ctx->l_key[7] ^ 0x9e3779b9;
-
-    for(i = 0; i < 132; i += 2)
-    {
-        ctx->l_key[i + 8] = rotr(i ^ ctx->l_key[i] ^ t2, 21);
-
-        t1 ^= ctx->l_key[i + 2] ^ ctx->l_key[i + 8];
-
-        ctx->l_key[i + 9] = rotr((i + 1) ^ ctx->l_key[i + 1] ^ t1, 21);
-
-        t2 ^= ctx->l_key[i + 3] ^ ctx->l_key[i + 9];
-    }
-
-    k_set( 0,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get( 0,e,f,g,h);
-    k_set( 1,a,b,c,d);sb2(a,b,c,d,e,f,g,h);k_get( 1,e,f,g,h);
-    k_set( 2,a,b,c,d);sb1(a,b,c,d,e,f,g,h);k_get( 2,e,f,g,h);
-    k_set( 3,a,b,c,d);sb0(a,b,c,d,e,f,g,h);k_get( 3,e,f,g,h);
-    k_set( 4,a,b,c,d);sb7(a,b,c,d,e,f,g,h);k_get( 4,e,f,g,h);
-    k_set( 5,a,b,c,d);sb6(a,b,c,d,e,f,g,h);k_get( 5,e,f,g,h);
-    k_set( 6,a,b,c,d);sb5(a,b,c,d,e,f,g,h);k_get( 6,e,f,g,h);
-    k_set( 7,a,b,c,d);sb4(a,b,c,d,e,f,g,h);k_get( 7,e,f,g,h);
-    k_set( 8,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get( 8,e,f,g,h);
-    k_set( 9,a,b,c,d);sb2(a,b,c,d,e,f,g,h);k_get( 9,e,f,g,h);
-    k_set(10,a,b,c,d);sb1(a,b,c,d,e,f,g,h);k_get(10,e,f,g,h);
-    k_set(11,a,b,c,d);sb0(a,b,c,d,e,f,g,h);k_get(11,e,f,g,h);
-    k_set(12,a,b,c,d);sb7(a,b,c,d,e,f,g,h);k_get(12,e,f,g,h);
-    k_set(13,a,b,c,d);sb6(a,b,c,d,e,f,g,h);k_get(13,e,f,g,h);
-    k_set(14,a,b,c,d);sb5(a,b,c,d,e,f,g,h);k_get(14,e,f,g,h);
-    k_set(15,a,b,c,d);sb4(a,b,c,d,e,f,g,h);k_get(15,e,f,g,h);
-    k_set(16,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get(16,e,f,g,h);
-    k_set(17,a,b,c,d);sb2(a,b,c,d,e,f,g,h);k_get(17,e,f,g,h);
-    k_set(18,a,b,c,d);sb1(a,b,c,d,e,f,g,h);k_get(18,e,f,g,h);
-    k_set(19,a,b,c,d);sb0(a,b,c,d,e,f,g,h);k_get(19,e,f,g,h);
-    k_set(20,a,b,c,d);sb7(a,b,c,d,e,f,g,h);k_get(20,e,f,g,h);
-    k_set(21,a,b,c,d);sb6(a,b,c,d,e,f,g,h);k_get(21,e,f,g,h);
-    k_set(22,a,b,c,d);sb5(a,b,c,d,e,f,g,h);k_get(22,e,f,g,h);
-    k_set(23,a,b,c,d);sb4(a,b,c,d,e,f,g,h);k_get(23,e,f,g,h);
-    k_set(24,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get(24,e,f,g,h);
-    k_set(25,a,b,c,d);sb2(a,b,c,d,e,f,g,h);k_get(25,e,f,g,h);
-    k_set(26,a,b,c,d);sb1(a,b,c,d,e,f,g,h);k_get(26,e,f,g,h);
-    k_set(27,a,b,c,d);sb0(a,b,c,d,e,f,g,h);k_get(27,e,f,g,h);
-    k_set(28,a,b,c,d);sb7(a,b,c,d,e,f,g,h);k_get(28,e,f,g,h);
-    k_set(29,a,b,c,d);sb6(a,b,c,d,e,f,g,h);k_get(29,e,f,g,h);
-    k_set(30,a,b,c,d);sb5(a,b,c,d,e,f,g,h);k_get(30,e,f,g,h);
-    k_set(31,a,b,c,d);sb4(a,b,c,d,e,f,g,h);k_get(31,e,f,g,h);
-    k_set(32,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get(32,e,f,g,h);
-
-    return;
-}
-
-// encrypt a block of text
-
-void serpent_encrypt(serpent_ctx *ctx, const u_int8_t in_blk[],
-		     u_int8_t out_blk[])
-{
-    u_int32_t  a,b,c,d,e,f,g,h;
-    u_int32_t  t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;
-
-    a = htole32(*((const u_int32_t *)(in_blk     )));
-    b = htole32(*((const u_int32_t *)(in_blk + 4 )));
-    c = htole32(*((const u_int32_t *)(in_blk + 8 )));
-    d = htole32(*((const u_int32_t *)(in_blk + 12)));
-
-    k_xor( 0,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor( 1,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor( 2,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor( 3,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor( 4,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor( 5,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor( 6,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor( 7,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor( 8,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor( 9,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(10,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(11,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(12,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(13,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(14,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(15,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(16,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(17,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(18,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(19,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(20,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(21,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(22,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(23,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(24,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(25,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(26,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(27,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(28,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(29,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);
-    k_xor(30,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);
-    k_xor(31,e,f,g,h); sb7(e,f,g,h,a,b,c,d); k_xor(32,a,b,c,d);
-
-    *((u_int32_t *)(out_blk     )) = le32toh(a);
-    *((u_int32_t *)(out_blk +  4)) = le32toh(b);
-    *((u_int32_t *)(out_blk +  8)) = le32toh(c);
-    *((u_int32_t *)(out_blk + 12)) = le32toh(d);
-}
-
-// decrypt a block of text
-
-void serpent_decrypt(serpent_ctx *ctx, const u_int8_t in_blk[],
-		     u_int8_t out_blk[])
-{
-    u_int32_t  a,b,c,d,e,f,g,h;
-    u_int32_t  t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;
-
-    a = htole32(*((const u_int32_t *)(in_blk     )));
-    b = htole32(*((const u_int32_t *)(in_blk + 4 )));
-    c = htole32(*((const u_int32_t *)(in_blk + 8 )));
-    d = htole32(*((const u_int32_t *)(in_blk + 12)));
-
-    k_xor(32,a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(31,e,f,g,h);
-    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(30,a,b,c,d);
-    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(29,e,f,g,h);
-    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(28,a,b,c,d);
-    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(27,e,f,g,h);
-    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(26,a,b,c,d);
-    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(25,e,f,g,h);
-    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(24,a,b,c,d);
-    irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(23,e,f,g,h);
-    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(22,a,b,c,d);
-    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(21,e,f,g,h);
-    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(20,a,b,c,d);
-    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(19,e,f,g,h);
-    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(18,a,b,c,d);
-    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(17,e,f,g,h);
-    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(16,a,b,c,d);
-    irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(15,e,f,g,h);
-    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(14,a,b,c,d);
-    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(13,e,f,g,h);
-    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(12,a,b,c,d);
-    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(11,e,f,g,h);
-    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(10,a,b,c,d);
-    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 9,e,f,g,h);
-    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 8,a,b,c,d);
-    irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor( 7,e,f,g,h);
-    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor( 6,a,b,c,d);
-    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor( 5,e,f,g,h);
-    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor( 4,a,b,c,d);
-    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor( 3,e,f,g,h);
-    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor( 2,a,b,c,d);
-    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 1,e,f,g,h);
-    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 0,a,b,c,d);
-
-    *((u_int32_t *)(out_blk     )) = le32toh(a);
-    *((u_int32_t *)(out_blk +  4)) = le32toh(b);
-    *((u_int32_t *)(out_blk +  8)) = le32toh(c);
-    *((u_int32_t *)(out_blk + 12)) = le32toh(d);
-}
-
diff --git a/sys/crypto/serpent/serpent.h b/sys/crypto/serpent/serpent.h
deleted file mode 100644
index 7319cb78a4..0000000000
--- a/sys/crypto/serpent/serpent.h
+++ /dev/null
@@ -1,16 +0,0 @@
-
-// Copyright in this code is held by Dr B. R. Gladman but free direct or
-// derivative use is permitted subject to acknowledgement of its origin.
-// Dr B. R. Gladman                               .   25th January 2000.
-
-typedef struct
-{
-	uint32_t l_key[140];
-} serpent_ctx;
-
-void serpent_set_key(serpent_ctx *ctx, const u_int8_t in_key[], int key_len);
-void serpent_encrypt(serpent_ctx *ctx, const u_int8_t in_blk[],
-		     u_int8_t out_blk[]);
-void serpent_decrypt(serpent_ctx *ctx, const u_int8_t in_blk[],
-		     u_int8_t out_blk[]);
-
diff --git a/sys/crypto/twofish/twofish.c b/sys/crypto/twofish/twofish.c
deleted file mode 100644
index 12a8763ede..0000000000
--- a/sys/crypto/twofish/twofish.c
+++ /dev/null
@@ -1,514 +0,0 @@
-/*
- ---------------------------------------------------------------------------
- Copyright (c) 1999, Dr Brian Gladman, Worcester, UK.   All rights reserved.
-
- LICENSE TERMS
-
- The free distribution and use of this software is allowed (with or without
- changes) provided that:
-
-  1. source code distributions include the above copyright notice, this
-     list of conditions and the following disclaimer;
-
-  2. binary distributions include the above copyright notice, this list
-     of conditions and the following disclaimer in their documentation;
-
-  3. the name of the copyright holder is not used to endorse products
-     built using this software without specific written permission.
-
- DISCLAIMER
-
- This software is provided 'as is' with no explicit or implied warranties
- in respect of its properties, including, but not limited to, correctness
- and/or fitness for purpose.
- ---------------------------------------------------------------------------
-
- My thanks to Doug Whiting and Niels Ferguson for comments that led
- to improvements in this implementation.
-
- Issue Date: 14th January 1999
-*/
-
-#include <sys/types.h>
-#include <sys/endian.h>
-
-#include <crypto/twofish/twofish.h>
-
-#define LE32(x) htole32(x)
-
-#ifndef extract_byte
-#define extract_byte(x,n)   ((u_int8_t)((x) >> (8 * n)))
-#endif
-
-#ifndef rotl
-#define rotr(x,n) (((x)>>(n))|((x)<<(32-(n))))
-#define rotl(x,n) (((x)<<(n))|((x)>>(32-(n))))
-#endif
-
-
-static const u_int8_t q_tab[2][256] = {
-    {
-    0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78,
-    0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
-    0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30,
-    0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82,
-    0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE,
-    0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B,
-    0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45,
-    0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7,
-    0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF,
-    0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8,
-    0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED,
-    0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90,
-    0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B,
-    0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B,
-    0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F,
-    0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A,
-    0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17,
-    0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72,
-    0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68,
-    0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4,
-    0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42,
-    0x4A, 0x5E, 0xC1, 0xE0
-    },
-    {
-    0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B,
-    0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1,
-    0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B,
-    0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5,
-    0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54,
-    0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96,
-    0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7,
-    0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8,
-    0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF,
-    0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9,
-    0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D,
-    0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E,
-    0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21,
-    0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01,
-    0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E,
-    0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64,
-    0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44,
-    0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E,
-    0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B,
-    0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9,
-    0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56,
-    0x55, 0x09, 0xBE, 0x91
-    }
-};
-
-#define q(n,x)  q_tab[n][x]
-
-
-static u_int32_t  m_tab[4][256] = {
-   {0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B,
-    0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B,
-    0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0x3C3C57D6, 0x93938A32,
-    0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1,
-    0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x4A4AF31B, 0xBFBF4887, 0x0D0D70FA,
-    0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x7D7D20BA, 0x666631AE, 0x3A3AA35B,
-    0x59591C8A, 0x00000000, 0xCDCD93BC, 0x1A1AE09D, 0xAEAE2C6D, 0x7F7FABC1,
-    0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x8A8A105D, 0x3B3B52D2, 0x6464BAD5,
-    0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x1B1B1114, 0x2C2CC2B5, 0xFCFCB490,
-    0x3131272C, 0x808065A3, 0x73732AB2, 0x0C0C8173, 0x79795F4C, 0x6B6B4154,
-    0x4B4B0292, 0x53536974, 0x94948F36, 0x83831F51, 0x2A2A3638, 0xC4C49CB0,
-    0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x48487860, 0xFFFFCE62, 0x4C4C0796,
-    0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x1C1C1410, 0x5D5D637C, 0x36362228,
-    0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x1414EA95, 0xF5F5BB9C, 0xCFCF18C7,
-    0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x54546C70, 0x29294CCA, 0xF0F035E3,
-    0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x8C8CE4D0, 0xA4A45993, 0xCACA96B8,
-    0x68683BA6, 0xB8B84D83, 0x38382820, 0xE5E52EFF, 0xADAD569F, 0x0B0B8477,
-    0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x19199A6F, 0x0E0E0A08, 0x95957EBF,
-    0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x1F1F6EE2, 0xB5B53D79, 0x09090F0C,
-    0x616134AA, 0x57571682, 0x9F9F0B41, 0x9D9D803A, 0x111164EA, 0x2525CDB9,
-    0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0xA3A35C97, 0xEAEAD57E, 0x353558DA,
-    0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0xFBFBB194, 0x3737D3A1, 0xFAFA401D,
-    0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x9C9C71B3, 0x5656E70B, 0xE3E3DA72,
-    0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x6363BFD1, 0x3434A953, 0x9A9A853E,
-    0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x3D3DA65F, 0xA1A1D7EC, 0xE4E4DF76,
-    0x8181942A, 0x91910149, 0x0F0FFB81, 0xEEEEAA88, 0x161661EE, 0xD7D77321,
-    0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x6D6DB5D9, 0x7878AEC5, 0xC5C56D39,
-    0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0xCBCB6731, 0xB6B6478B, 0xEFEF5B01,
-    0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x4D4DF61F, 0xCECEE94E, 0xDEDE7C2D,
-    0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x03037AF2, 0xA0A02665, 0x5E5E198E,
-    0x5A5A6678, 0x65654B5C, 0x62624E58, 0xFDFD4519, 0x0606F48D, 0x404086E5,
-    0xF2F2BE98, 0x3333AC57, 0x17179067, 0x05058E7F, 0xE8E85E05, 0x4F4F7D64,
-    0x89896AAF, 0x10109563, 0x74742FB6, 0x0A0A75FE, 0x5C5C92F5, 0x9B9B74B7,
-    0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x494989E9, 0x46467268, 0x77775544,
-    0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0xA9A92969, 0xD9D97929, 0x8686912E,
-    0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0xD6D682A8, 0xB9B9BC0A, 0x42420D9E,
-    0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x23233934, 0xCCCC6235, 0xF1F1C46A,
-    0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x7171A1C9, 0x9090F0C0, 0xAAAA539B,
-    0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x8E8E6FAB, 0xABABA212, 0x6F6F3EA2,
-    0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0xB7B7B602, 0x6969CA2F, 0x3939D9A9,
-    0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0xC3C399B4, 0x6C6C4450, 0x07070504,
-    0x04047FF6, 0x272746C2, 0xACACA716, 0xD0D07625, 0x50501386, 0xDCDCF756,
-    0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x1313EF91},
-
-   {0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252,
-    0xA3658080, 0x76DFE4E4, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A,
-    0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x0D54E6E6, 0xC6432020,
-    0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141,
-    0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x269B8888, 0xFA700D0D, 0x13F94444,
-    0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0xD0E48C8C, 0x8B47B6B6, 0x303C2424,
-    0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x23C56060, 0x1945FDFD, 0x5BA33A3A,
-    0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0xAE316666, 0xA23E6F6F, 0x82165757,
-    0x63951010, 0x015BEFEF, 0x834DB8B8, 0x2E918686, 0xD9B56D6D, 0x511F8383,
-    0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0xEB3FFEFE, 0xA5D63030, 0xBE257A7A,
-    0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x6123A7A7, 0xC0F09090, 0x8CAFE9E9,
-    0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x2C273131, 0x2576D0D0, 0x0BE75656,
-    0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x6B9F1E1E, 0x53A93434, 0x6AC4F1F1,
-    0xB499C3C3, 0xF1975B5B, 0xE1834747, 0xE66B1818, 0xBDC82222, 0x450E9898,
-    0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x66CBF8F8, 0xCCFF9999, 0x95EA1414,
-    0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x1C1B1515, 0x1EADA2A2, 0xD70CD3D3,
-    0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0xB5C22C2C, 0xE9894949, 0xCF12C1C1,
-    0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x77840B0B, 0x396DC5C5, 0xAF6A8989,
-    0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x7137BBBB, 0x81FB0F0F, 0x793DB5B5,
-    0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0xCDA47676, 0xF99D5555, 0xD8EE8282,
-    0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x4D049696, 0x44557777, 0x080A0E0E,
-    0x86135050, 0xE730F7F7, 0xA1D33737, 0x1D40FAFA, 0xAA346161, 0xED8C4E4E,
-    0x06B3B0B0, 0x706C5454, 0xB22A7373, 0xD2523B3B, 0x410B9F9F, 0x7B8B0202,
-    0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0xC2462727, 0x27C06767, 0x90B4FCFC,
-    0x20283838, 0xF67F0404, 0x60784848, 0xFF2EE5E5, 0x96074C4C, 0x5C4B6565,
-    0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x9CBBF5F5, 0x52F2DBDB, 0x1BF34A4A,
-    0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0xEF3AF9F9, 0x91EF1313, 0x85FE0808,
-    0x49019191, 0xEE611616, 0x2D7CDEDE, 0x4FB22121, 0x8F42B1B1, 0x3BDB7272,
-    0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x46E3C0C0, 0xD6573C3C, 0x3E859A9A,
-    0x6929A9A9, 0x647D4F4F, 0x2A948181, 0xCE492E2E, 0xCB17C6C6, 0x2FCA6969,
-    0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x7AD0EDED, 0xAC87D1D1, 0x7F8E0505,
-    0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x0EB9BEBE, 0xA7608787, 0x5AF8D5D5,
-    0x28223636, 0x14111B1B, 0x3FDE7575, 0x2979D9D9, 0x88AAEEEE, 0x3C332D2D,
-    0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0xDA583535, 0xB09CC4C4, 0x17FC4343,
-    0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x7D38B2B2, 0x57AC3333, 0xC718CFCF,
-    0x8DF40606, 0x74695353, 0xB7749B9B, 0xC4F59797, 0x9F56ADAD, 0x72DAE3E3,
-    0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x12A2ABAB, 0x584E6262, 0x07E85F5F,
-    0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x50446C6C, 0xDE5D3232, 0x68724646,
-    0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0xF8C6BABA, 0xC8FA9E9E, 0xA882D6D6,
-    0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0xFE750A0A, 0x328A9393, 0xA48DDFDF,
-    0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0xF0CCB4B4, 0xD309D4D4, 0x5D108A8A,
-    0x0FE25151, 0x00000000, 0x6F9A1919, 0x9DE01A1A, 0x368F9494, 0x42E6C7C7,
-    0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0xE0D8A8A8},
-
-   {0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B,
-    0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F,
-    0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0x3CD63C57, 0x9332938A,
-    0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783,
-    0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x4A1B4AF3, 0xBF87BF48, 0x0DFA0D70,
-    0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x7DBA7D20, 0x66AE6631, 0x3A5B3AA3,
-    0x598A591C, 0x00000000, 0xCDBCCD93, 0x1A9D1AE0, 0xAE6DAE2C, 0x7FC17FAB,
-    0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x8A5D8A10, 0x3BD23B52, 0x64D564BA,
-    0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x1B141B11, 0x2CB52CC2, 0xFC90FCB4,
-    0x312C3127, 0x80A38065, 0x73B2732A, 0x0C730C81, 0x794C795F, 0x6B546B41,
-    0x4B924B02, 0x53745369, 0x9436948F, 0x8351831F, 0x2A382A36, 0xC4B0C49C,
-    0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x48604878, 0xFF62FFCE, 0x4C964C07,
-    0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x1C101C14, 0x5D7C5D63, 0x36283622,
-    0x672767C0, 0xE98CE9AF, 0x441344F9, 0x149514EA, 0xF59CF5BB, 0xCFC7CF18,
-    0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x5470546C, 0x29CA294C, 0xF0E3F035,
-    0x088508FE, 0xC6CBC617, 0xF311F34F, 0x8CD08CE4, 0xA493A459, 0xCAB8CA96,
-    0x68A6683B, 0xB883B84D, 0x38203828, 0xE5FFE52E, 0xAD9FAD56, 0x0B770B84,
-    0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x196F199A, 0x0E080E0A, 0x95BF957E,
-    0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x1FE21F6E, 0xB579B53D, 0x090C090F,
-    0x61AA6134, 0x57825716, 0x9F419F0B, 0x9D3A9D80, 0x11EA1164, 0x25B925CD,
-    0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0xA397A35C, 0xEA7EEAD5, 0x35DA3558,
-    0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0xFB94FBB1, 0x37A137D3, 0xFA1DFA40,
-    0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x9CB39C71, 0x560B56E7, 0xE372E3DA,
-    0x87A78760, 0x151C151B, 0xF9EFF93A, 0x63D163BF, 0x345334A9, 0x9A3E9A85,
-    0xB18FB142, 0x7C337CD1, 0x8826889B, 0x3D5F3DA6, 0xA1ECA1D7, 0xE476E4DF,
-    0x812A8194, 0x91499101, 0x0F810FFB, 0xEE88EEAA, 0x16EE1661, 0xD721D773,
-    0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x6DD96DB5, 0x78C578AE, 0xC539C56D,
-    0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0xCB31CB67, 0xB68BB647, 0xEF01EF5B,
-    0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x4D1F4DF6, 0xCE4ECEE9, 0xDE2DDE7C,
-    0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x03F2037A, 0xA065A026, 0x5E8E5E19,
-    0x5A785A66, 0x655C654B, 0x6258624E, 0xFD19FD45, 0x068D06F4, 0x40E54086,
-    0xF298F2BE, 0x335733AC, 0x17671790, 0x057F058E, 0xE805E85E, 0x4F644F7D,
-    0x89AF896A, 0x10631095, 0x74B6742F, 0x0AFE0A75, 0x5CF55C92, 0x9BB79B74,
-    0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x49E94989, 0x46684672, 0x77447755,
-    0xA8E0A8D8, 0x964D9604, 0x284328BD, 0xA969A929, 0xD929D979, 0x862E8691,
-    0xD1ACD187, 0xF415F44A, 0x8D598D15, 0xD6A8D682, 0xB90AB9BC, 0x429E420D,
-    0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x23342339, 0xCC35CC62, 0xF16AF1C4,
-    0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x71C971A1, 0x90C090F0, 0xAA9BAA53,
-    0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x8EAB8E6F, 0xAB12ABA2, 0x6FA26F3E,
-    0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0xB702B7B6, 0x692F69CA, 0x39A939D9,
-    0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0xC3B4C399, 0x6C506C44, 0x07040705,
-    0x04F6047F, 0x27C22746, 0xAC16ACA7, 0xD025D076, 0x50865013, 0xDC56DCF7,
-    0x8455841A, 0xE109E151, 0x7ABE7A25, 0x139113EF},
-
-   {0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98,
-    0x6580A365, 0xDFE476DF, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866,
-    0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x54E60D54, 0x4320C643,
-    0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77,
-    0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x9B88269B, 0x700DFA70, 0xF94413F9,
-    0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0xE48CD0E4, 0x47B68B47, 0x3C24303C,
-    0xA5E784A5, 0x416B5441, 0x06DDDF06, 0xC56023C5, 0x45FD1945, 0xA33A5BA3,
-    0x68C23D68, 0x158D5915, 0x21ECF321, 0x3166AE31, 0x3E6FA23E, 0x16578216,
-    0x95106395, 0x5BEF015B, 0x4DB8834D, 0x91862E91, 0xB56DD9B5, 0x1F83511F,
-    0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x3FFEEB3F, 0xD630A5D6, 0x257ABE25,
-    0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x23A76123, 0xF090C0F0, 0xAFE98CAF,
-    0x809D3A80, 0x925CF592, 0x810C7381, 0x27312C27, 0x76D02576, 0xE7560BE7,
-    0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x9F1E6B9F, 0xA93453A9, 0xC4F16AC4,
-    0x99C3B499, 0x975BF197, 0x8347E183, 0x6B18E66B, 0xC822BDC8, 0x0E98450E,
-    0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0xCBF866CB, 0xFF99CCFF, 0xEA1495EA,
-    0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x1B151C1B, 0xADA21EAD, 0x0CD3D70C,
-    0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0xC22CB5C2, 0x8949E989, 0x12C1CF12,
-    0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x840B7784, 0x6DC5396D, 0x6A89AF6A,
-    0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x37BB7137, 0xFB0F81FB, 0x3DB5793D,
-    0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0xA476CDA4, 0x9D55F99D, 0xEE82D8EE,
-    0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x04964D04, 0x55774455, 0x0A0E080A,
-    0x13508613, 0x30F7E730, 0xD337A1D3, 0x40FA1D40, 0x3461AA34, 0x8C4EED8C,
-    0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x523BD252, 0x0B9F410B, 0x8B027B8B,
-    0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x4627C246, 0xC06727C0, 0xB4FC90B4,
-    0x28382028, 0x7F04F67F, 0x78486078, 0x2EE5FF2E, 0x074C9607, 0x4B655C4B,
-    0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0xBBF59CBB, 0xF2DB52F2, 0xF34A1BF3,
-    0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x3AF9EF3A, 0xEF1391EF, 0xFE0885FE,
-    0x01914901, 0x6116EE61, 0x7CDE2D7C, 0xB2214FB2, 0x42B18F42, 0xDB723BDB,
-    0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0xE3C046E3, 0x573CD657, 0x859A3E85,
-    0x29A96929, 0x7D4F647D, 0x94812A94, 0x492ECE49, 0x17C6CB17, 0xCA692FCA,
-    0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0xD0ED7AD0, 0x87D1AC87, 0x8E057F8E,
-    0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0xB9BE0EB9, 0x6087A760, 0xF8D55AF8,
-    0x22362822, 0x111B1411, 0xDE753FDE, 0x79D92979, 0xAAEE88AA, 0x332D3C33,
-    0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x5835DA58, 0x9CC4B09C, 0xFC4317FC,
-    0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x38B27D38, 0xAC3357AC, 0x18CFC718,
-    0xF4068DF4, 0x69537469, 0x749BB774, 0xF597C4F5, 0x56AD9F56, 0xDAE372DA,
-    0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0xA2AB12A2, 0x4E62584E, 0xE85F07E8,
-    0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x446C5044, 0x5D32DE5D, 0x72466872,
-    0x26A06526, 0x93CDBC93, 0x03DADB03, 0xC6BAF8C6, 0xFA9EC8FA, 0x82D6A882,
-    0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x750AFE75, 0x8A93328A, 0x8DDFA48D,
-    0x4C29CA4C, 0x141C1014, 0x73D72173, 0xCCB4F0CC, 0x09D4D309, 0x108A5D10,
-    0xE2510FE2, 0x00000000, 0x9A196F9A, 0xE01A9DE0, 0x8F94368F, 0xE6C742E6,
-    0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0xD8A8E0D8}
-};
-
-#define mds(n,x)    m_tab[n][x]
-
-
-static u_int32_t h_fun(twofish_ctx *ctx, const u_int32_t x, const u_int32_t key[])
-{
-    u_int32_t  b0, b1, b2, b3;
-
-    b0 = extract_byte(x, 0); b1 = extract_byte(x, 1); b2 = extract_byte(x, 2); b3 = extract_byte(x, 3);
-
-    switch(ctx->k_len)
-    {
-    case 4: b0 = q(1, (u_int8_t) b0) ^ extract_byte(key[3],0);
-            b1 = q(0, (u_int8_t) b1) ^ extract_byte(key[3],1);
-            b2 = q(0, (u_int8_t) b2) ^ extract_byte(key[3],2);
-            b3 = q(1, (u_int8_t) b3) ^ extract_byte(key[3],3);
-    case 3: b0 = q(1, (u_int8_t) b0) ^ extract_byte(key[2],0);
-            b1 = q(1, (u_int8_t) b1) ^ extract_byte(key[2],1);
-            b2 = q(0, (u_int8_t) b2) ^ extract_byte(key[2],2);
-            b3 = q(0, (u_int8_t) b3) ^ extract_byte(key[2],3);
-    case 2: b0 = q(0, (u_int8_t) (q(0, (u_int8_t) b0) ^ extract_byte(key[1],0))) ^ extract_byte(key[0],0);
-            b1 = q(0, (u_int8_t) (q(1, (u_int8_t) b1) ^ extract_byte(key[1],1))) ^ extract_byte(key[0],1);
-            b2 = q(1, (u_int8_t) (q(0, (u_int8_t) b2) ^ extract_byte(key[1],2))) ^ extract_byte(key[0],2);
-            b3 = q(1, (u_int8_t) (q(1, (u_int8_t) b3) ^ extract_byte(key[1],3))) ^ extract_byte(key[0],3);
-    }
-
-    return  mds(0, b0) ^ mds(1, b1) ^ mds(2, b2) ^ mds(3, b3);
-
-}
-
-
-#define q20(x)  q(0,q(0,x) ^ extract_byte(key[1],0)) ^ extract_byte(key[0],0)
-#define q21(x)  q(0,q(1,x) ^ extract_byte(key[1],1)) ^ extract_byte(key[0],1)
-#define q22(x)  q(1,q(0,x) ^ extract_byte(key[1],2)) ^ extract_byte(key[0],2)
-#define q23(x)  q(1,q(1,x) ^ extract_byte(key[1],3)) ^ extract_byte(key[0],3)
-
-#define q30(x)  q(0,q(0,q(1, x) ^ extract_byte(key[2],0)) ^ extract_byte(key[1],0)) ^ extract_byte(key[0],0)
-#define q31(x)  q(0,q(1,q(1, x) ^ extract_byte(key[2],1)) ^ extract_byte(key[1],1)) ^ extract_byte(key[0],1)
-#define q32(x)  q(1,q(0,q(0, x) ^ extract_byte(key[2],2)) ^ extract_byte(key[1],2)) ^ extract_byte(key[0],2)
-#define q33(x)  q(1,q(1,q(0, x) ^ extract_byte(key[2],3)) ^ extract_byte(key[1],3)) ^ extract_byte(key[0],3)
-
-#define q40(x)  q(0,q(0,q(1, q(1, x) ^ extract_byte(key[3],0)) ^ extract_byte(key[2],0)) ^ extract_byte(key[1],0)) ^ extract_byte(key[0],0)
-#define q41(x)  q(0,q(1,q(1, q(0, x) ^ extract_byte(key[3],1)) ^ extract_byte(key[2],1)) ^ extract_byte(key[1],1)) ^ extract_byte(key[0],1)
-#define q42(x)  q(1,q(0,q(0, q(0, x) ^ extract_byte(key[3],2)) ^ extract_byte(key[2],2)) ^ extract_byte(key[1],2)) ^ extract_byte(key[0],2)
-#define q43(x)  q(1,q(1,q(0, q(1, x) ^ extract_byte(key[3],3)) ^ extract_byte(key[2],3)) ^ extract_byte(key[1],3)) ^ extract_byte(key[0],3)
-
-static void gen_mk_tab(twofish_ctx *ctx, u_int32_t key[])
-{
-    u_int32_t  i;
-    u_int8_t  by;
-
-    u_int32_t *mk_tab = ctx->mk_tab;
-
-    switch(ctx->k_len)
-    {
-    case 2: for(i = 0; i < 256; ++i)
-            {
-                by = (u_int8_t)i;
-                mk_tab[0 + 4*i] = mds(0, q20(by)); mk_tab[1 + 4*i] = mds(1, q21(by));
-                mk_tab[2 + 4*i] = mds(2, q22(by)); mk_tab[3 + 4*i] = mds(3, q23(by));
-            }
-            break;
-
-    case 3: for(i = 0; i < 256; ++i)
-            {
-                by = (u_int8_t)i;
-                mk_tab[0 + 4*i] = mds(0, q30(by)); mk_tab[1 + 4*i] = mds(1, q31(by));
-                mk_tab[2 + 4*i] = mds(2, q32(by)); mk_tab[3 + 4*i] = mds(3, q33(by));
-            }
-            break;
-
-    case 4: for(i = 0; i < 256; ++i)
-            {
-                by = (u_int8_t)i;
-                mk_tab[0 + 4*i] = mds(0, q40(by)); mk_tab[1 + 4*i] = mds(1, q41(by));
-                mk_tab[2 + 4*i] = mds(2, q42(by)); mk_tab[3 + 4*i] = mds(3, q43(by));
-            }
-    }
-}
-
-#define g0_fun(x) ( mk_tab[0 + 4*extract_byte(x,0)] ^ mk_tab[1 + 4*extract_byte(x,1)] \
-                   ^ mk_tab[2 + 4*extract_byte(x,2)] ^ mk_tab[3 + 4*extract_byte(x,3)] )
-#define g1_fun(x) ( mk_tab[0 + 4*extract_byte(x,3)] ^ mk_tab[1 + 4*extract_byte(x,0)] \
-                   ^ mk_tab[2 + 4*extract_byte(x,1)] ^ mk_tab[3 + 4*extract_byte(x,2)] )
-
-
-/* The (12,8) Reed Soloman code has the generator polynomial
-
-  g(x) = x^4 + (a + 1/a) * x^3 + a * x^2 + (a + 1/a) * x + 1
-
-where the coefficients are in the finite field GF(2^8) with a
-modular polynomial a^8 + a^6 + a^3 + a^2 + 1. To generate the
-remainder we have to start with a 12th order polynomial with our
-eight input bytes as the coefficients of the 4th to 11th terms.
-That is:
-
-  m[7] * x^11 + m[6] * x^10 ... + m[0] * x^4 + 0 * x^3 +... + 0
-
-We then multiply the generator polynomial by m[7] * x^7 and subtract
-it - xor in GF(2^8) - from the above to eliminate the x^7 term (the
-artihmetic on the coefficients is done in GF(2^8). We then multiply
-the generator polynomial by x^6 * coeff(x^10) and use this to remove
-the x^10 term. We carry on in this way until the x^4 term is removed
-so that we are left with:
-
-  r[3] * x^3 + r[2] * x^2 + r[1] 8 x^1 + r[0]
-
-which give the resulting 4 bytes of the remainder. This is equivalent
-to the matrix multiplication in the Twofish description but much faster
-to implement.
-
-*/
-
-#define G_MOD   0x0000014d
-
-static u_int32_t mds_rem(u_int32_t p0, u_int32_t p1)
-{
-    u_int32_t  i, t, u;
-
-    for(i = 0; i < 8; ++i)
-    {
-        t = p1 >> 24;   // get most significant coefficient
-        p1 = (p1 << 8) | (p0 >> 24); p0 <<= 8;  // shift others up
-        // multiply t by a (the primitive element - i.e. left shift)
-
-        u = (t << 1);
-        if(t & 0x80)            // subtract modular polynomial on overflow
-            u ^= G_MOD;
-
-        p1 ^= t ^ (u << 16);    // remove t * (a * x^2 + 1)
-        u ^= (t >> 1);          // form u = a * t + t / a = t * (a + 1 / a);
-
-        if(t & 0x01)            // add the modular polynomial on underflow
-            u ^= G_MOD >> 1;
-
-        p1 ^= (u << 24) | (u << 8); // remove t * (a + 1/a) * (x^3 + x)
-    }
-
-    return p1;
-}
-
-/* initialise the key schedule from the user supplied key   */
-
-void twofish_set_key(twofish_ctx *ctx, const u_int8_t in_key[], int key_len_bits)
-{
-    u_int32_t  i, a, b, me_key[4], mo_key[4];
-    u_int32_t *l_key, *s_key;
-
-    l_key = ctx->l_key;
-    s_key = ctx->s_key;
-
-    ctx->k_len = key_len_bits / 64;   /* 2, 3 or 4 */
-
-    for(i = 0; i < ctx->k_len; ++i)
-    {
-        a = LE32(((const u_int32_t *)in_key)[i + i]);     me_key[i] = a;
-        b = LE32(((const u_int32_t *)in_key)[i + i + 1]); mo_key[i] = b;
-        s_key[ctx->k_len - i - 1] = mds_rem(a, b);
-    }
-
-    for(i = 0; i < 40; i += 2)
-    {
-        a = 0x01010101 * i; b = a + 0x01010101;
-        a = h_fun(ctx, a, me_key);
-        b = rotl(h_fun(ctx, b, mo_key), 8);
-        l_key[i] = a + b;
-        l_key[i + 1] = rotl(a + 2 * b, 9);
-    }
-
-    gen_mk_tab(ctx, s_key);
-}
-
-/* encrypt a block of text  */
-
-#define f_rnd(i)                                                    \
-    t1 = g1_fun(blk[1]); t0 = g0_fun(blk[0]);                       \
-    blk[2] = rotr(blk[2] ^ (t0 + t1 + l_key[4 * (i) + 8]), 1);      \
-    blk[3] = rotl(blk[3], 1) ^ (t0 + 2 * t1 + l_key[4 * (i) + 9]);  \
-    t1 = g1_fun(blk[3]); t0 = g0_fun(blk[2]);                       \
-    blk[0] = rotr(blk[0] ^ (t0 + t1 + l_key[4 * (i) + 10]), 1);     \
-    blk[1] = rotl(blk[1], 1) ^ (t0 + 2 * t1 + l_key[4 * (i) + 11])
-
-void twofish_encrypt(twofish_ctx *ctx, const u_int8_t in_blk[],
-             u_int8_t out_blk[])
-{
-    u_int32_t  t0, t1, blk[4];
-
-    u_int32_t *l_key = ctx->l_key;
-    u_int32_t *mk_tab = ctx->mk_tab;
-
-    blk[0] = LE32(((const u_int32_t *)in_blk)[0]) ^ l_key[0];
-    blk[1] = LE32(((const u_int32_t *)in_blk)[1]) ^ l_key[1];
-    blk[2] = LE32(((const u_int32_t *)in_blk)[2]) ^ l_key[2];
-    blk[3] = LE32(((const u_int32_t *)in_blk)[3]) ^ l_key[3];
-
-    f_rnd(0); f_rnd(1); f_rnd(2); f_rnd(3);
-    f_rnd(4); f_rnd(5); f_rnd(6); f_rnd(7);
-
-    ((u_int32_t *)out_blk)[0] = LE32(blk[2] ^ l_key[4]);
-    ((u_int32_t *)out_blk)[1] = LE32(blk[3] ^ l_key[5]);
-    ((u_int32_t *)out_blk)[2] = LE32(blk[0] ^ l_key[6]);
-    ((u_int32_t *)out_blk)[3] = LE32(blk[1] ^ l_key[7]);
-}
-
-
-/* decrypt a block of text  */
-
-#define i_rnd(i)                                                        \
-        t1 = g1_fun(blk[1]); t0 = g0_fun(blk[0]);                       \
-        blk[2] = rotl(blk[2], 1) ^ (t0 + t1 + l_key[4 * (i) + 10]);     \
-        blk[3] = rotr(blk[3] ^ (t0 + 2 * t1 + l_key[4 * (i) + 11]), 1); \
-        t1 = g1_fun(blk[3]); t0 = g0_fun(blk[2]);                       \
-        blk[0] = rotl(blk[0], 1) ^ (t0 + t1 + l_key[4 * (i) +  8]);     \
-        blk[1] = rotr(blk[1] ^ (t0 + 2 * t1 + l_key[4 * (i) +  9]), 1)
-
-void twofish_decrypt(twofish_ctx *ctx, const u_int8_t in_blk[], u_int8_t out_blk[])
-{
-    u_int32_t  t0, t1, blk[4];
-    u_int32_t *l_key = ctx->l_key;
-    u_int32_t *mk_tab = ctx->mk_tab;
-
-    blk[0] = LE32(((const u_int32_t *)in_blk)[0]) ^ l_key[4];
-    blk[1] = LE32(((const u_int32_t *)in_blk)[1]) ^ l_key[5];
-    blk[2] = LE32(((const u_int32_t *)in_blk)[2]) ^ l_key[6];
-    blk[3] = LE32(((const u_int32_t *)in_blk)[3]) ^ l_key[7];
-
-    i_rnd(7); i_rnd(6); i_rnd(5); i_rnd(4);
-    i_rnd(3); i_rnd(2); i_rnd(1); i_rnd(0);
-
-    ((u_int32_t *)out_blk)[0] = LE32(blk[2] ^ l_key[0]);
-    ((u_int32_t *)out_blk)[1] = LE32(blk[3] ^ l_key[1]);
-    ((u_int32_t *)out_blk)[2] = LE32(blk[0] ^ l_key[2]);
-    ((u_int32_t *)out_blk)[3] = LE32(blk[1] ^ l_key[3]);
-}
-
diff --git a/sys/crypto/twofish/twofish.h b/sys/crypto/twofish/twofish.h
deleted file mode 100644
index f9f0a37b49..0000000000
--- a/sys/crypto/twofish/twofish.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef __TWOFISH_H
-#define __TWOFISH_H
-
-typedef struct
-{
-        u_int32_t l_key[40];
-        u_int32_t s_key[4];
-        u_int32_t mk_tab[4*256];
-        u_int32_t k_len;
-} twofish_ctx;
-
-void twofish_set_key(twofish_ctx *ctx, const u_int8_t in_key[], int key_len);
-void twofish_encrypt(twofish_ctx *ctx, const u_int8_t in_blk[], u_int8_t out_blk[]);
-void twofish_decrypt(twofish_ctx *ctx, const u_int8_t in_blk[], u_int8_t out_blk[]);
-
-#endif /* __TWOFISH_H */
diff --git a/sys/dev/crypto/Makefile b/sys/dev/crypto/Makefile
index ad042d8baa..e6a6d452a8 100644
--- a/sys/dev/crypto/Makefile
+++ b/sys/dev/crypto/Makefile
@@ -1,4 +1,4 @@
-SUBDIR=	aesni cryptodev hifn padlock rdrand rndtest safe tpm ubsec
+SUBDIR=	rdrand rndtest tpm
 
 # Empty for concurrent build
 #
diff --git a/sys/dev/crypto/aesni/Makefile b/sys/dev/crypto/aesni/Makefile
deleted file mode 100644
index 6f55e9d588..0000000000
--- a/sys/dev/crypto/aesni/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-# $FreeBSD: src/sys/modules/aesni/Makefile,v 1.1 2010/07/23 11:00:46 kib Exp $
-
-KMOD=	aesni
-SRCS=	aesni.c aesni_wrap.c
-SRCS+=	aesencdec_${MACHINE_ARCH}.S aeskeys_${MACHINE_ARCH}.S
-SRCS+=	device_if.h bus_if.h cryptodev_if.h
-
-.include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/aesni/aesni.c b/sys/dev/crypto/aesni/aesni.c
deleted file mode 100644
index 3c4b7d2b1e..0000000000
--- a/sys/dev/crypto/aesni/aesni.c
+++ /dev/null
@@ -1,353 +0,0 @@
-/*-
- * 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.
- *
- * $FreeBSD: src/sys/crypto/aesni/aesni.c,v 1.3 2010/09/23 11:57:25 pjd Exp $
- */
-
-#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/spinlock.h>
-#include <sys/spinlock2.h>
-#include <sys/bus.h>
-#include <sys/uio.h>
-#include <dev/crypto/aesni/aesni.h>
-#include "cryptodev_if.h"
-
-struct aesni_softc {
-	int32_t cid;
-	uint32_t sid;
-	TAILQ_HEAD(aesni_sessions_head, aesni_session) sessions;
-	struct spinlock lock;
-};
-
-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);
-
-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, 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);
-	}
-	device_set_desc_copy(dev, "AES-CBC,AES-XTS");
-	return (0);
-}
-
-static int
-aesni_attach(device_t dev)
-{
-	struct aesni_softc *sc;
-
-	sc = device_get_softc(dev);
-	TAILQ_INIT(&sc->sessions);
-	sc->sid = 1;
-	sc->cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
-	if (sc->cid < 0) {
-		device_printf(dev, "Could not get crypto driver id.\n");
-		return (ENOMEM);
-	}
-
-	spin_init(&sc->lock, "aesniattach");
-	crypto_register(sc->cid, CRYPTO_AES_CBC, 0, 0);
-	crypto_register(sc->cid, CRYPTO_AES_XTS, 0, 0);
-	return (0);
-}
-
-static int
-aesni_detach(device_t dev)
-{
-	struct aesni_softc *sc;
-	struct aesni_session *ses;
-
-	sc = device_get_softc(dev);
-	spin_lock(&sc->lock);
-	TAILQ_FOREACH(ses, &sc->sessions, next) {
-		if (ses->used) {
-			spin_unlock(&sc->lock);
-			device_printf(dev,
-			    "Cannot detach, sessions still active.\n");
-			return (EBUSY);
-		}
-	}
-	while ((ses = TAILQ_FIRST(&sc->sessions)) != NULL) {
-		TAILQ_REMOVE(&sc->sessions, ses, next);
-		kfree(ses, M_AESNI);
-	}
-	spin_unlock(&sc->lock);
-	spin_uninit(&sc->lock);
-	crypto_unregister_all(sc->cid);
-	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;
-	int error;
-
-	if (sidp == NULL || cri == NULL)
-		return (EINVAL);
-
-	sc = device_get_softc(dev);
-	ses = NULL;
-	encini = NULL;
-	for (; cri != NULL; cri = cri->cri_next) {
-		switch (cri->cri_alg) {
-		case CRYPTO_AES_CBC:
-		case CRYPTO_AES_XTS:
-			if (encini != NULL)
-				return (EINVAL);
-			encini = cri;
-			break;
-		default:
-			return (EINVAL);
-		}
-	}
-	if (encini == NULL)
-		return (EINVAL);
-
-	spin_lock(&sc->lock);
-	/*
-	 * Free sessions goes first, so if first session is used, we need to
-	 * allocate one.
-	 */
-	ses = TAILQ_FIRST(&sc->sessions);
-	if (ses == NULL || ses->used) {
-		/*
-		 * Release the spinlock here, since the following
-		 * kmalloc(M_WAITOK) may block.  kmalloc(M_NOWAIT)
-		 * is not acceptable on this code path.
-		 */
-		spin_unlock(&sc->lock);
-
-		/*
-		 * aesni_session must be at least AESNI_ALIGN aligned.  To
-		 * make sure about that we always do a power-of-2 allocation.
-		 */
-		ses = kmalloc(sizeof(*ses), M_AESNI,
-		    M_WAITOK | M_ZERO | M_POWEROF2);
-		if ((uintptr_t)ses & (AESNI_ALIGN - 1)) {
-			panic("aesni: ses %p is not %d aligned",
-			    ses, AESNI_ALIGN);
-		}
-
-		spin_lock(&sc->lock);
-		ses->id = sc->sid++;
-	} else {
-		TAILQ_REMOVE(&sc->sessions, ses, next);
-	}
-	ses->used = 1;
-	TAILQ_INSERT_TAIL(&sc->sessions, ses, next);
-	spin_unlock(&sc->lock);
-	ses->algo = encini->cri_alg;
-
-	error = aesni_cipher_setup(ses, encini);
-	if (error != 0) {
-		spin_lock(&sc->lock);
-		aesni_freesession_locked(sc, ses);
-		spin_unlock(&sc->lock);
-		return (error);
-	}
-
-	*sidp = ses->id;
-	return (0);
-}
-
-static void
-aesni_freesession_locked(struct aesni_softc *sc, struct aesni_session *ses)
-{
-	uint32_t sid;
-
-	sid = ses->id;
-	TAILQ_REMOVE(&sc->sessions, ses, next);
-
-	bzero(ses, sizeof(*ses));
-	ses->id = sid;
-	TAILQ_INSERT_HEAD(&sc->sessions, ses, next);
-}
-
-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;
-	spin_lock(&sc->lock);
-	TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
-		if (ses->id == sid)
-			break;
-	}
-	if (ses == NULL) {
-		spin_unlock(&sc->lock);
-		return (EINVAL);
-	}
-	aesni_freesession_locked(sc, ses);
-	spin_unlock(&sc->lock);
-	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;
-	int error;
-
-	error = 0;
-	enccrd = NULL;
-
-	/* Sanity check. */
-	if (crp == NULL)
-		return (EINVAL);
-
-	if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
-		error = EINVAL;
-		goto out;
-	}
-
-	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;
-		default:
-			return (EINVAL);
-		}
-	}
-	if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
-		error = EINVAL;
-		goto out;
-	}
-
-	spin_lock(&sc->lock); /* XXX: was rd lock */
-	TAILQ_FOREACH_REVERSE(ses, &sc->sessions, aesni_sessions_head, next) {
-		if (ses->id == (crp->crp_sid & 0xffffffff))
-			break;
-	}
-	spin_unlock(&sc->lock); /* XXX: was rd lock */
-	if (ses == NULL) {
-		error = EINVAL;
-		goto out;
-	}
-
-	error = aesni_cipher_process(ses, enccrd, 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 uio *uio;
-	struct iovec *iov;
-	uint8_t *addr;
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF)
-		goto alloc;
-	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;
-	return (addr);
-
-alloc:
-	addr = kmalloc(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),
-
-	DEVMETHOD_END
-};
-
-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, NULL, NULL);
-MODULE_VERSION(aesni, 1);
-MODULE_DEPEND(aesni, crypto, 1, 1, 1);
diff --git a/sys/dev/crypto/aesni/aesni.h b/sys/dev/crypto/aesni/aesni.h
deleted file mode 100644
index 9f9be281be..0000000000
--- a/sys/dev/crypto/aesni/aesni.h
+++ /dev/null
@@ -1,103 +0,0 @@
-/*-
- * 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.
- *
- * $FreeBSD: src/sys/crypto/aesni/aesni.h,v 1.2 2010/09/23 11:57:25 pjd Exp $
- */
-
-#ifndef _AESNI_H_
-#define _AESNI_H_
-
-#include <sys/types.h>
-#include <sys/queue.h>
-
-#include <opencrypto/cryptodev.h>
-
-#if defined(__x86_64__)
-#include <machine/cpufunc.h>
-#include <machine/cputypes.h>
-#include <machine/md_var.h>
-#include <machine/specialreg.h>
-#endif
-
-#define AESNI_ALIGN	16
-
-#define	AES128_ROUNDS	10
-#define	AES192_ROUNDS	12
-#define	AES256_ROUNDS	14
-#define	AES_SCHED_LEN	((AES256_ROUNDS + 1) * AES_BLOCK_LEN)
-
-struct aesni_session {
-	uint8_t enc_schedule[AES_SCHED_LEN] __aligned(16);
-	uint8_t dec_schedule[AES_SCHED_LEN] __aligned(16);
-	uint8_t xts_schedule[AES_SCHED_LEN] __aligned(16);
-	uint8_t iv[AES_BLOCK_LEN];
-	int algo;
-	int rounds;
-	/* uint8_t *ses_ictx; */
-	/* uint8_t *ses_octx; */
-	/* int ses_mlen; */
-	int used;
-	uint32_t id;
-	TAILQ_ENTRY(aesni_session) next;
-#if 0
-	struct fpu_kern_ctx fpu_ctx;
-#endif
-};
-
-/*
- * Internal functions, implemented in assembler.
- */
-void aesni_enc(int rounds, const uint8_t *key_schedule,
-    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN],
-    const uint8_t iv[AES_BLOCK_LEN]);
-void aesni_dec(int rounds, const uint8_t *key_schedule,
-    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN],
-    const uint8_t iv[AES_BLOCK_LEN]);
-void aesni_set_enckey(const uint8_t *userkey, uint8_t *encrypt_schedule,
-    int number_of_rounds);
-void aesni_set_deckey(const uint8_t *encrypt_schedule,
-    uint8_t *decrypt_schedule, int number_of_rounds);
-
-/*
- * Slightly more public interfaces.
- */
-void aesni_encrypt_cbc(int rounds, const void *key_schedule, size_t len,
-    const uint8_t *from, uint8_t *to, const uint8_t iv[AES_BLOCK_LEN]);
-void aesni_decrypt_cbc(int rounds, const void *key_schedule, size_t len,
-    const uint8_t *from, const uint8_t iv[AES_BLOCK_LEN]);
-void aesni_encrypt_ecb(int rounds, const void *key_schedule, size_t len,
-    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN]);
-void aesni_decrypt_ecb(int rounds, const void *key_schedule, size_t len,
-    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN]);
-
-int aesni_cipher_setup(struct aesni_session *ses,
-    struct cryptoini *encini);
-int aesni_cipher_process(struct aesni_session *ses,
-    struct cryptodesc *enccrd, struct cryptop *crp);
-
-uint8_t *aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
-    int *allocated);
-
-#endif
diff --git a/sys/dev/crypto/aesni/aesni_wrap.c b/sys/dev/crypto/aesni/aesni_wrap.c
deleted file mode 100644
index d1e32851b3..0000000000
--- a/sys/dev/crypto/aesni/aesni_wrap.c
+++ /dev/null
@@ -1,331 +0,0 @@
-/*-
- * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
- * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@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.
- *
- * $FreeBSD: src/sys/crypto/aesni/aesni_wrap.c,v 1.7 2010/11/27 15:41:44 kib Exp $
- */
-
-#include <sys/param.h>
-#include <sys/libkern.h>
-#include <sys/malloc.h>
-#include <sys/proc.h>
-#include <sys/systm.h>
-#include <dev/crypto/aesni/aesni.h>
-
-MALLOC_DECLARE(M_AESNI);
-
-void
-aesni_encrypt_cbc(int rounds, const void *key_schedule, size_t len,
-    const uint8_t *from, uint8_t *to, const uint8_t iv[AES_BLOCK_LEN])
-{
-	const uint8_t *ivp;
-	size_t i;
-
-	len /= AES_BLOCK_LEN;
-	ivp = iv;
-	for (i = 0; i < len; i++) {
-		aesni_enc(rounds - 1, key_schedule, from, to, ivp);
-		ivp = to;
-		from += AES_BLOCK_LEN;
-		to += AES_BLOCK_LEN;
-	}
-}
-
-void
-aesni_encrypt_ecb(int rounds, const void *key_schedule, size_t len,
-    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN])
-{
-	size_t i;
-
-	len /= AES_BLOCK_LEN;
-	for (i = 0; i < len; i++) {
-		aesni_enc(rounds - 1, key_schedule, from, to, NULL);
-		from += AES_BLOCK_LEN;
-		to += AES_BLOCK_LEN;
-	}
-}
-
-void
-aesni_decrypt_ecb(int rounds, const void *key_schedule, size_t len,
-    const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN])
-{
-	size_t i;
-
-	len /= AES_BLOCK_LEN;
-	for (i = 0; i < len; i++) {
-		aesni_dec(rounds - 1, key_schedule, from, to, NULL);
-		from += AES_BLOCK_LEN;
-		to += AES_BLOCK_LEN;
-	}
-}
-
-#define	AES_XTS_BLOCKSIZE	16
-#define	AES_XTS_IVSIZE		8
-#define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
-
-static void
-aesni_crypt_xts_block(int rounds, const void *key_schedule, uint8_t *tweak,
-    const uint8_t *from, uint8_t *to, int do_encrypt)
-{
-	uint8_t block[AES_XTS_BLOCKSIZE];
-	u_int i, carry_in, carry_out;
-
-	for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
-		block[i] = from[i] ^ tweak[i];
-
-	if (do_encrypt)
-		aesni_enc(rounds - 1, key_schedule, block, to, NULL);
-	else
-		aesni_dec(rounds - 1, key_schedule, block, to, NULL);
-
-	for (i = 0; i < AES_XTS_BLOCKSIZE; i++)
-		to[i] ^= tweak[i];
-
-	/* Exponentiate tweak. */
-	carry_in = 0;
-	for (i = 0; i < AES_XTS_BLOCKSIZE; i++) {
-		carry_out = tweak[i] & 0x80;
-		tweak[i] = (tweak[i] << 1) | (carry_in ? 1 : 0);
-		carry_in = carry_out;
-	}
-	if (carry_in)
-		tweak[0] ^= AES_XTS_ALPHA;
-	bzero(block, sizeof(block));
-}
-
-static void
-aesni_crypt_xts(int rounds, const void *data_schedule,
-    const void *tweak_schedule, size_t len, const uint8_t *from, uint8_t *to,
-    const uint8_t iv[AES_BLOCK_LEN], int do_encrypt)
-{
-	uint8_t tweak[AES_XTS_BLOCKSIZE];
-	uint64_t blocknum;
-	size_t i;
-
-	/*
-	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
-	 * of a 64-bit block number which we allow to be passed in directly.
-	 */
-	bcopy(iv, &blocknum, AES_XTS_IVSIZE);
-	for (i = 0; i < AES_XTS_IVSIZE; i++) {
-		tweak[i] = blocknum & 0xff;
-		blocknum >>= 8;
-	}
-	/* Last 64 bits of IV are always zero. */
-	bzero(tweak + AES_XTS_IVSIZE, AES_XTS_IVSIZE);
-	aesni_enc(rounds - 1, tweak_schedule, tweak, tweak, NULL);
-
-	len /= AES_XTS_BLOCKSIZE;
-	for (i = 0; i < len; i++) {
-		aesni_crypt_xts_block(rounds, data_schedule, tweak, from, to,
-		    do_encrypt);
-		from += AES_XTS_BLOCKSIZE;
-		to += AES_XTS_BLOCKSIZE;
-	}
-
-	bzero(tweak, sizeof(tweak));
-}
-
-static void
-aesni_encrypt_xts(int rounds, const void *data_schedule,
-    const void *tweak_schedule, size_t len, const uint8_t *from, uint8_t *to,
-    const uint8_t iv[AES_BLOCK_LEN])
-{
-
-	aesni_crypt_xts(rounds, data_schedule, tweak_schedule, len, from, to,
-	    iv, 1);
-}
-
-static void
-aesni_decrypt_xts(int rounds, const void *data_schedule,
-    const void *tweak_schedule, size_t len, const uint8_t *from, uint8_t *to,
-    const uint8_t iv[AES_BLOCK_LEN])
-{
-
-	aesni_crypt_xts(rounds, data_schedule, tweak_schedule, len, from, to,
-	    iv, 0);
-}
-
-static int
-aesni_cipher_setup_common(struct aesni_session *ses, const uint8_t *key,
-    int keylen)
-{
-
-	switch (ses->algo) {
-	case CRYPTO_AES_CBC:
-		switch (keylen) {
-		case 128:
-			ses->rounds = AES128_ROUNDS;
-			break;
-		case 192:
-			ses->rounds = AES192_ROUNDS;
-			break;
-		case 256:
-			ses->rounds = AES256_ROUNDS;
-			break;
-		default:
-			return (EINVAL);
-		}
-		break;
-	case CRYPTO_AES_XTS:
-		switch (keylen) {
-		case 256:
-			ses->rounds = AES128_ROUNDS;
-			break;
-		case 512:
-			ses->rounds = AES256_ROUNDS;
-			break;
-		default:
-			return (EINVAL);
-		}
-		break;
-	default:
-		return (EINVAL);
-	}
-
-	aesni_set_enckey(key, ses->enc_schedule, ses->rounds);
-	aesni_set_deckey(ses->enc_schedule, ses->dec_schedule, ses->rounds);
-	if (ses->algo == CRYPTO_AES_CBC)
-		karc4random_buf(ses->iv, sizeof(ses->iv));
-	else /* if (ses->algo == CRYPTO_AES_XTS) */ {
-		aesni_set_enckey(key + keylen / 16, ses->xts_schedule,
-		    ses->rounds);
-	}
-
-	return (0);
-}
-
-int
-aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini)
-{
-	int error = 0;
-#if 0
-	struct thread *td;
-	int saved_ctx;
-#endif
-
-#if 0
-	td = curthread;
-	if (!is_fpu_kern_thread(0)) {
-		error = fpu_kern_enter(td, &ses->fpu_ctx, FPU_KERN_NORMAL);
-		saved_ctx = 1;
-	} else {
-		error = 0;
-		saved_ctx = 0;
-	}
-#endif
-	if (error == 0) {
-		error = aesni_cipher_setup_common(ses, encini->cri_key,
-		    encini->cri_klen);
-#if 0
-		if (saved_ctx)
-			fpu_kern_leave(td, &ses->fpu_ctx);
-#endif
-	}
-	return (error);
-}
-
-int
-aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
-    struct cryptop *crp)
-{
-	uint8_t *buf;
-	int error = 0, allocated;
-#if 0
-	struct thread *td;
-	int saved_ctx;
-#endif
-
-	buf = aesni_cipher_alloc(enccrd, crp, &allocated);
-	if (buf == NULL)
-		return (ENOMEM);
-
-#if 0
-	td = curthread;
-	if (!is_fpu_kern_thread(0)) {
-		error = fpu_kern_enter(td, &ses->fpu_ctx, FPU_KERN_NORMAL);
-		if (error != 0)
-			goto out;
-		saved_ctx = 1;
-	} else {
-		saved_ctx = 0;
-		error = 0;
-	}
-#endif
-
-	if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
-		error = aesni_cipher_setup_common(ses, enccrd->crd_key,
-		    enccrd->crd_klen);
-		if (error != 0)
-			goto out;
-	}
-
-	if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
-		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
-			bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN);
-		if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
-			crypto_copyback(crp->crp_flags, crp->crp_buf,
-			    enccrd->crd_inject, AES_BLOCK_LEN, ses->iv);
-		if (ses->algo == CRYPTO_AES_CBC) {
-			aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
-			    enccrd->crd_len, buf, buf, ses->iv);
-		} else /* if (ses->algo == CRYPTO_AES_XTS) */ {
-			aesni_encrypt_xts(ses->rounds, ses->enc_schedule,
-			    ses->xts_schedule, enccrd->crd_len, buf, buf,
-			    ses->iv);
-		}
-	} else {
-		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
-			bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN);
-		else
-			crypto_copydata(crp->crp_flags, crp->crp_buf,
-			    enccrd->crd_inject, AES_BLOCK_LEN, ses->iv);
-		if (ses->algo == CRYPTO_AES_CBC) {
-			aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
-			    enccrd->crd_len, buf, ses->iv);
-		} else /* if (ses->algo == CRYPTO_AES_XTS) */ {
-			aesni_decrypt_xts(ses->rounds, ses->dec_schedule,
-			    ses->xts_schedule, enccrd->crd_len, buf, buf,
-			    ses->iv);
-		}
-	}
-#if 0
-	if (saved_ctx)
-		fpu_kern_leave(td, &ses->fpu_ctx);
-#endif
-	if (allocated)
-		crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
-		    enccrd->crd_len, buf);
-	if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0)
-		crypto_copydata(crp->crp_flags, crp->crp_buf,
-		    enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN,
-		    AES_BLOCK_LEN, ses->iv);
- out:
-	if (allocated) {
-		bzero(buf, enccrd->crd_len);
-		kfree(buf, M_AESNI);
-	}
-	return (error);
-}
diff --git a/sys/dev/crypto/cryptodev/Makefile b/sys/dev/crypto/cryptodev/Makefile
deleted file mode 100644
index bdaeda9f11..0000000000
--- a/sys/dev/crypto/cryptodev/Makefile
+++ /dev/null
@@ -1,7 +0,0 @@
-# $FreeBSD: src/sys/modules/cryptodev/Makefile,v 1.1.2.1 2002/11/21 23:38:46 sam Exp $
-
-.PATH:	${.CURDIR}/../../../opencrypto
-KMOD	= cryptodev
-SRCS	= cryptodev.c device_if.h bus_if.h
-
-.include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/hifn/Makefile b/sys/dev/crypto/hifn/Makefile
deleted file mode 100644
index f56d669711..0000000000
--- a/sys/dev/crypto/hifn/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-# $FreeBSD: src/sys/modules/hifn/Makefile,v 1.2.2.2 2003/06/05 04:13:47 sam Exp $
-
-KMOD	= hifn
-SRCS	= hifn7751.c opt_hifn.h
-SRCS   += device_if.h bus_if.h pci_if.h
-SRCS   += cryptodev_if.h
-
-.if !defined(KERNBUILDDIR)
-opt_hifn.h:
-	echo "#define HIFN_DEBUG 1" > ${.TARGET}
-.endif
-
-.include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/hifn/hifn7751.c b/sys/dev/crypto/hifn/hifn7751.c
deleted file mode 100644
index c7bb6f59ac..0000000000
--- a/sys/dev/crypto/hifn/hifn7751.c
+++ /dev/null
@@ -1,2938 +0,0 @@
-/* $FreeBSD: src/sys/dev/hifn/hifn7751.c,v 1.5.2.5 2003/06/04 17:56:59 sam Exp $ */
-/* $OpenBSD: hifn7751.c,v 1.120 2002/05/17 00:33:34 deraadt Exp $	*/
-
-/*
- * Invertex AEON / Hifn 7751 driver
- * Copyright (c) 1999 Invertex Inc. All rights reserved.
- * Copyright (c) 1999 Theo de Raadt
- * Copyright (c) 2000-2001 Network Security Technologies, Inc.
- *			http://www.netsec.net
- * Copyright (c) 2003 Hifn Inc.
- *
- * This driver is based on a previous driver by Invertex, for which they
- * requested:  Please send any comments, feedback, bug-fixes, or feature
- * requests to software@invertex.com.
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-
-/*
- * Driver for various Hifn encryption processors.
- */
-#include "opt_hifn.h"
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/proc.h>
-#include <sys/errno.h>
-#include <sys/malloc.h>
-#include <sys/kernel.h>
-#include <sys/mbuf.h>
-#include <sys/sysctl.h>
-#include <sys/bus.h>
-#include <sys/rman.h>
-#include <sys/random.h>
-#include <sys/uio.h>
-
-#include <vm/vm.h>
-#include <vm/pmap.h>
-
-#include <machine/clock.h>
-#include <opencrypto/cryptodev.h>
-
-#include "cryptodev_if.h"
-
-#include <bus/pci/pcivar.h>
-#include <bus/pci/pcireg.h>
-
-#ifdef HIFN_RNDTEST
-#include "../rndtest/rndtest.h"
-#endif
-#include "hifn7751reg.h"
-#include "hifn7751var.h"
-
-/*
- * Prototypes and count for the pci_device structure
- */
-static	int hifn_probe(device_t);
-static	int hifn_attach(device_t);
-static	int hifn_detach(device_t);
-static	int hifn_suspend(device_t);
-static	int hifn_resume(device_t);
-static	void hifn_shutdown(device_t);
-
-static	void hifn_reset_board(struct hifn_softc *, int);
-static	void hifn_reset_puc(struct hifn_softc *);
-static	void hifn_puc_wait(struct hifn_softc *);
-static	int hifn_enable_crypto(struct hifn_softc *);
-static	void hifn_set_retry(struct hifn_softc *sc);
-static	void hifn_init_dma(struct hifn_softc *);
-static	void hifn_init_pci_registers(struct hifn_softc *);
-static	int hifn_sramsize(struct hifn_softc *);
-static	int hifn_dramsize(struct hifn_softc *);
-static	int hifn_ramtype(struct hifn_softc *);
-static	void hifn_sessions(struct hifn_softc *);
-static	void hifn_intr(void *);
-static	u_int hifn_write_command(struct hifn_command *, u_int8_t *);
-static	u_int32_t hifn_next_signature(u_int32_t a, u_int cnt);
-static	int hifn_newsession(device_t, u_int32_t *, struct cryptoini *);
-static	int hifn_freesession(device_t, u_int64_t);
-static	int hifn_process(device_t, struct cryptop *, int);
-static	void hifn_callback(struct hifn_softc *, struct hifn_command *, u_int8_t *);
-static	int hifn_crypto(struct hifn_softc *, struct hifn_command *, struct cryptop *, int);
-static	int hifn_readramaddr(struct hifn_softc *, int, u_int8_t *);
-static	int hifn_writeramaddr(struct hifn_softc *, int, u_int8_t *);
-static	int hifn_dmamap_load_src(struct hifn_softc *, struct hifn_command *);
-static	int hifn_dmamap_load_dst(struct hifn_softc *, struct hifn_command *);
-static	int hifn_init_pubrng(struct hifn_softc *);
-#ifndef HIFN_NO_RNG
-static	void hifn_rng(void *);
-#endif
-static	void hifn_tick(void *);
-static	void hifn_abort(struct hifn_softc *);
-static	void hifn_alloc_slot(struct hifn_softc *, int *, int *, int *, int *);
-
-static	void hifn_write_reg_0(struct hifn_softc *, bus_size_t, u_int32_t);
-static	void hifn_write_reg_1(struct hifn_softc *, bus_size_t, u_int32_t);
-
-
-static device_method_t hifn_methods[] = {
-	/* Device interface */
-	DEVMETHOD(device_probe,		hifn_probe),
-	DEVMETHOD(device_attach,	hifn_attach),
-	DEVMETHOD(device_detach,	hifn_detach),
-	DEVMETHOD(device_suspend,	hifn_suspend),
-	DEVMETHOD(device_resume,	hifn_resume),
-	DEVMETHOD(device_shutdown,	hifn_shutdown),
-
-	/* bus interface */
-	DEVMETHOD(bus_print_child,	bus_generic_print_child),
-	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
-
-	/* crypto device methods */
-	DEVMETHOD(cryptodev_newsession,	hifn_newsession),
-	DEVMETHOD(cryptodev_freesession,hifn_freesession),
-	DEVMETHOD(cryptodev_process,	hifn_process),
-
-	DEVMETHOD_END
-};
-static driver_t hifn_driver = {
-	"hifn",
-	hifn_methods,
-	sizeof (struct hifn_softc)
-};
-static devclass_t hifn_devclass;
-
-DECLARE_DUMMY_MODULE(hifn);
-DRIVER_MODULE(hifn, pci, hifn_driver, hifn_devclass, NULL, NULL);
-MODULE_DEPEND(hifn, crypto, 1, 1, 1);
-#ifdef HIFN_RNDTEST
-MODULE_DEPEND(hifn, rndtest, 1, 1, 1);
-#endif
-
-static __inline__ u_int32_t
-READ_REG_0(struct hifn_softc *sc, bus_size_t reg)
-{
-    u_int32_t v = bus_space_read_4(sc->sc_st0, sc->sc_sh0, reg);
-    sc->sc_bar0_lastreg = (bus_size_t) -1;
-    return (v);
-}
-#define	WRITE_REG_0(sc, reg, val)	hifn_write_reg_0(sc, reg, val)
-
-static __inline__ u_int32_t
-READ_REG_1(struct hifn_softc *sc, bus_size_t reg)
-{
-    u_int32_t v = bus_space_read_4(sc->sc_st1, sc->sc_sh1, reg);
-    sc->sc_bar1_lastreg = (bus_size_t) -1;
-    return (v);
-}
-#define	WRITE_REG_1(sc, reg, val)	hifn_write_reg_1(sc, reg, val)
-
-SYSCTL_NODE(_hw, OID_AUTO, hifn, CTLFLAG_RD, 0, "Hifn driver parameters");
-
-#ifdef HIFN_DEBUG
-static	int hifn_debug = 0;
-SYSCTL_INT(_hw_hifn, OID_AUTO, debug, CTLFLAG_RW, &hifn_debug,
-	    0, "control debugging msgs");
-#endif
-
-static	struct hifn_stats hifnstats;
-SYSCTL_STRUCT(_hw_hifn, OID_AUTO, stats, CTLFLAG_RD, &hifnstats,
-	    hifn_stats, "driver statistics");
-static	int hifn_maxbatch = 1;
-SYSCTL_INT(_hw_hifn, OID_AUTO, maxbatch, CTLFLAG_RW, &hifn_maxbatch,
-	    0, "max ops to batch w/o interrupt");
-
-/*
- * Probe for a supported device.  The PCI vendor and device
- * IDs are used to detect devices we know how to handle.
- */
-static int
-hifn_probe(device_t dev)
-{
-	if (pci_get_vendor(dev) == PCI_VENDOR_INVERTEX &&
-	    pci_get_device(dev) == PCI_PRODUCT_INVERTEX_AEON)
-		return (0);
-	if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
-	    (pci_get_device(dev) == PCI_PRODUCT_HIFN_7751 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7951 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7956 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7811))
-		return (0);
-	if (pci_get_vendor(dev) == PCI_VENDOR_NETSEC &&
-	    pci_get_device(dev) == PCI_PRODUCT_NETSEC_7751)
-		return (0);
-	if (pci_get_vendor(dev) == PCI_VENDOR_HIFN) {
-		device_printf(dev,"device id = 0x%x\n", pci_get_device(dev) );
-		return (0);
-	}
-	return (ENXIO);
-}
-
-static void
-hifn_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
-{
-	bus_addr_t *paddr = (bus_addr_t*) arg;
-	*paddr = segs->ds_addr;
-}
-
-static const char*
-hifn_partname(struct hifn_softc *sc)
-{
-	/* XXX sprintf numbers when not decoded */
-	switch (pci_get_vendor(sc->sc_dev)) {
-	case PCI_VENDOR_HIFN:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_HIFN_6500:	return "Hifn 6500";
-		case PCI_PRODUCT_HIFN_7751:	return "Hifn 7751";
-		case PCI_PRODUCT_HIFN_7811:	return "Hifn 7811";
-		case PCI_PRODUCT_HIFN_7951:	return "Hifn 7951";
-		case PCI_PRODUCT_HIFN_7955:	return "Hifn 7955";
-		case PCI_PRODUCT_HIFN_7956:	return "Hifn 7956";
-		}
-		return "Hifn unknown-part";
-	case PCI_VENDOR_INVERTEX:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_INVERTEX_AEON:	return "Invertex AEON";
-		}
-		return "Invertex unknown-part";
-	case PCI_VENDOR_NETSEC:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_NETSEC_7751:	return "NetSec 7751";
-		}
-		return "NetSec unknown-part";
-	}
-	return "Unknown-vendor unknown-part";
-}
-
-static void
-default_harvest(struct rndtest_state *rsp, void *buf, u_int count)
-{
-	add_buffer_randomness_src(buf, count, RAND_SRC_HIFN);
-}
-
-static u_int
-checkmaxmin(device_t dev, const char *what, u_int v, u_int min, u_int max)
-{
-	if (v > max) {
-		device_printf(dev, "Warning, %s %u out of range, "
-		    "using max %u\n", what, v, max);
-		v = max;
-	} else if (v < min) {
-		device_printf(dev, "Warning, %s %u out of range, "
-                    "using min %u\n", what, v, min);
-		v = min;
-	}
-	return v;
-}
-
-/*
- * Select PLL configuration for 795x parts.  This is complicated in
- * that we cannot determine the optimal parameters without user input.
- * The reference clock is derived from an external clock through a
- * multiplier.  The external clock is either the host bus (i.e. PCI)
- * or an external clock generator.  When using the PCI bus we assume
- * the clock is either 33 or 66 MHz; for an external source we cannot
- * tell the speed.
- *
- * PLL configuration is done with a string: "pci" for PCI bus, or "ext"
- * for an external source, followed by the frequency.  We calculate
- * the appropriate multiplier and PLL register contents accordingly.
- * When no configuration is given we default to "pci66" since that
- * always will allow the card to work.  If a card is using the PCI
- * bus clock and in a 33MHz slot then it will be operating at half
- * speed until the correct information is provided.
- *
- * We use a default setting of "ext66" because according to Mike Ham
- * of HiFn, almost every board in existence has an external crystal
- * populated at 66Mhz. Using PCI can be a problem on modern motherboards,
- * because PCI33 can have clocks from 0 to 33Mhz, and some have
- * non-PCI-compliant spread-spectrum clocks, which can confuse the pll.
- */
-static void
-hifn_getpllconfig(device_t dev, u_int *pll)
-{
-	const char *pllspec;
-	u_int freq, mul, fl, fh;
-	u_int32_t pllconfig;
-	char *nxt;
-
-	if (resource_string_value("hifn", device_get_unit(dev),
-	    "pllconfig", &pllspec))
-		pllspec = "ext66";
-	fl = 33, fh = 66;
-	pllconfig = 0;
-	if (strncmp(pllspec, "ext", 3) == 0) {
-		pllspec += 3;
-		pllconfig |= HIFN_PLL_REF_SEL;
-		switch (pci_get_device(dev)) {
-		case PCI_PRODUCT_HIFN_7955:
-		case PCI_PRODUCT_HIFN_7956:
-			fl = 20, fh = 100;
-			break;
-#ifdef notyet
-		case PCI_PRODUCT_HIFN_7954:
-			fl = 20, fh = 66;
-			break;
-#endif
-		}
-	} else if (strncmp(pllspec, "pci", 3) == 0)
-		pllspec += 3;
-	freq = strtoul(pllspec, &nxt, 10);
-	if (nxt == pllspec)
-		freq = 66;
-	else
-		freq = checkmaxmin(dev, "frequency", freq, fl, fh);
-	/*
-	 * Calculate multiplier.  We target a Fck of 266 MHz,
-	 * allowing only even values, possibly rounded down.
-	 * Multipliers > 8 must set the charge pump current.
-	 */
-	mul = checkmaxmin(dev, "PLL divisor", (266 / freq) &~ 1, 2, 12);
-	pllconfig |= (mul / 2 - 1) << HIFN_PLL_ND_SHIFT;
-	if (mul > 8)
-		pllconfig |= HIFN_PLL_IS;
-	*pll = pllconfig;
-}
-
-/*
- * Attach an interface that successfully probed.
- */
-static int 
-hifn_attach(device_t dev)
-{
-	struct hifn_softc *sc = device_get_softc(dev);
-	u_int32_t cmd;
-	caddr_t kva;
-	int rseg, rid;
-	char rbase;
-	u_int16_t ena, rev;
-
-	KASSERT(sc != NULL, ("hifn_attach: null software carrier!"));
-	bzero(sc, sizeof (*sc));
-	sc->sc_dev = dev;
-
-	lockinit(&sc->sc_lock, __DECONST(char *, device_get_nameunit(dev)),
-	    0, LK_CANRECURSE);
-
-	/* XXX handle power management */
-
-	/*
-	 * The 7951 and 795x have a random number generator and
-	 * public key support; note this.
-	 */
-	if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
-	    (pci_get_device(dev) == PCI_PRODUCT_HIFN_7951 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7956))
-		sc->sc_flags = HIFN_HAS_RNG | HIFN_HAS_PUBLIC;
-	/*
-	 * The 7811 has a random number generator and
-	 * we also note it's identity 'cuz of some quirks.
-	 */
-	if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
-	    pci_get_device(dev) == PCI_PRODUCT_HIFN_7811)
-		sc->sc_flags |= HIFN_IS_7811 | HIFN_HAS_RNG;
-
-	/*
-	 * The 795x parts support AES.
-	 */
-	if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
-	    (pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
-	     pci_get_device(dev) == PCI_PRODUCT_HIFN_7956)) {
-		sc->sc_flags |= HIFN_IS_7956 | HIFN_HAS_AES;
-		/*
-		 * Select PLL configuration.  This depends on the
-		 * bus and board design and must be manually configured
-		 * if the default setting is unacceptable.
-		 */
-		hifn_getpllconfig(dev, &sc->sc_pllconfig);
-	}
-
-	/*
-	 * Configure support for memory-mapped access to
-	 * registers and for DMA operations.
-	 */
-#define	PCIM_ENA	(PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN)
-	cmd = pci_read_config(dev, PCIR_COMMAND, 4);
-	cmd |= PCIM_ENA;
-	pci_write_config(dev, PCIR_COMMAND, cmd, 4);
-	cmd = pci_read_config(dev, PCIR_COMMAND, 4);
-	if ((cmd & PCIM_ENA) != PCIM_ENA) {
-		device_printf(dev, "failed to enable %s\n",
-			(cmd & PCIM_ENA) == 0 ?
-				"memory mapping & bus mastering" :
-			(cmd & PCIM_CMD_MEMEN) == 0 ?
-				"memory mapping" : "bus mastering");
-		goto fail_pci;
-	}
-#undef PCIM_ENA
-
-	/*
-	 * Setup PCI resources. Note that we record the bus
-	 * tag and handle for each register mapping, this is
-	 * used by the READ_REG_0, WRITE_REG_0, READ_REG_1,
-	 * and WRITE_REG_1 macros throughout the driver.
-	 */
-	rid = HIFN_BAR0;
-	sc->sc_bar0res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
-			 		    0, ~0, 1, RF_ACTIVE);
-	if (sc->sc_bar0res == NULL) {
-		device_printf(dev, "cannot map bar%d register space\n", 0);
-		goto fail_pci;
-	}
-	sc->sc_st0 = rman_get_bustag(sc->sc_bar0res);
-	sc->sc_sh0 = rman_get_bushandle(sc->sc_bar0res);
-	sc->sc_bar0_lastreg = (bus_size_t) -1;
-
-	rid = HIFN_BAR1;
-	sc->sc_bar1res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
-					    0, ~0, 1, RF_ACTIVE);
-	if (sc->sc_bar1res == NULL) {
-		device_printf(dev, "cannot map bar%d register space\n", 1);
-		goto fail_io0;
-	}
-	sc->sc_st1 = rman_get_bustag(sc->sc_bar1res);
-	sc->sc_sh1 = rman_get_bushandle(sc->sc_bar1res);
-	sc->sc_bar1_lastreg = (bus_size_t) -1;
-
-	hifn_set_retry(sc);
-
-	/*
-	 * Setup the area where the Hifn DMA's descriptors
-	 * and associated data structures.
-	 */
-	if (bus_dma_tag_create(NULL,			/* parent */
-			       1, 0,			/* alignment,boundary */
-			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
-			       BUS_SPACE_MAXADDR,	/* highaddr */
-			       HIFN_MAX_DMALEN,		/* maxsize */
-			       MAX_SCATTER,		/* nsegments */
-			       HIFN_MAX_SEGLEN,		/* maxsegsize */
-			       BUS_DMA_ALLOCNOW,	/* flags */
-			       &sc->sc_dmat)) {
-		device_printf(dev, "cannot allocate DMA tag\n");
-		goto fail_io1;
-	}
-	if (bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_dmamap)) {
-		device_printf(dev, "cannot create dma map\n");
-		bus_dma_tag_destroy(sc->sc_dmat);
-		goto fail_io1;
-	}
-	if (bus_dmamem_alloc(sc->sc_dmat, (void**) &kva, BUS_DMA_NOWAIT, &sc->sc_dmamap)) {
-		device_printf(dev, "cannot alloc dma buffer\n");
-		bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
-		bus_dma_tag_destroy(sc->sc_dmat);
-		goto fail_io1;
-	}
-	if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, kva,
-			     sizeof (*sc->sc_dma),
-			     hifn_dmamap_cb, &sc->sc_dma_physaddr,
-			     BUS_DMA_NOWAIT)) {
-		device_printf(dev, "cannot load dma map\n");
-		bus_dmamem_free(sc->sc_dmat, kva, sc->sc_dmamap);
-		bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
-		bus_dma_tag_destroy(sc->sc_dmat);
-		goto fail_io1;
-	}
-	sc->sc_dma = (struct hifn_dma *)kva;
-	bzero(sc->sc_dma, sizeof(*sc->sc_dma));
-
-	KASSERT(sc->sc_st0 != 0, ("hifn_attach: null bar0 tag!"));
-	KASSERT(sc->sc_sh0 != 0, ("hifn_attach: null bar0 handle!"));
-	KASSERT(sc->sc_st1 != 0, ("hifn_attach: null bar1 tag!"));
-	KASSERT(sc->sc_sh1 != 0, ("hifn_attach: null bar1 handle!"));
-
-	/*
-	 * Reset the board and do the ``secret handshake''
-	 * to enable the crypto support.  Then complete the
-	 * initialization procedure by setting up the interrupt
-	 * and hooking in to the system crypto support so we'll
-	 * get used for system services like the crypto device,
-	 * IPsec, RNG device, etc.
-	 */
-	hifn_reset_board(sc, 0);
-
-	if (hifn_enable_crypto(sc) != 0) {
-		device_printf(dev, "crypto enabling failed\n");
-		goto fail_mem;
-	}
-	hifn_reset_puc(sc);
-
-	hifn_init_dma(sc);
-	hifn_init_pci_registers(sc);
-
-	/* XXX can't dynamically determine ram type for 795x; force dram */
-	if (sc->sc_flags & HIFN_IS_7956)
-		sc->sc_drammodel = 1;
-	else if (hifn_ramtype(sc))
-		goto fail_mem;
-
-	if (sc->sc_drammodel == 0)
-		hifn_sramsize(sc);
-	else
-		hifn_dramsize(sc);
-
-	/*
-	 * Workaround for NetSec 7751 rev A: half ram size because two
-	 * of the address lines were left floating
-	 */
-	if (pci_get_vendor(dev) == PCI_VENDOR_NETSEC &&
-	    pci_get_device(dev) == PCI_PRODUCT_NETSEC_7751 &&
-	    pci_get_revid(dev) == 0x61)	/*XXX???*/
-		sc->sc_ramsize >>= 1;
-
-	/*
-	 * Arrange the interrupt line.
-	 */
-	rid = 0;
-	sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
-					0, ~0, 1, RF_SHAREABLE|RF_ACTIVE);
-	if (sc->sc_irq == NULL) {
-		device_printf(dev, "could not map interrupt\n");
-		goto fail_mem;
-	}
-	/*
-	 * NB: Network code assumes we are blocked with splimp()
-	 *     so make sure the IRQ is marked appropriately.
-	 */
-	if (bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE,
-			   hifn_intr, sc,
-			   &sc->sc_intrhand, NULL)) {
-		device_printf(dev, "could not setup interrupt\n");
-		goto fail_intr2;
-	}
-
-	hifn_sessions(sc);
-
-	/*
-	 * NB: Keep only the low 16 bits; this masks the chip id
-	 *     from the 7951.
-	 */
-	rev = READ_REG_1(sc, HIFN_1_REVID) & 0xffff;
-
-	rseg = sc->sc_ramsize / 1024;
-	rbase = 'K';
-	if (sc->sc_ramsize >= (1024 * 1024)) {
-		rbase = 'M';
-		rseg /= 1024;
-	}
-	device_printf(sc->sc_dev, "%s, rev %u, %d%cB %cram, %u sessions\n",
-		hifn_partname(sc), rev,
-		rseg, rbase, sc->sc_drammodel ? 'd' : 's',
-		sc->sc_maxses);
-
-	if (sc->sc_flags & HIFN_IS_7956)
-		kprintf(", pll=0x%x<%s clk, %ux mult>",
-			sc->sc_pllconfig,
-			sc->sc_pllconfig & HIFN_PLL_REF_SEL ? "ext" : "pci",
-			2 + 2*((sc->sc_pllconfig & HIFN_PLL_ND) >> 11));
-	kprintf("\n");
-
-	sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
-	if (sc->sc_cid < 0) {
-		device_printf(dev, "could not get crypto driver id\n");
-		goto fail_intr;
-	}
-
-	WRITE_REG_0(sc, HIFN_0_PUCNFG,
-	    READ_REG_0(sc, HIFN_0_PUCNFG) | HIFN_PUCNFG_CHIPID);
-	ena = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
-
-	switch (ena) {
-	case HIFN_PUSTAT_ENA_2:
-		crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
-		crypto_register(sc->sc_cid, CRYPTO_ARC4, 0, 0);
-		if (sc->sc_flags & HIFN_HAS_AES)
-			crypto_register(sc->sc_cid, CRYPTO_AES_CBC,  0, 0);
-		/*FALLTHROUGH*/
-	case HIFN_PUSTAT_ENA_1:
-		crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0);
-		crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0);
-		crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
-		crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
-		crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
-		break;
-	}
-
-	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-
-	if (sc->sc_flags & (HIFN_HAS_PUBLIC | HIFN_HAS_RNG))
-		hifn_init_pubrng(sc);
-
-	/* NB: 1 means the callout runs w/o Giant locked */
-	callout_init_mp(&sc->sc_tickto);
-	callout_reset(&sc->sc_tickto, hz, hifn_tick, sc);
-
-	return (0);
-
-fail_intr:
-	bus_teardown_intr(dev, sc->sc_irq, sc->sc_intrhand);
-fail_intr2:
-	/* XXX don't store rid */
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
-fail_mem:
-	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
-	bus_dmamem_free(sc->sc_dmat, sc->sc_dma, sc->sc_dmamap);
-	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
-	bus_dma_tag_destroy(sc->sc_dmat);
-
-	/* Turn off DMA polling */
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
-	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
-fail_io1:
-	bus_release_resource(dev, SYS_RES_MEMORY, HIFN_BAR1, sc->sc_bar1res);
-fail_io0:
-	bus_release_resource(dev, SYS_RES_MEMORY, HIFN_BAR0, sc->sc_bar0res);
-fail_pci:
-	lockuninit(&sc->sc_lock);
-	return (ENXIO);
-}
-
-/*
- * Detach an interface that successfully probed.
- */
-static int 
-hifn_detach(device_t dev)
-{
-	struct hifn_softc *sc = device_get_softc(dev);
-
-	KASSERT(sc != NULL, ("hifn_detach: null software carrier!"));
-
-	/* disable interrupts */
-	WRITE_REG_1(sc, HIFN_1_DMA_IER, 0);
-
-	/*XXX other resources */
-	callout_stop(&sc->sc_tickto);
-	callout_stop(&sc->sc_rngto);
-#ifdef HIFN_RNDTEST
-	if (sc->sc_rndtest)
-		rndtest_detach(sc->sc_rndtest);
-#endif
-
-	/* Turn off DMA polling */
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
-	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
-
-	crypto_unregister_all(sc->sc_cid);
-
-	bus_generic_detach(dev);	/*XXX should be no children, right? */
-
-	bus_teardown_intr(dev, sc->sc_irq, sc->sc_intrhand);
-	/* XXX don't store rid */
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
-
-	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
-	bus_dmamem_free(sc->sc_dmat, sc->sc_dma, sc->sc_dmamap);
-	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
-	bus_dma_tag_destroy(sc->sc_dmat);
-
-	bus_release_resource(dev, SYS_RES_MEMORY, HIFN_BAR1, sc->sc_bar1res);
-	bus_release_resource(dev, SYS_RES_MEMORY, HIFN_BAR0, sc->sc_bar0res);
-
-	lockuninit(&sc->sc_lock);
-
-	return (0);
-}
-
-/*
- * Stop all chip I/O so that the kernel's probe routines don't
- * get confused by errant DMAs when rebooting.
- */
-static void
-hifn_shutdown(device_t dev)
-{
-#ifdef notyet
-	hifn_stop(device_get_softc(dev));
-#endif
-}
-
-/*
- * Device suspend routine.  Stop the interface and save some PCI
- * settings in case the BIOS doesn't restore them properly on
- * resume.
- */
-static int
-hifn_suspend(device_t dev)
-{
-	struct hifn_softc *sc = device_get_softc(dev);
-#ifdef notyet
-	int i;
-
-	hifn_stop(sc);
-	for (i = 0; i < 5; i++)
-		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
-	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
-	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
-	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
-	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
-#endif
-	sc->sc_suspended = 1;
-
-	return (0);
-}
-
-/*
- * Device resume routine.  Restore some PCI settings in case the BIOS
- * doesn't, re-enable busmastering, and restart the interface if
- * appropriate.
- */
-static int
-hifn_resume(device_t dev)
-{
-	struct hifn_softc *sc = device_get_softc(dev);
-#ifdef notyet
-	int i;
-
-	/* better way to do this? */
-	for (i = 0; i < 5; i++)
-		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
-	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
-	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
-	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
-	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
-
-	/* reenable busmastering */
-	pci_enable_busmaster(dev);
-	pci_enable_io(dev, HIFN_RES);
-
-        /* reinitialize interface if necessary */
-        if (ifp->if_flags & IFF_UP)
-                rl_init(sc);
-#endif
-	sc->sc_suspended = 0;
-
-	return (0);
-}
-
-static int
-hifn_init_pubrng(struct hifn_softc *sc)
-{
-	u_int32_t r;
-	int i;
-
-#ifdef HIFN_RNDTEST
-	sc->sc_rndtest = rndtest_attach(sc->sc_dev);
-	if (sc->sc_rndtest)
-		sc->sc_harvest = rndtest_harvest;
-	else
-		sc->sc_harvest = default_harvest;
-#else
-	sc->sc_harvest = default_harvest;
-#endif
-	if ((sc->sc_flags & HIFN_IS_7811) == 0) {
-		/* Reset 7951 public key/rng engine */
-		WRITE_REG_1(sc, HIFN_1_PUB_RESET,
-		    READ_REG_1(sc, HIFN_1_PUB_RESET) | HIFN_PUBRST_RESET);
-
-		for (i = 0; i < 100; i++) {
-			DELAY(1000);
-			if ((READ_REG_1(sc, HIFN_1_PUB_RESET) &
-			    HIFN_PUBRST_RESET) == 0)
-				break;
-		}
-
-		if (i == 100) {
-			device_printf(sc->sc_dev, "public key init failed\n");
-			return (1);
-		}
-	}
-
-#ifndef HIFN_NO_RNG
-	/* Enable the rng, if available */
-	if (sc->sc_flags & HIFN_HAS_RNG) {
-		if (sc->sc_flags & HIFN_IS_7811) {
-			r = READ_REG_1(sc, HIFN_1_7811_RNGENA);
-			if (r & HIFN_7811_RNGENA_ENA) {
-				r &= ~HIFN_7811_RNGENA_ENA;
-				WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
-			}
-			WRITE_REG_1(sc, HIFN_1_7811_RNGCFG,
-			    HIFN_7811_RNGCFG_DEFL);
-			r |= HIFN_7811_RNGENA_ENA;
-			WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
-		} else
-			WRITE_REG_1(sc, HIFN_1_RNG_CONFIG,
-			    READ_REG_1(sc, HIFN_1_RNG_CONFIG) |
-			    HIFN_RNGCFG_ENA);
-
-		sc->sc_rngfirst = 1;
-		if (hz >= 100)
-			sc->sc_rnghz = hz / 100;
-		else
-			sc->sc_rnghz = 1;
-		/* NB: 1 means the callout runs w/o Giant locked */
-		callout_init_mp(&sc->sc_rngto);
-		callout_reset(&sc->sc_rngto, sc->sc_rnghz, hifn_rng, sc);
-	}
-#endif
-
-	/* Enable public key engine, if available */
-	if (sc->sc_flags & HIFN_HAS_PUBLIC) {
-		WRITE_REG_1(sc, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
-		sc->sc_dmaier |= HIFN_DMAIER_PUBDONE;
-		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
-	}
-
-	return (0);
-}
-
-#ifndef HIFN_NO_RNG
-static void
-hifn_rng(void *vsc)
-{
-#define	RANDOM_BITS(n)	(n)*sizeof (u_int32_t), (n)*sizeof (u_int32_t)*NBBY, 0
-	struct hifn_softc *sc = vsc;
-	u_int32_t sts, num[2];
-	int i;
-
-	if (sc->sc_flags & HIFN_IS_7811) {
-		for (i = 0; i < 5; i++) {
-			sts = READ_REG_1(sc, HIFN_1_7811_RNGSTS);
-			if (sts & HIFN_7811_RNGSTS_UFL) {
-				device_printf(sc->sc_dev,
-					      "RNG underflow: disabling\n");
-				return;
-			}
-			if ((sts & HIFN_7811_RNGSTS_RDY) == 0)
-				break;
-
-			/*
-			 * There are at least two words in the RNG FIFO
-			 * at this point.
-			 */
-			num[0] = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
-			num[1] = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
-			/* NB: discard first data read */
-			if (sc->sc_rngfirst)
-				sc->sc_rngfirst = 0;
-			else
-				(*sc->sc_harvest)(sc->sc_rndtest,
-					num, sizeof (num));
-		}
-	} else {
-		num[0] = READ_REG_1(sc, HIFN_1_RNG_DATA);
-
-		/* NB: discard first data read */
-		if (sc->sc_rngfirst)
-			sc->sc_rngfirst = 0;
-		else
-			(*sc->sc_harvest)(sc->sc_rndtest,
-				num, sizeof (num[0]));
-	}
-
-	callout_reset(&sc->sc_rngto, sc->sc_rnghz, hifn_rng, sc);
-#undef RANDOM_BITS
-}
-#endif
-
-static void
-hifn_puc_wait(struct hifn_softc *sc)
-{
-	int i;
-	int reg = HIFN_0_PUCTRL;
-
-	if (sc->sc_flags & HIFN_IS_7956) {
-		reg = HIFN_0_PUCTRL2;
-	}
-
-	for (i = 5000; i > 0; i--) {
-		DELAY(1);
-		if (!(READ_REG_0(sc, reg) & HIFN_PUCTRL_RESET))
-			break;
-	}
-	if (!i)
-		device_printf(sc->sc_dev, "proc unit did not reset\n");
-}
-
-/*
- * Reset the processing unit.
- */
-static void
-hifn_reset_puc(struct hifn_softc *sc)
-{
-	int reg = HIFN_0_PUCTRL;
-
-	if (sc->sc_flags & HIFN_IS_7956) {
-		reg = HIFN_0_PUCTRL2;
-	}
-
-	/* Reset processing unit */
-	WRITE_REG_0(sc, reg, HIFN_PUCTRL_DMAENA);
-	hifn_puc_wait(sc);
-}
-
-/*
- * Set the Retry and TRDY registers; note that we set them to
- * zero because the 7811 locks up when forced to retry (section
- * 3.6 of "Specification Update SU-0014-04".  Not clear if we
- * should do this for all Hifn parts, but it doesn't seem to hurt.
- */
-static void
-hifn_set_retry(struct hifn_softc *sc)
-{
-	/* NB: RETRY only responds to 8-bit reads/writes */
-	pci_write_config(sc->sc_dev, HIFN_RETRY_TIMEOUT, 0, 1);
-	pci_write_config(sc->sc_dev, HIFN_TRDY_TIMEOUT, 0, 4);
-}
-
-/*
- * Resets the board.  Values in the regesters are left as is
- * from the reset (i.e. initial values are assigned elsewhere).
- */
-static void
-hifn_reset_board(struct hifn_softc *sc, int full)
-{
-	u_int32_t reg;
-
-	/*
-	 * Set polling in the DMA configuration register to zero.  0x7 avoids
-	 * resetting the board and zeros out the other fields.
-	 */
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
-	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
-
-	/*
-	 * Now that polling has been disabled, we have to wait 1 ms
-	 * before resetting the board.
-	 */
-	DELAY(1000);
-
-	/* Reset the DMA unit */
-	if (full) {
-		WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
-		DELAY(1000);
-	} else {
-		WRITE_REG_1(sc, HIFN_1_DMA_CNFG,
-		    HIFN_DMACNFG_MODE | HIFN_DMACNFG_MSTRESET);
-		hifn_reset_puc(sc);
-	}
-
-	KASSERT(sc->sc_dma != NULL, ("hifn_reset_board: null DMA tag!"));
-	bzero(sc->sc_dma, sizeof(*sc->sc_dma));
-
-	/* Bring dma unit out of reset */
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
-	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
-
-	hifn_puc_wait(sc);
-	hifn_set_retry(sc);
-
-	if (sc->sc_flags & HIFN_IS_7811) {
-		for (reg = 0; reg < 1000; reg++) {
-			if (READ_REG_1(sc, HIFN_1_7811_MIPSRST) &
-			    HIFN_MIPSRST_CRAMINIT)
-				break;
-			DELAY(1000);
-		}
-		if (reg == 1000)
-			kprintf(": cram init timeout\n");
-	} else {
-	  /* set up DMA configuration register #2 */
-	  /* turn off all PK and BAR0 swaps */
-	  WRITE_REG_1(sc, HIFN_1_DMA_CNFG2,
-		      (3 << HIFN_DMACNFG2_INIT_WRITE_BURST_SHIFT)|
-		      (3 << HIFN_DMACNFG2_INIT_READ_BURST_SHIFT)|
-		      (2 << HIFN_DMACNFG2_TGT_WRITE_BURST_SHIFT)|
-		      (2 << HIFN_DMACNFG2_TGT_READ_BURST_SHIFT));
-	}
-}
-
-static u_int32_t
-hifn_next_signature(u_int32_t a, u_int cnt)
-{
-	int i;
-	u_int32_t v;
-
-	for (i = 0; i < cnt; i++) {
-
-		/* get the parity */
-		v = a & 0x80080125;
-		v ^= v >> 16;
-		v ^= v >> 8;
-		v ^= v >> 4;
-		v ^= v >> 2;
-		v ^= v >> 1;
-
-		a = (v & 1) ^ (a << 1);
-	}
-
-	return a;
-}
-
-struct pci2id {
-	u_short		pci_vendor;
-	u_short		pci_prod;
-	char		card_id[13];
-};
-static struct pci2id pci2id[] = {
-	{
-		PCI_VENDOR_HIFN,
-		PCI_PRODUCT_HIFN_7951,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	}, {
-		PCI_VENDOR_HIFN,
-		PCI_PRODUCT_HIFN_7955,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	}, {
-		PCI_VENDOR_HIFN,
-		PCI_PRODUCT_HIFN_7956,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	}, {
-		PCI_VENDOR_NETSEC,
-		PCI_PRODUCT_NETSEC_7751,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	}, {
-		PCI_VENDOR_INVERTEX,
-		PCI_PRODUCT_INVERTEX_AEON,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	}, {
-		PCI_VENDOR_HIFN,
-		PCI_PRODUCT_HIFN_7811,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	}, {
-		/*
-		 * Other vendors share this PCI ID as well, such as
-		 * http://www.powercrypt.com, and obviously they also
-		 * use the same key.
-		 */
-		PCI_VENDOR_HIFN,
-		PCI_PRODUCT_HIFN_7751,
-		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-		  0x00, 0x00, 0x00, 0x00, 0x00 }
-	},
-};
-
-/*
- * Checks to see if crypto is already enabled.  If crypto isn't enable,
- * "hifn_enable_crypto" is called to enable it.  The check is important,
- * as enabling crypto twice will lock the board.
- */
-static int 
-hifn_enable_crypto(struct hifn_softc *sc)
-{
-	u_int32_t dmacfg, ramcfg, encl, addr, i;
-	char *offtbl = NULL;
-
-	for (i = 0; i < NELEM(pci2id); i++) {
-		if (pci2id[i].pci_vendor == pci_get_vendor(sc->sc_dev) &&
-		    pci2id[i].pci_prod == pci_get_device(sc->sc_dev)) {
-			offtbl = pci2id[i].card_id;
-			break;
-		}
-	}
-	if (offtbl == NULL) {
-		device_printf(sc->sc_dev, "Unknown card!\n");
-		return (1);
-	}
-
-	ramcfg = READ_REG_0(sc, HIFN_0_PUCNFG);
-	dmacfg = READ_REG_1(sc, HIFN_1_DMA_CNFG);
-
-	/*
-	 * The RAM config register's encrypt level bit needs to be set before
-	 * every read performed on the encryption level register.
-	 */
-	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
-
-	encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
-
-	/*
-	 * Make sure we don't re-unlock.  Two unlocks kills chip until the
-	 * next reboot.
-	 */
-	if (encl == HIFN_PUSTAT_ENA_1 || encl == HIFN_PUSTAT_ENA_2) {
-#ifdef HIFN_DEBUG
-		if (hifn_debug)
-			device_printf(sc->sc_dev,
-			    "Strong crypto already enabled!\n");
-#endif
-		goto report;
-	}
-
-	if (encl != 0 && encl != HIFN_PUSTAT_ENA_0) {
-#ifdef HIFN_DEBUG
-		if (hifn_debug)
-			device_printf(sc->sc_dev,
-			      "Unknown encryption level 0x%x\n", encl);
-#endif
-		return 1;
-	}
-
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_UNLOCK |
-	    HIFN_DMACNFG_MSTRESET | HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
-	DELAY(1000);
-	addr = READ_REG_1(sc, HIFN_UNLOCK_SECRET1);
-	DELAY(1000);
-	WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, 0);
-	DELAY(1000);
-
-	for (i = 0; i <= 12; i++) {
-		addr = hifn_next_signature(addr, offtbl[i] + 0x101);
-		WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, addr);
-
-		DELAY(1000);
-	}
-
-	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
-	encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
-
-#ifdef HIFN_DEBUG
-	if (hifn_debug) {
-		if (encl != HIFN_PUSTAT_ENA_1 && encl != HIFN_PUSTAT_ENA_2)
-			device_printf(sc->sc_dev, "Engine is permanently "
-				"locked until next system reset!\n");
-		else
-			device_printf(sc->sc_dev, "Engine enabled "
-				"successfully!\n");
-	}
-#endif
-
-report:
-	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg);
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, dmacfg);
-
-	switch (encl) {
-	case HIFN_PUSTAT_ENA_1:
-	case HIFN_PUSTAT_ENA_2:
-		break;
-	case HIFN_PUSTAT_ENA_0:
-	default:
-		device_printf(sc->sc_dev, "disabled");
-		break;
-	}
-
-	return 0;
-}
-
-/*
- * Give initial values to the registers listed in the "Register Space"
- * section of the HIFN Software Development reference manual.
- */
-static void 
-hifn_init_pci_registers(struct hifn_softc *sc)
-{
-	/* write fixed values needed by the Initialization registers */
-	WRITE_REG_0(sc, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
-	WRITE_REG_0(sc, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
-	WRITE_REG_0(sc, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
-
-	/* write all 4 ring address registers */
-	WRITE_REG_1(sc, HIFN_1_DMA_CRAR, sc->sc_dma_physaddr +
-	    offsetof(struct hifn_dma, cmdr[0]));
-	WRITE_REG_1(sc, HIFN_1_DMA_SRAR, sc->sc_dma_physaddr +
-	    offsetof(struct hifn_dma, srcr[0]));
-	WRITE_REG_1(sc, HIFN_1_DMA_DRAR, sc->sc_dma_physaddr +
-	    offsetof(struct hifn_dma, dstr[0]));
-	WRITE_REG_1(sc, HIFN_1_DMA_RRAR, sc->sc_dma_physaddr +
-	    offsetof(struct hifn_dma, resr[0]));
-
-	DELAY(2000);
-
-	/* write status register */
-	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
-	    HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
-	    HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
-	    HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
-	    HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
-	    HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
-	    HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
-	    HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
-	    HIFN_DMACSR_S_WAIT |
-	    HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
-	    HIFN_DMACSR_C_WAIT |
-	    HIFN_DMACSR_ENGINE |
-	    ((sc->sc_flags & HIFN_HAS_PUBLIC) ?
-		HIFN_DMACSR_PUBDONE : 0) |
-	    ((sc->sc_flags & HIFN_IS_7811) ?
-		HIFN_DMACSR_ILLW | HIFN_DMACSR_ILLR : 0));
-
-	sc->sc_d_busy = sc->sc_r_busy = sc->sc_s_busy = sc->sc_c_busy = 0;
-	sc->sc_dmaier |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
-	    HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
-	    HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
-	    ((sc->sc_flags & HIFN_IS_7811) ?
-		HIFN_DMAIER_ILLW | HIFN_DMAIER_ILLR : 0);
-	sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
-	WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
-
-	if (sc->sc_flags & HIFN_IS_7956) {
-		u_int32_t pll;
-
-		WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
-		    HIFN_PUCNFG_TCALLPHASES |
-		    HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32);
-
-		/* turn off the clocks and insure bypass is set */
-		pll = READ_REG_1(sc, HIFN_1_PLL);
-		pll = (pll &~ (HIFN_PLL_PK_CLK_SEL | HIFN_PLL_PE_CLK_SEL))
-		    | HIFN_PLL_BP | HIFN_PLL_MBSET;
-		WRITE_REG_1(sc, HIFN_1_PLL, pll);
-		DELAY(10*1000);		/* 10ms */
-		/* change configuration */
-		pll = (pll &~ HIFN_PLL_CONFIG) | sc->sc_pllconfig;
-		WRITE_REG_1(sc, HIFN_1_PLL, pll);
-		DELAY(10*1000);		/* 10ms */
-		/* disable bypass */
-		pll &= ~HIFN_PLL_BP;
-		WRITE_REG_1(sc, HIFN_1_PLL, pll);
-		/* enable clocks with new configuration */
-		pll |= HIFN_PLL_PK_CLK_SEL | HIFN_PLL_PE_CLK_SEL;
-		WRITE_REG_1(sc, HIFN_1_PLL, pll);
-	} else {
-		WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
-		    HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
-		    HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
-		    (sc->sc_drammodel ? HIFN_PUCNFG_DRAM : HIFN_PUCNFG_SRAM));
-	}
-
-	WRITE_REG_0(sc, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
-	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
-	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
-	    ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
-	    ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
-}
-
-/*
- * The maximum number of sessions supported by the card
- * is dependent on the amount of context ram, which
- * encryption algorithms are enabled, and how compression
- * is configured.  This should be configured before this
- * routine is called.
- */
-static void
-hifn_sessions(struct hifn_softc *sc)
-{
-	u_int32_t pucnfg;
-	int ctxsize;
-
-	pucnfg = READ_REG_0(sc, HIFN_0_PUCNFG);
-
-	if (pucnfg & HIFN_PUCNFG_COMPSING) {
-		if (pucnfg & HIFN_PUCNFG_ENCCNFG)
-			ctxsize = 128;
-		else
-			ctxsize = 512;
-		/*
-		 * 7955/7956 has internal context memory of 32K
-		 */
-		if (sc->sc_flags & HIFN_IS_7956)
-			sc->sc_maxses = 32768 / ctxsize;
-		else
-			sc->sc_maxses = 1 +
-			    ((sc->sc_ramsize - 32768) / ctxsize);
-	} else
-		sc->sc_maxses = sc->sc_ramsize / 16384;
-
-	if (sc->sc_maxses > 2048)
-		sc->sc_maxses = 2048;
-}
-
-/*
- * Determine ram type (sram or dram).  Board should be just out of a reset
- * state when this is called.
- */
-static int
-hifn_ramtype(struct hifn_softc *sc)
-{
-	u_int8_t data[8], dataexpect[8];
-	int i;
-
-	for (i = 0; i < sizeof(data); i++)
-		data[i] = dataexpect[i] = 0x55;
-	if (hifn_writeramaddr(sc, 0, data))
-		return (-1);
-	if (hifn_readramaddr(sc, 0, data))
-		return (-1);
-	if (bcmp(data, dataexpect, sizeof(data)) != 0) {
-		sc->sc_drammodel = 1;
-		return (0);
-	}
-
-	for (i = 0; i < sizeof(data); i++)
-		data[i] = dataexpect[i] = 0xaa;
-	if (hifn_writeramaddr(sc, 0, data))
-		return (-1);
-	if (hifn_readramaddr(sc, 0, data))
-		return (-1);
-	if (bcmp(data, dataexpect, sizeof(data)) != 0) {
-		sc->sc_drammodel = 1;
-		return (0);
-	}
-
-	return (0);
-}
-
-#define	HIFN_SRAM_MAX		(32 << 20)
-#define	HIFN_SRAM_STEP_SIZE	16384
-#define	HIFN_SRAM_GRANULARITY	(HIFN_SRAM_MAX / HIFN_SRAM_STEP_SIZE)
-
-static int
-hifn_sramsize(struct hifn_softc *sc)
-{
-	u_int32_t a;
-	u_int8_t data[8];
-	u_int8_t dataexpect[sizeof(data)];
-	int32_t i;
-
-	for (i = 0; i < sizeof(data); i++)
-		data[i] = dataexpect[i] = i ^ 0x5a;
-
-	for (i = HIFN_SRAM_GRANULARITY - 1; i >= 0; i--) {
-		a = i * HIFN_SRAM_STEP_SIZE;
-		bcopy(&i, data, sizeof(i));
-		hifn_writeramaddr(sc, a, data);
-	}
-
-	for (i = 0; i < HIFN_SRAM_GRANULARITY; i++) {
-		a = i * HIFN_SRAM_STEP_SIZE;
-		bcopy(&i, dataexpect, sizeof(i));
-		if (hifn_readramaddr(sc, a, data) < 0)
-			return (0);
-		if (bcmp(data, dataexpect, sizeof(data)) != 0)
-			return (0);
-		sc->sc_ramsize = a + HIFN_SRAM_STEP_SIZE;
-	}
-
-	return (0);
-}
-
-/*
- * XXX For dram boards, one should really try all of the
- * HIFN_PUCNFG_DSZ_*'s.  This just assumes that PUCNFG
- * is already set up correctly.
- */
-static int
-hifn_dramsize(struct hifn_softc *sc)
-{
-	u_int32_t cnfg;
-	
-	if (sc->sc_flags & HIFN_IS_7956) {
-		/*
-		 * 7955/7956 have a fixed internal ram of only 32K.
-		 */
-		sc->sc_ramsize = 32768;
-	} else {
-		cnfg = READ_REG_0(sc, HIFN_0_PUCNFG) &
-		    HIFN_PUCNFG_DRAMMASK;
-		sc->sc_ramsize = 1 << ((cnfg >> 13) + 18);
-	}
-	return (0);
-}
-
-static void
-hifn_alloc_slot(struct hifn_softc *sc, int *cmdp, int *srcp, int *dstp, int *resp)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-
-	if (dma->cmdi == HIFN_D_CMD_RSIZE) {
-		dma->cmdi = 0;
-		dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-	}
-	*cmdp = dma->cmdi++;
-	dma->cmdk = dma->cmdi;
-
-	if (dma->srci == HIFN_D_SRC_RSIZE) {
-		dma->srci = 0;
-		dma->srcr[HIFN_D_SRC_RSIZE].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-	}
-	*srcp = dma->srci++;
-	dma->srck = dma->srci;
-
-	if (dma->dsti == HIFN_D_DST_RSIZE) {
-		dma->dsti = 0;
-		dma->dstr[HIFN_D_DST_RSIZE].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_DSTR_SYNC(sc, HIFN_D_DST_RSIZE,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-	}
-	*dstp = dma->dsti++;
-	dma->dstk = dma->dsti;
-
-	if (dma->resi == HIFN_D_RES_RSIZE) {
-		dma->resi = 0;
-		dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-	}
-	*resp = dma->resi++;
-	dma->resk = dma->resi;
-}
-
-static int
-hifn_writeramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	hifn_base_command_t wc;
-	const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
-	int r, cmdi, resi, srci, dsti;
-
-	wc.masks = htole16(3 << 13);
-	wc.session_num = htole16(addr >> 14);
-	wc.total_source_count = htole16(8);
-	wc.total_dest_count = htole16(addr & 0x3fff);
-
-	hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
-
-	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
-	    HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
-	    HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
-
-	/* build write command */
-	bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
-	*(hifn_base_command_t *)dma->command_bufs[cmdi] = wc;
-	bcopy(data, &dma->test_src, sizeof(dma->test_src));
-
-	dma->srcr[srci].p = htole32(sc->sc_dma_physaddr
-	    + offsetof(struct hifn_dma, test_src));
-	dma->dstr[dsti].p = htole32(sc->sc_dma_physaddr
-	    + offsetof(struct hifn_dma, test_dst));
-
-	dma->cmdr[cmdi].l = htole32(16 | masks);
-	dma->srcr[srci].l = htole32(8 | masks);
-	dma->dstr[dsti].l = htole32(4 | masks);
-	dma->resr[resi].l = htole32(4 | masks);
-
-	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-
-	for (r = 10000; r >= 0; r--) {
-		DELAY(10);
-		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
-		if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
-			break;
-		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	}
-	if (r == 0) {
-		device_printf(sc->sc_dev, "writeramaddr -- "
-		    "result[%d](addr %d) still valid\n", resi, addr);
-		r = -1;
-		return (-1);
-	} else
-		r = 0;
-
-	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
-	    HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
-	    HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
-
-	return (r);
-}
-
-static int
-hifn_readramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	hifn_base_command_t rc;
-	const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
-	int r, cmdi, srci, dsti, resi;
-
-	rc.masks = htole16(2 << 13);
-	rc.session_num = htole16(addr >> 14);
-	rc.total_source_count = htole16(addr & 0x3fff);
-	rc.total_dest_count = htole16(8);
-
-	hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
-
-	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
-	    HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
-	    HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
-
-	bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
-	*(hifn_base_command_t *)dma->command_bufs[cmdi] = rc;
-
-	dma->srcr[srci].p = htole32(sc->sc_dma_physaddr +
-	    offsetof(struct hifn_dma, test_src));
-	dma->test_src = 0;
-	dma->dstr[dsti].p =  htole32(sc->sc_dma_physaddr +
-	    offsetof(struct hifn_dma, test_dst));
-	dma->test_dst = 0;
-	dma->cmdr[cmdi].l = htole32(8 | masks);
-	dma->srcr[srci].l = htole32(8 | masks);
-	dma->dstr[dsti].l = htole32(8 | masks);
-	dma->resr[resi].l = htole32(HIFN_MAX_RESULT | masks);
-
-	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-
-	for (r = 10000; r >= 0; r--) {
-		DELAY(10);
-		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
-		if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
-			break;
-		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	}
-	if (r == 0) {
-		device_printf(sc->sc_dev, "readramaddr -- "
-		    "result[%d](addr %d) still valid\n", resi, addr);
-		r = -1;
-	} else {
-		r = 0;
-		bcopy(&dma->test_dst, data, sizeof(dma->test_dst));
-	}
-
-	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
-	    HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
-	    HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
-
-	return (r);
-}
-
-/*
- * Initialize the descriptor rings.
- */
-static void 
-hifn_init_dma(struct hifn_softc *sc)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	int i;
-
-	hifn_set_retry(sc);
-
-	/* initialize static pointer values */
-	for (i = 0; i < HIFN_D_CMD_RSIZE; i++)
-		dma->cmdr[i].p = htole32(sc->sc_dma_physaddr +
-		    offsetof(struct hifn_dma, command_bufs[i][0]));
-	for (i = 0; i < HIFN_D_RES_RSIZE; i++)
-		dma->resr[i].p = htole32(sc->sc_dma_physaddr +
-		    offsetof(struct hifn_dma, result_bufs[i][0]));
-
-	dma->cmdr[HIFN_D_CMD_RSIZE].p =
-	    htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, cmdr[0]));
-	dma->srcr[HIFN_D_SRC_RSIZE].p =
-	    htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, srcr[0]));
-	dma->dstr[HIFN_D_DST_RSIZE].p =
-	    htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, dstr[0]));
-	dma->resr[HIFN_D_RES_RSIZE].p =
-	    htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, resr[0]));
-
-	dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
-	dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
-	dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
-}
-
-/*
- * Writes out the raw command buffer space.  Returns the
- * command buffer size.
- */
-static u_int
-hifn_write_command(struct hifn_command *cmd, u_int8_t *buf)
-{
-	u_int8_t *buf_pos;
-	hifn_base_command_t *base_cmd;
-	hifn_mac_command_t *mac_cmd;
-	hifn_crypt_command_t *cry_cmd;
-	int using_mac, using_crypt, len, ivlen;
-	u_int32_t dlen, slen;
-
-	buf_pos = buf;
-	using_mac = cmd->base_masks & HIFN_BASE_CMD_MAC;
-	using_crypt = cmd->base_masks & HIFN_BASE_CMD_CRYPT;
-
-	base_cmd = (hifn_base_command_t *)buf_pos;
-	base_cmd->masks = htole16(cmd->base_masks);
-	slen = cmd->src_mapsize;
-	if (cmd->sloplen)
-		dlen = cmd->dst_mapsize - cmd->sloplen + sizeof(u_int32_t);
-	else
-		dlen = cmd->dst_mapsize;
-	base_cmd->total_source_count = htole16(slen & HIFN_BASE_CMD_LENMASK_LO);
-	base_cmd->total_dest_count = htole16(dlen & HIFN_BASE_CMD_LENMASK_LO);
-	dlen >>= 16;
-	slen >>= 16;
-
-	base_cmd->session_num = htole16(
-	    ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
-	    ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
-	buf_pos += sizeof(hifn_base_command_t);
-
-	if (using_mac) {
-		mac_cmd = (hifn_mac_command_t *)buf_pos;
-		dlen = cmd->maccrd->crd_len;
-		mac_cmd->source_count = htole16(dlen & 0xffff);
-		dlen >>= 16;
-		mac_cmd->masks = htole16(cmd->mac_masks |
-		    ((dlen << HIFN_MAC_CMD_SRCLEN_S) & HIFN_MAC_CMD_SRCLEN_M));
-		mac_cmd->header_skip = htole16(cmd->maccrd->crd_skip);
-		mac_cmd->reserved = 0;
-		buf_pos += sizeof(hifn_mac_command_t);
-	}
-
-	if (using_crypt) {
-		cry_cmd = (hifn_crypt_command_t *)buf_pos;
-		dlen = cmd->enccrd->crd_len;
-		cry_cmd->source_count = htole16(dlen & 0xffff);
-		dlen >>= 16;
-		cry_cmd->masks = htole16(cmd->cry_masks |
-		    ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) & HIFN_CRYPT_CMD_SRCLEN_M));
-		cry_cmd->header_skip = htole16(cmd->enccrd->crd_skip);
-		cry_cmd->reserved = 0;
-		buf_pos += sizeof(hifn_crypt_command_t);
-	}
-
-	if (using_mac && cmd->mac_masks & HIFN_MAC_CMD_NEW_KEY) {
-		bcopy(cmd->mac, buf_pos, HIFN_MAC_KEY_LENGTH);
-		buf_pos += HIFN_MAC_KEY_LENGTH;
-	}
-
-	if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_KEY) {
-		switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
-		case HIFN_CRYPT_CMD_ALG_3DES:
-			bcopy(cmd->ck, buf_pos, HIFN_3DES_KEY_LENGTH);
-			buf_pos += HIFN_3DES_KEY_LENGTH;
-			break;
-		case HIFN_CRYPT_CMD_ALG_DES:
-			bcopy(cmd->ck, buf_pos, HIFN_DES_KEY_LENGTH);
-			buf_pos += HIFN_DES_KEY_LENGTH;
-			break;
-		case HIFN_CRYPT_CMD_ALG_RC4:
-			len = 256;
-			do {
-				int clen;
-
-				clen = MIN(cmd->cklen, len);
-				bcopy(cmd->ck, buf_pos, clen);
-				len -= clen;
-				buf_pos += clen;
-			} while (len > 0);
-			bzero(buf_pos, 4);
-			buf_pos += 4;
-			break;
-		case HIFN_CRYPT_CMD_ALG_AES:
-			/*
-			 * AES keys are variable 128, 192 and
-			 * 256 bits (16, 24 and 32 bytes).
-			 */
-			bcopy(cmd->ck, buf_pos, cmd->cklen);
-			buf_pos += cmd->cklen;
-			break;
-		}
-	}
-
-	if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_IV) {
-		switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
-		case HIFN_CRYPT_CMD_ALG_AES:
-			ivlen = HIFN_AES_IV_LENGTH;
-			break;
-		default:
-			ivlen = HIFN_IV_LENGTH;
-			break;
-		}
-		bcopy(cmd->iv, buf_pos, ivlen);
-		buf_pos += ivlen;
-	}
-
-	if ((cmd->base_masks & (HIFN_BASE_CMD_MAC|HIFN_BASE_CMD_CRYPT)) == 0) {
-		bzero(buf_pos, 8);
-		buf_pos += 8;
-	}
-
-	return (buf_pos - buf);
-#undef	MIN
-}
-
-static int
-hifn_dmamap_aligned(struct hifn_operand *op)
-{
-	int i;
-
-	for (i = 0; i < op->nsegs; i++) {
-		if (op->segs[i].ds_addr & 3)
-			return (0);
-		if ((i != (op->nsegs - 1)) && (op->segs[i].ds_len & 3))
-			return (0);
-	}
-	return (1);
-}
-
-static __inline int
-hifn_dmamap_dstwrap(struct hifn_softc *sc, int idx)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-
-	if (++idx == HIFN_D_DST_RSIZE) {
-		dma->dstr[idx].l = htole32(HIFN_D_VALID | HIFN_D_JUMP |
-		    HIFN_D_MASKDONEIRQ);
-		HIFN_DSTR_SYNC(sc, idx,
-		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-		idx = 0;
-	}
-	return (idx);
-}
-
-static int
-hifn_dmamap_load_dst(struct hifn_softc *sc, struct hifn_command *cmd)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	struct hifn_operand *dst = &cmd->dst;
-	u_int32_t p, l;
-	int idx, used = 0, i;
-
-	idx = dma->dsti;
-	for (i = 0; i < dst->nsegs - 1; i++) {
-		dma->dstr[idx].p = htole32(dst->segs[i].ds_addr);
-		dma->dstr[idx].l = htole32(HIFN_D_VALID |
-		    HIFN_D_MASKDONEIRQ | dst->segs[i].ds_len);
-		HIFN_DSTR_SYNC(sc, idx,
-		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-		used++;
-
-		idx = hifn_dmamap_dstwrap(sc, idx);
-	}
-
-	if (cmd->sloplen == 0) {
-		p = dst->segs[i].ds_addr;
-		l = HIFN_D_VALID | HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
-		    dst->segs[i].ds_len;
-	} else {
-		p = sc->sc_dma_physaddr +
-		    offsetof(struct hifn_dma, slop[cmd->slopidx]);
-		l = HIFN_D_VALID | HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
-		    sizeof(u_int32_t);
-
-		if ((dst->segs[i].ds_len - cmd->sloplen) != 0) {
-			dma->dstr[idx].p = htole32(dst->segs[i].ds_addr);
-			dma->dstr[idx].l = htole32(HIFN_D_VALID |
-			    HIFN_D_MASKDONEIRQ |
-			    (dst->segs[i].ds_len - cmd->sloplen));
-			HIFN_DSTR_SYNC(sc, idx,
-			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-			used++;
-
-			idx = hifn_dmamap_dstwrap(sc, idx);
-		}
-	}
-	dma->dstr[idx].p = htole32(p);
-	dma->dstr[idx].l = htole32(l);
-	HIFN_DSTR_SYNC(sc, idx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	used++;
-
-	idx = hifn_dmamap_dstwrap(sc, idx);
-
-	dma->dsti = idx;
-	dma->dstu += used;
-	return (idx);
-}
-
-static __inline int
-hifn_dmamap_srcwrap(struct hifn_softc *sc, int idx)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-
-	if (++idx == HIFN_D_SRC_RSIZE) {
-		dma->srcr[idx].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-		idx = 0;
-	}
-	return (idx);
-}
-
-static int
-hifn_dmamap_load_src(struct hifn_softc *sc, struct hifn_command *cmd)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	struct hifn_operand *src = &cmd->src;
-	int idx, i;
-	u_int32_t last = 0;
-
-	idx = dma->srci;
-	for (i = 0; i < src->nsegs; i++) {
-		if (i == src->nsegs - 1)
-			last = HIFN_D_LAST;
-
-		dma->srcr[idx].p = htole32(src->segs[i].ds_addr);
-		dma->srcr[idx].l = htole32(src->segs[i].ds_len |
-		    HIFN_D_VALID | HIFN_D_MASKDONEIRQ | last);
-		HIFN_SRCR_SYNC(sc, idx,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-
-		idx = hifn_dmamap_srcwrap(sc, idx);
-	}
-	dma->srci = idx;
-	dma->srcu += src->nsegs;
-	return (idx);
-} 
-
-static void
-hifn_op_cb(void* arg, bus_dma_segment_t *seg, int nsegs, bus_size_t mapsize, int error)
-{
-	struct hifn_operand *op = arg;
-
-	KASSERT(nsegs <= MAX_SCATTER,
-		("hifn_op_cb: too many DMA segments (%u > %u) "
-		 "returned when mapping operand", nsegs, MAX_SCATTER));
-	op->mapsize = mapsize;
-	op->nsegs = nsegs;
-	bcopy(seg, op->segs, nsegs * sizeof (seg[0]));
-}
-
-static int 
-hifn_crypto(
-	struct hifn_softc *sc,
-	struct hifn_command *cmd,
-	struct cryptop *crp,
-	int hint)
-{
-	struct	hifn_dma *dma = sc->sc_dma;
-	u_int32_t cmdlen, csr;
-	int cmdi, resi, err = 0;
-
-	/*
-	 * need 1 cmd, and 1 res
-	 *
-	 * NB: check this first since it's easy.
-	 */
-	HIFN_LOCK(sc);
-	if ((dma->cmdu + 1) > HIFN_D_CMD_RSIZE ||
-	    (dma->resu + 1) > HIFN_D_RES_RSIZE) {
-#ifdef HIFN_DEBUG
-		if (hifn_debug) {
-			device_printf(sc->sc_dev,
-				"cmd/result exhaustion, cmdu %u resu %u\n",
-				dma->cmdu, dma->resu);
-		}
-#endif
-		hifnstats.hst_nomem_cr++;
-		HIFN_UNLOCK(sc);
-		return (ERESTART);
-	}
-
-	if (bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &cmd->src_map)) {
-		hifnstats.hst_nomem_map++;
-		HIFN_UNLOCK(sc);
-		return (ENOMEM);
-	}
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->src_map,
-		    cmd->src_m, hifn_op_cb, &cmd->src, BUS_DMA_NOWAIT)) {
-			hifnstats.hst_nomem_load++;
-			err = ENOMEM;
-			goto err_srcmap1;
-		}
-	} else if (crp->crp_flags & CRYPTO_F_IOV) {
-#if 0
-		cmd->src_io->uio_segflg = UIO_USERSPACE;
-#endif
-		if (bus_dmamap_load_uio(sc->sc_dmat, cmd->src_map,
-		    cmd->src_io, hifn_op_cb, &cmd->src, BUS_DMA_NOWAIT)) {
-			hifnstats.hst_nomem_load++;
-			err = ENOMEM;
-			goto err_srcmap1;
-		}
-	} else {
-		err = EINVAL;
-		goto err_srcmap1;
-	}
-
-	if (hifn_dmamap_aligned(&cmd->src)) {
-		cmd->sloplen = cmd->src_mapsize & 3;
-		cmd->dst = cmd->src;
-	} else {
-		if (crp->crp_flags & CRYPTO_F_IOV) {
-			err = EINVAL;
-			goto err_srcmap;
-		} else if (crp->crp_flags & CRYPTO_F_IMBUF) {
-			int totlen, len;
-			struct mbuf *m, *m0, *mlast;
-
-			KASSERT(cmd->dst_m == cmd->src_m,
-				("hifn_crypto: dst_m initialized improperly"));
-			hifnstats.hst_unaligned++;
-			/*
-			 * Source is not aligned on a longword boundary.
-			 * Copy the data to insure alignment.  If we fail
-			 * to allocate mbufs or clusters while doing this
-			 * we return ERESTART so the operation is requeued
-			 * at the crypto later, but only if there are
-			 * ops already posted to the hardware; otherwise we
-			 * have no guarantee that we'll be re-entered.
-			 */
-			totlen = cmd->src_mapsize;
-			if (cmd->src_m->m_flags & M_PKTHDR) {
-				len = MHLEN;
-				MGETHDR(m0, M_NOWAIT, MT_DATA);
-				if (m0 && !m_dup_pkthdr(m0, cmd->src_m, M_NOWAIT)) {
-					m_free(m0);
-					m0 = NULL;
-				}
-			} else {
-				len = MLEN;
-				MGET(m0, M_NOWAIT, MT_DATA);
-			}
-			if (m0 == NULL) {
-				hifnstats.hst_nomem_mbuf++;
-				err = dma->cmdu ? ERESTART : ENOMEM;
-				goto err_srcmap;
-			}
-			if (totlen >= MINCLSIZE) {
-				MCLGET(m0, M_NOWAIT);
-				if ((m0->m_flags & M_EXT) == 0) {
-					hifnstats.hst_nomem_mcl++;
-					err = dma->cmdu ? ERESTART : ENOMEM;
-					m_freem(m0);
-					goto err_srcmap;
-				}
-				len = MCLBYTES;
-			}
-			totlen -= len;
-			m0->m_pkthdr.len = m0->m_len = len;
-			mlast = m0;
-
-			while (totlen > 0) {
-				MGET(m, M_NOWAIT, MT_DATA);
-				if (m == NULL) {
-					hifnstats.hst_nomem_mbuf++;
-					err = dma->cmdu ? ERESTART : ENOMEM;
-					m_freem(m0);
-					goto err_srcmap;
-				}
-				len = MLEN;
-				if (totlen >= MINCLSIZE) {
-					MCLGET(m, M_NOWAIT);
-					if ((m->m_flags & M_EXT) == 0) {
-						hifnstats.hst_nomem_mcl++;
-						err = dma->cmdu ? ERESTART : ENOMEM;
-						mlast->m_next = m;
-						m_freem(m0);
-						goto err_srcmap;
-					}
-					len = MCLBYTES;
-				}
-
-				m->m_len = len;
-				m0->m_pkthdr.len += len;
-				totlen -= len;
-
-				mlast->m_next = m;
-				mlast = m;
-			}
-			cmd->dst_m = m0;
-		}
-	}
-
-	if (cmd->dst_map == NULL) {
-		if (bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &cmd->dst_map)) {
-			hifnstats.hst_nomem_map++;
-			err = ENOMEM;
-			goto err_srcmap;
-		}
-		if (crp->crp_flags & CRYPTO_F_IMBUF) {
-			if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->dst_map,
-			    cmd->dst_m, hifn_op_cb, &cmd->dst, BUS_DMA_NOWAIT)) {
-				hifnstats.hst_nomem_map++;
-				err = ENOMEM;
-				goto err_dstmap1;
-			}
-		} else if (crp->crp_flags & CRYPTO_F_IOV) {
-#if 0
-			cmd->dst_io->uio_segflg |= UIO_USERSPACE;
-#endif
-			if (bus_dmamap_load_uio(sc->sc_dmat, cmd->dst_map,
-			    cmd->dst_io, hifn_op_cb, &cmd->dst, BUS_DMA_NOWAIT)) {
-				hifnstats.hst_nomem_load++;
-				err = ENOMEM;
-				goto err_dstmap1;
-			}
-		}
-	}
-
-#ifdef HIFN_DEBUG
-	if (hifn_debug) {
-		device_printf(sc->sc_dev,
-		    "Entering cmd: stat %8x ien %8x u %d/%d/%d/%d n %d/%d\n",
-		    READ_REG_1(sc, HIFN_1_DMA_CSR),
-		    READ_REG_1(sc, HIFN_1_DMA_IER),
-		    dma->cmdu, dma->srcu, dma->dstu, dma->resu,
-		    cmd->src_nsegs, cmd->dst_nsegs);
-	}
-#endif
-
-	if (cmd->src_map == cmd->dst_map) {
-		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
-		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
-	} else {
-		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
-		    BUS_DMASYNC_PREWRITE);
-		bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
-		    BUS_DMASYNC_PREREAD);
-	}
-
-	/*
-	 * need N src, and N dst
-	 */
-	if ((dma->srcu + cmd->src_nsegs) > HIFN_D_SRC_RSIZE ||
-	    (dma->dstu + cmd->dst_nsegs + 1) > HIFN_D_DST_RSIZE) {
-#ifdef HIFN_DEBUG
-		if (hifn_debug) {
-			device_printf(sc->sc_dev,
-				"src/dst exhaustion, srcu %u+%u dstu %u+%u\n",
-				dma->srcu, cmd->src_nsegs,
-				dma->dstu, cmd->dst_nsegs);
-		}
-#endif
-		hifnstats.hst_nomem_sd++;
-		err = ERESTART;
-		goto err_dstmap;
-	}
-
-	if (dma->cmdi == HIFN_D_CMD_RSIZE) {
-		dma->cmdi = 0;
-		dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
-		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-	}
-	cmdi = dma->cmdi++;
-	cmdlen = hifn_write_command(cmd, dma->command_bufs[cmdi]);
-	HIFN_CMD_SYNC(sc, cmdi, BUS_DMASYNC_PREWRITE);
-
-	/* .p for command/result already set */
-	dma->cmdr[cmdi].l = htole32(cmdlen | HIFN_D_VALID | HIFN_D_LAST |
-	    HIFN_D_MASKDONEIRQ);
-	HIFN_CMDR_SYNC(sc, cmdi,
-	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
-	dma->cmdu++;
-
-	/*
-	 * We don't worry about missing an interrupt (which a "command wait"
-	 * interrupt salvages us from), unless there is more than one command
-	 * in the queue.
-	 */
-	if (dma->cmdu > 1) {
-		sc->sc_dmaier |= HIFN_DMAIER_C_WAIT;
-		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
-	}
-
-	hifnstats.hst_ipackets++;
-	hifnstats.hst_ibytes += cmd->src_mapsize;
-
-	hifn_dmamap_load_src(sc, cmd);
-
-	/*
-	 * Unlike other descriptors, we don't mask done interrupt from
-	 * result descriptor.
-	 */
-#ifdef HIFN_DEBUG
-	if (hifn_debug)
-		kprintf("load res\n");
-#endif
-	if (dma->resi == HIFN_D_RES_RSIZE) {
-		dma->resi = 0;
-		dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_VALID |
-		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
-		HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
-		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	}
-	resi = dma->resi++;
-	KASSERT(dma->hifn_commands[resi] == NULL,
-		("hifn_crypto: command slot %u busy", resi));
-	dma->hifn_commands[resi] = cmd;
-	HIFN_RES_SYNC(sc, resi, BUS_DMASYNC_PREREAD);
-	if ((hint & CRYPTO_HINT_MORE) && sc->sc_curbatch < hifn_maxbatch) {
-		dma->resr[resi].l = htole32(HIFN_MAX_RESULT |
-		    HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
-		sc->sc_curbatch++;
-		if (sc->sc_curbatch > hifnstats.hst_maxbatch)
-			hifnstats.hst_maxbatch = sc->sc_curbatch;
-		hifnstats.hst_totbatch++;
-	} else {
-		dma->resr[resi].l = htole32(HIFN_MAX_RESULT |
-		    HIFN_D_VALID | HIFN_D_LAST);
-		sc->sc_curbatch = 0;
-	}
-	HIFN_RESR_SYNC(sc, resi,
-	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	dma->resu++;
-
-	if (cmd->sloplen)
-		cmd->slopidx = resi;
-
-	hifn_dmamap_load_dst(sc, cmd);
-
-	csr = 0;
-	if (sc->sc_c_busy == 0) {
-		csr |= HIFN_DMACSR_C_CTRL_ENA;
-		sc->sc_c_busy = 1;
-	}
-	if (sc->sc_s_busy == 0) {
-		csr |= HIFN_DMACSR_S_CTRL_ENA;
-		sc->sc_s_busy = 1;
-	}
-	if (sc->sc_r_busy == 0) {
-		csr |= HIFN_DMACSR_R_CTRL_ENA;
-		sc->sc_r_busy = 1;
-	}
-	if (sc->sc_d_busy == 0) {
-		csr |= HIFN_DMACSR_D_CTRL_ENA;
-		sc->sc_d_busy = 1;
-	}
-	if (csr)
-		WRITE_REG_1(sc, HIFN_1_DMA_CSR, csr);
-
-#ifdef HIFN_DEBUG
-	if (hifn_debug) {
-		device_printf(sc->sc_dev, "command: stat %8x ier %8x\n",
-		    READ_REG_1(sc, HIFN_1_DMA_CSR),
-		    READ_REG_1(sc, HIFN_1_DMA_IER));
-	}
-#endif
-
-	sc->sc_active = 5;
-	HIFN_UNLOCK(sc);
-	KASSERT(err == 0, ("hifn_crypto: success with error %u", err));
-	return (err);		/* success */
-
-err_dstmap:
-	if (cmd->src_map != cmd->dst_map)
-		bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
-err_dstmap1:
-	if (cmd->src_map != cmd->dst_map)
-		bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
-err_srcmap:
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		if (cmd->src_m != cmd->dst_m)
-			m_freem(cmd->dst_m);
-	}
-	bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
-err_srcmap1:
-	bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
-	HIFN_UNLOCK(sc);
-	return (err);
-}
-
-static void
-hifn_tick(void* vsc)
-{
-	struct hifn_softc *sc = vsc;
-
-	HIFN_LOCK(sc);
-	if (sc->sc_active == 0) {
-		struct hifn_dma *dma = sc->sc_dma;
-		u_int32_t r = 0;
-
-		if (dma->cmdu == 0 && sc->sc_c_busy) {
-			sc->sc_c_busy = 0;
-			r |= HIFN_DMACSR_C_CTRL_DIS;
-		}
-		if (dma->srcu == 0 && sc->sc_s_busy) {
-			sc->sc_s_busy = 0;
-			r |= HIFN_DMACSR_S_CTRL_DIS;
-		}
-		if (dma->dstu == 0 && sc->sc_d_busy) {
-			sc->sc_d_busy = 0;
-			r |= HIFN_DMACSR_D_CTRL_DIS;
-		}
-		if (dma->resu == 0 && sc->sc_r_busy) {
-			sc->sc_r_busy = 0;
-			r |= HIFN_DMACSR_R_CTRL_DIS;
-		}
-		if (r)
-			WRITE_REG_1(sc, HIFN_1_DMA_CSR, r);
-	} else
-		sc->sc_active--;
-	HIFN_UNLOCK(sc);
-	callout_reset(&sc->sc_tickto, hz, hifn_tick, sc);
-}
-
-static void 
-hifn_intr(void *arg)
-{
-	struct hifn_softc *sc = arg;
-	struct hifn_dma *dma;
-	u_int32_t dmacsr, restart;
-	int i, u;
-
-	dmacsr = READ_REG_1(sc, HIFN_1_DMA_CSR);
-	
-	/* Nothing in the DMA unit interrupted */
-	if ((dmacsr & sc->sc_dmaier) == 0) {
-		hifnstats.hst_noirq++;
-		return;
-	}
-
-	HIFN_LOCK(sc);
-
-	dma = sc->sc_dma;
-
-#ifdef HIFN_DEBUG
-	if (hifn_debug) {
-		device_printf(sc->sc_dev,
-		    "irq: stat %08x ien %08x damier %08x i %d/%d/%d/%d k %d/%d/%d/%d u %d/%d/%d/%d\n",
-		    dmacsr, READ_REG_1(sc, HIFN_1_DMA_IER), sc->sc_dmaier,
-		    dma->cmdi, dma->srci, dma->dsti, dma->resi,
-		    dma->cmdk, dma->srck, dma->dstk, dma->resk,
-		    dma->cmdu, dma->srcu, dma->dstu, dma->resu);
-	}
-#endif
-
-	WRITE_REG_1(sc, HIFN_1_DMA_CSR, dmacsr & sc->sc_dmaier);
-
-	if ((sc->sc_flags & HIFN_HAS_PUBLIC) &&
-	    (dmacsr & HIFN_DMACSR_PUBDONE))
-		WRITE_REG_1(sc, HIFN_1_PUB_STATUS,
-		    READ_REG_1(sc, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
-
-	restart = dmacsr & (HIFN_DMACSR_D_OVER | HIFN_DMACSR_R_OVER);
-	if (restart)
-		device_printf(sc->sc_dev, "overrun %x\n", dmacsr);
-
-	if (sc->sc_flags & HIFN_IS_7811) {
-		if (dmacsr & HIFN_DMACSR_ILLR)
-			device_printf(sc->sc_dev, "illegal read\n");
-		if (dmacsr & HIFN_DMACSR_ILLW)
-			device_printf(sc->sc_dev, "illegal write\n");
-	}
-
-	restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
-	    HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
-	if (restart) {
-		device_printf(sc->sc_dev, "abort, resetting.\n");
-		hifnstats.hst_abort++;
-		hifn_abort(sc);
-		HIFN_UNLOCK(sc);
-		return;
-	}
-
-	if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
-		/*
-		 * If no slots to process and we receive a "waiting on
-		 * command" interrupt, we disable the "waiting on command"
-		 * (by clearing it).
-		 */
-		sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
-		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
-	}
-
-	/* clear the rings */
-	i = dma->resk; u = dma->resu;
-	while (u != 0) {
-		HIFN_RESR_SYNC(sc, i,
-		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
-		if (dma->resr[i].l & htole32(HIFN_D_VALID)) {
-			HIFN_RESR_SYNC(sc, i,
-			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-			break;
-		}
-
-		if (i != HIFN_D_RES_RSIZE) {
-			struct hifn_command *cmd;
-			u_int8_t *macbuf = NULL;
-
-			HIFN_RES_SYNC(sc, i, BUS_DMASYNC_POSTREAD);
-			cmd = dma->hifn_commands[i];
-			KASSERT(cmd != NULL,
-				("hifn_intr: null command slot %u", i));
-			dma->hifn_commands[i] = NULL;
-
-			if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
-				macbuf = dma->result_bufs[i];
-				macbuf += 12;
-			}
-
-			hifn_callback(sc, cmd, macbuf);
-			hifnstats.hst_opackets++;
-			u--;
-		}
-
-		if (++i == (HIFN_D_RES_RSIZE + 1))
-			i = 0;
-	}
-	dma->resk = i; dma->resu = u;
-
-	i = dma->srck; u = dma->srcu;
-	while (u != 0) {
-		if (i == HIFN_D_SRC_RSIZE)
-			i = 0;
-		HIFN_SRCR_SYNC(sc, i,
-		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
-		if (dma->srcr[i].l & htole32(HIFN_D_VALID)) {
-			HIFN_SRCR_SYNC(sc, i,
-			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-			break;
-		}
-		i++, u--;
-	}
-	dma->srck = i; dma->srcu = u;
-
-	i = dma->cmdk; u = dma->cmdu;
-	while (u != 0) {
-		HIFN_CMDR_SYNC(sc, i,
-		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
-		if (dma->cmdr[i].l & htole32(HIFN_D_VALID)) {
-			HIFN_CMDR_SYNC(sc, i,
-			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-			break;
-		}
-		if (i != HIFN_D_CMD_RSIZE) {
-			u--;
-			HIFN_CMD_SYNC(sc, i, BUS_DMASYNC_POSTWRITE);
-		}
-		if (++i == (HIFN_D_CMD_RSIZE + 1))
-			i = 0;
-	}
-	dma->cmdk = i; dma->cmdu = u;
-
-	HIFN_UNLOCK(sc);
-
-	if (sc->sc_needwakeup) {		/* XXX check high watermark */
-		int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
-#ifdef HIFN_DEBUG
-		if (hifn_debug)
-			device_printf(sc->sc_dev,
-				"wakeup crypto (%x) u %d/%d/%d/%d\n",
-				sc->sc_needwakeup,
-				dma->cmdu, dma->srcu, dma->dstu, dma->resu);
-#endif
-		sc->sc_needwakeup &= ~wakeup;
-		crypto_unblock(sc->sc_cid, wakeup);
-	}
-}
-
-/*
- * Allocate a new 'session' and return an encoded session id.  'sidp'
- * contains our registration id, and should contain an encoded session
- * id on successful allocation.
- */
-static int
-hifn_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
-{
-	struct cryptoini *c;
-	struct hifn_softc *sc = device_get_softc(dev);
-	int mac = 0, cry = 0, sesn;
-	struct hifn_session *ses = NULL;
-
-	KASSERT(sc != NULL, ("hifn_newsession: null softc"));
-	if (sidp == NULL || cri == NULL || sc == NULL)
-		return (EINVAL);
-
-	HIFN_LOCK(sc);
-	if (sc->sc_sessions == NULL) {
-		ses = sc->sc_sessions = (struct hifn_session *)kmalloc(
-		    sizeof(*ses), M_DEVBUF, M_NOWAIT);
-		if (ses == NULL) {
-			HIFN_UNLOCK(sc);
-			return (ENOMEM);
-		}
-		sesn = 0;
-		sc->sc_nsessions = 1;
-	} else {
-		for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
-			if (!sc->sc_sessions[sesn].hs_used) {
-				ses = &sc->sc_sessions[sesn];
-				break;
-			}
-		}
-
-		if (ses == NULL) {
-			sesn = sc->sc_nsessions;
-			ses = (struct hifn_session *)kmalloc((sesn + 1) *
-                            sizeof(*ses), M_DEVBUF, M_NOWAIT);
-			if (ses == NULL) {
-				HIFN_UNLOCK(sc);
-				return (ENOMEM);
-			}
-			bcopy(sc->sc_sessions, ses, sesn * sizeof(*ses));
-			bzero(sc->sc_sessions, sesn * sizeof(*ses));
-			kfree(sc->sc_sessions, M_DEVBUF);
-			sc->sc_sessions = ses;
-			ses = &sc->sc_sessions[sesn];
-			sc->sc_nsessions++;
-		}
-	}
-	HIFN_UNLOCK(sc);
-
-	bzero(ses, sizeof(*ses));
-	ses->hs_used = 1;
-
-	for (c = cri; c != NULL; c = c->cri_next) {
-		switch (c->cri_alg) {
-		case CRYPTO_MD5:
-		case CRYPTO_SHA1:
-		case CRYPTO_MD5_HMAC:
-		case CRYPTO_SHA1_HMAC:
-			if (mac)
-				return (EINVAL);
-			mac = 1;
-			ses->hs_mlen = c->cri_mlen;
-			if (ses->hs_mlen == 0) {
-				switch (c->cri_alg) {
-				case CRYPTO_MD5:
-				case CRYPTO_MD5_HMAC:
-					ses->hs_mlen = 16;
-					break;
-				case CRYPTO_SHA1:
-				case CRYPTO_SHA1_HMAC:
-					ses->hs_mlen = 20;
-					break;
-				}
-			}
-			break;
-		case CRYPTO_DES_CBC:
-		case CRYPTO_3DES_CBC:
-		case CRYPTO_AES_CBC:
-			/* XXX this may read fewer, does it matter? */
-			read_random(ses->hs_iv,
-				(c->cri_alg == CRYPTO_AES_CBC ?
-					HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH),
-				0);
-			/*FALLTHROUGH*/
-		case CRYPTO_ARC4:
-			if (cry)
-				return (EINVAL);
-			cry = 1;
-			break;
-		default:
-			return (EINVAL);
-		}
-	}
-	if (mac == 0 && cry == 0)
-		return (EINVAL);
-
-	*sidp = HIFN_SID(device_get_unit(sc->sc_dev), sesn);
-
-	return (0);
-}
-
-/*
- * Deallocate a session.
- * XXX this routine should run a zero'd mac/encrypt key into context ram.
- * XXX to blow away any keys already stored there.
- */
-#define	CRYPTO_SESID2LID(_sid)	(((u_int32_t) (_sid)) & 0xffffffff)
-
-static int
-hifn_freesession(device_t dev, u_int64_t tid)
-{
-	struct hifn_softc *sc = device_get_softc(dev);
-	int session, error;
-	u_int32_t sid = CRYPTO_SESID2LID(tid);
-
-	KASSERT(sc != NULL, ("hifn_freesession: null softc"));
-	if (sc == NULL)
-		return (EINVAL);
-
-	HIFN_LOCK(sc);
-	session = HIFN_SESSION(sid);
-	if (session < sc->sc_nsessions) {
-		bzero(&sc->sc_sessions[session], sizeof(struct hifn_session));
-		error = 0;
-	} else
-		error = EINVAL;
-	HIFN_UNLOCK(sc);
-
-	return (error);
-}
-
-static int
-hifn_process(device_t dev, struct cryptop *crp, int hint)
-{
-	struct hifn_softc *sc = device_get_softc(dev);
-	struct hifn_command *cmd = NULL;
-	int session, err, ivlen;
-	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
-
-	if (crp == NULL || crp->crp_callback == NULL) {
-		hifnstats.hst_invalid++;
-		return (EINVAL);
-	}
-	session = HIFN_SESSION(crp->crp_sid);
-
-	if (sc == NULL || session >= sc->sc_nsessions) {
-		err = EINVAL;
-		goto errout;
-	}
-
-	cmd = kmalloc(sizeof(struct hifn_command), M_DEVBUF, M_INTWAIT | M_ZERO);
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		cmd->src_m = (struct mbuf *)crp->crp_buf;
-		cmd->dst_m = (struct mbuf *)crp->crp_buf;
-	} else if (crp->crp_flags & CRYPTO_F_IOV) {
-		cmd->src_io = (struct uio *)crp->crp_buf;
-		cmd->dst_io = (struct uio *)crp->crp_buf;
-	} else {
-		err = EINVAL;
-		goto errout;	/* XXX we don't handle contiguous buffers! */
-	}
-
-	crd1 = crp->crp_desc;
-	if (crd1 == NULL) {
-		err = EINVAL;
-		goto errout;
-	}
-	crd2 = crd1->crd_next;
-
-	if (crd2 == NULL) {
-		if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
-		    crd1->crd_alg == CRYPTO_SHA1_HMAC ||
-		    crd1->crd_alg == CRYPTO_SHA1 ||
-		    crd1->crd_alg == CRYPTO_MD5) {
-			maccrd = crd1;
-			enccrd = NULL;
-		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
-		    crd1->crd_alg == CRYPTO_3DES_CBC ||
-		    crd1->crd_alg == CRYPTO_AES_CBC ||
-		    crd1->crd_alg == CRYPTO_ARC4) {
-			if ((crd1->crd_flags & CRD_F_ENCRYPT) == 0)
-				cmd->base_masks |= HIFN_BASE_CMD_DECODE;
-			maccrd = NULL;
-			enccrd = crd1;
-		} else {
-			err = EINVAL;
-			goto errout;
-		}
-	} else {
-		if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
-                     crd1->crd_alg == CRYPTO_SHA1_HMAC ||
-                     crd1->crd_alg == CRYPTO_MD5 ||
-                     crd1->crd_alg == CRYPTO_SHA1) &&
-		    (crd2->crd_alg == CRYPTO_DES_CBC ||
-		     crd2->crd_alg == CRYPTO_3DES_CBC ||
-		     crd2->crd_alg == CRYPTO_AES_CBC ||
-		     crd2->crd_alg == CRYPTO_ARC4) &&
-		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
-			cmd->base_masks = HIFN_BASE_CMD_DECODE;
-			maccrd = crd1;
-			enccrd = crd2;
-		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
-		     crd1->crd_alg == CRYPTO_ARC4 ||
-		     crd1->crd_alg == CRYPTO_3DES_CBC ||
-		     crd1->crd_alg == CRYPTO_AES_CBC) &&
-		    (crd2->crd_alg == CRYPTO_MD5_HMAC ||
-                     crd2->crd_alg == CRYPTO_SHA1_HMAC ||
-                     crd2->crd_alg == CRYPTO_MD5 ||
-                     crd2->crd_alg == CRYPTO_SHA1) &&
-		    (crd1->crd_flags & CRD_F_ENCRYPT)) {
-			enccrd = crd1;
-			maccrd = crd2;
-		} else {
-			/*
-			 * We cannot order the 7751 as requested
-			 */
-			err = EINVAL;
-			goto errout;
-		}
-	}
-
-	if (enccrd) {
-		cmd->enccrd = enccrd;
-		cmd->base_masks |= HIFN_BASE_CMD_CRYPT;
-		switch (enccrd->crd_alg) {
-		case CRYPTO_ARC4:
-			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_RC4;
-			break;
-		case CRYPTO_DES_CBC:
-			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_DES |
-			    HIFN_CRYPT_CMD_MODE_CBC |
-			    HIFN_CRYPT_CMD_NEW_IV;
-			break;
-		case CRYPTO_3DES_CBC:
-			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_3DES |
-			    HIFN_CRYPT_CMD_MODE_CBC |
-			    HIFN_CRYPT_CMD_NEW_IV;
-			break;
-		case CRYPTO_AES_CBC:
-			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_AES |
-			    HIFN_CRYPT_CMD_MODE_CBC |
-			    HIFN_CRYPT_CMD_NEW_IV;
-			break;
-		default:
-			err = EINVAL;
-			goto errout;
-		}
-		if (enccrd->crd_alg != CRYPTO_ARC4) {
-			ivlen = ((enccrd->crd_alg == CRYPTO_AES_CBC) ?
-				HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
-			if (enccrd->crd_flags & CRD_F_ENCRYPT) {
-				if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
-					bcopy(enccrd->crd_iv, cmd->iv, ivlen);
-				else
-					bcopy(sc->sc_sessions[session].hs_iv,
-					    cmd->iv, ivlen);
-
-				if ((enccrd->crd_flags & CRD_F_IV_PRESENT)
-				    == 0) {
-					crypto_copyback(crp->crp_flags,
-					    crp->crp_buf, enccrd->crd_inject,
-					    ivlen, cmd->iv);
-				}
-			} else {
-				if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
-					bcopy(enccrd->crd_iv, cmd->iv, ivlen);
-				else {
-					crypto_copydata(crp->crp_flags,
-					    crp->crp_buf, enccrd->crd_inject,
-					    ivlen, cmd->iv);
-				}
-			}
-		}
-
-		if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT)
-			cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
-		cmd->ck = enccrd->crd_key;
-		cmd->cklen = enccrd->crd_klen >> 3;
-		cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
-
-		/*
-		 * Need to specify the size for the AES key in the masks.
-		 */
-		if ((cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) ==
-		    HIFN_CRYPT_CMD_ALG_AES) {
-			switch (cmd->cklen) {
-			case 16:
-				cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_128;
-				break;
-			case 24:
-				cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_192;
-				break;
-			case 32:
-				cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_256;
-				break;
-			default:
-				err = EINVAL;
-				goto errout;
-			}
-		}
-	}
-
-	if (maccrd) {
-		cmd->maccrd = maccrd;
-		cmd->base_masks |= HIFN_BASE_CMD_MAC;
-
-		switch (maccrd->crd_alg) {
-		case CRYPTO_MD5:
-			cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
-			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
-			    HIFN_MAC_CMD_POS_IPSEC;
-                       break;
-		case CRYPTO_MD5_HMAC:
-			cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
-			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
-			    HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
-			break;
-		case CRYPTO_SHA1:
-			cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
-			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
-			    HIFN_MAC_CMD_POS_IPSEC;
-			break;
-		case CRYPTO_SHA1_HMAC:
-			cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
-			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
-			    HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
-			break;
-		}
-
-		if (maccrd->crd_alg == CRYPTO_SHA1_HMAC ||
-		    maccrd->crd_alg == CRYPTO_MD5_HMAC) {
-			cmd->mac_masks |= HIFN_MAC_CMD_NEW_KEY;
-			bcopy(maccrd->crd_key, cmd->mac, maccrd->crd_klen >> 3);
-			bzero(cmd->mac + (maccrd->crd_klen >> 3),
-			    HIFN_MAC_KEY_LENGTH - (maccrd->crd_klen >> 3));
-		}
-	}
-
-	cmd->crp = crp;
-	cmd->session_num = session;
-	cmd->softc = sc;
-
-	err = hifn_crypto(sc, cmd, crp, hint);
-	if (!err) {
-		return 0;
-	} else if (err == ERESTART) {
-		/*
-		 * There weren't enough resources to dispatch the request
-		 * to the part.  Notify the caller so they'll requeue this
-		 * request and resubmit it again soon.
-		 */
-#ifdef HIFN_DEBUG
-		if (hifn_debug)
-			device_printf(sc->sc_dev, "requeue request\n");
-#endif
-		kfree(cmd, M_DEVBUF);
-		sc->sc_needwakeup |= CRYPTO_SYMQ;
-		return (err);
-	}
-
-errout:
-	if (cmd != NULL)
-		kfree(cmd, M_DEVBUF);
-	if (err == EINVAL)
-		hifnstats.hst_invalid++;
-	else
-		hifnstats.hst_nomem++;
-	crp->crp_etype = err;
-	crypto_done(crp);
-	return (err);
-}
-
-static void
-hifn_abort(struct hifn_softc *sc)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	struct hifn_command *cmd;
-	struct cryptop *crp;
-	int i, u;
-
-	i = dma->resk; u = dma->resu;
-	while (u != 0) {
-		cmd = dma->hifn_commands[i];
-		KASSERT(cmd != NULL, ("hifn_abort: null command slot %u", i));
-		dma->hifn_commands[i] = NULL;
-		crp = cmd->crp;
-
-		if ((dma->resr[i].l & htole32(HIFN_D_VALID)) == 0) {
-			/* Salvage what we can. */
-			u_int8_t *macbuf;
-
-			if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
-				macbuf = dma->result_bufs[i];
-				macbuf += 12;
-			} else
-				macbuf = NULL;
-			hifnstats.hst_opackets++;
-			hifn_callback(sc, cmd, macbuf);
-		} else {
-			if (cmd->src_map == cmd->dst_map) {
-				bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
-				    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
-			} else {
-				bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
-				    BUS_DMASYNC_POSTWRITE);
-				bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
-				    BUS_DMASYNC_POSTREAD);
-			}
-
-			if (cmd->src_m != cmd->dst_m) {
-				m_freem(cmd->src_m);
-				crp->crp_buf = (caddr_t)cmd->dst_m;
-			}
-
-			/* non-shared buffers cannot be restarted */
-			if (cmd->src_map != cmd->dst_map) {
-				/*
-				 * XXX should be EAGAIN, delayed until
-				 * after the reset.
-				 */
-				crp->crp_etype = ENOMEM;
-				bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
-				bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
-			} else
-				crp->crp_etype = ENOMEM;
-
-			bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
-			bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
-
-			kfree(cmd, M_DEVBUF);
-			if (crp->crp_etype != EAGAIN)
-				crypto_done(crp);
-		}
-
-		if (++i == HIFN_D_RES_RSIZE)
-			i = 0;
-		u--;
-	}
-	dma->resk = i; dma->resu = u;
-
-	hifn_reset_board(sc, 1);
-	hifn_init_dma(sc);
-	hifn_init_pci_registers(sc);
-}
-
-static void
-hifn_callback(struct hifn_softc *sc, struct hifn_command *cmd, u_int8_t *macbuf)
-{
-	struct hifn_dma *dma = sc->sc_dma;
-	struct cryptop *crp = cmd->crp;
-	struct cryptodesc *crd;
-	struct mbuf *m;
-	int totlen, i, u, ivlen;
-
-	if (cmd->src_map == cmd->dst_map) {
-		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
-		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
-	} else {
-		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
-		    BUS_DMASYNC_POSTWRITE);
-		bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
-		    BUS_DMASYNC_POSTREAD);
-	}
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		if (cmd->src_m != cmd->dst_m) {
-			crp->crp_buf = (caddr_t)cmd->dst_m;
-			totlen = cmd->src_mapsize;
-			for (m = cmd->dst_m; m != NULL; m = m->m_next) {
-				if (totlen < m->m_len) {
-					m->m_len = totlen;
-					totlen = 0;
-				} else
-					totlen -= m->m_len;
-			}
-			cmd->dst_m->m_pkthdr.len = cmd->src_m->m_pkthdr.len;
-			m_freem(cmd->src_m);
-		}
-	}
-
-	if (cmd->sloplen != 0) {
-		crypto_copyback(crp->crp_flags, crp->crp_buf,
-		    cmd->src_mapsize - cmd->sloplen, cmd->sloplen,
-		    (caddr_t)&dma->slop[cmd->slopidx]);
-	}
-
-	i = dma->dstk; u = dma->dstu;
-	while (u != 0) {
-		if (i == HIFN_D_DST_RSIZE)
-			i = 0;
-		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
-		if (dma->dstr[i].l & htole32(HIFN_D_VALID)) {
-			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
-			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-			break;
-		}
-		i++, u--;
-	}
-	dma->dstk = i; dma->dstu = u;
-
-	hifnstats.hst_obytes += cmd->dst_mapsize;
-
-	if ((cmd->base_masks & (HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE)) ==
-	    HIFN_BASE_CMD_CRYPT) {
-		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-			if (crd->crd_alg != CRYPTO_DES_CBC &&
-			    crd->crd_alg != CRYPTO_3DES_CBC &&
-			    crd->crd_alg != CRYPTO_AES_CBC)
-				continue;
-			ivlen = ((crd->crd_alg == CRYPTO_AES_CBC) ?
-				 HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
-			crypto_copydata(crp->crp_flags, crp->crp_buf,
-			    crd->crd_skip + crd->crd_len - ivlen, ivlen,
-			    cmd->softc->sc_sessions[cmd->session_num].hs_iv);
-			break;
-		}
-	}
-
-	if (macbuf != NULL) {
-		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-                        int len;
-
-			if (crd->crd_alg != CRYPTO_MD5 &&
-			    crd->crd_alg != CRYPTO_SHA1 &&
-			    crd->crd_alg != CRYPTO_MD5_HMAC &&
-			    crd->crd_alg != CRYPTO_SHA1_HMAC) {
-				continue;
-			}
-			len = cmd->softc->sc_sessions[cmd->session_num].hs_mlen;
-			crypto_copyback(crp->crp_flags, crp->crp_buf,
-			    crd->crd_inject, len, macbuf);
-			break;
-		}
-	}
-
-	if (cmd->src_map != cmd->dst_map) {
-		bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
-		bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
-	}
-	bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
-	bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
-	kfree(cmd, M_DEVBUF);
-	crypto_done(crp);
-}
-
-/*
- * 7811 PB3 rev/2 parts lock-up on burst writes to Group 0
- * and Group 1 registers; avoid conditions that could create
- * burst writes by doing a read in between the writes.
- *
- * NB: The read we interpose is always to the same register;
- *     we do this because reading from an arbitrary (e.g. last)
- *     register may not always work.
- */
-static void
-hifn_write_reg_0(struct hifn_softc *sc, bus_size_t reg, u_int32_t val)
-{
-	if (sc->sc_flags & HIFN_IS_7811) {
-		if (sc->sc_bar0_lastreg == reg - 4)
-			bus_space_read_4(sc->sc_st0, sc->sc_sh0, HIFN_0_PUCNFG);
-		sc->sc_bar0_lastreg = reg;
-	}
-	bus_space_write_4(sc->sc_st0, sc->sc_sh0, reg, val);
-}
-
-static void
-hifn_write_reg_1(struct hifn_softc *sc, bus_size_t reg, u_int32_t val)
-{
-	if (sc->sc_flags & HIFN_IS_7811) {
-		if (sc->sc_bar1_lastreg == reg - 4)
-			bus_space_read_4(sc->sc_st1, sc->sc_sh1, HIFN_1_REVID);
-		sc->sc_bar1_lastreg = reg;
-	}
-	bus_space_write_4(sc->sc_st1, sc->sc_sh1, reg, val);
-}
diff --git a/sys/dev/crypto/hifn/hifn7751reg.h b/sys/dev/crypto/hifn/hifn7751reg.h
deleted file mode 100644
index d845074080..0000000000
--- a/sys/dev/crypto/hifn/hifn7751reg.h
+++ /dev/null
@@ -1,590 +0,0 @@
-/* $FreeBSD: src/sys/dev/hifn/hifn7751reg.h,v 1.1.2.1 2002/11/21 23:37:11 sam Exp $ */
-/* $DragonFly: src/sys/dev/crypto/hifn/hifn7751reg.h,v 1.3 2007/12/04 09:11:12 hasso Exp $ */
-/*	$OpenBSD: hifn7751reg.h,v 1.35 2002/04/08 17:49:42 jason Exp $	*/
-
-/*-
- * Invertex AEON / Hifn 7751 driver
- * Copyright (c) 1999 Invertex Inc. All rights reserved.
- * Copyright (c) 1999 Theo de Raadt
- * Copyright (c) 2000-2001 Network Security Technologies, Inc.
- *			http://www.netsec.net
- *
- * Please send any comments, feedback, bug-fixes, or feature requests to
- * software@invertex.com.
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-#ifndef __HIFN_H__
-#define	__HIFN_H__
-
-#include <sys/endian.h>
-
-/*
- * Some PCI configuration space offset defines.  The names were made
- * identical to the names used by the Linux kernel.
- */
-#ifdef notyet
-#define	HIFN_BAR0		(PCIR_MAPS+0x0)	/* PUC register map */
-#define	HIFN_BAR1		(PCIR_MAPS+0x4)	/* DMA register map */
-#else
-#define	HIFN_BAR0		PCIR_BAR(0)	/* PUC register map */
-#define	HIFN_BAR1		PCIR_BAR(1)	/* DMA register map */
-#endif
-#define	HIFN_TRDY_TIMEOUT	0x40
-#define	HIFN_RETRY_TIMEOUT	0x41
-
-/*
- * PCI vendor and device identifiers
- * (the names are preserved from their OpenBSD source).
- */
-#define	PCI_VENDOR_HIFN		0x13a3		/* Hifn */
-#define	PCI_PRODUCT_HIFN_7751	0x0005		/* 7751 */
-#define	PCI_PRODUCT_HIFN_6500	0x0006		/* 6500 */
-#define	PCI_PRODUCT_HIFN_7811	0x0007		/* 7811 */
-#define	PCI_PRODUCT_HIFN_7951	0x0012		/* 7951 */
-#define	PCI_PRODUCT_HIFN_7955	0x0020		/* 7954/7955 */
-#define	PCI_PRODUCT_HIFN_7956	0x001d		/* 7956 */
-
-#define	PCI_VENDOR_INVERTEX	0x14e1		/* Invertex */
-#define	PCI_PRODUCT_INVERTEX_AEON 0x0005	/* AEON */
-
-#define	PCI_VENDOR_NETSEC	0x1660		/* NetSec */
-#define	PCI_PRODUCT_NETSEC_7751	0x7751		/* 7751 */
-
-/*
- * The values below should multiple of 4 -- and be large enough to handle
- * any command the driver implements.
- *
- * MAX_COMMAND = base command + mac command + encrypt command +
- *			mac-key + rc4-key
- * MAX_RESULT  = base result + mac result + mac + encrypt result
- *			
- *
- */
-#define	HIFN_MAX_COMMAND	(8 + 8 + 8 + 64 + 260)
-#define	HIFN_MAX_RESULT		(8 + 4 + 20 + 4)
-
-/*
- * hifn_desc_t
- *
- * Holds an individual descriptor for any of the rings.
- */
-typedef struct hifn_desc {
-	volatile u_int32_t l;		/* length and status bits */
-	volatile u_int32_t p;
-} hifn_desc_t;
-
-/*
- * Masks for the "length" field of struct hifn_desc.
- */
-#define	HIFN_D_LENGTH		0x0000ffff	/* length bit mask */
-#define	HIFN_D_MASKDONEIRQ	0x02000000	/* mask the done interrupt */
-#define	HIFN_D_DESTOVER		0x04000000	/* destination overflow */
-#define	HIFN_D_OVER		0x08000000	/* overflow */
-#define	HIFN_D_LAST		0x20000000	/* last descriptor in chain */
-#define	HIFN_D_JUMP		0x40000000	/* jump descriptor */
-#define	HIFN_D_VALID		0x80000000	/* valid bit */
-
-
-/*
- * Processing Unit Registers (offset from BASEREG0)
- */
-#define	HIFN_0_PUDATA		0x00	/* Processing Unit Data */
-#define	HIFN_0_PUCTRL		0x04	/* Processing Unit Control */
-#define	HIFN_0_PUISR		0x08	/* Processing Unit Interrupt Status */
-#define	HIFN_0_PUCNFG		0x0c	/* Processing Unit Configuration */
-#define	HIFN_0_PUIER		0x10	/* Processing Unit Interrupt Enable */
-#define	HIFN_0_PUSTAT		0x14	/* Processing Unit Status/Chip ID */
-#define	HIFN_0_FIFOSTAT		0x18	/* FIFO Status */
-#define	HIFN_0_FIFOCNFG		0x1c	/* FIFO Configuration */
-#define	HIFN_0_PUCTRL2		0x28	/* Processing Unit Control (2nd map) */
-#define	HIFN_0_SPACESIZE	0x20	/* Register space size */
-
-/* Processing Unit Control Register (HIFN_0_PUCTRL) */
-#define	HIFN_PUCTRL_CLRSRCFIFO	0x0010	/* clear source fifo */
-#define	HIFN_PUCTRL_STOP	0x0008	/* stop pu */
-#define	HIFN_PUCTRL_LOCKRAM	0x0004	/* lock ram */
-#define	HIFN_PUCTRL_DMAENA	0x0002	/* enable dma */
-#define	HIFN_PUCTRL_RESET	0x0001	/* Reset processing unit */
-
-/* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
-#define	HIFN_PUISR_CMDINVAL	0x8000	/* Invalid command interrupt */
-#define	HIFN_PUISR_DATAERR	0x4000	/* Data error interrupt */
-#define	HIFN_PUISR_SRCFIFO	0x2000	/* Source FIFO ready interrupt */
-#define	HIFN_PUISR_DSTFIFO	0x1000	/* Destination FIFO ready interrupt */
-#define	HIFN_PUISR_DSTOVER	0x0200	/* Destination overrun interrupt */
-#define	HIFN_PUISR_SRCCMD	0x0080	/* Source command interrupt */
-#define	HIFN_PUISR_SRCCTX	0x0040	/* Source context interrupt */
-#define	HIFN_PUISR_SRCDATA	0x0020	/* Source data interrupt */
-#define	HIFN_PUISR_DSTDATA	0x0010	/* Destination data interrupt */
-#define	HIFN_PUISR_DSTRESULT	0x0004	/* Destination result interrupt */
-
-/* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
-#define	HIFN_PUCNFG_DRAMMASK	0xe000	/* DRAM size mask */
-#define	HIFN_PUCNFG_DSZ_256K	0x0000	/* 256k dram */
-#define	HIFN_PUCNFG_DSZ_512K	0x2000	/* 512k dram */
-#define	HIFN_PUCNFG_DSZ_1M	0x4000	/* 1m dram */
-#define	HIFN_PUCNFG_DSZ_2M	0x6000	/* 2m dram */
-#define	HIFN_PUCNFG_DSZ_4M	0x8000	/* 4m dram */
-#define	HIFN_PUCNFG_DSZ_8M	0xa000	/* 8m dram */
-#define	HIFN_PUNCFG_DSZ_16M	0xc000	/* 16m dram */
-#define	HIFN_PUCNFG_DSZ_32M	0xe000	/* 32m dram */
-#define	HIFN_PUCNFG_DRAMREFRESH	0x1800	/* DRAM refresh rate mask */
-#define	HIFN_PUCNFG_DRFR_512	0x0000	/* 512 divisor of ECLK */
-#define	HIFN_PUCNFG_DRFR_256	0x0800	/* 256 divisor of ECLK */
-#define	HIFN_PUCNFG_DRFR_128	0x1000	/* 128 divisor of ECLK */
-#define	HIFN_PUCNFG_TCALLPHASES	0x0200	/* your guess is as good as mine... */
-#define	HIFN_PUCNFG_TCDRVTOTEM	0x0100	/* your guess is as good as mine... */
-#define	HIFN_PUCNFG_BIGENDIAN	0x0080	/* DMA big endian mode */
-#define	HIFN_PUCNFG_BUS32	0x0040	/* Bus width 32bits */
-#define	HIFN_PUCNFG_BUS16	0x0000	/* Bus width 16 bits */
-#define	HIFN_PUCNFG_CHIPID	0x0020	/* Allow chipid from PUSTAT */
-#define	HIFN_PUCNFG_DRAM	0x0010	/* Context RAM is DRAM */
-#define	HIFN_PUCNFG_SRAM	0x0000	/* Context RAM is SRAM */
-#define	HIFN_PUCNFG_COMPSING	0x0004	/* Enable single compression context */
-#define	HIFN_PUCNFG_ENCCNFG	0x0002	/* Encryption configuration */
-
-/* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
-#define	HIFN_PUIER_CMDINVAL	0x8000	/* Invalid command interrupt */
-#define	HIFN_PUIER_DATAERR	0x4000	/* Data error interrupt */
-#define	HIFN_PUIER_SRCFIFO	0x2000	/* Source FIFO ready interrupt */
-#define	HIFN_PUIER_DSTFIFO	0x1000	/* Destination FIFO ready interrupt */
-#define	HIFN_PUIER_DSTOVER	0x0200	/* Destination overrun interrupt */
-#define	HIFN_PUIER_SRCCMD	0x0080	/* Source command interrupt */
-#define	HIFN_PUIER_SRCCTX	0x0040	/* Source context interrupt */
-#define	HIFN_PUIER_SRCDATA	0x0020	/* Source data interrupt */
-#define	HIFN_PUIER_DSTDATA	0x0010	/* Destination data interrupt */
-#define	HIFN_PUIER_DSTRESULT	0x0004	/* Destination result interrupt */
-
-/* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
-#define	HIFN_PUSTAT_CMDINVAL	0x8000	/* Invalid command interrupt */
-#define	HIFN_PUSTAT_DATAERR	0x4000	/* Data error interrupt */
-#define	HIFN_PUSTAT_SRCFIFO	0x2000	/* Source FIFO ready interrupt */
-#define	HIFN_PUSTAT_DSTFIFO	0x1000	/* Destination FIFO ready interrupt */
-#define	HIFN_PUSTAT_DSTOVER	0x0200	/* Destination overrun interrupt */
-#define	HIFN_PUSTAT_SRCCMD	0x0080	/* Source command interrupt */
-#define	HIFN_PUSTAT_SRCCTX	0x0040	/* Source context interrupt */
-#define	HIFN_PUSTAT_SRCDATA	0x0020	/* Source data interrupt */
-#define	HIFN_PUSTAT_DSTDATA	0x0010	/* Destination data interrupt */
-#define	HIFN_PUSTAT_DSTRESULT	0x0004	/* Destination result interrupt */
-#define	HIFN_PUSTAT_CHIPREV	0x00ff	/* Chip revision mask */
-#define	HIFN_PUSTAT_CHIPENA	0xff00	/* Chip enabled mask */
-#define	HIFN_PUSTAT_ENA_2	0x1100	/* Level 2 enabled */
-#define	HIFN_PUSTAT_ENA_1	0x1000	/* Level 1 enabled */
-#define	HIFN_PUSTAT_ENA_0	0x3000	/* Level 0 enabled */
-#define	HIFN_PUSTAT_REV_2	0x0020	/* 7751 PT6/2 */
-#define	HIFN_PUSTAT_REV_3	0x0030	/* 7751 PT6/3 */
-
-/* FIFO Status Register (HIFN_0_FIFOSTAT) */
-#define	HIFN_FIFOSTAT_SRC	0x7f00	/* Source FIFO available */
-#define	HIFN_FIFOSTAT_DST	0x007f	/* Destination FIFO available */
-
-/* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
-#define	HIFN_FIFOCNFG_THRESHOLD	0x0400	/* must be written as 1 */
-
-/*
- * DMA Interface Registers (offset from BASEREG1)
- */
-#define	HIFN_1_DMA_CRAR		0x0c	/* DMA Command Ring Address */
-#define	HIFN_1_DMA_SRAR		0x1c	/* DMA Source Ring Address */
-#define	HIFN_1_DMA_RRAR		0x2c	/* DMA Result Ring Address */
-#define	HIFN_1_DMA_DRAR		0x3c	/* DMA Destination Ring Address */
-#define	HIFN_1_DMA_CSR		0x40	/* DMA Status and Control */
-#define	HIFN_1_DMA_IER		0x44	/* DMA Interrupt Enable */
-#define	HIFN_1_DMA_CNFG		0x48	/* DMA Configuration */
-#define	HIFN_1_PLL		0x4c	/* 7955/7956: PLL config */
-#define	HIFN_1_7811_RNGENA	0x60	/* 7811: rng enable */
-#define	HIFN_1_7811_RNGCFG	0x64	/* 7811: rng config */
-#define	HIFN_1_7811_RNGDAT	0x68	/* 7811: rng data */
-#define	HIFN_1_7811_RNGSTS	0x6c	/* 7811: rng status */
-#define	HIFN_1_DMA_CNFG2	0x6c	/* 7955/7956: dma config #2 */
-#define	HIFN_1_7811_MIPSRST	0x94	/* 7811: MIPS reset */
-#define	HIFN_1_REVID		0x98	/* Revision ID */
-
-#define	HIFN_1_PUB_RESET	0x204	/* Public/RNG Reset */
-#define	HIFN_1_PUB_BASE		0x300	/* Public Base Address */
-#define	HIFN_1_PUB_OPLEN	0x304	/* Public Operand Length */
-#define	HIFN_1_PUB_OP		0x308	/* Public Operand */
-#define	HIFN_1_PUB_STATUS	0x30c	/* Public Status */
-#define	HIFN_1_PUB_IEN		0x310	/* Public Interrupt nable */
-#define	HIFN_1_RNG_CONFIG	0x314	/* RNG config */
-#define	HIFN_1_RNG_DATA		0x318	/* RNG data */
-#define	HIFN_1_PUB_MEM		0x400	/* start of Public key memory */
-#define	HIFN_1_PUB_MEMEND	0xbff	/* end of Public key memory */
-
-/* DMA Status and Control Register (HIFN_1_DMA_CSR) */
-#define	HIFN_DMACSR_D_CTRLMASK	0xc0000000	/* Destinition Ring Control */
-#define	HIFN_DMACSR_D_CTRL_NOP	0x00000000	/* Dest. Control: no-op */
-#define	HIFN_DMACSR_D_CTRL_DIS	0x40000000	/* Dest. Control: disable */
-#define	HIFN_DMACSR_D_CTRL_ENA	0x80000000	/* Dest. Control: enable */
-#define	HIFN_DMACSR_D_ABORT	0x20000000	/* Destinition Ring PCIAbort */
-#define	HIFN_DMACSR_D_DONE	0x10000000	/* Destinition Ring Done */
-#define	HIFN_DMACSR_D_LAST	0x08000000	/* Destinition Ring Last */
-#define	HIFN_DMACSR_D_WAIT	0x04000000	/* Destinition Ring Waiting */
-#define	HIFN_DMACSR_D_OVER	0x02000000	/* Destinition Ring Overflow */
-#define	HIFN_DMACSR_R_CTRL	0x00c00000	/* Result Ring Control */
-#define	HIFN_DMACSR_R_CTRL_NOP	0x00000000	/* Result Control: no-op */
-#define	HIFN_DMACSR_R_CTRL_DIS	0x00400000	/* Result Control: disable */
-#define	HIFN_DMACSR_R_CTRL_ENA	0x00800000	/* Result Control: enable */
-#define	HIFN_DMACSR_R_ABORT	0x00200000	/* Result Ring PCI Abort */
-#define	HIFN_DMACSR_R_DONE	0x00100000	/* Result Ring Done */
-#define	HIFN_DMACSR_R_LAST	0x00080000	/* Result Ring Last */
-#define	HIFN_DMACSR_R_WAIT	0x00040000	/* Result Ring Waiting */
-#define	HIFN_DMACSR_R_OVER	0x00020000	/* Result Ring Overflow */
-#define	HIFN_DMACSR_S_CTRL	0x0000c000	/* Source Ring Control */
-#define	HIFN_DMACSR_S_CTRL_NOP	0x00000000	/* Source Control: no-op */
-#define	HIFN_DMACSR_S_CTRL_DIS	0x00004000	/* Source Control: disable */
-#define	HIFN_DMACSR_S_CTRL_ENA	0x00008000	/* Source Control: enable */
-#define	HIFN_DMACSR_S_ABORT	0x00002000	/* Source Ring PCI Abort */
-#define	HIFN_DMACSR_S_DONE	0x00001000	/* Source Ring Done */
-#define	HIFN_DMACSR_S_LAST	0x00000800	/* Source Ring Last */
-#define	HIFN_DMACSR_S_WAIT	0x00000400	/* Source Ring Waiting */
-#define	HIFN_DMACSR_ILLW	0x00000200	/* Illegal write (7811 only) */
-#define	HIFN_DMACSR_ILLR	0x00000100	/* Illegal read (7811 only) */
-#define	HIFN_DMACSR_C_CTRL	0x000000c0	/* Command Ring Control */
-#define	HIFN_DMACSR_C_CTRL_NOP	0x00000000	/* Command Control: no-op */
-#define	HIFN_DMACSR_C_CTRL_DIS	0x00000040	/* Command Control: disable */
-#define	HIFN_DMACSR_C_CTRL_ENA	0x00000080	/* Command Control: enable */
-#define	HIFN_DMACSR_C_ABORT	0x00000020	/* Command Ring PCI Abort */
-#define	HIFN_DMACSR_C_DONE	0x00000010	/* Command Ring Done */
-#define	HIFN_DMACSR_C_LAST	0x00000008	/* Command Ring Last */
-#define	HIFN_DMACSR_C_WAIT	0x00000004	/* Command Ring Waiting */
-#define	HIFN_DMACSR_PUBDONE	0x00000002	/* Public op done (7951 only) */
-#define	HIFN_DMACSR_ENGINE	0x00000001	/* Command Ring Engine IRQ */
-
-/* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
-#define	HIFN_DMAIER_D_ABORT	0x20000000	/* Destination Ring PCIAbort */
-#define	HIFN_DMAIER_D_DONE	0x10000000	/* Destination Ring Done */
-#define	HIFN_DMAIER_D_LAST	0x08000000	/* Destination Ring Last */
-#define	HIFN_DMAIER_D_WAIT	0x04000000	/* Destination Ring Waiting */
-#define	HIFN_DMAIER_D_OVER	0x02000000	/* Destination Ring Overflow */
-#define	HIFN_DMAIER_R_ABORT	0x00200000	/* Result Ring PCI Abort */
-#define	HIFN_DMAIER_R_DONE	0x00100000	/* Result Ring Done */
-#define	HIFN_DMAIER_R_LAST	0x00080000	/* Result Ring Last */
-#define	HIFN_DMAIER_R_WAIT	0x00040000	/* Result Ring Waiting */
-#define	HIFN_DMAIER_R_OVER	0x00020000	/* Result Ring Overflow */
-#define	HIFN_DMAIER_S_ABORT	0x00002000	/* Source Ring PCI Abort */
-#define	HIFN_DMAIER_S_DONE	0x00001000	/* Source Ring Done */
-#define	HIFN_DMAIER_S_LAST	0x00000800	/* Source Ring Last */
-#define	HIFN_DMAIER_S_WAIT	0x00000400	/* Source Ring Waiting */
-#define	HIFN_DMAIER_ILLW	0x00000200	/* Illegal write (7811 only) */
-#define	HIFN_DMAIER_ILLR	0x00000100	/* Illegal read (7811 only) */
-#define	HIFN_DMAIER_C_ABORT	0x00000020	/* Command Ring PCI Abort */
-#define	HIFN_DMAIER_C_DONE	0x00000010	/* Command Ring Done */
-#define	HIFN_DMAIER_C_LAST	0x00000008	/* Command Ring Last */
-#define	HIFN_DMAIER_C_WAIT	0x00000004	/* Command Ring Waiting */
-#define	HIFN_DMAIER_PUBDONE	0x00000002	/* public op done (7951 only) */
-#define	HIFN_DMAIER_ENGINE	0x00000001	/* Engine IRQ */
-
-/* DMA Configuration Register (HIFN_1_DMA_CNFG) */
-#define	HIFN_DMACNFG_BIGENDIAN	0x10000000	/* big endian mode */
-#define	HIFN_DMACNFG_POLLFREQ	0x00ff0000	/* Poll frequency mask */
-#define	HIFN_DMACNFG_UNLOCK	0x00000800
-#define	HIFN_DMACNFG_POLLINVAL	0x00000700	/* Invalid Poll Scalar */
-#define	HIFN_DMACNFG_LAST	0x00000010	/* Host control LAST bit */
-#define	HIFN_DMACNFG_MODE	0x00000004	/* DMA mode */
-#define	HIFN_DMACNFG_DMARESET	0x00000002	/* DMA Reset # */
-#define	HIFN_DMACNFG_MSTRESET	0x00000001	/* Master Reset # */
-
-/* DMA Configuration Register (HIFN_1_DMA_CNFG2) */
-#define	HIFN_DMACNFG2_PKSWAP32	(1 << 19)	/* swap the OPLEN/OP reg */
-#define	HIFN_DMACNFG2_PKSWAP8	(1 << 18)	/* swap the bits of OPLEN/OP */
-#define	HIFN_DMACNFG2_BAR0_SWAP32 (1<<17)	/* swap the bytes of BAR0 */
-#define	HIFN_DMACNFG2_BAR1_SWAP8 (1<<16)	/* swap the bits  of BAR0 */
-#define	HIFN_DMACNFG2_INIT_WRITE_BURST_SHIFT 12
-#define	HIFN_DMACNFG2_INIT_READ_BURST_SHIFT 8
-#define	HIFN_DMACNFG2_TGT_WRITE_BURST_SHIFT 4
-#define	HIFN_DMACNFG2_TGT_READ_BURST_SHIFT  0
-
-/* 7811 RNG Enable Register (HIFN_1_7811_RNGENA) */
-#define	HIFN_7811_RNGENA_ENA	0x00000001	/* enable RNG */
-
-/* 7811 RNG Config Register (HIFN_1_7811_RNGCFG) */
-#define	HIFN_7811_RNGCFG_PRE1	0x00000f00	/* first prescalar */
-#define	HIFN_7811_RNGCFG_OPRE	0x00000080	/* output prescalar */
-#define	HIFN_7811_RNGCFG_DEFL	0x00000f80	/* 2 words/ 1/100 sec */
-
-/* 7811 RNG Status Register (HIFN_1_7811_RNGSTS) */
-#define	HIFN_7811_RNGSTS_RDY	0x00004000	/* two numbers in FIFO */
-#define	HIFN_7811_RNGSTS_UFL	0x00001000	/* rng underflow */
-
-/* 7811 MIPS Reset Register (HIFN_1_7811_MIPSRST) */
-#define	HIFN_MIPSRST_BAR2SIZE	0xffff0000	/* sdram size */
-#define	HIFN_MIPSRST_GPRAMINIT	0x00008000	/* gpram can be accessed */
-#define	HIFN_MIPSRST_CRAMINIT	0x00004000	/* ctxram can be accessed */
-#define	HIFN_MIPSRST_LED2	0x00000400	/* external LED2 */
-#define	HIFN_MIPSRST_LED1	0x00000200	/* external LED1 */
-#define	HIFN_MIPSRST_LED0	0x00000100	/* external LED0 */
-#define	HIFN_MIPSRST_MIPSDIS	0x00000004	/* disable MIPS */
-#define	HIFN_MIPSRST_MIPSRST	0x00000002	/* warm reset MIPS */
-#define	HIFN_MIPSRST_MIPSCOLD	0x00000001	/* cold reset MIPS */
-
-/* Public key reset register (HIFN_1_PUB_RESET) */
-#define	HIFN_PUBRST_RESET	0x00000001	/* reset public/rng unit */
-
-/* Public operation register (HIFN_1_PUB_OP) */
-#define	HIFN_PUBOP_AOFFSET	0x0000003e	/* A offset */
-#define	HIFN_PUBOP_BOFFSET	0x00000fc0	/* B offset */
-#define	HIFN_PUBOP_MOFFSET	0x0003f000	/* M offset */
-#define	HIFN_PUBOP_OP_MASK	0x003c0000	/* Opcode: */
-#define	HIFN_PUBOP_OP_NOP	0x00000000	/*  NOP */
-#define	HIFN_PUBOP_OP_ADD	0x00040000	/*  ADD */
-#define	HIFN_PUBOP_OP_ADDC	0x00080000	/*  ADD w/carry */
-#define	HIFN_PUBOP_OP_SUB	0x000c0000	/*  SUB */
-#define	HIFN_PUBOP_OP_SUBC	0x00100000	/*  SUB w/carry */
-#define	HIFN_PUBOP_OP_MODADD	0x00140000	/*  Modular ADD */
-#define	HIFN_PUBOP_OP_MODSUB	0x00180000	/*  Modular SUB */
-#define	HIFN_PUBOP_OP_INCA	0x001c0000	/*  INC A */
-#define	HIFN_PUBOP_OP_DECA	0x00200000	/*  DEC A */
-#define	HIFN_PUBOP_OP_MULT	0x00240000	/*  MULT */
-#define	HIFN_PUBOP_OP_MODMULT	0x00280000	/*  Modular MULT */
-#define	HIFN_PUBOP_OP_MODRED	0x002c0000	/*  Modular Red */
-#define	HIFN_PUBOP_OP_MODEXP	0x00300000	/*  Modular Exp */
-
-/* Public operand length register (HIFN_1_PUB_OPLEN) */
-#define	HIFN_PUBOPLEN_MODLEN	0x0000007f
-#define	HIFN_PUBOPLEN_EXPLEN	0x0003ff80
-#define	HIFN_PUBOPLEN_REDLEN	0x003c0000
-
-/* Public status register (HIFN_1_PUB_STATUS) */
-#define	HIFN_PUBSTS_DONE	0x00000001	/* operation done */
-#define	HIFN_PUBSTS_CARRY	0x00000002	/* carry */
-
-/* Public interrupt enable register (HIFN_1_PUB_IEN) */
-#define	HIFN_PUBIEN_DONE	0x00000001	/* operation done interrupt */
-
-/* Random number generator config register (HIFN_1_RNG_CONFIG) */
-#define	HIFN_RNGCFG_ENA		0x00000001	/* enable rng */
-
-/*
- * Register offsets in register set 1
- */
-
-#define	HIFN_UNLOCK_SECRET1	0xf4
-#define	HIFN_UNLOCK_SECRET2	0xfc
-
-/*
- * PLL config register
- *
- * This register is present only on 7954/7955/7956 parts. It must be
- * programmed according to the bus interface method used by the h/w.
- * Note that the parts require a stable clock.  Since the PCI clock
- * may vary the reference clock must usually be used.  To avoid
- * overclocking the core logic, setup must be done carefully, refer
- * to the driver for details.  The exact multiplier required varies
- * by part and system configuration; refer to the Hifn documentation.
- */
-#define	HIFN_PLL_REF_SEL	0x00000001	/* REF/HBI clk selection */
-#define	HIFN_PLL_BP		0x00000002	/* bypass (used during setup) */
-/* bit 2 reserved */
-#define	HIFN_PLL_PK_CLK_SEL	0x00000008	/* public key clk select */
-#define	HIFN_PLL_PE_CLK_SEL	0x00000010	/* packet engine clk select */
-/* bits 5-9 reserved */
-#define	HIFN_PLL_MBSET		0x00000400	/* must be set to 1 */
-#define	HIFN_PLL_ND		0x00003800	/* Fpll_ref multiplier select */
-#define	HIFN_PLL_ND_SHIFT	11
-#define	HIFN_PLL_ND_2		0x00000000	/* 2x */
-#define	HIFN_PLL_ND_4		0x00000800	/* 4x */
-#define	HIFN_PLL_ND_6		0x00001000	/* 6x */
-#define	HIFN_PLL_ND_8		0x00001800	/* 8x */
-#define	HIFN_PLL_ND_10		0x00002000	/* 10x */
-#define	HIFN_PLL_ND_12		0x00002800	/* 12x */
-/* bits 14-15 reserved */
-#define	HIFN_PLL_IS		0x00010000	/* charge pump current select */
-/* bits 17-31 reserved */
-
-/*
- * Board configuration specifies only these bits.
- */
-#define        HIFN_PLL_CONFIG         (HIFN_PLL_IS|HIFN_PLL_ND|HIFN_PLL_REF_SEL)
-
-/*********************************************************************
- * Structs for board commands 
- *
- *********************************************************************/
-
-/*
- * Structure to help build up the command data structure.
- */
-typedef struct hifn_base_command {
-	volatile u_int16_t masks;
-	volatile u_int16_t session_num;
-	volatile u_int16_t total_source_count;
-	volatile u_int16_t total_dest_count;
-} hifn_base_command_t;
-
-#define	HIFN_BASE_CMD_COMP		0x0100	/* enable compression engine */
-#define	HIFN_BASE_CMD_PAD		0x0200	/* enable padding engine */
-#define	HIFN_BASE_CMD_MAC		0x0400
-#define	HIFN_BASE_CMD_CRYPT		0x0800
-#define	HIFN_BASE_CMD_DECODE		0x2000
-#define	HIFN_BASE_CMD_SRCLEN_M		0xc000
-#define	HIFN_BASE_CMD_SRCLEN_S		14
-#define	HIFN_BASE_CMD_DSTLEN_M		0x3000
-#define	HIFN_BASE_CMD_DSTLEN_S		12
-#define	HIFN_BASE_CMD_LENMASK_HI	0x30000
-#define	HIFN_BASE_CMD_LENMASK_LO	0x0ffff
-
-/*
- * Structure to help build up the command data structure.
- */
-typedef struct hifn_crypt_command {
-	volatile u_int16_t masks;
-	volatile u_int16_t header_skip;
-	volatile u_int16_t source_count;
-	volatile u_int16_t reserved;
-} hifn_crypt_command_t;
-
-#define	HIFN_CRYPT_CMD_ALG_MASK		0x0003		/* algorithm: */
-#define	HIFN_CRYPT_CMD_ALG_DES		0x0000		/*   DES */
-#define	HIFN_CRYPT_CMD_ALG_3DES		0x0001		/*   3DES */
-#define	HIFN_CRYPT_CMD_ALG_RC4		0x0002		/*   RC4 */
-#define	HIFN_CRYPT_CMD_ALG_AES		0x0003		/*   AES */
-#define	HIFN_CRYPT_CMD_MODE_MASK	0x0018		/* Encrypt/DES mode: */
-#define	HIFN_CRYPT_CMD_MODE_ECB		0x0000		/*   ECB */
-#define	HIFN_CRYPT_CMD_MODE_CBC		0x0008		/*   CBC */
-#define	HIFN_CRYPT_CMD_MODE_CFB		0x0010		/*   CFB */
-#define	HIFN_CRYPT_CMD_MODE_OFB		0x0018		/*   OFB */
-#define	HIFN_CRYPT_CMD_CLR_CTX		0x0040		/* clear context */
-#define	HIFN_CRYPT_CMD_NEW_KEY		0x0800		/* expect new key */
-#define	HIFN_CRYPT_CMD_NEW_IV		0x1000		/* expect new iv */
-
-#define	HIFN_CRYPT_CMD_SRCLEN_M		0xc000
-#define	HIFN_CRYPT_CMD_SRCLEN_S		14
-
-#define	HIFN_CRYPT_CMD_KSZ_MASK		0x0600		/* AES key size: */
-#define	HIFN_CRYPT_CMD_KSZ_128		0x0000		/*  128 bit */
-#define	HIFN_CRYPT_CMD_KSZ_192		0x0200		/*  192 bit */
-#define	HIFN_CRYPT_CMD_KSZ_256		0x0400		/*  256 bit */
-
-/*
- * Structure to help build up the command data structure.
- */
-typedef struct hifn_mac_command {
-	volatile u_int16_t masks;
-	volatile u_int16_t header_skip;
-	volatile u_int16_t source_count;
-	volatile u_int16_t reserved;
-} hifn_mac_command_t;
-
-#define	HIFN_MAC_CMD_ALG_MASK		0x0001
-#define	HIFN_MAC_CMD_ALG_SHA1		0x0000
-#define	HIFN_MAC_CMD_ALG_MD5		0x0001
-#define	HIFN_MAC_CMD_MODE_MASK		0x000c
-#define	HIFN_MAC_CMD_MODE_HMAC		0x0000
-#define	HIFN_MAC_CMD_MODE_SSL_MAC	0x0004
-#define	HIFN_MAC_CMD_MODE_HASH		0x0008
-#define	HIFN_MAC_CMD_MODE_FULL		0x0004
-#define	HIFN_MAC_CMD_TRUNC		0x0010
-#define	HIFN_MAC_CMD_RESULT		0x0020
-#define	HIFN_MAC_CMD_APPEND		0x0040
-#define	HIFN_MAC_CMD_SRCLEN_M		0xc000
-#define	HIFN_MAC_CMD_SRCLEN_S		14
-
-/*
- * MAC POS IPsec initiates authentication after encryption on encodes
- * and before decryption on decodes.
- */
-#define	HIFN_MAC_CMD_POS_IPSEC		0x0200
-#define	HIFN_MAC_CMD_NEW_KEY		0x0800
-
-struct hifn_comp_command {
-	volatile u_int16_t masks;
-	volatile u_int16_t header_skip;
-	volatile u_int16_t source_count;
-	volatile u_int16_t reserved;
-};
-
-#define	HIFN_COMP_CMD_SRCLEN_M		0xc000
-#define	HIFN_COMP_CMD_SRCLEN_S		14
-#define	HIFN_COMP_CMD_ONE		0x0100	/* must be one */
-#define	HIFN_COMP_CMD_CLEARHIST		0x0010	/* clear history */
-#define	HIFN_COMP_CMD_UPDATEHIST	0x0008	/* update history */
-#define	HIFN_COMP_CMD_LZS_STRIP0	0x0004	/* LZS: strip zero */
-#define	HIFN_COMP_CMD_MPPC_RESTART	0x0004	/* MPPC: restart */
-#define	HIFN_COMP_CMD_ALG_MASK		0x0001	/* compression mode: */
-#define	HIFN_COMP_CMD_ALG_MPPC		0x0001	/*   MPPC */
-#define	HIFN_COMP_CMD_ALG_LZS		0x0000	/*   LZS */
-
-struct hifn_base_result {
-	volatile u_int16_t flags;
-	volatile u_int16_t session;
-	volatile u_int16_t src_cnt;		/* 15:0 of source count */
-	volatile u_int16_t dst_cnt;		/* 15:0 of dest count */
-};
-
-#define	HIFN_BASE_RES_DSTOVERRUN	0x0200	/* destination overrun */
-#define	HIFN_BASE_RES_SRCLEN_M		0xc000	/* 17:16 of source count */
-#define	HIFN_BASE_RES_SRCLEN_S		14
-#define	HIFN_BASE_RES_DSTLEN_M		0x3000	/* 17:16 of dest count */
-#define	HIFN_BASE_RES_DSTLEN_S		12
-
-struct hifn_comp_result {
-	volatile u_int16_t flags;
-	volatile u_int16_t crc;
-};
-
-#define	HIFN_COMP_RES_LCB_M		0xff00	/* longitudinal check byte */
-#define	HIFN_COMP_RES_LCB_S		8
-#define	HIFN_COMP_RES_RESTART		0x0004	/* MPPC: restart */
-#define	HIFN_COMP_RES_ENDMARKER		0x0002	/* LZS: end marker seen */
-#define	HIFN_COMP_RES_SRC_NOTZERO	0x0001	/* source expired */
-
-struct hifn_mac_result {
-	volatile u_int16_t flags;
-	volatile u_int16_t reserved;
-	/* followed by 0, 6, 8, or 10 u_int16_t's of the MAC, then crypt */
-};
-
-#define	HIFN_MAC_RES_MISCOMPARE		0x0002	/* compare failed */
-#define	HIFN_MAC_RES_SRC_NOTZERO	0x0001	/* source expired */
-
-struct hifn_crypt_result {
-	volatile u_int16_t flags;
-	volatile u_int16_t reserved;
-};
-
-#define	HIFN_CRYPT_RES_SRC_NOTZERO	0x0001	/* source expired */
-
-/*
- * The poll frequency and poll scalar defines are unshifted values used
- * to set fields in the DMA Configuration Register.
- */
-#ifndef HIFN_POLL_FREQUENCY
-#define	HIFN_POLL_FREQUENCY	0x1
-#endif
-
-#ifndef HIFN_POLL_SCALAR
-#define	HIFN_POLL_SCALAR	0x0
-#endif
-
-#define	HIFN_MAX_SEGLEN 	0xffff		/* maximum dma segment len */
-#define	HIFN_MAX_DMALEN		0x3ffff		/* maximum dma length */
-#endif /* __HIFN_H__ */
diff --git a/sys/dev/crypto/hifn/hifn7751var.h b/sys/dev/crypto/hifn/hifn7751var.h
deleted file mode 100644
index fb7586bd03..0000000000
--- a/sys/dev/crypto/hifn/hifn7751var.h
+++ /dev/null
@@ -1,365 +0,0 @@
-/* $FreeBSD: src/sys/dev/hifn/hifn7751var.h,v 1.1.2.2 2003/06/04 17:56:59 sam Exp $ */
-/* $DragonFly: src/sys/dev/crypto/hifn/hifn7751var.h,v 1.3 2007/12/04 09:11:12 hasso Exp $ */
-/* $OpenBSD: hifn7751var.h,v 1.42 2002/04/08 17:49:42 jason Exp $ */
-
-/*-
- * Invertex AEON / Hifn 7751 driver
- * Copyright (c) 1999 Invertex Inc. All rights reserved.
- * Copyright (c) 1999 Theo de Raadt
- * Copyright (c) 2000-2001 Network Security Technologies, Inc.
- *			http://www.netsec.net
- *
- * Please send any comments, feedback, bug-fixes, or feature requests to
- * software@invertex.com.
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-
-#ifndef __HIFN7751VAR_H__
-#define __HIFN7751VAR_H__
-
-#ifdef _KERNEL
-
-/*
- * Some configurable values for the driver.  By default command+result
- * descriptor rings are the same size.  The src+dst descriptor rings
- * are sized at 3.5x the number of potential commands.  Slower parts
- * (e.g. 7951) tend to run out of src descriptors; faster parts (7811)
- * src+cmd/result descriptors.  It's not clear that increasing the size
- * of the descriptor rings helps performance significantly as other
- * factors tend to come into play (e.g. copying misaligned packets).
- */
-#define	HIFN_D_CMD_RSIZE	24	/* command descriptors */
-#define	HIFN_D_SRC_RSIZE	((HIFN_D_CMD_RSIZE * 7) / 2)	/* source descriptors */
-#define	HIFN_D_RES_RSIZE	HIFN_D_CMD_RSIZE	/* result descriptors */
-#define	HIFN_D_DST_RSIZE	HIFN_D_SRC_RSIZE	/* destination descriptors */
-
-/*
- *  Length values for cryptography
- */
-#define HIFN_DES_KEY_LENGTH		8
-#define HIFN_3DES_KEY_LENGTH		24
-#define HIFN_MAX_CRYPT_KEY_LENGTH	HIFN_3DES_KEY_LENGTH
-#define HIFN_IV_LENGTH			8
-#define HIFN_AES_IV_LENGTH		16
-#define HIFN_MAX_IV_LENGTH		HIFN_AES_IV_LENGTH
-
-/*
- *  Length values for authentication
- */
-#define HIFN_MAC_KEY_LENGTH		64
-#define HIFN_MD5_LENGTH			16
-#define HIFN_SHA1_LENGTH		20
-#define HIFN_MAC_TRUNC_LENGTH		12
-
-#define MAX_SCATTER 64
-
-/*
- * Data structure to hold all 4 rings and any other ring related data.
- */
-struct hifn_dma {
-	/*
-	 *  Descriptor rings.  We add +1 to the size to accomidate the
-	 *  jump descriptor.
-	 */
-	struct hifn_desc	cmdr[HIFN_D_CMD_RSIZE+1];
-	struct hifn_desc	srcr[HIFN_D_SRC_RSIZE+1];
-	struct hifn_desc	dstr[HIFN_D_DST_RSIZE+1];
-	struct hifn_desc	resr[HIFN_D_RES_RSIZE+1];
-
-	struct hifn_command	*hifn_commands[HIFN_D_RES_RSIZE];
-
-	u_char			command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
-	u_char			result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
-	u_int32_t		slop[HIFN_D_CMD_RSIZE];
-
-	u_int64_t		test_src, test_dst;
-
-	/*
-	 *  Our current positions for insertion and removal from the desriptor
-	 *  rings. 
-	 */
-	int			cmdi, srci, dsti, resi;
-	volatile int		cmdu, srcu, dstu, resu;
-	int			cmdk, srck, dstk, resk;
-};
-
-struct hifn_session {
-	int hs_used;
-	int hs_mlen;
-	u_int8_t hs_iv[HIFN_MAX_IV_LENGTH];
-};
-
-#define	HIFN_RING_SYNC(sc, r, i, f)					\
-	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f))
-
-#define	HIFN_CMDR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), cmdr, (i), (f))
-#define	HIFN_RESR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), resr, (i), (f))
-#define	HIFN_SRCR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), srcr, (i), (f))
-#define	HIFN_DSTR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), dstr, (i), (f))
-
-#define	HIFN_CMD_SYNC(sc, i, f)						\
-	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f))
-
-#define	HIFN_RES_SYNC(sc, i, f)						\
-	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f))
-
-/* We use a state machine to on sessions */
-#define	HS_STATE_FREE	0		/* unused session entry */
-#define	HS_STATE_USED	1		/* allocated, but key not on card */
-#define	HS_STATE_KEY	2		/* allocated and key is on card */
-
-/*
- * Holds data specific to a single HIFN board.
- */
-struct hifn_softc {
-	device_t		sc_dev;		/* device backpointer */
-	struct lock		sc_lock;	/* per-instance lock */
-	bus_dma_tag_t		sc_dmat;	/* parent DMA tag decriptor */
-	struct resource		*sc_bar0res;
-	bus_space_handle_t	sc_sh0;		/* bar0 bus space handle */
-	bus_space_tag_t		sc_st0;		/* bar0 bus space tag */
-	bus_size_t		sc_bar0_lastreg;/* bar0 last reg written */
-	struct resource		*sc_bar1res;
-	bus_space_handle_t	sc_sh1;		/* bar1 bus space handle */
-	bus_space_tag_t		sc_st1;		/* bar1 bus space tag */
-	bus_size_t		sc_bar1_lastreg;/* bar1 last reg written */
-	struct resource		*sc_irq;
-	void			*sc_intrhand;	/* interrupt handle */
-
-	u_int32_t		sc_dmaier;
-	u_int32_t		sc_drammodel;	/* 1=dram, 0=sram */
-	u_int32_t		sc_pllconfig;	/* 7954/7955/7956 PLL config */
-
-	struct hifn_dma		*sc_dma;
-	bus_dmamap_t		sc_dmamap;
-	bus_dma_segment_t 	sc_dmasegs[1];
-	bus_addr_t		sc_dma_physaddr;/* physical address of sc_dma */
-	int			sc_dmansegs;
-	int32_t			sc_cid;
-	int			sc_maxses;
-	int			sc_nsessions;
-	struct hifn_session	*sc_sessions;
-	int			sc_ramsize;
-	int			sc_flags;
-#define	HIFN_HAS_RNG		0x1	/* includes random number generator */
-#define	HIFN_HAS_PUBLIC		0x2	/* includes public key support */
-#define	HIFN_HAS_AES		0x4	/* includes AES support */
-#define	HIFN_IS_7811		0x8	/* Hifn 7811 part */
-#define	HIFN_IS_7956		0x10	/* Hifn 7956/7955 don't have SDRAM */
-	struct callout		sc_rngto;	/* for polling RNG */
-	struct callout		sc_tickto;	/* for managing DMA */
-	int			sc_rngfirst;
-	int			sc_rnghz;	/* RNG polling frequency */
-	struct rndtest_state	*sc_rndtest;	/* RNG test state */
-	void			(*sc_harvest)(struct rndtest_state *,
-					void *, u_int);
-	int			sc_c_busy;	/* command ring busy */
-	int			sc_s_busy;	/* source data ring busy */
-	int			sc_d_busy;	/* destination data ring busy */
-	int			sc_r_busy;	/* result ring busy */
-	int			sc_active;	/* for initial countdown */
-	int			sc_needwakeup;	/* ops q'd wating on resources */
-	int			sc_curbatch;	/* # ops submitted w/o int */
-	int			sc_suspended;
-};
-
-#define	HIFN_LOCK(_sc)		lockmgr(&(_sc)->sc_lock, LK_EXCLUSIVE)
-#define	HIFN_UNLOCK(_sc)	lockmgr(&(_sc)->sc_lock, LK_RELEASE)
-
-/*
- *  hifn_command_t
- *
- *  This is the control structure used to pass commands to hifn_encrypt().
- *
- *  flags
- *  -----
- *  Flags is the bitwise "or" values for command configuration.  A single
- *  encrypt direction needs to be set:
- *
- *	HIFN_ENCODE or HIFN_DECODE
- *
- *  To use cryptography, a single crypto algorithm must be included:
- *
- *	HIFN_CRYPT_3DES or HIFN_CRYPT_DES
- *
- *  To use authentication is used, a single MAC algorithm must be included:
- *
- *	HIFN_MAC_MD5 or HIFN_MAC_SHA1
- *
- *  By default MD5 uses a 16 byte hash and SHA-1 uses a 20 byte hash.
- *  If the value below is set, hash values are truncated or assumed
- *  truncated to 12 bytes:
- *
- *	HIFN_MAC_TRUNC
- *
- *  Keys for encryption and authentication can be sent as part of a command,
- *  or the last key value used with a particular session can be retrieved
- *  and used again if either of these flags are not specified.
- *
- *	HIFN_CRYPT_NEW_KEY, HIFN_MAC_NEW_KEY
- *
- *  session_num
- *  -----------
- *  A number between 0 and 2048 (for DRAM models) or a number between 
- *  0 and 768 (for SRAM models).  Those who don't want to use session
- *  numbers should leave value at zero and send a new crypt key and/or
- *  new MAC key on every command.  If you use session numbers and
- *  don't send a key with a command, the last key sent for that same
- *  session number will be used.
- *
- *  Warning:  Using session numbers and multiboard at the same time
- *            is currently broken.
- *
- *  mbuf
- *  ----
- *  Either fill in the mbuf pointer and npa=0 or
- *	 fill packp[] and packl[] and set npa to > 0
- * 
- *  mac_header_skip
- *  ---------------
- *  The number of bytes of the source_buf that are skipped over before
- *  authentication begins.  This must be a number between 0 and 2^16-1
- *  and can be used by IPsec implementers to skip over IP headers.
- *  *** Value ignored if authentication not used ***
- *
- *  crypt_header_skip
- *  -----------------
- *  The number of bytes of the source_buf that are skipped over before
- *  the cryptographic operation begins.  This must be a number between 0
- *  and 2^16-1.  For IPsec, this number will always be 8 bytes larger
- *  than the auth_header_skip (to skip over the ESP header).
- *  *** Value ignored if cryptography not used ***
- *
- */
-struct hifn_operand {
-	union {
-		struct mbuf *m;
-		struct uio *io;
-	} u;
-	bus_dmamap_t	map;
-	bus_size_t	mapsize;
-	int		nsegs;
-	bus_dma_segment_t segs[MAX_SCATTER];
-};
-struct hifn_command {
-	u_int16_t session_num;
-	u_int16_t base_masks, cry_masks, mac_masks;
-	u_int8_t iv[HIFN_MAX_IV_LENGTH], *ck, mac[HIFN_MAC_KEY_LENGTH];
-	int cklen;
-	int sloplen, slopidx;
-
-	struct hifn_operand src;
-	struct hifn_operand dst;
-
-	struct hifn_softc *softc;
-	struct cryptop *crp;
-	struct cryptodesc *enccrd, *maccrd;
-};
-
-#define	src_m		src.u.m
-#define	src_io		src.u.io
-#define	src_map		src.map
-#define	src_mapsize	src.mapsize
-#define	src_segs	src.segs
-#define	src_nsegs	src.nsegs
-
-#define	dst_m		dst.u.m
-#define	dst_io		dst.u.io
-#define	dst_map		dst.map
-#define	dst_mapsize	dst.mapsize
-#define	dst_segs	dst.segs
-#define	dst_nsegs	dst.nsegs
-
-/*
- *  Return values for hifn_crypto()
- */
-#define HIFN_CRYPTO_SUCCESS	0
-#define HIFN_CRYPTO_BAD_INPUT	(-1)
-#define HIFN_CRYPTO_RINGS_FULL	(-2)
-
-/**************************************************************************
- *
- *  Function:  hifn_crypto
- *
- *  Purpose:   Called by external drivers to begin an encryption on the
- *             HIFN board.
- *
- *  Blocking/Non-blocking Issues
- *  ============================
- *  The driver cannot block in hifn_crypto (no calls to tsleep) currently.
- *  hifn_crypto() returns HIFN_CRYPTO_RINGS_FULL if there is not enough
- *  room in any of the rings for the request to proceed.
- *
- *  Return Values
- *  =============
- *  0 for success, negative values on error
- *
- *  Defines for negative error codes are:
- *  
- *    HIFN_CRYPTO_BAD_INPUT  :  The passed in command had invalid settings.
- *    HIFN_CRYPTO_RINGS_FULL :  All DMA rings were full and non-blocking
- *                              behaviour was requested.
- *
- *************************************************************************/
-
-/*
- * Convert back and forth from 'sid' to 'card' and 'session'
- */
-#define HIFN_CARD(sid)		(((sid) & 0xf0000000) >> 28)
-#define HIFN_SESSION(sid)	((sid) & 0x000007ff)
-#define HIFN_SID(crd,ses)	(((crd) << 28) | ((ses) & 0x7ff))
-
-#endif /* _KERNEL */
-
-struct hifn_stats {
-	u_int64_t hst_ibytes;
-	u_int64_t hst_obytes;
-	u_int32_t hst_ipackets;
-	u_int32_t hst_opackets;
-	u_int32_t hst_invalid;
-	u_int32_t hst_nomem;		/* malloc or one of hst_nomem_* */
-	u_int32_t hst_abort;
-	u_int32_t hst_noirq;		/* IRQ for no reason */
-	u_int32_t hst_totbatch;		/* ops submitted w/o interrupt */
-	u_int32_t hst_maxbatch;		/* max ops submitted together */
-	u_int32_t hst_unaligned;	/* unaligned src caused copy */
-	/*
-	 * The following divides hst_nomem into more specific buckets.
-	 */
-	u_int32_t hst_nomem_map;	/* bus_dmamap_create failed */
-	u_int32_t hst_nomem_load;	/* bus_dmamap_load_* failed */
-	u_int32_t hst_nomem_mbuf;	/* MGET* failed */
-	u_int32_t hst_nomem_mcl;	/* MCLGET* failed */
-	u_int32_t hst_nomem_cr;		/* out of command/result descriptor */
-	u_int32_t hst_nomem_sd;		/* out of src/dst descriptors */
-};
-
-#endif /* __HIFN7751VAR_H__ */
diff --git a/sys/dev/crypto/padlock/Makefile b/sys/dev/crypto/padlock/Makefile
deleted file mode 100644
index 0d2cf39124..0000000000
--- a/sys/dev/crypto/padlock/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-# $FreeBSD: src/sys/modules/padlock/Makefile,v 1.3 2007/03/21 17:37:13 sam Exp $
-
-KMOD=	padlock
-SRCS=	padlock.c padlock_cipher.c padlock_hash.c padlock_rng.c \
-	rng_harvest_${MACHINE_ARCH}.S
-SRCS+=	device_if.h bus_if.h cryptodev_if.h
-
-.include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/padlock/padlock.c b/sys/dev/crypto/padlock/padlock.c
deleted file mode 100644
index b203cf2a34..0000000000
--- a/sys/dev/crypto/padlock/padlock.c
+++ /dev/null
@@ -1,427 +0,0 @@
-/*-
- * Copyright (c) 2005-2008 Pawel Jakub Dawidek <pjd@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.
- *
- * $FreeBSD: src/sys/crypto/via/padlock.c,v 1.23 2009/02/05 19:30:28 imp Exp $
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/kernel.h>
-#include <sys/module.h>
-#include <sys/lock.h>
-#include <sys/spinlock.h>
-#include <sys/spinlock2.h>
-#include <sys/malloc.h>
-#include <sys/libkern.h>
-#if defined(__x86_64__) || defined(__i386__)
-#include <machine/cpufunc.h>
-#include <machine/cputypes.h>
-#include <machine/md_var.h>
-#include <machine/specialreg.h>
-#endif
-
-#include <opencrypto/cryptodev.h>
-
-#include <dev/crypto/padlock/padlock.h>
-
-#include <sys/kobj.h>
-#include <sys/bus.h>
-#include "cryptodev_if.h"
-
-/*
- * Technical documentation about the PadLock engine can be found here:
- *
- * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/programming_guide.pdf
- */
-
-static int padlock_newsession(device_t, uint32_t *sidp, struct cryptoini *cri);
-static int padlock_freesession(device_t, uint64_t tid);
-static void padlock_freesession_one(struct padlock_softc *sc,
-    struct padlock_session *ses, int locked);
-static int padlock_process(device_t, struct cryptop *crp, int hint __unused);
-
-MALLOC_DEFINE(M_PADLOCK, "padlock_data", "PadLock Data");
-
-static void
-padlock_identify(driver_t *drv, device_t parent)
-{
-	/* NB: order 10 is so we get attached after h/w devices */
-	/* XXX: wouldn't bet about this BUS_ADD_CHILD correctness */
-	if (device_find_child(parent, "padlock", -1) == NULL &&
-	    BUS_ADD_CHILD(parent, parent, 10, "padlock", -1) == 0)
-		panic("padlock: could not attach");
-}
-
-static int
-padlock_probe(device_t dev)
-{
-	char capp[256];
-
-#if defined(__x86_64__) || defined(__i386__)
-	/* If there is no AES support, we has nothing to do here. */
-	if (!(via_feature_xcrypt & VIA_HAS_AES)) {
-		device_printf(dev, "No ACE support.\n");
-		return (EINVAL);
-	}
-	strlcpy(capp, "AES-CBC", sizeof(capp));
-#if 0
-	strlcat(capp, ",AES-EBC", sizeof(capp));
-	strlcat(capp, ",AES-CFB", sizeof(capp));
-	strlcat(capp, ",AES-OFB", sizeof(capp));
-#endif
-	if (via_feature_xcrypt & VIA_HAS_SHA) {
-		strlcat(capp, ",SHA1", sizeof(capp));
-		strlcat(capp, ",SHA256", sizeof(capp));
-	}
-#if 0
-	if (via_feature_xcrypt & VIA_HAS_AESCTR)
-		strlcat(capp, ",AES-CTR", sizeof(capp));
-	if (via_feature_xcrypt & VIA_HAS_MM)
-		strlcat(capp, ",RSA", sizeof(capp));
-#endif
-
-	if (via_feature_rng & VIA_HAS_RNG)
-		strlcat(capp, ",RNG", sizeof(capp));
-
-	device_set_desc_copy(dev, capp);
-	return (0);
-#else
-	return (EINVAL);
-#endif
-}
-
-static int
-padlock_attach(device_t dev)
-{
-	struct padlock_softc *sc = device_get_softc(dev);
-
-	TAILQ_INIT(&sc->sc_sessions);
-	sc->sc_sid = 1;
-
-	sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
-	if (sc->sc_cid < 0) {
-		device_printf(dev, "Could not get crypto driver id.\n");
-		return (ENOMEM);
-	}
-
-	spin_init(&sc->sc_sessions_lock, "padlock");
-	crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_RIPEMD160_HMAC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_SHA2_256_HMAC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_SHA2_384_HMAC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_SHA2_512_HMAC, 0, 0);
-
-	if (via_feature_rng & VIA_HAS_RNG)
-		padlock_rng_init(sc);
-
-	return (0);
-}
-
-static int
-padlock_detach(device_t dev)
-{
-	struct padlock_softc *sc = device_get_softc(dev);
-	struct padlock_session *ses;
-
-	spin_lock(&sc->sc_sessions_lock);
-	TAILQ_FOREACH(ses, &sc->sc_sessions, ses_next) {
-		if (ses->ses_used) {
-			spin_unlock(&sc->sc_sessions_lock);
-			device_printf(dev,
-			    "Cannot detach, sessions still active.\n");
-			return (EBUSY);
-		}
-	}
-	while ((ses = TAILQ_FIRST(&sc->sc_sessions)) != NULL) {
-		TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next);
-		kfree(ses->ses_freeaddr, M_PADLOCK);
-	}
-	spin_unlock(&sc->sc_sessions_lock);
-	spin_uninit(&sc->sc_sessions_lock);
-	crypto_unregister_all(sc->sc_cid);
-
-	if (via_feature_rng & VIA_HAS_RNG)
-		padlock_rng_uninit(sc);
-
-	return (0);
-}
-
-static int
-padlock_newsession(device_t dev, uint32_t *sidp, struct cryptoini *cri)
-{
-	struct padlock_softc *sc = device_get_softc(dev);
-	struct padlock_session *ases, *ses = NULL;
-	struct cryptoini *encini, *macini;
-	int error;
-
-	if (sidp == NULL || cri == NULL)
-		return (EINVAL);
-
-	encini = macini = NULL;
-	for (; cri != NULL; cri = cri->cri_next) {
-		switch (cri->cri_alg) {
-		case CRYPTO_NULL_HMAC:
-		case CRYPTO_MD5_HMAC:
-		case CRYPTO_SHA1_HMAC:
-		case CRYPTO_RIPEMD160_HMAC:
-		case CRYPTO_SHA2_256_HMAC:
-		case CRYPTO_SHA2_384_HMAC:
-		case CRYPTO_SHA2_512_HMAC:
-			if (macini != NULL)
-				return (EINVAL);
-			macini = cri;
-			break;
-		case CRYPTO_AES_CBC:
-			if (encini != NULL)
-				return (EINVAL);
-			encini = cri;
-			break;
-		default:
-			return (EINVAL);
-		}
-	}
-
-	/*
-	 * We only support HMAC algorithms to be able to work with
-	 * ipsec(4), so if we are asked only for authentication without
-	 * encryption, don't pretend we can accellerate it.
-	 */
-	if (encini == NULL)
-		return (EINVAL);
-
-	/*
-	 * Let's look for a free session structure.
-	 */
-	spin_lock(&sc->sc_sessions_lock);
-	/*
-	 * Free sessions goes first, so if first session is used, we need to
-	 * allocate one.
-	 */
-	ses = TAILQ_FIRST(&sc->sc_sessions);
-	if (ses == NULL || ses->ses_used) {
-		ses = kmalloc(sizeof(*ses) + 16, M_PADLOCK, M_NOWAIT | M_ZERO);
-		if (ses == NULL) {
-			spin_unlock(&sc->sc_sessions_lock);
-			return (ENOMEM);
-		}
-		/* Check if 'ses' is 16-byte aligned. If not, align it. */
-		if (((uintptr_t)ses & 0xf) != 0) {
-			ases = PADLOCK_ALIGN(ses);
-			ases->ses_freeaddr = ses;
-			ses = ases;
-		} else {
-			ses->ses_freeaddr = ses;
-		}
-		ses->ses_id = sc->sc_sid++;
-	} else {
-		TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next);
-	}
-	ses->ses_used = 1;
-	TAILQ_INSERT_TAIL(&sc->sc_sessions, ses, ses_next);
-	spin_unlock(&sc->sc_sessions_lock);
-
-	error = padlock_cipher_setup(ses, encini);
-	if (error != 0) {
-		padlock_freesession_one(sc, ses, 0);
-		return (error);
-	}
-
-	if (macini != NULL) {
-		error = padlock_hash_setup(ses, macini);
-		if (error != 0) {
-			padlock_freesession_one(sc, ses, 0);
-			return (error);
-		}
-	}
-
-	*sidp = ses->ses_id;
-	return (0);
-}
-
-static void
-padlock_freesession_one(struct padlock_softc *sc, struct padlock_session *ses,
-    int locked)
-{
-	uint32_t sid = ses->ses_id;
-	void *freeaddr = ses->ses_freeaddr;
-
-	if (!locked)
-		spin_lock(&sc->sc_sessions_lock);
-	TAILQ_REMOVE(&sc->sc_sessions, ses, ses_next);
-	padlock_hash_free(ses);
-	bzero(ses, sizeof(*ses));
-	ses->ses_used = 0;
-	ses->ses_id = sid;
-	ses->ses_freeaddr = freeaddr;
-	TAILQ_INSERT_HEAD(&sc->sc_sessions, ses, ses_next);
-	if (!locked)
-		spin_unlock(&sc->sc_sessions_lock);
-}
-
-static int
-padlock_freesession(device_t dev, uint64_t tid)
-{
-	struct padlock_softc *sc = device_get_softc(dev);
-	struct padlock_session *ses;
-	uint32_t sid = ((uint32_t)tid) & 0xffffffff;
-
-	spin_lock(&sc->sc_sessions_lock);
-	TAILQ_FOREACH_REVERSE(ses, &sc->sc_sessions, padlock_sessions_head,
-	    ses_next) {
-		if (ses->ses_id == sid)
-			break;
-	}
-	if (ses == NULL) {
-		spin_unlock(&sc->sc_sessions_lock);
-		return (EINVAL);
-	}
-	padlock_freesession_one(sc, ses, 1);
-	spin_unlock(&sc->sc_sessions_lock);
-	return (0);
-}
-
-static int
-padlock_process(device_t dev, struct cryptop *crp, int hint __unused)
-{
-	struct padlock_softc *sc = device_get_softc(dev);
-	struct padlock_session *ses = NULL;
-	struct cryptodesc *crd, *enccrd, *maccrd;
-	int error = 0;
-
-	enccrd = maccrd = NULL;
-
-	/* Sanity check. */
-	if (crp == NULL)
-		return (EINVAL);
-
-	if (crp->crp_callback == NULL || crp->crp_desc == NULL) {
-		error = EINVAL;
-		goto out;
-	}
-
-	for (crd = crp->crp_desc; crd != NULL; crd = crd->crd_next) {
-		switch (crd->crd_alg) {
-		case CRYPTO_NULL_HMAC:
-		case CRYPTO_MD5_HMAC:
-		case CRYPTO_SHA1_HMAC:
-		case CRYPTO_RIPEMD160_HMAC:
-		case CRYPTO_SHA2_256_HMAC:
-		case CRYPTO_SHA2_384_HMAC:
-		case CRYPTO_SHA2_512_HMAC:
-			if (maccrd != NULL) {
-				error = EINVAL;
-				goto out;
-			}
-			maccrd = crd;
-			break;
-		case CRYPTO_AES_CBC:
-			if (enccrd != NULL) {
-				error = EINVAL;
-				goto out;
-			}
-			enccrd = crd;
-			break;
-		default:
-			return (EINVAL);
-		}
-	}
-	if (enccrd == NULL || (enccrd->crd_len % AES_BLOCK_LEN) != 0) {
-		error = EINVAL;
-		goto out;
-	}
-
-	spin_lock(&sc->sc_sessions_lock); /* XXX: was rd lock */
-	TAILQ_FOREACH_REVERSE(ses, &sc->sc_sessions, padlock_sessions_head,
-	    ses_next) {
-		if (ses->ses_id == (crp->crp_sid & 0xffffffff))
-			break;
-	}
-	spin_unlock(&sc->sc_sessions_lock); /* XXX: was rd lock */
-	if (ses == NULL) {
-		error = EINVAL;
-		goto out;
-	}
-
-	/* Perform data authentication if requested before encryption. */
-	if (maccrd != NULL && maccrd->crd_next == enccrd) {
-		error = padlock_hash_process(ses, maccrd, crp);
-		if (error != 0)
-			goto out;
-	}
-
-	error = padlock_cipher_process(ses, enccrd, crp);
-	if (error != 0)
-		goto out;
-
-	/* Perform data authentication if requested after encryption. */
-	if (maccrd != NULL && enccrd->crd_next == maccrd) {
-		error = padlock_hash_process(ses, maccrd, crp);
-		if (error != 0)
-			goto out;
-	}
-
-out:
-#if 0
-	/*
-	 * This code is not necessary, because contexts will be freed on next
-	 * padlock_setup_mackey() call or at padlock_freesession() call.
-	 */
-	if (ses != NULL && maccrd != NULL &&
-	    (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
-		padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
-		padlock_free_ctx(ses->ses_axf, ses->ses_octx);
-	}
-#endif
-	crp->crp_etype = error;
-	crypto_done(crp);
-	return (error);
-}
-
-static device_method_t padlock_methods[] = {
-	DEVMETHOD(device_identify,	padlock_identify),
-	DEVMETHOD(device_probe,		padlock_probe),
-	DEVMETHOD(device_attach,	padlock_attach),
-	DEVMETHOD(device_detach,	padlock_detach),
-
-	DEVMETHOD(cryptodev_newsession,	padlock_newsession),
-	DEVMETHOD(cryptodev_freesession,padlock_freesession),
-	DEVMETHOD(cryptodev_process,	padlock_process),
-
-	DEVMETHOD_END
-};
-
-static driver_t padlock_driver = {
-	"padlock",
-	padlock_methods,
-	sizeof(struct padlock_softc),
-};
-static devclass_t padlock_devclass;
-
-/* XXX where to attach */
-DRIVER_MODULE(padlock, nexus, padlock_driver, padlock_devclass, NULL, NULL);
-MODULE_VERSION(padlock, 1);
-MODULE_DEPEND(padlock, crypto, 1, 1, 1);
diff --git a/sys/dev/crypto/padlock/padlock.h b/sys/dev/crypto/padlock/padlock.h
deleted file mode 100644
index f8cd27a014..0000000000
--- a/sys/dev/crypto/padlock/padlock.h
+++ /dev/null
@@ -1,101 +0,0 @@
-/*-
- * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@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.
- *
- * $FreeBSD: src/sys/crypto/via/padlock.h,v 1.4 2006/07/28 14:46:19 pjd Exp $
- */
-
-#ifndef _PADLOCK_H_
-#define _PADLOCK_H_
-
-#include <sys/spinlock.h>
-#include <opencrypto/cryptodev.h>
-#include <crypto/rijndael/rijndael.h>
-
-
-union padlock_cw {
-	uint64_t raw;
-	struct {
-		u_int round_count : 4;
-		u_int algorithm_type : 3;
-		u_int key_generation : 1;
-		u_int intermediate : 1;
-		u_int direction : 1;
-		u_int key_size : 2;
-		u_int filler0 : 20;
-		u_int filler1 : 32;
-		u_int filler2 : 32;
-		u_int filler3 : 32;
-	} __field;
-};
-#define	cw_round_count		__field.round_count
-#define	cw_algorithm_type	__field.algorithm_type
-#define	cw_key_generation	__field.key_generation
-#define	cw_intermediate		__field.intermediate
-#define	cw_direction		__field.direction
-#define	cw_key_size		__field.key_size
-#define	cw_filler0		__field.filler0
-#define	cw_filler1		__field.filler1
-#define	cw_filler2		__field.filler2
-#define	cw_filler3		__field.filler3
-
-struct padlock_session {
-	union padlock_cw ses_cw __aligned(16);
-	uint32_t	ses_ekey[4 * (RIJNDAEL_MAXNR + 1) + 4] __aligned(16);	/* 128 bit aligned */
-	uint32_t	ses_dkey[4 * (RIJNDAEL_MAXNR + 1) + 4] __aligned(16);	/* 128 bit aligned */
-	uint8_t		ses_iv[16] __aligned(16);			/* 128 bit aligned */
-	struct auth_hash *ses_axf;
-	uint8_t		*ses_ictx;
-	uint8_t		*ses_octx;
-	int		ses_mlen;
-	int		ses_used;
-	uint32_t	ses_id;
-	TAILQ_ENTRY(padlock_session) ses_next;
-	void		*ses_freeaddr;
-} __aligned(16);
-
-struct padlock_softc {
-	int32_t		sc_cid;
-	uint32_t	sc_sid;
-	int32_t		sc_rng_ticks;
-	struct callout	sc_rng_co;
-	TAILQ_HEAD(padlock_sessions_head, padlock_session) sc_sessions;
-	struct spinlock	sc_sessions_lock;
-};
-
-#define	PADLOCK_ALIGN(p)	(void *)(roundup2((uintptr_t)(p), 16))
-
-int	padlock_cipher_setup(struct padlock_session *ses,
-	    struct cryptoini *encini);
-int	padlock_cipher_process(struct padlock_session *ses,
-	    struct cryptodesc *enccrd, struct cryptop *crp);
-int	padlock_hash_setup(struct padlock_session *ses,
-	    struct cryptoini *macini);
-int	padlock_hash_process(struct padlock_session *ses,
-	    struct cryptodesc *maccrd, struct cryptop *crp);
-void	padlock_hash_free(struct padlock_session *ses);
-void	padlock_rng_init(struct padlock_softc *sc);
-void	padlock_rng_uninit(struct padlock_softc *sc);
-
-#endif	/* !_PADLOCK_H_ */
diff --git a/sys/dev/crypto/padlock/padlock_cipher.c b/sys/dev/crypto/padlock/padlock_cipher.c
deleted file mode 100644
index 56d5b8ab7a..0000000000
--- a/sys/dev/crypto/padlock/padlock_cipher.c
+++ /dev/null
@@ -1,266 +0,0 @@
-/*-
- * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
- * Copyright (c) 2004 Mark R V Murray
- * 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.
- */
-
-/*	$OpenBSD: via.c,v 1.3 2004/06/15 23:36:55 deraadt Exp $	*/
-/*	$FreeBSD: src/sys/crypto/via/padlock_cipher.c,v 1.5 2006/09/15 10:44:55 pjd Exp $ */
-/*-
- * Copyright (c) 2003 Jason Wright
- * Copyright (c) 2003, 2004 Theo de Raadt
- * All rights reserved.
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/kernel.h>
-#include <sys/module.h>
-#include <sys/malloc.h>
-#include <sys/libkern.h>
-#include <sys/uio.h>
-
-#include <opencrypto/cryptodev.h>
-#include <crypto/rijndael/rijndael.h>
-
-#include <dev/crypto/padlock/padlock.h>
-
-#define	PADLOCK_ROUND_COUNT_AES128	10
-#define	PADLOCK_ROUND_COUNT_AES192	12
-#define	PADLOCK_ROUND_COUNT_AES256	14
-
-#define	PADLOCK_ALGORITHM_TYPE_AES	0
-
-#define	PADLOCK_KEY_GENERATION_HW	0
-#define	PADLOCK_KEY_GENERATION_SW	1
-
-#define	PADLOCK_DIRECTION_ENCRYPT	0
-#define	PADLOCK_DIRECTION_DECRYPT	1
-
-#define	PADLOCK_KEY_SIZE_128	0
-#define	PADLOCK_KEY_SIZE_192	1
-#define	PADLOCK_KEY_SIZE_256	2
-
-MALLOC_DECLARE(M_PADLOCK);
-
-static __inline void
-padlock_cbc(void *in, void *out, size_t count, void *key, union padlock_cw *cw,
-    void *iv)
-{
-	/* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
-	__asm __volatile(
-		"pushf				\n\t"
-		"popf				\n\t"
-		"rep				\n\t"
-		".byte	0x0f, 0xa7, 0xd0"
-			: "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
-			: "b" (key), "d" (cw)
-			: "cc", "memory"
-		);
-}
-
-static void
-padlock_cipher_key_setup(struct padlock_session *ses, caddr_t key, int klen)
-{
-	union padlock_cw *cw;
-	int i;
-
-	cw = &ses->ses_cw;
-	if (cw->cw_key_generation == PADLOCK_KEY_GENERATION_SW) {
-		/* Build expanded keys for both directions */
-		rijndaelKeySetupEnc(ses->ses_ekey, key, klen);
-		rijndaelKeySetupDec(ses->ses_dkey, key, klen);
-		for (i = 0; i < 4 * (RIJNDAEL_MAXNR + 1); i++) {
-			ses->ses_ekey[i] = ntohl(ses->ses_ekey[i]);
-			ses->ses_dkey[i] = ntohl(ses->ses_dkey[i]);
-		}
-	} else {
-		bcopy(key, ses->ses_ekey, klen);
-		bcopy(key, ses->ses_dkey, klen);
-	}
-}
-
-int
-padlock_cipher_setup(struct padlock_session *ses, struct cryptoini *encini)
-{
-	union padlock_cw *cw;
-
-	if (encini->cri_klen != 128 && encini->cri_klen != 192 &&
-	    encini->cri_klen != 256) {
-		return (EINVAL);
-	}
-
-	cw = &ses->ses_cw;
-	bzero(cw, sizeof(*cw));
-	cw->cw_algorithm_type = PADLOCK_ALGORITHM_TYPE_AES;
-	cw->cw_key_generation = PADLOCK_KEY_GENERATION_SW;
-	cw->cw_intermediate = 0;
-	switch (encini->cri_klen) {
-	case 128:
-		cw->cw_round_count = PADLOCK_ROUND_COUNT_AES128;
-		cw->cw_key_size = PADLOCK_KEY_SIZE_128;
-#ifdef HW_KEY_GENERATION
-		/* This doesn't buy us much, that's why it is commented out. */
-		cw->cw_key_generation = PADLOCK_KEY_GENERATION_HW;
-#endif
-		break;
-	case 192:
-		cw->cw_round_count = PADLOCK_ROUND_COUNT_AES192;
-		cw->cw_key_size = PADLOCK_KEY_SIZE_192;
-		break;
-	case 256:
-		cw->cw_round_count = PADLOCK_ROUND_COUNT_AES256;
-		cw->cw_key_size = PADLOCK_KEY_SIZE_256;
-		break;
-	}
-	if (encini->cri_key != NULL) {
-		padlock_cipher_key_setup(ses, encini->cri_key,
-		    encini->cri_klen);
-	}
-
-	karc4random_buf(ses->ses_iv, sizeof(ses->ses_iv));
-	return (0);
-}
-
-/*
- * Function checks if the given buffer is already 16 bytes aligned.
- * If it is there is no need to allocate new buffer.
- * If it isn't, new buffer is allocated.
- */
-static u_char *
-padlock_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
-    int *allocated)
-{
-	u_char *addr;
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF)
-		goto alloc;
-	else {
-		if (crp->crp_flags & CRYPTO_F_IOV) {
-			struct uio *uio;
-			struct iovec *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;
-		}
-		if (((uintptr_t)addr & 0xf) != 0) /* 16 bytes aligned? */
-			goto alloc;
-		*allocated = 0;
-		return (addr);
-	}
-alloc:
-	*allocated = 1;
-	addr = kmalloc(enccrd->crd_len + 16, M_PADLOCK, M_NOWAIT);
-	return (addr);
-}
-
-int
-padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd,
-    struct cryptop *crp)
-{
-	union padlock_cw *cw;
-	u_char *buf, *abuf;
-	uint32_t *key;
-	int allocated;
-
-	buf = padlock_cipher_alloc(enccrd, crp, &allocated);
-	if (buf == NULL)
-		return (ENOMEM);
-	/* Buffer has to be 16 bytes aligned. */
-	abuf = PADLOCK_ALIGN(buf);
-
-	if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
-		padlock_cipher_key_setup(ses, enccrd->crd_key,
-		    enccrd->crd_klen);
-	}
-
-	cw = &ses->ses_cw;
-	cw->cw_filler0 = 0;
-	cw->cw_filler1 = 0;
-	cw->cw_filler2 = 0;
-	cw->cw_filler3 = 0;
-	if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
-		cw->cw_direction = PADLOCK_DIRECTION_ENCRYPT;
-		key = ses->ses_ekey;
-		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
-			bcopy(enccrd->crd_iv, ses->ses_iv, AES_BLOCK_LEN);
-
-		if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
-			crypto_copyback(crp->crp_flags, crp->crp_buf,
-			    enccrd->crd_inject, AES_BLOCK_LEN, ses->ses_iv);
-		}
-	} else {
-		cw->cw_direction = PADLOCK_DIRECTION_DECRYPT;
-		key = ses->ses_dkey;
-		if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
-			bcopy(enccrd->crd_iv, ses->ses_iv, AES_BLOCK_LEN);
-		else {
-			crypto_copydata(crp->crp_flags, crp->crp_buf,
-			    enccrd->crd_inject, AES_BLOCK_LEN, ses->ses_iv);
-		}
-	}
-
-	if (allocated) {
-		crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
-		    enccrd->crd_len, abuf);
-	}
-
-	padlock_cbc(abuf, abuf, enccrd->crd_len / AES_BLOCK_LEN, key, cw,
-	    ses->ses_iv);
-
-	if (allocated) {
-		crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
-		    enccrd->crd_len, abuf);
-	}
-
-	/* copy out last block for use as next session IV */
-	if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
-		crypto_copydata(crp->crp_flags, crp->crp_buf,
-		    enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN,
-		    AES_BLOCK_LEN, ses->ses_iv);
-	}
-
-	if (allocated) {
-		bzero(buf, enccrd->crd_len + 16);
-		kfree(buf, M_PADLOCK);
-	}
-	return (0);
-}
diff --git a/sys/dev/crypto/padlock/padlock_hash.c b/sys/dev/crypto/padlock/padlock_hash.c
deleted file mode 100644
index 0b1fd807de..0000000000
--- a/sys/dev/crypto/padlock/padlock_hash.c
+++ /dev/null
@@ -1,388 +0,0 @@
-/*-
- * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@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.
- *
- * $FreeBSD: src/sys/crypto/via/padlock_hash.c,v 1.4 2009/05/27 09:52:12 vanhu Exp $
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/kernel.h>
-#include <sys/module.h>
-#include <sys/malloc.h>
-#include <sys/libkern.h>
-#include <sys/endian.h>
-#if defined(__x86_64__) || defined(__i386__)
-#include <machine/cpufunc.h>
-#include <machine/cputypes.h>
-#include <machine/md_var.h>
-#include <machine/specialreg.h>
-#endif
-
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/cryptosoft.h> /* for hmac_ipad_buffer and hmac_opad_buffer */
-#include <opencrypto/xform.h>
-
-#include <dev/crypto/padlock/padlock.h>
-
-/*
- * Implementation notes.
- *
- * Some VIA CPUs provides SHA1 and SHA256 acceleration.
- * We implement all HMAC algorithms provided by crypto(9) framework, but we do
- * the crypto work in software unless this is HMAC/SHA1 or HMAC/SHA256 and
- * our CPU can accelerate it.
- *
- * Additional CPU instructions, which preform SHA1 and SHA256 are one-shot
- * functions - we have only one chance to give the data, CPU itself will add
- * the padding and calculate hash automatically.
- * This means, it is not possible to implement common init(), update(), final()
- * methods.
- * The way I've choosen is to keep adding data to the buffer on update()
- * (reallocating the buffer if necessary) and call XSHA{1,256} instruction on
- * final().
- */
-
-struct padlock_sha_ctx {
-	uint8_t	*psc_buf;
-	int	 psc_offset;
-	int	 psc_size;
-};
-CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx));
-
-static void padlock_sha_init(struct padlock_sha_ctx *ctx);
-static int padlock_sha_update(struct padlock_sha_ctx *ctx, uint8_t *buf,
-    uint16_t bufsize);
-static void padlock_sha1_final(uint8_t *hash, struct padlock_sha_ctx *ctx);
-static void padlock_sha256_final(uint8_t *hash, struct padlock_sha_ctx *ctx);
-
-static struct auth_hash padlock_hmac_sha1 = {
-	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
-	20, SHA1_HASH_LEN, SHA1_HMAC_BLOCK_LEN, sizeof(struct padlock_sha_ctx),
-        (void (*)(void *))padlock_sha_init,
-	NULL, NULL,
-	(int (*)(void *, uint8_t *, uint16_t))padlock_sha_update,
-	(void (*)(uint8_t *, void *))padlock_sha1_final
-};
-
-static struct auth_hash padlock_hmac_sha256 = {
-	CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
-	32, SHA2_256_HASH_LEN, SHA2_256_HMAC_BLOCK_LEN, sizeof(struct padlock_sha_ctx),
-        (void (*)(void *))padlock_sha_init,
-	NULL, NULL,
-	(int (*)(void *, uint8_t *, uint16_t))padlock_sha_update,
-	(void (*)(uint8_t *, void *))padlock_sha256_final
-};
-
-MALLOC_DECLARE(M_PADLOCK);
-
-static __inline void
-padlock_output_block(uint32_t *src, uint32_t *dst, size_t count)
-{
-
-	while (count-- > 0)
-		*dst++ = bswap32(*src++);
-}
-
-static void
-padlock_do_sha1(const u_char *in, u_char *out, int count)
-{
-	u_char buf[128+16];	/* PadLock needs at least 128 bytes buffer. */
-	u_char *result = PADLOCK_ALIGN(buf);
-
-	((uint32_t *)result)[0] = 0x67452301;
-	((uint32_t *)result)[1] = 0xEFCDAB89;
-	((uint32_t *)result)[2] = 0x98BADCFE;
-	((uint32_t *)result)[3] = 0x10325476;
-	((uint32_t *)result)[4] = 0xC3D2E1F0;
-
-	__asm __volatile(
-		".byte  0xf3, 0x0f, 0xa6, 0xc8" /* rep xsha1 */
-			: "+S"(in), "+D"(result)
-			: "c"(count), "a"(0)
-		);
-
-	padlock_output_block((uint32_t *)result, (uint32_t *)out,
-	    SHA1_HASH_LEN / sizeof(uint32_t));
-}
-
-static void
-padlock_do_sha256(const char *in, char *out, int count)
-{
-	char buf[128+16];	/* PadLock needs at least 128 bytes buffer. */
-	char *result = PADLOCK_ALIGN(buf);
-
-	((uint32_t *)result)[0] = 0x6A09E667;
-	((uint32_t *)result)[1] = 0xBB67AE85;
-	((uint32_t *)result)[2] = 0x3C6EF372;
-	((uint32_t *)result)[3] = 0xA54FF53A;
-	((uint32_t *)result)[4] = 0x510E527F;
-	((uint32_t *)result)[5] = 0x9B05688C;
-	((uint32_t *)result)[6] = 0x1F83D9AB;
-	((uint32_t *)result)[7] = 0x5BE0CD19;
-
-	__asm __volatile(
-		".byte  0xf3, 0x0f, 0xa6, 0xd0" /* rep xsha256 */
-			: "+S"(in), "+D"(result)
-			: "c"(count), "a"(0)
-		);
-
-	padlock_output_block((uint32_t *)result, (uint32_t *)out,
-	    SHA2_256_HASH_LEN / sizeof(uint32_t));
-}
-
-static void
-padlock_sha_init(struct padlock_sha_ctx *ctx)
-{
-
-	ctx->psc_buf = NULL;
-	ctx->psc_offset = 0;
-	ctx->psc_size = 0;
-}
-
-static int
-padlock_sha_update(struct padlock_sha_ctx *ctx, uint8_t *buf, uint16_t bufsize)
-{
-
-	if (ctx->psc_size - ctx->psc_offset < bufsize) {
-		ctx->psc_size = MAX(ctx->psc_size * 2, ctx->psc_size + bufsize);
-		ctx->psc_buf = krealloc(ctx->psc_buf, ctx->psc_size, M_PADLOCK,
-		    M_NOWAIT);
-		if(ctx->psc_buf == NULL)
-			return (ENOMEM);
-	}
-	bcopy(buf, ctx->psc_buf + ctx->psc_offset, bufsize);
-	ctx->psc_offset += bufsize;
-	return (0);
-}
-
-static void
-padlock_sha_free(struct padlock_sha_ctx *ctx)
-{
-
-	if (ctx->psc_buf != NULL) {
-		//bzero(ctx->psc_buf, ctx->psc_size);
-		kfree(ctx->psc_buf, M_PADLOCK);
-		ctx->psc_buf = NULL;
-		ctx->psc_offset = 0;
-		ctx->psc_size = 0;
-	}
-}
-
-static void
-padlock_sha1_final(uint8_t *hash, struct padlock_sha_ctx *ctx)
-{
-
-	padlock_do_sha1(ctx->psc_buf, hash, ctx->psc_offset);
-	padlock_sha_free(ctx);
-}
-
-static void
-padlock_sha256_final(uint8_t *hash, struct padlock_sha_ctx *ctx)
-{
-
-	padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset);
-	padlock_sha_free(ctx);
-}
-
-static void
-padlock_copy_ctx(struct auth_hash *axf, void *sctx, void *dctx)
-{
-
-	if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 &&
-	    (axf->type == CRYPTO_SHA1_HMAC ||
-	     axf->type == CRYPTO_SHA2_256_HMAC)) {
-		struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx;
-
-		dpctx->psc_offset = spctx->psc_offset;
-		dpctx->psc_size = spctx->psc_size;
-		dpctx->psc_buf = kmalloc(dpctx->psc_size, M_PADLOCK, M_WAITOK);
-		bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size);
-	} else {
-		bcopy(sctx, dctx, axf->ctxsize);
-	}
-}
-
-static void
-padlock_free_ctx(struct auth_hash *axf, void *ctx)
-{
-
-	if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 &&
-	    (axf->type == CRYPTO_SHA1_HMAC ||
-	     axf->type == CRYPTO_SHA2_256_HMAC)) {
-		padlock_sha_free(ctx);
-	}
-}
-
-static void
-padlock_hash_key_setup(struct padlock_session *ses, caddr_t key, int klen)
-{
-	struct auth_hash *axf;
-	int i;
-
-	klen /= 8;
-	axf = ses->ses_axf;
-
-	/*
-	 * Try to free contexts before using them, because
-	 * padlock_hash_key_setup() can be called twice - once from
-	 * padlock_newsession() and again from padlock_process().
-	 */
-	padlock_free_ctx(axf, ses->ses_ictx);
-	padlock_free_ctx(axf, ses->ses_octx);
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= HMAC_IPAD_VAL;
-
-	axf->Init(ses->ses_ictx);
-	axf->Update(ses->ses_ictx, key, klen);
-	axf->Update(ses->ses_ictx, hmac_ipad_buffer, axf->blocksize - klen);
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
-
-	axf->Init(ses->ses_octx);
-	axf->Update(ses->ses_octx, key, klen);
-	axf->Update(ses->ses_octx, hmac_opad_buffer, axf->blocksize - klen);
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= HMAC_OPAD_VAL;
-}
-
-/*
- * Compute keyed-hash authenticator.
- */
-static int
-padlock_authcompute(struct padlock_session *ses, struct cryptodesc *crd,
-    caddr_t buf, int flags)
-{
-	u_char hash[HASH_MAX_LEN];
-	struct auth_hash *axf;
-	union authctx ctx;
-	int error;
-
-	axf = ses->ses_axf;
-
-	padlock_copy_ctx(axf, ses->ses_ictx, &ctx);
-	error = crypto_apply(flags, buf, crd->crd_skip, crd->crd_len,
-	    (int (*)(void *, void *, unsigned int))axf->Update, (caddr_t)&ctx);
-	if (error != 0) {
-		padlock_free_ctx(axf, &ctx);
-		return (error);
-	}
-	axf->Final(hash, &ctx);
-
-	padlock_copy_ctx(axf, ses->ses_octx, &ctx);
-	axf->Update(&ctx, hash, axf->hashsize);
-	axf->Final(hash, &ctx);
-
-	/* Inject the authentication data */
-	crypto_copyback(flags, buf, crd->crd_inject,
-	    ses->ses_mlen == 0 ? axf->hashsize : ses->ses_mlen, hash);
-	return (0);
-}
-
-int
-padlock_hash_setup(struct padlock_session *ses, struct cryptoini *macini)
-{
-
-	ses->ses_mlen = macini->cri_mlen;
-
-	/* Find software structure which describes HMAC algorithm. */
-	switch (macini->cri_alg) {
-	case CRYPTO_NULL_HMAC:
-		ses->ses_axf = &auth_hash_null;
-		break;
-	case CRYPTO_MD5_HMAC:
-		ses->ses_axf = &auth_hash_hmac_md5;
-		break;
-	case CRYPTO_SHA1_HMAC:
-		if ((via_feature_xcrypt & VIA_HAS_SHA) != 0)
-			ses->ses_axf = &padlock_hmac_sha1;
-		else
-			ses->ses_axf = &auth_hash_hmac_sha1;
-		break;
-	case CRYPTO_RIPEMD160_HMAC:
-		ses->ses_axf = &auth_hash_hmac_ripemd_160;
-		break;
-	case CRYPTO_SHA2_256_HMAC:
-		if ((via_feature_xcrypt & VIA_HAS_SHA) != 0)
-			ses->ses_axf = &padlock_hmac_sha256;
-		else
-			ses->ses_axf = &auth_hash_hmac_sha2_256;
-		break;
-	case CRYPTO_SHA2_384_HMAC:
-		ses->ses_axf = &auth_hash_hmac_sha2_384;
-		break;
-	case CRYPTO_SHA2_512_HMAC:
-		ses->ses_axf = &auth_hash_hmac_sha2_512;
-		break;
-	}
-
-	/* Allocate memory for HMAC inner and outer contexts. */
-	ses->ses_ictx = kmalloc(ses->ses_axf->ctxsize, M_PADLOCK,
-	    M_ZERO | M_NOWAIT);
-	ses->ses_octx = kmalloc(ses->ses_axf->ctxsize, M_PADLOCK,
-	    M_ZERO | M_NOWAIT);
-	if (ses->ses_ictx == NULL || ses->ses_octx == NULL)
-		return (ENOMEM);
-
-	/* Setup key if given. */
-	if (macini->cri_key != NULL) {
-		padlock_hash_key_setup(ses, macini->cri_key,
-		    macini->cri_klen);
-	}
-	return (0);
-}
-
-int
-padlock_hash_process(struct padlock_session *ses, struct cryptodesc *maccrd,
-    struct cryptop *crp)
-{
-	int error;
-
-	if ((maccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0)
-		padlock_hash_key_setup(ses, maccrd->crd_key, maccrd->crd_klen);
-
-	error = padlock_authcompute(ses, maccrd, crp->crp_buf, crp->crp_flags);
-	return (error);
-}
-
-void
-padlock_hash_free(struct padlock_session *ses)
-{
-
-	if (ses->ses_ictx != NULL) {
-		padlock_free_ctx(ses->ses_axf, ses->ses_ictx);
-		bzero(ses->ses_ictx, ses->ses_axf->ctxsize);
-		kfree(ses->ses_ictx, M_PADLOCK);
-		ses->ses_ictx = NULL;
-	}
-	if (ses->ses_octx != NULL) {
-		padlock_free_ctx(ses->ses_axf, ses->ses_octx);
-		bzero(ses->ses_octx, ses->ses_axf->ctxsize);
-		kfree(ses->ses_octx, M_PADLOCK);
-		ses->ses_octx = NULL;
-	}
-}
diff --git a/sys/dev/crypto/padlock/padlock_rng.c b/sys/dev/crypto/padlock/padlock_rng.c
deleted file mode 100644
index f1e00b4a71..0000000000
--- a/sys/dev/crypto/padlock/padlock_rng.c
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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/param.h>
-#include <sys/systm.h>
-#include <sys/kernel.h>
-#include <sys/module.h>
-#include <sys/malloc.h>
-#include <sys/libkern.h>
-#include <sys/random.h>
-
-#include <dev/crypto/padlock/padlock.h>
-
-int padlock_rng(uint8_t *out, long limit);
-
-#define PADLOCK_RNDBYTES	32
-
-static void
-padlock_rng_harvest(void *arg)
-{
-	struct padlock_softc *sc = arg;
-	uint8_t randomness[PADLOCK_RNDBYTES + 32];
-	uint8_t *arandomness; /* randomness aligned */
-	int cnt;
-
-	arandomness = PADLOCK_ALIGN(randomness);
-	cnt = padlock_rng(arandomness, PADLOCK_RNDBYTES);
-
-	add_buffer_randomness_src(arandomness, cnt, RAND_SRC_PADLOCK);
-
-	callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks,
-	    padlock_rng_harvest, sc);
-}
-
-void
-padlock_rng_init(struct padlock_softc *sc)
-{
-	if (hz > 100)
-		sc->sc_rng_ticks = hz/100;
-	else
-		sc->sc_rng_ticks = 1;
-
-	callout_init_mp(&sc->sc_rng_co);
-	callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks,
-	    padlock_rng_harvest, sc);
-}
-
-void
-padlock_rng_uninit(struct padlock_softc *sc)
-{
-	callout_stop(&sc->sc_rng_co);
-}
-
diff --git a/sys/dev/crypto/padlock/rng_harvest_x86_64.S b/sys/dev/crypto/padlock/rng_harvest_x86_64.S
deleted file mode 100644
index 3de64e3a86..0000000000
--- a/sys/dev/crypto/padlock/rng_harvest_x86_64.S
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
- * 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 COPYRIGHT HOLDERS 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
- * COPYRIGHT HOLDERS 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 <machine/asmacros.h>
-
-	.text
-
-/* int padlock_rng(uint8_t *out, long limit) */
-ENTRY(padlock_rng)
-	movq	%rsi,	%rcx
-
-	movq	$3,	%rdx
-	xorq	%r11, 	%r11
-1:
-	/*
-	 * edx: (input)		quality factor of rng entropy
-	 * rdi: (input)		buffer for random data
-	 			autoincremented at end of instruction
-	 * eax: (output)	rng status word
-	 */
-	.byte	0x0f, 0xa7, 0xc0 /* xstore-rng */
-
-
-	/*
-	 * The lower 5 bits of %eax contain the number of random
-	 * bytes stored.
-	 * If no bytes were stored, there is no more entropy
-	 * available, so we finish up.
-	 */
-	andq	$0x1f, 	%rax
-	jz	2f
-
-	/*
-	 * Increment the count of stored random bytes. The buffer pointer
-	 * in rdi is auto-incremented by the xstore-rng instruction.
-	 */
-	addq	%rax, 	%r11
-
-	/*
-	 * If we haven't already read enough random bytes,
-	 * loop again.
-	 */
-	cmpq	%rcx, 	%r11
-	jl	1b
-2:
-	/* return the number of stored random bytes. */
-	movq	%r11, 	%rax
-	ret
-END(padlock_rng)
diff --git a/sys/dev/crypto/safe/Makefile b/sys/dev/crypto/safe/Makefile
deleted file mode 100644
index 7240068bee..0000000000
--- a/sys/dev/crypto/safe/Makefile
+++ /dev/null
@@ -1,39 +0,0 @@
-#-
-# Copyright (c) 2003 Sam Leffler, Errno Consulting
-# 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 AUTHOR 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 AUTHOR 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.
-#
-# $FreeBSD: src/sys/modules/safe/Makefile,v 1.5 2008/09/01 23:59:00 imp Exp $
-#
-
-KMOD	= safe
-SRCS	= safe.c opt_safe.h
-SRCS   += device_if.h bus_if.h pci_if.h
-SRCS   += cryptodev_if.h
-
-.if !defined(KERNBUILDDIR)
-opt_safe.h:
-	echo "#define SAFE_DEBUG 1" > ${.TARGET}
-.endif
-
-.include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/safe/safe.c b/sys/dev/crypto/safe/safe.c
deleted file mode 100644
index e4db4d55ad..0000000000
--- a/sys/dev/crypto/safe/safe.c
+++ /dev/null
@@ -1,2248 +0,0 @@
-/*-
- * Copyright (c) 2003 Sam Leffler, Errno Consulting
- * Copyright (c) 2003 Global Technology Associates, Inc.
- * 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 AUTHOR 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 AUTHOR 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.
- *
- * $FreeBSD: src/sys/dev/safe/safe.c,v 1.22 2011/06/12 23:33:08 delphij Exp $
- */
-
-/*
- * SafeNet SafeXcel-1141 hardware crypto accelerator
- */
-#include "opt_safe.h"
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/proc.h>
-#include <sys/errno.h>
-#include <sys/malloc.h>
-#include <sys/kernel.h>
-#include <sys/mbuf.h>
-#include <sys/module.h>
-#include <sys/lock.h>
-#include <sys/sysctl.h>
-#include <sys/endian.h>
-
-#include <vm/vm.h>
-#include <vm/pmap.h>
-
-#include <sys/bus.h>
-#include <sys/rman.h>
-
-#include <crypto/sha1.h>
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/cryptosoft.h>
-#include <sys/md5.h>
-#include <sys/random.h>
-#include <sys/kobj.h>
-
-#include "cryptodev_if.h"
-
-#include <bus/pci/pcivar.h>
-#include <bus/pci/pcireg.h>
-
-#ifdef SAFE_RNDTEST
-#include <dev/crypto/rndtest/rndtest.h>
-#endif
-#include <dev/crypto/safe/safereg.h>
-#include <dev/crypto/safe/safevar.h>
-
-#ifndef bswap32
-#define	bswap32	NTOHL
-#endif
-
-/*
- * Prototypes and count for the pci_device structure
- */
-static	int safe_probe(device_t);
-static	int safe_attach(device_t);
-static	int safe_detach(device_t);
-static	int safe_suspend(device_t);
-static	int safe_resume(device_t);
-static	int safe_shutdown(device_t);
-
-static	int safe_newsession(device_t, u_int32_t *, struct cryptoini *);
-static	int safe_freesession(device_t, u_int64_t);
-static	int safe_process(device_t, struct cryptop *, int);
-
-static device_method_t safe_methods[] = {
-	/* Device interface */
-	DEVMETHOD(device_probe,		safe_probe),
-	DEVMETHOD(device_attach,	safe_attach),
-	DEVMETHOD(device_detach,	safe_detach),
-	DEVMETHOD(device_suspend,	safe_suspend),
-	DEVMETHOD(device_resume,	safe_resume),
-	DEVMETHOD(device_shutdown,	safe_shutdown),
-
-	/* bus interface */
-	DEVMETHOD(bus_print_child,	bus_generic_print_child),
-	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
-
-	/* crypto device methods */
-	DEVMETHOD(cryptodev_newsession,	safe_newsession),
-	DEVMETHOD(cryptodev_freesession,safe_freesession),
-	DEVMETHOD(cryptodev_process,	safe_process),
-
-	DEVMETHOD_END
-};
-static driver_t safe_driver = {
-	"safe",
-	safe_methods,
-	sizeof (struct safe_softc)
-};
-static devclass_t safe_devclass;
-
-DRIVER_MODULE(safe, pci, safe_driver, safe_devclass, NULL, NULL);
-MODULE_DEPEND(safe, crypto, 1, 1, 1);
-#ifdef SAFE_RNDTEST
-MODULE_DEPEND(safe, rndtest, 1, 1, 1);
-#endif
-
-static	void safe_intr(void *);
-static	void safe_callback(struct safe_softc *, struct safe_ringentry *);
-static	void safe_feed(struct safe_softc *, struct safe_ringentry *);
-static	void safe_mcopy(struct mbuf *, struct mbuf *, u_int);
-#ifndef SAFE_NO_RNG
-static	void safe_rng_init(struct safe_softc *);
-static	void safe_rng(void *);
-#endif /* SAFE_NO_RNG */
-static	int safe_dma_malloc(struct safe_softc *, bus_size_t,
-	        struct safe_dma_alloc *, int);
-#define	safe_dma_sync(_dma, _flags) \
-	bus_dmamap_sync((_dma)->dma_tag, (_dma)->dma_map, (_flags))
-static	void safe_dma_free(struct safe_softc *, struct safe_dma_alloc *);
-static	int safe_dmamap_aligned(const struct safe_operand *);
-static	int safe_dmamap_uniform(const struct safe_operand *);
-
-static	void safe_reset_board(struct safe_softc *);
-static	void safe_init_board(struct safe_softc *);
-static	void safe_init_pciregs(device_t dev);
-static	void safe_cleanchip(struct safe_softc *);
-static	void safe_totalreset(struct safe_softc *);
-
-static	int safe_free_entry(struct safe_softc *, struct safe_ringentry *);
-
-SYSCTL_NODE(_hw, OID_AUTO, safe, CTLFLAG_RD, 0, "SafeNet driver parameters");
-
-#ifdef SAFE_DEBUG
-static	void safe_dump_dmastatus(struct safe_softc *, const char *);
-static	void safe_dump_ringstate(struct safe_softc *, const char *);
-static	void safe_dump_intrstate(struct safe_softc *, const char *);
-static	void safe_dump_request(struct safe_softc *, const char *,
-		struct safe_ringentry *);
-
-static	struct safe_softc *safec;		/* for use by hw.safe.dump */
-
-static	int safe_debug = 0;
-SYSCTL_INT(_hw_safe, OID_AUTO, debug, CTLFLAG_RW, &safe_debug,
-	    0, "control debugging msgs");
-#define	DPRINTF(_x)	if (safe_debug) kprintf _x
-#else
-#define	DPRINTF(_x)
-#endif
-
-#define	READ_REG(sc,r) \
-	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r))
-
-#define WRITE_REG(sc,reg,val) \
-	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val)
-
-struct safe_stats safestats;
-SYSCTL_STRUCT(_hw_safe, OID_AUTO, stats, CTLFLAG_RD, &safestats,
-	    safe_stats, "driver statistics");
-#ifndef SAFE_NO_RNG
-static	int safe_rnginterval = 1;		/* poll once a second */
-SYSCTL_INT(_hw_safe, OID_AUTO, rnginterval, CTLFLAG_RW, &safe_rnginterval,
-	    0, "RNG polling interval (secs)");
-static	int safe_rngbufsize = 16;		/* 64 bytes each poll  */
-SYSCTL_INT(_hw_safe, OID_AUTO, rngbufsize, CTLFLAG_RW, &safe_rngbufsize,
-	    0, "RNG polling buffer size (32-bit words)");
-static	int safe_rngmaxalarm = 8;		/* max alarms before reset */
-SYSCTL_INT(_hw_safe, OID_AUTO, rngmaxalarm, CTLFLAG_RW, &safe_rngmaxalarm,
-	    0, "RNG max alarms before reset");
-#endif /* SAFE_NO_RNG */
-
-static int
-safe_probe(device_t dev)
-{
-	if (pci_get_vendor(dev) == PCI_VENDOR_SAFENET &&
-	    pci_get_device(dev) == PCI_PRODUCT_SAFEXCEL)
-		return (BUS_PROBE_DEFAULT);
-	return (ENXIO);
-}
-
-static const char*
-safe_partname(struct safe_softc *sc)
-{
-	/* XXX sprintf numbers when not decoded */
-	switch (pci_get_vendor(sc->sc_dev)) {
-	case PCI_VENDOR_SAFENET:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_SAFEXCEL: return "SafeNet SafeXcel-1141";
-		}
-		return "SafeNet unknown-part";
-	}
-	return "Unknown-vendor unknown-part";
-}
-
-#ifndef SAFE_NO_RNG
-static void
-default_harvest(struct rndtest_state *rsp, void *buf, u_int count)
-{
-	add_buffer_randomness_src(buf, count, RAND_SRC_SAFE);
-}
-#endif /* SAFE_NO_RNG */
-
-static int
-safe_attach(device_t dev)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-	u_int32_t raddr;
-	u_int32_t cmd, i, devinfo;
-	int rid;
-
-	bzero(sc, sizeof (*sc));
-	sc->sc_dev = dev;
-
-	/* XXX handle power management */
-
-	cmd = pci_read_config(dev, PCIR_COMMAND, 4);
-	cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
-	pci_write_config(dev, PCIR_COMMAND, cmd, 4);
-	cmd = pci_read_config(dev, PCIR_COMMAND, 4);
-
-	if (!(cmd & PCIM_CMD_MEMEN)) {
-		device_printf(dev, "failed to enable memory mapping\n");
-		goto bad;
-	}
-
-	if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
-		device_printf(dev, "failed to enable bus mastering\n");
-		goto bad;
-	}
-
-	/*
-	 * Setup memory-mapping of PCI registers.
-	 */
-	rid = BS_BAR;
-	sc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
-					   RF_ACTIVE);
-	if (sc->sc_sr == NULL) {
-		device_printf(dev, "cannot map register space\n");
-		goto bad;
-	}
-	sc->sc_st = rman_get_bustag(sc->sc_sr);
-	sc->sc_sh = rman_get_bushandle(sc->sc_sr);
-
-	/*
-	 * Arrange interrupt line.
-	 */
-	rid = 0;
-	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
-					    RF_SHAREABLE|RF_ACTIVE);
-	if (sc->sc_irq == NULL) {
-		device_printf(dev, "could not map interrupt\n");
-		goto bad1;
-	}
-	/*
-	 * NB: Network code assumes we are blocked with splimp()
-	 *     so make sure the IRQ is mapped appropriately.
-	 */
-	if (bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE,
-			   safe_intr, sc, &sc->sc_ih, NULL)) {
-		device_printf(dev, "could not establish interrupt\n");
-		goto bad2;
-	}
-
-	sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
-	if (sc->sc_cid < 0) {
-		device_printf(dev, "could not get crypto driver id\n");
-		goto bad3;
-	}
-
-	sc->sc_chiprev = READ_REG(sc, SAFE_DEVINFO) &
-		(SAFE_DEVINFO_REV_MAJ | SAFE_DEVINFO_REV_MIN);
-
-	/*
-	 * Setup DMA descriptor area.
-	 */
-	if (bus_dma_tag_create(NULL,			/* parent */
-			       1,			/* alignment */
-			       SAFE_DMA_BOUNDARY,	/* boundary */
-			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
-			       BUS_SPACE_MAXADDR,	/* highaddr */
-			       SAFE_MAX_DMA,		/* maxsize */
-			       SAFE_MAX_PART,		/* nsegments */
-			       SAFE_MAX_SSIZE,		/* maxsegsize */
-			       BUS_DMA_ALLOCNOW,	/* flags */
-			       &sc->sc_srcdmat)) {
-		device_printf(dev, "cannot allocate DMA tag\n");
-		goto bad4;
-	}
-	if (bus_dma_tag_create(NULL,			/* parent */
-			       1,			/* alignment */
-			       SAFE_MAX_DSIZE,		/* boundary */
-			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
-			       BUS_SPACE_MAXADDR,	/* highaddr */
-			       SAFE_MAX_DMA,		/* maxsize */
-			       SAFE_MAX_PART,		/* nsegments */
-			       SAFE_MAX_DSIZE,		/* maxsegsize */
-			       BUS_DMA_ALLOCNOW,	/* flags */
-			       &sc->sc_dstdmat)) {
-		device_printf(dev, "cannot allocate DMA tag\n");
-		goto bad4;
-	}
-
-	/*
-	 * Allocate packet engine descriptors.
-	 */
-	if (safe_dma_malloc(sc,
-	    SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry),
-	    &sc->sc_ringalloc, 0)) {
-		device_printf(dev, "cannot allocate PE descriptor ring\n");
-		bus_dma_tag_destroy(sc->sc_srcdmat);
-		goto bad4;
-	}
-	/*
-	 * Hookup the static portion of all our data structures.
-	 */
-	sc->sc_ring = (struct safe_ringentry *) sc->sc_ringalloc.dma_vaddr;
-	sc->sc_ringtop = sc->sc_ring + SAFE_MAX_NQUEUE;
-	sc->sc_front = sc->sc_ring;
-	sc->sc_back = sc->sc_ring;
-	raddr = sc->sc_ringalloc.dma_paddr;
-	bzero(sc->sc_ring, SAFE_MAX_NQUEUE * sizeof(struct safe_ringentry));
-	for (i = 0; i < SAFE_MAX_NQUEUE; i++) {
-		struct safe_ringentry *re = &sc->sc_ring[i];
-
-		re->re_desc.d_sa = raddr +
-			offsetof(struct safe_ringentry, re_sa);
-		re->re_sa.sa_staterec = raddr +
-			offsetof(struct safe_ringentry, re_sastate);
-
-		raddr += sizeof (struct safe_ringentry);
-	}
-	lockinit(&sc->sc_ringlock, "packet engine ring", 0, LK_CANRECURSE);
-
-	/*
-	 * Allocate scatter and gather particle descriptors.
-	 */
-	if (safe_dma_malloc(sc, SAFE_TOTAL_SPART * sizeof (struct safe_pdesc),
-	    &sc->sc_spalloc, 0)) {
-		device_printf(dev, "cannot allocate source particle "
-			"descriptor ring\n");
-		lockuninit(&sc->sc_ringlock);
-		safe_dma_free(sc, &sc->sc_ringalloc);
-		bus_dma_tag_destroy(sc->sc_srcdmat);
-		goto bad4;
-	}
-	sc->sc_spring = (struct safe_pdesc *) sc->sc_spalloc.dma_vaddr;
-	sc->sc_springtop = sc->sc_spring + SAFE_TOTAL_SPART;
-	sc->sc_spfree = sc->sc_spring;
-	bzero(sc->sc_spring, SAFE_TOTAL_SPART * sizeof(struct safe_pdesc));
-
-	if (safe_dma_malloc(sc, SAFE_TOTAL_DPART * sizeof (struct safe_pdesc),
-	    &sc->sc_dpalloc, 0)) {
-		device_printf(dev, "cannot allocate destination particle "
-			"descriptor ring\n");
-		lockuninit(&sc->sc_ringlock);
-		safe_dma_free(sc, &sc->sc_spalloc);
-		safe_dma_free(sc, &sc->sc_ringalloc);
-		bus_dma_tag_destroy(sc->sc_dstdmat);
-		goto bad4;
-	}
-	sc->sc_dpring = (struct safe_pdesc *) sc->sc_dpalloc.dma_vaddr;
-	sc->sc_dpringtop = sc->sc_dpring + SAFE_TOTAL_DPART;
-	sc->sc_dpfree = sc->sc_dpring;
-	bzero(sc->sc_dpring, SAFE_TOTAL_DPART * sizeof(struct safe_pdesc));
-
-	device_printf(sc->sc_dev, "%s", safe_partname(sc));
-
-	devinfo = READ_REG(sc, SAFE_DEVINFO);
-	if (devinfo & SAFE_DEVINFO_RNG) {
-		sc->sc_flags |= SAFE_FLAGS_RNG;
-		kprintf(" rng");
-	}
-	if (devinfo & SAFE_DEVINFO_PKEY) {
-#if 0
-		kprintf(" key");
-		sc->sc_flags |= SAFE_FLAGS_KEY;
-		crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0);
-		crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0);
-#endif
-	}
-	if (devinfo & SAFE_DEVINFO_DES) {
-		kprintf(" des/3des");
-		crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
-		crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
-	}
-	if (devinfo & SAFE_DEVINFO_AES) {
-		kprintf(" aes");
-		crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
-	}
-	if (devinfo & SAFE_DEVINFO_MD5) {
-		kprintf(" md5");
-		crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
-	}
-	if (devinfo & SAFE_DEVINFO_SHA1) {
-		kprintf(" sha1");
-		crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
-	}
-	kprintf(" null");
-	crypto_register(sc->sc_cid, CRYPTO_NULL_CBC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_NULL_HMAC, 0, 0);
-	/* XXX other supported algorithms */
-	kprintf("\n");
-
-	safe_reset_board(sc);		/* reset h/w */
-	safe_init_pciregs(dev);		/* init pci settings */
-	safe_init_board(sc);		/* init h/w */
-
-#ifndef SAFE_NO_RNG
-	if (sc->sc_flags & SAFE_FLAGS_RNG) {
-#ifdef SAFE_RNDTEST
-		sc->sc_rndtest = rndtest_attach(dev);
-		if (sc->sc_rndtest)
-			sc->sc_harvest = rndtest_harvest;
-		else
-			sc->sc_harvest = default_harvest;
-#else
-		sc->sc_harvest = default_harvest;
-#endif
-		safe_rng_init(sc);
-
-		callout_init_mp(&sc->sc_rngto);
-		callout_reset(&sc->sc_rngto, hz*safe_rnginterval, safe_rng, sc);
-	}
-#endif /* SAFE_NO_RNG */
-#ifdef SAFE_DEBUG
-	safec = sc;			/* for use by hw.safe.dump */
-#endif
-	return (0);
-bad4:
-	crypto_unregister_all(sc->sc_cid);
-bad3:
-	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
-bad2:
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
-bad1:
-	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
-bad:
-	return (ENXIO);
-}
-
-/*
- * Detach a device that successfully probed.
- */
-static int
-safe_detach(device_t dev)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-
-	/* XXX wait/abort active ops */
-
-	WRITE_REG(sc, SAFE_HI_MASK, 0);		/* disable interrupts */
-
-	callout_stop(&sc->sc_rngto);
-
-	crypto_unregister_all(sc->sc_cid);
-
-#ifdef SAFE_RNDTEST
-	if (sc->sc_rndtest)
-		rndtest_detach(sc->sc_rndtest);
-#endif
-
-	safe_cleanchip(sc);
-	safe_dma_free(sc, &sc->sc_dpalloc);
-	safe_dma_free(sc, &sc->sc_spalloc);
-	lockuninit(&sc->sc_ringlock);
-	safe_dma_free(sc, &sc->sc_ringalloc);
-
-	bus_generic_detach(dev);
-	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
-
-	bus_dma_tag_destroy(sc->sc_srcdmat);
-	bus_dma_tag_destroy(sc->sc_dstdmat);
-	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
-
-	return (0);
-}
-
-/*
- * Stop all chip i/o so that the kernel's probe routines don't
- * get confused by errant DMAs when rebooting.
- */
-static int
-safe_shutdown(device_t dev)
-{
-#ifdef notyet
-	safe_stop(device_get_softc(dev));
-#endif
-	return (0);
-}
-
-/*
- * Device suspend routine.
- */
-static int
-safe_suspend(device_t dev)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-
-#ifdef notyet
-	/* XXX stop the device and save PCI settings */
-#endif
-	sc->sc_suspended = 1;
-
-	return (0);
-}
-
-static int
-safe_resume(device_t dev)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-
-#ifdef notyet
-	/* XXX retore PCI settings and start the device */
-#endif
-	sc->sc_suspended = 0;
-	return (0);
-}
-
-/*
- * SafeXcel Interrupt routine
- */
-static void
-safe_intr(void *arg)
-{
-	struct safe_softc *sc = arg;
-	volatile u_int32_t stat;
-
-	stat = READ_REG(sc, SAFE_HM_STAT);
-	if (stat == 0)			/* shared irq, not for us */
-		return;
-
-	WRITE_REG(sc, SAFE_HI_CLR, stat);	/* IACK */
-
-	if ((stat & SAFE_INT_PE_DDONE)) {
-		/*
-		 * Descriptor(s) done; scan the ring and
-		 * process completed operations.
-		 */
-		lockmgr(&sc->sc_ringlock, LK_EXCLUSIVE);
-		while (sc->sc_back != sc->sc_front) {
-			struct safe_ringentry *re = sc->sc_back;
-#ifdef SAFE_DEBUG
-			if (safe_debug) {
-				safe_dump_ringstate(sc, __func__);
-				safe_dump_request(sc, __func__, re);
-			}
-#endif
-			/*
-			 * safe_process marks ring entries that were allocated
-			 * but not used with a csr of zero.  This insures the
-			 * ring front pointer never needs to be set backwards
-			 * in the event that an entry is allocated but not used
-			 * because of a setup error.
-			 */
-			if (re->re_desc.d_csr != 0) {
-				if (!SAFE_PE_CSR_IS_DONE(re->re_desc.d_csr))
-					break;
-				if (!SAFE_PE_LEN_IS_DONE(re->re_desc.d_len))
-					break;
-				sc->sc_nqchip--;
-				safe_callback(sc, re);
-			}
-			if (++(sc->sc_back) == sc->sc_ringtop)
-				sc->sc_back = sc->sc_ring;
-		}
-		lockmgr(&sc->sc_ringlock, LK_RELEASE);
-	}
-
-	/*
-	 * Check to see if we got any DMA Error
-	 */
-	if (stat & SAFE_INT_PE_ERROR) {
-		DPRINTF(("dmaerr dmastat %08x\n",
-			READ_REG(sc, SAFE_PE_DMASTAT)));
-		safestats.st_dmaerr++;
-		safe_totalreset(sc);
-#if 0
-		safe_feed(sc);
-#endif
-	}
-
-	if (sc->sc_needwakeup) {		/* XXX check high watermark */
-		int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
-		DPRINTF(("%s: wakeup crypto %x\n", __func__,
-			sc->sc_needwakeup));
-		sc->sc_needwakeup &= ~wakeup;
-		crypto_unblock(sc->sc_cid, wakeup);
-	}
-}
-
-/*
- * safe_feed() - post a request to chip
- */
-static void
-safe_feed(struct safe_softc *sc, struct safe_ringentry *re)
-{
-	bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_PREWRITE);
-	if (re->re_dst_map != NULL)
-		bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map,
-			BUS_DMASYNC_PREREAD);
-	/* XXX have no smaller granularity */
-	safe_dma_sync(&sc->sc_ringalloc,
-		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	safe_dma_sync(&sc->sc_spalloc, BUS_DMASYNC_PREWRITE);
-	safe_dma_sync(&sc->sc_dpalloc, BUS_DMASYNC_PREWRITE);
-
-#ifdef SAFE_DEBUG
-	if (safe_debug) {
-		safe_dump_ringstate(sc, __func__);
-		safe_dump_request(sc, __func__, re);
-	}
-#endif
-	sc->sc_nqchip++;
-	if (sc->sc_nqchip > safestats.st_maxqchip)
-		safestats.st_maxqchip = sc->sc_nqchip;
-	/* poke h/w to check descriptor ring, any value can be written */
-	WRITE_REG(sc, SAFE_HI_RD_DESCR, 0);
-}
-
-static void
-safe_setup_enckey(struct safe_session *ses, caddr_t key)
-{
-	int i;
-
-	bcopy(key, ses->ses_key, ses->ses_klen / 8);
-
-	/* PE is little-endian, insure proper byte order */
-	for (i = 0; i < nitems(ses->ses_key); i++)
-		ses->ses_key[i] = htole32(ses->ses_key[i]);
-}
-
-static void
-safe_setup_mackey(struct safe_session *ses, int algo, caddr_t key, int klen)
-{
-	MD5_CTX md5ctx;
-	SHA1_CTX sha1ctx;
-	int i;
-
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= HMAC_IPAD_VAL;
-
-	if (algo == CRYPTO_MD5_HMAC) {
-		MD5Init(&md5ctx);
-		MD5Update(&md5ctx, key, klen);
-		MD5Update(&md5ctx, hmac_ipad_buffer, MD5_HMAC_BLOCK_LEN - klen);
-		/* gcc8 craps out on -Warray-bounds w/ optimized bcopy */
-		_bcopy(&md5ctx.A, ses->ses_hminner, sizeof(md5ctx.A) * 4);
-	} else {
-		SHA1Init(&sha1ctx);
-		SHA1Update(&sha1ctx, key, klen);
-		SHA1Update(&sha1ctx, hmac_ipad_buffer,
-		    SHA1_HMAC_BLOCK_LEN - klen);
-		bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32));
-	}
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
-
-	if (algo == CRYPTO_MD5_HMAC) {
-		MD5Init(&md5ctx);
-		MD5Update(&md5ctx, key, klen);
-		MD5Update(&md5ctx, hmac_opad_buffer, MD5_HMAC_BLOCK_LEN - klen);
-		/* gcc8 craps out on -Warray-bounds w/ optimized bcopy */
-		_bcopy(&md5ctx.A, ses->ses_hmouter, sizeof(md5ctx.A) * 4);
-	} else {
-		SHA1Init(&sha1ctx);
-		SHA1Update(&sha1ctx, key, klen);
-		SHA1Update(&sha1ctx, hmac_opad_buffer,
-		    SHA1_HMAC_BLOCK_LEN - klen);
-		bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32));
-	}
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= HMAC_OPAD_VAL;
-
-	/* PE is little-endian, insure proper byte order */
-	for (i = 0; i < nitems(ses->ses_hminner); i++) {
-		ses->ses_hminner[i] = htole32(ses->ses_hminner[i]);
-		ses->ses_hmouter[i] = htole32(ses->ses_hmouter[i]);
-	}
-}
-
-/*
- * Allocate a new 'session' and return an encoded session id.  'sidp'
- * contains our registration id, and should contain an encoded session
- * id on successful allocation.
- */
-static int
-safe_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-	struct cryptoini *c, *encini = NULL, *macini = NULL;
-	struct safe_session *ses = NULL;
-	int sesn;
-
-	if (sidp == NULL || cri == NULL || sc == NULL)
-		return (EINVAL);
-
-	for (c = cri; c != NULL; c = c->cri_next) {
-		if (c->cri_alg == CRYPTO_MD5_HMAC ||
-		    c->cri_alg == CRYPTO_SHA1_HMAC ||
-		    c->cri_alg == CRYPTO_NULL_HMAC) {
-			if (macini)
-				return (EINVAL);
-			macini = c;
-		} else if (c->cri_alg == CRYPTO_DES_CBC ||
-		    c->cri_alg == CRYPTO_3DES_CBC ||
-		    c->cri_alg == CRYPTO_AES_CBC ||
-		    c->cri_alg == CRYPTO_NULL_CBC) {
-			if (encini)
-				return (EINVAL);
-			encini = c;
-		} else
-			return (EINVAL);
-	}
-	if (encini == NULL && macini == NULL)
-		return (EINVAL);
-	if (encini) {			/* validate key length */
-		switch (encini->cri_alg) {
-		case CRYPTO_DES_CBC:
-			if (encini->cri_klen != 64)
-				return (EINVAL);
-			break;
-		case CRYPTO_3DES_CBC:
-			if (encini->cri_klen != 192)
-				return (EINVAL);
-			break;
-		case CRYPTO_AES_CBC:
-			if (encini->cri_klen != 128 &&
-			    encini->cri_klen != 192 &&
-			    encini->cri_klen != 256)
-				return (EINVAL);
-			break;
-		}
-	}
-
-	if (sc->sc_sessions == NULL) {
-		ses = sc->sc_sessions = (struct safe_session *)kmalloc(
-		    sizeof(struct safe_session), M_DEVBUF, M_NOWAIT);
-		if (ses == NULL)
-			return (ENOMEM);
-		sesn = 0;
-		sc->sc_nsessions = 1;
-	} else {
-		for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
-			if (sc->sc_sessions[sesn].ses_used == 0) {
-				ses = &sc->sc_sessions[sesn];
-				break;
-			}
-		}
-
-		if (ses == NULL) {
-			sesn = sc->sc_nsessions;
-			ses = (struct safe_session *)kmalloc((sesn + 1) *
-			    sizeof(struct safe_session), M_DEVBUF, M_NOWAIT);
-			if (ses == NULL)
-				return (ENOMEM);
-			bcopy(sc->sc_sessions, ses, sesn *
-			    sizeof(struct safe_session));
-			bzero(sc->sc_sessions, sesn *
-			    sizeof(struct safe_session));
-			kfree(sc->sc_sessions, M_DEVBUF);
-			sc->sc_sessions = ses;
-			ses = &sc->sc_sessions[sesn];
-			sc->sc_nsessions++;
-		}
-	}
-
-	bzero(ses, sizeof(struct safe_session));
-	ses->ses_used = 1;
-
-	if (encini) {
-		/* get an IV */
-		/* XXX may read fewer than requested */
-		read_random(ses->ses_iv, sizeof(ses->ses_iv), 0);
-
-		ses->ses_klen = encini->cri_klen;
-		if (encini->cri_key != NULL)
-			safe_setup_enckey(ses, encini->cri_key);
-	}
-
-	if (macini) {
-		ses->ses_mlen = macini->cri_mlen;
-		if (ses->ses_mlen == 0) {
-			if (macini->cri_alg == CRYPTO_MD5_HMAC)
-				ses->ses_mlen = MD5_HASH_LEN;
-			else
-				ses->ses_mlen = SHA1_HASH_LEN;
-		}
-
-		if (macini->cri_key != NULL) {
-			safe_setup_mackey(ses, macini->cri_alg, macini->cri_key,
-			    macini->cri_klen / 8);
-		}
-	}
-
-	*sidp = SAFE_SID(device_get_unit(sc->sc_dev), sesn);
-	return (0);
-}
-
-/*
- * Deallocate a session.
- */
-static int
-safe_freesession(device_t dev, u_int64_t tid)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-	int session, ret;
-	u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
-
-	if (sc == NULL)
-		return (EINVAL);
-
-	session = SAFE_SESSION(sid);
-	if (session < sc->sc_nsessions) {
-		bzero(&sc->sc_sessions[session], sizeof(sc->sc_sessions[session]));
-		ret = 0;
-	} else
-		ret = EINVAL;
-	return (ret);
-}
-
-static void
-safe_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, bus_size_t mapsize, int error)
-{
-	struct safe_operand *op = arg;
-
-	DPRINTF(("%s: mapsize %u nsegs %d error %d\n", __func__,
-		(u_int) mapsize, nsegs, error));
-	if (error != 0)
-		return;
-	op->mapsize = mapsize;
-	op->nsegs = nsegs;
-	bcopy(seg, op->segs, nsegs * sizeof (seg[0]));
-}
-
-static int
-safe_process(device_t dev, struct cryptop *crp, int hint)
-{
-	struct safe_softc *sc = device_get_softc(dev);
-	int err = 0, i, nicealign, uniform;
-	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
-	int bypass, oplen, ivsize;
-	caddr_t iv;
-	int16_t coffset;
-	struct safe_session *ses;
-	struct safe_ringentry *re;
-	struct safe_sarec *sa;
-	struct safe_pdesc *pd;
-	u_int32_t cmd0, cmd1, staterec;
-
-	if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
-		safestats.st_invalid++;
-		return (EINVAL);
-	}
-	if (SAFE_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
-		safestats.st_badsession++;
-		return (EINVAL);
-	}
-
-	lockmgr(&sc->sc_ringlock, LK_EXCLUSIVE);
-	if (sc->sc_front == sc->sc_back && sc->sc_nqchip != 0) {
-		safestats.st_ringfull++;
-		sc->sc_needwakeup |= CRYPTO_SYMQ;
-		lockmgr(&sc->sc_ringlock, LK_RELEASE);
-		return (ERESTART);
-	}
-	re = sc->sc_front;
-
-	staterec = re->re_sa.sa_staterec;	/* save */
-	/* NB: zero everything but the PE descriptor */
-	bzero(&re->re_sa, sizeof(struct safe_ringentry) - sizeof(re->re_desc));
-	re->re_sa.sa_staterec = staterec;	/* restore */
-
-	re->re_crp = crp;
-	re->re_sesn = SAFE_SESSION(crp->crp_sid);
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		re->re_src_m = (struct mbuf *)crp->crp_buf;
-		re->re_dst_m = (struct mbuf *)crp->crp_buf;
-	} else if (crp->crp_flags & CRYPTO_F_IOV) {
-		re->re_src_io = (struct uio *)crp->crp_buf;
-		re->re_dst_io = (struct uio *)crp->crp_buf;
-	} else {
-		safestats.st_badflags++;
-		err = EINVAL;
-		goto errout;	/* XXX we don't handle contiguous blocks! */
-	}
-
-	sa = &re->re_sa;
-	ses = &sc->sc_sessions[re->re_sesn];
-
-	crd1 = crp->crp_desc;
-	if (crd1 == NULL) {
-		safestats.st_nodesc++;
-		err = EINVAL;
-		goto errout;
-	}
-	crd2 = crd1->crd_next;
-
-	cmd0 = SAFE_SA_CMD0_BASIC;		/* basic group operation */
-	cmd1 = 0;
-	if (crd2 == NULL) {
-		if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
-		    crd1->crd_alg == CRYPTO_SHA1_HMAC ||
-		    crd1->crd_alg == CRYPTO_NULL_HMAC) {
-			maccrd = crd1;
-			enccrd = NULL;
-			cmd0 |= SAFE_SA_CMD0_OP_HASH;
-		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
-		    crd1->crd_alg == CRYPTO_3DES_CBC ||
-		    crd1->crd_alg == CRYPTO_AES_CBC ||
-		    crd1->crd_alg == CRYPTO_NULL_CBC) {
-			maccrd = NULL;
-			enccrd = crd1;
-			cmd0 |= SAFE_SA_CMD0_OP_CRYPT;
-		} else {
-			safestats.st_badalg++;
-			err = EINVAL;
-			goto errout;
-		}
-	} else {
-		if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
-		    crd1->crd_alg == CRYPTO_SHA1_HMAC ||
-		    crd1->crd_alg == CRYPTO_NULL_HMAC) &&
-		    (crd2->crd_alg == CRYPTO_DES_CBC ||
-			crd2->crd_alg == CRYPTO_3DES_CBC ||
-		        crd2->crd_alg == CRYPTO_AES_CBC ||
-		        crd2->crd_alg == CRYPTO_NULL_CBC) &&
-		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
-			maccrd = crd1;
-			enccrd = crd2;
-		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
-		    crd1->crd_alg == CRYPTO_3DES_CBC ||
-		    crd1->crd_alg == CRYPTO_AES_CBC ||
-		    crd1->crd_alg == CRYPTO_NULL_CBC) &&
-		    (crd2->crd_alg == CRYPTO_MD5_HMAC ||
-			crd2->crd_alg == CRYPTO_SHA1_HMAC ||
-			crd2->crd_alg == CRYPTO_NULL_HMAC) &&
-		    (crd1->crd_flags & CRD_F_ENCRYPT)) {
-			enccrd = crd1;
-			maccrd = crd2;
-		} else {
-			safestats.st_badalg++;
-			err = EINVAL;
-			goto errout;
-		}
-		cmd0 |= SAFE_SA_CMD0_OP_BOTH;
-	}
-
-	if (enccrd) {
-		if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT)
-			safe_setup_enckey(ses, enccrd->crd_key);
-
-		if (enccrd->crd_alg == CRYPTO_DES_CBC) {
-			cmd0 |= SAFE_SA_CMD0_DES;
-			cmd1 |= SAFE_SA_CMD1_CBC;
-			ivsize = 2*sizeof(u_int32_t);
-		} else if (enccrd->crd_alg == CRYPTO_3DES_CBC) {
-			cmd0 |= SAFE_SA_CMD0_3DES;
-			cmd1 |= SAFE_SA_CMD1_CBC;
-			ivsize = 2*sizeof(u_int32_t);
-		} else if (enccrd->crd_alg == CRYPTO_AES_CBC) {
-			cmd0 |= SAFE_SA_CMD0_AES;
-			cmd1 |= SAFE_SA_CMD1_CBC;
-			if (ses->ses_klen == 128)
-			     cmd1 |=  SAFE_SA_CMD1_AES128;
-			else if (ses->ses_klen == 192)
-			     cmd1 |=  SAFE_SA_CMD1_AES192;
-			else
-			     cmd1 |=  SAFE_SA_CMD1_AES256;
-			ivsize = 4*sizeof(u_int32_t);
-		} else {
-			cmd0 |= SAFE_SA_CMD0_CRYPT_NULL;
-			ivsize = 0;
-		}
-
-		/*
-		 * Setup encrypt/decrypt state.  When using basic ops
-		 * we can't use an inline IV because hash/crypt offset
-		 * must be from the end of the IV to the start of the
-		 * crypt data and this leaves out the preceding header
-		 * from the hash calculation.  Instead we place the IV
-		 * in the state record and set the hash/crypt offset to
-		 * copy both the header+IV.
-		 */
-		if (enccrd->crd_flags & CRD_F_ENCRYPT) {
-			cmd0 |= SAFE_SA_CMD0_OUTBOUND;
-
-			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
-				iv = enccrd->crd_iv;
-			else
-				iv = (caddr_t) ses->ses_iv;
-			if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
-				crypto_copyback(crp->crp_flags, crp->crp_buf,
-				    enccrd->crd_inject, ivsize, iv);
-			}
-			bcopy(iv, re->re_sastate.sa_saved_iv, ivsize);
-			cmd0 |= SAFE_SA_CMD0_IVLD_STATE | SAFE_SA_CMD0_SAVEIV;
-			re->re_flags |= SAFE_QFLAGS_COPYOUTIV;
-		} else {
-			cmd0 |= SAFE_SA_CMD0_INBOUND;
-
-			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT) {
-				bcopy(enccrd->crd_iv,
-					re->re_sastate.sa_saved_iv, ivsize);
-			} else {
-				crypto_copydata(crp->crp_flags, crp->crp_buf,
-				    enccrd->crd_inject, ivsize,
-				    (caddr_t)re->re_sastate.sa_saved_iv);
-			}
-			cmd0 |= SAFE_SA_CMD0_IVLD_STATE;
-		}
-		/*
-		 * For basic encryption use the zero pad algorithm.
-		 * This pads results to an 8-byte boundary and
-		 * suppresses padding verification for inbound (i.e.
-		 * decrypt) operations.
-		 *
-		 * NB: Not sure if the 8-byte pad boundary is a problem.
-		 */
-		cmd0 |= SAFE_SA_CMD0_PAD_ZERO;
-
-		/* XXX assert key bufs have the same size */
-		bcopy(ses->ses_key, sa->sa_key, sizeof(sa->sa_key));
-	}
-
-	if (maccrd) {
-		if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
-			safe_setup_mackey(ses, maccrd->crd_alg,
-			    maccrd->crd_key, maccrd->crd_klen / 8);
-		}
-
-		if (maccrd->crd_alg == CRYPTO_MD5_HMAC) {
-			cmd0 |= SAFE_SA_CMD0_MD5;
-			cmd1 |= SAFE_SA_CMD1_HMAC;	/* NB: enable HMAC */
-		} else if (maccrd->crd_alg == CRYPTO_SHA1_HMAC) {
-			cmd0 |= SAFE_SA_CMD0_SHA1;
-			cmd1 |= SAFE_SA_CMD1_HMAC;	/* NB: enable HMAC */
-		} else {
-			cmd0 |= SAFE_SA_CMD0_HASH_NULL;
-		}
-		/*
-		 * Digest data is loaded from the SA and the hash
-		 * result is saved to the state block where we
-		 * retrieve it for return to the caller.
-		 */
-		/* XXX assert digest bufs have the same size */
-		bcopy(ses->ses_hminner, sa->sa_indigest,
-			sizeof(sa->sa_indigest));
-		bcopy(ses->ses_hmouter, sa->sa_outdigest,
-			sizeof(sa->sa_outdigest));
-
-		cmd0 |= SAFE_SA_CMD0_HSLD_SA | SAFE_SA_CMD0_SAVEHASH;
-		re->re_flags |= SAFE_QFLAGS_COPYOUTICV;
-	}
-
-	if (enccrd && maccrd) {
-		/*
-		 * The offset from hash data to the start of
-		 * crypt data is the difference in the skips.
-		 */
-		bypass = maccrd->crd_skip;
-		coffset = enccrd->crd_skip - maccrd->crd_skip;
-		if (coffset < 0) {
-			DPRINTF(("%s: hash does not precede crypt; "
-				"mac skip %u enc skip %u\n",
-				__func__, maccrd->crd_skip, enccrd->crd_skip));
-			safestats.st_skipmismatch++;
-			err = EINVAL;
-			goto errout;
-		}
-		oplen = enccrd->crd_skip + enccrd->crd_len;
-		if (maccrd->crd_skip + maccrd->crd_len != oplen) {
-			DPRINTF(("%s: hash amount %u != crypt amount %u\n",
-				__func__, maccrd->crd_skip + maccrd->crd_len,
-				oplen));
-			safestats.st_lenmismatch++;
-			err = EINVAL;
-			goto errout;
-		}
-#ifdef SAFE_DEBUG
-		if (safe_debug) {
-			kprintf("mac: skip %d, len %d, inject %d\n",
-			    maccrd->crd_skip, maccrd->crd_len,
-			    maccrd->crd_inject);
-			kprintf("enc: skip %d, len %d, inject %d\n",
-			    enccrd->crd_skip, enccrd->crd_len,
-			    enccrd->crd_inject);
-			kprintf("bypass %d coffset %d oplen %d\n",
-				bypass, coffset, oplen);
-		}
-#endif
-		if (coffset & 3) {	/* offset must be 32-bit aligned */
-			DPRINTF(("%s: coffset %u misaligned\n",
-				__func__, coffset));
-			safestats.st_coffmisaligned++;
-			err = EINVAL;
-			goto errout;
-		}
-		coffset >>= 2;
-		if (coffset > 255) {	/* offset must be <256 dwords */
-			DPRINTF(("%s: coffset %u too big\n",
-				__func__, coffset));
-			safestats.st_cofftoobig++;
-			err = EINVAL;
-			goto errout;
-		}
-		/*
-		 * Tell the hardware to copy the header to the output.
-		 * The header is defined as the data from the end of
-		 * the bypass to the start of data to be encrypted.
-		 * Typically this is the inline IV.  Note that you need
-		 * to do this even if src+dst are the same; it appears
-		 * that w/o this bit the crypted data is written
-		 * immediately after the bypass data.
-		 */
-		cmd1 |= SAFE_SA_CMD1_HDRCOPY;
-		/*
-		 * Disable IP header mutable bit handling.  This is
-		 * needed to get correct HMAC calculations.
-		 */
-		cmd1 |= SAFE_SA_CMD1_MUTABLE;
-	} else {
-		if (enccrd) {
-			bypass = enccrd->crd_skip;
-			oplen = bypass + enccrd->crd_len;
-		} else {
-			bypass = maccrd->crd_skip;
-			oplen = bypass + maccrd->crd_len;
-		}
-		coffset = 0;
-	}
-	/* XXX verify multiple of 4 when using s/g */
-	if (bypass > 96) {		/* bypass offset must be <= 96 bytes */
-		DPRINTF(("%s: bypass %u too big\n", __func__, bypass));
-		safestats.st_bypasstoobig++;
-		err = EINVAL;
-		goto errout;
-	}
-
-	if (bus_dmamap_create(sc->sc_srcdmat, BUS_DMA_NOWAIT, &re->re_src_map)) {
-		safestats.st_nomap++;
-		err = ENOMEM;
-		goto errout;
-	}
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		if (bus_dmamap_load_mbuf(sc->sc_srcdmat, re->re_src_map,
-		    re->re_src_m, safe_op_cb,
-		    &re->re_src, BUS_DMA_NOWAIT) != 0) {
-			bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
-			re->re_src_map = NULL;
-			safestats.st_noload++;
-			err = ENOMEM;
-			goto errout;
-		}
-	} else if (crp->crp_flags & CRYPTO_F_IOV) {
-		if (bus_dmamap_load_uio(sc->sc_srcdmat, re->re_src_map,
-		    re->re_src_io, safe_op_cb,
-		    &re->re_src, BUS_DMA_NOWAIT) != 0) {
-			bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
-			re->re_src_map = NULL;
-			safestats.st_noload++;
-			err = ENOMEM;
-			goto errout;
-		}
-	}
-	nicealign = safe_dmamap_aligned(&re->re_src);
-	uniform = safe_dmamap_uniform(&re->re_src);
-
-	DPRINTF(("src nicealign %u uniform %u nsegs %u\n",
-		nicealign, uniform, re->re_src.nsegs));
-	if (re->re_src.nsegs > 1) {
-		re->re_desc.d_src = sc->sc_spalloc.dma_paddr +
-			((caddr_t) sc->sc_spfree - (caddr_t) sc->sc_spring);
-		for (i = 0; i < re->re_src_nsegs; i++) {
-			/* NB: no need to check if there's space */
-			pd = sc->sc_spfree;
-			if (++(sc->sc_spfree) == sc->sc_springtop)
-				sc->sc_spfree = sc->sc_spring;
-
-			KASSERT((pd->pd_flags&3) == 0 ||
-				(pd->pd_flags&3) == SAFE_PD_DONE,
-				("bogus source particle descriptor; flags %x",
-				pd->pd_flags));
-			pd->pd_addr = re->re_src_segs[i].ds_addr;
-			pd->pd_size = re->re_src_segs[i].ds_len;
-			pd->pd_flags = SAFE_PD_READY;
-		}
-		cmd0 |= SAFE_SA_CMD0_IGATHER;
-	} else {
-		/*
-		 * No need for gather, reference the operand directly.
-		 */
-		re->re_desc.d_src = re->re_src_segs[0].ds_addr;
-	}
-
-	if (enccrd == NULL && maccrd != NULL) {
-		/*
-		 * Hash op; no destination needed.
-		 */
-	} else {
-		if (crp->crp_flags & CRYPTO_F_IOV) {
-			if (!nicealign) {
-				safestats.st_iovmisaligned++;
-				err = EINVAL;
-				goto errout;
-			}
-			if (uniform != 1) {
-				/*
-				 * Source is not suitable for direct use as
-				 * the destination.  Create a new scatter/gather
-				 * list based on the destination requirements
-				 * and check if that's ok.
-				 */
-				if (bus_dmamap_create(sc->sc_dstdmat,
-				    BUS_DMA_NOWAIT, &re->re_dst_map)) {
-					safestats.st_nomap++;
-					err = ENOMEM;
-					goto errout;
-				}
-				if (bus_dmamap_load_uio(sc->sc_dstdmat,
-				    re->re_dst_map, re->re_dst_io,
-				    safe_op_cb, &re->re_dst,
-				    BUS_DMA_NOWAIT) != 0) {
-					bus_dmamap_destroy(sc->sc_dstdmat,
-						re->re_dst_map);
-					re->re_dst_map = NULL;
-					safestats.st_noload++;
-					err = ENOMEM;
-					goto errout;
-				}
-				uniform = safe_dmamap_uniform(&re->re_dst);
-				if (!uniform) {
-					/*
-					 * There's no way to handle the DMA
-					 * requirements with this uio.  We
-					 * could create a separate DMA area for
-					 * the result and then copy it back,
-					 * but for now we just bail and return
-					 * an error.  Note that uio requests
-					 * > SAFE_MAX_DSIZE are handled because
-					 * the DMA map and segment list for the
-					 * destination wil result in a
-					 * destination particle list that does
-					 * the necessary scatter DMA.
-					 */
-					safestats.st_iovnotuniform++;
-					err = EINVAL;
-					goto errout;
-				}
-			} else
-				re->re_dst = re->re_src;
-		} else if (crp->crp_flags & CRYPTO_F_IMBUF) {
-			if (nicealign && uniform == 1) {
-				/*
-				 * Source layout is suitable for direct
-				 * sharing of the DMA map and segment list.
-				 */
-				re->re_dst = re->re_src;
-			} else if (nicealign && uniform == 2) {
-				/*
-				 * The source is properly aligned but requires a
-				 * different particle list to handle DMA of the
-				 * result.  Create a new map and do the load to
-				 * create the segment list.  The particle
-				 * descriptor setup code below will handle the
-				 * rest.
-				 */
-				if (bus_dmamap_create(sc->sc_dstdmat,
-				    BUS_DMA_NOWAIT, &re->re_dst_map)) {
-					safestats.st_nomap++;
-					err = ENOMEM;
-					goto errout;
-				}
-				if (bus_dmamap_load_mbuf(sc->sc_dstdmat,
-				    re->re_dst_map, re->re_dst_m,
-				    safe_op_cb, &re->re_dst,
-				    BUS_DMA_NOWAIT) != 0) {
-					bus_dmamap_destroy(sc->sc_dstdmat,
-						re->re_dst_map);
-					re->re_dst_map = NULL;
-					safestats.st_noload++;
-					err = ENOMEM;
-					goto errout;
-				}
-			} else {		/* !(aligned and/or uniform) */
-				int totlen, len;
-				struct mbuf *m, *top, **mp;
-
-				/*
-				 * DMA constraints require that we allocate a
-				 * new mbuf chain for the destination.  We
-				 * allocate an entire new set of mbufs of
-				 * optimal/required size and then tell the
-				 * hardware to copy any bits that are not
-				 * created as a byproduct of the operation.
-				 */
-				if (!nicealign)
-					safestats.st_unaligned++;
-				if (!uniform)
-					safestats.st_notuniform++;
-				totlen = re->re_src_mapsize;
-				if (re->re_src_m->m_flags & M_PKTHDR) {
-					len = MHLEN;
-					MGETHDR(m, M_NOWAIT, MT_DATA);
-					if (m && !m_dup_pkthdr(m, re->re_src_m,
-					    M_NOWAIT)) {
-						m_free(m);
-						m = NULL;
-					}
-				} else {
-					len = MLEN;
-					MGET(m, M_NOWAIT, MT_DATA);
-				}
-				if (m == NULL) {
-					safestats.st_nombuf++;
-					err = sc->sc_nqchip ? ERESTART : ENOMEM;
-					goto errout;
-				}
-				if (totlen >= MINCLSIZE) {
-					MCLGET(m, M_NOWAIT);
-					if ((m->m_flags & M_EXT) == 0) {
-						m_free(m);
-						safestats.st_nomcl++;
-						err = sc->sc_nqchip ?
-							ERESTART : ENOMEM;
-						goto errout;
-					}
-					len = MCLBYTES;
-				}
-				m->m_len = len;
-				top = NULL;
-				mp = &top;
-
-				while (totlen > 0) {
-					if (top) {
-						MGET(m, M_NOWAIT, MT_DATA);
-						if (m == NULL) {
-							m_freem(top);
-							safestats.st_nombuf++;
-							err = sc->sc_nqchip ?
-							    ERESTART : ENOMEM;
-							goto errout;
-						}
-						len = MLEN;
-					}
-					if (top && totlen >= MINCLSIZE) {
-						MCLGET(m, M_NOWAIT);
-						if ((m->m_flags & M_EXT) == 0) {
-							*mp = m;
-							m_freem(top);
-							safestats.st_nomcl++;
-							err = sc->sc_nqchip ?
-							    ERESTART : ENOMEM;
-							goto errout;
-						}
-						len = MCLBYTES;
-					}
-					m->m_len = len = min(totlen, len);
-					totlen -= len;
-					*mp = m;
-					mp = &m->m_next;
-				}
-				re->re_dst_m = top;
-				if (bus_dmamap_create(sc->sc_dstdmat,
-				    BUS_DMA_NOWAIT, &re->re_dst_map) != 0) {
-					safestats.st_nomap++;
-					err = ENOMEM;
-					goto errout;
-				}
-				if (bus_dmamap_load_mbuf(sc->sc_dstdmat,
-				    re->re_dst_map, re->re_dst_m,
-				    safe_op_cb, &re->re_dst,
-				    BUS_DMA_NOWAIT) != 0) {
-					bus_dmamap_destroy(sc->sc_dstdmat,
-					re->re_dst_map);
-					re->re_dst_map = NULL;
-					safestats.st_noload++;
-					err = ENOMEM;
-					goto errout;
-				}
-				if (re->re_src.mapsize > oplen) {
-					/*
-					 * There's data following what the
-					 * hardware will copy for us.  If this
-					 * isn't just the ICV (that's going to
-					 * be written on completion), copy it
-					 * to the new mbufs
-					 */
-					if (!(maccrd &&
-					    (re->re_src.mapsize-oplen) == 12 &&
-					    maccrd->crd_inject == oplen))
-						safe_mcopy(re->re_src_m,
-							   re->re_dst_m,
-							   oplen);
-					else
-						safestats.st_noicvcopy++;
-				}
-			}
-		} else {
-			safestats.st_badflags++;
-			err = EINVAL;
-			goto errout;
-		}
-
-		if (re->re_dst.nsegs > 1) {
-			re->re_desc.d_dst = sc->sc_dpalloc.dma_paddr +
-			    ((caddr_t) sc->sc_dpfree - (caddr_t) sc->sc_dpring);
-			for (i = 0; i < re->re_dst_nsegs; i++) {
-				pd = sc->sc_dpfree;
-				KASSERT((pd->pd_flags&3) == 0 ||
-					(pd->pd_flags&3) == SAFE_PD_DONE,
-					("bogus dest particle descriptor; flags %x",
-						pd->pd_flags));
-				if (++(sc->sc_dpfree) == sc->sc_dpringtop)
-					sc->sc_dpfree = sc->sc_dpring;
-				pd->pd_addr = re->re_dst_segs[i].ds_addr;
-				pd->pd_flags = SAFE_PD_READY;
-			}
-			cmd0 |= SAFE_SA_CMD0_OSCATTER;
-		} else {
-			/*
-			 * No need for scatter, reference the operand directly.
-			 */
-			re->re_desc.d_dst = re->re_dst_segs[0].ds_addr;
-		}
-	}
-
-	/*
-	 * All done with setup; fillin the SA command words
-	 * and the packet engine descriptor.  The operation
-	 * is now ready for submission to the hardware.
-	 */
-	sa->sa_cmd0 = cmd0 | SAFE_SA_CMD0_IPCI | SAFE_SA_CMD0_OPCI;
-	sa->sa_cmd1 = cmd1
-		    | (coffset << SAFE_SA_CMD1_OFFSET_S)
-		    | SAFE_SA_CMD1_SAREV1	/* Rev 1 SA data structure */
-		    | SAFE_SA_CMD1_SRPCI
-		    ;
-	/*
-	 * NB: the order of writes is important here.  In case the
-	 * chip is scanning the ring because of an outstanding request
-	 * it might nab this one too.  In that case we need to make
-	 * sure the setup is complete before we write the length
-	 * field of the descriptor as it signals the descriptor is
-	 * ready for processing.
-	 */
-	re->re_desc.d_csr = SAFE_PE_CSR_READY | SAFE_PE_CSR_SAPCI;
-	if (maccrd)
-		re->re_desc.d_csr |= SAFE_PE_CSR_LOADSA | SAFE_PE_CSR_HASHFINAL;
-	re->re_desc.d_len = oplen
-			  | SAFE_PE_LEN_READY
-			  | (bypass << SAFE_PE_LEN_BYPASS_S)
-			  ;
-
-	safestats.st_ipackets++;
-	safestats.st_ibytes += oplen;
-
-	if (++(sc->sc_front) == sc->sc_ringtop)
-		sc->sc_front = sc->sc_ring;
-
-	/* XXX honor batching */
-	safe_feed(sc, re);
-	lockmgr(&sc->sc_ringlock, LK_RELEASE);
-	return (0);
-
-errout:
-	if ((re->re_dst_m != NULL) && (re->re_src_m != re->re_dst_m))
-		m_freem(re->re_dst_m);
-
-	if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) {
-		bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map);
-		bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map);
-	}
-	if (re->re_src_map != NULL) {
-		bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map);
-		bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
-	}
-	lockmgr(&sc->sc_ringlock, LK_RELEASE);
-	if (err != ERESTART) {
-		crp->crp_etype = err;
-		crypto_done(crp);
-	} else {
-		sc->sc_needwakeup |= CRYPTO_SYMQ;
-	}
-	return (err);
-}
-
-static void
-safe_callback(struct safe_softc *sc, struct safe_ringentry *re)
-{
-	struct cryptop *crp = (struct cryptop *)re->re_crp;
-	struct cryptodesc *crd;
-
-	safestats.st_opackets++;
-	safestats.st_obytes += re->re_dst.mapsize;
-
-	safe_dma_sync(&sc->sc_ringalloc,
-		BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
-	if (re->re_desc.d_csr & SAFE_PE_CSR_STATUS) {
-		device_printf(sc->sc_dev, "csr 0x%x cmd0 0x%x cmd1 0x%x\n",
-			re->re_desc.d_csr,
-			re->re_sa.sa_cmd0, re->re_sa.sa_cmd1);
-		safestats.st_peoperr++;
-		crp->crp_etype = EIO;		/* something more meaningful? */
-	}
-	if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) {
-		bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map,
-		    BUS_DMASYNC_POSTREAD);
-		bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map);
-		bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map);
-	}
-	bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_POSTWRITE);
-	bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map);
-	bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map);
-
-	/*
-	 * If result was written to a differet mbuf chain, swap
-	 * it in as the return value and reclaim the original.
-	 */
-	if ((crp->crp_flags & CRYPTO_F_IMBUF) && re->re_src_m != re->re_dst_m) {
-		m_freem(re->re_src_m);
-		crp->crp_buf = (caddr_t)re->re_dst_m;
-	}
-
-	if (re->re_flags & SAFE_QFLAGS_COPYOUTIV) {
-		/* copy out IV for future use */
-		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-			int ivsize;
-
-			if (crd->crd_alg == CRYPTO_DES_CBC ||
-			    crd->crd_alg == CRYPTO_3DES_CBC) {
-				ivsize = 2*sizeof(u_int32_t);
-			} else if (crd->crd_alg == CRYPTO_AES_CBC) {
-				ivsize = 4*sizeof(u_int32_t);
-			} else
-				continue;
-			crypto_copydata(crp->crp_flags, crp->crp_buf,
-			    crd->crd_skip + crd->crd_len - ivsize, ivsize,
-			    (caddr_t)sc->sc_sessions[re->re_sesn].ses_iv);
-			break;
-		}
-	}
-
-	if (re->re_flags & SAFE_QFLAGS_COPYOUTICV) {
-		/* copy out ICV result */
-		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-			if (!(crd->crd_alg == CRYPTO_MD5_HMAC ||
-			    crd->crd_alg == CRYPTO_SHA1_HMAC ||
-			    crd->crd_alg == CRYPTO_NULL_HMAC))
-				continue;
-			if (crd->crd_alg == CRYPTO_SHA1_HMAC) {
-				/*
-				 * SHA-1 ICV's are byte-swapped; fix 'em up
-				 * before copy them to their destination.
-				 */
-				re->re_sastate.sa_saved_indigest[0] =
-				    bswap32(re->re_sastate.sa_saved_indigest[0]);
-				re->re_sastate.sa_saved_indigest[1] =
-				    bswap32(re->re_sastate.sa_saved_indigest[1]);
-				re->re_sastate.sa_saved_indigest[2] =
-				    bswap32(re->re_sastate.sa_saved_indigest[2]);
-			}
-			crypto_copyback(crp->crp_flags, crp->crp_buf,
-			    crd->crd_inject,
-			    sc->sc_sessions[re->re_sesn].ses_mlen,
-			    (caddr_t)re->re_sastate.sa_saved_indigest);
-			break;
-		}
-	}
-	crypto_done(crp);
-}
-
-/*
- * Copy all data past offset from srcm to dstm.
- */
-static void
-safe_mcopy(struct mbuf *srcm, struct mbuf *dstm, u_int offset)
-{
-	u_int j, dlen, slen;
-	caddr_t dptr, sptr;
-
-	/*
-	 * Advance src and dst to offset.
-	 */
-	j = offset;
-	while (j >= 0) {
-		if (srcm->m_len > j)
-			break;
-		j -= srcm->m_len;
-		srcm = srcm->m_next;
-		if (srcm == NULL)
-			return;
-	}
-	sptr = mtod(srcm, caddr_t) + j;
-	slen = srcm->m_len - j;
-
-	j = offset;
-	while (j >= 0) {
-		if (dstm->m_len > j)
-			break;
-		j -= dstm->m_len;
-		dstm = dstm->m_next;
-		if (dstm == NULL)
-			return;
-	}
-	dptr = mtod(dstm, caddr_t) + j;
-	dlen = dstm->m_len - j;
-
-	/*
-	 * Copy everything that remains.
-	 */
-	for (;;) {
-		j = min(slen, dlen);
-		bcopy(sptr, dptr, j);
-		if (slen == j) {
-			srcm = srcm->m_next;
-			if (srcm == NULL)
-				return;
-			sptr = srcm->m_data;
-			slen = srcm->m_len;
-		} else
-			sptr += j, slen -= j;
-		if (dlen == j) {
-			dstm = dstm->m_next;
-			if (dstm == NULL)
-				return;
-			dptr = dstm->m_data;
-			dlen = dstm->m_len;
-		} else
-			dptr += j, dlen -= j;
-	}
-}
-
-#ifndef SAFE_NO_RNG
-#define	SAFE_RNG_MAXWAIT	1000
-
-static void
-safe_rng_init(struct safe_softc *sc)
-{
-	u_int32_t w, v;
-	int i;
-
-	WRITE_REG(sc, SAFE_RNG_CTRL, 0);
-	/* use default value according to the manual */
-	WRITE_REG(sc, SAFE_RNG_CNFG, 0x834);	/* magic from SafeNet */
-	WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
-
-	/*
-	 * There is a bug in rev 1.0 of the 1140 that when the RNG
-	 * is brought out of reset the ready status flag does not
-	 * work until the RNG has finished its internal initialization.
-	 *
-	 * So in order to determine the device is through its
-	 * initialization we must read the data register, using the
-	 * status reg in the read in case it is initialized.  Then read
-	 * the data register until it changes from the first read.
-	 * Once it changes read the data register until it changes
-	 * again.  At this time the RNG is considered initialized.
-	 * This could take between 750ms - 1000ms in time.
-	 */
-	i = 0;
-	w = READ_REG(sc, SAFE_RNG_OUT);
-	do {
-		v = READ_REG(sc, SAFE_RNG_OUT);
-		if (v != w) {
-			w = v;
-			break;
-		}
-		DELAY(10);
-	} while (++i < SAFE_RNG_MAXWAIT);
-
-	/* Wait Until data changes again */
-	i = 0;
-	do {
-		v = READ_REG(sc, SAFE_RNG_OUT);
-		if (v != w)
-			break;
-		DELAY(10);
-	} while (++i < SAFE_RNG_MAXWAIT);
-}
-
-static __inline void
-safe_rng_disable_short_cycle(struct safe_softc *sc)
-{
-	WRITE_REG(sc, SAFE_RNG_CTRL,
-		READ_REG(sc, SAFE_RNG_CTRL) &~ SAFE_RNG_CTRL_SHORTEN);
-}
-
-static __inline void
-safe_rng_enable_short_cycle(struct safe_softc *sc)
-{
-	WRITE_REG(sc, SAFE_RNG_CTRL,
-		READ_REG(sc, SAFE_RNG_CTRL) | SAFE_RNG_CTRL_SHORTEN);
-}
-
-static __inline u_int32_t
-safe_rng_read(struct safe_softc *sc)
-{
-	int i;
-
-	i = 0;
-	while (READ_REG(sc, SAFE_RNG_STAT) != 0 && ++i < SAFE_RNG_MAXWAIT)
-		;
-	return READ_REG(sc, SAFE_RNG_OUT);
-}
-
-static void
-safe_rng(void *arg)
-{
-	struct safe_softc *sc = arg;
-	u_int32_t buf[SAFE_RNG_MAXBUFSIZ];	/* NB: maybe move to softc */
-	u_int maxwords;
-	int i;
-
-	safestats.st_rng++;
-	/*
-	 * Fetch the next block of data.
-	 */
-	maxwords = safe_rngbufsize;
-	if (maxwords > SAFE_RNG_MAXBUFSIZ)
-		maxwords = SAFE_RNG_MAXBUFSIZ;
-retry:
-	for (i = 0; i < maxwords; i++)
-		buf[i] = safe_rng_read(sc);
-	/*
-	 * Check the comparator alarm count and reset the h/w if
-	 * it exceeds our threshold.  This guards against the
-	 * hardware oscillators resonating with external signals.
-	 */
-	if (READ_REG(sc, SAFE_RNG_ALM_CNT) > safe_rngmaxalarm) {
-		u_int32_t freq_inc, w;
-
-		DPRINTF(("%s: alarm count %u exceeds threshold %u\n", __func__,
-			READ_REG(sc, SAFE_RNG_ALM_CNT), safe_rngmaxalarm));
-		safestats.st_rngalarm++;
-		safe_rng_enable_short_cycle(sc);
-		freq_inc = 18;
-		for (i = 0; i < 64; i++) {
-			w = READ_REG(sc, SAFE_RNG_CNFG);
-			freq_inc = ((w + freq_inc) & 0x3fL);
-			w = ((w & ~0x3fL) | freq_inc);
-			WRITE_REG(sc, SAFE_RNG_CNFG, w);
-
-			WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
-
-			(void) safe_rng_read(sc);
-			DELAY(25);
-
-			if (READ_REG(sc, SAFE_RNG_ALM_CNT) == 0) {
-				safe_rng_disable_short_cycle(sc);
-				goto retry;
-			}
-			freq_inc = 1;
-		}
-		safe_rng_disable_short_cycle(sc);
-	} else
-		WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0);
-
-	(*sc->sc_harvest)(sc->sc_rndtest, buf, maxwords*sizeof (u_int32_t));
-	callout_reset(&sc->sc_rngto,
-		hz * (safe_rnginterval ? safe_rnginterval : 1), safe_rng, sc);
-}
-#endif /* SAFE_NO_RNG */
-
-static void
-safe_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
-{
-	bus_addr_t *paddr = (bus_addr_t*) arg;
-	*paddr = segs->ds_addr;
-}
-
-static int
-safe_dma_malloc(
-	struct safe_softc *sc,
-	bus_size_t size,
-	struct safe_dma_alloc *dma,
-	int mapflags
-)
-{
-	int r;
-
-	r = bus_dma_tag_create(NULL,			/* parent */
-			       sizeof(u_int32_t), 0,	/* alignment, bounds */
-			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
-			       BUS_SPACE_MAXADDR,	/* highaddr */
-			       size,			/* maxsize */
-			       1,			/* nsegments */
-			       size,			/* maxsegsize */
-			       BUS_DMA_ALLOCNOW,	/* flags */
-			       &dma->dma_tag);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "safe_dma_malloc: "
-			"bus_dma_tag_create failed; error %u\n", r);
-		goto fail_0;
-	}
-
-	r = bus_dmamap_create(dma->dma_tag, BUS_DMA_NOWAIT, &dma->dma_map);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "safe_dma_malloc: "
-			"bus_dmamap_create failed; error %u\n", r);
-		goto fail_1;
-	}
-
-	r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
-			     BUS_DMA_NOWAIT, &dma->dma_map);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "safe_dma_malloc: "
-			"bus_dmammem_alloc failed; size %zu, error %u\n",
-			size, r);
-		goto fail_2;
-	}
-
-	r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
-		            size,
-			    safe_dmamap_cb,
-			    &dma->dma_paddr,
-			    mapflags | BUS_DMA_NOWAIT);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "safe_dma_malloc: "
-			"bus_dmamap_load failed; error %u\n", r);
-		goto fail_3;
-	}
-
-	dma->dma_size = size;
-	return (0);
-
-fail_3:
-	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
-fail_2:
-	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
-fail_1:
-	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
-	bus_dma_tag_destroy(dma->dma_tag);
-fail_0:
-	dma->dma_map = NULL;
-	dma->dma_tag = NULL;
-	return (r);
-}
-
-static void
-safe_dma_free(struct safe_softc *sc, struct safe_dma_alloc *dma)
-{
-	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
-	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
-	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
-	bus_dma_tag_destroy(dma->dma_tag);
-}
-
-/*
- * Resets the board.  Values in the regesters are left as is
- * from the reset (i.e. initial values are assigned elsewhere).
- */
-static void
-safe_reset_board(struct safe_softc *sc)
-{
-	u_int32_t v;
-	/*
-	 * Reset the device.  The manual says no delay
-	 * is needed between marking and clearing reset.
-	 */
-	v = READ_REG(sc, SAFE_PE_DMACFG) &~
-		(SAFE_PE_DMACFG_PERESET | SAFE_PE_DMACFG_PDRRESET |
-		 SAFE_PE_DMACFG_SGRESET);
-	WRITE_REG(sc, SAFE_PE_DMACFG, v
-				    | SAFE_PE_DMACFG_PERESET
-				    | SAFE_PE_DMACFG_PDRRESET
-				    | SAFE_PE_DMACFG_SGRESET);
-	WRITE_REG(sc, SAFE_PE_DMACFG, v);
-}
-
-/*
- * Initialize registers we need to touch only once.
- */
-static void
-safe_init_board(struct safe_softc *sc)
-{
-	u_int32_t v, dwords;
-
-	v = READ_REG(sc, SAFE_PE_DMACFG);
-	v &=~ SAFE_PE_DMACFG_PEMODE;
-	v |= SAFE_PE_DMACFG_FSENA		/* failsafe enable */
-	  |  SAFE_PE_DMACFG_GPRPCI		/* gather ring on PCI */
-	  |  SAFE_PE_DMACFG_SPRPCI		/* scatter ring on PCI */
-	  |  SAFE_PE_DMACFG_ESDESC		/* endian-swap descriptors */
-	  |  SAFE_PE_DMACFG_ESSA		/* endian-swap SA's */
-	  |  SAFE_PE_DMACFG_ESPDESC		/* endian-swap part. desc's */
-	  ;
-	WRITE_REG(sc, SAFE_PE_DMACFG, v);
-#if 0
-	/* XXX select byte swap based on host byte order */
-	WRITE_REG(sc, SAFE_ENDIAN, 0x1b);
-#endif
-	if (sc->sc_chiprev == SAFE_REV(1,0)) {
-		/*
-		 * Avoid large PCI DMA transfers.  Rev 1.0 has a bug where
-		 * "target mode transfers" done while the chip is DMA'ing
-		 * >1020 bytes cause the hardware to lockup.  To avoid this
-		 * we reduce the max PCI transfer size and use small source
-		 * particle descriptors (<= 256 bytes).
-		 */
-		WRITE_REG(sc, SAFE_DMA_CFG, 256);
-		device_printf(sc->sc_dev,
-			"Reduce max DMA size to %u words for rev %u.%u WAR\n",
-			(READ_REG(sc, SAFE_DMA_CFG)>>2) & 0xff,
-			SAFE_REV_MAJ(sc->sc_chiprev),
-			SAFE_REV_MIN(sc->sc_chiprev));
-	}
-
-	/* NB: operands+results are overlaid */
-	WRITE_REG(sc, SAFE_PE_PDRBASE, sc->sc_ringalloc.dma_paddr);
-	WRITE_REG(sc, SAFE_PE_RDRBASE, sc->sc_ringalloc.dma_paddr);
-	/*
-	 * Configure ring entry size and number of items in the ring.
-	 */
-	KASSERT((sizeof(struct safe_ringentry) % sizeof(u_int32_t)) == 0,
-		("PE ring entry not 32-bit aligned!"));
-	dwords = sizeof(struct safe_ringentry) / sizeof(u_int32_t);
-	WRITE_REG(sc, SAFE_PE_RINGCFG,
-		(dwords << SAFE_PE_RINGCFG_OFFSET_S) | SAFE_MAX_NQUEUE);
-	WRITE_REG(sc, SAFE_PE_RINGPOLL, 0);	/* disable polling */
-
-	WRITE_REG(sc, SAFE_PE_GRNGBASE, sc->sc_spalloc.dma_paddr);
-	WRITE_REG(sc, SAFE_PE_SRNGBASE, sc->sc_dpalloc.dma_paddr);
-	WRITE_REG(sc, SAFE_PE_PARTSIZE,
-		(SAFE_TOTAL_DPART<<16) | SAFE_TOTAL_SPART);
-	/*
-	 * NB: destination particles are fixed size.  We use
-	 *     an mbuf cluster and require all results go to
-	 *     clusters or smaller.
-	 */
-	WRITE_REG(sc, SAFE_PE_PARTCFG, SAFE_MAX_DSIZE);
-
-	/* it's now safe to enable PE mode, do it */
-	WRITE_REG(sc, SAFE_PE_DMACFG, v | SAFE_PE_DMACFG_PEMODE);
-
-	/*
-	 * Configure hardware to use level-triggered interrupts and
-	 * to interrupt after each descriptor is processed.
-	 */
-	WRITE_REG(sc, SAFE_HI_CFG, SAFE_HI_CFG_LEVEL);
-	WRITE_REG(sc, SAFE_HI_DESC_CNT, 1);
-	WRITE_REG(sc, SAFE_HI_MASK, SAFE_INT_PE_DDONE | SAFE_INT_PE_ERROR);
-}
-
-/*
- * Init PCI registers
- */
-static void
-safe_init_pciregs(device_t dev)
-{
-}
-
-/*
- * Clean up after a chip crash.
- * It is assumed that the caller in splimp()
- */
-static void
-safe_cleanchip(struct safe_softc *sc)
-{
-
-	if (sc->sc_nqchip != 0) {
-		struct safe_ringentry *re = sc->sc_back;
-
-		while (re != sc->sc_front) {
-			if (re->re_desc.d_csr != 0)
-				safe_free_entry(sc, re);
-			if (++re == sc->sc_ringtop)
-				re = sc->sc_ring;
-		}
-		sc->sc_back = re;
-		sc->sc_nqchip = 0;
-	}
-}
-
-/*
- * free a safe_q
- * It is assumed that the caller is within splimp().
- */
-static int
-safe_free_entry(struct safe_softc *sc, struct safe_ringentry *re)
-{
-	struct cryptop *crp;
-
-	/*
-	 * Free header MCR
-	 */
-	if ((re->re_dst_m != NULL) && (re->re_src_m != re->re_dst_m))
-		m_freem(re->re_dst_m);
-
-	crp = (struct cryptop *)re->re_crp;
-
-	re->re_desc.d_csr = 0;
-
-	crp->crp_etype = EFAULT;
-	crypto_done(crp);
-	return(0);
-}
-
-/*
- * Routine to reset the chip and clean up.
- * It is assumed that the caller is in splimp()
- */
-static void
-safe_totalreset(struct safe_softc *sc)
-{
-	safe_reset_board(sc);
-	safe_init_board(sc);
-	safe_cleanchip(sc);
-}
-
-/*
- * Is the operand suitable aligned for direct DMA.  Each
- * segment must be aligned on a 32-bit boundary and all
- * but the last segment must be a multiple of 4 bytes.
- */
-static int
-safe_dmamap_aligned(const struct safe_operand *op)
-{
-	int i;
-
-	for (i = 0; i < op->nsegs; i++) {
-		if (op->segs[i].ds_addr & 3)
-			return (0);
-		if (i != (op->nsegs - 1) && (op->segs[i].ds_len & 3))
-			return (0);
-	}
-	return (1);
-}
-
-/*
- * Is the operand suitable for direct DMA as the destination
- * of an operation.  The hardware requires that each ``particle''
- * but the last in an operation result have the same size.  We
- * fix that size at SAFE_MAX_DSIZE bytes.  This routine returns
- * 0 if some segment is not a multiple of of this size, 1 if all
- * segments are exactly this size, or 2 if segments are at worst
- * a multple of this size.
- */
-static int
-safe_dmamap_uniform(const struct safe_operand *op)
-{
-	int result = 1;
-
-	if (op->nsegs > 0) {
-		int i;
-
-		for (i = 0; i < op->nsegs-1; i++) {
-			if (op->segs[i].ds_len % SAFE_MAX_DSIZE)
-				return (0);
-			if (op->segs[i].ds_len != SAFE_MAX_DSIZE)
-				result = 2;
-		}
-	}
-	return (result);
-}
-
-#ifdef SAFE_DEBUG
-static void
-safe_dump_dmastatus(struct safe_softc *sc, const char *tag)
-{
-	kprintf("%s: ENDIAN 0x%x SRC 0x%x DST 0x%x STAT 0x%x\n"
-		, tag
-		, READ_REG(sc, SAFE_DMA_ENDIAN)
-		, READ_REG(sc, SAFE_DMA_SRCADDR)
-		, READ_REG(sc, SAFE_DMA_DSTADDR)
-		, READ_REG(sc, SAFE_DMA_STAT)
-	);
-}
-
-static void
-safe_dump_intrstate(struct safe_softc *sc, const char *tag)
-{
-	kprintf("%s: HI_CFG 0x%x HI_MASK 0x%x HI_DESC_CNT 0x%x HU_STAT 0x%x HM_STAT 0x%x\n"
-		, tag
-		, READ_REG(sc, SAFE_HI_CFG)
-		, READ_REG(sc, SAFE_HI_MASK)
-		, READ_REG(sc, SAFE_HI_DESC_CNT)
-		, READ_REG(sc, SAFE_HU_STAT)
-		, READ_REG(sc, SAFE_HM_STAT)
-	);
-}
-
-static void
-safe_dump_ringstate(struct safe_softc *sc, const char *tag)
-{
-	u_int32_t estat = READ_REG(sc, SAFE_PE_ERNGSTAT);
-
-	/* NB: assume caller has lock on ring */
-	kprintf("%s: ERNGSTAT %x (next %u) back %lu front %lu\n",
-		tag,
-		estat, (estat >> SAFE_PE_ERNGSTAT_NEXT_S),
-		(unsigned long)(sc->sc_back - sc->sc_ring),
-		(unsigned long)(sc->sc_front - sc->sc_ring));
-}
-
-static void
-safe_dump_request(struct safe_softc *sc, const char* tag, struct safe_ringentry *re)
-{
-	int ix, nsegs;
-
-	ix = re - sc->sc_ring;
-	kprintf("%s: %p (%u): csr %x src %x dst %x sa %x len %x\n"
-		, tag
-		, re, ix
-		, re->re_desc.d_csr
-		, re->re_desc.d_src
-		, re->re_desc.d_dst
-		, re->re_desc.d_sa
-		, re->re_desc.d_len
-	);
-	if (re->re_src.nsegs > 1) {
-		ix = (re->re_desc.d_src - sc->sc_spalloc.dma_paddr) /
-			sizeof(struct safe_pdesc);
-		for (nsegs = re->re_src.nsegs; nsegs; nsegs--) {
-			kprintf(" spd[%u] %p: %p size %u flags %x"
-				, ix, &sc->sc_spring[ix]
-				, (caddr_t)(uintptr_t) sc->sc_spring[ix].pd_addr
-				, sc->sc_spring[ix].pd_size
-				, sc->sc_spring[ix].pd_flags
-			);
-			if (sc->sc_spring[ix].pd_size == 0)
-				kprintf(" (zero!)");
-			kprintf("\n");
-			if (++ix == SAFE_TOTAL_SPART)
-				ix = 0;
-		}
-	}
-	if (re->re_dst.nsegs > 1) {
-		ix = (re->re_desc.d_dst - sc->sc_dpalloc.dma_paddr) /
-			sizeof(struct safe_pdesc);
-		for (nsegs = re->re_dst.nsegs; nsegs; nsegs--) {
-			kprintf(" dpd[%u] %p: %p flags %x\n"
-				, ix, &sc->sc_dpring[ix]
-				, (caddr_t)(uintptr_t) sc->sc_dpring[ix].pd_addr
-				, sc->sc_dpring[ix].pd_flags
-			);
-			if (++ix == SAFE_TOTAL_DPART)
-				ix = 0;
-		}
-	}
-	kprintf("sa: cmd0 %08x cmd1 %08x staterec %x\n",
-		re->re_sa.sa_cmd0, re->re_sa.sa_cmd1, re->re_sa.sa_staterec);
-	kprintf("sa: key %x %x %x %x %x %x %x %x\n"
-		, re->re_sa.sa_key[0]
-		, re->re_sa.sa_key[1]
-		, re->re_sa.sa_key[2]
-		, re->re_sa.sa_key[3]
-		, re->re_sa.sa_key[4]
-		, re->re_sa.sa_key[5]
-		, re->re_sa.sa_key[6]
-		, re->re_sa.sa_key[7]
-	);
-	kprintf("sa: indigest %x %x %x %x %x\n"
-		, re->re_sa.sa_indigest[0]
-		, re->re_sa.sa_indigest[1]
-		, re->re_sa.sa_indigest[2]
-		, re->re_sa.sa_indigest[3]
-		, re->re_sa.sa_indigest[4]
-	);
-	kprintf("sa: outdigest %x %x %x %x %x\n"
-		, re->re_sa.sa_outdigest[0]
-		, re->re_sa.sa_outdigest[1]
-		, re->re_sa.sa_outdigest[2]
-		, re->re_sa.sa_outdigest[3]
-		, re->re_sa.sa_outdigest[4]
-	);
-	kprintf("sr: iv %x %x %x %x\n"
-		, re->re_sastate.sa_saved_iv[0]
-		, re->re_sastate.sa_saved_iv[1]
-		, re->re_sastate.sa_saved_iv[2]
-		, re->re_sastate.sa_saved_iv[3]
-	);
-	kprintf("sr: hashbc %u indigest %x %x %x %x %x\n"
-		, re->re_sastate.sa_saved_hashbc
-		, re->re_sastate.sa_saved_indigest[0]
-		, re->re_sastate.sa_saved_indigest[1]
-		, re->re_sastate.sa_saved_indigest[2]
-		, re->re_sastate.sa_saved_indigest[3]
-		, re->re_sastate.sa_saved_indigest[4]
-	);
-}
-
-static void
-safe_dump_ring(struct safe_softc *sc, const char *tag)
-{
-	lockmgr(&sc->sc_ringlock, LK_EXCLUSIVE);
-	kprintf("\nSafeNet Ring State:\n");
-	safe_dump_intrstate(sc, tag);
-	safe_dump_dmastatus(sc, tag);
-	safe_dump_ringstate(sc, tag);
-	if (sc->sc_nqchip) {
-		struct safe_ringentry *re = sc->sc_back;
-		do {
-			safe_dump_request(sc, tag, re);
-			if (++re == sc->sc_ringtop)
-				re = sc->sc_ring;
-		} while (re != sc->sc_front);
-	}
-	lockmgr(&sc->sc_ringlock, LK_RELEASE);
-}
-
-static int
-sysctl_hw_safe_dump(SYSCTL_HANDLER_ARGS)
-{
-	char dmode[64];
-	int error;
-
-	strncpy(dmode, "", sizeof(dmode) - 1);
-	dmode[sizeof(dmode) - 1] = '\0';
-	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
-
-	if (error == 0 && req->newptr != NULL) {
-		struct safe_softc *sc = safec;
-
-		if (!sc)
-			return EINVAL;
-		if (strncmp(dmode, "dma", 3) == 0)
-			safe_dump_dmastatus(sc, "safe0");
-		else if (strncmp(dmode, "int", 3) == 0)
-			safe_dump_intrstate(sc, "safe0");
-		else if (strncmp(dmode, "ring", 4) == 0)
-			safe_dump_ring(sc, "safe0");
-		else
-			return EINVAL;
-	}
-	return error;
-}
-SYSCTL_PROC(_hw_safe, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
-	0, 0, sysctl_hw_safe_dump, "A", "Dump driver state");
-#endif /* SAFE_DEBUG */
diff --git a/sys/dev/crypto/safe/safereg.h b/sys/dev/crypto/safe/safereg.h
deleted file mode 100644
index b5f9223f15..0000000000
--- a/sys/dev/crypto/safe/safereg.h
+++ /dev/null
@@ -1,412 +0,0 @@
-/*-
- * Copyright (c) 2003 Sam Leffler, Errno Consulting
- * Copyright (c) 2003 Global Technology Associates, Inc.
- * 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 AUTHOR 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 AUTHOR 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.
- *
- * $FreeBSD: src/sys/dev/safe/safereg.h,v 1.1 2003/07/21 21:46:07 sam Exp $
- */
-#ifndef _SAFE_SAFEREG_H_
-#define	_SAFE_SAFEREG_H_
-
-/*
- * Register definitions for SafeNet SafeXcel-1141 crypto device.
- * Definitions from revision 1.3 (Nov 6 2002) of the User's Manual.
- */
-
-#define BS_BAR			0x10	/* DMA base address register */
-#define	BS_TRDY_TIMEOUT		0x40	/* TRDY timeout */
-#define	BS_RETRY_TIMEOUT	0x41	/* DMA retry timeout */
-
-#define	PCI_VENDOR_SAFENET	0x16ae		/* SafeNet, Inc. */
-
-/* SafeNet */
-#define	PCI_PRODUCT_SAFEXCEL	0x1141		/* 1141 */
-
-#define	SAFE_PE_CSR		0x0000	/* Packet Enginge Ctrl/Status */
-#define	SAFE_PE_SRC		0x0004	/* Packet Engine Source */
-#define	SAFE_PE_DST		0x0008	/* Packet Engine Destination */
-#define	SAFE_PE_SA		0x000c	/* Packet Engine SA */
-#define	SAFE_PE_LEN		0x0010	/* Packet Engine Length */
-#define	SAFE_PE_DMACFG		0x0040	/* Packet Engine DMA Configuration */
-#define	SAFE_PE_DMASTAT		0x0044	/* Packet Engine DMA Status */
-#define	SAFE_PE_PDRBASE		0x0048	/* Packet Engine Descriptor Ring Base */
-#define	SAFE_PE_RDRBASE		0x004c	/* Packet Engine Result Ring Base */
-#define	SAFE_PE_RINGCFG		0x0050	/* Packet Engine Ring Configuration */
-#define	SAFE_PE_RINGPOLL	0x0054	/* Packet Engine Ring Poll */
-#define	SAFE_PE_IRNGSTAT	0x0058	/* Packet Engine Internal Ring Status */
-#define	SAFE_PE_ERNGSTAT	0x005c	/* Packet Engine External Ring Status */
-#define	SAFE_PE_IOTHRESH	0x0060	/* Packet Engine I/O Threshold */
-#define	SAFE_PE_GRNGBASE	0x0064	/* Packet Engine Gather Ring Base */
-#define	SAFE_PE_SRNGBASE	0x0068	/* Packet Engine Scatter Ring Base */
-#define	SAFE_PE_PARTSIZE	0x006c	/* Packet Engine Particlar Ring Size */
-#define	SAFE_PE_PARTCFG		0x0070	/* Packet Engine Particle Ring Config */
-#define	SAFE_CRYPTO_CTRL	0x0080	/* Crypto Control */
-#define	SAFE_DEVID		0x0084	/* Device ID */
-#define	SAFE_DEVINFO		0x0088	/* Device Info */
-#define	SAFE_HU_STAT		0x00a0	/* Host Unmasked Status */
-#define	SAFE_HM_STAT		0x00a4	/* Host Masked Status (read-only) */
-#define	SAFE_HI_CLR		0x00a4	/* Host Clear Interrupt (write-only) */
-#define	SAFE_HI_MASK		0x00a8	/* Host Mask Control */
-#define	SAFE_HI_CFG		0x00ac	/* Interrupt Configuration */
-#define	SAFE_HI_RD_DESCR	0x00b4	/* Force Descriptor Read */
-#define	SAFE_HI_DESC_CNT	0x00b8	/* Host Descriptor Done Count */
-#define	SAFE_DMA_ENDIAN		0x00c0	/* Master Endian Status */
-#define	SAFE_DMA_SRCADDR	0x00c4	/* DMA Source Address Status */
-#define	SAFE_DMA_DSTADDR	0x00c8	/* DMA Destination Address Status */
-#define	SAFE_DMA_STAT		0x00cc	/* DMA Current Status */
-#define	SAFE_DMA_CFG		0x00d4	/* DMA Configuration/Status */
-#define	SAFE_ENDIAN		0x00e0	/* Endian Configuration */
-#define	SAFE_PK_A_ADDR		0x0800	/* Public Key A Address */
-#define	SAFE_PK_B_ADDR		0x0804	/* Public Key B Address */
-#define	SAFE_PK_C_ADDR		0x0808	/* Public Key C Address */
-#define	SAFE_PK_D_ADDR		0x080c	/* Public Key D Address */
-#define	SAFE_PK_A_LEN		0x0810	/* Public Key A Length */
-#define	SAFE_PK_B_LEN		0x0814	/* Public Key B Length */
-#define	SAFE_PK_SHIFT		0x0818	/* Public Key Shift */
-#define	SAFE_PK_FUNC		0x081c	/* Public Key Function */
-#define	SAFE_RNG_OUT		0x0100	/* RNG Output */
-#define	SAFE_RNG_STAT		0x0104	/* RNG Status */
-#define	SAFE_RNG_CTRL		0x0108	/* RNG Control */
-#define	SAFE_RNG_A		0x010c	/* RNG A */
-#define	SAFE_RNG_B		0x0110	/* RNG B */
-#define	SAFE_RNG_X_LO		0x0114	/* RNG X [31:0] */
-#define	SAFE_RNG_X_MID		0x0118	/* RNG X [63:32] */
-#define	SAFE_RNG_X_HI		0x011c	/* RNG X [80:64] */
-#define	SAFE_RNG_X_CNTR		0x0120	/* RNG Counter */
-#define	SAFE_RNG_ALM_CNT	0x0124	/* RNG Alarm Count */
-#define	SAFE_RNG_CNFG		0x0128	/* RNG Configuration */
-#define	SAFE_RNG_LFSR1_LO	0x012c	/* RNG LFSR1 [31:0] */
-#define	SAFE_RNG_LFSR1_HI	0x0130	/* RNG LFSR1 [47:32] */
-#define	SAFE_RNG_LFSR2_LO	0x0134	/* RNG LFSR1 [31:0] */
-#define	SAFE_RNG_LFSR2_HI	0x0138	/* RNG LFSR1 [47:32] */
-
-#define	SAFE_PE_CSR_READY	0x00000001	/* ready for processing */
-#define	SAFE_PE_CSR_DONE	0x00000002	/* h/w completed processing */
-#define	SAFE_PE_CSR_LOADSA	0x00000004	/* load SA digests */
-#define	SAFE_PE_CSR_HASHFINAL	0x00000010	/* do hash pad & write result */
-#define	SAFE_PE_CSR_SABUSID	0x000000c0	/* bus id for SA */
-#define	SAFE_PE_CSR_SAPCI	0x00000040	/* PCI bus id for SA */
-#define	SAFE_PE_CSR_NXTHDR	0x0000ff00	/* next hdr value for IPsec */
-#define	SAFE_PE_CSR_FPAD	0x0000ff00	/* fixed pad for basic ops */
-#define	SAFE_PE_CSR_STATUS	0x00ff0000	/* operation result status */
-#define	SAFE_PE_CSR_AUTH_FAIL	0x00010000	/* ICV mismatch (inbound) */
-#define	SAFE_PE_CSR_PAD_FAIL	0x00020000	/* pad verify fail (inbound) */
-#define	SAFE_PE_CSR_SEQ_FAIL	0x00040000	/* sequence number (inbound) */
-#define	SAFE_PE_CSR_XERROR	0x00080000	/* extended error follows */
-#define	SAFE_PE_CSR_XECODE	0x00f00000	/* extended error code */
-#define	SAFE_PE_CSR_XECODE_S	20
-#define	SAFE_PE_CSR_XECODE_BADCMD	0	/* invalid command */
-#define	SAFE_PE_CSR_XECODE_BADALG	1	/* invalid algorithm */
-#define	SAFE_PE_CSR_XECODE_ALGDIS	2	/* algorithm disabled */
-#define	SAFE_PE_CSR_XECODE_ZEROLEN	3	/* zero packet length */
-#define	SAFE_PE_CSR_XECODE_DMAERR	4	/* bus DMA error */
-#define	SAFE_PE_CSR_XECODE_PIPEABORT	5	/* secondary bus DMA error */
-#define	SAFE_PE_CSR_XECODE_BADSPI	6	/* IPsec SPI mismatch */
-#define	SAFE_PE_CSR_XECODE_TIMEOUT	10	/* failsafe timeout */
-#define	SAFE_PE_CSR_PAD		0xff000000	/* ESP padding control/status */
-#define	SAFE_PE_CSR_PAD_MIN	0x00000000	/* minimum IPsec padding */
-#define	SAFE_PE_CSR_PAD_16	0x08000000	/* pad to 16-byte boundary */
-#define	SAFE_PE_CSR_PAD_32	0x10000000	/* pad to 32-byte boundary */
-#define	SAFE_PE_CSR_PAD_64	0x20000000	/* pad to 64-byte boundary */
-#define	SAFE_PE_CSR_PAD_128	0x40000000	/* pad to 128-byte boundary */
-#define	SAFE_PE_CSR_PAD_256	0x80000000	/* pad to 256-byte boundary */
-
-/*
- * Check the CSR to see if the PE has returned ownership to
- * the host.  Note that before processing a descriptor this
- * must be done followed by a check of the SAFE_PE_LEN register
- * status bits to avoid premature processing of a descriptor
- * on its way back to the host.
- */
-#define	SAFE_PE_CSR_IS_DONE(_csr) \
-    (((_csr) & (SAFE_PE_CSR_READY | SAFE_PE_CSR_DONE)) == SAFE_PE_CSR_DONE)
-
-#define	SAFE_PE_LEN_LENGTH	0x000fffff	/* total length (bytes) */
-#define	SAFE_PE_LEN_READY	0x00400000	/* ready for processing */
-#define	SAFE_PE_LEN_DONE	0x00800000	/* h/w completed processing */
-#define	SAFE_PE_LEN_BYPASS	0xff000000	/* bypass offset (bytes) */
-#define	SAFE_PE_LEN_BYPASS_S	24
-
-#define	SAFE_PE_LEN_IS_DONE(_len) \
-    (((_len) & (SAFE_PE_LEN_READY | SAFE_PE_LEN_DONE)) == SAFE_PE_LEN_DONE)
-
-/* NB: these apply to HU_STAT, HM_STAT, HI_CLR, and HI_MASK */
-#define	SAFE_INT_PE_CDONE	0x00000002	/* PE context done */
-#define	SAFE_INT_PE_DDONE	0x00000008	/* PE descriptor done */
-#define	SAFE_INT_PE_ERROR	0x00000010	/* PE error */
-#define	SAFE_INT_PE_ODONE	0x00000020	/* PE operation done */
-
-#define	SAFE_HI_CFG_PULSE	0x00000001	/* use pulse interrupt */
-#define	SAFE_HI_CFG_LEVEL	0x00000000	/* use level interrupt */
-#define	SAFE_HI_CFG_AUTOCLR	0x00000002	/* auto-clear pulse interrupt */
-
-#define	SAFE_ENDIAN_PASS	0x000000e4	/* straight pass-thru */
-#define	SAFE_ENDIAN_SWAB	0x0000001b	/* swap bytes in 32-bit word */
-
-#define	SAFE_PE_DMACFG_PERESET	0x00000001	/* reset packet engine */
-#define	SAFE_PE_DMACFG_PDRRESET	0x00000002	/* reset PDR counters/ptrs */
-#define	SAFE_PE_DMACFG_SGRESET	0x00000004	/* reset scatter/gather cache */
-#define	SAFE_PE_DMACFG_FSENA	0x00000008	/* enable failsafe reset */
-#define	SAFE_PE_DMACFG_PEMODE	0x00000100	/* packet engine mode */
-#define	SAFE_PE_DMACFG_SAPREC	0x00000200	/* SA precedes packet */
-#define	SAFE_PE_DMACFG_PKFOLL	0x00000400	/* packet follows descriptor */
-#define	SAFE_PE_DMACFG_GPRBID	0x00003000	/* gather particle ring busid */
-#define	SAFE_PE_DMACFG_GPRPCI	0x00001000	/* PCI gather particle ring */
-#define	SAFE_PE_DMACFG_SPRBID	0x0000c000	/* scatter part. ring busid */
-#define	SAFE_PE_DMACFG_SPRPCI	0x00004000	/* PCI scatter part. ring */
-#define	SAFE_PE_DMACFG_ESDESC	0x00010000	/* endian swap descriptors */
-#define	SAFE_PE_DMACFG_ESSA	0x00020000	/* endian swap SA data */
-#define	SAFE_PE_DMACFG_ESPACKET	0x00040000	/* endian swap packet data */
-#define	SAFE_PE_DMACFG_ESPDESC	0x00080000	/* endian swap particle desc. */
-#define	SAFE_PE_DMACFG_NOPDRUP	0x00100000	/* supp. PDR ownership update */
-#define	SAFE_PD_EDMACFG_PCIMODE	0x01000000	/* PCI target mode */
-
-#define	SAFE_PE_DMASTAT_PEIDONE	0x00000001	/* PE core input done */
-#define	SAFE_PE_DMASTAT_PEODONE	0x00000002	/* PE core output done */
-#define	SAFE_PE_DMASTAT_ENCDONE	0x00000004	/* encryption done */
-#define	SAFE_PE_DMASTAT_IHDONE	0x00000008	/* inner hash done */
-#define	SAFE_PE_DMASTAT_OHDONE	0x00000010	/* outer hash (HMAC) done */
-#define	SAFE_PE_DMASTAT_PADFLT	0x00000020	/* crypto pad fault */
-#define	SAFE_PE_DMASTAT_ICVFLT	0x00000040	/* ICV fault */
-#define	SAFE_PE_DMASTAT_SPIMIS	0x00000080	/* SPI mismatch */
-#define	SAFE_PE_DMASTAT_CRYPTO	0x00000100	/* crypto engine timeout */
-#define	SAFE_PE_DMASTAT_CQACT	0x00000200	/* command queue active */
-#define	SAFE_PE_DMASTAT_IRACT	0x00000400	/* input request active */
-#define	SAFE_PE_DMASTAT_ORACT	0x00000800	/* output request active */
-#define	SAFE_PE_DMASTAT_PEISIZE	0x003ff000	/* PE input size:32-bit words */
-#define	SAFE_PE_DMASTAT_PEOSIZE	0xffc00000	/* PE out. size:32-bit words */
-
-#define	SAFE_PE_RINGCFG_SIZE	0x000003ff	/* ring size (descriptors) */
-#define	SAFE_PE_RINGCFG_OFFSET	0xffff0000	/* offset btw desc's (dwords) */
-#define	SAFE_PE_RINGCFG_OFFSET_S	16
-
-#define	SAFE_PE_RINGPOLL_POLL	0x00000fff	/* polling frequency/divisor */
-#define	SAFE_PE_RINGPOLL_RETRY	0x03ff0000	/* polling frequency/divisor */
-#define	SAFE_PE_RINGPOLL_CONT	0x80000000	/* continuously poll */
-
-#define	SAFE_PE_IRNGSTAT_CQAVAIL 0x00000001	/* command queue available */
-
-#define	SAFE_PE_ERNGSTAT_NEXT	0x03ff0000	/* index of next packet desc. */
-#define	SAFE_PE_ERNGSTAT_NEXT_S	16
-
-#define	SAFE_PE_IOTHRESH_INPUT	0x000003ff	/* input threshold (dwords) */
-#define	SAFE_PE_IOTHRESH_OUTPUT	0x03ff0000	/* output threshold (dwords) */
-
-#define	SAFE_PE_PARTCFG_SIZE	0x0000ffff	/* scatter particle size */
-#define	SAFE_PE_PARTCFG_GBURST	0x00030000	/* gather particle burst */
-#define	SAFE_PE_PARTCFG_GBURST_2	0x00000000
-#define	SAFE_PE_PARTCFG_GBURST_4	0x00010000
-#define	SAFE_PE_PARTCFG_GBURST_8	0x00020000
-#define	SAFE_PE_PARTCFG_GBURST_16	0x00030000
-#define	SAFE_PE_PARTCFG_SBURST	0x000c0000	/* scatter particle burst */
-#define	SAFE_PE_PARTCFG_SBURST_2	0x00000000
-#define	SAFE_PE_PARTCFG_SBURST_4	0x00040000
-#define	SAFE_PE_PARTCFG_SBURST_8	0x00080000
-#define	SAFE_PE_PARTCFG_SBURST_16	0x000c0000
-
-#define	SAFE_PE_PARTSIZE_SCAT	0xffff0000	/* scatter particle ring size */
-#define	SAFE_PE_PARTSIZE_GATH	0x0000ffff	/* gather particle ring size */
-
-#define	SAFE_CRYPTO_CTRL_3DES	0x00000001	/* enable 3DES support */
-#define	SAFE_CRYPTO_CTRL_PKEY	0x00010000	/* enable public key support */
-#define	SAFE_CRYPTO_CTRL_RNG	0x00020000	/* enable RNG support */
-
-#define	SAFE_DEVINFO_REV_MIN	0x0000000f	/* minor rev for chip */
-#define	SAFE_DEVINFO_REV_MAJ	0x000000f0	/* major rev for chip */
-#define	SAFE_DEVINFO_REV_MAJ_S	4
-#define	SAFE_DEVINFO_DES	0x00000100	/* DES/3DES support present */
-#define	SAFE_DEVINFO_ARC4	0x00000200	/* ARC4 support present */
-#define	SAFE_DEVINFO_AES	0x00000400	/* AES support present */
-#define	SAFE_DEVINFO_MD5	0x00001000	/* MD5 support present */
-#define	SAFE_DEVINFO_SHA1	0x00002000	/* SHA-1 support present */
-#define	SAFE_DEVINFO_RIPEMD	0x00004000	/* RIPEMD support present */
-#define	SAFE_DEVINFO_DEFLATE	0x00010000	/* Deflate support present */
-#define	SAFE_DEVINFO_SARAM	0x00100000	/* on-chip SA RAM present */
-#define	SAFE_DEVINFO_EMIBUS	0x00200000	/* EMI bus present */
-#define	SAFE_DEVINFO_PKEY	0x00400000	/* public key support present */
-#define	SAFE_DEVINFO_RNG	0x00800000	/* RNG present */
-
-#define	SAFE_REV(_maj, _min)	(((_maj) << SAFE_DEVINFO_REV_MAJ_S) | (_min))
-#define	SAFE_REV_MAJ(_chiprev) \
-	(((_chiprev) & SAFE_DEVINFO_REV_MAJ) >> SAFE_DEVINFO_REV_MAJ_S)
-#define	SAFE_REV_MIN(_chiprev)	((_chiprev) & SAFE_DEVINFO_REV_MIN)
-
-#define	SAFE_PK_FUNC_MULT	0x00000001	/* Multiply function */
-#define	SAFE_PK_FUNC_SQUARE	0x00000004	/* Square function */
-#define	SAFE_PK_FUNC_ADD	0x00000010	/* Add function */
-#define	SAFE_PK_FUNC_SUB	0x00000020	/* Subtract function */
-#define	SAFE_PK_FUNC_LSHIFT	0x00000040	/* Left-shift function */
-#define	SAFE_PK_FUNC_RSHIFT	0x00000080	/* Right-shift function */
-#define	SAFE_PK_FUNC_DIV	0x00000100	/* Divide function */
-#define	SAFE_PK_FUNC_CMP	0x00000400	/* Compare function */
-#define	SAFE_PK_FUNC_COPY	0x00000800	/* Copy function */
-#define	SAFE_PK_FUNC_EXP16	0x00002000	/* Exponentiate (4-bit ACT) */
-#define	SAFE_PK_FUNC_EXP4	0x00004000	/* Exponentiate (2-bit ACT) */
-
-#define	SAFE_RNG_STAT_BUSY	0x00000001	/* busy, data not valid */
-
-#define	SAFE_RNG_CTRL_PRE_LFSR	0x00000001	/* enable output pre-LFSR */
-#define	SAFE_RNG_CTRL_TST_MODE	0x00000002	/* enable test mode */
-#define	SAFE_RNG_CTRL_TST_RUN	0x00000004	/* start test state machine */
-#define	SAFE_RNG_CTRL_ENA_RING1	0x00000008	/* test entropy oscillator #1 */
-#define	SAFE_RNG_CTRL_ENA_RING2	0x00000010	/* test entropy oscillator #2 */
-#define	SAFE_RNG_CTRL_DIS_ALARM	0x00000020	/* disable RNG alarm reports */
-#define	SAFE_RNG_CTRL_TST_CLOCK	0x00000040	/* enable test clock */
-#define	SAFE_RNG_CTRL_SHORTEN	0x00000080	/* shorten state timers */
-#define	SAFE_RNG_CTRL_TST_ALARM	0x00000100	/* simulate alarm state */
-#define	SAFE_RNG_CTRL_RST_LFSR	0x00000200	/* reset LFSR */
-
-/*
- * Packet engine descriptor.  Note that d_csr is a copy of the
- * SAFE_PE_CSR register and all definitions apply, and d_len
- * is a copy of the SAFE_PE_LEN register and all definitions apply.
- * d_src and d_len may point directly to contiguous data or to a
- * list of ``particle descriptors'' when using scatter/gather i/o.
- */
-struct safe_desc {
-	u_int32_t	d_csr;			/* per-packet control/status */
-	u_int32_t	d_src;			/* source address */
-	u_int32_t	d_dst;			/* destination address */
-	u_int32_t	d_sa;			/* SA address */
-	u_int32_t	d_len;			/* length, bypass, status */
-};
-
-/*
- * Scatter/Gather particle descriptor.
- *
- * NB: scatter descriptors do not specify a size; this is fixed
- *     by the setting of the SAFE_PE_PARTCFG register.
- */
-struct safe_pdesc {
-	u_int32_t	pd_addr;		/* particle address */
-	u_int16_t	pd_flags;		/* control word */
-	u_int16_t	pd_size;		/* particle size (bytes) */
-};
-
-#define	SAFE_PD_READY	0x0001			/* ready for processing */
-#define	SAFE_PD_DONE	0x0002			/* h/w completed processing */
-
-/*
- * Security Association (SA) Record (Rev 1).  One of these is
- * required for each operation processed by the packet engine.
- */
-struct safe_sarec {
-	u_int32_t	sa_cmd0;
-	u_int32_t	sa_cmd1;
-	u_int32_t	sa_resv0;
-	u_int32_t	sa_resv1;
-	u_int32_t	sa_key[8];		/* DES/3DES/AES key */
-	u_int32_t	sa_indigest[5];		/* inner digest */
-	u_int32_t	sa_outdigest[5];	/* outer digest */
-	u_int32_t	sa_spi;			/* SPI */
-	u_int32_t	sa_seqnum;		/* sequence number */
-	u_int32_t	sa_seqmask[2];		/* sequence number mask */
-	u_int32_t	sa_resv2;
-	u_int32_t	sa_staterec;		/* address of state record */
-	u_int32_t	sa_resv3[2];
-	u_int32_t	sa_samgmt0;		/* SA management field 0 */
-	u_int32_t	sa_samgmt1;		/* SA management field 0 */
-};
-
-#define	SAFE_SA_CMD0_OP		0x00000007	/* operation code */
-#define	SAFE_SA_CMD0_OP_CRYPT	0x00000000	/* encrypt/decrypt (basic) */
-#define	SAFE_SA_CMD0_OP_BOTH	0x00000001	/* encrypt-hash/hash-decrypto */
-#define	SAFE_SA_CMD0_OP_HASH	0x00000003	/* hash (outbound-only) */
-#define	SAFE_SA_CMD0_OP_ESP	0x00000000	/* ESP in/out (proto) */
-#define	SAFE_SA_CMD0_OP_AH	0x00000001	/* AH in/out (proto) */
-#define	SAFE_SA_CMD0_INBOUND	0x00000008	/* inbound operation */
-#define	SAFE_SA_CMD0_OUTBOUND	0x00000000	/* outbound operation */
-#define	SAFE_SA_CMD0_GROUP	0x00000030	/* operation group */
-#define	SAFE_SA_CMD0_BASIC	0x00000000	/* basic operation */
-#define	SAFE_SA_CMD0_PROTO	0x00000010	/* protocol/packet operation */
-#define	SAFE_SA_CMD0_BUNDLE	0x00000020	/* bundled operation (resvd) */
-#define	SAFE_SA_CMD0_PAD	0x000000c0	/* crypto pad method */
-#define	SAFE_SA_CMD0_PAD_IPSEC	0x00000000	/* IPsec padding */
-#define	SAFE_SA_CMD0_PAD_PKCS7	0x00000040	/* PKCS#7 padding */
-#define	SAFE_SA_CMD0_PAD_CONS	0x00000080	/* constant padding */
-#define	SAFE_SA_CMD0_PAD_ZERO	0x000000c0	/* zero padding */
-#define	SAFE_SA_CMD0_CRYPT_ALG	0x00000f00	/* symmetric crypto algorithm */
-#define	SAFE_SA_CMD0_DES	0x00000000	/* DES crypto algorithm */
-#define	SAFE_SA_CMD0_3DES	0x00000100	/* 3DES crypto algorithm */
-#define	SAFE_SA_CMD0_AES	0x00000300	/* AES crypto algorithm */
-#define	SAFE_SA_CMD0_CRYPT_NULL	0x00000f00	/* null crypto algorithm */
-#define	SAFE_SA_CMD0_HASH_ALG	0x0000f000	/* hash algorithm */
-#define	SAFE_SA_CMD0_MD5	0x00000000	/* MD5 hash algorithm */
-#define	SAFE_SA_CMD0_SHA1	0x00001000	/* SHA-1 hash algorithm */
-#define	SAFE_SA_CMD0_HASH_NULL	0x0000f000	/* null hash algorithm */
-#define	SAFE_SA_CMD0_HDR_PROC	0x00080000	/* header processing */
-#define	SAFE_SA_CMD0_IBUSID	0x00300000	/* input bus id */
-#define	SAFE_SA_CMD0_IPCI	0x00100000	/* PCI input bus id */
-#define	SAFE_SA_CMD0_OBUSID	0x00c00000	/* output bus id */
-#define	SAFE_SA_CMD0_OPCI	0x00400000	/* PCI output bus id */
-#define	SAFE_SA_CMD0_IVLD	0x03000000	/* IV loading */
-#define	SAFE_SA_CMD0_IVLD_NONE	0x00000000	/* IV no load (reuse) */
-#define	SAFE_SA_CMD0_IVLD_IBUF	0x01000000	/* IV load from input buffer */
-#define	SAFE_SA_CMD0_IVLD_STATE	0x02000000	/* IV load from state */
-#define	SAFE_SA_CMD0_HSLD	0x0c000000	/* hash state loading */
-#define	SAFE_SA_CMD0_HSLD_SA	0x00000000	/* hash state load from SA */
-#define	SAFE_SA_CMD0_HSLD_STATE	0x08000000	/* hash state load from state */
-#define	SAFE_SA_CMD0_HSLD_NONE	0x0c000000	/* hash state no load */
-#define	SAFE_SA_CMD0_SAVEIV	0x10000000	/* save IV */
-#define	SAFE_SA_CMD0_SAVEHASH	0x20000000	/* save hash state */
-#define	SAFE_SA_CMD0_IGATHER	0x40000000	/* input gather */
-#define	SAFE_SA_CMD0_OSCATTER	0x80000000	/* output scatter */
-
-#define	SAFE_SA_CMD1_HDRCOPY	0x00000002	/* copy header to output */
-#define	SAFE_SA_CMD1_PAYCOPY	0x00000004	/* copy payload to output */
-#define	SAFE_SA_CMD1_PADCOPY	0x00000008	/* copy pad to output */
-#define	SAFE_SA_CMD1_IPV4	0x00000000	/* IPv4 protocol */
-#define	SAFE_SA_CMD1_IPV6	0x00000010	/* IPv6 protocol */
-#define	SAFE_SA_CMD1_MUTABLE	0x00000020	/* mutable bit processing */
-#define	SAFE_SA_CMD1_SRBUSID	0x000000c0	/* state record bus id */
-#define	SAFE_SA_CMD1_SRPCI	0x00000040	/* state record from PCI */
-#define	SAFE_SA_CMD1_CRMODE	0x00000300	/* crypto mode */
-#define	SAFE_SA_CMD1_ECB	0x00000000	/* ECB crypto mode */
-#define	SAFE_SA_CMD1_CBC	0x00000100	/* CBC crypto mode */
-#define	SAFE_SA_CMD1_OFB	0x00000200	/* OFB crypto mode */
-#define	SAFE_SA_CMD1_CFB	0x00000300	/* CFB crypto mode */
-#define	SAFE_SA_CMD1_CRFEEDBACK	0x00000c00	/* crypto feedback mode */
-#define	SAFE_SA_CMD1_64BIT	0x00000000	/* 64-bit crypto feedback */
-#define	SAFE_SA_CMD1_8BIT	0x00000400	/* 8-bit crypto feedback */
-#define	SAFE_SA_CMD1_1BIT	0x00000800	/* 1-bit crypto feedback */
-#define	SAFE_SA_CMD1_128BIT	0x00000c00	/* 128-bit crypto feedback */
-#define	SAFE_SA_CMD1_OPTIONS	0x00001000	/* HMAC/options mutable bit */
-#define	SAFE_SA_CMD1_HMAC	SAFE_SA_CMD1_OPTIONS
-#define	SAFE_SA_CMD1_SAREV1	0x00008000	/* SA Revision 1 */
-#define	SAFE_SA_CMD1_OFFSET	0x00ff0000	/* hash/crypto offset(dwords) */
-#define	SAFE_SA_CMD1_OFFSET_S	16
-#define	SAFE_SA_CMD1_AESKEYLEN	0x0f000000	/* AES key length */
-#define	SAFE_SA_CMD1_AES128	0x02000000	/* 128-bit AES key */
-#define	SAFE_SA_CMD1_AES192	0x03000000	/* 192-bit AES key */
-#define	SAFE_SA_CMD1_AES256	0x04000000	/* 256-bit AES key */
-
-/*
- * Security Associate State Record (Rev 1).
- */
-struct safe_sastate {
-	u_int32_t	sa_saved_iv[4];		/* saved IV (DES/3DES/AES) */
-	u_int32_t	sa_saved_hashbc;	/* saved hash byte count */
-	u_int32_t	sa_saved_indigest[5];	/* saved inner digest */
-};
-#endif /* _SAFE_SAFEREG_H_ */
diff --git a/sys/dev/crypto/safe/safevar.h b/sys/dev/crypto/safe/safevar.h
deleted file mode 100644
index 39ca3f17e3..0000000000
--- a/sys/dev/crypto/safe/safevar.h
+++ /dev/null
@@ -1,219 +0,0 @@
-/*-
- * Copyright (c) 2003 Sam Leffler, Errno Consulting
- * Copyright (c) 2003 Global Technology Associates, Inc.
- * 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 AUTHOR 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 AUTHOR 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.
- *
- * $FreeBSD: src/sys/dev/safe/safevar.h,v 1.2 2006/05/17 18:34:26 pjd Exp $
- */
-#ifndef _SAFE_SAFEVAR_H_
-#define	_SAFE_SAFEVAR_H_
-
-/* Maximum queue length */
-#ifndef SAFE_MAX_NQUEUE
-#define SAFE_MAX_NQUEUE	60
-#endif
-
-#define	SAFE_MAX_PART		64	/* Maximum scatter/gather depth */
-#define	SAFE_DMA_BOUNDARY	0	/* No boundary for source DMA ops */
-#define	SAFE_MAX_DSIZE		MCLBYTES /* Fixed scatter particle size */
-#define	SAFE_MAX_SSIZE		0x0ffff	/* Maximum gather particle size */
-#define	SAFE_MAX_DMA		0xfffff	/* Maximum PE operand size (20 bits) */
-/* total src+dst particle descriptors */
-#define	SAFE_TOTAL_DPART	(SAFE_MAX_NQUEUE * SAFE_MAX_PART)
-#define	SAFE_TOTAL_SPART	(SAFE_MAX_NQUEUE * SAFE_MAX_PART)
-
-#define	SAFE_RNG_MAXBUFSIZ	128	/* 32-bit words */
-
-#define	SAFE_CARD(sid)		(((sid) & 0xf0000000) >> 28)
-#define	SAFE_SESSION(sid)	( (sid) & 0x0fffffff)
-#define	SAFE_SID(crd, sesn)	(((crd) << 28) | ((sesn) & 0x0fffffff))
-
-#define SAFE_DEF_RTY		0xff	/* PCI Retry Timeout */
-#define SAFE_DEF_TOUT		0xff	/* PCI TRDY Timeout */
-#define SAFE_DEF_CACHELINE	0x01	/* Cache Line setting */
-
-#ifdef _KERNEL
-/*
- * State associated with the allocation of each chunk
- * of memory setup for DMA.
- */
-struct safe_dma_alloc {
-	u_int32_t		dma_paddr;	/* physical address */
-	caddr_t			dma_vaddr;	/* virtual address */
-	bus_dma_tag_t		dma_tag;	/* bus dma tag used */
-	bus_dmamap_t		dma_map;	/* associated map */
-	bus_dma_segment_t	dma_seg;
-	bus_size_t		dma_size;	/* mapped memory size (bytes) */
-	int			dma_nseg;	/* number of segments */
-};
-
-/*
- * Cryptographic operand state.  One of these exists for each
- * source and destination operand passed in from the crypto
- * subsystem.  When possible source and destination operands
- * refer to the same memory.  More often they are distinct.
- * We track the virtual address of each operand as well as
- * where each is mapped for DMA.
- */
-struct safe_operand {
-	union {
-		struct mbuf *m;
-		struct uio *io;
-	} u;
-	bus_dmamap_t		map;
-	bus_size_t		mapsize;
-	int			nsegs;
-	bus_dma_segment_t	segs[SAFE_MAX_PART];
-};
-
-/*
- * Packet engine ring entry and cryptographic operation state.
- * The packet engine requires a ring of descriptors that contain
- * pointers to various cryptographic state.  However the ring
- * configuration register allows you to specify an arbitrary size
- * for ring entries.  We use this feature to collect most of the
- * state for each cryptographic request into one spot.  Other than
- * ring entries only the ``particle descriptors'' (scatter/gather
- * lists) and the actual operand data are kept separate.  The
- * particle descriptors must also be organized in rings.  The
- * operand data can be located aribtrarily (modulo alignment constraints).
- *
- * Note that the descriptor ring is mapped onto the PCI bus so
- * the hardware can DMA data.  This means the entire ring must be
- * contiguous.
- */
-struct safe_ringentry {
-	struct safe_desc	re_desc;	/* command descriptor */
-	struct safe_sarec	re_sa;		/* SA record */
-	struct safe_sastate	re_sastate;	/* SA state record */
-	struct cryptop		*re_crp;	/* crypto operation */
-
-	struct safe_operand	re_src;		/* source operand */
-	struct safe_operand	re_dst;		/* destination operand */
-
-	int			re_sesn;	/* crypto session ID */
-	int			re_flags;
-#define	SAFE_QFLAGS_COPYOUTIV	0x1		/* copy back on completion */
-#define	SAFE_QFLAGS_COPYOUTICV	0x2		/* copy back on completion */
-};
-
-#define	re_src_m	re_src.u.m
-#define	re_src_io	re_src.u.io
-#define	re_src_map	re_src.map
-#define	re_src_nsegs	re_src.nsegs
-#define	re_src_segs	re_src.segs
-#define	re_src_mapsize	re_src.mapsize
-
-#define	re_dst_m	re_dst.u.m
-#define	re_dst_io	re_dst.u.io
-#define	re_dst_map	re_dst.map
-#define	re_dst_nsegs	re_dst.nsegs
-#define	re_dst_segs	re_dst.segs
-#define	re_dst_mapsize	re_dst.mapsize
-
-struct rndstate_test;
-
-struct safe_session {
-	u_int32_t	ses_used;
-	u_int32_t	ses_klen;		/* key length in bits */
-	u_int32_t	ses_key[8];		/* DES/3DES/AES key */
-	u_int32_t	ses_mlen;		/* hmac length in bytes */
-	u_int32_t	ses_hminner[5];		/* hmac inner state */
-	u_int32_t	ses_hmouter[5];		/* hmac outer state */
-	u_int32_t	ses_iv[4];		/* DES/3DES/AES iv */
-};
-
-struct safe_softc {
-	device_t		sc_dev;		/* device backpointer */
-	struct resource		*sc_irq;
-	void			*sc_ih;		/* interrupt handler cookie */
-	bus_space_handle_t	sc_sh;		/* memory handle */
-	bus_space_tag_t		sc_st;		/* memory tag */
-	struct resource		*sc_sr;		/* memory resource */
-	bus_dma_tag_t		sc_srcdmat;	/* source dma tag */
-	bus_dma_tag_t		sc_dstdmat;	/* destination dma tag */
-	u_int			sc_chiprev;	/* major/minor chip revision */
-	int			sc_flags;	/* device specific flags */
-#define	SAFE_FLAGS_KEY		0x01		/* has key accelerator */
-#define	SAFE_FLAGS_RNG		0x02		/* hardware rng */
-	int			sc_suspended;
-	int			sc_needwakeup;	/* notify crypto layer */
-	int32_t			sc_cid;		/* crypto tag */
-	struct safe_dma_alloc	sc_ringalloc;	/* PE ring allocation state */
-	struct safe_ringentry	*sc_ring;	/* PE ring */
-	struct safe_ringentry	*sc_ringtop;	/* PE ring top */
-	struct safe_ringentry	*sc_front;	/* next free entry */
-	struct safe_ringentry	*sc_back;	/* next pending entry */
-	int			sc_nqchip;	/* # passed to chip */
-	struct lock		sc_ringlock;	/* PE ring lock */
-	struct safe_pdesc	*sc_spring;	/* src particle ring */
-	struct safe_pdesc	*sc_springtop;	/* src particle ring top */
-	struct safe_pdesc	*sc_spfree;	/* next free src particle */
-	struct safe_dma_alloc	sc_spalloc;	/* src particle ring state */
-	struct safe_pdesc	*sc_dpring;	/* dest particle ring */
-	struct safe_pdesc	*sc_dpringtop;	/* dest particle ring top */
-	struct safe_pdesc	*sc_dpfree;	/* next free dest particle */
-	struct safe_dma_alloc	sc_dpalloc;	/* dst particle ring state */
-	int			sc_nsessions;	/* # of sessions */
-	struct safe_session	*sc_sessions;	/* sessions */
-
-	struct callout		sc_rngto;	/* rng timeout */
-	struct rndtest_state	*sc_rndtest;	/* RNG test state */
-	void			(*sc_harvest)(struct rndtest_state *,
-					void *, u_int);
-};
-#endif /* _KERNEL */
-
-struct safe_stats {
-	u_int64_t st_ibytes;
-	u_int64_t st_obytes;
-	u_int32_t st_ipackets;
-	u_int32_t st_opackets;
-	u_int32_t st_invalid;		/* invalid argument */
-	u_int32_t st_badsession;	/* invalid session id */
-	u_int32_t st_badflags;		/* flags indicate !(mbuf | uio) */
-	u_int32_t st_nodesc;		/* op submitted w/o descriptors */
-	u_int32_t st_badalg;		/* unsupported algorithm */
-	u_int32_t st_ringfull;		/* PE descriptor ring full */
-	u_int32_t st_peoperr;		/* PE marked error */
-	u_int32_t st_dmaerr;		/* PE DMA error */
-	u_int32_t st_bypasstoobig;	/* bypass > 96 bytes */
-	u_int32_t st_skipmismatch;	/* enc part begins before auth part */
-	u_int32_t st_lenmismatch;	/* enc length different auth length */
-	u_int32_t st_coffmisaligned;	/* crypto offset not 32-bit aligned */
-	u_int32_t st_cofftoobig;	/* crypto offset > 255 words */
-	u_int32_t st_iovmisaligned;	/* iov op not aligned */
-	u_int32_t st_iovnotuniform;	/* iov op not suitable */
-	u_int32_t st_unaligned;		/* unaligned src caused copy */
-	u_int32_t st_notuniform;	/* non-uniform src caused copy */
-	u_int32_t st_nomap;		/* bus_dmamap_create failed */
-	u_int32_t st_noload;		/* bus_dmamap_load_* failed */
-	u_int32_t st_nombuf;		/* MGET* failed */
-	u_int32_t st_nomcl;		/* MCLGET* failed */
-	u_int32_t st_maxqchip;		/* max mcr1 ops out for processing */
-	u_int32_t st_rng;		/* RNG requests */
-	u_int32_t st_rngalarm;		/* RNG alarm requests */
-	u_int32_t st_noicvcopy;		/* ICV data copies suppressed */
-};
-#endif /* _SAFE_SAFEVAR_H_ */
diff --git a/sys/dev/crypto/ubsec/Makefile b/sys/dev/crypto/ubsec/Makefile
deleted file mode 100644
index dcfb5a84db..0000000000
--- a/sys/dev/crypto/ubsec/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-# $FreeBSD: src/sys/modules/ubsec/Makefile,v 1.2.2.2 2003/06/05 04:13:48 sam Exp $
-
-KMOD	= ubsec
-SRCS	= ubsec.c opt_ubsec.h
-SRCS   += device_if.h bus_if.h pci_if.h
-SRCS   += cryptodev_if.h
-
-.if !defined(KERNBUILDDIR)
-opt_ubsec.h:
-	echo "#define UBSEC_DEBUG 1" > ${.TARGET}
-.endif
-
-.include <bsd.kmod.mk>
diff --git a/sys/dev/crypto/ubsec/ubsec.c b/sys/dev/crypto/ubsec/ubsec.c
deleted file mode 100644
index a4acf7a192..0000000000
--- a/sys/dev/crypto/ubsec/ubsec.c
+++ /dev/null
@@ -1,2856 +0,0 @@
-/* $FreeBSD: src/sys/dev/ubsec/ubsec.c,v 1.6.2.12 2003/06/04 17:56:59 sam Exp $ */
-/*	$OpenBSD: ubsec.c,v 1.115 2002/09/24 18:33:26 jason Exp $	*/
-
-/*
- * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
- * Copyright (c) 2000 Theo de Raadt (deraadt@openbsd.org)
- * Copyright (c) 2001 Patrik Lindergren (patrik@ipunplugged.com)
- * 
- * 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.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by Jason L. Wright
- * 4. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-
-/*
- * uBsec 5[56]01, 58xx hardware crypto accelerator
- */
-
-#include "opt_ubsec.h"
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/proc.h>
-#include <sys/errno.h>
-#include <sys/malloc.h>
-#include <sys/kernel.h>
-#include <sys/mbuf.h>
-#include <sys/sysctl.h>
-#include <sys/endian.h>
-#include <sys/bus.h>
-#include <sys/rman.h>
-#include <sys/md5.h>
-#include <sys/random.h>
-#include <sys/thread2.h>
-
-#include <vm/vm.h>
-#include <vm/pmap.h>
-
-#include <machine/clock.h>
-
-#include <crypto/sha1.h>
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/cryptosoft.h>
-
-#include "cryptodev_if.h"
-
-#include <bus/pci/pcivar.h>
-#include <bus/pci/pcireg.h>
-
-/* grr, #defines for gratuitous incompatibility in queue.h */
-#define	SIMPLEQ_HEAD		STAILQ_HEAD
-#define	SIMPLEQ_ENTRY		STAILQ_ENTRY
-#define	SIMPLEQ_INIT		STAILQ_INIT
-#define	SIMPLEQ_INSERT_TAIL	STAILQ_INSERT_TAIL
-#define	SIMPLEQ_EMPTY		STAILQ_EMPTY
-#define	SIMPLEQ_FIRST		STAILQ_FIRST
-#define	SIMPLEQ_REMOVE_HEAD	STAILQ_REMOVE_HEAD
-#define	SIMPLEQ_FOREACH		STAILQ_FOREACH
-/* ditto for endian.h */
-#define	letoh16(x)		le16toh(x)
-#define	letoh32(x)		le32toh(x)
-
-#ifdef UBSEC_RNDTEST
-#include "../rndtest/rndtest.h"
-#endif
-#include "ubsecreg.h"
-#include "ubsecvar.h"
-
-/*
- * Prototypes and count for the pci_device structure
- */
-static	int ubsec_probe(device_t);
-static	int ubsec_attach(device_t);
-static	int ubsec_detach(device_t);
-static	int ubsec_suspend(device_t);
-static	int ubsec_resume(device_t);
-static	void ubsec_shutdown(device_t);
-static	void ubsec_intr(void *);
-static	int ubsec_newsession(device_t, u_int32_t *, struct cryptoini *);
-static	int ubsec_freesession(device_t, u_int64_t);
-static	int ubsec_process(device_t, struct cryptop *, int);
-static	void ubsec_callback(struct ubsec_softc *, struct ubsec_q *);
-static	void ubsec_feed(struct ubsec_softc *);
-static	void ubsec_mcopy(struct mbuf *, struct mbuf *, int, int);
-static	void ubsec_callback2(struct ubsec_softc *, struct ubsec_q2 *);
-static	int ubsec_feed2(struct ubsec_softc *);
-static	void ubsec_rng(void *);
-static	int ubsec_dma_malloc(struct ubsec_softc *, bus_size_t,
-			     struct ubsec_dma_alloc *, int);
-#define	ubsec_dma_sync(_dma, _flags) \
-	bus_dmamap_sync((_dma)->dma_tag, (_dma)->dma_map, (_flags))
-static	void ubsec_dma_free(struct ubsec_softc *, struct ubsec_dma_alloc *);
-static	int ubsec_dmamap_aligned(struct ubsec_operand *op);
-
-static	void ubsec_reset_board(struct ubsec_softc *sc);
-static	void ubsec_init_board(struct ubsec_softc *sc);
-static	void ubsec_init_pciregs(device_t dev);
-static	void ubsec_totalreset(struct ubsec_softc *sc);
-
-static	int ubsec_free_q(struct ubsec_softc *sc, struct ubsec_q *q);
-
-static	int ubsec_kprocess(device_t, struct cryptkop *, int);
-static	int ubsec_kprocess_modexp_hw(struct ubsec_softc *, struct cryptkop *, int);
-static	int ubsec_kprocess_modexp_sw(struct ubsec_softc *, struct cryptkop *, int);
-static	int ubsec_kprocess_rsapriv(struct ubsec_softc *, struct cryptkop *, int);
-static	void ubsec_kfree(struct ubsec_softc *, struct ubsec_q2 *);
-static	int ubsec_ksigbits(struct crparam *);
-static	void ubsec_kshift_r(u_int, u_int8_t *, u_int, u_int8_t *, u_int);
-static	void ubsec_kshift_l(u_int, u_int8_t *, u_int, u_int8_t *, u_int);
-
-
-static device_method_t ubsec_methods[] = {
-	/* Device interface */
-	DEVMETHOD(device_probe,		ubsec_probe),
-	DEVMETHOD(device_attach,	ubsec_attach),
-	DEVMETHOD(device_detach,	ubsec_detach),
-	DEVMETHOD(device_suspend,	ubsec_suspend),
-	DEVMETHOD(device_resume,	ubsec_resume),
-	DEVMETHOD(device_shutdown,	ubsec_shutdown),
-
-	/* bus interface */
-	DEVMETHOD(bus_print_child,	bus_generic_print_child),
-	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
-
-	/* crypto device methods */
-	DEVMETHOD(cryptodev_newsession,	ubsec_newsession),
-	DEVMETHOD(cryptodev_freesession,ubsec_freesession),
-	DEVMETHOD(cryptodev_process,	ubsec_process),
-	DEVMETHOD(cryptodev_kprocess,	ubsec_kprocess),
-
-	DEVMETHOD_END
-};
-static driver_t ubsec_driver = {
-	"ubsec",
-	ubsec_methods,
-	sizeof (struct ubsec_softc)
-};
-static devclass_t ubsec_devclass;
-
-DECLARE_DUMMY_MODULE(ubsec);
-DRIVER_MODULE(ubsec, pci, ubsec_driver, ubsec_devclass, NULL, NULL);
-MODULE_DEPEND(ubsec, crypto, 1, 1, 1);
-#ifdef UBSEC_RNDTEST
-MODULE_DEPEND(ubsec, rndtest, 1, 1, 1);
-#endif
-
-SYSCTL_NODE(_hw, OID_AUTO, ubsec, CTLFLAG_RD, 0, "Broadcom driver parameters");
-
-#ifdef UBSEC_DEBUG
-static	void ubsec_dump_pb(volatile struct ubsec_pktbuf *);
-static	void ubsec_dump_mcr(struct ubsec_mcr *);
-static	void ubsec_dump_ctx2(struct ubsec_ctx_keyop *);
-
-static	int ubsec_debug = 0;
-SYSCTL_INT(_hw_ubsec, OID_AUTO, debug, CTLFLAG_RW, &ubsec_debug,
-	    0, "control debugging msgs");
-#endif
-
-#define	READ_REG(sc,r) \
-	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r))
-
-#define WRITE_REG(sc,reg,val) \
-	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val)
-
-#define	SWAP32(x) (x) = htole32(ntohl((x)))
-#define	HTOLE32(x) (x) = htole32(x)
-
-
-struct ubsec_stats ubsecstats;
-SYSCTL_STRUCT(_hw_ubsec, OID_AUTO, stats, CTLFLAG_RD, &ubsecstats,
-	    ubsec_stats, "driver statistics");
-
-static int
-ubsec_probe(device_t dev)
-{
-	if (pci_get_vendor(dev) == PCI_VENDOR_SUN &&
-	    (pci_get_device(dev) == PCI_PRODUCT_SUN_5821 ||
-	     pci_get_device(dev) == PCI_PRODUCT_SUN_SCA1K))
-		return (0);
-	if (pci_get_vendor(dev) == PCI_VENDOR_BLUESTEEL &&
-	    (pci_get_device(dev) == PCI_PRODUCT_BLUESTEEL_5501 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BLUESTEEL_5601))
-		return (0);
-	if (pci_get_vendor(dev) == PCI_VENDOR_BROADCOM &&
-	    (pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5801 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5802 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5805 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5820 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5821 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5822 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5823 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5825
-	     ))
-		return (0);
-	return (ENXIO);
-}
-
-static const char*
-ubsec_partname(struct ubsec_softc *sc)
-{
-	/* XXX sprintf numbers when not decoded */
-	switch (pci_get_vendor(sc->sc_dev)) {
-	case PCI_VENDOR_BROADCOM:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_BROADCOM_5801:	return "Broadcom 5801";
-		case PCI_PRODUCT_BROADCOM_5802:	return "Broadcom 5802";
-		case PCI_PRODUCT_BROADCOM_5805:	return "Broadcom 5805";
-		case PCI_PRODUCT_BROADCOM_5820:	return "Broadcom 5820";
-		case PCI_PRODUCT_BROADCOM_5821:	return "Broadcom 5821";
-		case PCI_PRODUCT_BROADCOM_5822:	return "Broadcom 5822";
-		case PCI_PRODUCT_BROADCOM_5823:	return "Broadcom 5823";
-		case PCI_PRODUCT_BROADCOM_5825: return "Broadcom 5825";
-		}
-		return "Broadcom unknown-part";
-	case PCI_VENDOR_BLUESTEEL:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_BLUESTEEL_5601: return "Bluesteel 5601";
-		}
-		return "Bluesteel unknown-part";
-	case PCI_VENDOR_SUN:
-		switch (pci_get_device(sc->sc_dev)) {
-		case PCI_PRODUCT_SUN_5821: return "Sun Crypto 5821";
-		case PCI_PRODUCT_SUN_SCA1K: return "Sun Crypto 1K";
-		}
-		return "Sun unknown-part";
-	}
-	return "Unknown-vendor unknown-part";
-}
-
-static void
-default_harvest(struct rndtest_state *rsp __unused, void *buf, u_int count)
-{
-	add_buffer_randomness_src(buf, count, RAND_SRC_UBSEC);
-}
-
-static int
-ubsec_attach(device_t dev)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-	struct ubsec_dma *dmap;
-	u_int32_t cmd, i;
-	int rid;
-
-	KASSERT(sc != NULL, ("ubsec_attach: null software carrier!"));
-	bzero(sc, sizeof (*sc));
-	sc->sc_dev = dev;
-
-	SIMPLEQ_INIT(&sc->sc_queue);
-	SIMPLEQ_INIT(&sc->sc_qchip);
-	SIMPLEQ_INIT(&sc->sc_queue2);
-	SIMPLEQ_INIT(&sc->sc_qchip2);
-	SIMPLEQ_INIT(&sc->sc_q2free);
-
-	/* XXX handle power management */
-
-	sc->sc_statmask = BS_STAT_MCR1_DONE | BS_STAT_DMAERR;
-
-	if (pci_get_vendor(dev) == PCI_VENDOR_BLUESTEEL &&
-	    pci_get_device(dev) == PCI_PRODUCT_BLUESTEEL_5601)
-		sc->sc_flags |= UBS_FLAGS_KEY | UBS_FLAGS_RNG;
-
-	if (pci_get_vendor(dev) == PCI_VENDOR_BROADCOM &&
-	    (pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5802 ||
-	     pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5805))
-		sc->sc_flags |= UBS_FLAGS_KEY | UBS_FLAGS_RNG;
-
-	if (pci_get_vendor(dev) == PCI_VENDOR_BROADCOM &&
-	    pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5820)
-		sc->sc_flags |= UBS_FLAGS_KEY | UBS_FLAGS_RNG |
-		    UBS_FLAGS_LONGCTX | UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY;
-
-	if ((pci_get_vendor(dev) == PCI_VENDOR_BROADCOM &&
-	     (pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5821 ||
-	      pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5822 ||
-	      pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5823 ||
-	      pci_get_device(dev) == PCI_PRODUCT_BROADCOM_5825)) ||
-	    (pci_get_vendor(dev) == PCI_VENDOR_SUN &&
-	     (pci_get_device(dev) == PCI_PRODUCT_SUN_SCA1K ||
-	      pci_get_device(dev) == PCI_PRODUCT_SUN_5821))) {
-		/* NB: the 5821/5822 defines some additional status bits */
-		sc->sc_statmask |= BS_STAT_MCR1_ALLEMPTY |
-		    BS_STAT_MCR2_ALLEMPTY;
-		sc->sc_flags |= UBS_FLAGS_KEY | UBS_FLAGS_RNG |
-		    UBS_FLAGS_LONGCTX | UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY;
-	}
- 
-	cmd = pci_read_config(dev, PCIR_COMMAND, 4);
-	cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
-	pci_write_config(dev, PCIR_COMMAND, cmd, 4);
-	cmd = pci_read_config(dev, PCIR_COMMAND, 4);
-
-	if (!(cmd & PCIM_CMD_MEMEN)) {
-		device_printf(dev, "failed to enable memory mapping\n");
-		goto bad;
-	}
-
-	if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
-		device_printf(dev, "failed to enable bus mastering\n");
-		goto bad;
-	}
-
-	/* 
-	 * Setup memory-mapping of PCI registers.
-	 */
-	rid = BS_BAR;
-	sc->sc_sr = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
-				       0, ~0, 1, RF_ACTIVE);
-	if (sc->sc_sr == NULL) {
-		device_printf(dev, "cannot map register space\n");
-		goto bad;
-	}
-	sc->sc_st = rman_get_bustag(sc->sc_sr);
-	sc->sc_sh = rman_get_bushandle(sc->sc_sr);
-
-	/*
-	 * Arrange interrupt line.
-	 */
-	rid = 0;
-	sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
-					0, ~0, 1, RF_SHAREABLE|RF_ACTIVE);
-	if (sc->sc_irq == NULL) {
-		device_printf(dev, "could not map interrupt\n");
-		goto bad1;
-	}
-	/*
-	 * NB: Network code assumes we are blocked with splimp()
-	 *     so make sure the IRQ is mapped appropriately.
-	 */
-	if (bus_setup_intr(dev, sc->sc_irq, 0,
-			   ubsec_intr, sc, 
-			   &sc->sc_ih, NULL)) {
-		device_printf(dev, "could not establish interrupt\n");
-		goto bad2;
-	}
-
-	sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE);
-	if (sc->sc_cid < 0) {
-		device_printf(dev, "could not get crypto driver id\n");
-		goto bad3;
-	}
-
-	/*
-	 * Setup DMA descriptor area.
-	 */
-	if (bus_dma_tag_create(NULL,			/* parent */
-			       1, 0,			/* alignment, bounds */
-			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
-			       BUS_SPACE_MAXADDR,	/* highaddr */
-			       0x3ffff,			/* maxsize */
-			       UBS_MAX_SCATTER,		/* nsegments */
-			       0xffff,			/* maxsegsize */
-			       BUS_DMA_ALLOCNOW,	/* flags */
-			       &sc->sc_dmat)) {
-		device_printf(dev, "cannot allocate DMA tag\n");
-		goto bad4;
-	}
-	SIMPLEQ_INIT(&sc->sc_freequeue);
-	dmap = sc->sc_dmaa;
-	for (i = 0; i < UBS_MAX_NQUEUE; i++, dmap++) {
-		struct ubsec_q *q;
-
-		q = kmalloc(sizeof(struct ubsec_q), M_DEVBUF, M_WAITOK);
-		if (ubsec_dma_malloc(sc, sizeof(struct ubsec_dmachunk),
-		    &dmap->d_alloc, 0)) {
-			device_printf(dev, "cannot allocate dma buffers\n");
-			kfree(q, M_DEVBUF);
-			break;
-		}
-		dmap->d_dma = (struct ubsec_dmachunk *)dmap->d_alloc.dma_vaddr;
-
-		q->q_dma = dmap;
-		sc->sc_queuea[i] = q;
-
-		SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
-	}
-
-	device_printf(sc->sc_dev, "%s\n", ubsec_partname(sc));
-
-	crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
-	crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
-
-	/*
-	 * Reset Broadcom chip
-	 */
-	ubsec_reset_board(sc);
-
-	/*
-	 * Init Broadcom specific PCI settings
-	 */
-	ubsec_init_pciregs(dev);
-
-	/*
-	 * Init Broadcom chip
-	 */
-	ubsec_init_board(sc);
-
-#ifndef UBSEC_NO_RNG
-	if (sc->sc_flags & UBS_FLAGS_RNG) {
-		sc->sc_statmask |= BS_STAT_MCR2_DONE;
-#ifdef UBSEC_RNDTEST
-		sc->sc_rndtest = rndtest_attach(dev);
-		if (sc->sc_rndtest)
-			sc->sc_harvest = rndtest_harvest;
-		else
-			sc->sc_harvest = default_harvest;
-#else
-		sc->sc_harvest = default_harvest;
-#endif
-
-		if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
-		    &sc->sc_rng.rng_q.q_mcr, 0))
-			goto skip_rng;
-
-		if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_rngbypass),
-		    &sc->sc_rng.rng_q.q_ctx, 0)) {
-			ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
-			goto skip_rng;
-		}
-
-		if (ubsec_dma_malloc(sc, sizeof(u_int32_t) *
-		    UBSEC_RNG_BUFSIZ, &sc->sc_rng.rng_buf, 0)) {
-			ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_ctx);
-			ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
-			goto skip_rng;
-		}
-
-		if (hz >= 100)
-			sc->sc_rnghz = hz / 100;
-		else
-			sc->sc_rnghz = 1;
-		callout_init(&sc->sc_rngto);
-		callout_reset(&sc->sc_rngto, sc->sc_rnghz, ubsec_rng, sc);
-skip_rng:
-	;
-	}
-#endif /* UBSEC_NO_RNG */
-
-	if (sc->sc_flags & UBS_FLAGS_KEY) {
-		sc->sc_statmask |= BS_STAT_MCR2_DONE;
-
-		crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0);
-#if 0
-		crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0);
-#endif
-	}
-	return (0);
-bad4:
-	crypto_unregister_all(sc->sc_cid);
-bad3:
-	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
-bad2:
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
-bad1:
-	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
-bad:
-	return (ENXIO);
-}
-
-/*
- * Detach a device that successfully probed.
- */
-static int
-ubsec_detach(device_t dev)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-
-	KASSERT(sc != NULL, ("ubsec_detach: null software carrier"));
-
-	/* XXX wait/abort active ops */
-
-	crit_enter();
-
-	callout_stop(&sc->sc_rngto);
-
-	crypto_unregister_all(sc->sc_cid);
-
-#ifdef UBSEC_RNDTEST
-	if (sc->sc_rndtest)
-		rndtest_detach(sc->sc_rndtest);
-#endif
-
-	while (!SIMPLEQ_EMPTY(&sc->sc_freequeue)) {
-		struct ubsec_q *q;
-
-		q = SIMPLEQ_FIRST(&sc->sc_freequeue);
-		SIMPLEQ_REMOVE_HEAD(&sc->sc_freequeue, q_next);
-		ubsec_dma_free(sc, &q->q_dma->d_alloc);
-		kfree(q, M_DEVBUF);
-	}
-#ifndef UBSEC_NO_RNG
-	if (sc->sc_flags & UBS_FLAGS_RNG) {
-		ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
-		ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_ctx);
-		ubsec_dma_free(sc, &sc->sc_rng.rng_buf);
-	}
-#endif /* UBSEC_NO_RNG */
-
-	bus_generic_detach(dev);
-	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
-	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
-
-	bus_dma_tag_destroy(sc->sc_dmat);
-	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr);
-
-	crit_exit();
-
-	return (0);
-}
-
-/*
- * Stop all chip i/o so that the kernel's probe routines don't
- * get confused by errant DMAs when rebooting.
- */
-static void
-ubsec_shutdown(device_t dev)
-{
-#ifdef notyet
-	ubsec_stop(device_get_softc(dev));
-#endif
-}
-
-/*
- * Device suspend routine.
- */
-static int
-ubsec_suspend(device_t dev)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-
-	KASSERT(sc != NULL, ("ubsec_suspend: null software carrier"));
-#ifdef notyet
-	/* XXX stop the device and save PCI settings */
-#endif
-	sc->sc_suspended = 1;
-
-	return (0);
-}
-
-static int
-ubsec_resume(device_t dev)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-
-	KASSERT(sc != NULL, ("ubsec_resume: null software carrier"));
-#ifdef notyet
-	/* XXX retore PCI settings and start the device */
-#endif
-	sc->sc_suspended = 0;
-	return (0);
-}
-
-/*
- * UBSEC Interrupt routine
- */
-static void
-ubsec_intr(void *arg)
-{
-	struct ubsec_softc *sc = arg;
-	volatile u_int32_t stat;
-	struct ubsec_q *q;
-	struct ubsec_dma *dmap;
-	int npkts = 0, i;
-
-	stat = READ_REG(sc, BS_STAT);
-	stat &= sc->sc_statmask;
-	if (stat == 0) {
-		return;
-	}
-
-	WRITE_REG(sc, BS_STAT, stat);		/* IACK */
-
-	/*
-	 * Check to see if we have any packets waiting for us
-	 */
-	if ((stat & BS_STAT_MCR1_DONE)) {
-		while (!SIMPLEQ_EMPTY(&sc->sc_qchip)) {
-			q = SIMPLEQ_FIRST(&sc->sc_qchip);
-			dmap = q->q_dma;
-
-			if ((dmap->d_dma->d_mcr.mcr_flags & htole16(UBS_MCR_DONE)) == 0)
-				break;
-
-			SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip, q_next);
-
-			npkts = q->q_nstacked_mcrs;
-			sc->sc_nqchip -= 1+npkts;
-			/*
-			 * search for further sc_qchip ubsec_q's that share
-			 * the same MCR, and complete them too, they must be
-			 * at the top.
-			 */
-			for (i = 0; i < npkts; i++) {
-				if(q->q_stacked_mcr[i]) {
-					ubsec_callback(sc, q->q_stacked_mcr[i]);
-				} else {
-					break;
-				}
-			}
-			ubsec_callback(sc, q);
-		}
-
-		/*
-		 * Don't send any more packet to chip if there has been
-		 * a DMAERR.
-		 */
-		if (!(stat & BS_STAT_DMAERR))
-			ubsec_feed(sc);
-	}
-
-	/*
-	 * Check to see if we have any key setups/rng's waiting for us
-	 */
-	if ((sc->sc_flags & (UBS_FLAGS_KEY|UBS_FLAGS_RNG)) &&
-	    (stat & BS_STAT_MCR2_DONE)) {
-		struct ubsec_q2 *q2;
-		struct ubsec_mcr *mcr;
-
-		while (!SIMPLEQ_EMPTY(&sc->sc_qchip2)) {
-			q2 = SIMPLEQ_FIRST(&sc->sc_qchip2);
-
-			ubsec_dma_sync(&q2->q_mcr,
-			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
-
-			mcr = (struct ubsec_mcr *)q2->q_mcr.dma_vaddr;
-			if ((mcr->mcr_flags & htole16(UBS_MCR_DONE)) == 0) {
-				ubsec_dma_sync(&q2->q_mcr,
-				    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
-				break;
-			}
-			SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip2, q_next);
-			ubsec_callback2(sc, q2);
-			/*
-			 * Don't send any more packet to chip if there has been
-			 * a DMAERR.
-			 */
-			if (!(stat & BS_STAT_DMAERR))
-				ubsec_feed2(sc);
-		}
-	}
-
-	/*
-	 * Check to see if we got any DMA Error
-	 */
-	if (stat & BS_STAT_DMAERR) {
-#ifdef UBSEC_DEBUG
-		if (ubsec_debug) {
-			volatile u_int32_t a = READ_REG(sc, BS_ERR);
-
-			kprintf("dmaerr %s@%08x\n",
-			    (a & BS_ERR_READ) ? "read" : "write",
-			    a & BS_ERR_ADDR);
-		}
-#endif /* UBSEC_DEBUG */
-		ubsecstats.hst_dmaerr++;
-		ubsec_totalreset(sc);
-		ubsec_feed(sc);
-	}
-
-	if (sc->sc_needwakeup) {		/* XXX check high watermark */
-		int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
-#ifdef UBSEC_DEBUG
-		if (ubsec_debug)
-			device_printf(sc->sc_dev, "wakeup crypto (%x)\n",
-				sc->sc_needwakeup);
-#endif /* UBSEC_DEBUG */
-		sc->sc_needwakeup &= ~wakeup;
-		crypto_unblock(sc->sc_cid, wakeup);
-	}
-}
-
-/*
- * ubsec_feed() - aggregate and post requests to chip
- */
-static void
-ubsec_feed(struct ubsec_softc *sc)
-{
-	struct ubsec_q *q, *q2;
-	int npkts, i;
-	void *v;
-	u_int32_t stat;
-
-	/*
-	 * Decide how many ops to combine in a single MCR.  We cannot
-	 * aggregate more than UBS_MAX_AGGR because this is the number
-	 * of slots defined in the data structure.  Note that
-	 * aggregation only happens if ops are marked batch'able.
-	 * Aggregating ops reduces the number of interrupts to the host
-	 * but also (potentially) increases the latency for processing
-	 * completed ops as we only get an interrupt when all aggregated
-	 * ops have completed.
-	 */
-	if (sc->sc_nqueue == 0)
-		return;
-	if (sc->sc_nqueue > 1) {
-		npkts = 0;
-		SIMPLEQ_FOREACH(q, &sc->sc_queue, q_next) {
-			npkts++;
-			if ((q->q_crp->crp_flags & CRYPTO_F_BATCH) == 0)
-				break;
-		}
-	} else
-		npkts = 1;
-	/*
-	 * Check device status before going any further.
-	 */
-	if ((stat = READ_REG(sc, BS_STAT)) & (BS_STAT_MCR1_FULL | BS_STAT_DMAERR)) {
-		if (stat & BS_STAT_DMAERR) {
-			ubsec_totalreset(sc);
-			ubsecstats.hst_dmaerr++;
-		} else
-			ubsecstats.hst_mcr1full++;
-		return;
-	}
-	if (sc->sc_nqueue > ubsecstats.hst_maxqueue)
-		ubsecstats.hst_maxqueue = sc->sc_nqueue;
-	if (npkts > UBS_MAX_AGGR)
-		npkts = UBS_MAX_AGGR;
-	if (npkts < 2)				/* special case 1 op */
-		goto feed1;
-
-	ubsecstats.hst_totbatch += npkts-1;
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug)
-		kprintf("merging %d records\n", npkts);
-#endif /* UBSEC_DEBUG */
-
-	q = SIMPLEQ_FIRST(&sc->sc_queue);
-	SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, q_next);
-	--sc->sc_nqueue;
-
-	bus_dmamap_sync(sc->sc_dmat, q->q_src_map, BUS_DMASYNC_PREWRITE);
-	if (q->q_dst_map != NULL)
-		bus_dmamap_sync(sc->sc_dmat, q->q_dst_map, BUS_DMASYNC_PREREAD);
-
-	q->q_nstacked_mcrs = npkts - 1;		/* Number of packets stacked */
-
-	for (i = 0; i < q->q_nstacked_mcrs; i++) {
-		q2 = SIMPLEQ_FIRST(&sc->sc_queue);
-		bus_dmamap_sync(sc->sc_dmat, q2->q_src_map,
-		    BUS_DMASYNC_PREWRITE);
-		if (q2->q_dst_map != NULL)
-			bus_dmamap_sync(sc->sc_dmat, q2->q_dst_map,
-			    BUS_DMASYNC_PREREAD);
-		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, q_next);
-		--sc->sc_nqueue;
-
-		v = (void*)(((char *)&q2->q_dma->d_dma->d_mcr) + sizeof(struct ubsec_mcr) -
-		    sizeof(struct ubsec_mcr_add));
-		bcopy(v, &q->q_dma->d_dma->d_mcradd[i], sizeof(struct ubsec_mcr_add));
-		q->q_stacked_mcr[i] = q2;
-	}
-	q->q_dma->d_dma->d_mcr.mcr_pkts = htole16(npkts);
-	SIMPLEQ_INSERT_TAIL(&sc->sc_qchip, q, q_next);
-	sc->sc_nqchip += npkts;
-	if (sc->sc_nqchip > ubsecstats.hst_maxqchip)
-		ubsecstats.hst_maxqchip = sc->sc_nqchip;
-	ubsec_dma_sync(&q->q_dma->d_alloc,
-	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-	WRITE_REG(sc, BS_MCR1, q->q_dma->d_alloc.dma_paddr +
-	    offsetof(struct ubsec_dmachunk, d_mcr));
-	return;
-
-feed1:
-	q = SIMPLEQ_FIRST(&sc->sc_queue);
-
-	bus_dmamap_sync(sc->sc_dmat, q->q_src_map, BUS_DMASYNC_PREWRITE);
-	if (q->q_dst_map != NULL)
-		bus_dmamap_sync(sc->sc_dmat, q->q_dst_map, BUS_DMASYNC_PREREAD);
-	ubsec_dma_sync(&q->q_dma->d_alloc,
-	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-
-	WRITE_REG(sc, BS_MCR1, q->q_dma->d_alloc.dma_paddr +
-	    offsetof(struct ubsec_dmachunk, d_mcr));
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug)
-		kprintf("feed1: q->chip %p %08x stat %08x\n",
-		      q, (u_int32_t)vtophys(&q->q_dma->d_dma->d_mcr),
-		      stat);
-#endif /* UBSEC_DEBUG */
-	SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, q_next);
-	--sc->sc_nqueue;
-	SIMPLEQ_INSERT_TAIL(&sc->sc_qchip, q, q_next);
-	sc->sc_nqchip++;
-	if (sc->sc_nqchip > ubsecstats.hst_maxqchip)
-		ubsecstats.hst_maxqchip = sc->sc_nqchip;
-	return;
-}
-
-static void
-ubsec_setup_enckey(struct ubsec_session *ses, int algo, caddr_t key)
-{
-
-	/* Go ahead and compute key in ubsec's byte order */
-	if (algo == CRYPTO_DES_CBC) {
-		bcopy(key, &ses->ses_deskey[0], 8);
-		bcopy(key, &ses->ses_deskey[2], 8);
-		bcopy(key, &ses->ses_deskey[4], 8);
-	} else
-		bcopy(key, ses->ses_deskey, 24);
-
-	SWAP32(ses->ses_deskey[0]);
-	SWAP32(ses->ses_deskey[1]);
-	SWAP32(ses->ses_deskey[2]);
-	SWAP32(ses->ses_deskey[3]);
-	SWAP32(ses->ses_deskey[4]);
-	SWAP32(ses->ses_deskey[5]);
-}
-
-static void
-ubsec_setup_mackey(struct ubsec_session *ses, int algo, caddr_t key, int klen)
-{
-	MD5_CTX md5ctx;
-	SHA1_CTX sha1ctx;
-	int i;
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= HMAC_IPAD_VAL;
-
-	if (algo == CRYPTO_MD5_HMAC) {
-		MD5Init(&md5ctx);
-		MD5Update(&md5ctx, key, klen);
-		MD5Update(&md5ctx, hmac_ipad_buffer, MD5_HMAC_BLOCK_LEN - klen);
-		/* gcc8 craps out on -Warray-bounds w/ optimized bcopy */
-		_bcopy(&md5ctx.A, ses->ses_hminner, sizeof(md5ctx.A) * 4);
-	} else {
-		SHA1Init(&sha1ctx);
-		SHA1Update(&sha1ctx, key, klen);
-		SHA1Update(&sha1ctx, hmac_ipad_buffer,
-		    SHA1_HMAC_BLOCK_LEN - klen);
-		bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32));
-	}
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
-
-	if (algo == CRYPTO_MD5_HMAC) {
-		MD5Init(&md5ctx);
-		MD5Update(&md5ctx, key, klen);
-		MD5Update(&md5ctx, hmac_opad_buffer, MD5_HMAC_BLOCK_LEN - klen);
-		/* gcc8 craps out on -Warray-bounds w/ optimized bcopy */
-		_bcopy(&md5ctx.A, ses->ses_hmouter, sizeof(md5ctx.A) * 4);
-	} else {
-		SHA1Init(&sha1ctx);
-		SHA1Update(&sha1ctx, key, klen);
-		SHA1Update(&sha1ctx, hmac_opad_buffer,
-		    SHA1_HMAC_BLOCK_LEN - klen);
-		bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32));
-	}
-
-	for (i = 0; i < klen; i++)
-		key[i] ^= HMAC_OPAD_VAL;
-}
-
-/*
- * Allocate a new 'session' and return an encoded session id.  'sidp'
- * contains our registration id, and should contain an encoded session
- * id on successful allocation.
- */
-static int
-ubsec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-	struct cryptoini *c, *encini = NULL, *macini = NULL;
-	struct ubsec_session *ses = NULL;
-	int sesn;
-#if 0
-	MD5_CTX md5ctx;
-	SHA1_CTX sha1ctx;
-	int i;
-#endif
-
-	KASSERT(sc != NULL, ("ubsec_newsession: null softc"));
-	if (sidp == NULL || cri == NULL || sc == NULL)
-		return (EINVAL);
-
-	for (c = cri; c != NULL; c = c->cri_next) {
-		if (c->cri_alg == CRYPTO_MD5_HMAC ||
-		    c->cri_alg == CRYPTO_SHA1_HMAC) {
-			if (macini)
-				return (EINVAL);
-			macini = c;
-		} else if (c->cri_alg == CRYPTO_DES_CBC ||
-		    c->cri_alg == CRYPTO_3DES_CBC) {
-			if (encini)
-				return (EINVAL);
-			encini = c;
-		} else
-			return (EINVAL);
-	}
-	if (encini == NULL && macini == NULL)
-		return (EINVAL);
-
-	if (sc->sc_sessions == NULL) {
-		ses = sc->sc_sessions = kmalloc(sizeof(struct ubsec_session),
-						M_DEVBUF, M_INTWAIT);
-		sesn = 0;
-		sc->sc_nsessions = 1;
-	} else {
-		for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
-			if (sc->sc_sessions[sesn].ses_used == 0) {
-				ses = &sc->sc_sessions[sesn];
-				break;
-			}
-		}
-
-		if (ses == NULL) {
-			sesn = sc->sc_nsessions;
-			ses = kmalloc((sesn + 1) * sizeof(struct ubsec_session),
-					M_DEVBUF, M_INTWAIT);
-			bcopy(sc->sc_sessions, ses, sesn *
-			    sizeof(struct ubsec_session));
-			bzero(sc->sc_sessions, sesn *
-			    sizeof(struct ubsec_session));
-			kfree(sc->sc_sessions, M_DEVBUF);
-			sc->sc_sessions = ses;
-			ses = &sc->sc_sessions[sesn];
-			sc->sc_nsessions++;
-		}
-	}
-
-	bzero(ses, sizeof(struct ubsec_session));
-	ses->ses_used = 1;
-	if (encini) {
-		read_random(ses->ses_iv, sizeof(ses->ses_iv), 0);
-		if (encini->cri_key != NULL) {
-			ubsec_setup_enckey(ses, encini->cri_alg,
-			    encini->cri_key);
-		}
-	}
-
-	if (macini) {
-		ses->ses_mlen = macini->cri_mlen;
-		if (ses->ses_mlen == 0) {
-			if (macini->cri_alg == CRYPTO_MD5_HMAC)
-				ses->ses_mlen = MD5_HASH_LEN;
-			else
-				ses->ses_mlen = SHA1_HASH_LEN;
-		}
-
-		if (macini->cri_key != NULL) {
-			ubsec_setup_mackey(ses, macini->cri_alg,
-			    macini->cri_key, macini->cri_klen/8);
-		}
-	}
-
-	*sidp = UBSEC_SID(device_get_unit(sc->sc_dev), sesn);
-	return (0);
-}
-
-/*
- * Deallocate a session.
- */
-static int
-ubsec_freesession(device_t dev, u_int64_t tid)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-	int session;
-	u_int32_t sid = CRYPTO_SESID2LID(tid);
-
-	KASSERT(sc != NULL, ("ubsec_freesession: null softc"));
-	if (sc == NULL)
-		return (EINVAL);
-
-	session = UBSEC_SESSION(sid);
-	if (session >= sc->sc_nsessions)
-		return (EINVAL);
-
-	bzero(&sc->sc_sessions[session], sizeof(sc->sc_sessions[session]));
-	return (0);
-}
-
-static void
-ubsec_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, bus_size_t mapsize, int error)
-{
-	struct ubsec_operand *op = arg;
-
-	KASSERT(nsegs <= UBS_MAX_SCATTER,
-		("Too many DMA segments returned when mapping operand"));
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug)
-		kprintf("ubsec_op_cb: mapsize %u nsegs %d error %d\n",
-			(u_int) mapsize, nsegs, error);
-#endif
-	if (error != 0)
-		return;
-
-	op->mapsize = mapsize;
-	op->nsegs = nsegs;
-	bcopy(seg, op->segs, nsegs * sizeof (seg[0]));
-}
-
-static int
-ubsec_process(device_t dev, struct cryptop *crp, int hint)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-	struct ubsec_q *q = NULL;
-	int err = 0, i, j, nicealign;
-	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
-	int encoffset = 0, macoffset = 0, cpskip, cpoffset;
-	int sskip, dskip, stheend, dtheend;
-	int16_t coffset;
-	struct ubsec_session *ses;
-	struct ubsec_pktctx ctx;
-	struct ubsec_dma *dmap = NULL;
-
-	if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
-		ubsecstats.hst_invalid++;
-		return (EINVAL);
-	}
-	if (UBSEC_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
-		ubsecstats.hst_badsession++;
-		return (EINVAL);
-	}
-
-	crit_enter();
-
-	if (SIMPLEQ_EMPTY(&sc->sc_freequeue)) {
-		ubsecstats.hst_queuefull++;
-		sc->sc_needwakeup |= CRYPTO_SYMQ;
-		crit_exit();
-		return (ERESTART);
-	}
-	q = SIMPLEQ_FIRST(&sc->sc_freequeue);
-	SIMPLEQ_REMOVE_HEAD(&sc->sc_freequeue, q_next);
-	crit_exit();
-
-	dmap = q->q_dma; /* Save dma pointer */
-	bzero(q, sizeof(struct ubsec_q));
-	bzero(&ctx, sizeof(ctx));
-
-	q->q_sesn = UBSEC_SESSION(crp->crp_sid);
-	q->q_dma = dmap;
-	ses = &sc->sc_sessions[q->q_sesn];
-
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		q->q_src_m = (struct mbuf *)crp->crp_buf;
-		q->q_dst_m = (struct mbuf *)crp->crp_buf;
-	} else if (crp->crp_flags & CRYPTO_F_IOV) {
-		q->q_src_io = (struct uio *)crp->crp_buf;
-		q->q_dst_io = (struct uio *)crp->crp_buf;
-	} else {
-		ubsecstats.hst_badflags++;
-		err = EINVAL;
-		goto errout;	/* XXX we don't handle contiguous blocks! */
-	}
-
-	bzero(&dmap->d_dma->d_mcr, sizeof(struct ubsec_mcr));
-
-	dmap->d_dma->d_mcr.mcr_pkts = htole16(1);
-	dmap->d_dma->d_mcr.mcr_flags = 0;
-	q->q_crp = crp;
-
-	crd1 = crp->crp_desc;
-	if (crd1 == NULL) {
-		ubsecstats.hst_nodesc++;
-		err = EINVAL;
-		goto errout;
-	}
-	crd2 = crd1->crd_next;
-
-	if (crd2 == NULL) {
-		if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
-		    crd1->crd_alg == CRYPTO_SHA1_HMAC) {
-			maccrd = crd1;
-			enccrd = NULL;
-		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
-		    crd1->crd_alg == CRYPTO_3DES_CBC) {
-			maccrd = NULL;
-			enccrd = crd1;
-		} else {
-			ubsecstats.hst_badalg++;
-			err = EINVAL;
-			goto errout;
-		}
-	} else {
-		if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
-		    crd1->crd_alg == CRYPTO_SHA1_HMAC) &&
-		    (crd2->crd_alg == CRYPTO_DES_CBC ||
-			crd2->crd_alg == CRYPTO_3DES_CBC) &&
-		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
-			maccrd = crd1;
-			enccrd = crd2;
-		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
-		    crd1->crd_alg == CRYPTO_3DES_CBC) &&
-		    (crd2->crd_alg == CRYPTO_MD5_HMAC ||
-			crd2->crd_alg == CRYPTO_SHA1_HMAC) &&
-		    (crd1->crd_flags & CRD_F_ENCRYPT)) {
-			enccrd = crd1;
-			maccrd = crd2;
-		} else {
-			/*
-			 * We cannot order the ubsec as requested
-			 */
-			ubsecstats.hst_badalg++;
-			err = EINVAL;
-			goto errout;
-		}
-	}
-
-	if (enccrd) {
-		if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
-			ubsec_setup_enckey(ses, enccrd->crd_alg,
-			    enccrd->crd_key);
-		}
-
-		encoffset = enccrd->crd_skip;
-		ctx.pc_flags |= htole16(UBS_PKTCTX_ENC_3DES);
-
-		if (enccrd->crd_flags & CRD_F_ENCRYPT) {
-			q->q_flags |= UBSEC_QFLAGS_COPYOUTIV;
-
-			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
-				bcopy(enccrd->crd_iv, ctx.pc_iv, 8);
-			else {
-				ctx.pc_iv[0] = ses->ses_iv[0];
-				ctx.pc_iv[1] = ses->ses_iv[1];
-			}
-
-			if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
-				crypto_copyback(crp->crp_flags, crp->crp_buf,
-				    enccrd->crd_inject, 8, (caddr_t)ctx.pc_iv);
-			}
-		} else {
-			ctx.pc_flags |= htole16(UBS_PKTCTX_INBOUND);
-
-			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
-				bcopy(enccrd->crd_iv, ctx.pc_iv, 8);
-			else {
-				crypto_copydata(crp->crp_flags, crp->crp_buf,
-				    enccrd->crd_inject, 8, (caddr_t)ctx.pc_iv);
-			}
-		}
-
-		ctx.pc_deskey[0] = ses->ses_deskey[0];
-		ctx.pc_deskey[1] = ses->ses_deskey[1];
-		ctx.pc_deskey[2] = ses->ses_deskey[2];
-		ctx.pc_deskey[3] = ses->ses_deskey[3];
-		ctx.pc_deskey[4] = ses->ses_deskey[4];
-		ctx.pc_deskey[5] = ses->ses_deskey[5];
-		SWAP32(ctx.pc_iv[0]);
-		SWAP32(ctx.pc_iv[1]);
-	}
-
-	if (maccrd) {
-		if (maccrd->crd_flags & CRD_F_KEY_EXPLICIT) {
-			ubsec_setup_mackey(ses, maccrd->crd_alg,
-			    maccrd->crd_key, maccrd->crd_klen / 8);
-		}
-
-		macoffset = maccrd->crd_skip;
-
-		if (maccrd->crd_alg == CRYPTO_MD5_HMAC)
-			ctx.pc_flags |= htole16(UBS_PKTCTX_AUTH_MD5);
-		else
-			ctx.pc_flags |= htole16(UBS_PKTCTX_AUTH_SHA1);
-
-		for (i = 0; i < 5; i++) {
-			ctx.pc_hminner[i] = ses->ses_hminner[i];
-			ctx.pc_hmouter[i] = ses->ses_hmouter[i];
-
-			HTOLE32(ctx.pc_hminner[i]);
-			HTOLE32(ctx.pc_hmouter[i]);
-		}
-	}
-
-	if (enccrd && maccrd) {
-		/*
-		 * ubsec cannot handle packets where the end of encryption
-		 * and authentication are not the same, or where the
-		 * encrypted part begins before the authenticated part.
-		 */
-		if ((encoffset + enccrd->crd_len) !=
-		    (macoffset + maccrd->crd_len)) {
-			ubsecstats.hst_lenmismatch++;
-			err = EINVAL;
-			goto errout;
-		}
-		if (enccrd->crd_skip < maccrd->crd_skip) {
-			ubsecstats.hst_skipmismatch++;
-			err = EINVAL;
-			goto errout;
-		}
-		sskip = maccrd->crd_skip;
-		cpskip = dskip = enccrd->crd_skip;
-		stheend = maccrd->crd_len;
-		dtheend = enccrd->crd_len;
-		coffset = enccrd->crd_skip - maccrd->crd_skip;
-		cpoffset = cpskip + dtheend;
-#ifdef UBSEC_DEBUG
-		if (ubsec_debug) {
-			kprintf("mac: skip %d, len %d, inject %d\n",
-			    maccrd->crd_skip, maccrd->crd_len, maccrd->crd_inject);
-			kprintf("enc: skip %d, len %d, inject %d\n",
-			    enccrd->crd_skip, enccrd->crd_len, enccrd->crd_inject);
-			kprintf("src: skip %d, len %d\n", sskip, stheend);
-			kprintf("dst: skip %d, len %d\n", dskip, dtheend);
-			kprintf("ubs: coffset %d, pktlen %d, cpskip %d, cpoffset %d\n",
-			    coffset, stheend, cpskip, cpoffset);
-		}
-#endif
-	} else {
-		cpskip = dskip = sskip = macoffset + encoffset;
-		dtheend = stheend = (enccrd)?enccrd->crd_len:maccrd->crd_len;
-		cpoffset = cpskip + dtheend;
-		coffset = 0;
-	}
-	ctx.pc_offset = htole16(coffset >> 2);
-
-	if (bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &q->q_src_map)) {
-		ubsecstats.hst_nomap++;
-		err = ENOMEM;
-		goto errout;
-	}
-	if (crp->crp_flags & CRYPTO_F_IMBUF) {
-		if (bus_dmamap_load_mbuf(sc->sc_dmat, q->q_src_map,
-		    q->q_src_m, ubsec_op_cb, &q->q_src, BUS_DMA_NOWAIT) != 0) {
-			bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
-			q->q_src_map = NULL;
-			ubsecstats.hst_noload++;
-			err = ENOMEM;
-			goto errout;
-		}
-	} else if (crp->crp_flags & CRYPTO_F_IOV) {
-		if (bus_dmamap_load_uio(sc->sc_dmat, q->q_src_map,
-		    q->q_src_io, ubsec_op_cb, &q->q_src, BUS_DMA_NOWAIT) != 0) {
-			bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
-			q->q_src_map = NULL;
-			ubsecstats.hst_noload++;
-			err = ENOMEM;
-			goto errout;
-		}
-	}
-	nicealign = ubsec_dmamap_aligned(&q->q_src);
-
-	dmap->d_dma->d_mcr.mcr_pktlen = htole16(stheend);
-
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug)
-		kprintf("src skip: %d nicealign: %u\n", sskip, nicealign);
-#endif
-	for (i = j = 0; i < q->q_src_nsegs; i++) {
-		struct ubsec_pktbuf *pb;
-		bus_size_t packl = q->q_src_segs[i].ds_len;
-		bus_addr_t packp = q->q_src_segs[i].ds_addr;
-
-		if (sskip >= packl) {
-			sskip -= packl;
-			continue;
-		}
-
-		packl -= sskip;
-		packp += sskip;
-		sskip = 0;
-
-		if (packl > 0xfffc) {
-			err = EIO;
-			goto errout;
-		}
-
-		if (j == 0)
-			pb = &dmap->d_dma->d_mcr.mcr_ipktbuf;
-		else
-			pb = &dmap->d_dma->d_sbuf[j - 1];
-
-		pb->pb_addr = htole32(packp);
-
-		if (stheend) {
-			if (packl > stheend) {
-				pb->pb_len = htole32(stheend);
-				stheend = 0;
-			} else {
-				pb->pb_len = htole32(packl);
-				stheend -= packl;
-			}
-		} else
-			pb->pb_len = htole32(packl);
-
-		if ((i + 1) == q->q_src_nsegs)
-			pb->pb_next = 0;
-		else
-			pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
-			    offsetof(struct ubsec_dmachunk, d_sbuf[j]));
-		j++;
-	}
-
-	if (enccrd == NULL && maccrd != NULL) {
-		dmap->d_dma->d_mcr.mcr_opktbuf.pb_addr = 0;
-		dmap->d_dma->d_mcr.mcr_opktbuf.pb_len = 0;
-		dmap->d_dma->d_mcr.mcr_opktbuf.pb_next = htole32(dmap->d_alloc.dma_paddr +
-		    offsetof(struct ubsec_dmachunk, d_macbuf[0]));
-#ifdef UBSEC_DEBUG
-		if (ubsec_debug)
-			kprintf("opkt: %x %x %x\n",
-			    dmap->d_dma->d_mcr.mcr_opktbuf.pb_addr,
-			    dmap->d_dma->d_mcr.mcr_opktbuf.pb_len,
-			    dmap->d_dma->d_mcr.mcr_opktbuf.pb_next);
-#endif
-	} else {
-		if (crp->crp_flags & CRYPTO_F_IOV) {
-			if (!nicealign) {
-				ubsecstats.hst_iovmisaligned++;
-				err = EINVAL;
-				goto errout;
-			}
-			if (bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
-			     &q->q_dst_map)) {
-				ubsecstats.hst_nomap++;
-				err = ENOMEM;
-				goto errout;
-			}
-			if (bus_dmamap_load_uio(sc->sc_dmat, q->q_dst_map,
-			    q->q_dst_io, ubsec_op_cb, &q->q_dst, BUS_DMA_NOWAIT) != 0) {
-				bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
-				q->q_dst_map = NULL;
-				ubsecstats.hst_noload++;
-				err = ENOMEM;
-				goto errout;
-			}
-		} else if (crp->crp_flags & CRYPTO_F_IMBUF) {
-			if (nicealign) {
-				q->q_dst = q->q_src;
-			} else {
-				int totlen, len;
-				struct mbuf *m, *top, **mp;
-
-				ubsecstats.hst_unaligned++;
-				totlen = q->q_src_mapsize;
-				if (q->q_src_m->m_flags & M_PKTHDR) {
-					len = MHLEN;
-					MGETHDR(m, M_NOWAIT, MT_DATA);
-					if (m && !m_dup_pkthdr(m, q->q_src_m, M_NOWAIT)) {
-						m_free(m);
-						m = NULL;
-					}
-				} else {
-					len = MLEN;
-					MGET(m, M_NOWAIT, MT_DATA);
-				}
-				if (m == NULL) {
-					ubsecstats.hst_nombuf++;
-					err = sc->sc_nqueue ? ERESTART : ENOMEM;
-					goto errout;
-				}
-				if (totlen >= MINCLSIZE) {
-					MCLGET(m, M_NOWAIT);
-					if ((m->m_flags & M_EXT) == 0) {
-						m_free(m);
-						ubsecstats.hst_nomcl++;
-						err = sc->sc_nqueue ? ERESTART : ENOMEM;
-						goto errout;
-					}
-					len = MCLBYTES;
-				}
-				m->m_len = len;
-				top = NULL;
-				mp = &top;
-
-				while (totlen > 0) {
-					if (top) {
-						MGET(m, M_NOWAIT, MT_DATA);
-						if (m == NULL) {
-							m_freem(top);
-							ubsecstats.hst_nombuf++;
-							err = sc->sc_nqueue ? ERESTART : ENOMEM;
-							goto errout;
-						}
-						len = MLEN;
-					}
-					if (top && totlen >= MINCLSIZE) {
-						MCLGET(m, M_NOWAIT);
-						if ((m->m_flags & M_EXT) == 0) {
-							*mp = m;
-							m_freem(top);
-							ubsecstats.hst_nomcl++;
-							err = sc->sc_nqueue ? ERESTART : ENOMEM;
-							goto errout;
-						}
-						len = MCLBYTES;
-					}
-					m->m_len = len = min(totlen, len);
-					totlen -= len;
-					*mp = m;
-					mp = &m->m_next;
-				}
-				q->q_dst_m = top;
-				ubsec_mcopy(q->q_src_m, q->q_dst_m,
-				    cpskip, cpoffset);
-				if (bus_dmamap_create(sc->sc_dmat, 
-				    BUS_DMA_NOWAIT, &q->q_dst_map) != 0) {
-					ubsecstats.hst_nomap++;
-					err = ENOMEM;
-					goto errout;
-				}
-				if (bus_dmamap_load_mbuf(sc->sc_dmat,
-				    q->q_dst_map, q->q_dst_m,
-				    ubsec_op_cb, &q->q_dst,
-				    BUS_DMA_NOWAIT) != 0) {
-					bus_dmamap_destroy(sc->sc_dmat,
-					q->q_dst_map);
-					q->q_dst_map = NULL;
-					ubsecstats.hst_noload++;
-					err = ENOMEM;
-					goto errout;
-				}
-			}
-		} else {
-			ubsecstats.hst_badflags++;
-			err = EINVAL;
-			goto errout;
-		}
-
-#ifdef UBSEC_DEBUG
-		if (ubsec_debug)
-			kprintf("dst skip: %d\n", dskip);
-#endif
-		for (i = j = 0; i < q->q_dst_nsegs; i++) {
-			struct ubsec_pktbuf *pb;
-			bus_size_t packl = q->q_dst_segs[i].ds_len;
-			bus_addr_t packp = q->q_dst_segs[i].ds_addr;
-
-			if (dskip >= packl) {
-				dskip -= packl;
-				continue;
-			}
-
-			packl -= dskip;
-			packp += dskip;
-			dskip = 0;
-
-			if (packl > 0xfffc) {
-				err = EIO;
-				goto errout;
-			}
-
-			if (j == 0)
-				pb = &dmap->d_dma->d_mcr.mcr_opktbuf;
-			else
-				pb = &dmap->d_dma->d_dbuf[j - 1];
-
-			pb->pb_addr = htole32(packp);
-
-			if (dtheend) {
-				if (packl > dtheend) {
-					pb->pb_len = htole32(dtheend);
-					dtheend = 0;
-				} else {
-					pb->pb_len = htole32(packl);
-					dtheend -= packl;
-				}
-			} else
-				pb->pb_len = htole32(packl);
-
-			if ((i + 1) == q->q_dst_nsegs) {
-				if (maccrd)
-					pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
-					    offsetof(struct ubsec_dmachunk, d_macbuf[0]));
-				else
-					pb->pb_next = 0;
-			} else
-				pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
-				    offsetof(struct ubsec_dmachunk, d_dbuf[j]));
-			j++;
-		}
-	}
-
-	dmap->d_dma->d_mcr.mcr_cmdctxp = htole32(dmap->d_alloc.dma_paddr +
-	    offsetof(struct ubsec_dmachunk, d_ctx));
-
-	if (sc->sc_flags & UBS_FLAGS_LONGCTX) {
-		struct ubsec_pktctx_long *ctxl;
-
-		ctxl = (struct ubsec_pktctx_long *)(dmap->d_alloc.dma_vaddr +
-		    offsetof(struct ubsec_dmachunk, d_ctx));
-		
-		/* transform small context into long context */
-		ctxl->pc_len = htole16(sizeof(struct ubsec_pktctx_long));
-		ctxl->pc_type = htole16(UBS_PKTCTX_TYPE_IPSEC);
-		ctxl->pc_flags = ctx.pc_flags;
-		ctxl->pc_offset = ctx.pc_offset;
-		for (i = 0; i < 6; i++)
-			ctxl->pc_deskey[i] = ctx.pc_deskey[i];
-		for (i = 0; i < 5; i++)
-			ctxl->pc_hminner[i] = ctx.pc_hminner[i];
-		for (i = 0; i < 5; i++)
-			ctxl->pc_hmouter[i] = ctx.pc_hmouter[i];   
-		ctxl->pc_iv[0] = ctx.pc_iv[0];
-		ctxl->pc_iv[1] = ctx.pc_iv[1];
-	} else
-		bcopy(&ctx, dmap->d_alloc.dma_vaddr +
-		    offsetof(struct ubsec_dmachunk, d_ctx),
-		    sizeof(struct ubsec_pktctx));
-
-	crit_enter();
-	SIMPLEQ_INSERT_TAIL(&sc->sc_queue, q, q_next);
-	sc->sc_nqueue++;
-	ubsecstats.hst_ipackets++;
-	ubsecstats.hst_ibytes += dmap->d_alloc.dma_size;
-	if ((hint & CRYPTO_HINT_MORE) == 0 || sc->sc_nqueue >= UBS_MAX_AGGR)
-		ubsec_feed(sc);
-	crit_exit();
-	return (0);
-
-errout:
-	if (q != NULL) {
-		if ((q->q_dst_m != NULL) && (q->q_src_m != q->q_dst_m))
-			m_freem(q->q_dst_m);
-
-		if (q->q_dst_map != NULL && q->q_dst_map != q->q_src_map) {
-			bus_dmamap_unload(sc->sc_dmat, q->q_dst_map);
-			bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
-		}
-		if (q->q_src_map != NULL) {
-			bus_dmamap_unload(sc->sc_dmat, q->q_src_map);
-			bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
-		}
-
-		crit_enter();
-		SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
-		crit_exit();
-	}
-	if (err != ERESTART) {
-		crp->crp_etype = err;
-		crypto_done(crp);
-	} else {
-		sc->sc_needwakeup |= CRYPTO_SYMQ;
-	}
-	return (err);
-}
-
-static void
-ubsec_callback(struct ubsec_softc *sc, struct ubsec_q *q)
-{
-	struct cryptop *crp = (struct cryptop *)q->q_crp;
-	struct cryptodesc *crd;
-	struct ubsec_dma *dmap = q->q_dma;
-
-	ubsecstats.hst_opackets++;
-	ubsecstats.hst_obytes += dmap->d_alloc.dma_size;
-
-	ubsec_dma_sync(&dmap->d_alloc,
-	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
-	if (q->q_dst_map != NULL && q->q_dst_map != q->q_src_map) {
-		bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
-		    BUS_DMASYNC_POSTREAD);
-		bus_dmamap_unload(sc->sc_dmat, q->q_dst_map);
-		bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
-	}
-	bus_dmamap_sync(sc->sc_dmat, q->q_src_map, BUS_DMASYNC_POSTWRITE);
-	bus_dmamap_unload(sc->sc_dmat, q->q_src_map);
-	bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
-
-	if ((crp->crp_flags & CRYPTO_F_IMBUF) && (q->q_src_m != q->q_dst_m)) {
-		m_freem(q->q_src_m);
-		crp->crp_buf = (caddr_t)q->q_dst_m;
-	}
-
-	/* copy out IV for future use */
-	if (q->q_flags & UBSEC_QFLAGS_COPYOUTIV) {
-		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-			if (crd->crd_alg != CRYPTO_DES_CBC &&
-			    crd->crd_alg != CRYPTO_3DES_CBC)
-				continue;
-			crypto_copydata(crp->crp_flags, crp->crp_buf,
-			    crd->crd_skip + crd->crd_len - 8, 8,
-			    (caddr_t)sc->sc_sessions[q->q_sesn].ses_iv);
-			break;
-		}
-	}
-
-	for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-		if (crd->crd_alg != CRYPTO_MD5_HMAC &&
-		    crd->crd_alg != CRYPTO_SHA1_HMAC)
-			continue;
-		crypto_copyback(crp->crp_flags, crp->crp_buf, crd->crd_inject,
-		    sc->sc_sessions[q->q_sesn].ses_mlen,
-		    (caddr_t)dmap->d_dma->d_macbuf);
-	}
-	SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
-	crypto_done(crp);
-}
-
-static void
-ubsec_mcopy(struct mbuf *srcm, struct mbuf *dstm, int hoffset, int toffset)
-{
-	int i, j, dlen, slen;
-	caddr_t dptr, sptr;
-
-	j = 0;
-	sptr = srcm->m_data;
-	slen = srcm->m_len;
-	dptr = dstm->m_data;
-	dlen = dstm->m_len;
-
-	while (1) {
-		for (i = 0; i < min(slen, dlen); i++) {
-			if (j < hoffset || j >= toffset)
-				*dptr++ = *sptr++;
-			slen--;
-			dlen--;
-			j++;
-		}
-		if (slen == 0) {
-			srcm = srcm->m_next;
-			if (srcm == NULL)
-				return;
-			sptr = srcm->m_data;
-			slen = srcm->m_len;
-		}
-		if (dlen == 0) {
-			dstm = dstm->m_next;
-			if (dstm == NULL)
-				return;
-			dptr = dstm->m_data;
-			dlen = dstm->m_len;
-		}
-	}
-}
-
-/*
- * feed the key generator, must be called at splimp() or higher.
- */
-static int
-ubsec_feed2(struct ubsec_softc *sc)
-{
-	struct ubsec_q2 *q;
-
-	while (!SIMPLEQ_EMPTY(&sc->sc_queue2)) {
-		if (READ_REG(sc, BS_STAT) & BS_STAT_MCR2_FULL)
-			break;
-		q = SIMPLEQ_FIRST(&sc->sc_queue2);
-
-		ubsec_dma_sync(&q->q_mcr,
-		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
-		ubsec_dma_sync(&q->q_ctx, BUS_DMASYNC_PREWRITE);
-
-		WRITE_REG(sc, BS_MCR2, q->q_mcr.dma_paddr);
-		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue2, q_next);
-		--sc->sc_nqueue2;
-		SIMPLEQ_INSERT_TAIL(&sc->sc_qchip2, q, q_next);
-	}
-	return (0);
-}
-
-/*
- * Callback for handling random numbers
- */
-static void
-ubsec_callback2(struct ubsec_softc *sc, struct ubsec_q2 *q)
-{
-	struct cryptkop *krp;
-	struct ubsec_ctx_keyop *ctx;
-
-	ctx = (struct ubsec_ctx_keyop *)q->q_ctx.dma_vaddr;
-	ubsec_dma_sync(&q->q_ctx, BUS_DMASYNC_POSTWRITE);
-
-	switch (q->q_type) {
-#ifndef UBSEC_NO_RNG
-	case UBS_CTXOP_RNGBYPASS: {
-		struct ubsec_q2_rng *rng = (struct ubsec_q2_rng *)q;
-
-		ubsec_dma_sync(&rng->rng_buf, BUS_DMASYNC_POSTREAD);
-		(*sc->sc_harvest)(sc->sc_rndtest,
-			rng->rng_buf.dma_vaddr,
-			UBSEC_RNG_BUFSIZ*sizeof (u_int32_t));
-		rng->rng_used = 0;
-		callout_reset(&sc->sc_rngto, sc->sc_rnghz, ubsec_rng, sc);
-		break;
-	}
-#endif
-	case UBS_CTXOP_MODEXP: {
-		struct ubsec_q2_modexp *me = (struct ubsec_q2_modexp *)q;
-		u_int rlen, clen;
-
-		krp = me->me_krp;
-		rlen = (me->me_modbits + 7) / 8;
-		clen = (krp->krp_param[krp->krp_iparams].crp_nbits + 7) / 8;
-
-		ubsec_dma_sync(&me->me_M, BUS_DMASYNC_POSTWRITE);
-		ubsec_dma_sync(&me->me_E, BUS_DMASYNC_POSTWRITE);
-		ubsec_dma_sync(&me->me_C, BUS_DMASYNC_POSTREAD);
-		ubsec_dma_sync(&me->me_epb, BUS_DMASYNC_POSTWRITE);
-
-		if (clen < rlen)
-			krp->krp_status = E2BIG;
-		else {
-			if (sc->sc_flags & UBS_FLAGS_HWNORM) {
-				bzero(krp->krp_param[krp->krp_iparams].crp_p,
-				    (krp->krp_param[krp->krp_iparams].crp_nbits
-					+ 7) / 8);
-				bcopy(me->me_C.dma_vaddr,
-				    krp->krp_param[krp->krp_iparams].crp_p,
-				    (me->me_modbits + 7) / 8);
-			} else
-				ubsec_kshift_l(me->me_shiftbits,
-				    me->me_C.dma_vaddr, me->me_normbits,
-				    krp->krp_param[krp->krp_iparams].crp_p,
-				    krp->krp_param[krp->krp_iparams].crp_nbits);
-		}
-
-		crypto_kdone(krp);
-
-		/* bzero all potentially sensitive data */
-		bzero(me->me_E.dma_vaddr, me->me_E.dma_size);
-		bzero(me->me_M.dma_vaddr, me->me_M.dma_size);
-		bzero(me->me_C.dma_vaddr, me->me_C.dma_size);
-		bzero(me->me_q.q_ctx.dma_vaddr, me->me_q.q_ctx.dma_size);
-
-		/* Can't free here, so put us on the free list. */
-		SIMPLEQ_INSERT_TAIL(&sc->sc_q2free, &me->me_q, q_next);
-		break;
-	}
-	case UBS_CTXOP_RSAPRIV: {
-		struct ubsec_q2_rsapriv *rp = (struct ubsec_q2_rsapriv *)q;
-		u_int len;
-
-		krp = rp->rpr_krp;
-		ubsec_dma_sync(&rp->rpr_msgin, BUS_DMASYNC_POSTWRITE);
-		ubsec_dma_sync(&rp->rpr_msgout, BUS_DMASYNC_POSTREAD);
-
-		len = (krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT].crp_nbits + 7) / 8;
-		bcopy(rp->rpr_msgout.dma_vaddr,
-		    krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT].crp_p, len);
-
-		crypto_kdone(krp);
-
-		bzero(rp->rpr_msgin.dma_vaddr, rp->rpr_msgin.dma_size);
-		bzero(rp->rpr_msgout.dma_vaddr, rp->rpr_msgout.dma_size);
-		bzero(rp->rpr_q.q_ctx.dma_vaddr, rp->rpr_q.q_ctx.dma_size);
-
-		/* Can't free here, so put us on the free list. */
-		SIMPLEQ_INSERT_TAIL(&sc->sc_q2free, &rp->rpr_q, q_next);
-		break;
-	}
-	default:
-		device_printf(sc->sc_dev, "unknown ctx op: %x\n",
-		    letoh16(ctx->ctx_op));
-		break;
-	}
-}
-
-#ifndef UBSEC_NO_RNG
-static void
-ubsec_rng(void *vsc)
-{
-	struct ubsec_softc *sc = vsc;
-	struct ubsec_q2_rng *rng = &sc->sc_rng;
-	struct ubsec_mcr *mcr;
-	struct ubsec_ctx_rngbypass *ctx;
-
-	crit_enter();
-	if (rng->rng_used) {
-		crit_exit();
-		return;
-	}
-	sc->sc_nqueue2++;
-	if (sc->sc_nqueue2 >= UBS_MAX_NQUEUE)
-		goto out;
-
-	mcr = (struct ubsec_mcr *)rng->rng_q.q_mcr.dma_vaddr;
-	ctx = (struct ubsec_ctx_rngbypass *)rng->rng_q.q_ctx.dma_vaddr;
-
-	mcr->mcr_pkts = htole16(1);
-	mcr->mcr_flags = 0;
-	mcr->mcr_cmdctxp = htole32(rng->rng_q.q_ctx.dma_paddr);
-	mcr->mcr_ipktbuf.pb_addr = mcr->mcr_ipktbuf.pb_next = 0;
-	mcr->mcr_ipktbuf.pb_len = 0;
-	mcr->mcr_reserved = mcr->mcr_pktlen = 0;
-	mcr->mcr_opktbuf.pb_addr = htole32(rng->rng_buf.dma_paddr);
-	mcr->mcr_opktbuf.pb_len = htole32(((sizeof(u_int32_t) * UBSEC_RNG_BUFSIZ)) &
-	    UBS_PKTBUF_LEN);
-	mcr->mcr_opktbuf.pb_next = 0;
-
-	ctx->rbp_len = htole16(sizeof(struct ubsec_ctx_rngbypass));
-	ctx->rbp_op = htole16(UBS_CTXOP_RNGBYPASS);
-	rng->rng_q.q_type = UBS_CTXOP_RNGBYPASS;
-
-	ubsec_dma_sync(&rng->rng_buf, BUS_DMASYNC_PREREAD);
-
-	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &rng->rng_q, q_next);
-	rng->rng_used = 1;
-	ubsec_feed2(sc);
-	ubsecstats.hst_rng++;
-	crit_exit();
-
-	return;
-
-out:
-	/*
-	 * Something weird happened, generate our own call back.
-	 */
-	sc->sc_nqueue2--;
-	crit_exit();
-	callout_reset(&sc->sc_rngto, sc->sc_rnghz, ubsec_rng, sc);
-}
-#endif /* UBSEC_NO_RNG */
-
-static void
-ubsec_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
-{
-	bus_addr_t *paddr = (bus_addr_t*) arg;
-	*paddr = segs->ds_addr;
-}
-
-static int
-ubsec_dma_malloc(
-	struct ubsec_softc *sc,
-	bus_size_t size,
-	struct ubsec_dma_alloc *dma,
-	int mapflags
-)
-{
-	int r;
-
-	/* XXX could specify sc_dmat as parent but that just adds overhead */
-	r = bus_dma_tag_create(NULL,			/* parent */
-			       1, 0,			/* alignment, bounds */
-			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
-			       BUS_SPACE_MAXADDR,	/* highaddr */
-			       size,			/* maxsize */
-			       1,			/* nsegments */
-			       size,			/* maxsegsize */
-			       BUS_DMA_ALLOCNOW,	/* flags */
-			       &dma->dma_tag);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "ubsec_dma_malloc: "
-			"bus_dma_tag_create failed; error %u\n", r);
-		goto fail_0;
-	}
-
-	r = bus_dmamap_create(dma->dma_tag, BUS_DMA_NOWAIT, &dma->dma_map);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "ubsec_dma_malloc: "
-			"bus_dmamap_create failed; error %u\n", r);
-		goto fail_1;
-	}
-
-	r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr,
-			     BUS_DMA_NOWAIT, &dma->dma_map);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "ubsec_dma_malloc: "
-			"bus_dmammem_alloc failed; size %ju, error %u\n",
-			(intmax_t)size, r);
-		goto fail_2;
-	}
-
-	r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
-		            size,
-			    ubsec_dmamap_cb,
-			    &dma->dma_paddr,
-			    mapflags | BUS_DMA_NOWAIT);
-	if (r != 0) {
-		device_printf(sc->sc_dev, "ubsec_dma_malloc: "
-			"bus_dmamap_load failed; error %u\n", r);
-		goto fail_3;
-	}
-
-	dma->dma_size = size;
-	return (0);
-
-fail_3:
-	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
-fail_2:
-	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
-fail_1:
-	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
-	bus_dma_tag_destroy(dma->dma_tag);
-fail_0:
-	dma->dma_map = NULL;
-	dma->dma_tag = NULL;
-	return (r);
-}
-
-static void
-ubsec_dma_free(struct ubsec_softc *sc, struct ubsec_dma_alloc *dma)
-{
-	bus_dmamap_unload(dma->dma_tag, dma->dma_map);
-	bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
-	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
-	bus_dma_tag_destroy(dma->dma_tag);
-}
-
-/*
- * Resets the board.  Values in the regesters are left as is
- * from the reset (i.e. initial values are assigned elsewhere).
- */
-static void
-ubsec_reset_board(struct ubsec_softc *sc)
-{
-    volatile u_int32_t ctrl;
-
-    ctrl = READ_REG(sc, BS_CTRL);
-    ctrl |= BS_CTRL_RESET;
-    WRITE_REG(sc, BS_CTRL, ctrl);
-
-    /*
-     * Wait aprox. 30 PCI clocks = 900 ns = 0.9 us
-     */
-    DELAY(10);
-}
-
-/*
- * Init Broadcom registers
- */
-static void
-ubsec_init_board(struct ubsec_softc *sc)
-{
-	u_int32_t ctrl;
-
-	ctrl = READ_REG(sc, BS_CTRL);
-	ctrl &= ~(BS_CTRL_BE32 | BS_CTRL_BE64);
-	ctrl |= BS_CTRL_LITTLE_ENDIAN | BS_CTRL_MCR1INT;
-
-	if (sc->sc_flags & (UBS_FLAGS_KEY|UBS_FLAGS_RNG))
-		ctrl |= BS_CTRL_MCR2INT;
-	else
-		ctrl &= ~BS_CTRL_MCR2INT;
-
-	if (sc->sc_flags & UBS_FLAGS_HWNORM)
-		ctrl &= ~BS_CTRL_SWNORM;
-
-	WRITE_REG(sc, BS_CTRL, ctrl);
-}
-
-/*
- * Init Broadcom PCI registers
- */
-static void
-ubsec_init_pciregs(device_t dev)
-{
-#if 0
-	u_int32_t misc;
-
-	misc = pci_conf_read(pc, pa->pa_tag, BS_RTY_TOUT);
-	misc = (misc & ~(UBS_PCI_RTY_MASK << UBS_PCI_RTY_SHIFT))
-	    | ((UBS_DEF_RTY & 0xff) << UBS_PCI_RTY_SHIFT);
-	misc = (misc & ~(UBS_PCI_TOUT_MASK << UBS_PCI_TOUT_SHIFT))
-	    | ((UBS_DEF_TOUT & 0xff) << UBS_PCI_TOUT_SHIFT);
-	pci_conf_write(pc, pa->pa_tag, BS_RTY_TOUT, misc);
-#endif
-
-	/*
-	 * This will set the cache line size to 1, this will
-	 * force the BCM58xx chip just to do burst read/writes.
-	 * Cache line read/writes are to slow
-	 */
-	pci_write_config(dev, PCIR_CACHELNSZ, UBS_DEF_CACHELINE, 1);
-}
-
-/*
- * Clean up after a chip crash.
- * It is assumed that the caller in splimp()
- */
-static void
-ubsec_cleanchip(struct ubsec_softc *sc)
-{
-	struct ubsec_q *q;
-
-	while (!SIMPLEQ_EMPTY(&sc->sc_qchip)) {
-		q = SIMPLEQ_FIRST(&sc->sc_qchip);
-		SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip, q_next);
-		ubsec_free_q(sc, q);
-	}
-	sc->sc_nqchip = 0;
-}
-
-/*
- * free a ubsec_q
- * It is assumed that the caller is within spimp()
- */
-static int
-ubsec_free_q(struct ubsec_softc *sc, struct ubsec_q *q)
-{
-	struct ubsec_q *q2;
-	struct cryptop *crp;
-	int npkts;
-	int i;
-
-	npkts = q->q_nstacked_mcrs;
-
-	for (i = 0; i < npkts; i++) {
-		if(q->q_stacked_mcr[i]) {
-			q2 = q->q_stacked_mcr[i];
-
-			if ((q2->q_dst_m != NULL) && (q2->q_src_m != q2->q_dst_m)) 
-				m_freem(q2->q_dst_m);
-
-			crp = (struct cryptop *)q2->q_crp;
-			
-			SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q2, q_next);
-			
-			crp->crp_etype = EFAULT;
-			crypto_done(crp);
-		} else {
-			break;
-		}
-	}
-
-	/*
-	 * Free header MCR
-	 */
-	if ((q->q_dst_m != NULL) && (q->q_src_m != q->q_dst_m))
-		m_freem(q->q_dst_m);
-
-	crp = (struct cryptop *)q->q_crp;
-	
-	SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
-	
-	crp->crp_etype = EFAULT;
-	crypto_done(crp);
-	return(0);
-}
-
-/*
- * Routine to reset the chip and clean up.
- * It is assumed that the caller is in splimp()
- */
-static void
-ubsec_totalreset(struct ubsec_softc *sc)
-{
-	ubsec_reset_board(sc);
-	ubsec_init_board(sc);
-	ubsec_cleanchip(sc);
-}
-
-static int
-ubsec_dmamap_aligned(struct ubsec_operand *op)
-{
-	int i;
-
-	for (i = 0; i < op->nsegs; i++) {
-		if (op->segs[i].ds_addr & 3)
-			return (0);
-		if ((i != (op->nsegs - 1)) &&
-		    (op->segs[i].ds_len & 3))
-			return (0);
-	}
-	return (1);
-}
-
-static void
-ubsec_kfree(struct ubsec_softc *sc, struct ubsec_q2 *q)
-{
-	switch (q->q_type) {
-	case UBS_CTXOP_MODEXP: {
-		struct ubsec_q2_modexp *me = (struct ubsec_q2_modexp *)q;
-
-		ubsec_dma_free(sc, &me->me_q.q_mcr);
-		ubsec_dma_free(sc, &me->me_q.q_ctx);
-		ubsec_dma_free(sc, &me->me_M);
-		ubsec_dma_free(sc, &me->me_E);
-		ubsec_dma_free(sc, &me->me_C);
-		ubsec_dma_free(sc, &me->me_epb);
-		kfree(me, M_DEVBUF);
-		break;
-	}
-	case UBS_CTXOP_RSAPRIV: {
-		struct ubsec_q2_rsapriv *rp = (struct ubsec_q2_rsapriv *)q;
-
-		ubsec_dma_free(sc, &rp->rpr_q.q_mcr);
-		ubsec_dma_free(sc, &rp->rpr_q.q_ctx);
-		ubsec_dma_free(sc, &rp->rpr_msgin);
-		ubsec_dma_free(sc, &rp->rpr_msgout);
-		kfree(rp, M_DEVBUF);
-		break;
-	}
-	default:
-		device_printf(sc->sc_dev, "invalid kfree 0x%x\n", q->q_type);
-		break;
-	}
-}
-
-static int
-ubsec_kprocess(device_t dev, struct cryptkop *krp, int hint)
-{
-	struct ubsec_softc *sc = device_get_softc(dev);
-	int r;
-
-	if (krp == NULL || krp->krp_callback == NULL)
-		return (EINVAL);
-
-	while (!SIMPLEQ_EMPTY(&sc->sc_q2free)) {
-		struct ubsec_q2 *q;
-
-		q = SIMPLEQ_FIRST(&sc->sc_q2free);
-		SIMPLEQ_REMOVE_HEAD(&sc->sc_q2free, q_next);
-		ubsec_kfree(sc, q);
-	}
-
-	switch (krp->krp_op) {
-	case CRK_MOD_EXP:
-		if (sc->sc_flags & UBS_FLAGS_HWNORM)
-			r = ubsec_kprocess_modexp_hw(sc, krp, hint);
-		else
-			r = ubsec_kprocess_modexp_sw(sc, krp, hint);
-		break;
-	case CRK_MOD_EXP_CRT:
-		return (ubsec_kprocess_rsapriv(sc, krp, hint));
-	default:
-		device_printf(sc->sc_dev, "kprocess: invalid op 0x%x\n",
-		    krp->krp_op);
-		krp->krp_status = EOPNOTSUPP;
-		crypto_kdone(krp);
-		return (0);
-	}
-	return (0);			/* silence compiler */
-}
-
-/*
- * Start computation of cr[C] = (cr[M] ^ cr[E]) mod cr[N] (sw normalization)
- */
-static int
-ubsec_kprocess_modexp_sw(struct ubsec_softc *sc, struct cryptkop *krp, int hint)
-{
-	struct ubsec_q2_modexp *me;
-	struct ubsec_mcr *mcr;
-	struct ubsec_ctx_modexp *ctx;
-	struct ubsec_pktbuf *epb;
-	int err = 0;
-	u_int nbits, normbits, mbits, shiftbits, ebits;
-
-	me = kmalloc(sizeof *me, M_DEVBUF, M_INTWAIT | M_ZERO);
-	me->me_krp = krp;
-	me->me_q.q_type = UBS_CTXOP_MODEXP;
-
-	nbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_N]);
-	if (nbits <= 512)
-		normbits = 512;
-	else if (nbits <= 768)
-		normbits = 768;
-	else if (nbits <= 1024)
-		normbits = 1024;
-	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 1536)
-		normbits = 1536;
-	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 2048)
-		normbits = 2048;
-	else {
-		err = E2BIG;
-		goto errout;
-	}
-
-	shiftbits = normbits - nbits;
-
-	me->me_modbits = nbits;
-	me->me_shiftbits = shiftbits;
-	me->me_normbits = normbits;
-
-	/* Sanity check: result bits must be >= true modulus bits. */
-	if (krp->krp_param[krp->krp_iparams].crp_nbits < nbits) {
-		err = ERANGE;
-		goto errout;
-	}
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
-	    &me->me_q.q_mcr, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	mcr = (struct ubsec_mcr *)me->me_q.q_mcr.dma_vaddr;
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_modexp),
-	    &me->me_q.q_ctx, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-
-	mbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_M]);
-	if (mbits > nbits) {
-		err = E2BIG;
-		goto errout;
-	}
-	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_M, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	ubsec_kshift_r(shiftbits,
-	    krp->krp_param[UBS_MODEXP_PAR_M].crp_p, mbits,
-	    me->me_M.dma_vaddr, normbits);
-
-	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_C, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	bzero(me->me_C.dma_vaddr, me->me_C.dma_size);
-
-	ebits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_E]);
-	if (ebits > nbits) {
-		err = E2BIG;
-		goto errout;
-	}
-	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_E, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	ubsec_kshift_r(shiftbits,
-	    krp->krp_param[UBS_MODEXP_PAR_E].crp_p, ebits,
-	    me->me_E.dma_vaddr, normbits);
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_pktbuf),
-	    &me->me_epb, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	epb = (struct ubsec_pktbuf *)me->me_epb.dma_vaddr;
-	epb->pb_addr = htole32(me->me_E.dma_paddr);
-	epb->pb_next = 0;
-	epb->pb_len = htole32(normbits / 8);
-
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug) {
-		kprintf("Epb ");
-		ubsec_dump_pb(epb);
-	}
-#endif
-
-	mcr->mcr_pkts = htole16(1);
-	mcr->mcr_flags = 0;
-	mcr->mcr_cmdctxp = htole32(me->me_q.q_ctx.dma_paddr);
-	mcr->mcr_reserved = 0;
-	mcr->mcr_pktlen = 0;
-
-	mcr->mcr_ipktbuf.pb_addr = htole32(me->me_M.dma_paddr);
-	mcr->mcr_ipktbuf.pb_len = htole32(normbits / 8);
-	mcr->mcr_ipktbuf.pb_next = htole32(me->me_epb.dma_paddr);
-
-	mcr->mcr_opktbuf.pb_addr = htole32(me->me_C.dma_paddr);
-	mcr->mcr_opktbuf.pb_next = 0;
-	mcr->mcr_opktbuf.pb_len = htole32(normbits / 8);
-
-#ifdef DIAGNOSTIC
-	/* Misaligned output buffer will hang the chip. */
-	if ((letoh32(mcr->mcr_opktbuf.pb_addr) & 3) != 0)
-		panic("%s: modexp invalid addr 0x%x",
-		    device_get_nameunit(sc->sc_dev),
-		    letoh32(mcr->mcr_opktbuf.pb_addr));
-	if ((letoh32(mcr->mcr_opktbuf.pb_len) & 3) != 0)
-		panic("%s: modexp invalid len 0x%x",
-		    device_get_nameunit(sc->sc_dev),
-		    letoh32(mcr->mcr_opktbuf.pb_len));
-#endif
-
-	ctx = (struct ubsec_ctx_modexp *)me->me_q.q_ctx.dma_vaddr;
-	bzero(ctx, sizeof(*ctx));
-	ubsec_kshift_r(shiftbits,
-	    krp->krp_param[UBS_MODEXP_PAR_N].crp_p, nbits,
-	    ctx->me_N, normbits);
-	ctx->me_len = htole16((normbits / 8) + (4 * sizeof(u_int16_t)));
-	ctx->me_op = htole16(UBS_CTXOP_MODEXP);
-	ctx->me_E_len = htole16(nbits);
-	ctx->me_N_len = htole16(nbits);
-
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug) {
-		ubsec_dump_mcr(mcr);
-		ubsec_dump_ctx2((struct ubsec_ctx_keyop *)ctx);
-	}
-#endif
-
-	/*
-	 * ubsec_feed2 will sync mcr and ctx, we just need to sync
-	 * everything else.
-	 */
-	ubsec_dma_sync(&me->me_M, BUS_DMASYNC_PREWRITE);
-	ubsec_dma_sync(&me->me_E, BUS_DMASYNC_PREWRITE);
-	ubsec_dma_sync(&me->me_C, BUS_DMASYNC_PREREAD);
-	ubsec_dma_sync(&me->me_epb, BUS_DMASYNC_PREWRITE);
-
-	/* Enqueue and we're done... */
-	crit_enter();
-	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &me->me_q, q_next);
-	ubsec_feed2(sc);
-	ubsecstats.hst_modexp++;
-	crit_exit();
-
-	return (0);
-
-errout:
-	if (me != NULL) {
-		if (me->me_q.q_mcr.dma_map != NULL)
-			ubsec_dma_free(sc, &me->me_q.q_mcr);
-		if (me->me_q.q_ctx.dma_map != NULL) {
-			bzero(me->me_q.q_ctx.dma_vaddr, me->me_q.q_ctx.dma_size);
-			ubsec_dma_free(sc, &me->me_q.q_ctx);
-		}
-		if (me->me_M.dma_map != NULL) {
-			bzero(me->me_M.dma_vaddr, me->me_M.dma_size);
-			ubsec_dma_free(sc, &me->me_M);
-		}
-		if (me->me_E.dma_map != NULL) {
-			bzero(me->me_E.dma_vaddr, me->me_E.dma_size);
-			ubsec_dma_free(sc, &me->me_E);
-		}
-		if (me->me_C.dma_map != NULL) {
-			bzero(me->me_C.dma_vaddr, me->me_C.dma_size);
-			ubsec_dma_free(sc, &me->me_C);
-		}
-		if (me->me_epb.dma_map != NULL)
-			ubsec_dma_free(sc, &me->me_epb);
-		kfree(me, M_DEVBUF);
-	}
-	krp->krp_status = err;
-	crypto_kdone(krp);
-	return (0);
-}
-
-/*
- * Start computation of cr[C] = (cr[M] ^ cr[E]) mod cr[N] (hw normalization)
- */
-static int
-ubsec_kprocess_modexp_hw(struct ubsec_softc *sc, struct cryptkop *krp, int hint)
-{
-	struct ubsec_q2_modexp *me;
-	struct ubsec_mcr *mcr;
-	struct ubsec_ctx_modexp *ctx;
-	struct ubsec_pktbuf *epb;
-	int err = 0;
-	u_int nbits, normbits, mbits, shiftbits, ebits;
-
-	me = kmalloc(sizeof *me, M_DEVBUF, M_INTWAIT | M_ZERO);
-	me->me_krp = krp;
-	me->me_q.q_type = UBS_CTXOP_MODEXP;
-
-	nbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_N]);
-	if (nbits <= 512)
-		normbits = 512;
-	else if (nbits <= 768)
-		normbits = 768;
-	else if (nbits <= 1024)
-		normbits = 1024;
-	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 1536)
-		normbits = 1536;
-	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 2048)
-		normbits = 2048;
-	else {
-		err = E2BIG;
-		goto errout;
-	}
-
-	shiftbits = normbits - nbits;
-
-	/* XXX ??? */
-	me->me_modbits = nbits;
-	me->me_shiftbits = shiftbits;
-	me->me_normbits = normbits;
-
-	/* Sanity check: result bits must be >= true modulus bits. */
-	if (krp->krp_param[krp->krp_iparams].crp_nbits < nbits) {
-		err = ERANGE;
-		goto errout;
-	}
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
-	    &me->me_q.q_mcr, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	mcr = (struct ubsec_mcr *)me->me_q.q_mcr.dma_vaddr;
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_modexp),
-	    &me->me_q.q_ctx, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-
-	mbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_M]);
-	if (mbits > nbits) {
-		err = E2BIG;
-		goto errout;
-	}
-	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_M, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	bzero(me->me_M.dma_vaddr, normbits / 8);
-	bcopy(krp->krp_param[UBS_MODEXP_PAR_M].crp_p,
-	    me->me_M.dma_vaddr, (mbits + 7) / 8);
-
-	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_C, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	bzero(me->me_C.dma_vaddr, me->me_C.dma_size);
-
-	ebits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_E]);
-	if (ebits > nbits) {
-		err = E2BIG;
-		goto errout;
-	}
-	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_E, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	bzero(me->me_E.dma_vaddr, normbits / 8);
-	bcopy(krp->krp_param[UBS_MODEXP_PAR_E].crp_p,
-	    me->me_E.dma_vaddr, (ebits + 7) / 8);
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_pktbuf),
-	    &me->me_epb, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	epb = (struct ubsec_pktbuf *)me->me_epb.dma_vaddr;
-	epb->pb_addr = htole32(me->me_E.dma_paddr);
-	epb->pb_next = 0;
-	epb->pb_len = htole32((ebits + 7) / 8);
-
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug) {
-		kprintf("Epb ");
-		ubsec_dump_pb(epb);
-	}
-#endif
-
-	mcr->mcr_pkts = htole16(1);
-	mcr->mcr_flags = 0;
-	mcr->mcr_cmdctxp = htole32(me->me_q.q_ctx.dma_paddr);
-	mcr->mcr_reserved = 0;
-	mcr->mcr_pktlen = 0;
-
-	mcr->mcr_ipktbuf.pb_addr = htole32(me->me_M.dma_paddr);
-	mcr->mcr_ipktbuf.pb_len = htole32(normbits / 8);
-	mcr->mcr_ipktbuf.pb_next = htole32(me->me_epb.dma_paddr);
-
-	mcr->mcr_opktbuf.pb_addr = htole32(me->me_C.dma_paddr);
-	mcr->mcr_opktbuf.pb_next = 0;
-	mcr->mcr_opktbuf.pb_len = htole32(normbits / 8);
-
-#ifdef DIAGNOSTIC
-	/* Misaligned output buffer will hang the chip. */
-	if ((letoh32(mcr->mcr_opktbuf.pb_addr) & 3) != 0)
-		panic("%s: modexp invalid addr 0x%x",
-		    device_get_nameunit(sc->sc_dev),
-		    letoh32(mcr->mcr_opktbuf.pb_addr));
-	if ((letoh32(mcr->mcr_opktbuf.pb_len) & 3) != 0)
-		panic("%s: modexp invalid len 0x%x",
-		    device_get_nameunit(sc->sc_dev),
-		    letoh32(mcr->mcr_opktbuf.pb_len));
-#endif
-
-	ctx = (struct ubsec_ctx_modexp *)me->me_q.q_ctx.dma_vaddr;
-	bzero(ctx, sizeof(*ctx));
-	bcopy(krp->krp_param[UBS_MODEXP_PAR_N].crp_p, ctx->me_N,
-	    (nbits + 7) / 8);
-	ctx->me_len = htole16((normbits / 8) + (4 * sizeof(u_int16_t)));
-	ctx->me_op = htole16(UBS_CTXOP_MODEXP);
-	ctx->me_E_len = htole16(ebits);
-	ctx->me_N_len = htole16(nbits);
-
-#ifdef UBSEC_DEBUG
-	if (ubsec_debug) {
-		ubsec_dump_mcr(mcr);
-		ubsec_dump_ctx2((struct ubsec_ctx_keyop *)ctx);
-	}
-#endif
-
-	/*
-	 * ubsec_feed2 will sync mcr and ctx, we just need to sync
-	 * everything else.
-	 */
-	ubsec_dma_sync(&me->me_M, BUS_DMASYNC_PREWRITE);
-	ubsec_dma_sync(&me->me_E, BUS_DMASYNC_PREWRITE);
-	ubsec_dma_sync(&me->me_C, BUS_DMASYNC_PREREAD);
-	ubsec_dma_sync(&me->me_epb, BUS_DMASYNC_PREWRITE);
-
-	/* Enqueue and we're done... */
-	crit_enter();
-	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &me->me_q, q_next);
-	ubsec_feed2(sc);
-	crit_exit();
-
-	return (0);
-
-errout:
-	if (me != NULL) {
-		if (me->me_q.q_mcr.dma_map != NULL)
-			ubsec_dma_free(sc, &me->me_q.q_mcr);
-		if (me->me_q.q_ctx.dma_map != NULL) {
-			bzero(me->me_q.q_ctx.dma_vaddr, me->me_q.q_ctx.dma_size);
-			ubsec_dma_free(sc, &me->me_q.q_ctx);
-		}
-		if (me->me_M.dma_map != NULL) {
-			bzero(me->me_M.dma_vaddr, me->me_M.dma_size);
-			ubsec_dma_free(sc, &me->me_M);
-		}
-		if (me->me_E.dma_map != NULL) {
-			bzero(me->me_E.dma_vaddr, me->me_E.dma_size);
-			ubsec_dma_free(sc, &me->me_E);
-		}
-		if (me->me_C.dma_map != NULL) {
-			bzero(me->me_C.dma_vaddr, me->me_C.dma_size);
-			ubsec_dma_free(sc, &me->me_C);
-		}
-		if (me->me_epb.dma_map != NULL)
-			ubsec_dma_free(sc, &me->me_epb);
-		kfree(me, M_DEVBUF);
-	}
-	krp->krp_status = err;
-	crypto_kdone(krp);
-	return (0);
-}
-
-static int
-ubsec_kprocess_rsapriv(struct ubsec_softc *sc, struct cryptkop *krp, int hint)
-{
-	struct ubsec_q2_rsapriv *rp = NULL;
-	struct ubsec_mcr *mcr;
-	struct ubsec_ctx_rsapriv *ctx;
-	int err = 0;
-	u_int padlen, msglen;
-
-	msglen = ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_P]);
-	padlen = ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_Q]);
-	if (msglen > padlen)
-		padlen = msglen;
-
-	if (padlen <= 256)
-		padlen = 256;
-	else if (padlen <= 384)
-		padlen = 384;
-	else if (padlen <= 512)
-		padlen = 512;
-	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && padlen <= 768)
-		padlen = 768;
-	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && padlen <= 1024)
-		padlen = 1024;
-	else {
-		err = E2BIG;
-		goto errout;
-	}
-
-	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_DP]) > padlen) {
-		err = E2BIG;
-		goto errout;
-	}
-
-	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_DQ]) > padlen) {
-		err = E2BIG;
-		goto errout;
-	}
-
-	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_PINV]) > padlen) {
-		err = E2BIG;
-		goto errout;
-	}
-
-	rp = kmalloc(sizeof *rp, M_DEVBUF, M_INTWAIT | M_ZERO);
-	rp->rpr_krp = krp;
-	rp->rpr_q.q_type = UBS_CTXOP_RSAPRIV;
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
-	    &rp->rpr_q.q_mcr, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	mcr = (struct ubsec_mcr *)rp->rpr_q.q_mcr.dma_vaddr;
-
-	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_rsapriv),
-	    &rp->rpr_q.q_ctx, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	ctx = (struct ubsec_ctx_rsapriv *)rp->rpr_q.q_ctx.dma_vaddr;
-	bzero(ctx, sizeof *ctx);
-
-	/* Copy in p */
-	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_P].crp_p,
-	    &ctx->rpr_buf[0 * (padlen / 8)],
-	    (krp->krp_param[UBS_RSAPRIV_PAR_P].crp_nbits + 7) / 8);
-
-	/* Copy in q */
-	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_Q].crp_p,
-	    &ctx->rpr_buf[1 * (padlen / 8)],
-	    (krp->krp_param[UBS_RSAPRIV_PAR_Q].crp_nbits + 7) / 8);
-
-	/* Copy in dp */
-	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_DP].crp_p,
-	    &ctx->rpr_buf[2 * (padlen / 8)],
-	    (krp->krp_param[UBS_RSAPRIV_PAR_DP].crp_nbits + 7) / 8);
-
-	/* Copy in dq */
-	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_DQ].crp_p,
-	    &ctx->rpr_buf[3 * (padlen / 8)],
-	    (krp->krp_param[UBS_RSAPRIV_PAR_DQ].crp_nbits + 7) / 8);
-
-	/* Copy in pinv */
-	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_PINV].crp_p,
-	    &ctx->rpr_buf[4 * (padlen / 8)],
-	    (krp->krp_param[UBS_RSAPRIV_PAR_PINV].crp_nbits + 7) / 8);
-
-	msglen = padlen * 2;
-
-	/* Copy in input message (aligned buffer/length). */
-	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_MSGIN]) > msglen) {
-		/* Is this likely? */
-		err = E2BIG;
-		goto errout;
-	}
-	if (ubsec_dma_malloc(sc, (msglen + 7) / 8, &rp->rpr_msgin, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	bzero(rp->rpr_msgin.dma_vaddr, (msglen + 7) / 8);
-	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_MSGIN].crp_p,
-	    rp->rpr_msgin.dma_vaddr,
-	    (krp->krp_param[UBS_RSAPRIV_PAR_MSGIN].crp_nbits + 7) / 8);
-
-	/* Prepare space for output message (aligned buffer/length). */
-	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT]) < msglen) {
-		/* Is this likely? */
-		err = E2BIG;
-		goto errout;
-	}
-	if (ubsec_dma_malloc(sc, (msglen + 7) / 8, &rp->rpr_msgout, 0)) {
-		err = ENOMEM;
-		goto errout;
-	}
-	bzero(rp->rpr_msgout.dma_vaddr, (msglen + 7) / 8);
-
-	mcr->mcr_pkts = htole16(1);
-	mcr->mcr_flags = 0;
-	mcr->mcr_cmdctxp = htole32(rp->rpr_q.q_ctx.dma_paddr);
-	mcr->mcr_ipktbuf.pb_addr = htole32(rp->rpr_msgin.dma_paddr);
-	mcr->mcr_ipktbuf.pb_next = 0;
-	mcr->mcr_ipktbuf.pb_len = htole32(rp->rpr_msgin.dma_size);
-	mcr->mcr_reserved = 0;
-	mcr->mcr_pktlen = htole16(msglen);
-	mcr->mcr_opktbuf.pb_addr = htole32(rp->rpr_msgout.dma_paddr);
-	mcr->mcr_opktbuf.pb_next = 0;
-	mcr->mcr_opktbuf.pb_len = htole32(rp->rpr_msgout.dma_size);
-
-#ifdef DIAGNOSTIC
-	if (rp->rpr_msgin.dma_paddr & 3 || rp->rpr_msgin.dma_size & 3) {
-		panic("%s: rsapriv: invalid msgin %x(0x%jx)",
-		    device_get_nameunit(sc->sc_dev),
-		    rp->rpr_msgin.dma_paddr,
-		    (uintmax_t)rp->rpr_msgin.dma_size);
-	}
-	if (rp->rpr_msgout.dma_paddr & 3 || rp->rpr_msgout.dma_size & 3) {
-		panic("%s: rsapriv: invalid msgout %x(0x%jx)",
-		    device_get_nameunit(sc->sc_dev),
-		    rp->rpr_msgout.dma_paddr,
-		    (uintmax_t)rp->rpr_msgout.dma_size);
-	}
-#endif
-
-	ctx->rpr_len = (sizeof(u_int16_t) * 4) + (5 * (padlen / 8));
-	ctx->rpr_op = htole16(UBS_CTXOP_RSAPRIV);
-	ctx->rpr_q_len = htole16(padlen);
-	ctx->rpr_p_len = htole16(padlen);
-
-	/*
-	 * ubsec_feed2 will sync mcr and ctx, we just need to sync
-	 * everything else.
-	 */
-	ubsec_dma_sync(&rp->rpr_msgin, BUS_DMASYNC_PREWRITE);
-	ubsec_dma_sync(&rp->rpr_msgout, BUS_DMASYNC_PREREAD);
-
-	/* Enqueue and we're done... */
-	crit_enter();
-	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &rp->rpr_q, q_next);
-	ubsec_feed2(sc);
-	ubsecstats.hst_modexpcrt++;
-	crit_exit();
-	return (0);
-
-errout:
-	if (rp != NULL) {
-		if (rp->rpr_q.q_mcr.dma_map != NULL)
-			ubsec_dma_free(sc, &rp->rpr_q.q_mcr);
-		if (rp->rpr_msgin.dma_map != NULL) {
-			bzero(rp->rpr_msgin.dma_vaddr, rp->rpr_msgin.dma_size);
-			ubsec_dma_free(sc, &rp->rpr_msgin);
-		}
-		if (rp->rpr_msgout.dma_map != NULL) {
-			bzero(rp->rpr_msgout.dma_vaddr, rp->rpr_msgout.dma_size);
-			ubsec_dma_free(sc, &rp->rpr_msgout);
-		}
-		kfree(rp, M_DEVBUF);
-	}
-	krp->krp_status = err;
-	crypto_kdone(krp);
-	return (0);
-}
-
-#ifdef UBSEC_DEBUG
-static void
-ubsec_dump_pb(volatile struct ubsec_pktbuf *pb)
-{
-	kprintf("addr 0x%x (0x%x) next 0x%x\n",
-	    pb->pb_addr, pb->pb_len, pb->pb_next);
-}
-
-static void
-ubsec_dump_ctx2(struct ubsec_ctx_keyop *c)
-{
-	kprintf("CTX (0x%x):\n", c->ctx_len);
-	switch (letoh16(c->ctx_op)) {
-	case UBS_CTXOP_RNGBYPASS:
-	case UBS_CTXOP_RNGSHA1:
-		break;
-	case UBS_CTXOP_MODEXP:
-	{
-		struct ubsec_ctx_modexp *cx = (void *)c;
-		int i, len;
-
-		kprintf(" Elen %u, Nlen %u\n",
-		    letoh16(cx->me_E_len), letoh16(cx->me_N_len));
-		len = (cx->me_N_len + 7)/8;
-		for (i = 0; i < len; i++)
-			kprintf("%s%02x", (i == 0) ? " N: " : ":", cx->me_N[i]);
-		kprintf("\n");
-		break;
-	}
-	default:
-		kprintf("unknown context: %x\n", c->ctx_op);
-	}
-	kprintf("END CTX\n");
-}
-
-static void
-ubsec_dump_mcr(struct ubsec_mcr *mcr)
-{
-	volatile struct ubsec_mcr_add *ma;
-	int i;
-
-	kprintf("MCR:\n");
-	kprintf(" pkts: %u, flags 0x%x\n",
-	    letoh16(mcr->mcr_pkts), letoh16(mcr->mcr_flags));
-	ma = (volatile struct ubsec_mcr_add *)&mcr->mcr_cmdctxp;
-	for (i = 0; i < letoh16(mcr->mcr_pkts); i++) {
-		kprintf(" %d: ctx 0x%x len 0x%x rsvd 0x%x\n", i,
-		    letoh32(ma->mcr_cmdctxp), letoh16(ma->mcr_pktlen),
-		    letoh16(ma->mcr_reserved));
-		kprintf(" %d: ipkt ", i);
-		ubsec_dump_pb(&ma->mcr_ipktbuf);
-		kprintf(" %d: opkt ", i);
-		ubsec_dump_pb(&ma->mcr_opktbuf);
-		ma++;
-	}
-	kprintf("END MCR\n");
-}
-#endif /* UBSEC_DEBUG */
-
-/*
- * Return the number of significant bits of a big number.
- */
-static int
-ubsec_ksigbits(struct crparam *cr)
-{
-	u_int plen = (cr->crp_nbits + 7) / 8;
-	int i, sig = plen * 8;
-	u_int8_t c, *p = cr->crp_p;
-
-	for (i = plen - 1; i >= 0; i--) {
-		c = p[i];
-		if (c != 0) {
-			while ((c & 0x80) == 0) {
-				sig--;
-				c <<= 1;
-			}
-			break;
-		}
-		sig -= 8;
-	}
-	return (sig);
-}
-
-static void
-ubsec_kshift_r(
-	u_int shiftbits,
-	u_int8_t *src, u_int srcbits,
-	u_int8_t *dst, u_int dstbits)
-{
-	u_int slen, dlen;
-	int i, si, di, n;
-
-	slen = (srcbits + 7) / 8;
-	dlen = (dstbits + 7) / 8;
-
-	for (i = 0; i < slen; i++)
-		dst[i] = src[i];
-	for (i = 0; i < dlen - slen; i++)
-		dst[slen + i] = 0;
-
-	n = shiftbits / 8;
-	if (n != 0) {
-		si = dlen - n - 1;
-		di = dlen - 1;
-		while (si >= 0)
-			dst[di--] = dst[si--];
-		while (di >= 0)
-			dst[di--] = 0;
-	}
-
-	n = shiftbits % 8;
-	if (n != 0) {
-		for (i = dlen - 1; i > 0; i--)
-			dst[i] = (dst[i] << n) |
-			    (dst[i - 1] >> (8 - n));
-		dst[0] = dst[0] << n;
-	}
-}
-
-static void
-ubsec_kshift_l(
-	u_int shiftbits,
-	u_int8_t *src, u_int srcbits,
-	u_int8_t *dst, u_int dstbits)
-{
-	int slen, dlen, i, n;
-
-	slen = (srcbits + 7) / 8;
-	dlen = (dstbits + 7) / 8;
-
-	n = shiftbits / 8;
-	for (i = 0; i < slen; i++)
-		dst[i] = src[i + n];
-	for (i = 0; i < dlen - slen; i++)
-		dst[slen + i] = 0;
-
-	n = shiftbits % 8;
-	if (n != 0) {
-		for (i = 0; i < (dlen - 1); i++)
-			dst[i] = (dst[i] >> n) | (dst[i + 1] << (8 - n));
-		dst[dlen - 1] = dst[dlen - 1] >> n;
-	}
-}
diff --git a/sys/dev/crypto/ubsec/ubsecreg.h b/sys/dev/crypto/ubsec/ubsecreg.h
deleted file mode 100644
index 8ba79adc41..0000000000
--- a/sys/dev/crypto/ubsec/ubsecreg.h
+++ /dev/null
@@ -1,221 +0,0 @@
-/* $FreeBSD: src/sys/dev/ubsec/ubsecreg.h,v 1.2.2.4 2003/06/04 17:05:11 sam Exp $ */
-/* $DragonFly: src/sys/dev/crypto/ubsec/ubsecreg.h,v 1.2 2003/06/17 04:28:32 dillon Exp $ */
-/*	$OpenBSD: ubsecreg.h,v 1.27 2002/09/11 22:40:31 jason Exp $	*/
-
-/*
- * Copyright (c) 2000 Theo de Raadt
- * Copyright (c) 2001 Patrik Lindergren (patrik@ipunplugged.com)
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-
-/*
- * Register definitions for 5601 BlueSteel Networks Ubiquitous Broadband
- * Security "uBSec" chip.  Definitions from revision 2.8 of the product
- * datasheet.
- */
-
-#define BS_BAR			0x10	/* DMA base address register */
-#define	BS_TRDY_TIMEOUT		0x40	/* TRDY timeout */
-#define	BS_RETRY_TIMEOUT	0x41	/* DMA retry timeout */
-
-#define	PCI_VENDOR_BROADCOM	0x14e4		/* Broadcom */
-#define	PCI_VENDOR_BLUESTEEL	0x15ab		/* Bluesteel Networks */
-#define	PCI_VENDOR_SUN		0x108e		/* Sun Microsystems */
-
-/* Bluesteel Networks */
-#define	PCI_PRODUCT_BLUESTEEL_5501	0x0000		/* 5501 */
-#define	PCI_PRODUCT_BLUESTEEL_5601	0x5601		/* 5601 */
-
-/* Broadcom */
-#define	PCI_PRODUCT_BROADCOM_BCM5700	0x1644		/* BCM5700 */
-#define	PCI_PRODUCT_BROADCOM_BCM5701	0x1645		/* BCM5701 */
-#define	PCI_PRODUCT_BROADCOM_5801	0x5801		/* 5801 */
-#define	PCI_PRODUCT_BROADCOM_5802	0x5802		/* 5802 */
-#define	PCI_PRODUCT_BROADCOM_5805	0x5805		/* 5805 */
-#define	PCI_PRODUCT_BROADCOM_5820	0x5820		/* 5820 */
-#define	PCI_PRODUCT_BROADCOM_5821	0x5821		/* 5821 */
-#define	PCI_PRODUCT_BROADCOM_5822	0x5822		/* 5822 */
-#define	PCI_PRODUCT_BROADCOM_5823	0x5823		/* 5823 */
-#define PCI_PRODUCT_BROADCOM_5825	0x5825		/* 5825 */
-
-/* Sun Microsystems */
-#define PCI_PRODUCT_SUN_5821		0x5454		/* Crypto 5821 */
-#define PCI_PRODUCT_SUN_SCA1K		0x5455		/* Crypto 1K */
-
-#define	UBS_PCI_RTY_SHIFT			8
-#define	UBS_PCI_RTY_MASK			0xff
-#define	UBS_PCI_RTY(misc) \
-    (((misc) >> UBS_PCI_RTY_SHIFT) & UBS_PCI_RTY_MASK)
-
-#define	UBS_PCI_TOUT_SHIFT			0
-#define	UBS_PCI_TOUT_MASK			0xff
-#define	UBS_PCI_TOUT(misc) \
-    (((misc) >> PCI_TOUT_SHIFT) & PCI_TOUT_MASK)
-
-/*
- * DMA Control & Status Registers (offset from BS_BAR)
- */
-#define	BS_MCR1		0x00	/* DMA Master Command Record 1 */
-#define	BS_CTRL		0x04	/* DMA Control */
-#define	BS_STAT		0x08	/* DMA Status */
-#define	BS_ERR		0x0c	/* DMA Error Address */
-#define	BS_MCR2		0x10	/* DMA Master Command Record 2 */
-
-/* BS_CTRL - DMA Control */
-#define	BS_CTRL_RESET		0x80000000	/* hardware reset, 5805/5820 */
-#define	BS_CTRL_MCR2INT		0x40000000	/* enable intr MCR for MCR2 */
-#define	BS_CTRL_MCR1INT		0x20000000	/* enable intr MCR for MCR1 */
-#define	BS_CTRL_OFM		0x10000000	/* Output fragment mode */
-#define	BS_CTRL_BE32		0x08000000	/* big-endian, 32bit bytes */
-#define	BS_CTRL_BE64		0x04000000	/* big-endian, 64bit bytes */
-#define	BS_CTRL_DMAERR		0x02000000	/* enable intr DMA error */
-#define	BS_CTRL_RNG_M		0x01800000	/* RNG mode */
-#define	BS_CTRL_RNG_1		0x00000000	/* 1bit rn/one slow clock */
-#define	BS_CTRL_RNG_4		0x00800000	/* 1bit rn/four slow clocks */
-#define	BS_CTRL_RNG_8		0x01000000	/* 1bit rn/eight slow clocks */
-#define	BS_CTRL_RNG_16		0x01800000	/* 1bit rn/16 slow clocks */
-#define	BS_CTRL_SWNORM		0x00400000	/* 582[01], sw normalization */
-#define	BS_CTRL_FRAG_M		0x0000ffff	/* output fragment size mask */
-#define	BS_CTRL_LITTLE_ENDIAN	(BS_CTRL_BE32 | BS_CTRL_BE64)
-
-/* BS_STAT - DMA Status */
-#define	BS_STAT_MCR1_BUSY	0x80000000	/* MCR1 is busy */
-#define	BS_STAT_MCR1_FULL	0x40000000	/* MCR1 is full */
-#define	BS_STAT_MCR1_DONE	0x20000000	/* MCR1 is done */
-#define	BS_STAT_DMAERR		0x10000000	/* DMA error */
-#define	BS_STAT_MCR2_FULL	0x08000000	/* MCR2 is full */
-#define	BS_STAT_MCR2_DONE	0x04000000	/* MCR2 is done */
-#define	BS_STAT_MCR1_ALLEMPTY	0x02000000	/* 5821, MCR1 is empty */
-#define	BS_STAT_MCR2_ALLEMPTY	0x01000000	/* 5821, MCR2 is empty */
-
-/* BS_ERR - DMA Error Address */
-#define	BS_ERR_ADDR		0xfffffffc	/* error address mask */
-#define	BS_ERR_READ		0x00000002	/* fault was on read */
-
-struct ubsec_pktctx {
-	u_int32_t	pc_deskey[6];		/* 3DES key */
-	u_int32_t	pc_hminner[5];		/* hmac inner state */
-	u_int32_t	pc_hmouter[5];		/* hmac outer state */
-	u_int32_t	pc_iv[2];		/* [3]DES iv */
-	u_int16_t	pc_flags;		/* flags, below */
-	u_int16_t	pc_offset;		/* crypto offset */
-};
-#define	UBS_PKTCTX_ENC_3DES	0x8000		/* use 3des */
-#define	UBS_PKTCTX_ENC_NONE	0x0000		/* no encryption */
-#define	UBS_PKTCTX_INBOUND	0x4000		/* inbound packet */
-#define	UBS_PKTCTX_AUTH		0x3000		/* authentication mask */
-#define	UBS_PKTCTX_AUTH_NONE	0x0000		/* no authentication */
-#define	UBS_PKTCTX_AUTH_MD5	0x1000		/* use hmac-md5 */
-#define	UBS_PKTCTX_AUTH_SHA1	0x2000		/* use hmac-sha1 */
-
-struct ubsec_pktctx_long {
-	volatile u_int16_t	pc_len;		/* length of ctx struct */
-	volatile u_int16_t	pc_type;	/* context type, 0 */
-	volatile u_int16_t	pc_flags;	/* flags, same as above */
-	volatile u_int16_t	pc_offset;	/* crypto/auth offset */
-	volatile u_int32_t	pc_deskey[6];	/* 3DES key */
-	volatile u_int32_t	pc_iv[2];	/* [3]DES iv */
-	volatile u_int32_t	pc_hminner[5];	/* hmac inner state */
-	volatile u_int32_t	pc_hmouter[5];	/* hmac outer state */
-};
-#define	UBS_PKTCTX_TYPE_IPSEC	0x0000
-
-struct ubsec_pktbuf {
-	volatile u_int32_t	pb_addr;	/* address of buffer start */
-	volatile u_int32_t	pb_next;	/* pointer to next pktbuf */
-	volatile u_int32_t	pb_len;		/* packet length */
-};
-#define	UBS_PKTBUF_LEN		0x0000ffff	/* length mask */
-
-struct ubsec_mcr {
-	volatile u_int16_t	mcr_pkts;	/* #pkts in this mcr */
-	volatile u_int16_t	mcr_flags;	/* mcr flags (below) */
-	volatile u_int32_t	mcr_cmdctxp;	/* command ctx pointer */
-	struct ubsec_pktbuf	mcr_ipktbuf;	/* input chain header */
-	volatile u_int16_t	mcr_reserved;
-	volatile u_int16_t	mcr_pktlen;
-	struct ubsec_pktbuf	mcr_opktbuf;	/* output chain header */
-};
-
-struct ubsec_mcr_add {
-	volatile u_int32_t	mcr_cmdctxp;	/* command ctx pointer */
-	struct ubsec_pktbuf	mcr_ipktbuf;	/* input chain header */
-	volatile u_int16_t	mcr_reserved;
-	volatile u_int16_t	mcr_pktlen;
-	struct ubsec_pktbuf	mcr_opktbuf;	/* output chain header */
-};
-
-#define	UBS_MCR_DONE		0x0001		/* mcr has been processed */
-#define	UBS_MCR_ERROR		0x0002		/* error in processing */
-#define	UBS_MCR_ERRORCODE	0xff00		/* error type */
-
-struct ubsec_ctx_keyop {
-	volatile u_int16_t	ctx_len;	/* command length */
-	volatile u_int16_t	ctx_op;		/* operation code */
-	volatile u_int8_t	ctx_pad[60];	/* padding */
-};
-#define	UBS_CTXOP_DHPKGEN	0x01		/* dh public key generation */
-#define	UBS_CTXOP_DHSSGEN	0x02		/* dh shared secret gen. */
-#define	UBS_CTXOP_RSAPUB	0x03		/* rsa public key op */
-#define	UBS_CTXOP_RSAPRIV	0x04		/* rsa private key op */
-#define	UBS_CTXOP_DSASIGN	0x05		/* dsa signing op */
-#define	UBS_CTXOP_DSAVRFY	0x06		/* dsa verification */
-#define	UBS_CTXOP_RNGBYPASS	0x41		/* rng direct test mode */
-#define	UBS_CTXOP_RNGSHA1	0x42		/* rng sha1 test mode */
-#define	UBS_CTXOP_MODADD	0x43		/* modular addition */
-#define	UBS_CTXOP_MODSUB	0x44		/* modular subtraction */
-#define	UBS_CTXOP_MODMUL	0x45		/* modular multiplication */
-#define	UBS_CTXOP_MODRED	0x46		/* modular reduction */
-#define	UBS_CTXOP_MODEXP	0x47		/* modular exponentiation */
-#define	UBS_CTXOP_MODINV	0x48		/* modular inverse */
-
-struct ubsec_ctx_rngbypass {
-	volatile u_int16_t	rbp_len;	/* command length, 64 */
-	volatile u_int16_t	rbp_op;		/* rng bypass, 0x41 */
-	volatile u_int8_t	rbp_pad[60];	/* padding */
-};
-
-/* modexp: C = (M ^ E) mod N */
-struct ubsec_ctx_modexp {
-	volatile u_int16_t	me_len;		/* command length */
-	volatile u_int16_t	me_op;		/* modexp, 0x47 */
-	volatile u_int16_t	me_E_len;	/* E (bits) */
-	volatile u_int16_t	me_N_len;	/* N (bits) */
-	u_int8_t		me_N[2048/8];	/* N */
-};
-
-struct ubsec_ctx_rsapriv {
-	volatile u_int16_t	rpr_len;	/* command length */
-	volatile u_int16_t	rpr_op;		/* rsaprivate, 0x04 */
-	volatile u_int16_t	rpr_q_len;	/* q (bits) */
-	volatile u_int16_t	rpr_p_len;	/* p (bits) */
-	u_int8_t		rpr_buf[5 * 1024 / 8];	/* parameters: */
-						/* p, q, dp, dq, pinv */
-};
diff --git a/sys/dev/crypto/ubsec/ubsecvar.h b/sys/dev/crypto/ubsec/ubsecvar.h
deleted file mode 100644
index a62dd93627..0000000000
--- a/sys/dev/crypto/ubsec/ubsecvar.h
+++ /dev/null
@@ -1,256 +0,0 @@
-/* $FreeBSD: src/sys/dev/ubsec/ubsecvar.h,v 1.2.2.5 2003/06/04 17:56:59 sam Exp $ */
-/* $DragonFly: src/sys/dev/crypto/ubsec/ubsecvar.h,v 1.2 2003/06/17 04:28:32 dillon Exp $ */
-/*	$OpenBSD: ubsecvar.h,v 1.35 2002/09/24 18:33:26 jason Exp $	*/
-
-/*
- * Copyright (c) 2000 Theo de Raadt
- * Copyright (c) 2001 Patrik Lindergren (patrik@ipunplugged.com)
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-
-/* Maximum queue length */
-#ifndef UBS_MAX_NQUEUE
-#define UBS_MAX_NQUEUE		60
-#endif
-
-#define	UBS_MAX_SCATTER		64	/* Maximum scatter/gather depth */
-
-#ifndef UBS_MAX_AGGR
-#define	UBS_MAX_AGGR		5	/* Maximum aggregation count */
-#endif
-
-#define	UBSEC_CARD(sid)		(((sid) & 0xf0000000) >> 28)
-#define	UBSEC_SESSION(sid)	( (sid) & 0x0fffffff)
-#define	UBSEC_SID(crd, sesn)	(((crd) << 28) | ((sesn) & 0x0fffffff))
-
-#define UBS_DEF_RTY		0xff	/* PCI Retry Timeout */
-#define UBS_DEF_TOUT		0xff	/* PCI TRDY Timeout */
-#define UBS_DEF_CACHELINE	0x01	/* Cache Line setting */
-
-#ifdef _KERNEL
-
-struct ubsec_dma_alloc {
-	u_int32_t		dma_paddr;
-	caddr_t			dma_vaddr;
-	bus_dma_tag_t		dma_tag;
-	bus_dmamap_t		dma_map;
-	bus_dma_segment_t	dma_seg;
-	bus_size_t		dma_size;
-	int			dma_nseg;
-};
-
-struct ubsec_q2 {
-	SIMPLEQ_ENTRY(ubsec_q2)		q_next;
-	struct ubsec_dma_alloc		q_mcr;
-	struct ubsec_dma_alloc		q_ctx;
-	u_int				q_type;
-};
-
-struct ubsec_q2_rng {
-	struct ubsec_q2			rng_q;
-	struct ubsec_dma_alloc		rng_buf;
-	int				rng_used;
-};
-
-/* C = (M ^ E) mod N */
-#define	UBS_MODEXP_PAR_M	0
-#define	UBS_MODEXP_PAR_E	1
-#define	UBS_MODEXP_PAR_N	2
-#define	UBS_MODEXP_PAR_C	3
-struct ubsec_q2_modexp {
-	struct ubsec_q2			me_q;
-	struct cryptkop *		me_krp;
-	struct ubsec_dma_alloc		me_M;
-	struct ubsec_dma_alloc		me_E;
-	struct ubsec_dma_alloc		me_C;
-	struct ubsec_dma_alloc		me_epb;
-	int				me_modbits;
-	int				me_shiftbits;
-	int				me_normbits;
-};
-
-#define	UBS_RSAPRIV_PAR_P	0
-#define	UBS_RSAPRIV_PAR_Q	1
-#define	UBS_RSAPRIV_PAR_DP	2
-#define	UBS_RSAPRIV_PAR_DQ	3
-#define	UBS_RSAPRIV_PAR_PINV	4
-#define	UBS_RSAPRIV_PAR_MSGIN	5
-#define	UBS_RSAPRIV_PAR_MSGOUT	6
-struct ubsec_q2_rsapriv {
-	struct ubsec_q2			rpr_q;
-	struct cryptkop *		rpr_krp;
-	struct ubsec_dma_alloc		rpr_msgin;
-	struct ubsec_dma_alloc		rpr_msgout;
-};
-
-#define	UBSEC_RNG_BUFSIZ	16		/* measured in 32bit words */
-
-struct ubsec_dmachunk {
-	struct ubsec_mcr	d_mcr;
-	struct ubsec_mcr_add	d_mcradd[UBS_MAX_AGGR-1];
-	struct ubsec_pktbuf	d_sbuf[UBS_MAX_SCATTER-1];
-	struct ubsec_pktbuf	d_dbuf[UBS_MAX_SCATTER-1];
-	u_int32_t		d_macbuf[5];
-	union {
-		struct ubsec_pktctx_long	ctxl;
-		struct ubsec_pktctx		ctx;
-	} d_ctx;
-};
-
-struct ubsec_dma {
-	SIMPLEQ_ENTRY(ubsec_dma)	d_next;
-	struct ubsec_dmachunk		*d_dma;
-	struct ubsec_dma_alloc		d_alloc;
-};
-
-#define	UBS_FLAGS_KEY		0x01		/* has key accelerator */
-#define	UBS_FLAGS_LONGCTX	0x02		/* uses long ipsec ctx */
-#define	UBS_FLAGS_BIGKEY	0x04		/* 2048bit keys */
-#define	UBS_FLAGS_HWNORM	0x08		/* hardware normalization */
-#define	UBS_FLAGS_RNG		0x10		/* hardware rng */
-
-struct ubsec_operand {
-	union {
-		struct mbuf *m;
-		struct uio *io;
-	} u;
-	bus_dmamap_t		map;
-	bus_size_t		mapsize;
-	int			nsegs;
-	bus_dma_segment_t	segs[UBS_MAX_SCATTER];
-};
-
-struct ubsec_q {
-	SIMPLEQ_ENTRY(ubsec_q)		q_next;
-	int				q_nstacked_mcrs;
-	struct ubsec_q			*q_stacked_mcr[UBS_MAX_AGGR-1];
-	struct cryptop			*q_crp;
-	struct ubsec_dma		*q_dma;
-
-	struct ubsec_operand		q_src;
-	struct ubsec_operand		q_dst;
-
-	int				q_sesn;
-	int				q_flags;
-};
-
-#define	q_src_m		q_src.u.m
-#define	q_src_io	q_src.u.io
-#define	q_src_map	q_src.map
-#define	q_src_nsegs	q_src.nsegs
-#define	q_src_segs	q_src.segs
-#define	q_src_mapsize	q_src.mapsize
-
-#define	q_dst_m		q_dst.u.m
-#define	q_dst_io	q_dst.u.io
-#define	q_dst_map	q_dst.map
-#define	q_dst_nsegs	q_dst.nsegs
-#define	q_dst_segs	q_dst.segs
-#define	q_dst_mapsize	q_dst.mapsize
-
-struct ubsec_softc {
-	device_t		sc_dev;		/* device backpointer */
-	struct resource		*sc_irq;
-	void			*sc_ih;		/* interrupt handler cookie */
-	bus_space_handle_t	sc_sh;		/* memory handle */
-	bus_space_tag_t		sc_st;		/* memory tag */
-	struct resource		*sc_sr;		/* memory resource */
-	bus_dma_tag_t		sc_dmat;	/* dma tag */
-	int			sc_flags;	/* device specific flags */
-	int			sc_suspended;
-	int			sc_needwakeup;	/* notify crypto layer */
-	u_int32_t		sc_statmask;	/* interrupt status mask */
-	int32_t			sc_cid;		/* crypto tag */
-	SIMPLEQ_HEAD(,ubsec_q)	sc_queue;	/* packet queue, mcr1 */
-	int			sc_nqueue;	/* count enqueued, mcr1 */
-	SIMPLEQ_HEAD(,ubsec_q)	sc_qchip;	/* on chip, mcr1 */
-	int			sc_nqchip;	/* count on chip, mcr1 */
-	SIMPLEQ_HEAD(,ubsec_q)	sc_freequeue;	/* list of free queue elements */
-	SIMPLEQ_HEAD(,ubsec_q2)	sc_queue2;	/* packet queue, mcr2 */
-	int			sc_nqueue2;	/* count enqueued, mcr2 */
-	SIMPLEQ_HEAD(,ubsec_q2)	sc_qchip2;	/* on chip, mcr2 */
-	int			sc_nsessions;	/* # of sessions */
-	struct ubsec_session	*sc_sessions;	/* sessions */
-	struct callout		sc_rngto;	/* rng timeout */
-	int			sc_rnghz;	/* rng poll time */
-	struct ubsec_q2_rng	sc_rng;
-	struct rndtest_state	*sc_rndtest;	/* RNG test state */
-	void			(*sc_harvest)(struct rndtest_state *,
-					void *, u_int);
-	struct ubsec_dma	sc_dmaa[UBS_MAX_NQUEUE];
-	struct ubsec_q		*sc_queuea[UBS_MAX_NQUEUE];
-	SIMPLEQ_HEAD(,ubsec_q2)	sc_q2free;	/* free list */
-};
-
-#define	UBSEC_QFLAGS_COPYOUTIV		0x1
-
-struct ubsec_session {
-	u_int32_t	ses_used;
-	u_int32_t	ses_deskey[6];		/* 3DES key */
-	u_int32_t	ses_mlen;		/* hmac length */
-	u_int32_t	ses_hminner[5];		/* hmac inner state */
-	u_int32_t	ses_hmouter[5];		/* hmac outer state */
-	u_int32_t	ses_iv[2];		/* [3]DES iv */
-};
-#endif /* _KERNEL */
-
-struct ubsec_stats {
-	u_int64_t hst_ibytes;
-	u_int64_t hst_obytes;
-	u_int32_t hst_ipackets;
-	u_int32_t hst_opackets;
-	u_int32_t hst_invalid;		/* invalid argument */
-	u_int32_t hst_badsession;	/* invalid session id */
-	u_int32_t hst_badflags;		/* flags indicate !(mbuf | uio) */
-	u_int32_t hst_nodesc;		/* op submitted w/o descriptors */
-	u_int32_t hst_badalg;		/* unsupported algorithm */
-	u_int32_t hst_nomem;
-	u_int32_t hst_queuefull;
-	u_int32_t hst_dmaerr;
-	u_int32_t hst_mcrerr;
-	u_int32_t hst_nodmafree;
-	u_int32_t hst_lenmismatch;	/* enc/auth lengths different */
-	u_int32_t hst_skipmismatch;	/* enc part begins before auth part */
-	u_int32_t hst_iovmisaligned;	/* iov op not aligned */
-	u_int32_t hst_noirq;		/* IRQ for no reason */
-	u_int32_t hst_unaligned;	/* unaligned src caused copy */
-	u_int32_t hst_nomap;		/* bus_dmamap_create failed */
-	u_int32_t hst_noload;		/* bus_dmamap_load_* failed */
-	u_int32_t hst_nombuf;		/* MGET* failed */
-	u_int32_t hst_nomcl;		/* MCLGET* failed */
-	u_int32_t hst_totbatch;		/* ops submitted w/o interrupt */
-	u_int32_t hst_maxbatch;		/* max ops submitted together */
-	u_int32_t hst_maxqueue;		/* max ops queued for submission */
-	u_int32_t hst_maxqchip;		/* max mcr1 ops out for processing */
-	u_int32_t hst_mcr1full;		/* MCR1 too busy to take ops */
-	u_int32_t hst_rng;		/* RNG requests */
-	u_int32_t hst_modexp;		/* MOD EXP requests */
-	u_int32_t hst_modexpcrt;	/* MOD EXP CRT requests */
-};
diff --git a/sys/dev/disk/dm/crypt/Makefile b/sys/dev/disk/dm/crypt/Makefile
index a8a241bdf8..0454bc7bce 100644
--- a/sys/dev/disk/dm/crypt/Makefile
+++ b/sys/dev/disk/dm/crypt/Makefile
@@ -1,6 +1,6 @@
 KMOD=   dm_target_crypt
 
-SRCS+=	bus_if.h device_if.h opt_ktr.h
-SRCS+=	dm_target_crypt.c
+SRCS+=	bus_if.h device_if.h
+SRCS+=	dm_target_crypt.c ../../../../crypto/crypto_cipher.h
 
 .include <bsd.kmod.mk>
diff --git a/sys/dev/disk/dm/crypt/dm_target_crypt.c b/sys/dev/disk/dm/crypt/dm_target_crypt.c
index 769d40e4f8..d63a8ab2c1 100644
--- a/sys/dev/disk/dm/crypt/dm_target_crypt.c
+++ b/sys/dev/disk/dm/crypt/dm_target_crypt.c
@@ -1,8 +1,9 @@
 /*
- * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
+ * Copyright (c) 2010, 2024 The DragonFly Project.  All rights reserved.
  *
  * This code is derived from software contributed to The DragonFly Project
- * by Alex Hornung <ahornung@gmail.com>
+ * by Alex Hornung <ahornung@gmail.com> and
+ * Michael Neumann <mneumann@ntecs.de>.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -35,65 +36,99 @@
 /*
  * This file implements initial version of device-mapper crypt target.
  */
-#include <sys/endian.h>
 
+#include <sys/systm.h>
+#include <sys/endian.h>
 #include <sys/bio.h>
-#include <sys/globaldata.h>
 #include <sys/kerneldump.h>
 #include <sys/malloc.h>
-#include <sys/mpipe.h>
 #include <sys/md5.h>
 #include <crypto/sha1.h>
 #include <crypto/sha2/sha2.h>
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/rmd160.h>
-#include <machine/cpufunc.h>
-#include <cpu/atomic.h>
+#include <crypto/rmd160/rmd160.h>
+#include <crypto/crypto_cipher.h>
+#include <dev/disk/dm/dm.h>
 
-#include <sys/ktr.h>
-#include <sys/spinlock2.h>
+#include <sys/lock.h>
+#include <sys/kthread.h>
+#include <sys/queue.h>
+#include <sys/proc.h>
+#include <sys/types.h>
+#include <cpu/atomic.h>
 
-#include <dev/disk/dm/dm.h>
 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
 
-KTR_INFO_MASTER(dmcrypt);
+/**
+ * A Multi Producer Single Consumer queue.
+ *
+ * Multiple threads can submit "jobs" to queues. Each queue
+ * is served by exactly one worker thread.
+ */
 
-#if !defined(KTR_DMCRYPT)
-#define KTR_DMCRYPT	KTR_ALL
-#endif
+typedef void workqueue_job_callback(void *arg1, void *arg2, void *ctx);
+
+struct workqueue_job {
+	workqueue_job_callback *wqj_cb;
+	void *wqj_arg1;
+	void *wqj_arg2;
+	bool wqj_is_free;
+	STAILQ_ENTRY(workqueue_job) wqj_next;
+};
+
+struct workqueue {
+	struct lock wq_lock;
+	bool wq_is_closing;
+	void *wq_ctx;
+	struct thread *wq_worker;
 
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_dispatch, 0,
-    "crypto_dispatch(%p)", struct cryptop *crp);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypt_strategy, 0,
-    "crypt_strategy(b_cmd = %d, bp = %p)", int cmd, struct buf *bp);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_start, 1,
-    "crypto_write_start(crp = %p, bp = %p, sector = %d/%d)",
-    struct cryptop *crp, struct buf *bp, int i, int sectors);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_write_done, 1,
-    "crypto_cb_write_done(crp = %p, bp = %p, n = %d)",
-    struct cryptop *crp, struct buf *bp, int n);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_write_done, 1,
-    "bio_write_done(bp = %p)", struct buf *bp);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_write_retry, 1,
-    "crypto_write_retry(crp = %p)", struct buf *bp);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, bio_read_done, 2,
-    "bio_read_done(bp = %p)", struct buf *bp);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_read_start, 2,
-    "crypto_read_start(crp = %p, bp = %p, sector = %d/%d)",
-    struct cryptop *crp, struct buf *bp, int i, int sectors);
-KTR_INFO(KTR_DMCRYPT, dmcrypt, crypto_cb_read_done, 2,
-    "crypto_cb_read_done(crp = %p, bp = %p, n = %d)",
-    struct cryptop *crp, struct buf *bp, int n);
+	STAILQ_HEAD(, workqueue_job) wq_jobs;
+
+	/**
+	 * We use a preallocated list of free `struct workqueue_job`s,
+	 * so in case of low-memory situations, we can still
+	 * submit a fixed number of requests to the queue before
+	 * we block (or retry).
+	 *
+	 * In the case of dm_target_crypt, wherer we distribute
+	 * jobs to multiple queues, that's probably not required
+	 * and a bounded queue of size 1 would suffice.
+	 */
+	STAILQ_HEAD(, workqueue_job) wq_free_jobs;
+};
+
+static int workqueue_submit_job(struct workqueue *wq,
+    workqueue_job_callback *wqj_cb, void *wqj_arg1, void *wqj_arg2);
+
+static void workqueue_start(struct workqueue *wq, int cpu, void *ctx,
+    int bounded_size);
+
+static void workqueue_stop(struct workqueue *wq);
+
+static int workqueue_dequeue_job_into(struct workqueue *wq,
+    struct workqueue_job *job_copy);
+
+/**
+ * Each worker "owns" it's own memory, so there is no need
+ * to allocate the "data_buf" memory from a mpipe.
+ */
+struct worker_context {
+	uint8_t *data_buf;
+	size_t data_buf_size;
+	volatile bool data_buf_busy;
+};
 
+/**
+ * End of MPSC queue
+ */
+ 
 struct target_crypt_config;
 
-typedef void dispatch_t(void *);
-typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t,
-    void *);
+typedef void ivgen_t(struct target_crypt_config *, u_int8_t *, size_t, off_t);
 
 typedef int ivgen_ctor_t(struct target_crypt_config *, char *, void **);
 typedef int ivgen_dtor_t(struct target_crypt_config *, void *);
 
+
 struct iv_generator {
 	const char	*name;
 	ivgen_ctor_t	*ctor;
@@ -101,19 +136,9 @@ struct iv_generator {
 	ivgen_t		*gen_iv;
 };
 
-struct essiv_ivgen_data {
-	struct essiv_ivgen_data *next;
-	void		*ivpriv;
-	void		*opaque;
-	struct cryptop	crp;
-	struct cryptodesc crd;
-};
-
 struct essiv_ivgen_priv {
-	struct cryptoini	crypto_session;
-	struct spinlock		ivdata_spin;
-	struct essiv_ivgen_data	*ivdata_base;
-	u_int64_t		crypto_sid;
+	struct crypto_cipher_context crypto_context;
+	const struct crypto_cipher  *crypto_cipher;
 	size_t			keyhash_len;
 	u_int8_t		crypto_keyhash[SHA512_DIGEST_LENGTH];
 };
@@ -122,30 +147,39 @@ typedef struct target_crypt_config {
 	size_t	params_len;
 	dm_pdev_t *pdev;
 	char	*status_str;
-	int	crypto_alg;
+	const struct crypto_cipher *crypto_cipher;
 	int	crypto_klen;
 	u_int8_t	crypto_key[512>>3];
+	struct crypto_cipher_context	crypto_context;
 
-	u_int64_t	crypto_sid;
 	u_int64_t	block_offset;
 	int64_t		iv_offset;
 	SHA512_CTX	essivsha512_ctx;
 
-	struct cryptoini	crypto_session;
-
 	struct iv_generator	*ivgen;
 	void	*ivgen_priv;
 
-	struct malloc_pipe	read_mpipe;
-	struct malloc_pipe	write_mpipe;
-} dm_target_crypt_config_t;
+	/**
+	 * per-CPU work queues and worker contexts [0..ncpus)
+	 *
+	 * We use separate workqueues for read and write requests.
+	 * Read requests do not cause long stalls, as they are synchronous
+	 * operations from userspace, but writes can cause long stalls
+	 * due to write buffering.
+	 */
+	struct workqueue	*crypto_read_workqueues;
+	struct worker_context  	*crypto_read_worker_contexts;
+	struct workqueue	*crypto_write_workqueues;
+	struct worker_context  	*crypto_write_worker_contexts;
+
+	/**
+	 * Atomic counter used to distribute requests
+	 * to the crypto work queues using round robin.
+	 */
+	int crypto_read_workqueue_next_rr;
+	int crypto_write_workqueue_next_rr;
 
-struct dmtc_helper {
-	dm_target_crypt_config_t *priv;
-	caddr_t	free_addr;
-	caddr_t	orig_buf;
-	caddr_t data_buf;
-};
+} dm_target_crypt_config_t;
 
 struct dmtc_dump_helper {
 	dm_target_crypt_config_t *priv;
@@ -153,33 +187,28 @@ struct dmtc_dump_helper {
 	size_t length;
 	off_t offset;
 
-	int sectors;
-	int *ident;
-
-	struct cryptodesc crd[128];
-	struct cryptop crp[128];
 	u_char space[65536];
 };
 
-#define DMTC_BUF_SIZE_WRITE \
-    (MAXPHYS + sizeof(struct dmtc_helper) + \
-     MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc)))
-#define DMTC_BUF_SIZE_READ \
-    (sizeof(struct dmtc_helper) + \
-     MAXPHYS/DEV_BSIZE*(sizeof(struct cryptop) + sizeof(struct cryptodesc)))
-
-static void dmtc_crypto_dispatch(void *arg);
-static void dmtc_crypto_dump_start(dm_target_crypt_config_t *priv,
-				struct dmtc_dump_helper *dump_helper);
-static void dmtc_crypto_read_start(dm_target_crypt_config_t *priv,
-				struct bio *bio);
-static void dmtc_crypto_write_start(dm_target_crypt_config_t *priv,
-				struct bio *bio);
+static const struct crypto_cipher *
+dmtc_find_crypto_cipher(const char *crypto_alg, const char *crypto_mode,
+    int klen_in_bits);
+
+#define DMTC_BUF_SIZE (MAXPHYS)
+
+static void dmtc_crypto_dump(dm_target_crypt_config_t *priv,
+    struct dmtc_dump_helper *dump_helper);
+
+static int dmtc_bio_encdec(dm_target_crypt_config_t *priv, uint8_t *data_buf,
+    int bytes, off_t offset, crypto_cipher_blockfn_t blockfn);
+
 static void dmtc_bio_read_done(struct bio *bio);
+static void dmtc_bio_read_decrypt(dm_target_crypt_config_t *priv,
+    struct bio *bio);
+
+static void dmtc_bio_write_encrypt(dm_target_crypt_config_t *priv,
+    struct bio *bio);
 static void dmtc_bio_write_done(struct bio *bio);
-static int dmtc_crypto_cb_dump_done(struct cryptop *crp);
-static int dmtc_crypto_cb_read_done(struct cryptop *crp);
-static int dmtc_crypto_cb_write_done(struct cryptop *crp);
 
 static ivgen_ctor_t	essiv_ivgen_ctor;
 static ivgen_dtor_t	essiv_ivgen_dtor;
@@ -195,53 +224,168 @@ static struct iv_generator ivgens[] = {
 	{ NULL, NULL, NULL, NULL }
 };
 
-/*
- * Number of crypto buffers.  All crypto buffers will be preallocated
- * in order to avoid kmalloc() deadlocks in critical low-memory paging
- * paths.
+/**
+ * MPSC queue implementation
  */
-static __inline int
-dmtc_get_nmax(void)
+
+static void
+workqueue_stop(struct workqueue *wq)
+{
+	lockmgr(&wq->wq_lock, LK_EXCLUSIVE);
+	wq->wq_is_closing = true;
+	lockmgr(&wq->wq_lock, LK_RELEASE);
+
+	while (true) {
+		wakeup(&wq->wq_jobs);
+		if (tsleep(wq, 0, "shutdown workqueue", 500) == 0)
+			break;
+	}
+
+	lockmgr(&wq->wq_lock, LK_EXCLUSIVE);
+
+	KKASSERT(STAILQ_FIRST(&wq->wq_jobs) == NULL);
+
+	/**
+	 * Free pre-allocated jobs.
+	 */
+	struct workqueue_job *free_job;
+	while ((free_job = STAILQ_FIRST(&wq->wq_free_jobs)) != NULL) {
+		KKASSERT(free_job->wqj_is_free);
+		STAILQ_REMOVE_HEAD(&wq->wq_free_jobs, wqj_next);
+		kfree(free_job, M_DMCRYPT);
+	}
+	KKASSERT(STAILQ_FIRST(&wq->wq_free_jobs) == NULL);
+
+	lockmgr(&wq->wq_lock, LK_RELEASE);
+	lockuninit(&wq->wq_lock);
+}
+
+static int
+workqueue_submit_job(struct workqueue *wq, workqueue_job_callback *wqj_cb,
+    void *wqj_arg1, void *wqj_arg2)
 {
-	int nmax;
+	struct workqueue_job *job = NULL;
 
-	nmax = (physmem * 2 / 1000 * PAGE_SIZE) /
-	       (DMTC_BUF_SIZE_WRITE + DMTC_BUF_SIZE_READ) + 1;
+	if (wq->wq_is_closing) {
+		return (EPIPE);
+	}
+
+	lockmgr(&wq->wq_lock, LK_EXCLUSIVE);
+
+	while (!wq->wq_is_closing) {
+		job = STAILQ_FIRST(&wq->wq_free_jobs);
+		if (job) {
+			KKASSERT(job->wqj_is_free);
 
-	if (nmax < 2)
-		nmax = 2;
-	if (nmax > 8 + ncpus * 2)
-		nmax = 8 + ncpus * 2;
+			STAILQ_REMOVE_HEAD(&wq->wq_free_jobs, wqj_next);
+			job->wqj_cb = wqj_cb;
+			job->wqj_arg1 = wqj_arg1;
+			job->wqj_arg2 = wqj_arg2;
 
-	return nmax;
+			STAILQ_INSERT_TAIL(&wq->wq_jobs, job, wqj_next);
+			lockmgr(&wq->wq_lock, LK_RELEASE);
+			wakeup_one(&wq->wq_jobs);
+
+			return (0);
+		} else {
+			/**
+			 * Wait until the worker has put back an item to the
+			 * free list.
+			 */
+			lksleep(&wq->wq_free_jobs, &wq->wq_lock, 0,
+			    "dm_target_crypt: wq free list empty", 0);
+		}
+	}
+
+	lockmgr(&wq->wq_lock, LK_RELEASE);
+
+	return (EPIPE);
 }
 
-/*
- * Initialize the crypto buffer mpipe.  Preallocate all crypto buffers
- * to avoid making any kmalloc()s in the critical path.
- */
-static void
-dmtc_init_mpipe(struct target_crypt_config *priv)
+static int
+workqueue_dequeue_job_into(struct workqueue *wq, struct workqueue_job *job_copy)
 {
-	int nmax;
+	lockmgr(&wq->wq_lock, LK_EXCLUSIVE);
+
+	bzero(job_copy, sizeof(struct workqueue_job));
+
+	/*
+	 * In case the queue is closing, no further jobs can be submitted, but
+	 * dequeue will drain the queue until empty.
+	 */
 
-	nmax = dmtc_get_nmax();
+	while (true) {
+		struct workqueue_job *job = STAILQ_FIRST(&wq->wq_jobs);
+		if (job) {
+			STAILQ_REMOVE_HEAD(&wq->wq_jobs, wqj_next);
 
-	kprintf("dm_target_crypt: Setting %d mpipe buffers\n", nmax);
+			/*
+			 * Copy the job definition to the caller-passed pointer
+			 * and then put back the job to the free-list.
+			 */
+			memcpy(job_copy, job, sizeof(struct workqueue_job));
 
-	mpipe_init(&priv->write_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_WRITE,
-		   nmax, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
-	mpipe_init(&priv->read_mpipe, M_DMCRYPT, DMTC_BUF_SIZE_READ,
-		   nmax, nmax, MPF_NOZERO | MPF_CALLBACK, NULL, NULL, NULL);
+			job->wqj_is_free = true;
+			STAILQ_INSERT_TAIL(&wq->wq_free_jobs, job, wqj_next);
+
+			lockmgr(&wq->wq_lock, LK_RELEASE);
+			wakeup(&wq->wq_free_jobs);
+			return (0);
+
+		} else if (wq->wq_is_closing) {
+			lockmgr(&wq->wq_lock, LK_RELEASE);
+			return (EPIPE);
+		} else {
+			lksleep(&wq->wq_jobs, &wq->wq_lock, 0,
+			    "dm_target_crypt: wq empty", 0);
+		}
+	}
 }
 
 static void
-dmtc_destroy_mpipe(struct target_crypt_config *priv)
+workqueue_worker(void *wq_arg)
 {
-	mpipe_done(&priv->write_mpipe);
-	mpipe_done(&priv->read_mpipe);
+	struct workqueue *wq = wq_arg;
+	struct workqueue_job job;
+
+	while (workqueue_dequeue_job_into(wq, &job) == 0) {
+		job.wqj_cb(job.wqj_arg1, job.wqj_arg2, wq->wq_ctx);
+	}
+
+	wakeup(wq);
 }
 
+static void
+workqueue_start(struct workqueue *wq, int cpu, void *ctx, int bounded_size)
+{
+	bzero(wq, sizeof(*wq));
+
+	lockinit(&wq->wq_lock, "dm_target_crypt: wq", 0, LK_CANRECURSE);
+
+	STAILQ_INIT(&wq->wq_jobs);
+	STAILQ_INIT(&wq->wq_free_jobs);
+
+	wq->wq_is_closing = false;
+	wq->wq_ctx = ctx;
+
+	/**
+	 * Initialize free-list
+	 */
+	for (int i = 0; i < bounded_size; ++i) {
+		struct workqueue_job *free_job = kmalloc(
+		    sizeof(struct workqueue_job), M_DMCRYPT, M_ZERO | M_WAITOK);
+		free_job->wqj_is_free = true;
+		STAILQ_INSERT_TAIL(&wq->wq_free_jobs, free_job, wqj_next);
+	}
+
+	kthread_create_cpu(workqueue_worker, wq, &wq->wq_worker, cpu,
+	    "dm_target_crypt: crypto worker");
+}
+
+/**
+ * End of MPSC queue implementation
+ */
+
 /*
  * Overwrite private information (in buf) to avoid leaking it
  */
@@ -249,7 +393,7 @@ static void
 dmtc_crypto_clear(void *buf, size_t len)
 {
 	memset(buf, 0xFF, len);
-	bzero(buf, len);
+	explicit_bzero(buf, len);
 }
 
 /*
@@ -262,50 +406,49 @@ essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpri
 	u_int8_t crypto_keyhash[SHA512_DIGEST_LENGTH];
 	unsigned int klen, hashlen;
 	int error;
-	int nmax;
 
 	klen = (priv->crypto_klen >> 3);
 
 	if (iv_hash == NULL)
 		return EINVAL;
 
-	if (!strcmp(iv_hash, "sha1")) {
+	if (strcmp(iv_hash, "sha1") == 0) {
 		SHA1_CTX ctx;
 
 		hashlen = SHA1_RESULTLEN;
 		SHA1Init(&ctx);
 		SHA1Update(&ctx, priv->crypto_key, klen);
 		SHA1Final(crypto_keyhash, &ctx);
-	} else if (!strcmp(iv_hash, "sha256")) {
+	} else if (strcmp(iv_hash, "sha256") == 0) {
 		SHA256_CTX ctx;
 
 		hashlen = SHA256_DIGEST_LENGTH;
 		SHA256_Init(&ctx);
 		SHA256_Update(&ctx, priv->crypto_key, klen);
 		SHA256_Final(crypto_keyhash, &ctx);
-	} else if (!strcmp(iv_hash, "sha384")) {
+	} else if (strcmp(iv_hash, "sha384") == 0) {
 		SHA384_CTX ctx;
 
 		hashlen = SHA384_DIGEST_LENGTH;
 		SHA384_Init(&ctx);
 		SHA384_Update(&ctx, priv->crypto_key, klen);
 		SHA384_Final(crypto_keyhash, &ctx);
-	} else if (!strcmp(iv_hash, "sha512")) {
+	} else if (strcmp(iv_hash, "sha512") == 0) {
 		SHA512_CTX ctx;
 
 		hashlen = SHA512_DIGEST_LENGTH;
 		SHA512_Init(&ctx);
 		SHA512_Update(&ctx, priv->crypto_key, klen);
 		SHA512_Final(crypto_keyhash, &ctx);
-	} else if (!strcmp(iv_hash, "md5")) {
+	} else if (strcmp(iv_hash, "md5") == 0) {
 		MD5_CTX ctx;
 
 		hashlen = MD5_DIGEST_LENGTH;
 		MD5Init(&ctx);
 		MD5Update(&ctx, priv->crypto_key, klen);
 		MD5Final(crypto_keyhash, &ctx);
-	} else if (!strcmp(iv_hash, "rmd160") ||
-		   !strcmp(iv_hash, "ripemd160")) {
+	} else if (strcmp(iv_hash, "rmd160") == 0 ||
+		   strcmp(iv_hash, "ripemd160") == 0) {
 		RMD160_CTX ctx;
 
 		hashlen = 160/8;
@@ -325,20 +468,18 @@ essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpri
 	ivpriv->keyhash_len = sizeof(crypto_keyhash);
 	dmtc_crypto_clear(crypto_keyhash, sizeof(crypto_keyhash));
 
-	ivpriv->crypto_session.cri_alg = priv->crypto_alg;
-	ivpriv->crypto_session.cri_key = (u_int8_t *)ivpriv->crypto_keyhash;
-	ivpriv->crypto_session.cri_klen = hashlen;
-	ivpriv->crypto_session.cri_mlen = 0;
-	ivpriv->crypto_session.cri_next = NULL;
 
 	/*
 	 * XXX: in principle we also need to check if the block size of the
 	 *	cipher is a valid iv size for the block cipher.
 	 */
 
-	error = crypto_newsession(&ivpriv->crypto_sid,
-				  &ivpriv->crypto_session,
-				  CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
+	ivpriv->crypto_cipher = priv->crypto_cipher;
+
+	error = ivpriv->crypto_cipher->setkey(&ivpriv->crypto_context,
+		(const uint8_t *)ivpriv->crypto_keyhash,
+		hashlen / 8);
+
 	if (error) {
 		kprintf("dm_target_crypt: Error during crypto_newsession "
 			"for essiv_ivgen, error = %d\n",
@@ -348,22 +489,6 @@ essiv_ivgen_ctor(struct target_crypt_config *priv, char *iv_hash, void **p_ivpri
 		return ENOTSUP;
 	}
 
-	/*
-	 * mpipe for 512-byte ivgen elements, make sure there are enough
-	 * to cover all in-flight read and write buffers.
-	 */
-	nmax = dmtc_get_nmax() * (int)(MAXPHYS / DEV_BSIZE) * 2;
-
-	spin_init(&ivpriv->ivdata_spin, "ivdata");
-
-	while (nmax) {
-		struct essiv_ivgen_data *ivdata;
-
-		ivdata = kmalloc(sizeof(*ivdata), M_DMCRYPT, M_WAITOK|M_ZERO);
-		ivdata->next = ivpriv->ivdata_base;
-		ivpriv->ivdata_base = ivdata;
-		--nmax;
-	}
 	*p_ivpriv = ivpriv;
 
 	return 0;
@@ -373,18 +498,11 @@ static int
 essiv_ivgen_dtor(struct target_crypt_config *priv, void *arg)
 {
 	struct essiv_ivgen_priv *ivpriv;
-	struct essiv_ivgen_data *ivdata;
 
 	ivpriv = (struct essiv_ivgen_priv *)arg;
 	KKASSERT(ivpriv != NULL);
 
-	crypto_freesession(ivpriv->crypto_sid);
-
-	while ((ivdata = ivpriv->ivdata_base) != NULL) {
-		ivpriv->ivdata_base = ivdata->next;
-		kfree(ivdata, M_DMCRYPT);
-	}
-	spin_uninit(&ivpriv->ivdata_spin);
+	dmtc_crypto_clear(&ivpriv->crypto_context, sizeof(ivpriv->crypto_context));
 
 	dmtc_crypto_clear(ivpriv->crypto_keyhash, ivpriv->keyhash_len);
 	kfree(ivpriv, M_DMCRYPT);
@@ -392,103 +510,31 @@ essiv_ivgen_dtor(struct target_crypt_config *priv, void *arg)
 	return 0;
 }
 
-static int
-essiv_ivgen_done(struct cryptop *crp)
-{
-	struct essiv_ivgen_priv *ivpriv;
-	struct essiv_ivgen_data *ivdata;
-	void *opaque;
-
-
-	if (crp->crp_etype == EAGAIN)
-		return crypto_dispatch(crp);
-
-	if (crp->crp_etype != 0) {
-		kprintf("dm_target_crypt: essiv_ivgen_done, "
-			"crp->crp_etype = %d\n", crp->crp_etype);
-	}
-
-	ivdata = (void *)crp->crp_opaque;
-
-	/*
-	 * In-memory structure is:
-	 * |  ivpriv  |  opaque  |     crp     |      crd      |
-	 * | (void *) | (void *) |   (cryptop) |  (cryptodesc) |
-	 */
-	ivpriv = ivdata->ivpriv;
-	opaque = ivdata->opaque;
-
-	spin_lock(&ivpriv->ivdata_spin);
-	ivdata->next = ivpriv->ivdata_base;
-	ivpriv->ivdata_base = ivdata;
-	spin_unlock(&ivpriv->ivdata_spin);
-
-	dmtc_crypto_dispatch(opaque);
-
-	return 0;
-}
-
 static void
 essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
-	    size_t iv_len, off_t sector, void *opaque)
+	    size_t iv_len, off_t sector)
 {
 	struct essiv_ivgen_priv *ivpriv;
-	struct essiv_ivgen_data *ivdata;
-	struct cryptodesc *crd;
-	struct cryptop *crp;
 	int error;
 
 	ivpriv = priv->ivgen_priv;
 	KKASSERT(ivpriv != NULL);
 
-	/*
-	 * We preallocated all necessary ivdata's, so pull one off and use
-	 * it.
-	 */
-	spin_lock(&ivpriv->ivdata_spin);
-	ivdata = ivpriv->ivdata_base;
-	ivpriv->ivdata_base = ivdata->next;
-	spin_unlock(&ivpriv->ivdata_spin);
-
-	KKASSERT(ivdata != NULL);
-
-	ivdata->ivpriv = ivpriv;
-	ivdata->opaque = opaque;
-	crp = &ivdata->crp;
-	crd = &ivdata->crd;
-
 	bzero(iv, iv_len);
-	bzero(crd, sizeof(struct cryptodesc));
-	bzero(crp, sizeof(struct cryptop));
 	*((off_t *)iv) = htole64(sector + priv->iv_offset);
-	crp->crp_buf = (caddr_t)iv;
 
-	crp->crp_sid = ivpriv->crypto_sid;
-	crp->crp_ilen = crp->crp_olen = iv_len;
+	struct crypto_cipher_iv iv2;
+	bzero(&iv2, sizeof(iv2));
 
-	crp->crp_opaque =  (caddr_t)ivdata;
+	error = ivpriv->crypto_cipher->encrypt(
+			&ivpriv->crypto_context,
+			(uint8_t*)iv,
+			iv_len,
+			&iv2
+			);
 
-	crp->crp_callback = essiv_ivgen_done;
+	explicit_bzero(&iv2, sizeof(iv2));
 
-	crp->crp_desc = crd;
-	crp->crp_etype = 0;
-	crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL | CRYPTO_F_BATCH;
-
-	crd->crd_alg = priv->crypto_alg;
-#if 0
-	crd->crd_key = (caddr_t)priv->crypto_keyhash;
-	crd->crd_klen = priv->crypto_klen;
-#endif
-
-	bzero(crd->crd_iv, sizeof(crd->crd_iv));
-
-	crd->crd_skip = 0;
-	crd->crd_len = iv_len;
-	crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
-	crd->crd_flags |= CRD_F_ENCRYPT;
-	crd->crd_next = NULL;
-
-	error = crypto_dispatch(crp);
 	if (error)
 		kprintf("dm_target_crypt: essiv_ivgen, error = %d\n", error);
 }
@@ -496,40 +542,20 @@ essiv_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
 
 static void
 plain_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
-	    size_t iv_len, off_t sector, void *opaque)
+	    size_t iv_len, off_t sector)
 {
 	bzero(iv, iv_len);
 	*((uint32_t *)iv) = htole32((uint32_t)(sector + priv->iv_offset));
-	dmtc_crypto_dispatch(opaque);
 }
 
 static void
 plain64_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
-    size_t iv_len, off_t sector, void *opaque)
+    size_t iv_len, off_t sector)
 {
 	bzero(iv, iv_len);
 	*((uint64_t *)iv) = htole64((uint64_t)(sector + priv->iv_offset));
-	dmtc_crypto_dispatch(opaque);
 }
 
-#if 0
-static void
-geli_ivgen(dm_target_crypt_config_t *priv, u_int8_t *iv,
-	   size_t iv_len, off_t sector, void *opaque)
-{
-
-	SHA512_CTX	ctx512;
-	u_int8_t	md[SHA512_DIGEST_LENGTH]; /* Max. Digest Size */
-
-	memcpy(&ctx512, &priv->essivsha512_ctx, sizeof(SHA512_CTX));
-	SHA512_Update(&ctx512, (u_int8_t*)&sector, sizeof(off_t));
-	SHA512_Final(md, &ctx512);
-
-	memcpy(iv, md, iv_len);
-	dmtc_crypto_dispatch(opaque);
-}
-#endif
-
 /*
  * Init function called from dm_table_load_ioctl.
  * cryptsetup actually passes us this:
@@ -553,14 +579,38 @@ hex2key(char *hex, size_t key_len, u_int8_t *key)
 	return 0;
 }
 
+/**
+ * Map between dm_target_crypt algorithm naming and our own crypto/crypt_cipher
+ * naming It happens that they are identical, but it doesn't have to be this
+ * way.
+ */
+static const struct crypto_cipher *
+dmtc_find_crypto_cipher(const char *crypto_alg, const char *crypto_mode,
+    int klen_in_bits)
+{
+#define ALGO_MODE_EQ(algo, mode) \
+	((strcmp(crypto_alg, algo) == 0) && (strcmp(crypto_mode, mode) == 0))
+
+	if (ALGO_MODE_EQ("aes", "cbc"))
+		return crypto_cipher_find("aes", "cbc", klen_in_bits);
+
+	if (ALGO_MODE_EQ("aes", "xts"))
+		return crypto_cipher_find("aes", "xts", klen_in_bits);
+
+	kprintf("dm_target_crypt: unsupported algo: %s and mode: %s\n",
+	    crypto_alg, crypto_mode);
+
+	return NULL;
+}
+
 static int
 dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 {
 	dm_target_crypt_config_t *priv;
 	size_t len;
-	char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *key, *dev;
+	char *crypto_alg, *crypto_mode, *iv_mode, *iv_opt, *hex_key, *dev;
 	char *status_str;
-	int i, klen, error;
+	int i, klen_in_bits, error;
 	uint64_t iv_offset, block_offset;
 
 	if (argc != 5) {
@@ -581,18 +631,18 @@ dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 	crypto_mode = strsep(&argv[0], "-");
 	iv_opt = strsep(&argv[0], "-");
 	iv_mode = strsep(&iv_opt, ":");
-	key = argv[1];
+	hex_key = argv[1];
 	iv_offset = strtouq(argv[2], NULL, 0);
 	dev = argv[3];
 	block_offset = strtouq(argv[4], NULL, 0);
-	/* bits / 8 = bytes, 1 byte = 2 hexa chars, so << 2 */
-	klen = strlen(key) << 2;
+	/* hex_key is specified in hex, so one hex char == 4 bits */
+	klen_in_bits = strlen(hex_key) * 4;
 
 #if 0
 	kprintf("dm_target_crypt - new: dev=%s, crypto_alg=%s, crypto_mode=%s, "
 		"iv_mode=%s, iv_opt=%s, key=%s, iv_offset=%ju, "
 		"block_offset=%ju\n",
-		dev, crypto_alg, crypto_mode, iv_mode, iv_opt, key, iv_offset,
+		dev, crypto_alg, crypto_mode, iv_mode, iv_opt, hex_key, iv_offset,
 		block_offset);
 #endif
 
@@ -605,103 +655,13 @@ dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 		return ENOENT;
 	}
 
-	/*
-	 * This code checks for valid combinations of algorithm and mode.
-	 * Currently supported options are:
-	 *
-	 * *-cbc
-	 * aes-xts
-	 * twofish-xts
-	 * serpent-xts
-	 */
-	if ((strcmp(crypto_mode, "cbc") != 0) &&
-	    !((strcmp(crypto_mode, "xts") == 0) &&
-	    ((strcmp(crypto_alg, "aes") == 0) ||
-	    (strcmp(crypto_alg, "twofish") == 0) ||
-	    (strcmp(crypto_alg, "serpent") == 0))))
-	{
-		kprintf("dm_target_crypt: only support 'cbc' chaining mode,"
-		    " aes-xts, twofish-xts and serpent-xts, "
-		    "invalid mode '%s-%s'\n",
-		    crypto_alg, crypto_mode);
+	priv->crypto_cipher = dmtc_find_crypto_cipher(crypto_alg, crypto_mode, klen_in_bits);
+	priv->crypto_klen = klen_in_bits;
+	if (priv->crypto_cipher == NULL)
 		goto notsup;
-	}
 
-	if (!strcmp(crypto_alg, "aes")) {
-		if (!strcmp(crypto_mode, "xts")) {
-			priv->crypto_alg = CRYPTO_AES_XTS;
-			if (klen != 256 && klen != 512)
-				goto notsup;
-		} else if (!strcmp(crypto_mode, "cbc")) {
-			priv->crypto_alg = CRYPTO_AES_CBC;
-			if (klen != 128 && klen != 192 && klen != 256)
-				goto notsup;
-		} else {
-			goto notsup;
-		}
-		priv->crypto_klen = klen;
-	} else if (!strcmp(crypto_alg, "twofish")) {
-		if (!strcmp(crypto_mode, "xts")) {
-			priv->crypto_alg = CRYPTO_TWOFISH_XTS;
-			if (klen != 256 && klen != 512)
-				goto notsup;
-		} else if (!strcmp(crypto_mode, "cbc")) {
-			priv->crypto_alg = CRYPTO_TWOFISH_CBC;
-			if (klen != 128 && klen != 192 && klen != 256)
-				goto notsup;
-		} else {
-			goto notsup;
-		}
-		priv->crypto_klen = klen;
-	} else if (!strcmp(crypto_alg, "serpent")) {
-		if (!strcmp(crypto_mode, "xts")) {
-			priv->crypto_alg = CRYPTO_SERPENT_XTS;
-			if (klen != 256 && klen != 512)
-				goto notsup;
-		} else if (!strcmp(crypto_mode, "cbc")) {
-			priv->crypto_alg = CRYPTO_SERPENT_CBC;
-			if (klen != 128 && klen != 192 && klen != 256)
-				goto notsup;
-		} else {
-			goto notsup;
-		}
-		priv->crypto_klen = klen;
-	} else if (!strcmp(crypto_alg, "blowfish")) {
-		priv->crypto_alg = CRYPTO_BLF_CBC;
-		if (klen < 128 || klen > 448 || (klen % 8) != 0)
-			goto notsup;
-		priv->crypto_klen = klen;
-	} else if (!strcmp(crypto_alg, "3des") ||
-		   !strncmp(crypto_alg, "des3", 4)) {
-		priv->crypto_alg = CRYPTO_3DES_CBC;
-		if (klen != 168)
-			goto notsup;
-		priv->crypto_klen = 168;
-	} else if (!strcmp(crypto_alg, "camellia")) {
-		priv->crypto_alg = CRYPTO_CAMELLIA_CBC;
-		if (klen != 128 && klen != 192 && klen != 256)
-			goto notsup;
-		priv->crypto_klen = klen;
-	} else if (!strcmp(crypto_alg, "skipjack")) {
-		priv->crypto_alg = CRYPTO_SKIPJACK_CBC;
-		if (klen != 80)
-			goto notsup;
-		priv->crypto_klen = 80;
-	} else if (!strcmp(crypto_alg, "cast5")) {
-		priv->crypto_alg = CRYPTO_CAST_CBC;
-		if (klen != 128)
-			goto notsup;
-		priv->crypto_klen = 128;
-	} else if (!strcmp(crypto_alg, "null")) {
-		priv->crypto_alg = CRYPTO_NULL_CBC;
-		if (klen != 128)
-			goto notsup;
-		priv->crypto_klen = 128;
-	} else {
-		kprintf("dm_target_crypt: Unsupported crypto algorithm: %s\n",
-			crypto_alg);
-		goto notsup;
-	}
+	kprintf("dm_target_crypt: using crypto_cipher: %s\n",
+			priv->crypto_cipher->description);
 
 	/* Save length of param string */
 	priv->params_len = len;
@@ -712,7 +672,7 @@ dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 
 	dm_table_init_target(table_en, priv);
 
-	error = hex2key(key, priv->crypto_klen >> 3,
+	error = hex2key(hex_key, priv->crypto_klen / 8,
 			(u_int8_t *)priv->crypto_key);
 
 	if (error) {
@@ -723,7 +683,7 @@ dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 
 	/* Handle cmd */
 	for(i = 0; ivgens[i].name != NULL; i++) {
-		if (!strcmp(iv_mode, ivgens[i].name))
+		if (strcmp(iv_mode, ivgens[i].name) == 0)
 			break;
 	}
 
@@ -746,15 +706,10 @@ dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 
 	priv->ivgen = &ivgens[i];
 
-	priv->crypto_session.cri_alg = priv->crypto_alg;
-	priv->crypto_session.cri_key = (u_int8_t *)priv->crypto_key;
-	priv->crypto_session.cri_klen = priv->crypto_klen;
-	priv->crypto_session.cri_mlen = 0;
-	priv->crypto_session.cri_next = NULL;
+	error = priv->crypto_cipher->setkey(&priv->crypto_context,
+		(const u_int8_t *)priv->crypto_key,
+		priv->crypto_klen / 8);
 
-	error = crypto_newsession(&priv->crypto_sid,
-				  &priv->crypto_session,
-				  CRYPTOCAP_F_SOFTWARE | CRYPTOCAP_F_HARDWARE);
 	if (error) {
 		kprintf("dm_target_crypt: Error during crypto_newsession, "
 			"error = %d\n",
@@ -762,20 +717,53 @@ dm_target_crypt_init(dm_table_entry_t *table_en, int argc, char **argv)
 		goto notsup;
 	}
 
-	memset(key, '0', strlen(key));
+	memset(hex_key, '0', strlen(hex_key));
 	if (iv_opt) {
 		ksprintf(status_str, "%s-%s-%s:%s %s %ju %s %ju",
 		    crypto_alg, crypto_mode, iv_mode, iv_opt,
-		    key, iv_offset, dev, block_offset);
+		    hex_key, iv_offset, dev, block_offset);
 	} else {
 		ksprintf(status_str, "%s-%s-%s %s %ju %s %ju",
 		    crypto_alg, crypto_mode, iv_mode,
-		    key, iv_offset, dev, block_offset);
+		    hex_key, iv_offset, dev, block_offset);
 	}
 	priv->status_str = status_str;
 
-	/* Initialize mpipes */
-	dmtc_init_mpipe(priv);
+	/**
+	 * Allocate and start work queues / workers.
+	 */
+	priv->crypto_read_worker_contexts =
+	    kmalloc(sizeof(struct worker_context) * ncpus, M_DMCRYPT,
+		M_WAITOK | M_ZERO);
+	priv->crypto_write_worker_contexts =
+	    kmalloc(sizeof(struct worker_context) * ncpus, M_DMCRYPT,
+		M_WAITOK | M_ZERO);
+	priv->crypto_read_workqueues = kmalloc(sizeof(struct workqueue) * ncpus,
+	    M_DMCRYPT, M_WAITOK | M_ZERO);
+	priv->crypto_write_workqueues = kmalloc(sizeof(struct workqueue) *
+		ncpus,
+	    M_DMCRYPT, M_WAITOK | M_ZERO);
+
+	for (int cpu = 0; cpu < ncpus; ++cpu) {
+		priv->crypto_read_worker_contexts[cpu].data_buf =
+		    kmalloc(DMTC_BUF_SIZE, M_DMCRYPT, M_WAITOK);
+		priv->crypto_read_worker_contexts[cpu].data_buf_size =
+		    DMTC_BUF_SIZE;
+		priv->crypto_read_worker_contexts[cpu].data_buf_busy = false;
+		workqueue_start(&priv->crypto_read_workqueues[cpu], cpu,
+		    &priv->crypto_read_worker_contexts[cpu], 10);
+
+		priv->crypto_write_worker_contexts[cpu].data_buf =
+		    kmalloc(DMTC_BUF_SIZE, M_DMCRYPT, M_WAITOK);
+		priv->crypto_write_worker_contexts[cpu].data_buf_size =
+		    DMTC_BUF_SIZE;
+		priv->crypto_write_worker_contexts[cpu].data_buf_busy = false;
+		workqueue_start(&priv->crypto_write_workqueues[cpu], cpu,
+		    &priv->crypto_write_worker_contexts[cpu], 5);
+	}
+
+	priv->crypto_read_workqueue_next_rr = 0;
+	priv->crypto_write_workqueue_next_rr = 0;
 
 	return 0;
 
@@ -815,6 +803,25 @@ dm_target_crypt_destroy(dm_table_entry_t *table_en)
 		return 0;
 	dm_pdev_decr(priv->pdev);
 
+	/*
+	 * Stop work queues / free allocated memory.
+	 */
+
+	for (int cpu = 0; cpu < ncpus; cpu++) {
+		workqueue_stop(&priv->crypto_read_workqueues[cpu]);
+		kfree(priv->crypto_read_worker_contexts[cpu].data_buf,
+		    M_DMCRYPT);
+
+		workqueue_stop(&priv->crypto_write_workqueues[cpu]);
+		kfree(priv->crypto_write_worker_contexts[cpu].data_buf,
+		    M_DMCRYPT);
+	}
+	kfree(priv->crypto_read_worker_contexts, M_DMCRYPT);
+	kfree(priv->crypto_read_workqueues, M_DMCRYPT);
+
+	kfree(priv->crypto_write_worker_contexts, M_DMCRYPT);
+	kfree(priv->crypto_write_workqueues, M_DMCRYPT);
+
 	/*
 	 * Clean up the crypt config
 	 *
@@ -824,16 +831,12 @@ dm_target_crypt_destroy(dm_table_entry_t *table_en)
 	if (priv->status_str) {
 		dmtc_crypto_clear(priv->status_str, strlen(priv->status_str));
 		kfree(priv->status_str, M_DMCRYPT);
-		crypto_freesession(priv->crypto_sid);
 	}
 
 	if ((priv->ivgen) && (priv->ivgen->dtor != NULL)) {
 		priv->ivgen->dtor(priv, priv->ivgen_priv);
 	}
 
-	/* Destroy mpipes */
-	dmtc_destroy_mpipe(priv);
-
 	dmtc_crypto_clear(priv, sizeof(dm_target_crypt_config_t));
 	kfree(priv, M_DMCRYPT);
 
@@ -844,23 +847,17 @@ dm_target_crypt_destroy(dm_table_entry_t *table_en)
  *			STRATEGY SUPPORT FUNCTIONS			*
  ************************************************************************
  *
- * READ PATH:	doio -> bio_read_done -> crypto_work -> crypto_cb_read_done
- * WRITE PATH:	crypto_work -> crypto_cb_write_done -> doio -> bio_write_done
+ * READ PATH:	doio -> bio_read_done -> bio_read_decrypt
+ * WRITE PATH:	bio_write_encrypt -> doio -> bio_write_done
  */
 
-/*
- * Wrapper around crypto_dispatch() to match dispatch_t type
+/**
+ * Use of bio_caller_infoX:
+ *
+ * bio_caller_info1: priv
+ * bio_caller_info2: orig b_data pointer (WRITE PATH only)
+ * bio_caller_info3: data_buf (WRITE PATH only)
  */
-static void
-dmtc_crypto_dispatch(void *arg)
-{
-	struct cryptop *crp;
-
-	crp = (struct cryptop *)arg;
-	KKASSERT(crp != NULL);
-	KTR_LOG(dmcrypt_crypto_dispatch, crp);
-	crypto_dispatch(crp);
-}
 
 /*
  * Start IO operation, called from dmstrategy routine.
@@ -874,11 +871,14 @@ dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
 	priv = table_en->target_config;
 
 	/* Get rid of stuff we can't really handle */
-	if ((bp->b_cmd == BUF_CMD_READ) || (bp->b_cmd == BUF_CMD_WRITE)) {
-		if (((bp->b_bcount % DEV_BSIZE) != 0) || (bp->b_bcount == 0)) {
-			kprintf("dm_target_crypt_strategy: can't really "
-				"handle bp->b_bcount = %d\n",
-				bp->b_bcount);
+	if ((bp->b_cmd == BUF_CMD_READ) ||
+	    (bp->b_cmd == BUF_CMD_WRITE)) {
+		if (((bp->b_bcount % DEV_BSIZE) != 0) ||
+		    (bp->b_bcount == 0)) {
+			kprintf(
+			    "dm_target_crypt_strategy: can't really "
+			    "handle bp->b_bcount = %d\n",
+			    bp->b_bcount);
 			bp->b_error = EINVAL;
 			bp->b_flags |= B_ERROR | B_INVAL;
 			biodone(&bp->b_bio1);
@@ -886,13 +886,11 @@ dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
 		}
 	}
 
-	KTR_LOG(dmcrypt_crypt_strategy, bp->b_cmd, bp);
-
 	switch (bp->b_cmd) {
 	case BUF_CMD_READ:
 		bio = push_bio(&bp->b_bio1);
 		bio->bio_offset = bp->b_bio1.bio_offset +
-				  priv->block_offset * DEV_BSIZE;
+		    priv->block_offset * DEV_BSIZE;
 		bio->bio_caller_info1.ptr = priv;
 		bio->bio_done = dmtc_bio_read_done;
 		vn_strategy(priv->pdev->pdev_vnode, bio);
@@ -900,9 +898,9 @@ dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
 	case BUF_CMD_WRITE:
 		bio = push_bio(&bp->b_bio1);
 		bio->bio_offset = bp->b_bio1.bio_offset +
-				  priv->block_offset * DEV_BSIZE;
+		    priv->block_offset * DEV_BSIZE;
 		bio->bio_caller_info1.ptr = priv;
-		dmtc_crypto_write_start(priv, bio);
+		dmtc_bio_write_encrypt(priv, bio);
 		break;
 	default:
 		vn_strategy(priv->pdev->pdev_vnode, &bp->b_bio1);
@@ -911,9 +909,26 @@ dm_target_crypt_strategy(dm_table_entry_t *table_en, struct buf *bp)
 	return 0;
 }
 
+__inline static int
+select_read_workqueue_index(dm_target_crypt_config_t *priv)
+{
+	return atomic_fetchadd_int(&priv->crypto_read_workqueue_next_rr,
+		   1) %
+	    ncpus;
+}
+
+__inline static int
+select_write_workqueue_index(dm_target_crypt_config_t *priv)
+{
+	return atomic_fetchadd_int(
+		   &priv->crypto_write_workqueue_next_rr, 1) %
+	    ncpus;
+}
+
 /*
- * STRATEGY READ PATH PART 1/3 (after read BIO completes)
+ * STRATEGY READ PATH (after read BIO completes)
  */
+
 static void
 dmtc_bio_read_done(struct bio *bio)
 {
@@ -921,384 +936,231 @@ dmtc_bio_read_done(struct bio *bio)
 
 	dm_target_crypt_config_t *priv;
 
-	KTR_LOG(dmcrypt_bio_read_done, bio->bio_buf);
-
 	/*
 	 * If a read error occurs we shortcut the operation, otherwise
-	 * go on to stage 2.
+	 * go on to stage 2 (decrypt).
 	 */
 	if (bio->bio_buf->b_flags & B_ERROR) {
 		obio = pop_bio(bio);
 		biodone(obio);
 	} else {
 		priv = bio->bio_caller_info1.ptr;
-		dmtc_crypto_read_start(priv, bio);
+		dmtc_bio_read_decrypt(priv, bio);
 	}
 }
 
-/*
- * STRATEGY READ PATH PART 2/3
- */
+__inline static void
+dmtc_bio_read_decrypt_job_do(dm_target_crypt_config_t *priv,
+    struct bio *bio, struct worker_context *ctx);
+
 static void
-dmtc_crypto_read_retry(void *arg1, void *arg2)
+dmtc_bio_read_decrypt_job(void *arg1, void *arg2, void *ctx)
 {
-	dm_target_crypt_config_t *priv = arg1;
-	struct bio *bio = arg2;
-
-	dmtc_crypto_read_start(priv, bio);
+	dmtc_bio_read_decrypt_job_do(arg1, arg2, ctx);
 }
 
-static void
-dmtc_crypto_read_start(dm_target_crypt_config_t *priv, struct bio *bio)
+__inline static void
+dmtc_bio_read_decrypt_job_do(dm_target_crypt_config_t *priv,
+    struct bio *bio, struct worker_context *ctx)
 {
-	struct dmtc_helper *dmtc;
-	struct cryptodesc *crd;
-	struct cryptop *crp;
-	int i, bytes, sectors, sz;
-	off_t isector;
-	u_char *ptr, *space;
+	uint8_t *data_buf = ctx->data_buf;
+
+	KKASSERT(ctx->data_buf_busy == false);
 
 	/*
 	 * Note: b_resid no good after read I/O, it will be 0, use
 	 *	 b_bcount.
 	 */
-	bytes = bio->bio_buf->b_bcount;
-	isector = bio->bio_offset / DEV_BSIZE;	/* ivgen salt base? */
-	sectors = bytes / DEV_BSIZE;		/* Number of sectors */
-	sz = sectors * (sizeof(*crp) + sizeof(*crd));
+	int bytes = bio->bio_buf->b_bcount;
 
 	/*
+	 * Unconditionally copy in data. Never decrypt in place!
+	 *
 	 * For reads with bogus page we can't decrypt in place as stuff
 	 * can get ripped out from under us.
-	 *
-	 * XXX actually it looks like we can, and in any case the initial
-	 * read already completed and threw crypted data into the buffer
-	 * cache buffer.  Disable for now.
 	 */
-	space = mpipe_alloc_callback(&priv->read_mpipe,
-				     dmtc_crypto_read_retry, priv, bio);
-	if (space == NULL)
-		return;
-
-	dmtc = (struct dmtc_helper *)space;
-	dmtc->free_addr = space;
-	space += sizeof(struct dmtc_helper);
-	dmtc->orig_buf = NULL;
-	dmtc->data_buf = bio->bio_buf->b_data;
-	dmtc->priv = priv;
-	bio->bio_caller_info2.ptr = dmtc;
-	bio->bio_buf->b_error = 0;
-
-	/*
-	 * Load crypto descriptors (crp/crd loop)
-	 */
-	bzero(space, sz);
-	ptr = space;
-	bio->bio_caller_info3.value = sectors;
-	cpu_sfence();
-#if 0
-	kprintf("Read, bytes = %d (b_bcount), "
-		"sectors = %d (bio = %p, b_cmd = %d)\n",
-		bytes, sectors, bio, bio->bio_buf->b_cmd);
-#endif
-	for (i = 0; i < sectors; i++) {
-		crp = (struct cryptop *)ptr;
-		ptr += sizeof(*crp);
-		crd = (struct cryptodesc *)ptr;
-		ptr += sizeof (*crd);
-
-		crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
-
-		crp->crp_sid = priv->crypto_sid;
-		crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
+	memcpy(data_buf, bio->bio_buf->b_data, bytes);
 
-		crp->crp_opaque = (void *)bio;
+	bio->bio_buf->b_error = dmtc_bio_encdec(priv, data_buf, bytes,
+	    bio->bio_offset, priv->crypto_cipher->decrypt);
 
-		crp->crp_callback = dmtc_crypto_cb_read_done;
-		crp->crp_desc = crd;
-		crp->crp_etype = 0;
-		crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
-				 CRYPTO_F_BATCH;
+	if (bio->bio_buf->b_error) {
+		kprintf(
+		    "dm_target_crypt: dmtc_bio_read_decrypt error = %d\n",
+		    bio->bio_buf->b_error);
 
-		crd->crd_alg = priv->crypto_alg;
+		bio->bio_buf->b_flags |= B_ERROR;
+	} else {
+		memcpy(bio->bio_buf->b_data, data_buf, bytes);
+	}
 #if 0
-		crd->crd_key = (caddr_t)priv->crypto_key;
-		crd->crd_klen = priv->crypto_klen;
-#endif
-
-		crd->crd_skip = 0;
-		crd->crd_len = DEV_BSIZE /* XXX */;
-		crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
-		crd->crd_next = NULL;
-
-		crd->crd_flags &= ~CRD_F_ENCRYPT;
-
-		KTR_LOG(dmcrypt_crypto_read_start, crp, bio->bio_buf, i,
-		    sectors);
-
-		/*
-		 * Note: last argument is used to generate salt(?) and is
-		 *	 a 64 bit value, but the original code passed an
-		 *	 int.  Changing it now will break pre-existing
-		 *	 crypt volumes.
-		 */
-		priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
-				    isector + i, crp);
+	else if (bio->bio_buf->b_flags & B_HASBOGUS) {
+		memcpy(bio->bio_buf->b_data, dmtc->data_buf,
+		       bio->bio_buf->b_bcount);
 	}
+#endif
+	struct bio *obio = pop_bio(bio);
+	biodone(obio);
 }
 
-/*
- * STRATEGY READ PATH PART 3/3
- */
-static int
-dmtc_crypto_cb_read_done(struct cryptop *crp)
+static void
+dmtc_bio_read_decrypt(dm_target_crypt_config_t *priv, struct bio *bio)
 {
-	struct dmtc_helper *dmtc;
-	struct bio *bio, *obio;
-	int n;
-
-	if (crp->crp_etype == EAGAIN)
-		return crypto_dispatch(crp);
-
-	bio = (struct bio *)crp->crp_opaque;
-	KKASSERT(bio != NULL);
-
-	/*
-	 * Cumulative error
-	 */
-	if (crp->crp_etype) {
-		kprintf("dm_target_crypt: dmtc_crypto_cb_read_done "
-			"crp_etype = %d\n",
-			crp->crp_etype);
-		bio->bio_buf->b_error = crp->crp_etype;
-	}
-
-	/*
-	 * On the last chunk of the decryption we do any required copybacks
-	 * and complete the I/O.
-	 */
-	n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
-#if 0
-	kprintf("dmtc_crypto_cb_read_done %p, n = %d\n", bio, n);
-#endif
-
-	KTR_LOG(dmcrypt_crypto_cb_read_done, crp, bio->bio_buf, n);
+	int error = workqueue_submit_job(
+	    &priv->crypto_read_workqueues[select_read_workqueue_index(
+		priv)],
+	    dmtc_bio_read_decrypt_job, (void *)priv, (void *)bio);
+	if (error) {
+		bio->bio_buf->b_error = error;
+		kprintf(
+		    "dm_target_crypt: dmtc_bio_read_decrypt error = %d\n",
+		    bio->bio_buf->b_error);
 
-	if (n == 1) {
-		/*
-		 * For the B_HASBOGUS case we didn't decrypt in place,
-		 * so we need to copy stuff back into the buf.
-		 *
-		 * (disabled for now).
-		 */
-		dmtc = bio->bio_caller_info2.ptr;
-		if (bio->bio_buf->b_error) {
-			bio->bio_buf->b_flags |= B_ERROR;
-		}
-#if 0
-		else if (bio->bio_buf->b_flags & B_HASBOGUS) {
-			memcpy(bio->bio_buf->b_data, dmtc->data_buf,
-			       bio->bio_buf->b_bcount);
-		}
-#endif
-		mpipe_free(&dmtc->priv->read_mpipe, dmtc->free_addr);
-		obio = pop_bio(bio);
+		bio->bio_buf->b_flags |= B_ERROR;
+		struct bio *obio = pop_bio(bio);
 		biodone(obio);
 	}
-	return 0;
 }
+
 /* END OF STRATEGY READ SECTION */
 
 /*
- * STRATEGY WRITE PATH PART 1/3
+ * STRATEGY WRITE PATH
  */
 
+__inline static void
+dmtc_bio_write_encrypt_job_do(dm_target_crypt_config_t *priv,
+    struct bio *bio, struct worker_context *ctx);
+
 static void
-dmtc_crypto_write_retry(void *arg1, void *arg2)
+dmtc_bio_write_encrypt_job(void *arg1, void *arg2, void *ctx)
 {
-	dm_target_crypt_config_t *priv = arg1;
-	struct bio *bio = arg2;
-
-	KTR_LOG(dmcrypt_crypto_write_retry, bio->bio_buf);
-
-	dmtc_crypto_write_start(priv, bio);
+	dmtc_bio_write_encrypt_job_do(arg1, arg2, ctx);
 }
 
-static void
-dmtc_crypto_write_start(dm_target_crypt_config_t *priv, struct bio *bio)
+__inline static void
+dmtc_bio_write_encrypt_job_do(dm_target_crypt_config_t *priv,
+    struct bio *bio, struct worker_context *ctx)
 {
-	struct dmtc_helper *dmtc;
-	struct cryptodesc *crd;
-	struct cryptop *crp;
-	int i, bytes, sectors, sz;
-	off_t isector;
-	u_char *ptr, *space;
-
-	/*
-	 * Use b_bcount for consistency
-	 */
-	bytes = bio->bio_buf->b_bcount;
-
-	isector = bio->bio_offset / DEV_BSIZE;	/* ivgen salt base? */
-	sectors = bytes / DEV_BSIZE;		/* Number of sectors */
-	sz = sectors * (sizeof(*crp) + sizeof(*crd));
-
-	/*
-	 * For writes and reads with bogus page don't decrypt in place.
-	 */
-	space = mpipe_alloc_callback(&priv->write_mpipe,
-				     dmtc_crypto_write_retry, priv, bio);
-	if (space == NULL)
-		return;
+	uint8_t *data_buf = ctx->data_buf;
 
-	dmtc = (struct dmtc_helper *)space;
-	dmtc->free_addr = space;
-	space += sizeof(struct dmtc_helper);
-	memcpy(space + sz, bio->bio_buf->b_data, bytes);
+	KKASSERT(ctx->data_buf_busy == false);
 
-	bio->bio_caller_info2.ptr = dmtc;
-	bio->bio_buf->b_error = 0;
-
-	dmtc->orig_buf = bio->bio_buf->b_data;
-	dmtc->data_buf = space + sz;
-	dmtc->priv = priv;
+	ctx->data_buf_busy = true;
 
 	/*
-	 * Load crypto descriptors (crp/crd loop)
+	 * Use b_bcount for consistency
 	 */
-	bzero(space, sz);
-	ptr = space;
-	bio->bio_caller_info3.value = sectors;
-	cpu_sfence();
-#if 0
-	kprintf("Write, bytes = %d (b_bcount), "
-		"sectors = %d (bio = %p, b_cmd = %d)\n",
-		bytes, sectors, bio, bio->bio_buf->b_cmd);
-#endif
-	for (i = 0; i < sectors; i++) {
-		crp = (struct cryptop *)ptr;
-		ptr += sizeof(*crp);
-		crd = (struct cryptodesc *)ptr;
-		ptr += sizeof (*crd);
+	int bytes = bio->bio_buf->b_bcount;
 
-		crp->crp_buf = dmtc->data_buf + i * DEV_BSIZE;
+	KKASSERT(bytes <= ctx->data_buf_size);
 
-		crp->crp_sid = priv->crypto_sid;
-		crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
+	memcpy(data_buf, bio->bio_buf->b_data, bytes);
 
-		crp->crp_opaque = (void *)bio;
+	bio->bio_buf->b_error = dmtc_bio_encdec(priv, data_buf, bytes,
+	    bio->bio_offset, priv->crypto_cipher->encrypt);
 
-		crp->crp_callback = dmtc_crypto_cb_write_done;
-		crp->crp_desc = crd;
-		crp->crp_etype = 0;
-		crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
-				 CRYPTO_F_BATCH;
+	if (bio->bio_buf->b_error) {
+		kprintf(
+		    "dm_target_crypt: dmtc_bio_write_encrypt error = %d\n",
+		    bio->bio_buf->b_error);
 
-		crd->crd_alg = priv->crypto_alg;
-#if 0
-		crd->crd_key = (caddr_t)priv->crypto_key;
-		crd->crd_klen = priv->crypto_klen;
-#endif
-
-		crd->crd_skip = 0;
-		crd->crd_len = DEV_BSIZE /* XXX */;
-		crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
-		crd->crd_next = NULL;
-
-		crd->crd_flags |= CRD_F_ENCRYPT;
+		bio->bio_buf->b_flags |= B_ERROR;
+		struct bio *obio = pop_bio(bio);
+		biodone(obio);
+	} else {
+		bio->bio_caller_info2.ptr =
+		    bio->bio_buf->b_data; /* orig_buf */
+		bio->bio_caller_info3.ptr = ctx;
+		bio->bio_buf->b_data = data_buf;
+		bio->bio_done = dmtc_bio_write_done;
 
 		/*
-		 * Note: last argument is used to generate salt(?) and is
-		 *	 a 64 bit value, but the original code passed an
-		 *	 int.  Changing it now will break pre-existing
-		 *	 crypt volumes.
+		 * TODO: @dillon can't we just copy back data_buf to
+		 * bio->bio_buf->b_data and then call vn_stategy with
+		 * it, or do we have to call vn_strategy pointing to our
+		 * own local allocated memory?
 		 */
+		vn_strategy(priv->pdev->pdev_vnode, bio);
 
-		KTR_LOG(dmcrypt_crypto_write_start, crp, bio->bio_buf,
-		    i, sectors);
-
-		priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
-				    isector + i, crp);
+		while (ctx->data_buf_busy) {
+			tsleep(ctx, 0,
+			    "wait for dmtc_bio_write_done to complete",
+			    100);
+		}
 	}
 }
 
-/*
- * STRATEGY WRITE PATH PART 2/3
- */
-static int
-dmtc_crypto_cb_write_done(struct cryptop *crp)
+static void
+dmtc_bio_write_done(struct bio *bio)
 {
-	struct dmtc_helper *dmtc;
-	dm_target_crypt_config_t *priv;
-	struct bio *bio, *obio;
-	int n;
+	struct worker_context *ctx = bio->bio_caller_info3.ptr;
 
-	if (crp->crp_etype == EAGAIN)
-		return crypto_dispatch(crp);
+	ctx->data_buf_busy = false;
+	wakeup(ctx);
 
-	bio = (struct bio *)crp->crp_opaque;
-	KKASSERT(bio != NULL);
+	// Restore original bio buffer
+	bio->bio_buf->b_data = bio->bio_caller_info2.ptr;
 
-	/*
-	 * Cumulative error
-	 */
-	if (crp->crp_etype != 0) {
-		kprintf("dm_target_crypt: dmtc_crypto_cb_write_done "
-			"crp_etype = %d\n",
-		crp->crp_etype);
-		bio->bio_buf->b_error = crp->crp_etype;
-	}
-
-	/*
-	 * On the last chunk of the encryption we issue the write
-	 */
-	n = atomic_fetchadd_int(&bio->bio_caller_info3.value, -1);
-#if 0
-	kprintf("dmtc_crypto_cb_write_done %p, n = %d\n", bio, n);
-#endif
-
-	KTR_LOG(dmcrypt_crypto_cb_write_done, crp, bio->bio_buf, n);
+	struct bio *obio = pop_bio(bio);
+	biodone(obio);
+}
 
-	if (n == 1) {
-		dmtc = bio->bio_caller_info2.ptr;
-		priv = (dm_target_crypt_config_t *)bio->bio_caller_info1.ptr;
+static void
+dmtc_bio_write_encrypt(dm_target_crypt_config_t *priv, struct bio *bio)
+{
+	int error = workqueue_submit_job(
+	    &priv->crypto_write_workqueues[select_write_workqueue_index(
+		priv)],
+	    dmtc_bio_write_encrypt_job, (void *)priv, (void *)bio);
+	if (error) {
+		bio->bio_buf->b_error = error;
+		kprintf(
+		    "dm_target_crypt: dmtc_bio_write_encrypt error = %d\n",
+		    bio->bio_buf->b_error);
 
-		if (bio->bio_buf->b_error) {
-			bio->bio_buf->b_flags |= B_ERROR;
-			mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
-			obio = pop_bio(bio);
-			biodone(obio);
-		} else {
-			dmtc->orig_buf = bio->bio_buf->b_data;
-			bio->bio_buf->b_data = dmtc->data_buf;
-			bio->bio_done = dmtc_bio_write_done;
-			vn_strategy(priv->pdev->pdev_vnode, bio);
-		}
+		bio->bio_buf->b_flags |= B_ERROR;
+		struct bio *obio = pop_bio(bio);
+		biodone(obio);
 	}
-	return 0;
 }
 
-/*
- * STRATEGY WRITE PATH PART 3/3
- */
-static void
-dmtc_bio_write_done(struct bio *bio)
+/* END OF STRATEGY WRITE SECTION */
+
+static int
+dmtc_bio_encdec(dm_target_crypt_config_t *priv, uint8_t *data_buf,
+    int bytes, off_t offset, crypto_cipher_blockfn_t blockfn)
 {
-	struct dmtc_helper *dmtc;
-	struct bio *obio;
+	struct crypto_cipher_iv iv;
+	int sectors = bytes / DEV_BSIZE;    /* Number of sectors */
+	off_t isector = offset / DEV_BSIZE; /* ivgen salt base? */
+	int error = 0;
 
-	dmtc = bio->bio_caller_info2.ptr;
-	bio->bio_buf->b_data = dmtc->orig_buf;
-	mpipe_free(&dmtc->priv->write_mpipe, dmtc->free_addr);
+	KKASSERT((sectors * DEV_BSIZE) == bytes);
 
-	KTR_LOG(dmcrypt_bio_write_done, bio->bio_buf);
+	for (int i = 0; i < sectors; i++) {
+		/*
+		 * Note: last argument is used to generate salt(?) and
+		 * is a 64 bit value, but the original code passed an
+		 *	 int.  Changing it now will break pre-existing
+		 *	 crypt volumes.
+		 */
+		priv->ivgen->gen_iv(priv, (uint8_t *)&iv, sizeof(iv),
+		    isector + i);
 
-	obio = pop_bio(bio);
-	biodone(obio);
-}
-/* END OF STRATEGY WRITE SECTION */
+		error = blockfn(&priv->crypto_context,
+		    data_buf + i * DEV_BSIZE, DEV_BSIZE, &iv);
 
+		if (error) {
+			break;
+		}
+	}
 
+	// TODO: required?
+	explicit_bzero(&iv, sizeof(iv));
+
+	return (error);
+}
 
 /* DUMPING MAGIC */
 
@@ -1309,7 +1171,6 @@ dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_
 {
 	static struct dmtc_dump_helper dump_helper;
 	dm_target_crypt_config_t *priv;
-	int id;
 	static int first_call = 1;
 
 	priv = table_en->target_config;
@@ -1321,7 +1182,6 @@ dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_
 
 	/* Magically enable tsleep */
 	tsleep_crypto_dump = 1;
-	id = 0;
 
 	/*
 	 * 0 length means flush buffers and return
@@ -1343,18 +1203,7 @@ dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_
 	dump_helper.length = length;
 	dump_helper.offset = offset +
 	    priv->block_offset * DEV_BSIZE;
-	dump_helper.ident = &id;
-	dmtc_crypto_dump_start(priv, &dump_helper);
-
-	/*
-	 * Hackery to make stuff appear synchronous. The crypto callback will
-	 * set id to 1 and call wakeup on it. If the request completed
-	 * synchronously, id will be 1 and we won't bother to sleep. If not,
-	 * the crypto request will complete asynchronously and we sleep until
-	 * it's done.
-	 */
-	if (id == 0)
-		tsleep(&dump_helper, 0, "cryptdump", 0);
+	dmtc_crypto_dump(priv, &dump_helper);
 
 	dump_helper.offset = dm_pdev_correct_dump_offset(priv->pdev,
 	    dump_helper.offset);
@@ -1368,95 +1217,20 @@ dm_target_crypt_dump(dm_table_entry_t *table_en, void *data, size_t length, off_
 }
 
 static void
-dmtc_crypto_dump_start(dm_target_crypt_config_t *priv, struct dmtc_dump_helper *dump_helper)
+dmtc_crypto_dump(dm_target_crypt_config_t *priv, struct dmtc_dump_helper *dump_helper)
 {
-	struct cryptodesc *crd;
-	struct cryptop *crp;
-	int i, bytes, sectors;
-	off_t isector;
-
-	bytes = dump_helper->length;
+	int bytes = dump_helper->length;
 
-	isector = dump_helper->offset / DEV_BSIZE;	/* ivgen salt base? */
-	sectors = bytes / DEV_BSIZE;		/* Number of sectors */
-	dump_helper->sectors = sectors;
-#if 0
-	kprintf("Dump, bytes = %d, "
-		"sectors = %d, LENGTH=%zu\n", bytes, sectors, dump_helper->length);
-#endif
 	KKASSERT(dump_helper->length <= 65536);
 
 	memcpy(dump_helper->space, dump_helper->data, bytes);
 
-	cpu_sfence();
-
-	for (i = 0; i < sectors; i++) {
-		crp = &dump_helper->crp[i];
-		crd = &dump_helper->crd[i];
-
-		crp->crp_buf = dump_helper->space + i * DEV_BSIZE;
-
-		crp->crp_sid = priv->crypto_sid;
-		crp->crp_ilen = crp->crp_olen = DEV_BSIZE;
-
-		crp->crp_opaque = (void *)dump_helper;
-
-		crp->crp_callback = dmtc_crypto_cb_dump_done;
-		crp->crp_desc = crd;
-		crp->crp_etype = 0;
-		crp->crp_flags = CRYPTO_F_CBIFSYNC | CRYPTO_F_REL |
-				 CRYPTO_F_BATCH;
-
-		crd->crd_alg = priv->crypto_alg;
-
-		crd->crd_skip = 0;
-		crd->crd_len = DEV_BSIZE /* XXX */;
-		crd->crd_flags = CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
-		crd->crd_next = NULL;
-
-		crd->crd_flags |= CRD_F_ENCRYPT;
-
-		/*
-		 * Note: last argument is used to generate salt(?) and is
-		 *	 a 64 bit value, but the original code passed an
-		 *	 int.  Changing it now will break pre-existing
-		 *	 crypt volumes.
-		 */
-		priv->ivgen->gen_iv(priv, crd->crd_iv, sizeof(crd->crd_iv),
-				    isector + i, crp);
-	}
-}
-
-static int
-dmtc_crypto_cb_dump_done(struct cryptop *crp)
-{
-	struct dmtc_dump_helper *dump_helper;
-	int n;
-
-	if (crp->crp_etype == EAGAIN)
-		return crypto_dispatch(crp);
-
-	dump_helper = (struct dmtc_dump_helper *)crp->crp_opaque;
-	KKASSERT(dump_helper != NULL);
-
-	if (crp->crp_etype != 0) {
-		kprintf("dm_target_crypt: dmtc_crypto_cb_dump_done "
-			"crp_etype = %d\n",
-		crp->crp_etype);
-		return crp->crp_etype;
+	int error = dmtc_bio_encdec(priv, dump_helper->space, bytes, dump_helper->offset,
+			priv->crypto_cipher->encrypt);
+	if (error != 0) {
+		kprintf("dm_target_crypt: dmtc_crypto_dump = %d\n",
+		error);
 	}
-
-	/*
-	 * On the last chunk of the encryption we return control
-	 */
-	n = atomic_fetchadd_int(&dump_helper->sectors, -1);
-
-	if (n == 1) {
-		atomic_add_int(dump_helper->ident, 1);
-		wakeup(dump_helper);
-	}
-
-	return 0;
 }
 
 static int
@@ -1498,3 +1272,4 @@ dmtc_mod_handler(module_t mod, int type, void *unused)
 }
 
 DM_TARGET_MODULE(dm_target_crypt, dmtc_mod_handler);
+MODULE_DEPEND(dm_target_crypt, crypto, 2, 2, 2);
diff --git a/sys/dev/raid/arcmsr/arcmsr.h b/sys/dev/raid/arcmsr/arcmsr.h
index 5140e6b7e3..f6cd595f86 100644
--- a/sys/dev/raid/arcmsr/arcmsr.h
+++ b/sys/dev/raid/arcmsr/arcmsr.h
@@ -35,6 +35,7 @@
 * $FreeBSD: head/sys/dev/arcmsr/arcmsr.h 259565 2013-12-18 19:25:40Z delphij $
 */
 #include <sys/ioccom.h>
+#include <sys/bus.h>
 #define ARCMSR_SCSI_INITIATOR_ID	255
 #define ARCMSR_DEV_SECTOR_SIZE		512
 #define ARCMSR_MAX_XFER_SECTORS		4096
diff --git a/sys/opencrypto/cast.c b/sys/opencrypto/cast.c
deleted file mode 100644
index 4b493cd25f..0000000000
--- a/sys/opencrypto/cast.c
+++ /dev/null
@@ -1,242 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/cast.c,v 1.4 2007/07/05 06:59:14 peter Exp $	*/
-/*      $OpenBSD: cast.c,v 1.2 2000/06/06 06:49:47 deraadt Exp $       */
-/*-
- *	CAST-128 in C
- *	Written by Steve Reid <sreid@sea-to-sky.net>
- *	100% Public Domain - no warranty
- *	Released 1997.10.11
- */
-
-#include <sys/types.h>
-#include <opencrypto/cast.h>
-#include <opencrypto/castsb.h>
-
-/* Macros to access 8-bit bytes out of a 32-bit word */
-#define U_INT8_Ta(x) ( (u_int8_t) (x>>24) )
-#define U_INT8_Tb(x) ( (u_int8_t) ((x>>16)&255) )
-#define U_INT8_Tc(x) ( (u_int8_t) ((x>>8)&255) )
-#define U_INT8_Td(x) ( (u_int8_t) ((x)&255) )
-
-/* Circular left shift */
-#define ROL(x, n) ( ((x)<<(n)) | ((x)>>(32-(n))) )
-
-/* CAST-128 uses three different round functions */
-#define F1(l, r, i) \
-	t = ROL(key->xkey[i] + r, key->xkey[i+16]); \
-	l ^= ((cast_sbox1[U_INT8_Ta(t)] ^ cast_sbox2[U_INT8_Tb(t)]) - \
-	 cast_sbox3[U_INT8_Tc(t)]) + cast_sbox4[U_INT8_Td(t)];
-#define F2(l, r, i) \
-	t = ROL(key->xkey[i] ^ r, key->xkey[i+16]); \
-	l ^= ((cast_sbox1[U_INT8_Ta(t)] - cast_sbox2[U_INT8_Tb(t)]) + \
-	 cast_sbox3[U_INT8_Tc(t)]) ^ cast_sbox4[U_INT8_Td(t)];
-#define F3(l, r, i) \
-	t = ROL(key->xkey[i] - r, key->xkey[i+16]); \
-	l ^= ((cast_sbox1[U_INT8_Ta(t)] + cast_sbox2[U_INT8_Tb(t)]) ^ \
-	 cast_sbox3[U_INT8_Tc(t)]) - cast_sbox4[U_INT8_Td(t)];
-
-
-/***** Encryption Function *****/
-
-void cast_encrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock)
-{
-u_int32_t t, l, r;
-
-	/* Get inblock into l,r */
-	l = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) |
-	 ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3];
-	r = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) |
-	 ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7];
-	/* Do the work */
-	F1(l, r,  0);
-	F2(r, l,  1);
-	F3(l, r,  2);
-	F1(r, l,  3);
-	F2(l, r,  4);
-	F3(r, l,  5);
-	F1(l, r,  6);
-	F2(r, l,  7);
-	F3(l, r,  8);
-	F1(r, l,  9);
-	F2(l, r, 10);
-	F3(r, l, 11);
-	/* Only do full 16 rounds if key length > 80 bits */
-	if (key->rounds > 12) {
-		F1(l, r, 12);
-		F2(r, l, 13);
-		F3(l, r, 14);
-		F1(r, l, 15);
-	}
-	/* Put l,r into outblock */
-	outblock[0] = U_INT8_Ta(r);
-	outblock[1] = U_INT8_Tb(r);
-	outblock[2] = U_INT8_Tc(r);
-	outblock[3] = U_INT8_Td(r);
-	outblock[4] = U_INT8_Ta(l);
-	outblock[5] = U_INT8_Tb(l);
-	outblock[6] = U_INT8_Tc(l);
-	outblock[7] = U_INT8_Td(l);
-	/* Wipe clean */
-	t = l = r = 0;
-}
-
-
-/***** Decryption Function *****/
-
-void cast_decrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock)
-{
-u_int32_t t, l, r;
-
-	/* Get inblock into l,r */
-	r = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) |
-	 ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3];
-	l = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) |
-	 ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7];
-	/* Do the work */
-	/* Only do full 16 rounds if key length > 80 bits */
-	if (key->rounds > 12) {
-		F1(r, l, 15);
-		F3(l, r, 14);
-		F2(r, l, 13);
-		F1(l, r, 12);
-	}
-	F3(r, l, 11);
-	F2(l, r, 10);
-	F1(r, l,  9);
-	F3(l, r,  8);
-	F2(r, l,  7);
-	F1(l, r,  6);
-	F3(r, l,  5);
-	F2(l, r,  4);
-	F1(r, l,  3);
-	F3(l, r,  2);
-	F2(r, l,  1);
-	F1(l, r,  0);
-	/* Put l,r into outblock */
-	outblock[0] = U_INT8_Ta(l);
-	outblock[1] = U_INT8_Tb(l);
-	outblock[2] = U_INT8_Tc(l);
-	outblock[3] = U_INT8_Td(l);
-	outblock[4] = U_INT8_Ta(r);
-	outblock[5] = U_INT8_Tb(r);
-	outblock[6] = U_INT8_Tc(r);
-	outblock[7] = U_INT8_Td(r);
-	/* Wipe clean */
-	t = l = r = 0;
-}
-
-
-/***** Key Schedual *****/
-
-void cast_setkey(cast_key* key, u_int8_t* rawkey, int keybytes)
-{
-	u_int32_t t[4] = {0, 0, 0, 0}, z[4] = {0, 0, 0, 0}, x[4];
-	int i;
-
-	/* Set number of rounds to 12 or 16, depending on key length */
-	key->rounds = (keybytes <= 10 ? 12 : 16);
-
-	/* Copy key to workspace x */
-	for (i = 0; i < 4; i++) {
-		x[i] = 0;
-		if ((i*4+0) < keybytes) x[i] = (u_int32_t)rawkey[i*4+0] << 24;
-		if ((i*4+1) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+1] << 16;
-		if ((i*4+2) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+2] << 8;
-		if ((i*4+3) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+3];
-	}
-	/* Generate 32 subkeys, four at a time */
-	for (i = 0; i < 32; i+=4) {
-		switch (i & 4) {
-		 case 0:
-			t[0] = z[0] = x[0] ^ cast_sbox5[U_INT8_Tb(x[3])] ^
-			 cast_sbox6[U_INT8_Td(x[3])] ^ cast_sbox7[U_INT8_Ta(x[3])] ^
-			 cast_sbox8[U_INT8_Tc(x[3])] ^ cast_sbox7[U_INT8_Ta(x[2])];
-			t[1] = z[1] = x[2] ^ cast_sbox5[U_INT8_Ta(z[0])] ^
-			 cast_sbox6[U_INT8_Tc(z[0])] ^ cast_sbox7[U_INT8_Tb(z[0])] ^
-			 cast_sbox8[U_INT8_Td(z[0])] ^ cast_sbox8[U_INT8_Tc(x[2])];
-			t[2] = z[2] = x[3] ^ cast_sbox5[U_INT8_Td(z[1])] ^
-			 cast_sbox6[U_INT8_Tc(z[1])] ^ cast_sbox7[U_INT8_Tb(z[1])] ^
-			 cast_sbox8[U_INT8_Ta(z[1])] ^ cast_sbox5[U_INT8_Tb(x[2])];
-			t[3] = z[3] = x[1] ^ cast_sbox5[U_INT8_Tc(z[2])] ^
-			 cast_sbox6[U_INT8_Tb(z[2])] ^ cast_sbox7[U_INT8_Td(z[2])] ^
-			 cast_sbox8[U_INT8_Ta(z[2])] ^ cast_sbox6[U_INT8_Td(x[2])];
-			break;
-		 case 4:
-			t[0] = x[0] = z[2] ^ cast_sbox5[U_INT8_Tb(z[1])] ^
-			 cast_sbox6[U_INT8_Td(z[1])] ^ cast_sbox7[U_INT8_Ta(z[1])] ^
-			 cast_sbox8[U_INT8_Tc(z[1])] ^ cast_sbox7[U_INT8_Ta(z[0])];
-			t[1] = x[1] = z[0] ^ cast_sbox5[U_INT8_Ta(x[0])] ^
-			 cast_sbox6[U_INT8_Tc(x[0])] ^ cast_sbox7[U_INT8_Tb(x[0])] ^
-			 cast_sbox8[U_INT8_Td(x[0])] ^ cast_sbox8[U_INT8_Tc(z[0])];
-			t[2] = x[2] = z[1] ^ cast_sbox5[U_INT8_Td(x[1])] ^
-			 cast_sbox6[U_INT8_Tc(x[1])] ^ cast_sbox7[U_INT8_Tb(x[1])] ^
-			 cast_sbox8[U_INT8_Ta(x[1])] ^ cast_sbox5[U_INT8_Tb(z[0])];
-			t[3] = x[3] = z[3] ^ cast_sbox5[U_INT8_Tc(x[2])] ^
-			 cast_sbox6[U_INT8_Tb(x[2])] ^ cast_sbox7[U_INT8_Td(x[2])] ^
-			 cast_sbox8[U_INT8_Ta(x[2])] ^ cast_sbox6[U_INT8_Td(z[0])];
-			break;
-		}
-		switch (i & 12) {
-		 case 0:
-		 case 12:
-			key->xkey[i+0] = cast_sbox5[U_INT8_Ta(t[2])] ^ cast_sbox6[U_INT8_Tb(t[2])] ^
-			 cast_sbox7[U_INT8_Td(t[1])] ^ cast_sbox8[U_INT8_Tc(t[1])];
-			key->xkey[i+1] = cast_sbox5[U_INT8_Tc(t[2])] ^ cast_sbox6[U_INT8_Td(t[2])] ^
-			 cast_sbox7[U_INT8_Tb(t[1])] ^ cast_sbox8[U_INT8_Ta(t[1])];
-			key->xkey[i+2] = cast_sbox5[U_INT8_Ta(t[3])] ^ cast_sbox6[U_INT8_Tb(t[3])] ^
-			 cast_sbox7[U_INT8_Td(t[0])] ^ cast_sbox8[U_INT8_Tc(t[0])];
-			key->xkey[i+3] = cast_sbox5[U_INT8_Tc(t[3])] ^ cast_sbox6[U_INT8_Td(t[3])] ^
-			 cast_sbox7[U_INT8_Tb(t[0])] ^ cast_sbox8[U_INT8_Ta(t[0])];
-			break;
-		 case 4:
-		 case 8:
-			key->xkey[i+0] = cast_sbox5[U_INT8_Td(t[0])] ^ cast_sbox6[U_INT8_Tc(t[0])] ^
-			 cast_sbox7[U_INT8_Ta(t[3])] ^ cast_sbox8[U_INT8_Tb(t[3])];
-			key->xkey[i+1] = cast_sbox5[U_INT8_Tb(t[0])] ^ cast_sbox6[U_INT8_Ta(t[0])] ^
-			 cast_sbox7[U_INT8_Tc(t[3])] ^ cast_sbox8[U_INT8_Td(t[3])];
-			key->xkey[i+2] = cast_sbox5[U_INT8_Td(t[1])] ^ cast_sbox6[U_INT8_Tc(t[1])] ^
-			 cast_sbox7[U_INT8_Ta(t[2])] ^ cast_sbox8[U_INT8_Tb(t[2])];
-			key->xkey[i+3] = cast_sbox5[U_INT8_Tb(t[1])] ^ cast_sbox6[U_INT8_Ta(t[1])] ^
-			 cast_sbox7[U_INT8_Tc(t[2])] ^ cast_sbox8[U_INT8_Td(t[2])];
-			break;
-		}
-		switch (i & 12) {
-		 case 0:
-			key->xkey[i+0] ^= cast_sbox5[U_INT8_Tc(z[0])];
-			key->xkey[i+1] ^= cast_sbox6[U_INT8_Tc(z[1])];
-			key->xkey[i+2] ^= cast_sbox7[U_INT8_Tb(z[2])];
-			key->xkey[i+3] ^= cast_sbox8[U_INT8_Ta(z[3])];
-			break;
-		 case 4:
-			key->xkey[i+0] ^= cast_sbox5[U_INT8_Ta(x[2])];
-			key->xkey[i+1] ^= cast_sbox6[U_INT8_Tb(x[3])];
-			key->xkey[i+2] ^= cast_sbox7[U_INT8_Td(x[0])];
-			key->xkey[i+3] ^= cast_sbox8[U_INT8_Td(x[1])];
-			break;
-		 case 8:
-			key->xkey[i+0] ^= cast_sbox5[U_INT8_Tb(z[2])];
-			key->xkey[i+1] ^= cast_sbox6[U_INT8_Ta(z[3])];
-			key->xkey[i+2] ^= cast_sbox7[U_INT8_Tc(z[0])];
-			key->xkey[i+3] ^= cast_sbox8[U_INT8_Tc(z[1])];
-			break;
-		 case 12:
-			key->xkey[i+0] ^= cast_sbox5[U_INT8_Td(x[0])];
-			key->xkey[i+1] ^= cast_sbox6[U_INT8_Td(x[1])];
-			key->xkey[i+2] ^= cast_sbox7[U_INT8_Ta(x[2])];
-			key->xkey[i+3] ^= cast_sbox8[U_INT8_Tb(x[3])];
-			break;
-		}
-		if (i >= 16) {
-			key->xkey[i+0] &= 31;
-			key->xkey[i+1] &= 31;
-			key->xkey[i+2] &= 31;
-			key->xkey[i+3] &= 31;
-		}
-	}
-	/* Wipe clean */
-	for (i = 0; i < 4; i++) {
-		t[i] = x[i] = z[i] = 0;
-	}
-}
-
-/* Made in Canada */
-
diff --git a/sys/opencrypto/cast.h b/sys/opencrypto/cast.h
deleted file mode 100644
index 048d5eef6a..0000000000
--- a/sys/opencrypto/cast.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/cast.h,v 1.2 2005/01/07 02:29:16 imp Exp $	*/
-/*      $OpenBSD: cast.h,v 1.2 2002/03/14 01:26:51 millert Exp $       */
-
-/*-
- *	CAST-128 in C
- *	Written by Steve Reid <sreid@sea-to-sky.net>
- *	100% Public Domain - no warranty
- *	Released 1997.10.11
- */
-
-#ifndef _CAST_H_
-#define _CAST_H_
-
-typedef struct {
-	u_int32_t	xkey[32];	/* Key, after expansion */
-	int		rounds;		/* Number of rounds to use, 12 or 16 */
-} cast_key;
-
-void cast_setkey(cast_key * key, u_int8_t * rawkey, int keybytes);
-void cast_encrypt(cast_key * key, u_int8_t * inblock, u_int8_t * outblock);
-void cast_decrypt(cast_key * key, u_int8_t * inblock, u_int8_t * outblock);
-
-#endif /* ifndef _CAST_H_ */
diff --git a/sys/opencrypto/castsb.h b/sys/opencrypto/castsb.h
deleted file mode 100644
index 2845ac8fb5..0000000000
--- a/sys/opencrypto/castsb.h
+++ /dev/null
@@ -1,545 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/castsb.h,v 1.2 2005/01/07 02:29:16 imp Exp $	*/
-/*      $OpenBSD: castsb.h,v 1.1 2000/02/28 23:13:04 deraadt Exp $       */
-/*-
- *	CAST-128 in C
- *	Written by Steve Reid <sreid@sea-to-sky.net>
- *	100% Public Domain - no warranty
- *	Released 1997.10.11
- */
-
-static const u_int32_t cast_sbox1[256] = {
-	0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A,
-	0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949,
-	0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675,
-	0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E,
-	0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2,
-	0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D,
-	0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F,
-	0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0,
-	0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE,
-	0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7,
-	0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F,
-	0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935,
-	0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D,
-	0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D,
-	0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165,
-	0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50,
-	0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272,
-	0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE,
-	0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D,
-	0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3,
-	0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A,
-	0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167,
-	0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F,
-	0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291,
-	0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9,
-	0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779,
-	0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6,
-	0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2,
-	0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9,
-	0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511,
-	0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E,
-	0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D,
-	0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E,
-	0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5,
-	0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82,
-	0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324,
-	0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC,
-	0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C,
-	0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F,
-	0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC,
-	0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491,
-	0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D,
-	0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE,
-	0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96,
-	0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A,
-	0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A,
-	0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79,
-	0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D,
-	0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779,
-	0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD,
-	0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755,
-	0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6,
-	0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB,
-	0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9,
-	0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0,
-	0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872,
-	0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79,
-	0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C,
-	0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298,
-	0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E,
-	0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571,
-	0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9,
-	0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D,
-	0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF
-};
-
-static const u_int32_t cast_sbox2[256] = {
-	0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380,
-	0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651,
-	0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA,
-	0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3,
-	0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909,
-	0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB,
-	0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B,
-	0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806,
-	0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4,
-	0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B,
-	0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F,
-	0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359,
-	0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21,
-	0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B,
-	0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D,
-	0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C,
-	0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F,
-	0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34,
-	0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D,
-	0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB,
-	0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4,
-	0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD,
-	0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801,
-	0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860,
-	0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755,
-	0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B,
-	0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709,
-	0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304,
-	0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B,
-	0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B,
-	0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C,
-	0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF,
-	0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9,
-	0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C,
-	0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3,
-	0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13,
-	0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9,
-	0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F,
-	0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB,
-	0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6,
-	0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4,
-	0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6,
-	0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43,
-	0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58,
-	0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8,
-	0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906,
-	0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171,
-	0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D,
-	0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89,
-	0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6,
-	0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B,
-	0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4,
-	0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB,
-	0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6,
-	0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E,
-	0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F,
-	0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA,
-	0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249,
-	0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA,
-	0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA,
-	0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD,
-	0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9,
-	0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF,
-	0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1
-};
-
-static const u_int32_t cast_sbox3[256] = {
-	0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907,
-	0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90,
-	0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE,
-	0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5,
-	0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E,
-	0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E,
-	0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC,
-	0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240,
-	0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E,
-	0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5,
-	0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F,
-	0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B,
-	0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99,
-	0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71,
-	0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F,
-	0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04,
-	0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380,
-	0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82,
-	0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8,
-	0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15,
-	0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504,
-	0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2,
-	0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6,
-	0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176,
-	0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E,
-	0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148,
-	0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D,
-	0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC,
-	0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1,
-	0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341,
-	0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C,
-	0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E,
-	0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15,
-	0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51,
-	0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4,
-	0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F,
-	0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B,
-	0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A,
-	0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392,
-	0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B,
-	0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231,
-	0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B,
-	0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889,
-	0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5,
-	0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67,
-	0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45,
-	0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49,
-	0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536,
-	0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D,
-	0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC,
-	0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D,
-	0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0,
-	0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E,
-	0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69,
-	0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767,
-	0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2,
-	0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE,
-	0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49,
-	0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24,
-	0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D,
-	0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0,
-	0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A,
-	0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5,
-	0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783
-};
-
-static const u_int32_t cast_sbox4[256] = {
-	0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298,
-	0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1,
-	0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120,
-	0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF,
-	0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220,
-	0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15,
-	0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE,
-	0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121,
-	0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701,
-	0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25,
-	0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B,
-	0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5,
-	0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93,
-	0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB,
-	0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746,
-	0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5,
-	0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9,
-	0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D,
-	0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB,
-	0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6,
-	0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C,
-	0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23,
-	0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7,
-	0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003,
-	0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340,
-	0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6,
-	0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327,
-	0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119,
-	0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC,
-	0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24,
-	0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205,
-	0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A,
-	0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031,
-	0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79,
-	0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5,
-	0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF,
-	0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C,
-	0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26,
-	0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69,
-	0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB,
-	0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9,
-	0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7,
-	0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF,
-	0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417,
-	0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3,
-	0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2,
-	0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2,
-	0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2,
-	0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF,
-	0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A,
-	0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091,
-	0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919,
-	0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF,
-	0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF,
-	0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF,
-	0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876,
-	0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367,
-	0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB,
-	0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C,
-	0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04,
-	0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43,
-	0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282,
-	0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E,
-	0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2
-};
-
-static const u_int32_t cast_sbox5[256] = {
-	0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911,
-	0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F,
-	0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00,
-	0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A,
-	0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180,
-	0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF,
-	0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2,
-	0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02,
-	0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725,
-	0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A,
-	0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B,
-	0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7,
-	0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571,
-	0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9,
-	0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC,
-	0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981,
-	0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA,
-	0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774,
-	0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263,
-	0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655,
-	0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468,
-	0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2,
-	0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B,
-	0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910,
-	0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284,
-	0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1,
-	0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4,
-	0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA,
-	0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7,
-	0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049,
-	0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE,
-	0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F,
-	0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6,
-	0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA,
-	0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4,
-	0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE,
-	0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561,
-	0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3,
-	0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6,
-	0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840,
-	0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406,
-	0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4,
-	0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472,
-	0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2,
-	0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487,
-	0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7,
-	0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288,
-	0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5,
-	0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2,
-	0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E,
-	0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78,
-	0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E,
-	0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76,
-	0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801,
-	0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0,
-	0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD,
-	0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58,
-	0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0,
-	0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2,
-	0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20,
-	0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE,
-	0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8,
-	0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55,
-	0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4
-};
-
-static const u_int32_t cast_sbox6[256] = {
-	0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C,
-	0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC,
-	0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9,
-	0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138,
-	0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E,
-	0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367,
-	0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866,
-	0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98,
-	0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C,
-	0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072,
-	0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD,
-	0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3,
-	0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53,
-	0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD,
-	0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D,
-	0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8,
-	0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF,
-	0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9,
-	0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807,
-	0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54,
-	0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A,
-	0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387,
-	0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563,
-	0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC,
-	0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0,
-	0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF,
-	0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE,
-	0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF,
-	0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0,
-	0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F,
-	0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2,
-	0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289,
-	0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853,
-	0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950,
-	0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA,
-	0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F,
-	0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9,
-	0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B,
-	0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751,
-	0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE,
-	0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358,
-	0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13,
-	0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397,
-	0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976,
-	0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459,
-	0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0,
-	0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4,
-	0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891,
-	0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F,
-	0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA,
-	0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB,
-	0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC,
-	0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2,
-	0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084,
-	0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB,
-	0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25,
-	0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B,
-	0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121,
-	0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B,
-	0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5,
-	0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855,
-	0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD,
-	0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454,
-	0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F
-};
-
-static const u_int32_t cast_sbox7[256] = {
-	0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693,
-	0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F,
-	0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82,
-	0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE,
-	0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD,
-	0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43,
-	0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F,
-	0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19,
-	0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9,
-	0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2,
-	0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E,
-	0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516,
-	0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83,
-	0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88,
-	0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E,
-	0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816,
-	0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A,
-	0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756,
-	0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F,
-	0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A,
-	0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B,
-	0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264,
-	0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78,
-	0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688,
-	0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D,
-	0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28,
-	0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802,
-	0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3,
-	0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9,
-	0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7,
-	0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302,
-	0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06,
-	0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858,
-	0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033,
-	0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A,
-	0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A,
-	0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4,
-	0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566,
-	0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF,
-	0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509,
-	0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9,
-	0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962,
-	0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C,
-	0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E,
-	0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07,
-	0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C,
-	0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939,
-	0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C,
-	0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E,
-	0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285,
-	0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378,
-	0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301,
-	0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD,
-	0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE,
-	0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567,
-	0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767,
-	0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2,
-	0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647,
-	0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF,
-	0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914,
-	0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2,
-	0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C,
-	0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA,
-	0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3
-};
-
-static const u_int32_t cast_sbox8[256] = {
-	0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095,
-	0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5,
-	0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174,
-	0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC,
-	0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940,
-	0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD,
-	0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42,
-	0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D,
-	0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164,
-	0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2,
-	0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4,
-	0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862,
-	0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0,
-	0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC,
-	0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6,
-	0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C,
-	0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491,
-	0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E,
-	0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B,
-	0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039,
-	0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8,
-	0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8,
-	0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006,
-	0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42,
-	0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564,
-	0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5,
-	0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB,
-	0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472,
-	0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC,
-	0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225,
-	0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8,
-	0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C,
-	0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441,
-	0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB,
-	0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F,
-	0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054,
-	0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504,
-	0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70,
-	0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C,
-	0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC,
-	0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6,
-	0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C,
-	0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD,
-	0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3,
-	0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4,
-	0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4,
-	0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC,
-	0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101,
-	0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA,
-	0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F,
-	0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF,
-	0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E,
-	0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603,
-	0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A,
-	0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37,
-	0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C,
-	0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819,
-	0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384,
-	0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D,
-	0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C,
-	0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347,
-	0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82,
-	0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D,
-	0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E
-};
-
diff --git a/sys/opencrypto/criov.c b/sys/opencrypto/criov.c
deleted file mode 100644
index a6725d9b15..0000000000
--- a/sys/opencrypto/criov.c
+++ /dev/null
@@ -1,197 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/criov.c,v 1.5 2006/06/04 22:15:13 pjd Exp $	*/
-/*      $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $	*/
-
-/*-
- * Copyright (c) 1999 Theo de Raadt
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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/param.h>
-#include <sys/systm.h>
-#include <sys/proc.h>
-#include <sys/bus.h>
-#include <sys/errno.h>
-#include <sys/malloc.h>
-#include <sys/kernel.h>
-#include <sys/mbuf.h>
-#include <sys/uio.h>
-
-#include <opencrypto/cryptodev.h>
-
-/*
- * This macro is only for avoiding code duplication, as we need to skip
- * given number of bytes in the same way in three functions below.
- */
-#define	CUIO_SKIP()	do {						\
-	KASSERT(off >= 0, ("%s: off %d < 0", __func__, off));		\
-	KASSERT(len >= 0, ("%s: len %d < 0", __func__, len));		\
-	while (off > 0) {						\
-		KASSERT(iol >= 0, ("%s: empty in skip", __func__));	\
-		if (off < iov->iov_len)					\
-			break;						\
-		off -= iov->iov_len;					\
-		iol--;							\
-		iov++;							\
-	}								\
-} while (0)
-
-void
-cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
-{
-	struct iovec *iov = uio->uio_iov;
-	int iol = uio->uio_iovcnt;
-	unsigned count;
-
-	CUIO_SKIP();
-	while (len > 0) {
-		KASSERT(iol >= 0, ("%s: empty", __func__));
-		count = min(iov->iov_len - off, len);
-		bcopy(((caddr_t)iov->iov_base) + off, cp, count);
-		len -= count;
-		cp += count;
-		off = 0;
-		iol--;
-		iov++;
-	}
-}
-
-void
-cuio_copyback(struct uio* uio, int off, int len, caddr_t cp)
-{
-	struct iovec *iov = uio->uio_iov;
-	int iol = uio->uio_iovcnt;
-	unsigned count;
-
-	CUIO_SKIP();
-	while (len > 0) {
-		KASSERT(iol >= 0, ("%s: empty", __func__));
-		count = min(iov->iov_len - off, len);
-		bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
-		len -= count;
-		cp += count;
-		off = 0;
-		iol--;
-		iov++;
-	}
-}
-
-/*
- * Return a pointer to iov/offset of location in iovec list.
- */
-struct iovec *
-cuio_getptr(struct uio *uio, int loc, int *off)
-{
-	struct iovec *iov = uio->uio_iov;
-	int iol = uio->uio_iovcnt;
-
-	while (loc >= 0) {
-		/* Normal end of search */
-		if (loc < iov->iov_len) {
-	    		*off = loc;
-	    		return (iov);
-		}
-
-		loc -= iov->iov_len;
-		if (iol == 0) {
-			if (loc == 0) {
-				/* Point at the end of valid data */
-				*off = iov->iov_len;
-				return (iov);
-			} else
-				return (NULL);
-		} else {
-			iov++, iol--;
-		}
-    	}
-
-	return (NULL);
-}
-
-/*
- * Apply function f to the data in an iovec list starting "off" bytes from
- * the beginning, continuing for "len" bytes.
- */
-int
-cuio_apply(struct uio *uio, int off, int len, int (*f)(void *, void *, u_int),
-    void *arg)
-{
-	struct iovec *iov = uio->uio_iov;
-	int iol = uio->uio_iovcnt;
-	unsigned count;
-	int rval;
-
-	CUIO_SKIP();
-	while (len > 0) {
-		KASSERT(iol >= 0, ("%s: empty", __func__));
-		count = min(iov->iov_len - off, len);
-		rval = (*f)(arg, ((caddr_t)iov->iov_base) + off, count);
-		if (rval)
-			return (rval);
-		len -= count;
-		off = 0;
-		iol--;
-		iov++;
-	}
-	return (0);
-}
-
-void
-crypto_copyback(int flags, caddr_t buf, int off, int size, caddr_t in)
-{
-
-	if ((flags & CRYPTO_F_IMBUF) != 0)
-		m_copyback2((struct mbuf *)buf, off, size, in, M_WAITOK);
-	else if ((flags & CRYPTO_F_IOV) != 0)
-		cuio_copyback((struct uio *)buf, off, size, in);
-	else
-		bcopy(in, buf + off, size);
-}
-
-void
-crypto_copydata(int flags, caddr_t buf, int off, int size, caddr_t out)
-{
-
-	if ((flags & CRYPTO_F_IMBUF) != 0)
-		m_copydata((struct mbuf *)buf, off, size, out);
-	else if ((flags & CRYPTO_F_IOV) != 0)
-		cuio_copydata((struct uio *)buf, off, size, out);
-	else
-		bcopy(buf + off, out, size);
-}
-
-int
-crypto_apply(int flags, caddr_t buf, int off, int len,
-    int (*f)(void *, void *, u_int), void *arg)
-{
-	int error;
-
-	if ((flags & CRYPTO_F_IMBUF) != 0)
-		error = m_apply((struct mbuf *)buf, off, len, f, arg);
-	else if ((flags & CRYPTO_F_IOV) != 0)
-		error = cuio_apply((struct uio *)buf, off, len, f, arg);
-	else
-		error = (*f)(arg, buf + off, len);
-	return (error);
-}
diff --git a/sys/opencrypto/crypto.c b/sys/opencrypto/crypto.c
deleted file mode 100644
index 4a87c90f8e..0000000000
--- a/sys/opencrypto/crypto.c
+++ /dev/null
@@ -1,1651 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/crypto.c,v 1.28 2007/10/20 23:23:22 julian Exp $	*/
-/*-
- * Copyright (c) 2002-2006 Sam Leffler.  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 AUTHOR ``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 AUTHOR 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.
- */
-
-/*
- * Cryptographic Subsystem.
- *
- * This code is derived from the Openbsd Cryptographic Framework (OCF)
- * that has the copyright shown below.  Very little of the original
- * code remains.
- */
-
-/*-
- * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
- *
- * This code was written by Angelos D. Keromytis in Athens, Greece, in
- * February 2000. Network Security Technologies Inc. (NSTI) kindly
- * supported the development of this code.
- *
- * Copyright (c) 2000, 2001 Angelos D. Keromytis
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all source code copies of any software which is or includes a copy or
- * modification of this software.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- */
-
-#define	CRYPTO_TIMING				/* enable timing support */
-
-#include "opt_ddb.h"
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/eventhandler.h>
-#include <sys/kernel.h>
-#include <sys/kthread.h>
-#include <sys/lock.h>
-#include <sys/module.h>
-#include <sys/malloc.h>
-#include <sys/proc.h>
-#include <sys/sysctl.h>
-#include <sys/objcache.h>
-
-#include <sys/thread2.h>
-
-#include <ddb/ddb.h>
-
-#include <opencrypto/cryptodev.h>
-
-#include <sys/kobj.h>
-#include <sys/bus.h>
-#include "cryptodev_if.h"
-
-/*
- * Crypto drivers register themselves by allocating a slot in the
- * crypto_drivers table with crypto_get_driverid() and then registering
- * each algorithm they support with crypto_register() and crypto_kregister().
- */
-static	struct lock crypto_drivers_lock;	/* lock on driver table */
-#define	CRYPTO_DRIVER_LOCK()	lockmgr(&crypto_drivers_lock, LK_EXCLUSIVE)
-#define	CRYPTO_DRIVER_UNLOCK()	lockmgr(&crypto_drivers_lock, LK_RELEASE)
-#define	CRYPTO_DRIVER_ASSERT()	KKASSERT(lockstatus(&crypto_drivers_lock, curthread) != 0)
-
-/*
- * Crypto device/driver capabilities structure.
- *
- * Synchronization:
- * (d) - protected by CRYPTO_DRIVER_LOCK()
- * (q) - protected by CRYPTO_Q_LOCK()
- * Not tagged fields are read-only.
- */
-struct cryptocap {
-	device_t	cc_dev;			/* (d) device/driver */
-	u_int32_t	cc_sessions;		/* (d) # of sessions */
-	u_int32_t	cc_koperations;		/* (d) # os asym operations */
-	/*
-	 * Largest possible operator length (in bits) for each type of
-	 * encryption algorithm. XXX not used
-	 */
-	u_int16_t	cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
-	u_int8_t	cc_alg[CRYPTO_ALGORITHM_MAX + 1];
-	u_int8_t	cc_kalg[CRK_ALGORITHM_MAX + 1];
-
-	int		cc_flags;		/* (d) flags */
-#define CRYPTOCAP_F_CLEANUP	0x80000000	/* needs resource cleanup */
-	int		cc_qblocked;		/* (q) symmetric q blocked */
-	int		cc_kqblocked;		/* (q) asymmetric q blocked */
-};
-static	struct cryptocap *crypto_drivers = NULL;
-static	int crypto_drivers_num = 0;
-
-typedef struct crypto_tdinfo {
-	TAILQ_HEAD(,cryptop)	crp_q;		/* request queues */
-	TAILQ_HEAD(,cryptkop)	crp_kq;
-	thread_t		crp_td;
-	struct lock		crp_lock;
-	int			crp_sleep;
-} *crypto_tdinfo_t;
-
-/*
- * There are two queues for crypto requests; one for symmetric (e.g.
- * cipher) operations and one for asymmetric (e.g. MOD) operations.
- * See below for how synchronization is handled.
- * A single lock is used to lock access to both queues.  We could
- * have one per-queue but having one simplifies handling of block/unblock
- * operations.
- */
-static  struct crypto_tdinfo tdinfo_array[MAXCPU];
-
-#define	CRYPTO_Q_LOCK(tdinfo)	lockmgr(&tdinfo->crp_lock, LK_EXCLUSIVE)
-#define	CRYPTO_Q_UNLOCK(tdinfo)	lockmgr(&tdinfo->crp_lock, LK_RELEASE)
-
-/*
- * There are two queues for processing completed crypto requests; one
- * for the symmetric and one for the asymmetric ops.  We only need one
- * but have two to avoid type futzing (cryptop vs. cryptkop).  A single
- * lock is used to lock access to both queues.  Note that this lock
- * must be separate from the lock on request queues to insure driver
- * callbacks don't generate lock order reversals.
- */
-static	TAILQ_HEAD(,cryptop) crp_ret_q;		/* callback queues */
-static	TAILQ_HEAD(,cryptkop) crp_ret_kq;
-static	struct lock crypto_ret_q_lock;
-#define	CRYPTO_RETQ_LOCK()	lockmgr(&crypto_ret_q_lock, LK_EXCLUSIVE)
-#define	CRYPTO_RETQ_UNLOCK()	lockmgr(&crypto_ret_q_lock, LK_RELEASE)
-#define	CRYPTO_RETQ_EMPTY()	(TAILQ_EMPTY(&crp_ret_q) && TAILQ_EMPTY(&crp_ret_kq))
-
-/*
- * Crypto op and desciptor data structures are allocated
- * from separate object caches.
- */
-static struct objcache *cryptop_oc, *cryptodesc_oc;
-
-static MALLOC_DEFINE(M_CRYPTO_OP, "crypto op", "crypto op");
-static MALLOC_DEFINE(M_CRYPTO_DESC, "crypto desc", "crypto desc");
-
-int	crypto_userasymcrypto = 1;	/* userland may do asym crypto reqs */
-SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
-	   &crypto_userasymcrypto, 0,
-	   "Enable/disable user-mode access to asymmetric crypto support");
-int	crypto_devallowsoft = 0;	/* only use hardware crypto for asym */
-SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
-	   &crypto_devallowsoft, 0,
-	   "Enable/disable use of software asym crypto support");
-int	crypto_altdispatch = 0;		/* dispatch to alternative cpu */
-SYSCTL_INT(_kern, OID_AUTO, cryptoaltdispatch, CTLFLAG_RW,
-	   &crypto_altdispatch, 0,
-	   "Do not queue crypto op on current cpu");
-
-MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
-
-static	void crypto_proc(void *dummy);
-static	void crypto_ret_proc(void *dummy);
-static	struct thread *cryptoretthread;
-static	void crypto_destroy(void);
-static	int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
-static	int crypto_kinvoke(struct cryptkop *krp, int flags);
-
-static struct cryptostats cryptostats;
-SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
-	    cryptostats, "Crypto system statistics");
-
-#ifdef CRYPTO_TIMING
-static	int crypto_timing = 0;
-SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
-	   &crypto_timing, 0, "Enable/disable crypto timing support");
-#endif
-
-static int
-crypto_init(void)
-{
-	crypto_tdinfo_t tdinfo;
-	int error;
-	int n;
-
-	lockinit(&crypto_drivers_lock, "crypto driver table", 0, LK_CANRECURSE);
-
-	TAILQ_INIT(&crp_ret_q);
-	TAILQ_INIT(&crp_ret_kq);
-	lockinit(&crypto_ret_q_lock, "crypto return queues", 0, LK_CANRECURSE);
-
-	cryptop_oc = objcache_create_simple(M_CRYPTO_OP, sizeof(struct cryptop));
-	cryptodesc_oc = objcache_create_simple(M_CRYPTO_DESC,
-				sizeof(struct cryptodesc));
-	if (cryptodesc_oc == NULL || cryptop_oc == NULL) {
-		kprintf("crypto_init: cannot setup crypto caches\n");
-		error = ENOMEM;
-		goto bad;
-	}
-
-	crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
-	crypto_drivers = kmalloc(crypto_drivers_num * sizeof(struct cryptocap),
-				 M_CRYPTO_DATA, M_WAITOK | M_ZERO);
-
-	for (n = 0; n < ncpus; ++n) {
-		tdinfo = &tdinfo_array[n];
-		TAILQ_INIT(&tdinfo->crp_q);
-		TAILQ_INIT(&tdinfo->crp_kq);
-		lockinit(&tdinfo->crp_lock, "crypto op queues",
-			 0, LK_CANRECURSE);
-		kthread_create_cpu(crypto_proc, tdinfo, &tdinfo->crp_td,
-				   n, "crypto %d", n);
-	}
-	kthread_create(crypto_ret_proc, NULL,
-		       &cryptoretthread, "crypto returns");
-	return 0;
-bad:
-	crypto_destroy();
-	return error;
-}
-
-/*
- * Signal a crypto thread to terminate.  We use the driver
- * table lock to synchronize the sleep/wakeups so that we
- * are sure the threads have terminated before we release
- * the data structures they use.  See crypto_finis below
- * for the other half of this song-and-dance.
- */
-static void
-crypto_terminate(struct thread **tp, void *q)
-{
-	struct thread *t;
-
-	KKASSERT(lockstatus(&crypto_drivers_lock, curthread) != 0);
-	t = *tp;
-	*tp = NULL;
-	if (t) {
-		kprintf("crypto_terminate: start\n");
-		wakeup_one(q);
-		crit_enter();
-		tsleep_interlock(t, 0);
-		CRYPTO_DRIVER_UNLOCK();	/* let crypto_finis progress */
-		crit_exit();
-		tsleep(t, PINTERLOCKED, "crypto_destroy", 0);
-		CRYPTO_DRIVER_LOCK();
-		kprintf("crypto_terminate: end\n");
-	}
-}
-
-static void
-crypto_destroy(void)
-{
-	crypto_tdinfo_t tdinfo;
-	int n;
-
-	/*
-	 * Terminate any crypto threads.
-	 */
-	CRYPTO_DRIVER_LOCK();
-	for (n = 0; n < ncpus; ++n) {
-		tdinfo = &tdinfo_array[n];
-		crypto_terminate(&tdinfo->crp_td, &tdinfo->crp_q);
-		lockuninit(&tdinfo->crp_lock);
-	}
-	crypto_terminate(&cryptoretthread, &crp_ret_q);
-	CRYPTO_DRIVER_UNLOCK();
-
-	/* XXX flush queues??? */
-
-	/*
-	 * Reclaim dynamically allocated resources.
-	 */
-	if (crypto_drivers != NULL)
-		kfree(crypto_drivers, M_CRYPTO_DATA);
-
-	if (cryptodesc_oc != NULL)
-		objcache_destroy(cryptodesc_oc);
-	if (cryptop_oc != NULL)
-		objcache_destroy(cryptop_oc);
-	lockuninit(&crypto_ret_q_lock);
-	lockuninit(&crypto_drivers_lock);
-}
-
-static struct cryptocap *
-crypto_checkdriver(u_int32_t hid)
-{
-	if (crypto_drivers == NULL)
-		return NULL;
-	return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
-}
-
-/*
- * Compare a driver's list of supported algorithms against another
- * list; return non-zero if all algorithms are supported.
- */
-static int
-driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
-{
-	const struct cryptoini *cr;
-
-	/* See if all the algorithms are supported. */
-	for (cr = cri; cr; cr = cr->cri_next)
-		if (cap->cc_alg[cr->cri_alg] == 0)
-			return 0;
-	return 1;
-}
-
-/*
- * Select a driver for a new session that supports the specified
- * algorithms and, optionally, is constrained according to the flags.
- * The algorithm we use here is pretty stupid; just use the
- * first driver that supports all the algorithms we need. If there
- * are multiple drivers we choose the driver with the fewest active
- * sessions.  We prefer hardware-backed drivers to software ones.
- *
- * XXX We need more smarts here (in real life too, but that's
- * XXX another story altogether).
- */
-static struct cryptocap *
-crypto_select_driver(const struct cryptoini *cri, int flags)
-{
-	struct cryptocap *cap, *best;
-	int match, hid;
-
-	CRYPTO_DRIVER_ASSERT();
-
-	/*
-	 * Look first for hardware crypto devices if permitted.
-	 */
-	if (flags & CRYPTOCAP_F_HARDWARE)
-		match = CRYPTOCAP_F_HARDWARE;
-	else
-		match = CRYPTOCAP_F_SOFTWARE;
-	best = NULL;
-again:
-	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = &crypto_drivers[hid];
-		/*
-		 * If it's not initialized, is in the process of
-		 * going away, or is not appropriate (hardware
-		 * or software based on match), then skip.
-		 */
-		if (cap->cc_dev == NULL ||
-		    (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
-		    (cap->cc_flags & match) == 0)
-			continue;
-
-		/* verify all the algorithms are supported. */
-		if (driver_suitable(cap, cri)) {
-			if (best == NULL ||
-			    cap->cc_sessions < best->cc_sessions)
-				best = cap;
-		}
-	}
-	if (best != NULL)
-		return best;
-	if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
-		/* sort of an Algol 68-style for loop */
-		match = CRYPTOCAP_F_SOFTWARE;
-		goto again;
-	}
-	return best;
-}
-
-/*
- * Create a new session.  The crid argument specifies a crypto
- * driver to use or constraints on a driver to select (hardware
- * only, software only, either).  Whatever driver is selected
- * must be capable of the requested crypto algorithms.
- */
-int
-crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
-{
-	struct cryptocap *cap;
-	u_int32_t hid, lid;
-	int err;
-
-	CRYPTO_DRIVER_LOCK();
-	if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
-		/*
-		 * Use specified driver; verify it is capable.
-		 */
-		cap = crypto_checkdriver(crid);
-		if (cap != NULL && !driver_suitable(cap, cri))
-			cap = NULL;
-	} else {
-		/*
-		 * No requested driver; select based on crid flags.
-		 */
-		cap = crypto_select_driver(cri, crid);
-		/*
-		 * if NULL then can't do everything in one session.
-		 * XXX Fix this. We need to inject a "virtual" session
-		 * XXX layer right about here.
-		 */
-	}
-	if (cap != NULL) {
-		/* Call the driver initialization routine. */
-		hid = cap - crypto_drivers;
-		lid = hid;		/* Pass the driver ID. */
-		err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
-		if (err == 0) {
-			(*sid) = (cap->cc_flags & 0xff000000)
-			       | (hid & 0x00ffffff);
-			(*sid) <<= 32;
-			(*sid) |= (lid & 0xffffffff);
-			cap->cc_sessions++;
-		}
-	} else
-		err = EINVAL;
-	CRYPTO_DRIVER_UNLOCK();
-	return err;
-}
-
-static void
-crypto_remove(struct cryptocap *cap)
-{
-
-	KKASSERT(lockstatus(&crypto_drivers_lock, curthread) != 0);
-	if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
-		bzero(cap, sizeof(*cap));
-}
-
-/*
- * Delete an existing session (or a reserved session on an unregistered
- * driver).
- */
-int
-crypto_freesession(u_int64_t sid)
-{
-	struct cryptocap *cap;
-	u_int32_t hid;
-	int err;
-
-	CRYPTO_DRIVER_LOCK();
-
-	if (crypto_drivers == NULL) {
-		err = EINVAL;
-		goto done;
-	}
-
-	/* Determine two IDs. */
-	hid = CRYPTO_SESID2HID(sid);
-
-	if (hid >= crypto_drivers_num) {
-		err = ENOENT;
-		goto done;
-	}
-	cap = &crypto_drivers[hid];
-
-	if (cap->cc_sessions)
-		cap->cc_sessions--;
-
-	/* Call the driver cleanup routine, if available. */
-	err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
-
-	if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
-		crypto_remove(cap);
-
-done:
-	CRYPTO_DRIVER_UNLOCK();
-	return err;
-}
-
-/*
- * Return an unused driver id.  Used by drivers prior to registering
- * support for the algorithms they handle.
- */
-int32_t
-crypto_get_driverid(device_t dev, int flags)
-{
-	struct cryptocap *newdrv;
-	int i;
-
-	if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
-		kprintf("%s: no flags specified when registering driver\n",
-		    device_get_nameunit(dev));
-		return -1;
-	}
-
-	CRYPTO_DRIVER_LOCK();
-
-	for (i = 0; i < crypto_drivers_num; i++) {
-		if (crypto_drivers[i].cc_dev == NULL &&
-		    (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
-			break;
-		}
-	}
-
-	/* Out of entries, allocate some more. */
-	if (i == crypto_drivers_num) {
-		/* Be careful about wrap-around. */
-		if (2 * crypto_drivers_num <= crypto_drivers_num) {
-			CRYPTO_DRIVER_UNLOCK();
-			kprintf("crypto: driver count wraparound!\n");
-			return -1;
-		}
-
-		newdrv = kmalloc(2 * crypto_drivers_num *
-				 sizeof(struct cryptocap),
-				 M_CRYPTO_DATA, M_WAITOK|M_ZERO);
-
-		bcopy(crypto_drivers, newdrv,
-		    crypto_drivers_num * sizeof(struct cryptocap));
-
-		crypto_drivers_num *= 2;
-
-		kfree(crypto_drivers, M_CRYPTO_DATA);
-		crypto_drivers = newdrv;
-	}
-
-	/* NB: state is zero'd on free */
-	crypto_drivers[i].cc_sessions = 1;	/* Mark */
-	crypto_drivers[i].cc_dev = dev;
-	crypto_drivers[i].cc_flags = flags;
-	if (bootverbose)
-		kprintf("crypto: assign %s driver id %u, flags %u\n",
-		    device_get_nameunit(dev), i, flags);
-
-	CRYPTO_DRIVER_UNLOCK();
-
-	return i;
-}
-
-/*
- * Lookup a driver by name.  We match against the full device
- * name and unit, and against just the name.  The latter gives
- * us a simple widlcarding by device name.  On success return the
- * driver/hardware identifier; otherwise return -1.
- */
-int
-crypto_find_driver(const char *match)
-{
-	int i, len = strlen(match);
-
-	CRYPTO_DRIVER_LOCK();
-	for (i = 0; i < crypto_drivers_num; i++) {
-		device_t dev = crypto_drivers[i].cc_dev;
-		if (dev == NULL ||
-		    (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
-			continue;
-		if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
-		    strncmp(match, device_get_name(dev), len) == 0)
-			break;
-	}
-	CRYPTO_DRIVER_UNLOCK();
-	return i < crypto_drivers_num ? i : -1;
-}
-
-/*
- * Return the device_t for the specified driver or NULL
- * if the driver identifier is invalid.
- */
-device_t
-crypto_find_device_byhid(int hid)
-{
-	struct cryptocap *cap = crypto_checkdriver(hid);
-	return cap != NULL ? cap->cc_dev : NULL;
-}
-
-/*
- * Return the device/driver capabilities.
- */
-int
-crypto_getcaps(int hid)
-{
-	struct cryptocap *cap = crypto_checkdriver(hid);
-	return cap != NULL ? cap->cc_flags : 0;
-}
-
-/*
- * Register support for a key-related algorithm.  This routine
- * is called once for each algorithm supported a driver.
- */
-int
-crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
-{
-	struct cryptocap *cap;
-	int err;
-
-	CRYPTO_DRIVER_LOCK();
-
-	cap = crypto_checkdriver(driverid);
-	if (cap != NULL &&
-	    (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
-		/*
-		 * XXX Do some performance testing to determine placing.
-		 * XXX We probably need an auxiliary data structure that
-		 * XXX describes relative performances.
-		 */
-
-		cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
-		if (bootverbose)
-			kprintf("crypto: %s registers key alg %u flags %u\n"
-				, device_get_nameunit(cap->cc_dev)
-				, kalg
-				, flags
-			);
-
-		err = 0;
-	} else
-		err = EINVAL;
-
-	CRYPTO_DRIVER_UNLOCK();
-	return err;
-}
-
-/*
- * Register support for a non-key-related algorithm.  This routine
- * is called once for each such algorithm supported by a driver.
- */
-int
-crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
-		u_int32_t flags)
-{
-	struct cryptocap *cap;
-	int err;
-
-	CRYPTO_DRIVER_LOCK();
-
-	cap = crypto_checkdriver(driverid);
-	/* NB: algorithms are in the range [1..max] */
-	if (cap != NULL &&
-	    (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
-		/*
-		 * XXX Do some performance testing to determine placing.
-		 * XXX We probably need an auxiliary data structure that
-		 * XXX describes relative performances.
-		 */
-
-		cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
-		cap->cc_max_op_len[alg] = maxoplen;
-		if (bootverbose)
-			kprintf("crypto: %s registers alg %u flags %u maxoplen %u\n"
-				, device_get_nameunit(cap->cc_dev)
-				, alg
-				, flags
-				, maxoplen
-			);
-		cap->cc_sessions = 0;		/* Unmark */
-		err = 0;
-	} else
-		err = EINVAL;
-
-	CRYPTO_DRIVER_UNLOCK();
-	return err;
-}
-
-static void
-driver_finis(struct cryptocap *cap)
-{
-	u_int32_t ses, kops;
-
-	CRYPTO_DRIVER_ASSERT();
-
-	ses = cap->cc_sessions;
-	kops = cap->cc_koperations;
-	bzero(cap, sizeof(*cap));
-	if (ses != 0 || kops != 0) {
-		/*
-		 * If there are pending sessions,
-		 * just mark as invalid.
-		 */
-		cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
-		cap->cc_sessions = ses;
-		cap->cc_koperations = kops;
-	}
-}
-
-/*
- * Unregister a crypto driver. If there are pending sessions using it,
- * leave enough information around so that subsequent calls using those
- * sessions will correctly detect the driver has been unregistered and
- * reroute requests.
- */
-int
-crypto_unregister(u_int32_t driverid, int alg)
-{
-	struct cryptocap *cap;
-	int i, err;
-
-	CRYPTO_DRIVER_LOCK();
-	cap = crypto_checkdriver(driverid);
-	if (cap != NULL &&
-	    (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
-	    cap->cc_alg[alg] != 0) {
-		cap->cc_alg[alg] = 0;
-		cap->cc_max_op_len[alg] = 0;
-
-		/* Was this the last algorithm ? */
-		for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++) {
-			if (cap->cc_alg[i] != 0)
-				break;
-		}
-
-		if (i == CRYPTO_ALGORITHM_MAX + 1)
-			driver_finis(cap);
-		err = 0;
-	} else {
-		err = EINVAL;
-	}
-	CRYPTO_DRIVER_UNLOCK();
-
-	return err;
-}
-
-/*
- * Unregister all algorithms associated with a crypto driver.
- * If there are pending sessions using it, leave enough information
- * around so that subsequent calls using those sessions will
- * correctly detect the driver has been unregistered and reroute
- * requests.
- */
-int
-crypto_unregister_all(u_int32_t driverid)
-{
-	struct cryptocap *cap;
-	int err;
-
-	CRYPTO_DRIVER_LOCK();
-	cap = crypto_checkdriver(driverid);
-	if (cap != NULL) {
-		driver_finis(cap);
-		err = 0;
-	} else {
-		err = EINVAL;
-	}
-	CRYPTO_DRIVER_UNLOCK();
-
-	return err;
-}
-
-/*
- * Clear blockage on a driver.  The what parameter indicates whether
- * the driver is now ready for cryptop's and/or cryptokop's.
- */
-int
-crypto_unblock(u_int32_t driverid, int what)
-{
-	crypto_tdinfo_t tdinfo;
-	struct cryptocap *cap;
-	int err;
-	int n;
-
-	CRYPTO_DRIVER_LOCK();
-	cap = crypto_checkdriver(driverid);
-	if (cap != NULL) {
-		if (what & CRYPTO_SYMQ)
-			cap->cc_qblocked = 0;
-		if (what & CRYPTO_ASYMQ)
-			cap->cc_kqblocked = 0;
-		for (n = 0; n < ncpus; ++n) {
-			tdinfo = &tdinfo_array[n];
-			CRYPTO_Q_LOCK(tdinfo);
-			if (tdinfo->crp_sleep)
-				wakeup_one(&tdinfo->crp_q);
-			CRYPTO_Q_UNLOCK(tdinfo);
-		}
-		err = 0;
-	} else {
-		err = EINVAL;
-	}
-	CRYPTO_DRIVER_UNLOCK();
-
-	return err;
-}
-
-static volatile int dispatch_rover;
-
-/*
- * Add a crypto request to a queue, to be processed by the kernel thread.
- */
-int
-crypto_dispatch(struct cryptop *crp)
-{
-	crypto_tdinfo_t tdinfo;
-	struct cryptocap *cap;
-	u_int32_t hid;
-	int result;
-	int n;
-
-	cryptostats.cs_ops++;
-
-#ifdef CRYPTO_TIMING
-	if (crypto_timing)
-		nanouptime(&crp->crp_tstamp);
-#endif
-
-	hid = CRYPTO_SESID2HID(crp->crp_sid);
-
-	/*
-	 * Dispatch the crypto op directly to the driver if the caller
-	 * marked the request to be processed immediately or this is
-	 * a synchronous callback chain occuring from within a crypto
-	 * processing thread.
-	 *
-	 * Fall through to queueing the driver is blocked.
-	 */
-	if ((crp->crp_flags & CRYPTO_F_BATCH) == 0 ||
-	    curthread->td_type == TD_TYPE_CRYPTO) {
-		cap = crypto_checkdriver(hid);
-		/* Driver cannot disappeared when there is an active session. */
-		KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
-		if (!cap->cc_qblocked) {
-			result = crypto_invoke(cap, crp, 0);
-			if (result != ERESTART)
-				return (result);
-			/*
-			 * The driver ran out of resources, put the request on
-			 * the queue.
-			 */
-		}
-	}
-
-	/*
-	 * Dispatch to a cpu for action if possible.  Dispatch to a different
-	 * cpu than the current cpu.
-	 */
-	if (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SMP) {
-		n = atomic_fetchadd_int(&dispatch_rover, 1) & 255;
-		if (crypto_altdispatch && mycpu->gd_cpuid == n)
-			++n;
-		n = n % ncpus;
-	} else {
-		n = 0;
-	}
-	tdinfo = &tdinfo_array[n];
-
-	CRYPTO_Q_LOCK(tdinfo);
-	TAILQ_INSERT_TAIL(&tdinfo->crp_q, crp, crp_next);
-	if (tdinfo->crp_sleep)
-		wakeup_one(&tdinfo->crp_q);
-	CRYPTO_Q_UNLOCK(tdinfo);
-	return 0;
-}
-
-/*
- * Add an asymetric crypto request to a queue,
- * to be processed by the kernel thread.
- */
-int
-crypto_kdispatch(struct cryptkop *krp)
-{
-	crypto_tdinfo_t tdinfo;
-	int error;
-	int n;
-
-	cryptostats.cs_kops++;
-
-#if 0
-	/* not sure how to test F_SMP here */
-	n = atomic_fetchadd_int(&dispatch_rover, 1) & 255;
-	n = n % ncpus;
-#endif
-	n = 0;
-	tdinfo = &tdinfo_array[n];
-
-	error = crypto_kinvoke(krp, krp->krp_crid);
-
-	if (error == ERESTART) {
-		CRYPTO_Q_LOCK(tdinfo);
-		TAILQ_INSERT_TAIL(&tdinfo->crp_kq, krp, krp_next);
-		if (tdinfo->crp_sleep)
-			wakeup_one(&tdinfo->crp_q);
-		CRYPTO_Q_UNLOCK(tdinfo);
-		error = 0;
-	}
-	return error;
-}
-
-/*
- * Verify a driver is suitable for the specified operation.
- */
-static __inline int
-kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
-{
-	return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
-}
-
-/*
- * Select a driver for an asym operation.  The driver must
- * support the necessary algorithm.  The caller can constrain
- * which device is selected with the flags parameter.  The
- * algorithm we use here is pretty stupid; just use the first
- * driver that supports the algorithms we need. If there are
- * multiple suitable drivers we choose the driver with the
- * fewest active operations.  We prefer hardware-backed
- * drivers to software ones when either may be used.
- */
-static struct cryptocap *
-crypto_select_kdriver(const struct cryptkop *krp, int flags)
-{
-	struct cryptocap *cap, *best;
-	int match, hid;
-
-	CRYPTO_DRIVER_ASSERT();
-
-	/*
-	 * Look first for hardware crypto devices if permitted.
-	 */
-	if (flags & CRYPTOCAP_F_HARDWARE)
-		match = CRYPTOCAP_F_HARDWARE;
-	else
-		match = CRYPTOCAP_F_SOFTWARE;
-	best = NULL;
-again:
-	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		cap = &crypto_drivers[hid];
-		/*
-		 * If it's not initialized, is in the process of
-		 * going away, or is not appropriate (hardware
-		 * or software based on match), then skip.
-		 */
-		if (cap->cc_dev == NULL ||
-		    (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
-		    (cap->cc_flags & match) == 0)
-			continue;
-
-		/* verify all the algorithms are supported. */
-		if (kdriver_suitable(cap, krp)) {
-			if (best == NULL ||
-			    cap->cc_koperations < best->cc_koperations)
-				best = cap;
-		}
-	}
-	if (best != NULL)
-		return best;
-	if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
-		/* sort of an Algol 68-style for loop */
-		match = CRYPTOCAP_F_SOFTWARE;
-		goto again;
-	}
-	return best;
-}
-
-/*
- * Dispatch an assymetric crypto request.
- */
-static int
-crypto_kinvoke(struct cryptkop *krp, int crid)
-{
-	struct cryptocap *cap = NULL;
-	int error;
-
-	KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
-	KASSERT(krp->krp_callback != NULL,
-	    ("%s: krp->crp_callback == NULL", __func__));
-
-	CRYPTO_DRIVER_LOCK();
-	if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
-		cap = crypto_checkdriver(crid);
-		if (cap != NULL) {
-			/*
-			 * Driver present, it must support the necessary
-			 * algorithm and, if s/w drivers are excluded,
-			 * it must be registered as hardware-backed.
-			 */
-			if (!kdriver_suitable(cap, krp) ||
-			    (!crypto_devallowsoft &&
-			     (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
-				cap = NULL;
-		}
-	} else {
-		/*
-		 * No requested driver; select based on crid flags.
-		 */
-		if (!crypto_devallowsoft)	/* NB: disallow s/w drivers */
-			crid &= ~CRYPTOCAP_F_SOFTWARE;
-		cap = crypto_select_kdriver(krp, crid);
-	}
-	if (cap != NULL && !cap->cc_kqblocked) {
-		krp->krp_hid = cap - crypto_drivers;
-		cap->cc_koperations++;
-		CRYPTO_DRIVER_UNLOCK();
-		error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
-		CRYPTO_DRIVER_LOCK();
-		if (error == ERESTART) {
-			cap->cc_koperations--;
-			CRYPTO_DRIVER_UNLOCK();
-			return (error);
-		}
-	} else {
-		/*
-		 * NB: cap is !NULL if device is blocked; in
-		 *     that case return ERESTART so the operation
-		 *     is resubmitted if possible.
-		 */
-		error = (cap == NULL) ? ENODEV : ERESTART;
-	}
-	CRYPTO_DRIVER_UNLOCK();
-
-	if (error) {
-		krp->krp_status = error;
-		crypto_kdone(krp);
-	}
-	return 0;
-}
-
-#ifdef CRYPTO_TIMING
-static void
-crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
-{
-	struct timespec now, t;
-
-	nanouptime(&now);
-	t.tv_sec = now.tv_sec - tv->tv_sec;
-	t.tv_nsec = now.tv_nsec - tv->tv_nsec;
-	if (t.tv_nsec < 0) {
-		t.tv_sec--;
-		t.tv_nsec += 1000000000;
-	}
-	timespecadd(&ts->acc, &t, &ts->acc);
-	if (timespeccmp(&t, &ts->min, <))
-		ts->min = t;
-	if (timespeccmp(&t, &ts->max, >))
-		ts->max = t;
-	ts->count++;
-
-	*tv = now;
-}
-#endif
-
-/*
- * Dispatch a crypto request to the appropriate crypto devices.
- */
-static int
-crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
-{
-
-	KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
-	KASSERT(crp->crp_callback != NULL,
-	    ("%s: crp->crp_callback == NULL", __func__));
-	KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
-
-#ifdef CRYPTO_TIMING
-	if (crypto_timing)
-		crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
-#endif
-	if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
-		struct cryptodesc *crd;
-		u_int64_t nid;
-
-		/*
-		 * Driver has unregistered; migrate the session and return
-		 * an error to the caller so they'll resubmit the op.
-		 *
-		 * XXX: What if there are more already queued requests for this
-		 *      session?
-		 */
-		crypto_freesession(crp->crp_sid);
-
-		for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
-			crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
-
-		/* XXX propagate flags from initial session? */
-		if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
-		    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
-			crp->crp_sid = nid;
-
-		crp->crp_etype = EAGAIN;
-		crypto_done(crp);
-		return 0;
-	} else {
-		/*
-		 * Invoke the driver to process the request.
-		 */
-		return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
-	}
-}
-
-/*
- * Release a set of crypto descriptors.
- */
-void
-crypto_freereq(struct cryptop *crp)
-{
-	struct cryptodesc *crd;
-#ifdef DIAGNOSTIC
-	crypto_tdinfo_t tdinfo;
-	struct cryptop *crp2;
-	int n;
-#endif
-
-	if (crp == NULL)
-		return;
-
-#ifdef DIAGNOSTIC
-	for (n = 0; n < ncpus; ++n) {
-		tdinfo = &tdinfo_array[n];
-
-		CRYPTO_Q_LOCK(tdinfo);
-		TAILQ_FOREACH(crp2, &tdinfo->crp_q, crp_next) {
-			KASSERT(crp2 != crp,
-			    ("Freeing cryptop from the crypto queue (%p).",
-			    crp));
-		}
-		CRYPTO_Q_UNLOCK(tdinfo);
-	}
-	CRYPTO_RETQ_LOCK();
-	TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
-		KASSERT(crp2 != crp,
-		    ("Freeing cryptop from the return queue (%p).",
-		    crp));
-	}
-	CRYPTO_RETQ_UNLOCK();
-#endif
-
-	while ((crd = crp->crp_desc) != NULL) {
-		crp->crp_desc = crd->crd_next;
-		objcache_put(cryptodesc_oc, crd);
-	}
-	objcache_put(cryptop_oc, crp);
-}
-
-/*
- * Acquire a set of crypto descriptors.
- */
-struct cryptop *
-crypto_getreq(int num)
-{
-	struct cryptodesc *crd;
-	struct cryptop *crp;
-
-	crp = objcache_get(cryptop_oc, M_WAITOK);
-	if (crp != NULL) {
-		bzero(crp, sizeof (*crp));
-		while (num--) {
-			crd = objcache_get(cryptodesc_oc, M_WAITOK);
-			if (crd == NULL) {
-				crypto_freereq(crp);
-				return NULL;
-			}
-			bzero(crd, sizeof (*crd));
-
-			crd->crd_next = crp->crp_desc;
-			crp->crp_desc = crd;
-		}
-	}
-	return crp;
-}
-
-/*
- * Invoke the callback on behalf of the driver.
- */
-void
-crypto_done(struct cryptop *crp)
-{
-	KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
-		("crypto_done: op already done, flags 0x%x", crp->crp_flags));
-	crp->crp_flags |= CRYPTO_F_DONE;
-	if (crp->crp_etype != 0)
-		cryptostats.cs_errs++;
-#ifdef CRYPTO_TIMING
-	if (crypto_timing)
-		crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
-#endif
-	/*
-	 * CBIMM means unconditionally do the callback immediately;
-	 * CBIFSYNC means do the callback immediately only if the
-	 * operation was done synchronously.  Both are used to avoid
-	 * doing extraneous context switches; the latter is mostly
-	 * used with the software crypto driver.
-	 */
-	if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
-	    ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
-	     (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
-		/*
-		 * Do the callback directly.  This is ok when the
-		 * callback routine does very little (e.g. the
-		 * /dev/crypto callback method just does a wakeup).
-		 */
-#ifdef CRYPTO_TIMING
-		if (crypto_timing) {
-			/*
-			 * NB: We must copy the timestamp before
-			 * doing the callback as the cryptop is
-			 * likely to be reclaimed.
-			 */
-			struct timespec t = crp->crp_tstamp;
-			crypto_tstat(&cryptostats.cs_cb, &t);
-			crp->crp_callback(crp);
-			crypto_tstat(&cryptostats.cs_finis, &t);
-		} else
-#endif
-			crp->crp_callback(crp);
-	} else {
-		/*
-		 * Normal case; queue the callback for the thread.
-		 */
-		CRYPTO_RETQ_LOCK();
-		if (CRYPTO_RETQ_EMPTY())
-			wakeup_one(&crp_ret_q);	/* shared wait channel */
-		TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
-		CRYPTO_RETQ_UNLOCK();
-	}
-}
-
-/*
- * Invoke the callback on behalf of the driver.
- */
-void
-crypto_kdone(struct cryptkop *krp)
-{
-	struct cryptocap *cap;
-
-	if (krp->krp_status != 0)
-		cryptostats.cs_kerrs++;
-	CRYPTO_DRIVER_LOCK();
-	/* XXX: What if driver is loaded in the meantime? */
-	if (krp->krp_hid < crypto_drivers_num) {
-		cap = &crypto_drivers[krp->krp_hid];
-		cap->cc_koperations--;
-		KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
-		if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
-			crypto_remove(cap);
-	}
-	CRYPTO_DRIVER_UNLOCK();
-	CRYPTO_RETQ_LOCK();
-	if (CRYPTO_RETQ_EMPTY())
-		wakeup_one(&crp_ret_q);		/* shared wait channel */
-	TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
-	CRYPTO_RETQ_UNLOCK();
-}
-
-int
-crypto_getfeat(int *featp)
-{
-	int hid, kalg, feat = 0;
-
-	CRYPTO_DRIVER_LOCK();
-	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		const struct cryptocap *cap = &crypto_drivers[hid];
-
-		if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
-		    !crypto_devallowsoft) {
-			continue;
-		}
-		for (kalg = 0; kalg <= CRK_ALGORITHM_MAX; kalg++)
-			if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
-				feat |=  1 << kalg;
-	}
-	CRYPTO_DRIVER_UNLOCK();
-	*featp = feat;
-	return (0);
-}
-
-/*
- * Terminate a thread at module unload.  The process that
- * initiated this is waiting for us to signal that we're gone;
- * wake it up and exit.  We use the driver table lock to insure
- * we don't do the wakeup before they're waiting.  There is no
- * race here because the waiter sleeps on the proc lock for the
- * thread so it gets notified at the right time because of an
- * extra wakeup that's done in exit1().
- */
-static void
-crypto_finis(void *chan)
-{
-	CRYPTO_DRIVER_LOCK();
-	wakeup_one(chan);
-	CRYPTO_DRIVER_UNLOCK();
-	kthread_exit();
-}
-
-/*
- * Crypto thread, dispatches crypto requests.
- *
- * MPSAFE
- */
-static void
-crypto_proc(void *arg)
-{
-	crypto_tdinfo_t tdinfo = arg;
-	struct cryptop *crp, *submit;
-	struct cryptkop *krp;
-	struct cryptocap *cap;
-	u_int32_t hid;
-	int result, hint;
-
-	CRYPTO_Q_LOCK(tdinfo);
-
-	curthread->td_type = TD_TYPE_CRYPTO;
-
-	for (;;) {
-		/*
-		 * Find the first element in the queue that can be
-		 * processed and look-ahead to see if multiple ops
-		 * are ready for the same driver.
-		 */
-		submit = NULL;
-		hint = 0;
-		TAILQ_FOREACH(crp, &tdinfo->crp_q, crp_next) {
-			hid = CRYPTO_SESID2HID(crp->crp_sid);
-			cap = crypto_checkdriver(hid);
-			/*
-			 * Driver cannot disappeared when there is an active
-			 * session.
-			 */
-			KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
-			    __func__, __LINE__));
-			if (cap == NULL || cap->cc_dev == NULL) {
-				/* Op needs to be migrated, process it. */
-				if (submit == NULL)
-					submit = crp;
-				break;
-			}
-			if (!cap->cc_qblocked) {
-				if (submit != NULL) {
-					/*
-					 * We stop on finding another op,
-					 * regardless whether its for the same
-					 * driver or not.  We could keep
-					 * searching the queue but it might be
-					 * better to just use a per-driver
-					 * queue instead.
-					 */
-					if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
-						hint = CRYPTO_HINT_MORE;
-					break;
-				} else {
-					submit = crp;
-					if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
-						break;
-					/* keep scanning for more are q'd */
-				}
-			}
-		}
-		if (submit != NULL) {
-			TAILQ_REMOVE(&tdinfo->crp_q, submit, crp_next);
-			hid = CRYPTO_SESID2HID(submit->crp_sid);
-			cap = crypto_checkdriver(hid);
-			KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
-			    __func__, __LINE__));
-
-			CRYPTO_Q_UNLOCK(tdinfo);
-			result = crypto_invoke(cap, submit, hint);
-			CRYPTO_Q_LOCK(tdinfo);
-
-			if (result == ERESTART) {
-				/*
-				 * The driver ran out of resources, mark the
-				 * driver ``blocked'' for cryptop's and put
-				 * the request back in the queue.  It would
-				 * best to put the request back where we got
-				 * it but that's hard so for now we put it
-				 * at the front.  This should be ok; putting
-				 * it at the end does not work.
-				 */
-				/* XXX validate sid again? */
-				crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
-				TAILQ_INSERT_HEAD(&tdinfo->crp_q,
-						  submit, crp_next);
-				cryptostats.cs_blocks++;
-			}
-		}
-
-		/* As above, but for key ops */
-		TAILQ_FOREACH(krp, &tdinfo->crp_kq, krp_next) {
-			cap = crypto_checkdriver(krp->krp_hid);
-			if (cap == NULL || cap->cc_dev == NULL) {
-				/*
-				 * Operation needs to be migrated, invalidate
-				 * the assigned device so it will reselect a
-				 * new one below.  Propagate the original
-				 * crid selection flags if supplied.
-				 */
-				krp->krp_hid = krp->krp_crid &
-				    (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
-				if (krp->krp_hid == 0)
-					krp->krp_hid =
-				    CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
-				break;
-			}
-			if (!cap->cc_kqblocked)
-				break;
-		}
-		if (krp != NULL) {
-			TAILQ_REMOVE(&tdinfo->crp_kq, krp, krp_next);
-
-			CRYPTO_Q_UNLOCK(tdinfo);
-			result = crypto_kinvoke(krp, krp->krp_hid);
-			CRYPTO_Q_LOCK(tdinfo);
-
-			if (result == ERESTART) {
-				/*
-				 * The driver ran out of resources, mark the
-				 * driver ``blocked'' for cryptkop's and put
-				 * the request back in the queue.  It would
-				 * best to put the request back where we got
-				 * it but that's hard so for now we put it
-				 * at the front.  This should be ok; putting
-				 * it at the end does not work.
-				 */
-				/* XXX validate sid again? */
-				crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
-				TAILQ_INSERT_HEAD(&tdinfo->crp_kq,
-						  krp, krp_next);
-				cryptostats.cs_kblocks++;
-			}
-		}
-
-		if (submit == NULL && krp == NULL) {
-			/*
-			 * Nothing more to be processed.  Sleep until we're
-			 * woken because there are more ops to process.
-			 * This happens either by submission or by a driver
-			 * becoming unblocked and notifying us through
-			 * crypto_unblock.  Note that when we wakeup we
-			 * start processing each queue again from the
-			 * front. It's not clear that it's important to
-			 * preserve this ordering since ops may finish
-			 * out of order if dispatched to different devices
-			 * and some become blocked while others do not.
-			 */
-			tdinfo->crp_sleep = 1;
-			lksleep (&tdinfo->crp_q, &tdinfo->crp_lock,
-				 0, "crypto_wait", 0);
-			tdinfo->crp_sleep = 0;
-			if (tdinfo->crp_td == NULL)
-				break;
-			cryptostats.cs_intrs++;
-		}
-	}
-	CRYPTO_Q_UNLOCK(tdinfo);
-
-	crypto_finis(&tdinfo->crp_q);
-}
-
-/*
- * Crypto returns thread, does callbacks for processed crypto requests.
- * Callbacks are done here, rather than in the crypto drivers, because
- * callbacks typically are expensive and would slow interrupt handling.
- *
- * MPSAFE
- */
-static void
-crypto_ret_proc(void *dummy __unused)
-{
-	struct cryptop *crpt;
-	struct cryptkop *krpt;
-
-	CRYPTO_RETQ_LOCK();
-	for (;;) {
-		/* Harvest return q's for completed ops */
-		crpt = TAILQ_FIRST(&crp_ret_q);
-		if (crpt != NULL)
-			TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
-
-		krpt = TAILQ_FIRST(&crp_ret_kq);
-		if (krpt != NULL)
-			TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
-
-		if (crpt != NULL || krpt != NULL) {
-			CRYPTO_RETQ_UNLOCK();
-			/*
-			 * Run callbacks unlocked.
-			 */
-			if (crpt != NULL) {
-#ifdef CRYPTO_TIMING
-				if (crypto_timing) {
-					/*
-					 * NB: We must copy the timestamp before
-					 * doing the callback as the cryptop is
-					 * likely to be reclaimed.
-					 */
-					struct timespec t = crpt->crp_tstamp;
-					crypto_tstat(&cryptostats.cs_cb, &t);
-					crpt->crp_callback(crpt);
-					crypto_tstat(&cryptostats.cs_finis, &t);
-				} else
-#endif
-					crpt->crp_callback(crpt);
-			}
-			if (krpt != NULL)
-				krpt->krp_callback(krpt);
-			CRYPTO_RETQ_LOCK();
-		} else {
-			/*
-			 * Nothing more to be processed.  Sleep until we're
-			 * woken because there are more returns to process.
-			 */
-			lksleep(&crp_ret_q, &crypto_ret_q_lock,
-				0, "crypto_ret_wait", 0);
-			if (cryptoretthread == NULL)
-				break;
-			cryptostats.cs_rets++;
-		}
-	}
-	CRYPTO_RETQ_UNLOCK();
-
-	crypto_finis(&crp_ret_q);
-}
-
-#ifdef DDB
-static void
-db_show_drivers(void)
-{
-	int hid;
-
-	db_printf("%12s %4s %4s %8s %2s %2s\n"
-		, "Device"
-		, "Ses"
-		, "Kops"
-		, "Flags"
-		, "QB"
-		, "KB"
-	);
-	for (hid = 0; hid < crypto_drivers_num; hid++) {
-		const struct cryptocap *cap = &crypto_drivers[hid];
-		if (cap->cc_dev == NULL)
-			continue;
-		db_printf("%-12s %4u %4u %08x %2u %2u\n"
-		    , device_get_nameunit(cap->cc_dev)
-		    , cap->cc_sessions
-		    , cap->cc_koperations
-		    , cap->cc_flags
-		    , cap->cc_qblocked
-		    , cap->cc_kqblocked
-		);
-	}
-}
-
-DB_SHOW_COMMAND(crypto, db_show_crypto)
-{
-	crypto_tdinfo_t tdinfo;
-	struct cryptop *crp;
-	int n;
-
-	db_show_drivers();
-	db_printf("\n");
-
-	db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
-	    "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
-	    "Desc", "Callback");
-
-	for (n = 0; n < ncpus; ++n) {
-		tdinfo = &tdinfo_array[n];
-
-		TAILQ_FOREACH(crp, &tdinfo->crp_q, crp_next) {
-			db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
-			    , (int) CRYPTO_SESID2HID(crp->crp_sid)
-			    , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
-			    , crp->crp_ilen, crp->crp_olen
-			    , crp->crp_etype
-			    , crp->crp_flags
-			    , crp->crp_desc
-			    , crp->crp_callback
-			);
-		}
-	}
-	if (!TAILQ_EMPTY(&crp_ret_q)) {
-		db_printf("\n%4s %4s %4s %8s\n",
-		    "HID", "Etype", "Flags", "Callback");
-		TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
-			db_printf("%4u %4u %04x %8p\n"
-			    , (int) CRYPTO_SESID2HID(crp->crp_sid)
-			    , crp->crp_etype
-			    , crp->crp_flags
-			    , crp->crp_callback
-			);
-		}
-	}
-}
-
-DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
-{
-	crypto_tdinfo_t tdinfo;
-	struct cryptkop *krp;
-	int n;
-
-	db_show_drivers();
-	db_printf("\n");
-
-	db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
-	    "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
-
-	for (n = 0; n < ncpus; ++n) {
-		tdinfo = &tdinfo_array[n];
-
-		TAILQ_FOREACH(krp, &tdinfo->crp_kq, krp_next) {
-			db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
-			    , krp->krp_op
-			    , krp->krp_status
-			    , krp->krp_iparams, krp->krp_oparams
-			    , krp->krp_crid, krp->krp_hid
-			    , krp->krp_callback
-			);
-		}
-	}
-	if (!TAILQ_EMPTY(&crp_ret_q)) {
-		db_printf("%4s %5s %8s %4s %8s\n",
-		    "Op", "Status", "CRID", "HID", "Callback");
-		TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
-			db_printf("%4u %5u %08x %4u %8p\n"
-			    , krp->krp_op
-			    , krp->krp_status
-			    , krp->krp_crid, krp->krp_hid
-			    , krp->krp_callback
-			);
-		}
-	}
-}
-#endif
-
-int crypto_modevent(module_t mod, int type, void *unused);
-
-/*
- * Initialization code, both for static and dynamic loading.
- * Note this is not invoked with the usual DECLARE_MODULE
- * mechanism but instead is listed as a dependency by the
- * cryptosoft driver.  This guarantees proper ordering of
- * calls on module load/unload.
- */
-int
-crypto_modevent(module_t mod, int type, void *unused)
-{
-	int error = EINVAL;
-
-	switch (type) {
-	case MOD_LOAD:
-		error = crypto_init();
-		if (error == 0 && bootverbose)
-			kprintf("crypto: <crypto core>\n");
-		break;
-	case MOD_UNLOAD:
-		/*XXX disallow if active sessions */
-		error = 0;
-		crypto_destroy();
-		return 0;
-	}
-	return error;
-}
-MODULE_VERSION(crypto, 1);
-MODULE_DEPEND(crypto, zlib, 1, 1, 1);
diff --git a/sys/opencrypto/cryptodev.c b/sys/opencrypto/cryptodev.c
deleted file mode 100644
index d4965a5389..0000000000
--- a/sys/opencrypto/cryptodev.c
+++ /dev/null
@@ -1,930 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/cryptodev.c,v 1.41 2009/09/04 09:48:18 pjd Exp $	*/
-/*	$OpenBSD: cryptodev.c,v 1.52 2002/06/19 07:22:46 deraadt Exp $	*/
-
-/*-
- * Copyright (c) 2001 Theo de Raadt
- * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/malloc.h>
-#include <sys/device.h>
-#include <sys/mbuf.h>
-#include <sys/lock.h>
-#include <sys/sysctl.h>
-#include <sys/file.h>
-#include <sys/filedesc.h>
-#include <sys/errno.h>
-#include <sys/uio.h>
-#include <sys/random.h>
-#include <sys/conf.h>
-#include <sys/module.h>
-#include <sys/kernel.h>
-#include <sys/fcntl.h>
-#include <sys/proc.h>
-
-#include <sys/file2.h>
-
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/xform.h>
-
-struct csession {
-	TAILQ_ENTRY(csession) next;
-	u_int64_t	sid;
-	u_int32_t	ses;
-	struct lock	lock;
-
-	u_int32_t	cipher;
-	struct enc_xform *txform;
-	u_int32_t	mac;
-	struct auth_hash *thash;
-
-	caddr_t		key;
-	int		keylen;
-	u_char		tmp_iv[EALG_MAX_BLOCK_LEN];
-
-	caddr_t		mackey;
-	int		mackeylen;
-
-	struct iovec	iovec;
-	struct uio	uio;
-	int		error;
-	uint32_t	busy;
-};
-
-struct fcrypt {
-	TAILQ_HEAD(csessionlist, csession) csessions;
-	int		sesn;
-};
-
-static	int cryptof_rw(struct file *fp, struct uio *uio,
-		    struct ucred *cred, int flags);
-static	int cryptof_ioctl(struct file *, u_long, caddr_t,
-		    struct ucred *, struct sysmsg *);
-static	int cryptof_kqfilter(struct file *, struct knote *);
-static	int cryptof_stat(struct file *, struct stat *, struct ucred *);
-static	int cryptof_close(struct file *);
-
-static struct fileops cryptofops = {
-    .fo_read = cryptof_rw,
-    .fo_write = cryptof_rw,
-    .fo_ioctl = cryptof_ioctl,
-    .fo_kqfilter = cryptof_kqfilter,
-    .fo_stat = cryptof_stat,
-    .fo_close = cryptof_close,
-    .fo_shutdown = nofo_shutdown,
-    .fo_seek = badfo_seek
-};
-
-static struct csession *csefind(struct fcrypt *, u_int, int *);
-static void csedrop(struct csession *);
-static int csedelete(struct fcrypt *, struct csession *);
-static struct csession *cseadd(struct fcrypt *, struct csession *);
-static struct csession *csecreate(struct fcrypt *, u_int64_t, caddr_t,
-    u_int64_t, caddr_t, u_int64_t, u_int32_t, u_int32_t, struct enc_xform *,
-    struct auth_hash *);
-static int csefree(struct csession *);
-
-static	int cryptodev_op(struct csession *, struct crypt_op *, struct ucred *);
-static	int cryptodev_key(struct crypt_kop *);
-static	int cryptodev_find(struct crypt_find_op *);
-
-static struct lock cryptodev_lock = LOCK_INITIALIZER("cryptodev", 0, 0);
-
-static int
-cryptof_rw(
-	struct file *fp,
-	struct uio *uio,
-	struct ucred *active_cred,
-	int flags)
-{
-	return (EIO);
-}
-
-/*
- * Check a crypto identifier to see if it requested
- * a software device/driver.  This can be done either
- * by device name/class or through search constraints.
- */
-static int
-checkforsoftware(int crid)
-{
-	if (crid & CRYPTOCAP_F_SOFTWARE)
-		return EINVAL;		/* XXX */
-	if ((crid & CRYPTOCAP_F_HARDWARE) == 0 &&
-	    (crypto_getcaps(crid) & CRYPTOCAP_F_HARDWARE) == 0)
-		return EINVAL;		/* XXX */
-	return 0;
-}
-
-/* ARGSUSED */
-static int
-cryptof_ioctl(struct file *fp, u_long cmd, caddr_t data,
-	      struct ucred *cred, struct sysmsg *msg)
-{
-#define	SES2(p)	((struct session2_op *)p)
-	struct cryptoini cria, crie;
-	struct fcrypt *fcr = fp->f_data;
-	struct csession *cse;
-	struct session_op *sop;
-	struct crypt_op *cop;
-	struct enc_xform *txform = NULL;
-	struct auth_hash *thash = NULL;
-	struct crypt_kop *kop;
-	u_int64_t sid;
-	u_int32_t ses;
-	int error = 0, crid;
-
-	switch (cmd) {
-	case CIOCGSESSION:
-	case CIOCGSESSION2:
-		sop = (struct session_op *)data;
-		switch (sop->cipher) {
-		case 0:
-			break;
-		case CRYPTO_DES_CBC:
-			txform = &enc_xform_des;
-			break;
-		case CRYPTO_3DES_CBC:
-			txform = &enc_xform_3des;
-			break;
-		case CRYPTO_BLF_CBC:
-			txform = &enc_xform_blf;
-			break;
-		case CRYPTO_CAST_CBC:
-			txform = &enc_xform_cast5;
-			break;
-		case CRYPTO_SKIPJACK_CBC:
-			txform = &enc_xform_skipjack;
-			break;
-		case CRYPTO_AES_CBC:
-			txform = &enc_xform_rijndael128;
-			break;
-		case CRYPTO_AES_XTS:
-			txform = &enc_xform_aes_xts;
-			break;
-		case CRYPTO_AES_CTR:
-			txform = &enc_xform_aes_ctr;
-			break;		
-		case CRYPTO_NULL_CBC:
-			txform = &enc_xform_null;
-			break;
-		case CRYPTO_ARC4:
-			txform = &enc_xform_arc4;
-			break;
-		case CRYPTO_CAMELLIA_CBC:
-			txform = &enc_xform_camellia;
-			break;
-		case CRYPTO_TWOFISH_CBC:
-			txform = &enc_xform_twofish;
-			break;
-		case CRYPTO_SERPENT_CBC:
-			txform = &enc_xform_serpent;
-			break;
-		case CRYPTO_TWOFISH_XTS:
-			txform = &enc_xform_twofish_xts;
-			break;
-		case CRYPTO_SERPENT_XTS:
-			txform = &enc_xform_serpent_xts;
-			break;
-		default:
-			return (EINVAL);
-		}
-
-		switch (sop->mac) {
-		case 0:
-			break;
-		case CRYPTO_MD5_HMAC:
-			thash = &auth_hash_hmac_md5;
-			break;
-		case CRYPTO_SHA1_HMAC:
-			thash = &auth_hash_hmac_sha1;
-			break;
-		case CRYPTO_SHA2_256_HMAC:
-			thash = &auth_hash_hmac_sha2_256;
-			break;
-		case CRYPTO_SHA2_384_HMAC:
-			thash = &auth_hash_hmac_sha2_384;
-			break;
-		case CRYPTO_SHA2_512_HMAC:
-			thash = &auth_hash_hmac_sha2_512;
-			break;
-		case CRYPTO_RIPEMD160_HMAC:
-			thash = &auth_hash_hmac_ripemd_160;
-			break;
-#ifdef notdef
-		case CRYPTO_MD5:
-			thash = &auth_hash_md5;
-			break;
-		case CRYPTO_SHA1:
-			thash = &auth_hash_sha1;
-			break;
-#endif
-		case CRYPTO_NULL_HMAC:
-			thash = &auth_hash_null;
-			break;
-		default:
-			return (EINVAL);
-		}
-
-		bzero(&crie, sizeof(crie));
-		bzero(&cria, sizeof(cria));
-
-		if (txform) {
-			crie.cri_alg = txform->type;
-			crie.cri_klen = sop->keylen * 8;
-			if (sop->keylen > txform->maxkey ||
-			    sop->keylen < txform->minkey) {
-				error = EINVAL;
-				goto bail;
-			}
-
-			crie.cri_key = kmalloc(crie.cri_klen / 8,
-			    M_XDATA, M_WAITOK);
-			if ((error = copyin(sop->key, crie.cri_key,
-			    crie.cri_klen / 8)))
-				goto bail;
-			if (thash)
-				crie.cri_next = &cria;
-		}
-
-		if (thash) {
-			cria.cri_alg = thash->type;
-			cria.cri_klen = sop->mackeylen * 8;
-			if (sop->mackeylen != thash->keysize) {
-				error = EINVAL;
-				goto bail;
-			}
-
-			if (cria.cri_klen) {
-				cria.cri_key = kmalloc(cria.cri_klen / 8,
-				    M_XDATA, M_WAITOK);
-				if ((error = copyin(sop->mackey, cria.cri_key,
-				    cria.cri_klen / 8)))
-					goto bail;
-			}
-		}
-
-		/* NB: CIOGSESSION2 has the crid */
-		if (cmd == CIOCGSESSION2) {
-			crid = SES2(sop)->crid;
-			error = checkforsoftware(crid);
-			if (error)
-				goto bail;
-		} else {
-			crid = CRYPTOCAP_F_HARDWARE;
-			if (crypto_devallowsoft)
-				crid |= CRYPTOCAP_F_SOFTWARE;
-		}
-		error = crypto_newsession(&sid, (txform ? &crie : &cria), crid);
-		if (error)
-			goto bail;
-
-		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
-		cse = csecreate(fcr, sid, crie.cri_key, crie.cri_klen,
-				cria.cri_key, cria.cri_klen, sop->cipher,
-				sop->mac, txform, thash);
-		lockmgr(&cryptodev_lock, LK_RELEASE);
-
-		if (cse == NULL) {
-			crypto_freesession(sid);
-			error = EINVAL;
-			goto bail;
-		}
-		sop->ses = cse->ses;
-		if (cmd == CIOCGSESSION2) {
-			/* return hardware/driver id */
-			SES2(sop)->crid = CRYPTO_SESID2HID(cse->sid);
-		}
-bail:
-		if (error) {
-			if (crie.cri_key) {
-				bzero(crie.cri_key, crie.cri_klen / 8);
-				kfree(crie.cri_key, M_XDATA);
-			}
-			if (cria.cri_key) {
-				bzero(cria.cri_key, cria.cri_klen / 8);
-				kfree(cria.cri_key, M_XDATA);
-			}
-		}
-		break;
-	case CIOCFSESSION:
-		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
-		ses = *(u_int32_t *)data;
-		cse = csefind(fcr, ses, &error);
-		if (cse) {
-			csedelete(fcr, cse);
-			error = csefree(cse);
-		}
-		lockmgr(&cryptodev_lock, LK_RELEASE);
-		break;
-	case CIOCCRYPT:
-		cop = (struct crypt_op *)data;
-		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
-		cse = csefind(fcr, cop->ses, &error);
-		lockmgr(&cryptodev_lock, LK_RELEASE);
-		if (cse) {
-			error = cryptodev_op(cse, cop, cred);
-			csedrop(cse);
-		}
-		break;
-	case CIOCKEY:
-	case CIOCKEY2:
-		if (!crypto_userasymcrypto)
-			return (EPERM);		/* XXX compat? */
-		lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
-		kop = (struct crypt_kop *)data;
-		if (cmd == CIOCKEY) {
-			/* NB: crypto core enforces s/w driver use */
-			kop->crk_crid =
-			    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
-		}
-		error = cryptodev_key(kop);
-		lockmgr(&cryptodev_lock, LK_RELEASE);
-		break;
-	case CIOCASYMFEAT:
-		if (!crypto_userasymcrypto) {
-			/*
-			 * NB: if user asym crypto operations are
-			 * not permitted return "no algorithms"
-			 * so well-behaved applications will just
-			 * fallback to doing them in software.
-			 */
-			*(int *)data = 0;
-		} else {
-			error = crypto_getfeat((int *)data);
-		}
-		break;
-	case CIOCFINDDEV:
-		error = cryptodev_find((struct crypt_find_op *)data);
-		break;
-	default:
-		error = EINVAL;
-		break;
-	}
-	return (error);
-#undef SES2
-}
-
-static int cryptodev_cb(void *);
-
-static int
-cryptodev_op(struct csession *cse, struct crypt_op *cop,
-	     struct ucred *active_cred)
-{
-	struct cryptop *crp = NULL;
-	struct cryptodesc *crde = NULL, *crda = NULL;
-	int error;
-
-	if (cop->len > 256*1024-4)
-		return (E2BIG);
-
-	if (cse->txform) {
-		if (cop->len == 0 || (cop->len % cse->txform->blocksize) != 0)
-			return (EINVAL);
-	}
-
-	bzero(&cse->uio, sizeof(cse->uio));
-	cse->uio.uio_iov = &cse->iovec;
-	cse->uio.uio_iovcnt = 1;
-	cse->uio.uio_offset = 0;
-	cse->uio.uio_resid = cop->len;
-	cse->uio.uio_segflg = UIO_SYSSPACE;
-	cse->uio.uio_rw = UIO_WRITE;
-	/* XXX: not sure, was td, now curthread? */
-	cse->uio.uio_td = curthread;
-	cse->uio.uio_iov[0].iov_len = cop->len;
-	if (cse->thash) {
-		cse->uio.uio_iov[0].iov_len += cse->thash->hashsize;
-		cse->uio.uio_resid += cse->thash->hashsize;
-	}
-	cse->uio.uio_iov[0].iov_base = kmalloc(cse->uio.uio_iov[0].iov_len,
-					       M_XDATA, M_WAITOK);
-
-	crp = crypto_getreq((cse->txform != NULL) + (cse->thash != NULL));
-	if (crp == NULL) {
-		error = ENOMEM;
-		goto bail;
-	}
-
-	if (cse->thash) {
-		crda = crp->crp_desc;
-		if (cse->txform)
-			crde = crda->crd_next;
-	} else {
-		if (cse->txform)
-			crde = crp->crp_desc;
-		else {
-			error = EINVAL;
-			goto bail;
-		}
-	}
-
-	if ((error = copyin(cop->src, cse->uio.uio_iov[0].iov_base, cop->len)))
-		goto bail;
-
-	if (crda) {
-		crda->crd_skip = 0;
-		crda->crd_len = cop->len;
-		crda->crd_inject = cop->len;
-
-		crda->crd_alg = cse->mac;
-		crda->crd_key = cse->mackey;
-		crda->crd_klen = cse->mackeylen * 8;
-	}
-
-	if (crde) {
-		if (cop->op == COP_ENCRYPT)
-			crde->crd_flags |= CRD_F_ENCRYPT;
-		else
-			crde->crd_flags &= ~CRD_F_ENCRYPT;
-		crde->crd_len = cop->len;
-		crde->crd_inject = 0;
-
-		crde->crd_alg = cse->cipher;
-		crde->crd_key = cse->key;
-		crde->crd_klen = cse->keylen * 8;
-	}
-
-	crp->crp_ilen = cop->len;
-	crp->crp_flags = CRYPTO_F_IOV | CRYPTO_F_CBIMM
-		       | (cop->flags & COP_F_BATCH);
-	crp->crp_buf = (caddr_t)&cse->uio;
-	crp->crp_callback = (int (*) (struct cryptop *)) cryptodev_cb;
-	crp->crp_sid = cse->sid;
-	crp->crp_opaque = (void *)cse;
-
-	if (cop->iv) {
-		if (crde == NULL) {
-			error = EINVAL;
-			goto bail;
-		}
-		if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
-			error = EINVAL;
-			goto bail;
-		}
-		if ((error = copyin(cop->iv, cse->tmp_iv, cse->txform->blocksize)))
-			goto bail;
-		bcopy(cse->tmp_iv, crde->crd_iv, cse->txform->blocksize);
-		crde->crd_flags |= CRD_F_IV_EXPLICIT | CRD_F_IV_PRESENT;
-		crde->crd_skip = 0;
-	} else if (cse->cipher == CRYPTO_ARC4) { /* XXX use flag? */
-		crde->crd_skip = 0;
-	} else if (crde) {
-		crde->crd_flags |= CRD_F_IV_PRESENT;
-		crde->crd_skip = cse->txform->blocksize;
-		crde->crd_len -= cse->txform->blocksize;
-	}
-
-	if (cop->mac && crda == NULL) {
-		error = EINVAL;
-		goto bail;
-	}
-
-again:
-	/*
-	 * Let the dispatch run unlocked, then, interlock against the
-	 * callback before checking if the operation completed and going
-	 * to sleep.  This insures drivers don't inherit our lock which
-	 * results in a lock order reversal between crypto_dispatch forced
-	 * entry and the crypto_done callback into us.
-	 */
-	error = crypto_dispatch(crp);
-	lockmgr(&cse->lock, LK_EXCLUSIVE);
-	if (error == 0 && (crp->crp_flags & CRYPTO_F_DONE) == 0)
-		error = lksleep(crp, &cse->lock, 0, "crydev", 0);
-	lockmgr(&cse->lock, LK_RELEASE);
-
-	if (error != 0)
-		goto bail;
-
-	if (crp->crp_etype == EAGAIN) {
-		crp->crp_etype = 0;
-		crp->crp_flags &= ~CRYPTO_F_DONE;
-		goto again;
-	}
-
-	if (crp->crp_etype != 0) {
-		error = crp->crp_etype;
-		goto bail;
-	}
-
-	if (cse->error) {
-		error = cse->error;
-		goto bail;
-	}
-
-	if (cop->dst &&
-	    (error = copyout(cse->uio.uio_iov[0].iov_base, cop->dst, cop->len)))
-		goto bail;
-
-	if (cop->mac &&
-	    (error = copyout((caddr_t)cse->uio.uio_iov[0].iov_base + cop->len,
-	    cop->mac, cse->thash->hashsize)))
-		goto bail;
-
-bail:
-	if (crp)
-		crypto_freereq(crp);
-	if (cse->uio.uio_iov[0].iov_base)
-		kfree(cse->uio.uio_iov[0].iov_base, M_XDATA);
-
-	return (error);
-}
-
-static int
-cryptodev_cb(void *op)
-{
-	struct cryptop *crp = (struct cryptop *) op;
-	struct csession *cse = (struct csession *)crp->crp_opaque;
-
-	lockmgr(&cse->lock, LK_EXCLUSIVE);
-	cse->error = crp->crp_etype;
-	wakeup_one(crp);
-	lockmgr(&cse->lock, LK_RELEASE);
-	return (0);
-}
-
-static int
-cryptodevkey_cb(void *op)
-{
-	struct cryptkop *krp = (struct cryptkop *) op;
-
-	wakeup_one(krp);
-	return (0);
-}
-
-static int
-cryptodev_key(struct crypt_kop *kop)
-{
-	struct cryptkop *krp = NULL;
-	int error = EINVAL;
-	int in, out, size, i;
-
-	if (kop->crk_iparams + kop->crk_oparams > CRK_MAXPARAM) {
-		return (EFBIG);
-	}
-
-	in = kop->crk_iparams;
-	out = kop->crk_oparams;
-	switch (kop->crk_op) {
-	case CRK_MOD_EXP:
-		if (in == 3 && out == 1)
-			break;
-		return (EINVAL);
-	case CRK_MOD_EXP_CRT:
-		if (in == 6 && out == 1)
-			break;
-		return (EINVAL);
-	case CRK_DSA_SIGN:
-		if (in == 5 && out == 2)
-			break;
-		return (EINVAL);
-	case CRK_DSA_VERIFY:
-		if (in == 7 && out == 0)
-			break;
-		return (EINVAL);
-	case CRK_DH_COMPUTE_KEY:
-		if (in == 3 && out == 1)
-			break;
-		return (EINVAL);
-	default:
-		return (EINVAL);
-	}
-
-	krp = (struct cryptkop *)kmalloc(sizeof *krp, M_XDATA, M_WAITOK | M_ZERO);
-	krp->krp_op = kop->crk_op;
-	krp->krp_status = kop->crk_status;
-	krp->krp_iparams = kop->crk_iparams;
-	krp->krp_oparams = kop->crk_oparams;
-	krp->krp_crid = kop->crk_crid;
-	krp->krp_status = 0;
-	krp->krp_callback = (int (*) (struct cryptkop *)) cryptodevkey_cb;
-
-	for (i = 0; i < CRK_MAXPARAM; i++) {
-		if (kop->crk_param[i].crp_nbits > 65536)
-			/* Limit is the same as in OpenBSD */
-			goto fail;
-		krp->krp_param[i].crp_nbits = kop->crk_param[i].crp_nbits;
-	}
-	for (i = 0; i < krp->krp_iparams + krp->krp_oparams; i++) {
-		size = (krp->krp_param[i].crp_nbits + 7) / 8;
-		if (size == 0)
-			continue;
-		krp->krp_param[i].crp_p = kmalloc(size, M_XDATA, M_WAITOK);
-		if (i >= krp->krp_iparams)
-			continue;
-		error = copyin(kop->crk_param[i].crp_p, krp->krp_param[i].crp_p, size);
-		if (error)
-			goto fail;
-	}
-
-	error = crypto_kdispatch(krp);
-	if (error)
-		goto fail;
-	error = tsleep(krp, 0, "crydev", 0);
-	if (error) {
-		/* XXX can this happen?  if so, how do we recover? */
-		goto fail;
-	}
-	
-	kop->crk_crid = krp->krp_crid;		/* device that did the work */
-	if (krp->krp_status != 0) {
-		error = krp->krp_status;
-		goto fail;
-	}
-
-	for (i = krp->krp_iparams; i < krp->krp_iparams + krp->krp_oparams; i++) {
-		size = (krp->krp_param[i].crp_nbits + 7) / 8;
-		if (size == 0)
-			continue;
-		error = copyout(krp->krp_param[i].crp_p, kop->crk_param[i].crp_p, size);
-		if (error)
-			goto fail;
-	}
-
-fail:
-	if (krp) {
-		kop->crk_status = krp->krp_status;
-		for (i = 0; i < CRK_MAXPARAM; i++) {
-			if (krp->krp_param[i].crp_p) {
-				bzero(krp->krp_param[i].crp_p,
-				    (krp->krp_param[i].crp_nbits + 7) / 8);
-				kfree(krp->krp_param[i].crp_p, M_XDATA);
-			}
-		}
-		kfree(krp, M_XDATA);
-	}
-	return (error);
-}
-
-static int
-cryptodev_find(struct crypt_find_op *find)
-{
-	device_t dev;
-
-	if (find->crid != -1) {
-		dev = crypto_find_device_byhid(find->crid);
-		if (dev == NULL)
-			return (ENOENT);
-		strlcpy(find->name, device_get_nameunit(dev),
-		    sizeof(find->name));
-	} else {
-		find->crid = crypto_find_driver(find->name);
-		if (find->crid == -1)
-			return (ENOENT);
-	}
-	return (0);
-}
-
-/*
- * MPSAFE
- */
-static int
-cryptof_kqfilter(struct file *fp, struct knote *kn)
-{
-
-	return (0);
-}
-
-/*
- * MPSAFE
- */
-static int
-cryptof_stat(struct file *fp, struct stat *sb, struct ucred *cred)
-{
-	return (EOPNOTSUPP);
-}
-
-static int
-cryptof_close(struct file *fp)
-{
-	struct fcrypt *fcr = fp->f_data;
-	struct csession *cse;
-
-	lockmgr(&cryptodev_lock, LK_EXCLUSIVE);
-	fcr = (struct fcrypt *)fp->f_data;
-	while ((cse = TAILQ_FIRST(&fcr->csessions))) {
-		TAILQ_REMOVE(&fcr->csessions, cse, next);
-		(void)csefree(cse);
-	}
-	fp->f_data = NULL;
-	lockmgr(&cryptodev_lock, LK_RELEASE);
-
-	kfree(fcr, M_XDATA);
-	return (0);
-}
-
-static struct csession *
-csefind(struct fcrypt *fcr, u_int ses, int *errorp)
-{
-	struct csession *cse;
-
-	TAILQ_FOREACH(cse, &fcr->csessions, next) {
-		if (cse->ses == ses) {
-			if (cse->busy) {
-				kprintf("csefind: cse %p BUSY\n", cse);
-				*errorp = EBUSY;
-				return NULL;
-			}
-			cse->busy = 1;
-			return (cse);
-		}
-	}
-	*errorp = EINVAL;
-	return (NULL);
-}
-
-static void
-csedrop(struct csession *cse)
-{
-	cse->busy = 0;
-}
-
-static int
-csedelete(struct fcrypt *fcr, struct csession *cse_del)
-{
-	struct csession *cse;
-
-	TAILQ_FOREACH(cse, &fcr->csessions, next) {
-		if (cse == cse_del) {
-			TAILQ_REMOVE(&fcr->csessions, cse, next);
-			return (1);
-		}
-	}
-	return (0);
-}
-	
-static struct csession *
-cseadd(struct fcrypt *fcr, struct csession *cse)
-{
-	TAILQ_INSERT_TAIL(&fcr->csessions, cse, next);
-	cse->ses = fcr->sesn++;
-	return (cse);
-}
-
-struct csession *
-csecreate(struct fcrypt *fcr, u_int64_t sid, caddr_t key, u_int64_t keylen,
-    caddr_t mackey, u_int64_t mackeylen, u_int32_t cipher, u_int32_t mac,
-    struct enc_xform *txform, struct auth_hash *thash)
-{
-	struct csession *cse;
-
-	cse = kmalloc(sizeof(struct csession), M_XDATA, M_WAITOK | M_ZERO);
-	lockinit(&cse->lock, "cryptodev", 0, LK_CANRECURSE);
-	cse->key = key;
-	cse->keylen = keylen/8;
-	cse->mackey = mackey;
-	cse->mackeylen = mackeylen/8;
-	cse->sid = sid;
-	cse->cipher = cipher;
-	cse->mac = mac;
-	cse->txform = txform;
-	cse->thash = thash;
-	cseadd(fcr, cse);
-	return (cse);
-}
-
-static int
-csefree(struct csession *cse)
-{
-	int error;
-
-	error = crypto_freesession(cse->sid);
-	lockuninit(&cse->lock);
-	if (cse->key)
-		kfree(cse->key, M_XDATA);
-	if (cse->mackey)
-		kfree(cse->mackey, M_XDATA);
-	kfree(cse, M_XDATA);
-	return (error);
-}
-
-static int
-cryptoopen(struct dev_open_args *ap)
-{
-	return (0);
-}
-
-static int
-cryptoread(struct dev_read_args *ap)
-{
-	return (EIO);
-}
-
-static int
-cryptowrite(struct dev_write_args *ap)
-{
-	return (EIO);
-}
-
-static int
-cryptoioctl(struct dev_ioctl_args *ap)
-{
-	struct thread *td = curthread;
-	struct file *f;
-	struct fcrypt *fcr;
-	int fd, error;
-
-	switch (ap->a_cmd) {
-	case CRIOGET:
-		fcr = kmalloc(sizeof(struct fcrypt), M_XDATA, M_WAITOK);
-		TAILQ_INIT(&fcr->csessions);
-		fcr->sesn = 0;
-
-		KKASSERT(td->td_lwp);
-		error = falloc(td->td_lwp, &f, &fd);
-
-		if (error) {
-			kfree(fcr, M_XDATA);
-			return (error);
-		}
-		/* falloc automatically provides an extra reference to 'f'. */
-		f->f_flag = FREAD | FWRITE;
-		f->f_type = DTYPE_CRYPTO;
-		f->f_ops = &cryptofops;
-		f->f_data = fcr;
-		fsetfd(td->td_proc->p_fd, f, fd);
-		*(u_int32_t *)ap->a_data = fd;
-		fdrop(f);
-
-		break;
-	case CRIOFINDDEV:
-		error = cryptodev_find((struct crypt_find_op *)ap->a_data);
-		break;
-	case CRIOASYMFEAT:
-		error = crypto_getfeat((int *)ap->a_data);
-		break;
-	default:
-		error = EINVAL;
-		break;
-	}
-	return (error);
-}
-
-static struct dev_ops crypto_ops = {
-	{ "crypto", 0, D_MPSAFE },
-	.d_open =	cryptoopen,
-	.d_read =	cryptoread,
-	.d_write =	cryptowrite,
-	.d_ioctl =	cryptoioctl,
-};
-
-/*
- * Initialization code, both for static and dynamic loading.
- */
-static int
-cryptodev_modevent(module_t mod, int type, void *unused)
-{
-	switch (type) {
-	case MOD_LOAD:
-		if (bootverbose)
-			kprintf("crypto: <crypto device>\n");
-		make_dev(&crypto_ops, 0, UID_ROOT, GID_WHEEL,
-			 0666, "crypto");
-		return 0;
-	case MOD_UNLOAD:
-		/*XXX disallow if active sessions */
-		//dev_ops_remove(&crypto_ops, 0, 0);
-		dev_ops_remove_all(&crypto_ops);
-		return 0;
-	}
-	return EINVAL;
-}
-
-static moduledata_t cryptodev_mod = {
-	"cryptodev",
-	cryptodev_modevent,
-	0
-};
-MODULE_VERSION(cryptodev, 1);
-DECLARE_MODULE(cryptodev, cryptodev_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
-MODULE_DEPEND(cryptodev, crypto, 1, 1, 1);
diff --git a/sys/opencrypto/cryptodev.h b/sys/opencrypto/cryptodev.h
deleted file mode 100644
index 818b724cee..0000000000
--- a/sys/opencrypto/cryptodev.h
+++ /dev/null
@@ -1,462 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.25 2007/05/09 19:37:02 gnn Exp $	*/
-/*	$OpenBSD: cryptodev.h,v 1.31 2002/06/11 11:14:29 beck Exp $	*/
-
-/*-
- * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
- * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
- *
- * This code was written by Angelos D. Keromytis in Athens, Greece, in
- * February 2000. Network Security Technologies Inc. (NSTI) kindly
- * supported the development of this code.
- *
- * Copyright (c) 2000 Angelos D. Keromytis
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all source code copies of any software which is or includes a copy or
- * modification of this software.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- *
- * Copyright (c) 2001 Theo de Raadt
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- *
- * Effort sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F30602-01-2-0537.
- *
- */
-
-#ifndef _CRYPTO_CRYPTO_H_
-#define _CRYPTO_CRYPTO_H_
-
-#include <sys/ioccom.h>
-#include <sys/bus.h>
-
-/* Some initial values */
-#define CRYPTO_DRIVERS_INITIAL	4
-#define CRYPTO_SW_SESSIONS	32
-
-/* Hash values */
-#define	NULL_HASH_LEN		16
-#define	MD5_HASH_LEN		16
-#define	SHA1_HASH_LEN		20
-#define	RIPEMD160_HASH_LEN	20
-#define	SHA2_256_HASH_LEN	32
-#define	SHA2_384_HASH_LEN	48
-#define	SHA2_512_HASH_LEN	64
-#define	MD5_KPDK_HASH_LEN	16
-#define	SHA1_KPDK_HASH_LEN	20
-/* Maximum hash algorithm result length */
-#define	HASH_MAX_LEN		SHA2_512_HASH_LEN /* Keep this updated */
-
-/* HMAC values */
-#define	NULL_HMAC_BLOCK_LEN		64
-#define	MD5_HMAC_BLOCK_LEN		64
-#define	SHA1_HMAC_BLOCK_LEN		64
-#define	RIPEMD160_HMAC_BLOCK_LEN	64
-#define	SHA2_256_HMAC_BLOCK_LEN		64
-#define	SHA2_384_HMAC_BLOCK_LEN		128
-#define	SHA2_512_HMAC_BLOCK_LEN		128
-/* Maximum HMAC block length */
-#define	HMAC_MAX_BLOCK_LEN		SHA2_512_HMAC_BLOCK_LEN /* Keep this updated */
-#define HMAC_IPAD_VAL		0x36
-#define HMAC_OPAD_VAL		0x5C
-
-/* Encryption algorithm block sizes */
-#define NULL_BLOCK_LEN		4
-#define DES_BLOCK_LEN		8
-#define DES3_BLOCK_LEN		8
-#define BLOWFISH_BLOCK_LEN	8
-#define SKIPJACK_BLOCK_LEN	8
-#define CAST128_BLOCK_LEN	8
-#define RIJNDAEL128_BLOCK_LEN	16
-#define AES_BLOCK_LEN		RIJNDAEL128_BLOCK_LEN
-#define AES_XTS_BLOCK_LEN	16
-#define AES_XTS_IV_LEN		8
-#define AESCTR_IV_LEN		8
-#define AESCTR_BLOCK_LEN	16
-#define AESGCM_IV_LEN		8
-#define AESGCM_BLOCK_LEN	1
-#define AESGMAC_IV_LEN		8
-#define AESGMAC_BLOCK_LEN	1
-#define CAMELLIA_BLOCK_LEN	16
-#define TWOFISH_BLOCK_LEN	16
-#define SERPENT_BLOCK_LEN	16
-#define TWOFISH_XTS_BLOCK_LEN	16
-#define TWOFISH_XTS_IV_LEN	8
-#define SERPENT_XTS_BLOCK_LEN	16
-#define SERPENT_XTS_IV_LEN	8
-#define EALG_MAX_BLOCK_LEN	AES_BLOCK_LEN /* Keep this updated */
-
-#define	CRYPTO_ALGORITHM_MIN	1
-#define CRYPTO_DES_CBC		1
-#define CRYPTO_3DES_CBC		2
-#define CRYPTO_BLF_CBC		3
-#define CRYPTO_CAST_CBC		4
-#define CRYPTO_SKIPJACK_CBC	5
-#define CRYPTO_MD5_HMAC		6
-#define CRYPTO_SHA1_HMAC	7
-#define CRYPTO_RIPEMD160_HMAC	8
-#define CRYPTO_MD5_KPDK		9
-#define CRYPTO_SHA1_KPDK	10
-#define CRYPTO_RIJNDAEL128_CBC	11 /* 128 bit blocksize */
-#define CRYPTO_AES_CBC		11 /* 128 bit blocksize -- the same as above */
-#define CRYPTO_ARC4		12
-#define	CRYPTO_MD5		13
-#define	CRYPTO_SHA1		14
-#define	CRYPTO_NULL_HMAC	15
-#define	CRYPTO_NULL_CBC		16
-#define	CRYPTO_DEFLATE_COMP	17 /* Deflate compression algorithm */
-#define	CRYPTO_SHA2_256_HMAC	18
-#define	CRYPTO_SHA2_384_HMAC	19
-#define	CRYPTO_SHA2_512_HMAC	20
-#define CRYPTO_CAMELLIA_CBC	21
-#define CRYPTO_AES_XTS		22
-#define CRYPTO_AES_CTR          23
-#define CRYPTO_AES_GCM_16	24
-#define CRYPTO_AES_128_GMAC	25
-#define CRYPTO_AES_192_GMAC	26
-#define CRYPTO_AES_256_GMAC	27
-#define CRYPTO_AES_GMAC		28
-#define CRYPTO_TWOFISH_CBC	29
-#define CRYPTO_SERPENT_CBC	30
-#define CRYPTO_TWOFISH_XTS	31
-#define CRYPTO_SERPENT_XTS	32
-#define	CRYPTO_ALGORITHM_MAX	32 /* Keep updated - see below */
-
-/* Algorithm flags */
-#define	CRYPTO_ALG_FLAG_SUPPORTED	0x01 /* Algorithm is supported */
-#define	CRYPTO_ALG_FLAG_RNG_ENABLE	0x02 /* Has HW RNG for DH/DSA */
-#define	CRYPTO_ALG_FLAG_DSA_SHA		0x04 /* Can do SHA on msg */
-
-/*
- * Crypto driver/device flags.  They can set in the crid
- * parameter when creating a session or submitting a key
- * op to affect the device/driver assigned.  If neither
- * of these are specified then the crid is assumed to hold
- * the driver id of an existing (and suitable) device that
- * must be used to satisfy the request.
- */
-#define CRYPTO_FLAG_HARDWARE	0x01000000	/* hardware accelerated */
-#define CRYPTO_FLAG_SOFTWARE	0x02000000	/* software implementation */
-
-/* NB: deprecated */
-struct session_op {
-	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
-	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
-
-	u_int32_t	keylen;		/* cipher key */
-	caddr_t		key;
-	int		mackeylen;	/* mac key */
-	caddr_t		mackey;
-
-  	u_int32_t	ses;		/* returns: session # */
-};
-
-struct session2_op {
-	u_int32_t	cipher;		/* ie. CRYPTO_DES_CBC */
-	u_int32_t	mac;		/* ie. CRYPTO_MD5_HMAC */
-
-	u_int32_t	keylen;		/* cipher key */
-	caddr_t		key;
-	int		mackeylen;	/* mac key */
-	caddr_t		mackey;
-
-	u_int32_t	ses;		/* returns: session # */
-	int		crid;		/* driver id + flags (rw) */
-	int		pad[4];		/* for future expansion */
-};
-
-struct crypt_op {
-	u_int32_t	ses;
-	u_int16_t	op;		/* i.e. COP_ENCRYPT */
-#define COP_ENCRYPT	1
-#define COP_DECRYPT	2
-	u_int16_t	flags;
-#define	COP_F_BATCH	0x0008		/* Batch op if possible */
-	u_int		len;
-	caddr_t		src, dst;	/* become iov[] inside kernel */
-	caddr_t		mac;		/* must be big enough for chosen MAC */
-	caddr_t		iv;
-};
-
-/*
- * Parameters for looking up a crypto driver/device by
- * device name or by id.  The latter are returned for
- * created sessions (crid) and completed key operations.
- */
-struct crypt_find_op {
-	int		crid;		/* driver id + flags */
-	char		name[32];	/* device/driver name */
-};
-
-/* bignum parameter, in packed bytes, ... */
-struct crparam {
-	caddr_t		crp_p;
-	u_int		crp_nbits;
-};
-
-#define CRK_MAXPARAM	8
-
-struct crypt_kop {
-	u_int		crk_op;		/* ie. CRK_MOD_EXP or other */
-	u_int		crk_status;	/* return status */
-	u_short		crk_iparams;	/* # of input parameters */
-	u_short		crk_oparams;	/* # of output parameters */
-	u_int		crk_crid;	/* NB: only used by CIOCKEY2 (rw) */
-	struct crparam	crk_param[CRK_MAXPARAM];
-};
-#define	CRK_ALGORITM_MIN	0
-#define CRK_MOD_EXP		0
-#define CRK_MOD_EXP_CRT		1
-#define CRK_DSA_SIGN		2
-#define CRK_DSA_VERIFY		3
-#define CRK_DH_COMPUTE_KEY	4
-#define CRK_ALGORITHM_MAX	4 /* Keep updated - see below */
-
-#define CRF_MOD_EXP		(1 << CRK_MOD_EXP)
-#define CRF_MOD_EXP_CRT		(1 << CRK_MOD_EXP_CRT)
-#define CRF_DSA_SIGN		(1 << CRK_DSA_SIGN)
-#define CRF_DSA_VERIFY		(1 << CRK_DSA_VERIFY)
-#define CRF_DH_COMPUTE_KEY	(1 << CRK_DH_COMPUTE_KEY)
-
-/*
- * done against open of /dev/crypto, to get a cloned descriptor.
- * Please use F_SETFD against the cloned descriptor.
- */
-#define	CRIOGET		_IOWR('c', 100, u_int32_t)
-#define	CRIOASYMFEAT	CIOCASYMFEAT
-#define	CRIOFINDDEV	CIOCFINDDEV
-
-/* the following are done against the cloned descriptor */
-#define	CIOCGSESSION	_IOWR('c', 101, struct session_op)
-#define	CIOCFSESSION	_IOW('c', 102, u_int32_t)
-#define CIOCCRYPT	_IOWR('c', 103, struct crypt_op)
-#define CIOCKEY		_IOWR('c', 104, struct crypt_kop)
-#define CIOCASYMFEAT	_IOR('c', 105, u_int32_t)
-#define	CIOCGSESSION2	_IOWR('c', 106, struct session2_op)
-#define	CIOCKEY2	_IOWR('c', 107, struct crypt_kop)
-#define	CIOCFINDDEV	_IOWR('c', 108, struct crypt_find_op)
-
-struct cryptotstat {
-	struct timespec	acc;		/* total accumulated time */
-	struct timespec	min;		/* min time */
-	struct timespec max;		/* max time */
-	u_int32_t	count;		/* number of observations */
-};
-
-struct cryptostats {
-	u_int32_t	cs_ops;		/* symmetric crypto ops submitted */
-	u_int32_t	cs_errs;	/* symmetric crypto ops that failed */
-	u_int32_t	cs_kops;	/* asymetric/key ops submitted */
-	u_int32_t	cs_kerrs;	/* asymetric/key ops that failed */
-	u_int32_t	cs_intrs;	/* crypto swi thread activations */
-	u_int32_t	cs_rets;	/* crypto return thread activations */
-	u_int32_t	cs_blocks;	/* symmetric op driver block */
-	u_int32_t	cs_kblocks;	/* symmetric op driver block */
-	/*
-	 * When CRYPTO_TIMING is defined at compile time and the
-	 * sysctl debug.crypto is set to 1, the crypto system will
-	 * accumulate statistics about how long it takes to process
-	 * crypto requests at various points during processing.
-	 */
-	struct cryptotstat cs_invoke;	/* crypto_dipsatch -> crypto_invoke */
-	struct cryptotstat cs_done;	/* crypto_invoke -> crypto_done */
-	struct cryptotstat cs_cb;	/* crypto_done -> callback */
-	struct cryptotstat cs_finis;	/* callback -> callback return */
-};
-
-#ifdef _KERNEL
-/* Standard initialization structure beginning */
-struct cryptoini {
-	int		cri_alg;	/* Algorithm to use */
-	int		cri_klen;	/* Key length, in bits */
-	int		cri_mlen;	/* Number of bytes we want from the
-					   entire hash. 0 means all. */
-	caddr_t		cri_key;	/* key to use */
-	u_int8_t	cri_iv[EALG_MAX_BLOCK_LEN];	/* IV to use */
-	struct cryptoini *cri_next;
-};
-
-/* Describe boundaries of a single crypto operation */
-struct cryptodesc {
-	int		crd_skip;	/* How many bytes to ignore from start */
-	int		crd_len;	/* How many bytes to process */
-	int		crd_inject;	/* Where to inject results, if applicable */
-	int		crd_flags;
-
-#define	CRD_F_ENCRYPT		0x01	/* Set when doing encryption */
-#define	CRD_F_IV_PRESENT	0x02	/* When encrypting, IV is already in
-					   place, so don't copy. */
-#define	CRD_F_IV_EXPLICIT	0x04	/* IV explicitly provided */
-#define	CRD_F_DSA_SHA_NEEDED	0x08	/* Compute SHA-1 of buffer for DSA */
-#define	CRD_F_COMP		0x0f	/* Set when doing compression */
-#define	CRD_F_KEY_EXPLICIT	0x10	/* Key explicitly provided */
-
-	struct cryptoini	CRD_INI; /* Initialization/context data */
-#define crd_iv		CRD_INI.cri_iv
-#define crd_key		CRD_INI.cri_key
-#define crd_alg		CRD_INI.cri_alg
-#define crd_klen	CRD_INI.cri_klen
-
-	struct cryptodesc *crd_next;
-};
-
-/* Structure describing complete operation */
-struct cryptop {
-	TAILQ_ENTRY(cryptop) crp_next;
-
-	u_int64_t	crp_sid;	/* Session ID */
-	int		crp_ilen;	/* Input data total length */
-	int		crp_olen;	/* Result total length */
-
-	int		crp_etype;	/*
-					 * Error type (zero means no error).
-					 * All error codes except EAGAIN
-					 * indicate possible data corruption (as in,
-					 * the data have been touched). On all
-					 * errors, the crp_sid may have changed
-					 * (reset to a new one), so the caller
-					 * should always check and use the new
-					 * value on future requests.
-					 */
-	int		crp_flags;
-
-#define CRYPTO_F_IMBUF	0x0001	/* Input/output are mbuf chains */
-#define CRYPTO_F_IOV	0x0002	/* Input/output are uio */
-#define CRYPTO_F_REL	0x0004	/* Must return data in same place */
-#define	CRYPTO_F_BATCH	0x0008	/* Batch op if possible */
-#define	CRYPTO_F_CBIMM	0x0010	/* Do callback immediately */
-#define	CRYPTO_F_DONE	0x0020	/* Operation completed */
-#define	CRYPTO_F_CBIFSYNC	0x0040	/* Do CBIMM if op is synchronous */
-
-	caddr_t		crp_buf;	/* Data to be processed */
-	caddr_t		crp_opaque;	/* Opaque pointer, passed along */
-	struct cryptodesc *crp_desc;	/* Linked list of processing descriptors */
-
-	int (*crp_callback)(struct cryptop *); /* Callback function */
-
-	struct timespec	crp_tstamp;	/* performance time stamp */
-
-	caddr_t		crp_mac;
-};
-
-#define CRYPTO_BUF_CONTIG	0x0
-#define CRYPTO_BUF_IOV		0x1
-#define CRYPTO_BUF_MBUF		0x2
-
-#define CRYPTO_OP_DECRYPT	0x0
-#define CRYPTO_OP_ENCRYPT	0x1
-
-/*
- * Hints passed to process methods.
- */
-#define	CRYPTO_HINT_MORE	0x1	/* more ops coming shortly */
-
-struct cryptkop {
-	TAILQ_ENTRY(cryptkop) krp_next;
-
-	u_int		krp_op;		/* ie. CRK_MOD_EXP or other */
-	u_int		krp_status;	/* return status */
-	u_short		krp_iparams;	/* # of input parameters */
-	u_short		krp_oparams;	/* # of output parameters */
-	u_int		krp_crid;	/* desired device, etc. */
-	u_int32_t	krp_hid;
-	struct crparam	krp_param[CRK_MAXPARAM];	/* kvm */
-	int		(*krp_callback)(struct cryptkop *);
-};
-
-/*
- * Session ids are 64 bits.  The lower 32 bits contain a "local id" which
- * is a driver-private session identifier.  The upper 32 bits contain a
- * "hardware id" used by the core crypto code to identify the driver and
- * a copy of the driver's capabilities that can be used by client code to
- * optimize operation.
- */
-#define	CRYPTO_SESID2HID(_sid)	(((_sid) >> 32) & 0x00ffffff)
-#define	CRYPTO_SESID2CAPS(_sid)	(((_sid) >> 32) & 0xff000000)
-#define	CRYPTO_SESID2LID(_sid)	(((u_int32_t) (_sid)) & 0xffffffff)
-
-#ifdef MALLOC_DECLARE
-MALLOC_DECLARE(M_CRYPTO_DATA);
-#endif
-
-extern	int crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard);
-extern	int crypto_freesession(u_int64_t sid);
-#define CRYPTOCAP_F_HARDWARE	CRYPTO_FLAG_HARDWARE
-#define CRYPTOCAP_F_SOFTWARE	CRYPTO_FLAG_SOFTWARE
-#define CRYPTOCAP_F_SYNC	0x04000000	/* operates synchronously */
-#define CRYPTOCAP_F_SMP		0x08000000	/* SMP dispatch ok */
-extern	int32_t crypto_get_driverid(device_t dev, int flags);
-extern	int crypto_find_driver(const char *);
-extern	device_t crypto_find_device_byhid(int hid);
-extern	int crypto_getcaps(int hid);
-extern	int crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
-	    u_int32_t flags);
-extern	int crypto_kregister(u_int32_t, int, u_int32_t);
-extern	int crypto_unregister(u_int32_t driverid, int alg);
-extern	int crypto_unregister_all(u_int32_t driverid);
-extern	int crypto_dispatch(struct cryptop *crp);
-extern	int crypto_kdispatch(struct cryptkop *);
-#define	CRYPTO_SYMQ	0x1
-#define	CRYPTO_ASYMQ	0x2
-extern	int crypto_unblock(u_int32_t, int);
-extern	void crypto_done(struct cryptop *crp);
-extern	void crypto_kdone(struct cryptkop *);
-extern	int crypto_getfeat(int *);
-
-extern	void crypto_freereq(struct cryptop *crp);
-extern	struct cryptop *crypto_getreq(int num);
-
-extern	int crypto_usercrypto;		/* userland may do crypto requests */
-extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
-extern	int crypto_devallowsoft;	/* only use hardware crypto */
-
-/*
- * Crypto-related utility routines used mainly by drivers.
- *
- * XXX these don't really belong here; but for now they're
- *     kept apart from the rest of the system.
- */
-struct uio;
-extern	void cuio_copydata(struct uio* uio, int off, int len, caddr_t cp);
-extern	void cuio_copyback(struct uio* uio, int off, int len, caddr_t cp);
-extern	struct iovec *cuio_getptr(struct uio *uio, int loc, int *off);
-extern	int cuio_apply(struct uio *uio, int off, int len,
-	    int (*f)(void *, void *, u_int), void *arg);
-
-extern	void crypto_copyback(int flags, caddr_t buf, int off, int size,
-	    caddr_t in);
-extern	void crypto_copydata(int flags, caddr_t buf, int off, int size,
-	    caddr_t out);
-extern	int crypto_apply(int flags, caddr_t buf, int off, int len,
-	    int (*f)(void *, void *, u_int), void *arg);
-#endif /* _KERNEL */
-#endif /* _CRYPTO_CRYPTO_H_ */
diff --git a/sys/opencrypto/cryptodev_if.m b/sys/opencrypto/cryptodev_if.m
deleted file mode 100644
index 31aa83d9a1..0000000000
--- a/sys/opencrypto/cryptodev_if.m
+++ /dev/null
@@ -1,55 +0,0 @@
-#-
-# Copyright (c) 2006, Sam Leffler
-# 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 AUTHOR 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 AUTHOR 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.
-#
-# $FreeBSD: src/sys/opencrypto/cryptodev_if.m,v 1.1 2007/03/21 03:43:33 sam Exp $
-#
-
-#include <sys/malloc.h>
-#include <opencrypto/cryptodev.h>
-
-INTERFACE cryptodev;
-
-METHOD int newsession {
-	device_t	dev;
-	uint32_t	*sid;
-	struct cryptoini *cri;
-};
-
-METHOD int freesession {
-	device_t	dev;
-	uint64_t	sid;
-};
-
-METHOD int process {
-	device_t	dev;
-	struct cryptop	*op;
-	int		flags;
-};
-
-METHOD int kprocess {
-	device_t	dev;
-	struct cryptkop	*op;
-	int		flags;
-};
diff --git a/sys/opencrypto/cryptosoft.c b/sys/opencrypto/cryptosoft.c
deleted file mode 100644
index 6ad6805cc3..0000000000
--- a/sys/opencrypto/cryptosoft.c
+++ /dev/null
@@ -1,1433 +0,0 @@
-/*-
- * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
- * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
- *
- * This code was written by Angelos D. Keromytis in Athens, Greece, in
- * February 2000. Network Security Technologies Inc. (NSTI) kindly
- * supported the development of this code.
- *
- * Copyright (c) 2000, 2001 Angelos D. Keromytis
- *
- * SMP modifications by Matthew Dillon for the DragonFlyBSD Project
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all source code copies of any software which is or includes a copy or
- * modification of this software.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- *
- * $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.23 2009/02/05 17:43:12 imp Exp $
- * $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/malloc.h>
-#include <sys/mbuf.h>
-#include <sys/module.h>
-#include <sys/sysctl.h>
-#include <sys/errno.h>
-#include <sys/endian.h>
-#include <sys/random.h>
-#include <sys/kernel.h>
-#include <sys/uio.h>
-#include <sys/spinlock2.h>
-
-#include <crypto/blowfish/blowfish.h>
-#include <crypto/sha1.h>
-#include <opencrypto/rmd160.h>
-#include <opencrypto/cast.h>
-#include <opencrypto/skipjack.h>
-#include <sys/md5.h>
-
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/cryptosoft.h>
-#include <opencrypto/xform.h>
-
-#include <sys/kobj.h>
-#include <sys/bus.h>
-#include "cryptodev_if.h"
-
-static	int32_t swcr_id;
-static	struct swcr_data **swcr_sessions = NULL;
-static	u_int32_t swcr_sesnum;
-static	u_int32_t swcr_minsesnum = 1;
-
-static struct spinlock swcr_spin = SPINLOCK_INITIALIZER(swcr_spin, "swcr_spin");
-
-u_int8_t hmac_ipad_buffer[HMAC_MAX_BLOCK_LEN];
-u_int8_t hmac_opad_buffer[HMAC_MAX_BLOCK_LEN];
-
-static	int swcr_encdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
-static	int swcr_authcompute(struct cryptodesc *, struct swcr_data *, caddr_t, int);
-static	int swcr_combined(struct cryptop *);
-static	int swcr_compdec(struct cryptodesc *, struct swcr_data *, caddr_t, int);
-static	int swcr_freesession(device_t dev, u_int64_t tid);
-static	int swcr_freesession_slot(struct swcr_data **swdp, u_int32_t sid);
-
-/*
- * Apply a symmetric encryption/decryption algorithm.
- */
-static int
-swcr_encdec(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
-    int flags)
-{
-	unsigned char iv[EALG_MAX_BLOCK_LEN], blk[EALG_MAX_BLOCK_LEN], *idat;
-	unsigned char *ivp, *nivp, iv2[EALG_MAX_BLOCK_LEN];
-	u_int8_t *kschedule;
-	u_int8_t *okschedule;
-	struct enc_xform *exf;
-	int i, k, j, blks, ivlen;
-	int error;
-	int explicit_kschedule;
-
-	exf = sw->sw_exf;
-	blks = exf->blocksize;
-	ivlen = exf->ivsize;
-
-	/* Check for non-padded data */
-	if (crd->crd_len % blks)
-		return EINVAL;
-
-	/* Initialize the IV */
-	if (crd->crd_flags & CRD_F_ENCRYPT) {
-		/* IV explicitly provided ? */
-		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
-			bcopy(crd->crd_iv, iv, ivlen);
-		else
-			karc4random_buf(iv, ivlen);
-
-		/* Do we need to write the IV */
-		if (!(crd->crd_flags & CRD_F_IV_PRESENT))
-			crypto_copyback(flags, buf, crd->crd_inject, ivlen, iv);
-
-	} else {	/* Decryption */
-			/* IV explicitly provided ? */
-		if (crd->crd_flags & CRD_F_IV_EXPLICIT)
-			bcopy(crd->crd_iv, iv, ivlen);
-		else {
-			/* Get IV off buf */
-			crypto_copydata(flags, buf, crd->crd_inject, ivlen, iv);
-		}
-	}
-
-	ivp = iv;
-
-	/*
-	 * The semantics are seriously broken because the session key
-	 * storage was never designed for concurrent ops.
-	 */
-	if (crd->crd_flags & CRD_F_KEY_EXPLICIT) {
-		kschedule = kmalloc(exf->ctxsize, M_CRYPTO_DATA,
-				    M_NOWAIT | M_ZERO);
-		if (kschedule == NULL) {
-			error = ENOMEM;
-			goto out;
-		}
-		error = exf->setkey(kschedule, crd->crd_key,
-				    crd->crd_klen / 8);
-		if (error)
-			goto out;
-		explicit_kschedule = 1;
-	} else {
-		spin_lock(&swcr_spin);
-		kschedule = sw->sw_kschedule;
-		++sw->sw_kschedule_refs;
-		spin_unlock(&swcr_spin);
-		explicit_kschedule = 0;
-	}
-
-	/*
-	 * xforms that provide a reinit method perform all IV
-	 * handling themselves.
-	 */
-	if (exf->reinit)
-		exf->reinit(kschedule, iv);
-
-	if (flags & CRYPTO_F_IMBUF) {
-		struct mbuf *m = (struct mbuf *) buf;
-
-		/* Find beginning of data */
-		m = m_getptr(m, crd->crd_skip, &k);
-		if (m == NULL) {
-			error = EINVAL;
-			goto done;
-		}
-
-		i = crd->crd_len;
-
-		while (i > 0) {
-			/*
-			 * If there's insufficient data at the end of
-			 * an mbuf, we have to do some copying.
-			 */
-			if (m->m_len < k + blks && m->m_len != k) {
-				m_copydata(m, k, blks, blk);
-
-				/* Actual encryption/decryption */
-				if (exf->reinit) {
-					if (crd->crd_flags & CRD_F_ENCRYPT) {
-						exf->encrypt(kschedule,
-						    blk, iv);
-					} else {
-						exf->decrypt(kschedule,
-						    blk, iv);
-					}
-				} else if (crd->crd_flags & CRD_F_ENCRYPT) {
-					/* XOR with previous block */
-					for (j = 0; j < blks; j++)
-						blk[j] ^= ivp[j];
-
-					exf->encrypt(kschedule, blk, iv);
-
-					/*
-					 * Keep encrypted block for XOR'ing
-					 * with next block
-					 */
-					bcopy(blk, iv, blks);
-					ivp = iv;
-				} else {	/* decrypt */
-					/*
-					 * Keep encrypted block for XOR'ing
-					 * with next block
-					 */
-					nivp = (ivp == iv) ? iv2 : iv;
-					bcopy(blk, nivp, blks);
-
-					exf->decrypt(kschedule, blk, iv);
-
-					/* XOR with previous block */
-					for (j = 0; j < blks; j++)
-						blk[j] ^= ivp[j];
-
-					ivp = nivp;
-				}
-
-				/* Copy back decrypted block */
-				m_copyback(m, k, blks, blk);
-
-				/* Advance pointer */
-				m = m_getptr(m, k + blks, &k);
-				if (m == NULL) {
-					error = EINVAL;
-					goto done;
-				}
-
-				i -= blks;
-
-				/* Could be done... */
-				if (i == 0)
-					break;
-			}
-
-			/* Skip possibly empty mbufs */
-			if (k == m->m_len) {
-				for (m = m->m_next; m && m->m_len == 0;
-				    m = m->m_next)
-					;
-				k = 0;
-			}
-
-			/* Sanity check */
-			if (m == NULL) {
-				error = EINVAL;
-				goto done;
-			}
-
-			/*
-			 * Warning: idat may point to garbage here, but
-			 * we only use it in the while() loop, only if
-			 * there are indeed enough data.
-			 */
-			idat = mtod(m, unsigned char *) + k;
-
-			while (m->m_len >= k + blks && i > 0) {
-				if (exf->reinit) {
-					if (crd->crd_flags & CRD_F_ENCRYPT) {
-						exf->encrypt(kschedule,
-						    idat, iv);
-					} else {
-						exf->decrypt(kschedule,
-						    idat, iv);
-					}
-				} else if (crd->crd_flags & CRD_F_ENCRYPT) {
-					/* XOR with previous block/IV */
-					for (j = 0; j < blks; j++)
-						idat[j] ^= ivp[j];
-
-					exf->encrypt(kschedule, idat, iv);
-					ivp = idat;
-				} else {	/* decrypt */
-					/*
-					 * Keep encrypted block to be used
-					 * in next block's processing.
-					 */
-					nivp = (ivp == iv) ? iv2 : iv;
-					bcopy(idat, nivp, blks);
-
-					exf->decrypt(kschedule, idat, iv);
-
-					/* XOR with previous block/IV */
-					for (j = 0; j < blks; j++)
-						idat[j] ^= ivp[j];
-
-					ivp = nivp;
-				}
-
-				idat += blks;
-				k += blks;
-				i -= blks;
-			}
-		}
-		error = 0;	/* Done with mbuf encryption/decryption */
-	} else if (flags & CRYPTO_F_IOV) {
-		struct uio *uio = (struct uio *) buf;
-		struct iovec *iov;
-
-		/* Find beginning of data */
-		iov = cuio_getptr(uio, crd->crd_skip, &k);
-		if (iov == NULL) {
-			error = EINVAL;
-			goto done;
-		}
-
-		i = crd->crd_len;
-
-		while (i > 0) {
-			/*
-			 * If there's insufficient data at the end of
-			 * an iovec, we have to do some copying.
-			 */
-			if (iov->iov_len < k + blks && iov->iov_len != k) {
-				cuio_copydata(uio, k, blks, blk);
-
-				/* Actual encryption/decryption */
-				if (exf->reinit) {
-					if (crd->crd_flags & CRD_F_ENCRYPT) {
-						exf->encrypt(kschedule,
-						    blk, iv);
-					} else {
-						exf->decrypt(kschedule,
-						    blk, iv);
-					}
-				} else if (crd->crd_flags & CRD_F_ENCRYPT) {
-					/* XOR with previous block */
-					for (j = 0; j < blks; j++)
-						blk[j] ^= ivp[j];
-
-					exf->encrypt(kschedule, blk, iv);
-
-					/*
-					 * Keep encrypted block for XOR'ing
-					 * with next block
-					 */
-					bcopy(blk, iv, blks);
-					ivp = iv;
-				} else {	/* decrypt */
-					/*
-					 * Keep encrypted block for XOR'ing
-					 * with next block
-					 */
-					nivp = (ivp == iv) ? iv2 : iv;
-					bcopy(blk, nivp, blks);
-
-					exf->decrypt(kschedule, blk, iv);
-
-					/* XOR with previous block */
-					for (j = 0; j < blks; j++)
-						blk[j] ^= ivp[j];
-
-					ivp = nivp;
-				}
-
-				/* Copy back decrypted block */
-				cuio_copyback(uio, k, blks, blk);
-
-				/* Advance pointer */
-				iov = cuio_getptr(uio, k + blks, &k);
-				if (iov == NULL) {
-					error = EINVAL;
-					goto done;
-				}
-
-				i -= blks;
-
-				/* Could be done... */
-				if (i == 0)
-					break;
-			}
-
-			/*
-			 * Warning: idat may point to garbage here, but
-			 * we only use it in the while() loop, only if
-			 * there are indeed enough data.
-			 */
-			idat = (char *)iov->iov_base + k;
-
-			while (iov->iov_len >= k + blks && i > 0) {
-				if (exf->reinit) {
-					if (crd->crd_flags & CRD_F_ENCRYPT) {
-						exf->encrypt(kschedule,
-						    idat, iv);
-					} else {
-						exf->decrypt(kschedule,
-						    idat, iv);
-					}
-				} else if (crd->crd_flags & CRD_F_ENCRYPT) {
-					/* XOR with previous block/IV */
-					for (j = 0; j < blks; j++)
-						idat[j] ^= ivp[j];
-
-					exf->encrypt(kschedule, idat, iv);
-					ivp = idat;
-				} else {	/* decrypt */
-					/*
-					 * Keep encrypted block to be used
-					 * in next block's processing.
-					 */
-					nivp = (ivp == iv) ? iv2 : iv;
-					bcopy(idat, nivp, blks);
-
-					exf->decrypt(kschedule, idat, iv);
-
-					/* XOR with previous block/IV */
-					for (j = 0; j < blks; j++)
-						idat[j] ^= ivp[j];
-
-					ivp = nivp;
-				}
-
-				idat += blks;
-				k += blks;
-				i -= blks;
-			}
-			if (k == iov->iov_len) {
-				iov++;
-				k = 0;
-			}
-		}
-		error = 0;	/* Done with iovec encryption/decryption */
-	} else {
-		/*
-		 * contiguous buffer
-		 */
-		if (exf->reinit) {
-			for(i = crd->crd_skip;
-			    i < crd->crd_skip + crd->crd_len; i += blks) {
-				if (crd->crd_flags & CRD_F_ENCRYPT) {
-					exf->encrypt(kschedule, buf + i, iv);
-				} else {
-					exf->decrypt(kschedule, buf + i, iv);
-				}
-			}
-		} else if (crd->crd_flags & CRD_F_ENCRYPT) {
-			for (i = crd->crd_skip;
-			    i < crd->crd_skip + crd->crd_len; i += blks) {
-				/* XOR with the IV/previous block, as appropriate. */
-				if (i == crd->crd_skip)
-					for (k = 0; k < blks; k++)
-						buf[i + k] ^= ivp[k];
-				else
-					for (k = 0; k < blks; k++)
-						buf[i + k] ^= buf[i + k - blks];
-				exf->encrypt(kschedule, buf + i, iv);
-			}
-		} else {		/* Decrypt */
-			/*
-			 * Start at the end, so we don't need to keep the
-			 * encrypted block as the IV for the next block.
-			 */
-			for (i = crd->crd_skip + crd->crd_len - blks;
-			    i >= crd->crd_skip; i -= blks) {
-				exf->decrypt(kschedule, buf + i, iv);
-
-				/* XOR with the IV/previous block, as appropriate */
-				if (i == crd->crd_skip)
-					for (k = 0; k < blks; k++)
-						buf[i + k] ^= ivp[k];
-				else
-					for (k = 0; k < blks; k++)
-						buf[i + k] ^= buf[i + k - blks];
-			}
-		}
-		error = 0; /* Done w/contiguous buffer encrypt/decrypt */
-	}
-
-done:
-	/*
-	 * Cleanup - explicitly replace the session key if requested
-	 *	     (horrible semantics for concurrent operation)
-	 */
-	if (explicit_kschedule) {
-		okschedule = NULL;
-		spin_lock(&swcr_spin);
-		if (sw->sw_kschedule && sw->sw_kschedule_refs == 0) {
-			okschedule = sw->sw_kschedule;
-			sw->sw_kschedule = kschedule;
-		}
-		spin_unlock(&swcr_spin);
-		if (okschedule) {
-			bzero(okschedule, exf->ctxsize);
-			kfree(okschedule, M_CRYPTO_DATA);
-		}
-	} else {
-		spin_lock(&swcr_spin);
-		--sw->sw_kschedule_refs;
-		spin_unlock(&swcr_spin);
-	}
-
-out:
-	return error;
-}
-
-static void
-swcr_authprepare(struct auth_hash *axf, struct swcr_data *sw, u_char *key,
-    int klen)
-{
-	int k;
-
-	klen /= 8;
-
-	switch (axf->type) {
-	case CRYPTO_MD5_HMAC:
-	case CRYPTO_SHA1_HMAC:
-	case CRYPTO_SHA2_256_HMAC:
-	case CRYPTO_SHA2_384_HMAC:
-	case CRYPTO_SHA2_512_HMAC:
-	case CRYPTO_NULL_HMAC:
-	case CRYPTO_RIPEMD160_HMAC:
-		for (k = 0; k < klen; k++)
-			key[k] ^= HMAC_IPAD_VAL;
-
-		axf->Init(sw->sw_ictx);
-		axf->Update(sw->sw_ictx, key, klen);
-		axf->Update(sw->sw_ictx, hmac_ipad_buffer, axf->blocksize - klen);
-
-		for (k = 0; k < klen; k++)
-			key[k] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
-
-		axf->Init(sw->sw_octx);
-		axf->Update(sw->sw_octx, key, klen);
-		axf->Update(sw->sw_octx, hmac_opad_buffer, axf->blocksize - klen);
-
-		for (k = 0; k < klen; k++)
-			key[k] ^= HMAC_OPAD_VAL;
-		break;
-	case CRYPTO_MD5_KPDK:
-	case CRYPTO_SHA1_KPDK:
-	{
-		/* We need a buffer that can hold an md5 and a sha1 result. */
-		u_char buf[SHA1_RESULTLEN];
-
-		sw->sw_klen = klen;
-		bcopy(key, sw->sw_octx, klen);
-		axf->Init(sw->sw_ictx);
-		axf->Update(sw->sw_ictx, key, klen);
-		axf->Final(buf, sw->sw_ictx);
-		break;
-	}
-	default:
-		kprintf("%s: CRD_F_KEY_EXPLICIT flag given, but algorithm %d "
-		    "doesn't use keys.\n", __func__, axf->type);
-	}
-}
-
-/*
- * Compute keyed-hash authenticator.
- */
-static int
-swcr_authcompute(struct cryptodesc *crd, struct swcr_data *sw, caddr_t buf,
-    int flags)
-{
-	unsigned char aalg[HASH_MAX_LEN];
-	struct auth_hash *axf;
-	union authctx ctx;
-	int err;
-
-	if (sw->sw_ictx == NULL)
-		return EINVAL;
-
-	axf = sw->sw_axf;
-
-	if (crd->crd_flags & CRD_F_KEY_EXPLICIT)
-		swcr_authprepare(axf, sw, crd->crd_key, crd->crd_klen);
-
-	bcopy(sw->sw_ictx, &ctx, axf->ctxsize);
-
-	err = crypto_apply(flags, buf, crd->crd_skip, crd->crd_len,
-	    (int (*)(void *, void *, unsigned int))axf->Update, (caddr_t)&ctx);
-	if (err)
-		return err;
-
-	switch (sw->sw_alg) {
-	case CRYPTO_MD5_HMAC:
-	case CRYPTO_SHA1_HMAC:
-	case CRYPTO_SHA2_256_HMAC:
-	case CRYPTO_SHA2_384_HMAC:
-	case CRYPTO_SHA2_512_HMAC:
-	case CRYPTO_RIPEMD160_HMAC:
-		if (sw->sw_octx == NULL)
-			return EINVAL;
-
-		axf->Final(aalg, &ctx);
-		bcopy(sw->sw_octx, &ctx, axf->ctxsize);
-		axf->Update(&ctx, aalg, axf->hashsize);
-		axf->Final(aalg, &ctx);
-		break;
-
-	case CRYPTO_MD5_KPDK:
-	case CRYPTO_SHA1_KPDK:
-		if (sw->sw_octx == NULL)
-			return EINVAL;
-
-		axf->Update(&ctx, sw->sw_octx, sw->sw_klen);
-		axf->Final(aalg, &ctx);
-		break;
-
-	case CRYPTO_NULL_HMAC:
-		axf->Final(aalg, &ctx);
-		break;
-	}
-
-	/* Inject the authentication data */
-	crypto_copyback(flags, buf, crd->crd_inject,
-	    sw->sw_mlen == 0 ? axf->hashsize : sw->sw_mlen, aalg);
-	return 0;
-}
-
-/*
- * Apply a combined encryption-authentication transformation
- */
-static int
-swcr_combined(struct cryptop *crp)
-{
-	uint32_t blkbuf[howmany(EALG_MAX_BLOCK_LEN, sizeof(uint32_t))];
-	u_char *blk = (u_char *)blkbuf;
-	u_char aalg[HASH_MAX_LEN];
-	u_char iv[EALG_MAX_BLOCK_LEN];
-	uint8_t *kschedule;
-	union authctx ctx;
-	struct cryptodesc *crd, *crda = NULL, *crde = NULL;
-	struct swcr_data *sw, *swa, *swe;
-	struct auth_hash *axf = NULL;
-	struct enc_xform *exf = NULL;
-	caddr_t buf = (caddr_t)crp->crp_buf;
-	uint32_t *blkp;
-	int i, blksz, ivlen, len;
-
-	blksz = 0;
-	ivlen = 0;
-
-	for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-		for (sw = swcr_sessions[crp->crp_sid & 0xffffffff];
-		     sw && sw->sw_alg != crd->crd_alg;
-		     sw = sw->sw_next)
-			;
-		if (sw == NULL)
-			return (EINVAL);
-
-		switch (sw->sw_alg) {
-		case CRYPTO_AES_GCM_16:
-		case CRYPTO_AES_GMAC:
-			swe = sw;
-			crde = crd;
-			exf = swe->sw_exf;
-			ivlen = exf->ivsize;
-			break;
-		case CRYPTO_AES_128_GMAC:
-		case CRYPTO_AES_192_GMAC:
-		case CRYPTO_AES_256_GMAC:
-			swa = sw;
-			crda = crd;
-			axf = swa->sw_axf;
-			if (swa->sw_ictx == NULL)
-				return (EINVAL);
-			bcopy(swa->sw_ictx, &ctx, axf->ctxsize);
-			blksz = axf->blocksize;
-			break;
-		default:
-			return (EINVAL);
-		}
-	}
-	if (crde == NULL || crda == NULL)
-		return (EINVAL);
-
-	/* Initialize the IV */
-	if (crde->crd_flags & CRD_F_ENCRYPT) {
-		/* IV explicitly provided ? */
-		if (crde->crd_flags & CRD_F_IV_EXPLICIT)
-			bcopy(crde->crd_iv, iv, ivlen);
-		else
-			karc4random_buf(iv, ivlen);
-
-		/* Do we need to write the IV */
-		if (!(crde->crd_flags & CRD_F_IV_PRESENT))
-			crypto_copyback(crde->crd_flags, buf, crde->crd_inject,
-			    ivlen, iv);
-
-	} else {	/* Decryption */
-			/* IV explicitly provided ? */
-		if (crde->crd_flags & CRD_F_IV_EXPLICIT)
-			bcopy(crde->crd_iv, iv, ivlen);
-		else
-			/* Get IV off buf */
-			crypto_copydata(crde->crd_flags, buf, crde->crd_inject,
-			    ivlen, iv);
-	}
-
-	/* Supply MAC with IV */
-	if (axf->Reinit)
-		axf->Reinit(&ctx, iv, ivlen);
-
-	/* Supply MAC with AAD */
-	for (i = 0; i < crda->crd_len; i += blksz) {
-		len = MIN(crda->crd_len - i, blksz);
-		crypto_copydata(crde->crd_flags, buf, crda->crd_skip + i, len,
-		    blk);
-		axf->Update(&ctx, blk, len);
-	}
-
-	spin_lock(&swcr_spin);
-	kschedule = sw->sw_kschedule;
-	++sw->sw_kschedule_refs;
-	spin_unlock(&swcr_spin);
-
-	if (exf->reinit)
-		exf->reinit(kschedule, iv);
-
-	/* Do encryption/decryption with MAC */
-	for (i = 0; i < crde->crd_len; i += blksz) {
-		len = MIN(crde->crd_len - i, blksz);
-		if (len < blksz)
-			bzero(blk, blksz);
-		crypto_copydata(crde->crd_flags, buf, crde->crd_skip + i, len,
-		    blk);
-		if (crde->crd_flags & CRD_F_ENCRYPT) {
-			exf->encrypt(kschedule, blk, iv);
-			axf->Update(&ctx, blk, len);
-		} else {
-			axf->Update(&ctx, blk, len);
-			exf->decrypt(kschedule, blk, iv);
-		}
-		crypto_copyback(crde->crd_flags, buf, crde->crd_skip + i, len,
-		    blk);
-	}
-
-	/* Do any required special finalization */
-	switch (crda->crd_alg) {
-		case CRYPTO_AES_128_GMAC:
-		case CRYPTO_AES_192_GMAC:
-		case CRYPTO_AES_256_GMAC:
-			/* length block */
-			bzero(blk, blksz);
-			blkp = (uint32_t *)blk + 1;
-			*blkp = htobe32(crda->crd_len * 8);
-			blkp = (uint32_t *)blk + 3;
-			*blkp = htobe32(crde->crd_len * 8);
-			axf->Update(&ctx, blk, blksz);
-			break;
-	}
-
-	/* Finalize MAC */
-	axf->Final(aalg, &ctx);
-
-	/* Inject the authentication data */
-	crypto_copyback(crda->crd_flags, crp->crp_buf, crda->crd_inject,
-	    axf->blocksize, aalg);
-
-	spin_lock(&swcr_spin);
-	--sw->sw_kschedule_refs;
-	spin_unlock(&swcr_spin);
-
-	return (0);
-}
-
-/*
- * Apply a compression/decompression algorithm
- */
-static int
-swcr_compdec(struct cryptodesc *crd, struct swcr_data *sw,
-	     caddr_t buf, int flags)
-{
-	u_int8_t *data, *out;
-	struct comp_algo *cxf;
-	int adj;
-	u_int32_t result;
-
-	cxf = sw->sw_cxf;
-
-	/*
-	 * We must handle the whole buffer of data in one time
-	 * then if there is not all the data in the mbuf, we must
-	 * copy in a buffer.
-	 */
-	data = kmalloc(crd->crd_len, M_CRYPTO_DATA, M_INTWAIT);
-	crypto_copydata(flags, buf, crd->crd_skip, crd->crd_len, data);
-
-	if (crd->crd_flags & CRD_F_COMP)
-		result = cxf->compress(data, crd->crd_len, &out);
-	else
-		result = cxf->decompress(data, crd->crd_len, &out);
-
-	kfree(data, M_CRYPTO_DATA);
-	if (result == 0)
-		return EINVAL;
-
-	sw->sw_size = result;
-	/* Check the compressed size when doing compression */
-	if (crd->crd_flags & CRD_F_COMP) {
-		if (result >= crd->crd_len) {
-			/* Compression was useless, we lost time */
-			kfree(out, M_CRYPTO_DATA);
-			return 0;
-		}
-	}
-
-	/*
-	 * Copy back the (de)compressed data.
-	 * If CRYPTO_F_IMBUF, the mbuf will be extended as necessary.
-	 */
-	crypto_copyback(flags, buf, crd->crd_skip, result, out);
-	if (result < crd->crd_len) {
-		adj = result - crd->crd_len;
-		if (flags & CRYPTO_F_IMBUF) {
-			adj = result - crd->crd_len;
-			m_adj((struct mbuf *)buf, adj);
-		} else if (flags & CRYPTO_F_IOV) {
-			struct uio *uio = (struct uio *)buf;
-			int ind;
-
-			adj = crd->crd_len - result;
-			ind = uio->uio_iovcnt - 1;
-
-			while (adj > 0 && ind >= 0) {
-				if (adj < uio->uio_iov[ind].iov_len) {
-					uio->uio_iov[ind].iov_len -= adj;
-					break;
-				}
-
-				adj -= uio->uio_iov[ind].iov_len;
-				uio->uio_iov[ind].iov_len = 0;
-				ind--;
-				uio->uio_iovcnt--;
-			}
-		}
-	}
-	kfree(out, M_CRYPTO_DATA);
-	return 0;
-}
-
-/*
- * Generate a new software session.
- */
-static int
-swcr_newsession(device_t dev, u_int32_t *sid, struct cryptoini *cri)
-{
-	struct swcr_data *swd_base;
-	struct swcr_data **swd;
-	struct swcr_data **oswd;
-	struct auth_hash *axf;
-	struct enc_xform *txf;
-	struct comp_algo *cxf;
-	u_int32_t i;
-	u_int32_t n;
-	int error;
-
-	if (sid == NULL || cri == NULL)
-		return EINVAL;
-
-	swd_base = NULL;
-	swd = &swd_base;
-
-	while (cri) {
-		*swd = kmalloc(sizeof(struct swcr_data),
-			       M_CRYPTO_DATA, M_WAITOK | M_ZERO);
-
-		switch (cri->cri_alg) {
-		case CRYPTO_DES_CBC:
-			txf = &enc_xform_des;
-			goto enccommon;
-		case CRYPTO_3DES_CBC:
-			txf = &enc_xform_3des;
-			goto enccommon;
-		case CRYPTO_BLF_CBC:
-			txf = &enc_xform_blf;
-			goto enccommon;
-		case CRYPTO_CAST_CBC:
-			txf = &enc_xform_cast5;
-			goto enccommon;
-		case CRYPTO_SKIPJACK_CBC:
-			txf = &enc_xform_skipjack;
-			goto enccommon;
-		case CRYPTO_RIJNDAEL128_CBC:
-			txf = &enc_xform_rijndael128;
-			goto enccommon;
-		case CRYPTO_AES_XTS:
-			txf = &enc_xform_aes_xts;
-			goto enccommon;
-		case CRYPTO_AES_CTR:
-			txf = &enc_xform_aes_ctr;
-			goto enccommon;
-		case CRYPTO_AES_GCM_16:
-			txf = &enc_xform_aes_gcm;
-			goto enccommon;
-		case CRYPTO_AES_GMAC:
-			txf = &enc_xform_aes_gmac;
-			(*swd)->sw_exf = txf;
-			break;
-		case CRYPTO_CAMELLIA_CBC:
-			txf = &enc_xform_camellia;
-			goto enccommon;
-		case CRYPTO_TWOFISH_CBC:
-			txf = &enc_xform_twofish;
-			goto enccommon;
-		case CRYPTO_SERPENT_CBC:
-			txf = &enc_xform_serpent;
-			goto enccommon;
-		case CRYPTO_TWOFISH_XTS:
-			txf = &enc_xform_twofish_xts;
-			goto enccommon;
-		case CRYPTO_SERPENT_XTS:
-			txf = &enc_xform_serpent_xts;
-			goto enccommon;
-		case CRYPTO_NULL_CBC:
-			txf = &enc_xform_null;
-			goto enccommon;
-		enccommon:
-			KKASSERT(txf->ctxsize > 0);
-			(*swd)->sw_kschedule = kmalloc(txf->ctxsize,
-						       M_CRYPTO_DATA,
-						       M_WAITOK | M_ZERO);
-			if (cri->cri_key != NULL) {
-				error = txf->setkey((*swd)->sw_kschedule,
-						    cri->cri_key,
-						    cri->cri_klen / 8);
-				if (error) {
-					swcr_freesession_slot(&swd_base, 0);
-					return error;
-				}
-			}
-			(*swd)->sw_exf = txf;
-			break;
-
-		case CRYPTO_MD5_HMAC:
-			axf = &auth_hash_hmac_md5;
-			goto authcommon;
-		case CRYPTO_SHA1_HMAC:
-			axf = &auth_hash_hmac_sha1;
-			goto authcommon;
-		case CRYPTO_SHA2_256_HMAC:
-			axf = &auth_hash_hmac_sha2_256;
-			goto authcommon;
-		case CRYPTO_SHA2_384_HMAC:
-			axf = &auth_hash_hmac_sha2_384;
-			goto authcommon;
-		case CRYPTO_SHA2_512_HMAC:
-			axf = &auth_hash_hmac_sha2_512;
-			goto authcommon;
-		case CRYPTO_NULL_HMAC:
-			axf = &auth_hash_null;
-			goto authcommon;
-		case CRYPTO_RIPEMD160_HMAC:
-			axf = &auth_hash_hmac_ripemd_160;
-		authcommon:
-			(*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
-						  M_WAITOK);
-			(*swd)->sw_octx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
-						  M_WAITOK);
-
-			if (cri->cri_key != NULL) {
-				swcr_authprepare(axf, *swd, cri->cri_key,
-				    cri->cri_klen);
-			}
-
-			(*swd)->sw_mlen = cri->cri_mlen;
-			(*swd)->sw_axf = axf;
-			break;
-
-		case CRYPTO_MD5_KPDK:
-			axf = &auth_hash_key_md5;
-			goto auth2common;
-
-		case CRYPTO_SHA1_KPDK:
-			axf = &auth_hash_key_sha1;
-		auth2common:
-			(*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
-						  M_WAITOK);
-			(*swd)->sw_octx = kmalloc(cri->cri_klen / 8,
-						  M_CRYPTO_DATA, M_WAITOK);
-
-			/* Store the key so we can "append" it to the payload */
-			if (cri->cri_key != NULL) {
-				swcr_authprepare(axf, *swd, cri->cri_key,
-						 cri->cri_klen);
-			}
-
-			(*swd)->sw_mlen = cri->cri_mlen;
-			(*swd)->sw_axf = axf;
-			break;
-#ifdef notdef
-		case CRYPTO_MD5:
-			axf = &auth_hash_md5;
-			goto auth3common;
-
-		case CRYPTO_SHA1:
-			axf = &auth_hash_sha1;
-		auth3common:
-			(*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
-						  M_WAITOK);
-
-			axf->Init((*swd)->sw_ictx);
-			(*swd)->sw_mlen = cri->cri_mlen;
-			(*swd)->sw_axf = axf;
-			break;
-#endif
-		case CRYPTO_AES_128_GMAC:
-			axf = &auth_hash_gmac_aes_128;
-			goto auth4common;
-
-		case CRYPTO_AES_192_GMAC:
-			axf = &auth_hash_gmac_aes_192;
-			goto auth4common;
-
-		case CRYPTO_AES_256_GMAC:
-			axf = &auth_hash_gmac_aes_256;
-		auth4common:
-			(*swd)->sw_ictx = kmalloc(axf->ctxsize, M_CRYPTO_DATA,
-						  M_NOWAIT);
-			if ((*swd)->sw_ictx == NULL) {
-				swcr_freesession_slot(&swd_base, 0);
-				return ENOBUFS;
-			}
-
-			axf->Init((*swd)->sw_ictx);
-			error = axf->Setkey((*swd)->sw_ictx, cri->cri_key,
-					    cri->cri_klen / 8);
-			if (error) {
-				swcr_freesession_slot(&swd_base, 0);
-				return error;
-			}
-			(*swd)->sw_axf = axf;
-			break;
-
-		case CRYPTO_DEFLATE_COMP:
-			cxf = &comp_algo_deflate;
-			(*swd)->sw_cxf = cxf;
-			break;
-		default:
-			swcr_freesession_slot(&swd_base, 0);
-			return EINVAL;
-		}
-
-		(*swd)->sw_alg = cri->cri_alg;
-		cri = cri->cri_next;
-		swd = &((*swd)->sw_next);
-	}
-
-	for (;;) {
-		/*
-		 * Atomically allocate a session
-		 */
-		spin_lock(&swcr_spin);
-		for (i = swcr_minsesnum; i < swcr_sesnum; ++i) {
-			if (swcr_sessions[i] == NULL)
-				break;
-		}
-		if (i < swcr_sesnum) {
-			swcr_sessions[i] = swd_base;
-			swcr_minsesnum = i + 1;
-			spin_unlock(&swcr_spin);
-			break;
-		}
-		n = swcr_sesnum;
-		spin_unlock(&swcr_spin);
-
-		/*
-		 * A larger allocation is required, reallocate the array
-		 * and replace, checking for SMP races.
-		 */
-		if (n < CRYPTO_SW_SESSIONS)
-			n = CRYPTO_SW_SESSIONS;
-		else
-			n = n * 3 / 2;
-		swd = kmalloc(n * sizeof(struct swcr_data *),
-			      M_CRYPTO_DATA, M_WAITOK | M_ZERO);
-
-		spin_lock(&swcr_spin);
-		if (swcr_sesnum >= n) {
-			spin_unlock(&swcr_spin);
-			kfree(swd, M_CRYPTO_DATA);
-		} else if (swcr_sesnum) {
-			bcopy(swcr_sessions, swd,
-			      swcr_sesnum * sizeof(struct swcr_data *));
-			oswd = swcr_sessions;
-			swcr_sessions = swd;
-			swcr_sesnum = n;
-			spin_unlock(&swcr_spin);
-			kfree(oswd, M_CRYPTO_DATA);
-		} else {
-			swcr_sessions = swd;
-			swcr_sesnum = n;
-			spin_unlock(&swcr_spin);
-		}
-	}
-
-	*sid = i;
-	return 0;
-}
-
-/*
- * Free a session.
- */
-static int
-swcr_freesession(device_t dev, u_int64_t tid)
-{
-	u_int32_t sid = CRYPTO_SESID2LID(tid);
-
-	if (sid > swcr_sesnum || swcr_sessions == NULL ||
-	    swcr_sessions[sid] == NULL) {
-		return EINVAL;
-	}
-
-	/* Silently accept and return */
-	if (sid == 0)
-		return 0;
-
-	return(swcr_freesession_slot(&swcr_sessions[sid], sid));
-}
-
-static
-int
-swcr_freesession_slot(struct swcr_data **swdp, u_int32_t sid)
-{
-	struct enc_xform *txf;
-	struct auth_hash *axf;
-	struct swcr_data *swd;
-	struct swcr_data *swnext;
-
-	/*
-	 * Protect session detachment with the spinlock.
-	 */
-	spin_lock(&swcr_spin);
-	swnext = *swdp;
-	*swdp = NULL;
-	if (sid && swcr_minsesnum > sid)
-		swcr_minsesnum = sid;
-	spin_unlock(&swcr_spin);
-
-	/*
-	 * Clean up at our leisure.
-	 */
-	while ((swd = swnext) != NULL) {
-		swnext = swd->sw_next;
-
-		swd->sw_next = NULL;
-
-		switch (swd->sw_alg) {
-		case CRYPTO_DES_CBC:
-		case CRYPTO_3DES_CBC:
-		case CRYPTO_BLF_CBC:
-		case CRYPTO_CAST_CBC:
-		case CRYPTO_SKIPJACK_CBC:
-		case CRYPTO_RIJNDAEL128_CBC:
-		case CRYPTO_AES_XTS:
-		case CRYPTO_AES_CTR:
-		case CRYPTO_AES_GCM_16:
-		case CRYPTO_AES_GMAC:
-		case CRYPTO_CAMELLIA_CBC:
-		case CRYPTO_TWOFISH_CBC:
-		case CRYPTO_SERPENT_CBC:
-		case CRYPTO_TWOFISH_XTS:
-		case CRYPTO_SERPENT_XTS:
-		case CRYPTO_NULL_CBC:
-			txf = swd->sw_exf;
-
-			if (swd->sw_kschedule) {
-				bzero(swd->sw_kschedule, txf->ctxsize);
-				kfree(swd->sw_kschedule, M_CRYPTO_DATA);
-			}
-			break;
-
-		case CRYPTO_MD5_HMAC:
-		case CRYPTO_SHA1_HMAC:
-		case CRYPTO_SHA2_256_HMAC:
-		case CRYPTO_SHA2_384_HMAC:
-		case CRYPTO_SHA2_512_HMAC:
-		case CRYPTO_RIPEMD160_HMAC:
-		case CRYPTO_NULL_HMAC:
-			axf = swd->sw_axf;
-
-			if (swd->sw_ictx) {
-				bzero(swd->sw_ictx, axf->ctxsize);
-				kfree(swd->sw_ictx, M_CRYPTO_DATA);
-			}
-			if (swd->sw_octx) {
-				bzero(swd->sw_octx, axf->ctxsize);
-				kfree(swd->sw_octx, M_CRYPTO_DATA);
-			}
-			break;
-
-		case CRYPTO_MD5_KPDK:
-		case CRYPTO_SHA1_KPDK:
-			axf = swd->sw_axf;
-
-			if (swd->sw_ictx) {
-				bzero(swd->sw_ictx, axf->ctxsize);
-				kfree(swd->sw_ictx, M_CRYPTO_DATA);
-			}
-			if (swd->sw_octx) {
-				bzero(swd->sw_octx, swd->sw_klen);
-				kfree(swd->sw_octx, M_CRYPTO_DATA);
-			}
-			break;
-
-		case CRYPTO_AES_128_GMAC:
-		case CRYPTO_AES_192_GMAC:
-		case CRYPTO_AES_256_GMAC:
-		case CRYPTO_MD5:
-		case CRYPTO_SHA1:
-			axf = swd->sw_axf;
-
-			if (swd->sw_ictx) {
-				bzero(swd->sw_ictx, axf->ctxsize);
-				kfree(swd->sw_ictx, M_CRYPTO_DATA);
-			}
-			break;
-
-		case CRYPTO_DEFLATE_COMP:
-			break;
-		}
-
-		//FREE(swd, M_CRYPTO_DATA);
-		kfree(swd, M_CRYPTO_DATA);
-	}
-	return 0;
-}
-
-/*
- * Process a software request.
- */
-static int
-swcr_process(device_t dev, struct cryptop *crp, int hint)
-{
-	struct cryptodesc *crd;
-	struct swcr_data *sw;
-	u_int32_t lid;
-
-	/* Sanity check */
-	if (crp == NULL)
-		return EINVAL;
-
-	if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
-		crp->crp_etype = EINVAL;
-		goto done;
-	}
-
-	lid = crp->crp_sid & 0xffffffff;
-	if (lid >= swcr_sesnum || lid == 0 || swcr_sessions[lid] == NULL) {
-		crp->crp_etype = ENOENT;
-		goto done;
-	}
-
-	/* Go through crypto descriptors, processing as we go */
-	for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
-		/*
-		 * Find the crypto context.
-		 *
-		 * XXX Note that the logic here prevents us from having
-		 * XXX the same algorithm multiple times in a session
-		 * XXX (or rather, we can but it won't give us the right
-		 * XXX results). To do that, we'd need some way of differentiating
-		 * XXX between the various instances of an algorithm (so we can
-		 * XXX locate the correct crypto context).
-		 */
-		for (sw = swcr_sessions[lid];
-		    sw && sw->sw_alg != crd->crd_alg;
-		    sw = sw->sw_next)
-			;
-
-		/* No such context ? */
-		if (sw == NULL) {
-			crp->crp_etype = EINVAL;
-			goto done;
-		}
-		switch (sw->sw_alg) {
-		case CRYPTO_DES_CBC:
-		case CRYPTO_3DES_CBC:
-		case CRYPTO_BLF_CBC:
-		case CRYPTO_CAST_CBC:
-		case CRYPTO_SKIPJACK_CBC:
-		case CRYPTO_RIJNDAEL128_CBC:
-		case CRYPTO_AES_XTS:
-		case CRYPTO_AES_CTR:
-		case CRYPTO_CAMELLIA_CBC:
-		case CRYPTO_TWOFISH_CBC:
-		case CRYPTO_SERPENT_CBC:
-		case CRYPTO_TWOFISH_XTS:
-		case CRYPTO_SERPENT_XTS:
-			if ((crp->crp_etype = swcr_encdec(crd, sw,
-			    crp->crp_buf, crp->crp_flags)) != 0)
-				goto done;
-			break;
-		case CRYPTO_NULL_CBC:
-			crp->crp_etype = 0;
-			break;
-		case CRYPTO_MD5_HMAC:
-		case CRYPTO_SHA1_HMAC:
-		case CRYPTO_SHA2_256_HMAC:
-		case CRYPTO_SHA2_384_HMAC:
-		case CRYPTO_SHA2_512_HMAC:
-		case CRYPTO_RIPEMD160_HMAC:
-		case CRYPTO_NULL_HMAC:
-		case CRYPTO_MD5_KPDK:
-		case CRYPTO_SHA1_KPDK:
-		case CRYPTO_MD5:
-		case CRYPTO_SHA1:
-			if ((crp->crp_etype = swcr_authcompute(crd, sw,
-			    crp->crp_buf, crp->crp_flags)) != 0)
-				goto done;
-			break;
-
-		case CRYPTO_AES_GCM_16:
-		case CRYPTO_AES_GMAC:
-		case CRYPTO_AES_128_GMAC:
-		case CRYPTO_AES_192_GMAC:
-		case CRYPTO_AES_256_GMAC:
-			crp->crp_etype = swcr_combined(crp);
-			goto done;
-
-		case CRYPTO_DEFLATE_COMP:
-			if ((crp->crp_etype = swcr_compdec(crd, sw,
-			    crp->crp_buf, crp->crp_flags)) != 0)
-				goto done;
-			else
-				crp->crp_olen = (int)sw->sw_size;
-			break;
-
-		default:
-			/* Unknown/unsupported algorithm */
-			crp->crp_etype = EINVAL;
-			goto done;
-		}
-	}
-
-done:
-	crypto_done(crp);
-	lwkt_yield();
-	return 0;
-}
-
-static void
-swcr_identify(driver_t *drv, device_t parent)
-{
-	/* NB: order 10 is so we get attached after h/w devices */
-	/* XXX: wouldn't bet about this BUS_ADD_CHILD correctness */
-	if (device_find_child(parent, "cryptosoft", -1) == NULL &&
-	    BUS_ADD_CHILD(parent, parent, 10, "cryptosoft", -1) == 0)
-		panic("cryptosoft: could not attach");
-}
-
-static int
-swcr_probe(device_t dev)
-{
-	device_set_desc(dev, "software crypto");
-	return (0);
-}
-
-static int
-swcr_attach(device_t dev)
-{
-	memset(hmac_ipad_buffer, HMAC_IPAD_VAL, HMAC_MAX_BLOCK_LEN);
-	memset(hmac_opad_buffer, HMAC_OPAD_VAL, HMAC_MAX_BLOCK_LEN);
-
-	swcr_id = crypto_get_driverid(dev, CRYPTOCAP_F_SOFTWARE |
-					   CRYPTOCAP_F_SYNC |
-					   CRYPTOCAP_F_SMP);
-	if (swcr_id < 0) {
-		device_printf(dev, "cannot initialize!");
-		return ENOMEM;
-	}
-#define	REGISTER(alg) \
-	crypto_register(swcr_id, alg, 0,0)
-	REGISTER(CRYPTO_DES_CBC);
-	REGISTER(CRYPTO_3DES_CBC);
-	REGISTER(CRYPTO_BLF_CBC);
-	REGISTER(CRYPTO_CAST_CBC);
-	REGISTER(CRYPTO_SKIPJACK_CBC);
-	REGISTER(CRYPTO_NULL_CBC);
-	REGISTER(CRYPTO_MD5_HMAC);
-	REGISTER(CRYPTO_SHA1_HMAC);
-	REGISTER(CRYPTO_SHA2_256_HMAC);
-	REGISTER(CRYPTO_SHA2_384_HMAC);
-	REGISTER(CRYPTO_SHA2_512_HMAC);
-	REGISTER(CRYPTO_RIPEMD160_HMAC);
-	REGISTER(CRYPTO_NULL_HMAC);
-	REGISTER(CRYPTO_MD5_KPDK);
-	REGISTER(CRYPTO_SHA1_KPDK);
-	REGISTER(CRYPTO_MD5);
-	REGISTER(CRYPTO_SHA1);
-	REGISTER(CRYPTO_RIJNDAEL128_CBC);
-	REGISTER(CRYPTO_AES_XTS);
-	REGISTER(CRYPTO_AES_CTR);
-	REGISTER(CRYPTO_AES_GCM_16);
-	REGISTER(CRYPTO_AES_GMAC);
-	REGISTER(CRYPTO_AES_128_GMAC);
-	REGISTER(CRYPTO_AES_192_GMAC);
-	REGISTER(CRYPTO_AES_256_GMAC);
-	REGISTER(CRYPTO_CAMELLIA_CBC);
-	REGISTER(CRYPTO_TWOFISH_CBC);
-	REGISTER(CRYPTO_SERPENT_CBC);
-	REGISTER(CRYPTO_TWOFISH_XTS);
-	REGISTER(CRYPTO_SERPENT_XTS);
-	REGISTER(CRYPTO_DEFLATE_COMP);
-#undef REGISTER
-
-	return 0;
-}
-
-static int
-swcr_detach(device_t dev)
-{
-	crypto_unregister_all(swcr_id);
-	if (swcr_sessions != NULL)
-		kfree(swcr_sessions, M_CRYPTO_DATA);
-	return 0;
-}
-
-static device_method_t swcr_methods[] = {
-	DEVMETHOD(device_identify,	swcr_identify),
-	DEVMETHOD(device_probe,		swcr_probe),
-	DEVMETHOD(device_attach,	swcr_attach),
-	DEVMETHOD(device_detach,	swcr_detach),
-
-	DEVMETHOD(cryptodev_newsession,	swcr_newsession),
-	DEVMETHOD(cryptodev_freesession,swcr_freesession),
-	DEVMETHOD(cryptodev_process,	swcr_process),
-
-	DEVMETHOD_END
-};
-
-static driver_t swcr_driver = {
-	"cryptosoft",
-	swcr_methods,
-	0,		/* NB: no softc */
-};
-static devclass_t swcr_devclass;
-
-/*
- * NB: We explicitly reference the crypto module so we
- * get the necessary ordering when built as a loadable
- * module.  This is required because we bundle the crypto
- * module code together with the cryptosoft driver (otherwise
- * normal module dependencies would handle things).
- */
-extern int crypto_modevent(struct module *, int, void *);
-/* XXX where to attach */
-DRIVER_MODULE(cryptosoft, nexus, swcr_driver, swcr_devclass, crypto_modevent,NULL);
-MODULE_VERSION(cryptosoft, 1);
-MODULE_DEPEND(cryptosoft, crypto, 1, 1, 1);
diff --git a/sys/opencrypto/cryptosoft.h b/sys/opencrypto/cryptosoft.h
deleted file mode 100644
index f12ac703ff..0000000000
--- a/sys/opencrypto/cryptosoft.h
+++ /dev/null
@@ -1,69 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/cryptosoft.h,v 1.4 2007/03/21 03:42:51 sam Exp $	*/
-/*	$OpenBSD: cryptosoft.h,v 1.10 2002/04/22 23:10:09 deraadt Exp $	*/
-
-/*-
- * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
- *
- * This code was written by Angelos D. Keromytis in Athens, Greece, in
- * February 2000. Network Security Technologies Inc. (NSTI) kindly
- * supported the development of this code.
- *
- * Copyright (c) 2000 Angelos D. Keromytis
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all source code copies of any software which is or includes a copy or
- * modification of this software.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- */
-
-#ifndef _CRYPTO_CRYPTOSOFT_H_
-#define _CRYPTO_CRYPTOSOFT_H_
-
-/* Software session entry */
-struct swcr_data {
-	int		sw_alg;		/* Algorithm */
-	union {
-		struct {
-			u_int8_t	 *SW_ictx;
-			u_int8_t	 *SW_octx;
-			u_int16_t	 SW_klen;
-			u_int16_t	 SW_mlen;
-			struct auth_hash *SW_axf;
-		} SWCR_AUTH;
-		struct {
-			int		 SW_kschedule_refs;
-			u_int8_t	 *SW_kschedule;
-			struct enc_xform *SW_exf;
-		} SWCR_ENC;
-		struct {
-			u_int32_t	 SW_size;
-			struct comp_algo *SW_cxf;
-		} SWCR_COMP;
-	} SWCR_UN;
-
-#define sw_ictx		SWCR_UN.SWCR_AUTH.SW_ictx
-#define sw_octx		SWCR_UN.SWCR_AUTH.SW_octx
-#define sw_klen		SWCR_UN.SWCR_AUTH.SW_klen
-#define sw_mlen		SWCR_UN.SWCR_AUTH.SW_mlen
-#define sw_axf		SWCR_UN.SWCR_AUTH.SW_axf
-#define sw_kschedule	SWCR_UN.SWCR_ENC.SW_kschedule
-#define sw_kschedule_refs SWCR_UN.SWCR_ENC.SW_kschedule_refs
-#define sw_exf		SWCR_UN.SWCR_ENC.SW_exf
-#define sw_size		SWCR_UN.SWCR_COMP.SW_size
-#define sw_cxf		SWCR_UN.SWCR_COMP.SW_cxf
-
-	struct swcr_data *sw_next;
-};
-
-#ifdef _KERNEL
-extern u_int8_t hmac_ipad_buffer[];
-extern u_int8_t hmac_opad_buffer[];
-#endif /* _KERNEL */
-
-#endif /* _CRYPTO_CRYPTO_H_ */
diff --git a/sys/opencrypto/deflate.c b/sys/opencrypto/deflate.c
deleted file mode 100644
index 0b746233d5..0000000000
--- a/sys/opencrypto/deflate.c
+++ /dev/null
@@ -1,174 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/deflate.c,v 1.5 2008/10/23 15:53:51 des Exp $	*/
-/* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
-
-/*-
- * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- */
-
-/*
- * This file contains a wrapper around the deflate algo compression
- * functions using the zlib library (see net/zlib.{c,h})
- */
-
-#include <sys/types.h>
-#include <sys/param.h>
-#include <sys/malloc.h>
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <net/zlib.h>
-
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/deflate.h>
-
-int window_inflate = -1 * MAX_WBITS;
-int window_deflate = -12;
-
-/*
- * This function takes a block of data and (de)compress it using the deflate
- * algorithm
- */
-
-u_int32_t
-deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out)
-{
-	/* decomp indicates whether we compress (0) or decompress (1) */
-
-	z_stream zbuf;
-	u_int8_t *output;
-	u_int32_t count, result;
-	int error, i = 0, j;
-	struct deflate_buf buf[ZBUF];
-
-	bzero(&zbuf, sizeof(z_stream));
-	for (j = 0; j < ZBUF; j++)
-		buf[j].flag = 0;
-
-	zbuf.next_in = data;	/* data that is going to be processed */
-	zbuf.zalloc = z_alloc;
-	zbuf.zfree = z_free;
-	zbuf.opaque = Z_NULL;
-	zbuf.avail_in = size;	/* Total length of data to be processed */
-
-	if (!decomp) {
-		buf[i].out = kmalloc((u_long) size, M_CRYPTO_DATA, M_WAITOK);
-		buf[i].size = size;
-		buf[i].flag = 1;
-		i++;
-	} else {
-		/*
-	 	 * Choose a buffer with 4x the size of the input buffer
-	 	 * for the size of the output buffer in the case of
-	 	 * decompression. If it's not sufficient, it will need to be
-	 	 * updated while the decompression is going on
-	 	 */
-
-		buf[i].out = kmalloc((u_long) (size * 4),
-				     M_CRYPTO_DATA, M_WAITOK);
-		buf[i].size = size * 4;
-		buf[i].flag = 1;
-		i++;
-	}
-
-	zbuf.next_out = buf[0].out;
-	zbuf.avail_out = buf[0].size;
-
-	error = decomp ? inflateInit2(&zbuf, window_inflate) :
-	    deflateInit2(&zbuf, Z_DEFAULT_COMPRESSION, Z_METHOD,
-		    window_deflate, Z_MEMLEVEL, Z_DEFAULT_STRATEGY);
-
-	if (error != Z_OK)
-		goto bad;
-	for (;;) {
-		error = decomp ? inflate(&zbuf, Z_PARTIAL_FLUSH) :
-				 deflate(&zbuf, Z_PARTIAL_FLUSH);
-		if (error != Z_OK && error != Z_STREAM_END)
-			goto bad;
-		else if (zbuf.avail_in == 0 && zbuf.avail_out != 0)
-			goto end;
-		else if (zbuf.avail_out == 0 && i < (ZBUF - 1)) {
-			/* we need more output space, allocate size */
-			buf[i].out = kmalloc((u_long) size,
-					     M_CRYPTO_DATA, M_WAITOK);
-			zbuf.next_out = buf[i].out;
-			buf[i].size = size;
-			buf[i].flag = 1;
-			zbuf.avail_out = buf[i].size;
-			i++;
-		} else
-			goto bad;
-	}
-
-end:
-	result = count = zbuf.total_out;
-
-	*out = kmalloc((u_long) result, M_CRYPTO_DATA, M_WAITOK);
-	if (decomp)
-		inflateEnd(&zbuf);
-	else
-		deflateEnd(&zbuf);
-	output = *out;
-	for (j = 0; buf[j].flag != 0; j++) {
-		if (count > buf[j].size) {
-			bcopy(buf[j].out, *out, buf[j].size);
-			*out += buf[j].size;
-			kfree(buf[j].out, M_CRYPTO_DATA);
-			count -= buf[j].size;
-		} else {
-			/* it should be the last buffer */
-			bcopy(buf[j].out, *out, count);
-			*out += count;
-			kfree(buf[j].out, M_CRYPTO_DATA);
-			count = 0;
-		}
-	}
-	*out = output;
-	return result;
-
-bad:
-	*out = NULL;
-	for (j = 0; buf[j].flag != 0; j++)
-		kfree(buf[j].out, M_CRYPTO_DATA);
-	if (decomp)
-		inflateEnd(&zbuf);
-	else
-		deflateEnd(&zbuf);
-	return 0;
-}
-
-void *
-z_alloc(void *nil, u_int type, u_int size)
-{
-	void *ptr;
-
-	ptr = kmalloc(type *size, M_CRYPTO_DATA, M_WAITOK);
-	return ptr;
-}
-
-void
-z_free(void *nil, void *ptr)
-{
-	kfree(ptr, M_CRYPTO_DATA);
-}
diff --git a/sys/opencrypto/deflate.h b/sys/opencrypto/deflate.h
deleted file mode 100644
index 7976b1afb8..0000000000
--- a/sys/opencrypto/deflate.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/deflate.h,v 1.2 2005/01/07 02:29:16 imp Exp $	*/
-/* $OpenBSD: deflate.h,v 1.3 2002/03/14 01:26:51 millert Exp $ */
-
-/*-
- * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
- *
- * 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.
- * 3. The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
- */
-
-/*
- * Definition for the wrapper around the deflate compression 
- * algorithm used in /sys/crypto
- */
-
-#ifndef _CRYPTO_DEFLATE_H_
-#define _CRYPTO_DEFLATE_H_
-
-#include <net/zlib.h>
-
-#define Z_METHOD	8
-#define Z_MEMLEVEL	8
-#define MINCOMP		2	/* won't be used, but must be defined */
-#define ZBUF		10
-
-u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **);
-void *z_alloc(void *, u_int, u_int);
-void z_free(void *, void *);
-
-struct deflate_buf {
-	u_int8_t *out;
-	u_int32_t size;
-	int flag;
-};
-
-#endif /* _CRYPTO_DEFLATE_H_ */
diff --git a/sys/opencrypto/gmac.c b/sys/opencrypto/gmac.c
deleted file mode 100644
index a8008ae5d6..0000000000
--- a/sys/opencrypto/gmac.c
+++ /dev/null
@@ -1,159 +0,0 @@
-/*	$OpenBSD: gmac.c,v 1.3 2011/01/11 15:44:23 deraadt Exp $	*/
-
-/*
- * Copyright (c) 2010 Mike Belopuhov <mike@vantronix.net>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-/*
- * This code implements the Message Authentication part of the
- * Galois/Counter Mode (as being described in the RFC 4543) using
- * the AES cipher.  FIPS SP 800-38D describes the algorithm details.
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/endian.h>
-
-#include <crypto/rijndael/rijndael.h>
-#include <opencrypto/gmac.h>
-
-void	ghash_gfmul(uint32_t *, uint32_t *, uint32_t *);
-void	ghash_update(GHASH_CTX *, uint8_t *, size_t);
-
-/* Computes a block multiplication in the GF(2^128) */
-void
-ghash_gfmul(uint32_t *X, uint32_t *Y, uint32_t *product)
-{
-	uint32_t	v[4];
-	uint32_t	z[4] = { 0, 0, 0, 0};
-	uint8_t		*x = (uint8_t *)X;
-	uint32_t	mul;
-	int		i;
-
-	v[0] = be32toh(Y[0]);
-	v[1] = be32toh(Y[1]);
-	v[2] = be32toh(Y[2]);
-	v[3] = be32toh(Y[3]);
-
-	for (i = 0; i < GMAC_BLOCK_LEN * 8; i++) {
-		/* update Z */
-		if (x[i >> 3] & (1 << (~i & 7))) {
-			z[0] ^= v[0];
-			z[1] ^= v[1];
-			z[2] ^= v[2];
-			z[3] ^= v[3];
-		} /* else: we preserve old values */
-
-		/* update V */
-		mul = v[3] & 1;
-		v[3] = (v[2] << 31) | (v[3] >> 1);
-		v[2] = (v[1] << 31) | (v[2] >> 1);
-		v[1] = (v[0] << 31) | (v[1] >> 1);
-		v[0] = (v[0] >> 1) ^ (0xe1000000 * mul);
-	}
-
-	product[0] = htobe32(z[0]);
-	product[1] = htobe32(z[1]);
-	product[2] = htobe32(z[2]);
-	product[3] = htobe32(z[3]);
-}
-
-void
-ghash_update(GHASH_CTX *ctx, uint8_t *X, size_t len)
-{
-	uint32_t	*x = (uint32_t *)X;
-	uint32_t	*s = (uint32_t *)ctx->S;
-	uint32_t	*y = (uint32_t *)ctx->Z;
-	int		i;
-
-	for (i = 0; i < len / GMAC_BLOCK_LEN; i++) {
-		s[0] = y[0] ^ x[0];
-		s[1] = y[1] ^ x[1];
-		s[2] = y[2] ^ x[2];
-		s[3] = y[3] ^ x[3];
-
-		ghash_gfmul((uint32_t *)ctx->S, (uint32_t *)ctx->H,
-		    (uint32_t *)ctx->S);
-
-		y = s;
-		x += 4;
-	}
-
-	bcopy(ctx->S, ctx->Z, GMAC_BLOCK_LEN);
-}
-
-#define AESCTR_NONCESIZE	4
-
-void
-AES_GMAC_Init(AES_GMAC_CTX *ctx)
-{
-	bzero(ctx->ghash.H, GMAC_BLOCK_LEN);
-	bzero(ctx->ghash.S, GMAC_BLOCK_LEN);
-	bzero(ctx->ghash.Z, GMAC_BLOCK_LEN);
-	bzero(ctx->J, GMAC_BLOCK_LEN);
-}
-
-int
-AES_GMAC_Setkey(AES_GMAC_CTX *ctx, const uint8_t *key, uint16_t klen)
-{
-	ctx->rounds = rijndaelKeySetupEnc(ctx->K, __DECONST(u_char *, key),
-	    (klen - AESCTR_NONCESIZE) * 8);
-	/* copy out salt to the counter block */
-	bcopy(key + klen - AESCTR_NONCESIZE, ctx->J, AESCTR_NONCESIZE);
-	/* prepare a hash subkey */
-	rijndaelEncrypt(ctx->K, ctx->rounds, ctx->ghash.H, ctx->ghash.H);
-	return (0);
-}
-
-void
-AES_GMAC_Reinit(AES_GMAC_CTX *ctx, const uint8_t *iv, uint16_t ivlen)
-{
-	/* copy out IV to the counter block */
-	bcopy(iv, ctx->J + AESCTR_NONCESIZE, ivlen);
-}
-
-int
-AES_GMAC_Update(AES_GMAC_CTX *ctx, uint8_t *data, uint16_t len)
-{
-	uint32_t	blk[4] = { 0, 0, 0, 0 };
-	int		plen;
-
-	if (len > 0) {
-		plen = len % GMAC_BLOCK_LEN;
-		if (len >= GMAC_BLOCK_LEN)
-			ghash_update(&ctx->ghash, data, len - plen);
-		if (plen) {
-			bcopy(data + (len - plen), (uint8_t *)blk, plen);
-			ghash_update(&ctx->ghash, (uint8_t *)blk,
-			    GMAC_BLOCK_LEN);
-		}
-	}
-	return (0);
-}
-
-void
-AES_GMAC_Final(uint8_t digest[GMAC_DIGEST_LEN], AES_GMAC_CTX *ctx)
-{
-	uint8_t		keystream[GMAC_BLOCK_LEN];
-	int		i;
-
-	/* do one round of GCTR */
-	ctx->J[GMAC_BLOCK_LEN - 1] = 1;
-	rijndaelEncrypt(ctx->K, ctx->rounds, ctx->J, keystream);
-	for (i = 0; i < GMAC_DIGEST_LEN; i++)
-		digest[i] = ctx->ghash.S[i] ^ keystream[i];
-	bzero(keystream, sizeof(keystream));
-}
-
diff --git a/sys/opencrypto/gmac.h b/sys/opencrypto/gmac.h
deleted file mode 100644
index f45a91f31e..0000000000
--- a/sys/opencrypto/gmac.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*	$OpenBSD: gmac.h,v 1.1 2010/09/22 11:54:23 mikeb Exp $	*/
-
-/*
- * Copyright (c) 2010 Mike Belopuhov <mike@vantronix.net>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#ifndef _GMAC_H_
-#define _GMAC_H_
-
-#include <crypto/rijndael/rijndael.h>
-
-#define GMAC_BLOCK_LEN		16
-#define GMAC_DIGEST_LEN		16
-
-typedef struct _GHASH_CTX {
-	uint8_t		H[GMAC_BLOCK_LEN];		/* hash subkey */
-	uint8_t		S[GMAC_BLOCK_LEN];		/* state */
-	uint8_t		Z[GMAC_BLOCK_LEN];		/* initial state */
-} GHASH_CTX;
-
-typedef struct _AES_GMAC_CTX {
-	GHASH_CTX	ghash;
-	uint32_t	K[4*(RIJNDAEL_MAXNR + 1)];
-	uint8_t		J[GMAC_BLOCK_LEN];		/* counter block */
-	int		rounds;
-} AES_GMAC_CTX;
-
-#include <sys/cdefs.h>
-
-__BEGIN_DECLS
-void	AES_GMAC_Init(AES_GMAC_CTX *);
-int	AES_GMAC_Setkey(AES_GMAC_CTX *, const uint8_t *, uint16_t);
-void	AES_GMAC_Reinit(AES_GMAC_CTX *, const uint8_t *, uint16_t);
-int	AES_GMAC_Update(AES_GMAC_CTX *, uint8_t *, uint16_t);
-void	AES_GMAC_Final(uint8_t [GMAC_DIGEST_LEN], AES_GMAC_CTX *);
-__END_DECLS
-
-#endif /* _GMAC_H_ */
-
diff --git a/sys/opencrypto/skipjack.c b/sys/opencrypto/skipjack.c
deleted file mode 100644
index fa351d86ba..0000000000
--- a/sys/opencrypto/skipjack.c
+++ /dev/null
@@ -1,258 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/skipjack.c,v 1.3 2005/01/07 02:29:16 imp Exp $	*/
-/*	$OpenBSD: skipjack.c,v 1.3 2001/05/05 00:31:34 angelos Exp $	*/
-/*-
- * Further optimized test implementation of SKIPJACK algorithm 
- * Mark Tillotson <markt@chaos.org.uk>, 25 June 98
- * Optimizations suit RISC (lots of registers) machine best.
- *
- * based on unoptimized implementation of
- * Panu Rissanen <bande@lut.fi> 960624
- *
- * SKIPJACK and KEA Algorithm Specifications 
- * Version 2.0 
- * 29 May 1998
-*/
-
-#include <sys/param.h>
-
-#include <opencrypto/skipjack.h>
-
-static const u_int8_t ftable[0x100] =
-{ 
-	0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, 
-	0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, 
-	0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, 
-	0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, 
-	0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, 
-	0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, 
-	0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, 
-	0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, 
-	0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, 
-	0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, 
-	0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, 
-	0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, 
-	0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, 
-	0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, 
-	0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, 
-	0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, 
-	0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, 
-	0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, 
-	0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, 
-	0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, 
-	0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, 
-	0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, 
-	0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, 
-	0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, 
-	0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, 
-	0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, 
-	0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, 
-	0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, 
-	0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, 
-	0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, 
-	0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, 
-	0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46
-};
-
-/*
- * For each key byte generate a table to represent the function 
- *    ftable [in ^ keybyte]
- *
- * These tables used to save an XOR in each stage of the G-function
- * the tables are hopefully pointed to by register allocated variables
- * k0, k1..k9
- */
-
-void
-subkey_table_gen (u_int8_t *key, u_int8_t **key_tables)
-{
-	int i, k;
-
-	for (k = 0; k < 10; k++) {
-		u_int8_t   key_byte = key [k];
-		u_int8_t * table = key_tables[k];
-		for (i = 0; i < 0x100; i++)
-			table [i] = ftable [i ^ key_byte];
-	}
-}
-
-
-#define g(k0, k1, k2, k3, ih, il, oh, ol) \
-{ \
-	oh = k##k0 [il] ^ ih; \
-	ol = k##k1 [oh] ^ il; \
-	oh = k##k2 [ol] ^ oh; \
-	ol = k##k3 [oh] ^ ol; \
-}
-
-#define g0(ih, il, oh, ol) g(0, 1, 2, 3, ih, il, oh, ol)
-#define g4(ih, il, oh, ol) g(4, 5, 6, 7, ih, il, oh, ol)
-#define g8(ih, il, oh, ol) g(8, 9, 0, 1, ih, il, oh, ol)
-#define g2(ih, il, oh, ol) g(2, 3, 4, 5, ih, il, oh, ol)
-#define g6(ih, il, oh, ol) g(6, 7, 8, 9, ih, il, oh, ol)
-
- 
-#define g_inv(k0, k1, k2, k3, ih, il, oh, ol) \
-{ \
-	ol = k##k3 [ih] ^ il; \
-	oh = k##k2 [ol] ^ ih; \
-	ol = k##k1 [oh] ^ ol; \
-	oh = k##k0 [ol] ^ oh; \
-}
-
-
-#define g0_inv(ih, il, oh, ol) g_inv(0, 1, 2, 3, ih, il, oh, ol)
-#define g4_inv(ih, il, oh, ol) g_inv(4, 5, 6, 7, ih, il, oh, ol)
-#define g8_inv(ih, il, oh, ol) g_inv(8, 9, 0, 1, ih, il, oh, ol)
-#define g2_inv(ih, il, oh, ol) g_inv(2, 3, 4, 5, ih, il, oh, ol)
-#define g6_inv(ih, il, oh, ol) g_inv(6, 7, 8, 9, ih, il, oh, ol)
-
-/* optimized version of Skipjack algorithm
- *
- * the appropriate g-function is inlined for each round
- *
- * the data movement is minimized by rotating the names of the 
- * variables w1..w4, not their contents (saves 3 moves per round)
- *
- * the loops are completely unrolled (needed to staticize choice of g)
- *
- * compiles to about 470 instructions on a Sparc (gcc -O)
- * which is about 58 instructions per byte, 14 per round.
- * gcc seems to leave in some unnecessary and with 0xFF operations
- * but only in the latter part of the functions.  Perhaps it
- * runs out of resources to properly optimize long inlined function?
- * in theory should get about 11 instructions per round, not 14
- */
-
-void
-skipjack_forwards(u_int8_t *plain, u_int8_t *cipher, u_int8_t **key_tables)
-{
-	u_int8_t wh1 = plain[0];  u_int8_t wl1 = plain[1];
-	u_int8_t wh2 = plain[2];  u_int8_t wl2 = plain[3];
-	u_int8_t wh3 = plain[4];  u_int8_t wl3 = plain[5];
-	u_int8_t wh4 = plain[6];  u_int8_t wl4 = plain[7];
-
-	u_int8_t * k0 = key_tables [0];
-	u_int8_t * k1 = key_tables [1];
-	u_int8_t * k2 = key_tables [2];
-	u_int8_t * k3 = key_tables [3];
-	u_int8_t * k4 = key_tables [4];
-	u_int8_t * k5 = key_tables [5];
-	u_int8_t * k6 = key_tables [6];
-	u_int8_t * k7 = key_tables [7];
-	u_int8_t * k8 = key_tables [8];
-	u_int8_t * k9 = key_tables [9];
-
-	/* first 8 rounds */
-	g0 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 1; wh4 ^= wh1;
-	g4 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 2; wh3 ^= wh4;
-	g8 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 3; wh2 ^= wh3;
-	g2 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 4; wh1 ^= wh2;
-	g6 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 5; wh4 ^= wh1;
-	g0 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 6; wh3 ^= wh4;
-	g4 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 7; wh2 ^= wh3;
-	g8 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 8; wh1 ^= wh2;
-
-	/* second 8 rounds */
-	wh2 ^= wh1; wl2 ^= wl1 ^ 9 ; g2 (wh1,wl1, wh1,wl1);
-	wh1 ^= wh4; wl1 ^= wl4 ^ 10; g6 (wh4,wl4, wh4,wl4);
-	wh4 ^= wh3; wl4 ^= wl3 ^ 11; g0 (wh3,wl3, wh3,wl3);
-	wh3 ^= wh2; wl3 ^= wl2 ^ 12; g4 (wh2,wl2, wh2,wl2);
-	wh2 ^= wh1; wl2 ^= wl1 ^ 13; g8 (wh1,wl1, wh1,wl1);
-	wh1 ^= wh4; wl1 ^= wl4 ^ 14; g2 (wh4,wl4, wh4,wl4);
-	wh4 ^= wh3; wl4 ^= wl3 ^ 15; g6 (wh3,wl3, wh3,wl3);
-	wh3 ^= wh2; wl3 ^= wl2 ^ 16; g0 (wh2,wl2, wh2,wl2);
-
-	/* third 8 rounds */
-	g4 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 17; wh4 ^= wh1;
-	g8 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 18; wh3 ^= wh4;
-	g2 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 19; wh2 ^= wh3;
-	g6 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 20; wh1 ^= wh2;
-	g0 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 21; wh4 ^= wh1;
-	g4 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 22; wh3 ^= wh4;
-	g8 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 23; wh2 ^= wh3;
-	g2 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 24; wh1 ^= wh2;
-
-	/* last 8 rounds */
-	wh2 ^= wh1; wl2 ^= wl1 ^ 25; g6 (wh1,wl1, wh1,wl1);
-	wh1 ^= wh4; wl1 ^= wl4 ^ 26; g0 (wh4,wl4, wh4,wl4);
-	wh4 ^= wh3; wl4 ^= wl3 ^ 27; g4 (wh3,wl3, wh3,wl3);
-	wh3 ^= wh2; wl3 ^= wl2 ^ 28; g8 (wh2,wl2, wh2,wl2);
-	wh2 ^= wh1; wl2 ^= wl1 ^ 29; g2 (wh1,wl1, wh1,wl1);
-	wh1 ^= wh4; wl1 ^= wl4 ^ 30; g6 (wh4,wl4, wh4,wl4);
-	wh4 ^= wh3; wl4 ^= wl3 ^ 31; g0 (wh3,wl3, wh3,wl3);
-	wh3 ^= wh2; wl3 ^= wl2 ^ 32; g4 (wh2,wl2, wh2,wl2);
-
-	/* pack into byte vector */
-	cipher [0] = wh1;  cipher [1] = wl1;
-	cipher [2] = wh2;  cipher [3] = wl2;
-	cipher [4] = wh3;  cipher [5] = wl3;
-	cipher [6] = wh4;  cipher [7] = wl4;
-}
-
-
-void
-skipjack_backwards (u_int8_t *cipher, u_int8_t *plain, u_int8_t **key_tables)
-{
-	/* setup 4 16-bit portions */
-	u_int8_t wh1 = cipher[0];  u_int8_t wl1 = cipher[1];
-	u_int8_t wh2 = cipher[2];  u_int8_t wl2 = cipher[3];
-	u_int8_t wh3 = cipher[4];  u_int8_t wl3 = cipher[5];
-	u_int8_t wh4 = cipher[6];  u_int8_t wl4 = cipher[7];
-
-	u_int8_t * k0 = key_tables [0];
-	u_int8_t * k1 = key_tables [1];
-	u_int8_t * k2 = key_tables [2];
-	u_int8_t * k3 = key_tables [3];
-	u_int8_t * k4 = key_tables [4];
-	u_int8_t * k5 = key_tables [5];
-	u_int8_t * k6 = key_tables [6];
-	u_int8_t * k7 = key_tables [7];
-	u_int8_t * k8 = key_tables [8];
-	u_int8_t * k9 = key_tables [9];
-
-	/* first 8 rounds */
-	g4_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 32; wh3 ^= wh2;
-	g0_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 31; wh4 ^= wh3;
-	g6_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 30; wh1 ^= wh4;
-	g2_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 29; wh2 ^= wh1;
-	g8_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 28; wh3 ^= wh2;
-	g4_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 27; wh4 ^= wh3;
-	g0_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 26; wh1 ^= wh4;
-	g6_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 25; wh2 ^= wh1;
-
-	/* second 8 rounds */
-	wh1 ^= wh2; wl1 ^= wl2 ^ 24; g2_inv (wh2,wl2, wh2,wl2);
-	wh2 ^= wh3; wl2 ^= wl3 ^ 23; g8_inv (wh3,wl3, wh3,wl3);
-	wh3 ^= wh4; wl3 ^= wl4 ^ 22; g4_inv (wh4,wl4, wh4,wl4);
-	wh4 ^= wh1; wl4 ^= wl1 ^ 21; g0_inv (wh1,wl1, wh1,wl1);
-	wh1 ^= wh2; wl1 ^= wl2 ^ 20; g6_inv (wh2,wl2, wh2,wl2);
-	wh2 ^= wh3; wl2 ^= wl3 ^ 19; g2_inv (wh3,wl3, wh3,wl3);
-	wh3 ^= wh4; wl3 ^= wl4 ^ 18; g8_inv (wh4,wl4, wh4,wl4);
-	wh4 ^= wh1; wl4 ^= wl1 ^ 17; g4_inv (wh1,wl1, wh1,wl1);
-
-	/* third 8 rounds */
-	g0_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 16; wh3 ^= wh2;
-	g6_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 15; wh4 ^= wh3;
-	g2_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 14; wh1 ^= wh4;
-	g8_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 13; wh2 ^= wh1;
-	g4_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 12; wh3 ^= wh2;
-	g0_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 11; wh4 ^= wh3;
-	g6_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 10; wh1 ^= wh4;
-	g2_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 9;  wh2 ^= wh1;
-
-	/* last 8 rounds */
-	wh1 ^= wh2; wl1 ^= wl2 ^ 8; g8_inv (wh2,wl2, wh2,wl2);
-	wh2 ^= wh3; wl2 ^= wl3 ^ 7; g4_inv (wh3,wl3, wh3,wl3);
-	wh3 ^= wh4; wl3 ^= wl4 ^ 6; g0_inv (wh4,wl4, wh4,wl4);
-	wh4 ^= wh1; wl4 ^= wl1 ^ 5; g6_inv (wh1,wl1, wh1,wl1);
-	wh1 ^= wh2; wl1 ^= wl2 ^ 4; g2_inv (wh2,wl2, wh2,wl2);
-	wh2 ^= wh3; wl2 ^= wl3 ^ 3; g8_inv (wh3,wl3, wh3,wl3);
-	wh3 ^= wh4; wl3 ^= wl4 ^ 2; g4_inv (wh4,wl4, wh4,wl4);
-	wh4 ^= wh1; wl4 ^= wl1 ^ 1; g0_inv (wh1,wl1, wh1,wl1);
-
-	/* pack into byte vector */
-	plain [0] = wh1;  plain [1] = wl1;
-	plain [2] = wh2;  plain [3] = wl2;
-	plain [4] = wh3;  plain [5] = wl3;
-	plain [6] = wh4;  plain [7] = wl4;
-}
diff --git a/sys/opencrypto/skipjack.h b/sys/opencrypto/skipjack.h
deleted file mode 100644
index f170cc708f..0000000000
--- a/sys/opencrypto/skipjack.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/skipjack.h,v 1.2 2005/01/07 02:29:16 imp Exp $	*/
-/*	$OpenBSD: skipjack.h,v 1.3 2002/03/14 01:26:51 millert Exp $	*/
-
-/*-
- * Further optimized test implementation of SKIPJACK algorithm 
- * Mark Tillotson <markt@chaos.org.uk>, 25 June 98
- * Optimizations suit RISC (lots of registers) machine best.
- *
- * based on unoptimized implementation of
- * Panu Rissanen <bande@lut.fi> 960624
- *
- * SKIPJACK and KEA Algorithm Specifications 
- * Version 2.0 
- * 29 May 1998
-*/
-
-extern void skipjack_forwards(u_int8_t *plain, u_int8_t *cipher, u_int8_t **key);
-extern void skipjack_backwards(u_int8_t *cipher, u_int8_t *plain, u_int8_t **key);
-extern void subkey_table_gen(u_int8_t *key, u_int8_t **key_tables);
diff --git a/sys/opencrypto/xform.c b/sys/opencrypto/xform.c
deleted file mode 100644
index d6ffd9d475..0000000000
--- a/sys/opencrypto/xform.c
+++ /dev/null
@@ -1,1099 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/xform.c,v 1.10 2008/10/23 15:53:51 des Exp $	*/
-/*	$OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $	*/
-/*-
- * The authors of this code are John Ioannidis (ji@tla.org),
- * Angelos D. Keromytis (kermit@csd.uch.gr) and
- * Niels Provos (provos@physnet.uni-hamburg.de).
- *
- * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
- * in November 1995.
- *
- * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
- * by Angelos D. Keromytis.
- *
- * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
- * and Niels Provos.
- *
- * Additional features in 1999 by Angelos D. Keromytis.
- *
- * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
- * Angelos D. Keromytis and Niels Provos.
- *
- * Copyright (C) 2001, Angelos D. Keromytis.
- *
- * Permission to use, copy, and modify this software with or without fee
- * is hereby granted, provided that this entire notice is included in
- * all copies of any software which is or includes a copy or
- * modification of this software.
- * You may use this code under the GNU public license if you so wish. Please
- * contribute changes back to the authors under this freer than GPL license
- * so that we may further the use of strong encryption without limitations to
- * all.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- */
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/malloc.h>
-#include <sys/sysctl.h>
-#include <sys/errno.h>
-#include <sys/time.h>
-#include <sys/kernel.h>
-#include <machine/cpu.h>
-
-#include <crypto/blowfish/blowfish.h>
-#include <crypto/camellia/camellia.h>
-#include <crypto/des/des.h>
-#include <crypto/rijndael/rijndael.h>
-#include <crypto/serpent/serpent.h>
-#include <crypto/sha1.h>
-#include <crypto/twofish/twofish.h>
-
-#include <opencrypto/cast.h>
-#include <opencrypto/deflate.h>
-#include <opencrypto/gmac.h>
-#include <opencrypto/rmd160.h>
-#include <opencrypto/skipjack.h>
-
-#include <sys/md5.h>
-
-#include <opencrypto/cryptodev.h>
-#include <opencrypto/xform.h>
-
-static	void null_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void null_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	int null_setkey(void *, u_int8_t *, int);
-
-static	int des1_setkey(void *, u_int8_t *, int);
-static	int des3_setkey(void *, u_int8_t *, int);
-static	int blf_setkey(void *, u_int8_t *, int);
-static	int cast5_setkey(void *, u_int8_t *, int);
-static	int skipjack_setkey(void *, u_int8_t *, int);
-static	int rijndael128_setkey(void *, u_int8_t *, int);
-static	int aes_xts_setkey(void *, u_int8_t *, int);
-static	int aes_ctr_setkey(void *, u_int8_t *, int);
-static	int cml_setkey(void *, u_int8_t *, int);
-static	int twofish128_setkey(void *, u_int8_t *, int);
-static	int serpent128_setkey(void *, u_int8_t *, int);
-static	int twofish_xts_setkey(void *, u_int8_t *, int);
-static	int serpent_xts_setkey(void *, u_int8_t *, int);
-
-static	void des1_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void des3_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void blf_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void cast5_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void skipjack_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void rijndael128_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void aes_xts_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void cml_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void twofish128_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void serpent128_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void twofish_xts_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void serpent_xts_encrypt(caddr_t, u_int8_t *, u_int8_t *);
-
-static	void des1_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void des3_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void blf_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void cast5_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void skipjack_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void rijndael128_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void aes_xts_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void cml_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void twofish128_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void serpent128_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void twofish_xts_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-static	void serpent_xts_decrypt(caddr_t, u_int8_t *, u_int8_t *);
-
-static	void aes_ctr_crypt(caddr_t, u_int8_t *, u_int8_t *);
-
-static	void aes_ctr_reinit(caddr_t, u_int8_t *);
-static	void aes_xts_reinit(caddr_t, u_int8_t *);
-static	void aes_gcm_reinit(caddr_t, u_int8_t *);
-static	void twofish_xts_reinit(caddr_t, u_int8_t *);
-static	void serpent_xts_reinit(caddr_t, u_int8_t *);
-
-static	void null_init(void *);
-static	int null_update(void *, u_int8_t *, u_int16_t);
-static	void null_final(u_int8_t *, void *);
-static	int MD5Update_int(void *, u_int8_t *, u_int16_t);
-static	void SHA1Init_int(void *);
-static	int SHA1Update_int(void *, u_int8_t *, u_int16_t);
-static	void SHA1Final_int(u_int8_t *, void *);
-static	int RMD160Update_int(void *, u_int8_t *, u_int16_t);
-static	int SHA256Update_int(void *, u_int8_t *, u_int16_t);
-static	int SHA384Update_int(void *, u_int8_t *, u_int16_t);
-static	int SHA512Update_int(void *, u_int8_t *, u_int16_t);
-
-static	u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
-static	u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
-
-#define AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
-#define AESCTR_NONCESIZE	4
-
-struct aes_xts_ctx {
-	rijndael_ctx key1;
-	rijndael_ctx key2;
-};
-
-struct aes_ctr_ctx {
-	u_int32_t	ac_ek[4*(14 + 1)];
-	u_int8_t	ac_block[AESCTR_BLOCK_LEN];
-	int		ac_nr;
-};
-
-struct twofish_xts_ctx {
-	twofish_ctx key1;
-	twofish_ctx key2;
-};
-
-struct serpent_xts_ctx {
-	serpent_ctx key1;
-	serpent_ctx key2;
-};
-
-/* Helper */
-static void aes_xts_crypt(struct aes_xts_ctx *, u_int8_t *, u_int8_t *, u_int);
-static void twofish_xts_crypt(struct twofish_xts_ctx *, u_int8_t *, u_int8_t *,
-    u_int);
-static void serpent_xts_crypt(struct serpent_xts_ctx *, u_int8_t *, u_int8_t *,
-    u_int);
-
-MALLOC_DEFINE(M_XDATA, "xform", "xform data buffers");
-
-/* Encryption instances */
-struct enc_xform enc_xform_null = {
-	CRYPTO_NULL_CBC, "NULL",
-	/* NB: blocksize of 4 is to generate a properly aligned ESP header */
-	NULL_BLOCK_LEN, NULL_BLOCK_LEN, 0, 256, /* 2048 bits, max key */
-	sizeof(int),	/* NB: context isn't used */
-	null_encrypt,
-	null_decrypt,
-	null_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_des = {
-	CRYPTO_DES_CBC, "DES",
-	DES_BLOCK_LEN, DES_BLOCK_LEN, 8, 8,
-	sizeof(des_key_schedule),
-	des1_encrypt,
-	des1_decrypt,
-	des1_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_3des = {
-	CRYPTO_3DES_CBC, "3DES",
-	DES3_BLOCK_LEN, DES3_BLOCK_LEN, 24, 24,
-	3 * sizeof(des_key_schedule),
-	des3_encrypt,
-	des3_decrypt,
-	des3_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_blf = {
-	CRYPTO_BLF_CBC, "Blowfish",
-	BLOWFISH_BLOCK_LEN, BLOWFISH_BLOCK_LEN, 5, 56 /* 448 bits, max key */,
-	sizeof(BF_KEY),
-	blf_encrypt,
-	blf_decrypt,
-	blf_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_cast5 = {
-	CRYPTO_CAST_CBC, "CAST-128",
-	CAST128_BLOCK_LEN, CAST128_BLOCK_LEN, 5, 16,
-	sizeof(cast_key),
-	cast5_encrypt,
-	cast5_decrypt,
-	cast5_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_skipjack = {
-	CRYPTO_SKIPJACK_CBC, "Skipjack",
-	SKIPJACK_BLOCK_LEN, SKIPJACK_BLOCK_LEN, 10, 10,
-	10 * (sizeof(u_int8_t *) + 0x100), /* NB: all needed memory */
-	skipjack_encrypt,
-	skipjack_decrypt,
-	skipjack_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_rijndael128 = {
-	CRYPTO_RIJNDAEL128_CBC, "Rijndael-128/AES",
-	RIJNDAEL128_BLOCK_LEN, RIJNDAEL128_BLOCK_LEN, 8, 32,
-	sizeof(rijndael_ctx),
-	rijndael128_encrypt,
-	rijndael128_decrypt,
-	rijndael128_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_aes_xts = {
-	CRYPTO_AES_XTS, "AES-XTS",
-	AES_XTS_BLOCK_LEN, AES_XTS_IV_LEN, 32, 64,
-	sizeof(struct aes_xts_ctx),
-	aes_xts_encrypt,
-	aes_xts_decrypt,
-	aes_xts_setkey,
-	aes_xts_reinit,
-};
-
-struct enc_xform enc_xform_aes_ctr = {
-	CRYPTO_AES_CTR, "AES-CTR",
-	AESCTR_BLOCK_LEN, AESCTR_IV_LEN, 16+4, 32+4,
-	sizeof(struct aes_ctr_ctx),
-	aes_ctr_crypt,
-	aes_ctr_crypt,
-	aes_ctr_setkey,
-	aes_ctr_reinit,
-};
-
-struct enc_xform enc_xform_aes_gcm = {
-	CRYPTO_AES_GCM_16, "AES-GCM",
-	AESGCM_BLOCK_LEN, AESGCM_IV_LEN, 16+4, 32+4,
-	sizeof(struct aes_ctr_ctx),
-	aes_ctr_crypt,
-	aes_ctr_crypt,
-	aes_ctr_setkey,
-	aes_gcm_reinit,
-};
-
-struct enc_xform enc_xform_aes_gmac = {
-	CRYPTO_AES_GMAC, "AES-GMAC",
-	AESGMAC_BLOCK_LEN, AESGMAC_IV_LEN, 16+4, 32+4,
-	0, /* NB: no context */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-};
-
-struct enc_xform enc_xform_arc4 = {
-	CRYPTO_ARC4, "ARC4",
-	1, 1, 1, 32,
-	0, /* NB: no context */
-	NULL,
-	NULL,
-	NULL,
-	NULL,
-};
-
-struct enc_xform enc_xform_camellia = {
-	CRYPTO_CAMELLIA_CBC, "Camellia",
-	CAMELLIA_BLOCK_LEN, CAMELLIA_BLOCK_LEN, 8, 32,
-	sizeof(camellia_ctx),
-	cml_encrypt,
-	cml_decrypt,
-	cml_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_twofish = {
-	CRYPTO_TWOFISH_CBC, "Twofish",
-	TWOFISH_BLOCK_LEN, TWOFISH_BLOCK_LEN, 8, 32,
-	sizeof(twofish_ctx),
-	twofish128_encrypt,
-	twofish128_decrypt,
-	twofish128_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_serpent = {
-	CRYPTO_SERPENT_CBC, "Serpent",
-	SERPENT_BLOCK_LEN, SERPENT_BLOCK_LEN, 8, 32,
-	sizeof(serpent_ctx),
-	serpent128_encrypt,
-	serpent128_decrypt,
-	serpent128_setkey,
-	NULL,
-};
-
-struct enc_xform enc_xform_twofish_xts = {
-	CRYPTO_TWOFISH_XTS, "TWOFISH-XTS",
-	TWOFISH_XTS_BLOCK_LEN, TWOFISH_XTS_IV_LEN, 32, 64,
-	sizeof(struct twofish_xts_ctx),
-	twofish_xts_encrypt,
-	twofish_xts_decrypt,
-	twofish_xts_setkey,
-	twofish_xts_reinit,
-};
-
-struct enc_xform enc_xform_serpent_xts = {
-	CRYPTO_SERPENT_XTS, "SERPENT-XTS",
-	SERPENT_XTS_BLOCK_LEN, SERPENT_XTS_IV_LEN, 32, 64,
-	sizeof(struct serpent_xts_ctx),
-	serpent_xts_encrypt,
-	serpent_xts_decrypt,
-	serpent_xts_setkey,
-	serpent_xts_reinit,
-};
-
-
-/* Authentication instances */
-struct auth_hash auth_hash_null = {
-	CRYPTO_NULL_HMAC, "NULL-HMAC",
-	0, NULL_HASH_LEN, NULL_HMAC_BLOCK_LEN,
-	sizeof(int),	/* NB: context isn't used */
-	null_init, NULL, NULL, null_update, null_final
-};
-
-struct auth_hash auth_hash_hmac_md5 = {
-	CRYPTO_MD5_HMAC, "HMAC-MD5",
-	16, MD5_HASH_LEN, MD5_HMAC_BLOCK_LEN, sizeof(MD5_CTX),
-	(void (*) (void *)) MD5Init, NULL, NULL,
-	MD5Update_int,
-	(void (*) (u_int8_t *, void *)) MD5Final
-};
-
-struct auth_hash auth_hash_hmac_sha1 = {
-	CRYPTO_SHA1_HMAC, "HMAC-SHA1",
-	20, SHA1_HASH_LEN, SHA1_HMAC_BLOCK_LEN, sizeof(SHA1_CTX),
-	SHA1Init_int, NULL, NULL,
-	SHA1Update_int, SHA1Final_int
-};
-
-struct auth_hash auth_hash_hmac_ripemd_160 = {
-	CRYPTO_RIPEMD160_HMAC, "HMAC-RIPEMD-160",
-	20, RIPEMD160_HASH_LEN, RIPEMD160_HMAC_BLOCK_LEN, sizeof(RMD160_CTX),
-	(void (*)(void *)) RMD160Init, NULL, NULL,
-	RMD160Update_int,
-	(void (*)(u_int8_t *, void *)) RMD160Final
-};
-
-struct auth_hash auth_hash_key_md5 = {
-	CRYPTO_MD5_KPDK, "Keyed MD5",
-	0, MD5_KPDK_HASH_LEN, 0, sizeof(MD5_CTX),
-	(void (*)(void *)) MD5Init, NULL, NULL,
-	MD5Update_int,
-	(void (*)(u_int8_t *, void *)) MD5Final
-};
-
-struct auth_hash auth_hash_key_sha1 = {
-	CRYPTO_SHA1_KPDK, "Keyed SHA1",
-	0, SHA1_KPDK_HASH_LEN, 0, sizeof(SHA1_CTX),
-	SHA1Init_int, NULL, NULL,
-	SHA1Update_int, SHA1Final_int
-};
-
-struct auth_hash auth_hash_hmac_sha2_256 = {
-	CRYPTO_SHA2_256_HMAC, "HMAC-SHA2-256",
-	32, SHA2_256_HASH_LEN, SHA2_256_HMAC_BLOCK_LEN, sizeof(SHA256_CTX),
-	(void (*)(void *)) SHA256_Init, NULL, NULL,
-	SHA256Update_int,
-	(void (*)(u_int8_t *, void *)) SHA256_Final
-};
-
-struct auth_hash auth_hash_hmac_sha2_384 = {
-	CRYPTO_SHA2_384_HMAC, "HMAC-SHA2-384",
-	48, SHA2_384_HASH_LEN, SHA2_384_HMAC_BLOCK_LEN, sizeof(SHA384_CTX),
-	(void (*)(void *)) SHA384_Init, NULL, NULL,
-	SHA384Update_int,
-	(void (*)(u_int8_t *, void *)) SHA384_Final
-};
-
-struct auth_hash auth_hash_hmac_sha2_512 = {
-	CRYPTO_SHA2_512_HMAC, "HMAC-SHA2-512",
-	64, SHA2_512_HASH_LEN, SHA2_512_HMAC_BLOCK_LEN, sizeof(SHA512_CTX),
-	(void (*)(void *)) SHA512_Init, NULL, NULL,
-	SHA512Update_int,
-	(void (*)(u_int8_t *, void *)) SHA512_Final
-};
-
-struct auth_hash auth_hash_gmac_aes_128 = {
-	CRYPTO_AES_128_GMAC, "GMAC-AES-128",
-	16+4, 16, 16, sizeof(AES_GMAC_CTX),
-	(void (*)(void *)) AES_GMAC_Init,
-	(int  (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
-	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
-	(int  (*)(void *, u_int8_t *, u_int16_t)) AES_GMAC_Update,
-	(void (*)(u_int8_t *, void *)) AES_GMAC_Final
-};
-
-struct auth_hash auth_hash_gmac_aes_192 = {
-	CRYPTO_AES_192_GMAC, "GMAC-AES-192",
-	24+4, 16, 16, sizeof(AES_GMAC_CTX),
-	(void (*)(void *)) AES_GMAC_Init,
-	(int  (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
-	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
-	(int  (*)(void *, u_int8_t *, u_int16_t)) AES_GMAC_Update,
-	(void (*)(u_int8_t *, void *)) AES_GMAC_Final
-};
-
-struct auth_hash auth_hash_gmac_aes_256 = {
-	CRYPTO_AES_256_GMAC, "GMAC-AES-256",
-	32+4, 16, 16, sizeof(AES_GMAC_CTX),
-	(void (*)(void *)) AES_GMAC_Init,
-	(int  (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Setkey,
-	(void (*)(void *, const u_int8_t *, u_int16_t)) AES_GMAC_Reinit,
-	(int  (*)(void *, u_int8_t *, u_int16_t)) AES_GMAC_Update,
-	(void (*)(u_int8_t *, void *)) AES_GMAC_Final
-};
-
-/* Compression instance */
-struct comp_algo comp_algo_deflate = {
-	CRYPTO_DEFLATE_COMP, "Deflate",
-	90, deflate_compress,
-	deflate_decompress
-};
-
-/*
- * Encryption wrapper routines.
- */
-
-static void
-null_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-}
-
-static void
-null_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-}
-
-static int
-null_setkey(void *sched, u_int8_t *key, int len)
-{
-	return 0;
-}
-
-static void
-des1_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	des_cblock *cb = (des_cblock *) blk;
-	des_key_schedule *p = (des_key_schedule *) key;
-
-	des_ecb_encrypt(cb, cb, p[0], DES_ENCRYPT);
-}
-
-static void
-des1_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	des_cblock *cb = (des_cblock *) blk;
-	des_key_schedule *p = (des_key_schedule *) key;
-
-	des_ecb_encrypt(cb, cb, p[0], DES_DECRYPT);
-}
-
-static int
-des1_setkey(void *sched, u_int8_t *key, int len)
-{
-	return des_set_key((des_cblock *)key, sched);
-}
-
-static void
-des3_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	des_cblock *cb = (des_cblock *) blk;
-	des_key_schedule *p = (des_key_schedule *) key;
-
-	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_ENCRYPT);
-}
-
-static void
-des3_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	des_cblock *cb = (des_cblock *) blk;
-	des_key_schedule *p = (des_key_schedule *) key;
-
-	des_ecb3_encrypt(cb, cb, p[0], p[1], p[2], DES_DECRYPT);
-}
-
-static int
-des3_setkey(void *sched, u_int8_t *key, int len)
-{
-	des_key_schedule *p;
-
-	p = sched;
-	if (des_set_key((des_cblock *)(key +  0), p[0]) < 0 ||
-	    des_set_key((des_cblock *)(key +  8), p[1]) < 0 ||
-	    des_set_key((des_cblock *)(key + 16), p[2]) < 0)
-		return -1;
-
-	return 0;
-}
-
-static void
-blf_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	BF_LONG t[2];
-
-	memcpy(t, blk, sizeof (t));
-	t[0] = ntohl(t[0]);
-	t[1] = ntohl(t[1]);
-	/* NB: BF_encrypt expects the block in host order! */
-	BF_encrypt(t, (BF_KEY *) key);
-	t[0] = htonl(t[0]);
-	t[1] = htonl(t[1]);
-	memcpy(blk, t, sizeof (t));
-}
-
-static void
-blf_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	BF_LONG t[2];
-
-	memcpy(t, blk, sizeof (t));
-	t[0] = ntohl(t[0]);
-	t[1] = ntohl(t[1]);
-	/* NB: BF_decrypt expects the block in host order! */
-	BF_decrypt(t, (BF_KEY *) key);
-	t[0] = htonl(t[0]);
-	t[1] = htonl(t[1]);
-	memcpy(blk, t, sizeof (t));
-}
-
-static int
-blf_setkey(void *sched, u_int8_t *key, int len)
-{
-	BF_set_key(sched, len, key);
-	return 0;
-}
-
-static void
-cast5_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	cast_encrypt((cast_key *) key, blk, blk);
-}
-
-static void
-cast5_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	cast_decrypt((cast_key *) key, blk, blk);
-}
-
-static int
-cast5_setkey(void *sched, u_int8_t *key, int len)
-{
-	cast_setkey(sched, key, len);
-	return 0;
-}
-
-static void
-skipjack_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	skipjack_forwards(blk, blk, (u_int8_t **) key);
-}
-
-static void
-skipjack_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	skipjack_backwards(blk, blk, (u_int8_t **) key);
-}
-
-static int
-skipjack_setkey(void *sched, u_int8_t *key, int len)
-{
-	u_int8_t **key_tables = sched;
-	u_int8_t *table = (u_int8_t *)&key_tables[10];
-	int k;
-
-	for (k = 0; k < 10; k++) {
-		key_tables[k] = table;
-		table += 0x100;
-	}
-	subkey_table_gen(key, sched);
-
-	return 0;
-}
-
-static void
-rijndael128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	rijndael_encrypt((rijndael_ctx *) key, (u_char *) blk, (u_char *) blk);
-}
-
-static void
-rijndael128_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	rijndael_decrypt(((rijndael_ctx *) key), (u_char *) blk,
-			 (u_char *) blk);
-}
-
-static int
-rijndael128_setkey(void *sched, u_int8_t *key, int len)
-{
-	if (len != 16 && len != 24 && len != 32)
-		return (EINVAL);
-
-	rijndael_set_key(sched, (u_char *) key, len * 8);
-
-	return 0;
-}
-
-void
-aes_xts_reinit(caddr_t key, u_int8_t *iv)
-{
-	struct aes_xts_ctx *ctx = (struct aes_xts_ctx *)key;
-#if 0
-	u_int64_t blocknum;
-	u_int i;
-#endif
-
-#if 0
-	/*
-	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
-	 * of a 64-bit block number which we allow to be passed in directly.
-	 */
-	/* XXX: possibly use htole64? */
-#endif
-	/* Last 64 bits of IV are always zero */
-	bzero(iv + AES_XTS_IV_LEN, AES_XTS_IV_LEN);
-
-	rijndael_encrypt(&ctx->key2, iv, iv);
-}
-
-void
-aes_xts_crypt(struct aes_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
-	      u_int do_encrypt)
-{
-	u_int8_t block[AES_XTS_BLOCK_LEN];
-	u_int i, carry_in, carry_out;
-
-	for (i = 0; i < AES_XTS_BLOCK_LEN; i++)
-		block[i] = data[i] ^ iv[i];
-
-	if (do_encrypt)
-		rijndael_encrypt(&ctx->key1, block, data);
-	else
-		rijndael_decrypt(&ctx->key1, block, data);
-
-	for (i = 0; i < AES_XTS_BLOCK_LEN; i++)
-		data[i] ^= iv[i];
-
-	/* Exponentiate tweak */
-	carry_in = 0;
-	for (i = 0; i < AES_XTS_BLOCK_LEN; i++) {
-		carry_out = iv[i] & 0x80;
-		iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
-		carry_in = carry_out;
-	}
-	if (carry_in)
-		iv[0] ^= AES_XTS_ALPHA;
-	bzero(block, sizeof(block));
-}
-
-void
-aes_xts_encrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	aes_xts_crypt((struct aes_xts_ctx *)key, data, iv, 1);
-}
-
-void
-aes_xts_decrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	aes_xts_crypt((struct aes_xts_ctx *)key, data, iv, 0);
-}
-
-int
-aes_xts_setkey(void *sched, u_int8_t *key, int len)
-{
-	struct aes_xts_ctx *ctx;
-
-	if (len != 32 && len != 64)
-		return -1;
-
-	ctx = sched;
-	rijndael_set_key(&ctx->key1, key, len * 4);
-	rijndael_set_key(&ctx->key2, key + (len / 2), len * 4);
-
-	return 0;
-}
-
-void
-aes_ctr_reinit(caddr_t key, u_int8_t *iv)
-{
-	struct aes_ctr_ctx *ctx;
-
-	ctx = (struct aes_ctr_ctx *)key;
-	bcopy(iv, iv + AESCTR_NONCESIZE, AESCTR_IV_LEN);
-	bcopy(ctx->ac_block, iv, AESCTR_NONCESIZE);
-
-	/* reset counter */
-	bzero(iv + AESCTR_NONCESIZE + AESCTR_IV_LEN, 4);
-}
-
-void
-aes_ctr_crypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	struct aes_ctr_ctx *ctx;
-	u_int8_t keystream[AESCTR_BLOCK_LEN];
-	int i;
-
-	ctx = (struct aes_ctr_ctx *)key;
-	/* increment counter */
-	for (i = AESCTR_BLOCK_LEN - 1;
-	i >= AESCTR_NONCESIZE + AESCTR_IV_LEN; i--)
-		if (++iv[i])   /* continue on overflow */
-			break;
-	rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, iv, keystream);
-	for (i = 0; i < AESCTR_BLOCK_LEN; i++)
-		data[i] ^= keystream[i];
-	bzero(keystream, sizeof(keystream));
-}
-
-int
-aes_ctr_setkey(void *sched, u_int8_t *key, int len)
-{
-	struct aes_ctr_ctx *ctx;
-
-	len -= AESCTR_NONCESIZE;
-	if (len < 0)
-		return -1;
-	if (!(len == 16 || len == 24 || len == 32))
-		return -1; /* invalid key bits */
-
-	ctx = sched;
-	ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, key, len * 8);
-	if (ctx->ac_nr == 0) {
-		bzero(ctx, sizeof(struct aes_ctr_ctx));
-		return -1;
-	}
-
-	bcopy(key + len, ctx->ac_block, AESCTR_NONCESIZE);
-
-	return 0;
-}
-
-static void
-aes_gcm_reinit(caddr_t key, u_int8_t *iv)
-{
-	struct aes_ctr_ctx *ctx;
-
-	ctx = (struct aes_ctr_ctx *)key;
-	bcopy(iv, ctx->ac_block + AESCTR_NONCESIZE, AESCTR_IV_LEN);
-
-	/* reset counter */
-	bzero(ctx->ac_block + AESCTR_NONCESIZE + AESCTR_IV_LEN, 4);
-	ctx->ac_block[AESCTR_BLOCK_LEN - 1] = 1; /* GCM starts with 1 */
-}
-
-static void
-cml_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	camellia_encrypt((camellia_ctx *) key, (u_char *) blk, (u_char *) blk);
-}
-
-static void
-cml_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	camellia_decrypt(((camellia_ctx *) key), (u_char *) blk,
-			 (u_char *) blk);
-}
-
-static int
-cml_setkey(void *sched, u_int8_t *key, int len)
-{
-	if (len != 16 && len != 24 && len != 32)
-		return (EINVAL);
-
-	camellia_set_key(sched, key, len * 8);
-
-	return 0;
-}
-
-static void
-twofish128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	twofish_encrypt((twofish_ctx *) key, blk, blk);
-}
-
-static void
-twofish128_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	twofish_decrypt(((twofish_ctx *) key), blk, blk);
-}
-
-static int
-twofish128_setkey(void *sched, u_int8_t *key, int len)
-{
-	if (len != 16 && len != 24 && len != 32)
-		return (EINVAL);
-
-	twofish_set_key(sched, key, len * 8);
-
-	return 0;
-}
-
-static void
-serpent128_encrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	serpent_encrypt((serpent_ctx *) key, blk, blk);
-}
-
-static void
-serpent128_decrypt(caddr_t key, u_int8_t *blk, u_int8_t *iv)
-{
-	serpent_decrypt(((serpent_ctx *) key), blk, blk);
-}
-
-static int
-serpent128_setkey(void *sched, u_int8_t *key, int len)
-{
-	if (len != 16 && len != 24 && len != 32)
-		return (EINVAL);
-
-	serpent_set_key(sched, key, len * 8);
-
-	return 0;
-}
-
-
-void
-twofish_xts_reinit(caddr_t key, u_int8_t *iv)
-{
-	struct twofish_xts_ctx *ctx = (struct twofish_xts_ctx *)key;
-#if 0
-	u_int64_t blocknum;
-#endif
-
-#if 0
-	/*
-	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
-	 * of a 64-bit block number which we allow to be passed in directly.
-	 */
-	/* XXX: possibly use htole64? */
-#endif
-	/* Last 64 bits of IV are always zero */
-	bzero(iv + TWOFISH_XTS_IV_LEN, TWOFISH_XTS_IV_LEN);
-
-	twofish_encrypt(&ctx->key2, iv, iv);
-}
-
-void
-twofish_xts_crypt(struct twofish_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
-    u_int do_encrypt)
-{
-	u_int8_t block[TWOFISH_XTS_BLOCK_LEN];
-	u_int i, carry_in, carry_out;
-
-	for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++)
-		block[i] = data[i] ^ iv[i];
-
-	if (do_encrypt)
-		twofish_encrypt(&ctx->key1, block, data);
-	else
-		twofish_decrypt(&ctx->key1, block, data);
-
-	for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++)
-		data[i] ^= iv[i];
-
-	/* Exponentiate tweak */
-	carry_in = 0;
-	for (i = 0; i < TWOFISH_XTS_BLOCK_LEN; i++) {
-		carry_out = iv[i] & 0x80;
-		iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
-		carry_in = carry_out;
-	}
-	if (carry_in)
-		iv[0] ^= AES_XTS_ALPHA;
-	bzero(block, sizeof(block));
-}
-
-void
-twofish_xts_encrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	twofish_xts_crypt((struct twofish_xts_ctx *)key, data, iv, 1);
-}
-
-void
-twofish_xts_decrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	twofish_xts_crypt((struct twofish_xts_ctx *)key, data, iv, 0);
-}
-
-int
-twofish_xts_setkey(void *sched, u_int8_t *key, int len)
-{
-	struct twofish_xts_ctx *ctx;
-
-	if (len != 32 && len != 64)
-		return -1;
-
-	ctx = sched;
-	twofish_set_key(&ctx->key1, key, len * 4);
-	twofish_set_key(&ctx->key2, key + (len / 2), len * 4);
-
-	return 0;
-}
-
-
-void
-serpent_xts_reinit(caddr_t key, u_int8_t *iv)
-{
-	struct serpent_xts_ctx *ctx = (struct serpent_xts_ctx *)key;
-#if 0
-	u_int64_t blocknum;
-	u_int i;
-#endif
-
-#if 0
-	/*
-	 * Prepare tweak as E_k2(IV). IV is specified as LE representation
-	 * of a 64-bit block number which we allow to be passed in directly.
-	 */
-	/* XXX: possibly use htole64? */
-#endif
-	/* Last 64 bits of IV are always zero */
-	bzero(iv + SERPENT_XTS_IV_LEN, SERPENT_XTS_IV_LEN);
-
-	serpent_encrypt(&ctx->key2, iv, iv);
-}
-
-void
-serpent_xts_crypt(struct serpent_xts_ctx *ctx, u_int8_t *data, u_int8_t *iv,
-    u_int do_encrypt)
-{
-	u_int8_t block[SERPENT_XTS_BLOCK_LEN];
-	u_int i, carry_in, carry_out;
-
-	for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++)
-		block[i] = data[i] ^ iv[i];
-
-	if (do_encrypt)
-		serpent_encrypt(&ctx->key1, block, data);
-	else
-		serpent_decrypt(&ctx->key1, block, data);
-
-	for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++)
-		data[i] ^= iv[i];
-
-	/* Exponentiate tweak */
-	carry_in = 0;
-	for (i = 0; i < SERPENT_XTS_BLOCK_LEN; i++) {
-		carry_out = iv[i] & 0x80;
-		iv[i] = (iv[i] << 1) | (carry_in ? 1 : 0);
-		carry_in = carry_out;
-	}
-	if (carry_in)
-		iv[0] ^= AES_XTS_ALPHA;
-	bzero(block, sizeof(block));
-}
-
-void
-serpent_xts_encrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	serpent_xts_crypt((struct serpent_xts_ctx *)key, data, iv, 1);
-}
-
-void
-serpent_xts_decrypt(caddr_t key, u_int8_t *data, u_int8_t *iv)
-{
-	serpent_xts_crypt((struct serpent_xts_ctx *)key, data, iv, 0);
-}
-
-int
-serpent_xts_setkey(void *sched, u_int8_t *key, int len)
-{
-	struct serpent_xts_ctx *ctx;
-
-	if (len != 32 && len != 64)
-		return -1;
-
-	ctx = sched;
-	serpent_set_key(&ctx->key1, key, len * 4);
-	serpent_set_key(&ctx->key2, key + (len / 2), len * 4);
-
-	return 0;
-}
-
-
-/*
- * And now for auth.
- */
-
-static void
-null_init(void *ctx)
-{
-}
-
-static int
-null_update(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	return 0;
-}
-
-static void
-null_final(u_int8_t *buf, void *ctx)
-{
-	if (buf != NULL)
-		bzero(buf, 12);
-}
-
-static int
-RMD160Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	RMD160Update(ctx, buf, len);
-	return 0;
-}
-
-static int
-MD5Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	MD5Update(ctx, buf, len);
-	return 0;
-}
-
-static void
-SHA1Init_int(void *ctx)
-{
-	SHA1Init(ctx);
-}
-
-static int
-SHA1Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	SHA1Update(ctx, buf, len);
-	return 0;
-}
-
-static void
-SHA1Final_int(u_int8_t *blk, void *ctx)
-{
-	SHA1Final(blk, ctx);
-}
-
-static int
-SHA256Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	SHA256_Update(ctx, buf, len);
-	return 0;
-}
-
-static int
-SHA384Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	SHA384_Update(ctx, buf, len);
-	return 0;
-}
-
-static int
-SHA512Update_int(void *ctx, u_int8_t *buf, u_int16_t len)
-{
-	SHA512_Update(ctx, buf, len);
-	return 0;
-}
-
-/*
- * And compression
- */
-
-static u_int32_t
-deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
-{
-	return deflate_global(data, size, 0, out);
-}
-
-static u_int32_t
-deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out)
-{
-	return deflate_global(data, size, 1, out);
-}
diff --git a/sys/opencrypto/xform.h b/sys/opencrypto/xform.h
deleted file mode 100644
index 01178f71de..0000000000
--- a/sys/opencrypto/xform.h
+++ /dev/null
@@ -1,119 +0,0 @@
-/*	$FreeBSD: src/sys/opencrypto/xform.h,v 1.4 2007/05/09 19:37:02 gnn Exp $	*/
-/*	$OpenBSD: xform.h,v 1.8 2001/08/28 12:20:43 ben Exp $	*/
-
-/*-
- * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
- *
- * This code was written by Angelos D. Keromytis in Athens, Greece, in
- * February 2000. Network Security Technologies Inc. (NSTI) kindly
- * supported the development of this code.
- *
- * Copyright (c) 2000 Angelos D. Keromytis
- *
- * Permission to use, copy, and modify this software without fee
- * is hereby granted, provided that this entire notice is included in
- * all source code copies of any software which is or includes a copy or
- * modification of this software.
- *
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
- * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
- * PURPOSE.
- */
-
-#ifndef _CRYPTO_XFORM_H_
-#define _CRYPTO_XFORM_H_
-
-#include <sys/md5.h>
-#include <crypto/sha1.h>
-#include <crypto/sha2/sha2.h>
-#include <opencrypto/rmd160.h>
-#include <opencrypto/gmac.h>
-
-/* Declarations */
-struct auth_hash {
-	int type;
-	char *name;
-	u_int16_t keysize;
-	u_int16_t hashsize;
-	u_int16_t blocksize;
-	u_int16_t ctxsize;
-	void (*Init) (void *);
-	int  (*Setkey) (void *, const u_int8_t *, u_int16_t);
-	void (*Reinit) (void *, const u_int8_t *, u_int16_t);
-	int  (*Update) (void *, u_int8_t *, u_int16_t);
-	void (*Final) (u_int8_t *, void *);
-};
-
-#define	AH_ALEN_MAX	20	/* max authenticator hash length */
-
-struct enc_xform {
-	int type;
-	char *name;
-	u_int16_t blocksize;
-	u_int16_t ivsize;
-	u_int16_t minkey;
-	u_int16_t maxkey;
-	u_int16_t ctxsize;
-	void (*encrypt) (caddr_t, u_int8_t *, u_int8_t *);
-	void (*decrypt) (caddr_t, u_int8_t *, u_int8_t *);
-	int (*setkey) (void *, u_int8_t *, int);
-	void (*reinit) (caddr_t, u_int8_t *);
-};
-
-struct comp_algo {
-	int type;
-	char *name;
-	size_t minlen;
-	u_int32_t (*compress) (u_int8_t *, u_int32_t, u_int8_t **);
-	u_int32_t (*decompress) (u_int8_t *, u_int32_t, u_int8_t **);
-};
-
-union authctx {
-	MD5_CTX md5ctx;
-	SHA1_CTX sha1ctx;
-	RMD160_CTX rmd160ctx;
-	SHA256_CTX sha256ctx;
-	SHA384_CTX sha384ctx;
-	SHA512_CTX sha512ctx;
-	AES_GMAC_CTX	aes_gmac_ctx;
-};
-
-extern struct enc_xform enc_xform_null;
-extern struct enc_xform enc_xform_des;
-extern struct enc_xform enc_xform_3des;
-extern struct enc_xform enc_xform_blf;
-extern struct enc_xform enc_xform_cast5;
-extern struct enc_xform enc_xform_skipjack;
-extern struct enc_xform enc_xform_rijndael128;
-extern struct enc_xform enc_xform_aes_xts;
-extern struct enc_xform enc_xform_aes_ctr;
-extern struct enc_xform enc_xform_aes_gcm;
-extern struct enc_xform enc_xform_aes_gmac;
-extern struct enc_xform enc_xform_arc4;
-extern struct enc_xform enc_xform_camellia;
-extern struct enc_xform enc_xform_twofish;
-extern struct enc_xform enc_xform_serpent;
-extern struct enc_xform enc_xform_twofish_xts;
-extern struct enc_xform enc_xform_serpent_xts;
-
-extern struct auth_hash auth_hash_null;
-extern struct auth_hash auth_hash_key_md5;
-extern struct auth_hash auth_hash_key_sha1;
-extern struct auth_hash auth_hash_hmac_md5;
-extern struct auth_hash auth_hash_hmac_sha1;
-extern struct auth_hash auth_hash_hmac_ripemd_160;
-extern struct auth_hash auth_hash_hmac_sha2_256;
-extern struct auth_hash auth_hash_hmac_sha2_384;
-extern struct auth_hash auth_hash_hmac_sha2_512;
-extern struct auth_hash auth_hash_gmac_aes_128;
-extern struct auth_hash auth_hash_gmac_aes_192;
-extern struct auth_hash auth_hash_gmac_aes_256;
-
-extern struct comp_algo comp_algo_deflate;
-
-#ifdef MALLOC_DECLARE
-MALLOC_DECLARE(M_XDATA);
-#endif
-#endif /* _CRYPTO_XFORM_H_ */
diff --git a/sys/platform/pc64/conf/files b/sys/platform/pc64/conf/files
index 46c9fb9715..add86e3a73 100644
--- a/sys/platform/pc64/conf/files
+++ b/sys/platform/pc64/conf/files
@@ -35,7 +35,6 @@ hptrr_lib.bo			optional	hptrr			\
 	clean 		"hptrr_lib.bo"
 
 # XXX use assembler versions of bf_enc
-crypto/blowfish/bf_enc.c		optional	crypto
 crypto/des/des_enc.c			optional	crypto
 crypto/des/des_ecb.c			optional	crypto
 crypto/des/des_setkey.c			optional	crypto
@@ -62,15 +61,9 @@ ${OSACPI_MD_DIR}/acpi_wakeup.c		optional        acpi
 #	no-obj no-implicit-rule	before-depend				\
 #	clean		"acpi_wakecode.h"
 #
-dev/crypto/aesni/aesencdec_x86_64.S	optional	aesni
-dev/crypto/aesni/aeskeys_x86_64.S	optional	aesni
-dev/crypto/aesni/aesni.c		optional	aesni
-dev/crypto/aesni/aesni_wrap.c		optional	aesni
-dev/crypto/padlock/padlock.c		optional	padlock
-dev/crypto/padlock/padlock_cipher.c	optional	padlock
-dev/crypto/padlock/padlock_hash.c	optional	padlock
-dev/crypto/padlock/padlock_rng.c	optional	padlock
-dev/crypto/padlock/rng_harvest_x86_64.S	optional	padlock
+crypto/aesni/aesencdec_x86_64.S	optional	aesni
+crypto/aesni/aeskeys_x86_64.S	optional	aesni
+crypto/aesni/aesni.c		optional	aesni
 dev/crypto/rdrand/rdrand.c		optional	rdrand
 dev/crypto/rdrand/rdrand_harvest_x86_64.S	optional	rdrand
 dev/video/fb/fb.c			optional	vga
diff --git a/sys/platform/vkernel64/conf/files b/sys/platform/vkernel64/conf/files
index 1ff7e6aa94..feb7675aa7 100644
--- a/sys/platform/vkernel64/conf/files
+++ b/sys/platform/vkernel64/conf/files
@@ -1,7 +1,6 @@
 # This file tells config what files go into building a kernel,
 # files marked standard are always included.
 #
-crypto/blowfish/bf_enc.c		optional	crypto
 crypto/des/des_enc.c			optional	crypto
 crypto/des/des_ecb.c			optional	crypto
 crypto/des/des_setkey.c			optional	crypto
diff --git a/tools/tools/crypto/Makefile b/tools/tools/crypto/Makefile
deleted file mode 100644
index 5af648474c..0000000000
--- a/tools/tools/crypto/Makefile
+++ /dev/null
@@ -1,63 +0,0 @@
-#	$FreeBSD$
-#
-# Copyright (c) 2002, 2003	Sam Leffler, Errno Consulting
-# 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 AUTHOR 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 AUTHOR 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.
-#
-
-ALL=	cryptotest cryptokeytest cryptostats \
-	ubsecstats hifnstats
-BINDIR=	/usr/local/bin
-PRIVATELIB_LDFLAGS?= -rpath /lib/priv:/usr/lib/priv -L /usr/lib/priv
-
-all:	${ALL}
-
-# program to test asymmetric crypto functions
-cryptokeytest: cryptokeytest.c
-	${CC} -o cryptokeytest cryptokeytest.c \
-	 -rpath /usr/lib/priv ${PRIVATELIB_LDFLAGS} \
-	 -lprivate_crypto
-
-# program to dump statistics kept by the core crypto code
-cryptostats: cryptostats.c
-	${CC} -o cryptostats cryptostats.c
-
-# program to print statistics kept by the Broadcom driver
-ubsecstats: ubsecstats.c
-	${CC} -o ubsecstats ubsecstats.c
-
-# program to print statistics kept by the HIFN driver
-hifnstats: hifnstats.c
-	${CC} -o hifnstats hifnstats.c
-
-# program to print statistics kept by the SafeNet driver
-#safestats: safestats.c
-#	${CC} -o safestats safestats.c
-#
-clean:
-	rm -f ${ALL} core a.out
-
-install: ${ALL}
-	for i in ${ALL}; do \
-		install $$i ${DESTDIR}${BINDIR}; \
-	done
diff --git a/tools/tools/crypto/README b/tools/tools/crypto/README
deleted file mode 100644
index d8363ac6ca..0000000000
--- a/tools/tools/crypto/README
+++ /dev/null
@@ -1,45 +0,0 @@
-$FreeBSD: src/tools/tools/crypto/README,v 1.1.2.1 2003/02/24 22:52:36 sam Exp $
-$DragonFly: src/tools/tools/crypto/README,v 1.2 2003/06/17 04:29:11 dillon Exp $
-
-The cryptotest program repeatedly encrypts and decrypts a buffer
-with the built-in iv and key, using hardware crypto.  At the end,
-it computes the data rate achieved.  Operations are carried out by
-making ioctl calls to /dev/crypto.
-
-For a test of how fast a crypto card is, use something like:
-
-	cryptotest -z 1024
-
-This will run a series of tests using the available crypto/cipher
-algorithms over a variety of buffer sizes.  The 1024 says to do
-1024 iterations.  Extra arguments can be used to specify one or
-more buffer sizes to use in doing tests.
-
-A sample run is:
-
- 0.129 sec,    2048    des crypts,       8 bytes,   127120 byte/sec,     1.0 Mb/sec
- 0.129 sec,    2048    des crypts,      16 bytes,   253915 byte/sec,     1.9 Mb/sec
- 0.129 sec,    2048    des crypts,      32 bytes,   508942 byte/sec,     3.9 Mb/sec
- 0.128 sec,    2048    des crypts,      64 bytes,  1020135 byte/sec,     7.8 Mb/sec
- 0.134 sec,    2048    des crypts,     128 bytes,  1954869 byte/sec,    14.9 Mb/sec
- 0.142 sec,    2048    des crypts,     256 bytes,  3698107 byte/sec,    28.2 Mb/sec
- 0.190 sec,    2048    des crypts,    1024 bytes, 11037700 byte/sec,    84.2 Mb/sec
- 0.264 sec,    2048    des crypts,    2048 bytes, 15891127 byte/sec,   121.2 Mb/sec
- 0.403 sec,    2048    des crypts,    4096 bytes, 20828998 byte/sec,   158.9 Mb/sec
- 0.687 sec,    2048    des crypts,    8192 bytes, 24426602 byte/sec,   186.4 Mb/sec
- 0.129 sec,    2048   3des crypts,       8 bytes,   127321 byte/sec,     1.0 Mb/sec
- 0.131 sec,    2048   3des crypts,      16 bytes,   249773 byte/sec,     1.9 Mb/sec
- 0.128 sec,    2048   3des crypts,      32 bytes,   512304 byte/sec,     3.9 Mb/sec
- 0.128 sec,    2048   3des crypts,      64 bytes,  1021685 byte/sec,     7.8 Mb/sec
- 0.132 sec,    2048   3des crypts,     128 bytes,  1986511 byte/sec,    15.2 Mb/sec
- 0.142 sec,    2048   3des crypts,     256 bytes,  3695005 byte/sec,    28.2 Mb/sec
- 0.190 sec,    2048   3des crypts,    1024 bytes, 11024876 byte/sec,    84.1 Mb/sec
- 0.264 sec,    2048   3des crypts,    2048 bytes, 15887997 byte/sec,   121.2 Mb/sec
- 0.402 sec,    2048   3des crypts,    4096 bytes, 20850846 byte/sec,   159.1 Mb/sec
- 0.689 sec,    2048   3des crypts,    8192 bytes, 24333532 byte/sec,   185.7 Mb/sec
-
-Expect ~400 Mb/s for a Broadcom 582x for 16K buffers on a reasonable
-CPU.  Hifn 7811 parts top out at ~120 Mb/s.  Performance depends
-heavily on memory and bus performance.
-
-This code originally came from openbsd; give them all the credit.
diff --git a/tools/tools/crypto/cryptokeytest.c b/tools/tools/crypto/cryptokeytest.c
deleted file mode 100644
index 18de2c9919..0000000000
--- a/tools/tools/crypto/cryptokeytest.c
+++ /dev/null
@@ -1,287 +0,0 @@
-/* $FreeBSD$ */
-/*
- * The big num stuff is a bit broken at the moment and I've not yet fixed it.
- * The symtom is that odd size big nums will fail.  Test code below (it only
- * uses modexp currently).
- * 
- * --Jason L. Wright
- */
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <machine/endian.h>
-#include <sys/time.h>
-#include <crypto/cryptodev.h>
-#include <openssl/bn.h>
-
-#include <paths.h>
-#include <fcntl.h>
-#include <err.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdlib.h>
-
-int	crid = CRYPTO_FLAG_HARDWARE;
-int	verbose = 0;
-
-static int
-devcrypto(void)
-{
-	static int fd = -1;
-
-	if (fd < 0) {
-		fd = open(_PATH_DEV "crypto", O_RDWR, 0);
-		if (fd < 0)
-			err(1, _PATH_DEV "crypto");
-		if (fcntl(fd, F_SETFD, 1) == -1)
-			err(1, "fcntl(F_SETFD) (devcrypto)");
-	}
-	return fd;
-}
-
-static int
-crlookup(const char *devname)
-{
-	struct crypt_find_op find;
-
-	find.crid = -1;
-	strlcpy(find.name, devname, sizeof(find.name));
-	if (ioctl(devcrypto(), CIOCFINDDEV, &find) == -1)
-		err(1, "ioctl(CIOCFINDDEV)");
-	return find.crid;
-}
-
-static const char *
-crfind(int crid)
-{
-	static struct crypt_find_op find;
-
-	bzero(&find, sizeof(find));
-	find.crid = crid;
-	if (ioctl(devcrypto(), CIOCFINDDEV, &find) == -1)
-		err(1, "ioctl(CIOCFINDDEV)");
-	return find.name;
-}
-
-/*
- * Convert a little endian byte string in 'p' that
- * is 'plen' bytes long to a BIGNUM. If 'dst' is NULL,
- * a new BIGNUM is allocated.  Returns NULL on failure.
- *
- * XXX there has got to be a more efficient way to do
- * this, but I haven't figured out enough of the OpenSSL
- * magic.
- */
-BIGNUM *
-le_to_bignum(BIGNUM *dst, u_int8_t *p, int plen)
-{
-	u_int8_t *pd;
-	int i;
-
-	if (plen == 0)
-		return (NULL);
-
-	if ((pd = (u_int8_t *)malloc(plen)) == NULL)
-		return (NULL);
-
-	for (i = 0; i < plen; i++)
-		pd[i] = p[plen - i - 1];
-
-	dst = BN_bin2bn(pd, plen, dst);
-	free(pd);
-	return (dst);
-}
-
-/*
- * Convert a BIGNUM to a little endian byte string.
- * If 'rd' is NULL, allocate space for it, otherwise
- * 'rd' is assumed to have room for BN_num_bytes(n)
- * bytes.  Returns NULL on failure.
- */
-u_int8_t *
-bignum_to_le(BIGNUM *n, u_int8_t *rd)
-{
-	int i, j, k;
-	int blen = BN_num_bytes(n);
-
-	if (blen == 0)
-		return (NULL);
-	if (rd == NULL)
-		rd = (u_int8_t *)malloc(blen);
-	if (rd == NULL)
-		return (NULL);
-
-	for (i = 0, j = 0; i < n->top; i++) {
-		for (k = 0; k < BN_BITS2 / 8; k++) {
-			if ((j + k) >= blen)
-				goto out;
-			rd[j + k] = n->d[i] >> (k * 8);
-		}
-		j += BN_BITS2 / 8;
-	}
-out:
-	return (rd);
-}
-
-int
-UB_mod_exp(BIGNUM *res, BIGNUM *a, BIGNUM *b, BIGNUM *c, BN_CTX *ctx)
-{
-	struct crypt_kop kop;
-	u_int8_t *ale, *ble, *cle;
-	static int crypto_fd = -1;
-
-	if (crypto_fd == -1 && ioctl(devcrypto(), CRIOGET, &crypto_fd) == -1)
-			err(1, "CRIOGET");
-
-	if ((ale = bignum_to_le(a, NULL)) == NULL)
-		err(1, "bignum_to_le, a");
-	if ((ble = bignum_to_le(b, NULL)) == NULL)
-		err(1, "bignum_to_le, b");
-	if ((cle = bignum_to_le(c, NULL)) == NULL)
-		err(1, "bignum_to_le, c");
-
-	bzero(&kop, sizeof(kop));
-	kop.crk_op = CRK_MOD_EXP;
-	kop.crk_iparams = 3;
-	kop.crk_oparams = 1;
-	kop.crk_crid = crid;
-	kop.crk_param[0].crp_p = ale;
-	kop.crk_param[0].crp_nbits = BN_num_bytes(a) * 8;
-	kop.crk_param[1].crp_p = ble;
-	kop.crk_param[1].crp_nbits = BN_num_bytes(b) * 8;
-	kop.crk_param[2].crp_p = cle;
-	kop.crk_param[2].crp_nbits = BN_num_bytes(c) * 8;
-	kop.crk_param[3].crp_p = cle;
-	kop.crk_param[3].crp_nbits = BN_num_bytes(c) * 8;
-
-	if (ioctl(crypto_fd, CIOCKEY2, &kop) == -1)
-		err(1, "CIOCKEY2");
-	if (verbose)
-		printf("device = %s\n", crfind(kop.crk_crid));
-
-	bzero(ale, BN_num_bytes(a));
-	free(ale);
-	bzero(ble, BN_num_bytes(b));
-	free(ble);
-
-	if (kop.crk_status != 0) {
-		printf("error %d\n", kop.crk_status);
-		bzero(cle, BN_num_bytes(c));
-		free(cle);
-		return (-1);
-	} else {
-		res = le_to_bignum(res, cle, BN_num_bytes(c));
-		bzero(cle, BN_num_bytes(c));
-		free(cle);
-		if (res == NULL)
-			err(1, "le_to_bignum");
-		return (0);
-	}
-	return (0);
-}
-
-void
-show_result(a, b, c, sw, hw)
-BIGNUM *a, *b, *c, *sw, *hw;
-{
-	printf("\n");
-
-	printf("A = ");
-	BN_print_fp(stdout, a);
-	printf("\n");
-
-	printf("B = ");
-	BN_print_fp(stdout, b);
-	printf("\n");
-
-	printf("C = ");
-	BN_print_fp(stdout, c);
-	printf("\n");
-
-	printf("sw= ");
-	BN_print_fp(stdout, sw);
-	printf("\n");
-
-	printf("hw= ");
-	BN_print_fp(stdout, hw);
-	printf("\n");
-
-	printf("\n");
-}
-
-void
-testit(void)
-{
-	BIGNUM *a, *b, *c, *r1, *r2;
-	BN_CTX *ctx;
-
-	ctx = BN_CTX_new();
-
-	a = BN_new();
-	b = BN_new();
-	c = BN_new();
-	r1 = BN_new();
-	r2 = BN_new();
-
-	BN_pseudo_rand(a, 1023, 0, 0);
-	BN_pseudo_rand(b, 1023, 0, 0);
-	BN_pseudo_rand(c, 1024, 0, 0);
-
-	if (BN_cmp(a, c) > 0) {
-		BIGNUM *rem = BN_new();
-
-		BN_mod(rem, a, c, ctx);
-		UB_mod_exp(r2, rem, b, c, ctx);
-		BN_free(rem);
-	} else {
-		UB_mod_exp(r2, a, b, c, ctx);
-	}
-	BN_mod_exp(r1, a, b, c, ctx);
-
-	if (BN_cmp(r1, r2) != 0) {
-		show_result(a, b, c, r1, r2);
-	}
-
-	BN_free(r2);
-	BN_free(r1);
-	BN_free(c);
-	BN_free(b);
-	BN_free(a);
-	BN_CTX_free(ctx);
-}
-
-static void
-usage(const char* cmd)
-{
-	printf("usage: %s [-d dev] [-v] [count]\n", cmd);
-	printf("count is the number of bignum ops to do\n");
-	printf("\n");
-	printf("-d use specific device\n");
-	printf("-v be verbose\n");
-	exit(-1);
-}
-
-int
-main(int argc, char *argv[])
-{
-	int c, i;
-
-	while ((c = getopt(argc, argv, "d:v")) != -1) {
-		switch (c) {
-		case 'd':
-			crid = crlookup(optarg);
-			break;
-		case 'v':
-			verbose = 1;
-			break;
-		default:
-			usage(argv[0]);
-		}
-	}
-	argc -= optind, argv += optind;
-
-	for (i = 0; i < 1000; i++) {
-		fprintf(stderr, "test %d\n", i);
-		testit();
-	}
-	return (0);
-}
diff --git a/tools/tools/crypto/cryptostats.c b/tools/tools/crypto/cryptostats.c
deleted file mode 100644
index b54c296d41..0000000000
--- a/tools/tools/crypto/cryptostats.c
+++ /dev/null
@@ -1,105 +0,0 @@
-/*-
- * Copyright (c) 2002, 2003	Sam Leffler, Errno Consulting
- * 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 AUTHOR 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 AUTHOR 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.
- *
- * $FreeBSD$
- */
-
-/*
- * Little program to dump the crypto statistics block and, optionally,
- * zero all the stats or just the timing stuff.
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <sys/types.h>
-#include <sys/sysctl.h>
-#include <sys/time.h>
-#include <crypto/cryptodev.h>
-
-static void
-printt(const char* tag, struct cryptotstat *ts)
-{
-	uint64_t avg, min, max;
-
-	if (ts->count == 0)
-		return;
-	avg = (1000000000LL*ts->acc.tv_sec + ts->acc.tv_nsec) / ts->count;
-	min = 1000000000LL*ts->min.tv_sec + ts->min.tv_nsec;
-	max = 1000000000LL*ts->max.tv_sec + ts->max.tv_nsec;
-	printf("%16.16s: avg %6llu ns : min %6llu ns : max %7llu ns [%u samps]\n",
-		tag, avg, min, max, ts->count);
-}
-
-int
-main(int argc, char *argv[])
-{
-	struct cryptostats stats;
-	size_t slen;
-
-	slen = sizeof (stats);
-	if (sysctlbyname("kern.crypto_stats", &stats, &slen, NULL, 0) < 0)
-		err(1, "kern.cryptostats");
-
-	if (argc > 1 && strcmp(argv[1], "-z") == 0) {
-		bzero(&stats.cs_invoke, sizeof (stats.cs_invoke));
-		bzero(&stats.cs_done, sizeof (stats.cs_done));
-		bzero(&stats.cs_cb, sizeof (stats.cs_cb));
-		bzero(&stats.cs_finis, sizeof (stats.cs_finis));
-		stats.cs_invoke.min.tv_sec = 10000;
-		stats.cs_done.min.tv_sec = 10000;
-		stats.cs_cb.min.tv_sec = 10000;
-		stats.cs_finis.min.tv_sec = 10000;
-		if (sysctlbyname("kern.crypto_stats", NULL, NULL, &stats, sizeof (stats)) < 0)
-			err(1, "kern.cryptostats");
-		exit(0);
-	}
-	if (argc > 1 && strcmp(argv[1], "-Z") == 0) {
-		bzero(&stats, sizeof (stats));
-		stats.cs_invoke.min.tv_sec = 10000;
-		stats.cs_done.min.tv_sec = 10000;
-		stats.cs_cb.min.tv_sec = 10000;
-		stats.cs_finis.min.tv_sec = 10000;
-		if (sysctlbyname("kern.crypto_stats", NULL, NULL, &stats, sizeof (stats)) < 0)
-			err(1, "kern.cryptostats");
-		exit(0);
-	}
-
-
-	printf("%u symmetric crypto ops (%u errors, %u times driver blocked)\n"
-		, stats.cs_ops, stats.cs_errs, stats.cs_blocks);
-	printf("%u key ops (%u errors, %u times driver blocked)\n"
-		, stats.cs_kops, stats.cs_kerrs, stats.cs_kblocks);
-	printf("%u crypto dispatch thread activations\n", stats.cs_intrs);
-	printf("%u crypto return thread activations\n", stats.cs_rets);
-	if (stats.cs_invoke.count) {
-		printf("\n");
-		printt("dispatch->invoke", &stats.cs_invoke);
-		printt("invoke->done", &stats.cs_done);
-		printt("done->cb", &stats.cs_cb);
-		printt("cb->finis", &stats.cs_finis);
-	}
-	return 0;
-}
diff --git a/tools/tools/crypto/cryptotest.c b/tools/tools/crypto/cryptotest.c
deleted file mode 100644
index 495f89797e..0000000000
--- a/tools/tools/crypto/cryptotest.c
+++ /dev/null
@@ -1,622 +0,0 @@
-/*-
- * Copyright (c) 2004 Sam Leffler, Errno Consulting
- * 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,
- *    without modification.
- * 2. Redistributions in binary form must reproduce at minimum a disclaimer
- *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
- *    redistribution must be conditioned upon including a substantially
- *    similar Disclaimer requirement for further binary redistribution.
- * 3. Neither the names of the above-listed copyright holders nor the names
- *    of any contributors may be used to endorse or promote products derived
- *    from this software without specific prior written permission.
- *
- * NO WARRANTY
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
- * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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 DAMAGES.
- *
- * $FreeBSD$
- */
-
-/*
- * Simple tool for testing hardware/system crypto support.
- *
- * cryptotest [-czsbv] [-a algorithm] [count] [size ...]
- *
- * Run count iterations of a crypt+decrypt or mac operation on a buffer of
- * size bytes.  A random key and iv are used.  Options:
- *	-c	check the results
- *	-d dev	pin work on device dev
- *	-z	run all available algorithms on a variety of buffer sizes
- *	-v	be verbose
- *	-b	mark operations for batching
- *	-p	profile kernel crypto operations (must be root)
- *	-t n	fork n threads and run tests concurrently
- * Known algorithms are:
- *	null	null cbc
- *	des	des cbc
- *	3des	3des cbc
- *	blf	blowfish cbc
- *	cast	cast cbc
- *	skj	skipjack cbc
- *	aes	rijndael/aes 128-bit cbc
- *	aes192	rijndael/aes 192-bit cbc
- *	aes256	rijndael/aes 256-bit cbc
- *	md5	md5 hmac
- *	sha1	sha1 hmac
- *	sha256	256-bit sha2 hmac
- *	sha384	384-bit sha2 hmac
- *	sha512	512--bit sha2 hmac
- *
- * For a test of how fast a crypto card is, use something like:
- *	cryptotest -z 1024
- * This will run a series of tests using the available crypto/cipher
- * algorithms over a variety of buffer sizes.  The 1024 says to do 1024
- * iterations.  Extra arguments can be used to specify one or more buffer
- * sizes to use in doing tests.
- *
- * To fork multiple processes all doing the same work, specify -t X on the
- * command line to get X "threads" running simultaneously.  No effort is made
- * to synchronize the threads or otherwise maximize load.
- *
- * If the kernel crypto code is built with CRYPTO_TIMING and you run as root,
- * then you can specify the -p option to get a "profile" of the time spent
- * processing crypto operations.  At present this data is only meaningful for
- * symmetric operations.  To get meaningful numbers you must run on an idle
- * machine.
- *
- * Expect ~400 Mb/s for a Broadcom 582x for 8K buffers on a reasonable CPU
- * (64-bit PCI helps).  Hifn 7811 parts top out at ~110 Mb/s.
- */
-#include <sys/types.h>
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/ioctl.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/wait.h>
-#include <sys/mman.h>
-#include <paths.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <sys/sysctl.h>
-#include <sys/time.h>
-#include <crypto/cryptodev.h>
-
-#define	CHUNK	64	/* how much to display */
-#define	N(a)		(sizeof (a) / sizeof (a[0]))
-#define	streq(a,b)	(strcasecmp(a,b) == 0)
-
-void	hexdump(char *, int);
-
-int	verbose = 0;
-int	opflags = 0;
-int	verify = 0;
-int	crid = CRYPTO_FLAG_HARDWARE;
-
-struct alg {
-	const char* name;
-	int	ishash;
-	int	blocksize;
-	int	minkeylen;
-	int	maxkeylen;
-	int	code;
-} algorithms[] = {
-#ifdef CRYPTO_NULL_CBC
-	{ "null",	0,	8,	1,	256,	CRYPTO_NULL_CBC },
-#endif
-	{ "des",	0,	8,	8,	8,	CRYPTO_DES_CBC },
-	{ "3des",	0,	8,	24,	24,	CRYPTO_3DES_CBC },
-	{ "blf",	0,	8,	5,	56,	CRYPTO_BLF_CBC },
-	{ "cast",	0,	8,	5,	16,	CRYPTO_CAST_CBC },
-	{ "skj",	0,	8,	10,	10,	CRYPTO_SKIPJACK_CBC },
-	{ "aes",	0,	16,	16,	16,	CRYPTO_RIJNDAEL128_CBC},
-	{ "aes192",	0,	16,	24,	24,	CRYPTO_RIJNDAEL128_CBC},
-	{ "aes256",	0,	16,	32,	32,	CRYPTO_RIJNDAEL128_CBC},
-#ifdef notdef
-	{ "arc4",	0,	8,	1,	32,	CRYPTO_ARC4 },
-#endif
-	{ "md5",	1,	8,	16,	16,	CRYPTO_MD5_HMAC },
-	{ "sha1",	1,	8,	20,	20,	CRYPTO_SHA1_HMAC },
-	{ "sha256",	1,	8,	32,	32,	CRYPTO_SHA2_256_HMAC },
-	{ "sha384",	1,	8,	48,	48,	CRYPTO_SHA2_384_HMAC },
-	{ "sha512",	1,	8,	64,	64,	CRYPTO_SHA2_512_HMAC },
-};
-
-static void
-usage(const char* cmd)
-{
-	printf("usage: %s [-czsbv] [-d dev] [-a algorithm] [count] [size ...]\n",
-		cmd);
-	printf("where algorithm is one of:\n");
-	printf("    des 3des (default) blowfish cast skipjack\n");
-	printf("    aes (aka rijndael) aes192 aes256 arc4\n");
-	printf("count is the number of encrypt/decrypt ops to do\n");
-	printf("size is the number of bytes of text to encrypt+decrypt\n");
-	printf("\n");
-	printf("-c check the results (slows timing)\n");
-	printf("-d use specific device\n");
-	printf("-z run all available algorithms on a variety of sizes\n");
-	printf("-v be verbose\n");
-	printf("-b mark operations for batching\n");
-	printf("-p profile kernel crypto operation (must be root)\n");
-	exit(-1);
-}
-
-static struct alg*
-getalgbycode(int cipher)
-{
-	int i;
-
-	for (i = 0; i < N(algorithms); i++)
-		if (cipher == algorithms[i].code)
-			return &algorithms[i];
-	return NULL;
-}
-
-static struct alg*
-getalgbyname(const char* name)
-{
-	int i;
-
-	for (i = 0; i < N(algorithms); i++)
-		if (streq(name, algorithms[i].name))
-			return &algorithms[i];
-	return NULL;
-}
-
-static int
-devcrypto(void)
-{
-	static int fd = -1;
-
-	if (fd < 0) {
-		fd = open(_PATH_DEV "crypto", O_RDWR, 0);
-		if (fd < 0)
-			err(1, _PATH_DEV "crypto");
-		if (fcntl(fd, F_SETFD, 1) == -1)
-			err(1, "fcntl(F_SETFD) (devcrypto)");
-	}
-	return fd;
-}
-
-static int
-crlookup(const char *devname)
-{
-	struct crypt_find_op find;
-
-	find.crid = -1;
-	strlcpy(find.name, devname, sizeof(find.name));
-	if (ioctl(devcrypto(), CIOCFINDDEV, &find) == -1)
-		err(1, "ioctl(CIOCFINDDEV)");
-	return find.crid;
-}
-
-static const char *
-crfind(int crid)
-{
-	static struct crypt_find_op find;
-
-	bzero(&find, sizeof(find));
-	find.crid = crid;
-	if (ioctl(devcrypto(), CRIOFINDDEV, &find) == -1)
-		err(1, "ioctl(CIOCFINDDEV): crid %d", crid);
-	return find.name;
-}
-
-static int
-crget(void)
-{
-	int fd;
-
-	if (ioctl(devcrypto(), CRIOGET, &fd) == -1)
-		err(1, "ioctl(CRIOGET)");
-	if (fcntl(fd, F_SETFD, 1) == -1)
-		err(1, "fcntl(F_SETFD) (crget)");
-	return fd;
-}
-
-static char
-rdigit(void)
-{
-	const char a[] = {
-		0x10,0x54,0x11,0x48,0x45,0x12,0x4f,0x13,0x49,0x53,0x14,0x41,
-		0x15,0x16,0x4e,0x55,0x54,0x17,0x18,0x4a,0x4f,0x42,0x19,0x01
-	};
-	return 0x20+a[random()%N(a)];
-}
-
-static void
-runtest(struct alg *alg, int count, int size, u_long cmd, struct timeval *tv)
-{
-	int i, fd = crget();
-	struct timeval start, stop, dt;
-	char *cleartext, *ciphertext, *originaltext;
-	struct session2_op sop;
-	struct crypt_op cop;
-	char iv[EALG_MAX_BLOCK_LEN];
-
-	bzero(&sop, sizeof(sop));
-	if (!alg->ishash) {
-		sop.keylen = (alg->minkeylen + alg->maxkeylen)/2;
-		sop.key = (char *) malloc(sop.keylen);
-		if (sop.key == NULL)
-			err(1, "malloc (key)");
-		for (i = 0; i < sop.keylen; i++)
-			sop.key[i] = rdigit();
-		sop.cipher = alg->code;
-	} else {
-		sop.mackeylen = (alg->minkeylen + alg->maxkeylen)/2;
-		sop.mackey = (char *) malloc(sop.mackeylen);
-		if (sop.mackey == NULL)
-			err(1, "malloc (mac)");
-		for (i = 0; i < sop.mackeylen; i++)
-			sop.mackey[i] = rdigit();
-		sop.mac = alg->code;
-	}
-	sop.crid = crid;
-	if (ioctl(fd, cmd, &sop) < 0) {
-		if (cmd == CIOCGSESSION || cmd == CIOCGSESSION2) {
-			close(fd);
-			if (verbose) {
-				printf("cipher %s", alg->name);
-				if (alg->ishash)
-					printf(" mackeylen %u\n", sop.mackeylen);
-				else
-					printf(" keylen %u\n", sop.keylen);
-				perror("CIOCGSESSION");
-			}
-			/* hardware doesn't support algorithm; skip it */
-			return;
-		}
-		printf("cipher %s keylen %u mackeylen %u\n",
-			alg->name, sop.keylen, sop.mackeylen);
-		err(1, "CIOCGSESSION");
-	}
-
-	originaltext = malloc(3*size);
-	if (originaltext == NULL)
-		err(1, "malloc (text)");
-	cleartext = originaltext+size;
-	ciphertext = cleartext+size;
-	for (i = 0; i < size; i++)
-		cleartext[i] = rdigit();
-	memcpy(originaltext, cleartext, size);
-	for (i = 0; i < N(iv); i++)
-		iv[i] = rdigit();
-
-	if (verbose) {
-		printf("session = 0x%x\n", sop.ses);
-		printf("device = %s\n", crfind(sop.crid));
-		printf("count = %d, size = %d\n", count, size);
-		if (!alg->ishash) {
-			printf("iv:");
-			hexdump(iv, sizeof iv);
-		}
-		printf("cleartext:");
-		hexdump(cleartext, MIN(size, CHUNK));
-	}
-
-	gettimeofday(&start, NULL);
-	if (!alg->ishash) {
-		for (i = 0; i < count; i++) {
-			cop.ses = sop.ses;
-			cop.op = COP_ENCRYPT;
-			cop.flags = opflags;
-			cop.len = size;
-			cop.src = cleartext;
-			cop.dst = ciphertext;
-			cop.mac = 0;
-			cop.iv = iv;
-
-			if (ioctl(fd, CIOCCRYPT, &cop) < 0)
-				err(1, "ioctl(CIOCCRYPT)");
-
-			if (verify && bcmp(ciphertext, cleartext, size) == 0) {
-				printf("cipher text unchanged:");
-				hexdump(ciphertext, size);
-			}
-
-			memset(cleartext, 'x', MIN(size, CHUNK));
-			cop.ses = sop.ses;
-			cop.op = COP_DECRYPT;
-			cop.flags = opflags;
-			cop.len = size;
-			cop.src = ciphertext;
-			cop.dst = cleartext;
-			cop.mac = 0;
-			cop.iv = iv;
-
-			if (ioctl(fd, CIOCCRYPT, &cop) < 0)
-				err(1, "ioctl(CIOCCRYPT)");
-
-			if (verify && bcmp(cleartext, originaltext, size) != 0) {
-				printf("decrypt mismatch:\n");
-				printf("original:");
-				hexdump(originaltext, size);
-				printf("cleartext:");
-				hexdump(cleartext, size);
-			}
-		}
-	} else {
-		for (i = 0; i < count; i++) {
-			cop.ses = sop.ses;
-			cop.op = 0;
-			cop.flags = opflags;
-			cop.len = size;
-			cop.src = cleartext;
-			cop.dst = 0;
-			cop.mac = ciphertext;
-			cop.iv = 0;
-
-			if (ioctl(fd, CIOCCRYPT, &cop) < 0)
-				err(1, "ioctl(CIOCCRYPT)");
-		}
-	}
-	gettimeofday(&stop, NULL);
-
-	if (ioctl(fd, CIOCFSESSION, &sop.ses) < 0)
-		perror("ioctl(CIOCFSESSION)");
-
-	if (verbose) {
-		printf("cleartext:");
-		hexdump(cleartext, MIN(size, CHUNK));
-	}
-	timersub(&stop, &start, tv);
-
-	free(originaltext);
-
-	close(fd);
-}
-
-#ifdef __FreeBSD__
-static void
-resetstats()
-{
-	struct cryptostats stats;
-	size_t slen;
-
-	slen = sizeof (stats);
-	if (sysctlbyname("kern.crypto_stats", &stats, &slen, NULL, 0) < 0) {
-		perror("kern.crypto_stats");
-		return;
-	}
-	bzero(&stats.cs_invoke, sizeof (stats.cs_invoke));
-	bzero(&stats.cs_done, sizeof (stats.cs_done));
-	bzero(&stats.cs_cb, sizeof (stats.cs_cb));
-	bzero(&stats.cs_finis, sizeof (stats.cs_finis));
-	stats.cs_invoke.min.tv_sec = 10000;
-	stats.cs_done.min.tv_sec = 10000;
-	stats.cs_cb.min.tv_sec = 10000;
-	stats.cs_finis.min.tv_sec = 10000;
-	if (sysctlbyname("kern.crypto_stats", NULL, NULL, &stats, sizeof (stats)) < 0)
-		perror("kern.cryptostats");
-}
-
-static void
-printt(const char* tag, struct cryptotstat *ts)
-{
-	uint64_t avg, min, max;
-
-	if (ts->count == 0)
-		return;
-	avg = (1000000000LL*ts->acc.tv_sec + ts->acc.tv_nsec) / ts->count;
-	min = 1000000000LL*ts->min.tv_sec + ts->min.tv_nsec;
-	max = 1000000000LL*ts->max.tv_sec + ts->max.tv_nsec;
-	printf("%16.16s: avg %6llu ns : min %6llu ns : max %7llu ns [%u samps]\n",
-		tag, avg, min, max, ts->count);
-}
-#endif
-
-static void
-runtests(struct alg *alg, int count, int size, u_long cmd, int threads, int profile)
-{
-	int i, status;
-	double t;
-	void *region;
-	struct timeval *tvp;
-	struct timeval total;
-	int otiming;
-
-	if (size % alg->blocksize) {
-		if (verbose)
-			printf("skipping blocksize %u 'cuz not a multiple of "
-				"%s blocksize %u\n",
-				size, alg->name, alg->blocksize);
-		return;
-	}
-
-	region = mmap(NULL, threads * sizeof (struct timeval),
-			PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
-	if (region == MAP_FAILED) {
-		perror("mmap");
-		return;
-	}
-	tvp = (struct timeval *) region;
-#ifdef __FreeBSD__
-	if (profile) {
-		size_t tlen = sizeof (otiming);
-		int timing = 1;
-
-		resetstats();
-		if (sysctlbyname("debug.crypto_timing", &otiming, &tlen,
-				&timing, sizeof (timing)) < 0)
-			perror("debug.crypto_timing");
-	}
-#endif
-
-	if (threads > 1) {
-		for (i = 0; i < threads; i++)
-			if (fork() == 0) {
-				runtest(alg, count, size, cmd, &tvp[i]);
-				exit(0);
-			}
-		while (waitpid(WAIT_MYPGRP, &status, 0) != -1)
-			;
-	} else
-		runtest(alg, count, size, cmd, tvp);
-
-	t = 0;
-	for (i = 0; i < threads; i++)
-		t += (((double)tvp[i].tv_sec * 1000000 + tvp[i].tv_usec) / 1000000);
-	if (t) {
-		int nops = alg->ishash ? count : 2*count;
-
-#if 0
-		t /= threads;
-		printf("%6.3lf sec, %7d %6s crypts, %7d bytes, %8.0lf byte/sec, %7.1lf Mb/sec\n",
-		    t, nops, alg->name, size, (double)nops*size / t,
-		    (double)nops*size / t * 8 / 1024 / 1024);
-#else
-		nops *= threads;
-		printf("%8.3lf sec, %7d %6s crypts, %7d bytes, %8.0lf byte/sec, %7.1lf Mb/sec\n",
-		    t, nops, alg->name, size, (double)nops*size / t,
-		    (double)nops*size / t * 8 / 1024 / 1024);
-#endif
-	}
-#ifdef __FreeBSD__
-	if (profile) {
-		struct cryptostats stats;
-		size_t slen = sizeof (stats);
-
-		if (sysctlbyname("debug.crypto_timing", NULL, NULL,
-				&otiming, sizeof (otiming)) < 0)
-			perror("debug.crypto_timing");
-		if (sysctlbyname("kern.crypto_stats", &stats, &slen, NULL, 0) < 0)
-			perror("kern.cryptostats");
-		if (stats.cs_invoke.count) {
-			printt("dispatch->invoke", &stats.cs_invoke);
-			printt("invoke->done", &stats.cs_done);
-			printt("done->cb", &stats.cs_cb);
-			printt("cb->finis", &stats.cs_finis);
-		}
-	}
-#endif
-	fflush(stdout);
-}
-
-int
-main(int argc, char **argv)
-{
-	struct alg *alg = NULL;
-	int count = 1;
-	int sizes[128], nsizes = 0;
-	u_long cmd = CIOCGSESSION2;
-	int testall = 0;
-	int maxthreads = 1;
-	int profile = 0;
-	int i, ch;
-
-	while ((ch = getopt(argc, argv, "cpzsva:bd:t:")) != -1) {
-		switch (ch) {
-#ifdef CIOCGSSESSION
-		case 's':
-			cmd = CIOCGSSESSION;
-			break;
-#endif
-		case 'v':
-			verbose++;
-			break;
-		case 'a':
-			alg = getalgbyname(optarg);
-			if (alg == NULL) {
-				if (streq(optarg, "rijndael"))
-					alg = getalgbyname("aes");
-				else
-					usage(argv[0]);
-			}
-			break;
-		case 'd':
-			crid = crlookup(optarg);
-			break;
-		case 't':
-			maxthreads = atoi(optarg);
-			break;
-		case 'z':
-			testall = 1;
-			break;
-		case 'p':
-			profile = 1;
-			break;
-		case 'b':
-			opflags |= COP_F_BATCH;
-			break;
-		case 'c':
-			verify = 1;
-			break;
-		default:
-			usage(argv[0]);
-		}
-	}
-	argc -= optind, argv += optind;
-	if (argc > 0)
-		count = atoi(argv[0]);
-	while (argc > 1) {
-		int s = atoi(argv[1]);
-		if (nsizes < N(sizes)) {
-			sizes[nsizes++] = s;
-		} else {
-			printf("Too many sizes, ignoring %u\n", s);
-		}
-		argc--, argv++;
-	}
-	if (nsizes == 0) {
-		if (alg)
-			sizes[nsizes++] = alg->blocksize;
-		else
-			sizes[nsizes++] = 8;
-		if (testall) {
-			while (sizes[nsizes-1] < 8*1024) {
-				sizes[nsizes] = sizes[nsizes-1]<<1;
-				nsizes++;
-			}
-		}
-	}
-
-	if (testall) {
-		for (i = 0; i < N(algorithms); i++) {
-			int j;
-			alg = &algorithms[i];
-			for (j = 0; j < nsizes; j++)
-				runtests(alg, count, sizes[j], cmd, maxthreads, profile);
-		}
-	} else {
-		if (alg == NULL)
-			alg = getalgbycode(CRYPTO_3DES_CBC);
-		for (i = 0; i < nsizes; i++)
-			runtests(alg, count, sizes[i], cmd, maxthreads, profile);
-	}
-
-	return (0);
-}
-
-void
-hexdump(char *p, int n)
-{
-	int i, off;
-
-	for (off = 0; n > 0; off += 16, n -= 16) {
-		printf("%s%04x:", off == 0 ? "\n" : "", off);
-		i = (n >= 16 ? 16 : n);
-		do {
-			printf(" %02x", *p++ & 0xff);
-		} while (--i);
-		printf("\n");
-	}
-}
diff --git a/tools/tools/crypto/hifnstats.c b/tools/tools/crypto/hifnstats.c
deleted file mode 100644
index d59fa76cca..0000000000
--- a/tools/tools/crypto/hifnstats.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/*-
- * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
- * 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 AUTHOR 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 AUTHOR 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.
- *
- * $FreeBSD: src/tools/tools/crypto/hifnstats.c,v 1.1.4.1 2003/06/03 00:13:13 sam Exp $
- * $DragonFly: src/tools/tools/crypto/hifnstats.c,v 1.3 2008/04/26 09:19:10 swildner Exp $
- */
-#include <stdio.h>
-#include <sys/types.h>
-#include "../../../sys/dev/crypto/hifn/hifn7751var.h"
-
-/*
- * Little program to dump the statistics block for the hifn driver.
- */
-int
-main(int argc, char *argv[])
-{
-	struct hifn_stats stats;
-	size_t slen;
-
-	slen = sizeof (stats);
-	if (sysctlbyname("hw.hifn.stats", &stats, &slen, NULL, NULL) < 0)
-		err(1, "hw.hifn.stats");
-
-	printf("input %llu bytes %u packets\n",
-		stats.hst_ibytes, stats.hst_ipackets);
-	printf("output %llu bytes %u packets\n",
-		stats.hst_obytes, stats.hst_opackets);
-	printf("invalid %u nomem %u abort %u\n",
-		stats.hst_invalid, stats.hst_nomem, stats.hst_abort);
-	printf("noirq %u unaligned %u\n",
-		stats.hst_noirq, stats.hst_unaligned);
-	printf("totbatch %u maxbatch %u\n",
-		stats.hst_totbatch, stats.hst_maxbatch);
-	printf("nomem: map %u load %u mbuf %u mcl %u cr %u sd %u\n",
-		stats.hst_nomem_map, stats.hst_nomem_load,
-		stats.hst_nomem_mbuf, stats.hst_nomem_mcl,
-		stats.hst_nomem_cr, stats.hst_nomem_sd);
-	return 0;
-}
diff --git a/tools/tools/crypto/ubsecstats.c b/tools/tools/crypto/ubsecstats.c
deleted file mode 100644
index 1b5930ff63..0000000000
--- a/tools/tools/crypto/ubsecstats.c
+++ /dev/null
@@ -1,69 +0,0 @@
-/*-
- * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
- * 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 AUTHOR 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 AUTHOR 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.
- *
- * $FreeBSD: src/tools/tools/crypto/ubsecstats.c,v 1.3.4.1 2003/06/03 00:13:13 sam Exp $
- * $DragonFly: src/tools/tools/crypto/ubsecstats.c,v 1.3 2008/04/26 09:19:10 swildner Exp $
- */
-#include <stdio.h>
-#include <sys/types.h>
-#include "../../../sys/dev/crypto/ubsec/ubsecvar.h"
-
-/*
- * Little program to dump the statistics block for the ubsec driver.
- */
-int
-main(int argc, char *argv[])
-{
-	struct ubsec_stats stats;
-	size_t slen;
-
-	slen = sizeof (stats);
-	if (sysctlbyname("hw.ubsec.stats", &stats, &slen, NULL, NULL) < 0)
-		err(1, "hw.ubsec.stats");
-
-	printf("input %llu bytes %u packets\n",
-		stats.hst_ibytes, stats.hst_ipackets);
-	printf("output %llu bytes %u packets\n",
-		stats.hst_obytes, stats.hst_opackets);
-	printf("invalid %u badsession %u badflags %u\n",
-		stats.hst_invalid, stats.hst_badsession, stats.hst_badflags);
-	printf("nodesc %u badalg %u nomem %u queuefull %u\n",
-		stats.hst_nodesc, stats.hst_badalg,
-		stats.hst_nomem, stats.hst_queuefull);
-	printf("dmaerr %u mcrerr %u nodmafree %u\n",
-		stats.hst_dmaerr, stats.hst_mcrerr, stats.hst_nodmafree);
-	printf("lenmismatch %u skipmisatch %u iovmisalined %u\n",
-		stats.hst_lenmismatch, stats.hst_skipmismatch, stats.hst_iovmisaligned);
-	printf("noirq %u unaligned %u nomap %u noload %u nomcl %u\n",
-		stats.hst_noirq, stats.hst_unaligned, stats.hst_nomap,
-		stats.hst_noload, stats.hst_nomcl);
-	printf("totbatch %u maxbatch %u\n",
-		stats.hst_totbatch, stats.hst_maxbatch);
-	printf("maxqueue %u maxqchip %u mcr1full %u\n",
-		stats.hst_maxqueue, stats.hst_maxqchip, stats.hst_mcr1full);
-	printf("rng %u modexp %u moexpcrt %u\n",
-		stats.hst_rng, stats.hst_modexp, stats.hst_modexpcrt);
-	return 0;
-}
-- 
2.47.1

