From 44d58e0a10c7dfdb33899fa98c0eea790e0b90ce Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 17 Jun 2015 23:25:52 +0300 Subject: factoring out AES-specific stuff --- src/aes.c | 362 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/c/aes128.c | 18 +-- src/c/aes192.c | 20 +-- src/c/aes256.c | 26 ++-- src/data.c | 410 --------------------------------------------------------- 5 files changed, 394 insertions(+), 442 deletions(-) create mode 100644 src/aes.c delete mode 100644 src/data.c (limited to 'src') diff --git a/src/aes.c b/src/aes.c new file mode 100644 index 0000000..98cc9cd --- /dev/null +++ b/src/aes.c @@ -0,0 +1,362 @@ +/** + * \file + * \author Egor Tensin + * \date 2015 + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include + +#include +#include +#include + +AesNI_StatusCode aesni_aes_format_block( + AesNI_Aes_BlockString* str, + const AesNI_Aes_Block* block, + AesNI_ErrorDetails* err_details) +{ + assert(str); + assert(block); + + if (str == NULL) + return aesni_make_null_argument_error(err_details, "str"); + if (block == NULL) + return aesni_make_null_argument_error(err_details, "block"); + + char* cursor = str->str; + + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, *block); + + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + + *cursor = '\0'; + return AESNI_SUCCESS; +} + +AesNI_StatusCode aesni_aes_format_block_as_matrix( + AesNI_Aes_BlockMatrixString* str, + const AesNI_Aes_Block* block, + AesNI_ErrorDetails* err_details) +{ + assert(str); + assert(block); + + if (str == NULL) + return aesni_make_null_argument_error(err_details, "str"); + if (block == NULL) + return aesni_make_null_argument_error(err_details, "block"); + + char* cursor = str->str; + + __declspec(align(16)) unsigned char bytes[4][4]; + aesni_store_block128_aligned(bytes, *block); + + for (int i = 0; i < 4; ++i, cursor += 3) + { + for (int j = 0; j < 3; ++j, cursor += 3) + sprintf(cursor, "%02x ", bytes[j][i]); + sprintf(cursor, "%02x\n", bytes[3][i]); + } + + *cursor = '\0'; + return AESNI_SUCCESS; +} + +AesNI_StatusCode aesni_aes_print_block( + const AesNI_Aes_Block* block, + AesNI_ErrorDetails* err_details) +{ + assert(block); + + if (block == NULL) + return aesni_make_null_argument_error(err_details, "block"); + + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes_BlockString str; + + if (aesni_is_error(ec = aesni_aes_format_block(&str, block, err_details))) + return ec; + + printf("%s\n", str.str); + return ec; +} + +AesNI_StatusCode aesni_aes_print_block_as_matrix( + const AesNI_Aes_Block* block, + AesNI_ErrorDetails* err_details) +{ + assert(block); + + if (block == NULL) + return aesni_make_null_argument_error(err_details, "block"); + + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes_BlockMatrixString str; + + if (aesni_is_error(ec = aesni_aes_format_block_as_matrix(&str, block, err_details))) + return ec; + + printf("%s", str.str); + return ec; +} + +AesNI_StatusCode aesni_aes_parse_block( + AesNI_Aes_Block* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + assert(dest); + assert(src); + + if (dest == NULL) + return aesni_make_null_argument_error(err_details, "dest"); + if (src == NULL) + return aesni_make_null_argument_error(err_details, "src"); + + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + *dest = aesni_load_block128_aligned(bytes); + return AESNI_SUCCESS; +} + +AesNI_StatusCode aesni_aes128_format_key( + AesNI_Aes128_KeyString* str, + const AesNI_Aes128_Key* key, + AesNI_ErrorDetails* err_details) +{ + return aesni_aes_format_block(str, &key->key, err_details); +} + +AesNI_StatusCode aesni_aes192_format_key( + AesNI_Aes192_KeyString* str, + const AesNI_Aes192_Key* key, + AesNI_ErrorDetails* err_details) +{ + assert(str); + assert(key); + + if (str == NULL) + return aesni_make_null_argument_error(err_details, "str"); + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); + + char* cursor = str->str; + + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, key->lo); + + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } + + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, key->hi); + + for (int i = 0; i < 8; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } + + *cursor = '\0'; + return AESNI_SUCCESS; +} + +AesNI_StatusCode aesni_aes256_format_key( + AesNI_Aes256_KeyString* str, + const AesNI_Aes256_Key* key, + AesNI_ErrorDetails* err_details) +{ + assert(str); + assert(key); + + if (str == NULL) + return aesni_make_null_argument_error(err_details, "str"); + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); + + char* cursor = str->str; + + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, key->lo); + + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } + + { + __declspec(align(16)) unsigned char bytes[16]; + aesni_store_block128_aligned(bytes, key->hi); + + for (int i = 0; i < 16; ++i, cursor += 2) + sprintf(cursor, "%02x", bytes[i]); + } + + *cursor = '\0'; + return AESNI_SUCCESS; +} + +AesNI_StatusCode aesni_aes128_print_key( + const AesNI_Aes128_Key* key, + AesNI_ErrorDetails* err_details) +{ + return aesni_aes_print_block(&key->key, err_details); +} + +AesNI_StatusCode aesni_aes192_print_key( + const AesNI_Aes192_Key* key, + AesNI_ErrorDetails* err_details) +{ + assert(key); + + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); + + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes192_KeyString str; + + if (aesni_is_error(ec = aesni_aes192_format_key(&str, key, err_details))) + return ec; + + printf("%s\n", str.str); + return ec; +} + +AesNI_StatusCode aesni_aes256_print_key( + const AesNI_Aes256_Key* key, + AesNI_ErrorDetails* err_details) +{ + assert(key); + + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); + + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes256_KeyString str; + + if (aesni_is_error(ec = aesni_aes256_format_key(&str, key, err_details))) + return ec; + + printf("%s\n", str.str); + return ec; +} + +AesNI_StatusCode aesni_aes128_parse_key( + AesNI_Aes128_Key* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + return aesni_aes_parse_block(&dest->key, src, err_details); +} + +AesNI_StatusCode aesni_aes192_parse_key( + AesNI_Aes192_Key* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + assert(dest); + assert(src); + + if (dest == NULL) + return aesni_make_null_argument_error(err_details, "dest"); + if (src == NULL) + return aesni_make_null_argument_error(err_details, "src"); + + { + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + dest->lo = aesni_load_block128_aligned(bytes); + } + + { + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 8; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + memset(bytes + 8, 0x00, 8); + dest->hi = aesni_load_block128_aligned(bytes); + } + + return AESNI_SUCCESS; +} + +AesNI_StatusCode aesni_aes256_parse_key( + AesNI_Aes256_Key* dest, + const char* src, + AesNI_ErrorDetails* err_details) +{ + assert(dest); + assert(src); + + if (dest == NULL) + return aesni_make_null_argument_error(err_details, "dest"); + if (src == NULL) + return aesni_make_null_argument_error(err_details, "src"); + + { + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + dest->lo = aesni_load_block128_aligned(bytes); + } + + { + __declspec(align(16)) unsigned char bytes[16]; + + for (int i = 0; i < 16; ++i) + { + int n; + unsigned int byte; + if (sscanf(src, "%2x%n", &byte, &n) != 1) + return aesni_make_parse_error(err_details, src); + bytes[i] = (unsigned char) byte; + src += n; + } + + dest->hi = aesni_load_block128_aligned(bytes); + } + + return AESNI_SUCCESS; +} diff --git a/src/c/aes128.c b/src/c/aes128.c index 8f2da44..b2e37ba 100644 --- a/src/c/aes128.c +++ b/src/c/aes128.c @@ -11,8 +11,8 @@ #include #include -AesNI_Block128 __fastcall aesni_aes128_encrypt_block_( - AesNI_Block128 plaintext, +AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_( + AesNI_Aes_Block plaintext, const AesNI_Aes128_RoundKeys* encryption_keys) { plaintext = _mm_xor_si128(plaintext, encryption_keys->keys[0]); @@ -28,8 +28,8 @@ AesNI_Block128 __fastcall aesni_aes128_encrypt_block_( return _mm_aesenclast_si128(plaintext, encryption_keys->keys[10]); } -AesNI_Block128 __fastcall aesni_aes128_decrypt_block_( - AesNI_Block128 ciphertext, +AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_( + AesNI_Aes_Block ciphertext, const AesNI_Aes128_RoundKeys* decryption_keys) { ciphertext = _mm_xor_si128(ciphertext, decryption_keys->keys[0]); @@ -45,11 +45,11 @@ AesNI_Block128 __fastcall aesni_aes128_decrypt_block_( return _mm_aesdeclast_si128(ciphertext, decryption_keys->keys[10]); } -static AesNI_Block128 __fastcall aesni_aes128_expand_key_assist( - AesNI_Block128 prev, - AesNI_Block128 hwgen) +static AesNI_Aes_Block __fastcall aesni_aes128_expand_key_assist( + AesNI_Aes_Block prev, + AesNI_Aes_Block hwgen) { - AesNI_Block128 tmp = prev; + AesNI_Aes_Block tmp = prev; tmp = _mm_slli_si128(tmp, 4); prev = _mm_xor_si128(prev, tmp); @@ -65,7 +65,7 @@ static AesNI_Block128 __fastcall aesni_aes128_expand_key_assist( } void __fastcall aesni_aes128_expand_key_( - AesNI_Block128 key, + AesNI_Aes_Block key, AesNI_Aes128_RoundKeys* encryption_keys) { AesNI_Block128 prev = encryption_keys->keys[0] = key; diff --git a/src/c/aes192.c b/src/c/aes192.c index 6e97637..702ee5e 100644 --- a/src/c/aes192.c +++ b/src/c/aes192.c @@ -11,8 +11,8 @@ #include #include -AesNI_Block128 __fastcall aesni_aes192_encrypt_block_( - AesNI_Block128 plaintext, +AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_( + AesNI_Aes_Block plaintext, const AesNI_Aes192_RoundKeys* encryption_keys) { plaintext = _mm_xor_si128(plaintext, encryption_keys->keys[0]); @@ -30,8 +30,8 @@ AesNI_Block128 __fastcall aesni_aes192_encrypt_block_( return _mm_aesenclast_si128(plaintext, encryption_keys->keys[12]); } -AesNI_Block128 __fastcall aesni_aes192_decrypt_block_( - AesNI_Block128 ciphertext, +AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_( + AesNI_Aes_Block ciphertext, const AesNI_Aes192_RoundKeys* decryption_keys) { ciphertext = _mm_xor_si128(ciphertext, decryption_keys->keys[0]); @@ -50,11 +50,11 @@ AesNI_Block128 __fastcall aesni_aes192_decrypt_block_( } static void __fastcall aesni_aes192_expand_key_assist( - AesNI_Block128* prev_lo, - AesNI_Block128* prev_hi, - AesNI_Block128 hwgen) + AesNI_Aes_Block* prev_lo, + AesNI_Aes_Block* prev_hi, + AesNI_Aes_Block hwgen) { - AesNI_Block128 tmp = *prev_lo; + AesNI_Aes_Block tmp = *prev_lo; tmp = _mm_slli_si128(tmp, 4); *prev_lo = _mm_xor_si128(*prev_lo, tmp); @@ -75,8 +75,8 @@ static void __fastcall aesni_aes192_expand_key_assist( } void __fastcall aesni_aes192_expand_key_( - AesNI_Block128 key_lo, - AesNI_Block128 key_hi, + AesNI_Aes_Block key_lo, + AesNI_Aes_Block key_hi, AesNI_Aes192_RoundKeys* encryption_keys) { encryption_keys->keys[0] = key_lo; diff --git a/src/c/aes256.c b/src/c/aes256.c index 820010d..243ec03 100644 --- a/src/c/aes256.c +++ b/src/c/aes256.c @@ -11,8 +11,8 @@ #include #include -AesNI_Block128 __fastcall aesni_aes256_encrypt_block_( - AesNI_Block128 plaintext, +AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_( + AesNI_Aes_Block plaintext, const AesNI_Aes256_RoundKeys* encryption_keys) { plaintext = _mm_xor_si128(plaintext, encryption_keys->keys[0]); @@ -32,8 +32,8 @@ AesNI_Block128 __fastcall aesni_aes256_encrypt_block_( return _mm_aesenclast_si128(plaintext, encryption_keys->keys[14]); } -AesNI_Block128 __fastcall aesni_aes256_decrypt_block_( - AesNI_Block128 ciphertext, +AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_( + AesNI_Aes_Block ciphertext, const AesNI_Aes256_RoundKeys* decryption_keys) { ciphertext = _mm_xor_si128(ciphertext, decryption_keys->keys[0]); @@ -53,12 +53,12 @@ AesNI_Block128 __fastcall aesni_aes256_decrypt_block_( return _mm_aesdeclast_si128(ciphertext, decryption_keys->keys[14]); } -static AesNI_Block128 __fastcall aesni_aes256_expand_key_assist( - AesNI_Block128* prev_lo, - AesNI_Block128* prev_hi, - AesNI_Block128 hwgen) +static AesNI_Aes_Block __fastcall aesni_aes256_expand_key_assist( + AesNI_Aes_Block* prev_lo, + AesNI_Aes_Block* prev_hi, + AesNI_Aes_Block hwgen) { - AesNI_Block128 tmp = *prev_lo; + AesNI_Aes_Block tmp = *prev_lo; tmp = _mm_slli_si128(tmp, 4); *prev_lo = _mm_xor_si128(*prev_lo, tmp); @@ -77,12 +77,12 @@ static AesNI_Block128 __fastcall aesni_aes256_expand_key_assist( } void __fastcall aesni_aes256_expand_key_( - AesNI_Block128 key_lo, - AesNI_Block128 key_hi, + AesNI_Aes_Block key_lo, + AesNI_Aes_Block key_hi, AesNI_Aes256_RoundKeys* encryption_keys) { - AesNI_Block128 prev_lo, prev_hi; - AesNI_Block128 hwgen; + AesNI_Aes_Block prev_lo, prev_hi; + AesNI_Aes_Block hwgen; prev_lo = encryption_keys->keys[0] = key_lo; prev_hi = encryption_keys->keys[1] = key_hi; diff --git a/src/data.c b/src/data.c deleted file mode 100644 index ef2d7f3..0000000 --- a/src/data.c +++ /dev/null @@ -1,410 +0,0 @@ -/** - * \file - * \author Egor Tensin - * \date 2015 - * \copyright This file is licensed under the terms of the MIT License. - * See LICENSE.txt for details. - */ - -#include - -#include - -#include -#include -#include - -AesNI_StatusCode aesni_format_block128(AesNI_BlockString128* str, const AesNI_Block128* block, AesNI_ErrorDetails* err_details) -{ - assert(str); - assert(block); - - if (str == NULL) - return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - char* cursor = str->str; - - __declspec(align(16)) unsigned char bytes[16]; - aesni_store_block128_aligned(bytes, *block); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - - *cursor = '\0'; - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_format_block192(AesNI_BlockString192* str, const AesNI_Block192* block, AesNI_ErrorDetails* err_details) -{ - assert(str); - assert(block); - - if (str == NULL) - return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - char* cursor = str->str; - - { - __declspec(align(16)) unsigned char bytes[16]; - aesni_store_block128_aligned(bytes, block->lo); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - - { - __declspec(align(16)) unsigned char bytes[16]; - aesni_store_block128_aligned(bytes, block->hi); - - for (int i = 0; i < 8; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - - *cursor = '\0'; - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_format_block256(AesNI_BlockString256* str, const AesNI_Block256* block, AesNI_ErrorDetails* err_details) -{ - assert(str); - assert(block); - - if (str == NULL) - return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - char* cursor = str->str; - - { - __declspec(align(16)) unsigned char bytes[16]; - aesni_store_block128_aligned(bytes, block->lo); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - - { - __declspec(align(16)) unsigned char bytes[16]; - aesni_store_block128_aligned(bytes, block->hi); - - for (int i = 0; i < 16; ++i, cursor += 2) - sprintf(cursor, "%02x", bytes[i]); - } - - *cursor = '\0'; - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_format_block128_as_matrix(AesNI_BlockMatrixString128* str, const AesNI_Block128* block, AesNI_ErrorDetails* err_details) -{ - assert(str); - assert(block); - - if (str == NULL) - return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - char* cursor = str->str; - - __declspec(align(16)) unsigned char bytes[4][4]; - aesni_store_block128_aligned(bytes, *block); - - for (int i = 0; i < 4; ++i, cursor += 3) - { - for (int j = 0; j < 3; ++j, cursor += 3) - sprintf(cursor, "%02x ", bytes[j][i]); - sprintf(cursor, "%02x\n", bytes[3][i]); - } - - *cursor = '\0'; - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_format_block192_as_matrix(AesNI_BlockMatrixString192* str, const AesNI_Block192* block, AesNI_ErrorDetails* err_details) -{ - assert(str); - assert(block); - - if (str == NULL) - return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - char* cursor = str->str; - - __declspec(align(16)) unsigned char bytes[8][4]; - aesni_store_block128_aligned(bytes, block->lo); - aesni_store_block128_aligned(bytes + 4, block->hi); - - for (int i = 0; i < 4; ++i, cursor += 3) - { - for (int j = 0; j < 5; ++j, cursor += 3) - sprintf(cursor, "%02x ", bytes[j][i]); - sprintf(cursor, "%02x\n", bytes[5][i]); - } - - *cursor = '\0'; - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_format_block256_as_matrix(AesNI_BlockMatrixString256* str, const AesNI_Block256* block, AesNI_ErrorDetails* err_details) -{ - assert(str); - assert(block); - - if (str == NULL) - return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - char* cursor = str->str; - - __declspec(align(16)) unsigned char bytes[8][4]; - aesni_store_block128_aligned(bytes, block->lo); - aesni_store_block128_aligned(bytes + 4, block->hi); - - for (int i = 0; i < 4; ++i, cursor += 3) - { - for (int j = 0; j < 7; ++j, cursor += 3) - sprintf(cursor, "%02x ", bytes[j][i]); - sprintf(cursor, "%02x\n", bytes[7][i]); - } - - *cursor = '\0'; - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_print_block128(const AesNI_Block128* block, AesNI_ErrorDetails* err_details) -{ - assert(block); - - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockString128 str; - - if (aesni_is_error(ec = aesni_format_block128(&str, block, err_details))) - return ec; - - printf("%s\n", str.str); - return ec; -} - -AesNI_StatusCode aesni_print_block192(const AesNI_Block192* block, AesNI_ErrorDetails* err_details) -{ - assert(block); - - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockString192 str; - - if (aesni_is_error(ec = aesni_format_block192(&str, block, err_details))) - return ec; - - printf("%s\n", str.str); - return ec; -} - -AesNI_StatusCode aesni_print_block256(const AesNI_Block256* block, AesNI_ErrorDetails* err_details) -{ - assert(block); - - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockString256 str; - - if (aesni_is_error(ec = aesni_format_block256(&str, block, err_details))) - return ec; - - printf("%s\n", str.str); - return ec; -} - -AesNI_StatusCode aesni_print_block128_as_matrix(const AesNI_Block128* block, AesNI_ErrorDetails* err_details) -{ - assert(block); - - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockMatrixString128 str; - - if (aesni_is_error(ec = aesni_format_block128_as_matrix(&str, block, err_details))) - return ec; - - printf("%s", str.str); - return ec; -} - -AesNI_StatusCode aesni_print_block192_as_matrix(const AesNI_Block192* block, AesNI_ErrorDetails* err_details) -{ - assert(block); - - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockMatrixString192 str; - - if (aesni_is_error(ec = aesni_format_block192_as_matrix(&str, block, err_details))) - return ec; - - printf("%s", str.str); - return ec; -} - -AesNI_StatusCode aesni_print_block256_as_matrix(const AesNI_Block256* block, AesNI_ErrorDetails* err_details) -{ - assert(block); - - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); - - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockMatrixString256 str; - - if (aesni_is_error(ec = aesni_format_block256_as_matrix(&str, block, err_details))) - return ec; - - printf("%s", str.str); - return ec; -} - -AesNI_StatusCode aesni_parse_block128( - AesNI_Block128* dest, - const char* src, - AesNI_ErrorDetails* err_details) -{ - assert(dest); - assert(src); - - if (dest == NULL) - return aesni_make_null_argument_error(err_details, "dest"); - if (src == NULL) - return aesni_make_null_argument_error(err_details, "src"); - - __declspec(align(16)) unsigned char bytes[16]; - - for (int i = 0; i < 16; ++i) - { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return aesni_make_parse_error(err_details, src); - bytes[i] = (unsigned char) byte; - src += n; - } - - *dest = aesni_load_block128_aligned(bytes); - - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_parse_block192( - AesNI_Block192* dest, - const char* src, - AesNI_ErrorDetails* err_details) -{ - assert(dest); - assert(src); - - if (dest == NULL) - return aesni_make_null_argument_error(err_details, "dest"); - if (src == NULL) - return aesni_make_null_argument_error(err_details, "src"); - - { - __declspec(align(16)) unsigned char bytes[16]; - - for (int i = 0; i < 16; ++i) - { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return aesni_make_parse_error(err_details, src); - bytes[i] = (unsigned char) byte; - src += n; - } - - dest->lo = aesni_load_block128_aligned(bytes); - } - - { - __declspec(align(16)) unsigned char bytes[16]; - - for (int i = 0; i < 8; ++i) - { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return aesni_make_parse_error(err_details, src); - bytes[i] = (unsigned char) byte; - src += n; - } - - memset(bytes + 8, 0x00, 8); - dest->hi = aesni_load_block128_aligned(bytes); - } - - return aesni_initialize_error_details(err_details); -} - -AesNI_StatusCode aesni_parse_block256( - AesNI_Block256* dest, - const char* src, - AesNI_ErrorDetails* err_details) -{ - assert(dest); - assert(src); - - if (dest == NULL) - return aesni_make_null_argument_error(err_details, "dest"); - if (src == NULL) - return aesni_make_null_argument_error(err_details, "src"); - - { - __declspec(align(16)) unsigned char bytes[16]; - - for (int i = 0; i < 16; ++i) - { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return aesni_make_parse_error(err_details, src); - bytes[i] = (unsigned char) byte; - src += n; - } - - dest->lo = aesni_load_block128_aligned(bytes); - } - - { - __declspec(align(16)) unsigned char bytes[16]; - - for (int i = 0; i < 16; ++i) - { - int n; - unsigned int byte; - if (sscanf(src, "%2x%n", &byte, &n) != 1) - return aesni_make_parse_error(err_details, src); - bytes[i] = (unsigned char) byte; - src += n; - } - - dest->hi = aesni_load_block128_aligned(bytes); - } - - return aesni_initialize_error_details(err_details); -} -- cgit v1.2.3