summaryrefslogtreecommitdiffstats
path: root/src/crypto
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-11-29 05:33:12 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-11-29 05:35:49 +0100
commit61349d3d273aa23935b0c413c5885005db2669db (patch)
tree9cbc05acb31476d45b48d4a51e9edca19328b8e8 /src/crypto
parentc13fe36e4c0730037ae75d51f7f052d916486aac (diff)
downloadfastd-61349d3d273aa23935b0c413c5885005db2669db.tar
fastd-61349d3d273aa23935b0c413c5885005db2669db.zip
Compile with -std=c99 and restructure some code to ensure there is no invalid aliasing (hopefully)
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c6
-rw-r--r--src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c33
-rw-r--r--src/crypto/mac/ghash/builtin/ghash_builtin.c26
-rw-r--r--src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c21
4 files changed, 49 insertions, 37 deletions
diff --git a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c
index 37ed95c..6917333 100644
--- a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c
+++ b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c
@@ -41,7 +41,7 @@ static fastd_cipher_state_t* aes128_ctr_init_state(fastd_context_t *ctx UNUSED,
fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t));
state->aes = EVP_CIPHER_CTX_new();
- EVP_EncryptInit(state->aes, EVP_aes_128_ctr(), (const void*)key, NULL);
+ EVP_EncryptInit(state->aes, EVP_aes_128_ctr(), (const unsigned char*)key, NULL);
return state;
}
@@ -52,10 +52,10 @@ static bool aes128_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_sta
if (!EVP_EncryptInit(state->aes, NULL, NULL, iv))
return false;
- if (!EVP_EncryptUpdate(state->aes, (void*)out, &clen, (const void*)in, len))
+ if (!EVP_EncryptUpdate(state->aes, (unsigned char*)out, &clen, (const unsigned char*)in, len))
return false;
- if (!EVP_EncryptFinal(state->aes, ((void*)out) + clen, &clen2))
+ if (!EVP_EncryptFinal(state->aes, ((unsigned char*)out) + clen, &clen2))
return false;
if ((size_t)(clen+clen2) != len)
diff --git a/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c b/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c
index 8c18203..70c4d35 100644
--- a/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c
+++ b/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c
@@ -26,6 +26,13 @@
#include "../../../../crypto.h"
+
+typedef union bf_block {
+ fastd_block128_t b;
+ uint32_t u32[4];
+} bf_block_t;
+
+
static const uint32_t Sdefault[4][256] = {
{
0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
@@ -240,24 +247,30 @@ static fastd_cipher_state_t* blowfish_ctr_init_state(fastd_context_t *ctx UNUSED
}
static bool blowfish_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) {
+ register bf_block_t block;
register uint32_t ctr[2];
- register uint32_t block[2];
-
- uint32_t *out4 = (uint32_t*)out;
- uint32_t *in4 = (uint32_t*)in;
ctr[0] = (iv[0] << 24)|(iv[1] << 16)|(iv[2] << 8)|(iv[3]);
ctr[1] = (iv[4] << 24)|(iv[5] << 16)|(iv[6] << 8)|(iv[7]);
size_t i;
- for(i = 0; i < len; i += 8) {
- block[0] = ctr[0];
- block[1] = ctr[1];
- BF_ENCRYPT(state, block[0], block[1]);
+ for(i = 0; i < len; i += 16) {
+ block.u32[0] = ctr[0];
+ block.u32[1] = ctr[1];
+ BF_ENCRYPT(state, block.u32[0], block.u32[1]);
+ ctr[1]++;
- *(out4++) = *(in4++) ^ htonl(block[0]);
- *(out4++) = *(in4++) ^ htonl(block[1]);
+ block.u32[2] = ctr[0];
+ block.u32[3] = ctr[1];
+ BF_ENCRYPT(state, block.u32[2], block.u32[3]);
ctr[1]++;
+
+ block.u32[0] = htonl(block.u32[0]);
+ block.u32[1] = htonl(block.u32[1]);
+ block.u32[2] = htonl(block.u32[2]);
+ block.u32[3] = htonl(block.u32[3]);
+
+ xor(out++, *(in++), block.b);
}
return true;
diff --git a/src/crypto/mac/ghash/builtin/ghash_builtin.c b/src/crypto/mac/ghash/builtin/ghash_builtin.c
index cc81e74..511e844 100644
--- a/src/crypto/mac/ghash/builtin/ghash_builtin.c
+++ b/src/crypto/mac/ghash/builtin/ghash_builtin.c
@@ -35,13 +35,13 @@ struct fastd_mac_state {
static const fastd_block128_t r = { .b = {0xe1} };
-static inline uint8_t shr(fastd_block128_t *out, const fastd_block128_t *in, int n) {
+static inline uint8_t shr(fastd_block128_t *out, fastd_block128_t in, int n) {
size_t i;
uint8_t c = 0;
for (i = 0; i < sizeof(fastd_block128_t); i++) {
- uint8_t c2 = in->b[i] << (8-n);
- out->b[i] = (in->b[i] >> n) | c;
+ uint8_t c2 = in.b[i] << (8-n);
+ out->b[i] = (in.b[i] >> n) | c;
c = c2;
}
@@ -53,8 +53,8 @@ static inline void mulH_a(fastd_block128_t *x, const fastd_mac_state_t *cstate)
int i;
for (i = 0; i < 16; i++) {
- xor_a(&out, &cstate->H[2*i][x->b[i]>>4]);
- xor_a(&out, &cstate->H[2*i+1][x->b[i]&0xf]);
+ xor_a(&out, cstate->H[2*i][x->b[i]>>4]);
+ xor_a(&out, cstate->H[2*i+1][x->b[i]&0xf]);
}
*x = out;
@@ -76,11 +76,11 @@ static fastd_mac_state_t* ghash_init_state(fastd_context_t *ctx UNUSED, const fa
int i;
for (i = 1; i < 4; i++) {
- uint8_t carry = shr(&Hbase[i], &Hbase[i-1], 1);
+ uint8_t carry = shr(&Hbase[i], Hbase[i-1], 1);
if (carry)
- xor_a(&Hbase[i], &r);
+ xor_a(&Hbase[i], r);
- shr(&Rbase[i], &Rbase[i-1], 1);
+ shr(&Rbase[i], Rbase[i-1], 1);
}
fastd_block128_t R[16];
@@ -91,8 +91,8 @@ static fastd_mac_state_t* ghash_init_state(fastd_context_t *ctx UNUSED, const fa
int j;
for (j = 0; j < 4; j++) {
if (i & (8 >> j)) {
- xor_a(&state->H[0][i], &Hbase[j]);
- xor_a(&R[i], &Rbase[j]);
+ xor_a(&state->H[0][i], Hbase[j]);
+ xor_a(&R[i], Rbase[j]);
}
}
}
@@ -101,8 +101,8 @@ static fastd_mac_state_t* ghash_init_state(fastd_context_t *ctx UNUSED, const fa
int j;
for (j = 0; j < 16; j++) {
- uint8_t carry = shr(&state->H[i][j], &state->H[i-1][j], 4);
- xor_a(&state->H[i][j], &R[carry]);
+ uint8_t carry = shr(&state->H[i][j], state->H[i-1][j], 4);
+ xor_a(&state->H[i][j], R[carry]);
}
}
@@ -114,7 +114,7 @@ static bool ghash_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *sta
size_t i;
for (i = 0; i < n_blocks; i++) {
- xor_a(out, &in[i]);
+ xor_a(out, in[i]);
mulH_a(out, state);
}
diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c
index 0abff8c..375cf91 100644
--- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c
+++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c
@@ -30,10 +30,10 @@
#include <tmmintrin.h>
-typedef union _vecblock {
+typedef union vecblock {
__m128i v;
fastd_block128_t b;
-} vecblock;
+} vecblock_t;
static inline __m128i shl(__m128i v, int a) {
__m128i tmpl = _mm_slli_epi64(v, a);
@@ -61,7 +61,7 @@ static inline __m128i byteswap(__m128i v) {
fastd_mac_state_t* fastd_ghash_pclmulqdq_init_state(fastd_context_t *ctx UNUSED, const fastd_mac_context_t *mctx UNUSED, const uint8_t *key) {
fastd_mac_state_t *state = malloc(sizeof(fastd_mac_state_t));
- vecblock h;
+ vecblock_t h;
memcpy(&h, key, sizeof(__m128i));
h.v = byteswap(h.v);
@@ -124,19 +124,18 @@ 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) {
- const __m128i *inv = (const __m128i*)in;
-
- __m128i h = ((vecblock*)&state->H)->v;
- __m128i v = _mm_setzero_si128();
+ vecblock_t h = {.b = state->H};
+ vecblock_t v = {.v = _mm_setzero_si128()};
size_t i;
for (i = 0; i < n_blocks; i++) {
- __m128i b = inv[i];
- v = _mm_xor_si128(v, byteswap(b));
- v = gmul(v, h);
+ __m128i b = ((vecblock_t)in[i]).v;
+ v.v = _mm_xor_si128(v.v, byteswap(b));
+ v.v = gmul(v.v, h.v);
}
- ((vecblock*)out)->v = byteswap(v);
+ v.v = byteswap(v.v);
+ *out = v.b;
return true;
}