Skip to content

Commit

Permalink
* more cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmnt committed Dec 1, 2021
1 parent 425386f commit 0f27c74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
17 changes: 7 additions & 10 deletions aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
The storage may be implemented in some hardware registers (think TRESOR of linux-x86).
It's assumed the architecture is little-endian and CPU handles unaligned access just fine.
Yes, this AES is quite slow :-)
*/
Expand All @@ -26,16 +28,11 @@ static inline uint32_t rotr32(uint32_t x, int r)
return(x >> r) | (x << (32 - r));
}

static inline uint32_t rotl32(uint32_t x, int r)
{
return(x << r) | (x >> (32 - r));
}

typedef union
{
uint8_t mx[4][4];
uint32_t cols[4];
} state_t;
} aes128_state_t;


static const uint8_t sbox[256] =
Expand All @@ -62,7 +59,7 @@ static const uint8_t sbox[256] =
static const uint8_t rconrev[10] = { 0x36, 0x1b, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};


static void shift_and_subst(state_t* state)
static void shift_and_subst(aes128_state_t *state)
{
int tmp;

Expand Down Expand Up @@ -99,7 +96,7 @@ static uint32_t xtime32(uint32_t x)
return ((x << 1) & 0xFEFEFEFE) ^ (((x >> 7) & 0x01010101) * 0x1B);
}

static void mix_columns(state_t* state)
static void mix_columns(aes128_state_t *state)
{
for (int i = 0; i < 4; ++i)
{
Expand All @@ -109,7 +106,7 @@ static void mix_columns(state_t* state)
}


static void schedule_and_add_key(state_t *state, void *kmctx, int round)
static void schedule_and_add_key(aes128_state_t *state, void *kmctx, int round)
{
uint32_t k3 = aes128_load_km(kmctx, 3);

Expand Down Expand Up @@ -149,7 +146,7 @@ void aes128_set_key(void *kmctx, const uint8_t key[16])

void aes128_encrypt_ecb(void *kmctx, uint8_t buf[16])
{
state_t *state = (state_t*)buf;
aes128_state_t *state = (aes128_state_t *)buf;

// copy key
uint32_t tmp;
Expand Down
6 changes: 3 additions & 3 deletions aes.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef _AES_H_
#define _AES_H_

#define AES128_KMCTX_NWORDS 8
#define AES128_KMSTORE_NWORDS 8

extern void aes128_save_km(void *kmctx, int i, uint32_t w);
extern uint32_t aes128_load_km(void *kmctx, int i);
extern void aes128_save_km(void *kmstore, int i, uint32_t w);
extern uint32_t aes128_load_km(void *kmstore, int i);

void aes128_set_key(void *kmctx, const uint8_t key[16]);
void aes128_encrypt_ecb(void *kmctx, uint8_t buf[16]);
Expand Down
16 changes: 8 additions & 8 deletions eax_aes_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@

typedef struct
{
uint32_t words[AES128_KMCTX_NWORDS];
} aes_ctx_t;
uint32_t words[AES128_KMSTORE_NWORDS];
} aes_kmstore_t;

static aes_ctx_t aes_ctx;
static aes_kmstore_t aes_kmstore;

void aes128_save_km(void *ctx, int i, uint32_t w)
{
aes_ctx_t *store = ctx;
aes_kmstore_t *store = ctx;
store->words[i] = w;
}

uint32_t aes128_load_km(void *ctx, int i)
{
const aes_ctx_t *store = ctx;
const aes_kmstore_t *store = ctx;
return store->words[i];
}

void aes_install_key(const uint8_t *key)
{
aes128_set_key(&aes_ctx, key);
aes128_set_key(&aes_kmstore, key);
}


Expand Down Expand Up @@ -57,7 +57,7 @@ void print_dump(const void *data, int len)

extern void eax128_cipher(uint8_t block[16], void *ctx)
{
aes128_encrypt_ecb(&aes_ctx, block);
aes128_encrypt_ecb(&aes_kmstore, block);
}

static void test_vector(const testvector_t *v)
Expand Down Expand Up @@ -116,7 +116,7 @@ static void test_ctr_ovf(void)
eax128_ctr_t ctr;

aes_install_key(key);
eax128_ctr_init(&ctr, &aes_ctx, nonce);
eax128_ctr_init(&ctr, &aes_kmstore, nonce);

for (int i = 0; i < sizeof(pt); i++)
{
Expand Down

0 comments on commit 0f27c74

Please sign in to comment.