18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2019 48c2ecf20Sopenharmony_ci * Author(s): Harald Freudenberger <freude@linux.ibm.com> 58c2ecf20Sopenharmony_ci * Ingo Franzki <ifranzki@linux.ibm.com> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Collection of CCA misc functions used by zcrypt and pkey 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#ifndef _ZCRYPT_CCAMISC_H_ 118c2ecf20Sopenharmony_ci#define _ZCRYPT_CCAMISC_H_ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <asm/zcrypt.h> 148c2ecf20Sopenharmony_ci#include <asm/pkey.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci/* Key token types */ 178c2ecf20Sopenharmony_ci#define TOKTYPE_NON_CCA 0x00 /* Non-CCA key token */ 188c2ecf20Sopenharmony_ci#define TOKTYPE_CCA_INTERNAL 0x01 /* CCA internal sym key token */ 198c2ecf20Sopenharmony_ci#define TOKTYPE_CCA_INTERNAL_PKA 0x1f /* CCA internal asym key token */ 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* For TOKTYPE_NON_CCA: */ 228c2ecf20Sopenharmony_ci#define TOKVER_PROTECTED_KEY 0x01 /* Protected key token */ 238c2ecf20Sopenharmony_ci#define TOKVER_CLEAR_KEY 0x02 /* Clear key token */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* For TOKTYPE_CCA_INTERNAL: */ 268c2ecf20Sopenharmony_ci#define TOKVER_CCA_AES 0x04 /* CCA AES key token */ 278c2ecf20Sopenharmony_ci#define TOKVER_CCA_VLSC 0x05 /* var length sym cipher key token */ 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* Max size of a cca variable length cipher key token */ 308c2ecf20Sopenharmony_ci#define MAXCCAVLSCTOKENSIZE 725 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* header part of a CCA key token */ 338c2ecf20Sopenharmony_cistruct keytoken_header { 348c2ecf20Sopenharmony_ci u8 type; /* one of the TOKTYPE values */ 358c2ecf20Sopenharmony_ci u8 res0[1]; 368c2ecf20Sopenharmony_ci u16 len; /* vlsc token: total length in bytes */ 378c2ecf20Sopenharmony_ci u8 version; /* one of the TOKVER values */ 388c2ecf20Sopenharmony_ci u8 res1[3]; 398c2ecf20Sopenharmony_ci} __packed; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* inside view of a CCA secure key token (only type 0x01 version 0x04) */ 428c2ecf20Sopenharmony_cistruct secaeskeytoken { 438c2ecf20Sopenharmony_ci u8 type; /* 0x01 for internal key token */ 448c2ecf20Sopenharmony_ci u8 res0[3]; 458c2ecf20Sopenharmony_ci u8 version; /* should be 0x04 */ 468c2ecf20Sopenharmony_ci u8 res1[1]; 478c2ecf20Sopenharmony_ci u8 flag; /* key flags */ 488c2ecf20Sopenharmony_ci u8 res2[1]; 498c2ecf20Sopenharmony_ci u64 mkvp; /* master key verification pattern */ 508c2ecf20Sopenharmony_ci u8 key[32]; /* key value (encrypted) */ 518c2ecf20Sopenharmony_ci u8 cv[8]; /* control vector */ 528c2ecf20Sopenharmony_ci u16 bitsize; /* key bit size */ 538c2ecf20Sopenharmony_ci u16 keysize; /* key byte size */ 548c2ecf20Sopenharmony_ci u8 tvv[4]; /* token validation value */ 558c2ecf20Sopenharmony_ci} __packed; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* inside view of a variable length symmetric cipher AES key token */ 588c2ecf20Sopenharmony_cistruct cipherkeytoken { 598c2ecf20Sopenharmony_ci u8 type; /* 0x01 for internal key token */ 608c2ecf20Sopenharmony_ci u8 res0[1]; 618c2ecf20Sopenharmony_ci u16 len; /* total key token length in bytes */ 628c2ecf20Sopenharmony_ci u8 version; /* should be 0x05 */ 638c2ecf20Sopenharmony_ci u8 res1[3]; 648c2ecf20Sopenharmony_ci u8 kms; /* key material state, 0x03 means wrapped with MK */ 658c2ecf20Sopenharmony_ci u8 kvpt; /* key verification pattern type, should be 0x01 */ 668c2ecf20Sopenharmony_ci u64 mkvp0; /* master key verification pattern, lo part */ 678c2ecf20Sopenharmony_ci u64 mkvp1; /* master key verification pattern, hi part (unused) */ 688c2ecf20Sopenharmony_ci u8 eskwm; /* encrypted section key wrapping method */ 698c2ecf20Sopenharmony_ci u8 hashalg; /* hash algorithmus used for wrapping key */ 708c2ecf20Sopenharmony_ci u8 plfver; /* pay load format version */ 718c2ecf20Sopenharmony_ci u8 res2[1]; 728c2ecf20Sopenharmony_ci u8 adsver; /* associated data section version */ 738c2ecf20Sopenharmony_ci u8 res3[1]; 748c2ecf20Sopenharmony_ci u16 adslen; /* associated data section length */ 758c2ecf20Sopenharmony_ci u8 kllen; /* optional key label length */ 768c2ecf20Sopenharmony_ci u8 ieaslen; /* optional extended associated data length */ 778c2ecf20Sopenharmony_ci u8 uadlen; /* optional user definable associated data length */ 788c2ecf20Sopenharmony_ci u8 res4[1]; 798c2ecf20Sopenharmony_ci u16 wpllen; /* wrapped payload length in bits: */ 808c2ecf20Sopenharmony_ci /* plfver 0x00 0x01 */ 818c2ecf20Sopenharmony_ci /* AES-128 512 640 */ 828c2ecf20Sopenharmony_ci /* AES-192 576 640 */ 838c2ecf20Sopenharmony_ci /* AES-256 640 640 */ 848c2ecf20Sopenharmony_ci u8 res5[1]; 858c2ecf20Sopenharmony_ci u8 algtype; /* 0x02 for AES cipher */ 868c2ecf20Sopenharmony_ci u16 keytype; /* 0x0001 for 'cipher' */ 878c2ecf20Sopenharmony_ci u8 kufc; /* key usage field count */ 888c2ecf20Sopenharmony_ci u16 kuf1; /* key usage field 1 */ 898c2ecf20Sopenharmony_ci u16 kuf2; /* key usage field 2 */ 908c2ecf20Sopenharmony_ci u8 kmfc; /* key management field count */ 918c2ecf20Sopenharmony_ci u16 kmf1; /* key management field 1 */ 928c2ecf20Sopenharmony_ci u16 kmf2; /* key management field 2 */ 938c2ecf20Sopenharmony_ci u16 kmf3; /* key management field 3 */ 948c2ecf20Sopenharmony_ci u8 vdata[]; /* variable part data follows */ 958c2ecf20Sopenharmony_ci} __packed; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* inside view of an CCA secure ECC private key */ 988c2ecf20Sopenharmony_cistruct eccprivkeytoken { 998c2ecf20Sopenharmony_ci u8 type; /* 0x1f for internal asym key token */ 1008c2ecf20Sopenharmony_ci u8 version; /* should be 0x00 */ 1018c2ecf20Sopenharmony_ci u16 len; /* total key token length in bytes */ 1028c2ecf20Sopenharmony_ci u8 res1[4]; 1038c2ecf20Sopenharmony_ci u8 secid; /* 0x20 for ECC priv key section marker */ 1048c2ecf20Sopenharmony_ci u8 secver; /* section version */ 1058c2ecf20Sopenharmony_ci u16 seclen; /* section length */ 1068c2ecf20Sopenharmony_ci u8 wtype; /* wrapping method, 0x00 clear, 0x01 AES */ 1078c2ecf20Sopenharmony_ci u8 htype; /* hash method, 0x02 for SHA-256 */ 1088c2ecf20Sopenharmony_ci u8 res2[2]; 1098c2ecf20Sopenharmony_ci u8 kutc; /* key usage and translation control */ 1108c2ecf20Sopenharmony_ci u8 ctype; /* curve type */ 1118c2ecf20Sopenharmony_ci u8 kfs; /* key format and security */ 1128c2ecf20Sopenharmony_ci u8 ksrc; /* key source */ 1138c2ecf20Sopenharmony_ci u16 pbitlen; /* length of prime p in bits */ 1148c2ecf20Sopenharmony_ci u16 ibmadlen; /* IBM associated data length in bytes */ 1158c2ecf20Sopenharmony_ci u64 mkvp; /* master key verification pattern */ 1168c2ecf20Sopenharmony_ci u8 opk[48]; /* encrypted object protection key data */ 1178c2ecf20Sopenharmony_ci u16 adatalen; /* associated data length in bytes */ 1188c2ecf20Sopenharmony_ci u16 fseclen; /* formated section length in bytes */ 1198c2ecf20Sopenharmony_ci u8 more_data[]; /* more data follows */ 1208c2ecf20Sopenharmony_ci} __packed; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci/* Some defines for the CCA AES cipherkeytoken kmf1 field */ 1238c2ecf20Sopenharmony_ci#define KMF1_XPRT_SYM 0x8000 1248c2ecf20Sopenharmony_ci#define KMF1_XPRT_UASY 0x4000 1258c2ecf20Sopenharmony_ci#define KMF1_XPRT_AASY 0x2000 1268c2ecf20Sopenharmony_ci#define KMF1_XPRT_RAW 0x1000 1278c2ecf20Sopenharmony_ci#define KMF1_XPRT_CPAC 0x0800 1288c2ecf20Sopenharmony_ci#define KMF1_XPRT_DES 0x0080 1298c2ecf20Sopenharmony_ci#define KMF1_XPRT_AES 0x0040 1308c2ecf20Sopenharmony_ci#define KMF1_XPRT_RSA 0x0008 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* 1338c2ecf20Sopenharmony_ci * Simple check if the token is a valid CCA secure AES data key 1348c2ecf20Sopenharmony_ci * token. If keybitsize is given, the bitsize of the key is 1358c2ecf20Sopenharmony_ci * also checked. Returns 0 on success or errno value on failure. 1368c2ecf20Sopenharmony_ci */ 1378c2ecf20Sopenharmony_ciint cca_check_secaeskeytoken(debug_info_t *dbg, int dbflvl, 1388c2ecf20Sopenharmony_ci const u8 *token, int keybitsize); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/* 1418c2ecf20Sopenharmony_ci * Simple check if the token is a valid CCA secure AES cipher key 1428c2ecf20Sopenharmony_ci * token. If keybitsize is given, the bitsize of the key is 1438c2ecf20Sopenharmony_ci * also checked. If checkcpacfexport is enabled, the key is also 1448c2ecf20Sopenharmony_ci * checked for the export flag to allow CPACF export. 1458c2ecf20Sopenharmony_ci * Returns 0 on success or errno value on failure. 1468c2ecf20Sopenharmony_ci */ 1478c2ecf20Sopenharmony_ciint cca_check_secaescipherkey(debug_info_t *dbg, int dbflvl, 1488c2ecf20Sopenharmony_ci const u8 *token, int keybitsize, 1498c2ecf20Sopenharmony_ci int checkcpacfexport); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* 1528c2ecf20Sopenharmony_ci * Simple check if the token is a valid CCA secure ECC private 1538c2ecf20Sopenharmony_ci * key token. Returns 0 on success or errno value on failure. 1548c2ecf20Sopenharmony_ci */ 1558c2ecf20Sopenharmony_ciint cca_check_sececckeytoken(debug_info_t *dbg, int dbflvl, 1568c2ecf20Sopenharmony_ci const u8 *token, size_t keysize, 1578c2ecf20Sopenharmony_ci int checkcpacfexport); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci/* 1608c2ecf20Sopenharmony_ci * Generate (random) CCA AES DATA secure key. 1618c2ecf20Sopenharmony_ci */ 1628c2ecf20Sopenharmony_ciint cca_genseckey(u16 cardnr, u16 domain, u32 keybitsize, u8 *seckey); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci/* 1658c2ecf20Sopenharmony_ci * Generate CCA AES DATA secure key with given clear key value. 1668c2ecf20Sopenharmony_ci */ 1678c2ecf20Sopenharmony_ciint cca_clr2seckey(u16 cardnr, u16 domain, u32 keybitsize, 1688c2ecf20Sopenharmony_ci const u8 *clrkey, u8 *seckey); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci/* 1718c2ecf20Sopenharmony_ci * Derive proteced key from an CCA AES DATA secure key. 1728c2ecf20Sopenharmony_ci */ 1738c2ecf20Sopenharmony_ciint cca_sec2protkey(u16 cardnr, u16 domain, 1748c2ecf20Sopenharmony_ci const u8 seckey[SECKEYBLOBSIZE], 1758c2ecf20Sopenharmony_ci u8 *protkey, u32 *protkeylen, u32 *protkeytype); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci/* 1788c2ecf20Sopenharmony_ci * Generate (random) CCA AES CIPHER secure key. 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_ciint cca_gencipherkey(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags, 1818c2ecf20Sopenharmony_ci u8 *keybuf, size_t *keybufsize); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci/* 1848c2ecf20Sopenharmony_ci * Derive proteced key from CCA AES cipher secure key. 1858c2ecf20Sopenharmony_ci */ 1868c2ecf20Sopenharmony_ciint cca_cipher2protkey(u16 cardnr, u16 domain, const u8 *ckey, 1878c2ecf20Sopenharmony_ci u8 *protkey, u32 *protkeylen, u32 *protkeytype); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci/* 1908c2ecf20Sopenharmony_ci * Build CCA AES CIPHER secure key with a given clear key value. 1918c2ecf20Sopenharmony_ci */ 1928c2ecf20Sopenharmony_ciint cca_clr2cipherkey(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags, 1938c2ecf20Sopenharmony_ci const u8 *clrkey, u8 *keybuf, size_t *keybufsize); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/* 1968c2ecf20Sopenharmony_ci * Derive proteced key from CCA ECC secure private key. 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_ciint cca_ecc2protkey(u16 cardnr, u16 domain, const u8 *key, 1998c2ecf20Sopenharmony_ci u8 *protkey, u32 *protkeylen, u32 *protkeytype); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/* 2028c2ecf20Sopenharmony_ci * Query cryptographic facility from CCA adapter 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_ciint cca_query_crypto_facility(u16 cardnr, u16 domain, 2058c2ecf20Sopenharmony_ci const char *keyword, 2068c2ecf20Sopenharmony_ci u8 *rarray, size_t *rarraylen, 2078c2ecf20Sopenharmony_ci u8 *varray, size_t *varraylen); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci/* 2108c2ecf20Sopenharmony_ci * Search for a matching crypto card based on the Master Key 2118c2ecf20Sopenharmony_ci * Verification Pattern provided inside a secure key. 2128c2ecf20Sopenharmony_ci * Works with CCA AES data and cipher keys. 2138c2ecf20Sopenharmony_ci * Returns < 0 on failure, 0 if CURRENT MKVP matches and 2148c2ecf20Sopenharmony_ci * 1 if OLD MKVP matches. 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_ciint cca_findcard(const u8 *key, u16 *pcardnr, u16 *pdomain, int verify); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci/* 2198c2ecf20Sopenharmony_ci * Build a list of cca apqns meeting the following constrains: 2208c2ecf20Sopenharmony_ci * - apqn is online and is in fact a CCA apqn 2218c2ecf20Sopenharmony_ci * - if cardnr is not FFFF only apqns with this cardnr 2228c2ecf20Sopenharmony_ci * - if domain is not FFFF only apqns with this domainnr 2238c2ecf20Sopenharmony_ci * - if minhwtype > 0 only apqns with hwtype >= minhwtype 2248c2ecf20Sopenharmony_ci * - if cur_mkvp != 0 only apqns where cur_mkvp == mkvp 2258c2ecf20Sopenharmony_ci * - if old_mkvp != 0 only apqns where old_mkvp == mkvp 2268c2ecf20Sopenharmony_ci * - if verify is enabled and a cur_mkvp and/or old_mkvp 2278c2ecf20Sopenharmony_ci * value is given, then refetch the cca_info and make sure the current 2288c2ecf20Sopenharmony_ci * cur_mkvp or old_mkvp values of the apqn are used. 2298c2ecf20Sopenharmony_ci * The mktype determines which set of master keys to use: 2308c2ecf20Sopenharmony_ci * 0 = AES_MK_SET - AES MK set, 1 = APKA MK_SET - APKA MK set 2318c2ecf20Sopenharmony_ci * The array of apqn entries is allocated with kmalloc and returned in *apqns; 2328c2ecf20Sopenharmony_ci * the number of apqns stored into the list is returned in *nr_apqns. One apqn 2338c2ecf20Sopenharmony_ci * entry is simple a 32 bit value with 16 bit cardnr and 16 bit domain nr and 2348c2ecf20Sopenharmony_ci * may be casted to struct pkey_apqn. The return value is either 0 for success 2358c2ecf20Sopenharmony_ci * or a negative errno value. If no apqn meeting the criterias is found, 2368c2ecf20Sopenharmony_ci * -ENODEV is returned. 2378c2ecf20Sopenharmony_ci */ 2388c2ecf20Sopenharmony_ciint cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain, 2398c2ecf20Sopenharmony_ci int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp, 2408c2ecf20Sopenharmony_ci int verify); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci#define AES_MK_SET 0 2438c2ecf20Sopenharmony_ci#define APKA_MK_SET 1 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci/* struct to hold info for each CCA queue */ 2468c2ecf20Sopenharmony_cistruct cca_info { 2478c2ecf20Sopenharmony_ci int hwtype; /* one of the defined AP_DEVICE_TYPE_* */ 2488c2ecf20Sopenharmony_ci char new_aes_mk_state; /* '1' empty, '2' partially full, '3' full */ 2498c2ecf20Sopenharmony_ci char cur_aes_mk_state; /* '1' invalid, '2' valid */ 2508c2ecf20Sopenharmony_ci char old_aes_mk_state; /* '1' invalid, '2' valid */ 2518c2ecf20Sopenharmony_ci char new_apka_mk_state; /* '1' empty, '2' partially full, '3' full */ 2528c2ecf20Sopenharmony_ci char cur_apka_mk_state; /* '1' invalid, '2' valid */ 2538c2ecf20Sopenharmony_ci char old_apka_mk_state; /* '1' invalid, '2' valid */ 2548c2ecf20Sopenharmony_ci u64 new_aes_mkvp; /* truncated sha256 of new aes master key */ 2558c2ecf20Sopenharmony_ci u64 cur_aes_mkvp; /* truncated sha256 of current aes master key */ 2568c2ecf20Sopenharmony_ci u64 old_aes_mkvp; /* truncated sha256 of old aes master key */ 2578c2ecf20Sopenharmony_ci u64 new_apka_mkvp; /* truncated sha256 of new apka master key */ 2588c2ecf20Sopenharmony_ci u64 cur_apka_mkvp; /* truncated sha256 of current apka mk */ 2598c2ecf20Sopenharmony_ci u64 old_apka_mkvp; /* truncated sha256 of old apka mk */ 2608c2ecf20Sopenharmony_ci char serial[9]; /* serial number (8 ascii numbers + 0x00) */ 2618c2ecf20Sopenharmony_ci}; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci/* 2648c2ecf20Sopenharmony_ci * Fetch cca information about an CCA queue. 2658c2ecf20Sopenharmony_ci */ 2668c2ecf20Sopenharmony_ciint cca_get_info(u16 card, u16 dom, struct cca_info *ci, int verify); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_civoid zcrypt_ccamisc_exit(void); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci#endif /* _ZCRYPT_CCAMISC_H_ */ 271