1e80d4af0SHarald Freudenberger /* 2e80d4af0SHarald Freudenberger * pkey device driver 3e80d4af0SHarald Freudenberger * 4e80d4af0SHarald Freudenberger * Copyright IBM Corp. 2017 5e80d4af0SHarald Freudenberger * Author(s): Harald Freudenberger 6e80d4af0SHarald Freudenberger * 7e80d4af0SHarald Freudenberger * This program is free software; you can redistribute it and/or modify 8e80d4af0SHarald Freudenberger * it under the terms of the GNU General Public License (version 2 only) 9e80d4af0SHarald Freudenberger * as published by the Free Software Foundation. 10e80d4af0SHarald Freudenberger * 11e80d4af0SHarald Freudenberger */ 12e80d4af0SHarald Freudenberger 13e80d4af0SHarald Freudenberger #define KMSG_COMPONENT "pkey" 14e80d4af0SHarald Freudenberger #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 15e80d4af0SHarald Freudenberger 16e80d4af0SHarald Freudenberger #include <linux/fs.h> 17e80d4af0SHarald Freudenberger #include <linux/init.h> 18e80d4af0SHarald Freudenberger #include <linux/miscdevice.h> 19e80d4af0SHarald Freudenberger #include <linux/module.h> 20e80d4af0SHarald Freudenberger #include <linux/slab.h> 21e80d4af0SHarald Freudenberger #include <linux/kallsyms.h> 22e80d4af0SHarald Freudenberger #include <linux/debugfs.h> 23e80d4af0SHarald Freudenberger #include <asm/zcrypt.h> 24e80d4af0SHarald Freudenberger #include <asm/cpacf.h> 25e80d4af0SHarald Freudenberger #include <asm/pkey.h> 26e80d4af0SHarald Freudenberger 27e80d4af0SHarald Freudenberger #include "zcrypt_api.h" 28e80d4af0SHarald Freudenberger 29e80d4af0SHarald Freudenberger MODULE_LICENSE("GPL"); 30e80d4af0SHarald Freudenberger MODULE_AUTHOR("IBM Corporation"); 31e80d4af0SHarald Freudenberger MODULE_DESCRIPTION("s390 protected key interface"); 32e80d4af0SHarald Freudenberger 33e80d4af0SHarald Freudenberger /* Size of parameter block used for all cca requests/replies */ 34e80d4af0SHarald Freudenberger #define PARMBSIZE 512 35e80d4af0SHarald Freudenberger 36e80d4af0SHarald Freudenberger /* Size of vardata block used for some of the cca requests/replies */ 37e80d4af0SHarald Freudenberger #define VARDATASIZE 4096 38e80d4af0SHarald Freudenberger 39e80d4af0SHarald Freudenberger /* 40e80d4af0SHarald Freudenberger * debug feature data and functions 41e80d4af0SHarald Freudenberger */ 42e80d4af0SHarald Freudenberger 43e80d4af0SHarald Freudenberger static debug_info_t *debug_info; 44e80d4af0SHarald Freudenberger 45e80d4af0SHarald Freudenberger #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__) 46e80d4af0SHarald Freudenberger #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__) 47e80d4af0SHarald Freudenberger #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__) 48e80d4af0SHarald Freudenberger #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__) 49e80d4af0SHarald Freudenberger 50e80d4af0SHarald Freudenberger static void __init pkey_debug_init(void) 51e80d4af0SHarald Freudenberger { 52e80d4af0SHarald Freudenberger debug_info = debug_register("pkey", 1, 1, 4 * sizeof(long)); 53e80d4af0SHarald Freudenberger debug_register_view(debug_info, &debug_sprintf_view); 54e80d4af0SHarald Freudenberger debug_set_level(debug_info, 3); 55e80d4af0SHarald Freudenberger } 56e80d4af0SHarald Freudenberger 57e80d4af0SHarald Freudenberger static void __exit pkey_debug_exit(void) 58e80d4af0SHarald Freudenberger { 59e80d4af0SHarald Freudenberger debug_unregister(debug_info); 60e80d4af0SHarald Freudenberger } 61e80d4af0SHarald Freudenberger 62e80d4af0SHarald Freudenberger /* inside view of a secure key token (only type 0x01 version 0x04) */ 63e80d4af0SHarald Freudenberger struct secaeskeytoken { 64e80d4af0SHarald Freudenberger u8 type; /* 0x01 for internal key token */ 65e80d4af0SHarald Freudenberger u8 res0[3]; 66e80d4af0SHarald Freudenberger u8 version; /* should be 0x04 */ 67e80d4af0SHarald Freudenberger u8 res1[1]; 68e80d4af0SHarald Freudenberger u8 flag; /* key flags */ 69e80d4af0SHarald Freudenberger u8 res2[1]; 70e80d4af0SHarald Freudenberger u64 mkvp; /* master key verification pattern */ 71e80d4af0SHarald Freudenberger u8 key[32]; /* key value (encrypted) */ 72e80d4af0SHarald Freudenberger u8 cv[8]; /* control vector */ 73e80d4af0SHarald Freudenberger u16 bitsize; /* key bit size */ 74e80d4af0SHarald Freudenberger u16 keysize; /* key byte size */ 75e80d4af0SHarald Freudenberger u8 tvv[4]; /* token validation value */ 76e80d4af0SHarald Freudenberger } __packed; 77e80d4af0SHarald Freudenberger 78e80d4af0SHarald Freudenberger /* 79e80d4af0SHarald Freudenberger * Simple check if the token is a valid CCA secure AES key 80e80d4af0SHarald Freudenberger * token. If keybitsize is given, the bitsize of the key is 81e80d4af0SHarald Freudenberger * also checked. Returns 0 on success or errno value on failure. 82e80d4af0SHarald Freudenberger */ 83e80d4af0SHarald Freudenberger static int check_secaeskeytoken(u8 *token, int keybitsize) 84e80d4af0SHarald Freudenberger { 85e80d4af0SHarald Freudenberger struct secaeskeytoken *t = (struct secaeskeytoken *) token; 86e80d4af0SHarald Freudenberger 87e80d4af0SHarald Freudenberger if (t->type != 0x01) { 88e80d4af0SHarald Freudenberger DEBUG_ERR( 89e80d4af0SHarald Freudenberger "check_secaeskeytoken secure token check failed, type mismatch 0x%02x != 0x01\n", 90e80d4af0SHarald Freudenberger (int) t->type); 91e80d4af0SHarald Freudenberger return -EINVAL; 92e80d4af0SHarald Freudenberger } 93e80d4af0SHarald Freudenberger if (t->version != 0x04) { 94e80d4af0SHarald Freudenberger DEBUG_ERR( 95e80d4af0SHarald Freudenberger "check_secaeskeytoken secure token check failed, version mismatch 0x%02x != 0x04\n", 96e80d4af0SHarald Freudenberger (int) t->version); 97e80d4af0SHarald Freudenberger return -EINVAL; 98e80d4af0SHarald Freudenberger } 99e80d4af0SHarald Freudenberger if (keybitsize > 0 && t->bitsize != keybitsize) { 100e80d4af0SHarald Freudenberger DEBUG_ERR( 101e80d4af0SHarald Freudenberger "check_secaeskeytoken secure token check failed, bitsize mismatch %d != %d\n", 102e80d4af0SHarald Freudenberger (int) t->bitsize, keybitsize); 103e80d4af0SHarald Freudenberger return -EINVAL; 104e80d4af0SHarald Freudenberger } 105e80d4af0SHarald Freudenberger 106e80d4af0SHarald Freudenberger return 0; 107e80d4af0SHarald Freudenberger } 108e80d4af0SHarald Freudenberger 109e80d4af0SHarald Freudenberger /* 110e80d4af0SHarald Freudenberger * Allocate consecutive memory for request CPRB, request param 111e80d4af0SHarald Freudenberger * block, reply CPRB and reply param block and fill in values 112e80d4af0SHarald Freudenberger * for the common fields. Returns 0 on success or errno value 113e80d4af0SHarald Freudenberger * on failure. 114e80d4af0SHarald Freudenberger */ 115e80d4af0SHarald Freudenberger static int alloc_and_prep_cprbmem(size_t paramblen, 116e80d4af0SHarald Freudenberger u8 **pcprbmem, 117e80d4af0SHarald Freudenberger struct CPRBX **preqCPRB, 118e80d4af0SHarald Freudenberger struct CPRBX **prepCPRB) 119e80d4af0SHarald Freudenberger { 120e80d4af0SHarald Freudenberger u8 *cprbmem; 121e80d4af0SHarald Freudenberger size_t cprbplusparamblen = sizeof(struct CPRBX) + paramblen; 122e80d4af0SHarald Freudenberger struct CPRBX *preqcblk, *prepcblk; 123e80d4af0SHarald Freudenberger 124e80d4af0SHarald Freudenberger /* 125e80d4af0SHarald Freudenberger * allocate consecutive memory for request CPRB, request param 126e80d4af0SHarald Freudenberger * block, reply CPRB and reply param block 127e80d4af0SHarald Freudenberger */ 128e80d4af0SHarald Freudenberger cprbmem = kmalloc(2 * cprbplusparamblen, GFP_KERNEL); 129e80d4af0SHarald Freudenberger if (!cprbmem) 130e80d4af0SHarald Freudenberger return -ENOMEM; 131e80d4af0SHarald Freudenberger memset(cprbmem, 0, 2 * cprbplusparamblen); 132e80d4af0SHarald Freudenberger 133e80d4af0SHarald Freudenberger preqcblk = (struct CPRBX *) cprbmem; 134e80d4af0SHarald Freudenberger prepcblk = (struct CPRBX *) (cprbmem + cprbplusparamblen); 135e80d4af0SHarald Freudenberger 136e80d4af0SHarald Freudenberger /* fill request cprb struct */ 137e80d4af0SHarald Freudenberger preqcblk->cprb_len = sizeof(struct CPRBX); 138e80d4af0SHarald Freudenberger preqcblk->cprb_ver_id = 0x02; 139e80d4af0SHarald Freudenberger memcpy(preqcblk->func_id, "T2", 2); 140e80d4af0SHarald Freudenberger preqcblk->rpl_msgbl = cprbplusparamblen; 141e80d4af0SHarald Freudenberger if (paramblen) { 142e80d4af0SHarald Freudenberger preqcblk->req_parmb = 143e80d4af0SHarald Freudenberger ((u8 *) preqcblk) + sizeof(struct CPRBX); 144e80d4af0SHarald Freudenberger preqcblk->rpl_parmb = 145e80d4af0SHarald Freudenberger ((u8 *) prepcblk) + sizeof(struct CPRBX); 146e80d4af0SHarald Freudenberger } 147e80d4af0SHarald Freudenberger 148e80d4af0SHarald Freudenberger *pcprbmem = cprbmem; 149e80d4af0SHarald Freudenberger *preqCPRB = preqcblk; 150e80d4af0SHarald Freudenberger *prepCPRB = prepcblk; 151e80d4af0SHarald Freudenberger 152e80d4af0SHarald Freudenberger return 0; 153e80d4af0SHarald Freudenberger } 154e80d4af0SHarald Freudenberger 155e80d4af0SHarald Freudenberger /* 156e80d4af0SHarald Freudenberger * Free the cprb memory allocated with the function above. 157e80d4af0SHarald Freudenberger * If the scrub value is not zero, the memory is filled 158e80d4af0SHarald Freudenberger * with zeros before freeing (useful if there was some 159e80d4af0SHarald Freudenberger * clear key material in there). 160e80d4af0SHarald Freudenberger */ 161e80d4af0SHarald Freudenberger static void free_cprbmem(void *mem, size_t paramblen, int scrub) 162e80d4af0SHarald Freudenberger { 163e80d4af0SHarald Freudenberger if (scrub) 164e80d4af0SHarald Freudenberger memzero_explicit(mem, 2 * (sizeof(struct CPRBX) + paramblen)); 165e80d4af0SHarald Freudenberger kfree(mem); 166e80d4af0SHarald Freudenberger } 167e80d4af0SHarald Freudenberger 168e80d4af0SHarald Freudenberger /* 169e80d4af0SHarald Freudenberger * Helper function to prepare the xcrb struct 170e80d4af0SHarald Freudenberger */ 171e80d4af0SHarald Freudenberger static inline void prep_xcrb(struct ica_xcRB *pxcrb, 172e80d4af0SHarald Freudenberger u16 cardnr, 173e80d4af0SHarald Freudenberger struct CPRBX *preqcblk, 174e80d4af0SHarald Freudenberger struct CPRBX *prepcblk) 175e80d4af0SHarald Freudenberger { 176e80d4af0SHarald Freudenberger memset(pxcrb, 0, sizeof(*pxcrb)); 177e80d4af0SHarald Freudenberger pxcrb->agent_ID = 0x4341; /* 'CA' */ 178e80d4af0SHarald Freudenberger pxcrb->user_defined = (cardnr == 0xFFFF ? AUTOSELECT : cardnr); 179e80d4af0SHarald Freudenberger pxcrb->request_control_blk_length = 180e80d4af0SHarald Freudenberger preqcblk->cprb_len + preqcblk->req_parml; 181e80d4af0SHarald Freudenberger pxcrb->request_control_blk_addr = (void *) preqcblk; 182e80d4af0SHarald Freudenberger pxcrb->reply_control_blk_length = preqcblk->rpl_msgbl; 183e80d4af0SHarald Freudenberger pxcrb->reply_control_blk_addr = (void *) prepcblk; 184e80d4af0SHarald Freudenberger } 185e80d4af0SHarald Freudenberger 186e80d4af0SHarald Freudenberger /* 187e80d4af0SHarald Freudenberger * Helper function which calls zcrypt_send_cprb with 188e80d4af0SHarald Freudenberger * memory management segment adjusted to kernel space 189e80d4af0SHarald Freudenberger * so that the copy_from_user called within this 190e80d4af0SHarald Freudenberger * function do in fact copy from kernel space. 191e80d4af0SHarald Freudenberger */ 192e80d4af0SHarald Freudenberger static inline int _zcrypt_send_cprb(struct ica_xcRB *xcrb) 193e80d4af0SHarald Freudenberger { 194e80d4af0SHarald Freudenberger int rc; 195e80d4af0SHarald Freudenberger mm_segment_t old_fs = get_fs(); 196e80d4af0SHarald Freudenberger 197e80d4af0SHarald Freudenberger set_fs(KERNEL_DS); 198e80d4af0SHarald Freudenberger rc = zcrypt_send_cprb(xcrb); 199e80d4af0SHarald Freudenberger set_fs(old_fs); 200e80d4af0SHarald Freudenberger 201e80d4af0SHarald Freudenberger return rc; 202e80d4af0SHarald Freudenberger } 203e80d4af0SHarald Freudenberger 204e80d4af0SHarald Freudenberger /* 205e80d4af0SHarald Freudenberger * Generate (random) AES secure key. 206e80d4af0SHarald Freudenberger */ 207e80d4af0SHarald Freudenberger int pkey_genseckey(u16 cardnr, u16 domain, 208e80d4af0SHarald Freudenberger u32 keytype, struct pkey_seckey *seckey) 209e80d4af0SHarald Freudenberger { 210e80d4af0SHarald Freudenberger int i, rc, keysize; 211e80d4af0SHarald Freudenberger int seckeysize; 212e80d4af0SHarald Freudenberger u8 *mem; 213e80d4af0SHarald Freudenberger struct CPRBX *preqcblk, *prepcblk; 214e80d4af0SHarald Freudenberger struct ica_xcRB xcrb; 215e80d4af0SHarald Freudenberger struct kgreqparm { 216e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 217e80d4af0SHarald Freudenberger u16 rule_array_len; 218e80d4af0SHarald Freudenberger struct lv1 { 219e80d4af0SHarald Freudenberger u16 len; 220e80d4af0SHarald Freudenberger char key_form[8]; 221e80d4af0SHarald Freudenberger char key_length[8]; 222e80d4af0SHarald Freudenberger char key_type1[8]; 223e80d4af0SHarald Freudenberger char key_type2[8]; 224e80d4af0SHarald Freudenberger } lv1; 225e80d4af0SHarald Freudenberger struct lv2 { 226e80d4af0SHarald Freudenberger u16 len; 227e80d4af0SHarald Freudenberger struct keyid { 228e80d4af0SHarald Freudenberger u16 len; 229e80d4af0SHarald Freudenberger u16 attr; 230e80d4af0SHarald Freudenberger u8 data[SECKEYBLOBSIZE]; 231e80d4af0SHarald Freudenberger } keyid[6]; 232e80d4af0SHarald Freudenberger } lv2; 233e80d4af0SHarald Freudenberger } *preqparm; 234e80d4af0SHarald Freudenberger struct kgrepparm { 235e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 236e80d4af0SHarald Freudenberger u16 rule_array_len; 237e80d4af0SHarald Freudenberger struct lv3 { 238e80d4af0SHarald Freudenberger u16 len; 239e80d4af0SHarald Freudenberger u16 keyblocklen; 240e80d4af0SHarald Freudenberger struct { 241e80d4af0SHarald Freudenberger u16 toklen; 242e80d4af0SHarald Freudenberger u16 tokattr; 243e80d4af0SHarald Freudenberger u8 tok[0]; 244e80d4af0SHarald Freudenberger /* ... some more data ... */ 245e80d4af0SHarald Freudenberger } keyblock; 246e80d4af0SHarald Freudenberger } lv3; 247e80d4af0SHarald Freudenberger } *prepparm; 248e80d4af0SHarald Freudenberger 249e80d4af0SHarald Freudenberger /* get already prepared memory for 2 cprbs with param block each */ 250e80d4af0SHarald Freudenberger rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk); 251e80d4af0SHarald Freudenberger if (rc) 252e80d4af0SHarald Freudenberger return rc; 253e80d4af0SHarald Freudenberger 254e80d4af0SHarald Freudenberger /* fill request cprb struct */ 255e80d4af0SHarald Freudenberger preqcblk->domain = domain; 256e80d4af0SHarald Freudenberger 257e80d4af0SHarald Freudenberger /* fill request cprb param block with KG request */ 258e80d4af0SHarald Freudenberger preqparm = (struct kgreqparm *) preqcblk->req_parmb; 259e80d4af0SHarald Freudenberger memcpy(preqparm->subfunc_code, "KG", 2); 260e80d4af0SHarald Freudenberger preqparm->rule_array_len = sizeof(preqparm->rule_array_len); 261e80d4af0SHarald Freudenberger preqparm->lv1.len = sizeof(struct lv1); 262e80d4af0SHarald Freudenberger memcpy(preqparm->lv1.key_form, "OP ", 8); 263e80d4af0SHarald Freudenberger switch (keytype) { 264e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_128: 265e80d4af0SHarald Freudenberger keysize = 16; 266e80d4af0SHarald Freudenberger memcpy(preqparm->lv1.key_length, "KEYLN16 ", 8); 267e80d4af0SHarald Freudenberger break; 268e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_192: 269e80d4af0SHarald Freudenberger keysize = 24; 270e80d4af0SHarald Freudenberger memcpy(preqparm->lv1.key_length, "KEYLN24 ", 8); 271e80d4af0SHarald Freudenberger break; 272e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_256: 273e80d4af0SHarald Freudenberger keysize = 32; 274e80d4af0SHarald Freudenberger memcpy(preqparm->lv1.key_length, "KEYLN32 ", 8); 275e80d4af0SHarald Freudenberger break; 276e80d4af0SHarald Freudenberger default: 277e80d4af0SHarald Freudenberger DEBUG_ERR( 278e80d4af0SHarald Freudenberger "pkey_genseckey unknown/unsupported keytype %d\n", 279e80d4af0SHarald Freudenberger keytype); 280e80d4af0SHarald Freudenberger rc = -EINVAL; 281e80d4af0SHarald Freudenberger goto out; 282e80d4af0SHarald Freudenberger } 283e80d4af0SHarald Freudenberger memcpy(preqparm->lv1.key_type1, "AESDATA ", 8); 284e80d4af0SHarald Freudenberger preqparm->lv2.len = sizeof(struct lv2); 285e80d4af0SHarald Freudenberger for (i = 0; i < 6; i++) { 286e80d4af0SHarald Freudenberger preqparm->lv2.keyid[i].len = sizeof(struct keyid); 287e80d4af0SHarald Freudenberger preqparm->lv2.keyid[i].attr = (i == 2 ? 0x30 : 0x10); 288e80d4af0SHarald Freudenberger } 289e80d4af0SHarald Freudenberger preqcblk->req_parml = sizeof(struct kgreqparm); 290e80d4af0SHarald Freudenberger 291e80d4af0SHarald Freudenberger /* fill xcrb struct */ 292e80d4af0SHarald Freudenberger prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk); 293e80d4af0SHarald Freudenberger 294e80d4af0SHarald Freudenberger /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */ 295e80d4af0SHarald Freudenberger rc = _zcrypt_send_cprb(&xcrb); 296e80d4af0SHarald Freudenberger if (rc) { 297e80d4af0SHarald Freudenberger DEBUG_ERR( 298e80d4af0SHarald Freudenberger "pkey_genseckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n", 299e80d4af0SHarald Freudenberger (int) cardnr, (int) domain, rc); 300e80d4af0SHarald Freudenberger goto out; 301e80d4af0SHarald Freudenberger } 302e80d4af0SHarald Freudenberger 303e80d4af0SHarald Freudenberger /* check response returncode and reasoncode */ 304e80d4af0SHarald Freudenberger if (prepcblk->ccp_rtcode != 0) { 305e80d4af0SHarald Freudenberger DEBUG_ERR( 306e80d4af0SHarald Freudenberger "pkey_genseckey secure key generate failure, card response %d/%d\n", 307e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rtcode, 308e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rscode); 309e80d4af0SHarald Freudenberger rc = -EIO; 310e80d4af0SHarald Freudenberger goto out; 311e80d4af0SHarald Freudenberger } 312e80d4af0SHarald Freudenberger 313e80d4af0SHarald Freudenberger /* process response cprb param block */ 314e80d4af0SHarald Freudenberger prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX); 315e80d4af0SHarald Freudenberger prepparm = (struct kgrepparm *) prepcblk->rpl_parmb; 316e80d4af0SHarald Freudenberger 317e80d4af0SHarald Freudenberger /* check length of the returned secure key token */ 318e80d4af0SHarald Freudenberger seckeysize = prepparm->lv3.keyblock.toklen 319e80d4af0SHarald Freudenberger - sizeof(prepparm->lv3.keyblock.toklen) 320e80d4af0SHarald Freudenberger - sizeof(prepparm->lv3.keyblock.tokattr); 321e80d4af0SHarald Freudenberger if (seckeysize != SECKEYBLOBSIZE) { 322e80d4af0SHarald Freudenberger DEBUG_ERR( 323e80d4af0SHarald Freudenberger "pkey_genseckey secure token size mismatch %d != %d bytes\n", 324e80d4af0SHarald Freudenberger seckeysize, SECKEYBLOBSIZE); 325e80d4af0SHarald Freudenberger rc = -EIO; 326e80d4af0SHarald Freudenberger goto out; 327e80d4af0SHarald Freudenberger } 328e80d4af0SHarald Freudenberger 329e80d4af0SHarald Freudenberger /* check secure key token */ 330e80d4af0SHarald Freudenberger rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize); 331e80d4af0SHarald Freudenberger if (rc) { 332e80d4af0SHarald Freudenberger rc = -EIO; 333e80d4af0SHarald Freudenberger goto out; 334e80d4af0SHarald Freudenberger } 335e80d4af0SHarald Freudenberger 336e80d4af0SHarald Freudenberger /* copy the generated secure key token */ 337e80d4af0SHarald Freudenberger memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE); 338e80d4af0SHarald Freudenberger 339e80d4af0SHarald Freudenberger out: 340e80d4af0SHarald Freudenberger free_cprbmem(mem, PARMBSIZE, 0); 341e80d4af0SHarald Freudenberger return rc; 342e80d4af0SHarald Freudenberger } 343e80d4af0SHarald Freudenberger EXPORT_SYMBOL(pkey_genseckey); 344e80d4af0SHarald Freudenberger 345e80d4af0SHarald Freudenberger /* 346e80d4af0SHarald Freudenberger * Generate an AES secure key with given key value. 347e80d4af0SHarald Freudenberger */ 348e80d4af0SHarald Freudenberger int pkey_clr2seckey(u16 cardnr, u16 domain, u32 keytype, 349e80d4af0SHarald Freudenberger const struct pkey_clrkey *clrkey, 350e80d4af0SHarald Freudenberger struct pkey_seckey *seckey) 351e80d4af0SHarald Freudenberger { 352e80d4af0SHarald Freudenberger int rc, keysize, seckeysize; 353e80d4af0SHarald Freudenberger u8 *mem; 354e80d4af0SHarald Freudenberger struct CPRBX *preqcblk, *prepcblk; 355e80d4af0SHarald Freudenberger struct ica_xcRB xcrb; 356e80d4af0SHarald Freudenberger struct cmreqparm { 357e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 358e80d4af0SHarald Freudenberger u16 rule_array_len; 359e80d4af0SHarald Freudenberger char rule_array[8]; 360e80d4af0SHarald Freudenberger struct lv1 { 361e80d4af0SHarald Freudenberger u16 len; 362e80d4af0SHarald Freudenberger u8 clrkey[0]; 363e80d4af0SHarald Freudenberger } lv1; 364e80d4af0SHarald Freudenberger struct lv2 { 365e80d4af0SHarald Freudenberger u16 len; 366e80d4af0SHarald Freudenberger struct keyid { 367e80d4af0SHarald Freudenberger u16 len; 368e80d4af0SHarald Freudenberger u16 attr; 369e80d4af0SHarald Freudenberger u8 data[SECKEYBLOBSIZE]; 370e80d4af0SHarald Freudenberger } keyid; 371e80d4af0SHarald Freudenberger } lv2; 372e80d4af0SHarald Freudenberger } *preqparm; 373e80d4af0SHarald Freudenberger struct lv2 *plv2; 374e80d4af0SHarald Freudenberger struct cmrepparm { 375e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 376e80d4af0SHarald Freudenberger u16 rule_array_len; 377e80d4af0SHarald Freudenberger struct lv3 { 378e80d4af0SHarald Freudenberger u16 len; 379e80d4af0SHarald Freudenberger u16 keyblocklen; 380e80d4af0SHarald Freudenberger struct { 381e80d4af0SHarald Freudenberger u16 toklen; 382e80d4af0SHarald Freudenberger u16 tokattr; 383e80d4af0SHarald Freudenberger u8 tok[0]; 384e80d4af0SHarald Freudenberger /* ... some more data ... */ 385e80d4af0SHarald Freudenberger } keyblock; 386e80d4af0SHarald Freudenberger } lv3; 387e80d4af0SHarald Freudenberger } *prepparm; 388e80d4af0SHarald Freudenberger 389e80d4af0SHarald Freudenberger /* get already prepared memory for 2 cprbs with param block each */ 390e80d4af0SHarald Freudenberger rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk); 391e80d4af0SHarald Freudenberger if (rc) 392e80d4af0SHarald Freudenberger return rc; 393e80d4af0SHarald Freudenberger 394e80d4af0SHarald Freudenberger /* fill request cprb struct */ 395e80d4af0SHarald Freudenberger preqcblk->domain = domain; 396e80d4af0SHarald Freudenberger 397e80d4af0SHarald Freudenberger /* fill request cprb param block with CM request */ 398e80d4af0SHarald Freudenberger preqparm = (struct cmreqparm *) preqcblk->req_parmb; 399e80d4af0SHarald Freudenberger memcpy(preqparm->subfunc_code, "CM", 2); 400e80d4af0SHarald Freudenberger memcpy(preqparm->rule_array, "AES ", 8); 401e80d4af0SHarald Freudenberger preqparm->rule_array_len = 402e80d4af0SHarald Freudenberger sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array); 403e80d4af0SHarald Freudenberger switch (keytype) { 404e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_128: 405e80d4af0SHarald Freudenberger keysize = 16; 406e80d4af0SHarald Freudenberger break; 407e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_192: 408e80d4af0SHarald Freudenberger keysize = 24; 409e80d4af0SHarald Freudenberger break; 410e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_256: 411e80d4af0SHarald Freudenberger keysize = 32; 412e80d4af0SHarald Freudenberger break; 413e80d4af0SHarald Freudenberger default: 414e80d4af0SHarald Freudenberger DEBUG_ERR( 415e80d4af0SHarald Freudenberger "pkey_clr2seckey unknown/unsupported keytype %d\n", 416e80d4af0SHarald Freudenberger keytype); 417e80d4af0SHarald Freudenberger rc = -EINVAL; 418e80d4af0SHarald Freudenberger goto out; 419e80d4af0SHarald Freudenberger } 420e80d4af0SHarald Freudenberger preqparm->lv1.len = sizeof(struct lv1) + keysize; 421e80d4af0SHarald Freudenberger memcpy(preqparm->lv1.clrkey, clrkey->clrkey, keysize); 422e80d4af0SHarald Freudenberger plv2 = (struct lv2 *) (((u8 *) &preqparm->lv2) + keysize); 423e80d4af0SHarald Freudenberger plv2->len = sizeof(struct lv2); 424e80d4af0SHarald Freudenberger plv2->keyid.len = sizeof(struct keyid); 425e80d4af0SHarald Freudenberger plv2->keyid.attr = 0x30; 426e80d4af0SHarald Freudenberger preqcblk->req_parml = sizeof(struct cmreqparm) + keysize; 427e80d4af0SHarald Freudenberger 428e80d4af0SHarald Freudenberger /* fill xcrb struct */ 429e80d4af0SHarald Freudenberger prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk); 430e80d4af0SHarald Freudenberger 431e80d4af0SHarald Freudenberger /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */ 432e80d4af0SHarald Freudenberger rc = _zcrypt_send_cprb(&xcrb); 433e80d4af0SHarald Freudenberger if (rc) { 434e80d4af0SHarald Freudenberger DEBUG_ERR( 435e80d4af0SHarald Freudenberger "pkey_clr2seckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n", 436e80d4af0SHarald Freudenberger (int) cardnr, (int) domain, rc); 437e80d4af0SHarald Freudenberger goto out; 438e80d4af0SHarald Freudenberger } 439e80d4af0SHarald Freudenberger 440e80d4af0SHarald Freudenberger /* check response returncode and reasoncode */ 441e80d4af0SHarald Freudenberger if (prepcblk->ccp_rtcode != 0) { 442e80d4af0SHarald Freudenberger DEBUG_ERR( 443e80d4af0SHarald Freudenberger "pkey_clr2seckey clear key import failure, card response %d/%d\n", 444e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rtcode, 445e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rscode); 446e80d4af0SHarald Freudenberger rc = -EIO; 447e80d4af0SHarald Freudenberger goto out; 448e80d4af0SHarald Freudenberger } 449e80d4af0SHarald Freudenberger 450e80d4af0SHarald Freudenberger /* process response cprb param block */ 451e80d4af0SHarald Freudenberger prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX); 452e80d4af0SHarald Freudenberger prepparm = (struct cmrepparm *) prepcblk->rpl_parmb; 453e80d4af0SHarald Freudenberger 454e80d4af0SHarald Freudenberger /* check length of the returned secure key token */ 455e80d4af0SHarald Freudenberger seckeysize = prepparm->lv3.keyblock.toklen 456e80d4af0SHarald Freudenberger - sizeof(prepparm->lv3.keyblock.toklen) 457e80d4af0SHarald Freudenberger - sizeof(prepparm->lv3.keyblock.tokattr); 458e80d4af0SHarald Freudenberger if (seckeysize != SECKEYBLOBSIZE) { 459e80d4af0SHarald Freudenberger DEBUG_ERR( 460e80d4af0SHarald Freudenberger "pkey_clr2seckey secure token size mismatch %d != %d bytes\n", 461e80d4af0SHarald Freudenberger seckeysize, SECKEYBLOBSIZE); 462e80d4af0SHarald Freudenberger rc = -EIO; 463e80d4af0SHarald Freudenberger goto out; 464e80d4af0SHarald Freudenberger } 465e80d4af0SHarald Freudenberger 466e80d4af0SHarald Freudenberger /* check secure key token */ 467e80d4af0SHarald Freudenberger rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize); 468e80d4af0SHarald Freudenberger if (rc) { 469e80d4af0SHarald Freudenberger rc = -EIO; 470e80d4af0SHarald Freudenberger goto out; 471e80d4af0SHarald Freudenberger } 472e80d4af0SHarald Freudenberger 473e80d4af0SHarald Freudenberger /* copy the generated secure key token */ 474e80d4af0SHarald Freudenberger memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE); 475e80d4af0SHarald Freudenberger 476e80d4af0SHarald Freudenberger out: 477e80d4af0SHarald Freudenberger free_cprbmem(mem, PARMBSIZE, 1); 478e80d4af0SHarald Freudenberger return rc; 479e80d4af0SHarald Freudenberger } 480e80d4af0SHarald Freudenberger EXPORT_SYMBOL(pkey_clr2seckey); 481e80d4af0SHarald Freudenberger 482e80d4af0SHarald Freudenberger /* 483e80d4af0SHarald Freudenberger * Derive a proteced key from the secure key blob. 484e80d4af0SHarald Freudenberger */ 485e80d4af0SHarald Freudenberger int pkey_sec2protkey(u16 cardnr, u16 domain, 486e80d4af0SHarald Freudenberger const struct pkey_seckey *seckey, 487e80d4af0SHarald Freudenberger struct pkey_protkey *protkey) 488e80d4af0SHarald Freudenberger { 489e80d4af0SHarald Freudenberger int rc; 490e80d4af0SHarald Freudenberger u8 *mem; 491e80d4af0SHarald Freudenberger struct CPRBX *preqcblk, *prepcblk; 492e80d4af0SHarald Freudenberger struct ica_xcRB xcrb; 493e80d4af0SHarald Freudenberger struct uskreqparm { 494e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 495e80d4af0SHarald Freudenberger u16 rule_array_len; 496e80d4af0SHarald Freudenberger struct lv1 { 497e80d4af0SHarald Freudenberger u16 len; 498e80d4af0SHarald Freudenberger u16 attr_len; 499e80d4af0SHarald Freudenberger u16 attr_flags; 500e80d4af0SHarald Freudenberger } lv1; 501e80d4af0SHarald Freudenberger struct lv2 { 502e80d4af0SHarald Freudenberger u16 len; 503e80d4af0SHarald Freudenberger u16 attr_len; 504e80d4af0SHarald Freudenberger u16 attr_flags; 505e80d4af0SHarald Freudenberger u8 token[0]; /* cca secure key token */ 506e80d4af0SHarald Freudenberger } lv2 __packed; 507e80d4af0SHarald Freudenberger } *preqparm; 508e80d4af0SHarald Freudenberger struct uskrepparm { 509e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 510e80d4af0SHarald Freudenberger u16 rule_array_len; 511e80d4af0SHarald Freudenberger struct lv3 { 512e80d4af0SHarald Freudenberger u16 len; 513e80d4af0SHarald Freudenberger u16 attr_len; 514e80d4af0SHarald Freudenberger u16 attr_flags; 515e80d4af0SHarald Freudenberger struct cpacfkeyblock { 516e80d4af0SHarald Freudenberger u8 version; /* version of this struct */ 517e80d4af0SHarald Freudenberger u8 flags[2]; 518e80d4af0SHarald Freudenberger u8 algo; 519e80d4af0SHarald Freudenberger u8 form; 520e80d4af0SHarald Freudenberger u8 pad1[3]; 521e80d4af0SHarald Freudenberger u16 keylen; 522e80d4af0SHarald Freudenberger u8 key[64]; /* the key (keylen bytes) */ 523e80d4af0SHarald Freudenberger u16 keyattrlen; 524e80d4af0SHarald Freudenberger u8 keyattr[32]; 525e80d4af0SHarald Freudenberger u8 pad2[1]; 526e80d4af0SHarald Freudenberger u8 vptype; 527e80d4af0SHarald Freudenberger u8 vp[32]; /* verification pattern */ 528e80d4af0SHarald Freudenberger } keyblock; 529e80d4af0SHarald Freudenberger } lv3 __packed; 530e80d4af0SHarald Freudenberger } *prepparm; 531e80d4af0SHarald Freudenberger 532e80d4af0SHarald Freudenberger /* get already prepared memory for 2 cprbs with param block each */ 533e80d4af0SHarald Freudenberger rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk); 534e80d4af0SHarald Freudenberger if (rc) 535e80d4af0SHarald Freudenberger return rc; 536e80d4af0SHarald Freudenberger 537e80d4af0SHarald Freudenberger /* fill request cprb struct */ 538e80d4af0SHarald Freudenberger preqcblk->domain = domain; 539e80d4af0SHarald Freudenberger 540e80d4af0SHarald Freudenberger /* fill request cprb param block with USK request */ 541e80d4af0SHarald Freudenberger preqparm = (struct uskreqparm *) preqcblk->req_parmb; 542e80d4af0SHarald Freudenberger memcpy(preqparm->subfunc_code, "US", 2); 543e80d4af0SHarald Freudenberger preqparm->rule_array_len = sizeof(preqparm->rule_array_len); 544e80d4af0SHarald Freudenberger preqparm->lv1.len = sizeof(struct lv1); 545e80d4af0SHarald Freudenberger preqparm->lv1.attr_len = sizeof(struct lv1) - sizeof(preqparm->lv1.len); 546e80d4af0SHarald Freudenberger preqparm->lv1.attr_flags = 0x0001; 547e80d4af0SHarald Freudenberger preqparm->lv2.len = sizeof(struct lv2) + SECKEYBLOBSIZE; 548e80d4af0SHarald Freudenberger preqparm->lv2.attr_len = sizeof(struct lv2) 549e80d4af0SHarald Freudenberger - sizeof(preqparm->lv2.len) + SECKEYBLOBSIZE; 550e80d4af0SHarald Freudenberger preqparm->lv2.attr_flags = 0x0000; 551e80d4af0SHarald Freudenberger memcpy(preqparm->lv2.token, seckey->seckey, SECKEYBLOBSIZE); 552e80d4af0SHarald Freudenberger preqcblk->req_parml = sizeof(struct uskreqparm) + SECKEYBLOBSIZE; 553e80d4af0SHarald Freudenberger 554e80d4af0SHarald Freudenberger /* fill xcrb struct */ 555e80d4af0SHarald Freudenberger prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk); 556e80d4af0SHarald Freudenberger 557e80d4af0SHarald Freudenberger /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */ 558e80d4af0SHarald Freudenberger rc = _zcrypt_send_cprb(&xcrb); 559e80d4af0SHarald Freudenberger if (rc) { 560e80d4af0SHarald Freudenberger DEBUG_ERR( 561e80d4af0SHarald Freudenberger "pkey_sec2protkey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n", 562e80d4af0SHarald Freudenberger (int) cardnr, (int) domain, rc); 563e80d4af0SHarald Freudenberger goto out; 564e80d4af0SHarald Freudenberger } 565e80d4af0SHarald Freudenberger 566e80d4af0SHarald Freudenberger /* check response returncode and reasoncode */ 567e80d4af0SHarald Freudenberger if (prepcblk->ccp_rtcode != 0) { 568e80d4af0SHarald Freudenberger DEBUG_ERR( 569e80d4af0SHarald Freudenberger "pkey_sec2protkey unwrap secure key failure, card response %d/%d\n", 570e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rtcode, 571e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rscode); 572e80d4af0SHarald Freudenberger rc = -EIO; 573e80d4af0SHarald Freudenberger goto out; 574e80d4af0SHarald Freudenberger } 575*ca681ec8SHarald Freudenberger if (prepcblk->ccp_rscode != 0) { 576*ca681ec8SHarald Freudenberger DEBUG_WARN( 577*ca681ec8SHarald Freudenberger "pkey_sec2protkey unwrap secure key warning, card response %d/%d\n", 578*ca681ec8SHarald Freudenberger (int) prepcblk->ccp_rtcode, 579*ca681ec8SHarald Freudenberger (int) prepcblk->ccp_rscode); 580*ca681ec8SHarald Freudenberger } 581e80d4af0SHarald Freudenberger 582e80d4af0SHarald Freudenberger /* process response cprb param block */ 583e80d4af0SHarald Freudenberger prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX); 584e80d4af0SHarald Freudenberger prepparm = (struct uskrepparm *) prepcblk->rpl_parmb; 585e80d4af0SHarald Freudenberger 586e80d4af0SHarald Freudenberger /* check the returned keyblock */ 587e80d4af0SHarald Freudenberger if (prepparm->lv3.keyblock.version != 0x01) { 588e80d4af0SHarald Freudenberger DEBUG_ERR( 589e80d4af0SHarald Freudenberger "pkey_sec2protkey reply param keyblock version mismatch 0x%02x != 0x01\n", 590e80d4af0SHarald Freudenberger (int) prepparm->lv3.keyblock.version); 591e80d4af0SHarald Freudenberger rc = -EIO; 592e80d4af0SHarald Freudenberger goto out; 593e80d4af0SHarald Freudenberger } 594e80d4af0SHarald Freudenberger 595e80d4af0SHarald Freudenberger /* copy the tanslated protected key */ 596e80d4af0SHarald Freudenberger switch (prepparm->lv3.keyblock.keylen) { 597e80d4af0SHarald Freudenberger case 16+32: 598e80d4af0SHarald Freudenberger protkey->type = PKEY_KEYTYPE_AES_128; 599e80d4af0SHarald Freudenberger break; 600e80d4af0SHarald Freudenberger case 24+32: 601e80d4af0SHarald Freudenberger protkey->type = PKEY_KEYTYPE_AES_192; 602e80d4af0SHarald Freudenberger break; 603e80d4af0SHarald Freudenberger case 32+32: 604e80d4af0SHarald Freudenberger protkey->type = PKEY_KEYTYPE_AES_256; 605e80d4af0SHarald Freudenberger break; 606e80d4af0SHarald Freudenberger default: 607e80d4af0SHarald Freudenberger DEBUG_ERR("pkey_sec2protkey unknown/unsupported keytype %d\n", 608e80d4af0SHarald Freudenberger prepparm->lv3.keyblock.keylen); 609e80d4af0SHarald Freudenberger rc = -EIO; 610e80d4af0SHarald Freudenberger goto out; 611e80d4af0SHarald Freudenberger } 612e80d4af0SHarald Freudenberger protkey->len = prepparm->lv3.keyblock.keylen; 613e80d4af0SHarald Freudenberger memcpy(protkey->protkey, prepparm->lv3.keyblock.key, protkey->len); 614e80d4af0SHarald Freudenberger 615e80d4af0SHarald Freudenberger out: 616e80d4af0SHarald Freudenberger free_cprbmem(mem, PARMBSIZE, 0); 617e80d4af0SHarald Freudenberger return rc; 618e80d4af0SHarald Freudenberger } 619e80d4af0SHarald Freudenberger EXPORT_SYMBOL(pkey_sec2protkey); 620e80d4af0SHarald Freudenberger 621e80d4af0SHarald Freudenberger /* 622e80d4af0SHarald Freudenberger * Create a protected key from a clear key value. 623e80d4af0SHarald Freudenberger */ 624e80d4af0SHarald Freudenberger int pkey_clr2protkey(u32 keytype, 625e80d4af0SHarald Freudenberger const struct pkey_clrkey *clrkey, 626e80d4af0SHarald Freudenberger struct pkey_protkey *protkey) 627e80d4af0SHarald Freudenberger { 628e80d4af0SHarald Freudenberger long fc; 629e80d4af0SHarald Freudenberger int keysize; 630e80d4af0SHarald Freudenberger u8 paramblock[64]; 631e80d4af0SHarald Freudenberger 632e80d4af0SHarald Freudenberger switch (keytype) { 633e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_128: 634e80d4af0SHarald Freudenberger keysize = 16; 635e80d4af0SHarald Freudenberger fc = CPACF_PCKMO_ENC_AES_128_KEY; 636e80d4af0SHarald Freudenberger break; 637e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_192: 638e80d4af0SHarald Freudenberger keysize = 24; 639e80d4af0SHarald Freudenberger fc = CPACF_PCKMO_ENC_AES_192_KEY; 640e80d4af0SHarald Freudenberger break; 641e80d4af0SHarald Freudenberger case PKEY_KEYTYPE_AES_256: 642e80d4af0SHarald Freudenberger keysize = 32; 643e80d4af0SHarald Freudenberger fc = CPACF_PCKMO_ENC_AES_256_KEY; 644e80d4af0SHarald Freudenberger break; 645e80d4af0SHarald Freudenberger default: 646e80d4af0SHarald Freudenberger DEBUG_ERR("pkey_clr2protkey unknown/unsupported keytype %d\n", 647e80d4af0SHarald Freudenberger keytype); 648e80d4af0SHarald Freudenberger return -EINVAL; 649e80d4af0SHarald Freudenberger } 650e80d4af0SHarald Freudenberger 651e80d4af0SHarald Freudenberger /* prepare param block */ 652e80d4af0SHarald Freudenberger memset(paramblock, 0, sizeof(paramblock)); 653e80d4af0SHarald Freudenberger memcpy(paramblock, clrkey->clrkey, keysize); 654e80d4af0SHarald Freudenberger 655e80d4af0SHarald Freudenberger /* call the pckmo instruction */ 656e80d4af0SHarald Freudenberger cpacf_pckmo(fc, paramblock); 657e80d4af0SHarald Freudenberger 658e80d4af0SHarald Freudenberger /* copy created protected key */ 659e80d4af0SHarald Freudenberger protkey->type = keytype; 660e80d4af0SHarald Freudenberger protkey->len = keysize + 32; 661e80d4af0SHarald Freudenberger memcpy(protkey->protkey, paramblock, keysize + 32); 662e80d4af0SHarald Freudenberger 663e80d4af0SHarald Freudenberger return 0; 664e80d4af0SHarald Freudenberger } 665e80d4af0SHarald Freudenberger EXPORT_SYMBOL(pkey_clr2protkey); 666e80d4af0SHarald Freudenberger 667e80d4af0SHarald Freudenberger /* 668e80d4af0SHarald Freudenberger * query cryptographic facility from adapter 669e80d4af0SHarald Freudenberger */ 670e80d4af0SHarald Freudenberger static int query_crypto_facility(u16 cardnr, u16 domain, 671e80d4af0SHarald Freudenberger const char *keyword, 672e80d4af0SHarald Freudenberger u8 *rarray, size_t *rarraylen, 673e80d4af0SHarald Freudenberger u8 *varray, size_t *varraylen) 674e80d4af0SHarald Freudenberger { 675e80d4af0SHarald Freudenberger int rc; 676e80d4af0SHarald Freudenberger u16 len; 677e80d4af0SHarald Freudenberger u8 *mem, *ptr; 678e80d4af0SHarald Freudenberger struct CPRBX *preqcblk, *prepcblk; 679e80d4af0SHarald Freudenberger struct ica_xcRB xcrb; 680e80d4af0SHarald Freudenberger struct fqreqparm { 681e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 682e80d4af0SHarald Freudenberger u16 rule_array_len; 683e80d4af0SHarald Freudenberger char rule_array[8]; 684e80d4af0SHarald Freudenberger struct lv1 { 685e80d4af0SHarald Freudenberger u16 len; 686e80d4af0SHarald Freudenberger u8 data[VARDATASIZE]; 687e80d4af0SHarald Freudenberger } lv1; 688e80d4af0SHarald Freudenberger u16 dummylen; 689e80d4af0SHarald Freudenberger } *preqparm; 690e80d4af0SHarald Freudenberger size_t parmbsize = sizeof(struct fqreqparm); 691e80d4af0SHarald Freudenberger struct fqrepparm { 692e80d4af0SHarald Freudenberger u8 subfunc_code[2]; 693e80d4af0SHarald Freudenberger u8 lvdata[0]; 694e80d4af0SHarald Freudenberger } *prepparm; 695e80d4af0SHarald Freudenberger 696e80d4af0SHarald Freudenberger /* get already prepared memory for 2 cprbs with param block each */ 697e80d4af0SHarald Freudenberger rc = alloc_and_prep_cprbmem(parmbsize, &mem, &preqcblk, &prepcblk); 698e80d4af0SHarald Freudenberger if (rc) 699e80d4af0SHarald Freudenberger return rc; 700e80d4af0SHarald Freudenberger 701e80d4af0SHarald Freudenberger /* fill request cprb struct */ 702e80d4af0SHarald Freudenberger preqcblk->domain = domain; 703e80d4af0SHarald Freudenberger 704e80d4af0SHarald Freudenberger /* fill request cprb param block with FQ request */ 705e80d4af0SHarald Freudenberger preqparm = (struct fqreqparm *) preqcblk->req_parmb; 706e80d4af0SHarald Freudenberger memcpy(preqparm->subfunc_code, "FQ", 2); 707e80d4af0SHarald Freudenberger strncpy(preqparm->rule_array, keyword, sizeof(preqparm->rule_array)); 708e80d4af0SHarald Freudenberger preqparm->rule_array_len = 709e80d4af0SHarald Freudenberger sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array); 710e80d4af0SHarald Freudenberger preqparm->lv1.len = sizeof(preqparm->lv1); 711e80d4af0SHarald Freudenberger preqparm->dummylen = sizeof(preqparm->dummylen); 712e80d4af0SHarald Freudenberger preqcblk->req_parml = parmbsize; 713e80d4af0SHarald Freudenberger 714e80d4af0SHarald Freudenberger /* fill xcrb struct */ 715e80d4af0SHarald Freudenberger prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk); 716e80d4af0SHarald Freudenberger 717e80d4af0SHarald Freudenberger /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */ 718e80d4af0SHarald Freudenberger rc = _zcrypt_send_cprb(&xcrb); 719e80d4af0SHarald Freudenberger if (rc) { 720e80d4af0SHarald Freudenberger DEBUG_ERR( 721e80d4af0SHarald Freudenberger "query_crypto_facility zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n", 722e80d4af0SHarald Freudenberger (int) cardnr, (int) domain, rc); 723e80d4af0SHarald Freudenberger goto out; 724e80d4af0SHarald Freudenberger } 725e80d4af0SHarald Freudenberger 726e80d4af0SHarald Freudenberger /* check response returncode and reasoncode */ 727e80d4af0SHarald Freudenberger if (prepcblk->ccp_rtcode != 0) { 728e80d4af0SHarald Freudenberger DEBUG_ERR( 729e80d4af0SHarald Freudenberger "query_crypto_facility unwrap secure key failure, card response %d/%d\n", 730e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rtcode, 731e80d4af0SHarald Freudenberger (int) prepcblk->ccp_rscode); 732e80d4af0SHarald Freudenberger rc = -EIO; 733e80d4af0SHarald Freudenberger goto out; 734e80d4af0SHarald Freudenberger } 735e80d4af0SHarald Freudenberger 736e80d4af0SHarald Freudenberger /* process response cprb param block */ 737e80d4af0SHarald Freudenberger prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX); 738e80d4af0SHarald Freudenberger prepparm = (struct fqrepparm *) prepcblk->rpl_parmb; 739e80d4af0SHarald Freudenberger ptr = prepparm->lvdata; 740e80d4af0SHarald Freudenberger 741e80d4af0SHarald Freudenberger /* check and possibly copy reply rule array */ 742e80d4af0SHarald Freudenberger len = *((u16 *) ptr); 743e80d4af0SHarald Freudenberger if (len > sizeof(u16)) { 744e80d4af0SHarald Freudenberger ptr += sizeof(u16); 745e80d4af0SHarald Freudenberger len -= sizeof(u16); 746e80d4af0SHarald Freudenberger if (rarray && rarraylen && *rarraylen > 0) { 747e80d4af0SHarald Freudenberger *rarraylen = (len > *rarraylen ? *rarraylen : len); 748e80d4af0SHarald Freudenberger memcpy(rarray, ptr, *rarraylen); 749e80d4af0SHarald Freudenberger } 750e80d4af0SHarald Freudenberger ptr += len; 751e80d4af0SHarald Freudenberger } 752e80d4af0SHarald Freudenberger /* check and possible copy reply var array */ 753e80d4af0SHarald Freudenberger len = *((u16 *) ptr); 754e80d4af0SHarald Freudenberger if (len > sizeof(u16)) { 755e80d4af0SHarald Freudenberger ptr += sizeof(u16); 756e80d4af0SHarald Freudenberger len -= sizeof(u16); 757e80d4af0SHarald Freudenberger if (varray && varraylen && *varraylen > 0) { 758e80d4af0SHarald Freudenberger *varraylen = (len > *varraylen ? *varraylen : len); 759e80d4af0SHarald Freudenberger memcpy(varray, ptr, *varraylen); 760e80d4af0SHarald Freudenberger } 761e80d4af0SHarald Freudenberger ptr += len; 762e80d4af0SHarald Freudenberger } 763e80d4af0SHarald Freudenberger 764e80d4af0SHarald Freudenberger out: 765e80d4af0SHarald Freudenberger free_cprbmem(mem, parmbsize, 0); 766e80d4af0SHarald Freudenberger return rc; 767e80d4af0SHarald Freudenberger } 768e80d4af0SHarald Freudenberger 769e80d4af0SHarald Freudenberger /* 770*ca681ec8SHarald Freudenberger * Fetch the current and old mkvp values via 771*ca681ec8SHarald Freudenberger * query_crypto_facility from adapter. 772e80d4af0SHarald Freudenberger */ 773*ca681ec8SHarald Freudenberger static int fetch_mkvp(u16 cardnr, u16 domain, u64 mkvp[2]) 774e80d4af0SHarald Freudenberger { 775e80d4af0SHarald Freudenberger int rc, found = 0; 776e80d4af0SHarald Freudenberger size_t rlen, vlen; 777e80d4af0SHarald Freudenberger u8 *rarray, *varray, *pg; 778e80d4af0SHarald Freudenberger 779e80d4af0SHarald Freudenberger pg = (u8 *) __get_free_page(GFP_KERNEL); 780e80d4af0SHarald Freudenberger if (!pg) 781e80d4af0SHarald Freudenberger return -ENOMEM; 782e80d4af0SHarald Freudenberger rarray = pg; 783e80d4af0SHarald Freudenberger varray = pg + PAGE_SIZE/2; 784e80d4af0SHarald Freudenberger rlen = vlen = PAGE_SIZE/2; 785e80d4af0SHarald Freudenberger 786e80d4af0SHarald Freudenberger rc = query_crypto_facility(cardnr, domain, "STATICSA", 787e80d4af0SHarald Freudenberger rarray, &rlen, varray, &vlen); 788e80d4af0SHarald Freudenberger if (rc == 0 && rlen > 8*8 && vlen > 184+8) { 789*ca681ec8SHarald Freudenberger if (rarray[8*8] == '2') { 790e80d4af0SHarald Freudenberger /* current master key state is valid */ 791*ca681ec8SHarald Freudenberger mkvp[0] = *((u64 *)(varray + 184)); 792*ca681ec8SHarald Freudenberger mkvp[1] = *((u64 *)(varray + 172)); 793e80d4af0SHarald Freudenberger found = 1; 794e80d4af0SHarald Freudenberger } 795e80d4af0SHarald Freudenberger } 796e80d4af0SHarald Freudenberger 797e80d4af0SHarald Freudenberger free_page((unsigned long) pg); 798e80d4af0SHarald Freudenberger 799e80d4af0SHarald Freudenberger return found ? 0 : -ENOENT; 800e80d4af0SHarald Freudenberger } 801e80d4af0SHarald Freudenberger 802e80d4af0SHarald Freudenberger /* struct to hold cached mkvp info for each card/domain */ 803e80d4af0SHarald Freudenberger struct mkvp_info { 804e80d4af0SHarald Freudenberger struct list_head list; 805e80d4af0SHarald Freudenberger u16 cardnr; 806e80d4af0SHarald Freudenberger u16 domain; 807*ca681ec8SHarald Freudenberger u64 mkvp[2]; 808e80d4af0SHarald Freudenberger }; 809e80d4af0SHarald Freudenberger 810e80d4af0SHarald Freudenberger /* a list with mkvp_info entries */ 811e80d4af0SHarald Freudenberger static LIST_HEAD(mkvp_list); 812e80d4af0SHarald Freudenberger static DEFINE_SPINLOCK(mkvp_list_lock); 813e80d4af0SHarald Freudenberger 814*ca681ec8SHarald Freudenberger static int mkvp_cache_fetch(u16 cardnr, u16 domain, u64 mkvp[2]) 815e80d4af0SHarald Freudenberger { 816e80d4af0SHarald Freudenberger int rc = -ENOENT; 817e80d4af0SHarald Freudenberger struct mkvp_info *ptr; 818e80d4af0SHarald Freudenberger 819e80d4af0SHarald Freudenberger spin_lock_bh(&mkvp_list_lock); 820e80d4af0SHarald Freudenberger list_for_each_entry(ptr, &mkvp_list, list) { 821e80d4af0SHarald Freudenberger if (ptr->cardnr == cardnr && 822e80d4af0SHarald Freudenberger ptr->domain == domain) { 823*ca681ec8SHarald Freudenberger memcpy(mkvp, ptr->mkvp, 2 * sizeof(u64)); 824e80d4af0SHarald Freudenberger rc = 0; 825e80d4af0SHarald Freudenberger break; 826e80d4af0SHarald Freudenberger } 827e80d4af0SHarald Freudenberger } 828e80d4af0SHarald Freudenberger spin_unlock_bh(&mkvp_list_lock); 829e80d4af0SHarald Freudenberger 830e80d4af0SHarald Freudenberger return rc; 831e80d4af0SHarald Freudenberger } 832e80d4af0SHarald Freudenberger 833*ca681ec8SHarald Freudenberger static void mkvp_cache_update(u16 cardnr, u16 domain, u64 mkvp[2]) 834e80d4af0SHarald Freudenberger { 835e80d4af0SHarald Freudenberger int found = 0; 836e80d4af0SHarald Freudenberger struct mkvp_info *ptr; 837e80d4af0SHarald Freudenberger 838e80d4af0SHarald Freudenberger spin_lock_bh(&mkvp_list_lock); 839e80d4af0SHarald Freudenberger list_for_each_entry(ptr, &mkvp_list, list) { 840e80d4af0SHarald Freudenberger if (ptr->cardnr == cardnr && 841e80d4af0SHarald Freudenberger ptr->domain == domain) { 842*ca681ec8SHarald Freudenberger memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64)); 843e80d4af0SHarald Freudenberger found = 1; 844e80d4af0SHarald Freudenberger break; 845e80d4af0SHarald Freudenberger } 846e80d4af0SHarald Freudenberger } 847e80d4af0SHarald Freudenberger if (!found) { 848e80d4af0SHarald Freudenberger ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC); 849e80d4af0SHarald Freudenberger if (!ptr) { 850e80d4af0SHarald Freudenberger spin_unlock_bh(&mkvp_list_lock); 851e80d4af0SHarald Freudenberger return; 852e80d4af0SHarald Freudenberger } 853e80d4af0SHarald Freudenberger ptr->cardnr = cardnr; 854e80d4af0SHarald Freudenberger ptr->domain = domain; 855*ca681ec8SHarald Freudenberger memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64)); 856e80d4af0SHarald Freudenberger list_add(&ptr->list, &mkvp_list); 857e80d4af0SHarald Freudenberger } 858e80d4af0SHarald Freudenberger spin_unlock_bh(&mkvp_list_lock); 859e80d4af0SHarald Freudenberger } 860e80d4af0SHarald Freudenberger 861e80d4af0SHarald Freudenberger static void mkvp_cache_scrub(u16 cardnr, u16 domain) 862e80d4af0SHarald Freudenberger { 863e80d4af0SHarald Freudenberger struct mkvp_info *ptr; 864e80d4af0SHarald Freudenberger 865e80d4af0SHarald Freudenberger spin_lock_bh(&mkvp_list_lock); 866e80d4af0SHarald Freudenberger list_for_each_entry(ptr, &mkvp_list, list) { 867e80d4af0SHarald Freudenberger if (ptr->cardnr == cardnr && 868e80d4af0SHarald Freudenberger ptr->domain == domain) { 869e80d4af0SHarald Freudenberger list_del(&ptr->list); 870e80d4af0SHarald Freudenberger kfree(ptr); 871e80d4af0SHarald Freudenberger break; 872e80d4af0SHarald Freudenberger } 873e80d4af0SHarald Freudenberger } 874e80d4af0SHarald Freudenberger spin_unlock_bh(&mkvp_list_lock); 875e80d4af0SHarald Freudenberger } 876e80d4af0SHarald Freudenberger 877e80d4af0SHarald Freudenberger static void __exit mkvp_cache_free(void) 878e80d4af0SHarald Freudenberger { 879e80d4af0SHarald Freudenberger struct mkvp_info *ptr, *pnext; 880e80d4af0SHarald Freudenberger 881e80d4af0SHarald Freudenberger spin_lock_bh(&mkvp_list_lock); 882e80d4af0SHarald Freudenberger list_for_each_entry_safe(ptr, pnext, &mkvp_list, list) { 883e80d4af0SHarald Freudenberger list_del(&ptr->list); 884e80d4af0SHarald Freudenberger kfree(ptr); 885e80d4af0SHarald Freudenberger } 886e80d4af0SHarald Freudenberger spin_unlock_bh(&mkvp_list_lock); 887e80d4af0SHarald Freudenberger } 888e80d4af0SHarald Freudenberger 889e80d4af0SHarald Freudenberger /* 890e80d4af0SHarald Freudenberger * Search for a matching crypto card based on the Master Key 891e80d4af0SHarald Freudenberger * Verification Pattern provided inside a secure key. 892e80d4af0SHarald Freudenberger */ 893e80d4af0SHarald Freudenberger int pkey_findcard(const struct pkey_seckey *seckey, 894e80d4af0SHarald Freudenberger u16 *pcardnr, u16 *pdomain, int verify) 895e80d4af0SHarald Freudenberger { 896e80d4af0SHarald Freudenberger struct secaeskeytoken *t = (struct secaeskeytoken *) seckey; 897e80d4af0SHarald Freudenberger struct zcrypt_device_matrix *device_matrix; 898e80d4af0SHarald Freudenberger u16 card, dom; 899*ca681ec8SHarald Freudenberger u64 mkvp[2]; 900*ca681ec8SHarald Freudenberger int i, rc, oi = -1; 901e80d4af0SHarald Freudenberger 902e80d4af0SHarald Freudenberger /* mkvp must not be zero */ 903e80d4af0SHarald Freudenberger if (t->mkvp == 0) 904e80d4af0SHarald Freudenberger return -EINVAL; 905e80d4af0SHarald Freudenberger 906e80d4af0SHarald Freudenberger /* fetch status of all crypto cards */ 907e80d4af0SHarald Freudenberger device_matrix = kmalloc(sizeof(struct zcrypt_device_matrix), 908e80d4af0SHarald Freudenberger GFP_KERNEL); 909e80d4af0SHarald Freudenberger if (!device_matrix) 910e80d4af0SHarald Freudenberger return -ENOMEM; 911e80d4af0SHarald Freudenberger zcrypt_device_status_mask(device_matrix); 912e80d4af0SHarald Freudenberger 913e80d4af0SHarald Freudenberger /* walk through all crypto cards */ 914e80d4af0SHarald Freudenberger for (i = 0; i < MAX_ZDEV_ENTRIES; i++) { 915e80d4af0SHarald Freudenberger card = AP_QID_CARD(device_matrix->device[i].qid); 916e80d4af0SHarald Freudenberger dom = AP_QID_QUEUE(device_matrix->device[i].qid); 917e80d4af0SHarald Freudenberger if (device_matrix->device[i].online && 918e80d4af0SHarald Freudenberger device_matrix->device[i].functions & 0x04) { 919e80d4af0SHarald Freudenberger /* an enabled CCA Coprocessor card */ 920e80d4af0SHarald Freudenberger /* try cached mkvp */ 921*ca681ec8SHarald Freudenberger if (mkvp_cache_fetch(card, dom, mkvp) == 0 && 922*ca681ec8SHarald Freudenberger t->mkvp == mkvp[0]) { 923e80d4af0SHarald Freudenberger if (!verify) 924e80d4af0SHarald Freudenberger break; 925e80d4af0SHarald Freudenberger /* verify: fetch mkvp from adapter */ 926*ca681ec8SHarald Freudenberger if (fetch_mkvp(card, dom, mkvp) == 0) { 927e80d4af0SHarald Freudenberger mkvp_cache_update(card, dom, mkvp); 928*ca681ec8SHarald Freudenberger if (t->mkvp == mkvp[0]) 929e80d4af0SHarald Freudenberger break; 930e80d4af0SHarald Freudenberger } 931e80d4af0SHarald Freudenberger } 932e80d4af0SHarald Freudenberger } else { 933e80d4af0SHarald Freudenberger /* Card is offline and/or not a CCA card. */ 934e80d4af0SHarald Freudenberger /* del mkvp entry from cache if it exists */ 935e80d4af0SHarald Freudenberger mkvp_cache_scrub(card, dom); 936e80d4af0SHarald Freudenberger } 937e80d4af0SHarald Freudenberger } 938e80d4af0SHarald Freudenberger if (i >= MAX_ZDEV_ENTRIES) { 939e80d4af0SHarald Freudenberger /* nothing found, so this time without cache */ 940e80d4af0SHarald Freudenberger for (i = 0; i < MAX_ZDEV_ENTRIES; i++) { 941e80d4af0SHarald Freudenberger if (!(device_matrix->device[i].online && 942e80d4af0SHarald Freudenberger device_matrix->device[i].functions & 0x04)) 943e80d4af0SHarald Freudenberger continue; 944e80d4af0SHarald Freudenberger card = AP_QID_CARD(device_matrix->device[i].qid); 945e80d4af0SHarald Freudenberger dom = AP_QID_QUEUE(device_matrix->device[i].qid); 946e80d4af0SHarald Freudenberger /* fresh fetch mkvp from adapter */ 947*ca681ec8SHarald Freudenberger if (fetch_mkvp(card, dom, mkvp) == 0) { 948e80d4af0SHarald Freudenberger mkvp_cache_update(card, dom, mkvp); 949*ca681ec8SHarald Freudenberger if (t->mkvp == mkvp[0]) 950e80d4af0SHarald Freudenberger break; 951*ca681ec8SHarald Freudenberger if (t->mkvp == mkvp[1] && oi < 0) 952*ca681ec8SHarald Freudenberger oi = i; 953e80d4af0SHarald Freudenberger } 954e80d4af0SHarald Freudenberger } 955*ca681ec8SHarald Freudenberger if (i >= MAX_ZDEV_ENTRIES && oi >= 0) { 956*ca681ec8SHarald Freudenberger /* old mkvp matched, use this card then */ 957*ca681ec8SHarald Freudenberger card = AP_QID_CARD(device_matrix->device[oi].qid); 958*ca681ec8SHarald Freudenberger dom = AP_QID_QUEUE(device_matrix->device[oi].qid); 959e80d4af0SHarald Freudenberger } 960*ca681ec8SHarald Freudenberger } 961*ca681ec8SHarald Freudenberger if (i < MAX_ZDEV_ENTRIES || oi >= 0) { 962e80d4af0SHarald Freudenberger if (pcardnr) 963e80d4af0SHarald Freudenberger *pcardnr = card; 964e80d4af0SHarald Freudenberger if (pdomain) 965e80d4af0SHarald Freudenberger *pdomain = dom; 966e80d4af0SHarald Freudenberger rc = 0; 967e80d4af0SHarald Freudenberger } else 968e80d4af0SHarald Freudenberger rc = -ENODEV; 969e80d4af0SHarald Freudenberger 970e80d4af0SHarald Freudenberger kfree(device_matrix); 971e80d4af0SHarald Freudenberger return rc; 972e80d4af0SHarald Freudenberger } 973e80d4af0SHarald Freudenberger EXPORT_SYMBOL(pkey_findcard); 974e80d4af0SHarald Freudenberger 975e80d4af0SHarald Freudenberger /* 976e80d4af0SHarald Freudenberger * Find card and transform secure key into protected key. 977e80d4af0SHarald Freudenberger */ 978e80d4af0SHarald Freudenberger int pkey_skey2pkey(const struct pkey_seckey *seckey, 979e80d4af0SHarald Freudenberger struct pkey_protkey *protkey) 980e80d4af0SHarald Freudenberger { 981e80d4af0SHarald Freudenberger u16 cardnr, domain; 982e80d4af0SHarald Freudenberger int rc, verify; 983e80d4af0SHarald Freudenberger 984e80d4af0SHarald Freudenberger /* 985e80d4af0SHarald Freudenberger * The pkey_sec2protkey call may fail when a card has been 986e80d4af0SHarald Freudenberger * addressed where the master key was changed after last fetch 987e80d4af0SHarald Freudenberger * of the mkvp into the cache. So first try without verify then 988e80d4af0SHarald Freudenberger * with verify enabled (thus refreshing the mkvp for each card). 989e80d4af0SHarald Freudenberger */ 990e80d4af0SHarald Freudenberger for (verify = 0; verify < 2; verify++) { 991e80d4af0SHarald Freudenberger rc = pkey_findcard(seckey, &cardnr, &domain, verify); 992e80d4af0SHarald Freudenberger if (rc) 993e80d4af0SHarald Freudenberger continue; 994e80d4af0SHarald Freudenberger rc = pkey_sec2protkey(cardnr, domain, seckey, protkey); 995e80d4af0SHarald Freudenberger if (rc == 0) 996e80d4af0SHarald Freudenberger break; 997e80d4af0SHarald Freudenberger } 998e80d4af0SHarald Freudenberger 999e80d4af0SHarald Freudenberger if (rc) 1000e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_skey2pkey failed rc=%d\n", rc); 1001e80d4af0SHarald Freudenberger 1002e80d4af0SHarald Freudenberger return rc; 1003e80d4af0SHarald Freudenberger } 1004e80d4af0SHarald Freudenberger EXPORT_SYMBOL(pkey_skey2pkey); 1005e80d4af0SHarald Freudenberger 1006e80d4af0SHarald Freudenberger /* 1007e80d4af0SHarald Freudenberger * File io functions 1008e80d4af0SHarald Freudenberger */ 1009e80d4af0SHarald Freudenberger 1010e80d4af0SHarald Freudenberger static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd, 1011e80d4af0SHarald Freudenberger unsigned long arg) 1012e80d4af0SHarald Freudenberger { 1013e80d4af0SHarald Freudenberger int rc; 1014e80d4af0SHarald Freudenberger 1015e80d4af0SHarald Freudenberger switch (cmd) { 1016e80d4af0SHarald Freudenberger case PKEY_GENSECK: { 1017e80d4af0SHarald Freudenberger struct pkey_genseck __user *ugs = (void __user *) arg; 1018e80d4af0SHarald Freudenberger struct pkey_genseck kgs; 1019e80d4af0SHarald Freudenberger 1020e80d4af0SHarald Freudenberger if (copy_from_user(&kgs, ugs, sizeof(kgs))) 1021e80d4af0SHarald Freudenberger return -EFAULT; 1022e80d4af0SHarald Freudenberger rc = pkey_genseckey(kgs.cardnr, kgs.domain, 1023e80d4af0SHarald Freudenberger kgs.keytype, &kgs.seckey); 1024e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_ioctl pkey_genseckey()=%d\n", rc); 1025e80d4af0SHarald Freudenberger if (rc) 1026e80d4af0SHarald Freudenberger break; 1027e80d4af0SHarald Freudenberger if (copy_to_user(ugs, &kgs, sizeof(kgs))) 1028e80d4af0SHarald Freudenberger return -EFAULT; 1029e80d4af0SHarald Freudenberger break; 1030e80d4af0SHarald Freudenberger } 1031e80d4af0SHarald Freudenberger case PKEY_CLR2SECK: { 1032e80d4af0SHarald Freudenberger struct pkey_clr2seck __user *ucs = (void __user *) arg; 1033e80d4af0SHarald Freudenberger struct pkey_clr2seck kcs; 1034e80d4af0SHarald Freudenberger 1035e80d4af0SHarald Freudenberger if (copy_from_user(&kcs, ucs, sizeof(kcs))) 1036e80d4af0SHarald Freudenberger return -EFAULT; 1037e80d4af0SHarald Freudenberger rc = pkey_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype, 1038e80d4af0SHarald Freudenberger &kcs.clrkey, &kcs.seckey); 1039e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_ioctl pkey_clr2seckey()=%d\n", rc); 1040e80d4af0SHarald Freudenberger if (rc) 1041e80d4af0SHarald Freudenberger break; 1042e80d4af0SHarald Freudenberger if (copy_to_user(ucs, &kcs, sizeof(kcs))) 1043e80d4af0SHarald Freudenberger return -EFAULT; 1044e80d4af0SHarald Freudenberger memzero_explicit(&kcs, sizeof(kcs)); 1045e80d4af0SHarald Freudenberger break; 1046e80d4af0SHarald Freudenberger } 1047e80d4af0SHarald Freudenberger case PKEY_SEC2PROTK: { 1048e80d4af0SHarald Freudenberger struct pkey_sec2protk __user *usp = (void __user *) arg; 1049e80d4af0SHarald Freudenberger struct pkey_sec2protk ksp; 1050e80d4af0SHarald Freudenberger 1051e80d4af0SHarald Freudenberger if (copy_from_user(&ksp, usp, sizeof(ksp))) 1052e80d4af0SHarald Freudenberger return -EFAULT; 1053e80d4af0SHarald Freudenberger rc = pkey_sec2protkey(ksp.cardnr, ksp.domain, 1054e80d4af0SHarald Freudenberger &ksp.seckey, &ksp.protkey); 1055e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_ioctl pkey_sec2protkey()=%d\n", rc); 1056e80d4af0SHarald Freudenberger if (rc) 1057e80d4af0SHarald Freudenberger break; 1058e80d4af0SHarald Freudenberger if (copy_to_user(usp, &ksp, sizeof(ksp))) 1059e80d4af0SHarald Freudenberger return -EFAULT; 1060e80d4af0SHarald Freudenberger break; 1061e80d4af0SHarald Freudenberger } 1062e80d4af0SHarald Freudenberger case PKEY_CLR2PROTK: { 1063e80d4af0SHarald Freudenberger struct pkey_clr2protk __user *ucp = (void __user *) arg; 1064e80d4af0SHarald Freudenberger struct pkey_clr2protk kcp; 1065e80d4af0SHarald Freudenberger 1066e80d4af0SHarald Freudenberger if (copy_from_user(&kcp, ucp, sizeof(kcp))) 1067e80d4af0SHarald Freudenberger return -EFAULT; 1068e80d4af0SHarald Freudenberger rc = pkey_clr2protkey(kcp.keytype, 1069e80d4af0SHarald Freudenberger &kcp.clrkey, &kcp.protkey); 1070e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_ioctl pkey_clr2protkey()=%d\n", rc); 1071e80d4af0SHarald Freudenberger if (rc) 1072e80d4af0SHarald Freudenberger break; 1073e80d4af0SHarald Freudenberger if (copy_to_user(ucp, &kcp, sizeof(kcp))) 1074e80d4af0SHarald Freudenberger return -EFAULT; 1075e80d4af0SHarald Freudenberger memzero_explicit(&kcp, sizeof(kcp)); 1076e80d4af0SHarald Freudenberger break; 1077e80d4af0SHarald Freudenberger } 1078e80d4af0SHarald Freudenberger case PKEY_FINDCARD: { 1079e80d4af0SHarald Freudenberger struct pkey_findcard __user *ufc = (void __user *) arg; 1080e80d4af0SHarald Freudenberger struct pkey_findcard kfc; 1081e80d4af0SHarald Freudenberger 1082e80d4af0SHarald Freudenberger if (copy_from_user(&kfc, ufc, sizeof(kfc))) 1083e80d4af0SHarald Freudenberger return -EFAULT; 1084e80d4af0SHarald Freudenberger rc = pkey_findcard(&kfc.seckey, 1085e80d4af0SHarald Freudenberger &kfc.cardnr, &kfc.domain, 1); 1086e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_ioctl pkey_findcard()=%d\n", rc); 1087e80d4af0SHarald Freudenberger if (rc) 1088e80d4af0SHarald Freudenberger break; 1089e80d4af0SHarald Freudenberger if (copy_to_user(ufc, &kfc, sizeof(kfc))) 1090e80d4af0SHarald Freudenberger return -EFAULT; 1091e80d4af0SHarald Freudenberger break; 1092e80d4af0SHarald Freudenberger } 1093e80d4af0SHarald Freudenberger case PKEY_SKEY2PKEY: { 1094e80d4af0SHarald Freudenberger struct pkey_skey2pkey __user *usp = (void __user *) arg; 1095e80d4af0SHarald Freudenberger struct pkey_skey2pkey ksp; 1096e80d4af0SHarald Freudenberger 1097e80d4af0SHarald Freudenberger if (copy_from_user(&ksp, usp, sizeof(ksp))) 1098e80d4af0SHarald Freudenberger return -EFAULT; 1099e80d4af0SHarald Freudenberger rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey); 1100e80d4af0SHarald Freudenberger DEBUG_DBG("pkey_ioctl pkey_skey2pkey()=%d\n", rc); 1101e80d4af0SHarald Freudenberger if (rc) 1102e80d4af0SHarald Freudenberger break; 1103e80d4af0SHarald Freudenberger if (copy_to_user(usp, &ksp, sizeof(ksp))) 1104e80d4af0SHarald Freudenberger return -EFAULT; 1105e80d4af0SHarald Freudenberger break; 1106e80d4af0SHarald Freudenberger } 1107e80d4af0SHarald Freudenberger default: 1108e80d4af0SHarald Freudenberger /* unknown/unsupported ioctl cmd */ 1109e80d4af0SHarald Freudenberger return -ENOTTY; 1110e80d4af0SHarald Freudenberger } 1111e80d4af0SHarald Freudenberger 1112e80d4af0SHarald Freudenberger return rc; 1113e80d4af0SHarald Freudenberger } 1114e80d4af0SHarald Freudenberger 1115e80d4af0SHarald Freudenberger /* 1116e80d4af0SHarald Freudenberger * Sysfs and file io operations 1117e80d4af0SHarald Freudenberger */ 1118e80d4af0SHarald Freudenberger static const struct file_operations pkey_fops = { 1119e80d4af0SHarald Freudenberger .owner = THIS_MODULE, 1120e80d4af0SHarald Freudenberger .open = nonseekable_open, 1121e80d4af0SHarald Freudenberger .llseek = no_llseek, 1122e80d4af0SHarald Freudenberger .unlocked_ioctl = pkey_unlocked_ioctl, 1123e80d4af0SHarald Freudenberger }; 1124e80d4af0SHarald Freudenberger 1125e80d4af0SHarald Freudenberger static struct miscdevice pkey_dev = { 1126e80d4af0SHarald Freudenberger .name = "pkey", 1127e80d4af0SHarald Freudenberger .minor = MISC_DYNAMIC_MINOR, 1128e80d4af0SHarald Freudenberger .mode = 0666, 1129e80d4af0SHarald Freudenberger .fops = &pkey_fops, 1130e80d4af0SHarald Freudenberger }; 1131e80d4af0SHarald Freudenberger 1132e80d4af0SHarald Freudenberger /* 1133e80d4af0SHarald Freudenberger * Module init 1134e80d4af0SHarald Freudenberger */ 1135e80d4af0SHarald Freudenberger int __init pkey_init(void) 1136e80d4af0SHarald Freudenberger { 1137e80d4af0SHarald Freudenberger cpacf_mask_t pckmo_functions; 1138e80d4af0SHarald Freudenberger 1139e80d4af0SHarald Freudenberger /* check for pckmo instructions available */ 1140e80d4af0SHarald Freudenberger if (!cpacf_query(CPACF_PCKMO, &pckmo_functions)) 1141e80d4af0SHarald Freudenberger return -EOPNOTSUPP; 1142e80d4af0SHarald Freudenberger if (!cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_128_KEY) || 1143e80d4af0SHarald Freudenberger !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_192_KEY) || 1144e80d4af0SHarald Freudenberger !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_256_KEY)) 1145e80d4af0SHarald Freudenberger return -EOPNOTSUPP; 1146e80d4af0SHarald Freudenberger 1147e80d4af0SHarald Freudenberger pkey_debug_init(); 1148e80d4af0SHarald Freudenberger 1149e80d4af0SHarald Freudenberger return misc_register(&pkey_dev); 1150e80d4af0SHarald Freudenberger } 1151e80d4af0SHarald Freudenberger 1152e80d4af0SHarald Freudenberger /* 1153e80d4af0SHarald Freudenberger * Module exit 1154e80d4af0SHarald Freudenberger */ 1155e80d4af0SHarald Freudenberger static void __exit pkey_exit(void) 1156e80d4af0SHarald Freudenberger { 1157e80d4af0SHarald Freudenberger misc_deregister(&pkey_dev); 1158e80d4af0SHarald Freudenberger mkvp_cache_free(); 1159e80d4af0SHarald Freudenberger pkey_debug_exit(); 1160e80d4af0SHarald Freudenberger } 1161e80d4af0SHarald Freudenberger 1162e80d4af0SHarald Freudenberger module_init(pkey_init); 1163e80d4af0SHarald Freudenberger module_exit(pkey_exit); 1164