diff options
author | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2013-09-21 13:54:38 +0300 |
---|---|---|
committer | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2013-09-21 14:08:24 +0300 |
commit | 9337e03824a5bdd3bbbcb8382cabefe6d6c32e1e (patch) | |
tree | cf6f796989327c58327d3178f312d2876f268483 /cipher/serpent.c | |
parent | 7409de7bc28ff8847c9d71d8c3e35e1968d59d60 (diff) | |
download | libgcrypt-9337e03824a5bdd3bbbcb8382cabefe6d6c32e1e.tar.gz |
Optimize and cleanup 32-bit and 64-bit endianess transforms
* cipher/bithelp.h (bswap32, bswap64, le_bswap32, be_bswap32)
(le_bswap64, be_bswap64): New.
* cipher/bufhelp.h (buf_get_be32, buf_get_le32, buf_put_le32)
(buf_put_be32, buf_get_be64, buf_get_le64, buf_put_be64)
(buf_put_le64): New.
* cipher/blowfish.c (do_encrypt_block, do_decrypt_block): Use new
endian conversion helpers.
(do_bf_setkey): Turn endian specific code to generic.
* cipher/camellia.c (GETU32, PUTU32): Use new endian conversion
helpers.
* cipher/cast5.c (rol): Remove, use rol from bithelp.
(F1, F2, F3): Fix to use rol from bithelp.
(do_encrypt_block, do_decrypt_block, do_cast_setkey): Use new endian
conversion helpers.
* cipher/des.c (READ_64BIT_DATA, WRITE_64BIT_DATA): Ditto.
* cipher/md4.c (transform, md4_final): Ditto.
* cipher/md5.c (transform, md5_final): Ditto.
* cipher/rmd160.c (transform, rmd160_final): Ditto.
* cipher/salsa20.c (LE_SWAP32, LE_READ_UINT32): Ditto.
* cipher/scrypt.c (READ_UINT64, LE_READ_UINT64, LE_SWAP32): Ditto.
* cipher/seed.c (GETU32, PUTU32): Ditto.
* cipher/serpent.c (byte_swap_32): Remove.
(serpent_key_prepare, serpent_encrypt_internal)
(serpent_decrypt_internal): Use new endian conversion helpers.
* cipher/sha1.c (transform, sha1_final): Ditto.
* cipher/sha256.c (transform, sha256_final): Ditto.
* cipher/sha512.c (__transform, sha512_final): Ditto.
* cipher/stribog.c (transform, stribog_final): Ditto.
* cipher/tiger.c (transform, tiger_final): Ditto.
* cipher/twofish.c (INPACK, OUTUNPACK): Ditto.
* cipher/whirlpool.c (buffer_to_block, block_to_buffer): Ditto.
* configure.ac (gcry_cv_have_builtin_bswap32): Check for compiler
provided __builtin_bswap32.
(gcry_cv_have_builtin_bswap64): Check for compiler provided
__builtin_bswap64.
--
Patch add helper functions that provide conversions to/from integers and
buffers of different endianess. Benefits are code cleanup and optimization
for architectures that have byte-swaping instructions and/or can do fast
unaligned memory accesses.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Diffstat (limited to 'cipher/serpent.c')
-rw-r--r-- | cipher/serpent.c | 57 |
1 files changed, 18 insertions, 39 deletions
diff --git a/cipher/serpent.c b/cipher/serpent.c index 72895ede..4720b9c6 100644 --- a/cipher/serpent.c +++ b/cipher/serpent.c @@ -119,11 +119,6 @@ extern void _gcry_serpent_avx2_cfb_dec(serpent_context_t *ctx, static const char *serpent_test (void); -#define byte_swap_32(x) \ - (0 \ - | (((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) \ - | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) - /* * These are the S-Boxes of Serpent from following research paper. * @@ -548,14 +543,10 @@ serpent_key_prepare (const byte *key, unsigned int key_length, int i; /* Copy key. */ - memcpy (key_prepared, key, key_length); key_length /= 4; -#ifdef WORDS_BIGENDIAN for (i = 0; i < key_length; i++) - key_prepared[i] = byte_swap_32 (key_prepared[i]); -#else - i = key_length; -#endif + key_prepared[i] = buf_get_le32 (key + i * 4); + if (i < 8) { /* Key must be padded according to the Serpent @@ -683,13 +674,10 @@ serpent_encrypt_internal (serpent_context_t *context, serpent_block_t b, b_next; int round = 0; - memcpy (b, input, sizeof (b)); -#ifdef WORDS_BIGENDIAN - b[0] = byte_swap_32 (b[0]); - b[1] = byte_swap_32 (b[1]); - b[2] = byte_swap_32 (b[2]); - b[3] = byte_swap_32 (b[3]); -#endif + b[0] = buf_get_le32 (input + 0); + b[1] = buf_get_le32 (input + 4); + b[2] = buf_get_le32 (input + 8); + b[3] = buf_get_le32 (input + 12); ROUND (0, context->keys, b, b_next); ROUND (1, context->keys, b, b_next); @@ -725,13 +713,10 @@ serpent_encrypt_internal (serpent_context_t *context, ROUND_LAST (7, context->keys, b, b_next); -#ifdef WORDS_BIGENDIAN - b_next[0] = byte_swap_32 (b_next[0]); - b_next[1] = byte_swap_32 (b_next[1]); - b_next[2] = byte_swap_32 (b_next[2]); - b_next[3] = byte_swap_32 (b_next[3]); -#endif - memcpy (output, b_next, sizeof (b_next)); + buf_put_le32 (output + 0, b_next[0]); + buf_put_le32 (output + 4, b_next[1]); + buf_put_le32 (output + 8, b_next[2]); + buf_put_le32 (output + 12, b_next[3]); } static void @@ -741,13 +726,10 @@ serpent_decrypt_internal (serpent_context_t *context, serpent_block_t b, b_next; int round = ROUNDS; - memcpy (b_next, input, sizeof (b)); -#ifdef WORDS_BIGENDIAN - b_next[0] = byte_swap_32 (b_next[0]); - b_next[1] = byte_swap_32 (b_next[1]); - b_next[2] = byte_swap_32 (b_next[2]); - b_next[3] = byte_swap_32 (b_next[3]); -#endif + b_next[0] = buf_get_le32 (input + 0); + b_next[1] = buf_get_le32 (input + 4); + b_next[2] = buf_get_le32 (input + 8); + b_next[3] = buf_get_le32 (input + 12); ROUND_FIRST_INVERSE (7, context->keys, b_next, b); @@ -783,13 +765,10 @@ serpent_decrypt_internal (serpent_context_t *context, ROUND_INVERSE (1, context->keys, b, b_next); ROUND_INVERSE (0, context->keys, b, b_next); -#ifdef WORDS_BIGENDIAN - b_next[0] = byte_swap_32 (b_next[0]); - b_next[1] = byte_swap_32 (b_next[1]); - b_next[2] = byte_swap_32 (b_next[2]); - b_next[3] = byte_swap_32 (b_next[3]); -#endif - memcpy (output, b_next, sizeof (b_next)); + buf_put_le32 (output + 0, b_next[0]); + buf_put_le32 (output + 4, b_next[1]); + buf_put_le32 (output + 8, b_next[2]); + buf_put_le32 (output + 12, b_next[3]); } static unsigned int |