srtp.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * srtp.h
  3. *
  4. * interface to libsrtp
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifndef SRTP_H
  45. #define SRTP_H
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #include "crypto_kernel.h"
  50. /**
  51. * @defgroup SRTP Secure RTP
  52. *
  53. * @brief libSRTP provides functions for protecting RTP and RTCP. See
  54. * Section @ref Overview for an introduction to the use of the library.
  55. *
  56. * @{
  57. */
  58. /*
  59. * SRTP_MASTER_KEY_LEN is the nominal master key length supported by libSRTP
  60. */
  61. #define SRTP_MASTER_KEY_LEN 30
  62. /*
  63. * SRTP_MAX_KEY_LEN is the maximum key length supported by libSRTP
  64. */
  65. #define SRTP_MAX_KEY_LEN 64
  66. /*
  67. * SRTP_MAX_TAG_LEN is the maximum tag length supported by libSRTP
  68. */
  69. #define SRTP_MAX_TAG_LEN 12
  70. /**
  71. * SRTP_MAX_TRAILER_LEN is the maximum length of the SRTP trailer
  72. * (authentication tag and MKI) supported by libSRTP. This value is
  73. * the maximum number of octets that will be added to an RTP packet by
  74. * srtp_protect().
  75. *
  76. * @brief the maximum number of octets added by srtp_protect().
  77. */
  78. #define SRTP_MAX_TRAILER_LEN SRTP_MAX_TAG_LEN
  79. /*
  80. * nota bene: since libSRTP doesn't support the use of the MKI, the
  81. * SRTP_MAX_TRAILER_LEN value is just the maximum tag length
  82. */
  83. /**
  84. * @brief sec_serv_t describes a set of security services.
  85. *
  86. * A sec_serv_t enumeration is used to describe the particular
  87. * security services that will be applied by a particular crypto
  88. * policy (or other mechanism).
  89. */
  90. typedef enum {
  91. sec_serv_none = 0, /**< no services */
  92. sec_serv_conf = 1, /**< confidentiality */
  93. sec_serv_auth = 2, /**< authentication */
  94. sec_serv_conf_and_auth = 3 /**< confidentiality and authentication */
  95. }
  96. sec_serv_t;
  97. /**
  98. * @brief crypto_policy_t describes a particular crypto policy that
  99. * can be applied to an SRTP stream.
  100. *
  101. * A crypto_policy_t describes a particular cryptographic policy that
  102. * can be applied to an SRTP or SRTCP stream. An SRTP session policy
  103. * consists of a list of these policies, one for each SRTP stream
  104. * in the session.
  105. */
  106. typedef struct crypto_policy_t {
  107. cipher_type_id_t cipher_type; /**< An integer representing
  108. * the type of cipher. */
  109. int cipher_key_len; /**< The length of the cipher key
  110. * in octets. */
  111. auth_type_id_t auth_type; /**< An integer representing the
  112. * authentication function. */
  113. int auth_key_len; /**< The length of the authentication
  114. * function key in octets. */
  115. int auth_tag_len; /**< The length of the authentication
  116. * tag in octets. */
  117. sec_serv_t sec_serv; /**< The flag indicating the security
  118. * services to be applied. */
  119. } crypto_policy_t;
  120. /**
  121. * @brief ssrc_type_t describes the type of an SSRC.
  122. *
  123. * An ssrc_type_t enumeration is used to indicate a type of SSRC. See
  124. * @ref srtp_policy_t for more informataion.
  125. */
  126. typedef enum {
  127. ssrc_undefined = 0, /**< Indicates an undefined SSRC type. */
  128. ssrc_specific = 1, /**< Indicates a specific SSRC value */
  129. ssrc_any_inbound = 2, /**< Indicates any inbound SSRC value
  130. (i.e. a value that is used in the
  131. function srtp_unprotect()) */
  132. ssrc_any_outbound = 3 /**< Indicates any outbound SSRC value
  133. (i.e. a value that is used in the
  134. function srtp_protect()) */
  135. } ssrc_type_t;
  136. /**
  137. * @brief An ssrc_t represents a particular SSRC value, or a `wildcard' SSRC.
  138. *
  139. * An ssrc_t represents a particular SSRC value (if its type is
  140. * ssrc_specific), or a wildcard SSRC value that will match all
  141. * outbound SSRCs (if its type is ssrc_any_outbound) or all inbound
  142. * SSRCs (if its type is ssrc_any_inbound).
  143. *
  144. */
  145. typedef struct {
  146. ssrc_type_t type; /**< The type of this particular SSRC */
  147. unsigned int value; /**< The value of this SSRC, if it is not a wildcard */
  148. } ssrc_t;
  149. /**
  150. * @brief points to an EKT policy
  151. */
  152. typedef struct ekt_policy_ctx_t *ekt_policy_t;
  153. /**
  154. * @brief points to EKT stream data
  155. */
  156. typedef struct ekt_stream_ctx_t *ekt_stream_t;
  157. /**
  158. * @brief represents the policy for an SRTP session.
  159. *
  160. * A single srtp_policy_t struct represents the policy for a single
  161. * SRTP stream, and a linked list of these elements represents the
  162. * policy for an entire SRTP session. Each element contains the SRTP
  163. * and SRTCP crypto policies for that stream, a pointer to the SRTP
  164. * master key for that stream, the SSRC describing that stream, or a
  165. * flag indicating a `wildcard' SSRC value, and a `next' field that
  166. * holds a pointer to the next element in the list of policy elements,
  167. * or NULL if it is the last element.
  168. *
  169. * The wildcard value SSRC_ANY_INBOUND matches any SSRC from an
  170. * inbound stream that for which there is no explicit SSRC entry in
  171. * another policy element. Similarly, the value SSRC_ANY_OUTBOUND
  172. * will matches any SSRC from an outbound stream that does not appear
  173. * in another policy element. Note that wildcard SSRCs &b cannot be
  174. * used to match both inbound and outbound traffic. This restriction
  175. * is intentional, and it allows libSRTP to ensure that no security
  176. * lapses result from accidental re-use of SSRC values during key
  177. * sharing.
  178. *
  179. *
  180. * @warning The final element of the list @b must have its `next' pointer
  181. * set to NULL.
  182. */
  183. typedef struct srtp_policy_t {
  184. ssrc_t ssrc; /**< The SSRC value of stream, or the
  185. * flags SSRC_ANY_INBOUND or
  186. * SSRC_ANY_OUTBOUND if key sharing
  187. * is used for this policy element.
  188. */
  189. crypto_policy_t rtp; /**< SRTP crypto policy. */
  190. crypto_policy_t rtcp; /**< SRTCP crypto policy. */
  191. unsigned char *key; /**< Pointer to the SRTP master key for
  192. * this stream. */
  193. ekt_policy_t ekt; /**< Pointer to the EKT policy structure
  194. * for this stream (if any) */
  195. unsigned long window_size; /**< The window size to use for replay
  196. * protection. */
  197. int allow_repeat_tx; /**< Whether retransmissions of
  198. * packets with the same sequence number
  199. * are allowed. (Note that such repeated
  200. * transmissions must have the same RTP
  201. * payload, or a severe security weakness
  202. * is introduced!) */
  203. struct srtp_policy_t *next; /**< Pointer to next stream policy. */
  204. } srtp_policy_t;
  205. /**
  206. * @brief An srtp_t points to an SRTP session structure.
  207. *
  208. * The typedef srtp_t is a pointer to a structure that represents
  209. * an SRTP session. This datatype is intentially opaque in
  210. * order to separate the interface from the implementation.
  211. *
  212. * An SRTP session consists of all of the traffic sent to the RTP and
  213. * RTCP destination transport addresses, using the RTP/SAVP (Secure
  214. * Audio/Video Profile). A session can be viewed as a set of SRTP
  215. * streams, each of which originates with a different participant.
  216. */
  217. typedef struct srtp_ctx_t *srtp_t;
  218. /**
  219. * @brief An srtp_stream_t points to an SRTP stream structure.
  220. *
  221. * The typedef srtp_stream_t is a pointer to a structure that
  222. * represents an SRTP stream. This datatype is intentionally
  223. * opaque in order to separate the interface from the implementation.
  224. *
  225. * An SRTP stream consists of all of the traffic sent to an SRTP
  226. * session by a single participant. A session can be viewed as
  227. * a set of streams.
  228. *
  229. */
  230. typedef struct srtp_stream_ctx_t *srtp_stream_t;
  231. /**
  232. * @brief srtp_init() initializes the srtp library.
  233. *
  234. * @warning This function @b must be called before any other srtp
  235. * functions.
  236. */
  237. err_status_t
  238. srtp_init(void);
  239. /**
  240. * @brief srtp_shutdown() de-initializes the srtp library.
  241. *
  242. * @warning No srtp functions may be called after calling this function.
  243. */
  244. err_status_t
  245. srtp_shutdown(void);
  246. /**
  247. * @brief srtp_protect() is the Secure RTP sender-side packet processing
  248. * function.
  249. *
  250. * The function call srtp_protect(ctx, rtp_hdr, len_ptr) applies SRTP
  251. * protection to the RTP packet rtp_hdr (which has length *len_ptr) using
  252. * the SRTP context ctx. If err_status_ok is returned, then rtp_hdr
  253. * points to the resulting SRTP packet and *len_ptr is the number of
  254. * octets in that packet; otherwise, no assumptions should be made
  255. * about the value of either data elements.
  256. *
  257. * The sequence numbers of the RTP packets presented to this function
  258. * need not be consecutive, but they @b must be out of order by less
  259. * than 2^15 = 32,768 packets.
  260. *
  261. * @warning This function assumes that it can write the authentication
  262. * tag into the location in memory immediately following the RTP
  263. * packet, and assumes that the RTP packet is aligned on a 32-bit
  264. * boundary.
  265. *
  266. * @param ctx is the SRTP context to use in processing the packet.
  267. *
  268. * @param rtp_hdr is a pointer to the RTP packet (before the call); after
  269. * the function returns, it points to the srtp packet.
  270. *
  271. * @param len_ptr is a pointer to the length in octets of the complete
  272. * RTP packet (header and body) before the function call, and of the
  273. * complete SRTP packet after the call, if err_status_ok was returned.
  274. * Otherwise, the value of the data to which it points is undefined.
  275. *
  276. * @return
  277. * - err_status_ok no problems
  278. * - err_status_replay_fail rtp sequence number was non-increasing
  279. * - @e other failure in cryptographic mechanisms
  280. */
  281. err_status_t
  282. srtp_protect(srtp_t ctx, void *rtp_hdr, int *len_ptr);
  283. /**
  284. * @brief srtp_unprotect() is the Secure RTP receiver-side packet
  285. * processing function.
  286. *
  287. * The function call srtp_unprotect(ctx, srtp_hdr, len_ptr) verifies
  288. * the Secure RTP protection of the SRTP packet pointed to by srtp_hdr
  289. * (which has length *len_ptr), using the SRTP context ctx. If
  290. * err_status_ok is returned, then srtp_hdr points to the resulting
  291. * RTP packet and *len_ptr is the number of octets in that packet;
  292. * otherwise, no assumptions should be made about the value of either
  293. * data elements.
  294. *
  295. * The sequence numbers of the RTP packets presented to this function
  296. * need not be consecutive, but they @b must be out of order by less
  297. * than 2^15 = 32,768 packets.
  298. *
  299. * @warning This function assumes that the SRTP packet is aligned on a
  300. * 32-bit boundary.
  301. *
  302. * @param ctx is a pointer to the srtp_t which applies to the
  303. * particular packet.
  304. *
  305. * @param srtp_hdr is a pointer to the header of the SRTP packet
  306. * (before the call). after the function returns, it points to the
  307. * rtp packet if err_status_ok was returned; otherwise, the value of
  308. * the data to which it points is undefined.
  309. *
  310. * @param len_ptr is a pointer to the length in octets of the complete
  311. * srtp packet (header and body) before the function call, and of the
  312. * complete rtp packet after the call, if err_status_ok was returned.
  313. * Otherwise, the value of the data to which it points is undefined.
  314. *
  315. * @return
  316. * - err_status_ok if the RTP packet is valid.
  317. * - err_status_auth_fail if the SRTP packet failed the message
  318. * authentication check.
  319. * - err_status_replay_fail if the SRTP packet is a replay (e.g. packet has
  320. * already been processed and accepted).
  321. * - [other] if there has been an error in the cryptographic mechanisms.
  322. *
  323. */
  324. err_status_t
  325. srtp_unprotect(srtp_t ctx, void *srtp_hdr, int *len_ptr);
  326. /**
  327. * @brief srtp_create() allocates and initializes an SRTP session.
  328. * The function call srtp_create(session, policy, key) allocates and
  329. * initializes an SRTP session context, applying the given policy and
  330. * key.
  331. *
  332. * @param session is the SRTP session to which the policy is to be added.
  333. *
  334. * @param policy is the srtp_policy_t struct that describes the policy
  335. * for the session. The struct may be a single element, or it may be
  336. * the head of a list, in which case each element of the list is
  337. * processed. It may also be NULL, in which case streams should be added
  338. * later using srtp_add_stream(). The final element of the list @b must
  339. * have its `next' field set to NULL.
  340. *
  341. * @return
  342. * - err_status_ok if creation succeded.
  343. * - err_status_alloc_fail if allocation failed.
  344. * - err_status_init_fail if initialization failed.
  345. */
  346. err_status_t
  347. srtp_create(srtp_t *session, const srtp_policy_t *policy);
  348. /**
  349. * @brief srtp_add_stream() allocates and initializes an SRTP stream
  350. * within a given SRTP session.
  351. *
  352. * The function call srtp_add_stream(session, policy) allocates and
  353. * initializes a new SRTP stream within a given, previously created
  354. * session, applying the policy given as the other argument to that
  355. * stream.
  356. *
  357. * @return values:
  358. * - err_status_ok if stream creation succeded.
  359. * - err_status_alloc_fail if stream allocation failed
  360. * - err_status_init_fail if stream initialization failed.
  361. */
  362. err_status_t
  363. srtp_add_stream(srtp_t session,
  364. const srtp_policy_t *policy);
  365. /**
  366. * @brief srtp_remove_stream() deallocates an SRTP stream.
  367. *
  368. * The function call srtp_remove_stream(session, ssrc) removes
  369. * the SRTP stream with the SSRC value ssrc from the SRTP session
  370. * context given by the argument session.
  371. *
  372. * @param session is the SRTP session from which the stream
  373. * will be removed.
  374. *
  375. * @param ssrc is the SSRC value of the stream to be removed.
  376. *
  377. * @warning Wildcard SSRC values cannot be removed from a
  378. * session.
  379. *
  380. * @return
  381. * - err_status_ok if the stream deallocation succeded.
  382. * - [other] otherwise.
  383. *
  384. */
  385. err_status_t
  386. srtp_remove_stream(srtp_t session, unsigned int ssrc);
  387. /**
  388. * @brief crypto_policy_set_rtp_default() sets a crypto policy
  389. * structure to the SRTP default policy for RTP protection.
  390. *
  391. * @param p is a pointer to the policy structure to be set
  392. *
  393. * The function call crypto_policy_set_rtp_default(&p) sets the
  394. * crypto_policy_t at location p to the SRTP default policy for RTP
  395. * protection, as defined in the specification. This function is a
  396. * convenience that helps to avoid dealing directly with the policy
  397. * data structure. You are encouraged to initialize policy elements
  398. * with this function call. Doing so may allow your code to be
  399. * forward compatible with later versions of libSRTP that include more
  400. * elements in the crypto_policy_t datatype.
  401. *
  402. * @return void.
  403. *
  404. */
  405. void
  406. crypto_policy_set_rtp_default(crypto_policy_t *p);
  407. /**
  408. * @brief crypto_policy_set_rtcp_default() sets a crypto policy
  409. * structure to the SRTP default policy for RTCP protection.
  410. *
  411. * @param p is a pointer to the policy structure to be set
  412. *
  413. * The function call crypto_policy_set_rtcp_default(&p) sets the
  414. * crypto_policy_t at location p to the SRTP default policy for RTCP
  415. * protection, as defined in the specification. This function is a
  416. * convenience that helps to avoid dealing directly with the policy
  417. * data structure. You are encouraged to initialize policy elements
  418. * with this function call. Doing so may allow your code to be
  419. * forward compatible with later versions of libSRTP that include more
  420. * elements in the crypto_policy_t datatype.
  421. *
  422. * @return void.
  423. *
  424. */
  425. void
  426. crypto_policy_set_rtcp_default(crypto_policy_t *p);
  427. /**
  428. * @brief crypto_policy_set_aes_cm_128_hmac_sha1_80() sets a crypto
  429. * policy structure to the SRTP default policy for RTP protection.
  430. *
  431. * @param p is a pointer to the policy structure to be set
  432. *
  433. * The function crypto_policy_set_aes_cm_128_hmac_sha1_80() is a
  434. * synonym for crypto_policy_set_rtp_default(). It conforms to the
  435. * naming convention used in RFC 4568 (SDP Security Descriptions for
  436. * Media Streams).
  437. *
  438. * @return void.
  439. *
  440. */
  441. #define crypto_policy_set_aes_cm_128_hmac_sha1_80(p) crypto_policy_set_rtp_default(p)
  442. /**
  443. * @brief crypto_policy_set_aes_cm_128_hmac_sha1_32() sets a crypto
  444. * policy structure to a short-authentication tag policy
  445. *
  446. * @param p is a pointer to the policy structure to be set
  447. *
  448. * The function call crypto_policy_set_aes_cm_128_hmac_sha1_32(&p)
  449. * sets the crypto_policy_t at location p to use policy
  450. * AES_CM_128_HMAC_SHA1_32 as defined in RFC 4568.
  451. * This policy uses AES-128
  452. * Counter Mode encryption and HMAC-SHA1 authentication, with an
  453. * authentication tag that is only 32 bits long. This length is
  454. * considered adequate only for protecting audio and video media that
  455. * use a stateless playback function. See Section 7.5 of RFC 3711
  456. * (http://www.ietf.org/rfc/rfc3711.txt).
  457. *
  458. * This function is a convenience that helps to avoid dealing directly
  459. * with the policy data structure. You are encouraged to initialize
  460. * policy elements with this function call. Doing so may allow your
  461. * code to be forward compatible with later versions of libSRTP that
  462. * include more elements in the crypto_policy_t datatype.
  463. *
  464. * @warning This crypto policy is intended for use in SRTP, but not in
  465. * SRTCP. It is recommended that a policy that uses longer
  466. * authentication tags be used for SRTCP. See Section 7.5 of RFC 3711
  467. * (http://www.ietf.org/rfc/rfc3711.txt).
  468. *
  469. * @return void.
  470. *
  471. */
  472. void
  473. crypto_policy_set_aes_cm_128_hmac_sha1_32(crypto_policy_t *p);
  474. /**
  475. * @brief crypto_policy_set_aes_cm_128_null_auth() sets a crypto
  476. * policy structure to an encryption-only policy
  477. *
  478. * @param p is a pointer to the policy structure to be set
  479. *
  480. * The function call crypto_policy_set_aes_cm_128_null_auth(&p) sets
  481. * the crypto_policy_t at location p to use the SRTP default cipher
  482. * (AES-128 Counter Mode), but to use no authentication method. This
  483. * policy is NOT RECOMMENDED unless it is unavoidable; see Section 7.5
  484. * of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
  485. *
  486. * This function is a convenience that helps to avoid dealing directly
  487. * with the policy data structure. You are encouraged to initialize
  488. * policy elements with this function call. Doing so may allow your
  489. * code to be forward compatible with later versions of libSRTP that
  490. * include more elements in the crypto_policy_t datatype.
  491. *
  492. * @warning This policy is NOT RECOMMENDED for SRTP unless it is
  493. * unavoidable, and it is NOT RECOMMENDED at all for SRTCP; see
  494. * Section 7.5 of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
  495. *
  496. * @return void.
  497. *
  498. */
  499. void
  500. crypto_policy_set_aes_cm_128_null_auth(crypto_policy_t *p);
  501. /**
  502. * @brief crypto_policy_set_null_cipher_hmac_sha1_80() sets a crypto
  503. * policy structure to an authentication-only policy
  504. *
  505. * @param p is a pointer to the policy structure to be set
  506. *
  507. * The function call crypto_policy_set_null_cipher_hmac_sha1_80(&p)
  508. * sets the crypto_policy_t at location p to use HMAC-SHA1 with an 80
  509. * bit authentication tag to provide message authentication, but to
  510. * use no encryption. This policy is NOT RECOMMENDED for SRTP unless
  511. * there is a requirement to forego encryption.
  512. *
  513. * This function is a convenience that helps to avoid dealing directly
  514. * with the policy data structure. You are encouraged to initialize
  515. * policy elements with this function call. Doing so may allow your
  516. * code to be forward compatible with later versions of libSRTP that
  517. * include more elements in the crypto_policy_t datatype.
  518. *
  519. * @warning This policy is NOT RECOMMENDED for SRTP unless there is a
  520. * requirement to forego encryption.
  521. *
  522. * @return void.
  523. *
  524. */
  525. void
  526. crypto_policy_set_null_cipher_hmac_sha1_80(crypto_policy_t *p);
  527. /**
  528. * @brief crypto_policy_set_aes_cm_256_hmac_sha1_80() sets a crypto
  529. * policy structure to a encryption and authentication policy using AES-256
  530. * for RTP protection.
  531. *
  532. * @param p is a pointer to the policy structure to be set
  533. *
  534. * The function call crypto_policy_set_aes_cm_256_hmac_sha1_80(&p)
  535. * sets the crypto_policy_t at location p to use policy
  536. * AES_CM_256_HMAC_SHA1_80 as defined in
  537. * draft-ietf-avt-srtp-big-aes-03.txt. This policy uses AES-256
  538. * Counter Mode encryption and HMAC-SHA1 authentication, with an 80 bit
  539. * authentication tag.
  540. *
  541. * This function is a convenience that helps to avoid dealing directly
  542. * with the policy data structure. You are encouraged to initialize
  543. * policy elements with this function call. Doing so may allow your
  544. * code to be forward compatible with later versions of libSRTP that
  545. * include more elements in the crypto_policy_t datatype.
  546. *
  547. * @return void.
  548. *
  549. */
  550. void crypto_policy_set_aes_cm_256_hmac_sha1_80(crypto_policy_t *p);
  551. /**
  552. * @brief crypto_policy_set_aes_cm_256_hmac_sha1_32() sets a crypto
  553. * policy structure to a short-authentication tag policy using AES-256
  554. * encryption.
  555. *
  556. * @param p is a pointer to the policy structure to be set
  557. *
  558. * The function call crypto_policy_set_aes_cm_256_hmac_sha1_32(&p)
  559. * sets the crypto_policy_t at location p to use policy
  560. * AES_CM_256_HMAC_SHA1_32 as defined in
  561. * draft-ietf-avt-srtp-big-aes-03.txt. This policy uses AES-256
  562. * Counter Mode encryption and HMAC-SHA1 authentication, with an
  563. * authentication tag that is only 32 bits long. This length is
  564. * considered adequate only for protecting audio and video media that
  565. * use a stateless playback function. See Section 7.5 of RFC 3711
  566. * (http://www.ietf.org/rfc/rfc3711.txt).
  567. *
  568. * This function is a convenience that helps to avoid dealing directly
  569. * with the policy data structure. You are encouraged to initialize
  570. * policy elements with this function call. Doing so may allow your
  571. * code to be forward compatible with later versions of libSRTP that
  572. * include more elements in the crypto_policy_t datatype.
  573. *
  574. * @warning This crypto policy is intended for use in SRTP, but not in
  575. * SRTCP. It is recommended that a policy that uses longer
  576. * authentication tags be used for SRTCP. See Section 7.5 of RFC 3711
  577. * (http://www.ietf.org/rfc/rfc3711.txt).
  578. *
  579. * @return void.
  580. *
  581. */
  582. void
  583. crypto_policy_set_aes_cm_256_hmac_sha1_32(crypto_policy_t *p);
  584. /**
  585. * @brief srtp_dealloc() deallocates storage for an SRTP session
  586. * context.
  587. *
  588. * The function call srtp_dealloc(s) deallocates storage for the
  589. * SRTP session context s. This function should be called no more
  590. * than one time for each of the contexts allocated by the function
  591. * srtp_create().
  592. *
  593. * @param s is the srtp_t for the session to be deallocated.
  594. *
  595. * @return
  596. * - err_status_ok if there no problems.
  597. * - err_status_dealloc_fail a memory deallocation failure occured.
  598. */
  599. err_status_t
  600. srtp_dealloc(srtp_t s);
  601. /*
  602. * @brief identifies a particular SRTP profile
  603. *
  604. * An srtp_profile_t enumeration is used to identify a particular SRTP
  605. * profile (that is, a set of algorithms and parameters). These
  606. * profiles are defined in the DTLS-SRTP draft.
  607. */
  608. typedef enum {
  609. srtp_profile_reserved = 0,
  610. srtp_profile_aes128_cm_sha1_80 = 1,
  611. srtp_profile_aes128_cm_sha1_32 = 2,
  612. srtp_profile_aes256_cm_sha1_80 = 3,
  613. srtp_profile_aes256_cm_sha1_32 = 4,
  614. srtp_profile_null_sha1_80 = 5,
  615. srtp_profile_null_sha1_32 = 6,
  616. } srtp_profile_t;
  617. /**
  618. * @brief crypto_policy_set_from_profile_for_rtp() sets a crypto policy
  619. * structure to the appropriate value for RTP based on an srtp_profile_t
  620. *
  621. * @param p is a pointer to the policy structure to be set
  622. *
  623. * The function call crypto_policy_set_rtp_default(&policy, profile)
  624. * sets the crypto_policy_t at location policy to the policy for RTP
  625. * protection, as defined by the srtp_profile_t profile.
  626. *
  627. * This function is a convenience that helps to avoid dealing directly
  628. * with the policy data structure. You are encouraged to initialize
  629. * policy elements with this function call. Doing so may allow your
  630. * code to be forward compatible with later versions of libSRTP that
  631. * include more elements in the crypto_policy_t datatype.
  632. *
  633. * @return values
  634. * - err_status_ok no problems were encountered
  635. * - err_status_bad_param the profile is not supported
  636. *
  637. */
  638. err_status_t
  639. crypto_policy_set_from_profile_for_rtp(crypto_policy_t *policy,
  640. srtp_profile_t profile);
  641. /**
  642. * @brief crypto_policy_set_from_profile_for_rtcp() sets a crypto policy
  643. * structure to the appropriate value for RTCP based on an srtp_profile_t
  644. *
  645. * @param p is a pointer to the policy structure to be set
  646. *
  647. * The function call crypto_policy_set_rtcp_default(&policy, profile)
  648. * sets the crypto_policy_t at location policy to the policy for RTCP
  649. * protection, as defined by the srtp_profile_t profile.
  650. *
  651. * This function is a convenience that helps to avoid dealing directly
  652. * with the policy data structure. You are encouraged to initialize
  653. * policy elements with this function call. Doing so may allow your
  654. * code to be forward compatible with later versions of libSRTP that
  655. * include more elements in the crypto_policy_t datatype.
  656. *
  657. * @return values
  658. * - err_status_ok no problems were encountered
  659. * - err_status_bad_param the profile is not supported
  660. *
  661. */
  662. err_status_t
  663. crypto_policy_set_from_profile_for_rtcp(crypto_policy_t *policy,
  664. srtp_profile_t profile);
  665. /**
  666. * @brief returns the master key length for a given SRTP profile
  667. */
  668. unsigned int
  669. srtp_profile_get_master_key_length(srtp_profile_t profile);
  670. /**
  671. * @brief returns the master salt length for a given SRTP profile
  672. */
  673. unsigned int
  674. srtp_profile_get_master_salt_length(srtp_profile_t profile);
  675. /**
  676. * @brief appends the salt to the key
  677. *
  678. * The function call append_salt_to_key(k, klen, s, slen)
  679. * copies the string s to the location at klen bytes following
  680. * the location k.
  681. *
  682. * @warning There must be at least bytes_in_salt + bytes_in_key bytes
  683. * available at the location pointed to by key.
  684. *
  685. */
  686. void
  687. append_salt_to_key(unsigned char *key, unsigned int bytes_in_key,
  688. unsigned char *salt, unsigned int bytes_in_salt);
  689. /**
  690. * @}
  691. */
  692. /**
  693. * @defgroup SRTCP Secure RTCP
  694. * @ingroup SRTP
  695. *
  696. * @brief Secure RTCP functions are used to protect RTCP traffic.
  697. *
  698. * RTCP is the control protocol for RTP. libSRTP protects RTCP
  699. * traffic in much the same way as it does RTP traffic. The function
  700. * srtp_protect_rtcp() applies cryptographic protections to outbound
  701. * RTCP packets, and srtp_unprotect_rtcp() verifies the protections on
  702. * inbound RTCP packets.
  703. *
  704. * A note on the naming convention: srtp_protect_rtcp() has an srtp_t
  705. * as its first argument, and thus has `srtp_' as its prefix. The
  706. * trailing `_rtcp' indicates the protocol on which it acts.
  707. *
  708. * @{
  709. */
  710. /**
  711. * @brief srtp_protect_rtcp() is the Secure RTCP sender-side packet
  712. * processing function.
  713. *
  714. * The function call srtp_protect_rtcp(ctx, rtp_hdr, len_ptr) applies
  715. * SRTCP protection to the RTCP packet rtcp_hdr (which has length
  716. * *len_ptr) using the SRTP session context ctx. If err_status_ok is
  717. * returned, then rtp_hdr points to the resulting SRTCP packet and
  718. * *len_ptr is the number of octets in that packet; otherwise, no
  719. * assumptions should be made about the value of either data elements.
  720. *
  721. * @warning This function assumes that it can write the authentication
  722. * tag into the location in memory immediately following the RTCP
  723. * packet, and assumes that the RTCP packet is aligned on a 32-bit
  724. * boundary.
  725. *
  726. * @param ctx is the SRTP context to use in processing the packet.
  727. *
  728. * @param rtcp_hdr is a pointer to the RTCP packet (before the call); after
  729. * the function returns, it points to the srtp packet.
  730. *
  731. * @param pkt_octet_len is a pointer to the length in octets of the
  732. * complete RTCP packet (header and body) before the function call,
  733. * and of the complete SRTCP packet after the call, if err_status_ok
  734. * was returned. Otherwise, the value of the data to which it points
  735. * is undefined.
  736. *
  737. * @return
  738. * - err_status_ok if there were no problems.
  739. * - [other] if there was a failure in
  740. * the cryptographic mechanisms.
  741. */
  742. err_status_t
  743. srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len);
  744. /**
  745. * @brief srtp_unprotect_rtcp() is the Secure RTCP receiver-side packet
  746. * processing function.
  747. *
  748. * The function call srtp_unprotect_rtcp(ctx, srtp_hdr, len_ptr)
  749. * verifies the Secure RTCP protection of the SRTCP packet pointed to
  750. * by srtcp_hdr (which has length *len_ptr), using the SRTP session
  751. * context ctx. If err_status_ok is returned, then srtcp_hdr points
  752. * to the resulting RTCP packet and *len_ptr is the number of octets
  753. * in that packet; otherwise, no assumptions should be made about the
  754. * value of either data elements.
  755. *
  756. * @warning This function assumes that the SRTCP packet is aligned on a
  757. * 32-bit boundary.
  758. *
  759. * @param ctx is a pointer to the srtp_t which applies to the
  760. * particular packet.
  761. *
  762. * @param srtcp_hdr is a pointer to the header of the SRTCP packet
  763. * (before the call). After the function returns, it points to the
  764. * rtp packet if err_status_ok was returned; otherwise, the value of
  765. * the data to which it points is undefined.
  766. *
  767. * @param pkt_octet_len is a pointer to the length in octets of the
  768. * complete SRTCP packet (header and body) before the function call,
  769. * and of the complete rtp packet after the call, if err_status_ok was
  770. * returned. Otherwise, the value of the data to which it points is
  771. * undefined.
  772. *
  773. * @return
  774. * - err_status_ok if the RTCP packet is valid.
  775. * - err_status_auth_fail if the SRTCP packet failed the message
  776. * authentication check.
  777. * - err_status_replay_fail if the SRTCP packet is a replay (e.g. has
  778. * already been processed and accepted).
  779. * - [other] if there has been an error in the cryptographic mechanisms.
  780. *
  781. */
  782. err_status_t
  783. srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len);
  784. /**
  785. * @}
  786. */
  787. /**
  788. * @defgroup SRTPevents SRTP events and callbacks
  789. * @ingroup SRTP
  790. *
  791. * @brief libSRTP can use a user-provided callback function to
  792. * handle events.
  793. *
  794. *
  795. * libSRTP allows a user to provide a callback function to handle
  796. * events that need to be dealt with outside of the data plane (see
  797. * the enum srtp_event_t for a description of these events). Dealing
  798. * with these events is not a strict necessity; they are not
  799. * security-critical, but the application may suffer if they are not
  800. * handled. The function srtp_set_event_handler() is used to provide
  801. * the callback function.
  802. *
  803. * A default event handler that merely reports on the events as they
  804. * happen is included. It is also possible to set the event handler
  805. * function to NULL, in which case all events will just be silently
  806. * ignored.
  807. *
  808. * @{
  809. */
  810. /**
  811. * @brief srtp_event_t defines events that need to be handled
  812. *
  813. * The enum srtp_event_t defines events that need to be handled
  814. * outside the `data plane', such as SSRC collisions and
  815. * key expirations.
  816. *
  817. * When a key expires or the maximum number of packets has been
  818. * reached, an SRTP stream will enter an `expired' state in which no
  819. * more packets can be protected or unprotected. When this happens,
  820. * it is likely that you will want to either deallocate the stream
  821. * (using srtp_stream_dealloc()), and possibly allocate a new one.
  822. *
  823. * When an SRTP stream expires, the other streams in the same session
  824. * are unaffected, unless key sharing is used by that stream. In the
  825. * latter case, all of the streams in the session will expire.
  826. */
  827. typedef enum {
  828. event_ssrc_collision, /**<
  829. * An SSRC collision occured.
  830. */
  831. event_key_soft_limit, /**< An SRTP stream reached the soft key
  832. * usage limit and will expire soon.
  833. */
  834. event_key_hard_limit, /**< An SRTP stream reached the hard
  835. * key usage limit and has expired.
  836. */
  837. event_packet_index_limit /**< An SRTP stream reached the hard
  838. * packet limit (2^48 packets).
  839. */
  840. } srtp_event_t;
  841. /**
  842. * @brief srtp_event_data_t is the structure passed as a callback to
  843. * the event handler function
  844. *
  845. * The struct srtp_event_data_t holds the data passed to the event
  846. * handler function.
  847. */
  848. typedef struct srtp_event_data_t {
  849. srtp_t session; /**< The session in which the event happend. */
  850. srtp_stream_t stream; /**< The stream in which the event happend. */
  851. srtp_event_t event; /**< An enum indicating the type of event. */
  852. } srtp_event_data_t;
  853. /**
  854. * @brief srtp_event_handler_func_t is the function prototype for
  855. * the event handler.
  856. *
  857. * The typedef srtp_event_handler_func_t is the prototype for the
  858. * event handler function. It has as its only argument an
  859. * srtp_event_data_t which describes the event that needs to be handled.
  860. * There can only be a single, global handler for all events in
  861. * libSRTP.
  862. */
  863. typedef void (srtp_event_handler_func_t)(srtp_event_data_t *data);
  864. /**
  865. * @brief sets the event handler to the function supplied by the caller.
  866. *
  867. * The function call srtp_install_event_handler(func) sets the event
  868. * handler function to the value func. The value NULL is acceptable
  869. * as an argument; in this case, events will be ignored rather than
  870. * handled.
  871. *
  872. * @param func is a pointer to a fuction that takes an srtp_event_data_t
  873. * pointer as an argument and returns void. This function
  874. * will be used by libSRTP to handle events.
  875. */
  876. err_status_t
  877. srtp_install_event_handler(srtp_event_handler_func_t func);
  878. /**
  879. * @}
  880. */
  881. /* in host order, so outside the #if */
  882. #define SRTCP_E_BIT 0x80000000
  883. /* for byte-access */
  884. #define SRTCP_E_BYTE_BIT 0x80
  885. #define SRTCP_INDEX_MASK 0x7fffffff
  886. #ifdef __cplusplus
  887. }
  888. #endif
  889. #endif /* SRTP_H */