tor  0.4.2.0-alpha-dev
crypto_util.c
Go to the documentation of this file.
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
13 #define CRYPTO_UTIL_PRIVATE
14 
16 #include "lib/cc/compat_compiler.h"
17 
18 #include <string.h>
19 
20 #ifdef _WIN32
21 #include <winsock2.h>
22 #include <windows.h>
23 #include <wincrypt.h>
24 #endif /* defined(_WIN32) */
25 
26 #include <stdlib.h>
27 
28 #ifdef ENABLE_OPENSSL
29 DISABLE_GCC_WARNING(redundant-decls)
30 #include <openssl/err.h>
31 #include <openssl/crypto.h>
32 ENABLE_GCC_WARNING(redundant-decls)
33 #endif /* defined(ENABLE_OPENSSL) */
34 
35 #include "lib/log/log.h"
36 #include "lib/log/util_bug.h"
37 
56 void
57 memwipe(void *mem, uint8_t byte, size_t sz)
58 {
59  if (sz == 0) {
60  return;
61  }
62  /* If sz is nonzero, then mem must not be NULL. */
63  tor_assert(mem != NULL);
64 
65  /* Data this large is likely to be an underflow. */
67 
68  /* Because whole-program-optimization exists, we may not be able to just
69  * have this function call "memset". A smart compiler could inline it, then
70  * eliminate dead memsets, and declare itself to be clever. */
71 
72 #if defined(SecureZeroMemory) || defined(HAVE_SECUREZEROMEMORY)
73  /* Here's what you do on windows. */
74  SecureZeroMemory(mem,sz);
75 #elif defined(HAVE_RTLSECUREZEROMEMORY)
76  RtlSecureZeroMemory(mem,sz);
77 #elif defined(HAVE_EXPLICIT_BZERO)
78  /* The BSDs provide this. */
79  explicit_bzero(mem, sz);
80 #elif defined(HAVE_MEMSET_S)
81  /* This is in the C99 standard. */
82  memset_s(mem, sz, 0, sz);
83 #elif defined(ENABLE_OPENSSL)
84  /* This is a slow and ugly function from OpenSSL that fills 'mem' with junk
85  * based on the pointer value, then uses that junk to update a global
86  * variable. It's an elaborate ruse to trick the compiler into not
87  * optimizing out the "wipe this memory" code. Read it if you like zany
88  * programming tricks! In later versions of Tor, we should look for better
89  * not-optimized-out memory wiping stuff...
90  *
91  * ...or maybe not. In practice, there are pure-asm implementations of
92  * OPENSSL_cleanse() on most platforms, which ought to do the job.
93  **/
94 
95  OPENSSL_cleanse(mem, sz);
96 #else
97  memset(mem, 0, sz);
98  asm volatile("" ::: "memory");
99 #endif /* defined(SecureZeroMemory) || defined(HAVE_SECUREZEROMEMORY) || ... */
100 
101  /* Just in case some caller of memwipe() is relying on getting a buffer
102  * filled with a particular value, fill the buffer.
103  *
104  * If this function gets inlined, this memset might get eliminated, but
105  * that's okay: We only care about this particular memset in the case where
106  * the caller should have been using memset(), and the memset() wouldn't get
107  * eliminated. In other words, this is here so that we won't break anything
108  * if somebody accidentally calls memwipe() instead of memset().
109  **/
110  memset(mem, byte, sz);
111 }
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
Common functions for cryptographic routines.
tor_assert(buffer)
#define SIZE_T_CEILING
Definition: torint.h:126
Utility macros to handle different features and behavior in different compilers.
Headers for log.c.
Macros to manage assertions, fatal and non-fatal.