summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Ramacher <sebastian+dev@ramacher.at>2014-02-22 17:07:50 +0100
committerSebastian Ramacher <sebastian+dev@ramacher.at>2014-02-22 17:07:50 +0100
commit694f536eb05c5dea85ad0488fedef122bb124237 (patch)
tree3463a3ce56c735137d446a376ae11b3fbbc461f3
parent7d16bb41592155949a711815da2a39202052b423 (diff)
downloadpycrypto-694f536eb05c5dea85ad0488fedef122bb124237.tar.gz
Add wrapper for free
For _aligned_malloc calling free is illegal. We need to use_aligned_free instead. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
-rw-r--r--src/AESNI.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/AESNI.c b/src/AESNI.c
index 6c27225..16119e0 100644
--- a/src/AESNI.c
+++ b/src/AESNI.c
@@ -45,7 +45,7 @@ typedef struct {
int rounds;
} block_state;
-/* Wrapper function for malloc with memory alignment */
+/* Wrapper function for malloc and free with memory alignment */
static void* memalign_wrapper(size_t alignment, size_t size)
{
@@ -61,7 +61,20 @@ static void* memalign_wrapper(size_t alignment, size_t size)
/* _aligned_malloc is available on Windows */
return _aligned_malloc(size, alignment);
#else
-#error "No function to allocate aligned memory is available."
+#error "No function to allocate/free aligned memory is available."
+#endif
+}
+
+static void free_wrapper(void* ptr)
+{
+#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_ALIGNED_ALLOC)
+ /* free is fine for aligned_alloc and posix_memalign */
+ free(ptr);
+#elif defined(HAVE__ALIGNED_MALLOC)
+ /* _aligned_malloc requires _aligned_free */
+ _aligned_free(ptr);
+#else
+#error "No function to allocate/free aligned memory is available."
#endif
}
@@ -190,8 +203,8 @@ static void block_init(block_state* self, unsigned char* key, int keylen)
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);
+ free_wrapper(tek);
+ free_wrapper(tdk);
PyErr_SetString(PyExc_MemoryError,
"failed to allocate memory for keys");
return;
@@ -211,8 +224,8 @@ static void block_finalize(block_state* self)
memset(self->ek, 0, (self->rounds + 1) * sizeof(__m128i));
memset(self->dk, 0, (self->rounds + 1) * sizeof(__m128i));
- free(self->ek);
- free(self->dk);
+ free_wrapper(self->ek);
+ free_wrapper(self->dk);
}
static void block_encrypt(block_state* self, const u8* in, u8* out)