mfkey32.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. string_t str;
  78. string_init_printf(
  79. str,
  80. "Sector %d key %c cuid %08x nt0 %08x nr0 %08x ar0 %08x nt1 %08x nr1 %08x ar1 %08x\n",
  81. params->sector,
  82. params->key == MfClassicKeyA ? 'A' : 'B',
  83. params->cuid,
  84. params->nt0,
  85. params->nr0,
  86. params->ar0,
  87. params->nt1,
  88. params->nr1,
  89. params->ar1);
  90. bool write_success = stream_write_string(instance->file_stream, str);
  91. string_clear(str);
  92. return write_success;
  93. }
  94. static void mfkey32_add_params(Mfkey32* instance) {
  95. Mfkey32Nonce* nonce = &instance->nonce;
  96. bool nonce_added = false;
  97. // Search if we partially collected params
  98. if(Mfkey32Params_size(instance->params_arr)) {
  99. Mfkey32Params_it_t it;
  100. for(Mfkey32Params_it(it, instance->params_arr); !Mfkey32Params_end_p(it);
  101. Mfkey32Params_next(it)) {
  102. Mfkey32Params* params = Mfkey32Params_ref(it);
  103. if((params->sector == nonce->sector) && (params->key == nonce->key)) {
  104. params->nt1 = nonce->nt;
  105. params->nr1 = nonce->nr;
  106. params->ar1 = nonce->ar;
  107. nonce_added = true;
  108. FURI_LOG_I(
  109. TAG,
  110. "Params for sector %d key %c collected",
  111. params->sector,
  112. params->key == MfClassicKeyA ? 'A' : 'B');
  113. // Write on sd card
  114. if(mfkey32_write_params(instance, params)) {
  115. Mfkey32Params_remove(instance->params_arr, it);
  116. if(instance->callback) {
  117. instance->callback(Mfkey32EventParamCollected, instance->context);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. if(!nonce_added) {
  124. Mfkey32Params params = {
  125. .sector = nonce->sector,
  126. .key = nonce->key,
  127. .cuid = instance->cuid,
  128. .nt0 = nonce->nt,
  129. .nr0 = nonce->nr,
  130. .ar0 = nonce->ar,
  131. };
  132. Mfkey32Params_push_back(instance->params_arr, params);
  133. }
  134. }
  135. void mfkey32_process_data(
  136. Mfkey32* instance,
  137. uint8_t* data,
  138. uint16_t len,
  139. bool reader_to_tag,
  140. bool crc_dropped) {
  141. furi_assert(instance);
  142. furi_assert(data);
  143. Mfkey32Nonce* nonce = &instance->nonce;
  144. uint16_t data_len = len;
  145. if((data_len > 3) && !crc_dropped) {
  146. data_len -= 2;
  147. }
  148. bool data_processed = false;
  149. if(instance->state == Mfkey32StateIdle) {
  150. if(reader_to_tag) {
  151. if((data[0] == 0x60) || (data[0] == 0x61)) {
  152. nonce->key = data[0] == 0x60 ? MfClassicKeyA : MfClassicKeyB;
  153. nonce->sector = mf_classic_get_sector_by_block(data[1]);
  154. instance->state = Mfkey32StateAuthReceived;
  155. data_processed = true;
  156. }
  157. }
  158. } else if(instance->state == Mfkey32StateAuthReceived) {
  159. if(!reader_to_tag) {
  160. if(len == 4) {
  161. nonce->nt = nfc_util_bytes2num(data, 4);
  162. instance->state = Mfkey32StateAuthNtSent;
  163. data_processed = true;
  164. }
  165. }
  166. } else if(instance->state == Mfkey32StateAuthNtSent) {
  167. if(reader_to_tag) {
  168. if(len == 8) {
  169. nonce->nr = nfc_util_bytes2num(data, 4);
  170. nonce->ar = nfc_util_bytes2num(&data[4], 4);
  171. mfkey32_add_params(instance);
  172. instance->state = Mfkey32StateIdle;
  173. }
  174. }
  175. }
  176. if(!data_processed) {
  177. instance->state = Mfkey32StateIdle;
  178. }
  179. }
  180. uint16_t mfkey32_get_auth_sectors(string_t data_str) {
  181. furi_assert(data_str);
  182. uint16_t nonces_num = 0;
  183. Storage* storage = furi_record_open(RECORD_STORAGE);
  184. Stream* file_stream = buffered_file_stream_alloc(storage);
  185. string_t temp_str;
  186. string_init(temp_str);
  187. do {
  188. if(!buffered_file_stream_open(
  189. file_stream, MFKEY32_LOGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  190. break;
  191. while(true) {
  192. if(!stream_read_line(file_stream, temp_str)) break;
  193. size_t uid_pos = string_search_str(temp_str, "cuid");
  194. string_left(temp_str, uid_pos);
  195. string_push_back(temp_str, '\n');
  196. string_cat(data_str, temp_str);
  197. nonces_num++;
  198. }
  199. } while(false);
  200. buffered_file_stream_close(file_stream);
  201. stream_free(file_stream);
  202. string_clear(temp_str);
  203. return nonces_num;
  204. }