mfoc.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #define MEM_CHUNK 10000
  2. #define TRY_KEYS 50
  3. // Number of trailers == number of sectors
  4. // Mifare Classic 1k 16x64b = 16
  5. #define NR_TRAILERS_1k (16)
  6. // Mifare Classic Mini
  7. #define NR_TRAILERS_MINI (5)
  8. // Mifare Classic 4k 32x64b + 8*256b = 40
  9. #define NR_TRAILERS_4k (40)
  10. // Mifare Classic 2k 32x64b
  11. #define NR_TRAILERS_2k (32)
  12. // Number of blocks
  13. // Mifare Classic 1k
  14. #define NR_BLOCKS_1k 0x3f
  15. // Mifare Classic Mini
  16. #define NR_BLOCKS_MINI 0x13
  17. // Mifare Classic 4k
  18. #define NR_BLOCKS_4k 0xff
  19. // Mifare Classic 2k
  20. #define NR_BLOCKS_2k 0x7f
  21. #define MAX_FRAME_LEN 264
  22. // Used for counting nonce distances, explore [nd-value, nd+value]
  23. #define DEFAULT_TOLERANCE 20
  24. // Default number of distance probes
  25. #define DEFAULT_DIST_NR 15
  26. // Default number of probes for a key recovery for one sector
  27. #define DEFAULT_PROBES_NR 150
  28. // Number of sets with 32b keys
  29. #define DEFAULT_SETS_NR 5
  30. #define odd_parity(i) (( (i) ^ (i)>>1 ^ (i)>>2 ^ (i)>>3 ^ (i)>>4 ^ (i)>>5 ^ (i)>>6 ^ (i)>>7 ^ 1) & 0x01)
  31. typedef struct {
  32. uint8_t KeyA[6];
  33. uint8_t KeyB[6];
  34. bool foundKeyA;
  35. bool foundKeyB;
  36. uint8_t trailer; // Value of a trailer block
  37. } sector;
  38. typedef struct {
  39. uint32_t *distances;
  40. uint32_t median;
  41. uint32_t num_distances;
  42. uint32_t tolerance;
  43. uint8_t parity[3]; // used for 3 bits of parity information
  44. } denonce; // Revealed information about nonce
  45. typedef struct {
  46. nfc_target nt;
  47. sector *sectors; // Allocate later, we do not know the number of sectors yet
  48. sector e_sector; // Exploit sector
  49. uint8_t num_sectors;
  50. uint8_t num_blocks;
  51. uint32_t authuid;
  52. } mftag;
  53. typedef struct {
  54. uint64_t *possibleKeys;
  55. uint32_t size;
  56. } pKeys;
  57. typedef struct {
  58. uint64_t *brokenKeys;
  59. uint32_t size;
  60. } bKeys;
  61. typedef struct {
  62. nfc_device *pdi;
  63. } mfreader;
  64. typedef struct {
  65. uint64_t key;
  66. int count;
  67. } countKeys;
  68. void mfoc_usage();
  69. void mf_init(mfreader *r);
  70. void mf_configure(nfc_device *pdi);
  71. void mf_select_tag(nfc_device *pdi, nfc_target *pnt);
  72. int trailer_block(uint32_t block);
  73. int find_exploit_sector(mftag t);
  74. void mf_anticollision(mftag t, mfreader r);
  75. bool get_rats_is_2k(mftag t, mfreader r);
  76. int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d, pKeys *pk, char mode, bool dumpKeysA);
  77. uint32_t median(denonce d);
  78. int compar_int(const void *a, const void *b);
  79. int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity);
  80. int compar_special_int(const void *a, const void *b);
  81. countKeys *uniqsort(uint64_t *possibleKeys, uint32_t size);
  82. void num_to_bytes(uint64_t n, uint32_t len, uint8_t *dest);
  83. long long unsigned int bytes_to_num(uint8_t *src, uint32_t len);