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