1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Cryptographic API.
4 *
5 * s390 implementation of the AES Cipher Algorithm.
6 *
7 * s390 Version:
8 * Copyright IBM Corp. 2005, 2017
9 * Author(s): Jan Glauber (jang@de.ibm.com)
10 * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
11 * Patrick Steuer <patrick.steuer@de.ibm.com>
12 * Harald Freudenberger <freude@de.ibm.com>
13 *
14 * Derived from "crypto/aes_generic.c"
15 */
16
17 #define pr_fmt(fmt) "aes_s390: " fmt
18
19 #include <crypto/aes.h>
20 #include <crypto/algapi.h>
21 #include <crypto/ghash.h>
22 #include <crypto/internal/aead.h>
23 #include <crypto/internal/skcipher.h>
24 #include <crypto/scatterwalk.h>
25 #include <linux/err.h>
26 #include <linux/module.h>
27 #include <linux/cpufeature.h>
28 #include <linux/init.h>
29 #include <linux/fips.h>
30 #include <linux/semaphore.h>
31 #include <linux/string.h>
32 #include <crypto/xts.h>
33 #include <asm/cpacf.h>
34
35 static u8 *ctrblk;
36 static DEFINE_SEMAPHORE(ctrblk_sem, 1);
37
38 static cpacf_mask_t km_functions, kmc_functions, kmctr_functions,
39 kma_functions;
40
41 struct s390_aes_ctx {
42 u8 key[AES_MAX_KEY_SIZE];
43 int key_len;
44 unsigned long fc;
45 union {
46 struct crypto_skcipher *skcipher;
47 } fallback;
48 };
49
50 struct s390_xts_ctx {
51 union {
52 u8 keys[64];
53 struct {
54 u8 key[32];
55 u8 pcc_key[32];
56 };
57 };
58 int key_len;
59 unsigned long fc;
60 struct crypto_skcipher *fallback;
61 };
62
63 struct gcm_sg_walk {
64 struct scatter_walk walk;
65 unsigned int walk_bytes;
66 unsigned int walk_bytes_remain;
67 u8 buf[AES_BLOCK_SIZE];
68 unsigned int buf_bytes;
69 u8 *ptr;
70 unsigned int nbytes;
71 };
72
setkey_fallback_skcipher(struct crypto_skcipher * tfm,const u8 * key,unsigned int len)73 static int setkey_fallback_skcipher(struct crypto_skcipher *tfm, const u8 *key,
74 unsigned int len)
75 {
76 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
77
78 crypto_skcipher_clear_flags(sctx->fallback.skcipher,
79 CRYPTO_TFM_REQ_MASK);
80 crypto_skcipher_set_flags(sctx->fallback.skcipher,
81 crypto_skcipher_get_flags(tfm) &
82 CRYPTO_TFM_REQ_MASK);
83 return crypto_skcipher_setkey(sctx->fallback.skcipher, key, len);
84 }
85
fallback_skcipher_crypt(struct s390_aes_ctx * sctx,struct skcipher_request * req,unsigned long modifier)86 static int fallback_skcipher_crypt(struct s390_aes_ctx *sctx,
87 struct skcipher_request *req,
88 unsigned long modifier)
89 {
90 struct skcipher_request *subreq = skcipher_request_ctx(req);
91
92 *subreq = *req;
93 skcipher_request_set_tfm(subreq, sctx->fallback.skcipher);
94 return (modifier & CPACF_DECRYPT) ?
95 crypto_skcipher_decrypt(subreq) :
96 crypto_skcipher_encrypt(subreq);
97 }
98
ecb_aes_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)99 static int ecb_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
100 unsigned int key_len)
101 {
102 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
103 unsigned long fc;
104
105 /* Pick the correct function code based on the key length */
106 fc = (key_len == 16) ? CPACF_KM_AES_128 :
107 (key_len == 24) ? CPACF_KM_AES_192 :
108 (key_len == 32) ? CPACF_KM_AES_256 : 0;
109
110 /* Check if the function code is available */
111 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
112 if (!sctx->fc)
113 return setkey_fallback_skcipher(tfm, in_key, key_len);
114
115 sctx->key_len = key_len;
116 memcpy(sctx->key, in_key, key_len);
117 return 0;
118 }
119
ecb_aes_crypt(struct skcipher_request * req,unsigned long modifier)120 static int ecb_aes_crypt(struct skcipher_request *req, unsigned long modifier)
121 {
122 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
123 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
124 struct skcipher_walk walk;
125 unsigned int nbytes, n;
126 int ret;
127
128 if (unlikely(!sctx->fc))
129 return fallback_skcipher_crypt(sctx, req, modifier);
130
131 ret = skcipher_walk_virt(&walk, req, false);
132 while (!ret && ((nbytes = walk.nbytes) != 0)) {
133 /* only use complete blocks */
134 n = nbytes & ~(AES_BLOCK_SIZE - 1);
135 cpacf_km(sctx->fc | modifier, sctx->key,
136 walk.dst.virt.addr, walk.src.virt.addr, n);
137 ret = skcipher_walk_done(&walk, nbytes - n);
138 }
139 return ret;
140 }
141
ecb_aes_encrypt(struct skcipher_request * req)142 static int ecb_aes_encrypt(struct skcipher_request *req)
143 {
144 return ecb_aes_crypt(req, 0);
145 }
146
ecb_aes_decrypt(struct skcipher_request * req)147 static int ecb_aes_decrypt(struct skcipher_request *req)
148 {
149 return ecb_aes_crypt(req, CPACF_DECRYPT);
150 }
151
fallback_init_skcipher(struct crypto_skcipher * tfm)152 static int fallback_init_skcipher(struct crypto_skcipher *tfm)
153 {
154 const char *name = crypto_tfm_alg_name(&tfm->base);
155 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
156
157 sctx->fallback.skcipher = crypto_alloc_skcipher(name, 0,
158 CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
159
160 if (IS_ERR(sctx->fallback.skcipher)) {
161 pr_err("Allocating AES fallback algorithm %s failed\n",
162 name);
163 return PTR_ERR(sctx->fallback.skcipher);
164 }
165
166 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
167 crypto_skcipher_reqsize(sctx->fallback.skcipher));
168 return 0;
169 }
170
fallback_exit_skcipher(struct crypto_skcipher * tfm)171 static void fallback_exit_skcipher(struct crypto_skcipher *tfm)
172 {
173 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
174
175 crypto_free_skcipher(sctx->fallback.skcipher);
176 }
177
178 static struct skcipher_alg ecb_aes_alg = {
179 .base.cra_name = "ecb(aes)",
180 .base.cra_driver_name = "ecb-aes-s390",
181 .base.cra_priority = 401, /* combo: aes + ecb + 1 */
182 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
183 .base.cra_blocksize = AES_BLOCK_SIZE,
184 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
185 .base.cra_module = THIS_MODULE,
186 .init = fallback_init_skcipher,
187 .exit = fallback_exit_skcipher,
188 .min_keysize = AES_MIN_KEY_SIZE,
189 .max_keysize = AES_MAX_KEY_SIZE,
190 .setkey = ecb_aes_set_key,
191 .encrypt = ecb_aes_encrypt,
192 .decrypt = ecb_aes_decrypt,
193 };
194
cbc_aes_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)195 static int cbc_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
196 unsigned int key_len)
197 {
198 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
199 unsigned long fc;
200
201 /* Pick the correct function code based on the key length */
202 fc = (key_len == 16) ? CPACF_KMC_AES_128 :
203 (key_len == 24) ? CPACF_KMC_AES_192 :
204 (key_len == 32) ? CPACF_KMC_AES_256 : 0;
205
206 /* Check if the function code is available */
207 sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
208 if (!sctx->fc)
209 return setkey_fallback_skcipher(tfm, in_key, key_len);
210
211 sctx->key_len = key_len;
212 memcpy(sctx->key, in_key, key_len);
213 return 0;
214 }
215
cbc_aes_crypt(struct skcipher_request * req,unsigned long modifier)216 static int cbc_aes_crypt(struct skcipher_request *req, unsigned long modifier)
217 {
218 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
219 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
220 struct skcipher_walk walk;
221 unsigned int nbytes, n;
222 int ret;
223 struct {
224 u8 iv[AES_BLOCK_SIZE];
225 u8 key[AES_MAX_KEY_SIZE];
226 } param;
227
228 if (unlikely(!sctx->fc))
229 return fallback_skcipher_crypt(sctx, req, modifier);
230
231 ret = skcipher_walk_virt(&walk, req, false);
232 if (ret)
233 return ret;
234 memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
235 memcpy(param.key, sctx->key, sctx->key_len);
236 while (!ret && ((nbytes = walk.nbytes) != 0)) {
237 /* only use complete blocks */
238 n = nbytes & ~(AES_BLOCK_SIZE - 1);
239 cpacf_kmc(sctx->fc | modifier, ¶m,
240 walk.dst.virt.addr, walk.src.virt.addr, n);
241 memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
242 ret = skcipher_walk_done(&walk, nbytes - n);
243 }
244 memzero_explicit(¶m, sizeof(param));
245 return ret;
246 }
247
cbc_aes_encrypt(struct skcipher_request * req)248 static int cbc_aes_encrypt(struct skcipher_request *req)
249 {
250 return cbc_aes_crypt(req, 0);
251 }
252
cbc_aes_decrypt(struct skcipher_request * req)253 static int cbc_aes_decrypt(struct skcipher_request *req)
254 {
255 return cbc_aes_crypt(req, CPACF_DECRYPT);
256 }
257
258 static struct skcipher_alg cbc_aes_alg = {
259 .base.cra_name = "cbc(aes)",
260 .base.cra_driver_name = "cbc-aes-s390",
261 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
262 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
263 .base.cra_blocksize = AES_BLOCK_SIZE,
264 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
265 .base.cra_module = THIS_MODULE,
266 .init = fallback_init_skcipher,
267 .exit = fallback_exit_skcipher,
268 .min_keysize = AES_MIN_KEY_SIZE,
269 .max_keysize = AES_MAX_KEY_SIZE,
270 .ivsize = AES_BLOCK_SIZE,
271 .setkey = cbc_aes_set_key,
272 .encrypt = cbc_aes_encrypt,
273 .decrypt = cbc_aes_decrypt,
274 };
275
xts_fallback_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int len)276 static int xts_fallback_setkey(struct crypto_skcipher *tfm, const u8 *key,
277 unsigned int len)
278 {
279 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
280
281 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
282 crypto_skcipher_set_flags(xts_ctx->fallback,
283 crypto_skcipher_get_flags(tfm) &
284 CRYPTO_TFM_REQ_MASK);
285 return crypto_skcipher_setkey(xts_ctx->fallback, key, len);
286 }
287
xts_aes_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)288 static int xts_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
289 unsigned int key_len)
290 {
291 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
292 unsigned long fc;
293 int err;
294
295 err = xts_fallback_setkey(tfm, in_key, key_len);
296 if (err)
297 return err;
298
299 /* Pick the correct function code based on the key length */
300 fc = (key_len == 32) ? CPACF_KM_XTS_128 :
301 (key_len == 64) ? CPACF_KM_XTS_256 : 0;
302
303 /* Check if the function code is available */
304 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
305 if (!xts_ctx->fc)
306 return 0;
307
308 /* Split the XTS key into the two subkeys */
309 key_len = key_len / 2;
310 xts_ctx->key_len = key_len;
311 memcpy(xts_ctx->key, in_key, key_len);
312 memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
313 return 0;
314 }
315
xts_aes_crypt(struct skcipher_request * req,unsigned long modifier)316 static int xts_aes_crypt(struct skcipher_request *req, unsigned long modifier)
317 {
318 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
319 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
320 struct skcipher_walk walk;
321 unsigned int offset, nbytes, n;
322 int ret;
323 struct {
324 u8 key[32];
325 u8 tweak[16];
326 u8 block[16];
327 u8 bit[16];
328 u8 xts[16];
329 } pcc_param;
330 struct {
331 u8 key[32];
332 u8 init[16];
333 } xts_param;
334
335 if (req->cryptlen < AES_BLOCK_SIZE)
336 return -EINVAL;
337
338 if (unlikely(!xts_ctx->fc || (req->cryptlen % AES_BLOCK_SIZE) != 0)) {
339 struct skcipher_request *subreq = skcipher_request_ctx(req);
340
341 *subreq = *req;
342 skcipher_request_set_tfm(subreq, xts_ctx->fallback);
343 return (modifier & CPACF_DECRYPT) ?
344 crypto_skcipher_decrypt(subreq) :
345 crypto_skcipher_encrypt(subreq);
346 }
347
348 ret = skcipher_walk_virt(&walk, req, false);
349 if (ret)
350 return ret;
351 offset = xts_ctx->key_len & 0x10;
352 memset(pcc_param.block, 0, sizeof(pcc_param.block));
353 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
354 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
355 memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
356 memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
357 cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
358
359 memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
360 memcpy(xts_param.init, pcc_param.xts, 16);
361
362 while (!ret && ((nbytes = walk.nbytes) != 0)) {
363 /* only use complete blocks */
364 n = nbytes & ~(AES_BLOCK_SIZE - 1);
365 cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
366 walk.dst.virt.addr, walk.src.virt.addr, n);
367 ret = skcipher_walk_done(&walk, nbytes - n);
368 }
369 memzero_explicit(&pcc_param, sizeof(pcc_param));
370 memzero_explicit(&xts_param, sizeof(xts_param));
371 return ret;
372 }
373
xts_aes_encrypt(struct skcipher_request * req)374 static int xts_aes_encrypt(struct skcipher_request *req)
375 {
376 return xts_aes_crypt(req, 0);
377 }
378
xts_aes_decrypt(struct skcipher_request * req)379 static int xts_aes_decrypt(struct skcipher_request *req)
380 {
381 return xts_aes_crypt(req, CPACF_DECRYPT);
382 }
383
xts_fallback_init(struct crypto_skcipher * tfm)384 static int xts_fallback_init(struct crypto_skcipher *tfm)
385 {
386 const char *name = crypto_tfm_alg_name(&tfm->base);
387 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
388
389 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
390 CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
391
392 if (IS_ERR(xts_ctx->fallback)) {
393 pr_err("Allocating XTS fallback algorithm %s failed\n",
394 name);
395 return PTR_ERR(xts_ctx->fallback);
396 }
397 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
398 crypto_skcipher_reqsize(xts_ctx->fallback));
399 return 0;
400 }
401
xts_fallback_exit(struct crypto_skcipher * tfm)402 static void xts_fallback_exit(struct crypto_skcipher *tfm)
403 {
404 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
405
406 crypto_free_skcipher(xts_ctx->fallback);
407 }
408
409 static struct skcipher_alg xts_aes_alg = {
410 .base.cra_name = "xts(aes)",
411 .base.cra_driver_name = "xts-aes-s390",
412 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
413 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
414 .base.cra_blocksize = AES_BLOCK_SIZE,
415 .base.cra_ctxsize = sizeof(struct s390_xts_ctx),
416 .base.cra_module = THIS_MODULE,
417 .init = xts_fallback_init,
418 .exit = xts_fallback_exit,
419 .min_keysize = 2 * AES_MIN_KEY_SIZE,
420 .max_keysize = 2 * AES_MAX_KEY_SIZE,
421 .ivsize = AES_BLOCK_SIZE,
422 .setkey = xts_aes_set_key,
423 .encrypt = xts_aes_encrypt,
424 .decrypt = xts_aes_decrypt,
425 };
426
fullxts_aes_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)427 static int fullxts_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
428 unsigned int key_len)
429 {
430 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
431 unsigned long fc;
432 int err;
433
434 err = xts_fallback_setkey(tfm, in_key, key_len);
435 if (err)
436 return err;
437
438 /* Pick the correct function code based on the key length */
439 fc = (key_len == 32) ? CPACF_KM_XTS_128_FULL :
440 (key_len == 64) ? CPACF_KM_XTS_256_FULL : 0;
441
442 /* Check if the function code is available */
443 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
444 if (!xts_ctx->fc)
445 return 0;
446
447 /* Store double-key */
448 memcpy(xts_ctx->keys, in_key, key_len);
449 xts_ctx->key_len = key_len;
450 return 0;
451 }
452
fullxts_aes_crypt(struct skcipher_request * req,unsigned long modifier)453 static int fullxts_aes_crypt(struct skcipher_request *req, unsigned long modifier)
454 {
455 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
456 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
457 unsigned int offset, nbytes, n;
458 struct skcipher_walk walk;
459 int ret;
460 struct {
461 __u8 key[64];
462 __u8 tweak[16];
463 __u8 nap[16];
464 } fxts_param = {
465 .nap = {0},
466 };
467
468 if (req->cryptlen < AES_BLOCK_SIZE)
469 return -EINVAL;
470
471 if (unlikely(!xts_ctx->fc || (req->cryptlen % AES_BLOCK_SIZE) != 0)) {
472 struct skcipher_request *subreq = skcipher_request_ctx(req);
473
474 *subreq = *req;
475 skcipher_request_set_tfm(subreq, xts_ctx->fallback);
476 return (modifier & CPACF_DECRYPT) ?
477 crypto_skcipher_decrypt(subreq) :
478 crypto_skcipher_encrypt(subreq);
479 }
480
481 ret = skcipher_walk_virt(&walk, req, false);
482 if (ret)
483 return ret;
484
485 offset = xts_ctx->key_len & 0x20;
486 memcpy(fxts_param.key + offset, xts_ctx->keys, xts_ctx->key_len);
487 memcpy(fxts_param.tweak, req->iv, AES_BLOCK_SIZE);
488 fxts_param.nap[0] = 0x01; /* initial alpha power (1, little-endian) */
489
490 while (!ret && ((nbytes = walk.nbytes) != 0)) {
491 /* only use complete blocks */
492 n = nbytes & ~(AES_BLOCK_SIZE - 1);
493 cpacf_km(xts_ctx->fc | modifier, fxts_param.key + offset,
494 walk.dst.virt.addr, walk.src.virt.addr, n);
495 ret = skcipher_walk_done(&walk, nbytes - n);
496 }
497 memzero_explicit(&fxts_param, sizeof(fxts_param));
498 return ret;
499 }
500
fullxts_aes_encrypt(struct skcipher_request * req)501 static int fullxts_aes_encrypt(struct skcipher_request *req)
502 {
503 return fullxts_aes_crypt(req, 0);
504 }
505
fullxts_aes_decrypt(struct skcipher_request * req)506 static int fullxts_aes_decrypt(struct skcipher_request *req)
507 {
508 return fullxts_aes_crypt(req, CPACF_DECRYPT);
509 }
510
511 static struct skcipher_alg fullxts_aes_alg = {
512 .base.cra_name = "xts(aes)",
513 .base.cra_driver_name = "full-xts-aes-s390",
514 .base.cra_priority = 403, /* aes-xts-s390 + 1 */
515 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
516 .base.cra_blocksize = AES_BLOCK_SIZE,
517 .base.cra_ctxsize = sizeof(struct s390_xts_ctx),
518 .base.cra_module = THIS_MODULE,
519 .init = xts_fallback_init,
520 .exit = xts_fallback_exit,
521 .min_keysize = 2 * AES_MIN_KEY_SIZE,
522 .max_keysize = 2 * AES_MAX_KEY_SIZE,
523 .ivsize = AES_BLOCK_SIZE,
524 .setkey = fullxts_aes_set_key,
525 .encrypt = fullxts_aes_encrypt,
526 .decrypt = fullxts_aes_decrypt,
527 };
528
ctr_aes_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)529 static int ctr_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
530 unsigned int key_len)
531 {
532 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
533 unsigned long fc;
534
535 /* Pick the correct function code based on the key length */
536 fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
537 (key_len == 24) ? CPACF_KMCTR_AES_192 :
538 (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
539
540 /* Check if the function code is available */
541 sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
542 if (!sctx->fc)
543 return setkey_fallback_skcipher(tfm, in_key, key_len);
544
545 sctx->key_len = key_len;
546 memcpy(sctx->key, in_key, key_len);
547 return 0;
548 }
549
__ctrblk_init(u8 * ctrptr,u8 * iv,unsigned int nbytes)550 static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
551 {
552 unsigned int i, n;
553
554 /* only use complete blocks, max. PAGE_SIZE */
555 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
556 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
557 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
558 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
559 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
560 ctrptr += AES_BLOCK_SIZE;
561 }
562 return n;
563 }
564
__ctr_aes_crypt(struct s390_aes_ctx * sctx,struct skcipher_walk * walk,bool locked)565 static int __ctr_aes_crypt(struct s390_aes_ctx *sctx,
566 struct skcipher_walk *walk, bool locked)
567 {
568 unsigned int n, nbytes;
569 int ret = 0;
570 u8 *ctrptr;
571
572 while (!ret && ((nbytes = walk->nbytes) >= AES_BLOCK_SIZE)) {
573 n = AES_BLOCK_SIZE;
574 if (nbytes >= 2 * AES_BLOCK_SIZE && locked)
575 n = __ctrblk_init(ctrblk, walk->iv, nbytes);
576 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk->iv;
577 cpacf_kmctr(sctx->fc, sctx->key, walk->dst.virt.addr,
578 walk->src.virt.addr, n, ctrptr);
579 if (ctrptr == ctrblk)
580 memcpy(walk->iv, ctrptr + n - AES_BLOCK_SIZE,
581 AES_BLOCK_SIZE);
582 crypto_inc(walk->iv, AES_BLOCK_SIZE);
583 ret = skcipher_walk_done(walk, nbytes - n);
584 }
585
586 return ret;
587 }
588
ctr_aes_crypt(struct skcipher_request * req)589 static int ctr_aes_crypt(struct skcipher_request *req)
590 {
591 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
592 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
593 struct skcipher_walk walk;
594 u8 buf[AES_BLOCK_SIZE];
595 int ret;
596
597 if (unlikely(!sctx->fc))
598 return fallback_skcipher_crypt(sctx, req, 0);
599
600 ret = skcipher_walk_virt(&walk, req, false);
601 if (ret)
602 return ret;
603
604 if (down_trylock(&ctrblk_sem) == 0) {
605 ret = __ctr_aes_crypt(sctx, &walk, true);
606 up(&ctrblk_sem);
607 } else {
608 ret = __ctr_aes_crypt(sctx, &walk, false);
609 }
610
611 /*
612 * final block may be < AES_BLOCK_SIZE, copy only nbytes
613 */
614 if (!ret && walk.nbytes > 0) {
615 memset(buf, 0, AES_BLOCK_SIZE);
616 memcpy(buf, walk.src.virt.addr, walk.nbytes);
617 cpacf_kmctr(sctx->fc, sctx->key, buf, buf,
618 AES_BLOCK_SIZE, walk.iv);
619 memcpy(walk.dst.virt.addr, buf, walk.nbytes);
620 crypto_inc(walk.iv, AES_BLOCK_SIZE);
621 ret = skcipher_walk_done(&walk, 0);
622 memzero_explicit(buf, sizeof(buf));
623 }
624
625 return ret;
626 }
627
628 static struct skcipher_alg ctr_aes_alg = {
629 .base.cra_name = "ctr(aes)",
630 .base.cra_driver_name = "ctr-aes-s390",
631 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
632 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
633 .base.cra_blocksize = 1,
634 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
635 .base.cra_module = THIS_MODULE,
636 .init = fallback_init_skcipher,
637 .exit = fallback_exit_skcipher,
638 .min_keysize = AES_MIN_KEY_SIZE,
639 .max_keysize = AES_MAX_KEY_SIZE,
640 .ivsize = AES_BLOCK_SIZE,
641 .setkey = ctr_aes_set_key,
642 .encrypt = ctr_aes_crypt,
643 .decrypt = ctr_aes_crypt,
644 .chunksize = AES_BLOCK_SIZE,
645 };
646
gcm_aes_setkey(struct crypto_aead * tfm,const u8 * key,unsigned int keylen)647 static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key,
648 unsigned int keylen)
649 {
650 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
651
652 switch (keylen) {
653 case AES_KEYSIZE_128:
654 ctx->fc = CPACF_KMA_GCM_AES_128;
655 break;
656 case AES_KEYSIZE_192:
657 ctx->fc = CPACF_KMA_GCM_AES_192;
658 break;
659 case AES_KEYSIZE_256:
660 ctx->fc = CPACF_KMA_GCM_AES_256;
661 break;
662 default:
663 return -EINVAL;
664 }
665
666 memcpy(ctx->key, key, keylen);
667 ctx->key_len = keylen;
668 return 0;
669 }
670
gcm_aes_setauthsize(struct crypto_aead * tfm,unsigned int authsize)671 static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
672 {
673 switch (authsize) {
674 case 4:
675 case 8:
676 case 12:
677 case 13:
678 case 14:
679 case 15:
680 case 16:
681 break;
682 default:
683 return -EINVAL;
684 }
685
686 return 0;
687 }
688
gcm_walk_start(struct gcm_sg_walk * gw,struct scatterlist * sg,unsigned int len)689 static void gcm_walk_start(struct gcm_sg_walk *gw, struct scatterlist *sg,
690 unsigned int len)
691 {
692 memset(gw, 0, sizeof(*gw));
693 gw->walk_bytes_remain = len;
694 scatterwalk_start(&gw->walk, sg);
695 }
696
_gcm_sg_clamp_and_map(struct gcm_sg_walk * gw)697 static inline unsigned int _gcm_sg_clamp_and_map(struct gcm_sg_walk *gw)
698 {
699 if (gw->walk_bytes_remain == 0)
700 return 0;
701 gw->walk_bytes = scatterwalk_next(&gw->walk, gw->walk_bytes_remain);
702 return gw->walk_bytes;
703 }
704
_gcm_sg_unmap_and_advance(struct gcm_sg_walk * gw,unsigned int nbytes,bool out)705 static inline void _gcm_sg_unmap_and_advance(struct gcm_sg_walk *gw,
706 unsigned int nbytes, bool out)
707 {
708 gw->walk_bytes_remain -= nbytes;
709 if (out)
710 scatterwalk_done_dst(&gw->walk, nbytes);
711 else
712 scatterwalk_done_src(&gw->walk, nbytes);
713 }
714
gcm_in_walk_go(struct gcm_sg_walk * gw,unsigned int minbytesneeded)715 static int gcm_in_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
716 {
717 int n;
718
719 if (gw->buf_bytes && gw->buf_bytes >= minbytesneeded) {
720 gw->ptr = gw->buf;
721 gw->nbytes = gw->buf_bytes;
722 goto out;
723 }
724
725 if (gw->walk_bytes_remain == 0) {
726 gw->ptr = NULL;
727 gw->nbytes = 0;
728 goto out;
729 }
730
731 if (!_gcm_sg_clamp_and_map(gw)) {
732 gw->ptr = NULL;
733 gw->nbytes = 0;
734 goto out;
735 }
736
737 if (!gw->buf_bytes && gw->walk_bytes >= minbytesneeded) {
738 gw->ptr = gw->walk.addr;
739 gw->nbytes = gw->walk_bytes;
740 goto out;
741 }
742
743 while (1) {
744 n = min(gw->walk_bytes, AES_BLOCK_SIZE - gw->buf_bytes);
745 memcpy(gw->buf + gw->buf_bytes, gw->walk.addr, n);
746 gw->buf_bytes += n;
747 _gcm_sg_unmap_and_advance(gw, n, false);
748 if (gw->buf_bytes >= minbytesneeded) {
749 gw->ptr = gw->buf;
750 gw->nbytes = gw->buf_bytes;
751 goto out;
752 }
753 if (!_gcm_sg_clamp_and_map(gw)) {
754 gw->ptr = NULL;
755 gw->nbytes = 0;
756 goto out;
757 }
758 }
759
760 out:
761 return gw->nbytes;
762 }
763
gcm_out_walk_go(struct gcm_sg_walk * gw,unsigned int minbytesneeded)764 static int gcm_out_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
765 {
766 if (gw->walk_bytes_remain == 0) {
767 gw->ptr = NULL;
768 gw->nbytes = 0;
769 goto out;
770 }
771
772 if (!_gcm_sg_clamp_and_map(gw)) {
773 gw->ptr = NULL;
774 gw->nbytes = 0;
775 goto out;
776 }
777
778 if (gw->walk_bytes >= minbytesneeded) {
779 gw->ptr = gw->walk.addr;
780 gw->nbytes = gw->walk_bytes;
781 goto out;
782 }
783
784 scatterwalk_unmap(&gw->walk);
785
786 gw->ptr = gw->buf;
787 gw->nbytes = sizeof(gw->buf);
788
789 out:
790 return gw->nbytes;
791 }
792
gcm_in_walk_done(struct gcm_sg_walk * gw,unsigned int bytesdone)793 static int gcm_in_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
794 {
795 if (gw->ptr == NULL)
796 return 0;
797
798 if (gw->ptr == gw->buf) {
799 int n = gw->buf_bytes - bytesdone;
800 if (n > 0) {
801 memmove(gw->buf, gw->buf + bytesdone, n);
802 gw->buf_bytes = n;
803 } else
804 gw->buf_bytes = 0;
805 } else
806 _gcm_sg_unmap_and_advance(gw, bytesdone, false);
807
808 return bytesdone;
809 }
810
gcm_out_walk_done(struct gcm_sg_walk * gw,unsigned int bytesdone)811 static int gcm_out_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
812 {
813 int i, n;
814
815 if (gw->ptr == NULL)
816 return 0;
817
818 if (gw->ptr == gw->buf) {
819 for (i = 0; i < bytesdone; i += n) {
820 if (!_gcm_sg_clamp_and_map(gw))
821 return i;
822 n = min(gw->walk_bytes, bytesdone - i);
823 memcpy(gw->walk.addr, gw->buf + i, n);
824 _gcm_sg_unmap_and_advance(gw, n, true);
825 }
826 } else
827 _gcm_sg_unmap_and_advance(gw, bytesdone, true);
828
829 return bytesdone;
830 }
831
gcm_aes_crypt(struct aead_request * req,unsigned int flags)832 static int gcm_aes_crypt(struct aead_request *req, unsigned int flags)
833 {
834 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
835 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
836 unsigned int ivsize = crypto_aead_ivsize(tfm);
837 unsigned int taglen = crypto_aead_authsize(tfm);
838 unsigned int aadlen = req->assoclen;
839 unsigned int pclen = req->cryptlen;
840 int ret = 0;
841
842 unsigned int n, len, in_bytes, out_bytes,
843 min_bytes, bytes, aad_bytes, pc_bytes;
844 struct gcm_sg_walk gw_in, gw_out;
845 u8 tag[GHASH_DIGEST_SIZE];
846
847 struct {
848 u32 _[3]; /* reserved */
849 u32 cv; /* Counter Value */
850 u8 t[GHASH_DIGEST_SIZE];/* Tag */
851 u8 h[AES_BLOCK_SIZE]; /* Hash-subkey */
852 u64 taadl; /* Total AAD Length */
853 u64 tpcl; /* Total Plain-/Cipher-text Length */
854 u8 j0[GHASH_BLOCK_SIZE];/* initial counter value */
855 u8 k[AES_MAX_KEY_SIZE]; /* Key */
856 } param;
857
858 /*
859 * encrypt
860 * req->src: aad||plaintext
861 * req->dst: aad||ciphertext||tag
862 * decrypt
863 * req->src: aad||ciphertext||tag
864 * req->dst: aad||plaintext, return 0 or -EBADMSG
865 * aad, plaintext and ciphertext may be empty.
866 */
867 if (flags & CPACF_DECRYPT)
868 pclen -= taglen;
869 len = aadlen + pclen;
870
871 memset(¶m, 0, sizeof(param));
872 param.cv = 1;
873 param.taadl = aadlen * 8;
874 param.tpcl = pclen * 8;
875 memcpy(param.j0, req->iv, ivsize);
876 *(u32 *)(param.j0 + ivsize) = 1;
877 memcpy(param.k, ctx->key, ctx->key_len);
878
879 gcm_walk_start(&gw_in, req->src, len);
880 gcm_walk_start(&gw_out, req->dst, len);
881
882 do {
883 min_bytes = min_t(unsigned int,
884 aadlen > 0 ? aadlen : pclen, AES_BLOCK_SIZE);
885 in_bytes = gcm_in_walk_go(&gw_in, min_bytes);
886 out_bytes = gcm_out_walk_go(&gw_out, min_bytes);
887 bytes = min(in_bytes, out_bytes);
888
889 if (aadlen + pclen <= bytes) {
890 aad_bytes = aadlen;
891 pc_bytes = pclen;
892 flags |= CPACF_KMA_LAAD | CPACF_KMA_LPC;
893 } else {
894 if (aadlen <= bytes) {
895 aad_bytes = aadlen;
896 pc_bytes = (bytes - aadlen) &
897 ~(AES_BLOCK_SIZE - 1);
898 flags |= CPACF_KMA_LAAD;
899 } else {
900 aad_bytes = bytes & ~(AES_BLOCK_SIZE - 1);
901 pc_bytes = 0;
902 }
903 }
904
905 if (aad_bytes > 0)
906 memcpy(gw_out.ptr, gw_in.ptr, aad_bytes);
907
908 cpacf_kma(ctx->fc | flags, ¶m,
909 gw_out.ptr + aad_bytes,
910 gw_in.ptr + aad_bytes, pc_bytes,
911 gw_in.ptr, aad_bytes);
912
913 n = aad_bytes + pc_bytes;
914 if (gcm_in_walk_done(&gw_in, n) != n) {
915 ret = -ENOMEM;
916 goto out;
917 }
918 if (gcm_out_walk_done(&gw_out, n) != n) {
919 ret = -ENOMEM;
920 goto out;
921 }
922 aadlen -= aad_bytes;
923 pclen -= pc_bytes;
924 } while (aadlen + pclen > 0);
925
926 if (flags & CPACF_DECRYPT) {
927 scatterwalk_map_and_copy(tag, req->src, len, taglen, 0);
928 if (crypto_memneq(tag, param.t, taglen))
929 ret = -EBADMSG;
930 } else
931 scatterwalk_map_and_copy(param.t, req->dst, len, taglen, 1);
932
933 out:
934 memzero_explicit(¶m, sizeof(param));
935 memzero_explicit(gw_in.buf, sizeof(gw_in.buf));
936 memzero_explicit(gw_out.buf, sizeof(gw_out.buf));
937 return ret;
938 }
939
gcm_aes_encrypt(struct aead_request * req)940 static int gcm_aes_encrypt(struct aead_request *req)
941 {
942 return gcm_aes_crypt(req, CPACF_ENCRYPT);
943 }
944
gcm_aes_decrypt(struct aead_request * req)945 static int gcm_aes_decrypt(struct aead_request *req)
946 {
947 return gcm_aes_crypt(req, CPACF_DECRYPT);
948 }
949
950 static struct aead_alg gcm_aes_aead = {
951 .setkey = gcm_aes_setkey,
952 .setauthsize = gcm_aes_setauthsize,
953 .encrypt = gcm_aes_encrypt,
954 .decrypt = gcm_aes_decrypt,
955
956 .ivsize = GHASH_BLOCK_SIZE - sizeof(u32),
957 .maxauthsize = GHASH_DIGEST_SIZE,
958 .chunksize = AES_BLOCK_SIZE,
959
960 .base = {
961 .cra_blocksize = 1,
962 .cra_ctxsize = sizeof(struct s390_aes_ctx),
963 .cra_priority = 900,
964 .cra_name = "gcm(aes)",
965 .cra_driver_name = "gcm-aes-s390",
966 .cra_module = THIS_MODULE,
967 },
968 };
969
970 static struct skcipher_alg *aes_s390_skcipher_algs[5];
971 static int aes_s390_skciphers_num;
972 static struct aead_alg *aes_s390_aead_alg;
973
aes_s390_register_skcipher(struct skcipher_alg * alg)974 static int aes_s390_register_skcipher(struct skcipher_alg *alg)
975 {
976 int ret;
977
978 ret = crypto_register_skcipher(alg);
979 if (!ret)
980 aes_s390_skcipher_algs[aes_s390_skciphers_num++] = alg;
981 return ret;
982 }
983
aes_s390_fini(void)984 static void aes_s390_fini(void)
985 {
986 while (aes_s390_skciphers_num--)
987 crypto_unregister_skcipher(aes_s390_skcipher_algs[aes_s390_skciphers_num]);
988 if (ctrblk)
989 free_page((unsigned long) ctrblk);
990
991 if (aes_s390_aead_alg)
992 crypto_unregister_aead(aes_s390_aead_alg);
993 }
994
aes_s390_init(void)995 static int __init aes_s390_init(void)
996 {
997 int ret;
998
999 /* Query available functions for KM, KMC, KMCTR and KMA */
1000 cpacf_query(CPACF_KM, &km_functions);
1001 cpacf_query(CPACF_KMC, &kmc_functions);
1002 cpacf_query(CPACF_KMCTR, &kmctr_functions);
1003 cpacf_query(CPACF_KMA, &kma_functions);
1004
1005 if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
1006 cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
1007 cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
1008 ret = aes_s390_register_skcipher(&ecb_aes_alg);
1009 if (ret)
1010 goto out_err;
1011 }
1012
1013 if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
1014 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
1015 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
1016 ret = aes_s390_register_skcipher(&cbc_aes_alg);
1017 if (ret)
1018 goto out_err;
1019 }
1020
1021 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128_FULL) ||
1022 cpacf_test_func(&km_functions, CPACF_KM_XTS_256_FULL)) {
1023 ret = aes_s390_register_skcipher(&fullxts_aes_alg);
1024 if (ret)
1025 goto out_err;
1026 }
1027
1028 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
1029 cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
1030 ret = aes_s390_register_skcipher(&xts_aes_alg);
1031 if (ret)
1032 goto out_err;
1033 }
1034
1035 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
1036 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
1037 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
1038 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
1039 if (!ctrblk) {
1040 ret = -ENOMEM;
1041 goto out_err;
1042 }
1043 ret = aes_s390_register_skcipher(&ctr_aes_alg);
1044 if (ret)
1045 goto out_err;
1046 }
1047
1048 if (cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_128) ||
1049 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_192) ||
1050 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_256)) {
1051 ret = crypto_register_aead(&gcm_aes_aead);
1052 if (ret)
1053 goto out_err;
1054 aes_s390_aead_alg = &gcm_aes_aead;
1055 }
1056
1057 return 0;
1058 out_err:
1059 aes_s390_fini();
1060 return ret;
1061 }
1062
1063 module_cpu_feature_match(S390_CPU_FEATURE_MSA, aes_s390_init);
1064 module_exit(aes_s390_fini);
1065
1066 MODULE_ALIAS_CRYPTO("aes-all");
1067
1068 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
1069 MODULE_LICENSE("GPL");
1070