test_suite_constant_time.function 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* BEGIN_HEADER */
  2. /** \file test_suite_constant_time.function
  3. *
  4. * Functional testing of functions in the constant_time module.
  5. *
  6. * The tests are instrumented with #TEST_CF_SECRET and #TEST_CF_PUBLIC
  7. * (see tests/include/test/constant_flow.h) so that running the tests
  8. * under MSan or Valgrind will detect a non-constant-time implementation.
  9. */
  10. #include <mbedtls/constant_time.h>
  11. #include <constant_time_internal.h>
  12. #include <constant_time_invasive.h>
  13. #include <test/constant_flow.h>
  14. /* END_HEADER */
  15. /* BEGIN_CASE */
  16. void mbedtls_ct_memcmp_null()
  17. {
  18. uint32_t x = 0;
  19. TEST_ASSERT(mbedtls_ct_memcmp(&x, NULL, 0) == 0);
  20. TEST_ASSERT(mbedtls_ct_memcmp(NULL, &x, 0) == 0);
  21. TEST_ASSERT(mbedtls_ct_memcmp(NULL, NULL, 0) == 0);
  22. }
  23. /* END_CASE */
  24. /* BEGIN_CASE */
  25. void mbedtls_ct_memcmp(int same, int size, int offset)
  26. {
  27. uint8_t *a = NULL, *b = NULL;
  28. ASSERT_ALLOC(a, size + offset);
  29. ASSERT_ALLOC(b, size + offset);
  30. TEST_CF_SECRET(a + offset, size);
  31. TEST_CF_SECRET(b + offset, size);
  32. /* Construct data that matches, if same == -1, otherwise
  33. * same gives the number of bytes (after the initial offset)
  34. * that will match; after that it will differ.
  35. */
  36. for (int i = 0; i < size + offset; i++) {
  37. a[i] = i & 0xff;
  38. if (same == -1 || (i - offset) < same) {
  39. b[i] = a[i];
  40. } else {
  41. b[i] = (i + 1) & 0xff;
  42. }
  43. }
  44. int reference = memcmp(a + offset, b + offset, size);
  45. int actual = mbedtls_ct_memcmp(a + offset, b + offset, size);
  46. TEST_CF_PUBLIC(a + offset, size);
  47. TEST_CF_PUBLIC(b + offset, size);
  48. if (same == -1 || same >= size) {
  49. TEST_ASSERT(reference == 0);
  50. TEST_ASSERT(actual == 0);
  51. } else {
  52. TEST_ASSERT(reference != 0);
  53. TEST_ASSERT(actual != 0);
  54. }
  55. exit:
  56. mbedtls_free(a);
  57. mbedtls_free(b);
  58. }
  59. /* END_CASE */
  60. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_MAC */
  61. void mbedtls_ct_memcpy_if_eq(int eq, int size, int offset)
  62. {
  63. uint8_t *src = NULL, *result = NULL, *expected = NULL;
  64. ASSERT_ALLOC(src, size + offset);
  65. ASSERT_ALLOC(result, size + offset);
  66. ASSERT_ALLOC(expected, size + offset);
  67. for (int i = 0; i < size + offset; i++) {
  68. src[i] = 1;
  69. result[i] = 0xff;
  70. expected[i] = eq ? 1 : 0xff;
  71. }
  72. int one, secret_eq;
  73. TEST_CF_SECRET(&one, sizeof(one));
  74. TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
  75. one = 1;
  76. secret_eq = eq;
  77. mbedtls_ct_memcpy_if_eq(result + offset, src, size, secret_eq, one);
  78. TEST_CF_PUBLIC(&one, sizeof(one));
  79. TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
  80. ASSERT_COMPARE(expected, size, result + offset, size);
  81. for (int i = 0; i < size + offset; i++) {
  82. src[i] = 1;
  83. result[i] = 0xff;
  84. expected[i] = eq ? 1 : 0xff;
  85. }
  86. TEST_CF_SECRET(&one, sizeof(one));
  87. TEST_CF_SECRET(&secret_eq, sizeof(secret_eq));
  88. one = 1;
  89. secret_eq = eq;
  90. mbedtls_ct_memcpy_if_eq(result, src + offset, size, secret_eq, one);
  91. TEST_CF_PUBLIC(&one, sizeof(one));
  92. TEST_CF_PUBLIC(&secret_eq, sizeof(secret_eq));
  93. ASSERT_COMPARE(expected, size, result, size);
  94. exit:
  95. mbedtls_free(src);
  96. mbedtls_free(result);
  97. mbedtls_free(expected);
  98. }
  99. /* END_CASE */
  100. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
  101. void ssl_cf_memcpy_offset(int offset_min, int offset_max, int len)
  102. {
  103. unsigned char *dst = NULL;
  104. unsigned char *src = NULL;
  105. size_t src_len = offset_max + len;
  106. size_t secret;
  107. ASSERT_ALLOC(dst, len);
  108. ASSERT_ALLOC(src, src_len);
  109. /* Fill src in a way that we can detect if we copied the right bytes */
  110. mbedtls_test_rnd_std_rand(NULL, src, src_len);
  111. for (secret = offset_min; secret <= (size_t) offset_max; secret++) {
  112. mbedtls_test_set_step((int) secret);
  113. TEST_CF_SECRET(&secret, sizeof(secret));
  114. mbedtls_ct_memcpy_offset(dst, src, secret,
  115. offset_min, offset_max, len);
  116. TEST_CF_PUBLIC(&secret, sizeof(secret));
  117. TEST_CF_PUBLIC(dst, len);
  118. ASSERT_COMPARE(dst, len, src + secret, len);
  119. }
  120. exit:
  121. mbedtls_free(dst);
  122. mbedtls_free(src);
  123. }
  124. /* END_CASE */