diff options
| author | Egor Tensin <egor@tensin.name> | 2026-07-22 18:31:50 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-07-22 21:07:53 +0200 |
| commit | 4720b5ac21163936fb47f524037a6f12ce6e6ce2 (patch) | |
| tree | 7b1f3f7f0e4047134048bb9b9967498b6f8ead12 /aes/src | |
| parent | minor refactoring (diff) | |
| download | aes-tools-4720b5ac21163936fb47f524037a6f12ce6e6ce2.tar.gz aes-tools-4720b5ac21163936fb47f524037a6f12ce6e6ce2.zip | |
rewrite: the beginning
This a giant fuck-you to myself while I have nothing (actually, tons of
things) to do in a hospital.
* The giant screens of preprocessor code are gone, they always sucked,
fuck them.
* Switched everything to the "boxes" interface as the only one
remaining.
* Restuctured things in aes/ so that the files they're in make at least
a little bit of sense.
* Removed a bunch of cruft and prematurely generic code.
* This is half-baked at best: I just made sure that the tests pass. A
lot of cleanup awaits.
Diffstat (limited to 'aes/src')
| -rw-r--r-- | aes/src/algorithm.c | 264 | ||||
| -rw-r--r-- | aes/src/asm/aes128.asm | 16 | ||||
| -rw-r--r-- | aes/src/asm/aes192.asm | 16 | ||||
| -rw-r--r-- | aes/src/asm/aes256.asm | 16 | ||||
| -rw-r--r-- | aes/src/block.c | 104 | ||||
| -rw-r--r-- | aes/src/box.c | 49 | ||||
| -rw-r--r-- | aes/src/box_aes.c | 219 | ||||
| -rw-r--r-- | aes/src/c/aes128.c | 8 | ||||
| -rw-r--r-- | aes/src/c/aes192.c | 8 | ||||
| -rw-r--r-- | aes/src/c/aes256.c | 8 | ||||
| -rw-r--r-- | aes/src/error.c | 4 | ||||
| -rw-r--r-- | aes/src/key.c (renamed from aes/src/aes.c) | 147 |
12 files changed, 417 insertions, 442 deletions
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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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, ¶ms->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); } |
