aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/aes
diff options
context:
space:
mode:
Diffstat (limited to 'aes')
-rw-r--r--aes/include/aes/aes.h245
-rw-r--r--aes/include/aes/algorithm.h70
-rw-r--r--aes/include/aes/all.h6
-rw-r--r--aes/include/aes/block.h66
-rw-r--r--aes/include/aes/box.h42
-rw-r--r--aes/include/aes/box_aes.h20
-rw-r--r--aes/include/aes/box_data.h95
-rw-r--r--aes/include/aes/data.h60
-rw-r--r--aes/include/aes/error.h3
-rw-r--r--aes/include/aes/key.h80
-rw-r--r--aes/include/aes/mode.h146
-rw-r--r--aes/include/aes/round_keys.h111
-rw-r--r--aes/src/algorithm.c264
-rw-r--r--aes/src/asm/aes128.asm16
-rw-r--r--aes/src/asm/aes192.asm16
-rw-r--r--aes/src/asm/aes256.asm16
-rw-r--r--aes/src/block.c104
-rw-r--r--aes/src/box.c49
-rw-r--r--aes/src/box_aes.c219
-rw-r--r--aes/src/c/aes128.c8
-rw-r--r--aes/src/c/aes192.c8
-rw-r--r--aes/src/c/aes256.c8
-rw-r--r--aes/src/error.c4
-rw-r--r--aes/src/key.c (renamed from aes/src/aes.c)147
24 files changed, 791 insertions, 1012 deletions
diff --git a/aes/include/aes/aes.h b/aes/include/aes/aes.h
index 80f2b6f..01ab441 100644
--- a/aes/include/aes/aes.h
+++ b/aes/include/aes/aes.h
@@ -5,9 +5,8 @@
#pragma once
-#include "data.h"
-#include "error.h"
-#include "mode.h"
+#include "block.h"
+#include "round_keys.h"
#include <assert.h>
@@ -15,234 +14,42 @@
extern "C" {
#endif
-typedef struct {
- AES_Block key;
-} AES128_Key;
+AES_Block __fastcall aes128_encrypt_block_internal(AES_Block, const AES128_RoundKeys*);
+AES_Block __fastcall aes192_encrypt_block_internal(AES_Block, const AES192_RoundKeys*);
+AES_Block __fastcall aes256_encrypt_block_internal(AES_Block, const AES256_RoundKeys*);
-typedef struct {
- AES_Block hi;
- AES_Block lo;
-} AES192_Key;
+AES_Block __fastcall aes128_decrypt_block_internal(AES_Block, const AES128_RoundKeys*);
+AES_Block __fastcall aes192_decrypt_block_internal(AES_Block, const AES192_RoundKeys*);
+AES_Block __fastcall aes256_decrypt_block_internal(AES_Block, const AES256_RoundKeys*);
-typedef struct {
- AES_Block hi;
- AES_Block lo;
-} AES256_Key;
-
-static inline AES128_Key aes128_make_key(int hi3, int hi2, int lo1, int lo0) {
- AES128_Key key;
- key.key = aes_make_block(hi3, hi2, lo1, lo0);
- return key;
-}
-
-static inline AES192_Key aes192_make_key(int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) {
- AES192_Key key;
- key.hi = aes_make_block(0, 0, hi5, hi4);
- key.lo = aes_make_block(lo3, lo2, lo1, lo0);
- return key;
-}
-
-static inline AES256_Key aes256_make_key(
- int hi7,
- int hi6,
- int hi5,
- int hi4,
- int lo3,
- int lo2,
- int lo1,
- int lo0
-) {
- AES256_Key key;
- key.hi = aes_make_block(hi7, hi6, hi5, hi4);
- key.lo = aes_make_block(lo3, lo2, lo1, lo0);
- return key;
-}
-
-typedef struct {
- char str[33];
-} AES_BlockString;
-
-typedef struct {
- char str[49];
-} AES_BlockMatrixString;
-
-AES_StatusCode aes_format_block(AES_BlockString*, const AES_Block*, AES_ErrorDetails*);
-
-AES_StatusCode aes_format_block_as_matrix(
- AES_BlockMatrixString*,
- const AES_Block*,
- AES_ErrorDetails*
-);
-
-AES_StatusCode aes_print_block(const AES_Block*, AES_ErrorDetails*);
-
-AES_StatusCode aes_print_block_as_matrix(const AES_Block*, AES_ErrorDetails*);
-
-AES_StatusCode aes_parse_block(AES_Block* dest, const char* src, AES_ErrorDetails* err_details);
-
-typedef struct {
- char str[33];
-} AES128_KeyString;
-
-typedef struct {
- char str[49];
-} AES192_KeyString;
-
-typedef struct {
- char str[65];
-} AES256_KeyString;
-
-AES_StatusCode aes128_format_key(AES128_KeyString*, const AES128_Key*, AES_ErrorDetails*);
-AES_StatusCode aes192_format_key(AES192_KeyString*, const AES192_Key*, AES_ErrorDetails*);
-AES_StatusCode aes256_format_key(AES256_KeyString*, const AES256_Key*, AES_ErrorDetails*);
-
-AES_StatusCode aes128_print_key(const AES128_Key*, AES_ErrorDetails*);
-AES_StatusCode aes192_print_key(const AES192_Key*, AES_ErrorDetails*);
-AES_StatusCode aes256_print_key(const AES256_Key*, AES_ErrorDetails*);
-
-AES_StatusCode aes128_parse_key(AES128_Key* dest, const char* src, AES_ErrorDetails* err_details);
-AES_StatusCode aes192_parse_key(AES192_Key* dest, const char* src, AES_ErrorDetails* err_details);
-AES_StatusCode aes256_parse_key(AES256_Key* dest, const char* src, AES_ErrorDetails* err_details);
-
-typedef struct {
- AES_Block keys[11];
-} AES128_RoundKeys;
-
-typedef struct {
- AES_Block keys[13];
-} AES192_RoundKeys;
-
-typedef struct {
- AES_Block keys[15];
-} AES256_RoundKeys;
-
-void __fastcall aes128_expand_key_(AES_Block key, AES128_RoundKeys* encryption_keys);
-
-void __fastcall aes192_expand_key_(
- AES_Block key_lo,
- AES_Block key_hi,
- AES192_RoundKeys* encryption_keys
-);
-
-void __fastcall aes256_expand_key_(
- AES_Block key_lo,
- AES_Block key_hi,
- AES256_RoundKeys* encryption_keys
-);
-
-void __fastcall aes128_derive_decryption_keys_(
- const AES128_RoundKeys* encryption_keys,
- AES128_RoundKeys* decryption_keys
-);
-
-void __fastcall aes192_derive_decryption_keys_(
- const AES192_RoundKeys* encryption_keys,
- AES192_RoundKeys* decryption_keys
-);
-
-void __fastcall aes256_derive_decryption_keys_(
- const AES256_RoundKeys* encryption_keys,
- AES256_RoundKeys* decryption_keys
-);
-
-AES_Block __fastcall aes128_encrypt_block_(AES_Block plaintext, const AES128_RoundKeys*);
-AES_Block __fastcall aes192_encrypt_block_(AES_Block plaintext, const AES192_RoundKeys*);
-AES_Block __fastcall aes256_encrypt_block_(AES_Block plaintext, const AES256_RoundKeys*);
-
-AES_Block __fastcall aes128_decrypt_block_(AES_Block ciphertext, const AES128_RoundKeys*);
-AES_Block __fastcall aes192_decrypt_block_(AES_Block ciphertext, const AES192_RoundKeys*);
-AES_Block __fastcall aes256_decrypt_block_(AES_Block ciphertext, const AES256_RoundKeys*);
-
-AES_ENCRYPT_BLOCK_ECB(128)
-AES_DECRYPT_BLOCK_ECB(128)
-AES_ENCRYPT_BLOCK_CBC(128)
-AES_DECRYPT_BLOCK_CBC(128)
-AES_ENCRYPT_BLOCK_CFB(128)
-AES_DECRYPT_BLOCK_CFB(128)
-AES_ENCRYPT_BLOCK_OFB(128)
-AES_DECRYPT_BLOCK_OFB(128)
-AES_ENCRYPT_BLOCK_CTR(128)
-AES_DECRYPT_BLOCK_CTR(128)
-
-AES_ENCRYPT_BLOCK_ECB(192)
-AES_DECRYPT_BLOCK_ECB(192)
-AES_ENCRYPT_BLOCK_CBC(192)
-AES_DECRYPT_BLOCK_CBC(192)
-AES_ENCRYPT_BLOCK_CFB(192)
-AES_DECRYPT_BLOCK_CFB(192)
-AES_ENCRYPT_BLOCK_OFB(192)
-AES_DECRYPT_BLOCK_OFB(192)
-AES_ENCRYPT_BLOCK_CTR(192)
-AES_DECRYPT_BLOCK_CTR(192)
-
-AES_ENCRYPT_BLOCK_ECB(256)
-AES_DECRYPT_BLOCK_ECB(256)
-AES_ENCRYPT_BLOCK_CBC(256)
-AES_DECRYPT_BLOCK_CBC(256)
-AES_ENCRYPT_BLOCK_CFB(256)
-AES_DECRYPT_BLOCK_CFB(256)
-AES_ENCRYPT_BLOCK_OFB(256)
-AES_DECRYPT_BLOCK_OFB(256)
-AES_ENCRYPT_BLOCK_CTR(256)
-AES_DECRYPT_BLOCK_CTR(256)
-
-static inline void __fastcall aes128_expand_key(
- const AES128_Key* key,
- AES128_RoundKeys* encryption_keys
-) {
- assert(encryption_keys);
-
- aes128_expand_key_(key->key, encryption_keys);
+static inline AES_Block aes128_encrypt_block(AES_Block plaintext, const AES128_RoundKeys* keys) {
+ assert(keys);
+ return aes128_encrypt_block_internal(plaintext, keys);
}
-static inline void __fastcall aes128_derive_decryption_keys(
- const AES128_RoundKeys* encryption_keys,
- AES128_RoundKeys* decryption_keys
-) {
- assert(encryption_keys);
- assert(decryption_keys);
-
- aes128_derive_decryption_keys_(encryption_keys, decryption_keys);
+static inline AES_Block aes192_encrypt_block(AES_Block plaintext, const AES192_RoundKeys* keys) {
+ assert(keys);
+ return aes192_encrypt_block_internal(plaintext, keys);
}
-static inline void __fastcall aes192_expand_key(
- const AES192_Key* key,
- AES192_RoundKeys* encryption_keys
-) {
- assert(key);
- assert(encryption_keys);
-
- aes192_expand_key_(key->lo, key->hi, encryption_keys);
+static inline AES_Block aes256_encrypt_block(AES_Block plaintext, const AES256_RoundKeys* keys) {
+ assert(keys);
+ return aes256_encrypt_block_internal(plaintext, keys);
}
-static inline void __fastcall aes192_derive_decryption_keys(
- const AES192_RoundKeys* encryption_keys,
- AES192_RoundKeys* decryption_keys
-) {
- assert(encryption_keys);
- assert(decryption_keys);
-
- aes192_derive_decryption_keys_(encryption_keys, decryption_keys);
+static inline AES_Block aes128_decrypt_block(AES_Block ciphertext, const AES128_RoundKeys* keys) {
+ assert(keys);
+ return aes128_decrypt_block_internal(ciphertext, keys);
}
-static inline void __fastcall aes256_expand_key(
- const AES256_Key* key,
- AES256_RoundKeys* encryption_keys
-) {
- assert(key);
- assert(encryption_keys);
-
- aes256_expand_key_(key->lo, key->hi, encryption_keys);
+static inline AES_Block aes192_decrypt_block(AES_Block ciphertext, const AES192_RoundKeys* keys) {
+ assert(keys);
+ return aes192_decrypt_block_internal(ciphertext, keys);
}
-static inline void __fastcall aes256_derive_decryption_keys(
- const AES256_RoundKeys* encryption_keys,
- AES256_RoundKeys* decryption_keys
-) {
- assert(encryption_keys);
- assert(decryption_keys);
-
- aes256_derive_decryption_keys_(encryption_keys, decryption_keys);
+static inline AES_Block aes256_decrypt_block(AES_Block ciphertext, const AES256_RoundKeys* keys) {
+ assert(keys);
+ return aes256_decrypt_block_internal(ciphertext, keys);
}
#ifdef __cplusplus
diff --git a/aes/include/aes/algorithm.h b/aes/include/aes/algorithm.h
index e17a75a..2e283fd 100644
--- a/aes/include/aes/algorithm.h
+++ b/aes/include/aes/algorithm.h
@@ -15,6 +15,76 @@ typedef enum {
AES_AES256,
} AES_Algorithm;
+typedef union {
+ AES128_Key aes128_key;
+ AES192_Key aes192_key;
+ AES256_Key aes256_key;
+} AES_Key;
+
+typedef union {
+ AES128_KeyString aes128;
+ AES192_KeyString aes192;
+ AES256_KeyString aes256;
+} AES_KeyString;
+
+typedef union {
+ AES128_RoundKeys aes128_enc_keys;
+ AES192_RoundKeys aes192_enc_keys;
+ AES256_RoundKeys aes256_enc_keys;
+} AES_EncryptionRoundKeys;
+
+typedef union {
+ AES128_RoundKeys aes128_dec_keys;
+ AES192_RoundKeys aes192_dec_keys;
+ AES256_RoundKeys aes256_dec_keys;
+} AES_DecryptionRoundKeys;
+
+typedef AES_StatusCode (*AES_ParseKey)(
+ AES_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details
+);
+
+typedef AES_StatusCode (*AES_FormatKey)(
+ AES_KeyString* dest,
+ const AES_Key* src,
+ AES_ErrorDetails* err_details
+);
+
+typedef AES_StatusCode (*AES_ExpandKey)(
+ const AES_Key* params,
+ AES_EncryptionRoundKeys*,
+ AES_DecryptionRoundKeys*,
+ AES_ErrorDetails* err_details
+);
+
+typedef AES_StatusCode (*AES_EncryptBlock)(
+ const AES_Block* plaintext,
+ const AES_EncryptionRoundKeys* params,
+ AES_Block* ciphertext,
+ AES_ErrorDetails* err_details
+);
+
+typedef AES_StatusCode (*AES_DecryptBlock)(
+ const AES_Block* ciphertext,
+ const AES_DecryptionRoundKeys* params,
+ AES_Block* plaintext,
+ AES_ErrorDetails* err_details
+);
+
+typedef struct {
+ AES_ParseKey parse_key;
+ AES_FormatKey format_key;
+ AES_ExpandKey expand_key;
+ AES_EncryptBlock encrypt_block;
+ AES_DecryptBlock decrypt_block;
+} AES_Ops;
+
+const AES_Ops* aes_get_ops(AES_Algorithm);
+
+AES_StatusCode aes_parse_key(AES_Algorithm, AES_Key*, const char*, AES_ErrorDetails*);
+AES_StatusCode aes_format_key(AES_Algorithm, AES_KeyString*, const AES_Key*, AES_ErrorDetails*);
+
#ifdef __cplusplus
}
#endif
diff --git a/aes/include/aes/all.h b/aes/include/aes/all.h
index 8b60483..fb0a191 100644
--- a/aes/include/aes/all.h
+++ b/aes/include/aes/all.h
@@ -7,11 +7,11 @@
#include "aes.h"
#include "algorithm.h"
+#include "block.h"
#include "box.h"
-#include "box_aes.h"
-#include "box_data.h"
-#include "data.h"
#include "error.h"
+#include "key.h"
#include "mode.h"
#include "padding.h"
+#include "round_keys.h"
#include "workarounds.h"
diff --git a/aes/include/aes/block.h b/aes/include/aes/block.h
new file mode 100644
index 0000000..a41f79c
--- /dev/null
+++ b/aes/include/aes/block.h
@@ -0,0 +1,66 @@
+// 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 <emmintrin.h>
+#include <tmmintrin.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef __m128i AES_Block;
+
+/* hi3 - the most significant 4-byte value, lo0 - the least. */
+static inline AES_Block aes_make_block(int hi3, int hi2, int lo1, int lo0) {
+ return _mm_set_epi32(hi3, hi2, lo1, lo0);
+}
+
+static inline AES_Block aes_load_block(const void* src) {
+ return _mm_loadu_si128((AES_Block*)src);
+}
+
+static inline AES_Block aes_load_block_aligned(const void* src) {
+ return _mm_load_si128((AES_Block*)src);
+}
+
+static inline void aes_store_block(void* dest, AES_Block block) {
+ _mm_storeu_si128((AES_Block*)dest, block);
+}
+
+static inline void aes_store_block_aligned(void* dest, AES_Block block) {
+ _mm_store_si128((AES_Block*)dest, block);
+}
+
+static inline AES_Block aes_xor_blocks(AES_Block a, AES_Block b) {
+ return _mm_xor_si128(a, b);
+}
+
+AES_Block aes_inc_block(AES_Block x);
+
+typedef struct {
+ char str[33];
+} AES_BlockString;
+
+typedef struct {
+ char str[49];
+} AES_BlockMatrixString;
+
+AES_StatusCode aes_format_block(AES_BlockString*, const AES_Block*, AES_ErrorDetails*);
+
+AES_StatusCode aes_format_block_as_matrix(
+ AES_BlockMatrixString*,
+ const AES_Block*,
+ AES_ErrorDetails*
+);
+
+AES_StatusCode aes_parse_block(AES_Block* dest, const char* src, AES_ErrorDetails* err_details);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/box.h b/aes/include/aes/box.h
index 37292dd..3347edd 100644
--- a/aes/include/aes/box.h
+++ b/aes/include/aes/box.h
@@ -6,8 +6,9 @@
#pragma once
#include "algorithm.h"
-#include "box_data.h"
+#include "block.h"
#include "error.h"
+#include "mode.h"
#include <stdlib.h>
@@ -15,43 +16,24 @@
extern "C" {
#endif
+typedef struct {
+ AES_Algorithm algorithm;
+ AES_Mode mode;
+ AES_Block iv;
+ AES_EncryptionRoundKeys encryption_keys;
+ AES_DecryptionRoundKeys decryption_keys;
+ const AES_Ops* ops;
+} AES_Box;
+
AES_StatusCode aes_box_init(
AES_Box* box,
AES_Algorithm algorithm,
- const AES_BoxKey* box_key,
+ const AES_Key* box_key,
AES_Mode mode,
const AES_Block* 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_Block* 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_BlockString* dest,
- AES_Algorithm algorithm,
- const AES_Block* src,
- AES_ErrorDetails* err_details
-);
-
AES_StatusCode aes_box_encrypt_block(
AES_Box* box,
const AES_Block* plaintext,
diff --git a/aes/include/aes/box_aes.h b/aes/include/aes/box_aes.h
deleted file mode 100644
index c9b9b67..0000000
--- a/aes/include/aes/box_aes.h
+++ /dev/null
@@ -1,20 +0,0 @@
-// 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_BoxOps aes128_box_ops;
-extern AES_BoxOps aes192_box_ops;
-extern AES_BoxOps aes256_box_ops;
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/aes/include/aes/box_data.h b/aes/include/aes/box_data.h
deleted file mode 100644
index 3e49fa0..0000000
--- a/aes/include/aes/box_data.h
+++ /dev/null
@@ -1,95 +0,0 @@
-// 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 "algorithm.h"
-#include "error.h"
-#include "mode.h"
-
-#include <stdlib.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef union {
- AES128_Key aes128_key;
- AES192_Key aes192_key;
- AES256_Key aes256_key;
-} AES_BoxKey;
-
-typedef union {
- AES128_RoundKeys aes128_encryption_keys;
- AES192_RoundKeys aes192_encryption_keys;
- AES256_RoundKeys aes256_encryption_keys;
-} AES_BoxEncryptionRoundKeys;
-
-typedef union {
- AES128_RoundKeys aes128_decryption_keys;
- AES192_RoundKeys aes192_decryption_keys;
- AES256_RoundKeys aes256_decryption_keys;
-} AES_BoxDecryptionRoundKeys;
-
-typedef union {
- AES128_KeyString aes128;
- AES192_KeyString aes192;
- AES256_KeyString aes256;
-} AES_BoxKeyString;
-
-typedef AES_StatusCode (*AES_BoxCalculateRoundKeys)(
- const AES_BoxKey* params,
- AES_BoxEncryptionRoundKeys*,
- AES_BoxDecryptionRoundKeys*,
- AES_ErrorDetails* err_details
-);
-
-typedef AES_StatusCode (*AES_BoxParseKey)(
- AES_BoxKey* dest,
- const char* 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_Block* plaintext,
- const AES_BoxEncryptionRoundKeys* params,
- AES_Block* ciphertext,
- AES_ErrorDetails* err_details
-);
-
-typedef AES_StatusCode (*AES_BoxDecryptBlock)(
- const AES_Block* ciphertext,
- const AES_BoxDecryptionRoundKeys* params,
- AES_Block* plaintext,
- AES_ErrorDetails* err_details
-);
-
-typedef struct {
- AES_BoxCalculateRoundKeys calc_round_keys;
- AES_BoxParseKey parse_key;
- AES_BoxFormatKey format_key;
- AES_BoxEncryptBlock encrypt_block;
- AES_BoxDecryptBlock decrypt_block;
-} AES_BoxOps;
-
-typedef struct {
- AES_Algorithm algorithm;
- AES_Mode mode;
- AES_Block iv;
- AES_BoxEncryptionRoundKeys encryption_keys;
- AES_BoxDecryptionRoundKeys decryption_keys;
- const AES_BoxOps* ops;
-} AES_Box;
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/aes/include/aes/data.h b/aes/include/aes/data.h
deleted file mode 100644
index e06ec33..0000000
--- a/aes/include/aes/data.h
+++ /dev/null
@@ -1,60 +0,0 @@
-// 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
-
-typedef __m128i AES_Block;
-
-static inline AES_Block aes_load_block(const void* src) {
- return _mm_loadu_si128((AES_Block*)src);
-}
-
-static inline AES_Block aes_load_block_aligned(const void* src) {
- return _mm_load_si128((AES_Block*)src);
-}
-
-static inline void __fastcall aes_store_block(void* dest, AES_Block block) {
- _mm_storeu_si128((AES_Block*)dest, block);
-}
-
-static inline void __fastcall aes_store_block_aligned(void* dest, AES_Block block) {
- _mm_store_si128((AES_Block*)dest, block);
-}
-
-static inline AES_Block __fastcall aes_xor_blocks(AES_Block a, AES_Block b) {
- return _mm_xor_si128(a, b);
-}
-
-/**
- * hi3 - The most significant 4-byte value.
- * hi2 - The more significant 4-byte value.
- * lo1 - The less significant 4-byte value.
- * lo0 - The least significant 4-byte value.
- */
-static inline AES_Block __fastcall aes_make_block(int hi3, int hi2, int lo1, int lo0) {
- return _mm_set_epi32(hi3, hi2, lo1, lo0);
-}
-
-static inline AES_Block __fastcall aes_reverse_byte_order_in_block(AES_Block block) {
- return _mm_shuffle_epi8(block, aes_make_block(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f));
-}
-
-static inline AES_Block __fastcall aes_inc_block(AES_Block x) {
- x = aes_reverse_byte_order_in_block(x);
- x = _mm_add_epi32(x, aes_make_block(0, 0, 0, 1));
- x = aes_reverse_byte_order_in_block(x);
- return x;
-}
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/aes/include/aes/error.h b/aes/include/aes/error.h
index d3822de..c32d1b7 100644
--- a/aes/include/aes/error.h
+++ b/aes/include/aes/error.h
@@ -19,6 +19,7 @@ typedef enum {
AES_NOT_IMPLEMENTED_ERROR,
AES_MISSING_PADDING_ERROR,
AES_MEMORY_ALLOCATION_ERROR,
+ AES_MODE_REQUIRES_INIT_VECTOR_ERROR,
} AES_StatusCode;
static inline int aes_is_error(AES_StatusCode ec) {
@@ -69,6 +70,8 @@ AES_StatusCode aes_error_missing_padding(AES_ErrorDetails* err_details);
AES_StatusCode aes_error_memory_allocation(AES_ErrorDetails* err_details);
+AES_StatusCode aes_error_mode_requires_init_vector(AES_ErrorDetails* err_details);
+
#ifdef __cplusplus
}
#endif
diff --git a/aes/include/aes/key.h b/aes/include/aes/key.h
new file mode 100644
index 0000000..95fe8e5
--- /dev/null
+++ b/aes/include/aes/key.h
@@ -0,0 +1,80 @@
+// Copyright (c) 2026 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 "block.h"
+#include "error.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ AES_Block key;
+} AES128_Key;
+
+typedef struct {
+ AES_Block hi;
+ AES_Block lo;
+} AES192_Key;
+
+typedef struct {
+ AES_Block hi;
+ AES_Block lo;
+} AES256_Key;
+
+static inline AES128_Key aes128_make_key(int hi3, int hi2, int lo1, int lo0) {
+ AES128_Key key;
+ key.key = aes_make_block(hi3, hi2, lo1, lo0);
+ return key;
+}
+
+static inline AES192_Key aes192_make_key(int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) {
+ AES192_Key key;
+ key.hi = aes_make_block(0, 0, hi5, hi4);
+ key.lo = aes_make_block(lo3, lo2, lo1, lo0);
+ return key;
+}
+
+static inline AES256_Key aes256_make_key(
+ int hi7,
+ int hi6,
+ int hi5,
+ int hi4,
+ int lo3,
+ int lo2,
+ int lo1,
+ int lo0
+) {
+ AES256_Key key;
+ key.hi = aes_make_block(hi7, hi6, hi5, hi4);
+ key.lo = aes_make_block(lo3, lo2, lo1, lo0);
+ return key;
+}
+
+typedef struct {
+ char str[33];
+} AES128_KeyString;
+
+typedef struct {
+ char str[49];
+} AES192_KeyString;
+
+typedef struct {
+ char str[65];
+} AES256_KeyString;
+
+AES_StatusCode aes128_format_key(AES128_KeyString*, const AES128_Key*, AES_ErrorDetails*);
+AES_StatusCode aes192_format_key(AES192_KeyString*, const AES192_Key*, AES_ErrorDetails*);
+AES_StatusCode aes256_format_key(AES256_KeyString*, const AES256_Key*, AES_ErrorDetails*);
+
+AES_StatusCode aes128_parse_key(AES128_Key* dest, const char* src, AES_ErrorDetails* err_details);
+AES_StatusCode aes192_parse_key(AES192_Key* dest, const char* src, AES_ErrorDetails* err_details);
+AES_StatusCode aes256_parse_key(AES256_Key* dest, const char* src, AES_ErrorDetails* err_details);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/include/aes/mode.h b/aes/include/aes/mode.h
index 656388f..57dfb50 100644
--- a/aes/include/aes/mode.h
+++ b/aes/include/aes/mode.h
@@ -19,149 +19,9 @@ typedef enum {
AES_CTR,
} AES_Mode;
-#define AES_ENCRYPT_BLOCK_ECB(version) \
- static inline AES_Block __fastcall aes##version##_encrypt_block_ECB( \
- AES_Block plaintext, const AES##version##_RoundKeys* encryption_keys \
- ) { \
- assert(encryption_keys); \
- \
- return aes##version##_encrypt_block_(plaintext, encryption_keys); \
- }
-
-#define AES_DECRYPT_BLOCK_ECB(version) \
- static inline AES_Block __fastcall aes##version##_decrypt_block_ECB( \
- AES_Block ciphertext, const AES##version##_RoundKeys* decryption_keys \
- ) { \
- assert(decryption_keys); \
- \
- return aes##version##_decrypt_block_(ciphertext, decryption_keys); \
- }
-
-#define AES_ENCRYPT_BLOCK_CBC(version) \
- static inline AES_Block __fastcall aes##version##_encrypt_block_CBC( \
- AES_Block plaintext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- return *next_init_vector = aes##version##_encrypt_block_( \
- aes_xor_blocks(plaintext, init_vector), encryption_keys \
- ); \
- }
-
-#define AES_DECRYPT_BLOCK_CBC(version) \
- static inline AES_Block __fastcall aes##version##_decrypt_block_CBC( \
- AES_Block ciphertext, \
- const AES##version##_RoundKeys* decryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(decryption_keys); \
- assert(next_init_vector); \
- \
- AES_Block plaintext = aes_xor_blocks( \
- aes##version##_decrypt_block_(ciphertext, decryption_keys), init_vector \
- ); \
- *next_init_vector = ciphertext; \
- return plaintext; \
- }
-
-#define AES_ENCRYPT_BLOCK_CFB(version) \
- static inline AES_Block __fastcall aes##version##_encrypt_block_CFB( \
- AES_Block plaintext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- return *next_init_vector = aes_xor_blocks( \
- aes##version##_encrypt_block_(init_vector, encryption_keys), plaintext \
- ); \
- }
-
-#define AES_DECRYPT_BLOCK_CFB(version) \
- static inline AES_Block __fastcall aes##version##_decrypt_block_CFB( \
- AES_Block ciphertext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- AES_Block plaintext = aes_xor_blocks( \
- aes##version##_encrypt_block_(init_vector, encryption_keys), ciphertext \
- ); \
- *next_init_vector = ciphertext; \
- return plaintext; \
- }
-
-#define AES_ENCRYPT_BLOCK_OFB(version) \
- static inline AES_Block __fastcall aes##version##_encrypt_block_OFB( \
- AES_Block plaintext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- AES_Block tmp = aes##version##_encrypt_block_(init_vector, encryption_keys); \
- *next_init_vector = tmp; \
- return aes_xor_blocks(tmp, plaintext); \
- }
-
-#define AES_DECRYPT_BLOCK_OFB(version) \
- static inline AES_Block __fastcall aes##version##_decrypt_block_OFB( \
- AES_Block ciphertext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- return aes##version##_encrypt_block_OFB( \
- ciphertext, encryption_keys, init_vector, next_init_vector \
- ); \
- }
-
-#define AES_ENCRYPT_BLOCK_CTR(version) \
- static inline AES_Block __fastcall aes##version##_encrypt_block_CTR( \
- AES_Block plaintext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- AES_Block ciphertext = aes_xor_blocks( \
- plaintext, aes##version##_encrypt_block_(init_vector, encryption_keys) \
- ); \
- *next_init_vector = aes_inc_block(init_vector); \
- return ciphertext; \
- }
-
-#define AES_DECRYPT_BLOCK_CTR(version) \
- static inline AES_Block __fastcall aes##version##_decrypt_block_CTR( \
- AES_Block ciphertext, \
- const AES##version##_RoundKeys* encryption_keys, \
- AES_Block init_vector, \
- AES_Block* next_init_vector \
- ) { \
- assert(encryption_keys); \
- assert(next_init_vector); \
- \
- return aes##version##_encrypt_block_CTR( \
- ciphertext, encryption_keys, init_vector, next_init_vector \
- ); \
- }
+static inline int aes_mode_requires_init_vector(AES_Mode mode) {
+ return mode != AES_ECB;
+}
#ifdef __cplusplus
}
diff --git a/aes/include/aes/round_keys.h b/aes/include/aes/round_keys.h
new file mode 100644
index 0000000..f5814b6
--- /dev/null
+++ b/aes/include/aes/round_keys.h
@@ -0,0 +1,111 @@
+// 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 "block.h"
+#include "error.h"
+#include "key.h"
+
+#include <assert.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct {
+ AES_Block keys[11];
+} AES128_RoundKeys;
+
+typedef struct {
+ AES_Block keys[13];
+} AES192_RoundKeys;
+
+typedef struct {
+ AES_Block keys[15];
+} AES256_RoundKeys;
+
+void __fastcall aes128_expand_key_internal(AES_Block key, AES128_RoundKeys* encryption_keys);
+
+void __fastcall aes192_expand_key_internal(
+ AES_Block key_lo,
+ AES_Block key_hi,
+ AES192_RoundKeys* encryption_keys
+);
+
+void __fastcall aes256_expand_key_internal(
+ AES_Block key_lo,
+ AES_Block key_hi,
+ AES256_RoundKeys* encryption_keys
+);
+
+void __fastcall aes128_derive_decryption_keys_internal(
+ const AES128_RoundKeys* encryption_keys,
+ AES128_RoundKeys* decryption_keys
+);
+
+void __fastcall aes192_derive_decryption_keys_internal(
+ const AES192_RoundKeys* encryption_keys,
+ AES192_RoundKeys* decryption_keys
+);
+
+void __fastcall aes256_derive_decryption_keys_internal(
+ const AES256_RoundKeys* encryption_keys,
+ AES256_RoundKeys* decryption_keys
+);
+
+static inline void aes128_expand_key(const AES128_Key* key, AES128_RoundKeys* encryption_keys) {
+ assert(encryption_keys);
+
+ aes128_expand_key_internal(key->key, encryption_keys);
+}
+
+static inline void aes128_derive_decryption_keys(
+ const AES128_RoundKeys* encryption_keys,
+ AES128_RoundKeys* decryption_keys
+) {
+ assert(encryption_keys);
+ assert(decryption_keys);
+
+ aes128_derive_decryption_keys_internal(encryption_keys, decryption_keys);
+}
+
+static inline void aes192_expand_key(const AES192_Key* key, AES192_RoundKeys* encryption_keys) {
+ assert(key);
+ assert(encryption_keys);
+
+ aes192_expand_key_internal(key->lo, key->hi, encryption_keys);
+}
+
+static inline void aes192_derive_decryption_keys(
+ const AES192_RoundKeys* encryption_keys,
+ AES192_RoundKeys* decryption_keys
+) {
+ assert(encryption_keys);
+ assert(decryption_keys);
+
+ aes192_derive_decryption_keys_internal(encryption_keys, decryption_keys);
+}
+
+static inline void aes256_expand_key(const AES256_Key* key, AES256_RoundKeys* encryption_keys) {
+ assert(key);
+ assert(encryption_keys);
+
+ aes256_expand_key_internal(key->lo, key->hi, encryption_keys);
+}
+
+static inline void aes256_derive_decryption_keys(
+ const AES256_RoundKeys* encryption_keys,
+ AES256_RoundKeys* decryption_keys
+) {
+ assert(encryption_keys);
+ assert(decryption_keys);
+
+ aes256_derive_decryption_keys_internal(encryption_keys, decryption_keys);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/aes/src/algorithm.c b/aes/src/algorithm.c
new file mode 100644
index 0000000..2c3e056
--- /dev/null
+++ b/aes/src/algorithm.c
@@ -0,0 +1,264 @@
+/*
+ * Copyright (c) 2026 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.
+ */
+
+#include <aes/all.h>
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static AES_StatusCode aes_parse_key_aes128(
+ AES_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+
+ return aes128_parse_key(&dest->aes128_key, src, err_details);
+}
+
+static AES_StatusCode aes_parse_key_aes192(
+ AES_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+
+ return aes192_parse_key(&dest->aes192_key, src, err_details);
+}
+
+static AES_StatusCode aes_parse_key_aes256(
+ AES_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+
+ return aes256_parse_key(&dest->aes256_key, src, err_details);
+}
+
+static AES_StatusCode aes_format_key_aes128(
+ AES_KeyString* dest,
+ const AES_Key* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+ if (src == NULL)
+ return aes_error_null_argument(err_details, "src");
+
+ return aes128_format_key(&dest->aes128, &src->aes128_key, err_details);
+}
+
+static AES_StatusCode aes_format_key_aes192(
+ AES_KeyString* dest,
+ const AES_Key* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+ if (src == NULL)
+ return aes_error_null_argument(err_details, "src");
+
+ return aes192_format_key(&dest->aes192, &src->aes192_key, err_details);
+}
+
+static AES_StatusCode aes_format_key_aes256(
+ AES_KeyString* dest,
+ const AES_Key* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+ if (src == NULL)
+ return aes_error_null_argument(err_details, "src");
+
+ return aes256_format_key(&dest->aes256, &src->aes256_key, err_details);
+}
+
+static AES_StatusCode aes_expand_key_aes128(
+ const AES_Key* key,
+ AES_EncryptionRoundKeys* encryption_keys,
+ AES_DecryptionRoundKeys* decryption_keys,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ aes128_expand_key(&key->aes128_key, &encryption_keys->aes128_enc_keys);
+ aes128_derive_decryption_keys(
+ &encryption_keys->aes128_enc_keys, &decryption_keys->aes128_dec_keys
+ );
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_expand_key_aes192(
+ const AES_Key* key,
+ AES_EncryptionRoundKeys* encryption_keys,
+ AES_DecryptionRoundKeys* decryption_keys,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ aes192_expand_key(&key->aes192_key, &encryption_keys->aes192_enc_keys);
+ aes192_derive_decryption_keys(
+ &encryption_keys->aes192_enc_keys, &decryption_keys->aes192_dec_keys
+ );
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_expand_key_aes256(
+ const AES_Key* key,
+ AES_EncryptionRoundKeys* encryption_keys,
+ AES_DecryptionRoundKeys* decryption_keys,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ aes256_expand_key(&key->aes256_key, &encryption_keys->aes256_enc_keys);
+ aes256_derive_decryption_keys(
+ &encryption_keys->aes256_enc_keys, &decryption_keys->aes256_dec_keys
+ );
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_encrypt_block_aes128(
+ const AES_Block* input,
+ const AES_EncryptionRoundKeys* params,
+ AES_Block* output,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ *output = aes128_encrypt_block(*input, &params->aes128_enc_keys);
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_decrypt_block_aes128(
+ const AES_Block* input,
+ const AES_DecryptionRoundKeys* params,
+ AES_Block* output,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ *output = aes128_decrypt_block(*input, &params->aes128_dec_keys);
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_encrypt_block_aes192(
+ const AES_Block* input,
+ const AES_EncryptionRoundKeys* params,
+ AES_Block* output,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ *output = aes192_encrypt_block(*input, &params->aes192_enc_keys);
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_decrypt_block_aes192(
+ const AES_Block* input,
+ const AES_DecryptionRoundKeys* params,
+ AES_Block* output,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ *output = aes192_decrypt_block(*input, &params->aes192_dec_keys);
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_encrypt_block_aes256(
+ const AES_Block* input,
+ const AES_EncryptionRoundKeys* params,
+ AES_Block* output,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ *output = aes256_encrypt_block(*input, &params->aes256_enc_keys);
+ return AES_SUCCESS;
+}
+
+static AES_StatusCode aes_decrypt_block_aes256(
+ const AES_Block* input,
+ const AES_DecryptionRoundKeys* params,
+ AES_Block* output,
+ AES_ErrorDetails* err_details
+) {
+ AES_UNUSED_PARAMETER(err_details);
+ *output = aes256_decrypt_block(*input, &params->aes256_dec_keys);
+ return AES_SUCCESS;
+}
+
+static AES_Ops aes128_ops = {
+ &aes_parse_key_aes128,
+ &aes_format_key_aes128,
+ &aes_expand_key_aes128,
+ &aes_encrypt_block_aes128,
+ &aes_decrypt_block_aes128,
+};
+
+static AES_Ops aes192_ops = {
+ &aes_parse_key_aes192,
+ &aes_format_key_aes192,
+ &aes_expand_key_aes192,
+ &aes_encrypt_block_aes192,
+ &aes_decrypt_block_aes192,
+};
+
+static AES_Ops aes256_ops = {
+ &aes_parse_key_aes256,
+ &aes_format_key_aes256,
+ &aes_expand_key_aes256,
+ &aes_encrypt_block_aes256,
+ &aes_decrypt_block_aes256,
+};
+
+static const AES_Ops* aes_ops_list[] = {
+ &aes128_ops,
+ &aes192_ops,
+ &aes256_ops,
+};
+
+const AES_Ops* aes_get_ops(AES_Algorithm algorithm) {
+ int l = 0;
+ size_t r = sizeof(aes_ops_list) / sizeof(aes_ops_list[0]) - 1;
+
+ if ((int)algorithm < l || (size_t)algorithm > r) {
+ assert(0);
+ return NULL;
+ }
+
+ return aes_ops_list[algorithm];
+}
+
+AES_StatusCode aes_parse_key(
+ AES_Algorithm algorithm,
+ AES_Key* dest,
+ const char* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+ if (src == NULL)
+ return aes_error_null_argument(err_details, "src");
+
+ return aes_get_ops(algorithm)->parse_key(dest, src, err_details);
+}
+
+AES_StatusCode aes_format_key(
+ AES_Algorithm algorithm,
+ AES_KeyString* dest,
+ const AES_Key* src,
+ AES_ErrorDetails* err_details
+) {
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+ if (src == NULL)
+ return aes_error_null_argument(err_details, "src");
+
+ return aes_get_ops(algorithm)->format_key(dest, src, err_details);
+}
diff --git a/aes/src/asm/aes128.asm b/aes/src/asm/aes128.asm
index abf5063..67498fd 100644
--- a/aes/src/asm/aes128.asm
+++ b/aes/src/asm/aes128.asm
@@ -9,7 +9,7 @@
.code
-@aes128_encrypt_block_@20 proc
+@aes128_encrypt_block_internal@20 proc
pxor xmm0, [ecx]
aesenc xmm0, [ecx + 10h]
aesenc xmm0, [ecx + 20h]
@@ -22,9 +22,9 @@
aesenc xmm0, [ecx + 90h]
aesenclast xmm0, [ecx + 0A0h]
ret
-@aes128_encrypt_block_@20 endp
+@aes128_encrypt_block_internal@20 endp
-@aes128_decrypt_block_@20 proc
+@aes128_decrypt_block_internal@20 proc
pxor xmm0, [ecx]
aesdec xmm0, [ecx + 10h]
aesdec xmm0, [ecx + 20h]
@@ -37,9 +37,9 @@
aesdec xmm0, [ecx + 90h]
aesdeclast xmm0, [ecx + 0A0h]
ret
-@aes128_decrypt_block_@20 endp
+@aes128_decrypt_block_internal@20 endp
-@aes128_expand_key_@20 proc
+@aes128_expand_key_internal@20 proc
; A "word" (in terms of the FIPS 187 standard) is a 32-bit block.
; Words are denoted by `w[N]`.
;
@@ -166,9 +166,9 @@ aes128_keygen_assist:
add ecx, 10h ; ecx = &w[i+8]
ret
-@aes128_expand_key_@20 endp
+@aes128_expand_key_internal@20 endp
-@aes128_derive_decryption_keys_@8 proc
+@aes128_derive_decryption_keys_internal@8 proc
movdqa xmm5, [ecx]
movdqa xmm4, [ecx + 0A0h]
movdqa [edx], xmm4
@@ -198,6 +198,6 @@ aes128_keygen_assist:
movdqa [edx + 50h], xmm5
ret
-@aes128_derive_decryption_keys_@8 endp
+@aes128_derive_decryption_keys_internal@8 endp
end
diff --git a/aes/src/asm/aes192.asm b/aes/src/asm/aes192.asm
index 5411419..765152c 100644
--- a/aes/src/asm/aes192.asm
+++ b/aes/src/asm/aes192.asm
@@ -9,7 +9,7 @@
.code
-@aes192_encrypt_block_@20 proc
+@aes192_encrypt_block_internal@20 proc
pxor xmm0, [ecx]
aesenc xmm0, [ecx + 10h]
aesenc xmm0, [ecx + 20h]
@@ -24,9 +24,9 @@
aesenc xmm0, [ecx + 0B0h]
aesenclast xmm0, [ecx + 0C0h]
ret
-@aes192_encrypt_block_@20 endp
+@aes192_encrypt_block_internal@20 endp
-@aes192_decrypt_block_@20 proc
+@aes192_decrypt_block_internal@20 proc
pxor xmm0, [ecx]
aesdec xmm0, [ecx + 10h]
aesdec xmm0, [ecx + 20h]
@@ -41,9 +41,9 @@
aesdec xmm0, [ecx + 0B0h]
aesdeclast xmm0, [ecx + 0C0h]
ret
-@aes192_decrypt_block_@20 endp
+@aes192_decrypt_block_internal@20 endp
-@aes192_expand_key_@36 proc
+@aes192_expand_key_internal@36 proc
; A "word" (in terms of the FIPS 187 standard) is a 32-bit block.
; Words are denoted by `w[N]`.
;
@@ -207,9 +207,9 @@ aes192_keygen_assist:
; xmm1[31:0] == w[i+10] == RotWord(SubWord(w[i+5]))^Rcon^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i]
ret
-@aes192_expand_key_@36 endp
+@aes192_expand_key_internal@36 endp
-@aes192_derive_decryption_keys_@8 proc
+@aes192_derive_decryption_keys_internal@8 proc
movdqa xmm5, [ecx]
movdqa xmm4, [ecx + 0C0h]
movdqa [edx], xmm4
@@ -244,6 +244,6 @@ aes192_keygen_assist:
movdqa [edx + 60h], xmm5
ret
-@aes192_derive_decryption_keys_@8 endp
+@aes192_derive_decryption_keys_internal@8 endp
end
diff --git a/aes/src/asm/aes256.asm b/aes/src/asm/aes256.asm
index 4c6c07c..58aad6b 100644
--- a/aes/src/asm/aes256.asm
+++ b/aes/src/asm/aes256.asm
@@ -9,7 +9,7 @@
.code
-@aes256_encrypt_block_@20 proc
+@aes256_encrypt_block_internal@20 proc
pxor xmm0, [ecx]
aesenc xmm0, [ecx + 10h]
aesenc xmm0, [ecx + 20h]
@@ -26,9 +26,9 @@
aesenc xmm0, [ecx + 0D0h]
aesenclast xmm0, [ecx + 0E0h]
ret
-@aes256_encrypt_block_@20 endp
+@aes256_encrypt_block_internal@20 endp
-@aes256_decrypt_block_@20 proc
+@aes256_decrypt_block_internal@20 proc
pxor xmm0, [ecx]
aesdec xmm0, [ecx + 10h]
aesdec xmm0, [ecx + 20h]
@@ -45,9 +45,9 @@
aesdec xmm0, [ecx + 0D0h]
aesdeclast xmm0, [ecx + 0E0h]
ret
-@aes256_decrypt_block_@20 endp
+@aes256_decrypt_block_internal@20 endp
-@aes256_expand_key_@36 proc
+@aes256_expand_key_internal@36 proc
; A "word" (in terms of the FIPS 187 standard) is a 32-bit block.
; Words are denoted by `w[N]`.
;
@@ -240,9 +240,9 @@ aes256_keygen_assist:
pxor xmm0, xmm1
ret
-@aes256_expand_key_@36 endp
+@aes256_expand_key_internal@36 endp
-@aes256_derive_decryption_keys_@8 proc
+@aes256_derive_decryption_keys_internal@8 proc
movdqa xmm5, [ecx]
movdqa xmm4, [ecx + 0E0h]
movdqa [edx], xmm4
@@ -282,6 +282,6 @@ aes256_keygen_assist:
movdqa [edx + 70h], xmm5
ret
-@aes256_derive_decryption_keys_@8 endp
+@aes256_derive_decryption_keys_internal@8 endp
end
diff --git a/aes/src/block.c b/aes/src/block.c
new file mode 100644
index 0000000..552c445
--- /dev/null
+++ b/aes/src/block.c
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2026 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.
+ */
+
+#include <aes/all.h>
+
+#include <assert.h>
+#include <emmintrin.h>
+#include <stdio.h>
+#include <string.h>
+#include <tmmintrin.h>
+
+static AES_Block reverse_byte_order(AES_Block block) {
+ return _mm_shuffle_epi8(block, aes_make_block(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f));
+}
+
+AES_Block aes_inc_block(AES_Block x) {
+ x = reverse_byte_order(x);
+ x = _mm_add_epi32(x, aes_make_block(0, 0, 0, 1));
+ x = reverse_byte_order(x);
+ return x;
+}
+
+AES_StatusCode aes_format_block(
+ AES_BlockString* str,
+ const AES_Block* block,
+ AES_ErrorDetails* err_details
+) {
+ assert(str);
+ assert(block);
+
+ if (str == NULL)
+ return aes_error_null_argument(err_details, "str");
+ if (block == NULL)
+ return aes_error_null_argument(err_details, "block");
+
+ char* cursor = str->str;
+
+ AES_ALIGN(unsigned char, 16) bytes[16];
+ aes_store_block_aligned(bytes, *block);
+
+ for (int i = 0; i < 16; ++i, cursor += 2)
+ sprintf(cursor, "%02x", bytes[i]);
+
+ *cursor = '\0';
+ return AES_SUCCESS;
+}
+
+AES_StatusCode aes_format_block_as_matrix(
+ AES_BlockMatrixString* str,
+ const AES_Block* block,
+ AES_ErrorDetails* err_details
+) {
+ assert(str);
+ assert(block);
+
+ if (str == NULL)
+ return aes_error_null_argument(err_details, "str");
+ if (block == NULL)
+ return aes_error_null_argument(err_details, "block");
+
+ char* cursor = str->str;
+
+ AES_ALIGN(unsigned char, 16) bytes[4][4];
+ aes_store_block_aligned(bytes, *block);
+
+ for (int i = 0; i < 4; ++i, cursor += 3) {
+ 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 AES_SUCCESS;
+}
+
+AES_StatusCode aes_parse_block(AES_Block* dest, const char* src, AES_ErrorDetails* err_details) {
+ assert(dest);
+ assert(src);
+
+ if (dest == NULL)
+ return aes_error_null_argument(err_details, "dest");
+ if (src == NULL)
+ return aes_error_null_argument(err_details, "src");
+
+ const char* cursor = src;
+
+ AES_ALIGN(unsigned char, 16) bytes[16];
+
+ for (int i = 0; i < 16; ++i) {
+ int n;
+ unsigned int byte;
+ if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
+ return aes_error_parse(err_details, src, "a 128-bit block");
+ bytes[i] = (unsigned char)byte;
+ cursor += n;
+ }
+
+ *dest = aes_load_block_aligned(bytes);
+ return AES_SUCCESS;
+}
diff --git a/aes/src/box.c b/aes/src/box.c
index dd50f25..e4750dc 100644
--- a/aes/src/box.c
+++ b/aes/src/box.c
@@ -10,16 +10,10 @@
#include <stdlib.h>
#include <string.h>
-static const AES_BoxOps* aes_box_ops_list[] = {
- &aes128_box_ops,
- &aes192_box_ops,
- &aes256_box_ops,
-};
-
AES_StatusCode aes_box_init(
AES_Box* box,
AES_Algorithm algorithm,
- const AES_BoxKey* box_key,
+ const AES_Key* box_key,
AES_Mode mode,
const AES_Block* iv,
AES_ErrorDetails* err_details
@@ -28,13 +22,16 @@ AES_StatusCode aes_box_init(
box->algorithm = algorithm;
box->mode = mode;
- if (iv != NULL)
+
+ if (!iv && aes_mode_requires_init_vector(mode))
+ return aes_error_mode_requires_init_vector(err_details);
+ if (iv)
box->iv = *iv;
- box->ops = aes_box_ops_list[algorithm];
- status = box->ops->calc_round_keys(
- box_key, &box->encryption_keys, &box->decryption_keys, err_details
- );
+ box->ops = aes_get_ops(algorithm);
+
+ status =
+ box->ops->expand_key(box_key, &box->encryption_keys, &box->decryption_keys, err_details);
if (aes_is_error(status))
return status;
@@ -533,31 +530,3 @@ AES_StatusCode aes_box_decrypt_buffer(
return status;
}
}
-
-AES_StatusCode aes_box_parse_key(
- AES_BoxKey* dest,
- AES_Algorithm algorithm,
- const char* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
- if (src == NULL)
- return aes_error_null_argument(err_details, "src");
-
- return aes_box_ops_list[algorithm]->parse_key(dest, src, err_details);
-}
-
-AES_StatusCode aes_box_format_key(
- AES_BoxKeyString* dest,
- AES_Algorithm algorithm,
- const AES_BoxKey* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
- if (src == NULL)
- return aes_error_null_argument(err_details, "src");
-
- return aes_box_ops_list[algorithm]->format_key(dest, src, err_details);
-}
diff --git a/aes/src/box_aes.c b/aes/src/box_aes.c
deleted file mode 100644
index d0c9a41..0000000
--- a/aes/src/box_aes.c
+++ /dev/null
@@ -1,219 +0,0 @@
-/*
- * 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.
- */
-
-#include <aes/all.h>
-
-#include <stdlib.h>
-#include <string.h>
-
-static AES_StatusCode aes_box_derive_params_aes128(
- const AES_BoxKey* box_key,
- AES_BoxEncryptionRoundKeys* encryption_keys,
- AES_BoxDecryptionRoundKeys* decryption_keys,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- aes128_expand_key_(box_key->aes128_key.key, &encryption_keys->aes128_encryption_keys);
- aes128_derive_decryption_keys_(
- &encryption_keys->aes128_encryption_keys, &decryption_keys->aes128_decryption_keys
- );
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_derive_params_aes192(
- const AES_BoxKey* box_key,
- AES_BoxEncryptionRoundKeys* encryption_keys,
- AES_BoxDecryptionRoundKeys* decryption_keys,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- aes192_expand_key_(
- box_key->aes192_key.lo, box_key->aes192_key.hi, &encryption_keys->aes192_encryption_keys
- );
- aes192_derive_decryption_keys_(
- &encryption_keys->aes192_encryption_keys, &decryption_keys->aes192_decryption_keys
- );
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_derive_params_aes256(
- const AES_BoxKey* box_key,
- AES_BoxEncryptionRoundKeys* encryption_keys,
- AES_BoxDecryptionRoundKeys* decryption_keys,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- aes256_expand_key_(
- box_key->aes256_key.lo, box_key->aes256_key.hi, &encryption_keys->aes256_encryption_keys
- );
- aes256_derive_decryption_keys_(
- &encryption_keys->aes256_encryption_keys, &decryption_keys->aes256_decryption_keys
- );
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_parse_key_aes128(
- AES_BoxKey* dest,
- const char* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
-
- return aes128_parse_key(&dest->aes128_key, src, err_details);
-}
-
-static AES_StatusCode aes_box_parse_key_aes192(
- AES_BoxKey* dest,
- const char* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
-
- return aes192_parse_key(&dest->aes192_key, src, err_details);
-}
-
-static AES_StatusCode aes_box_parse_key_aes256(
- AES_BoxKey* dest,
- const char* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
-
- return aes256_parse_key(&dest->aes256_key, src, err_details);
-}
-
-static AES_StatusCode aes_box_format_key_aes128(
- AES_BoxKeyString* dest,
- const AES_BoxKey* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
- if (src == NULL)
- return aes_error_null_argument(err_details, "src");
-
- return aes128_format_key(&dest->aes128, &src->aes128_key, err_details);
-}
-
-static AES_StatusCode aes_box_format_key_aes192(
- AES_BoxKeyString* dest,
- const AES_BoxKey* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
- if (src == NULL)
- return aes_error_null_argument(err_details, "src");
-
- return aes192_format_key(&dest->aes192, &src->aes192_key, err_details);
-}
-
-static AES_StatusCode aes_box_format_key_aes256(
- AES_BoxKeyString* dest,
- const AES_BoxKey* src,
- AES_ErrorDetails* err_details
-) {
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
- if (src == NULL)
- return aes_error_null_argument(err_details, "src");
-
- return aes256_format_key(&dest->aes256, &src->aes256_key, err_details);
-}
-
-static AES_StatusCode aes_box_encrypt_block_aes128(
- const AES_Block* input,
- const AES_BoxEncryptionRoundKeys* params,
- AES_Block* output,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- *output = aes128_encrypt_block_(*input, &params->aes128_encryption_keys);
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_decrypt_block_aes128(
- const AES_Block* input,
- const AES_BoxDecryptionRoundKeys* params,
- AES_Block* output,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- *output = aes128_decrypt_block_(*input, &params->aes128_decryption_keys);
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_encrypt_block_aes192(
- const AES_Block* input,
- const AES_BoxEncryptionRoundKeys* params,
- AES_Block* output,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- *output = aes192_encrypt_block_(*input, &params->aes192_encryption_keys);
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_decrypt_block_aes192(
- const AES_Block* input,
- const AES_BoxDecryptionRoundKeys* params,
- AES_Block* output,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- *output = aes192_decrypt_block_(*input, &params->aes192_decryption_keys);
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_encrypt_block_aes256(
- const AES_Block* input,
- const AES_BoxEncryptionRoundKeys* params,
- AES_Block* output,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- *output = aes256_encrypt_block_(*input, &params->aes256_encryption_keys);
- return AES_SUCCESS;
-}
-
-static AES_StatusCode aes_box_decrypt_block_aes256(
- const AES_Block* input,
- const AES_BoxDecryptionRoundKeys* params,
- AES_Block* output,
- AES_ErrorDetails* err_details
-) {
- AES_UNUSED_PARAMETER(err_details);
- *output = aes256_decrypt_block_(*input, &params->aes256_decryption_keys);
- return AES_SUCCESS;
-}
-
-AES_BoxOps aes128_box_ops = {
- &aes_box_derive_params_aes128,
- &aes_box_parse_key_aes128,
- &aes_box_format_key_aes128,
- &aes_box_encrypt_block_aes128,
- &aes_box_decrypt_block_aes128,
-};
-
-AES_BoxOps aes192_box_ops = {
- &aes_box_derive_params_aes192,
- &aes_box_parse_key_aes192,
- &aes_box_format_key_aes192,
- &aes_box_encrypt_block_aes192,
- &aes_box_decrypt_block_aes192,
-};
-
-AES_BoxOps aes256_box_ops = {
- &aes_box_derive_params_aes256,
- &aes_box_parse_key_aes256,
- &aes_box_format_key_aes256,
- &aes_box_encrypt_block_aes256,
- &aes_box_decrypt_block_aes256,
-};
diff --git a/aes/src/c/aes128.c b/aes/src/c/aes128.c
index 909b208..115c55e 100644
--- a/aes/src/c/aes128.c
+++ b/aes/src/c/aes128.c
@@ -10,7 +10,7 @@
#include <emmintrin.h>
#include <wmmintrin.h>
-AES_Block __fastcall aes128_encrypt_block_(
+AES_Block __fastcall aes128_encrypt_block_internal(
AES_Block plaintext,
const AES128_RoundKeys* encryption_keys
) {
@@ -27,7 +27,7 @@ AES_Block __fastcall aes128_encrypt_block_(
return _mm_aesenclast_si128(plaintext, encryption_keys->keys[10]);
}
-AES_Block __fastcall aes128_decrypt_block_(
+AES_Block __fastcall aes128_decrypt_block_internal(
AES_Block ciphertext,
const AES128_RoundKeys* decryption_keys
) {
@@ -60,7 +60,7 @@ static AES_Block __fastcall aes128_expand_key_assist(AES_Block prev, AES_Block h
return prev;
}
-void __fastcall aes128_expand_key_(AES_Block key, AES128_RoundKeys* encryption_keys) {
+void __fastcall aes128_expand_key_internal(AES_Block key, AES128_RoundKeys* encryption_keys) {
AES_Block prev = encryption_keys->keys[0] = key;
prev = encryption_keys->keys[1] =
aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x01));
@@ -84,7 +84,7 @@ void __fastcall aes128_expand_key_(AES_Block key, AES128_RoundKeys* encryption_k
aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x36));
}
-void __fastcall aes128_derive_decryption_keys_(
+void __fastcall aes128_derive_decryption_keys_internal(
const AES128_RoundKeys* encryption_keys,
AES128_RoundKeys* decryption_keys
) {
diff --git a/aes/src/c/aes192.c b/aes/src/c/aes192.c
index b5f1103..ba72107 100644
--- a/aes/src/c/aes192.c
+++ b/aes/src/c/aes192.c
@@ -10,7 +10,7 @@
#include <emmintrin.h>
#include <wmmintrin.h>
-AES_Block __fastcall aes192_encrypt_block_(
+AES_Block __fastcall aes192_encrypt_block_internal(
AES_Block plaintext,
const AES192_RoundKeys* encryption_keys
) {
@@ -29,7 +29,7 @@ AES_Block __fastcall aes192_encrypt_block_(
return _mm_aesenclast_si128(plaintext, encryption_keys->keys[12]);
}
-AES_Block __fastcall aes192_decrypt_block_(
+AES_Block __fastcall aes192_decrypt_block_internal(
AES_Block ciphertext,
const AES192_RoundKeys* decryption_keys
) {
@@ -73,7 +73,7 @@ static void __fastcall aes192_expand_key_assist(
*prev_hi = _mm_xor_si128(*prev_hi, tmp);
}
-void __fastcall aes192_expand_key_(
+void __fastcall aes192_expand_key_internal(
AES_Block key_lo,
AES_Block key_hi,
AES192_RoundKeys* encryption_keys
@@ -125,7 +125,7 @@ void __fastcall aes192_expand_key_(
encryption_keys->keys[12] = key_lo;
}
-void __fastcall aes192_derive_decryption_keys_(
+void __fastcall aes192_derive_decryption_keys_internal(
const AES192_RoundKeys* encryption_keys,
AES192_RoundKeys* decryption_keys
) {
diff --git a/aes/src/c/aes256.c b/aes/src/c/aes256.c
index cc5e13e..bc86965 100644
--- a/aes/src/c/aes256.c
+++ b/aes/src/c/aes256.c
@@ -10,7 +10,7 @@
#include <emmintrin.h>
#include <wmmintrin.h>
-AES_Block __fastcall aes256_encrypt_block_(
+AES_Block __fastcall aes256_encrypt_block_internal(
AES_Block plaintext,
const AES256_RoundKeys* encryption_keys
) {
@@ -31,7 +31,7 @@ AES_Block __fastcall aes256_encrypt_block_(
return _mm_aesenclast_si128(plaintext, encryption_keys->keys[14]);
}
-AES_Block __fastcall aes256_decrypt_block_(
+AES_Block __fastcall aes256_decrypt_block_internal(
AES_Block ciphertext,
const AES256_RoundKeys* decryption_keys
) {
@@ -75,7 +75,7 @@ static AES_Block __fastcall aes256_expand_key_assist(
return *prev_hi;
}
-void __fastcall aes256_expand_key_(
+void __fastcall aes256_expand_key_internal(
AES_Block key_lo,
AES_Block key_hi,
AES256_RoundKeys* encryption_keys
@@ -139,7 +139,7 @@ void __fastcall aes256_expand_key_(
encryption_keys->keys[14] = aes256_expand_key_assist(&prev_lo, &prev_hi, hwgen);
}
-void __fastcall aes256_derive_decryption_keys_(
+void __fastcall aes256_derive_decryption_keys_internal(
const AES256_RoundKeys* encryption_keys,
AES256_RoundKeys* decryption_keys
) {
diff --git a/aes/src/error.c b/aes/src/error.c
index 8de8a5d..004c869 100644
--- a/aes/src/error.c
+++ b/aes/src/error.c
@@ -220,3 +220,7 @@ AES_StatusCode aes_error_missing_padding(AES_ErrorDetails* err_details) {
AES_StatusCode aes_error_memory_allocation(AES_ErrorDetails* err_details) {
return aes_make_error(err_details, AES_MEMORY_ALLOCATION_ERROR);
}
+
+AES_StatusCode aes_error_mode_requires_init_vector(AES_ErrorDetails* err_details) {
+ return aes_make_error(err_details, AES_MODE_REQUIRES_INIT_VECTOR_ERROR);
+}
diff --git a/aes/src/aes.c b/aes/src/key.c
index 999bdf1..35f858c 100644
--- a/aes/src/aes.c
+++ b/aes/src/key.c
@@ -11,117 +11,6 @@
#include <stdio.h>
#include <string.h>
-AES_StatusCode aes_format_block(
- AES_BlockString* str,
- const AES_Block* block,
- AES_ErrorDetails* err_details
-) {
- assert(str);
- assert(block);
-
- if (str == NULL)
- return aes_error_null_argument(err_details, "str");
- if (block == NULL)
- return aes_error_null_argument(err_details, "block");
-
- char* cursor = str->str;
-
- AES_ALIGN(unsigned char, 16) bytes[16];
- aes_store_block_aligned(bytes, *block);
-
- for (int i = 0; i < 16; ++i, cursor += 2)
- sprintf(cursor, "%02x", bytes[i]);
-
- *cursor = '\0';
- return AES_SUCCESS;
-}
-
-AES_StatusCode aes_format_block_as_matrix(
- AES_BlockMatrixString* str,
- const AES_Block* block,
- AES_ErrorDetails* err_details
-) {
- assert(str);
- assert(block);
-
- if (str == NULL)
- return aes_error_null_argument(err_details, "str");
- if (block == NULL)
- return aes_error_null_argument(err_details, "block");
-
- char* cursor = str->str;
-
- AES_ALIGN(unsigned char, 16) bytes[4][4];
- aes_store_block_aligned(bytes, *block);
-
- for (int i = 0; i < 4; ++i, cursor += 3) {
- 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 AES_SUCCESS;
-}
-
-AES_StatusCode aes_print_block(const AES_Block* block, AES_ErrorDetails* err_details) {
- assert(block);
-
- if (block == NULL)
- return aes_error_null_argument(err_details, "block");
-
- AES_StatusCode ec = AES_SUCCESS;
- AES_BlockString str;
-
- if (aes_is_error(ec = aes_format_block(&str, block, err_details)))
- return ec;
-
- printf("%s\n", str.str);
- return ec;
-}
-
-AES_StatusCode aes_print_block_as_matrix(const AES_Block* block, AES_ErrorDetails* err_details) {
- assert(block);
-
- if (block == NULL)
- return aes_error_null_argument(err_details, "block");
-
- AES_StatusCode ec = AES_SUCCESS;
- AES_BlockMatrixString str;
-
- if (aes_is_error(ec = aes_format_block_as_matrix(&str, block, err_details)))
- return ec;
-
- printf("%s", str.str);
- return ec;
-}
-
-AES_StatusCode aes_parse_block(AES_Block* dest, const char* src, AES_ErrorDetails* err_details) {
- assert(dest);
- assert(src);
-
- if (dest == NULL)
- return aes_error_null_argument(err_details, "dest");
- if (src == NULL)
- return aes_error_null_argument(err_details, "src");
-
- const char* cursor = src;
-
- AES_ALIGN(unsigned char, 16) bytes[16];
-
- for (int i = 0; i < 16; ++i) {
- int n;
- unsigned int byte;
- if (sscanf(cursor, "%2x%n", &byte, &n) != 1)
- return aes_error_parse(err_details, src, "a 128-bit block");
- bytes[i] = (unsigned char)byte;
- cursor += n;
- }
-
- *dest = aes_load_block_aligned(bytes);
- return AES_SUCCESS;
-}
-
AES_StatusCode aes128_format_key(
AES128_KeyString* str,
const AES128_Key* key,
@@ -217,42 +106,6 @@ AES_StatusCode aes256_format_key(
return AES_SUCCESS;
}
-AES_StatusCode aes128_print_key(const AES128_Key* key, AES_ErrorDetails* err_details) {
- return aes_print_block(&key->key, err_details);
-}
-
-AES_StatusCode aes192_print_key(const AES192_Key* key, AES_ErrorDetails* err_details) {
- assert(key);
-
- if (key == NULL)
- return aes_error_null_argument(err_details, "key");
-
- AES_StatusCode ec = AES_SUCCESS;
- AES192_KeyString str;
-
- if (aes_is_error(ec = aes192_format_key(&str, key, err_details)))
- return ec;
-
- printf("%s\n", str.str);
- return ec;
-}
-
-AES_StatusCode aes256_print_key(const AES256_Key* key, AES_ErrorDetails* err_details) {
- assert(key);
-
- if (key == NULL)
- return aes_error_null_argument(err_details, "key");
-
- AES_StatusCode ec = AES_SUCCESS;
- AES256_KeyString str;
-
- if (aes_is_error(ec = aes256_format_key(&str, key, err_details)))
- return ec;
-
- printf("%s\n", str.str);
- return ec;
-}
-
AES_StatusCode aes128_parse_key(AES128_Key* dest, const char* src, AES_ErrorDetails* err_details) {
return aes_parse_block(&dest->key, src, err_details);
}