162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Userspace interface to the pkey device driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright IBM Corp. 2017, 2023
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Author: Harald Freudenberger <freude@de.ibm.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef _UAPI_PKEY_H
1262306a36Sopenharmony_ci#define _UAPI_PKEY_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/ioctl.h>
1562306a36Sopenharmony_ci#include <linux/types.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*
1862306a36Sopenharmony_ci * Ioctl calls supported by the pkey device driver
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci#define PKEY_IOCTL_MAGIC 'p'
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define SECKEYBLOBSIZE	64	   /* secure key blob size is always 64 bytes */
2462306a36Sopenharmony_ci#define PROTKEYBLOBSIZE 80	/* protected key blob size is always 80 bytes */
2562306a36Sopenharmony_ci#define MAXPROTKEYSIZE	64	/* a protected key blob may be up to 64 bytes */
2662306a36Sopenharmony_ci#define MAXCLRKEYSIZE	32	   /* a clear key value may be up to 32 bytes */
2762306a36Sopenharmony_ci#define MAXAESCIPHERKEYSIZE 136  /* our aes cipher keys have always 136 bytes */
2862306a36Sopenharmony_ci#define MINEP11AESKEYBLOBSIZE 256  /* min EP11 AES key blob size  */
2962306a36Sopenharmony_ci#define MAXEP11AESKEYBLOBSIZE 336  /* max EP11 AES key blob size */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* Minimum size of a key blob */
3262306a36Sopenharmony_ci#define MINKEYBLOBSIZE	SECKEYBLOBSIZE
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* defines for the type field within the pkey_protkey struct */
3562306a36Sopenharmony_ci#define PKEY_KEYTYPE_AES_128		1
3662306a36Sopenharmony_ci#define PKEY_KEYTYPE_AES_192		2
3762306a36Sopenharmony_ci#define PKEY_KEYTYPE_AES_256		3
3862306a36Sopenharmony_ci#define PKEY_KEYTYPE_ECC		4
3962306a36Sopenharmony_ci#define PKEY_KEYTYPE_ECC_P256		5
4062306a36Sopenharmony_ci#define PKEY_KEYTYPE_ECC_P384		6
4162306a36Sopenharmony_ci#define PKEY_KEYTYPE_ECC_P521		7
4262306a36Sopenharmony_ci#define PKEY_KEYTYPE_ECC_ED25519	8
4362306a36Sopenharmony_ci#define PKEY_KEYTYPE_ECC_ED448		9
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* the newer ioctls use a pkey_key_type enum for type information */
4662306a36Sopenharmony_cienum pkey_key_type {
4762306a36Sopenharmony_ci	PKEY_TYPE_CCA_DATA   = (__u32) 1,
4862306a36Sopenharmony_ci	PKEY_TYPE_CCA_CIPHER = (__u32) 2,
4962306a36Sopenharmony_ci	PKEY_TYPE_EP11	     = (__u32) 3,
5062306a36Sopenharmony_ci	PKEY_TYPE_CCA_ECC    = (__u32) 0x1f,
5162306a36Sopenharmony_ci	PKEY_TYPE_EP11_AES   = (__u32) 6,
5262306a36Sopenharmony_ci	PKEY_TYPE_EP11_ECC   = (__u32) 7,
5362306a36Sopenharmony_ci};
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci/* the newer ioctls use a pkey_key_size enum for key size information */
5662306a36Sopenharmony_cienum pkey_key_size {
5762306a36Sopenharmony_ci	PKEY_SIZE_AES_128 = (__u32) 128,
5862306a36Sopenharmony_ci	PKEY_SIZE_AES_192 = (__u32) 192,
5962306a36Sopenharmony_ci	PKEY_SIZE_AES_256 = (__u32) 256,
6062306a36Sopenharmony_ci	PKEY_SIZE_UNKNOWN = (__u32) 0xFFFFFFFF,
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* some of the newer ioctls use these flags */
6462306a36Sopenharmony_ci#define PKEY_FLAGS_MATCH_CUR_MKVP  0x00000002
6562306a36Sopenharmony_ci#define PKEY_FLAGS_MATCH_ALT_MKVP  0x00000004
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* keygenflags defines for CCA AES cipher keys */
6862306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_SYM  0x00008000
6962306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_UASY 0x00004000
7062306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_AASY 0x00002000
7162306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_RAW  0x00001000
7262306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_CPAC 0x00000800
7362306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_DES  0x00000080
7462306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_AES  0x00000040
7562306a36Sopenharmony_ci#define PKEY_KEYGEN_XPRT_RSA  0x00000008
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/* Struct to hold apqn target info (card/domain pair) */
7862306a36Sopenharmony_cistruct pkey_apqn {
7962306a36Sopenharmony_ci	__u16 card;
8062306a36Sopenharmony_ci	__u16 domain;
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/* Struct to hold a CCA AES secure key blob */
8462306a36Sopenharmony_cistruct pkey_seckey {
8562306a36Sopenharmony_ci	__u8  seckey[SECKEYBLOBSIZE];		  /* the secure key blob */
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/* Struct to hold protected key and length info */
8962306a36Sopenharmony_cistruct pkey_protkey {
9062306a36Sopenharmony_ci	__u32 type;	 /* key type, one of the PKEY_KEYTYPE_AES values */
9162306a36Sopenharmony_ci	__u32 len;		/* bytes actually stored in protkey[]	 */
9262306a36Sopenharmony_ci	__u8  protkey[MAXPROTKEYSIZE];	       /* the protected key blob */
9362306a36Sopenharmony_ci};
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/* Struct to hold an AES clear key value */
9662306a36Sopenharmony_cistruct pkey_clrkey {
9762306a36Sopenharmony_ci	__u8  clrkey[MAXCLRKEYSIZE]; /* 16, 24, or 32 byte clear key value */
9862306a36Sopenharmony_ci};
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/*
10162306a36Sopenharmony_ci * EP11 key blobs of type PKEY_TYPE_EP11_AES and PKEY_TYPE_EP11_ECC
10262306a36Sopenharmony_ci * are ep11 blobs prepended by this header:
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_cistruct ep11kblob_header {
10562306a36Sopenharmony_ci	__u8  type;	/* always 0x00 */
10662306a36Sopenharmony_ci	__u8  hver;	/* header version,  currently needs to be 0x00 */
10762306a36Sopenharmony_ci	__u16 len;	/* total length in bytes (including this header) */
10862306a36Sopenharmony_ci	__u8  version;	/* PKEY_TYPE_EP11_AES or PKEY_TYPE_EP11_ECC */
10962306a36Sopenharmony_ci	__u8  res0;	/* unused */
11062306a36Sopenharmony_ci	__u16 bitlen;	/* clear key bit len, 0 for unknown */
11162306a36Sopenharmony_ci	__u8  res1[8];	/* unused */
11262306a36Sopenharmony_ci} __packed;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/*
11562306a36Sopenharmony_ci * Generate CCA AES secure key.
11662306a36Sopenharmony_ci */
11762306a36Sopenharmony_cistruct pkey_genseck {
11862306a36Sopenharmony_ci	__u16 cardnr;		    /* in: card to use or FFFF for any	 */
11962306a36Sopenharmony_ci	__u16 domain;		    /* in: domain or FFFF for any	 */
12062306a36Sopenharmony_ci	__u32 keytype;		    /* in: key type to generate		 */
12162306a36Sopenharmony_ci	struct pkey_seckey seckey;  /* out: the secure key blob		 */
12262306a36Sopenharmony_ci};
12362306a36Sopenharmony_ci#define PKEY_GENSECK _IOWR(PKEY_IOCTL_MAGIC, 0x01, struct pkey_genseck)
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * Construct CCA AES secure key from clear key value
12762306a36Sopenharmony_ci */
12862306a36Sopenharmony_cistruct pkey_clr2seck {
12962306a36Sopenharmony_ci	__u16 cardnr;		    /* in: card to use or FFFF for any	 */
13062306a36Sopenharmony_ci	__u16 domain;		    /* in: domain or FFFF for any	 */
13162306a36Sopenharmony_ci	__u32 keytype;		    /* in: key type to generate		 */
13262306a36Sopenharmony_ci	struct pkey_clrkey clrkey;  /* in: the clear key value		 */
13362306a36Sopenharmony_ci	struct pkey_seckey seckey;  /* out: the secure key blob		 */
13462306a36Sopenharmony_ci};
13562306a36Sopenharmony_ci#define PKEY_CLR2SECK _IOWR(PKEY_IOCTL_MAGIC, 0x02, struct pkey_clr2seck)
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/*
13862306a36Sopenharmony_ci * Fabricate AES protected key from a CCA AES secure key
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_cistruct pkey_sec2protk {
14162306a36Sopenharmony_ci	__u16 cardnr;		     /* in: card to use or FFFF for any   */
14262306a36Sopenharmony_ci	__u16 domain;		     /* in: domain or FFFF for any	  */
14362306a36Sopenharmony_ci	struct pkey_seckey seckey;   /* in: the secure key blob		  */
14462306a36Sopenharmony_ci	struct pkey_protkey protkey; /* out: the protected key		  */
14562306a36Sopenharmony_ci};
14662306a36Sopenharmony_ci#define PKEY_SEC2PROTK _IOWR(PKEY_IOCTL_MAGIC, 0x03, struct pkey_sec2protk)
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci/*
14962306a36Sopenharmony_ci * Fabricate AES protected key from clear key value
15062306a36Sopenharmony_ci */
15162306a36Sopenharmony_cistruct pkey_clr2protk {
15262306a36Sopenharmony_ci	__u32 keytype;		     /* in: key type to generate	  */
15362306a36Sopenharmony_ci	struct pkey_clrkey clrkey;   /* in: the clear key value		  */
15462306a36Sopenharmony_ci	struct pkey_protkey protkey; /* out: the protected key		  */
15562306a36Sopenharmony_ci};
15662306a36Sopenharmony_ci#define PKEY_CLR2PROTK _IOWR(PKEY_IOCTL_MAGIC, 0x04, struct pkey_clr2protk)
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci/*
15962306a36Sopenharmony_ci * Search for matching crypto card based on the Master Key
16062306a36Sopenharmony_ci * Verification Pattern provided inside a CCA AES secure key.
16162306a36Sopenharmony_ci */
16262306a36Sopenharmony_cistruct pkey_findcard {
16362306a36Sopenharmony_ci	struct pkey_seckey seckey;	       /* in: the secure key blob */
16462306a36Sopenharmony_ci	__u16  cardnr;			       /* out: card number	  */
16562306a36Sopenharmony_ci	__u16  domain;			       /* out: domain number	  */
16662306a36Sopenharmony_ci};
16762306a36Sopenharmony_ci#define PKEY_FINDCARD _IOWR(PKEY_IOCTL_MAGIC, 0x05, struct pkey_findcard)
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/*
17062306a36Sopenharmony_ci * Combined together: findcard + sec2prot
17162306a36Sopenharmony_ci */
17262306a36Sopenharmony_cistruct pkey_skey2pkey {
17362306a36Sopenharmony_ci	struct pkey_seckey seckey;   /* in: the secure key blob		  */
17462306a36Sopenharmony_ci	struct pkey_protkey protkey; /* out: the protected key		  */
17562306a36Sopenharmony_ci};
17662306a36Sopenharmony_ci#define PKEY_SKEY2PKEY _IOWR(PKEY_IOCTL_MAGIC, 0x06, struct pkey_skey2pkey)
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci/*
17962306a36Sopenharmony_ci * Verify the given CCA AES secure key for being able to be usable with
18062306a36Sopenharmony_ci * the pkey module. Check for correct key type and check for having at
18162306a36Sopenharmony_ci * least one crypto card being able to handle this key (master key
18262306a36Sopenharmony_ci * or old master key verification pattern matches).
18362306a36Sopenharmony_ci * Return some info about the key: keysize in bits, keytype (currently
18462306a36Sopenharmony_ci * only AES), flag if key is wrapped with an old MKVP.
18562306a36Sopenharmony_ci */
18662306a36Sopenharmony_cistruct pkey_verifykey {
18762306a36Sopenharmony_ci	struct pkey_seckey seckey;	       /* in: the secure key blob */
18862306a36Sopenharmony_ci	__u16  cardnr;			       /* out: card number	  */
18962306a36Sopenharmony_ci	__u16  domain;			       /* out: domain number	  */
19062306a36Sopenharmony_ci	__u16  keysize;			       /* out: key size in bits   */
19162306a36Sopenharmony_ci	__u32  attributes;		       /* out: attribute bits	  */
19262306a36Sopenharmony_ci};
19362306a36Sopenharmony_ci#define PKEY_VERIFYKEY _IOWR(PKEY_IOCTL_MAGIC, 0x07, struct pkey_verifykey)
19462306a36Sopenharmony_ci#define PKEY_VERIFY_ATTR_AES	   0x00000001  /* key is an AES key */
19562306a36Sopenharmony_ci#define PKEY_VERIFY_ATTR_OLD_MKVP  0x00000100  /* key has old MKVP value */
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci/*
19862306a36Sopenharmony_ci * Generate AES random protected key.
19962306a36Sopenharmony_ci */
20062306a36Sopenharmony_cistruct pkey_genprotk {
20162306a36Sopenharmony_ci	__u32 keytype;			       /* in: key type to generate */
20262306a36Sopenharmony_ci	struct pkey_protkey protkey;	       /* out: the protected key   */
20362306a36Sopenharmony_ci};
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci#define PKEY_GENPROTK _IOWR(PKEY_IOCTL_MAGIC, 0x08, struct pkey_genprotk)
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/*
20862306a36Sopenharmony_ci * Verify an AES protected key.
20962306a36Sopenharmony_ci */
21062306a36Sopenharmony_cistruct pkey_verifyprotk {
21162306a36Sopenharmony_ci	struct pkey_protkey protkey;	/* in: the protected key to verify */
21262306a36Sopenharmony_ci};
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#define PKEY_VERIFYPROTK _IOW(PKEY_IOCTL_MAGIC, 0x09, struct pkey_verifyprotk)
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci/*
21762306a36Sopenharmony_ci * Transform an key blob (of any type) into a protected key
21862306a36Sopenharmony_ci */
21962306a36Sopenharmony_cistruct pkey_kblob2pkey {
22062306a36Sopenharmony_ci	__u8 __user *key;		/* in: the key blob	   */
22162306a36Sopenharmony_ci	__u32 keylen;			/* in: the key blob length */
22262306a36Sopenharmony_ci	struct pkey_protkey protkey;	/* out: the protected key  */
22362306a36Sopenharmony_ci};
22462306a36Sopenharmony_ci#define PKEY_KBLOB2PROTK _IOWR(PKEY_IOCTL_MAGIC, 0x0A, struct pkey_kblob2pkey)
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci/*
22762306a36Sopenharmony_ci * Generate secure key, version 2.
22862306a36Sopenharmony_ci * Generate CCA AES secure key, CCA AES cipher key or EP11 AES secure key.
22962306a36Sopenharmony_ci * There needs to be a list of apqns given with at least one entry in there.
23062306a36Sopenharmony_ci * All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain
23162306a36Sopenharmony_ci * is not supported. The implementation walks through the list of apqns and
23262306a36Sopenharmony_ci * tries to send the request to each apqn without any further checking (like
23362306a36Sopenharmony_ci * card type or online state). If the apqn fails, simple the next one in the
23462306a36Sopenharmony_ci * list is tried until success (return 0) or the end of the list is reached
23562306a36Sopenharmony_ci * (return -1 with errno ENODEV). You may use the PKEY_APQNS4KT ioctl to
23662306a36Sopenharmony_ci * generate a list of apqns based on the key type to generate.
23762306a36Sopenharmony_ci * The keygenflags argument is passed to the low level generation functions
23862306a36Sopenharmony_ci * individual for the key type and has a key type specific meaning. When
23962306a36Sopenharmony_ci * generating CCA cipher keys you can use one or more of the PKEY_KEYGEN_*
24062306a36Sopenharmony_ci * flags to widen the export possibilities. By default a cipher key is
24162306a36Sopenharmony_ci * only exportable for CPACF (PKEY_KEYGEN_XPRT_CPAC).
24262306a36Sopenharmony_ci * The keygenflag argument for generating an EP11 AES key should either be 0
24362306a36Sopenharmony_ci * to use the defaults which are XCP_BLOB_ENCRYPT, XCP_BLOB_DECRYPT and
24462306a36Sopenharmony_ci * XCP_BLOB_PROTKEY_EXTRACTABLE or a valid combination of XCP_BLOB_* flags.
24562306a36Sopenharmony_ci */
24662306a36Sopenharmony_cistruct pkey_genseck2 {
24762306a36Sopenharmony_ci	struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets*/
24862306a36Sopenharmony_ci	__u32 apqn_entries;	    /* in: # of apqn target list entries  */
24962306a36Sopenharmony_ci	enum pkey_key_type type;    /* in: key type to generate		  */
25062306a36Sopenharmony_ci	enum pkey_key_size size;    /* in: key size to generate		  */
25162306a36Sopenharmony_ci	__u32 keygenflags;	    /* in: key generation flags		  */
25262306a36Sopenharmony_ci	__u8 __user *key;	    /* in: pointer to key blob buffer	  */
25362306a36Sopenharmony_ci	__u32 keylen;		    /* in: available key blob buffer size */
25462306a36Sopenharmony_ci				    /* out: actual key blob size	  */
25562306a36Sopenharmony_ci};
25662306a36Sopenharmony_ci#define PKEY_GENSECK2 _IOWR(PKEY_IOCTL_MAGIC, 0x11, struct pkey_genseck2)
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci/*
25962306a36Sopenharmony_ci * Generate secure key from clear key value, version 2.
26062306a36Sopenharmony_ci * Construct an CCA AES secure key, CCA AES cipher key or EP11 AES secure
26162306a36Sopenharmony_ci * key from a given clear key value.
26262306a36Sopenharmony_ci * There needs to be a list of apqns given with at least one entry in there.
26362306a36Sopenharmony_ci * All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain
26462306a36Sopenharmony_ci * is not supported. The implementation walks through the list of apqns and
26562306a36Sopenharmony_ci * tries to send the request to each apqn without any further checking (like
26662306a36Sopenharmony_ci * card type or online state). If the apqn fails, simple the next one in the
26762306a36Sopenharmony_ci * list is tried until success (return 0) or the end of the list is reached
26862306a36Sopenharmony_ci * (return -1 with errno ENODEV). You may use the PKEY_APQNS4KT ioctl to
26962306a36Sopenharmony_ci * generate a list of apqns based on the key type to generate.
27062306a36Sopenharmony_ci * The keygenflags argument is passed to the low level generation functions
27162306a36Sopenharmony_ci * individual for the key type and has a key type specific meaning. When
27262306a36Sopenharmony_ci * generating CCA cipher keys you can use one or more of the PKEY_KEYGEN_*
27362306a36Sopenharmony_ci * flags to widen the export possibilities. By default a cipher key is
27462306a36Sopenharmony_ci * only exportable for CPACF (PKEY_KEYGEN_XPRT_CPAC).
27562306a36Sopenharmony_ci * The keygenflag argument for generating an EP11 AES key should either be 0
27662306a36Sopenharmony_ci * to use the defaults which are XCP_BLOB_ENCRYPT, XCP_BLOB_DECRYPT and
27762306a36Sopenharmony_ci * XCP_BLOB_PROTKEY_EXTRACTABLE or a valid combination of XCP_BLOB_* flags.
27862306a36Sopenharmony_ci */
27962306a36Sopenharmony_cistruct pkey_clr2seck2 {
28062306a36Sopenharmony_ci	struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets */
28162306a36Sopenharmony_ci	__u32 apqn_entries;	    /* in: # of apqn target list entries   */
28262306a36Sopenharmony_ci	enum pkey_key_type type;    /* in: key type to generate		   */
28362306a36Sopenharmony_ci	enum pkey_key_size size;    /* in: key size to generate		   */
28462306a36Sopenharmony_ci	__u32 keygenflags;	    /* in: key generation flags		   */
28562306a36Sopenharmony_ci	struct pkey_clrkey clrkey;  /* in: the clear key value		   */
28662306a36Sopenharmony_ci	__u8 __user *key;	    /* in: pointer to key blob buffer	   */
28762306a36Sopenharmony_ci	__u32 keylen;		    /* in: available key blob buffer size  */
28862306a36Sopenharmony_ci				    /* out: actual key blob size	   */
28962306a36Sopenharmony_ci};
29062306a36Sopenharmony_ci#define PKEY_CLR2SECK2 _IOWR(PKEY_IOCTL_MAGIC, 0x12, struct pkey_clr2seck2)
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci/*
29362306a36Sopenharmony_ci * Verify the given secure key, version 2.
29462306a36Sopenharmony_ci * Check for correct key type. If cardnr and domain are given (are not
29562306a36Sopenharmony_ci * 0xFFFF) also check if this apqn is able to handle this type of key.
29662306a36Sopenharmony_ci * If cardnr and/or domain is 0xFFFF, on return these values are filled
29762306a36Sopenharmony_ci * with one apqn able to handle this key.
29862306a36Sopenharmony_ci * The function also checks for the master key verification patterns
29962306a36Sopenharmony_ci * of the key matching to the current or alternate mkvp of the apqn.
30062306a36Sopenharmony_ci * For CCA AES secure keys and CCA AES cipher keys this means to check
30162306a36Sopenharmony_ci * the key's mkvp against the current or old mkvp of the apqns. The flags
30262306a36Sopenharmony_ci * field is updated with some additional info about the apqn mkvp
30362306a36Sopenharmony_ci * match: If the current mkvp matches to the key's mkvp then the
30462306a36Sopenharmony_ci * PKEY_FLAGS_MATCH_CUR_MKVP bit is set, if the alternate mkvp matches to
30562306a36Sopenharmony_ci * the key's mkvp the PKEY_FLAGS_MATCH_ALT_MKVP is set. For CCA keys the
30662306a36Sopenharmony_ci * alternate mkvp is the old master key verification pattern.
30762306a36Sopenharmony_ci * CCA AES secure keys are also checked to have the CPACF export allowed
30862306a36Sopenharmony_ci * bit enabled (XPRTCPAC) in the kmf1 field.
30962306a36Sopenharmony_ci * EP11 keys are also supported and the wkvp of the key is checked against
31062306a36Sopenharmony_ci * the current wkvp of the apqns. There is no alternate for this type of
31162306a36Sopenharmony_ci * key and so on a match the flag PKEY_FLAGS_MATCH_CUR_MKVP always is set.
31262306a36Sopenharmony_ci * EP11 keys are also checked to have XCP_BLOB_PROTKEY_EXTRACTABLE set.
31362306a36Sopenharmony_ci * The ioctl returns 0 as long as the given or found apqn matches to
31462306a36Sopenharmony_ci * matches with the current or alternate mkvp to the key's mkvp. If the given
31562306a36Sopenharmony_ci * apqn does not match or there is no such apqn found, -1 with errno
31662306a36Sopenharmony_ci * ENODEV is returned.
31762306a36Sopenharmony_ci */
31862306a36Sopenharmony_cistruct pkey_verifykey2 {
31962306a36Sopenharmony_ci	__u8 __user *key;	    /* in: pointer to key blob		 */
32062306a36Sopenharmony_ci	__u32 keylen;		    /* in: key blob size		 */
32162306a36Sopenharmony_ci	__u16 cardnr;		    /* in/out: card number		 */
32262306a36Sopenharmony_ci	__u16 domain;		    /* in/out: domain number		 */
32362306a36Sopenharmony_ci	enum pkey_key_type type;    /* out: the key type		 */
32462306a36Sopenharmony_ci	enum pkey_key_size size;    /* out: the key size		 */
32562306a36Sopenharmony_ci	__u32 flags;		    /* out: additional key info flags	 */
32662306a36Sopenharmony_ci};
32762306a36Sopenharmony_ci#define PKEY_VERIFYKEY2 _IOWR(PKEY_IOCTL_MAGIC, 0x17, struct pkey_verifykey2)
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci/*
33062306a36Sopenharmony_ci * Transform a key blob into a protected key, version 2.
33162306a36Sopenharmony_ci * There needs to be a list of apqns given with at least one entry in there.
33262306a36Sopenharmony_ci * All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain
33362306a36Sopenharmony_ci * is not supported. The implementation walks through the list of apqns and
33462306a36Sopenharmony_ci * tries to send the request to each apqn without any further checking (like
33562306a36Sopenharmony_ci * card type or online state). If the apqn fails, simple the next one in the
33662306a36Sopenharmony_ci * list is tried until success (return 0) or the end of the list is reached
33762306a36Sopenharmony_ci * (return -1 with errno ENODEV). You may use the PKEY_APQNS4K ioctl to
33862306a36Sopenharmony_ci * generate a list of apqns based on the key.
33962306a36Sopenharmony_ci * Deriving ECC protected keys from ECC secure keys is not supported with
34062306a36Sopenharmony_ci * this ioctl, use PKEY_KBLOB2PROTK3 for this purpose.
34162306a36Sopenharmony_ci */
34262306a36Sopenharmony_cistruct pkey_kblob2pkey2 {
34362306a36Sopenharmony_ci	__u8 __user *key;	     /* in: pointer to key blob		   */
34462306a36Sopenharmony_ci	__u32 keylen;		     /* in: key blob size		   */
34562306a36Sopenharmony_ci	struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets */
34662306a36Sopenharmony_ci	__u32 apqn_entries;	     /* in: # of apqn target list entries  */
34762306a36Sopenharmony_ci	struct pkey_protkey protkey; /* out: the protected key		   */
34862306a36Sopenharmony_ci};
34962306a36Sopenharmony_ci#define PKEY_KBLOB2PROTK2 _IOWR(PKEY_IOCTL_MAGIC, 0x1A, struct pkey_kblob2pkey2)
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci/*
35262306a36Sopenharmony_ci * Build a list of APQNs based on a key blob given.
35362306a36Sopenharmony_ci * Is able to find out which type of secure key is given (CCA AES secure
35462306a36Sopenharmony_ci * key, CCA AES cipher key, CCA ECC private key, EP11 AES key, EP11 ECC private
35562306a36Sopenharmony_ci * key) and tries to find all matching crypto cards based on the MKVP and maybe
35662306a36Sopenharmony_ci * other criteria (like CCA AES cipher keys need a CEX5C or higher, EP11 keys
35762306a36Sopenharmony_ci * with BLOB_PKEY_EXTRACTABLE need a CEX7 and EP11 api version 4). The list of
35862306a36Sopenharmony_ci * APQNs is further filtered by the key's mkvp which needs to match to either
35962306a36Sopenharmony_ci * the current mkvp (CCA and EP11) or the alternate mkvp (old mkvp, CCA adapters
36062306a36Sopenharmony_ci * only) of the apqns. The flags argument may be used to limit the matching
36162306a36Sopenharmony_ci * apqns. If the PKEY_FLAGS_MATCH_CUR_MKVP is given, only the current mkvp of
36262306a36Sopenharmony_ci * each apqn is compared. Likewise with the PKEY_FLAGS_MATCH_ALT_MKVP. If both
36362306a36Sopenharmony_ci * are given, it is assumed to return apqns where either the current or the
36462306a36Sopenharmony_ci * alternate mkvp matches. At least one of the matching flags needs to be given.
36562306a36Sopenharmony_ci * The flags argument for EP11 keys has no further action and is currently
36662306a36Sopenharmony_ci * ignored (but needs to be given as PKEY_FLAGS_MATCH_CUR_MKVP) as there is only
36762306a36Sopenharmony_ci * the wkvp from the key to match against the apqn's wkvp.
36862306a36Sopenharmony_ci * The list of matching apqns is stored into the space given by the apqns
36962306a36Sopenharmony_ci * argument and the number of stored entries goes into apqn_entries. If the list
37062306a36Sopenharmony_ci * is empty (apqn_entries is 0) the apqn_entries field is updated to the number
37162306a36Sopenharmony_ci * of apqn targets found and the ioctl returns with 0. If apqn_entries is > 0
37262306a36Sopenharmony_ci * but the number of apqn targets does not fit into the list, the apqn_targets
37362306a36Sopenharmony_ci * field is updated with the number of required entries but there are no apqn
37462306a36Sopenharmony_ci * values stored in the list and the ioctl returns with ENOSPC. If no matching
37562306a36Sopenharmony_ci * APQN is found, the ioctl returns with 0 but the apqn_entries value is 0.
37662306a36Sopenharmony_ci */
37762306a36Sopenharmony_cistruct pkey_apqns4key {
37862306a36Sopenharmony_ci	__u8 __user *key;	   /* in: pointer to key blob		      */
37962306a36Sopenharmony_ci	__u32 keylen;		   /* in: key blob size			      */
38062306a36Sopenharmony_ci	__u32 flags;		   /* in: match controlling flags	      */
38162306a36Sopenharmony_ci	struct pkey_apqn __user *apqns; /* in/out: ptr to list of apqn targets*/
38262306a36Sopenharmony_ci	__u32 apqn_entries;	   /* in: max # of apqn entries in the list   */
38362306a36Sopenharmony_ci				   /* out: # apqns stored into the list	      */
38462306a36Sopenharmony_ci};
38562306a36Sopenharmony_ci#define PKEY_APQNS4K _IOWR(PKEY_IOCTL_MAGIC, 0x1B, struct pkey_apqns4key)
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci/*
38862306a36Sopenharmony_ci * Build a list of APQNs based on a key type given.
38962306a36Sopenharmony_ci * Build a list of APQNs based on a given key type and maybe further
39062306a36Sopenharmony_ci * restrict the list by given master key verification patterns.
39162306a36Sopenharmony_ci * For different key types there may be different ways to match the
39262306a36Sopenharmony_ci * master key verification patterns. For CCA keys (CCA data key and CCA
39362306a36Sopenharmony_ci * cipher key) the first 8 bytes of cur_mkvp refer to the current AES mkvp value
39462306a36Sopenharmony_ci * of the apqn and the first 8 bytes of the alt_mkvp refer to the old AES mkvp.
39562306a36Sopenharmony_ci * For CCA ECC keys it is similar but the match is against the APKA current/old
39662306a36Sopenharmony_ci * mkvp. The flags argument controls if the apqns current and/or alternate mkvp
39762306a36Sopenharmony_ci * should match. If the PKEY_FLAGS_MATCH_CUR_MKVP is given, only the current
39862306a36Sopenharmony_ci * mkvp of each apqn is compared. Likewise with the PKEY_FLAGS_MATCH_ALT_MKVP.
39962306a36Sopenharmony_ci * If both are given, it is assumed to return apqns where either the
40062306a36Sopenharmony_ci * current or the alternate mkvp matches. If no match flag is given
40162306a36Sopenharmony_ci * (flags is 0) the mkvp values are ignored for the match process.
40262306a36Sopenharmony_ci * For EP11 keys there is only the current wkvp. So if the apqns should also
40362306a36Sopenharmony_ci * match to a given wkvp, then the PKEY_FLAGS_MATCH_CUR_MKVP flag should be
40462306a36Sopenharmony_ci * set. The wkvp value is 32 bytes but only the leftmost 16 bytes are compared
40562306a36Sopenharmony_ci * against the leftmost 16 byte of the wkvp of the apqn.
40662306a36Sopenharmony_ci * The list of matching apqns is stored into the space given by the apqns
40762306a36Sopenharmony_ci * argument and the number of stored entries goes into apqn_entries. If the list
40862306a36Sopenharmony_ci * is empty (apqn_entries is 0) the apqn_entries field is updated to the number
40962306a36Sopenharmony_ci * of apqn targets found and the ioctl returns with 0. If apqn_entries is > 0
41062306a36Sopenharmony_ci * but the number of apqn targets does not fit into the list, the apqn_targets
41162306a36Sopenharmony_ci * field is updated with the number of required entries but there are no apqn
41262306a36Sopenharmony_ci * values stored in the list and the ioctl returns with ENOSPC. If no matching
41362306a36Sopenharmony_ci * APQN is found, the ioctl returns with 0 but the apqn_entries value is 0.
41462306a36Sopenharmony_ci */
41562306a36Sopenharmony_cistruct pkey_apqns4keytype {
41662306a36Sopenharmony_ci	enum pkey_key_type type;   /* in: key type			      */
41762306a36Sopenharmony_ci	__u8  cur_mkvp[32];	   /* in: current mkvp			      */
41862306a36Sopenharmony_ci	__u8  alt_mkvp[32];	   /* in: alternate mkvp		      */
41962306a36Sopenharmony_ci	__u32 flags;		   /* in: match controlling flags	      */
42062306a36Sopenharmony_ci	struct pkey_apqn __user *apqns; /* in/out: ptr to list of apqn targets*/
42162306a36Sopenharmony_ci	__u32 apqn_entries;	   /* in: max # of apqn entries in the list   */
42262306a36Sopenharmony_ci				   /* out: # apqns stored into the list	      */
42362306a36Sopenharmony_ci};
42462306a36Sopenharmony_ci#define PKEY_APQNS4KT _IOWR(PKEY_IOCTL_MAGIC, 0x1C, struct pkey_apqns4keytype)
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci/*
42762306a36Sopenharmony_ci * Transform a key blob into a protected key, version 3.
42862306a36Sopenharmony_ci * The difference to version 2 of this ioctl is that the protected key
42962306a36Sopenharmony_ci * buffer is now explicitly and not within a struct pkey_protkey any more.
43062306a36Sopenharmony_ci * So this ioctl is also able to handle EP11 and CCA ECC secure keys and
43162306a36Sopenharmony_ci * provide ECC protected keys.
43262306a36Sopenharmony_ci * There needs to be a list of apqns given with at least one entry in there.
43362306a36Sopenharmony_ci * All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain
43462306a36Sopenharmony_ci * is not supported. The implementation walks through the list of apqns and
43562306a36Sopenharmony_ci * tries to send the request to each apqn without any further checking (like
43662306a36Sopenharmony_ci * card type or online state). If the apqn fails, simple the next one in the
43762306a36Sopenharmony_ci * list is tried until success (return 0) or the end of the list is reached
43862306a36Sopenharmony_ci * (return -1 with errno ENODEV). You may use the PKEY_APQNS4K ioctl to
43962306a36Sopenharmony_ci * generate a list of apqns based on the key.
44062306a36Sopenharmony_ci */
44162306a36Sopenharmony_cistruct pkey_kblob2pkey3 {
44262306a36Sopenharmony_ci	__u8 __user *key;	     /* in: pointer to key blob		   */
44362306a36Sopenharmony_ci	__u32 keylen;		     /* in: key blob size		   */
44462306a36Sopenharmony_ci	struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets */
44562306a36Sopenharmony_ci	__u32 apqn_entries;	     /* in: # of apqn target list entries  */
44662306a36Sopenharmony_ci	__u32 pkeytype;		/* out: prot key type (enum pkey_key_type) */
44762306a36Sopenharmony_ci	__u32 pkeylen;	 /* in/out: size of pkey buffer/actual len of pkey */
44862306a36Sopenharmony_ci	__u8 __user *pkey;		 /* in: pkey blob buffer space ptr */
44962306a36Sopenharmony_ci};
45062306a36Sopenharmony_ci#define PKEY_KBLOB2PROTK3 _IOWR(PKEY_IOCTL_MAGIC, 0x1D, struct pkey_kblob2pkey3)
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci#endif /* _UAPI_PKEY_H */
453