crypto_util.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_util.c
  8. *
  9. * \brief Common cryptographic utilities.
  10. **/
  11. #ifndef CRYPTO_UTIL_PRIVATE
  12. #define CRYPTO_UTIL_PRIVATE
  13. #include <string.h>
  14. #ifdef _WIN32
  15. #include <windows.h>
  16. #include <wincrypt.h>
  17. #endif /* defined(_WIN32) */
  18. #include "crypto_util.h"
  19. #include "util.h"
  20. DISABLE_GCC_WARNING(redundant-decls)
  21. #include <openssl/crypto.h>
  22. ENABLE_GCC_WARNING(redundant-decls)
  23. /**
  24. * Destroy the <b>sz</b> bytes of data stored at <b>mem</b>, setting them to
  25. * the value <b>byte</b>.
  26. * If <b>mem</b> is NULL or <b>sz</b> is zero, nothing happens.
  27. *
  28. * This function is preferable to memset, since many compilers will happily
  29. * optimize out memset() when they can convince themselves that the data being
  30. * cleared will never be read.
  31. *
  32. * Right now, our convention is to use this function when we are wiping data
  33. * that's about to become inaccessible, such as stack buffers that are about
  34. * to go out of scope or structures that are about to get freed. (In
  35. * practice, it appears that the compilers we're currently using will optimize
  36. * out the memset()s for stack-allocated buffers, but not those for
  37. * about-to-be-freed structures. That could change, though, so we're being
  38. * wary.) If there are live reads for the data, then you can just use
  39. * memset().
  40. */
  41. void
  42. memwipe(void *mem, uint8_t byte, size_t sz)
  43. {
  44. if (sz == 0) {
  45. return;
  46. }
  47. /* If sz is nonzero, then mem must not be NULL. */
  48. tor_assert(mem != NULL);
  49. /* Data this large is likely to be an underflow. */
  50. tor_assert(sz < SIZE_T_CEILING);
  51. /* Because whole-program-optimization exists, we may not be able to just
  52. * have this function call "memset". A smart compiler could inline it, then
  53. * eliminate dead memsets, and declare itself to be clever. */
  54. #if defined(SecureZeroMemory) || defined(HAVE_SECUREZEROMEMORY)
  55. /* Here's what you do on windows. */
  56. SecureZeroMemory(mem,sz);
  57. #elif defined(HAVE_RTLSECUREZEROMEMORY)
  58. RtlSecureZeroMemory(mem,sz);
  59. #elif defined(HAVE_EXPLICIT_BZERO)
  60. /* The BSDs provide this. */
  61. explicit_bzero(mem, sz);
  62. #elif defined(HAVE_MEMSET_S)
  63. /* This is in the C99 standard. */
  64. memset_s(mem, sz, 0, sz);
  65. #else
  66. /* This is a slow and ugly function from OpenSSL that fills 'mem' with junk
  67. * based on the pointer value, then uses that junk to update a global
  68. * variable. It's an elaborate ruse to trick the compiler into not
  69. * optimizing out the "wipe this memory" code. Read it if you like zany
  70. * programming tricks! In later versions of Tor, we should look for better
  71. * not-optimized-out memory wiping stuff...
  72. *
  73. * ...or maybe not. In practice, there are pure-asm implementations of
  74. * OPENSSL_cleanse() on most platforms, which ought to do the job.
  75. **/
  76. OPENSSL_cleanse(mem, sz);
  77. #endif /* defined(SecureZeroMemory) || defined(HAVE_SECUREZEROMEMORY) || ... */
  78. /* Just in case some caller of memwipe() is relying on getting a buffer
  79. * filled with a particular value, fill the buffer.
  80. *
  81. * If this function gets inlined, this memset might get eliminated, but
  82. * that's okay: We only care about this particular memset in the case where
  83. * the caller should have been using memset(), and the memset() wouldn't get
  84. * eliminated. In other words, this is here so that we won't break anything
  85. * if somebody accidentally calls memwipe() instead of memset().
  86. **/
  87. memset(mem, byte, sz);
  88. }
  89. #endif /* !defined(CRYPTO_UTIL_PRIVATE) */