Skip to content

Commit

Permalink
Merge remote-tracking branch 'leptoptilos' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Feb 10, 2024
2 parents 8584702 + 4265b9c commit 14ae52f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 76 deletions.
2 changes: 1 addition & 1 deletion applications/main/nfc/plugins/supported_cards/aime.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static bool aime_read(Nfc* nfc, NfcDevice* device) {

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
2 changes: 1 addition & 1 deletion applications/main/nfc/plugins/supported_cards/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static bool hid_read(Nfc* nfc, NfcDevice* device) {

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
114 changes: 83 additions & 31 deletions applications/main/nfc/plugins/supported_cards/kazan.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct {
uint64_t b;
} MfClassicKeyPair;

static const MfClassicKeyPair kazan_1k_keys_standart[] = {
static const MfClassicKeyPair kazan_1k_keys_v1[] = {
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
Expand All @@ -53,7 +53,7 @@ static const MfClassicKeyPair kazan_1k_keys_standart[] = {
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
};

static const MfClassicKeyPair kazan_1k_keys_old[] = {
static const MfClassicKeyPair kazan_1k_keys_v2[] = {
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
Expand All @@ -72,6 +72,25 @@ static const MfClassicKeyPair kazan_1k_keys_old[] = {
{.a = 0xFFFFFFFFFFFF, .b = 0xFFFFFFFFFFFF},
};

static const MfClassicKeyPair kazan_1k_keys_v3[] = {
{.a = 0x165D3B5280C0, .b = 0xFC7C2BB34E0F},
{.a = 0xC178E3DA7A39, .b = 0xC70FB78B4934},
{.a = 0x1BDF96089D2F, .b = 0x9500F058ABC5},
{.a = 0xB65AA70AD524, .b = 0x733A63B8B7F3},
{.a = 0x8BDB8FECDCAF, .b = 0xB0048EE71C0F},
{.a = 0xBC10468ABF05, .b = 0x1700A7D5C034},
{.a = 0xE7F3282E0C7D, .b = 0x65909B89BDA5},
{.a = 0x986C63DD0355, .b = 0x901C125ED37D},
{.a = 0x2058EAEE8446, .b = 0xCB9B23815F87},
{.a = 0x492F3744A1DC, .b = 0x6B770AADA274},
{.a = 0x87EB933B9BF7, .b = 0xFC98A9460EE5},
{.a = 0x7EBC8337F8F0, .b = 0x887C97E53DBC},
{.a = 0xA369BF6D4452, .b = 0x03BBA7CA2F24},
{.a = 0x37569D7992EF, .b = 0x710BBD01B3B8},
{.a = 0xD4AA94C4B5E8, .b = 0x7F5C4D210F0B},
{.a = 0x521B8C4B2123, .b = 0x2D2392CC43A7},
};

enum SubscriptionType {
SUBSCRIPTION_TYPE_UNKNOWN,
SUBSCRIPTION_TYPE_PURSE,
Expand All @@ -96,6 +115,9 @@ enum SubscriptionType get_subscription_type(uint8_t value, FuriString* tariff_na
case 0x53:
furi_string_printf(tariff_name, "Standart purse");
return SUBSCRIPTION_TYPE_PURSE;
case 0x01:
furi_string_printf(tariff_name, "Token");
return SUBSCRIPTION_TYPE_ABONNEMENT_BY_TRIPS;
default:
furi_string_printf(tariff_name, "Unknown");
return SUBSCRIPTION_TYPE_UNKNOWN;
Expand All @@ -106,21 +128,34 @@ static bool kazan_verify(Nfc* nfc) {
bool verified = false;

do {
const uint8_t verification_sector_number = 10;
const uint8_t verification_sector_number = 8;
const uint8_t verification_block_number =
mf_classic_get_first_block_num_of_sector(verification_sector_number) + 1;
FURI_LOG_D(TAG, "Verifying sector %u", verification_sector_number);

MfClassicKey key = {0};
MfClassicKey key_1 = {0};
nfc_util_num2bytes(
kazan_1k_keys_standart[verification_sector_number].a, COUNT_OF(key.data), key.data);
kazan_1k_keys_v1[verification_sector_number].a, COUNT_OF(key_1.data), key_1.data);

MfClassicAuthContext auth_context;
MfClassicError error = mf_classic_poller_sync_auth(
nfc, verification_block_number, &key, MfClassicKeyTypeA, &auth_context);
nfc, verification_block_number, &key_1, MfClassicKeyTypeA, &auth_context);
if(error != MfClassicErrorNone) {
FURI_LOG_D(TAG, "Failed to read block %u: %d", verification_block_number, error);
break;
FURI_LOG_D(
TAG, "Failed to read block %u: %d. Keys: v1", verification_block_number, error);

MfClassicKey key_2 = {0};
nfc_util_num2bytes(
kazan_1k_keys_v2[verification_sector_number].a, COUNT_OF(key_2.data), key_2.data);

MfClassicAuthContext auth_context;
MfClassicError error = mf_classic_poller_sync_auth(
nfc, verification_block_number, &key_2, MfClassicKeyTypeA, &auth_context);
if(error != MfClassicErrorNone) {
FURI_LOG_D(
TAG, "Failed to read block %u: %d. Keys: v2", verification_block_number, error);
break;
}
}

verified = true;
Expand All @@ -145,41 +180,57 @@ static bool kazan_read(Nfc* nfc, NfcDevice* device) {
data->type = type;
if(type != MfClassicType1k) break;

MfClassicDeviceKeys keys = {
MfClassicDeviceKeys keys_v1 = {
.key_a_mask = 0,
.key_b_mask = 0,
};

MfClassicDeviceKeys keys_v2 = {
.key_a_mask = 0,
.key_b_mask = 0,
};

MfClassicDeviceKeys keys_old = {
MfClassicDeviceKeys keys_v3 = {
.key_a_mask = 0,
.key_b_mask = 0,
};

for(size_t i = 0; i < mf_classic_get_total_sectors_num(data->type); i++) {
nfc_util_num2bytes(
kazan_1k_keys_standart[i].a, sizeof(MfClassicKey), keys.key_a[i].data);
nfc_util_num2bytes(
kazan_1k_keys_old[i].a, sizeof(MfClassicKey), keys_old.key_a[i].data);
FURI_BIT_SET(keys.key_a_mask, i);
FURI_BIT_SET(keys_old.key_a_mask, i);
nfc_util_num2bytes(kazan_1k_keys_v1[i].a, sizeof(MfClassicKey), keys_v1.key_a[i].data);
nfc_util_num2bytes(kazan_1k_keys_v2[i].a, sizeof(MfClassicKey), keys_v2.key_a[i].data);
nfc_util_num2bytes(kazan_1k_keys_v3[i].a, sizeof(MfClassicKey), keys_v3.key_a[i].data);

nfc_util_num2bytes(
kazan_1k_keys_standart[i].b, sizeof(MfClassicKey), keys.key_b[i].data);
nfc_util_num2bytes(
kazan_1k_keys_old[i].b, sizeof(MfClassicKey), keys_old.key_b[i].data);
FURI_BIT_SET(keys.key_b_mask, i);
FURI_BIT_SET(keys_old.key_b_mask, i);
FURI_BIT_SET(keys_v1.key_a_mask, i);
FURI_BIT_SET(keys_v2.key_a_mask, i);
FURI_BIT_SET(keys_v3.key_a_mask, i);

nfc_util_num2bytes(kazan_1k_keys_v1[i].b, sizeof(MfClassicKey), keys_v1.key_b[i].data);
nfc_util_num2bytes(kazan_1k_keys_v2[i].b, sizeof(MfClassicKey), keys_v2.key_b[i].data);
nfc_util_num2bytes(kazan_1k_keys_v3[i].b, sizeof(MfClassicKey), keys_v3.key_b[i].data);

FURI_BIT_SET(keys_v1.key_b_mask, i);
FURI_BIT_SET(keys_v2.key_b_mask, i);
FURI_BIT_SET(keys_v3.key_b_mask, i);
}

error = mf_classic_poller_sync_read(nfc, &keys, data);
if(error != MfClassicErrorNone) {
FURI_LOG_W(TAG, "Failed to read data: standart keys");
error = mf_classic_poller_sync_read(nfc, &keys_v1, data);
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data: keys_v1");
break;
}

if(!mf_classic_is_card_read(data)) {
error = mf_classic_poller_sync_read(nfc, &keys_old, data);
if(error != MfClassicErrorNone) {
FURI_LOG_W(TAG, "Failed to read data: old keys");
error = mf_classic_poller_sync_read(nfc, &keys_v2, data);
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data: keys_v1");
break;
}
}

if(!mf_classic_is_card_read(data)) {
error = mf_classic_poller_sync_read(nfc, &keys_v3, data);
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data: keys_v3");
break;
}
}
Expand All @@ -202,18 +253,19 @@ static bool kazan_parse(const NfcDevice* device, FuriString* parsed_data) {
bool parsed = false;

do {
const uint8_t verification_sector_number = 10;
const uint8_t ticket_sector_number = 8;
const uint8_t balance_sector_number = 9;

// Verify keys
MfClassicKeyPair keys = {};
const MfClassicSectorTrailer* sec_tr =
mf_classic_get_sector_trailer_by_sector(data, verification_sector_number);
mf_classic_get_sector_trailer_by_sector(data, ticket_sector_number);

keys.a = nfc_util_bytes2num(sec_tr->key_a.data, COUNT_OF(sec_tr->key_a.data));
keys.b = nfc_util_bytes2num(sec_tr->key_b.data, COUNT_OF(sec_tr->key_b.data));

if(keys.a != 0xF7A545095C49) {
if(((keys.a != kazan_1k_keys_v1[8].a) && (keys.a != kazan_1k_keys_v2[8].a)) ||
((keys.b != kazan_1k_keys_v1[8].b) && (keys.b != kazan_1k_keys_v2[8].b))) {
FURI_LOG_D(TAG, "Parser: Failed to verify key a: %llu", keys.a);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions applications/main/nfc/plugins/supported_cards/metromoney.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ static bool metromoney_read(Nfc* nfc, NfcDevice* device) {
}

error = mf_classic_poller_sync_read(nfc, &keys, data);
if(error != MfClassicErrorNone) {
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data");
break;
}

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
4 changes: 2 additions & 2 deletions applications/main/nfc/plugins/supported_cards/saflok.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ static bool saflok_read(Nfc* nfc, NfcDevice* device) {
}

error = mf_classic_poller_sync_read(nfc, &keys, data);
if(error != MfClassicErrorNone) {
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data");
break;
}

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
4 changes: 2 additions & 2 deletions applications/main/nfc/plugins/supported_cards/social_moscow.c
Original file line number Diff line number Diff line change
Expand Up @@ -1534,14 +1534,14 @@ static bool social_moscow_read(Nfc* nfc, NfcDevice* device) {
}

error = mf_classic_poller_sync_read(nfc, &keys, data);
if(error != MfClassicErrorNone) {
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data");
break;
}

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
4 changes: 2 additions & 2 deletions applications/main/nfc/plugins/supported_cards/troika.c
Original file line number Diff line number Diff line change
Expand Up @@ -1559,14 +1559,14 @@ static bool troika_read(Nfc* nfc, NfcDevice* device) {
}

error = mf_classic_poller_sync_read(nfc, &keys, data);
if(error != MfClassicErrorNone) {
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data");
break;
}

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
4 changes: 2 additions & 2 deletions applications/main/nfc/plugins/supported_cards/washcity.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ static bool washcity_read(Nfc* nfc, NfcDevice* device) {
}

error = mf_classic_poller_sync_read(nfc, &keys, data);
if(error != MfClassicErrorNone) {
if(error == MfClassicErrorNotPresent) {
FURI_LOG_W(TAG, "Failed to read data");
break;
}

nfc_device_set_data(device, NfcProtocolMfClassic, data);

is_read = mf_classic_is_card_read(data);
is_read = (error == MfClassicErrorNone);
} while(false);

mf_classic_free(data);
Expand Down
35 changes: 2 additions & 33 deletions applications/main/nfc/plugins/supported_cards/zolotaya_korona.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,6 @@ static const uint8_t info_sector_signature[] = {0xE2, 0x87, 0x80, 0x8E, 0x20, 0x
0xAE, 0xE0, 0xAE, 0xAD, 0xA0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};

#define FURI_HAL_RTC_SECONDS_PER_MINUTE 60
#define FURI_HAL_RTC_SECONDS_PER_HOUR (FURI_HAL_RTC_SECONDS_PER_MINUTE * 60)
#define FURI_HAL_RTC_SECONDS_PER_DAY (FURI_HAL_RTC_SECONDS_PER_HOUR * 24)
#define FURI_HAL_RTC_EPOCH_START_YEAR 1970

void timestamp_to_datetime(uint32_t timestamp, FuriHalRtcDateTime* datetime) {
uint32_t days = timestamp / FURI_HAL_RTC_SECONDS_PER_DAY;
uint32_t seconds_in_day = timestamp % FURI_HAL_RTC_SECONDS_PER_DAY;

datetime->year = FURI_HAL_RTC_EPOCH_START_YEAR;

while(days >= furi_hal_rtc_get_days_per_year(datetime->year)) {
days -= furi_hal_rtc_get_days_per_year(datetime->year);
(datetime->year)++;
}

datetime->month = 1;
while(days >= furi_hal_rtc_get_days_per_month(
furi_hal_rtc_is_leap_year(datetime->year), datetime->month)) {
days -= furi_hal_rtc_get_days_per_month(
furi_hal_rtc_is_leap_year(datetime->year), datetime->month);
(datetime->month)++;
}

datetime->day = days + 1;
datetime->hour = seconds_in_day / FURI_HAL_RTC_SECONDS_PER_HOUR;
datetime->minute =
(seconds_in_day % FURI_HAL_RTC_SECONDS_PER_HOUR) / FURI_HAL_RTC_SECONDS_PER_MINUTE;
datetime->second = seconds_in_day % FURI_HAL_RTC_SECONDS_PER_MINUTE;
}

uint64_t bytes2num_bcd(const uint8_t* src, uint8_t len_bytes, bool* is_bcd) {
furi_assert(src);
furi_assert(len_bytes <= 9);
Expand Down Expand Up @@ -151,7 +120,7 @@ static bool zolotaya_korona_parse(const NfcDevice* device, FuriString* parsed_da
const uint16_t refill_counter = nfc_util_bytes2num_little_endian(block_start_ptr + 10, 2);

FuriHalRtcDateTime last_refill_datetime = {0};
timestamp_to_datetime(last_refill_timestamp, &last_refill_datetime);
furi_hal_rtc_timestamp_to_datetime(last_refill_timestamp, &last_refill_datetime);

// block 2: trip block
block_start_ptr = &data->block[start_trip_block_number + 2].data[0];
Expand All @@ -166,7 +135,7 @@ static bool zolotaya_korona_parse(const NfcDevice* device, FuriString* parsed_da
const uint8_t prev_balance_kop = prev_balance % 100;

FuriHalRtcDateTime last_trip_datetime = {0};
timestamp_to_datetime(last_trip_timestamp, &last_trip_datetime);
furi_hal_rtc_timestamp_to_datetime(last_trip_timestamp, &last_trip_datetime);

// PARSE DATA FROM PURSE SECTOR
const uint8_t start_purse_block_number =
Expand Down

0 comments on commit 14ae52f

Please sign in to comment.