I would like to understand the manipulation of NfcV ISO-15639 - android

I'm new in NFC Technology!
I have difficulty understanding how to manipulate blocks.
byte[] cmd = new byte[] {
(byte)0x20, //FLAG
(byte)0x21, //WRITE SINGLE BLOCK COMMAND
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, //UID
(byte)0x00, //OFFSET
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 //DATA
};
My questions about code above:
What is a FLAG and what is its function?
What is a UID and what is its function? in code the UID line has 8 "index", is it possible to increase or decrease the size? instead of 8 as in code, decrease to 6 or increase to 10?
What is a OFFSET and what is its function?
In line 6 of the commented code as DATA, is this where I define the byte size of my block? In code, it has 4 indexes, does it mean that my data stored in the block will have 4 bytes? can i increase or decrease?
Lets go suppose, i have a 4 bytes data myData = "ABCD", and i want to write this data to block 04 of my tag, how would I do it according to the code mentioned above?

I'm not an NfcV expert but below is what I have learnt about Tags and low level access
What is the Flag byte mean? - Unknown BUT http://www.ti.com/lit/an/sloa141/sloa141.pdf Section 4.1 has details of ISO 15693 Flag meanings
But one of the Flag means using addressed or un-addressed mode, this leads on to the UID
What is UID bytes - Most tags have a serial number or Unique IDentifier number
In addressed mode you have to provide the correct UID of the card you are reading or writing from for it to succeed. This means you won't write or reading from the wrong card. There is a command to read the UID from the card first.
In un-addressed mode the UID is provided as zeros
You have worked out the the second byte is 0x21 for an write command.
0x20 for read command
http://www.ti.com/lit/an/sloa141/sloa141.pdf Section 4.2 has details of ISO 15693 Command values and as you can see must of them are Optional or Custom and support and what they do depends on the chip.
The OFFSET as you put it is the memory block offset from the first block or better described as the memory address (think of this like the page number in a book). Most chips split the memory in to blocks of a set size. There are some chips that use a single byte as the memory address and there are some that use 2 bytes.
Each block is a set number of bytes, it's common for it to be 4 bytes, but I've seen chip specs where it is 128 bytes.
The data structure you gave in your question is usually used as a template of a well formed command for chip you are trying to communicate to.
The DATA 4 bytes in your example are just place holders for the actual data your want to write, you should copy in to the template the actual 4 bytes you want to write before the send the command.
So when you are using it for write you have to adjust the OFFSET/Memory Address to be the "right page of the book" and copy in the right number of "letters" that can be written on the page in to the DATA part of the template
Some examples of NfcV code for Android from a chip manufacturer can be seen at the end of https://www.st.com/content/ccc/resource/technical/document/application_note/group0/76/0e/00/a0/1b/04/4c/f2/DM00103491/files/DM00103491.pdf/jcr:content/translations/en.DM00103491.pdf
So the last question Lets go suppose, i have a 4 bytes data myData = "ABCD", and i want to write this data to block 04 of my tag
example of constructing the command
// Command Template
byte[] cmd = new byte[] {
(byte)0x20, //FLAG
(byte)0x21, //WRITE SINGLE BLOCK COMMAND
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, //UID
(byte)0x00, //OFFSET
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 //DATA
};
// The data to be written
String myData = "ABCD";
// Get the data as bytes
byte[] data = myData.getBytes();
// Change the "OFFSET" / "Block number" to the the fourth Block
// If that what was meant by "block 04"
// The addresses start at Zero and the byte array starts at zero
// So the "Block Number" is the 11th byte in the command
cmd[10] = (byte)((3) & 0x0ff);
// Copy in 4 bytes of data in to bytes 11 to 15
// Starting at byte 0 in the data array
System.arraycopy(data, 0, cmd, 11, 4);
For Reference what the arraycopy Parameters https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)

Related

NFC - Help to exchange data between RC522 & Android HCE

