mifare_classic.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. #include "mifare_classic.h"
  2. #include "nfca.h"
  3. #include "nfc_util.h"
  4. #include <furi_hal_rtc.h>
  5. // Algorithm from https://github.com/RfidResearchGroup/proxmark3.git
  6. #define TAG "MfClassic"
  7. #define MF_CLASSIC_AUTH_KEY_A_CMD (0x60U)
  8. #define MF_CLASSIC_AUTH_KEY_B_CMD (0x61U)
  9. #define MF_CLASSIC_READ_SECT_CMD (0x30)
  10. typedef enum {
  11. MfClassicActionDataRead,
  12. MfClassicActionDataWrite,
  13. MfClassicActionDataInc,
  14. MfClassicActionDataDec,
  15. MfClassicActionKeyARead,
  16. MfClassicActionKeyAWrite,
  17. MfClassicActionKeyBRead,
  18. MfClassicActionKeyBWrite,
  19. MfClassicActionACRead,
  20. MfClassicActionACWrite,
  21. } MfClassicAction;
  22. const char* mf_classic_get_type_str(MfClassicType type) {
  23. if(type == MfClassicType1k) {
  24. return "MIFARE Classic 1K";
  25. } else if(type == MfClassicType4k) {
  26. return "MIFARE Classic 4K";
  27. } else {
  28. return "Unknown";
  29. }
  30. }
  31. static uint8_t mf_classic_get_first_block_num_of_sector(uint8_t sector) {
  32. furi_assert(sector < 40);
  33. if(sector < 32) {
  34. return sector * 4;
  35. } else {
  36. return 32 * 4 + (sector - 32) * 16;
  37. }
  38. }
  39. uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector) {
  40. furi_assert(sector < 40);
  41. if(sector < 32) {
  42. return sector * 4 + 3;
  43. } else {
  44. return 32 * 4 + (sector - 32) * 16 + 15;
  45. }
  46. }
  47. uint8_t mf_classic_get_sector_by_block(uint8_t block) {
  48. if(block < 128) {
  49. return (block | 0x03) / 4;
  50. } else {
  51. return 32 + ((block | 0xf) - 32 * 4) / 16;
  52. }
  53. }
  54. static uint8_t mf_classic_get_blocks_num_in_sector(uint8_t sector) {
  55. furi_assert(sector < 40);
  56. return sector < 32 ? 4 : 16;
  57. }
  58. uint8_t mf_classic_get_sector_trailer_num_by_block(uint8_t block) {
  59. if(block < 128) {
  60. return block | 0x03;
  61. } else {
  62. return block | 0x0f;
  63. }
  64. }
  65. bool mf_classic_is_sector_trailer(uint8_t block) {
  66. return block == mf_classic_get_sector_trailer_num_by_block(block);
  67. }
  68. MfClassicSectorTrailer*
  69. mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector) {
  70. furi_assert(data);
  71. uint8_t sec_tr_block_num = mf_classic_get_sector_trailer_block_num_by_sector(sector);
  72. return (MfClassicSectorTrailer*)data->block[sec_tr_block_num].value;
  73. }
  74. uint8_t mf_classic_get_total_sectors_num(MfClassicType type) {
  75. if(type == MfClassicType1k) {
  76. return MF_CLASSIC_1K_TOTAL_SECTORS_NUM;
  77. } else if(type == MfClassicType4k) {
  78. return MF_CLASSIC_4K_TOTAL_SECTORS_NUM;
  79. } else {
  80. return 0;
  81. }
  82. }
  83. static uint16_t mf_classic_get_total_block_num(MfClassicType type) {
  84. if(type == MfClassicType1k) {
  85. return 64;
  86. } else if(type == MfClassicType4k) {
  87. return 256;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num) {
  93. furi_assert(data);
  94. return (FURI_BIT(data->block_read_mask[block_num / 32], block_num % 32) == 1);
  95. }
  96. void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data) {
  97. furi_assert(data);
  98. if(mf_classic_is_sector_trailer(block_num)) {
  99. memcpy(&data->block[block_num].value[6], &block_data->value[6], 4);
  100. } else {
  101. memcpy(data->block[block_num].value, block_data->value, MF_CLASSIC_BLOCK_SIZE);
  102. }
  103. FURI_BIT_SET(data->block_read_mask[block_num / 32], block_num % 32);
  104. }
  105. bool mf_classic_is_key_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type) {
  106. furi_assert(data);
  107. bool key_found = false;
  108. if(key_type == MfClassicKeyA) {
  109. key_found = (FURI_BIT(data->key_a_mask, sector_num) == 1);
  110. } else if(key_type == MfClassicKeyB) {
  111. key_found = (FURI_BIT(data->key_b_mask, sector_num) == 1);
  112. }
  113. return key_found;
  114. }
  115. void mf_classic_set_key_found(
  116. MfClassicData* data,
  117. uint8_t sector_num,
  118. MfClassicKey key_type,
  119. uint64_t key) {
  120. furi_assert(data);
  121. uint8_t key_arr[6] = {};
  122. MfClassicSectorTrailer* sec_trailer =
  123. mf_classic_get_sector_trailer_by_sector(data, sector_num);
  124. nfc_util_num2bytes(key, 6, key_arr);
  125. if(key_type == MfClassicKeyA) {
  126. memcpy(sec_trailer->key_a, key_arr, sizeof(sec_trailer->key_a));
  127. FURI_BIT_SET(data->key_a_mask, sector_num);
  128. } else if(key_type == MfClassicKeyB) {
  129. memcpy(sec_trailer->key_b, key_arr, sizeof(sec_trailer->key_b));
  130. FURI_BIT_SET(data->key_b_mask, sector_num);
  131. }
  132. }
  133. void mf_classic_set_key_not_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type) {
  134. furi_assert(data);
  135. if(key_type == MfClassicKeyA) {
  136. FURI_BIT_CLEAR(data->key_a_mask, sector_num);
  137. } else if(key_type == MfClassicKeyB) {
  138. FURI_BIT_CLEAR(data->key_b_mask, sector_num);
  139. }
  140. }
  141. bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num) {
  142. furi_assert(data);
  143. bool sector_read = false;
  144. do {
  145. if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) break;
  146. if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) break;
  147. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sector_num);
  148. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sector_num);
  149. uint8_t block_read = true;
  150. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  151. block_read = mf_classic_is_block_read(data, i);
  152. if(!block_read) break;
  153. }
  154. sector_read = block_read;
  155. } while(false);
  156. return sector_read;
  157. }
  158. void mf_classic_get_read_sectors_and_keys(
  159. MfClassicData* data,
  160. uint8_t* sectors_read,
  161. uint8_t* keys_found) {
  162. furi_assert(data);
  163. *sectors_read = 0;
  164. *keys_found = 0;
  165. uint8_t sectors_total = mf_classic_get_total_sectors_num(data->type);
  166. for(size_t i = 0; i < sectors_total; i++) {
  167. if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
  168. *keys_found += 1;
  169. }
  170. if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
  171. *keys_found += 1;
  172. }
  173. uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
  174. uint8_t total_blocks_in_sec = mf_classic_get_blocks_num_in_sector(i);
  175. bool blocks_read = true;
  176. for(size_t i = first_block; i < first_block + total_blocks_in_sec; i++) {
  177. blocks_read = mf_classic_is_block_read(data, i);
  178. if(!blocks_read) break;
  179. }
  180. if(blocks_read) {
  181. *sectors_read += 1;
  182. }
  183. }
  184. }
  185. bool mf_classic_is_card_read(MfClassicData* data) {
  186. furi_assert(data);
  187. uint8_t sectors_total = mf_classic_get_total_sectors_num(data->type);
  188. uint8_t sectors_read = 0;
  189. uint8_t keys_found = 0;
  190. mf_classic_get_read_sectors_and_keys(data, &sectors_read, &keys_found);
  191. bool card_read = (sectors_read == sectors_total) && (keys_found == sectors_total * 2);
  192. return card_read;
  193. }
  194. static bool mf_classic_is_allowed_access_sector_trailer(
  195. MfClassicEmulator* emulator,
  196. uint8_t block_num,
  197. MfClassicKey key,
  198. MfClassicAction action) {
  199. uint8_t* sector_trailer = emulator->data.block[block_num].value;
  200. uint8_t AC = ((sector_trailer[7] >> 5) & 0x04) | ((sector_trailer[8] >> 2) & 0x02) |
  201. ((sector_trailer[8] >> 7) & 0x01);
  202. switch(action) {
  203. case MfClassicActionKeyARead: {
  204. return false;
  205. }
  206. case MfClassicActionKeyAWrite: {
  207. return (
  208. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  209. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  210. }
  211. case MfClassicActionKeyBRead: {
  212. return (key == MfClassicKeyA && (AC == 0x00 || AC == 0x02 || AC == 0x01));
  213. }
  214. case MfClassicActionKeyBWrite: {
  215. return (
  216. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  217. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  218. }
  219. case MfClassicActionACRead: {
  220. return (
  221. (key == MfClassicKeyA) ||
  222. (key == MfClassicKeyB && !(AC == 0x00 || AC == 0x02 || AC == 0x01)));
  223. }
  224. case MfClassicActionACWrite: {
  225. return (
  226. (key == MfClassicKeyA && (AC == 0x01)) ||
  227. (key == MfClassicKeyB && (AC == 0x03 || AC == 0x05)));
  228. }
  229. default:
  230. return false;
  231. }
  232. return true;
  233. }
  234. static bool mf_classic_is_allowed_access_data_block(
  235. MfClassicEmulator* emulator,
  236. uint8_t block_num,
  237. MfClassicKey key,
  238. MfClassicAction action) {
  239. uint8_t* sector_trailer =
  240. emulator->data.block[mf_classic_get_sector_trailer_num_by_block(block_num)].value;
  241. uint8_t sector_block;
  242. if(block_num <= 128) {
  243. sector_block = block_num & 0x03;
  244. } else {
  245. sector_block = (block_num & 0x0f) / 5;
  246. }
  247. uint8_t AC;
  248. switch(sector_block) {
  249. case 0x00: {
  250. AC = ((sector_trailer[7] >> 2) & 0x04) | ((sector_trailer[8] << 1) & 0x02) |
  251. ((sector_trailer[8] >> 4) & 0x01);
  252. break;
  253. }
  254. case 0x01: {
  255. AC = ((sector_trailer[7] >> 3) & 0x04) | ((sector_trailer[8] >> 0) & 0x02) |
  256. ((sector_trailer[8] >> 5) & 0x01);
  257. break;
  258. }
  259. case 0x02: {
  260. AC = ((sector_trailer[7] >> 4) & 0x04) | ((sector_trailer[8] >> 1) & 0x02) |
  261. ((sector_trailer[8] >> 6) & 0x01);
  262. break;
  263. }
  264. default:
  265. return false;
  266. }
  267. switch(action) {
  268. case MfClassicActionDataRead: {
  269. return (
  270. (key == MfClassicKeyA && !(AC == 0x03 || AC == 0x05 || AC == 0x07)) ||
  271. (key == MfClassicKeyB && !(AC == 0x07)));
  272. }
  273. case MfClassicActionDataWrite: {
  274. return (
  275. (key == MfClassicKeyA && (AC == 0x00)) ||
  276. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x04 || AC == 0x06 || AC == 0x03)));
  277. }
  278. case MfClassicActionDataInc: {
  279. return (
  280. (key == MfClassicKeyA && (AC == 0x00)) ||
  281. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06)));
  282. }
  283. case MfClassicActionDataDec: {
  284. return (
  285. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x06 || AC == 0x01)) ||
  286. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06 || AC == 0x01)));
  287. }
  288. default:
  289. return false;
  290. }
  291. return false;
  292. }
  293. static bool mf_classic_is_allowed_access(
  294. MfClassicEmulator* emulator,
  295. uint8_t block_num,
  296. MfClassicKey key,
  297. MfClassicAction action) {
  298. if(mf_classic_is_sector_trailer(block_num)) {
  299. return mf_classic_is_allowed_access_sector_trailer(emulator, block_num, key, action);
  300. } else {
  301. return mf_classic_is_allowed_access_data_block(emulator, block_num, key, action);
  302. }
  303. }
  304. bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  305. UNUSED(ATQA1);
  306. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  307. return true;
  308. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  309. //skylanders support
  310. return true;
  311. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  312. return true;
  313. } else {
  314. return false;
  315. }
  316. }
  317. MfClassicType mf_classic_get_classic_type(int8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  318. UNUSED(ATQA1);
  319. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  320. return MfClassicType1k;
  321. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  322. //skylanders support
  323. return MfClassicType1k;
  324. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  325. return MfClassicType4k;
  326. }
  327. return MfClassicType1k;
  328. }
  329. void mf_classic_reader_add_sector(
  330. MfClassicReader* reader,
  331. uint8_t sector,
  332. uint64_t key_a,
  333. uint64_t key_b) {
  334. furi_assert(reader);
  335. furi_assert(sector < MF_CLASSIC_SECTORS_MAX);
  336. furi_assert((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY));
  337. if(reader->sectors_to_read < MF_CLASSIC_SECTORS_MAX) {
  338. reader->sector_reader[reader->sectors_to_read].key_a = key_a;
  339. reader->sector_reader[reader->sectors_to_read].key_b = key_b;
  340. reader->sector_reader[reader->sectors_to_read].sector_num = sector;
  341. reader->sectors_to_read++;
  342. }
  343. }
  344. void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector) {
  345. furi_assert(auth_ctx);
  346. auth_ctx->sector = sector;
  347. auth_ctx->key_a = MF_CLASSIC_NO_KEY;
  348. auth_ctx->key_b = MF_CLASSIC_NO_KEY;
  349. }
  350. static bool mf_classic_auth(
  351. FuriHalNfcTxRxContext* tx_rx,
  352. uint32_t block,
  353. uint64_t key,
  354. MfClassicKey key_type,
  355. Crypto1* crypto) {
  356. bool auth_success = false;
  357. uint32_t cuid = 0;
  358. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  359. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  360. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  361. do {
  362. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  363. if(key_type == MfClassicKeyA) {
  364. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
  365. } else {
  366. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
  367. }
  368. tx_rx->tx_data[1] = block;
  369. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
  370. tx_rx->tx_bits = 2 * 8;
  371. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  372. uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
  373. crypto1_init(crypto, key);
  374. crypto1_word(crypto, nt ^ cuid, 0);
  375. uint8_t nr[4] = {};
  376. nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
  377. for(uint8_t i = 0; i < 4; i++) {
  378. tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
  379. tx_rx->tx_parity[0] |=
  380. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
  381. }
  382. nt = prng_successor(nt, 32);
  383. for(uint8_t i = 4; i < 8; i++) {
  384. nt = prng_successor(nt, 8);
  385. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
  386. tx_rx->tx_parity[0] |=
  387. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
  388. << (7 - i));
  389. }
  390. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  391. tx_rx->tx_bits = 8 * 8;
  392. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  393. if(tx_rx->rx_bits == 32) {
  394. crypto1_word(crypto, 0, 0);
  395. auth_success = true;
  396. }
  397. } while(false);
  398. return auth_success;
  399. }
  400. bool mf_classic_authenticate(
  401. FuriHalNfcTxRxContext* tx_rx,
  402. uint8_t block_num,
  403. uint64_t key,
  404. MfClassicKey key_type) {
  405. furi_assert(tx_rx);
  406. Crypto1 crypto = {};
  407. bool key_found = mf_classic_auth(tx_rx, block_num, key, key_type, &crypto);
  408. furi_hal_nfc_sleep();
  409. return key_found;
  410. }
  411. bool mf_classic_auth_attempt(
  412. FuriHalNfcTxRxContext* tx_rx,
  413. MfClassicAuthContext* auth_ctx,
  414. uint64_t key) {
  415. furi_assert(tx_rx);
  416. furi_assert(auth_ctx);
  417. bool found_key = false;
  418. bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
  419. (auth_ctx->key_b == MF_CLASSIC_NO_KEY);
  420. Crypto1 crypto;
  421. if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
  422. // Try AUTH with key A
  423. if(mf_classic_auth(
  424. tx_rx,
  425. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  426. key,
  427. MfClassicKeyA,
  428. &crypto)) {
  429. auth_ctx->key_a = key;
  430. found_key = true;
  431. }
  432. }
  433. if(need_halt) {
  434. furi_hal_nfc_sleep();
  435. }
  436. if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
  437. // Try AUTH with key B
  438. if(mf_classic_auth(
  439. tx_rx,
  440. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  441. key,
  442. MfClassicKeyB,
  443. &crypto)) {
  444. auth_ctx->key_b = key;
  445. found_key = true;
  446. }
  447. }
  448. return found_key;
  449. }
  450. bool mf_classic_read_block(
  451. FuriHalNfcTxRxContext* tx_rx,
  452. Crypto1* crypto,
  453. uint8_t block_num,
  454. MfClassicBlock* block) {
  455. furi_assert(tx_rx);
  456. furi_assert(crypto);
  457. furi_assert(block);
  458. bool read_block_success = false;
  459. uint8_t plain_cmd[4] = {MF_CLASSIC_READ_SECT_CMD, block_num, 0x00, 0x00};
  460. nfca_append_crc16(plain_cmd, 2);
  461. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  462. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  463. for(uint8_t i = 0; i < 4; i++) {
  464. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ plain_cmd[i];
  465. tx_rx->tx_parity[0] |=
  466. ((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_cmd[i])) & 0x01) << (7 - i);
  467. }
  468. tx_rx->tx_bits = 4 * 9;
  469. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  470. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  471. if(tx_rx->rx_bits == 8 * (MF_CLASSIC_BLOCK_SIZE + 2)) {
  472. uint8_t block_received[MF_CLASSIC_BLOCK_SIZE + 2];
  473. for(uint8_t i = 0; i < MF_CLASSIC_BLOCK_SIZE + 2; i++) {
  474. block_received[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
  475. }
  476. uint16_t crc_calc = nfca_get_crc16(block_received, MF_CLASSIC_BLOCK_SIZE);
  477. uint16_t crc_received = (block_received[MF_CLASSIC_BLOCK_SIZE + 1] << 8) |
  478. block_received[MF_CLASSIC_BLOCK_SIZE];
  479. if(crc_received != crc_calc) {
  480. FURI_LOG_E(
  481. TAG,
  482. "Incorrect CRC while reading block %d. Expected %04X, Received %04X",
  483. block_num,
  484. crc_received,
  485. crc_calc);
  486. } else {
  487. memcpy(block->value, block_received, MF_CLASSIC_BLOCK_SIZE);
  488. read_block_success = true;
  489. }
  490. }
  491. }
  492. return read_block_success;
  493. }
  494. void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num) {
  495. furi_assert(tx_rx);
  496. furi_assert(data);
  497. furi_hal_nfc_sleep();
  498. bool key_a_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyA);
  499. bool key_b_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyB);
  500. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sec_num);
  501. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sec_num);
  502. MfClassicBlock block_tmp = {};
  503. uint64_t key = 0;
  504. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sec_num);
  505. Crypto1 crypto = {};
  506. uint8_t blocks_read = 0;
  507. do {
  508. if(!key_a_found) break;
  509. FURI_LOG_D(TAG, "Try to read blocks with key A");
  510. key = nfc_util_bytes2num(sec_tr->key_a, sizeof(sec_tr->key_a));
  511. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyA, &crypto)) break;
  512. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  513. if(!mf_classic_is_block_read(data, i)) {
  514. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  515. mf_classic_set_block_read(data, i, &block_tmp);
  516. blocks_read++;
  517. }
  518. } else {
  519. blocks_read++;
  520. }
  521. }
  522. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  523. } while(false);
  524. do {
  525. if(blocks_read == total_blocks) break;
  526. if(!key_b_found) break;
  527. FURI_LOG_D(TAG, "Try to read blocks with key B");
  528. key = nfc_util_bytes2num(sec_tr->key_b, sizeof(sec_tr->key_b));
  529. furi_hal_nfc_sleep();
  530. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto)) break;
  531. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  532. if(!mf_classic_is_block_read(data, i)) {
  533. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  534. mf_classic_set_block_read(data, i, &block_tmp);
  535. blocks_read++;
  536. }
  537. } else {
  538. blocks_read++;
  539. }
  540. }
  541. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  542. } while(false);
  543. }
  544. static bool mf_classic_read_sector_with_reader(
  545. FuriHalNfcTxRxContext* tx_rx,
  546. Crypto1* crypto,
  547. MfClassicSectorReader* sector_reader,
  548. MfClassicSector* sector) {
  549. furi_assert(tx_rx);
  550. furi_assert(sector_reader);
  551. furi_assert(sector);
  552. uint64_t key;
  553. MfClassicKey key_type;
  554. uint8_t first_block;
  555. bool sector_read = false;
  556. furi_hal_nfc_sleep();
  557. do {
  558. // Activate card
  559. first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
  560. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  561. key = sector_reader->key_a;
  562. key_type = MfClassicKeyA;
  563. } else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  564. key = sector_reader->key_b;
  565. key_type = MfClassicKeyB;
  566. } else {
  567. break;
  568. }
  569. // Auth to first block in sector
  570. if(!mf_classic_auth(tx_rx, first_block, key, key_type, crypto)) {
  571. // Set key to MF_CLASSIC_NO_KEY to prevent further attempts
  572. if(key_type == MfClassicKeyA) {
  573. sector_reader->key_a = MF_CLASSIC_NO_KEY;
  574. } else {
  575. sector_reader->key_b = MF_CLASSIC_NO_KEY;
  576. }
  577. break;
  578. }
  579. sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
  580. // Read blocks
  581. for(uint8_t i = 0; i < sector->total_blocks; i++) {
  582. mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i]);
  583. }
  584. // Save sector keys in last block
  585. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  586. nfc_util_num2bytes(
  587. sector_reader->key_a, 6, &sector->block[sector->total_blocks - 1].value[0]);
  588. }
  589. if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  590. nfc_util_num2bytes(
  591. sector_reader->key_b, 6, &sector->block[sector->total_blocks - 1].value[10]);
  592. }
  593. sector_read = true;
  594. } while(false);
  595. return sector_read;
  596. }
  597. uint8_t mf_classic_read_card(
  598. FuriHalNfcTxRxContext* tx_rx,
  599. MfClassicReader* reader,
  600. MfClassicData* data) {
  601. furi_assert(tx_rx);
  602. furi_assert(reader);
  603. furi_assert(data);
  604. uint8_t sectors_read = 0;
  605. data->type = reader->type;
  606. data->key_a_mask = 0;
  607. data->key_b_mask = 0;
  608. MfClassicSector temp_sector = {};
  609. for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
  610. if(mf_classic_read_sector_with_reader(
  611. tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
  612. uint8_t first_block =
  613. mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
  614. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  615. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  616. }
  617. if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
  618. mf_classic_set_key_found(
  619. data,
  620. reader->sector_reader[i].sector_num,
  621. MfClassicKeyA,
  622. reader->sector_reader[i].key_a);
  623. }
  624. if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
  625. mf_classic_set_key_found(
  626. data,
  627. reader->sector_reader[i].sector_num,
  628. MfClassicKeyB,
  629. reader->sector_reader[i].key_b);
  630. }
  631. sectors_read++;
  632. }
  633. }
  634. return sectors_read;
  635. }
  636. uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data) {
  637. furi_assert(tx_rx);
  638. furi_assert(data);
  639. uint8_t sectors_read = 0;
  640. Crypto1 crypto = {};
  641. uint8_t total_sectors = mf_classic_get_total_sectors_num(data->type);
  642. uint64_t key_a = 0;
  643. uint64_t key_b = 0;
  644. MfClassicSectorReader sec_reader = {};
  645. MfClassicSector temp_sector = {};
  646. for(size_t i = 0; i < total_sectors; i++) {
  647. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  648. // Load key A
  649. if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
  650. sec_reader.key_a = nfc_util_bytes2num(sec_tr->key_a, 6);
  651. } else {
  652. sec_reader.key_a = MF_CLASSIC_NO_KEY;
  653. }
  654. // Load key B
  655. if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
  656. sec_reader.key_b = nfc_util_bytes2num(sec_tr->key_b, 6);
  657. } else {
  658. sec_reader.key_b = MF_CLASSIC_NO_KEY;
  659. }
  660. if((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY)) {
  661. sec_reader.sector_num = i;
  662. if(mf_classic_read_sector_with_reader(tx_rx, &crypto, &sec_reader, &temp_sector)) {
  663. uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
  664. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  665. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  666. }
  667. sectors_read++;
  668. } else {
  669. // Invalid key, set it to not found
  670. if(key_a != MF_CLASSIC_NO_KEY) {
  671. mf_classic_set_key_not_found(data, i, MfClassicKeyA);
  672. } else {
  673. mf_classic_set_key_not_found(data, i, MfClassicKeyB);
  674. }
  675. }
  676. }
  677. }
  678. return sectors_read;
  679. }
  680. void mf_crypto1_decrypt(
  681. Crypto1* crypto,
  682. uint8_t* encrypted_data,
  683. uint16_t encrypted_data_bits,
  684. uint8_t* decrypted_data) {
  685. if(encrypted_data_bits < 8) {
  686. uint8_t decrypted_byte = 0;
  687. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
  688. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
  689. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
  690. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
  691. decrypted_data[0] = decrypted_byte;
  692. } else {
  693. for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
  694. decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
  695. }
  696. }
  697. }
  698. void mf_crypto1_encrypt(
  699. Crypto1* crypto,
  700. uint8_t* keystream,
  701. uint8_t* plain_data,
  702. uint16_t plain_data_bits,
  703. uint8_t* encrypted_data,
  704. uint8_t* encrypted_parity) {
  705. if(plain_data_bits < 8) {
  706. encrypted_data[0] = 0;
  707. for(size_t i = 0; i < plain_data_bits; i++) {
  708. encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
  709. }
  710. } else {
  711. memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
  712. for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
  713. encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
  714. plain_data[i];
  715. encrypted_parity[i / 8] |=
  716. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
  717. << (7 - (i & 0x0007)));
  718. }
  719. }
  720. }
  721. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
  722. furi_assert(emulator);
  723. furi_assert(tx_rx);
  724. bool command_processed = false;
  725. bool is_encrypted = false;
  726. uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
  727. MfClassicKey access_key = MfClassicKeyA;
  728. // Read command
  729. while(!command_processed) {
  730. if(!is_encrypted) {
  731. crypto1_reset(&emulator->crypto);
  732. memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
  733. } else {
  734. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
  735. FURI_LOG_D(
  736. TAG,
  737. "Error in tx rx. Tx :%d bits, Rx: %d bits",
  738. tx_rx->tx_bits,
  739. tx_rx->rx_bits);
  740. break;
  741. }
  742. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  743. }
  744. if(plain_data[0] == 0x50 && plain_data[1] == 0x00) {
  745. FURI_LOG_T(TAG, "Halt received");
  746. furi_hal_nfc_listen_sleep();
  747. command_processed = true;
  748. break;
  749. } else if(plain_data[0] == 0x60 || plain_data[0] == 0x61) {
  750. uint8_t block = plain_data[1];
  751. uint64_t key = 0;
  752. uint8_t sector_trailer_block = mf_classic_get_sector_trailer_num_by_block(block);
  753. MfClassicSectorTrailer* sector_trailer =
  754. (MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
  755. if(plain_data[0] == 0x60) {
  756. key = nfc_util_bytes2num(sector_trailer->key_a, 6);
  757. access_key = MfClassicKeyA;
  758. } else {
  759. key = nfc_util_bytes2num(sector_trailer->key_b, 6);
  760. access_key = MfClassicKeyB;
  761. }
  762. uint32_t nonce = prng_successor(DWT->CYCCNT, 32) ^ 0xAA;
  763. uint8_t nt[4];
  764. uint8_t nt_keystream[4];
  765. nfc_util_num2bytes(nonce, 4, nt);
  766. nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
  767. crypto1_init(&emulator->crypto, key);
  768. if(!is_encrypted) {
  769. crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
  770. memcpy(tx_rx->tx_data, nt, sizeof(nt));
  771. tx_rx->tx_parity[0] = 0;
  772. for(size_t i = 0; i < sizeof(nt); i++) {
  773. tx_rx->tx_parity[0] |= nfc_util_odd_parity8(nt[i]) << (7 - i);
  774. }
  775. tx_rx->tx_bits = sizeof(nt) * 8;
  776. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  777. } else {
  778. mf_crypto1_encrypt(
  779. &emulator->crypto,
  780. nt_keystream,
  781. nt,
  782. sizeof(nt) * 8,
  783. tx_rx->tx_data,
  784. tx_rx->tx_parity);
  785. tx_rx->tx_bits = sizeof(nt) * 8;
  786. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  787. }
  788. if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
  789. FURI_LOG_E(TAG, "Error in NT exchange");
  790. command_processed = true;
  791. break;
  792. }
  793. if(tx_rx->rx_bits != 64) {
  794. FURI_LOG_W(TAG, "Incorrect nr + ar");
  795. command_processed = true;
  796. break;
  797. }
  798. uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
  799. uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
  800. FURI_LOG_D(
  801. TAG,
  802. "%08lx key%c block %d nt/nr/ar: %08lx %08lx %08lx",
  803. emulator->cuid,
  804. access_key == MfClassicKeyA ? 'A' : 'B',
  805. sector_trailer_block,
  806. nonce,
  807. nr,
  808. ar);
  809. crypto1_word(&emulator->crypto, nr, 1);
  810. uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
  811. if(cardRr != prng_successor(nonce, 64)) {
  812. FURI_LOG_T(TAG, "Wrong AUTH! %08lX != %08lX", cardRr, prng_successor(nonce, 64));
  813. // Don't send NACK, as the tag doesn't send it
  814. command_processed = true;
  815. break;
  816. }
  817. uint32_t ans = prng_successor(nonce, 96);
  818. uint8_t responce[4] = {};
  819. nfc_util_num2bytes(ans, 4, responce);
  820. mf_crypto1_encrypt(
  821. &emulator->crypto,
  822. NULL,
  823. responce,
  824. sizeof(responce) * 8,
  825. tx_rx->tx_data,
  826. tx_rx->tx_parity);
  827. tx_rx->tx_bits = sizeof(responce) * 8;
  828. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  829. is_encrypted = true;
  830. } else if(is_encrypted && plain_data[0] == 0x30) {
  831. uint8_t block = plain_data[1];
  832. uint8_t block_data[18] = {};
  833. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  834. if(mf_classic_is_sector_trailer(block)) {
  835. if(!mf_classic_is_allowed_access(
  836. emulator, block, access_key, MfClassicActionKeyARead)) {
  837. memset(block_data, 0, 6);
  838. }
  839. if(!mf_classic_is_allowed_access(
  840. emulator, block, access_key, MfClassicActionKeyBRead)) {
  841. memset(&block_data[10], 0, 6);
  842. }
  843. if(!mf_classic_is_allowed_access(
  844. emulator, block, access_key, MfClassicActionACRead)) {
  845. memset(&block_data[6], 0, 4);
  846. }
  847. } else {
  848. if(!mf_classic_is_allowed_access(
  849. emulator, block, access_key, MfClassicActionDataRead)) {
  850. // Send NACK
  851. uint8_t nack = 0x04;
  852. if(is_encrypted) {
  853. mf_crypto1_encrypt(
  854. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  855. } else {
  856. tx_rx->tx_data[0] = nack;
  857. }
  858. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  859. tx_rx->tx_bits = 4;
  860. furi_hal_nfc_tx_rx(tx_rx, 300);
  861. break;
  862. }
  863. }
  864. nfca_append_crc16(block_data, 16);
  865. mf_crypto1_encrypt(
  866. &emulator->crypto,
  867. NULL,
  868. block_data,
  869. sizeof(block_data) * 8,
  870. tx_rx->tx_data,
  871. tx_rx->tx_parity);
  872. tx_rx->tx_bits = 18 * 8;
  873. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  874. } else if(is_encrypted && plain_data[0] == 0xA0) {
  875. uint8_t block = plain_data[1];
  876. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  877. break;
  878. }
  879. // Send ACK
  880. uint8_t ack = 0x0A;
  881. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  882. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  883. tx_rx->tx_bits = 4;
  884. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  885. if(tx_rx->rx_bits != 18 * 8) break;
  886. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  887. uint8_t block_data[16] = {};
  888. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  889. if(mf_classic_is_sector_trailer(block)) {
  890. if(mf_classic_is_allowed_access(
  891. emulator, block, access_key, MfClassicActionKeyAWrite)) {
  892. memcpy(block_data, plain_data, 6);
  893. }
  894. if(mf_classic_is_allowed_access(
  895. emulator, block, access_key, MfClassicActionKeyBWrite)) {
  896. memcpy(&block_data[10], &plain_data[10], 6);
  897. }
  898. if(mf_classic_is_allowed_access(
  899. emulator, block, access_key, MfClassicActionACWrite)) {
  900. memcpy(&block_data[6], &plain_data[6], 4);
  901. }
  902. } else {
  903. if(mf_classic_is_allowed_access(
  904. emulator, block, access_key, MfClassicActionDataWrite)) {
  905. memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
  906. }
  907. }
  908. if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE)) {
  909. memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
  910. emulator->data_changed = true;
  911. }
  912. // Send ACK
  913. ack = 0x0A;
  914. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  915. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  916. tx_rx->tx_bits = 4;
  917. } else {
  918. // Unknown command
  919. break;
  920. }
  921. }
  922. if(!command_processed) {
  923. // Send NACK
  924. uint8_t nack = 0x04;
  925. if(is_encrypted) {
  926. mf_crypto1_encrypt(
  927. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  928. } else {
  929. tx_rx->tx_data[0] = nack;
  930. }
  931. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  932. tx_rx->tx_bits = 4;
  933. furi_hal_nfc_tx_rx(tx_rx, 300);
  934. }
  935. return true;
  936. }