summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2021-03-11 22:58:55 -0800
committerCommit Bot <commit-bot@chromium.org>2021-03-18 16:00:10 +0000
commit9fbcb24c53e5322044e5d71456d1fc0256b2e225 (patch)
tree0d9ea1d26a78a9d5680bbfe1bc027034d8f55d2a
parent3d3f2316b7d2f060d4f8c919a2c82326f8651af3 (diff)
downloadchrome-ec-9fbcb24c53e5322044e5d71456d1fc0256b2e225.tar.gz
crc32: Add crc32_hash and crc32_ctx_hash
This patch adds crc32_hash and crc32_ctx_hash, which compute CRC32 of data in arbitrary length using an internal context and an external context, respectively. BUG=None BRANCH=None TEST=make run-crc Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> Change-Id: I08729528fba6b1350bbb8387c048025f72496b2d Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2757097 Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r--common/crc.c18
-rw-r--r--include/crc.h17
-rw-r--r--test/crc.c13
3 files changed, 41 insertions, 7 deletions
diff --git a/common/crc.c b/common/crc.c
index 2db1ffc361..04847cf095 100644
--- a/common/crc.c
+++ b/common/crc.c
@@ -56,7 +56,7 @@ static const uint32_t crc32_tab[] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
-static uint32_t crc32_hash(uint32_t crc, const void *buf, int size)
+static uint32_t _crc32_hash(uint32_t crc, const void *buf, int size)
{
const uint8_t *p;
@@ -75,19 +75,24 @@ void crc32_ctx_init(uint32_t *crc)
*crc = CRC32_INITIAL;
}
+void crc32_ctx_hash(uint32_t *crc, const void *buf, int size)
+{
+ *crc = _crc32_hash(*crc, buf, size);
+}
+
void crc32_ctx_hash32(uint32_t *crc, uint32_t val)
{
- *crc = crc32_hash(*crc, &val, sizeof(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));
+ *crc = _crc32_hash(*crc, &val, sizeof(val));
}
void crc32_ctx_hash8(uint32_t *crc, uint8_t val)
{
- *crc = crc32_hash(*crc, &val, sizeof(val));
+ *crc = _crc32_hash(*crc, &val, sizeof(val));
}
uint32_t crc32_ctx_result(uint32_t *crc)
@@ -103,6 +108,11 @@ void crc32_init(void)
crc32_ctx_init(&crc_);
}
+void crc32_hash(const void *buf, int size)
+{
+ crc32_ctx_hash(&crc_, buf, size);
+}
+
void crc32_hash32(uint32_t val)
{
crc32_ctx_hash32(&crc_, val);
diff --git a/include/crc.h b/include/crc.h
index 511a7cdb9b..b899ca965e 100644
--- a/include/crc.h
+++ b/include/crc.h
@@ -18,6 +18,14 @@
void crc32_init(void);
+/**
+ * Calculate CRC32 of data in arbitrary length.
+ *
+ * @param buf Data for CRC32 to be calculated for.
+ * @param size Size of <buf> in bytes.
+ */
+void crc32_hash(const void *buf, int size);
+
void crc32_hash32(uint32_t val);
void crc32_hash16(uint16_t val);
@@ -28,6 +36,15 @@ uint32_t crc32_result(void);
void crc32_ctx_init(uint32_t *ctx);
+/**
+ * Calculate CRC32 of data in arbitrary length using given context.
+ *
+ * @param crc CRC32 context.
+ * @param buf Data for CRC32 to be calculated for.
+ * @param size Size of <buf> in bytes.
+ */
+void crc32_ctx_hash(uint32_t *crc, const void *buf, int size);
+
void crc32_ctx_hash32(uint32_t *ctx, uint32_t val);
void crc32_ctx_hash16(uint32_t *ctx, uint16_t val);
diff --git a/test/crc.c b/test/crc.c
index 3f24c1a7e4..e65be72ace 100644
--- a/test/crc.c
+++ b/test/crc.c
@@ -18,12 +18,15 @@ static int test_static_version(void)
uint32_t crc;
const uint32_t input = 0xdeadbeef;
- crc32_init();
- crc32_hash32(input);
-
crc32_ctx_init(&crc);
crc32_ctx_hash32(&crc, input);
+ crc32_init();
+ crc32_hash32(input);
+ TEST_ASSERT(crc32_result() == crc32_ctx_result(&crc));
+
+ crc32_init();
+ crc32_hash(&input, sizeof(input));
TEST_ASSERT(crc32_result() == crc32_ctx_result(&crc));
return EC_SUCCESS;
@@ -61,6 +64,10 @@ static int test_kat0(void)
crc32_ctx_hash8(&crc, input[i]);
TEST_ASSERT(crc32_ctx_result(&crc) == 0x414fa339);
+ crc32_ctx_init(&crc);
+ crc32_ctx_hash(&crc, input, strlen(input));
+ TEST_ASSERT(crc32_ctx_result(&crc) == 0x414fa339);
+
return EC_SUCCESS;
}