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