Merge tag 'rproc-v4.8' of git://github.com/andersson/remoteproc
[deliverable/linux.git] / arch / s390 / crypto / aes_s390.c
CommitLineData
bf754ae8
JG
1/*
2 * Cryptographic API.
3 *
4 * s390 implementation of the AES Cipher Algorithm.
5 *
6 * s390 Version:
a53c8fab 7 * Copyright IBM Corp. 2005, 2007
bf754ae8 8 * Author(s): Jan Glauber (jang@de.ibm.com)
b0c3e75d 9 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
bf754ae8 10 *
f8246af0 11 * Derived from "crypto/aes_generic.c"
bf754ae8
JG
12 *
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
39f09392
JG
20#define KMSG_COMPONENT "aes_s390"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
89e12654 23#include <crypto/aes.h>
a9e62fad 24#include <crypto/algapi.h>
64e26807 25#include <crypto/internal/skcipher.h>
b0c3e75d 26#include <linux/err.h>
bf754ae8 27#include <linux/module.h>
d05377c1 28#include <linux/cpufeature.h>
bf754ae8 29#include <linux/init.h>
0519e9ad 30#include <linux/spinlock.h>
49abc0d2 31#include <crypto/xts.h>
c7d4d259 32#include <asm/cpacf.h>
bf754ae8 33
86aa9fc2
JG
34#define AES_KEYLEN_128 1
35#define AES_KEYLEN_192 2
36#define AES_KEYLEN_256 4
37
0200f3ec 38static u8 *ctrblk;
0519e9ad 39static DEFINE_SPINLOCK(ctrblk_lock);
0200f3ec 40static char keylen_flag;
bf754ae8
JG
41
42struct s390_aes_ctx {
bf754ae8 43 u8 key[AES_MAX_KEY_SIZE];
a9e62fad
HX
44 long enc;
45 long dec;
bf754ae8 46 int key_len;
b0c3e75d 47 union {
64e26807 48 struct crypto_skcipher *blk;
b0c3e75d
SS
49 struct crypto_cipher *cip;
50 } fallback;
bf754ae8
JG
51};
52
99d97222
GS
53struct pcc_param {
54 u8 key[32];
55 u8 tweak[16];
56 u8 block[16];
57 u8 bit[16];
58 u8 xts[16];
59};
60
61struct s390_xts_ctx {
62 u8 key[32];
9dda2769 63 u8 pcc_key[32];
99d97222
GS
64 long enc;
65 long dec;
66 int key_len;
64e26807 67 struct crypto_skcipher *fallback;
99d97222
GS
68};
69
b0c3e75d
SS
70/*
71 * Check if the key_len is supported by the HW.
72 * Returns 0 if it is, a positive number if it is not and software fallback is
73 * required or a negative number in case the key size is not valid
74 */
75static int need_fallback(unsigned int key_len)
bf754ae8 76{
bf754ae8
JG
77 switch (key_len) {
78 case 16:
86aa9fc2 79 if (!(keylen_flag & AES_KEYLEN_128))
b0c3e75d 80 return 1;
bf754ae8
JG
81 break;
82 case 24:
86aa9fc2 83 if (!(keylen_flag & AES_KEYLEN_192))
b0c3e75d 84 return 1;
bf754ae8
JG
85 break;
86 case 32:
86aa9fc2 87 if (!(keylen_flag & AES_KEYLEN_256))
b0c3e75d 88 return 1;
bf754ae8
JG
89 break;
90 default:
b0c3e75d 91 return -1;
bf754ae8
JG
92 break;
93 }
b0c3e75d
SS
94 return 0;
95}
96
97static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
98 unsigned int key_len)
99{
100 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
101 int ret;
102
d7ac7690
RK
103 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
104 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
b0c3e75d
SS
105 CRYPTO_TFM_REQ_MASK);
106
107 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
108 if (ret) {
109 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
d7ac7690 110 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
b0c3e75d
SS
111 CRYPTO_TFM_RES_MASK);
112 }
113 return ret;
114}
115
116static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
117 unsigned int key_len)
118{
119 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
120 u32 *flags = &tfm->crt_flags;
121 int ret;
122
123 ret = need_fallback(key_len);
124 if (ret < 0) {
125 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
126 return -EINVAL;
127 }
bf754ae8
JG
128
129 sctx->key_len = key_len;
b0c3e75d
SS
130 if (!ret) {
131 memcpy(sctx->key, in_key, key_len);
132 return 0;
133 }
134
135 return setkey_fallback_cip(tfm, in_key, key_len);
bf754ae8
JG
136}
137
6c2bb98b 138static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
bf754ae8 139{
e6a67ad0 140 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
bf754ae8 141
b0c3e75d
SS
142 if (unlikely(need_fallback(sctx->key_len))) {
143 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
144 return;
145 }
146
bf754ae8
JG
147 switch (sctx->key_len) {
148 case 16:
c7d4d259
MS
149 cpacf_km(CPACF_KM_AES_128_ENC, &sctx->key, out, in,
150 AES_BLOCK_SIZE);
bf754ae8
JG
151 break;
152 case 24:
c7d4d259
MS
153 cpacf_km(CPACF_KM_AES_192_ENC, &sctx->key, out, in,
154 AES_BLOCK_SIZE);
bf754ae8
JG
155 break;
156 case 32:
c7d4d259
MS
157 cpacf_km(CPACF_KM_AES_256_ENC, &sctx->key, out, in,
158 AES_BLOCK_SIZE);
bf754ae8
JG
159 break;
160 }
161}
162
6c2bb98b 163static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
bf754ae8 164{
e6a67ad0 165 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
bf754ae8 166
b0c3e75d
SS
167 if (unlikely(need_fallback(sctx->key_len))) {
168 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
169 return;
170 }
171
bf754ae8
JG
172 switch (sctx->key_len) {
173 case 16:
c7d4d259
MS
174 cpacf_km(CPACF_KM_AES_128_DEC, &sctx->key, out, in,
175 AES_BLOCK_SIZE);
bf754ae8
JG
176 break;
177 case 24:
c7d4d259
MS
178 cpacf_km(CPACF_KM_AES_192_DEC, &sctx->key, out, in,
179 AES_BLOCK_SIZE);
bf754ae8
JG
180 break;
181 case 32:
c7d4d259
MS
182 cpacf_km(CPACF_KM_AES_256_DEC, &sctx->key, out, in,
183 AES_BLOCK_SIZE);
bf754ae8
JG
184 break;
185 }
186}
187
b0c3e75d
SS
188static int fallback_init_cip(struct crypto_tfm *tfm)
189{
190 const char *name = tfm->__crt_alg->cra_name;
191 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
192
193 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
194 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
195
196 if (IS_ERR(sctx->fallback.cip)) {
39f09392
JG
197 pr_err("Allocating AES fallback algorithm %s failed\n",
198 name);
b59cdcb3 199 return PTR_ERR(sctx->fallback.cip);
b0c3e75d
SS
200 }
201
202 return 0;
203}
204
205static void fallback_exit_cip(struct crypto_tfm *tfm)
206{
207 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
208
209 crypto_free_cipher(sctx->fallback.cip);
210 sctx->fallback.cip = NULL;
211}
bf754ae8
JG
212
213static struct crypto_alg aes_alg = {
214 .cra_name = "aes",
65b75c36 215 .cra_driver_name = "aes-s390",
c7d4d259 216 .cra_priority = 300,
f67d1369
JG
217 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
218 CRYPTO_ALG_NEED_FALLBACK,
bf754ae8
JG
219 .cra_blocksize = AES_BLOCK_SIZE,
220 .cra_ctxsize = sizeof(struct s390_aes_ctx),
221 .cra_module = THIS_MODULE,
b0c3e75d
SS
222 .cra_init = fallback_init_cip,
223 .cra_exit = fallback_exit_cip,
bf754ae8
JG
224 .cra_u = {
225 .cipher = {
226 .cia_min_keysize = AES_MIN_KEY_SIZE,
227 .cia_max_keysize = AES_MAX_KEY_SIZE,
228 .cia_setkey = aes_set_key,
229 .cia_encrypt = aes_encrypt,
230 .cia_decrypt = aes_decrypt,
bf754ae8
JG
231 }
232 }
233};
234
b0c3e75d
SS
235static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
236 unsigned int len)
237{
238 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
239 unsigned int ret;
240
64e26807
HX
241 crypto_skcipher_clear_flags(sctx->fallback.blk, CRYPTO_TFM_REQ_MASK);
242 crypto_skcipher_set_flags(sctx->fallback.blk, tfm->crt_flags &
243 CRYPTO_TFM_REQ_MASK);
244
245 ret = crypto_skcipher_setkey(sctx->fallback.blk, key, len);
246
247 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
248 tfm->crt_flags |= crypto_skcipher_get_flags(sctx->fallback.blk) &
249 CRYPTO_TFM_RES_MASK;
b0c3e75d 250
b0c3e75d
SS
251 return ret;
252}
253
254static int fallback_blk_dec(struct blkcipher_desc *desc,
255 struct scatterlist *dst, struct scatterlist *src,
256 unsigned int nbytes)
257{
258 unsigned int ret;
64e26807
HX
259 struct crypto_blkcipher *tfm = desc->tfm;
260 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
261 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
b0c3e75d 262
64e26807
HX
263 skcipher_request_set_tfm(req, sctx->fallback.blk);
264 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
265 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
b0c3e75d 266
64e26807 267 ret = crypto_skcipher_decrypt(req);
b0c3e75d 268
64e26807 269 skcipher_request_zero(req);
b0c3e75d
SS
270 return ret;
271}
272
273static int fallback_blk_enc(struct blkcipher_desc *desc,
274 struct scatterlist *dst, struct scatterlist *src,
275 unsigned int nbytes)
276{
277 unsigned int ret;
64e26807
HX
278 struct crypto_blkcipher *tfm = desc->tfm;
279 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
280 SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
b0c3e75d 281
64e26807
HX
282 skcipher_request_set_tfm(req, sctx->fallback.blk);
283 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
284 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
b0c3e75d 285
64e26807 286 ret = crypto_skcipher_encrypt(req);
b0c3e75d
SS
287 return ret;
288}
289
a9e62fad
HX
290static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
291 unsigned int key_len)
292{
293 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
b0c3e75d
SS
294 int ret;
295
296 ret = need_fallback(key_len);
297 if (ret > 0) {
298 sctx->key_len = key_len;
299 return setkey_fallback_blk(tfm, in_key, key_len);
300 }
a9e62fad
HX
301
302 switch (key_len) {
303 case 16:
c7d4d259
MS
304 sctx->enc = CPACF_KM_AES_128_ENC;
305 sctx->dec = CPACF_KM_AES_128_DEC;
a9e62fad
HX
306 break;
307 case 24:
c7d4d259
MS
308 sctx->enc = CPACF_KM_AES_192_ENC;
309 sctx->dec = CPACF_KM_AES_192_DEC;
a9e62fad
HX
310 break;
311 case 32:
c7d4d259
MS
312 sctx->enc = CPACF_KM_AES_256_ENC;
313 sctx->dec = CPACF_KM_AES_256_DEC;
a9e62fad
HX
314 break;
315 }
316
317 return aes_set_key(tfm, in_key, key_len);
318}
319
320static int ecb_aes_crypt(struct blkcipher_desc *desc, long func, void *param,
321 struct blkcipher_walk *walk)
322{
323 int ret = blkcipher_walk_virt(desc, walk);
324 unsigned int nbytes;
325
326 while ((nbytes = walk->nbytes)) {
327 /* only use complete blocks */
328 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
329 u8 *out = walk->dst.virt.addr;
330 u8 *in = walk->src.virt.addr;
331
c7d4d259 332 ret = cpacf_km(func, param, out, in, n);
36eb2caa
JG
333 if (ret < 0 || ret != n)
334 return -EIO;
a9e62fad
HX
335
336 nbytes &= AES_BLOCK_SIZE - 1;
337 ret = blkcipher_walk_done(desc, walk, nbytes);
338 }
339
340 return ret;
341}
342
343static int ecb_aes_encrypt(struct blkcipher_desc *desc,
344 struct scatterlist *dst, struct scatterlist *src,
345 unsigned int nbytes)
346{
347 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
348 struct blkcipher_walk walk;
349
b0c3e75d
SS
350 if (unlikely(need_fallback(sctx->key_len)))
351 return fallback_blk_enc(desc, dst, src, nbytes);
352
a9e62fad
HX
353 blkcipher_walk_init(&walk, dst, src, nbytes);
354 return ecb_aes_crypt(desc, sctx->enc, sctx->key, &walk);
355}
356
357static int ecb_aes_decrypt(struct blkcipher_desc *desc,
358 struct scatterlist *dst, struct scatterlist *src,
359 unsigned int nbytes)
360{
361 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
362 struct blkcipher_walk walk;
363
b0c3e75d
SS
364 if (unlikely(need_fallback(sctx->key_len)))
365 return fallback_blk_dec(desc, dst, src, nbytes);
366
a9e62fad
HX
367 blkcipher_walk_init(&walk, dst, src, nbytes);
368 return ecb_aes_crypt(desc, sctx->dec, sctx->key, &walk);
369}
370
b0c3e75d
SS
371static int fallback_init_blk(struct crypto_tfm *tfm)
372{
373 const char *name = tfm->__crt_alg->cra_name;
374 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
375
64e26807
HX
376 sctx->fallback.blk = crypto_alloc_skcipher(name, 0,
377 CRYPTO_ALG_ASYNC |
378 CRYPTO_ALG_NEED_FALLBACK);
b0c3e75d
SS
379
380 if (IS_ERR(sctx->fallback.blk)) {
39f09392
JG
381 pr_err("Allocating AES fallback algorithm %s failed\n",
382 name);
b0c3e75d
SS
383 return PTR_ERR(sctx->fallback.blk);
384 }
385
386 return 0;
387}
388
389static void fallback_exit_blk(struct crypto_tfm *tfm)
390{
391 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
392
64e26807 393 crypto_free_skcipher(sctx->fallback.blk);
b0c3e75d
SS
394}
395
a9e62fad
HX
396static struct crypto_alg ecb_aes_alg = {
397 .cra_name = "ecb(aes)",
398 .cra_driver_name = "ecb-aes-s390",
c7d4d259 399 .cra_priority = 400, /* combo: aes + ecb */
f67d1369
JG
400 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
401 CRYPTO_ALG_NEED_FALLBACK,
a9e62fad
HX
402 .cra_blocksize = AES_BLOCK_SIZE,
403 .cra_ctxsize = sizeof(struct s390_aes_ctx),
404 .cra_type = &crypto_blkcipher_type,
405 .cra_module = THIS_MODULE,
b0c3e75d
SS
406 .cra_init = fallback_init_blk,
407 .cra_exit = fallback_exit_blk,
a9e62fad
HX
408 .cra_u = {
409 .blkcipher = {
410 .min_keysize = AES_MIN_KEY_SIZE,
411 .max_keysize = AES_MAX_KEY_SIZE,
412 .setkey = ecb_aes_set_key,
413 .encrypt = ecb_aes_encrypt,
414 .decrypt = ecb_aes_decrypt,
415 }
416 }
417};
418
419static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
420 unsigned int key_len)
421{
422 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
b0c3e75d
SS
423 int ret;
424
425 ret = need_fallback(key_len);
426 if (ret > 0) {
427 sctx->key_len = key_len;
428 return setkey_fallback_blk(tfm, in_key, key_len);
429 }
a9e62fad
HX
430
431 switch (key_len) {
432 case 16:
c7d4d259
MS
433 sctx->enc = CPACF_KMC_AES_128_ENC;
434 sctx->dec = CPACF_KMC_AES_128_DEC;
a9e62fad
HX
435 break;
436 case 24:
c7d4d259
MS
437 sctx->enc = CPACF_KMC_AES_192_ENC;
438 sctx->dec = CPACF_KMC_AES_192_DEC;
a9e62fad
HX
439 break;
440 case 32:
c7d4d259
MS
441 sctx->enc = CPACF_KMC_AES_256_ENC;
442 sctx->dec = CPACF_KMC_AES_256_DEC;
a9e62fad
HX
443 break;
444 }
445
446 return aes_set_key(tfm, in_key, key_len);
447}
448
f262f0f5 449static int cbc_aes_crypt(struct blkcipher_desc *desc, long func,
a9e62fad
HX
450 struct blkcipher_walk *walk)
451{
f262f0f5 452 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
a9e62fad
HX
453 int ret = blkcipher_walk_virt(desc, walk);
454 unsigned int nbytes = walk->nbytes;
f262f0f5
HX
455 struct {
456 u8 iv[AES_BLOCK_SIZE];
457 u8 key[AES_MAX_KEY_SIZE];
458 } param;
a9e62fad
HX
459
460 if (!nbytes)
461 goto out;
462
f262f0f5
HX
463 memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
464 memcpy(param.key, sctx->key, sctx->key_len);
a9e62fad
HX
465 do {
466 /* only use complete blocks */
467 unsigned int n = nbytes & ~(AES_BLOCK_SIZE - 1);
468 u8 *out = walk->dst.virt.addr;
469 u8 *in = walk->src.virt.addr;
470
c7d4d259 471 ret = cpacf_kmc(func, &param, out, in, n);
36eb2caa
JG
472 if (ret < 0 || ret != n)
473 return -EIO;
a9e62fad
HX
474
475 nbytes &= AES_BLOCK_SIZE - 1;
476 ret = blkcipher_walk_done(desc, walk, nbytes);
477 } while ((nbytes = walk->nbytes));
f262f0f5 478 memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
a9e62fad
HX
479
480out:
481 return ret;
482}
483
484static int cbc_aes_encrypt(struct blkcipher_desc *desc,
485 struct scatterlist *dst, struct scatterlist *src,
486 unsigned int nbytes)
487{
488 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
489 struct blkcipher_walk walk;
490
b0c3e75d
SS
491 if (unlikely(need_fallback(sctx->key_len)))
492 return fallback_blk_enc(desc, dst, src, nbytes);
493
a9e62fad 494 blkcipher_walk_init(&walk, dst, src, nbytes);
f262f0f5 495 return cbc_aes_crypt(desc, sctx->enc, &walk);
a9e62fad
HX
496}
497
498static int cbc_aes_decrypt(struct blkcipher_desc *desc,
499 struct scatterlist *dst, struct scatterlist *src,
500 unsigned int nbytes)
501{
502 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
503 struct blkcipher_walk walk;
504
b0c3e75d
SS
505 if (unlikely(need_fallback(sctx->key_len)))
506 return fallback_blk_dec(desc, dst, src, nbytes);
507
a9e62fad 508 blkcipher_walk_init(&walk, dst, src, nbytes);
f262f0f5 509 return cbc_aes_crypt(desc, sctx->dec, &walk);
a9e62fad
HX
510}
511
512static struct crypto_alg cbc_aes_alg = {
513 .cra_name = "cbc(aes)",
514 .cra_driver_name = "cbc-aes-s390",
c7d4d259 515 .cra_priority = 400, /* combo: aes + cbc */
f67d1369
JG
516 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
517 CRYPTO_ALG_NEED_FALLBACK,
a9e62fad
HX
518 .cra_blocksize = AES_BLOCK_SIZE,
519 .cra_ctxsize = sizeof(struct s390_aes_ctx),
520 .cra_type = &crypto_blkcipher_type,
521 .cra_module = THIS_MODULE,
b0c3e75d
SS
522 .cra_init = fallback_init_blk,
523 .cra_exit = fallback_exit_blk,
a9e62fad
HX
524 .cra_u = {
525 .blkcipher = {
526 .min_keysize = AES_MIN_KEY_SIZE,
527 .max_keysize = AES_MAX_KEY_SIZE,
528 .ivsize = AES_BLOCK_SIZE,
529 .setkey = cbc_aes_set_key,
530 .encrypt = cbc_aes_encrypt,
531 .decrypt = cbc_aes_decrypt,
532 }
533 }
534};
535
99d97222
GS
536static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
537 unsigned int len)
538{
539 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
540 unsigned int ret;
541
64e26807
HX
542 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
543 crypto_skcipher_set_flags(xts_ctx->fallback, tfm->crt_flags &
544 CRYPTO_TFM_REQ_MASK);
545
546 ret = crypto_skcipher_setkey(xts_ctx->fallback, key, len);
547
548 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
549 tfm->crt_flags |= crypto_skcipher_get_flags(xts_ctx->fallback) &
550 CRYPTO_TFM_RES_MASK;
99d97222 551
99d97222
GS
552 return ret;
553}
554
555static int xts_fallback_decrypt(struct blkcipher_desc *desc,
556 struct scatterlist *dst, struct scatterlist *src,
557 unsigned int nbytes)
558{
64e26807
HX
559 struct crypto_blkcipher *tfm = desc->tfm;
560 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
561 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
99d97222
GS
562 unsigned int ret;
563
64e26807
HX
564 skcipher_request_set_tfm(req, xts_ctx->fallback);
565 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
566 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
99d97222 567
64e26807 568 ret = crypto_skcipher_decrypt(req);
99d97222 569
64e26807 570 skcipher_request_zero(req);
99d97222
GS
571 return ret;
572}
573
574static int xts_fallback_encrypt(struct blkcipher_desc *desc,
575 struct scatterlist *dst, struct scatterlist *src,
576 unsigned int nbytes)
577{
64e26807
HX
578 struct crypto_blkcipher *tfm = desc->tfm;
579 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
580 SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
99d97222
GS
581 unsigned int ret;
582
64e26807
HX
583 skcipher_request_set_tfm(req, xts_ctx->fallback);
584 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
585 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
99d97222 586
64e26807 587 ret = crypto_skcipher_encrypt(req);
99d97222 588
64e26807 589 skcipher_request_zero(req);
99d97222
GS
590 return ret;
591}
592
593static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
594 unsigned int key_len)
595{
596 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
597 u32 *flags = &tfm->crt_flags;
28856a9e
SM
598 int err;
599
600 err = xts_check_key(tfm, in_key, key_len);
601 if (err)
602 return err;
99d97222
GS
603
604 switch (key_len) {
605 case 32:
c7d4d259
MS
606 xts_ctx->enc = CPACF_KM_XTS_128_ENC;
607 xts_ctx->dec = CPACF_KM_XTS_128_DEC;
99d97222 608 memcpy(xts_ctx->key + 16, in_key, 16);
9dda2769 609 memcpy(xts_ctx->pcc_key + 16, in_key + 16, 16);
99d97222
GS
610 break;
611 case 48:
612 xts_ctx->enc = 0;
613 xts_ctx->dec = 0;
614 xts_fallback_setkey(tfm, in_key, key_len);
615 break;
616 case 64:
c7d4d259
MS
617 xts_ctx->enc = CPACF_KM_XTS_256_ENC;
618 xts_ctx->dec = CPACF_KM_XTS_256_DEC;
99d97222 619 memcpy(xts_ctx->key, in_key, 32);
9dda2769 620 memcpy(xts_ctx->pcc_key, in_key + 32, 32);
99d97222
GS
621 break;
622 default:
623 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
624 return -EINVAL;
625 }
626 xts_ctx->key_len = key_len;
627 return 0;
628}
629
630static int xts_aes_crypt(struct blkcipher_desc *desc, long func,
631 struct s390_xts_ctx *xts_ctx,
632 struct blkcipher_walk *walk)
633{
634 unsigned int offset = (xts_ctx->key_len >> 1) & 0x10;
635 int ret = blkcipher_walk_virt(desc, walk);
636 unsigned int nbytes = walk->nbytes;
637 unsigned int n;
638 u8 *in, *out;
9dda2769
GS
639 struct pcc_param pcc_param;
640 struct {
641 u8 key[32];
642 u8 init[16];
643 } xts_param;
99d97222
GS
644
645 if (!nbytes)
646 goto out;
647
9dda2769
GS
648 memset(pcc_param.block, 0, sizeof(pcc_param.block));
649 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
650 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
651 memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
652 memcpy(pcc_param.key, xts_ctx->pcc_key, 32);
c7d4d259
MS
653 /* remove decipher modifier bit from 'func' and call PCC */
654 ret = cpacf_pcc(func & 0x7f, &pcc_param.key[offset]);
36eb2caa
JG
655 if (ret < 0)
656 return -EIO;
99d97222 657
9dda2769
GS
658 memcpy(xts_param.key, xts_ctx->key, 32);
659 memcpy(xts_param.init, pcc_param.xts, 16);
99d97222
GS
660 do {
661 /* only use complete blocks */
662 n = nbytes & ~(AES_BLOCK_SIZE - 1);
663 out = walk->dst.virt.addr;
664 in = walk->src.virt.addr;
665
c7d4d259 666 ret = cpacf_km(func, &xts_param.key[offset], out, in, n);
36eb2caa
JG
667 if (ret < 0 || ret != n)
668 return -EIO;
99d97222
GS
669
670 nbytes &= AES_BLOCK_SIZE - 1;
671 ret = blkcipher_walk_done(desc, walk, nbytes);
672 } while ((nbytes = walk->nbytes));
673out:
674 return ret;
675}
676
677static int xts_aes_encrypt(struct blkcipher_desc *desc,
678 struct scatterlist *dst, struct scatterlist *src,
679 unsigned int nbytes)
680{
681 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
682 struct blkcipher_walk walk;
683
684 if (unlikely(xts_ctx->key_len == 48))
685 return xts_fallback_encrypt(desc, dst, src, nbytes);
686
687 blkcipher_walk_init(&walk, dst, src, nbytes);
688 return xts_aes_crypt(desc, xts_ctx->enc, xts_ctx, &walk);
689}
690
691static int xts_aes_decrypt(struct blkcipher_desc *desc,
692 struct scatterlist *dst, struct scatterlist *src,
693 unsigned int nbytes)
694{
695 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
696 struct blkcipher_walk walk;
697
698 if (unlikely(xts_ctx->key_len == 48))
699 return xts_fallback_decrypt(desc, dst, src, nbytes);
700
701 blkcipher_walk_init(&walk, dst, src, nbytes);
702 return xts_aes_crypt(desc, xts_ctx->dec, xts_ctx, &walk);
703}
704
705static int xts_fallback_init(struct crypto_tfm *tfm)
706{
707 const char *name = tfm->__crt_alg->cra_name;
708 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
709
64e26807
HX
710 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
711 CRYPTO_ALG_ASYNC |
712 CRYPTO_ALG_NEED_FALLBACK);
99d97222
GS
713
714 if (IS_ERR(xts_ctx->fallback)) {
715 pr_err("Allocating XTS fallback algorithm %s failed\n",
716 name);
717 return PTR_ERR(xts_ctx->fallback);
718 }
719 return 0;
720}
721
722static void xts_fallback_exit(struct crypto_tfm *tfm)
723{
724 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
725
64e26807 726 crypto_free_skcipher(xts_ctx->fallback);
99d97222
GS
727}
728
729static struct crypto_alg xts_aes_alg = {
730 .cra_name = "xts(aes)",
731 .cra_driver_name = "xts-aes-s390",
c7d4d259 732 .cra_priority = 400, /* combo: aes + xts */
99d97222
GS
733 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
734 CRYPTO_ALG_NEED_FALLBACK,
735 .cra_blocksize = AES_BLOCK_SIZE,
736 .cra_ctxsize = sizeof(struct s390_xts_ctx),
737 .cra_type = &crypto_blkcipher_type,
738 .cra_module = THIS_MODULE,
99d97222
GS
739 .cra_init = xts_fallback_init,
740 .cra_exit = xts_fallback_exit,
741 .cra_u = {
742 .blkcipher = {
743 .min_keysize = 2 * AES_MIN_KEY_SIZE,
744 .max_keysize = 2 * AES_MAX_KEY_SIZE,
745 .ivsize = AES_BLOCK_SIZE,
746 .setkey = xts_aes_set_key,
747 .encrypt = xts_aes_encrypt,
748 .decrypt = xts_aes_decrypt,
749 }
750 }
751};
752
4f57ba71
IT
753static int xts_aes_alg_reg;
754
0200f3ec
GS
755static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
756 unsigned int key_len)
757{
758 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
759
760 switch (key_len) {
761 case 16:
c7d4d259
MS
762 sctx->enc = CPACF_KMCTR_AES_128_ENC;
763 sctx->dec = CPACF_KMCTR_AES_128_DEC;
0200f3ec
GS
764 break;
765 case 24:
c7d4d259
MS
766 sctx->enc = CPACF_KMCTR_AES_192_ENC;
767 sctx->dec = CPACF_KMCTR_AES_192_DEC;
0200f3ec
GS
768 break;
769 case 32:
c7d4d259
MS
770 sctx->enc = CPACF_KMCTR_AES_256_ENC;
771 sctx->dec = CPACF_KMCTR_AES_256_DEC;
0200f3ec
GS
772 break;
773 }
774
775 return aes_set_key(tfm, in_key, key_len);
776}
777
0519e9ad
HF
778static unsigned int __ctrblk_init(u8 *ctrptr, unsigned int nbytes)
779{
780 unsigned int i, n;
781
782 /* only use complete blocks, max. PAGE_SIZE */
783 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
784 for (i = AES_BLOCK_SIZE; i < n; i += AES_BLOCK_SIZE) {
785 memcpy(ctrptr + i, ctrptr + i - AES_BLOCK_SIZE,
786 AES_BLOCK_SIZE);
787 crypto_inc(ctrptr + i, AES_BLOCK_SIZE);
788 }
789 return n;
790}
791
0200f3ec
GS
792static int ctr_aes_crypt(struct blkcipher_desc *desc, long func,
793 struct s390_aes_ctx *sctx, struct blkcipher_walk *walk)
794{
795 int ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
0519e9ad
HF
796 unsigned int n, nbytes;
797 u8 buf[AES_BLOCK_SIZE], ctrbuf[AES_BLOCK_SIZE];
798 u8 *out, *in, *ctrptr = ctrbuf;
0200f3ec
GS
799
800 if (!walk->nbytes)
801 return ret;
802
0519e9ad
HF
803 if (spin_trylock(&ctrblk_lock))
804 ctrptr = ctrblk;
805
806 memcpy(ctrptr, walk->iv, AES_BLOCK_SIZE);
0200f3ec
GS
807 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
808 out = walk->dst.virt.addr;
809 in = walk->src.virt.addr;
810 while (nbytes >= AES_BLOCK_SIZE) {
0519e9ad
HF
811 if (ctrptr == ctrblk)
812 n = __ctrblk_init(ctrptr, nbytes);
813 else
814 n = AES_BLOCK_SIZE;
c7d4d259 815 ret = cpacf_kmctr(func, sctx->key, out, in, n, ctrptr);
0519e9ad
HF
816 if (ret < 0 || ret != n) {
817 if (ctrptr == ctrblk)
818 spin_unlock(&ctrblk_lock);
36eb2caa 819 return -EIO;
0519e9ad 820 }
0200f3ec 821 if (n > AES_BLOCK_SIZE)
0519e9ad 822 memcpy(ctrptr, ctrptr + n - AES_BLOCK_SIZE,
0200f3ec 823 AES_BLOCK_SIZE);
0519e9ad 824 crypto_inc(ctrptr, AES_BLOCK_SIZE);
0200f3ec
GS
825 out += n;
826 in += n;
827 nbytes -= n;
828 }
829 ret = blkcipher_walk_done(desc, walk, nbytes);
830 }
0519e9ad
HF
831 if (ctrptr == ctrblk) {
832 if (nbytes)
833 memcpy(ctrbuf, ctrptr, AES_BLOCK_SIZE);
834 else
835 memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
836 spin_unlock(&ctrblk_lock);
3901c112
HF
837 } else {
838 if (!nbytes)
839 memcpy(walk->iv, ctrptr, AES_BLOCK_SIZE);
0519e9ad 840 }
0200f3ec
GS
841 /*
842 * final block may be < AES_BLOCK_SIZE, copy only nbytes
843 */
844 if (nbytes) {
845 out = walk->dst.virt.addr;
846 in = walk->src.virt.addr;
c7d4d259
MS
847 ret = cpacf_kmctr(func, sctx->key, buf, in,
848 AES_BLOCK_SIZE, ctrbuf);
36eb2caa
JG
849 if (ret < 0 || ret != AES_BLOCK_SIZE)
850 return -EIO;
0200f3ec 851 memcpy(out, buf, nbytes);
0519e9ad 852 crypto_inc(ctrbuf, AES_BLOCK_SIZE);
0200f3ec 853 ret = blkcipher_walk_done(desc, walk, 0);
0519e9ad 854 memcpy(walk->iv, ctrbuf, AES_BLOCK_SIZE);
0200f3ec 855 }
0519e9ad 856
0200f3ec
GS
857 return ret;
858}
859
860static int ctr_aes_encrypt(struct blkcipher_desc *desc,
861 struct scatterlist *dst, struct scatterlist *src,
862 unsigned int nbytes)
863{
864 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
865 struct blkcipher_walk walk;
866
867 blkcipher_walk_init(&walk, dst, src, nbytes);
868 return ctr_aes_crypt(desc, sctx->enc, sctx, &walk);
869}
870
871static int ctr_aes_decrypt(struct blkcipher_desc *desc,
872 struct scatterlist *dst, struct scatterlist *src,
873 unsigned int nbytes)
874{
875 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
876 struct blkcipher_walk walk;
877
878 blkcipher_walk_init(&walk, dst, src, nbytes);
879 return ctr_aes_crypt(desc, sctx->dec, sctx, &walk);
880}
881
882static struct crypto_alg ctr_aes_alg = {
883 .cra_name = "ctr(aes)",
884 .cra_driver_name = "ctr-aes-s390",
c7d4d259 885 .cra_priority = 400, /* combo: aes + ctr */
0200f3ec
GS
886 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
887 .cra_blocksize = 1,
888 .cra_ctxsize = sizeof(struct s390_aes_ctx),
889 .cra_type = &crypto_blkcipher_type,
890 .cra_module = THIS_MODULE,
0200f3ec
GS
891 .cra_u = {
892 .blkcipher = {
893 .min_keysize = AES_MIN_KEY_SIZE,
894 .max_keysize = AES_MAX_KEY_SIZE,
895 .ivsize = AES_BLOCK_SIZE,
896 .setkey = ctr_aes_set_key,
897 .encrypt = ctr_aes_encrypt,
898 .decrypt = ctr_aes_decrypt,
899 }
900 }
901};
902
4f57ba71
IT
903static int ctr_aes_alg_reg;
904
9f7819c1 905static int __init aes_s390_init(void)
bf754ae8
JG
906{
907 int ret;
908
c7d4d259 909 if (cpacf_query(CPACF_KM, CPACF_KM_AES_128_ENC))
86aa9fc2 910 keylen_flag |= AES_KEYLEN_128;
c7d4d259 911 if (cpacf_query(CPACF_KM, CPACF_KM_AES_192_ENC))
86aa9fc2 912 keylen_flag |= AES_KEYLEN_192;
c7d4d259 913 if (cpacf_query(CPACF_KM, CPACF_KM_AES_256_ENC))
86aa9fc2
JG
914 keylen_flag |= AES_KEYLEN_256;
915
916 if (!keylen_flag)
917 return -EOPNOTSUPP;
bf754ae8 918
86aa9fc2 919 /* z9 109 and z9 BC/EC only support 128 bit key length */
b0c3e75d 920 if (keylen_flag == AES_KEYLEN_128)
39f09392
JG
921 pr_info("AES hardware acceleration is only available for"
922 " 128-bit keys\n");
bf754ae8
JG
923
924 ret = crypto_register_alg(&aes_alg);
86aa9fc2 925 if (ret)
a9e62fad 926 goto aes_err;
a9e62fad
HX
927
928 ret = crypto_register_alg(&ecb_aes_alg);
86aa9fc2 929 if (ret)
a9e62fad 930 goto ecb_aes_err;
a9e62fad
HX
931
932 ret = crypto_register_alg(&cbc_aes_alg);
86aa9fc2 933 if (ret)
a9e62fad 934 goto cbc_aes_err;
a9e62fad 935
c7d4d259
MS
936 if (cpacf_query(CPACF_KM, CPACF_KM_XTS_128_ENC) &&
937 cpacf_query(CPACF_KM, CPACF_KM_XTS_256_ENC)) {
99d97222
GS
938 ret = crypto_register_alg(&xts_aes_alg);
939 if (ret)
940 goto xts_aes_err;
4f57ba71 941 xts_aes_alg_reg = 1;
99d97222
GS
942 }
943
c7d4d259
MS
944 if (cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_128_ENC) &&
945 cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_192_ENC) &&
946 cpacf_query(CPACF_KMCTR, CPACF_KMCTR_AES_256_ENC)) {
0200f3ec
GS
947 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
948 if (!ctrblk) {
949 ret = -ENOMEM;
950 goto ctr_aes_err;
951 }
952 ret = crypto_register_alg(&ctr_aes_alg);
953 if (ret) {
954 free_page((unsigned long) ctrblk);
955 goto ctr_aes_err;
956 }
4f57ba71 957 ctr_aes_alg_reg = 1;
0200f3ec
GS
958 }
959
a9e62fad 960out:
bf754ae8 961 return ret;
a9e62fad 962
0200f3ec
GS
963ctr_aes_err:
964 crypto_unregister_alg(&xts_aes_alg);
99d97222
GS
965xts_aes_err:
966 crypto_unregister_alg(&cbc_aes_alg);
a9e62fad
HX
967cbc_aes_err:
968 crypto_unregister_alg(&ecb_aes_alg);
969ecb_aes_err:
970 crypto_unregister_alg(&aes_alg);
971aes_err:
972 goto out;
bf754ae8
JG
973}
974
9f7819c1 975static void __exit aes_s390_fini(void)
bf754ae8 976{
4f57ba71
IT
977 if (ctr_aes_alg_reg) {
978 crypto_unregister_alg(&ctr_aes_alg);
979 free_page((unsigned long) ctrblk);
980 }
981 if (xts_aes_alg_reg)
982 crypto_unregister_alg(&xts_aes_alg);
a9e62fad
HX
983 crypto_unregister_alg(&cbc_aes_alg);
984 crypto_unregister_alg(&ecb_aes_alg);
bf754ae8
JG
985 crypto_unregister_alg(&aes_alg);
986}
987
d05377c1 988module_cpu_feature_match(MSA, aes_s390_init);
9f7819c1 989module_exit(aes_s390_fini);
bf754ae8 990
5d26a105 991MODULE_ALIAS_CRYPTO("aes-all");
bf754ae8
JG
992
993MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
994MODULE_LICENSE("GPL");
This page took 0.707266 seconds and 5 git commands to generate.