picopass_device.h 4.3 KB

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