散列相同的字符串在内核模块中产生不同的结果

Hashing same string generates different results in kernel module

我正在关注 https://www.kernel.org/doc/html/v4.15/crypto/api-samples.html#code-example-for-use-of-operational-state-memory-with-shash 并且我有这样的 LKM 结构:

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;
}

生成文件

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

模块按预期加载和运行,我的目标是获取从内核模块读回的写入字符串的散列版本。我读回的值是预期长度的十六进制字符串,但与输入的正确散列不匹配。我可能做错了什么?

dev_write 必须记住它存储了多少字节,dev_read 必须使用该数字而不是它的 len 参数

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;
}