Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/libalpm/sha2.c
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-01-06 16:02:52 -0600
committerDan McGee <dan@archlinux.org>2012-01-07 11:27:41 -0600
commitd59324c8fff0a91930bf226d013273691f0580e6 (patch)
treea357aae15d3e0e9d4a9f710edce18d6b3ed27c70 /lib/libalpm/sha2.c
parent6513b0ba591e44b4da75aa924046a34b8a269e9d (diff)
Use 32-bit wide integer type in PolarSSL code
A look at what this does on 64 bit systems since we were using the unnecessarily large 'unsigned long' type before even though it was 64 bits wide: $ ~/bin/bloat-o-meter libalpm.so.old lib/libalpm/.libs/libalpm.so add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-10412 (-10412) function old new delta md5_finish 370 356 -14 sha2_finish 547 531 -16 md5_process 3762 2643 -1119 sha2_process 20356 11093 -9263 The code size is nearly halved in the sha2 case (44% smaller code size), and md5 gets a nice size reduction (27% smaller) as well. We also move base64 code to <stdint.h> types as well; we can use 'uint32_t' rather than 'unsigned long' for at least two variables in the decode function. This doesn't net the same size benefit as the hash code case, but it is more proper. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/sha2.c')
-rw-r--r--lib/libalpm/sha2.c87
1 files changed, 44 insertions, 43 deletions
diff --git a/lib/libalpm/sha2.c b/lib/libalpm/sha2.c
index 3632c131..366dc650 100644
--- a/lib/libalpm/sha2.c
+++ b/lib/libalpm/sha2.c
@@ -38,28 +38,29 @@
* * removal of SELF_TEST code
* * removal of ipad and opad from the sha2_context struct in sha2.h
* * increase the size of buffer for performance reasons
- * * various static changes
+ * * change 'unsigned long' to uint32_t
*/
#include <stdio.h>
+#include <stdint.h>
#include "sha2.h"
/*
* 32-bit integer manipulation macros (big endian)
*/
-#ifndef GET_ULONG_BE
-#define GET_ULONG_BE(n,b,i) \
+#ifndef GET_U32_BE
+#define GET_U32_BE(n,b,i) \
{ \
- (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
- | ( (unsigned long) (b)[(i) + 1] << 16 ) \
- | ( (unsigned long) (b)[(i) + 2] << 8 ) \
- | ( (unsigned long) (b)[(i) + 3] ); \
+ (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
+ | ( (uint32_t) (b)[(i) + 1] << 16 ) \
+ | ( (uint32_t) (b)[(i) + 2] << 8 ) \
+ | ( (uint32_t) (b)[(i) + 3] ); \
}
#endif
-#ifndef PUT_ULONG_BE
-#define PUT_ULONG_BE(n,b,i) \
+#ifndef PUT_U32_BE
+#define PUT_U32_BE(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
@@ -106,25 +107,25 @@ static void sha2_starts( sha2_context *ctx, int is224 )
static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
{
- unsigned long temp1, temp2, W[64];
- unsigned long A, B, C, D, E, F, G, H;
-
- GET_ULONG_BE( W[ 0], data, 0 );
- GET_ULONG_BE( W[ 1], data, 4 );
- GET_ULONG_BE( W[ 2], data, 8 );
- GET_ULONG_BE( W[ 3], data, 12 );
- GET_ULONG_BE( W[ 4], data, 16 );
- GET_ULONG_BE( W[ 5], data, 20 );
- GET_ULONG_BE( W[ 6], data, 24 );
- GET_ULONG_BE( W[ 7], data, 28 );
- GET_ULONG_BE( W[ 8], data, 32 );
- GET_ULONG_BE( W[ 9], data, 36 );
- GET_ULONG_BE( W[10], data, 40 );
- GET_ULONG_BE( W[11], data, 44 );
- GET_ULONG_BE( W[12], data, 48 );
- GET_ULONG_BE( W[13], data, 52 );
- GET_ULONG_BE( W[14], data, 56 );
- GET_ULONG_BE( W[15], data, 60 );
+ uint32_t temp1, temp2, W[64];
+ uint32_t A, B, C, D, E, F, G, H;
+
+ GET_U32_BE( W[ 0], data, 0 );
+ GET_U32_BE( W[ 1], data, 4 );
+ GET_U32_BE( W[ 2], data, 8 );
+ GET_U32_BE( W[ 3], data, 12 );
+ GET_U32_BE( W[ 4], data, 16 );
+ GET_U32_BE( W[ 5], data, 20 );
+ GET_U32_BE( W[ 6], data, 24 );
+ GET_U32_BE( W[ 7], data, 28 );
+ GET_U32_BE( W[ 8], data, 32 );
+ GET_U32_BE( W[ 9], data, 36 );
+ GET_U32_BE( W[10], data, 40 );
+ GET_U32_BE( W[11], data, 44 );
+ GET_U32_BE( W[12], data, 48 );
+ GET_U32_BE( W[13], data, 52 );
+ GET_U32_BE( W[14], data, 56 );
+ GET_U32_BE( W[15], data, 60 );
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
@@ -241,7 +242,7 @@ static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
static void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen )
{
size_t fill;
- unsigned long left;
+ uint32_t left;
if( ilen <= 0 )
return;
@@ -249,10 +250,10 @@ static void sha2_update( sha2_context *ctx, const unsigned char *input, size_t i
left = ctx->total[0] & 0x3F;
fill = 64 - left;
- ctx->total[0] += (unsigned long) ilen;
+ ctx->total[0] += (uint32_t) ilen;
ctx->total[0] &= 0xFFFFFFFF;
- if( ctx->total[0] < (unsigned long) ilen )
+ if( ctx->total[0] < (uint32_t) ilen )
ctx->total[1]++;
if( left && ilen >= fill )
@@ -292,16 +293,16 @@ static const unsigned char sha2_padding[64] =
*/
static void sha2_finish( sha2_context *ctx, unsigned char output[32] )
{
- unsigned long last, padn;
- unsigned long high, low;
+ uint32_t last, padn;
+ uint32_t high, low;
unsigned char msglen[8];
high = ( ctx->total[0] >> 29 )
| ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 );
- PUT_ULONG_BE( high, msglen, 0 );
- PUT_ULONG_BE( low, msglen, 4 );
+ PUT_U32_BE( high, msglen, 0 );
+ PUT_U32_BE( low, msglen, 4 );
last = ctx->total[0] & 0x3F;
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
@@ -309,16 +310,16 @@ static void sha2_finish( sha2_context *ctx, unsigned char output[32] )
sha2_update( ctx, (unsigned char *) sha2_padding, padn );
sha2_update( ctx, msglen, 8 );
- PUT_ULONG_BE( ctx->state[0], output, 0 );
- PUT_ULONG_BE( ctx->state[1], output, 4 );
- PUT_ULONG_BE( ctx->state[2], output, 8 );
- PUT_ULONG_BE( ctx->state[3], output, 12 );
- PUT_ULONG_BE( ctx->state[4], output, 16 );
- PUT_ULONG_BE( ctx->state[5], output, 20 );
- PUT_ULONG_BE( ctx->state[6], output, 24 );
+ PUT_U32_BE( ctx->state[0], output, 0 );
+ PUT_U32_BE( ctx->state[1], output, 4 );
+ PUT_U32_BE( ctx->state[2], output, 8 );
+ PUT_U32_BE( ctx->state[3], output, 12 );
+ PUT_U32_BE( ctx->state[4], output, 16 );
+ PUT_U32_BE( ctx->state[5], output, 20 );
+ PUT_U32_BE( ctx->state[6], output, 24 );
if( ctx->is224 == 0 )
- PUT_ULONG_BE( ctx->state[7], output, 28 );
+ PUT_U32_BE( ctx->state[7], output, 28 );
}
/*