mirror of
git://git.gniibe.org/gnuk/gnuk.git
synced 2026-11-20 19:01:26 +00:00
Fix AES CTR mode.
There are multiple definitions of "counter mode". It was my mistake to just use PolarSSL API, which is big endian integer counter mode. Now, it's AES_CTR described in RFC 8452. Signed-off-by: NIIBE Yutaka <[email protected]>
This commit is contained in:
@@ -24,6 +24,11 @@
|
||||
* keysize fixed for AES-256.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* NOTE: This implementation is not safe against various kinds of
|
||||
* side-channel attacks. For example, when it uses cache memory or
|
||||
* flash access accelerator, observing table access may be possible.
|
||||
*/
|
||||
|
||||
/*
|
||||
* For AES-256:
|
||||
@@ -239,30 +244,26 @@ aes_clear_key (aes_context *ctx)
|
||||
* AES counter mode encryption
|
||||
*/
|
||||
void
|
||||
aes_crypt_ctr (const aes_context *ctx, int len,
|
||||
unsigned int *iv_off_p,
|
||||
uint8_t iv[16], uint8_t str_block[16],
|
||||
const uint8_t *input, uint8_t *output)
|
||||
aes_ctr (const aes_context *ctx, uint8_t ctr_blk[AES_BLOCK_SIZE],
|
||||
const uint8_t *input, unsigned int len, uint8_t *output)
|
||||
{
|
||||
unsigned int n = *iv_off_p;
|
||||
uint32_t counter;
|
||||
|
||||
while (len--)
|
||||
counter = get_uint32_le (ctr_blk, 0);
|
||||
|
||||
while (len)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
int i;
|
||||
uint8_t blk[AES_BLOCK_SIZE];
|
||||
int i, todo;
|
||||
|
||||
aes_encrypt (ctx, iv, str_block);
|
||||
todo = len < AES_BLOCK_SIZE ? len : AES_BLOCK_SIZE;
|
||||
|
||||
/* increment big endian counter. */
|
||||
for (i = 16; i > 0; i--)
|
||||
if (++iv[i - 1] != 0)
|
||||
break;
|
||||
}
|
||||
aes_encrypt (ctx, ctr_blk, blk);
|
||||
counter++;
|
||||
put_uint32_le (ctr_blk, 0, counter);
|
||||
|
||||
*output++ = (*input++ ^ str_block[n]);
|
||||
n = (n + 1) & 0x0F;
|
||||
for (i = 0; i < todo; i++)
|
||||
*output++ = (*input++ ^ blk[i]);
|
||||
len -= todo;
|
||||
}
|
||||
|
||||
*iv_off_p = n;
|
||||
}
|
||||
|
||||
@@ -57,15 +57,11 @@ void aes_clear_key (aes_context *ctx);
|
||||
* AES counter mode encryption
|
||||
*
|
||||
* @param ctx AES context
|
||||
* @param len length of input/output
|
||||
* @param iv_off_p offset in the current stream_block
|
||||
* @param iv iv and counter
|
||||
* @param st_block saved stream block
|
||||
* @param output output
|
||||
* @param ctr_blk counter block
|
||||
* @param input input
|
||||
* @param len length of input/output
|
||||
* @param output output
|
||||
*
|
||||
*/
|
||||
void aes_crypt_ctr (const aes_context *ctx, int len,
|
||||
unsigned int *iv_off_p,
|
||||
uint8_t iv[16], uint8_t str_block[16],
|
||||
const uint8_t *input, uint8_t *output);
|
||||
void aes_ctr (const aes_context *ctx, uint8_t ctr_blk[16],
|
||||
const uint8_t *input, unsigned int len, uint8_t *output);
|
||||
|
||||
+5
-8
@@ -1194,31 +1194,28 @@ compute_key_data_checksum (const uint8_t auth_key[DATA_ENCRYPTION_KEY_SIZE],
|
||||
|
||||
static void
|
||||
crypt0 (const uint8_t *key, uint8_t iv0[ENCRYPTION_BLOCK_SIZE],
|
||||
uint8_t *data, int len)
|
||||
uint8_t *data, unsigned int len)
|
||||
{
|
||||
aes_context aes;
|
||||
uint8_t stream_block[16];
|
||||
unsigned int iv_offset = 0;
|
||||
|
||||
aes_set_key (&aes, key);
|
||||
aes_crypt_ctr (&aes, len, &iv_offset, iv0, stream_block, data, data);
|
||||
aes_ctr (&aes, iv0, data, len, data);
|
||||
aes_clear_key (&aes);
|
||||
}
|
||||
|
||||
static void
|
||||
crypt1 (const uint8_t *key, const uint8_t *iv, uint8_t *data, int len)
|
||||
crypt1 (const uint8_t *key, const uint8_t *iv, uint8_t *data,
|
||||
unsigned int len)
|
||||
{
|
||||
aes_context aes;
|
||||
uint8_t iv0[ENCRYPTION_BLOCK_SIZE];
|
||||
uint8_t stream_block[16];
|
||||
unsigned int iv_offset = 0;
|
||||
|
||||
aes_set_key (&aes, key);
|
||||
memset (iv0, 0, ENCRYPTION_BLOCK_SIZE - INITIAL_VECTOR_SIZE);
|
||||
memcpy (iv0 + ENCRYPTION_BLOCK_SIZE - INITIAL_VECTOR_SIZE,
|
||||
iv, INITIAL_VECTOR_SIZE);
|
||||
memcpy (iv0, "\xd1\xff\xd1\xff", 4);
|
||||
aes_crypt_ctr (&aes, len, &iv_offset, iv0, stream_block, data, data);
|
||||
aes_ctr (&aes, iv0, data, len, data);
|
||||
aes_clear_key (&aes);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user