Use our own endianness conversion macros.

This commit is contained in:
Dag-Erling Smørgrav 2015-10-04 07:07:53 +00:00 committed by des
parent 03bd9ea902
commit 476c9f27f7
2 changed files with 37 additions and 71 deletions

View file

@ -38,29 +38,6 @@
#include <cryb/aes.h> #include <cryb/aes.h>
/*
* 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 * Forward S-box
*/ */
@ -344,7 +321,7 @@ aes_setkey_enc(aes_ctx *ctx, const uint8_t *key, int keysize)
} }
ctx->rk = RK = ctx->buf; ctx->rk = RK = ctx->buf;
for (i = 0; i < (keysize >> 2); i++) for (i = 0; i < (keysize >> 2); i++)
GET_ULONG_LE(RK[i], key, i << 2); RK[i] = le32dec(key + (i << 2));
switch (ctx->nr) { switch (ctx->nr) {
case 10: case 10:
for (i = 0; i < 10; i++, RK += 4) { 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; uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
RK = ctx->rk; RK = ctx->rk;
GET_ULONG_LE(X0, input, 0); X0 = le32dec(input + 0);
X0 ^= *RK++; X0 ^= *RK++;
GET_ULONG_LE(X1, input, 4); X1 = le32dec(input + 4);
X1 ^= *RK++; X1 ^= *RK++;
GET_ULONG_LE(X2, input, 8); X2 = le32dec(input + 8);
X2 ^= *RK++; X2 ^= *RK++;
GET_ULONG_LE(X3, input, 12); X3 = le32dec(input + 12);
X3 ^= *RK++; X3 ^= *RK++;
for (i = (ctx->nr >> 1) - 1; i > 0; i--) { for (i = (ctx->nr >> 1) - 1; i > 0; i--) {
AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3); 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[(Y0 >> 8) & 0xFF] << 8) ^
(FSb[(Y1 >> 16) & 0xFF] << 16) ^ (FSb[(Y1 >> 16) & 0xFF] << 16) ^
(FSb[(Y2 >> 24) & 0xFF] << 24); (FSb[(Y2 >> 24) & 0xFF] << 24);
PUT_ULONG_LE(X0, output, 0); le32enc(output + 0, X0);
PUT_ULONG_LE(X1, output, 4); le32enc(output + 4, X1);
PUT_ULONG_LE(X2, output, 8); le32enc(output + 8, X2);
PUT_ULONG_LE(X3, output, 12); 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; uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
RK = ctx->rk; RK = ctx->rk;
GET_ULONG_LE(X0, input, 0); X0 = le32dec(input + 0);
X0 ^= *RK++; X0 ^= *RK++;
GET_ULONG_LE(X1, input, 4); X1 = le32dec(input + 4);
X1 ^= *RK++; X1 ^= *RK++;
GET_ULONG_LE(X2, input, 8); X2 = le32dec(input + 8);
X2 ^= *RK++; X2 ^= *RK++;
GET_ULONG_LE(X3, input, 12); X3 = le32dec(input + 12);
X3 ^= *RK++; X3 ^= *RK++;
for (i = (ctx->nr >> 1) - 1; i > 0; i--) { for (i = (ctx->nr >> 1) - 1; i > 0; i--) {
AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3); 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[(Y2 >> 8) & 0xFF] << 8) ^
(RSb[(Y1 >> 16) & 0xFF] << 16) ^ (RSb[(Y1 >> 16) & 0xFF] << 16) ^
(RSb[(Y0 >> 24) & 0xFF] << 24); (RSb[(Y0 >> 24) & 0xFF] << 24);
PUT_ULONG_LE(X0, output, 0); le32enc(output + 0, X0);
PUT_ULONG_LE(X1, output, 4); le32enc(output + 4, X1);
PUT_ULONG_LE(X2, output, 8); le32enc(output + 8, X2);
PUT_ULONG_LE(X3, output, 12); le32enc(output + 12, X3);
} }
void void

View file

@ -41,17 +41,6 @@
#include <cryb/endian.h> #include <cryb/endian.h>
#include <cryb/md4.h> #include <cryb/md4.h>
/*
* 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 * 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; uint32_t X[16], A, B, C, D;
GET_ULONG_LE( X[ 0], data, 0 ); X[ 0] = le32dec(data + 0);
GET_ULONG_LE( X[ 1], data, 4 ); X[ 1] = le32dec(data + 4);
GET_ULONG_LE( X[ 2], data, 8 ); X[ 2] = le32dec(data + 8);
GET_ULONG_LE( X[ 3], data, 12 ); X[ 3] = le32dec(data + 12);
GET_ULONG_LE( X[ 4], data, 16 ); X[ 4] = le32dec(data + 16);
GET_ULONG_LE( X[ 5], data, 20 ); X[ 5] = le32dec(data + 20);
GET_ULONG_LE( X[ 6], data, 24 ); X[ 6] = le32dec(data + 24);
GET_ULONG_LE( X[ 7], data, 28 ); X[ 7] = le32dec(data + 28);
GET_ULONG_LE( X[ 8], data, 32 ); X[ 8] = le32dec(data + 32);
GET_ULONG_LE( X[ 9], data, 36 ); X[ 9] = le32dec(data + 36);
GET_ULONG_LE( X[10], data, 40 ); X[10] = le32dec(data + 40);
GET_ULONG_LE( X[11], data, 44 ); X[11] = le32dec(data + 44);
GET_ULONG_LE( X[12], data, 48 ); X[12] = le32dec(data + 48);
GET_ULONG_LE( X[13], data, 52 ); X[13] = le32dec(data + 52);
GET_ULONG_LE( X[14], data, 56 ); X[14] = le32dec(data + 56);
GET_ULONG_LE( X[15], data, 60 ); X[15] = le32dec(data + 60);
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) #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, md4_padding, padn );
md4_update( ctx, msglen, 8 ); md4_update( ctx, msglen, 8 );
PUT_ULONG_LE( ctx->state[0], output, 0 ); le32enc(output + 0, ctx->state[0]);
PUT_ULONG_LE( ctx->state[1], output, 4 ); le32enc(output + 4, ctx->state[1]);
PUT_ULONG_LE( ctx->state[2], output, 8 ); le32enc(output + 8, ctx->state[2]);
PUT_ULONG_LE( ctx->state[3], output, 12 ); le32enc(output + 12, ctx->state[3]);
} }
/* /*