diff options
| author | Egor Tensin <egor@tensin.name> | 2026-07-23 11:04:28 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-07-23 11:05:30 +0200 |
| commit | 127077c93bfee72470ba80b8b48e0a516129e9d4 (patch) | |
| tree | c81474ef57da4954b4843f817d488ecc3c533405 /aes/src/key.c | |
| parent | aes: always require full inputs (diff) | |
| download | aes-tools-127077c93bfee72470ba80b8b48e0a516129e9d4.tar.gz aes-tools-127077c93bfee72470ba80b8b48e0a516129e9d4.zip | |
aes: factor out block formatting code
Diffstat (limited to 'aes/src/key.c')
| -rw-r--r-- | aes/src/key.c | 49 |
1 files changed, 5 insertions, 44 deletions
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; } |
