Verify the integrity of key state update messages using CRC16-CCITT.
This commit is contained in:
@@ -33,3 +33,24 @@ void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
|
||||
{
|
||||
*hash = crc16Config->currentCrc;
|
||||
}
|
||||
|
||||
crc16_data_t crc16data;
|
||||
|
||||
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes)
|
||||
{
|
||||
uint16_t hash;
|
||||
crc16_init(&crc16data);
|
||||
crc16_update(&crc16data, message, lengthInBytes);
|
||||
crc16_finalize(&crc16data, &hash);
|
||||
message[lengthInBytes] = hash & 0xff;
|
||||
message[lengthInBytes+1] = hash >> 8;
|
||||
}
|
||||
|
||||
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes)
|
||||
{
|
||||
uint16_t hash;
|
||||
crc16_init(&crc16data);
|
||||
crc16_update(&crc16data, message, lengthInBytes);
|
||||
crc16_finalize(&crc16data, &hash);
|
||||
return (message[lengthInBytes] == (hash & 0xff)) && (message[lengthInBytes+1] == (hash >> 8));
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#define __CRC16_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define CRC16_HASH_LENGTH 2 // bytes
|
||||
|
||||
typedef struct Crc16Data {
|
||||
uint16_t currentCrc;
|
||||
@@ -22,4 +25,7 @@ void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t length
|
||||
//! @param hash Pointer to the value returned for the final calculated crc value.
|
||||
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
|
||||
|
||||
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes);
|
||||
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user