From e926662490560dafe60645dac692c9c9ed61c85f Mon Sep 17 00:00:00 2001 From: Ariel Mendelzon Date: Wed, 9 Oct 2024 14:29:31 -0300 Subject: [PATCH] Unit testing endian and keccak256 --- firmware/src/hal/x86/test/endian/Makefile | 39 +++++ .../src/hal/x86/test/endian/test_endian.c | 61 +++++++ firmware/src/hal/x86/test/keccak256/Makefile | 38 +++++ .../hal/x86/test/keccak256/test_keccak256.c | 159 ++++++++++++++++++ firmware/src/hal/x86/test/run-all.sh | 2 +- 5 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 firmware/src/hal/x86/test/endian/Makefile create mode 100644 firmware/src/hal/x86/test/endian/test_endian.c create mode 100644 firmware/src/hal/x86/test/keccak256/Makefile create mode 100644 firmware/src/hal/x86/test/keccak256/test_keccak256.c diff --git a/firmware/src/hal/x86/test/endian/Makefile b/firmware/src/hal/x86/test/endian/Makefile new file mode 100644 index 00000000..6047e5ba --- /dev/null +++ b/firmware/src/hal/x86/test/endian/Makefile @@ -0,0 +1,39 @@ +# The MIT License (MIT) +# +# Copyright (c) 2021 RSK Labs Ltd +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +include ../common.mk + +PROG = test.out +OBJS = test_endian.o endian.o + +all: $(PROG) + +$(PROG): $(OBJS) + $(CC) $(COVFLAGS) -o $@ $^ + +.PHONY: clean test + +clean: + rm -f $(PROG) ./*.o $(COVFILES) + +test: all + ./$(PROG) diff --git a/firmware/src/hal/x86/test/endian/test_endian.c b/firmware/src/hal/x86/test/endian/test_endian.c new file mode 100644 index 00000000..3b6495ba --- /dev/null +++ b/firmware/src/hal/x86/test/endian/test_endian.c @@ -0,0 +1,61 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2021 RSK Labs Ltd + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include + +#include "endian.h" + +void assert_write_uint32_be(const uint32_t n, const uint8_t exp[]) { + uint8_t dest[sizeof(uint32_t)]; + + memset(dest, 0, sizeof(dest)); + write_uint32_be(dest, n); + + for (int i = 0; i < sizeof(dest); i++) { + assert(dest[i] == exp[i]); + } +} + +void test_write_uint32_be() { + printf("Testing write_uint32_be... "); + + assert_write_uint32_be(0xaabbccdd, + (const uint8_t[]){0xaa, 0xbb, 0xcc, 0xdd}); + + assert_write_uint32_be(0x44, (const uint8_t[]){0x0, 0x0, 0x0, 0x44}); + + assert_write_uint32_be(0x4455, (const uint8_t[]){0x0, 0x0, 0x44, 0x55}); + + assert_write_uint32_be(0x445566, (const uint8_t[]){0x0, 0x44, 0x55, 0x66}); + + printf("OK\n"); +} + +int main() { + test_write_uint32_be(); + return 0; +} + diff --git a/firmware/src/hal/x86/test/keccak256/Makefile b/firmware/src/hal/x86/test/keccak256/Makefile new file mode 100644 index 00000000..acfdf22b --- /dev/null +++ b/firmware/src/hal/x86/test/keccak256/Makefile @@ -0,0 +1,38 @@ +# The MIT License (MIT) +# +# Copyright (c) 2021 RSK Labs Ltd +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +include ../common.mk + +PROG = test.out +OBJS = test_keccak256.o keccak256.o + +all: $(PROG) + +$(PROG): $(OBJS) + $(CC) $(COVFLAGS) -o $@ $^ + +.PHONY: clean test +clean: + rm -f $(PROG) *.o $(COVFILES) + +test: all + ./$(PROG) diff --git a/firmware/src/hal/x86/test/keccak256/test_keccak256.c b/firmware/src/hal/x86/test/keccak256/test_keccak256.c new file mode 100644 index 00000000..d410bf02 --- /dev/null +++ b/firmware/src/hal/x86/test/keccak256/test_keccak256.c @@ -0,0 +1,159 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2021 RSK Labs Ltd + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include + +#include "keccak256.h" + +#define KECCAK256_HASH_SIZE 32 + +void assert_keccak256(const uint8_t* data, + const uint16_t data_size, + const uint8_t* expected) { + SHA3_CTX ctx; + uint8_t hash[KECCAK256_HASH_SIZE]; + + keccak_init(&ctx); + keccak_update(&ctx, data, data_size); + keccak_final(&ctx, hash); + + printf("Expected hash: "); + for (int i = 0; i < KECCAK256_HASH_SIZE; i++) { + printf("%02x", expected[i]); + } + printf("\n"); + + printf("Computed hash: "); + for (int i = 0; i < KECCAK256_HASH_SIZE; i++) { + printf("%02x", hash[i]); + } + printf("\n"); + + assert(memcmp(hash, expected, KECCAK256_HASH_SIZE) == 0); +} + +int main() { + { + uint8_t data[] = { + 0xbd, 0x47, 0x7c, 0xdb, 0xd1, 0x03, 0xf2, 0xf3, 0xe1, 0xce, 0x10, + 0x0d, 0xe2, 0xc8, 0xd7, 0x59, 0xea, 0x55, 0xa9, 0xed, 0x16, 0xf7, + 0xe1, 0x07, 0xba, 0x75, 0x46, 0x52, 0x53, 0x4b, 0x42, 0x4c, 0x4f, + 0x43, 0x4b, 0x3a, 0x77, 0x87, 0xe3, 0xb6, 0xa2, 0x26, 0x24, 0x61, + 0x48, 0x66, 0xc2, 0x1d, 0xee, 0x14, 0x62, 0xcc, 0xa6, 0x7a, 0x82, + 0x0d, 0xae, 0xfc, 0x51, 0x34, 0x98, 0xdd, 0xe3, 0xf6, 0xc4, 0x70, + 0xc8, 0xa4, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, 0x2a, + 0x01, 0x00, 0x00, 0x00, 0x23, 0x21, 0x02, 0x98, 0x58, 0xd5, 0x67, + 0x01, 0xbf, 0x60, 0x7d, 0x66, 0xb5, 0x2a, 0xf1, 0xbe, 0x2b, 0xdb, + 0xeb, 0xc9, 0x06, 0xe4, 0x28, 0x89, 0xa7, 0x4a, 0x12, 0x44, 0x6e, + 0x94, 0x0a, 0x68, 0x40, 0xc8, 0x6a, 0xac, 0x00, 0x00, 0x00, 0x00, + }; + + uint8_t expected[] = { + 0x00, 0x70, 0x1a, 0x12, 0x0f, 0x86, 0x03, 0x52, 0x87, 0x3c, 0xa8, + 0x94, 0xb0, 0x25, 0xa0, 0x88, 0xed, 0x1d, 0x41, 0xb3, 0x84, 0xbe, + 0xaf, 0x3d, 0x1c, 0x18, 0xaf, 0xb9, 0x91, 0x83, 0x28, 0xfb, + }; + printf("Case 1:\n"); + assert_keccak256(data, sizeof(data), expected); + } + + { + uint8_t data[] = "thisisamessage"; + + uint8_t expected[] = {0x34, 0x8f, 0x3f, 0xcb, 0x24, 0x99, 0x30, 0x86, + 0x7d, 0x7c, 0x88, 0xdb, 0x8a, 0x47, 0xfa, 0x49, + 0xe5, 0x09, 0x0f, 0xbd, 0xbc, 0x1e, 0x8c, 0xcc, + 0x95, 0x06, 0x2a, 0x18, 0x4d, 0x41, 0xce, 0xa2}; + printf("Case 2:\n"); + assert_keccak256(data, sizeof(data) - 1, expected); + } + + { + uint8_t data[] = { + 0xf9, 0x02, 0x34, 0xa0, 0x8d, 0xa9, 0x6f, 0x21, 0x20, 0xc2, 0xc4, + 0x65, 0x39, 0x69, 0x77, 0x6a, 0xa2, 0x38, 0x82, 0x42, 0xc4, 0xd2, + 0x47, 0x62, 0x0d, 0x68, 0x66, 0xfe, 0xcf, 0x2a, 0x2b, 0x27, 0x7e, + 0x6b, 0xfd, 0x6c, 0xa0, 0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, + 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, + 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, + 0xd4, 0x93, 0x47, 0x94, 0x5b, 0x24, 0x27, 0x72, 0x9a, 0x74, 0x66, + 0x7f, 0x6c, 0xc9, 0xec, 0xe0, 0xb8, 0x50, 0x00, 0x4c, 0x1b, 0x75, + 0xc6, 0x36, 0xa0, 0x25, 0xa7, 0x98, 0x60, 0x13, 0x29, 0xc5, 0x54, + 0x7a, 0x96, 0x25, 0xb4, 0x21, 0x19, 0x6d, 0x6b, 0xcf, 0x2f, 0x4a, + 0x2b, 0x02, 0xe1, 0xf5, 0x04, 0xfe, 0xb0, 0xda, 0xeb, 0x4d, 0xaa, + 0xaf, 0x48, 0xa0, 0x98, 0x12, 0x48, 0x35, 0xd3, 0xfd, 0xb6, 0x38, + 0x8f, 0x0e, 0xc7, 0x85, 0x0f, 0xfb, 0xe2, 0xc2, 0x51, 0x8d, 0x6e, + 0x1a, 0x3c, 0x1b, 0x59, 0xce, 0x1c, 0x4a, 0xa8, 0x27, 0x43, 0x09, + 0xf9, 0xfb, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xb9, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, + 0x10, 0x00, 0x00, 0x08, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, + 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x02, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x20, 0x00, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x82, 0x04, + 0xe2, 0x83, 0x67, 0xc2, 0x80, 0x80, 0x84, 0x5f, 0x35, 0xc0, 0x1f, + 0x92, 0xd1, 0x01, 0x8f, 0x50, 0x41, 0x50, 0x59, 0x52, 0x55, 0x53, + 0x2d, 0x65, 0x66, 0x61, 0x31, 0x61, 0x30, 0x64, 0x80, 0x00, 0x80, + 0x80, 0xb8, 0x50, 0x71, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xa5, 0xa4, 0xc1, 0x10, + 0x74, 0x6d, 0x62, 0x15, 0x31, 0x6f, 0xaf, 0x7c, 0x0c, 0xdd, 0x06, + 0x28, 0x16, 0x2b, 0xea, 0x46, 0xc7, 0x5d, 0xdb, 0x8b, 0x08, 0xb1, + 0x6a, 0xc0, 0xe4, 0x5a, 0x2d, 0x22, 0xc0, 0x35, 0x5f, 0xff, 0xff, + 0x7f, 0x21, 0x00, 0x00, 0x00, 0x06}; + + uint8_t expected[] = {0x98, 0x72, 0x8e, 0x67, 0x93, 0xf9, 0xe9, 0x55, + 0x7a, 0x42, 0xf1, 0x14, 0xb2, 0x55, 0x67, 0xb5, + 0x88, 0x46, 0xae, 0x68, 0xfa, 0xbf, 0xd7, 0x6a, + 0xd6, 0x24, 0xa6, 0x2b, 0x4f, 0x38, 0x57, 0xb8}; + printf("Case 3:\n"); + assert_keccak256(data, sizeof(data), expected); + } + + return 0; +} + diff --git a/firmware/src/hal/x86/test/run-all.sh b/firmware/src/hal/x86/test/run-all.sh index e7cc9829..1a466aa5 100755 --- a/firmware/src/hal/x86/test/run-all.sh +++ b/firmware/src/hal/x86/test/run-all.sh @@ -1,7 +1,7 @@ #!/bin/bash ROOTDIR=$(dirname $0)/../../../../.. TESTDIR=$(realpath $(dirname $0) --relative-to $ROOTDIR) -TESTDIRS="bip32 hmac_sha256" +TESTDIRS="bip32 endian hmac_sha256 keccak256" TESTDIRS=${1:-"$TESTDIRS"} for d in $TESTDIRS; do