1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright IBM Corp. 2001, 2012 4 * Author(s): Robert Burroughs 5 * Eric Rossman (edrossma@us.ibm.com) 6 * 7 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com) 8 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com> 9 * Ralph Wuerthner <rwuerthn@de.ibm.com> 10 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> 11 */ 12 13 #define KMSG_COMPONENT "zcrypt" 14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 15 16 #include <linux/module.h> 17 #include <linux/slab.h> 18 #include <linux/init.h> 19 #include <linux/err.h> 20 #include <linux/atomic.h> 21 #include <linux/uaccess.h> 22 23 #include "ap_bus.h" 24 #include "zcrypt_api.h" 25 #include "zcrypt_error.h" 26 #include "zcrypt_msgtype50.h" 27 28 /* >= CEX3A: 4096 bits */ 29 #define CEX3A_MAX_MOD_SIZE 512 30 31 /* CEX2A: max outputdatalength + type80_hdr */ 32 #define CEX2A_MAX_RESPONSE_SIZE 0x110 33 34 /* >= CEX3A: 512 bit modulus, (max outputdatalength) + type80_hdr */ 35 #define CEX3A_MAX_RESPONSE_SIZE 0x210 36 37 MODULE_AUTHOR("IBM Corporation"); 38 MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \ 39 "Copyright IBM Corp. 2001, 2012"); 40 MODULE_LICENSE("GPL"); 41 42 /* 43 * The type 50 message family is associated with a CEXxA cards. 44 * 45 * The four members of the family are described below. 46 * 47 * Note that all unsigned char arrays are right-justified and left-padded 48 * with zeroes. 49 * 50 * Note that all reserved fields must be zeroes. 51 */ 52 struct type50_hdr { 53 unsigned char reserved1; 54 unsigned char msg_type_code; /* 0x50 */ 55 unsigned short msg_len; 56 unsigned char reserved2; 57 unsigned char ignored; 58 unsigned short reserved3; 59 } __packed; 60 61 #define TYPE50_TYPE_CODE 0x50 62 63 #define TYPE50_MEB1_FMT 0x0001 64 #define TYPE50_MEB2_FMT 0x0002 65 #define TYPE50_MEB3_FMT 0x0003 66 #define TYPE50_CRB1_FMT 0x0011 67 #define TYPE50_CRB2_FMT 0x0012 68 #define TYPE50_CRB3_FMT 0x0013 69 70 /* Mod-Exp, with a small modulus */ 71 struct type50_meb1_msg { 72 struct type50_hdr header; 73 unsigned short keyblock_type; /* 0x0001 */ 74 unsigned char reserved[6]; 75 unsigned char exponent[128]; 76 unsigned char modulus[128]; 77 unsigned char message[128]; 78 } __packed; 79 80 /* Mod-Exp, with a large modulus */ 81 struct type50_meb2_msg { 82 struct type50_hdr header; 83 unsigned short keyblock_type; /* 0x0002 */ 84 unsigned char reserved[6]; 85 unsigned char exponent[256]; 86 unsigned char modulus[256]; 87 unsigned char message[256]; 88 } __packed; 89 90 /* Mod-Exp, with a larger modulus */ 91 struct type50_meb3_msg { 92 struct type50_hdr header; 93 unsigned short keyblock_type; /* 0x0003 */ 94 unsigned char reserved[6]; 95 unsigned char exponent[512]; 96 unsigned char modulus[512]; 97 unsigned char message[512]; 98 } __packed; 99 100 /* CRT, with a small modulus */ 101 struct type50_crb1_msg { 102 struct type50_hdr header; 103 unsigned short keyblock_type; /* 0x0011 */ 104 unsigned char reserved[6]; 105 unsigned char p[64]; 106 unsigned char q[64]; 107 unsigned char dp[64]; 108 unsigned char dq[64]; 109 unsigned char u[64]; 110 unsigned char message[128]; 111 } __packed; 112 113 /* CRT, with a large modulus */ 114 struct type50_crb2_msg { 115 struct type50_hdr header; 116 unsigned short keyblock_type; /* 0x0012 */ 117 unsigned char reserved[6]; 118 unsigned char p[128]; 119 unsigned char q[128]; 120 unsigned char dp[128]; 121 unsigned char dq[128]; 122 unsigned char u[128]; 123 unsigned char message[256]; 124 } __packed; 125 126 /* CRT, with a larger modulus */ 127 struct type50_crb3_msg { 128 struct type50_hdr header; 129 unsigned short keyblock_type; /* 0x0013 */ 130 unsigned char reserved[6]; 131 unsigned char p[256]; 132 unsigned char q[256]; 133 unsigned char dp[256]; 134 unsigned char dq[256]; 135 unsigned char u[256]; 136 unsigned char message[512]; 137 } __packed; 138 139 /* 140 * The type 80 response family is associated with a CEXxA cards. 141 * 142 * Note that all unsigned char arrays are right-justified and left-padded 143 * with zeroes. 144 * 145 * Note that all reserved fields must be zeroes. 146 */ 147 148 #define TYPE80_RSP_CODE 0x80 149 150 struct type80_hdr { 151 unsigned char reserved1; 152 unsigned char type; /* 0x80 */ 153 unsigned short len; 154 unsigned char code; /* 0x00 */ 155 unsigned char reserved2[3]; 156 unsigned char reserved3[8]; 157 } __packed; 158 159 int get_rsa_modex_fc(struct ica_rsa_modexpo *mex, int *fcode) 160 { 161 if (!mex->inputdatalength) 162 return -EINVAL; 163 164 if (mex->inputdatalength <= 128) /* 1024 bit */ 165 *fcode = MEX_1K; 166 else if (mex->inputdatalength <= 256) /* 2048 bit */ 167 *fcode = MEX_2K; 168 else /* 4096 bit */ 169 *fcode = MEX_4K; 170 171 return 0; 172 } 173 174 int get_rsa_crt_fc(struct ica_rsa_modexpo_crt *crt, int *fcode) 175 { 176 if (!crt->inputdatalength) 177 return -EINVAL; 178 179 if (crt->inputdatalength <= 128) /* 1024 bit */ 180 *fcode = CRT_1K; 181 else if (crt->inputdatalength <= 256) /* 2048 bit */ 182 *fcode = CRT_2K; 183 else /* 4096 bit */ 184 *fcode = CRT_4K; 185 186 return 0; 187 } 188 189 /* 190 * Convert a ICAMEX message to a type50 MEX message. 191 * 192 * @zq: crypto queue pointer 193 * @ap_msg: crypto request pointer 194 * @mex: pointer to user input data 195 * 196 * Returns 0 on success or -EFAULT. 197 */ 198 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue *zq, 199 struct ap_message *ap_msg, 200 struct ica_rsa_modexpo *mex) 201 { 202 unsigned char *mod, *exp, *inp; 203 int mod_len; 204 205 mod_len = mex->inputdatalength; 206 207 if (mod_len <= 128) { 208 struct type50_meb1_msg *meb1 = ap_msg->msg; 209 210 memset(meb1, 0, sizeof(*meb1)); 211 ap_msg->len = sizeof(*meb1); 212 meb1->header.msg_type_code = TYPE50_TYPE_CODE; 213 meb1->header.msg_len = sizeof(*meb1); 214 meb1->keyblock_type = TYPE50_MEB1_FMT; 215 mod = meb1->modulus + sizeof(meb1->modulus) - mod_len; 216 exp = meb1->exponent + sizeof(meb1->exponent) - mod_len; 217 inp = meb1->message + sizeof(meb1->message) - mod_len; 218 } else if (mod_len <= 256) { 219 struct type50_meb2_msg *meb2 = ap_msg->msg; 220 221 memset(meb2, 0, sizeof(*meb2)); 222 ap_msg->len = sizeof(*meb2); 223 meb2->header.msg_type_code = TYPE50_TYPE_CODE; 224 meb2->header.msg_len = sizeof(*meb2); 225 meb2->keyblock_type = TYPE50_MEB2_FMT; 226 mod = meb2->modulus + sizeof(meb2->modulus) - mod_len; 227 exp = meb2->exponent + sizeof(meb2->exponent) - mod_len; 228 inp = meb2->message + sizeof(meb2->message) - mod_len; 229 } else if (mod_len <= 512) { 230 struct type50_meb3_msg *meb3 = ap_msg->msg; 231 232 memset(meb3, 0, sizeof(*meb3)); 233 ap_msg->len = sizeof(*meb3); 234 meb3->header.msg_type_code = TYPE50_TYPE_CODE; 235 meb3->header.msg_len = sizeof(*meb3); 236 meb3->keyblock_type = TYPE50_MEB3_FMT; 237 mod = meb3->modulus + sizeof(meb3->modulus) - mod_len; 238 exp = meb3->exponent + sizeof(meb3->exponent) - mod_len; 239 inp = meb3->message + sizeof(meb3->message) - mod_len; 240 } else { 241 return -EINVAL; 242 } 243 244 if (copy_from_user(mod, mex->n_modulus, mod_len) || 245 copy_from_user(exp, mex->b_key, mod_len) || 246 copy_from_user(inp, mex->inputdata, mod_len)) 247 return -EFAULT; 248 249 return 0; 250 } 251 252 /* 253 * Convert a ICACRT message to a type50 CRT message. 254 * 255 * @zq: crypto queue pointer 256 * @ap_msg: crypto request pointer 257 * @crt: pointer to user input data 258 * 259 * Returns 0 on success or -EFAULT. 260 */ 261 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue *zq, 262 struct ap_message *ap_msg, 263 struct ica_rsa_modexpo_crt *crt) 264 { 265 int mod_len, short_len; 266 unsigned char *p, *q, *dp, *dq, *u, *inp; 267 268 mod_len = crt->inputdatalength; 269 short_len = (mod_len + 1) / 2; 270 271 /* 272 * CEX2A and CEX3A w/o FW update can handle requests up to 273 * 256 byte modulus (2k keys). 274 * CEX3A with FW update and newer CEXxA cards are able to handle 275 * 512 byte modulus (4k keys). 276 */ 277 if (mod_len <= 128) { /* up to 1024 bit key size */ 278 struct type50_crb1_msg *crb1 = ap_msg->msg; 279 280 memset(crb1, 0, sizeof(*crb1)); 281 ap_msg->len = sizeof(*crb1); 282 crb1->header.msg_type_code = TYPE50_TYPE_CODE; 283 crb1->header.msg_len = sizeof(*crb1); 284 crb1->keyblock_type = TYPE50_CRB1_FMT; 285 p = crb1->p + sizeof(crb1->p) - short_len; 286 q = crb1->q + sizeof(crb1->q) - short_len; 287 dp = crb1->dp + sizeof(crb1->dp) - short_len; 288 dq = crb1->dq + sizeof(crb1->dq) - short_len; 289 u = crb1->u + sizeof(crb1->u) - short_len; 290 inp = crb1->message + sizeof(crb1->message) - mod_len; 291 } else if (mod_len <= 256) { /* up to 2048 bit key size */ 292 struct type50_crb2_msg *crb2 = ap_msg->msg; 293 294 memset(crb2, 0, sizeof(*crb2)); 295 ap_msg->len = sizeof(*crb2); 296 crb2->header.msg_type_code = TYPE50_TYPE_CODE; 297 crb2->header.msg_len = sizeof(*crb2); 298 crb2->keyblock_type = TYPE50_CRB2_FMT; 299 p = crb2->p + sizeof(crb2->p) - short_len; 300 q = crb2->q + sizeof(crb2->q) - short_len; 301 dp = crb2->dp + sizeof(crb2->dp) - short_len; 302 dq = crb2->dq + sizeof(crb2->dq) - short_len; 303 u = crb2->u + sizeof(crb2->u) - short_len; 304 inp = crb2->message + sizeof(crb2->message) - mod_len; 305 } else if ((mod_len <= 512) && /* up to 4096 bit key size */ 306 (zq->zcard->max_mod_size == CEX3A_MAX_MOD_SIZE)) { 307 struct type50_crb3_msg *crb3 = ap_msg->msg; 308 309 memset(crb3, 0, sizeof(*crb3)); 310 ap_msg->len = sizeof(*crb3); 311 crb3->header.msg_type_code = TYPE50_TYPE_CODE; 312 crb3->header.msg_len = sizeof(*crb3); 313 crb3->keyblock_type = TYPE50_CRB3_FMT; 314 p = crb3->p + sizeof(crb3->p) - short_len; 315 q = crb3->q + sizeof(crb3->q) - short_len; 316 dp = crb3->dp + sizeof(crb3->dp) - short_len; 317 dq = crb3->dq + sizeof(crb3->dq) - short_len; 318 u = crb3->u + sizeof(crb3->u) - short_len; 319 inp = crb3->message + sizeof(crb3->message) - mod_len; 320 } else { 321 return -EINVAL; 322 } 323 324 /* 325 * correct the offset of p, bp and mult_inv according zcrypt.h 326 * block size right aligned (skip the first byte) 327 */ 328 if (copy_from_user(p, crt->np_prime + MSGTYPE_ADJUSTMENT, short_len) || 329 copy_from_user(q, crt->nq_prime, short_len) || 330 copy_from_user(dp, crt->bp_key + MSGTYPE_ADJUSTMENT, short_len) || 331 copy_from_user(dq, crt->bq_key, short_len) || 332 copy_from_user(u, crt->u_mult_inv + MSGTYPE_ADJUSTMENT, short_len) || 333 copy_from_user(inp, crt->inputdata, mod_len)) 334 return -EFAULT; 335 336 return 0; 337 } 338 339 /* 340 * Copy results from a type 80 reply message back to user space. 341 * 342 * @zq: crypto device pointer 343 * @reply: reply AP message. 344 * @data: pointer to user output data 345 * @length: size of user output data 346 * 347 * Returns 0 on success or -EFAULT. 348 */ 349 static int convert_type80(struct zcrypt_queue *zq, 350 struct ap_message *reply, 351 char __user *outputdata, 352 unsigned int outputdatalength) 353 { 354 struct type80_hdr *t80h = reply->msg; 355 unsigned char *data; 356 357 if (t80h->len < sizeof(*t80h) + outputdatalength) { 358 /* The result is too short, the CEXxA card may not do that.. */ 359 zq->online = 0; 360 pr_err("Crypto dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n", 361 AP_QID_CARD(zq->queue->qid), 362 AP_QID_QUEUE(zq->queue->qid), t80h->code); 363 ZCRYPT_DBF_ERR("%s dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n", 364 __func__, AP_QID_CARD(zq->queue->qid), 365 AP_QID_QUEUE(zq->queue->qid), t80h->code); 366 ap_send_online_uevent(&zq->queue->ap_dev, zq->online); 367 return -EAGAIN; 368 } 369 if (zq->zcard->user_space_type == ZCRYPT_CEX2A) 370 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE); 371 else 372 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE); 373 data = reply->msg + t80h->len - outputdatalength; 374 if (copy_to_user(outputdata, data, outputdatalength)) 375 return -EFAULT; 376 return 0; 377 } 378 379 static int convert_response_cex2a(struct zcrypt_queue *zq, 380 struct ap_message *reply, 381 char __user *outputdata, 382 unsigned int outputdatalength) 383 { 384 /* Response type byte is the second byte in the response. */ 385 unsigned char rtype = ((unsigned char *)reply->msg)[1]; 386 387 switch (rtype) { 388 case TYPE82_RSP_CODE: 389 case TYPE88_RSP_CODE: 390 return convert_error(zq, reply); 391 case TYPE80_RSP_CODE: 392 return convert_type80(zq, reply, 393 outputdata, outputdatalength); 394 default: /* Unknown response type, this should NEVER EVER happen */ 395 zq->online = 0; 396 pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n", 397 AP_QID_CARD(zq->queue->qid), 398 AP_QID_QUEUE(zq->queue->qid), 399 (int)rtype); 400 ZCRYPT_DBF_ERR( 401 "%s dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n", 402 __func__, AP_QID_CARD(zq->queue->qid), 403 AP_QID_QUEUE(zq->queue->qid), (int)rtype); 404 ap_send_online_uevent(&zq->queue->ap_dev, zq->online); 405 return -EAGAIN; 406 } 407 } 408 409 /* 410 * This function is called from the AP bus code after a crypto request 411 * "msg" has finished with the reply message "reply". 412 * It is called from tasklet context. 413 * @aq: pointer to the AP device 414 * @msg: pointer to the AP message 415 * @reply: pointer to the AP reply message 416 */ 417 static void zcrypt_cex2a_receive(struct ap_queue *aq, 418 struct ap_message *msg, 419 struct ap_message *reply) 420 { 421 static struct error_hdr error_reply = { 422 .type = TYPE82_RSP_CODE, 423 .reply_code = REP82_ERROR_MACHINE_FAILURE, 424 }; 425 struct type80_hdr *t80h; 426 int len; 427 428 /* Copy the reply message to the request message buffer. */ 429 if (!reply) 430 goto out; /* ap_msg->rc indicates the error */ 431 t80h = reply->msg; 432 if (t80h->type == TYPE80_RSP_CODE) { 433 len = t80h->len; 434 if (len > reply->bufsize || len > msg->bufsize || 435 len != reply->len) { 436 ZCRYPT_DBF_DBG("%s len mismatch => EMSGSIZE\n", __func__); 437 msg->rc = -EMSGSIZE; 438 goto out; 439 } 440 memcpy(msg->msg, reply->msg, len); 441 msg->len = len; 442 } else { 443 memcpy(msg->msg, reply->msg, sizeof(error_reply)); 444 msg->len = sizeof(error_reply); 445 } 446 out: 447 complete((struct completion *)msg->private); 448 } 449 450 static atomic_t zcrypt_step = ATOMIC_INIT(0); 451 452 /* 453 * The request distributor calls this function if it picked the CEXxA 454 * device to handle a modexpo request. 455 * @zq: pointer to zcrypt_queue structure that identifies the 456 * CEXxA device to the request distributor 457 * @mex: pointer to the modexpo request buffer 458 */ 459 static long zcrypt_cex2a_modexpo(struct zcrypt_queue *zq, 460 struct ica_rsa_modexpo *mex, 461 struct ap_message *ap_msg) 462 { 463 struct completion work; 464 int rc; 465 466 ap_msg->bufsize = (zq->zcard->user_space_type == ZCRYPT_CEX2A) ? 467 MSGTYPE50_CRB2_MAX_MSG_SIZE : MSGTYPE50_CRB3_MAX_MSG_SIZE; 468 ap_msg->msg = kmalloc(ap_msg->bufsize, GFP_KERNEL); 469 if (!ap_msg->msg) 470 return -ENOMEM; 471 ap_msg->receive = zcrypt_cex2a_receive; 472 ap_msg->psmid = (((unsigned long)current->pid) << 32) + 473 atomic_inc_return(&zcrypt_step); 474 ap_msg->private = &work; 475 rc = ICAMEX_msg_to_type50MEX_msg(zq, ap_msg, mex); 476 if (rc) 477 goto out; 478 init_completion(&work); 479 rc = ap_queue_message(zq->queue, ap_msg); 480 if (rc) 481 goto out; 482 rc = wait_for_completion_interruptible(&work); 483 if (rc == 0) { 484 rc = ap_msg->rc; 485 if (rc == 0) 486 rc = convert_response_cex2a(zq, ap_msg, 487 mex->outputdata, 488 mex->outputdatalength); 489 } else { 490 /* Signal pending. */ 491 ap_cancel_message(zq->queue, ap_msg); 492 } 493 494 out: 495 ap_msg->private = NULL; 496 if (rc) 497 ZCRYPT_DBF_DBG("%s send me cprb at dev=%02x.%04x rc=%d\n", 498 __func__, AP_QID_CARD(zq->queue->qid), 499 AP_QID_QUEUE(zq->queue->qid), rc); 500 return rc; 501 } 502 503 /* 504 * The request distributor calls this function if it picked the CEXxA 505 * device to handle a modexpo_crt request. 506 * @zq: pointer to zcrypt_queue structure that identifies the 507 * CEXxA device to the request distributor 508 * @crt: pointer to the modexpoc_crt request buffer 509 */ 510 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_queue *zq, 511 struct ica_rsa_modexpo_crt *crt, 512 struct ap_message *ap_msg) 513 { 514 struct completion work; 515 int rc; 516 517 ap_msg->bufsize = (zq->zcard->user_space_type == ZCRYPT_CEX2A) ? 518 MSGTYPE50_CRB2_MAX_MSG_SIZE : MSGTYPE50_CRB3_MAX_MSG_SIZE; 519 ap_msg->msg = kmalloc(ap_msg->bufsize, GFP_KERNEL); 520 if (!ap_msg->msg) 521 return -ENOMEM; 522 ap_msg->receive = zcrypt_cex2a_receive; 523 ap_msg->psmid = (((unsigned long)current->pid) << 32) + 524 atomic_inc_return(&zcrypt_step); 525 ap_msg->private = &work; 526 rc = ICACRT_msg_to_type50CRT_msg(zq, ap_msg, crt); 527 if (rc) 528 goto out; 529 init_completion(&work); 530 rc = ap_queue_message(zq->queue, ap_msg); 531 if (rc) 532 goto out; 533 rc = wait_for_completion_interruptible(&work); 534 if (rc == 0) { 535 rc = ap_msg->rc; 536 if (rc == 0) 537 rc = convert_response_cex2a(zq, ap_msg, 538 crt->outputdata, 539 crt->outputdatalength); 540 } else { 541 /* Signal pending. */ 542 ap_cancel_message(zq->queue, ap_msg); 543 } 544 545 out: 546 ap_msg->private = NULL; 547 if (rc) 548 ZCRYPT_DBF_DBG("%s send crt cprb at dev=%02x.%04x rc=%d\n", 549 __func__, AP_QID_CARD(zq->queue->qid), 550 AP_QID_QUEUE(zq->queue->qid), rc); 551 return rc; 552 } 553 554 /* 555 * The crypto operations for message type 50. 556 */ 557 static struct zcrypt_ops zcrypt_msgtype50_ops = { 558 .rsa_modexpo = zcrypt_cex2a_modexpo, 559 .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt, 560 .owner = THIS_MODULE, 561 .name = MSGTYPE50_NAME, 562 .variant = MSGTYPE50_VARIANT_DEFAULT, 563 }; 564 565 void __init zcrypt_msgtype50_init(void) 566 { 567 zcrypt_msgtype_register(&zcrypt_msgtype50_ops); 568 } 569 570 void __exit zcrypt_msgtype50_exit(void) 571 { 572 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops); 573 } 574