ocrypto_srtp.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. * @defgroup nrf_oberon_srpt SRPT - Secure Real-Time Transport Protocol APIs
  42. * @ingroup nrf_oberon
  43. * @{
  44. * @brief Type declarations and APIs for SRTP - Secure Real-time Transport Protocol.
  45. */
  46. #ifndef OCRYPTO_SRTP_H
  47. #define OCRYPTO_SRTP_H
  48. #include <stddef.h>
  49. #include <stdint.h>
  50. #include "ocrypto_aes_key.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**
  55. * SRTP Authentication Key Size.
  56. */
  57. #define ocrypto_srtp_AuthKeySize (20)
  58. /**
  59. * SRTP Salt Size.
  60. */
  61. #define ocrypto_srtp_SaltSize (14)
  62. /**
  63. * SRTP Maximum Key Size.
  64. */
  65. #define ocrypto_srtp_MaxKeySize (ocrypto_aes256_KEY_BYTES)
  66. /**
  67. * SRTP Context.
  68. */
  69. typedef struct {
  70. /**
  71. * Key size [bytes].
  72. */
  73. uint32_t keySize;
  74. /**
  75. * Tag size [bytes].
  76. */
  77. uint32_t tagSize;
  78. /**
  79. * Session encryption key (max 256 bits).
  80. */
  81. uint8_t encrKey[ocrypto_srtp_MaxKeySize];
  82. /**
  83. * Session authentication key
  84. * 160 bits.
  85. */
  86. uint8_t authKey[ocrypto_srtp_AuthKeySize];
  87. /**
  88. * Session salt
  89. * 112 bits.
  90. */
  91. uint8_t saltKey[ocrypto_srtp_SaltSize];
  92. } ocrypto_srtp_context;
  93. /**
  94. * Setup SRTP contexts.
  95. *
  96. * @param[out] srtpContext SRTP context to be setup.
  97. * @param[out] srtcpContext SRTCP context to be setup.
  98. * @param key Master key.
  99. * @param keySize Size of the master key (16, 24, or 32 bytes)
  100. * @param salt Master salt.
  101. * @param tagSize Size of the authentication tag.
  102. * @param ssrc Synchronization source.
  103. */
  104. void ocrypto_srtp_setupContext(
  105. ocrypto_srtp_context *srtpContext,
  106. ocrypto_srtp_context *srtcpContext,
  107. const uint8_t *key,
  108. uint32_t keySize,
  109. const uint8_t *salt,
  110. uint32_t tagSize,
  111. uint32_t ssrc);
  112. /**
  113. * Encrypt SRTP packet.
  114. *
  115. * The final packet consists of @p numHeaderBytes encrypted in place, followed
  116. * by @p numDataBytes copied from @p dataBytes during encryption.
  117. *
  118. * @param srtpContext SRTP context.
  119. * @param[in,out] packet Encrypted packet.
  120. * @param dataBytes Data bytes to be encrypted.
  121. * @param numHeaderBytes Number of header bytes.
  122. * @param numDataBytes Number of data bytes.
  123. * @param index Packet index.
  124. */
  125. void ocrypto_srtp_encrypt(
  126. const ocrypto_srtp_context *srtpContext,
  127. uint8_t *packet,
  128. const uint8_t *dataBytes,
  129. size_t numHeaderBytes,
  130. size_t numDataBytes,
  131. uint32_t index);
  132. /**
  133. * Decrypt SRTP packet.
  134. *
  135. * @param srtpContext SRTP context.
  136. * @param[out] data Decrypted data.
  137. * @param packetBytes Packet bytes.
  138. * @param numPacketBytes Number of packet bytes.
  139. * @param index Packet index.
  140. */
  141. void ocrypto_srtp_decrypt(
  142. const ocrypto_srtp_context *srtpContext,
  143. uint8_t *data,
  144. const uint8_t *packetBytes,
  145. size_t numPacketBytes,
  146. uint32_t index);
  147. /**
  148. * Generate SRTP authentication tag from bytes and index.
  149. *
  150. * @param context SRTP context.
  151. * @param[out] tag Authentication tag generated.
  152. * @param bytes Byte buffer.
  153. * @param numBytes Number of bytes in buffer.
  154. * @param index Index.
  155. */
  156. void ocrypto_srtp_authenticate(
  157. const ocrypto_srtp_context *context,
  158. uint8_t *tag,
  159. const uint8_t *bytes,
  160. size_t numBytes,
  161. uint32_t index);
  162. /**
  163. * Check SRTP authentication tag against bytes and index.
  164. *
  165. * @param context SRTP context.
  166. * @param tag Tag.
  167. * @param bytes Byte buffer.
  168. * @param numBytes Number of bytes in buffer.
  169. * @param index Index.
  170. *
  171. * @retval 1 If the tag is valid.
  172. * @retval 0 Otherwise.
  173. */
  174. int ocrypto_srtp_verifyAuthentication(
  175. const ocrypto_srtp_context *context,
  176. const uint8_t *tag,
  177. const uint8_t *bytes,
  178. size_t numBytes,
  179. uint32_t index);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif /* #ifndef OCRYPTO_SRTP_H */
  184. /** @} */