aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/aes.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/aes.c (renamed from src/data.c)326
1 files changed, 139 insertions, 187 deletions
diff --git a/src/data.c b/src/aes.c
index ef2d7f3..98cc9cd 100644
--- a/src/data.c
+++ b/src/aes.c
@@ -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;
}