47 #include "rust-bindings.h"
50 #define SHA256_MT "suricata:hashlib:sha256"
54 static int CheckHashingEnabled(
lua_State *L)
57 return luaL_error(L,
"hashing is disabled");
65 if (
len > (
size_t)UINT32_MAX) {
66 return luaL_error(L,
"input too long: %I bytes", (lua_Integer)
len);
70 #define SHA1_MT "suricata:hashlib:sha1"
71 #define MD5_MT "suricata:hashlib:md5"
76 static int LuaHashLibSha256New(
lua_State *L)
78 if (CheckHashingEnabled(L)) {
83 return luaL_error(L,
"failed to allocate userdata for sha256");
85 *hasher = SCSha256New();
87 lua_setmetatable(L, -2);
94 static int LuaHashLibSha256Update(
lua_State *L)
97 if (hasher == NULL || *hasher == NULL) {
98 return luaL_error(L,
"invalid sha256 hash context");
100 if (*hasher == NULL) {
101 return luaL_error(L,
"sha256 hasher already finalized");
104 const char *data = luaL_checklstring(L, 2, &data_len);
105 if (CheckInputLength(L, data_len)) {
108 SCSha256Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
112 static int LuaHashLibSha256Finalize(
lua_State *L)
115 if (hasher == NULL || *hasher == NULL) {
116 return luaL_error(L,
"invalid sha256 hash context");
118 if (*hasher == NULL) {
119 return luaL_error(L,
"sha256 hasher already finalized");
125 SCSha256Finalize(
ctx, hash,
sizeof(hash));
126 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
131 static int LuaHashLibSha256FinalizeToHex(
lua_State *L)
134 if (hasher == NULL || *hasher == NULL) {
135 return luaL_error(L,
"invalid sha256 hash context");
137 if (*hasher == NULL) {
138 return luaL_error(L,
"sha256 hasher already finalized");
141 char hash[SC_SHA256_HEX_LEN + 1];
144 if (!SCSha256FinalizeToHex(
ctx, hash,
sizeof(hash))) {
145 return luaL_error(L,
"sha256 hashing failed");
148 lua_pushstring(L, (
const char *)hash);
153 static int LuaHashLibSha256Digest(
lua_State *L)
156 const char *input = luaL_checklstring(L, 1, &buf_len);
158 if (CheckHashingEnabled(L)) {
161 if (CheckInputLength(L, buf_len)) {
166 if (!SCSha256HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
SC_SHA256_LEN)) {
167 return luaL_error(L,
"sha256 hashing failed");
175 static int LuaHashLibSha256HexDigest(
lua_State *L)
178 const char *input = luaL_checklstring(L, 1, &buf_len);
180 if (CheckHashingEnabled(L)) {
183 if (CheckInputLength(L, buf_len)) {
187 char output[SC_SHA256_HEX_LEN + 1];
188 if (!SCSha256HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
189 return luaL_error(L,
"sha256 hashing failed");
192 lua_pushstring(L, (
const char *)output);
202 if (lua_type(L, 1) != LUA_TUSERDATA) {
209 uint8_t *payload = (uint8_t *)h +
sizeof(*h);
213 if (*hasher != NULL) {
214 SCSha256Free(*hasher);
221 if (*hasher != NULL) {
228 struct SCMd5 **hasher = (
struct SCMd5 **)payload;
229 if (*hasher != NULL) {
241 static int LuaHashLibSha1New(
lua_State *L)
243 if (CheckHashingEnabled(L)) {
247 if (hasher == NULL) {
248 return luaL_error(L,
"failed to allocate userdata for sha1");
250 *hasher = SCSha1New();
252 lua_setmetatable(L, -2);
256 static int LuaHashLibSha1Update(
lua_State *L)
259 if (hasher == NULL || *hasher == NULL) {
260 return luaL_error(L,
"invalid sha1 hash context");
262 if (*hasher == NULL) {
263 return luaL_error(L,
"sha1 hasher already finalized");
267 const char *data = luaL_checklstring(L, 2, &data_len);
268 if (CheckInputLength(L, data_len)) {
271 SCSha1Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
275 static int LuaHashLibSha1Finalize(
lua_State *L)
278 if (hasher == NULL || *hasher == NULL) {
279 return luaL_error(L,
"invalid sha1 hash context");
281 if (*hasher == NULL) {
282 return luaL_error(L,
"sha1 hasher already finalized");
288 SCSha1Finalize(
ctx, hash,
sizeof(hash));
289 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
294 static int LuaHashLibSha1FinalizeToHex(
lua_State *L)
297 if (hasher == NULL || *hasher == NULL) {
298 return luaL_error(L,
"invalid sha1 hash context");
300 if (*hasher == NULL) {
301 return luaL_error(L,
"sha1 hasher already finalized");
304 char hash[SC_SHA1_HEX_LEN + 1];
307 if (!SCSha1FinalizeToHex(
ctx, hash,
sizeof(hash))) {
308 return luaL_error(L,
"sha1 hashing failed");
311 lua_pushstring(L, (
const char *)hash);
316 static int LuaHashLibSha1Digest(
lua_State *L)
319 const char *input = luaL_checklstring(L, 1, &buf_len);
321 if (CheckHashingEnabled(L)) {
324 if (CheckInputLength(L, buf_len)) {
329 if (!SCSha1HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
330 return luaL_error(L,
"sha1 hashing failed");
333 lua_pushlstring(L, (
const char *)output,
sizeof(output));
337 static int LuaHashLibSha1HexDigest(
lua_State *L)
340 const char *input = luaL_checklstring(L, 1, &buf_len);
342 if (CheckHashingEnabled(L)) {
345 if (CheckInputLength(L, buf_len)) {
349 char output[SC_SHA1_HEX_LEN + 1];
350 if (!SCSha1HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
351 return luaL_error(L,
"sha1 hashing failed");
354 lua_pushstring(L, (
const char *)output);
358 static int LuaHashLibMd5New(
lua_State *L)
360 if (CheckHashingEnabled(L)) {
364 if (hasher == NULL) {
365 return luaL_error(L,
"failed to allocate userdata for sha1");
367 *hasher = SCMd5New();
368 luaL_getmetatable(L,
MD5_MT);
369 lua_setmetatable(L, -2);
373 static int LuaHashLibMd5Update(
lua_State *L)
376 if (hasher == NULL || *hasher == NULL) {
377 return luaL_error(L,
"invalid md5 hash context");
379 if (*hasher == NULL) {
380 return luaL_error(L,
"md5 hasher already finalized");
384 const char *data = luaL_checklstring(L, 2, &data_len);
385 if (CheckInputLength(L, data_len)) {
388 SCMd5Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
392 static int LuaHashLibMd5Finalize(
lua_State *L)
395 if (hasher == NULL || *hasher == NULL) {
396 return luaL_error(L,
"invalid md5 hash context");
398 if (*hasher == NULL) {
399 return luaL_error(L,
"md5 hasher already finalized");
405 SCMd5Finalize(
ctx, hash,
sizeof(hash));
406 lua_pushlstring(L, (
const char *)hash,
sizeof(hash));
411 static int LuaHashLibMd5FinalizeToHex(
lua_State *L)
414 if (hasher == NULL || *hasher == NULL) {
415 return luaL_error(L,
"invalid md5 hash context");
417 if (*hasher == NULL) {
418 return luaL_error(L,
"md5 hasher already finalized");
421 char hash[SC_MD5_HEX_LEN + 1];
424 if (!SCMd5FinalizeToHex(
ctx, hash,
sizeof(hash))) {
425 return luaL_error(L,
"md5 hashing failed");
428 lua_pushstring(L, (
const char *)hash);
433 static int LuaHashLibMd5Digest(
lua_State *L)
436 const char *input = luaL_checklstring(L, 1, &buf_len);
438 if (CheckHashingEnabled(L)) {
441 if (CheckInputLength(L, buf_len)) {
446 if (!SCMd5HashBuffer((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
447 return luaL_error(L,
"md5 hashing failed");
450 lua_pushlstring(L, (
const char *)output,
sizeof(output));
454 static int LuaHashLibMd5HexDigest(
lua_State *L)
457 const char *input = luaL_checklstring(L, 1, &buf_len);
459 if (CheckHashingEnabled(L)) {
462 if (CheckInputLength(L, buf_len)) {
466 char output[SC_MD5_HEX_LEN + 1];
467 if (!SCMd5HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output,
sizeof(output))) {
468 return luaL_error(L,
"md5 hashing failed");
471 lua_pushstring(L, (
const char *)output);
475 static const struct luaL_Reg hashlib[] = {
477 {
"sha256_digest", LuaHashLibSha256Digest },
478 {
"sha256_hexdigest", LuaHashLibSha256HexDigest },
479 {
"sha256", LuaHashLibSha256New },
480 {
"sha1_digest", LuaHashLibSha1Digest },
481 {
"sha1_hexdigest", LuaHashLibSha1HexDigest },
482 {
"sha1", LuaHashLibSha1New },
483 {
"md5_digest", LuaHashLibMd5Digest },
484 {
"md5_hexdigest", LuaHashLibMd5HexDigest },
485 {
"md5", LuaHashLibMd5New },
490 static const struct luaL_Reg sha256_methods[] = {
492 {
"update", LuaHashLibSha256Update },
493 {
"finalize", LuaHashLibSha256Finalize },
494 {
"finalize_to_hex", LuaHashLibSha256FinalizeToHex },
499 static const struct luaL_Reg sha1_methods[] = {
501 {
"update", LuaHashLibSha1Update },
502 {
"finalize", LuaHashLibSha1Finalize },
503 {
"finalize_to_hex", LuaHashLibSha1FinalizeToHex },
508 static const struct luaL_Reg md5_methods[] = {
510 {
"update", LuaHashLibMd5Update },
511 {
"finalize", LuaHashLibMd5Finalize },
512 {
"finalize_to_hex", LuaHashLibMd5FinalizeToHex },
520 lua_pushcfunction(L, LuaHashLibGc);
521 lua_setfield(L, -2,
"__gc");
522 luaL_newlib(L, sha256_methods);
523 lua_setfield(L, -2,
"__index");
526 lua_pushcfunction(L, LuaHashLibGc);
527 lua_setfield(L, -2,
"__gc");
528 luaL_newlib(L, sha1_methods);
529 lua_setfield(L, -2,
"__index");
531 luaL_newmetatable(L,
MD5_MT);
532 lua_pushcfunction(L, LuaHashLibGc);
533 lua_setfield(L, -2,
"__gc");
534 luaL_newlib(L, md5_methods);
535 lua_setfield(L, -2,
"__index");
537 luaL_newlib(L, hashlib);