#include <string.h>
#include "macros.h"
#include "memxor.h"
+#include "block-internal.h"
+/* provided_data is either NULL or a pointer to
+ DRBG_CTR_AES256_SEED_SIZE (= 48) bytes. */
static void
-drbg_ctr_aes256_update (struct aes256_ctx *Key,
- uint8_t *V, uint8_t *provided_data)
+drbg_ctr_aes256_update (struct aes256_ctx *key,
+ union nettle_block16 *V, const uint8_t *provided_data)
{
- uint8_t tmp[DRBG_CTR_AES256_SEED_SIZE];
+ union nettle_block16 tmp[3];
- INCREMENT (AES_BLOCK_SIZE, V);
- aes256_encrypt (Key, AES_BLOCK_SIZE, tmp, V);
+ INCREMENT (AES_BLOCK_SIZE, V->b);
+ aes256_encrypt (key, AES_BLOCK_SIZE, tmp[0].b, V->b);
- INCREMENT (AES_BLOCK_SIZE, V);
- aes256_encrypt (Key, AES_BLOCK_SIZE, tmp + AES_BLOCK_SIZE, V);
+ INCREMENT (AES_BLOCK_SIZE, V->b);
+ aes256_encrypt (key, AES_BLOCK_SIZE, tmp[1].b, V->b);
- INCREMENT (AES_BLOCK_SIZE, V);
- aes256_encrypt (Key, AES_BLOCK_SIZE, tmp + 2 * AES_BLOCK_SIZE, V);
+ INCREMENT (AES_BLOCK_SIZE, V->b);
+ aes256_encrypt (key, AES_BLOCK_SIZE, tmp[2].b, V->b);
if (provided_data)
- memxor (tmp, provided_data, 48);
+ memxor (tmp[0].b, provided_data, DRBG_CTR_AES256_SEED_SIZE);
- aes256_set_encrypt_key (Key, tmp);
-
- memcpy (V, tmp + AES256_KEY_SIZE, AES_BLOCK_SIZE);
+ aes256_set_encrypt_key (key, tmp[0].b);
+ block16_set (V, &tmp[2]);
}
void
drbg_ctr_aes256_init (struct drbg_ctr_aes256_ctx *ctx, uint8_t *seed_material)
{
- uint8_t Key[AES256_KEY_SIZE];
-
- memset (Key, 0, AES256_KEY_SIZE);
- aes256_set_encrypt_key (&ctx->Key, Key);
+ static const uint8_t zero_key[AES256_KEY_SIZE] = {0};
- memset (ctx->V, 0, AES_BLOCK_SIZE);
+ aes256_set_encrypt_key (&ctx->key, zero_key);
- drbg_ctr_aes256_update (&ctx->Key, ctx->V, seed_material);
+ block16_zero (&ctx->V);
+ drbg_ctr_aes256_update (&ctx->key, &ctx->V, seed_material);
}
void
{
while (n >= AES_BLOCK_SIZE)
{
- INCREMENT (AES_BLOCK_SIZE, ctx->V);
- aes256_encrypt (&ctx->Key, AES_BLOCK_SIZE, dst, ctx->V);
+ INCREMENT (AES_BLOCK_SIZE, ctx->V.b);
+ aes256_encrypt (&ctx->key, AES_BLOCK_SIZE, dst, ctx->V.b);
dst += AES_BLOCK_SIZE;
n -= AES_BLOCK_SIZE;
}
if (n > 0)
{
- uint8_t block[AES_BLOCK_SIZE];
+ union nettle_block16 block;
- INCREMENT (AES_BLOCK_SIZE, ctx->V);
- aes256_encrypt (&ctx->Key, AES_BLOCK_SIZE, block, ctx->V);
- memcpy (dst, block, n);
+ INCREMENT (AES_BLOCK_SIZE, ctx->V.b);
+ aes256_encrypt (&ctx->key, AES_BLOCK_SIZE, block.b, ctx->V.b);
+ memcpy (dst, block.b, n);
}
- drbg_ctr_aes256_update (&ctx->Key, ctx->V, NULL);
+ drbg_ctr_aes256_update (&ctx->key, &ctx->V, NULL);
}