From 181715c5bc6e74f87fe284b063ca301a300ad098 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 1 Sep 2014 22:03:43 +0200 Subject: Add alloc helpers for aligned allocations --- src/alloc.h | 17 +++++++++++++++++ src/buffer.h | 7 ++----- .../cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c | 6 ++---- src/crypto/mac/ghash/builtin/ghash_builtin.c | 6 ++---- src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c | 6 ++---- src/fastd.h | 1 - 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/alloc.h b/src/alloc.h index 7b3e93a..740ec24 100644 --- a/src/alloc.h +++ b/src/alloc.h @@ -48,6 +48,20 @@ static inline void * fastd_alloc(size_t size) { return ret; } +/** + Allocates a block of uninitialized memory on the heap, aligned to 16 bytes + + Terminates the process on failure. +*/ +static inline void * fastd_alloc_aligned(size_t size, size_t align) { + void *ret; + int err = posix_memalign(&ret, align, size); + if (err) + exit_error("posix_memalign: %s", strerror(err)); + + return ret; +} + /** Allocates a block of memory set to zero for an array on the heap @@ -87,6 +101,9 @@ static inline void * fastd_realloc(void *ptr, size_t size) { /** Allocates a block of uninitialized memory in the size of a given type */ #define fastd_new(type) ((type *)fastd_alloc(sizeof(type))) +/** Allocates a block of uninitialized memory in the size of a given type, aligned to 16 bytes */ +#define fastd_new_aligned(type, align) ((type *)fastd_alloc_aligned(sizeof(type), align)) + /** Allocates a block of memory set to zero in the size of a given type */ #define fastd_new0(type) ((type *)fastd_alloc0(sizeof(type))) diff --git a/src/buffer.h b/src/buffer.h index 5b2e16a..58e8023 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -32,7 +32,7 @@ #pragma once -#include "log.h" +#include "alloc.h" /** A buffer descriptor */ @@ -55,10 +55,7 @@ struct fastd_buffer { */ static inline fastd_buffer_t fastd_buffer_alloc(const size_t len, size_t head_space, size_t tail_space) { size_t base_len = head_space+len+tail_space; - void *ptr; - int err = posix_memalign(&ptr, 16, base_len); - if (err) - exit_error("posix_memalign: %s", strerror(err)); + void *ptr = fastd_alloc_aligned(base_len, 16); return (fastd_buffer_t){ .base = ptr, .base_len = base_len, .data = ptr+head_space, .len = len }; } diff --git a/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c b/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c index c9adfcd..9611b11 100644 --- a/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c +++ b/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c @@ -31,6 +31,7 @@ #include "../../../../crypto.h" +#include "../../../../alloc.h" #include @@ -46,10 +47,7 @@ static fastd_cipher_state_t * aes128_ctr_init(const uint8_t *key) { fastd_block128_t k; memcpy(k.b, key, sizeof(fastd_block128_t)); - fastd_cipher_state_t *state; - if (posix_memalign((void **)&state, 16, sizeof(fastd_cipher_state_t))) - abort(); - + fastd_cipher_state_t *state = fastd_new_aligned(fastd_cipher_state_t, 16); crypto_stream_aes128ctr_beforenm(state->d, k.b); return state; diff --git a/src/crypto/mac/ghash/builtin/ghash_builtin.c b/src/crypto/mac/ghash/builtin/ghash_builtin.c index 32ed5e7..2e70f52 100644 --- a/src/crypto/mac/ghash/builtin/ghash_builtin.c +++ b/src/crypto/mac/ghash/builtin/ghash_builtin.c @@ -31,7 +31,7 @@ #include "../../../../crypto.h" -#include "../../../../log.h" +#include "../../../../alloc.h" /** MAC state used by this GHASH implmentation */ @@ -74,9 +74,7 @@ static inline void mulH_a(fastd_block128_t *x, const fastd_mac_state_t *cstate) /** Initializes the MAC state with the unpacked key data */ static fastd_mac_state_t * ghash_init(const uint8_t *key) { - fastd_mac_state_t *state; - if (posix_memalign((void **)&state, 16, sizeof(fastd_mac_state_t))) - abort(); + fastd_mac_state_t *state = fastd_new_aligned(fastd_mac_state_t, 16); fastd_block128_t Hbase[4]; fastd_block128_t Rbase[4]; diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c index 6117735..040ce14 100644 --- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c +++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c @@ -31,7 +31,7 @@ #include "ghash_pclmulqdq.h" -#include "../../../../log.h" +#include "../../../../alloc.h" #include #include @@ -79,9 +79,7 @@ static inline __m128i byteswap(__m128i v) { /** Initializes the state used by this GHASH implementation */ fastd_mac_state_t * fastd_ghash_pclmulqdq_init(const uint8_t *key) { - fastd_mac_state_t *state; - if (posix_memalign((void **)&state, 16, sizeof(fastd_mac_state_t))) - abort(); + fastd_mac_state_t *state = fastd_new_aligned(fastd_mac_state_t, 16); memcpy(&state->H, key, sizeof(__m128i)); state->H.v = byteswap(state->H.v); diff --git a/src/fastd.h b/src/fastd.h index acdc572..79d72e1 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -32,7 +32,6 @@ #pragma once -#include "alloc.h" #include "dlist.h" #include "buffer.h" #include "log.h" -- cgit v1.2.3