aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aes/include
diff options
context:
space:
mode:
Diffstat (limited to 'aes/include')
-rw-r--r--aes/include/aes/aes.h541
-rw-r--r--aes/include/aes/algorithm.h23
-rw-r--r--aes/include/aes/all.h21
-rw-r--r--aes/include/aes/box.h81
-rw-r--r--aes/include/aes/box_aes.h21
-rw-r--r--aes/include/aes/box_data.h153
-rw-r--r--aes/include/aes/data.h122
-rw-r--r--aes/include/aes/error.h96
-rw-r--r--aes/include/aes/mode.h166
-rw-r--r--aes/include/aes/padding.h38
-rw-r--r--aes/include/aes/workarounds.h16
11 files changed, 1278 insertions, 0 deletions
diff --git a/aes/include/aes/aes.h b/aes/include/aes/aes.h
new file mode 100644
index 0000000..1108235
--- /dev/null
+++ b/aes/include/aes/aes.h
@@ -0,0 +1,541 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "data.h"
+#include "error.h"
+#include "mode.h"
+
+#include <assert.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef AES_Block128 AES_AES_Block;
+typedef AES_AES_Block AES_AES128_Block;
+typedef AES_AES_Block AES_AES192_Block;
+typedef AES_AES_Block AES_AES256_Block;
+
+typedef struct
+{
+ AES_AES_Block key;
+}
+AES_AES128_Key;
+
+typedef struct
+{
+ AES_AES_Block hi;
+ AES_AES_Block lo;
+}
+AES_AES192_Key;
+
+typedef struct
+{
+ AES_AES_Block hi;
+ AES_AES_Block lo;
+}
+AES_AES256_Key;
+
+static __inline AES_AES_Block aes_AES_make_block(
+ int hi3, int hi2, int lo1, int lo0)
+{
+ return aes_make_block128(hi3, hi2, lo1, lo0);
+}
+
+static __inline AES_AES128_Block aes_AES128_make_block(
+ int hi3, int hi2, int lo1, int lo0)
+{
+ return aes_AES_make_block(hi3, hi2, lo1, lo0);
+}
+
+static __inline AES_AES192_Block aes_AES192_make_block(
+ int hi3, int hi2, int lo1, int lo0)
+{
+ return aes_AES_make_block(hi3, hi2, lo1, lo0);
+}
+
+static __inline AES_AES256_Block aes_AES256_make_block(
+ int hi3, int hi2, int lo1, int lo0)
+{
+ return aes_AES_make_block(hi3, hi2, lo1, lo0);
+}
+
+static __inline AES_AES128_Key aes_AES128_make_key(
+ int hi3, int hi2, int lo1, int lo0)
+{
+ AES_AES128_Key key;
+ key.key = aes_AES_make_block(hi3, hi2, lo1, lo0);
+ return key;
+}
+
+static __inline AES_AES192_Key aes_AES192_make_key(
+ int hi5, int hi4,
+ int lo3, int lo2, int lo1, int lo0)
+{
+ AES_AES192_Key key;
+ key.hi = aes_AES_make_block( 0, 0, hi5, hi4);
+ key.lo = aes_AES_make_block(lo3, lo2, lo1, lo0);
+ return key;
+}
+
+static __inline AES_AES256_Key aes_AES256_make_key(
+ int hi7, int hi6, int hi5, int hi4,
+ int lo3, int lo2, int lo1, int lo0)
+{
+ AES_AES256_Key key;
+ key.hi = aes_AES_make_block(hi7, hi6, hi5, hi4);
+ key.lo = aes_AES_make_block(lo3, lo2, lo1, lo0);
+ return key;
+}
+
+typedef struct { char str[33]; } AES_AES_BlockString;
+typedef AES_AES_BlockString AES_AES128_BlockString;
+typedef AES_AES_BlockString AES_AES192_BlockString;
+typedef AES_AES_BlockString AES_AES256_BlockString;
+
+typedef struct { char str[49]; } AES_AES_BlockMatrixString;
+typedef AES_AES_BlockMatrixString AES_AES128_BlockMatrixString;
+typedef AES_AES_BlockMatrixString AES_AES192_BlockMatrixString;
+typedef AES_AES_BlockMatrixString AES_AES256_BlockMatrixString;
+
+AES_StatusCode aes_AES_format_block(
+ AES_AES_BlockString*,
+ const AES_AES_Block*,
+ AES_ErrorDetails*);
+
+static __inline AES_StatusCode aes_AES128_format_block(
+ AES_AES128_BlockString* dest,
+ const AES_AES128_Block* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_format_block(dest, src, err_details);
+}
+
+static __inline AES_StatusCode aes_AES192_format_block(
+ AES_AES192_BlockString* dest,
+ const AES_AES192_Block* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_format_block(dest, src, err_details);
+}
+
+static __inline AES_StatusCode aes_AES256_format_block(
+ AES_AES256_BlockString* dest,
+ const AES_AES256_Block* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_format_block(dest, src, err_details);
+}
+
+AES_StatusCode aes_AES_format_block_as_matrix(
+ AES_AES_BlockMatrixString*,
+ const AES_AES_Block*,
+ AES_ErrorDetails*);
+
+static __inline AES_StatusCode aes_AES128_format_block_as_matrix(
+ AES_AES128_BlockMatrixString* dest,
+ const AES_AES128_Block* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_format_block_as_matrix(dest, src, err_details);
+}
+
+static __inline AES_StatusCode aes_AES192_format_block_as_matrix(
+ AES_AES192_BlockMatrixString* dest,
+ const AES_AES192_Block* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_format_block_as_matrix(dest, src, err_details);
+}
+
+static __inline AES_StatusCode aes_AES256_format_block_as_matrix(
+ AES_AES256_BlockMatrixString* dest,
+ const AES_AES256_Block* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_format_block_as_matrix(dest, src, err_details);
+}
+
+AES_StatusCode aes_AES_print_block(
+ const AES_AES_Block*,
+ AES_ErrorDetails*);
+
+static __inline AES_StatusCode aes_AES128_print_block(
+ const AES_AES128_Block* block,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_print_block(block, err_details);
+}
+
+static __inline AES_StatusCode aes_AES192_print_block(
+ const AES_AES192_Block* block,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_print_block(block, err_details);
+}
+
+static __inline AES_StatusCode aes_AES256_print_block(
+ const AES_AES256_Block* block,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_print_block(block, err_details);
+}
+
+AES_StatusCode aes_AES_print_block_as_matrix(
+ const AES_AES_Block*,
+ AES_ErrorDetails*);
+
+static __inline AES_StatusCode aes_AES128_print_block_as_matrix(
+ const AES_AES128_Block* block,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_print_block_as_matrix(block, err_details);
+}
+
+static __inline AES_StatusCode aes_AES192_print_block_as_matrix(
+ const AES_AES192_Block* block,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_print_block_as_matrix(block, err_details);
+}
+
+static __inline AES_StatusCode aes_AES256_print_block_as_matrix(
+ const AES_AES256_Block* block,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_print_block_as_matrix(block, err_details);
+}
+
+AES_StatusCode aes_AES_parse_block(
+ AES_AES_Block* dest,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+static __inline AES_StatusCode aes_AES128_parse_block(
+ AES_AES128_Block* dest,
+ const char* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_parse_block(dest, src, err_details);
+}
+
+static __inline AES_StatusCode aes_AES192_parse_block(
+ AES_AES192_Block* dest,
+ const char* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_parse_block(dest, src, err_details);
+}
+
+static __inline AES_StatusCode aes_AES256_parse_block(
+ AES_AES256_Block* dest,
+ const char* src,
+ AES_ErrorDetails* err_details)
+{
+ return aes_AES_parse_block(dest, src, err_details);
+}
+
+typedef struct { char str[33]; } AES_AES128_KeyString;
+typedef struct { char str[49]; } AES_AES192_KeyString;
+typedef struct { char str[65]; } AES_AES256_KeyString;
+
+AES_StatusCode aes_AES128_format_key(
+ AES_AES128_KeyString*,
+ const AES_AES128_Key*,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_AES192_format_key(
+ AES_AES192_KeyString*,
+ const AES_AES192_Key*,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_AES256_format_key(
+ AES_AES256_KeyString*,
+ const AES_AES256_Key*,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_AES128_print_key(
+ const AES_AES128_Key*,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_AES192_print_key(
+ const AES_AES192_Key*,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_AES256_print_key(
+ const AES_AES256_Key*,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_AES128_parse_key(
+ AES_AES128_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_AES192_parse_key(
+ AES_AES192_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_AES256_parse_key(
+ AES_AES256_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+typedef struct
+{
+ AES_AES_Block keys[11];
+}
+AES_AES128_RoundKeys;
+
+typedef struct
+{
+ AES_AES_Block keys[13];
+}
+AES_AES192_RoundKeys;
+
+typedef struct
+{
+ AES_AES_Block keys[15];
+}
+AES_AES256_RoundKeys;
+
+void __fastcall aes_AES128_expand_key_(
+ AES_AES_Block key,
+ AES_AES128_RoundKeys* encryption_keys);
+
+void __fastcall aes_AES192_expand_key_(
+ AES_AES_Block key_lo,
+ AES_AES_Block key_hi,
+ AES_AES192_RoundKeys* encryption_keys);
+
+void __fastcall aes_AES256_expand_key_(
+ AES_AES_Block key_lo,
+ AES_AES_Block key_hi,
+ AES_AES256_RoundKeys* encryption_keys);
+
+void __fastcall aes_AES128_derive_decryption_keys_(
+ const AES_AES128_RoundKeys* encryption_keys,
+ AES_AES128_RoundKeys* decryption_keys);
+
+void __fastcall aes_AES192_derive_decryption_keys_(
+ const AES_AES192_RoundKeys* encryption_keys,
+ AES_AES192_RoundKeys* decryption_keys);
+
+void __fastcall aes_AES256_derive_decryption_keys_(
+ const AES_AES256_RoundKeys* encryption_keys,
+ AES_AES256_RoundKeys* decryption_keys);
+
+AES_AES_Block __fastcall aes_AES128_encrypt_block_(
+ AES_AES_Block plaintext,
+ const AES_AES128_RoundKeys*);
+
+AES_AES_Block __fastcall aes_AES192_encrypt_block_(
+ AES_AES_Block plaintext,
+ const AES_AES192_RoundKeys*);
+
+AES_AES_Block __fastcall aes_AES256_encrypt_block_(
+ AES_AES_Block plaintext,
+ const AES_AES256_RoundKeys*);
+
+AES_AES_Block __fastcall aes_AES128_decrypt_block_(
+ AES_AES_Block ciphertext,
+ const AES_AES128_RoundKeys*);
+
+AES_AES_Block __fastcall aes_AES192_decrypt_block_(
+ AES_AES_Block ciphertext,
+ const AES_AES192_RoundKeys*);
+
+AES_AES_Block __fastcall aes_AES256_decrypt_block_(
+ AES_AES_Block ciphertext,
+ const AES_AES256_RoundKeys*);
+
+static __inline AES_AES_Block __fastcall aes_AES_xor_blocks(
+ AES_AES_Block a,
+ AES_AES_Block b)
+{
+ return aes_xor_block128(a, b);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES128_xor_blocks(
+ AES_AES128_Block a,
+ AES_AES128_Block b)
+{
+ return aes_AES_xor_blocks(a, b);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES192_xor_blocks(
+ AES_AES192_Block a,
+ AES_AES192_Block b)
+{
+ return aes_AES_xor_blocks(a, b);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES256_xor_blocks(
+ AES_AES256_Block a,
+ AES_AES256_Block b)
+{
+ return aes_AES_xor_blocks(a, b);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES_inc_block(
+ AES_AES_Block block)
+{
+ block = aes_reverse_byte_order_block128(block);
+ block = aes_inc_block128(block);
+ return aes_reverse_byte_order_block128(block);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES128_inc_block(
+ AES_AES128_Block block)
+{
+ return aes_AES_inc_block(block);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES192_inc_block(
+ AES_AES192_Block block)
+{
+ return aes_AES_inc_block(block);
+}
+
+static __inline AES_AES_Block __fastcall aes_AES256_inc_block(
+ AES_AES256_Block block)
+{
+ return aes_AES_inc_block(block);
+}
+
+AES_ENCRYPT_BLOCK_ECB(AES128);
+AES_DECRYPT_BLOCK_ECB(AES128);
+AES_ENCRYPT_BLOCK_CBC(AES128);
+AES_DECRYPT_BLOCK_CBC(AES128);
+AES_ENCRYPT_BLOCK_CFB(AES128);
+AES_DECRYPT_BLOCK_CFB(AES128);
+AES_ENCRYPT_BLOCK_OFB(AES128);
+AES_DECRYPT_BLOCK_OFB(AES128);
+AES_ENCRYPT_BLOCK_CTR(AES128);
+AES_DECRYPT_BLOCK_CTR(AES128);
+
+AES_ENCRYPT_BLOCK_ECB(AES192);
+AES_DECRYPT_BLOCK_ECB(AES192);
+AES_ENCRYPT_BLOCK_CBC(AES192);
+AES_DECRYPT_BLOCK_CBC(AES192);
+AES_ENCRYPT_BLOCK_CFB(AES192);
+AES_DECRYPT_BLOCK_CFB(AES192);
+AES_ENCRYPT_BLOCK_OFB(AES192);
+AES_DECRYPT_BLOCK_OFB(AES192);
+AES_ENCRYPT_BLOCK_CTR(AES192);
+AES_DECRYPT_BLOCK_CTR(AES192);
+
+AES_ENCRYPT_BLOCK_ECB(AES256);
+AES_DECRYPT_BLOCK_ECB(AES256);
+AES_ENCRYPT_BLOCK_CBC(AES256);
+AES_DECRYPT_BLOCK_CBC(AES256);
+AES_ENCRYPT_BLOCK_CFB(AES256);
+AES_DECRYPT_BLOCK_CFB(AES256);
+AES_ENCRYPT_BLOCK_OFB(AES256);
+AES_DECRYPT_BLOCK_OFB(AES256);
+AES_ENCRYPT_BLOCK_CTR(AES256);
+AES_DECRYPT_BLOCK_CTR(AES256);
+
+/**
+ * \brief Expands an AES-128 key into 10 encryption round keys.
+ *
+ * \param[in] key The AES-128 key.
+ * \param[out] encryption_keys The AES-128 encryption round keys. Must not be `NULL`.
+ */
+static __inline void __fastcall aes_AES128_expand_key(
+ const AES_AES128_Key* key,
+ AES_AES128_RoundKeys* encryption_keys)
+{
+ assert(encryption_keys);
+
+ aes_AES128_expand_key_(key->key, encryption_keys);
+}
+
+/**
+ * \brief Derives AES-128 decryption round keys from AES-128 encryption round keys.
+ *
+ * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`.
+ * \param[out] decryption_keys The AES-128 decryption round keys. Must not be `NULL`.
+ */
+static __inline void __fastcall aes_AES128_derive_decryption_keys(
+ const AES_AES128_RoundKeys* encryption_keys,
+ AES_AES128_RoundKeys* decryption_keys)
+{
+ assert(encryption_keys);
+ assert(decryption_keys);
+
+ aes_AES128_derive_decryption_keys_(encryption_keys, decryption_keys);
+}
+
+/**
+ * \brief Expands an AES-192 key into 12 encryption round keys.
+ *
+ * \param[in] key The AES-192 key.
+ * \param[out] encryption_keys The AES-192 encryption round keys. Must not be `NULL`.
+ */
+static __inline void __fastcall aes_AES192_expand_key(
+ const AES_AES192_Key* key,
+ AES_AES192_RoundKeys* encryption_keys)
+{
+ assert(key);
+ assert(encryption_keys);
+
+ aes_AES192_expand_key_(key->lo, key->hi, encryption_keys);
+}
+
+/**
+ * \brief Derives AES-192 decryption round keys from AES-192 encryption round keys.
+ *
+ * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`.
+ * \param[out] decryption_keys The AES-192 decryption round keys. Must not be `NULL`.
+ */
+static __inline void __fastcall aes_AES192_derive_decryption_keys(
+ const AES_AES192_RoundKeys* encryption_keys,
+ AES_AES192_RoundKeys* decryption_keys)
+{
+ assert(encryption_keys);
+ assert(decryption_keys);
+
+ aes_AES192_derive_decryption_keys_(encryption_keys, decryption_keys);
+}
+
+/**
+ * \brief Expands an AES-256 key into 14 encryption round keys.
+ *
+ * \param[in] key The AES-256 key.
+ * \param[out] encryption_keys The AES-256 encryption round keys. Must not be `NULL`.
+ */
+static __inline void __fastcall aes_AES256_expand_key(
+ const AES_AES256_Key* key,
+ AES_AES256_RoundKeys* encryption_keys)
+{
+ assert(key);
+ assert(encryption_keys);
+
+ aes_AES256_expand_key_(key->lo, key->hi, encryption_keys);
+}
+
+/**
+ * \brief Derives AES-256 decryption round keys from AES-256 encryption round keys.
+ *
+ * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`.
+ * \param[out] decryption_keys The AES-256 decryption round keys. Must not be `NULL`.
+ */
+static __inline void __fastcall aes_AES256_derive_decryption_keys(
+ const AES_AES256_RoundKeys* encryption_keys,
+ AES_AES256_RoundKeys* decryption_keys)
+{
+ assert(encryption_keys);
+ assert(decryption_keys);
+
+ aes_AES256_derive_decryption_keys_(encryption_keys, decryption_keys);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/algorithm.h b/aes/include/aes/algorithm.h
new file mode 100644
index 0000000..d88cf59
--- /dev/null
+++ b/aes/include/aes/algorithm.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef enum
+{
+ AES_AES128,
+ AES_AES192,
+ AES_AES256,
+}
+AES_Algorithm;
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/all.h b/aes/include/aes/all.h
new file mode 100644
index 0000000..ff27c18
--- /dev/null
+++ b/aes/include/aes/all.h
@@ -0,0 +1,21 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+/**
+ * \defgroup aes AES
+ */
+
+#include "aes.h"
+#include "algorithm.h"
+#include "box.h"
+#include "box_aes.h"
+#include "box_data.h"
+#include "data.h"
+#include "error.h"
+#include "mode.h"
+#include "padding.h"
+#include "workarounds.h"
diff --git a/aes/include/aes/box.h b/aes/include/aes/box.h
new file mode 100644
index 0000000..2051d3d
--- /dev/null
+++ b/aes/include/aes/box.h
@@ -0,0 +1,81 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "algorithm.h"
+#include "box_data.h"
+#include "error.h"
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+AES_StatusCode aes_box_init(
+ AES_Box* box,
+ AES_Algorithm algorithm,
+ const AES_BoxKey* box_key,
+ AES_Mode mode,
+ const AES_BoxBlock* iv,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_parse_key(
+ AES_BoxKey* dest,
+ AES_Algorithm algorithm,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_parse_block(
+ AES_BoxBlock* dest,
+ AES_Algorithm algorithm,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_format_key(
+ AES_BoxKeyString* dest,
+ AES_Algorithm algorithm,
+ const AES_BoxKey* src,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_format_block(
+ AES_BoxBlockString* dest,
+ AES_Algorithm algorithm,
+ const AES_BoxBlock* src,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_encrypt_block(
+ AES_Box* box,
+ const AES_BoxBlock* plaintext,
+ AES_BoxBlock* ciphertext,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_decrypt_block(
+ AES_Box* box,
+ const AES_BoxBlock* ciphertext,
+ AES_BoxBlock* plaintext,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_encrypt_buffer(
+ AES_Box* box,
+ const void* src,
+ size_t src_size,
+ void* dest,
+ size_t* dest_size,
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_box_decrypt_buffer(
+ AES_Box* box,
+ const void* src,
+ size_t src_size,
+ void* dest,
+ size_t* dest_size,
+ AES_ErrorDetails* err_details);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/box_aes.h b/aes/include/aes/box_aes.h
new file mode 100644
index 0000000..c92d883
--- /dev/null
+++ b/aes/include/aes/box_aes.h
@@ -0,0 +1,21 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "box_data.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+extern AES_BoxAlgorithmInterface aes_box_algorithm_aes128;
+extern AES_BoxAlgorithmInterface aes_box_algorithm_aes192;
+extern AES_BoxAlgorithmInterface aes_box_algorithm_aes256;
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/box_data.h b/aes/include/aes/box_data.h
new file mode 100644
index 0000000..e2315eb
--- /dev/null
+++ b/aes/include/aes/box_data.h
@@ -0,0 +1,153 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "aes.h"
+#include "error.h"
+#include "mode.h"
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef union
+{
+ AES_AES128_Key aes128_key;
+ AES_AES192_Key aes192_key;
+ AES_AES256_Key aes256_key;
+}
+AES_BoxKey;
+
+typedef union
+{
+ AES_AES128_RoundKeys aes128_encryption_keys;
+ AES_AES192_RoundKeys aes192_encryption_keys;
+ AES_AES256_RoundKeys aes256_encryption_keys;
+}
+AES_BoxEncryptionRoundKeys;
+
+typedef union
+{
+ AES_AES128_RoundKeys aes128_decryption_keys;
+ AES_AES192_RoundKeys aes192_decryption_keys;
+ AES_AES256_RoundKeys aes256_decryption_keys;
+}
+AES_BoxDecryptionRoundKeys;
+
+typedef union
+{
+ AES_AES128_KeyString aes128;
+ AES_AES192_KeyString aes192;
+ AES_AES256_KeyString aes256;
+}
+AES_BoxKeyString;
+
+typedef union
+{
+ AES_AES_Block aes_block;
+}
+AES_BoxBlock;
+
+typedef union
+{
+ AES_AES_BlockString aes;
+}
+AES_BoxBlockString;
+
+typedef AES_StatusCode (*AES_BoxCalculateRoundKeys)(
+ const AES_BoxKey* params,
+ AES_BoxEncryptionRoundKeys*,
+ AES_BoxDecryptionRoundKeys*,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxParseBlock)(
+ AES_BoxBlock* dest,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxParseKey)(
+ AES_BoxKey* dest,
+ const char* src,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxFormatBlock)(
+ AES_BoxBlockString* dest,
+ const AES_BoxBlock* src,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxFormatKey)(
+ AES_BoxKeyString* dest,
+ const AES_BoxKey* src,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxEncryptBlock)(
+ const AES_BoxBlock* plaintext,
+ const AES_BoxEncryptionRoundKeys* params,
+ AES_BoxBlock* ciphertext,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxDecryptBlock)(
+ const AES_BoxBlock* ciphertext,
+ const AES_BoxDecryptionRoundKeys* params,
+ AES_BoxBlock* plaintext,
+ AES_ErrorDetails* err_details);
+
+typedef AES_StatusCode (*AES_BoxXorBlock)(
+ AES_BoxBlock*,
+ const AES_BoxBlock*,
+ AES_ErrorDetails*);
+
+typedef AES_StatusCode (*AES_BoxIncBlock)(
+ AES_BoxBlock*,
+ AES_ErrorDetails*);
+
+typedef AES_StatusCode (*AES_BoxGetBlockSize)(
+ size_t*,
+ AES_ErrorDetails*);
+
+typedef AES_StatusCode (*AES_BoxStoreBlock)(
+ void*,
+ const AES_BoxBlock*,
+ AES_ErrorDetails*);
+
+typedef AES_StatusCode (*AES_BoxLoadBlock)(
+ AES_BoxBlock*,
+ const void*,
+ AES_ErrorDetails*);
+
+typedef struct
+{
+ AES_BoxCalculateRoundKeys calc_round_keys;
+ AES_BoxParseBlock parse_block;
+ AES_BoxParseKey parse_key;
+ AES_BoxFormatBlock format_block;
+ AES_BoxFormatKey format_key;
+ AES_BoxEncryptBlock encrypt_block;
+ AES_BoxDecryptBlock decrypt_block;
+ AES_BoxXorBlock xor_block;
+ AES_BoxIncBlock inc_block;
+ AES_BoxGetBlockSize get_block_size;
+ AES_BoxStoreBlock store_block;
+ AES_BoxLoadBlock load_block;
+}
+AES_BoxAlgorithmInterface;
+
+typedef struct
+{
+ const AES_BoxAlgorithmInterface* algorithm;
+ AES_BoxEncryptionRoundKeys encryption_keys;
+ AES_BoxDecryptionRoundKeys decryption_keys;
+ AES_Mode mode;
+ AES_BoxBlock iv;
+}
+AES_Box;
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/data.h b/aes/include/aes/data.h
new file mode 100644
index 0000000..a441939
--- /dev/null
+++ b/aes/include/aes/data.h
@@ -0,0 +1,122 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include <emmintrin.h>
+#include <tmmintrin.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/**
+ * \brief Represents a 128-bit block.
+ */
+typedef __m128i AES_Block128;
+
+/**
+ * \brief Loads a 128-bit block from a memory location.
+ *
+ * \param[in] src The pointer to a memory location. Must not be `NULL`.
+ *
+ * \return The loaded 128-bit block.
+ */
+static __inline AES_Block128 aes_load_block128(const void* src)
+{
+ return _mm_loadu_si128((AES_Block128*) src);
+}
+
+/**
+ * \brief Loads a 128-bit block from a 16-byte aligned memory location.
+ *
+ * \param[in] src The pointer to a 16-byte aligned memory location. Must not be `NULL`.
+ *
+ * \return The loaded 128-bit block.
+ */
+static __inline AES_Block128 aes_load_block128_aligned(const void* src)
+{
+ return _mm_load_si128((AES_Block128*) src);
+}
+
+/**
+ * \brief Stores a 128-bit block in a memory location.
+ *
+ * \param[out] dest The pointer to a memory location. Must not be `NULL`.
+ *
+ * \param[in] block The block to be stored.
+ */
+static __inline void __fastcall aes_store_block128(
+ void* dest,
+ AES_Block128 block)
+{
+ _mm_storeu_si128((AES_Block128*) dest, block);
+}
+
+/**
+ * \brief Stores a 128-bit block in a 16-byte aligned memory location.
+ *
+ * \param[out] dest The pointer to a 16-byte aligned memory location. Must not be `NULL`.
+ *
+ * \param[in] block The block to be stored.
+ */
+static __inline void __fastcall aes_store_block128_aligned(
+ void* dest,
+ AES_Block128 block)
+{
+ _mm_store_si128((AES_Block128*) dest, block);
+}
+
+/**
+ * \brief XORs two 128-bit blocks.
+ *
+ * \param[in] a The first XOR operand.
+ * \param[in] b The second XOR operand.
+ *
+ * \return `a^b`.
+ */
+static __inline AES_Block128 __fastcall aes_xor_block128(
+ AES_Block128 a,
+ AES_Block128 b)
+{
+ return _mm_xor_si128(a, b);
+}
+
+/**
+ * \brief Builds a 128-bit block from four 4-byte values.
+ *
+ * Builds a 128-bit block like this:
+ *
+ * * dest[127:96] = hi3
+ * * dest[95:64] = hi2
+ * * dest[63:32] = lo1
+ * * dest[31:0] = lo0
+ *
+ * \param[in] hi3 The most significant 4-byte value.
+ * \param[in] hi2 The more significant 4-byte value.
+ * \param[in] lo1 The less significant 4-byte value.
+ * \param[in] lo0 The least significant 4-byte value.
+ *
+ * \return The built 128-bit block.
+ */
+static __inline AES_Block128 __fastcall aes_make_block128(int hi3, int hi2, int lo1, int lo0)
+{
+ return _mm_set_epi32(hi3, hi2, lo1, lo0);
+}
+
+static __inline AES_Block128 __fastcall aes_reverse_byte_order_block128(AES_Block128 block)
+{
+ return _mm_shuffle_epi8(block, aes_make_block128(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f));
+}
+
+static __inline AES_Block128 __fastcall aes_inc_block128(AES_Block128 x)
+{
+ return _mm_add_epi32(x, aes_make_block128(0, 0, 0, 1));
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/error.h b/aes/include/aes/error.h
new file mode 100644
index 0000000..e094f8a
--- /dev/null
+++ b/aes/include/aes/error.h
@@ -0,0 +1,96 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef enum
+{
+ AES_SUCCESS,
+ AES_NULL_ARGUMENT_ERROR,
+ AES_PARSE_ERROR,
+ AES_INVALID_PKCS7_PADDING_ERROR,
+ AES_NOT_IMPLEMENTED_ERROR,
+ AES_MISSING_PADDING_ERROR,
+ AES_MEMORY_ALLOCATION_ERROR,
+}
+AES_StatusCode;
+
+static __inline int aes_is_error(AES_StatusCode ec)
+{
+ return ec != AES_SUCCESS;
+}
+
+const char* aes_strerror(AES_StatusCode ec);
+
+#define AES_MAX_CALL_STACK_LENGTH 32
+
+typedef struct
+{
+ AES_StatusCode ec; ///< Error code
+
+ union
+ {
+ struct { char param_name[32]; } null_arg;
+ struct
+ {
+ char src[128];
+ char what[32];
+ }
+ parse_error;
+ struct { char what[128]; } not_implemented;
+ }
+ params;
+
+ void* call_stack[AES_MAX_CALL_STACK_LENGTH];
+ size_t call_stack_len;
+}
+AES_ErrorDetails;
+
+static __inline AES_StatusCode aes_get_error_code(
+ const AES_ErrorDetails* err_details)
+{
+ return err_details->ec;
+}
+
+size_t aes_format_error(
+ const AES_ErrorDetails* err_details,
+ char* dest,
+ size_t dest_size);
+
+AES_StatusCode aes_success(
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_error_null_argument(
+ AES_ErrorDetails* err_details,
+ const char* param_name);
+
+AES_StatusCode aes_error_parse(
+ AES_ErrorDetails* err_details,
+ const char* src,
+ const char* what);
+
+AES_StatusCode aes_error_invalid_pkcs7_padding(
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_error_not_implemented(
+ AES_ErrorDetails* err_details,
+ const char* what);
+
+AES_StatusCode aes_error_missing_padding(
+ AES_ErrorDetails* err_details);
+
+AES_StatusCode aes_error_memory_allocation(
+ AES_ErrorDetails* err_details);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/mode.h b/aes/include/aes/mode.h
new file mode 100644
index 0000000..090628b
--- /dev/null
+++ b/aes/include/aes/mode.h
@@ -0,0 +1,166 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include <assert.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef enum
+{
+ AES_ECB,
+ AES_CBC,
+ AES_CFB,
+ AES_OFB,
+ AES_CTR,
+}
+AES_Mode;
+
+#define AES_ENCRYPT_BLOCK_ECB(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_ECB( \
+ AES_## prefix ##_Block plaintext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys) \
+{ \
+ assert(encryption_keys); \
+\
+ return aes_## prefix ##_encrypt_block_(plaintext, encryption_keys); \
+}
+
+#define AES_DECRYPT_BLOCK_ECB(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_ECB( \
+ AES_## prefix ##_Block ciphertext, \
+ const AES_## prefix ##_RoundKeys* decryption_keys) \
+{ \
+ assert(decryption_keys); \
+\
+ return aes_## prefix ##_decrypt_block_(ciphertext, decryption_keys); \
+}
+
+#define AES_ENCRYPT_BLOCK_CBC(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CBC( \
+ AES_## prefix ##_Block plaintext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ return *next_init_vector = aes_## prefix ##_encrypt_block_( \
+ aes_## prefix ##_xor_blocks(plaintext, init_vector), encryption_keys); \
+}
+
+#define AES_DECRYPT_BLOCK_CBC(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CBC( \
+ AES_## prefix ##_Block ciphertext, \
+ const AES_## prefix ##_RoundKeys* decryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(decryption_keys); \
+ assert(next_init_vector); \
+\
+ AES_## prefix ##_Block plaintext = aes_## prefix ##_xor_blocks( \
+ aes_## prefix ##_decrypt_block_(ciphertext, decryption_keys), init_vector); \
+ *next_init_vector = ciphertext; \
+ return plaintext; \
+}
+
+#define AES_ENCRYPT_BLOCK_CFB(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CFB( \
+ AES_## prefix ##_Block plaintext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ return *next_init_vector = aes_## prefix ##_xor_blocks( \
+ aes_## prefix ##_encrypt_block_(init_vector, encryption_keys), plaintext); \
+}
+
+#define AES_DECRYPT_BLOCK_CFB(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CFB( \
+ AES_## prefix ##_Block ciphertext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ AES_## prefix ##_Block plaintext = aes_## prefix ##_xor_blocks( \
+ aes_## prefix ##_encrypt_block_(init_vector, encryption_keys), ciphertext); \
+ *next_init_vector = ciphertext; \
+ return plaintext; \
+}
+
+#define AES_ENCRYPT_BLOCK_OFB(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_OFB( \
+ AES_## prefix ##_Block plaintext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ AES_## prefix ##_Block tmp = aes_## prefix ##_encrypt_block_(init_vector, encryption_keys); \
+ *next_init_vector = tmp; \
+ return aes_## prefix ##_xor_blocks(tmp, plaintext); \
+}
+
+#define AES_DECRYPT_BLOCK_OFB(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_OFB( \
+ AES_## prefix ##_Block ciphertext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ return aes_## prefix ##_encrypt_block_OFB( \
+ ciphertext, encryption_keys, init_vector, next_init_vector); \
+}
+
+#define AES_ENCRYPT_BLOCK_CTR(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CTR( \
+ AES_## prefix ##_Block plaintext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ AES_## prefix ##_Block ciphertext = aes_## prefix ##_xor_blocks( \
+ plaintext, aes_## prefix ##_encrypt_block_(init_vector, encryption_keys)); \
+ *next_init_vector = aes_## prefix ##_inc_block(init_vector); \
+ return ciphertext; \
+}
+
+#define AES_DECRYPT_BLOCK_CTR(prefix) \
+static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CTR( \
+ AES_## prefix ##_Block ciphertext, \
+ const AES_## prefix ##_RoundKeys* encryption_keys, \
+ AES_## prefix ##_Block init_vector, \
+ AES_## prefix ##_Block* next_init_vector) \
+{ \
+ assert(encryption_keys); \
+ assert(next_init_vector); \
+\
+ return aes_## prefix ##_encrypt_block_CTR( \
+ ciphertext, encryption_keys, init_vector, next_init_vector); \
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/padding.h b/aes/include/aes/padding.h
new file mode 100644
index 0000000..ba316f5
--- /dev/null
+++ b/aes/include/aes/padding.h
@@ -0,0 +1,38 @@
+// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include "error.h"
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef enum
+{
+ AES_PADDING_PKCS7,
+}
+AES_PaddingMethod;
+
+AES_StatusCode aes_extract_padding_size(
+ AES_PaddingMethod,
+ const void* src,
+ size_t src_size,
+ size_t* padding_size,
+ AES_ErrorDetails*);
+
+AES_StatusCode aes_fill_with_padding(
+ AES_PaddingMethod,
+ void* dest,
+ size_t padding_size,
+ AES_ErrorDetails*);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/workarounds.h b/aes/include/aes/workarounds.h
new file mode 100644
index 0000000..914bd21
--- /dev/null
+++ b/aes/include/aes/workarounds.h
@@ -0,0 +1,16 @@
+// Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "AES tools" project.
+// For details, see https://github.com/egor-tensin/aes-tools.
+// Distributed under the MIT License.
+
+#pragma once
+
+#if defined(_MSC_VER)
+#define AES_ALIGN(t, x) __declspec(align(x)) t
+#elif defined(__GNUC__) || defined(__MINGW32__)
+#define AES_ALIGN(t, x) t __attribute__((aligned(x)))
+#else
+#warning "couldn't determine alignment attribute"
+#endif
+
+#define AES_UNUSED_PARAMETER(...) (void) (__VA_ARGS__)