mifare.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*-
  2. * Public platform independent Near Field Communication (NFC) library examples
  3. *
  4. * Copyright (C) 2009 Roel Verdult
  5. * Copyright (C) 2010 Romain Tartière
  6. * Copyright (C) 2010, 2011 Romuald Conty
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * 1) Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2 )Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Note that this license only applies on the examples, NFC library itself is under LGPL
  29. *
  30. */
  31. /**
  32. * @file mifare.h
  33. * @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc
  34. */
  35. #ifndef _LIBNFC_MIFARE_H_
  36. # define _LIBNFC_MIFARE_H_
  37. # include <nfc/nfc-types.h>
  38. // Compiler directive, set struct alignment to 1 uint8_t for compatibility
  39. # pragma pack(1)
  40. typedef enum {
  41. MC_AUTH_A = 0x60,
  42. MC_AUTH_B = 0x61,
  43. MC_READ = 0x30,
  44. MC_WRITE = 0xA0,
  45. MC_TRANSFER = 0xB0,
  46. MC_DECREMENT = 0xC0,
  47. MC_INCREMENT = 0xC1,
  48. MC_STORE = 0xC2
  49. } mifare_cmd;
  50. // MIFARE command params
  51. struct mifare_param_auth {
  52. uint8_t abtKey[6];
  53. uint8_t abtAuthUid[4];
  54. };
  55. struct mifare_param_data {
  56. uint8_t abtData[16];
  57. };
  58. struct mifare_param_value {
  59. uint8_t abtValue[4];
  60. };
  61. typedef union {
  62. struct mifare_param_auth mpa;
  63. struct mifare_param_data mpd;
  64. struct mifare_param_value mpv;
  65. } mifare_param;
  66. // Reset struct alignment to default
  67. # pragma pack()
  68. bool nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp);
  69. // Compiler directive, set struct alignment to 1 uint8_t for compatibility
  70. # pragma pack(1)
  71. // MIFARE Classic
  72. typedef struct {
  73. uint8_t abtUID[4];
  74. uint8_t btBCC;
  75. uint8_t btUnknown;
  76. uint8_t abtATQA[2];
  77. uint8_t abtUnknown[8];
  78. } mifare_classic_block_manufacturer;
  79. typedef struct {
  80. uint8_t abtData[16];
  81. } mifare_classic_block_data;
  82. typedef struct {
  83. uint8_t abtKeyA[6];
  84. uint8_t abtAccessBits[4];
  85. uint8_t abtKeyB[6];
  86. } mifare_classic_block_trailer;
  87. typedef union {
  88. mifare_classic_block_manufacturer mbm;
  89. mifare_classic_block_data mbd;
  90. mifare_classic_block_trailer mbt;
  91. } mifare_classic_block;
  92. typedef struct {
  93. mifare_classic_block amb[256];
  94. } mifare_classic_tag;
  95. // MIFARE Ultralight
  96. typedef struct {
  97. uint8_t sn0[3];
  98. uint8_t btBCC0;
  99. uint8_t sn1[4];
  100. uint8_t btBCC1;
  101. uint8_t internal;
  102. uint8_t lock[2];
  103. uint8_t otp[4];
  104. } mifareul_block_manufacturer;
  105. typedef struct {
  106. uint8_t abtData[16];
  107. } mifareul_block_data;
  108. typedef union {
  109. mifareul_block_manufacturer mbm;
  110. mifareul_block_data mbd;
  111. } mifareul_block;
  112. typedef struct {
  113. mifareul_block amb[4];
  114. } mifareul_tag;
  115. // Reset struct alignment to default
  116. # pragma pack()
  117. #endif // _LIBNFC_MIFARE_H_