Commit 85bce274 authored by Davide Galassi's avatar Davide Galassi

CRC16-DNP CRY port

parent c2bf3a3c
Pipeline #45874739 passed with stages
in 1 minute and 10 seconds
......@@ -123,7 +123,7 @@ uint16_t cry_crc16_ibm(const unsigned char *in, size_t n);
*
* @param ctx CRC-16 context.
*/
void tsc_crc16_dnp_init(struct tsc_crc16_ctx *ctx);
void cry_crc16_dnp_init(struct cry_crc16_ctx *ctx);
/**
* CRC-16-DNP all in one function.
......@@ -133,7 +133,7 @@ void tsc_crc16_dnp_init(struct tsc_crc16_ctx *ctx);
* @param n Number of octets in the buffer.
* @return CRC value.
*/
uint16_t tsc_crc16_dnp(const uint8_t *ptr, size_t n);
uint16_t cry_crc16_dnp(const uint8_t *ptr, size_t n);
/**
* Initialize a generic CRC-32 algoritm context.
......
/**
* Terranova Security Library.
*
* @author Giampaolo Tomassoni
* @file tsc_crc16_dnp.c
*/
#include "tsc_crc.h"
#include <cry/crc.h>
static const uint16_t table[] = {
0x0000, 0x365e, 0x6cbc, 0x5ae2, 0xd978, 0xef26, 0xb5c4, 0x839a,
......@@ -42,24 +35,16 @@ static const uint16_t table[] = {
0x91af, 0xa7f1, 0xfd13, 0xcb4d, 0x48d7, 0x7e89, 0x246b, 0x1235
};
void tsc_crc16_dnp_init(struct tsc_crc16_ctx *ctx)
void cry_crc16_dnp_init(struct cry_crc16_ctx *ctx)
{
tsc_crc16_init(
ctx,
0x0000,
table,
TSC_CRC_FLAG_COMPLEMENT
);
cry_crc16_init(ctx, 0x0000, table, CRY_CRC_FLAG_COMPLEMENT);
}
uint16_t tsc_crc16_dnp(const uint8_t *ptr, size_t n)
uint16_t cry_crc16_dnp(const uint8_t *ptr, size_t n)
{
struct tsc_crc16_ctx ctx;
uint16_t crc;
struct cry_crc16_ctx ctx;
tsc_crc16_dnp_init(&ctx);
tsc_crc16_update(&ctx, ptr, n);
tsc_crc16_final(&ctx, &crc);
return crc;
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,11 +18,11 @@ 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,
......@@ -45,40 +45,29 @@ static const unsigned char en_60870_5_1_pattern[] = {
};
struct binary_test {
char* name;
unsigned char* pattern;
size_t pattern_size;
unsigned short expected;
char *name;
const unsigned char *pattern;
size_t pattern_size;
unsigned short expected;
};
static const struct binary_test brs[] = {
{
"CEI EN 60870-5-1 ref pattern",
en_60870_5_1_pattern, sizeof(en_60870_5_1_pattern),
0xea02
{
"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)
{
struct tsc_crc16_ctx ctx;
int i, t;
unsigned short crc;
printf("\nCTR (DNP, CEI EN 60870-5-1)\n");
for(t = 0; t < sizeof(brs)/sizeof(*brs); ++t) {
printf("CRC16(%s) = 0x%04x\n", brs[t].name,
tsc_crc16_dnp(brs[t].pattern, brs[t].pattern_size));
printf("CRC16(");
tsc_crc16_dnp_init(&ctx);
for (i = 0; i < brs[t].pattern_size; i++) {
printf("%02x ", brs[t].pattern[i]);
tsc_crc16_update(&ctx, &brs[t].pattern[i], 1);
}
tsc_crc16_final(&ctx, &crc);
printf(") = 0x%04x\n", crc);
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);
}
}
......@@ -87,16 +76,25 @@ 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(crc16_dnp3_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