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/skcipher.h>
25 #include <crypto/scatterwalk.h>
26 #include <linux/err.h>
27 #include <linux/module.h>
28 #include <linux/cpufeature.h>
29 #include <linux/init.h>
30 #include <linux/mutex.h>
31 #include <linux/fips.h>
32 #include <linux/string.h>
33 #include <crypto/xts.h>
34 #include <asm/cpacf.h>
37 static DEFINE_MUTEX(ctrblk_lock);
39 static cpacf_mask_t km_functions, kmc_functions, kmctr_functions,
43 u8 key[AES_MAX_KEY_SIZE];
47 struct crypto_skcipher *skcipher;
48 struct crypto_cipher *cip;
57 struct crypto_skcipher *fallback;
61 struct scatter_walk walk;
62 unsigned int walk_bytes;
64 unsigned int walk_bytes_remain;
65 u8 buf[AES_BLOCK_SIZE];
66 unsigned int buf_bytes;
71 static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
74 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 ret = crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
83 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
84 tfm->crt_flags |= (sctx->fallback.cip->base.crt_flags &
90 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
93 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
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;
101 /* Check if the function code is available */
102 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
104 return setkey_fallback_cip(tfm, in_key, key_len);
106 sctx->key_len = key_len;
107 memcpy(sctx->key, in_key, key_len);
111 static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
113 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
115 if (unlikely(!sctx->fc)) {
116 crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
119 cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
122 static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
124 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
126 if (unlikely(!sctx->fc)) {
127 crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
130 cpacf_km(sctx->fc | CPACF_DECRYPT,
131 &sctx->key, out, in, AES_BLOCK_SIZE);
134 static int fallback_init_cip(struct crypto_tfm *tfm)
136 const char *name = tfm->__crt_alg->cra_name;
137 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
139 sctx->fallback.cip = crypto_alloc_cipher(name, 0,
140 CRYPTO_ALG_NEED_FALLBACK);
142 if (IS_ERR(sctx->fallback.cip)) {
143 pr_err("Allocating AES fallback algorithm %s failed\n",
145 return PTR_ERR(sctx->fallback.cip);
151 static void fallback_exit_cip(struct crypto_tfm *tfm)
153 struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
155 crypto_free_cipher(sctx->fallback.cip);
156 sctx->fallback.cip = NULL;
159 static struct crypto_alg aes_alg = {
161 .cra_driver_name = "aes-s390",
163 .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
164 CRYPTO_ALG_NEED_FALLBACK,
165 .cra_blocksize = AES_BLOCK_SIZE,
166 .cra_ctxsize = sizeof(struct s390_aes_ctx),
167 .cra_module = THIS_MODULE,
168 .cra_init = fallback_init_cip,
169 .cra_exit = fallback_exit_cip,
172 .cia_min_keysize = AES_MIN_KEY_SIZE,
173 .cia_max_keysize = AES_MAX_KEY_SIZE,
174 .cia_setkey = aes_set_key,
175 .cia_encrypt = crypto_aes_encrypt,
176 .cia_decrypt = crypto_aes_decrypt,
181 static int setkey_fallback_skcipher(struct crypto_skcipher *tfm, const u8 *key,
184 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
187 crypto_skcipher_clear_flags(sctx->fallback.skcipher,
188 CRYPTO_TFM_REQ_MASK);
189 crypto_skcipher_set_flags(sctx->fallback.skcipher,
190 crypto_skcipher_get_flags(tfm) &
191 CRYPTO_TFM_REQ_MASK);
192 ret = crypto_skcipher_setkey(sctx->fallback.skcipher, key, len);
193 crypto_skcipher_set_flags(tfm,
194 crypto_skcipher_get_flags(sctx->fallback.skcipher) &
195 CRYPTO_TFM_RES_MASK);
199 static int fallback_skcipher_crypt(struct s390_aes_ctx *sctx,
200 struct skcipher_request *req,
201 unsigned long modifier)
203 struct skcipher_request *subreq = skcipher_request_ctx(req);
206 skcipher_request_set_tfm(subreq, sctx->fallback.skcipher);
207 return (modifier & CPACF_DECRYPT) ?
208 crypto_skcipher_decrypt(subreq) :
209 crypto_skcipher_encrypt(subreq);
212 static int ecb_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
213 unsigned int key_len)
215 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
218 /* Pick the correct function code based on the key length */
219 fc = (key_len == 16) ? CPACF_KM_AES_128 :
220 (key_len == 24) ? CPACF_KM_AES_192 :
221 (key_len == 32) ? CPACF_KM_AES_256 : 0;
223 /* Check if the function code is available */
224 sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
226 return setkey_fallback_skcipher(tfm, in_key, key_len);
228 sctx->key_len = key_len;
229 memcpy(sctx->key, in_key, key_len);
233 static int ecb_aes_crypt(struct skcipher_request *req, unsigned long modifier)
235 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
236 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
237 struct skcipher_walk walk;
238 unsigned int nbytes, n;
241 if (unlikely(!sctx->fc))
242 return fallback_skcipher_crypt(sctx, req, modifier);
244 ret = skcipher_walk_virt(&walk, req, false);
245 while ((nbytes = walk.nbytes) != 0) {
246 /* only use complete blocks */
247 n = nbytes & ~(AES_BLOCK_SIZE - 1);
248 cpacf_km(sctx->fc | modifier, sctx->key,
249 walk.dst.virt.addr, walk.src.virt.addr, n);
250 ret = skcipher_walk_done(&walk, nbytes - n);
255 static int ecb_aes_encrypt(struct skcipher_request *req)
257 return ecb_aes_crypt(req, 0);
260 static int ecb_aes_decrypt(struct skcipher_request *req)
262 return ecb_aes_crypt(req, CPACF_DECRYPT);
265 static int fallback_init_skcipher(struct crypto_skcipher *tfm)
267 const char *name = crypto_tfm_alg_name(&tfm->base);
268 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
270 sctx->fallback.skcipher = crypto_alloc_skcipher(name, 0,
271 CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
273 if (IS_ERR(sctx->fallback.skcipher)) {
274 pr_err("Allocating AES fallback algorithm %s failed\n",
276 return PTR_ERR(sctx->fallback.skcipher);
279 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
280 crypto_skcipher_reqsize(sctx->fallback.skcipher));
284 static void fallback_exit_skcipher(struct crypto_skcipher *tfm)
286 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
288 crypto_free_skcipher(sctx->fallback.skcipher);
291 static struct skcipher_alg ecb_aes_alg = {
292 .base.cra_name = "ecb(aes)",
293 .base.cra_driver_name = "ecb-aes-s390",
294 .base.cra_priority = 401, /* combo: aes + ecb + 1 */
295 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
296 .base.cra_blocksize = AES_BLOCK_SIZE,
297 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
298 .base.cra_module = THIS_MODULE,
299 .init = fallback_init_skcipher,
300 .exit = fallback_exit_skcipher,
301 .min_keysize = AES_MIN_KEY_SIZE,
302 .max_keysize = AES_MAX_KEY_SIZE,
303 .setkey = ecb_aes_set_key,
304 .encrypt = ecb_aes_encrypt,
305 .decrypt = ecb_aes_decrypt,
308 static int cbc_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
309 unsigned int key_len)
311 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
314 /* Pick the correct function code based on the key length */
315 fc = (key_len == 16) ? CPACF_KMC_AES_128 :
316 (key_len == 24) ? CPACF_KMC_AES_192 :
317 (key_len == 32) ? CPACF_KMC_AES_256 : 0;
319 /* Check if the function code is available */
320 sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
322 return setkey_fallback_skcipher(tfm, in_key, key_len);
324 sctx->key_len = key_len;
325 memcpy(sctx->key, in_key, key_len);
329 static int cbc_aes_crypt(struct skcipher_request *req, unsigned long modifier)
331 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
332 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
333 struct skcipher_walk walk;
334 unsigned int nbytes, n;
337 u8 iv[AES_BLOCK_SIZE];
338 u8 key[AES_MAX_KEY_SIZE];
341 if (unlikely(!sctx->fc))
342 return fallback_skcipher_crypt(sctx, req, modifier);
344 ret = skcipher_walk_virt(&walk, req, false);
347 memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
348 memcpy(param.key, sctx->key, sctx->key_len);
349 while ((nbytes = walk.nbytes) != 0) {
350 /* only use complete blocks */
351 n = nbytes & ~(AES_BLOCK_SIZE - 1);
352 cpacf_kmc(sctx->fc | modifier, ¶m,
353 walk.dst.virt.addr, walk.src.virt.addr, n);
354 memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
355 ret = skcipher_walk_done(&walk, nbytes - n);
360 static int cbc_aes_encrypt(struct skcipher_request *req)
362 return cbc_aes_crypt(req, 0);
365 static int cbc_aes_decrypt(struct skcipher_request *req)
367 return cbc_aes_crypt(req, CPACF_DECRYPT);
370 static struct skcipher_alg cbc_aes_alg = {
371 .base.cra_name = "cbc(aes)",
372 .base.cra_driver_name = "cbc-aes-s390",
373 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
374 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
375 .base.cra_blocksize = AES_BLOCK_SIZE,
376 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
377 .base.cra_module = THIS_MODULE,
378 .init = fallback_init_skcipher,
379 .exit = fallback_exit_skcipher,
380 .min_keysize = AES_MIN_KEY_SIZE,
381 .max_keysize = AES_MAX_KEY_SIZE,
382 .ivsize = AES_BLOCK_SIZE,
383 .setkey = cbc_aes_set_key,
384 .encrypt = cbc_aes_encrypt,
385 .decrypt = cbc_aes_decrypt,
388 static int xts_fallback_setkey(struct crypto_skcipher *tfm, const u8 *key,
391 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
394 crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
395 crypto_skcipher_set_flags(xts_ctx->fallback,
396 crypto_skcipher_get_flags(tfm) &
397 CRYPTO_TFM_REQ_MASK);
398 ret = crypto_skcipher_setkey(xts_ctx->fallback, key, len);
399 crypto_skcipher_set_flags(tfm,
400 crypto_skcipher_get_flags(xts_ctx->fallback) &
401 CRYPTO_TFM_RES_MASK);
405 static int xts_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
406 unsigned int key_len)
408 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
412 err = xts_fallback_setkey(tfm, in_key, key_len);
416 /* In fips mode only 128 bit or 256 bit keys are valid */
417 if (fips_enabled && key_len != 32 && key_len != 64) {
418 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
422 /* Pick the correct function code based on the key length */
423 fc = (key_len == 32) ? CPACF_KM_XTS_128 :
424 (key_len == 64) ? CPACF_KM_XTS_256 : 0;
426 /* Check if the function code is available */
427 xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
431 /* Split the XTS key into the two subkeys */
432 key_len = key_len / 2;
433 xts_ctx->key_len = key_len;
434 memcpy(xts_ctx->key, in_key, key_len);
435 memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
439 static int xts_aes_crypt(struct skcipher_request *req, unsigned long modifier)
441 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
442 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
443 struct skcipher_walk walk;
444 unsigned int offset, nbytes, n;
458 if (req->cryptlen < AES_BLOCK_SIZE)
461 if (unlikely(!xts_ctx->fc || (req->cryptlen % AES_BLOCK_SIZE) != 0)) {
462 struct skcipher_request *subreq = skcipher_request_ctx(req);
465 skcipher_request_set_tfm(subreq, xts_ctx->fallback);
466 return (modifier & CPACF_DECRYPT) ?
467 crypto_skcipher_decrypt(subreq) :
468 crypto_skcipher_encrypt(subreq);
471 ret = skcipher_walk_virt(&walk, req, false);
474 offset = xts_ctx->key_len & 0x10;
475 memset(pcc_param.block, 0, sizeof(pcc_param.block));
476 memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
477 memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
478 memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
479 memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
480 cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
482 memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
483 memcpy(xts_param.init, pcc_param.xts, 16);
485 while ((nbytes = walk.nbytes) != 0) {
486 /* only use complete blocks */
487 n = nbytes & ~(AES_BLOCK_SIZE - 1);
488 cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
489 walk.dst.virt.addr, walk.src.virt.addr, n);
490 ret = skcipher_walk_done(&walk, nbytes - n);
495 static int xts_aes_encrypt(struct skcipher_request *req)
497 return xts_aes_crypt(req, 0);
500 static int xts_aes_decrypt(struct skcipher_request *req)
502 return xts_aes_crypt(req, CPACF_DECRYPT);
505 static int xts_fallback_init(struct crypto_skcipher *tfm)
507 const char *name = crypto_tfm_alg_name(&tfm->base);
508 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
510 xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
511 CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
513 if (IS_ERR(xts_ctx->fallback)) {
514 pr_err("Allocating XTS fallback algorithm %s failed\n",
516 return PTR_ERR(xts_ctx->fallback);
518 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
519 crypto_skcipher_reqsize(xts_ctx->fallback));
523 static void xts_fallback_exit(struct crypto_skcipher *tfm)
525 struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
527 crypto_free_skcipher(xts_ctx->fallback);
530 static struct skcipher_alg xts_aes_alg = {
531 .base.cra_name = "xts(aes)",
532 .base.cra_driver_name = "xts-aes-s390",
533 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
534 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
535 .base.cra_blocksize = AES_BLOCK_SIZE,
536 .base.cra_ctxsize = sizeof(struct s390_xts_ctx),
537 .base.cra_module = THIS_MODULE,
538 .init = xts_fallback_init,
539 .exit = xts_fallback_exit,
540 .min_keysize = 2 * AES_MIN_KEY_SIZE,
541 .max_keysize = 2 * AES_MAX_KEY_SIZE,
542 .ivsize = AES_BLOCK_SIZE,
543 .setkey = xts_aes_set_key,
544 .encrypt = xts_aes_encrypt,
545 .decrypt = xts_aes_decrypt,
548 static int ctr_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
549 unsigned int key_len)
551 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
554 /* Pick the correct function code based on the key length */
555 fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
556 (key_len == 24) ? CPACF_KMCTR_AES_192 :
557 (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
559 /* Check if the function code is available */
560 sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
562 return setkey_fallback_skcipher(tfm, in_key, key_len);
564 sctx->key_len = key_len;
565 memcpy(sctx->key, in_key, key_len);
569 static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
573 /* only use complete blocks, max. PAGE_SIZE */
574 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
575 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
576 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
577 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
578 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
579 ctrptr += AES_BLOCK_SIZE;
584 static int ctr_aes_crypt(struct skcipher_request *req)
586 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
587 struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
588 u8 buf[AES_BLOCK_SIZE], *ctrptr;
589 struct skcipher_walk walk;
590 unsigned int n, nbytes;
593 if (unlikely(!sctx->fc))
594 return fallback_skcipher_crypt(sctx, req, 0);
596 locked = mutex_trylock(&ctrblk_lock);
598 ret = skcipher_walk_virt(&walk, req, false);
599 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
602 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
603 n = __ctrblk_init(ctrblk, walk.iv, nbytes);
604 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
605 cpacf_kmctr(sctx->fc, sctx->key, walk.dst.virt.addr,
606 walk.src.virt.addr, n, ctrptr);
607 if (ctrptr == ctrblk)
608 memcpy(walk.iv, ctrptr + n - AES_BLOCK_SIZE,
610 crypto_inc(walk.iv, AES_BLOCK_SIZE);
611 ret = skcipher_walk_done(&walk, nbytes - n);
614 mutex_unlock(&ctrblk_lock);
616 * final block may be < AES_BLOCK_SIZE, copy only nbytes
619 cpacf_kmctr(sctx->fc, sctx->key, buf, walk.src.virt.addr,
620 AES_BLOCK_SIZE, walk.iv);
621 memcpy(walk.dst.virt.addr, buf, nbytes);
622 crypto_inc(walk.iv, AES_BLOCK_SIZE);
623 ret = skcipher_walk_done(&walk, 0);
629 static struct skcipher_alg ctr_aes_alg = {
630 .base.cra_name = "ctr(aes)",
631 .base.cra_driver_name = "ctr-aes-s390",
632 .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
633 .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
634 .base.cra_blocksize = 1,
635 .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
636 .base.cra_module = THIS_MODULE,
637 .init = fallback_init_skcipher,
638 .exit = fallback_exit_skcipher,
639 .min_keysize = AES_MIN_KEY_SIZE,
640 .max_keysize = AES_MAX_KEY_SIZE,
641 .ivsize = AES_BLOCK_SIZE,
642 .setkey = ctr_aes_set_key,
643 .encrypt = ctr_aes_crypt,
644 .decrypt = ctr_aes_crypt,
645 .chunksize = AES_BLOCK_SIZE,
648 static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key,
651 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
654 case AES_KEYSIZE_128:
655 ctx->fc = CPACF_KMA_GCM_AES_128;
657 case AES_KEYSIZE_192:
658 ctx->fc = CPACF_KMA_GCM_AES_192;
660 case AES_KEYSIZE_256:
661 ctx->fc = CPACF_KMA_GCM_AES_256;
667 memcpy(ctx->key, key, keylen);
668 ctx->key_len = keylen;
672 static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
690 static void gcm_walk_start(struct gcm_sg_walk *gw, struct scatterlist *sg,
693 memset(gw, 0, sizeof(*gw));
694 gw->walk_bytes_remain = len;
695 scatterwalk_start(&gw->walk, sg);
698 static inline unsigned int _gcm_sg_clamp_and_map(struct gcm_sg_walk *gw)
700 struct scatterlist *nextsg;
702 gw->walk_bytes = scatterwalk_clamp(&gw->walk, gw->walk_bytes_remain);
703 while (!gw->walk_bytes) {
704 nextsg = sg_next(gw->walk.sg);
707 scatterwalk_start(&gw->walk, nextsg);
708 gw->walk_bytes = scatterwalk_clamp(&gw->walk,
709 gw->walk_bytes_remain);
711 gw->walk_ptr = scatterwalk_map(&gw->walk);
712 return gw->walk_bytes;
715 static inline void _gcm_sg_unmap_and_advance(struct gcm_sg_walk *gw,
718 gw->walk_bytes_remain -= nbytes;
719 scatterwalk_unmap(&gw->walk);
720 scatterwalk_advance(&gw->walk, nbytes);
721 scatterwalk_done(&gw->walk, 0, gw->walk_bytes_remain);
725 static int gcm_in_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
729 if (gw->buf_bytes && gw->buf_bytes >= minbytesneeded) {
731 gw->nbytes = gw->buf_bytes;
735 if (gw->walk_bytes_remain == 0) {
741 if (!_gcm_sg_clamp_and_map(gw)) {
747 if (!gw->buf_bytes && gw->walk_bytes >= minbytesneeded) {
748 gw->ptr = gw->walk_ptr;
749 gw->nbytes = gw->walk_bytes;
754 n = min(gw->walk_bytes, AES_BLOCK_SIZE - gw->buf_bytes);
755 memcpy(gw->buf + gw->buf_bytes, gw->walk_ptr, n);
757 _gcm_sg_unmap_and_advance(gw, n);
758 if (gw->buf_bytes >= minbytesneeded) {
760 gw->nbytes = gw->buf_bytes;
763 if (!_gcm_sg_clamp_and_map(gw)) {
774 static int gcm_out_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
776 if (gw->walk_bytes_remain == 0) {
782 if (!_gcm_sg_clamp_and_map(gw)) {
788 if (gw->walk_bytes >= minbytesneeded) {
789 gw->ptr = gw->walk_ptr;
790 gw->nbytes = gw->walk_bytes;
794 scatterwalk_unmap(&gw->walk);
798 gw->nbytes = sizeof(gw->buf);
804 static int gcm_in_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
809 if (gw->ptr == gw->buf) {
810 int n = gw->buf_bytes - bytesdone;
812 memmove(gw->buf, gw->buf + bytesdone, n);
817 _gcm_sg_unmap_and_advance(gw, bytesdone);
822 static int gcm_out_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
829 if (gw->ptr == gw->buf) {
830 for (i = 0; i < bytesdone; i += n) {
831 if (!_gcm_sg_clamp_and_map(gw))
833 n = min(gw->walk_bytes, bytesdone - i);
834 memcpy(gw->walk_ptr, gw->buf + i, n);
835 _gcm_sg_unmap_and_advance(gw, n);
838 _gcm_sg_unmap_and_advance(gw, bytesdone);
843 static int gcm_aes_crypt(struct aead_request *req, unsigned int flags)
845 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
846 struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
847 unsigned int ivsize = crypto_aead_ivsize(tfm);
848 unsigned int taglen = crypto_aead_authsize(tfm);
849 unsigned int aadlen = req->assoclen;
850 unsigned int pclen = req->cryptlen;
853 unsigned int n, len, in_bytes, out_bytes,
854 min_bytes, bytes, aad_bytes, pc_bytes;
855 struct gcm_sg_walk gw_in, gw_out;
856 u8 tag[GHASH_DIGEST_SIZE];
859 u32 _[3]; /* reserved */
860 u32 cv; /* Counter Value */
861 u8 t[GHASH_DIGEST_SIZE];/* Tag */
862 u8 h[AES_BLOCK_SIZE]; /* Hash-subkey */
863 u64 taadl; /* Total AAD Length */
864 u64 tpcl; /* Total Plain-/Cipher-text Length */
865 u8 j0[GHASH_BLOCK_SIZE];/* initial counter value */
866 u8 k[AES_MAX_KEY_SIZE]; /* Key */
871 * req->src: aad||plaintext
872 * req->dst: aad||ciphertext||tag
874 * req->src: aad||ciphertext||tag
875 * req->dst: aad||plaintext, return 0 or -EBADMSG
876 * aad, plaintext and ciphertext may be empty.
878 if (flags & CPACF_DECRYPT)
880 len = aadlen + pclen;
882 memset(¶m, 0, sizeof(param));
884 param.taadl = aadlen * 8;
885 param.tpcl = pclen * 8;
886 memcpy(param.j0, req->iv, ivsize);
887 *(u32 *)(param.j0 + ivsize) = 1;
888 memcpy(param.k, ctx->key, ctx->key_len);
890 gcm_walk_start(&gw_in, req->src, len);
891 gcm_walk_start(&gw_out, req->dst, len);
894 min_bytes = min_t(unsigned int,
895 aadlen > 0 ? aadlen : pclen, AES_BLOCK_SIZE);
896 in_bytes = gcm_in_walk_go(&gw_in, min_bytes);
897 out_bytes = gcm_out_walk_go(&gw_out, min_bytes);
898 bytes = min(in_bytes, out_bytes);
900 if (aadlen + pclen <= bytes) {
903 flags |= CPACF_KMA_LAAD | CPACF_KMA_LPC;
905 if (aadlen <= bytes) {
907 pc_bytes = (bytes - aadlen) &
908 ~(AES_BLOCK_SIZE - 1);
909 flags |= CPACF_KMA_LAAD;
911 aad_bytes = bytes & ~(AES_BLOCK_SIZE - 1);
917 memcpy(gw_out.ptr, gw_in.ptr, aad_bytes);
919 cpacf_kma(ctx->fc | flags, ¶m,
920 gw_out.ptr + aad_bytes,
921 gw_in.ptr + aad_bytes, pc_bytes,
922 gw_in.ptr, aad_bytes);
924 n = aad_bytes + pc_bytes;
925 if (gcm_in_walk_done(&gw_in, n) != n)
927 if (gcm_out_walk_done(&gw_out, n) != n)
931 } while (aadlen + pclen > 0);
933 if (flags & CPACF_DECRYPT) {
934 scatterwalk_map_and_copy(tag, req->src, len, taglen, 0);
935 if (crypto_memneq(tag, param.t, taglen))
938 scatterwalk_map_and_copy(param.t, req->dst, len, taglen, 1);
940 memzero_explicit(¶m, sizeof(param));
944 static int gcm_aes_encrypt(struct aead_request *req)
946 return gcm_aes_crypt(req, CPACF_ENCRYPT);
949 static int gcm_aes_decrypt(struct aead_request *req)
951 return gcm_aes_crypt(req, CPACF_DECRYPT);
954 static struct aead_alg gcm_aes_aead = {
955 .setkey = gcm_aes_setkey,
956 .setauthsize = gcm_aes_setauthsize,
957 .encrypt = gcm_aes_encrypt,
958 .decrypt = gcm_aes_decrypt,
960 .ivsize = GHASH_BLOCK_SIZE - sizeof(u32),
961 .maxauthsize = GHASH_DIGEST_SIZE,
962 .chunksize = AES_BLOCK_SIZE,
966 .cra_ctxsize = sizeof(struct s390_aes_ctx),
968 .cra_name = "gcm(aes)",
969 .cra_driver_name = "gcm-aes-s390",
970 .cra_module = THIS_MODULE,
974 static struct crypto_alg *aes_s390_alg;
975 static struct skcipher_alg *aes_s390_skcipher_algs[4];
976 static int aes_s390_skciphers_num;
977 static struct aead_alg *aes_s390_aead_alg;
979 static int aes_s390_register_skcipher(struct skcipher_alg *alg)
983 ret = crypto_register_skcipher(alg);
985 aes_s390_skcipher_algs[aes_s390_skciphers_num++] = alg;
989 static void aes_s390_fini(void)
992 crypto_unregister_alg(aes_s390_alg);
993 while (aes_s390_skciphers_num--)
994 crypto_unregister_skcipher(aes_s390_skcipher_algs[aes_s390_skciphers_num]);
996 free_page((unsigned long) ctrblk);
998 if (aes_s390_aead_alg)
999 crypto_unregister_aead(aes_s390_aead_alg);
1002 static int __init aes_s390_init(void)
1006 /* Query available functions for KM, KMC, KMCTR and KMA */
1007 cpacf_query(CPACF_KM, &km_functions);
1008 cpacf_query(CPACF_KMC, &kmc_functions);
1009 cpacf_query(CPACF_KMCTR, &kmctr_functions);
1010 cpacf_query(CPACF_KMA, &kma_functions);
1012 if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
1013 cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
1014 cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
1015 ret = crypto_register_alg(&aes_alg);
1018 aes_s390_alg = &aes_alg;
1019 ret = aes_s390_register_skcipher(&ecb_aes_alg);
1024 if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
1025 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
1026 cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
1027 ret = aes_s390_register_skcipher(&cbc_aes_alg);
1032 if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
1033 cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
1034 ret = aes_s390_register_skcipher(&xts_aes_alg);
1039 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
1040 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
1041 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
1042 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
1047 ret = aes_s390_register_skcipher(&ctr_aes_alg);
1052 if (cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_128) ||
1053 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_192) ||
1054 cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_256)) {
1055 ret = crypto_register_aead(&gcm_aes_aead);
1058 aes_s390_aead_alg = &gcm_aes_aead;
1067 module_cpu_feature_match(MSA, aes_s390_init);
1068 module_exit(aes_s390_fini);
1070 MODULE_ALIAS_CRYPTO("aes-all");
1072 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
1073 MODULE_LICENSE("GPL");