mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-26 03:39:58 -07:00
Revert NFC FeliCa WIP PR (breaks more than adds)
This commit is contained in:
@@ -1,943 +0,0 @@
|
||||
#include <limits.h>
|
||||
#include <mbedtls/des.h>
|
||||
#include <mbedtls/sha1.h>
|
||||
#include "felica.h"
|
||||
#include "nfc_util.h"
|
||||
#include <furi.h>
|
||||
#include "furi_hal_nfc.h"
|
||||
|
||||
#define TAG "FeliCa"
|
||||
|
||||
bool felica_check_ic_type(uint8_t* PMm) {
|
||||
uint8_t rom_type = PMm[0];
|
||||
uint8_t ic_type = PMm[1];
|
||||
|
||||
bool is_valid_ic = false;
|
||||
if(ic_type == 0xff) { // RC-S967 in nfc-dep
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0xf2) { // RC-S732?
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0xf0 || ic_type == 0xf1) { // Lite(S)
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0xe1) { // RC-S967 in plug mode
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0xe0) { // RC-S926
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type >= 0x44 && ic_type <= 0x48) { // SD2
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x3e && rom_type == 0x03) { // RC-SA08
|
||||
return true;
|
||||
} else if(ic_type == 0x35) { // RC-SA01
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x32) { // RC-SA00
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x31) { // Suica/PASMO
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x20) { // RC-S962
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type >= 0x10 && ic_type <= 0x1f) { // Mobile IC version 2/3
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x0d) { // RC-S960
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x0c) { // RC-S954
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x0b) { // Old Suica?
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x09) { // RC-S953
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x08) { // RC-S952
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x06 || ic_type == 0x07) { // Mobile IC version 1
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x02) { // RC-S919
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x01) { // RC-S915
|
||||
is_valid_ic = true;
|
||||
} else if(ic_type == 0x00) { // RC-S830
|
||||
is_valid_ic = true;
|
||||
}
|
||||
|
||||
if(!is_valid_ic) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// need more samples to confirm below
|
||||
/*
|
||||
if (rom_type != 0x01) {
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
FelicaICType felica_get_ic_type(uint8_t* PMm) {
|
||||
uint8_t rom_type = PMm[0];
|
||||
uint8_t ic_type = PMm[1];
|
||||
|
||||
UNUSED(rom_type);
|
||||
switch(ic_type) {
|
||||
case 0xff:
|
||||
return FelicaICTypeLink;
|
||||
case 0xf2:
|
||||
return FelicaICTypeLink;
|
||||
case 0xf1:
|
||||
return FelicaICTypeLiteS;
|
||||
case 0xf0:
|
||||
return FelicaICTypeLite;
|
||||
case 0xe1:
|
||||
return FelicaICTypeLink;
|
||||
case 0xe0:
|
||||
return FelicaICTypePlug;
|
||||
case 0x48:
|
||||
return FelicaICTypeSD2_6K;
|
||||
case 0x47:
|
||||
return FelicaICTypeRC_SA24_6K;
|
||||
case 0x46:
|
||||
return FelicaICTypeSD2_4K;
|
||||
case 0x45:
|
||||
case 0x44:
|
||||
return FelicaICTypeSD2WithDES;
|
||||
case 0x3e:
|
||||
return FelicaICTypeRC_SA08;
|
||||
case 0x35:
|
||||
return FelicaICTypeSD1;
|
||||
case 0x32:
|
||||
return FelicaICTypeSD1WithDES;
|
||||
case 0x31:
|
||||
return FelicaICTypeSuica;
|
||||
case 0x20:
|
||||
return FelicaICTypeFRAM_4K;
|
||||
case 0x1f:
|
||||
case 0x1e:
|
||||
case 0x1d:
|
||||
case 0x1c:
|
||||
case 0x1b:
|
||||
case 0x1a:
|
||||
case 0x19:
|
||||
case 0x18:
|
||||
return FelicaICTypeMobileIC_V4_1;
|
||||
case 0x17:
|
||||
return FelicaICTypeMobileIC_V4;
|
||||
case 0x16:
|
||||
case 0x15:
|
||||
case 0x14:
|
||||
return FelicaICTypeMobileIC_V3;
|
||||
case 0x13:
|
||||
case 0x12:
|
||||
case 0x11:
|
||||
case 0x10:
|
||||
return FelicaICTypeMobileIC_V2;
|
||||
case 0x0d:
|
||||
return FelicaICTypeFRAM_9K;
|
||||
case 0x0c:
|
||||
return FelicaICTypeEMV_36K;
|
||||
case 0x0b: // Old Suica?
|
||||
return FelicaICTypeSuica;
|
||||
case 0x09:
|
||||
return FelicaICTypeEMV_16K;
|
||||
case 0x08:
|
||||
return FelicaICTypeEMV_32K;
|
||||
case 0x07:
|
||||
case 0x06:
|
||||
return FelicaICTypeMobileIC_V1;
|
||||
case 0x02:
|
||||
return FelicaICType576B;
|
||||
case 0x01:
|
||||
return FelicaICType4K;
|
||||
case 0x00:
|
||||
return FelicaICType2K;
|
||||
}
|
||||
|
||||
return FelicaICType2K;
|
||||
}
|
||||
|
||||
// static void felica_lite_diversify_key(uint8_t* id_block, uint8_t* master_key, uint8_t* card_key) {
|
||||
// uint8_t ZERO[8] = {0};
|
||||
// uint8_t L[8];
|
||||
// mbedtls_des3_context ctx;
|
||||
// mbedtls_des3_init(&ctx);
|
||||
// mbedtls_des3_set3key_enc(&ctx, master_key);
|
||||
// mbedtls_des3_crypt_ecb(&ctx, ZERO, L);
|
||||
// mbedtls_des3_free(&ctx);
|
||||
|
||||
// uint8_t K1[8];
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// K1[i] = L[i] << 1;
|
||||
// if(i < 7) {
|
||||
// K1[i] |= (L[i + 1] >> 7);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if((L[0] ^ 0x80) == 0) {
|
||||
// K1[7] ^= 0x1B;
|
||||
// }
|
||||
|
||||
// uint8_t M1[8];
|
||||
// uint8_t M2[8];
|
||||
// memcpy(M1, id_block, 8);
|
||||
// memcpy(M2, id_block + 8, 8);
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// M2[i] ^= K1[i];
|
||||
// }
|
||||
|
||||
// uint8_t C1[8];
|
||||
// mbedtls_des3_init(&ctx);
|
||||
// mbedtls_des3_set3key_enc(&ctx, master_key);
|
||||
// mbedtls_des3_crypt_ecb(&ctx, M1, C1);
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// C1[i] ^= M2[i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_crypt_ecb(&ctx, C1, card_key); // T
|
||||
|
||||
// M1[0] ^= 0x80; // M'1
|
||||
// mbedtls_des3_crypt_ecb(&ctx, M1, C1); // C'1
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// C1[i] ^= M2[i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_crypt_ecb(&ctx, C1, card_key + 8); // T'
|
||||
// mbedtls_des3_free(&ctx);
|
||||
// }
|
||||
|
||||
// static void felica_lite_generate_session_key(
|
||||
// uint8_t* random_challenge,
|
||||
// uint8_t* card_key,
|
||||
// uint8_t* session_key) {
|
||||
// uint8_t RC1[8];
|
||||
// uint8_t RC2[8];
|
||||
// uint8_t CK[16];
|
||||
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// RC1[i] = random_challenge[7 - i];
|
||||
// RC2[i] = random_challenge[i];
|
||||
// CK[i] = card_key[7 - i];
|
||||
// CK[i + 8] = card_key[15 - i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_context ctx;
|
||||
|
||||
// uint8_t SK1[8];
|
||||
// mbedtls_des3_init(&ctx);
|
||||
// mbedtls_des3_set2key_enc(&ctx, CK);
|
||||
// mbedtls_des3_crypt_ecb(&ctx, RC1, SK1);
|
||||
|
||||
// uint8_t SK2[8];
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// RC2[i] ^= SK1[i];
|
||||
// }
|
||||
// mbedtls_des3_crypt_ecb(&ctx, RC2, SK2);
|
||||
// mbedtls_des3_free(&ctx);
|
||||
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// session_key[i] = SK1[7 - i];
|
||||
// session_key[i + 8] = SK2[7 - i];
|
||||
// }
|
||||
// }
|
||||
|
||||
// static void felica_lite_calculate_mac(
|
||||
// uint8_t* random_challenge,
|
||||
// uint8_t* session_key,
|
||||
// uint8_t* block_data,
|
||||
// size_t block_count,
|
||||
// uint8_t* MAC) {
|
||||
// uint8_t SK[16];
|
||||
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// MAC[i] = random_challenge[7 - i];
|
||||
// SK[i] = session_key[7 - i];
|
||||
// SK[i + 8] = session_key[15 - i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_context ctx;
|
||||
// mbedtls_des3_init(&ctx);
|
||||
// mbedtls_des3_set3key_enc(&ctx, SK);
|
||||
|
||||
// for(size_t block_num = 0; block_num < block_count; block_num++) {
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// MAC[i] ^= block_data[block_num * FELICA_BLOCK_SIZE + 7 - i];
|
||||
// }
|
||||
|
||||
// uint8_t intermediate[8];
|
||||
// mbedtls_des3_crypt_ecb(&ctx, MAC, intermediate);
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// intermediate[i] ^= block_data[block_num * FELICA_BLOCK_SIZE + 15 - i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_crypt_ecb(&ctx, intermediate, MAC);
|
||||
// }
|
||||
|
||||
// mbedtls_des3_free(&ctx);
|
||||
// }
|
||||
|
||||
// static void felica_lite_calculate_mac_a(
|
||||
// uint8_t* random_challenge,
|
||||
// uint8_t* session_key,
|
||||
// uint8_t* iv,
|
||||
// uint8_t* block_data,
|
||||
// size_t block_count,
|
||||
// uint8_t* MAC_A) {
|
||||
// uint8_t SK[16];
|
||||
// uint8_t intermediate_a[8];
|
||||
// uint8_t intermediate_b[8];
|
||||
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// intermediate_a[i] = iv[7 - 1] ^ random_challenge[7 - i];
|
||||
// SK[i] = session_key[7 - i];
|
||||
// SK[i + 8] = session_key[15 - i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_context ctx;
|
||||
// mbedtls_des3_init(&ctx);
|
||||
// mbedtls_des3_set3key_enc(&ctx, SK);
|
||||
// mbedtls_des3_crypt_ecb(&ctx, intermediate_a, intermediate_b);
|
||||
|
||||
// for(size_t block_num = 0; block_num < block_count; block_num++) {
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// intermediate_b[i] ^= block_data[block_num * FELICA_BLOCK_SIZE + 7 - i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_crypt_ecb(&ctx, intermediate_b, intermediate_a);
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// intermediate_a[i] ^= block_data[block_num * FELICA_BLOCK_SIZE + 7 - i];
|
||||
// }
|
||||
|
||||
// mbedtls_des3_crypt_ecb(&ctx, intermediate_a, intermediate_b);
|
||||
// }
|
||||
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// MAC_A[i] = intermediate_b[7 - 1];
|
||||
// }
|
||||
// }
|
||||
|
||||
// static void felica_lite_calculate_mac_a_for_write(
|
||||
// uint8_t* random_challenge,
|
||||
// uint8_t* session_key,
|
||||
// uint32_t write_count,
|
||||
// uint8_t block_number,
|
||||
// uint8_t* block_data,
|
||||
// uint8_t* MAC_A) {
|
||||
// uint8_t iv[8];
|
||||
// nfc_util_num2bytes(write_count, 3, iv);
|
||||
// iv[3] = 0x00;
|
||||
// iv[4] = block_number;
|
||||
// iv[5] = 0x00;
|
||||
// iv[6] = 0x91;
|
||||
// iv[7] = 0x00;
|
||||
|
||||
// uint8_t SK[16];
|
||||
// for(int i = 0; i < 8; i++) {
|
||||
// SK[i] = session_key[i + 8];
|
||||
// SK[i + 8] = session_key[i];
|
||||
// }
|
||||
|
||||
// felica_lite_calculate_mac_a(random_challenge, SK, iv, block_data, 1, MAC_A);
|
||||
// }
|
||||
|
||||
// static void felica_lite_calculate_mac_a_for_read(
|
||||
// uint8_t* random_challenge,
|
||||
// uint8_t* session_key,
|
||||
// uint8_t* block_list,
|
||||
// uint8_t block_list_count,
|
||||
// uint8_t* block_data,
|
||||
// uint8_t block_count,
|
||||
// uint8_t* MAC_A) {
|
||||
// uint8_t iv[8] = {0};
|
||||
|
||||
// uint8_t block_list_to_write = MIN(block_list_count, 4);
|
||||
// for(int i = 0; i < block_list_to_write; i++) {
|
||||
// iv[i * 2] = block_list[i];
|
||||
// }
|
||||
// if(block_list_to_write < 4) {
|
||||
// iv[6] = 0xFF;
|
||||
// iv[7] = 0xFF;
|
||||
// }
|
||||
// if(block_list_to_write < 3) {
|
||||
// iv[4] = 0xFF;
|
||||
// iv[5] = 0xFF;
|
||||
// }
|
||||
|
||||
// felica_lite_calculate_mac_a(random_challenge, session_key, iv, block_data, block_count, MAC_A);
|
||||
// }
|
||||
|
||||
/** Parse common FeliCa response headers.
|
||||
*
|
||||
* This parses and validates the most commonly occurring response header types.
|
||||
*
|
||||
* The header needs to match the (length, res, idm) format, and also (sf1, sf2) when always_succeed
|
||||
* is set to false.
|
||||
*
|
||||
* @param buf RX buffer.
|
||||
* @param len RX buffer length.
|
||||
* @param reader The FeliCa reader context.
|
||||
* @param expected_resp Expected response code. Must be an odd number.
|
||||
* @param always_succeed When set to true, skip status flags (sf1 and sf2) parsing.
|
||||
* @return The number of bytes parsed, or 0 when response is invalid or status flags are set.
|
||||
*/
|
||||
static uint8_t felica_consume_unencrypted_header(
|
||||
uint8_t* buf,
|
||||
uint8_t len,
|
||||
FelicaReader* reader,
|
||||
uint8_t expected_resp,
|
||||
bool always_succeed) {
|
||||
furi_assert(expected_resp & 1);
|
||||
furi_assert(buf != NULL);
|
||||
furi_assert(reader != NULL);
|
||||
|
||||
uint8_t header_size = always_succeed ? 10 : 12;
|
||||
if(len < header_size) {
|
||||
FURI_LOG_E(TAG, "Malformed header: too short.");
|
||||
return 0;
|
||||
}
|
||||
if(buf[1] != expected_resp) {
|
||||
FURI_LOG_E(TAG, "Expecting %u, got %u.", expected_resp, buf[1]);
|
||||
return 0;
|
||||
}
|
||||
if(memcmp(&buf[2], reader->current_idm, 8) != 0) {
|
||||
FURI_LOG_E(TAG, "IDm mismatch.");
|
||||
return 0;
|
||||
}
|
||||
if(always_succeed) {
|
||||
reader->status_flags[0] = buf[10];
|
||||
reader->status_flags[1] = buf[11];
|
||||
if(reader->status_flags[0] != 0 || reader->status_flags[1] != 0) {
|
||||
FURI_LOG_W(
|
||||
TAG, "SF1: %02X SF2: %02X", reader->status_flags[0], reader->status_flags[1]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return header_size;
|
||||
}
|
||||
|
||||
uint8_t felica_prepare_unencrypted_read(
|
||||
uint8_t* dest,
|
||||
const FelicaReader* reader,
|
||||
const uint16_t* service_code_list,
|
||||
uint8_t service_count,
|
||||
const uint32_t* block_list,
|
||||
uint8_t block_count) {
|
||||
dest[0] = FELICA_UNENCRYPTED_READ_CMD;
|
||||
memcpy(&dest[1], reader->current_idm, 8);
|
||||
|
||||
dest[9] = service_count;
|
||||
uint8_t msg_len = 10;
|
||||
for(int i = 0; i < service_count; i++) {
|
||||
uint16_t service_code = service_code_list[i];
|
||||
dest[msg_len++] = service_code & 0xFF;
|
||||
dest[msg_len++] = service_code >> 8;
|
||||
}
|
||||
|
||||
dest[msg_len++] = block_count;
|
||||
for(int i = 0; i < block_count; i++) {
|
||||
uint16_t block_num = block_list[i];
|
||||
dest[msg_len++] = block_num & 0xFF;
|
||||
dest[msg_len++] = block_num >> 8;
|
||||
}
|
||||
|
||||
return msg_len;
|
||||
}
|
||||
|
||||
uint8_t felica_lite_prepare_unencrypted_read(
|
||||
uint8_t* dest,
|
||||
const FelicaReader* reader,
|
||||
bool is_read_only,
|
||||
const uint8_t* block_list,
|
||||
uint8_t block_count) {
|
||||
dest[0] = FELICA_UNENCRYPTED_READ_CMD;
|
||||
memcpy(&dest[1], reader->current_idm, 8);
|
||||
|
||||
dest[9] = 1;
|
||||
uint8_t msg_len = 10;
|
||||
uint8_t service_code =
|
||||
FelicaServiceTypeRandom |
|
||||
((is_read_only) ? FelicaServiceAttributeUnauthRO : FelicaServiceAttributeUnauthRO);
|
||||
|
||||
dest[msg_len++] = service_code & 0xFF;
|
||||
dest[msg_len++] = service_code >> 8;
|
||||
|
||||
dest[msg_len++] = block_count;
|
||||
for(int i = 0; i < block_count; i++) {
|
||||
dest[msg_len++] = IS_2_BYTE_BLOCK_LIST_ELEMENT;
|
||||
dest[msg_len++] = block_list[i];
|
||||
}
|
||||
|
||||
return msg_len;
|
||||
}
|
||||
|
||||
uint16_t felica_parse_unencrypted_read(
|
||||
uint8_t* buf,
|
||||
uint8_t len,
|
||||
FelicaReader* reader,
|
||||
uint8_t* out,
|
||||
uint16_t out_len) {
|
||||
uint8_t consumed =
|
||||
felica_consume_unencrypted_header(buf, len, reader, FELICA_UNENCRYPTED_READ_RES, false);
|
||||
if(!consumed) {
|
||||
return 0;
|
||||
}
|
||||
len -= consumed;
|
||||
buf += consumed;
|
||||
|
||||
if(len < 1) {
|
||||
return 0;
|
||||
}
|
||||
uint16_t data_length = *buf * FELICA_BLOCK_SIZE;
|
||||
len--;
|
||||
buf++;
|
||||
|
||||
if(len < data_length || out_len < data_length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(out, buf, data_length);
|
||||
|
||||
return data_length;
|
||||
}
|
||||
|
||||
uint8_t felica_prepare_unencrypted_write(
|
||||
uint8_t* dest,
|
||||
FelicaReader* reader,
|
||||
const uint16_t* service_code_list,
|
||||
uint8_t service_count,
|
||||
const uint32_t* block_list,
|
||||
uint8_t block_count,
|
||||
const uint8_t* block_data) {
|
||||
dest[0] = FELICA_UNENCRYPTED_WRITE_CMD;
|
||||
memcpy(&dest[1], reader->current_idm, 8);
|
||||
|
||||
dest[9] = service_count;
|
||||
uint8_t msg_len = 10;
|
||||
for(int i = 0; i < service_count; i++) {
|
||||
uint16_t service_code = service_code_list[i];
|
||||
dest[msg_len++] = service_code & 0xFF;
|
||||
dest[msg_len++] = service_code >> 8;
|
||||
}
|
||||
|
||||
dest[msg_len++] = block_count;
|
||||
for(int i = 0; i < block_count; i++) {
|
||||
uint16_t block_num = block_list[i];
|
||||
dest[msg_len++] = block_num & 0xFF;
|
||||
dest[msg_len++] = block_num >> 8;
|
||||
}
|
||||
|
||||
uint16_t data_length = block_count * FELICA_BLOCK_SIZE;
|
||||
memcpy(dest + msg_len, block_data, data_length);
|
||||
msg_len += data_length;
|
||||
return msg_len;
|
||||
}
|
||||
|
||||
uint8_t felica_lite_prepare_unencrypted_write(
|
||||
uint8_t* dest,
|
||||
const FelicaReader* reader,
|
||||
const uint8_t* block_list,
|
||||
uint8_t block_count,
|
||||
const uint8_t* block_data) {
|
||||
dest[0] = FELICA_UNENCRYPTED_WRITE_CMD;
|
||||
memcpy(&dest[1], reader->current_idm, 8);
|
||||
|
||||
dest[9] = 1;
|
||||
uint8_t msg_len = 10;
|
||||
uint8_t service_code = FelicaServiceTypeRandom | FelicaServiceAttributeUnauthRW;
|
||||
dest[msg_len++] = service_code & 0xFF;
|
||||
dest[msg_len++] = service_code >> 8;
|
||||
|
||||
dest[msg_len++] = block_count;
|
||||
for(int i = 0; i < block_count; i++) {
|
||||
dest[msg_len++] = block_list[i];
|
||||
dest[msg_len++] = IS_2_BYTE_BLOCK_LIST_ELEMENT;
|
||||
}
|
||||
|
||||
uint16_t data_length = block_count * FELICA_BLOCK_SIZE;
|
||||
memcpy(dest + msg_len, block_data, data_length);
|
||||
msg_len += data_length;
|
||||
return msg_len;
|
||||
}
|
||||
|
||||
bool felica_parse_unencrypted_write(uint8_t* buf, uint8_t len, FelicaReader* reader) {
|
||||
uint8_t consumed =
|
||||
felica_consume_unencrypted_header(buf, len, reader, FELICA_UNENCRYPTED_WRITE_RES, false);
|
||||
if(!consumed) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t felica_prepare_request_system_code(uint8_t* dest, FelicaReader* reader) {
|
||||
dest[0] = FELICA_REQUEST_SYSTEM_CODE_CMD;
|
||||
memcpy(&dest[1], reader->current_idm, 8);
|
||||
return 9;
|
||||
}
|
||||
|
||||
bool felica_parse_request_system_code(
|
||||
uint8_t* buf,
|
||||
uint8_t len,
|
||||
FelicaReader* reader,
|
||||
FelicaSystemArray_t* systems) {
|
||||
uint8_t consumed =
|
||||
felica_consume_unencrypted_header(buf, len, reader, FELICA_REQUEST_SYSTEM_CODE_RES, true);
|
||||
if(consumed == 0) {
|
||||
return false;
|
||||
}
|
||||
len -= consumed;
|
||||
buf += consumed;
|
||||
|
||||
uint8_t entries = *buf;
|
||||
len--;
|
||||
buf++;
|
||||
|
||||
if(len < 2 * entries) {
|
||||
FURI_LOG_E(TAG, "FELICA_REQUEST_SYSTEM_CODE_RES: Response too short");
|
||||
return false;
|
||||
}
|
||||
|
||||
for(uint8_t idx = 0; idx < entries; idx++) {
|
||||
FelicaSystem* system = FelicaSystemArray_push_new(*systems);
|
||||
furi_assert(system != NULL);
|
||||
|
||||
// Set system code
|
||||
system->number = idx;
|
||||
system->code = buf[2 * idx] | (buf[2 * idx + 1] << 8);
|
||||
|
||||
FURI_LOG_D(TAG, "Found system code %04X", system->code);
|
||||
|
||||
// Fill in IDm and PMm
|
||||
memcpy(system->idm, reader->current_idm, 8);
|
||||
memcpy(system->pmm, reader->current_pmm, 8);
|
||||
|
||||
// Set system index field in IDm
|
||||
system->idm[0] &= 0x0f;
|
||||
system->idm[0] |= ((idx & 0xf) << 4);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static FelicaSystem* felica_gen_monolithic_system_code(
|
||||
FelicaReader* reader,
|
||||
FelicaSystemArray_t* systems,
|
||||
uint16_t system_code) {
|
||||
FelicaSystem* system = FelicaSystemArray_push_new(*systems);
|
||||
furi_assert(reader != NULL);
|
||||
furi_assert(system != NULL);
|
||||
|
||||
memcpy(system->idm, reader->current_idm, 8);
|
||||
memcpy(system->pmm, reader->current_pmm, 8);
|
||||
system->code = system_code;
|
||||
|
||||
return system;
|
||||
}
|
||||
|
||||
bool felica_lite_can_read_without_mac(uint8_t* mc_r_restr, uint8_t block_number) {
|
||||
if(block_number > REG_LITE_BLOCK) {
|
||||
return true;
|
||||
}
|
||||
uint8_t byte = mc_r_restr[block_number < 8 ? 0 : 1];
|
||||
return ((byte >> (block_number % 8)) & 1) == 0;
|
||||
}
|
||||
|
||||
void felica_define_normal_block(FelicaService* service, uint16_t number, uint8_t* data) {
|
||||
FelicaBlock* block = FelicaBlockArray_safe_get(service->blocks, number);
|
||||
memcpy(block->data, data, FELICA_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
void felica_push_normal_block(FelicaService* service, uint8_t* data) {
|
||||
FelicaBlock* block = FelicaBlockArray_push_new(service->blocks);
|
||||
memcpy(block->data, data, FELICA_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
bool felica_lite_dump_data(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
FelicaReader* reader,
|
||||
FelicaData* data,
|
||||
FelicaSystem* system) {
|
||||
const uint8_t fixed_blocks[] = {
|
||||
SYS_CODE_LITE_BLOCK,
|
||||
DEVICE_ID_LITE_BLOCK,
|
||||
ID_LITE_BLOCK,
|
||||
RC_LITE_BLOCK,
|
||||
CARD_KEY_LITE_BLOCK,
|
||||
MAC_LITE_BLOCK,
|
||||
CARD_KEY_VER_LITE_BLOCK,
|
||||
MEM_CONFIG_LITE_BLOCK,
|
||||
};
|
||||
|
||||
uint8_t block_data[FELICA_BLOCK_SIZE * 4];
|
||||
|
||||
tx_rx->tx_bits =
|
||||
8 * felica_lite_prepare_unencrypted_read(tx_rx->tx_data, reader, true, fixed_blocks, 1);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange verifying Lite system code");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (SYS_C)");
|
||||
return false;
|
||||
}
|
||||
if(nfc_util_bytes2num(block_data, 2) != LITE_SYSTEM_CODE) {
|
||||
FURI_LOG_W(TAG, "Unexpected SYS_C value");
|
||||
return false;
|
||||
}
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, &fixed_blocks[1], 1);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading D_ID");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (D_ID)");
|
||||
return false;
|
||||
}
|
||||
if(memcmp(system->idm, block_data, 8) != 0 || memcmp(system->pmm, block_data + 8, 8) != 0) {
|
||||
FURI_LOG_W(TAG, "Mismatching values for D_ID");
|
||||
return false;
|
||||
}
|
||||
|
||||
FelicaLiteInfo* lite_info = &system->lite_info;
|
||||
lite_info->card_key_1 = NULL;
|
||||
lite_info->card_key_2 = NULL;
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, &fixed_blocks[2], 1);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading ID");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (ID)");
|
||||
return false;
|
||||
}
|
||||
lite_info->data_format_code = nfc_util_bytes2num(block_data + 8, 2);
|
||||
memcpy(lite_info->ID_value, block_data + 10, 6);
|
||||
FURI_LOG_I(TAG, "ID:");
|
||||
for(int i = 0; i < FELICA_BLOCK_SIZE; i++) {
|
||||
FURI_LOG_I(TAG, "%02X", block_data[i]);
|
||||
}
|
||||
|
||||
memset(block_data, 0, FELICA_BLOCK_SIZE);
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_write(
|
||||
tx_rx->tx_data, reader, &fixed_blocks[3], 1, block_data);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange writing random challenge");
|
||||
return false;
|
||||
}
|
||||
if(!felica_parse_unencrypted_write(tx_rx->rx_data, tx_rx->rx_bits / 8, reader)) {
|
||||
FURI_LOG_W(TAG, "Bad response to Write without Encryption (RC)");
|
||||
return false;
|
||||
}
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, &fixed_blocks[4], 2);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading CK and MAC");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE * 2) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (CK, MAC)");
|
||||
return false;
|
||||
}
|
||||
memcpy(lite_info->MAC, block_data + FELICA_BLOCK_SIZE, 8);
|
||||
FURI_LOG_I(TAG, "MAC:");
|
||||
for(int i = 0; i < FELICA_BLOCK_SIZE; i++) {
|
||||
FURI_LOG_I(TAG, "%02X", block_data[i + FELICA_BLOCK_SIZE]);
|
||||
}
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, &fixed_blocks[6], 2);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading CKV and MC");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE * 2) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (CKV, MC)");
|
||||
return false;
|
||||
}
|
||||
lite_info->card_key_version = nfc_util_bytes2num(block_data, 2);
|
||||
memcpy(lite_info->memory_config, block_data + FELICA_BLOCK_SIZE, FELICA_BLOCK_SIZE);
|
||||
|
||||
// Read SPAD and REG accordingly to MC
|
||||
uint8_t* mc_data = lite_info->memory_config;
|
||||
for(uint8_t block_number = 0; block_number <= REG_LITE_BLOCK; block_number++) {
|
||||
if(!felica_lite_can_read_without_mac(mc_data + 6, block_number)) {
|
||||
if(block_number < REG_LITE_BLOCK) {
|
||||
lite_info->S_PAD[block_number] = NULL;
|
||||
} else {
|
||||
lite_info->REG = NULL;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, &block_number, 1);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading blocks");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (block %d)", block_number);
|
||||
return false;
|
||||
}
|
||||
uint8_t* block = malloc(FELICA_BLOCK_SIZE);
|
||||
memcpy(block, block_data, FELICA_BLOCK_SIZE);
|
||||
if(block_number < REG_LITE_BLOCK) {
|
||||
lite_info->S_PAD[block_number] = block;
|
||||
} else {
|
||||
lite_info->REG = block;
|
||||
}
|
||||
}
|
||||
if(data->type == FelicaICTypeLiteS) {
|
||||
lite_info->is_lite_s = true;
|
||||
const uint8_t fixed_s_blocks[] = {
|
||||
CARD_KEY_LITE_BLOCK,
|
||||
MAC_A_LITE_BLOCK,
|
||||
WRITE_COUNT_LITE_BLOCK,
|
||||
CRC_CHECK_LITE_BLOCK,
|
||||
};
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, fixed_s_blocks, 2);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading ID with MAC_A");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE * 2) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (CK, MAC_A)");
|
||||
return false;
|
||||
}
|
||||
memcpy(lite_info->MAC_A, block_data + FELICA_BLOCK_SIZE, FELICA_BLOCK_SIZE);
|
||||
|
||||
tx_rx->tx_bits = 8 * felica_lite_prepare_unencrypted_read(
|
||||
tx_rx->tx_data, reader, true, &fixed_s_blocks[2], 2);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_W(TAG, "Bad exchange reading ID with MAC_A");
|
||||
return false;
|
||||
}
|
||||
if(felica_parse_unencrypted_read(
|
||||
tx_rx->rx_data, tx_rx->rx_bits / 8, reader, block_data, sizeof(block_data)) !=
|
||||
FELICA_BLOCK_SIZE * 2) {
|
||||
FURI_LOG_W(TAG, "Bad response to Read without Encryption (WC, CRC_CHECK)");
|
||||
return false;
|
||||
}
|
||||
lite_info->write_count = nfc_util_bytes2num(block_data, 3);
|
||||
lite_info->crc_valid = block_data[FELICA_BLOCK_SIZE] == 0x00;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool felica_std_request_system_code(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
FelicaReader* reader,
|
||||
FelicaSystemArray_t* systems) {
|
||||
tx_rx->tx_bits = 8 * felica_prepare_request_system_code(tx_rx->tx_data, reader);
|
||||
if(!furi_hal_nfc_tx_rx_full(tx_rx)) {
|
||||
FURI_LOG_E(TAG, "Bad exchange requesting system code");
|
||||
return false;
|
||||
}
|
||||
if(!felica_parse_request_system_code(tx_rx->rx_data, tx_rx->rx_bits / 8, reader, systems)) {
|
||||
FURI_LOG_E(TAG, "Bad response to Request System Code command");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool felica_read_card(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
FelicaData* data,
|
||||
uint8_t* polled_idm,
|
||||
uint8_t* polled_pmm) {
|
||||
furi_assert(tx_rx);
|
||||
furi_assert(polled_idm);
|
||||
furi_assert(polled_pmm);
|
||||
|
||||
bool card_read = false;
|
||||
do {
|
||||
FelicaReader reader;
|
||||
memcpy(reader.current_idm, polled_idm, 8);
|
||||
memcpy(reader.current_pmm, polled_pmm, 8);
|
||||
|
||||
FelicaSystemArray_init(data->systems);
|
||||
|
||||
if(data->type == FelicaICTypeLite || data->type == FelicaICTypeLiteS) {
|
||||
FURI_LOG_I(TAG, "Reading Felica Lite system");
|
||||
FelicaSystem* lite_system =
|
||||
felica_gen_monolithic_system_code(&reader, &(data->systems), LITE_SYSTEM_CODE);
|
||||
felica_lite_dump_data(tx_rx, &reader, data, lite_system);
|
||||
card_read = true;
|
||||
break;
|
||||
}
|
||||
FURI_LOG_I(TAG, "Reading Felica Standard system");
|
||||
} while(false);
|
||||
|
||||
return card_read;
|
||||
}
|
||||
|
||||
void felica_service_clear(FelicaService* service) {
|
||||
FelicaBlockArray_clear(service->blocks);
|
||||
}
|
||||
|
||||
void felica_lite_clear(FelicaLiteInfo* lite_info) {
|
||||
for(int i = 0; i < REG_LITE_BLOCK; i++) {
|
||||
uint8_t* block = lite_info->S_PAD[i];
|
||||
if(block != NULL) {
|
||||
free(block);
|
||||
}
|
||||
}
|
||||
|
||||
if(lite_info->REG != NULL) {
|
||||
free(lite_info->REG);
|
||||
}
|
||||
|
||||
if(lite_info->card_key_1 != NULL) {
|
||||
free(lite_info->card_key_1);
|
||||
}
|
||||
if(lite_info->card_key_2 != NULL) {
|
||||
free(lite_info->card_key_2);
|
||||
}
|
||||
}
|
||||
|
||||
void felica_node_clear(FelicaNode* node);
|
||||
|
||||
void felica_area_clear(FelicaArea* area) {
|
||||
for
|
||||
M_EACH(node, area->nodes, FelicaNodeArray_t) {
|
||||
felica_node_clear(node);
|
||||
}
|
||||
FelicaNodeArray_clear(area->nodes);
|
||||
}
|
||||
|
||||
void felica_node_clear(FelicaNode* node) {
|
||||
if(node->type == FelicaNodeTypeArea) {
|
||||
felica_area_clear(node->area);
|
||||
} else if(node->type == FelicaNodeTypeService) {
|
||||
felica_service_clear(node->service);
|
||||
}
|
||||
}
|
||||
|
||||
void felica_clear(FelicaData* data) {
|
||||
for
|
||||
M_EACH(system, data->systems, FelicaSystemArray_t) {
|
||||
if(system->code == LITE_SYSTEM_CODE) {
|
||||
felica_lite_clear(&system->lite_info);
|
||||
} else {
|
||||
felica_node_clear(&system->root);
|
||||
FelicaPublicServiceDict_clear(system->public_services);
|
||||
}
|
||||
}
|
||||
FelicaSystemArray_clear(data->systems);
|
||||
}
|
||||
@@ -1,320 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal_nfc.h>
|
||||
#include <m-array.h>
|
||||
#include <m-dict.h>
|
||||
|
||||
#define NFCF_F_SIG (13560000.0)
|
||||
#define MRT_T_SIG 302064.89 //ns, 256 * 16 / NFC_F_SIG * 1e9
|
||||
#define MRT_T_SIG_x4 1208259.56 //ns, MRT_T_SIG * (4 ** 1)
|
||||
#define MRT_T_SIG_x16 4833038.24 //ns, MRT_T_SIG * (4 ** 2)
|
||||
#define MRT_T_SIG_x64 19332152.96 //ns, MRT_T_SIG * (4 ** 2)
|
||||
|
||||
#define FELICA_PMM_MRT_BASE 2
|
||||
|
||||
#define FELICA_VARIABLE_MRT 0
|
||||
#define FELICA_FIXED_MRT 1
|
||||
#define FELICA_MUTUAL_AUTH_MRT 2
|
||||
#define FELICA_READ_MRT 3
|
||||
#define FELICA_WRITE_MRT 4
|
||||
#define FELICA_OTHER_MRT 5
|
||||
|
||||
#define FELICA_PMM_VARIABLE_MRT (FELICA_PMM_MRT_BASE + FELICA_VARIABLE_MRT)
|
||||
#define FELICA_PMM_FIXED_MRT (FELICA_PMM_MRT_BASE + FELICA_FIXED_MRT)
|
||||
#define FELICA_PMM_MUTUAL_AUTH_MRT (FELICA_PMM_MRT_BASE + FELICA_MUTUAL_AUTH_MRT)
|
||||
#define FELICA_PMM_READ_MRT (FELICA_PMM_MRT_BASE + FELICA_READ_MRT)
|
||||
#define FELICA_PMM_WRITE_MRT (FELICA_PMM_MRT_BASE + FELICA_WRITE_MRT)
|
||||
#define FELICA_PMM_OTHER_MRT (FELICA_PMM_MRT_BASE + FELICA_OTHER_MRT)
|
||||
|
||||
#define FELICA_BLOCK_SIZE 16
|
||||
|
||||
#define CYBERNET_SYSTEM_CODE 0x0003
|
||||
#define NDEF_SYSTEM_CODE 0x12fc
|
||||
#define HCE_F_SYSTEM_CODE 0x4000
|
||||
#define OCTOPUS_SYSTEM_CODE 0x8008
|
||||
#define IRUCA_SYSTEM_CODE 0x80de
|
||||
#define EDY_SYSTEM_CODE 0x811d
|
||||
#define PASPY_SYSTEM_CODE 0x8592
|
||||
#define BLACKBOARD_SYSTEM_CODE 0x8620
|
||||
#define SAPICA_SYSTEM_CODE 0x865e
|
||||
#define SUICA_SYSTEM_CODE 0x86a7
|
||||
#define LITE_SYSTEM_CODE 0x88b4
|
||||
#define RYUTO_SYSTEM_CODE 0x8b5d
|
||||
#define OKICA_SYSTEM_CODE 0x8fc1
|
||||
#define SECURE_ID_SYSTEM_CODE 0x957a
|
||||
#define COMMON_AREA_SYSTEM_CODE 0xfe00
|
||||
#define PLUG_SYSTEM_CODE 0xfee1
|
||||
|
||||
#define REG_LITE_BLOCK 0x0e
|
||||
#define RC_LITE_BLOCK 0x80
|
||||
#define MAC_LITE_BLOCK 0x81
|
||||
#define ID_LITE_BLOCK 0x82
|
||||
#define DEVICE_ID_LITE_BLOCK 0x83
|
||||
#define SERVICE_CODE_LITE_BLOCK 0x84
|
||||
#define SYS_CODE_LITE_BLOCK 0x85
|
||||
#define CARD_KEY_VER_LITE_BLOCK 0x86
|
||||
#define CARD_KEY_LITE_BLOCK 0x87
|
||||
#define MEM_CONFIG_LITE_BLOCK 0x88
|
||||
#define WRITE_COUNT_LITE_BLOCK 0x90
|
||||
#define MAC_A_LITE_BLOCK 0x91
|
||||
#define STATE_LITE_BLOCK 0x92
|
||||
#define CRC_CHECK_LITE_BLOCK 0xA0
|
||||
|
||||
#define IS_2_BYTE_BLOCK_LIST_ELEMENT 0x80
|
||||
|
||||
#define FELICA_UNENCRYPTED_READ_CMD 0x06
|
||||
#define FELICA_UNENCRYPTED_WRITE_CMD 0x08
|
||||
#define FELICA_SEARCH_SERVICE_CODE_CMD 0x0a
|
||||
#define FELICA_REQUEST_SYSTEM_CODE_CMD 0x0c
|
||||
|
||||
#define FELICA_UNENCRYPTED_READ_RES 0x07
|
||||
#define FELICA_UNENCRYPTED_WRITE_RES 0x09
|
||||
#define FELICA_SEARCH_SERVICE_CODE_RES 0x0b
|
||||
#define FELICA_REQUEST_SYSTEM_CODE_RES 0x0d
|
||||
|
||||
typedef enum {
|
||||
FelicaICTypeRC_SA24_10K, // RC-SA24/1x
|
||||
FelicaICTypeRC_SA24_6K, // RC-SA24/1x1
|
||||
FelicaICTypeSD2_6K, // RC-SA21/2x1
|
||||
FelicaICTypeSD2_4K, // RC-SA21/2
|
||||
FelicaICTypeSD2WithDES, // RC-SA20/1, RC-SA20/2
|
||||
FelicaICTypeRC_SA08, // Certifications exist, prototype?
|
||||
FelicaICTypeSD1, // RC-SA01
|
||||
FelicaICTypeSD1WithDES, // RC-SA00
|
||||
FelicaICTypeFRAM_4K, // RC-S962
|
||||
FelicaICTypeFRAM_9K, // RC-S960
|
||||
FelicaICTypeEMV_36K, // RC-S954
|
||||
FelicaICTypeEMV_16K, // RC-S953
|
||||
FelicaICTypeEMV_32K, // RC-S952
|
||||
FelicaICType576B, // RC-S919
|
||||
FelicaICType4K, // RC-S915
|
||||
FelicaICType2K, // RC-S830 series cards, chip name unknown,
|
||||
FelicaICTypeMobileIC_V4_1,
|
||||
FelicaICTypeMobileIC_V4,
|
||||
FelicaICTypeMobileIC_V3,
|
||||
FelicaICTypeMobileIC_V2,
|
||||
FelicaICTypeMobileIC_V1,
|
||||
FelicaICTypeLite, // RC-S965
|
||||
FelicaICTypeLiteS, // RC-S966
|
||||
FelicaICTypeLink, // RC-S967,
|
||||
FelicaICTypePlug, // RC-S926
|
||||
FelicaICTypeSuica, // https://www.tuv-nederland.nl/assets/files/cerfiticaten/2019/07/cr-nscib-cc-10-30076-cr.pdf
|
||||
} FelicaICType;
|
||||
|
||||
typedef enum {
|
||||
FelicaServiceTypeRandom = (0b0010 << 2),
|
||||
FelicaServiceTypeCyclic = (0b0011 << 2),
|
||||
FelicaServiceTypePurse = (0b010 << 3),
|
||||
} FelicaServiceType;
|
||||
typedef enum {
|
||||
FelicaServiceAttributeAuthRW = 0b00,
|
||||
FelicaServiceAttributeUnauthRW = 0b01,
|
||||
FelicaServiceAttributeAuthRO = 0b10,
|
||||
FelicaServiceAttributeUnauthRO = 0b11,
|
||||
|
||||
FelicaServiceAttributeAuthDirectAccess = 0b000,
|
||||
FelicaServiceAttributeUnauthDirectAccess = 0b001,
|
||||
FelicaServiceAttributeAuthCashbackDecrement = 0b010,
|
||||
FelicaServiceAttributeUnauthCashbackDecrement = 0b011,
|
||||
FelicaServiceAttributeAuthDecrement = 0b100,
|
||||
FelicaServiceAttributeUnauthDecrement = 0b101,
|
||||
FelicaServiceAttributeAuthPurseRO = 0b110,
|
||||
FelicaServiceAttributeUnauthPurseRO = 0b111,
|
||||
} FelicaServiceAttribute;
|
||||
|
||||
DICT_SET_DEF(
|
||||
FelicaServiceAttributeList,
|
||||
FelicaServiceAttribute,
|
||||
M_ENUM_OPLIST(FelicaServiceAttribute, FelicaServiceAttributeAuthRW))
|
||||
|
||||
typedef struct {
|
||||
uint8_t data[FELICA_BLOCK_SIZE];
|
||||
} FelicaBlock;
|
||||
|
||||
ARRAY_DEF(FelicaBlockArray, FelicaBlock, M_POD_OPLIST)
|
||||
#define M_OPL_FelicaBlockArray_t() ARRAY_OPLIST(FelicaBlockArray, M_POD_OPLIST)
|
||||
|
||||
typedef struct {
|
||||
uint16_t number;
|
||||
FelicaServiceAttributeList_t access_control_list; // accounts for overlap services
|
||||
bool is_extended_overlap; // We don't know much about this currently. will always be false
|
||||
union {
|
||||
// TODO change this to use FelicaBlockArray_t
|
||||
FelicaBlockArray_t blocks;
|
||||
struct {
|
||||
uint16_t overlap_target;
|
||||
uint8_t block_start;
|
||||
uint8_t block_count;
|
||||
};
|
||||
};
|
||||
} FelicaService;
|
||||
|
||||
typedef enum {
|
||||
FelicaNodeTypeArea,
|
||||
FelicaNodeTypeService,
|
||||
} FelicaNodeType;
|
||||
|
||||
struct _FelicaArea_t;
|
||||
typedef struct _FelicaArea_t FelicaArea;
|
||||
|
||||
struct _FelicaNode_s;
|
||||
typedef struct _FelicaNode_s FelicaNode;
|
||||
|
||||
struct _FelicaNode_s {
|
||||
/** Node type. */
|
||||
FelicaNodeType type;
|
||||
/** Borrowed pointer to its parent node. */
|
||||
FelicaNode* parent;
|
||||
union {
|
||||
/** (Area/dir type only) The area struct. */
|
||||
FelicaArea* area;
|
||||
/** (Service/file type only) The service struct. */
|
||||
FelicaService* service;
|
||||
};
|
||||
};
|
||||
|
||||
// TODO properly remove this
|
||||
//ARRAY_DEF(FelicaNodeList, FelicaNode*, M_PTR_OPLIST)
|
||||
ARRAY_DEF(FelicaNodeArray, FelicaNode, M_POD_OPLIST)
|
||||
#define M_OPL_FelicaNodeArray_t() ARRAY_OPLIST(FelicaNodeArray, M_POD_OPLIST)
|
||||
|
||||
ARRAY_DEF(FelicaNodeRefArray, FelicaNode*, M_PTR_OPLIST)
|
||||
#define M_OPL_FelicaNodeRefArray_t() ARRAY_OPLIST(FelicaNodeRefArray, M_PTR_OPLIST)
|
||||
|
||||
// { service_number: service_ptr_in_tree }
|
||||
DICT_DEF2(FelicaPublicServiceDict, uint16_t, M_DEFAULT_OPLIST, FelicaService*, M_PTR_OPLIST)
|
||||
#define M_OPL_FelicaPublicServiceDict_t() \
|
||||
DICT_OPLIST(FelicaPublicServiceDict, M_DEFAULT_OPLIST, M_PTR_OPLIST)
|
||||
|
||||
struct _FelicaArea_t {
|
||||
uint16_t number;
|
||||
bool can_create_subareas;
|
||||
uint16_t end_service_code;
|
||||
|
||||
FelicaNodeArray_t nodes;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint8_t* S_PAD[14];
|
||||
uint8_t* REG;
|
||||
// MACs of all zero bytes (read from CK)
|
||||
uint8_t MAC[8];
|
||||
uint16_t data_format_code;
|
||||
uint8_t ID_value[6];
|
||||
uint8_t* card_key_1;
|
||||
uint8_t* card_key_2;
|
||||
uint16_t card_key_version;
|
||||
uint8_t memory_config[FELICA_BLOCK_SIZE];
|
||||
|
||||
bool is_lite_s;
|
||||
// Lite-S only
|
||||
uint8_t MAC_A[8];
|
||||
uint32_t write_count;
|
||||
bool crc_valid;
|
||||
} FelicaLiteInfo;
|
||||
|
||||
typedef struct _FelicaSystem_t {
|
||||
/** FeliCa system index. */
|
||||
uint8_t number;
|
||||
/** If the system belongs to a FeliCa Lite (and be its only system). */
|
||||
bool is_lite;
|
||||
/** FeliCa system code. */
|
||||
uint16_t code;
|
||||
/** System IDm with system index bitfield properly set. */
|
||||
uint8_t idm[8];
|
||||
/** Cached card PMm. */
|
||||
uint8_t pmm[8];
|
||||
|
||||
union {
|
||||
/** (For FeliCa Lite only) Card content. */
|
||||
FelicaLiteInfo lite_info;
|
||||
struct {
|
||||
/** (For FeliCa Standard only) The root of the raw filesystem tree. */
|
||||
FelicaNode root;
|
||||
/** (For FeliCa Standard only) Shortcut for all publicly accessible services for quick
|
||||
* access by card parsers. */
|
||||
FelicaPublicServiceDict_t public_services;
|
||||
};
|
||||
};
|
||||
} FelicaSystem;
|
||||
|
||||
// TODO properly remove this
|
||||
//ARRAY_DEF(FelicaSystemList, FelicaSystem*, M_PTR_OPLIST)
|
||||
ARRAY_DEF(FelicaSystemArray, FelicaSystem, M_POD_OPLIST)
|
||||
#define M_OPL_FelicaSystemArray_t() ARRAY_OPLIST(FelicaSystemArray, M_POD_OPLIST)
|
||||
|
||||
typedef struct {
|
||||
FelicaICType type;
|
||||
uint8_t subtype;
|
||||
FelicaSystemArray_t systems;
|
||||
} FelicaData;
|
||||
|
||||
typedef struct {
|
||||
uint8_t current_idm[8];
|
||||
uint8_t current_pmm[8];
|
||||
|
||||
uint8_t status_flags[2];
|
||||
} FelicaReader;
|
||||
|
||||
bool felica_check_ic_type(uint8_t* PMm);
|
||||
FelicaICType felica_get_ic_type(uint8_t* PMm);
|
||||
|
||||
uint8_t felica_prepare_unencrypted_read(
|
||||
uint8_t* dest,
|
||||
const FelicaReader* reader,
|
||||
const uint16_t* service_code_list,
|
||||
uint8_t service_count,
|
||||
const uint32_t* block_list,
|
||||
uint8_t block_count);
|
||||
uint8_t felica_lite_prepare_unencrypted_read(
|
||||
uint8_t* dest,
|
||||
const FelicaReader* reader,
|
||||
bool is_read_only,
|
||||
const uint8_t* block_list,
|
||||
uint8_t block_count);
|
||||
uint16_t felica_parse_unencrypted_read(
|
||||
uint8_t* buf,
|
||||
uint8_t len,
|
||||
FelicaReader* reader,
|
||||
uint8_t* out,
|
||||
uint16_t out_len);
|
||||
|
||||
uint8_t felica_prepare_unencrypted_write(
|
||||
uint8_t* dest,
|
||||
FelicaReader* reader,
|
||||
const uint16_t* service_code_list,
|
||||
uint8_t service_count,
|
||||
const uint32_t* block_list,
|
||||
uint8_t block_count,
|
||||
const uint8_t* block_data);
|
||||
uint8_t felica_lite_prepare_unencrypted_write(
|
||||
uint8_t* dest,
|
||||
const FelicaReader* reader,
|
||||
const uint8_t* block_list,
|
||||
uint8_t block_count,
|
||||
const uint8_t* block_data);
|
||||
bool felica_parse_unencrypted_write(uint8_t* buf, uint8_t len, FelicaReader* reader);
|
||||
|
||||
void felica_define_normal_block(FelicaService* service, uint16_t number, uint8_t* data);
|
||||
void felica_push_normal_block(FelicaService* service, uint8_t* data);
|
||||
|
||||
/** Dump a FeliCa Lite or Lite-S tag.
|
||||
*
|
||||
* @param tx_rx NFC context.
|
||||
* @param reader FeliCa reader context.
|
||||
* @param data Output data object.
|
||||
* @param system FeliCa system description.
|
||||
* @return true if successful.
|
||||
*/
|
||||
bool felica_lite_dump_data(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
FelicaReader* reader,
|
||||
FelicaData* data,
|
||||
FelicaSystem* system);
|
||||
|
||||
bool felica_read_card(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
FelicaData* data,
|
||||
uint8_t* polled_idm,
|
||||
uint8_t* polled_pmm);
|
||||
void felica_clear(FelicaData* data);
|
||||
@@ -1,67 +0,0 @@
|
||||
#include "./felica.h"
|
||||
#include <furi.h>
|
||||
|
||||
static const uint32_t TIME_CONSTANT_US = 302;
|
||||
|
||||
// TODO move this to felica.c
|
||||
uint_least32_t felica_estimate_timing_us(uint_least8_t timing, uint_least8_t units) {
|
||||
uint_least32_t base_cost_factor = 1 + (timing & 0x7);
|
||||
uint_least32_t unit_cost_factor = 1 + ((timing >> 3) & 0x7);
|
||||
uint_least32_t scale = 1 << ((timing >> 6) * 2);
|
||||
return TIME_CONSTANT_US * scale * (base_cost_factor + unit_cost_factor * units);
|
||||
}
|
||||
|
||||
bool felica_lite_is_issued(FelicaLiteInfo* lite_info) {
|
||||
// System blocks aren't writable?
|
||||
if(lite_info->memory_config[2] == 0x00) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// MC is not writable?
|
||||
if(lite_info->memory_config[1] & 0x80) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
FuriString* felica_get_system_name(FelicaSystem* system) {
|
||||
uint16_t code = system->code;
|
||||
|
||||
const char* prefix;
|
||||
if(code == SUICA_SYSTEM_CODE) {
|
||||
prefix = "SuiCa";
|
||||
} else if(code == NDEF_SYSTEM_CODE) {
|
||||
prefix = "NDEF";
|
||||
} else if(code == HCE_F_SYSTEM_CODE) {
|
||||
prefix = "HCE-F";
|
||||
} else if(code == OCTOPUS_SYSTEM_CODE) {
|
||||
prefix = "Octopus";
|
||||
} else if(code == EDY_SYSTEM_CODE) {
|
||||
prefix = "Edy";
|
||||
} else if(code == PASPY_SYSTEM_CODE) {
|
||||
prefix = "PASPY";
|
||||
} else if(code == BLACKBOARD_SYSTEM_CODE) {
|
||||
prefix = "Blackboard";
|
||||
} else if(code == SAPICA_SYSTEM_CODE) {
|
||||
prefix = "SAPICA";
|
||||
} else if(code == LITE_SYSTEM_CODE) {
|
||||
prefix = "FeliCa Lite";
|
||||
} else if(code == RYUTO_SYSTEM_CODE) {
|
||||
prefix = "Ryuto";
|
||||
} else if(code == OKICA_SYSTEM_CODE) {
|
||||
prefix = "OKICA";
|
||||
} else if(code == SECURE_ID_SYSTEM_CODE) {
|
||||
prefix = "FeliCa Secure ID";
|
||||
} else if(code == IRUCA_SYSTEM_CODE) {
|
||||
prefix = "IruCa";
|
||||
} else if(code == COMMON_AREA_SYSTEM_CODE) {
|
||||
prefix = "Common Area";
|
||||
} else if(code == PLUG_SYSTEM_CODE) {
|
||||
prefix = "FeliCa Plug";
|
||||
} else {
|
||||
return furi_string_alloc_printf("System %04X", code);
|
||||
}
|
||||
|
||||
return furi_string_alloc_printf("%s (%04X)", prefix, code);
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
#include "./felica.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint_least32_t felica_estimate_timing_us(uint_least8_t timing, uint_least8_t units);
|
||||
bool felica_lite_is_issued(FelicaLiteInfo* lite_info);
|
||||
FuriString* felica_get_system_name(FelicaSystem* system);
|
||||
FuriString* felica_get_service_name(FelicaService* service);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -374,10 +374,7 @@ bool mf_classic_is_value_block(MfClassicData* data, uint8_t block_num) {
|
||||
data, block_num, MfClassicKeyB, MfClassicActionDataDec));
|
||||
}
|
||||
|
||||
bool mf_classic_check_card_type(FuriHalNfcADevData* data) {
|
||||
uint8_t ATQA0 = data->atqa[0];
|
||||
uint8_t ATQA1 = data->atqa[1];
|
||||
uint8_t SAK = data->sak;
|
||||
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
if((ATQA0 == 0x44 || ATQA0 == 0x04) &&
|
||||
(SAK == 0x08 || SAK == 0x88 || SAK == 0x09 || SAK == 0x89)) {
|
||||
return true;
|
||||
@@ -391,10 +388,7 @@ bool mf_classic_check_card_type(FuriHalNfcADevData* data) {
|
||||
}
|
||||
}
|
||||
|
||||
MfClassicType mf_classic_get_classic_type(FuriHalNfcADevData* data) {
|
||||
uint8_t ATQA0 = data->atqa[0];
|
||||
uint8_t ATQA1 = data->atqa[1];
|
||||
uint8_t SAK = data->sak;
|
||||
MfClassicType mf_classic_get_classic_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
if((ATQA0 == 0x44 || ATQA0 == 0x04)) {
|
||||
if((SAK == 0x08 || SAK == 0x88)) {
|
||||
return MfClassicType1k;
|
||||
|
||||
@@ -98,9 +98,9 @@ typedef struct {
|
||||
|
||||
const char* mf_classic_get_type_str(MfClassicType type);
|
||||
|
||||
bool mf_classic_check_card_type(FuriHalNfcADevData* data);
|
||||
bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
MfClassicType mf_classic_get_classic_type(FuriHalNfcADevData* data);
|
||||
MfClassicType mf_classic_get_classic_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
uint8_t mf_classic_get_total_sectors_num(MfClassicType type);
|
||||
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
#include "mifare_common.h"
|
||||
#include "furi_hal_nfc.h"
|
||||
|
||||
MifareType mifare_common_get_type(FuriHalNfcADevData* data) {
|
||||
uint8_t ATQA0 = data->atqa[0];
|
||||
uint8_t ATQA1 = data->atqa[1];
|
||||
uint8_t SAK = data->sak;
|
||||
MifareType mifare_common_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
MifareType type = MifareTypeUnknown;
|
||||
|
||||
if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "furi_hal_nfc.h"
|
||||
|
||||
typedef enum {
|
||||
MifareTypeUnknown,
|
||||
@@ -10,4 +9,4 @@ typedef enum {
|
||||
MifareTypeDesfire,
|
||||
} MifareType;
|
||||
|
||||
MifareType mifare_common_get_type(FuriHalNfcADevData* data);
|
||||
MifareType mifare_common_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
@@ -258,11 +258,7 @@ void mf_df_cat_file(MifareDesfireFile* file, FuriString* out) {
|
||||
}
|
||||
}
|
||||
|
||||
bool mf_df_check_card_type(FuriHalNfcADevData* data) {
|
||||
uint8_t ATQA0 = data->atqa[0];
|
||||
uint8_t ATQA1 = data->atqa[1];
|
||||
uint8_t SAK = data->sak;
|
||||
|
||||
bool mf_df_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
return ATQA0 == 0x44 && ATQA1 == 0x03 && SAK == 0x20;
|
||||
}
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ void mf_df_cat_application_info(MifareDesfireApplication* app, FuriString* out);
|
||||
void mf_df_cat_application(MifareDesfireApplication* app, FuriString* out);
|
||||
void mf_df_cat_file(MifareDesfireFile* file, FuriString* out);
|
||||
|
||||
bool mf_df_check_card_type(FuriHalNfcADevData* data);
|
||||
bool mf_df_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
MifareDesfireApplication* mf_df_get_application(MifareDesfireData* data, const uint8_t (*aid)[3]);
|
||||
MifareDesfireFile* mf_df_get_file(MifareDesfireApplication* app, uint8_t id);
|
||||
|
||||
@@ -33,12 +33,11 @@ uint32_t mf_ul_pwdgen_amiibo(FuriHalNfcDevData* data) {
|
||||
return pwd;
|
||||
}
|
||||
|
||||
bool mf_ul_check_card_type(FuriHalNfcADevData* data) {
|
||||
uint8_t ATQA0 = data->atqa[0];
|
||||
uint8_t ATQA1 = data->atqa[1];
|
||||
uint8_t SAK = data->sak;
|
||||
|
||||
return ((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00));
|
||||
bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
|
||||
if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void mf_ul_reset(MfUltralightData* data) {
|
||||
|
||||
@@ -207,7 +207,7 @@ typedef struct {
|
||||
|
||||
void mf_ul_reset(MfUltralightData* data);
|
||||
|
||||
bool mf_ul_check_card_type(FuriHalNfcADevData* data);
|
||||
bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK);
|
||||
|
||||
bool mf_ultralight_read_version(
|
||||
FuriHalNfcTxRxContext* tx_rx,
|
||||
|
||||
Reference in New Issue
Block a user