constant_flow.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * \file constant_flow.h
  3. *
  4. * \brief This file contains tools to ensure tested code has constant flow.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. #ifndef TEST_CONSTANT_FLOW_H
  23. #define TEST_CONSTANT_FLOW_H
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. /*
  30. * This file defines the two macros
  31. *
  32. * #define TEST_CF_SECRET(ptr, size)
  33. * #define TEST_CF_PUBLIC(ptr, size)
  34. *
  35. * that can be used in tests to mark a memory area as secret (no branch or
  36. * memory access should depend on it) or public (default, only needs to be
  37. * marked explicitly when it was derived from secret data).
  38. *
  39. * Arguments:
  40. * - ptr: a pointer to the memory area to be marked
  41. * - size: the size in bytes of the memory area
  42. *
  43. * Implementation:
  44. * The basic idea is that of ctgrind <https://github.com/agl/ctgrind>: we can
  45. * re-use tools that were designed for checking use of uninitialized memory.
  46. * This file contains two implementations: one based on MemorySanitizer, the
  47. * other on valgrind's memcheck. If none of them is enabled, dummy macros that
  48. * do nothing are defined for convenience.
  49. */
  50. #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN)
  51. #include <sanitizer/msan_interface.h>
  52. /* Use macros to avoid messing up with origin tracking */
  53. #define TEST_CF_SECRET __msan_allocated_memory
  54. // void __msan_allocated_memory(const volatile void* data, size_t size);
  55. #define TEST_CF_PUBLIC __msan_unpoison
  56. // void __msan_unpoison(const volatile void *a, size_t size);
  57. #elif defined(MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND)
  58. #include <valgrind/memcheck.h>
  59. #define TEST_CF_SECRET VALGRIND_MAKE_MEM_UNDEFINED
  60. // VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len)
  61. #define TEST_CF_PUBLIC VALGRIND_MAKE_MEM_DEFINED
  62. // VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len)
  63. #else /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
  64. MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
  65. #define TEST_CF_SECRET(ptr, size)
  66. #define TEST_CF_PUBLIC(ptr, size)
  67. #endif /* MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN ||
  68. MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND */
  69. #endif /* TEST_CONSTANT_FLOW_H */