Commit d3210786 authored by Davide Galassi's avatar Davide Galassi

Merge branch 'feature/crc-16-DNP' into 'master'

Feature CRC 16 DNP

See merge request TSML/TSC!3
parents bf2230b7 85bce274
Pipeline #45875128 passed with stages
in 1 minute and 32 seconds
......@@ -114,6 +114,27 @@ void cry_crc16_ibm_init(struct cry_crc16_ctx *ctx);
*/
uint16_t cry_crc16_ibm(const unsigned char *in, size_t n);
/**
* Initialize as a CRC-16-DNP algorithm
*
* Standardized by: CEI EN 60870-5-1
* Used by: CTR protocol.
* Polynomial: x^16 + x^13 + x^12 + x^11 + x^10 + x^8 + x^6 + x^5 + x^2 + 1.
*
* @param ctx CRC-16 context.
*/
void cry_crc16_dnp_init(struct cry_crc16_ctx *ctx);
/**
* CRC-16-DNP all in one function.
*
*
* @param ptr Buffer pointer.
* @param n Number of octets in the buffer.
* @return CRC value.
*/
uint16_t cry_crc16_dnp(const uint8_t *ptr, size_t n);
/**
* Initialize a generic CRC-32 algoritm context.
*
......
#include <cry/crc.h>
static const uint16_t table[] = {
0x0000, 0x365e, 0x6cbc, 0x5ae2, 0xd978, 0xef26, 0xb5c4, 0x839a,
0xff89, 0xc9d7, 0x9335, 0xa56b, 0x26f1, 0x10af, 0x4a4d, 0x7c13,
0xb26b, 0x8435, 0xded7, 0xe889, 0x6b13, 0x5d4d, 0x07af, 0x31f1,
0x4de2, 0x7bbc, 0x215e, 0x1700, 0x949a, 0xa2c4, 0xf826, 0xce78,
0x29af, 0x1ff1, 0x4513, 0x734d, 0xf0d7, 0xc689, 0x9c6b, 0xaa35,
0xd626, 0xe078, 0xba9a, 0x8cc4, 0x0f5e, 0x3900, 0x63e2, 0x55bc,
0x9bc4, 0xad9a, 0xf778, 0xc126, 0x42bc, 0x74e2, 0x2e00, 0x185e,
0x644d, 0x5213, 0x08f1, 0x3eaf, 0xbd35, 0x8b6b, 0xd189, 0xe7d7,
0x535e, 0x6500, 0x3fe2, 0x09bc, 0x8a26, 0xbc78, 0xe69a, 0xd0c4,
0xacd7, 0x9a89, 0xc06b, 0xf635, 0x75af, 0x43f1, 0x1913, 0x2f4d,
0xe135, 0xd76b, 0x8d89, 0xbbd7, 0x384d, 0x0e13, 0x54f1, 0x62af,
0x1ebc, 0x28e2, 0x7200, 0x445e, 0xc7c4, 0xf19a, 0xab78, 0x9d26,
0x7af1, 0x4caf, 0x164d, 0x2013, 0xa389, 0x95d7, 0xcf35, 0xf96b,
0x8578, 0xb326, 0xe9c4, 0xdf9a, 0x5c00, 0x6a5e, 0x30bc, 0x06e2,
0xc89a, 0xfec4, 0xa426, 0x9278, 0x11e2, 0x27bc, 0x7d5e, 0x4b00,
0x3713, 0x014d, 0x5baf, 0x6df1, 0xee6b, 0xd835, 0x82d7, 0xb489,
0xa6bc, 0x90e2, 0xca00, 0xfc5e, 0x7fc4, 0x499a, 0x1378, 0x2526,
0x5935, 0x6f6b, 0x3589, 0x03d7, 0x804d, 0xb613, 0xecf1, 0xdaaf,
0x14d7, 0x2289, 0x786b, 0x4e35, 0xcdaf, 0xfbf1, 0xa113, 0x974d,
0xeb5e, 0xdd00, 0x87e2, 0xb1bc, 0x3226, 0x0478, 0x5e9a, 0x68c4,
0x8f13, 0xb94d, 0xe3af, 0xd5f1, 0x566b, 0x6035, 0x3ad7, 0x0c89,
0x709a, 0x46c4, 0x1c26, 0x2a78, 0xa9e2, 0x9fbc, 0xc55e, 0xf300,
0x3d78, 0x0b26, 0x51c4, 0x679a, 0xe400, 0xd25e, 0x88bc, 0xbee2,
0xc2f1, 0xf4af, 0xae4d, 0x9813, 0x1b89, 0x2dd7, 0x7735, 0x416b,
0xf5e2, 0xc3bc, 0x995e, 0xaf00, 0x2c9a, 0x1ac4, 0x4026, 0x7678,
0x0a6b, 0x3c35, 0x66d7, 0x5089, 0xd313, 0xe54d, 0xbfaf, 0x89f1,
0x4789, 0x71d7, 0x2b35, 0x1d6b, 0x9ef1, 0xa8af, 0xf24d, 0xc413,
0xb800, 0x8e5e, 0xd4bc, 0xe2e2, 0x6178, 0x5726, 0x0dc4, 0x3b9a,
0xdc4d, 0xea13, 0xb0f1, 0x86af, 0x0535, 0x336b, 0x6989, 0x5fd7,
0x23c4, 0x159a, 0x4f78, 0x7926, 0xfabc, 0xcce2, 0x9600, 0xa05e,
0x6e26, 0x5878, 0x029a, 0x34c4, 0xb75e, 0x8100, 0xdbe2, 0xedbc,
0x91af, 0xa7f1, 0xfd13, 0xcb4d, 0x48d7, 0x7e89, 0x246b, 0x1235
};
void cry_crc16_dnp_init(struct cry_crc16_ctx *ctx)
{
cry_crc16_init(ctx, 0x0000, table, CRY_CRC_FLAG_COMPLEMENT);
}
uint16_t cry_crc16_dnp(const uint8_t *ptr, size_t n)
{
struct cry_crc16_ctx ctx;
cry_crc16_dnp_init(&ctx);
cry_crc16_update(&ctx, ptr, n);
return cry_crc16_final(&ctx);
}
......@@ -4,6 +4,7 @@ objects-y := \
crc16.o \
crc16_ccitt.o \
crc16_ibm.o \
crc16_dnp.o \
crc32.o \
crc32_eth.o \
cs8.o \
......
......@@ -18,9 +18,9 @@ objs := \
aes_test.o \
trivium_test.o \
hill_test.o \
affine_test.o
# crc_test.o \
md5_test.o \
affine_test.o \
crc_test.o \
# md5_test.o \
sha256_test.o \
cmac_test.o \
sum_test.o \
......
#include "test.h"
#include <cry/crc.h>
#define MSG "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define MSG ((unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#define MSGLEN (strlen((char *)MSG))
static void crc16_ccitt_test(void)
{
uint16_t crc;
#define CRC16_CCITT 0xad3b
crc = cry_crc16_ccitt(MSG, strlen(MSG));
TRACE("crc16-ccitt = 0x%04x\n", crc);
crc = cry_crc16_ccitt(MSG, MSGLEN);
ASSERT_EQ(crc, CRC16_CCITT);
}
......@@ -18,25 +18,83 @@ static void crc16_ibm_test(void)
uint16_t crc;
#define CRC16_IBM 0xfe85
crc = cry_crc16_ibm(MSG, strlen(MSG));
TRACE("crc16-ibm = 0x%04x\n", crc);
crc = cry_crc16_ibm(MSG, MSGLEN);
ASSERT_EQ(crc, CRC16_IBM);
}
/* CEI EN 60870-5-1 reference pattern */
static const unsigned char en_60870_5_1_pattern[] = {
0x00, 0x00, 0x00, 0x28, 0x30, 0x00, 0x3B, 0xE1,
0x58, 0x0F, 0x12, 0xA7, 0x46, 0x1B, 0x01, 0x00,
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, 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, 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,
0x00, 0x00
};
struct binary_test {
char *name;
const unsigned char *pattern;
size_t pattern_size;
unsigned short expected;
};
static const struct binary_test brs[] = {
{
"CEI EN 60870-5-1 reference pattern",
en_60870_5_1_pattern,
sizeof(en_60870_5_1_pattern),
0x02ea
}
};
static void crc16_dnp_test(void)
{
int i;
uint16_t crc;
for(i = 0; i < sizeof(brs)/sizeof(*brs); ++i) {
crc = cry_crc16_dnp(brs[i].pattern, brs[i].pattern_size);
ASSERT_EQ(crc, brs[i].expected);
}
}
static void crc32_eth_test(void)
{
uint32_t crc;
#define CRC32_ETH 0Xabf77822
crc = cry_crc32_eth(MSG, strlen(MSG));
TRACE("crc32-eth = 0x%08x\n", crc);
crc = cry_crc32_eth(MSG, MSGLEN);
ASSERT_EQ(crc, CRC32_ETH);
}
static struct test_case tests[] = {
{ "CRC16 CCITT", crc16_ccitt_test },
{ "CRC16 IBM", crc16_ibm_test },
{ "CRC16 DNP", crc16_dnp_test },
{ "CRC32 ETH", crc32_eth_test },
};
#define NTESTS (sizeof(tests) / sizeof(tests[0]))
void crc_test(void)
{
TRACE("msg: %s\n", MSG);
RUN(crc16_ccitt_test);
RUN(crc16_ibm_test);
RUN(crc32_eth_test);
int i;
printf("* CRC\n");
for (i = 0; i < NTESTS; i++)
run(tests[i].name, tests[i].func, NULL, NULL);
printf("\n");
}
......@@ -10,6 +10,7 @@ void aes_test(void);
void trivium_test(void);
void hill_test(void);
void affine_test(void);
void crc_test(void);
static int g_runs;
......@@ -37,13 +38,9 @@ struct sub_test g_tests[] = {
SUB_TEST(trivium),
SUB_TEST(hill),
SUB_TEST(affine),
SUB_TEST(crc),
#endif
#if 0
TEST_ELEM(cbc),
TEST_ELEM(ctr),
TEST_ELEM(gcm),
TEST_ELEM(des),
TEST_ELEM(crc),
TEST_ELEM(md5),
TEST_ELEM(sha256),
TEST_ELEM(cmac),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment