From 428d5035d0803f135e65e7c968d2a90888099c8d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 15 Oct 2016 06:17:58 +0300 Subject: add "portable" alignment macro --- include/aes/all.h | 1 + include/aes/workarounds.h | 14 ++++++++++++++ src/aes.c | 24 ++++++++++++------------ 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 include/aes/workarounds.h diff --git a/include/aes/all.h b/include/aes/all.h index 959e10c..ff27c18 100644 --- a/include/aes/all.h +++ b/include/aes/all.h @@ -18,3 +18,4 @@ #include "error.h" #include "mode.h" #include "padding.h" +#include "workarounds.h" diff --git a/include/aes/workarounds.h b/include/aes/workarounds.h new file mode 100644 index 0000000..45fece2 --- /dev/null +++ b/include/aes/workarounds.h @@ -0,0 +1,14 @@ +// Copyright (c) 2016 Egor Tensin +// This file is part of the "AES tools" project. +// For details, see https://github.com/egor-tensin/aes-tools. +// Distributed under the MIT License. + +#pragma once + +#if defined(_MSC_VER) +#define AES_ALIGN(t, x) __declspec(align(x)) t +#elif defined(__GNUC__) || defined(__MINGW32__) +#define AES_ALIGN(t, x) t __attribute__((aligned(x))) +#else +#warning "couldn't determine alignment attribute" +#endif diff --git a/src/aes.c b/src/aes.c index 0aded1a..1f7da13 100644 --- a/src/aes.c +++ b/src/aes.c @@ -26,7 +26,7 @@ AES_StatusCode aes_AES_format_block( char* cursor = str->str; - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; aes_store_block128_aligned(bytes, *block); for (int i = 0; i < 16; ++i, cursor += 2) @@ -51,7 +51,7 @@ AES_StatusCode aes_AES_format_block_as_matrix( char* cursor = str->str; - __declspec(align(16)) unsigned char bytes[4][4]; + AES_ALIGN(unsigned char, 16) bytes[4][4]; aes_store_block128_aligned(bytes, *block); for (int i = 0; i < 4; ++i, cursor += 3) @@ -118,7 +118,7 @@ AES_StatusCode aes_AES_parse_block( const char* cursor = src; - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; for (int i = 0; i < 16; ++i) { @@ -149,7 +149,7 @@ AES_StatusCode aes_AES128_format_key( char* cursor = str->str; - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; aes_store_block128_aligned(bytes, key->key); for (int i = 0; i < 16; ++i, cursor += 2) @@ -175,7 +175,7 @@ AES_StatusCode aes_AES192_format_key( char* cursor = str->str; { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; aes_store_block128_aligned(bytes, key->lo); for (int i = 0; i < 16; ++i, cursor += 2) @@ -183,7 +183,7 @@ AES_StatusCode aes_AES192_format_key( } { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; aes_store_block128_aligned(bytes, key->hi); for (int i = 0; i < 8; ++i, cursor += 2) @@ -210,7 +210,7 @@ AES_StatusCode aes_AES256_format_key( char* cursor = str->str; { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; aes_store_block128_aligned(bytes, key->lo); for (int i = 0; i < 16; ++i, cursor += 2) @@ -218,7 +218,7 @@ AES_StatusCode aes_AES256_format_key( } { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; aes_store_block128_aligned(bytes, key->hi); for (int i = 0; i < 16; ++i, cursor += 2) @@ -298,7 +298,7 @@ AES_StatusCode aes_AES192_parse_key( const char* cursor = src; { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; for (int i = 0; i < 16; ++i) { @@ -314,7 +314,7 @@ AES_StatusCode aes_AES192_parse_key( } { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; for (int i = 0; i < 8; ++i) { @@ -349,7 +349,7 @@ AES_StatusCode aes_AES256_parse_key( const char* cursor = src; { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; for (int i = 0; i < 16; ++i) { @@ -365,7 +365,7 @@ AES_StatusCode aes_AES256_parse_key( } { - __declspec(align(16)) unsigned char bytes[16]; + AES_ALIGN(unsigned char, 16) bytes[16]; for (int i = 0; i < 16; ++i) { -- cgit v1.2.3