summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Ramacher <sebastian+dev@ramacher.at>2013-10-28 21:44:35 +0100
committerSebastian Ramacher <sebastian+dev@ramacher.at>2013-10-28 21:44:35 +0100
commit7d16bb41592155949a711815da2a39202052b423 (patch)
treefae6f0b5db2a4b1cf254e06edaaac23700041725
parent8b68505248a54477f7cb81b30e33520d9c5d1083 (diff)
downloadpycrypto-7d16bb41592155949a711815da2a39202052b423.tar.gz
Add a wrapper for posix_memalign and friends
This also fixes the order of arguments passed to _aligned_malloc. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
-rw-r--r--src/AESNI.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/AESNI.c b/src/AESNI.c
index bd03cc8..6c27225 100644
--- a/src/AESNI.c
+++ b/src/AESNI.c
@@ -45,6 +45,26 @@ typedef struct {
int rounds;
} block_state;
+/* Wrapper function for malloc with memory alignment */
+
+static void* memalign_wrapper(size_t alignment, size_t size)
+{
+#if defined(HAVE_POSIX_MEMALIGN)
+ /* posix_memalign is defined by POSIX */
+ void* tmp = NULL;
+ posix_memalign(&tmp, alignment, size);
+ return tmp;
+#elif defined(HAVE_ALIGNED_ALLOC)
+ /* aligned_alloc is defined by C11 */
+ return aligned_alloc(alignment, size);
+#elif defined(HAVE__ALIGNED_MALLOC)
+ /* _aligned_malloc is available on Windows */
+ return _aligned_malloc(size, alignment);
+#else
+#error "No function to allocate aligned memory is available."
+#endif
+}
+
/* Helper functions to expand keys */
static __m128i aes128_keyexpand(__m128i key, __m128i keygened, int shuf)
@@ -167,23 +187,8 @@ static void block_init(block_state* self, unsigned char* key, int keylen)
}
/* ensure that self->ek and self->dk are aligned to 16 byte boundaries */
- void* tek = NULL;
- void* tdk = NULL;
-#if defined(HAVE_POSIX_MEMALIGN)
- /* posix_memalign is defined by POSIX */
- posix_memalign(&tek, 16, (nr + 1) * sizeof(__m128i));
- posix_memalign(&tdk, 16, (nr + 1) * sizeof(__m128i));
-#elif defined(HAVE_ALIGNED_ALLOC)
- /* aligned_alloc is defined by C11 */
- tek = aligned_alloc(16, (nr + 1) * sizeof(__m128i));
- tdk = aligned_alloc(16, (nr + 1) * sizeof(__m128i));
-#elif defined(HAVE__ALIGNED_MALLOC)
- /* _aligned_malloc is available on Windows */
- tek = _aligned_malloc(16, (nr + 1) * sizeof(__m128i));
- tdk = _aligned_malloc(16, (nr + 1) * sizeof(__m128i));
-#else
-#error "No function to allocate aligned memory is available."
-#endif
+ void* tek = memalign_wrapper(16, (nr + 1) * sizeof(__m128i));
+ void* tdk = memalign_wrapper(16, (nr + 1) * sizeof(__m128i));
if (!tek || !tdk) {
free(tek);
free(tdk);