picopass_device.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 129
  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_CRYPT0 0x08
  38. #define PICOPASS_FUSE_CRYPT10 (PICOPASS_FUSE_CRYPT1 | PICOPASS_FUSE_CRYPT0)
  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. PicopassDeviceSaveFormatOriginal,
  60. PicopassDeviceSaveFormatLegacy,
  61. PicopassDeviceSaveFormatLF,
  62. PicopassDeviceSaveFormatSeader,
  63. PicopassDeviceSaveFormatPartial,
  64. } PicopassDeviceSaveFormat;
  65. typedef enum {
  66. PicopassDeviceAuthMethodUnset,
  67. PicopassDeviceAuthMethodNone, // unsecured picopass
  68. PicopassDeviceAuthMethodKey,
  69. PicopassDeviceAuthMethodNrMac,
  70. PicopassDeviceAuthMethodFailed,
  71. } PicopassDeviceAuthMethod;
  72. typedef enum {
  73. PicopassEmulatorStateHalt,
  74. PicopassEmulatorStateIdle,
  75. PicopassEmulatorStateActive,
  76. PicopassEmulatorStateSelected,
  77. PicopassEmulatorStateStopEmulation,
  78. } PicopassEmulatorState;
  79. typedef struct {
  80. bool legacy;
  81. bool se_enabled;
  82. bool sio;
  83. bool biometrics;
  84. uint8_t key[8];
  85. bool elite_kdf;
  86. uint8_t pin_length;
  87. PicopassEncryption encryption;
  88. uint8_t bitLength;
  89. uint8_t credential[8];
  90. uint8_t pin0[8];
  91. uint8_t pin1[8];
  92. } PicopassPacs;
  93. typedef struct {
  94. uint8_t data[PICOPASS_BLOCK_LEN];
  95. bool valid;
  96. } PicopassBlock;
  97. typedef struct {
  98. PicopassBlock card_data[PICOPASS_MAX_APP_LIMIT];
  99. PicopassPacs pacs;
  100. PicopassDeviceAuthMethod auth;
  101. } PicopassDeviceData;
  102. typedef struct {
  103. PicopassEmulatorState state;
  104. LoclassState_t cipher_state;
  105. uint8_t key_block_num; // in loclass mode used to store csn#
  106. bool loclass_mode;
  107. bool loclass_got_std_key;
  108. uint8_t loclass_mac_buffer[8 * LOCLASS_NUM_PER_CSN];
  109. LoclassWriter* loclass_writer;
  110. } PicopassEmulatorCtx;
  111. typedef struct {
  112. Storage* storage;
  113. DialogsApp* dialogs;
  114. PicopassDeviceData dev_data;
  115. char dev_name[PICOPASS_DEV_NAME_MAX_LEN];
  116. FuriString* load_path;
  117. PicopassDeviceSaveFormat format;
  118. PicopassLoadingCallback loading_cb;
  119. void* loading_cb_ctx;
  120. } PicopassDevice;
  121. PicopassDevice* picopass_device_alloc();
  122. void picopass_device_free(PicopassDevice* picopass_dev);
  123. void picopass_device_set_name(PicopassDevice* dev, const char* name);
  124. bool picopass_device_save(PicopassDevice* dev, const char* dev_name);
  125. bool picopass_file_select(PicopassDevice* dev);
  126. void picopass_device_data_clear(PicopassDeviceData* dev_data);
  127. void picopass_device_clear(PicopassDevice* dev);
  128. bool picopass_device_delete(PicopassDevice* dev, bool use_load_path);
  129. void picopass_device_set_loading_callback(
  130. PicopassDevice* dev,
  131. PicopassLoadingCallback callback,
  132. void* context);
  133. void picopass_device_parse_credential(PicopassBlock* card_data, PicopassPacs* pacs);
  134. void picopass_device_parse_wiegand(PicopassPacs* pacs);
  135. bool picopass_device_hid_csn(PicopassDevice* dev);