mifare.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*-
  2. * Public platform independent Near Field Communication (NFC) library examples
  3. *
  4. * Copyright (C) 2009, Roel Verdult
  5. * Copyright (C) 2010, Romuald Conty, Romain Tartière
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1) Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2 )Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * Note that this license only applies on the examples, NFC library itself is under LGPL
  28. *
  29. */
  30. #include "mifare.h"
  31. #include <string.h>
  32. #include <nfc/nfc.h>
  33. /**
  34. * @brief Execute a MIFARE Classic Command
  35. * @return Returns true if action was successfully performed; otherwise returns false.
  36. * @param pmp Some commands need additional information. This information should be supplied in the mifare_param union.
  37. *
  38. * The specified MIFARE command will be executed on the tag. There are different commands possible, they all require the destination block number.
  39. * @note There are three different types of information (Authenticate, Data and Value).
  40. *
  41. * First an authentication must take place using Key A or B. It requires a 48 bit Key (6 bytes) and the UID.
  42. * They are both used to initialize the internal cipher-state of the PN53X chip (http://libnfc.org/hardware/pn53x-chip).
  43. * After a successful authentication it will be possible to execute other commands (e.g. Read/Write).
  44. * The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
  45. */
  46. bool
  47. nfc_initiator_mifare_cmd (nfc_device * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
  48. {
  49. uint8_t abtRx[265];
  50. size_t szRx = sizeof(abtRx);
  51. size_t szParamLen;
  52. uint8_t abtCmd[265];
  53. //bool bEasyFraming;
  54. abtCmd[0] = mc; // The MIFARE Classic command
  55. abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
  56. switch (mc) {
  57. // Read and store command have no parameter
  58. case MC_READ:
  59. case MC_STORE:
  60. szParamLen = 0;
  61. break;
  62. // Authenticate command
  63. case MC_AUTH_A:
  64. case MC_AUTH_B:
  65. szParamLen = sizeof (mifare_param_auth);
  66. break;
  67. // Data command
  68. case MC_WRITE:
  69. szParamLen = sizeof (mifare_param_data);
  70. break;
  71. // Value command
  72. case MC_DECREMENT:
  73. case MC_INCREMENT:
  74. case MC_TRANSFER:
  75. szParamLen = sizeof (mifare_param_value);
  76. break;
  77. // Please fix your code, you never should reach this statement
  78. default:
  79. return false;
  80. break;
  81. }
  82. // When available, copy the parameter bytes
  83. if (szParamLen)
  84. memcpy (abtCmd + 2, (uint8_t *) pmp, szParamLen);
  85. // FIXME: Save and restore bEasyFraming
  86. // bEasyFraming = nfc_device_get_property_bool (pnd, NP_EASY_FRAMING, &bEasyFraming);
  87. if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true) < 0) {
  88. nfc_perror (pnd, "nfc_device_set_property_bool");
  89. return false;
  90. }
  91. // Fire the mifare command
  92. int res;
  93. if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, -1)) < 0) {
  94. if (res == NFC_ERFTRANS) {
  95. // "Invalid received frame", usual means we are
  96. // authenticated on a sector but the requested MIFARE cmd (read, write)
  97. // is not permitted by current acces bytes;
  98. // So there is nothing to do here.
  99. }
  100. else if(res == NFC_ECHIP) {
  101. // Since we implement a dictionary brute force attack,
  102. // don't print an error on failed authentications.
  103. }
  104. else {
  105. nfc_perror (pnd, "nfc_initiator_transceive_bytes");
  106. }
  107. // XXX nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming);
  108. return false;
  109. }
  110. /* XXX
  111. if (nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, bEasyFraming) < 0) {
  112. nfc_perror (pnd, "nfc_device_set_property_bool");
  113. return false;
  114. }
  115. */
  116. // When we have executed a read command, copy the received bytes into the param
  117. if (mc == MC_READ) {
  118. if (szRx == 16) {
  119. memcpy (pmp->mpd.abtData, abtRx, 16);
  120. } else {
  121. return false;
  122. }
  123. }
  124. // Command succesfully executed
  125. return true;
  126. }