minimal speedups for mifare_classic.c

included missing nfcv.c API update
This commit is contained in:
g3gg0
2022-11-23 02:16:52 +01:00
parent 021695b2a3
commit 9d07f8db29
4 changed files with 34 additions and 13 deletions
+12 -3
View File
@@ -36,7 +36,7 @@ uint32_t crypto1_filter(uint32_t in) {
return FURI_BIT(0xEC57E80A, out);
}
uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted) {
static inline uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted) {
furi_assert(crypto1);
uint8_t out = crypto1_filter(crypto1->odd);
uint32_t feed = out & (!!is_encrypted);
@@ -58,6 +58,15 @@ uint8_t crypto1_byte(Crypto1* crypto1, uint8_t in, int is_encrypted) {
return out;
}
static inline uint8_t crypto1_byte_inline(Crypto1* crypto1, uint8_t in, int is_encrypted) {
furi_assert(crypto1);
uint8_t out = 0;
for(uint8_t i = 0; i < 8; i++) {
out |= crypto1_bit(crypto1, FURI_BIT(in, i), is_encrypted) << i;
}
return out;
}
uint32_t crypto1_word(Crypto1* crypto1, uint32_t in, int is_encrypted) {
furi_assert(crypto1);
uint32_t out = 0;
@@ -92,7 +101,7 @@ void crypto1_decrypt(
decrypted_data[0] = decrypted_byte;
} else {
for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
decrypted_data[i] = crypto1_byte_inline(crypto, 0, 0) ^ encrypted_data[i];
}
}
}
@@ -117,7 +126,7 @@ void crypto1_encrypt(
} else {
memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
encrypted_data[i] = crypto1_byte_inline(crypto, keystream ? keystream[i] : 0, 0) ^
plain_data[i];
encrypted_parity[i / 8] |=
(((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
+1 -1
View File
@@ -12,7 +12,7 @@ void crypto1_reset(Crypto1* crypto1);
void crypto1_init(Crypto1* crypto1, uint64_t key);
uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted);
//uint8_t crypto1_bit(Crypto1* crypto1, uint8_t in, int is_encrypted);
uint8_t crypto1_byte(Crypto1* crypto1, uint8_t in, int is_encrypted);
+19 -7
View File
@@ -769,19 +769,28 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
crypto1_reset(&emulator->crypto);
memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
} else {
tx_rx->rx_bits = 0;
if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
FURI_LOG_D(
TAG,
"Error in tx rx. Tx :%d bits, Rx: %d bits",
"Error in tx rx. Tx :%d bits, Rx: %d bits. Received:",
tx_rx->tx_bits,
tx_rx->rx_bits);
FURI_LOG_D(TAG,"Sent:");
for(int pos = 0; pos < tx_rx->tx_bits/8; pos++) {
FURI_LOG_D(TAG," %02X", tx_rx->tx_data[pos]);
}
FURI_LOG_D(TAG,"Received:");
for(int pos = 0; pos < tx_rx->rx_bits/8; pos++) {
FURI_LOG_D(TAG," %02X", tx_rx->rx_data[pos]);
}
break;
}
crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
}
if(plain_data[0] == 0x50 && plain_data[1] == 0x00) {
FURI_LOG_T(TAG, "Halt received");
furi_hal_nfc_listen_sleep();
command_processed = true;
break;
@@ -799,7 +808,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
access_key = MfClassicKeyB;
}
uint32_t nonce = prng_successor(DWT->CYCCNT, 32) ^ 0xAA;
uint32_t nonce = prng_successor(DWT->CYCCNT, 2) ^ 0xAA;
uint8_t nt[4];
uint8_t nt_keystream[4];
nfc_util_num2bytes(nonce, 4, nt);
@@ -807,7 +816,9 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
crypto1_init(&emulator->crypto, key);
if(!is_encrypted) {
crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
memcpy(tx_rx->tx_data, nt, sizeof(nt));
for(size_t pos = 0; pos < sizeof(nt); pos++) {
tx_rx->tx_data[pos] = nt[pos];
}
tx_rx->tx_parity[0] = 0;
for(size_t i = 0; i < sizeof(nt); i++) {
tx_rx->tx_parity[0] |= nfc_util_odd_parity8(nt[i]) << (7 - i);
@@ -826,7 +837,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
}
if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
FURI_LOG_E(TAG, "Error in NT exchange");
FURI_LOG_E(TAG, "Error in NT exchange?");
command_processed = true;
break;
}
@@ -839,7 +850,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
/*
FURI_LOG_D(
TAG,
"%08lx key%c block %d nt/nr/ar: %08lx %08lx %08lx",
@@ -849,7 +860,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
nonce,
nr,
ar);
*/
crypto1_word(&emulator->crypto, nr, 1);
uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
if(cardRr != prng_successor(nonce, 64)) {
@@ -964,6 +975,7 @@ bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_
tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
tx_rx->tx_bits = 4;
} else {
FURI_LOG_T(TAG, "%02X unknown received", plain_data[0]);
// Unknown command
break;
}
+2 -2
View File
@@ -215,7 +215,7 @@ void nfcv_emu_alloc() {
if(!nfcv_signal) {
/* assuming max frame length is 255 bytes */
nfcv_signal = digital_sequence_alloc(8 * 255 + 2);
nfcv_signal = digital_sequence_alloc(8 * 255 + 2, nfcv_out_io);
}
if(!nfcv_resp_unmod_256) {
@@ -297,7 +297,7 @@ void nfcv_emu_send_raw(uint8_t* data, uint8_t length) {
digital_sequence_add(nfcv_signal, SIG_EOF);
FURI_CRITICAL_ENTER();
digital_sequence_send(nfcv_signal, nfcv_out_io);
digital_sequence_send(nfcv_signal);
FURI_CRITICAL_EXIT();
furi_hal_gpio_write(nfcv_out_io, false);
}