diff --git a/tool/net/lfuncs.c b/tool/net/lfuncs.c index 1e0633511..f31c70466 100644 --- a/tool/net/lfuncs.c +++ b/tool/net/lfuncs.c @@ -63,6 +63,7 @@ #include "third_party/lua/lua.h" #include "third_party/lua/luaconf.h" #include "third_party/lua/lunix.h" +#include "third_party/mbedtls/everest.h" #include "third_party/mbedtls/md.h" #include "third_party/mbedtls/md5.h" #include "third_party/mbedtls/platform.h" @@ -1136,3 +1137,38 @@ int LuaInflate(lua_State *L) { luaL_pushresultsize(&buf, actualoutsize); return 1; } + +static void GetCurve25519Arg(lua_State *L, int arg, uint8_t buf[static 32]) { + size_t len; + const char *val; + val = luaL_checklstring(L, arg, &len); + bzero(buf, 32); + if (len) { + if (len > 32) { + len = 32; + } + memcpy(buf, val, len); + } +} + +/* + * Example usage: + * + * >: secret1 = "\1" + * >: secret2 = "\2" + * >: public1 = Curve25519(secret1, "\9") + * >: public2 = Curve25519(secret2, "\9") + * >: Curve25519(secret1, public2) + * "\x93\xfe\xa2\xa7\xc1\xae\xb6,\xfddR\xff... + * >: Curve25519(secret2, public1) + * "\x93\xfe\xa2\xa7\xc1\xae\xb6,\xfddR\xff... + * + */ +int LuaCurve25519(lua_State *L) { + uint8_t mypublic[32], secret[32], basepoint[32]; + GetCurve25519Arg(L, 1, secret); + GetCurve25519Arg(L, 2, basepoint); + curve25519(mypublic, secret, basepoint); + lua_pushlstring(L, (const char *)mypublic, 32); + return 1; +} diff --git a/tool/net/lfuncs.h b/tool/net/lfuncs.h index d44c9433b..17e1fbcba 100644 --- a/tool/net/lfuncs.h +++ b/tool/net/lfuncs.h @@ -18,6 +18,7 @@ int LuaCategorizeIp(lua_State *); int LuaCompress(lua_State *); int LuaCrc32(lua_State *); int LuaCrc32c(lua_State *); +int LuaCurve25519(lua_State *); int LuaDecimate(lua_State *); int LuaDecodeBase32(lua_State *); int LuaDecodeBase64(lua_State *); diff --git a/tool/net/redbean.c b/tool/net/redbean.c index ca0937800..4f76409c1 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -49,7 +49,6 @@ #include "libc/math.h" #include "libc/mem/alloca.h" #include "libc/mem/gc.h" -#include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/crc32.h" #include "libc/nexgen32e/rdtsc.h" @@ -5161,6 +5160,7 @@ static const luaL_Reg kLuaFuncs[] = { {"Compress", LuaCompress}, // {"Crc32", LuaCrc32}, // {"Crc32c", LuaCrc32c}, // + {"Curve25519", LuaCurve25519}, // {"Decimate", LuaDecimate}, // {"DecodeBase32", LuaDecodeBase32}, // {"DecodeBase64", LuaDecodeBase64}, //