protocomm_security.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <esp_err.h>
  8. #include "esp_event.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. ESP_EVENT_DECLARE_BASE(PROTOCOMM_SECURITY_SESSION_EVENT);
  13. /**
  14. * @brief Events generated by the protocomm security layer
  15. *
  16. * These events are generated while establishing secured session.
  17. */
  18. typedef enum {
  19. PROTOCOMM_SECURITY_SESSION_SETUP_OK, /**< Secured session established successfully */
  20. PROTOCOMM_SECURITY_SESSION_INVALID_SECURITY_PARAMS, /**< Received invalid (NULL) security parameters (username / client public-key) */
  21. PROTOCOMM_SECURITY_SESSION_CREDENTIALS_MISMATCH, /**< Received incorrect credentials (username / PoP) */
  22. } protocomm_security_session_event_t;
  23. /**
  24. * @brief Protocomm Security 1 parameters: Proof Of Possession
  25. */
  26. typedef struct protocomm_security1_params {
  27. /**
  28. * Pointer to buffer containing the proof of possession data
  29. */
  30. const uint8_t *data;
  31. /**
  32. * Length (in bytes) of the proof of possession data
  33. */
  34. uint16_t len;
  35. } protocomm_security1_params_t;
  36. typedef protocomm_security1_params_t protocomm_security_pop_t __attribute__((deprecated("Use protocomm_security1_params_t instead")));
  37. /**
  38. * @brief Protocomm Security 2 parameters: Salt and Verifier
  39. *
  40. */
  41. typedef struct protocomm_security2_params {
  42. /**
  43. * Pointer to the buffer containing the salt
  44. */
  45. const char *salt;
  46. /**
  47. * Length (in bytes) of the salt
  48. */
  49. uint16_t salt_len;
  50. /**
  51. * Pointer to the buffer containing the verifier
  52. */
  53. const char *verifier;
  54. /**
  55. * Length (in bytes) of the verifier
  56. */
  57. uint16_t verifier_len;
  58. } protocomm_security2_params_t;
  59. typedef void * protocomm_security_handle_t;
  60. /**
  61. * @brief Protocomm security object structure.
  62. *
  63. * The member functions are used for implementing secure
  64. * protocomm sessions.
  65. *
  66. * @note This structure should not have any dynamic
  67. * members to allow re-entrancy
  68. */
  69. typedef struct protocomm_security {
  70. /**
  71. * Unique version number of security implementation
  72. */
  73. int ver;
  74. /**
  75. * Function for initializing/allocating security
  76. * infrastructure
  77. */
  78. esp_err_t (*init)(protocomm_security_handle_t *handle);
  79. /**
  80. * Function for deallocating security infrastructure
  81. */
  82. esp_err_t (*cleanup)(protocomm_security_handle_t handle);
  83. /**
  84. * Starts new secure transport session with specified ID
  85. */
  86. esp_err_t (*new_transport_session)(protocomm_security_handle_t handle,
  87. uint32_t session_id);
  88. /**
  89. * Closes a secure transport session with specified ID
  90. */
  91. esp_err_t (*close_transport_session)(protocomm_security_handle_t handle,
  92. uint32_t session_id);
  93. /**
  94. * Handler function for authenticating connection
  95. * request and establishing secure session
  96. */
  97. esp_err_t (*security_req_handler)(protocomm_security_handle_t handle,
  98. const void *sec_params,
  99. uint32_t session_id,
  100. const uint8_t *inbuf, ssize_t inlen,
  101. uint8_t **outbuf, ssize_t *outlen,
  102. void *priv_data);
  103. /**
  104. * Function which implements the encryption algorithm
  105. */
  106. esp_err_t (*encrypt)(protocomm_security_handle_t handle,
  107. uint32_t session_id,
  108. const uint8_t *inbuf, ssize_t inlen,
  109. uint8_t **outbuf, ssize_t *outlen);
  110. /**
  111. * Function which implements the decryption algorithm
  112. */
  113. esp_err_t (*decrypt)(protocomm_security_handle_t handle,
  114. uint32_t session_id,
  115. const uint8_t *inbuf, ssize_t inlen,
  116. uint8_t **outbuf, ssize_t *outlen);
  117. } protocomm_security_t;
  118. #ifdef __cplusplus
  119. }
  120. #endif