[U-Boot] [PATCH V2 1/4] aes: add a define for the size of a block
Philippe Reynes
philippe.reynes at softathome.com
Tue Oct 29 17:29:30 UTC 2019
In the code, we use the size of the key for the
size of the block. It's true when the key is 128 bits,
but it become false for key of 192 bits and 256 bits.
So to prepare the support of aes192 and 256,
we introduce a constant for the iaes block size.
Signed-off-by: Philippe Reynes <philippe.reynes at softathome.com>
---
cmd/aes.c | 2 +-
include/uboot_aes.h | 5 +++--
lib/aes.c | 34 +++++++++++++++++-----------------
3 files changed, 21 insertions(+), 20 deletions(-)
Changelog:
v2:
- no change
diff --git a/cmd/aes.c b/cmd/aes.c
index 8c61cee..24b0256 100644
--- a/cmd/aes.c
+++ b/cmd/aes.c
@@ -56,7 +56,7 @@ static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
aes_expand_key(key_ptr, key_exp);
/* Calculate the number of AES blocks to encrypt. */
- aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
+ aes_blocks = DIV_ROUND_UP(len, AES_BLOCK_LENGTH);
if (enc)
aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
diff --git a/include/uboot_aes.h b/include/uboot_aes.h
index 2fda384..1ae3ac9 100644
--- a/include/uboot_aes.h
+++ b/include/uboot_aes.h
@@ -18,7 +18,7 @@ typedef unsigned int u32;
* AES encryption library, with small code size, supporting only 128-bit AES
*
* AES is a stream cipher which works a block at a time, with each block
- * in this case being AES_KEY_LENGTH bytes.
+ * in this case being AES_BLOCK_LENGTH bytes.
*/
enum {
@@ -28,6 +28,7 @@ enum {
AES_KEY_LENGTH = 128 / 8,
AES_EXPAND_KEY_LENGTH = 4 * AES_STATECOLS * (AES_ROUNDS + 1),
+ AES_BLOCK_LENGTH = 128 / 8,
};
/**
@@ -62,7 +63,7 @@ void aes_decrypt(u8 *in, u8 *expkey, u8 *out);
/**
* Apply chain data to the destination using EOR
*
- * Each array is of length AES_KEY_LENGTH.
+ * Each array is of length AES_BLOCK_LENGTH.
*
* @cbc_chain_data Chain data
* @src Source data
diff --git a/lib/aes.c b/lib/aes.c
index a12a192..cfa57b6 100644
--- a/lib/aes.c
+++ b/lib/aes.c
@@ -596,62 +596,62 @@ void aes_apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst)
{
int i;
- for (i = 0; i < AES_KEY_LENGTH; i++)
+ for (i = 0; i < AES_BLOCK_LENGTH; i++)
*dst++ = *src++ ^ *cbc_chain_data++;
}
void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
u32 num_aes_blocks)
{
- u8 tmp_data[AES_KEY_LENGTH];
+ u8 tmp_data[AES_BLOCK_LENGTH];
u8 *cbc_chain_data = iv;
u32 i;
for (i = 0; i < num_aes_blocks; i++) {
debug("encrypt_object: block %d of %d\n", i, num_aes_blocks);
- debug_print_vector("AES Src", AES_KEY_LENGTH, src);
+ debug_print_vector("AES Src", AES_BLOCK_LENGTH, src);
/* Apply the chain data */
aes_apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
- debug_print_vector("AES Xor", AES_KEY_LENGTH, tmp_data);
+ debug_print_vector("AES Xor", AES_BLOCK_LENGTH, tmp_data);
/* Encrypt the AES block */
aes_encrypt(tmp_data, key_exp, dst);
- debug_print_vector("AES Dst", AES_KEY_LENGTH, dst);
+ debug_print_vector("AES Dst", AES_BLOCK_LENGTH, dst);
/* Update pointers for next loop. */
cbc_chain_data = dst;
- src += AES_KEY_LENGTH;
- dst += AES_KEY_LENGTH;
+ src += AES_BLOCK_LENGTH;
+ dst += AES_BLOCK_LENGTH;
}
}
void aes_cbc_decrypt_blocks(u8 *key_exp, u8 *iv, u8 *src, u8 *dst,
u32 num_aes_blocks)
{
- u8 tmp_data[AES_KEY_LENGTH], tmp_block[AES_KEY_LENGTH];
+ u8 tmp_data[AES_BLOCK_LENGTH], tmp_block[AES_BLOCK_LENGTH];
/* Convenient array of 0's for IV */
- u8 cbc_chain_data[AES_KEY_LENGTH];
+ u8 cbc_chain_data[AES_BLOCK_LENGTH];
u32 i;
- memcpy(cbc_chain_data, iv, AES_KEY_LENGTH);
+ memcpy(cbc_chain_data, iv, AES_BLOCK_LENGTH);
for (i = 0; i < num_aes_blocks; i++) {
debug("encrypt_object: block %d of %d\n", i, num_aes_blocks);
- debug_print_vector("AES Src", AES_KEY_LENGTH, src);
+ debug_print_vector("AES Src", AES_BLOCK_LENGTH, src);
- memcpy(tmp_block, src, AES_KEY_LENGTH);
+ memcpy(tmp_block, src, AES_BLOCK_LENGTH);
/* Decrypt the AES block */
aes_decrypt(src, key_exp, tmp_data);
- debug_print_vector("AES Xor", AES_KEY_LENGTH, tmp_data);
+ debug_print_vector("AES Xor", AES_BLOCK_LENGTH, tmp_data);
/* Apply the chain data */
aes_apply_cbc_chain_data(cbc_chain_data, tmp_data, dst);
- debug_print_vector("AES Dst", AES_KEY_LENGTH, dst);
+ debug_print_vector("AES Dst", AES_BLOCK_LENGTH, dst);
/* Update pointers for next loop. */
- memcpy(cbc_chain_data, tmp_block, AES_KEY_LENGTH);
- src += AES_KEY_LENGTH;
- dst += AES_KEY_LENGTH;
+ memcpy(cbc_chain_data, tmp_block, AES_BLOCK_LENGTH);
+ src += AES_BLOCK_LENGTH;
+ dst += AES_BLOCK_LENGTH;
}
}
--
2.7.4
More information about the U-Boot
mailing list