From 476c9f27f79cafabd85525a85c98a550b009a173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sun, 4 Oct 2015 07:07:53 +0000 Subject: [PATCH] Use our own endianness conversion macros. --- lib/cipher/aes.c | 57 +++++++++++++++--------------------------------- lib/digest/md4.c | 51 +++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 71 deletions(-) diff --git a/lib/cipher/aes.c b/lib/cipher/aes.c index 9922ab6..20163e8 100644 --- a/lib/cipher/aes.c +++ b/lib/cipher/aes.c @@ -38,29 +38,6 @@ #include -/* - * 32-bit integer manipulation macros (little endian) - */ -#ifndef GET_ULONG_LE -#define GET_ULONG_LE(n,b,i) \ - do { \ - (n) = ((uint32_t)(b)[(i) ] ) \ - | ((uint32_t)(b)[(i) + 1] << 8) \ - | ((uint32_t)(b)[(i) + 2] << 16) \ - | ((uint32_t)(b)[(i) + 3] << 24); \ - } while (0) -#endif - -#ifndef PUT_ULONG_LE -#define PUT_ULONG_LE(n,b,i) \ - do { \ - (b)[(i) ] = (uint8_t)((n) ); \ - (b)[(i) + 1] = (uint8_t)((n) >> 8); \ - (b)[(i) + 2] = (uint8_t)((n) >> 16); \ - (b)[(i) + 3] = (uint8_t)((n) >> 24); \ - } while (0) -#endif - /* * Forward S-box */ @@ -344,7 +321,7 @@ aes_setkey_enc(aes_ctx *ctx, const uint8_t *key, int keysize) } ctx->rk = RK = ctx->buf; for (i = 0; i < (keysize >> 2); i++) - GET_ULONG_LE(RK[i], key, i << 2); + RK[i] = le32dec(key + (i << 2)); switch (ctx->nr) { case 10: for (i = 0; i < 10; i++, RK += 4) { @@ -502,13 +479,13 @@ aes_enc(aes_ctx *ctx, const uint8_t *input, uint8_t *output) uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; RK = ctx->rk; - GET_ULONG_LE(X0, input, 0); + X0 = le32dec(input + 0); X0 ^= *RK++; - GET_ULONG_LE(X1, input, 4); + X1 = le32dec(input + 4); X1 ^= *RK++; - GET_ULONG_LE(X2, input, 8); + X2 = le32dec(input + 8); X2 ^= *RK++; - GET_ULONG_LE(X3, input, 12); + X3 = le32dec(input + 12); X3 ^= *RK++; for (i = (ctx->nr >> 1) - 1; i > 0; i--) { AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3); @@ -535,10 +512,10 @@ aes_enc(aes_ctx *ctx, const uint8_t *input, uint8_t *output) (FSb[(Y0 >> 8) & 0xFF] << 8) ^ (FSb[(Y1 >> 16) & 0xFF] << 16) ^ (FSb[(Y2 >> 24) & 0xFF] << 24); - PUT_ULONG_LE(X0, output, 0); - PUT_ULONG_LE(X1, output, 4); - PUT_ULONG_LE(X2, output, 8); - PUT_ULONG_LE(X3, output, 12); + le32enc(output + 0, X0); + le32enc(output + 4, X1); + le32enc(output + 8, X2); + le32enc(output + 12, X3); } /* @@ -551,13 +528,13 @@ aes_dec(aes_ctx *ctx, const uint8_t *input, uint8_t *output) uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; RK = ctx->rk; - GET_ULONG_LE(X0, input, 0); + X0 = le32dec(input + 0); X0 ^= *RK++; - GET_ULONG_LE(X1, input, 4); + X1 = le32dec(input + 4); X1 ^= *RK++; - GET_ULONG_LE(X2, input, 8); + X2 = le32dec(input + 8); X2 ^= *RK++; - GET_ULONG_LE(X3, input, 12); + X3 = le32dec(input + 12); X3 ^= *RK++; for (i = (ctx->nr >> 1) - 1; i > 0; i--) { AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3); @@ -584,10 +561,10 @@ aes_dec(aes_ctx *ctx, const uint8_t *input, uint8_t *output) (RSb[(Y2 >> 8) & 0xFF] << 8) ^ (RSb[(Y1 >> 16) & 0xFF] << 16) ^ (RSb[(Y0 >> 24) & 0xFF] << 24); - PUT_ULONG_LE(X0, output, 0); - PUT_ULONG_LE(X1, output, 4); - PUT_ULONG_LE(X2, output, 8); - PUT_ULONG_LE(X3, output, 12); + le32enc(output + 0, X0); + le32enc(output + 4, X1); + le32enc(output + 8, X2); + le32enc(output + 12, X3); } void diff --git a/lib/digest/md4.c b/lib/digest/md4.c index 755ed79..47e8ffd 100644 --- a/lib/digest/md4.c +++ b/lib/digest/md4.c @@ -41,17 +41,6 @@ #include #include -/* - * 32-bit integer manipulation macros (little endian) - */ -#undef GET_ULONG_LE -#define GET_ULONG_LE(n,b,i) \ - do { (n) = le32dec((const uint8_t *)(b) + (i)); } while (0) - -#undef PUT_ULONG_LE -#define PUT_ULONG_LE(n,b,i) \ - do { le32enc((uint8_t *)(b) + (i), (n)); } while (0) - /* * MD4 context setup */ @@ -69,22 +58,22 @@ static void md4_process( md4_ctx *ctx, const uint8_t *data ) { uint32_t X[16], A, B, C, D; - 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 ); + X[ 0] = le32dec(data + 0); + X[ 1] = le32dec(data + 4); + X[ 2] = le32dec(data + 8); + X[ 3] = le32dec(data + 12); + X[ 4] = le32dec(data + 16); + X[ 5] = le32dec(data + 20); + X[ 6] = le32dec(data + 24); + X[ 7] = le32dec(data + 28); + X[ 8] = le32dec(data + 32); + X[ 9] = le32dec(data + 36); + X[10] = le32dec(data + 40); + X[11] = le32dec(data + 44); + X[12] = le32dec(data + 48); + X[13] = le32dec(data + 52); + X[14] = le32dec(data + 56); + X[15] = le32dec(data + 60); #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) @@ -230,10 +219,10 @@ void md4_final( md4_ctx *ctx, uint8_t *output ) md4_update( ctx, md4_padding, padn ); md4_update( ctx, msglen, 8 ); - PUT_ULONG_LE( ctx->state[0], output, 0 ); - PUT_ULONG_LE( ctx->state[1], output, 4 ); - PUT_ULONG_LE( ctx->state[2], output, 8 ); - PUT_ULONG_LE( ctx->state[3], output, 12 ); + le32enc(output + 0, ctx->state[0]); + le32enc(output + 4, ctx->state[1]); + le32enc(output + 8, ctx->state[2]); + le32enc(output + 12, ctx->state[3]); } /*