srtp.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. * @warning This function assumes that it can write SRTP_MAX_TRAILER_LEN
  267. * into the location in memory immediately following the RTP packet.
  268. * Callers MUST ensure that this much writable memory is available in
  269. * the buffer that holds the RTP packet.
  270. *
  271. * @param ctx is the SRTP context to use in processing the packet.
  272. *
  273. * @param rtp_hdr is a pointer to the RTP packet (before the call); after
  274. * the function returns, it points to the srtp packet.
  275. *
  276. * @param len_ptr is a pointer to the length in octets of the complete
  277. * RTP packet (header and body) before the function call, and of the
  278. * complete SRTP packet after the call, if err_status_ok was returned.
  279. * Otherwise, the value of the data to which it points is undefined.
  280. *
  281. * @return
  282. * - err_status_ok no problems
  283. * - err_status_replay_fail rtp sequence number was non-increasing
  284. * - @e other failure in cryptographic mechanisms
  285. */
  286. err_status_t
  287. srtp_protect(srtp_t ctx, void *rtp_hdr, int *len_ptr);
  288. /**
  289. * @brief srtp_unprotect() is the Secure RTP receiver-side packet
  290. * processing function.
  291. *
  292. * The function call srtp_unprotect(ctx, srtp_hdr, len_ptr) verifies
  293. * the Secure RTP protection of the SRTP packet pointed to by srtp_hdr
  294. * (which has length *len_ptr), using the SRTP context ctx. If
  295. * err_status_ok is returned, then srtp_hdr points to the resulting
  296. * RTP packet and *len_ptr is the number of octets in that packet;
  297. * otherwise, no assumptions should be made about the value of either
  298. * data elements.
  299. *
  300. * The sequence numbers of the RTP packets presented to this function
  301. * need not be consecutive, but they @b must be out of order by less
  302. * than 2^15 = 32,768 packets.
  303. *
  304. * @warning This function assumes that the SRTP packet is aligned on a
  305. * 32-bit boundary.
  306. *
  307. * @param ctx is a pointer to the srtp_t which applies to the
  308. * particular packet.
  309. *
  310. * @param srtp_hdr is a pointer to the header of the SRTP packet
  311. * (before the call). after the function returns, it points to the
  312. * rtp packet if err_status_ok was returned; otherwise, the value of
  313. * the data to which it points is undefined.
  314. *
  315. * @param len_ptr is a pointer to the length in octets of the complete
  316. * srtp packet (header and body) before the function call, and of the
  317. * complete rtp packet after the call, if err_status_ok was returned.
  318. * Otherwise, the value of the data to which it points is undefined.
  319. *
  320. * @return
  321. * - err_status_ok if the RTP packet is valid.
  322. * - err_status_auth_fail if the SRTP packet failed the message
  323. * authentication check.
  324. * - err_status_replay_fail if the SRTP packet is a replay (e.g. packet has
  325. * already been processed and accepted).
  326. * - [other] if there has been an error in the cryptographic mechanisms.
  327. *
  328. */
  329. err_status_t
  330. srtp_unprotect(srtp_t ctx, void *srtp_hdr, int *len_ptr);
  331. /**
  332. * @brief srtp_create() allocates and initializes an SRTP session.
  333. * The function call srtp_create(session, policy, key) allocates and
  334. * initializes an SRTP session context, applying the given policy and
  335. * key.
  336. *
  337. * @param session is the SRTP session to which the policy is to be added.
  338. *
  339. * @param policy is the srtp_policy_t struct that describes the policy
  340. * for the session. The struct may be a single element, or it may be
  341. * the head of a list, in which case each element of the list is
  342. * processed. It may also be NULL, in which case streams should be added
  343. * later using srtp_add_stream(). The final element of the list @b must
  344. * have its `next' field set to NULL.
  345. *
  346. * @return
  347. * - err_status_ok if creation succeded.
  348. * - err_status_alloc_fail if allocation failed.
  349. * - err_status_init_fail if initialization failed.
  350. */
  351. err_status_t
  352. srtp_create(srtp_t *session, const srtp_policy_t *policy);
  353. /**
  354. * @brief srtp_add_stream() allocates and initializes an SRTP stream
  355. * within a given SRTP session.
  356. *
  357. * The function call srtp_add_stream(session, policy) allocates and
  358. * initializes a new SRTP stream within a given, previously created
  359. * session, applying the policy given as the other argument to that
  360. * stream.
  361. *
  362. * @return values:
  363. * - err_status_ok if stream creation succeded.
  364. * - err_status_alloc_fail if stream allocation failed
  365. * - err_status_init_fail if stream initialization failed.
  366. */
  367. err_status_t
  368. srtp_add_stream(srtp_t session,
  369. const srtp_policy_t *policy);
  370. /**
  371. * @brief srtp_remove_stream() deallocates an SRTP stream.
  372. *
  373. * The function call srtp_remove_stream(session, ssrc) removes
  374. * the SRTP stream with the SSRC value ssrc from the SRTP session
  375. * context given by the argument session.
  376. *
  377. * @param session is the SRTP session from which the stream
  378. * will be removed.
  379. *
  380. * @param ssrc is the SSRC value of the stream to be removed.
  381. *
  382. * @warning Wildcard SSRC values cannot be removed from a
  383. * session.
  384. *
  385. * @return
  386. * - err_status_ok if the stream deallocation succeded.
  387. * - [other] otherwise.
  388. *
  389. */
  390. err_status_t
  391. srtp_remove_stream(srtp_t session, unsigned int ssrc);
  392. /**
  393. * @brief crypto_policy_set_rtp_default() sets a crypto policy
  394. * structure to the SRTP default policy for RTP protection.
  395. *
  396. * @param p is a pointer to the policy structure to be set
  397. *
  398. * The function call crypto_policy_set_rtp_default(&p) sets the
  399. * crypto_policy_t at location p to the SRTP default policy for RTP
  400. * protection, as defined in the specification. This function is a
  401. * convenience that helps to avoid dealing directly with the policy
  402. * data structure. You are encouraged to initialize policy elements
  403. * with this function call. Doing so may allow your code to be
  404. * forward compatible with later versions of libSRTP that include more
  405. * elements in the crypto_policy_t datatype.
  406. *
  407. * @return void.
  408. *
  409. */
  410. void
  411. crypto_policy_set_rtp_default(crypto_policy_t *p);
  412. /**
  413. * @brief crypto_policy_set_rtcp_default() sets a crypto policy
  414. * structure to the SRTP default policy for RTCP protection.
  415. *
  416. * @param p is a pointer to the policy structure to be set
  417. *
  418. * The function call crypto_policy_set_rtcp_default(&p) sets the
  419. * crypto_policy_t at location p to the SRTP default policy for RTCP
  420. * protection, as defined in the specification. This function is a
  421. * convenience that helps to avoid dealing directly with the policy
  422. * data structure. You are encouraged to initialize policy elements
  423. * with this function call. Doing so may allow your code to be
  424. * forward compatible with later versions of libSRTP that include more
  425. * elements in the crypto_policy_t datatype.
  426. *
  427. * @return void.
  428. *
  429. */
  430. void
  431. crypto_policy_set_rtcp_default(crypto_policy_t *p);
  432. /**
  433. * @brief crypto_policy_set_aes_cm_128_hmac_sha1_80() sets a crypto
  434. * policy structure to the SRTP default policy for RTP protection.
  435. *
  436. * @param p is a pointer to the policy structure to be set
  437. *
  438. * The function crypto_policy_set_aes_cm_128_hmac_sha1_80() is a
  439. * synonym for crypto_policy_set_rtp_default(). It conforms to the
  440. * naming convention used in RFC 4568 (SDP Security Descriptions for
  441. * Media Streams).
  442. *
  443. * @return void.
  444. *
  445. */
  446. #define crypto_policy_set_aes_cm_128_hmac_sha1_80(p) crypto_policy_set_rtp_default(p)
  447. /**
  448. * @brief crypto_policy_set_aes_cm_128_hmac_sha1_32() sets a crypto
  449. * policy structure to a short-authentication tag policy
  450. *
  451. * @param p is a pointer to the policy structure to be set
  452. *
  453. * The function call crypto_policy_set_aes_cm_128_hmac_sha1_32(&p)
  454. * sets the crypto_policy_t at location p to use policy
  455. * AES_CM_128_HMAC_SHA1_32 as defined in RFC 4568.
  456. * This policy uses AES-128
  457. * Counter Mode encryption and HMAC-SHA1 authentication, with an
  458. * authentication tag that is only 32 bits long. This length is
  459. * considered adequate only for protecting audio and video media that
  460. * use a stateless playback function. See Section 7.5 of RFC 3711
  461. * (http://www.ietf.org/rfc/rfc3711.txt).
  462. *
  463. * This function is a convenience that helps to avoid dealing directly
  464. * with the policy data structure. You are encouraged to initialize
  465. * policy elements with this function call. Doing so may allow your
  466. * code to be forward compatible with later versions of libSRTP that
  467. * include more elements in the crypto_policy_t datatype.
  468. *
  469. * @warning This crypto policy is intended for use in SRTP, but not in
  470. * SRTCP. It is recommended that a policy that uses longer
  471. * authentication tags be used for SRTCP. See Section 7.5 of RFC 3711
  472. * (http://www.ietf.org/rfc/rfc3711.txt).
  473. *
  474. * @return void.
  475. *
  476. */
  477. void
  478. crypto_policy_set_aes_cm_128_hmac_sha1_32(crypto_policy_t *p);
  479. /**
  480. * @brief crypto_policy_set_aes_cm_128_null_auth() sets a crypto
  481. * policy structure to an encryption-only policy
  482. *
  483. * @param p is a pointer to the policy structure to be set
  484. *
  485. * The function call crypto_policy_set_aes_cm_128_null_auth(&p) sets
  486. * the crypto_policy_t at location p to use the SRTP default cipher
  487. * (AES-128 Counter Mode), but to use no authentication method. This
  488. * policy is NOT RECOMMENDED unless it is unavoidable; see Section 7.5
  489. * of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
  490. *
  491. * This function is a convenience that helps to avoid dealing directly
  492. * with the policy data structure. You are encouraged to initialize
  493. * policy elements with this function call. Doing so may allow your
  494. * code to be forward compatible with later versions of libSRTP that
  495. * include more elements in the crypto_policy_t datatype.
  496. *
  497. * @warning This policy is NOT RECOMMENDED for SRTP unless it is
  498. * unavoidable, and it is NOT RECOMMENDED at all for SRTCP; see
  499. * Section 7.5 of RFC 3711 (http://www.ietf.org/rfc/rfc3711.txt).
  500. *
  501. * @return void.
  502. *
  503. */
  504. void
  505. crypto_policy_set_aes_cm_128_null_auth(crypto_policy_t *p);
  506. /**
  507. * @brief crypto_policy_set_null_cipher_hmac_sha1_80() sets a crypto
  508. * policy structure to an authentication-only policy
  509. *
  510. * @param p is a pointer to the policy structure to be set
  511. *
  512. * The function call crypto_policy_set_null_cipher_hmac_sha1_80(&p)
  513. * sets the crypto_policy_t at location p to use HMAC-SHA1 with an 80
  514. * bit authentication tag to provide message authentication, but to
  515. * use no encryption. This policy is NOT RECOMMENDED for SRTP unless
  516. * there is a requirement to forego encryption.
  517. *
  518. * This function is a convenience that helps to avoid dealing directly
  519. * with the policy data structure. You are encouraged to initialize
  520. * policy elements with this function call. Doing so may allow your
  521. * code to be forward compatible with later versions of libSRTP that
  522. * include more elements in the crypto_policy_t datatype.
  523. *
  524. * @warning This policy is NOT RECOMMENDED for SRTP unless there is a
  525. * requirement to forego encryption.
  526. *
  527. * @return void.
  528. *
  529. */
  530. void
  531. crypto_policy_set_null_cipher_hmac_sha1_80(crypto_policy_t *p);
  532. /**
  533. * @brief crypto_policy_set_aes_cm_256_hmac_sha1_80() sets a crypto
  534. * policy structure to a encryption and authentication policy using AES-256
  535. * for RTP protection.
  536. *
  537. * @param p is a pointer to the policy structure to be set
  538. *
  539. * The function call crypto_policy_set_aes_cm_256_hmac_sha1_80(&p)
  540. * sets the crypto_policy_t at location p to use policy
  541. * AES_CM_256_HMAC_SHA1_80 as defined in
  542. * draft-ietf-avt-srtp-big-aes-03.txt. This policy uses AES-256
  543. * Counter Mode encryption and HMAC-SHA1 authentication, with an 80 bit
  544. * authentication tag.
  545. *
  546. * This function is a convenience that helps to avoid dealing directly
  547. * with the policy data structure. You are encouraged to initialize
  548. * policy elements with this function call. Doing so may allow your
  549. * code to be forward compatible with later versions of libSRTP that
  550. * include more elements in the crypto_policy_t datatype.
  551. *
  552. * @return void.
  553. *
  554. */
  555. void crypto_policy_set_aes_cm_256_hmac_sha1_80(crypto_policy_t *p);
  556. /**
  557. * @brief crypto_policy_set_aes_cm_256_hmac_sha1_32() sets a crypto
  558. * policy structure to a short-authentication tag policy using AES-256
  559. * encryption.
  560. *
  561. * @param p is a pointer to the policy structure to be set
  562. *
  563. * The function call crypto_policy_set_aes_cm_256_hmac_sha1_32(&p)
  564. * sets the crypto_policy_t at location p to use policy
  565. * AES_CM_256_HMAC_SHA1_32 as defined in
  566. * draft-ietf-avt-srtp-big-aes-03.txt. This policy uses AES-256
  567. * Counter Mode encryption and HMAC-SHA1 authentication, with an
  568. * authentication tag that is only 32 bits long. This length is
  569. * considered adequate only for protecting audio and video media that
  570. * use a stateless playback function. See Section 7.5 of RFC 3711
  571. * (http://www.ietf.org/rfc/rfc3711.txt).
  572. *
  573. * This function is a convenience that helps to avoid dealing directly
  574. * with the policy data structure. You are encouraged to initialize
  575. * policy elements with this function call. Doing so may allow your
  576. * code to be forward compatible with later versions of libSRTP that
  577. * include more elements in the crypto_policy_t datatype.
  578. *
  579. * @warning This crypto policy is intended for use in SRTP, but not in
  580. * SRTCP. It is recommended that a policy that uses longer
  581. * authentication tags be used for SRTCP. See Section 7.5 of RFC 3711
  582. * (http://www.ietf.org/rfc/rfc3711.txt).
  583. *
  584. * @return void.
  585. *
  586. */
  587. void
  588. crypto_policy_set_aes_cm_256_hmac_sha1_32(crypto_policy_t *p);
  589. /**
  590. * @brief srtp_dealloc() deallocates storage for an SRTP session
  591. * context.
  592. *
  593. * The function call srtp_dealloc(s) deallocates storage for the
  594. * SRTP session context s. This function should be called no more
  595. * than one time for each of the contexts allocated by the function
  596. * srtp_create().
  597. *
  598. * @param s is the srtp_t for the session to be deallocated.
  599. *
  600. * @return
  601. * - err_status_ok if there no problems.
  602. * - err_status_dealloc_fail a memory deallocation failure occured.
  603. */
  604. err_status_t
  605. srtp_dealloc(srtp_t s);
  606. /*
  607. * @brief identifies a particular SRTP profile
  608. *
  609. * An srtp_profile_t enumeration is used to identify a particular SRTP
  610. * profile (that is, a set of algorithms and parameters). These
  611. * profiles are defined in the DTLS-SRTP draft.
  612. */
  613. typedef enum {
  614. srtp_profile_reserved = 0,
  615. srtp_profile_aes128_cm_sha1_80 = 1,
  616. srtp_profile_aes128_cm_sha1_32 = 2,
  617. srtp_profile_aes256_cm_sha1_80 = 3,
  618. srtp_profile_aes256_cm_sha1_32 = 4,
  619. srtp_profile_null_sha1_80 = 5,
  620. srtp_profile_null_sha1_32 = 6,
  621. } srtp_profile_t;
  622. /**
  623. * @brief crypto_policy_set_from_profile_for_rtp() sets a crypto policy
  624. * structure to the appropriate value for RTP based on an srtp_profile_t
  625. *
  626. * @param p is a pointer to the policy structure to be set
  627. *
  628. * The function call crypto_policy_set_rtp_default(&policy, profile)
  629. * sets the crypto_policy_t at location policy to the policy for RTP
  630. * protection, as defined by the srtp_profile_t profile.
  631. *
  632. * This function is a convenience that helps to avoid dealing directly
  633. * with the policy data structure. You are encouraged to initialize
  634. * policy elements with this function call. Doing so may allow your
  635. * code to be forward compatible with later versions of libSRTP that
  636. * include more elements in the crypto_policy_t datatype.
  637. *
  638. * @return values
  639. * - err_status_ok no problems were encountered
  640. * - err_status_bad_param the profile is not supported
  641. *
  642. */
  643. err_status_t
  644. crypto_policy_set_from_profile_for_rtp(crypto_policy_t *policy,
  645. srtp_profile_t profile);
  646. /**
  647. * @brief crypto_policy_set_from_profile_for_rtcp() sets a crypto policy
  648. * structure to the appropriate value for RTCP based on an srtp_profile_t
  649. *
  650. * @param p is a pointer to the policy structure to be set
  651. *
  652. * The function call crypto_policy_set_rtcp_default(&policy, profile)
  653. * sets the crypto_policy_t at location policy to the policy for RTCP
  654. * protection, as defined by the srtp_profile_t profile.
  655. *
  656. * This function is a convenience that helps to avoid dealing directly
  657. * with the policy data structure. You are encouraged to initialize
  658. * policy elements with this function call. Doing so may allow your
  659. * code to be forward compatible with later versions of libSRTP that
  660. * include more elements in the crypto_policy_t datatype.
  661. *
  662. * @return values
  663. * - err_status_ok no problems were encountered
  664. * - err_status_bad_param the profile is not supported
  665. *
  666. */
  667. err_status_t
  668. crypto_policy_set_from_profile_for_rtcp(crypto_policy_t *policy,
  669. srtp_profile_t profile);
  670. /**
  671. * @brief returns the master key length for a given SRTP profile
  672. */
  673. unsigned int
  674. srtp_profile_get_master_key_length(srtp_profile_t profile);
  675. /**
  676. * @brief returns the master salt length for a given SRTP profile
  677. */
  678. unsigned int
  679. srtp_profile_get_master_salt_length(srtp_profile_t profile);
  680. /**
  681. * @brief appends the salt to the key
  682. *
  683. * The function call append_salt_to_key(k, klen, s, slen)
  684. * copies the string s to the location at klen bytes following
  685. * the location k.
  686. *
  687. * @warning There must be at least bytes_in_salt + bytes_in_key bytes
  688. * available at the location pointed to by key.
  689. *
  690. */
  691. void
  692. append_salt_to_key(unsigned char *key, unsigned int bytes_in_key,
  693. unsigned char *salt, unsigned int bytes_in_salt);
  694. /**
  695. * @}
  696. */
  697. /**
  698. * @defgroup SRTCP Secure RTCP
  699. * @ingroup SRTP
  700. *
  701. * @brief Secure RTCP functions are used to protect RTCP traffic.
  702. *
  703. * RTCP is the control protocol for RTP. libSRTP protects RTCP
  704. * traffic in much the same way as it does RTP traffic. The function
  705. * srtp_protect_rtcp() applies cryptographic protections to outbound
  706. * RTCP packets, and srtp_unprotect_rtcp() verifies the protections on
  707. * inbound RTCP packets.
  708. *
  709. * A note on the naming convention: srtp_protect_rtcp() has an srtp_t
  710. * as its first argument, and thus has `srtp_' as its prefix. The
  711. * trailing `_rtcp' indicates the protocol on which it acts.
  712. *
  713. * @{
  714. */
  715. /**
  716. * @brief srtp_protect_rtcp() is the Secure RTCP sender-side packet
  717. * processing function.
  718. *
  719. * The function call srtp_protect_rtcp(ctx, rtp_hdr, len_ptr) applies
  720. * SRTCP protection to the RTCP packet rtcp_hdr (which has length
  721. * *len_ptr) using the SRTP session context ctx. If err_status_ok is
  722. * returned, then rtp_hdr points to the resulting SRTCP packet and
  723. * *len_ptr is the number of octets in that packet; otherwise, no
  724. * assumptions should be made about the value of either data elements.
  725. *
  726. * @warning This function assumes that it can write the authentication
  727. * tag into the location in memory immediately following the RTCP
  728. * packet, and assumes that the RTCP packet is aligned on a 32-bit
  729. * boundary.
  730. *
  731. * @warning This function assumes that it can write SRTP_MAX_TRAILER_LEN+4
  732. * into the location in memory immediately following the RTCP packet.
  733. * Callers MUST ensure that this much writable memory is available in
  734. * the buffer that holds the RTCP packet.
  735. *
  736. * @param ctx is the SRTP context to use in processing the packet.
  737. *
  738. * @param rtcp_hdr is a pointer to the RTCP packet (before the call); after
  739. * the function returns, it points to the srtp packet.
  740. *
  741. * @param pkt_octet_len is a pointer to the length in octets of the
  742. * complete RTCP packet (header and body) before the function call,
  743. * and of the complete SRTCP packet after the call, if err_status_ok
  744. * was returned. Otherwise, the value of the data to which it points
  745. * is undefined.
  746. *
  747. * @return
  748. * - err_status_ok if there were no problems.
  749. * - [other] if there was a failure in
  750. * the cryptographic mechanisms.
  751. */
  752. err_status_t
  753. srtp_protect_rtcp(srtp_t ctx, void *rtcp_hdr, int *pkt_octet_len);
  754. /**
  755. * @brief srtp_unprotect_rtcp() is the Secure RTCP receiver-side packet
  756. * processing function.
  757. *
  758. * The function call srtp_unprotect_rtcp(ctx, srtp_hdr, len_ptr)
  759. * verifies the Secure RTCP protection of the SRTCP packet pointed to
  760. * by srtcp_hdr (which has length *len_ptr), using the SRTP session
  761. * context ctx. If err_status_ok is returned, then srtcp_hdr points
  762. * to the resulting RTCP packet and *len_ptr is the number of octets
  763. * in that packet; otherwise, no assumptions should be made about the
  764. * value of either data elements.
  765. *
  766. * @warning This function assumes that the SRTCP packet is aligned on a
  767. * 32-bit boundary.
  768. *
  769. * @param ctx is a pointer to the srtp_t which applies to the
  770. * particular packet.
  771. *
  772. * @param srtcp_hdr is a pointer to the header of the SRTCP packet
  773. * (before the call). After the function returns, it points to the
  774. * rtp packet if err_status_ok was returned; otherwise, the value of
  775. * the data to which it points is undefined.
  776. *
  777. * @param pkt_octet_len is a pointer to the length in octets of the
  778. * complete SRTCP packet (header and body) before the function call,
  779. * and of the complete rtp packet after the call, if err_status_ok was
  780. * returned. Otherwise, the value of the data to which it points is
  781. * undefined.
  782. *
  783. * @return
  784. * - err_status_ok if the RTCP packet is valid.
  785. * - err_status_auth_fail if the SRTCP packet failed the message
  786. * authentication check.
  787. * - err_status_replay_fail if the SRTCP packet is a replay (e.g. has
  788. * already been processed and accepted).
  789. * - [other] if there has been an error in the cryptographic mechanisms.
  790. *
  791. */
  792. err_status_t
  793. srtp_unprotect_rtcp(srtp_t ctx, void *srtcp_hdr, int *pkt_octet_len);
  794. /**
  795. * @}
  796. */
  797. /**
  798. * @defgroup SRTPevents SRTP events and callbacks
  799. * @ingroup SRTP
  800. *
  801. * @brief libSRTP can use a user-provided callback function to
  802. * handle events.
  803. *
  804. *
  805. * libSRTP allows a user to provide a callback function to handle
  806. * events that need to be dealt with outside of the data plane (see
  807. * the enum srtp_event_t for a description of these events). Dealing
  808. * with these events is not a strict necessity; they are not
  809. * security-critical, but the application may suffer if they are not
  810. * handled. The function srtp_set_event_handler() is used to provide
  811. * the callback function.
  812. *
  813. * A default event handler that merely reports on the events as they
  814. * happen is included. It is also possible to set the event handler
  815. * function to NULL, in which case all events will just be silently
  816. * ignored.
  817. *
  818. * @{
  819. */
  820. /**
  821. * @brief srtp_event_t defines events that need to be handled
  822. *
  823. * The enum srtp_event_t defines events that need to be handled
  824. * outside the `data plane', such as SSRC collisions and
  825. * key expirations.
  826. *
  827. * When a key expires or the maximum number of packets has been
  828. * reached, an SRTP stream will enter an `expired' state in which no
  829. * more packets can be protected or unprotected. When this happens,
  830. * it is likely that you will want to either deallocate the stream
  831. * (using srtp_stream_dealloc()), and possibly allocate a new one.
  832. *
  833. * When an SRTP stream expires, the other streams in the same session
  834. * are unaffected, unless key sharing is used by that stream. In the
  835. * latter case, all of the streams in the session will expire.
  836. */
  837. typedef enum {
  838. event_ssrc_collision, /**<
  839. * An SSRC collision occured.
  840. */
  841. event_key_soft_limit, /**< An SRTP stream reached the soft key
  842. * usage limit and will expire soon.
  843. */
  844. event_key_hard_limit, /**< An SRTP stream reached the hard
  845. * key usage limit and has expired.
  846. */
  847. event_packet_index_limit /**< An SRTP stream reached the hard
  848. * packet limit (2^48 packets).
  849. */
  850. } srtp_event_t;
  851. /**
  852. * @brief srtp_event_data_t is the structure passed as a callback to
  853. * the event handler function
  854. *
  855. * The struct srtp_event_data_t holds the data passed to the event
  856. * handler function.
  857. */
  858. typedef struct srtp_event_data_t {
  859. srtp_t session; /**< The session in which the event happend. */
  860. srtp_stream_t stream; /**< The stream in which the event happend. */
  861. srtp_event_t event; /**< An enum indicating the type of event. */
  862. } srtp_event_data_t;
  863. /**
  864. * @brief srtp_event_handler_func_t is the function prototype for
  865. * the event handler.
  866. *
  867. * The typedef srtp_event_handler_func_t is the prototype for the
  868. * event handler function. It has as its only argument an
  869. * srtp_event_data_t which describes the event that needs to be handled.
  870. * There can only be a single, global handler for all events in
  871. * libSRTP.
  872. */
  873. typedef void (srtp_event_handler_func_t)(srtp_event_data_t *data);
  874. /**
  875. * @brief sets the event handler to the function supplied by the caller.
  876. *
  877. * The function call srtp_install_event_handler(func) sets the event
  878. * handler function to the value func. The value NULL is acceptable
  879. * as an argument; in this case, events will be ignored rather than
  880. * handled.
  881. *
  882. * @param func is a pointer to a fuction that takes an srtp_event_data_t
  883. * pointer as an argument and returns void. This function
  884. * will be used by libSRTP to handle events.
  885. */
  886. err_status_t
  887. srtp_install_event_handler(srtp_event_handler_func_t func);
  888. /**
  889. * @}
  890. */
  891. /* in host order, so outside the #if */
  892. #define SRTCP_E_BIT 0x80000000
  893. /* for byte-access */
  894. #define SRTCP_E_BYTE_BIT 0x80
  895. #define SRTCP_INDEX_MASK 0x7fffffff
  896. #ifdef __cplusplus
  897. }
  898. #endif
  899. #endif /* SRTP_H */