crypto_util.c 3.6 KB

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