Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * pkey device driver |
| 4 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5 | * Copyright IBM Corp. 2017,2019 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6 | * Author(s): Harald Freudenberger |
| 7 | */ |
| 8 | |
| 9 | #define KMSG_COMPONENT "pkey" |
| 10 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/miscdevice.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/kallsyms.h> |
| 18 | #include <linux/debugfs.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 19 | #include <linux/random.h> |
| 20 | #include <linux/cpufeature.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | #include <asm/zcrypt.h> |
| 22 | #include <asm/cpacf.h> |
| 23 | #include <asm/pkey.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 24 | #include <crypto/aes.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 25 | |
| 26 | #include "zcrypt_api.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 27 | #include "zcrypt_ccamisc.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 28 | |
| 29 | MODULE_LICENSE("GPL"); |
| 30 | MODULE_AUTHOR("IBM Corporation"); |
| 31 | MODULE_DESCRIPTION("s390 protected key interface"); |
| 32 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 33 | #define KEYBLOBBUFSIZE 8192 /* key buffer size used for internal processing */ |
| 34 | #define MAXAPQNSINLIST 64 /* max 64 apqns within a apqn list */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 36 | /* |
| 37 | * debug feature data and functions |
| 38 | */ |
| 39 | |
| 40 | static debug_info_t *debug_info; |
| 41 | |
| 42 | #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__) |
| 43 | #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__) |
| 44 | #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__) |
| 45 | #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__) |
| 46 | |
| 47 | static void __init pkey_debug_init(void) |
| 48 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 49 | /* 5 arguments per dbf entry (including the format string ptr) */ |
| 50 | debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 51 | debug_register_view(debug_info, &debug_sprintf_view); |
| 52 | debug_set_level(debug_info, 3); |
| 53 | } |
| 54 | |
| 55 | static void __exit pkey_debug_exit(void) |
| 56 | { |
| 57 | debug_unregister(debug_info); |
| 58 | } |
| 59 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 60 | /* inside view of a protected key token (only type 0x00 version 0x01) */ |
| 61 | struct protaeskeytoken { |
| 62 | u8 type; /* 0x00 for PAES specific key tokens */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 63 | u8 res0[3]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 64 | u8 version; /* should be 0x01 for protected AES key token */ |
| 65 | u8 res1[3]; |
| 66 | u32 keytype; /* key type, one of the PKEY_KEYTYPE values */ |
| 67 | u32 len; /* bytes actually stored in protkey[] */ |
| 68 | u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 69 | } __packed; |
| 70 | |
| 71 | /* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 72 | * Create a protected key from a clear key value. |
| 73 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | static int pkey_clr2protkey(u32 keytype, |
| 75 | const struct pkey_clrkey *clrkey, |
| 76 | struct pkey_protkey *protkey) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 78 | /* mask of available pckmo subfunctions */ |
| 79 | static cpacf_mask_t pckmo_functions; |
| 80 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | long fc; |
| 82 | int keysize; |
| 83 | u8 paramblock[64]; |
| 84 | |
| 85 | switch (keytype) { |
| 86 | case PKEY_KEYTYPE_AES_128: |
| 87 | keysize = 16; |
| 88 | fc = CPACF_PCKMO_ENC_AES_128_KEY; |
| 89 | break; |
| 90 | case PKEY_KEYTYPE_AES_192: |
| 91 | keysize = 24; |
| 92 | fc = CPACF_PCKMO_ENC_AES_192_KEY; |
| 93 | break; |
| 94 | case PKEY_KEYTYPE_AES_256: |
| 95 | keysize = 32; |
| 96 | fc = CPACF_PCKMO_ENC_AES_256_KEY; |
| 97 | break; |
| 98 | default: |
| 99 | DEBUG_ERR("%s unknown/unsupported keytype %d\n", |
| 100 | __func__, keytype); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 104 | /* Did we already check for PCKMO ? */ |
| 105 | if (!pckmo_functions.bytes[0]) { |
| 106 | /* no, so check now */ |
| 107 | if (!cpacf_query(CPACF_PCKMO, &pckmo_functions)) |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | /* check for the pckmo subfunction we need now */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 111 | if (!cpacf_test_func(&pckmo_functions, fc)) { |
| 112 | DEBUG_ERR("%s pckmo functions not available\n", __func__); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | /* prepare param block */ |
| 117 | memset(paramblock, 0, sizeof(paramblock)); |
| 118 | memcpy(paramblock, clrkey->clrkey, keysize); |
| 119 | |
| 120 | /* call the pckmo instruction */ |
| 121 | cpacf_pckmo(fc, paramblock); |
| 122 | |
| 123 | /* copy created protected key */ |
| 124 | protkey->type = keytype; |
| 125 | protkey->len = keysize + 32; |
| 126 | memcpy(protkey->protkey, paramblock, keysize + 32); |
| 127 | |
| 128 | return 0; |
| 129 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Find card and transform secure key into protected key. |
| 133 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 134 | static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 135 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 136 | int rc, verify; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 137 | u16 cardnr, domain; |
| 138 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 141 | * The cca_xxx2protkey call may fail when a card has been |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 142 | * addressed where the master key was changed after last fetch |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 143 | * of the mkvp into the cache. Try 3 times: First witout verify |
| 144 | * then with verify and last round with verify and old master |
| 145 | * key verification pattern match not ignored. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 146 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 147 | for (verify = 0; verify < 3; verify++) { |
| 148 | rc = cca_findcard(key, &cardnr, &domain, verify); |
| 149 | if (rc < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 150 | continue; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 151 | if (rc > 0 && verify < 2) |
| 152 | continue; |
| 153 | switch (hdr->version) { |
| 154 | case TOKVER_CCA_AES: |
| 155 | rc = cca_sec2protkey(cardnr, domain, |
| 156 | key, pkey->protkey, |
| 157 | &pkey->len, &pkey->type); |
| 158 | break; |
| 159 | case TOKVER_CCA_VLSC: |
| 160 | rc = cca_cipher2protkey(cardnr, domain, |
| 161 | key, pkey->protkey, |
| 162 | &pkey->len, &pkey->type); |
| 163 | break; |
| 164 | default: |
| 165 | return -EINVAL; |
| 166 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 167 | if (rc == 0) |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | if (rc) |
| 172 | DEBUG_DBG("%s failed rc=%d\n", __func__, rc); |
| 173 | |
| 174 | return rc; |
| 175 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * Verify key and give back some info about the key. |
| 179 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 180 | static int pkey_verifykey(const struct pkey_seckey *seckey, |
| 181 | u16 *pcardnr, u16 *pdomain, |
| 182 | u16 *pkeysize, u32 *pattributes) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 183 | { |
| 184 | struct secaeskeytoken *t = (struct secaeskeytoken *) seckey; |
| 185 | u16 cardnr, domain; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 186 | int rc; |
| 187 | |
| 188 | /* check the secure key for valid AES secure key */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 189 | rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 190 | if (rc) |
| 191 | goto out; |
| 192 | if (pattributes) |
| 193 | *pattributes = PKEY_VERIFY_ATTR_AES; |
| 194 | if (pkeysize) |
| 195 | *pkeysize = t->bitsize; |
| 196 | |
| 197 | /* try to find a card which can handle this key */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 198 | rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1); |
| 199 | if (rc < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 200 | goto out; |
| 201 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 202 | if (rc > 0) { |
| 203 | /* key mkvp matches to old master key mkvp */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 204 | DEBUG_DBG("%s secure key has old mkvp\n", __func__); |
| 205 | if (pattributes) |
| 206 | *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 207 | rc = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (pcardnr) |
| 211 | *pcardnr = cardnr; |
| 212 | if (pdomain) |
| 213 | *pdomain = domain; |
| 214 | |
| 215 | out: |
| 216 | DEBUG_DBG("%s rc=%d\n", __func__, rc); |
| 217 | return rc; |
| 218 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Generate a random protected key |
| 222 | */ |
| 223 | static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey) |
| 224 | { |
| 225 | struct pkey_clrkey clrkey; |
| 226 | int keysize; |
| 227 | int rc; |
| 228 | |
| 229 | switch (keytype) { |
| 230 | case PKEY_KEYTYPE_AES_128: |
| 231 | keysize = 16; |
| 232 | break; |
| 233 | case PKEY_KEYTYPE_AES_192: |
| 234 | keysize = 24; |
| 235 | break; |
| 236 | case PKEY_KEYTYPE_AES_256: |
| 237 | keysize = 32; |
| 238 | break; |
| 239 | default: |
| 240 | DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__, |
| 241 | keytype); |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | /* generate a dummy random clear key */ |
| 246 | get_random_bytes(clrkey.clrkey, keysize); |
| 247 | |
| 248 | /* convert it to a dummy protected key */ |
| 249 | rc = pkey_clr2protkey(keytype, &clrkey, protkey); |
| 250 | if (rc) |
| 251 | return rc; |
| 252 | |
| 253 | /* replace the key part of the protected key with random bytes */ |
| 254 | get_random_bytes(protkey->protkey, keysize); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Verify if a protected key is still valid |
| 261 | */ |
| 262 | static int pkey_verifyprotkey(const struct pkey_protkey *protkey) |
| 263 | { |
| 264 | unsigned long fc; |
| 265 | struct { |
| 266 | u8 iv[AES_BLOCK_SIZE]; |
| 267 | u8 key[MAXPROTKEYSIZE]; |
| 268 | } param; |
| 269 | u8 null_msg[AES_BLOCK_SIZE]; |
| 270 | u8 dest_buf[AES_BLOCK_SIZE]; |
| 271 | unsigned int k; |
| 272 | |
| 273 | switch (protkey->type) { |
| 274 | case PKEY_KEYTYPE_AES_128: |
| 275 | fc = CPACF_KMC_PAES_128; |
| 276 | break; |
| 277 | case PKEY_KEYTYPE_AES_192: |
| 278 | fc = CPACF_KMC_PAES_192; |
| 279 | break; |
| 280 | case PKEY_KEYTYPE_AES_256: |
| 281 | fc = CPACF_KMC_PAES_256; |
| 282 | break; |
| 283 | default: |
| 284 | DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__, |
| 285 | protkey->type); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
| 289 | memset(null_msg, 0, sizeof(null_msg)); |
| 290 | |
| 291 | memset(param.iv, 0, sizeof(param.iv)); |
| 292 | memcpy(param.key, protkey->protkey, sizeof(param.key)); |
| 293 | |
| 294 | k = cpacf_kmc(fc | CPACF_ENCRYPT, ¶m, null_msg, dest_buf, |
| 295 | sizeof(null_msg)); |
| 296 | if (k != sizeof(null_msg)) { |
| 297 | DEBUG_ERR("%s protected key is not valid\n", __func__); |
| 298 | return -EKEYREJECTED; |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Transform a non-CCA key token into a protected key |
| 306 | */ |
| 307 | static int pkey_nonccatok2pkey(const u8 *key, u32 keylen, |
| 308 | struct pkey_protkey *protkey) |
| 309 | { |
| 310 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
| 311 | struct protaeskeytoken *t; |
| 312 | |
| 313 | switch (hdr->version) { |
| 314 | case TOKVER_PROTECTED_KEY: |
| 315 | if (keylen != sizeof(struct protaeskeytoken)) |
| 316 | return -EINVAL; |
| 317 | |
| 318 | t = (struct protaeskeytoken *)key; |
| 319 | protkey->len = t->len; |
| 320 | protkey->type = t->keytype; |
| 321 | memcpy(protkey->protkey, t->protkey, |
| 322 | sizeof(protkey->protkey)); |
| 323 | |
| 324 | return pkey_verifyprotkey(protkey); |
| 325 | default: |
| 326 | DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n", |
| 327 | __func__, hdr->version); |
| 328 | return -EINVAL; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Transform a CCA internal key token into a protected key |
| 334 | */ |
| 335 | static int pkey_ccainttok2pkey(const u8 *key, u32 keylen, |
| 336 | struct pkey_protkey *protkey) |
| 337 | { |
| 338 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
| 339 | |
| 340 | switch (hdr->version) { |
| 341 | case TOKVER_CCA_AES: |
| 342 | if (keylen != sizeof(struct secaeskeytoken)) |
| 343 | return -EINVAL; |
| 344 | break; |
| 345 | case TOKVER_CCA_VLSC: |
| 346 | if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE) |
| 347 | return -EINVAL; |
| 348 | break; |
| 349 | default: |
| 350 | DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n", |
| 351 | __func__, hdr->version); |
| 352 | return -EINVAL; |
| 353 | } |
| 354 | |
| 355 | return pkey_skey2pkey(key, protkey); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Transform a key blob (of any type) into a protected key |
| 360 | */ |
| 361 | int pkey_keyblob2pkey(const u8 *key, u32 keylen, |
| 362 | struct pkey_protkey *protkey) |
| 363 | { |
| 364 | int rc; |
| 365 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
| 366 | |
| 367 | if (keylen < sizeof(struct keytoken_header)) { |
| 368 | DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | |
| 372 | switch (hdr->type) { |
| 373 | case TOKTYPE_NON_CCA: |
| 374 | rc = pkey_nonccatok2pkey(key, keylen, protkey); |
| 375 | break; |
| 376 | case TOKTYPE_CCA_INTERNAL: |
| 377 | rc = pkey_ccainttok2pkey(key, keylen, protkey); |
| 378 | break; |
| 379 | default: |
| 380 | DEBUG_ERR("%s unknown/unsupported blob type %d\n", |
| 381 | __func__, hdr->type); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | DEBUG_DBG("%s rc=%d\n", __func__, rc); |
| 386 | return rc; |
| 387 | |
| 388 | } |
| 389 | EXPORT_SYMBOL(pkey_keyblob2pkey); |
| 390 | |
| 391 | static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns, |
| 392 | enum pkey_key_type ktype, enum pkey_key_size ksize, |
| 393 | u32 kflags, u8 *keybuf, size_t *keybufsize) |
| 394 | { |
| 395 | int i, card, dom, rc; |
| 396 | |
| 397 | /* check for at least one apqn given */ |
| 398 | if (!apqns || !nr_apqns) |
| 399 | return -EINVAL; |
| 400 | |
| 401 | /* check key type and size */ |
| 402 | switch (ktype) { |
| 403 | case PKEY_TYPE_CCA_DATA: |
| 404 | case PKEY_TYPE_CCA_CIPHER: |
| 405 | if (*keybufsize < SECKEYBLOBSIZE) |
| 406 | return -EINVAL; |
| 407 | break; |
| 408 | default: |
| 409 | return -EINVAL; |
| 410 | } |
| 411 | switch (ksize) { |
| 412 | case PKEY_SIZE_AES_128: |
| 413 | case PKEY_SIZE_AES_192: |
| 414 | case PKEY_SIZE_AES_256: |
| 415 | break; |
| 416 | default: |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | |
| 420 | /* simple try all apqns from the list */ |
| 421 | for (i = 0, rc = -ENODEV; i < nr_apqns; i++) { |
| 422 | card = apqns[i].card; |
| 423 | dom = apqns[i].domain; |
| 424 | if (ktype == PKEY_TYPE_CCA_DATA) { |
| 425 | rc = cca_genseckey(card, dom, ksize, keybuf); |
| 426 | *keybufsize = (rc ? 0 : SECKEYBLOBSIZE); |
| 427 | } else /* TOKVER_CCA_VLSC */ |
| 428 | rc = cca_gencipherkey(card, dom, ksize, kflags, |
| 429 | keybuf, keybufsize); |
| 430 | if (rc == 0) |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | return rc; |
| 435 | } |
| 436 | |
| 437 | static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns, |
| 438 | enum pkey_key_type ktype, enum pkey_key_size ksize, |
| 439 | u32 kflags, const u8 *clrkey, |
| 440 | u8 *keybuf, size_t *keybufsize) |
| 441 | { |
| 442 | int i, card, dom, rc; |
| 443 | |
| 444 | /* check for at least one apqn given */ |
| 445 | if (!apqns || !nr_apqns) |
| 446 | return -EINVAL; |
| 447 | |
| 448 | /* check key type and size */ |
| 449 | switch (ktype) { |
| 450 | case PKEY_TYPE_CCA_DATA: |
| 451 | case PKEY_TYPE_CCA_CIPHER: |
| 452 | if (*keybufsize < SECKEYBLOBSIZE) |
| 453 | return -EINVAL; |
| 454 | break; |
| 455 | default: |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | switch (ksize) { |
| 459 | case PKEY_SIZE_AES_128: |
| 460 | case PKEY_SIZE_AES_192: |
| 461 | case PKEY_SIZE_AES_256: |
| 462 | break; |
| 463 | default: |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | /* simple try all apqns from the list */ |
| 468 | for (i = 0, rc = -ENODEV; i < nr_apqns; i++) { |
| 469 | card = apqns[i].card; |
| 470 | dom = apqns[i].domain; |
| 471 | if (ktype == PKEY_TYPE_CCA_DATA) { |
| 472 | rc = cca_clr2seckey(card, dom, ksize, |
| 473 | clrkey, keybuf); |
| 474 | *keybufsize = (rc ? 0 : SECKEYBLOBSIZE); |
| 475 | } else /* TOKVER_CCA_VLSC */ |
| 476 | rc = cca_clr2cipherkey(card, dom, ksize, kflags, |
| 477 | clrkey, keybuf, keybufsize); |
| 478 | if (rc == 0) |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | static int pkey_verifykey2(const u8 *key, size_t keylen, |
| 486 | u16 *cardnr, u16 *domain, |
| 487 | enum pkey_key_type *ktype, |
| 488 | enum pkey_key_size *ksize, u32 *flags) |
| 489 | { |
| 490 | int rc; |
| 491 | u32 _nr_apqns, *_apqns = NULL; |
| 492 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
| 493 | |
| 494 | if (keylen < sizeof(struct keytoken_header) || |
| 495 | hdr->type != TOKTYPE_CCA_INTERNAL) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | if (hdr->version == TOKVER_CCA_AES) { |
| 499 | struct secaeskeytoken *t = (struct secaeskeytoken *)key; |
| 500 | |
| 501 | rc = cca_check_secaeskeytoken(debug_info, 3, key, 0); |
| 502 | if (rc) |
| 503 | goto out; |
| 504 | if (ktype) |
| 505 | *ktype = PKEY_TYPE_CCA_DATA; |
| 506 | if (ksize) |
| 507 | *ksize = (enum pkey_key_size) t->bitsize; |
| 508 | |
| 509 | rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain, |
| 510 | ZCRYPT_CEX3C, t->mkvp, 0, 1); |
| 511 | if (rc == 0 && flags) |
| 512 | *flags = PKEY_FLAGS_MATCH_CUR_MKVP; |
| 513 | if (rc == -ENODEV) { |
| 514 | rc = cca_findcard2(&_apqns, &_nr_apqns, |
| 515 | *cardnr, *domain, |
| 516 | ZCRYPT_CEX3C, 0, t->mkvp, 1); |
| 517 | if (rc == 0 && flags) |
| 518 | *flags = PKEY_FLAGS_MATCH_ALT_MKVP; |
| 519 | } |
| 520 | if (rc) |
| 521 | goto out; |
| 522 | |
| 523 | *cardnr = ((struct pkey_apqn *)_apqns)->card; |
| 524 | *domain = ((struct pkey_apqn *)_apqns)->domain; |
| 525 | |
| 526 | } else if (hdr->version == TOKVER_CCA_VLSC) { |
| 527 | struct cipherkeytoken *t = (struct cipherkeytoken *)key; |
| 528 | |
| 529 | rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1); |
| 530 | if (rc) |
| 531 | goto out; |
| 532 | if (ktype) |
| 533 | *ktype = PKEY_TYPE_CCA_CIPHER; |
| 534 | if (ksize) { |
| 535 | *ksize = PKEY_SIZE_UNKNOWN; |
| 536 | if (!t->plfver && t->wpllen == 512) |
| 537 | *ksize = PKEY_SIZE_AES_128; |
| 538 | else if (!t->plfver && t->wpllen == 576) |
| 539 | *ksize = PKEY_SIZE_AES_192; |
| 540 | else if (!t->plfver && t->wpllen == 640) |
| 541 | *ksize = PKEY_SIZE_AES_256; |
| 542 | } |
| 543 | |
| 544 | rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain, |
| 545 | ZCRYPT_CEX6, t->mkvp0, 0, 1); |
| 546 | if (rc == 0 && flags) |
| 547 | *flags = PKEY_FLAGS_MATCH_CUR_MKVP; |
| 548 | if (rc == -ENODEV) { |
| 549 | rc = cca_findcard2(&_apqns, &_nr_apqns, |
| 550 | *cardnr, *domain, |
| 551 | ZCRYPT_CEX6, 0, t->mkvp0, 1); |
| 552 | if (rc == 0 && flags) |
| 553 | *flags = PKEY_FLAGS_MATCH_ALT_MKVP; |
| 554 | } |
| 555 | if (rc) |
| 556 | goto out; |
| 557 | |
| 558 | *cardnr = ((struct pkey_apqn *)_apqns)->card; |
| 559 | *domain = ((struct pkey_apqn *)_apqns)->domain; |
| 560 | |
| 561 | } else |
| 562 | rc = -EINVAL; |
| 563 | |
| 564 | out: |
| 565 | kfree(_apqns); |
| 566 | return rc; |
| 567 | } |
| 568 | |
| 569 | static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns, |
| 570 | const u8 *key, size_t keylen, |
| 571 | struct pkey_protkey *pkey) |
| 572 | { |
| 573 | int i, card, dom, rc; |
| 574 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
| 575 | |
| 576 | /* check for at least one apqn given */ |
| 577 | if (!apqns || !nr_apqns) |
| 578 | return -EINVAL; |
| 579 | |
| 580 | if (keylen < sizeof(struct keytoken_header)) |
| 581 | return -EINVAL; |
| 582 | |
| 583 | switch (hdr->type) { |
| 584 | case TOKTYPE_NON_CCA: |
| 585 | return pkey_nonccatok2pkey(key, keylen, pkey); |
| 586 | case TOKTYPE_CCA_INTERNAL: |
| 587 | switch (hdr->version) { |
| 588 | case TOKVER_CCA_AES: |
| 589 | if (keylen != sizeof(struct secaeskeytoken)) |
| 590 | return -EINVAL; |
| 591 | if (cca_check_secaeskeytoken(debug_info, 3, key, 0)) |
| 592 | return -EINVAL; |
| 593 | break; |
| 594 | case TOKVER_CCA_VLSC: |
| 595 | if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE) |
| 596 | return -EINVAL; |
| 597 | if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1)) |
| 598 | return -EINVAL; |
| 599 | break; |
| 600 | default: |
| 601 | DEBUG_ERR("%s unknown CCA internal token version %d\n", |
| 602 | __func__, hdr->version); |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | break; |
| 606 | default: |
| 607 | DEBUG_ERR("%s unknown/unsupported blob type %d\n", |
| 608 | __func__, hdr->type); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
| 612 | /* simple try all apqns from the list */ |
| 613 | for (i = 0, rc = -ENODEV; i < nr_apqns; i++) { |
| 614 | card = apqns[i].card; |
| 615 | dom = apqns[i].domain; |
| 616 | if (hdr->version == TOKVER_CCA_AES) |
| 617 | rc = cca_sec2protkey(card, dom, key, pkey->protkey, |
| 618 | &pkey->len, &pkey->type); |
| 619 | else /* TOKVER_CCA_VLSC */ |
| 620 | rc = cca_cipher2protkey(card, dom, key, pkey->protkey, |
| 621 | &pkey->len, &pkey->type); |
| 622 | if (rc == 0) |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | return rc; |
| 627 | } |
| 628 | |
| 629 | static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags, |
| 630 | struct pkey_apqn *apqns, size_t *nr_apqns) |
| 631 | { |
| 632 | int rc = EINVAL; |
| 633 | u32 _nr_apqns, *_apqns = NULL; |
| 634 | struct keytoken_header *hdr = (struct keytoken_header *)key; |
| 635 | |
| 636 | if (keylen < sizeof(struct keytoken_header) || |
| 637 | hdr->type != TOKTYPE_CCA_INTERNAL || |
| 638 | flags == 0) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | if (hdr->version == TOKVER_CCA_AES || hdr->version == TOKVER_CCA_VLSC) { |
| 642 | int minhwtype = ZCRYPT_CEX3C; |
| 643 | u64 cur_mkvp = 0, old_mkvp = 0; |
| 644 | |
| 645 | if (hdr->version == TOKVER_CCA_AES) { |
| 646 | struct secaeskeytoken *t = (struct secaeskeytoken *)key; |
| 647 | |
| 648 | if (flags & PKEY_FLAGS_MATCH_CUR_MKVP) |
| 649 | cur_mkvp = t->mkvp; |
| 650 | if (flags & PKEY_FLAGS_MATCH_ALT_MKVP) |
| 651 | old_mkvp = t->mkvp; |
| 652 | } else { |
| 653 | struct cipherkeytoken *t = (struct cipherkeytoken *)key; |
| 654 | |
| 655 | minhwtype = ZCRYPT_CEX6; |
| 656 | if (flags & PKEY_FLAGS_MATCH_CUR_MKVP) |
| 657 | cur_mkvp = t->mkvp0; |
| 658 | if (flags & PKEY_FLAGS_MATCH_ALT_MKVP) |
| 659 | old_mkvp = t->mkvp0; |
| 660 | } |
| 661 | rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF, |
| 662 | minhwtype, cur_mkvp, old_mkvp, 1); |
| 663 | if (rc) |
| 664 | goto out; |
| 665 | if (apqns) { |
| 666 | if (*nr_apqns < _nr_apqns) |
| 667 | rc = -ENOSPC; |
| 668 | else |
| 669 | memcpy(apqns, _apqns, _nr_apqns * sizeof(u32)); |
| 670 | } |
| 671 | *nr_apqns = _nr_apqns; |
| 672 | } |
| 673 | |
| 674 | out: |
| 675 | kfree(_apqns); |
| 676 | return rc; |
| 677 | } |
| 678 | |
| 679 | static int pkey_apqns4keytype(enum pkey_key_type ktype, |
| 680 | u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags, |
| 681 | struct pkey_apqn *apqns, size_t *nr_apqns) |
| 682 | { |
| 683 | int rc = -EINVAL; |
| 684 | u32 _nr_apqns, *_apqns = NULL; |
| 685 | |
| 686 | if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) { |
| 687 | u64 cur_mkvp = 0, old_mkvp = 0; |
| 688 | int minhwtype = ZCRYPT_CEX3C; |
| 689 | |
| 690 | if (flags & PKEY_FLAGS_MATCH_CUR_MKVP) |
| 691 | cur_mkvp = *((u64 *) cur_mkvp); |
| 692 | if (flags & PKEY_FLAGS_MATCH_ALT_MKVP) |
| 693 | old_mkvp = *((u64 *) alt_mkvp); |
| 694 | if (ktype == PKEY_TYPE_CCA_CIPHER) |
| 695 | minhwtype = ZCRYPT_CEX6; |
| 696 | rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF, |
| 697 | minhwtype, cur_mkvp, old_mkvp, 1); |
| 698 | if (rc) |
| 699 | goto out; |
| 700 | if (apqns) { |
| 701 | if (*nr_apqns < _nr_apqns) |
| 702 | rc = -ENOSPC; |
| 703 | else |
| 704 | memcpy(apqns, _apqns, _nr_apqns * sizeof(u32)); |
| 705 | } |
| 706 | *nr_apqns = _nr_apqns; |
| 707 | } |
| 708 | |
| 709 | out: |
| 710 | kfree(_apqns); |
| 711 | return rc; |
| 712 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * File io functions |
| 716 | */ |
| 717 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 718 | static void *_copy_key_from_user(void __user *ukey, size_t keylen) |
| 719 | { |
| 720 | void *kkey; |
| 721 | |
| 722 | if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE) |
| 723 | return ERR_PTR(-EINVAL); |
| 724 | kkey = kmalloc(keylen, GFP_KERNEL); |
| 725 | if (!kkey) |
| 726 | return ERR_PTR(-ENOMEM); |
| 727 | if (copy_from_user(kkey, ukey, keylen)) { |
| 728 | kfree(kkey); |
| 729 | return ERR_PTR(-EFAULT); |
| 730 | } |
| 731 | |
| 732 | return kkey; |
| 733 | } |
| 734 | |
| 735 | static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns) |
| 736 | { |
| 737 | void *kapqns = NULL; |
| 738 | size_t nbytes; |
| 739 | |
| 740 | if (uapqns && nr_apqns > 0) { |
| 741 | nbytes = nr_apqns * sizeof(struct pkey_apqn); |
| 742 | kapqns = kmalloc(nbytes, GFP_KERNEL); |
| 743 | if (!kapqns) |
| 744 | return ERR_PTR(-ENOMEM); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 745 | if (copy_from_user(kapqns, uapqns, nbytes)) { |
| 746 | kfree(kapqns); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 747 | return ERR_PTR(-EFAULT); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 748 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | return kapqns; |
| 752 | } |
| 753 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 754 | static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd, |
| 755 | unsigned long arg) |
| 756 | { |
| 757 | int rc; |
| 758 | |
| 759 | switch (cmd) { |
| 760 | case PKEY_GENSECK: { |
| 761 | struct pkey_genseck __user *ugs = (void __user *) arg; |
| 762 | struct pkey_genseck kgs; |
| 763 | |
| 764 | if (copy_from_user(&kgs, ugs, sizeof(kgs))) |
| 765 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 766 | rc = cca_genseckey(kgs.cardnr, kgs.domain, |
| 767 | kgs.keytype, kgs.seckey.seckey); |
| 768 | DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 769 | if (rc) |
| 770 | break; |
| 771 | if (copy_to_user(ugs, &kgs, sizeof(kgs))) |
| 772 | return -EFAULT; |
| 773 | break; |
| 774 | } |
| 775 | case PKEY_CLR2SECK: { |
| 776 | struct pkey_clr2seck __user *ucs = (void __user *) arg; |
| 777 | struct pkey_clr2seck kcs; |
| 778 | |
| 779 | if (copy_from_user(&kcs, ucs, sizeof(kcs))) |
| 780 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 781 | rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype, |
| 782 | kcs.clrkey.clrkey, kcs.seckey.seckey); |
| 783 | DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 784 | if (rc) |
| 785 | break; |
| 786 | if (copy_to_user(ucs, &kcs, sizeof(kcs))) |
| 787 | return -EFAULT; |
| 788 | memzero_explicit(&kcs, sizeof(kcs)); |
| 789 | break; |
| 790 | } |
| 791 | case PKEY_SEC2PROTK: { |
| 792 | struct pkey_sec2protk __user *usp = (void __user *) arg; |
| 793 | struct pkey_sec2protk ksp; |
| 794 | |
| 795 | if (copy_from_user(&ksp, usp, sizeof(ksp))) |
| 796 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 797 | rc = cca_sec2protkey(ksp.cardnr, ksp.domain, |
| 798 | ksp.seckey.seckey, ksp.protkey.protkey, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 799 | &ksp.protkey.len, &ksp.protkey.type); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 800 | DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 801 | if (rc) |
| 802 | break; |
| 803 | if (copy_to_user(usp, &ksp, sizeof(ksp))) |
| 804 | return -EFAULT; |
| 805 | break; |
| 806 | } |
| 807 | case PKEY_CLR2PROTK: { |
| 808 | struct pkey_clr2protk __user *ucp = (void __user *) arg; |
| 809 | struct pkey_clr2protk kcp; |
| 810 | |
| 811 | if (copy_from_user(&kcp, ucp, sizeof(kcp))) |
| 812 | return -EFAULT; |
| 813 | rc = pkey_clr2protkey(kcp.keytype, |
| 814 | &kcp.clrkey, &kcp.protkey); |
| 815 | DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc); |
| 816 | if (rc) |
| 817 | break; |
| 818 | if (copy_to_user(ucp, &kcp, sizeof(kcp))) |
| 819 | return -EFAULT; |
| 820 | memzero_explicit(&kcp, sizeof(kcp)); |
| 821 | break; |
| 822 | } |
| 823 | case PKEY_FINDCARD: { |
| 824 | struct pkey_findcard __user *ufc = (void __user *) arg; |
| 825 | struct pkey_findcard kfc; |
| 826 | |
| 827 | if (copy_from_user(&kfc, ufc, sizeof(kfc))) |
| 828 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 829 | rc = cca_findcard(kfc.seckey.seckey, |
| 830 | &kfc.cardnr, &kfc.domain, 1); |
| 831 | DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc); |
| 832 | if (rc < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 833 | break; |
| 834 | if (copy_to_user(ufc, &kfc, sizeof(kfc))) |
| 835 | return -EFAULT; |
| 836 | break; |
| 837 | } |
| 838 | case PKEY_SKEY2PKEY: { |
| 839 | struct pkey_skey2pkey __user *usp = (void __user *) arg; |
| 840 | struct pkey_skey2pkey ksp; |
| 841 | |
| 842 | if (copy_from_user(&ksp, usp, sizeof(ksp))) |
| 843 | return -EFAULT; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 844 | rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 845 | DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc); |
| 846 | if (rc) |
| 847 | break; |
| 848 | if (copy_to_user(usp, &ksp, sizeof(ksp))) |
| 849 | return -EFAULT; |
| 850 | break; |
| 851 | } |
| 852 | case PKEY_VERIFYKEY: { |
| 853 | struct pkey_verifykey __user *uvk = (void __user *) arg; |
| 854 | struct pkey_verifykey kvk; |
| 855 | |
| 856 | if (copy_from_user(&kvk, uvk, sizeof(kvk))) |
| 857 | return -EFAULT; |
| 858 | rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain, |
| 859 | &kvk.keysize, &kvk.attributes); |
| 860 | DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc); |
| 861 | if (rc) |
| 862 | break; |
| 863 | if (copy_to_user(uvk, &kvk, sizeof(kvk))) |
| 864 | return -EFAULT; |
| 865 | break; |
| 866 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 867 | case PKEY_GENPROTK: { |
| 868 | struct pkey_genprotk __user *ugp = (void __user *) arg; |
| 869 | struct pkey_genprotk kgp; |
| 870 | |
| 871 | if (copy_from_user(&kgp, ugp, sizeof(kgp))) |
| 872 | return -EFAULT; |
| 873 | rc = pkey_genprotkey(kgp.keytype, &kgp.protkey); |
| 874 | DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc); |
| 875 | if (rc) |
| 876 | break; |
| 877 | if (copy_to_user(ugp, &kgp, sizeof(kgp))) |
| 878 | return -EFAULT; |
| 879 | break; |
| 880 | } |
| 881 | case PKEY_VERIFYPROTK: { |
| 882 | struct pkey_verifyprotk __user *uvp = (void __user *) arg; |
| 883 | struct pkey_verifyprotk kvp; |
| 884 | |
| 885 | if (copy_from_user(&kvp, uvp, sizeof(kvp))) |
| 886 | return -EFAULT; |
| 887 | rc = pkey_verifyprotkey(&kvp.protkey); |
| 888 | DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc); |
| 889 | break; |
| 890 | } |
| 891 | case PKEY_KBLOB2PROTK: { |
| 892 | struct pkey_kblob2pkey __user *utp = (void __user *) arg; |
| 893 | struct pkey_kblob2pkey ktp; |
| 894 | u8 *kkey; |
| 895 | |
| 896 | if (copy_from_user(&ktp, utp, sizeof(ktp))) |
| 897 | return -EFAULT; |
| 898 | kkey = _copy_key_from_user(ktp.key, ktp.keylen); |
| 899 | if (IS_ERR(kkey)) |
| 900 | return PTR_ERR(kkey); |
| 901 | rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey); |
| 902 | DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc); |
| 903 | kfree(kkey); |
| 904 | if (rc) |
| 905 | break; |
| 906 | if (copy_to_user(utp, &ktp, sizeof(ktp))) |
| 907 | return -EFAULT; |
| 908 | break; |
| 909 | } |
| 910 | case PKEY_GENSECK2: { |
| 911 | struct pkey_genseck2 __user *ugs = (void __user *) arg; |
| 912 | struct pkey_genseck2 kgs; |
| 913 | struct pkey_apqn *apqns; |
| 914 | size_t klen = KEYBLOBBUFSIZE; |
| 915 | u8 *kkey; |
| 916 | |
| 917 | if (copy_from_user(&kgs, ugs, sizeof(kgs))) |
| 918 | return -EFAULT; |
| 919 | apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries); |
| 920 | if (IS_ERR(apqns)) |
| 921 | return PTR_ERR(apqns); |
| 922 | kkey = kmalloc(klen, GFP_KERNEL); |
| 923 | if (!kkey) { |
| 924 | kfree(apqns); |
| 925 | return -ENOMEM; |
| 926 | } |
| 927 | rc = pkey_genseckey2(apqns, kgs.apqn_entries, |
| 928 | kgs.type, kgs.size, kgs.keygenflags, |
| 929 | kkey, &klen); |
| 930 | DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc); |
| 931 | kfree(apqns); |
| 932 | if (rc) { |
| 933 | kfree(kkey); |
| 934 | break; |
| 935 | } |
| 936 | if (kgs.key) { |
| 937 | if (kgs.keylen < klen) { |
| 938 | kfree(kkey); |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | if (copy_to_user(kgs.key, kkey, klen)) { |
| 942 | kfree(kkey); |
| 943 | return -EFAULT; |
| 944 | } |
| 945 | } |
| 946 | kgs.keylen = klen; |
| 947 | if (copy_to_user(ugs, &kgs, sizeof(kgs))) |
| 948 | rc = -EFAULT; |
| 949 | kfree(kkey); |
| 950 | break; |
| 951 | } |
| 952 | case PKEY_CLR2SECK2: { |
| 953 | struct pkey_clr2seck2 __user *ucs = (void __user *) arg; |
| 954 | struct pkey_clr2seck2 kcs; |
| 955 | struct pkey_apqn *apqns; |
| 956 | size_t klen = KEYBLOBBUFSIZE; |
| 957 | u8 *kkey; |
| 958 | |
| 959 | if (copy_from_user(&kcs, ucs, sizeof(kcs))) |
| 960 | return -EFAULT; |
| 961 | apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries); |
| 962 | if (IS_ERR(apqns)) |
| 963 | return PTR_ERR(apqns); |
| 964 | kkey = kmalloc(klen, GFP_KERNEL); |
| 965 | if (!kkey) { |
| 966 | kfree(apqns); |
| 967 | return -ENOMEM; |
| 968 | } |
| 969 | rc = pkey_clr2seckey2(apqns, kcs.apqn_entries, |
| 970 | kcs.type, kcs.size, kcs.keygenflags, |
| 971 | kcs.clrkey.clrkey, kkey, &klen); |
| 972 | DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc); |
| 973 | kfree(apqns); |
| 974 | if (rc) { |
| 975 | kfree(kkey); |
| 976 | break; |
| 977 | } |
| 978 | if (kcs.key) { |
| 979 | if (kcs.keylen < klen) { |
| 980 | kfree(kkey); |
| 981 | return -EINVAL; |
| 982 | } |
| 983 | if (copy_to_user(kcs.key, kkey, klen)) { |
| 984 | kfree(kkey); |
| 985 | return -EFAULT; |
| 986 | } |
| 987 | } |
| 988 | kcs.keylen = klen; |
| 989 | if (copy_to_user(ucs, &kcs, sizeof(kcs))) |
| 990 | rc = -EFAULT; |
| 991 | memzero_explicit(&kcs, sizeof(kcs)); |
| 992 | kfree(kkey); |
| 993 | break; |
| 994 | } |
| 995 | case PKEY_VERIFYKEY2: { |
| 996 | struct pkey_verifykey2 __user *uvk = (void __user *) arg; |
| 997 | struct pkey_verifykey2 kvk; |
| 998 | u8 *kkey; |
| 999 | |
| 1000 | if (copy_from_user(&kvk, uvk, sizeof(kvk))) |
| 1001 | return -EFAULT; |
| 1002 | kkey = _copy_key_from_user(kvk.key, kvk.keylen); |
| 1003 | if (IS_ERR(kkey)) |
| 1004 | return PTR_ERR(kkey); |
| 1005 | rc = pkey_verifykey2(kkey, kvk.keylen, |
| 1006 | &kvk.cardnr, &kvk.domain, |
| 1007 | &kvk.type, &kvk.size, &kvk.flags); |
| 1008 | DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc); |
| 1009 | kfree(kkey); |
| 1010 | if (rc) |
| 1011 | break; |
| 1012 | if (copy_to_user(uvk, &kvk, sizeof(kvk))) |
| 1013 | return -EFAULT; |
| 1014 | break; |
| 1015 | } |
| 1016 | case PKEY_KBLOB2PROTK2: { |
| 1017 | struct pkey_kblob2pkey2 __user *utp = (void __user *) arg; |
| 1018 | struct pkey_kblob2pkey2 ktp; |
| 1019 | struct pkey_apqn *apqns = NULL; |
| 1020 | u8 *kkey; |
| 1021 | |
| 1022 | if (copy_from_user(&ktp, utp, sizeof(ktp))) |
| 1023 | return -EFAULT; |
| 1024 | apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries); |
| 1025 | if (IS_ERR(apqns)) |
| 1026 | return PTR_ERR(apqns); |
| 1027 | kkey = _copy_key_from_user(ktp.key, ktp.keylen); |
| 1028 | if (IS_ERR(kkey)) { |
| 1029 | kfree(apqns); |
| 1030 | return PTR_ERR(kkey); |
| 1031 | } |
| 1032 | rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries, |
| 1033 | kkey, ktp.keylen, &ktp.protkey); |
| 1034 | DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc); |
| 1035 | kfree(apqns); |
| 1036 | kfree(kkey); |
| 1037 | if (rc) |
| 1038 | break; |
| 1039 | if (copy_to_user(utp, &ktp, sizeof(ktp))) |
| 1040 | return -EFAULT; |
| 1041 | break; |
| 1042 | } |
| 1043 | case PKEY_APQNS4K: { |
| 1044 | struct pkey_apqns4key __user *uak = (void __user *) arg; |
| 1045 | struct pkey_apqns4key kak; |
| 1046 | struct pkey_apqn *apqns = NULL; |
| 1047 | size_t nr_apqns, len; |
| 1048 | u8 *kkey; |
| 1049 | |
| 1050 | if (copy_from_user(&kak, uak, sizeof(kak))) |
| 1051 | return -EFAULT; |
| 1052 | nr_apqns = kak.apqn_entries; |
| 1053 | if (nr_apqns) { |
| 1054 | apqns = kmalloc_array(nr_apqns, |
| 1055 | sizeof(struct pkey_apqn), |
| 1056 | GFP_KERNEL); |
| 1057 | if (!apqns) |
| 1058 | return -ENOMEM; |
| 1059 | } |
| 1060 | kkey = _copy_key_from_user(kak.key, kak.keylen); |
| 1061 | if (IS_ERR(kkey)) { |
| 1062 | kfree(apqns); |
| 1063 | return PTR_ERR(kkey); |
| 1064 | } |
| 1065 | rc = pkey_apqns4key(kkey, kak.keylen, kak.flags, |
| 1066 | apqns, &nr_apqns); |
| 1067 | DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc); |
| 1068 | kfree(kkey); |
| 1069 | if (rc && rc != -ENOSPC) { |
| 1070 | kfree(apqns); |
| 1071 | break; |
| 1072 | } |
| 1073 | if (!rc && kak.apqns) { |
| 1074 | if (nr_apqns > kak.apqn_entries) { |
| 1075 | kfree(apqns); |
| 1076 | return -EINVAL; |
| 1077 | } |
| 1078 | len = nr_apqns * sizeof(struct pkey_apqn); |
| 1079 | if (len) { |
| 1080 | if (copy_to_user(kak.apqns, apqns, len)) { |
| 1081 | kfree(apqns); |
| 1082 | return -EFAULT; |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | kak.apqn_entries = nr_apqns; |
| 1087 | if (copy_to_user(uak, &kak, sizeof(kak))) |
| 1088 | rc = -EFAULT; |
| 1089 | kfree(apqns); |
| 1090 | break; |
| 1091 | } |
| 1092 | case PKEY_APQNS4KT: { |
| 1093 | struct pkey_apqns4keytype __user *uat = (void __user *) arg; |
| 1094 | struct pkey_apqns4keytype kat; |
| 1095 | struct pkey_apqn *apqns = NULL; |
| 1096 | size_t nr_apqns, len; |
| 1097 | |
| 1098 | if (copy_from_user(&kat, uat, sizeof(kat))) |
| 1099 | return -EFAULT; |
| 1100 | nr_apqns = kat.apqn_entries; |
| 1101 | if (nr_apqns) { |
| 1102 | apqns = kmalloc_array(nr_apqns, |
| 1103 | sizeof(struct pkey_apqn), |
| 1104 | GFP_KERNEL); |
| 1105 | if (!apqns) |
| 1106 | return -ENOMEM; |
| 1107 | } |
| 1108 | rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp, |
| 1109 | kat.flags, apqns, &nr_apqns); |
| 1110 | DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc); |
| 1111 | if (rc && rc != -ENOSPC) { |
| 1112 | kfree(apqns); |
| 1113 | break; |
| 1114 | } |
| 1115 | if (!rc && kat.apqns) { |
| 1116 | if (nr_apqns > kat.apqn_entries) { |
| 1117 | kfree(apqns); |
| 1118 | return -EINVAL; |
| 1119 | } |
| 1120 | len = nr_apqns * sizeof(struct pkey_apqn); |
| 1121 | if (len) { |
| 1122 | if (copy_to_user(kat.apqns, apqns, len)) { |
| 1123 | kfree(apqns); |
| 1124 | return -EFAULT; |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | kat.apqn_entries = nr_apqns; |
| 1129 | if (copy_to_user(uat, &kat, sizeof(kat))) |
| 1130 | rc = -EFAULT; |
| 1131 | kfree(apqns); |
| 1132 | break; |
| 1133 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1134 | default: |
| 1135 | /* unknown/unsupported ioctl cmd */ |
| 1136 | return -ENOTTY; |
| 1137 | } |
| 1138 | |
| 1139 | return rc; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Sysfs and file io operations |
| 1144 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1145 | |
| 1146 | /* |
| 1147 | * Sysfs attribute read function for all protected key binary attributes. |
| 1148 | * The implementation can not deal with partial reads, because a new random |
| 1149 | * protected key blob is generated with each read. In case of partial reads |
| 1150 | * (i.e. off != 0 or count < key blob size) -EINVAL is returned. |
| 1151 | */ |
| 1152 | static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf, |
| 1153 | loff_t off, size_t count) |
| 1154 | { |
| 1155 | struct protaeskeytoken protkeytoken; |
| 1156 | struct pkey_protkey protkey; |
| 1157 | int rc; |
| 1158 | |
| 1159 | if (off != 0 || count < sizeof(protkeytoken)) |
| 1160 | return -EINVAL; |
| 1161 | if (is_xts) |
| 1162 | if (count < 2 * sizeof(protkeytoken)) |
| 1163 | return -EINVAL; |
| 1164 | |
| 1165 | memset(&protkeytoken, 0, sizeof(protkeytoken)); |
| 1166 | protkeytoken.type = TOKTYPE_NON_CCA; |
| 1167 | protkeytoken.version = TOKVER_PROTECTED_KEY; |
| 1168 | protkeytoken.keytype = keytype; |
| 1169 | |
| 1170 | rc = pkey_genprotkey(protkeytoken.keytype, &protkey); |
| 1171 | if (rc) |
| 1172 | return rc; |
| 1173 | |
| 1174 | protkeytoken.len = protkey.len; |
| 1175 | memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len); |
| 1176 | |
| 1177 | memcpy(buf, &protkeytoken, sizeof(protkeytoken)); |
| 1178 | |
| 1179 | if (is_xts) { |
| 1180 | rc = pkey_genprotkey(protkeytoken.keytype, &protkey); |
| 1181 | if (rc) |
| 1182 | return rc; |
| 1183 | |
| 1184 | protkeytoken.len = protkey.len; |
| 1185 | memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len); |
| 1186 | |
| 1187 | memcpy(buf + sizeof(protkeytoken), &protkeytoken, |
| 1188 | sizeof(protkeytoken)); |
| 1189 | |
| 1190 | return 2 * sizeof(protkeytoken); |
| 1191 | } |
| 1192 | |
| 1193 | return sizeof(protkeytoken); |
| 1194 | } |
| 1195 | |
| 1196 | static ssize_t protkey_aes_128_read(struct file *filp, |
| 1197 | struct kobject *kobj, |
| 1198 | struct bin_attribute *attr, |
| 1199 | char *buf, loff_t off, |
| 1200 | size_t count) |
| 1201 | { |
| 1202 | return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf, |
| 1203 | off, count); |
| 1204 | } |
| 1205 | |
| 1206 | static ssize_t protkey_aes_192_read(struct file *filp, |
| 1207 | struct kobject *kobj, |
| 1208 | struct bin_attribute *attr, |
| 1209 | char *buf, loff_t off, |
| 1210 | size_t count) |
| 1211 | { |
| 1212 | return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf, |
| 1213 | off, count); |
| 1214 | } |
| 1215 | |
| 1216 | static ssize_t protkey_aes_256_read(struct file *filp, |
| 1217 | struct kobject *kobj, |
| 1218 | struct bin_attribute *attr, |
| 1219 | char *buf, loff_t off, |
| 1220 | size_t count) |
| 1221 | { |
| 1222 | return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf, |
| 1223 | off, count); |
| 1224 | } |
| 1225 | |
| 1226 | static ssize_t protkey_aes_128_xts_read(struct file *filp, |
| 1227 | struct kobject *kobj, |
| 1228 | struct bin_attribute *attr, |
| 1229 | char *buf, loff_t off, |
| 1230 | size_t count) |
| 1231 | { |
| 1232 | return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf, |
| 1233 | off, count); |
| 1234 | } |
| 1235 | |
| 1236 | static ssize_t protkey_aes_256_xts_read(struct file *filp, |
| 1237 | struct kobject *kobj, |
| 1238 | struct bin_attribute *attr, |
| 1239 | char *buf, loff_t off, |
| 1240 | size_t count) |
| 1241 | { |
| 1242 | return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf, |
| 1243 | off, count); |
| 1244 | } |
| 1245 | |
| 1246 | static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken)); |
| 1247 | static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken)); |
| 1248 | static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken)); |
| 1249 | static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken)); |
| 1250 | static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken)); |
| 1251 | |
| 1252 | static struct bin_attribute *protkey_attrs[] = { |
| 1253 | &bin_attr_protkey_aes_128, |
| 1254 | &bin_attr_protkey_aes_192, |
| 1255 | &bin_attr_protkey_aes_256, |
| 1256 | &bin_attr_protkey_aes_128_xts, |
| 1257 | &bin_attr_protkey_aes_256_xts, |
| 1258 | NULL |
| 1259 | }; |
| 1260 | |
| 1261 | static struct attribute_group protkey_attr_group = { |
| 1262 | .name = "protkey", |
| 1263 | .bin_attrs = protkey_attrs, |
| 1264 | }; |
| 1265 | |
| 1266 | /* |
| 1267 | * Sysfs attribute read function for all secure key ccadata binary attributes. |
| 1268 | * The implementation can not deal with partial reads, because a new random |
| 1269 | * protected key blob is generated with each read. In case of partial reads |
| 1270 | * (i.e. off != 0 or count < key blob size) -EINVAL is returned. |
| 1271 | */ |
| 1272 | static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf, |
| 1273 | loff_t off, size_t count) |
| 1274 | { |
| 1275 | int rc; |
| 1276 | struct pkey_seckey *seckey = (struct pkey_seckey *) buf; |
| 1277 | |
| 1278 | if (off != 0 || count < sizeof(struct secaeskeytoken)) |
| 1279 | return -EINVAL; |
| 1280 | if (is_xts) |
| 1281 | if (count < 2 * sizeof(struct secaeskeytoken)) |
| 1282 | return -EINVAL; |
| 1283 | |
| 1284 | rc = cca_genseckey(-1, -1, keytype, seckey->seckey); |
| 1285 | if (rc) |
| 1286 | return rc; |
| 1287 | |
| 1288 | if (is_xts) { |
| 1289 | seckey++; |
| 1290 | rc = cca_genseckey(-1, -1, keytype, seckey->seckey); |
| 1291 | if (rc) |
| 1292 | return rc; |
| 1293 | |
| 1294 | return 2 * sizeof(struct secaeskeytoken); |
| 1295 | } |
| 1296 | |
| 1297 | return sizeof(struct secaeskeytoken); |
| 1298 | } |
| 1299 | |
| 1300 | static ssize_t ccadata_aes_128_read(struct file *filp, |
| 1301 | struct kobject *kobj, |
| 1302 | struct bin_attribute *attr, |
| 1303 | char *buf, loff_t off, |
| 1304 | size_t count) |
| 1305 | { |
| 1306 | return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf, |
| 1307 | off, count); |
| 1308 | } |
| 1309 | |
| 1310 | static ssize_t ccadata_aes_192_read(struct file *filp, |
| 1311 | struct kobject *kobj, |
| 1312 | struct bin_attribute *attr, |
| 1313 | char *buf, loff_t off, |
| 1314 | size_t count) |
| 1315 | { |
| 1316 | return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf, |
| 1317 | off, count); |
| 1318 | } |
| 1319 | |
| 1320 | static ssize_t ccadata_aes_256_read(struct file *filp, |
| 1321 | struct kobject *kobj, |
| 1322 | struct bin_attribute *attr, |
| 1323 | char *buf, loff_t off, |
| 1324 | size_t count) |
| 1325 | { |
| 1326 | return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf, |
| 1327 | off, count); |
| 1328 | } |
| 1329 | |
| 1330 | static ssize_t ccadata_aes_128_xts_read(struct file *filp, |
| 1331 | struct kobject *kobj, |
| 1332 | struct bin_attribute *attr, |
| 1333 | char *buf, loff_t off, |
| 1334 | size_t count) |
| 1335 | { |
| 1336 | return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf, |
| 1337 | off, count); |
| 1338 | } |
| 1339 | |
| 1340 | static ssize_t ccadata_aes_256_xts_read(struct file *filp, |
| 1341 | struct kobject *kobj, |
| 1342 | struct bin_attribute *attr, |
| 1343 | char *buf, loff_t off, |
| 1344 | size_t count) |
| 1345 | { |
| 1346 | return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf, |
| 1347 | off, count); |
| 1348 | } |
| 1349 | |
| 1350 | static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken)); |
| 1351 | static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken)); |
| 1352 | static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken)); |
| 1353 | static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken)); |
| 1354 | static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken)); |
| 1355 | |
| 1356 | static struct bin_attribute *ccadata_attrs[] = { |
| 1357 | &bin_attr_ccadata_aes_128, |
| 1358 | &bin_attr_ccadata_aes_192, |
| 1359 | &bin_attr_ccadata_aes_256, |
| 1360 | &bin_attr_ccadata_aes_128_xts, |
| 1361 | &bin_attr_ccadata_aes_256_xts, |
| 1362 | NULL |
| 1363 | }; |
| 1364 | |
| 1365 | static struct attribute_group ccadata_attr_group = { |
| 1366 | .name = "ccadata", |
| 1367 | .bin_attrs = ccadata_attrs, |
| 1368 | }; |
| 1369 | |
| 1370 | #define CCACIPHERTOKENSIZE (sizeof(struct cipherkeytoken) + 80) |
| 1371 | |
| 1372 | /* |
| 1373 | * Sysfs attribute read function for all secure key ccacipher binary attributes. |
| 1374 | * The implementation can not deal with partial reads, because a new random |
| 1375 | * secure key blob is generated with each read. In case of partial reads |
| 1376 | * (i.e. off != 0 or count < key blob size) -EINVAL is returned. |
| 1377 | */ |
| 1378 | static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits, |
| 1379 | bool is_xts, char *buf, loff_t off, |
| 1380 | size_t count) |
| 1381 | { |
| 1382 | size_t keysize; |
| 1383 | int rc; |
| 1384 | |
| 1385 | if (off != 0 || count < CCACIPHERTOKENSIZE) |
| 1386 | return -EINVAL; |
| 1387 | if (is_xts) |
| 1388 | if (count < 2 * CCACIPHERTOKENSIZE) |
| 1389 | return -EINVAL; |
| 1390 | |
| 1391 | keysize = CCACIPHERTOKENSIZE; |
| 1392 | rc = cca_gencipherkey(-1, -1, keybits, 0, buf, &keysize); |
| 1393 | if (rc) |
| 1394 | return rc; |
| 1395 | memset(buf + keysize, 0, CCACIPHERTOKENSIZE - keysize); |
| 1396 | |
| 1397 | if (is_xts) { |
| 1398 | keysize = CCACIPHERTOKENSIZE; |
| 1399 | rc = cca_gencipherkey(-1, -1, keybits, 0, |
| 1400 | buf + CCACIPHERTOKENSIZE, &keysize); |
| 1401 | if (rc) |
| 1402 | return rc; |
| 1403 | memset(buf + CCACIPHERTOKENSIZE + keysize, 0, |
| 1404 | CCACIPHERTOKENSIZE - keysize); |
| 1405 | |
| 1406 | return 2 * CCACIPHERTOKENSIZE; |
| 1407 | } |
| 1408 | |
| 1409 | return CCACIPHERTOKENSIZE; |
| 1410 | } |
| 1411 | |
| 1412 | static ssize_t ccacipher_aes_128_read(struct file *filp, |
| 1413 | struct kobject *kobj, |
| 1414 | struct bin_attribute *attr, |
| 1415 | char *buf, loff_t off, |
| 1416 | size_t count) |
| 1417 | { |
| 1418 | return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf, |
| 1419 | off, count); |
| 1420 | } |
| 1421 | |
| 1422 | static ssize_t ccacipher_aes_192_read(struct file *filp, |
| 1423 | struct kobject *kobj, |
| 1424 | struct bin_attribute *attr, |
| 1425 | char *buf, loff_t off, |
| 1426 | size_t count) |
| 1427 | { |
| 1428 | return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf, |
| 1429 | off, count); |
| 1430 | } |
| 1431 | |
| 1432 | static ssize_t ccacipher_aes_256_read(struct file *filp, |
| 1433 | struct kobject *kobj, |
| 1434 | struct bin_attribute *attr, |
| 1435 | char *buf, loff_t off, |
| 1436 | size_t count) |
| 1437 | { |
| 1438 | return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf, |
| 1439 | off, count); |
| 1440 | } |
| 1441 | |
| 1442 | static ssize_t ccacipher_aes_128_xts_read(struct file *filp, |
| 1443 | struct kobject *kobj, |
| 1444 | struct bin_attribute *attr, |
| 1445 | char *buf, loff_t off, |
| 1446 | size_t count) |
| 1447 | { |
| 1448 | return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf, |
| 1449 | off, count); |
| 1450 | } |
| 1451 | |
| 1452 | static ssize_t ccacipher_aes_256_xts_read(struct file *filp, |
| 1453 | struct kobject *kobj, |
| 1454 | struct bin_attribute *attr, |
| 1455 | char *buf, loff_t off, |
| 1456 | size_t count) |
| 1457 | { |
| 1458 | return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf, |
| 1459 | off, count); |
| 1460 | } |
| 1461 | |
| 1462 | static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE); |
| 1463 | static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE); |
| 1464 | static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE); |
| 1465 | static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE); |
| 1466 | static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE); |
| 1467 | |
| 1468 | static struct bin_attribute *ccacipher_attrs[] = { |
| 1469 | &bin_attr_ccacipher_aes_128, |
| 1470 | &bin_attr_ccacipher_aes_192, |
| 1471 | &bin_attr_ccacipher_aes_256, |
| 1472 | &bin_attr_ccacipher_aes_128_xts, |
| 1473 | &bin_attr_ccacipher_aes_256_xts, |
| 1474 | NULL |
| 1475 | }; |
| 1476 | |
| 1477 | static struct attribute_group ccacipher_attr_group = { |
| 1478 | .name = "ccacipher", |
| 1479 | .bin_attrs = ccacipher_attrs, |
| 1480 | }; |
| 1481 | |
| 1482 | static const struct attribute_group *pkey_attr_groups[] = { |
| 1483 | &protkey_attr_group, |
| 1484 | &ccadata_attr_group, |
| 1485 | &ccacipher_attr_group, |
| 1486 | NULL, |
| 1487 | }; |
| 1488 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1489 | static const struct file_operations pkey_fops = { |
| 1490 | .owner = THIS_MODULE, |
| 1491 | .open = nonseekable_open, |
| 1492 | .llseek = no_llseek, |
| 1493 | .unlocked_ioctl = pkey_unlocked_ioctl, |
| 1494 | }; |
| 1495 | |
| 1496 | static struct miscdevice pkey_dev = { |
| 1497 | .name = "pkey", |
| 1498 | .minor = MISC_DYNAMIC_MINOR, |
| 1499 | .mode = 0666, |
| 1500 | .fops = &pkey_fops, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1501 | .groups = pkey_attr_groups, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1502 | }; |
| 1503 | |
| 1504 | /* |
| 1505 | * Module init |
| 1506 | */ |
| 1507 | static int __init pkey_init(void) |
| 1508 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1509 | cpacf_mask_t func_mask; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1510 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1511 | /* |
| 1512 | * The pckmo instruction should be available - even if we don't |
| 1513 | * actually invoke it. This instruction comes with MSA 3 which |
| 1514 | * is also the minimum level for the kmc instructions which |
| 1515 | * are able to work with protected keys. |
| 1516 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1517 | if (!cpacf_query(CPACF_PCKMO, &func_mask)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1518 | return -ENODEV; |
| 1519 | |
| 1520 | /* check for kmc instructions available */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1521 | if (!cpacf_query(CPACF_KMC, &func_mask)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1522 | return -ENODEV; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1523 | if (!cpacf_test_func(&func_mask, CPACF_KMC_PAES_128) || |
| 1524 | !cpacf_test_func(&func_mask, CPACF_KMC_PAES_192) || |
| 1525 | !cpacf_test_func(&func_mask, CPACF_KMC_PAES_256)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1526 | return -ENODEV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1527 | |
| 1528 | pkey_debug_init(); |
| 1529 | |
| 1530 | return misc_register(&pkey_dev); |
| 1531 | } |
| 1532 | |
| 1533 | /* |
| 1534 | * Module exit |
| 1535 | */ |
| 1536 | static void __exit pkey_exit(void) |
| 1537 | { |
| 1538 | misc_deregister(&pkey_dev); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1539 | pkey_debug_exit(); |
| 1540 | } |
| 1541 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1542 | module_cpu_feature_match(MSA, pkey_init); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1543 | module_exit(pkey_exit); |