Commit 4beb1060 authored by Herbert Xu's avatar Herbert Xu

crypto: vmx - Reindent to kernel style

This patch reidents the vmx code-base to the kernel coding style.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 0903e435
...@@ -30,110 +30,112 @@ ...@@ -30,110 +30,112 @@
#include "aesp8-ppc.h" #include "aesp8-ppc.h"
struct p8_aes_ctx { struct p8_aes_ctx {
struct crypto_cipher *fallback; struct crypto_cipher *fallback;
struct aes_key enc_key; struct aes_key enc_key;
struct aes_key dec_key; struct aes_key dec_key;
}; };
static int p8_aes_init(struct crypto_tfm *tfm) static int p8_aes_init(struct crypto_tfm *tfm)
{ {
const char *alg; const char *alg;
struct crypto_cipher *fallback; struct crypto_cipher *fallback;
struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
if (!(alg = crypto_tfm_alg_name(tfm))) { if (!(alg = crypto_tfm_alg_name(tfm))) {
printk(KERN_ERR "Failed to get algorithm name.\n"); printk(KERN_ERR "Failed to get algorithm name.\n");
return -ENOENT; return -ENOENT;
} }
fallback = crypto_alloc_cipher(alg, 0 ,CRYPTO_ALG_NEED_FALLBACK); fallback = crypto_alloc_cipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(fallback)) { if (IS_ERR(fallback)) {
printk(KERN_ERR "Failed to allocate transformation for '%s': %ld\n", printk(KERN_ERR
alg, PTR_ERR(fallback)); "Failed to allocate transformation for '%s': %ld\n",
return PTR_ERR(fallback); alg, PTR_ERR(fallback));
} return PTR_ERR(fallback);
printk(KERN_INFO "Using '%s' as fallback implementation.\n", }
crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback)); printk(KERN_INFO "Using '%s' as fallback implementation.\n",
crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
crypto_cipher_set_flags(fallback,
crypto_cipher_get_flags((struct crypto_cipher *) tfm)); crypto_cipher_set_flags(fallback,
ctx->fallback = fallback; crypto_cipher_get_flags((struct
crypto_cipher *)
return 0; tfm));
ctx->fallback = fallback;
return 0;
} }
static void p8_aes_exit(struct crypto_tfm *tfm) static void p8_aes_exit(struct crypto_tfm *tfm)
{ {
struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
if (ctx->fallback) { if (ctx->fallback) {
crypto_free_cipher(ctx->fallback); crypto_free_cipher(ctx->fallback);
ctx->fallback = NULL; ctx->fallback = NULL;
} }
} }
static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key, static int p8_aes_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen) unsigned int keylen)
{ {
int ret; int ret;
struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key); ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key); ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
pagefault_enable(); pagefault_enable();
ret += crypto_cipher_setkey(ctx->fallback, key, keylen); ret += crypto_cipher_setkey(ctx->fallback, key, keylen);
return ret; return ret;
} }
static void p8_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) static void p8_aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
{ {
struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
if (in_interrupt()) { if (in_interrupt()) {
crypto_cipher_encrypt_one(ctx->fallback, dst, src); crypto_cipher_encrypt_one(ctx->fallback, dst, src);
} else { } else {
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
aes_p8_encrypt(src, dst, &ctx->enc_key); aes_p8_encrypt(src, dst, &ctx->enc_key);
pagefault_enable(); pagefault_enable();
} }
} }
static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) static void p8_aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
{ {
struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctx *ctx = crypto_tfm_ctx(tfm);
if (in_interrupt()) { if (in_interrupt()) {
crypto_cipher_decrypt_one(ctx->fallback, dst, src); crypto_cipher_decrypt_one(ctx->fallback, dst, src);
} else { } else {
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
aes_p8_decrypt(src, dst, &ctx->dec_key); aes_p8_decrypt(src, dst, &ctx->dec_key);
pagefault_enable(); pagefault_enable();
} }
} }
struct crypto_alg p8_aes_alg = { struct crypto_alg p8_aes_alg = {
.cra_name = "aes", .cra_name = "aes",
.cra_driver_name = "p8_aes", .cra_driver_name = "p8_aes",
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
.cra_priority = 1000, .cra_priority = 1000,
.cra_type = NULL, .cra_type = NULL,
.cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK, .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = 0, .cra_alignmask = 0,
.cra_blocksize = AES_BLOCK_SIZE, .cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct p8_aes_ctx), .cra_ctxsize = sizeof(struct p8_aes_ctx),
.cra_init = p8_aes_init, .cra_init = p8_aes_init,
.cra_exit = p8_aes_exit, .cra_exit = p8_aes_exit,
.cra_cipher = { .cra_cipher = {
.cia_min_keysize = AES_MIN_KEY_SIZE, .cia_min_keysize = AES_MIN_KEY_SIZE,
.cia_max_keysize = AES_MAX_KEY_SIZE, .cia_max_keysize = AES_MAX_KEY_SIZE,
.cia_setkey = p8_aes_setkey, .cia_setkey = p8_aes_setkey,
.cia_encrypt = p8_aes_encrypt, .cia_encrypt = p8_aes_encrypt,
.cia_decrypt = p8_aes_decrypt, .cia_decrypt = p8_aes_decrypt,
}, },
}; };
...@@ -31,154 +31,162 @@ ...@@ -31,154 +31,162 @@
#include "aesp8-ppc.h" #include "aesp8-ppc.h"
struct p8_aes_cbc_ctx { struct p8_aes_cbc_ctx {
struct crypto_blkcipher *fallback; struct crypto_blkcipher *fallback;
struct aes_key enc_key; struct aes_key enc_key;
struct aes_key dec_key; struct aes_key dec_key;
}; };
static int p8_aes_cbc_init(struct crypto_tfm *tfm) static int p8_aes_cbc_init(struct crypto_tfm *tfm)
{ {
const char *alg; const char *alg;
struct crypto_blkcipher *fallback; struct crypto_blkcipher *fallback;
struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
if (!(alg = crypto_tfm_alg_name(tfm))) { if (!(alg = crypto_tfm_alg_name(tfm))) {
printk(KERN_ERR "Failed to get algorithm name.\n"); printk(KERN_ERR "Failed to get algorithm name.\n");
return -ENOENT; return -ENOENT;
} }
fallback = crypto_alloc_blkcipher(alg, 0 ,CRYPTO_ALG_NEED_FALLBACK); fallback =
if (IS_ERR(fallback)) { crypto_alloc_blkcipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
printk(KERN_ERR "Failed to allocate transformation for '%s': %ld\n", if (IS_ERR(fallback)) {
alg, PTR_ERR(fallback)); printk(KERN_ERR
return PTR_ERR(fallback); "Failed to allocate transformation for '%s': %ld\n",
} alg, PTR_ERR(fallback));
printk(KERN_INFO "Using '%s' as fallback implementation.\n", return PTR_ERR(fallback);
crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback)); }
printk(KERN_INFO "Using '%s' as fallback implementation.\n",
crypto_blkcipher_set_flags(fallback, crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
crypto_blkcipher_get_flags((struct crypto_blkcipher *) tfm));
ctx->fallback = fallback; crypto_blkcipher_set_flags(
fallback,
return 0; crypto_blkcipher_get_flags((struct crypto_blkcipher *)tfm));
ctx->fallback = fallback;
return 0;
} }
static void p8_aes_cbc_exit(struct crypto_tfm *tfm) static void p8_aes_cbc_exit(struct crypto_tfm *tfm)
{ {
struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
if (ctx->fallback) { if (ctx->fallback) {
crypto_free_blkcipher(ctx->fallback); crypto_free_blkcipher(ctx->fallback);
ctx->fallback = NULL; ctx->fallback = NULL;
} }
} }
static int p8_aes_cbc_setkey(struct crypto_tfm *tfm, const u8 *key, static int p8_aes_cbc_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen) unsigned int keylen)
{ {
int ret; int ret;
struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key); ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key); ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
pagefault_enable(); pagefault_enable();
ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen); ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen);
return ret; return ret;
} }
static int p8_aes_cbc_encrypt(struct blkcipher_desc *desc, static int p8_aes_cbc_encrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst,
unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
{ {
int ret; int ret;
struct blkcipher_walk walk; struct blkcipher_walk walk;
struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx( struct p8_aes_cbc_ctx *ctx =
crypto_blkcipher_tfm(desc->tfm)); crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
struct blkcipher_desc fallback_desc = { struct blkcipher_desc fallback_desc = {
.tfm = ctx->fallback, .tfm = ctx->fallback,
.info = desc->info, .info = desc->info,
.flags = desc->flags .flags = desc->flags
}; };
if (in_interrupt()) { if (in_interrupt()) {
ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src, nbytes); ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src,
} else { nbytes);
pagefault_disable(); } else {
enable_kernel_altivec(); pagefault_disable();
enable_kernel_altivec();
blkcipher_walk_init(&walk, dst, src, nbytes);
ret = blkcipher_walk_virt(desc, &walk); blkcipher_walk_init(&walk, dst, src, nbytes);
while ((nbytes = walk.nbytes)) { ret = blkcipher_walk_virt(desc, &walk);
aes_p8_cbc_encrypt(walk.src.virt.addr, walk.dst.virt.addr, while ((nbytes = walk.nbytes)) {
nbytes & AES_BLOCK_MASK, &ctx->enc_key, walk.iv, 1); aes_p8_cbc_encrypt(walk.src.virt.addr,
walk.dst.virt.addr,
nbytes & AES_BLOCK_MASK,
&ctx->enc_key, walk.iv, 1);
nbytes &= AES_BLOCK_SIZE - 1; nbytes &= AES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, &walk, nbytes); ret = blkcipher_walk_done(desc, &walk, nbytes);
} }
pagefault_enable(); pagefault_enable();
} }
return ret; return ret;
} }
static int p8_aes_cbc_decrypt(struct blkcipher_desc *desc, static int p8_aes_cbc_decrypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst,
unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
{ {
int ret; int ret;
struct blkcipher_walk walk; struct blkcipher_walk walk;
struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx( struct p8_aes_cbc_ctx *ctx =
crypto_blkcipher_tfm(desc->tfm)); crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
struct blkcipher_desc fallback_desc = { struct blkcipher_desc fallback_desc = {
.tfm = ctx->fallback, .tfm = ctx->fallback,
.info = desc->info, .info = desc->info,
.flags = desc->flags .flags = desc->flags
}; };
if (in_interrupt()) { if (in_interrupt()) {
ret = crypto_blkcipher_decrypt(&fallback_desc, dst, src, nbytes); ret = crypto_blkcipher_decrypt(&fallback_desc, dst, src,
} else { nbytes);
pagefault_disable(); } else {
enable_kernel_altivec(); pagefault_disable();
enable_kernel_altivec();
blkcipher_walk_init(&walk, dst, src, nbytes);
ret = blkcipher_walk_virt(desc, &walk); blkcipher_walk_init(&walk, dst, src, nbytes);
while ((nbytes = walk.nbytes)) { ret = blkcipher_walk_virt(desc, &walk);
aes_p8_cbc_encrypt(walk.src.virt.addr, walk.dst.virt.addr, while ((nbytes = walk.nbytes)) {
nbytes & AES_BLOCK_MASK, &ctx->dec_key, walk.iv, 0); aes_p8_cbc_encrypt(walk.src.virt.addr,
walk.dst.virt.addr,
nbytes & AES_BLOCK_MASK,
&ctx->dec_key, walk.iv, 0);
nbytes &= AES_BLOCK_SIZE - 1; nbytes &= AES_BLOCK_SIZE - 1;
ret = blkcipher_walk_done(desc, &walk, nbytes); ret = blkcipher_walk_done(desc, &walk, nbytes);
} }
pagefault_enable(); pagefault_enable();
} }
return ret; return ret;
} }
struct crypto_alg p8_aes_cbc_alg = { struct crypto_alg p8_aes_cbc_alg = {
.cra_name = "cbc(aes)", .cra_name = "cbc(aes)",
.cra_driver_name = "p8_aes_cbc", .cra_driver_name = "p8_aes_cbc",
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
.cra_priority = 1000, .cra_priority = 1000,
.cra_type = &crypto_blkcipher_type, .cra_type = &crypto_blkcipher_type,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK, .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = 0, .cra_alignmask = 0,
.cra_blocksize = AES_BLOCK_SIZE, .cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct p8_aes_cbc_ctx), .cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
.cra_init = p8_aes_cbc_init, .cra_init = p8_aes_cbc_init,
.cra_exit = p8_aes_cbc_exit, .cra_exit = p8_aes_cbc_exit,
.cra_blkcipher = { .cra_blkcipher = {
.ivsize = 0, .ivsize = 0,
.min_keysize = AES_MIN_KEY_SIZE, .min_keysize = AES_MIN_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE, .max_keysize = AES_MAX_KEY_SIZE,
.setkey = p8_aes_cbc_setkey, .setkey = p8_aes_cbc_setkey,
.encrypt = p8_aes_cbc_encrypt, .encrypt = p8_aes_cbc_encrypt,
.decrypt = p8_aes_cbc_decrypt, .decrypt = p8_aes_cbc_decrypt,
}, },
}; };
...@@ -30,138 +30,147 @@ ...@@ -30,138 +30,147 @@
#include "aesp8-ppc.h" #include "aesp8-ppc.h"
struct p8_aes_ctr_ctx { struct p8_aes_ctr_ctx {
struct crypto_blkcipher *fallback; struct crypto_blkcipher *fallback;
struct aes_key enc_key; struct aes_key enc_key;
}; };
static int p8_aes_ctr_init(struct crypto_tfm *tfm) static int p8_aes_ctr_init(struct crypto_tfm *tfm)
{ {
const char *alg; const char *alg;
struct crypto_blkcipher *fallback; struct crypto_blkcipher *fallback;
struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
if (!(alg = crypto_tfm_alg_name(tfm))) { if (!(alg = crypto_tfm_alg_name(tfm))) {
printk(KERN_ERR "Failed to get algorithm name.\n"); printk(KERN_ERR "Failed to get algorithm name.\n");
return -ENOENT; return -ENOENT;
} }
fallback = crypto_alloc_blkcipher(alg, 0 ,CRYPTO_ALG_NEED_FALLBACK); fallback =
if (IS_ERR(fallback)) { crypto_alloc_blkcipher(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
printk(KERN_ERR "Failed to allocate transformation for '%s': %ld\n", if (IS_ERR(fallback)) {
alg, PTR_ERR(fallback)); printk(KERN_ERR
return PTR_ERR(fallback); "Failed to allocate transformation for '%s': %ld\n",
} alg, PTR_ERR(fallback));
printk(KERN_INFO "Using '%s' as fallback implementation.\n", return PTR_ERR(fallback);
crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback)); }
printk(KERN_INFO "Using '%s' as fallback implementation.\n",
crypto_blkcipher_set_flags(fallback, crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
crypto_blkcipher_get_flags((struct crypto_blkcipher *) tfm));
ctx->fallback = fallback; crypto_blkcipher_set_flags(
fallback,
return 0; crypto_blkcipher_get_flags((struct crypto_blkcipher *)tfm));
ctx->fallback = fallback;
return 0;
} }
static void p8_aes_ctr_exit(struct crypto_tfm *tfm) static void p8_aes_ctr_exit(struct crypto_tfm *tfm)
{ {
struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
if (ctx->fallback) { if (ctx->fallback) {
crypto_free_blkcipher(ctx->fallback); crypto_free_blkcipher(ctx->fallback);
ctx->fallback = NULL; ctx->fallback = NULL;
} }
} }
static int p8_aes_ctr_setkey(struct crypto_tfm *tfm, const u8 *key, static int p8_aes_ctr_setkey(struct crypto_tfm *tfm, const u8 *key,
unsigned int keylen) unsigned int keylen)
{ {
int ret; int ret;
struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key); ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
pagefault_enable(); pagefault_enable();
ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen); ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen);
return ret; return ret;
} }
static void p8_aes_ctr_final(struct p8_aes_ctr_ctx *ctx, static void p8_aes_ctr_final(struct p8_aes_ctr_ctx *ctx,
struct blkcipher_walk *walk) struct blkcipher_walk *walk)
{ {
u8 *ctrblk = walk->iv; u8 *ctrblk = walk->iv;
u8 keystream[AES_BLOCK_SIZE]; u8 keystream[AES_BLOCK_SIZE];
u8 *src = walk->src.virt.addr; u8 *src = walk->src.virt.addr;
u8 *dst = walk->dst.virt.addr; u8 *dst = walk->dst.virt.addr;
unsigned int nbytes = walk->nbytes; unsigned int nbytes = walk->nbytes;
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
aes_p8_encrypt(ctrblk, keystream, &ctx->enc_key); aes_p8_encrypt(ctrblk, keystream, &ctx->enc_key);
pagefault_enable(); pagefault_enable();
crypto_xor(keystream, src, nbytes); crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes); memcpy(dst, keystream, nbytes);
crypto_inc(ctrblk, AES_BLOCK_SIZE); crypto_inc(ctrblk, AES_BLOCK_SIZE);
} }
static int p8_aes_ctr_crypt(struct blkcipher_desc *desc, static int p8_aes_ctr_crypt(struct blkcipher_desc *desc,
struct scatterlist *dst, struct scatterlist *src, struct scatterlist *dst,
unsigned int nbytes) struct scatterlist *src, unsigned int nbytes)
{ {
int ret; int ret;
struct blkcipher_walk walk; struct blkcipher_walk walk;
struct p8_aes_ctr_ctx *ctx = crypto_tfm_ctx( struct p8_aes_ctr_ctx *ctx =
crypto_blkcipher_tfm(desc->tfm)); crypto_tfm_ctx(crypto_blkcipher_tfm(desc->tfm));
struct blkcipher_desc fallback_desc = { struct blkcipher_desc fallback_desc = {
.tfm = ctx->fallback, .tfm = ctx->fallback,
.info = desc->info, .info = desc->info,
.flags = desc->flags .flags = desc->flags
}; };
if (in_interrupt()) { if (in_interrupt()) {
ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src, nbytes); ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src,
} else { nbytes);
blkcipher_walk_init(&walk, dst, src, nbytes); } else {
ret = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE); blkcipher_walk_init(&walk, dst, src, nbytes);
while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) { ret = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
pagefault_disable(); while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
enable_kernel_altivec(); pagefault_disable();
aes_p8_ctr32_encrypt_blocks(walk.src.virt.addr, walk.dst.virt.addr, enable_kernel_altivec();
(nbytes & AES_BLOCK_MASK)/AES_BLOCK_SIZE, &ctx->enc_key, walk.iv); aes_p8_ctr32_encrypt_blocks(walk.src.virt.addr,
pagefault_enable(); walk.dst.virt.addr,
(nbytes &
crypto_inc(walk.iv, AES_BLOCK_SIZE); AES_BLOCK_MASK) /
nbytes &= AES_BLOCK_SIZE - 1; AES_BLOCK_SIZE,
ret = blkcipher_walk_done(desc, &walk, nbytes); &ctx->enc_key,
} walk.iv);
if (walk.nbytes) { pagefault_enable();
p8_aes_ctr_final(ctx, &walk);
ret = blkcipher_walk_done(desc, &walk, 0); crypto_inc(walk.iv, AES_BLOCK_SIZE);
} nbytes &= AES_BLOCK_SIZE - 1;
} ret = blkcipher_walk_done(desc, &walk, nbytes);
}
return ret; if (walk.nbytes) {
p8_aes_ctr_final(ctx, &walk);
ret = blkcipher_walk_done(desc, &walk, 0);
}
}
return ret;
} }
struct crypto_alg p8_aes_ctr_alg = { struct crypto_alg p8_aes_ctr_alg = {
.cra_name = "ctr(aes)", .cra_name = "ctr(aes)",
.cra_driver_name = "p8_aes_ctr", .cra_driver_name = "p8_aes_ctr",
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
.cra_priority = 1000, .cra_priority = 1000,
.cra_type = &crypto_blkcipher_type, .cra_type = &crypto_blkcipher_type,
.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK, .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = 0, .cra_alignmask = 0,
.cra_blocksize = 1, .cra_blocksize = 1,
.cra_ctxsize = sizeof(struct p8_aes_ctr_ctx), .cra_ctxsize = sizeof(struct p8_aes_ctr_ctx),
.cra_init = p8_aes_ctr_init, .cra_init = p8_aes_ctr_init,
.cra_exit = p8_aes_ctr_exit, .cra_exit = p8_aes_ctr_exit,
.cra_blkcipher = { .cra_blkcipher = {
.ivsize = 0, .ivsize = 0,
.min_keysize = AES_MIN_KEY_SIZE, .min_keysize = AES_MIN_KEY_SIZE,
.max_keysize = AES_MAX_KEY_SIZE, .max_keysize = AES_MAX_KEY_SIZE,
.setkey = p8_aes_ctr_setkey, .setkey = p8_aes_ctr_setkey,
.encrypt = p8_aes_ctr_crypt, .encrypt = p8_aes_ctr_crypt,
.decrypt = p8_aes_ctr_crypt, .decrypt = p8_aes_ctr_crypt,
}, },
}; };
...@@ -4,17 +4,18 @@ ...@@ -4,17 +4,18 @@
#define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1)) #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1))
struct aes_key { struct aes_key {
u8 key[AES_MAX_KEYLENGTH]; u8 key[AES_MAX_KEYLENGTH];
int rounds; int rounds;
}; };
int aes_p8_set_encrypt_key(const u8 *userKey, const int bits, int aes_p8_set_encrypt_key(const u8 *userKey, const int bits,
struct aes_key *key); struct aes_key *key);
int aes_p8_set_decrypt_key(const u8 *userKey, const int bits, int aes_p8_set_decrypt_key(const u8 *userKey, const int bits,
struct aes_key *key); struct aes_key *key);
void aes_p8_encrypt(const u8 *in, u8 *out, const struct aes_key *key); void aes_p8_encrypt(const u8 *in, u8 *out, const struct aes_key *key);
void aes_p8_decrypt(const u8 *in, u8 *out,const struct aes_key *key); void aes_p8_decrypt(const u8 *in, u8 *out, const struct aes_key *key);
void aes_p8_cbc_encrypt(const u8 *in, u8 *out, size_t len, void aes_p8_cbc_encrypt(const u8 *in, u8 *out, size_t len,
const struct aes_key *key, u8 *iv, const int enc); const struct aes_key *key, u8 *iv, const int enc);
void aes_p8_ctr32_encrypt_blocks(const u8 *in, u8 *out, void aes_p8_ctr32_encrypt_blocks(const u8 *in, u8 *out,
size_t len, const struct aes_key *key, const u8 *iv); size_t len, const struct aes_key *key,
const u8 *iv);
...@@ -39,176 +39,180 @@ ...@@ -39,176 +39,180 @@
void gcm_init_p8(u128 htable[16], const u64 Xi[2]); void gcm_init_p8(u128 htable[16], const u64 Xi[2]);
void gcm_gmult_p8(u64 Xi[2], const u128 htable[16]); void gcm_gmult_p8(u64 Xi[2], const u128 htable[16]);
void gcm_ghash_p8(u64 Xi[2], const u128 htable[16], void gcm_ghash_p8(u64 Xi[2], const u128 htable[16],
const u8 *in,size_t len); const u8 *in, size_t len);
struct p8_ghash_ctx { struct p8_ghash_ctx {
u128 htable[16]; u128 htable[16];
struct crypto_shash *fallback; struct crypto_shash *fallback;
}; };
struct p8_ghash_desc_ctx { struct p8_ghash_desc_ctx {
u64 shash[2]; u64 shash[2];
u8 buffer[GHASH_DIGEST_SIZE]; u8 buffer[GHASH_DIGEST_SIZE];
int bytes; int bytes;
struct shash_desc fallback_desc; struct shash_desc fallback_desc;
}; };
static int p8_ghash_init_tfm(struct crypto_tfm *tfm) static int p8_ghash_init_tfm(struct crypto_tfm *tfm)
{ {
const char *alg; const char *alg;
struct crypto_shash *fallback; struct crypto_shash *fallback;
struct crypto_shash *shash_tfm = __crypto_shash_cast(tfm); struct crypto_shash *shash_tfm = __crypto_shash_cast(tfm);
struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm);
if (!(alg = crypto_tfm_alg_name(tfm))) { if (!(alg = crypto_tfm_alg_name(tfm))) {
printk(KERN_ERR "Failed to get algorithm name.\n"); printk(KERN_ERR "Failed to get algorithm name.\n");
return -ENOENT; return -ENOENT;
} }
fallback = crypto_alloc_shash(alg, 0 ,CRYPTO_ALG_NEED_FALLBACK); fallback = crypto_alloc_shash(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(fallback)) { if (IS_ERR(fallback)) {
printk(KERN_ERR "Failed to allocate transformation for '%s': %ld\n", printk(KERN_ERR
alg, PTR_ERR(fallback)); "Failed to allocate transformation for '%s': %ld\n",
return PTR_ERR(fallback); alg, PTR_ERR(fallback));
} return PTR_ERR(fallback);
printk(KERN_INFO "Using '%s' as fallback implementation.\n", }
crypto_tfm_alg_driver_name(crypto_shash_tfm(fallback))); printk(KERN_INFO "Using '%s' as fallback implementation.\n",
crypto_tfm_alg_driver_name(crypto_shash_tfm(fallback)));
crypto_shash_set_flags(fallback,
crypto_shash_get_flags((struct crypto_shash *) tfm)); crypto_shash_set_flags(fallback,
ctx->fallback = fallback; crypto_shash_get_flags((struct crypto_shash
*) tfm));
shash_tfm->descsize = sizeof(struct p8_ghash_desc_ctx) ctx->fallback = fallback;
+ crypto_shash_descsize(fallback);
shash_tfm->descsize = sizeof(struct p8_ghash_desc_ctx)
return 0; + crypto_shash_descsize(fallback);
return 0;
} }
static void p8_ghash_exit_tfm(struct crypto_tfm *tfm) static void p8_ghash_exit_tfm(struct crypto_tfm *tfm)
{ {
struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm); struct p8_ghash_ctx *ctx = crypto_tfm_ctx(tfm);
if (ctx->fallback) { if (ctx->fallback) {
crypto_free_shash(ctx->fallback); crypto_free_shash(ctx->fallback);
ctx->fallback = NULL; ctx->fallback = NULL;
} }
} }
static int p8_ghash_init(struct shash_desc *desc) static int p8_ghash_init(struct shash_desc *desc)
{ {
struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
dctx->bytes = 0; dctx->bytes = 0;
memset(dctx->shash, 0, GHASH_DIGEST_SIZE); memset(dctx->shash, 0, GHASH_DIGEST_SIZE);
dctx->fallback_desc.tfm = ctx->fallback; dctx->fallback_desc.tfm = ctx->fallback;
dctx->fallback_desc.flags = desc->flags; dctx->fallback_desc.flags = desc->flags;
return crypto_shash_init(&dctx->fallback_desc); return crypto_shash_init(&dctx->fallback_desc);
} }
static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key, static int p8_ghash_setkey(struct crypto_shash *tfm, const u8 *key,
unsigned int keylen) unsigned int keylen)
{ {
struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm)); struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(tfm));
if (keylen != GHASH_KEY_LEN) if (keylen != GHASH_KEY_LEN)
return -EINVAL; return -EINVAL;
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
enable_kernel_fp(); enable_kernel_fp();
gcm_init_p8(ctx->htable, (const u64 *) key); gcm_init_p8(ctx->htable, (const u64 *) key);
pagefault_enable(); pagefault_enable();
return crypto_shash_setkey(ctx->fallback, key, keylen); return crypto_shash_setkey(ctx->fallback, key, keylen);
} }
static int p8_ghash_update(struct shash_desc *desc, static int p8_ghash_update(struct shash_desc *desc,
const u8 *src, unsigned int srclen) const u8 *src, unsigned int srclen)
{ {
unsigned int len; unsigned int len;
struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
if (IN_INTERRUPT) { if (IN_INTERRUPT) {
return crypto_shash_update(&dctx->fallback_desc, src, srclen); return crypto_shash_update(&dctx->fallback_desc, src,
} else { srclen);
if (dctx->bytes) { } else {
if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) { if (dctx->bytes) {
memcpy(dctx->buffer + dctx->bytes, src, srclen); if (dctx->bytes + srclen < GHASH_DIGEST_SIZE) {
dctx->bytes += srclen; memcpy(dctx->buffer + dctx->bytes, src,
return 0; srclen);
} dctx->bytes += srclen;
memcpy(dctx->buffer + dctx->bytes, src, return 0;
GHASH_DIGEST_SIZE - dctx->bytes); }
pagefault_disable(); memcpy(dctx->buffer + dctx->bytes, src,
enable_kernel_altivec(); GHASH_DIGEST_SIZE - dctx->bytes);
enable_kernel_fp(); pagefault_disable();
gcm_ghash_p8(dctx->shash, ctx->htable, dctx->buffer, enable_kernel_altivec();
GHASH_DIGEST_SIZE); enable_kernel_fp();
pagefault_enable(); gcm_ghash_p8(dctx->shash, ctx->htable,
src += GHASH_DIGEST_SIZE - dctx->bytes; dctx->buffer, GHASH_DIGEST_SIZE);
srclen -= GHASH_DIGEST_SIZE - dctx->bytes; pagefault_enable();
dctx->bytes = 0; src += GHASH_DIGEST_SIZE - dctx->bytes;
} srclen -= GHASH_DIGEST_SIZE - dctx->bytes;
len = srclen & ~(GHASH_DIGEST_SIZE - 1); dctx->bytes = 0;
if (len) { }
pagefault_disable(); len = srclen & ~(GHASH_DIGEST_SIZE - 1);
enable_kernel_altivec(); if (len) {
enable_kernel_fp(); pagefault_disable();
gcm_ghash_p8(dctx->shash, ctx->htable, src, len); enable_kernel_altivec();
pagefault_enable(); enable_kernel_fp();
src += len; gcm_ghash_p8(dctx->shash, ctx->htable, src, len);
srclen -= len; pagefault_enable();
} src += len;
if (srclen) { srclen -= len;
memcpy(dctx->buffer, src, srclen); }
dctx->bytes = srclen; if (srclen) {
} memcpy(dctx->buffer, src, srclen);
return 0; dctx->bytes = srclen;
} }
return 0;
}
} }
static int p8_ghash_final(struct shash_desc *desc, u8 *out) static int p8_ghash_final(struct shash_desc *desc, u8 *out)
{ {
int i; int i;
struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm)); struct p8_ghash_ctx *ctx = crypto_tfm_ctx(crypto_shash_tfm(desc->tfm));
struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc); struct p8_ghash_desc_ctx *dctx = shash_desc_ctx(desc);
if (IN_INTERRUPT) { if (IN_INTERRUPT) {
return crypto_shash_final(&dctx->fallback_desc, out); return crypto_shash_final(&dctx->fallback_desc, out);
} else { } else {
if (dctx->bytes) { if (dctx->bytes) {
for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++) for (i = dctx->bytes; i < GHASH_DIGEST_SIZE; i++)
dctx->buffer[i] = 0; dctx->buffer[i] = 0;
pagefault_disable(); pagefault_disable();
enable_kernel_altivec(); enable_kernel_altivec();
enable_kernel_fp(); enable_kernel_fp();
gcm_ghash_p8(dctx->shash, ctx->htable, dctx->buffer, gcm_ghash_p8(dctx->shash, ctx->htable,
GHASH_DIGEST_SIZE); dctx->buffer, GHASH_DIGEST_SIZE);
pagefault_enable(); pagefault_enable();
dctx->bytes = 0; dctx->bytes = 0;
} }
memcpy(out, dctx->shash, GHASH_DIGEST_SIZE); memcpy(out, dctx->shash, GHASH_DIGEST_SIZE);
return 0; return 0;
} }
} }
struct shash_alg p8_ghash_alg = { struct shash_alg p8_ghash_alg = {
.digestsize = GHASH_DIGEST_SIZE, .digestsize = GHASH_DIGEST_SIZE,
.init = p8_ghash_init, .init = p8_ghash_init,
.update = p8_ghash_update, .update = p8_ghash_update,
.final = p8_ghash_final, .final = p8_ghash_final,
.setkey = p8_ghash_setkey, .setkey = p8_ghash_setkey,
.descsize = sizeof(struct p8_ghash_desc_ctx), .descsize = sizeof(struct p8_ghash_desc_ctx),
.base = { .base = {
.cra_name = "ghash", .cra_name = "ghash",
.cra_driver_name = "p8_ghash", .cra_driver_name = "p8_ghash",
.cra_priority = 1000, .cra_priority = 1000,
.cra_flags = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_NEED_FALLBACK, .cra_flags = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = GHASH_BLOCK_SIZE, .cra_blocksize = GHASH_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct p8_ghash_ctx), .cra_ctxsize = sizeof(struct p8_ghash_ctx),
.cra_module = THIS_MODULE, .cra_module = THIS_MODULE,
.cra_init = p8_ghash_init_tfm, .cra_init = p8_ghash_init_tfm,
.cra_exit = p8_ghash_exit_tfm, .cra_exit = p8_ghash_exit_tfm,
}, },
}; };
...@@ -32,57 +32,57 @@ extern struct crypto_alg p8_aes_alg; ...@@ -32,57 +32,57 @@ extern struct crypto_alg p8_aes_alg;
extern struct crypto_alg p8_aes_cbc_alg; extern struct crypto_alg p8_aes_cbc_alg;
extern struct crypto_alg p8_aes_ctr_alg; extern struct crypto_alg p8_aes_ctr_alg;
static struct crypto_alg *algs[] = { static struct crypto_alg *algs[] = {
&p8_aes_alg, &p8_aes_alg,
&p8_aes_cbc_alg, &p8_aes_cbc_alg,
&p8_aes_ctr_alg, &p8_aes_ctr_alg,
NULL, NULL,
}; };
int __init p8_init(void) int __init p8_init(void)
{ {
int ret = 0; int ret = 0;
struct crypto_alg **alg_it; struct crypto_alg **alg_it;
if (!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_VEC_CRYPTO)) if (!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_VEC_CRYPTO))
return -ENODEV; return -ENODEV;
for (alg_it = algs; *alg_it; alg_it++) { for (alg_it = algs; *alg_it; alg_it++) {
ret = crypto_register_alg(*alg_it); ret = crypto_register_alg(*alg_it);
printk(KERN_INFO "crypto_register_alg '%s' = %d\n", printk(KERN_INFO "crypto_register_alg '%s' = %d\n",
(*alg_it)->cra_name, ret); (*alg_it)->cra_name, ret);
if (ret) { if (ret) {
for (alg_it--; alg_it >= algs; alg_it--) for (alg_it--; alg_it >= algs; alg_it--)
crypto_unregister_alg(*alg_it); crypto_unregister_alg(*alg_it);
break; break;
} }
} }
if (ret) if (ret)
return ret; return ret;
ret = crypto_register_shash(&p8_ghash_alg); ret = crypto_register_shash(&p8_ghash_alg);
if (ret) { if (ret) {
for (alg_it = algs; *alg_it; alg_it++) for (alg_it = algs; *alg_it; alg_it++)
crypto_unregister_alg(*alg_it); crypto_unregister_alg(*alg_it);
} }
return ret; return ret;
} }
void __exit p8_exit(void) void __exit p8_exit(void)
{ {
struct crypto_alg **alg_it; struct crypto_alg **alg_it;
for (alg_it = algs; *alg_it; alg_it++) { for (alg_it = algs; *alg_it; alg_it++) {
printk(KERN_INFO "Removing '%s'\n", (*alg_it)->cra_name); printk(KERN_INFO "Removing '%s'\n", (*alg_it)->cra_name);
crypto_unregister_alg(*alg_it); crypto_unregister_alg(*alg_it);
} }
crypto_unregister_shash(&p8_ghash_alg); crypto_unregister_shash(&p8_ghash_alg);
} }
module_init(p8_init); module_init(p8_init);
module_exit(p8_exit); module_exit(p8_exit);
MODULE_AUTHOR("Marcelo Cerri<mhcerri@br.ibm.com>"); MODULE_AUTHOR("Marcelo Cerri<mhcerri@br.ibm.com>");
MODULE_DESCRIPTION("IBM VMX cryptographic acceleration instructions support on Power 8"); MODULE_DESCRIPTION("IBM VMX cryptographic acceleration instructions "
"support on Power 8");
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.0"); MODULE_VERSION("1.0.0");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment