Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *  pkey device driver
   4 *
   5 *  Copyright IBM Corp. 2017,2019
   6 *  Author(s): Harald Freudenberger
   7 */
   8
   9#define KMSG_COMPONENT "pkey"
  10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11
  12#include <linux/fs.h>
  13#include <linux/init.h>
  14#include <linux/miscdevice.h>
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/kallsyms.h>
  18#include <linux/debugfs.h>
  19#include <linux/random.h>
  20#include <linux/cpufeature.h>
  21#include <asm/zcrypt.h>
  22#include <asm/cpacf.h>
  23#include <asm/pkey.h>
  24#include <crypto/aes.h>
  25
  26#include "zcrypt_api.h"
  27#include "zcrypt_ccamisc.h"
  28#include "zcrypt_ep11misc.h"
  29
  30MODULE_LICENSE("GPL");
  31MODULE_AUTHOR("IBM Corporation");
  32MODULE_DESCRIPTION("s390 protected key interface");
  33
  34#define KEYBLOBBUFSIZE 8192	/* key buffer size used for internal processing */
  35#define PROTKEYBLOBBUFSIZE 256	/* protected key buffer size used internal */
  36#define MAXAPQNSINLIST 64	/* max 64 apqns within a apqn list */
 
 
  37
  38/*
  39 * debug feature data and functions
  40 */
  41
  42static debug_info_t *debug_info;
  43
  44#define DEBUG_DBG(...)	debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
  45#define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
  46#define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
  47#define DEBUG_ERR(...)	debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
  48
  49static void __init pkey_debug_init(void)
  50{
  51	/* 5 arguments per dbf entry (including the format string ptr) */
  52	debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
  53	debug_register_view(debug_info, &debug_sprintf_view);
  54	debug_set_level(debug_info, 3);
  55}
  56
  57static void __exit pkey_debug_exit(void)
  58{
  59	debug_unregister(debug_info);
  60}
  61
  62/* inside view of a protected key token (only type 0x00 version 0x01) */
  63struct protaeskeytoken {
  64	u8  type;     /* 0x00 for PAES specific key tokens */
  65	u8  res0[3];
  66	u8  version;  /* should be 0x01 for protected AES key token */
  67	u8  res1[3];
  68	u32 keytype;  /* key type, one of the PKEY_KEYTYPE values */
  69	u32 len;      /* bytes actually stored in protkey[] */
  70	u8  protkey[MAXPROTKEYSIZE]; /* the protected key blob */
  71} __packed;
  72
  73/* inside view of a clear key token (type 0x00 version 0x02) */
  74struct clearaeskeytoken {
  75	u8  type;	 /* 0x00 for PAES specific key tokens */
  76	u8  res0[3];
  77	u8  version;	 /* 0x02 for clear AES key token */
  78	u8  res1[3];
  79	u32 keytype;	 /* key type, one of the PKEY_KEYTYPE values */
  80	u32 len;	 /* bytes actually stored in clearkey[] */
  81	u8  clearkey[]; /* clear key value */
  82} __packed;
  83
  84/*
  85 * Create a protected key from a clear key value.
  86 */
  87static int pkey_clr2protkey(u32 keytype,
  88			    const struct pkey_clrkey *clrkey,
  89			    struct pkey_protkey *protkey)
  90{
  91	/* mask of available pckmo subfunctions */
  92	static cpacf_mask_t pckmo_functions;
  93
  94	long fc;
  95	int keysize;
  96	u8 paramblock[64];
  97
  98	switch (keytype) {
  99	case PKEY_KEYTYPE_AES_128:
 100		keysize = 16;
 101		fc = CPACF_PCKMO_ENC_AES_128_KEY;
 102		break;
 103	case PKEY_KEYTYPE_AES_192:
 104		keysize = 24;
 105		fc = CPACF_PCKMO_ENC_AES_192_KEY;
 106		break;
 107	case PKEY_KEYTYPE_AES_256:
 108		keysize = 32;
 109		fc = CPACF_PCKMO_ENC_AES_256_KEY;
 110		break;
 111	default:
 112		DEBUG_ERR("%s unknown/unsupported keytype %d\n",
 113			  __func__, keytype);
 114		return -EINVAL;
 115	}
 116
 117	/* Did we already check for PCKMO ? */
 118	if (!pckmo_functions.bytes[0]) {
 119		/* no, so check now */
 120		if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
 121			return -ENODEV;
 122	}
 123	/* check for the pckmo subfunction we need now */
 124	if (!cpacf_test_func(&pckmo_functions, fc)) {
 125		DEBUG_ERR("%s pckmo functions not available\n", __func__);
 126		return -ENODEV;
 127	}
 128
 129	/* prepare param block */
 130	memset(paramblock, 0, sizeof(paramblock));
 131	memcpy(paramblock, clrkey->clrkey, keysize);
 132
 133	/* call the pckmo instruction */
 134	cpacf_pckmo(fc, paramblock);
 135
 136	/* copy created protected key */
 137	protkey->type = keytype;
 138	protkey->len = keysize + 32;
 139	memcpy(protkey->protkey, paramblock, keysize + 32);
 140
 141	return 0;
 142}
 143
 144/*
 145 * Find card and transform secure key into protected key.
 146 */
 147static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
 148{
 149	int rc, verify;
 150	u16 cardnr, domain;
 151	struct keytoken_header *hdr = (struct keytoken_header *)key;
 152
 153	zcrypt_wait_api_operational();
 154
 155	/*
 156	 * The cca_xxx2protkey call may fail when a card has been
 157	 * addressed where the master key was changed after last fetch
 158	 * of the mkvp into the cache. Try 3 times: First without verify
 159	 * then with verify and last round with verify and old master
 160	 * key verification pattern match not ignored.
 161	 */
 162	for (verify = 0; verify < 3; verify++) {
 163		rc = cca_findcard(key, &cardnr, &domain, verify);
 164		if (rc < 0)
 165			continue;
 166		if (rc > 0 && verify < 2)
 167			continue;
 168		switch (hdr->version) {
 169		case TOKVER_CCA_AES:
 170			rc = cca_sec2protkey(cardnr, domain,
 171					     key, pkey->protkey,
 172					     &pkey->len, &pkey->type);
 173			break;
 174		case TOKVER_CCA_VLSC:
 175			rc = cca_cipher2protkey(cardnr, domain,
 176						key, pkey->protkey,
 177						&pkey->len, &pkey->type);
 178			break;
 179		default:
 180			return -EINVAL;
 181		}
 182		if (rc == 0)
 183			break;
 184	}
 185
 186	if (rc)
 187		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
 188
 189	return rc;
 190}
 191
 192/*
 193 * Construct EP11 key with given clear key value.
 194 */
 195static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
 196			    u8 *keybuf, size_t *keybuflen)
 197{
 198	int i, rc;
 199	u16 card, dom;
 200	u32 nr_apqns, *apqns = NULL;
 201
 202	zcrypt_wait_api_operational();
 203
 204	/* build a list of apqns suitable for ep11 keys with cpacf support */
 205	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
 206			    ZCRYPT_CEX7, EP11_API_V, NULL);
 207	if (rc)
 208		goto out;
 209
 210	/* go through the list of apqns and try to bild an ep11 key */
 211	for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
 212		card = apqns[i] >> 16;
 213		dom = apqns[i] & 0xFFFF;
 214		rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
 215				      0, clrkey, keybuf, keybuflen);
 216		if (rc == 0)
 217			break;
 218	}
 219
 220out:
 221	kfree(apqns);
 222	if (rc)
 223		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
 224	return rc;
 225}
 226
 227/*
 228 * Find card and transform EP11 secure key into protected key.
 229 */
 230static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
 231{
 232	int i, rc;
 233	u16 card, dom;
 234	u32 nr_apqns, *apqns = NULL;
 235	struct ep11keyblob *kb = (struct ep11keyblob *)key;
 236
 237	zcrypt_wait_api_operational();
 238
 239	/* build a list of apqns suitable for this key */
 240	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
 241			    ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
 242	if (rc)
 243		goto out;
 244
 245	/* go through the list of apqns and try to derive an pkey */
 246	for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
 247		card = apqns[i] >> 16;
 248		dom = apqns[i] & 0xFFFF;
 249		pkey->len = sizeof(pkey->protkey);
 250		rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
 251					pkey->protkey, &pkey->len, &pkey->type);
 252		if (rc == 0)
 253			break;
 254	}
 255
 256out:
 257	kfree(apqns);
 258	if (rc)
 259		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
 260	return rc;
 261}
 262
 263/*
 264 * Verify key and give back some info about the key.
 265 */
 266static int pkey_verifykey(const struct pkey_seckey *seckey,
 267			  u16 *pcardnr, u16 *pdomain,
 268			  u16 *pkeysize, u32 *pattributes)
 269{
 270	struct secaeskeytoken *t = (struct secaeskeytoken *)seckey;
 271	u16 cardnr, domain;
 272	int rc;
 273
 274	/* check the secure key for valid AES secure key */
 275	rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *)seckey, 0);
 276	if (rc)
 277		goto out;
 278	if (pattributes)
 279		*pattributes = PKEY_VERIFY_ATTR_AES;
 280	if (pkeysize)
 281		*pkeysize = t->bitsize;
 282
 283	/* try to find a card which can handle this key */
 284	rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
 285	if (rc < 0)
 286		goto out;
 287
 288	if (rc > 0) {
 289		/* key mkvp matches to old master key mkvp */
 290		DEBUG_DBG("%s secure key has old mkvp\n", __func__);
 291		if (pattributes)
 292			*pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
 293		rc = 0;
 294	}
 295
 296	if (pcardnr)
 297		*pcardnr = cardnr;
 298	if (pdomain)
 299		*pdomain = domain;
 300
 301out:
 302	DEBUG_DBG("%s rc=%d\n", __func__, rc);
 303	return rc;
 304}
 305
 306/*
 307 * Generate a random protected key
 308 */
 309static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
 310{
 311	struct pkey_clrkey clrkey;
 312	int keysize;
 313	int rc;
 314
 315	switch (keytype) {
 316	case PKEY_KEYTYPE_AES_128:
 317		keysize = 16;
 318		break;
 319	case PKEY_KEYTYPE_AES_192:
 320		keysize = 24;
 321		break;
 322	case PKEY_KEYTYPE_AES_256:
 323		keysize = 32;
 324		break;
 325	default:
 326		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
 327			  keytype);
 328		return -EINVAL;
 329	}
 330
 331	/* generate a dummy random clear key */
 332	get_random_bytes(clrkey.clrkey, keysize);
 333
 334	/* convert it to a dummy protected key */
 335	rc = pkey_clr2protkey(keytype, &clrkey, protkey);
 336	if (rc)
 337		return rc;
 338
 339	/* replace the key part of the protected key with random bytes */
 340	get_random_bytes(protkey->protkey, keysize);
 341
 342	return 0;
 343}
 344
 345/*
 346 * Verify if a protected key is still valid
 347 */
 348static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
 349{
 350	unsigned long fc;
 351	struct {
 352		u8 iv[AES_BLOCK_SIZE];
 353		u8 key[MAXPROTKEYSIZE];
 354	} param;
 355	u8 null_msg[AES_BLOCK_SIZE];
 356	u8 dest_buf[AES_BLOCK_SIZE];
 357	unsigned int k;
 358
 359	switch (protkey->type) {
 360	case PKEY_KEYTYPE_AES_128:
 361		fc = CPACF_KMC_PAES_128;
 362		break;
 363	case PKEY_KEYTYPE_AES_192:
 364		fc = CPACF_KMC_PAES_192;
 365		break;
 366	case PKEY_KEYTYPE_AES_256:
 367		fc = CPACF_KMC_PAES_256;
 368		break;
 369	default:
 370		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
 371			  protkey->type);
 372		return -EINVAL;
 373	}
 374
 375	memset(null_msg, 0, sizeof(null_msg));
 376
 377	memset(param.iv, 0, sizeof(param.iv));
 378	memcpy(param.key, protkey->protkey, sizeof(param.key));
 379
 380	k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
 381		      sizeof(null_msg));
 382	if (k != sizeof(null_msg)) {
 383		DEBUG_ERR("%s protected key is not valid\n", __func__);
 384		return -EKEYREJECTED;
 385	}
 386
 387	return 0;
 388}
 389
 390/*
 391 * Transform a non-CCA key token into a protected key
 392 */
 393static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
 394			       struct pkey_protkey *protkey)
 395{
 396	int rc = -EINVAL;
 397	u8 *tmpbuf = NULL;
 398	struct keytoken_header *hdr = (struct keytoken_header *)key;
 399
 400	switch (hdr->version) {
 401	case TOKVER_PROTECTED_KEY: {
 402		struct protaeskeytoken *t;
 403
 404		if (keylen != sizeof(struct protaeskeytoken))
 405			goto out;
 406		t = (struct protaeskeytoken *)key;
 407		protkey->len = t->len;
 408		protkey->type = t->keytype;
 409		memcpy(protkey->protkey, t->protkey,
 410		       sizeof(protkey->protkey));
 411		rc = pkey_verifyprotkey(protkey);
 412		break;
 413	}
 414	case TOKVER_CLEAR_KEY: {
 415		struct clearaeskeytoken *t;
 416		struct pkey_clrkey ckey;
 417		union u_tmpbuf {
 418			u8 skey[SECKEYBLOBSIZE];
 419			u8 ep11key[MAXEP11AESKEYBLOBSIZE];
 420		};
 421		size_t tmpbuflen = sizeof(union u_tmpbuf);
 422
 423		if (keylen < sizeof(struct clearaeskeytoken))
 424			goto out;
 425		t = (struct clearaeskeytoken *)key;
 426		if (keylen != sizeof(*t) + t->len)
 427			goto out;
 428		if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16) ||
 429		    (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24) ||
 430		    (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
 431			memcpy(ckey.clrkey, t->clearkey, t->len);
 432		else
 433			goto out;
 434		/* alloc temp key buffer space */
 435		tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
 436		if (!tmpbuf) {
 437			rc = -ENOMEM;
 438			goto out;
 439		}
 440		/* try direct way with the PCKMO instruction */
 441		rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
 442		if (rc == 0)
 443			break;
 444		/* PCKMO failed, so try the CCA secure key way */
 445		zcrypt_wait_api_operational();
 446		rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
 447				    ckey.clrkey, tmpbuf);
 448		if (rc == 0)
 449			rc = pkey_skey2pkey(tmpbuf, protkey);
 450		if (rc == 0)
 451			break;
 452		/* if the CCA way also failed, let's try via EP11 */
 453		rc = pkey_clr2ep11key(ckey.clrkey, t->len,
 454				      tmpbuf, &tmpbuflen);
 455		if (rc == 0)
 456			rc = pkey_ep11key2pkey(tmpbuf, protkey);
 457		/* now we should really have an protected key */
 458		DEBUG_ERR("%s unable to build protected key from clear",
 459			  __func__);
 460		break;
 461	}
 462	case TOKVER_EP11_AES: {
 
 
 463		/* check ep11 key for exportable as protected key */
 464		rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
 465		if (rc)
 466			goto out;
 467		rc = pkey_ep11key2pkey(key, protkey);
 468		break;
 469	}
 470	case TOKVER_EP11_AES_WITH_HEADER:
 471		/* check ep11 key with header for exportable as protected key */
 472		rc = ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1);
 473		if (rc)
 474			goto out;
 475		rc = pkey_ep11key2pkey(key + sizeof(struct ep11kblob_header),
 476				       protkey);
 477		break;
 478	default:
 479		DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
 480			  __func__, hdr->version);
 481		rc = -EINVAL;
 482	}
 483
 484out:
 485	kfree(tmpbuf);
 486	return rc;
 487}
 488
 489/*
 490 * Transform a CCA internal key token into a protected key
 491 */
 492static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
 493			       struct pkey_protkey *protkey)
 494{
 495	struct keytoken_header *hdr = (struct keytoken_header *)key;
 496
 497	switch (hdr->version) {
 498	case TOKVER_CCA_AES:
 499		if (keylen != sizeof(struct secaeskeytoken))
 500			return -EINVAL;
 501		break;
 502	case TOKVER_CCA_VLSC:
 503		if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
 504			return -EINVAL;
 505		break;
 506	default:
 507		DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
 508			  __func__, hdr->version);
 509		return -EINVAL;
 510	}
 511
 512	return pkey_skey2pkey(key, protkey);
 513}
 514
 515/*
 516 * Transform a key blob (of any type) into a protected key
 517 */
 518int pkey_keyblob2pkey(const u8 *key, u32 keylen,
 519		      struct pkey_protkey *protkey)
 520{
 521	int rc;
 522	struct keytoken_header *hdr = (struct keytoken_header *)key;
 523
 524	if (keylen < sizeof(struct keytoken_header)) {
 525		DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
 526		return -EINVAL;
 527	}
 528
 529	switch (hdr->type) {
 530	case TOKTYPE_NON_CCA:
 531		rc = pkey_nonccatok2pkey(key, keylen, protkey);
 532		break;
 533	case TOKTYPE_CCA_INTERNAL:
 534		rc = pkey_ccainttok2pkey(key, keylen, protkey);
 535		break;
 536	default:
 537		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
 538			  __func__, hdr->type);
 539		return -EINVAL;
 540	}
 541
 542	DEBUG_DBG("%s rc=%d\n", __func__, rc);
 543	return rc;
 
 544}
 545EXPORT_SYMBOL(pkey_keyblob2pkey);
 546
 547static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
 548			   enum pkey_key_type ktype, enum pkey_key_size ksize,
 549			   u32 kflags, u8 *keybuf, size_t *keybufsize)
 550{
 551	int i, card, dom, rc;
 552
 553	/* check for at least one apqn given */
 554	if (!apqns || !nr_apqns)
 555		return -EINVAL;
 556
 557	/* check key type and size */
 558	switch (ktype) {
 559	case PKEY_TYPE_CCA_DATA:
 560	case PKEY_TYPE_CCA_CIPHER:
 561		if (*keybufsize < SECKEYBLOBSIZE)
 562			return -EINVAL;
 563		break;
 564	case PKEY_TYPE_EP11:
 565		if (*keybufsize < MINEP11AESKEYBLOBSIZE)
 566			return -EINVAL;
 567		break;
 568	default:
 569		return -EINVAL;
 570	}
 571	switch (ksize) {
 572	case PKEY_SIZE_AES_128:
 573	case PKEY_SIZE_AES_192:
 574	case PKEY_SIZE_AES_256:
 575		break;
 576	default:
 577		return -EINVAL;
 578	}
 579
 580	/* simple try all apqns from the list */
 581	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
 582		card = apqns[i].card;
 583		dom = apqns[i].domain;
 584		if (ktype == PKEY_TYPE_EP11) {
 585			rc = ep11_genaeskey(card, dom, ksize, kflags,
 586					    keybuf, keybufsize);
 587		} else if (ktype == PKEY_TYPE_CCA_DATA) {
 588			rc = cca_genseckey(card, dom, ksize, keybuf);
 589			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
 590		} else {
 591			/* TOKVER_CCA_VLSC */
 592			rc = cca_gencipherkey(card, dom, ksize, kflags,
 593					      keybuf, keybufsize);
 594		}
 595		if (rc == 0)
 596			break;
 597	}
 598
 599	return rc;
 600}
 601
 602static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
 603			    enum pkey_key_type ktype, enum pkey_key_size ksize,
 604			    u32 kflags, const u8 *clrkey,
 605			    u8 *keybuf, size_t *keybufsize)
 606{
 607	int i, card, dom, rc;
 608
 609	/* check for at least one apqn given */
 610	if (!apqns || !nr_apqns)
 611		return -EINVAL;
 612
 613	/* check key type and size */
 614	switch (ktype) {
 615	case PKEY_TYPE_CCA_DATA:
 616	case PKEY_TYPE_CCA_CIPHER:
 617		if (*keybufsize < SECKEYBLOBSIZE)
 618			return -EINVAL;
 619		break;
 620	case PKEY_TYPE_EP11:
 621		if (*keybufsize < MINEP11AESKEYBLOBSIZE)
 622			return -EINVAL;
 623		break;
 624	default:
 625		return -EINVAL;
 626	}
 627	switch (ksize) {
 628	case PKEY_SIZE_AES_128:
 629	case PKEY_SIZE_AES_192:
 630	case PKEY_SIZE_AES_256:
 631		break;
 632	default:
 633		return -EINVAL;
 634	}
 635
 636	zcrypt_wait_api_operational();
 637
 638	/* simple try all apqns from the list */
 639	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
 640		card = apqns[i].card;
 641		dom = apqns[i].domain;
 642		if (ktype == PKEY_TYPE_EP11) {
 643			rc = ep11_clr2keyblob(card, dom, ksize, kflags,
 644					      clrkey, keybuf, keybufsize);
 645		} else if (ktype == PKEY_TYPE_CCA_DATA) {
 646			rc = cca_clr2seckey(card, dom, ksize,
 647					    clrkey, keybuf);
 648			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
 649		} else {
 650			/* TOKVER_CCA_VLSC */
 651			rc = cca_clr2cipherkey(card, dom, ksize, kflags,
 652					       clrkey, keybuf, keybufsize);
 653		}
 654		if (rc == 0)
 655			break;
 656	}
 657
 658	return rc;
 659}
 660
 661static int pkey_verifykey2(const u8 *key, size_t keylen,
 662			   u16 *cardnr, u16 *domain,
 663			   enum pkey_key_type *ktype,
 664			   enum pkey_key_size *ksize, u32 *flags)
 665{
 666	int rc;
 667	u32 _nr_apqns, *_apqns = NULL;
 668	struct keytoken_header *hdr = (struct keytoken_header *)key;
 669
 670	if (keylen < sizeof(struct keytoken_header))
 671		return -EINVAL;
 672
 673	if (hdr->type == TOKTYPE_CCA_INTERNAL &&
 674	    hdr->version == TOKVER_CCA_AES) {
 675		struct secaeskeytoken *t = (struct secaeskeytoken *)key;
 676
 677		rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
 678		if (rc)
 679			goto out;
 680		if (ktype)
 681			*ktype = PKEY_TYPE_CCA_DATA;
 682		if (ksize)
 683			*ksize = (enum pkey_key_size)t->bitsize;
 684
 685		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
 686				   ZCRYPT_CEX3C, AES_MK_SET, t->mkvp, 0, 1);
 687		if (rc == 0 && flags)
 688			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
 689		if (rc == -ENODEV) {
 690			rc = cca_findcard2(&_apqns, &_nr_apqns,
 691					   *cardnr, *domain,
 692					   ZCRYPT_CEX3C, AES_MK_SET,
 693					   0, t->mkvp, 1);
 694			if (rc == 0 && flags)
 695				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
 696		}
 697		if (rc)
 698			goto out;
 699
 700		*cardnr = ((struct pkey_apqn *)_apqns)->card;
 701		*domain = ((struct pkey_apqn *)_apqns)->domain;
 702
 703	} else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
 704		   hdr->version == TOKVER_CCA_VLSC) {
 705		struct cipherkeytoken *t = (struct cipherkeytoken *)key;
 706
 707		rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
 708		if (rc)
 709			goto out;
 710		if (ktype)
 711			*ktype = PKEY_TYPE_CCA_CIPHER;
 712		if (ksize) {
 713			*ksize = PKEY_SIZE_UNKNOWN;
 714			if (!t->plfver && t->wpllen == 512)
 715				*ksize = PKEY_SIZE_AES_128;
 716			else if (!t->plfver && t->wpllen == 576)
 717				*ksize = PKEY_SIZE_AES_192;
 718			else if (!t->plfver && t->wpllen == 640)
 719				*ksize = PKEY_SIZE_AES_256;
 720		}
 721
 722		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
 723				   ZCRYPT_CEX6, AES_MK_SET, t->mkvp0, 0, 1);
 724		if (rc == 0 && flags)
 725			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
 726		if (rc == -ENODEV) {
 727			rc = cca_findcard2(&_apqns, &_nr_apqns,
 728					   *cardnr, *domain,
 729					   ZCRYPT_CEX6, AES_MK_SET,
 730					   0, t->mkvp0, 1);
 731			if (rc == 0 && flags)
 732				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
 733		}
 734		if (rc)
 735			goto out;
 736
 737		*cardnr = ((struct pkey_apqn *)_apqns)->card;
 738		*domain = ((struct pkey_apqn *)_apqns)->domain;
 739
 740	} else if (hdr->type == TOKTYPE_NON_CCA &&
 741		   hdr->version == TOKVER_EP11_AES) {
 742		struct ep11keyblob *kb = (struct ep11keyblob *)key;
 743
 744		rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
 745		if (rc)
 746			goto out;
 747		if (ktype)
 748			*ktype = PKEY_TYPE_EP11;
 749		if (ksize)
 750			*ksize = kb->head.keybitlen;
 751
 752		rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
 753				    ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
 754		if (rc)
 755			goto out;
 756
 757		if (flags)
 758			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
 759
 760		*cardnr = ((struct pkey_apqn *)_apqns)->card;
 761		*domain = ((struct pkey_apqn *)_apqns)->domain;
 762
 763	} else {
 764		rc = -EINVAL;
 765	}
 766
 767out:
 768	kfree(_apqns);
 769	return rc;
 770}
 771
 772static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
 773			      const u8 *key, size_t keylen,
 774			      struct pkey_protkey *pkey)
 775{
 776	int i, card, dom, rc;
 777	struct keytoken_header *hdr = (struct keytoken_header *)key;
 778
 779	/* check for at least one apqn given */
 780	if (!apqns || !nr_apqns)
 781		return -EINVAL;
 782
 783	if (keylen < sizeof(struct keytoken_header))
 784		return -EINVAL;
 785
 786	if (hdr->type == TOKTYPE_CCA_INTERNAL) {
 787		if (hdr->version == TOKVER_CCA_AES) {
 788			if (keylen != sizeof(struct secaeskeytoken))
 789				return -EINVAL;
 790			if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
 791				return -EINVAL;
 792		} else if (hdr->version == TOKVER_CCA_VLSC) {
 793			if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
 794				return -EINVAL;
 795			if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
 796				return -EINVAL;
 797		} else {
 798			DEBUG_ERR("%s unknown CCA internal token version %d\n",
 799				  __func__, hdr->version);
 800			return -EINVAL;
 801		}
 802	} else if (hdr->type == TOKTYPE_NON_CCA) {
 803		if (hdr->version == TOKVER_EP11_AES) {
 804			if (keylen < sizeof(struct ep11keyblob))
 805				return -EINVAL;
 806			if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
 807				return -EINVAL;
 808		} else {
 809			return pkey_nonccatok2pkey(key, keylen, pkey);
 810		}
 811	} else {
 812		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
 813			  __func__, hdr->type);
 814		return -EINVAL;
 815	}
 816
 817	zcrypt_wait_api_operational();
 818
 819	/* simple try all apqns from the list */
 820	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
 821		card = apqns[i].card;
 822		dom = apqns[i].domain;
 823		if (hdr->type == TOKTYPE_CCA_INTERNAL &&
 824		    hdr->version == TOKVER_CCA_AES) {
 825			rc = cca_sec2protkey(card, dom, key, pkey->protkey,
 826					     &pkey->len, &pkey->type);
 827		} else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
 828			   hdr->version == TOKVER_CCA_VLSC) {
 829			rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
 830						&pkey->len, &pkey->type);
 831		} else {
 832			/* EP11 AES secure key blob */
 833			struct ep11keyblob *kb = (struct ep11keyblob *)key;
 834
 835			pkey->len = sizeof(pkey->protkey);
 836			rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
 837						pkey->protkey, &pkey->len,
 838						&pkey->type);
 839		}
 840		if (rc == 0)
 841			break;
 842	}
 843
 844	return rc;
 845}
 846
 847static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
 848			  struct pkey_apqn *apqns, size_t *nr_apqns)
 849{
 850	int rc;
 851	u32 _nr_apqns, *_apqns = NULL;
 852	struct keytoken_header *hdr = (struct keytoken_header *)key;
 853
 854	if (keylen < sizeof(struct keytoken_header) || flags == 0)
 855		return -EINVAL;
 856
 857	zcrypt_wait_api_operational();
 858
 859	if (hdr->type == TOKTYPE_NON_CCA &&
 860	    (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
 861	     hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
 862	    is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
 863		int minhwtype = 0, api = 0;
 864		struct ep11keyblob *kb = (struct ep11keyblob *)
 865			(key + sizeof(struct ep11kblob_header));
 866
 867		if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
 868			return -EINVAL;
 869		if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
 870			minhwtype = ZCRYPT_CEX7;
 871			api = EP11_API_V;
 872		}
 873		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 874				    minhwtype, api, kb->wkvp);
 875		if (rc)
 876			goto out;
 877	} else if (hdr->type == TOKTYPE_NON_CCA &&
 878		   hdr->version == TOKVER_EP11_AES &&
 879		   is_ep11_keyblob(key)) {
 880		int minhwtype = 0, api = 0;
 881		struct ep11keyblob *kb = (struct ep11keyblob *)key;
 882
 883		if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
 884			return -EINVAL;
 885		if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
 886			minhwtype = ZCRYPT_CEX7;
 887			api = EP11_API_V;
 888		}
 889		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 890				    minhwtype, api, kb->wkvp);
 891		if (rc)
 892			goto out;
 893	} else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
 894		int minhwtype = ZCRYPT_CEX3C;
 895		u64 cur_mkvp = 0, old_mkvp = 0;
 896
 897		if (hdr->version == TOKVER_CCA_AES) {
 898			struct secaeskeytoken *t = (struct secaeskeytoken *)key;
 899
 900			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 901				cur_mkvp = t->mkvp;
 902			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 903				old_mkvp = t->mkvp;
 904		} else if (hdr->version == TOKVER_CCA_VLSC) {
 905			struct cipherkeytoken *t = (struct cipherkeytoken *)key;
 906
 907			minhwtype = ZCRYPT_CEX6;
 908			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 909				cur_mkvp = t->mkvp0;
 910			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 911				old_mkvp = t->mkvp0;
 912		} else {
 913			/* unknown cca internal token type */
 914			return -EINVAL;
 915		}
 916		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 917				   minhwtype, AES_MK_SET,
 918				   cur_mkvp, old_mkvp, 1);
 919		if (rc)
 920			goto out;
 921	} else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
 922		u64 cur_mkvp = 0, old_mkvp = 0;
 923		struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
 924
 925		if (t->secid == 0x20) {
 926			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 927				cur_mkvp = t->mkvp;
 928			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 929				old_mkvp = t->mkvp;
 930		} else {
 931			/* unknown cca internal 2 token type */
 932			return -EINVAL;
 933		}
 934		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 935				   ZCRYPT_CEX7, APKA_MK_SET,
 936				   cur_mkvp, old_mkvp, 1);
 937		if (rc)
 938			goto out;
 939	} else {
 940		return -EINVAL;
 941	}
 942
 943	if (apqns) {
 944		if (*nr_apqns < _nr_apqns)
 945			rc = -ENOSPC;
 946		else
 947			memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
 948	}
 949	*nr_apqns = _nr_apqns;
 950
 951out:
 952	kfree(_apqns);
 953	return rc;
 954}
 955
 956static int pkey_apqns4keytype(enum pkey_key_type ktype,
 957			      u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
 958			      struct pkey_apqn *apqns, size_t *nr_apqns)
 959{
 960	int rc;
 961	u32 _nr_apqns, *_apqns = NULL;
 962
 963	zcrypt_wait_api_operational();
 964
 965	if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
 966		u64 cur_mkvp = 0, old_mkvp = 0;
 967		int minhwtype = ZCRYPT_CEX3C;
 968
 969		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 970			cur_mkvp = *((u64 *)cur_mkvp);
 971		if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 972			old_mkvp = *((u64 *)alt_mkvp);
 973		if (ktype == PKEY_TYPE_CCA_CIPHER)
 974			minhwtype = ZCRYPT_CEX6;
 975		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 976				   minhwtype, AES_MK_SET,
 977				   cur_mkvp, old_mkvp, 1);
 978		if (rc)
 979			goto out;
 980	} else if (ktype == PKEY_TYPE_CCA_ECC) {
 981		u64 cur_mkvp = 0, old_mkvp = 0;
 982
 983		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 984			cur_mkvp = *((u64 *)cur_mkvp);
 985		if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 986			old_mkvp = *((u64 *)alt_mkvp);
 987		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 988				   ZCRYPT_CEX7, APKA_MK_SET,
 989				   cur_mkvp, old_mkvp, 1);
 990		if (rc)
 991			goto out;
 992
 993	} else if (ktype == PKEY_TYPE_EP11 ||
 994		   ktype == PKEY_TYPE_EP11_AES ||
 995		   ktype == PKEY_TYPE_EP11_ECC) {
 996		u8 *wkvp = NULL;
 997
 998		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 999			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
1017out:
1018	kfree(_apqns);
1019	return rc;
1020}
1021
1022static 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
1131static 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
1139static 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
1147static 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 */
1594static 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
1638static 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
1648static 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
1658static 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
1668static 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
1678static 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
1688static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1689static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1690static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1691static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1692static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1693
1694static 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
1703static 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 */
1714static 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
1742static 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
1752static 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
1762static 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
1772static 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
1782static 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
1792static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1793static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1794static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1795static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1796static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1797
1798static 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
1807static 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 */
1820static 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
1864static 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
1874static 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
1884static 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
1894static 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
1904static 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
1914static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1915static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1916static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1917static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1918static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1919
1920static 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
1929static 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 */
1943static 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
1987static 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
1997static 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
2007static 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
2017static 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
2027static 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
2037static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
2038static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
2039static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
2040static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2041static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
2042
2043static 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
2052static struct attribute_group ep11_attr_group = {
2053	.name	   = "ep11",
2054	.bin_attrs = ep11_attrs,
2055};
2056
2057static 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
2065static 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
2072static 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 */
2083static 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 */
2112static void __exit pkey_exit(void)
2113{
2114	misc_deregister(&pkey_dev);
2115	pkey_debug_exit();
2116}
2117
2118module_cpu_feature_match(S390_CPU_FEATURE_MSA, pkey_init);
2119module_exit(pkey_exit);
v5.9
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *  pkey device driver
   4 *
   5 *  Copyright IBM Corp. 2017,2019
   6 *  Author(s): Harald Freudenberger
   7 */
   8
   9#define KMSG_COMPONENT "pkey"
  10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11
  12#include <linux/fs.h>
  13#include <linux/init.h>
  14#include <linux/miscdevice.h>
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/kallsyms.h>
  18#include <linux/debugfs.h>
  19#include <linux/random.h>
  20#include <linux/cpufeature.h>
  21#include <asm/zcrypt.h>
  22#include <asm/cpacf.h>
  23#include <asm/pkey.h>
  24#include <crypto/aes.h>
  25
  26#include "zcrypt_api.h"
  27#include "zcrypt_ccamisc.h"
  28#include "zcrypt_ep11misc.h"
  29
  30MODULE_LICENSE("GPL");
  31MODULE_AUTHOR("IBM Corporation");
  32MODULE_DESCRIPTION("s390 protected key interface");
  33
  34#define KEYBLOBBUFSIZE 8192  /* key buffer size used for internal processing */
  35#define MAXAPQNSINLIST 64    /* max 64 apqns within a apqn list */
  36
  37/* mask of available pckmo subfunctions, fetched once at module init */
  38static cpacf_mask_t pckmo_functions;
  39
  40/*
  41 * debug feature data and functions
  42 */
  43
  44static debug_info_t *debug_info;
  45
  46#define DEBUG_DBG(...)	debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
  47#define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
  48#define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
  49#define DEBUG_ERR(...)	debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
  50
  51static void __init pkey_debug_init(void)
  52{
  53	/* 5 arguments per dbf entry (including the format string ptr) */
  54	debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
  55	debug_register_view(debug_info, &debug_sprintf_view);
  56	debug_set_level(debug_info, 3);
  57}
  58
  59static void __exit pkey_debug_exit(void)
  60{
  61	debug_unregister(debug_info);
  62}
  63
  64/* inside view of a protected key token (only type 0x00 version 0x01) */
  65struct protaeskeytoken {
  66	u8  type;     /* 0x00 for PAES specific key tokens */
  67	u8  res0[3];
  68	u8  version;  /* should be 0x01 for protected AES key token */
  69	u8  res1[3];
  70	u32 keytype;  /* key type, one of the PKEY_KEYTYPE values */
  71	u32 len;      /* bytes actually stored in protkey[] */
  72	u8  protkey[MAXPROTKEYSIZE]; /* the protected key blob */
  73} __packed;
  74
  75/* inside view of a clear key token (type 0x00 version 0x02) */
  76struct clearaeskeytoken {
  77	u8  type;	 /* 0x00 for PAES specific key tokens */
  78	u8  res0[3];
  79	u8  version;	 /* 0x02 for clear AES key token */
  80	u8  res1[3];
  81	u32 keytype;	 /* key type, one of the PKEY_KEYTYPE values */
  82	u32 len;	 /* bytes actually stored in clearkey[] */
  83	u8  clearkey[]; /* clear key value */
  84} __packed;
  85
  86/*
  87 * Create a protected key from a clear key value.
  88 */
  89static int pkey_clr2protkey(u32 keytype,
  90			    const struct pkey_clrkey *clrkey,
  91			    struct pkey_protkey *protkey)
  92{
 
 
 
  93	long fc;
  94	int keysize;
  95	u8 paramblock[64];
  96
  97	switch (keytype) {
  98	case PKEY_KEYTYPE_AES_128:
  99		keysize = 16;
 100		fc = CPACF_PCKMO_ENC_AES_128_KEY;
 101		break;
 102	case PKEY_KEYTYPE_AES_192:
 103		keysize = 24;
 104		fc = CPACF_PCKMO_ENC_AES_192_KEY;
 105		break;
 106	case PKEY_KEYTYPE_AES_256:
 107		keysize = 32;
 108		fc = CPACF_PCKMO_ENC_AES_256_KEY;
 109		break;
 110	default:
 111		DEBUG_ERR("%s unknown/unsupported keytype %d\n",
 112			  __func__, keytype);
 113		return -EINVAL;
 114	}
 115
 116	/*
 117	 * Check if the needed pckmo subfunction is available.
 118	 * These subfunctions can be enabled/disabled by customers
 119	 * in the LPAR profile or may even change on the fly.
 120	 */
 
 
 121	if (!cpacf_test_func(&pckmo_functions, fc)) {
 122		DEBUG_ERR("%s pckmo functions not available\n", __func__);
 123		return -ENODEV;
 124	}
 125
 126	/* prepare param block */
 127	memset(paramblock, 0, sizeof(paramblock));
 128	memcpy(paramblock, clrkey->clrkey, keysize);
 129
 130	/* call the pckmo instruction */
 131	cpacf_pckmo(fc, paramblock);
 132
 133	/* copy created protected key */
 134	protkey->type = keytype;
 135	protkey->len = keysize + 32;
 136	memcpy(protkey->protkey, paramblock, keysize + 32);
 137
 138	return 0;
 139}
 140
 141/*
 142 * Find card and transform secure key into protected key.
 143 */
 144static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
 145{
 146	int rc, verify;
 147	u16 cardnr, domain;
 148	struct keytoken_header *hdr = (struct keytoken_header *)key;
 149
 
 
 150	/*
 151	 * The cca_xxx2protkey call may fail when a card has been
 152	 * addressed where the master key was changed after last fetch
 153	 * of the mkvp into the cache. Try 3 times: First witout verify
 154	 * then with verify and last round with verify and old master
 155	 * key verification pattern match not ignored.
 156	 */
 157	for (verify = 0; verify < 3; verify++) {
 158		rc = cca_findcard(key, &cardnr, &domain, verify);
 159		if (rc < 0)
 160			continue;
 161		if (rc > 0 && verify < 2)
 162			continue;
 163		switch (hdr->version) {
 164		case TOKVER_CCA_AES:
 165			rc = cca_sec2protkey(cardnr, domain,
 166					     key, pkey->protkey,
 167					     &pkey->len, &pkey->type);
 168			break;
 169		case TOKVER_CCA_VLSC:
 170			rc = cca_cipher2protkey(cardnr, domain,
 171						key, pkey->protkey,
 172						&pkey->len, &pkey->type);
 173			break;
 174		default:
 175			return -EINVAL;
 176		}
 177		if (rc == 0)
 178			break;
 179	}
 180
 181	if (rc)
 182		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
 183
 184	return rc;
 185}
 186
 187/*
 188 * Construct EP11 key with given clear key value.
 189 */
 190static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
 191			    u8 *keybuf, size_t *keybuflen)
 192{
 193	int i, rc;
 194	u16 card, dom;
 195	u32 nr_apqns, *apqns = NULL;
 196
 
 
 197	/* build a list of apqns suitable for ep11 keys with cpacf support */
 198	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
 199			    ZCRYPT_CEX7, EP11_API_V, NULL);
 200	if (rc)
 201		goto out;
 202
 203	/* go through the list of apqns and try to bild an ep11 key */
 204	for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
 205		card = apqns[i] >> 16;
 206		dom = apqns[i] & 0xFFFF;
 207		rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
 208				      0, clrkey, keybuf, keybuflen);
 209		if (rc == 0)
 210			break;
 211	}
 212
 213out:
 214	kfree(apqns);
 215	if (rc)
 216		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
 217	return rc;
 218}
 219
 220/*
 221 * Find card and transform EP11 secure key into protected key.
 222 */
 223static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
 224{
 225	int i, rc;
 226	u16 card, dom;
 227	u32 nr_apqns, *apqns = NULL;
 228	struct ep11keyblob *kb = (struct ep11keyblob *) key;
 
 
 229
 230	/* build a list of apqns suitable for this key */
 231	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
 232			    ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
 233	if (rc)
 234		goto out;
 235
 236	/* go through the list of apqns and try to derive an pkey */
 237	for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
 238		card = apqns[i] >> 16;
 239		dom = apqns[i] & 0xFFFF;
 240		rc = ep11_key2protkey(card, dom, key, kb->head.len,
 241				      pkey->protkey, &pkey->len, &pkey->type);
 
 242		if (rc == 0)
 243			break;
 244	}
 245
 246out:
 247	kfree(apqns);
 248	if (rc)
 249		DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
 250	return rc;
 251}
 252
 253/*
 254 * Verify key and give back some info about the key.
 255 */
 256static int pkey_verifykey(const struct pkey_seckey *seckey,
 257			  u16 *pcardnr, u16 *pdomain,
 258			  u16 *pkeysize, u32 *pattributes)
 259{
 260	struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
 261	u16 cardnr, domain;
 262	int rc;
 263
 264	/* check the secure key for valid AES secure key */
 265	rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0);
 266	if (rc)
 267		goto out;
 268	if (pattributes)
 269		*pattributes = PKEY_VERIFY_ATTR_AES;
 270	if (pkeysize)
 271		*pkeysize = t->bitsize;
 272
 273	/* try to find a card which can handle this key */
 274	rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
 275	if (rc < 0)
 276		goto out;
 277
 278	if (rc > 0) {
 279		/* key mkvp matches to old master key mkvp */
 280		DEBUG_DBG("%s secure key has old mkvp\n", __func__);
 281		if (pattributes)
 282			*pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
 283		rc = 0;
 284	}
 285
 286	if (pcardnr)
 287		*pcardnr = cardnr;
 288	if (pdomain)
 289		*pdomain = domain;
 290
 291out:
 292	DEBUG_DBG("%s rc=%d\n", __func__, rc);
 293	return rc;
 294}
 295
 296/*
 297 * Generate a random protected key
 298 */
 299static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
 300{
 301	struct pkey_clrkey clrkey;
 302	int keysize;
 303	int rc;
 304
 305	switch (keytype) {
 306	case PKEY_KEYTYPE_AES_128:
 307		keysize = 16;
 308		break;
 309	case PKEY_KEYTYPE_AES_192:
 310		keysize = 24;
 311		break;
 312	case PKEY_KEYTYPE_AES_256:
 313		keysize = 32;
 314		break;
 315	default:
 316		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
 317			  keytype);
 318		return -EINVAL;
 319	}
 320
 321	/* generate a dummy random clear key */
 322	get_random_bytes(clrkey.clrkey, keysize);
 323
 324	/* convert it to a dummy protected key */
 325	rc = pkey_clr2protkey(keytype, &clrkey, protkey);
 326	if (rc)
 327		return rc;
 328
 329	/* replace the key part of the protected key with random bytes */
 330	get_random_bytes(protkey->protkey, keysize);
 331
 332	return 0;
 333}
 334
 335/*
 336 * Verify if a protected key is still valid
 337 */
 338static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
 339{
 340	unsigned long fc;
 341	struct {
 342		u8 iv[AES_BLOCK_SIZE];
 343		u8 key[MAXPROTKEYSIZE];
 344	} param;
 345	u8 null_msg[AES_BLOCK_SIZE];
 346	u8 dest_buf[AES_BLOCK_SIZE];
 347	unsigned int k;
 348
 349	switch (protkey->type) {
 350	case PKEY_KEYTYPE_AES_128:
 351		fc = CPACF_KMC_PAES_128;
 352		break;
 353	case PKEY_KEYTYPE_AES_192:
 354		fc = CPACF_KMC_PAES_192;
 355		break;
 356	case PKEY_KEYTYPE_AES_256:
 357		fc = CPACF_KMC_PAES_256;
 358		break;
 359	default:
 360		DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
 361			  protkey->type);
 362		return -EINVAL;
 363	}
 364
 365	memset(null_msg, 0, sizeof(null_msg));
 366
 367	memset(param.iv, 0, sizeof(param.iv));
 368	memcpy(param.key, protkey->protkey, sizeof(param.key));
 369
 370	k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
 371		      sizeof(null_msg));
 372	if (k != sizeof(null_msg)) {
 373		DEBUG_ERR("%s protected key is not valid\n", __func__);
 374		return -EKEYREJECTED;
 375	}
 376
 377	return 0;
 378}
 379
 380/*
 381 * Transform a non-CCA key token into a protected key
 382 */
 383static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
 384			       struct pkey_protkey *protkey)
 385{
 386	int rc = -EINVAL;
 387	u8 *tmpbuf = NULL;
 388	struct keytoken_header *hdr = (struct keytoken_header *)key;
 389
 390	switch (hdr->version) {
 391	case TOKVER_PROTECTED_KEY: {
 392		struct protaeskeytoken *t;
 393
 394		if (keylen != sizeof(struct protaeskeytoken))
 395			goto out;
 396		t = (struct protaeskeytoken *)key;
 397		protkey->len = t->len;
 398		protkey->type = t->keytype;
 399		memcpy(protkey->protkey, t->protkey,
 400		       sizeof(protkey->protkey));
 401		rc = pkey_verifyprotkey(protkey);
 402		break;
 403	}
 404	case TOKVER_CLEAR_KEY: {
 405		struct clearaeskeytoken *t;
 406		struct pkey_clrkey ckey;
 407		union u_tmpbuf {
 408			u8 skey[SECKEYBLOBSIZE];
 409			u8 ep11key[MAXEP11AESKEYBLOBSIZE];
 410		};
 411		size_t tmpbuflen = sizeof(union u_tmpbuf);
 412
 413		if (keylen < sizeof(struct clearaeskeytoken))
 414			goto out;
 415		t = (struct clearaeskeytoken *)key;
 416		if (keylen != sizeof(*t) + t->len)
 417			goto out;
 418		if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16)
 419		    || (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24)
 420		    || (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
 421			memcpy(ckey.clrkey, t->clearkey, t->len);
 422		else
 423			goto out;
 424		/* alloc temp key buffer space */
 425		tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
 426		if (!tmpbuf) {
 427			rc = -ENOMEM;
 428			goto out;
 429		}
 430		/* try direct way with the PCKMO instruction */
 431		rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
 432		if (rc == 0)
 433			break;
 434		/* PCKMO failed, so try the CCA secure key way */
 
 435		rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
 436				    ckey.clrkey, tmpbuf);
 437		if (rc == 0)
 438			rc = pkey_skey2pkey(tmpbuf, protkey);
 439		if (rc == 0)
 440			break;
 441		/* if the CCA way also failed, let's try via EP11 */
 442		rc = pkey_clr2ep11key(ckey.clrkey, t->len,
 443				      tmpbuf, &tmpbuflen);
 444		if (rc == 0)
 445			rc = pkey_ep11key2pkey(tmpbuf, protkey);
 446		/* now we should really have an protected key */
 447		DEBUG_ERR("%s unable to build protected key from clear",
 448			  __func__);
 449		break;
 450	}
 451	case TOKVER_EP11_AES: {
 452		if (keylen < MINEP11AESKEYBLOBSIZE)
 453			goto out;
 454		/* check ep11 key for exportable as protected key */
 455		rc = ep11_check_aeskeyblob(debug_info, 3, key, 0, 1);
 456		if (rc)
 457			goto out;
 458		rc = pkey_ep11key2pkey(key, protkey);
 459		break;
 460	}
 
 
 
 
 
 
 
 
 461	default:
 462		DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
 463			  __func__, hdr->version);
 464		rc = -EINVAL;
 465	}
 466
 467out:
 468	kfree(tmpbuf);
 469	return rc;
 470}
 471
 472/*
 473 * Transform a CCA internal key token into a protected key
 474 */
 475static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
 476			       struct pkey_protkey *protkey)
 477{
 478	struct keytoken_header *hdr = (struct keytoken_header *)key;
 479
 480	switch (hdr->version) {
 481	case TOKVER_CCA_AES:
 482		if (keylen != sizeof(struct secaeskeytoken))
 483			return -EINVAL;
 484		break;
 485	case TOKVER_CCA_VLSC:
 486		if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
 487			return -EINVAL;
 488		break;
 489	default:
 490		DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
 491			  __func__, hdr->version);
 492		return -EINVAL;
 493	}
 494
 495	return pkey_skey2pkey(key, protkey);
 496}
 497
 498/*
 499 * Transform a key blob (of any type) into a protected key
 500 */
 501int pkey_keyblob2pkey(const u8 *key, u32 keylen,
 502		      struct pkey_protkey *protkey)
 503{
 504	int rc;
 505	struct keytoken_header *hdr = (struct keytoken_header *)key;
 506
 507	if (keylen < sizeof(struct keytoken_header)) {
 508		DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
 509		return -EINVAL;
 510	}
 511
 512	switch (hdr->type) {
 513	case TOKTYPE_NON_CCA:
 514		rc = pkey_nonccatok2pkey(key, keylen, protkey);
 515		break;
 516	case TOKTYPE_CCA_INTERNAL:
 517		rc = pkey_ccainttok2pkey(key, keylen, protkey);
 518		break;
 519	default:
 520		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
 521			  __func__, hdr->type);
 522		return -EINVAL;
 523	}
 524
 525	DEBUG_DBG("%s rc=%d\n", __func__, rc);
 526	return rc;
 527
 528}
 529EXPORT_SYMBOL(pkey_keyblob2pkey);
 530
 531static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
 532			   enum pkey_key_type ktype, enum pkey_key_size ksize,
 533			   u32 kflags, u8 *keybuf, size_t *keybufsize)
 534{
 535	int i, card, dom, rc;
 536
 537	/* check for at least one apqn given */
 538	if (!apqns || !nr_apqns)
 539		return -EINVAL;
 540
 541	/* check key type and size */
 542	switch (ktype) {
 543	case PKEY_TYPE_CCA_DATA:
 544	case PKEY_TYPE_CCA_CIPHER:
 545		if (*keybufsize < SECKEYBLOBSIZE)
 546			return -EINVAL;
 547		break;
 548	case PKEY_TYPE_EP11:
 549		if (*keybufsize < MINEP11AESKEYBLOBSIZE)
 550			return -EINVAL;
 551		break;
 552	default:
 553		return -EINVAL;
 554	}
 555	switch (ksize) {
 556	case PKEY_SIZE_AES_128:
 557	case PKEY_SIZE_AES_192:
 558	case PKEY_SIZE_AES_256:
 559		break;
 560	default:
 561		return -EINVAL;
 562	}
 563
 564	/* simple try all apqns from the list */
 565	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
 566		card = apqns[i].card;
 567		dom = apqns[i].domain;
 568		if (ktype == PKEY_TYPE_EP11) {
 569			rc = ep11_genaeskey(card, dom, ksize, kflags,
 570					    keybuf, keybufsize);
 571		} else if (ktype == PKEY_TYPE_CCA_DATA) {
 572			rc = cca_genseckey(card, dom, ksize, keybuf);
 573			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
 574		} else /* TOKVER_CCA_VLSC */
 
 575			rc = cca_gencipherkey(card, dom, ksize, kflags,
 576					      keybuf, keybufsize);
 
 577		if (rc == 0)
 578			break;
 579	}
 580
 581	return rc;
 582}
 583
 584static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
 585			    enum pkey_key_type ktype, enum pkey_key_size ksize,
 586			    u32 kflags, const u8 *clrkey,
 587			    u8 *keybuf, size_t *keybufsize)
 588{
 589	int i, card, dom, rc;
 590
 591	/* check for at least one apqn given */
 592	if (!apqns || !nr_apqns)
 593		return -EINVAL;
 594
 595	/* check key type and size */
 596	switch (ktype) {
 597	case PKEY_TYPE_CCA_DATA:
 598	case PKEY_TYPE_CCA_CIPHER:
 599		if (*keybufsize < SECKEYBLOBSIZE)
 600			return -EINVAL;
 601		break;
 602	case PKEY_TYPE_EP11:
 603		if (*keybufsize < MINEP11AESKEYBLOBSIZE)
 604			return -EINVAL;
 605		break;
 606	default:
 607		return -EINVAL;
 608	}
 609	switch (ksize) {
 610	case PKEY_SIZE_AES_128:
 611	case PKEY_SIZE_AES_192:
 612	case PKEY_SIZE_AES_256:
 613		break;
 614	default:
 615		return -EINVAL;
 616	}
 617
 
 
 618	/* simple try all apqns from the list */
 619	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
 620		card = apqns[i].card;
 621		dom = apqns[i].domain;
 622		if (ktype == PKEY_TYPE_EP11) {
 623			rc = ep11_clr2keyblob(card, dom, ksize, kflags,
 624					      clrkey, keybuf, keybufsize);
 625		} else if (ktype == PKEY_TYPE_CCA_DATA) {
 626			rc = cca_clr2seckey(card, dom, ksize,
 627					    clrkey, keybuf);
 628			*keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
 629		} else /* TOKVER_CCA_VLSC */
 
 630			rc = cca_clr2cipherkey(card, dom, ksize, kflags,
 631					       clrkey, keybuf, keybufsize);
 
 632		if (rc == 0)
 633			break;
 634	}
 635
 636	return rc;
 637}
 638
 639static int pkey_verifykey2(const u8 *key, size_t keylen,
 640			   u16 *cardnr, u16 *domain,
 641			   enum pkey_key_type *ktype,
 642			   enum pkey_key_size *ksize, u32 *flags)
 643{
 644	int rc;
 645	u32 _nr_apqns, *_apqns = NULL;
 646	struct keytoken_header *hdr = (struct keytoken_header *)key;
 647
 648	if (keylen < sizeof(struct keytoken_header))
 649		return -EINVAL;
 650
 651	if (hdr->type == TOKTYPE_CCA_INTERNAL
 652	    && hdr->version == TOKVER_CCA_AES) {
 653		struct secaeskeytoken *t = (struct secaeskeytoken *)key;
 654
 655		rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
 656		if (rc)
 657			goto out;
 658		if (ktype)
 659			*ktype = PKEY_TYPE_CCA_DATA;
 660		if (ksize)
 661			*ksize = (enum pkey_key_size) t->bitsize;
 662
 663		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
 664				   ZCRYPT_CEX3C, t->mkvp, 0, 1);
 665		if (rc == 0 && flags)
 666			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
 667		if (rc == -ENODEV) {
 668			rc = cca_findcard2(&_apqns, &_nr_apqns,
 669					   *cardnr, *domain,
 670					   ZCRYPT_CEX3C, 0, t->mkvp, 1);
 
 671			if (rc == 0 && flags)
 672				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
 673		}
 674		if (rc)
 675			goto out;
 676
 677		*cardnr = ((struct pkey_apqn *)_apqns)->card;
 678		*domain = ((struct pkey_apqn *)_apqns)->domain;
 679
 680	} else if (hdr->type == TOKTYPE_CCA_INTERNAL
 681		   && hdr->version == TOKVER_CCA_VLSC) {
 682		struct cipherkeytoken *t = (struct cipherkeytoken *)key;
 683
 684		rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
 685		if (rc)
 686			goto out;
 687		if (ktype)
 688			*ktype = PKEY_TYPE_CCA_CIPHER;
 689		if (ksize) {
 690			*ksize = PKEY_SIZE_UNKNOWN;
 691			if (!t->plfver && t->wpllen == 512)
 692				*ksize = PKEY_SIZE_AES_128;
 693			else if (!t->plfver && t->wpllen == 576)
 694				*ksize = PKEY_SIZE_AES_192;
 695			else if (!t->plfver && t->wpllen == 640)
 696				*ksize = PKEY_SIZE_AES_256;
 697		}
 698
 699		rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
 700				   ZCRYPT_CEX6, t->mkvp0, 0, 1);
 701		if (rc == 0 && flags)
 702			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
 703		if (rc == -ENODEV) {
 704			rc = cca_findcard2(&_apqns, &_nr_apqns,
 705					   *cardnr, *domain,
 706					   ZCRYPT_CEX6, 0, t->mkvp0, 1);
 
 707			if (rc == 0 && flags)
 708				*flags = PKEY_FLAGS_MATCH_ALT_MKVP;
 709		}
 710		if (rc)
 711			goto out;
 712
 713		*cardnr = ((struct pkey_apqn *)_apqns)->card;
 714		*domain = ((struct pkey_apqn *)_apqns)->domain;
 715
 716	} else if (hdr->type == TOKTYPE_NON_CCA
 717		   && hdr->version == TOKVER_EP11_AES) {
 718		struct ep11keyblob *kb = (struct ep11keyblob *)key;
 719
 720		rc = ep11_check_aeskeyblob(debug_info, 3, key, 0, 1);
 721		if (rc)
 722			goto out;
 723		if (ktype)
 724			*ktype = PKEY_TYPE_EP11;
 725		if (ksize)
 726			*ksize = kb->head.keybitlen;
 727
 728		rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
 729				    ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
 730		if (rc)
 731			goto out;
 732
 733		if (flags)
 734			*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
 735
 736		*cardnr = ((struct pkey_apqn *)_apqns)->card;
 737		*domain = ((struct pkey_apqn *)_apqns)->domain;
 738
 739	} else
 740		rc = -EINVAL;
 
 741
 742out:
 743	kfree(_apqns);
 744	return rc;
 745}
 746
 747static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
 748			      const u8 *key, size_t keylen,
 749			      struct pkey_protkey *pkey)
 750{
 751	int i, card, dom, rc;
 752	struct keytoken_header *hdr = (struct keytoken_header *)key;
 753
 754	/* check for at least one apqn given */
 755	if (!apqns || !nr_apqns)
 756		return -EINVAL;
 757
 758	if (keylen < sizeof(struct keytoken_header))
 759		return -EINVAL;
 760
 761	if (hdr->type == TOKTYPE_CCA_INTERNAL) {
 762		if (hdr->version == TOKVER_CCA_AES) {
 763			if (keylen != sizeof(struct secaeskeytoken))
 764				return -EINVAL;
 765			if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
 766				return -EINVAL;
 767		} else if (hdr->version == TOKVER_CCA_VLSC) {
 768			if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
 769				return -EINVAL;
 770			if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
 771				return -EINVAL;
 772		} else {
 773			DEBUG_ERR("%s unknown CCA internal token version %d\n",
 774				  __func__, hdr->version);
 775			return -EINVAL;
 776		}
 777	} else if (hdr->type == TOKTYPE_NON_CCA) {
 778		if (hdr->version == TOKVER_EP11_AES) {
 779			if (keylen < sizeof(struct ep11keyblob))
 780				return -EINVAL;
 781			if (ep11_check_aeskeyblob(debug_info, 3, key, 0, 1))
 782				return -EINVAL;
 783		} else {
 784			return pkey_nonccatok2pkey(key, keylen, pkey);
 785		}
 786	} else {
 787		DEBUG_ERR("%s unknown/unsupported blob type %d\n",
 788			  __func__, hdr->type);
 789		return -EINVAL;
 790	}
 791
 
 
 792	/* simple try all apqns from the list */
 793	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
 794		card = apqns[i].card;
 795		dom = apqns[i].domain;
 796		if (hdr->type == TOKTYPE_CCA_INTERNAL
 797		    && hdr->version == TOKVER_CCA_AES)
 798			rc = cca_sec2protkey(card, dom, key, pkey->protkey,
 799					     &pkey->len, &pkey->type);
 800		else if (hdr->type == TOKTYPE_CCA_INTERNAL
 801			 && hdr->version == TOKVER_CCA_VLSC)
 802			rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
 803						&pkey->len, &pkey->type);
 804		else { /* EP11 AES secure key blob */
 805			struct ep11keyblob *kb = (struct ep11keyblob *) key;
 
 806
 807			rc = ep11_key2protkey(card, dom, key, kb->head.len,
 808					      pkey->protkey, &pkey->len,
 809					      &pkey->type);
 
 810		}
 811		if (rc == 0)
 812			break;
 813	}
 814
 815	return rc;
 816}
 817
 818static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
 819			  struct pkey_apqn *apqns, size_t *nr_apqns)
 820{
 821	int rc;
 822	u32 _nr_apqns, *_apqns = NULL;
 823	struct keytoken_header *hdr = (struct keytoken_header *)key;
 824
 825	if (keylen < sizeof(struct keytoken_header) || flags == 0)
 826		return -EINVAL;
 827
 828	if (hdr->type == TOKTYPE_NON_CCA && hdr->version == TOKVER_EP11_AES) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 829		int minhwtype = 0, api = 0;
 830		struct ep11keyblob *kb = (struct ep11keyblob *) key;
 831
 832		if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
 833			return -EINVAL;
 834		if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
 835			minhwtype = ZCRYPT_CEX7;
 836			api = EP11_API_V;
 837		}
 838		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 839				    minhwtype, api, kb->wkvp);
 840		if (rc)
 841			goto out;
 842	} else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
 843		int minhwtype = ZCRYPT_CEX3C;
 844		u64 cur_mkvp = 0, old_mkvp = 0;
 845
 846		if (hdr->version == TOKVER_CCA_AES) {
 847			struct secaeskeytoken *t = (struct secaeskeytoken *)key;
 848
 849			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 850				cur_mkvp = t->mkvp;
 851			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 852				old_mkvp = t->mkvp;
 853		} else if (hdr->version == TOKVER_CCA_VLSC) {
 854			struct cipherkeytoken *t = (struct cipherkeytoken *)key;
 855
 856			minhwtype = ZCRYPT_CEX6;
 857			if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 858				cur_mkvp = t->mkvp0;
 859			if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 860				old_mkvp = t->mkvp0;
 861		} else {
 862			/* unknown cca internal token type */
 863			return -EINVAL;
 864		}
 865		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 866				   minhwtype, cur_mkvp, old_mkvp, 1);
 
 867		if (rc)
 868			goto out;
 869	} else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 870		return -EINVAL;
 
 871
 872	if (apqns) {
 873		if (*nr_apqns < _nr_apqns)
 874			rc = -ENOSPC;
 875		else
 876			memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
 877	}
 878	*nr_apqns = _nr_apqns;
 879
 880out:
 881	kfree(_apqns);
 882	return rc;
 883}
 884
 885static int pkey_apqns4keytype(enum pkey_key_type ktype,
 886			      u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
 887			      struct pkey_apqn *apqns, size_t *nr_apqns)
 888{
 889	int rc;
 890	u32 _nr_apqns, *_apqns = NULL;
 891
 
 
 892	if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
 893		u64 cur_mkvp = 0, old_mkvp = 0;
 894		int minhwtype = ZCRYPT_CEX3C;
 895
 896		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 897			cur_mkvp = *((u64 *) cur_mkvp);
 898		if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
 899			old_mkvp = *((u64 *) alt_mkvp);
 900		if (ktype == PKEY_TYPE_CCA_CIPHER)
 901			minhwtype = ZCRYPT_CEX6;
 902		rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 903				   minhwtype, cur_mkvp, old_mkvp, 1);
 
 904		if (rc)
 905			goto out;
 906	} else if (ktype == PKEY_TYPE_EP11) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 907		u8 *wkvp = NULL;
 908
 909		if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
 910			wkvp = cur_mkvp;
 911		rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
 912				    ZCRYPT_CEX7, EP11_API_V, wkvp);
 913		if (rc)
 914			goto out;
 915
 916	} else
 917		return -EINVAL;
 
 918
 919	if (apqns) {
 920		if (*nr_apqns < _nr_apqns)
 921			rc = -ENOSPC;
 922		else
 923			memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
 924	}
 925	*nr_apqns = _nr_apqns;
 926
 927out:
 928	kfree(_apqns);
 929	return rc;
 930}
 931
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 932/*
 933 * File io functions
 934 */
 935
 936static void *_copy_key_from_user(void __user *ukey, size_t keylen)
 937{
 938	if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
 939		return ERR_PTR(-EINVAL);
 940
 941	return memdup_user(ukey, keylen);
 942}
 943
 944static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
 945{
 946	if (!uapqns || nr_apqns == 0)
 947		return NULL;
 948
 949	return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
 950}
 951
 952static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
 953				unsigned long arg)
 954{
 955	int rc;
 956
 957	switch (cmd) {
 958	case PKEY_GENSECK: {
 959		struct pkey_genseck __user *ugs = (void __user *) arg;
 960		struct pkey_genseck kgs;
 961
 962		if (copy_from_user(&kgs, ugs, sizeof(kgs)))
 963			return -EFAULT;
 964		rc = cca_genseckey(kgs.cardnr, kgs.domain,
 965				   kgs.keytype, kgs.seckey.seckey);
 966		DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
 967		if (rc)
 968			break;
 969		if (copy_to_user(ugs, &kgs, sizeof(kgs)))
 970			return -EFAULT;
 971		break;
 972	}
 973	case PKEY_CLR2SECK: {
 974		struct pkey_clr2seck __user *ucs = (void __user *) arg;
 975		struct pkey_clr2seck kcs;
 976
 977		if (copy_from_user(&kcs, ucs, sizeof(kcs)))
 978			return -EFAULT;
 979		rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
 980				    kcs.clrkey.clrkey, kcs.seckey.seckey);
 981		DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
 982		if (rc)
 983			break;
 984		if (copy_to_user(ucs, &kcs, sizeof(kcs)))
 985			return -EFAULT;
 986		memzero_explicit(&kcs, sizeof(kcs));
 987		break;
 988	}
 989	case PKEY_SEC2PROTK: {
 990		struct pkey_sec2protk __user *usp = (void __user *) arg;
 991		struct pkey_sec2protk ksp;
 992
 993		if (copy_from_user(&ksp, usp, sizeof(ksp)))
 994			return -EFAULT;
 995		rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
 996				     ksp.seckey.seckey, ksp.protkey.protkey,
 997				     &ksp.protkey.len, &ksp.protkey.type);
 998		DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
 999		if (rc)
1000			break;
1001		if (copy_to_user(usp, &ksp, sizeof(ksp)))
1002			return -EFAULT;
1003		break;
1004	}
1005	case PKEY_CLR2PROTK: {
1006		struct pkey_clr2protk __user *ucp = (void __user *) arg;
1007		struct pkey_clr2protk kcp;
1008
1009		if (copy_from_user(&kcp, ucp, sizeof(kcp)))
1010			return -EFAULT;
1011		rc = pkey_clr2protkey(kcp.keytype,
1012				      &kcp.clrkey, &kcp.protkey);
1013		DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
1014		if (rc)
1015			break;
1016		if (copy_to_user(ucp, &kcp, sizeof(kcp)))
1017			return -EFAULT;
1018		memzero_explicit(&kcp, sizeof(kcp));
1019		break;
1020	}
1021	case PKEY_FINDCARD: {
1022		struct pkey_findcard __user *ufc = (void __user *) arg;
1023		struct pkey_findcard kfc;
1024
1025		if (copy_from_user(&kfc, ufc, sizeof(kfc)))
1026			return -EFAULT;
1027		rc = cca_findcard(kfc.seckey.seckey,
1028				  &kfc.cardnr, &kfc.domain, 1);
1029		DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
1030		if (rc < 0)
1031			break;
1032		if (copy_to_user(ufc, &kfc, sizeof(kfc)))
1033			return -EFAULT;
1034		break;
1035	}
1036	case PKEY_SKEY2PKEY: {
1037		struct pkey_skey2pkey __user *usp = (void __user *) arg;
1038		struct pkey_skey2pkey ksp;
1039
1040		if (copy_from_user(&ksp, usp, sizeof(ksp)))
1041			return -EFAULT;
1042		rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
1043		DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
1044		if (rc)
1045			break;
1046		if (copy_to_user(usp, &ksp, sizeof(ksp)))
1047			return -EFAULT;
1048		break;
1049	}
1050	case PKEY_VERIFYKEY: {
1051		struct pkey_verifykey __user *uvk = (void __user *) arg;
1052		struct pkey_verifykey kvk;
1053
1054		if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1055			return -EFAULT;
1056		rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
1057				    &kvk.keysize, &kvk.attributes);
1058		DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
1059		if (rc)
1060			break;
1061		if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1062			return -EFAULT;
1063		break;
1064	}
1065	case PKEY_GENPROTK: {
1066		struct pkey_genprotk __user *ugp = (void __user *) arg;
1067		struct pkey_genprotk kgp;
1068
1069		if (copy_from_user(&kgp, ugp, sizeof(kgp)))
1070			return -EFAULT;
1071		rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
1072		DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
1073		if (rc)
1074			break;
1075		if (copy_to_user(ugp, &kgp, sizeof(kgp)))
1076			return -EFAULT;
1077		break;
1078	}
1079	case PKEY_VERIFYPROTK: {
1080		struct pkey_verifyprotk __user *uvp = (void __user *) arg;
1081		struct pkey_verifyprotk kvp;
1082
1083		if (copy_from_user(&kvp, uvp, sizeof(kvp)))
1084			return -EFAULT;
1085		rc = pkey_verifyprotkey(&kvp.protkey);
1086		DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
1087		break;
1088	}
1089	case PKEY_KBLOB2PROTK: {
1090		struct pkey_kblob2pkey __user *utp = (void __user *) arg;
1091		struct pkey_kblob2pkey ktp;
1092		u8 *kkey;
1093
1094		if (copy_from_user(&ktp, utp, sizeof(ktp)))
1095			return -EFAULT;
1096		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1097		if (IS_ERR(kkey))
1098			return PTR_ERR(kkey);
1099		rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
1100		DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
1101		kfree(kkey);
1102		if (rc)
1103			break;
1104		if (copy_to_user(utp, &ktp, sizeof(ktp)))
1105			return -EFAULT;
1106		break;
1107	}
1108	case PKEY_GENSECK2: {
1109		struct pkey_genseck2 __user *ugs = (void __user *) arg;
1110		struct pkey_genseck2 kgs;
1111		struct pkey_apqn *apqns;
1112		size_t klen = KEYBLOBBUFSIZE;
1113		u8 *kkey;
1114
1115		if (copy_from_user(&kgs, ugs, sizeof(kgs)))
1116			return -EFAULT;
1117		apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
1118		if (IS_ERR(apqns))
1119			return PTR_ERR(apqns);
1120		kkey = kmalloc(klen, GFP_KERNEL);
1121		if (!kkey) {
1122			kfree(apqns);
1123			return -ENOMEM;
1124		}
1125		rc = pkey_genseckey2(apqns, kgs.apqn_entries,
1126				     kgs.type, kgs.size, kgs.keygenflags,
1127				     kkey, &klen);
1128		DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
1129		kfree(apqns);
1130		if (rc) {
1131			kfree(kkey);
1132			break;
1133		}
1134		if (kgs.key) {
1135			if (kgs.keylen < klen) {
1136				kfree(kkey);
1137				return -EINVAL;
1138			}
1139			if (copy_to_user(kgs.key, kkey, klen)) {
1140				kfree(kkey);
1141				return -EFAULT;
1142			}
1143		}
1144		kgs.keylen = klen;
1145		if (copy_to_user(ugs, &kgs, sizeof(kgs)))
1146			rc = -EFAULT;
1147		kfree(kkey);
1148		break;
1149	}
1150	case PKEY_CLR2SECK2: {
1151		struct pkey_clr2seck2 __user *ucs = (void __user *) arg;
1152		struct pkey_clr2seck2 kcs;
1153		struct pkey_apqn *apqns;
1154		size_t klen = KEYBLOBBUFSIZE;
1155		u8 *kkey;
1156
1157		if (copy_from_user(&kcs, ucs, sizeof(kcs)))
1158			return -EFAULT;
1159		apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
1160		if (IS_ERR(apqns))
1161			return PTR_ERR(apqns);
1162		kkey = kmalloc(klen, GFP_KERNEL);
1163		if (!kkey) {
1164			kfree(apqns);
1165			return -ENOMEM;
1166		}
1167		rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
1168				      kcs.type, kcs.size, kcs.keygenflags,
1169				      kcs.clrkey.clrkey, kkey, &klen);
1170		DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
1171		kfree(apqns);
1172		if (rc) {
1173			kfree(kkey);
1174			break;
1175		}
1176		if (kcs.key) {
1177			if (kcs.keylen < klen) {
1178				kfree(kkey);
1179				return -EINVAL;
1180			}
1181			if (copy_to_user(kcs.key, kkey, klen)) {
1182				kfree(kkey);
1183				return -EFAULT;
1184			}
1185		}
1186		kcs.keylen = klen;
1187		if (copy_to_user(ucs, &kcs, sizeof(kcs)))
1188			rc = -EFAULT;
1189		memzero_explicit(&kcs, sizeof(kcs));
1190		kfree(kkey);
1191		break;
1192	}
1193	case PKEY_VERIFYKEY2: {
1194		struct pkey_verifykey2 __user *uvk = (void __user *) arg;
1195		struct pkey_verifykey2 kvk;
1196		u8 *kkey;
1197
1198		if (copy_from_user(&kvk, uvk, sizeof(kvk)))
1199			return -EFAULT;
1200		kkey = _copy_key_from_user(kvk.key, kvk.keylen);
1201		if (IS_ERR(kkey))
1202			return PTR_ERR(kkey);
1203		rc = pkey_verifykey2(kkey, kvk.keylen,
1204				     &kvk.cardnr, &kvk.domain,
1205				     &kvk.type, &kvk.size, &kvk.flags);
1206		DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
1207		kfree(kkey);
1208		if (rc)
1209			break;
1210		if (copy_to_user(uvk, &kvk, sizeof(kvk)))
1211			return -EFAULT;
1212		break;
1213	}
1214	case PKEY_KBLOB2PROTK2: {
1215		struct pkey_kblob2pkey2 __user *utp = (void __user *) arg;
1216		struct pkey_kblob2pkey2 ktp;
1217		struct pkey_apqn *apqns = NULL;
1218		u8 *kkey;
1219
1220		if (copy_from_user(&ktp, utp, sizeof(ktp)))
1221			return -EFAULT;
1222		apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
1223		if (IS_ERR(apqns))
1224			return PTR_ERR(apqns);
1225		kkey = _copy_key_from_user(ktp.key, ktp.keylen);
1226		if (IS_ERR(kkey)) {
1227			kfree(apqns);
1228			return PTR_ERR(kkey);
1229		}
1230		rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
1231					kkey, ktp.keylen, &ktp.protkey);
1232		DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
1233		kfree(apqns);
1234		kfree(kkey);
1235		if (rc)
1236			break;
1237		if (copy_to_user(utp, &ktp, sizeof(ktp)))
1238			return -EFAULT;
1239		break;
1240	}
1241	case PKEY_APQNS4K: {
1242		struct pkey_apqns4key __user *uak = (void __user *) arg;
1243		struct pkey_apqns4key kak;
1244		struct pkey_apqn *apqns = NULL;
1245		size_t nr_apqns, len;
1246		u8 *kkey;
1247
1248		if (copy_from_user(&kak, uak, sizeof(kak)))
1249			return -EFAULT;
1250		nr_apqns = kak.apqn_entries;
1251		if (nr_apqns) {
1252			apqns = kmalloc_array(nr_apqns,
1253					      sizeof(struct pkey_apqn),
1254					      GFP_KERNEL);
1255			if (!apqns)
1256				return -ENOMEM;
1257		}
1258		kkey = _copy_key_from_user(kak.key, kak.keylen);
1259		if (IS_ERR(kkey)) {
1260			kfree(apqns);
1261			return PTR_ERR(kkey);
1262		}
1263		rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
1264				    apqns, &nr_apqns);
1265		DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
1266		kfree(kkey);
1267		if (rc && rc != -ENOSPC) {
1268			kfree(apqns);
1269			break;
1270		}
1271		if (!rc && kak.apqns) {
1272			if (nr_apqns > kak.apqn_entries) {
1273				kfree(apqns);
1274				return -EINVAL;
1275			}
1276			len = nr_apqns * sizeof(struct pkey_apqn);
1277			if (len) {
1278				if (copy_to_user(kak.apqns, apqns, len)) {
1279					kfree(apqns);
1280					return -EFAULT;
1281				}
1282			}
1283		}
1284		kak.apqn_entries = nr_apqns;
1285		if (copy_to_user(uak, &kak, sizeof(kak)))
1286			rc = -EFAULT;
1287		kfree(apqns);
1288		break;
1289	}
1290	case PKEY_APQNS4KT: {
1291		struct pkey_apqns4keytype __user *uat = (void __user *) arg;
1292		struct pkey_apqns4keytype kat;
1293		struct pkey_apqn *apqns = NULL;
1294		size_t nr_apqns, len;
1295
1296		if (copy_from_user(&kat, uat, sizeof(kat)))
1297			return -EFAULT;
1298		nr_apqns = kat.apqn_entries;
1299		if (nr_apqns) {
1300			apqns = kmalloc_array(nr_apqns,
1301					      sizeof(struct pkey_apqn),
1302					      GFP_KERNEL);
1303			if (!apqns)
1304				return -ENOMEM;
1305		}
1306		rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
1307					kat.flags, apqns, &nr_apqns);
1308		DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
1309		if (rc && rc != -ENOSPC) {
1310			kfree(apqns);
1311			break;
1312		}
1313		if (!rc && kat.apqns) {
1314			if (nr_apqns > kat.apqn_entries) {
1315				kfree(apqns);
1316				return -EINVAL;
1317			}
1318			len = nr_apqns * sizeof(struct pkey_apqn);
1319			if (len) {
1320				if (copy_to_user(kat.apqns, apqns, len)) {
1321					kfree(apqns);
1322					return -EFAULT;
1323				}
1324			}
1325		}
1326		kat.apqn_entries = nr_apqns;
1327		if (copy_to_user(uat, &kat, sizeof(kat)))
1328			rc = -EFAULT;
1329		kfree(apqns);
1330		break;
1331	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1332	default:
1333		/* unknown/unsupported ioctl cmd */
1334		return -ENOTTY;
1335	}
1336
1337	return rc;
1338}
1339
1340/*
1341 * Sysfs and file io operations
1342 */
1343
1344/*
1345 * Sysfs attribute read function for all protected key binary attributes.
1346 * The implementation can not deal with partial reads, because a new random
1347 * protected key blob is generated with each read. In case of partial reads
1348 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1349 */
1350static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1351					  loff_t off, size_t count)
1352{
1353	struct protaeskeytoken protkeytoken;
1354	struct pkey_protkey protkey;
1355	int rc;
1356
1357	if (off != 0 || count < sizeof(protkeytoken))
1358		return -EINVAL;
1359	if (is_xts)
1360		if (count < 2 * sizeof(protkeytoken))
1361			return -EINVAL;
1362
1363	memset(&protkeytoken, 0, sizeof(protkeytoken));
1364	protkeytoken.type = TOKTYPE_NON_CCA;
1365	protkeytoken.version = TOKVER_PROTECTED_KEY;
1366	protkeytoken.keytype = keytype;
1367
1368	rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1369	if (rc)
1370		return rc;
1371
1372	protkeytoken.len = protkey.len;
1373	memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1374
1375	memcpy(buf, &protkeytoken, sizeof(protkeytoken));
1376
1377	if (is_xts) {
1378		rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
1379		if (rc)
1380			return rc;
1381
1382		protkeytoken.len = protkey.len;
1383		memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
1384
1385		memcpy(buf + sizeof(protkeytoken), &protkeytoken,
1386		       sizeof(protkeytoken));
1387
1388		return 2 * sizeof(protkeytoken);
1389	}
1390
1391	return sizeof(protkeytoken);
1392}
1393
1394static ssize_t protkey_aes_128_read(struct file *filp,
1395				    struct kobject *kobj,
1396				    struct bin_attribute *attr,
1397				    char *buf, loff_t off,
1398				    size_t count)
1399{
1400	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1401					  off, count);
1402}
1403
1404static ssize_t protkey_aes_192_read(struct file *filp,
1405				    struct kobject *kobj,
1406				    struct bin_attribute *attr,
1407				    char *buf, loff_t off,
1408				    size_t count)
1409{
1410	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1411					  off, count);
1412}
1413
1414static ssize_t protkey_aes_256_read(struct file *filp,
1415				    struct kobject *kobj,
1416				    struct bin_attribute *attr,
1417				    char *buf, loff_t off,
1418				    size_t count)
1419{
1420	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1421					  off, count);
1422}
1423
1424static ssize_t protkey_aes_128_xts_read(struct file *filp,
1425					struct kobject *kobj,
1426					struct bin_attribute *attr,
1427					char *buf, loff_t off,
1428					size_t count)
1429{
1430	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1431					  off, count);
1432}
1433
1434static ssize_t protkey_aes_256_xts_read(struct file *filp,
1435					struct kobject *kobj,
1436					struct bin_attribute *attr,
1437					char *buf, loff_t off,
1438					size_t count)
1439{
1440	return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1441					  off, count);
1442}
1443
1444static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
1445static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
1446static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
1447static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
1448static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
1449
1450static struct bin_attribute *protkey_attrs[] = {
1451	&bin_attr_protkey_aes_128,
1452	&bin_attr_protkey_aes_192,
1453	&bin_attr_protkey_aes_256,
1454	&bin_attr_protkey_aes_128_xts,
1455	&bin_attr_protkey_aes_256_xts,
1456	NULL
1457};
1458
1459static struct attribute_group protkey_attr_group = {
1460	.name	   = "protkey",
1461	.bin_attrs = protkey_attrs,
1462};
1463
1464/*
1465 * Sysfs attribute read function for all secure key ccadata binary attributes.
1466 * The implementation can not deal with partial reads, because a new random
1467 * protected key blob is generated with each read. In case of partial reads
1468 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1469 */
1470static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
1471					  loff_t off, size_t count)
1472{
1473	int rc;
1474	struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
1475
1476	if (off != 0 || count < sizeof(struct secaeskeytoken))
1477		return -EINVAL;
1478	if (is_xts)
1479		if (count < 2 * sizeof(struct secaeskeytoken))
1480			return -EINVAL;
1481
1482	rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1483	if (rc)
1484		return rc;
1485
1486	if (is_xts) {
1487		seckey++;
1488		rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
1489		if (rc)
1490			return rc;
1491
1492		return 2 * sizeof(struct secaeskeytoken);
1493	}
1494
1495	return sizeof(struct secaeskeytoken);
1496}
1497
1498static ssize_t ccadata_aes_128_read(struct file *filp,
1499				    struct kobject *kobj,
1500				    struct bin_attribute *attr,
1501				    char *buf, loff_t off,
1502				    size_t count)
1503{
1504	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
1505					  off, count);
1506}
1507
1508static ssize_t ccadata_aes_192_read(struct file *filp,
1509				    struct kobject *kobj,
1510				    struct bin_attribute *attr,
1511				    char *buf, loff_t off,
1512				    size_t count)
1513{
1514	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
1515					  off, count);
1516}
1517
1518static ssize_t ccadata_aes_256_read(struct file *filp,
1519				    struct kobject *kobj,
1520				    struct bin_attribute *attr,
1521				    char *buf, loff_t off,
1522				    size_t count)
1523{
1524	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
1525					  off, count);
1526}
1527
1528static ssize_t ccadata_aes_128_xts_read(struct file *filp,
1529					struct kobject *kobj,
1530					struct bin_attribute *attr,
1531					char *buf, loff_t off,
1532					size_t count)
1533{
1534	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
1535					  off, count);
1536}
1537
1538static ssize_t ccadata_aes_256_xts_read(struct file *filp,
1539					struct kobject *kobj,
1540					struct bin_attribute *attr,
1541					char *buf, loff_t off,
1542					size_t count)
1543{
1544	return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
1545					  off, count);
1546}
1547
1548static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
1549static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
1550static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
1551static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
1552static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
1553
1554static struct bin_attribute *ccadata_attrs[] = {
1555	&bin_attr_ccadata_aes_128,
1556	&bin_attr_ccadata_aes_192,
1557	&bin_attr_ccadata_aes_256,
1558	&bin_attr_ccadata_aes_128_xts,
1559	&bin_attr_ccadata_aes_256_xts,
1560	NULL
1561};
1562
1563static struct attribute_group ccadata_attr_group = {
1564	.name	   = "ccadata",
1565	.bin_attrs = ccadata_attrs,
1566};
1567
1568#define CCACIPHERTOKENSIZE	(sizeof(struct cipherkeytoken) + 80)
1569
1570/*
1571 * Sysfs attribute read function for all secure key ccacipher binary attributes.
1572 * The implementation can not deal with partial reads, because a new random
1573 * secure key blob is generated with each read. In case of partial reads
1574 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1575 */
1576static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
1577					    bool is_xts, char *buf, loff_t off,
1578					    size_t count)
1579{
1580	int i, rc, card, dom;
1581	u32 nr_apqns, *apqns = NULL;
1582	size_t keysize = CCACIPHERTOKENSIZE;
1583
1584	if (off != 0 || count < CCACIPHERTOKENSIZE)
1585		return -EINVAL;
1586	if (is_xts)
1587		if (count < 2 * CCACIPHERTOKENSIZE)
1588			return -EINVAL;
1589
1590	/* build a list of apqns able to generate an cipher key */
1591	rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1592			   ZCRYPT_CEX6, 0, 0, 0);
1593	if (rc)
1594		return rc;
1595
1596	memset(buf, 0, is_xts ? 2 * keysize : keysize);
1597
1598	/* simple try all apqns from the list */
1599	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1600		card = apqns[i] >> 16;
1601		dom = apqns[i] & 0xFFFF;
1602		rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1603		if (rc == 0)
1604			break;
1605	}
1606	if (rc)
1607		return rc;
1608
1609	if (is_xts) {
1610		keysize = CCACIPHERTOKENSIZE;
1611		buf += CCACIPHERTOKENSIZE;
1612		rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
1613		if (rc == 0)
1614			return 2 * CCACIPHERTOKENSIZE;
1615	}
1616
1617	return CCACIPHERTOKENSIZE;
1618}
1619
1620static ssize_t ccacipher_aes_128_read(struct file *filp,
1621				      struct kobject *kobj,
1622				      struct bin_attribute *attr,
1623				      char *buf, loff_t off,
1624				      size_t count)
1625{
1626	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1627					    off, count);
1628}
1629
1630static ssize_t ccacipher_aes_192_read(struct file *filp,
1631				      struct kobject *kobj,
1632				      struct bin_attribute *attr,
1633				      char *buf, loff_t off,
1634				      size_t count)
1635{
1636	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1637					    off, count);
1638}
1639
1640static ssize_t ccacipher_aes_256_read(struct file *filp,
1641				      struct kobject *kobj,
1642				      struct bin_attribute *attr,
1643				      char *buf, loff_t off,
1644				      size_t count)
1645{
1646	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1647					    off, count);
1648}
1649
1650static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
1651					  struct kobject *kobj,
1652					  struct bin_attribute *attr,
1653					  char *buf, loff_t off,
1654					  size_t count)
1655{
1656	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1657					    off, count);
1658}
1659
1660static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
1661					  struct kobject *kobj,
1662					  struct bin_attribute *attr,
1663					  char *buf, loff_t off,
1664					  size_t count)
1665{
1666	return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1667					    off, count);
1668}
1669
1670static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
1671static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
1672static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
1673static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
1674static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
1675
1676static struct bin_attribute *ccacipher_attrs[] = {
1677	&bin_attr_ccacipher_aes_128,
1678	&bin_attr_ccacipher_aes_192,
1679	&bin_attr_ccacipher_aes_256,
1680	&bin_attr_ccacipher_aes_128_xts,
1681	&bin_attr_ccacipher_aes_256_xts,
1682	NULL
1683};
1684
1685static struct attribute_group ccacipher_attr_group = {
1686	.name	   = "ccacipher",
1687	.bin_attrs = ccacipher_attrs,
1688};
1689
1690/*
1691 * Sysfs attribute read function for all ep11 aes key binary attributes.
1692 * The implementation can not deal with partial reads, because a new random
1693 * secure key blob is generated with each read. In case of partial reads
1694 * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
1695 * This function and the sysfs attributes using it provide EP11 key blobs
1696 * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
1697 * 320 bytes.
1698 */
1699static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
1700				       bool is_xts, char *buf, loff_t off,
1701				       size_t count)
1702{
1703	int i, rc, card, dom;
1704	u32 nr_apqns, *apqns = NULL;
1705	size_t keysize = MAXEP11AESKEYBLOBSIZE;
1706
1707	if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
1708		return -EINVAL;
1709	if (is_xts)
1710		if (count < 2 * MAXEP11AESKEYBLOBSIZE)
1711			return -EINVAL;
1712
1713	/* build a list of apqns able to generate an cipher key */
1714	rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
1715			    ZCRYPT_CEX7, EP11_API_V, NULL);
1716	if (rc)
1717		return rc;
1718
1719	memset(buf, 0, is_xts ? 2 * keysize : keysize);
1720
1721	/* simple try all apqns from the list */
1722	for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
1723		card = apqns[i] >> 16;
1724		dom = apqns[i] & 0xFFFF;
1725		rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1726		if (rc == 0)
1727			break;
1728	}
1729	if (rc)
1730		return rc;
1731
1732	if (is_xts) {
1733		keysize = MAXEP11AESKEYBLOBSIZE;
1734		buf += MAXEP11AESKEYBLOBSIZE;
1735		rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize);
1736		if (rc == 0)
1737			return 2 * MAXEP11AESKEYBLOBSIZE;
1738	}
1739
1740	return MAXEP11AESKEYBLOBSIZE;
1741}
1742
1743static ssize_t ep11_aes_128_read(struct file *filp,
1744				 struct kobject *kobj,
1745				 struct bin_attribute *attr,
1746				 char *buf, loff_t off,
1747				 size_t count)
1748{
1749	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
1750				       off, count);
1751}
1752
1753static ssize_t ep11_aes_192_read(struct file *filp,
1754				 struct kobject *kobj,
1755				 struct bin_attribute *attr,
1756				 char *buf, loff_t off,
1757				 size_t count)
1758{
1759	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
1760				       off, count);
1761}
1762
1763static ssize_t ep11_aes_256_read(struct file *filp,
1764				 struct kobject *kobj,
1765				 struct bin_attribute *attr,
1766				 char *buf, loff_t off,
1767				 size_t count)
1768{
1769	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
1770				       off, count);
1771}
1772
1773static ssize_t ep11_aes_128_xts_read(struct file *filp,
1774				     struct kobject *kobj,
1775				     struct bin_attribute *attr,
1776				     char *buf, loff_t off,
1777				     size_t count)
1778{
1779	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
1780				       off, count);
1781}
1782
1783static ssize_t ep11_aes_256_xts_read(struct file *filp,
1784				     struct kobject *kobj,
1785				     struct bin_attribute *attr,
1786				     char *buf, loff_t off,
1787				     size_t count)
1788{
1789	return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
1790				       off, count);
1791}
1792
1793static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
1794static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
1795static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
1796static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
1797static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
1798
1799static struct bin_attribute *ep11_attrs[] = {
1800	&bin_attr_ep11_aes_128,
1801	&bin_attr_ep11_aes_192,
1802	&bin_attr_ep11_aes_256,
1803	&bin_attr_ep11_aes_128_xts,
1804	&bin_attr_ep11_aes_256_xts,
1805	NULL
1806};
1807
1808static struct attribute_group ep11_attr_group = {
1809	.name	   = "ep11",
1810	.bin_attrs = ep11_attrs,
1811};
1812
1813static const struct attribute_group *pkey_attr_groups[] = {
1814	&protkey_attr_group,
1815	&ccadata_attr_group,
1816	&ccacipher_attr_group,
1817	&ep11_attr_group,
1818	NULL,
1819};
1820
1821static const struct file_operations pkey_fops = {
1822	.owner		= THIS_MODULE,
1823	.open		= nonseekable_open,
1824	.llseek		= no_llseek,
1825	.unlocked_ioctl = pkey_unlocked_ioctl,
1826};
1827
1828static struct miscdevice pkey_dev = {
1829	.name	= "pkey",
1830	.minor	= MISC_DYNAMIC_MINOR,
1831	.mode	= 0666,
1832	.fops	= &pkey_fops,
1833	.groups = pkey_attr_groups,
1834};
1835
1836/*
1837 * Module init
1838 */
1839static int __init pkey_init(void)
1840{
1841	cpacf_mask_t kmc_functions;
1842
1843	/*
1844	 * The pckmo instruction should be available - even if we don't
1845	 * actually invoke it. This instruction comes with MSA 3 which
1846	 * is also the minimum level for the kmc instructions which
1847	 * are able to work with protected keys.
1848	 */
1849	if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
1850		return -ENODEV;
1851
1852	/* check for kmc instructions available */
1853	if (!cpacf_query(CPACF_KMC, &kmc_functions))
1854		return -ENODEV;
1855	if (!cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
1856	    !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
1857	    !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256))
1858		return -ENODEV;
1859
1860	pkey_debug_init();
1861
1862	return misc_register(&pkey_dev);
1863}
1864
1865/*
1866 * Module exit
1867 */
1868static void __exit pkey_exit(void)
1869{
1870	misc_deregister(&pkey_dev);
1871	pkey_debug_exit();
1872}
1873
1874module_cpu_feature_match(MSA, pkey_init);
1875module_exit(pkey_exit);