protocol_keri.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <furi.h>
  2. #include <toolbox/protocols/protocol.h>
  3. #include <lfrfid/tools/bit_lib.h>
  4. #include "lfrfid_protocols.h"
  5. #define KERI_PREAMBLE_BIT_SIZE (33)
  6. #define KERI_PREAMBLE_DATA_SIZE (5)
  7. #define KERI_ENCODED_BIT_SIZE (64)
  8. #define KERI_ENCODED_DATA_SIZE (((KERI_ENCODED_BIT_SIZE) / 8) + KERI_PREAMBLE_DATA_SIZE)
  9. #define KERI_ENCODED_DATA_LAST ((KERI_ENCODED_BIT_SIZE) / 8)
  10. #define KERI_DECODED_BIT_SIZE (28)
  11. #define KERI_DECODED_DATA_SIZE (4)
  12. #define KERI_US_PER_BIT (255)
  13. #define KERI_ENCODER_PULSES_PER_BIT (16)
  14. typedef struct {
  15. uint8_t data_index;
  16. uint8_t bit_clock_index;
  17. bool last_bit;
  18. bool current_polarity;
  19. bool pulse_phase;
  20. } ProtocolKeriEncoder;
  21. typedef struct {
  22. uint8_t encoded_data[KERI_ENCODED_DATA_SIZE];
  23. uint8_t negative_encoded_data[KERI_ENCODED_DATA_SIZE];
  24. uint8_t corrupted_encoded_data[KERI_ENCODED_DATA_SIZE];
  25. uint8_t corrupted_negative_encoded_data[KERI_ENCODED_DATA_SIZE];
  26. uint8_t data[KERI_DECODED_DATA_SIZE];
  27. ProtocolKeriEncoder encoder;
  28. } ProtocolKeri;
  29. ProtocolKeri* protocol_keri_alloc(void) {
  30. ProtocolKeri* protocol = malloc(sizeof(ProtocolKeri));
  31. return protocol;
  32. };
  33. void protocol_keri_free(ProtocolKeri* protocol) {
  34. free(protocol);
  35. };
  36. uint8_t* protocol_keri_get_data(ProtocolKeri* protocol) {
  37. return protocol->data;
  38. };
  39. void protocol_keri_decoder_start(ProtocolKeri* protocol) {
  40. memset(protocol->encoded_data, 0, KERI_ENCODED_DATA_SIZE);
  41. memset(protocol->negative_encoded_data, 0, KERI_ENCODED_DATA_SIZE);
  42. memset(protocol->corrupted_encoded_data, 0, KERI_ENCODED_DATA_SIZE);
  43. memset(protocol->corrupted_negative_encoded_data, 0, KERI_ENCODED_DATA_SIZE);
  44. };
  45. static bool protocol_keri_check_preamble(uint8_t* data, size_t bit_index) {
  46. // Preamble 11100000 00000000 00000000 00000000 1
  47. if(*(uint32_t*)&data[bit_index / 8] != 0b00000000000000000000000011100000) return false;
  48. if(bit_lib_get_bit(data, bit_index + 32) != 1) return false;
  49. return true;
  50. }
  51. static bool protocol_keri_can_be_decoded(uint8_t* data) {
  52. if(!protocol_keri_check_preamble(data, 0)) return false;
  53. if(!protocol_keri_check_preamble(data, 64)) return false;
  54. ///if(bit_lib_get_bit(data, 61) != 0) return false;
  55. //if(bit_lib_get_bit(data, 60) != 0) return false;
  56. return true;
  57. }
  58. static bool protocol_keri_decoder_feed_internal(bool polarity, uint32_t time, uint8_t* data) {
  59. time += (KERI_US_PER_BIT / 2);
  60. size_t bit_count = (time / KERI_US_PER_BIT);
  61. bool result = false;
  62. if(bit_count < KERI_ENCODED_BIT_SIZE) {
  63. for(size_t i = 0; i < bit_count; i++) {
  64. bit_lib_push_bit(data, KERI_ENCODED_DATA_SIZE, polarity);
  65. if(protocol_keri_can_be_decoded(data)) {
  66. result = true;
  67. break;
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. static void protocol_keri_descramble(uint32_t* fc, uint32_t* cn, uint32_t* internal_id) {
  74. const uint8_t card_to_id[] = {255, 255, 255, 255, 13, 12, 20, 5, 16, 6, 21,
  75. 17, 8, 255, 0, 7, 10, 15, 255, 11, 4, 1,
  76. 255, 18, 255, 19, 2, 14, 3, 9, 255, 255};
  77. const uint8_t card_to_fc[] = {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  78. 255, 255, 0, 255, 255, 255, 255, 2, 255, 255, 255,
  79. 3, 255, 4, 255, 255, 255, 255, 255, 1, 255};
  80. *fc = 0;
  81. *cn = 0;
  82. for(uint8_t card_idx = 0; card_idx < 32; card_idx++) {
  83. bool bit = (*internal_id >> card_idx) & 1;
  84. // Card ID
  85. if(card_to_id[card_idx] < 32) {
  86. *cn = *cn | (bit << card_to_id[card_idx]);
  87. }
  88. // Card FC
  89. if(card_to_fc[card_idx] < 32) {
  90. *fc = *fc | (bit << card_to_fc[card_idx]);
  91. }
  92. }
  93. }
  94. static void protocol_keri_decoder_save(uint8_t* data_to, const uint8_t* data_from) {
  95. uint32_t id = bit_lib_get_bits_32(data_from, 32, 32);
  96. data_to[3] = (uint8_t)id;
  97. data_to[2] = (uint8_t)(id >>= 8);
  98. data_to[1] = (uint8_t)(id >>= 8);
  99. data_to[0] = (uint8_t)(id >>= 8);
  100. }
  101. bool protocol_keri_decoder_feed(ProtocolKeri* protocol, bool level, uint32_t duration) {
  102. bool result = false;
  103. if(duration > (KERI_US_PER_BIT / 2)) {
  104. if(protocol_keri_decoder_feed_internal(level, duration, protocol->encoded_data)) {
  105. protocol_keri_decoder_save(protocol->data, protocol->encoded_data);
  106. result = true;
  107. return result;
  108. }
  109. if(protocol_keri_decoder_feed_internal(!level, duration, protocol->negative_encoded_data)) {
  110. protocol_keri_decoder_save(protocol->data, protocol->negative_encoded_data);
  111. result = true;
  112. return result;
  113. }
  114. }
  115. if(duration > (KERI_US_PER_BIT / 4)) {
  116. // Try to decode wrong phase synced data
  117. if(level) {
  118. duration += 120;
  119. } else {
  120. if(duration > 120) {
  121. duration -= 120;
  122. }
  123. }
  124. if(protocol_keri_decoder_feed_internal(level, duration, protocol->corrupted_encoded_data)) {
  125. protocol_keri_decoder_save(protocol->data, protocol->corrupted_encoded_data);
  126. result = true;
  127. return result;
  128. }
  129. if(protocol_keri_decoder_feed_internal(
  130. !level, duration, protocol->corrupted_negative_encoded_data)) {
  131. protocol_keri_decoder_save(protocol->data, protocol->corrupted_negative_encoded_data);
  132. result = true;
  133. return result;
  134. }
  135. }
  136. return result;
  137. };
  138. bool protocol_keri_encoder_start(ProtocolKeri* protocol) {
  139. memset(protocol->encoded_data, 0, KERI_ENCODED_DATA_SIZE);
  140. *(uint32_t*)&protocol->encoded_data[0] = 0b00000000000000000000000011100000;
  141. bit_lib_copy_bits(protocol->encoded_data, 32, 32, protocol->data, 0);
  142. bit_lib_set_bits(protocol->encoded_data, 32, 1, 1);
  143. protocol->encoder.last_bit =
  144. bit_lib_get_bit(protocol->encoded_data, KERI_ENCODED_BIT_SIZE - 1);
  145. protocol->encoder.data_index = 0;
  146. protocol->encoder.current_polarity = true;
  147. protocol->encoder.pulse_phase = true;
  148. protocol->encoder.bit_clock_index = 0;
  149. return true;
  150. };
  151. LevelDuration protocol_keri_encoder_yield(ProtocolKeri* protocol) {
  152. LevelDuration level_duration;
  153. ProtocolKeriEncoder* encoder = &protocol->encoder;
  154. if(encoder->pulse_phase) {
  155. level_duration = level_duration_make(encoder->current_polarity, 1);
  156. encoder->pulse_phase = false;
  157. } else {
  158. level_duration = level_duration_make(!encoder->current_polarity, 1);
  159. encoder->pulse_phase = true;
  160. encoder->bit_clock_index++;
  161. if(encoder->bit_clock_index >= KERI_ENCODER_PULSES_PER_BIT) {
  162. encoder->bit_clock_index = 0;
  163. bool current_bit = bit_lib_get_bit(protocol->encoded_data, encoder->data_index);
  164. if(current_bit != encoder->last_bit) {
  165. encoder->current_polarity = !encoder->current_polarity;
  166. }
  167. encoder->last_bit = current_bit;
  168. bit_lib_increment_index(encoder->data_index, KERI_ENCODED_BIT_SIZE);
  169. }
  170. }
  171. return level_duration;
  172. };
  173. void protocol_keri_render_data(ProtocolKeri* protocol, FuriString* result) {
  174. uint32_t data = bit_lib_get_bits_32(protocol->data, 0, 32);
  175. uint32_t internal_id = data & 0x7FFFFFFF;
  176. uint32_t fc = 0;
  177. uint32_t cn = 0;
  178. protocol_keri_descramble(&fc, &cn, &data);
  179. furi_string_printf(result, "Internal ID: %lu\r\nFC: %lu, Card: %lu\r\n", internal_id, fc, cn);
  180. }
  181. bool protocol_keri_write_data(ProtocolKeri* protocol, void* data) {
  182. LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
  183. bool result = false;
  184. // Start bit should be always set
  185. protocol->data[0] |= (1 << 7);
  186. protocol_keri_encoder_start(protocol);
  187. if(request->write_type == LFRFIDWriteTypeT5577) {
  188. request->t5577.block[0] = LFRFID_T5577_TESTMODE_DISABLED | LFRFID_T5577_X_MODE |
  189. LFRFID_T5577_MODULATION_PSK1 | LFRFID_T5577_PSKCF_RF_2 |
  190. (2 << LFRFID_T5577_MAXBLOCK_SHIFT);
  191. request->t5577.block[0] |= 0xF << 18;
  192. request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
  193. request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
  194. request->t5577.blocks_to_write = 3;
  195. result = true;
  196. }
  197. return result;
  198. };
  199. const ProtocolBase protocol_keri = {
  200. .name = "Keri",
  201. .manufacturer = "Keri",
  202. .data_size = KERI_DECODED_DATA_SIZE,
  203. .features = LFRFIDFeaturePSK,
  204. .validate_count = 6,
  205. .alloc = (ProtocolAlloc)protocol_keri_alloc,
  206. .free = (ProtocolFree)protocol_keri_free,
  207. .get_data = (ProtocolGetData)protocol_keri_get_data,
  208. .decoder =
  209. {
  210. .start = (ProtocolDecoderStart)protocol_keri_decoder_start,
  211. .feed = (ProtocolDecoderFeed)protocol_keri_decoder_feed,
  212. },
  213. .encoder =
  214. {
  215. .start = (ProtocolEncoderStart)protocol_keri_encoder_start,
  216. .yield = (ProtocolEncoderYield)protocol_keri_encoder_yield,
  217. },
  218. .render_data = (ProtocolRenderData)protocol_keri_render_data,
  219. .render_brief_data = (ProtocolRenderData)protocol_keri_render_data,
  220. .write_data = (ProtocolWriteData)protocol_keri_write_data,
  221. };