| 1 |
/* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 |
/* |
| 3 |
* Copyright IBM Corp. 2019 |
| 4 |
* Author(s): Harald Freudenberger <[email protected]> |
| 5 |
* Ingo Franzki <[email protected]> |
| 6 |
* |
| 7 |
* Collection of CCA misc functions used by zcrypt and pkey |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef _ZCRYPT_CCAMISC_H_ |
| 11 |
#define _ZCRYPT_CCAMISC_H_ |
| 12 |
|
| 13 |
#include <asm/zcrypt.h> |
| 14 |
#include <asm/pkey.h> |
| 15 |
#include "zcrypt_api.h" |
| 16 |
|
| 17 |
/* Key token types */ |
| 18 |
#define TOKTYPE_NON_CCA 0x00 /* Non-CCA key token */ |
| 19 |
#define TOKTYPE_CCA_INTERNAL 0x01 /* CCA internal sym key token */ |
| 20 |
#define TOKTYPE_CCA_INTERNAL_PKA 0x1f /* CCA internal asym key token */ |
| 21 |
|
| 22 |
/* For TOKTYPE_NON_CCA: */ |
| 23 |
#define TOKVER_PROTECTED_KEY 0x01 /* Protected key token */ |
| 24 |
#define TOKVER_CLEAR_KEY 0x02 /* Clear key token */ |
| 25 |
|
| 26 |
/* For TOKTYPE_CCA_INTERNAL: */ |
| 27 |
#define TOKVER_CCA_AES 0x04 /* CCA AES key token */ |
| 28 |
#define TOKVER_CCA_VLSC 0x05 /* var length sym cipher key token */ |
| 29 |
|
| 30 |
/* Max size of a cca variable length cipher key token */ |
| 31 |
#define MAXCCAVLSCTOKENSIZE 725 |
| 32 |
|
| 33 |
/* header part of a CCA key token */ |
| 34 |
struct keytoken_header { |
| 35 |
u8 type; /* one of the TOKTYPE values */ |
| 36 |
u8 res0[1]; |
| 37 |
u16 len; /* vlsc token: total length in bytes */ |
| 38 |
u8 version; /* one of the TOKVER values */ |
| 39 |
u8 res1[3]; |
| 40 |
} __packed; |
| 41 |
|
| 42 |
/* inside view of a CCA secure key token (only type 0x01 version 0x04) */ |
| 43 |
struct secaeskeytoken { |
| 44 |
u8 type; /* 0x01 for internal key token */ |
| 45 |
u8 res0[3]; |
| 46 |
u8 version; /* should be 0x04 */ |
| 47 |
u8 res1[1]; |
| 48 |
u8 flag; /* key flags */ |
| 49 |
u8 res2[1]; |
| 50 |
u64 mkvp; /* master key verification pattern */ |
| 51 |
u8 key[32]; /* key value (encrypted) */ |
| 52 |
u8 cv[8]; /* control vector */ |
| 53 |
u16 bitsize; /* key bit size */ |
| 54 |
u16 keysize; /* key byte size */ |
| 55 |
u8 tvv[4]; /* token validation value */ |
| 56 |
} __packed; |
| 57 |
|
| 58 |
/* inside view of a variable length symmetric cipher AES key token */ |
| 59 |
struct cipherkeytoken { |
| 60 |
u8 type; /* 0x01 for internal key token */ |
| 61 |
u8 res0[1]; |
| 62 |
u16 len; /* total key token length in bytes */ |
| 63 |
u8 version; /* should be 0x05 */ |
| 64 |
u8 res1[3]; |
| 65 |
u8 kms; /* key material state, 0x03 means wrapped with MK */ |
| 66 |
u8 kvpt; /* key verification pattern type, should be 0x01 */ |
| 67 |
u64 mkvp0; /* master key verification pattern, lo part */ |
| 68 |
u64 mkvp1; /* master key verification pattern, hi part (unused) */ |
| 69 |
u8 eskwm; /* encrypted section key wrapping method */ |
| 70 |
u8 hashalg; /* hash algorithmus used for wrapping key */ |
| 71 |
u8 plfver; /* pay load format version */ |
| 72 |
u8 res2[1]; |
| 73 |
u8 adsver; /* associated data section version */ |
| 74 |
u8 res3[1]; |
| 75 |
u16 adslen; /* associated data section length */ |
| 76 |
u8 kllen; /* optional key label length */ |
| 77 |
u8 ieaslen; /* optional extended associated data length */ |
| 78 |
u8 uadlen; /* optional user definable associated data length */ |
| 79 |
u8 res4[1]; |
| 80 |
u16 wpllen; /* wrapped payload length in bits: */ |
| 81 |
/* plfver 0x00 0x01 */ |
| 82 |
/* AES-128 512 640 */ |
| 83 |
/* AES-192 576 640 */ |
| 84 |
/* AES-256 640 640 */ |
| 85 |
u8 res5[1]; |
| 86 |
u8 algtype; /* 0x02 for AES cipher */ |
| 87 |
u16 keytype; /* 0x0001 for 'cipher' */ |
| 88 |
u8 kufc; /* key usage field count */ |
| 89 |
u16 kuf1; /* key usage field 1 */ |
| 90 |
u16 kuf2; /* key usage field 2 */ |
| 91 |
u8 kmfc; /* key management field count */ |
| 92 |
u16 kmf1; /* key management field 1 */ |
| 93 |
u16 kmf2; /* key management field 2 */ |
| 94 |
u16 kmf3; /* key management field 3 */ |
| 95 |
u8 vdata[]; /* variable part data follows */ |
| 96 |
} __packed; |
| 97 |
|
| 98 |
/* inside view of an CCA secure ECC private key */ |
| 99 |
struct eccprivkeytoken { |
| 100 |
u8 type; /* 0x1f for internal asym key token */ |
| 101 |
u8 version; /* should be 0x00 */ |
| 102 |
u16 len; /* total key token length in bytes */ |
| 103 |
u8 res1[4]; |
| 104 |
u8 secid; /* 0x20 for ECC priv key section marker */ |
| 105 |
u8 secver; /* section version */ |
| 106 |
u16 seclen; /* section length */ |
| 107 |
u8 wtype; /* wrapping method, 0x00 clear, 0x01 AES */ |
| 108 |
u8 htype; /* hash method, 0x02 for SHA-256 */ |
| 109 |
u8 res2[2]; |
| 110 |
u8 kutc; /* key usage and translation control */ |
| 111 |
u8 ctype; /* curve type */ |
| 112 |
u8 kfs; /* key format and security */ |
| 113 |
u8 ksrc; /* key source */ |
| 114 |
u16 pbitlen; /* length of prime p in bits */ |
| 115 |
u16 ibmadlen; /* IBM associated data length in bytes */ |
| 116 |
u64 mkvp; /* master key verification pattern */ |
| 117 |
u8 opk[48]; /* encrypted object protection key data */ |
| 118 |
u16 adatalen; /* associated data length in bytes */ |
| 119 |
u16 fseclen; /* formatted section length in bytes */ |
| 120 |
u8 more_data[]; /* more data follows */ |
| 121 |
} __packed; |
| 122 |
|
| 123 |
/* Some defines for the CCA AES cipherkeytoken kmf1 field */ |
| 124 |
#define KMF1_XPRT_SYM 0x8000 |
| 125 |
#define KMF1_XPRT_UASY 0x4000 |
| 126 |
#define KMF1_XPRT_AASY 0x2000 |
| 127 |
#define KMF1_XPRT_RAW 0x1000 |
| 128 |
#define KMF1_XPRT_CPAC 0x0800 |
| 129 |
#define KMF1_XPRT_DES 0x0080 |
| 130 |
#define KMF1_XPRT_AES 0x0040 |
| 131 |
#define KMF1_XPRT_RSA 0x0008 |
| 132 |
|
| 133 |
/* |
| 134 |
* Simple check if the token is a valid CCA secure AES data key |
| 135 |
* token. If keybitsize is given, the bitsize of the key is |
| 136 |
* also checked. Returns 0 on success or errno value on failure. |
| 137 |
*/ |
| 138 |
int cca_check_secaeskeytoken(debug_info_t *dbg, int dbflvl, |
| 139 |
const u8 *token, int keybitsize); |
| 140 |
|
| 141 |
/* |
| 142 |
* Simple check if the token is a valid CCA secure AES cipher key |
| 143 |
* token. If keybitsize is given, the bitsize of the key is |
| 144 |
* also checked. If checkcpacfexport is enabled, the key is also |
| 145 |
* checked for the export flag to allow CPACF export. |
| 146 |
* Returns 0 on success or errno value on failure. |
| 147 |
*/ |
| 148 |
int cca_check_secaescipherkey(debug_info_t *dbg, int dbflvl, |
| 149 |
const u8 *token, int keybitsize, |
| 150 |
int checkcpacfexport); |
| 151 |
|
| 152 |
/* |
| 153 |
* Simple check if the token is a valid CCA secure ECC private |
| 154 |
* key token. Returns 0 on success or errno value on failure. |
| 155 |
*/ |
| 156 |
int cca_check_sececckeytoken(debug_info_t *dbg, int dbflvl, |
| 157 |
const u8 *token, u32 keysize, |
| 158 |
int checkcpacfexport); |
| 159 |
|
| 160 |
/* |
| 161 |
* Generate (random) CCA AES DATA secure key. |
| 162 |
*/ |
| 163 |
int cca_genseckey(u16 cardnr, u16 domain, u32 keybitsize, u8 *seckey, |
| 164 |
u32 xflags); |
| 165 |
|
| 166 |
/* |
| 167 |
* Generate CCA AES DATA secure key with given clear key value. |
| 168 |
*/ |
| 169 |
int cca_clr2seckey(u16 cardnr, u16 domain, u32 keybitsize, |
| 170 |
const u8 *clrkey, u8 *seckey, u32 xflags); |
| 171 |
|
| 172 |
/* |
| 173 |
* Derive proteced key from an CCA AES DATA secure key. |
| 174 |
*/ |
| 175 |
int cca_sec2protkey(u16 cardnr, u16 domain, |
| 176 |
const u8 *seckey, u8 *protkey, u32 *protkeylen, |
| 177 |
u32 *protkeytype, u32 xflags); |
| 178 |
|
| 179 |
/* |
| 180 |
* Generate (random) CCA AES CIPHER secure key. |
| 181 |
*/ |
| 182 |
int cca_gencipherkey(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags, |
| 183 |
u8 *keybuf, u32 *keybufsize, u32 xflags); |
| 184 |
|
| 185 |
/* |
| 186 |
* Derive proteced key from CCA AES cipher secure key. |
| 187 |
*/ |
| 188 |
int cca_cipher2protkey(u16 cardnr, u16 domain, const u8 *ckey, |
| 189 |
u8 *protkey, u32 *protkeylen, u32 *protkeytype, |
| 190 |
u32 xflags); |
| 191 |
|
| 192 |
/* |
| 193 |
* Build CCA AES CIPHER secure key with a given clear key value. |
| 194 |
*/ |
| 195 |
int cca_clr2cipherkey(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags, |
| 196 |
const u8 *clrkey, u8 *keybuf, u32 *keybufsize, |
| 197 |
u32 xflags); |
| 198 |
|
| 199 |
/* |
| 200 |
* Derive proteced key from CCA ECC secure private key. |
| 201 |
*/ |
| 202 |
int cca_ecc2protkey(u16 cardnr, u16 domain, const u8 *key, |
| 203 |
u8 *protkey, u32 *protkeylen, u32 *protkeytype, u32 xflags); |
| 204 |
|
| 205 |
/* |
| 206 |
* Query cryptographic facility from CCA adapter |
| 207 |
*/ |
| 208 |
int cca_query_crypto_facility(u16 cardnr, u16 domain, |
| 209 |
const char *keyword, |
| 210 |
u8 *rarray, size_t *rarraylen, |
| 211 |
u8 *varray, size_t *varraylen, |
| 212 |
u32 xflags); |
| 213 |
|
| 214 |
/* |
| 215 |
* Build a list of cca apqns meeting the following constrains: |
| 216 |
* - apqn is online and is in fact a CCA apqn |
| 217 |
* - if cardnr is not FFFF only apqns with this cardnr |
| 218 |
* - if domain is not FFFF only apqns with this domainnr |
| 219 |
* - if minhwtype > 0 only apqns with hwtype >= minhwtype |
| 220 |
* - if cur_mkvp != 0 only apqns where cur_mkvp == mkvp |
| 221 |
* - if old_mkvp != 0 only apqns where old_mkvp == mkvp |
| 222 |
* The mktype determines which set of master keys to use: |
| 223 |
* 0 = AES_MK_SET - AES MK set, 1 = APKA MK_SET - APKA MK set |
| 224 |
* The caller should set *nr_apqns to the nr of elements available in *apqns. |
| 225 |
* On return *nr_apqns is then updated with the nr of apqns filled into *apqns. |
| 226 |
* The return value is either 0 for success or a negative errno value. |
| 227 |
* If no apqn meeting the criteria is found, -ENODEV is returned. |
| 228 |
*/ |
| 229 |
int cca_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain, |
| 230 |
int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp, |
| 231 |
u32 xflags); |
| 232 |
|
| 233 |
#define AES_MK_SET 0 |
| 234 |
#define APKA_MK_SET 1 |
| 235 |
|
| 236 |
/* struct to hold info for each CCA queue */ |
| 237 |
struct cca_info { |
| 238 |
int hwtype; /* one of the defined AP_DEVICE_TYPE_* */ |
| 239 |
char new_aes_mk_state; /* '1' empty, '2' partially full, '3' full */ |
| 240 |
char cur_aes_mk_state; /* '1' invalid, '2' valid */ |
| 241 |
char old_aes_mk_state; /* '1' invalid, '2' valid */ |
| 242 |
char new_apka_mk_state; /* '1' empty, '2' partially full, '3' full */ |
| 243 |
char cur_apka_mk_state; /* '1' invalid, '2' valid */ |
| 244 |
char old_apka_mk_state; /* '1' invalid, '2' valid */ |
| 245 |
char new_asym_mk_state; /* '1' empty, '2' partially full, '3' full */ |
| 246 |
char cur_asym_mk_state; /* '1' invalid, '2' valid */ |
| 247 |
char old_asym_mk_state; /* '1' invalid, '2' valid */ |
| 248 |
u64 new_aes_mkvp; /* truncated sha256 of new aes master key */ |
| 249 |
u64 cur_aes_mkvp; /* truncated sha256 of current aes master key */ |
| 250 |
u64 old_aes_mkvp; /* truncated sha256 of old aes master key */ |
| 251 |
u64 new_apka_mkvp; /* truncated sha256 of new apka master key */ |
| 252 |
u64 cur_apka_mkvp; /* truncated sha256 of current apka mk */ |
| 253 |
u64 old_apka_mkvp; /* truncated sha256 of old apka mk */ |
| 254 |
u8 new_asym_mkvp[16]; /* verify pattern of new asym master key */ |
| 255 |
u8 cur_asym_mkvp[16]; /* verify pattern of current asym master key */ |
| 256 |
u8 old_asym_mkvp[16]; /* verify pattern of old asym master key */ |
| 257 |
char serial[9]; /* serial number (8 ascii numbers + 0x00) */ |
| 258 |
}; |
| 259 |
|
| 260 |
/* |
| 261 |
* Fetch cca information about an CCA queue. |
| 262 |
*/ |
| 263 |
int cca_get_info(u16 card, u16 dom, struct cca_info *ci, u32 xflags); |
| 264 |
|
| 265 |
int zcrypt_ccamisc_init(void); |
| 266 |
void zcrypt_ccamisc_exit(void); |
| 267 |
|
| 268 |
#endif /* _ZCRYPT_CCAMISC_H_ */ |