1 /*
2  *  linux/drivers/s390/crypto/zcrypt_cex2a.c
3  *
4  *  zcrypt 2.1.0
5  *
6  *  Copyright (C)  2001, 2006 IBM Corporation
7  *  Author(s): Robert Burroughs
8  *	       Eric Rossman (edrossma@us.ibm.com)
9  *
10  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
11  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
12  *				  Ralph Wuerthner <rwuerthn@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28 
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/err.h>
32 #include <asm/atomic.h>
33 #include <asm/uaccess.h>
34 
35 #include "ap_bus.h"
36 #include "zcrypt_api.h"
37 #include "zcrypt_error.h"
38 #include "zcrypt_cex2a.h"
39 
40 #define CEX2A_MIN_MOD_SIZE	  1	/*    8 bits	*/
41 #define CEX2A_MAX_MOD_SIZE	256	/* 2048 bits	*/
42 
43 #define CEX2A_SPEED_RATING	970
44 
45 #define CEX2A_MAX_MESSAGE_SIZE	0x390	/* sizeof(struct type50_crb2_msg)    */
46 #define CEX2A_MAX_RESPONSE_SIZE 0x110	/* max outputdatalength + type80_hdr */
47 
48 #define CEX2A_CLEANUP_TIME	(15*HZ)
49 
50 static struct ap_device_id zcrypt_cex2a_ids[] = {
51 	{ AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
52 	{ /* end of list */ },
53 };
54 
55 #ifndef CONFIG_ZCRYPT_MONOLITHIC
56 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
57 MODULE_AUTHOR("IBM Corporation");
58 MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, "
59 		   "Copyright 2001, 2006 IBM Corporation");
60 MODULE_LICENSE("GPL");
61 #endif
62 
63 static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
64 static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
65 static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
66 				 struct ap_message *);
67 
68 static struct ap_driver zcrypt_cex2a_driver = {
69 	.probe = zcrypt_cex2a_probe,
70 	.remove = zcrypt_cex2a_remove,
71 	.receive = zcrypt_cex2a_receive,
72 	.ids = zcrypt_cex2a_ids,
73 	.request_timeout = CEX2A_CLEANUP_TIME,
74 };
75 
76 /**
77  * Convert a ICAMEX message to a type50 MEX message.
78  *
79  * @zdev: crypto device pointer
80  * @zreq: crypto request pointer
81  * @mex: pointer to user input data
82  *
83  * Returns 0 on success or -EFAULT.
84  */
85 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
86 				       struct ap_message *ap_msg,
87 				       struct ica_rsa_modexpo *mex)
88 {
89 	unsigned char *mod, *exp, *inp;
90 	int mod_len;
91 
92 	mod_len = mex->inputdatalength;
93 
94 	if (mod_len <= 128) {
95 		struct type50_meb1_msg *meb1 = ap_msg->message;
96 		memset(meb1, 0, sizeof(*meb1));
97 		ap_msg->length = sizeof(*meb1);
98 		meb1->header.msg_type_code = TYPE50_TYPE_CODE;
99 		meb1->header.msg_len = sizeof(*meb1);
100 		meb1->keyblock_type = TYPE50_MEB1_FMT;
101 		mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
102 		exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
103 		inp = meb1->message + sizeof(meb1->message) - mod_len;
104 	} else {
105 		struct type50_meb2_msg *meb2 = ap_msg->message;
106 		memset(meb2, 0, sizeof(*meb2));
107 		ap_msg->length = sizeof(*meb2);
108 		meb2->header.msg_type_code = TYPE50_TYPE_CODE;
109 		meb2->header.msg_len = sizeof(*meb2);
110 		meb2->keyblock_type = TYPE50_MEB2_FMT;
111 		mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
112 		exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
113 		inp = meb2->message + sizeof(meb2->message) - mod_len;
114 	}
115 
116 	if (copy_from_user(mod, mex->n_modulus, mod_len) ||
117 	    copy_from_user(exp, mex->b_key, mod_len) ||
118 	    copy_from_user(inp, mex->inputdata, mod_len))
119 		return -EFAULT;
120 	return 0;
121 }
122 
123 /**
124  * Convert a ICACRT message to a type50 CRT message.
125  *
126  * @zdev: crypto device pointer
127  * @zreq: crypto request pointer
128  * @crt: pointer to user input data
129  *
130  * Returns 0 on success or -EFAULT.
131  */
132 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
133 				       struct ap_message *ap_msg,
134 				       struct ica_rsa_modexpo_crt *crt)
135 {
136 	int mod_len, short_len, long_len, long_offset;
137 	unsigned char *p, *q, *dp, *dq, *u, *inp;
138 
139 	mod_len = crt->inputdatalength;
140 	short_len = mod_len / 2;
141 	long_len = mod_len / 2 + 8;
142 
143 	/*
144 	 * CEX2A cannot handle p, dp, or U > 128 bytes.
145 	 * If we have one of these, we need to do extra checking.
146 	 */
147 	if (long_len > 128) {
148 		/*
149 		 * zcrypt_rsa_crt already checked for the leading
150 		 * zeroes of np_prime, bp_key and u_mult_inc.
151 		 */
152 		long_offset = long_len - 128;
153 		long_len = 128;
154 	} else
155 		long_offset = 0;
156 
157 	/*
158 	 * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
159 	 * the larger message structure.
160 	 */
161 	if (long_len <= 64) {
162 		struct type50_crb1_msg *crb1 = ap_msg->message;
163 		memset(crb1, 0, sizeof(*crb1));
164 		ap_msg->length = sizeof(*crb1);
165 		crb1->header.msg_type_code = TYPE50_TYPE_CODE;
166 		crb1->header.msg_len = sizeof(*crb1);
167 		crb1->keyblock_type = TYPE50_CRB1_FMT;
168 		p = crb1->p + sizeof(crb1->p) - long_len;
169 		q = crb1->q + sizeof(crb1->q) - short_len;
170 		dp = crb1->dp + sizeof(crb1->dp) - long_len;
171 		dq = crb1->dq + sizeof(crb1->dq) - short_len;
172 		u = crb1->u + sizeof(crb1->u) - long_len;
173 		inp = crb1->message + sizeof(crb1->message) - mod_len;
174 	} else {
175 		struct type50_crb2_msg *crb2 = ap_msg->message;
176 		memset(crb2, 0, sizeof(*crb2));
177 		ap_msg->length = sizeof(*crb2);
178 		crb2->header.msg_type_code = TYPE50_TYPE_CODE;
179 		crb2->header.msg_len = sizeof(*crb2);
180 		crb2->keyblock_type = TYPE50_CRB2_FMT;
181 		p = crb2->p + sizeof(crb2->p) - long_len;
182 		q = crb2->q + sizeof(crb2->q) - short_len;
183 		dp = crb2->dp + sizeof(crb2->dp) - long_len;
184 		dq = crb2->dq + sizeof(crb2->dq) - short_len;
185 		u = crb2->u + sizeof(crb2->u) - long_len;
186 		inp = crb2->message + sizeof(crb2->message) - mod_len;
187 	}
188 
189 	if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
190 	    copy_from_user(q, crt->nq_prime, short_len) ||
191 	    copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
192 	    copy_from_user(dq, crt->bq_key, short_len) ||
193 	    copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
194 	    copy_from_user(inp, crt->inputdata, mod_len))
195 		return -EFAULT;
196 
197 
198 	return 0;
199 }
200 
201 /**
202  * Copy results from a type 80 reply message back to user space.
203  *
204  * @zdev: crypto device pointer
205  * @reply: reply AP message.
206  * @data: pointer to user output data
207  * @length: size of user output data
208  *
209  * Returns 0 on success or -EFAULT.
210  */
211 static int convert_type80(struct zcrypt_device *zdev,
212 			  struct ap_message *reply,
213 			  char __user *outputdata,
214 			  unsigned int outputdatalength)
215 {
216 	struct type80_hdr *t80h = reply->message;
217 	unsigned char *data;
218 
219 	if (t80h->len < sizeof(*t80h) + outputdatalength) {
220 		/* The result is too short, the CEX2A card may not do that.. */
221 		zdev->online = 0;
222 		return -EAGAIN;	/* repeat the request on a different device. */
223 	}
224 	BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
225 	data = reply->message + t80h->len - outputdatalength;
226 	if (copy_to_user(outputdata, data, outputdatalength))
227 		return -EFAULT;
228 	return 0;
229 }
230 
231 static int convert_response(struct zcrypt_device *zdev,
232 			    struct ap_message *reply,
233 			    char __user *outputdata,
234 			    unsigned int outputdatalength)
235 {
236 	/* Response type byte is the second byte in the response. */
237 	switch (((unsigned char *) reply->message)[1]) {
238 	case TYPE82_RSP_CODE:
239 	case TYPE88_RSP_CODE:
240 		return convert_error(zdev, reply);
241 	case TYPE80_RSP_CODE:
242 		return convert_type80(zdev, reply,
243 				      outputdata, outputdatalength);
244 	default: /* Unknown response type, this should NEVER EVER happen */
245 		PRINTK("Unrecognized Message Header: %08x%08x\n",
246 		       *(unsigned int *) reply->message,
247 		       *(unsigned int *) (reply->message+4));
248 		zdev->online = 0;
249 		return -EAGAIN;	/* repeat the request on a different device. */
250 	}
251 }
252 
253 /**
254  * This function is called from the AP bus code after a crypto request
255  * "msg" has finished with the reply message "reply".
256  * It is called from tasklet context.
257  * @ap_dev: pointer to the AP device
258  * @msg: pointer to the AP message
259  * @reply: pointer to the AP reply message
260  */
261 static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
262 				 struct ap_message *msg,
263 				 struct ap_message *reply)
264 {
265 	static struct error_hdr error_reply = {
266 		.type = TYPE82_RSP_CODE,
267 		.reply_code = REP82_ERROR_MACHINE_FAILURE,
268 	};
269 	struct type80_hdr *t80h = reply->message;
270 	int length;
271 
272 	/* Copy the reply message to the request message buffer. */
273 	if (IS_ERR(reply))
274 		memcpy(msg->message, &error_reply, sizeof(error_reply));
275 	else if (t80h->type == TYPE80_RSP_CODE) {
276 		length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len);
277 		memcpy(msg->message, reply->message, length);
278 	} else
279 		memcpy(msg->message, reply->message, sizeof error_reply);
280 	complete((struct completion *) msg->private);
281 }
282 
283 static atomic_t zcrypt_step = ATOMIC_INIT(0);
284 
285 /**
286  * The request distributor calls this function if it picked the CEX2A
287  * device to handle a modexpo request.
288  * @zdev: pointer to zcrypt_device structure that identifies the
289  *	  CEX2A device to the request distributor
290  * @mex: pointer to the modexpo request buffer
291  */
292 static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
293 				 struct ica_rsa_modexpo *mex)
294 {
295 	struct ap_message ap_msg;
296 	struct completion work;
297 	int rc;
298 
299 	ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
300 	if (!ap_msg.message)
301 		return -ENOMEM;
302 	ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
303 				atomic_inc_return(&zcrypt_step);
304 	ap_msg.private = &work;
305 	rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
306 	if (rc)
307 		goto out_free;
308 	init_completion(&work);
309 	ap_queue_message(zdev->ap_dev, &ap_msg);
310 	rc = wait_for_completion_interruptible(&work);
311 	if (rc == 0)
312 		rc = convert_response(zdev, &ap_msg, mex->outputdata,
313 				      mex->outputdatalength);
314 	else
315 		/* Signal pending. */
316 		ap_cancel_message(zdev->ap_dev, &ap_msg);
317 out_free:
318 	kfree(ap_msg.message);
319 	return rc;
320 }
321 
322 /**
323  * The request distributor calls this function if it picked the CEX2A
324  * device to handle a modexpo_crt request.
325  * @zdev: pointer to zcrypt_device structure that identifies the
326  *	  CEX2A device to the request distributor
327  * @crt: pointer to the modexpoc_crt request buffer
328  */
329 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
330 				     struct ica_rsa_modexpo_crt *crt)
331 {
332 	struct ap_message ap_msg;
333 	struct completion work;
334 	int rc;
335 
336 	ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
337 	if (!ap_msg.message)
338 		return -ENOMEM;
339 	ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
340 				atomic_inc_return(&zcrypt_step);
341 	ap_msg.private = &work;
342 	rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
343 	if (rc)
344 		goto out_free;
345 	init_completion(&work);
346 	ap_queue_message(zdev->ap_dev, &ap_msg);
347 	rc = wait_for_completion_interruptible(&work);
348 	if (rc == 0)
349 		rc = convert_response(zdev, &ap_msg, crt->outputdata,
350 				      crt->outputdatalength);
351 	else
352 		/* Signal pending. */
353 		ap_cancel_message(zdev->ap_dev, &ap_msg);
354 out_free:
355 	kfree(ap_msg.message);
356 	return rc;
357 }
358 
359 /**
360  * The crypto operations for a CEX2A card.
361  */
362 static struct zcrypt_ops zcrypt_cex2a_ops = {
363 	.rsa_modexpo = zcrypt_cex2a_modexpo,
364 	.rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
365 };
366 
367 /**
368  * Probe function for CEX2A cards. It always accepts the AP device
369  * since the bus_match already checked the hardware type.
370  * @ap_dev: pointer to the AP device.
371  */
372 static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
373 {
374 	struct zcrypt_device *zdev;
375 	int rc;
376 
377 	zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
378 	if (!zdev)
379 		return -ENOMEM;
380 	zdev->ap_dev = ap_dev;
381 	zdev->ops = &zcrypt_cex2a_ops;
382 	zdev->online = 1;
383 	zdev->user_space_type = ZCRYPT_CEX2A;
384 	zdev->type_string = "CEX2A";
385 	zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
386 	zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
387 	zdev->short_crt = 1;
388 	zdev->speed_rating = CEX2A_SPEED_RATING;
389 	ap_dev->reply = &zdev->reply;
390 	ap_dev->private = zdev;
391 	rc = zcrypt_device_register(zdev);
392 	if (rc)
393 		goto out_free;
394 	return 0;
395 
396 out_free:
397 	ap_dev->private = NULL;
398 	zcrypt_device_free(zdev);
399 	return rc;
400 }
401 
402 /**
403  * This is called to remove the extended CEX2A driver information
404  * if an AP device is removed.
405  */
406 static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
407 {
408 	struct zcrypt_device *zdev = ap_dev->private;
409 
410 	zcrypt_device_unregister(zdev);
411 }
412 
413 int __init zcrypt_cex2a_init(void)
414 {
415 	return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
416 }
417 
418 void __exit zcrypt_cex2a_exit(void)
419 {
420 	ap_driver_unregister(&zcrypt_cex2a_driver);
421 }
422 
423 #ifndef CONFIG_ZCRYPT_MONOLITHIC
424 module_init(zcrypt_cex2a_init);
425 module_exit(zcrypt_cex2a_exit);
426 #endif
427