mifare.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.c
  38. * @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc
  39. */
  40. #include "mifare.h"
  41. #include <string.h>
  42. #include <nfc/nfc.h>
  43. /**
  44. * @brief Execute a MIFARE Classic Command
  45. * @return Returns true if action was successfully performed; otherwise returns false.
  46. * @param pmp Some commands need additional information. This information should be supplied in the mifare_param union.
  47. *
  48. * The specified MIFARE command will be executed on the tag. There are different commands possible, they all require the destination block number.
  49. * @note There are three different types of information (Authenticate, Data and Value).
  50. *
  51. * First an authentication must take place using Key A or B. It requires a 48 bit Key (6 bytes) and the UID.
  52. * They are both used to initialize the internal cipher-state of the PN53X chip.
  53. * After a successful authentication it will be possible to execute other commands (e.g. Read/Write).
  54. * The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
  55. */
  56. bool
  57. nfc_initiator_mifare_cmd(nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp)
  58. {
  59. uint8_t abtRx[265];
  60. size_t szParamLen;
  61. uint8_t abtCmd[265];
  62. //bool bEasyFraming;
  63. abtCmd[0] = mc; // The MIFARE Classic command
  64. abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
  65. switch (mc) {
  66. // Read and store command have no parameter
  67. case MC_READ:
  68. case MC_STORE:
  69. szParamLen = 0;
  70. break;
  71. // Authenticate command
  72. case MC_AUTH_A:
  73. case MC_AUTH_B:
  74. szParamLen = sizeof(struct mifare_param_auth);
  75. break;
  76. // Data command
  77. case MC_WRITE:
  78. szParamLen = sizeof(struct mifare_param_data);
  79. break;
  80. // Value command
  81. case MC_DECREMENT:
  82. case MC_INCREMENT:
  83. case MC_TRANSFER:
  84. szParamLen = sizeof(struct mifare_param_value);
  85. break;
  86. // Please fix your code, you never should reach this statement
  87. default:
  88. return false;
  89. break;
  90. }
  91. // When available, copy the parameter bytes
  92. if (szParamLen)
  93. memcpy(abtCmd + 2, (uint8_t *) pmp, szParamLen);
  94. // FIXME: Save and restore bEasyFraming
  95. // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
  96. if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
  97. // nfc_perror(pnd, "nfc_device_set_property_bool");
  98. return false;
  99. }
  100. // Fire the mifare command
  101. int res;
  102. if ((res = nfc_initiator_transceive_bytes(pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) {
  103. if (res == NFC_ERFTRANS) {
  104. // "Invalid received frame", usual means we are
  105. // authenticated on a sector but the requested MIFARE cmd (read, write)
  106. // is not permitted by current acces bytes;
  107. // So there is nothing to do here.
  108. } else {
  109. // nfc_perror(pnd, "nfc_initiator_transceive_bytes");
  110. }
  111. // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
  112. return false;
  113. }
  114. /* XXX
  115. if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) {
  116. nfc_perror (pnd, "nfc_device_set_property_bool");
  117. return false;
  118. }
  119. */
  120. // When we have executed a read command, copy the received bytes into the param
  121. if (mc == MC_READ) {
  122. if (res == 16) {
  123. memcpy(pmp->mpd.abtData, abtRx, 16);
  124. } else {
  125. return false;
  126. }
  127. }
  128. // Command succesfully executed
  129. return true;
  130. }