picopass_device.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <storage/storage.h>
  5. #include <dialogs/dialogs.h>
  6. #include <mbedtls/des.h>
  7. #include "rfal_picopass.h"
  8. #include "loclass_writer.h"
  9. #include <optimized_ikeys.h>
  10. #include <optimized_cipher.h>
  11. #include "helpers/iclass_elite_dict.h"
  12. #define PICOPASS_DEV_NAME_MAX_LEN 22
  13. #define PICOPASS_READER_DATA_MAX_SIZE 64
  14. #define PICOPASS_MAX_APP_LIMIT 32
  15. #define PICOPASS_CSN_BLOCK_INDEX 0
  16. #define PICOPASS_CONFIG_BLOCK_INDEX 1
  17. // These definitions for blocks above 2 only hold for secure cards.
  18. #define PICOPASS_SECURE_EPURSE_BLOCK_INDEX 2
  19. #define PICOPASS_SECURE_KD_BLOCK_INDEX 3
  20. #define PICOPASS_SECURE_KC_BLOCK_INDEX 4
  21. #define PICOPASS_SECURE_AIA_BLOCK_INDEX 5
  22. // Non-secure cards instead have an AIA at block 2
  23. #define PICOPASS_NONSECURE_AIA_BLOCK_INDEX 2
  24. // Only iClass cards
  25. #define PICOPASS_ICLASS_PACS_CFG_BLOCK_INDEX 6
  26. // Personalization Mode
  27. #define PICOPASS_FUSE_PERS 0x80
  28. // Crypt1 // 1+1 (crypt1+crypt0) means secured and keys changable
  29. #define PICOPASS_FUSE_CRYPT1 0x10
  30. // Crypt0 // 1+0 means secure and keys locked, 0+1 means not secured, 0+0 means disable auth entirely
  31. #define PICOPASS_FUSE_CRTPT0 0x08
  32. #define PICOPASS_FUSE_CRYPT10 (PICOPASS_FUSE_CRYPT1 | PICOPASS_FUSE_CRTPT0)
  33. // Read Access, 1 meanns anonymous read enabled, 0 means must auth to read applicaion
  34. #define PICOPASS_FUSE_RA 0x01
  35. #define PICOPASS_APP_FOLDER ANY_PATH("picopass")
  36. #define PICOPASS_APP_EXTENSION ".picopass"
  37. #define PICOPASS_APP_SHADOW_EXTENSION ".pas"
  38. #define PICOPASS_DICT_KEY_BATCH_SIZE 10
  39. typedef void (*PicopassLoadingCallback)(void* context, bool state);
  40. typedef struct {
  41. IclassEliteDict* dict;
  42. IclassEliteDictType type;
  43. uint8_t current_sector;
  44. } IclassEliteDictAttackData;
  45. typedef enum {
  46. PicopassDeviceEncryptionUnknown = 0,
  47. PicopassDeviceEncryptionNone = 0x14,
  48. PicopassDeviceEncryptionDES = 0x15,
  49. PicopassDeviceEncryption3DES = 0x17,
  50. } PicopassEncryption;
  51. typedef enum {
  52. PicopassDeviceSaveFormatHF,
  53. PicopassDeviceSaveFormatLF,
  54. } PicopassDeviceSaveFormat;
  55. typedef enum {
  56. PicopassEmulatorStateHalt,
  57. PicopassEmulatorStateIdle,
  58. PicopassEmulatorStateActive,
  59. PicopassEmulatorStateSelected,
  60. } PicopassEmulatorState;
  61. typedef struct {
  62. bool valid;
  63. uint8_t bitLength;
  64. uint8_t FacilityCode;
  65. uint16_t CardNumber;
  66. } PicopassWiegandRecord;
  67. typedef struct {
  68. bool legacy;
  69. bool se_enabled;
  70. bool sio;
  71. bool biometrics;
  72. uint8_t key[8];
  73. bool elite_kdf;
  74. uint8_t pin_length;
  75. PicopassEncryption encryption;
  76. uint8_t credential[8];
  77. uint8_t pin0[8];
  78. uint8_t pin1[8];
  79. PicopassWiegandRecord record;
  80. } PicopassPacs;
  81. typedef struct {
  82. uint8_t data[RFAL_PICOPASS_BLOCK_LEN];
  83. } PicopassBlock;
  84. typedef struct {
  85. PicopassBlock AA1[PICOPASS_MAX_APP_LIMIT];
  86. PicopassPacs pacs;
  87. IclassEliteDictAttackData iclass_elite_dict_attack_data;
  88. } PicopassDeviceData;
  89. typedef struct {
  90. PicopassEmulatorState state;
  91. LoclassState_t cipher_state;
  92. uint8_t key_block_num; // in loclass mode used to store csn#
  93. bool loclass_mode;
  94. bool loclass_got_std_key;
  95. LoclassWriter* loclass_writer;
  96. } PicopassEmulatorCtx;
  97. typedef struct {
  98. Storage* storage;
  99. DialogsApp* dialogs;
  100. PicopassDeviceData dev_data;
  101. char dev_name[PICOPASS_DEV_NAME_MAX_LEN + 1];
  102. FuriString* load_path;
  103. PicopassDeviceSaveFormat format;
  104. PicopassLoadingCallback loading_cb;
  105. void* loading_cb_ctx;
  106. } PicopassDevice;
  107. PicopassDevice* picopass_device_alloc();
  108. void picopass_device_free(PicopassDevice* picopass_dev);
  109. void picopass_device_set_name(PicopassDevice* dev, const char* name);
  110. bool picopass_device_save(PicopassDevice* dev, const char* dev_name);
  111. bool picopass_file_select(PicopassDevice* dev);
  112. void picopass_device_data_clear(PicopassDeviceData* dev_data);
  113. void picopass_device_clear(PicopassDevice* dev);
  114. bool picopass_device_delete(PicopassDevice* dev, bool use_load_path);
  115. void picopass_device_set_loading_callback(
  116. PicopassDevice* dev,
  117. PicopassLoadingCallback callback,
  118. void* context);
  119. ReturnCode picopass_device_parse_credential(PicopassBlock* AA1, PicopassPacs* pacs);
  120. ReturnCode picopass_device_parse_wiegand(uint8_t* data, PicopassWiegandRecord* record);