Add helper function for adding value to cipher block
authorJussi Kivilinna <jussi.kivilinna@iki.fi>
Sun, 31 Mar 2019 15:21:20 +0000 (18:21 +0300)
committerJussi Kivilinna <jussi.kivilinna@iki.fi>
Sun, 31 Mar 2019 15:21:20 +0000 (18:21 +0300)
* cipher/cipher-internal.h (cipher_block_add): New.
* cipher/blowfish.c (_gcry_blowfish_ctr_enc): Use new helper function
for CTR block increment.
* cipher/camellia-glue.c (_gcry_camellia_ctr_enc): Ditto.
* cipher/cast5.c (_gcry_cast5_ctr_enc): Ditto.
* cipher/cipher-ctr.c (_gcry_cipher_ctr_encrypt): Ditto.
* cipher/des.c (_gcry_3des_ctr_enc): Ditto.
* cipher/rijndael.c (_gcry_aes_ctr_enc): Ditto.
* cipher/serpent.c (_gcry_serpent_ctr_enc): Ditto.
* cipher/twofish.c (_gcry_twofish_ctr_enc): Ditto.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
cipher/blowfish.c
cipher/camellia-glue.c
cipher/cast5.c
cipher/cipher-ctr.c
cipher/cipher-internal.h
cipher/des.c
cipher/rijndael.c
cipher/serpent.c
cipher/twofish.c

index f032c5c..e7e199a 100644 (file)
@@ -619,7 +619,6 @@ _gcry_blowfish_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
   const unsigned char *inbuf = inbuf_arg;
   unsigned char tmpbuf[BLOWFISH_BLOCKSIZE];
   int burn_stack_depth = (64) + 2 * BLOWFISH_BLOCKSIZE;
-  int i;
 
 #ifdef USE_AMD64_ASM
   {
@@ -665,12 +664,7 @@ _gcry_blowfish_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
       outbuf += BLOWFISH_BLOCKSIZE;
       inbuf  += BLOWFISH_BLOCKSIZE;
       /* Increment the counter.  */
-      for (i = BLOWFISH_BLOCKSIZE; i > 0; i--)
-        {
-          ctr[i-1]++;
-          if (ctr[i-1])
-            break;
-        }
+      cipher_block_add (ctr, 1, BLOWFISH_BLOCKSIZE);
     }
 
   wipememory(tmpbuf, sizeof(tmpbuf));
