summaryrefslogtreecommitdiffstats
path: root/src/crypto.h
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-09-21 15:07:11 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-09-21 15:10:28 +0200
commit2acd81bd7a1b364b02831ae5f8e46457d9d07865 (patch)
tree969429177db12b56e402ad767531189b7a41834c /src/crypto.h
parent65912e3e6fce703b03eafc2b4bf11a17a02bd39e (diff)
downloadfastd-2acd81bd7a1b364b02831ae5f8e46457d9d07865.tar
fastd-2acd81bd7a1b364b02831ae5f8e46457d9d07865.zip
Nicely encapsulate different crypto algorithm implementations
Diffstat (limited to 'src/crypto.h')
-rw-r--r--src/crypto.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/crypto.h b/src/crypto.h
new file mode 100644
index 0000000..89c44d6
--- /dev/null
+++ b/src/crypto.h
@@ -0,0 +1,77 @@
+/*
+ Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#ifndef _FASTD_CRYPTO_H_
+#define _FASTD_CRYPTO_H_
+
+#include "types.h"
+
+#include <stdint.h>
+
+
+typedef union _fastd_block128 {
+ uint8_t b[16];
+ uint64_t qw[2];
+} __attribute__((aligned(16))) fastd_block128;
+
+
+#ifdef USE_CRYPTO_AES128CTR
+struct _fastd_crypto_aes128ctr {
+ const char *name;
+
+ fastd_crypto_aes128ctr_context* (*init)(fastd_context *ctx);
+ fastd_crypto_aes128ctr_state* (*set_key)(fastd_context *ctx, const fastd_crypto_aes128ctr_context *cctx, const fastd_block128 *key);
+ bool (*crypt)(fastd_context *ctx, const fastd_crypto_aes128ctr_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t len, const fastd_block128 *iv);
+
+ void (*free_state)(fastd_context *ctx, fastd_crypto_aes128ctr_state *cstate);
+ void (*free)(fastd_context *ctx, fastd_crypto_aes128ctr_context *cctx);
+};
+#endif
+
+#ifdef USE_CRYPTO_GHASH
+struct _fastd_crypto_ghash {
+ const char *name;
+
+ fastd_crypto_ghash_context* (*init)(fastd_context *ctx);
+ fastd_crypto_ghash_state* (*set_h)(fastd_context *ctx, const fastd_crypto_ghash_context *cctx, const fastd_block128 *h);
+ bool (*hash)(fastd_context *ctx, const fastd_crypto_ghash_state *cstate, fastd_block128 *out, const fastd_block128 *in, size_t n_blocks);
+
+ void (*free_state)(fastd_context *ctx, fastd_crypto_ghash_state *cstate);
+ void (*free)(fastd_context *ctx, fastd_crypto_ghash_context *cctx);
+};
+#endif
+
+
+static inline void xor(fastd_block128 *x, const fastd_block128 *a, const fastd_block128 *b) {
+ x->qw[0] = a->qw[0] ^ b->qw[0];
+ x->qw[1] = a->qw[1] ^ b->qw[1];
+}
+
+static inline void xor_a(fastd_block128 *x, const fastd_block128 *a) {
+ xor(x, x, a);
+}
+
+#endif /* _FASTD_CRYPTO_H_ */