From 3c4800e59452a7124c34f563d548e09849d59b8d Mon Sep 17 00:00:00 2001 From: Marius Schilder Date: Fri, 23 Feb 2018 15:41:27 -0800 Subject: ec: add crc32_ctx..() to take context parameter. Add crc32_ctx.. functions to take context parameter. This allows for multiple instances to exist in parallel. Signed-off-by: mschilder@google.com TEST=make buildall -j8 succeeds BRANCH=none BUG=b:73832883 Change-Id: I66bbc56377eeebf01c790caad0bc4c7a51a1bc58 Reviewed-on: https://chromium-review.googlesource.com/935825 Commit-Ready: Marius Schilder Tested-by: Marius Schilder Reviewed-by: Marius Schilder Reviewed-by: Vadim Bendebury --- common/crc.c | 39 +++++++++++++++++++++++----- include/crc.h | 14 ++++++++++ test/build.mk | 2 ++ test/crc32.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/crc32.tasklist | 17 ++++++++++++ test/test_config.h | 4 +++ 6 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 test/crc32.c create mode 100644 test/crc32.tasklist diff --git a/common/crc.c b/common/crc.c index 31054ad732..79d405eb13 100644 --- a/common/crc.c +++ b/common/crc.c @@ -56,9 +56,6 @@ static const uint32_t crc32_tab[] = { 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; -/* Accumulator for the CRC */ -static uint32_t crc_; - static uint32_t crc32_hash(uint32_t crc, const void *buf, int size) { const uint8_t *p; @@ -73,22 +70,50 @@ static uint32_t crc32_hash(uint32_t crc, const void *buf, int size) return crc; } +void crc32_ctx_init(uint32_t *crc) +{ + *crc = CRC32_INITIAL; +} + +void crc32_ctx_hash32(uint32_t *crc, uint32_t val) +{ + *crc = crc32_hash(*crc, &val, sizeof(val)); +} + +void crc32_ctx_hash16(uint32_t *crc, uint16_t val) +{ + *crc = crc32_hash(*crc, &val, sizeof(val)); +} + +void crc32_ctx_hash8(uint32_t *crc, uint8_t val) +{ + *crc = crc32_hash(*crc, &val, sizeof(val)); +} + +uint32_t crc32_ctx_result(uint32_t *crc) +{ + return *crc ^ 0xFFFFFFFF; +} + +/* Accumulator for the CRC */ +static uint32_t crc_; + void crc32_init(void) { - crc_ = CRC32_INITIAL; + crc32_ctx_init(&crc_); } void crc32_hash32(uint32_t val) { - crc_ = crc32_hash(crc_, &val, sizeof(uint32_t)); + crc32_ctx_hash32(&crc_, val); } void crc32_hash16(uint16_t val) { - crc_ = crc32_hash(crc_, &val, sizeof(uint16_t)); + crc32_ctx_hash16(&crc_, val); } uint32_t crc32_result(void) { - return crc_ ^ 0xFFFFFFFF; + return crc32_ctx_result(&crc_); } diff --git a/include/crc.h b/include/crc.h index 6615885f27..e3b5e4ffd1 100644 --- a/include/crc.h +++ b/include/crc.h @@ -14,6 +14,8 @@ /* Use software implementation */ +/* Static context variant */ + void crc32_init(void); void crc32_hash32(uint32_t val); @@ -22,6 +24,18 @@ void crc32_hash16(uint16_t val); uint32_t crc32_result(void); +/* Provided context variant */ + +void crc32_ctx_init(uint32_t *ctx); + +void crc32_ctx_hash32(uint32_t *ctx, uint32_t val); + +void crc32_ctx_hash16(uint32_t *ctx, uint16_t val); + +void crc32_ctx_hash8(uint32_t *ctx, uint8_t val); + +uint32_t crc32_ctx_result(uint32_t *ctx); + #endif /* CONFIG_HW_CRC */ #endif /* __CROS_EC_CRC_H */ diff --git a/test/build.mk b/test/build.mk index 9c5dd53349..2bcd163655 100644 --- a/test/build.mk +++ b/test/build.mk @@ -39,6 +39,7 @@ test-list-host += charge_manager test-list-host += charge_manager_drp_charging test-list-host += charge_ramp test-list-host += console_edit +test-list-host += crc32 test-list-host += entropy test-list-host += extpwr_gpio test-list-host += fan @@ -89,6 +90,7 @@ charge_manager-y=charge_manager.o charge_manager_drp_charging-y=charge_manager.o charge_ramp-y+=charge_ramp.o console_edit-y=console_edit.o +crc32-y=crc32.o entropy-y=entropy.o extpwr_gpio-y=extpwr_gpio.o fan-y=fan.o diff --git a/test/crc32.c b/test/crc32.c new file mode 100644 index 0000000000..e3f6f8d5c6 --- /dev/null +++ b/test/crc32.c @@ -0,0 +1,75 @@ +/* Copyright 2016 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Tests crc32 sw implementation. + */ + +#include "common.h" +#include "console.h" +#include "crc.h" +#include "test_util.h" +#include "util.h" + +// test that static version matches context version +static int test_static(void) +{ + uint32_t crc; + const uint32_t input = 0xdeadbeef; + + crc32_init(); + crc32_hash32(input); + + crc32_ctx_init(&crc); + crc32_ctx_hash32(&crc, input); + + TEST_ASSERT(crc32_result() == crc32_ctx_result(&crc)); + + return EC_SUCCESS; +} + +// test that context bytes at a time matches static word at time +static int test_8(void) +{ + uint32_t crc; + const uint32_t input = 0xdeadbeef; + const uint8_t *p = (const uint8_t *) &input; + int i; + + crc32_init(); + crc32_hash32(input); + + crc32_ctx_init(&crc); + for (i = 0; i < sizeof(input); ++i) + crc32_ctx_hash8(&crc, p[i]); + + TEST_ASSERT(crc32_result() == crc32_ctx_result(&crc)); + + return EC_SUCCESS; +} + +// http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/ +static int test_kat0(void) +{ + uint32_t crc; + int i; + const char input[] = "The quick brown fox jumps over the lazy dog"; + + crc32_ctx_init(&crc); + for (i = 0; i < strlen(input); ++i) + crc32_ctx_hash8(&crc, input[i]); + TEST_ASSERT(crc32_ctx_result(&crc) == 0x414fa339); + + return EC_SUCCESS; +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_static); + RUN_TEST(test_8); + RUN_TEST(test_kat0); + + test_print_result(); +} diff --git a/test/crc32.tasklist b/test/crc32.tasklist new file mode 100644 index 0000000000..30e5f7fc22 --- /dev/null +++ b/test/crc32.tasklist @@ -0,0 +1,17 @@ +/* Copyright 2016 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * List of enabled tasks in the priority order + * + * The first one has the lowest priority. + * + * For each task, use the macro TASK_TEST(n, r, d, s) where : + * 'n' in the name of the task + * 'r' in the main routine of the task + * 'd' in an opaque parameter passed to the routine at startup + * 's' is the stack size in bytes; must be a multiple of 8 + */ +#define CONFIG_TEST_TASK_LIST diff --git a/test/test_config.h b/test/test_config.h index e784bb8b2e..f46e61443f 100644 --- a/test/test_config.h +++ b/test/test_config.h @@ -84,6 +84,10 @@ #endif +#ifdef TEST_CRC32 +#define CONFIG_SW_CRC +#endif + #ifdef TEST_RSA #define CONFIG_RSA #define CONFIG_RSA_KEY_SIZE 2048 -- cgit v1.2.1