0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define KMSG_COMPONENT "paes_s390"
0014 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0015
0016 #include <crypto/aes.h>
0017 #include <crypto/algapi.h>
0018 #include <linux/bug.h>
0019 #include <linux/err.h>
0020 #include <linux/module.h>
0021 #include <linux/cpufeature.h>
0022 #include <linux/init.h>
0023 #include <linux/mutex.h>
0024 #include <linux/spinlock.h>
0025 #include <linux/delay.h>
0026 #include <crypto/internal/skcipher.h>
0027 #include <crypto/xts.h>
0028 #include <asm/cpacf.h>
0029 #include <asm/pkey.h>
0030
0031
0032
0033
0034
0035
0036
0037 #define PAES_MIN_KEYSIZE 16
0038 #define PAES_MAX_KEYSIZE 320
0039
0040 static u8 *ctrblk;
0041 static DEFINE_MUTEX(ctrblk_lock);
0042
0043 static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
0044
0045 struct key_blob {
0046
0047
0048
0049
0050
0051
0052
0053 u8 *key;
0054 u8 keybuf[128];
0055 unsigned int keylen;
0056 };
0057
0058 static inline int _key_to_kb(struct key_blob *kb,
0059 const u8 *key,
0060 unsigned int keylen)
0061 {
0062 struct clearkey_header {
0063 u8 type;
0064 u8 res0[3];
0065 u8 version;
0066 u8 res1[3];
0067 u32 keytype;
0068 u32 len;
0069 } __packed * h;
0070
0071 switch (keylen) {
0072 case 16:
0073 case 24:
0074 case 32:
0075
0076 memset(kb->keybuf, 0, sizeof(kb->keybuf));
0077 h = (struct clearkey_header *) kb->keybuf;
0078 h->version = 0x02;
0079 h->keytype = (keylen - 8) >> 3;
0080 h->len = keylen;
0081 memcpy(kb->keybuf + sizeof(*h), key, keylen);
0082 kb->keylen = sizeof(*h) + keylen;
0083 kb->key = kb->keybuf;
0084 break;
0085 default:
0086
0087 if (keylen <= sizeof(kb->keybuf))
0088 kb->key = kb->keybuf;
0089 else {
0090 kb->key = kmalloc(keylen, GFP_KERNEL);
0091 if (!kb->key)
0092 return -ENOMEM;
0093 }
0094 memcpy(kb->key, key, keylen);
0095 kb->keylen = keylen;
0096 break;
0097 }
0098
0099 return 0;
0100 }
0101
0102 static inline void _free_kb_keybuf(struct key_blob *kb)
0103 {
0104 if (kb->key && kb->key != kb->keybuf
0105 && kb->keylen > sizeof(kb->keybuf)) {
0106 kfree(kb->key);
0107 kb->key = NULL;
0108 }
0109 }
0110
0111 struct s390_paes_ctx {
0112 struct key_blob kb;
0113 struct pkey_protkey pk;
0114 spinlock_t pk_lock;
0115 unsigned long fc;
0116 };
0117
0118 struct s390_pxts_ctx {
0119 struct key_blob kb[2];
0120 struct pkey_protkey pk[2];
0121 spinlock_t pk_lock;
0122 unsigned long fc;
0123 };
0124
0125 static inline int __paes_keyblob2pkey(struct key_blob *kb,
0126 struct pkey_protkey *pk)
0127 {
0128 int i, ret;
0129
0130
0131 for (i = 0; i < 3; i++) {
0132 if (i > 0 && ret == -EAGAIN && in_task())
0133 if (msleep_interruptible(1000))
0134 return -EINTR;
0135 ret = pkey_keyblob2pkey(kb->key, kb->keylen, pk);
0136 if (ret == 0)
0137 break;
0138 }
0139
0140 return ret;
0141 }
0142
0143 static inline int __paes_convert_key(struct s390_paes_ctx *ctx)
0144 {
0145 int ret;
0146 struct pkey_protkey pkey;
0147
0148 ret = __paes_keyblob2pkey(&ctx->kb, &pkey);
0149 if (ret)
0150 return ret;
0151
0152 spin_lock_bh(&ctx->pk_lock);
0153 memcpy(&ctx->pk, &pkey, sizeof(pkey));
0154 spin_unlock_bh(&ctx->pk_lock);
0155
0156 return 0;
0157 }
0158
0159 static int ecb_paes_init(struct crypto_skcipher *tfm)
0160 {
0161 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0162
0163 ctx->kb.key = NULL;
0164 spin_lock_init(&ctx->pk_lock);
0165
0166 return 0;
0167 }
0168
0169 static void ecb_paes_exit(struct crypto_skcipher *tfm)
0170 {
0171 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0172
0173 _free_kb_keybuf(&ctx->kb);
0174 }
0175
0176 static inline int __ecb_paes_set_key(struct s390_paes_ctx *ctx)
0177 {
0178 int rc;
0179 unsigned long fc;
0180
0181 rc = __paes_convert_key(ctx);
0182 if (rc)
0183 return rc;
0184
0185
0186 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PAES_128 :
0187 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KM_PAES_192 :
0188 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KM_PAES_256 : 0;
0189
0190
0191 ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
0192
0193 return ctx->fc ? 0 : -EINVAL;
0194 }
0195
0196 static int ecb_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
0197 unsigned int key_len)
0198 {
0199 int rc;
0200 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0201
0202 _free_kb_keybuf(&ctx->kb);
0203 rc = _key_to_kb(&ctx->kb, in_key, key_len);
0204 if (rc)
0205 return rc;
0206
0207 return __ecb_paes_set_key(ctx);
0208 }
0209
0210 static int ecb_paes_crypt(struct skcipher_request *req, unsigned long modifier)
0211 {
0212 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0213 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0214 struct skcipher_walk walk;
0215 unsigned int nbytes, n, k;
0216 int ret;
0217 struct {
0218 u8 key[MAXPROTKEYSIZE];
0219 } param;
0220
0221 ret = skcipher_walk_virt(&walk, req, false);
0222 if (ret)
0223 return ret;
0224
0225 spin_lock_bh(&ctx->pk_lock);
0226 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0227 spin_unlock_bh(&ctx->pk_lock);
0228
0229 while ((nbytes = walk.nbytes) != 0) {
0230
0231 n = nbytes & ~(AES_BLOCK_SIZE - 1);
0232 k = cpacf_km(ctx->fc | modifier, ¶m,
0233 walk.dst.virt.addr, walk.src.virt.addr, n);
0234 if (k)
0235 ret = skcipher_walk_done(&walk, nbytes - k);
0236 if (k < n) {
0237 if (__paes_convert_key(ctx))
0238 return skcipher_walk_done(&walk, -EIO);
0239 spin_lock_bh(&ctx->pk_lock);
0240 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0241 spin_unlock_bh(&ctx->pk_lock);
0242 }
0243 }
0244 return ret;
0245 }
0246
0247 static int ecb_paes_encrypt(struct skcipher_request *req)
0248 {
0249 return ecb_paes_crypt(req, 0);
0250 }
0251
0252 static int ecb_paes_decrypt(struct skcipher_request *req)
0253 {
0254 return ecb_paes_crypt(req, CPACF_DECRYPT);
0255 }
0256
0257 static struct skcipher_alg ecb_paes_alg = {
0258 .base.cra_name = "ecb(paes)",
0259 .base.cra_driver_name = "ecb-paes-s390",
0260 .base.cra_priority = 401,
0261 .base.cra_blocksize = AES_BLOCK_SIZE,
0262 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
0263 .base.cra_module = THIS_MODULE,
0264 .base.cra_list = LIST_HEAD_INIT(ecb_paes_alg.base.cra_list),
0265 .init = ecb_paes_init,
0266 .exit = ecb_paes_exit,
0267 .min_keysize = PAES_MIN_KEYSIZE,
0268 .max_keysize = PAES_MAX_KEYSIZE,
0269 .setkey = ecb_paes_set_key,
0270 .encrypt = ecb_paes_encrypt,
0271 .decrypt = ecb_paes_decrypt,
0272 };
0273
0274 static int cbc_paes_init(struct crypto_skcipher *tfm)
0275 {
0276 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0277
0278 ctx->kb.key = NULL;
0279 spin_lock_init(&ctx->pk_lock);
0280
0281 return 0;
0282 }
0283
0284 static void cbc_paes_exit(struct crypto_skcipher *tfm)
0285 {
0286 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0287
0288 _free_kb_keybuf(&ctx->kb);
0289 }
0290
0291 static inline int __cbc_paes_set_key(struct s390_paes_ctx *ctx)
0292 {
0293 int rc;
0294 unsigned long fc;
0295
0296 rc = __paes_convert_key(ctx);
0297 if (rc)
0298 return rc;
0299
0300
0301 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMC_PAES_128 :
0302 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMC_PAES_192 :
0303 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ? CPACF_KMC_PAES_256 : 0;
0304
0305
0306 ctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
0307
0308 return ctx->fc ? 0 : -EINVAL;
0309 }
0310
0311 static int cbc_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
0312 unsigned int key_len)
0313 {
0314 int rc;
0315 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0316
0317 _free_kb_keybuf(&ctx->kb);
0318 rc = _key_to_kb(&ctx->kb, in_key, key_len);
0319 if (rc)
0320 return rc;
0321
0322 return __cbc_paes_set_key(ctx);
0323 }
0324
0325 static int cbc_paes_crypt(struct skcipher_request *req, unsigned long modifier)
0326 {
0327 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0328 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0329 struct skcipher_walk walk;
0330 unsigned int nbytes, n, k;
0331 int ret;
0332 struct {
0333 u8 iv[AES_BLOCK_SIZE];
0334 u8 key[MAXPROTKEYSIZE];
0335 } param;
0336
0337 ret = skcipher_walk_virt(&walk, req, false);
0338 if (ret)
0339 return ret;
0340
0341 memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
0342 spin_lock_bh(&ctx->pk_lock);
0343 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0344 spin_unlock_bh(&ctx->pk_lock);
0345
0346 while ((nbytes = walk.nbytes) != 0) {
0347
0348 n = nbytes & ~(AES_BLOCK_SIZE - 1);
0349 k = cpacf_kmc(ctx->fc | modifier, ¶m,
0350 walk.dst.virt.addr, walk.src.virt.addr, n);
0351 if (k) {
0352 memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
0353 ret = skcipher_walk_done(&walk, nbytes - k);
0354 }
0355 if (k < n) {
0356 if (__paes_convert_key(ctx))
0357 return skcipher_walk_done(&walk, -EIO);
0358 spin_lock_bh(&ctx->pk_lock);
0359 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0360 spin_unlock_bh(&ctx->pk_lock);
0361 }
0362 }
0363 return ret;
0364 }
0365
0366 static int cbc_paes_encrypt(struct skcipher_request *req)
0367 {
0368 return cbc_paes_crypt(req, 0);
0369 }
0370
0371 static int cbc_paes_decrypt(struct skcipher_request *req)
0372 {
0373 return cbc_paes_crypt(req, CPACF_DECRYPT);
0374 }
0375
0376 static struct skcipher_alg cbc_paes_alg = {
0377 .base.cra_name = "cbc(paes)",
0378 .base.cra_driver_name = "cbc-paes-s390",
0379 .base.cra_priority = 402,
0380 .base.cra_blocksize = AES_BLOCK_SIZE,
0381 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
0382 .base.cra_module = THIS_MODULE,
0383 .base.cra_list = LIST_HEAD_INIT(cbc_paes_alg.base.cra_list),
0384 .init = cbc_paes_init,
0385 .exit = cbc_paes_exit,
0386 .min_keysize = PAES_MIN_KEYSIZE,
0387 .max_keysize = PAES_MAX_KEYSIZE,
0388 .ivsize = AES_BLOCK_SIZE,
0389 .setkey = cbc_paes_set_key,
0390 .encrypt = cbc_paes_encrypt,
0391 .decrypt = cbc_paes_decrypt,
0392 };
0393
0394 static int xts_paes_init(struct crypto_skcipher *tfm)
0395 {
0396 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
0397
0398 ctx->kb[0].key = NULL;
0399 ctx->kb[1].key = NULL;
0400 spin_lock_init(&ctx->pk_lock);
0401
0402 return 0;
0403 }
0404
0405 static void xts_paes_exit(struct crypto_skcipher *tfm)
0406 {
0407 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
0408
0409 _free_kb_keybuf(&ctx->kb[0]);
0410 _free_kb_keybuf(&ctx->kb[1]);
0411 }
0412
0413 static inline int __xts_paes_convert_key(struct s390_pxts_ctx *ctx)
0414 {
0415 struct pkey_protkey pkey0, pkey1;
0416
0417 if (__paes_keyblob2pkey(&ctx->kb[0], &pkey0) ||
0418 __paes_keyblob2pkey(&ctx->kb[1], &pkey1))
0419 return -EINVAL;
0420
0421 spin_lock_bh(&ctx->pk_lock);
0422 memcpy(&ctx->pk[0], &pkey0, sizeof(pkey0));
0423 memcpy(&ctx->pk[1], &pkey1, sizeof(pkey1));
0424 spin_unlock_bh(&ctx->pk_lock);
0425
0426 return 0;
0427 }
0428
0429 static inline int __xts_paes_set_key(struct s390_pxts_ctx *ctx)
0430 {
0431 unsigned long fc;
0432
0433 if (__xts_paes_convert_key(ctx))
0434 return -EINVAL;
0435
0436 if (ctx->pk[0].type != ctx->pk[1].type)
0437 return -EINVAL;
0438
0439
0440 fc = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? CPACF_KM_PXTS_128 :
0441 (ctx->pk[0].type == PKEY_KEYTYPE_AES_256) ?
0442 CPACF_KM_PXTS_256 : 0;
0443
0444
0445 ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
0446
0447 return ctx->fc ? 0 : -EINVAL;
0448 }
0449
0450 static int xts_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
0451 unsigned int xts_key_len)
0452 {
0453 int rc;
0454 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
0455 u8 ckey[2 * AES_MAX_KEY_SIZE];
0456 unsigned int ckey_len, key_len;
0457
0458 if (xts_key_len % 2)
0459 return -EINVAL;
0460
0461 key_len = xts_key_len / 2;
0462
0463 _free_kb_keybuf(&ctx->kb[0]);
0464 _free_kb_keybuf(&ctx->kb[1]);
0465 rc = _key_to_kb(&ctx->kb[0], in_key, key_len);
0466 if (rc)
0467 return rc;
0468 rc = _key_to_kb(&ctx->kb[1], in_key + key_len, key_len);
0469 if (rc)
0470 return rc;
0471
0472 rc = __xts_paes_set_key(ctx);
0473 if (rc)
0474 return rc;
0475
0476
0477
0478
0479
0480
0481 ckey_len = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ?
0482 AES_KEYSIZE_128 : AES_KEYSIZE_256;
0483 memcpy(ckey, ctx->pk[0].protkey, ckey_len);
0484 memcpy(ckey + ckey_len, ctx->pk[1].protkey, ckey_len);
0485 return xts_verify_key(tfm, ckey, 2*ckey_len);
0486 }
0487
0488 static int xts_paes_crypt(struct skcipher_request *req, unsigned long modifier)
0489 {
0490 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0491 struct s390_pxts_ctx *ctx = crypto_skcipher_ctx(tfm);
0492 struct skcipher_walk walk;
0493 unsigned int keylen, offset, nbytes, n, k;
0494 int ret;
0495 struct {
0496 u8 key[MAXPROTKEYSIZE];
0497 u8 tweak[16];
0498 u8 block[16];
0499 u8 bit[16];
0500 u8 xts[16];
0501 } pcc_param;
0502 struct {
0503 u8 key[MAXPROTKEYSIZE];
0504 u8 init[16];
0505 } xts_param;
0506
0507 ret = skcipher_walk_virt(&walk, req, false);
0508 if (ret)
0509 return ret;
0510
0511 keylen = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 48 : 64;
0512 offset = (ctx->pk[0].type == PKEY_KEYTYPE_AES_128) ? 16 : 0;
0513
0514 memset(&pcc_param, 0, sizeof(pcc_param));
0515 memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
0516 spin_lock_bh(&ctx->pk_lock);
0517 memcpy(pcc_param.key + offset, ctx->pk[1].protkey, keylen);
0518 memcpy(xts_param.key + offset, ctx->pk[0].protkey, keylen);
0519 spin_unlock_bh(&ctx->pk_lock);
0520 cpacf_pcc(ctx->fc, pcc_param.key + offset);
0521 memcpy(xts_param.init, pcc_param.xts, 16);
0522
0523 while ((nbytes = walk.nbytes) != 0) {
0524
0525 n = nbytes & ~(AES_BLOCK_SIZE - 1);
0526 k = cpacf_km(ctx->fc | modifier, xts_param.key + offset,
0527 walk.dst.virt.addr, walk.src.virt.addr, n);
0528 if (k)
0529 ret = skcipher_walk_done(&walk, nbytes - k);
0530 if (k < n) {
0531 if (__xts_paes_convert_key(ctx))
0532 return skcipher_walk_done(&walk, -EIO);
0533 spin_lock_bh(&ctx->pk_lock);
0534 memcpy(xts_param.key + offset,
0535 ctx->pk[0].protkey, keylen);
0536 spin_unlock_bh(&ctx->pk_lock);
0537 }
0538 }
0539
0540 return ret;
0541 }
0542
0543 static int xts_paes_encrypt(struct skcipher_request *req)
0544 {
0545 return xts_paes_crypt(req, 0);
0546 }
0547
0548 static int xts_paes_decrypt(struct skcipher_request *req)
0549 {
0550 return xts_paes_crypt(req, CPACF_DECRYPT);
0551 }
0552
0553 static struct skcipher_alg xts_paes_alg = {
0554 .base.cra_name = "xts(paes)",
0555 .base.cra_driver_name = "xts-paes-s390",
0556 .base.cra_priority = 402,
0557 .base.cra_blocksize = AES_BLOCK_SIZE,
0558 .base.cra_ctxsize = sizeof(struct s390_pxts_ctx),
0559 .base.cra_module = THIS_MODULE,
0560 .base.cra_list = LIST_HEAD_INIT(xts_paes_alg.base.cra_list),
0561 .init = xts_paes_init,
0562 .exit = xts_paes_exit,
0563 .min_keysize = 2 * PAES_MIN_KEYSIZE,
0564 .max_keysize = 2 * PAES_MAX_KEYSIZE,
0565 .ivsize = AES_BLOCK_SIZE,
0566 .setkey = xts_paes_set_key,
0567 .encrypt = xts_paes_encrypt,
0568 .decrypt = xts_paes_decrypt,
0569 };
0570
0571 static int ctr_paes_init(struct crypto_skcipher *tfm)
0572 {
0573 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0574
0575 ctx->kb.key = NULL;
0576 spin_lock_init(&ctx->pk_lock);
0577
0578 return 0;
0579 }
0580
0581 static void ctr_paes_exit(struct crypto_skcipher *tfm)
0582 {
0583 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0584
0585 _free_kb_keybuf(&ctx->kb);
0586 }
0587
0588 static inline int __ctr_paes_set_key(struct s390_paes_ctx *ctx)
0589 {
0590 int rc;
0591 unsigned long fc;
0592
0593 rc = __paes_convert_key(ctx);
0594 if (rc)
0595 return rc;
0596
0597
0598 fc = (ctx->pk.type == PKEY_KEYTYPE_AES_128) ? CPACF_KMCTR_PAES_128 :
0599 (ctx->pk.type == PKEY_KEYTYPE_AES_192) ? CPACF_KMCTR_PAES_192 :
0600 (ctx->pk.type == PKEY_KEYTYPE_AES_256) ?
0601 CPACF_KMCTR_PAES_256 : 0;
0602
0603
0604 ctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
0605
0606 return ctx->fc ? 0 : -EINVAL;
0607 }
0608
0609 static int ctr_paes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
0610 unsigned int key_len)
0611 {
0612 int rc;
0613 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0614
0615 _free_kb_keybuf(&ctx->kb);
0616 rc = _key_to_kb(&ctx->kb, in_key, key_len);
0617 if (rc)
0618 return rc;
0619
0620 return __ctr_paes_set_key(ctx);
0621 }
0622
0623 static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
0624 {
0625 unsigned int i, n;
0626
0627
0628 memcpy(ctrptr, iv, AES_BLOCK_SIZE);
0629 n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
0630 for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
0631 memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
0632 crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
0633 ctrptr += AES_BLOCK_SIZE;
0634 }
0635 return n;
0636 }
0637
0638 static int ctr_paes_crypt(struct skcipher_request *req)
0639 {
0640 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0641 struct s390_paes_ctx *ctx = crypto_skcipher_ctx(tfm);
0642 u8 buf[AES_BLOCK_SIZE], *ctrptr;
0643 struct skcipher_walk walk;
0644 unsigned int nbytes, n, k;
0645 int ret, locked;
0646 struct {
0647 u8 key[MAXPROTKEYSIZE];
0648 } param;
0649
0650 ret = skcipher_walk_virt(&walk, req, false);
0651 if (ret)
0652 return ret;
0653
0654 spin_lock_bh(&ctx->pk_lock);
0655 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0656 spin_unlock_bh(&ctx->pk_lock);
0657
0658 locked = mutex_trylock(&ctrblk_lock);
0659
0660 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
0661 n = AES_BLOCK_SIZE;
0662 if (nbytes >= 2*AES_BLOCK_SIZE && locked)
0663 n = __ctrblk_init(ctrblk, walk.iv, nbytes);
0664 ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
0665 k = cpacf_kmctr(ctx->fc, ¶m, walk.dst.virt.addr,
0666 walk.src.virt.addr, n, ctrptr);
0667 if (k) {
0668 if (ctrptr == ctrblk)
0669 memcpy(walk.iv, ctrptr + k - AES_BLOCK_SIZE,
0670 AES_BLOCK_SIZE);
0671 crypto_inc(walk.iv, AES_BLOCK_SIZE);
0672 ret = skcipher_walk_done(&walk, nbytes - k);
0673 }
0674 if (k < n) {
0675 if (__paes_convert_key(ctx)) {
0676 if (locked)
0677 mutex_unlock(&ctrblk_lock);
0678 return skcipher_walk_done(&walk, -EIO);
0679 }
0680 spin_lock_bh(&ctx->pk_lock);
0681 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0682 spin_unlock_bh(&ctx->pk_lock);
0683 }
0684 }
0685 if (locked)
0686 mutex_unlock(&ctrblk_lock);
0687
0688
0689
0690 if (nbytes) {
0691 while (1) {
0692 if (cpacf_kmctr(ctx->fc, ¶m, buf,
0693 walk.src.virt.addr, AES_BLOCK_SIZE,
0694 walk.iv) == AES_BLOCK_SIZE)
0695 break;
0696 if (__paes_convert_key(ctx))
0697 return skcipher_walk_done(&walk, -EIO);
0698 spin_lock_bh(&ctx->pk_lock);
0699 memcpy(param.key, ctx->pk.protkey, MAXPROTKEYSIZE);
0700 spin_unlock_bh(&ctx->pk_lock);
0701 }
0702 memcpy(walk.dst.virt.addr, buf, nbytes);
0703 crypto_inc(walk.iv, AES_BLOCK_SIZE);
0704 ret = skcipher_walk_done(&walk, nbytes);
0705 }
0706
0707 return ret;
0708 }
0709
0710 static struct skcipher_alg ctr_paes_alg = {
0711 .base.cra_name = "ctr(paes)",
0712 .base.cra_driver_name = "ctr-paes-s390",
0713 .base.cra_priority = 402,
0714 .base.cra_blocksize = 1,
0715 .base.cra_ctxsize = sizeof(struct s390_paes_ctx),
0716 .base.cra_module = THIS_MODULE,
0717 .base.cra_list = LIST_HEAD_INIT(ctr_paes_alg.base.cra_list),
0718 .init = ctr_paes_init,
0719 .exit = ctr_paes_exit,
0720 .min_keysize = PAES_MIN_KEYSIZE,
0721 .max_keysize = PAES_MAX_KEYSIZE,
0722 .ivsize = AES_BLOCK_SIZE,
0723 .setkey = ctr_paes_set_key,
0724 .encrypt = ctr_paes_crypt,
0725 .decrypt = ctr_paes_crypt,
0726 .chunksize = AES_BLOCK_SIZE,
0727 };
0728
0729 static inline void __crypto_unregister_skcipher(struct skcipher_alg *alg)
0730 {
0731 if (!list_empty(&alg->base.cra_list))
0732 crypto_unregister_skcipher(alg);
0733 }
0734
0735 static void paes_s390_fini(void)
0736 {
0737 __crypto_unregister_skcipher(&ctr_paes_alg);
0738 __crypto_unregister_skcipher(&xts_paes_alg);
0739 __crypto_unregister_skcipher(&cbc_paes_alg);
0740 __crypto_unregister_skcipher(&ecb_paes_alg);
0741 if (ctrblk)
0742 free_page((unsigned long) ctrblk);
0743 }
0744
0745 static int __init paes_s390_init(void)
0746 {
0747 int ret;
0748
0749
0750 cpacf_query(CPACF_KM, &km_functions);
0751 cpacf_query(CPACF_KMC, &kmc_functions);
0752 cpacf_query(CPACF_KMCTR, &kmctr_functions);
0753
0754 if (cpacf_test_func(&km_functions, CPACF_KM_PAES_128) ||
0755 cpacf_test_func(&km_functions, CPACF_KM_PAES_192) ||
0756 cpacf_test_func(&km_functions, CPACF_KM_PAES_256)) {
0757 ret = crypto_register_skcipher(&ecb_paes_alg);
0758 if (ret)
0759 goto out_err;
0760 }
0761
0762 if (cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
0763 cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
0764 cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256)) {
0765 ret = crypto_register_skcipher(&cbc_paes_alg);
0766 if (ret)
0767 goto out_err;
0768 }
0769
0770 if (cpacf_test_func(&km_functions, CPACF_KM_PXTS_128) ||
0771 cpacf_test_func(&km_functions, CPACF_KM_PXTS_256)) {
0772 ret = crypto_register_skcipher(&xts_paes_alg);
0773 if (ret)
0774 goto out_err;
0775 }
0776
0777 if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_128) ||
0778 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_192) ||
0779 cpacf_test_func(&kmctr_functions, CPACF_KMCTR_PAES_256)) {
0780 ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
0781 if (!ctrblk) {
0782 ret = -ENOMEM;
0783 goto out_err;
0784 }
0785 ret = crypto_register_skcipher(&ctr_paes_alg);
0786 if (ret)
0787 goto out_err;
0788 }
0789
0790 return 0;
0791 out_err:
0792 paes_s390_fini();
0793 return ret;
0794 }
0795
0796 module_init(paes_s390_init);
0797 module_exit(paes_s390_fini);
0798
0799 MODULE_ALIAS_CRYPTO("paes");
0800
0801 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm with protected keys");
0802 MODULE_LICENSE("GPL");