1/**
2 * AES CBC routines supporting VMX instructions on the Power 8
3 *
4 * Copyright (C) 2015 International Business Machines Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 only.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
20 */
21
22#include <linux/types.h>
23#include <linux/err.h>
24#include <linux/crypto.h>
25#include <linux/delay.h>
26#include <linux/hardirq.h>
27#include <asm/switch_to.h>
28#include <crypto/aes.h>
29#include <crypto/scatterwalk.h>
30
31#include "aesp8-ppc.h"
32
33struct p8_aes_cbc_ctx {
34    struct crypto_blkcipher *fallback;
35    struct aes_key enc_key;
36    struct aes_key dec_key;
37};
38
39static int p8_aes_cbc_init(struct crypto_tfm *tfm)
40{
41    const char *alg;
42    struct crypto_blkcipher *fallback;
43    struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
44
45    if (!(alg = crypto_tfm_alg_name(tfm))) {
46        printk(KERN_ERR "Failed to get algorithm name.\n");
47        return -ENOENT;
48    }
49
50    fallback = crypto_alloc_blkcipher(alg, 0 ,CRYPTO_ALG_NEED_FALLBACK);
51    if (IS_ERR(fallback)) {
52        printk(KERN_ERR "Failed to allocate transformation for '%s': %ld\n",
53                alg, PTR_ERR(fallback));
54        return PTR_ERR(fallback);
55    }
56    printk(KERN_INFO "Using '%s' as fallback implementation.\n",
57            crypto_tfm_alg_driver_name((struct crypto_tfm *) fallback));
58
59    crypto_blkcipher_set_flags(fallback,
60            crypto_blkcipher_get_flags((struct crypto_blkcipher *) tfm));
61    ctx->fallback = fallback;
62
63    return 0;
64}
65
66static void p8_aes_cbc_exit(struct crypto_tfm *tfm)
67{
68    struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
69
70    if (ctx->fallback) {
71        crypto_free_blkcipher(ctx->fallback);
72        ctx->fallback = NULL;
73    }
74}
75
76static int p8_aes_cbc_setkey(struct crypto_tfm *tfm, const u8 *key,
77    unsigned int keylen)
78{
79    int ret;
80    struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
81
82    pagefault_disable();
83    enable_kernel_altivec();
84    enable_kernel_vsx();
85    ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
86    ret += aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
87    pagefault_enable();
88
89    ret += crypto_blkcipher_setkey(ctx->fallback, key, keylen);
90    return ret;
91}
92
93static int p8_aes_cbc_encrypt(struct blkcipher_desc *desc,
94    struct scatterlist *dst, struct scatterlist *src,
95    unsigned int nbytes)
96{
97    int ret;
98    struct blkcipher_walk walk;
99    struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(
100            crypto_blkcipher_tfm(desc->tfm));
101    struct blkcipher_desc fallback_desc = {
102        .tfm = ctx->fallback,
103        .info = desc->info,
104        .flags = desc->flags
105    };
106
107    if (in_interrupt()) {
108        ret = crypto_blkcipher_encrypt(&fallback_desc, dst, src, nbytes);
109    } else {
110        pagefault_disable();
111        enable_kernel_altivec();
112        enable_kernel_vsx();
113
114	blkcipher_walk_init(&walk, dst, src, nbytes);
115        ret = blkcipher_walk_virt(desc, &walk);
116        while ((nbytes = walk.nbytes)) {
117			aes_p8_cbc_encrypt(walk.src.virt.addr, walk.dst.virt.addr,
118				nbytes & AES_BLOCK_MASK, &ctx->enc_key, walk.iv, 1);
119			nbytes &= AES_BLOCK_SIZE - 1;
120			ret = blkcipher_walk_done(desc, &walk, nbytes);
121	}
122
123        pagefault_enable();
124    }
125
126    return ret;
127}
128
129static int p8_aes_cbc_decrypt(struct blkcipher_desc *desc,
130    struct scatterlist *dst, struct scatterlist *src,
131    unsigned int nbytes)
132{
133    int ret;
134    struct blkcipher_walk walk;
135    struct p8_aes_cbc_ctx *ctx = crypto_tfm_ctx(
136            crypto_blkcipher_tfm(desc->tfm));
137    struct blkcipher_desc fallback_desc = {
138        .tfm = ctx->fallback,
139        .info = desc->info,
140        .flags = desc->flags
141    };
142
143    if (in_interrupt()) {
144        ret = crypto_blkcipher_decrypt(&fallback_desc, dst, src, nbytes);
145    } else {
146        pagefault_disable();
147        enable_kernel_altivec();
148        enable_kernel_vsx();
149
150	blkcipher_walk_init(&walk, dst, src, nbytes);
151        ret = blkcipher_walk_virt(desc, &walk);
152        while ((nbytes = walk.nbytes)) {
153			aes_p8_cbc_encrypt(walk.src.virt.addr, walk.dst.virt.addr,
154				nbytes & AES_BLOCK_MASK, &ctx->dec_key, walk.iv, 0);
155			nbytes &= AES_BLOCK_SIZE - 1;
156			ret = blkcipher_walk_done(desc, &walk, nbytes);
157		}
158
159        pagefault_enable();
160    }
161
162    return ret;
163}
164
165
166struct crypto_alg p8_aes_cbc_alg = {
167    .cra_name = "cbc(aes)",
168    .cra_driver_name = "p8_aes_cbc",
169    .cra_module = THIS_MODULE,
170    .cra_priority = 1000,
171    .cra_type = &crypto_blkcipher_type,
172    .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER | CRYPTO_ALG_NEED_FALLBACK,
173    .cra_alignmask = 0,
174    .cra_blocksize = AES_BLOCK_SIZE,
175    .cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
176    .cra_init = p8_aes_cbc_init,
177    .cra_exit = p8_aes_cbc_exit,
178    .cra_blkcipher = {
179        .ivsize = 0,
180        .min_keysize = AES_MIN_KEY_SIZE,
181        .max_keysize = AES_MAX_KEY_SIZE,
182        .setkey = p8_aes_cbc_setkey,
183        .encrypt = p8_aes_cbc_encrypt,
184        .decrypt = p8_aes_cbc_decrypt,
185    },
186};
187
188