Skip to content

Commit

Permalink
Add Curve25519() API to Redbean
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jan 20, 2024
1 parent 1226eb7 commit d50064a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
36 changes: 36 additions & 0 deletions tool/net/lfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions tool/net/lfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
2 changes: 1 addition & 1 deletion tool/net/redbean.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -5161,6 +5160,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"Compress", LuaCompress}, //
{"Crc32", LuaCrc32}, //
{"Crc32c", LuaCrc32c}, //
{"Curve25519", LuaCurve25519}, //
{"Decimate", LuaDecimate}, //
{"DecodeBase32", LuaDecodeBase32}, //
{"DecodeBase64", LuaDecodeBase64}, //
Expand Down

1 comment on commit d50064a

@solisoft
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works just fine for me thanks!

Please sign in to comment.