45 #include "rust-bindings.h"
47 #define SHA256_MT "suricata:hashlib:sha256"
48 #define SHA1_MT "suricata:hashlib:sha1"
49 #define MD5_MT "suricata:hashlib:md5"
54 static int LuaHashLibSha256New(
lua_State *L)
58 return luaL_error(L,
"failed to allocate userdata for sha256");
60 *hasher = SCSha256New();
62 lua_setmetatable(L, -2);
69 static int LuaHashLibSha256Update(
lua_State *L)
73 return luaL_error(L,
"null userdata");
76 const char *data = luaL_checklstring(L, 2, &data_len);
77 SCSha256Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
81 static int LuaHashLibSha256Finalize(
lua_State *L)
85 return luaL_error(L,
"null userdata");
89 SCSha256Finalize(*hasher, hash,
sizeof(hash));
90 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
99 static int LuaHashLibSha256FinalizeToHex(
lua_State *L)
102 if (hasher == NULL) {
103 return luaL_error(L,
"null userdata");
106 char hash[SC_SHA256_HEX_LEN + 1];
107 if (!SCSha256FinalizeToHex(*hasher, hash,
sizeof(hash))) {
109 return luaL_error(L,
"sha256 hashing failed");
112 lua_pushstring(L, (
const char *)hash);
121 static int LuaHashLibSha256Digest(
lua_State *L)
124 const char *input = luaL_checklstring(L, 1, &buf_len);
127 if (!SCSha256HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
SC_SHA256_LEN)) {
128 return luaL_error(L,
"sha256 hashing failed");
136 static int LuaHashLibSha256HexDigest(
lua_State *L)
139 const char *input = luaL_checklstring(L, 1, &buf_len);
141 char output[SC_SHA256_HEX_LEN + 1];
142 if (!SCSha256HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
143 return luaL_error(L,
"sha256 hashing failed");
146 lua_pushstring(L, (
const char *)output);
150 static int LuaHashLibSha256Gc(
lua_State *L)
153 if (hasher && *hasher) {
154 SCSha256Free(*hasher);
159 static int LuaHashLibSha1New(
lua_State *L)
161 struct SCSha1 **hasher = lua_newuserdata(L,
sizeof(
struct SCSha1 *));
162 if (hasher == NULL) {
163 return luaL_error(L,
"failed to allocate userdata for sha1");
165 *hasher = SCSha1New();
167 lua_setmetatable(L, -2);
171 static int LuaHashLibSha1Update(
lua_State *L)
174 if (hasher == NULL) {
175 return luaL_error(L,
"null userdata");
179 const char *data = luaL_checklstring(L, 2, &data_len);
180 SCSha1Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
184 static int LuaHashLibSha1Finalize(
lua_State *L)
187 if (hasher == NULL) {
188 return luaL_error(L,
"null userdata");
192 SCSha1Finalize(*hasher, hash,
sizeof(hash));
193 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
202 static int LuaHashLibSha1FinalizeToHex(
lua_State *L)
205 if (hasher == NULL) {
206 return luaL_error(L,
"null userdata");
209 char hash[SC_SHA1_HEX_LEN + 1];
210 if (!SCSha1FinalizeToHex(*hasher, hash,
sizeof(hash))) {
212 return luaL_error(L,
"sha1 hashing failed");
215 lua_pushstring(L, (
const char *)hash);
224 static int LuaHashLibSha1Digest(
lua_State *L)
227 const char *input = luaL_checklstring(L, 1, &buf_len);
230 if (!SCSha1HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
231 return luaL_error(L,
"sha1 hashing failed");
234 lua_pushlstring(L, (
const char *)output,
sizeof(output));
238 static int LuaHashLibSha1HexDigest(
lua_State *L)
241 const char *input = luaL_checklstring(L, 1, &buf_len);
243 char output[SC_SHA1_HEX_LEN + 1];
244 if (!SCSha1HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
245 return luaL_error(L,
"sha1 hashing failed");
248 lua_pushstring(L, (
const char *)output);
252 static int LuaHashLibSha1Gc(
lua_State *L)
255 if (hasher && *hasher) {
261 static int LuaHashLibMd5New(
lua_State *L)
263 struct SCMd5 **hasher = lua_newuserdata(L,
sizeof(
struct SCMd5 *));
264 if (hasher == NULL) {
265 return luaL_error(L,
"failed to allocate userdata for sha1");
267 *hasher = SCMd5New();
268 luaL_getmetatable(L,
MD5_MT);
269 lua_setmetatable(L, -2);
273 static int LuaHashLibMd5Update(
lua_State *L)
275 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
276 if (hasher == NULL) {
277 return luaL_error(L,
"null userdata");
281 const char *data = luaL_checklstring(L, 2, &data_len);
282 SCMd5Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
286 static int LuaHashLibMd5Finalize(
lua_State *L)
288 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
289 if (hasher == NULL) {
290 return luaL_error(L,
"null userdata");
294 SCMd5Finalize(*hasher, hash,
sizeof(hash));
295 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
304 static int LuaHashLibMd5FinalizeToHex(
lua_State *L)
306 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
307 if (hasher == NULL) {
308 return luaL_error(L,
"null userdata");
311 char hash[SC_MD5_HEX_LEN + 1];
312 if (!SCMd5FinalizeToHex(*hasher, hash,
sizeof(hash))) {
314 return luaL_error(L,
"md5 hashing failed");
317 lua_pushstring(L, (
const char *)hash);
326 static int LuaHashLibMd5Digest(
lua_State *L)
329 const char *input = luaL_checklstring(L, 1, &buf_len);
332 if (!SCMd5HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
333 return luaL_error(L,
"md5 hashing failed");
336 lua_pushlstring(L, (
const char *)output,
sizeof(output));
340 static int LuaHashLibMd5HexDigest(
lua_State *L)
343 const char *input = luaL_checklstring(L, 1, &buf_len);
345 char output[SC_MD5_HEX_LEN + 1];
346 if (!SCMd5HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
347 return luaL_error(L,
"md5 hashing failed");
350 lua_pushstring(L, (
const char *)output);
356 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
357 if (hasher && *hasher) {
363 static const struct luaL_Reg hashlib[] = {
365 {
"sha256_digest", LuaHashLibSha256Digest },
366 {
"sha256_hexdigest", LuaHashLibSha256HexDigest },
367 {
"sha256", LuaHashLibSha256New },
368 {
"sha1_digest", LuaHashLibSha1Digest },
369 {
"sha1_hexdigest", LuaHashLibSha1HexDigest },
370 {
"sha1", LuaHashLibSha1New },
371 {
"md5_digest", LuaHashLibMd5Digest },
372 {
"md5_hexdigest", LuaHashLibMd5HexDigest },
373 {
"md5", LuaHashLibMd5New },
378 static const struct luaL_Reg sha256_meta[] = {
380 {
"update", LuaHashLibSha256Update },
381 {
"finalize", LuaHashLibSha256Finalize },
382 {
"finalize_to_hex", LuaHashLibSha256FinalizeToHex },
383 {
"__gc", LuaHashLibSha256Gc },
388 static const struct luaL_Reg sha1_meta[] = {
390 {
"update", LuaHashLibSha1Update },
391 {
"finalize", LuaHashLibSha1Finalize },
392 {
"finalize_to_hex", LuaHashLibSha1FinalizeToHex },
393 {
"__gc", LuaHashLibSha1Gc },
398 static const struct luaL_Reg md5_meta[] = {
400 {
"update", LuaHashLibMd5Update },
401 {
"finalize", LuaHashLibMd5Finalize },
402 {
"finalize_to_hex", LuaHashLibMd5FinalizeToHex },
403 {
"__gc", LuaHashLibMd5Gc },
411 lua_pushvalue(L, -1);
412 lua_setfield(L, -2,
"__index");
413 luaL_setfuncs(L, sha256_meta, 0);
416 lua_pushvalue(L, -1);
417 lua_setfield(L, -2,
"__index");
418 luaL_setfuncs(L, sha1_meta, 0);
420 luaL_newmetatable(L,
MD5_MT);
421 lua_pushvalue(L, -1);
422 lua_setfield(L, -2,
"__index");
423 luaL_setfuncs(L, md5_meta, 0);
425 luaL_newlib(L, hashlib);