mifare.c 5.2 KB

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