Fix valid_nonce

This commit is contained in:
noproto 2024-08-11 14:30:26 -04:00
parent 3acba77070
commit 6332ec7478
6 changed files with 18 additions and 18 deletions

View File

@ -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 // 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) { bool valid_nonce(uint32_t nt, uint32_t ks, uint8_t nt_par_enc) {
return ((nfc_util_odd_parity8((Nt >> 24) & 0xFF) == return (nfc_util_even_parity8((nt >> 24) & 0xFF) ==
(((parity >> 3) & 1) ^ nfc_util_odd_parity8((NtEnc >> 24) & 0xFF) ^ (((nt_par_enc >> 3) & 1) ^ FURI_BIT(ks, 16))) &&
FURI_BIT(Ks1, 16))) && (nfc_util_even_parity8((nt >> 16) & 0xFF) ==
(nfc_util_odd_parity8((Nt >> 16) & 0xFF) == (((nt_par_enc >> 2) & 1) ^ FURI_BIT(ks, 8))) &&
(((parity >> 2) & 1) ^ nfc_util_odd_parity8((NtEnc >> 16) & 0xFF) ^ (nfc_util_even_parity8((nt >> 8) & 0xFF) ==
FURI_BIT(Ks1, 8))) && (((nt_par_enc >> 1) & 1) ^ FURI_BIT(ks, 0)));
(nfc_util_odd_parity8((Nt >> 8) & 0xFF) ==
(((parity >> 1) & 1) ^ nfc_util_odd_parity8((NtEnc >> 8) & 0xFF) ^
FURI_BIT(Ks1, 0)))) ?
true :
false;
} }

View File

@ -40,7 +40,7 @@ void crypto1_encrypt_reader_nonce(
uint32_t lfsr_rollback_word(Crypto1* crypto1, uint32_t in, int fb); 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); uint32_t prng_successor(uint32_t x, uint32_t n);

View File

@ -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, 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}; 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) { uint8_t nfc_util_even_parity32(uint32_t data) {
// data ^= data >> 16; // data ^= data >> 16;
// data ^= data >> 8; // data ^= data >> 8;

View File

@ -6,6 +6,8 @@
extern "C" { extern "C" {
#endif #endif
uint8_t nfc_util_even_parity8(uint8_t data);
uint8_t nfc_util_even_parity32(uint32_t data); uint8_t nfc_util_even_parity32(uint32_t data);
uint8_t nfc_util_odd_parity8(uint8_t data); uint8_t nfc_util_odd_parity8(uint8_t data);

View File

@ -1250,8 +1250,7 @@ NfcCommand mf_classic_poller_handler_nested_collect_nt_enc(MfClassicPoller* inst
((dict_attack_ctx->d_median + current_dist) != UINT16_MAX)) { ((dict_attack_ctx->d_median + current_dist) != UINT16_MAX)) {
uint32_t nth_successor_positive = uint32_t nth_successor_positive =
prng_successor(decrypted_nt_prev, dict_attack_ctx->d_median + current_dist); prng_successor(decrypted_nt_prev, dict_attack_ctx->d_median + current_dist);
if(valid_nonce( if(valid_nonce(nth_successor_positive, nth_successor_positive ^ nt_enc, parity)) {
nth_successor_positive, nt_enc, nth_successor_positive ^ nt_enc, parity)) {
found_matching_nt = true; found_matching_nt = true;
found_nt = nth_successor_positive; found_nt = nth_successor_positive;
break; break;
@ -1259,8 +1258,7 @@ NfcCommand mf_classic_poller_handler_nested_collect_nt_enc(MfClassicPoller* inst
if(current_dist > 0) { if(current_dist > 0) {
uint32_t nth_successor_negative = uint32_t nth_successor_negative =
prng_successor(decrypted_nt_prev, dict_attack_ctx->d_median - current_dist); prng_successor(decrypted_nt_prev, dict_attack_ctx->d_median - current_dist);
if(valid_nonce( if(valid_nonce(nth_successor_negative, nth_successor_negative ^ nt_enc, parity)) {
nth_successor_negative, nt_enc, nth_successor_negative ^ nt_enc, parity)) {
found_matching_nt = true; found_matching_nt = true;
found_nt = nth_successor_negative; found_nt = nth_successor_negative;
break; break;

View File

@ -2805,6 +2805,7 @@ Function,+,nfc_set_mask_receive_time_fc,void,"Nfc*, uint32_t"
Function,+,nfc_start,void,"Nfc*, NfcEventCallback, void*" Function,+,nfc_start,void,"Nfc*, NfcEventCallback, void*"
Function,+,nfc_stop,void,Nfc* Function,+,nfc_stop,void,Nfc*
Function,+,nfc_util_even_parity32,uint8_t,uint32_t 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_parity,void,"const uint8_t*, uint8_t*, uint8_t"
Function,+,nfc_util_odd_parity8,uint8_t,uint8_t Function,+,nfc_util_odd_parity8,uint8_t,uint8_t
Function,+,notification_internal_message,void,"NotificationApp*, const NotificationSequence*" Function,+,notification_internal_message,void,"NotificationApp*, const NotificationSequence*"
@ -3526,7 +3527,7 @@ Function,-,ungetc,int,"int, FILE*"
Function,-,unsetenv,int,const char* Function,-,unsetenv,int,const char*
Function,-,usbd_poll,void,usbd_device* Function,-,usbd_poll,void,usbd_device*
Function,-,utoa,char*,"unsigned, char*, int" 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_alloc_init,ValidatorIsFile*,"const char*, const char*, const char*"
Function,+,validator_is_file_callback,_Bool,"const char*, FuriString*, void*" Function,+,validator_is_file_callback,_Bool,"const char*, FuriString*, void*"
Function,+,validator_is_file_free,void,ValidatorIsFile* Function,+,validator_is_file_free,void,ValidatorIsFile*

1 entry status name type params
2805 Function + nfc_start void Nfc*, NfcEventCallback, void*
2806 Function + nfc_stop void Nfc*
2807 Function + nfc_util_even_parity32 uint8_t uint32_t
2808 Function + nfc_util_even_parity8 uint8_t uint8_t
2809 Function + nfc_util_odd_parity void const uint8_t*, uint8_t*, uint8_t
2810 Function + nfc_util_odd_parity8 uint8_t uint8_t
2811 Function + notification_internal_message void NotificationApp*, const NotificationSequence*
3527 Function - unsetenv int const char*
3528 Function - usbd_poll void usbd_device*
3529 Function - utoa char* unsigned, char*, int
3530 Function + valid_nonce _Bool uint32_t, uint32_t, uint32_t, uint8_t uint32_t, uint32_t, uint8_t
3531 Function + validator_is_file_alloc_init ValidatorIsFile* const char*, const char*, const char*
3532 Function + validator_is_file_callback _Bool const char*, FuriString*, void*
3533 Function + validator_is_file_free void ValidatorIsFile*