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