简体   繁体   中英

Hashing same string generates different results in kernel module

I am following https://www.kernel.org/doc/html/v4.15/crypto/api-samples.html#code-example-for-use-of-operational-state-memory-with-shash and I have a LKM structure as such:

test.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <asm/uaccess.h>
#include "chip.h"

#define SHA_DIGEST_SIZE 32 
#define SHA_DIGEST_HD_SIZE SHA_DIGEST_SIZE * 2 

MODULE_LICENSE("GPL"); 

static char ker_buf[200];

static int dev_open(struct inode *inod, struct file *fil);
static ssize_t dev_read(struct file *filep,char *buf,size_t len,loff_t *off);
static ssize_t dev_write(struct file *flip,const char *buff,size_t len,loff_t *off);
static int dev_release(struct inode *inod,struct file *fil);

static struct file_operations fops=
{
.read=dev_read,
.write=dev_write,
.open=dev_open,
.release=dev_release,
};

static int hello_init(void){
int t=register_chrdev(90,"mydev",&fops);
if(t<0)
printk(KERN_ALERT "device registration failed.");
else
printk(KERN_ALERT "device registred\n");
return 0;
}

static void hello_exit(void){
unregister_chrdev(90,"mydev");
printk(KERN_ALERT "exit");
} 

static int dev_open(struct inode *inod, struct file *fil){
printk("KERN_ALERT device opened");
return 0;
} 

static ssize_t dev_read(struct file *filep,char *buf,size_t len,loff_t *off){
size_t i;
unsigned char digest[SHA_DIGEST_SIZE];
unsigned char digest_hd[SHA_DIGEST_HD_SIZE+2];
int hash = generate_hash(ker_buf, len, digest);

for (i = 0; i < SHA_DIGEST_SIZE; i++)
    sprintf(&digest_hd[i*2], "%02x", digest[i]);   

copy_to_user(buf,ker_buf,len);
return len;
}

static ssize_t dev_write(struct file *flip,const char *buf,size_t len,loff_t *off)
{  
copy_from_user(ker_buf,buf,len);
ker_buf[len]=0;
return len;
}

static int dev_release(struct inode *inod,struct file *fil){
printk("KERN_ALERT device closed\n");
return 0;
}

module_init(hello_init);
module_exit(hello_exit);

chip.h

#include <crypto/hash.h>

#ifndef MODULE_CHIP_H
#define MODULE_CHIP_H

struct shash_desc *init_sdesc(struct crypto_shash *alg);

int calc_hash(struct crypto_shash *alg, const unsigned char *data, unsigned 
int datalen,unsigned char *digest);

int generate_hash(const unsigned char *data, unsigned int datalen,unsigned char *digest);

#endif

chip.c

#include "chip.h"

struct shash_desc *init_sdesc(struct crypto_shash *alg)
{
struct shash_desc *sdesc;

sdesc = kmalloc(sizeof(*sdesc) + crypto_shash_descsize(alg), GFP_KERNEL);
if (!sdesc)
    return ERR_PTR(-ENOMEM);
sdesc->tfm = alg;

return sdesc;
}

int calc_hash(struct crypto_shash *alg,
                 const unsigned char *data, unsigned int datalen,
                 unsigned char *digest)
{
struct shash_desc *sdesc;
int ret;

sdesc = init_sdesc(alg);
if (IS_ERR(sdesc)) {
    pr_err("can't alloc sdesc\n");
    return PTR_ERR(sdesc);
}

ret = crypto_shash_digest(sdesc, data, datalen, digest);
kfree(sdesc);
return ret;
}

int generate_hash(const unsigned char *data, unsigned int datalen,
                 unsigned char *digest)
{
struct crypto_shash *alg;
char *hash_alg_name = "sha256";
int ret;

alg = crypto_alloc_shash(hash_alg_name, 0, 0);
if (IS_ERR(alg)) {
    pr_err("can't alloc alg %s\n", hash_alg_name);
    return PTR_ERR(alg);
}
ret = calc_hash(alg, data, datalen, digest);

crypto_free_shash(alg);
return ret;
}

Makefile

KVERSION := $(shell uname -r)
obj-m := test1.o
test1-objs := test.o chip.o
all:
    $(MAKE) -C /lib/modules/$(KVERSION)/build M=$(PWD) modules

clean:
    $(MAKE) -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

module loads and runs as expected, my goal is to get the hashed version of the written string read back from kernel module. The values I read back are a hex string of the expected length, but not matching the correct hash of the input. What I might be doing wrong ?

dev_write has to remember how many bytes it stored and dev_read has to use that number instead of its len argument

static size_t ker_len = 0;
static ssize_t dev_read(struct file *filep,char *buf,size_t len,loff_t *off){
size_t i;
unsigned char digest[SHA_DIGEST_SIZE];
unsigned char digest_hd[SHA_DIGEST_HD_SIZE+2];
int hash = generate_hash(ker_buf, ker_len, digest);

for (i = 0; i < SHA_DIGEST_SIZE; i++)
    sprintf(&digest_hd[i*2], "%02x", digest[i]);   

copy_to_user(buf,digest_hd,len);
return len;
}

static ssize_t dev_write(struct file *flip,const char *buf,size_t len,loff_t *off)
{  
copy_from_user(ker_buf,buf,len);
ker_buf[len]=0;
ker_len = len;
return len;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM