diff options
Diffstat (limited to 'aes/src')
| -rw-r--r-- | aes/src/block.c | 9 | ||||
| -rw-r--r-- | aes/src/hex.c | 16 | ||||
| -rw-r--r-- | aes/src/key.c | 49 |
3 files changed, 22 insertions, 52 deletions
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; } |
