summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Kivilinna <jussi.kivilinna@iki.fi>2019-03-31 18:21:20 +0300
committerJussi Kivilinna <jussi.kivilinna@iki.fi>2019-03-31 18:21:20 +0300
commit0fe918fa897cca9e01cbdb80d14106cfe5af680e (patch)
treebc38e691a6734ef4578adfbb47daa16b7c8aa711
parentefd700e31dc8e1e386d367d1b682000977e0c810 (diff)
downloadlibgcrypt-0fe918fa897cca9e01cbdb80d14106cfe5af680e.tar.gz
Add helper function for adding value to cipher block
* 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>
-rw-r--r--cipher/blowfish.c8
-rw-r--r--cipher/camellia-glue.c8
-rw-r--r--cipher/cast5.c8
-rw-r--r--cipher/cipher-ctr.c7
-rw-r--r--cipher/cipher-internal.h23
-rw-r--r--cipher/des.c8
-rw-r--r--cipher/rijndael.c8
-rw-r--r--cipher/serpent.c8
-rw-r--r--cipher/twofish.c8
9 files changed, 31 insertions, 55 deletions
diff --git a/cipher/blowfish.c b/cipher/blowfish.c
index f032c5c6..e7e199af 100644
--- a/cipher/blowfish.c
+++ b/cipher/blowfish.c
@@ -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));
diff --git a/cipher/camellia-glue.c b/cipher/camellia-glue.c
index 69b240b7..4b0989ea 100644
--- a/cipher/camellia-glue.c
+++ b/cipher/camellia-glue.c
@@ -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));
diff --git a/cipher/cast5.c b/cipher/cast5.c
index 49e8b781..cc5bd9d6 100644
--- a/cipher/cast5.c
+++ b/cipher/cast5.c
@@ -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));
diff --git a/cipher/cipher-ctr.c b/cipher/cipher-ctr.c
index 546d4f8e..5f0afc2f 100644
--- a/cipher/cipher-ctr.c
+++ b/cipher/cipher-ctr.c
@@ -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;
diff --git a/cipher/cipher-internal.h b/cipher/cipher-internal.h
index 2283bf31..970aa986 100644
--- a/cipher/cipher-internal.h
+++ b/cipher/cipher-internal.h
@@ -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)
diff --git a/cipher/des.c b/cipher/des.c
index a008b93e..e4d10caa 100644
--- a/cipher/des.c
+++ b/cipher/des.c
@@ -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));
diff --git a/cipher/rijndael.c b/cipher/rijndael.c
index 80945376..1001b1d5 100644
--- a/cipher/rijndael.c
+++ b/cipher/rijndael.c
@@ -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));
diff --git a/cipher/serpent.c b/cipher/serpent.c
index 8e3faa7c..71d843d0 100644
--- a/cipher/serpent.c
+++ b/cipher/serpent.c
@@ -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));
diff --git a/cipher/twofish.c b/cipher/twofish.c
index 51982c53..417d7378 100644
--- a/cipher/twofish.c
+++ b/cipher/twofish.c
@@ -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));