LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-26-2009, 04:09 PM   #1
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Rep: Reputation: 15
Anyone ever used crypto/aes.h ?


I admit I haven't dug around the source too much yet and I'll do that the second after I post this, but figured I'd just throw this in real quick first and see what's quicker, the source or the forum.

So I'm expanding keys to perform AES CTR mode, what should I pass as the struct crypto_aes_ctx *ctx argument to the key expansion function and how should I initialize it?

Any good documentation you can point me to?

thanks guys
 
Old 06-26-2009, 06:48 PM   #2
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Original Poster
Rep: Reputation: 15
Haven't compiled and tested thoroughly yet, but this is from a mix of tcrypt.c and the aes.h. The general flow is correct, maybe I call some functions where I should call others, like tfm_set_key might eventually call crypto_aes_setkey if I have it initialized correct, but hey, figured I'd post something to show some progress.

Code:
/* Expand the key using kernel aes library */
tfm = crypto_alloc_ablkcipher("rfc3686(ctr(aes))", 0, 0);
if (IS_ERR(tfm))
{
    printk("%s: failed to load transform for %s: %ld\n",
           AES_MODULE, "rfc3686(ctr(aes))", PTR_ERR(tfm));
    err = PTR_ERR(tfm);
    return err;
}
/* This function handles key expansion */
err = crypto_aes_setkey(tfm, newTek->tek, TEK_LEN);
if (err)
{
    printk("%s: aes setkey for key expansion failed\n", AES_MODULE);
    return err;
}
ctx = crypto_tfm_ctx(tfm);
newTek->ek_len = 8*(ctx->key_length);
if (newTek->ek_len != EXPANDED_KEY_LEN)
{
    printk(KERN_DEBUG "%s: expanded key length is %u, expected %u\n",
           AES_MODULE, newTek->ek_len, EXPANDED_KEY_LEN);
    err = -1;
    return err;
}
memcpy(newTek->expanded_key, ctx->key_enc, newTek->ek_len);
crypto_free_tfm(tfm);
 
Old 06-26-2009, 08:02 PM   #3
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Original Poster
Rep: Reputation: 15
Alright, got it to compile, no errors or warnings. It's a mix of using the kernel crypto API plus using some lower level stuff just so I can get at the expanded key and copy it to where I need it in memory. Check it out:

Code:
struct crypto_ablkcipher *cipher = NULL;
struct crypto_tfm *tfm = NULL;
struct ablkcipher_request *req = NULL;
struct crypto_aes_ctx *ctx = NULL;
int err;

/* Expand the key using kernel aes library */
cipher = crypto_alloc_ablkcipher("rfc3686(ctr(aes))", 0, 0);
if (IS_ERR(cipher))
{
    printk("%s: failed to load transform for %s: %ld\n",
           AES_MODULE, "rfc3686(ctr(aes))", PTR_ERR(tfm));
    return -1;
}

/* Request needed just for key expansion? */
req = ablkcipher_request_alloc(cipher, GFP_KERNEL);
if (!req) {
    printk("%s: failed to allocate request for %s\n",
           AES_MODULE, "rfc3686(ctr(aes))");
    return -1;
}

/* This function handles key expansion */
err = crypto_ablkcipher_setkey(cipher, newTek->tek, TEK_LEN);
if (err)
{
    printk("%s: aes setkey for key expansion failed\n", AES_MODULE);
    return err;
}
tfm = crypto_ablkcipher_tfm(cipher);
ctx = crypto_tfm_ctx(tfm);
newTek->ek_len = 4*(ctx->key_length);
if (newTek->ek_len != EXPANDED_KEY_LEN)
{
    printk(KERN_DEBUG "%s: expanded key length is %u, expected %u\n",
           AES_MODULE, newTek->ek_len, EXPANDED_KEY_LEN);
    err = -1;
    return err;
}
memcpy(newTek->expanded_key, ctx->key_enc, newTek->ek_len); 
crypto_free_ablkcipher(cipher);
ablkcipher_request_free(req);
FYI newTek->tek is a 256-bit encryption key, TEK_LEN is 32 and EXPANDED_KEY_LEN is 240.

Anybody know if I need to bother with the request like I make a mention of in my comments?

Should I multiply ek_len by 4? If you look at aes.h the keys are stored as u32 whereas I am storing expanded_key as a u8 so that was my logic behind it.

C'mon I hate talking to myself...
 
  


Reply

Tags
aes, expansion, key



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AES boobymonster Linux - Security 4 01-31-2009 09:04 AM
using aes-i586 instead of just aes whysyn Linux - Security 0 03-07-2007 03:47 PM
add aes /proc/crypto to permanently Melsync Linux - Security 7 07-20-2006 05:26 AM
add aes to /proc/crypto permanently Melsync Linux - Security 2 07-16-2006 02:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration