Loading...
Searching...
No Matches
felica.h
1#pragma once
2
5#include <mbedtls/include/mbedtls/des.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#define FELICA_IDM_SIZE (8U)
12#define FELICA_PMM_SIZE (8U)
13#define FELICA_DATA_BLOCK_SIZE (16U)
14
15#define FELICA_CMD_READ_WITHOUT_ENCRYPTION (0x06U)
16#define FELICA_CMD_WRITE_WITHOUT_ENCRYPTION (0x08U)
17
18#define FELICA_SERVICE_RW_ACCESS (0x0009U)
19#define FELICA_SERVICE_RO_ACCESS (0x000BU)
20
21#define FELICA_BLOCKS_TOTAL_COUNT (28U)
22#define FELICA_BLOCK_INDEX_REG (0x0EU)
23#define FELICA_BLOCK_INDEX_RC (0x80U)
24#define FELICA_BLOCK_INDEX_MAC (0x81U)
25#define FELICA_BLOCK_INDEX_ID (0x82U)
26#define FELICA_BLOCK_INDEX_D_ID (0x83U)
27#define FELICA_BLOCK_INDEX_SER_C (0x84U)
28#define FELICA_BLOCK_INDEX_SYS_C (0x85U)
29#define FELICA_BLOCK_INDEX_CKV (0x86U)
30#define FELICA_BLOCK_INDEX_CK (0x87U)
31#define FELICA_BLOCK_INDEX_MC (0x88U)
32#define FELICA_BLOCK_INDEX_WCNT (0x90U)
33#define FELICA_BLOCK_INDEX_MAC_A (0x91U)
34#define FELICA_BLOCK_INDEX_STATE (0x92U)
35#define FELICA_BLOCK_INDEX_CRC_CHECK (0xA0U)
36
37#define FELICA_GUARD_TIME_US (20000U)
38#define FELICA_FDT_POLL_FC (10000U)
39#define FELICA_POLL_POLL_MIN_US (1280U)
40
41#define FELICA_FDT_LISTEN_FC (1172)
42
43#define FELICA_SYSTEM_CODE_CODE (0xFFFFU)
44#define FELICA_TIME_SLOT_1 (0x00U)
45#define FELICA_TIME_SLOT_2 (0x01U)
46#define FELICA_TIME_SLOT_4 (0x03U)
47#define FELICA_TIME_SLOT_8 (0x07U)
48#define FELICA_TIME_SLOT_16 (0x0FU)
49
51typedef enum {
52 FelicaErrorNone,
53 FelicaErrorNotPresent,
54 FelicaErrorColResFailed,
55 FelicaErrorBufferOverflow,
56 FelicaErrorCommunication,
57 FelicaErrorFieldOff,
58 FelicaErrorWrongCrc,
59 FelicaErrorProtocol,
60 FelicaErrorTimeout,
61} FelicaError;
62
63typedef struct {
64 uint8_t data[FELICA_DATA_BLOCK_SIZE];
66
68typedef struct {
69 uint8_t data[FELICA_DATA_BLOCK_SIZE];
71
75typedef struct {
76 bool internal : 1;
77 bool external : 1;
79
88
92typedef struct {
93 uint8_t data[FELICA_DATA_BLOCK_SIZE];
95
104
106typedef struct {
107 uint8_t data[FELICA_IDM_SIZE];
108} FelicaIDm;
109
111typedef struct {
112 uint8_t data[FELICA_PMM_SIZE];
113} FelicaPMm;
114
117typedef struct {
118 uint8_t SF1;
119 uint8_t SF2;
120 uint8_t data[FELICA_DATA_BLOCK_SIZE];
122
124typedef struct {
125 FelicaBlock spad[14];
126 FelicaBlock reg;
127 FelicaBlock rc;
128 FelicaBlock mac;
129 FelicaBlock id;
130 FelicaBlock d_id;
131 FelicaBlock ser_c;
132 FelicaBlock sys_c;
133 FelicaBlock ckv;
134 FelicaBlock ck;
135 FelicaBlock mc;
136 FelicaBlock wcnt;
137 FelicaBlock mac_a;
138 FelicaBlock state;
139 FelicaBlock crc_check;
141
143typedef union {
145 uint8_t dump[sizeof(FelicaFileSystem)];
147
149typedef struct {
150 FelicaIDm idm;
151 FelicaPMm pmm;
152 uint8_t blocks_total;
153 uint8_t blocks_read;
154 FelicaFSUnion data;
155} FelicaData;
156
157#pragma pack(push, 1)
158typedef struct {
159 uint8_t code;
160 FelicaIDm idm;
161 uint8_t service_num;
162 uint16_t service_code;
163 uint8_t block_count;
165#pragma pack(pop)
166
167typedef struct {
168 uint8_t length;
169 uint8_t response_code;
170 FelicaIDm idm;
171 uint8_t SF1;
172 uint8_t SF2;
174
175typedef struct {
176 uint8_t service_code : 4;
177 uint8_t access_mode : 3;
178 uint8_t length : 1;
179 uint8_t block_number;
181
182typedef struct {
183 uint8_t length;
184 uint8_t response_code;
185 FelicaIDm idm;
186 uint8_t SF1;
187 uint8_t SF2;
188 uint8_t block_count;
189 uint8_t data[];
191
192typedef struct {
194 uint8_t block_count;
195 uint8_t data[];
197
199
201
202extern const NfcDeviceBase nfc_device_felica;
203
204FelicaData* felica_alloc(void);
205
206void felica_free(FelicaData* data);
207
208void felica_reset(FelicaData* data);
209
210void felica_copy(FelicaData* data, const FelicaData* other);
211
212bool felica_verify(FelicaData* data, const FuriString* device_type);
213
214bool felica_load(FelicaData* data, FlipperFormat* ff, uint32_t version);
215
216bool felica_save(const FelicaData* data, FlipperFormat* ff);
217
218bool felica_is_equal(const FelicaData* data, const FelicaData* other);
219
220const char* felica_get_device_name(const FelicaData* data, NfcDeviceNameType name_type);
221
222const uint8_t* felica_get_uid(const FelicaData* data, size_t* uid_len);
223
224bool felica_set_uid(FelicaData* data, const uint8_t* uid, size_t uid_len);
225
226FelicaData* felica_get_base_data(const FelicaData* data);
227
228void felica_calculate_session_key(
229 mbedtls_des3_context* ctx,
230 const uint8_t* ck,
231 const uint8_t* rc,
232 uint8_t* out);
233
234bool felica_check_mac(
235 mbedtls_des3_context* ctx,
236 const uint8_t* session_key,
237 const uint8_t* rc,
238 const uint8_t* blocks,
239 const uint8_t block_count,
240 uint8_t* data);
241
242void felica_calculate_mac_read(
243 mbedtls_des3_context* ctx,
244 const uint8_t* session_key,
245 const uint8_t* rc,
246 const uint8_t* blocks,
247 const uint8_t block_count,
248 const uint8_t* data,
249 uint8_t* mac);
250
251void felica_calculate_mac_write(
252 mbedtls_des3_context* ctx,
253 const uint8_t* session_key,
254 const uint8_t* rc,
255 const uint8_t* wcnt,
256 const uint8_t* data,
257 uint8_t* mac);
258#ifdef __cplusplus
259}
260#endif
Bit Buffer.
NfcDeviceNameType
Verbosity level of the displayed NFC device name.
Definition nfc_device_base.h:14
Abstract interface definitions for the NFC device system.
Struct which controls the process of authentication and can be passed as a parameter to the applicati...
Definition felica.h:82
FelicaAuthenticationStatus auth_status
Authentication status.
Definition felica.h:86
bool skip_auth
By default it is true, so auth is skipped.
Definition felica.h:83
FelicaCardKey card_key
User must fill this field with known card key in order to pass auth.
Definition felica.h:85
Structure used to hold authentication related fields.
Definition felica.h:99
mbedtls_des3_context des_context
Context for mbedtls des functions.
Definition felica.h:100
FelicaAuthenticationContext context
Public auth context provided to upper levels.
Definition felica.h:102
FelicaSessionKey session_key
Calculated session key.
Definition felica.h:101
In Felica there two types of auth.
Definition felica.h:75
Definition felica.h:63
Felica block with status flags indicating last operation with it.
Definition felica.h:117
uint8_t SF1
Status flag 1, equals to 0 when success.
Definition felica.h:118
uint8_t SF2
Status flag 2, equals to 0 when success.
Definition felica.h:119
Definition felica.h:175
Separate type for card key block.
Definition felica.h:68
Definition felica.h:158
Definition felica.h:167
Structure used to store Felica data and additional values about reading.
Definition felica.h:149
Felica filesystem structure.
Definition felica.h:124
Felica ID block.
Definition felica.h:106
Definition felica.h:192
Felica PMm block.
Definition felica.h:111
Definition felica.h:182
Stucture for holding Felica session key which is calculated from rc and ck.
Definition felica.h:92
Definition flipper_format.c:12
Definition string.c:4
Generic NFC device interface.
Definition nfc_device_base_i.h:142
Union which represents filesystem in junction with plain data dump.
Definition felica.h:143