]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Rename tor_mem_is_zero to fast_mem_is_zero()
authorNick Mathewson <nickm@torproject.org>
Tue, 30 Apr 2019 18:43:35 +0000 (14:43 -0400)
committerNick Mathewson <nickm@torproject.org>
Tue, 30 Apr 2019 18:45:51 +0000 (14:45 -0400)
For memeq and friends, "tor_" indicates constant-time and "fast_"
indicates optimized.  I'm fine with leaving the constant-time
"safe_mem_is_zero" with its current name, but the "tor_" prefix on
the current optimized version is misleading.

Also, make the tor_digest*_is_zero() uniformly constant-time, and
add a fast_digest*_is_zero() version to use as needed.

A later commit in this branch will fix all the users of
tor_mem_is_zero().

Closes ticket 30309.

changes/bug30309 [new file with mode: 0644]
src/lib/string/util_string.c
src/lib/string/util_string.h

diff --git a/changes/bug30309 b/changes/bug30309
new file mode 100644 (file)
index 0000000..6cbbe8d
--- /dev/null
@@ -0,0 +1,3 @@
+  o Code simplification and refactoring:
+    - Rename tor_mem_is_zero() to fast_mem_is_zero(), to emphasize that
+      it is not a constant-time function. Closes ticket 30309.
index 0c4e39900807a9849bedbecd362d4d027208e918..f5061a11d26690082aff0c17560858c34a525690 100644 (file)
@@ -71,7 +71,7 @@ tor_memstr(const void *haystack, size_t hlen, const char *needle)
 
 /** Return true iff the 'len' bytes at 'mem' are all zero. */
 int
-tor_mem_is_zero(const char *mem, size_t len)
+fast_mem_is_zero(const char *mem, size_t len)
 {
   static const char ZERO[] = {
     0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
@@ -95,17 +95,14 @@ tor_mem_is_zero(const char *mem, size_t len)
 int
 tor_digest_is_zero(const char *digest)
 {
-  static const uint8_t ZERO_DIGEST[] = {
-    0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
-  };
-  return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
+  return safe_mem_is_zero(digest, DIGEST_LEN);
 }
 
 /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
 int
 tor_digest256_is_zero(const char *digest)
 {
-  return tor_mem_is_zero(digest, DIGEST256_LEN);
+  return safe_mem_is_zero(digest, DIGEST256_LEN);
 }
 
 /** Remove from the string <b>s</b> every character which appears in
index da4fab159c9b8e5e30c1555c3189457204ad870a..7e8af0578c349ed2c49fb9a4b225ae76326c4803 100644 (file)
@@ -20,7 +20,13 @@ const void *tor_memmem(const void *haystack, size_t hlen, const void *needle,
                        size_t nlen);
 const void *tor_memstr(const void *haystack, size_t hlen,
                        const char *needle);
-int tor_mem_is_zero(const char *mem, size_t len);
+int fast_mem_is_zero(const char *mem, size_t len);
+#define fast_digest_is_zero(d) fast_mem_is_zero((d), DIGEST_LEN)
+#define fast_digetst256_is_zero(d) fast_mem_is_zero((d), DIGEST256_LEN)
+
+// XXXX remove this after we replace all users.
+#define tor_mem_is_zero fast_mem_is_zero
+
 int tor_digest_is_zero(const char *digest);
 int tor_digest256_is_zero(const char *digest);