This is my read function:
dev_info* m_nfcAdaptor;
tag_info m_info;
bool pkNfcTagHandle::readMifareClassic()
{
mifare_param mp;
int size = (this->type() == pkNfcMifareClassic1k) ? 0x3f : 0xff;
if (m_nfcAdaptor)
{
// Read the card from begin to end.
for (int block = 0; block <= size; block++)
{
// Authenticate everytime we reach a trailer block.
if (is_trailer_block(block))
{
// Set the authentication information (uid).
memcpy(mp.mpa.abtUid, m_info.tia.abtUid, 4);
// Try to authenticate for the current sector, first with the B key
// then with the A one.
if (!mifare_classic_autenthicate(m_nfcAdaptor, mp, block))
this->setErrorString(tr("Failed to authenticate for block %1 of a MIFARE Classic tag.").arg(block));
continue;
}
// Try to read out the data block.
if (!nfc_initiator_mifare_cmd(m_nfcAdaptor, MC_READ, block, &mp))
{
if (!nfc_initiator_select_tag(m_nfcAdaptor, IM_ISO14443A_106, NULL, 0, &m_info))
{
this->setErrorString(tr("MIFARE Classic tag (UID %1) removed.").arg((char*)m_info.tia.abtUid));
return false;
}
if (!nfc_initiator_mifare_cmd(m_nfcAdaptor, MC_READ, block, &mp))
{
this->setErrorString(tr("Failed to read the block %1 from the MIFARE Classic tag.").arg(block));
return false;
}
}
m_buffer.append((const char*)mp.mpd.abtData, 16);
}
return true;
}
return false;
}
Which uses this authentication function:
bool mifare_classic_autenthicate(dev_info* device, mifare_param mp, uint32_t block)
{
byte_t keys[] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,
0x4d,0x3a,0x99,0xc3,0x51,0xdd,
0x1a,0x98,0x2c,0x7e,0x45,0x9a,
0x00,0x00,0x00,0x00,0x00,0x00,
0xd3,0xf7,0xd3,0xf7,0xd3,0xf7,
0xaa,0xbb,0xcc,0xdd,0xee,0xff
};
int num_keys = sizeof(keys) / 6;
for (int i = 0; i < num_keys; i++)
{
memcpy(mp.mpa.abtKey, keys + (i * 6), 6);
if (nfc_initiator_mifare_cmd(device, MC_AUTH_A, block, &mp))
return true;
if (nfc_initiator_mifare_cmd(device, MC_AUTH_B, block, &mp))
return true;
}
return false;
}
The authentication returns true but the next read command fails and I don't know why.
Last edited by zuck (2009-09-26 13:31:21)