RE: SHA-1 hash calculate in Kernel.

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

 



 

 

From: kernelnewbies-bounces@xxxxxxxxxxxxxxxxx [mailto:kernelnewbies-bounces@xxxxxxxxxxxxxxxxx] On Behalf Of lx
Sent: Friday, August 28, 2015 12:36 AM
To: kernelnewbies
Subject: SHA-1 hash calculate in Kernel.

 

hi all:

      I built a module for calculate the SHA-1. The code is:

##################

#include <linux/init.h>

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/crypto.h>

#include <linux/err.h>

#include <linux/scatterlist.h>

MODULE_LICENSE("Dual BSD/GPL");

 

#define SHA1_LENGTH     20

 

static int hello_init(void)

{

    /*  

     */

    struct scatterlist sg; 

    struct hash_desc desc;

 

    //way 1     

    /*  

    char *plaintext = "c";

    size_t len = strlen(plaintext);

    */

 

    //way 2

    /*  

    char *plaintext = kmalloc(sizeof(char), GFP_KERNEL);

    plaintext = "c";    

    size_t len = 1;

    */

 

    // way 3.

    /*  

    char plaintext[1] = {'c'};

    size_t len = 1;

    */

 

    // way 4.

    /*  

    char *plaintext = (char *)__get_free_page(GFP_KERNEL);

    memcpy(plaintext, "c", 1);

    size_t len = 1;

    */

 

    int rc = 0;

    int i;  

    char hashtext[SHA1_LENGTH];

 

    memset(hashtext, 0x00, SHA1_LENGTH);

    printk(KERN_INFO "sha1: %s\n", __FUNCTION__);

 

    sg_init_one(&sg, plaintext, len);

    desc.tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);

    desc.flags = 0;

 

    rc = crypto_hash_init(&desc);

    if (rc) {

        printk(KERN_ERR "%s: Error initializing crypto hash; rc = [%d]\n", __func__, rc);

        goto out;

    }

    rc = crypto_hash_update(&desc, &sg, len);

    if (rc) {

        printk(KERN_ERR "%s: Error updating crypto hash; rc = [%d]\n", __func__, rc);

        goto out;

    }

    rc = crypto_hash_final(&desc, hashtext);

    if (rc) {

        printk(KERN_ERR "%s: Error finalizing crypto hash; rc = [%d]\n", __func__, rc);

        goto out;

    }

    crypto_free_hash(desc.tfm);

 

    for (i = 0; i < 20; i++) {

        printk(KERN_INFO "%02x-%d\n", hashtext[i]&0xff, i);

    }

 

out:

    printk(KERN_INFO "end\n");

    return rc;

}

 

static void hello_exit(void)

{

    printk(KERN_ALERT "Goodbye, cruel world\n");

}

 

module_init(hello_init);

module_exit(hello_exit);

##################

I can just get the right result by way 3 and 4, In way 1 and 2 , I get the wrong result, 

Please tell me why? Thank you.

 

I don’t know why the crypto function isn’t generating the right result in the first two cases. Just pointing out that way 1 and way 2 are identical except that way 2 leaks the memory that was kmalloc()’ed.  This in way 2 is a pointer assignment, not a buffer copy:

 

    plaintext = "c"; 

 

The big difference it seems between ways 1 and 2 and ways 3 and 4 is in the former, plaintext points to a string constant that I believe is going to be read only whereas in the latter, plaintext points to r/w memory. Maybe the crypto functions need r/w access?

 

Jeff Haran

 

 

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux