Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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);
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.