mifare_classic.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <furi_hal_nfc.h>
  3. #include "crypto1.h"
  4. #define MF_CLASSIC_BLOCK_SIZE (16)
  5. #define MF_CLASSIC_TOTAL_BLOCKS_MAX (256)
  6. #define MF_CLASSIC_1K_TOTAL_SECTORS_NUM (16)
  7. #define MF_CLASSIC_4K_TOTAL_SECTORS_NUM (40)
  8. #define MF_CLASSIC_SECTORS_MAX (40)
  9. #define MF_CLASSIC_BLOCKS_IN_SECTOR_MAX (16)
  10. #define MF_CLASSIC_NO_KEY (0xFFFFFFFFFFFFFFFF)
  11. #define MF_CLASSIC_MAX_DATA_SIZE (16)
  12. typedef enum {
  13. MfClassicType1k,
  14. MfClassicType4k,
  15. } MfClassicType;
  16. typedef enum {
  17. MfClassicKeyA,
  18. MfClassicKeyB,
  19. } MfClassicKey;
  20. typedef struct {
  21. uint8_t value[MF_CLASSIC_BLOCK_SIZE];
  22. } MfClassicBlock;
  23. typedef struct {
  24. uint8_t key_a[6];
  25. uint8_t access_bits[4];
  26. uint8_t key_b[6];
  27. } MfClassicSectorTrailer;
  28. typedef struct {
  29. uint8_t total_blocks;
  30. MfClassicBlock block[MF_CLASSIC_BLOCKS_IN_SECTOR_MAX];
  31. } MfClassicSector;
  32. typedef struct {
  33. MfClassicType type;
  34. uint64_t key_a_mask;
  35. uint64_t key_b_mask;
  36. MfClassicBlock block[MF_CLASSIC_TOTAL_BLOCKS_MAX];
  37. } MfClassicData;
  38. typedef struct {
  39. uint32_t cuid;
  40. uint8_t sector;
  41. uint64_t key_a;
  42. uint64_t key_b;
  43. } MfClassicAuthContext;
  44. typedef struct {
  45. uint8_t sector_num;
  46. uint64_t key_a;
  47. uint64_t key_b;
  48. } MfClassicSectorReader;
  49. typedef struct {
  50. MfClassicType type;
  51. uint32_t cuid;
  52. uint8_t sectors_to_read;
  53. Crypto1 crypto;
  54. MfClassicSectorReader sector_reader[MF_CLASSIC_SECTORS_MAX];
  55. } MfClassicReader;
  56. typedef struct {
  57. uint32_t cuid;
  58. Crypto1 crypto;
  59. MfClassicData data;
  60. bool data_changed;
  61. } MfClassicEmulator;
  62. bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
  63. bool mf_classic_get_type(
  64. uint8_t* uid,
  65. uint8_t uid_len,
  66. uint8_t ATQA0,
  67. uint8_t ATQA1,
  68. uint8_t SAK,
  69. MfClassicReader* reader);
  70. uint8_t mf_classic_get_total_sectors_num(MfClassicReader* reader);
  71. void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint32_t cuid, uint8_t sector);
  72. bool mf_classic_auth_attempt(
  73. FuriHalNfcTxRxContext* tx_rx,
  74. MfClassicAuthContext* auth_ctx,
  75. uint64_t key);
  76. void mf_classic_reader_add_sector(
  77. MfClassicReader* reader,
  78. uint8_t sector,
  79. uint64_t key_a,
  80. uint64_t key_b);
  81. bool mf_classic_read_sector(
  82. FuriHalNfcTxRxContext* tx_rx,
  83. Crypto1* crypto,
  84. MfClassicSectorReader* sector_reader,
  85. MfClassicSector* sector);
  86. uint8_t mf_classic_read_card(
  87. FuriHalNfcTxRxContext* tx_rx,
  88. MfClassicReader* reader,
  89. MfClassicData* data);
  90. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx);