2
0

test_srtp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * test_srtp.c
  3. *
  4. * Unit tests for internal srtp functions
  5. *
  6. * Cisco Systems, Inc.
  7. *
  8. */
  9. /*
  10. *
  11. * Copyright (c) 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. /*
  45. * libSRTP specific.
  46. */
  47. #include "../srtp/srtp.c" // Get access to static functions
  48. /*
  49. * Test specific.
  50. */
  51. #include "cutest.h"
  52. /*
  53. * Standard library.
  54. */
  55. /*
  56. * Forward declarations for all tests.
  57. */
  58. void srtp_calc_aead_iv_srtcp_all_zero_input_yield_zero_output(void);
  59. void srtp_calc_aead_iv_srtcp_seq_num_over_0x7FFFFFFF_bad_param(void);
  60. void srtp_calc_aead_iv_srtcp_distinct_iv_per_sequence_number(void);
  61. /*
  62. * NULL terminated array of tests.
  63. * The first item in the array is a char[] which give some information about
  64. * what is being tested and is displayed to the user during runtime, the second
  65. * item is the test function.
  66. */
  67. TEST_LIST = { { "srtp_calc_aead_iv_srtcp_all_zero_input_yield_zero_output()",
  68. srtp_calc_aead_iv_srtcp_all_zero_input_yield_zero_output },
  69. { "srtp_calc_aead_iv_srtcp_seq_num_over_0x7FFFFFFF_bad_param()",
  70. srtp_calc_aead_iv_srtcp_seq_num_over_0x7FFFFFFF_bad_param },
  71. { "srtp_calc_aead_iv_srtcp_distinct_iv_per_sequence_number()",
  72. srtp_calc_aead_iv_srtcp_distinct_iv_per_sequence_number },
  73. { NULL } /* End of tests */ };
  74. /*
  75. * Implementation.
  76. */
  77. void srtp_calc_aead_iv_srtcp_all_zero_input_yield_zero_output()
  78. {
  79. // Preconditions
  80. srtp_session_keys_t session_keys;
  81. v128_t init_vector;
  82. srtcp_hdr_t header;
  83. uint32_t sequence_num;
  84. // Postconditions
  85. srtp_err_status_t status;
  86. const v128_t zero_vector;
  87. memset((v128_t *)&zero_vector, 0, sizeof(v128_t));
  88. // Given
  89. memset(&session_keys, 0, sizeof(srtp_session_keys_t));
  90. memset(&init_vector, 0, sizeof(v128_t));
  91. memset(&header, 0, sizeof(srtcp_hdr_t));
  92. sequence_num = 0x0UL;
  93. // When
  94. status = srtp_calc_aead_iv_srtcp(&session_keys, &init_vector, sequence_num,
  95. &header);
  96. // Then
  97. TEST_CHECK(status == srtp_err_status_ok);
  98. TEST_CHECK(memcmp(&zero_vector, &init_vector, sizeof(v128_t)) == 0);
  99. }
  100. void srtp_calc_aead_iv_srtcp_seq_num_over_0x7FFFFFFF_bad_param()
  101. {
  102. // Preconditions
  103. srtp_session_keys_t session_keys;
  104. v128_t init_vector;
  105. srtcp_hdr_t header;
  106. uint32_t sequence_num;
  107. // Postconditions
  108. srtp_err_status_t status;
  109. // Given
  110. memset(&session_keys, 0, sizeof(srtp_session_keys_t));
  111. memset(&init_vector, 0, sizeof(v128_t));
  112. memset(&header, 0, sizeof(srtcp_hdr_t));
  113. sequence_num = 0x7FFFFFFFUL + 0x1UL;
  114. // When
  115. status = srtp_calc_aead_iv_srtcp(&session_keys, &init_vector, sequence_num,
  116. &header);
  117. // Then
  118. TEST_CHECK(status == srtp_err_status_bad_param);
  119. }
  120. /*
  121. * Regression test for issue #256:
  122. * Srtcp IV calculation incorrectly masks high bit of sequence number for
  123. * little-endian platforms.
  124. * Ensure that for each valid sequence number where the most significant bit is
  125. * high that we get an expected and unique IV.
  126. */
  127. void srtp_calc_aead_iv_srtcp_distinct_iv_per_sequence_number()
  128. {
  129. #define SAMPLE_COUNT (3)
  130. // Preconditions
  131. // Test each significant bit high in each full byte.
  132. srtp_session_keys_t session_keys;
  133. srtcp_hdr_t header;
  134. v128_t output_iv[SAMPLE_COUNT];
  135. uint32_t sequence_num[SAMPLE_COUNT];
  136. v128_t final_iv[SAMPLE_COUNT];
  137. size_t i = 0;
  138. memset(&output_iv, 0, SAMPLE_COUNT * sizeof(v128_t));
  139. sequence_num[0] = 0xFF;
  140. sequence_num[1] = 0xFF00;
  141. sequence_num[2] = 0xFF0000;
  142. // Postconditions
  143. memset(&final_iv, 0, SAMPLE_COUNT * sizeof(v128_t));
  144. final_iv[0].v8[11] = 0xFF;
  145. final_iv[1].v8[10] = 0xFF;
  146. final_iv[2].v8[9] = 0xFF;
  147. // Given
  148. memset(&session_keys, 0, sizeof(srtp_session_keys_t));
  149. memset(&header, 0, sizeof(srtcp_hdr_t));
  150. // When
  151. for (i = 0; i < SAMPLE_COUNT; i++) {
  152. TEST_CHECK(srtp_calc_aead_iv_srtcp(&session_keys, &output_iv[i],
  153. sequence_num[i],
  154. &header) == srtp_err_status_ok);
  155. }
  156. // Then all IVs are as expected
  157. for (i = 0; i < SAMPLE_COUNT; i++) {
  158. TEST_CHECK(memcmp(&final_iv[i], &output_iv[i], sizeof(v128_t)) == 0);
  159. }
  160. #undef SAMPLE_COUNT
  161. }