summaryrefslogtreecommitdiffstats
path: root/src/blake2s.h
diff options
context:
space:
mode:
authorMatt Dunwoodie <ncon@mail.noconroy.net>2019-03-24 16:19:29 +1100
committerMatt Dunwoodie <ncon@mail.noconroy.net>2019-03-25 22:48:58 +1100
commitb6f5012cf81a997507d5571f5e33090ec2c41173 (patch)
tree4f8f4934f3edb4155a9917c6b1a4c6a314b2ae4a /src/blake2s.h
parentEnsure struct is cleared before passing to kernel (diff)
downloadwireguard-openbsd-b6f5012cf81a997507d5571f5e33090ec2c41173.tar.xz
wireguard-openbsd-b6f5012cf81a997507d5571f5e33090ec2c41173.zip
Start splitting of the crypto function
The end goal is to use crypto already in the kernel (chachapoly, siphash), the first step, taken here is to integrate the crypto not already in the kernel curve25519 and blake2s.
Diffstat (limited to 'src/blake2s.h')
-rw-r--r--src/blake2s.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/blake2s.h b/src/blake2s.h
new file mode 100644
index 00000000000..0cb586eca2f
--- /dev/null
+++ b/src/blake2s.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+ */
+
+#include <sys/types.h>
+
+#ifndef _BLAKE2S_H_
+#define _BLAKE2S_H_
+
+/*#define WARN_ON(a) if(a) printf("%s failed at %s:%d\n", #a, __FILE__, __LINE__)
+#define IS_ENABLED(...) true*/
+
+
+enum blake2s_lengths {
+ BLAKE2S_BLOCK_SIZE = 64,
+ BLAKE2S_HASH_SIZE = 32,
+ BLAKE2S_KEY_SIZE = 32
+};
+
+struct blake2s_state {
+ u_int32_t h[8];
+ u_int32_t t[2];
+ u_int32_t f[2];
+ u_int8_t buf[BLAKE2S_BLOCK_SIZE];
+ size_t buflen;
+ u_int8_t last_node;
+};
+
+void blake2s_init(struct blake2s_state *state, const size_t outlen);
+void blake2s_init_key(struct blake2s_state *state, const size_t outlen,
+ const void *key, const size_t keylen);
+void blake2s_update(struct blake2s_state *state, const u_int8_t *in, size_t inlen);
+void blake2s_final(struct blake2s_state *state, u_int8_t *out, const size_t outlen);
+
+static inline void blake2s(u_int8_t *out, const u_int8_t *in, const u_int8_t *key,
+ const size_t outlen, const size_t inlen,
+ const size_t keylen)
+{
+ struct blake2s_state state;
+
+ /*WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
+ outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
+ (!key && keylen)));*/
+
+ if (keylen)
+ blake2s_init_key(&state, outlen, key, keylen);
+ else
+ blake2s_init(&state, outlen);
+
+ blake2s_update(&state, in, inlen);
+ blake2s_final(&state, out, outlen);
+}
+
+void blake2s_hmac(u_int8_t *out, const u_int8_t *in, const u_int8_t *key, const size_t outlen,
+ const size_t inlen, const size_t keylen);
+
+#endif /* _BLAKE2S_H_ */