[PATCH 3/3] Use memset instead of setting buffers byte by byte

Jussi Kivilinna jussi.kivilinna at iki.fi
Thu Mar 21 20:36:30 CET 2019


* cipher/cipher-ccm.c (do_cbc_mac): Replace buffer setting loop with memset call.
* cipher/cipher-gcm.c (do_ghash_buf): Ditto.
* cipher/poly1305.c (poly1305_final): Ditto.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 0 files changed

diff --git a/cipher/cipher-ccm.c b/cipher/cipher-ccm.c
index 3bacb6b16..dcb268d08 100644
--- a/cipher/cipher-ccm.c
+++ b/cipher/cipher-ccm.c
@@ -65,8 +65,12 @@ do_cbc_mac (gcry_cipher_hd_t c, const unsigned char *inbuf, size_t inlen,
           if (!do_padding)
             break;
 
-          while (unused < blocksize)
-            c->u_mode.ccm.macbuf[unused++] = 0;
+	  n = blocksize - unused;
+	  if (n > 0)
+	    {
+	      memset (&c->u_mode.ccm.macbuf[unused], 0, n);
+	      unused = blocksize;
+	    }
         }
 
       if (unused > 0)
diff --git a/cipher/cipher-gcm.c b/cipher/cipher-gcm.c
index f9ddbc568..4fdd61207 100644
--- a/cipher/cipher-gcm.c
+++ b/cipher/cipher-gcm.c
@@ -525,8 +525,12 @@ do_ghash_buf(gcry_cipher_hd_t c, byte *hash, const byte *buf,
           if (!do_padding)
             break;
 
-          while (unused < blocksize)
-            c->u_mode.gcm.macbuf[unused++] = 0;
+	  n = blocksize - unused;
+	  if (n > 0)
+	    {
+	      memset (&c->u_mode.gcm.macbuf[unused], 0, n);
+	      unused = blocksize;
+	    }
         }
 
       if (unused > 0)
diff --git a/cipher/poly1305.c b/cipher/poly1305.c
index 8de6cd5e6..cded7cb2e 100644
--- a/cipher/poly1305.c
+++ b/cipher/poly1305.c
@@ -202,8 +202,12 @@ static unsigned int poly1305_final (poly1305_context_t *ctx,
   if (ctx->leftover)
     {
       ctx->buffer[ctx->leftover++] = 1;
-      for (; ctx->leftover < POLY1305_BLOCKSIZE; ctx->leftover++)
-	ctx->buffer[ctx->leftover] = 0;
+      if (ctx->leftover < POLY1305_BLOCKSIZE)
+	{
+	  memset (&ctx->buffer[ctx->leftover], 0,
+		  POLY1305_BLOCKSIZE - ctx->leftover);
+	  ctx->leftover = POLY1305_BLOCKSIZE;
+	}
       burn = poly1305_blocks (ctx, ctx->buffer, POLY1305_BLOCKSIZE, 0);
     }
 
@@ -398,8 +402,12 @@ static unsigned int poly1305_final (poly1305_context_t *ctx,
   if (ctx->leftover)
     {
       ctx->buffer[ctx->leftover++] = 1;
-      for (; ctx->leftover < POLY1305_BLOCKSIZE; ctx->leftover++)
-	ctx->buffer[ctx->leftover] = 0;
+      if (ctx->leftover < POLY1305_BLOCKSIZE)
+	{
+	  memset (&ctx->buffer[ctx->leftover], 0,
+		  POLY1305_BLOCKSIZE - ctx->leftover);
+	  ctx->leftover = POLY1305_BLOCKSIZE;
+	}
       burn = poly1305_blocks (ctx, ctx->buffer, POLY1305_BLOCKSIZE, 0);
     }
 




More information about the Gcrypt-devel mailing list