summaryrefslogtreecommitdiff
path: root/cipher/sha512.c
diff options
context:
space:
mode:
authorJussi Kivilinna <jussi.kivilinna@iki.fi>2013-09-21 13:54:38 +0300
committerJussi Kivilinna <jussi.kivilinna@iki.fi>2013-09-21 14:08:24 +0300
commit9337e03824a5bdd3bbbcb8382cabefe6d6c32e1e (patch)
treecf6f796989327c58327d3178f312d2876f268483 /cipher/sha512.c
parent7409de7bc28ff8847c9d71d8c3e35e1968d59d60 (diff)
downloadlibgcrypt-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/sha512.c')
-rw-r--r--cipher/sha512.c51
1 files changed, 6 insertions, 45 deletions
diff --git a/cipher/sha512.c b/cipher/sha512.c
index 26cbe14d..505a1e46 100644
--- a/cipher/sha512.c
+++ b/cipher/sha512.c
@@ -50,6 +50,7 @@
#include <string.h>
#include "g10lib.h"
#include "bithelp.h"
+#include "bufhelp.h"
#include "cipher.h"
#include "hash-common.h"
@@ -225,26 +226,8 @@ __transform (SHA512_STATE *hd, const unsigned char *data)
g = hd->h6;
h = hd->h7;
-#ifdef WORDS_BIGENDIAN
- memcpy (w, data, 128);
-#else
- {
- int i;
- byte *p2;
-
- for (i = 0, p2 = (byte *) w; i < 16; i++, p2 += 8)
- {
- p2[7] = *data++;
- p2[6] = *data++;
- p2[5] = *data++;
- p2[4] = *data++;
- p2[3] = *data++;
- p2[2] = *data++;
- p2[1] = *data++;
- p2[0] = *data++;
- }
- }
-#endif
+ for ( t = 0; t < 16; t++ )
+ w[t] = buf_get_be64(data + t * 8);
#define S0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
#define S1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
@@ -566,35 +549,13 @@ sha512_final (void *context)
memset (hd->bctx.buf, 0, 112); /* fill next block with zeroes */
}
/* append the 128 bit count */
- hd->bctx.buf[112] = msb >> 56;
- hd->bctx.buf[113] = msb >> 48;
- hd->bctx.buf[114] = msb >> 40;
- hd->bctx.buf[115] = msb >> 32;
- hd->bctx.buf[116] = msb >> 24;
- hd->bctx.buf[117] = msb >> 16;
- hd->bctx.buf[118] = msb >> 8;
- hd->bctx.buf[119] = msb;
-
- hd->bctx.buf[120] = lsb >> 56;
- hd->bctx.buf[121] = lsb >> 48;
- hd->bctx.buf[122] = lsb >> 40;
- hd->bctx.buf[123] = lsb >> 32;
- hd->bctx.buf[124] = lsb >> 24;
- hd->bctx.buf[125] = lsb >> 16;
- hd->bctx.buf[126] = lsb >> 8;
- hd->bctx.buf[127] = lsb;
+ buf_put_be64(hd->bctx.buf + 112, msb);
+ buf_put_be64(hd->bctx.buf + 120, lsb);
stack_burn_depth = transform (hd, hd->bctx.buf);
_gcry_burn_stack (stack_burn_depth);
p = hd->bctx.buf;
-#ifdef WORDS_BIGENDIAN
-#define X(a) do { *(u64*)p = hd->state.h##a ; p += 8; } while (0)
-#else /* little endian */
-#define X(a) do { *p++ = hd->state.h##a >> 56; *p++ = hd->state.h##a >> 48; \
- *p++ = hd->state.h##a >> 40; *p++ = hd->state.h##a >> 32; \
- *p++ = hd->state.h##a >> 24; *p++ = hd->state.h##a >> 16; \
- *p++ = hd->state.h##a >> 8; *p++ = hd->state.h##a; } while(0)
-#endif
+#define X(a) do { *(u64*)p = be_bswap64(hd->state.h##a) ; p += 8; } while (0)
X (0);
X (1);
X (2);