Skip to content

Commit d50064a

Browse files
committed
Add Curve25519() API to Redbean
1 parent 1226eb7 commit d50064a

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

tool/net/lfuncs.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "third_party/lua/lua.h"
6464
#include "third_party/lua/luaconf.h"
6565
#include "third_party/lua/lunix.h"
66+
#include "third_party/mbedtls/everest.h"
6667
#include "third_party/mbedtls/md.h"
6768
#include "third_party/mbedtls/md5.h"
6869
#include "third_party/mbedtls/platform.h"
@@ -1136,3 +1137,38 @@ int LuaInflate(lua_State *L) {
11361137
luaL_pushresultsize(&buf, actualoutsize);
11371138
return 1;
11381139
}
1140+
1141+
static void GetCurve25519Arg(lua_State *L, int arg, uint8_t buf[static 32]) {
1142+
size_t len;
1143+
const char *val;
1144+
val = luaL_checklstring(L, arg, &len);
1145+
bzero(buf, 32);
1146+
if (len) {
1147+
if (len > 32) {
1148+
len = 32;
1149+
}
1150+
memcpy(buf, val, len);
1151+
}
1152+
}
1153+
1154+
/*
1155+
* Example usage:
1156+
*
1157+
* >: secret1 = "\1"
1158+
* >: secret2 = "\2"
1159+
* >: public1 = Curve25519(secret1, "\9")
1160+
* >: public2 = Curve25519(secret2, "\9")
1161+
* >: Curve25519(secret1, public2)
1162+
* "\x93\xfe\xa2\xa7\xc1\xae\xb6,\xfddR\xff...
1163+
* >: Curve25519(secret2, public1)
1164+
* "\x93\xfe\xa2\xa7\xc1\xae\xb6,\xfddR\xff...
1165+
*
1166+
*/
1167+
int LuaCurve25519(lua_State *L) {
1168+
uint8_t mypublic[32], secret[32], basepoint[32];
1169+
GetCurve25519Arg(L, 1, secret);
1170+
GetCurve25519Arg(L, 2, basepoint);
1171+
curve25519(mypublic, secret, basepoint);
1172+
lua_pushlstring(L, (const char *)mypublic, 32);
1173+
return 1;
1174+
}

tool/net/lfuncs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int LuaCategorizeIp(lua_State *);
1818
int LuaCompress(lua_State *);
1919
int LuaCrc32(lua_State *);
2020
int LuaCrc32c(lua_State *);
21+
int LuaCurve25519(lua_State *);
2122
int LuaDecimate(lua_State *);
2223
int LuaDecodeBase32(lua_State *);
2324
int LuaDecodeBase64(lua_State *);

tool/net/redbean.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#include "libc/math.h"
5050
#include "libc/mem/alloca.h"
5151
#include "libc/mem/gc.h"
52-
#include "libc/mem/gc.h"
5352
#include "libc/mem/mem.h"
5453
#include "libc/nexgen32e/crc32.h"
5554
#include "libc/nexgen32e/rdtsc.h"
@@ -5161,6 +5160,7 @@ static const luaL_Reg kLuaFuncs[] = {
51615160
{"Compress", LuaCompress}, //
51625161
{"Crc32", LuaCrc32}, //
51635162
{"Crc32c", LuaCrc32c}, //
5163+
{"Curve25519", LuaCurve25519}, //
51645164
{"Decimate", LuaDecimate}, //
51655165
{"DecodeBase32", LuaDecodeBase32}, //
51665166
{"DecodeBase64", LuaDecodeBase64}, //

0 commit comments

Comments
 (0)