blob: be87575270ff446b2eb64df0081b5d590a2453db [file] [log] [blame]
Jan Glauberbf754ae2006-01-06 00:19:18 -08001/*
2 * Cryptographic API.
3 *
4 * s390 implementation of the AES Cipher Algorithm.
5 *
6 * s390 Version:
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 2005, 2007
Jan Glauberbf754ae2006-01-06 00:19:18 -08008 * Author(s): Jan Glauber (jang@de.ibm.com)
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +11009 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
Jan Glauberbf754ae2006-01-06 00:19:18 -080010 *
Sebastian Siewiorf8246af2007-10-05 16:52:01 +080011 * Derived from "crypto/aes_generic.c"
Jan Glauberbf754ae2006-01-06 00:19:18 -080012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 */
19
Jan Glauber39f09392008-12-25 13:39:37 +010020#define KMSG_COMPONENT "aes_s390"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
Sebastian Siewior89e126542007-10-17 23:18:57 +080023#include <crypto/aes.h>
Herbert Xua9e62fa2006-08-21 21:39:24 +100024#include <crypto/algapi.h>
Herbert Xu64e26802016-06-29 18:04:07 +080025#include <crypto/internal/skcipher.h>
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110026#include <linux/err.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080027#include <linux/module.h>
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +010028#include <linux/cpufeature.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080029#include <linux/init.h>
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010030#include <linux/spinlock.h>
Stephan Mueller49abc0d2016-02-17 07:00:01 +010031#include <crypto/xts.h>
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +010032#include <asm/cpacf.h>
Jan Glauberbf754ae2006-01-06 00:19:18 -080033
Jan Glauber86aa9fc2007-02-05 21:18:14 +010034#define AES_KEYLEN_128 1
35#define AES_KEYLEN_192 2
36#define AES_KEYLEN_256 4
37
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100038static u8 *ctrblk;
Harald Freudenberger0519e9a2014-01-16 16:01:11 +010039static DEFINE_SPINLOCK(ctrblk_lock);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +100040static char keylen_flag;
Jan Glauberbf754ae2006-01-06 00:19:18 -080041
42struct s390_aes_ctx {
Jan Glauberbf754ae2006-01-06 00:19:18 -080043 u8 key[AES_MAX_KEY_SIZE];
44 int key_len;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020045 unsigned long fc;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110046 union {
Herbert Xu64e26802016-06-29 18:04:07 +080047 struct crypto_skcipher *blk;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110048 struct crypto_cipher *cip;
49 } fallback;
Jan Glauberbf754ae2006-01-06 00:19:18 -080050};
51
Gerald Schaefer99d97222011-04-26 16:12:42 +100052struct pcc_param {
53 u8 key[32];
54 u8 tweak[16];
55 u8 block[16];
56 u8 bit[16];
57 u8 xts[16];
58};
59
60struct s390_xts_ctx {
61 u8 key[32];
Gerald Schaefer9dda2762013-11-19 17:12:47 +010062 u8 pcc_key[32];
Gerald Schaefer99d97222011-04-26 16:12:42 +100063 int key_len;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +020064 unsigned long fc;
Herbert Xu64e26802016-06-29 18:04:07 +080065 struct crypto_skcipher *fallback;
Gerald Schaefer99d97222011-04-26 16:12:42 +100066};
67
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +110068/*
69 * Check if the key_len is supported by the HW.
70 * Returns 0 if it is, a positive number if it is not and software fallback is
71 * required or a negative number in case the key size is not valid
72 */
73static int need_fallback(unsigned int key_len)
74{
75 switch (key_len) {
76 case 16:
77 if (!(keylen_flag & AES_KEYLEN_128))
78 return 1;
79 break;
80 case 24:
81 if (!(keylen_flag & AES_KEYLEN_192))
82 return 1;
83 break;
84 case 32:
85 if (!(keylen_flag & AES_KEYLEN_256))
86 return 1;
87 break;
88 default:
89 return -1;
90 break;
91 }
92 return 0;
93}
94
95static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
96 unsigned int key_len)
97{
98 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
99 int ret;
100
Roel Kluind7ac7692010-01-08 14:18:34 +1100101 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
102 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100103 CRYPTO_TFM_REQ_MASK);
104
105 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
106 if (ret) {
107 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
Roel Kluind7ac7692010-01-08 14:18:34 +1100108 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100109 CRYPTO_TFM_RES_MASK);
110 }
111 return ret;
112}
113
Herbert Xu6c2bb982006-05-16 22:09:29 +1000114static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000115 unsigned int key_len)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800116{
Herbert Xu6c2bb982006-05-16 22:09:29 +1000117 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Herbert Xu560c06a2006-08-13 14:16:39 +1000118 u32 *flags = &tfm->crt_flags;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100119 int ret;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800120
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100121 ret = need_fallback(key_len);
122 if (ret < 0) {
123 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
124 return -EINVAL;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800125 }
126
127 sctx->key_len = key_len;
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100128 if (!ret) {
129 memcpy(sctx->key, in_key, key_len);
130 return 0;
131 }
132
133 return setkey_fallback_cip(tfm, in_key, key_len);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800134}
135
Herbert Xu6c2bb982006-05-16 22:09:29 +1000136static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800137{
Chen Gange6a67ad2015-01-01 22:56:02 +0800138 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800139
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100140 if (unlikely(need_fallback(sctx->key_len))) {
141 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
142 return;
143 }
144
Jan Glauberbf754ae2006-01-06 00:19:18 -0800145 switch (sctx->key_len) {
146 case 16:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200147 cpacf_km(CPACF_KM_AES_128,
148 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800149 break;
150 case 24:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200151 cpacf_km(CPACF_KM_AES_192,
152 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800153 break;
154 case 32:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200155 cpacf_km(CPACF_KM_AES_256,
156 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800157 break;
158 }
159}
160
Herbert Xu6c2bb982006-05-16 22:09:29 +1000161static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800162{
Chen Gange6a67ad2015-01-01 22:56:02 +0800163 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800164
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100165 if (unlikely(need_fallback(sctx->key_len))) {
166 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
167 return;
168 }
169
Jan Glauberbf754ae2006-01-06 00:19:18 -0800170 switch (sctx->key_len) {
171 case 16:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200172 cpacf_km(CPACF_KM_AES_128 | CPACF_DECRYPT,
173 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800174 break;
175 case 24:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200176 cpacf_km(CPACF_KM_AES_192 | CPACF_DECRYPT,
177 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800178 break;
179 case 32:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200180 cpacf_km(CPACF_KM_AES_256 | CPACF_DECRYPT,
181 &sctx->key, out, in, AES_BLOCK_SIZE);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800182 break;
183 }
184}
185
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100186static int fallback_init_cip(struct crypto_tfm *tfm)
187{
188 const char *name = tfm->__crt_alg->cra_name;
189 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
190
191 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
192 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
193
194 if (IS_ERR(sctx->fallback.cip)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100195 pr_err("Allocating AES fallback algorithm %s failed\n",
196 name);
Roel Kluinb59cdcb32009-12-18 17:43:18 +0100197 return PTR_ERR(sctx->fallback.cip);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100198 }
199
200 return 0;
201}
202
203static void fallback_exit_cip(struct crypto_tfm *tfm)
204{
205 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
206
207 crypto_free_cipher(sctx->fallback.cip);
208 sctx->fallback.cip = NULL;
209}
Jan Glauberbf754ae2006-01-06 00:19:18 -0800210
211static struct crypto_alg aes_alg = {
212 .cra_name = "aes",
Herbert Xu65b75c32006-08-21 21:18:50 +1000213 .cra_driver_name = "aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100214 .cra_priority = 300,
Jan Glauberf67d1362007-05-04 18:47:47 +0200215 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
216 CRYPTO_ALG_NEED_FALLBACK,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800217 .cra_blocksize = AES_BLOCK_SIZE,
218 .cra_ctxsize = sizeof(struct s390_aes_ctx),
219 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100220 .cra_init = fallback_init_cip,
221 .cra_exit = fallback_exit_cip,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800222 .cra_u = {
223 .cipher = {
224 .cia_min_keysize = AES_MIN_KEY_SIZE,
225 .cia_max_keysize = AES_MAX_KEY_SIZE,
226 .cia_setkey = aes_set_key,
227 .cia_encrypt = aes_encrypt,
228 .cia_decrypt = aes_decrypt,
Jan Glauberbf754ae2006-01-06 00:19:18 -0800229 }
230 }
231};
232
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100233static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
234 unsigned int len)
235{
236 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
237 unsigned int ret;
238
Herbert Xu64e26802016-06-29 18:04:07 +0800239 crypto_skcipher_clear_flags(sctx->fallback.blk, CRYPTO_TFM_REQ_MASK);
240 crypto_skcipher_set_flags(sctx->fallback.blk, tfm->crt_flags &
241 CRYPTO_TFM_REQ_MASK);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100242
Herbert Xu64e26802016-06-29 18:04:07 +0800243 ret = crypto_skcipher_setkey(sctx->fallback.blk, key, len);
244
245 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
246 tfm->crt_flags |= crypto_skcipher_get_flags(sctx->fallback.blk) &
247 CRYPTO_TFM_RES_MASK;
248
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100249 return ret;
250}
251
252static int fallback_blk_dec(struct blkcipher_desc *desc,
253 struct scatterlist *dst, struct scatterlist *src,
254 unsigned int nbytes)
255{
256 unsigned int ret;
Herbert Xu64e26802016-06-29 18:04:07 +0800257 struct crypto_blkcipher *tfm = desc->tfm;
258 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
259 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100260
Herbert Xu64e26802016-06-29 18:04:07 +0800261 skcipher_request_set_tfm(req, sctx->fallback.blk);
262 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
263 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100264
Herbert Xu64e26802016-06-29 18:04:07 +0800265 ret = crypto_skcipher_decrypt(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100266
Herbert Xu64e26802016-06-29 18:04:07 +0800267 skcipher_request_zero(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100268 return ret;
269}
270
271static int fallback_blk_enc(struct blkcipher_desc *desc,
272 struct scatterlist *dst, struct scatterlist *src,
273 unsigned int nbytes)
274{
275 unsigned int ret;
Herbert Xu64e26802016-06-29 18:04:07 +0800276 struct crypto_blkcipher *tfm = desc->tfm;
277 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
278 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100279
Herbert Xu64e26802016-06-29 18:04:07 +0800280 skcipher_request_set_tfm(req, sctx->fallback.blk);
281 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
282 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100283
Herbert Xu64e26802016-06-29 18:04:07 +0800284 ret = crypto_skcipher_encrypt(req);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100285 return ret;
286}
287
Herbert Xua9e62fa2006-08-21 21:39:24 +1000288static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
289 unsigned int key_len)
290{
291 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100292 int ret;
293
294 ret = need_fallback(key_len);
295 if (ret > 0) {
296 sctx->key_len = key_len;
297 return setkey_fallback_blk(tfm, in_key, key_len);
298 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000299
300 switch (key_len) {
301 case 16:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200302 sctx->fc = CPACF_KM_AES_128;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000303 break;
304 case 24:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200305 sctx->fc = CPACF_KM_AES_192;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000306 break;
307 case 32:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200308 sctx->fc = CPACF_KM_AES_256;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000309 break;
310 }
311
312 return aes_set_key(tfm, in_key, key_len);
313}
314
315static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
316 struct blkcipher_walk *walk)
317{
318 int ret = blkcipher_walk_virt(desc, walk);
319 unsigned int nbytes;
320
321 while ((nbytes = walk->nbytes)) {
322 /* only use complete blocks */
323 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
324 u8 *out = walk->dst.virt.addr;
325 u8 *in = walk->src.virt.addr;
326
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200327 cpacf_km(func, param, out, in, n);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000328
329 nbytes &= AES_BLOCK_SIZE - 1;
330 ret = blkcipher_walk_done(desc, walk, nbytes);
331 }
332
333 return ret;
334}
335
336static int ecb_aes_encrypt(struct blkcipher_desc *desc,
337 struct scatterlist *dst, struct scatterlist *src,
338 unsigned int nbytes)
339{
340 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
341 struct blkcipher_walk walk;
342
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100343 if (unlikely(need_fallback(sctx->key_len)))
344 return fallback_blk_enc(desc, dst, src, nbytes);
345
Herbert Xua9e62fa2006-08-21 21:39:24 +1000346 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200347 return ecb_aes_crypt(desc, sctx->fc, sctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000348}
349
350static int ecb_aes_decrypt(struct blkcipher_desc *desc,
351 struct scatterlist *dst, struct scatterlist *src,
352 unsigned int nbytes)
353{
354 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
355 struct blkcipher_walk walk;
356
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100357 if (unlikely(need_fallback(sctx->key_len)))
358 return fallback_blk_dec(desc, dst, src, nbytes);
359
Herbert Xua9e62fa2006-08-21 21:39:24 +1000360 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200361 return ecb_aes_crypt(desc, sctx->fc | CPACF_DECRYPT, sctx->key, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000362}
363
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100364static int fallback_init_blk(struct crypto_tfm *tfm)
365{
366 const char *name = tfm->__crt_alg->cra_name;
367 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
368
Herbert Xu64e26802016-06-29 18:04:07 +0800369 sctx->fallback.blk = crypto_alloc_skcipher(name, 0,
370 CRYPTO_ALG_ASYNC |
371 CRYPTO_ALG_NEED_FALLBACK);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100372
373 if (IS_ERR(sctx->fallback.blk)) {
Jan Glauber39f09392008-12-25 13:39:37 +0100374 pr_err("Allocating AES fallback algorithm %s failed\n",
375 name);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100376 return PTR_ERR(sctx->fallback.blk);
377 }
378
379 return 0;
380}
381
382static void fallback_exit_blk(struct crypto_tfm *tfm)
383{
384 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
385
Herbert Xu64e26802016-06-29 18:04:07 +0800386 crypto_free_skcipher(sctx->fallback.blk);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100387}
388
Herbert Xua9e62fa2006-08-21 21:39:24 +1000389static struct crypto_alg ecb_aes_alg = {
390 .cra_name = "ecb(aes)",
391 .cra_driver_name = "ecb-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100392 .cra_priority = 400, /* combo: aes + ecb */
Jan Glauberf67d1362007-05-04 18:47:47 +0200393 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
394 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000395 .cra_blocksize = AES_BLOCK_SIZE,
396 .cra_ctxsize = sizeof(struct s390_aes_ctx),
397 .cra_type = &crypto_blkcipher_type,
398 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100399 .cra_init = fallback_init_blk,
400 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000401 .cra_u = {
402 .blkcipher = {
403 .min_keysize = AES_MIN_KEY_SIZE,
404 .max_keysize = AES_MAX_KEY_SIZE,
405 .setkey = ecb_aes_set_key,
406 .encrypt = ecb_aes_encrypt,
407 .decrypt = ecb_aes_decrypt,
408 }
409 }
410};
411
412static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
413 unsigned int key_len)
414{
415 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100416 int ret;
417
418 ret = need_fallback(key_len);
419 if (ret > 0) {
420 sctx->key_len = key_len;
421 return setkey_fallback_blk(tfm, in_key, key_len);
422 }
Herbert Xua9e62fa2006-08-21 21:39:24 +1000423
424 switch (key_len) {
425 case 16:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200426 sctx->fc = CPACF_KMC_AES_128;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000427 break;
428 case 24:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200429 sctx->fc = CPACF_KMC_AES_192;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000430 break;
431 case 32:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200432 sctx->fc = CPACF_KMC_AES_256;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000433 break;
434 }
435
436 return aes_set_key(tfm, in_key, key_len);
437}
438
Herbert Xuf262f0f2013-11-05 19:36:27 +0800439static int cbc_aes_crypt(struct blkcipher_desc *desc, long func,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000440 struct blkcipher_walk *walk)
441{
Herbert Xuf262f0f2013-11-05 19:36:27 +0800442 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000443 int ret = blkcipher_walk_virt(desc, walk);
444 unsigned int nbytes = walk->nbytes;
Herbert Xuf262f0f2013-11-05 19:36:27 +0800445 struct {
446 u8 iv[AES_BLOCK_SIZE];
447 u8 key[AES_MAX_KEY_SIZE];
448 } param;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000449
450 if (!nbytes)
451 goto out;
452
Herbert Xuf262f0f2013-11-05 19:36:27 +0800453 memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
454 memcpy(param.key, sctx->key, sctx->key_len);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000455 do {
456 /* only use complete blocks */
457 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
458 u8 *out = walk->dst.virt.addr;
459 u8 *in = walk->src.virt.addr;
460
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200461 cpacf_kmc(func, &param, out, in, n);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000462
463 nbytes &= AES_BLOCK_SIZE - 1;
464 ret = blkcipher_walk_done(desc, walk, nbytes);
465 } while ((nbytes = walk->nbytes));
Herbert Xuf262f0f2013-11-05 19:36:27 +0800466 memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000467
468out:
469 return ret;
470}
471
472static int cbc_aes_encrypt(struct blkcipher_desc *desc,
473 struct scatterlist *dst, struct scatterlist *src,
474 unsigned int nbytes)
475{
476 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
477 struct blkcipher_walk walk;
478
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100479 if (unlikely(need_fallback(sctx->key_len)))
480 return fallback_blk_enc(desc, dst, src, nbytes);
481
Herbert Xua9e62fa2006-08-21 21:39:24 +1000482 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200483 return cbc_aes_crypt(desc, sctx->fc, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000484}
485
486static int cbc_aes_decrypt(struct blkcipher_desc *desc,
487 struct scatterlist *dst, struct scatterlist *src,
488 unsigned int nbytes)
489{
490 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
491 struct blkcipher_walk walk;
492
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100493 if (unlikely(need_fallback(sctx->key_len)))
494 return fallback_blk_dec(desc, dst, src, nbytes);
495
Herbert Xua9e62fa2006-08-21 21:39:24 +1000496 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200497 return cbc_aes_crypt(desc, sctx->fc | CPACF_DECRYPT, &walk);
Herbert Xua9e62fa2006-08-21 21:39:24 +1000498}
499
500static struct crypto_alg cbc_aes_alg = {
501 .cra_name = "cbc(aes)",
502 .cra_driver_name = "cbc-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100503 .cra_priority = 400, /* combo: aes + cbc */
Jan Glauberf67d1362007-05-04 18:47:47 +0200504 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
505 CRYPTO_ALG_NEED_FALLBACK,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000506 .cra_blocksize = AES_BLOCK_SIZE,
507 .cra_ctxsize = sizeof(struct s390_aes_ctx),
508 .cra_type = &crypto_blkcipher_type,
509 .cra_module = THIS_MODULE,
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100510 .cra_init = fallback_init_blk,
511 .cra_exit = fallback_exit_blk,
Herbert Xua9e62fa2006-08-21 21:39:24 +1000512 .cra_u = {
513 .blkcipher = {
514 .min_keysize = AES_MIN_KEY_SIZE,
515 .max_keysize = AES_MAX_KEY_SIZE,
516 .ivsize = AES_BLOCK_SIZE,
517 .setkey = cbc_aes_set_key,
518 .encrypt = cbc_aes_encrypt,
519 .decrypt = cbc_aes_decrypt,
520 }
521 }
522};
523
Gerald Schaefer99d97222011-04-26 16:12:42 +1000524static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
525 unsigned int len)
526{
527 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
528 unsigned int ret;
529
Herbert Xu64e26802016-06-29 18:04:07 +0800530 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
531 crypto_skcipher_set_flags(xts_ctx->fallback, tfm->crt_flags &
532 CRYPTO_TFM_REQ_MASK);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000533
Herbert Xu64e26802016-06-29 18:04:07 +0800534 ret = crypto_skcipher_setkey(xts_ctx->fallback, key, len);
535
536 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
537 tfm->crt_flags |= crypto_skcipher_get_flags(xts_ctx->fallback) &
538 CRYPTO_TFM_RES_MASK;
539
Gerald Schaefer99d97222011-04-26 16:12:42 +1000540 return ret;
541}
542
543static int xts_fallback_decrypt(struct blkcipher_desc *desc,
544 struct scatterlist *dst, struct scatterlist *src,
545 unsigned int nbytes)
546{
Herbert Xu64e26802016-06-29 18:04:07 +0800547 struct crypto_blkcipher *tfm = desc->tfm;
548 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
549 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000550 unsigned int ret;
551
Herbert Xu64e26802016-06-29 18:04:07 +0800552 skcipher_request_set_tfm(req, xts_ctx->fallback);
553 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
554 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000555
Herbert Xu64e26802016-06-29 18:04:07 +0800556 ret = crypto_skcipher_decrypt(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000557
Herbert Xu64e26802016-06-29 18:04:07 +0800558 skcipher_request_zero(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000559 return ret;
560}
561
562static int xts_fallback_encrypt(struct blkcipher_desc *desc,
563 struct scatterlist *dst, struct scatterlist *src,
564 unsigned int nbytes)
565{
Herbert Xu64e26802016-06-29 18:04:07 +0800566 struct crypto_blkcipher *tfm = desc->tfm;
567 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
568 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000569 unsigned int ret;
570
Herbert Xu64e26802016-06-29 18:04:07 +0800571 skcipher_request_set_tfm(req, xts_ctx->fallback);
572 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
573 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000574
Herbert Xu64e26802016-06-29 18:04:07 +0800575 ret = crypto_skcipher_encrypt(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000576
Herbert Xu64e26802016-06-29 18:04:07 +0800577 skcipher_request_zero(req);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000578 return ret;
579}
580
581static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
582 unsigned int key_len)
583{
584 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
585 u32 *flags = &tfm->crt_flags;
Stephan Mueller28856a92016-02-09 15:37:47 +0100586 int err;
587
588 err = xts_check_key(tfm, in_key, key_len);
589 if (err)
590 return err;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000591
592 switch (key_len) {
593 case 32:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200594 xts_ctx->fc = CPACF_KM_XTS_128;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000595 memcpy(xts_ctx->key + 16, in_key, 16);
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100596 memcpy(xts_ctx->pcc_key + 16, in_key + 16, 16);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000597 break;
598 case 48:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200599 xts_ctx->fc = 0;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000600 xts_fallback_setkey(tfm, in_key, key_len);
601 break;
602 case 64:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200603 xts_ctx->fc = CPACF_KM_XTS_256;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000604 memcpy(xts_ctx->key, in_key, 32);
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100605 memcpy(xts_ctx->pcc_key, in_key + 32, 32);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000606 break;
607 default:
608 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
609 return -EINVAL;
610 }
611 xts_ctx->key_len = key_len;
612 return 0;
613}
614
615static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
616 struct s390_xts_ctx *xts_ctx,
617 struct blkcipher_walk *walk)
618{
619 unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
620 int ret = blkcipher_walk_virt(desc, walk);
621 unsigned int nbytes = walk->nbytes;
622 unsigned int n;
623 u8 *in, *out;
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100624 struct pcc_param pcc_param;
625 struct {
626 u8 key[32];
627 u8 init[16];
628 } xts_param;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000629
630 if (!nbytes)
631 goto out;
632
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100633 memset(pcc_param.block, 0, sizeof(pcc_param.block));
634 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
635 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
636 memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
637 memcpy(pcc_param.key, xts_ctx->pcc_key, 32);
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100638 /* remove decipher modifier bit from 'func' and call PCC */
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200639 cpacf_pcc(func & 0x7f, &pcc_param.key[offset]);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000640
Gerald Schaefer9dda2762013-11-19 17:12:47 +0100641 memcpy(xts_param.key, xts_ctx->key, 32);
642 memcpy(xts_param.init, pcc_param.xts, 16);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000643 do {
644 /* only use complete blocks */
645 n = nbytes & ~(AES_BLOCK_SIZE - 1);
646 out = walk->dst.virt.addr;
647 in = walk->src.virt.addr;
648
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200649 cpacf_km(func, &xts_param.key[offset], out, in, n);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000650
651 nbytes &= AES_BLOCK_SIZE - 1;
652 ret = blkcipher_walk_done(desc, walk, nbytes);
653 } while ((nbytes = walk->nbytes));
654out:
655 return ret;
656}
657
658static int xts_aes_encrypt(struct blkcipher_desc *desc,
659 struct scatterlist *dst, struct scatterlist *src,
660 unsigned int nbytes)
661{
662 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
663 struct blkcipher_walk walk;
664
665 if (unlikely(xts_ctx->key_len == 48))
666 return xts_fallback_encrypt(desc, dst, src, nbytes);
667
668 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200669 return xts_aes_crypt(desc, xts_ctx->fc, xts_ctx, &walk);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000670}
671
672static int xts_aes_decrypt(struct blkcipher_desc *desc,
673 struct scatterlist *dst, struct scatterlist *src,
674 unsigned int nbytes)
675{
676 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
677 struct blkcipher_walk walk;
678
679 if (unlikely(xts_ctx->key_len == 48))
680 return xts_fallback_decrypt(desc, dst, src, nbytes);
681
682 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200683 return xts_aes_crypt(desc, xts_ctx->fc | CPACF_DECRYPT, xts_ctx, &walk);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000684}
685
686static int xts_fallback_init(struct crypto_tfm *tfm)
687{
688 const char *name = tfm->__crt_alg->cra_name;
689 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
690
Herbert Xu64e26802016-06-29 18:04:07 +0800691 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
692 CRYPTO_ALG_ASYNC |
693 CRYPTO_ALG_NEED_FALLBACK);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000694
695 if (IS_ERR(xts_ctx->fallback)) {
696 pr_err("Allocating XTS fallback algorithm %s failed\n",
697 name);
698 return PTR_ERR(xts_ctx->fallback);
699 }
700 return 0;
701}
702
703static void xts_fallback_exit(struct crypto_tfm *tfm)
704{
705 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
706
Herbert Xu64e26802016-06-29 18:04:07 +0800707 crypto_free_skcipher(xts_ctx->fallback);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000708}
709
710static struct crypto_alg xts_aes_alg = {
711 .cra_name = "xts(aes)",
712 .cra_driver_name = "xts-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100713 .cra_priority = 400, /* combo: aes + xts */
Gerald Schaefer99d97222011-04-26 16:12:42 +1000714 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
715 CRYPTO_ALG_NEED_FALLBACK,
716 .cra_blocksize = AES_BLOCK_SIZE,
717 .cra_ctxsize = sizeof(struct s390_xts_ctx),
718 .cra_type = &crypto_blkcipher_type,
719 .cra_module = THIS_MODULE,
Gerald Schaefer99d97222011-04-26 16:12:42 +1000720 .cra_init = xts_fallback_init,
721 .cra_exit = xts_fallback_exit,
722 .cra_u = {
723 .blkcipher = {
724 .min_keysize = 2 * AES_MIN_KEY_SIZE,
725 .max_keysize = 2 * AES_MAX_KEY_SIZE,
726 .ivsize = AES_BLOCK_SIZE,
727 .setkey = xts_aes_set_key,
728 .encrypt = xts_aes_encrypt,
729 .decrypt = xts_aes_decrypt,
730 }
731 }
732};
733
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000734static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
735 unsigned int key_len)
736{
737 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
738
739 switch (key_len) {
740 case 16:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200741 sctx->fc = CPACF_KMCTR_AES_128;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000742 break;
743 case 24:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200744 sctx->fc = CPACF_KMCTR_AES_192;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000745 break;
746 case 32:
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200747 sctx->fc = CPACF_KMCTR_AES_256;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000748 break;
749 }
750
751 return aes_set_key(tfm, in_key, key_len);
752}
753
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100754static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
755{
756 unsigned int i, n;
757
758 /* only use complete blocks, max. PAGE_SIZE */
759 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
760 for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) {
761 memcpy(ctrptr + i, ctrptr + i - AES_BLOCK_SIZE,
762 AES_BLOCK_SIZE);
763 crypto_inc(ctrptr + i, AES_BLOCK_SIZE);
764 }
765 return n;
766}
767
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000768static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
769 struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
770{
771 int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100772 unsigned int n, nbytes;
773 u8 buf[AES_BLOCK_SIZE], ctrbuf[AES_BLOCK_SIZE];
774 u8 *out, *in, *ctrptr = ctrbuf;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000775
776 if (!walk->nbytes)
777 return ret;
778
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100779 if (spin_trylock(&ctrblk_lock))
780 ctrptr = ctrblk;
781
782 memcpy(ctrptr, walk->iv, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000783 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
784 out = walk->dst.virt.addr;
785 in = walk->src.virt.addr;
786 while (nbytes >= AES_BLOCK_SIZE) {
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100787 if (ctrptr == ctrblk)
788 n = __ctrblk_init(ctrptr, nbytes);
789 else
790 n = AES_BLOCK_SIZE;
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200791 cpacf_kmctr(func, sctx->key, out, in, n, ctrptr);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000792 if (n > AES_BLOCK_SIZE)
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100793 memcpy(ctrptr, ctrptr + n - AES_BLOCK_SIZE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000794 AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100795 crypto_inc(ctrptr, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000796 out += n;
797 in += n;
798 nbytes -= n;
799 }
800 ret = blkcipher_walk_done(desc, walk, nbytes);
801 }
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100802 if (ctrptr == ctrblk) {
803 if (nbytes)
804 memcpy(ctrbuf, ctrptr, AES_BLOCK_SIZE);
805 else
806 memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
807 spin_unlock(&ctrblk_lock);
Harald Freudenberger3901c112014-05-07 16:51:29 +0200808 } else {
809 if (!nbytes)
810 memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100811 }
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000812 /*
813 * final block may be < AES_BLOCK_SIZE, copy only nbytes
814 */
815 if (nbytes) {
816 out = walk->dst.virt.addr;
817 in = walk->src.virt.addr;
Martin Schwidefsky0177db02016-08-15 10:41:52 +0200818 cpacf_kmctr(func, sctx->key, buf, in, AES_BLOCK_SIZE, ctrbuf);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000819 memcpy(out, buf, nbytes);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100820 crypto_inc(ctrbuf, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000821 ret = blkcipher_walk_done(desc, walk, 0);
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100822 memcpy(walk->iv, ctrbuf, AES_BLOCK_SIZE);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000823 }
Harald Freudenberger0519e9a2014-01-16 16:01:11 +0100824
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000825 return ret;
826}
827
828static int ctr_aes_encrypt(struct blkcipher_desc *desc,
829 struct scatterlist *dst, struct scatterlist *src,
830 unsigned int nbytes)
831{
832 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
833 struct blkcipher_walk walk;
834
835 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200836 return ctr_aes_crypt(desc, sctx->fc, sctx, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000837}
838
839static int ctr_aes_decrypt(struct blkcipher_desc *desc,
840 struct scatterlist *dst, struct scatterlist *src,
841 unsigned int nbytes)
842{
843 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
844 struct blkcipher_walk walk;
845
846 blkcipher_walk_init(&walk, dst, src, nbytes);
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200847 return ctr_aes_crypt(desc, sctx->fc | CPACF_DECRYPT, sctx, &walk);
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000848}
849
850static struct crypto_alg ctr_aes_alg = {
851 .cra_name = "ctr(aes)",
852 .cra_driver_name = "ctr-aes-s390",
Martin Schwidefskyc7d4d252016-03-17 15:22:12 +0100853 .cra_priority = 400, /* combo: aes + ctr */
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000854 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
855 .cra_blocksize = 1,
856 .cra_ctxsize = sizeof(struct s390_aes_ctx),
857 .cra_type = &crypto_blkcipher_type,
858 .cra_module = THIS_MODULE,
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000859 .cra_u = {
860 .blkcipher = {
861 .min_keysize = AES_MIN_KEY_SIZE,
862 .max_keysize = AES_MAX_KEY_SIZE,
863 .ivsize = AES_BLOCK_SIZE,
864 .setkey = ctr_aes_set_key,
865 .encrypt = ctr_aes_encrypt,
866 .decrypt = ctr_aes_decrypt,
867 }
868 }
869};
870
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200871static struct crypto_alg *aes_s390_algs_ptr[5];
872static int aes_s390_algs_num;
873
874static int aes_s390_register_alg(struct crypto_alg *alg)
875{
876 int ret;
877
878 ret = crypto_register_alg(alg);
879 if (!ret)
880 aes_s390_algs_ptr[aes_s390_algs_num++] = alg;
881 return ret;
882}
883
884static void aes_s390_fini(void)
885{
886 while (aes_s390_algs_num--)
887 crypto_unregister_alg(aes_s390_algs_ptr[aes_s390_algs_num]);
888 if (ctrblk)
889 free_page((unsigned long) ctrblk);
890}
Ingo Tuchscherer4f57ba72013-10-15 11:24:07 +0200891
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200892static int __init aes_s390_init(void)
Jan Glauberbf754ae2006-01-06 00:19:18 -0800893{
894 int ret;
895
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200896 if (cpacf_query(CPACF_KM, CPACF_KM_AES_128))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100897 keylen_flag |= AES_KEYLEN_128;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200898 if (cpacf_query(CPACF_KM, CPACF_KM_AES_192))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100899 keylen_flag |= AES_KEYLEN_192;
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200900 if (cpacf_query(CPACF_KM, CPACF_KM_AES_256))
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100901 keylen_flag |= AES_KEYLEN_256;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800902
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100903 if (!keylen_flag)
904 return -EOPNOTSUPP;
905
906 /* z9 109 and z9 BC/EC only support 128 bit key length */
Sebastian Siewiorb0c3e752007-12-01 12:47:37 +1100907 if (keylen_flag == AES_KEYLEN_128)
Jan Glauber39f09392008-12-25 13:39:37 +0100908 pr_info("AES hardware acceleration is only available for"
909 " 128-bit keys\n");
Jan Glauberbf754ae2006-01-06 00:19:18 -0800910
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200911 ret = aes_s390_register_alg(&aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100912 if (ret)
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200913 goto out_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000914
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200915 ret = aes_s390_register_alg(&ecb_aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100916 if (ret)
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200917 goto out_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000918
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200919 ret = aes_s390_register_alg(&cbc_aes_alg);
Jan Glauber86aa9fc2007-02-05 21:18:14 +0100920 if (ret)
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200921 goto out_err;
Herbert Xua9e62fa2006-08-21 21:39:24 +1000922
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200923 if (cpacf_query(CPACF_KM, CPACF_KM_XTS_128) &&
924 cpacf_query(CPACF_KM, CPACF_KM_XTS_256)) {
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200925 ret = aes_s390_register_alg(&xts_aes_alg);
Gerald Schaefer99d97222011-04-26 16:12:42 +1000926 if (ret)
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200927 goto out_err;
Gerald Schaefer99d97222011-04-26 16:12:42 +1000928 }
929
Martin Schwidefskyedc63a32016-08-15 09:19:16 +0200930 if (cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_128) &&
931 cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_192) &&
932 cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_256)) {
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000933 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
934 if (!ctrblk) {
935 ret = -ENOMEM;
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200936 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000937 }
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200938 ret = aes_s390_register_alg(&ctr_aes_alg);
939 if (ret)
940 goto out_err;
Gerald Schaefer0200f3e2011-05-04 15:09:44 +1000941 }
942
Martin Schwidefskyd863d592016-08-18 12:34:34 +0200943 return 0;
944out_err:
945 aes_s390_fini();
Jan Glauberbf754ae2006-01-06 00:19:18 -0800946 return ret;
Jan Glauberbf754ae2006-01-06 00:19:18 -0800947}
948
Hendrik Bruecknerd05377c2015-02-19 17:34:07 +0100949module_cpu_feature_match(MSA, aes_s390_init);
Heiko Carstens9f7819c2008-04-17 07:46:17 +0200950module_exit(aes_s390_fini);
Jan Glauberbf754ae2006-01-06 00:19:18 -0800951
Kees Cook5d26a102014-11-20 17:05:53 -0800952MODULE_ALIAS_CRYPTO("aes-all");
Jan Glauberbf754ae2006-01-06 00:19:18 -0800953
954MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
955MODULE_LICENSE("GPL");