/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */1/*2* Userspace interface to the pkey device driver3*4* Copyright IBM Corp. 2017, 20235*6* Author: Harald Freudenberger <[email protected]>7*8*/910#ifndef _UAPI_PKEY_H11#define _UAPI_PKEY_H1213#include <linux/ioctl.h>14#include <linux/types.h>1516/*17* Ioctl calls supported by the pkey device driver18*/1920#define PKEY_IOCTL_MAGIC 'p'2122#define SECKEYBLOBSIZE 64 /* secure key blob size is always 64 bytes */23#define PROTKEYBLOBSIZE 80 /* protected key blob size is always 80 bytes */24#define MAXPROTKEYSIZE 64 /* a protected key blob may be up to 64 bytes */25#define MAXCLRKEYSIZE 32 /* a clear key value may be up to 32 bytes */26#define MAXAESCIPHERKEYSIZE 136 /* our aes cipher keys have always 136 bytes */27#define MINEP11AESKEYBLOBSIZE 256 /* min EP11 AES key blob size */28#define MAXEP11AESKEYBLOBSIZE 336 /* max EP11 AES key blob size */2930/* Minimum size of a key blob */31#define MINKEYBLOBSIZE SECKEYBLOBSIZE3233/* defines for the type field within the pkey_protkey struct */34#define PKEY_KEYTYPE_AES_128 135#define PKEY_KEYTYPE_AES_192 236#define PKEY_KEYTYPE_AES_256 337#define PKEY_KEYTYPE_ECC 438#define PKEY_KEYTYPE_ECC_P256 539#define PKEY_KEYTYPE_ECC_P384 640#define PKEY_KEYTYPE_ECC_P521 741#define PKEY_KEYTYPE_ECC_ED25519 842#define PKEY_KEYTYPE_ECC_ED448 943#define PKEY_KEYTYPE_AES_XTS_128 1044#define PKEY_KEYTYPE_AES_XTS_256 1145#define PKEY_KEYTYPE_HMAC_512 1246#define PKEY_KEYTYPE_HMAC_1024 134748/* the newer ioctls use a pkey_key_type enum for type information */49enum pkey_key_type {50PKEY_TYPE_CCA_DATA = (__u32)1,51PKEY_TYPE_CCA_CIPHER = (__u32)2,52PKEY_TYPE_EP11 = (__u32)3,53PKEY_TYPE_CCA_ECC = (__u32)0x1f,54PKEY_TYPE_EP11_AES = (__u32)6,55PKEY_TYPE_EP11_ECC = (__u32)7,56PKEY_TYPE_PROTKEY = (__u32)8,57PKEY_TYPE_UVSECRET = (__u32)9,58};5960/* the newer ioctls use a pkey_key_size enum for key size information */61enum pkey_key_size {62PKEY_SIZE_AES_128 = (__u32)128,63PKEY_SIZE_AES_192 = (__u32)192,64PKEY_SIZE_AES_256 = (__u32)256,65PKEY_SIZE_UNKNOWN = (__u32)0xFFFFFFFF,66};6768/* some of the newer ioctls use these flags */69#define PKEY_FLAGS_MATCH_CUR_MKVP 0x0000000270#define PKEY_FLAGS_MATCH_ALT_MKVP 0x000000047172/* keygenflags defines for CCA AES cipher keys */73#define PKEY_KEYGEN_XPRT_SYM 0x0000800074#define PKEY_KEYGEN_XPRT_UASY 0x0000400075#define PKEY_KEYGEN_XPRT_AASY 0x0000200076#define PKEY_KEYGEN_XPRT_RAW 0x0000100077#define PKEY_KEYGEN_XPRT_CPAC 0x0000080078#define PKEY_KEYGEN_XPRT_DES 0x0000008079#define PKEY_KEYGEN_XPRT_AES 0x0000004080#define PKEY_KEYGEN_XPRT_RSA 0x000000088182/* Struct to hold apqn target info (card/domain pair) */83struct pkey_apqn {84__u16 card;85__u16 domain;86};8788/* Struct to hold a CCA AES secure key blob */89struct pkey_seckey {90__u8 seckey[SECKEYBLOBSIZE]; /* the secure key blob */91};9293/* Struct to hold protected key and length info */94struct pkey_protkey {95__u32 type; /* key type, one of the PKEY_KEYTYPE_AES values */96__u32 len; /* bytes actually stored in protkey[] */97__u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */98};99100/* Struct to hold an AES clear key value */101struct pkey_clrkey {102__u8 clrkey[MAXCLRKEYSIZE]; /* 16, 24, or 32 byte clear key value */103};104105/*106* EP11 key blobs of type PKEY_TYPE_EP11_AES and PKEY_TYPE_EP11_ECC107* are ep11 blobs prepended by this header:108*/109struct ep11kblob_header {110__u8 type; /* always 0x00 */111__u8 hver; /* header version, currently needs to be 0x00 */112__u16 len; /* total length in bytes (including this header) */113__u8 version; /* PKEY_TYPE_EP11_AES or PKEY_TYPE_EP11_ECC */114__u8 res0; /* unused */115__u16 bitlen; /* clear key bit len, 0 for unknown */116__u8 res1[8]; /* unused */117} __packed;118119/*120* Generate CCA AES secure key.121*/122struct pkey_genseck {123__u16 cardnr; /* in: card to use or FFFF for any */124__u16 domain; /* in: domain or FFFF for any */125__u32 keytype; /* in: key type to generate */126struct pkey_seckey seckey; /* out: the secure key blob */127};128129#define PKEY_GENSECK _IOWR(PKEY_IOCTL_MAGIC, 0x01, struct pkey_genseck)130131/*132* Construct CCA AES secure key from clear key value133*/134struct pkey_clr2seck {135__u16 cardnr; /* in: card to use or FFFF for any */136__u16 domain; /* in: domain or FFFF for any */137__u32 keytype; /* in: key type to generate */138struct pkey_clrkey clrkey; /* in: the clear key value */139struct pkey_seckey seckey; /* out: the secure key blob */140};141142#define PKEY_CLR2SECK _IOWR(PKEY_IOCTL_MAGIC, 0x02, struct pkey_clr2seck)143144/*145* Fabricate AES protected key from a CCA AES secure key146*/147struct pkey_sec2protk {148__u16 cardnr; /* in: card to use or FFFF for any */149__u16 domain; /* in: domain or FFFF for any */150struct pkey_seckey seckey; /* in: the secure key blob */151struct pkey_protkey protkey; /* out: the protected key */152};153154#define PKEY_SEC2PROTK _IOWR(PKEY_IOCTL_MAGIC, 0x03, struct pkey_sec2protk)155156/*157* Fabricate AES protected key from clear key value158*/159struct pkey_clr2protk {160__u32 keytype; /* in: key type to generate */161struct pkey_clrkey clrkey; /* in: the clear key value */162struct pkey_protkey protkey; /* out: the protected key */163};164165#define PKEY_CLR2PROTK _IOWR(PKEY_IOCTL_MAGIC, 0x04, struct pkey_clr2protk)166167/*168* Search for matching crypto card based on the Master Key169* Verification Pattern provided inside a CCA AES secure key.170*/171struct pkey_findcard {172struct pkey_seckey seckey; /* in: the secure key blob */173__u16 cardnr; /* out: card number */174__u16 domain; /* out: domain number */175};176177#define PKEY_FINDCARD _IOWR(PKEY_IOCTL_MAGIC, 0x05, struct pkey_findcard)178179/*180* Combined together: findcard + sec2prot181*/182struct pkey_skey2pkey {183struct pkey_seckey seckey; /* in: the secure key blob */184struct pkey_protkey protkey; /* out: the protected key */185};186187#define PKEY_SKEY2PKEY _IOWR(PKEY_IOCTL_MAGIC, 0x06, struct pkey_skey2pkey)188189/*190* Verify the given CCA AES secure key for being able to be usable with191* the pkey module. Check for correct key type and check for having at192* least one crypto card being able to handle this key (master key193* or old master key verification pattern matches).194* Return some info about the key: keysize in bits, keytype (currently195* only AES), flag if key is wrapped with an old MKVP.196*/197struct pkey_verifykey {198struct pkey_seckey seckey; /* in: the secure key blob */199__u16 cardnr; /* out: card number */200__u16 domain; /* out: domain number */201__u16 keysize; /* out: key size in bits */202__u32 attributes; /* out: attribute bits */203};204205#define PKEY_VERIFYKEY _IOWR(PKEY_IOCTL_MAGIC, 0x07, struct pkey_verifykey)206#define PKEY_VERIFY_ATTR_AES 0x00000001 /* key is an AES key */207#define PKEY_VERIFY_ATTR_OLD_MKVP 0x00000100 /* key has old MKVP value */208209/*210* Generate AES random protected key.211*/212struct pkey_genprotk {213__u32 keytype; /* in: key type to generate */214struct pkey_protkey protkey; /* out: the protected key */215};216217#define PKEY_GENPROTK _IOWR(PKEY_IOCTL_MAGIC, 0x08, struct pkey_genprotk)218219/*220* Verify an AES protected key.221*/222struct pkey_verifyprotk {223struct pkey_protkey protkey; /* in: the protected key to verify */224};225226#define PKEY_VERIFYPROTK _IOW(PKEY_IOCTL_MAGIC, 0x09, struct pkey_verifyprotk)227228/*229* Transform an key blob (of any type) into a protected key230*/231struct pkey_kblob2pkey {232__u8 __user *key; /* in: the key blob */233__u32 keylen; /* in: the key blob length */234struct pkey_protkey protkey; /* out: the protected key */235};236237#define PKEY_KBLOB2PROTK _IOWR(PKEY_IOCTL_MAGIC, 0x0A, struct pkey_kblob2pkey)238239/*240* Generate secure key, version 2.241* Generate CCA AES secure key, CCA AES cipher key or EP11 AES secure key.242* There needs to be a list of apqns given with at least one entry in there.243* All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain244* is not supported. The implementation walks through the list of apqns and245* tries to send the request to each apqn without any further checking (like246* card type or online state). If the apqn fails, simple the next one in the247* list is tried until success (return 0) or the end of the list is reached248* (return -1 with errno ENODEV). You may use the PKEY_APQNS4KT ioctl to249* generate a list of apqns based on the key type to generate.250* The keygenflags argument is passed to the low level generation functions251* individual for the key type and has a key type specific meaning. When252* generating CCA cipher keys you can use one or more of the PKEY_KEYGEN_*253* flags to widen the export possibilities. By default a cipher key is254* only exportable for CPACF (PKEY_KEYGEN_XPRT_CPAC).255* The keygenflag argument for generating an EP11 AES key should either be 0256* to use the defaults which are XCP_BLOB_ENCRYPT, XCP_BLOB_DECRYPT and257* XCP_BLOB_PROTKEY_EXTRACTABLE or a valid combination of XCP_BLOB_* flags.258*/259struct pkey_genseck2 {260struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets*/261__u32 apqn_entries; /* in: # of apqn target list entries */262enum pkey_key_type type; /* in: key type to generate */263enum pkey_key_size size; /* in: key size to generate */264__u32 keygenflags; /* in: key generation flags */265__u8 __user *key; /* in: pointer to key blob buffer */266__u32 keylen; /* in: available key blob buffer size */267/* out: actual key blob size */268};269270#define PKEY_GENSECK2 _IOWR(PKEY_IOCTL_MAGIC, 0x11, struct pkey_genseck2)271272/*273* Generate secure key from clear key value, version 2.274* Construct an CCA AES secure key, CCA AES cipher key or EP11 AES secure275* key from a given clear key value.276* There needs to be a list of apqns given with at least one entry in there.277* All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain278* is not supported. The implementation walks through the list of apqns and279* tries to send the request to each apqn without any further checking (like280* card type or online state). If the apqn fails, simple the next one in the281* list is tried until success (return 0) or the end of the list is reached282* (return -1 with errno ENODEV). You may use the PKEY_APQNS4KT ioctl to283* generate a list of apqns based on the key type to generate.284* The keygenflags argument is passed to the low level generation functions285* individual for the key type and has a key type specific meaning. When286* generating CCA cipher keys you can use one or more of the PKEY_KEYGEN_*287* flags to widen the export possibilities. By default a cipher key is288* only exportable for CPACF (PKEY_KEYGEN_XPRT_CPAC).289* The keygenflag argument for generating an EP11 AES key should either be 0290* to use the defaults which are XCP_BLOB_ENCRYPT, XCP_BLOB_DECRYPT and291* XCP_BLOB_PROTKEY_EXTRACTABLE or a valid combination of XCP_BLOB_* flags.292*/293struct pkey_clr2seck2 {294struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets */295__u32 apqn_entries; /* in: # of apqn target list entries */296enum pkey_key_type type; /* in: key type to generate */297enum pkey_key_size size; /* in: key size to generate */298__u32 keygenflags; /* in: key generation flags */299struct pkey_clrkey clrkey; /* in: the clear key value */300__u8 __user *key; /* in: pointer to key blob buffer */301__u32 keylen; /* in: available key blob buffer size */302/* out: actual key blob size */303};304305#define PKEY_CLR2SECK2 _IOWR(PKEY_IOCTL_MAGIC, 0x12, struct pkey_clr2seck2)306307/*308* Verify the given secure key, version 2.309* Check for correct key type. If cardnr and domain are given (are not310* 0xFFFF) also check if this apqn is able to handle this type of key.311* If cardnr and/or domain is 0xFFFF, on return these values are filled312* with one apqn able to handle this key.313* The function also checks for the master key verification patterns314* of the key matching to the current or alternate mkvp of the apqn.315* For CCA AES secure keys and CCA AES cipher keys this means to check316* the key's mkvp against the current or old mkvp of the apqns. The flags317* field is updated with some additional info about the apqn mkvp318* match: If the current mkvp matches to the key's mkvp then the319* PKEY_FLAGS_MATCH_CUR_MKVP bit is set, if the alternate mkvp matches to320* the key's mkvp the PKEY_FLAGS_MATCH_ALT_MKVP is set. For CCA keys the321* alternate mkvp is the old master key verification pattern.322* CCA AES secure keys are also checked to have the CPACF export allowed323* bit enabled (XPRTCPAC) in the kmf1 field.324* EP11 keys are also supported and the wkvp of the key is checked against325* the current wkvp of the apqns. There is no alternate for this type of326* key and so on a match the flag PKEY_FLAGS_MATCH_CUR_MKVP always is set.327* EP11 keys are also checked to have XCP_BLOB_PROTKEY_EXTRACTABLE set.328* The ioctl returns 0 as long as the given or found apqn matches to329* matches with the current or alternate mkvp to the key's mkvp. If the given330* apqn does not match or there is no such apqn found, -1 with errno331* ENODEV is returned.332*/333struct pkey_verifykey2 {334__u8 __user *key; /* in: pointer to key blob */335__u32 keylen; /* in: key blob size */336__u16 cardnr; /* in/out: card number */337__u16 domain; /* in/out: domain number */338enum pkey_key_type type; /* out: the key type */339enum pkey_key_size size; /* out: the key size */340__u32 flags; /* out: additional key info flags */341};342343#define PKEY_VERIFYKEY2 _IOWR(PKEY_IOCTL_MAGIC, 0x17, struct pkey_verifykey2)344345/*346* Transform a key blob into a protected key, version 2.347* There needs to be a list of apqns given with at least one entry in there.348* All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain349* is not supported. The implementation walks through the list of apqns and350* tries to send the request to each apqn without any further checking (like351* card type or online state). If the apqn fails, simple the next one in the352* list is tried until success (return 0) or the end of the list is reached353* (return -1 with errno ENODEV). You may use the PKEY_APQNS4K ioctl to354* generate a list of apqns based on the key.355* Deriving ECC protected keys from ECC secure keys is not supported with356* this ioctl, use PKEY_KBLOB2PROTK3 for this purpose.357*/358struct pkey_kblob2pkey2 {359__u8 __user *key; /* in: pointer to key blob */360__u32 keylen; /* in: key blob size */361struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets */362__u32 apqn_entries; /* in: # of apqn target list entries */363struct pkey_protkey protkey; /* out: the protected key */364};365366#define PKEY_KBLOB2PROTK2 _IOWR(PKEY_IOCTL_MAGIC, 0x1A, struct pkey_kblob2pkey2)367368/*369* Build a list of APQNs based on a key blob given.370* Is able to find out which type of secure key is given (CCA AES secure371* key, CCA AES cipher key, CCA ECC private key, EP11 AES key, EP11 ECC private372* key) and tries to find all matching crypto cards based on the MKVP and maybe373* other criteria (like CCA AES cipher keys need a CEX5C or higher, EP11 keys374* with BLOB_PKEY_EXTRACTABLE need a CEX7 and EP11 api version 4). The list of375* APQNs is further filtered by the key's mkvp which needs to match to either376* the current mkvp (CCA and EP11) or the alternate mkvp (old mkvp, CCA adapters377* only) of the apqns. The flags argument may be used to limit the matching378* apqns. If the PKEY_FLAGS_MATCH_CUR_MKVP is given, only the current mkvp of379* each apqn is compared. Likewise with the PKEY_FLAGS_MATCH_ALT_MKVP. If both380* are given, it is assumed to return apqns where either the current or the381* alternate mkvp matches. At least one of the matching flags needs to be given.382* The flags argument for EP11 keys has no further action and is currently383* ignored (but needs to be given as PKEY_FLAGS_MATCH_CUR_MKVP) as there is only384* the wkvp from the key to match against the apqn's wkvp.385* The list of matching apqns is stored into the space given by the apqns386* argument and the number of stored entries goes into apqn_entries. If the list387* is empty (apqn_entries is 0) the apqn_entries field is updated to the number388* of apqn targets found and the ioctl returns with 0. If apqn_entries is > 0389* but the number of apqn targets does not fit into the list, the apqn_targets390* field is updated with the number of required entries but there are no apqn391* values stored in the list and the ioctl returns with ENOSPC. If no matching392* APQN is found, the ioctl returns with 0 but the apqn_entries value is 0.393*/394struct pkey_apqns4key {395__u8 __user *key; /* in: pointer to key blob */396__u32 keylen; /* in: key blob size */397__u32 flags; /* in: match controlling flags */398struct pkey_apqn __user *apqns; /* in/out: ptr to list of apqn targets*/399__u32 apqn_entries; /* in: max # of apqn entries in the list */400/* out: # apqns stored into the list */401};402403#define PKEY_APQNS4K _IOWR(PKEY_IOCTL_MAGIC, 0x1B, struct pkey_apqns4key)404405/*406* Build a list of APQNs based on a key type given.407* Build a list of APQNs based on a given key type and maybe further408* restrict the list by given master key verification patterns.409* For different key types there may be different ways to match the410* master key verification patterns. For CCA keys (CCA data key and CCA411* cipher key) the first 8 bytes of cur_mkvp refer to the current AES mkvp value412* of the apqn and the first 8 bytes of the alt_mkvp refer to the old AES mkvp.413* For CCA ECC keys it is similar but the match is against the APKA current/old414* mkvp. The flags argument controls if the apqns current and/or alternate mkvp415* should match. If the PKEY_FLAGS_MATCH_CUR_MKVP is given, only the current416* mkvp of each apqn is compared. Likewise with the PKEY_FLAGS_MATCH_ALT_MKVP.417* If both are given, it is assumed to return apqns where either the418* current or the alternate mkvp matches. If no match flag is given419* (flags is 0) the mkvp values are ignored for the match process.420* For EP11 keys there is only the current wkvp. So if the apqns should also421* match to a given wkvp, then the PKEY_FLAGS_MATCH_CUR_MKVP flag should be422* set. The wkvp value is 32 bytes but only the leftmost 16 bytes are compared423* against the leftmost 16 byte of the wkvp of the apqn.424* The list of matching apqns is stored into the space given by the apqns425* argument and the number of stored entries goes into apqn_entries. If the list426* is empty (apqn_entries is 0) the apqn_entries field is updated to the number427* of apqn targets found and the ioctl returns with 0. If apqn_entries is > 0428* but the number of apqn targets does not fit into the list, the apqn_targets429* field is updated with the number of required entries but there are no apqn430* values stored in the list and the ioctl returns with ENOSPC. If no matching431* APQN is found, the ioctl returns with 0 but the apqn_entries value is 0.432*/433struct pkey_apqns4keytype {434enum pkey_key_type type; /* in: key type */435__u8 cur_mkvp[32]; /* in: current mkvp */436__u8 alt_mkvp[32]; /* in: alternate mkvp */437__u32 flags; /* in: match controlling flags */438struct pkey_apqn __user *apqns; /* in/out: ptr to list of apqn targets*/439__u32 apqn_entries; /* in: max # of apqn entries in the list */440/* out: # apqns stored into the list */441};442443#define PKEY_APQNS4KT _IOWR(PKEY_IOCTL_MAGIC, 0x1C, struct pkey_apqns4keytype)444445/*446* Transform a key blob into a protected key, version 3.447* The difference to version 2 of this ioctl is that the protected key448* buffer is now explicitly and not within a struct pkey_protkey any more.449* So this ioctl is also able to handle EP11 and CCA ECC secure keys and450* provide ECC protected keys.451* There needs to be a list of apqns given with at least one entry in there.452* All apqns in the list need to be exact apqns, 0xFFFF as ANY card or domain453* is not supported. The implementation walks through the list of apqns and454* tries to send the request to each apqn without any further checking (like455* card type or online state). If the apqn fails, simple the next one in the456* list is tried until success (return 0) or the end of the list is reached457* (return -1 with errno ENODEV). You may use the PKEY_APQNS4K ioctl to458* generate a list of apqns based on the key.459*/460struct pkey_kblob2pkey3 {461__u8 __user *key; /* in: pointer to key blob */462__u32 keylen; /* in: key blob size */463struct pkey_apqn __user *apqns; /* in: ptr to list of apqn targets */464__u32 apqn_entries; /* in: # of apqn target list entries */465__u32 pkeytype; /* out: prot key type (enum pkey_key_type) */466__u32 pkeylen; /* in/out: size of pkey buffer/actual len of pkey */467__u8 __user *pkey; /* in: pkey blob buffer space ptr */468};469470#define PKEY_KBLOB2PROTK3 _IOWR(PKEY_IOCTL_MAGIC, 0x1D, struct pkey_kblob2pkey3)471472#endif /* _UAPI_PKEY_H */473474475