Add Curve25519() API to Redbean

This commit is contained in:
Justine Tunney 2024-01-20 01:06:19 -08:00
parent 1226eb7a5e
commit d50064a779
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 38 additions and 1 deletions

View file

@ -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;
}

View file

@ -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 *);

View file

@ -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}, //