diff --git a/include/crypto.h b/include/crypto.h index 698f2704..f4ba3fe6 100644 --- a/include/crypto.h +++ b/include/crypto.h @@ -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, @@ -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); @@ -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; @@ -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 \ No newline at end of file diff --git a/include/crypto_config.h b/include/crypto_config.h index 6d597282..1ae784e1 100644 --- a/include/crypto_config.h +++ b/include/crypto_config.h @@ -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 diff --git a/src/core/crypto.c b/src/core/crypto.c index 1799322a..3bea259f 100644 --- a/src/core/crypto.c +++ b/src/core/crypto.c @@ -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 */