Skip to content

Commit

Permalink
[#208] Start of FHECF
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnie-Ice committed Feb 5, 2025
1 parent 7bc238b commit 1adf3c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
/*
** User Prototypes
*/
uint8_t gf_mul(uint8_t a, uint8_t b);

// Crypto Library Configuration functions
extern int32_t Crypto_Config_CryptoLib(uint8_t key_type, uint8_t mc_type, uint8_t sa_type, uint8_t cryptography_type,
Expand Down Expand Up @@ -240,6 +241,7 @@ void Crypto_Local_Config(void);
void Crypto_Local_Init(void);
int32_t Crypto_window(uint8_t *actual, uint8_t *expected, int length, int window);
uint16_t Crypto_Calc_FECF(const uint8_t *ingest, int len_ingest);
uint16_t Crypto_Calc_FHECF(uint8_t *data);
void Crypto_Calc_CRC_Init_Table(void);
uint16_t Crypto_Calc_CRC16(uint8_t *data, int size);
int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t *sa_ptr, uint8_t *arsn, uint8_t *iv);
Expand Down Expand Up @@ -316,6 +318,7 @@ extern TM_FrameSecurityHeader_t tm_frame_sec_hdr; // Used to reduce bit math dup
// exterm AOS_t aos_frame
extern AOS_FramePrimaryHeader_t aos_frame_pri_hdr;
extern AOS_FrameSecurityHeader_t aos_frame_sec_hdr; // Used to reduce bit math duplication
extern uint8_t parity[4]; // Used in FHECF calc

// Global configuration structs
extern CryptoConfig_t crypto_config;
Expand Down Expand Up @@ -356,4 +359,17 @@ extern uint8_t badFECF;
extern uint32_t crc32Table[CRC32TBL_SIZE];
extern uint16_t crc16Table[CRC16TBL_SIZE];

// GF(2^4) field and logarithm tables
static const uint8_t gf_exp[30] = {
1, 2, 4, 8, 3, 6, 12, 11, 5, 10, 7, 14, 15, 13, 9, 1,
2, 4, 8, 3, 6, 12, 11, 5, 10, 7, 14, 15, 13, 9
};

static const uint8_t gf_log[GF_SIZE] = {
0, 0, 1, 4, 2, 8, 5, 10, 3, 14, 9, 7, 6, 13, 11, 12
};

// Generator polynomial coefficients for g(x) = x^4 + a^3x^3 + ax^2 + a^3x + 1
static const uint8_t gen_poly[RS_PARITY + 1] = {1, 8, 2, 8, 1};

#endif // CRYPTO_H
7 changes: 7 additions & 0 deletions include/crypto_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@
#define TM_CADU_SIZE TM_FRAME_DATA_SIZE
#endif

// AOS Behavior Defines
// FHECF Calculation
#define RS_SYMS 10 // Total symbols in codeword
#define RS_DATA 6 // Data symbols
#define RS_PARITY 4 // Parity symbols
#define GF_SIZE 16 // 2^4

// Logic Behavior Defines
#define CRYPTO_FALSE 0
#define CRYPTO_TRUE 1
Expand Down
32 changes: 32 additions & 0 deletions src/core/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,38 @@ uint16_t Crypto_Calc_CRC16(uint8_t *data, int size)
return crc;
}

uint8_t gf_mul(uint8_t a, uint8_t b)
{
if (a == 0 || b == 0)
{
return 0;
}
else
{
return gf_exp[(gf_log[a] + gf_log[b]) % (GF_SIZE - 1)];
}
}

uint16_t Crypto_Calc_FHECF(uint8_t *data)
{
uint8_t feedback = 0;

// RS encoding
memset(parity, 0, RS_PARITY);
for (int i = 0; i < RS_DATA; i++)
{
feedback = data[i] ^ parity[0];
memmove(&parity[0], &parity[1], RS_PARITY - 1);
parity[RS_PARITY - 1] = 0;

for (int j = 0; j < RS_PARITY; j++)
{
parity[j] ^= gf_mul(feedback, gen_poly[j + 1]);
}
}
return 0;
}

/*
** Procedures Specifications
*/
Expand Down

0 comments on commit 1adf3c7

Please sign in to comment.