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