aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-05-30 00:38:19 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-05-30 00:38:19 +0300
commitb08c0e5b32c625215bee0ae96a023733d34e9357 (patch)
tree8f809b7f483ba11d2e4e2a235f3e7c9bc9b78ccb
parentmerge aes256{ecb,cbc}.asm (diff)
downloadaes-tools-b08c0e5b32c625215bee0ae96a023733d34e9357.tar.gz
aes-tools-b08c0e5b32c625215bee0ae96a023733d34e9357.zip
support AES-{128,192}-cbc
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/aes128cbc_example.c48
-rw-r--r--examples/aes192cbc_example.c49
-rw-r--r--include/aesni/api.h32
-rw-r--r--include/aesni/raw.h20
-rw-r--r--src/aes128.asm13
-rw-r--r--src/aes192.asm13
-rw-r--r--src/aes256.asm8
-rw-r--r--test/CMakeLists.txt2
-rw-r--r--test/aes128cbc_decrypt_block.c51
-rw-r--r--test/aes128cbc_encrypt_block.c51
-rw-r--r--test/aes192cbc_decrypt_block.c52
-rw-r--r--test/aes192cbc_encrypt_block.c52
13 files changed, 390 insertions, 3 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 5813793..b9bbf37 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -6,6 +6,8 @@ macro(example prefix)
endmacro()
example(aes128ecb)
+example(aes128cbc)
example(aes192ecb)
+example(aes192cbc)
example(aes256ecb)
example(aes256cbc)
diff --git a/examples/aes128cbc_example.c b/examples/aes128cbc_example.c
new file mode 100644
index 0000000..ce568ea
--- /dev/null
+++ b/examples/aes128cbc_example.c
@@ -0,0 +1,48 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include <aesni/all.h>
+
+#include <stdio.h>
+
+int main()
+{
+ __declspec(align(16)) AesBlock128 plain, key, cypher, decrypted, iv;
+
+ plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
+ key = make_aes_block128(0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
+ iv = make_aes_block128(0xfedcba98, 0x76543210, 0xfedcba98, 0x76543210);
+
+ 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("\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("\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);
+
+ cypher = aes128cbc_encrypt(plain, key, &iv);
+ 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);
+
+ decrypted = aes128cbc_decrypt(cypher, key, &iv);
+ 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/aes192cbc_example.c b/examples/aes192cbc_example.c
new file mode 100644
index 0000000..25719c8
--- /dev/null
+++ b/examples/aes192cbc_example.c
@@ -0,0 +1,49 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include <aesni/all.h>
+
+#include <stdio.h>
+
+int main()
+{
+ __declspec(align(16)) AesBlock128 plain, cypher, decrypted, iv;
+ __declspec(align(16)) AesBlock192 key;
+
+ plain = make_aes_block128(0xffeeddcc, 0xbbaa9988, 0x77665544, 0x33221100);
+ key = make_aes_block192(0x17161514, 0x13121110, 0x0f0e0d0c, 0x0b0a0908, 0x07060504, 0x03020100);
+ iv = make_aes_block128(0xfedcba98, 0x76543210, 0xfedcba98, 0x76543210);
+
+ 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("\n");
+ printf("Key: %s\n", format_aes_block192(&key).str);
+ printf(" %s\n", format_aes_block192_fips_style(&key).str);
+ print_aes_block192_fips_matrix_style(&key);
+
+ 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);
+
+ cypher = aes192cbc_encrypt(plain, &key, &iv);
+ 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);
+
+ decrypted = aes192cbc_decrypt(cypher, &key, &iv);
+ 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/api.h b/include/aesni/api.h
index 64d169c..d64b225 100644
--- a/include/aesni/api.h
+++ b/include/aesni/api.h
@@ -25,6 +25,22 @@ static __inline AesBlock128 __fastcall aes128ecb_decrypt(
return raw_aes128ecb_decrypt(cypher, key);
}
+static __inline AesBlock128 __fastcall aes128cbc_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key,
+ AesBlock128* initialization_vector)
+{
+ return raw_aes128cbc_encrypt(plain, key, initialization_vector);
+}
+
+static __inline AesBlock128 __fastcall aes128cbc_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key,
+ AesBlock128* initialization_vector)
+{
+ return raw_aes128cbc_decrypt(cypher, key, initialization_vector);
+}
+
static __inline AesBlock128 __fastcall aes192ecb_encrypt(
AesBlock128 plain,
AesBlock192* key)
@@ -39,6 +55,22 @@ static __inline AesBlock128 __fastcall aes192ecb_decrypt(
return raw_aes192ecb_decrypt(cypher, key->lo, key->hi);
}
+static __inline AesBlock128 __fastcall aes192cbc_encrypt(
+ AesBlock128 plain,
+ AesBlock192* key,
+ AesBlock128* initialization_vector)
+{
+ return raw_aes192cbc_encrypt(plain, key->lo, key->hi, initialization_vector);
+}
+
+static __inline AesBlock128 __fastcall aes192cbc_decrypt(
+ AesBlock128 cypher,
+ AesBlock192* key,
+ AesBlock128* initialization_vector)
+{
+ return raw_aes192cbc_decrypt(cypher, key->lo, key->hi, initialization_vector);
+}
+
static __inline AesBlock128 __fastcall aes256ecb_encrypt(
AesBlock128 plain,
AesBlock256* key)
diff --git a/include/aesni/raw.h b/include/aesni/raw.h
index d648cac..ffda8d2 100644
--- a/include/aesni/raw.h
+++ b/include/aesni/raw.h
@@ -17,6 +17,15 @@ AesBlock128 __fastcall raw_aes128ecb_decrypt(
AesBlock128 cypher,
AesBlock128 key);
+AesBlock128 __fastcall raw_aes128cbc_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key,
+ AesBlock128* iv);
+AesBlock128 __fastcall raw_aes128cbc_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key,
+ AesBlock128* iv);
+
AesBlock128 __fastcall raw_aes192ecb_encrypt(
AesBlock128 plain,
AesBlock128 key_lo,
@@ -26,6 +35,17 @@ AesBlock128 __fastcall raw_aes192ecb_decrypt(
AesBlock128 key_lo,
AesBlock128 key_hi);
+AesBlock128 __fastcall raw_aes192cbc_encrypt(
+ AesBlock128 plain,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi,
+ AesBlock128 *iv);
+AesBlock128 __fastcall raw_aes192cbc_decrypt(
+ AesBlock128 cypher,
+ AesBlock128 key_lo,
+ AesBlock128 key_hi,
+ AesBlock128 *iv);
+
AesBlock128 __fastcall raw_aes256ecb_encrypt(
AesBlock128 plain,
AesBlock128 key_lo,
diff --git a/src/aes128.asm b/src/aes128.asm
index 06a0275..5d62621 100644
--- a/src/aes128.asm
+++ b/src/aes128.asm
@@ -32,6 +32,11 @@ inverted_key_schedule oword 11 dup(0)
ret
@raw_aes128ecb_encrypt@32 endp
+@raw_aes128cbc_encrypt@36 proc
+ pxor xmm0, [ecx]
+ jmp @raw_aes128ecb_encrypt@32
+@raw_aes128cbc_encrypt@36 endp
+
@raw_aes128ecb_decrypt@32 proc
call expand_keys128
pxor xmm0, [inverted_key_schedule]
@@ -48,6 +53,14 @@ inverted_key_schedule oword 11 dup(0)
ret
@raw_aes128ecb_decrypt@32 endp
+@raw_aes128cbc_decrypt@36 proc
+ push ecx
+ call @raw_aes128ecb_decrypt@32
+ pop ecx
+ pxor xmm0, [ecx]
+ ret
+@raw_aes128cbc_decrypt@36 endp
+
expand_keys128 proc
; A "word" (in terms of the FIPS 187 standard) is a 32-bit block.
; Words are denoted by `w[N]`.
diff --git a/src/aes192.asm b/src/aes192.asm
index 9d26dca..55fd540 100644
--- a/src/aes192.asm
+++ b/src/aes192.asm
@@ -34,6 +34,11 @@ inverted_key_schedule oword 13 dup(0)
ret
@raw_aes192ecb_encrypt@48 endp
+@raw_aes192cbc_encrypt@52 proc
+ pxor xmm0, [ecx]
+ jmp @raw_aes192ecb_encrypt@48
+@raw_aes192cbc_encrypt@52 endp
+
@raw_aes192ecb_decrypt@48 proc
call expand_keys192
pxor xmm0, [inverted_key_schedule]
@@ -52,6 +57,14 @@ inverted_key_schedule oword 13 dup(0)
ret
@raw_aes192ecb_decrypt@48 endp
+@raw_aes192cbc_decrypt@52 proc
+ push ecx
+ call @raw_aes192ecb_decrypt@48
+ pop ecx
+ pxor xmm0, [ecx]
+ ret
+@raw_aes192cbc_decrypt@52 endp
+
expand_keys192 proc
; A "word" (in terms of the FIPS 187 standard) is a 32-bit block.
; Words are denoted by `w[N]`.
diff --git a/src/aes256.asm b/src/aes256.asm
index 56dbd05..acea8e3 100644
--- a/src/aes256.asm
+++ b/src/aes256.asm
@@ -62,7 +62,9 @@ inverse_key_schedule oword 15 dup(0)
@raw_aes256ecb_decrypt@48 endp
@raw_aes256cbc_decrypt@52 proc
+ push ecx
call @raw_aes256ecb_decrypt@48
+ pop ecx
pxor xmm0, [ecx]
ret
@raw_aes256cbc_decrypt@52 endp
@@ -138,7 +140,7 @@ expand_keys256 proc
movdqa [key_schedule], xmm1 ; sets w[0], w[1], w[2], w[3]
movdqa [key_schedule + 10h], xmm2 ; sets w[4], w[5], w[6], w[7]
- lea edx, [key_schedule + 20h] ; ecx = &w[8]
+ lea ecx, [key_schedule + 20h] ; ecx = &w[8]
aeskeygenassist xmm7, xmm2, 1h ; xmm7[127:96] = RotWord(SubWord(w[7]))^Rcon
pshufd xmm7, xmm7, 0FFh ; xmm7[95:64] = xmm7[63:32] = xmm7[31:0] = xmm7[127:96]
@@ -251,11 +253,11 @@ gen_round_key:
; xmm1[31:0] == w[i+8] == HWGEN^w[i]
; Set w[i+8], w[i+9], w[i+10] and w[i+11].
- movdqa [edx], xmm1 ; w[i+8] = HWGEN^w[i]
+ movdqa [ecx], xmm1 ; w[i+8] = HWGEN^w[i]
; w[i+9] = HWGEN^w[i+1]^w[i]
; w[i+10] = HWGEN^w[i+2]^w[i+1]^w[i]
; w[i+11] = HWGEN^w[i+3]^w[i+2]^w[i+1]^w[i]
- add edx, 10h ; ecx = &w[i+12]
+ add ecx, 10h ; ecx = &w[i+12]
; Swap the values in xmm1 and xmm2.
pxor xmm1, xmm2
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index cba5d79..40fc20c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -9,6 +9,8 @@ macro(test prefix)
endmacro()
test(aes128ecb)
+test(aes128cbc)
test(aes192ecb)
+test(aes192cbc)
test(aes256ecb)
test(aes256cbc)
diff --git a/test/aes128cbc_decrypt_block.c b/test/aes128cbc_decrypt_block.c
new file mode 100644
index 0000000..1a77cdf
--- /dev/null
+++ b/test/aes128cbc_decrypt_block.c
@@ -0,0 +1,51 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include <aesni/all.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static void exit_with_usage(const char* argv0)
+{
+ printf("Usage: %s KEY INIT_VECTOR [CIPHER...]\n", argv0);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+ __declspec(align(16)) AesBlock128 plain, key, cipher, iv;
+
+ if (argc < 3)
+ exit_with_usage(argv[0]);
+
+ if (parse_aes_block128(&key, argv[1]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]);
+ exit_with_usage(argv[0]);
+ }
+
+ if (parse_aes_block128(&iv, argv[2]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[2]);
+ exit_with_usage(argv[0]);
+ }
+
+ for (int i = 3; i < argc; ++i)
+ {
+ if (parse_aes_block128(&cipher, argv[i]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]);
+ continue;
+ }
+ plain = aes128cbc_decrypt(cipher, key, &iv);
+ print_aes_block128(&plain);
+ }
+
+ return 0;
+}
diff --git a/test/aes128cbc_encrypt_block.c b/test/aes128cbc_encrypt_block.c
new file mode 100644
index 0000000..31bb479
--- /dev/null
+++ b/test/aes128cbc_encrypt_block.c
@@ -0,0 +1,51 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include <aesni/all.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static void exit_with_usage(const char* argv0)
+{
+ printf("Usage: %s KEY INIT_VECTOR [PLAIN...]\n", argv0);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+ __declspec(align(16)) AesBlock128 plain, key, cipher, iv;
+
+ if (argc < 3)
+ exit_with_usage(argv[0]);
+
+ if (parse_aes_block128(&key, argv[1]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[1]);
+ exit_with_usage(argv[0]);
+ }
+
+ if (parse_aes_block128(&iv, argv[2]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[2]);
+ exit_with_usage(argv[0]);
+ }
+
+ for (int i = 3; i < argc; ++i)
+ {
+ if (parse_aes_block128(&plain, argv[i]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]);
+ continue;
+ }
+ cipher = aes128cbc_encrypt(plain, key, &iv);
+ print_aes_block128(&cipher);
+ }
+
+ return 0;
+}
diff --git a/test/aes192cbc_decrypt_block.c b/test/aes192cbc_decrypt_block.c
new file mode 100644
index 0000000..0fddf43
--- /dev/null
+++ b/test/aes192cbc_decrypt_block.c
@@ -0,0 +1,52 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include <aesni/all.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static void exit_with_usage(const char* argv0)
+{
+ printf("Usage: %s KEY INIT_VECTOR [CIPHER...]\n", argv0);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+ __declspec(align(16)) AesBlock128 plain, cipher, iv;
+ __declspec(align(16)) AesBlock192 key;
+
+ if (argc < 3)
+ exit_with_usage(argv[0]);
+
+ if (parse_aes_block192(&key, argv[1]) != 0)
+ {
+ fprintf(stderr, "Invalid 192-bit AES block '%s'\n", argv[1]);
+ exit_with_usage(argv[0]);
+ }
+
+ if (parse_aes_block128(&iv, argv[2]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[2]);
+ exit_with_usage(argv[0]);
+ }
+
+ for (int i = 3; i < argc; ++i)
+ {
+ if (parse_aes_block128(&cipher, argv[i]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]);
+ continue;
+ }
+ plain = aes192cbc_decrypt(cipher, &key, &iv);
+ print_aes_block128(&plain);
+ }
+
+ return 0;
+}
diff --git a/test/aes192cbc_encrypt_block.c b/test/aes192cbc_encrypt_block.c
new file mode 100644
index 0000000..069c408
--- /dev/null
+++ b/test/aes192cbc_encrypt_block.c
@@ -0,0 +1,52 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include <aesni/all.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static void exit_with_usage(const char* argv0)
+{
+ printf("Usage: %s KEY INIT_VECTOR [PLAIN...]\n", argv0);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+ __declspec(align(16)) AesBlock128 plain, cipher, iv;
+ __declspec(align(16)) AesBlock192 key;
+
+ if (argc < 3)
+ exit_with_usage(argv[0]);
+
+ if (parse_aes_block192(&key, argv[1]) != 0)
+ {
+ fprintf(stderr, "Invalid 192-bit AES block '%s'\n", argv[1]);
+ exit_with_usage(argv[0]);
+ }
+
+ if (parse_aes_block128(&iv, argv[2]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[2]);
+ exit_with_usage(argv[0]);
+ }
+
+ for (int i = 3; i < argc; ++i)
+ {
+ if (parse_aes_block128(&plain, argv[i]) != 0)
+ {
+ fprintf(stderr, "Invalid 128-bit AES block '%s'\n", argv[i]);
+ continue;
+ }
+ cipher = aes192cbc_encrypt(plain, &key, &iv);
+ print_aes_block128(&cipher);
+ }
+
+ return 0;
+}