+ if (chip_state == SELECTED) {
+ if (simulationMode == ICLASS_SIM_MODE_FULL) {
+ //NR, from reader, is in receivedCmd+1
+ opt_doTagMAC_2(*cipher_state, receivedCmd+1, data_generic_trace, diversified_key);
+ trace_data = data_generic_trace;
+ trace_data_size = 4;
+ CodeIso15693AsTag(trace_data, trace_data_size);
+ memcpy(data_response, ToSend, ToSendMax);
+ modulated_response = data_response;
+ modulated_response_size = ToSendMax;
+ //exitLoop = true;
+ } else { // Not fullsim, we don't respond
+ // We do not know what to answer, so lets keep quiet
+ if (simulationMode == ICLASS_SIM_MODE_EXIT_AFTER_MAC) {
+ if (reader_mac_buf != NULL) {
+ // save NR and MAC for sim 2,4
+ memcpy(reader_mac_buf + 8, receivedCmd + 1, 8);
+ }
+ exitLoop = true;
+ }
+ }
+ }
+
+ } 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;