1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright IBM Corp. 2001, 2012
6 * Author(s): Robert Burroughs
7 * Eric Rossman (edrossma@us.ibm.com)
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>
15 #define KMSG_COMPONENT "zcrypt"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
26 #include "zcrypt_api.h"
27 #include "zcrypt_error.h"
28 #include "zcrypt_msgtype50.h"
30 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
32 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
34 #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
35 * (max outputdatalength) +
38 MODULE_AUTHOR("IBM Corporation");
39 MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
40 "Copyright IBM Corp. 2001, 2012");
41 MODULE_LICENSE("GPL");
44 * The type 50 message family is associated with a CEX2A card.
46 * The four members of the family are described below.
48 * Note that all unsigned char arrays are right-justified and left-padded
51 * Note that all reserved fields must be zeroes.
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;
62 #define TYPE50_TYPE_CODE 0x50
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
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];
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];
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];
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];
108 unsigned char dp[64];
109 unsigned char dq[64];
111 unsigned char message[128];
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];
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];
141 * The type 80 response family is associated with a CEX2A card.
143 * Note that all unsigned char arrays are right-justified and left-padded
146 * Note that all reserved fields must be zeroes.
149 #define TYPE80_RSP_CODE 0x80
152 unsigned char reserved1;
153 unsigned char type; /* 0x80 */
155 unsigned char code; /* 0x00 */
156 unsigned char reserved2[3];
157 unsigned char reserved3[8];
160 unsigned int get_rsa_modex_fc(struct ica_rsa_modexpo *mex, int *fcode)
163 if (!mex->inputdatalength)
166 if (mex->inputdatalength <= 128) /* 1024 bit */
168 else if (mex->inputdatalength <= 256) /* 2048 bit */
176 unsigned int get_rsa_crt_fc(struct ica_rsa_modexpo_crt *crt, int *fcode)
179 if (!crt->inputdatalength)
182 if (crt->inputdatalength <= 128) /* 1024 bit */
184 else if (crt->inputdatalength <= 256) /* 2048 bit */
193 * Convert a ICAMEX message to a type50 MEX message.
195 * @zq: crypto queue pointer
196 * @ap_msg: crypto request pointer
197 * @mex: pointer to user input data
199 * Returns 0 on success or -EFAULT.
201 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue *zq,
202 struct ap_message *ap_msg,
203 struct ica_rsa_modexpo *mex)
205 unsigned char *mod, *exp, *inp;
208 mod_len = mex->inputdatalength;
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;
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))
251 * Convert a ICACRT message to a type50 CRT message.
253 * @zq: crypto queue pointer
254 * @ap_msg: crypto request pointer
255 * @crt: pointer to user input data
257 * Returns 0 on success or -EFAULT.
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)
263 int mod_len, short_len;
264 unsigned char *p, *q, *dp, *dq, *u, *inp;
266 mod_len = crt->inputdatalength;
267 short_len = (mod_len + 1) / 2;
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).
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;
319 * correct the offset of p, bp and mult_inv according zcrypt.h
320 * block size right aligned (skip the first byte)
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))
334 * Copy results from a type 80 reply message back to user space.
336 * @zq: crypto device pointer
337 * @reply: reply AP message.
338 * @data: pointer to user output data
339 * @length: size of user output data
341 * Returns 0 on success or -EFAULT.
343 static int convert_type80(struct zcrypt_queue *zq,
344 struct ap_message *reply,
345 char __user *outputdata,
346 unsigned int outputdatalength)
348 struct type80_hdr *t80h = reply->message;
351 if (t80h->len < sizeof(*t80h) + outputdatalength) {
352 /* The result is too short, the CEX2A card may not do that.. */
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));
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),
362 return -EAGAIN; /* repeat the request on a different device. */
364 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
365 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
367 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
368 data = reply->message + t80h->len - outputdatalength;
369 if (copy_to_user(outputdata, data, outputdatalength))
374 static int convert_response(struct zcrypt_queue *zq,
375 struct ap_message *reply,
376 char __user *outputdata,
377 unsigned int outputdatalength)
379 /* Response type byte is the second byte in the response. */
380 unsigned char rtype = ((unsigned char *) reply->message)[1];
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 */
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));
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. */
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
411 static void zcrypt_cex2a_receive(struct ap_queue *aq,
412 struct ap_message *msg,
413 struct ap_message *reply)
415 static struct error_hdr error_reply = {
416 .type = TYPE82_RSP_CODE,
417 .reply_code = REP82_ERROR_MACHINE_FAILURE,
419 struct type80_hdr *t80h;
422 /* Copy the reply message to the request message buffer. */
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)
429 CEX2A_MAX_RESPONSE_SIZE, t80h->len);
432 CEX3A_MAX_RESPONSE_SIZE, t80h->len);
433 memcpy(msg->message, reply->message, length);
435 memcpy(msg->message, reply->message, sizeof(error_reply));
437 complete((struct completion *) msg->private);
440 static atomic_t zcrypt_step = ATOMIC_INIT(0);
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
449 static long zcrypt_cex2a_modexpo(struct zcrypt_queue *zq,
450 struct ica_rsa_modexpo *mex)
452 struct ap_message ap_msg;
453 struct completion work;
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,
461 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
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);
472 init_completion(&work);
473 ap_queue_message(zq->queue, &ap_msg);
474 rc = wait_for_completion_interruptible(&work);
478 rc = convert_response(zq, &ap_msg, mex->outputdata,
479 mex->outputdatalength);
481 /* Signal pending. */
482 ap_cancel_message(zq->queue, &ap_msg);
484 kfree(ap_msg.message);
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
495 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_queue *zq,
496 struct ica_rsa_modexpo_crt *crt)
498 struct ap_message ap_msg;
499 struct completion work;
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,
507 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
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);
518 init_completion(&work);
519 ap_queue_message(zq->queue, &ap_msg);
520 rc = wait_for_completion_interruptible(&work);
524 rc = convert_response(zq, &ap_msg, crt->outputdata,
525 crt->outputdatalength);
527 /* Signal pending. */
528 ap_cancel_message(zq->queue, &ap_msg);
530 kfree(ap_msg.message);
535 * The crypto operations for message type 50.
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,
545 void __init zcrypt_msgtype50_init(void)
547 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
550 void __exit zcrypt_msgtype50_exit(void)
552 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);