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

问题描述

我正在关注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,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,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,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,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 char *digest)
{
struct crypto_shash *alg;
char *hash_alg_name = "sha256";
int ret;

alg = crypto_alloc_shash(hash_alg_name,0);
if (IS_ERR(alg)) {
    pr_err("can't alloc alg %s\n",hash_alg_name);
    return PTR_ERR(alg);
}
ret = calc_hash(alg,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

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

解决方法

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,loff_t *off)
{  
copy_from_user(ker_buf,buf,len);
ker_buf[len]=0;
ker_len = len;
return len;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...