summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorphk <phk@FreeBSD.org>2002-06-22 12:54:11 +0000
committerphk <phk@FreeBSD.org>2002-06-22 12:54:11 +0000
commit5f785ab6ddf246014a12006e50bb06e8a8bb9fe7 (patch)
tree366f9e7a79d3e8fb4f9fc054bf145663fc36d7f4
parent5d43ad8ce6600bf1aae971105f88f49e10d3317d (diff)
downloadFreeBSD-src-5f785ab6ddf246014a12006e50bb06e8a8bb9fe7.zip
FreeBSD-src-5f785ab6ddf246014a12006e50bb06e8a8bb9fe7.tar.gz
Improve the handling of Encode and Decode operations in MD5.
Use memcpy for all little-endian architectures, sys/kern/md5c.c indicates this should be safe for all currently supported LE archs. Change the Encode and Decode functions for other archs to use le32toh() and htole32() functions instead of explicit byte shuffling. On sparc64 this gives md5(1) about 8% speed increase.
-rw-r--r--lib/libmd/md5c.c37
1 files changed, 15 insertions, 22 deletions
diff --git a/lib/libmd/md5c.c b/lib/libmd/md5c.c
index 954d7f8..d459e71 100644
--- a/lib/libmd/md5c.c
+++ b/lib/libmd/md5c.c
@@ -37,6 +37,8 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#endif
+#include <machine/endian.h>
+#include <sys/endian.h>
#include <sys/md5.h>
static void MD5Transform(u_int32_t [4], const unsigned char [64]);
@@ -46,10 +48,10 @@ static void MD5Transform(u_int32_t [4], const unsigned char [64]);
#define memcpy(x,y,z) bcopy(y, x, z)
#endif
-#ifdef i386
+#if (BYTE_ORDER == LITTLE_ENDIAN)
#define Encode memcpy
#define Decode memcpy
-#else /* i386 */
+#else
/*
* Encodes input (u_int32_t) into output (unsigned char). Assumes len is
@@ -57,19 +59,13 @@ static void MD5Transform(u_int32_t [4], const unsigned char [64]);
*/
static void
-Encode (output, input, len)
- unsigned char *output;
- u_int32_t *input;
- unsigned int len;
+Encode (unsigned char *output, u_int32_t *input, unsigned int len)
{
- unsigned int i, j;
+ unsigned int i;
+ u_int32_t *op = (u_int32_t *)output;
- for (i = 0, j = 0; j < len; i++, j += 4) {
- output[j] = (unsigned char)(input[i] & 0xff);
- output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
- output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
- output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
- }
+ for (i = 0; i < len / 4; i++)
+ op[i] = htole32(input[i]);
}
/*
@@ -78,18 +74,15 @@ Encode (output, input, len)
*/
static void
-Decode (output, input, len)
- u_int32_t *output;
- const unsigned char *input;
- unsigned int len;
+Decode (u_int32_t *output, const unsigned char *input, unsigned int len)
{
- unsigned int i, j;
+ unsigned int i;
+ u_int32_t *ip = (u_int32_t *)input;
- for (i = 0, j = 0; j < len; i++, j += 4)
- output[i] = ((u_int32_t)input[j]) | (((u_int32_t)input[j+1]) << 8) |
- (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24);
+ for (i = 0; i < len / 4; i++)
+ output[i] = le32toh(ip[i]);
}
-#endif /* i386 */
+#endif
static unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
OpenPOWER on IntegriCloud