gen2_poller_i.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #include "gen2_poller.h"
  3. #include <nfc/protocols/nfc_generic_event.h>
  4. #include "crypto1.h" // TODO: Move to a better home
  5. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define GEN2_CMD_READ_ATS (0xE0)
  10. #define GEN2_FSDI_256 (0x8U)
  11. #define GEN2_POLLER_BLOCK_SIZE (16)
  12. #define GEN2_POLLER_MAX_BUFFER_SIZE (64U)
  13. #define GEN2_POLLER_MAX_FWT (150000U)
  14. typedef enum {
  15. Gen2PollerStateIdle,
  16. Gen2PollerStateRequestMode,
  17. Gen2PollerStateWipe,
  18. Gen2PollerStateWriteSourceDataRequest,
  19. Gen2PollerStateWriteTargetDataRequest,
  20. Gen2PollerStateWrite,
  21. Gen2PollerStateSuccess,
  22. Gen2PollerStateFail,
  23. Gen2PollerStateNum,
  24. } Gen2PollerState;
  25. typedef enum {
  26. Gen2AuthStateIdle,
  27. Gen2AuthStatePassed,
  28. } Gen2AuthState;
  29. typedef enum {
  30. Gen2CardStateDetected,
  31. Gen2CardStateLost,
  32. } Gen2CardState;
  33. typedef struct {
  34. MfClassicData* mfc_data_source;
  35. MfClassicData* mfc_data_target;
  36. MfClassicKey auth_key;
  37. MfClassicKeyType read_key;
  38. MfClassicKeyType write_key;
  39. uint16_t current_block;
  40. bool need_halt_before_write;
  41. } Gen2PollerWriteContext;
  42. typedef union {
  43. Gen2PollerWriteContext write_ctx;
  44. } Gen2PollerModeContext;
  45. struct Gen2Poller {
  46. Nfc* nfc;
  47. Gen2PollerState state;
  48. NfcPoller* poller;
  49. Iso14443_3aPoller* iso3_poller;
  50. Gen2AuthState auth_state;
  51. Gen2CardState card_state;
  52. Gen2PollerModeContext mode_ctx;
  53. Gen2PollerMode mode;
  54. Crypto1* crypto;
  55. BitBuffer* tx_plain_buffer;
  56. BitBuffer* tx_encrypted_buffer;
  57. BitBuffer* rx_plain_buffer;
  58. BitBuffer* rx_encrypted_buffer;
  59. MfClassicData* data;
  60. Gen2PollerEvent gen2_event;
  61. Gen2PollerEventData gen2_event_data;
  62. Gen2PollerCallback callback;
  63. void* context;
  64. };
  65. typedef struct {
  66. uint8_t block;
  67. MfClassicKeyType key_type;
  68. MfClassicNt nt;
  69. } Gen2CollectNtContext;
  70. typedef struct {
  71. uint8_t block_num;
  72. MfClassicKey key;
  73. MfClassicKeyType key_type;
  74. MfClassicBlock block;
  75. } Gen2ReadBlockContext;
  76. typedef struct {
  77. uint8_t block_num;
  78. MfClassicKey key;
  79. MfClassicKeyType key_type;
  80. MfClassicBlock block;
  81. } Gen2WriteBlockContext;
  82. Gen2PollerError gen2_poller_write(Gen2Poller* instance);
  83. Gen2PollerError gen2_poller_auth(
  84. Gen2Poller* instance,
  85. uint8_t block_num,
  86. MfClassicKey* key,
  87. MfClassicKeyType key_type,
  88. MfClassicAuthContext* data);
  89. Gen2PollerError gen2_poller_halt(Gen2Poller* instance);
  90. Gen2PollerError
  91. gen2_poller_write_block(Gen2Poller* instance, uint8_t block_num, const MfClassicBlock* data);
  92. MfClassicKeyType
  93. gen2_poller_get_key_type_to_write(const MfClassicData* mfc_data, uint8_t block_num);
  94. bool gen2_poller_can_write_block(const MfClassicData* mfc_data, uint8_t block_num);
  95. bool gen2_can_reset_access_conditions(const MfClassicData* mfc_data, uint8_t block_num);
  96. Gen2PollerWriteProblems
  97. gen2_poller_can_write_data_block(const MfClassicData* mfc_data, uint8_t block_num);
  98. Gen2PollerWriteProblems
  99. gen2_poller_can_write_sector_trailer(const MfClassicData* mfc_data, uint8_t block_num);
  100. bool gen2_is_allowed_access(
  101. const MfClassicData* data,
  102. uint8_t block_num,
  103. MfClassicKeyType key_type,
  104. MfClassicAction action);
  105. #ifdef __cplusplus
  106. }
  107. #endif