mfkey32.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "mfkey32.h"
  2. #include <furi/furi.h>
  3. #include <storage/storage.h>
  4. #include <stream/stream.h>
  5. #include <stream/buffered_file_stream.h>
  6. #include <m-array.h>
  7. #include <lib/nfc/protocols/mifare_classic.h>
  8. #include <lib/nfc/protocols/nfc_util.h>
  9. #define TAG "Mfkey32"
  10. #define MFKEY32_LOGS_PATH EXT_PATH("nfc/.mfkey32.log")
  11. typedef enum {
  12. Mfkey32StateIdle,
  13. Mfkey32StateAuthReceived,
  14. Mfkey32StateAuthNtSent,
  15. Mfkey32StateAuthArNrReceived,
  16. } Mfkey32State;
  17. typedef struct {
  18. uint32_t cuid;
  19. uint8_t sector;
  20. MfClassicKey key;
  21. uint32_t nt0;
  22. uint32_t nr0;
  23. uint32_t ar0;
  24. uint32_t nt1;
  25. uint32_t nr1;
  26. uint32_t ar1;
  27. } Mfkey32Params;
  28. ARRAY_DEF(Mfkey32Params, Mfkey32Params, M_POD_OPLIST);
  29. typedef struct {
  30. uint8_t sector;
  31. MfClassicKey key;
  32. uint32_t nt;
  33. uint32_t nr;
  34. uint32_t ar;
  35. } Mfkey32Nonce;
  36. struct Mfkey32 {
  37. Mfkey32State state;
  38. Stream* file_stream;
  39. Mfkey32Params_t params_arr;
  40. Mfkey32Nonce nonce;
  41. uint32_t cuid;
  42. Mfkey32ParseDataCallback callback;
  43. void* context;
  44. };
  45. Mfkey32* mfkey32_alloc(uint32_t cuid) {
  46. Mfkey32* instance = malloc(sizeof(Mfkey32));
  47. instance->cuid = cuid;
  48. instance->state = Mfkey32StateIdle;
  49. Storage* storage = furi_record_open(RECORD_STORAGE);
  50. instance->file_stream = buffered_file_stream_alloc(storage);
  51. if(!buffered_file_stream_open(
  52. instance->file_stream, MFKEY32_LOGS_PATH, FSAM_WRITE, FSOM_OPEN_APPEND)) {
  53. buffered_file_stream_close(instance->file_stream);
  54. stream_free(instance->file_stream);
  55. free(instance);
  56. instance = NULL;
  57. } else {
  58. Mfkey32Params_init(instance->params_arr);
  59. }
  60. furi_record_close(RECORD_STORAGE);
  61. return instance;
  62. }
  63. void mfkey32_free(Mfkey32* instance) {
  64. furi_assert(instance != NULL);
  65. Mfkey32Params_clear(instance->params_arr);
  66. buffered_file_stream_close(instance->file_stream);
  67. stream_free(instance->file_stream);
  68. free(instance);
  69. }
  70. void mfkey32_set_callback(Mfkey32* instance, Mfkey32ParseDataCallback callback, void* context) {
  71. furi_assert(instance);
  72. furi_assert(callback);
  73. instance->callback = callback;
  74. instance->context = context;
  75. }
  76. static bool mfkey32_write_params(Mfkey32* instance, Mfkey32Params* params) {
  77. FuriString* str = furi_string_alloc_printf(
  78. "Sec %d key %c cuid %08lx nt0 %08lx nr0 %08lx ar0 %08lx nt1 %08lx nr1 %08lx ar1 %08lx\n",
  79. params->sector,
  80. params->key == MfClassicKeyA ? 'A' : 'B',
  81. params->cuid,
  82. params->nt0,
  83. params->nr0,
  84. params->ar0,
  85. params->nt1,
  86. params->nr1,
  87. params->ar1);
  88. bool write_success = stream_write_string(instance->file_stream, str);
  89. furi_string_free(str);
  90. return write_success;
  91. }
  92. static void mfkey32_add_params(Mfkey32* instance) {
  93. Mfkey32Nonce* nonce = &instance->nonce;
  94. bool nonce_added = false;
  95. // Search if we partially collected params
  96. if(Mfkey32Params_size(instance->params_arr)) {
  97. Mfkey32Params_it_t it;
  98. for(Mfkey32Params_it(it, instance->params_arr); !Mfkey32Params_end_p(it);
  99. Mfkey32Params_next(it)) {
  100. Mfkey32Params* params = Mfkey32Params_ref(it);
  101. if((params->sector == nonce->sector) && (params->key == nonce->key)) {
  102. params->nt1 = nonce->nt;
  103. params->nr1 = nonce->nr;
  104. params->ar1 = nonce->ar;
  105. nonce_added = true;
  106. FURI_LOG_I(
  107. TAG,
  108. "Params for sector %d key %c collected",
  109. params->sector,
  110. params->key == MfClassicKeyA ? 'A' : 'B');
  111. // Write on sd card
  112. if(mfkey32_write_params(instance, params)) {
  113. Mfkey32Params_remove(instance->params_arr, it);
  114. if(instance->callback) {
  115. instance->callback(Mfkey32EventParamCollected, instance->context);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. if(!nonce_added) {
  122. Mfkey32Params params = {
  123. .sector = nonce->sector,
  124. .key = nonce->key,
  125. .cuid = instance->cuid,
  126. .nt0 = nonce->nt,
  127. .nr0 = nonce->nr,
  128. .ar0 = nonce->ar,
  129. };
  130. Mfkey32Params_push_back(instance->params_arr, params);
  131. }
  132. }
  133. void mfkey32_process_data(
  134. Mfkey32* instance,
  135. uint8_t* data,
  136. uint16_t len,
  137. bool reader_to_tag,
  138. bool crc_dropped) {
  139. furi_assert(instance);
  140. furi_assert(data);
  141. Mfkey32Nonce* nonce = &instance->nonce;
  142. uint16_t data_len = len;
  143. if((data_len > 3) && !crc_dropped) {
  144. data_len -= 2;
  145. }
  146. bool data_processed = false;
  147. if(instance->state == Mfkey32StateIdle) {
  148. if(reader_to_tag) {
  149. if((data[0] == 0x60) || (data[0] == 0x61)) {
  150. nonce->key = data[0] == 0x60 ? MfClassicKeyA : MfClassicKeyB;
  151. nonce->sector = mf_classic_get_sector_by_block(data[1]);
  152. instance->state = Mfkey32StateAuthReceived;
  153. data_processed = true;
  154. }
  155. }
  156. } else if(instance->state == Mfkey32StateAuthReceived) {
  157. if(!reader_to_tag) {
  158. if(len == 4) {
  159. nonce->nt = nfc_util_bytes2num(data, 4);
  160. instance->state = Mfkey32StateAuthNtSent;
  161. data_processed = true;
  162. }
  163. }
  164. } else if(instance->state == Mfkey32StateAuthNtSent) {
  165. if(reader_to_tag) {
  166. if(len == 8) {
  167. nonce->nr = nfc_util_bytes2num(data, 4);
  168. nonce->ar = nfc_util_bytes2num(&data[4], 4);
  169. mfkey32_add_params(instance);
  170. instance->state = Mfkey32StateIdle;
  171. }
  172. }
  173. }
  174. if(!data_processed) {
  175. instance->state = Mfkey32StateIdle;
  176. }
  177. }
  178. uint16_t mfkey32_get_auth_sectors(FuriString* data_str) {
  179. furi_assert(data_str);
  180. uint16_t nonces_num = 0;
  181. Storage* storage = furi_record_open(RECORD_STORAGE);
  182. Stream* file_stream = buffered_file_stream_alloc(storage);
  183. FuriString* temp_str;
  184. temp_str = furi_string_alloc();
  185. do {
  186. if(!buffered_file_stream_open(
  187. file_stream, MFKEY32_LOGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  188. break;
  189. while(true) {
  190. if(!stream_read_line(file_stream, temp_str)) break;
  191. size_t uid_pos = furi_string_search(temp_str, "cuid");
  192. furi_string_left(temp_str, uid_pos);
  193. furi_string_push_back(temp_str, '\n');
  194. furi_string_cat(data_str, temp_str);
  195. nonces_num++;
  196. }
  197. } while(false);
  198. buffered_file_stream_close(file_stream);
  199. stream_free(file_stream);
  200. furi_string_free(temp_str);
  201. return nonces_num;
  202. }