summaryrefslogtreecommitdiff
path: root/tests/dev
diff options
context:
space:
mode:
authorriastradh <riastradh@NetBSD.org>2022-08-12 10:49:17 +0000
committerriastradh <riastradh@NetBSD.org>2022-08-12 10:49:17 +0000
commita21ea60e236e1412e32c609a173146fefa53223a (patch)
tree2ae01a74ffb676f1da34134914477bed8ae752fd /tests/dev
parent949939c595850a6ee4b54a726d50759a76bcbf25 (diff)
cgdconfig(8): Add support for shared keys.
New clause `shared <id> algorithm <alg> subkey <info>' in a keygen block enables `cgdconfig -C' to reuse a key between different params files, so you can, e.g., use a single password for multiple disks. This is better than simply caching the password itself because: - Hashing the password is expensive, so it should only be done once. Suppose your budget is time t before you get bored, and you calibrate password hash parameters to unlock n disks before you get bored waiting for `cgdconfig -C'. . With n password hashings the adversary's cost goes up only by a factor of t/n. . With one password hashing and n subkeys the adversary's cost goes up by a factor of n. And if you ever add a disk, rehashing it will make `cgdconfig -C' go over budget, whereas another subkey adds negligible cost to you. - Subkeys work for other types of keygen blocks, like shell_cmd, which could be used to get a key from a hardware token that needs a button press. The <info> parameter must be different for each params file; everything else in the keygen block must be the same. With this clause, the keygen block determines a shared key used only to derive keys; the actual key used by cgdconfig is derived from the shared key by the specified algorithm. The only supported algorithm is hkdf-hmac-sha256, which uses HKDF-Expand of RFC 5869 instantiated with SHA-256. Example: algorithm aes-cbc; iv-method encblkno1; keylength 128; verify_method none; keygen pkcs5_pbkdf2/sha1 { iterations 39361; salt AAAAgMoHiYonye6KogdYJAobCHE=; shared "pw" algorithm hkdf-hmac-sha256 subkey AAAAgFlw0BMQ5gY+haYkZ6JC+yY=; }; The key used for this disk will be derived by HKDF-HMAC-SHA256_k(WXDQExDmBj6FpiRnokL7Jg==), where k is the outcome of PBKDF2-SHA1 with the given parameters. Note that <info> encodes a four-byte prefix giving the big-endian length in bits of the info argument to HKDF, just like all other bit strings in cgdconfig parameters files. If you have multiple disks configured using the same keygen block except for the info parameter, `cgdconfig -C' will only prompt once for your passphrase, generate a shared key k with PBKDF2 as usual, and then reuse it for each of the disks.
Diffstat (limited to 'tests/dev')
-rw-r--r--tests/dev/cgd/Makefile5
-rw-r--r--tests/dev/cgd/h_countkey.sh11
-rw-r--r--tests/dev/cgd/t_cgdconfig.sh154
3 files changed, 168 insertions, 2 deletions
diff --git a/tests/dev/cgd/Makefile b/tests/dev/cgd/Makefile
index 01217401ef9..30051874da5 100644
--- a/tests/dev/cgd/Makefile
+++ b/tests/dev/cgd/Makefile
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.12 2022/08/12 10:48:28 riastradh Exp $
+# $NetBSD: Makefile,v 1.13 2022/08/12 10:49:17 riastradh Exp $
#
.include <bsd.own.mk>
@@ -10,6 +10,9 @@ FILESDIR= ${TESTSDIR}
TESTS_SH+= t_cgd
TESTS_SH+= t_cgdconfig
+SCRIPTSDIR= ${TESTSDIR}
+SCRIPTS+= h_countkey.sh
+
.if ${MKRUMP} != "no"
TESTS_C+= t_cgd_3des
TESTS_C+= t_cgd_adiantum
diff --git a/tests/dev/cgd/h_countkey.sh b/tests/dev/cgd/h_countkey.sh
new file mode 100644
index 00000000000..a229c140f7f
--- /dev/null
+++ b/tests/dev/cgd/h_countkey.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+set -Ceu
+
+n=$(cat "$1" 2>/dev/null || echo 0)
+n=$((n + 1))
+echo $n >"$1".tmp
+mv -f "$1".tmp "$1"
+shift
+
+echo ${1+"$@"} | base64 -d
diff --git a/tests/dev/cgd/t_cgdconfig.sh b/tests/dev/cgd/t_cgdconfig.sh
index d1dcb0c7a54..caa4e7a6776 100644
--- a/tests/dev/cgd/t_cgdconfig.sh
+++ b/tests/dev/cgd/t_cgdconfig.sh
@@ -1,4 +1,4 @@
-# $NetBSD: t_cgdconfig.sh,v 1.2 2022/08/12 10:48:44 riastradh Exp $
+# $NetBSD: t_cgdconfig.sh,v 1.3 2022/08/12 10:49:17 riastradh Exp $
#
# Copyright (c) 2022 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -25,6 +25,8 @@
# POSSIBILITY OF SUCH DAMAGE.
#
+COUNTKEY=$(atf_get_srcdir)/h_countkey
+
atf_test_case storedkey
storedkey_head()
{
@@ -120,8 +122,158 @@ EOF
cgdconfig -t params
}
+atf_test_case sharedstoredkey10
+sharedstoredkey10_head()
+{
+ atf_set descr "Test shared key generation from storedkey, 10-byte info"
+}
+sharedstoredkey10_body()
+{
+ cat <<EOF >params
+algorithm adiantum;
+iv-method encblkno1;
+keylength 256;
+verify_method none;
+keygen storedkey {
+ key AAABAAd3CTYsLjLfDdw/DcR7umOQtsc7tQ+cMSLshErXwrPl;
+ shared "helloworld" algorithm hkdf-hmac-sha256 \
+ subkey AAAAUPDx8vP09fb3+Pk=;
+};
+EOF
+ atf_check -o inline:'PLJfJfqs1XqQQ09k0DYvKi0tCpDPGlpMXbAtVuzExb8=\n' \
+ cgdconfig -t params
+}
+
+atf_test_case sharedstoredkey80
+sharedstoredkey80_head()
+{
+ atf_set descr "Test shared key generation from storedkey, 80-byte info"
+}
+sharedstoredkey80_body()
+{
+ cat <<EOF >params
+algorithm adiantum;
+iv-method encblkno1;
+keylength 256;
+verify_method none;
+keygen storedkey {
+ key AAABAAamuIxYUzYaBhBMnOs1tFzvdgAUkEZxAUoZP0DBX8JE;
+ shared "helloworld" algorithm hkdf-hmac-sha256 \
+ subkey AAACgLCxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJ \
+ ysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn \
+ 6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/;
+};
+EOF
+ atf_check -o inline:'sR45jcgDJ6HI5/eMWWpJNE8BLtotTvrYoFDMTBmvqXw=\n' \
+ cgdconfig -t params
+}
+
+atf_test_case sharedstoredkeys
+sharedstoredkeys_head()
+{
+ atf_set descr "Test multiple shared key generations from stored keys"
+}
+sharedstoredkeys_body()
+{
+ cat <<EOF >wd0e
+algorithm adiantum;
+iv-method encblkno1;
+keylength 256;
+verify_method none;
+keygen storedkey {
+ key AAABAAd3CTYsLjLfDdw/DcR7umOQtsc7tQ+cMSLshErXwrPl;
+ shared "helloworld" algorithm hkdf-hmac-sha256 \
+ subkey AAAAUPDx8vP09fb3+Pk=;
+};
+EOF
+ cat <<EOF >ld1e
+algorithm adiantum;
+iv-method encblkno1;
+keylength 256;
+verify_method none;
+keygen storedkey {
+ key AAABAAd3CTYsLjLfDdw/DcR7umOQtsc7tQ+cMSLshErXwrPl;
+ shared "helloworld" algorithm hkdf-hmac-sha256 \
+ subkey AAAAQMxUtCBh7ha6mUU=;
+};
+EOF
+ cat <<EOF >cgd.conf0
+cgd0 /dev/wd0e wd0e
+cgd1 /dev/ld1e ld1e
+EOF
+ cat <<EOF >expected0
+/dev/wd0e: PLJfJfqs1XqQQ09k0DYvKi0tCpDPGlpMXbAtVuzExb8=
+/dev/ld1e: ADxn574yb7sVdxHphNRRdObZxntMJA/ssMuUX6SXgEY=
+EOF
+ cat <<EOF >cgd.conf1
+cgd0 /dev/ld1e ld1e
+cgd1 /dev/wd0e wd0e
+EOF
+ cat <<EOF >expected1
+/dev/ld1e: ADxn574yb7sVdxHphNRRdObZxntMJA/ssMuUX6SXgEY=
+/dev/wd0e: PLJfJfqs1XqQQ09k0DYvKi0tCpDPGlpMXbAtVuzExb8=
+EOF
+ atf_check -o file:expected0 cgdconfig -T -f cgd.conf0
+ atf_check -o file:expected1 cgdconfig -T -f cgd.conf1
+}
+
+atf_test_case sharedshellkeys
+sharedshellkeys_head()
+{
+ atf_set descr "Test multiple shared key generations from shell_cmd"
+}
+sharedshellkeys_body()
+{
+ cat <<EOF >wd0e
+algorithm adiantum;
+iv-method encblkno1;
+keylength 256;
+verify_method none;
+keygen shell_cmd {
+ cmd "${COUNTKEY} n B3cJNiwuMt8N3D8NxHu6Y5C2xzu1D5wxIuyEStfCs+U=";
+ shared "helloworld" algorithm hkdf-hmac-sha256 \
+ subkey AAAAUPDx8vP09fb3+Pk=;
+};
+EOF
+ cat <<EOF >ld1e
+algorithm adiantum;
+iv-method encblkno1;
+keylength 256;
+verify_method none;
+keygen shell_cmd {
+ cmd "${COUNTKEY} n B3cJNiwuMt8N3D8NxHu6Y5C2xzu1D5wxIuyEStfCs+U=";
+ shared "helloworld" algorithm hkdf-hmac-sha256 \
+ subkey AAAAQMxUtCBh7ha6mUU=;
+};
+EOF
+ cat <<EOF >cgd.conf0
+cgd0 /dev/wd0e wd0e
+cgd1 /dev/ld1e ld1e
+EOF
+ cat <<EOF >expected0
+/dev/wd0e: PLJfJfqs1XqQQ09k0DYvKi0tCpDPGlpMXbAtVuzExb8=
+/dev/ld1e: ADxn574yb7sVdxHphNRRdObZxntMJA/ssMuUX6SXgEY=
+EOF
+ cat <<EOF >cgd.conf1
+cgd0 /dev/ld1e ld1e
+cgd1 /dev/wd0e wd0e
+EOF
+ cat <<EOF >expected1
+/dev/ld1e: ADxn574yb7sVdxHphNRRdObZxntMJA/ssMuUX6SXgEY=
+/dev/wd0e: PLJfJfqs1XqQQ09k0DYvKi0tCpDPGlpMXbAtVuzExb8=
+EOF
+ atf_check -o file:expected0 cgdconfig -T -f cgd.conf0
+ atf_check -o inline:'1\n' cat n
+ atf_check -o file:expected1 cgdconfig -T -f cgd.conf1
+ atf_check -o inline:'2\n' cat n
+}
+
atf_init_test_cases()
{
+ atf_add_test_case sharedshellkeys
+ atf_add_test_case sharedstoredkey10
+ atf_add_test_case sharedstoredkey80
+ atf_add_test_case sharedstoredkeys
atf_add_test_case storedkey
atf_add_test_case storedkey2a
atf_add_test_case storedkey2b