diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-02-23 05:29:54 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-03-02 16:42:29 +0100 |
commit | 6e09a46be8cf93139cf3904e5411cf424d07c349 (patch) | |
tree | 1d05109c320bf67540351f197fc4d9a32fa56c25 /contrib/examples/keygen-html/src | |
parent | compat: workaround netlink refcount bug (diff) | |
download | wireguard-monolithic-historical-6e09a46be8cf93139cf3904e5411cf424d07c349.tar.xz wireguard-monolithic-historical-6e09a46be8cf93139cf3904e5411cf424d07c349.zip |
contrib: keygen-html: rewrite in pure javascript
Emscripten is too cumbersome. This code here is much slower, but it's
shorter and simpler.
Diffstat (limited to 'contrib/examples/keygen-html/src')
-rw-r--r-- | contrib/examples/keygen-html/src/curve25519_generate.c | 97 | ||||
-rw-r--r-- | contrib/examples/keygen-html/src/glue.js | 25 |
2 files changed, 0 insertions, 122 deletions
diff --git a/contrib/examples/keygen-html/src/curve25519_generate.c b/contrib/examples/keygen-html/src/curve25519_generate.c deleted file mode 100644 index fc47575..0000000 --- a/contrib/examples/keygen-html/src/curve25519_generate.c +++ /dev/null @@ -1,97 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. - */ - -#include <emscripten.h> - -typedef unsigned long long u64; -typedef unsigned int u32; -typedef unsigned char u8; -typedef u32 __le32; - -enum { CURVE25519_POINT_SIZE = 32 }; - -#ifndef __always_inline -#define __always_inline __inline __attribute__((__always_inline__)) -#endif -#ifndef noinline -#define noinline __attribute__((noinline)) -#endif -#ifndef __aligned -#define __aligned(x) __attribute__((aligned(x))) -#endif -#ifndef __force -#define __force -#endif - -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define le32_to_cpup(a) (*(a)) -#else -#define le32_to_cpup(a) __builtin_bswap32(*(a)) -#endif - -#define memset(a, b, c) __builtin_memset(a, b, c) -#define memcpy(a, b, c) __builtin_memcpy(a, b, c) -#define memmove(a, b, c) __builtin_memmove(a, b, c) -/* We don't even attempt to deal with this in javascript. */ -#define memzero_explicit(a, b) - -static __always_inline void normalize_secret(u8 secret[CURVE25519_POINT_SIZE]) -{ - secret[0] &= 248; - secret[31] &= 127; - secret[31] |= 64; -} - -#include "../../../../src/crypto/curve25519-fiat32.h" - -EMSCRIPTEN_KEEPALIVE void curve25519_generate_public(u8 public[static 32], const u8 private[static 32]) -{ - static const u8 basepoint[32] = { 9 }; - - curve25519_generic(public, private, basepoint); -} - -EMSCRIPTEN_KEEPALIVE void curve25519_generate_private(u8 private[static 32]) -{ - int i; - - EM_ASM({ - /* Same trick as libsodium */ - var getRandomValue = function() { - var buf = new Uint32Array(1); - window.crypto.getRandomValues(buf); - return buf[0] >>> 0; - }; - Module.getRandomValue = getRandomValue; - }); - - for (i = 0; i < 32; ++i) - private[i] = EM_ASM_INT_V({ return Module.getRandomValue(); }); - normalize_secret(private); -} - -static inline void encode_base64(char dest[4], const u8 src[3]) -{ - const u8 input[] = { (src[0] >> 2) & 63, ((src[0] << 4) | (src[1] >> 4)) & 63, ((src[1] << 2) | (src[2] >> 6)) & 63, src[2] & 63 }; - - for (unsigned int i = 0; i < 4; ++i) - dest[i] = input[i] + 'A' - + (((25 - input[i]) >> 8) & 6) - - (((51 - input[i]) >> 8) & 75) - - (((61 - input[i]) >> 8) & 15) - + (((62 - input[i]) >> 8) & 3); - -} - -EMSCRIPTEN_KEEPALIVE void key_to_base64(char base64[static 45], const u8 key[static 32]) -{ - unsigned int i; - - for (i = 0; i < 32 / 3; ++i) - encode_base64(&base64[i * 4], &key[i * 3]); - encode_base64(&base64[i * 4], (const u8[]){ key[i * 3 + 0], key[i * 3 + 1], 0 }); - base64[45 - 2] = '='; - base64[45 - 1] = '\0'; -} diff --git a/contrib/examples/keygen-html/src/glue.js b/contrib/examples/keygen-html/src/glue.js deleted file mode 100644 index 981e533..0000000 --- a/contrib/examples/keygen-html/src/glue.js +++ /dev/null @@ -1,25 +0,0 @@ -/*! SPDX-License-Identifier: GPL-2.0 - * - * Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. - */ -window["WireGuard"] = { - "generateKeypair": function() { - var privateKey = Module["_malloc"](32); - var publicKey = Module["_malloc"](32); - Module["_curve25519_generate_private"](privateKey); - Module["_curve25519_generate_public"](publicKey, privateKey); - var privateBase64 = Module["_malloc"](45); - var publicBase64 = Module["_malloc"](45); - Module["_key_to_base64"](privateBase64, privateKey); - Module["_key_to_base64"](publicBase64, publicKey); - Module["_free"](privateKey); - Module["_free"](publicKey); - var keypair = { - publicKey: Module["Pointer_stringify"](publicBase64), - privateKey: Module["Pointer_stringify"](privateBase64) - }; - Module["_free"](privateBase64); - Module["_free"](publicBase64); - return keypair; - } -}; |