mifare.h 4.0 KB

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