Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  pkey device driver
0004  *
0005  *  Copyright IBM Corp. 2017,2019
0006  *  Author(s): Harald Freudenberger
0007  */
0008 
0009 #define KMSG_COMPONENT "pkey"
0010 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0011 
0012 #include <linux/fs.h>
0013 #include <linux/init.h>
0014 #include <linux/miscdevice.h>
0015 #include <linux/module.h>
0016 #include <linux/slab.h>
0017 #include <linux/kallsyms.h>
0018 #include <linux/debugfs.h>
0019 #include <linux/random.h>
0020 #include <linux/cpufeature.h>
0021 #include <asm/zcrypt.h>
0022 #include <asm/cpacf.h>
0023 #include <asm/pkey.h>
0024 #include <crypto/aes.h>
0025 
0026 #include "zcrypt_api.h"
0027 #include "zcrypt_ccamisc.h"
0028 #include "zcrypt_ep11misc.h"
0029 
0030 MODULE_LICENSE("GPL");
0031 MODULE_AUTHOR("IBM Corporation");
0032 MODULE_DESCRIPTION("s390 protected key interface");
0033 
0034 #define KEYBLOBBUFSIZE 8192 /* key buffer size used for internal processing */
0035 #define PROTKEYBLOBBUFSIZE 256  /* protected key buffer size used internal */
0036 #define MAXAPQNSINLIST 64   /* max 64 apqns within a apqn list */
0037 
0038 /*
0039  * debug feature data and functions
0040  */
0041 
0042 static debug_info_t *debug_info;
0043 
0044 #define DEBUG_DBG(...)  debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
0045 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
0046 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
0047 #define DEBUG_ERR(...)  debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
0048 
0049 static void __init pkey_debug_init(void)
0050 {
0051     /* 5 arguments per dbf entry (including the format string ptr) */
0052     debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
0053     debug_register_view(debug_info, &debug_sprintf_view);
0054     debug_set_level(debug_info, 3);
0055 }
0056 
0057 static void __exit pkey_debug_exit(void)
0058 {
0059     debug_unregister(debug_info);
0060 }
0061 
0062 /* inside view of a protected key token (only type 0x00 version 0x01) */
0063 struct protaeskeytoken {
0064     u8  type;     /* 0x00 for PAES specific key tokens */
0065     u8  res0[3];
0066     u8  version;  /* should be 0x01 for protected AES key token */
0067     u8  res1[3];
0068     u32 keytype;  /* key type, one of the PKEY_KEYTYPE values */
0069     u32 len;      /* bytes actually stored in protkey[] */
0070     u8  protkey[MAXPROTKEYSIZE]; /* the protected key blob */
0071 } __packed;
0072 
0073 /* inside view of a clear key token (type 0x00 version 0x02) */
0074 struct clearaeskeytoken {
0075     u8  type;    /* 0x00 for PAES specific key tokens */
0076     u8  res0[3];
0077     u8  version;     /* 0x02 for clear AES key token */
0078     u8  res1[3];
0079     u32 keytype;     /* key type, one of the PKEY_KEYTYPE values */
0080     u32 len;     /* bytes actually stored in clearkey[] */
0081     u8  clearkey[]; /* clear key value */
0082 } __packed;
0083 
0084 /*
0085  * Create a protected key from a clear key value.
0086  */
0087 static int pkey_clr2protkey(u32 keytype,
0088                 const struct pkey_clrkey *clrkey,
0089                 struct pkey_protkey *protkey)
0090 {
0091     /* mask of available pckmo subfunctions */
0092     static cpacf_mask_t pckmo_functions;
0093 
0094     long fc;
0095     int keysize;
0096     u8 paramblock[64];
0097 
0098     switch (keytype) {
0099     case PKEY_KEYTYPE_AES_128:
0100         keysize = 16;
0101         fc = CPACF_PCKMO_ENC_AES_128_KEY;
0102         break;
0103     case PKEY_KEYTYPE_AES_192:
0104         keysize = 24;
0105         fc = CPACF_PCKMO_ENC_AES_192_KEY;
0106         break;
0107     case PKEY_KEYTYPE_AES_256:
0108         keysize = 32;
0109         fc = CPACF_PCKMO_ENC_AES_256_KEY;
0110         break;
0111     default:
0112         DEBUG_ERR("%s unknown/unsupported keytype %d\n",
0113               __func__, keytype);
0114         return -EINVAL;
0115     }
0116 
0117     /* Did we already check for PCKMO ? */
0118     if (!pckmo_functions.bytes[0]) {
0119         /* no, so check now */
0120         if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
0121             return -ENODEV;
0122     }
0123     /* check for the pckmo subfunction we need now */
0124     if (!cpacf_test_func(&pckmo_functions, fc)) {
0125         DEBUG_ERR("%s pckmo functions not available\n", __func__);
0126         return -ENODEV;
0127     }
0128 
0129     /* prepare param block */
0130     memset(paramblock, 0, sizeof(paramblock));
0131     memcpy(paramblock, clrkey->clrkey, keysize);
0132 
0133     /* call the pckmo instruction */
0134     cpacf_pckmo(fc, paramblock);
0135 
0136     /* copy created protected key */
0137     protkey->type = keytype;
0138     protkey->len = keysize + 32;
0139     memcpy(protkey->protkey, paramblock, keysize + 32);
0140 
0141     return 0;
0142 }
0143 
0144 /*
0145  * Find card and transform secure key into protected key.
0146  */
0147 static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
0148 {
0149     int rc, verify;
0150     u16 cardnr, domain;
0151     struct keytoken_header *hdr = (struct keytoken_header *)key;
0152 
0153     zcrypt_wait_api_operational();
0154 
0155     /*
0156      * The cca_xxx2protkey call may fail when a card has been
0157      * addressed where the master key was changed after last fetch
0158      * of the mkvp into the cache. Try 3 times: First without verify
0159      * then with verify and last round with verify and old master
0160      * key verification pattern match not ignored.
0161      */
0162     for (verify = 0; verify < 3; verify++) {
0163         rc = cca_findcard(key, &cardnr, &domain, verify);
0164         if (rc < 0)
0165             continue;
0166         if (rc > 0 && verify < 2)
0167             continue;
0168         switch (hdr->version) {
0169         case TOKVER_CCA_AES:
0170             rc = cca_sec2protkey(cardnr, domain,
0171                          key, pkey->protkey,
0172                          &pkey->len, &pkey->type);
0173             break;
0174         case TOKVER_CCA_VLSC:
0175             rc = cca_cipher2protkey(cardnr, domain,
0176                         key, pkey->protkey,
0177                         &pkey->len, &pkey->type);
0178             break;
0179         default:
0180             return -EINVAL;
0181         }
0182         if (rc == 0)
0183             break;
0184     }
0185 
0186     if (rc)
0187         DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
0188 
0189     return rc;
0190 }
0191 
0192 /*
0193  * Construct EP11 key with given clear key value.
0194  */
0195 static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
0196                 u8 *keybuf, size_t *keybuflen)
0197 {
0198     int i, rc;
0199     u16 card, dom;
0200     u32 nr_apqns, *apqns = NULL;
0201 
0202     zcrypt_wait_api_operational();
0203 
0204     /* build a list of apqns suitable for ep11 keys with cpacf support */
0205     rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
0206                 ZCRYPT_CEX7, EP11_API_V, NULL);
0207     if (rc)
0208         goto out;
0209 
0210     /* go through the list of apqns and try to bild an ep11 key */
0211     for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
0212         card = apqns[i] >> 16;
0213         dom = apqns[i] & 0xFFFF;
0214         rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
0215                       0, clrkey, keybuf, keybuflen);
0216         if (rc == 0)
0217             break;
0218     }
0219 
0220 out:
0221     kfree(apqns);
0222     if (rc)
0223         DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
0224     return rc;
0225 }
0226 
0227 /*
0228  * Find card and transform EP11 secure key into protected key.
0229  */
0230 static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
0231 {
0232     int i, rc;
0233     u16 card, dom;
0234     u32 nr_apqns, *apqns = NULL;
0235     struct ep11keyblob *kb = (struct ep11keyblob *)key;
0236 
0237     zcrypt_wait_api_operational();
0238 
0239     /* build a list of apqns suitable for this key */
0240     rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
0241                 ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
0242     if (rc)
0243         goto out;
0244 
0245     /* go through the list of apqns and try to derive an pkey */
0246     for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
0247         card = apqns[i] >> 16;
0248         dom = apqns[i] & 0xFFFF;
0249         pkey->len = sizeof(pkey->protkey);
0250         rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
0251                     pkey->protkey, &pkey->len, &pkey->type);
0252         if (rc == 0)
0253             break;
0254     }
0255 
0256 out:
0257     kfree(apqns);
0258     if (rc)
0259         DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
0260     return rc;
0261 }
0262 
0263 /*
0264  * Verify key and give back some info about the key.
0265  */
0266 static int pkey_verifykey(const struct pkey_seckey *seckey,
0267               u16 *pcardnr, u16 *pdomain,
0268               u16 *pkeysize, u32 *pattributes)
0269 {
0270     struct secaeskeytoken *t = (struct secaeskeytoken *)seckey;
0271     u16 cardnr, domain;
0272     int rc;
0273 
0274     /* check the secure key for valid AES secure key */
0275     rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *)seckey, 0);
0276     if (rc)
0277         goto out;
0278     if (pattributes)
0279         *pattributes = PKEY_VERIFY_ATTR_AES;
0280     if (pkeysize)
0281         *pkeysize = t->bitsize;
0282 
0283     /* try to find a card which can handle this key */
0284     rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
0285     if (rc < 0)
0286         goto out;
0287 
0288     if (rc > 0) {
0289         /* key mkvp matches to old master key mkvp */
0290         DEBUG_DBG("%s secure key has old mkvp\n", __func__);
0291         if (pattributes)
0292             *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
0293         rc = 0;
0294     }
0295 
0296     if (pcardnr)
0297         *pcardnr = cardnr;
0298     if (pdomain)
0299         *pdomain = domain;
0300 
0301 out:
0302     DEBUG_DBG("%s rc=%d\n", __func__, rc);
0303     return rc;
0304 }
0305 
0306 /*
0307  * Generate a random protected key
0308  */
0309 static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
0310 {
0311     struct pkey_clrkey clrkey;
0312     int keysize;
0313     int rc;
0314 
0315     switch (keytype) {
0316     case PKEY_KEYTYPE_AES_128:
0317         keysize = 16;
0318         break;
0319     case PKEY_KEYTYPE_AES_192:
0320         keysize = 24;
0321         break;
0322     case PKEY_KEYTYPE_AES_256:
0323         keysize = 32;
0324         break;
0325     default:
0326         DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
0327               keytype);
0328         return -EINVAL;
0329     }
0330 
0331     /* generate a dummy random clear key */
0332     get_random_bytes(clrkey.clrkey, keysize);
0333 
0334     /* convert it to a dummy protected key */
0335     rc = pkey_clr2protkey(keytype, &clrkey, protkey);
0336     if (rc)
0337         return rc;
0338 
0339     /* replace the key part of the protected key with random bytes */
0340     get_random_bytes(protkey->protkey, keysize);
0341 
0342     return 0;
0343 }
0344 
0345 /*
0346  * Verify if a protected key is still valid
0347  */
0348 static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
0349 {
0350     unsigned long fc;
0351     struct {
0352         u8 iv[AES_BLOCK_SIZE];
0353         u8 key[MAXPROTKEYSIZE];
0354     } param;
0355     u8 null_msg[AES_BLOCK_SIZE];
0356     u8 dest_buf[AES_BLOCK_SIZE];
0357     unsigned int k;
0358 
0359     switch (protkey->type) {
0360     case PKEY_KEYTYPE_AES_128:
0361         fc = CPACF_KMC_PAES_128;
0362         break;
0363     case PKEY_KEYTYPE_AES_192:
0364         fc = CPACF_KMC_PAES_192;
0365         break;
0366     case PKEY_KEYTYPE_AES_256:
0367         fc = CPACF_KMC_PAES_256;
0368         break;
0369     default:
0370         DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
0371               protkey->type);
0372         return -EINVAL;
0373     }
0374 
0375     memset(null_msg, 0, sizeof(null_msg));
0376 
0377     memset(param.iv, 0, sizeof(param.iv));
0378     memcpy(param.key, protkey->protkey, sizeof(param.key));
0379 
0380     k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
0381               sizeof(null_msg));
0382     if (k != sizeof(null_msg)) {
0383         DEBUG_ERR("%s protected key is not valid\n", __func__);
0384         return -EKEYREJECTED;
0385     }
0386 
0387     return 0;
0388 }
0389 
0390 /*
0391  * Transform a non-CCA key token into a protected key
0392  */
0393 static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
0394                    struct pkey_protkey *protkey)
0395 {
0396     int rc = -EINVAL;
0397     u8 *tmpbuf = NULL;
0398     struct keytoken_header *hdr = (struct keytoken_header *)key;
0399 
0400     switch (hdr->version) {
0401     case TOKVER_PROTECTED_KEY: {
0402         struct protaeskeytoken *t;
0403 
0404         if (keylen != sizeof(struct protaeskeytoken))
0405             goto out;
0406         t = (struct protaeskeytoken *)key;
0407         protkey->len = t->len;
0408         protkey->type = t->keytype;
0409         memcpy(protkey->protkey, t->protkey,
0410                sizeof(protkey->protkey));
0411         rc = pkey_verifyprotkey(protkey);
0412         break;
0413     }
0414     case TOKVER_CLEAR_KEY: {
0415         struct clearaeskeytoken *t;
0416         struct pkey_clrkey ckey;
0417         union u_tmpbuf {
0418             u8 skey[SECKEYBLOBSIZE];
0419             u8 ep11key[MAXEP11AESKEYBLOBSIZE];
0420         };
0421         size_t tmpbuflen = sizeof(union u_tmpbuf);
0422 
0423         if (keylen < sizeof(struct clearaeskeytoken))
0424             goto out;
0425         t = (struct clearaeskeytoken *)key;
0426         if (keylen != sizeof(*t) + t->len)
0427             goto out;
0428         if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16) ||
0429             (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24) ||
0430             (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
0431             memcpy(ckey.clrkey, t->clearkey, t->len);
0432         else
0433             goto out;
0434         /* alloc temp key buffer space */
0435         tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
0436         if (!tmpbuf) {
0437             rc = -ENOMEM;
0438             goto out;
0439         }
0440         /* try direct way with the PCKMO instruction */
0441         rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
0442         if (rc == 0)
0443             break;
0444         /* PCKMO failed, so try the CCA secure key way */
0445         zcrypt_wait_api_operational();
0446         rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
0447                     ckey.clrkey, tmpbuf);
0448         if (rc == 0)
0449             rc = pkey_skey2pkey(tmpbuf, protkey);
0450         if (rc == 0)
0451             break;
0452         /* if the CCA way also failed, let's try via EP11 */
0453         rc = pkey_clr2ep11key(ckey.clrkey, t->len,
0454                       tmpbuf, &tmpbuflen);
0455         if (rc == 0)
0456             rc = pkey_ep11key2pkey(tmpbuf, protkey);
0457         /* now we should really have an protected key */
0458         DEBUG_ERR("%s unable to build protected key from clear",
0459               __func__);
0460         break;
0461     }
0462     case TOKVER_EP11_AES: {
0463         /* check ep11 key for exportable as protected key */
0464         rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
0465         if (rc)
0466             goto out;
0467         rc = pkey_ep11key2pkey(key, protkey);
0468         break;
0469     }
0470     case TOKVER_EP11_AES_WITH_HEADER:
0471         /* check ep11 key with header for exportable as protected key */
0472         rc = ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1);
0473         if (rc)
0474             goto out;
0475         rc = pkey_ep11key2pkey(key + sizeof(struct ep11kblob_header),
0476                        protkey);
0477         break;
0478     default:
0479         DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
0480               __func__, hdr->version);
0481         rc = -EINVAL;
0482     }
0483 
0484 out:
0485     kfree(tmpbuf);
0486     return rc;
0487 }
0488 
0489 /*
0490  * Transform a CCA internal key token into a protected key
0491  */
0492 static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
0493                    struct pkey_protkey *protkey)
0494 {
0495     struct keytoken_header *hdr = (struct keytoken_header *)key;
0496 
0497     switch (hdr->version) {
0498     case TOKVER_CCA_AES:
0499         if (keylen != sizeof(struct secaeskeytoken))
0500             return -EINVAL;
0501         break;
0502     case TOKVER_CCA_VLSC:
0503         if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
0504             return -EINVAL;
0505         break;
0506     default:
0507         DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
0508               __func__, hdr->version);
0509         return -EINVAL;
0510     }
0511 
0512     return pkey_skey2pkey(key, protkey);
0513 }
0514 
0515 /*
0516  * Transform a key blob (of any type) into a protected key
0517  */
0518 int pkey_keyblob2pkey(const u8 *key, u32 keylen,
0519               struct pkey_protkey *protkey)
0520 {
0521     int rc;
0522     struct keytoken_header *hdr = (struct keytoken_header *)key;
0523 
0524     if (keylen < sizeof(struct keytoken_header)) {
0525         DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
0526         return -EINVAL;
0527     }
0528 
0529     switch (hdr->type) {
0530     case TOKTYPE_NON_CCA:
0531         rc = pkey_nonccatok2pkey(key, keylen, protkey);
0532         break;
0533     case TOKTYPE_CCA_INTERNAL:
0534         rc = pkey_ccainttok2pkey(key, keylen, protkey);
0535         break;
0536     default:
0537         DEBUG_ERR("%s unknown/unsupported blob type %d\n",
0538               __func__, hdr->type);
0539         return -EINVAL;
0540     }
0541 
0542     DEBUG_DBG("%s rc=%d\n", __func__, rc);
0543     return rc;
0544 }
0545 EXPORT_SYMBOL(pkey_keyblob2pkey);
0546 
0547 static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
0548                enum pkey_key_type ktype, enum pkey_key_size ksize,
0549                u32 kflags, u8 *keybuf, size_t *keybufsize)
0550 {
0551     int i, card, dom, rc;
0552 
0553     /* check for at least one apqn given */
0554     if (!apqns || !nr_apqns)
0555         return -EINVAL;
0556 
0557     /* check key type and size */
0558     switch (ktype) {
0559     case PKEY_TYPE_CCA_DATA:
0560     case PKEY_TYPE_CCA_CIPHER:
0561         if (*keybufsize < SECKEYBLOBSIZE)
0562             return -EINVAL;
0563         break;
0564     case PKEY_TYPE_EP11:
0565         if (*keybufsize < MINEP11AESKEYBLOBSIZE)
0566             return -EINVAL;
0567         break;
0568     default:
0569         return -EINVAL;
0570     }
0571     switch (ksize) {
0572     case PKEY_SIZE_AES_128:
0573     case PKEY_SIZE_AES_192:
0574     case PKEY_SIZE_AES_256:
0575         break;
0576     default:
0577         return -EINVAL;
0578     }
0579 
0580     /* simple try all apqns from the list */
0581     for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
0582         card = apqns[i].card;
0583         dom = apqns[i].domain;
0584         if (ktype == PKEY_TYPE_EP11) {
0585             rc = ep11_genaeskey(card, dom, ksize, kflags,
0586                         keybuf, keybufsize);
0587         } else if (ktype == PKEY_TYPE_CCA_DATA) {
0588             rc = cca_genseckey(card, dom, ksize, keybuf);
0589             *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
0590         } else {
0591             /* TOKVER_CCA_VLSC */
0592             rc = cca_gencipherkey(card, dom, ksize, kflags,
0593                           keybuf, keybufsize);
0594         }
0595         if (rc == 0)
0596             break;
0597     }
0598 
0599     return rc;
0600 }
0601 
0602 static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
0603                 enum pkey_key_type ktype, enum pkey_key_size ksize,
0604                 u32 kflags, const u8 *clrkey,
0605                 u8 *keybuf, size_t *keybufsize)
0606 {
0607     int i, card, dom, rc;
0608 
0609     /* check for at least one apqn given */
0610     if (!apqns || !nr_apqns)
0611         return -EINVAL;
0612 
0613     /* check key type and size */
0614     switch (ktype) {
0615     case PKEY_TYPE_CCA_DATA:
0616     case PKEY_TYPE_CCA_CIPHER:
0617         if (*keybufsize < SECKEYBLOBSIZE)
0618             return -EINVAL;
0619         break;
0620     case PKEY_TYPE_EP11:
0621         if (*keybufsize < MINEP11AESKEYBLOBSIZE)
0622             return -EINVAL;
0623         break;
0624     default:
0625         return -EINVAL;
0626     }
0627     switch (ksize) {
0628     case PKEY_SIZE_AES_128:
0629     case PKEY_SIZE_AES_192:
0630     case PKEY_SIZE_AES_256:
0631         break;
0632     default:
0633         return -EINVAL;
0634     }
0635 
0636     zcrypt_wait_api_operational();
0637 
0638     /* simple try all apqns from the list */
0639     for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
0640         card = apqns[i].card;
0641         dom = apqns[i].domain;
0642         if (ktype == PKEY_TYPE_EP11) {
0643             rc = ep11_clr2keyblob(card, dom, ksize, kflags,
0644                           clrkey, keybuf, keybufsize);
0645         } else if (ktype == PKEY_TYPE_CCA_DATA) {
0646             rc = cca_clr2seckey(card, dom, ksize,
0647                         clrkey, keybuf);
0648             *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
0649         } else {
0650             /* TOKVER_CCA_VLSC */
0651             rc = cca_clr2cipherkey(card, dom, ksize, kflags,
0652                            clrkey, keybuf, keybufsize);
0653         }
0654         if (rc == 0)
0655             break;
0656     }
0657 
0658     return rc;
0659 }
0660 
0661 static int pkey_verifykey2(const u8 *key, size_t keylen,
0662                u16 *cardnr, u16 *domain,
0663                enum pkey_key_type *ktype,
0664                enum pkey_key_size *ksize, u32 *flags)
0665 {
0666     int rc;
0667     u32 _nr_apqns, *_apqns = NULL;
0668     struct keytoken_header *hdr = (struct keytoken_header *)key;
0669 
0670     if (keylen < sizeof(struct keytoken_header))
0671         return -EINVAL;
0672 
0673     if (hdr->type == TOKTYPE_CCA_INTERNAL &&
0674         hdr->version == TOKVER_CCA_AES) {
0675         struct secaeskeytoken *t = (struct secaeskeytoken *)key;
0676 
0677         rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
0678         if (rc)
0679             goto out;
0680         if (ktype)
0681             *ktype = PKEY_TYPE_CCA_DATA;
0682         if (ksize)
0683             *ksize = (enum pkey_key_size)t->bitsize;
0684 
0685         rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
0686                    ZCRYPT_CEX3C, AES_MK_SET, t->mkvp, 0, 1);
0687         if (rc == 0 && flags)
0688             *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
0689         if (rc == -ENODEV) {
0690             rc = cca_findcard2(&_apqns, &_nr_apqns,
0691                        *cardnr, *domain,
0692                        ZCRYPT_CEX3C, AES_MK_SET,
0693                        0, t->mkvp, 1);
0694             if (rc == 0 && flags)
0695                 *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
0696         }
0697         if (rc)
0698             goto out;
0699 
0700         *cardnr = ((struct pkey_apqn *)_apqns)->card;
0701         *domain = ((struct pkey_apqn *)_apqns)->domain;
0702 
0703     } else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
0704            hdr->version == TOKVER_CCA_VLSC) {
0705         struct cipherkeytoken *t = (struct cipherkeytoken *)key;
0706 
0707         rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
0708         if (rc)
0709             goto out;
0710         if (ktype)
0711             *ktype = PKEY_TYPE_CCA_CIPHER;
0712         if (ksize) {
0713             *ksize = PKEY_SIZE_UNKNOWN;
0714             if (!t->plfver && t->wpllen == 512)
0715                 *ksize = PKEY_SIZE_AES_128;
0716             else if (!t->plfver && t->wpllen == 576)
0717                 *ksize = PKEY_SIZE_AES_192;
0718             else if (!t->plfver && t->wpllen == 640)
0719                 *ksize = PKEY_SIZE_AES_256;
0720         }
0721 
0722         rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
0723                    ZCRYPT_CEX6, AES_MK_SET, t->mkvp0, 0, 1);
0724         if (rc == 0 && flags)
0725             *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
0726         if (rc == -ENODEV) {
0727             rc = cca_findcard2(&_apqns, &_nr_apqns,
0728                        *cardnr, *domain,
0729                        ZCRYPT_CEX6, AES_MK_SET,
0730                        0, t->mkvp0, 1);
0731             if (rc == 0 && flags)
0732                 *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
0733         }
0734         if (rc)
0735             goto out;
0736 
0737         *cardnr = ((struct pkey_apqn *)_apqns)->card;
0738         *domain = ((struct pkey_apqn *)_apqns)->domain;
0739 
0740     } else if (hdr->type == TOKTYPE_NON_CCA &&
0741            hdr->version == TOKVER_EP11_AES) {
0742         struct ep11keyblob *kb = (struct ep11keyblob *)key;
0743 
0744         rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
0745         if (rc)
0746             goto out;
0747         if (ktype)
0748             *ktype = PKEY_TYPE_EP11;
0749         if (ksize)
0750             *ksize = kb->head.keybitlen;
0751 
0752         rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
0753                     ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
0754         if (rc)
0755             goto out;
0756 
0757         if (flags)
0758             *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
0759 
0760         *cardnr = ((struct pkey_apqn *)_apqns)->card;
0761         *domain = ((struct pkey_apqn *)_apqns)->domain;
0762 
0763     } else {
0764         rc = -EINVAL;
0765     }
0766 
0767 out:
0768     kfree(_apqns);
0769     return rc;
0770 }
0771 
0772 static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
0773                   const u8 *key, size_t keylen,
0774                   struct pkey_protkey *pkey)
0775 {
0776     int i, card, dom, rc;
0777     struct keytoken_header *hdr = (struct keytoken_header *)key;
0778 
0779     /* check for at least one apqn given */
0780     if (!apqns || !nr_apqns)
0781         return -EINVAL;
0782 
0783     if (keylen < sizeof(struct keytoken_header))
0784         return -EINVAL;
0785 
0786     if (hdr->type == TOKTYPE_CCA_INTERNAL) {
0787         if (hdr->version == TOKVER_CCA_AES) {
0788             if (keylen != sizeof(struct secaeskeytoken))
0789                 return -EINVAL;
0790             if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
0791                 return -EINVAL;
0792         } else if (hdr->version == TOKVER_CCA_VLSC) {
0793             if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
0794                 return -EINVAL;
0795             if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
0796                 return -EINVAL;
0797         } else {
0798             DEBUG_ERR("%s unknown CCA internal token version %d\n",
0799                   __func__, hdr->version);
0800             return -EINVAL;
0801         }
0802     } else if (hdr->type == TOKTYPE_NON_CCA) {
0803         if (hdr->version == TOKVER_EP11_AES) {
0804             if (keylen < sizeof(struct ep11keyblob))
0805                 return -EINVAL;
0806             if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
0807                 return -EINVAL;
0808         } else {
0809             return pkey_nonccatok2pkey(key, keylen, pkey);
0810         }
0811     } else {
0812         DEBUG_ERR("%s unknown/unsupported blob type %d\n",
0813               __func__, hdr->type);
0814         return -EINVAL;
0815     }
0816 
0817     zcrypt_wait_api_operational();
0818 
0819     /* simple try all apqns from the list */
0820     for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
0821         card = apqns[i].card;
0822         dom = apqns[i].domain;
0823         if (hdr->type == TOKTYPE_CCA_INTERNAL &&
0824             hdr->version == TOKVER_CCA_AES) {
0825             rc = cca_sec2protkey(card, dom, key, pkey->protkey,
0826                          &pkey->len, &pkey->type);
0827         } else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
0828                hdr->version == TOKVER_CCA_VLSC) {
0829             rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
0830                         &pkey->len, &pkey->type);
0831         } else {
0832             /* EP11 AES secure key blob */
0833             struct ep11keyblob *kb = (struct ep11keyblob *)key;
0834 
0835             pkey->len = sizeof(pkey->protkey);
0836             rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
0837                         pkey->protkey, &pkey->len,
0838                         &pkey->type);
0839         }
0840         if (rc == 0)
0841             break;
0842     }
0843 
0844     return rc;
0845 }
0846 
0847 static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
0848               struct pkey_apqn *apqns, size_t *nr_apqns)
0849 {
0850     int rc;
0851     u32 _nr_apqns, *_apqns = NULL;
0852     struct keytoken_header *hdr = (struct keytoken_header *)key;
0853 
0854     if (keylen < sizeof(struct keytoken_header) || flags == 0)
0855         return -EINVAL;
0856 
0857     zcrypt_wait_api_operational();
0858 
0859     if (hdr->type == TOKTYPE_NON_CCA &&
0860         (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
0861          hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
0862         is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
0863         int minhwtype = 0, api = 0;
0864         struct ep11keyblob *kb = (struct ep11keyblob *)
0865             (key + sizeof(struct ep11kblob_header));
0866 
0867         if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
0868             return -EINVAL;
0869         if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
0870             minhwtype = ZCRYPT_CEX7;
0871             api = EP11_API_V;
0872         }
0873         rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
0874                     minhwtype, api, kb->wkvp);
0875         if (rc)
0876             goto out;
0877     } else if (hdr->type == TOKTYPE_NON_CCA &&
0878            hdr->version == TOKVER_EP11_AES &&
0879            is_ep11_keyblob(key)) {
0880         int minhwtype = 0, api = 0;
0881         struct ep11keyblob *kb = (struct ep11keyblob *)key;
0882 
0883         if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
0884             return -EINVAL;
0885         if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
0886             minhwtype = ZCRYPT_CEX7;
0887             api = EP11_API_V;
0888         }
0889         rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
0890                     minhwtype, api, kb->wkvp);
0891         if (rc)
0892             goto out;
0893     } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
0894         int minhwtype = ZCRYPT_CEX3C;
0895         u64 cur_mkvp = 0, old_mkvp = 0;
0896 
0897         if (hdr->version == TOKVER_CCA_AES) {
0898             struct secaeskeytoken *t = (struct secaeskeytoken *)key;
0899 
0900             if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
0901                 cur_mkvp = t->mkvp;
0902             if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
0903                 old_mkvp = t->mkvp;
0904         } else if (hdr->version == TOKVER_CCA_VLSC) {
0905             struct cipherkeytoken *t = (struct cipherkeytoken *)key;
0906 
0907             minhwtype = ZCRYPT_CEX6;
0908             if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
0909                 cur_mkvp = t->mkvp0;
0910             if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
0911                 old_mkvp = t->mkvp0;
0912         } else {
0913             /* unknown cca internal token type */
0914             return -EINVAL;
0915         }
0916         rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
0917                    minhwtype, AES_MK_SET,
0918                    cur_mkvp, old_mkvp, 1);
0919         if (rc)
0920             goto out;
0921     } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
0922         u64 cur_mkvp = 0, old_mkvp = 0;
0923         struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
0924 
0925         if (t->secid == 0x20) {
0926             if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
0927                 cur_mkvp = t->mkvp;
0928             if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
0929                 old_mkvp = t->mkvp;
0930         } else {
0931             /* unknown cca internal 2 token type */
0932             return -EINVAL;
0933         }
0934         rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
0935                    ZCRYPT_CEX7, APKA_MK_SET,
0936                    cur_mkvp, old_mkvp, 1);
0937         if (rc)
0938             goto out;
0939     } else {
0940         return -EINVAL;
0941     }
0942 
0943     if (apqns) {
0944         if (*nr_apqns < _nr_apqns)
0945             rc = -ENOSPC;
0946         else
0947             memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
0948     }
0949     *nr_apqns = _nr_apqns;
0950 
0951 out:
0952     kfree(_apqns);
0953     return rc;
0954 }
0955 
0956 static int pkey_apqns4keytype(enum pkey_key_type ktype,
0957                   u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
0958                   struct pkey_apqn *apqns, size_t *nr_apqns)
0959 {
0960     int rc;
0961     u32 _nr_apqns, *_apqns = NULL;
0962 
0963     zcrypt_wait_api_operational();
0964 
0965     if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
0966         u64 cur_mkvp = 0, old_mkvp = 0;
0967         int minhwtype = ZCRYPT_CEX3C;
0968 
0969         if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
0970             cur_mkvp = *((u64 *)cur_mkvp);
0971         if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
0972             old_mkvp = *((u64 *)alt_mkvp);
0973         if (ktype == PKEY_TYPE_CCA_CIPHER)
0974             minhwtype = ZCRYPT_CEX6;
0975         rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
0976                    minhwtype, AES_MK_SET,
0977                    cur_mkvp, old_mkvp, 1);
0978         if (rc)
0979             goto out;
0980     } else if (ktype == PKEY_TYPE_CCA_ECC) {
0981         u64 cur_mkvp = 0, old_mkvp = 0;
0982 
0983         if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
0984             cur_mkvp = *((u64 *)cur_mkvp);
0985         if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
0986             old_mkvp = *((u64 *)alt_mkvp);
0987         rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
0988                    ZCRYPT_CEX7, APKA_MK_SET,
0989                    cur_mkvp, old_mkvp, 1);
0990         if (rc)
0991             goto out;
0992 
0993     } else if (ktype == PKEY_TYPE_EP11 ||
0994            ktype == PKEY_TYPE_EP11_AES ||
0995            ktype == PKEY_TYPE_EP11_ECC) {
0996         u8 *wkvp = NULL;
0997 
0998         if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
0999             wkvp = cur_mkvp;
1000         rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
1001                     ZCRYPT_CEX7, EP11_API_V, wkvp);
1002         if (rc)
1003             goto out;
1004 
1005     } else {
1006         return -EINVAL;
1007     }
1008 
1009     if (apqns) {
1010         if (*nr_apqns < _nr_apqns)
1011             rc = -ENOSPC;
1012         else
1013             memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
1014     }
1015     *nr_apqns = _nr_apqns;
1016 
1017 out:
1018     kfree(_apqns);
1019     return rc;
1020 }
1021 
1022 static int pkey_keyblob2pkey3(const struct pkey_apqn *apqns, size_t nr_apqns,
1023                   const u8 *key, size_t keylen, u32 *protkeytype,
1024                   u8 *protkey, u32 *protkeylen)
1025 {
1026     int i, card, dom, rc;
1027     struct keytoken_header *hdr = (struct keytoken_header *)key;
1028 
1029     /* check for at least one apqn given */
1030     if (!apqns || !nr_apqns)
1031         return -EINVAL;
1032 
1033     if (keylen < sizeof(struct keytoken_header))
1034         return -EINVAL;
1035 
1036     if (hdr->type == TOKTYPE_NON_CCA &&
1037         hdr->version == TOKVER_EP11_AES_WITH_HEADER &&
1038         is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1039         /* EP11 AES key blob with header */
1040         if (ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1))
1041             return -EINVAL;
1042     } else if (hdr->type == TOKTYPE_NON_CCA &&
1043            hdr->version == TOKVER_EP11_ECC_WITH_HEADER &&
1044            is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
1045         /* EP11 ECC key blob with header */
1046         if (ep11_check_ecc_key_with_hdr(debug_info, 3, key, keylen, 1))
1047             return -EINVAL;
1048     } else if (hdr->type == TOKTYPE_NON_CCA &&
1049            hdr->version == TOKVER_EP11_AES &&
1050            is_ep11_keyblob(key)) {
1051         /* EP11 AES key blob with header in session field */
1052         if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
1053             return -EINVAL;
1054     } else  if (hdr->type == TOKTYPE_CCA_INTERNAL) {
1055         if (hdr->version == TOKVER_CCA_AES) {
1056             /* CCA AES data key */
1057             if (keylen != sizeof(struct secaeskeytoken))
1058                 return -EINVAL;
1059             if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
1060                 return -EINVAL;
1061         } else if (hdr->version == TOKVER_CCA_VLSC) {
1062             /* CCA AES cipher key */
1063             if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
1064                 return -EINVAL;
1065             if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
1066                 return -EINVAL;
1067         } else {
1068             DEBUG_ERR("%s unknown CCA internal token version %d\n",
1069                   __func__, hdr->version);
1070             return -EINVAL;
1071         }
1072     } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
1073         /* CCA ECC (private) key */
1074         if (keylen < sizeof(struct eccprivkeytoken))
1075             return -EINVAL;
1076         if (cca_check_sececckeytoken(debug_info, 3, key, keylen, 1))
1077             return -EINVAL;
1078     } else if (hdr->type == TOKTYPE_NON_CCA) {
1079         struct pkey_protkey pkey;
1080 
1081         rc = pkey_nonccatok2pkey(key, keylen, &pkey);
1082         if (rc)
1083             return rc;
1084         memcpy(protkey, pkey.protkey, pkey.len);
1085         *protkeylen = pkey.len;
1086         *protkeytype = pkey.type;
1087         return 0;
1088     } else {
1089         DEBUG_ERR("%s unknown/unsupported blob type %d\n",
1090               __func__, hdr->type);
1091         return -EINVAL;
1092     }
1093 
1094     /* simple try all apqns from the list */
1095     for (rc = -ENODEV, i = 0; rc && i < nr_apqns; i++) {
1096         card = apqns[i].card;
1097         dom = apqns[i].domain;
1098         if (hdr->type == TOKTYPE_NON_CCA &&
1099             (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
1100              hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
1101             is_ep11_keyblob(key + sizeof(struct ep11kblob_header)))
1102             rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1103                         protkey, protkeylen, protkeytype);
1104         else if (hdr->type == TOKTYPE_NON_CCA &&
1105              hdr->version == TOKVER_EP11_AES &&
1106              is_ep11_keyblob(key))
1107             rc = ep11_kblob2protkey(card, dom, key, hdr->len,
1108                         protkey, protkeylen, protkeytype);
1109         else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1110              hdr->version == TOKVER_CCA_AES)
1111             rc = cca_sec2protkey(card, dom, key, protkey,
1112                          protkeylen, protkeytype);
1113         else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
1114              hdr->version == TOKVER_CCA_VLSC)
1115             rc = cca_cipher2protkey(card, dom, key, protkey,
1116                         protkeylen, protkeytype);
1117         else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA)
1118             rc = cca_ecc2protkey(card, dom, key, protkey,
1119                          protkeylen, protkeytype);
1120         else
1121             return -EINVAL;
1122     }
1123 
1124     return rc;
1125 }
1126 
1127 /*
1128  * File io functions
1129  */
1130 
1131 static void *_copy_key_from_user(void __user *ukey, size_t keylen)
1132 {
1133     if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
1134         return ERR_PTR(-EINVAL);
1135 
1136     return memdup_user(ukey, keylen);
1137 }
1138 
1139 static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
1140 {
1141     if (!uapqns || nr_apqns == 0)
1142         return NULL;
1143 
1144     return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
1145 }
1146 
1147 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
1148                 unsigned long arg)
1149 {
1150     int rc;
1151 
1152     switch (cmd) {
1153     case PKEY_GENSECK: {
1154         struct pkey_genseck __user *ugs = (void __user *)arg;
1155         struct pkey_genseck kgs;
1156 
1157         if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1158             return -EFAULT;
1159         rc = cca_genseckey(kgs.cardnr, kgs.domain,
1160                    kgs.keytype, kgs.seckey.seckey);
1161         DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
1162         if (rc)
1163             break;
1164         if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1165             return -EFAULT;
1166         break;
1167     }
1168     case PKEY_CLR2SECK: {
1169         struct pkey_clr2seck __user *ucs = (void __user *)arg;
1170         struct pkey_clr2seck kcs;
1171 
1172         if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1173             return -EFAULT;
1174         rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
1175                     kcs.clrkey.clrkey, kcs.seckey.seckey);
1176         DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
1177         if (rc)
1178             break;
1179         if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1180             return -EFAULT;
1181         memzero_explicit(&kcs, sizeof(kcs));
1182         break;
1183     }
1184     case PKEY_SEC2PROTK: {
1185         struct pkey_sec2protk __user *usp = (void __user *)arg;
1186         struct pkey_sec2protk ksp;
1187 
1188         if (copy_from_user(&ksp, usp, sizeof(ksp)))
1189             return -EFAULT;
1190         rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
1191                      ksp.seckey.seckey, ksp.protkey.protkey,
1192                      &ksp.protkey.len, &ksp.protkey.type);
1193         DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
1194         if (rc)
1195             break;
1196         if (copy_to_user(usp, &ksp, sizeof(ksp)))
1197             return -EFAULT;
1198         break;
1199     }
1200     case PKEY_CLR2PROTK: {
1201         struct pkey_clr2protk __user *ucp = (void __user *)arg;
1202         struct pkey_clr2protk kcp;
1203 
1204         if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1205             return -EFAULT;
1206         rc = pkey_clr2protkey(kcp.keytype,
1207                       &kcp.clrkey, &kcp.protkey);
1208         DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
1209         if (rc)
1210             break;
1211         if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1212             return -EFAULT;
1213         memzero_explicit(&kcp, sizeof(kcp));
1214         break;
1215     }
1216     case PKEY_FINDCARD: {
1217         struct pkey_findcard __user *ufc = (void __user *)arg;
1218         struct pkey_findcard kfc;
1219 
1220         if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1221             return -EFAULT;
1222         rc = cca_findcard(kfc.seckey.seckey,
1223                   &kfc.cardnr, &kfc.domain, 1);
1224         DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
1225         if (rc < 0)
1226             break;
1227         if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1228             return -EFAULT;
1229         break;
1230     }
1231     case PKEY_SKEY2PKEY: {
1232         struct pkey_skey2pkey __user *usp = (void __user *)arg;
1233         struct pkey_skey2pkey ksp;
1234 
1235         if (copy_from_user(&ksp, usp, sizeof(ksp)))
1236             return -EFAULT;
1237         rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
1238         DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
1239         if (rc)
1240             break;
1241         if (copy_to_user(usp, &ksp, sizeof(ksp)))
1242             return -EFAULT;
1243         break;
1244     }
1245     case PKEY_VERIFYKEY: {
1246         struct pkey_verifykey __user *uvk = (void __user *)arg;
1247         struct pkey_verifykey kvk;
1248 
1249         if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1250             return -EFAULT;
1251         rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1252                     &kvk.keysize, &kvk.attributes);
1253         DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
1254         if (rc)
1255             break;
1256         if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1257             return -EFAULT;
1258         break;
1259     }
1260     case PKEY_GENPROTK: {
1261         struct pkey_genprotk __user *ugp = (void __user *)arg;
1262         struct pkey_genprotk kgp;
1263 
1264         if (copy_from_user(&kgp, ugp, sizeof(kgp)))
1265             return -EFAULT;
1266         rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
1267         DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
1268         if (rc)
1269             break;
1270         if (copy_to_user(ugp, &kgp, sizeof(kgp)))
1271             return -EFAULT;
1272         break;
1273     }
1274     case PKEY_VERIFYPROTK: {
1275         struct pkey_verifyprotk __user *uvp = (void __user *)arg;
1276         struct pkey_verifyprotk kvp;
1277 
1278         if (copy_from_user(&kvp, uvp, sizeof(kvp)))
1279             return -EFAULT;
1280         rc = pkey_verifyprotkey(&kvp.protkey);
1281         DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
1282         break;
1283     }
1284     case PKEY_KBLOB2PROTK: {
1285         struct pkey_kblob2pkey __user *utp = (void __user *)arg;
1286         struct pkey_kblob2pkey ktp;
1287         u8 *kkey;
1288 
1289         if (copy_from_user(&ktp, utp, sizeof(ktp)))
1290             return -EFAULT;
1291         kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1292         if (IS_ERR(kkey))
1293             return PTR_ERR(kkey);
1294         rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
1295         DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
1296         kfree(kkey);
1297         if (rc)
1298             break;
1299         if (copy_to_user(utp, &ktp, sizeof(ktp)))
1300             return -EFAULT;
1301         break;
1302     }
1303     case PKEY_GENSECK2: {
1304         struct pkey_genseck2 __user *ugs = (void __user *)arg;
1305         struct pkey_genseck2 kgs;
1306         struct pkey_apqn *apqns;
1307         size_t klen = KEYBLOBBUFSIZE;
1308         u8 *kkey;
1309 
1310         if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1311             return -EFAULT;
1312         apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
1313         if (IS_ERR(apqns))
1314             return PTR_ERR(apqns);
1315         kkey = kmalloc(klen, GFP_KERNEL);
1316         if (!kkey) {
1317             kfree(apqns);
1318             return -ENOMEM;
1319         }
1320         rc = pkey_genseckey2(apqns, kgs.apqn_entries,
1321                      kgs.type, kgs.size, kgs.keygenflags,
1322                      kkey, &klen);
1323         DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
1324         kfree(apqns);
1325         if (rc) {
1326             kfree(kkey);
1327             break;
1328         }
1329         if (kgs.key) {
1330             if (kgs.keylen < klen) {
1331                 kfree(kkey);
1332                 return -EINVAL;
1333             }
1334             if (copy_to_user(kgs.key, kkey, klen)) {
1335                 kfree(kkey);
1336                 return -EFAULT;
1337             }
1338         }
1339         kgs.keylen = klen;
1340         if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1341             rc = -EFAULT;
1342         kfree(kkey);
1343         break;
1344     }
1345     case PKEY_CLR2SECK2: {
1346         struct pkey_clr2seck2 __user *ucs = (void __user *)arg;
1347         struct pkey_clr2seck2 kcs;
1348         struct pkey_apqn *apqns;
1349         size_t klen = KEYBLOBBUFSIZE;
1350         u8 *kkey;
1351 
1352         if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1353             return -EFAULT;
1354         apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
1355         if (IS_ERR(apqns))
1356             return PTR_ERR(apqns);
1357         kkey = kmalloc(klen, GFP_KERNEL);
1358         if (!kkey) {
1359             kfree(apqns);
1360             return -ENOMEM;
1361         }
1362         rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
1363                       kcs.type, kcs.size, kcs.keygenflags,
1364                       kcs.clrkey.clrkey, kkey, &klen);
1365         DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
1366         kfree(apqns);
1367         if (rc) {
1368             kfree(kkey);
1369             break;
1370         }
1371         if (kcs.key) {
1372             if (kcs.keylen < klen) {
1373                 kfree(kkey);
1374                 return -EINVAL;
1375             }
1376             if (copy_to_user(kcs.key, kkey, klen)) {
1377                 kfree(kkey);
1378                 return -EFAULT;
1379             }
1380         }
1381         kcs.keylen = klen;
1382         if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1383             rc = -EFAULT;
1384         memzero_explicit(&kcs, sizeof(kcs));
1385         kfree(kkey);
1386         break;
1387     }
1388     case PKEY_VERIFYKEY2: {
1389         struct pkey_verifykey2 __user *uvk = (void __user *)arg;
1390         struct pkey_verifykey2 kvk;
1391         u8 *kkey;
1392 
1393         if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1394             return -EFAULT;
1395         kkey = _copy_key_from_user(kvk.key, kvk.keylen);
1396         if (IS_ERR(kkey))
1397             return PTR_ERR(kkey);
1398         rc = pkey_verifykey2(kkey, kvk.keylen,
1399                      &kvk.cardnr, &kvk.domain,
1400                      &kvk.type, &kvk.size, &kvk.flags);
1401         DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
1402         kfree(kkey);
1403         if (rc)
1404             break;
1405         if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1406             return -EFAULT;
1407         break;
1408     }
1409     case PKEY_KBLOB2PROTK2: {
1410         struct pkey_kblob2pkey2 __user *utp = (void __user *)arg;
1411         struct pkey_kblob2pkey2 ktp;
1412         struct pkey_apqn *apqns = NULL;
1413         u8 *kkey;
1414 
1415         if (copy_from_user(&ktp, utp, sizeof(ktp)))
1416             return -EFAULT;
1417         apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1418         if (IS_ERR(apqns))
1419             return PTR_ERR(apqns);
1420         kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1421         if (IS_ERR(kkey)) {
1422             kfree(apqns);
1423             return PTR_ERR(kkey);
1424         }
1425         rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1426                     kkey, ktp.keylen, &ktp.protkey);
1427         DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1428         kfree(apqns);
1429         kfree(kkey);
1430         if (rc)
1431             break;
1432         if (copy_to_user(utp, &ktp, sizeof(ktp)))
1433             return -EFAULT;
1434         break;
1435     }
1436     case PKEY_APQNS4K: {
1437         struct pkey_apqns4key __user *uak = (void __user *)arg;
1438         struct pkey_apqns4key kak;
1439         struct pkey_apqn *apqns = NULL;
1440         size_t nr_apqns, len;
1441         u8 *kkey;
1442 
1443         if (copy_from_user(&kak, uak, sizeof(kak)))
1444             return -EFAULT;
1445         nr_apqns = kak.apqn_entries;
1446         if (nr_apqns) {
1447             apqns = kmalloc_array(nr_apqns,
1448                           sizeof(struct pkey_apqn),
1449                           GFP_KERNEL);
1450             if (!apqns)
1451                 return -ENOMEM;
1452         }
1453         kkey = _copy_key_from_user(kak.key, kak.keylen);
1454         if (IS_ERR(kkey)) {
1455             kfree(apqns);
1456             return PTR_ERR(kkey);
1457         }
1458         rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1459                     apqns, &nr_apqns);
1460         DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1461         kfree(kkey);
1462         if (rc && rc != -ENOSPC) {
1463             kfree(apqns);
1464             break;
1465         }
1466         if (!rc && kak.apqns) {
1467             if (nr_apqns > kak.apqn_entries) {
1468                 kfree(apqns);
1469                 return -EINVAL;
1470             }
1471             len = nr_apqns * sizeof(struct pkey_apqn);
1472             if (len) {
1473                 if (copy_to_user(kak.apqns, apqns, len)) {
1474                     kfree(apqns);
1475                     return -EFAULT;
1476                 }
1477             }
1478         }
1479         kak.apqn_entries = nr_apqns;
1480         if (copy_to_user(uak, &kak, sizeof(kak)))
1481             rc = -EFAULT;
1482         kfree(apqns);
1483         break;
1484     }
1485     case PKEY_APQNS4KT: {
1486         struct pkey_apqns4keytype __user *uat = (void __user *)arg;
1487         struct pkey_apqns4keytype kat;
1488         struct pkey_apqn *apqns = NULL;
1489         size_t nr_apqns, len;
1490 
1491         if (copy_from_user(&kat, uat, sizeof(kat)))
1492             return -EFAULT;
1493         nr_apqns = kat.apqn_entries;
1494         if (nr_apqns) {
1495             apqns = kmalloc_array(nr_apqns,
1496                           sizeof(struct pkey_apqn),
1497                           GFP_KERNEL);
1498             if (!apqns)
1499                 return -ENOMEM;
1500         }
1501         rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1502                     kat.flags, apqns, &nr_apqns);
1503         DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1504         if (rc && rc != -ENOSPC) {
1505             kfree(apqns);
1506             break;
1507         }
1508         if (!rc && kat.apqns) {
1509             if (nr_apqns > kat.apqn_entries) {
1510                 kfree(apqns);
1511                 return -EINVAL;
1512             }
1513             len = nr_apqns * sizeof(struct pkey_apqn);
1514             if (len) {
1515                 if (copy_to_user(kat.apqns, apqns, len)) {
1516                     kfree(apqns);
1517                     return -EFAULT;
1518                 }
1519             }
1520         }
1521         kat.apqn_entries = nr_apqns;
1522         if (copy_to_user(uat, &kat, sizeof(kat)))
1523             rc = -EFAULT;
1524         kfree(apqns);
1525         break;
1526     }
1527     case PKEY_KBLOB2PROTK3: {
1528         struct pkey_kblob2pkey3 __user *utp = (void __user *)arg;
1529         struct pkey_kblob2pkey3 ktp;
1530         struct pkey_apqn *apqns = NULL;
1531         u32 protkeylen = PROTKEYBLOBBUFSIZE;
1532         u8 *kkey, *protkey;
1533 
1534         if (copy_from_user(&ktp, utp, sizeof(ktp)))
1535             return -EFAULT;
1536         apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1537         if (IS_ERR(apqns))
1538             return PTR_ERR(apqns);
1539         kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1540         if (IS_ERR(kkey)) {
1541             kfree(apqns);
1542             return PTR_ERR(kkey);
1543         }
1544         protkey = kmalloc(protkeylen, GFP_KERNEL);
1545         if (!protkey) {
1546             kfree(apqns);
1547             kfree(kkey);
1548             return -ENOMEM;
1549         }
1550         rc = pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey,
1551                     ktp.keylen, &ktp.pkeytype,
1552                     protkey, &protkeylen);
1553         DEBUG_DBG("%s pkey_keyblob2pkey3()=%d\n", __func__, rc);
1554         kfree(apqns);
1555         kfree(kkey);
1556         if (rc) {
1557             kfree(protkey);
1558             break;
1559         }
1560         if (ktp.pkey && ktp.pkeylen) {
1561             if (protkeylen > ktp.pkeylen) {
1562                 kfree(protkey);
1563                 return -EINVAL;
1564             }
1565             if (copy_to_user(ktp.pkey, protkey, protkeylen)) {
1566                 kfree(protkey);
1567                 return -EFAULT;
1568             }
1569         }
1570         kfree(protkey);
1571         ktp.pkeylen = protkeylen;
1572         if (copy_to_user(utp, &ktp, sizeof(ktp)))
1573             return -EFAULT;
1574         break;
1575     }
1576     default:
1577         /* unknown/unsupported ioctl cmd */
1578         return -ENOTTY;
1579     }
1580 
1581     return rc;
1582 }
1583 
1584 /*
1585  * Sysfs and file io operations
1586  */
1587 
1588 /*
1589  * Sysfs attribute read function for all protected key binary attributes.
1590  * The implementation can not deal with partial reads, because a new random
1591  * protected key blob is generated with each read. In case of partial reads
1592  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1593  */
1594 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1595                       loff_t off, size_t count)
1596 {
1597     struct protaeskeytoken protkeytoken;
1598     struct pkey_protkey protkey;
1599     int rc;
1600 
1601     if (off != 0 || count < sizeof(protkeytoken))
1602         return -EINVAL;
1603     if (is_xts)
1604         if (count < 2 * sizeof(protkeytoken))
1605             return -EINVAL;
1606 
1607     memset(&protkeytoken, 0, sizeof(protkeytoken));
1608     protkeytoken.type = TOKTYPE_NON_CCA;
1609     protkeytoken.version = TOKVER_PROTECTED_KEY;
1610     protkeytoken.keytype = keytype;
1611 
1612     rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1613     if (rc)
1614         return rc;
1615 
1616     protkeytoken.len = protkey.len;
1617     memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1618 
1619     memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1620 
1621     if (is_xts) {
1622         rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1623         if (rc)
1624             return rc;
1625 
1626         protkeytoken.len = protkey.len;
1627         memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1628 
1629         memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1630                sizeof(protkeytoken));
1631 
1632         return 2 * sizeof(protkeytoken);
1633     }
1634 
1635     return sizeof(protkeytoken);
1636 }
1637 
1638 static ssize_t protkey_aes_128_read(struct file *filp,
1639                     struct kobject *kobj,
1640                     struct bin_attribute *attr,
1641                     char *buf, loff_t off,
1642                     size_t count)
1643 {
1644     return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1645                       off, count);
1646 }
1647 
1648 static ssize_t protkey_aes_192_read(struct file *filp,
1649                     struct kobject *kobj,
1650                     struct bin_attribute *attr,
1651                     char *buf, loff_t off,
1652                     size_t count)
1653 {
1654     return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1655                       off, count);
1656 }
1657 
1658 static ssize_t protkey_aes_256_read(struct file *filp,
1659                     struct kobject *kobj,
1660                     struct bin_attribute *attr,
1661                     char *buf, loff_t off,
1662                     size_t count)
1663 {
1664     return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1665                       off, count);
1666 }
1667 
1668 static ssize_t protkey_aes_128_xts_read(struct file *filp,
1669                     struct kobject *kobj,
1670                     struct bin_attribute *attr,
1671                     char *buf, loff_t off,
1672                     size_t count)
1673 {
1674     return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1675                       off, count);
1676 }
1677 
1678 static ssize_t protkey_aes_256_xts_read(struct file *filp,
1679                     struct kobject *kobj,
1680                     struct bin_attribute *attr,
1681                     char *buf, loff_t off,
1682                     size_t count)
1683 {
1684     return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1685                       off, count);
1686 }
1687 
1688 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1689 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1690 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1691 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1692 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1693 
1694 static struct bin_attribute *protkey_attrs[] = {
1695     &bin_attr_protkey_aes_128,
1696     &bin_attr_protkey_aes_192,
1697     &bin_attr_protkey_aes_256,
1698     &bin_attr_protkey_aes_128_xts,
1699     &bin_attr_protkey_aes_256_xts,
1700     NULL
1701 };
1702 
1703 static struct attribute_group protkey_attr_group = {
1704     .name      = "protkey",
1705     .bin_attrs = protkey_attrs,
1706 };
1707 
1708 /*
1709  * Sysfs attribute read function for all secure key ccadata binary attributes.
1710  * The implementation can not deal with partial reads, because a new random
1711  * protected key blob is generated with each read. In case of partial reads
1712  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1713  */
1714 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1715                       loff_t off, size_t count)
1716 {
1717     int rc;
1718     struct pkey_seckey *seckey = (struct pkey_seckey *)buf;
1719 
1720     if (off != 0 || count < sizeof(struct secaeskeytoken))
1721         return -EINVAL;
1722     if (is_xts)
1723         if (count < 2 * sizeof(struct secaeskeytoken))
1724             return -EINVAL;
1725 
1726     rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1727     if (rc)
1728         return rc;
1729 
1730     if (is_xts) {
1731         seckey++;
1732         rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1733         if (rc)
1734             return rc;
1735 
1736         return 2 * sizeof(struct secaeskeytoken);
1737     }
1738 
1739     return sizeof(struct secaeskeytoken);
1740 }
1741 
1742 static ssize_t ccadata_aes_128_read(struct file *filp,
1743                     struct kobject *kobj,
1744                     struct bin_attribute *attr,
1745                     char *buf, loff_t off,
1746                     size_t count)
1747 {
1748     return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1749                       off, count);
1750 }
1751 
1752 static ssize_t ccadata_aes_192_read(struct file *filp,
1753                     struct kobject *kobj,
1754                     struct bin_attribute *attr,
1755                     char *buf, loff_t off,
1756                     size_t count)
1757 {
1758     return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1759                       off, count);
1760 }
1761 
1762 static ssize_t ccadata_aes_256_read(struct file *filp,
1763                     struct kobject *kobj,
1764                     struct bin_attribute *attr,
1765                     char *buf, loff_t off,
1766                     size_t count)
1767 {
1768     return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1769                       off, count);
1770 }
1771 
1772 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1773                     struct kobject *kobj,
1774                     struct bin_attribute *attr,
1775                     char *buf, loff_t off,
1776                     size_t count)
1777 {
1778     return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1779                       off, count);
1780 }
1781 
1782 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1783                     struct kobject *kobj,
1784                     struct bin_attribute *attr,
1785                     char *buf, loff_t off,
1786                     size_t count)
1787 {
1788     return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1789                       off, count);
1790 }
1791 
1792 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1793 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1794 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1795 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1796 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1797 
1798 static struct bin_attribute *ccadata_attrs[] = {
1799     &bin_attr_ccadata_aes_128,
1800     &bin_attr_ccadata_aes_192,
1801     &bin_attr_ccadata_aes_256,
1802     &bin_attr_ccadata_aes_128_xts,
1803     &bin_attr_ccadata_aes_256_xts,
1804     NULL
1805 };
1806 
1807 static struct attribute_group ccadata_attr_group = {
1808     .name      = "ccadata",
1809     .bin_attrs = ccadata_attrs,
1810 };
1811 
1812 #define CCACIPHERTOKENSIZE  (sizeof(struct cipherkeytoken) + 80)
1813 
1814 /*
1815  * Sysfs attribute read function for all secure key ccacipher binary attributes.
1816  * The implementation can not deal with partial reads, because a new random
1817  * secure key blob is generated with each read. In case of partial reads
1818  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1819  */
1820 static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1821                         bool is_xts, char *buf, loff_t off,
1822                         size_t count)
1823 {
1824     int i, rc, card, dom;
1825     u32 nr_apqns, *apqns = NULL;
1826     size_t keysize = CCACIPHERTOKENSIZE;
1827 
1828     if (off != 0 || count < CCACIPHERTOKENSIZE)
1829         return -EINVAL;
1830     if (is_xts)
1831         if (count < 2 * CCACIPHERTOKENSIZE)
1832             return -EINVAL;
1833 
1834     /* build a list of apqns able to generate an cipher key */
1835     rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1836                ZCRYPT_CEX6, 0, 0, 0, 0);
1837     if (rc)
1838         return rc;
1839 
1840     memset(buf, 0, is_xts ? 2 * keysize : keysize);
1841 
1842     /* simple try all apqns from the list */
1843     for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1844         card = apqns[i] >> 16;
1845         dom = apqns[i] & 0xFFFF;
1846         rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1847         if (rc == 0)
1848             break;
1849     }
1850     if (rc)
1851         return rc;
1852 
1853     if (is_xts) {
1854         keysize = CCACIPHERTOKENSIZE;
1855         buf += CCACIPHERTOKENSIZE;
1856         rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1857         if (rc == 0)
1858             return 2 * CCACIPHERTOKENSIZE;
1859     }
1860 
1861     return CCACIPHERTOKENSIZE;
1862 }
1863 
1864 static ssize_t ccacipher_aes_128_read(struct file *filp,
1865                       struct kobject *kobj,
1866                       struct bin_attribute *attr,
1867                       char *buf, loff_t off,
1868                       size_t count)
1869 {
1870     return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1871                         off, count);
1872 }
1873 
1874 static ssize_t ccacipher_aes_192_read(struct file *filp,
1875                       struct kobject *kobj,
1876                       struct bin_attribute *attr,
1877                       char *buf, loff_t off,
1878                       size_t count)
1879 {
1880     return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1881                         off, count);
1882 }
1883 
1884 static ssize_t ccacipher_aes_256_read(struct file *filp,
1885                       struct kobject *kobj,
1886                       struct bin_attribute *attr,
1887                       char *buf, loff_t off,
1888                       size_t count)
1889 {
1890     return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1891                         off, count);
1892 }
1893 
1894 static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1895                       struct kobject *kobj,
1896                       struct bin_attribute *attr,
1897                       char *buf, loff_t off,
1898                       size_t count)
1899 {
1900     return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1901                         off, count);
1902 }
1903 
1904 static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1905                       struct kobject *kobj,
1906                       struct bin_attribute *attr,
1907                       char *buf, loff_t off,
1908                       size_t count)
1909 {
1910     return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1911                         off, count);
1912 }
1913 
1914 static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1915 static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1916 static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1917 static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1918 static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1919 
1920 static struct bin_attribute *ccacipher_attrs[] = {
1921     &bin_attr_ccacipher_aes_128,
1922     &bin_attr_ccacipher_aes_192,
1923     &bin_attr_ccacipher_aes_256,
1924     &bin_attr_ccacipher_aes_128_xts,
1925     &bin_attr_ccacipher_aes_256_xts,
1926     NULL
1927 };
1928 
1929 static struct attribute_group ccacipher_attr_group = {
1930     .name      = "ccacipher",
1931     .bin_attrs = ccacipher_attrs,
1932 };
1933 
1934 /*
1935  * Sysfs attribute read function for all ep11 aes key binary attributes.
1936  * The implementation can not deal with partial reads, because a new random
1937  * secure key blob is generated with each read. In case of partial reads
1938  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1939  * This function and the sysfs attributes using it provide EP11 key blobs
1940  * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
1941  * 320 bytes.
1942  */
1943 static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
1944                        bool is_xts, char *buf, loff_t off,
1945                        size_t count)
1946 {
1947     int i, rc, card, dom;
1948     u32 nr_apqns, *apqns = NULL;
1949     size_t keysize = MAXEP11AESKEYBLOBSIZE;
1950 
1951     if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
1952         return -EINVAL;
1953     if (is_xts)
1954         if (count < 2 * MAXEP11AESKEYBLOBSIZE)
1955             return -EINVAL;
1956 
1957     /* build a list of apqns able to generate an cipher key */
1958     rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1959                 ZCRYPT_CEX7, EP11_API_V, NULL);
1960     if (rc)
1961         return rc;
1962 
1963     memset(buf, 0, is_xts ? 2 * keysize : keysize);
1964 
1965     /* simple try all apqns from the list */
1966     for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1967         card = apqns[i] >> 16;
1968         dom = apqns[i] & 0xFFFF;
1969         rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1970         if (rc == 0)
1971             break;
1972     }
1973     if (rc)
1974         return rc;
1975 
1976     if (is_xts) {
1977         keysize = MAXEP11AESKEYBLOBSIZE;
1978         buf += MAXEP11AESKEYBLOBSIZE;
1979         rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1980         if (rc == 0)
1981             return 2 * MAXEP11AESKEYBLOBSIZE;
1982     }
1983 
1984     return MAXEP11AESKEYBLOBSIZE;
1985 }
1986 
1987 static ssize_t ep11_aes_128_read(struct file *filp,
1988                  struct kobject *kobj,
1989                  struct bin_attribute *attr,
1990                  char *buf, loff_t off,
1991                  size_t count)
1992 {
1993     return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1994                        off, count);
1995 }
1996 
1997 static ssize_t ep11_aes_192_read(struct file *filp,
1998                  struct kobject *kobj,
1999                  struct bin_attribute *attr,
2000                  char *buf, loff_t off,
2001                  size_t count)
2002 {
2003     return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
2004                        off, count);
2005 }
2006 
2007 static ssize_t ep11_aes_256_read(struct file *filp,
2008                  struct kobject *kobj,
2009                  struct bin_attribute *attr,
2010                  char *buf, loff_t off,
2011                  size_t count)
2012 {
2013     return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
2014                        off, count);
2015 }
2016 
2017 static ssize_t ep11_aes_128_xts_read(struct file *filp,
2018                      struct kobject *kobj,
2019                      struct bin_attribute *attr,
2020                      char *buf, loff_t off,
2021                      size_t count)
2022 {
2023     return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
2024                        off, count);
2025 }
2026 
2027 static ssize_t ep11_aes_256_xts_read(struct file *filp,
2028                      struct kobject *kobj,
2029                      struct bin_attribute *attr,
2030                      char *buf, loff_t off,
2031                      size_t count)
2032 {
2033     return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
2034                        off, count);
2035 }
2036 
2037 static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
2038 static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
2039 static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
2040 static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2041 static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2042 
2043 static struct bin_attribute *ep11_attrs[] = {
2044     &bin_attr_ep11_aes_128,
2045     &bin_attr_ep11_aes_192,
2046     &bin_attr_ep11_aes_256,
2047     &bin_attr_ep11_aes_128_xts,
2048     &bin_attr_ep11_aes_256_xts,
2049     NULL
2050 };
2051 
2052 static struct attribute_group ep11_attr_group = {
2053     .name      = "ep11",
2054     .bin_attrs = ep11_attrs,
2055 };
2056 
2057 static const struct attribute_group *pkey_attr_groups[] = {
2058     &protkey_attr_group,
2059     &ccadata_attr_group,
2060     &ccacipher_attr_group,
2061     &ep11_attr_group,
2062     NULL,
2063 };
2064 
2065 static const struct file_operations pkey_fops = {
2066     .owner      = THIS_MODULE,
2067     .open       = nonseekable_open,
2068     .llseek     = no_llseek,
2069     .unlocked_ioctl = pkey_unlocked_ioctl,
2070 };
2071 
2072 static struct miscdevice pkey_dev = {
2073     .name   = "pkey",
2074     .minor  = MISC_DYNAMIC_MINOR,
2075     .mode   = 0666,
2076     .fops   = &pkey_fops,
2077     .groups = pkey_attr_groups,
2078 };
2079 
2080 /*
2081  * Module init
2082  */
2083 static int __init pkey_init(void)
2084 {
2085     cpacf_mask_t func_mask;
2086 
2087     /*
2088      * The pckmo instruction should be available - even if we don't
2089      * actually invoke it. This instruction comes with MSA 3 which
2090      * is also the minimum level for the kmc instructions which
2091      * are able to work with protected keys.
2092      */
2093     if (!cpacf_query(CPACF_PCKMO, &func_mask))
2094         return -ENODEV;
2095 
2096     /* check for kmc instructions available */
2097     if (!cpacf_query(CPACF_KMC, &func_mask))
2098         return -ENODEV;
2099     if (!cpacf_test_func(&func_mask, CPACF_KMC_PAES_128) ||
2100         !cpacf_test_func(&func_mask, CPACF_KMC_PAES_192) ||
2101         !cpacf_test_func(&func_mask, CPACF_KMC_PAES_256))
2102         return -ENODEV;
2103 
2104     pkey_debug_init();
2105 
2106     return misc_register(&pkey_dev);
2107 }
2108 
2109 /*
2110  * Module exit
2111  */
2112 static void __exit pkey_exit(void)
2113 {
2114     misc_deregister(&pkey_dev);
2115     pkey_debug_exit();
2116 }
2117 
2118 module_cpu_feature_match(S390_CPU_FEATURE_MSA, pkey_init);
2119 module_exit(pkey_exit);