1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2013 by Alain Pannetrat <L1L1@gmx.com>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <lauxlib.h>
#include "lua_crypto.h"
#include "crypto.h"
#include "lua_bytes.h"
/***********************************************************
* BASIC CRYPTO FUNCTIONS
*/
static int subr_crypto_create_context(lua_State* L)
{
int alg = luaL_checkinteger(L,1);
bytestring_t* bs;
bytestring_t* res = bytestring_new(8);
crypto_error_t e;
if (lua_isnoneornil(L,2))
bs = NULL;
else
bs = luaL_check_bytestring(L,2);
if ((e=crypto_create_context(res,alg,bs))==CRYPTO_OK)
lua_push_bytestring(L,res);
else
{
bytestring_free(res);
return luaL_error(L,crypto_stringify_error(e));
}
return 1;
}
static int subr_crypto_encrypt(lua_State* L)
{
bytestring_t* ctx = luaL_check_bytestring(L,1);
bytestring_t* src = luaL_check_bytestring(L,2);
bytestring_t* iv;
bytestring_t* res = bytestring_new(8);
crypto_error_t e;
if (lua_isnoneornil(L,3))
iv = NULL;
else
iv = luaL_check_bytestring(L,3);
if ((e=crypto_encrypt(res,ctx,src,iv))==CRYPTO_OK)
lua_push_bytestring(L,res);
else
{
bytestring_free(res);
return luaL_error(L,crypto_stringify_error(e));
}
return 1;
}
static int subr_crypto_decrypt(lua_State* L)
{
bytestring_t* ctx = luaL_check_bytestring(L,1);
bytestring_t* src = luaL_check_bytestring(L,2);
bytestring_t* iv;
bytestring_t* res = bytestring_new(8);
crypto_error_t e;
if (lua_isnoneornil(L,3))
iv = NULL;
else
iv = luaL_check_bytestring(L,3);
if ((e=crypto_decrypt(res,ctx,src,iv))==CRYPTO_OK)
lua_push_bytestring(L,res);
else
{
bytestring_free(res);
return luaL_error(L,crypto_stringify_error(e));
}
return 1;
}
static int subr_crypto_mac(lua_State* L)
{
bytestring_t* ctx = luaL_check_bytestring(L,1);
bytestring_t* src = luaL_check_bytestring(L,2);
bytestring_t* res = bytestring_new(8);
crypto_error_t e;
if ((e=crypto_mac(res,ctx,src))==CRYPTO_OK)
lua_push_bytestring(L,res);
else
{
bytestring_free(res);
return luaL_error(L,crypto_stringify_error(e));
}
return 1;
}
static int subr_crypto_digest(lua_State* L)
{
bytestring_t* ctx = luaL_check_bytestring(L,1);
bytestring_t* src = luaL_check_bytestring(L,2);
bytestring_t* res = bytestring_new(8);
crypto_error_t e;
if ((e=crypto_digest(res,ctx,src))==CRYPTO_OK)
lua_push_bytestring(L,res);
else
{
bytestring_free(res);
return luaL_error(L,crypto_stringify_error(e));
}
return 1;
}
static const struct luaL_Reg cryptolib [] = {
{ "create_context", subr_crypto_create_context },
{ "encrypt", subr_crypto_encrypt },
{ "decrypt", subr_crypto_decrypt },
{ "mac", subr_crypto_mac },
{ "digest", subr_crypto_digest },
{ NULL, NULL} /* sentinel */
};
struct luaL_reg_integer {
const char* name;
int value;
};
static const struct luaL_reg_integer cryptoconstants [] = {
{ "ALG_DES_ECB", CRYPTO_ALG_DES_ECB },
{ "ALG_DES_CBC", CRYPTO_ALG_DES_CBC },
{ "ALG_DES2_EDE_ECB", CRYPTO_ALG_DES2_EDE_ECB },
{ "ALG_DES2_EDE_CBC", CRYPTO_ALG_DES2_EDE_CBC },
{ "ALG_ISO9797_M3", CRYPTO_ALG_ISO9797_M3 },
{ "ALG_SHA1", CRYPTO_ALG_SHA1 },
{ "PAD_ZERO", CRYPTO_PAD_ZERO },
{ "PAD_OPT_80_ZERO", CRYPTO_PAD_OPT_80_ZERO },
{ "PAD_ISO9797_P2", CRYPTO_PAD_ISO9797_P2 },
{ NULL, 0} /* sentinel */
};
int luaopen_crypto(lua_State* L)
{
int i;
luaL_newlib(L,cryptolib);
for (i=0;cryptoconstants[i].name;i++)
{
lua_pushstring (L, cryptoconstants[i].name);
lua_pushinteger(L, cryptoconstants[i].value);
lua_settable(L,-3);
}
lua_setglobal(L,"crypto");
return 1;
}
|