Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot authenticate NTAG213

I am using an ACR122U NFC reader to password protect an NTAG213 NFC label. I think I have managed to set the password correctly, but I cannot authenticate and change the label afterward. My code for authenticating looks like this:

#include <winscard.h>
#include <iostream>
#pragma comment(lib, "winscard.lib")

const char *ReaderName = "ACS ACR122 0";
unsigned Password = 0x12345678;

int main()
{
  //Establish context
  SCARDCONTEXT              hContext;
  DWORD SCard_Status1 = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &hContext);
  if(SCard_Status1 != SCARD_S_SUCCESS)
    return 1;

  //connect to the card
  SCARDHANDLE   hCardHandle;
  DWORD Protocol;
  DWORD SCard_Status2 = SCardConnect(hContext, ReaderName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T1, &hCardHandle, &Protocol);
  if(SCard_Status2 != SCARD_S_SUCCESS)
  {
    SCardReleaseContext(hContext);
    return 1;
  }

  SCARD_IO_REQUEST Request;
  Request.dwProtocol = Protocol;
  Request.cbPciLength = sizeof(SCARD_IO_REQUEST);
  BYTE TxData[] =
  {
    0xFF, //CLA
    0x00, //INS
    0x00, //P1
    0x00, //P2
    0x08, //LC
    0xD4,
    0x40,
    0x01,
    0x1B, //PWD_AUTH (See data sheet)
    Password,
    Password >> 8,
    Password >> 16,
    Password >> 24,
  };

  BYTE RxData[254];
  unsigned long RxLength = sizeof(RxData);
  DWORD SCard_Status3 = SCardTransmit(hCardHandle, &Request, TxData, sizeof(TxData), NULL, RxData, &RxLength);
  std::cout << "SCard_Status = " << SCard_Status3 << std::endl;
  std::cout << "RxLength = " << RxLength << std::endl;

  SCardDisconnect(hCardHandle, SCARD_EJECT_CARD);
  SCardReleaseContext(hContext);
  return 0;
}

This should send the PWD_AUTH command to the NTAG213 with the Pwd. I expected to receive an error if the password is wrong or two bytes with the PACK if the password is correct. But SCard_Status is SCARD_S_SUCCESS and RxLength is 0 afterwards. And if I try to write to the label I get an error.

I am having trouble finding any examples showing how to do this. Can anyone see what I am doing wrong?

like image 385
Ivan Johansen Avatar asked Oct 16 '25 13:10

Ivan Johansen


1 Answers

I am going to answer this myself. I managed to make it work by changing TxData to this:

BYTE TxData[] =
{
  0xFF, //CLA
  0x00, //INS
  0x00, //P1
  0x00, //P2
  0x07, //LC
  0xD4, 0x42, //InCommunicateThru
  0x1B, //PWD_AUTH (See data sheet)
  Password,
  Password >> 8,
  Password >> 16,
  Password >> 24,
};

I found the command InCommunicateThru (D4 42) as a replacement for InDataExchange (D4 40) in the data sheet for the PN532, which is the NFC processor inside the ACR122U.

like image 146
Ivan Johansen Avatar answered Oct 19 '25 11:10

Ivan Johansen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!