summaryrefslogtreecommitdiff
path: root/cipher/cipher.c
diff options
context:
space:
mode:
authorJussi Kivilinna <jussi.kivilinna@iki.fi>2013-10-26 14:51:44 +0300
committerJussi Kivilinna <jussi.kivilinna@iki.fi>2013-10-26 14:51:44 +0300
commit51f1beab3d1e879942a95f58b08de7dbcce75dce (patch)
treebeb85485bcc52f62c432735583761900570029af /cipher/cipher.c
parentd9431725952e40f201c7eda000d3c8511ebd5b33 (diff)
downloadlibgcrypt-51f1beab3d1e879942a95f58b08de7dbcce75dce.tar.gz
Deduplicate code for ECB encryption and decryption
* cipher/cipher.c (do_ecb_crypt): New, based on old 'do_ecb_encrypt'. (do_ecb_encrypt): Use 'do_ecb_crypt', pass encryption function. (do_ecb_decrypt): Use 'do_ecb_crypt', pass decryption function. -- Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Diffstat (limited to 'cipher/cipher.c')
-rw-r--r--cipher/cipher.c44
1 files changed, 14 insertions, 30 deletions
diff --git a/cipher/cipher.c b/cipher/cipher.c
index df6d2025..73a97b11 100644
--- a/cipher/cipher.c
+++ b/cipher/cipher.c
@@ -627,11 +627,11 @@ cipher_reset (gcry_cipher_hd_t c)
static gcry_err_code_t
-do_ecb_encrypt (gcry_cipher_hd_t c,
- unsigned char *outbuf, unsigned int outbuflen,
- const unsigned char *inbuf, unsigned int inbuflen)
+do_ecb_crypt (gcry_cipher_hd_t c,
+ unsigned char *outbuf, unsigned int outbuflen,
+ const unsigned char *inbuf, unsigned int inbuflen,
+ gcry_cipher_encrypt_t crypt_fn)
{
- gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
unsigned int blocksize = c->spec->blocksize;
unsigned int n, nblocks;
unsigned int burn, nburn;
@@ -646,7 +646,7 @@ do_ecb_encrypt (gcry_cipher_hd_t c,
for (n=0; n < nblocks; n++ )
{
- nburn = enc_fn (&c->context.c, outbuf, (byte*)/*arggg*/inbuf);
+ nburn = crypt_fn (&c->context.c, outbuf, inbuf);
burn = nburn > burn ? nburn : burn;
inbuf += blocksize;
outbuf += blocksize;
@@ -659,35 +659,19 @@ do_ecb_encrypt (gcry_cipher_hd_t c,
}
static gcry_err_code_t
-do_ecb_decrypt (gcry_cipher_hd_t c,
+do_ecb_encrypt (gcry_cipher_hd_t c,
unsigned char *outbuf, unsigned int outbuflen,
const unsigned char *inbuf, unsigned int inbuflen)
{
- gcry_cipher_decrypt_t dec_fn = c->spec->decrypt;
- unsigned int blocksize = c->spec->blocksize;
- unsigned int n, nblocks;
- unsigned int burn, nburn;
-
- if (outbuflen < inbuflen)
- return GPG_ERR_BUFFER_TOO_SHORT;
- if ((inbuflen % blocksize))
- return GPG_ERR_INV_LENGTH;
-
- nblocks = inbuflen / blocksize;
- burn = 0;
-
- for (n=0; n < nblocks; n++ )
- {
- nburn = dec_fn (&c->context.c, outbuf, (byte*)/*arggg*/inbuf);
- burn = nburn > burn ? nburn : burn;
- inbuf += blocksize;
- outbuf += blocksize;
- }
-
- if (burn > 0)
- _gcry_burn_stack (burn + 4 * sizeof(void *));
+ return do_ecb_crypt (c, outbuf, outbuflen, inbuf, inbuflen, c->spec->encrypt);
+}
- return 0;
+static gcry_err_code_t
+do_ecb_decrypt (gcry_cipher_hd_t c,
+ unsigned char *outbuf, unsigned int outbuflen,
+ const unsigned char *inbuf, unsigned int inbuflen)
+{
+ return do_ecb_crypt (c, outbuf, outbuflen, inbuf, inbuflen, c->spec->decrypt);
}