zcrypt_ccamisc.h 9.7 KB

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