Skip to content

Commit

Permalink
Double aesrand performance by using all bytes from each block (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
droe committed Mar 2, 2024
1 parent 76b1123 commit 612c805
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/aesrand.c
Expand Up @@ -8,6 +8,7 @@

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>

Expand All @@ -28,6 +29,7 @@ struct aesrand {
uint32_t input[AES_BLOCK_WORDS];
uint32_t sched[(AES_ROUNDS + 1) * 4];
uint8_t output[OUTPUT_BYTES];
bool remaining;
};

static aesrand_t *_aesrand_init(uint8_t *key)
Expand All @@ -38,6 +40,7 @@ static aesrand_t *_aesrand_init(uint8_t *key)
log_fatal("aesrand", "could not initialize AES key");
}
memset(aes->output, 0, OUTPUT_BYTES);
aes->remaining = false;
return aes;
}

Expand All @@ -62,10 +65,18 @@ aesrand_t *aesrand_init_from_random(void)

uint64_t aesrand_getword(aesrand_t *aes)
{
uint64_t retval;

if (aes->remaining) {
memcpy(&retval, &aes->output[sizeof(retval)], sizeof(retval));
aes->remaining = false;
return retval;
}

memcpy(aes->input, aes->output, sizeof(aes->input));
rijndaelEncrypt(aes->sched, AES_ROUNDS, (uint8_t *)aes->input,
aes->output);
uint64_t retval;
memcpy(&retval, aes->output, sizeof(retval));
aes->remaining = true;
return retval;
}

0 comments on commit 612c805

Please sign in to comment.