Set up meson, ninja, and GoogleTest
Juniper Beatitudes [email protected]
Thu, 01 Jan 2026 15:55:31 -0600
11 files changed,
92 insertions(+),
155 deletions(-)
A
.gitmodules
@@ -0,0 +1,3 @@
+[submodule "tests/googletest"] + path = tests/googletest + url = https://github.com/google/googletest.git
A
meson.build
@@ -0,0 +1,44 @@
+project('TinyCrypT', 'c', version: '0.1.0') + +incdir = include_directories('tinycrypt') + +sources = [ + 'tinycrypt/chacha20-poly1305.c', + 'tinycrypt/ed25519.c', + 'tinycrypt/kangarootwelve128.c', + 'tinycrypt/sha2.c', + 'tinycrypt/x25519.c', +] + +build_args = [ + '-Os', + '-ffreestanding', +] + +target = static_library('tinycrypt', sources, c_args: build_args, include_directories: incdir) + +project_dep = declare_dependency( + include_directories: incdir, + link_with : target +) +set_variable(meson.project_name() + '_dep', project_dep) + +test_sources = [ + 'tests/src/sha256.cpp', +] + +if not meson.is_subproject() + add_languages('cpp') + subdir('tests') + test( + 'all_tests', + executable( + 'run_tests', + test_sources, + include_directories: incdir, + link_with: target, + dependencies: test_dep, + install: false + ) + ) +endif
A
tests/meson.build
@@ -0,0 +1,20 @@
+# Builds google test as a dependency called "test_dep". + +gtest_dir = 'googletest/googletest' +gtest_incdir = include_directories(join_paths(gtest_dir, 'include'), is_system : true) + +libgtest = static_library( + 'gtest', + cpp_args : ['-w'], + include_directories : [include_directories(gtest_dir), gtest_incdir], + sources : [ + join_paths(gtest_dir, 'src', 'gtest-all.cc'), + join_paths(gtest_dir, 'src', 'gtest_main.cc') + ] +) + +test_dep = declare_dependency( + dependencies : dependency('threads'), + include_directories : gtest_incdir, + link_with : libgtest +)
A
tests/src/sha256.cpp
@@ -0,0 +1,20 @@
+extern "C" +{ +#include "tinycrypt/sha2.h" +} +#include "gtest/gtest.h" +#include <cstdint> +#include <cstring> +#include <cstddef> + +TEST (TinyCrypT, empty_sha256) +{ + uint8_t digest[32]; + uint8_t reference[32] = { + 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, + 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, + 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, + }; + tct_sha256(NULL, 0, digest); + EXPECT_EQ (memcmp (digest, reference, 32), 0); +}
M
tinycrypt/ed25519.c
→
tinycrypt/ed25519.c
@@ -14,16 +14,6 @@ return (u << 8) | x[0];
} static void -to_le32 (uint32_t u, uint8_t *x) -{ - for (unsigned int i = 0; i < 4; ++i) - { - x[i] = u & 0xFF; - u >>= 8; - } -} - -static void to_le64 (uint64_t u, uint8_t *x) { for (unsigned int i = 0; i < 8; ++i)@@ -117,17 +107,6 @@ out[32] = in[63] >> 7;
} static bool -iszero264 (const uint8_t *a) -{ - uint8_t dummy = 0x0; - for (unsigned int i = 0; i < 33; ++i) - { - dummy |= a[i]; - } - return !dummy; -} - -static bool iszero256 (const uint8_t *a) { uint8_t dummy = 0x0;@@ -335,9 +314,7 @@
static void pow256_2523_modp (const uint8_t *in, uint8_t *out) { - uint8_t buf[64]; - uint8_t *i0 = buf; - uint8_t *i1 = buf + 32; + uint8_t i0[32]; for (unsigned int i = 0; i < 32; ++i) { i0[i] = in[i];@@ -559,16 +536,6 @@ }
} static void -shr512_by_254 (const uint8_t *in, uint8_t *out) -{ - for (unsigned int i = 0; i < 32; ++i) - { - out[i] = (in[i + 31] >> 6) | (in[i + 32] << 2); - } - out[32] = in[63] >> 6; -} - -static void shl512 (const uint8_t *in, const uint64_t shift, uint8_t *out) { for (uint64_t i = 0; i < 64; ++i)@@ -605,7 +572,7 @@ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, }; - uint8_t i0[64], i1[64], i2[33]; + uint8_t i0[64]; for (unsigned int i = 0; i < 64; ++i) { i0[i] = x[i];
D
tinycrypt/tests/harness.c
@@ -1,39 +0,0 @@
-#include <stdbool.h> -#include <stddef.h> -#include <stdio.h> -#include <sys/time.h> - -#include "tinycrypt/tests/harness.h" - -void -tct_testing_init_ctx (struct tct_testing_ctx *ctx) -{ - ctx->tests_passed = 0; - ctx->tests_failed = 0; -} - -void -tct_testing_assert (struct tct_testing_ctx *ctx, bool value, const char *name) -{ - if (!value) - { - ctx->tests_failed++; - printf ("Test '%s' failed!\n", name); - } - else - { - ctx->tests_passed++; - } -} - -void -tct_testing_start (struct tct_testing_ctx *ctx) -{ - gettimeofday (&(ctx->start), NULL); -} - -void -tct_testing_end (struct tct_testing_ctx *ctx) -{ - gettimeofday (&(ctx->end), NULL); -}
D
tinycrypt/tests/harness.h
@@ -1,25 +0,0 @@
-#ifndef TCT_TEST_HARNESS_H -#define TCT_TEST_HARNESS_H - -#include <stdbool.h> -#include <stddef.h> -#include <sys/time.h> - -struct tct_testing_ctx -{ - size_t tests_passed; - size_t tests_failed; - struct timeval start; - struct timeval end; -}; - -void tct_testing_init_ctx (struct tct_testing_ctx *ctx); - -void tct_testing_assert (struct tct_testing_ctx *ctx, bool value, - const char *name); - -void tct_testing_start (struct tct_testing_ctx *ctx); - -void tct_testing_end (struct tct_testing_ctx *ctx); - -#endif
D
tinycrypt/tests/main.c
@@ -1,32 +0,0 @@
-#include <stdbool.h> -#include <stddef.h> -#include <stdio.h> -#include <string.h> - -#include "tinycrypt/sha2.h" -#include "tinycrypt/tests/harness.h" - -int -main (int argc, const char **argv) -{ - struct tct_testing_ctx ctx; - tct_testing_init_ctx (&ctx); - printf ("Beginning tests\n"); - tct_testing_start (&ctx); - - uint8_t buf[32]; - tct_sha256 (NULL, 0, buf); - uint8_t reference[] = { - 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, - 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, - 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, - }; - tct_testing_assert (&ctx, !memcmp (buf, reference, sizeof (buf)), - "Empty SHA-256"); - - tct_testing_end (&ctx); - printf ("%lu tests passed, %lu tests failed, %lu microseconds elapsed\n", - ctx.tests_passed, ctx.tests_failed, - (1000000 * ctx.end.tv_sec + ctx.end.tv_usec) - - (1000000 * ctx.start.tv_sec + ctx.start.tv_usec)); -}
M
tinycrypt/x25519.c
→
tinycrypt/x25519.c
@@ -13,16 +13,6 @@ return (u << 8) | x[0];
} static void -to_le32 (uint32_t u, uint8_t *x) -{ - for (unsigned int i = 0; i < 4; ++i) - { - x[i] = u & 0xFF; - u >>= 8; - } -} - -static void to_le64 (uint64_t u, uint8_t *x) { for (unsigned int i = 0; i < 8; ++i)@@ -113,17 +103,6 @@ {
out[i] = (in[i + 31] >> 7) | (in[i + 32] << 1); } out[32] = in[63] >> 7; -} - -static bool -iszero264 (const uint8_t *a) -{ - uint8_t dummy = 0x0; - for (unsigned int i = 0; i < 33; ++i) - { - dummy |= a[i]; - } - return !dummy; } static void@@ -382,7 +361,6 @@ swap256 (swap, x2, x3);
swap256 (swap, z2, z3); } - uint8_t intermediate[32]; inv256_modp (z2, z2); mult256_modp (x2, z2, out); }