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/slab.h>
31#include <linux/init.h>
32#include <linux/err.h>
33#include <linux/atomic.h>
34#include <asm/uaccess.h>
35
36#include "ap_bus.h"
37#include "zcrypt_api.h"
38#include "zcrypt_error.h"
39#include "zcrypt_cex2a.h"
40
41#define CEX2A_MIN_MOD_SIZE	  1	/*    8 bits	*/
42#define CEX2A_MAX_MOD_SIZE	256	/* 2048 bits	*/
43#define CEX3A_MIN_MOD_SIZE	CEX2A_MIN_MOD_SIZE
44#define CEX3A_MAX_MOD_SIZE	512	/* 4096 bits	*/
45
46#define CEX2A_SPEED_RATING	970
47#define CEX3A_SPEED_RATING	900 /* Fixme: Needs finetuning */
48
49#define CEX2A_MAX_MESSAGE_SIZE	0x390	/* sizeof(struct type50_crb2_msg)    */
50#define CEX2A_MAX_RESPONSE_SIZE 0x110	/* max outputdatalength + type80_hdr */
51
52#define CEX3A_MAX_RESPONSE_SIZE	0x210	/* 512 bit modulus
53					 * (max outputdatalength) +
54					 * type80_hdr*/
55#define CEX3A_MAX_MESSAGE_SIZE	sizeof(struct type50_crb3_msg)
56
57#define CEX2A_CLEANUP_TIME	(15*HZ)
58#define CEX3A_CLEANUP_TIME	CEX2A_CLEANUP_TIME
59
60static struct ap_device_id zcrypt_cex2a_ids[] = {
61	{ AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
62	{ AP_DEVICE(AP_DEVICE_TYPE_CEX3A) },
63	{ /* end of list */ },
64};
65
66MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
67MODULE_AUTHOR("IBM Corporation");
68MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, "
69		   "Copyright 2001, 2006 IBM Corporation");
70MODULE_LICENSE("GPL");
71
72static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
73static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
74static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
75				 struct ap_message *);
76
77static struct ap_driver zcrypt_cex2a_driver = {
78	.probe = zcrypt_cex2a_probe,
79	.remove = zcrypt_cex2a_remove,
80	.receive = zcrypt_cex2a_receive,
81	.ids = zcrypt_cex2a_ids,
82	.request_timeout = CEX2A_CLEANUP_TIME,
83};
84
85/**
86 * Convert a ICAMEX message to a type50 MEX message.
87 *
88 * @zdev: crypto device pointer
89 * @zreq: crypto request pointer
90 * @mex: pointer to user input data
91 *
92 * Returns 0 on success or -EFAULT.
93 */
94static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
95				       struct ap_message *ap_msg,
96				       struct ica_rsa_modexpo *mex)
97{
98	unsigned char *mod, *exp, *inp;
99	int mod_len;
100
101	mod_len = mex->inputdatalength;
102
103	if (mod_len <= 128) {
104		struct type50_meb1_msg *meb1 = ap_msg->message;
105		memset(meb1, 0, sizeof(*meb1));
106		ap_msg->length = sizeof(*meb1);
107		meb1->header.msg_type_code = TYPE50_TYPE_CODE;
108		meb1->header.msg_len = sizeof(*meb1);
109		meb1->keyblock_type = TYPE50_MEB1_FMT;
110		mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
111		exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
112		inp = meb1->message + sizeof(meb1->message) - mod_len;
113	} else if (mod_len <= 256) {
114		struct type50_meb2_msg *meb2 = ap_msg->message;
115		memset(meb2, 0, sizeof(*meb2));
116		ap_msg->length = sizeof(*meb2);
117		meb2->header.msg_type_code = TYPE50_TYPE_CODE;
118		meb2->header.msg_len = sizeof(*meb2);
119		meb2->keyblock_type = TYPE50_MEB2_FMT;
120		mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
121		exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
122		inp = meb2->message + sizeof(meb2->message) - mod_len;
123	} else {
124		/* mod_len > 256 = 4096 bit RSA Key */
125		struct type50_meb3_msg *meb3 = ap_msg->message;
126		memset(meb3, 0, sizeof(*meb3));
127		ap_msg->length = sizeof(*meb3);
128		meb3->header.msg_type_code = TYPE50_TYPE_CODE;
129		meb3->header.msg_len = sizeof(*meb3);
130		meb3->keyblock_type = TYPE50_MEB3_FMT;
131		mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
132		exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
133		inp = meb3->message + sizeof(meb3->message) - mod_len;
134	}
135
136	if (copy_from_user(mod, mex->n_modulus, mod_len) ||
137	    copy_from_user(exp, mex->b_key, mod_len) ||
138	    copy_from_user(inp, mex->inputdata, mod_len))
139		return -EFAULT;
140	return 0;
141}
142
143/**
144 * Convert a ICACRT message to a type50 CRT message.
145 *
146 * @zdev: crypto device pointer
147 * @zreq: crypto request pointer
148 * @crt: pointer to user input data
149 *
150 * Returns 0 on success or -EFAULT.
151 */
152static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
153				       struct ap_message *ap_msg,
154				       struct ica_rsa_modexpo_crt *crt)
155{
156	int mod_len, short_len, long_len, long_offset, limit;
157	unsigned char *p, *q, *dp, *dq, *u, *inp;
158
159	mod_len = crt->inputdatalength;
160	short_len = mod_len / 2;
161	long_len = mod_len / 2 + 8;
162
163	/*
164	 * CEX2A cannot handle p, dp, or U > 128 bytes.
165	 * If we have one of these, we need to do extra checking.
166	 * For CEX3A the limit is 256 bytes.
167	 */
168	if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)
169		limit = 256;
170	else
171		limit = 128;
172
173	if (long_len > limit) {
174		/*
175		 * zcrypt_rsa_crt already checked for the leading
176		 * zeroes of np_prime, bp_key and u_mult_inc.
177		 */
178		long_offset = long_len - limit;
179		long_len = limit;
180	} else
181		long_offset = 0;
182
183	/*
184	 * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
185	 * the larger message structure.
186	 */
187	if (long_len <= 64) {
188		struct type50_crb1_msg *crb1 = ap_msg->message;
189		memset(crb1, 0, sizeof(*crb1));
190		ap_msg->length = sizeof(*crb1);
191		crb1->header.msg_type_code = TYPE50_TYPE_CODE;
192		crb1->header.msg_len = sizeof(*crb1);
193		crb1->keyblock_type = TYPE50_CRB1_FMT;
194		p = crb1->p + sizeof(crb1->p) - long_len;
195		q = crb1->q + sizeof(crb1->q) - short_len;
196		dp = crb1->dp + sizeof(crb1->dp) - long_len;
197		dq = crb1->dq + sizeof(crb1->dq) - short_len;
198		u = crb1->u + sizeof(crb1->u) - long_len;
199		inp = crb1->message + sizeof(crb1->message) - mod_len;
200	} else if (long_len <= 128) {
201		struct type50_crb2_msg *crb2 = ap_msg->message;
202		memset(crb2, 0, sizeof(*crb2));
203		ap_msg->length = sizeof(*crb2);
204		crb2->header.msg_type_code = TYPE50_TYPE_CODE;
205		crb2->header.msg_len = sizeof(*crb2);
206		crb2->keyblock_type = TYPE50_CRB2_FMT;
207		p = crb2->p + sizeof(crb2->p) - long_len;
208		q = crb2->q + sizeof(crb2->q) - short_len;
209		dp = crb2->dp + sizeof(crb2->dp) - long_len;
210		dq = crb2->dq + sizeof(crb2->dq) - short_len;
211		u = crb2->u + sizeof(crb2->u) - long_len;
212		inp = crb2->message + sizeof(crb2->message) - mod_len;
213	} else {
214		/* long_len >= 256 */
215		struct type50_crb3_msg *crb3 = ap_msg->message;
216		memset(crb3, 0, sizeof(*crb3));
217		ap_msg->length = sizeof(*crb3);
218		crb3->header.msg_type_code = TYPE50_TYPE_CODE;
219		crb3->header.msg_len = sizeof(*crb3);
220		crb3->keyblock_type = TYPE50_CRB3_FMT;
221		p = crb3->p + sizeof(crb3->p) - long_len;
222		q = crb3->q + sizeof(crb3->q) - short_len;
223		dp = crb3->dp + sizeof(crb3->dp) - long_len;
224		dq = crb3->dq + sizeof(crb3->dq) - short_len;
225		u = crb3->u + sizeof(crb3->u) - long_len;
226		inp = crb3->message + sizeof(crb3->message) - mod_len;
227	}
228
229	if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
230	    copy_from_user(q, crt->nq_prime, short_len) ||
231	    copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
232	    copy_from_user(dq, crt->bq_key, short_len) ||
233	    copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
234	    copy_from_user(inp, crt->inputdata, mod_len))
235		return -EFAULT;
236
237	return 0;
238}
239
240/**
241 * Copy results from a type 80 reply message back to user space.
242 *
243 * @zdev: crypto device pointer
244 * @reply: reply AP message.
245 * @data: pointer to user output data
246 * @length: size of user output data
247 *
248 * Returns 0 on success or -EFAULT.
249 */
250static int convert_type80(struct zcrypt_device *zdev,
251			  struct ap_message *reply,
252			  char __user *outputdata,
253			  unsigned int outputdatalength)
254{
255	struct type80_hdr *t80h = reply->message;
256	unsigned char *data;
257
258	if (t80h->len < sizeof(*t80h) + outputdatalength) {
259		/* The result is too short, the CEX2A card may not do that.. */
260		zdev->online = 0;
261		return -EAGAIN;	/* repeat the request on a different device. */
262	}
263	if (zdev->user_space_type == ZCRYPT_CEX2A)
264		BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
265	else
266		BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
267	data = reply->message + t80h->len - outputdatalength;
268	if (copy_to_user(outputdata, data, outputdatalength))
269		return -EFAULT;
270	return 0;
271}
272
273static int convert_response(struct zcrypt_device *zdev,
274			    struct ap_message *reply,
275			    char __user *outputdata,
276			    unsigned int outputdatalength)
277{
278	/* Response type byte is the second byte in the response. */
279	switch (((unsigned char *) reply->message)[1]) {
280	case TYPE82_RSP_CODE:
281	case TYPE88_RSP_CODE:
282		return convert_error(zdev, reply);
283	case TYPE80_RSP_CODE:
284		return convert_type80(zdev, reply,
285				      outputdata, outputdatalength);
286	default: /* Unknown response type, this should NEVER EVER happen */
287		zdev->online = 0;
288		return -EAGAIN;	/* repeat the request on a different device. */
289	}
290}
291
292/**
293 * This function is called from the AP bus code after a crypto request
294 * "msg" has finished with the reply message "reply".
295 * It is called from tasklet context.
296 * @ap_dev: pointer to the AP device
297 * @msg: pointer to the AP message
298 * @reply: pointer to the AP reply message
299 */
300static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
301				 struct ap_message *msg,
302				 struct ap_message *reply)
303{
304	static struct error_hdr error_reply = {
305		.type = TYPE82_RSP_CODE,
306		.reply_code = REP82_ERROR_MACHINE_FAILURE,
307	};
308	struct type80_hdr *t80h;
309	int length;
310
311	/* Copy the reply message to the request message buffer. */
312	if (IS_ERR(reply)) {
313		memcpy(msg->message, &error_reply, sizeof(error_reply));
314		goto out;
315	}
316	t80h = reply->message;
317	if (t80h->type == TYPE80_RSP_CODE) {
318		if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
319			length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len);
320		else
321			length = min(CEX3A_MAX_RESPONSE_SIZE, (int) t80h->len);
322		memcpy(msg->message, reply->message, length);
323	} else
324		memcpy(msg->message, reply->message, sizeof error_reply);
325out:
326	complete((struct completion *) msg->private);
327}
328
329static atomic_t zcrypt_step = ATOMIC_INIT(0);
330
331/**
332 * The request distributor calls this function if it picked the CEX2A
333 * device to handle a modexpo request.
334 * @zdev: pointer to zcrypt_device structure that identifies the
335 *	  CEX2A device to the request distributor
336 * @mex: pointer to the modexpo request buffer
337 */
338static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
339				 struct ica_rsa_modexpo *mex)
340{
341	struct ap_message ap_msg;
342	struct completion work;
343	int rc;
344
345	ap_init_message(&ap_msg);
346	if (zdev->user_space_type == ZCRYPT_CEX2A)
347		ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
348	else
349		ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL);
350	if (!ap_msg.message)
351		return -ENOMEM;
352	ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
353				atomic_inc_return(&zcrypt_step);
354	ap_msg.private = &work;
355	rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
356	if (rc)
357		goto out_free;
358	init_completion(&work);
359	ap_queue_message(zdev->ap_dev, &ap_msg);
360	rc = wait_for_completion_interruptible(&work);
361	if (rc == 0)
362		rc = convert_response(zdev, &ap_msg, mex->outputdata,
363				      mex->outputdatalength);
364	else
365		/* Signal pending. */
366		ap_cancel_message(zdev->ap_dev, &ap_msg);
367out_free:
368	kfree(ap_msg.message);
369	return rc;
370}
371
372/**
373 * The request distributor calls this function if it picked the CEX2A
374 * device to handle a modexpo_crt request.
375 * @zdev: pointer to zcrypt_device structure that identifies the
376 *	  CEX2A device to the request distributor
377 * @crt: pointer to the modexpoc_crt request buffer
378 */
379static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
380				     struct ica_rsa_modexpo_crt *crt)
381{
382	struct ap_message ap_msg;
383	struct completion work;
384	int rc;
385
386	ap_init_message(&ap_msg);
387	if (zdev->user_space_type == ZCRYPT_CEX2A)
388		ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
389	else
390		ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL);
391	if (!ap_msg.message)
392		return -ENOMEM;
393	ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
394				atomic_inc_return(&zcrypt_step);
395	ap_msg.private = &work;
396	rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
397	if (rc)
398		goto out_free;
399	init_completion(&work);
400	ap_queue_message(zdev->ap_dev, &ap_msg);
401	rc = wait_for_completion_interruptible(&work);
402	if (rc == 0)
403		rc = convert_response(zdev, &ap_msg, crt->outputdata,
404				      crt->outputdatalength);
405	else
406		/* Signal pending. */
407		ap_cancel_message(zdev->ap_dev, &ap_msg);
408out_free:
409	kfree(ap_msg.message);
410	return rc;
411}
412
413/**
414 * The crypto operations for a CEX2A card.
415 */
416static struct zcrypt_ops zcrypt_cex2a_ops = {
417	.rsa_modexpo = zcrypt_cex2a_modexpo,
418	.rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
419};
420
421/**
422 * Probe function for CEX2A cards. It always accepts the AP device
423 * since the bus_match already checked the hardware type.
424 * @ap_dev: pointer to the AP device.
425 */
426static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
427{
428	struct zcrypt_device *zdev = NULL;
429	int rc = 0;
430
431	switch (ap_dev->device_type) {
432	case AP_DEVICE_TYPE_CEX2A:
433		zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
434		if (!zdev)
435			return -ENOMEM;
436		zdev->user_space_type = ZCRYPT_CEX2A;
437		zdev->type_string = "CEX2A";
438		zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
439		zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
440		zdev->short_crt = 1;
441		zdev->speed_rating = CEX2A_SPEED_RATING;
442		zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
443		break;
444	case AP_DEVICE_TYPE_CEX3A:
445		zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE);
446		if (!zdev)
447			return -ENOMEM;
448		zdev->user_space_type = ZCRYPT_CEX3A;
449		zdev->type_string = "CEX3A";
450		zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
451		zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
452		zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
453		if (ap_4096_commands_available(ap_dev->qid)) {
454			zdev->max_mod_size = CEX3A_MAX_MOD_SIZE;
455			zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
456		}
457		zdev->short_crt = 1;
458		zdev->speed_rating = CEX3A_SPEED_RATING;
459		break;
460	}
461	if (zdev != NULL) {
462		zdev->ap_dev = ap_dev;
463		zdev->ops = &zcrypt_cex2a_ops;
464		zdev->online = 1;
465		ap_dev->reply = &zdev->reply;
466		ap_dev->private = zdev;
467		rc = zcrypt_device_register(zdev);
468	}
469	if (rc) {
470		ap_dev->private = NULL;
471		zcrypt_device_free(zdev);
472	}
473	return rc;
474}
475
476/**
477 * This is called to remove the extended CEX2A driver information
478 * if an AP device is removed.
479 */
480static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
481{
482	struct zcrypt_device *zdev = ap_dev->private;
483
484	zcrypt_device_unregister(zdev);
485}
486
487int __init zcrypt_cex2a_init(void)
488{
489	return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
490}
491
492void __exit zcrypt_cex2a_exit(void)
493{
494	ap_driver_unregister(&zcrypt_cex2a_driver);
495}
496
497module_init(zcrypt_cex2a_init);
498module_exit(zcrypt_cex2a_exit);
499