Index: md5.c =================================================================== RCS file: /cvs/src/sys/crypto/md5.c,v retrieving revision 1.2 diff -u -p -r1.2 md5.c --- md5.c 11 Jan 2011 15:42:05 -0000 1.2 +++ md5.c 21 Oct 2014 01:49:39 -0000 @@ -21,22 +21,6 @@ #include #include -#define PUT_64BIT_LE(cp, value) do { \ - (cp)[7] = (value) >> 56; \ - (cp)[6] = (value) >> 48; \ - (cp)[5] = (value) >> 40; \ - (cp)[4] = (value) >> 32; \ - (cp)[3] = (value) >> 24; \ - (cp)[2] = (value) >> 16; \ - (cp)[1] = (value) >> 8; \ - (cp)[0] = (value); } while (0) - -#define PUT_32BIT_LE(cp, value) do { \ - (cp)[3] = (value) >> 24; \ - (cp)[2] = (value) >> 16; \ - (cp)[1] = (value) >> 8; \ - (cp)[0] = (value); } while (0) - static u_int8_t PADDING[MD5_BLOCK_LENGTH] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -102,12 +86,13 @@ MD5Update(MD5_CTX *ctx, const unsigned c void MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx) { - u_int8_t count[8]; + u_int32_t *w = (u_int32_t *)digest; + u_int64_t count; size_t padlen; int i; /* Convert count to 8 bytes in little endian order. */ - PUT_64BIT_LE(count, ctx->count); + count = htole64(ctx->count); /* Pad out to 56 mod 64. */ padlen = MD5_BLOCK_LENGTH - @@ -115,11 +100,11 @@ MD5Final(unsigned char digest[MD5_DIGEST if (padlen < 1 + 8) padlen += MD5_BLOCK_LENGTH; MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ - MD5Update(ctx, count, 8); + MD5Update(ctx, (unsigned char *)&count, 8); if (digest != NULL) { for (i = 0; i < 4; i++) - PUT_32BIT_LE(digest + i * 4, ctx->state[i]); + htolem32(&w[i], ctx->state[i]); } explicit_bzero(ctx, sizeof(*ctx)); /* in case it's sensitive */ } @@ -145,19 +130,11 @@ MD5Final(unsigned char digest[MD5_DIGEST void MD5Transform(u_int32_t state[4], const u_int8_t block[MD5_BLOCK_LENGTH]) { + const u_int32_t *w = (u_int32_t *)block; u_int32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4]; -#if BYTE_ORDER == LITTLE_ENDIAN - bcopy(block, in, sizeof(in)); -#else - for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) { - in[a] = (u_int32_t)( - (u_int32_t)(block[a * 4 + 0]) | - (u_int32_t)(block[a * 4 + 1]) << 8 | - (u_int32_t)(block[a * 4 + 2]) << 16 | - (u_int32_t)(block[a * 4 + 3]) << 24); - } -#endif + for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) + in[a] = lemtoh32(&w[a]); a = state[0]; b = state[1]; Index: sha2.c =================================================================== RCS file: /cvs/src/sys/crypto/sha2.c,v retrieving revision 1.8 diff -u -p -r1.8 sha2.c --- sha2.c 11 Jan 2011 15:42:05 -0000 1.8 +++ sha2.c 21 Oct 2014 01:49:39 -0000 @@ -39,6 +39,13 @@ #include #include +#if 0 +#include +#include +#include +#include +#endif + /* * UNROLLED TRANSFORM LOOP NOTE: * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform @@ -54,63 +61,12 @@ */ -/*** SHA-256/384/512 Machine Architecture Definitions *****************/ -/* - * BYTE_ORDER NOTE: - * - * Please make sure that your system defines BYTE_ORDER. If your - * architecture is little-endian, make sure it also defines - * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are - * equivilent. - * - * If your system does not define the above, then you can do so by - * hand like this: - * - * #define LITTLE_ENDIAN 1234 - * #define BIG_ENDIAN 4321 - * - * And for little-endian machines, add: - * - * #define BYTE_ORDER LITTLE_ENDIAN - * - * Or for big-endian machines: - * - * #define BYTE_ORDER BIG_ENDIAN - * - * The FreeBSD machine this was written on defines BYTE_ORDER - * appropriately by including (which in turn includes - * where the appropriate definitions are actually - * made). - */ -#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) -#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN -#endif - - /*** SHA-256/384/512 Various Length Definitions ***********************/ /* NOTE: Most of these are in sha2.h */ #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16) #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) - -/*** ENDIAN REVERSAL MACROS *******************************************/ -#if BYTE_ORDER == LITTLE_ENDIAN -#define REVERSE32(w,x) { \ - u_int32_t tmp = (w); \ - tmp = (tmp >> 16) | (tmp << 16); \ - (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \ -} -#define REVERSE64(w,x) { \ - u_int64_t tmp = (w); \ - tmp = (tmp >> 32) | (tmp << 32); \ - tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | \ - ((tmp & 0x00ff00ff00ff00ffULL) << 8); \ - (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | \ - ((tmp & 0x0000ffff0000ffffULL) << 16); \ -} -#endif /* BYTE_ORDER == LITTLE_ENDIAN */ - /* * Macro for incrementally adding the unsigned 64-bit integer n to the * unsigned 128-bit integer (represented using a two-element array of @@ -494,14 +450,11 @@ SHA256Final(u_int8_t digest[], SHA2_CTX { u_int32_t *d = (u_int32_t *)digest; unsigned int usedspace; + int j; /* If no digest buffer is passed, we don't bother doing this: */ if (digest != NULL) { usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH; -#if BYTE_ORDER == LITTLE_ENDIAN - /* Convert FROM host byte order */ - REVERSE64(context->bitcount[0], context->bitcount[0]); -#endif if (usedspace > 0) { /* Begin padding with a 1 bit: */ context->buffer[usedspace++] = 0x80; @@ -527,24 +480,15 @@ SHA256Final(u_int8_t digest[], SHA2_CTX *context->buffer = 0x80; } /* Set the bit count: */ - *(u_int64_t *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount[0]; + htobem64( + (u_int64_t *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH], + context->bitcount[0]); /* Final transform: */ SHA256Transform(context, context->buffer); -#if BYTE_ORDER == LITTLE_ENDIAN - { - /* Convert TO host byte order */ - int j; - for (j = 0; j < 8; j++) { - REVERSE32(context->state.st32[j], - context->state.st32[j]); - *d++ = context->state.st32[j]; - } - } -#else - bcopy(context->state.st32, d, SHA256_DIGEST_LENGTH); -#endif + for (j = 0; j < 8; j++) + d[j] = bemtoh32(&context->state.st32[j]); } /* Clean up state data: */ @@ -781,11 +725,6 @@ SHA512Last(SHA2_CTX *context) unsigned int usedspace; usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; -#if BYTE_ORDER == LITTLE_ENDIAN - /* Convert FROM host byte order */ - REVERSE64(context->bitcount[0],context->bitcount[0]); - REVERSE64(context->bitcount[1],context->bitcount[1]); -#endif if (usedspace > 0) { /* Begin padding with a 1 bit: */ context->buffer[usedspace++] = 0x80; @@ -811,8 +750,10 @@ SHA512Last(SHA2_CTX *context) *context->buffer = 0x80; } /* Store the length of input data (in bits): */ - *(u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1]; - *(u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0]; + htobem64((u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH], + context->bitcount[1]); + htobem64((u_int64_t *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8], + context->bitcount[0]); /* Final transform: */ SHA512Transform(context, context->buffer); @@ -822,25 +763,15 @@ void SHA512Final(u_int8_t digest[], SHA2_CTX *context) { u_int64_t *d = (u_int64_t *)digest; + int j; /* If no digest buffer is passed, we don't bother doing this: */ if (digest != NULL) { SHA512Last(context); /* Save the hash data for output: */ -#if BYTE_ORDER == LITTLE_ENDIAN - { - /* Convert TO host byte order */ - int j; - for (j = 0; j < 8; j++) { - REVERSE64(context->state.st64[j], - context->state.st64[j]); - *d++ = context->state.st64[j]; - } - } -#else - bcopy(context->state.st64, d, SHA512_DIGEST_LENGTH); -#endif + for (j = 0; j < 8; j++) + d[j] = bemtoh64(&context->state.st64[j]); } /* Zero out state data */ @@ -870,25 +801,15 @@ void SHA384Final(u_int8_t digest[], SHA2_CTX *context) { u_int64_t *d = (u_int64_t *)digest; + int j; /* If no digest buffer is passed, we don't bother doing this: */ if (digest != NULL) { SHA512Last((SHA2_CTX *)context); /* Save the hash data for output: */ -#if BYTE_ORDER == LITTLE_ENDIAN - { - /* Convert TO host byte order */ - int j; - for (j = 0; j < 6; j++) { - REVERSE64(context->state.st64[j], - context->state.st64[j]); - *d++ = context->state.st64[j]; - } - } -#else - bcopy(context->state.st64, d, SHA384_DIGEST_LENGTH); -#endif + for (j = 0; j < 6; j++) + d[j] = bemtoh64(&context->state.st64[j]); } /* Zero out state data */