[PATCH v2 3/3] Add some basic test programs for libfsverity

Eric Biggers
2020-05-25T20:55:19Z

From: Eric Biggers <ebiggers@google.com>

Add three test programs: 'test_hash_algs', 'test_compute_digest', and
'test_sign_digest'. Nothing fancy yet, just some basic tests to test
each library function.

With the new Makefile, these get run by 'make check'.

Reviewed-by: Jes Sorensen <jsorensen@fb.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
programs/test_compute_digest.c | 61 +++++++++++++++++++++++++++++++++
programs/test_hash_algs.c | 38 ++++++++++++++++++++
programs/test_sign_digest.c | 50 +++++++++++++++++++++++++++
testdata/cert.pem | 31 +++++++++++++++++
testdata/file.sig | Bin 0 -> 708 bytes
testdata/key.pem | 52 ++++++++++++++++++++++++++++
6 files changed, 232 insertions(+)
create mode 100644 programs/test_compute_digest.c
create mode 100644 programs/test_hash_algs.c
create mode 100644 programs/test_sign_digest.c
create mode 100644 testdata/cert.pem
create mode 100644 testdata/file.sig
create mode 100644 testdata/key.pem

diff --git a/programs/test_compute_digest.c b/programs/test_compute_digest.c
new file mode 100644
index 0000000..fab64a4
--- /dev/null
+++ b/programs/test_compute_digest.c
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Test libfsverity_compute_digest().
+ *
+ * Copyright 2020 Google LLC
+ */
+#include "utils.h"
+
+#define SHA256_DIGEST_SIZE 32
+
+struct mem_file {
+ u8 *data;
+ size_t size;
+ size_t offset;
+};
+
+static int read_fn(void *fd, void *buf, size_t count)
+{
+ struct mem_file *f = fd;
+
+ ASSERT(count <= f->size - f->offset);
+ memcpy(buf, &f->data[f->offset], count);
+ f->offset += count;
+ return 0;
+}
+
+int main(void)
+{
+ struct mem_file f = { .size = 1000000 };
+ size_t i;
+ const struct libfsverity_merkle_tree_params params = {
+ .version = 1,
+ .hash_algorithm = FS_VERITY_HASH_ALG_SHA256,
+ .block_size = 4096,
+ .salt_size = 4,
+ .salt = (u8 *)"abcd",
+ .file_size = f.size,
+ };
+ struct libfsverity_digest *d;
+ static const u8 expected_digest[SHA256_DIGEST_SIZE] =
+ "\x91\x79\x00\xb0\xd2\x99\x45\x4a\xa3\x04\xd5\xde\xbc\x6f\x39"
+ "\xe4\xaf\x7b\x5a\xbe\x33\xbd\xbc\x56\x8d\x5d\x8f\x1e\x5c\x4d"
+ "\x86\x52";
+ int err;
+
+ f.data = xmalloc(f.size);
+ for (i = 0; i < f.size; i++)
+ f.data[i] = (i % 11) + (i % 439) + (i % 1103);
+
+ err = libfsverity_compute_digest(&f, read_fn, &params, &d);
+ ASSERT(err == 0);
+
+ ASSERT(d->digest_algorithm == FS_VERITY_HASH_ALG_SHA256);
+ ASSERT(d->digest_size == SHA256_DIGEST_SIZE);
+ ASSERT(!memcmp(d->digest, expected_digest, SHA256_DIGEST_SIZE));
+
+ free(f.data);
+ free(d);
+ printf("test_compute_digest passed\n");
+ return 0;
+}
diff --git a/programs/test_hash_algs.c b/programs/test_hash_algs.c
new file mode 100644
index 0000000..80d7a07
--- /dev/null
+++ b/programs/test_hash_algs.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Test the hash algorithm-related libfsverity APIs.
+ *
+ * Copyright 2020 Google LLC
+ */
+#include "utils.h"
+
+#define SHA256_DIGEST_SIZE 32
+#define SHA512_DIGEST_SIZE 64
+
+int main(void)
+{
+ ASSERT(libfsverity_get_digest_size(0) == -1);
+ ASSERT(libfsverity_get_hash_name(0) == NULL);
+ ASSERT(libfsverity_find_hash_alg_by_name("bad") == 0);
+ ASSERT(libfsverity_find_hash_alg_by_name(NULL) == 0);
+
+ ASSERT(libfsverity_get_digest_size(100) == -1);
+ ASSERT(libfsverity_get_hash_name(100) == NULL);
+
+ ASSERT(libfsverity_get_digest_size(FS_VERITY_HASH_ALG_SHA256) ==
+ SHA256_DIGEST_SIZE);
+ ASSERT(!strcmp("sha256",
+ libfsverity_get_hash_name(FS_VERITY_HASH_ALG_SHA256)));
+ ASSERT(libfsverity_find_hash_alg_by_name("sha256") ==
+ FS_VERITY_HASH_ALG_SHA256);
+
+ ASSERT(libfsverity_get_digest_size(FS_VERITY_HASH_ALG_SHA512) ==
+ SHA512_DIGEST_SIZE);
+ ASSERT(!strcmp("sha512",
+ libfsverity_get_hash_name(FS_VERITY_HASH_ALG_SHA512)));
+ ASSERT(libfsverity_find_hash_alg_by_name("sha512") ==
+ FS_VERITY_HASH_ALG_SHA512);
+
+ printf("test_hash_algs passed\n");
+ return 0;
+}
diff --git a/programs/test_sign_digest.c b/programs/test_sign_digest.c
new file mode 100644
index 0000000..52738e4
--- /dev/null
+++ b/programs/test_sign_digest.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Test libfsverity_sign_digest().
+ *
+ * Copyright 2020 Google LLC
+ */
+#include "utils.h"
+
+#include <fcntl.h>
+
+#define SHA256_DIGEST_SIZE 32
+
+int main(void)
+{
+ struct libfsverity_digest *d = xzalloc(sizeof(*d) + SHA256_DIGEST_SIZE);
+ const struct libfsverity_signature_params params = {
+ .keyfile = "testdata/key.pem",
+ .certfile = "testdata/cert.pem",
+ };
+ u8 *sig;
+ size_t sig_size;
+ struct filedes file;
+ u8 *expected_sig;
+ u64 expected_sig_size;
+ int err;
+
+ d->digest_algorithm = FS_VERITY_HASH_ALG_SHA256;
+ d->digest_size = SHA256_DIGEST_SIZE;
+ memcpy(d->digest,
+ "\x91\x79\x00\xb0\xd2\x99\x45\x4a\xa3\x04\xd5\xde\xbc\x6f\x39"
+ "\xe4\xaf\x7b\x5a\xbe\x33\xbd\xbc\x56\x8d\x5d\x8f\x1e\x5c\x4d"
+ "\x86\x52", SHA256_DIGEST_SIZE);
+
+ err = libfsverity_sign_digest(d, &params, &sig, &sig_size);
+ ASSERT(err == 0);
+
+ ASSERT(open_file(&file, "testdata/file.sig", O_RDONLY, 0));
+ ASSERT(get_file_size(&file, &expected_sig_size));
+ ASSERT(sig_size == expected_sig_size);
+ expected_sig = xmalloc(sig_size);
+ ASSERT(full_read(&file, expected_sig, sig_size));
+ ASSERT(!memcmp(sig, expected_sig, sig_size));
+
+ free(d);
+ free(sig);
+ free(expected_sig);
+ filedes_close(&file);
+ printf("test_sign_digest passed\n");
+ return 0;
+}
diff --git a/testdata/cert.pem b/testdata/cert.pem
new file mode 100644
index 0000000..c63b965
--- /dev/null
+++ b/testdata/cert.pem
@@ -0,0 +1,31 @@
+-----BEGIN CERTIFICATE-----
+MIIFazCCA1OgAwIBAgIUYaRYcyZGDIv9fIxx/RoJwQu23+owDQYJKoZIhvcNAQEL
+BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
+GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDA1MTUwMjUyMzFaFw0yMDA2
+MTQwMjUyMzFaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
+HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggIiMA0GCSqGSIb3DQEB
+AQUAA4ICDwAwggIKAoICAQC5lXk9otBz/VM/tbvBBK6sK//HE+q3ctY4+fPVv3Ob
+D1YNYuWRD59U+7K8fVUfZlXyjgxt3n4VPzkLgRSr/w5YUTa5NEOVJIltKT4ugswL
+5oY8eRVSuIr1O+vTbu+EpUk3DhTaFkalVzwspwipBeiVTDO9kh0NAueRk2HyLJte
+IoPyfzSCKxg9sED6/WtLFqrhDb5+1qeGoMNGM66ueWXKX0QjomMEODGXC04ypIY3
+sTwB+sYhZUe3YRpY0HyaVNh/6cxCxSiKr2jkC5UL+ry+46EJerNZbKmeqqyqmhXh
+P2cHv8MO91zdH1xbXUUenLcdpK/0oq+/sTAVV1/qPvnAofpN8tdZdrH65JD753jt
+s+lH/f0iGuKAb9ZpLOTM2d3wjY13OcHElj0zu2Usw9PXQpTK/DYlbcapOI4NTVyU
+NpK3yP4i5dPnkHoxpjLWz75Eq6gP9ZXohGq3YG0Lx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

