01e1152003-03-12Niels Möller /* nettle.cmod -*- c -*- */ #include "global.h"
654f152003-03-14Marcus Comstedt RCSID("$Id: nettle.cmod,v 1.4 2003/03/14 15:30:20 marcus Exp $");
01e1152003-03-12Niels Möller #include "interpret.h" #include "svalue.h" /* For this_object() */ #include "object.h" #include "module_support.h"
4e1f622003-03-13Niels Möller #include "nettle_config.h"
770fee2003-03-12Henrik Grubbström (Grubba) #ifdef HAVE_LIBNETTLE
01e1152003-03-12Niels 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); PIKEFUN string name() { push_string(make_shared_string("aes")); } 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)); } }
770fee2003-03-12Henrik Grubbström (Grubba) 
654f152003-03-14Marcus Comstedt #endif /* HAVE_LIBNETTLE */
4e1f622003-03-13Niels Möller PIKE_MODULE_INIT { INIT;
654f152003-03-14Marcus Comstedt #ifdef HAVE_LIBNETTLE
4e1f622003-03-13Niels Möller  hash_init();
654f152003-03-14Marcus Comstedt #endif /* HAVE_LIBNETTLE */
4e1f622003-03-13Niels Möller } PIKE_MODULE_EXIT {
654f152003-03-14Marcus Comstedt #ifdef HAVE_LIBNETTLE
4e1f622003-03-13Niels Möller  hash_exit();
654f152003-03-14Marcus Comstedt #endif /* HAVE_LIBNETTLE */
4e1f622003-03-13Niels Möller  EXIT; }