1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 22 Dec 2013 03:01:08 -0500
changeset 11 b52e0f1798b8
parent 7 682d7ea1e7f3
child 12 6a2d5b34d5ca
permissions -rw-r--r--
Start building in GUI stuff.
icculus@0
     1
#include <stdio.h>
icculus@0
     2
#include <stdlib.h>
icculus@0
     3
#include <string.h>
icculus@0
     4
#include <assert.h>
icculus@0
     5
#include "lua.h"
icculus@0
     6
#include "lauxlib.h"
icculus@0
     7
#include "lualib.h"
icculus@0
     8
#include "pkcs5_pbkdf2.h"
icculus@0
     9
#include "aes.h"
icculus@0
    10
#include "base64.h"
icculus@0
    11
#include "md5.h"
icculus@11
    12
#include <gtk/gtk.h>
icculus@0
    13
icculus@0
    14
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
icculus@0
    15
icculus@0
    16
static lua_State *luaState = NULL;
icculus@0
    17
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
icculus@0
    18
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
icculus@0
    19
icculus@0
    20
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
icculus@0
    21
{
icculus@0
    22
    if (str != NULL)
icculus@0
    23
        lua_pushlstring(L, (const char *) str, len);
icculus@0
    24
    else
icculus@0
    25
        lua_pushnil(L);
icculus@0
    26
    return 1;
icculus@0
    27
} // retvalStringBytes
icculus@0
    28
icculus@11
    29
static inline int retvalPointer(lua_State *L, void *ptr)
icculus@11
    30
{
icculus@11
    31
    if (ptr != NULL)
icculus@11
    32
        lua_pushlightuserdata(L, ptr);
icculus@11
    33
    else
icculus@11
    34
        lua_pushnil(L);
icculus@11
    35
    return 1;
icculus@11
    36
} // retvalPointer
icculus@11
    37
icculus@0
    38
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
icculus@0
    39
{
icculus@0
    40
    int i;
icculus@0
    41
    for (i = 0; i < 16; i++, dst++, src++)
icculus@0
    42
        *dst ^= *src;
icculus@0
    43
} // xorBlock
icculus@0
    44
icculus@0
    45
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv)
icculus@0
    46
{
icculus@0
    47
    const size_t blocks = *datalen / 16;
icculus@0
    48
    uint8_t *block = data + ((blocks-1) * 16);   // start at final block, work backwards
icculus@0
    49
    const uint8_t *padding = &block[15];
icculus@0
    50
    uint8_t expkey[aesExpandedKeySize];
icculus@0
    51
    size_t i;
icculus@0
    52
icculus@0
    53
    if (blocks == 0)
icculus@0
    54
        return 1;  // nothing to do.
icculus@0
    55
icculus@0
    56
	aesExpandKey(key, expkey);
icculus@0
    57
icculus@0
    58
    for (i = 0; i < blocks-1; i++)
icculus@0
    59
    {
icculus@0
    60
        aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
    61
        xorBlock(block, block-16);
icculus@0
    62
        block -= 16;
icculus@0
    63
    }
icculus@0
    64
    aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
    65
    xorBlock(block, iv);   // xor against initial vector for final block.
icculus@0
    66
icculus@0
    67
    if (*padding > 16)
icculus@0
    68
        return 0;  // bad data?
icculus@0
    69
icculus@0
    70
    *datalen -= *padding;
icculus@0
    71
icculus@0
    72
    return 1;
icculus@0
    73
} // decryptBinaryUsingKeyAndIvec
icculus@0
    74
icculus@0
    75
icculus@0
    76
static inline int isSalted(const uint8_t *data, const size_t datalen)
icculus@0
    77
{
icculus@0
    78
    return ( (datalen > sizeof (saltprefix)) &&
icculus@0
    79
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
icculus@0
    80
} // isSalted
icculus@0
    81
icculus@0
    82
icculus@0
    83
static int decryptUsingPBKDF2(lua_State *L)
icculus@0
    84
{
icculus@0
    85
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
    86
    const char *password = luaL_checkstring(L, 2);
icculus@0
    87
    const int iterations = luaL_checkinteger(L, 3);
icculus@0
    88
    size_t datalen = strlen(base64);
icculus@0
    89
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
    90
    uint8_t *data = dataptr;
icculus@0
    91
    base64_decodestate base64state;
icculus@0
    92
icculus@0
    93
    base64_init_decodestate(&base64state);
icculus@0
    94
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
    95
icculus@0
    96
    const uint8_t *salt = zero16;
icculus@0
    97
    int saltlen = sizeof (zero16);
icculus@0
    98
    if (isSalted(data, datalen))
icculus@0
    99
    {
icculus@0
   100
        salt = data + 8;
icculus@0
   101
        saltlen = 8;
icculus@0
   102
        data += 16;
icculus@0
   103
        datalen -= 16;
icculus@0
   104
    } // if
icculus@0
   105
icculus@0
   106
    uint8_t output[32];
icculus@0
   107
    pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations);
icculus@0
   108
icculus@0
   109
    const uint8_t *aeskey = &output[0];
icculus@0
   110
    const uint8_t *aesiv = &output[16];
icculus@0
   111
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   112
        retvalStringBytes(L, data, datalen);
icculus@0
   113
    else
icculus@0
   114
        lua_pushnil(L);
icculus@0
   115
icculus@0
   116
    free(dataptr);
icculus@0
   117
    return 1;
icculus@0
   118
} // decryptUsingPBKDF2
icculus@0
   119
