From 127077c93bfee72470ba80b8b48e0a516129e9d4 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 23 Jul 2026 11:04:28 +0200 Subject: aes: factor out block formatting code --- aes/src/key.c | 49 +++++-------------------------------------------- 1 file changed, 5 insertions(+), 44 deletions(-) (limited to 'aes/src/key.c') diff --git a/aes/src/key.c b/aes/src/key.c index 1251a8c..b41686d 100644 --- a/aes/src/key.c +++ b/aes/src/key.c @@ -21,14 +21,7 @@ AES_StatusCode aes128_format_key( if (key == NULL) return aes_error_null_argument(err_details, "key"); - char* cursor = str->str; - - AES_ALIGN(unsigned char, 16) bytes[16]; - aes_store_block_aligned(bytes, key->key); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - + char* cursor = aes_format_block_hex(str->str, key->key); *cursor = '\0'; return AES_SUCCESS; } @@ -43,24 +36,8 @@ AES_StatusCode aes192_format_key( if (key == NULL) return aes_error_null_argument(err_details, "key"); - char* cursor = str->str; - - { - AES_ALIGN(unsigned char, 16) bytes[16]; - aes_store_block_aligned(bytes, key->lo); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - - { - AES_ALIGN(unsigned char, 16) bytes[16]; - aes_store_block_aligned(bytes, key->hi); - - for (int i = 0; i < 8; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - + char* cursor = aes_format_block_hex(str->str, key->lo); + cursor = aes_format_block_hex_partial(cursor, key->hi, 8); *cursor = '\0'; return AES_SUCCESS; } @@ -75,24 +52,8 @@ AES_StatusCode aes256_format_key( if (key == NULL) return aes_error_null_argument(err_details, "key"); - char* cursor = str->str; - - { - AES_ALIGN(unsigned char, 16) bytes[16]; - aes_store_block_aligned(bytes, key->lo); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - - { - AES_ALIGN(unsigned char, 16) bytes[16]; - aes_store_block_aligned(bytes, key->hi); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - + char* cursor = aes_format_block_hex(str->str, key->lo); + cursor = aes_format_block_hex(cursor, key->hi); *cursor = '\0'; return AES_SUCCESS; } -- cgit v1.3