test_suite_platform_util.function 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/platform_util.h"
  3. /* END_HEADER */
  4. /* BEGIN_CASE */
  5. void mbedtls_platform_zeroize(int len, int null)
  6. {
  7. char buf[130];
  8. char *p = NULL;
  9. TEST_ASSERT(len <= 128);
  10. /* Write sentinel values */
  11. buf[0] = 2;
  12. buf[len + 1] = 2;
  13. /* Write non-zero content */
  14. if (!null) {
  15. p = &buf[1];
  16. for (int i = 0; i < len; i++) {
  17. p[i] = 1;
  18. }
  19. }
  20. /* Check content is non-zero */
  21. TEST_EQUAL(buf[0], 2);
  22. for (int i = 0; i < len; i++) {
  23. TEST_ASSERT(p[i] == 1);
  24. }
  25. TEST_EQUAL(buf[len + 1], 2);
  26. mbedtls_platform_zeroize(p, len);
  27. /* Check content is zero and sentinels un-changed */
  28. TEST_EQUAL(buf[0], 2);
  29. for (int i = 0; i < len; i++) {
  30. TEST_ASSERT(p[i] == 0);
  31. }
  32. TEST_EQUAL(buf[len + 1], 2);
  33. }
  34. /* END_CASE */
  35. /* BEGIN_CASE */
  36. void mbedtls_platform_zeroize_uninitialised(int len, int p)
  37. {
  38. /*
  39. * As per #7301: on some platforms, including modern Linux, Clang with Msan
  40. * does not recognize that explicit_bzero() writes well-defined content to
  41. * its output buffer. For us, this causes CMAC operations to fail in Msan
  42. * builds when mbedtls_platform_zeroize() is implemented over
  43. * explicit_bzero().
  44. *
  45. * This test ensures we have a simple/obvious MSan test rather than
  46. * spurious errors in crypto code that are hard to track down.
  47. */
  48. char buf[128];
  49. mbedtls_platform_zeroize(buf, len);
  50. TEST_EQUAL(buf[p], 0);
  51. }
  52. /* END_CASE */