aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/data.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-06-15 21:32:42 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-06-15 21:32:42 +0300
commit3f5df3abf1386ce9071046bb00e5eae27a10e2ef (patch)
treec37bdb612d3c0e0175d5092c9d6be8a7b90335f5 /src/data.c
parentcode style (diff)
downloadaes-tools-3f5df3abf1386ce9071046bb00e5eae27a10e2ef.tar.gz
aes-tools-3f5df3abf1386ce9071046bb00e5eae27a10e2ef.zip
handle block parsing errors the standard way
Diffstat (limited to 'src/data.c')
-rw-r--r--src/data.c439
1 files changed, 129 insertions, 310 deletions
diff --git a/src/data.c b/src/data.c
index 3f64b80..b596118 100644
--- a/src/data.c
+++ b/src/data.c
@@ -6,7 +6,7 @@
* See LICENSE.txt for details.
*/
-#include "aesni/all.h"
+#include <aesni/all.h>
#include <intrin.h>
@@ -18,122 +18,68 @@ AesNI_BlockString128 aesni_format_block128(AesNI_Block128* block)
{
assert(block);
-#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO
- return aesni_format_block128_le(block);
-#else
- return aesni_format_block128_be(block);
-#endif
-}
-
-AesNI_BlockString192 aesni_format_block192(AesNI_Block192* block)
-{
- assert(block);
-
-#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO
- return aesni_format_block192_le(block);
-#else
- return aesni_format_block192_be(block);
-#endif
-}
-
-AesNI_BlockString256 aesni_format_block256(AesNI_Block256* block)
-{
- assert(block);
-
-#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO
- return aesni_format_block256_le(block);
-#else
- return aesni_format_block256_be(block);
-#endif
-}
-
-AesNI_BlockString128 aesni_format_block128_le(AesNI_Block128* block)
-{
- assert(block);
-
AesNI_BlockString128 result;
char *cursor = result.str;
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) block + 15 - i));
-
- *cursor = '\0';
- return result;
-}
-
-AesNI_BlockString192 aesni_format_block192_le(AesNI_Block192* block)
-{
- assert(block);
+ __declspec(align(16)) unsigned char bytes[16];
+ aesni_store_block128_aligned(bytes, *block);
- AesNI_BlockString192 result;
- char *cursor = result.str;
-
- for (int i = 0; i < 8; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->hi + 7 - i));
for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->lo + 15 - i));
+ sprintf(cursor, "%02x", bytes[i]);
*cursor = '\0';
return result;
}
-AesNI_BlockString256 aesni_format_block256_le(AesNI_Block256* block)
+AesNI_BlockString192 aesni_format_block192(AesNI_Block192* block)
{
assert(block);
- AesNI_BlockString256 result;
+ AesNI_BlockString192 result;
char *cursor = result.str;
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->hi + 15 - i));
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->lo + 15 - i));
-
- *cursor = '\0';
- return result;
-}
+ {
+ __declspec(align(16)) unsigned char bytes[16];
+ aesni_store_block128_aligned(bytes, block->lo);
-AesNI_BlockString128 aesni_format_block128_be(AesNI_Block128* block)
-{
- assert(block);
+ for (int i = 0; i < 16; ++i, cursor += 2)
+ sprintf(cursor, "%02x", bytes[i]);
+ }
- AesNI_BlockString128 result;
- char *cursor = result.str;
+ {
+ __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", *((unsigned char*) block + i));
+ for (int i = 0; i < 8; ++i, cursor += 2)
+ sprintf(cursor, "%02x", bytes[i]);
+ }
*cursor = '\0';
return result;
}
-AesNI_BlockString192 aesni_format_block192_be(AesNI_Block192* block)
+AesNI_BlockString256 aesni_format_block256(AesNI_Block256* block)
{
assert(block);
- AesNI_BlockString192 result;
+ AesNI_BlockString256 result;
char *cursor = result.str;
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->lo + i));
- for (int i = 0; i < 8; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->hi + i));
-
- *cursor = '\0';
- return result;
-}
+ {
+ __declspec(align(16)) unsigned char bytes[16];
+ aesni_store_block128_aligned(bytes, block->lo);
-AesNI_BlockString256 aesni_format_block256_be(AesNI_Block256* block)
-{
- assert(block);
+ for (int i = 0; i < 16; ++i, cursor += 2)
+ sprintf(cursor, "%02x", bytes[i]);
+ }
- AesNI_BlockString256 result;
- char *cursor = result.str;
+ {
+ __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", *((unsigned char*) &block->lo + i));
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", *((unsigned char*) &block->hi + i));
+ for (int i = 0; i < 16; ++i, cursor += 2)
+ sprintf(cursor, "%02x", bytes[i]);
+ }
*cursor = '\0';
return result;
@@ -143,32 +89,11 @@ AesNI_BlockMatrixString128 aesni_format_block128_as_matrix(AesNI_Block128* block
{
assert(block);
- return aesni_format_block128_be_as_matrix(block);
-}
-
-AesNI_BlockMatrixString192 aesni_format_block192_as_matrix(AesNI_Block192* block)
-{
- assert(block);
-
- return aesni_format_block192_be_as_matrix(block);
-}
-
-AesNI_BlockMatrixString256 aesni_format_block256_as_matrix(AesNI_Block256* block)
-{
- assert(block);
-
- return aesni_format_block256_be_as_matrix(block);
-}
-
-AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128* block)
-{
- assert(block);
-
- __declspec(align(16)) unsigned char bytes[4][4];
AesNI_BlockMatrixString128 result;
char* cursor = result.str;
- _mm_store_si128((AesNI_Block128*) bytes, *block);
+ __declspec(align(16)) unsigned char bytes[4][4];
+ aesni_store_block128_aligned(bytes, *block);
for (int i = 0; i < 4; ++i, cursor += 3)
{
@@ -181,16 +106,16 @@ AesNI_BlockMatrixString128 aesni_format_block128_be_as_matrix(AesNI_Block128* bl
return result;
}
-AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192* block)
+AesNI_BlockMatrixString192 aesni_format_block192_as_matrix(AesNI_Block192* block)
{
assert(block);
- __declspec(align(16)) unsigned char bytes[8][4];
AesNI_BlockMatrixString192 result;
char* cursor = result.str;
- _mm_store_si128((AesNI_Block128*) bytes, block->lo);
- _mm_store_si128((AesNI_Block128*) bytes + 1, block->hi);
+ __declspec(align(16)) unsigned char bytes[8][4];
+ aesni_store_block128_aligned(bytes, block->lo);
+ aesni_store_block128_aligned(bytes + 16, block->hi);
for (int i = 0; i < 4; ++i, cursor += 3)
{
@@ -203,16 +128,16 @@ AesNI_BlockMatrixString192 aesni_format_block192_be_as_matrix(AesNI_Block192* bl
return result;
}
-AesNI_BlockMatrixString256 aesni_format_block256_be_as_matrix(AesNI_Block256* block)
+AesNI_BlockMatrixString256 aesni_format_block256_as_matrix(AesNI_Block256* block)
{
assert(block);
- __declspec(align(16)) unsigned char bytes[8][4];
AesNI_BlockMatrixString256 result;
char* cursor = result.str;
- _mm_store_si128((AesNI_Block128*) bytes, block->lo);
- _mm_store_si128((AesNI_Block128*) bytes + 1, block->hi);
+ __declspec(align(16)) unsigned char bytes[8][4];
+ aesni_store_block128_aligned(bytes, block->lo);
+ aesni_store_block128_aligned(bytes + 16, block->hi);
for (int i = 0; i < 4; ++i, cursor += 3)
{
@@ -246,48 +171,6 @@ void aesni_print_block256(AesNI_Block256* block)
printf("%s\n", aesni_format_block256(block).str);
}
-void aesni_print_block128_le(AesNI_Block128* block)
-{
- assert(block);
-
- printf("%s\n", aesni_format_block128_le(block).str);
-}
-
-void aesni_print_block192_le(AesNI_Block192* block)
-{
- assert(block);
-
- printf("%s\n", aesni_format_block192_le(block).str);
-}
-
-void aesni_print_block256_le(AesNI_Block256* block)
-{
- assert(block);
-
- printf("%s\n", aesni_format_block256_le(block).str);
-}
-
-void aesni_print_block128_be(AesNI_Block128* block)
-{
- assert(block);
-
- printf("%s\n", aesni_format_block128_be(block).str);
-}
-
-void aesni_print_block192_be(AesNI_Block192* block)
-{
- assert(block);
-
- printf("%s\n", aesni_format_block192_be(block).str);
-}
-
-void aesni_print_block256_be(AesNI_Block256* block)
-{
- assert(block);
-
- printf("%s\n", aesni_format_block256_be(block).str);
-}
-
void aesni_print_block128_as_matrix(AesNI_Block128* block)
{
assert(block);
@@ -309,193 +192,129 @@ void aesni_print_block256_as_matrix(AesNI_Block256* block)
printf("%s", aesni_format_block256_as_matrix(block).str);
}
-void aesni_print_block128_be_as_matrix(AesNI_Block128* block)
-{
- assert(block);
-
- printf("%s", aesni_format_block128_be_as_matrix(block).str);
-}
-
-void aesni_print_block192_be_as_matrix(AesNI_Block192* block)
-{
- assert(block);
-
- printf("%s", aesni_format_block192_be_as_matrix(block).str);
-}
-
-void aesni_print_block256_be_as_matrix(AesNI_Block256* block)
-{
- assert(block);
-
- printf("%s", aesni_format_block256_be_as_matrix(block).str);
-}
-
-int aesni_parse_block128(AesNI_Block128* block, const char* src)
-{
- assert(block);
- assert(src);
-
-#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO
- return aesni_parse_block128_le(block, src);
-#else
- return aesni_parse_block128_be(block, src);
-#endif
-}
-
-int aesni_parse_block192(AesNI_Block192* block, const char* src)
-{
- assert(block);
- assert(src);
-
-#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO
- return aesni_parse_block192_le(block, src);
-#else
- return aesni_parse_block192_be(block, src);
-#endif
-}
-
-int aesni_parse_block256(AesNI_Block256* block, const char* src)
-{
- assert(block);
- assert(src);
-
-#if defined AESNI_LE_BLOCK_IO && AESNI_LE_BLOCK_IO
- return aesni_parse_block256_le(block, src);
-#else
- return aesni_parse_block256_be(block, src);
-#endif
-}
-
-int aesni_parse_block128_le(AesNI_Block128* block, const char* src)
+AesNI_StatusCode aesni_parse_block128(
+ AesNI_Block128* dest,
+ const char* src,
+ AesNI_ErrorDetails* err_details)
{
- assert(block);
+ assert(dest);
assert(src);
- int n, xs[4];
- if (sscanf(src, "%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &n) != 4
- || n != strlen(src))
- return 1;
- *block = aesni_make_block128(xs[0], xs[1], xs[2], xs[3]);
- return 0;
-}
+ if (dest == NULL)
+ return aesni_make_null_argument_error(err_details, "dest");
+ if (src == NULL)
+ return aesni_make_null_argument_error(err_details, "src");
-int aesni_parse_block192_le(AesNI_Block192* block, const char* src)
-{
- assert(block);
- assert(src);
-
- int n, xs[6];
- if (sscanf(src, "%8x%8x%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &xs[4], &xs[5], &n) != 6
- || n != strlen(src))
- return 1;
- *block = aesni_make_block192(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5]);
- return 0;
-}
-
-int aesni_parse_block256_le(AesNI_Block256* block, const char* src)
-{
- assert(block);
- assert(src);
-
- int n, xs[8];
- if (sscanf(src, "%8x%8x%8x%8x%8x%8x%8x%8x%n", &xs[0], &xs[1], &xs[2], &xs[3], &xs[4], &xs[5], &xs[6], &xs[7], &n) != 8
- || n != strlen(src))
- return 1;
- *block = aesni_make_block256(xs[0], xs[1], xs[2], xs[3], xs[4], xs[5], xs[6], xs[7]);
- return 0;
-}
-
-int aesni_parse_block128_be(AesNI_Block128* block, const char* src)
-{
- assert(block);
- assert(src);
-
- unsigned char bytes[16];
+ __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 1;
+ return aesni_make_parse_error(err_details, src);
bytes[i] = (unsigned char) byte;
src += n;
}
- *block = _mm_loadu_si128((AesNI_Block128*) bytes);
- return 0;
+ *dest = aesni_load_block128_aligned(bytes);
+
+ return aesni_initialize_error_details(err_details);
}
-int aesni_parse_block192_be(AesNI_Block192* block, const char* src)
+AesNI_StatusCode aesni_parse_block192(
+ AesNI_Block192* dest,
+ const char* src,
+ AesNI_ErrorDetails* err_details)
{
- assert(block);
+ assert(dest);
assert(src);
- AesNI_Block128 lo, hi;
- unsigned char lo_bytes[16], hi_bytes[16] = { 0 };
+ if (dest == NULL)
+ return aesni_make_null_argument_error(err_details, "dest");
+ if (src == NULL)
+ return aesni_make_null_argument_error(err_details, "src");
- for (int i = 0; i < 16; ++i)
{
- int n;
- unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return 1;
- lo_bytes[i] = (unsigned char) byte;
- src += n;
+ __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);
}
- lo = _mm_loadu_si128((AesNI_Block128*) lo_bytes);
-
- for (int i = 0; i < 8; ++i)
{
- int n;
- unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return 1;
- hi_bytes[i] = (unsigned char) byte;
- src += n;
+ __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);
}
- hi = _mm_loadu_si128((AesNI_Block128*) hi_bytes);
-
- block->hi = hi;
- block->lo = lo;
- return 0;
+ return aesni_initialize_error_details(err_details);
}
-int aesni_parse_block256_be(AesNI_Block256* block, const char* src)
+AesNI_StatusCode aesni_parse_block256(
+ AesNI_Block256* dest,
+ const char* src,
+ AesNI_ErrorDetails* err_details)
{
- assert(block);
+ assert(dest);
assert(src);
- AesNI_Block128 lo, hi;
- unsigned char lo_bytes[16], hi_bytes[16];
+ if (dest == NULL)
+ return aesni_make_null_argument_error(err_details, "dest");
+ if (src == NULL)
+ return aesni_make_null_argument_error(err_details, "src");
- for (int i = 0; i < 16; ++i)
{
- int n;
- unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return 1;
- lo_bytes[i] = (unsigned char) byte;
- src += n;
+ __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);
}
- lo = _mm_loadu_si128((AesNI_Block128*) lo_bytes);
-
- for (int i = 0; i < 16; ++i)
{
- int n;
- unsigned int byte;
- if (sscanf(src, "%2x%n", &byte, &n) != 1)
- return 1;
- hi_bytes[i] = (unsigned char) byte;
- src += n;
+ __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);
}
- hi = _mm_loadu_si128((AesNI_Block128*) hi_bytes);
-
- block->hi = hi;
- block->lo = lo;
- return 0;
+ return aesni_initialize_error_details(err_details);
}