diff options
41 files changed, 2137 insertions, 3005 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..78868be --- /dev/null +++ b/.clang-format @@ -0,0 +1,32 @@ +--- +Language: Cpp +BasedOnStyle: Chromium + +IndentWidth: 4 + +AccessModifierOffset: -4 +AlignConsecutiveMacros: AcrossComments +AllowShortFunctionsOnASingleLine: Empty +BinPackArguments: false +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +BreakStringLiterals: false +ColumnLimit: 100 +IncludeBlocks: Regroup +SpacesBeforeTrailingComments: 1 +WrapNamespaceBodyWithEmptyLines: Always + +IncludeCategories: + - Regex: '^".+' + Priority: 1 + - Regex: '^<aes\/|<aesxx\/' + Priority: 2 + - Regex: '^<boost\/' + Priority: 3 + - Regex: '^<windows\.h>' + Priority: 4 + - Regex: '^<.*\.h>$' + Priority: 5 + - Regex: '.*' + Priority: 6 +... diff --git a/aes/include/aes/aes.h b/aes/include/aes/aes.h index 1108235..2b3e9ff 100644 --- a/aes/include/aes/aes.h +++ b/aes/include/aes/aes.h @@ -12,8 +12,7 @@ #include <assert.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif typedef AES_Block128 AES_AES_Block; @@ -21,390 +20,301 @@ typedef AES_AES_Block AES_AES128_Block; typedef AES_AES_Block AES_AES192_Block; typedef AES_AES_Block AES_AES256_Block; -typedef struct -{ +typedef struct { AES_AES_Block key; -} -AES_AES128_Key; +} AES_AES128_Key; -typedef struct -{ +typedef struct { AES_AES_Block hi; AES_AES_Block lo; -} -AES_AES192_Key; +} AES_AES192_Key; -typedef struct -{ +typedef struct { AES_AES_Block hi; AES_AES_Block lo; -} -AES_AES256_Key; +} AES_AES256_Key; -static __inline AES_AES_Block aes_AES_make_block( - int hi3, int hi2, int lo1, int lo0) -{ +static __inline AES_AES_Block aes_AES_make_block(int hi3, int hi2, int lo1, int lo0) { return aes_make_block128(hi3, hi2, lo1, lo0); } -static __inline AES_AES128_Block aes_AES128_make_block( - int hi3, int hi2, int lo1, int lo0) -{ +static __inline AES_AES128_Block aes_AES128_make_block(int hi3, int hi2, int lo1, int lo0) { return aes_AES_make_block(hi3, hi2, lo1, lo0); } -static __inline AES_AES192_Block aes_AES192_make_block( - int hi3, int hi2, int lo1, int lo0) -{ +static __inline AES_AES192_Block aes_AES192_make_block(int hi3, int hi2, int lo1, int lo0) { return aes_AES_make_block(hi3, hi2, lo1, lo0); } -static __inline AES_AES256_Block aes_AES256_make_block( - int hi3, int hi2, int lo1, int lo0) -{ +static __inline AES_AES256_Block aes_AES256_make_block(int hi3, int hi2, int lo1, int lo0) { return aes_AES_make_block(hi3, hi2, lo1, lo0); } -static __inline AES_AES128_Key aes_AES128_make_key( - int hi3, int hi2, int lo1, int lo0) -{ +static __inline AES_AES128_Key aes_AES128_make_key(int hi3, int hi2, int lo1, int lo0) { AES_AES128_Key key; key.key = aes_AES_make_block(hi3, hi2, lo1, lo0); return key; } -static __inline AES_AES192_Key aes_AES192_make_key( - int hi5, int hi4, - int lo3, int lo2, int lo1, int lo0) -{ +static __inline AES_AES192_Key aes_AES192_make_key(int hi5, + int hi4, + int lo3, + int lo2, + int lo1, + int lo0) { AES_AES192_Key key; - key.hi = aes_AES_make_block( 0, 0, hi5, hi4); + key.hi = aes_AES_make_block(0, 0, hi5, hi4); key.lo = aes_AES_make_block(lo3, lo2, lo1, lo0); return key; } -static __inline AES_AES256_Key aes_AES256_make_key( - int hi7, int hi6, int hi5, int hi4, - int lo3, int lo2, int lo1, int lo0) -{ +static __inline AES_AES256_Key +aes_AES256_make_key(int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0) { AES_AES256_Key key; key.hi = aes_AES_make_block(hi7, hi6, hi5, hi4); key.lo = aes_AES_make_block(lo3, lo2, lo1, lo0); return key; } -typedef struct { char str[33]; } AES_AES_BlockString; +typedef struct { + char str[33]; +} AES_AES_BlockString; + typedef AES_AES_BlockString AES_AES128_BlockString; typedef AES_AES_BlockString AES_AES192_BlockString; typedef AES_AES_BlockString AES_AES256_BlockString; -typedef struct { char str[49]; } AES_AES_BlockMatrixString; +typedef struct { + char str[49]; +} AES_AES_BlockMatrixString; + typedef AES_AES_BlockMatrixString AES_AES128_BlockMatrixString; typedef AES_AES_BlockMatrixString AES_AES192_BlockMatrixString; typedef AES_AES_BlockMatrixString AES_AES256_BlockMatrixString; -AES_StatusCode aes_AES_format_block( - AES_AES_BlockString*, - const AES_AES_Block*, - AES_ErrorDetails*); +AES_StatusCode aes_AES_format_block(AES_AES_BlockString*, const AES_AES_Block*, AES_ErrorDetails*); -static __inline AES_StatusCode aes_AES128_format_block( - AES_AES128_BlockString* dest, - const AES_AES128_Block* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES128_format_block(AES_AES128_BlockString* dest, + const AES_AES128_Block* src, + AES_ErrorDetails* err_details) { return aes_AES_format_block(dest, src, err_details); } -static __inline AES_StatusCode aes_AES192_format_block( - AES_AES192_BlockString* dest, - const AES_AES192_Block* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES192_format_block(AES_AES192_BlockString* dest, + const AES_AES192_Block* src, + AES_ErrorDetails* err_details) { return aes_AES_format_block(dest, src, err_details); } -static __inline AES_StatusCode aes_AES256_format_block( - AES_AES256_BlockString* dest, - const AES_AES256_Block* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES256_format_block(AES_AES256_BlockString* dest, + const AES_AES256_Block* src, + AES_ErrorDetails* err_details) { return aes_AES_format_block(dest, src, err_details); } -AES_StatusCode aes_AES_format_block_as_matrix( - AES_AES_BlockMatrixString*, - const AES_AES_Block*, - AES_ErrorDetails*); +AES_StatusCode aes_AES_format_block_as_matrix(AES_AES_BlockMatrixString*, + const AES_AES_Block*, + AES_ErrorDetails*); -static __inline AES_StatusCode aes_AES128_format_block_as_matrix( - AES_AES128_BlockMatrixString* dest, - const AES_AES128_Block* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES128_format_block_as_matrix(AES_AES128_BlockMatrixString* dest, + const AES_AES128_Block* src, + AES_ErrorDetails* err_details) { return aes_AES_format_block_as_matrix(dest, src, err_details); } -static __inline AES_StatusCode aes_AES192_format_block_as_matrix( - AES_AES192_BlockMatrixString* dest, - const AES_AES192_Block* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES192_format_block_as_matrix(AES_AES192_BlockMatrixString* dest, + const AES_AES192_Block* src, + AES_ErrorDetails* err_details) { return aes_AES_format_block_as_matrix(dest, src, err_details); } -static __inline AES_StatusCode aes_AES256_format_block_as_matrix( - AES_AES256_BlockMatrixString* dest, - const AES_AES256_Block* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES256_format_block_as_matrix(AES_AES256_BlockMatrixString* dest, + const AES_AES256_Block* src, + AES_ErrorDetails* err_details) { return aes_AES_format_block_as_matrix(dest, src, err_details); } -AES_StatusCode aes_AES_print_block( - const AES_AES_Block*, - AES_ErrorDetails*); +AES_StatusCode aes_AES_print_block(const AES_AES_Block*, AES_ErrorDetails*); -static __inline AES_StatusCode aes_AES128_print_block( - const AES_AES128_Block* block, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES128_print_block(const AES_AES128_Block* block, + AES_ErrorDetails* err_details) { return aes_AES_print_block(block, err_details); } -static __inline AES_StatusCode aes_AES192_print_block( - const AES_AES192_Block* block, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES192_print_block(const AES_AES192_Block* block, + AES_ErrorDetails* err_details) { return aes_AES_print_block(block, err_details); } -static __inline AES_StatusCode aes_AES256_print_block( - const AES_AES256_Block* block, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES256_print_block(const AES_AES256_Block* block, + AES_ErrorDetails* err_details) { return aes_AES_print_block(block, err_details); } -AES_StatusCode aes_AES_print_block_as_matrix( - const AES_AES_Block*, - AES_ErrorDetails*); +AES_StatusCode aes_AES_print_block_as_matrix(const AES_AES_Block*, AES_ErrorDetails*); -static __inline AES_StatusCode aes_AES128_print_block_as_matrix( - const AES_AES128_Block* block, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES128_print_block_as_matrix(const AES_AES128_Block* block, + AES_ErrorDetails* err_details) { return aes_AES_print_block_as_matrix(block, err_details); } -static __inline AES_StatusCode aes_AES192_print_block_as_matrix( - const AES_AES192_Block* block, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES192_print_block_as_matrix(const AES_AES192_Block* block, + AES_ErrorDetails* err_details) { return aes_AES_print_block_as_matrix(block, err_details); } -static __inline AES_StatusCode aes_AES256_print_block_as_matrix( - const AES_AES256_Block* block, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES256_print_block_as_matrix(const AES_AES256_Block* block, + AES_ErrorDetails* err_details) { return aes_AES_print_block_as_matrix(block, err_details); } -AES_StatusCode aes_AES_parse_block( - AES_AES_Block* dest, - const char* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_AES_parse_block(AES_AES_Block* dest, + const char* src, + AES_ErrorDetails* err_details); -static __inline AES_StatusCode aes_AES128_parse_block( - AES_AES128_Block* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES128_parse_block(AES_AES128_Block* dest, + const char* src, + AES_ErrorDetails* err_details) { return aes_AES_parse_block(dest, src, err_details); } -static __inline AES_StatusCode aes_AES192_parse_block( - AES_AES192_Block* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES192_parse_block(AES_AES192_Block* dest, + const char* src, + AES_ErrorDetails* err_details) { return aes_AES_parse_block(dest, src, err_details); } -static __inline AES_StatusCode aes_AES256_parse_block( - AES_AES256_Block* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_AES256_parse_block(AES_AES256_Block* dest, + const char* src, + AES_ErrorDetails* err_details) { return aes_AES_parse_block(dest, src, err_details); } -typedef struct { char str[33]; } AES_AES128_KeyString; -typedef struct { char str[49]; } AES_AES192_KeyString; -typedef struct { char str[65]; } AES_AES256_KeyString; +typedef struct { + char str[33]; +} AES_AES128_KeyString; + +typedef struct { + char str[49]; +} AES_AES192_KeyString; + +typedef struct { + char str[65]; +} AES_AES256_KeyString; -AES_StatusCode aes_AES128_format_key( - AES_AES128_KeyString*, - const AES_AES128_Key*, - AES_ErrorDetails*); +AES_StatusCode aes_AES128_format_key(AES_AES128_KeyString*, + const AES_AES128_Key*, + AES_ErrorDetails*); -AES_StatusCode aes_AES192_format_key( - AES_AES192_KeyString*, - const AES_AES192_Key*, - AES_ErrorDetails*); +AES_StatusCode aes_AES192_format_key(AES_AES192_KeyString*, + const AES_AES192_Key*, + AES_ErrorDetails*); -AES_StatusCode aes_AES256_format_key( - AES_AES256_KeyString*, - const AES_AES256_Key*, - AES_ErrorDetails*); +AES_StatusCode aes_AES256_format_key(AES_AES256_KeyString*, + const AES_AES256_Key*, + AES_ErrorDetails*); -AES_StatusCode aes_AES128_print_key( - const AES_AES128_Key*, - AES_ErrorDetails*); +AES_StatusCode aes_AES128_print_key(const AES_AES128_Key*, AES_ErrorDetails*); -AES_StatusCode aes_AES192_print_key( - const AES_AES192_Key*, - AES_ErrorDetails*); +AES_StatusCode aes_AES192_print_key(const AES_AES192_Key*, AES_ErrorDetails*); -AES_StatusCode aes_AES256_print_key( - const AES_AES256_Key*, - AES_ErrorDetails*); +AES_StatusCode aes_AES256_print_key(const AES_AES256_Key*, AES_ErrorDetails*); -AES_StatusCode aes_AES128_parse_key( - AES_AES128_Key* dest, - const char* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_AES128_parse_key(AES_AES128_Key* dest, + const char* src, + AES_ErrorDetails* err_details); -AES_StatusCode aes_AES192_parse_key( - AES_AES192_Key* dest, - const char* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_AES192_parse_key(AES_AES192_Key* dest, + const char* src, + AES_ErrorDetails* err_details); -AES_StatusCode aes_AES256_parse_key( - AES_AES256_Key* dest, - const char* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_AES256_parse_key(AES_AES256_Key* dest, + const char* src, + AES_ErrorDetails* err_details); -typedef struct -{ +typedef struct { AES_AES_Block keys[11]; -} -AES_AES128_RoundKeys; +} AES_AES128_RoundKeys; -typedef struct -{ +typedef struct { AES_AES_Block keys[13]; -} -AES_AES192_RoundKeys; +} AES_AES192_RoundKeys; -typedef struct -{ +typedef struct { AES_AES_Block keys[15]; -} -AES_AES256_RoundKeys; +} AES_AES256_RoundKeys; -void __fastcall aes_AES128_expand_key_( - AES_AES_Block key, - AES_AES128_RoundKeys* encryption_keys); +void __fastcall aes_AES128_expand_key_(AES_AES_Block key, AES_AES128_RoundKeys* encryption_keys); -void __fastcall aes_AES192_expand_key_( - AES_AES_Block key_lo, - AES_AES_Block key_hi, - AES_AES192_RoundKeys* encryption_keys); +void __fastcall aes_AES192_expand_key_(AES_AES_Block key_lo, + AES_AES_Block key_hi, + AES_AES192_RoundKeys* encryption_keys); -void __fastcall aes_AES256_expand_key_( - AES_AES_Block key_lo, - AES_AES_Block key_hi, - AES_AES256_RoundKeys* encryption_keys); +void __fastcall aes_AES256_expand_key_(AES_AES_Block key_lo, + AES_AES_Block key_hi, + AES_AES256_RoundKeys* encryption_keys); -void __fastcall aes_AES128_derive_decryption_keys_( - const AES_AES128_RoundKeys* encryption_keys, - AES_AES128_RoundKeys* decryption_keys); +void __fastcall aes_AES128_derive_decryption_keys_(const AES_AES128_RoundKeys* encryption_keys, + AES_AES128_RoundKeys* decryption_keys); -void __fastcall aes_AES192_derive_decryption_keys_( - const AES_AES192_RoundKeys* encryption_keys, - AES_AES192_RoundKeys* decryption_keys); +void __fastcall aes_AES192_derive_decryption_keys_(const AES_AES192_RoundKeys* encryption_keys, + AES_AES192_RoundKeys* decryption_keys); -void __fastcall aes_AES256_derive_decryption_keys_( - const AES_AES256_RoundKeys* encryption_keys, - AES_AES256_RoundKeys* decryption_keys); +void __fastcall aes_AES256_derive_decryption_keys_(const AES_AES256_RoundKeys* encryption_keys, + AES_AES256_RoundKeys* decryption_keys); -AES_AES_Block __fastcall aes_AES128_encrypt_block_( - AES_AES_Block plaintext, - const AES_AES128_RoundKeys*); +AES_AES_Block __fastcall aes_AES128_encrypt_block_(AES_AES_Block plaintext, + const AES_AES128_RoundKeys*); -AES_AES_Block __fastcall aes_AES192_encrypt_block_( - AES_AES_Block plaintext, - const AES_AES192_RoundKeys*); +AES_AES_Block __fastcall aes_AES192_encrypt_block_(AES_AES_Block plaintext, + const AES_AES192_RoundKeys*); -AES_AES_Block __fastcall aes_AES256_encrypt_block_( - AES_AES_Block plaintext, - const AES_AES256_RoundKeys*); +AES_AES_Block __fastcall aes_AES256_encrypt_block_(AES_AES_Block plaintext, + const AES_AES256_RoundKeys*); -AES_AES_Block __fastcall aes_AES128_decrypt_block_( - AES_AES_Block ciphertext, - const AES_AES128_RoundKeys*); +AES_AES_Block __fastcall aes_AES128_decrypt_block_(AES_AES_Block ciphertext, + const AES_AES128_RoundKeys*); -AES_AES_Block __fastcall aes_AES192_decrypt_block_( - AES_AES_Block ciphertext, - const AES_AES192_RoundKeys*); +AES_AES_Block __fastcall aes_AES192_decrypt_block_(AES_AES_Block ciphertext, + const AES_AES192_RoundKeys*); -AES_AES_Block __fastcall aes_AES256_decrypt_block_( - AES_AES_Block ciphertext, - const AES_AES256_RoundKeys*); +AES_AES_Block __fastcall aes_AES256_decrypt_block_(AES_AES_Block ciphertext, + const AES_AES256_RoundKeys*); -static __inline AES_AES_Block __fastcall aes_AES_xor_blocks( - AES_AES_Block a, - AES_AES_Block b) -{ +static __inline AES_AES_Block __fastcall aes_AES_xor_blocks(AES_AES_Block a, AES_AES_Block b) { return aes_xor_block128(a, b); } -static __inline AES_AES_Block __fastcall aes_AES128_xor_blocks( - AES_AES128_Block a, - AES_AES128_Block b) -{ +static __inline AES_AES_Block __fastcall aes_AES128_xor_blocks(AES_AES128_Block a, + AES_AES128_Block b) { return aes_AES_xor_blocks(a, b); } -static __inline AES_AES_Block __fastcall aes_AES192_xor_blocks( - AES_AES192_Block a, - AES_AES192_Block b) -{ +static __inline AES_AES_Block __fastcall aes_AES192_xor_blocks(AES_AES192_Block a, + AES_AES192_Block b) { return aes_AES_xor_blocks(a, b); } -static __inline AES_AES_Block __fastcall aes_AES256_xor_blocks( - AES_AES256_Block a, - AES_AES256_Block b) -{ +static __inline AES_AES_Block __fastcall aes_AES256_xor_blocks(AES_AES256_Block a, + AES_AES256_Block b) { return aes_AES_xor_blocks(a, b); } -static __inline AES_AES_Block __fastcall aes_AES_inc_block( - AES_AES_Block block) -{ +static __inline AES_AES_Block __fastcall aes_AES_inc_block(AES_AES_Block block) { block = aes_reverse_byte_order_block128(block); block = aes_inc_block128(block); return aes_reverse_byte_order_block128(block); } -static __inline AES_AES_Block __fastcall aes_AES128_inc_block( - AES_AES128_Block block) -{ +static __inline AES_AES_Block __fastcall aes_AES128_inc_block(AES_AES128_Block block) { return aes_AES_inc_block(block); } -static __inline AES_AES_Block __fastcall aes_AES192_inc_block( - AES_AES192_Block block) -{ +static __inline AES_AES_Block __fastcall aes_AES192_inc_block(AES_AES192_Block block) { return aes_AES_inc_block(block); } -static __inline AES_AES_Block __fastcall aes_AES256_inc_block( - AES_AES256_Block block) -{ +static __inline AES_AES_Block __fastcall aes_AES256_inc_block(AES_AES256_Block block) { return aes_AES_inc_block(block); } @@ -447,10 +357,8 @@ AES_DECRYPT_BLOCK_CTR(AES256); * \param[in] key The AES-128 key. * \param[out] encryption_keys The AES-128 encryption round keys. Must not be `NULL`. */ -static __inline void __fastcall aes_AES128_expand_key( - const AES_AES128_Key* key, - AES_AES128_RoundKeys* encryption_keys) -{ +static __inline void __fastcall aes_AES128_expand_key(const AES_AES128_Key* key, + AES_AES128_RoundKeys* encryption_keys) { assert(encryption_keys); aes_AES128_expand_key_(key->key, encryption_keys); @@ -464,8 +372,7 @@ static __inline void __fastcall aes_AES128_expand_key( */ static __inline void __fastcall aes_AES128_derive_decryption_keys( const AES_AES128_RoundKeys* encryption_keys, - AES_AES128_RoundKeys* decryption_keys) -{ + AES_AES128_RoundKeys* decryption_keys) { assert(encryption_keys); assert(decryption_keys); @@ -478,10 +385,8 @@ static __inline void __fastcall aes_AES128_derive_decryption_keys( * \param[in] key The AES-192 key. * \param[out] encryption_keys The AES-192 encryption round keys. Must not be `NULL`. */ -static __inline void __fastcall aes_AES192_expand_key( - const AES_AES192_Key* key, - AES_AES192_RoundKeys* encryption_keys) -{ +static __inline void __fastcall aes_AES192_expand_key(const AES_AES192_Key* key, + AES_AES192_RoundKeys* encryption_keys) { assert(key); assert(encryption_keys); @@ -496,8 +401,7 @@ static __inline void __fastcall aes_AES192_expand_key( */ static __inline void __fastcall aes_AES192_derive_decryption_keys( const AES_AES192_RoundKeys* encryption_keys, - AES_AES192_RoundKeys* decryption_keys) -{ + AES_AES192_RoundKeys* decryption_keys) { assert(encryption_keys); assert(decryption_keys); @@ -510,10 +414,8 @@ static __inline void __fastcall aes_AES192_derive_decryption_keys( * \param[in] key The AES-256 key. * \param[out] encryption_keys The AES-256 encryption round keys. Must not be `NULL`. */ -static __inline void __fastcall aes_AES256_expand_key( - const AES_AES256_Key* key, - AES_AES256_RoundKeys* encryption_keys) -{ +static __inline void __fastcall aes_AES256_expand_key(const AES_AES256_Key* key, + AES_AES256_RoundKeys* encryption_keys) { assert(key); assert(encryption_keys); @@ -528,8 +430,7 @@ static __inline void __fastcall aes_AES256_expand_key( */ static __inline void __fastcall aes_AES256_derive_decryption_keys( const AES_AES256_RoundKeys* encryption_keys, - AES_AES256_RoundKeys* decryption_keys) -{ + AES_AES256_RoundKeys* decryption_keys) { assert(encryption_keys); assert(decryption_keys); diff --git a/aes/include/aes/algorithm.h b/aes/include/aes/algorithm.h index d88cf59..e17a75a 100644 --- a/aes/include/aes/algorithm.h +++ b/aes/include/aes/algorithm.h @@ -6,17 +6,14 @@ #pragma once #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -typedef enum -{ +typedef enum { AES_AES128, AES_AES192, AES_AES256, -} -AES_Algorithm; +} AES_Algorithm; #ifdef __cplusplus } diff --git a/aes/include/aes/box.h b/aes/include/aes/box.h index 2051d3d..4960fcd 100644 --- a/aes/include/aes/box.h +++ b/aes/include/aes/box.h @@ -12,69 +12,59 @@ #include <stdlib.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -AES_StatusCode aes_box_init( - AES_Box* box, - AES_Algorithm algorithm, - const AES_BoxKey* box_key, - AES_Mode mode, - const AES_BoxBlock* iv, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_init(AES_Box* box, + AES_Algorithm algorithm, + const AES_BoxKey* box_key, + AES_Mode mode, + const AES_BoxBlock* iv, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_parse_key( - AES_BoxKey* dest, - AES_Algorithm algorithm, - const char* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_parse_key(AES_BoxKey* dest, + AES_Algorithm algorithm, + const char* src, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_parse_block( - AES_BoxBlock* dest, - AES_Algorithm algorithm, - const char* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_parse_block(AES_BoxBlock* dest, + AES_Algorithm algorithm, + const char* src, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_format_key( - AES_BoxKeyString* dest, - AES_Algorithm algorithm, - const AES_BoxKey* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_format_key(AES_BoxKeyString* dest, + AES_Algorithm algorithm, + const AES_BoxKey* src, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_format_block( - AES_BoxBlockString* dest, - AES_Algorithm algorithm, - const AES_BoxBlock* src, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_format_block(AES_BoxBlockString* dest, + AES_Algorithm algorithm, + const AES_BoxBlock* src, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_encrypt_block( - AES_Box* box, - const AES_BoxBlock* plaintext, - AES_BoxBlock* ciphertext, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_encrypt_block(AES_Box* box, + const AES_BoxBlock* plaintext, + AES_BoxBlock* ciphertext, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_decrypt_block( - AES_Box* box, - const AES_BoxBlock* ciphertext, - AES_BoxBlock* plaintext, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_decrypt_block(AES_Box* box, + const AES_BoxBlock* ciphertext, + AES_BoxBlock* plaintext, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_encrypt_buffer( - AES_Box* box, - const void* src, - size_t src_size, - void* dest, - size_t* dest_size, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_encrypt_buffer(AES_Box* box, + const void* src, + size_t src_size, + void* dest, + size_t* dest_size, + AES_ErrorDetails* err_details); -AES_StatusCode aes_box_decrypt_buffer( - AES_Box* box, - const void* src, - size_t src_size, - void* dest, - size_t* dest_size, - AES_ErrorDetails* err_details); +AES_StatusCode aes_box_decrypt_buffer(AES_Box* box, + const void* src, + size_t src_size, + void* dest, + size_t* dest_size, + AES_ErrorDetails* err_details); #ifdef __cplusplus } diff --git a/aes/include/aes/box_aes.h b/aes/include/aes/box_aes.h index c92d883..e15f0cb 100644 --- a/aes/include/aes/box_aes.h +++ b/aes/include/aes/box_aes.h @@ -8,8 +8,7 @@ #include "box_data.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif extern AES_BoxAlgorithmInterface aes_box_algorithm_aes128; diff --git a/aes/include/aes/box_data.h b/aes/include/aes/box_data.h index e2315eb..296afa0 100644 --- a/aes/include/aes/box_data.h +++ b/aes/include/aes/box_data.h @@ -12,117 +12,83 @@ #include <stdlib.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -typedef union -{ +typedef union { AES_AES128_Key aes128_key; AES_AES192_Key aes192_key; AES_AES256_Key aes256_key; -} -AES_BoxKey; +} AES_BoxKey; -typedef union -{ +typedef union { AES_AES128_RoundKeys aes128_encryption_keys; AES_AES192_RoundKeys aes192_encryption_keys; AES_AES256_RoundKeys aes256_encryption_keys; -} -AES_BoxEncryptionRoundKeys; +} AES_BoxEncryptionRoundKeys; -typedef union -{ +typedef union { AES_AES128_RoundKeys aes128_decryption_keys; AES_AES192_RoundKeys aes192_decryption_keys; AES_AES256_RoundKeys aes256_decryption_keys; -} -AES_BoxDecryptionRoundKeys; +} AES_BoxDecryptionRoundKeys; -typedef union -{ +typedef union { AES_AES128_KeyString aes128; AES_AES192_KeyString aes192; AES_AES256_KeyString aes256; -} -AES_BoxKeyString; +} AES_BoxKeyString; -typedef union -{ +typedef union { AES_AES_Block aes_block; -} -AES_BoxBlock; +} AES_BoxBlock; -typedef union -{ +typedef union { AES_AES_BlockString aes; -} -AES_BoxBlockString; +} AES_BoxBlockString; -typedef AES_StatusCode (*AES_BoxCalculateRoundKeys)( - const AES_BoxKey* params, - AES_BoxEncryptionRoundKeys*, - AES_BoxDecryptionRoundKeys*, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxCalculateRoundKeys)(const AES_BoxKey* params, + AES_BoxEncryptionRoundKeys*, + AES_BoxDecryptionRoundKeys*, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxParseBlock)( - AES_BoxBlock* dest, - const char* src, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxParseBlock)(AES_BoxBlock* dest, + const char* src, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxParseKey)( - AES_BoxKey* dest, - const char* src, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxParseKey)(AES_BoxKey* dest, + const char* src, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxFormatBlock)( - AES_BoxBlockString* dest, - const AES_BoxBlock* src, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxFormatBlock)(AES_BoxBlockString* dest, + const AES_BoxBlock* src, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxFormatKey)( - AES_BoxKeyString* dest, - const AES_BoxKey* src, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxFormatKey)(AES_BoxKeyString* dest, + const AES_BoxKey* src, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxEncryptBlock)( - const AES_BoxBlock* plaintext, - const AES_BoxEncryptionRoundKeys* params, - AES_BoxBlock* ciphertext, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxEncryptBlock)(const AES_BoxBlock* plaintext, + const AES_BoxEncryptionRoundKeys* params, + AES_BoxBlock* ciphertext, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxDecryptBlock)( - const AES_BoxBlock* ciphertext, - const AES_BoxDecryptionRoundKeys* params, - AES_BoxBlock* plaintext, - AES_ErrorDetails* err_details); +typedef AES_StatusCode (*AES_BoxDecryptBlock)(const AES_BoxBlock* ciphertext, + const AES_BoxDecryptionRoundKeys* params, + AES_BoxBlock* plaintext, + AES_ErrorDetails* err_details); -typedef AES_StatusCode (*AES_BoxXorBlock)( - AES_BoxBlock*, - const AES_BoxBlock*, - AES_ErrorDetails*); +typedef AES_StatusCode (*AES_BoxXorBlock)(AES_BoxBlock*, const AES_BoxBlock*, AES_ErrorDetails*); -typedef AES_StatusCode (*AES_BoxIncBlock)( - AES_BoxBlock*, - AES_ErrorDetails*); +typedef AES_StatusCode (*AES_BoxIncBlock)(AES_BoxBlock*, AES_ErrorDetails*); -typedef AES_StatusCode (*AES_BoxGetBlockSize)( - size_t*, - AES_ErrorDetails*); +typedef AES_StatusCode (*AES_BoxGetBlockSize)(size_t*, AES_ErrorDetails*); -typedef AES_StatusCode (*AES_BoxStoreBlock)( - void*, - const AES_BoxBlock*, - AES_ErrorDetails*); +typedef AES_StatusCode (*AES_BoxStoreBlock)(void*, const AES_BoxBlock*, AES_ErrorDetails*); -typedef AES_StatusCode (*AES_BoxLoadBlock)( - AES_BoxBlock*, - const void*, - AES_ErrorDetails*); +typedef AES_StatusCode (*AES_BoxLoadBlock)(AES_BoxBlock*, const void*, AES_ErrorDetails*); -typedef struct -{ +typedef struct { AES_BoxCalculateRoundKeys calc_round_keys; AES_BoxParseBlock parse_block; AES_BoxParseKey parse_key; @@ -135,18 +101,15 @@ typedef struct AES_BoxGetBlockSize get_block_size; AES_BoxStoreBlock store_block; AES_BoxLoadBlock load_block; -} -AES_BoxAlgorithmInterface; +} AES_BoxAlgorithmInterface; -typedef struct -{ +typedef struct { const AES_BoxAlgorithmInterface* algorithm; AES_BoxEncryptionRoundKeys encryption_keys; AES_BoxDecryptionRoundKeys decryption_keys; AES_Mode mode; AES_BoxBlock iv; -} -AES_Box; +} AES_Box; #ifdef __cplusplus } diff --git a/aes/include/aes/data.h b/aes/include/aes/data.h index a441939..71b339a 100644 --- a/aes/include/aes/data.h +++ b/aes/include/aes/data.h @@ -9,8 +9,7 @@ #include <tmmintrin.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif /** @@ -25,9 +24,8 @@ typedef __m128i AES_Block128; * * \return The loaded 128-bit block. */ -static __inline AES_Block128 aes_load_block128(const void* src) -{ - return _mm_loadu_si128((AES_Block128*) src); +static __inline AES_Block128 aes_load_block128(const void* src) { + return _mm_loadu_si128((AES_Block128*)src); } /** @@ -37,9 +35,8 @@ static __inline AES_Block128 aes_load_block128(const void* src) * * \return The loaded 128-bit block. */ -static __inline AES_Block128 aes_load_block128_aligned(const void* src) -{ - return _mm_load_si128((AES_Block128*) src); +static __inline AES_Block128 aes_load_block128_aligned(const void* src) { + return _mm_load_si128((AES_Block128*)src); } /** @@ -49,11 +46,8 @@ static __inline AES_Block128 aes_load_block128_aligned(const void* src) * * \param[in] block The block to be stored. */ -static __inline void __fastcall aes_store_block128( - void* dest, - AES_Block128 block) -{ - _mm_storeu_si128((AES_Block128*) dest, block); +static __inline void __fastcall aes_store_block128(void* dest, AES_Block128 block) { + _mm_storeu_si128((AES_Block128*)dest, block); } /** @@ -63,11 +57,8 @@ static __inline void __fastcall aes_store_block128( * * \param[in] block The block to be stored. */ -static __inline void __fastcall aes_store_block128_aligned( - void* dest, - AES_Block128 block) -{ - _mm_store_si128((AES_Block128*) dest, block); +static __inline void __fastcall aes_store_block128_aligned(void* dest, AES_Block128 block) { + _mm_store_si128((AES_Block128*)dest, block); } /** @@ -78,10 +69,7 @@ static __inline void __fastcall aes_store_block128_aligned( * * \return `a^b`. */ -static __inline AES_Block128 __fastcall aes_xor_block128( - AES_Block128 a, - AES_Block128 b) -{ +static __inline AES_Block128 __fastcall aes_xor_block128(AES_Block128 a, AES_Block128 b) { return _mm_xor_si128(a, b); } @@ -102,18 +90,16 @@ static __inline AES_Block128 __fastcall aes_xor_block128( * * \return The built 128-bit block. */ -static __inline AES_Block128 __fastcall aes_make_block128(int hi3, int hi2, int lo1, int lo0) -{ +static __inline AES_Block128 __fastcall aes_make_block128(int hi3, int hi2, int lo1, int lo0) { return _mm_set_epi32(hi3, hi2, lo1, lo0); } -static __inline AES_Block128 __fastcall aes_reverse_byte_order_block128(AES_Block128 block) -{ - return _mm_shuffle_epi8(block, aes_make_block128(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f)); +static __inline AES_Block128 __fastcall aes_reverse_byte_order_block128(AES_Block128 block) { + return _mm_shuffle_epi8(block, + aes_make_block128(0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f)); } -static __inline AES_Block128 __fastcall aes_inc_block128(AES_Block128 x) -{ +static __inline AES_Block128 __fastcall aes_inc_block128(AES_Block128 x) { return _mm_add_epi32(x, aes_make_block128(0, 0, 0, 1)); } diff --git a/aes/include/aes/error.h b/aes/include/aes/error.h index e094f8a..2c93ab0 100644 --- a/aes/include/aes/error.h +++ b/aes/include/aes/error.h @@ -8,12 +8,10 @@ #include <stdlib.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -typedef enum -{ +typedef enum { AES_SUCCESS, AES_NULL_ARGUMENT_ERROR, AES_PARSE_ERROR, @@ -21,11 +19,9 @@ typedef enum AES_NOT_IMPLEMENTED_ERROR, AES_MISSING_PADDING_ERROR, AES_MEMORY_ALLOCATION_ERROR, -} -AES_StatusCode; +} AES_StatusCode; -static __inline int aes_is_error(AES_StatusCode ec) -{ +static __inline int aes_is_error(AES_StatusCode ec) { return ec != AES_SUCCESS; } @@ -33,63 +29,45 @@ const char* aes_strerror(AES_StatusCode ec); #define AES_MAX_CALL_STACK_LENGTH 32 -typedef struct -{ +typedef struct { AES_StatusCode ec; ///< Error code - union - { - struct { char param_name[32]; } null_arg; - struct - { + union { + struct { + char param_name[32]; + } null_arg; + struct { char src[128]; char what[32]; - } - parse_error; - struct { char what[128]; } not_implemented; - } - params; + } parse_error; + struct { + char what[128]; + } not_implemented; + } params; void* call_stack[AES_MAX_CALL_STACK_LENGTH]; size_t call_stack_len; -} -AES_ErrorDetails; +} AES_ErrorDetails; -static __inline AES_StatusCode aes_get_error_code( - const AES_ErrorDetails* err_details) -{ +static __inline AES_StatusCode aes_get_error_code(const AES_ErrorDetails* err_details) { return err_details->ec; } -size_t aes_format_error( - const AES_ErrorDetails* err_details, - char* dest, - size_t dest_size); +size_t aes_format_error(const AES_ErrorDetails* err_details, char* dest, size_t dest_size); -AES_StatusCode aes_success( - AES_ErrorDetails* err_details); +AES_StatusCode aes_success(AES_ErrorDetails* err_details); -AES_StatusCode aes_error_null_argument( - AES_ErrorDetails* err_details, - const char* param_name); +AES_StatusCode aes_error_null_argument(AES_ErrorDetails* err_details, const char* param_name); -AES_StatusCode aes_error_parse( - AES_ErrorDetails* err_details, - const char* src, - const char* what); +AES_StatusCode aes_error_parse(AES_ErrorDetails* err_details, const char* src, const char* what); -AES_StatusCode aes_error_invalid_pkcs7_padding( - AES_ErrorDetails* err_details); +AES_StatusCode aes_error_invalid_pkcs7_padding(AES_ErrorDetails* err_details); -AES_StatusCode aes_error_not_implemented( - AES_ErrorDetails* err_details, - const char* what); +AES_StatusCode aes_error_not_implemented(AES_ErrorDetails* err_details, const char* what); -AES_StatusCode aes_error_missing_padding( - AES_ErrorDetails* err_details); +AES_StatusCode aes_error_missing_padding(AES_ErrorDetails* err_details); -AES_StatusCode aes_error_memory_allocation( - AES_ErrorDetails* err_details); +AES_StatusCode aes_error_memory_allocation(AES_ErrorDetails* err_details); #ifdef __cplusplus } diff --git a/aes/include/aes/mode.h b/aes/include/aes/mode.h index 090628b..4c5e931 100644 --- a/aes/include/aes/mode.h +++ b/aes/include/aes/mode.h @@ -8,158 +8,143 @@ #include <assert.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -typedef enum -{ +typedef enum { AES_ECB, AES_CBC, AES_CFB, AES_OFB, AES_CTR, -} -AES_Mode; +} AES_Mode; -#define AES_ENCRYPT_BLOCK_ECB(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_ECB( \ - AES_## prefix ##_Block plaintext, \ - const AES_## prefix ##_RoundKeys* encryption_keys) \ -{ \ - assert(encryption_keys); \ -\ - return aes_## prefix ##_encrypt_block_(plaintext, encryption_keys); \ -} +#define AES_ENCRYPT_BLOCK_ECB(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_encrypt_block_ECB( \ + AES_##prefix##_Block plaintext, const AES_##prefix##_RoundKeys* encryption_keys) { \ + assert(encryption_keys); \ + \ + return aes_##prefix##_encrypt_block_(plaintext, encryption_keys); \ + } -#define AES_DECRYPT_BLOCK_ECB(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_ECB( \ - AES_## prefix ##_Block ciphertext, \ - const AES_## prefix ##_RoundKeys* decryption_keys) \ -{ \ - assert(decryption_keys); \ -\ - return aes_## prefix ##_decrypt_block_(ciphertext, decryption_keys); \ -} +#define AES_DECRYPT_BLOCK_ECB(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_decrypt_block_ECB( \ + AES_##prefix##_Block ciphertext, const AES_##prefix##_RoundKeys* decryption_keys) { \ + assert(decryption_keys); \ + \ + return aes_##prefix##_decrypt_block_(ciphertext, decryption_keys); \ + } -#define AES_ENCRYPT_BLOCK_CBC(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CBC( \ - AES_## prefix ##_Block plaintext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return *next_init_vector = aes_## prefix ##_encrypt_block_( \ - aes_## prefix ##_xor_blocks(plaintext, init_vector), encryption_keys); \ -} +#define AES_ENCRYPT_BLOCK_CBC(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_encrypt_block_CBC( \ + AES_##prefix##_Block plaintext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + return *next_init_vector = aes_##prefix##_encrypt_block_( \ + aes_##prefix##_xor_blocks(plaintext, init_vector), encryption_keys); \ + } -#define AES_DECRYPT_BLOCK_CBC(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CBC( \ - AES_## prefix ##_Block ciphertext, \ - const AES_## prefix ##_RoundKeys* decryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(decryption_keys); \ - assert(next_init_vector); \ -\ - AES_## prefix ##_Block plaintext = aes_## prefix ##_xor_blocks( \ - aes_## prefix ##_decrypt_block_(ciphertext, decryption_keys), init_vector); \ - *next_init_vector = ciphertext; \ - return plaintext; \ -} +#define AES_DECRYPT_BLOCK_CBC(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_decrypt_block_CBC( \ + AES_##prefix##_Block ciphertext, \ + const AES_##prefix##_RoundKeys* decryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(decryption_keys); \ + assert(next_init_vector); \ + \ + AES_##prefix##_Block plaintext = aes_##prefix##_xor_blocks( \ + aes_##prefix##_decrypt_block_(ciphertext, decryption_keys), init_vector); \ + *next_init_vector = ciphertext; \ + return plaintext; \ + } -#define AES_ENCRYPT_BLOCK_CFB(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CFB( \ - AES_## prefix ##_Block plaintext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return *next_init_vector = aes_## prefix ##_xor_blocks( \ - aes_## prefix ##_encrypt_block_(init_vector, encryption_keys), plaintext); \ -} +#define AES_ENCRYPT_BLOCK_CFB(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_encrypt_block_CFB( \ + AES_##prefix##_Block plaintext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + return *next_init_vector = aes_##prefix##_xor_blocks( \ + aes_##prefix##_encrypt_block_(init_vector, encryption_keys), plaintext); \ + } -#define AES_DECRYPT_BLOCK_CFB(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CFB( \ - AES_## prefix ##_Block ciphertext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - AES_## prefix ##_Block plaintext = aes_## prefix ##_xor_blocks( \ - aes_## prefix ##_encrypt_block_(init_vector, encryption_keys), ciphertext); \ - *next_init_vector = ciphertext; \ - return plaintext; \ -} +#define AES_DECRYPT_BLOCK_CFB(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_decrypt_block_CFB( \ + AES_##prefix##_Block ciphertext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + AES_##prefix##_Block plaintext = aes_##prefix##_xor_blocks( \ + aes_##prefix##_encrypt_block_(init_vector, encryption_keys), ciphertext); \ + *next_init_vector = ciphertext; \ + return plaintext; \ + } -#define AES_ENCRYPT_BLOCK_OFB(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_OFB( \ - AES_## prefix ##_Block plaintext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - AES_## prefix ##_Block tmp = aes_## prefix ##_encrypt_block_(init_vector, encryption_keys); \ - *next_init_vector = tmp; \ - return aes_## prefix ##_xor_blocks(tmp, plaintext); \ -} +#define AES_ENCRYPT_BLOCK_OFB(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_encrypt_block_OFB( \ + AES_##prefix##_Block plaintext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + AES_##prefix##_Block tmp = aes_##prefix##_encrypt_block_(init_vector, encryption_keys); \ + *next_init_vector = tmp; \ + return aes_##prefix##_xor_blocks(tmp, plaintext); \ + } -#define AES_DECRYPT_BLOCK_OFB(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_OFB( \ - AES_## prefix ##_Block ciphertext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return aes_## prefix ##_encrypt_block_OFB( \ - ciphertext, encryption_keys, init_vector, next_init_vector); \ -} +#define AES_DECRYPT_BLOCK_OFB(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_decrypt_block_OFB( \ + AES_##prefix##_Block ciphertext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + return aes_##prefix##_encrypt_block_OFB( \ + ciphertext, encryption_keys, init_vector, next_init_vector); \ + } -#define AES_ENCRYPT_BLOCK_CTR(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CTR( \ - AES_## prefix ##_Block plaintext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - AES_## prefix ##_Block ciphertext = aes_## prefix ##_xor_blocks( \ - plaintext, aes_## prefix ##_encrypt_block_(init_vector, encryption_keys)); \ - *next_init_vector = aes_## prefix ##_inc_block(init_vector); \ - return ciphertext; \ -} +#define AES_ENCRYPT_BLOCK_CTR(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_encrypt_block_CTR( \ + AES_##prefix##_Block plaintext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + AES_##prefix##_Block ciphertext = aes_##prefix##_xor_blocks( \ + plaintext, aes_##prefix##_encrypt_block_(init_vector, encryption_keys)); \ + *next_init_vector = aes_##prefix##_inc_block(init_vector); \ + return ciphertext; \ + } -#define AES_DECRYPT_BLOCK_CTR(prefix) \ -static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CTR( \ - AES_## prefix ##_Block ciphertext, \ - const AES_## prefix ##_RoundKeys* encryption_keys, \ - AES_## prefix ##_Block init_vector, \ - AES_## prefix ##_Block* next_init_vector) \ -{ \ - assert(encryption_keys); \ - assert(next_init_vector); \ -\ - return aes_## prefix ##_encrypt_block_CTR( \ - ciphertext, encryption_keys, init_vector, next_init_vector); \ -} +#define AES_DECRYPT_BLOCK_CTR(prefix) \ + static __inline AES_##prefix##_Block __fastcall aes_##prefix##_decrypt_block_CTR( \ + AES_##prefix##_Block ciphertext, \ + const AES_##prefix##_RoundKeys* encryption_keys, \ + AES_##prefix##_Block init_vector, \ + AES_##prefix##_Block* next_init_vector) { \ + assert(encryption_keys); \ + assert(next_init_vector); \ + \ + return aes_##prefix##_encrypt_block_CTR( \ + ciphertext, encryption_keys, init_vector, next_init_vector); \ + } #ifdef __cplusplus } diff --git a/aes/include/aes/padding.h b/aes/include/aes/padding.h index ba316f5..727196b 100644 --- a/aes/include/aes/padding.h +++ b/aes/include/aes/padding.h @@ -10,28 +10,23 @@ #include <stdlib.h> #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -typedef enum -{ +typedef enum { AES_PADDING_PKCS7, -} -AES_PaddingMethod; +} AES_PaddingMethod; -AES_StatusCode aes_extract_padding_size( - AES_PaddingMethod, - const void* src, - size_t src_size, - size_t* padding_size, - AES_ErrorDetails*); +AES_StatusCode aes_extract_padding_size(AES_PaddingMethod, + const void* src, + size_t src_size, + size_t* padding_size, + AES_ErrorDetails*); -AES_StatusCode aes_fill_with_padding( - AES_PaddingMethod, - void* dest, - size_t padding_size, - AES_ErrorDetails*); +AES_StatusCode aes_fill_with_padding(AES_PaddingMethod, + void* dest, + size_t padding_size, + AES_ErrorDetails*); #ifdef __cplusplus } diff --git a/aes/include/aes/workarounds.h b/aes/include/aes/workarounds.h index 914bd21..c3ffae7 100644 --- a/aes/include/aes/workarounds.h +++ b/aes/include/aes/workarounds.h @@ -13,4 +13,4 @@ #warning "couldn't determine alignment attribute" #endif -#define AES_UNUSED_PARAMETER(...) (void) (__VA_ARGS__) +#define AES_UNUSED_PARAMETER(...) (void)(__VA_ARGS__) diff --git a/aes/src/aes.c b/aes/src/aes.c index 1f7da13..a409b5e 100644 --- a/aes/src/aes.c +++ b/aes/src/aes.c @@ -11,11 +11,9 @@ #include <stdio.h> #include <string.h> -AES_StatusCode aes_AES_format_block( - AES_AES_BlockString* str, - const AES_AES_Block* block, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES_format_block(AES_AES_BlockString* str, + const AES_AES_Block* block, + AES_ErrorDetails* err_details) { assert(str); assert(block); @@ -36,11 +34,9 @@ AES_StatusCode aes_AES_format_block( return AES_SUCCESS; } -AES_StatusCode aes_AES_format_block_as_matrix( - AES_AES_BlockMatrixString* str, - const AES_AES_Block* block, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES_format_block_as_matrix(AES_AES_BlockMatrixString* str, + const AES_AES_Block* block, + AES_ErrorDetails* err_details) { assert(str); assert(block); @@ -54,8 +50,7 @@ AES_StatusCode aes_AES_format_block_as_matrix( AES_ALIGN(unsigned char, 16) bytes[4][4]; aes_store_block128_aligned(bytes, *block); - for (int i = 0; i < 4; ++i, cursor += 3) - { + 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]); @@ -65,10 +60,7 @@ AES_StatusCode aes_AES_format_block_as_matrix( return AES_SUCCESS; } -AES_StatusCode aes_AES_print_block( - const AES_AES_Block* block, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES_print_block(const AES_AES_Block* block, AES_ErrorDetails* err_details) { assert(block); if (block == NULL) @@ -84,10 +76,8 @@ AES_StatusCode aes_AES_print_block( return ec; } -AES_StatusCode aes_AES_print_block_as_matrix( - const AES_AES_Block* block, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES_print_block_as_matrix(const AES_AES_Block* block, + AES_ErrorDetails* err_details) { assert(block); if (block == NULL) @@ -103,11 +93,9 @@ AES_StatusCode aes_AES_print_block_as_matrix( return ec; } -AES_StatusCode aes_AES_parse_block( - AES_AES_Block* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES_parse_block(AES_AES_Block* dest, + const char* src, + AES_ErrorDetails* err_details) { assert(dest); assert(src); @@ -120,13 +108,12 @@ AES_StatusCode aes_AES_parse_block( AES_ALIGN(unsigned char, 16) bytes[16]; - for (int i = 0; i < 16; ++i) - { + 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; + bytes[i] = (unsigned char)byte; cursor += n; } @@ -134,11 +121,9 @@ AES_StatusCode aes_AES_parse_block( return AES_SUCCESS; } -AES_StatusCode aes_AES128_format_key( - AES_AES128_KeyString* str, - const AES_AES128_Key* key, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES128_format_key(AES_AES128_KeyString* str, + const AES_AES128_Key* key, + AES_ErrorDetails* err_details) { assert(str); assert(key); @@ -159,11 +144,9 @@ AES_StatusCode aes_AES128_format_key( return AES_SUCCESS; } -AES_StatusCode aes_AES192_format_key( - AES_AES192_KeyString* str, - const AES_AES192_Key* key, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES192_format_key(AES_AES192_KeyString* str, + const AES_AES192_Key* key, + AES_ErrorDetails* err_details) { assert(str); assert(key); @@ -194,11 +177,9 @@ AES_StatusCode aes_AES192_format_key( return AES_SUCCESS; } -AES_StatusCode aes_AES256_format_key( - AES_AES256_KeyString* str, - const AES_AES256_Key* key, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES256_format_key(AES_AES256_KeyString* str, + const AES_AES256_Key* key, + AES_ErrorDetails* err_details) { assert(str); assert(key); @@ -229,17 +210,11 @@ AES_StatusCode aes_AES256_format_key( return AES_SUCCESS; } -AES_StatusCode aes_AES128_print_key( - const AES_AES128_Key* key, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES128_print_key(const AES_AES128_Key* key, AES_ErrorDetails* err_details) { return aes_AES_print_block(&key->key, err_details); } -AES_StatusCode aes_AES192_print_key( - const AES_AES192_Key* key, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES192_print_key(const AES_AES192_Key* key, AES_ErrorDetails* err_details) { assert(key); if (key == NULL) @@ -255,10 +230,7 @@ AES_StatusCode aes_AES192_print_key( return ec; } -AES_StatusCode aes_AES256_print_key( - const AES_AES256_Key* key, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES256_print_key(const AES_AES256_Key* key, AES_ErrorDetails* err_details) { assert(key); if (key == NULL) @@ -274,19 +246,15 @@ AES_StatusCode aes_AES256_print_key( return ec; } -AES_StatusCode aes_AES128_parse_key( - AES_AES128_Key* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES128_parse_key(AES_AES128_Key* dest, + const char* src, + AES_ErrorDetails* err_details) { return aes_AES_parse_block(&dest->key, src, err_details); } -AES_StatusCode aes_AES192_parse_key( - AES_AES192_Key* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES192_parse_key(AES_AES192_Key* dest, + const char* src, + AES_ErrorDetails* err_details) { assert(dest); assert(src); @@ -300,13 +268,12 @@ AES_StatusCode aes_AES192_parse_key( { AES_ALIGN(unsigned char, 16) bytes[16]; - for (int i = 0; i < 16; ++i) - { + 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 192-bit block"); - bytes[i] = (unsigned char) byte; + bytes[i] = (unsigned char)byte; cursor += n; } @@ -316,13 +283,12 @@ AES_StatusCode aes_AES192_parse_key( { AES_ALIGN(unsigned char, 16) bytes[16]; - for (int i = 0; i < 8; ++i) - { + for (int i = 0; i < 8; ++i) { int n; unsigned int byte; if (sscanf(cursor, "%2x%n", &byte, &n) != 1) return aes_error_parse(err_details, src, "a 192-bit block"); - bytes[i] = (unsigned char) byte; + bytes[i] = (unsigned char)byte; cursor += n; } @@ -333,11 +299,9 @@ AES_StatusCode aes_AES192_parse_key( return AES_SUCCESS; } -AES_StatusCode aes_AES256_parse_key( - AES_AES256_Key* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_AES256_parse_key(AES_AES256_Key* dest, + const char* src, + AES_ErrorDetails* err_details) { assert(dest); assert(src); @@ -351,13 +315,12 @@ AES_StatusCode aes_AES256_parse_key( { AES_ALIGN(unsigned char, 16) bytes[16]; - for (int i = 0; i < 16; ++i) - { + 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 256-bit block"); - bytes[i] = (unsigned char) byte; + bytes[i] = (unsigned char)byte; cursor += n; } @@ -367,13 +330,12 @@ AES_StatusCode aes_AES256_parse_key( { AES_ALIGN(unsigned char, 16) bytes[16]; - for (int i = 0; i < 16; ++i) - { + 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 256-bit block"); - bytes[i] = (unsigned char) byte; + bytes[i] = (unsigned char)byte; cursor += n; } diff --git a/aes/src/box.c b/aes/src/box.c index c7e1e90..48457c9 100644 --- a/aes/src/box.c +++ b/aes/src/box.c @@ -10,30 +10,24 @@ #include <stdlib.h> #include <string.h> -static const AES_BoxAlgorithmInterface* aes_box_algorithms[] = -{ +static const AES_BoxAlgorithmInterface* aes_box_algorithms[] = { &aes_box_algorithm_aes128, &aes_box_algorithm_aes192, &aes_box_algorithm_aes256, }; -AES_StatusCode aes_box_init( - AES_Box* box, - AES_Algorithm algorithm, - const AES_BoxKey* box_key, - AES_Mode mode, - const AES_BoxBlock* iv, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_box_init(AES_Box* box, + AES_Algorithm algorithm, + const AES_BoxKey* box_key, + AES_Mode mode, + const AES_BoxBlock* iv, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; box->algorithm = aes_box_algorithms[algorithm]; if (aes_is_error(status = box->algorithm->calc_round_keys( - box_key, - &box->encryption_keys, - &box->decryption_keys, - err_details))) + box_key, &box->encryption_keys, &box->decryption_keys, err_details))) return status; box->mode = mode; @@ -43,109 +37,91 @@ AES_StatusCode aes_box_init( return status; } -static AES_StatusCode aes_box_encrypt_block_ecb( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ - return box->algorithm->encrypt_block( - input, &box->encryption_keys, output, err_details); +static AES_StatusCode aes_box_encrypt_block_ecb(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { + return box->algorithm->encrypt_block(input, &box->encryption_keys, output, err_details); } -static AES_StatusCode aes_box_encrypt_block_cbc( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_cbc(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; AES_BoxBlock xored_input = *input; - if (aes_is_error(status = box->algorithm->xor_block( - &xored_input, &box->iv, err_details))) + if (aes_is_error(status = box->algorithm->xor_block(&xored_input, &box->iv, err_details))) return status; if (aes_is_error(status = box->algorithm->encrypt_block( - &xored_input, &box->encryption_keys, output, err_details))) + &xored_input, &box->encryption_keys, output, err_details))) return status; box->iv = *output; return status; } -static AES_StatusCode aes_box_encrypt_block_cfb( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_cfb(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (aes_is_error(status = box->algorithm->encrypt_block( - &box->iv, &box->encryption_keys, output, err_details))) + &box->iv, &box->encryption_keys, output, err_details))) return status; - if (aes_is_error(status = box->algorithm->xor_block( - output, input, err_details))) + if (aes_is_error(status = box->algorithm->xor_block(output, input, err_details))) return status; box->iv = *output; return status; } -static AES_StatusCode aes_box_encrypt_block_ofb( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_ofb(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (aes_is_error(status = box->algorithm->encrypt_block( - &box->iv, &box->encryption_keys, &box->iv, err_details))) + &box->iv, &box->encryption_keys, &box->iv, err_details))) return status; *output = box->iv; - if (aes_is_error(status = box->algorithm->xor_block( - output, input, err_details))) + if (aes_is_error(status = box->algorithm->xor_block(output, input, err_details))) return status; return status; } -static AES_StatusCode aes_box_encrypt_block_ctr( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_ctr(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (aes_is_error(status = box->algorithm->encrypt_block( - &box->iv, &box->encryption_keys, output, err_details))) + &box->iv, &box->encryption_keys, output, err_details))) return status; - if (aes_is_error(status = box->algorithm->xor_block( - output, input, err_details))) + if (aes_is_error(status = box->algorithm->xor_block(output, input, err_details))) return status; - if (aes_is_error(status = box->algorithm->inc_block( - &box->iv, err_details))) + if (aes_is_error(status = box->algorithm->inc_block(&box->iv, err_details))) return status; return status; } -typedef AES_StatusCode (*AES_BoxEncryptBlockInMode)( - AES_Box*, - const AES_BoxBlock*, - AES_BoxBlock*, - AES_ErrorDetails*); +typedef AES_StatusCode (*AES_BoxEncryptBlockInMode)(AES_Box*, + const AES_BoxBlock*, + AES_BoxBlock*, + AES_ErrorDetails*); -static AES_BoxEncryptBlockInMode aes_box_encrypt_block_in_mode[] = -{ +static AES_BoxEncryptBlockInMode aes_box_encrypt_block_in_mode[] = { &aes_box_encrypt_block_ecb, &aes_box_encrypt_block_cbc, &aes_box_encrypt_block_cfb, @@ -153,60 +129,48 @@ static AES_BoxEncryptBlockInMode aes_box_encrypt_block_in_mode[] = &aes_box_encrypt_block_ctr, }; -AES_StatusCode aes_box_encrypt_block( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ - return aes_box_encrypt_block_in_mode[box->mode]( - box, input, output, err_details); +AES_StatusCode aes_box_encrypt_block(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { + return aes_box_encrypt_block_in_mode[box->mode](box, input, output, err_details); } -static AES_StatusCode aes_box_decrypt_block_ecb( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ - return box->algorithm->decrypt_block( - input, &box->decryption_keys, output, err_details); +static AES_StatusCode aes_box_decrypt_block_ecb(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { + return box->algorithm->decrypt_block(input, &box->decryption_keys, output, err_details); } -static AES_StatusCode aes_box_decrypt_block_cbc( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_block_cbc(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (aes_is_error(status = box->algorithm->decrypt_block( - input, &box->decryption_keys, output, err_details))) + input, &box->decryption_keys, output, err_details))) return status; - if (aes_is_error(status = box->algorithm->xor_block( - output, &box->iv, err_details))) + if (aes_is_error(status = box->algorithm->xor_block(output, &box->iv, err_details))) return status; box->iv = *input; return status; } -static AES_StatusCode aes_box_decrypt_block_cfb( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_block_cfb(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (aes_is_error(status = box->algorithm->encrypt_block( - &box->iv, &box->encryption_keys, output, err_details))) + &box->iv, &box->encryption_keys, output, err_details))) return status; - if (aes_is_error(status = box->algorithm->xor_block( - output, input, err_details))) + if (aes_is_error(status = box->algorithm->xor_block(output, input, err_details))) return status; box->iv = *input; @@ -215,8 +179,7 @@ static AES_StatusCode aes_box_decrypt_block_cfb( typedef AES_BoxEncryptBlockInMode AES_BoxDecryptBlockInMode; -static AES_BoxDecryptBlockInMode aes_box_decrypt_block_in_mode[] = -{ +static AES_BoxDecryptBlockInMode aes_box_decrypt_block_in_mode[] = { &aes_box_decrypt_block_ecb, &aes_box_decrypt_block_cbc, &aes_box_decrypt_block_cfb, @@ -224,34 +187,26 @@ static AES_BoxDecryptBlockInMode aes_box_decrypt_block_in_mode[] = &aes_box_encrypt_block_ctr, }; -AES_StatusCode aes_box_decrypt_block( - AES_Box* box, - const AES_BoxBlock* input, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ - return aes_box_decrypt_block_in_mode[box->mode]( - box, input, output, err_details); +AES_StatusCode aes_box_decrypt_block(AES_Box* box, + const AES_BoxBlock* input, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { + return aes_box_decrypt_block_in_mode[box->mode](box, input, output, err_details); } -static AES_StatusCode aes_box_get_encrypted_buffer_size( - AES_Box* box, - size_t src_size, - size_t* dest_size, - size_t* padding_size, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_get_encrypted_buffer_size(AES_Box* box, + size_t src_size, + size_t* dest_size, + size_t* padding_size, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; - switch (box->mode) - { + switch (box->mode) { case AES_ECB: - case AES_CBC: - { + case AES_CBC: { size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; *padding_size = block_size - src_size % block_size; @@ -267,33 +222,27 @@ static AES_StatusCode aes_box_get_encrypted_buffer_size( return status; default: - return aes_error_not_implemented( - err_details, "unsupported mode of operation"); + return aes_error_not_implemented(err_details, "unsupported mode of operation"); } } -static AES_StatusCode aes_box_encrypt_buffer_block( - AES_Box* box, - const void* src, - void* dest, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_buffer_block(AES_Box* box, + const void* src, + void* dest, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; AES_BoxBlock plaintext; - if (aes_is_error(status = box->algorithm->load_block( - &plaintext, src, err_details))) + if (aes_is_error(status = box->algorithm->load_block(&plaintext, src, err_details))) return status; AES_BoxBlock ciphertext; - if (aes_is_error(status = aes_box_encrypt_block( - box, &plaintext, &ciphertext, err_details))) + if (aes_is_error(status = aes_box_encrypt_block(box, &plaintext, &ciphertext, err_details))) return status; - if (aes_is_error(status = box->algorithm->store_block( - dest, &ciphertext, err_details))) + if (aes_is_error(status = box->algorithm->store_block(dest, &ciphertext, err_details))) return status; return status; @@ -305,14 +254,12 @@ static AES_StatusCode aes_box_encrypt_buffer_partial_block_with_padding( size_t src_size, void* dest, size_t padding_size, - AES_ErrorDetails* err_details) -{ + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; void* plaintext_buf = malloc(block_size); @@ -322,15 +269,12 @@ static AES_StatusCode aes_box_encrypt_buffer_partial_block_with_padding( memcpy(plaintext_buf, src, src_size); - if (aes_is_error(status = aes_fill_with_padding( - AES_PADDING_PKCS7, - (char*) plaintext_buf + src_size, - padding_size, - err_details))) + if (aes_is_error( + status = aes_fill_with_padding( + AES_PADDING_PKCS7, (char*)plaintext_buf + src_size, padding_size, err_details))) goto FREE_PLAINTEXT_BUF; - if (aes_is_error(status = aes_box_encrypt_buffer_block( - box, plaintext_buf, dest, err_details))) + if (aes_is_error(status = aes_box_encrypt_buffer_block(box, plaintext_buf, dest, err_details))) goto FREE_PLAINTEXT_BUF; FREE_PLAINTEXT_BUF: @@ -339,13 +283,11 @@ FREE_PLAINTEXT_BUF: return status; } -static AES_StatusCode aes_box_encrypt_buffer_partial_block( - AES_Box* box, - const void* src, - size_t src_size, - void* dest, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_buffer_partial_block(AES_Box* box, + const void* src, + size_t src_size, + void* dest, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (src_size == 0) @@ -353,8 +295,7 @@ static AES_StatusCode aes_box_encrypt_buffer_partial_block( size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; void* plaintext_buf = malloc(block_size); @@ -367,14 +308,13 @@ static AES_StatusCode aes_box_encrypt_buffer_partial_block( void* ciphertext_buf = malloc(block_size); - if (ciphertext_buf == NULL) - { + if (ciphertext_buf == NULL) { status = aes_error_memory_allocation(err_details); goto FREE_PLAINTEXT_BUF; } - if (aes_is_error(status = aes_box_encrypt_buffer_block( - box, plaintext_buf, ciphertext_buf, err_details))) + if (aes_is_error( + status = aes_box_encrypt_buffer_block(box, plaintext_buf, ciphertext_buf, err_details))) goto FREE_CIPHERTEXT_BUF; memcpy(dest, ciphertext_buf, src_size); @@ -388,14 +328,12 @@ FREE_PLAINTEXT_BUF: return status; } -AES_StatusCode aes_box_encrypt_buffer( - AES_Box* box, - const void* src, - size_t src_size, - void* dest, - size_t* dest_size, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_box_encrypt_buffer(AES_Box* box, + const void* src, + size_t src_size, + void* dest, + size_t* dest_size, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (box == NULL) @@ -406,7 +344,7 @@ AES_StatusCode aes_box_encrypt_buffer( size_t padding_size = 0; if (aes_is_error(status = aes_box_get_encrypted_buffer_size( - box, src_size, dest_size, &padding_size, err_details))) + box, src_size, dest_size, &padding_size, err_details))) return status; if (dest == NULL) @@ -416,20 +354,17 @@ AES_StatusCode aes_box_encrypt_buffer( size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; const size_t src_len = src_size / block_size; - for (size_t i = 0; i < src_len; ++i) - { - if (aes_is_error(status = aes_box_encrypt_buffer_block( - box, src, dest, err_details))) + for (size_t i = 0; i < src_len; ++i) { + if (aes_is_error(status = aes_box_encrypt_buffer_block(box, src, dest, err_details))) return status; - src = (char*) src + block_size; - dest = (char*) dest + block_size; + src = (char*)src + block_size; + dest = (char*)dest + block_size; } if (padding_size == 0) @@ -440,24 +375,19 @@ AES_StatusCode aes_box_encrypt_buffer( box, src, src_size % block_size, dest, padding_size, err_details); } -static AES_StatusCode aes_box_get_decrypted_buffer_size( - AES_Box* box, - size_t src_size, - size_t* dest_size, - size_t* max_padding_size, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_get_decrypted_buffer_size(AES_Box* box, + size_t src_size, + size_t* dest_size, + size_t* max_padding_size, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; - switch (box->mode) - { + switch (box->mode) { case AES_ECB: - case AES_CBC: - { + case AES_CBC: { size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; if (src_size == 0 || src_size % block_size != 0) @@ -476,45 +406,37 @@ static AES_StatusCode aes_box_get_decrypted_buffer_size( return status; default: - return aes_error_not_implemented( - err_details, "unsupported mode of operation"); + return aes_error_not_implemented(err_details, "unsupported mode of operation"); } } -static AES_StatusCode aes_box_decrypt_buffer_block( - AES_Box* box, - const void* src, - void* dest, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_buffer_block(AES_Box* box, + const void* src, + void* dest, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; AES_BoxBlock ciphertext; - if (aes_is_error(status = box->algorithm->load_block( - &ciphertext, src, err_details))) + if (aes_is_error(status = box->algorithm->load_block(&ciphertext, src, err_details))) return status; AES_BoxBlock plaintext; - if (aes_is_error(status = aes_box_decrypt_block( - box, &ciphertext, &plaintext, err_details))) + if (aes_is_error(status = aes_box_decrypt_block(box, &ciphertext, &plaintext, err_details))) return status; - if (aes_is_error(status = box->algorithm->store_block( - dest, &plaintext, err_details))) + if (aes_is_error(status = box->algorithm->store_block(dest, &plaintext, err_details))) return status; return status; } -static AES_StatusCode aes_box_decrypt_buffer_partial_block( - AES_Box* box, - const void* src, - size_t src_size, - void* dest, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_buffer_partial_block(AES_Box* box, + const void* src, + size_t src_size, + void* dest, + AES_ErrorDetails* err_details) { AES_StatusCode status = AES_SUCCESS; if (src_size == 0) @@ -522,8 +444,7 @@ static AES_StatusCode aes_box_decrypt_buffer_partial_block( size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; void* ciphertext_buf = malloc(block_size); @@ -536,14 +457,13 @@ static AES_StatusCode aes_box_decrypt_buffer_partial_block( void* plaintext_buf = malloc(block_size); - if (plaintext_buf == NULL) - { + if (plaintext_buf == NULL) { status = aes_error_memory_allocation(err_details); goto FREE_CIPHERTEXT_BUF; } - if (aes_is_error(status = aes_box_decrypt_buffer_block( - box, ciphertext_buf, plaintext_buf, err_details))) + if (aes_is_error( + status = aes_box_decrypt_buffer_block(box, ciphertext_buf, plaintext_buf, err_details))) goto FREE_PLAINTEXT_BUF; memcpy(dest, plaintext_buf, src_size); @@ -557,14 +477,12 @@ FREE_CIPHERTEXT_BUF: return status; } -AES_StatusCode aes_box_decrypt_buffer( - AES_Box* box, - const void* src, - size_t src_size, - void* dest, - size_t* dest_size, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_box_decrypt_buffer(AES_Box* box, + const void* src, + size_t src_size, + void* dest, + size_t* dest_size, + AES_ErrorDetails* err_details) { if (box == NULL) return aes_error_null_argument(err_details, "box"); if (dest_size == NULL) @@ -574,7 +492,7 @@ AES_StatusCode aes_box_decrypt_buffer( size_t max_padding_size = 0; if (aes_is_error(status = aes_box_get_decrypted_buffer_size( - box, src_size, dest_size, &max_padding_size, err_details))) + box, src_size, dest_size, &max_padding_size, err_details))) return status; if (dest == NULL) @@ -584,37 +502,30 @@ AES_StatusCode aes_box_decrypt_buffer( size_t block_size; - if (aes_is_error(status = box->algorithm->get_block_size( - &block_size, err_details))) + if (aes_is_error(status = box->algorithm->get_block_size(&block_size, err_details))) return status; const size_t src_len = src_size / block_size; - for (size_t i = 0; i < src_len; ++i) - { - if (aes_is_error(status = aes_box_decrypt_buffer_block( - box, src, dest, err_details))) + for (size_t i = 0; i < src_len; ++i) { + if (aes_is_error(status = aes_box_decrypt_buffer_block(box, src, dest, err_details))) return status; - src = (char*) src + block_size; - dest = (char*) dest + block_size; + src = (char*)src + block_size; + dest = (char*)dest + block_size; } - if (max_padding_size == 0) - { + if (max_padding_size == 0) { return aes_box_decrypt_buffer_partial_block( box, src, src_size % block_size, dest, err_details); - } - else - { + } else { size_t padding_size; - if (aes_is_error(status = aes_extract_padding_size( - AES_PADDING_PKCS7, - (char*) dest - block_size, - block_size, - &padding_size, - err_details))) + if (aes_is_error(status = aes_extract_padding_size(AES_PADDING_PKCS7, + (char*)dest - block_size, + block_size, + &padding_size, + err_details))) return status; *dest_size -= padding_size; @@ -622,62 +533,50 @@ AES_StatusCode aes_box_decrypt_buffer( } } -AES_StatusCode aes_box_parse_block( - AES_BoxBlock* dest, - AES_Algorithm algorithm, - const char* src, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_box_parse_block(AES_BoxBlock* dest, + AES_Algorithm algorithm, + const char* src, + AES_ErrorDetails* err_details) { 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_algorithms[algorithm]->parse_block( - dest, src, err_details); + return aes_box_algorithms[algorithm]->parse_block(dest, src, 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_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_algorithms[algorithm]->parse_key( - dest, src, err_details); + return aes_box_algorithms[algorithm]->parse_key(dest, src, err_details); } -AES_StatusCode aes_box_format_block( - AES_BoxBlockString* dest, - AES_Algorithm algorithm, - const AES_BoxBlock* src, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_box_format_block(AES_BoxBlockString* dest, + AES_Algorithm algorithm, + const AES_BoxBlock* 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_algorithms[algorithm]->format_block( - dest, src, err_details); + return aes_box_algorithms[algorithm]->format_block(dest, src, 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_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_algorithms[algorithm]->format_key( - dest, src, err_details); + return aes_box_algorithms[algorithm]->format_key(dest, src, err_details); } diff --git a/aes/src/box_aes.c b/aes/src/box_aes.c index 4d08d10..7e78a2c 100644 --- a/aes/src/box_aes.c +++ b/aes/src/box_aes.c @@ -10,105 +10,80 @@ #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) -{ +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); - aes_AES128_expand_key_( - box_key->aes128_key.key, - &encryption_keys->aes128_encryption_keys); - aes_AES128_derive_decryption_keys_( - &encryption_keys->aes128_encryption_keys, - &decryption_keys->aes128_decryption_keys); + aes_AES128_expand_key_(box_key->aes128_key.key, &encryption_keys->aes128_encryption_keys); + aes_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) -{ +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); aes_AES192_expand_key_( - box_key->aes192_key.lo, - box_key->aes192_key.hi, - &encryption_keys->aes192_encryption_keys); - aes_AES192_derive_decryption_keys_( - &encryption_keys->aes192_encryption_keys, - &decryption_keys->aes192_decryption_keys); + box_key->aes192_key.lo, box_key->aes192_key.hi, &encryption_keys->aes192_encryption_keys); + aes_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) -{ +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); aes_AES256_expand_key_( - box_key->aes256_key.lo, - box_key->aes256_key.hi, - &encryption_keys->aes256_encryption_keys); - aes_AES256_derive_decryption_keys_( - &encryption_keys->aes256_encryption_keys, - &decryption_keys->aes256_decryption_keys); + box_key->aes256_key.lo, box_key->aes256_key.hi, &encryption_keys->aes256_encryption_keys); + aes_AES256_derive_decryption_keys_(&encryption_keys->aes256_encryption_keys, + &decryption_keys->aes256_decryption_keys); return AES_SUCCESS; } -static AES_StatusCode aes_box_parse_block_aes( - AES_BoxBlock* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_parse_block_aes(AES_BoxBlock* dest, + const char* src, + AES_ErrorDetails* err_details) { if (dest == NULL) return aes_error_null_argument(err_details, "dest"); return aes_AES_parse_block(&dest->aes_block, src, err_details); } -static AES_StatusCode aes_box_parse_key_aes128( - AES_BoxKey* dest, - const char* src, - AES_ErrorDetails* err_details) -{ +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 aes_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) -{ +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 aes_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) -{ +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 aes_AES256_parse_key(&dest->aes256_key, src, err_details); } -static AES_StatusCode aes_box_format_block_aes( - AES_BoxBlockString* dest, - const AES_BoxBlock* src, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_format_block_aes(AES_BoxBlockString* dest, + const AES_BoxBlock* src, + AES_ErrorDetails* err_details) { if (dest == NULL) return aes_error_null_argument(err_details, "dest"); if (src == NULL) @@ -117,11 +92,9 @@ static AES_StatusCode aes_box_format_block_aes( return aes_AES128_format_block(&dest->aes, &src->aes_block, err_details); } -static AES_StatusCode aes_box_format_key_aes128( - AES_BoxKeyString* dest, - const AES_BoxKey* src, - AES_ErrorDetails* 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) @@ -130,11 +103,9 @@ static AES_StatusCode aes_box_format_key_aes128( return aes_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) -{ +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) @@ -143,11 +114,9 @@ static AES_StatusCode aes_box_format_key_aes192( return aes_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) -{ +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) @@ -156,134 +125,104 @@ static AES_StatusCode aes_box_format_key_aes256( return aes_AES256_format_key(&dest->aes256, &src->aes256_key, err_details); } -static AES_StatusCode aes_box_xor_block_aes( - AES_BoxBlock* dest, - const AES_BoxBlock* src, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_xor_block_aes(AES_BoxBlock* dest, + const AES_BoxBlock* src, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); dest->aes_block = aes_AES_xor_blocks(dest->aes_block, src->aes_block); return AES_SUCCESS; } -static AES_StatusCode aes_box_inc_block_aes( - AES_BoxBlock* ctr, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_inc_block_aes(AES_BoxBlock* ctr, AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); ctr->aes_block = aes_AES_inc_block(ctr->aes_block); return AES_SUCCESS; } -static AES_StatusCode aes_box_get_block_size_aes( - size_t* block_size, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_get_block_size_aes(size_t* block_size, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); *block_size = 16; return AES_SUCCESS; } -static AES_StatusCode aes_box_store_block_aes( - void* dest, - const AES_BoxBlock* src, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_store_block_aes(void* dest, + const AES_BoxBlock* src, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); aes_store_block128(dest, src->aes_block); return AES_SUCCESS; } -static AES_StatusCode aes_box_load_block_aes( - AES_BoxBlock* dest, - const void* src, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_load_block_aes(AES_BoxBlock* dest, + const void* src, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); dest->aes_block = aes_load_block128(src); return AES_SUCCESS; } -static AES_StatusCode aes_box_encrypt_block_aes128( - const AES_BoxBlock* input, - const AES_BoxEncryptionRoundKeys* params, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_aes128(const AES_BoxBlock* input, + const AES_BoxEncryptionRoundKeys* params, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - output->aes_block = aes_AES128_encrypt_block_( - input->aes_block, - ¶ms->aes128_encryption_keys); + output->aes_block = + aes_AES128_encrypt_block_(input->aes_block, ¶ms->aes128_encryption_keys); return AES_SUCCESS; } -static AES_StatusCode aes_box_decrypt_block_aes128( - const AES_BoxBlock* input, - const AES_BoxDecryptionRoundKeys* params, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_block_aes128(const AES_BoxBlock* input, + const AES_BoxDecryptionRoundKeys* params, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - output->aes_block = aes_AES128_decrypt_block_( - input->aes_block, - ¶ms->aes128_decryption_keys); + output->aes_block = + aes_AES128_decrypt_block_(input->aes_block, ¶ms->aes128_decryption_keys); return AES_SUCCESS; } -static AES_StatusCode aes_box_encrypt_block_aes192( - const AES_BoxBlock* input, - const AES_BoxEncryptionRoundKeys* params, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_aes192(const AES_BoxBlock* input, + const AES_BoxEncryptionRoundKeys* params, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - output->aes_block = aes_AES192_encrypt_block_( - input->aes_block, - ¶ms->aes192_encryption_keys); + output->aes_block = + aes_AES192_encrypt_block_(input->aes_block, ¶ms->aes192_encryption_keys); return AES_SUCCESS; } -static AES_StatusCode aes_box_decrypt_block_aes192( - const AES_BoxBlock* input, - const AES_BoxDecryptionRoundKeys* params, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_block_aes192(const AES_BoxBlock* input, + const AES_BoxDecryptionRoundKeys* params, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - output->aes_block = aes_AES192_decrypt_block_( - input->aes_block, - ¶ms->aes192_decryption_keys); + output->aes_block = + aes_AES192_decrypt_block_(input->aes_block, ¶ms->aes192_decryption_keys); return AES_SUCCESS; } -static AES_StatusCode aes_box_encrypt_block_aes256( - const AES_BoxBlock* input, - const AES_BoxEncryptionRoundKeys* params, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_encrypt_block_aes256(const AES_BoxBlock* input, + const AES_BoxEncryptionRoundKeys* params, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - output->aes_block = aes_AES256_encrypt_block_( - input->aes_block, - ¶ms->aes256_encryption_keys); + output->aes_block = + aes_AES256_encrypt_block_(input->aes_block, ¶ms->aes256_encryption_keys); return AES_SUCCESS; } -static AES_StatusCode aes_box_decrypt_block_aes256( - const AES_BoxBlock* input, - const AES_BoxDecryptionRoundKeys* params, - AES_BoxBlock* output, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_box_decrypt_block_aes256(const AES_BoxBlock* input, + const AES_BoxDecryptionRoundKeys* params, + AES_BoxBlock* output, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - output->aes_block = aes_AES256_decrypt_block_( - input->aes_block, - ¶ms->aes256_decryption_keys); + output->aes_block = + aes_AES256_decrypt_block_(input->aes_block, ¶ms->aes256_decryption_keys); return AES_SUCCESS; } -AES_BoxAlgorithmInterface aes_box_algorithm_aes128 = -{ +AES_BoxAlgorithmInterface aes_box_algorithm_aes128 = { &aes_box_derive_params_aes128, &aes_box_parse_block_aes, &aes_box_parse_key_aes128, @@ -298,8 +237,7 @@ AES_BoxAlgorithmInterface aes_box_algorithm_aes128 = &aes_box_load_block_aes, }; -AES_BoxAlgorithmInterface aes_box_algorithm_aes192 = -{ +AES_BoxAlgorithmInterface aes_box_algorithm_aes192 = { &aes_box_derive_params_aes192, &aes_box_parse_block_aes, &aes_box_parse_key_aes192, @@ -314,8 +252,7 @@ AES_BoxAlgorithmInterface aes_box_algorithm_aes192 = &aes_box_load_block_aes, }; -AES_BoxAlgorithmInterface aes_box_algorithm_aes256 = -{ +AES_BoxAlgorithmInterface aes_box_algorithm_aes256 = { &aes_box_derive_params_aes256, &aes_box_parse_block_aes, &aes_box_parse_key_aes256, diff --git a/aes/src/c/aes128.c b/aes/src/c/aes128.c index a1bad40..6b3a96a 100644 --- a/aes/src/c/aes128.c +++ b/aes/src/c/aes128.c @@ -10,10 +10,8 @@ #include <emmintrin.h> #include <wmmintrin.h> -AES_AES_Block __fastcall aes_AES128_encrypt_block_( - AES_AES_Block plaintext, - const AES_AES128_RoundKeys* encryption_keys) -{ +AES_AES_Block __fastcall aes_AES128_encrypt_block_(AES_AES_Block plaintext, + const AES_AES128_RoundKeys* encryption_keys) { plaintext = _mm_xor_si128(plaintext, encryption_keys->keys[0]); plaintext = _mm_aesenc_si128(plaintext, encryption_keys->keys[1]); plaintext = _mm_aesenc_si128(plaintext, encryption_keys->keys[2]); @@ -27,10 +25,8 @@ AES_AES_Block __fastcall aes_AES128_encrypt_block_( return _mm_aesenclast_si128(plaintext, encryption_keys->keys[10]); } -AES_AES_Block __fastcall aes_AES128_decrypt_block_( - AES_AES_Block ciphertext, - const AES_AES128_RoundKeys* decryption_keys) -{ +AES_AES_Block __fastcall aes_AES128_decrypt_block_(AES_AES_Block ciphertext, + const AES_AES128_RoundKeys* decryption_keys) { ciphertext = _mm_xor_si128(ciphertext, decryption_keys->keys[0]); ciphertext = _mm_aesdec_si128(ciphertext, decryption_keys->keys[1]); ciphertext = _mm_aesdec_si128(ciphertext, decryption_keys->keys[2]); @@ -44,10 +40,8 @@ AES_AES_Block __fastcall aes_AES128_decrypt_block_( return _mm_aesdeclast_si128(ciphertext, decryption_keys->keys[10]); } -static AES_AES_Block __fastcall aes_aes128_expand_key_assist( - AES_AES_Block prev, - AES_AES_Block hwgen) -{ +static AES_AES_Block __fastcall aes_aes128_expand_key_assist(AES_AES_Block prev, + AES_AES_Block hwgen) { AES_AES_Block tmp = prev; tmp = _mm_slli_si128(tmp, 4); @@ -63,27 +57,32 @@ static AES_AES_Block __fastcall aes_aes128_expand_key_assist( return prev; } -void __fastcall aes_AES128_expand_key_( - AES_AES_Block key, - AES_AES128_RoundKeys* encryption_keys) -{ +void __fastcall aes_AES128_expand_key_(AES_AES_Block key, AES_AES128_RoundKeys* encryption_keys) { AES_Block128 prev = encryption_keys->keys[0] = key; - prev = encryption_keys->keys[1] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x01)); - prev = encryption_keys->keys[2] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x02)); - prev = encryption_keys->keys[3] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x04)); - prev = encryption_keys->keys[4] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x08)); - prev = encryption_keys->keys[5] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x10)); - prev = encryption_keys->keys[6] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x20)); - prev = encryption_keys->keys[7] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x40)); - prev = encryption_keys->keys[8] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x80)); - prev = encryption_keys->keys[9] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x1b)); - prev = encryption_keys->keys[10] = aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x36)); + prev = encryption_keys->keys[1] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x01)); + prev = encryption_keys->keys[2] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x02)); + prev = encryption_keys->keys[3] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x04)); + prev = encryption_keys->keys[4] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x08)); + prev = encryption_keys->keys[5] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x10)); + prev = encryption_keys->keys[6] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x20)); + prev = encryption_keys->keys[7] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x40)); + prev = encryption_keys->keys[8] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x80)); + prev = encryption_keys->keys[9] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x1b)); + prev = encryption_keys->keys[10] = + aes_aes128_expand_key_assist(prev, _mm_aeskeygenassist_si128(prev, 0x36)); } -void __fastcall aes_AES128_derive_decryption_keys_( - const AES_AES128_RoundKeys* encryption_keys, - AES_AES128_RoundKeys* decryption_keys) -{ +void __fastcall aes_AES128_derive_decryption_keys_(const AES_AES128_RoundKeys* encryption_keys, + AES_AES128_RoundKeys* decryption_keys) { decryption_keys->keys[0] = encryption_keys->keys[10]; decryption_keys->keys[1] = _mm_aesimc_si128(encryption_keys->keys[9]); decryption_keys->keys[2] = _mm_aesimc_si128(encryption_keys->keys[8]); diff --git a/aes/src/c/aes192.c b/aes/src/c/aes192.c index d661b78..01f6698 100644 --- a/aes/src/c/aes192.c +++ b/aes/src/c/aes192.c @@ -10,10 +10,8 @@ #include <emmintrin.h> #include <wmmintrin.h> -AES_AES_Block __fastcall aes_AES192_encrypt_block_( - AES_AES_Block plaintext, - const AES_AES192_RoundKeys* encryption_keys) -{ +AES_AES_Block __fastcall aes_AES192_encrypt_block_(AES_AES_Block plaintext, + const AES_AES192_RoundKeys* encryption_keys) { plaintext = _mm_xor_si128(plaintext, encryption_keys->keys[0]); plaintext = _mm_aesenc_si128(plaintext, encryption_keys->keys[1]); plaintext = _mm_aesenc_si128(plaintext, encryption_keys->keys[2]); @@ -29,10 +27,8 @@ AES_AES_Block __fastcall aes_AES192_encrypt_block_( return _mm_aesenclast_si128(plaintext, encryption_keys->keys[12]); } -AES_AES_Block __fastcall aes_AES192_decrypt_block_( - AES_AES_Block ciphertext, - const AES_AES192_RoundKeys* decryption_keys) -{ +AES_AES_Block __fastcall aes_AES192_decrypt_block_(AES_AES_Block ciphertext, + const AES_AES192_RoundKeys* decryption_keys) { ciphertext = _mm_xor_si128(ciphertext, decryption_keys->keys[0]); ciphertext = _mm_aesdec_si128(ciphertext, decryption_keys->keys[1]); ciphertext = _mm_aesdec_si128(ciphertext, decryption_keys->keys[2]); @@ -48,11 +44,9 @@ AES_AES_Block __fastcall aes_AES192_decrypt_block_( return _mm_aesdeclast_si128(ciphertext, decryption_keys->keys[12]); } -static void __fastcall aes_aes192_expand_key_assist( - AES_AES_Block* prev_lo, - AES_AES_Block* prev_hi, - AES_AES_Block hwgen) -{ +static void __fastcall aes_aes192_expand_key_assist(AES_AES_Block* prev_lo, + AES_AES_Block* prev_hi, + AES_AES_Block hwgen) { AES_AES_Block tmp = *prev_lo; tmp = _mm_slli_si128(tmp, 4); @@ -73,50 +67,54 @@ static void __fastcall aes_aes192_expand_key_assist( *prev_hi = _mm_xor_si128(*prev_hi, tmp); } -void __fastcall aes_AES192_expand_key_( - AES_AES_Block key_lo, - AES_AES_Block key_hi, - AES_AES192_RoundKeys* encryption_keys) -{ +void __fastcall aes_AES192_expand_key_(AES_AES_Block key_lo, + AES_AES_Block key_hi, + AES_AES192_RoundKeys* encryption_keys) { encryption_keys->keys[0] = key_lo; encryption_keys->keys[1] = key_hi; aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x01)); - encryption_keys->keys[1] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[1]), _mm_castsi128_pd(key_lo), 0)); - encryption_keys->keys[2] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); + encryption_keys->keys[1] = _mm_castpd_si128( + _mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[1]), _mm_castsi128_pd(key_lo), 0)); + encryption_keys->keys[2] = + _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x02)); encryption_keys->keys[3] = key_lo; encryption_keys->keys[4] = key_hi; aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x04)); - encryption_keys->keys[4] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[4]), _mm_castsi128_pd(key_lo), 0)); - encryption_keys->keys[5] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); + encryption_keys->keys[4] = _mm_castpd_si128( + _mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[4]), _mm_castsi128_pd(key_lo), 0)); + encryption_keys->keys[5] = + _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x08)); encryption_keys->keys[6] = key_lo; encryption_keys->keys[7] = key_hi; aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x10)); - encryption_keys->keys[7] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[7]), _mm_castsi128_pd(key_lo), 0)); - encryption_keys->keys[8] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); + encryption_keys->keys[7] = _mm_castpd_si128( + _mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[7]), _mm_castsi128_pd(key_lo), 0)); + encryption_keys->keys[8] = + _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x20)); encryption_keys->keys[9] = key_lo; encryption_keys->keys[10] = key_hi; aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x40)); - encryption_keys->keys[10] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[10]), _mm_castsi128_pd(key_lo), 0)); - encryption_keys->keys[11] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); + encryption_keys->keys[10] = _mm_castpd_si128( + _mm_shuffle_pd(_mm_castsi128_pd(encryption_keys->keys[10]), _mm_castsi128_pd(key_lo), 0)); + encryption_keys->keys[11] = + _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(key_lo), _mm_castsi128_pd(key_hi), 1)); aes_aes192_expand_key_assist(&key_lo, &key_hi, _mm_aeskeygenassist_si128(key_hi, 0x80)); encryption_keys->keys[12] = key_lo; } -void __fastcall aes_AES192_derive_decryption_keys_( - const AES_AES192_RoundKeys* encryption_keys, - AES_AES192_RoundKeys* decryption_keys) -{ +void __fastcall aes_AES192_derive_decryption_keys_(const AES_AES192_RoundKeys* encryption_keys, + AES_AES192_RoundKeys* decryption_keys) { decryption_keys->keys[0] = encryption_keys->keys[12]; decryption_keys->keys[1] = _mm_aesimc_si128(encryption_keys->keys[11]); decryption_keys->keys[2] = _mm_aesimc_si128(encryption_keys->keys[10]); diff --git a/aes/src/c/aes256.c b/aes/src/c/aes256.c index 2190322..ea64f0f 100644 --- a/aes/src/c/aes256.c +++ b/aes/src/c/aes256.c @@ -10,10 +10,8 @@ #include <emmintrin.h> #include <wmmintrin.h> -AES_AES_Block __fastcall aes_AES256_encrypt_block_( - AES_AES_Block plaintext, - const AES_AES256_RoundKeys* encryption_keys) -{ +AES_AES_Block __fastcall aes_AES256_encrypt_block_(AES_AES_Block plaintext, + const AES_AES256_RoundKeys* encryption_keys) { plaintext = _mm_xor_si128(plaintext, encryption_keys->keys[0]); plaintext = _mm_aesenc_si128(plaintext, encryption_keys->keys[1]); plaintext = _mm_aesenc_si128(plaintext, encryption_keys->keys[2]); @@ -31,10 +29,8 @@ AES_AES_Block __fastcall aes_AES256_encrypt_block_( return _mm_aesenclast_si128(plaintext, encryption_keys->keys[14]); } -AES_AES_Block __fastcall aes_AES256_decrypt_block_( - AES_AES_Block ciphertext, - const AES_AES256_RoundKeys* decryption_keys) -{ +AES_AES_Block __fastcall aes_AES256_decrypt_block_(AES_AES_Block ciphertext, + const AES_AES256_RoundKeys* decryption_keys) { ciphertext = _mm_xor_si128(ciphertext, decryption_keys->keys[0]); ciphertext = _mm_aesdec_si128(ciphertext, decryption_keys->keys[1]); ciphertext = _mm_aesdec_si128(ciphertext, decryption_keys->keys[2]); @@ -52,11 +48,9 @@ AES_AES_Block __fastcall aes_AES256_decrypt_block_( return _mm_aesdeclast_si128(ciphertext, decryption_keys->keys[14]); } -static AES_AES_Block __fastcall aes_aes256_expand_key_assist( - AES_AES_Block* prev_lo, - AES_AES_Block* prev_hi, - AES_AES_Block hwgen) -{ +static AES_AES_Block __fastcall aes_aes256_expand_key_assist(AES_AES_Block* prev_lo, + AES_AES_Block* prev_hi, + AES_AES_Block hwgen) { AES_AES_Block tmp = *prev_lo; tmp = _mm_slli_si128(tmp, 4); @@ -75,11 +69,9 @@ static AES_AES_Block __fastcall aes_aes256_expand_key_assist( return *prev_hi; } -void __fastcall aes_AES256_expand_key_( - AES_AES_Block key_lo, - AES_AES_Block key_hi, - AES_AES256_RoundKeys* encryption_keys) -{ +void __fastcall aes_AES256_expand_key_(AES_AES_Block key_lo, + AES_AES_Block key_hi, + AES_AES256_RoundKeys* encryption_keys) { AES_AES_Block prev_lo, prev_hi; AES_AES_Block hwgen; @@ -139,10 +131,8 @@ void __fastcall aes_AES256_expand_key_( encryption_keys->keys[14] = aes_aes256_expand_key_assist(&prev_lo, &prev_hi, hwgen); } -void __fastcall aes_AES256_derive_decryption_keys_( - const AES_AES256_RoundKeys* encryption_keys, - AES_AES256_RoundKeys* decryption_keys) -{ +void __fastcall aes_AES256_derive_decryption_keys_(const AES_AES256_RoundKeys* encryption_keys, + AES_AES256_RoundKeys* decryption_keys) { decryption_keys->keys[0] = encryption_keys->keys[14]; decryption_keys->keys[1] = _mm_aesimc_si128(encryption_keys->keys[13]); decryption_keys->keys[2] = _mm_aesimc_si128(encryption_keys->keys[12]); diff --git a/aes/src/error.c b/aes/src/error.c index d49c456..e498458 100644 --- a/aes/src/error.c +++ b/aes/src/error.c @@ -16,11 +16,10 @@ #include <stdarg.h> -#define snprintf c99_snprintf +#define snprintf c99_snprintf #define vsnprintf c99_vsnprintf -static int c99_vsnprintf(char *dest, size_t dest_size, const char *fmt, va_list ap) -{ +static int c99_vsnprintf(char* dest, size_t dest_size, const char* fmt, va_list ap) { int dest_len = -1; if (dest_size != 0) @@ -31,8 +30,7 @@ static int c99_vsnprintf(char *dest, size_t dest_size, const char *fmt, va_list return dest_len; } -static int c99_snprintf(char *dest, size_t dest_size, const char *fmt, ...) -{ +static int c99_snprintf(char* dest, size_t dest_size, const char* fmt, ...) { va_list ap; va_start(ap, fmt); @@ -44,14 +42,12 @@ static int c99_snprintf(char *dest, size_t dest_size, const char *fmt, ...) #endif -static void aes_fill_string(char* dest, size_t dest_size, const char* src) -{ +static void aes_fill_string(char* dest, size_t dest_size, const char* src) { strncpy(dest, src, dest_size); dest[dest_size - 1] = '\0'; } -static const char* aes_strerror_messages[] = -{ +static const char* aes_strerror_messages[] = { "Success", "Invalid argument value NULL", "Couldn't parse", @@ -61,16 +57,13 @@ static const char* aes_strerror_messages[] = "Couldn't allocate memory", }; -const char* aes_strerror(AES_StatusCode ec) -{ +const char* aes_strerror(AES_StatusCode ec) { return aes_strerror_messages[ec]; } -static size_t aes_format_error_strerror( - const AES_ErrorDetails* err_details,\ - char* dest, - size_t dest_size) -{ +static size_t aes_format_error_strerror(const AES_ErrorDetails* err_details, + char* dest, + size_t dest_size) { const AES_StatusCode ec = aes_get_error_code(err_details); const char* const msg = aes_strerror(ec); @@ -81,11 +74,9 @@ static size_t aes_format_error_strerror( return strlen(dest); } -static size_t aes_format_null_argument_error( - const AES_ErrorDetails* err_details, - char* dest, - size_t dest_size) -{ +static size_t aes_format_null_argument_error(const AES_ErrorDetails* err_details, + char* dest, + size_t dest_size) { static const char* const fmt = "Invalid argument value NULL for parameter '%s'"; const char* const param_name = err_details->params.null_arg.param_name; @@ -96,11 +87,9 @@ static size_t aes_format_null_argument_error( return strlen(dest); } -static size_t aes_format_parse_error( - const AES_ErrorDetails* err_details, - char* dest, - size_t dest_size) -{ +static size_t aes_format_parse_error(const AES_ErrorDetails* err_details, + char* dest, + size_t dest_size) { static const char* const fmt = "Couldn't parse '%s' (possibly not complete input) as %s"; const char* const src = err_details->params.parse_error.src; const char* const what = err_details->params.parse_error.what; @@ -112,11 +101,9 @@ static size_t aes_format_parse_error( return strlen(dest); } -static size_t aes_format_not_implemented_error( - const AES_ErrorDetails* err_details, - char* dest, - size_t dest_size) -{ +static size_t aes_format_not_implemented_error(const AES_ErrorDetails* err_details, + char* dest, + size_t dest_size) { static const char* const fmt = "Not implemented: %s"; const char* const src = err_details->params.not_implemented.what; @@ -129,8 +116,7 @@ static size_t aes_format_not_implemented_error( typedef size_t (*AES_ErrorFormatter)(const AES_ErrorDetails*, char*, size_t); -static AES_ErrorFormatter err_formatters[] = -{ +static AES_ErrorFormatter err_formatters[] = { &aes_format_error_strerror, &aes_format_null_argument_error, &aes_format_parse_error, @@ -140,11 +126,7 @@ static AES_ErrorFormatter err_formatters[] = &aes_format_error_strerror, }; -size_t aes_format_error( - const AES_ErrorDetails* err_details, - char* dest, - size_t dest_size) -{ +size_t aes_format_error(const AES_ErrorDetails* err_details, char* dest, size_t dest_size) { assert(err_details); return err_formatters[err_details->ec](err_details, dest, dest_size); @@ -153,22 +135,17 @@ size_t aes_format_error( #ifdef WIN32 #include <windows.h> -static void aes_collect_call_stack(AES_ErrorDetails* err_details) -{ - err_details->call_stack_len = CaptureStackBackTrace( - 1, AES_MAX_CALL_STACK_LENGTH, err_details->call_stack, NULL); +static void aes_collect_call_stack(AES_ErrorDetails* err_details) { + err_details->call_stack_len = + CaptureStackBackTrace(1, AES_MAX_CALL_STACK_LENGTH, err_details->call_stack, NULL); } #else -static void aes_collect_call_stack(AES_ErrorDetails* err_details) -{ +static void aes_collect_call_stack(AES_ErrorDetails* err_details) { err_details->call_stack_len = 0; } #endif -static AES_StatusCode aes_make_error( - AES_ErrorDetails* err_details, - AES_StatusCode ec) -{ +static AES_StatusCode aes_make_error(AES_ErrorDetails* err_details, AES_StatusCode ec) { if (err_details == NULL) return ec; @@ -178,74 +155,54 @@ static AES_StatusCode aes_make_error( return err_details->ec = ec; } -AES_StatusCode aes_success( - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_success(AES_ErrorDetails* err_details) { return aes_make_error(err_details, AES_SUCCESS); } -AES_StatusCode aes_error_null_argument( - AES_ErrorDetails* err_details, - const char* param_name) -{ +AES_StatusCode aes_error_null_argument(AES_ErrorDetails* err_details, const char* param_name) { AES_StatusCode status = aes_make_error(err_details, AES_NULL_ARGUMENT_ERROR); if (err_details != NULL) - aes_fill_string( - err_details->params.null_arg.param_name, - sizeof(err_details->params.null_arg.param_name), param_name); + aes_fill_string(err_details->params.null_arg.param_name, + sizeof(err_details->params.null_arg.param_name), + param_name); return status; } -AES_StatusCode aes_error_parse( - AES_ErrorDetails* err_details, - const char* src, - const char* what) -{ +AES_StatusCode aes_error_parse(AES_ErrorDetails* err_details, const char* src, const char* what) { AES_StatusCode status = aes_make_error(err_details, AES_PARSE_ERROR); - if (err_details != NULL) - { + if (err_details != NULL) { aes_fill_string( - err_details->params.parse_error.src, - sizeof(err_details->params.parse_error.src), src); - aes_fill_string( - err_details->params.parse_error.what, - sizeof(err_details->params.parse_error.what), what); + err_details->params.parse_error.src, sizeof(err_details->params.parse_error.src), src); + aes_fill_string(err_details->params.parse_error.what, + sizeof(err_details->params.parse_error.what), + what); } return status; } -AES_StatusCode aes_error_invalid_pkcs7_padding( - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_error_invalid_pkcs7_padding(AES_ErrorDetails* err_details) { return aes_make_error(err_details, AES_INVALID_PKCS7_PADDING_ERROR); } -AES_StatusCode aes_error_not_implemented( - AES_ErrorDetails* err_details, - const char* what) -{ +AES_StatusCode aes_error_not_implemented(AES_ErrorDetails* err_details, const char* what) { AES_StatusCode status = aes_make_error(err_details, AES_NOT_IMPLEMENTED_ERROR); if (err_details != NULL) - aes_fill_string( - err_details->params.not_implemented.what, - sizeof(err_details->params.not_implemented.what), what); + aes_fill_string(err_details->params.not_implemented.what, + sizeof(err_details->params.not_implemented.what), + what); return status; } -AES_StatusCode aes_error_missing_padding( - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_error_missing_padding(AES_ErrorDetails* err_details) { return aes_make_error(err_details, AES_MISSING_PADDING_ERROR); } -AES_StatusCode aes_error_memory_allocation( - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_error_memory_allocation(AES_ErrorDetails* err_details) { return aes_make_error(err_details, AES_MEMORY_ALLOCATION_ERROR); } diff --git a/aes/src/padding.c b/aes/src/padding.c index a161ec7..5647ea6 100644 --- a/aes/src/padding.c +++ b/aes/src/padding.c @@ -11,13 +11,11 @@ #include <stdlib.h> #include <string.h> -static AES_StatusCode aes_extract_padding_size_pkcs7( - const void* src, - size_t src_size, - size_t* padding_size, - AES_ErrorDetails* err_details) -{ - const unsigned char* cursor = (const unsigned char*) src + src_size - 1; +static AES_StatusCode aes_extract_padding_size_pkcs7(const void* src, + size_t src_size, + size_t* padding_size, + AES_ErrorDetails* err_details) { + const unsigned char* cursor = (const unsigned char*)src + src_size - 1; *padding_size = *cursor; for (size_t i = 1; i < *padding_size; ++i) @@ -27,13 +25,11 @@ static AES_StatusCode aes_extract_padding_size_pkcs7( return AES_SUCCESS; } -AES_StatusCode aes_extract_padding_size( - AES_PaddingMethod method, - const void* src, - size_t src_size, - size_t* padding_size, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_extract_padding_size(AES_PaddingMethod method, + const void* src, + size_t src_size, + size_t* padding_size, + AES_ErrorDetails* err_details) { assert(src); assert(padding_size); @@ -42,47 +38,37 @@ AES_StatusCode aes_extract_padding_size( if (padding_size == NULL) return aes_error_null_argument(err_details, "padding_size"); - switch (method) - { + switch (method) { case AES_PADDING_PKCS7: - return aes_extract_padding_size_pkcs7( - src, src_size, padding_size, err_details); + return aes_extract_padding_size_pkcs7(src, src_size, padding_size, err_details); default: - return aes_error_not_implemented( - err_details, "unsupported padding method"); + return aes_error_not_implemented(err_details, "unsupported padding method"); } } -static AES_StatusCode aes_fill_with_padding_pkcs7( - void* dest, - size_t padding_size, - AES_ErrorDetails* err_details) -{ +static AES_StatusCode aes_fill_with_padding_pkcs7(void* dest, + size_t padding_size, + AES_ErrorDetails* err_details) { AES_UNUSED_PARAMETER(err_details); - memset(dest, (int) padding_size, padding_size); + memset(dest, (int)padding_size, padding_size); return AES_SUCCESS; } -AES_StatusCode aes_fill_with_padding( - AES_PaddingMethod method, - void* dest, - size_t padding_size, - AES_ErrorDetails* err_details) -{ +AES_StatusCode aes_fill_with_padding(AES_PaddingMethod method, + void* dest, + size_t padding_size, + AES_ErrorDetails* err_details) { assert(dest); if (dest == NULL) return aes_error_null_argument(err_details, "dest"); - switch (method) - { + switch (method) { case AES_PADDING_PKCS7: - return aes_fill_with_padding_pkcs7( - dest, padding_size, err_details); + return aes_fill_with_padding_pkcs7(dest, padding_size, err_details); default: - return aes_error_not_implemented( - err_details, "unsupported padding method"); + return aes_error_not_implemented(err_details, "unsupported padding method"); } } diff --git a/aesxx/include/aesxx/aes.hpp b/aesxx/include/aesxx/aes.hpp index efac790..c3457c8 100644 --- a/aesxx/include/aesxx/aes.hpp +++ b/aesxx/include/aesxx/aes.hpp @@ -13,263 +13,226 @@ #include <aes/all.h> #include <cstddef> - #include <string> -namespace aes -{ - namespace aes128 - { - typedef AES_AES128_Block Block; - typedef AES_AES128_RoundKeys RoundKeys; - typedef AES_AES128_Key Key; - } +namespace aes { +namespace aes128 { - template <> - struct Types<AES_AES128> - { - typedef aes128::Block Block; - typedef aes128::RoundKeys RoundKeys; - typedef aes128::Key Key; - }; +typedef AES_AES128_Block Block; +typedef AES_AES128_RoundKeys RoundKeys; +typedef AES_AES128_Key Key; - template <> - inline std::size_t get_number_of_rounds<AES_AES128>() - { - return 11; - } +} // namespace aes128 - template <> - inline void from_string<AES_AES128>(aes128::Block& dest, const char* src) - { - aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); - } +template <> +struct Types<AES_AES128> { + typedef aes128::Block Block; + typedef aes128::RoundKeys RoundKeys; + typedef aes128::Key Key; +}; - template <> - inline std::string to_string<AES_AES128>(const aes128::Block& src) - { - AES_AES128_BlockString str; - aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +template <> +inline std::size_t get_number_of_rounds<AES_AES128>() { + return 11; +} - template <> - inline std::string to_matrix_string<AES_AES128>(const aes128::Block& src) - { - AES_AES128_BlockMatrixString str; - aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +template <> +inline void from_string<AES_AES128>(aes128::Block& dest, const char* src) { + aes_AES128_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +} - template <> - inline void from_string<AES_AES128>(aes128::Key& dest, const char* src) - { - aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); - } +template <> +inline std::string to_string<AES_AES128>(const aes128::Block& src) { + AES_AES128_BlockString str; + aes_AES128_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - template <> - inline std::string to_string<AES_AES128>(const aes128::Key& src) - { - AES_AES128_KeyString str; - aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +template <> +inline std::string to_matrix_string<AES_AES128>(const aes128::Block& src) { + AES_AES128_BlockMatrixString str; + aes_AES128_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - template <> - inline void expand_key<AES_AES128>( - const aes128::Key& key, - aes128::RoundKeys& encryption_keys) - { - aes_AES128_expand_key(&key, &encryption_keys); - } +template <> +inline void from_string<AES_AES128>(aes128::Key& dest, const char* src) { + aes_AES128_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +} - template <> - inline void derive_decryption_keys<AES_AES128>( - const aes128::RoundKeys& encryption_keys, - aes128::RoundKeys& decryption_keys) - { - aes_AES128_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } +template <> +inline std::string to_string<AES_AES128>(const aes128::Key& src) { + AES_AES128_KeyString str; + aes_AES128_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - AESXX_ENCRYPT_BLOCK_ECB(AES128); - AESXX_DECRYPT_BLOCK_ECB(AES128); - AESXX_ENCRYPT_BLOCK_CBC(AES128); - AESXX_DECRYPT_BLOCK_CBC(AES128); - AESXX_ENCRYPT_BLOCK_CFB(AES128); - AESXX_DECRYPT_BLOCK_CFB(AES128); - AESXX_ENCRYPT_BLOCK_OFB(AES128); - AESXX_DECRYPT_BLOCK_OFB(AES128); - AESXX_ENCRYPT_BLOCK_CTR(AES128); - AESXX_DECRYPT_BLOCK_CTR(AES128); +template <> +inline void expand_key<AES_AES128>(const aes128::Key& key, aes128::RoundKeys& encryption_keys) { + aes_AES128_expand_key(&key, &encryption_keys); +} - namespace aes192 - { - typedef AES_AES192_Block Block; - typedef AES_AES192_RoundKeys RoundKeys; - typedef AES_AES192_Key Key; - } +template <> +inline void derive_decryption_keys<AES_AES128>(const aes128::RoundKeys& encryption_keys, + aes128::RoundKeys& decryption_keys) { + aes_AES128_derive_decryption_keys(&encryption_keys, &decryption_keys); +} - template <> - struct Types<AES_AES192> - { - typedef aes192::Block Block; - typedef aes192::RoundKeys RoundKeys; - typedef aes192::Key Key; - }; +AESXX_ENCRYPT_BLOCK_ECB(AES128); +AESXX_DECRYPT_BLOCK_ECB(AES128); +AESXX_ENCRYPT_BLOCK_CBC(AES128); +AESXX_DECRYPT_BLOCK_CBC(AES128); +AESXX_ENCRYPT_BLOCK_CFB(AES128); +AESXX_DECRYPT_BLOCK_CFB(AES128); +AESXX_ENCRYPT_BLOCK_OFB(AES128); +AESXX_DECRYPT_BLOCK_OFB(AES128); +AESXX_ENCRYPT_BLOCK_CTR(AES128); +AESXX_DECRYPT_BLOCK_CTR(AES128); - template <> - inline std::size_t get_number_of_rounds<AES_AES192>() - { - return 13; - } +namespace aes192 { - template <> - inline void from_string<AES_AES192>(aes192::Block& dest, const char* src) - { - aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); - } +typedef AES_AES192_Block Block; +typedef AES_AES192_RoundKeys RoundKeys; +typedef AES_AES192_Key Key; - template <> - inline std::string to_string<AES_AES192>(const aes192::Block& src) - { - AES_AES192_BlockString str; - aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +} // namespace aes192 - template <> - inline std::string to_matrix_string<AES_AES192>(const aes192::Block& src) - { - AES_AES192_BlockMatrixString str; - aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +template <> +struct Types<AES_AES192> { + typedef aes192::Block Block; + typedef aes192::RoundKeys RoundKeys; + typedef aes192::Key Key; +}; - template <> - inline void from_string<AES_AES192>(aes192::Key& dest, const char* src) - { - aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); - } +template <> +inline std::size_t get_number_of_rounds<AES_AES192>() { + return 13; +} - template <> - inline std::string to_string<AES_AES192>(const aes192::Key& src) - { - AES_AES192_KeyString str; - aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +template <> +inline void from_string<AES_AES192>(aes192::Block& dest, const char* src) { + aes_AES192_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +} - template <> - inline void expand_key<AES_AES192>( - const aes192::Key& key, - aes192::RoundKeys& encryption_keys) - { - aes_AES192_expand_key(&key, &encryption_keys); - } +template <> +inline std::string to_string<AES_AES192>(const aes192::Block& src) { + AES_AES192_BlockString str; + aes_AES192_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - template <> - inline void derive_decryption_keys<AES_AES192>( - const aes192::RoundKeys& encryption_keys, - aes192::RoundKeys& decryption_keys) - { - aes_AES192_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } +template <> +inline std::string to_matrix_string<AES_AES192>(const aes192::Block& src) { + AES_AES192_BlockMatrixString str; + aes_AES192_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - AESXX_ENCRYPT_BLOCK_ECB(AES192); - AESXX_DECRYPT_BLOCK_ECB(AES192); - AESXX_ENCRYPT_BLOCK_CBC(AES192); - AESXX_DECRYPT_BLOCK_CBC(AES192); - AESXX_ENCRYPT_BLOCK_CFB(AES192); - AESXX_DECRYPT_BLOCK_CFB(AES192); - AESXX_ENCRYPT_BLOCK_OFB(AES192); - AESXX_DECRYPT_BLOCK_OFB(AES192); - AESXX_ENCRYPT_BLOCK_CTR(AES192); - AESXX_DECRYPT_BLOCK_CTR(AES192); +template <> +inline void from_string<AES_AES192>(aes192::Key& dest, const char* src) { + aes_AES192_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); +} - namespace aes256 - { - typedef AES_AES256_Block Block; - typedef AES_AES256_RoundKeys RoundKeys; - typedef AES_AES256_Key Key; - } +template <> +inline std::string to_string<AES_AES192>(const aes192::Key& src) { + AES_AES192_KeyString str; + aes_AES192_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - template <> - struct Types<AES_AES256> - { - typedef aes256::Block Block; - typedef aes256::RoundKeys RoundKeys; - typedef aes256::Key Key; - }; +template <> +inline void expand_key<AES_AES192>(const aes192::Key& key, aes192::RoundKeys& encryption_keys) { + aes_AES192_expand_key(&key, &encryption_keys); +} - template <> - inline std::size_t get_number_of_rounds<AES_AES256>() - { - return 15; - } +template <> +inline void derive_decryption_keys<AES_AES192>(const aes192::RoundKeys& encryption_keys, + aes192::RoundKeys& decryption_keys) { + aes_AES192_derive_decryption_keys(&encryption_keys, &decryption_keys); +} + +AESXX_ENCRYPT_BLOCK_ECB(AES192); +AESXX_DECRYPT_BLOCK_ECB(AES192); +AESXX_ENCRYPT_BLOCK_CBC(AES192); +AESXX_DECRYPT_BLOCK_CBC(AES192); +AESXX_ENCRYPT_BLOCK_CFB(AES192); +AESXX_DECRYPT_BLOCK_CFB(AES192); +AESXX_ENCRYPT_BLOCK_OFB(AES192); +AESXX_DECRYPT_BLOCK_OFB(AES192); +AESXX_ENCRYPT_BLOCK_CTR(AES192); +AESXX_DECRYPT_BLOCK_CTR(AES192); - template <> - inline void from_string<AES_AES256>(aes256::Block& dest, const char* src) - { - aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); - } +namespace aes256 { - template <> - inline std::string to_string<AES_AES256>(const aes256::Block& src) - { - AES_AES256_BlockString str; - aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +typedef AES_AES256_Block Block; +typedef AES_AES256_RoundKeys RoundKeys; +typedef AES_AES256_Key Key; - template <> - inline std::string to_matrix_string<AES_AES256>(const aes256::Block& src) - { - AES_AES256_BlockMatrixString str; - aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +} // namespace aes256 - template <> - inline void from_string<AES_AES256>(aes256::Key& dest, const char* src) - { - aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); - } +template <> +struct Types<AES_AES256> { + typedef aes256::Block Block; + typedef aes256::RoundKeys RoundKeys; + typedef aes256::Key Key; +}; + +template <> +inline std::size_t get_number_of_rounds<AES_AES256>() { + return 15; +} - template <> - inline std::string to_string<AES_AES256>(const aes256::Key& src) - { - AES_AES256_KeyString str; - aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); - return {str.str}; - } +template <> +inline void from_string<AES_AES256>(aes256::Block& dest, const char* src) { + aes_AES256_parse_block(&dest, src, ErrorDetailsThrowsInDestructor{}); +} - template <> - inline void expand_key<AES_AES256>( - const aes256::Key& key, - aes256::RoundKeys& encryption_keys) - { - aes_AES256_expand_key(&key, &encryption_keys); - } +template <> +inline std::string to_string<AES_AES256>(const aes256::Block& src) { + AES_AES256_BlockString str; + aes_AES256_format_block(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - template <> - inline void derive_decryption_keys<AES_AES256>( - const aes256::RoundKeys& encryption_keys, - aes256::RoundKeys& decryption_keys) - { - aes_AES256_derive_decryption_keys( - &encryption_keys, &decryption_keys); - } +template <> +inline std::string to_matrix_string<AES_AES256>(const aes256::Block& src) { + AES_AES256_BlockMatrixString str; + aes_AES256_format_block_as_matrix(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} - AESXX_ENCRYPT_BLOCK_ECB(AES256); - AESXX_DECRYPT_BLOCK_ECB(AES256); - AESXX_ENCRYPT_BLOCK_CBC(AES256); - AESXX_DECRYPT_BLOCK_CBC(AES256); - AESXX_ENCRYPT_BLOCK_CFB(AES256); - AESXX_DECRYPT_BLOCK_CFB(AES256); - AESXX_ENCRYPT_BLOCK_OFB(AES256); - AESXX_DECRYPT_BLOCK_OFB(AES256); - AESXX_ENCRYPT_BLOCK_CTR(AES256); - AESXX_DECRYPT_BLOCK_CTR(AES256); +template <> +inline void from_string<AES_AES256>(aes256::Key& dest, const char* src) { + aes_AES256_parse_key(&dest, src, ErrorDetailsThrowsInDestructor{}); } + +template <> +inline std::string to_string<AES_AES256>(const aes256::Key& src) { + AES_AES256_KeyString str; + aes_AES256_format_key(&str, &src, ErrorDetailsThrowsInDestructor{}); + return {str.str}; +} + +template <> +inline void expand_key<AES_AES256>(const aes256::Key& key, aes256::RoundKeys& encryption_keys) { + aes_AES256_expand_key(&key, &encryption_keys); +} + +template <> +inline void derive_decryption_keys<AES_AES256>(const aes256::RoundKeys& encryption_keys, + aes256::RoundKeys& decryption_keys) { + aes_AES256_derive_decryption_keys(&encryption_keys, &decryption_keys); +} + +AESXX_ENCRYPT_BLOCK_ECB(AES256); +AESXX_DECRYPT_BLOCK_ECB(AES256); +AESXX_ENCRYPT_BLOCK_CBC(AES256); +AESXX_DECRYPT_BLOCK_CBC(AES256); +AESXX_ENCRYPT_BLOCK_CFB(AES256); +AESXX_DECRYPT_BLOCK_CFB(AES256); +AESXX_ENCRYPT_BLOCK_OFB(AES256); +AESXX_DECRYPT_BLOCK_OFB(AES256); +AESXX_ENCRYPT_BLOCK_CTR(AES256); +AESXX_DECRYPT_BLOCK_CTR(AES256); + +} // namespace aes diff --git a/aesxx/include/aesxx/algorithm.hpp b/aesxx/include/aesxx/algorithm.hpp index 823e57f..933cd5a 100644 --- a/aesxx/include/aesxx/algorithm.hpp +++ b/aesxx/include/aesxx/algorithm.hpp @@ -7,7 +7,8 @@ #include <aes/all.h> -namespace aes -{ - typedef AES_Algorithm Algorithm; +namespace aes { + +typedef AES_Algorithm Algorithm; + } diff --git a/aesxx/include/aesxx/api.hpp b/aesxx/include/aesxx/api.hpp index 68112bf..759ff0c 100644 --- a/aesxx/include/aesxx/api.hpp +++ b/aesxx/include/aesxx/api.hpp @@ -9,166 +9,139 @@ #include "mode.hpp" #include <cstddef> - #include <string> #include <type_traits> -namespace aes -{ - template <Algorithm algorithm> - struct Types; +namespace aes { - template <Algorithm algorithm> - std::size_t get_number_of_rounds(); +template <Algorithm algorithm> +struct Types; - template <Algorithm algorithm> - void from_string( - typename Types<algorithm>::Block&, - const char*); +template <Algorithm algorithm> +std::size_t get_number_of_rounds(); - template <Algorithm algorithm> - void from_string( - typename Types<algorithm>::Block& dest, - const std::string& src) - { - from_string<algorithm>(dest, src.c_str()); - } +template <Algorithm algorithm> +void from_string(typename Types<algorithm>::Block&, const char*); - template <Algorithm algorithm> - std::string to_string(const typename Types<algorithm>::Block&); +template <Algorithm algorithm> +void from_string(typename Types<algorithm>::Block& dest, const std::string& src) { + from_string<algorithm>(dest, src.c_str()); +} - template <Algorithm algorithm> - std::string to_matrix_string(const typename Types<algorithm>::Block&); +template <Algorithm algorithm> +std::string to_string(const typename Types<algorithm>::Block&); - template <Algorithm algorithm> - void from_string( - typename Types<algorithm>::Key&, - const char*); +template <Algorithm algorithm> +std::string to_matrix_string(const typename Types<algorithm>::Block&); - template <Algorithm algorithm> - void from_string( - typename Types<algorithm>::Key& dest, - const std::string& src) - { - from_string<algorithm>(dest, src.c_str()); - } +template <Algorithm algorithm> +void from_string(typename Types<algorithm>::Key&, const char*); + +template <Algorithm algorithm> +void from_string(typename Types<algorithm>::Key& dest, const std::string& src) { + from_string<algorithm>(dest, src.c_str()); +} - template <Algorithm algorithm> - std::string to_string(const typename Types<algorithm>::Key&); +template <Algorithm algorithm> +std::string to_string(const typename Types<algorithm>::Key&); - template <Algorithm algorithm> - void expand_key( - const typename Types<algorithm>::Key& key, - typename Types<algorithm>::RoundKeys& encryption_keys); +template <Algorithm algorithm> +void expand_key(const typename Types<algorithm>::Key& key, + typename Types<algorithm>::RoundKeys& encryption_keys); - template <Algorithm algorithm> - void derive_decryption_keys( - const typename Types<algorithm>::RoundKeys& encryption_keys, - typename Types<algorithm>::RoundKeys& decryption_keys); +template <Algorithm algorithm> +void derive_decryption_keys(const typename Types<algorithm>::RoundKeys& encryption_keys, + typename Types<algorithm>::RoundKeys& decryption_keys); - template <Algorithm algorithm, Mode mode, - typename std::enable_if<ModeRequiresInitVector<mode>::value>::type* = nullptr> - void encrypt_block( - const typename Types<algorithm>::Block& plaintext, - const typename Types<algorithm>::RoundKeys& round_keys, - typename Types<algorithm>::Block& iv, - typename Types<algorithm>::Block& ciphertext); +template <Algorithm algorithm, + Mode mode, + typename std::enable_if<ModeRequiresInitVector<mode>::value>::type* = nullptr> +void encrypt_block(const typename Types<algorithm>::Block& plaintext, + const typename Types<algorithm>::RoundKeys& round_keys, + typename Types<algorithm>::Block& iv, + typename Types<algorithm>::Block& ciphertext); - template <Algorithm algorithm, Mode mode, - typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> - void encrypt_block( - const typename Types<algorithm>::Block& plaintext, - const typename Types<algorithm>::RoundKeys& round_keys, - typename Types<algorithm>::Block& ciphertext); +template <Algorithm algorithm, + Mode mode, + typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> +void encrypt_block(const typename Types<algorithm>::Block& plaintext, + const typename Types<algorithm>::RoundKeys& round_keys, + typename Types<algorithm>::Block& ciphertext); - template <Algorithm algorithm, Mode mode, - typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> - void encrypt_block( - const typename Types<algorithm>::Block& plaintext, - const typename Types<algorithm>::RoundKeys& round_keys, - typename Types<algorithm>::Block&, - typename Types<algorithm>::Block& ciphertext) - { - encrypt_block<algorithm, mode>(plaintext, round_keys, ciphertext); - } +template <Algorithm algorithm, + Mode mode, + typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> +void encrypt_block(const typename Types<algorithm>::Block& plaintext, + const typename Types<algorithm>::RoundKeys& round_keys, + typename Types<algorithm>::Block&, + typename Types<algorithm>::Block& ciphertext) { + encrypt_block<algorithm, mode>(plaintext, round_keys, ciphertext); +} - template <Algorithm algorithm, Mode mode, - typename std::enable_if<ModeRequiresInitVector<mode>::value>::type* = nullptr> - void decrypt_block( - const typename Types<algorithm>::Block& ciphertext, - const typename Types<algorithm>::RoundKeys& round_keys, - typename Types<algorithm>::Block& iv, - typename Types<algorithm>::Block& plaintext); +template <Algorithm algorithm, + Mode mode, + typename std::enable_if<ModeRequiresInitVector<mode>::value>::type* = nullptr> +void decrypt_block(const typename Types<algorithm>::Block& ciphertext, + const typename Types<algorithm>::RoundKeys& round_keys, + typename Types<algorithm>::Block& iv, + typename Types<algorithm>::Block& plaintext); - template <Algorithm algorithm, Mode mode, - typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> - void decrypt_block( - const typename Types<algorithm>::Block& ciphertext, - const typename Types<algorithm>::RoundKeys& round_keys, - typename Types<algorithm>::Block& plaintext); +template <Algorithm algorithm, + Mode mode, + typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> +void decrypt_block(const typename Types<algorithm>::Block& ciphertext, + const typename Types<algorithm>::RoundKeys& round_keys, + typename Types<algorithm>::Block& plaintext); + +template <Algorithm algorithm, + Mode mode, + typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> +void decrypt_block(const typename Types<algorithm>::Block& ciphertext, + const typename Types<algorithm>::RoundKeys& round_keys, + typename Types<algorithm>::Block&, + typename Types<algorithm>::Block& plaintext) { + decrypt_block<algorithm, mode>(ciphertext, round_keys, plaintext); +} - template <Algorithm algorithm, Mode mode, - typename std::enable_if<!ModeRequiresInitVector<mode>::value>::type* = nullptr> - void decrypt_block( - const typename Types<algorithm>::Block& ciphertext, - const typename Types<algorithm>::RoundKeys& round_keys, - typename Types<algorithm>::Block&, - typename Types<algorithm>::Block& plaintext) - { - decrypt_block<algorithm, mode>(ciphertext, round_keys, plaintext); +template <Algorithm algorithm, Mode mode> +struct EncryptWrapper { + EncryptWrapper(const typename Types<algorithm>::Key& key, + const typename Types<algorithm>::Block& iv) + : iv{iv} { + expand_key<algorithm>(key, encryption_keys); } - template <Algorithm algorithm, Mode mode> - struct EncryptWrapper - { - EncryptWrapper( - const typename Types<algorithm>::Key& key, - const typename Types<algorithm>::Block& iv) : iv{iv} - { - expand_key<algorithm>(key, encryption_keys); - } + void encrypt_block(const typename Types<algorithm>::Block& plaintext, + typename Types<algorithm>::Block& ciphertext) { + aes::encrypt_block<algorithm, mode>(plaintext, encryption_keys, iv, ciphertext); + } - void encrypt_block( - const typename Types<algorithm>::Block& plaintext, - typename Types<algorithm>::Block& ciphertext) - { - aes::encrypt_block<algorithm, mode>( - plaintext, encryption_keys, iv, ciphertext); - } + typename Types<algorithm>::Block iv; + typename Types<algorithm>::RoundKeys encryption_keys; +}; - typename Types<algorithm>::Block iv; +template <Algorithm algorithm, Mode mode> +struct DecryptWrapper { + DecryptWrapper(const typename Types<algorithm>::Key& key, + const typename Types<algorithm>::Block& iv) + : iv{iv} { typename Types<algorithm>::RoundKeys encryption_keys; - }; + expand_key<algorithm>(key, encryption_keys); - template <Algorithm algorithm, Mode mode> - struct DecryptWrapper - { - DecryptWrapper( - const typename Types<algorithm>::Key& key, - const typename Types<algorithm>::Block& iv) : iv{iv} - { - typename Types<algorithm>::RoundKeys encryption_keys; - expand_key<algorithm>(key, encryption_keys); - - if (ModeUsesEncryptionKeysOnly<mode>::value) - { - decryption_keys = encryption_keys; - } - else - { - derive_decryption_keys<algorithm>(encryption_keys, decryption_keys); - } + if (ModeUsesEncryptionKeysOnly<mode>::value) { + decryption_keys = encryption_keys; + } else { + derive_decryption_keys<algorithm>(encryption_keys, decryption_keys); } + } - void decrypt_block( - const typename Types<algorithm>::Block& ciphertext, - typename Types<algorithm>::Block& plaintext) - { - aes::decrypt_block<algorithm, mode>( - ciphertext, decryption_keys, iv, plaintext); - } + void decrypt_block(const typename Types<algorithm>::Block& ciphertext, + typename Types<algorithm>::Block& plaintext) { + aes::decrypt_block<algorithm, mode>(ciphertext, decryption_keys, iv, plaintext); + } - typename Types<algorithm>::Block iv; - typename Types<algorithm>::RoundKeys decryption_keys; - }; -} + typename Types<algorithm>::Block iv; + typename Types<algorithm>::RoundKeys decryption_keys; +}; + +} // namespace aes diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index 076407c..540688b 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -12,196 +12,140 @@ #include <aes/all.h> #include <cstddef> - #include <string> #include <vector> -namespace aes -{ - class Box - { - public: - typedef AES_BoxBlock Block; - typedef AES_BoxKey Key; +namespace aes { + +class Box { +public: + typedef AES_BoxBlock Block; + typedef AES_BoxKey Key; + + static std::string format_key(const Key& src, Algorithm algorithm) { + AES_BoxKeyString str; + aes_box_format_key(&str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast<const char*>(&str); + } - static std::string format_key(const Key& src, Algorithm algorithm) - { - AES_BoxKeyString str; - aes_box_format_key( - &str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast<const char*>(&str); - } + static std::string format_block(const Block& src, Algorithm algorithm) { + AES_BoxBlockString str; + aes_box_format_block(&str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast<const char*>(&str); + } - static std::string format_block(const Block& src, Algorithm algorithm) - { - AES_BoxBlockString str; - aes_box_format_block( - &str, algorithm, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast<const char*>(&str); - } + static void parse_block(Block& dest, Algorithm algorithm, const char* src) { + aes_box_parse_block(&dest, algorithm, src, ErrorDetailsThrowsInDestructor{}); + } - static void parse_block( - Block& dest, - Algorithm algorithm, - const char* src) - { - aes_box_parse_block(&dest, algorithm, src, - ErrorDetailsThrowsInDestructor{}); - } + static void parse_block(Block& dest, Algorithm algorithm, const std::string& src) { + parse_block(dest, algorithm, src.c_str()); + } - static void parse_block( - Block& dest, - Algorithm algorithm, - const std::string& src) - { - parse_block(dest, algorithm, src.c_str()); - } + static void parse_key(Key& dest, Algorithm algorithm, const char* src) { + aes_box_parse_key(&dest, algorithm, src, ErrorDetailsThrowsInDestructor{}); + } - static void parse_key( - Key& dest, - Algorithm algorithm, - const char* src) - { - aes_box_parse_key(&dest, algorithm, src, - ErrorDetailsThrowsInDestructor{}); - } + static void parse_key(Key& dest, Algorithm algorithm, const std::string& src) { + parse_key(dest, algorithm, src.c_str()); + } - static void parse_key( - Key& dest, - Algorithm algorithm, - const std::string& src) - { - parse_key(dest, algorithm, src.c_str()); - } + Box(Algorithm algorithm, const Key& key) : algorithm{algorithm}, mode{AES_ECB} { + aes_box_init(&impl, algorithm, &key, mode, nullptr, ErrorDetailsThrowsInDestructor{}); + } - Box(Algorithm algorithm, const Key& key) - : algorithm{algorithm} - , mode{AES_ECB} - { - aes_box_init(&impl, algorithm, &key, mode, nullptr, - ErrorDetailsThrowsInDestructor{}); - } + Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) + : algorithm{algorithm}, mode{mode} { + aes_box_init(&impl, algorithm, &key, mode, &iv, ErrorDetailsThrowsInDestructor{}); + } - Box(Algorithm algorithm, const Key& key, Mode mode, const Block& iv) - : algorithm{algorithm} - , mode{mode} - { - aes_box_init(&impl, algorithm, &key, mode, &iv, - ErrorDetailsThrowsInDestructor{}); - } + void encrypt_block(const Block& plaintext, Block& ciphertext) { + aes_box_encrypt_block(&impl, &plaintext, &ciphertext, ErrorDetailsThrowsInDestructor{}); + } - void encrypt_block(const Block& plaintext, Block& ciphertext) - { - aes_box_encrypt_block( - &impl, &plaintext, &ciphertext, - ErrorDetailsThrowsInDestructor{}); - } + void decrypt_block(const Block& ciphertext, Block& plaintext) { + aes_box_decrypt_block(&impl, &ciphertext, &plaintext, ErrorDetailsThrowsInDestructor{}); + } - void decrypt_block(const Block& ciphertext, Block& plaintext) - { - aes_box_decrypt_block( - &impl, &ciphertext, &plaintext, - ErrorDetailsThrowsInDestructor{}); - } + std::vector<unsigned char> encrypt_buffer(const void* src_buf, std::size_t src_size) { + std::size_t dest_size = 0; - std::vector<unsigned char> encrypt_buffer( - const void* src_buf, - std::size_t src_size) - { - std::size_t dest_size = 0; + aes_box_encrypt_buffer( + &impl, src_buf, src_size, nullptr, &dest_size, aes::ErrorDetailsThrowsInDestructor{}); - aes_box_encrypt_buffer( - &impl, - src_buf, - src_size, - nullptr, - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); + std::vector<unsigned char> dest_buf; + dest_buf.resize(dest_size); - std::vector<unsigned char> dest_buf; - dest_buf.resize(dest_size); + aes_box_encrypt_buffer(&impl, + src_buf, + src_size, + dest_buf.data(), + &dest_size, + aes::ErrorDetailsThrowsInDestructor{}); - aes_box_encrypt_buffer( - &impl, - src_buf, - src_size, - dest_buf.data(), - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); + dest_buf.resize(dest_size); + return dest_buf; + } - dest_buf.resize(dest_size); - return dest_buf; - } + std::vector<unsigned char> decrypt_buffer(const void* src_buf, std::size_t src_size) { + std::size_t dest_size = 0; - std::vector<unsigned char> decrypt_buffer( - const void* src_buf, - std::size_t src_size) - { - std::size_t dest_size = 0; + aes_box_decrypt_buffer( + &impl, src_buf, src_size, nullptr, &dest_size, aes::ErrorDetailsThrowsInDestructor{}); - aes_box_decrypt_buffer( - &impl, - src_buf, - src_size, - nullptr, - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); + std::vector<unsigned char> dest_buf; + dest_buf.resize(dest_size); - std::vector<unsigned char> dest_buf; - dest_buf.resize(dest_size); + aes_box_decrypt_buffer(&impl, + src_buf, + src_size, + dest_buf.data(), + &dest_size, + aes::ErrorDetailsThrowsInDestructor{}); - aes_box_decrypt_buffer( - &impl, - src_buf, - src_size, - dest_buf.data(), - &dest_size, - aes::ErrorDetailsThrowsInDestructor{}); + dest_buf.resize(dest_size); + return dest_buf; + } - dest_buf.resize(dest_size); - return dest_buf; - } + std::string format_block(const Block& src) { + return format_block(src, get_algorithm()); + } - std::string format_block(const Block& src) - { - return format_block(src, get_algorithm()); - } + std::string format_key(const Key& src) { + return format_key(src, get_algorithm()); + } - std::string format_key(const Key& src) - { - return format_key(src, get_algorithm()); - } + void parse_block(Block& dest, const char* src) { + parse_block(dest, get_algorithm(), src); + } - void parse_block(Block& dest, const char* src) - { - parse_block(dest, get_algorithm(), src); - } + void parse_block(Block& dest, const std::string& src) { + parse_block(dest, src.c_str()); + } - void parse_block(Block& dest, const std::string& src) - { - parse_block(dest, src.c_str()); - } + void parse_key(Key& dest, const char* src) { + parse_key(dest, get_algorithm(), src); + } - void parse_key(Key& dest, const char* src) - { - parse_key(dest, get_algorithm(), src); - } + void parse_key(Key& dest, const std::string& src) { + parse_key(dest, src.c_str()); + } - void parse_key(Key& dest, const std::string& src) - { - parse_key(dest, src.c_str()); - } + Algorithm get_algorithm() const { + return algorithm; + } - Algorithm get_algorithm() const { return algorithm; } + Mode get_mode() const { + return mode; + } - Mode get_mode() const { return mode; } +private: + Key key; - private: - Key key; + Algorithm algorithm; + Mode mode; - Algorithm algorithm; - Mode mode; + AES_Box impl; +}; - AES_Box impl; - }; -} +} // namespace aes diff --git a/aesxx/include/aesxx/data.hpp b/aesxx/include/aesxx/data.hpp index f36df5e..e381995 100644 --- a/aesxx/include/aesxx/data.hpp +++ b/aesxx/include/aesxx/data.hpp @@ -9,47 +9,40 @@ #include <aes/all.h> -namespace aes -{ - typedef AES_Block128 Block128; +namespace aes { - inline void make_block(Block128& dest, int hi3, int hi2, int lo1, int lo0) - { - dest = aes_make_block128(hi3, hi2, lo1, lo0); - } +typedef AES_Block128 Block128; - inline void load_block(Block128& dest, const void* src) - { - dest = aes_load_block128(src); - } +inline void make_block(Block128& dest, int hi3, int hi2, int lo1, int lo0) { + dest = aes_make_block128(hi3, hi2, lo1, lo0); +} - inline void load_block_aligned(Block128& dest, const void* src) - { - dest = aes_load_block128_aligned(src); - } +inline void load_block(Block128& dest, const void* src) { + dest = aes_load_block128(src); +} - inline void store_block(void* dest, Block128& src) - { - aes_store_block128(dest, src); - } +inline void load_block_aligned(Block128& dest, const void* src) { + dest = aes_load_block128_aligned(src); +} - inline void store_block_aligned(void* dest, Block128& src) - { - aes_store_block128_aligned(dest, src); - } +inline void store_block(void* dest, Block128& src) { + aes_store_block128(dest, src); +} - inline Block128 xor_blocks(Block128& a, Block128& b) - { - return aes_xor_block128(a, b); - } +inline void store_block_aligned(void* dest, Block128& src) { + aes_store_block128_aligned(dest, src); +} - inline Block128 reverse_byte_order(Block128& block) - { - return aes_reverse_byte_order_block128(block); - } +inline Block128 xor_blocks(Block128& a, Block128& b) { + return aes_xor_block128(a, b); +} + +inline Block128 reverse_byte_order(Block128& block) { + return aes_reverse_byte_order_block128(block); +} - inline Block128 inc_block(Block128& block) - { - return aes_inc_block128(block); - } +inline Block128 inc_block(Block128& block) { + return aes_inc_block128(block); } + +} // namespace aes diff --git a/aesxx/include/aesxx/debug.hpp b/aesxx/include/aesxx/debug.hpp index d7d5d15..d54605d 100644 --- a/aesxx/include/aesxx/debug.hpp +++ b/aesxx/include/aesxx/debug.hpp @@ -7,168 +7,134 @@ #ifdef WIN32 #include <windows.h> + #include <dbghelp.h> #pragma comment(lib, "dbghelp.lib") #endif #include <cstddef> - #include <sstream> #include <string> -namespace aes -{ - namespace aux - { - class CallStackFormatter - { - public: - CallStackFormatter() = default; +namespace aes { +namespace aux { - std::string format_address(const void* addr) const - { - #ifdef WIN32 - return format_address_win32(addr); - #else - return format_address_fallback(addr); - #endif - } +class CallStackFormatter { +public: + CallStackFormatter() = default; - private: - template <typename T> - static std::string put_between_brackets(const T& x) - { - std::ostringstream oss; - oss << "[" << x << "]"; - return oss.str(); - } + std::string format_address(const void* addr) const { +#ifdef WIN32 + return format_address_win32(addr); +#else + return format_address_fallback(addr); +#endif + } - template <typename T> - static std::string stringify(const T& x) - { - std::ostringstream oss; - oss << x; - return oss.str(); - } +private: + template <typename T> + static std::string put_between_brackets(const T& x) { + std::ostringstream oss; + oss << "[" << x << "]"; + return oss.str(); + } - static std::string format_address_fallback(const void* addr) - { - return put_between_brackets(addr); - } + template <typename T> + static std::string stringify(const T& x) { + std::ostringstream oss; + oss << x; + return oss.str(); + } - static std::string format_module( - const std::string& module_name, - const void* offset = nullptr) - { - if (offset == nullptr) - return put_between_brackets(module_name); - else - return put_between_brackets(module_name + "+" + stringify(offset)); - } + static std::string format_address_fallback(const void* addr) { + return put_between_brackets(addr); + } - static std::string format_symbol( - const std::string& symbol_name, - const void* offset = nullptr) - { - return format_module(symbol_name, offset); - } + static std::string format_module(const std::string& module_name, const void* offset = nullptr) { + if (offset == nullptr) + return put_between_brackets(module_name); + else + return put_between_brackets(module_name + "+" + stringify(offset)); + } - static std::string format_symbol( - const std::string& module_name, - const std::string& symbol_name, - const void* offset = nullptr) - { - return format_symbol(module_name + "!" + symbol_name, offset); - } + static std::string format_symbol(const std::string& symbol_name, const void* offset = nullptr) { + return format_module(symbol_name, offset); + } + + static std::string format_symbol(const std::string& module_name, + const std::string& symbol_name, + const void* offset = nullptr) { + return format_symbol(module_name + "!" + symbol_name, offset); + } - #ifdef WIN32 - class DbgHelp - { - public: - DbgHelp() - { - initialized_flag = SymInitialize(GetCurrentProcess(), NULL, TRUE) != FALSE; - } +#ifdef WIN32 + class DbgHelp { + public: + DbgHelp() { + initialized_flag = SymInitialize(GetCurrentProcess(), NULL, TRUE) != FALSE; + } - bool initialized() const - { - return initialized_flag; - } + bool initialized() const { + return initialized_flag; + } - ~DbgHelp() - { - if (initialized_flag) - SymCleanup(GetCurrentProcess()); - } + ~DbgHelp() { + if (initialized_flag) + SymCleanup(GetCurrentProcess()); + } - private: - bool initialized_flag = false; + private: + bool initialized_flag = false; - DbgHelp(const DbgHelp&) = delete; - DbgHelp& operator=(const DbgHelp&) = delete; - }; + DbgHelp(const DbgHelp&) = delete; + DbgHelp& operator=(const DbgHelp&) = delete; + }; - DbgHelp dbghelp; + DbgHelp dbghelp; - std::string format_address_win32(const void* addr) const - { - if (!dbghelp.initialized()) - return format_address_fallback(addr); + std::string format_address_win32(const void* addr) const { + if (!dbghelp.initialized()) + return format_address_fallback(addr); - DWORD64 symbol_info_buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME]; - const auto symbol_info = reinterpret_cast<SYMBOL_INFO*>(symbol_info_buf); - symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); - symbol_info->MaxNameLen = MAX_SYM_NAME; + DWORD64 symbol_info_buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME]; + const auto symbol_info = reinterpret_cast<SYMBOL_INFO*>(symbol_info_buf); + symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); + symbol_info->MaxNameLen = MAX_SYM_NAME; - IMAGEHLP_MODULE64 module_info; - module_info.SizeOfStruct = sizeof(IMAGEHLP_MODULE64); + IMAGEHLP_MODULE64 module_info; + module_info.SizeOfStruct = sizeof(IMAGEHLP_MODULE64); - DWORD64 symbol_offset; + DWORD64 symbol_offset; - const auto symbol_resolved = SymFromAddr( - GetCurrentProcess(), - reinterpret_cast<DWORD64>(addr), - &symbol_offset, - symbol_info); + const auto symbol_resolved = SymFromAddr( + GetCurrentProcess(), reinterpret_cast<DWORD64>(addr), &symbol_offset, symbol_info); - if (symbol_resolved) - { - const auto module_resolved = SymGetModuleInfo64( - GetCurrentProcess(), - symbol_info->ModBase, - &module_info); + if (symbol_resolved) { + const auto module_resolved = + SymGetModuleInfo64(GetCurrentProcess(), symbol_info->ModBase, &module_info); - if (module_resolved) - { - return format_symbol( - module_info.ModuleName, - symbol_info->Name, - reinterpret_cast<const void*>(symbol_offset)); - } - else - { - return format_symbol(symbol_info->Name, addr); - } - } - else - { - const auto module_resolved = SymGetModuleInfo64( - GetCurrentProcess(), - reinterpret_cast<DWORD64>(addr), - &module_info); + if (module_resolved) { + return format_symbol(module_info.ModuleName, + symbol_info->Name, + reinterpret_cast<const void*>(symbol_offset)); + } else { + return format_symbol(symbol_info->Name, addr); + } + } else { + const auto module_resolved = SymGetModuleInfo64( + GetCurrentProcess(), reinterpret_cast<DWORD64>(addr), &module_info); - if (module_resolved) - { - const auto module_offset = reinterpret_cast<const char*>(addr) - module_info.BaseOfImage; - return format_module(module_info.ModuleName, module_offset); - } - else - { - return format_address_fallback(addr); - } - } + if (module_resolved) { + const auto module_offset = + reinterpret_cast<const char*>(addr) - module_info.BaseOfImage; + return format_module(module_info.ModuleName, module_offset); + } else { + return format_address_fallback(addr); } - #endif - }; + } } -} +#endif +}; + +} // namespace aux +} // namespace aes diff --git a/aesxx/include/aesxx/error.hpp b/aesxx/include/aesxx/error.hpp index 4d2c783..e5c5803 100644 --- a/aesxx/include/aesxx/error.hpp +++ b/aesxx/include/aesxx/error.hpp @@ -11,94 +11,84 @@ #include <boost/config.hpp> +#include <algorithm> #include <cstdlib> #include <cstring> - -#include <algorithm> #include <functional> #include <ostream> #include <stdexcept> #include <string> #include <vector> -namespace aes -{ - class Error : public std::runtime_error - { - public: - Error(const AES_ErrorDetails& err_details) - : std::runtime_error{format_error_message(err_details)} - { - fill_call_stack(err_details); - } +namespace aes { - void for_each_addr( - const std::function<void (const void*, const std::string&)>& callback) const - { - aux::CallStackFormatter formatter; +class Error : public std::runtime_error { +public: + Error(const AES_ErrorDetails& err_details) + : std::runtime_error{format_error_message(err_details)} { + fill_call_stack(err_details); + } - std::for_each( - call_stack, - call_stack + call_stack_len, - [&formatter, &callback] (const void* addr) - { + void for_each_addr(const std::function<void(const void*, const std::string&)>& callback) const { + aux::CallStackFormatter formatter; + + std::for_each( + call_stack, call_stack + call_stack_len, [&formatter, &callback](const void* addr) { callback(addr, formatter.format_address(addr)); }); - } + } + +private: + static std::string format_error_message(const AES_ErrorDetails& err_details) { + std::vector<char> buf; + buf.resize(aes_format_error(&err_details, NULL, 0)); + aes_format_error(&err_details, buf.data(), buf.size()); + return {buf.begin(), buf.end()}; + } - private: - static std::string format_error_message(const AES_ErrorDetails& err_details) - { - std::vector<char> buf; - buf.resize(aes_format_error(&err_details, NULL, 0)); - aes_format_error(&err_details, buf.data(), buf.size()); - return {buf.begin(), buf.end()}; - } + void fill_call_stack(const AES_ErrorDetails& err_details) { + call_stack_len = err_details.call_stack_len; - void fill_call_stack(const AES_ErrorDetails& err_details) - { - call_stack_len = err_details.call_stack_len; + if (call_stack_len > AES_MAX_CALL_STACK_LENGTH) + call_stack_len = AES_MAX_CALL_STACK_LENGTH; - if (call_stack_len > AES_MAX_CALL_STACK_LENGTH) - call_stack_len = AES_MAX_CALL_STACK_LENGTH; + std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*)); + } - std::memcpy(call_stack, err_details.call_stack, call_stack_len * sizeof(const void*)); - } + std::size_t call_stack_len = 0; + const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; +}; - std::size_t call_stack_len = 0; - const void* call_stack[AES_MAX_CALL_STACK_LENGTH] = {nullptr}; - }; +inline std::ostream& operator<<(std::ostream& os, const Error& error) { + os << "AES error: " << error.what() << '\n'; + os << "Call stack:\n"; + error.for_each_addr([&os](const void* addr, const std::string& name) { + os << '\t' << addr << ' ' << name << '\n'; + }); + return os; +} - inline std::ostream& operator<<(std::ostream& os, const Error& error) - { - os << "AES error: " << error.what() << '\n'; - os << "Call stack:\n"; - error.for_each_addr([&os] (const void* addr, const std::string& name) - { - os << '\t' << addr << ' ' << name << '\n'; - }); - return os; +class ErrorDetailsThrowsInDestructor { +public: + ErrorDetailsThrowsInDestructor() { + aes_success(get()); } - class ErrorDetailsThrowsInDestructor - { - public: - ErrorDetailsThrowsInDestructor() - { - aes_success(get()); - } + ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) { + if (aes_is_error(aes_get_error_code(get()))) + throw Error(impl); + } - ~ErrorDetailsThrowsInDestructor() BOOST_NOEXCEPT_IF(false) - { - if (aes_is_error(aes_get_error_code(get()))) - throw Error(impl); - } + AES_ErrorDetails* get() { + return &impl; + } - AES_ErrorDetails* get() { return &impl; } + operator AES_ErrorDetails*() { + return get(); + } - operator AES_ErrorDetails*() { return get(); } +private: + AES_ErrorDetails impl; +}; - private: - AES_ErrorDetails impl; - }; -} +} // namespace aes diff --git a/aesxx/include/aesxx/mode.hpp b/aesxx/include/aesxx/mode.hpp index 1c6c4cf..999db3a 100644 --- a/aesxx/include/aesxx/mode.hpp +++ b/aesxx/include/aesxx/mode.hpp @@ -9,145 +9,129 @@ #include <type_traits> -namespace aes -{ - typedef AES_Mode Mode; +namespace aes { - template <Mode mode> - struct ModeRequiresInitVector : public std::true_type - { }; +typedef AES_Mode Mode; - template <> - struct ModeRequiresInitVector<AES_ECB> : public std::false_type - { }; +template <Mode mode> +struct ModeRequiresInitVector : public std::true_type {}; - template <Mode mode> - struct ModeUsesEncryptionKeysOnly : public std::true_type - { }; +template <> +struct ModeRequiresInitVector<AES_ECB> : public std::false_type {}; - inline bool mode_requires_init_vector(Mode mode) - { - return mode != AES_ECB; - } +template <Mode mode> +struct ModeUsesEncryptionKeysOnly : public std::true_type {}; - template <> - struct ModeUsesEncryptionKeysOnly<AES_ECB> : public std::false_type - { }; +inline bool mode_requires_init_vector(Mode mode) { + return mode != AES_ECB; +} - template <> - struct ModeUsesEncryptionKeysOnly<AES_CBC> : public std::false_type - { }; +template <> +struct ModeUsesEncryptionKeysOnly<AES_ECB> : public std::false_type {}; - inline bool mode_uses_encryption_keys_only(Mode mode) - { - return mode != AES_ECB && mode != AES_CBC; - } +template <> +struct ModeUsesEncryptionKeysOnly<AES_CBC> : public std::false_type {}; -#define AESXX_ENCRYPT_BLOCK_ECB(prefix) \ - template <> \ - inline void encrypt_block<AES_## prefix, AES_ECB>( \ - const typename Types<AES_## prefix>::Block& plaintext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& ciphertext) \ - { \ - ciphertext = aes_## prefix ##_encrypt_block_ECB(plaintext, &encryption_keys); \ +inline bool mode_uses_encryption_keys_only(Mode mode) { + return mode != AES_ECB && mode != AES_CBC; +} + +#define AESXX_ENCRYPT_BLOCK_ECB(prefix) \ + template <> \ + inline void encrypt_block<AES_##prefix, AES_ECB>( \ + const typename Types<AES_##prefix>::Block& plaintext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& ciphertext) { \ + ciphertext = aes_##prefix##_encrypt_block_ECB(plaintext, &encryption_keys); \ } -#define AESXX_DECRYPT_BLOCK_ECB(prefix) \ - template <> \ - inline void decrypt_block<AES_## prefix, AES_ECB>( \ - const typename Types<AES_## prefix>::Block& ciphertext, \ - const typename Types<AES_## prefix>::RoundKeys& decryption_keys, \ - typename Types<AES_## prefix>::Block& plaintext) \ - { \ - plaintext = aes_## prefix ##_decrypt_block_ECB(ciphertext, &decryption_keys); \ +#define AESXX_DECRYPT_BLOCK_ECB(prefix) \ + template <> \ + inline void decrypt_block<AES_##prefix, AES_ECB>( \ + const typename Types<AES_##prefix>::Block& ciphertext, \ + const typename Types<AES_##prefix>::RoundKeys& decryption_keys, \ + typename Types<AES_##prefix>::Block& plaintext) { \ + plaintext = aes_##prefix##_decrypt_block_ECB(ciphertext, &decryption_keys); \ } -#define AESXX_ENCRYPT_BLOCK_CBC(prefix) \ - template <> \ - inline void encrypt_block<AES_## prefix, AES_CBC>( \ - const typename Types<AES_## prefix>::Block& plaintext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& ciphertext) \ - { \ - ciphertext = aes_## prefix ##_encrypt_block_CBC(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_ENCRYPT_BLOCK_CBC(prefix) \ + template <> \ + inline void encrypt_block<AES_##prefix, AES_CBC>( \ + const typename Types<AES_##prefix>::Block& plaintext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& ciphertext) { \ + ciphertext = aes_##prefix##_encrypt_block_CBC(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_DECRYPT_BLOCK_CBC(prefix) \ - template <> \ - inline void decrypt_block<AES_## prefix, AES_CBC>( \ - const typename Types<AES_## prefix>::Block& ciphertext, \ - const typename Types<AES_## prefix>::RoundKeys& decryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& plaintext) \ - { \ - plaintext = aes_## prefix ##_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &iv); \ +#define AESXX_DECRYPT_BLOCK_CBC(prefix) \ + template <> \ + inline void decrypt_block<AES_##prefix, AES_CBC>( \ + const typename Types<AES_##prefix>::Block& ciphertext, \ + const typename Types<AES_##prefix>::RoundKeys& decryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& plaintext) { \ + plaintext = aes_##prefix##_decrypt_block_CBC(ciphertext, &decryption_keys, iv, &iv); \ } -#define AESXX_ENCRYPT_BLOCK_CFB(prefix) \ - template <> \ - inline void encrypt_block<AES_## prefix, AES_CFB>( \ - const typename Types<AES_## prefix>::Block& plaintext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& ciphertext) \ - { \ - ciphertext = aes_## prefix ##_encrypt_block_CFB(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_ENCRYPT_BLOCK_CFB(prefix) \ + template <> \ + inline void encrypt_block<AES_##prefix, AES_CFB>( \ + const typename Types<AES_##prefix>::Block& plaintext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& ciphertext) { \ + ciphertext = aes_##prefix##_encrypt_block_CFB(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_DECRYPT_BLOCK_CFB(prefix) \ - template <> \ - inline void decrypt_block<AES_## prefix, AES_CFB>( \ - const typename Types<AES_## prefix>::Block& ciphertext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& plaintext) \ - { \ - plaintext = aes_## prefix ##_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &iv); \ +#define AESXX_DECRYPT_BLOCK_CFB(prefix) \ + template <> \ + inline void decrypt_block<AES_##prefix, AES_CFB>( \ + const typename Types<AES_##prefix>::Block& ciphertext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& plaintext) { \ + plaintext = aes_##prefix##_decrypt_block_CFB(ciphertext, &encryption_keys, iv, &iv); \ } -#define AESXX_ENCRYPT_BLOCK_OFB(prefix) \ - template <> \ - inline void encrypt_block<AES_## prefix, AES_OFB>( \ - const typename Types<AES_## prefix>::Block& plaintext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& ciphertext) \ - { \ - ciphertext = aes_## prefix ##_encrypt_block_OFB(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_ENCRYPT_BLOCK_OFB(prefix) \ + template <> \ + inline void encrypt_block<AES_##prefix, AES_OFB>( \ + const typename Types<AES_##prefix>::Block& plaintext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& ciphertext) { \ + ciphertext = aes_##prefix##_encrypt_block_OFB(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_DECRYPT_BLOCK_OFB(prefix) \ - template <> \ - inline void decrypt_block<AES_## prefix, AES_OFB>( \ - const typename Types<AES_## prefix>::Block& ciphertext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& plaintext) \ - { \ - plaintext = aes_## prefix ##_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &iv); \ +#define AESXX_DECRYPT_BLOCK_OFB(prefix) \ + template <> \ + inline void decrypt_block<AES_##prefix, AES_OFB>( \ + const typename Types<AES_##prefix>::Block& ciphertext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& plaintext) { \ + plaintext = aes_##prefix##_decrypt_block_OFB(ciphertext, &encryption_keys, iv, &iv); \ } -#define AESXX_ENCRYPT_BLOCK_CTR(prefix) \ - template <> \ - inline void encrypt_block<AES_## prefix, AES_CTR>( \ - const typename Types<AES_## prefix>::Block& plaintext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& ciphertext) \ - { \ - ciphertext = aes_## prefix ##_encrypt_block_CTR(plaintext, &encryption_keys, iv, &iv); \ +#define AESXX_ENCRYPT_BLOCK_CTR(prefix) \ + template <> \ + inline void encrypt_block<AES_##prefix, AES_CTR>( \ + const typename Types<AES_##prefix>::Block& plaintext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& ciphertext) { \ + ciphertext = aes_##prefix##_encrypt_block_CTR(plaintext, &encryption_keys, iv, &iv); \ } -#define AESXX_DECRYPT_BLOCK_CTR(prefix) \ - template <> \ - inline void decrypt_block<AES_## prefix, AES_CTR>( \ - const typename Types<AES_## prefix>::Block& ciphertext, \ - const typename Types<AES_## prefix>::RoundKeys& encryption_keys, \ - typename Types<AES_## prefix>::Block& iv, \ - typename Types<AES_## prefix>::Block& plaintext) \ - { \ - plaintext = aes_## prefix ##_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &iv); \ +#define AESXX_DECRYPT_BLOCK_CTR(prefix) \ + template <> \ + inline void decrypt_block<AES_##prefix, AES_CTR>( \ + const typename Types<AES_##prefix>::Block& ciphertext, \ + const typename Types<AES_##prefix>::RoundKeys& encryption_keys, \ + typename Types<AES_##prefix>::Block& iv, \ + typename Types<AES_##prefix>::Block& plaintext) { \ + plaintext = aes_##prefix##_decrypt_block_CTR(ciphertext, &encryption_keys, iv, &iv); \ } -} + +} // namespace aes diff --git a/aesxx/utils/block_cmd_parser.hpp b/aesxx/utils/block_cmd_parser.hpp index 58f86d8..3ae0eac 100644 --- a/aesxx/utils/block_cmd_parser.hpp +++ b/aesxx/utils/block_cmd_parser.hpp @@ -19,8 +19,7 @@ #include <utility> #include <vector> -class BlockSettings : public command_line::SettingsParser -{ +class BlockSettings : public command_line::SettingsParser { public: aes::Algorithm algorithm = AES_AES128; aes::Mode mode = AES_ECB; @@ -30,65 +29,52 @@ public: std::vector<Input> inputs; - explicit BlockSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("verbose,v", - boost::program_options::bool_switch(&verbose), - "enable verbose output") - ("algorithm,a", - boost::program_options::value<aes::Algorithm>(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value<aes::Mode>(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("use-boxes,b", - boost::program_options::bool_switch(&use_boxes), - "use the \"boxes\" interface"); - hidden.add_options() - ("args", - boost::program_options::value<std::vector<std::string>>(&args), - "shouldn't be visible"); + explicit BlockSettings(const std::string& argv0) : SettingsParser{argv0} { + visible.add_options()( + "verbose,v", boost::program_options::bool_switch(&verbose), "enable verbose output"); + visible.add_options()("algorithm,a", + boost::program_options::value<aes::Algorithm>(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm"); + visible.add_options()( + "mode,m", + boost::program_options::value<aes::Mode>(&mode)->required()->value_name("MODE"), + "set mode of operation"); + visible.add_options()("use-boxes,b", + boost::program_options::bool_switch(&use_boxes), + "use the \"boxes\" interface"); + hidden.add_options()("args", + boost::program_options::value<std::vector<std::string>>(&args), + "shouldn't be visible"); positional.add("args", -1); } - const char* get_short_description() const override - { + const char* get_short_description() const override { return "[-h|--help] [-v|--verbose] [-a|--algorithm NAME] [-m|--mode MODE]" " [-- KEY [IV] [BLOCK]...]..."; } - void parse(int argc, char* argv[]) override - { + void parse(int argc, char* argv[]) override { SettingsParser::parse(argc, argv); - parse_inputs(std::deque<std::string>{ - std::make_move_iterator(args.begin()), - std::make_move_iterator(args.end())}); + parse_inputs(std::deque<std::string>{std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())}); } private: - void parse_inputs(std::deque<std::string>&& src) - { + void parse_inputs(std::deque<std::string>&& src) { while (!src.empty()) inputs.emplace_back(parse_input(src)); } - Input parse_input(std::deque<std::string>& src) const - { + Input parse_input(std::deque<std::string>& src) const { std::string key{std::move(src.front())}; src.pop_front(); std::string iv; - if (aes::mode_requires_init_vector(mode)) - { - if (src.empty()) - { + if (aes::mode_requires_init_vector(mode)) { + if (src.empty()) { throw boost::program_options::error{ "an initialization vector is required for the selected mode of operation"}; } @@ -104,12 +90,10 @@ private: return {key, std::move(blocks)}; } - static std::vector<std::string> parse_blocks(std::deque<std::string>& src) - { + static std::vector<std::string> parse_blocks(std::deque<std::string>& src) { std::vector<std::string> blocks; - while (!src.empty()) - { + while (!src.empty()) { std::string block{std::move(src.front())}; src.pop_front(); if (block == "--") diff --git a/aesxx/utils/block_dumper.hpp b/aesxx/utils/block_dumper.hpp index a07d855..226ba20 100644 --- a/aesxx/utils/block_dumper.hpp +++ b/aesxx/utils/block_dumper.hpp @@ -8,48 +8,38 @@ #include <aesxx/all.hpp> #include <cstdlib> - #include <iostream> #include <type_traits> template <aes::Algorithm algorithm> -void dump_block( - const char* header, - const typename aes::Types<algorithm>::Block& block) -{ +void dump_block(const char* header, const typename aes::Types<algorithm>::Block& block) { std::cout << header << ": " << aes::to_string<algorithm>(block) << "\n"; std::cout << aes::to_matrix_string<algorithm>(block) << "\n"; } template <aes::Algorithm algorithm> -void dump_plaintext(const typename aes::Types<algorithm>::Block& block) -{ +void dump_plaintext(const typename aes::Types<algorithm>::Block& block) { dump_block<algorithm>("Plaintext", block); } template <aes::Algorithm algorithm> -void dump_key(const typename aes::Types<algorithm>::Key& key) -{ +void dump_key(const typename aes::Types<algorithm>::Key& key) { std::cout << "Key: " << aes::to_string<algorithm>(key) << "\n\n"; } template <aes::Algorithm algorithm> -void dump_ciphertext(const typename aes::Types<algorithm>::Block& ciphertext) -{ +void dump_ciphertext(const typename aes::Types<algorithm>::Block& ciphertext) { dump_block<algorithm>("Ciphertext", ciphertext); } template <aes::Algorithm algorithm> -void dump_iv(const typename aes::Types<algorithm>::Block& iv) -{ +void dump_iv(const typename aes::Types<algorithm>::Block& iv) { dump_block<algorithm>("Initialization vector", iv); } template <aes::Algorithm algorithm> -void dump_round_keys( - const char* header, - const typename aes::Types<algorithm>::RoundKeys& round_keys) -{ +void dump_round_keys(const char* header, + const typename aes::Types<algorithm>::RoundKeys& round_keys) { std::cout << header << ":\n"; for (std::size_t i = 0; i < aes::get_number_of_rounds<algorithm>(); ++i) std::cout << "\t[" << i << "]: " << aes::to_string<algorithm>(round_keys.keys[i]) << "\n"; @@ -57,49 +47,45 @@ void dump_round_keys( } template <aes::Algorithm algorithm> -void dump_encryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) -{ +void dump_encryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) { dump_round_keys<algorithm>("Encryption round keys", round_keys); } template <aes::Algorithm algorithm> -void dump_decryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) -{ +void dump_decryption_keys(const typename aes::Types<algorithm>::RoundKeys& round_keys) { dump_round_keys<algorithm>("Decryption round keys", round_keys); } template <aes::Algorithm algorithm, aes::Mode mode> -void dump_wrapper(const aes::EncryptWrapper<algorithm, mode>& wrapper) -{ +void dump_wrapper(const aes::EncryptWrapper<algorithm, mode>& wrapper) { dump_encryption_keys<algorithm>(wrapper.encryption_keys); } template <aes::Algorithm algorithm, aes::Mode mode> -void dump_wrapper(const aes::DecryptWrapper<algorithm, mode>& wrapper) -{ +void dump_wrapper(const aes::DecryptWrapper<algorithm, mode>& wrapper) { dump_decryption_keys<algorithm>(wrapper.decryption_keys); } -template <aes::Algorithm algorithm, aes::Mode mode, +template <aes::Algorithm algorithm, + aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> -void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>& wrapper) -{ +void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>& wrapper) { dump_block<algorithm>("Next initialization vector", wrapper.iv); } -template <aes::Algorithm algorithm, aes::Mode mode, +template <aes::Algorithm algorithm, + aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> -void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>&) -{ } +void dump_next_iv(const aes::EncryptWrapper<algorithm, mode>&) {} -template <aes::Algorithm algorithm, aes::Mode mode, +template <aes::Algorithm algorithm, + aes::Mode mode, typename std::enable_if<aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> -void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>& wrapper) -{ +void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>& wrapper) { dump_block<algorithm>("Next initialization vector", wrapper.iv); } -template <aes::Algorithm algorithm, aes::Mode mode, +template <aes::Algorithm algorithm, + aes::Mode mode, typename std::enable_if<!aes::ModeRequiresInitVector<mode>::value>::type* = nullptr> -void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>&) -{ } +void dump_next_iv(const aes::DecryptWrapper<algorithm, mode>&) {} diff --git a/aesxx/utils/block_input.hpp b/aesxx/utils/block_input.hpp index f629062..1dd3f70 100644 --- a/aesxx/utils/block_input.hpp +++ b/aesxx/utils/block_input.hpp @@ -9,19 +9,13 @@ #include <utility> #include <vector> -class Input -{ +class Input { public: Input(const std::string& key, const std::string& iv, std::vector<std::string>&& blocks) - : key{key} - , iv{iv} - , blocks{std::move(blocks)} - { } + : key{key}, iv{iv}, blocks{std::move(blocks)} {} Input(const std::string& key, std::vector<std::string>&& blocks) - : key{key} - , blocks{std::move(blocks)} - { } + : key{key}, blocks{std::move(blocks)} {} const std::string key; const std::string iv; diff --git a/aesxx/utils/data_parsers.hpp b/aesxx/utils/data_parsers.hpp index 1207c7c..a08f1bf 100644 --- a/aesxx/utils/data_parsers.hpp +++ b/aesxx/utils/data_parsers.hpp @@ -14,13 +14,11 @@ #include <string> #include <unordered_map> -inline std::istream& operator>>(std::istream& is, aes::Mode& dest) -{ +inline std::istream& operator>>(std::istream& is, aes::Mode& dest) { std::string src; is >> src; - static const std::unordered_map<std::string, aes::Mode> lookup_table = - { + static const std::unordered_map<std::string, aes::Mode> lookup_table = { {"ecb", AES_ECB}, {"cbc", AES_CBC}, {"cfb", AES_CFB}, @@ -37,13 +35,11 @@ inline std::istream& operator>>(std::istream& is, aes::Mode& dest) return is; } -inline std::istream& operator>>(std::istream& is, aes::Algorithm& dest) -{ +inline std::istream& operator>>(std::istream& is, aes::Algorithm& dest) { std::string src; is >> src; - static const std::unordered_map<std::string, aes::Algorithm> lookup_table = - { + static const std::unordered_map<std::string, aes::Algorithm> lookup_table = { {"aes128", AES_AES128}, {"aes192", AES_AES192}, {"aes256", AES_AES256}, diff --git a/aesxx/utils/decrypt_block.cpp b/aesxx/utils/decrypt_block.cpp index 02a3033..42e496e 100644 --- a/aesxx/utils/decrypt_block.cpp +++ b/aesxx/utils/decrypt_block.cpp @@ -16,198 +16,151 @@ #include <stdexcept> #include <string> -namespace -{ - template <aes::Algorithm algorithm, aes::Mode mode> - void decrypt_with_mode( - const Input& input, - bool verbose = false) - { - typename aes::Types<algorithm>::Block iv; +namespace { - if (aes::ModeRequiresInitVector<mode>()) - { - aes::from_string<algorithm>(iv, input.iv); - if (verbose) - dump_iv<algorithm>(iv); - } +template <aes::Algorithm algorithm, aes::Mode mode> +void decrypt_with_mode(const Input& input, bool verbose = false) { + typename aes::Types<algorithm>::Block iv; - typename aes::Types<algorithm>::Key key; - aes::from_string<algorithm>(key, input.key); + if (aes::ModeRequiresInitVector<mode>()) { + aes::from_string<algorithm>(iv, input.iv); if (verbose) - dump_key<algorithm>(key); + dump_iv<algorithm>(iv); + } - aes::DecryptWrapper<algorithm, mode> decrypt{key, iv}; - if (verbose) - dump_wrapper<algorithm, mode>(decrypt); + typename aes::Types<algorithm>::Key key; + aes::from_string<algorithm>(key, input.key); + if (verbose) + dump_key<algorithm>(key); - for (const auto& block : input.blocks) - { - typename aes::Types<algorithm>::Block ciphertext, plaintext; - aes::from_string<algorithm>(ciphertext, block); + aes::DecryptWrapper<algorithm, mode> decrypt{key, iv}; + if (verbose) + dump_wrapper<algorithm, mode>(decrypt); - decrypt.decrypt_block(ciphertext, plaintext); + for (const auto& block : input.blocks) { + typename aes::Types<algorithm>::Block ciphertext, plaintext; + aes::from_string<algorithm>(ciphertext, block); - if (verbose) - { - dump_ciphertext<algorithm>(ciphertext); - dump_plaintext<algorithm>(plaintext); - dump_next_iv<algorithm, mode>(decrypt); - } - else - { - std::cout << aes::to_string<algorithm>(plaintext) << '\n'; - } + decrypt.decrypt_block(ciphertext, plaintext); + + if (verbose) { + dump_ciphertext<algorithm>(ciphertext); + dump_plaintext<algorithm>(plaintext); + dump_next_iv<algorithm, mode>(decrypt); + } else { + std::cout << aes::to_string<algorithm>(plaintext) << '\n'; } } +} - template <aes::Algorithm algorithm> - void decrypt_with_algorithm( - aes::Mode mode, - const Input& input, - bool verbose = false) - { - switch (mode) - { - case AES_ECB: - decrypt_with_mode<algorithm, AES_ECB>(input, verbose); - break; +template <aes::Algorithm algorithm> +void decrypt_with_algorithm(aes::Mode mode, const Input& input, bool verbose = false) { + switch (mode) { + case AES_ECB: + decrypt_with_mode<algorithm, AES_ECB>(input, verbose); + break; - case AES_CBC: - decrypt_with_mode<algorithm, AES_CBC>(input, verbose); - break; + case AES_CBC: + decrypt_with_mode<algorithm, AES_CBC>(input, verbose); + break; - case AES_CFB: - decrypt_with_mode<algorithm, AES_CFB>(input, verbose); - break; + case AES_CFB: + decrypt_with_mode<algorithm, AES_CFB>(input, verbose); + break; - case AES_OFB: - decrypt_with_mode<algorithm, AES_OFB>(input, verbose); - break; + case AES_OFB: + decrypt_with_mode<algorithm, AES_OFB>(input, verbose); + break; - case AES_CTR: - decrypt_with_mode<algorithm, AES_CTR>(input, verbose); - break; + case AES_CTR: + decrypt_with_mode<algorithm, AES_CTR>(input, verbose); + break; - default: - throw std::runtime_error("the selected mode of operation is not implemented"); - break; - } + default: + throw std::runtime_error("the selected mode of operation is not implemented"); + break; } +} - void decrypt_using_cxx_api( - aes::Algorithm algorithm, - aes::Mode mode, - const Input& input, - bool verbose = false) - { - switch (algorithm) - { - case AES_AES128: - decrypt_with_algorithm<AES_AES128>(mode, input, verbose); - break; +void decrypt_using_cxx_api(aes::Algorithm algorithm, + aes::Mode mode, + const Input& input, + bool verbose = false) { + switch (algorithm) { + case AES_AES128: + decrypt_with_algorithm<AES_AES128>(mode, input, verbose); + break; - case AES_AES192: - decrypt_with_algorithm<AES_AES192>(mode, input, verbose); - break; + case AES_AES192: + decrypt_with_algorithm<AES_AES192>(mode, input, verbose); + break; - case AES_AES256: - decrypt_with_algorithm<AES_AES256>(mode, input, verbose); - break; + case AES_AES256: + decrypt_with_algorithm<AES_AES256>(mode, input, verbose); + break; - default: - throw std::runtime_error("the selected algorithm is not implemented"); - break; - } + default: + throw std::runtime_error("the selected algorithm is not implemented"); + break; } +} - void decrypt_using_particular_box( - aes::Box& box, - const std::vector<std::string>& blocks) - { - for (const auto& block : blocks) - { - aes::Box::Block ciphertext; - box.parse_block(ciphertext, block); +void decrypt_using_particular_box(aes::Box& box, const std::vector<std::string>& blocks) { + for (const auto& block : blocks) { + aes::Box::Block ciphertext; + box.parse_block(ciphertext, block); - aes::Box::Block plaintext; - box.decrypt_block(ciphertext, plaintext); - std::cout << box.format_block(plaintext) << '\n'; - } + aes::Box::Block plaintext; + box.decrypt_block(ciphertext, plaintext); + std::cout << box.format_block(plaintext) << '\n'; } +} - void decrypt_using_boxes( - aes::Algorithm algorithm, - aes::Mode mode, - const Input& input) - { - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.key); +void decrypt_using_boxes(aes::Algorithm algorithm, aes::Mode mode, const Input& input) { + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, input.key); - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, input.iv); - aes::Box box{algorithm, key, mode, iv}; + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, input.iv); + aes::Box box{algorithm, key, mode, iv}; - decrypt_using_particular_box(box, input.blocks); - } - else - { - aes::Box box{algorithm, key}; - decrypt_using_particular_box(box, input.blocks); - } + decrypt_using_particular_box(box, input.blocks); + } else { + aes::Box box{algorithm, key}; + decrypt_using_particular_box(box, input.blocks); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { BlockSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } - for (const auto& input : settings.inputs) - { - if (settings.use_boxes) - { - decrypt_using_boxes( - settings.algorithm, - settings.mode, - input); - } - else - { - decrypt_using_cxx_api( - settings.algorithm, - settings.mode, - input, - settings.verbose); + for (const auto& input : settings.inputs) { + if (settings.use_boxes) { + decrypt_using_boxes(settings.algorithm, settings.mode, input); + } else { + decrypt_using_cxx_api(settings.algorithm, settings.mode, input, settings.verbose); } } - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/aesxx/utils/decrypt_bmp.cpp b/aesxx/utils/decrypt_bmp.cpp index 9f9cd52..86f6b7d 100644 --- a/aesxx/utils/decrypt_bmp.cpp +++ b/aesxx/utils/decrypt_bmp.cpp @@ -9,83 +9,66 @@ #include <aesxx/all.hpp> -#include <windows.h> - #include <boost/program_options.hpp> +#include <windows.h> + #include <exception> #include <iostream> #include <string> -namespace -{ - void decrypt_bmp( - aes::Box& box, - const std::string& ciphertext_path, - const std::string& plaintext_path) - { - bmp::BmpFile bmp{file::read_file(ciphertext_path)}; - bmp.replace_pixels(box.decrypt_buffer( - bmp.get_pixels(), - bmp.get_pixels_size())); - file::write_file(plaintext_path, bmp.get_buffer(), bmp.get_size()); - } +namespace { - void decrypt_bmp(const FileSettings& settings) - { - const auto& algorithm = settings.algorithm; - const auto& mode = settings.mode; +void decrypt_bmp(aes::Box& box, + const std::string& ciphertext_path, + const std::string& plaintext_path) { + bmp::BmpFile bmp{file::read_file(ciphertext_path)}; + bmp.replace_pixels(box.decrypt_buffer(bmp.get_pixels(), bmp.get_pixels_size())); + file::write_file(plaintext_path, bmp.get_buffer(), bmp.get_size()); +} - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.key); +void decrypt_bmp(const FileSettings& settings) { + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.iv); + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, settings.key); - aes::Box box{algorithm, key, mode, iv}; - decrypt_bmp(box, settings.input_path, settings.output_path); - } - else - { - aes::Box box{algorithm, key}; - decrypt_bmp(box, settings.input_path, settings.output_path); - } + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, settings.iv); + + aes::Box box{algorithm, key, mode, iv}; + decrypt_bmp(box, settings.input_path, settings.output_path); + } else { + aes::Box box{algorithm, key}; + decrypt_bmp(box, settings.input_path, settings.output_path); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { FileSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } decrypt_bmp(settings); - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/aesxx/utils/decrypt_file.cpp b/aesxx/utils/decrypt_file.cpp index 6da3d17..727362c 100644 --- a/aesxx/utils/decrypt_file.cpp +++ b/aesxx/utils/decrypt_file.cpp @@ -15,75 +15,58 @@ #include <string> #include <vector> -namespace -{ - void decrypt_file( - aes::Box& box, - const std::string& ciphertext_path, - const std::string& plaintext_path) - { - const auto ciphertext_buf = file::read_file(ciphertext_path); - const auto plaintext_buf = box.decrypt_buffer( - ciphertext_buf.data(), - ciphertext_buf.size()); - file::write_file(plaintext_path, plaintext_buf); - } +namespace { - void decrypt_file(const FileSettings& settings) - { - const auto& algorithm = settings.algorithm; - const auto& mode = settings.mode; +void decrypt_file(aes::Box& box, + const std::string& ciphertext_path, + const std::string& plaintext_path) { + const auto ciphertext_buf = file::read_file(ciphertext_path); + const auto plaintext_buf = box.decrypt_buffer(ciphertext_buf.data(), ciphertext_buf.size()); + file::write_file(plaintext_path, plaintext_buf); +} - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.key); +void decrypt_file(const FileSettings& settings) { + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.iv); + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, settings.key); - aes::Box box{algorithm, key, mode, iv}; - decrypt_file(box, settings.input_path, settings.output_path); - } - else - { - aes::Box box{algorithm, key}; - decrypt_file(box, settings.input_path, settings.output_path); - } + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, settings.iv); + + aes::Box box{algorithm, key, mode, iv}; + decrypt_file(box, settings.input_path, settings.output_path); + } else { + aes::Box box{algorithm, key}; + decrypt_file(box, settings.input_path, settings.output_path); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { FileSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } decrypt_file(settings); - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/aesxx/utils/encrypt_block.cpp b/aesxx/utils/encrypt_block.cpp index 0b87364..b6bdca9 100644 --- a/aesxx/utils/encrypt_block.cpp +++ b/aesxx/utils/encrypt_block.cpp @@ -16,198 +16,152 @@ #include <stdexcept> #include <string> -namespace -{ - template <aes::Algorithm algorithm, aes::Mode mode> - void encrypt_with_mode( - const Input& input, - bool verbose = false) - { - typename aes::Types<algorithm>::Block iv; +namespace { - if (aes::ModeRequiresInitVector<mode>::value) - { - aes::from_string<algorithm>(iv, input.iv); - if (verbose) - dump_iv<algorithm>(iv); - } +template <aes::Algorithm algorithm, aes::Mode mode> +void encrypt_with_mode(const Input& input, bool verbose = false) { + typename aes::Types<algorithm>::Block iv; - typename aes::Types<algorithm>::Key key; - aes::from_string<algorithm>(key, input.key); + if (aes::ModeRequiresInitVector<mode>::value) { + aes::from_string<algorithm>(iv, input.iv); if (verbose) - dump_key<algorithm>(key); + dump_iv<algorithm>(iv); + } - aes::EncryptWrapper<algorithm, mode> encrypt{key, iv}; - if (verbose) - dump_wrapper<algorithm, mode>(encrypt); + typename aes::Types<algorithm>::Key key; + aes::from_string<algorithm>(key, input.key); + if (verbose) + dump_key<algorithm>(key); - for (const auto& input_block_string : input.blocks) - { - typename aes::Types<algorithm>::Block plaintext, ciphertext; - aes::from_string<algorithm>(plaintext, input_block_string); + aes::EncryptWrapper<algorithm, mode> encrypt{key, iv}; + if (verbose) + dump_wrapper<algorithm, mode>(encrypt); - encrypt.encrypt_block(plaintext, ciphertext); + for (const auto& input_block_string : input.blocks) { + typename aes::Types<algorithm>::Block plaintext, ciphertext; + aes::from_string<algorithm>(plaintext, input_block_string); - if (verbose) - { - dump_plaintext<algorithm>(plaintext); - dump_ciphertext<algorithm>(ciphertext); - dump_next_iv<algorithm, mode>(encrypt); - } - else - { - std::cout << aes::to_string<algorithm>(ciphertext) << '\n'; - } + encrypt.encrypt_block(plaintext, ciphertext); + + if (verbose) { + dump_plaintext<algorithm>(plaintext); + dump_ciphertext<algorithm>(ciphertext); + dump_next_iv<algorithm, mode>(encrypt); + } else { + std::cout << aes::to_string<algorithm>(ciphertext) << '\n'; } } +} - template <aes::Algorithm algorithm> - void encrypt_with_algorithm( - aes::Mode mode, - const Input& input, - bool verbose = false) - { - switch (mode) - { - case AES_ECB: - encrypt_with_mode<algorithm, AES_ECB>(input, verbose); - break; +template <aes::Algorithm algorithm> +void encrypt_with_algorithm(aes::Mode mode, const Input& input, bool verbose = false) { + switch (mode) { + case AES_ECB: + encrypt_with_mode<algorithm, AES_ECB>(input, verbose); + break; - case AES_CBC: - encrypt_with_mode<algorithm, AES_CBC>(input, verbose); - break; + case AES_CBC: + encrypt_with_mode<algorithm, AES_CBC>(input, verbose); + break; - case AES_CFB: - encrypt_with_mode<algorithm, AES_CFB>(input, verbose); - break; + case AES_CFB: + encrypt_with_mode<algorithm, AES_CFB>(input, verbose); + break; - case AES_OFB: - encrypt_with_mode<algorithm, AES_OFB>(input, verbose); - break; + case AES_OFB: + encrypt_with_mode<algorithm, AES_OFB>(input, verbose); + break; - case AES_CTR: - encrypt_with_mode<algorithm, AES_CTR>(input, verbose); - break; + case AES_CTR: + encrypt_with_mode<algorithm, AES_CTR>(input, verbose); + break; - default: - throw std::runtime_error("the selected mode of operation is not implemented"); - break; - } + default: + throw std::runtime_error("the selected mode of operation is not implemented"); + break; } +} - void encrypt_using_cxx_api( - aes::Algorithm algorithm, - aes::Mode mode, - const Input& input, - bool verbose = false) - { - switch (algorithm) - { - case AES_AES128: - encrypt_with_algorithm<AES_AES128>(mode, input, verbose); - break; +void encrypt_using_cxx_api(aes::Algorithm algorithm, + aes::Mode mode, + const Input& input, + bool verbose = false) { + switch (algorithm) { + case AES_AES128: + encrypt_with_algorithm<AES_AES128>(mode, input, verbose); + break; - case AES_AES192: - encrypt_with_algorithm<AES_AES192>(mode, input, verbose); - break; + case AES_AES192: + encrypt_with_algorithm<AES_AES192>(mode, input, verbose); + break; - case AES_AES256: - encrypt_with_algorithm<AES_AES256>(mode, input, verbose); - break; + case AES_AES256: + encrypt_with_algorithm<AES_AES256>(mode, input, verbose); + break; - default: - throw std::runtime_error("the selected algorithm is not implemented"); - break; - } + default: + throw std::runtime_error("the selected algorithm is not implemented"); + break; } +} - void encrypt_using_particular_box( - aes::Box& box, - const std::vector<std::string>& input_block_strings) - { - for (const auto& input_block_string : input_block_strings) - { - aes::Box::Block plaintext; - box.parse_block(plaintext, input_block_string); +void encrypt_using_particular_box(aes::Box& box, + const std::vector<std::string>& input_block_strings) { + for (const auto& input_block_string : input_block_strings) { + aes::Box::Block plaintext; + box.parse_block(plaintext, input_block_string); - aes::Box::Block ciphertext; - box.encrypt_block(plaintext, ciphertext); - std::cout << box.format_block(ciphertext) << '\n'; - } + aes::Box::Block ciphertext; + box.encrypt_block(plaintext, ciphertext); + std::cout << box.format_block(ciphertext) << '\n'; } +} - void encrypt_using_boxes( - aes::Algorithm algorithm, - aes::Mode mode, - const Input& input) - { - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, input.key); +void encrypt_using_boxes(aes::Algorithm algorithm, aes::Mode mode, const Input& input) { + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, input.key); - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, input.iv); - aes::Box box{algorithm, key, mode, iv}; + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, input.iv); + aes::Box box{algorithm, key, mode, iv}; - encrypt_using_particular_box(box, input.blocks); - } - else - { - aes::Box box{algorithm, key}; - encrypt_using_particular_box(box, input.blocks); - } + encrypt_using_particular_box(box, input.blocks); + } else { + aes::Box box{algorithm, key}; + encrypt_using_particular_box(box, input.blocks); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { BlockSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } - for (const auto& input : settings.inputs) - { - if (settings.use_boxes) - { - encrypt_using_boxes( - settings.algorithm, - settings.mode, - input); - } - else - { - encrypt_using_cxx_api( - settings.algorithm, - settings.mode, - input, - settings.verbose); + for (const auto& input : settings.inputs) { + if (settings.use_boxes) { + encrypt_using_boxes(settings.algorithm, settings.mode, input); + } else { + encrypt_using_cxx_api(settings.algorithm, settings.mode, input, settings.verbose); } } - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/aesxx/utils/encrypt_bmp.cpp b/aesxx/utils/encrypt_bmp.cpp index 7f1a890..01f3a78 100644 --- a/aesxx/utils/encrypt_bmp.cpp +++ b/aesxx/utils/encrypt_bmp.cpp @@ -15,75 +15,58 @@ #include <iostream> #include <string> -namespace -{ - void encrypt_bmp( - aes::Box& box, - const std::string& plaintext_path, - const std::string& ciphertext_path) - { - bmp::BmpFile bmp{file::read_file(plaintext_path)}; - bmp.replace_pixels(box.encrypt_buffer( - bmp.get_pixels(), - bmp.get_pixels_size())); - file::write_file(ciphertext_path, bmp.get_buffer(), bmp.get_size()); - } +namespace { - void encrypt_bmp(const FileSettings& settings) - { - const auto& algorithm = settings.algorithm; - const auto& mode = settings.mode; +void encrypt_bmp(aes::Box& box, + const std::string& plaintext_path, + const std::string& ciphertext_path) { + bmp::BmpFile bmp{file::read_file(plaintext_path)}; + bmp.replace_pixels(box.encrypt_buffer(bmp.get_pixels(), bmp.get_pixels_size())); + file::write_file(ciphertext_path, bmp.get_buffer(), bmp.get_size()); +} - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.key); +void encrypt_bmp(const FileSettings& settings) { + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.iv); + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, settings.key); - aes::Box box{algorithm, key, mode, iv}; - encrypt_bmp(box, settings.input_path, settings.output_path); - } - else - { - aes::Box box{algorithm, key}; - encrypt_bmp(box, settings.input_path, settings.output_path); - } + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, settings.iv); + + aes::Box box{algorithm, key, mode, iv}; + encrypt_bmp(box, settings.input_path, settings.output_path); + } else { + aes::Box box{algorithm, key}; + encrypt_bmp(box, settings.input_path, settings.output_path); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { FileSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } encrypt_bmp(settings); - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/aesxx/utils/encrypt_file.cpp b/aesxx/utils/encrypt_file.cpp index 570bc60..4a774fd 100644 --- a/aesxx/utils/encrypt_file.cpp +++ b/aesxx/utils/encrypt_file.cpp @@ -15,75 +15,58 @@ #include <string> #include <vector> -namespace -{ - void encrypt_file( - aes::Box& box, - const std::string& plaintext_path, - const std::string& ciphertext_path) - { - const auto plaintext_buf = file::read_file(plaintext_path); - const auto ciphertext_buf = box.encrypt_buffer( - plaintext_buf.data(), - plaintext_buf.size()); - file::write_file(ciphertext_path, ciphertext_buf); - } +namespace { - void encrypt_file(const FileSettings& settings) - { - const auto& algorithm = settings.algorithm; - const auto& mode = settings.mode; +void encrypt_file(aes::Box& box, + const std::string& plaintext_path, + const std::string& ciphertext_path) { + const auto plaintext_buf = file::read_file(plaintext_path); + const auto ciphertext_buf = box.encrypt_buffer(plaintext_buf.data(), plaintext_buf.size()); + file::write_file(ciphertext_path, ciphertext_buf); +} - aes::Box::Key key; - aes::Box::parse_key(key, algorithm, settings.key); +void encrypt_file(const FileSettings& settings) { + const auto& algorithm = settings.algorithm; + const auto& mode = settings.mode; - if (aes::mode_requires_init_vector(mode)) - { - aes::Box::Block iv; - aes::Box::parse_block(iv, algorithm, settings.iv); + aes::Box::Key key; + aes::Box::parse_key(key, algorithm, settings.key); - aes::Box box{algorithm, key, mode, iv}; - encrypt_file(box, settings.input_path, settings.output_path); - } - else - { - aes::Box box{algorithm, key}; - encrypt_file(box, settings.input_path, settings.output_path); - } + if (aes::mode_requires_init_vector(mode)) { + aes::Box::Block iv; + aes::Box::parse_block(iv, algorithm, settings.iv); + + aes::Box box{algorithm, key, mode, iv}; + encrypt_file(box, settings.input_path, settings.output_path); + } else { + aes::Box box{algorithm, key}; + encrypt_file(box, settings.input_path, settings.output_path); } } -int main(int argc, char** argv) -{ - try - { +} // namespace + +int main(int argc, char** argv) { + try { FileSettings settings{argv[0]}; - try - { + try { settings.parse(argc, argv); - } - catch (const boost::program_options::error& e) - { + } catch (const boost::program_options::error& e) { settings.usage_error(e); return 1; } - if (settings.exit_with_usage) - { + if (settings.exit_with_usage) { settings.usage(); return 0; } encrypt_file(settings); - } - catch (const aes::Error& e) - { + } catch (const aes::Error& e) { std::cerr << e; return 1; - } - catch (const std::exception& e) - { + } catch (const std::exception& e) { std::cerr << e.what() << "\n"; return 1; } diff --git a/aesxx/utils/file_cmd_parser.hpp b/aesxx/utils/file_cmd_parser.hpp index e199409..4a504c8 100644 --- a/aesxx/utils/file_cmd_parser.hpp +++ b/aesxx/utils/file_cmd_parser.hpp @@ -17,8 +17,7 @@ #include <string> #include <utility> -class FileSettings : public command_line::SettingsParser -{ +class FileSettings : public command_line::SettingsParser { public: aes::Algorithm algorithm = AES_AES128; aes::Mode mode = AES_ECB; @@ -28,54 +27,44 @@ public: std::string key; std::string iv; - explicit FileSettings(const std::string& argv0) - : SettingsParser{argv0} - { - visible.add_options() - ("algorithm,a", - boost::program_options::value<aes::Algorithm>(&algorithm) - ->required() - ->value_name("NAME"), - "set algorithm") - ("mode,m", - boost::program_options::value<aes::Mode>(&mode) - ->required() - ->value_name("MODE"), - "set mode of operation") - ("key,k", - boost::program_options::value<std::string>(&key) - ->required() - ->value_name("KEY"), - "set encryption key") - ("iv,v", - boost::program_options::value<std::string>(&iv) - ->value_name("BLOCK"), - "set initialization vector") - ("input,i", - boost::program_options::value<std::string>(&input_path) - ->required() - ->value_name("PATH"), - "set input file path") - ("output,o", - boost::program_options::value<std::string>(&output_path) - ->required() - ->value_name("PATH"), - "set output file path"); + explicit FileSettings(const std::string& argv0) : SettingsParser{argv0} { + visible.add_options()("algorithm,a", + boost::program_options::value<aes::Algorithm>(&algorithm) + ->required() + ->value_name("NAME"), + "set algorithm"); + visible.add_options()( + "mode,m", + boost::program_options::value<aes::Mode>(&mode)->required()->value_name("MODE"), + "set mode of operation"); + visible.add_options()( + "key,k", + boost::program_options::value<std::string>(&key)->required()->value_name("KEY"), + "set encryption key"); + visible.add_options()("iv,v", + boost::program_options::value<std::string>(&iv)->value_name("BLOCK"), + "set initialization vector"); + visible.add_options()( + "input,i", + boost::program_options::value<std::string>(&input_path)->required()->value_name("PATH"), + "set input file path"); + visible.add_options()("output,o", + boost::program_options::value<std::string>(&output_path) + ->required() + ->value_name("PATH"), + "set output file path"); } - const char* get_short_description() const override - { + const char* get_short_description() const override { return "[-h|--help] [-a|--algorithm NAME] [-m|--mode MODE]" " [-k|--key KEY] [-v|--iv BLOCK]" " [-i|--input PATH] [-o|--output PATH]"; } - void parse(int argc, char** argv) override - { + void parse(int argc, char** argv) override { SettingsParser::parse(argc, argv); - if (aes::mode_requires_init_vector(mode) && iv.empty()) - { + if (aes::mode_requires_init_vector(mode) && iv.empty()) { throw boost::program_options::error{ "an initialization vector is required for the selected mode of operation"}; } diff --git a/aesxx/utils/helpers/bmp.hpp b/aesxx/utils/helpers/bmp.hpp index b4b283b..c256a7a 100644 --- a/aesxx/utils/helpers/bmp.hpp +++ b/aesxx/utils/helpers/bmp.hpp @@ -7,51 +7,50 @@ #include <cstddef> #include <cstring> - #include <string> #include <utility> #include <vector> -namespace bmp -{ - class BmpFile - { - public: - BmpFile(std::vector<char>&& buffer) - : buffer{std::move(buffer)} - , header_size{extract_pixels_offset()} - { } +namespace bmp { + +class BmpFile { +public: + BmpFile(std::vector<char>&& buffer) + : buffer{std::move(buffer)}, header_size{extract_pixels_offset()} {} + + const void* get_buffer() const { + return buffer.data(); + } - const void* get_buffer() const { return buffer.data(); } + std::size_t get_size() const { + return buffer.size(); + } - std::size_t get_size() const { return buffer.size(); } + std::size_t get_header_size() const { + return header_size; + } - std::size_t get_header_size() const { return header_size; } + const void* get_pixels() const { + return buffer.data() + get_header_size(); + } - const void* get_pixels() const - { - return buffer.data() + get_header_size(); - } + std::size_t get_pixels_size() const { + return get_size() - get_header_size(); + } - std::size_t get_pixels_size() const - { - return get_size() - get_header_size(); - } + void replace_pixels(std::vector<unsigned char>&& pixels) { + buffer.resize(get_header_size() + pixels.size()); + std::memcpy(buffer.data() + get_header_size(), pixels.data(), pixels.size()); + } - void replace_pixels(std::vector<unsigned char>&& pixels) - { - buffer.resize(get_header_size() + pixels.size()); - std::memcpy(buffer.data() + get_header_size(), pixels.data(), pixels.size()); - } +private: + std::size_t extract_pixels_offset() const { + const auto header = reinterpret_cast<const BITMAPFILEHEADER*>(get_buffer()); + return header->bfOffBits; + } - private: - std::size_t extract_pixels_offset() const - { - const auto header = reinterpret_cast<const BITMAPFILEHEADER*>(get_buffer()); - return header->bfOffBits; - } + std::vector<char> buffer; + std::size_t header_size; +}; - std::vector<char> buffer; - std::size_t header_size; - }; -} +} // namespace bmp diff --git a/aesxx/utils/helpers/command_line.hpp b/aesxx/utils/helpers/command_line.hpp index 2e4d803..9a2da05 100644 --- a/aesxx/utils/helpers/command_line.hpp +++ b/aesxx/utils/helpers/command_line.hpp @@ -13,75 +13,64 @@ #include <ostream> #include <string> -namespace command_line -{ - class SettingsParser - { - public: - explicit SettingsParser(const std::string& argv0) - : prog_name{extract_filename(argv0)} - { - visible.add_options() - ("help,h", - "show this message and exit"); - } +namespace command_line { - virtual ~SettingsParser() = default; +class SettingsParser { +public: + explicit SettingsParser(const std::string& argv0) : prog_name{extract_filename(argv0)} { + visible.add_options()("help,h", "show this message and exit"); + } - virtual const char* get_short_description() const - { - return "[--option VALUE]..."; - } + virtual ~SettingsParser() = default; - virtual void parse(int argc, char* argv[]) - { - boost::program_options::options_description all; - all.add(hidden).add(visible); - boost::program_options::variables_map vm; - boost::program_options::store( - boost::program_options::command_line_parser{argc, argv} - .options(all) - .positional(positional) - .run(), - vm); - if (vm.count("help")) - exit_with_usage = true; - else - boost::program_options::notify(vm); - } + virtual const char* get_short_description() const { + return "[--option VALUE]..."; + } - bool exit_with_usage = false; + virtual void parse(int argc, char* argv[]) { + boost::program_options::options_description all; + all.add(hidden).add(visible); + boost::program_options::variables_map vm; + boost::program_options::store(boost::program_options::command_line_parser{argc, argv} + .options(all) + .positional(positional) + .run(), + vm); + if (vm.count("help")) + exit_with_usage = true; + else + boost::program_options::notify(vm); + } - void usage() const - { - std::cout << *this; - } + bool exit_with_usage = false; - void usage_error(const std::exception& e) const - { - std::cerr << "usage error: " << e.what() << '\n'; - std::cerr << *this; - } + void usage() const { + std::cout << *this; + } - protected: - boost::program_options::options_description hidden; - boost::program_options::options_description visible; - boost::program_options::positional_options_description positional; + void usage_error(const std::exception& e) const { + std::cerr << "usage error: " << e.what() << '\n'; + std::cerr << *this; + } - private: - static std::string extract_filename(const std::string& path) - { - return boost::filesystem::path{path}.filename().string(); - } +protected: + boost::program_options::options_description hidden; + boost::program_options::options_description visible; + boost::program_options::positional_options_description positional; - const std::string prog_name; +private: + static std::string extract_filename(const std::string& path) { + return boost::filesystem::path{path}.filename().string(); + } - friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser) - { - const auto short_descr = parser.get_short_description(); - os << "usage: " << parser.prog_name << ' ' << short_descr << '\n'; - os << parser.visible; - return os; - } - }; -} + const std::string prog_name; + + friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser) { + const auto short_descr = parser.get_short_description(); + os << "usage: " << parser.prog_name << ' ' << short_descr << '\n'; + os << parser.visible; + return os; + } +}; + +} // namespace command_line diff --git a/aesxx/utils/helpers/file.hpp b/aesxx/utils/helpers/file.hpp index 48e4c51..76e3e96 100644 --- a/aesxx/utils/helpers/file.hpp +++ b/aesxx/utils/helpers/file.hpp @@ -4,7 +4,6 @@ // Distributed under the MIT License. #include <cstddef> - #include <fstream> #include <iterator> #include <limits> @@ -13,57 +12,46 @@ #include <type_traits> #include <vector> -namespace file -{ - inline std::size_t cast_to_size_t(std::streamoff size) - { - if (size < 0) - throw std::range_error{"file::cast_to_size_t: something went really wrong"}; - typedef std::make_unsigned<std::streamoff>::type unsigned_streamoff; - if (static_cast<unsigned_streamoff>(size) > std::numeric_limits<std::size_t>::max()) - throw std::range_error{"file::cast_to_size_t: this file is too large"}; - return static_cast<std::size_t>(size); - } +namespace file { - inline std::size_t get_file_size(const std::string& path) - { - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary | std::ifstream::ate); - return cast_to_size_t(ifs.tellg()); - } +inline std::size_t cast_to_size_t(std::streamoff size) { + if (size < 0) + throw std::range_error{"file::cast_to_size_t: something went really wrong"}; + typedef std::make_unsigned<std::streamoff>::type unsigned_streamoff; + if (static_cast<unsigned_streamoff>(size) > std::numeric_limits<std::size_t>::max()) + throw std::range_error{"file::cast_to_size_t: this file is too large"}; + return static_cast<std::size_t>(size); +} - inline std::vector<char> read_file(const std::string& path) - { - const auto size = get_file_size(path); +inline std::size_t get_file_size(const std::string& path) { + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary | std::ifstream::ate); + return cast_to_size_t(ifs.tellg()); +} - std::ifstream ifs; - ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); - ifs.open(path, std::ifstream::binary); +inline std::vector<char> read_file(const std::string& path) { + const auto size = get_file_size(path); - std::vector<char> src_buf; - src_buf.reserve(size); - src_buf.assign( - std::istreambuf_iterator<char>{ifs}, - std::istreambuf_iterator<char>{}); - return src_buf; - } + std::ifstream ifs; + ifs.exceptions(std::ifstream::badbit | std::ifstream::failbit); + ifs.open(path, std::ifstream::binary); - inline void write_file( - const std::string& path, - const void* buffer, - const std::size_t size) - { - std::ofstream ofs; - ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); - ofs.open(path, std::ofstream::binary); - ofs.write(reinterpret_cast<const char*>(buffer), size); - } + std::vector<char> src_buf; + src_buf.reserve(size); + src_buf.assign(std::istreambuf_iterator<char>{ifs}, std::istreambuf_iterator<char>{}); + return src_buf; +} - inline void write_file( - const std::string& path, - const std::vector<unsigned char>& src) - { - write_file(path, src.data(), src.size()); - } +inline void write_file(const std::string& path, const void* buffer, const std::size_t size) { + std::ofstream ofs; + ofs.exceptions(std::ofstream::badbit | std::ofstream::failbit); + ofs.open(path, std::ofstream::binary); + ofs.write(reinterpret_cast<const char*>(buffer), size); } + +inline void write_file(const std::string& path, const std::vector<unsigned char>& src) { + write_file(path, src.data(), src.size()); +} + +} // namespace file |
