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     uint8_t output[output_len];
 
  128     if (!SCSha256HashBuffer((uint8_t *)input, (uint32_t)buf_len, output, output_len)) {
 
  129         return luaL_error(L, 
"sha256 hashing failed");
 
  132     lua_pushlstring(L, (
const char *)output, output_len);
 
  137 static int LuaHashLibSha256HexDigest(
lua_State *L)
 
  140     const char *input = luaL_checklstring(L, 1, &buf_len);
 
  142     char output[SC_SHA256_HEX_LEN + 1];
 
  143     if (!SCSha256HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output, 
sizeof(output))) {
 
  144         return luaL_error(L, 
"sha256 hashing failed");
 
  147     lua_pushstring(L, (
const char *)output);
 
  151 static int LuaHashLibSha256Gc(
lua_State *L)
 
  154     if (hasher && *hasher) {
 
  155         SCSha256Free(*hasher);
 
  160 static int LuaHashLibSha1New(
lua_State *L)
 
  162     struct SCSha1 **hasher = lua_newuserdata(L, 
sizeof(
struct SCSha1 *));
 
  163     if (hasher == NULL) {
 
  164         return luaL_error(L, 
"failed to allocate userdata for sha1");
 
  166     *hasher = SCSha1New();
 
  168     lua_setmetatable(L, -2);
 
  172 static int LuaHashLibSha1Update(
lua_State *L)
 
  175     if (hasher == NULL) {
 
  176         return luaL_error(L, 
"null userdata");
 
  180     const char *data = luaL_checklstring(L, 2, &data_len);
 
  181     SCSha1Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
 
  185 static int LuaHashLibSha1Finalize(
lua_State *L)
 
  188     if (hasher == NULL) {
 
  189         return luaL_error(L, 
"null userdata");
 
  193     SCSha1Finalize(*hasher, hash, 
sizeof(hash));
 
  194     lua_pushlstring(L, (
const char *)hash, 
sizeof(hash));
 
  203 static int LuaHashLibSha1FinalizeToHex(
lua_State *L)
 
  206     if (hasher == NULL) {
 
  207         return luaL_error(L, 
"null userdata");
 
  210     char hash[SC_SHA1_HEX_LEN + 1];
 
  211     if (!SCSha1FinalizeToHex(*hasher, hash, 
sizeof(hash))) {
 
  213         return luaL_error(L, 
"sha1 hashing failed");
 
  216     lua_pushstring(L, (
const char *)hash);
 
  225 static int LuaHashLibSha1Digest(
lua_State *L)
 
  228     const char *input = luaL_checklstring(L, 1, &buf_len);
 
  231     if (!SCSha1HashBuffer((uint8_t *)input, (uint32_t)buf_len, output, 
sizeof(output))) {
 
  232         return luaL_error(L, 
"sha1 hashing failed");
 
  235     lua_pushlstring(L, (
const char *)output, 
sizeof(output));
 
  239 static int LuaHashLibSha1HexDigest(
lua_State *L)
 
  242     const char *input = luaL_checklstring(L, 1, &buf_len);
 
  244     char output[SC_SHA1_HEX_LEN + 1];
 
  245     if (!SCSha1HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output, 
sizeof(output))) {
 
  246         return luaL_error(L, 
"sha1 hashing failed");
 
  249     lua_pushstring(L, (
const char *)output);
 
  253 static int LuaHashLibSha1Gc(
lua_State *L)
 
  256     if (hasher && *hasher) {
 
  262 static int LuaHashLibMd5New(
lua_State *L)
 
  264     struct SCMd5 **hasher = lua_newuserdata(L, 
sizeof(
struct SCMd5 *));
 
  265     if (hasher == NULL) {
 
  266         return luaL_error(L, 
"failed to allocate userdata for sha1");
 
  268     *hasher = SCMd5New();
 
  269     luaL_getmetatable(L, 
MD5_MT);
 
  270     lua_setmetatable(L, -2);
 
  274 static int LuaHashLibMd5Update(
lua_State *L)
 
  276     struct SCMd5 **hasher = luaL_checkudata(L, 1, 
MD5_MT);
 
  277     if (hasher == NULL) {
 
  278         return luaL_error(L, 
"null userdata");
 
  282     const char *data = luaL_checklstring(L, 2, &data_len);
 
  283     SCMd5Update(*hasher, (
const uint8_t *)data, (uint32_t)data_len);
 
  287 static int LuaHashLibMd5Finalize(
lua_State *L)
 
  289     struct SCMd5 **hasher = luaL_checkudata(L, 1, 
MD5_MT);
 
  290     if (hasher == NULL) {
 
  291         return luaL_error(L, 
"null userdata");
 
  295     SCMd5Finalize(*hasher, hash, 
sizeof(hash));
 
  296     lua_pushlstring(L, (
const char *)hash, 
sizeof(hash));
 
  305 static int LuaHashLibMd5FinalizeToHex(
lua_State *L)
 
  307     struct SCMd5 **hasher = luaL_checkudata(L, 1, 
MD5_MT);
 
  308     if (hasher == NULL) {
 
  309         return luaL_error(L, 
"null userdata");
 
  312     char hash[SC_MD5_HEX_LEN + 1];
 
  313     if (!SCMd5FinalizeToHex(*hasher, hash, 
sizeof(hash))) {
 
  315         return luaL_error(L, 
"md5 hashing failed");
 
  318     lua_pushstring(L, (
const char *)hash);
 
  327 static int LuaHashLibMd5Digest(
lua_State *L)
 
  330     const char *input = luaL_checklstring(L, 1, &buf_len);
 
  333     if (!SCMd5HashBuffer((uint8_t *)input, (uint32_t)buf_len, output, 
sizeof(output))) {
 
  334         return luaL_error(L, 
"md5 hashing failed");
 
  337     lua_pushlstring(L, (
const char *)output, 
sizeof(output));
 
  341 static int LuaHashLibMd5HexDigest(
lua_State *L)
 
  344     const char *input = luaL_checklstring(L, 1, &buf_len);
 
  346     char output[SC_MD5_HEX_LEN + 1];
 
  347     if (!SCMd5HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output, 
sizeof(output))) {
 
  348         return luaL_error(L, 
"md5 hashing failed");
 
  351     lua_pushstring(L, (
const char *)output);
 
  357     struct SCMd5 **hasher = luaL_checkudata(L, 1, 
MD5_MT);
 
  358     if (hasher && *hasher) {
 
  364 static const struct luaL_Reg hashlib[] = {
 
  366     { 
"sha256_digest", LuaHashLibSha256Digest },
 
  367     { 
"sha256_hexdigest", LuaHashLibSha256HexDigest },
 
  368     { 
"sha256", LuaHashLibSha256New },
 
  369     { 
"sha1_digest", LuaHashLibSha1Digest },
 
  370     { 
"sha1_hexdigest", LuaHashLibSha1HexDigest },
 
  371     { 
"sha1", LuaHashLibSha1New },
 
  372     { 
"md5_digest", LuaHashLibMd5Digest },
 
  373     { 
"md5_hexdigest", LuaHashLibMd5HexDigest },
 
  374     { 
"md5", LuaHashLibMd5New },
 
  379 static const struct luaL_Reg sha256_meta[] = {
 
  381     { 
"update", LuaHashLibSha256Update },
 
  382     { 
"finalize", LuaHashLibSha256Finalize },
 
  383     { 
"finalize_to_hex", LuaHashLibSha256FinalizeToHex },
 
  384     { 
"__gc", LuaHashLibSha256Gc },
 
  389 static const struct luaL_Reg sha1_meta[] = {
 
  391     { 
"update", LuaHashLibSha1Update },
 
  392     { 
"finalize", LuaHashLibSha1Finalize },
 
  393     { 
"finalize_to_hex", LuaHashLibSha1FinalizeToHex },
 
  394     { 
"__gc", LuaHashLibSha1Gc },
 
  399 static const struct luaL_Reg md5_meta[] = {
 
  401     { 
"update", LuaHashLibMd5Update },
 
  402     { 
"finalize", LuaHashLibMd5Finalize },
 
  403     { 
"finalize_to_hex", LuaHashLibMd5FinalizeToHex },
 
  404     { 
"__gc", LuaHashLibMd5Gc },
 
  412     lua_pushvalue(L, -1);
 
  413     lua_setfield(L, -2, 
"__index");
 
  414     luaL_setfuncs(L, sha256_meta, 0);
 
  417     lua_pushvalue(L, -1);
 
  418     lua_setfield(L, -2, 
"__index");
 
  419     luaL_setfuncs(L, sha1_meta, 0);
 
  421     luaL_newmetatable(L, 
MD5_MT);
 
  422     lua_pushvalue(L, -1);
 
  423     lua_setfield(L, -2, 
"__index");
 
  424     luaL_setfuncs(L, md5_meta, 0);
 
  426     luaL_newlib(L, hashlib);