0

Trying to communicate with my Android app. My app sends a 32 byte md5 hash back to the Arduino. However, when I receive the response apdu, the last 9 bytes are corrupted.

Here's the relevant part of my Arduino sketch:

        uint8_t response[32];

        uint8_t responseLength = sizeof(response);

        if (nfc.inDataExchange(message, sizeof(message), response, &responseLength)) {

            Serial.print("TYPECASTED RAW: ");
            for (int i = 0; i < sizeof(response); i++) {
                Serial.print((char)response[i]);
            }

            Serial.println(" ");

This outputs:

TYPECASTED RAW: e68d3f574009cbbe0111502ÿÿÿÿÿÿÿÿÿ

As you can see the last 9 bytes are wrong. Now, I turned on the debug mode in the Adafruit NFC I2C library and it outputs 'Status code indicates an error' when I send the apdu back.

Here's the relevant part of the NFC library that throws the status code:

if (pn532_packetbuffer[5]==PN532_PN532TOHOST && pn532_packetbuffer[6]==PN532_RESPONSE_INDATAEXCHANGE) {
      if ((pn532_packetbuffer[7] & 0x3f)!=0) {
        #ifdef PN532DEBUG
          Serial.println("Status code indicates an error");
        #endif
        return false;
      }

Anyone has any ideas as to why my last 9 bytes are corrupted?

Thanks in advance.

ReX357
  • 1,199
  • 3
  • 19
  • 43

2 Answers2

0

Your using wrong size. sizeof(response) is declared size of variable response, 32 bytes.

inDataExchange uses responseLength to return filled bytes, so not all 32 are used.

for (int i = 0; i < responseLength; i++) {
    Serial.print((char)response[i]);
}

will print just used bytes.

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
0

Here's how I fixed my problem.

First I built the APDU properly on the Android side. My original response APDU was simply a byte array of the hash without the required SW1 and SW2 bytes.

Then on the Arduino side of things, here's how I got the hash back out of it:

        uint8_t response[64];

        uint8_t responseLength = sizeof(response);

        if (nfc.inDataExchange(message, sizeof(message), response, &responseLength)) {

            String respBuffer;

            for (int i = 0; i < 16; i++) {
                if (response[i] < 0x10) respBuffer = respBuffer + "0";
                respBuffer = respBuffer + String(response[i], HEX);                        
            }

            Serial.println(respBuffer);

        }

Hope this helps someone.

ReX357
  • 1,199
  • 3
  • 19
  • 43