Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gornek/nfc_refactoring_v1' into …
Browse files Browse the repository at this point in the history
…gornek/nfc_more_fixes
  • Loading branch information
gornekich committed Oct 17, 2023
2 parents 2baad11 + 268cd9f commit 6e131c3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/nfc/protocols/mf_ultralight/mf_ultralight.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {

#define MF_ULTRALIGHT_CMD_ACK (0x0A)
#define MF_ULTRALIGHT_CMD_NACK (0x00)
#define MF_ULTRALIGHT_CMD_AUTH_NAK (0x04)

#define MF_ULTRALIGHT_MAX_CNTR_VAL (0x00FFFFFF)
#define MF_ULTRALIGHT_MAX_PAGE_NUM (510)
Expand Down
18 changes: 15 additions & 3 deletions lib/nfc/protocols/mf_ultralight/mf_ultralight_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,19 @@ static MfUltralightCommand
instance->callback(instance->generic_event, instance->context);
}

uint8_t* config_pass = instance->config->password.data;
uint8_t* auth_pass = password.data;
if(memcmp(config_pass, auth_pass, sizeof(MfUltralightAuthPassword)) != 0) break;
bool auth_success =
mf_ultralight_auth_check_password(&instance->config->password, &password);
bool card_locked = mf_ultralight_auth_limit_check_and_update(instance, auth_success);

if(card_locked) {
command = MfUltralightCommandNotProcessedAuthNAK;
break;
}

if(!auth_success) {
command = MfUltralightCommandNotProcessedNAK;
break;
}

bit_buffer_copy_bytes(
instance->tx_buffer, instance->config->pack.data, sizeof(MfUltralightAuthPack));
Expand Down Expand Up @@ -645,6 +655,8 @@ static NfcCommand mf_ultralight_command_postprocess(

if(mfu_command == MfUltralightCommandNotProcessedNAK) {
mf_ultralight_listener_send_short_resp(instance, MF_ULTRALIGHT_CMD_NACK);
} else if(mfu_command == MfUltralightCommandNotProcessedAuthNAK) {
mf_ultralight_listener_send_short_resp(instance, MF_ULTRALIGHT_CMD_AUTH_NAK);
}
}

Expand Down
38 changes: 38 additions & 0 deletions lib/nfc/protocols/mf_ultralight/mf_ultralight_listener_i.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#define MF_ULTRALIGHT_I2C_PAGE_ON_MIRRORED_SESSION_REG(page) \
MF_ULTRALIGHT_PAGE_IN_BOUNDS(page, 0x00F8, 0x00F9)

#define MF_ULTRALIGHT_AUTH_RESET_ATTEMPTS(instance) (instance->data->auth_attempts = 0)
#define MF_ULTRALIGHT_AUTH_INCREASE_ATTEMPTS(instance) (instance->data->auth_attempts++)

static MfUltralightMirrorConf mf_ultralight_mirror_check_mode(
const MfUltralightConfigPages* const config,
const MfUltralightListenerAuthState auth_state) {
Expand Down Expand Up @@ -536,4 +539,39 @@ bool mf_ultralight_dynamic_lock_check_page(const MfUltralightListener* instance,
locked = MF_ULTRALIGHT_PAGE_LOCKED(current_locks, bit);
}
return locked;
}

static bool mf_ultralight_auth_check_attempts(const MfUltralightListener* instance) {
uint8_t authlim = ((instance->data->type == MfUltralightTypeNTAGI2CPlus1K) ||
(instance->data->type == MfUltralightTypeNTAGI2CPlus2K)) ?
(1U << instance->config->access.authlim) :
instance->config->access.authlim;

return (instance->data->auth_attempts >= authlim);
}

bool mf_ultralight_auth_limit_check_and_update(MfUltralightListener* instance, bool auth_success) {
bool card_locked = false;

do {
if(instance->config->access.authlim == 0) break;
card_locked = mf_ultralight_auth_check_attempts(instance);
if(card_locked) break;

if(auth_success) {
MF_ULTRALIGHT_AUTH_RESET_ATTEMPTS(instance);
} else {
MF_ULTRALIGHT_AUTH_INCREASE_ATTEMPTS(instance);
}

card_locked = mf_ultralight_auth_check_attempts(instance);
} while(false);

return card_locked;
}

bool mf_ultralight_auth_check_password(
const MfUltralightAuthPassword* config_pass,
const MfUltralightAuthPassword* auth_pass) {
return memcmp(config_pass->data, auth_pass->data, sizeof(MfUltralightAuthPassword)) == 0;
}
6 changes: 5 additions & 1 deletion lib/nfc/protocols/mf_ultralight/mf_ultralight_listener_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef enum {
MfUltralightCommandProcessedSilent,
MfUltralightCommandNotProcessedNAK,
MfUltralightCommandNotProcessedSilent,
MfUltralightCommandNotProcessedAuthNAK,
} MfUltralightCommand;

typedef MfUltralightCommand (
Expand Down Expand Up @@ -113,7 +114,10 @@ void mf_ultralight_dynamic_lock_bytes_write(
MfUltralightDynamicLockData* const lock_bits,
uint32_t new_bits);
bool mf_ultralight_dynamic_lock_check_page(const MfUltralightListener* instance, uint16_t page);

bool mf_ultralight_auth_limit_check_and_update(MfUltralightListener* instance, bool auth_success);
bool mf_ultralight_auth_check_password(
const MfUltralightAuthPassword* config_pass,
const MfUltralightAuthPassword* auth_pass);
#ifdef __cplusplus
}
#endif

0 comments on commit 6e131c3

Please sign in to comment.