mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-27 06:50:21 +00:00
d92b0a82cc
"A long time ago in a galaxy far, far away...." we started NFC subsystem refactoring. Starring: - @gornekich - NFC refactoring project lead, architect, senior developer - @gsurkov - architect, senior developer - @RebornedBrain - senior developer Supporting roles: - @skotopes, @DrZlo13, @hedger - general architecture advisors, code review - @Astrrra, @doomwastaken, @Hellitron, @ImagineVagon333 - quality assurance Special thanks: @bettse, @pcunning, @nxv, @noproto, @AloneLiberty and everyone else who has been helping us all this time and contributing valuable knowledges, ideas and source code.
57 lines
1.8 KiB
C
57 lines
1.8 KiB
C
#include "iso14443_crc.h"
|
|
|
|
#include <core/check.h>
|
|
|
|
#define ISO14443_3A_CRC_INIT (0x6363U)
|
|
#define ISO14443_3B_CRC_INIT (0xFFFFU)
|
|
|
|
static uint16_t
|
|
iso14443_crc_calculate(Iso14443CrcType type, const uint8_t* data, size_t data_size) {
|
|
uint16_t crc;
|
|
|
|
if(type == Iso14443CrcTypeA) {
|
|
crc = ISO14443_3A_CRC_INIT;
|
|
} else if(type == Iso14443CrcTypeB) {
|
|
crc = ISO14443_3B_CRC_INIT;
|
|
} else {
|
|
furi_crash("Wrong ISO14443 CRC type");
|
|
}
|
|
|
|
for(size_t i = 0; i < data_size; i++) {
|
|
uint8_t byte = data[i];
|
|
byte ^= (uint8_t)(crc & 0xff);
|
|
byte ^= byte << 4;
|
|
crc = (crc >> 8) ^ (((uint16_t)byte) << 8) ^ (((uint16_t)byte) << 3) ^ (byte >> 4);
|
|
}
|
|
|
|
return type == Iso14443CrcTypeA ? crc : ~crc;
|
|
}
|
|
|
|
void iso14443_crc_append(Iso14443CrcType type, BitBuffer* buf) {
|
|
const uint8_t* data = bit_buffer_get_data(buf);
|
|
const size_t data_size = bit_buffer_get_size_bytes(buf);
|
|
|
|
const uint16_t crc = iso14443_crc_calculate(type, data, data_size);
|
|
bit_buffer_append_bytes(buf, (const uint8_t*)&crc, ISO14443_CRC_SIZE);
|
|
}
|
|
|
|
bool iso14443_crc_check(Iso14443CrcType type, const BitBuffer* buf) {
|
|
const size_t data_size = bit_buffer_get_size_bytes(buf);
|
|
if(data_size <= ISO14443_CRC_SIZE) return false;
|
|
|
|
uint16_t crc_received;
|
|
bit_buffer_write_bytes_mid(
|
|
buf, &crc_received, data_size - ISO14443_CRC_SIZE, ISO14443_CRC_SIZE);
|
|
|
|
const uint8_t* data = bit_buffer_get_data(buf);
|
|
const uint16_t crc_calc = iso14443_crc_calculate(type, data, data_size - ISO14443_CRC_SIZE);
|
|
|
|
return (crc_calc == crc_received);
|
|
}
|
|
|
|
void iso14443_crc_trim(BitBuffer* buf) {
|
|
const size_t data_size = bit_buffer_get_size_bytes(buf);
|
|
furi_assert(data_size > ISO14443_CRC_SIZE);
|
|
|
|
bit_buffer_set_size_bytes(buf, data_size - ISO14443_CRC_SIZE);
|
|
}
|