1 // SPDX-License-Identifier: GPL-2.0+
5 * s390 implementation of the AES Cipher Algorithm.
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>
14 * Derived from "crypto/aes_generic.c"
17 #define KMSG_COMPONENT "aes_s390"
18 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20 #include <crypto/aes.h>
21 #include <crypto/algapi.h>
22 #include <crypto/ghash.h>
23 #include <crypto/internal/aead.h>
24 #include <crypto/internal/cipher.h>
25 #include <crypto/internal/skcipher.h>
26 #include <crypto/scatterwalk.h>
27 #include <linux/err.h>
28 #include <linux/module.h>
29 #include <linux/cpufeature.h>
30 #include <linux/init.h>
31 #include <linux/mutex.h>
32 #include <linux/fips.h>
33 #include <linux/string.h>
34 #include <crypto/xts.h>
35 #include <asm/cpacf.h>
38 static DEFINE_MUTEX(ctrblk_lock);
40 static cpacf_mask_t km_functions, kmc_functions, kmctr_functions,
44 u8 key[AES_MAX_KEY_SIZE];
48 struct crypto_skcipher *skcipher;
49 struct crypto_cipher *cip;
58 struct crypto_skcipher *fallback;
62 struct scatter_walk walk;
63 unsigned int walk_bytes;
65 unsigned int walk_bytes_remain;
66 u8 buf[AES_BLOCK_SIZE];
67 unsigned int buf_bytes;
72 static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
75 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
77 sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
78 sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
81 return crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
84 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
87 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
90 /* Pick the correct function code based on the key length */
91 fc = (key_len == 16) ? CPACF_KM_AES_128 :
92 (key_len == 24) ? CPACF_KM_AES_192 :
93 (key_len == 32) ? CPACF_KM_AES_256 : 0;
95 /* Check if the function code is available */
96 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
98 return setkey_fallback_cip(tfm, in_key, key_len);
100 sctx->key_len = key_len;
101 memcpy(sctx->key, in_key, key_len);
105 static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
107 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
109 if (unlikely(!sctx->fc)) {
110 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
113 cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
116 static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
118 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
120 if (unlikely(!sctx->fc)) {
121 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
124 cpacf_km(sctx->fc | CPACF_DECRYPT,
125 &sctx->key, out, in, AES_BLOCK_SIZE);
128 static int fallback_init_cip(struct crypto_tfm *tfm)
130 const char *name = tfm->__crt_alg->cra_name;
131 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
133 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
134 CRYPTO_ALG_NEED_FALLBACK);
136 if (IS_ERR(sctx->fallback.cip)) {
137 pr_err("Allocating AES fallback algorithm %s failed\n",
139 return PTR_ERR(sctx->fallback.cip);
145 static void fallback_exit_cip(struct crypto_tfm *tfm)
147 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
149 crypto_free_cipher(sctx->fallback.cip);
150 sctx->fallback.cip = NULL;
153 static struct crypto_alg aes_alg = {
155 .cra_driver_name = "aes-s390",
157 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
158 CRYPTO_ALG_NEED_FALLBACK,
159 .cra_blocksize = AES_BLOCK_SIZE,
160 .cra_ctxsize = sizeof(struct s390_aes_ctx),
161 .cra_module = THIS_MODULE,
162 .cra_init = fallback_init_cip,
163 .cra_exit = fallback_exit_cip,
166 .cia_min_keysize = AES_MIN_KEY_SIZE,
167 .cia_max_keysize = AES_MAX_KEY_SIZE,
168 .cia_setkey = aes_set_key,
169 .cia_encrypt = crypto_aes_encrypt,
170 .cia_decrypt = crypto_aes_decrypt,
175 static int setkey_fallback_skcipher(struct crypto_skcipher *tfm, const u8 *key,
178 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
180 crypto_skcipher_clear_flags(sctx->fallback.skcipher,
181 CRYPTO_TFM_REQ_MASK);
182 crypto_skcipher_set_flags(sctx->fallback.skcipher,
183 crypto_skcipher_get_flags(tfm) &
184 CRYPTO_TFM_REQ_MASK);
185 return crypto_skcipher_setkey(sctx->fallback.skcipher, key, len);
188 static int fallback_skcipher_crypt(struct s390_aes_ctx *sctx,
189 struct skcipher_request *req,
190 unsigned long modifier)
192 struct skcipher_request *subreq = skcipher_request_ctx(req);
195 skcipher_request_set_tfm(subreq, sctx->fallback.skcipher);
196 return (modifier & CPACF_DECRYPT) ?
197 crypto_skcipher_decrypt(subreq) :
198 crypto_skcipher_encrypt(subreq);
201 static int ecb_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
202 unsigned int key_len)
204 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
207 /* Pick the correct function code based on the key length */
208 fc = (key_len == 16) ? CPACF_KM_AES_128 :
209 (key_len == 24) ? CPACF_KM_AES_192 :
210 (key_len == 32) ? CPACF_KM_AES_256 : 0;
212 /* Check if the function code is available */
213 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
215 return setkey_fallback_skcipher(tfm, in_key, key_len);
217 sctx->key_len = key_len;
218 memcpy(sctx->key, in_key, key_len);
222 static int ecb_aes_crypt(struct skcipher_request *req, unsigned long modifier)
224 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
225 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
226 struct skcipher_walk walk;
227 unsigned int nbytes, n;
230 if (unlikely(!sctx->fc))
231 return fallback_skcipher_crypt(sctx, req, modifier);
233 ret = skcipher_walk_virt(&walk, req, false);
234 while ((nbytes = walk.nbytes) != 0) {
235 /* only use complete blocks */
236 n = nbytes & ~(AES_BLOCK_SIZE - 1);
237 cpacf_km(sctx->fc | modifier, sctx->key,
238 walk.dst.virt.addr, walk.src.virt.addr, n);
239 ret = skcipher_walk_done(&walk, nbytes - n);
244 static int ecb_aes_encrypt(struct skcipher_request *req)
246 return ecb_aes_crypt(req, 0);
249 static int ecb_aes_decrypt(struct skcipher_request *req)
251 return ecb_aes_crypt(req, CPACF_DECRYPT);
254 static int fallback_init_skcipher(struct crypto_skcipher *tfm)
256 const char *name = crypto_tfm_alg_name(&tfm->base);
257 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
259 sctx->fallback.skcipher = crypto_alloc_skcipher(name, 0,
260 CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
262 if (IS_ERR(sctx->fallback.skcipher)) {
263 pr_err("Allocating AES fallback algorithm %s failed\n",
265 return PTR_ERR(sctx->fallback.skcipher);
268 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
269 crypto_skcipher_reqsize(sctx->fallback.skcipher));
273 static void fallback_exit_skcipher(struct crypto_skcipher *tfm)
275 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
277 crypto_free_skcipher(sctx->fallback.skcipher);
280 static struct skcipher_alg ecb_aes_alg = {
281 .base.cra_name = "ecb(aes)",
282 .base.cra_driver_name = "ecb-aes-s390",
283 .base.cra_priority = 401, /* combo: aes + ecb + 1 */
284 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
285 .base.cra_blocksize = AES_BLOCK_SIZE,
286 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
287 .base.cra_module = THIS_MODULE,
288 .init = fallback_init_skcipher,
289 .exit = fallback_exit_skcipher,
290 .min_keysize = AES_MIN_KEY_SIZE,
291 .max_keysize = AES_MAX_KEY_SIZE,
292 .setkey = ecb_aes_set_key,
293 .encrypt = ecb_aes_encrypt,
294 .decrypt = ecb_aes_decrypt,
297 static int cbc_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
298 unsigned int key_len)
300 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
303 /* Pick the correct function code based on the key length */
304 fc = (key_len == 16) ? CPACF_KMC_AES_128 :
305 (key_len == 24) ? CPACF_KMC_AES_192 :
306 (key_len == 32) ? CPACF_KMC_AES_256 : 0;
308 /* Check if the function code is available */
309 sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
311 return setkey_fallback_skcipher(tfm, in_key, key_len);
313 sctx->key_len = key_len;
314 memcpy(sctx->key, in_key, key_len);
318 static int cbc_aes_crypt(struct skcipher_request *req, unsigned long modifier)
320 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
321 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
322 struct skcipher_walk walk;
323 unsigned int nbytes, n;
326 u8 iv[AES_BLOCK_SIZE];
327 u8 key[AES_MAX_KEY_SIZE];
330 if (unlikely(!sctx->fc))
331 return fallback_skcipher_crypt(sctx, req, modifier);
333 ret = skcipher_walk_virt(&walk, req, false);
336 memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
337 memcpy(param.key, sctx->key, sctx->key_len);
338 while ((nbytes = walk.nbytes) != 0) {
339 /* only use complete blocks */
340 n = nbytes & ~(AES_BLOCK_SIZE - 1);
341 cpacf_kmc(sctx->fc | modifier, ¶m,
342 walk.dst.virt.addr, walk.src.virt.addr, n);
343 memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
344 ret = skcipher_walk_done(&walk, nbytes - n);
346 memzero_explicit(¶m, sizeof(param));
350 static int cbc_aes_encrypt(struct skcipher_request *req)
352 return cbc_aes_crypt(req, 0);
355 static int cbc_aes_decrypt(struct skcipher_request *req)
357 return cbc_aes_crypt(req, CPACF_DECRYPT);
360 static struct skcipher_alg cbc_aes_alg = {
361 .base.cra_name = "cbc(aes)",
362 .base.cra_driver_name = "cbc-aes-s390",
363 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
364 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
365 .base.cra_blocksize = AES_BLOCK_SIZE,
366 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
367 .base.cra_module = THIS_MODULE,
368 .init = fallback_init_skcipher,
369 .exit = fallback_exit_skcipher,
370 .min_keysize = AES_MIN_KEY_SIZE,
371 .max_keysize = AES_MAX_KEY_SIZE,
372 .ivsize = AES_BLOCK_SIZE,
373 .setkey = cbc_aes_set_key,
374 .encrypt = cbc_aes_encrypt,
375 .decrypt = cbc_aes_decrypt,
378 static int xts_fallback_setkey(struct crypto_skcipher *tfm, const u8 *key,
381 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
383 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
384 crypto_skcipher_set_flags(xts_ctx->fallback,
385 crypto_skcipher_get_flags(tfm) &
386 CRYPTO_TFM_REQ_MASK);
387 return crypto_skcipher_setkey(xts_ctx->fallback, key, len);
390 static int xts_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
391 unsigned int key_len)
393 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
397 err = xts_fallback_setkey(tfm, in_key, key_len);
401 /* In fips mode only 128 bit or 256 bit keys are valid */
402 if (fips_enabled && key_len != 32 && key_len != 64)
405 /* Pick the correct function code based on the key length */
406 fc = (key_len == 32) ? CPACF_KM_XTS_128 :
407 (key_len == 64) ? CPACF_KM_XTS_256 : 0;
409 /* Check if the function code is available */
410 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
414 /* Split the XTS key into the two subkeys */
415 key_len = key_len / 2;
416 xts_ctx->key_len = key_len;
417 memcpy(xts_ctx->key, in_key, key_len);
418 memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
422 static int xts_aes_crypt(struct skcipher_request *req, unsigned long modifier)
424 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
425 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
426 struct skcipher_walk walk;
427 unsigned int offset, nbytes, n;
441 if (req->cryptlen < AES_BLOCK_SIZE)
444 if (unlikely(!xts_ctx->fc || (req->cryptlen % AES_BLOCK_SIZE) != 0)) {
445 struct skcipher_request *subreq = skcipher_request_ctx(req);
448 skcipher_request_set_tfm(subreq, xts_ctx->fallback);
449 return (modifier & CPACF_DECRYPT) ?
450 crypto_skcipher_decrypt(subreq) :
451 crypto_skcipher_encrypt(subreq);
454 ret = skcipher_walk_virt(&walk, req, false);
457 offset = xts_ctx->key_len & 0x10;
458 memset(pcc_param.block, 0, sizeof(pcc_param.block));
459 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
460 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
461 memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
462 memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
463 cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
465 memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
466 memcpy(xts_param.init, pcc_param.xts, 16);
468 while ((nbytes = walk.nbytes) != 0) {
469 /* only use complete blocks */
470 n = nbytes & ~(AES_BLOCK_SIZE - 1);
471 cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
472 walk.dst.virt.addr, walk.src.virt.addr, n);
473 ret = skcipher_walk_done(&walk, nbytes - n);
475 memzero_explicit(&pcc_param, sizeof(pcc_param));
476 memzero_explicit(&xts_param, sizeof(xts_param));
480 static int xts_aes_encrypt(struct skcipher_request *req)
482 return xts_aes_crypt(req, 0);
485 static int xts_aes_decrypt(struct skcipher_request *req)
487 return xts_aes_crypt(req, CPACF_DECRYPT);
490 static int xts_fallback_init(struct crypto_skcipher *tfm)
492 const char *name = crypto_tfm_alg_name(&tfm->base);
493 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
495 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
496 CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
498 if (IS_ERR(xts_ctx->fallback)) {
499 pr_err("Allocating XTS fallback algorithm %s failed\n",
501 return PTR_ERR(xts_ctx->fallback);
503 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
504 crypto_skcipher_reqsize(xts_ctx->fallback));
508 static void xts_fallback_exit(struct crypto_skcipher *tfm)
510 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
512 crypto_free_skcipher(xts_ctx->fallback);
515 static struct skcipher_alg xts_aes_alg = {
516 .base.cra_name = "xts(aes)",
517 .base.cra_driver_name = "xts-aes-s390",
518 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
519 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
520 .base.cra_blocksize = AES_BLOCK_SIZE,
521 .base.cra_ctxsize = sizeof(struct s390_xts_ctx),
522 .base.cra_module = THIS_MODULE,
523 .init = xts_fallback_init,
524 .exit = xts_fallback_exit,
525 .min_keysize = 2 * AES_MIN_KEY_SIZE,
526 .max_keysize = 2 * AES_MAX_KEY_SIZE,
527 .ivsize = AES_BLOCK_SIZE,
528 .setkey = xts_aes_set_key,
529 .encrypt = xts_aes_encrypt,
530 .decrypt = xts_aes_decrypt,
533 static int ctr_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
534 unsigned int key_len)
536 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
539 /* Pick the correct function code based on the key length */
540 fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
541 (key_len == 24) ? CPACF_KMCTR_AES_192 :
542 (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
544 /* Check if the function code is available */
545 sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
547 return setkey_fallback_skcipher(tfm, in_key, key_len);
549 sctx->key_len = key_len;
550 memcpy(sctx->key, in_key, key_len);
554 static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
558 /* only use complete blocks, max. PAGE_SIZE */
559 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
560 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
561 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
562 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
563 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
564 ctrptr += AES_BLOCK_SIZE;
569 static int ctr_aes_crypt(struct skcipher_request *req)
571 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
572 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
573 u8 buf[AES_BLOCK_SIZE], *ctrptr;
574 struct skcipher_walk walk;
575 unsigned int n, nbytes;
578 if (unlikely(!sctx->fc))
579 return fallback_skcipher_crypt(sctx, req, 0);
581 locked = mutex_trylock(&ctrblk_lock);
583 ret = skcipher_walk_virt(&walk, req, false);
584 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
587 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
588 n = __ctrblk_init(ctrblk, walk.iv, nbytes);
589 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
590 cpacf_kmctr(sctx->fc, sctx->key, walk.dst.virt.addr,
591 walk.src.virt.addr, n, ctrptr);
592 if (ctrptr == ctrblk)
593 memcpy(walk.iv, ctrptr + n - AES_BLOCK_SIZE,
595 crypto_inc(walk.iv, AES_BLOCK_SIZE);
596 ret = skcipher_walk_done(&walk, nbytes - n);
599 mutex_unlock(&ctrblk_lock);
601 * final block may be < AES_BLOCK_SIZE, copy only nbytes
604 cpacf_kmctr(sctx->fc, sctx->key, buf, walk.src.virt.addr,
605 AES_BLOCK_SIZE, walk.iv);
606 memcpy(walk.dst.virt.addr, buf, nbytes);
607 crypto_inc(walk.iv, AES_BLOCK_SIZE);
608 ret = skcipher_walk_done(&walk, 0);
614 static struct skcipher_alg ctr_aes_alg = {
615 .base.cra_name = "ctr(aes)",
616 .base.cra_driver_name = "ctr-aes-s390",
617 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
618 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
619 .base.cra_blocksize = 1,
620 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
621 .base.cra_module = THIS_MODULE,
622 .init = fallback_init_skcipher,
623 .exit = fallback_exit_skcipher,
624 .min_keysize = AES_MIN_KEY_SIZE,
625 .max_keysize = AES_MAX_KEY_SIZE,
626 .ivsize = AES_BLOCK_SIZE,
627 .setkey = ctr_aes_set_key,
628 .encrypt = ctr_aes_crypt,
629 .decrypt = ctr_aes_crypt,
630 .chunksize = AES_BLOCK_SIZE,
633 static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key,
636 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
639 case AES_KEYSIZE_128:
640 ctx->fc = CPACF_KMA_GCM_AES_128;
642 case AES_KEYSIZE_192:
643 ctx->fc = CPACF_KMA_GCM_AES_192;
645 case AES_KEYSIZE_256:
646 ctx->fc = CPACF_KMA_GCM_AES_256;
652 memcpy(ctx->key, key, keylen);
653 ctx->key_len = keylen;
657 static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
675 static void gcm_walk_start(struct gcm_sg_walk *gw, struct scatterlist *sg,
678 memset(gw, 0, sizeof(*gw));
679 gw->walk_bytes_remain = len;
680 scatterwalk_start(&gw->walk, sg);
683 static inline unsigned int _gcm_sg_clamp_and_map(struct gcm_sg_walk *gw)
685 struct scatterlist *nextsg;
687 gw->walk_bytes = scatterwalk_clamp(&gw->walk, gw->walk_bytes_remain);
688 while (!gw->walk_bytes) {
689 nextsg = sg_next(gw->walk.sg);
692 scatterwalk_start(&gw->walk, nextsg);
693 gw->walk_bytes = scatterwalk_clamp(&gw->walk,
694 gw->walk_bytes_remain);
696 gw->walk_ptr = scatterwalk_map(&gw->walk);
697 return gw->walk_bytes;
700 static inline void _gcm_sg_unmap_and_advance(struct gcm_sg_walk *gw,
703 gw->walk_bytes_remain -= nbytes;
704 scatterwalk_unmap(&gw->walk);
705 scatterwalk_advance(&gw->walk, nbytes);
706 scatterwalk_done(&gw->walk, 0, gw->walk_bytes_remain);
710 static int gcm_in_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
714 if (gw->buf_bytes && gw->buf_bytes >= minbytesneeded) {
716 gw->nbytes = gw->buf_bytes;
720 if (gw->walk_bytes_remain == 0) {
726 if (!_gcm_sg_clamp_and_map(gw)) {
732 if (!gw->buf_bytes && gw->walk_bytes >= minbytesneeded) {
733 gw->ptr = gw->walk_ptr;
734 gw->nbytes = gw->walk_bytes;
739 n = min(gw->walk_bytes, AES_BLOCK_SIZE - gw->buf_bytes);
740 memcpy(gw->buf + gw->buf_bytes, gw->walk_ptr, n);
742 _gcm_sg_unmap_and_advance(gw, n);
743 if (gw->buf_bytes >= minbytesneeded) {
745 gw->nbytes = gw->buf_bytes;
748 if (!_gcm_sg_clamp_and_map(gw)) {
759 static int gcm_out_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
761 if (gw->walk_bytes_remain == 0) {
767 if (!_gcm_sg_clamp_and_map(gw)) {
773 if (gw->walk_bytes >= minbytesneeded) {
774 gw->ptr = gw->walk_ptr;
775 gw->nbytes = gw->walk_bytes;
779 scatterwalk_unmap(&gw->walk);
783 gw->nbytes = sizeof(gw->buf);
789 static int gcm_in_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
794 if (gw->ptr == gw->buf) {
795 int n = gw->buf_bytes - bytesdone;
797 memmove(gw->buf, gw->buf + bytesdone, n);
802 _gcm_sg_unmap_and_advance(gw, bytesdone);
807 static int gcm_out_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
814 if (gw->ptr == gw->buf) {
815 for (i = 0; i < bytesdone; i += n) {
816 if (!_gcm_sg_clamp_and_map(gw))
818 n = min(gw->walk_bytes, bytesdone - i);
819 memcpy(gw->walk_ptr, gw->buf + i, n);
820 _gcm_sg_unmap_and_advance(gw, n);
823 _gcm_sg_unmap_and_advance(gw, bytesdone);
828 static int gcm_aes_crypt(struct aead_request *req, unsigned int flags)
830 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
831 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
832 unsigned int ivsize = crypto_aead_ivsize(tfm);
833 unsigned int taglen = crypto_aead_authsize(tfm);
834 unsigned int aadlen = req->assoclen;
835 unsigned int pclen = req->cryptlen;
838 unsigned int n, len, in_bytes, out_bytes,
839 min_bytes, bytes, aad_bytes, pc_bytes;
840 struct gcm_sg_walk gw_in, gw_out;
841 u8 tag[GHASH_DIGEST_SIZE];
844 u32 _[3]; /* reserved */
845 u32 cv; /* Counter Value */
846 u8 t[GHASH_DIGEST_SIZE];/* Tag */
847 u8 h[AES_BLOCK_SIZE]; /* Hash-subkey */
848 u64 taadl; /* Total AAD Length */
849 u64 tpcl; /* Total Plain-/Cipher-text Length */
850 u8 j0[GHASH_BLOCK_SIZE];/* initial counter value */
851 u8 k[AES_MAX_KEY_SIZE]; /* Key */
856 * req->src: aad||plaintext
857 * req->dst: aad||ciphertext||tag
859 * req->src: aad||ciphertext||tag
860 * req->dst: aad||plaintext, return 0 or -EBADMSG
861 * aad, plaintext and ciphertext may be empty.
863 if (flags & CPACF_DECRYPT)
865 len = aadlen + pclen;
867 memset(¶m, 0, sizeof(param));
869 param.taadl = aadlen * 8;
870 param.tpcl = pclen * 8;
871 memcpy(param.j0, req->iv, ivsize);
872 *(u32 *)(param.j0 + ivsize) = 1;
873 memcpy(param.k, ctx->key, ctx->key_len);
875 gcm_walk_start(&gw_in, req->src, len);
876 gcm_walk_start(&gw_out, req->dst, len);
879 min_bytes = min_t(unsigned int,
880 aadlen > 0 ? aadlen : pclen, AES_BLOCK_SIZE);
881 in_bytes = gcm_in_walk_go(&gw_in, min_bytes);
882 out_bytes = gcm_out_walk_go(&gw_out, min_bytes);
883 bytes = min(in_bytes, out_bytes);
885 if (aadlen + pclen <= bytes) {
888 flags |= CPACF_KMA_LAAD | CPACF_KMA_LPC;
890 if (aadlen <= bytes) {
892 pc_bytes = (bytes - aadlen) &
893 ~(AES_BLOCK_SIZE - 1);
894 flags |= CPACF_KMA_LAAD;
896 aad_bytes = bytes & ~(AES_BLOCK_SIZE - 1);
902 memcpy(gw_out.ptr, gw_in.ptr, aad_bytes);
904 cpacf_kma(ctx->fc | flags, ¶m,
905 gw_out.ptr + aad_bytes,
906 gw_in.ptr + aad_bytes, pc_bytes,
907 gw_in.ptr, aad_bytes);
909 n = aad_bytes + pc_bytes;
910 if (gcm_in_walk_done(&gw_in, n) != n)
912 if (gcm_out_walk_done(&gw_out, n) != n)
916 } while (aadlen + pclen > 0);
918 if (flags & CPACF_DECRYPT) {
919 scatterwalk_map_and_copy(tag, req->src, len, taglen, 0);
920 if (crypto_memneq(tag, param.t, taglen))
923 scatterwalk_map_and_copy(param.t, req->dst, len, taglen, 1);
925 memzero_explicit(¶m, sizeof(param));
929 static int gcm_aes_encrypt(struct aead_request *req)
931 return gcm_aes_crypt(req, CPACF_ENCRYPT);
934 static int gcm_aes_decrypt(struct aead_request *req)
936 return gcm_aes_crypt(req, CPACF_DECRYPT);
939 static struct aead_alg gcm_aes_aead = {
940 .setkey = gcm_aes_setkey,
941 .setauthsize = gcm_aes_setauthsize,
942 .encrypt = gcm_aes_encrypt,
943 .decrypt = gcm_aes_decrypt,
945 .ivsize = GHASH_BLOCK_SIZE - sizeof(u32),
946 .maxauthsize = GHASH_DIGEST_SIZE,
947 .chunksize = AES_BLOCK_SIZE,
951 .cra_ctxsize = sizeof(struct s390_aes_ctx),
953 .cra_name = "gcm(aes)",
954 .cra_driver_name = "gcm-aes-s390",
955 .cra_module = THIS_MODULE,
959 static struct crypto_alg *aes_s390_alg;
960 static struct skcipher_alg *aes_s390_skcipher_algs[4];
961 static int aes_s390_skciphers_num;
962 static struct aead_alg *aes_s390_aead_alg;
964 static int aes_s390_register_skcipher(struct skcipher_alg *alg)
968 ret = crypto_register_skcipher(alg);
970 aes_s390_skcipher_algs[aes_s390_skciphers_num++] = alg;
974 static void aes_s390_fini(void)
977 crypto_unregister_alg(aes_s390_alg);
978 while (aes_s390_skciphers_num--)
979 crypto_unregister_skcipher(aes_s390_skcipher_algs[aes_s390_skciphers_num]);
981 free_page((unsigned long) ctrblk);
983 if (aes_s390_aead_alg)
984 crypto_unregister_aead(aes_s390_aead_alg);
987 static int __init aes_s390_init(void)
991 /* Query available functions for KM, KMC, KMCTR and KMA */
992 cpacf_query(CPACF_KM, &km_functions);
993 cpacf_query(CPACF_KMC, &kmc_functions);
994 cpacf_query(CPACF_KMCTR, &kmctr_functions);
995 cpacf_query(CPACF_KMA, &kma_functions);
997 if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
998 cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
999 cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
1000 ret = crypto_register_alg(&aes_alg);
1003 aes_s390_alg = &aes_alg;
1004 ret = aes_s390_register_skcipher(&ecb_aes_alg);
1009 if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
1010 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
1011 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
1012 ret = aes_s390_register_skcipher(&cbc_aes_alg);
1017 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
1018 cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
1019 ret = aes_s390_register_skcipher(&xts_aes_alg);
1024 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
1025 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
1026 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
1027 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
1032 ret = aes_s390_register_skcipher(&ctr_aes_alg);
1037 if (cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_128) ||
1038 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_192) ||
1039 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_256)) {
1040 ret = crypto_register_aead(&gcm_aes_aead);
1043 aes_s390_aead_alg = &gcm_aes_aead;
1052 module_cpu_feature_match(MSA, aes_s390_init);
1053 module_exit(aes_s390_fini);
1055 MODULE_ALIAS_CRYPTO("aes-all");
1057 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
1058 MODULE_LICENSE("GPL");
1059 MODULE_IMPORT_NS(CRYPTO_INTERNAL);