diff options
Diffstat (limited to '')
-rw-r--r-- | src/aes.c (renamed from src/data.c) | 326 | ||||
-rw-r--r-- | src/c/aes128.c | 18 | ||||
-rw-r--r-- | src/c/aes192.c | 20 | ||||
-rw-r--r-- | src/c/aes256.c | 26 |
4 files changed, 171 insertions, 219 deletions
@@ -8,13 +8,14 @@ #include <aesni/all.h> -#include <intrin.h> - #include <assert.h> #include <stdio.h> #include <string.h> -AesNI_StatusCode aesni_format_block128(AesNI_BlockString128* str, const AesNI_Block128* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes_format_block( + AesNI_Aes_BlockString* str, + const AesNI_Aes_Block* block, + AesNI_ErrorDetails* err_details) { assert(str); assert(block); @@ -33,10 +34,13 @@ AesNI_StatusCode aesni_format_block128(AesNI_BlockString128* str, const AesNI_Bl sprintf(cursor, "%02x", bytes[i]); *cursor = '\0'; - return aesni_initialize_error_details(err_details); + return AESNI_SUCCESS; } -AesNI_StatusCode aesni_format_block192(AesNI_BlockString192* str, const AesNI_Block192* block, AesNI_ErrorDetails* err_details) +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); @@ -48,272 +52,220 @@ AesNI_StatusCode aesni_format_block192(AesNI_BlockString192* str, const AesNI_Bl 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[4][4]; + aesni_store_block128_aligned(bytes, *block); + for (int i = 0; i < 4; ++i, cursor += 3) { - __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]); + 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); + return AESNI_SUCCESS; } -AesNI_StatusCode aesni_format_block256(AesNI_BlockString256* str, const AesNI_Block256* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes_print_block( + 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; + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes_BlockString 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]); - } + if (aesni_is_error(ec = aesni_aes_format_block(&str, block, err_details))) + return ec; - *cursor = '\0'; - return aesni_initialize_error_details(err_details); + printf("%s\n", str.str); + return ec; } -AesNI_StatusCode aesni_format_block128_as_matrix(AesNI_BlockMatrixString128* str, const AesNI_Block128* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes_print_block_as_matrix( + 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; + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes_BlockMatrixString str; - __declspec(align(16)) unsigned char bytes[4][4]; - aesni_store_block128_aligned(bytes, *block); + if (aesni_is_error(ec = aesni_aes_format_block_as_matrix(&str, block, err_details))) + return ec; - for (int i = 0; i < 4; ++i, cursor += 3) + 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) { - for (int j = 0; j < 3; ++j, cursor += 3) - sprintf(cursor, "%02x ", bytes[j][i]); - sprintf(cursor, "%02x\n", bytes[3][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; } - *cursor = '\0'; - return aesni_initialize_error_details(err_details); + *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_format_block192_as_matrix(AesNI_BlockMatrixString192* str, const AesNI_Block192* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes192_format_key( + AesNI_Aes192_KeyString* str, + const AesNI_Aes192_Key* key, + AesNI_ErrorDetails* err_details) { assert(str); - assert(block); + assert(key); if (str == NULL) return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); 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); + { + __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]); + } - 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]); + __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_initialize_error_details(err_details); + return AESNI_SUCCESS; } -AesNI_StatusCode aesni_format_block256_as_matrix(AesNI_BlockMatrixString256* str, const AesNI_Block256* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes256_format_key( + AesNI_Aes256_KeyString* str, + const AesNI_Aes256_Key* key, + AesNI_ErrorDetails* err_details) { assert(str); - assert(block); + assert(key); if (str == NULL) return aesni_make_null_argument_error(err_details, "str"); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); 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); + { + __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]); + } - 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]); + __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_initialize_error_details(err_details); + return AESNI_SUCCESS; } -AesNI_StatusCode aesni_print_block128(const AesNI_Block128* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes128_print_key( + const AesNI_Aes128_Key* key, + 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; + return aesni_aes_print_block(&key->key, err_details); } -AesNI_StatusCode aesni_print_block192(const AesNI_Block192* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes192_print_key( + const AesNI_Aes192_Key* key, + AesNI_ErrorDetails* err_details) { - assert(block); + assert(key); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockString192 str; + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes192_KeyString str; - if (aesni_is_error(ec = aesni_format_block192(&str, block, err_details))) + 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_print_block256(const AesNI_Block256* block, AesNI_ErrorDetails* err_details) +AesNI_StatusCode aesni_aes256_print_key( + const AesNI_Aes256_Key* key, + AesNI_ErrorDetails* err_details) { - assert(block); + assert(key); - if (block == NULL) - return aesni_make_null_argument_error(err_details, "block"); + if (key == NULL) + return aesni_make_null_argument_error(err_details, "key"); - AesNI_StatusCode ec = aesni_initialize_error_details(err_details); - AesNI_BlockString256 str; + AesNI_StatusCode ec = AESNI_SUCCESS; + AesNI_Aes256_KeyString str; - if (aesni_is_error(ec = aesni_format_block256(&str, block, err_details))) + 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_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, +AesNI_StatusCode aesni_aes128_parse_key( + AesNI_Aes128_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 = aesni_load_block128_aligned(bytes); - - return aesni_initialize_error_details(err_details); + return aesni_aes_parse_block(&dest->key, src, err_details); } -AesNI_StatusCode aesni_parse_block192( - AesNI_Block192* dest, +AesNI_StatusCode aesni_aes192_parse_key( + AesNI_Aes192_Key* dest, const char* src, AesNI_ErrorDetails* err_details) { @@ -358,11 +310,11 @@ AesNI_StatusCode aesni_parse_block192( dest->hi = aesni_load_block128_aligned(bytes); } - return aesni_initialize_error_details(err_details); + return AESNI_SUCCESS; } -AesNI_StatusCode aesni_parse_block256( - AesNI_Block256* dest, +AesNI_StatusCode aesni_aes256_parse_key( + AesNI_Aes256_Key* dest, const char* src, AesNI_ErrorDetails* err_details) { @@ -406,5 +358,5 @@ AesNI_StatusCode aesni_parse_block256( dest->hi = aesni_load_block128_aligned(bytes); } - return aesni_initialize_error_details(err_details); + 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 <emmintrin.h> #include <wmmintrin.h> -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 <emmintrin.h> #include <wmmintrin.h> -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 <emmintrin.h> #include <wmmintrin.h> -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; |