I will explain my project :
I have my RC522 and a door connected on my Arduino UNO.
I can currently open the door with a MIFARE classic.
But now I want to open it with my Android smartphone, this is why I develop a HCE applet to accept the good APDU with the selected AID, then my phone will transfer the data in order to open the door.
But the problem is :
I don't know how to send an APDU command with my Arduino using the RC522.
Currently, for my MIFARE Cards, I use the https://github.com/miguelbalboa/rfid library.
My test code :
byte selectApdu[] = {
0x00, /* CLA */
0xA4, /* INS */
0x04, /* P1 */
0x00, /* P2 */
0x05, /* Length of AID */
0xF2, 0x22, 0x22, 0x22, 0x22,
};
byte * backData = (byte *)malloc(16*sizeof(byte));
byte * dataLen = (byte *)16;
status = mfrc522.PCD_TransceiveData(selectApdu,10,backData,dataLen,NULL,0,false);
if ( status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_TransceiveData() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
else
{
Serial.println(F("PICC_TransceiveData() success "));
}
Neverless, it doesn't work ( "Timeout in communication"), and I slowly need to think that the RC522 is not compatible...
It's an (well commented) open-source project. Just have a look to source code, for instance If you use MIFARE_Read function of MFRC522.cpp
MFRC522::StatusCode MFRC522::MIFARE_Read( byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from.
byte *buffer, ///< The buffer to store the data in
byte *bufferSize ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK.
) {
MFRC522::StatusCode result;
// Sanity check
if (buffer == NULL || *bufferSize < 18) {
return STATUS_NO_ROOM;
}
// Build command buffer
buffer[0] = PICC_CMD_MF_READ;
buffer[1] = blockAddr;
// Calculate CRC_A
result = PCD_CalculateCRC(buffer, 2, &buffer[2]);
if (result != STATUS_OK) {
return result;
}
// Transmit the buffer and receive the response, validate CRC_A.
return PCD_TransceiveData(buffer, 4, buffer, bufferSize, NULL, 0, true);
} // End MIFARE_Read()
You could see function PCD_TransceiveData is called and check source of this function:
/**
* Executes the Transceive command.
* CRC validation can only be done if backData and backLen are specified.
*
* #return STATUS_OK on success, STATUS_??? otherwise.
*/
MFRC522::StatusCode MFRC522::PCD_TransceiveData( byte *sendData, ///< Pointer to the data to transfer to the FIFO.
byte sendLen, ///< Number of bytes to transfer to the FIFO.
byte *backData, ///< NULL or pointer to buffer if data should be read back after executing the command.
byte *backLen, ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned.
byte *validBits, ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL.
byte rxAlign, ///< In: Defines the bit position in backData[0] for the first bit received. Default 0.
bool checkCRC ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
) {
byte waitIRq = 0x30; // RxIRq and IdleIRq
return PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, sendData, sendLen, backData, backLen, validBits, rxAlign, checkCRC);
} // End PCD_TransceiveData()
You could called PCD_TransceiveData or PCD_CommunicateWithPICC functions.
UPDATE
You put 0 values for parameters: "backData", "backLen", and "validBits".
validBits could be null.
backData and backLen must be defined as byte.
UPDATE2
If you have a look at library support protocols It supports ISO/IEC 14443-3 (type A) and not supports ISO/IEC 14443-4 (type B).
If you have a look at Android HCE documentation :
Specifically, Android 4.4 supports emulating cards that are based on
the NFC-Forum ISO-DEP specification (based on ISO/IEC 14443-4) and
process Application Protocol Data Units (APDUs) as defined in the
ISO/IEC 7816-4 specification. Android mandates emulating ISO-DEP only
on top of the Nfc-A (ISO/IEC 14443-3 Type A) technology. Support for
Nfc-B (ISO/IEC 14443-4 Type B) technology is optional. The layering of
all these specifications is shown in the figure 3
In this post: HCE support for ISO/IEC 14443-3 Type B?
Looking at devices in the field, some devices use Type A for HCE and
some seem to use Type B. So it's basically the device manufacturer who
decides if Type A or Type B is used. The Android API provides no means
for the app developer to influence this.
So you have to check with another Android NFC device if your device emulate ISO/IEC 14443-3 (Type A) or ISO/IEC 14443-4 (Type B). You could use NfcTagInfo application on other android device to check this.

