| 1 | |
| 2 | |
| 3 | * Copyright IBM Corp. 2019
|
| 4 | * Author(s): Harald Freudenberger <[email protected]>
|
| 5 | *
|
| 6 | * Collection of EP11 misc functions used by zcrypt and pkey
|
| 7 |
|
| 8 |
|
| 9 | #define pr_fmt(fmt) "zcrypt: " fmt
|
| 10 |
|
| 11 | #include <linux/export.h>
|
| 12 | #include <linux/init.h>
|
| 13 | #include <linux/mempool.h>
|
| 14 | #include <linux/module.h>
|
| 15 | #include <linux/random.h>
|
| 16 | #include <linux/slab.h>
|
| 17 | #include <linux/align.h>
|
| 18 | #include <asm/zcrypt.h>
|
| 19 | #include <asm/pkey.h>
|
| 20 | #include <crypto/aes.h>
|
| 21 |
|
| 22 | #include "ap_bus.h"
|
| 23 | #include "zcrypt_api.h"
|
| 24 | #include "zcrypt_debug.h"
|
| 25 | #include "zcrypt_msgtype6.h"
|
| 26 | #include "zcrypt_ep11misc.h"
|
| 27 | #include "zcrypt_ccamisc.h"
|
| 28 |
|
| 29 | #define EP11_PINBLOB_V1_BYTES 56
|
| 30 |
|
| 31 |
|
| 32 | static const u8 def_iv[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
| 33 | 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
|
| 34 |
|
| 35 | |
| 36 | * Cprb memory pool held for urgent cases where no memory
|
| 37 | * can be allocated via kmalloc. This pool is only used when
|
| 38 | * alloc_cprbmem() is called with the xflag ZCRYPT_XFLAG_NOMEMALLOC.
|
| 39 |
|
| 40 | #define CPRB_MEMPOOL_ITEM_SIZE (8 * 1024)
|
| 41 | static mempool_t *cprb_mempool;
|
| 42 |
|
| 43 | |
| 44 | * This is a pre-allocated memory for the device status array
|
| 45 | * used within the ep11_findcard2() function. It is currently
|
| 46 | * 128 * 128 * 4 bytes = 64 KB big. Usage of this memory is
|
| 47 | * controlled via dev_status_mem_mutex. Needs adaption if more
|
| 48 | * than 128 cards or domains to be are supported.
|
| 49 |
|
| 50 | #define ZCRYPT_DEV_STATUS_CARD_MAX 128
|
| 51 | #define ZCRYPT_DEV_STATUS_QUEUE_MAX 128
|
| 52 | #define ZCRYPT_DEV_STATUS_ENTRIES (ZCRYPT_DEV_STATUS_CARD_MAX * \
|
| 53 | ZCRYPT_DEV_STATUS_QUEUE_MAX)
|
| 54 | #define ZCRYPT_DEV_STATUS_EXT_SIZE (ZCRYPT_DEV_STATUS_ENTRIES * \
|
| 55 | sizeof(struct zcrypt_device_status_ext))
|
| 56 | static void *dev_status_mem;
|
| 57 | static DEFINE_MUTEX(dev_status_mem_mutex);
|
| 58 |
|
| 59 | static int ep11_kb_split(const u8 *kb, size_t kblen, u32 kbver,
|
| 60 | struct ep11kblob_header **kbhdr, size_t *kbhdrsize,
|
| 61 | u8 **kbpl, size_t *kbplsize)
|
| 62 | {
|
| 63 | struct ep11kblob_header *hdr = NULL;
|
| 64 | size_t hdrsize, plsize = 0;
|
| 65 | int rc = -EINVAL;
|
| 66 | u8 *pl = NULL;
|
| 67 |
|
| 68 | if (kblen < sizeof(struct ep11kblob_header))
|
| 69 | goto out;
|
| 70 | hdr = (struct ep11kblob_header *)kb;
|
| 71 |
|
| 72 | switch (kbver) {
|
| 73 | case TOKVER_EP11_AES:
|
| 74 |
|
| 75 | hdrsize = 0;
|
| 76 | break;
|
| 77 | case TOKVER_EP11_ECC_WITH_HEADER:
|
| 78 | case TOKVER_EP11_AES_WITH_HEADER:
|
| 79 |
|
| 80 | hdrsize = sizeof(struct ep11kblob_header);
|
| 81 | break;
|
| 82 | default:
|
| 83 | goto out;
|
| 84 | }
|
| 85 |
|
| 86 | plsize = kblen - hdrsize;
|
| 87 | pl = (u8 *)kb + hdrsize;
|
| 88 |
|
| 89 | if (kbhdr)
|
| 90 | *kbhdr = hdr;
|
| 91 | if (kbhdrsize)
|
| 92 | *kbhdrsize = hdrsize;
|
| 93 | if (kbpl)
|
| 94 | *kbpl = pl;
|
| 95 | if (kbplsize)
|
| 96 | *kbplsize = plsize;
|
| 97 |
|
| 98 | rc = 0;
|
| 99 | out:
|
| 100 | return rc;
|
| 101 | }
|
| 102 |
|
| 103 | static int ep11_kb_decode(const u8 *kb, size_t kblen,
|
| 104 | struct ep11kblob_header **kbhdr, size_t *kbhdrsize,
|
| 105 | struct ep11keyblob **kbpl, size_t *kbplsize)
|
| 106 | {
|
| 107 | struct ep11kblob_header *tmph, *hdr = NULL;
|
| 108 | size_t hdrsize = 0, plsize = 0;
|
| 109 | struct ep11keyblob *pl = NULL;
|
| 110 | int rc = -EINVAL;
|
| 111 | u8 *tmpp;
|
| 112 |
|
| 113 | if (kblen < sizeof(struct ep11kblob_header))
|
| 114 | goto out;
|
| 115 | tmph = (struct ep11kblob_header *)kb;
|
| 116 |
|
| 117 | if (tmph->type != TOKTYPE_NON_CCA &&
|
| 118 | tmph->len > kblen)
|
| 119 | goto out;
|
| 120 |
|
| 121 | if (ep11_kb_split(kb, kblen, tmph->version,
|
| 122 | &hdr, &hdrsize, &tmpp, &plsize))
|
| 123 | goto out;
|
| 124 |
|
| 125 | if (plsize < sizeof(struct ep11keyblob))
|
| 126 | goto out;
|
| 127 |
|
| 128 | if (!is_ep11_keyblob(tmpp))
|
| 129 | goto out;
|
| 130 |
|
| 131 | pl = (struct ep11keyblob *)tmpp;
|
| 132 | plsize = hdr->len - hdrsize;
|
| 133 |
|
| 134 | if (kbhdr)
|
| 135 | *kbhdr = hdr;
|
| 136 | if (kbhdrsize)
|
| 137 | *kbhdrsize = hdrsize;
|
| 138 | if (kbpl)
|
| 139 | *kbpl = pl;
|
| 140 | if (kbplsize)
|
| 141 | *kbplsize = plsize;
|
| 142 |
|
| 143 | rc = 0;
|
| 144 | out:
|
| 145 | return rc;
|
| 146 | }
|
| 147 |
|
| 148 | |
| 149 | * For valid ep11 keyblobs, returns a reference to the wrappingkey verification
|
| 150 | * pattern. Otherwise NULL.
|
| 151 |
|
| 152 | const u8 *ep11_kb_wkvp(const u8 *keyblob, u32 keybloblen)
|
| 153 | {
|
| 154 | struct ep11keyblob *kb;
|
| 155 |
|
| 156 | if (ep11_kb_decode(keyblob, keybloblen, NULL, NULL, &kb, NULL))
|
| 157 | return NULL;
|
| 158 | return kb->wkvp;
|
| 159 | }
|
| 160 | EXPORT_SYMBOL(ep11_kb_wkvp);
|
| 161 |
|
| 162 | |
| 163 | * Simple check if the key blob is a valid EP11 AES key blob with header.
|
| 164 |
|
| 165 | int ep11_check_aes_key_with_hdr(debug_info_t *dbg, int dbflvl,
|
| 166 | const u8 *key, u32 keylen, int checkcpacfexp)
|
| 167 | {
|
| 168 | struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
|
| 169 | struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
|
| 170 |
|
| 171 | #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
|
| 172 |
|
| 173 | if (keylen < sizeof(*hdr) + sizeof(*kb)) {
|
| 174 | DBF("%s key check failed, keylen %u < %zu\n",
|
| 175 | __func__, keylen, sizeof(*hdr) + sizeof(*kb));
|
| 176 | return -EINVAL;
|
| 177 | }
|
| 178 |
|
| 179 | if (hdr->type != TOKTYPE_NON_CCA) {
|
| 180 | if (dbg)
|
| 181 | DBF("%s key check failed, type 0x%02x != 0x%02x\n",
|
| 182 | __func__, (int)hdr->type, TOKTYPE_NON_CCA);
|
| 183 | return -EINVAL;
|
| 184 | }
|
| 185 | if (hdr->hver != 0x00) {
|
| 186 | if (dbg)
|
| 187 | DBF("%s key check failed, header version 0x%02x != 0x00\n",
|
| 188 | __func__, (int)hdr->hver);
|
| 189 | return -EINVAL;
|
| 190 | }
|
| 191 | if (hdr->version != TOKVER_EP11_AES_WITH_HEADER) {
|
| 192 | if (dbg)
|
| 193 | DBF("%s key check failed, version 0x%02x != 0x%02x\n",
|
| 194 | __func__, (int)hdr->version, TOKVER_EP11_AES_WITH_HEADER);
|
| 195 | return -EINVAL;
|
| 196 | }
|
| 197 | if (hdr->len > keylen) {
|
| 198 | if (dbg)
|
| 199 | DBF("%s key check failed, header len %d keylen %u mismatch\n",
|
| 200 | __func__, (int)hdr->len, keylen);
|
| 201 | return -EINVAL;
|
| 202 | }
|
| 203 | if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
|
| 204 | if (dbg)
|
| 205 | DBF("%s key check failed, header len %d < %zu\n",
|
| 206 | __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
|
| 207 | return -EINVAL;
|
| 208 | }
|
| 209 |
|
| 210 | if (kb->version != EP11_STRUCT_MAGIC) {
|
| 211 | if (dbg)
|
| 212 | DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
|
| 213 | __func__, (int)kb->version, EP11_STRUCT_MAGIC);
|
| 214 | return -EINVAL;
|
| 215 | }
|
| 216 | if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
|
| 217 | if (dbg)
|
| 218 | DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
|
| 219 | __func__);
|
| 220 | return -EINVAL;
|
| 221 | }
|
| 222 |
|
| 223 | #undef DBF
|
| 224 |
|
| 225 | return 0;
|
| 226 | }
|
| 227 | EXPORT_SYMBOL(ep11_check_aes_key_with_hdr);
|
| 228 |
|
| 229 | |
| 230 | * Simple check if the key blob is a valid EP11 ECC key blob with header.
|
| 231 |
|
| 232 | int ep11_check_ecc_key_with_hdr(debug_info_t *dbg, int dbflvl,
|
| 233 | const u8 *key, u32 keylen, int checkcpacfexp)
|
| 234 | {
|
| 235 | struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
|
| 236 | struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
|
| 237 |
|
| 238 | #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
|
| 239 |
|
| 240 | if (keylen < sizeof(*hdr) + sizeof(*kb)) {
|
| 241 | DBF("%s key check failed, keylen %u < %zu\n",
|
| 242 | __func__, keylen, sizeof(*hdr) + sizeof(*kb));
|
| 243 | return -EINVAL;
|
| 244 | }
|
| 245 |
|
| 246 | if (hdr->type != TOKTYPE_NON_CCA) {
|
| 247 | if (dbg)
|
| 248 | DBF("%s key check failed, type 0x%02x != 0x%02x\n",
|
| 249 | __func__, (int)hdr->type, TOKTYPE_NON_CCA);
|
| 250 | return -EINVAL;
|
| 251 | }
|
| 252 | if (hdr->hver != 0x00) {
|
| 253 | if (dbg)
|
| 254 | DBF("%s key check failed, header version 0x%02x != 0x00\n",
|
| 255 | __func__, (int)hdr->hver);
|
| 256 | return -EINVAL;
|
| 257 | }
|
| 258 | if (hdr->version != TOKVER_EP11_ECC_WITH_HEADER) {
|
| 259 | if (dbg)
|
| 260 | DBF("%s key check failed, version 0x%02x != 0x%02x\n",
|
| 261 | __func__, (int)hdr->version, TOKVER_EP11_ECC_WITH_HEADER);
|
| 262 | return -EINVAL;
|
| 263 | }
|
| 264 | if (hdr->len > keylen) {
|
| 265 | if (dbg)
|
| 266 | DBF("%s key check failed, header len %d keylen %u mismatch\n",
|
| 267 | __func__, (int)hdr->len, keylen);
|
| 268 | return -EINVAL;
|
| 269 | }
|
| 270 | if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
|
| 271 | if (dbg)
|
| 272 | DBF("%s key check failed, header len %d < %zu\n",
|
| 273 | __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
|
| 274 | return -EINVAL;
|
| 275 | }
|
| 276 |
|
| 277 | if (kb->version != EP11_STRUCT_MAGIC) {
|
| 278 | if (dbg)
|
| 279 | DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
|
| 280 | __func__, (int)kb->version, EP11_STRUCT_MAGIC);
|
| 281 | return -EINVAL;
|
| 282 | }
|
| 283 | if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
|
| 284 | if (dbg)
|
| 285 | DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
|
| 286 | __func__);
|
| 287 | return -EINVAL;
|
| 288 | }
|
| 289 |
|
| 290 | #undef DBF
|
| 291 |
|
| 292 | return 0;
|
| 293 | }
|
| 294 | EXPORT_SYMBOL(ep11_check_ecc_key_with_hdr);
|
| 295 |
|
| 296 | |
| 297 | * Simple check if the key blob is a valid EP11 AES key blob with
|
| 298 | * the header in the session field (old style EP11 AES key).
|
| 299 |
|
| 300 | int ep11_check_aes_key(debug_info_t *dbg, int dbflvl,
|
| 301 | const u8 *key, u32 keylen, int checkcpacfexp)
|
| 302 | {
|
| 303 | struct ep11keyblob *kb = (struct ep11keyblob *)key;
|
| 304 |
|
| 305 | #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
|
| 306 |
|
| 307 | if (keylen < sizeof(*kb)) {
|
| 308 | DBF("%s key check failed, keylen %u < %zu\n",
|
| 309 | __func__, keylen, sizeof(*kb));
|
| 310 | return -EINVAL;
|
| 311 | }
|
| 312 |
|
| 313 | if (kb->head.type != TOKTYPE_NON_CCA) {
|
| 314 | if (dbg)
|
| 315 | DBF("%s key check failed, type 0x%02x != 0x%02x\n",
|
| 316 | __func__, (int)kb->head.type, TOKTYPE_NON_CCA);
|
| 317 | return -EINVAL;
|
| 318 | }
|
| 319 | if (kb->head.version != TOKVER_EP11_AES) {
|
| 320 | if (dbg)
|
| 321 | DBF("%s key check failed, version 0x%02x != 0x%02x\n",
|
| 322 | __func__, (int)kb->head.version, TOKVER_EP11_AES);
|
| 323 | return -EINVAL;
|
| 324 | }
|
| 325 | if (kb->head.len > keylen) {
|
| 326 | if (dbg)
|
| 327 | DBF("%s key check failed, header len %d keylen %u mismatch\n",
|
| 328 | __func__, (int)kb->head.len, keylen);
|
| 329 | return -EINVAL;
|
| 330 | }
|
| 331 | if (kb->head.len < sizeof(*kb)) {
|
| 332 | if (dbg)
|
| 333 | DBF("%s key check failed, header len %d < %zu\n",
|
| 334 | __func__, (int)kb->head.len, sizeof(*kb));
|
| 335 | return -EINVAL;
|
| 336 | }
|
| 337 |
|
| 338 | if (kb->version != EP11_STRUCT_MAGIC) {
|
| 339 | if (dbg)
|
| 340 | DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
|
| 341 | __func__, (int)kb->version, EP11_STRUCT_MAGIC);
|
| 342 | return -EINVAL;
|
| 343 | }
|
| 344 | if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
|
| 345 | if (dbg)
|
| 346 | DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
|
| 347 | __func__);
|
| 348 | return -EINVAL;
|
| 349 | }
|
| 350 |
|
| 351 | #undef DBF
|
| 352 |
|
| 353 | return 0;
|
| 354 | }
|
| 355 | EXPORT_SYMBOL(ep11_check_aes_key);
|
| 356 |
|
| 357 | |
| 358 | * Allocate and prepare ep11 cprb plus additional payload.
|
| 359 | * It is guaranteed that the memory is aligned to a 4 byte boundary.
|
| 360 | * Furthermore the memory allocation is rounded up to the next
|
| 361 | * multiple of 4 bytes (with taking the payload_len into account).
|
| 362 |
|
| 363 | static void *alloc_cprbmem(size_t payload_len, u32 xflags)
|
| 364 | {
|
| 365 | size_t memlen = ALIGN(sizeof(struct ep11_cprb) + payload_len, 4);
|
| 366 | struct ep11_cprb *cprb = NULL;
|
| 367 |
|
| 368 | if (xflags & ZCRYPT_XFLAG_NOMEMALLOC) {
|
| 369 | if (memlen <= CPRB_MEMPOOL_ITEM_SIZE)
|
| 370 | cprb = mempool_alloc_preallocated(cprb_mempool);
|
| 371 | } else {
|
| 372 | cprb = kmalloc(memlen, GFP_KERNEL);
|
| 373 | }
|
| 374 | if (!cprb)
|
| 375 | return NULL;
|
| 376 | memset(cprb, 0, memlen);
|
| 377 |
|
| 378 | cprb->cprb_len = sizeof(struct ep11_cprb);
|
| 379 | cprb->cprb_ver_id = 0x04;
|
| 380 | memcpy(cprb->func_id, "T4", 2);
|
| 381 | cprb->ret_code = 0xFFFFFFFF;
|
| 382 | cprb->payload_len = payload_len;
|
| 383 |
|
| 384 | return cprb;
|
| 385 | }
|
| 386 |
|
| 387 | |
| 388 | * Free ep11 cprb buffer space.
|
| 389 |
|
| 390 | static void free_cprbmem(void *mem, size_t payload_len, bool scrub, u32 xflags)
|
| 391 | {
|
| 392 | size_t memlen = ALIGN(sizeof(struct ep11_cprb) + payload_len, 4);
|
| 393 |
|
| 394 | if (mem && scrub)
|
| 395 | memzero_explicit(mem, memlen);
|
| 396 |
|
| 397 | if (xflags & ZCRYPT_XFLAG_NOMEMALLOC)
|
| 398 | mempool_free(mem, cprb_mempool);
|
| 399 | else
|
| 400 | kfree(mem);
|
| 401 | }
|
| 402 |
|
| 403 | |
| 404 | * Some helper functions related to ASN1 encoding.
|
| 405 | * Limited to length info <= 2 byte.
|
| 406 |
|
| 407 |
|
| 408 | #define ASN1TAGLEN(x) (2 + (x) + ((x) > 127 ? 1 : 0) + ((x) > 255 ? 1 : 0))
|
| 409 |
|
| 410 | static int asn1tag_write(u8 *ptr, u8 tag, const u8 *pvalue, u16 valuelen)
|
| 411 | {
|
| 412 | ptr[0] = tag;
|
| 413 | if (valuelen > 255) {
|
| 414 | ptr[1] = 0x82;
|
| 415 | *((u16 *)(ptr + 2)) = valuelen;
|
| 416 | memcpy(ptr + 4, pvalue, valuelen);
|
| 417 | return 4 + valuelen;
|
| 418 | }
|
| 419 | if (valuelen > 127) {
|
| 420 | ptr[1] = 0x81;
|
| 421 | ptr[2] = (u8)valuelen;
|
| 422 | memcpy(ptr + 3, pvalue, valuelen);
|
| 423 | return 3 + valuelen;
|
| 424 | }
|
| 425 | ptr[1] = (u8)valuelen;
|
| 426 | memcpy(ptr + 2, pvalue, valuelen);
|
| 427 | return 2 + valuelen;
|
| 428 | }
|
| 429 |
|
| 430 |
|
| 431 | struct pl_head {
|
| 432 | u8 tag;
|
| 433 | u8 lenfmt;
|
| 434 | u16 len;
|
| 435 | u8 func_tag;
|
| 436 | u8 func_len;
|
| 437 | u32 func;
|
| 438 | u8 dom_tag;
|
| 439 | u8 dom_len;
|
| 440 | u32 dom;
|
| 441 | } __packed;
|
| 442 |
|
| 443 |
|
| 444 | static inline void prep_head(struct pl_head *h,
|
| 445 | size_t pl_size, int api, int func)
|
| 446 | {
|
| 447 | h->tag = 0x30;
|
| 448 | h->lenfmt = 0x82;
|
| 449 | h->len = pl_size - 4;
|
| 450 | h->func_tag = 0x04;
|
| 451 | h->func_len = sizeof(u32);
|
| 452 | h->func = (api << 16) + func;
|
| 453 | h->dom_tag = 0x04;
|
| 454 | h->dom_len = sizeof(u32);
|
| 455 | }
|
| 456 |
|
| 457 |
|
| 458 | static inline void prep_urb(struct ep11_urb *u,
|
| 459 | struct ep11_target_dev *t, int nt,
|
| 460 | struct ep11_cprb *req, size_t req_len,
|
| 461 | struct ep11_cprb *rep, size_t rep_len)
|
| 462 | {
|
| 463 | memset(u, 0, sizeof(*u));
|
| 464 | u->targets = (u8 __user *)t;
|
| 465 | u->targets_num = nt;
|
| 466 | u->req = (u8 __user *)req;
|
| 467 | u->req_len = req_len;
|
| 468 | u->resp = (u8 __user *)rep;
|
| 469 | u->resp_len = rep_len;
|
| 470 | }
|
| 471 |
|
| 472 |
|
| 473 | static int check_reply_pl(const u8 *pl, const char *func)
|
| 474 | {
|
| 475 | int len;
|
| 476 | u32 ret;
|
| 477 |
|
| 478 |
|
| 479 | if (*pl++ != 0x30) {
|
| 480 | ZCRYPT_DBF_ERR("%s reply start tag mismatch\n", func);
|
| 481 | return -EIO;
|
| 482 | }
|
| 483 |
|
| 484 |
|
| 485 | if (*pl < 127) {
|
| 486 | len = *pl;
|
| 487 | pl++;
|
| 488 | } else if (*pl == 0x81) {
|
| 489 | pl++;
|
| 490 | len = *pl;
|
| 491 | pl++;
|
| 492 | } else if (*pl == 0x82) {
|
| 493 | pl++;
|
| 494 | len = *((u16 *)pl);
|
| 495 | pl += 2;
|
| 496 | } else {
|
| 497 | ZCRYPT_DBF_ERR("%s reply start tag lenfmt mismatch 0x%02hhx\n",
|
| 498 | func, *pl);
|
| 499 | return -EIO;
|
| 500 | }
|
| 501 |
|
| 502 |
|
| 503 | if (len < 3 * 6) {
|
| 504 | ZCRYPT_DBF_ERR("%s reply length %d too small\n", func, len);
|
| 505 | return -EIO;
|
| 506 | }
|
| 507 |
|
| 508 |
|
| 509 | if (pl[0] != 0x04 || pl[1] != 0x04) {
|
| 510 | ZCRYPT_DBF_ERR("%s function tag or length mismatch\n", func);
|
| 511 | return -EIO;
|
| 512 | }
|
| 513 | pl += 6;
|
| 514 |
|
| 515 |
|
| 516 | if (pl[0] != 0x04 || pl[1] != 0x04) {
|
| 517 | ZCRYPT_DBF_ERR("%s dom tag or length mismatch\n", func);
|
| 518 | return -EIO;
|
| 519 | }
|
| 520 | pl += 6;
|
| 521 |
|
| 522 |
|
| 523 | if (pl[0] != 0x04 || pl[1] != 0x04) {
|
| 524 | ZCRYPT_DBF_ERR("%s return value tag or length mismatch\n",
|
| 525 | func);
|
| 526 | return -EIO;
|
| 527 | }
|
| 528 | pl += 2;
|
| 529 | ret = *((u32 *)pl);
|
| 530 | if (ret != 0) {
|
| 531 | ZCRYPT_DBF_ERR("%s return value 0x%08x != 0\n", func, ret);
|
| 532 | return -EIO;
|
| 533 | }
|
| 534 |
|
| 535 | return 0;
|
| 536 | }
|
| 537 |
|
| 538 |
|
| 539 | static int check_reply_cprb(const struct ep11_cprb *rep, const char *func)
|
| 540 | {
|
| 541 |
|
| 542 | if (rep->ret_code) {
|
| 543 | ZCRYPT_DBF_ERR("%s ep11 reply ret_code=0x%08x\n", __func__,
|
| 544 | rep->ret_code);
|
| 545 | if (rep->ret_code == 0x000c0003)
|
| 546 | return -EBUSY;
|
| 547 | else
|
| 548 | return -EIO;
|
| 549 | }
|
| 550 |
|
| 551 | return 0;
|
| 552 | }
|
| 553 |
|
| 554 | |
| 555 | * Helper function which does an ep11 query with given query type.
|
| 556 |
|
| 557 | static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
|
| 558 | size_t buflen, u8 *buf, u32 xflags)
|
| 559 | {
|
| 560 | struct ep11_info_req_pl {
|
| 561 | struct pl_head head;
|
| 562 | u8 query_type_tag;
|
| 563 | u8 query_type_len;
|
| 564 | u32 query_type;
|
| 565 | u8 query_subtype_tag;
|
| 566 | u8 query_subtype_len;
|
| 567 | u32 query_subtype;
|
| 568 | } __packed * req_pl;
|
| 569 | struct ep11_info_rep_pl {
|
| 570 | struct pl_head head;
|
| 571 | u8 rc_tag;
|
| 572 | u8 rc_len;
|
| 573 | u32 rc;
|
| 574 | u8 data_tag;
|
| 575 | u8 data_lenfmt;
|
| 576 | u16 data_len;
|
| 577 | } __packed * rep_pl;
|
| 578 | struct ep11_cprb *req = NULL, *rep = NULL;
|
| 579 | struct ep11_target_dev target;
|
| 580 | struct ep11_urb urb;
|
| 581 | int api = EP11_API_V1, rc = -ENOMEM;
|
| 582 |
|
| 583 |
|
| 584 | req = alloc_cprbmem(sizeof(struct ep11_info_req_pl), xflags);
|
| 585 | if (!req)
|
| 586 | goto out;
|
| 587 | req_pl = (struct ep11_info_req_pl *)(((u8 *)req) + sizeof(*req));
|
| 588 | prep_head(&req_pl->head, sizeof(*req_pl), api, 38);
|
| 589 | req_pl->query_type_tag = 0x04;
|
| 590 | req_pl->query_type_len = sizeof(u32);
|
| 591 | req_pl->query_type = query_type;
|
| 592 | req_pl->query_subtype_tag = 0x04;
|
| 593 | req_pl->query_subtype_len = sizeof(u32);
|
| 594 |
|
| 595 |
|
| 596 | rep = alloc_cprbmem(sizeof(struct ep11_info_rep_pl) + buflen, xflags);
|
| 597 | if (!rep)
|
| 598 | goto out;
|
| 599 | rep_pl = (struct ep11_info_rep_pl *)(((u8 *)rep) + sizeof(*rep));
|
| 600 |
|
| 601 |
|
| 602 | target.ap_id = cardnr;
|
| 603 | target.dom_id = domain;
|
| 604 | prep_urb(&urb, &target, 1,
|
| 605 | req, sizeof(*req) + sizeof(*req_pl),
|
| 606 | rep, sizeof(*rep) + sizeof(*rep_pl) + buflen);
|
| 607 |
|
| 608 | rc = zcrypt_send_ep11_cprb(&urb, xflags);
|
| 609 | if (rc) {
|
| 610 | ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
|
| 611 | __func__, (int)cardnr, (int)domain, rc);
|
| 612 | goto out;
|
| 613 | }
|
| 614 |
|
| 615 |
|
| 616 | rc = check_reply_cprb(rep, __func__);
|
| 617 | if (rc)
|
| 618 | goto out;
|
| 619 |
|
| 620 |
|
| 621 | rc = check_reply_pl((u8 *)rep_pl, __func__);
|
| 622 | if (rc)
|
| 623 | goto out;
|
| 624 | if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
|
| 625 | ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__);
|
| 626 | rc = -EIO;
|
| 627 | goto out;
|
| 628 | }
|
| 629 | if (rep_pl->data_len > buflen) {
|
| 630 | ZCRYPT_DBF_ERR("%s mismatch between reply data len and buffer len\n",
|
| 631 | __func__);
|
| 632 | rc = -ENOSPC;
|
| 633 | goto out;
|
| 634 | }
|
| 635 |
|
| 636 | memcpy(buf, ((u8 *)rep_pl) + sizeof(*rep_pl), rep_pl->data_len);
|
| 637 |
|
| 638 | out:
|
| 639 | free_cprbmem(req, 0, false, xflags);
|
| 640 | free_cprbmem(rep, 0, false, xflags);
|
| 641 | return rc;
|
| 642 | }
|
| 643 |
|
| 644 | |
| 645 | * Provide information about an EP11 card.
|
| 646 |
|
| 647 | int ep11_get_card_info(u16 card, struct ep11_card_info *info, u32 xflags)
|
| 648 | {
|
| 649 | int rc;
|
| 650 | struct ep11_module_query_info {
|
| 651 | u32 API_ord_nr;
|
| 652 | u32 firmware_id;
|
| 653 | u8 FW_major_vers;
|
| 654 | u8 FW_minor_vers;
|
| 655 | u8 CSP_major_vers;
|
| 656 | u8 CSP_minor_vers;
|
| 657 | u8 fwid[32];
|
| 658 | u8 xcp_config_hash[32];
|
| 659 | u8 CSP_config_hash[32];
|
| 660 | u8 serial[16];
|
| 661 | u8 module_date_time[16];
|
| 662 | u64 op_mode;
|
| 663 | u32 PKCS11_flags;
|
| 664 | u32 ext_flags;
|
| 665 | u32 domains;
|
| 666 | u32 sym_state_bytes;
|
| 667 | u32 digest_state_bytes;
|
| 668 | u32 pin_blob_bytes;
|
| 669 | u32 SPKI_bytes;
|
| 670 | u32 priv_key_blob_bytes;
|
| 671 | u32 sym_blob_bytes;
|
| 672 | u32 max_payload_bytes;
|
| 673 | u32 CP_profile_bytes;
|
| 674 | u32 max_CP_index;
|
| 675 | } __packed * pmqi = NULL;
|
| 676 |
|
| 677 |
|
| 678 | pmqi = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
|
| 679 | mempool_alloc_preallocated(cprb_mempool) :
|
| 680 | mempool_alloc(cprb_mempool, GFP_KERNEL);
|
| 681 | if (!pmqi)
|
| 682 | return -ENOMEM;
|
| 683 | rc = ep11_query_info(card, AUTOSEL_DOM,
|
| 684 | 0x01 ,
|
| 685 | sizeof(*pmqi), (u8 *)pmqi, xflags);
|
| 686 | if (rc)
|
| 687 | goto out;
|
| 688 |
|
| 689 | memset(info, 0, sizeof(*info));
|
| 690 | info->API_ord_nr = pmqi->API_ord_nr;
|
| 691 | info->FW_version = (pmqi->FW_major_vers << 8) + pmqi->FW_minor_vers;
|
| 692 | memcpy(info->serial, pmqi->serial, sizeof(info->serial));
|
| 693 | info->op_mode = pmqi->op_mode;
|
| 694 |
|
| 695 | out:
|
| 696 | mempool_free(pmqi, cprb_mempool);
|
| 697 | return rc;
|
| 698 | }
|
| 699 | EXPORT_SYMBOL(ep11_get_card_info);
|
| 700 |
|
| 701 | |
| 702 | * Provide information about a domain within an EP11 card.
|
| 703 |
|
| 704 | int ep11_get_domain_info(u16 card, u16 domain,
|
| 705 | struct ep11_domain_info *info, u32 xflags)
|
| 706 | {
|
| 707 | int rc;
|
| 708 | struct ep11_domain_query_info {
|
| 709 | u32 dom_index;
|
| 710 | u8 cur_WK_VP[32];
|
| 711 | u8 new_WK_VP[32];
|
| 712 | u32 dom_flags;
|
| 713 | u64 op_mode;
|
| 714 | } __packed dom_query_info;
|
| 715 |
|
| 716 | rc = ep11_query_info(card, domain, 0x03 ,
|
| 717 | sizeof(dom_query_info), (u8 *)&dom_query_info,
|
| 718 | xflags);
|
| 719 | if (rc)
|
| 720 | goto out;
|
| 721 |
|
| 722 | memset(info, 0, sizeof(*info));
|
| 723 | info->cur_wk_state = '0';
|
| 724 | info->new_wk_state = '0';
|
| 725 | if (dom_query_info.dom_flags & 0x10 ) {
|
| 726 | if (dom_query_info.dom_flags & 0x02 ) {
|
| 727 | info->cur_wk_state = '1';
|
| 728 | memcpy(info->cur_wkvp, dom_query_info.cur_WK_VP, 32);
|
| 729 | }
|
| 730 | if (dom_query_info.dom_flags & 0x04 ||
|
| 731 | dom_query_info.dom_flags & 0x08 ) {
|
| 732 | info->new_wk_state =
|
| 733 | dom_query_info.dom_flags & 0x08 ? '2' : '1';
|
| 734 | memcpy(info->new_wkvp, dom_query_info.new_WK_VP, 32);
|
| 735 | }
|
| 736 | }
|
| 737 | info->op_mode = dom_query_info.op_mode;
|
| 738 |
|
| 739 | out:
|
| 740 | return rc;
|
| 741 | }
|
| 742 | EXPORT_SYMBOL(ep11_get_domain_info);
|
| 743 |
|
| 744 | |
| 745 | * Default EP11 AES key generate attributes, used when no keygenflags given:
|
| 746 | * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
|
| 747 |
|
| 748 | #define KEY_ATTR_DEFAULTS 0x00200c00
|
| 749 |
|
| 750 | static int _ep11_genaeskey(u16 card, u16 domain,
|
| 751 | u32 keybitsize, u32 keygenflags,
|
| 752 | u8 *keybuf, size_t *keybufsize, u32 xflags)
|
| 753 | {
|
| 754 | struct keygen_req_pl {
|
| 755 | struct pl_head head;
|
| 756 | u8 var_tag;
|
| 757 | u8 var_len;
|
| 758 | u32 var;
|
| 759 | u8 keybytes_tag;
|
| 760 | u8 keybytes_len;
|
| 761 | u32 keybytes;
|
| 762 | u8 mech_tag;
|
| 763 | u8 mech_len;
|
| 764 | u32 mech;
|
| 765 | u8 attr_tag;
|
| 766 | u8 attr_len;
|
| 767 | u32 attr_header;
|
| 768 | u32 attr_bool_mask;
|
| 769 | u32 attr_bool_bits;
|
| 770 | u32 attr_val_len_type;
|
| 771 | u32 attr_val_len_value;
|
| 772 |
|
| 773 | } __packed * req_pl;
|
| 774 | struct keygen_rep_pl {
|
| 775 | struct pl_head head;
|
| 776 | u8 rc_tag;
|
| 777 | u8 rc_len;
|
| 778 | u32 rc;
|
| 779 | u8 data_tag;
|
| 780 | u8 data_lenfmt;
|
| 781 | u16 data_len;
|
| 782 | u8 data[512];
|
| 783 | } __packed * rep_pl;
|
| 784 | struct ep11_cprb *req = NULL, *rep = NULL;
|
| 785 | size_t req_pl_size, pinblob_size = 0;
|
| 786 | struct ep11_target_dev target;
|
| 787 | struct ep11_urb urb;
|
| 788 | int api, rc = -ENOMEM;
|
| 789 | u8 *p;
|
| 790 |
|
| 791 | switch (keybitsize) {
|
| 792 | case 128:
|
| 793 | case 192:
|
| 794 | case 256:
|
| 795 | break;
|
| 796 | default:
|
| 797 | ZCRYPT_DBF_ERR("%s unknown/unsupported keybitsize %d\n",
|
| 798 | __func__, keybitsize);
|
| 799 | rc = -EINVAL;
|
| 800 | goto out;
|
| 801 | }
|
| 802 |
|
| 803 |
|
| 804 | api = (!keygenflags || keygenflags & 0x00200000) ?
|
| 805 | EP11_API_V4 : EP11_API_V1;
|
| 806 | if (ap_is_se_guest()) {
|
| 807 | |
| 808 | * genkey within SE environment requires API ordinal 6
|
| 809 | * with empty pinblob
|
| 810 |
|
| 811 | api = EP11_API_V6;
|
| 812 | pinblob_size = EP11_PINBLOB_V1_BYTES;
|
| 813 | }
|
| 814 | req_pl_size = sizeof(struct keygen_req_pl) + ASN1TAGLEN(pinblob_size);
|
| 815 | req = alloc_cprbmem(req_pl_size, xflags);
|
| 816 | if (!req)
|
| 817 | goto out;
|
| 818 | req_pl = (struct keygen_req_pl *)(((u8 *)req) + sizeof(*req));
|
| 819 | prep_head(&req_pl->head, req_pl_size, api, 21);
|
| 820 | req_pl->var_tag = 0x04;
|
| 821 | req_pl->var_len = sizeof(u32);
|
| 822 | req_pl->keybytes_tag = 0x04;
|
| 823 | req_pl->keybytes_len = sizeof(u32);
|
| 824 | req_pl->keybytes = keybitsize / 8;
|
| 825 | req_pl->mech_tag = 0x04;
|
| 826 | req_pl->mech_len = sizeof(u32);
|
| 827 | req_pl->mech = 0x00001080;
|
| 828 | req_pl->attr_tag = 0x04;
|
| 829 | req_pl->attr_len = 5 * sizeof(u32);
|
| 830 | req_pl->attr_header = 0x10010000;
|
| 831 | req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
|
| 832 | req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
|
| 833 | req_pl->attr_val_len_type = 0x00000161;
|
| 834 | req_pl->attr_val_len_value = keybitsize / 8;
|
| 835 | p = ((u8 *)req_pl) + sizeof(*req_pl);
|
| 836 |
|
| 837 | *p++ = 0x04;
|
| 838 | *p++ = pinblob_size;
|
| 839 |
|
| 840 |
|
| 841 | rep = alloc_cprbmem(sizeof(struct keygen_rep_pl), xflags);
|
| 842 | if (!rep)
|
| 843 | goto out;
|
| 844 | rep_pl = (struct keygen_rep_pl *)(((u8 *)rep) + sizeof(*rep));
|
| 845 |
|
| 846 |
|
| 847 | target.ap_id = card;
|
| 848 | target.dom_id = domain;
|
| 849 | prep_urb(&urb, &target, 1,
|
| 850 | req, sizeof(*req) + req_pl_size,
|
| 851 | rep, sizeof(*rep) + sizeof(*rep_pl));
|
| 852 |
|
| 853 | rc = zcrypt_send_ep11_cprb(&urb, xflags);
|
| 854 | if (rc) {
|
| 855 | ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
|
| 856 | __func__, (int)card, (int)domain, rc);
|
| 857 | goto out;
|
| 858 | }
|
| 859 |
|
| 860 |
|
| 861 | rc = check_reply_cprb(rep, __func__);
|
| 862 | if (rc)
|
| 863 | goto out;
|
| 864 |
|
| 865 |
|
| 866 | rc = check_reply_pl((u8 *)rep_pl, __func__);
|
| 867 | if (rc)
|
| 868 | goto out;
|
| 869 | if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
|
| 870 | ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__);
|
| 871 | rc = -EIO;
|
| 872 | goto out;
|
| 873 | }
|
| 874 | if (rep_pl->data_len > *keybufsize) {
|
| 875 | ZCRYPT_DBF_ERR("%s mismatch reply data len / key buffer len\n",
|
| 876 | __func__);
|
| 877 | rc = -ENOSPC;
|
| 878 | goto out;
|
| 879 | }
|
| 880 |
|
| 881 |
|
| 882 | memcpy(keybuf, rep_pl->data, rep_pl->data_len);
|
| 883 | *keybufsize = rep_pl->data_len;
|
| 884 |
|
| 885 | out:
|
| 886 | free_cprbmem(req, 0, false, xflags);
|
| 887 | free_cprbmem(rep, sizeof(struct keygen_rep_pl), true, xflags);
|
| 888 | return rc;
|
| 889 | }
|
| 890 |
|
| 891 | int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
|
| 892 | u8 *keybuf, u32 *keybufsize, u32 keybufver, u32 xflags)
|
| 893 | {
|
| 894 | struct ep11kblob_header *hdr;
|
| 895 | size_t hdr_size, pl_size;
|
| 896 | u8 *pl;
|
| 897 | int rc;
|
| 898 |
|
| 899 | switch (keybufver) {
|
| 900 | case TOKVER_EP11_AES:
|
| 901 | case TOKVER_EP11_AES_WITH_HEADER:
|
| 902 | break;
|
| 903 | default:
|
| 904 | return -EINVAL;
|
| 905 | }
|
| 906 |
|
| 907 | rc = ep11_kb_split(keybuf, *keybufsize, keybufver,
|
| 908 | &hdr, &hdr_size, &pl, &pl_size);
|
| 909 | if (rc)
|
| 910 | return rc;
|
| 911 |
|
| 912 | rc = _ep11_genaeskey(card, domain, keybitsize, keygenflags,
|
| 913 | pl, &pl_size, xflags);
|
| 914 | if (rc)
|
| 915 | return rc;
|
| 916 |
|
| 917 | *keybufsize = hdr_size + pl_size;
|
| 918 |
|
| 919 |
|
| 920 | hdr->type = TOKTYPE_NON_CCA;
|
| 921 | hdr->len = *keybufsize;
|
| 922 | hdr->version = keybufver;
|
| 923 | hdr->bitlen = keybitsize;
|
| 924 |
|
| 925 | return 0;
|
| 926 | }
|
| 927 | EXPORT_SYMBOL(ep11_genaeskey);
|
| 928 |
|
| 929 | static int ep11_cryptsingle(u16 card, u16 domain,
|
| 930 | u16 mode, u32 mech, const u8 *iv,
|
| 931 | const u8 *key, size_t keysize,
|
| 932 | const u8 *inbuf, size_t inbufsize,
|
| 933 | u8 *outbuf, size_t *outbufsize,
|
| 934 | u32 xflags)
|
| 935 | {
|
| 936 | struct crypt_req_pl {
|
| 937 | struct pl_head head;
|
| 938 | u8 var_tag;
|
| 939 | u8 var_len;
|
| 940 | u32 var;
|
| 941 | u8 mech_tag;
|
| 942 | u8 mech_len;
|
| 943 | u32 mech;
|
| 944 | |
| 945 | * maybe followed by iv data
|
| 946 | * followed by key tag + key blob
|
| 947 | * followed by plaintext tag + plaintext
|
| 948 |
|
| 949 | } __packed * req_pl;
|
| 950 | struct crypt_rep_pl {
|
| 951 | struct pl_head head;
|
| 952 | u8 rc_tag;
|
| 953 | u8 rc_len;
|
| 954 | u32 rc;
|
| 955 | u8 data_tag;
|
| 956 | u8 data_lenfmt;
|
| 957 |
|
| 958 | } __packed * rep_pl;
|
| 959 | struct ep11_cprb *req = NULL, *rep = NULL;
|
| 960 | struct ep11_target_dev target;
|
| 961 | struct ep11_urb urb;
|
| 962 | size_t req_pl_size, rep_pl_size = 0;
|
| 963 | int n, api = EP11_API_V1, rc = -ENOMEM;
|
| 964 | u8 *p;
|
| 965 |
|
| 966 |
|
| 967 | if (keysize > 0xFFFF || inbufsize > 0xFFFF)
|
| 968 | return -EINVAL;
|
| 969 |
|
| 970 |
|
| 971 | req_pl_size = sizeof(struct crypt_req_pl) + (iv ? 16 : 0)
|
| 972 | + ASN1TAGLEN(keysize) + ASN1TAGLEN(inbufsize);
|
| 973 | req = alloc_cprbmem(req_pl_size, xflags);
|
| 974 | if (!req)
|
| 975 | goto out;
|
| 976 | req_pl = (struct crypt_req_pl *)(((u8 *)req) + sizeof(*req));
|
| 977 | prep_head(&req_pl->head, req_pl_size, api, (mode ? 20 : 19));
|
| 978 | req_pl->var_tag = 0x04;
|
| 979 | req_pl->var_len = sizeof(u32);
|
| 980 |
|
| 981 | req_pl->mech_tag = 0x04;
|
| 982 | req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
|
| 983 | req_pl->mech = (mech ? mech : 0x00001085);
|
| 984 | p = ((u8 *)req_pl) + sizeof(*req_pl);
|
| 985 | if (iv) {
|
| 986 | memcpy(p, iv, 16);
|
| 987 | p += 16;
|
| 988 | }
|
| 989 |
|
| 990 | p += asn1tag_write(p, 0x04, key, keysize);
|
| 991 | p += asn1tag_write(p, 0x04, inbuf, inbufsize);
|
| 992 |
|
| 993 |
|
| 994 | rep_pl_size = sizeof(struct crypt_rep_pl) + ASN1TAGLEN(inbufsize + 32);
|
| 995 | rep = alloc_cprbmem(rep_pl_size, xflags);
|
| 996 | if (!rep)
|
| 997 | goto out;
|
| 998 | rep_pl = (struct crypt_rep_pl *)(((u8 *)rep) + sizeof(*rep));
|
| 999 |
|
| 1000 |
|
| 1001 | target.ap_id = card;
|
| 1002 | target.dom_id = domain;
|
| 1003 | prep_urb(&urb, &target, 1,
|
| 1004 | req, sizeof(*req) + req_pl_size,
|
| 1005 | rep, sizeof(*rep) + rep_pl_size);
|
| 1006 |
|
| 1007 | rc = zcrypt_send_ep11_cprb(&urb, xflags);
|
| 1008 | if (rc) {
|
| 1009 | ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
|
| 1010 | __func__, (int)card, (int)domain, rc);
|
| 1011 | goto out;
|
| 1012 | }
|
| 1013 |
|
| 1014 |
|
| 1015 | rc = check_reply_cprb(rep, __func__);
|
| 1016 | if (rc)
|
| 1017 | goto out;
|
| 1018 |
|
| 1019 |
|
| 1020 | rc = check_reply_pl((u8 *)rep_pl, __func__);
|
| 1021 | if (rc)
|
| 1022 | goto out;
|
| 1023 | if (rep_pl->data_tag != 0x04) {
|
| 1024 | ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__);
|
| 1025 | rc = -EIO;
|
| 1026 | goto out;
|
| 1027 | }
|
| 1028 | p = ((u8 *)rep_pl) + sizeof(*rep_pl);
|
| 1029 | if (rep_pl->data_lenfmt <= 127) {
|
| 1030 | n = rep_pl->data_lenfmt;
|
| 1031 | } else if (rep_pl->data_lenfmt == 0x81) {
|
| 1032 | n = *p++;
|
| 1033 | } else if (rep_pl->data_lenfmt == 0x82) {
|
| 1034 | n = *((u16 *)p);
|
| 1035 | p += 2;
|
| 1036 | } else {
|
| 1037 | ZCRYPT_DBF_ERR("%s unknown reply data length format 0x%02hhx\n",
|
| 1038 | __func__, rep_pl->data_lenfmt);
|
| 1039 | rc = -EIO;
|
| 1040 | goto out;
|
| 1041 | }
|
| 1042 | if (n > *outbufsize) {
|
| 1043 | ZCRYPT_DBF_ERR("%s mismatch reply data len %d / output buffer %zu\n",
|
| 1044 | __func__, n, *outbufsize);
|
| 1045 | rc = -ENOSPC;
|
| 1046 | goto out;
|
| 1047 | }
|
| 1048 |
|
| 1049 | memcpy(outbuf, p, n);
|
| 1050 | *outbufsize = n;
|
| 1051 |
|
| 1052 | out:
|
| 1053 | free_cprbmem(req, req_pl_size, true, xflags);
|
| 1054 | free_cprbmem(rep, rep_pl_size, true, xflags);
|
| 1055 | return rc;
|
| 1056 | }
|
| 1057 |
|
| 1058 | static int _ep11_unwrapkey(u16 card, u16 domain,
|
| 1059 | const u8 *kek, size_t keksize,
|
| 1060 | const u8 *enckey, size_t enckeysize,
|
| 1061 | u32 mech, const u8 *iv,
|
| 1062 | u32 keybitsize, u32 keygenflags,
|
| 1063 | u8 *keybuf, size_t *keybufsize, u32 xflags)
|
| 1064 | {
|
| 1065 | struct uw_req_pl {
|
| 1066 | struct pl_head head;
|
| 1067 | u8 attr_tag;
|
| 1068 | u8 attr_len;
|
| 1069 | u32 attr_header;
|
| 1070 | u32 attr_bool_mask;
|
| 1071 | u32 attr_bool_bits;
|
| 1072 | u32 attr_key_type;
|
| 1073 | u32 attr_key_type_value;
|
| 1074 | u32 attr_val_len;
|
| 1075 | u32 attr_val_len_value;
|
| 1076 | u8 mech_tag;
|
| 1077 | u8 mech_len;
|
| 1078 | u32 mech;
|
| 1079 | |
| 1080 | * maybe followed by iv data
|
| 1081 | * followed by kek tag + kek blob
|
| 1082 | * followed by empty mac tag
|
| 1083 | * followed by empty pin tag or empty pinblob tag
|
| 1084 | * followed by encryted key tag + bytes
|
| 1085 |
|
| 1086 | } __packed * req_pl;
|
| 1087 | struct uw_rep_pl {
|
| 1088 | struct pl_head head;
|
| 1089 | u8 rc_tag;
|
| 1090 | u8 rc_len;
|
| 1091 | u32 rc;
|
| 1092 | u8 data_tag;
|
| 1093 | u8 data_lenfmt;
|
| 1094 | u16 data_len;
|
| 1095 | u8 data[512];
|
| 1096 | } __packed * rep_pl;
|
| 1097 | struct ep11_cprb *req = NULL, *rep = NULL;
|
| 1098 | size_t req_pl_size, pinblob_size = 0;
|
| 1099 | struct ep11_target_dev target;
|
| 1100 | struct ep11_urb urb;
|
| 1101 | int api, rc = -ENOMEM;
|
| 1102 | u8 *p;
|
| 1103 |
|
| 1104 |
|
| 1105 | api = (!keygenflags || keygenflags & 0x00200000) ?
|
| 1106 | EP11_API_V4 : EP11_API_V1;
|
| 1107 | if (ap_is_se_guest()) {
|
| 1108 | |
| 1109 | * unwrap within SE environment requires API ordinal 6
|
| 1110 | * with empty pinblob
|
| 1111 |
|
| 1112 | api = EP11_API_V6;
|
| 1113 | pinblob_size = EP11_PINBLOB_V1_BYTES;
|
| 1114 | }
|
| 1115 | req_pl_size = sizeof(struct uw_req_pl) + (iv ? 16 : 0)
|
| 1116 | + ASN1TAGLEN(keksize) + ASN1TAGLEN(0)
|
| 1117 | + ASN1TAGLEN(pinblob_size) + ASN1TAGLEN(enckeysize);
|
| 1118 | req = alloc_cprbmem(req_pl_size, xflags);
|
| 1119 | if (!req)
|
| 1120 | goto out;
|
| 1121 | req_pl = (struct uw_req_pl *)(((u8 *)req) + sizeof(*req));
|
| 1122 | prep_head(&req_pl->head, req_pl_size, api, 34);
|
| 1123 | req_pl->attr_tag = 0x04;
|
| 1124 | req_pl->attr_len = 7 * sizeof(u32);
|
| 1125 | req_pl->attr_header = 0x10020000;
|
| 1126 | req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
|
| 1127 | req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
|
| 1128 | req_pl->attr_key_type = 0x00000100;
|
| 1129 | req_pl->attr_key_type_value = 0x0000001f;
|
| 1130 | req_pl->attr_val_len = 0x00000161;
|
| 1131 | req_pl->attr_val_len_value = keybitsize / 8;
|
| 1132 |
|
| 1133 | req_pl->mech_tag = 0x04;
|
| 1134 | req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
|
| 1135 | req_pl->mech = (mech ? mech : 0x00001085);
|
| 1136 | p = ((u8 *)req_pl) + sizeof(*req_pl);
|
| 1137 | if (iv) {
|
| 1138 | memcpy(p, iv, 16);
|
| 1139 | p += 16;
|
| 1140 | }
|
| 1141 |
|
| 1142 | p += asn1tag_write(p, 0x04, kek, keksize);
|
| 1143 |
|
| 1144 | *p++ = 0x04;
|
| 1145 | *p++ = 0;
|
| 1146 |
|
| 1147 | *p++ = 0x04;
|
| 1148 | *p++ = pinblob_size;
|
| 1149 | p += pinblob_size;
|
| 1150 |
|
| 1151 | p += asn1tag_write(p, 0x04, enckey, enckeysize);
|
| 1152 |
|
| 1153 |
|
| 1154 | rep = alloc_cprbmem(sizeof(struct uw_rep_pl), xflags);
|
| 1155 | if (!rep)
|
| 1156 | goto out;
|
| 1157 | rep_pl = (struct uw_rep_pl *)(((u8 *)rep) + sizeof(*rep));
|
| 1158 |
|
| 1159 |
|
| 1160 | target.ap_id = card;
|
| 1161 | target.dom_id = domain;
|
| 1162 | prep_urb(&urb, &target, 1,
|
| 1163 | req, sizeof(*req) + req_pl_size,
|
| 1164 | rep, sizeof(*rep) + sizeof(*rep_pl));
|
| 1165 |
|
| 1166 | rc = zcrypt_send_ep11_cprb(&urb, xflags);
|
| 1167 | if (rc) {
|
| 1168 | ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
|
| 1169 | __func__, (int)card, (int)domain, rc);
|
| 1170 | goto out;
|
| 1171 | }
|
| 1172 |
|
| 1173 |
|
| 1174 | rc = check_reply_cprb(rep, __func__);
|
| 1175 | if (rc)
|
| 1176 | goto out;
|
| 1177 |
|
| 1178 |
|
| 1179 | rc = check_reply_pl((u8 *)rep_pl, __func__);
|
| 1180 | if (rc)
|
| 1181 | goto out;
|
| 1182 | if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
|
| 1183 | ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__);
|
| 1184 | rc = -EIO;
|
| 1185 | goto out;
|
| 1186 | }
|
| 1187 | if (rep_pl->data_len > *keybufsize) {
|
| 1188 | ZCRYPT_DBF_ERR("%s mismatch reply data len / key buffer len\n",
|
| 1189 | __func__);
|
| 1190 | rc = -ENOSPC;
|
| 1191 | goto out;
|
| 1192 | }
|
| 1193 |
|
| 1194 |
|
| 1195 | memcpy(keybuf, rep_pl->data, rep_pl->data_len);
|
| 1196 | *keybufsize = rep_pl->data_len;
|
| 1197 |
|
| 1198 | out:
|
| 1199 | free_cprbmem(req, req_pl_size, true, xflags);
|
| 1200 | free_cprbmem(rep, sizeof(struct uw_rep_pl), true, xflags);
|
| 1201 | return rc;
|
| 1202 | }
|
| 1203 |
|
| 1204 | static int ep11_unwrapkey(u16 card, u16 domain,
|
| 1205 | const u8 *kek, size_t keksize,
|
| 1206 | const u8 *enckey, size_t enckeysize,
|
| 1207 | u32 mech, const u8 *iv,
|
| 1208 | u32 keybitsize, u32 keygenflags,
|
| 1209 | u8 *keybuf, u32 *keybufsize,
|
| 1210 | u8 keybufver, u32 xflags)
|
| 1211 | {
|
| 1212 | struct ep11kblob_header *hdr;
|
| 1213 | size_t hdr_size, pl_size;
|
| 1214 | u8 *pl;
|
| 1215 | int rc;
|
| 1216 |
|
| 1217 | rc = ep11_kb_split(keybuf, *keybufsize, keybufver,
|
| 1218 | &hdr, &hdr_size, &pl, &pl_size);
|
| 1219 | if (rc)
|
| 1220 | return rc;
|
| 1221 |
|
| 1222 | rc = _ep11_unwrapkey(card, domain, kek, keksize, enckey, enckeysize,
|
| 1223 | mech, iv, keybitsize, keygenflags,
|
| 1224 | pl, &pl_size, xflags);
|
| 1225 | if (rc)
|
| 1226 | return rc;
|
| 1227 |
|
| 1228 | *keybufsize = hdr_size + pl_size;
|
| 1229 |
|
| 1230 |
|
| 1231 | hdr = (struct ep11kblob_header *)keybuf;
|
| 1232 | hdr->type = TOKTYPE_NON_CCA;
|
| 1233 | hdr->len = *keybufsize;
|
| 1234 | hdr->version = keybufver;
|
| 1235 | hdr->bitlen = keybitsize;
|
| 1236 |
|
| 1237 | return 0;
|
| 1238 | }
|
| 1239 |
|
| 1240 | static int _ep11_wrapkey(u16 card, u16 domain,
|
| 1241 | const u8 *key, size_t keysize,
|
| 1242 | u32 mech, const u8 *iv,
|
| 1243 | u8 *databuf, size_t *datasize, u32 xflags)
|
| 1244 | {
|
| 1245 | struct wk_req_pl {
|
| 1246 | struct pl_head head;
|
| 1247 | u8 var_tag;
|
| 1248 | u8 var_len;
|
| 1249 | u32 var;
|
| 1250 | u8 mech_tag;
|
| 1251 | u8 mech_len;
|
| 1252 | u32 mech;
|
| 1253 | |
| 1254 | * followed by iv data
|
| 1255 | * followed by key tag + key blob
|
| 1256 | * followed by dummy kek param
|
| 1257 | * followed by dummy mac param
|
| 1258 |
|
| 1259 | } __packed * req_pl;
|
| 1260 | struct wk_rep_pl {
|
| 1261 | struct pl_head head;
|
| 1262 | u8 rc_tag;
|
| 1263 | u8 rc_len;
|
| 1264 | u32 rc;
|
| 1265 | u8 data_tag;
|
| 1266 | u8 data_lenfmt;
|
| 1267 | u16 data_len;
|
| 1268 | u8 data[1024];
|
| 1269 | } __packed * rep_pl;
|
| 1270 | struct ep11_cprb *req = NULL, *rep = NULL;
|
| 1271 | struct ep11_target_dev target;
|
| 1272 | struct ep11_urb urb;
|
| 1273 | size_t req_pl_size;
|
| 1274 | int api, rc = -ENOMEM;
|
| 1275 | u8 *p;
|
| 1276 |
|
| 1277 |
|
| 1278 | req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
|
| 1279 | + ASN1TAGLEN(keysize) + 4;
|
| 1280 | req = alloc_cprbmem(req_pl_size, xflags);
|
| 1281 | if (!req)
|
| 1282 | goto out;
|
| 1283 | if (!mech || mech == 0x80060001)
|
| 1284 | req->flags |= 0x20;
|
| 1285 | req_pl = (struct wk_req_pl *)(((u8 *)req) + sizeof(*req));
|
| 1286 | api = (!mech || mech == 0x80060001) ?
|
| 1287 | EP11_API_V4 : EP11_API_V1;
|
| 1288 | prep_head(&req_pl->head, req_pl_size, api, 33);
|
| 1289 | req_pl->var_tag = 0x04;
|
| 1290 | req_pl->var_len = sizeof(u32);
|
| 1291 |
|
| 1292 | req_pl->mech_tag = 0x04;
|
| 1293 | req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
|
| 1294 | req_pl->mech = (mech ? mech : 0x80060001);
|
| 1295 | p = ((u8 *)req_pl) + sizeof(*req_pl);
|
| 1296 | if (iv) {
|
| 1297 | memcpy(p, iv, 16);
|
| 1298 | p += 16;
|
| 1299 | }
|
| 1300 |
|
| 1301 | p += asn1tag_write(p, 0x04, key, keysize);
|
| 1302 |
|
| 1303 | *p++ = 0x04;
|
| 1304 | *p++ = 0;
|
| 1305 |
|
| 1306 | *p++ = 0x04;
|
| 1307 | *p++ = 0;
|
| 1308 |
|
| 1309 |
|
| 1310 | rep = alloc_cprbmem(sizeof(struct wk_rep_pl), xflags);
|
| 1311 | if (!rep)
|
| 1312 | goto out;
|
| 1313 | rep_pl = (struct wk_rep_pl *)(((u8 *)rep) + sizeof(*rep));
|
| 1314 |
|
| 1315 |
|
| 1316 | target.ap_id = card;
|
| 1317 | target.dom_id = domain;
|
| 1318 | prep_urb(&urb, &target, 1,
|
| 1319 | req, sizeof(*req) + req_pl_size,
|
| 1320 | rep, sizeof(*rep) + sizeof(*rep_pl));
|
| 1321 |
|
| 1322 | rc = zcrypt_send_ep11_cprb(&urb, xflags);
|
| 1323 | if (rc) {
|
| 1324 | ZCRYPT_DBF_ERR("%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
|
| 1325 | __func__, (int)card, (int)domain, rc);
|
| 1326 | goto out;
|
| 1327 | }
|
| 1328 |
|
| 1329 |
|
| 1330 | rc = check_reply_cprb(rep, __func__);
|
| 1331 | if (rc)
|
| 1332 | goto out;
|
| 1333 |
|
| 1334 |
|
| 1335 | rc = check_reply_pl((u8 *)rep_pl, __func__);
|
| 1336 | if (rc)
|
| 1337 | goto out;
|
| 1338 | if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
|
| 1339 | ZCRYPT_DBF_ERR("%s unknown reply data format\n", __func__);
|
| 1340 | rc = -EIO;
|
| 1341 | goto out;
|
| 1342 | }
|
| 1343 | if (rep_pl->data_len > *datasize) {
|
| 1344 | ZCRYPT_DBF_ERR("%s mismatch reply data len / data buffer len\n",
|
| 1345 | __func__);
|
| 1346 | rc = -ENOSPC;
|
| 1347 | goto out;
|
| 1348 | }
|
| 1349 |
|
| 1350 |
|
| 1351 | memcpy(databuf, rep_pl->data, rep_pl->data_len);
|
| 1352 | *datasize = rep_pl->data_len;
|
| 1353 |
|
| 1354 | out:
|
| 1355 | free_cprbmem(req, req_pl_size, true, xflags);
|
| 1356 | free_cprbmem(rep, sizeof(struct wk_rep_pl), true, xflags);
|
| 1357 | return rc;
|
| 1358 | }
|
| 1359 |
|
| 1360 | int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
|
| 1361 | const u8 *clrkey, u8 *keybuf, u32 *keybufsize,
|
| 1362 | u32 keytype, u32 xflags)
|
| 1363 | {
|
| 1364 | int rc;
|
| 1365 | void *mem;
|
| 1366 | u8 encbuf[64], *kek;
|
| 1367 | size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
|
| 1368 |
|
| 1369 | if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256) {
|
| 1370 | clrkeylen = keybitsize / 8;
|
| 1371 | } else {
|
| 1372 | ZCRYPT_DBF_ERR("%s unknown/unsupported keybitsize %d\n",
|
| 1373 | __func__, keybitsize);
|
| 1374 | return -EINVAL;
|
| 1375 | }
|
| 1376 |
|
| 1377 | |
| 1378 | * Allocate space for the temp kek.
|
| 1379 | * Also we only need up to MAXEP11AESKEYBLOBSIZE bytes for this
|
| 1380 | * we use the already existing cprb mempool to solve this
|
| 1381 | * short term memory requirement.
|
| 1382 |
|
| 1383 | mem = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
|
| 1384 | mempool_alloc_preallocated(cprb_mempool) :
|
| 1385 | mempool_alloc(cprb_mempool, GFP_KERNEL);
|
| 1386 | if (!mem)
|
| 1387 | return -ENOMEM;
|
| 1388 | kek = (u8 *)mem;
|
| 1389 | keklen = MAXEP11AESKEYBLOBSIZE;
|
| 1390 |
|
| 1391 |
|
| 1392 | rc = _ep11_genaeskey(card, domain, 256,
|
| 1393 | 0x00006c00,
|
| 1394 | kek, &keklen, xflags);
|
| 1395 | if (rc) {
|
| 1396 | ZCRYPT_DBF_ERR("%s generate kek key failed, rc=%d\n",
|
| 1397 | __func__, rc);
|
| 1398 | goto out;
|
| 1399 | }
|
| 1400 |
|
| 1401 |
|
| 1402 | rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
|
| 1403 | clrkey, clrkeylen, encbuf, &encbuflen, xflags);
|
| 1404 | if (rc) {
|
| 1405 | ZCRYPT_DBF_ERR("%s encrypting key value with kek key failed, rc=%d\n",
|
| 1406 | __func__, rc);
|
| 1407 | goto out;
|
| 1408 | }
|
| 1409 |
|
| 1410 |
|
| 1411 | rc = ep11_unwrapkey(card, domain, kek, keklen,
|
| 1412 | encbuf, encbuflen, 0, def_iv,
|
| 1413 | keybitsize, keygenflags,
|
| 1414 | keybuf, keybufsize,
|
| 1415 | keytype, xflags);
|
| 1416 | if (rc) {
|
| 1417 | ZCRYPT_DBF_ERR("%s importing key value as new key failed, rc=%d\n",
|
| 1418 | __func__, rc);
|
| 1419 | goto out;
|
| 1420 | }
|
| 1421 |
|
| 1422 | out:
|
| 1423 | mempool_free(mem, cprb_mempool);
|
| 1424 | return rc;
|
| 1425 | }
|
| 1426 | EXPORT_SYMBOL(ep11_clr2keyblob);
|
| 1427 |
|
| 1428 | int ep11_kblob2protkey(u16 card, u16 dom,
|
| 1429 | const u8 *keyblob, u32 keybloblen,
|
| 1430 | u8 *protkey, u32 *protkeylen, u32 *protkeytype,
|
| 1431 | u32 xflags)
|
| 1432 | {
|
| 1433 | struct ep11kblob_header *hdr;
|
| 1434 | struct ep11keyblob *key;
|
| 1435 | size_t wkbuflen, keylen;
|
| 1436 | struct wk_info {
|
| 1437 | u16 version;
|
| 1438 | u8 res1[16];
|
| 1439 | u32 pkeytype;
|
| 1440 | u32 pkeybitsize;
|
| 1441 | u64 pkeysize;
|
| 1442 | u8 res2[8];
|
| 1443 | u8 pkey[];
|
| 1444 | } __packed * wki;
|
| 1445 | u8 *wkbuf = NULL;
|
| 1446 | int rc = -EIO;
|
| 1447 |
|
| 1448 | if (ep11_kb_decode((u8 *)keyblob, keybloblen, &hdr, NULL, &key, &keylen))
|
| 1449 | return -EINVAL;
|
| 1450 |
|
| 1451 | if (hdr->version == TOKVER_EP11_AES) {
|
| 1452 |
|
| 1453 | memset(hdr, 0, sizeof(*hdr));
|
| 1454 | }
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 | wkbuflen = (keylen + AES_BLOCK_SIZE) & (~(AES_BLOCK_SIZE - 1));
|
| 1459 | if (wkbuflen > CPRB_MEMPOOL_ITEM_SIZE) {
|
| 1460 |
|
| 1461 | rc = -ENOMEM;
|
| 1462 | ZCRYPT_DBF_WARN("%s wkbuflen %d > cprb mempool item size %d, rc=%d\n",
|
| 1463 | __func__, (int)wkbuflen, CPRB_MEMPOOL_ITEM_SIZE, rc);
|
| 1464 | return rc;
|
| 1465 | }
|
| 1466 |
|
| 1467 | wkbuf = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
|
| 1468 | mempool_alloc_preallocated(cprb_mempool) :
|
| 1469 | mempool_alloc(cprb_mempool, GFP_ATOMIC);
|
| 1470 | if (!wkbuf) {
|
| 1471 | rc = -ENOMEM;
|
| 1472 | ZCRYPT_DBF_WARN("%s allocating tmp buffer via cprb mempool failed, rc=%d\n",
|
| 1473 | __func__, rc);
|
| 1474 | return rc;
|
| 1475 | }
|
| 1476 |
|
| 1477 |
|
| 1478 | rc = _ep11_wrapkey(card, dom, (u8 *)key, keylen,
|
| 1479 | 0, def_iv, wkbuf, &wkbuflen, xflags);
|
| 1480 | if (rc) {
|
| 1481 | ZCRYPT_DBF_ERR("%s rewrapping ep11 key to pkey failed, rc=%d\n",
|
| 1482 | __func__, rc);
|
| 1483 | goto out;
|
| 1484 | }
|
| 1485 | wki = (struct wk_info *)wkbuf;
|
| 1486 |
|
| 1487 |
|
| 1488 | if (wki->version != 1 || wki->pkeytype < 1 || wki->pkeytype > 5) {
|
| 1489 | ZCRYPT_DBF_ERR("%s wk info version %d or pkeytype %d mismatch.\n",
|
| 1490 | __func__, (int)wki->version, (int)wki->pkeytype);
|
| 1491 | rc = -EIO;
|
| 1492 | goto out;
|
| 1493 | }
|
| 1494 |
|
| 1495 |
|
| 1496 | switch (wki->pkeytype) {
|
| 1497 | case 1:
|
| 1498 | switch (wki->pkeysize) {
|
| 1499 | case 16 + 32:
|
| 1500 |
|
| 1501 | if (protkeytype)
|
| 1502 | *protkeytype = PKEY_KEYTYPE_AES_128;
|
| 1503 | break;
|
| 1504 | case 24 + 32:
|
| 1505 |
|
| 1506 | if (protkeytype)
|
| 1507 | *protkeytype = PKEY_KEYTYPE_AES_192;
|
| 1508 | break;
|
| 1509 | case 32 + 32:
|
| 1510 |
|
| 1511 | if (protkeytype)
|
| 1512 | *protkeytype = PKEY_KEYTYPE_AES_256;
|
| 1513 | break;
|
| 1514 | default:
|
| 1515 | ZCRYPT_DBF_ERR("%s unknown/unsupported AES pkeysize %d\n",
|
| 1516 | __func__, (int)wki->pkeysize);
|
| 1517 | rc = -EIO;
|
| 1518 | goto out;
|
| 1519 | }
|
| 1520 | break;
|
| 1521 | case 3:
|
| 1522 | case 4:
|
| 1523 | case 5:
|
| 1524 | if (protkeytype)
|
| 1525 | *protkeytype = PKEY_KEYTYPE_ECC;
|
| 1526 | break;
|
| 1527 | case 2:
|
| 1528 | default:
|
| 1529 | ZCRYPT_DBF_ERR("%s unknown/unsupported key type %d\n",
|
| 1530 | __func__, (int)wki->pkeytype);
|
| 1531 | rc = -EIO;
|
| 1532 | goto out;
|
| 1533 | }
|
| 1534 |
|
| 1535 |
|
| 1536 | if (wki->pkeysize > *protkeylen) {
|
| 1537 | ZCRYPT_DBF_ERR("%s wk info pkeysize %llu > protkeysize %u\n",
|
| 1538 | __func__, wki->pkeysize, *protkeylen);
|
| 1539 | rc = -EINVAL;
|
| 1540 | goto out;
|
| 1541 | }
|
| 1542 | memcpy(protkey, wki->pkey, wki->pkeysize);
|
| 1543 | *protkeylen = wki->pkeysize;
|
| 1544 |
|
| 1545 | out:
|
| 1546 | mempool_free(wkbuf, cprb_mempool);
|
| 1547 | return rc;
|
| 1548 | }
|
| 1549 | EXPORT_SYMBOL(ep11_kblob2protkey);
|
| 1550 |
|
| 1551 | int ep11_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
|
| 1552 | int minhwtype, int minapi, const u8 *wkvp, u32 xflags)
|
| 1553 | {
|
| 1554 | struct zcrypt_device_status_ext *device_status;
|
| 1555 | struct ep11_domain_info edi;
|
| 1556 | struct ep11_card_info eci;
|
| 1557 | u32 _nr_apqns = 0;
|
| 1558 | int i, card, dom;
|
| 1559 |
|
| 1560 |
|
| 1561 | mutex_lock(&dev_status_mem_mutex);
|
| 1562 | memset(dev_status_mem, 0, ZCRYPT_DEV_STATUS_EXT_SIZE);
|
| 1563 | device_status = (struct zcrypt_device_status_ext *)dev_status_mem;
|
| 1564 |
|
| 1565 |
|
| 1566 | zcrypt_device_status_mask_ext(device_status,
|
| 1567 | ZCRYPT_DEV_STATUS_CARD_MAX,
|
| 1568 | ZCRYPT_DEV_STATUS_QUEUE_MAX);
|
| 1569 |
|
| 1570 |
|
| 1571 | for (i = 0; i < ZCRYPT_DEV_STATUS_ENTRIES; i++) {
|
| 1572 | card = AP_QID_CARD(device_status[i].qid);
|
| 1573 | dom = AP_QID_QUEUE(device_status[i].qid);
|
| 1574 |
|
| 1575 | if (!device_status[i].online)
|
| 1576 | continue;
|
| 1577 |
|
| 1578 | if (!(device_status[i].functions & 0x01))
|
| 1579 | continue;
|
| 1580 |
|
| 1581 | if (cardnr != 0xFFFF && card != cardnr)
|
| 1582 | continue;
|
| 1583 |
|
| 1584 | if (domain != 0xFFFF && dom != domain)
|
| 1585 | continue;
|
| 1586 |
|
| 1587 | if (minhwtype && device_status[i].hwtype < minhwtype)
|
| 1588 | continue;
|
| 1589 |
|
| 1590 | if (minapi > 0) {
|
| 1591 | if (ep11_get_card_info(card, &eci, xflags))
|
| 1592 | continue;
|
| 1593 | if (minapi > eci.API_ord_nr)
|
| 1594 | continue;
|
| 1595 | }
|
| 1596 |
|
| 1597 | if (wkvp) {
|
| 1598 | if (ep11_get_domain_info(card, dom, &edi, xflags))
|
| 1599 | continue;
|
| 1600 | if (edi.cur_wk_state != '1')
|
| 1601 | continue;
|
| 1602 | if (memcmp(wkvp, edi.cur_wkvp, 16))
|
| 1603 | continue;
|
| 1604 | }
|
| 1605 |
|
| 1606 | if (_nr_apqns < *nr_apqns)
|
| 1607 | apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
|
| 1608 | }
|
| 1609 |
|
| 1610 | *nr_apqns = _nr_apqns;
|
| 1611 |
|
| 1612 | mutex_unlock(&dev_status_mem_mutex);
|
| 1613 |
|
| 1614 | return _nr_apqns ? 0 : -ENODEV;
|
| 1615 | }
|
| 1616 | EXPORT_SYMBOL(ep11_findcard2);
|
| 1617 |
|
| 1618 | int __init zcrypt_ep11misc_init(void)
|
| 1619 | {
|
| 1620 |
|
| 1621 | cprb_mempool = mempool_create_kmalloc_pool(2 * zcrypt_mempool_threshold,
|
| 1622 | CPRB_MEMPOOL_ITEM_SIZE);
|
| 1623 | if (!cprb_mempool)
|
| 1624 | return -ENOMEM;
|
| 1625 |
|
| 1626 |
|
| 1627 | dev_status_mem = kvmalloc(ZCRYPT_DEV_STATUS_EXT_SIZE, GFP_KERNEL);
|
| 1628 | if (!dev_status_mem) {
|
| 1629 | mempool_destroy(cprb_mempool);
|
| 1630 | return -ENOMEM;
|
| 1631 | }
|
| 1632 |
|
| 1633 | return 0;
|
| 1634 | }
|
| 1635 |
|
| 1636 | void zcrypt_ep11misc_exit(void)
|
| 1637 | {
|
| 1638 | mutex_lock(&dev_status_mem_mutex);
|
| 1639 | kvfree(dev_status_mem);
|
| 1640 | mutex_unlock(&dev_status_mem_mutex);
|
| 1641 | mempool_destroy(cprb_mempool);
|
| 1642 | }
|