icculus@0
   120
icculus@0
   121
static int decryptBase64UsingKey(lua_State *L)
icculus@0
   122
{
icculus@0
   123
    size_t keylen = 0;
icculus@0
   124
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
   125
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
icculus@0
   126
    size_t datalen = strlen(base64);
icculus@0
   127
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
   128
    uint8_t *data = dataptr;
icculus@0
   129
    base64_decodestate base64state;
icculus@0
   130
icculus@0
   131
    base64_init_decodestate(&base64state);
icculus@0
   132
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   133
icculus@0
   134
    uint8_t aeskey[16];
icculus@0
   135
    uint8_t aesiv[16];
icculus@0
   136
    MD5_CTX md5;
icculus@0
   137
icculus@0
   138
    if (isSalted(data, datalen))
icculus@0
   139
    {
icculus@0
   140
        const uint8_t *salt = data + 8;
icculus@0
   141
        const size_t saltlen = 8;
icculus@0
   142
        data += 16;
icculus@0
   143
        datalen -= 16;
icculus@0
   144
icculus@0
   145
        assert(aesNr == 10);  // AES-256 needs more rounds.
icculus@0
   146
        assert(aesNk == 4);   // hashing size is hardcoded later.
icculus@0
   147
        uint8_t hashing[32];
icculus@0
   148
icculus@0
   149
        MD5_init(&md5);
icculus@0
   150
        MD5_append(&md5, key, keylen);
icculus@0
   151
        MD5_append(&md5, salt, saltlen);
icculus@0
   152
        MD5_finish(&md5, hashing);
icculus@0
   153
icculus@0
   154
        MD5_init(&md5);
icculus@0
   155
        MD5_append(&md5, hashing, 16);
icculus@0
   156
        MD5_append(&md5, key, keylen);
icculus@0
   157
        MD5_append(&md5, salt, saltlen);
icculus@0
   158
        MD5_finish(&md5, &hashing[16]);
icculus@0
   159
icculus@0
   160
        memcpy(aeskey, hashing, 4 * aesNk);
icculus@0
   161
        memcpy(aesiv, &hashing[4 * aesNk], 16);
icculus@0
   162
    } // if
icculus@0
   163
    else
icculus@0
   164
    {
icculus@0
   165
        MD5_init(&md5);
icculus@0
   166
        MD5_append(&md5, key, keylen);
icculus@0
   167
        MD5_finish(&md5, aeskey);
icculus@0
   168
        memset(aesiv, '\0', sizeof (aesiv));
icculus@0
   169
    } // else
icculus@0
   170
icculus@0
   171
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   172
        retvalStringBytes(L, data, datalen);
icculus@0
   173
    else
icculus@0
   174
        lua_pushnil(L);
icculus@0
   175
icculus@0
   176
    free(dataptr);
icculus@0
   177
    return 1;
icculus@0
   178
} // decryptBase64UsingKey
icculus@0
   179
icculus@0
   180
icculus@11
   181
static int makeGuiMenu(lua_State *L)
icculus@11
   182
{
icculus@11
   183
    return retvalPointer(L, gtk_menu_new());
icculus@11
   184
} // makeGuiMenu
icculus@11
   185
icculus@11
   186
icculus@11
   187
static void clickedMenuItem(void *arg)
icculus@11
   188
{
icculus@11
   189
    // This is the callback from GTK+; now call into our actual Lua callback!
icculus@11
   190
    const int callback = (int) ((size_t)arg);
icculus@11
   191
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
icculus@11
   192
    lua_call(luaState, 0, 0);
icculus@11
   193
} // clickedMenuItem
icculus@11
   194
icculus@11
   195
icculus@11
   196
// !!! FIXME: on destruction of a menu item, we need to luaL_unref(L, LUA_REGISTRYINDEX, callback)...
icculus@11
   197
static int appendGuiMenuItem(lua_State *L)
icculus@11
   198
{
icculus@11
   199
    const int argc = lua_gettop(L);
icculus@11
   200
    GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
icculus@11
   201
    const char *label = luaL_checkstring(L, 2);
icculus@11
   202
    GtkWidget *item = gtk_menu_item_new_with_label(label);
icculus@11
   203
icculus@11
   204
    if ((argc >= 3) && (!lua_isnil(L, 3)))
icculus@11
   205
    {
icculus@11
   206
        assert(lua_isfunction(L, 3));
icculus@11
   207
        lua_pushvalue(L, 3);  // copy the Lua callback (luaL_ref() pops it).
icculus@11
   208
        const int callback = luaL_ref(L, LUA_REGISTRYINDEX);
icculus@11
   209
        gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback));
icculus@11
   210
    } // if
icculus@11
   211
icculus@11
   212
    gtk_widget_show(item);
icculus@11
   213
    gtk_menu_append(menu, item);
