From: Yu Watanabe Date: Fri, 19 Jun 2026 17:42:13 +0000 (+0900) Subject: test-fsprg: add unit test for FSPRG X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d95656816051e03021dd90c29b95ed964e8fd1f7;p=thirdparty%2Fsystemd.git test-fsprg: add unit test for FSPRG --- diff --git a/src/test/meson.build b/src/test/meson.build index cd54f4d8dc2..53ff5c3ab78 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -336,6 +336,15 @@ executables += [ 'link_with' : libsystemd, 'type' : 'manual', }, + test_template + { + 'sources' : files( + 'test-fsprg.c', + ), + 'dependencies' : [ + libgcrypt_cflags, + ], + 'conditions' : ['HAVE_GCRYPT'], + }, test_template + { 'sources' : files( 'test-hashmap.c', diff --git a/src/test/test-fsprg.c b/src/test/test-fsprg.c new file mode 100644 index 00000000000..07c758f314e --- /dev/null +++ b/src/test/test-fsprg.c @@ -0,0 +1,413 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#if HAVE_VALGRIND_VALGRIND_H +# include +#endif + +#include "fsprg.h" +#include "gcrypt-util.h" +#include "iovec-util.h" +#include "journal-def.h" +#include "stdio-util.h" +#include "tests.h" + +#define STATE_LENGTH (2 + 2 * FSPRG_RECOMMENDED_SECPAR / 8 + 8) +#define KEY_LENGTH 32 + +static const uint8_t expected_state[4][STATE_LENGTH] = { + { + 0x00, 0x5f, 0xcd, 0x65, 0x12, 0x14, 0xa6, 0x15, + 0xe4, 0xd3, 0xc9, 0xe7, 0x5c, 0x70, 0x2c, 0x37, + 0xca, 0xa7, 0x21, 0xf4, 0x40, 0xf3, 0x6a, 0x88, + 0xe3, 0xce, 0x85, 0x9b, 0xe4, 0x8f, 0x90, 0x8c, + 0x4a, 0xad, 0xf4, 0xcf, 0x99, 0xcf, 0xdc, 0x3b, + 0x29, 0xad, 0x59, 0x72, 0x05, 0xa3, 0xca, 0x3e, + 0xf9, 0x72, 0x9f, 0x7c, 0xb6, 0xa4,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, 0xb7, 0xe4, 0x7f, 0xcd, 0x82, 0x66, + 0x23, 0x7d, 0xf8, 0x57, 0x2c, 0x22, 0x0b, 0x7e, + 0x87, 0x3f, 0xe1, 0xc1, 0x1a, 0xea, 0xc3, 0x42, + 0x40, 0x03, 0xaf, 0x11, 0xd5, 0x07, 0xa1, 0x79, + 0x55, 0x34, 0x4a, 0x41, 0x6a, 0x75, 0x68, 0x13, + 0x37, 0xfd, 0x38, 0x97, 0x52, 0x4f, 0xbb, 0x68, + 0xb4, 0x9f, 0xa8, 0xda, 0x9c, 0x4c, 0x8e, 0x65, + 0x3e, 0x42, 0x19, 0x40, 0x5f, 0x47, 0xe8, 0x50, + 0x0c, 0xb3, 0xc5, 0x6e, 0x0e, 0xe6, 0x6b, 0xe7, + 0xb9, 0x1f, 0xe0, 0x66, 0x2a, 0x38, 0x8e, 0x38, + 0xa8, 0x7f, 0xfd, 0x2c, 0x92, 0xf8, 0xf2, 0x91, + 0x36, 0x90, 0xfd, 0x14, 0x1d, 0x07, 0xae, 0x71, + 0x7f, 0x3c, 0xaa, 0xd6, 0xc4, 0x10, 0x20, 0xe5, + 0xd0, 0xc2, 0xa2, 0xe7, 0x5e, 0x36, 0x91, 0xf7, + 0xff, 0x27, 0x4e, 0x81, 0xc0, 0x85, 0xab, 0x0a, + 0x46, 0x88, 0xe7, 0x32, 0xce, 0x83, 0x4a, 0x09, + 0x72, 0xb3, 0xae, 0xdd, 0x65, 0xf8, 0x13, 0xd0, + 0x9f, 0x4f, 0x45, 0x83, 0xfa, 0x41, 0x1f, 0x8d, + 0xfe, 0x96, 0xbd, 0xe0, 0x32, 0x5c, 0xb9, 0x87, + 0xd8, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, + }, + { + 0x00, 0x5f, 0xcd, 0x65, 0x12, 0x14, 0xa6, 0x15, + 0xe4, 0xd3, 0xc9, 0xe7, 0x5c, 0x70, 0x2c, 0x37, + 0xca, 0xa7, 0x21, 0xf4,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+ 0x56, 0x44, 0x04, 0x10, 0x9e, 0x43, 0x7f, 0x62, + 0x18, 0x1f, 0x5d, 0x68, 0x8f, 0xa8, 0x26, 0xab, + 0xd4, 0xf9, 0xa6, 0xef, 0xfa, 0x3f, 0x41, 0x09, + 0xae, 0x44, 0x8f, 0x9e, 0xd3, 0x4c, 0xc5, 0x7b, + 0x35, 0x50, 0x7f, 0xc0, 0xba, 0xd9, 0x42, 0xe9, + 0x89, 0x9b, 0x4f, 0x30, 0x40, 0x02, 0x96, 0xed, + 0x7c, 0x67, 0x7e, 0xab, 0x26, 0x3b, 0xc9, 0x65, + 0xf6, 0x16, 0x9b, 0x7e, 0x28, 0xf2, 0xed, 0xb9, + 0xfd, 0x9d, 0xe4, 0x68, 0x67, 0x65, 0xf2, 0x5d, + 0x54, 0x43, 0xd5, 0x2d, 0xfd, 0xf4, 0x2d, 0xfd, + 0x49, 0x00, 0xca, 0x35, 0xc9, 0x87, 0x7c, 0xd4, + 0xfe, 0xfb, 0x9d, 0x8d, 0xda, 0x1c, 0x54, 0xc2, + 0x26, 0xed, 0xe5, 0xaa, 0xde, 0xa9, 0x41, 0x88, + 0x88, 0xec, 0x17, 0xab, 0x12, 0x00, 0xc4, 0x71, + 0x8f, 0xe4, 0x5a, 0xf9, 0x55, 0xe6, 0xd9, 0x56, + 0x45, 0x86, 0x6a, 0x1c, 0xd1, 0x09, 0xb1, 0xfc, + 0x0d, 0xd0, 0x15, 0x2c, 0xa1, 0xb6, 0xe9, 0x95, + 0x01, 0xe1, 0x71, 0xb3, 0xb7, 0xbf, 0x57, 0xd5, + 0x15, 0xec, 0xe7, 0xde, 0x85, 0x90, 0xb2, 0x47, + 0x51, 0x0f, 0x0f, 0x9b, 0xcf, 0x21, 0x0c, 0x77, + 0x21, 0xe7, 0xbb, 0x44, 0xbb, 0x70, 0xbf, 0xfb, + 0x29, 0x58, 0x50, 0xdc, 0x01, 0xfb, 0x2b, 0xdf, + 0x8d, 0x74, 0xc5, 0x89, 0xe1, 0x29, 0xcd, 0x97, + 0xe5, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x01, + }, + { + 0x00, 0x5f, 0xcd, 0x65, 0x12, 0x14, 0xa6, 0x15, + 0xe4, 0xd3, 0xc9, 0xe7, 0x5c, 0x70, 0x2c, 0x37, + 0xca, 0xa7, 0x21, 0xf4, 0x40, 0xf3, 0x6a, 0x88, + 0xe3, 0xce, 0x85, 0x9b, 0xe4, 0x8f, 0x90, 0x8c, + 0x4a, 0xad, 0xf4, 0xcf, 0x99, 0xcf, 0xdc, 0x3b, + 0x29, 0xad, 0x59, 0x72, 0x05, 0xa3, 0xca, 0x3e, + 0xf9, 0x72, 0x9f, 0x7c, 0xb6, 0xa4, 0xd6, 0x86, + 0xb4, 0x64, 0x5a, 0xc4, 0xe0, 0x95, 0x98, 0xb7, + 0xdf, 0xc5, 0x79, 0x8a, 0x48, 0xff, 0xd6, 0x71, + 0xab, 0x0f, 0x17, 0x54, 0xdd, 0x3d, 0xd0, 0x23, + 0x18, 0xc9, 0x4c, 0xf4, 0x40, 0xcf, 0x34, 0xc9, + 0x33, 0x04, 0x35, 0x4b, 0x2c, 0xd9, 0xbd, 0xcf, + 0x59, 0x62, 0x18, 0x2d, 0x9e, 0x5a, 0x11, 0x71, + 0x4c, 0x1f, 0x78, 0x0e, 0x65, 0x75, 0xe6, 0x15, + 0x2f, 0x61, 0x32, 0x3c, 0xac, 0xaa, 0xa8, 0x6a, + 0x1d, 0x11, 0xb4, 0x91, 0x38, 0xd3, 0x34, 0x31, + 0xd7, 0xfa, 0xff, 0xef, 0xbf, 0xb9, 0x90, 0x4f, + 0x0d, 0x29, 0x16, 0xb6, 0x64, 0x48, 0x5b, 0x7b, + 0x00, 0x42, 0x7a, 0xab, 0x2b, 0xe7, 0xee, 0x9b, + 0x5d, 0x0c, 0xe9, 0x14, 0x74, 0x72, 0x29, 0xd9, + 0x10, 0xa5, 0xd5, 0x95, 0xbb, 0x9f, 0x85, 0xc0, + 0xa2, 0xfa, 0xbb, 0x84, 0xf5, 0x59, 0x11, 0xdb, + 0xf4, 0xce, 0x08, 0xe4, 0x36, 0x1c, 0xeb, 0xc9, + 0xa6, 0xcd, 0xdb, 0xf2, 0x10, 0xab, 0x51, 0xb2, + 0x8c, 0x9d, 0x57, 0xb9, 0x1b, 0x5d, 0x82, 0x02, + 0x77, 0x8c, 0x96, 0x3e, 0x36, 0x0c, 0x17, 0x8f, + 0x61, 0x3b, 0x3f, 0x17, 0x7d, 0x1d, 0xa9, 0x03, + 0xf8, 0x57, 0x4b, 0xf7, 0x48, 0xb8, 0x65, 0xfc, + 0xbb, 0xd4, 0x2b, 0x19, 0x06, 0xa3, 0x82, 0x61, + 0xe0, 0x21, 0x62, 0x31, 0xb4, 0xd2, 0x5b, 0xf0, + 0x4b, 0x21, 0xcd, 0xca, 0x84, 0x84, 0xc0, 0x1b, + 0x55, 0x4d, 0xf0, 0x32, 0xc9, 0x37, 0x57, 0xd9, + 0x63, 0x1b, 0x00, 0x65, 0x61, 0x93, 0x1d, 0xa4, + 0xb0, 0x86, 0xef, 0x4f, 0x15, 0x88, 0xf8, 0x58, + 0xcd, 0xfb, 0xc6, 0x2b, 0x09, 0xcd, 0x9a, 0x32, + 0x44, 0x97, 0x8e, 0xea, 0xfa, 0x3c, 0x5a, 0x7d, + 0x94, 0xda, 0x09, 0x94, 0x27, 0xa0, 0xc5, 0x57, + 0xa8, 0x2d, 0xc5, 0xc4, 0xdf, 0x7c, 0x5f, 0xb4, + 0x9b, 0x27, 0x0b, 0x62, 0xc1, 0x55, 0xcc, 0x55, + 0xb6, 0xfa, 0xf8, 0x70, 0x66, 0x48, 0xba, 0x21, + 0xe5, 0x9c, 0x27, 0x3f, 0x6b, 0xc5, 0xf3, 0x00, + 0x03, 0x52, 0x90, 0x06, 0xa3, 0xb9, 0xda, 0xee, + 0x87, 0xab, 0x78, 0x96, 0x93, 0x69, 0x34, 0x5a, + 0x04, 0x77, 0xcf, 0x0d, 0x20, 0xa3, 0xd7, 0x42, + 0x27, 0x8d, 0x59, 0xfe, 0x89, 0x67, 0x3e, 0x5d, + 0xe0, 0x0c, 0xb6, 0xad, 0xf0, 0x36, 0x09, 0x60, + 0xc1, 0x7c, 0xce, 0x2d, 0xe4, 0x75, 0xaa, 0xf5, + 0xa3, 0x5f, 0x72, 0x3b, 0xf2, 0x73, 0xb4, 0x9b, + 0xb5, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x02, + }, + { + 0x00, 0x5f, 0xcd, 0x65, 0x12, 0x14, 0xa6, 0x15, + 0xe4, 0xd3, 0xc9, 0xe7, 0x5c, 0x70, 0x2c, 0x37, + 0xca, 0xa7, 0x21, 0xf4, 0x40, 0xf3, 0x6a, 0x88, + 0xe3, 0xce, 0x85, 0x9b, 0xe4, 0x8f, 0x90, 0x8c, + 0x4a, 0xad, 0xf4, 0xcf, 0x99, 0xcf, 0xdc, 0x3b, + 0x29, 0xad, 0x59, 0x72, 0x05, 0xa3, 0xca, 0x3e, + 0xf9, 0x72, 0x9f, 0x7c, 0xb6, 0xa4, 0xd6, 0x86, + 0xb4, 0x64, 0x5a, 0xc4, 0xe0, 0x95, 0x98, 0xb7, + 0xdf, 0xc5, 0x79, 0x8a, 0x48, 0xff, 0xd6, 0x71, + 0xab, 0x0f, 0x17, 0x54, 0xdd, 0x3d, 0xd0, 0x23, + 0x18, 0xc9, 0x4c, 0xf4, 0x40, 0xcf, 0x34, 0xc9, + 0x33, 0x04, 0x35, 0x4b, 0x2c, 0xd9, 0xbd, 0xcf, + 0x59, 0x62, 0x18, 0x2d, 0x9e, 0x5a, 0x11, 0x71, + 0x4c, 0x1f, 0x78, 0x0e, 0x65, 0x75, 0xe6, 0x15, + 0x2f, 0x61, 0x32, 0x3c, 0xac, 0xaa, 0xa8, 0x6a, + 0x1d, 0x11, 0xb4, 0x91, 0x38, 0xd3, 0x34, 0x31, + 0xd7, 0xfa, 0xff, 0xef, 0xbf, 0xb9,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, 0x1b, 0xf3, 0x3c, 0xb5, 0xee, 0x8c, 0x62, + 0x9c, 0x9d, 0x0d, 0xb4, 0x57, 0x6c, 0x8d, 0xf5, + 0xe2, 0x16, 0xc0, 0x62, 0x9c, 0x52, 0x49, 0xf8, + 0xc7, 0xac, 0xb0, 0xa0, 0xe8, 0x81, 0xd9, 0x05, + 0x87, 0xda, 0x09, 0x6f, 0xf7, 0x95, 0xc7, 0x68, + 0x69, 0xbb, 0x68, 0xf4, 0x76, 0x3e, 0x2f, 0xd1, + 0x9d, 0x18, 0xc1, 0x47, 0x59, 0x3d, 0x56, 0x57, + 0x26, 0x10, 0x64, 0xc5, 0x9c, 0x35, 0xe8, 0x58, + 0x1b, 0x0e, 0x20, 0x97, 0xdf, 0xfa, 0xcc, 0x88, + 0xa0, 0xeb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x03, + }, +}, expected_key[4][KEY_LENGTH] = { + { + 0xca, 0xe4, 0x2e, 0xba, 0x74, 0x55, 0x4f, 0x10, + 0x4f, 0x72, 0xa4, 0x3a, 0x0b, 0x00, 0xa0, 0xc8, + 0xd8, 0x8f, 0xa7, 0x11, 0x8d, 0x56, 0x2a, 0x72, + 0xe4, 0xa8, 0xd2, 0x69, 0xf9, 0xd8, 0xa0, 0xf1, + }, + { + 0x63, 0x1d, 0x52, 0x11, 0xed, 0x13, 0xa8, 0x8e, + 0x4c, 0xe0, 0x06, 0x2e, 0xe6, 0xa3, 0xa1, 0xf6, + 0x7a, 0xf1, 0xc4, 0x1e, 0x18, 0xbf, 0x74, 0xe2, + 0x52, 0x23, 0x86, 0x56, 0x1b, 0x0e, 0x78, 0x7d, + }, + { + 0x3f, 0xb2, 0xb6, 0x39, 0xe3, 0x54, 0x41, 0x04, + 0xf3, 0x48, 0x5f, 0x50, 0xf7, 0x8b, 0xd6, 0xe7, + 0x8e, 0x45, 0xc2, 0xee, 0xbf, 0xdd, 0xfd, 0x83, + 0x34, 0x7a, 0x9a, 0x8a, 0xf3, 0x4d, 0xd4, 0xc6, + }, + { + 0x98, 0x76, 0x29, 0xe2, 0x27, 0xf0, 0x0e, 0x2a, + 0xff, 0xc8, 0xfd, 0x3b, 0xcf, 0xcb, 0x9f, 0x6d, + 0xe9, 0x80, 0xfe, 0x6e, 0xd9, 0xc0, 0x43, 0x29, + 0xc5, 0x53, 0x5f, 0xf7, 0x86, 0x33, 0x3b, 0x70, + }, +}, expected_tag[4][TAG_LENGTH] = { + { + 0x10, 0xa4, 0x14, 0x19, 0xec, 0xf1, 0x9b, 0x6d, + 0xe5, 0xe3, 0x12, 0x2b, 0x06, 0xee, 0x5b, 0xac, + 0xa7, 0x11, 0x0b, 0x70, 0x15, 0xfd, 0x47, 0xa4, + 0xb9, 0xd7, 0x45, 0x19, 0x4b, 0xde, 0x92, 0x5d, + }, + { + 0x75, 0x54, 0x5b, 0x6a, 0xfb, 0x30, 0x43, 0x3b, + 0x29, 0x94, 0x58, 0xee, 0x62, 0xd9, 0x62, 0x2b, + 0x61, 0x88, 0xed, 0xce, 0xc9, 0x95, 0x1e, 0x82, + 0x9e, 0xd6, 0xd1, 0x90, 0x23, 0xc6, 0x90, 0x65, + }, + { + 0x2f, 0x8b, 0x64, 0xa4, 0x21, 0xe8, 0x74, 0x93, + 0xa2, 0x3a, 0x6a, 0xa9, 0x0d, 0x7e, 0xb0, 0x80, + 0xce, 0x74, 0x80, 0xb9, 0x06, 0xae, 0xec, 0x86, + 0x4c, 0x32, 0x99, 0xf9, 0x48, 0x1f, 0x0c, 0xad, + }, + { + 0x39, 0x9e, 0xf6, 0xc3, 0x1e, 0xca, 0x02, 0xb2, + 0x6a, 0xe2, 0xa6, 0x47, 0x0b, 0xb5, 0xb6, 0xaf, + 0x46, 0xd7, 0x99, 0x13, 0xc6, 0x07, 0xa8, 0x80, + 0x73, 0x35, 0x75, 0x73, 0xfd, 0x03, 0x5b, 0x3e, + }, +}; + +static void generate_seed(struct iovec *seed) { + assert(iovec_is_set(seed)); + + uint8_t *p = seed->iov_base; + for (size_t i = 0; i < seed->iov_len; i++) + *p++ = (uint8_t) i; +} + +TEST(fsprg) { + size_t n_states = 20; + + /* Generating private keys is slow when running on valgrind or with sanitizers. */ +#if HAVE_VALGRIND_VALGRIND_H + if (RUNNING_ON_VALGRIND) + n_states = ELEMENTSOF(expected_state); +#endif +#if HAS_FEATURE_ADDRESS_SANITIZER + n_states = ELEMENTSOF(expected_state); +#endif + + struct iovec *states = new0(struct iovec, n_states); + CLEANUP_ARRAY(states, n_states, iovec_array_free); + + if (initialize_libgcrypt(false) >= 0) { + struct iovec seed = IOVEC_ALLOCA(FSPRG_RECOMMENDED_SEEDLEN), + msk = IOVEC_ALLOCA(FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR)), + mpk = IOVEC_ALLOCA(FSPRG_mpkinbytes(FSPRG_RECOMMENDED_SECPAR)), + state = IOVEC_ALLOCA(FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR)), + key = IOVEC_ALLOCA(KEY_LENGTH); + + generate_seed(&seed); + + /* Generate secret keys */ + ASSERT_OK(FSPRG_GenMK(msk.iov_base, mpk.iov_base, seed.iov_base, seed.iov_len, FSPRG_RECOMMENDED_SECPAR)); + + /* Generate states by FSPRG_Seek() */ + for (size_t i = 0; i < n_states; i++) { + ASSERT_OK(FSPRG_Seek(state.iov_base, i, msk.iov_base, seed.iov_base, seed.iov_len)); + + /* Verify state */ + if (i < ELEMENTSOF(expected_state)) + ASSERT_TRUE(iovec_equal(&state, &IOVEC_MAKE(expected_state[i], sizeof(expected_state[i])))); + + /* Verify key */ + ASSERT_OK(FSPRG_GetKey(state.iov_base, key.iov_base, key.iov_len, 0)); + if (i < ELEMENTSOF(expected_key)) + ASSERT_TRUE(iovec_equal(&key, &IOVEC_MAKE(expected_key[i], sizeof(expected_key[i])))); + + /* Verify epoch */ + ASSERT_EQ(FSPRG_GetEpoch(state.iov_base), (uint64_t) i); + + ASSERT_NOT_NULL(iovec_memdup(&state, &states[i])); + } + + /* Generate state by FSPRG_GetState0() */ + ASSERT_OK(FSPRG_GenState0(state.iov_base, mpk.iov_base, seed.iov_base, seed.iov_len)); + ASSERT_TRUE(iovec_equal(&state, &states[0])); + + /* Generate states by FSPRG_Evolve() */ + for (size_t i = 1; i < n_states; i++) { + ASSERT_OK(FSPRG_Evolve(state.iov_base)); + ASSERT_TRUE(iovec_equal(&state, &states[i])); + } + } +} + +TEST(hmac) { + /* This emulates what journald and journalctl does. + * 1. journalctl generates random seed, calculate initial state, and write the state in + * /var/log/journal/$MACHINE_ID/fss. + * 2. journald loads the fss file when a new journal file is created by journal_file_fss_load(). + * 3. Allocate HMAC with SHA256 by journal_file_hmac_setup(). + * 4. Write inital tag in journal file by journal_file_append_first_tag(). + * 5. For each journal entry, + * - initialize HMAC with fsprg key by journal_file_hmac_start(), + * - put each object data to the HMAC by journal_file_hmac_put_object(), + * - finalize HMAC and write the value with the fsprg epoch to a tag object by + * journal_file_maybe_append_tag(). */ + + size_t n_tags = 20; + +#if HAVE_GCRYPT + if (initialize_libgcrypt(false) >= 0) { + struct iovec seed = IOVEC_ALLOCA(FSPRG_RECOMMENDED_SEEDLEN), + msk = IOVEC_ALLOCA(FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR)), + mpk = IOVEC_ALLOCA(FSPRG_mpkinbytes(FSPRG_RECOMMENDED_SECPAR)), + state = IOVEC_ALLOCA(FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR)), + key = IOVEC_ALLOCA(KEY_LENGTH), + tag = IOVEC_ALLOCA(TAG_LENGTH); + + generate_seed(&seed); + + /* Generate secret keys */ + ASSERT_OK(FSPRG_GenMK(msk.iov_base, mpk.iov_base, seed.iov_base, seed.iov_len, FSPRG_RECOMMENDED_SECPAR)); + + /* Calculate initial state */ + ASSERT_OK(FSPRG_GenState0(state.iov_base, mpk.iov_base, seed.iov_base, seed.iov_len)); + + /* Allocate HMAC */ + _cleanup_(sym_gcry_md_closep) gcry_md_hd_t hmac = NULL; + ASSERT_EQ(gcry_err_code(sym_gcry_md_open(&hmac, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC)), (gcry_err_code_t) GPG_ERR_NO_ERROR); + + for (size_t i = 0; i < n_tags; i++) { + /* Reset the previous state */ + sym_gcry_md_reset(hmac); + + /* Initialize HMAC with fsprg key */ + ASSERT_OK(FSPRG_GetKey(state.iov_base, key.iov_base, key.iov_len, 0)); + ASSERT_EQ(gcry_err_code(sym_gcry_md_setkey(hmac, key.iov_base, key.iov_len)), (gcry_err_code_t) GPG_ERR_NO_ERROR); + + /* Put dummy data */ + _cleanup_free_ char *str = ASSERT_NOT_NULL(asprintf_safe("dummy data: %zu, hoge hoge hoge hoge hoge", i)); + sym_gcry_md_write(hmac, str, strlen(str)); + + /* Finalize HMAC and generate tag. */ + memcpy(tag.iov_base, sym_gcry_md_read(hmac, 0), tag.iov_len); + + /* Verify tag */ + if (i < ELEMENTSOF(expected_tag)) + ASSERT_TRUE(iovec_equal(&tag, &IOVEC_MAKE(expected_tag[i], sizeof(expected_tag[i])))); + + /* Increment epoch */ + ASSERT_OK(FSPRG_Evolve(state.iov_base)); + } + } +#endif +} + +static int intro(void) { + if (DLOPEN_GCRYPT(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED) < 0) + return EXIT_TEST_SKIP; + + return EXIT_SUCCESS; +} + +DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);