diff --git a/lib/md5.c b/lib/md5.c index 0c17abb5..a01ab0d7 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -1,28 +1,54 @@ /* - * Modified from Polarssl here + * Loosely based from GPL2+later Polarssl MD5 available here in its + * original form: + * * http://polarssl.org/show_source?file=md5 - * under GPL2 or later */ #include #include +#include + +#ifdef htobe16 +#else +/* Conversion interfaces. */ +# include + +# if __BYTE_ORDER == __LITTLE_ENDIAN +# define htobe16(x) __bswap_16 (x) +# define htole16(x) (x) +# define be16toh(x) __bswap_16 (x) +# define le16toh(x) (x) + +# define htobe32(x) __bswap_32 (x) +# define htole32(x) (x) +# define be32toh(x) __bswap_32 (x) +# define le32toh(x) (x) + +# define htobe64(x) __bswap_64 (x) +# define htole64(x) (x) +# define be64toh(x) __bswap_64 (x) +# define le64toh(x) (x) +# else +# define htobe16(x) (x) +# define htole16(x) __bswap_16 (x) +# define be16toh(x) (x) +# define le16toh(x) __bswap_16 (x) + +# define htobe32(x) (x) +# define htole32(x) __bswap_32 (x) +# define be32toh(x) (x) +# define le32toh(x) __bswap_32 (x) + +# define htobe64(x) (x) +# define htole64(x) __bswap_64 (x) +# define be64toh(x) (x) +# define le64toh(x) __bswap_64 (x) +# endif +#endif -#define GET_ULONG_LE(n, b, i) \ -{ \ - (n) = ((unsigned long)(b)[i]) \ - | ((unsigned long)(b)[(i) + 1] << 8) \ - | ((unsigned long)(b)[(i) + 2] << 16) \ - | ((unsigned long)(b)[(i) + 3] << 24); \ -} -#define PUT_ULONG_LE(n, b, i) \ -{ \ - (b)[i] = (unsigned char)(n); \ - (b)[(i) + 1] = (unsigned char)((n) >> 8); \ - (b)[(i) + 2] = (unsigned char)((n) >> 16); \ - (b)[(i) + 3] = (unsigned char)((n) >> 24); \ -} static const unsigned char md5_padding[64] = { 0x80 @@ -36,23 +62,10 @@ static void md5_process(unsigned long *state, const unsigned char *data) { unsigned long X[16], A, B, C, D; + int n; - GET_ULONG_LE(X[0], data, 0); - GET_ULONG_LE(X[1], data, 4); - GET_ULONG_LE(X[2], data, 8); - GET_ULONG_LE(X[3], data, 12); - GET_ULONG_LE(X[4], data, 16); - GET_ULONG_LE(X[5], data, 20); - GET_ULONG_LE(X[6], data, 24); - GET_ULONG_LE(X[7], data, 28); - GET_ULONG_LE(X[8], data, 32); - GET_ULONG_LE(X[9], data, 36); - GET_ULONG_LE(X[10], data, 40); - GET_ULONG_LE(X[11], data, 44); - GET_ULONG_LE(X[12], data, 48); - GET_ULONG_LE(X[13], data, 52); - GET_ULONG_LE(X[14], data, 56); - GET_ULONG_LE(X[15], data, 60); + for (n = 0; n < 16; n++) + X[n] = htole32(*(unsigned long *)(&data[n << 2])); #define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) @@ -198,6 +211,7 @@ libwebsockets_md5(const unsigned char *input, int ilen, unsigned char *output) unsigned char msglen[8]; unsigned long state[6]; unsigned char buffer[64]; + unsigned long *p = (unsigned long *)&msglen[0]; memcpy(&state[0], &state_init[0], sizeof(state_init)); @@ -206,8 +220,8 @@ libwebsockets_md5(const unsigned char *input, int ilen, unsigned char *output) high = (state[0] >> 29) | (state[1] << 3); low = state[0] << 3; - PUT_ULONG_LE(low, msglen, 0); - PUT_ULONG_LE(high, msglen, 4); + *p++ = le32toh(low); + *p = le32toh(high); last = state[0] & 0x3F; padn = (last < 56) ? (56 - last) : (120 - last); @@ -215,10 +229,11 @@ libwebsockets_md5(const unsigned char *input, int ilen, unsigned char *output) md5_update(state, buffer, md5_padding, padn); md5_update(state, buffer, msglen, 8); - PUT_ULONG_LE(state[2], output, 0); - PUT_ULONG_LE(state[3], output, 4); - PUT_ULONG_LE(state[4], output, 8); - PUT_ULONG_LE(state[5], output, 12); + p = (unsigned long *)&output[0]; + *p++ = le32toh(state[2]); + *p++ = le32toh(state[3]); + *p++ = le32toh(state[4]); + *p++ = le32toh(state[5]); }