Question about implementation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm the student who want to encrypt, decrypt some data in 
kernel mode.  I've stuck for serveral days, but I still cannot 
figure it out why I can't really encrypt the data.  Could you 
please point out what's problem that I've made.

Thanks so much.

Code:

#define __KERNEL__
#define MODULE
#include <linux/module.h>

#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif

#include <linux/string.h>
//#include <linux/mm.h>

#include <linux/init.h>
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <linux/crypto.h>
#include <asm-i386/types.h>
#include <linux/inet.h>

#define DEBUG

struct cipher_context *cx;
struct cipher_implementation *ci;
unsigned char key1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00};

void dump_u32_buffer(u32 *buf, int number)
{
	int i;

	for(i=0;i<number;i++)
	{
		printk("%08x ",buf[i]);
		if(i && ((i+1)%4)==0)
			printk("\n");
		else
			printk("     ");
	}
	if((i%4)!=0)
		printk("\n");
}


static int init_cipher()
{
  char plain_text[11] = "readline26\n";
  char * cipher_text = NULL;
  __u32 input1 = 0;
  __u32 output1 = 0;


  ci = find_cipher_by_name("rijndael-ecb", 1);
  if (!ci) {
    printk("Find cipher error.\n");
    return 1;
  }

  if ( (cx = (struct cipher_context *) kmalloc(sizeof(struct 
cipher_context), GFP_KERNEL)) == NULL) {
    printk("Creation of cipher_context failed..\n");
    return 1;
  }

  cx = ci->realloc_context(NULL, ci, sizeof(key1));
  cx->ci = ci;
  ci->wipe_context(cx);
  ci->set_key(cx, key1, sizeof(key1));
  printk("%s\n", plain_text);
  input1 = (__u32)plain_text;
  printk("B4 Encrypt:\n");
  dump_u32_buffer(&input1, sizeof(input1));
  ci->encrypt(cx, (u8 *)&input1, (u8 *) &output1, 
sizeof(&input1));
  printk("\nEncrypt:\n");
  dump_u32_buffer(&output1, sizeof(output1)); 
  ci->decrypt(cx, (u8 *)&output1, (u8 *) &input1, 
sizeof(&input1));
  printk("\nDecrypt:\n");
  dump_u32_buffer(&input1, sizeof(input1));
  printk("\nString :\n");
  cipher_text = (char *)&input1;
  printk("%s\n", (char*)input1);
  return 0;
}

static int __init init(void)
{
  printk("Init..\n");
#ifdef DEBUG

  while(MOD_IN_USE)
    MOD_DEC_USE_COUNT;
  MOD_INC_USE_COUNT;
#endif
  return init_cipher();
}

static void __exit fini(void)
{
  kfree(cx->keyinfo);
  kfree(cx);
  printk("Finish..\n");
}

module_init(init);
module_exit(fini);


Very respectfully,
marco.



Linux-crypto:  cryptography in and on the Linux system
Archive:       http://mail.nl.linux.org/linux-crypto/


[Index of Archives]     [Kernel]     [Linux Crypto]     [Gnu Crypto]     [Gnu Classpath]     [Netfilter]     [Bugtraq]
  Powered by Linux