picopass.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "picopass.h"
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <stdlib.h>
  6. #include <st25r3916.h>
  7. #include <rfal_analogConfig.h>
  8. #include <rfal_rf.h>
  9. #include <rfal_nfc.h>
  10. #include <storage/storage.h>
  11. #include <lib/toolbox/path.h>
  12. #define TAG "PicoPass"
  13. #define PICOPASS_APP_ICLASS_KEY_PATH "/any/picopass/iclass_key.bin"
  14. #define PICOPASS_APP_ICLASS_DECRYPT_KEY_PATH "/any/picopass/iclass_decryptionkey.bin"
  15. typedef enum {
  16. EventTypeTick,
  17. EventTypeKey,
  18. } EventType;
  19. typedef struct {
  20. EventType type;
  21. InputEvent input;
  22. } PluginEvent;
  23. typedef struct {
  24. bool valid;
  25. uint8_t bitLength;
  26. uint8_t FacilityCode;
  27. uint16_t CardNumber;
  28. } WiegandRecord;
  29. typedef struct {
  30. bool biometrics;
  31. uint8_t encryption;
  32. uint8_t credential[8];
  33. uint8_t pin0[8];
  34. uint8_t pin1[8];
  35. WiegandRecord record;
  36. } PACS;
  37. enum State { INIT, KEYS_MISSING, READY, RESULT };
  38. typedef struct {
  39. enum State state;
  40. PACS pacs;
  41. } PluginState;
  42. uint8_t iclass_key[8] = {0}; // NB: not the permuted version
  43. uint8_t iclass_decryptionkey[16] = {0};
  44. ApplicationArea AA1;
  45. static bool picopass_load_keys() {
  46. Storage* storage = furi_record_open("storage");
  47. File* file = storage_file_alloc(storage);
  48. if(!storage_file_open(file, PICOPASS_APP_ICLASS_KEY_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  49. FURI_LOG_E(TAG, "Unable to open iClass key");
  50. storage_file_free(file);
  51. furi_record_close("storage");
  52. return false;
  53. };
  54. storage_file_read(file, iclass_key, sizeof(iclass_key));
  55. storage_file_close(file);
  56. FURI_LOG_D(TAG, "iClass key loaded");
  57. if(!storage_file_open(
  58. file, PICOPASS_APP_ICLASS_DECRYPT_KEY_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  59. FURI_LOG_E(TAG, "Unable to open iClass decryption key");
  60. storage_file_free(file);
  61. furi_record_close("storage");
  62. return false;
  63. };
  64. storage_file_read(file, iclass_decryptionkey, sizeof(iclass_decryptionkey));
  65. storage_file_close(file);
  66. FURI_LOG_D(TAG, "iClass decryption key loaded");
  67. storage_file_free(file);
  68. furi_record_close("storage");
  69. return true;
  70. }
  71. static void render_callback(Canvas* const canvas, void* ctx) {
  72. const PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
  73. if(plugin_state == NULL) {
  74. return;
  75. }
  76. // border around the edge of the screen
  77. canvas_draw_frame(canvas, 0, 0, 128, 64);
  78. canvas_set_font(canvas, FontPrimary);
  79. if(plugin_state->state == INIT) {
  80. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignTop, "Loading...");
  81. } else if(plugin_state->state == KEYS_MISSING) {
  82. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignTop, "Keys missing");
  83. } else if(plugin_state->state == READY) {
  84. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignTop, "Push center to scan");
  85. } else if(plugin_state->state == RESULT) {
  86. char raw_credential[25] = {0};
  87. sprintf(
  88. raw_credential,
  89. "%02x %02x %02x %02x %02x %02x %02x %02x",
  90. plugin_state->pacs.credential[0],
  91. plugin_state->pacs.credential[1],
  92. plugin_state->pacs.credential[2],
  93. plugin_state->pacs.credential[3],
  94. plugin_state->pacs.credential[4],
  95. plugin_state->pacs.credential[5],
  96. plugin_state->pacs.credential[6],
  97. plugin_state->pacs.credential[7]);
  98. canvas_draw_str_aligned(canvas, 64, 34, AlignCenter, AlignTop, raw_credential);
  99. if(plugin_state->pacs.record.valid) {
  100. char parsed[20] = {0};
  101. sprintf(
  102. parsed,
  103. "FC: %03u CN: %05u",
  104. plugin_state->pacs.record.FacilityCode,
  105. plugin_state->pacs.record.CardNumber);
  106. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, parsed);
  107. }
  108. }
  109. release_mutex((ValueMutex*)ctx, plugin_state);
  110. }
  111. static void input_callback(InputEvent* input_event, osMessageQueueId_t event_queue) {
  112. furi_assert(event_queue);
  113. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  114. osMessageQueuePut(event_queue, &event, 0, osWaitForever);
  115. }
  116. static void picopass_state_init(PluginState* const plugin_state) {
  117. plugin_state->state = INIT;
  118. if(picopass_load_keys()) {
  119. plugin_state->state = READY;
  120. } else {
  121. plugin_state->state = KEYS_MISSING;
  122. }
  123. }
  124. ReturnCode decrypt(uint8_t* enc_data, uint8_t* dec_data) {
  125. uint8_t key[32] = {0};
  126. memcpy(key, iclass_decryptionkey, sizeof(iclass_decryptionkey));
  127. mbedtls_des3_context ctx;
  128. mbedtls_des3_init(&ctx);
  129. mbedtls_des3_set2key_dec(&ctx, key);
  130. mbedtls_des3_crypt_ecb(&ctx, enc_data, dec_data);
  131. mbedtls_des3_free(&ctx);
  132. return ERR_NONE;
  133. }
  134. ReturnCode parseWiegand(uint8_t* data, WiegandRecord* record) {
  135. uint32_t* halves = (uint32_t*)data;
  136. if(halves[0] == 0) {
  137. uint8_t leading0s = __builtin_clz(REVERSE_BYTES_U32(halves[1]));
  138. record->bitLength = 31 - leading0s;
  139. } else {
  140. uint8_t leading0s = __builtin_clz(REVERSE_BYTES_U32(halves[0]));
  141. record->bitLength = 63 - leading0s;
  142. }
  143. FURI_LOG_D(TAG, "bitLength: %d", record->bitLength);
  144. if(record->bitLength == 26) {
  145. uint8_t* v4 = data + 4;
  146. v4[0] = 0;
  147. uint32_t bot = v4[3] | (v4[2] << 8) | (v4[1] << 16) | (v4[0] << 24);
  148. record->CardNumber = (bot >> 1) & 0xFFFF;
  149. record->FacilityCode = (bot >> 17) & 0xFF;
  150. record->valid = true;
  151. } else {
  152. record->CardNumber = 0;
  153. record->FacilityCode = 0;
  154. record->valid = false;
  155. }
  156. return ERR_NONE;
  157. }
  158. ReturnCode disable_field(ReturnCode rc) {
  159. st25r3916TxRxOff();
  160. rfalLowPowerModeStart();
  161. return rc;
  162. }
  163. ReturnCode picopass_read_card(ApplicationArea* AA1) {
  164. rfalPicoPassIdentifyRes idRes;
  165. rfalPicoPassSelectRes selRes;
  166. rfalPicoPassReadCheckRes rcRes;
  167. rfalPicoPassCheckRes chkRes;
  168. ReturnCode err;
  169. uint8_t div_key[8] = {0};
  170. uint8_t mac[4] = {0};
  171. uint8_t ccnr[12] = {0};
  172. st25r3916TxRxOn();
  173. rfalLowPowerModeStop();
  174. rfalWorker();
  175. err = rfalPicoPassPollerInitialize();
  176. if(err != ERR_NONE) {
  177. FURI_LOG_E(TAG, "rfalPicoPassPollerInitialize error %d\n", err);
  178. return disable_field(err);
  179. }
  180. err = rfalFieldOnAndStartGT();
  181. if(err != ERR_NONE) {
  182. FURI_LOG_E(TAG, "rfalFieldOnAndStartGT error %d\n", err);
  183. return disable_field(err);
  184. }
  185. err = rfalPicoPassPollerCheckPresence();
  186. if(err != ERR_RF_COLLISION) {
  187. FURI_LOG_E(TAG, "rfalPicoPassPollerCheckPresence error %d\n", err);
  188. return disable_field(err);
  189. }
  190. err = rfalPicoPassPollerIdentify(&idRes);
  191. if(err != ERR_NONE) {
  192. FURI_LOG_E(TAG, "rfalPicoPassPollerIdentify error %d\n", err);
  193. return disable_field(err);
  194. }
  195. err = rfalPicoPassPollerSelect(idRes.CSN, &selRes);
  196. if(err != ERR_NONE) {
  197. FURI_LOG_E(TAG, "rfalPicoPassPollerSelect error %d\n", err);
  198. return disable_field(err);
  199. }
  200. err = rfalPicoPassPollerReadCheck(&rcRes);
  201. if(err != ERR_NONE) {
  202. FURI_LOG_E(TAG, "rfalPicoPassPollerReadCheck error %d", err);
  203. return disable_field(err);
  204. }
  205. memcpy(ccnr, rcRes.CCNR, sizeof(rcRes.CCNR)); // last 4 bytes left 0
  206. diversifyKey(selRes.CSN, iclass_key, div_key);
  207. opt_doReaderMAC(ccnr, div_key, mac);
  208. err = rfalPicoPassPollerCheck(mac, &chkRes);
  209. if(err != ERR_NONE) {
  210. FURI_LOG_E(TAG, "rfalPicoPassPollerCheck error %d", err);
  211. return disable_field(err);
  212. }
  213. for(size_t i = 0; i < 4; i++) {
  214. FURI_LOG_D(TAG, "rfalPicoPassPollerReadBlock block %d", i + 6);
  215. err = rfalPicoPassPollerReadBlock(i + 6, &(AA1->block[i]));
  216. if(err != ERR_NONE) {
  217. FURI_LOG_E(TAG, "rfalPicoPassPollerReadBlock error %d", err);
  218. return disable_field(err);
  219. }
  220. }
  221. return disable_field(ERR_NONE);
  222. }
  223. int32_t picopass_app(void* p) {
  224. UNUSED(p);
  225. osMessageQueueId_t event_queue = osMessageQueueNew(8, sizeof(PluginEvent), NULL);
  226. PluginState* plugin_state = malloc(sizeof(PluginState));
  227. picopass_state_init(plugin_state);
  228. ValueMutex state_mutex;
  229. if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) {
  230. FURI_LOG_E("Hello_world", "cannot create mutex\r\n");
  231. free(plugin_state);
  232. return 255;
  233. }
  234. // Set system callbacks
  235. ViewPort* view_port = view_port_alloc();
  236. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  237. view_port_input_callback_set(view_port, input_callback, event_queue);
  238. // Open GUI and register view_port
  239. Gui* gui = furi_record_open("gui");
  240. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  241. PluginEvent event;
  242. ReturnCode err;
  243. for(bool processing = true; processing;) {
  244. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 100);
  245. PluginState* plugin_state = (PluginState*)acquire_mutex_block(&state_mutex);
  246. if(event_status == osOK) {
  247. // press events
  248. if(event.type == EventTypeKey) {
  249. if(event.input.type == InputTypePress) {
  250. switch(event.input.key) {
  251. case InputKeyUp:
  252. FURI_LOG_D(TAG, "Input Up");
  253. break;
  254. case InputKeyDown:
  255. FURI_LOG_D(TAG, "Input Down");
  256. break;
  257. case InputKeyRight:
  258. FURI_LOG_D(TAG, "Input Right");
  259. break;
  260. case InputKeyLeft:
  261. FURI_LOG_D(TAG, "Input Left");
  262. break;
  263. case InputKeyOk:
  264. FURI_LOG_D(TAG, "Input OK");
  265. err = picopass_read_card(&AA1);
  266. if(err != ERR_NONE) {
  267. FURI_LOG_E(TAG, "picopass_read_card error %d", err);
  268. plugin_state->state = READY;
  269. break;
  270. }
  271. FURI_LOG_D(TAG, "read OK");
  272. plugin_state->pacs.biometrics = AA1.block[0].data[4];
  273. plugin_state->pacs.encryption = AA1.block[0].data[7];
  274. if(plugin_state->pacs.encryption == 0x17) {
  275. FURI_LOG_D(TAG, "3DES Encrypted");
  276. err = decrypt(AA1.block[1].data, plugin_state->pacs.credential);
  277. if(err != ERR_NONE) {
  278. FURI_LOG_E(TAG, "decrypt error %d", err);
  279. break;
  280. }
  281. FURI_LOG_D(TAG, "Decrypted 7");
  282. err = decrypt(AA1.block[2].data, plugin_state->pacs.pin0);
  283. if(err != ERR_NONE) {
  284. FURI_LOG_E(TAG, "decrypt error %d", err);
  285. break;
  286. }
  287. FURI_LOG_D(TAG, "Decrypted 8");
  288. err = decrypt(AA1.block[3].data, plugin_state->pacs.pin1);
  289. if(err != ERR_NONE) {
  290. FURI_LOG_E(TAG, "decrypt error %d", err);
  291. break;
  292. }
  293. FURI_LOG_D(TAG, "Decrypted 9");
  294. } else if(plugin_state->pacs.encryption == 0x14) {
  295. FURI_LOG_D(TAG, "No Encryption");
  296. memcpy(
  297. plugin_state->pacs.credential,
  298. AA1.block[1].data,
  299. RFAL_PICOPASS_MAX_BLOCK_LEN);
  300. memcpy(
  301. plugin_state->pacs.pin0,
  302. AA1.block[2].data,
  303. RFAL_PICOPASS_MAX_BLOCK_LEN);
  304. memcpy(
  305. plugin_state->pacs.pin1,
  306. AA1.block[3].data,
  307. RFAL_PICOPASS_MAX_BLOCK_LEN);
  308. } else if(plugin_state->pacs.encryption == 0x15) {
  309. FURI_LOG_D(TAG, "DES Encrypted");
  310. } else {
  311. FURI_LOG_D(TAG, "Unknown encryption");
  312. break;
  313. }
  314. FURI_LOG_D(
  315. TAG,
  316. "credential %02x%02x%02x%02x%02x%02x%02x%02x",
  317. plugin_state->pacs.credential[0],
  318. plugin_state->pacs.credential[1],
  319. plugin_state->pacs.credential[2],
  320. plugin_state->pacs.credential[3],
  321. plugin_state->pacs.credential[4],
  322. plugin_state->pacs.credential[5],
  323. plugin_state->pacs.credential[6],
  324. plugin_state->pacs.credential[7]);
  325. FURI_LOG_D(
  326. TAG,
  327. "pin0 %02x%02x%02x%02x%02x%02x%02x%02x",
  328. plugin_state->pacs.pin0[0],
  329. plugin_state->pacs.pin0[1],
  330. plugin_state->pacs.pin0[2],
  331. plugin_state->pacs.pin0[3],
  332. plugin_state->pacs.pin0[4],
  333. plugin_state->pacs.pin0[5],
  334. plugin_state->pacs.pin0[6],
  335. plugin_state->pacs.pin0[7]);
  336. FURI_LOG_D(
  337. TAG,
  338. "pin1 %02x%02x%02x%02x%02x%02x%02x%02x",
  339. plugin_state->pacs.pin1[0],
  340. plugin_state->pacs.pin1[1],
  341. plugin_state->pacs.pin1[2],
  342. plugin_state->pacs.pin1[3],
  343. plugin_state->pacs.pin1[4],
  344. plugin_state->pacs.pin1[5],
  345. plugin_state->pacs.pin1[6],
  346. plugin_state->pacs.pin1[7]);
  347. err = parseWiegand(
  348. plugin_state->pacs.credential, &plugin_state->pacs.record);
  349. if(err != ERR_NONE) {
  350. FURI_LOG_E(TAG, "parse error %d", err);
  351. break;
  352. }
  353. if(plugin_state->pacs.record.valid) {
  354. FURI_LOG_D(
  355. TAG,
  356. "FC: %03d CN: %05d",
  357. plugin_state->pacs.record.FacilityCode,
  358. plugin_state->pacs.record.CardNumber);
  359. }
  360. plugin_state->state = RESULT;
  361. break;
  362. case InputKeyBack:
  363. FURI_LOG_D(TAG, "Input Back");
  364. processing = false;
  365. break;
  366. }
  367. }
  368. }
  369. } else {
  370. // FURI_LOG_D(TAG, "osMessageQueue: event timeout");
  371. // event timeout
  372. }
  373. view_port_update(view_port);
  374. release_mutex(&state_mutex, plugin_state);
  375. }
  376. view_port_enabled_set(view_port, false);
  377. gui_remove_view_port(gui, view_port);
  378. furi_record_close("gui");
  379. view_port_free(view_port);
  380. osMessageQueueDelete(event_queue);
  381. return 0;
  382. }