Skip to content

Commit 5e4d208

Browse files
committed
[WIP] Signature checking function
1 parent 7b0362b commit 5e4d208

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/lua-bindings/rehex.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ void print_error(const wxString &text);
1111
void bulk_updates_freeze();
1212
void bulk_updates_thaw();
1313

14+
bool _verify_signature(wxString message, wxString signature, wxString pubkey);
15+
1416
// TODO: Make this a proper enum class?
1517
enum REHex::App::SetupPhase
1618
{};

src/lua-bindings/rehex_override.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,63 @@ static int LUACALL wxLua_function_bulk_updates_thaw(lua_State *L)
5151
}
5252
%end
5353

54+
%override wxLua_function__verify_signature
55+
#include <botan/ed25519.h>
56+
#include <botan/hex.h>
57+
#include <botan/pubkey.h>
58+
59+
static int LUACALL wxLua_function__verify_signature(lua_State *L)
60+
{
61+
if(!(lua_isstring(L, 3)))
62+
{
63+
wxlua_argerror(L, 3, wxT("a hex string"));
64+
return 0;
65+
}
66+
67+
std::string pubkey_string(lua_tostring(L, 3), lua_strlen(L, 3));
68+
69+
if(!(lua_isstring(L, 2)))
70+
{
71+
wxlua_argerror(L, 2, wxT("a binary string"));
72+
return 0;
73+
}
74+
75+
const uint8_t *signature = (const uint8_t*)(lua_tostring(L, 2));
76+
size_t signature_len = lua_strlen(L, 2);
77+
78+
if(!(lua_isstring(L, 1)))
79+
{
80+
wxlua_argerror(L, 1, wxT("a binary string"));
81+
return 0;
82+
}
83+
84+
const uint8_t *message = (const uint8_t*)(lua_tostring(L, 1));
85+
size_t message_len = lua_strlen(L, 1);
86+
87+
uint8_t pubkey_bin[32];
88+
try {
89+
if(pubkey_string.length() != 64 || Botan::hex_decode(pubkey_bin, pubkey_string) != 32)
90+
{
91+
luaL_error(L, "Invalid public key");
92+
return 0;
93+
}
94+
}
95+
catch(const Botan::Exception &e)
96+
{
97+
luaL_error(L, "Invalid public key");
98+
return 0;
99+
}
100+
101+
Botan::Ed25519_PublicKey pubkey(pubkey_bin, 32);
102+
103+
Botan::PK_Verifier verifier(pubkey, "Pure");
104+
bool ok = verifier.verify_message(message, message_len, signature, signature_len);
105+
106+
lua_pushboolean(L, ok);
107+
return 1;
108+
}
109+
%end
110+
54111
%override wxLua_REHex_App_SetupHookRegistration_constructor
55112
static int LUACALL wxLua_REHex_App_SetupHookRegistration_constructor(lua_State *L)
56113
{

0 commit comments

Comments
 (0)