crypto – use Linux kernel crypto API

Crypto is an important part of the Linux kernel source code. One can tell from the standalone crypto directory under the kernel source tree. Device drivers, file system and security all need crypto. This post does not help understand the implementation of Linux kernel crypto API. Instead, a working kernel module is provided to show how to use the kernel crypto API.

0. R.t.D.C.

/*
 * sha1_mod.c
 * A kernel module using SHA1 and Linux kernel crypto API
 * Reference: http://stackoverflow.com/questions/3869028/how-to-use-cryptoapi-in-the-linux-kernel-2-6
 * Jun 9, 2014
 * root@davejingtian.org
 * http://davejingtian.org
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/crypto.h>
#include <linux/err.h>
#include <linux/scatterlist.h>

#define SHA1_LENGTH     20

static int __init sha1_init(void)
{
    struct scatterlist sg;
    struct crypto_hash *tfm;
    struct hash_desc desc;
    unsigned char output[SHA1_LENGTH];
    unsigned char buf[10];
    int i, j;

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

    memset(buf, 'A', 10);
    memset(output, 0x00, SHA1_LENGTH);

    tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
    if (IS_ERR(tfm)) {
	printk(KERN_ERR "daveti: tfm allocation failed\n");
	return 0;
    }

    desc.tfm = tfm;
    desc.flags = 0;

    //crypto_hash_init(&desc);
    //daveti: NOTE, crypto_hash_init is needed
    //for every new hasing!

    for (j = 0; j < 3; j++) {
	crypto_hash_init(&desc);
	sg_init_one(&sg, buf, 10);
	crypto_hash_update(&desc, &sg, 10);
	crypto_hash_final(&desc, output);

	for (i = 0; i < 20; i++) {
		printk(KERN_ERR "%02x", output[i]);
	}
	printk(KERN_INFO "\n---------------\n");
	memset(output, 0x00, SHA1_LENGTH);
    }

    crypto_free_hash(tfm);

    return 0;
}

static void __exit sha1_exit(void)
{
    printk(KERN_INFO "sha1: %s\n", __FUNCTION__);
}

module_init(sha1_init);
module_exit(sha1_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("daveti");

1. Pitfalls

As one might have known already, when we are trying to call crypto API for SHA1, we are querying the crypto manager (cryptomgr) eventually – a kernel thread maintains different algorithm instances and handles crypto operation requests all over the kernel. The truth is if crypto API is called, like crypto_alloc_hash(), before the cryptomgr is initialized during the kernel booting process, then NULL would be return. Make sure the crypto API is called after cryptomgr. Otherwise, you have to provide your own implementation.

If you are working in an IRQ context, be aware that some older crypto APIs may cause trouble. Based on my experience working on CentOS 6 with kernel 2.6.32, crypto_hash_digest() does not work. Instead, crypto_hash_final() works well.

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in OS, Programming, Security and tagged , , , , , . Bookmark the permalink.

2 Responses to crypto – use Linux kernel crypto API

  1. Pingback: Kernel Hacking – use crypto API in the IRQ context | davejingtian.org

  2. Hey very cool web site!! Guuy .. Beautiful .. Amazing ..
    I’ll bookmark your bllg and take the feeds also? I’m satisfied too seek out numerous useful
    info here in the post, we need work outt more techniques
    in this regard, thanks for sharing. . . . . .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s