wiegand.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "interface.h"
  2. #include <lib/bit_lib/bit_lib.h>
  3. #include <flipper_application/flipper_application.h>
  4. /*
  5. * Huge thanks to the proxmark codebase:
  6. * https://github.com/RfidResearchGroup/proxmark3/blob/master/client/src/wiegand_formats.c
  7. */
  8. // Structure for packed wiegand messages
  9. // Always align lowest value (last transmitted) bit to ordinal position 0 (lowest valued bit bottom)
  10. typedef struct {
  11. uint8_t Length; // Number of encoded bits in wiegand message (excluding headers and preamble)
  12. uint32_t Top; // Bits in x<<64 positions
  13. uint32_t Mid; // Bits in x<<32 positions
  14. uint32_t Bot; // Lowest ordinal positions
  15. } wiegand_message_t;
  16. static inline uint8_t oddparity32(uint32_t x) {
  17. return bit_lib_test_parity_32(x, BitLibParityOdd);
  18. }
  19. static inline uint8_t evenparity32(uint32_t x) {
  20. return bit_lib_test_parity_32(x, BitLibParityEven);
  21. }
  22. uint8_t get_bit_by_position(wiegand_message_t* data, uint8_t pos) {
  23. if(pos >= data->Length) return false;
  24. pos = (data->Length - pos) -
  25. 1; // invert ordering; Indexing goes from 0 to 1. Subtract 1 for weight of bit.
  26. uint8_t result = 0;
  27. if(pos > 95)
  28. result = 0;
  29. else if(pos > 63)
  30. result = (data->Top >> (pos - 64)) & 1;
  31. else if(pos > 31)
  32. result = (data->Mid >> (pos - 32)) & 1;
  33. else
  34. result = (data->Bot >> pos) & 1;
  35. return result;
  36. }
  37. uint64_t get_linear_field(wiegand_message_t* data, uint8_t firstBit, uint8_t length) {
  38. uint64_t result = 0;
  39. for(uint8_t i = 0; i < length; i++) {
  40. result = (result << 1) | get_bit_by_position(data, firstBit + i);
  41. }
  42. return result;
  43. }
  44. static int wiegand_C1k35s_parse(uint8_t bit_length, uint64_t bits, FuriString* description) {
  45. if(bit_length != 35) {
  46. return 0;
  47. }
  48. wiegand_message_t value;
  49. value.Mid = bits >> 32;
  50. value.Bot = bits;
  51. wiegand_message_t* packed = &value;
  52. uint32_t cn = (packed->Bot >> 1) & 0x000FFFFF;
  53. uint32_t fc = ((packed->Mid & 1) << 11) | ((packed->Bot >> 21));
  54. bool valid = (evenparity32((packed->Mid & 0x1) ^ (packed->Bot & 0xB6DB6DB6)) ==
  55. ((packed->Mid >> 1) & 1)) &&
  56. (oddparity32((packed->Mid & 0x3) ^ (packed->Bot & 0x6DB6DB6C)) ==
  57. ((packed->Bot >> 0) & 1)) &&
  58. (oddparity32((packed->Mid & 0x3) ^ (packed->Bot & 0xFFFFFFFF)) ==
  59. ((packed->Mid >> 2) & 1));
  60. if(valid) {
  61. furi_string_cat_printf(description, "C1k35s\nFC: %ld CN: %ld\n", fc, cn);
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. static int wiegand_h10301_parse(uint8_t bit_length, uint64_t bits, FuriString* description) {
  67. if(bit_length != 26) {
  68. return 0;
  69. }
  70. //E XXXX XXXX XXXX
  71. //XXXX XXXX XXXX O
  72. uint32_t eBitMask = 0x02000000;
  73. uint32_t oBitMask = 0x00000001;
  74. uint32_t eParityMask = 0x01FFE000;
  75. uint32_t oParityMask = 0x00001FFE;
  76. uint8_t eBit = (eBitMask & bits) >> 25;
  77. uint8_t oBit = (oBitMask & bits) >> 0;
  78. bool eParity = bit_lib_test_parity_32((bits & eParityMask) >> 13, BitLibParityEven) ==
  79. (eBit == 1);
  80. bool oParity = bit_lib_test_parity_32((bits & oParityMask) >> 1, BitLibParityOdd) ==
  81. (oBit == 1);
  82. FURI_LOG_D(
  83. PLUGIN_APP_ID,
  84. "eBit: %d, oBit: %d, eParity: %d, oParity: %d",
  85. eBit,
  86. oBit,
  87. eParity,
  88. oParity);
  89. if(eParity && oParity) {
  90. uint32_t cnMask = 0x1FFFE;
  91. uint16_t cn = ((bits & cnMask) >> 1);
  92. uint32_t fcMask = 0x1FE0000;
  93. uint16_t fc = ((bits & fcMask) >> 17);
  94. furi_string_cat_printf(description, "H10301\nFC: %d CN: %d\n", fc, cn);
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static int wiegand_H10304_parse(uint8_t bit_length, uint64_t bits, FuriString* description) {
  100. if(bit_length != 37) {
  101. return 0;
  102. }
  103. wiegand_message_t value;
  104. value.Mid = bits >> 32;
  105. value.Bot = bits;
  106. wiegand_message_t* packed = &value;
  107. uint32_t fc = get_linear_field(packed, 1, 16);
  108. uint32_t cn = get_linear_field(packed, 17, 19);
  109. bool valid =
  110. (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
  111. (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18)));
  112. if(valid) {
  113. furi_string_cat_printf(description, "H10304\nFC: %ld CN: %ld\n", fc, cn);
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static int wiegand_format_count(uint8_t bit_length, uint64_t bits) {
  119. UNUSED(bit_length);
  120. UNUSED(bits);
  121. int count = 0;
  122. FuriString* ignore = furi_string_alloc();
  123. count += wiegand_h10301_parse(bit_length, bits, ignore);
  124. count += wiegand_C1k35s_parse(bit_length, bits, ignore);
  125. count += wiegand_H10304_parse(bit_length, bits, ignore);
  126. furi_string_free(ignore);
  127. FURI_LOG_I(PLUGIN_APP_ID, "count: %i", count);
  128. return count;
  129. }
  130. static void wiegand_format_description(
  131. uint8_t bit_length,
  132. uint64_t bits,
  133. size_t index,
  134. FuriString* description) {
  135. FURI_LOG_I(PLUGIN_APP_ID, "description %d", index);
  136. UNUSED(bit_length);
  137. UNUSED(bits);
  138. size_t i = 0;
  139. i += wiegand_h10301_parse(bit_length, bits, description);
  140. if(i - 1 == index) {
  141. return;
  142. }
  143. i += wiegand_C1k35s_parse(bit_length, bits, description);
  144. if(i - 1 == index) {
  145. return;
  146. }
  147. furi_string_cat_printf(description, "[%i] <name> FC: CN:", index);
  148. }
  149. /* Actual implementation of app<>plugin interface */
  150. static const PluginWiegand plugin_wiegand = {
  151. .name = "Plugin Wiegand",
  152. .count = &wiegand_format_count,
  153. .description = &wiegand_format_description,
  154. };
  155. /* Plugin descriptor to comply with basic plugin specification */
  156. static const FlipperAppPluginDescriptor plugin_wiegand_descriptor = {
  157. .appid = PLUGIN_APP_ID,
  158. .ep_api_version = PLUGIN_API_VERSION,
  159. .entry_point = &plugin_wiegand,
  160. };
  161. /* Plugin entry point - must return a pointer to const descriptor */
  162. const FlipperAppPluginDescriptor* plugin_wiegand_ep(void) {
  163. return &plugin_wiegand_descriptor;
  164. }