Android NfcV get information command returns only one byte

I'm writing an app for reading binary infos of NFC tags. Here's the code of the NFC intent handler function:
protected void onNewIntent(#NonNull Intent intent)
{
try
{
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcV nfcV = NfcV.get(tag);
byte[] cmdInfo = new byte[]{(byte) 0x02, (byte) 0x2b}; // 'Get info' command
byte[] answer = nfcV.transceive(cmdInfo); // Read info from tag.
...
}
catch(IOException e)
{
...
}
}
If I run this function reading an NFC tag on a Samsung S3 Neo everything works fine, the answer variable is filled with the expected data:
00 04 4B A0 14 01 00 A0 07 E0 F3 07
Howewer if I run the same function reading the same NFC tag on a Huawei P8lite the answer variable is filled with only one byte:
03
In case of an error, the ISO 15693-3 standard says that I should receive something like
01 03
I.e. at least two bytes, where the second byte is the error code. So the answer I'm actually getting is theoretically not possible.
What am I doing wrong? Does it depend on the mobile hardware? And how can I fix it?
Since you are not using an addressed command (Address_flag not set, no UID field in request). You should not receive any resonse at all if an optional command is not supported by the tag (not even an error code). Hence you should neither receive 03 nor 01 03 according to the standard.
Note that NFC chipsets in Android devices often have limitations when it comes to ISO/IEC 15693 and do not support the complete standard. For instance, some chipsets are known to have issues with non-addressed commands. You could work around that by using the addressed version of the command:
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcV nfcV = NfcV.get(tag);
byte[] cmdInfo = new byte[]{
(byte)0x20,
(byte)0x2B,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
System.arraycopy(tag.getId(), 0, cmdInfo, 2, 8);
byte[] answer = nfcV.transceive(cmdInfo);
You might also want to test with different settings for the Data_rate_flag since the NFC chipset on your device might have problems with VICCs responding with at high data rate.

Access NFC tag memory over 2kbytes

I need to develop an Android app that is able to read an NfcV tag containing about 8 kByte of binary data. A single tag block is 8 bytes.
I wrote following code:
for (int i = 0; i < 256; ++i)
{
byte[] cmd = new byte[] {
0x02,
0x20,
(byte)i // Block number
};
byte[] block = nfcV.transceive(cmd);
for(int j = 0; j < 8; ++j) this.sensorData[i * 8 + j] = block[j + 1];
}
But this lets me only read the first 2 kByte of the tag (256 blocks of 8 bytes).
How can I read all 8 kByte?
Unfortunately I have few info about the Tag. I know that it's made by Texas Instruments, and Taginfo app says that it's compatible with
ISO/IEC 15693-3 and ISO/IEC 15693-2.
You are using the READ SINGLE BLOCK command (command code 0x20) to read from an ISO/IEC 15693 tag. The standard only defines the READ SINGLE BLOCK command for block addresses from 0 to 255. Hence, since your tag seems to have a block size of 8 bytes, this command limits you to the first 2 KB of the tag memory.
Reading beyond this address space is not defined by the ISO/IEC 15693 standard. Hence, this depends on the tag type that you use and you should therefore consult the user manual of your tag. Some tag manufacturers overcome this address-space limitation by defining a protocol extension (see Protocol_Extension_flag in the request flags byte of the command)
byte[] cmd = new byte[]{
(byte)0x08, //Protocol_Extension_flag=1
(byte)0x20, //READ SINGLE BLOCK
(byte)(address & 0x00FF), (byte)((address>>8) & 0x00FF)
};
However, this will only work if your tag supports this specific protocol extension. Depending on your tag type, it's also possible, that the tag uses some other method to address the remaining memory.
Also note that several Android devices will not properly work with unaddressed ISO/IEC 15693 commands. Thus, it's typically better to stick to the addressed version of commands. The addressed version of the above command (Addressed_flag set and UID of tag included in command) would like this:
byte tagId = nfcV.getTag().getId();
byte[] cmd = new byte[]{
(byte)0x28, //Addressed_flag=1, Protocol_Extension_flag=1
(byte)0x20, //READ SINGLE BLOCK
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, // placeholder for UID
(byte)(address & 0x00FF), (byte)((address>>8) & 0x00FF)
};
System.arraycopy(tagId, 0, cmd, 2, 8);
In both of the above cases, you could try variations of the Data_rate_flag (second lowest bit of the flags byte) and the Sub-carrier_flag (lowest bit of the flags byte), though I'm unsure how different Android devices will handle this.

Mifare Ultralight C Lock

I'm attempting to lock a Mifare Ultralight C tag. I want to set NDEF pages 0x04 to 0x27 to read-only. I think this can be achieved via Ndef.makeReadonly().
Additionally, I would like to set pages 0x29 to 0x2F to be password protected (for read and write) so they can only be accessed after authentication was successful. I'm struggling to understand which bytes need to set in lock bytes (page 0x28, bytes 0, 1) and if sectors 0x2A and 0x2B need to be set as well.
I'm attempting to set NDEF pages 0x04 to 0x27 to readonly. I think this can be achieved via Ndef.makeReadonly().
Not necessarily, Ndef.makeReadonly() might only set the read-only flag in the capability container (according to the NFC Forum Type 2 Tag Operation specification).
If you want to make sure to set the actual lock bits, you would connect the tag as NfcA or MifareUltralight tag technology and issue a write command for the lock bits instead.
NfcA nfcA = NfcA.get(tag);
nfcA.connect();
byte[] result1 = nfcA.transceive(new byte[] {
(byte)0xA2, /* CMD = WRITE */
(byte)0x02, /* PAGE = 2 */
(byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xFF /* DATA = lock pages 3..15 */
});
byte[] result2 = nfcA.transceive(new byte[] {
(byte)0xA2, /* CMD = WRITE */
(byte)0x28, /* PAGE = 40 */
(byte)0x0F, (byte)0x00, (byte)0x00, (byte)0x00 /* DATA = lock pages 16..27 */
});
Also see Mifare Ultralight: lock specific pages for the coding of the lockbits.
I would like to set pages 0x29 to 0x2F to be password protected (for read and write) so they can only be accessed after authentication was successful.
Using the write command that I showed above, you would first write your authentication key into pages 44..47. You would then write AUTH1 (page 43) as all-zeros. Finally, you would write AUTH0 (page 42) as 0x29 0x00 0x00 0x00 to require authentication for pages 41 and up. Actually I would suggest to lock pages 40 and up so that nobody could set the lock bits for those pages. Alternatively, you could set the block locking bits (i.e. write 0x1F 0x0F 0x00 0x00 to page 40) so that the lock bits for the unlocked pages cannot be changed.

Android : NFC APDU command response '6700' Wrong length'

I am sending below APDU command to ISODep tag and I am getting '6700 wrong length'
byte[] command = new byte[]{ (byte)0x80, (byte)0xD0, (byte)0x01, (byte)0x00, (byte)0x07,(byte)0x22 , (byte)0x22,(byte)0x12 , (byte)0x34 , (byte)0x56 , (byte)0x78 , (byte)0x90 };
Please suggest if something is wrong in above command.
You are sending a case 4 APDU there, right? Your Lc indicates 0x07 bytes of data, and there's a Le of 0x90 trailing.
Check with your specification whether this command is actually expecting a case 4 structure, or whether you just got the data length in the Lc field wrong (0x08 instead of 0x07).

Categories

Resources