tag.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef TAG__H
  2. #define TAG__H
  3. /**
  4. * Copyright (C) 2011 Anders Sundman <anders@4zm.org>
  5. *
  6. * This file is part of mfterm.
  7. *
  8. * mfterm is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * mfterm is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with mfterm. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Parts of code used in this file are from the GNU readline library file
  22. * fileman.c (GPLv3). Copyright (C) 1987-2009 Free Software Foundation, Inc
  23. */
  24. #include "mifare.h"
  25. typedef enum {
  26. MF_INVALID_SIZE = 0,
  27. MF_1K = 1024,
  28. MF_4K = 4096
  29. } mf_size_t;
  30. typedef enum {
  31. MF_INVALID_KEY_TYPE = 0,
  32. MF_KEY_A = 'a',
  33. MF_KEY_B = 'b',
  34. MF_KEY_UNLOCKED = 0xff,
  35. } mf_key_type_t;
  36. // Convenience typedefs (shortening)
  37. typedef mifare_classic_tag mf_tag_t;
  38. typedef mifare_classic_block mf_block_t;
  39. // The active tag
  40. extern mf_tag_t current_tag;
  41. // The ACL + keys used
  42. extern mf_tag_t current_auth;
  43. // Load/Save tag or keys from file
  44. int load_tag(const char* fn);
  45. int load_auth(const char* fn);
  46. int save_tag(const char* fn);
  47. int save_auth(const char* fn);
  48. // Copy key data from the 'current_tag' to the 'current_auth'
  49. int import_auth();
  50. // Output tag data
  51. void print_tag();
  52. void print_tag_block_range(size_t first, size_t last);
  53. void print_tag_data_range(size_t byte_offset, size_t bit_offset,
  54. size_t byte_len, size_t bit_len);
  55. void print_tag_bytes(size_t first_byte, size_t last_byte);
  56. void print_keys(const mf_tag_t* tag, mf_size_t size);
  57. void print_ac(const mf_tag_t* tag);
  58. // Return a hex string representationon of the key
  59. const char* sprint_key(const uint8_t* key);
  60. // Parse the string and set the key. Return the key, or NULL on error.
  61. uint8_t* read_key(uint8_t* key, const char* str);
  62. // Return a string describing the tag type 1k|4k
  63. const char* sprint_size(mf_size_t size);
  64. // Set the contents of a tag to zeroes
  65. void clear_tag(mf_tag_t* tag);
  66. // Return number of blocks for size
  67. size_t block_count(mf_size_t size);
  68. // Return number of sectors for size
  69. size_t sector_count(mf_size_t size);
  70. // Return > 0 if the block is a trailer, 0 otherwise.
  71. int is_trailer_block(size_t block);
  72. // Return the sector index of the block
  73. size_t block_to_sector(size_t block);
  74. // Return the head block for the specified block
  75. size_t block_to_header(size_t block);
  76. // Return the trailer block for the specified block
  77. size_t block_to_trailer(size_t block);
  78. // Return the trailer block for the specified sector
  79. size_t sector_to_trailer(size_t sector);
  80. // Return the sector size (in blocks) that contains the block
  81. size_t sector_size(size_t block);
  82. // Extract the key for the block parameters sector of the tag and return it
  83. uint8_t* key_from_tag(const mf_tag_t* tag,
  84. mf_key_type_t key_type,
  85. size_t block);
  86. // Write key to the sector of a tag, where the sector is specified by
  87. // the block (anywhere in the sector).
  88. void key_to_tag(mf_tag_t* tag, const uint8_t* key,
  89. mf_key_type_t key_type, size_t block);
  90. /**
  91. * Return block index of the first block in every sector in turn on
  92. * repeated calls. Initialize the iterator by calling with state
  93. * 0. Subsequent calls should use the tag size as state. The iterator
  94. * returns -1 as an end marker.
  95. */
  96. int sector_header_iterator(int state);
  97. #endif