1efc598e6SHarald Freudenberger /* SPDX-License-Identifier: GPL-2.0+ */ 2efc598e6SHarald Freudenberger /* 3efc598e6SHarald Freudenberger * Copyright IBM Corp. 2019 4efc598e6SHarald Freudenberger * Author(s): Harald Freudenberger <freude@linux.ibm.com> 5efc598e6SHarald Freudenberger * Ingo Franzki <ifranzki@linux.ibm.com> 6efc598e6SHarald Freudenberger * 7efc598e6SHarald Freudenberger * Collection of CCA misc functions used by zcrypt and pkey 8efc598e6SHarald Freudenberger */ 9efc598e6SHarald Freudenberger 10efc598e6SHarald Freudenberger #ifndef _ZCRYPT_CCAMISC_H_ 11efc598e6SHarald Freudenberger #define _ZCRYPT_CCAMISC_H_ 12efc598e6SHarald Freudenberger 13efc598e6SHarald Freudenberger #include <asm/zcrypt.h> 14efc598e6SHarald Freudenberger #include <asm/pkey.h> 15efc598e6SHarald Freudenberger 16efc598e6SHarald Freudenberger /* Key token types */ 17efc598e6SHarald Freudenberger #define TOKTYPE_NON_CCA 0x00 /* Non-CCA key token */ 18fa6999e3SHarald Freudenberger #define TOKTYPE_CCA_INTERNAL 0x01 /* CCA internal sym key token */ 19fa6999e3SHarald Freudenberger #define TOKTYPE_CCA_INTERNAL_PKA 0x1f /* CCA internal asym key token */ 20efc598e6SHarald Freudenberger 21efc598e6SHarald Freudenberger /* For TOKTYPE_NON_CCA: */ 22efc598e6SHarald Freudenberger #define TOKVER_PROTECTED_KEY 0x01 /* Protected key token */ 23888edbc4SHarald Freudenberger #define TOKVER_CLEAR_KEY 0x02 /* Clear key token */ 24efc598e6SHarald Freudenberger 25efc598e6SHarald Freudenberger /* For TOKTYPE_CCA_INTERNAL: */ 26efc598e6SHarald Freudenberger #define TOKVER_CCA_AES 0x04 /* CCA AES key token */ 274bc123b1SHarald Freudenberger #define TOKVER_CCA_VLSC 0x05 /* var length sym cipher key token */ 284bc123b1SHarald Freudenberger 294bc123b1SHarald Freudenberger /* Max size of a cca variable length cipher key token */ 304bc123b1SHarald Freudenberger #define MAXCCAVLSCTOKENSIZE 725 31efc598e6SHarald Freudenberger 32efc598e6SHarald Freudenberger /* header part of a CCA key token */ 33efc598e6SHarald Freudenberger struct keytoken_header { 34efc598e6SHarald Freudenberger u8 type; /* one of the TOKTYPE values */ 354bc123b1SHarald Freudenberger u8 res0[1]; 364bc123b1SHarald Freudenberger u16 len; /* vlsc token: total length in bytes */ 37efc598e6SHarald Freudenberger u8 version; /* one of the TOKVER values */ 38efc598e6SHarald Freudenberger u8 res1[3]; 39efc598e6SHarald Freudenberger } __packed; 40efc598e6SHarald Freudenberger 41efc598e6SHarald Freudenberger /* inside view of a CCA secure key token (only type 0x01 version 0x04) */ 42efc598e6SHarald Freudenberger struct secaeskeytoken { 43efc598e6SHarald Freudenberger u8 type; /* 0x01 for internal key token */ 44efc598e6SHarald Freudenberger u8 res0[3]; 45efc598e6SHarald Freudenberger u8 version; /* should be 0x04 */ 46efc598e6SHarald Freudenberger u8 res1[1]; 47efc598e6SHarald Freudenberger u8 flag; /* key flags */ 48efc598e6SHarald Freudenberger u8 res2[1]; 49efc598e6SHarald Freudenberger u64 mkvp; /* master key verification pattern */ 50efc598e6SHarald Freudenberger u8 key[32]; /* key value (encrypted) */ 51efc598e6SHarald Freudenberger u8 cv[8]; /* control vector */ 52efc598e6SHarald Freudenberger u16 bitsize; /* key bit size */ 53efc598e6SHarald Freudenberger u16 keysize; /* key byte size */ 54efc598e6SHarald Freudenberger u8 tvv[4]; /* token validation value */ 55efc598e6SHarald Freudenberger } __packed; 56efc598e6SHarald Freudenberger 574bc123b1SHarald Freudenberger /* inside view of a variable length symmetric cipher AES key token */ 584bc123b1SHarald Freudenberger struct cipherkeytoken { 594bc123b1SHarald Freudenberger u8 type; /* 0x01 for internal key token */ 604bc123b1SHarald Freudenberger u8 res0[1]; 614bc123b1SHarald Freudenberger u16 len; /* total key token length in bytes */ 624bc123b1SHarald Freudenberger u8 version; /* should be 0x05 */ 634bc123b1SHarald Freudenberger u8 res1[3]; 644bc123b1SHarald Freudenberger u8 kms; /* key material state, 0x03 means wrapped with MK */ 654bc123b1SHarald Freudenberger u8 kvpt; /* key verification pattern type, should be 0x01 */ 664bc123b1SHarald Freudenberger u64 mkvp0; /* master key verification pattern, lo part */ 674bc123b1SHarald Freudenberger u64 mkvp1; /* master key verification pattern, hi part (unused) */ 684bc123b1SHarald Freudenberger u8 eskwm; /* encrypted section key wrapping method */ 694bc123b1SHarald Freudenberger u8 hashalg; /* hash algorithmus used for wrapping key */ 704bc123b1SHarald Freudenberger u8 plfver; /* pay load format version */ 714bc123b1SHarald Freudenberger u8 res2[1]; 724bc123b1SHarald Freudenberger u8 adsver; /* associated data section version */ 734bc123b1SHarald Freudenberger u8 res3[1]; 744bc123b1SHarald Freudenberger u16 adslen; /* associated data section length */ 754bc123b1SHarald Freudenberger u8 kllen; /* optional key label length */ 764bc123b1SHarald Freudenberger u8 ieaslen; /* optional extended associated data length */ 774bc123b1SHarald Freudenberger u8 uadlen; /* optional user definable associated data length */ 784bc123b1SHarald Freudenberger u8 res4[1]; 794bc123b1SHarald Freudenberger u16 wpllen; /* wrapped payload length in bits: */ 804bc123b1SHarald Freudenberger /* plfver 0x00 0x01 */ 814bc123b1SHarald Freudenberger /* AES-128 512 640 */ 824bc123b1SHarald Freudenberger /* AES-192 576 640 */ 834bc123b1SHarald Freudenberger /* AES-256 640 640 */ 844bc123b1SHarald Freudenberger u8 res5[1]; 854bc123b1SHarald Freudenberger u8 algtype; /* 0x02 for AES cipher */ 864bc123b1SHarald Freudenberger u16 keytype; /* 0x0001 for 'cipher' */ 874bc123b1SHarald Freudenberger u8 kufc; /* key usage field count */ 884bc123b1SHarald Freudenberger u16 kuf1; /* key usage field 1 */ 894bc123b1SHarald Freudenberger u16 kuf2; /* key usage field 2 */ 904bc123b1SHarald Freudenberger u8 kmfc; /* key management field count */ 914bc123b1SHarald Freudenberger u16 kmf1; /* key management field 1 */ 924bc123b1SHarald Freudenberger u16 kmf2; /* key management field 2 */ 934bc123b1SHarald Freudenberger u16 kmf3; /* key management field 3 */ 94fa226f1dSGustavo A. R. Silva u8 vdata[]; /* variable part data follows */ 954bc123b1SHarald Freudenberger } __packed; 964bc123b1SHarald Freudenberger 97fa6999e3SHarald Freudenberger /* inside view of an CCA secure ECC private key */ 98fa6999e3SHarald Freudenberger struct eccprivkeytoken { 99fa6999e3SHarald Freudenberger u8 type; /* 0x1f for internal asym key token */ 100fa6999e3SHarald Freudenberger u8 version; /* should be 0x00 */ 101fa6999e3SHarald Freudenberger u16 len; /* total key token length in bytes */ 102fa6999e3SHarald Freudenberger u8 res1[4]; 103fa6999e3SHarald Freudenberger u8 secid; /* 0x20 for ECC priv key section marker */ 104fa6999e3SHarald Freudenberger u8 secver; /* section version */ 105fa6999e3SHarald Freudenberger u16 seclen; /* section length */ 106fa6999e3SHarald Freudenberger u8 wtype; /* wrapping method, 0x00 clear, 0x01 AES */ 107fa6999e3SHarald Freudenberger u8 htype; /* hash method, 0x02 for SHA-256 */ 108fa6999e3SHarald Freudenberger u8 res2[2]; 109fa6999e3SHarald Freudenberger u8 kutc; /* key usage and translation control */ 110fa6999e3SHarald Freudenberger u8 ctype; /* curve type */ 111fa6999e3SHarald Freudenberger u8 kfs; /* key format and security */ 112fa6999e3SHarald Freudenberger u8 ksrc; /* key source */ 113fa6999e3SHarald Freudenberger u16 pbitlen; /* length of prime p in bits */ 114fa6999e3SHarald Freudenberger u16 ibmadlen; /* IBM associated data length in bytes */ 115fa6999e3SHarald Freudenberger u64 mkvp; /* master key verification pattern */ 116fa6999e3SHarald Freudenberger u8 opk[48]; /* encrypted object protection key data */ 117fa6999e3SHarald Freudenberger u16 adatalen; /* associated data length in bytes */ 118*cada938aSHeiko Carstens u16 fseclen; /* formatted section length in bytes */ 119fa6999e3SHarald Freudenberger u8 more_data[]; /* more data follows */ 120fa6999e3SHarald Freudenberger } __packed; 121fa6999e3SHarald Freudenberger 1224bc123b1SHarald Freudenberger /* Some defines for the CCA AES cipherkeytoken kmf1 field */ 1234bc123b1SHarald Freudenberger #define KMF1_XPRT_SYM 0x8000 1244bc123b1SHarald Freudenberger #define KMF1_XPRT_UASY 0x4000 1254bc123b1SHarald Freudenberger #define KMF1_XPRT_AASY 0x2000 1264bc123b1SHarald Freudenberger #define KMF1_XPRT_RAW 0x1000 1274bc123b1SHarald Freudenberger #define KMF1_XPRT_CPAC 0x0800 1284bc123b1SHarald Freudenberger #define KMF1_XPRT_DES 0x0080 1294bc123b1SHarald Freudenberger #define KMF1_XPRT_AES 0x0040 1304bc123b1SHarald Freudenberger #define KMF1_XPRT_RSA 0x0008 1314bc123b1SHarald Freudenberger 132efc598e6SHarald Freudenberger /* 133efc598e6SHarald Freudenberger * Simple check if the token is a valid CCA secure AES data key 134efc598e6SHarald Freudenberger * token. If keybitsize is given, the bitsize of the key is 135efc598e6SHarald Freudenberger * also checked. Returns 0 on success or errno value on failure. 136efc598e6SHarald Freudenberger */ 137efc598e6SHarald Freudenberger int cca_check_secaeskeytoken(debug_info_t *dbg, int dbflvl, 138efc598e6SHarald Freudenberger const u8 *token, int keybitsize); 139efc598e6SHarald Freudenberger 140efc598e6SHarald Freudenberger /* 1414bc123b1SHarald Freudenberger * Simple check if the token is a valid CCA secure AES cipher key 1424bc123b1SHarald Freudenberger * token. If keybitsize is given, the bitsize of the key is 1434bc123b1SHarald Freudenberger * also checked. If checkcpacfexport is enabled, the key is also 1444bc123b1SHarald Freudenberger * checked for the export flag to allow CPACF export. 1454bc123b1SHarald Freudenberger * Returns 0 on success or errno value on failure. 1464bc123b1SHarald Freudenberger */ 1474bc123b1SHarald Freudenberger int cca_check_secaescipherkey(debug_info_t *dbg, int dbflvl, 1484bc123b1SHarald Freudenberger const u8 *token, int keybitsize, 1494bc123b1SHarald Freudenberger int checkcpacfexport); 1504bc123b1SHarald Freudenberger 1514bc123b1SHarald Freudenberger /* 152fa6999e3SHarald Freudenberger * Simple check if the token is a valid CCA secure ECC private 153fa6999e3SHarald Freudenberger * key token. Returns 0 on success or errno value on failure. 154fa6999e3SHarald Freudenberger */ 155fa6999e3SHarald Freudenberger int cca_check_sececckeytoken(debug_info_t *dbg, int dbflvl, 156fa6999e3SHarald Freudenberger const u8 *token, size_t keysize, 157fa6999e3SHarald Freudenberger int checkcpacfexport); 158fa6999e3SHarald Freudenberger 159fa6999e3SHarald Freudenberger /* 160efc598e6SHarald Freudenberger * Generate (random) CCA AES DATA secure key. 161efc598e6SHarald Freudenberger */ 162f2bbc96eSHarald Freudenberger int cca_genseckey(u16 cardnr, u16 domain, u32 keybitsize, u8 *seckey); 163efc598e6SHarald Freudenberger 164efc598e6SHarald Freudenberger /* 165efc598e6SHarald Freudenberger * Generate CCA AES DATA secure key with given clear key value. 166efc598e6SHarald Freudenberger */ 167f2bbc96eSHarald Freudenberger int cca_clr2seckey(u16 cardnr, u16 domain, u32 keybitsize, 168efc598e6SHarald Freudenberger const u8 *clrkey, u8 *seckey); 169efc598e6SHarald Freudenberger 170efc598e6SHarald Freudenberger /* 171efc598e6SHarald Freudenberger * Derive proteced key from an CCA AES DATA secure key. 172efc598e6SHarald Freudenberger */ 173efc598e6SHarald Freudenberger int cca_sec2protkey(u16 cardnr, u16 domain, 174a237283fSSven Schnelle const u8 *seckey, u8 *protkey, u32 *protkeylen, 175a237283fSSven Schnelle u32 *protkeytype); 176efc598e6SHarald Freudenberger 177efc598e6SHarald Freudenberger /* 1784bc123b1SHarald Freudenberger * Generate (random) CCA AES CIPHER secure key. 1794bc123b1SHarald Freudenberger */ 1804bc123b1SHarald Freudenberger int cca_gencipherkey(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags, 1814bc123b1SHarald Freudenberger u8 *keybuf, size_t *keybufsize); 1824bc123b1SHarald Freudenberger 1834bc123b1SHarald Freudenberger /* 1844bc123b1SHarald Freudenberger * Derive proteced key from CCA AES cipher secure key. 1854bc123b1SHarald Freudenberger */ 1864bc123b1SHarald Freudenberger int cca_cipher2protkey(u16 cardnr, u16 domain, const u8 *ckey, 1874bc123b1SHarald Freudenberger u8 *protkey, u32 *protkeylen, u32 *protkeytype); 1884bc123b1SHarald Freudenberger 1894bc123b1SHarald Freudenberger /* 1904bc123b1SHarald Freudenberger * Build CCA AES CIPHER secure key with a given clear key value. 1914bc123b1SHarald Freudenberger */ 1924bc123b1SHarald Freudenberger int cca_clr2cipherkey(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags, 1934bc123b1SHarald Freudenberger const u8 *clrkey, u8 *keybuf, size_t *keybufsize); 1944bc123b1SHarald Freudenberger 1954bc123b1SHarald Freudenberger /* 196fa6999e3SHarald Freudenberger * Derive proteced key from CCA ECC secure private key. 197fa6999e3SHarald Freudenberger */ 198fa6999e3SHarald Freudenberger int cca_ecc2protkey(u16 cardnr, u16 domain, const u8 *key, 199fa6999e3SHarald Freudenberger u8 *protkey, u32 *protkeylen, u32 *protkeytype); 200fa6999e3SHarald Freudenberger 201fa6999e3SHarald Freudenberger /* 202efc598e6SHarald Freudenberger * Query cryptographic facility from CCA adapter 203efc598e6SHarald Freudenberger */ 204efc598e6SHarald Freudenberger int cca_query_crypto_facility(u16 cardnr, u16 domain, 205efc598e6SHarald Freudenberger const char *keyword, 206efc598e6SHarald Freudenberger u8 *rarray, size_t *rarraylen, 207efc598e6SHarald Freudenberger u8 *varray, size_t *varraylen); 208efc598e6SHarald Freudenberger 209efc598e6SHarald Freudenberger /* 210efc598e6SHarald Freudenberger * Search for a matching crypto card based on the Master Key 211efc598e6SHarald Freudenberger * Verification Pattern provided inside a secure key. 212f2bbc96eSHarald Freudenberger * Works with CCA AES data and cipher keys. 213efc598e6SHarald Freudenberger * Returns < 0 on failure, 0 if CURRENT MKVP matches and 214efc598e6SHarald Freudenberger * 1 if OLD MKVP matches. 215efc598e6SHarald Freudenberger */ 2164da57a2fSHarald Freudenberger int cca_findcard(const u8 *key, u16 *pcardnr, u16 *pdomain, int verify); 217efc598e6SHarald Freudenberger 2184bc123b1SHarald Freudenberger /* 2194bc123b1SHarald Freudenberger * Build a list of cca apqns meeting the following constrains: 2204bc123b1SHarald Freudenberger * - apqn is online and is in fact a CCA apqn 2214bc123b1SHarald Freudenberger * - if cardnr is not FFFF only apqns with this cardnr 2224bc123b1SHarald Freudenberger * - if domain is not FFFF only apqns with this domainnr 2234bc123b1SHarald Freudenberger * - if minhwtype > 0 only apqns with hwtype >= minhwtype 2244bc123b1SHarald Freudenberger * - if cur_mkvp != 0 only apqns where cur_mkvp == mkvp 2254bc123b1SHarald Freudenberger * - if old_mkvp != 0 only apqns where old_mkvp == mkvp 2264bc123b1SHarald Freudenberger * - if verify is enabled and a cur_mkvp and/or old_mkvp 2274bc123b1SHarald Freudenberger * value is given, then refetch the cca_info and make sure the current 2284bc123b1SHarald Freudenberger * cur_mkvp or old_mkvp values of the apqn are used. 22932ca04bbSHarald Freudenberger * The mktype determines which set of master keys to use: 23032ca04bbSHarald Freudenberger * 0 = AES_MK_SET - AES MK set, 1 = APKA MK_SET - APKA MK set 2314bc123b1SHarald Freudenberger * The array of apqn entries is allocated with kmalloc and returned in *apqns; 2324bc123b1SHarald Freudenberger * the number of apqns stored into the list is returned in *nr_apqns. One apqn 2334bc123b1SHarald Freudenberger * entry is simple a 32 bit value with 16 bit cardnr and 16 bit domain nr and 2344bc123b1SHarald Freudenberger * may be casted to struct pkey_apqn. The return value is either 0 for success 235*cada938aSHeiko Carstens * or a negative errno value. If no apqn meeting the criteria is found, 2364bc123b1SHarald Freudenberger * -ENODEV is returned. 2374bc123b1SHarald Freudenberger */ 2384bc123b1SHarald Freudenberger int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain, 23932ca04bbSHarald Freudenberger int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp, 24032ca04bbSHarald Freudenberger int verify); 24132ca04bbSHarald Freudenberger 24232ca04bbSHarald Freudenberger #define AES_MK_SET 0 24332ca04bbSHarald Freudenberger #define APKA_MK_SET 1 2444bc123b1SHarald Freudenberger 2457c4e91c0SHarald Freudenberger /* struct to hold info for each CCA queue */ 2467c4e91c0SHarald Freudenberger struct cca_info { 2474da57a2fSHarald Freudenberger int hwtype; /* one of the defined AP_DEVICE_TYPE_* */ 24832ca04bbSHarald Freudenberger char new_aes_mk_state; /* '1' empty, '2' partially full, '3' full */ 24932ca04bbSHarald Freudenberger char cur_aes_mk_state; /* '1' invalid, '2' valid */ 25032ca04bbSHarald Freudenberger char old_aes_mk_state; /* '1' invalid, '2' valid */ 25132ca04bbSHarald Freudenberger char new_apka_mk_state; /* '1' empty, '2' partially full, '3' full */ 25232ca04bbSHarald Freudenberger char cur_apka_mk_state; /* '1' invalid, '2' valid */ 25332ca04bbSHarald Freudenberger char old_apka_mk_state; /* '1' invalid, '2' valid */ 25428d3417aSHarald Freudenberger char new_asym_mk_state; /* '1' empty, '2' partially full, '3' full */ 25528d3417aSHarald Freudenberger char cur_asym_mk_state; /* '1' invalid, '2' valid */ 25628d3417aSHarald Freudenberger char old_asym_mk_state; /* '1' invalid, '2' valid */ 25732ca04bbSHarald Freudenberger u64 new_aes_mkvp; /* truncated sha256 of new aes master key */ 25832ca04bbSHarald Freudenberger u64 cur_aes_mkvp; /* truncated sha256 of current aes master key */ 25932ca04bbSHarald Freudenberger u64 old_aes_mkvp; /* truncated sha256 of old aes master key */ 26032ca04bbSHarald Freudenberger u64 new_apka_mkvp; /* truncated sha256 of new apka master key */ 26132ca04bbSHarald Freudenberger u64 cur_apka_mkvp; /* truncated sha256 of current apka mk */ 26232ca04bbSHarald Freudenberger u64 old_apka_mkvp; /* truncated sha256 of old apka mk */ 26328d3417aSHarald Freudenberger u8 new_asym_mkvp[16]; /* verify pattern of new asym master key */ 26428d3417aSHarald Freudenberger u8 cur_asym_mkvp[16]; /* verify pattern of current asym master key */ 26528d3417aSHarald Freudenberger u8 old_asym_mkvp[16]; /* verify pattern of old asym master key */ 26632ca04bbSHarald Freudenberger char serial[9]; /* serial number (8 ascii numbers + 0x00) */ 2677c4e91c0SHarald Freudenberger }; 2687c4e91c0SHarald Freudenberger 2697c4e91c0SHarald Freudenberger /* 2707c4e91c0SHarald Freudenberger * Fetch cca information about an CCA queue. 2717c4e91c0SHarald Freudenberger */ 2727c4e91c0SHarald Freudenberger int cca_get_info(u16 card, u16 dom, struct cca_info *ci, int verify); 2737c4e91c0SHarald Freudenberger 274efc598e6SHarald Freudenberger void zcrypt_ccamisc_exit(void); 275efc598e6SHarald Freudenberger 276efc598e6SHarald Freudenberger #endif /* _ZCRYPT_CCAMISC_H_ */ 277