01e115 | 2003-03-12 | Niels Möller | | /* nettle.cmod -*- c -*- */
#include "global.h"
|
636c42 | 2003-03-18 | Niels Möller | | RCSID("$Id: nettle.cmod,v 1.6 2003/03/18 16:28:37 nisse Exp $");
|
01e115 | 2003-03-12 | Niels Möller | | #include "interpret.h"
#include "svalue.h"
/* For this_object() */
#include "object.h"
#include "module_support.h"
|
4e1f62 | 2003-03-13 | Niels Möller | | #include "nettle_config.h"
|
770fee | 2003-03-12 | Henrik Grubbström (Grubba) | | #ifdef HAVE_LIBNETTLE
|
01e115 | 2003-03-12 | Niels Möller | | #include "nettle.h"
#include <nettle/aes.h>
#include <nettle/nettle-meta.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
/* #define sp Pike_sp */
DECLARATIONS
/*! @module Nettle
*/
PIKECLASS aes
{
CVAR struct aes_ctx ctx;
CVAR void (*f)(struct aes_ctx *, unsigned length,
uint8_t *dst, const uint8_t *src);
|
636c42 | 2003-03-18 | Niels Möller | |
|
01e115 | 2003-03-12 | Niels Möller | | PIKEFUN string name()
{
push_string(make_shared_string("aes"));
}
|
636c42 | 2003-03-18 | Niels Möller | |
|
01e115 | 2003-03-12 | Niels Möller | | PIKEFUN int key_size()
{
/* Recommended key size. */
push_int(AES_KEY_SIZE);
}
PIKEFUN int block_size()
{
push_int(AES_BLOCK_SIZE);
}
PIKEFUN object set_encrypt_key(string key)
{
NO_WIDE_STRING(key);
if ( (key->len < AES_MIN_KEY_SIZE)
|| (key->len > AES_MAX_KEY_SIZE) )
Pike_error("Invalid key size for aes.");
aes_set_encrypt_key(&THIS->ctx, key->len, key->str);
THIS->f = aes_encrypt;
push_object(this_object());
}
PIKEFUN object set_decrypt_key(string key)
{
NO_WIDE_STRING(key);
if ( (key->len < AES_MIN_KEY_SIZE)
|| (key->len > AES_MAX_KEY_SIZE) )
Pike_error("Invalid key size for aes.");
aes_set_decrypt_key(&THIS->ctx, key->len, key->str);
THIS->f = aes_decrypt;
push_object(this_object());
}
PIKEFUN string crypt(string data)
{
struct pike_string *s;
/* werror("aes->encrypt\n"); */
#if 0
{
struct aes_ctx test_ctx;
const uint8_t clear[16] = "aaaaaaaaaaaaaaaa";
uint8_t cipher[16];
werror("tfoo\n");
aes_set_encrypt_key(&test_ctx, 16, "0123456789abcdef");
werror("tbar\n");
aes_encrypt(&test_ctx, 16, cipher, clear);
werror("tbaz\n");
}
#endif
if (!THIS->f)
Pike_error("Must install an encrypt or decrypt key first.");
// werror("foo\n");
NO_WIDE_STRING(data);
// werror("bar\n");
if (data->len % AES_BLOCK_SIZE)
Pike_error("Data must be an integer number of blocks.");
// werror("baz\n");
s = begin_shared_string(data->len);
// werror("bazz\n");
// werror("ctx = %p, len = %d, dst = %p, src = %p\n",
// &THIS->ctx, data->len, s->str, data->str);
THIS->f(&THIS->ctx, data->len, s->str, data->str);
/* aes_encrypt(&THIS->ctx, data->len, s->str, data->str); */
// werror("bazzz\n");
push_string(end_shared_string(s));
// werror("bazzzz\n");
}
INIT
{
/* werror("aes->INIT\n"); */
THIS->f = NULL;
}
EXIT
{
/* werror("aes->EXIT\n"); */
memset(&THIS->ctx, 0, sizeof(THIS->ctx));
}
}
|
770fee | 2003-03-12 | Henrik Grubbström (Grubba) | |
|
654f15 | 2003-03-14 | Marcus Comstedt | | #endif /* HAVE_LIBNETTLE */
|
4e1f62 | 2003-03-13 | Niels Möller | | PIKE_MODULE_INIT
{
INIT;
|
654f15 | 2003-03-14 | Marcus Comstedt | | #ifdef HAVE_LIBNETTLE
|
4e1f62 | 2003-03-13 | Niels Möller | | hash_init();
|
636c42 | 2003-03-18 | Niels Möller | | cipher_init();
|
654f15 | 2003-03-14 | Marcus Comstedt | | #endif /* HAVE_LIBNETTLE */
|
4e1f62 | 2003-03-13 | Niels Möller | | }
PIKE_MODULE_EXIT
{
|
654f15 | 2003-03-14 | Marcus Comstedt | | #ifdef HAVE_LIBNETTLE
|
636c42 | 2003-03-18 | Niels Möller | | cipher_exit();
|
4e1f62 | 2003-03-13 | Niels Möller | | hash_exit();
|
654f15 | 2003-03-14 | Marcus Comstedt | | #endif /* HAVE_LIBNETTLE */
|
4e1f62 | 2003-03-13 | Niels Möller | | EXIT;
}
|
4a63d4 | 2003-03-18 | Martin Nilsson | | /*! @endmodule
*/
|