tlv8.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. /**
  6. * TLV8 encoder/decoder for HAP protocol
  7. * Type-Length-Value format where Length is max 255 bytes
  8. * Longer values are split across multiple TLVs with same type
  9. */
  10. // HAP TLV types for pair-verify
  11. #define TLV_TYPE_METHOD 0x00
  12. #define TLV_TYPE_IDENTIFIER 0x01
  13. #define TLV_TYPE_SALT 0x02
  14. #define TLV_TYPE_PUBLIC_KEY 0x03
  15. #define TLV_TYPE_PROOF 0x04
  16. #define TLV_TYPE_ENCRYPTED_DATA 0x05
  17. #define TLV_TYPE_STATE 0x06
  18. #define TLV_TYPE_ERROR 0x07
  19. #define TLV_TYPE_SIGNATURE 0x0A
  20. // HAP Error codes
  21. #define TLV_ERROR_UNKNOWN 0x01
  22. #define TLV_ERROR_AUTHENTICATION 0x02
  23. #define TLV_ERROR_BACKOFF 0x03
  24. #define TLV_ERROR_MAX_PEERS 0x04
  25. #define TLV_ERROR_MAX_TRIES 0x05
  26. #define TLV_ERROR_UNAVAILABLE 0x06
  27. #define TLV_ERROR_BUSY 0x07
  28. // Pair-verify states
  29. #define PAIR_VERIFY_STATE_M1 0x01
  30. #define PAIR_VERIFY_STATE_M2 0x02
  31. #define PAIR_VERIFY_STATE_M3 0x03
  32. #define PAIR_VERIFY_STATE_M4 0x04
  33. typedef struct {
  34. uint8_t *buffer;
  35. size_t size;
  36. size_t capacity;
  37. } tlv8_encoder_t;
  38. typedef struct {
  39. const uint8_t *data;
  40. size_t len;
  41. } tlv8_value_t;
  42. /**
  43. * Initialize TLV8 encoder
  44. */
  45. void tlv8_encoder_init(tlv8_encoder_t *enc, uint8_t *buffer, size_t capacity);
  46. /**
  47. * Add a TLV to the encoder
  48. * Handles values > 255 bytes by splitting into multiple TLVs
  49. */
  50. bool tlv8_encode(tlv8_encoder_t *enc, uint8_t type, const uint8_t *value,
  51. size_t len);
  52. /**
  53. * Add a single byte TLV
  54. */
  55. bool tlv8_encode_byte(tlv8_encoder_t *enc, uint8_t type, uint8_t value);
  56. /**
  57. * Get encoded size
  58. */
  59. size_t tlv8_encoder_size(const tlv8_encoder_t *enc);
  60. /**
  61. * Find a TLV by type in encoded data
  62. * Returns pointer to value and sets len, or NULL if not found
  63. * For split TLVs, only returns first chunk (use tlv8_decode_concat for full
  64. * value)
  65. */
  66. const uint8_t *tlv8_find(const uint8_t *data, size_t data_len, uint8_t type,
  67. size_t *value_len);
  68. /**
  69. * Decode and concatenate split TLVs of same type
  70. * @param data Input TLV data
  71. * @param data_len Length of input data
  72. * @param type TLV type to find
  73. * @param out_buffer Output buffer for concatenated value
  74. * @param out_capacity Capacity of output buffer
  75. * @param out_len Actual length of decoded value
  76. * @return true if found, false otherwise
  77. */
  78. bool tlv8_decode_concat(const uint8_t *data, size_t data_len, uint8_t type,
  79. uint8_t *out_buffer, size_t out_capacity,
  80. size_t *out_len);