diff --git a/lib/nfc/helpers/crypto1.c b/lib/nfc/helpers/crypto1.c index 97b92edfd..a228e3f09 100644 --- a/lib/nfc/helpers/crypto1.c +++ b/lib/nfc/helpers/crypto1.c @@ -207,16 +207,11 @@ uint32_t lfsr_rollback_word(Crypto1* crypto1, uint32_t in, int fb) { } // Return true if the nonce is invalid else return false -bool valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t parity) { - return ((nfc_util_odd_parity8((Nt >> 24) & 0xFF) == - (((parity >> 3) & 1) ^ nfc_util_odd_parity8((NtEnc >> 24) & 0xFF) ^ - FURI_BIT(Ks1, 16))) && - (nfc_util_odd_parity8((Nt >> 16) & 0xFF) == - (((parity >> 2) & 1) ^ nfc_util_odd_parity8((NtEnc >> 16) & 0xFF) ^ - FURI_BIT(Ks1, 8))) && - (nfc_util_odd_parity8((Nt >> 8) & 0xFF) == - (((parity >> 1) & 1) ^ nfc_util_odd_parity8((NtEnc >> 8) & 0xFF) ^ - FURI_BIT(Ks1, 0)))) ? - true : - false; +bool valid_nonce(uint32_t nt, uint32_t ks, uint8_t nt_par_enc) { + return (nfc_util_even_parity8((nt >> 24) & 0xFF) == + (((nt_par_enc >> 3) & 1) ^ FURI_BIT(ks, 16))) && + (nfc_util_even_parity8((nt >> 16) & 0xFF) == + (((nt_par_enc >> 2) & 1) ^ FURI_BIT(ks, 8))) && + (nfc_util_even_parity8((nt >> 8) & 0xFF) == + (((nt_par_enc >> 1) & 1) ^ FURI_BIT(ks, 0))); } diff --git a/lib/nfc/helpers/crypto1.h b/lib/nfc/helpers/crypto1.h index ed71bc3e1..ef9bd69b5 100644 --- a/lib/nfc/helpers/crypto1.h +++ b/lib/nfc/helpers/crypto1.h @@ -40,7 +40,7 @@ void crypto1_encrypt_reader_nonce( uint32_t lfsr_rollback_word(Crypto1* crypto1, uint32_t in, int fb); -bool valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t parity); +bool valid_nonce(uint32_t nt, uint32_t ks, uint8_t nt_par_enc); uint32_t prng_successor(uint32_t x, uint32_t n); diff --git a/lib/nfc/helpers/nfc_util.c b/lib/nfc/helpers/nfc_util.c index f502b4bfb..80af5cf11 100644 --- a/lib/nfc/helpers/nfc_util.c +++ b/lib/nfc/helpers/nfc_util.c @@ -13,6 +13,10 @@ static const uint8_t nfc_util_odd_byte_parity[256] = { 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1}; +uint8_t nfc_util_even_parity8(uint8_t data) { + return !nfc_util_odd_byte_parity[data]; +} + uint8_t nfc_util_even_parity32(uint32_t data) { // data ^= data >> 16; // data ^= data >> 8; diff --git a/lib/nfc/helpers/nfc_util.h b/lib/nfc/helpers/nfc_util.h index f8e86d865..4abde4521 100644 --- a/lib/nfc/helpers/nfc_util.h +++ b/lib/nfc/helpers/nfc_util.h @@ -6,6 +6,8 @@ extern "C" { #endif +uint8_t nfc_util_even_parity8(uint8_t data); + uint8_t nfc_util_even_parity32(uint32_t data); uint8_t nfc_util_odd_parity8(uint8_t data); diff --git a/lib/nfc/protocols/mf_classic/mf_classic_poller.c b/lib/nfc/protocols/mf_classic/mf_classic_poller.c index d6d142875..b35f6d2df 100644 --- a/lib/nfc/protocols/mf_classic/mf_classic_poller.c +++ b/lib/nfc/protocols/mf_classic/mf_classic_poller.c @@ -1250,8 +1250,7 @@ NfcCommand mf_classic_poller_handler_nested_collect_nt_enc(MfClassicPoller* inst ((dict_attack_ctx->d_median + current_dist) != UINT16_MAX)) { uint32_t nth_successor_positive = prng_successor(decrypted_nt_prev, dict_attack_ctx->d_median + current_dist); - if(valid_nonce( - nth_successor_positive, nt_enc, nth_successor_positive ^ nt_enc, parity)) { + if(valid_nonce(nth_successor_positive, nth_successor_positive ^ nt_enc, parity)) { found_matching_nt = true; found_nt = nth_successor_positive; break; @@ -1259,8 +1258,7 @@ NfcCommand mf_classic_poller_handler_nested_collect_nt_enc(MfClassicPoller* inst if(current_dist > 0) { uint32_t nth_successor_negative = prng_successor(decrypted_nt_prev, dict_attack_ctx->d_median - current_dist); - if(valid_nonce( - nth_successor_negative, nt_enc, nth_successor_negative ^ nt_enc, parity)) { + if(valid_nonce(nth_successor_negative, nth_successor_negative ^ nt_enc, parity)) { found_matching_nt = true; found_nt = nth_successor_negative; break; diff --git a/targets/f7/api_symbols.csv b/targets/f7/api_symbols.csv index b2e818fb8..7dae5837a 100644 --- a/targets/f7/api_symbols.csv +++ b/targets/f7/api_symbols.csv @@ -2805,6 +2805,7 @@ Function,+,nfc_set_mask_receive_time_fc,void,"Nfc*, uint32_t" Function,+,nfc_start,void,"Nfc*, NfcEventCallback, void*" Function,+,nfc_stop,void,Nfc* Function,+,nfc_util_even_parity32,uint8_t,uint32_t +Function,+,nfc_util_even_parity8,uint8_t,uint8_t Function,+,nfc_util_odd_parity,void,"const uint8_t*, uint8_t*, uint8_t" Function,+,nfc_util_odd_parity8,uint8_t,uint8_t Function,+,notification_internal_message,void,"NotificationApp*, const NotificationSequence*" @@ -3526,7 +3527,7 @@ Function,-,ungetc,int,"int, FILE*" Function,-,unsetenv,int,const char* Function,-,usbd_poll,void,usbd_device* Function,-,utoa,char*,"unsigned, char*, int" -Function,+,valid_nonce,_Bool,"uint32_t, uint32_t, uint32_t, uint8_t" +Function,+,valid_nonce,_Bool,"uint32_t, uint32_t, uint8_t" Function,+,validator_is_file_alloc_init,ValidatorIsFile*,"const char*, const char*, const char*" Function,+,validator_is_file_callback,_Bool,"const char*, FuriString*, void*" Function,+,validator_is_file_free,void,ValidatorIsFile*