Verify the integrity of key state update messages using CRC16-CCITT.

This commit is contained in:
László Monda
2017-05-31 03:26:50 +02:00
parent 57319489d2
commit 1e290ebc34
6 changed files with 38 additions and 3 deletions

View File

@@ -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));
}

View File

@@ -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