ekt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * ekt.c
  3. *
  4. * Encrypted Key Transport for SRTP
  5. *
  6. * David McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017 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. #include "srtp_priv.h"
  45. #include "err.h"
  46. #include "ekt.h"
  47. extern srtp_debug_module_t mod_srtp;
  48. /*
  49. * The EKT Authentication Tag format.
  50. *
  51. * 0 1 2 3
  52. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  53. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  54. * : Base Authentication Tag :
  55. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  56. * : Encrypted Master Key :
  57. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  58. * | Rollover Counter |
  59. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  60. * | Initial Sequence Number | Security Parameter Index |
  61. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. *
  63. */
  64. #define EKT_OCTETS_AFTER_BASE_TAG 24
  65. #define EKT_OCTETS_AFTER_EMK 8
  66. #define EKT_OCTETS_AFTER_ROC 4
  67. #define EKT_SPI_LEN 2
  68. unsigned srtp_ekt_octets_after_base_tag(srtp_ekt_stream_t ekt)
  69. {
  70. /*
  71. * if the pointer ekt is NULL, then EKT is not in effect, so we
  72. * indicate this by returning zero
  73. */
  74. if (!ekt)
  75. return 0;
  76. switch (ekt->data->ekt_cipher_type) {
  77. case SRTP_EKT_CIPHER_AES_128_ECB:
  78. return 16 + EKT_OCTETS_AFTER_EMK;
  79. break;
  80. default:
  81. break;
  82. }
  83. return 0;
  84. }
  85. static inline srtp_ekt_spi_t srtcp_packet_get_ekt_spi(
  86. const uint8_t *packet_start,
  87. unsigned pkt_octet_len)
  88. {
  89. const uint8_t *spi_location;
  90. spi_location = packet_start + (pkt_octet_len - EKT_SPI_LEN);
  91. return *((const srtp_ekt_spi_t *)spi_location);
  92. }
  93. static inline uint32_t srtcp_packet_get_ekt_roc(const uint8_t *packet_start,
  94. unsigned pkt_octet_len)
  95. {
  96. const uint8_t *roc_location;
  97. roc_location = packet_start + (pkt_octet_len - EKT_OCTETS_AFTER_ROC);
  98. return *((const uint32_t *)roc_location);
  99. }
  100. static inline const uint8_t *srtcp_packet_get_emk_location(
  101. const uint8_t *packet_start,
  102. unsigned pkt_octet_len)
  103. {
  104. const uint8_t *location;
  105. location = packet_start + (pkt_octet_len - EKT_OCTETS_AFTER_BASE_TAG);
  106. return location;
  107. }
  108. srtp_err_status_t srtp_ekt_alloc(srtp_ekt_stream_t *stream_data,
  109. srtp_ekt_policy_t policy)
  110. {
  111. /*
  112. * if the policy pointer is NULL, then EKT is not in use
  113. * so we just set the EKT stream data pointer to NULL
  114. */
  115. if (!policy) {
  116. *stream_data = NULL;
  117. return srtp_err_status_ok;
  118. }
  119. /* TODO */
  120. *stream_data = NULL;
  121. return srtp_err_status_ok;
  122. }
  123. srtp_err_status_t srtp_ekt_stream_init_from_policy(
  124. srtp_ekt_stream_t stream_data,
  125. srtp_ekt_policy_t policy)
  126. {
  127. if (!stream_data)
  128. return srtp_err_status_ok;
  129. return srtp_err_status_ok;
  130. }
  131. void aes_decrypt_with_raw_key(void *ciphertext, const void *key, int key_len)
  132. {
  133. #ifndef GCM
  134. // FIXME: need to get this working through the crypto module interface
  135. srtp_aes_expanded_key_t expanded_key;
  136. srtp_aes_expand_decryption_key(key, key_len, &expanded_key);
  137. srtp_aes_decrypt(ciphertext, &expanded_key);
  138. #endif
  139. }
  140. /*
  141. * The function srtp_stream_init_from_ekt() initializes a stream using
  142. * the EKT data from an SRTCP trailer.
  143. */
  144. srtp_err_status_t srtp_stream_init_from_ekt(srtp_stream_t stream,
  145. const void *srtcp_hdr,
  146. unsigned pkt_octet_len)
  147. {
  148. srtp_err_status_t err;
  149. const uint8_t *master_key;
  150. srtp_policy_t srtp_policy;
  151. uint32_t roc;
  152. /*
  153. * NOTE: at present, we only support a single ekt_policy at a time.
  154. */
  155. if (stream->ekt->data->spi !=
  156. srtcp_packet_get_ekt_spi(srtcp_hdr, pkt_octet_len))
  157. return srtp_err_status_no_ctx;
  158. if (stream->ekt->data->ekt_cipher_type != SRTP_EKT_CIPHER_AES_128_ECB)
  159. return srtp_err_status_bad_param;
  160. /* decrypt the Encrypted Master Key field */
  161. master_key = srtcp_packet_get_emk_location(srtcp_hdr, pkt_octet_len);
  162. /* FIX!? This decrypts the master key in-place, and never uses it */
  163. /* FIX!? It's also passing to ekt_dec_key (which is an aes_expanded_key_t)
  164. * to a function which expects a raw (unexpanded) key */
  165. aes_decrypt_with_raw_key((void *)master_key,
  166. &stream->ekt->data->ekt_dec_key, 16);
  167. /* set the SRTP ROC */
  168. roc = srtcp_packet_get_ekt_roc(srtcp_hdr, pkt_octet_len);
  169. err = srtp_rdbx_set_roc(&stream->rtp_rdbx, roc);
  170. if (err)
  171. return err;
  172. err = srtp_stream_init(stream, &srtp_policy);
  173. if (err)
  174. return err;
  175. return srtp_err_status_ok;
  176. }
  177. void srtp_ekt_write_data(srtp_ekt_stream_t ekt,
  178. uint8_t *base_tag,
  179. unsigned base_tag_len,
  180. int *packet_len,
  181. srtp_xtd_seq_num_t pkt_index)
  182. {
  183. uint32_t roc;
  184. uint16_t isn;
  185. unsigned emk_len;
  186. uint8_t *packet;
  187. /* if the pointer ekt is NULL, then EKT is not in effect */
  188. if (!ekt) {
  189. debug_print0(mod_srtp, "EKT not in use");
  190. return;
  191. }
  192. /* write zeros into the location of the base tag */
  193. octet_string_set_to_zero(base_tag, base_tag_len);
  194. packet = base_tag + base_tag_len;
  195. /* copy encrypted master key into packet */
  196. emk_len = srtp_ekt_octets_after_base_tag(ekt);
  197. memcpy(packet, ekt->encrypted_master_key, emk_len);
  198. debug_print(mod_srtp, "writing EKT EMK: %s,",
  199. srtp_octet_string_hex_string(packet, emk_len));
  200. packet += emk_len;
  201. /* copy ROC into packet */
  202. roc = (uint32_t)(pkt_index >> 16);
  203. *((uint32_t *)packet) = be32_to_cpu(roc);
  204. debug_print(mod_srtp, "writing EKT ROC: %s,",
  205. srtp_octet_string_hex_string(packet, sizeof(roc)));
  206. packet += sizeof(roc);
  207. /* copy ISN into packet */
  208. isn = (uint16_t)pkt_index;
  209. *((uint16_t *)packet) = htons(isn);
  210. debug_print(mod_srtp, "writing EKT ISN: %s,",
  211. srtp_octet_string_hex_string(packet, sizeof(isn)));
  212. packet += sizeof(isn);
  213. /* copy SPI into packet */
  214. *((uint16_t *)packet) = htons(ekt->data->spi);
  215. debug_print(mod_srtp, "writing EKT SPI: %s,",
  216. srtp_octet_string_hex_string(packet, sizeof(ekt->data->spi)));
  217. /* increase packet length appropriately */
  218. *packet_len += EKT_OCTETS_AFTER_EMK + emk_len;
  219. }
  220. /*
  221. * The function call srtcp_ekt_trailer(ekt, auth_len, auth_tag )
  222. *
  223. * If the pointer ekt is NULL, then the other inputs are unaffected.
  224. *
  225. * auth_tag is a pointer to the pointer to the location of the
  226. * authentication tag in the packet. If EKT is in effect, then the
  227. * auth_tag pointer is set to the location
  228. */
  229. void srtcp_ekt_trailer(srtp_ekt_stream_t ekt,
  230. unsigned *auth_len,
  231. void **auth_tag,
  232. void *tag_copy)
  233. {
  234. /*
  235. * if there is no EKT policy, then the other inputs are unaffected
  236. */
  237. if (!ekt)
  238. return;
  239. /* copy auth_tag into temporary location */
  240. }