summaryrefslogtreecommitdiffstats
path: root/src/crypto/mac
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-11-30 05:34:49 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-11-30 05:35:18 +0100
commitaa1d894e102e23d162b8e2bccd4b3bf1700de2f2 (patch)
tree3027bc84e829650a798071ad9e13f4391260328b /src/crypto/mac
parent5f7258ade2dd8bad076d17d3a85fb04d9bf71bda (diff)
downloadfastd-aa1d894e102e23d162b8e2bccd4b3bf1700de2f2.tar
fastd-aa1d894e102e23d162b8e2bccd4b3bf1700de2f2.zip
Make the crypto implementations independent of fastd.h (and fix more minor bugs)
Diffstat (limited to 'src/crypto/mac')
-rw-r--r--src/crypto/mac/ghash/builtin/ghash_builtin.c10
-rw-r--r--src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c9
-rw-r--r--src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h10
-rw-r--r--src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c30
-rw-r--r--src/crypto/mac/macs.c.in1
5 files changed, 31 insertions, 29 deletions
diff --git a/src/crypto/mac/ghash/builtin/ghash_builtin.c b/src/crypto/mac/ghash/builtin/ghash_builtin.c
index 341408f..c518663 100644
--- a/src/crypto/mac/ghash/builtin/ghash_builtin.c
+++ b/src/crypto/mac/ghash/builtin/ghash_builtin.c
@@ -61,8 +61,10 @@ static inline void mulH_a(fastd_block128_t *x, const fastd_mac_state_t *cstate)
}
-static fastd_mac_state_t* ghash_init(fastd_context_t *ctx UNUSED, const uint8_t *key) {
- fastd_mac_state_t *state = malloc(sizeof(fastd_mac_state_t));
+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_block128_t Hbase[4];
fastd_block128_t Rbase[4];
@@ -105,7 +107,7 @@ static fastd_mac_state_t* ghash_init(fastd_context_t *ctx UNUSED, const uint8_t
return state;
}
-static bool ghash_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) {
+static bool ghash_hash(const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) {
memset(out, 0, sizeof(fastd_block128_t));
size_t i;
@@ -117,7 +119,7 @@ static bool ghash_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *sta
return true;
}
-static void ghash_free(fastd_context_t *ctx UNUSED, fastd_mac_state_t *state) {
+static void ghash_free(fastd_mac_state_t *state) {
if (state) {
secure_memzero(state, sizeof(*state));
free(state);
diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c
index 8f1edb0..419d406 100644
--- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c
+++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c
@@ -34,17 +34,10 @@ static bool ghash_available(void) {
return ((fastd_cpuid()&REQ) == REQ);
}
-static void ghash_free(fastd_context_t *ctx UNUSED, fastd_mac_state_t *state) {
- if (state) {
- secure_memzero(state, sizeof(*state));
- free(state);
- }
-}
-
const fastd_mac_t fastd_mac_ghash_pclmulqdq = {
.available = ghash_available,
.init = fastd_ghash_pclmulqdq_init,
.hash = fastd_ghash_pclmulqdq_hash,
- .free = ghash_free,
+ .free = fastd_ghash_pclmulqdq_free,
};
diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h
index ccb1ecf..81313a3 100644
--- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h
+++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h
@@ -27,10 +27,6 @@
#include "../../../../crypto.h"
-struct fastd_mac_state {
- fastd_block128_t H;
-};
-
-
-fastd_mac_state_t* fastd_ghash_pclmulqdq_init(fastd_context_t *ctx, const uint8_t *key);
-bool fastd_ghash_pclmulqdq_hash(fastd_context_t *ctx, const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks);
+fastd_mac_state_t* fastd_ghash_pclmulqdq_init(const uint8_t *key);
+bool fastd_ghash_pclmulqdq_hash(const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks);
+void fastd_ghash_pclmulqdq_free(fastd_mac_state_t *state);
diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c
index 134d4ed..201ec9a 100644
--- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c
+++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c
@@ -35,6 +35,11 @@ typedef union vecblock {
fastd_block128_t b;
} vecblock_t;
+struct fastd_mac_state {
+ vecblock_t H;
+};
+
+
static inline __m128i shl(__m128i v, int a) {
__m128i tmpl = _mm_slli_epi64(v, a);
__m128i tmpr = _mm_srli_epi64(v, 64-a);
@@ -58,18 +63,24 @@ static inline __m128i byteswap(__m128i v) {
}
-fastd_mac_state_t* fastd_ghash_pclmulqdq_init(fastd_context_t *ctx UNUSED, const uint8_t *key) {
- fastd_mac_state_t *state = malloc(sizeof(fastd_mac_state_t));
-
- vecblock_t h;
- memcpy(&h, key, sizeof(__m128i));
+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();
- h.v = byteswap(h.v);
- state->H = h.b;
+ memcpy(&state->H, key, sizeof(__m128i));
+ state->H.v = byteswap(state->H.v);
return state;
}
+void fastd_ghash_pclmulqdq_free(fastd_mac_state_t *state) {
+ if (state) {
+ secure_memzero(state, sizeof(*state));
+ free(state);
+ }
+}
+
static __m128i gmul(__m128i v, __m128i h) {
/* multiply */
__m128i z0, z1, z2, tmp;
@@ -123,15 +134,14 @@ static __m128i gmul(__m128i v, __m128i h) {
}
-bool fastd_ghash_pclmulqdq_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) {
- vecblock_t h = {.b = state->H};
+bool fastd_ghash_pclmulqdq_hash(const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) {
vecblock_t v = {.v = _mm_setzero_si128()};
size_t i;
for (i = 0; i < n_blocks; i++) {
__m128i b = ((vecblock_t)in[i]).v;
v.v = _mm_xor_si128(v.v, byteswap(b));
- v.v = gmul(v.v, h.v);
+ v.v = gmul(v.v, state->H.v);
}
v.v = byteswap(v.v);
diff --git a/src/crypto/mac/macs.c.in b/src/crypto/mac/macs.c.in
index 57f327a..cdf1b10 100644
--- a/src/crypto/mac/macs.c.in
+++ b/src/crypto/mac/macs.c.in
@@ -25,6 +25,7 @@
#include <src/crypto.h>
+#include <src/fastd.h>
@MAC_DEFINITIONS@