icculus@11
   214
    return retvalPointer(L, item);
icculus@11
   215
} // appendGuiMenuItem
icculus@11
   216
icculus@11
   217
icculus@11
   218
static int setGuiMenuItemSubmenu(lua_State *L)
icculus@11
   219
{
icculus@11
   220
    GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1);
icculus@11
   221
    GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2);
icculus@11
   222
    gtk_menu_item_set_submenu(item, submenu);
icculus@11
   223
    return 0;
icculus@11
   224
} // setGuiMenuItemSubmenu
icculus@11
   225
icculus@11
   226
icculus@11
   227
static int popupGuiMenu(lua_State *L)
icculus@11
   228
{
icculus@11
   229
    GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1);
icculus@11
   230
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
icculus@11
   231
    return 0;
icculus@11
   232
} // popupGuiMenu
icculus@11
   233
icculus@11
   234
icculus@11
   235
static int giveControlToGui(lua_State *L)
icculus@11
   236
{
icculus@11
   237
    gtk_main();
icculus@11
   238
    return 0;
icculus@11
   239
} // giveControlToGui
icculus@11
   240
icculus@7
   241
icculus@0
   242
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
icculus@0
   243
{
icculus@0
   244
    if (nsize == 0)
icculus@0
   245
    {
icculus@0
   246
        free(ptr);
icculus@0
   247
        return NULL;
icculus@0
   248
    } // if
icculus@0
   249
    return realloc(ptr, nsize);
icculus@0
   250
} // luaAlloc
icculus@0
   251
icculus@0
   252
icculus@0
   253
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym)
icculus@0
   254
{
icculus@0
   255
    lua_pushcfunction(L, f);
icculus@0
   256
    lua_setglobal(luaState, sym);
icculus@0
   257
} // luaSetCFunc
icculus@0
   258
icculus@0
   259
icculus@0
   260
static int luaFatal(lua_State *L)
icculus@0
   261
{
icculus@0
   262
    const char *errstr = lua_tostring(L, -1);
icculus@0
   263
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
icculus@0
   264
    fflush(stderr);
icculus@0
   265
    exit(1);
icculus@0
   266
} // luaFatal
icculus@0
   267
icculus@0
   268
icculus@11
   269
static void deinitLua(void)
icculus@11
   270
{
icculus@11
   271
    if (luaState != NULL)
icculus@11
   272
    {
icculus@11
   273
        lua_close(luaState);
icculus@11
   274
        luaState = NULL;
icculus@11
   275
    } // if
icculus@11
   276
} // deinitLua
icculus@11
   277
icculus@11
   278
icculus@7
   279
static int initLua(const int argc, char **argv)
icculus@0
   280
{
icculus@11
   281
    atexit(deinitLua);
icculus@11
   282
icculus@0
   283
    assert(luaState == NULL);
icculus@0
   284
    luaState = lua_newstate(luaAlloc, NULL);
icculus@0
   285
icculus@0
   286
    lua_atpanic(luaState, luaFatal);
icculus@0
   287
    assert(lua_checkstack(luaState, 20));  // Just in case.
icculus@2
   288
    luaL_openlibs(luaState);
icculus@0
   289
icculus@0
   290
    // Set up initial C functions, etc we want to expose to Lua code...
icculus@0
   291
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
icculus@0
   292
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
icculus@11
   293
    luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
icculus@11
   294
    luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
icculus@11
   295
    luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
icculus@11
   296
    luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
icculus@11
   297
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
icculus@0
   298
icculus@7
   299
    // Set up argv table...
icculus@7
   300
    lua_newtable(luaState);
icculus@7
   301
    int i;
icculus@7
   302
    for (i = 0; i < argc; i++)
icculus@7
   303
    {
icculus@7
   304
        lua_pushinteger(luaState, i+1);
icculus@7
   305
        lua_pushstring(luaState, argv[i]);
icculus@7
   306
        lua_settable(luaState, -3);
icculus@7
   307
    } // for
icculus@7
   308
    lua_setglobal(luaState, "argv");
icculus@7
   309
icculus@3
   310
    // Transfer control to Lua...
icculus@0
   311
    if (luaL_dofile(luaState, "1pass.lua") != 0)
icculus@0
   312
    {
icculus@0
   313
        const char *msg = lua_tostring(luaState, -1);
icculus@0
   314
        fprintf(stderr, "1pass.lua didn't run: %s\n", msg);
icculus@0
   315
        lua_pop(luaState, 1);
icculus@0
   316
        return 0;
icculus@0
   317
    } // if
icculus@0
   318
icculus@0
   319
    return 1;
icculus@0
   320
} // initLua
icculus@0
   321
icculus@0
   322
icculus@0
   323
int main(int argc, char **argv)
icculus@0
   324
{
icculus@11
   325
    gtk_init(&argc, &argv);
icculus@0
   326
icculus@7
   327
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
icculus@0
   328
        return 1;
icculus@0
   329
icculus@0
   330
    return 0;
icculus@0
   331
} // main
icculus@0
   332
icculus@0
   333
// end of 1pass.c ...
icculus@0
   334