luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

crypto_init.c (2303B)


      1 /*
      2  * crypto_init.c - Initialize crypto library and register Lua bindings
      3  */
      4 
      5 #include <lua.h>
      6 #include <lauxlib.h>
      7 #include <lualib.h>
      8 
      9 /* Include crypto Lua binding headers (only those we compiled) */
     10 #include "crypto/CSPRNG_Lua.h"
     11 #include "crypto/Ed25519_Lua.h"
     12 #include "crypto/X25519_Lua.h"
     13 #include "crypto/Hash_Lua.h"
     14 
     15 /*
     16  * Register all crypto functions in the 'crypto' table
     17  */
     18 int luaopen_crypto(lua_State *L) {
     19     /* Create crypto table */
     20     lua_newtable(L);
     21 
     22     /* Register CSPRNG functions */
     23     lua_pushcfunction(L, l_csprng_new_key);
     24     lua_setfield(L, -2, "new_key");
     25 
     26     lua_pushcfunction(L, l_csprng_random_bytes);
     27     lua_setfield(L, -2, "random_bytes");
     28 
     29     lua_pushcfunction(L, l_csprng_new_instance);
     30     lua_setfield(L, -2, "new_instance");
     31 
     32     /* Register CSPRNG metatable */
     33     register_csprng_metatable(L);
     34 
     35     /* Register Ed25519 functions */
     36     lua_pushcfunction(L, l_ed25519_keypair);
     37     lua_setfield(L, -2, "ed25519_keypair");
     38 
     39     lua_pushcfunction(L, l_ed25519_sign);
     40     lua_setfield(L, -2, "ed25519_sign");
     41 
     42     lua_pushcfunction(L, l_ed25519_verify);
     43     lua_setfield(L, -2, "ed25519_verify");
     44 
     45     /* Register X25519 functions */
     46     lua_pushcfunction(L, l_x25519_keypair);
     47     lua_setfield(L, -2, "x25519_keypair");
     48 
     49     lua_pushcfunction(L, l_x25519_public_key);
     50     lua_setfield(L, -2, "x25519_public_key");
     51 
     52     lua_pushcfunction(L, l_x25519_shared_secret);
     53     lua_setfield(L, -2, "x25519_shared_secret");
     54 
     55     /* Register hash functions directly on crypto table */
     56     lua_pushcfunction(L, l_hash_md5);
     57     lua_setfield(L, -2, "MD5");
     58 
     59     lua_pushcfunction(L, l_hash_sha1);
     60     lua_setfield(L, -2, "SHA1");
     61 
     62     lua_pushcfunction(L, l_hash);
     63     lua_setfield(L, -2, "SHA256");
     64 
     65     lua_pushcfunction(L, l_hash_sha512);
     66     lua_setfield(L, -2, "SHA512");
     67 
     68     lua_pushcfunction(L, l_hash_sha3);
     69     lua_setfield(L, -2, "SHA3-256");
     70 
     71     lua_pushcfunction(L, l_hash_sha3_512);
     72     lua_setfield(L, -2, "SHA3-512");
     73 
     74     lua_pushcfunction(L, l_hash_blake2b);
     75     lua_setfield(L, -2, "BLAKE2b-256");
     76 
     77     lua_pushcfunction(L, l_hash_blake2b_512);
     78     lua_setfield(L, -2, "BLAKE2b-512");
     79 
     80     lua_pushcfunction(L, l_hash_crc32);
     81     lua_setfield(L, -2, "CRC32");
     82 
     83     /* Set crypto table as global */
     84     lua_setglobal(L, "crypto");
     85 
     86     return 1;
     87 }