Blame src/lua/quvi/crypto/en_decrypt.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2013  Toni Gundogdu <legatvs@gmail.com>
Packit 3ff1e7
 *
Packit 3ff1e7
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is free software: you can redistribute it and/or
Packit 3ff1e7
 * modify it under the terms of the GNU Affero General Public
Packit 3ff1e7
 * License as published by the Free Software Foundation, either
Packit 3ff1e7
 * version 3 of the License, or (at your option) any later version.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is distributed in the hope that it will be useful,
Packit 3ff1e7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff1e7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3ff1e7
 * GNU Affero General Public License for more details.
Packit 3ff1e7
 *
Packit 3ff1e7
 * You should have received a copy of the GNU Affero General
Packit 3ff1e7
 * Public License along with this library.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
/*
Packit 3ff1e7
 * NOTES
Packit 3ff1e7
 *
Packit 3ff1e7
 * The 'key', the 'ciphertext' and the 'plaintext' are all expected to
Packit 3ff1e7
 * be hexadecimal strings when passed from and to a Lua script.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <lauxlib.h>
Packit 3ff1e7
#include <gcrypt.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
#include <quvi.h>
Packit 3ff1e7
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "gcrypt/crypto.h"
Packit 3ff1e7
#include "lua/quvi/crypto/opts.h"
Packit 3ff1e7
#include "lua/quvi/crypto/err.h"
Packit 3ff1e7
#include "lua/quvi/opts.h"
Packit 3ff1e7
#include "lua/getfield.h"
Packit 3ff1e7
#include "lua/setfield.h"
Packit 3ff1e7
#include "lua/def.h"
Packit 3ff1e7
Packit 3ff1e7
static gint _exec(lua_State *l, const CryptoMode crypto_mode,
Packit 3ff1e7
                  const gchar *push_as)
Packit 3ff1e7
{
Packit 3ff1e7
  struct l_quvi_object_crypto_opts_s co;
Packit 3ff1e7
  gboolean croak_if_error;
Packit 3ff1e7
  GSList *opts;
Packit 3ff1e7
  crypto_t c;
Packit 3ff1e7
  _quvi_t q;
Packit 3ff1e7
Packit 3ff1e7
  memset(&co, 0, sizeof(struct l_quvi_object_crypto_opts_s));
Packit 3ff1e7
Packit 3ff1e7
  /* quvi handle */
Packit 3ff1e7
Packit 3ff1e7
  q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
Packit 3ff1e7
  g_assert(q != NULL);
Packit 3ff1e7
Packit 3ff1e7
  /* function args */
Packit 3ff1e7
Packit 3ff1e7
  co.text = luaL_checkstring(l, 1);
Packit 3ff1e7
  lua_pop(l, 1);
Packit 3ff1e7
Packit 3ff1e7
  /* options */
Packit 3ff1e7
Packit 3ff1e7
  opts = l_quvi_object_opts_new(l, 2);
Packit 3ff1e7
  croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);
Packit 3ff1e7
  l_quvi_object_crypto_chk_opts(l, opts, &co);
Packit 3ff1e7
Packit 3ff1e7
  /* encrypt/decrypt (return as a hexstring). */
Packit 3ff1e7
Packit 3ff1e7
  c = crypto_new(co.algoname, crypto_mode, co.cipher.key,
Packit 3ff1e7
                 co.cipher.mode, co.cipher.flags);
Packit 3ff1e7
  q->status.rc = l_quvi_object_crypto_chk_if_failed(l, c, croak_if_error, q);
Packit 3ff1e7
  {
Packit 3ff1e7
    guchar *s;
Packit 3ff1e7
    gsize n;
Packit 3ff1e7
Packit 3ff1e7
    s = crypto_hex2bytes(co.text, &n);
Packit 3ff1e7
    if (s != NULL)
Packit 3ff1e7
      {
Packit 3ff1e7
#ifdef _1
Packit 3ff1e7
        crypto_dump("s", s, n);
Packit 3ff1e7
#endif
Packit 3ff1e7
        crypto_exec(c, s, n);
Packit 3ff1e7
        g_free(s);
Packit 3ff1e7
Packit 3ff1e7
        q->status.rc =
Packit 3ff1e7
          l_quvi_object_crypto_chk_if_failed(l, c, croak_if_error, q);
Packit 3ff1e7
      }
Packit 3ff1e7
    else
Packit 3ff1e7
      l_quvi_object_crypto_invalid_hexstr(l, q, croak_if_error);
Packit 3ff1e7
  }
Packit 3ff1e7
Packit 3ff1e7
  /* Return a table of results. */
Packit 3ff1e7
Packit 3ff1e7
  lua_newtable(l);
Packit 3ff1e7
  l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1);
Packit 3ff1e7
  l_setfield_n(l, QO_QUVI_CODE, q->status.rc);
Packit 3ff1e7
Packit 3ff1e7
  if (q->status.rc == QUVI_OK)
Packit 3ff1e7
    {
Packit 3ff1e7
      gchar *s = crypto_bytes2hex(c->out.data, c->out.dlen);
Packit 3ff1e7
      l_setfield_s(l, push_as, s, -1);
Packit 3ff1e7
      g_free(s);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  l_quvi_object_opts_free(opts);
Packit 3ff1e7
  crypto_free(c);
Packit 3ff1e7
Packit 3ff1e7
  return (1); /* no. of returned values (a table) */
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gint l_quvi_crypto_encrypt(lua_State *l)
Packit 3ff1e7
{
Packit 3ff1e7
  return (_exec(l, CRYPTO_MODE_ENCRYPT, QO_CIPHERTEXT));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gint l_quvi_crypto_decrypt(lua_State *l)
Packit 3ff1e7
{
Packit 3ff1e7
  return (_exec(l, CRYPTO_MODE_DECRYPT, QO_PLAINTEXT));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */