PN7150/NCI: Unable to update MIFARE Classic 1K Sector Trailer with Custom Embedded Key B Hello NXP Community, I am working with a MIFARE Classic 1K card and an NXP PN7150 NFC controller running over the standard NCI protocol layer. I am trying to implement a provisioning feature where I write a custom secret password into Key B of a target sector (Sector 7), but the card chip consistently rejects the write command. Here is the exact state of my card and the implementation details: Current Sector 7 State: The sector is currently blank/unformatted. A read dump shows it is in the factory default state with access bits FF078069 and both Key A and Key B set to 0xFFFFFFFFFFFF. The Goal: I want to keep Key A as the read key, preserve the open access bit layout, and overwrite Key B with a personal custom key payload (0x01 0x02 0x03 0x04 0x05 0x06). The Issue: When I attempt to write the 16-byte raw block layout string (FFFFFFFFFFFFFF078069010203040506) to Block 3 (Sector 7 Trailer), the write transaction fails. Note: i did the card NDEF format with an App and access bytes are showing 7F078840 Any guidance would be greatly appreciated! Kind Regards Re: PN7150/NCI: Unable to update MIFARE Classic 1K Sector Trailer with Custom Embedded Key B Hello @Saqib1
Hope you are doing well.
Please consider that PN7150 is Not Recommended for New Designs; we recommend using PN7160 instead. Additionally, MIFARE Classic is also Not Recommended for New Designs; MIFARE DESFire Light can be considered.
Before sending the write command make sure the authentication with Key A (FFFFFFFFFFFFh) on block 1Fh (Sector 7 trailer) is performed successfully.
Now, after authentication on block 1Fh, please verify that the write command is being sent following the procedure described in the MIFARE Classic EV1 1K datasheet, section 12.3. The WRITE operation consists of two parts that must be sent sequentially:
Part 1: Send the command byte and block address, including CRC: A0 + XX +CRC where XX is the block address (e.g., 1Fh for the Sector 7 Trailer). The card must respond with ACK before proceeding.
Part 2: Only after receiving the ACK, send the 16 bytes of data including CRC: [16 bytes payload] + CRC. The card will respond with a final ACK to confirm the write was accepted.
Regards, Eduardo. Re: PN7150/NCI: Unable to update MIFARE Classic 1K Sector Trailer with Custom Embedded Key B Hello Eduardo, I am waiting for new card to recieve. As soon as i receive, i implement your suggested solution and get to you back. One more question was about CRC calculation. How could we approach with CRC calculation? Best Regards Re: PN7150/NCI: Unable to update MIFARE Classic 1K Sector Trailer with Custom Embedded Key B Hello @EduardoZamora I have written the code .the thing is i want to format it as NDEF morever continue to write the private key by self as it is the key requirement.Can you please jave a look over program flow void RW_NDEF_MIFARE_Write_Next(uint8_t* pRsp, uint16_t Rsp_size, uint8_t* pCmd, uint16_t* pCmd_size) { *pCmd_size = 0; NDEF_MIFARE_t* TxT = &RfInterface.NDEF_MIFARE; NDef_MsgBuf_t* Msg = &RfInterface.Msg; switch(Msg->Wr_State) { case M_Wr_Init: { pCmd[0] = 0x40; pCmd[1] = 0x01; pCmd[2] = 0x10; memcpy(&pCmd[3], KeyA, 6); *pCmd_size = 9; Msg->Wr_State = M_Wr_Authenticated; } break; case M_Wr_Authenticated: { if (Rsp_size != 2) break; if (pRsp[Rsp_size-1] != 0x00) break; pCmd[0] = 0x10; pCmd[1] = 0x30; pCmd[2] = 0x01; *pCmd_size = 3; Msg->Wr_State = M_Wr_Read_GPB; } break; case M_Wr_Read_GPB: { if (Rsp_size != 18) break; if (pRsp[Rsp_size-1] != 0x00) break; if (pRsp[3] != MIFARE_NFC_CLUSTER) break; if (pRsp[4] != MIFARE_FUNCTION_CLUSTER) break; pCmd[0] = 0x40; pCmd[1] = 0x01; pCmd[2] = 0x01; *pCmd_size = 3; Msg->Wr_Index = 0; TxT->BlkNb = 3; Msg->Wr_State = M_Wr_Write_Data_1; } break; case M_Wr_Write_Data_1: { if (pRsp[Rsp_size-1] != 0x00) break; /* Is NDEF write already completed ? */ if (Msg->Wr_Size <= Msg->Wr_Index) { if ( TxT->BlkNb ==7) { pCmd[0] = 0x10; pCmd[1] = 0xA0; pCmd[2] = 0x07; *pCmd_size = 3; Msg->Wr_State = M_Wr_Write_Data_2; } else { if (nfc_Use_Callback) { if (NDEF_PushCb != NULL) { NDEF_PushCb(Msg->Wr, Msg->Wr_Size); } } } break; } if (( (TxT->BlkNb+1) % 4) == 0) { /* Authenticate next block */ pCmd[0] = 0x40; pCmd[1] = (TxT->BlkNb+1)/4; pCmd[2] = 0x01; *pCmd_size = 3; Msg->Wr_State = M_Wr_Write_Data_1; TxT->BlkNb++; } else { pCmd[0] = 0x10; pCmd[1] = 0xA0; pCmd[2] = TxT->BlkNb; *pCmd_size = 3; Msg->Wr_State = M_Wr_Write_Data_2; } } break; case M_Wr_Write_Data_2: { if (Rsp_size != 3) break; if (pRsp[Rsp_size-1] != 0x00) break; if(TxT->BlkNb == 7 && Msg->Wr_Size <= Msg->Wr_Index ) { pCmd[0] = 0x10; memcpy(&pCmd[1], KeyA,6); memcpy(&pCmd[7], accessBytes, 4); memcpy(&pCmd[11], KeyB,6); *pCmd_size = 17; TxT->BlkNb ++; Msg->Wr_State = M_Wr_Write_Data_1; break; } /* First block to write ? */ if (TxT->BlkNb == 4) { pCmd[0] = 0x10; //pCmd[1] = 0x00; //pCmd[2] = 0x00; memset(&pCmd[1], 0x00, 16); pCmd[3] = 0x03; if (Msg->Wr_Size > 0xFF) { pCmd[4] = 0xFF; pCmd[5] = (Msg->Wr_Size & 0xFF00) >> 8; pCmd[6] = Msg->Wr_Size & 0xFF; memcpy(&pCmd[7], Msg->Wr, 10); Msg->Wr_Index = 10; } else { pCmd[4] = Msg->Wr_Size; //memcpy(&pCmd[5], Msg->Wr, 12); //Msg->Wr_Index = 12; uint16_t CopySize = Msg->Wr_Size; if (CopySize > 12) { CopySize = 12; } memcpy(&pCmd[5], Msg->Wr, CopySize); Msg->Wr_Index = CopySize; } } else { //pCmd[0] = 0x10; //memcpy(&pCmd[1], Msg->Wr + Msg->Wr_Index, 16); //Msg->Wr_Index += 16; pCmd[0] = 0x10; uint16_t Remaining = Msg->Wr_Size - Msg->Wr_Index; uint16_t CopySize = (Remaining > 16) ? 16 : Remaining; memset(&pCmd[1], 0x00, 16); memcpy(&pCmd[1], Msg->Wr + Msg->Wr_Index, CopySize); Msg->Wr_Index += CopySize; } *pCmd_size = 17; TxT->BlkNb++; Msg->Wr_State = M_Wr_Write_Data_1; } break; default: break; } }
View full article