From 4c3157497d4394cb6b09565a94091256aa3d5b54 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Sat, 2 Aug 2008 06:36:49 +0000 Subject: [PATCH] Make more uses of load/store_32_be/le, which on x86 at least expands to a single unaligned load/store instruction, instead of open-coding the shifts and masks. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20599 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/crypto/aes/aes_s2k.c | 5 +---- src/lib/crypto/arcfour/arcfour.c | 10 ++-------- src/lib/crypto/des/f_sched.c | 12 ++++-------- src/lib/crypto/md4/md4.c | 21 +++++---------------- src/lib/crypto/md5/md5.c | 18 +++--------------- src/lib/crypto/pbkdf2.c | 5 +---- src/lib/crypto/sha1/shs.c | 18 ++++++------------ 7 files changed, 22 insertions(+), 67 deletions(-) diff --git a/src/lib/crypto/aes/aes_s2k.c b/src/lib/crypto/aes/aes_s2k.c index 68d3111bf..30ca27501 100644 --- a/src/lib/crypto/aes/aes_s2k.c +++ b/src/lib/crypto/aes/aes_s2k.c @@ -51,10 +51,7 @@ krb5int_aes_string_to_key(const struct krb5_enc_provider *enc, if (params->length != 4) return KRB5_ERR_BAD_S2K_PARAMS; /* The first two need casts in case 'int' is 16 bits. */ - iter_count = (((unsigned long)p[0] << 24) - | ((unsigned long)p[1] << 16) - | (p[2] << 8) - | (p[3])); + iter_count = load_32_be(p); if (iter_count == 0) { iter_count = (1L << 16) << 16; if (((iter_count >> 16) >> 16) != 1) diff --git a/src/lib/crypto/arcfour/arcfour.c b/src/lib/crypto/arcfour/arcfour.c index 3481fadcb..a2df5ddf5 100644 --- a/src/lib/crypto/arcfour/arcfour.c +++ b/src/lib/crypto/arcfour/arcfour.c @@ -140,16 +140,10 @@ krb5_arcfour_encrypt(const struct krb5_enc_provider *enc, ms_usage=krb5int_arcfour_translate_usage(usage); if (key->enctype == ENCTYPE_ARCFOUR_HMAC_EXP) { strncpy(salt.data, l40, salt.length); - salt.data[10]=ms_usage & 0xff; - salt.data[11]=(ms_usage >> 8) & 0xff; - salt.data[12]=(ms_usage >> 16) & 0xff; - salt.data[13]=(ms_usage >> 24) & 0xff; + store_32_le(ms_usage, salt.data+10); } else { salt.length=4; - salt.data[0]=ms_usage & 0xff; - salt.data[1]=(ms_usage >> 8) & 0xff; - salt.data[2]=(ms_usage >> 16) & 0xff; - salt.data[3]=(ms_usage >> 24) & 0xff; + store_32_le(ms_usage, salt.data); } krb5_hmac(hash, key, 1, &salt, &d1); diff --git a/src/lib/crypto/des/f_sched.c b/src/lib/crypto/des/f_sched.c index 99d1dc313..5c269252b 100644 --- a/src/lib/crypto/des/f_sched.c +++ b/src/lib/crypto/des/f_sched.c @@ -242,10 +242,8 @@ mit_des_make_key_sched(mit_des_cblock key, mit_des_key_schedule schedule) * the right, while D0 gets 16 from the left and 12 from the * right. The code knows which bits go where. */ - tmp = ((unsigned DES_INT32)(*(k)++)) << 24; - tmp |= ((unsigned DES_INT32)(*(k)++)) << 16; - tmp |= ((unsigned DES_INT32)(*(k)++)) << 8; - tmp |= (unsigned DES_INT32)(*(k)++); /* left part of key */ + tmp = load_32_be(k), k += 4; + c = PC1_CL[(tmp >> 29) & 0x7] | (PC1_CL[(tmp >> 21) & 0x7] << 1) | (PC1_CL[(tmp >> 13) & 0x7] << 2) @@ -255,10 +253,8 @@ mit_des_make_key_sched(mit_des_cblock key, mit_des_key_schedule schedule) | (PC1_DL[(tmp >> 9) & 0xf] << 2) | (PC1_DL[(tmp >> 1) & 0xf] << 3); - tmp = ((unsigned DES_INT32)(*(k)++)) << 24; - tmp |= ((unsigned DES_INT32)(*(k)++)) << 16; - tmp |= ((unsigned DES_INT32)(*(k)++)) << 8; - tmp |= (unsigned DES_INT32)(*(k)++); /* right part of key */ + tmp = load_32_be(k), k += 4; + c |= PC1_CR[(tmp >> 28) & 0xf] | (PC1_CR[(tmp >> 20) & 0xf] << 1) | (PC1_CR[(tmp >> 12) & 0xf] << 2) diff --git a/src/lib/crypto/md4/md4.c b/src/lib/crypto/md4/md4.c index 5e95d359f..d1da9ef0c 100644 --- a/src/lib/crypto/md4/md4.c +++ b/src/lib/crypto/md4/md4.c @@ -110,11 +110,9 @@ krb5_MD4Update (krb5_MD4_CTX *mdContext, const unsigned char *inBuf, unsigned in /* transform if necessary */ if (mdi == 0x40) { - for (i = 0, ii = 0; i < 16; i++, ii += 4) - in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) | - (((krb5_ui_4)mdContext->in[ii+2]) << 16) | - (((krb5_ui_4)mdContext->in[ii+1]) << 8) | - ((krb5_ui_4)mdContext->in[ii]); + for (i = 0, ii = 0; i < 16; i++, ii += 4) { + in[i] = load_32_le(mdContext->in+ii); + } Transform (mdContext->buf, in); mdi = 0; } @@ -142,22 +140,13 @@ krb5_MD4Final (krb5_MD4_CTX *mdContext) /* append length in bits and transform */ for (i = 0, ii = 0; i < 14; i++, ii += 4) - in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) | - (((krb5_ui_4)mdContext->in[ii+2]) << 16) | - (((krb5_ui_4)mdContext->in[ii+1]) << 8) | - ((krb5_ui_4)mdContext->in[ii]); + in[i] = load_32_le(mdContext->in+ii); Transform (mdContext->buf, in); /* store buffer in digest */ for (i = 0, ii = 0; i < 4; i++, ii += 4) { - mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF); - mdContext->digest[ii+1] = - (unsigned char)((mdContext->buf[i] >> 8) & 0xFF); - mdContext->digest[ii+2] = - (unsigned char)((mdContext->buf[i] >> 16) & 0xFF); - mdContext->digest[ii+3] = - (unsigned char)((mdContext->buf[i] >> 24) & 0xFF); + store_32_le(mdContext->buf[i], mdContext->digest+ii); } } diff --git a/src/lib/crypto/md5/md5.c b/src/lib/crypto/md5/md5.c index 4b56755a8..da8aca451 100644 --- a/src/lib/crypto/md5/md5.c +++ b/src/lib/crypto/md5/md5.c @@ -146,10 +146,7 @@ krb5_MD5Update (krb5_MD5_CTX *mdContext, const unsigned char *inBuf, unsigned in /* transform if necessary */ if (mdi == 0x40) { for (i = 0, ii = 0; i < 16; i++, ii += 4) - in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) | - (((krb5_ui_4)mdContext->in[ii+2]) << 16) | - (((krb5_ui_4)mdContext->in[ii+1]) << 8) | - ((krb5_ui_4)mdContext->in[ii]); + in[i] = load_32_le(mdContext->in+ii); Transform (mdContext->buf, in); mdi = 0; } @@ -180,21 +177,12 @@ krb5_MD5Final (krb5_MD5_CTX *mdContext) /* append length in bits and transform */ for (i = 0, ii = 0; i < 14; i++, ii += 4) - in[i] = (((krb5_ui_4)mdContext->in[ii+3]) << 24) | - (((krb5_ui_4)mdContext->in[ii+2]) << 16) | - (((krb5_ui_4)mdContext->in[ii+1]) << 8) | - ((krb5_ui_4)mdContext->in[ii]); + in[i] = load_32_le(mdContext->in+ii); Transform (mdContext->buf, in); /* store buffer in digest */ for (i = 0, ii = 0; i < 4; i++, ii += 4) { - mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF); - mdContext->digest[ii+1] = - (unsigned char)((mdContext->buf[i] >> 8) & 0xFF); - mdContext->digest[ii+2] = - (unsigned char)((mdContext->buf[i] >> 16) & 0xFF); - mdContext->digest[ii+3] = - (unsigned char)((mdContext->buf[i] >> 24) & 0xFF); + store_32_le(mdContext->buf[i], mdContext->digest+ii); } } diff --git a/src/lib/crypto/pbkdf2.c b/src/lib/crypto/pbkdf2.c index a6cce1cd0..5b3286ef2 100644 --- a/src/lib/crypto/pbkdf2.c +++ b/src/lib/crypto/pbkdf2.c @@ -93,10 +93,7 @@ F(char *output, char *u_tmp1, char *u_tmp2, #endif /* Compute U_1. */ - ibytes[3] = i & 0xff; - ibytes[2] = (i >> 8) & 0xff; - ibytes[1] = (i >> 16) & 0xff; - ibytes[0] = (i >> 24) & 0xff; + store_32_be(i, ibytes); tlen = salt->length; memcpy(u_tmp2, salt->data, tlen); diff --git a/src/lib/crypto/sha1/shs.c b/src/lib/crypto/sha1/shs.c index a027fd76d..0089055d7 100644 --- a/src/lib/crypto/sha1/shs.c +++ b/src/lib/crypto/sha1/shs.c @@ -286,10 +286,8 @@ void shsUpdate(SHS_INFO *shsInfo, const SHS_BYTE *buffer, unsigned int count) count = 0; break; /* out of while loop */ } - *lp = (SHS_LONG) *buffer++ << 24; - *lp |= (SHS_LONG) *buffer++ << 16; - *lp |= (SHS_LONG) *buffer++ << 8; - *lp++ |= (SHS_LONG) *buffer++; + *lp++ = load_32_be(buffer); + buffer += 4; count -= 4; } if (canfill) { @@ -301,10 +299,8 @@ void shsUpdate(SHS_INFO *shsInfo, const SHS_BYTE *buffer, unsigned int count) while (count >= SHS_DATASIZE) { lp = shsInfo->data; while (lp < shsInfo->data + 16) { - *lp = ((SHS_LONG) *buffer++) << 24; - *lp |= ((SHS_LONG) *buffer++) << 16; - *lp |= ((SHS_LONG) *buffer++) << 8; - *lp++ |= (SHS_LONG) *buffer++; + *lp++ = load_32_be(buffer); + buffer += 4; } SHSTransform(shsInfo->digest, shsInfo->data); count -= SHS_DATASIZE; @@ -313,10 +309,8 @@ void shsUpdate(SHS_INFO *shsInfo, const SHS_BYTE *buffer, unsigned int count) if (count > 0) { lp = shsInfo->data; while (count > 4) { - *lp = ((SHS_LONG) *buffer++) << 24; - *lp |= ((SHS_LONG) *buffer++) << 16; - *lp |= ((SHS_LONG) *buffer++) << 8; - *lp++ |= (SHS_LONG) *buffer++; + *lp++ = load_32_be(buffer); + buffer += 4; count -= 4; } *lp = 0; -- 2.26.2