zrtp_srtp.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
  3. * Copyright (c) 2006-2009 Philip R. Zimmermann. All rights reserved.
  4. * Contact: http://philzimmermann.com
  5. * For licensing and other legal details, see the file zrtp_legal.c.
  6. *
  7. * Vitaly Rozhkov <v.rozhkov at soft-industry.com>
  8. */
  9. #ifndef __ZRTP_SRTP_H__
  10. #define __ZRTP_SRTP_H__
  11. #include "zrtp_config.h"
  12. #include "zrtp_error.h"
  13. #include "zrtp_types.h"
  14. #include "zrtp_crypto.h"
  15. /* in host order, so outside the #if */
  16. #define ZRTP_RTCP_E_BIT 0x80000000
  17. /* for byte-access */
  18. #define ZRTP_RTCP_E_BYTE_BIT 0x80
  19. #define ZRTP_RTCP_INDEX_MASK 0x7fffffff
  20. /*!
  21. * \defgroup srtp SRTP encryption interface
  22. * \ingroup zrtp_dev
  23. * \{
  24. */
  25. /* Special types and definitions for the embedded implementation */
  26. #if (!defined(ZRTP_USE_EXTERN_SRTP) || (ZRTP_USE_EXTERN_SRTP == 0))
  27. #include "zrtp_srtp_builtin.h"
  28. /*!
  29. * \brief Structure describing an SRTP session.
  30. * An instance of this structure is created by calling zrtp_srtp_create()
  31. * and destroyed by calling zrtp_srtp_destroy(). It is used for
  32. * protecting and unprotecting included streams.
  33. */
  34. struct zrtp_srtp_ctx_t
  35. {
  36. zrtp_srtp_stream_ctx_t *outgoing_srtp; /*!< pointer to outgoing SRTP stream context */
  37. zrtp_srtp_stream_ctx_t *incoming_srtp; /*!< pointer to incoming SRTP stream context */
  38. };
  39. /*!
  40. * \brief Global context of an internal SRTP implementation.
  41. * It is created by calling zrtp_srtp_init() and destroyed by calling zrtp_srtp_down().
  42. * This context is used for holding replay protection mechanism data.
  43. */
  44. typedef struct
  45. {
  46. zrtp_rp_ctx_t *rp_ctx; /*!< pointer to replay protection context. */
  47. } zrtp_srtp_global_t;
  48. #else
  49. typedef void zrtp_srtp_global_t;
  50. #endif /* BUILDIN SRTP */
  51. /*! Defines types of SRTP hmac functions */
  52. typedef enum zrtp_srtp_hash_id_t
  53. {
  54. /*!
  55. * @warning SHA1 hash algorithm is for internal use only! It used for srtp authentication and does
  56. * not used in ZRTP protocol itself. Don't use it in \ref zrtp_profile_t#hash_schemes configuration.
  57. */
  58. ZRTP_SRTP_HASH_HMAC_SHA1 = 10
  59. } zrtp_srtp_hash_id_t;
  60. /*!
  61. * \brief Structure describing SRTP/SRTCP stream parameters.
  62. */
  63. typedef struct
  64. {
  65. /*!< Cipher used to encrypt packets */
  66. zrtp_cipher_t *cipher;
  67. /*!
  68. * \brief Cipher key length in bytes (not including salt length).
  69. * Used for cipher key derivation on stream initialization
  70. * by calling \ref zrtp_srtp_create().
  71. */
  72. uint32_t cipher_key_len;
  73. /*!< Hash used for packets authentication */
  74. zrtp_hash_t *hash;
  75. /*!
  76. * \brief Key length in bytes for HMAC generation.
  77. * Used for auth key derivation on stream initialization by calling \ref
  78. * zrtp_srtp_create() and for filling the key buffer with zeros on
  79. * stream deinitialization by calling \ref zrtp_srtp_destroy().
  80. */
  81. uint32_t auth_key_len;
  82. /*!< Structure describing SRTP authentication scheme */
  83. zrtp_auth_tag_length_t *auth_tag_len;
  84. } zrtp_srtp_policy_t;
  85. /*!
  86. * \brief Structure describing SRTP stream parameters.
  87. * Variables of this type should be mapped into the SRTP stream context when
  88. * a new stream is created.
  89. */
  90. typedef struct
  91. {
  92. zrtp_srtp_policy_t rtp_policy; /*!< crypto policy for RTP stream */
  93. zrtp_srtp_policy_t rtcp_policy; /*!< crypto policy for RTCP stream */
  94. zrtp_cipher_t *dk_cipher; /*!< cipher for the key derivation mechanism */
  95. /*!< Master key for key derivation. (holds the key value only, without the salt) */
  96. zrtp_string64_t key;
  97. /*!< Master salt for key derivation. (salt should be 14 bytes length) */
  98. zrtp_string64_t salt;
  99. uint16_t ssrc;
  100. } zrtp_srtp_profile_t;
  101. /*!
  102. * \brief Initialize SRTP engine and allocate global SRTP context.
  103. * Contains global data for all sessions and streams. For correct memory
  104. * management, the global SRTP context should be released by calling \ref
  105. * zrtp_srtp_destroy(). A pointer to the allocated SRTP global should be saved
  106. * at zrtp->srtp_global.
  107. * \warning this function \b must be called before any operation with the SRTP
  108. * engine.
  109. * \param zrtp - pointer to libzrtp global context
  110. * \return
  111. * - zrtp_status_ok if success
  112. * - zrtp_status_fail if error.
  113. */
  114. zrtp_status_t zrtp_srtp_init(zrtp_global_t *zrtp);
  115. /*!
  116. * \brief Free all allocated resources that were allocated by initialization
  117. * This function \b must be called at the end of SRTP engine use.
  118. * A pointer to deallocated SRTP global context (zrtp->srtp_global)
  119. * should be cleared ( set to NULL).
  120. * \param zrtp - pointer to libzrtp global context;
  121. * \return
  122. * - zrtp_status_ok - if SRTP engine has been deinitialized successfully;
  123. * - one of \ref zrtp_status_t errors - if deinitialization failed.
  124. */
  125. zrtp_status_t zrtp_srtp_down( zrtp_global_t *zrtp);
  126. /*!
  127. * \brief Creates SRTP context based on given incoming and outgoing profiles.
  128. * \param srtp_global - pointer to SRTP engine global context;
  129. * \param inc_profile - profile for incoming stream configuration;
  130. * \param out_profile - profile for outgoing stream configuration.
  131. * \return
  132. * - pointer to allocated and initialized SRTP session;
  133. * - NULL if error.
  134. */
  135. zrtp_srtp_ctx_t * zrtp_srtp_create( zrtp_srtp_global_t *srtp_global,
  136. zrtp_srtp_profile_t *inc_profile,
  137. zrtp_srtp_profile_t *out_profile );
  138. /*!
  139. * \brief Destroys SRTP context that was allocated by \ref zrtp_srtp_create()
  140. * \param srtp_global - pointer to SRTP engine global context;
  141. * \param srtp_ctx - pointer to SRTP context.
  142. * \return
  143. * - zrtp_status_ok - if SRTP context has been destroyed successfully;
  144. * - one of \ref zrtp_status_t errors if error.
  145. */
  146. zrtp_status_t zrtp_srtp_destroy( zrtp_srtp_global_t *srtp_global,
  147. zrtp_srtp_ctx_t * srtp_ctx );
  148. /*!
  149. * \brief Function applies SRTP protection to the RTP packet.
  150. * If zrtp_status_ok is returned, then packet points to the resulting SRTP
  151. * packet; otherwise, no assumptions should be made about the value of either
  152. * data elements.
  153. * \note This function assumes that it can write the authentication tag
  154. * directly into the packet buffer, right after the the RTP payload. 32-bit
  155. * boundary alignment of the packet is assumed as well.
  156. * \param srtp_global - global SRTP context;
  157. * \param srtp_ctx - SRTP context to use in processing the packet;
  158. * \param packet - pointer to the packet to be protected.
  159. * \return
  160. * - zrtp_status_ok - if packet has been protected successfully;
  161. * - one of \ref zrtp_status_t errors - if protection failed.
  162. */
  163. zrtp_status_t zrtp_srtp_protect( zrtp_srtp_global_t *srtp_global,
  164. zrtp_srtp_ctx_t *srtp_ctx,
  165. zrtp_rtp_info_t *packet );
  166. /*!
  167. * \brief Decrypts SRTP packet.
  168. * If zrtp_status_ok is returned, then packet points to the resulting plain RTP
  169. * packet; otherwise, no assumptions should be made about the value of either
  170. * data elements.
  171. * \warning This function assumes that the SRTP packet is aligned on
  172. * a 32-bit boundary.
  173. * \param srtp_global - global SRTP context;
  174. * \param srtp_ctx - SRTP context to use in processing the packet;
  175. * \param packet - pointer to the packet to be unprotected.
  176. * \return
  177. * - zrtp_status_ok - if packet has been unprotected successfully
  178. * - one of \ref zrtp_status_t errors - if decryption failed
  179. */
  180. zrtp_status_t zrtp_srtp_unprotect( zrtp_srtp_global_t *srtp_global,
  181. zrtp_srtp_ctx_t *srtp_ctx,
  182. zrtp_rtp_info_t *packet );
  183. /*!
  184. * \brief Function applies SRTCP protection to the RTCP packet.
  185. * If zrtp_status_ok is returned, then packet points to the result in SRTCP
  186. * packet; otherwise, no assumptions should be made about the value of either
  187. * data elements.
  188. * \note This function assumes that it can write the authentication tag
  189. * directly into the packet buffer, right after the the RTP payload. 32-bit
  190. * boundary alignment of the packet is also assumed.
  191. * \param srtp_global - global SRTP context;
  192. * \param srtp_ctx - SRTP context to use in processing the packet;
  193. * \param packet - pointer to the packet to be protected.
  194. * \return
  195. * - zrtp_status_ok - if packet has been protected successfully;
  196. * - one of \ref zrtp_status_t errors - if protection failed.
  197. */
  198. zrtp_status_t zrtp_srtp_protect_rtcp( zrtp_srtp_global_t *srtp_global,
  199. zrtp_srtp_ctx_t *srtp_ctx,
  200. zrtp_rtp_info_t *packet );
  201. /*!
  202. * \brief Decrypts SRTCP packet.
  203. * If zrtp_status_ok is returned, then packet points to the resulting RTCP
  204. * packet; otherwise, no assumptions should be made about the value of either
  205. * data elements.
  206. * \warning This function assumes that the SRTP packet is aligned on
  207. * a 32-bit boundary.
  208. * \param srtp_global - global SRTP context;
  209. * \param srtp_ctx - SRTP context to use in processing the packet;
  210. * \param packet - pointer to the packet to be unprotected.
  211. * \return
  212. * - zrtp_status_ok - if packet has been unprotected successfully;
  213. * - one of \ref zrtp_status_t errors - if decryption failed.
  214. */
  215. zrtp_status_t zrtp_srtp_unprotect_rtcp( zrtp_srtp_global_t *srtp_global,
  216. zrtp_srtp_ctx_t *srtp_ctx,
  217. zrtp_rtp_info_t *packet );
  218. /* \} */
  219. #endif /*__ZRTP_SRTP_H__ */