Merge tag 'usercopy-v5.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[linux-2.6-block.git] / arch / s390 / crypto / aes_s390.c
CommitLineData
20a884f5 1// SPDX-License-Identifier: GPL-2.0+
bf754ae8
JG
2/*
3 * Cryptographic API.
4 *
5 * s390 implementation of the AES Cipher Algorithm.
6 *
7 * s390 Version:
bf7fa038 8 * Copyright IBM Corp. 2005, 2017
bf754ae8 9 * Author(s): Jan Glauber (jang@de.ibm.com)
b0c3e75d 10 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
bf7fa038
HF
11 * Patrick Steuer <patrick.steuer@de.ibm.com>
12 * Harald Freudenberger <freude@de.ibm.com>
bf754ae8 13 *
f8246af0 14 * Derived from "crypto/aes_generic.c"
bf754ae8
JG
15 */
16
39f09392
JG
17#define KMSG_COMPONENT "aes_s390"
18#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19
89e12654 20#include <crypto/aes.h>
a9e62fad 21#include <crypto/algapi.h>
bf7fa038
HF
22#include <crypto/ghash.h>
23#include <crypto/internal/aead.h>
64e26807 24#include <crypto/internal/skcipher.h>
bf7fa038 25#include <crypto/scatterwalk.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>
1c2c7029 30#include <linux/mutex.h>
a4f2779e 31#include <linux/fips.h>
bf7fa038 32#include <linux/string.h>
49abc0d2 33#include <crypto/xts.h>
c7d4d259 34#include <asm/cpacf.h>
bf754ae8 35
0200f3ec 36static u8 *ctrblk;
1c2c7029 37static DEFINE_MUTEX(ctrblk_lock);
69c0e360 38
bf7fa038
HF
39static cpacf_mask_t km_functions, kmc_functions, kmctr_functions,
40 kma_functions;
bf754ae8
JG
41
42struct s390_aes_ctx {
bf754ae8
JG
43 u8 key[AES_MAX_KEY_SIZE];
44 int key_len;
edc63a37 45 unsigned long fc;
b0c3e75d 46 union {
531fa5d6 47 struct crypto_sync_skcipher *blk;
b0c3e75d
SS
48 struct crypto_cipher *cip;
49 } fallback;
bf754ae8
JG
50};
51
99d97222
GS
52struct s390_xts_ctx {
53 u8 key[32];
9dda2769 54 u8 pcc_key[32];
99d97222 55 int key_len;
edc63a37 56 unsigned long fc;
531fa5d6 57 struct crypto_sync_skcipher *fallback;
99d97222
GS
58};
59
bf7fa038
HF
60struct gcm_sg_walk {
61 struct scatter_walk walk;
62 unsigned int walk_bytes;
63 u8 *walk_ptr;
64 unsigned int walk_bytes_remain;
65 u8 buf[AES_BLOCK_SIZE];
66 unsigned int buf_bytes;
67 u8 *ptr;
68 unsigned int nbytes;
69};
70
b0c3e75d
SS
71static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
72 unsigned int key_len)
73{
74 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
75 int ret;
76
d7ac7690
RK
77 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
78 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
b0c3e75d
SS
79 CRYPTO_TFM_REQ_MASK);
80
81 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
82 if (ret) {
83 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
d7ac7690 84 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
b0c3e75d
SS
85 CRYPTO_TFM_RES_MASK);
86 }
87 return ret;
88}
89
90static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
91 unsigned int key_len)
92{
93 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
69c0e360 94 unsigned long fc;
b0c3e75d 95
69c0e360
MS
96 /* Pick the correct function code based on the key length */
97 fc = (key_len == 16) ? CPACF_KM_AES_128 :
98 (key_len == 24) ? CPACF_KM_AES_192 :
99 (key_len == 32) ? CPACF_KM_AES_256 : 0;
bf754ae8 100
69c0e360
MS
101 /* Check if the function code is available */
102 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
103 if (!sctx->fc)
104 return setkey_fallback_cip(tfm, in_key, key_len);
b0c3e75d 105
69c0e360
MS
106 sctx->key_len = key_len;
107 memcpy(sctx->key, in_key, key_len);
108 return 0;
bf754ae8
JG
109}
110
931c940f 111static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
bf754ae8 112{
e6a67ad0 113 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
bf754ae8 114
69c0e360 115 if (unlikely(!sctx->fc)) {
b0c3e75d
SS
116 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
117 return;
118 }
69c0e360 119 cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
bf754ae8
JG
120}
121
931c940f 122static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
bf754ae8 123{
e6a67ad0 124 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
bf754ae8 125
69c0e360 126 if (unlikely(!sctx->fc)) {
b0c3e75d
SS
127 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
128 return;
129 }
69c0e360
MS
130 cpacf_km(sctx->fc | CPACF_DECRYPT,
131 &sctx->key, out, in, AES_BLOCK_SIZE);
bf754ae8
JG
132}
133
b0c3e75d
SS
134static int fallback_init_cip(struct crypto_tfm *tfm)
135{
136 const char *name = tfm->__crt_alg->cra_name;
137 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
138
139 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
1ad0f160 140 CRYPTO_ALG_NEED_FALLBACK);
b0c3e75d
SS
141
142 if (IS_ERR(sctx->fallback.cip)) {
39f09392
JG
143 pr_err("Allocating AES fallback algorithm %s failed\n",
144 name);
b59cdcb3 145 return PTR_ERR(sctx->fallback.cip);
b0c3e75d
SS
146 }
147
148 return 0;
149}
150
151static void fallback_exit_cip(struct crypto_tfm *tfm)
152{
153 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
154
155 crypto_free_cipher(sctx->fallback.cip);
156 sctx->fallback.cip = NULL;
157}
bf754ae8
JG
158
159static struct crypto_alg aes_alg = {
160 .cra_name = "aes",
65b75c36 161 .cra_driver_name = "aes-s390",
c7d4d259 162 .cra_priority = 300,
f67d1369
JG
163 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
164 CRYPTO_ALG_NEED_FALLBACK,
bf754ae8
JG
165 .cra_blocksize = AES_BLOCK_SIZE,
166 .cra_ctxsize = sizeof(struct s390_aes_ctx),
167 .cra_module = THIS_MODULE,
b0c3e75d
SS
168 .cra_init = fallback_init_cip,
169 .cra_exit = fallback_exit_cip,
bf754ae8
JG
170 .cra_u = {
171 .cipher = {
172 .cia_min_keysize = AES_MIN_KEY_SIZE,
173 .cia_max_keysize = AES_MAX_KEY_SIZE,
174 .cia_setkey = aes_set_key,
931c940f
AB
175 .cia_encrypt = crypto_aes_encrypt,
176 .cia_decrypt = crypto_aes_decrypt,
bf754ae8
JG
177 }
178 }
179};
180
b0c3e75d
SS
181static int setkey_fallback_blk(struct crypto_tfm *tfm, const u8 *key,
182 unsigned int len)
183{
184 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
185 unsigned int ret;
186
531fa5d6
KC
187 crypto_sync_skcipher_clear_flags(sctx->fallback.blk,
188 CRYPTO_TFM_REQ_MASK);
189 crypto_sync_skcipher_set_flags(sctx->fallback.blk, tfm->crt_flags &
64e26807
HX
190 CRYPTO_TFM_REQ_MASK);
191
531fa5d6 192 ret = crypto_sync_skcipher_setkey(sctx->fallback.blk, key, len);
64e26807
HX
193
194 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
531fa5d6 195 tfm->crt_flags |= crypto_sync_skcipher_get_flags(sctx->fallback.blk) &
64e26807 196 CRYPTO_TFM_RES_MASK;
b0c3e75d 197
b0c3e75d
SS
198 return ret;
199}
200
201static int fallback_blk_dec(struct blkcipher_desc *desc,
202 struct scatterlist *dst, struct scatterlist *src,
203 unsigned int nbytes)
204{
205 unsigned int ret;
64e26807
HX
206 struct crypto_blkcipher *tfm = desc->tfm;
207 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
531fa5d6 208 SYNC_SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
b0c3e75d 209
531fa5d6 210 skcipher_request_set_sync_tfm(req, sctx->fallback.blk);
64e26807
HX
211 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
212 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
b0c3e75d 213
64e26807 214 ret = crypto_skcipher_decrypt(req);
b0c3e75d 215
64e26807 216 skcipher_request_zero(req);
b0c3e75d
SS
217 return ret;
218}
219
220static int fallback_blk_enc(struct blkcipher_desc *desc,
221 struct scatterlist *dst, struct scatterlist *src,
222 unsigned int nbytes)
223{
224 unsigned int ret;
64e26807
HX
225 struct crypto_blkcipher *tfm = desc->tfm;
226 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(tfm);
531fa5d6 227 SYNC_SKCIPHER_REQUEST_ON_STACK(req, sctx->fallback.blk);
b0c3e75d 228
531fa5d6 229 skcipher_request_set_sync_tfm(req, sctx->fallback.blk);
64e26807
HX
230 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
231 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
b0c3e75d 232
64e26807 233 ret = crypto_skcipher_encrypt(req);
b0c3e75d
SS
234 return ret;
235}
236
a9e62fad
HX
237static int ecb_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
238 unsigned int key_len)
239{
240 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
69c0e360 241 unsigned long fc;
b0c3e75d 242
69c0e360
MS
243 /* Pick the correct function code based on the key length */
244 fc = (key_len == 16) ? CPACF_KM_AES_128 :
245 (key_len == 24) ? CPACF_KM_AES_192 :
246 (key_len == 32) ? CPACF_KM_AES_256 : 0;
a9e62fad 247
69c0e360
MS
248 /* Check if the function code is available */
249 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
250 if (!sctx->fc)
251 return setkey_fallback_blk(tfm, in_key, key_len);
a9e62fad 252
69c0e360
MS
253 sctx->key_len = key_len;
254 memcpy(sctx->key, in_key, key_len);
255 return 0;
a9e62fad
HX
256}
257
7bac4f5b 258static int ecb_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
a9e62fad
HX
259 struct blkcipher_walk *walk)
260{
7bac4f5b
MS
261 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
262 unsigned int nbytes, n;
263 int ret;
a9e62fad 264
7bac4f5b
MS
265 ret = blkcipher_walk_virt(desc, walk);
266 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
a9e62fad 267 /* only use complete blocks */
7bac4f5b
MS
268 n = nbytes & ~(AES_BLOCK_SIZE - 1);
269 cpacf_km(sctx->fc | modifier, sctx->key,
270 walk->dst.virt.addr, walk->src.virt.addr, n);
271 ret = blkcipher_walk_done(desc, walk, nbytes - n);
a9e62fad
HX
272 }
273
274 return ret;
275}
276
277static int ecb_aes_encrypt(struct blkcipher_desc *desc,
278 struct scatterlist *dst, struct scatterlist *src,
279 unsigned int nbytes)
280{
281 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
282 struct blkcipher_walk walk;
283
69c0e360 284 if (unlikely(!sctx->fc))
b0c3e75d
SS
285 return fallback_blk_enc(desc, dst, src, nbytes);
286
a9e62fad 287 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 288 return ecb_aes_crypt(desc, 0, &walk);
a9e62fad
HX
289}
290
291static int ecb_aes_decrypt(struct blkcipher_desc *desc,
292 struct scatterlist *dst, struct scatterlist *src,
293 unsigned int nbytes)
294{
295 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
296 struct blkcipher_walk walk;
297
69c0e360 298 if (unlikely(!sctx->fc))
b0c3e75d
SS
299 return fallback_blk_dec(desc, dst, src, nbytes);
300
a9e62fad 301 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 302 return ecb_aes_crypt(desc, CPACF_DECRYPT, &walk);
a9e62fad
HX
303}
304
b0c3e75d
SS
305static int fallback_init_blk(struct crypto_tfm *tfm)
306{
307 const char *name = tfm->__crt_alg->cra_name;
308 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
309
531fa5d6 310 sctx->fallback.blk = crypto_alloc_sync_skcipher(name, 0,
64e26807 311 CRYPTO_ALG_NEED_FALLBACK);
b0c3e75d
SS
312
313 if (IS_ERR(sctx->fallback.blk)) {
39f09392
JG
314 pr_err("Allocating AES fallback algorithm %s failed\n",
315 name);
b0c3e75d
SS
316 return PTR_ERR(sctx->fallback.blk);
317 }
318
319 return 0;
320}
321
322static void fallback_exit_blk(struct crypto_tfm *tfm)
323{
324 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
325
531fa5d6 326 crypto_free_sync_skcipher(sctx->fallback.blk);
b0c3e75d
SS
327}
328
a9e62fad
HX
329static struct crypto_alg ecb_aes_alg = {
330 .cra_name = "ecb(aes)",
331 .cra_driver_name = "ecb-aes-s390",
aff304e7 332 .cra_priority = 401, /* combo: aes + ecb + 1 */
f67d1369
JG
333 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
334 CRYPTO_ALG_NEED_FALLBACK,
a9e62fad
HX
335 .cra_blocksize = AES_BLOCK_SIZE,
336 .cra_ctxsize = sizeof(struct s390_aes_ctx),
337 .cra_type = &crypto_blkcipher_type,
338 .cra_module = THIS_MODULE,
b0c3e75d
SS
339 .cra_init = fallback_init_blk,
340 .cra_exit = fallback_exit_blk,
a9e62fad
HX
341 .cra_u = {
342 .blkcipher = {
343 .min_keysize = AES_MIN_KEY_SIZE,
344 .max_keysize = AES_MAX_KEY_SIZE,
345 .setkey = ecb_aes_set_key,
346 .encrypt = ecb_aes_encrypt,
347 .decrypt = ecb_aes_decrypt,
348 }
349 }
350};
351
352static int cbc_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
353 unsigned int key_len)
354{
355 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
69c0e360 356 unsigned long fc;
b0c3e75d 357
69c0e360
MS
358 /* Pick the correct function code based on the key length */
359 fc = (key_len == 16) ? CPACF_KMC_AES_128 :
360 (key_len == 24) ? CPACF_KMC_AES_192 :
361 (key_len == 32) ? CPACF_KMC_AES_256 : 0;
a9e62fad 362
69c0e360
MS
363 /* Check if the function code is available */
364 sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
365 if (!sctx->fc)
366 return setkey_fallback_blk(tfm, in_key, key_len);
a9e62fad 367
69c0e360
MS
368 sctx->key_len = key_len;
369 memcpy(sctx->key, in_key, key_len);
370 return 0;
a9e62fad
HX
371}
372
7bac4f5b 373static int cbc_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
a9e62fad
HX
374 struct blkcipher_walk *walk)
375{
f262f0f5 376 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
7bac4f5b
MS
377 unsigned int nbytes, n;
378 int ret;
f262f0f5
HX
379 struct {
380 u8 iv[AES_BLOCK_SIZE];
381 u8 key[AES_MAX_KEY_SIZE];
382 } param;
a9e62fad 383
7bac4f5b 384 ret = blkcipher_walk_virt(desc, walk);
f262f0f5
HX
385 memcpy(param.iv, walk->iv, AES_BLOCK_SIZE);
386 memcpy(param.key, sctx->key, sctx->key_len);
7bac4f5b 387 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
a9e62fad 388 /* only use complete blocks */
7bac4f5b
MS
389 n = nbytes & ~(AES_BLOCK_SIZE - 1);
390 cpacf_kmc(sctx->fc | modifier, &param,
391 walk->dst.virt.addr, walk->src.virt.addr, n);
392 ret = blkcipher_walk_done(desc, walk, nbytes - n);
393 }
f262f0f5 394 memcpy(walk->iv, param.iv, AES_BLOCK_SIZE);
a9e62fad
HX
395 return ret;
396}
397
398static int cbc_aes_encrypt(struct blkcipher_desc *desc,
399 struct scatterlist *dst, struct scatterlist *src,
400 unsigned int nbytes)
401{
402 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
403 struct blkcipher_walk walk;
404
69c0e360 405 if (unlikely(!sctx->fc))
b0c3e75d
SS
406 return fallback_blk_enc(desc, dst, src, nbytes);
407
a9e62fad 408 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 409 return cbc_aes_crypt(desc, 0, &walk);
a9e62fad
HX
410}
411
412static int cbc_aes_decrypt(struct blkcipher_desc *desc,
413 struct scatterlist *dst, struct scatterlist *src,
414 unsigned int nbytes)
415{
416 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
417 struct blkcipher_walk walk;
418
69c0e360 419 if (unlikely(!sctx->fc))
b0c3e75d
SS
420 return fallback_blk_dec(desc, dst, src, nbytes);
421
a9e62fad 422 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 423 return cbc_aes_crypt(desc, CPACF_DECRYPT, &walk);
a9e62fad
HX
424}
425
426static struct crypto_alg cbc_aes_alg = {
427 .cra_name = "cbc(aes)",
428 .cra_driver_name = "cbc-aes-s390",
aff304e7 429 .cra_priority = 402, /* ecb-aes-s390 + 1 */
f67d1369
JG
430 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
431 CRYPTO_ALG_NEED_FALLBACK,
a9e62fad
HX
432 .cra_blocksize = AES_BLOCK_SIZE,
433 .cra_ctxsize = sizeof(struct s390_aes_ctx),
434 .cra_type = &crypto_blkcipher_type,
435 .cra_module = THIS_MODULE,
b0c3e75d
SS
436 .cra_init = fallback_init_blk,
437 .cra_exit = fallback_exit_blk,
a9e62fad
HX
438 .cra_u = {
439 .blkcipher = {
440 .min_keysize = AES_MIN_KEY_SIZE,
441 .max_keysize = AES_MAX_KEY_SIZE,
442 .ivsize = AES_BLOCK_SIZE,
443 .setkey = cbc_aes_set_key,
444 .encrypt = cbc_aes_encrypt,
445 .decrypt = cbc_aes_decrypt,
446 }
447 }
448};
449
99d97222
GS
450static int xts_fallback_setkey(struct crypto_tfm *tfm, const u8 *key,
451 unsigned int len)
452{
453 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
454 unsigned int ret;
455
531fa5d6
KC
456 crypto_sync_skcipher_clear_flags(xts_ctx->fallback,
457 CRYPTO_TFM_REQ_MASK);
458 crypto_sync_skcipher_set_flags(xts_ctx->fallback, tfm->crt_flags &
64e26807
HX
459 CRYPTO_TFM_REQ_MASK);
460
531fa5d6 461 ret = crypto_sync_skcipher_setkey(xts_ctx->fallback, key, len);
64e26807
HX
462
463 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
531fa5d6 464 tfm->crt_flags |= crypto_sync_skcipher_get_flags(xts_ctx->fallback) &
64e26807 465 CRYPTO_TFM_RES_MASK;
99d97222 466
99d97222
GS
467 return ret;
468}
469
470static int xts_fallback_decrypt(struct blkcipher_desc *desc,
471 struct scatterlist *dst, struct scatterlist *src,
472 unsigned int nbytes)
473{
64e26807
HX
474 struct crypto_blkcipher *tfm = desc->tfm;
475 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
531fa5d6 476 SYNC_SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
99d97222
GS
477 unsigned int ret;
478
531fa5d6 479 skcipher_request_set_sync_tfm(req, xts_ctx->fallback);
64e26807
HX
480 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
481 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
99d97222 482
64e26807 483 ret = crypto_skcipher_decrypt(req);
99d97222 484
64e26807 485 skcipher_request_zero(req);
99d97222
GS
486 return ret;
487}
488
489static int xts_fallback_encrypt(struct blkcipher_desc *desc,
490 struct scatterlist *dst, struct scatterlist *src,
491 unsigned int nbytes)
492{
64e26807
HX
493 struct crypto_blkcipher *tfm = desc->tfm;
494 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(tfm);
531fa5d6 495 SYNC_SKCIPHER_REQUEST_ON_STACK(req, xts_ctx->fallback);
99d97222
GS
496 unsigned int ret;
497
531fa5d6 498 skcipher_request_set_sync_tfm(req, xts_ctx->fallback);
64e26807
HX
499 skcipher_request_set_callback(req, desc->flags, NULL, NULL);
500 skcipher_request_set_crypt(req, src, dst, nbytes, desc->info);
99d97222 501
64e26807 502 ret = crypto_skcipher_encrypt(req);
99d97222 503
64e26807 504 skcipher_request_zero(req);
99d97222
GS
505 return ret;
506}
507
508static int xts_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
509 unsigned int key_len)
510{
511 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
69c0e360 512 unsigned long fc;
28856a9e
SM
513 int err;
514
ce68acbc 515 err = xts_fallback_setkey(tfm, in_key, key_len);
28856a9e
SM
516 if (err)
517 return err;
99d97222 518
a4f2779e
HF
519 /* In fips mode only 128 bit or 256 bit keys are valid */
520 if (fips_enabled && key_len != 32 && key_len != 64) {
521 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
522 return -EINVAL;
523 }
524
69c0e360
MS
525 /* Pick the correct function code based on the key length */
526 fc = (key_len == 32) ? CPACF_KM_XTS_128 :
527 (key_len == 64) ? CPACF_KM_XTS_256 : 0;
528
529 /* Check if the function code is available */
530 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
531 if (!xts_ctx->fc)
ce68acbc 532 return 0;
69c0e360
MS
533
534 /* Split the XTS key into the two subkeys */
535 key_len = key_len / 2;
99d97222 536 xts_ctx->key_len = key_len;
69c0e360
MS
537 memcpy(xts_ctx->key, in_key, key_len);
538 memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
99d97222
GS
539 return 0;
540}
541
7bac4f5b 542static int xts_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
99d97222
GS
543 struct blkcipher_walk *walk)
544{
7bac4f5b
MS
545 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
546 unsigned int offset, nbytes, n;
547 int ret;
548 struct {
549 u8 key[32];
550 u8 tweak[16];
551 u8 block[16];
552 u8 bit[16];
553 u8 xts[16];
554 } pcc_param;
9dda2769
GS
555 struct {
556 u8 key[32];
557 u8 init[16];
558 } xts_param;
99d97222 559
7bac4f5b
MS
560 ret = blkcipher_walk_virt(desc, walk);
561 offset = xts_ctx->key_len & 0x10;
9dda2769
GS
562 memset(pcc_param.block, 0, sizeof(pcc_param.block));
563 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
564 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
565 memcpy(pcc_param.tweak, walk->iv, sizeof(pcc_param.tweak));
69c0e360 566 memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
7bac4f5b 567 cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
99d97222 568
69c0e360 569 memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
9dda2769 570 memcpy(xts_param.init, pcc_param.xts, 16);
7bac4f5b
MS
571
572 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
99d97222
GS
573 /* only use complete blocks */
574 n = nbytes & ~(AES_BLOCK_SIZE - 1);
7bac4f5b
MS
575 cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
576 walk->dst.virt.addr, walk->src.virt.addr, n);
577 ret = blkcipher_walk_done(desc, walk, nbytes - n);
578 }
99d97222
GS
579 return ret;
580}
581
582static int xts_aes_encrypt(struct blkcipher_desc *desc,
583 struct scatterlist *dst, struct scatterlist *src,
584 unsigned int nbytes)
585{
586 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
587 struct blkcipher_walk walk;
588
9e323d45
HF
589 if (!nbytes)
590 return -EINVAL;
591
5a74362c 592 if (unlikely(!xts_ctx->fc || (nbytes % XTS_BLOCK_SIZE) != 0))
99d97222
GS
593 return xts_fallback_encrypt(desc, dst, src, nbytes);
594
595 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 596 return xts_aes_crypt(desc, 0, &walk);
99d97222
GS
597}
598
599static int xts_aes_decrypt(struct blkcipher_desc *desc,
600 struct scatterlist *dst, struct scatterlist *src,
601 unsigned int nbytes)
602{
603 struct s390_xts_ctx *xts_ctx = crypto_blkcipher_ctx(desc->tfm);
604 struct blkcipher_walk walk;
605
9e323d45
HF
606 if (!nbytes)
607 return -EINVAL;
608
5a74362c 609 if (unlikely(!xts_ctx->fc || (nbytes % XTS_BLOCK_SIZE) != 0))
99d97222
GS
610 return xts_fallback_decrypt(desc, dst, src, nbytes);
611
612 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 613 return xts_aes_crypt(desc, CPACF_DECRYPT, &walk);
99d97222
GS
614}
615
616static int xts_fallback_init(struct crypto_tfm *tfm)
617{
618 const char *name = tfm->__crt_alg->cra_name;
619 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
620
531fa5d6 621 xts_ctx->fallback = crypto_alloc_sync_skcipher(name, 0,
64e26807 622 CRYPTO_ALG_NEED_FALLBACK);
99d97222
GS
623
624 if (IS_ERR(xts_ctx->fallback)) {
625 pr_err("Allocating XTS fallback algorithm %s failed\n",
626 name);
627 return PTR_ERR(xts_ctx->fallback);
628 }
629 return 0;
630}
631
632static void xts_fallback_exit(struct crypto_tfm *tfm)
633{
634 struct s390_xts_ctx *xts_ctx = crypto_tfm_ctx(tfm);
635
531fa5d6 636 crypto_free_sync_skcipher(xts_ctx->fallback);
99d97222
GS
637}
638
639static struct crypto_alg xts_aes_alg = {
640 .cra_name = "xts(aes)",
641 .cra_driver_name = "xts-aes-s390",
aff304e7 642 .cra_priority = 402, /* ecb-aes-s390 + 1 */
99d97222
GS
643 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
644 CRYPTO_ALG_NEED_FALLBACK,
645 .cra_blocksize = AES_BLOCK_SIZE,
646 .cra_ctxsize = sizeof(struct s390_xts_ctx),
647 .cra_type = &crypto_blkcipher_type,
648 .cra_module = THIS_MODULE,
99d97222
GS
649 .cra_init = xts_fallback_init,
650 .cra_exit = xts_fallback_exit,
651 .cra_u = {
652 .blkcipher = {
653 .min_keysize = 2 * AES_MIN_KEY_SIZE,
654 .max_keysize = 2 * AES_MAX_KEY_SIZE,
655 .ivsize = AES_BLOCK_SIZE,
656 .setkey = xts_aes_set_key,
657 .encrypt = xts_aes_encrypt,
658 .decrypt = xts_aes_decrypt,
659 }
660 }
661};
662
0200f3ec
GS
663static int ctr_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
664 unsigned int key_len)
665{
666 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
69c0e360 667 unsigned long fc;
0200f3ec 668
69c0e360
MS
669 /* Pick the correct function code based on the key length */
670 fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
671 (key_len == 24) ? CPACF_KMCTR_AES_192 :
672 (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
673
674 /* Check if the function code is available */
675 sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
676 if (!sctx->fc)
677 return setkey_fallback_blk(tfm, in_key, key_len);
0200f3ec 678
69c0e360
MS
679 sctx->key_len = key_len;
680 memcpy(sctx->key, in_key, key_len);
681 return 0;
0200f3ec
GS
682}
683
7bac4f5b 684static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
0519e9ad
HF
685{
686 unsigned int i, n;
687
688 /* only use complete blocks, max. PAGE_SIZE */
7bac4f5b 689 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
0519e9ad 690 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
7bac4f5b
MS
691 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
692 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
693 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
694 ctrptr += AES_BLOCK_SIZE;
0519e9ad
HF
695 }
696 return n;
697}
698
7bac4f5b
MS
699static int ctr_aes_crypt(struct blkcipher_desc *desc, unsigned long modifier,
700 struct blkcipher_walk *walk)
0200f3ec 701{
7bac4f5b
MS
702 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
703 u8 buf[AES_BLOCK_SIZE], *ctrptr;
0519e9ad 704 unsigned int n, nbytes;
7bac4f5b 705 int ret, locked;
0200f3ec 706
1c2c7029 707 locked = mutex_trylock(&ctrblk_lock);
0519e9ad 708
7bac4f5b 709 ret = blkcipher_walk_virt_block(desc, walk, AES_BLOCK_SIZE);
0200f3ec 710 while ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE) {
7bac4f5b
MS
711 n = AES_BLOCK_SIZE;
712 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
713 n = __ctrblk_init(ctrblk, walk->iv, nbytes);
714 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
715 cpacf_kmctr(sctx->fc | modifier, sctx->key,
716 walk->dst.virt.addr, walk->src.virt.addr,
717 n, ctrptr);
718 if (ctrptr == ctrblk)
719 memcpy(walk->iv, ctrptr + n - AES_BLOCK_SIZE,
720 AES_BLOCK_SIZE);
721 crypto_inc(walk->iv, AES_BLOCK_SIZE);
722 ret = blkcipher_walk_done(desc, walk, nbytes - n);
0200f3ec 723 }
7bac4f5b 724 if (locked)
1c2c7029 725 mutex_unlock(&ctrblk_lock);
0200f3ec
GS
726 /*
727 * final block may be < AES_BLOCK_SIZE, copy only nbytes
728 */
729 if (nbytes) {
7bac4f5b
MS
730 cpacf_kmctr(sctx->fc | modifier, sctx->key,
731 buf, walk->src.virt.addr,
732 AES_BLOCK_SIZE, walk->iv);
733 memcpy(walk->dst.virt.addr, buf, nbytes);
734 crypto_inc(walk->iv, AES_BLOCK_SIZE);
0200f3ec
GS
735 ret = blkcipher_walk_done(desc, walk, 0);
736 }
0519e9ad 737
0200f3ec
GS
738 return ret;
739}
740
741static int ctr_aes_encrypt(struct blkcipher_desc *desc,
742 struct scatterlist *dst, struct scatterlist *src,
743 unsigned int nbytes)
744{
745 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
746 struct blkcipher_walk walk;
747
69c0e360
MS
748 if (unlikely(!sctx->fc))
749 return fallback_blk_enc(desc, dst, src, nbytes);
750
0200f3ec 751 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 752 return ctr_aes_crypt(desc, 0, &walk);
0200f3ec
GS
753}
754
755static int ctr_aes_decrypt(struct blkcipher_desc *desc,
756 struct scatterlist *dst, struct scatterlist *src,
757 unsigned int nbytes)
758{
759 struct s390_aes_ctx *sctx = crypto_blkcipher_ctx(desc->tfm);
760 struct blkcipher_walk walk;
761
69c0e360
MS
762 if (unlikely(!sctx->fc))
763 return fallback_blk_dec(desc, dst, src, nbytes);
764
0200f3ec 765 blkcipher_walk_init(&walk, dst, src, nbytes);
7bac4f5b 766 return ctr_aes_crypt(desc, CPACF_DECRYPT, &walk);
0200f3ec
GS
767}
768
769static struct crypto_alg ctr_aes_alg = {
770 .cra_name = "ctr(aes)",
771 .cra_driver_name = "ctr-aes-s390",
aff304e7 772 .cra_priority = 402, /* ecb-aes-s390 + 1 */
69c0e360
MS
773 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
774 CRYPTO_ALG_NEED_FALLBACK,
0200f3ec
GS
775 .cra_blocksize = 1,
776 .cra_ctxsize = sizeof(struct s390_aes_ctx),
777 .cra_type = &crypto_blkcipher_type,
778 .cra_module = THIS_MODULE,
69c0e360
MS
779 .cra_init = fallback_init_blk,
780 .cra_exit = fallback_exit_blk,
0200f3ec
GS
781 .cra_u = {
782 .blkcipher = {
783 .min_keysize = AES_MIN_KEY_SIZE,
784 .max_keysize = AES_MAX_KEY_SIZE,
785 .ivsize = AES_BLOCK_SIZE,
786 .setkey = ctr_aes_set_key,
787 .encrypt = ctr_aes_encrypt,
788 .decrypt = ctr_aes_decrypt,
789 }
790 }
791};
792
bf7fa038
HF
793static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key,
794 unsigned int keylen)
795{
796 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
797
798 switch (keylen) {
799 case AES_KEYSIZE_128:
800 ctx->fc = CPACF_KMA_GCM_AES_128;
801 break;
802 case AES_KEYSIZE_192:
803 ctx->fc = CPACF_KMA_GCM_AES_192;
804 break;
805 case AES_KEYSIZE_256:
806 ctx->fc = CPACF_KMA_GCM_AES_256;
807 break;
808 default:
809 return -EINVAL;
810 }
811
812 memcpy(ctx->key, key, keylen);
813 ctx->key_len = keylen;
814 return 0;
815}
816
817static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
818{
819 switch (authsize) {
820 case 4:
821 case 8:
822 case 12:
823 case 13:
824 case 14:
825 case 15:
826 case 16:
827 break;
828 default:
829 return -EINVAL;
830 }
831
832 return 0;
833}
834
bef9f0ba
HF
835static void gcm_walk_start(struct gcm_sg_walk *gw, struct scatterlist *sg,
836 unsigned int len)
bf7fa038
HF
837{
838 memset(gw, 0, sizeof(*gw));
839 gw->walk_bytes_remain = len;
840 scatterwalk_start(&gw->walk, sg);
841}
842
bef9f0ba
HF
843static inline unsigned int _gcm_sg_clamp_and_map(struct gcm_sg_walk *gw)
844{
845 struct scatterlist *nextsg;
846
847 gw->walk_bytes = scatterwalk_clamp(&gw->walk, gw->walk_bytes_remain);
848 while (!gw->walk_bytes) {
849 nextsg = sg_next(gw->walk.sg);
850 if (!nextsg)
851 return 0;
852 scatterwalk_start(&gw->walk, nextsg);
853 gw->walk_bytes = scatterwalk_clamp(&gw->walk,
854 gw->walk_bytes_remain);
855 }
856 gw->walk_ptr = scatterwalk_map(&gw->walk);
857 return gw->walk_bytes;
858}
859
860static inline void _gcm_sg_unmap_and_advance(struct gcm_sg_walk *gw,
861 unsigned int nbytes)
862{
863 gw->walk_bytes_remain -= nbytes;
864 scatterwalk_unmap(&gw->walk);
865 scatterwalk_advance(&gw->walk, nbytes);
866 scatterwalk_done(&gw->walk, 0, gw->walk_bytes_remain);
867 gw->walk_ptr = NULL;
868}
869
870static int gcm_in_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
bf7fa038
HF
871{
872 int n;
873
bf7fa038
HF
874 if (gw->buf_bytes && gw->buf_bytes >= minbytesneeded) {
875 gw->ptr = gw->buf;
876 gw->nbytes = gw->buf_bytes;
877 goto out;
878 }
879
880 if (gw->walk_bytes_remain == 0) {
881 gw->ptr = NULL;
882 gw->nbytes = 0;
883 goto out;
884 }
885
bef9f0ba
HF
886 if (!_gcm_sg_clamp_and_map(gw)) {
887 gw->ptr = NULL;
888 gw->nbytes = 0;
889 goto out;
bf7fa038 890 }
bf7fa038
HF
891
892 if (!gw->buf_bytes && gw->walk_bytes >= minbytesneeded) {
893 gw->ptr = gw->walk_ptr;
894 gw->nbytes = gw->walk_bytes;
895 goto out;
896 }
897
898 while (1) {
899 n = min(gw->walk_bytes, AES_BLOCK_SIZE - gw->buf_bytes);
900 memcpy(gw->buf + gw->buf_bytes, gw->walk_ptr, n);
901 gw->buf_bytes += n;
bef9f0ba 902 _gcm_sg_unmap_and_advance(gw, n);
bf7fa038
HF
903 if (gw->buf_bytes >= minbytesneeded) {
904 gw->ptr = gw->buf;
905 gw->nbytes = gw->buf_bytes;
906 goto out;
907 }
bef9f0ba
HF
908 if (!_gcm_sg_clamp_and_map(gw)) {
909 gw->ptr = NULL;
910 gw->nbytes = 0;
911 goto out;
bf7fa038 912 }
bf7fa038
HF
913 }
914
915out:
916 return gw->nbytes;
917}
918
bef9f0ba 919static int gcm_out_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
bf7fa038 920{
bef9f0ba
HF
921 if (gw->walk_bytes_remain == 0) {
922 gw->ptr = NULL;
923 gw->nbytes = 0;
924 goto out;
925 }
bf7fa038 926
bef9f0ba
HF
927 if (!_gcm_sg_clamp_and_map(gw)) {
928 gw->ptr = NULL;
929 gw->nbytes = 0;
930 goto out;
931 }
932
933 if (gw->walk_bytes >= minbytesneeded) {
934 gw->ptr = gw->walk_ptr;
935 gw->nbytes = gw->walk_bytes;
936 goto out;
937 }
938
939 scatterwalk_unmap(&gw->walk);
940 gw->walk_ptr = NULL;
941
942 gw->ptr = gw->buf;
943 gw->nbytes = sizeof(gw->buf);
944
945out:
946 return gw->nbytes;
947}
948
949static int gcm_in_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
950{
bf7fa038 951 if (gw->ptr == NULL)
bef9f0ba 952 return 0;
bf7fa038
HF
953
954 if (gw->ptr == gw->buf) {
bef9f0ba 955 int n = gw->buf_bytes - bytesdone;
bf7fa038
HF
956 if (n > 0) {
957 memmove(gw->buf, gw->buf + bytesdone, n);
bef9f0ba 958 gw->buf_bytes = n;
bf7fa038
HF
959 } else
960 gw->buf_bytes = 0;
bef9f0ba
HF
961 } else
962 _gcm_sg_unmap_and_advance(gw, bytesdone);
963
964 return bytesdone;
965}
966
967static int gcm_out_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
968{
969 int i, n;
970
971 if (gw->ptr == NULL)
972 return 0;
973
974 if (gw->ptr == gw->buf) {
975 for (i = 0; i < bytesdone; i += n) {
976 if (!_gcm_sg_clamp_and_map(gw))
977 return i;
978 n = min(gw->walk_bytes, bytesdone - i);
979 memcpy(gw->walk_ptr, gw->buf + i, n);
980 _gcm_sg_unmap_and_advance(gw, n);
981 }
982 } else
983 _gcm_sg_unmap_and_advance(gw, bytesdone);
984
985 return bytesdone;
bf7fa038
HF
986}
987
988static int gcm_aes_crypt(struct aead_request *req, unsigned int flags)
989{
990 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
991 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
992 unsigned int ivsize = crypto_aead_ivsize(tfm);
993 unsigned int taglen = crypto_aead_authsize(tfm);
994 unsigned int aadlen = req->assoclen;
995 unsigned int pclen = req->cryptlen;
996 int ret = 0;
997
bef9f0ba 998 unsigned int n, len, in_bytes, out_bytes,
bf7fa038
HF
999 min_bytes, bytes, aad_bytes, pc_bytes;
1000 struct gcm_sg_walk gw_in, gw_out;
1001 u8 tag[GHASH_DIGEST_SIZE];
1002
1003 struct {
1004 u32 _[3]; /* reserved */
1005 u32 cv; /* Counter Value */
1006 u8 t[GHASH_DIGEST_SIZE];/* Tag */
1007 u8 h[AES_BLOCK_SIZE]; /* Hash-subkey */
1008 u64 taadl; /* Total AAD Length */
1009 u64 tpcl; /* Total Plain-/Cipher-text Length */
1010 u8 j0[GHASH_BLOCK_SIZE];/* initial counter value */
1011 u8 k[AES_MAX_KEY_SIZE]; /* Key */
1012 } param;
1013
1014 /*
1015 * encrypt
1016 * req->src: aad||plaintext
1017 * req->dst: aad||ciphertext||tag
1018 * decrypt
1019 * req->src: aad||ciphertext||tag
1020 * req->dst: aad||plaintext, return 0 or -EBADMSG
1021 * aad, plaintext and ciphertext may be empty.
1022 */
1023 if (flags & CPACF_DECRYPT)
1024 pclen -= taglen;
1025 len = aadlen + pclen;
1026
1027 memset(&param, 0, sizeof(param));
1028 param.cv = 1;
1029 param.taadl = aadlen * 8;
1030 param.tpcl = pclen * 8;
1031 memcpy(param.j0, req->iv, ivsize);
1032 *(u32 *)(param.j0 + ivsize) = 1;
1033 memcpy(param.k, ctx->key, ctx->key_len);
1034
bef9f0ba
HF
1035 gcm_walk_start(&gw_in, req->src, len);
1036 gcm_walk_start(&gw_out, req->dst, len);
bf7fa038
HF
1037
1038 do {
1039 min_bytes = min_t(unsigned int,
1040 aadlen > 0 ? aadlen : pclen, AES_BLOCK_SIZE);
bef9f0ba
HF
1041 in_bytes = gcm_in_walk_go(&gw_in, min_bytes);
1042 out_bytes = gcm_out_walk_go(&gw_out, min_bytes);
bf7fa038
HF
1043 bytes = min(in_bytes, out_bytes);
1044
1045 if (aadlen + pclen <= bytes) {
1046 aad_bytes = aadlen;
1047 pc_bytes = pclen;
1048 flags |= CPACF_KMA_LAAD | CPACF_KMA_LPC;
1049 } else {
1050 if (aadlen <= bytes) {
1051 aad_bytes = aadlen;
1052 pc_bytes = (bytes - aadlen) &
1053 ~(AES_BLOCK_SIZE - 1);
1054 flags |= CPACF_KMA_LAAD;
1055 } else {
1056 aad_bytes = bytes & ~(AES_BLOCK_SIZE - 1);
1057 pc_bytes = 0;
1058 }
1059 }
1060
1061 if (aad_bytes > 0)
1062 memcpy(gw_out.ptr, gw_in.ptr, aad_bytes);
1063
1064 cpacf_kma(ctx->fc | flags, &param,
1065 gw_out.ptr + aad_bytes,
1066 gw_in.ptr + aad_bytes, pc_bytes,
1067 gw_in.ptr, aad_bytes);
1068
bef9f0ba
HF
1069 n = aad_bytes + pc_bytes;
1070 if (gcm_in_walk_done(&gw_in, n) != n)
1071 return -ENOMEM;
1072 if (gcm_out_walk_done(&gw_out, n) != n)
1073 return -ENOMEM;
bf7fa038
HF
1074 aadlen -= aad_bytes;
1075 pclen -= pc_bytes;
1076 } while (aadlen + pclen > 0);
1077
1078 if (flags & CPACF_DECRYPT) {
1079 scatterwalk_map_and_copy(tag, req->src, len, taglen, 0);
1080 if (crypto_memneq(tag, param.t, taglen))
1081 ret = -EBADMSG;
1082 } else
1083 scatterwalk_map_and_copy(param.t, req->dst, len, taglen, 1);
1084
1085 memzero_explicit(&param, sizeof(param));
1086 return ret;
1087}
1088
1089static int gcm_aes_encrypt(struct aead_request *req)
1090{
1091 return gcm_aes_crypt(req, CPACF_ENCRYPT);
1092}
1093
1094static int gcm_aes_decrypt(struct aead_request *req)
1095{
1096 return gcm_aes_crypt(req, CPACF_DECRYPT);
1097}
1098
1099static struct aead_alg gcm_aes_aead = {
1100 .setkey = gcm_aes_setkey,
1101 .setauthsize = gcm_aes_setauthsize,
1102 .encrypt = gcm_aes_encrypt,
1103 .decrypt = gcm_aes_decrypt,
1104
1105 .ivsize = GHASH_BLOCK_SIZE - sizeof(u32),
1106 .maxauthsize = GHASH_DIGEST_SIZE,
1107 .chunksize = AES_BLOCK_SIZE,
1108
1109 .base = {
bf7fa038
HF
1110 .cra_blocksize = 1,
1111 .cra_ctxsize = sizeof(struct s390_aes_ctx),
1112 .cra_priority = 900,
1113 .cra_name = "gcm(aes)",
1114 .cra_driver_name = "gcm-aes-s390",
1115 .cra_module = THIS_MODULE,
1116 },
1117};
1118
d863d594
MS
1119static struct crypto_alg *aes_s390_algs_ptr[5];
1120static int aes_s390_algs_num;
c7260ca3 1121static struct aead_alg *aes_s390_aead_alg;
d863d594
MS
1122
1123static int aes_s390_register_alg(struct crypto_alg *alg)
1124{
1125 int ret;
1126
1127 ret = crypto_register_alg(alg);
1128 if (!ret)
1129 aes_s390_algs_ptr[aes_s390_algs_num++] = alg;
1130 return ret;
1131}
1132
1133static void aes_s390_fini(void)
1134{
1135 while (aes_s390_algs_num--)
1136 crypto_unregister_alg(aes_s390_algs_ptr[aes_s390_algs_num]);
1137 if (ctrblk)
1138 free_page((unsigned long) ctrblk);
bf7fa038 1139
c7260ca3
HF
1140 if (aes_s390_aead_alg)
1141 crypto_unregister_aead(aes_s390_aead_alg);
d863d594 1142}
4f57ba71 1143
9f7819c1 1144static int __init aes_s390_init(void)
bf754ae8
JG
1145{
1146 int ret;
1147
bf7fa038 1148 /* Query available functions for KM, KMC, KMCTR and KMA */
69c0e360
MS
1149 cpacf_query(CPACF_KM, &km_functions);
1150 cpacf_query(CPACF_KMC, &kmc_functions);
1151 cpacf_query(CPACF_KMCTR, &kmctr_functions);
bf7fa038 1152 cpacf_query(CPACF_KMA, &kma_functions);
a9e62fad 1153
69c0e360
MS
1154 if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
1155 cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
1156 cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
1157 ret = aes_s390_register_alg(&aes_alg);
1158 if (ret)
1159 goto out_err;
1160 ret = aes_s390_register_alg(&ecb_aes_alg);
1161 if (ret)
1162 goto out_err;
1163 }
a9e62fad 1164
69c0e360
MS
1165 if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
1166 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
1167 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
1168 ret = aes_s390_register_alg(&cbc_aes_alg);
1169 if (ret)
1170 goto out_err;
1171 }
a9e62fad 1172
69c0e360
MS
1173 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
1174 cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
d863d594 1175 ret = aes_s390_register_alg(&xts_aes_alg);
99d97222 1176 if (ret)
d863d594 1177 goto out_err;
99d97222
GS
1178 }
1179
69c0e360
MS
1180 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
1181 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
1182 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
0200f3ec
GS
1183 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
1184 if (!ctrblk) {
1185 ret = -ENOMEM;
d863d594 1186 goto out_err;
0200f3ec 1187 }
d863d594
MS
1188 ret = aes_s390_register_alg(&ctr_aes_alg);
1189 if (ret)
1190 goto out_err;
0200f3ec
GS
1191 }
1192
bf7fa038
HF
1193 if (cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_128) ||
1194 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_192) ||
1195 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_256)) {
1196 ret = crypto_register_aead(&gcm_aes_aead);
1197 if (ret)
1198 goto out_err;
c7260ca3 1199 aes_s390_aead_alg = &gcm_aes_aead;
bf7fa038
HF
1200 }
1201
d863d594
MS
1202 return 0;
1203out_err:
1204 aes_s390_fini();
bf754ae8 1205 return ret;
bf754ae8
JG
1206}
1207
d05377c1 1208module_cpu_feature_match(MSA, aes_s390_init);
9f7819c1 1209module_exit(aes_s390_fini);
bf754ae8 1210
5d26a105 1211MODULE_ALIAS_CRYPTO("aes-all");
bf754ae8
JG
1212
1213MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
1214MODULE_LICENSE("GPL");