srp.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <stdbool.h>
  5. #include "esp_err.h"
  6. /**
  7. * SRP-6a implementation for HomeKit/AirPlay pair-setup
  8. * Uses 3072-bit prime N and SHA-512
  9. */
  10. // SRP parameter sizes
  11. #define SRP_PRIME_BITS 3072
  12. #define SRP_PRIME_BYTES (SRP_PRIME_BITS / 8) // 384 bytes
  13. #define SRP_SALT_BYTES 16
  14. #define SRP_PROOF_BYTES 64
  15. #define SRP_SESSION_KEY_BYTES 64 // SHA-512 output
  16. // SRP session context
  17. typedef struct srp_session {
  18. uint8_t salt[SRP_SALT_BYTES];
  19. uint8_t server_public_key[SRP_PRIME_BYTES]; // B
  20. uint8_t server_secret[SRP_PRIME_BYTES]; // b
  21. uint8_t client_public_key[SRP_PRIME_BYTES]; // A
  22. uint8_t session_key[SRP_SESSION_KEY_BYTES]; // K
  23. size_t session_key_len;
  24. uint8_t proof_m1[SRP_SESSION_KEY_BYTES]; // Client proof
  25. uint8_t proof_m2[SRP_SESSION_KEY_BYTES]; // Server proof
  26. int state;
  27. bool verified;
  28. } srp_session_t;
  29. /**
  30. * Create a new SRP session
  31. */
  32. srp_session_t *srp_session_create(void);
  33. /**
  34. * Free an SRP session
  35. */
  36. void srp_session_free(srp_session_t *session);
  37. /**
  38. * Start SRP session (generate salt and server public key B)
  39. * For transient pairing, username="Pair-Setup" and password="3939"
  40. *
  41. * @param session SRP session
  42. * @param username Username (typically "Pair-Setup")
  43. * @param password Password (typically "3939" for transient)
  44. * @return ESP_OK on success
  45. */
  46. esp_err_t srp_start(srp_session_t *session, const char *username,
  47. const char *password);
  48. /**
  49. * Get salt for M2 response
  50. */
  51. const uint8_t *srp_get_salt(srp_session_t *session);
  52. /**
  53. * Get server public key B for M2 response
  54. * @param len Output: length of public key
  55. */
  56. const uint8_t *srp_get_public_key(srp_session_t *session, size_t *len);
  57. /**
  58. * Process client's public key A and proof M1 from M3
  59. * Verifies client's proof and generates server proof M2
  60. *
  61. * @param session SRP session
  62. * @param client_public_key Client's public key A
  63. * @param client_pk_len Length of client public key
  64. * @param client_proof Client's proof M1
  65. * @param proof_len Length of proof
  66. * @return ESP_OK if verification succeeds
  67. */
  68. esp_err_t srp_verify_client(srp_session_t *session,
  69. const uint8_t *client_public_key,
  70. size_t client_pk_len, const uint8_t *client_proof,
  71. size_t proof_len);
  72. /**
  73. * Get server proof M2 for M4 response
  74. */
  75. const uint8_t *srp_get_proof(srp_session_t *session);
  76. /**
  77. * Get session key K after successful verification
  78. * This key is used to encrypt M5/M6 messages
  79. */
  80. const uint8_t *srp_get_session_key(srp_session_t *session, size_t *len);