picopass_device.h 4.7 KB

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