+ } 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;
+ }