kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: bjorn@mork.no (Bjørn Mork)
To: kernelnewbies@lists.kernelnewbies.org
Subject: SHA-1 hash calculate in Kernel.
Date: Fri, 28 Aug 2015 10:27:02 +0200	[thread overview]
Message-ID: <87r3mnzull.fsf@nemi.mork.no> (raw)
In-Reply-To: <CA+5jrfnTkfLUmfTk-CAA2uimS_DcHU-Buk4ovpZ+RZtD4N2thw@mail.gmail.com> (lx's message of "Fri, 28 Aug 2015 15:36:03 +0800")

lx <lxlenovostar@gmail.com> writes:

> 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)
> {
>     /*
>      * http://lxr.oss.org.cn/source/fs/ecryptfs/crypto.c?v=2.6.30
>      */
>     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,

I suggest you add something like this:

    printk(KERN_INFO "plaintext='%c', len=%zu, valid=%s\n", *plaintext, len, virt_addr_valid(plaintext) ? "true" : "false");


and then go figure out the relationship between that and the correct
result :)




Bj?rn

  reply	other threads:[~2015-08-28  8:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-28  7:36 SHA-1 hash calculate in Kernel lx
2015-08-28  8:27 ` Bjørn Mork [this message]
2015-08-28 17:04 ` Jeff Haran
2015-08-29 11:49 ` Bernd Petrovitsch
2015-10-22  8:24   ` lx

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r3mnzull.fsf@nemi.mork.no \
    --to=bjorn@mork.no \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).