2015-05-26 06:06:49 +02:00
|
|
|
// Module for cryptography
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <errno.h>
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-05-26 06:06:49 +02:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2016-09-05 20:17:13 +02:00
|
|
|
#include "vfs.h"
|
2015-06-02 10:17:30 +02:00
|
|
|
#include "../crypto/digests.h"
|
2016-01-19 04:35:22 +01:00
|
|
|
#include "../crypto/mech.h"
|
2016-09-16 17:46:39 +02:00
|
|
|
#include "lmem.h"
|
2015-05-26 06:06:49 +02:00
|
|
|
|
|
|
|
#include "user_interface.h"
|
|
|
|
|
2015-05-28 22:10:12 +02:00
|
|
|
#include "rom.h"
|
|
|
|
|
2016-09-16 17:46:39 +02:00
|
|
|
typedef struct {
|
|
|
|
const digest_mech_info_t *mech_info;
|
|
|
|
void *ctx;
|
|
|
|
uint8_t *k_opad;
|
|
|
|
} digest_user_datum_t;
|
|
|
|
|
2015-05-26 06:06:49 +02:00
|
|
|
/**
|
|
|
|
* hash = crypto.sha1(input)
|
|
|
|
*
|
|
|
|
* Calculates raw SHA1 hash of input string.
|
|
|
|
* Input is arbitrary string, output is raw 20-byte hash as string.
|
|
|
|
*/
|
|
|
|
static int crypto_sha1( lua_State* L )
|
|
|
|
{
|
2015-05-28 22:10:12 +02:00
|
|
|
SHA1_CTX ctx;
|
2015-05-26 06:06:49 +02:00
|
|
|
uint8_t digest[20];
|
|
|
|
// Read the string from lua (with length)
|
|
|
|
int len;
|
|
|
|
const char* msg = luaL_checklstring(L, 1, &len);
|
|
|
|
// Use the SHA* functions in the rom
|
2015-05-28 22:10:12 +02:00
|
|
|
SHA1Init(&ctx);
|
|
|
|
SHA1Update(&ctx, msg, len);
|
|
|
|
SHA1Final(digest, &ctx);
|
2015-05-26 06:06:49 +02:00
|
|
|
|
|
|
|
// Push the result as a lua string
|
|
|
|
lua_pushlstring(L, digest, 20);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-28 04:54:08 +02:00
|
|
|
/**
|
|
|
|
* masked = crypto.mask(message, mask)
|
|
|
|
*
|
|
|
|
* Apply a mask (repeated if shorter than message) as XOR to each byte.
|
|
|
|
*/
|
|
|
|
static int crypto_mask( lua_State* L )
|
|
|
|
{
|
2019-04-05 19:18:00 +02:00
|
|
|
int len, mask_len, i;
|
2015-05-28 04:54:08 +02:00
|
|
|
const char* msg = luaL_checklstring(L, 1, &len);
|
|
|
|
const char* mask = luaL_checklstring(L, 2, &mask_len);
|
2019-04-05 19:18:00 +02:00
|
|
|
luaL_Buffer b;
|
2016-12-23 18:03:02 +01:00
|
|
|
|
|
|
|
if(mask_len <= 0)
|
|
|
|
return luaL_error(L, "invalid argument: mask");
|
|
|
|
|
2019-04-05 19:18:00 +02:00
|
|
|
luaL_buffinit(L, &b);
|
2015-05-28 04:54:08 +02:00
|
|
|
for (i = 0; i < len; i++) {
|
2019-04-05 19:18:00 +02:00
|
|
|
luaL_addchar(&b, msg[i] ^ mask[i % mask_len]);
|
2015-05-28 04:54:08 +02:00
|
|
|
}
|
2016-12-23 18:03:02 +01:00
|
|
|
|
2019-04-05 19:18:00 +02:00
|
|
|
luaL_pushresult(&b);
|
2015-05-28 04:54:08 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-02 10:17:30 +02:00
|
|
|
static inline int bad_mech (lua_State *L) { return luaL_error (L, "unknown hash mech"); }
|
|
|
|
static inline int bad_mem (lua_State *L) { return luaL_error (L, "insufficient memory"); }
|
2016-02-05 23:19:00 +01:00
|
|
|
static inline int bad_file (lua_State *L) { return luaL_error (L, "file does not exist"); }
|
2015-06-02 10:17:30 +02:00
|
|
|
|
|
|
|
/* rawdigest = crypto.hash("MD5", str)
|
2020-09-28 20:42:16 +02:00
|
|
|
* strdigest = encoder.toHex(rawdigest)
|
2015-06-02 10:17:30 +02:00
|
|
|
*/
|
|
|
|
static int crypto_lhash (lua_State *L)
|
|
|
|
{
|
|
|
|
const digest_mech_info_t *mi = crypto_digest_mech (luaL_checkstring (L, 1));
|
|
|
|
if (!mi)
|
|
|
|
return bad_mech (L);
|
|
|
|
size_t len = 0;
|
|
|
|
const char *data = luaL_checklstring (L, 2, &len);
|
|
|
|
|
|
|
|
uint8_t digest[mi->digest_size];
|
|
|
|
if (crypto_hash (mi, data, len, digest) != 0)
|
|
|
|
return bad_mem (L);
|
|
|
|
|
|
|
|
lua_pushlstring (L, digest, sizeof (digest));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-11 04:39:45 +01:00
|
|
|
/* General Usage for extensible hash functions:
|
|
|
|
* sha = crypto.new_hash("MD5")
|
|
|
|
* sha.update("Data")
|
|
|
|
* sha.update("Data2")
|
2020-09-28 20:42:16 +02:00
|
|
|
* strdigest = encoder.toHex(sha.finalize())
|
2016-02-11 04:39:45 +01:00
|
|
|
*/
|
|
|
|
|
2016-09-16 17:46:39 +02:00
|
|
|
#define WANT_HASH 0
|
|
|
|
#define WANT_HMAC 1
|
|
|
|
|
|
|
|
static int crypto_new_hash_hmac (lua_State *L, int what)
|
2016-02-11 04:39:45 +01:00
|
|
|
{
|
2019-04-08 14:57:46 +02:00
|
|
|
// get pointer to relevant hash_mechs table entry in app/crypto/digest.c. Note that
|
|
|
|
// the size of the table needed is dependent on the the digest type
|
2016-02-11 04:39:45 +01:00
|
|
|
const digest_mech_info_t *mi = crypto_digest_mech (luaL_checkstring (L, 1));
|
|
|
|
if (!mi)
|
|
|
|
return bad_mech (L);
|
|
|
|
|
2019-04-05 19:18:00 +02:00
|
|
|
size_t len = 0, k_opad_len = 0, udlen;
|
|
|
|
const char *key = NULL;
|
|
|
|
uint8_t *k_opad = NULL;
|
|
|
|
|
2016-09-16 17:46:39 +02:00
|
|
|
if (what == WANT_HMAC)
|
2019-04-08 14:57:46 +02:00
|
|
|
{ // The key and k_opad are only used for HMAC; these default to NULLs for HASH
|
2016-09-16 17:46:39 +02:00
|
|
|
key = luaL_checklstring (L, 2, &len);
|
2019-04-05 19:18:00 +02:00
|
|
|
k_opad_len = mi->block_size;
|
2016-09-16 17:46:39 +02:00
|
|
|
}
|
2016-02-13 21:11:59 +01:00
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
// create a userdatum with specific metatable. This comprises the ud header,
|
2019-04-08 14:57:46 +02:00
|
|
|
// the encrypto context block, and an optional HMAC block as a single allocation
|
|
|
|
// unit
|
2019-04-05 19:18:00 +02:00
|
|
|
udlen = sizeof(digest_user_datum_t) + mi->ctx_size + k_opad_len;
|
|
|
|
digest_user_datum_t *dudat = (digest_user_datum_t *)lua_newuserdata(L, udlen);
|
2019-04-08 14:57:46 +02:00
|
|
|
luaL_getmetatable(L, "crypto.hash"); // and set its metatable to the crypto.hash table
|
2016-02-11 04:39:45 +01:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
2019-04-08 14:57:46 +02:00
|
|
|
void *ctx = dudat + 1; // The context block immediately follows the digest_user_datum
|
|
|
|
mi->create (ctx);
|
2020-04-27 02:13:38 +02:00
|
|
|
|
2019-04-05 19:18:00 +02:00
|
|
|
if (what == WANT_HMAC) {
|
2019-04-08 14:57:46 +02:00
|
|
|
// The k_opad block immediately follows the context block
|
2020-04-27 02:13:38 +02:00
|
|
|
k_opad = (char *)ctx + mi->ctx_size;
|
2019-04-05 19:18:00 +02:00
|
|
|
crypto_hmac_begin (ctx, mi, key, len, k_opad);
|
|
|
|
}
|
|
|
|
|
2016-02-11 04:39:45 +01:00
|
|
|
// Set pointers to the mechanics and CTX
|
|
|
|
dudat->mech_info = mi;
|
|
|
|
dudat->ctx = ctx;
|
2016-09-16 17:46:39 +02:00
|
|
|
dudat->k_opad = k_opad;
|
2016-02-11 04:39:45 +01:00
|
|
|
|
|
|
|
return 1; // Pass userdata object back
|
|
|
|
}
|
|
|
|
|
2016-09-16 17:46:39 +02:00
|
|
|
/* crypto.new_hash("MECHTYPE") */
|
|
|
|
static int crypto_new_hash (lua_State *L)
|
|
|
|
{
|
|
|
|
return crypto_new_hash_hmac (L, WANT_HASH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* crypto.new_hmac("MECHTYPE", "KEY") */
|
|
|
|
static int crypto_new_hmac (lua_State *L)
|
|
|
|
{
|
|
|
|
return crypto_new_hash_hmac (L, WANT_HMAC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-11 04:39:45 +01:00
|
|
|
/* Called as object, params:
|
|
|
|
1 - userdata "this"
|
|
|
|
2 - new string to add to the hash state */
|
|
|
|
static int crypto_hash_update (lua_State *L)
|
|
|
|
{
|
|
|
|
NODE_DBG("enter crypto_hash_update.\n");
|
|
|
|
digest_user_datum_t *dudat;
|
|
|
|
size_t sl;
|
|
|
|
|
|
|
|
dudat = (digest_user_datum_t *)luaL_checkudata(L, 1, "crypto.hash");
|
|
|
|
|
|
|
|
const digest_mech_info_t *mi = dudat->mech_info;
|
|
|
|
|
|
|
|
size_t len = 0;
|
|
|
|
const char *data = luaL_checklstring (L, 2, &len);
|
|
|
|
|
|
|
|
mi->update (dudat->ctx, data, len);
|
|
|
|
|
|
|
|
return 0; // No return value
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called as object, no params. Returns digest of default size. */
|
|
|
|
static int crypto_hash_finalize (lua_State *L)
|
|
|
|
{
|
|
|
|
NODE_DBG("enter crypto_hash_update.\n");
|
|
|
|
digest_user_datum_t *dudat;
|
|
|
|
size_t sl;
|
|
|
|
|
|
|
|
dudat = (digest_user_datum_t *)luaL_checkudata(L, 1, "crypto.hash");
|
|
|
|
|
|
|
|
const digest_mech_info_t *mi = dudat->mech_info;
|
|
|
|
|
|
|
|
uint8_t digest[mi->digest_size]; // Allocate as local
|
2016-09-16 17:46:39 +02:00
|
|
|
if (dudat->k_opad)
|
|
|
|
crypto_hmac_finalize (dudat->ctx, mi, dudat->k_opad, digest);
|
|
|
|
else
|
|
|
|
mi->finalize (digest, dudat->ctx);
|
2016-02-11 04:39:45 +01:00
|
|
|
|
|
|
|
lua_pushlstring (L, digest, sizeof (digest));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 20:17:13 +02:00
|
|
|
static sint32_t vfs_read_wrap (int fd, void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
return vfs_read (fd, ptr, len);
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:19:00 +01:00
|
|
|
/* rawdigest = crypto.hash("MD5", filename)
|
2020-09-28 20:42:16 +02:00
|
|
|
* strdigest = encoder.toHex(rawdigest)
|
2016-02-05 23:19:00 +01:00
|
|
|
*/
|
|
|
|
static int crypto_flhash (lua_State *L)
|
|
|
|
{
|
|
|
|
const digest_mech_info_t *mi = crypto_digest_mech (luaL_checkstring (L, 1));
|
|
|
|
if (!mi)
|
|
|
|
return bad_mech (L);
|
|
|
|
const char *filename = luaL_checkstring (L, 2);
|
|
|
|
|
|
|
|
// Open the file
|
2016-09-05 20:17:13 +02:00
|
|
|
int file_fd = vfs_open (filename, "r");
|
|
|
|
if(!file_fd) {
|
2016-02-05 23:19:00 +01:00
|
|
|
return bad_file(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute hash
|
|
|
|
uint8_t digest[mi->digest_size];
|
2016-09-05 20:17:13 +02:00
|
|
|
int returncode = crypto_fhash (mi, &vfs_read_wrap, file_fd, digest);
|
2016-02-05 23:19:00 +01:00
|
|
|
|
|
|
|
// Finish up
|
2016-09-05 20:17:13 +02:00
|
|
|
vfs_close(file_fd);
|
2016-02-05 23:19:00 +01:00
|
|
|
|
|
|
|
if (returncode == ENOMEM)
|
|
|
|
return bad_mem (L);
|
|
|
|
else if (returncode == EINVAL)
|
|
|
|
return bad_mech(L);
|
|
|
|
else
|
|
|
|
lua_pushlstring (L, digest, sizeof (digest));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-02 10:17:30 +02:00
|
|
|
/* rawsignature = crypto.hmac("SHA1", str, key)
|
2020-09-28 20:42:16 +02:00
|
|
|
* strsignature = encoder.toHex(rawsignature)
|
2015-06-02 10:17:30 +02:00
|
|
|
*/
|
|
|
|
static int crypto_lhmac (lua_State *L)
|
|
|
|
{
|
|
|
|
const digest_mech_info_t *mi = crypto_digest_mech (luaL_checkstring (L, 1));
|
|
|
|
if (!mi)
|
|
|
|
return bad_mech (L);
|
|
|
|
size_t len = 0;
|
|
|
|
const char *data = luaL_checklstring (L, 2, &len);
|
|
|
|
size_t klen = 0;
|
|
|
|
const char *key = luaL_checklstring (L, 3, &klen);
|
|
|
|
|
|
|
|
uint8_t digest[mi->digest_size];
|
|
|
|
if (crypto_hmac (mi, data, len, key, klen, digest) != 0)
|
|
|
|
return bad_mem (L);
|
|
|
|
|
|
|
|
lua_pushlstring (L, digest, sizeof (digest));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-19 04:35:22 +01:00
|
|
|
|
|
|
|
static const crypto_mech_t *get_mech (lua_State *L, int idx)
|
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring (L, idx);
|
|
|
|
|
|
|
|
const crypto_mech_t *mech = crypto_encryption_mech (name);
|
|
|
|
if (mech)
|
|
|
|
return mech;
|
|
|
|
|
|
|
|
luaL_error (L, "unknown cipher: %s", name);
|
|
|
|
__builtin_unreachable ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int crypto_encdec (lua_State *L, bool enc)
|
|
|
|
{
|
|
|
|
const crypto_mech_t *mech = get_mech (L, 1);
|
2019-04-05 19:18:00 +02:00
|
|
|
size_t klen, dlen, ivlen, bs = mech->block_size;
|
|
|
|
|
2016-01-19 04:35:22 +01:00
|
|
|
const char *key = luaL_checklstring (L, 2, &klen);
|
|
|
|
const char *data = luaL_checklstring (L, 3, &dlen);
|
|
|
|
const char *iv = luaL_optlstring (L, 4, "", &ivlen);
|
|
|
|
|
|
|
|
size_t outlen = ((dlen + bs -1) / bs) * bs;
|
|
|
|
|
2019-04-05 19:18:00 +02:00
|
|
|
char *buf = luaM_newvector(L, outlen, char);
|
|
|
|
|
|
|
|
crypto_op_t op = {
|
2016-01-19 04:35:22 +01:00
|
|
|
key, klen,
|
|
|
|
iv, ivlen,
|
|
|
|
data, dlen,
|
|
|
|
buf, outlen,
|
|
|
|
enc ? OP_ENCRYPT : OP_DECRYPT
|
|
|
|
};
|
2019-04-05 19:18:00 +02:00
|
|
|
|
|
|
|
int status = mech->run (&op);
|
|
|
|
|
|
|
|
lua_pushlstring (L, buf, outlen); /* discarded on error but what the hell */
|
2020-04-27 02:13:38 +02:00
|
|
|
luaN_freearray(L, buf, outlen);
|
2019-04-05 19:18:00 +02:00
|
|
|
|
|
|
|
return status ? 1 : luaL_error (L, "crypto op failed");
|
|
|
|
|
2016-01-19 04:35:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lcrypto_encrypt (lua_State *L)
|
|
|
|
{
|
|
|
|
return crypto_encdec (L, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lcrypto_decrypt (lua_State *L)
|
|
|
|
{
|
|
|
|
return crypto_encdec (L, false);
|
|
|
|
}
|
|
|
|
|
2016-02-11 04:39:45 +01:00
|
|
|
// Hash function map
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
LROT_BEGIN(crypto_hash_map, NULL, LROT_MASK_INDEX)
|
|
|
|
LROT_TABENTRY( __index, crypto_hash_map )
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( update, crypto_hash_update )
|
|
|
|
LROT_FUNCENTRY( finalize, crypto_hash_finalize )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(crypto_hash_map, NULL, LROT_MASK_INDEX)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
2016-02-11 04:39:45 +01:00
|
|
|
|
2016-01-19 04:35:22 +01:00
|
|
|
|
2015-05-26 06:06:49 +02:00
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(crypto, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( sha1, crypto_sha1 )
|
|
|
|
LROT_FUNCENTRY( mask, crypto_mask )
|
|
|
|
LROT_FUNCENTRY( hash, crypto_lhash )
|
|
|
|
LROT_FUNCENTRY( fhash, crypto_flhash )
|
|
|
|
LROT_FUNCENTRY( new_hash, crypto_new_hash )
|
|
|
|
LROT_FUNCENTRY( hmac, crypto_lhmac )
|
|
|
|
LROT_FUNCENTRY( new_hmac, crypto_new_hmac )
|
|
|
|
LROT_FUNCENTRY( encrypt, lcrypto_encrypt )
|
|
|
|
LROT_FUNCENTRY( decrypt, lcrypto_decrypt )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(crypto, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
2015-05-26 06:06:49 +02:00
|
|
|
|
2016-02-11 04:39:45 +01:00
|
|
|
int luaopen_crypto ( lua_State *L )
|
|
|
|
{
|
2020-04-27 02:13:38 +02:00
|
|
|
luaL_rometatable(L, "crypto.hash", LROT_TABLEREF(crypto_hash_map));
|
2016-02-11 04:39:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
NODEMCU_MODULE(CRYPTO, "crypto", crypto, luaopen_crypto);
|