Skip to content

Commit

Permalink
* cosmetic. defines for the endian swap
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmnt committed Dec 1, 2021
1 parent 0f27c74 commit 16e2f34
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 15 deletions.
67 changes: 54 additions & 13 deletions eax128.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <string.h>
#include "eax128.h"

#define BIG_CTR 1
#define BIG_TAIL 1

static uint64_t byterev64(uint64_t a)
{
return (((a >> 0) & 0xff) << 56)
Expand All @@ -14,31 +17,69 @@ static uint64_t byterev64(uint64_t a)
| (((a >> 56) & 0xff) << 0);
}

static void gf_double_be(eax128_block_t *block)
static void gf_double(eax128_block_t *block)
{
uint64_t q1 = byterev64(block->q[0]);
uint64_t q0 = byterev64(block->q[1]);
uint64_t q1;
uint64_t q0;

if (BIG_TAIL)
{
q0 = byterev64(block->q[1]);
q1 = byterev64(block->q[0]);
}
else
{
q1 = block->q[1];
q0 = block->q[0];
}

uint32_t m = (q1 >> 63) * 0x87;
q1 = (q1 << 1) ^ (q0 >> 63);
q0 = (q0 << 1) ^ m;

block->q[0] = byterev64(q1);
block->q[1] = byterev64(q0);
if (BIG_TAIL)
{
block->q[0] = byterev64(q1);
block->q[1] = byterev64(q0);
}
else
{
block->q[0] = q0;
block->q[1] = q1;
}
}

static void add_be(eax128_block_t *out, const eax128_block_t *a, int inc)
static void add_ctr(eax128_block_t *out, const eax128_block_t *a, int inc)
{
uint64_t q1 = byterev64(a->q[0]);
uint64_t q0 = byterev64(a->q[1]);
uint64_t q1;
uint64_t q0;

if (BIG_CTR)
{
q0 = byterev64(a->q[1]);
q1 = byterev64(a->q[0]);
}
else
{
q1 = a->q[1];
q0 = a->q[0];
}

q0 += inc;

if (q0 < inc)
q1 += 1;

out->q[0] = byterev64(q1);
out->q[1] = byterev64(q0);
if (BIG_CTR)
{
out->q[0] = byterev64(q1);
out->q[1] = byterev64(q0);
}
else
{
out->q[0] = q0;
out->q[1] = q1;
}
}


Expand Down Expand Up @@ -75,12 +116,12 @@ eax128_block_t *eax128_omac_digest(eax128_omac_t *ctx)
{
eax128_block_t tail = {0};
eax128_cipher(tail.b, ctx->cipher_ctx);
gf_double_be(&tail);
gf_double(&tail);

if (ctx->bytepos != 0)
{
ctx->block.b[ctx->bytepos] = 0x80;
gf_double_be(&tail);
gf_double(&tail);
}

xor128(&ctx->block, &tail);
Expand Down Expand Up @@ -111,7 +152,7 @@ int eax128_ctr_process(eax128_ctr_t *ctx, int pos, int byte)
if (blocknum != ctx->blocknum) // change of block
{
ctx->blocknum = blocknum;
add_be(&ctx->xorbuf, &ctx->nonce, blocknum);
add_ctr(&ctx->xorbuf, &ctx->nonce, blocknum);
eax128_cipher(ctx->xorbuf.b, ctx->cipher_ctx);
}

Expand Down
38 changes: 36 additions & 2 deletions eax64.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,32 @@
#include <string.h>
#include "eax64.h"

#define BIG_CTR 0
#define BIG_TAIL 0

static uint64_t byterev64(uint64_t a)
{
return (((a >> 0) & 0xff) << 56)
| (((a >> 8) & 0xff) << 48)
| (((a >> 16) & 0xff) << 40)
| (((a >> 24) & 0xff) << 32)
| (((a >> 32) & 0xff) << 24)
| (((a >> 40) & 0xff) << 16)
| (((a >> 48) & 0xff) << 8)
| (((a >> 56) & 0xff) << 0);
}

static uint64_t gf_double(uint64_t a)
{
return (a << 1) ^ ((a >> 63) * 0x1B);
if (BIG_TAIL)
a = byterev64(a);

a = (a << 1) ^ ((a >> 63) * 0x1B);

if (BIG_TAIL)
a = byterev64(a);

return a;
}

void eax64_omac_init(eax64_omac_t *ctx, void *cipher_ctx, int k)
Expand Down Expand Up @@ -63,7 +86,18 @@ int eax64_ctr_process(eax64_ctr_t *ctx, int pos, int byte)
if (blocknum != ctx->blocknum) // change of block
{
ctx->blocknum = blocknum;
ctx->xorbuf.q = eax64_cipher(ctx->nonce + blocknum, ctx->cipher_ctx);

uint64_t a = ctx->nonce;

if (BIG_TAIL)
a = byterev64(a);

a += blocknum;

if (BIG_TAIL)
a = byterev64(a);

ctx->xorbuf.q = eax64_cipher(a, ctx->cipher_ctx);
}

return ctx->xorbuf.b[pos % 8] ^ byte;
Expand Down

0 comments on commit 16e2f34

Please sign in to comment.