Added XTEA and Pearson Hashing.
Improved message encryption for RF24.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#include "CRC16.h"
|
||||
|
||||
|
||||
uint16_t CRC16::calcCRC(const void* data, uint16_t len) {
|
||||
uint16_t _crc = 0xffff;
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
_crc = _crc16_update(_crc, ((uint8_t*)data)[i]);
|
||||
}
|
||||
return _crc;
|
||||
}
|
||||
|
||||
bool CRC16::checkCRC(uint16_t crc, const void* data, uint16_t len) {
|
||||
return crc == calcCRC(data, len);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef _CRC16_H_
|
||||
#define _CRC16_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <util/crc16.h>
|
||||
|
||||
|
||||
class CRC16 {
|
||||
public:
|
||||
uint16_t calcCRC(const void* data, uint16_t len);
|
||||
inline bool checkCRC(uint16_t crc, const void* data, uint16_t len);
|
||||
};
|
||||
|
||||
|
||||
#endif /* _CRC16_H_ */
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
#include "PearsonHash.h"
|
||||
|
||||
|
||||
|
||||
// Permutation function for Pearson hashing (uses less space than a lookup table)
|
||||
// Needs to be bijective, which it is
|
||||
uint8_t PearsonHash::_pearsonPermutation(uint8_t n) {
|
||||
return (n*121) % 256;
|
||||
}
|
||||
|
||||
|
||||
uint16_t PearsonHash::calcHash16(const void* data, uint16_t len) {
|
||||
uint8_t h[2] = { _pearsonPermutation((((uint8_t*)data)[0]+1)%256), _pearsonPermutation(((uint8_t*)data)[0]) };
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
auto _tmp = h[0];
|
||||
h[0] = _pearsonPermutation(h[1] ^ ((uint8_t*)data)[i]);
|
||||
h[1] = _pearsonPermutation(_tmp ^ ((uint8_t*)data)[i]);
|
||||
}
|
||||
void* _tmp = h; // To avoid the type punning warning
|
||||
return *((uint16_t*)_tmp);
|
||||
}
|
||||
|
||||
|
||||
bool PearsonHash::checkHash16(uint16_t crc, const void* data, uint16_t len) {
|
||||
return crc == calcHash16(data, len);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef _PEARSONHASH_H_
|
||||
#define _PEARSONHASH_H_
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class PearsonHash {
|
||||
public:
|
||||
static uint16_t calcHash16(const void* data, uint16_t len);
|
||||
inline static bool checkHash16(uint16_t crc, const void* data, uint16_t len);
|
||||
|
||||
private:
|
||||
static uint8_t _pearsonPermutation(uint8_t n);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,82 @@
|
||||
|
||||
#include <string.h>
|
||||
#include "XTEA.h"
|
||||
|
||||
|
||||
|
||||
void XTEA::setKey(void* key) {
|
||||
memcpy(_key, key, 16);
|
||||
}
|
||||
|
||||
|
||||
void XTEA::setIV(void* iv) {
|
||||
memcpy(_vector, iv, 8);
|
||||
}
|
||||
|
||||
|
||||
void XTEA::setIV(uint32_t ivh = 0, uint32_t ivl = 0) {
|
||||
memcpy(_vector, &ivh, 4);
|
||||
memcpy(_vector+4, &ivl, 4);
|
||||
}
|
||||
|
||||
|
||||
void XTEA::cbc_encrypt(void *dest, const void *plain, uint16_t nr_blocks) {
|
||||
for (uint32_t i = 0; i < nr_blocks; i++) {
|
||||
|
||||
// XOR the current vector with the block before encrypting
|
||||
for (auto j = 0; j < 8; j++) {
|
||||
((uint8_t*)plain)[i*8 + j] ^= _vector[j];
|
||||
}
|
||||
|
||||
// Encrypt the block
|
||||
encrypt_block((uint8_t*)dest + i*8, (uint8_t*)plain + i*8);
|
||||
|
||||
// Copy the cipher output to the current vector
|
||||
memcpy(_vector, (uint8_t*)dest + i*8, 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void XTEA::cbc_decrypt(void *dest, const void *cipher, uint16_t nr_blocks) {
|
||||
for (uint32_t i = 0; i < nr_blocks; i++) {
|
||||
|
||||
// Decrypt the block
|
||||
decrypt_block((uint8_t*)dest + i*8, (uint8_t*)cipher + i*8);
|
||||
|
||||
// XOR the output with the current vector to fully decrypt
|
||||
for (auto j = 0; j < 8; j++) {
|
||||
((uint8_t*)dest)[i*8 + j] ^= _vector[j];
|
||||
}
|
||||
|
||||
// Copy the cipher text to the current vector
|
||||
memcpy(_vector, (uint8_t*)cipher + i*8, 8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void XTEA::encrypt_block(void *dest, const void *block) {
|
||||
uint8_t i;
|
||||
uint32_t v0=((uint32_t*)block)[0], v1=((uint32_t*)block)[1];
|
||||
uint32_t sum=0, delta=0x9E3779B9;
|
||||
for(i=0; i<XTEA_CYCLES; i++) {
|
||||
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)_key)[sum & 3]);
|
||||
sum += delta;
|
||||
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)_key)[sum>>11 & 3]);
|
||||
}
|
||||
((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
|
||||
}
|
||||
|
||||
|
||||
void XTEA::decrypt_block(void *dest, const void *block) {
|
||||
uint8_t i;
|
||||
uint32_t v0=((uint32_t*)block)[0], v1=((uint32_t*)block)[1];
|
||||
uint32_t delta=0x9E3779B9, sum=delta*XTEA_CYCLES;
|
||||
for(i=0; i<XTEA_CYCLES; i++) {
|
||||
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)_key)[sum>>11 & 3]);
|
||||
sum -= delta;
|
||||
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)_key)[sum & 3]);
|
||||
}
|
||||
((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef XTEA_H_
|
||||
#define XTEA_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#define XTEA_CYCLES 32
|
||||
|
||||
|
||||
class XTEA {
|
||||
public:
|
||||
void setKey(void* key);
|
||||
|
||||
void setIV(void* iv);
|
||||
void setIV(uint32_t ivh, uint32_t ivl);
|
||||
|
||||
void cbc_encrypt(void *dest, const void *plain, uint16_t nr_blocks);
|
||||
void cbc_decrypt(void *dest, const void *cipher, uint16_t nr_blocks);
|
||||
|
||||
void encrypt_block(void *dest, const void *block);
|
||||
void decrypt_block(void *dest, const void *block);
|
||||
|
||||
private:
|
||||
uint8_t _key[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
|
||||
uint8_t _vector[8] = {0};
|
||||
};
|
||||
|
||||
|
||||
#endif /*XTEA_H_*/
|
||||
Reference in New Issue
Block a user