index 69b240b..4b0989e 100644 (file)
@@ -363,7 +363,6 @@ _gcry_camellia_ctr_enc(void *context, unsigned char *ctr,
   const unsigned char *inbuf = inbuf_arg;
   unsigned char tmpbuf[CAMELLIA_BLOCK_SIZE];
   int burn_stack_depth = CAMELLIA_encrypt_stack_burn_size;
-  int i;
 
 #ifdef USE_AESNI_AVX2
   if (ctx->use_aesni_avx2)
@@ -434,12 +433,7 @@ _gcry_camellia_ctr_enc(void *context, unsigned char *ctr,
       outbuf += CAMELLIA_BLOCK_SIZE;
       inbuf  += CAMELLIA_BLOCK_SIZE;
       /* Increment the counter.  */
-      for (i = CAMELLIA_BLOCK_SIZE; i > 0; i--)
-        {
-          ctr[i-1]++;
-          if (ctr[i-1])
-            break;
-        }
+      cipher_block_add(ctr, 1, CAMELLIA_BLOCK_SIZE);
     }
 
   wipememory(tmpbuf, sizeof(tmpbuf));
index 49e8b78..cc5bd9d 100644 (file)
@@ -593,7 +593,6 @@ _gcry_cast5_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
   unsigned char tmpbuf[CAST5_BLOCKSIZE];
   int burn_stack_depth = (20 + 4 * sizeof(void*)) + 2 * CAST5_BLOCKSIZE;
 
-  int i;
 
 #ifdef USE_AMD64_ASM
   {
@@ -639,12 +638,7 @@ _gcry_cast5_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
       outbuf += CAST5_BLOCKSIZE;
       inbuf  += CAST5_BLOCKSIZE;
       /* Increment the counter.  */
-      for (i = CAST5_BLOCKSIZE; i > 0; i--)
-        {
-          ctr[i-1]++;
-          if (ctr[i-1])
-            break;
-        }
+      cipher_block_add (ctr, 1, CAST5_BLOCKSIZE);
     }
 
   wipememory(tmpbuf, sizeof(tmpbuf));
index 546d4f8..5f0afc2 100644 (file)
@@ -83,12 +83,7 @@ _gcry_cipher_ctr_encrypt (gcry_cipher_hd_t c,
           nburn = enc_fn (&c->context.c, tmp, c->u_ctr.ctr);
           burn = nburn > burn ? nburn : burn;
 
-          for (i = blocksize; i > 0; i--)
-            {
-              c->u_ctr.ctr[i-1]++;
-              if (c->u_ctr.ctr[i-1] != 0)
-                break;
-            }
+         cipher_block_add(c->u_ctr.ctr, 1, blocksize);
 
           if (inbuflen < blocksize)
             break;
index 2283bf3..970aa98 100644 (file)
@@ -628,6 +628,29 @@ static inline unsigned int _gcry_blocksize_shift(gcry_cipher_hd_t c)
 }
 
 
+/* Optimized function for adding value to cipher block. */
+static inline void
+cipher_block_add(void *_dstsrc, unsigned int add, size_t blocksize)
+{
+  byte *dstsrc = _dstsrc;
+  u64 s[2];
+
+  if (blocksize == 8)
+    {
+      buf_put_be64(dstsrc + 0, buf_get_be64(dstsrc + 0) + add);
+    }
+  else /* blocksize == 16 */
+    {
+      s[0] = buf_get_be64(dstsrc + 8);
+      s[1] = buf_get_be64(dstsrc + 0);
+      s[0] += add;
+      s[1] += (s[0] < add);
+      buf_put_be64(dstsrc + 8, s[0]);
+      buf_put_be64(dstsrc + 0, s[1]);
+    }
+}
+
+
 /* Optimized function for cipher block copying */
 static inline void
 cipher_block_cpy(void *_dst, const void *_src, size_t blocksize)
index a008b93..e4d10ca 100644 (file)
@@ -881,7 +881,6 @@ _gcry_3des_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
   const unsigned char *inbuf = inbuf_arg;
   unsigned char tmpbuf[DES_BLOCKSIZE];
   int burn_stack_depth = TRIPLEDES_ECB_BURN_STACK;
-  int i;
 
 #ifdef USE_AMD64_ASM
   {
@@ -913,12 +912,7 @@ _gcry_3des_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
       outbuf += DES_BLOCKSIZE;
       inbuf  += DES_BLOCKSIZE;
       /* Increment the counter.  */
-      for (i = DES_BLOCKSIZE; i > 0; i--)
-        {
-          ctr[i-1]++;
-          if (ctr[i-1])
-            break;
-        }
+      cipher_block_add(ctr, 1, DES_BLOCKSIZE);
     }
 
   wipememory(tmpbuf, sizeof(tmpbuf));
index 8094537..1001b1d 100644 (file)
@@ -928,7 +928,6 @@ _gcry_aes_ctr_enc (void *context, unsigned char *ctr,
   unsigned char *outbuf = outbuf_arg;
   const unsigned char *inbuf = inbuf_arg;
   unsigned int burn_depth = 0;
-  int i;
 
   if (0)
     ;
@@ -970,12 +969,7 @@ _gcry_aes_ctr_enc (void *context, unsigned char *ctr,
           outbuf += BLOCKSIZE;
           inbuf  += BLOCKSIZE;
           /* Increment the counter.  */
-          for (i = BLOCKSIZE; i > 0; i--)
-            {
-              ctr[i-1]++;
-              if (ctr[i-1])
-                break;
-            }
+         cipher_block_add(ctr, 1, BLOCKSIZE);
         }
 
       wipememory(&tmp, sizeof(tmp));
index 8e3faa7..71d843d 100644 (file)
@@ -912,7 +912,6 @@ _gcry_serpent_ctr_enc(void *context, unsigned char *ctr,
   const unsigned char *inbuf = inbuf_arg;
   unsigned char tmpbuf[sizeof(serpent_block_t)];
   int burn_stack_depth = 2 * sizeof (serpent_block_t);
-  int i;
 
 #ifdef USE_AVX2
   if (ctx->use_avx2)
@@ -1006,12 +1005,7 @@ _gcry_serpent_ctr_enc(void *context, unsigned char *ctr,
       outbuf += sizeof(serpent_block_t);
       inbuf  += sizeof(serpent_block_t);
       /* Increment the counter.  */
-      for (i = sizeof(serpent_block_t); i > 0; i--)
-        {
-          ctr[i-1]++;
-          if (ctr[i-1])
-            break;
-        }
+      cipher_block_add(ctr, 1, sizeof(serpent_block_t));
     }
 
   wipememory(tmpbuf, sizeof(tmpbuf));
index 51982c5..417d737 100644 (file)
@@ -1105,7 +1105,6 @@ _gcry_twofish_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
   const unsigned char *inbuf = inbuf_arg;
   unsigned char tmpbuf[TWOFISH_BLOCKSIZE];
   unsigned int burn, burn_stack_depth = 0;
-  int i;
 
 #ifdef USE_AVX2
   if (ctx->use_avx2)
@@ -1165,12 +1164,7 @@ _gcry_twofish_ctr_enc(void *context, unsigned char *ctr, void *outbuf_arg,
       outbuf += TWOFISH_BLOCKSIZE;
       inbuf  += TWOFISH_BLOCKSIZE;
       /* Increment the counter.  */
-      for (i = TWOFISH_BLOCKSIZE; i > 0; i--)
-        {
-          ctr[i-1]++;
-          if (ctr[i-1])
-            break;
-        }
+      cipher_block_add(ctr, 1, TWOFISH_BLOCKSIZE);
     }
 
   wipememory(tmpbuf, sizeof(tmpbuf));