literal 0
HcmV?d00001

diff --git a/testdata/key.pem b/testdata/key.pem
new file mode 100644
index 0000000..e76db4c
--- /dev/null
+++ b/testdata/key.pem
@@ -0,0 +1,52 @@
+-----BEGIN PRIVATE KEY-----
+MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQC5lXk9otBz/VM/
+tbvBBK6sK//HE+q3ctY4+fPVv3ObD1YNYuWRD59U+7K8fVUfZlXyjgxt3n4VPzkL
+gRSr/w5YUTa5NEOVJIltKT4ugswL5oY8eRVSuIr1O+vTbu+EpUk3DhTaFkalVzws
+pwipBeiVTDO9kh0NAueRk2HyLJteIoPyfzSCKxg9sED6/WtLFqrhDb5+1qeGoMNG
+M66ueWXKX0QjomMEODGXC04ypIY3sTwB+sYhZUe3YRpY0HyaVNh/6cxCxSiKr2jk
+C5UL+ry+46EJerNZbKmeqqyqmhXhP2cHv8MO91zdH1xbXUUenLcdpK/0oq+/sTAV
+V1/qPvnAofpN8tdZdrH65JD753jts+lH/f0iGuKAb9ZpLOTM2d3wjY13OcHElj0z
+u2Usw9PXQpTK/DYlbcapOI4NTVyUNpK3yP4i5dPnkHoxpjLWz75Eq6gP9ZXohGq3
+YG0LxtvELWfaFmpzEUTlD59hJJOZELGxAXzsxLbelX/EmpX+GIqnFBpdMIuPO4HE
+fJwD5IcdeHqGl2iH9LIqsY5DcGj4hnqYplIYYk5mWgbhkexRbJIGNdn8WyXlraVp
+2MoSr3p7xJbmo0qYRRtt3kQShzDG0FrZX7wqxemc/g/hr+g2xqMQj0nYLehDeodq
+xA2n9grDUr4AdgQyxXDMUkGZdrg3cwIDAQABAoICACla0aWWfnUaYk60JJ6ieHoN
+Y/XszkUK5gnUSS28d/p5tGdPPnDQ1mSNogq2sx1IJKbkWIizJ818RS33GbAqKfws
+PNGQf+7gMW+N3TloFCgiuo8HPGUukmiLbcWz1tPsMSB/ls3yYNO/WL1qi1d+5ZE/
+Zdg8kxSvLQMXoJ/iqMyVTGnhRsYq7D/y4s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