You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mfcuk_mifare.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Package:
  3. MiFare Classic Universal toolKit (MFCUK)
  4. Package version:
  5. 0.1
  6. Filename:
  7. mfcuk_mifare.h
  8. Description:
  9. MFCUK defines and function prototypes header file extending
  10. mainly libnfc's "mifare.h" interface/functionality.
  11. Contact, bug-reports:
  12. http://andreicostin.com/
  13. mailto:zveriu@gmail.com
  14. License:
  15. GPL2 (see below), Copyright (C) 2009, Andrei Costin
  16. * @file mfcuk_mifare.h
  17. * @brief
  18. */
  19. /*
  20. VERSION HISTORY
  21. --------------------------------------------------------------------------------
  22. | Number : 0.1
  23. | dd/mm/yyyy : 23/11/2009
  24. | Author : zveriu@gmail.com, http://andreicostin.com
  25. | Description: Moved bulk of defines and functions from "mfcuk_keyrecovery_darkside.c"
  26. --------------------------------------------------------------------------------
  27. */
  28. /*
  29. LICENSE
  30. This program is free software: you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation, either version 2 of the License, or
  33. (at your option) any later version.
  34. This program is distributed in the hope that it will be useful,
  35. but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. GNU General Public License for more details.
  38. You should have received a copy of the GNU General Public License
  39. along with this program. If not, see <http://www.gnu.org/licenses/>.
  40. */
  41. #ifndef _MFCUK_MIFARE_H_
  42. #define _MFCUK_MIFARE_H_
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <nfc/nfc.h>
  46. #include "mifare.h"
  47. #define MIFARE_CLASSIC_UID_BYTELENGTH 4 // Length of a Mifare Classic UID in bytes
  48. #define MIFARE_CLASSIC_KEY_BYTELENGTH 6 // Length of a Mifare Classic key in bytes
  49. #define MIFARE_CLASSIC_1K_NAME "MC1K"
  50. #define MIFARE_CLASSIC_4K_NAME "MC4K"
  51. #define MIFARE_CLASSIC_UNKN_NAME "UNKN"
  52. #define MIFARE_CLASSIC_1K 0x08 // MF1ICS50 Functional Specifications - 0x08
  53. #define MIFARE_CLASSIC_4K 0x18 // MF1ICS70 Functional Specifications - 0x18
  54. #define MIFARE_DESFIRE 0x20 // XXXXXXXX Functional Specifications - 0x20
  55. #define MIFARE_CLASSIC_1K_RATB 0x88 // Infineon Licensed Mifare 1K = 0x88 (thanks JPS)
  56. #define MIFARE_CLASSIC_4K_SKGT 0x98 // Infineon Licensed Mifare 4K = 0x98???
  57. #define IS_MIFARE_CLASSIC_1K(ats_sak) ( ((ats_sak) == MIFARE_CLASSIC_1K) || ((ats_sak) == MIFARE_CLASSIC_1K_RATB) )
  58. #define IS_MIFARE_CLASSIC_4K(ats_sak) ( ((ats_sak) == MIFARE_CLASSIC_4K) || ((ats_sak) == MIFARE_CLASSIC_4K_SKGT) )
  59. #define IS_MIFARE_DESFIRE(ats_sak) ( ((ats_sak) == MIFARE_DESFIRE) )
  60. #define IS_MIFARE_CLASSIC_1K_TAG(tag) IS_MIFARE_CLASSIC_1K(tag->amb[0].mbm.btUnknown)
  61. #define IS_MIFARE_CLASSIC_4K_TAG(tag) IS_MIFARE_CLASSIC_4K(tag->amb[0].mbm.btUnknown)
  62. #define IS_MIFARE_DESFIRE_TAG(tag) IS_MIFARE_DESFIRE(tag->amb[0].mbm.btUnknown)
  63. #define MIFARE_CLASSIC_BYTES_PER_BLOCK 16 // Common for Mifare Classic 1K and Mifare Classic 4K
  64. #define MIFARE_CLASSIC_INVALID_BLOCK 0xFFFFFFFF
  65. #define MIFARE_CLASSIC_1K_MAX_SECTORS 16
  66. #define MIFARE_CLASSIC_1K_BLOCKS_PER_SECTOR 4
  67. #define MIFARE_CLASSIC_1K_MAX_BLOCKS ( (MIFARE_CLASSIC_1K_MAX_SECTORS) * (MIFARE_CLASSIC_1K_BLOCKS_PER_SECTOR) )
  68. #define MIFARE_CLASSIC_4K_MAX_SECTORS1 32
  69. #define MIFARE_CLASSIC_4K_BLOCKS_PER_SECTOR1 MIFARE_CLASSIC_1K_BLOCKS_PER_SECTOR // Possibly NXP made it for Mifare 1K backward compatibility
  70. #define MIFARE_CLASSIC_4K_MAX_BLOCKS1 ( (MIFARE_CLASSIC_4K_MAX_SECTORS1) * (MIFARE_CLASSIC_4K_BLOCKS_PER_SECTOR1) )
  71. #define MIFARE_CLASSIC_4K_MAX_SECTORS2 8
  72. #define MIFARE_CLASSIC_4K_BLOCKS_PER_SECTOR2 16
  73. #define MIFARE_CLASSIC_4K_MAX_BLOCKS2 ( (MIFARE_CLASSIC_4K_MAX_SECTORS2) * (MIFARE_CLASSIC_4K_BLOCKS_PER_SECTOR2) )
  74. #define MIFARE_CLASSIC_4K_MAX_SECTORS ( (MIFARE_CLASSIC_4K_MAX_SECTORS1) + (MIFARE_CLASSIC_4K_MAX_SECTORS2) )
  75. #define MIFARE_CLASSIC_4K_MAX_BLOCKS ( (MIFARE_CLASSIC_4K_MAX_BLOCKS1) + (MIFARE_CLASSIC_4K_MAX_BLOCKS2) )
  76. #define MFCUK_EXTENDED_DESCRIPTION_LENGTH 128
  77. // Define an extended type of dump, basically a wrapper dump around basic tag dump
  78. typedef struct {
  79. uint32_t uid; // looks redundant, but it is easier to use dmp.uid instead of dmp.amb.mbm.abtUID[0]...[3]
  80. uint8_t type; // ATS/SAK from ti.tia.btSak, example 0x08h for Mifare 1K, 0x18h for Mifare 4K
  81. char datetime[14]; // non-zero-terminated date-time of dump in format YYYYMMDDH24MISS, example 20091114231541 - 14 Nov 2009, 11:15:41 PM
  82. char description[MFCUK_EXTENDED_DESCRIPTION_LENGTH]; // a description of the tag dump, example "RATB_DUMP_BEFORE_PAY"
  83. mifare_classic_tag tag_basic;
  84. } mifare_classic_tag_ext;
  85. // Define type of keys (A or B) in NXP notation
  86. typedef enum {
  87. keyA = 0x60,
  88. keyB = 0x61,
  89. } mifare_key_type;
  90. // Default keys used as a *BIG* mistake in many applications - especially System Integrators should pay attention!
  91. extern uint8_t mfcuk_default_keys[][MIFARE_CLASSIC_KEY_BYTELENGTH];
  92. extern int mfcuk_default_keys_num;
  93. bool is_valid_block(uint8_t bTagType, uint32_t uiBlock);
  94. bool is_valid_sector(uint8_t bTagType, uint32_t uiSector);
  95. bool is_first_block(uint8_t bTagType, uint32_t uiBlock);
  96. bool is_trailer_block(uint8_t bTagType, uint32_t uiBlock);
  97. uint32_t get_first_block(uint8_t bTagType, uint32_t uiBlock);
  98. uint32_t get_trailer_block(uint8_t bTagType, uint32_t uiBlock);
  99. bool is_big_sector(uint8_t bTagType, uint32_t uiSector);
  100. uint32_t get_first_block_for_sector(uint8_t bTagType, uint32_t uiSector);
  101. uint32_t get_trailer_block_for_sector(uint8_t bTagType, uint32_t uiSector);
  102. uint32_t get_sector_for_block(uint8_t bTagType, uint32_t uiBlock);
  103. bool is_first_sector(uint8_t bTagType, uint32_t uiSector);
  104. bool is_first_big_sector(uint8_t bTagType, uint32_t uiSector);
  105. bool is_first_small_sector(uint8_t bTagType, uint32_t uiSector);
  106. bool is_last_sector(uint8_t bTagType, uint32_t uiSector);
  107. bool is_last_big_sector(uint8_t bTagType, uint32_t uiSector);
  108. bool is_last_small_sector(uint8_t bTagType, uint32_t uiSector);
  109. void test_mifare_classic_blocks_sectors_functions(uint8_t bTagType);
  110. bool mfcuk_save_tag_dump(const char *filename, mifare_classic_tag *tag);
  111. bool mfcuk_save_tag_dump_ext(const char *filename, mifare_classic_tag_ext *tag_ext);
  112. bool mfcuk_load_tag_dump(const char *filename, mifare_classic_tag *tag);
  113. bool mfcuk_load_tag_dump_ext(const char *filename, mifare_classic_tag_ext *tag_ext);
  114. void print_mifare_classic_tag_keys(const char *title, mifare_classic_tag *tag);
  115. bool mfcuk_key_uint64_to_arr(const uint64_t *ui64Key, uint8_t *arr6Key);
  116. bool mfcuk_key_arr_to_uint64(const uint8_t *arr6Key, uint64_t *ui64Key);
  117. #endif // _MFCUK_MIFARE_H_