[PATCH] Change _gcry_burn_stack take burn depth as unsigned integer

Jussi Kivilinna jussi.kivilinna at iki.fi
Wed Sep 4 09:32:51 CEST 2013


* src/misc.c (_gcry_burn_stack): Change to handle 'unsigned int' bytes.
--

Unsigned integer is better here for code generation because we can now avoid
possible branching caused by (bytes <= 0) check.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 src/g10lib.h |    2 +-
 src/misc.c   |   10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/g10lib.h b/src/g10lib.h
index 31131a5..cc1a1a8 100644
--- a/src/g10lib.h
+++ b/src/g10lib.h
@@ -235,7 +235,7 @@ int strcasecmp (const char *a, const char *b) _GCRY_GCC_ATTR_PURE;
 
 /* Stack burning.  */
 
-void _gcry_burn_stack (int bytes);
+void _gcry_burn_stack (unsigned int bytes);
 
 
 /* To avoid that a compiler optimizes certain memset calls away, these
diff --git a/src/misc.c b/src/misc.c
index 135aeb4..dece1d0 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -288,10 +288,11 @@ _gcry_log_printhex (const char *text, const void *buffer, size_t length)
 
 
 void
-_gcry_burn_stack (int bytes)
+_gcry_burn_stack (unsigned int bytes)
 {
 #ifdef HAVE_VLA
-    int buflen = (((bytes <= 0) ? 1 : bytes) + 63) & ~63;
+    /* (bytes == 0 ? 1 : bytes) == (!bytes + bytes) */
+    unsigned int buflen = ((!bytes + bytes) + 63) & ~63;
     volatile char buf[buflen];
 
     wipememory (buf, sizeof buf);
@@ -300,9 +301,8 @@ _gcry_burn_stack (int bytes)
 
     wipememory (buf, sizeof buf);
 
-    bytes -= sizeof buf;
-    if (bytes > 0)
-        _gcry_burn_stack (bytes);
+    if (bytes > sizeof buf)
+        _gcry_burn_stack (bytes - sizeof buf);
 #endif
 }
 




More information about the Gcrypt-devel mailing list