xref: /linux/drivers/s390/crypto/zcrypt_msgtype50.c (revision 375d5d57545f53d2bc3e15ada0e9da360fc68bea)
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 
155 struct type80_reply {
156 	struct type80_hdr hdr;
157 	char data[];
158 } __packed;
159 
get_rsa_modex_fc(struct ica_rsa_modexpo * mex,int * fcode)160 int get_rsa_modex_fc(struct ica_rsa_modexpo *mex, int *fcode)
161 {
162 	if (!mex->inputdatalength)
163 		return -EINVAL;
164 
165 	if (mex->inputdatalength <= 128)	/* 1024 bit */
166 		*fcode = MEX_1K;
167 	else if (mex->inputdatalength <= 256)	/* 2048 bit */
168 		*fcode = MEX_2K;
169 	else					/* 4096 bit */
170 		*fcode = MEX_4K;
171 
172 	return 0;
173 }
174 
get_rsa_crt_fc(struct ica_rsa_modexpo_crt * crt,int * fcode)175 int get_rsa_crt_fc(struct ica_rsa_modexpo_crt *crt, int *fcode)
176 {
177 	if (!crt->inputdatalength)
178 		return -EINVAL;
179 
180 	if (crt->inputdatalength <= 128)	/* 1024 bit */
181 		*fcode = CRT_1K;
182 	else if (crt->inputdatalength <= 256)	/* 2048 bit */
183 		*fcode = CRT_2K;
184 	else					/* 4096 bit */
185 		*fcode = CRT_4K;
186 
187 	return 0;
188 }
189 
190 /*
191  * Convert a ICAMEX message to a type50 MEX message.
192  *
193  * @zq: crypto queue pointer
194  * @ap_msg: crypto request pointer
195  * @mex: pointer to user input data
196  *
197  * Returns 0 on success or -EFAULT.
198  */
ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue * zq,struct ap_message * ap_msg,struct ica_rsa_modexpo * mex)199 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue *zq,
200 				       struct ap_message *ap_msg,
201 				       struct ica_rsa_modexpo *mex)
202 {
203 	unsigned char *mod, *exp, *inp;
204 	int mod_len;
205 
206 	mod_len = mex->inputdatalength;
207 
208 	if (mod_len <= 128) {
209 		struct type50_meb1_msg *meb1 = ap_msg->msg;
210 
211 		memset(meb1, 0, sizeof(*meb1));
212 		ap_msg->len = sizeof(*meb1);
213 		meb1->header.msg_type_code = TYPE50_TYPE_CODE;
214 		meb1->header.msg_len = sizeof(*meb1);
215 		meb1->keyblock_type = TYPE50_MEB1_FMT;
216 		mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
217 		exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
218 		inp = meb1->message + sizeof(meb1->message) - mod_len;
219 	} else if (mod_len <= 256) {
220 		struct type50_meb2_msg *meb2 = ap_msg->msg;
221 
222 		memset(meb2, 0, sizeof(*meb2));
223 		ap_msg->len = 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->msg;
232 
233 		memset(meb3, 0, sizeof(*meb3));
234 		ap_msg->len = sizeof(*meb3);
235 		meb3->header.msg_type_code = TYPE50_TYPE_CODE;
236 		meb3->header.msg_len = sizeof(*meb3);
237 		meb3->keyblock_type = TYPE50_MEB3_FMT;
238 		mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
239 		exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
240 		inp = meb3->message + sizeof(meb3->message) - mod_len;
241 	} else {
242 		return -EINVAL;
243 	}
244 
245 	if (copy_from_user(mod, mex->n_modulus, mod_len) ||
246 	    copy_from_user(exp, mex->b_key, mod_len) ||
247 	    copy_from_user(inp, mex->inputdata, mod_len))
248 		return -EFAULT;
249 
250 	return 0;
251 }
252 
253 /*
254  * Convert a ICACRT message to a type50 CRT message.
255  *
256  * @zq: crypto queue pointer
257  * @ap_msg: crypto request pointer
258  * @crt: pointer to user input data
259  *
260  * Returns 0 on success or -EFAULT.
261  */
ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue * zq,struct ap_message * ap_msg,struct ica_rsa_modexpo_crt * crt)262 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue *zq,
263 				       struct ap_message *ap_msg,
264 				       struct ica_rsa_modexpo_crt *crt)
265 {
266 	int mod_len, short_len;
267 	unsigned char *p, *q, *dp, *dq, *u, *inp;
268 
269 	mod_len = crt->inputdatalength;
270 	short_len = (mod_len + 1) / 2;
271 
272 	/*
273 	 * CEX2A and CEX3A w/o FW update can handle requests up to
274 	 * 256 byte modulus (2k keys).
275 	 * CEX3A with FW update and newer CEXxA cards are able to handle
276 	 * 512 byte modulus (4k keys).
277 	 */
278 	if (mod_len <= 128) {		/* up to 1024 bit key size */
279 		struct type50_crb1_msg *crb1 = ap_msg->msg;
280 
281 		memset(crb1, 0, sizeof(*crb1));
282 		ap_msg->len = sizeof(*crb1);
283 		crb1->header.msg_type_code = TYPE50_TYPE_CODE;
284 		crb1->header.msg_len = sizeof(*crb1);
285 		crb1->keyblock_type = TYPE50_CRB1_FMT;
286 		p = crb1->p + sizeof(crb1->p) - short_len;
287 		q = crb1->q + sizeof(crb1->q) - short_len;
288 		dp = crb1->dp + sizeof(crb1->dp) - short_len;
289 		dq = crb1->dq + sizeof(crb1->dq) - short_len;
290 		u = crb1->u + sizeof(crb1->u) - short_len;
291 		inp = crb1->message + sizeof(crb1->message) - mod_len;
292 	} else if (mod_len <= 256) {	/* up to 2048 bit key size */
293 		struct type50_crb2_msg *crb2 = ap_msg->msg;
294 
295 		memset(crb2, 0, sizeof(*crb2));
296 		ap_msg->len = sizeof(*crb2);
297 		crb2->header.msg_type_code = TYPE50_TYPE_CODE;
298 		crb2->header.msg_len = sizeof(*crb2);
299 		crb2->keyblock_type = TYPE50_CRB2_FMT;
300 		p = crb2->p + sizeof(crb2->p) - short_len;
301 		q = crb2->q + sizeof(crb2->q) - short_len;
302 		dp = crb2->dp + sizeof(crb2->dp) - short_len;
303 		dq = crb2->dq + sizeof(crb2->dq) - short_len;
304 		u = crb2->u + sizeof(crb2->u) - short_len;
305 		inp = crb2->message + sizeof(crb2->message) - mod_len;
306 	} else if ((mod_len <= 512) &&	/* up to 4096 bit key size */
307 		   (zq->zcard->max_mod_size == CEX3A_MAX_MOD_SIZE)) {
308 		struct type50_crb3_msg *crb3 = ap_msg->msg;
309 
310 		memset(crb3, 0, sizeof(*crb3));
311 		ap_msg->len = sizeof(*crb3);
312 		crb3->header.msg_type_code = TYPE50_TYPE_CODE;
313 		crb3->header.msg_len = sizeof(*crb3);
314 		crb3->keyblock_type = TYPE50_CRB3_FMT;
315 		p = crb3->p + sizeof(crb3->p) - short_len;
316 		q = crb3->q + sizeof(crb3->q) - short_len;
317 		dp = crb3->dp + sizeof(crb3->dp) - short_len;
318 		dq = crb3->dq + sizeof(crb3->dq) - short_len;
319 		u = crb3->u + sizeof(crb3->u) - short_len;
320 		inp = crb3->message + sizeof(crb3->message) - mod_len;
321 	} else {
322 		return -EINVAL;
323 	}
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 neg. errno value on failure.
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_reply *msg = reply->msg;
356 	size_t payload_len;
357 
358 	/*
359 	 * reply->len is always >= sizeof(struct type80_reply) here and
360 	 * outputdatalength (the modulus size) is guaranteed to be equal to
361 	 * inputdatalength and 0 < outputdatalength <= CEX3A_MAX_MOD_SIZE,
362 	 * also payload size may be >= outputdatalength.
363 	 */
364 
365 	payload_len = reply->len - sizeof(msg->hdr);
366 
367 	if (outputdatalength > payload_len) {
368 		/* We expect at least outputdatalength bytes, broken card ? */
369 		zq->online = 0;
370 		pr_err("Crypto dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
371 		       AP_QID_CARD(zq->queue->qid),
372 		       AP_QID_QUEUE(zq->queue->qid), msg->hdr.code);
373 		ZCRYPT_DBF_ERR("%s dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
374 			       __func__, AP_QID_CARD(zq->queue->qid),
375 			       AP_QID_QUEUE(zq->queue->qid), msg->hdr.code);
376 		ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
377 		return -EAGAIN;
378 	}
379 
380 	if (copy_to_user(outputdata,
381 			 msg->data + payload_len - outputdatalength,
382 			 outputdatalength))
383 		return -EFAULT;
384 
385 	return 0;
386 }
387 
convert_response(struct zcrypt_queue * zq,struct ap_message * reply,char __user * outputdata,unsigned int outputdatalength)388 static int convert_response(struct zcrypt_queue *zq,
389 			    struct ap_message *reply,
390 			    char __user *outputdata,
391 			    unsigned int outputdatalength)
392 {
393 	struct type80_reply *msg = reply->msg;
394 
395 	/* reply->len is always >= sizeof(struct error_hdr) here */
396 
397 	switch (msg->hdr.type) {
398 	case TYPE82_RSP_CODE:
399 	case TYPE88_RSP_CODE:
400 		return convert_error(zq, reply);
401 	case TYPE80_RSP_CODE:
402 		if (msg->hdr.code)
403 			return convert_error(zq, reply);
404 		if (reply->len < sizeof(struct type80_reply))
405 			return -EINVAL;
406 		return convert_type80(zq, reply,
407 				      outputdata, outputdatalength);
408 	default: /* Unknown response type, this should NEVER EVER happen */
409 		zq->online = 0;
410 		pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
411 		       AP_QID_CARD(zq->queue->qid),
412 		       AP_QID_QUEUE(zq->queue->qid),
413 		       (int)msg->hdr.type);
414 		ZCRYPT_DBF_ERR(
415 			"%s dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
416 			__func__, AP_QID_CARD(zq->queue->qid),
417 			AP_QID_QUEUE(zq->queue->qid), (int)msg->hdr.type);
418 		ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
419 		return -EAGAIN;
420 	}
421 }
422 
423 /*
424  * This function is called from the AP bus code after a crypto request
425  * "msg" has finished with the reply message "reply".
426  * It is called from tasklet context.
427  * @aq: pointer to the AP device
428  * @msg: pointer to the AP message
429  * @reply: pointer to the AP reply message
430  */
zcrypt_msgtype50_receive(struct ap_queue * aq,struct ap_message * msg,struct ap_message * reply)431 static void zcrypt_msgtype50_receive(struct ap_queue *aq,
432 				     struct ap_message *msg,
433 				     struct ap_message *reply)
434 {
435 	static struct error_hdr error_reply = {
436 		.type = TYPE82_RSP_CODE,
437 		.reply_code = REP82_ERROR_MACHINE_FAILURE,
438 	};
439 	struct type80_hdr *t80h;
440 	size_t len;
441 
442 	/* Copy the reply message to the request message buffer. */
443 	if (!reply)
444 		goto out;	/* ap_msg->rc indicates the error */
445 
446 	t80h = reply->msg;
447 
448 	if (reply->len < sizeof(*t80h) ||
449 	    t80h->type != TYPE80_RSP_CODE) {
450 		if (reply->len < sizeof(error_reply)) {
451 			/* total broken reply, use static error reply instead */
452 			memcpy(msg->msg, &error_reply, sizeof(error_reply));
453 			msg->len = sizeof(error_reply);
454 			goto out;
455 		} else {
456 			/* malformed reply, convert function will handle this */
457 			len = reply->len;
458 			goto copy_len_and_out;
459 		}
460 	}
461 
462 	len = t80h->len;
463 
464 copy_len_and_out:
465 	if (len != reply->len) {
466 		msg->rc = -EMSGSIZE;
467 		pr_debug("len %zu rpl.len %zu mismatch, msg.rc=%d\n",
468 			 len, reply->len, msg->rc);
469 		goto out;
470 	}
471 	if (len > reply->bufsize || len > msg->bufsize) {
472 		msg->rc = -EMSGSIZE;
473 		pr_debug("len %zu exceeds buf %zu/%zu, msg.rc=%d\n",
474 			 len, reply->bufsize, msg->bufsize, msg->rc);
475 		goto out;
476 	}
477 	memcpy(msg->msg, reply->msg, len);
478 	msg->len = len;
479 
480 out:
481 	complete(&msg->response.work);
482 }
483 
484 static atomic_t zcrypt_step = ATOMIC_INIT(0);
485 
486 /*
487  * The request distributor calls this function if it picked the CEXxA
488  * device to handle a modexpo request.
489  * @zq: pointer to zcrypt_queue structure that identifies the
490  *	CEXxA device to the request distributor
491  * @mex: pointer to the modexpo request buffer
492  * This function assumes that ap_msg has been initialized with
493  * ap_init_apmsg() and thus a valid buffer with the size of
494  * ap_msg->bufsize is available within ap_msg. Also the caller has
495  * to make sure ap_release_apmsg() is always called even on failure.
496  */
zcrypt_msgtype50_modexpo(struct zcrypt_queue * zq,struct ica_rsa_modexpo * mex,struct ap_message * ap_msg)497 static long zcrypt_msgtype50_modexpo(struct zcrypt_queue *zq,
498 				     struct ica_rsa_modexpo *mex,
499 				     struct ap_message *ap_msg)
500 {
501 	int rc;
502 
503 	if (ap_msg->bufsize < MSGTYPE50_CRB3_MAX_MSG_SIZE)
504 		return -EMSGSIZE;
505 	ap_msg->receive = zcrypt_msgtype50_receive;
506 	ap_msg->psmid = (((unsigned long)current->pid) << 32) +
507 		atomic_inc_return(&zcrypt_step);
508 	rc = ICAMEX_msg_to_type50MEX_msg(zq, ap_msg, mex);
509 	if (rc)
510 		goto out;
511 	init_completion(&ap_msg->response.work);
512 	rc = ap_queue_message(zq->queue, ap_msg);
513 	if (rc)
514 		goto out;
515 	rc = wait_for_completion_interruptible(&ap_msg->response.work);
516 	if (rc == 0) {
517 		rc = ap_msg->rc;
518 		if (rc == 0)
519 			rc = convert_response(zq, ap_msg,
520 					      mex->outputdata,
521 					      mex->outputdatalength);
522 	} else {
523 		/* Signal pending. */
524 		ap_cancel_message(zq->queue, ap_msg);
525 	}
526 
527 out:
528 	if (rc)
529 		pr_debug("send me cprb at dev=%02x.%04x rc=%d\n",
530 			 AP_QID_CARD(zq->queue->qid),
531 			 AP_QID_QUEUE(zq->queue->qid), rc);
532 	return rc;
533 }
534 
535 /*
536  * The request distributor calls this function if it picked the CEXxA
537  * device to handle a modexpo_crt request.
538  * @zq: pointer to zcrypt_queue structure that identifies the
539  *	CEXxA device to the request distributor
540  * @crt: pointer to the modexpoc_crt request buffer
541  * This function assumes that ap_msg has been initialized with
542  * ap_init_apmsg() and thus a valid buffer with the size of
543  * ap_msg->bufsize is available within ap_msg. Also the caller has
544  * to make sure ap_release_apmsg() is always called even on failure.
545  */
zcrypt_msgtype50_modexpo_crt(struct zcrypt_queue * zq,struct ica_rsa_modexpo_crt * crt,struct ap_message * ap_msg)546 static long zcrypt_msgtype50_modexpo_crt(struct zcrypt_queue *zq,
547 					 struct ica_rsa_modexpo_crt *crt,
548 					 struct ap_message *ap_msg)
549 {
550 	int rc;
551 
552 	if (ap_msg->bufsize < MSGTYPE50_CRB3_MAX_MSG_SIZE)
553 		return -EMSGSIZE;
554 	ap_msg->receive = zcrypt_msgtype50_receive;
555 	ap_msg->psmid = (((unsigned long)current->pid) << 32) +
556 		atomic_inc_return(&zcrypt_step);
557 	rc = ICACRT_msg_to_type50CRT_msg(zq, ap_msg, crt);
558 	if (rc)
559 		goto out;
560 	init_completion(&ap_msg->response.work);
561 	rc = ap_queue_message(zq->queue, ap_msg);
562 	if (rc)
563 		goto out;
564 	rc = wait_for_completion_interruptible(&ap_msg->response.work);
565 	if (rc == 0) {
566 		rc = ap_msg->rc;
567 		if (rc == 0)
568 			rc = convert_response(zq, ap_msg,
569 					      crt->outputdata,
570 					      crt->outputdatalength);
571 	} else {
572 		/* Signal pending. */
573 		ap_cancel_message(zq->queue, ap_msg);
574 	}
575 
576 out:
577 	if (rc)
578 		pr_debug("send crt cprb at dev=%02x.%04x rc=%d\n",
579 			 AP_QID_CARD(zq->queue->qid),
580 			 AP_QID_QUEUE(zq->queue->qid), rc);
581 	return rc;
582 }
583 
584 /*
585  * The crypto operations for message type 50.
586  */
587 static struct zcrypt_ops zcrypt_msgtype50_ops = {
588 	.rsa_modexpo = zcrypt_msgtype50_modexpo,
589 	.rsa_modexpo_crt = zcrypt_msgtype50_modexpo_crt,
590 	.owner = THIS_MODULE,
591 	.name = MSGTYPE50_NAME,
592 	.variant = MSGTYPE50_VARIANT_DEFAULT,
593 };
594 
zcrypt_msgtype50_init(void)595 void __init zcrypt_msgtype50_init(void)
596 {
597 	zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
598 }
599 
zcrypt_msgtype50_exit(void)600 void __exit zcrypt_msgtype50_exit(void)
601 {
602 	zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
603 }
604