aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2026-07-23 11:04:28 +0200
committerEgor Tensin <egor@tensin.name>2026-07-23 11:05:30 +0200
commit127077c93bfee72470ba80b8b48e0a516129e9d4 (patch)
treec81474ef57da4954b4843f817d488ecc3c533405
parentaes: always require full inputs (diff)
downloadaes-tools-127077c93bfee72470ba80b8b48e0a516129e9d4.tar.gz
aes-tools-127077c93bfee72470ba80b8b48e0a516129e9d4.zip
aes: factor out block formatting code
-rw-r--r--aes/include/aes/hex.h4
-rw-r--r--aes/src/block.c9
-rw-r--r--aes/src/hex.c16
-rw-r--r--aes/src/key.c49
4 files changed, 26 insertions, 52 deletions
diff --git a/aes/include/aes/hex.h b/aes/include/aes/hex.h
index d0e4586..8adb9e4 100644
--- a/aes/include/aes/hex.h
+++ b/aes/include/aes/hex.h
@@ -5,6 +5,7 @@
#pragma once
+#include "block.h"
#include "error.h"
#include <stdlib.h>
@@ -20,6 +21,9 @@ AES_StatusCode aes_parse_hex_string(
AES_ErrorDetails* err_details
);
+char* aes_format_block_hex(char* dest, AES_Block);
+char* aes_format_block_hex_partial(char* dest, AES_Block, size_t numof_bytes);
+
#ifdef __cplusplus
}
#endif
diff --git a/aes/src/block.c b/aes/src/block.c
index 1b72b62..6183f87 100644
--- a/aes/src/block.c
+++ b/aes/src/block.c
@@ -34,14 +34,7 @@ AES_StatusCode aes_format_block(
if (block == NULL)
return aes_error_null_argument(err_details, "block");
- char* cursor = str->str;
-
- AES_ALIGN(unsigned char, 16) bytes[16];
- aes_store_block_aligned(bytes, *block);
-
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", bytes[i]);
-
+ char* cursor = aes_format_block_hex(str->str, *block);
*cursor = '\0';
return AES_SUCCESS;
}
diff --git a/aes/src/hex.c b/aes/src/hex.c
index 62b6e5a..7a695b4 100644
--- a/aes/src/hex.c
+++ b/aes/src/hex.c
@@ -40,3 +40,19 @@ AES_StatusCode aes_parse_hex_string(
return AES_SUCCESS;
}
+
+char* aes_format_block_hex_partial(char* dest, AES_Block block, size_t numof_bytes) {
+ char* cursor = dest;
+
+ AES_ALIGN(unsigned char, 16) bytes[16];
+ aes_store_block_aligned(bytes, block);
+
+ for (size_t i = 0; i < numof_bytes; ++i, cursor += 2)
+ sprintf(cursor, "%02x", bytes[i]);
+
+ return cursor;
+}
+
+char* aes_format_block_hex(char* dest, AES_Block block) {
+ return aes_format_block_hex_partial(dest, block, 16);
+}
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;
}