aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--examples/aes128_example.c29
-rw-r--r--examples/aes192_example.c40
-rw-r--r--examples/aes256cbc_example.c48
-rw-r--r--examples/aes256ecb_example.c40
-rw-r--r--include/aesni/data.h49
-rw-r--r--include/aesni/raw.h64
-rw-r--r--src/common.c138
7 files changed, 286 insertions, 122 deletions
diff --git a/examples/aes128_example.c b/examples/aes128_example.c
index b4f689f..afb8dd9 100644
--- a/examples/aes128_example.c
+++ b/examples/aes128_example.c
@@ -12,24 +12,31 @@
int main()
{
- __declspec(align(16)) AesBlock plain, key, cypher, decrypted;
+ __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted;
- plain = make_aes_block(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
- key = make_aes_block(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
+ plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
+ key = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
- printf("Plain:\n");
- print_aes_block(plain);
+ printf("Plain: %s\n", format_aes_block128(&plain).str);
+ printf(" %s\n", format_aes_block128_fips_style(&plain).str);
+ print_aes_block128_fips_matrix_style(&plain);
- printf("\nKey:\n");
- print_aes_block(key);
+ printf("\n");
+ printf("Key: %s\n", format_aes_block128(&key).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key).str);
+ print_aes_block128_fips_matrix_style(&key);
- printf("\nCypher:\n");
cypher = aes128ecb_encrypt(plain, key);
- print_aes_block(cypher);
+ printf("\n");
+ printf("Cypher: %s\n", format_aes_block128(&cypher).str);
+ printf(" %s\n", format_aes_block128_fips_style(&cypher).str);
+ print_aes_block128_fips_matrix_style(&cypher);
- printf("\nDecrypted:\n");
decrypted = aes128ecb_decrypt(cypher, key);
- print_aes_block(decrypted);
+ printf("\n");
+ printf("Decrypted: %s\n", format_aes_block128(&decrypted).str);
+ printf(" %s\n", format_aes_block128_fips_style(&decrypted).str);
+ print_aes_block128_fips_matrix_style(&decrypted);
return 0;
}
diff --git a/examples/aes192_example.c b/examples/aes192_example.c
index dd22d80..a310ab2 100644
--- a/examples/aes192_example.c
+++ b/examples/aes192_example.c
@@ -12,28 +12,38 @@
int main()
{
- __declspec(align(16)) AesBlock plain, cypher, decrypted;
- __declspec(align(16)) AesBlock key_low, key_high;
+ __declspec(align(16)) AesBlock128 plain, cypher, decrypted;
+ __declspec(align(16)) AesBlock128 key_low, key_high;
- plain = make_aes_block(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
- key_low = make_aes_block(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
- key_high = make_aes_block( 0, 0, 0x17161514, 0x13121110);
+ plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
+ key_low = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
+ key_high = make_aes_block128( 0, 0, 0x17161514, 0x13121110);
- printf("Plain:\n");
- print_aes_block(plain);
+ printf("Plain: %s\n", format_aes_block128(&plain).str);
+ printf(" %s\n", format_aes_block128_fips_style(&plain).str);
+ print_aes_block128_fips_matrix_style(&plain);
- printf("\nKey low:\n");
- print_aes_block(key_low);
- printf("\nKey high:\n");
- print_aes_block(key_high);
+ printf("\n");
+ printf("Key (low): %s\n", format_aes_block128(&key_low).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key_low).str);
+ print_aes_block128_fips_matrix_style(&key_low);
+
+ printf("\n");
+ printf("Key (high): %s\n", format_aes_block128(&key_high).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key_high).str);
+ print_aes_block128_fips_matrix_style(&key_high);
- printf("\nCypher:\n");
cypher = aes192ecb_encrypt(plain, key_low, key_high);
- print_aes_block(cypher);
+ printf("\n");
+ printf("Cypher: %s\n", format_aes_block128(&cypher).str);
+ printf(" %s\n", format_aes_block128_fips_style(&cypher).str);
+ print_aes_block128_fips_matrix_style(&cypher);
- printf("\nDecrypted:\n");
decrypted = aes192ecb_decrypt(cypher, key_low, key_high);
- print_aes_block(decrypted);
+ printf("\n");
+ printf("Decrypted: %s\n", format_aes_block128(&decrypted).str);
+ printf(" %s\n", format_aes_block128_fips_style(&decrypted).str);
+ print_aes_block128_fips_matrix_style(&decrypted);
return 0;
}
diff --git a/examples/aes256cbc_example.c b/examples/aes256cbc_example.c
index 1fcc615..2847f41 100644
--- a/examples/aes256cbc_example.c
+++ b/examples/aes256cbc_example.c
@@ -12,32 +12,44 @@
int main()
{
- __declspec(align(16)) AesBlock plain, cypher, decrypted;
- __declspec(align(16)) AesBlock key_low, key_high, iv;
+ __declspec(align(16)) AesBlock128 plain, cypher, decrypted;
+ __declspec(align(16)) AesBlock128 key_low, key_high, iv;
- plain = make_aes_block(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
- key_low = make_aes_block(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
- key_high = make_aes_block(0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110);
- iv = make_aes_block(0xfedcba98, 0x76543210, 0xfedcba98, 0x76543210);
+ plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
+ key_low = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
+ key_high = make_aes_block128(0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110);
+ iv = make_aes_block128(0xfedcba98, 0x76543210, 0xfedcba98, 0x76543210);
- printf("Plain:\n");
- print_aes_block(plain);
+ printf("Plain: %s\n", format_aes_block128(&plain).str);
+ printf(" %s\n", format_aes_block128_fips_style(&plain).str);
+ print_aes_block128_fips_matrix_style(&plain);
- printf("\nKey low:\n");
- print_aes_block(key_low);
- printf("\nKey high:\n");
- print_aes_block(key_high);
+ printf("\n");
+ printf("Key (low): %s\n", format_aes_block128(&key_low).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key_low).str);
+ print_aes_block128_fips_matrix_style(&key_low);
- printf("\nInitialization vector:\n");
- print_aes_block(iv);
+ printf("\n");
+ printf("Key (high): %s\n", format_aes_block128(&key_high).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key_high).str);
+ print_aes_block128_fips_matrix_style(&key_high);
+
+ printf("\n");
+ printf("Initialization vector: %s\n", format_aes_block128(&iv).str);
+ printf(" %s\n", format_aes_block128_fips_style(&iv).str);
+ print_aes_block128_fips_matrix_style(&iv);
- printf("\nCypher:\n");
cypher = aes256cbc_encrypt(plain, key_low, key_high, &iv);
- print_aes_block(cypher);
+ printf("\n");
+ printf("Cypher: %s\n", format_aes_block128(&cypher).str);
+ printf(" %s\n", format_aes_block128_fips_style(&cypher).str);
+ print_aes_block128_fips_matrix_style(&cypher);
- printf("\nDecrypted:\n");
decrypted = aes256cbc_decrypt(cypher, key_low, key_high, &iv);
- print_aes_block(decrypted);
+ printf("\n");
+ printf("Decrypted: %s\n", format_aes_block128(&decrypted).str);
+ printf(" %s\n", format_aes_block128_fips_style(&decrypted).str);
+ print_aes_block128_fips_matrix_style(&decrypted);
return 0;
}
diff --git a/examples/aes256ecb_example.c b/examples/aes256ecb_example.c
index 25ec61c..c15082d 100644
--- a/examples/aes256ecb_example.c
+++ b/examples/aes256ecb_example.c
@@ -12,28 +12,38 @@
int main()
{
- __declspec(align(16)) AesBlock plain, cypher, decrypted;
- __declspec(align(16)) AesBlock key_low, key_high;
+ __declspec(align(16)) AesBlock128 plain, cypher, decrypted;
+ __declspec(align(16)) AesBlock128 key_low, key_high;
- plain = make_aes_block(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
- key_low = make_aes_block(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
- key_high = make_aes_block(0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110);
+ plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
+ key_low = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
+ key_high = make_aes_block128(0x1f1e1d1c, 0x1b1a1918, 0x17161514, 0x13121110);
- printf("Plain:\n");
- print_aes_block(plain);
+ printf("Plain: %s\n", format_aes_block128(&plain).str);
+ printf(" %s\n", format_aes_block128_fips_style(&plain).str);
+ print_aes_block128_fips_matrix_style(&plain);
- printf("\nKey low:\n");
- print_aes_block(key_low);
- printf("\nKey high:\n");
- print_aes_block(key_high);
+ printf("\n");
+ printf("Key (low): %s\n", format_aes_block128(&key_low).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key_low).str);
+ print_aes_block128_fips_matrix_style(&key_low);
+
+ printf("\n");
+ printf("Key (high): %s\n", format_aes_block128(&key_high).str);
+ printf(" %s\n", format_aes_block128_fips_style(&key_high).str);
+ print_aes_block128_fips_matrix_style(&key_high);
- printf("\nCypher:\n");
cypher = aes256ecb_encrypt(plain, key_low, key_high);
- print_aes_block(cypher);
+ printf("\n");
+ printf("Cypher: %s\n", format_aes_block128(&cypher).str);
+ printf(" %s\n", format_aes_block128_fips_style(&cypher).str);
+ print_aes_block128_fips_matrix_style(&cypher);
- printf("\nDecrypted:\n");
decrypted = aes256ecb_decrypt(cypher, key_low, key_high);
- print_aes_block(decrypted);
+ printf("\n");
+ printf("Decrypted: %s\n", format_aes_block128(&decrypted).str);
+ printf(" %s\n", format_aes_block128_fips_style(&decrypted).str);
+ print_aes_block128_fips_matrix_style(&decrypted);
return 0;
}
diff --git a/include/aesni/data.h b/include/aesni/data.h
index d2f6cd5..8819214 100644
--- a/include/aesni/data.h
+++ b/include/aesni/data.h
@@ -10,33 +10,44 @@
#include <emmintrin.h>
-typedef __m128i AesBlock;
-
-AesBlock make_aes_block(int highest, int high, int low, int lowest);
-
-typedef AesBlock Aes128Key;
+typedef __m128i AesBlock128;
typedef struct
{
- AesBlock hi;
- AesBlock lo;
+ AesBlock128 hi;
+ AesBlock128 lo;
}
-Aes192Key;
+AesBlock192;
typedef struct
{
- AesBlock hi;
- AesBlock lo;
+ AesBlock128 hi;
+ AesBlock128 lo;
}
-Aes256Key;
+AesBlock256;
-typedef struct
-{
- unsigned char bytes[4][4];
-}
-AesState;
+AesBlock128 make_aes_block128(int hi3, int hi2, int lo1, int lo0);
+AesBlock192 make_aes_block192(int hi5, int hi4, int lo3, int lo2, int lo1, int lo0);
+AesBlock256 make_aes_block256(int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0);
+
+typedef struct { char str[33]; } AesBlockString128;
+typedef struct { char str[49]; } AesBlockString192;
+typedef struct { char str[65]; } AesBlockString256;
+
+AesBlockString128 format_aes_block128(AesBlock128*);
+AesBlockString192 format_aes_block192(AesBlock192*);
+AesBlockString256 format_aes_block256(AesBlock256*);
+
+AesBlockString128 format_aes_block128_fips_style(AesBlock128*);
+AesBlockString192 format_aes_block192_fips_style(AesBlock192*);
+AesBlockString256 format_aes_block256_fips_style(AesBlock256*);
+
+void print_aes_block128(AesBlock128*);
+void print_aes_block192(AesBlock192*);
+void print_aes_block256(AesBlock256*);
-AesState aes_block_to_state(AesBlock);
-AesBlock aes_state_to_block(AesState);
+void print_aes_block128_fips_style(AesBlock128*);
+void print_aes_block192_fips_style(AesBlock192*);
+void print_aes_block256_fips_style(AesBlock256*);
-void print_aes_block(AesBlock);
+void print_aes_block128_fips_matrix_style(AesBlock128*);
diff --git a/include/aesni/raw.h b/include/aesni/raw.h
index 03ce217..9ee6ae9 100644
--- a/include/aesni/raw.h
+++ b/include/aesni/raw.h
@@ -10,38 +10,38 @@
#include "data.h"
-AesBlock __fastcall aes128ecb_encrypt(
- AesBlock plain,
- AesBlock key);
-AesBlock __fastcall aes128ecb_decrypt(
- AesBlock cypher,
- AesBlock key);
+AesBlock128 __fastcall aes128ecb_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key);
+AesBlock128 __fastcall aes128ecb_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key);
-AesBlock __fastcall aes192ecb_encrypt(
- AesBlock plain,
- AesBlock key_lo,
- AesBlock key_hi);
-AesBlock __fastcall aes192ecb_decrypt(
- AesBlock cypher,
- AesBlock key_lo,
- AesBlock key_hi);
+AesBlock128 __fastcall aes192ecb_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi);
+AesBlock128 __fastcall aes192ecb_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi);
-AesBlock __fastcall aes256ecb_encrypt(
- AesBlock plain,
- AesBlock key_lo,
- AesBlock key_hi);
-AesBlock __fastcall aes256ecb_decrypt(
- AesBlock cypher,
- AesBlock key_lo,
- AesBlock key_hi);
+AesBlock128 __fastcall aes256ecb_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi);
+AesBlock128 __fastcall aes256ecb_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi);
-AesBlock __fastcall aes256cbc_encrypt(
- AesBlock plain,
- AesBlock key_lo,
- AesBlock key_hi,
- AesBlock *iv);
-AesBlock __fastcall aes256cbc_decrypt(
- AesBlock cypher,
- AesBlock key_lo,
- AesBlock key_hi,
- AesBlock *iv);
+AesBlock128 __fastcall aes256cbc_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi,
+ AesBlock128 *iv);
+AesBlock128 __fastcall aes256cbc_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi,
+ AesBlock128 *iv);
diff --git a/src/common.c b/src/common.c
index f4020db..c15da4e 100644
--- a/src/common.c
+++ b/src/common.c
@@ -12,32 +12,146 @@
#include <stdio.h>
-AesBlock make_aes_block(int highest, int high, int low, int lowest)
+AesBlock128 make_aes_block128(int hi3, int hi2, int lo1, int lo0)
{
- return _mm_set_epi32(highest, high, low, lowest);
+ return _mm_set_epi32(hi3, hi2, lo1, lo0);
}
-AesState aes_block_to_state(AesBlock block)
+AesBlock192 make_aes_block192(int hi5, int hi4, int lo3, int lo2, int lo1, int lo0)
{
- AesState state;
- _mm_storeu_si128((__m128i*) &state.bytes, block);
- return state;
+ AesBlock192 result;
+ result.hi = make_aes_block128( 0, 0, hi5, hi4);
+ result.lo = make_aes_block128(lo3, lo2, lo1, lo0);
+ return result;
}
-AesBlock aes_state_to_block(AesState state)
+AesBlock256 make_aes_block256(int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0)
{
- return _mm_loadu_si128((__m128i*) &state.bytes);
+ AesBlock256 result;
+ result.hi = make_aes_block128(hi7, hi6, hi5, hi4);
+ result.lo = make_aes_block128(lo3, lo2, lo1, lo0);
+ return result;
}
-void print_aes_block(AesBlock block)
+AesBlockString128 format_aes_block128(AesBlock128* block)
+{
+ int i;
+ char *cursor;
+ AesBlockString128 result;
+
+ for (i = 0, cursor = result.str; i < 16; ++i, cursor += 2)
+ sprintf(cursor, "%02x", *((unsigned char*) block + 15 - i));
+
+ *cursor = '\0';
+ return result;
+}
+
+AesBlockString192 format_aes_block192(AesBlock192* block)
+{
+ int i;
+ char *cursor;
+ AesBlockString192 result;
+
+ for (i = 0, cursor = result.str; i < 24; ++i, cursor += 2)
+ sprintf(cursor, "%02x", *((unsigned char*) block + 15 - i));
+
+ *cursor = '\0';
+ return result;
+}
+
+AesBlockString256 format_aes_block256(AesBlock256* block)
+{
+ int i;
+ char *cursor;
+ AesBlockString256 result;
+
+ for (i = 0, cursor = result.str; i < 32; ++i, cursor += 2)
+ sprintf(cursor, "%02x", *((unsigned char*) block + 15 - i));
+
+ *cursor = '\0';
+ return result;
+}
+
+AesBlockString128 format_aes_block128_fips_style(AesBlock128* block)
+{
+ int i;
+ char *cursor;
+ AesBlockString128 result;
+
+ for (i = 0, cursor = result.str; i < 16; ++i, cursor += 2)
+ sprintf(cursor, "%02x", *((unsigned char*) block + i));
+
+ *cursor = '\0';
+ return result;
+}
+
+AesBlockString192 format_aes_block192_fips_style(AesBlock192* block)
+{
+ int i;
+ char *cursor;
+ AesBlockString192 result;
+
+ for (i = 0, cursor = result.str; i < 24; ++i, cursor += 2)
+ sprintf(cursor, "%02x", *((unsigned char*) block + i));
+
+ *cursor = '\0';
+ return result;
+}
+
+AesBlockString256 format_aes_block256_fips_style(AesBlock256* block)
+{
+ int i;
+ char *cursor;
+ AesBlockString256 result;
+
+ for (i = 0, cursor = result.str; i < 32; ++i, cursor += 2)
+ sprintf(cursor, "%02x", *((unsigned char*) block + i));
+
+ *cursor = '\0';
+ return result;
+}
+
+void print_aes_block128(AesBlock128* block)
+{
+ printf("%s\n", format_aes_block128(block).str);
+}
+
+void print_aes_block192(AesBlock192* block)
+{
+ printf("%s\n", format_aes_block192(block).str);
+}
+
+void print_aes_block256(AesBlock256* block)
+{
+ printf("%s\n", format_aes_block256(block).str);
+}
+
+void print_aes_block128_fips_style(AesBlock128* block)
+{
+ printf("%s\n", format_aes_block128_fips_style(block).str);
+}
+
+void print_aes_block192_fips_style(AesBlock192* block)
+{
+ printf("%s\n", format_aes_block192_fips_style(block).str);
+}
+
+void print_aes_block256_fips_style(AesBlock256* block)
+{
+ printf("%s\n", format_aes_block256_fips_style(block).str);
+}
+
+void print_aes_block128_fips_matrix_style(AesBlock128* block)
{
int i, j;
- AesState state = aes_block_to_state(block);
+ __declspec(align(16)) unsigned char bytes[4][4];
+
+ _mm_store_si128((AesBlock128*) bytes, *block);
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 3; ++j)
- printf("%02x ", state.bytes[j][i]);
- printf("%02x\n", state.bytes[3][i]);
+ printf("%02x ", bytes[j][i]);
+ printf("%02x\n", bytes[3][i]);
}
}