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)
72 if (hasher == NULL || *hasher == NULL) {
73 return luaL_error(L,
"invalid sha256 hash context");
75 if (*hasher == NULL) {
76 return luaL_error(L,
"sha256 hasher already finalized");
79 const char *data = luaL_checklstring(L, 2, &data_len);
80 SCSha256Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
84 static int LuaHashLibSha256Finalize(
lua_State *L)
87 if (hasher == NULL || *hasher == NULL) {
88 return luaL_error(L,
"invalid sha256 hash context");
90 if (*hasher == NULL) {
91 return luaL_error(L,
"sha256 hasher already finalized");
97 SCSha256Finalize(
ctx, hash,
sizeof(hash));
98 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
103 static int LuaHashLibSha256FinalizeToHex(
lua_State *L)
106 if (hasher == NULL || *hasher == NULL) {
107 return luaL_error(L,
"invalid sha256 hash context");
109 if (*hasher == NULL) {
110 return luaL_error(L,
"sha256 hasher already finalized");
113 char hash[SC_SHA256_HEX_LEN + 1];
116 if (!SCSha256FinalizeToHex(
ctx, hash,
sizeof(hash))) {
117 return luaL_error(L,
"sha256 hashing failed");
120 lua_pushstring(L, (
const char *)hash);
125 static int LuaHashLibSha256Digest(
lua_State *L)
128 const char *input = luaL_checklstring(L, 1, &buf_len);
131 if (!SCSha256HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
SC_SHA256_LEN)) {
132 return luaL_error(L,
"sha256 hashing failed");
140 static int LuaHashLibSha256HexDigest(
lua_State *L)
143 const char *input = luaL_checklstring(L, 1, &buf_len);
145 char output[SC_SHA256_HEX_LEN + 1];
146 if (!SCSha256HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
147 return luaL_error(L,
"sha256 hashing failed");
150 lua_pushstring(L, (
const char *)output);
154 static int LuaHashLibSha256Gc(
lua_State *L)
157 if (hasher && *hasher) {
165 static int LuaHashLibSha1New(
lua_State *L)
167 struct SCSha1 **hasher = lua_newuserdata(L,
sizeof(
struct SCSha1 *));
168 if (hasher == NULL) {
169 return luaL_error(L,
"failed to allocate userdata for sha1");
171 *hasher = SCSha1New();
173 lua_setmetatable(L, -2);
177 static int LuaHashLibSha1Update(
lua_State *L)
180 if (hasher == NULL || *hasher == NULL) {
181 return luaL_error(L,
"invalid sha1 hash context");
183 if (*hasher == NULL) {
184 return luaL_error(L,
"sha1 hasher already finalized");
188 const char *data = luaL_checklstring(L, 2, &data_len);
189 SCSha1Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
193 static int LuaHashLibSha1Finalize(
lua_State *L)
196 if (hasher == NULL || *hasher == NULL) {
197 return luaL_error(L,
"invalid sha1 hash context");
199 if (*hasher == NULL) {
200 return luaL_error(L,
"sha1 hasher already finalized");
206 SCSha1Finalize(
ctx, hash,
sizeof(hash));
207 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
212 static int LuaHashLibSha1FinalizeToHex(
lua_State *L)
215 if (hasher == NULL || *hasher == NULL) {
216 return luaL_error(L,
"invalid sha1 hash context");
218 if (*hasher == NULL) {
219 return luaL_error(L,
"sha1 hasher already finalized");
222 char hash[SC_SHA1_HEX_LEN + 1];
225 if (!SCSha1FinalizeToHex(
ctx, hash,
sizeof(hash))) {
226 return luaL_error(L,
"sha1 hashing failed");
229 lua_pushstring(L, (
const char *)hash);
234 static int LuaHashLibSha1Digest(
lua_State *L)
237 const char *input = luaL_checklstring(L, 1, &buf_len);
240 if (!SCSha1HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
241 return luaL_error(L,
"sha1 hashing failed");
244 lua_pushlstring(L, (
const char *)output,
sizeof(output));
248 static int LuaHashLibSha1HexDigest(
lua_State *L)
251 const char *input = luaL_checklstring(L, 1, &buf_len);
253 char output[SC_SHA1_HEX_LEN + 1];
254 if (!SCSha1HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
255 return luaL_error(L,
"sha1 hashing failed");
258 lua_pushstring(L, (
const char *)output);
262 static int LuaHashLibSha1Gc(
lua_State *L)
265 if (hasher && *hasher) {
273 static int LuaHashLibMd5New(
lua_State *L)
275 struct SCMd5 **hasher = lua_newuserdata(L,
sizeof(
struct SCMd5 *));
276 if (hasher == NULL) {
277 return luaL_error(L,
"failed to allocate userdata for sha1");
279 *hasher = SCMd5New();
280 luaL_getmetatable(L,
MD5_MT);
281 lua_setmetatable(L, -2);
285 static int LuaHashLibMd5Update(
lua_State *L)
287 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
288 if (hasher == NULL || *hasher == NULL) {
289 return luaL_error(L,
"invalid md5 hash context");
291 if (*hasher == NULL) {
292 return luaL_error(L,
"md5 hasher already finalized");
296 const char *data = luaL_checklstring(L, 2, &data_len);
297 SCMd5Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
301 static int LuaHashLibMd5Finalize(
lua_State *L)
303 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
304 if (hasher == NULL || *hasher == NULL) {
305 return luaL_error(L,
"invalid md5 hash context");
307 if (*hasher == NULL) {
308 return luaL_error(L,
"md5 hasher already finalized");
314 SCMd5Finalize(
ctx, hash,
sizeof(hash));
315 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
320 static int LuaHashLibMd5FinalizeToHex(
lua_State *L)
322 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
323 if (hasher == NULL || *hasher == NULL) {
324 return luaL_error(L,
"invalid md5 hash context");
326 if (*hasher == NULL) {
327 return luaL_error(L,
"md5 hasher already finalized");
330 char hash[SC_MD5_HEX_LEN + 1];
333 if (!SCMd5FinalizeToHex(
ctx, hash,
sizeof(hash))) {
334 return luaL_error(L,
"md5 hashing failed");
337 lua_pushstring(L, (
const char *)hash);
342 static int LuaHashLibMd5Digest(
lua_State *L)
345 const char *input = luaL_checklstring(L, 1, &buf_len);
348 if (!SCMd5HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
349 return luaL_error(L,
"md5 hashing failed");
352 lua_pushlstring(L, (
const char *)output,
sizeof(output));
356 static int LuaHashLibMd5HexDigest(
lua_State *L)
359 const char *input = luaL_checklstring(L, 1, &buf_len);
361 char output[SC_MD5_HEX_LEN + 1];
362 if (!SCMd5HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
363 return luaL_error(L,
"md5 hashing failed");
366 lua_pushstring(L, (
const char *)output);
372 struct SCMd5 **hasher = luaL_checkudata(L, 1,
MD5_MT);
373 if (hasher && *hasher) {
381 static const struct luaL_Reg hashlib[] = {
383 {
"sha256_digest", LuaHashLibSha256Digest },
384 {
"sha256_hexdigest", LuaHashLibSha256HexDigest },
385 {
"sha256", LuaHashLibSha256New },
386 {
"sha1_digest", LuaHashLibSha1Digest },
387 {
"sha1_hexdigest", LuaHashLibSha1HexDigest },
388 {
"sha1", LuaHashLibSha1New },
389 {
"md5_digest", LuaHashLibMd5Digest },
390 {
"md5_hexdigest", LuaHashLibMd5HexDigest },
391 {
"md5", LuaHashLibMd5New },
396 static const struct luaL_Reg sha256_methods[] = {
398 {
"update", LuaHashLibSha256Update },
399 {
"finalize", LuaHashLibSha256Finalize },
400 {
"finalize_to_hex", LuaHashLibSha256FinalizeToHex },
405 static const struct luaL_Reg sha1_methods[] = {
407 {
"update", LuaHashLibSha1Update },
408 {
"finalize", LuaHashLibSha1Finalize },
409 {
"finalize_to_hex", LuaHashLibSha1FinalizeToHex },
414 static const struct luaL_Reg md5_methods[] = {
416 {
"update", LuaHashLibMd5Update },
417 {
"finalize", LuaHashLibMd5Finalize },
418 {
"finalize_to_hex", LuaHashLibMd5FinalizeToHex },
426 lua_pushcfunction(L, LuaHashLibSha256Gc);
427 lua_setfield(L, -2,
"__gc");
428 luaL_newlib(L, sha256_methods);
429 lua_setfield(L, -2,
"__index");
432 lua_pushcfunction(L, LuaHashLibSha1Gc);
433 lua_setfield(L, -2,
"__gc");
434 luaL_newlib(L, sha1_methods);
435 lua_setfield(L, -2,
"__index");
437 luaL_newmetatable(L,
MD5_MT);
438 lua_pushcfunction(L, LuaHashLibMd5Gc);
439 lua_setfield(L, -2,
"__gc");
440 luaL_newlib(L, md5_methods);
441 lua_setfield(L, -2,
"__index");
443 luaL_newlib(L, hashlib);