-//-----------------------------------------------------------------------------
-// Prepare tag messages
-//-----------------------------------------------------------------------------
-static void CodeIClassTagAnswer(const uint8_t *cmd, int len)
-{
-
- /*
- * SOF comprises 3 parts;
- * * An unmodulated time of 56.64 us
- * * 24 pulses of 423.75 KHz (fc/32)
- * * A logic 1, which starts with an unmodulated time of 18.88us
- * followed by 8 pulses of 423.75kHz (fc/32)
- *
- *
- * EOF comprises 3 parts:
- * - A logic 0 (which starts with 8 pulses of fc/32 followed by an unmodulated
- * time of 18.88us.
- * - 24 pulses of fc/32
- * - An unmodulated time of 56.64 us
- *
- *
- * A logic 0 starts with 8 pulses of fc/32
- * followed by an unmodulated time of 256/fc (~18,88us).
- *
- * A logic 0 starts with unmodulated time of 256/fc (~18,88us) followed by
- * 8 pulses of fc/32 (also 18.88us)
- *
- * The mode FPGA_HF_SIMULATOR_MODULATE_424K_8BIT which we use to simulate tag,
- * works like this.
- * - A 1-bit input to the FPGA becomes 8 pulses on 423.5kHz (fc/32) (18.88us).
- * - A 0-bit inptu to the FPGA becomes an unmodulated time of 18.88us
- *
- * In this mode the SOF can be written as 00011101 = 0x1D
- * The EOF can be written as 10111000 = 0xb8
- * A logic 1 is 01
- * A logic 0 is 10
- *
- * */
+ } else if (receivedCmd[0] == ICLASS_CMD_HALT && len == 1) {
+ if (chip_state == SELECTED) {
+ // Reader ends the session
+ modulated_response = resp_sof;
+ modulated_response_size = resp_sof_Len;
+ chip_state = HALTED;
+ }
+
+ } else if (simulationMode == ICLASS_SIM_MODE_FULL && receivedCmd[0] == ICLASS_CMD_READ4 && len == 4) { // 0x06
+ //Read 4 blocks
+ if (chip_state == SELECTED) {
+ uint8_t blockNo = receivedCmd[1];
+ memcpy(data_generic_trace, emulator + current_page*page_size + blockNo*8, 8 * 4);
+ AppendCrc(data_generic_trace, 8 * 4);
+ trace_data = data_generic_trace;
+ trace_data_size = 8 * 4 + 2;
+ CodeIso15693AsTag(trace_data, trace_data_size);
+ memcpy(data_response, ToSend, ToSendMax);
+ modulated_response = data_response;
+ modulated_response_size = ToSendMax;
+ }
+
+ } else if (receivedCmd[0] == ICLASS_CMD_UPDATE && (len == 12 || len == 14)) {
+ // We're expected to respond with the data+crc, exactly what's already in the receivedCmd
+ // receivedCmd is now UPDATE 1b | ADDRESS 1b | DATA 8b | Signature 4b or CRC 2b
+ if (chip_state == SELECTED) {
+ uint8_t blockNo = receivedCmd[1];
+ if (blockNo == 2) { // update e-purse
+ memcpy(card_challenge_data, receivedCmd+2, 8);
+ CodeIso15693AsTag(card_challenge_data, sizeof(card_challenge_data));
+ memcpy(resp_cc, ToSend, ToSendMax);
+ resp_cc_len = ToSendMax;
+ cipher_state_KD[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_d);
+ cipher_state_KC[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_c);
+ if (simulationMode == ICLASS_SIM_MODE_FULL) {
+ memcpy(emulator + current_page*page_size + 8*2, card_challenge_data, 8);
+ }
+ } else if (blockNo == 3) { // update Kd
+ for (int i = 0; i < 8; i++) {
+ if (personalization_mode) {
+ diversified_key_d[i] = receivedCmd[2 + i];
+ } else {
+ diversified_key_d[i] ^= receivedCmd[2 + i];
+ }
+ }
+ cipher_state_KD[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_d);
+ if (simulationMode == ICLASS_SIM_MODE_FULL) {
+ memcpy(emulator + current_page*page_size + 8*3, diversified_key_d, 8);
+ }
+ } else if (blockNo == 4) { // update Kc
+ for (int i = 0; i < 8; i++) {
+ if (personalization_mode) {
+ diversified_key_c[i] = receivedCmd[2 + i];
+ } else {
+ diversified_key_c[i] ^= receivedCmd[2 + i];
+ }
+ }
+ cipher_state_KC[current_page] = opt_doTagMAC_1(card_challenge_data, diversified_key_c);
+ if (simulationMode == ICLASS_SIM_MODE_FULL) {
+ memcpy(emulator + current_page*page_size + 8*4, diversified_key_c, 8);
+ }
+ } else if (simulationMode == ICLASS_SIM_MODE_FULL) { // update any other data block
+ memcpy(emulator + current_page*page_size + 8*blockNo, receivedCmd+2, 8);
+ }
+ memcpy(data_generic_trace, receivedCmd + 2, 8);
+ AppendCrc(data_generic_trace, 8);
+ trace_data = data_generic_trace;
+ trace_data_size = 10;
+ CodeIso15693AsTag(trace_data, trace_data_size);
+ memcpy(data_response, ToSend, ToSendMax);
+ modulated_response = data_response;
+ modulated_response_size = ToSendMax;
+ }
+
+ } else if (receivedCmd[0] == ICLASS_CMD_PAGESEL && len == 4) {
+ // Pagesel
+ // Chips with a single page will not answer to this command
+ // Otherwise, we should answer 8bytes (conf block 1) + 2bytes CRC
+ if (chip_state == SELECTED) {
+ if (simulationMode == ICLASS_SIM_MODE_FULL && max_page > 0) {
+ current_page = receivedCmd[1];
+ memcpy(data_generic_trace, emulator + current_page*page_size + 8*1, 8);
+ memcpy(diversified_key_d, emulator + current_page*page_size + 8*3, 8);
+ memcpy(diversified_key_c, emulator + current_page*page_size + 8*4, 8);
+ cipher_state = &cipher_state_KD[current_page];
+ personalization_mode = data_generic_trace[7] & 0x80;
+ AppendCrc(data_generic_trace, 8);
+ trace_data = data_generic_trace;
+ trace_data_size = 10;
+ CodeIso15693AsTag(trace_data, trace_data_size);
+ memcpy(data_response, ToSend, ToSendMax);
+ modulated_response = data_response;
+ modulated_response_size = ToSendMax;
+ }
+ }