]> Pileus Git - ~andy/linux/blob - drivers/s390/crypto/zcrypt_pcica.c
Merge branch 'for-linus' of git://git.open-osd.org/linux-open-osd
[~andy/linux] / drivers / s390 / crypto / zcrypt_pcica.c
1 /*
2  *  linux/drivers/s390/crypto/zcrypt_pcica.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_pcica.h"
40
41 #define PCICA_MIN_MOD_SIZE        1     /*    8 bits    */
42 #define PCICA_MAX_MOD_SIZE      256     /* 2048 bits    */
43
44 #define PCICA_SPEED_RATING      2800
45
46 #define PCICA_MAX_MESSAGE_SIZE  0x3a0   /* sizeof(struct type4_lcr)          */
47 #define PCICA_MAX_RESPONSE_SIZE 0x110   /* max outputdatalength + type80_hdr */
48
49 #define PCICA_CLEANUP_TIME      (15*HZ)
50
51 static struct ap_device_id zcrypt_pcica_ids[] = {
52         { AP_DEVICE(AP_DEVICE_TYPE_PCICA) },
53         { /* end of list */ },
54 };
55
56 MODULE_DEVICE_TABLE(ap, zcrypt_pcica_ids);
57 MODULE_AUTHOR("IBM Corporation");
58 MODULE_DESCRIPTION("PCICA Cryptographic Coprocessor device driver, "
59                    "Copyright 2001, 2006 IBM Corporation");
60 MODULE_LICENSE("GPL");
61
62 static int zcrypt_pcica_probe(struct ap_device *ap_dev);
63 static void zcrypt_pcica_remove(struct ap_device *ap_dev);
64 static void zcrypt_pcica_receive(struct ap_device *, struct ap_message *,
65                                  struct ap_message *);
66
67 static struct ap_driver zcrypt_pcica_driver = {
68         .probe = zcrypt_pcica_probe,
69         .remove = zcrypt_pcica_remove,
70         .ids = zcrypt_pcica_ids,
71         .request_timeout = PCICA_CLEANUP_TIME,
72 };
73
74 /**
75  * Convert a ICAMEX message to a type4 MEX message.
76  *
77  * @zdev: crypto device pointer
78  * @zreq: crypto request pointer
79  * @mex: pointer to user input data
80  *
81  * Returns 0 on success or -EFAULT.
82  */
83 static int ICAMEX_msg_to_type4MEX_msg(struct zcrypt_device *zdev,
84                                       struct ap_message *ap_msg,
85                                       struct ica_rsa_modexpo *mex)
86 {
87         unsigned char *modulus, *exponent, *message;
88         int mod_len;
89
90         mod_len = mex->inputdatalength;
91
92         if (mod_len <= 128) {
93                 struct type4_sme *sme = ap_msg->message;
94                 memset(sme, 0, sizeof(*sme));
95                 ap_msg->length = sizeof(*sme);
96                 sme->header.msg_fmt = TYPE4_SME_FMT;
97                 sme->header.msg_len = sizeof(*sme);
98                 sme->header.msg_type_code = TYPE4_TYPE_CODE;
99                 sme->header.request_code = TYPE4_REQU_CODE;
100                 modulus = sme->modulus + sizeof(sme->modulus) - mod_len;
101                 exponent = sme->exponent + sizeof(sme->exponent) - mod_len;
102                 message = sme->message + sizeof(sme->message) - mod_len;
103         } else {
104                 struct type4_lme *lme = ap_msg->message;
105                 memset(lme, 0, sizeof(*lme));
106                 ap_msg->length = sizeof(*lme);
107                 lme->header.msg_fmt = TYPE4_LME_FMT;
108                 lme->header.msg_len = sizeof(*lme);
109                 lme->header.msg_type_code = TYPE4_TYPE_CODE;
110                 lme->header.request_code = TYPE4_REQU_CODE;
111                 modulus = lme->modulus + sizeof(lme->modulus) - mod_len;
112                 exponent = lme->exponent + sizeof(lme->exponent) - mod_len;
113                 message = lme->message + sizeof(lme->message) - mod_len;
114         }
115
116         if (copy_from_user(modulus, mex->n_modulus, mod_len) ||
117             copy_from_user(exponent, mex->b_key, mod_len) ||
118             copy_from_user(message, mex->inputdata, mod_len))
119                 return -EFAULT;
120         return 0;
121 }
122
123 /**
124  * Convert a ICACRT message to a type4 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_type4CRT_msg(struct zcrypt_device *zdev,
133                                       struct ap_message *ap_msg,
134                                       struct ica_rsa_modexpo_crt *crt)
135 {
136         unsigned char *p, *q, *dp, *dq, *u, *inp;
137         int mod_len, short_len, long_len;
138
139         mod_len = crt->inputdatalength;
140         short_len = mod_len / 2;
141         long_len = mod_len / 2 + 8;
142
143         if (mod_len <= 128) {
144                 struct type4_scr *scr = ap_msg->message;
145                 memset(scr, 0, sizeof(*scr));
146                 ap_msg->length = sizeof(*scr);
147                 scr->header.msg_type_code = TYPE4_TYPE_CODE;
148                 scr->header.request_code = TYPE4_REQU_CODE;
149                 scr->header.msg_fmt = TYPE4_SCR_FMT;
150                 scr->header.msg_len = sizeof(*scr);
151                 p = scr->p + sizeof(scr->p) - long_len;
152                 q = scr->q + sizeof(scr->q) - short_len;
153                 dp = scr->dp + sizeof(scr->dp) - long_len;
154                 dq = scr->dq + sizeof(scr->dq) - short_len;
155                 u = scr->u + sizeof(scr->u) - long_len;
156                 inp = scr->message + sizeof(scr->message) - mod_len;
157         } else {
158                 struct type4_lcr *lcr = ap_msg->message;
159                 memset(lcr, 0, sizeof(*lcr));
160                 ap_msg->length = sizeof(*lcr);
161                 lcr->header.msg_type_code = TYPE4_TYPE_CODE;
162                 lcr->header.request_code = TYPE4_REQU_CODE;
163                 lcr->header.msg_fmt = TYPE4_LCR_FMT;
164                 lcr->header.msg_len = sizeof(*lcr);
165                 p = lcr->p + sizeof(lcr->p) - long_len;
166                 q = lcr->q + sizeof(lcr->q) - short_len;
167                 dp = lcr->dp + sizeof(lcr->dp) - long_len;
168                 dq = lcr->dq + sizeof(lcr->dq) - short_len;
169                 u = lcr->u + sizeof(lcr->u) - long_len;
170                 inp = lcr->message + sizeof(lcr->message) - mod_len;
171         }
172
173         if (copy_from_user(p, crt->np_prime, long_len) ||
174             copy_from_user(q, crt->nq_prime, short_len) ||
175             copy_from_user(dp, crt->bp_key, long_len) ||
176             copy_from_user(dq, crt->bq_key, short_len) ||
177             copy_from_user(u, crt->u_mult_inv, long_len) ||
178             copy_from_user(inp, crt->inputdata, mod_len))
179                 return -EFAULT;
180         return 0;
181 }
182
183 /**
184  * Copy results from a type 84 reply message back to user space.
185  *
186  * @zdev: crypto device pointer
187  * @reply: reply AP message.
188  * @data: pointer to user output data
189  * @length: size of user output data
190  *
191  * Returns 0 on success or -EFAULT.
192  */
193 static int convert_type84(struct zcrypt_device *zdev,
194                           struct ap_message *reply,
195                           char __user *outputdata,
196                           unsigned int outputdatalength)
197 {
198         struct type84_hdr *t84h = reply->message;
199         char *data;
200
201         if (t84h->len < sizeof(*t84h) + outputdatalength) {
202                 /* The result is too short, the PCICA card may not do that.. */
203                 zdev->online = 0;
204                 return -EAGAIN; /* repeat the request on a different device. */
205         }
206         BUG_ON(t84h->len > PCICA_MAX_RESPONSE_SIZE);
207         data = reply->message + t84h->len - outputdatalength;
208         if (copy_to_user(outputdata, data, outputdatalength))
209                 return -EFAULT;
210         return 0;
211 }
212
213 static int convert_response(struct zcrypt_device *zdev,
214                             struct ap_message *reply,
215                             char __user *outputdata,
216                             unsigned int outputdatalength)
217 {
218         /* Response type byte is the second byte in the response. */
219         switch (((unsigned char *) reply->message)[1]) {
220         case TYPE82_RSP_CODE:
221         case TYPE88_RSP_CODE:
222                 return convert_error(zdev, reply);
223         case TYPE84_RSP_CODE:
224                 return convert_type84(zdev, reply,
225                                       outputdata, outputdatalength);
226         default: /* Unknown response type, this should NEVER EVER happen */
227                 zdev->online = 0;
228                 return -EAGAIN; /* repeat the request on a different device. */
229         }
230 }
231
232 /**
233  * This function is called from the AP bus code after a crypto request
234  * "msg" has finished with the reply message "reply".
235  * It is called from tasklet context.
236  * @ap_dev: pointer to the AP device
237  * @msg: pointer to the AP message
238  * @reply: pointer to the AP reply message
239  */
240 static void zcrypt_pcica_receive(struct ap_device *ap_dev,
241                                  struct ap_message *msg,
242                                  struct ap_message *reply)
243 {
244         static struct error_hdr error_reply = {
245                 .type = TYPE82_RSP_CODE,
246                 .reply_code = REP82_ERROR_MACHINE_FAILURE,
247         };
248         struct type84_hdr *t84h;
249         int length;
250
251         /* Copy the reply message to the request message buffer. */
252         if (IS_ERR(reply)) {
253                 memcpy(msg->message, &error_reply, sizeof(error_reply));
254                 goto out;
255         }
256         t84h = reply->message;
257         if (t84h->code == TYPE84_RSP_CODE) {
258                 length = min(PCICA_MAX_RESPONSE_SIZE, (int) t84h->len);
259                 memcpy(msg->message, reply->message, length);
260         } else
261                 memcpy(msg->message, reply->message, sizeof error_reply);
262 out:
263         complete((struct completion *) msg->private);
264 }
265
266 static atomic_t zcrypt_step = ATOMIC_INIT(0);
267
268 /**
269  * The request distributor calls this function if it picked the PCICA
270  * device to handle a modexpo request.
271  * @zdev: pointer to zcrypt_device structure that identifies the
272  *        PCICA device to the request distributor
273  * @mex: pointer to the modexpo request buffer
274  */
275 static long zcrypt_pcica_modexpo(struct zcrypt_device *zdev,
276                                  struct ica_rsa_modexpo *mex)
277 {
278         struct ap_message ap_msg;
279         struct completion work;
280         int rc;
281
282         ap_init_message(&ap_msg);
283         ap_msg.message = kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL);
284         if (!ap_msg.message)
285                 return -ENOMEM;
286         ap_msg.receive = zcrypt_pcica_receive;
287         ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
288                                 atomic_inc_return(&zcrypt_step);
289         ap_msg.private = &work;
290         rc = ICAMEX_msg_to_type4MEX_msg(zdev, &ap_msg, mex);
291         if (rc)
292                 goto out_free;
293         init_completion(&work);
294         ap_queue_message(zdev->ap_dev, &ap_msg);
295         rc = wait_for_completion_interruptible(&work);
296         if (rc == 0)
297                 rc = convert_response(zdev, &ap_msg, mex->outputdata,
298                                       mex->outputdatalength);
299         else
300                 /* Signal pending. */
301                 ap_cancel_message(zdev->ap_dev, &ap_msg);
302 out_free:
303         kfree(ap_msg.message);
304         return rc;
305 }
306
307 /**
308  * The request distributor calls this function if it picked the PCICA
309  * device to handle a modexpo_crt request.
310  * @zdev: pointer to zcrypt_device structure that identifies the
311  *        PCICA device to the request distributor
312  * @crt: pointer to the modexpoc_crt request buffer
313  */
314 static long zcrypt_pcica_modexpo_crt(struct zcrypt_device *zdev,
315                                      struct ica_rsa_modexpo_crt *crt)
316 {
317         struct ap_message ap_msg;
318         struct completion work;
319         int rc;
320
321         ap_init_message(&ap_msg);
322         ap_msg.message = kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL);
323         if (!ap_msg.message)
324                 return -ENOMEM;
325         ap_msg.receive = zcrypt_pcica_receive;
326         ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
327                                 atomic_inc_return(&zcrypt_step);
328         ap_msg.private = &work;
329         rc = ICACRT_msg_to_type4CRT_msg(zdev, &ap_msg, crt);
330         if (rc)
331                 goto out_free;
332         init_completion(&work);
333         ap_queue_message(zdev->ap_dev, &ap_msg);
334         rc = wait_for_completion_interruptible(&work);
335         if (rc == 0)
336                 rc = convert_response(zdev, &ap_msg, crt->outputdata,
337                                       crt->outputdatalength);
338         else
339                 /* Signal pending. */
340                 ap_cancel_message(zdev->ap_dev, &ap_msg);
341 out_free:
342         kfree(ap_msg.message);
343         return rc;
344 }
345
346 /**
347  * The crypto operations for a PCICA card.
348  */
349 static struct zcrypt_ops zcrypt_pcica_ops = {
350         .rsa_modexpo = zcrypt_pcica_modexpo,
351         .rsa_modexpo_crt = zcrypt_pcica_modexpo_crt,
352 };
353
354 /**
355  * Probe function for PCICA cards. It always accepts the AP device
356  * since the bus_match already checked the hardware type.
357  * @ap_dev: pointer to the AP device.
358  */
359 static int zcrypt_pcica_probe(struct ap_device *ap_dev)
360 {
361         struct zcrypt_device *zdev;
362         int rc;
363
364         zdev = zcrypt_device_alloc(PCICA_MAX_RESPONSE_SIZE);
365         if (!zdev)
366                 return -ENOMEM;
367         zdev->ap_dev = ap_dev;
368         zdev->ops = &zcrypt_pcica_ops;
369         zdev->online = 1;
370         zdev->user_space_type = ZCRYPT_PCICA;
371         zdev->type_string = "PCICA";
372         zdev->min_mod_size = PCICA_MIN_MOD_SIZE;
373         zdev->max_mod_size = PCICA_MAX_MOD_SIZE;
374         zdev->speed_rating = PCICA_SPEED_RATING;
375         zdev->max_exp_bit_length = PCICA_MAX_MOD_SIZE;
376         ap_dev->reply = &zdev->reply;
377         ap_dev->private = zdev;
378         rc = zcrypt_device_register(zdev);
379         if (rc)
380                 goto out_free;
381         return 0;
382
383 out_free:
384         ap_dev->private = NULL;
385         zcrypt_device_free(zdev);
386         return rc;
387 }
388
389 /**
390  * This is called to remove the extended PCICA driver information
391  * if an AP device is removed.
392  */
393 static void zcrypt_pcica_remove(struct ap_device *ap_dev)
394 {
395         struct zcrypt_device *zdev = ap_dev->private;
396
397         zcrypt_device_unregister(zdev);
398 }
399
400 int __init zcrypt_pcica_init(void)
401 {
402         return ap_driver_register(&zcrypt_pcica_driver, THIS_MODULE, "pcica");
403 }
404
405 void zcrypt_pcica_exit(void)
406 {
407         ap_driver_unregister(&zcrypt_pcica_driver);
408 }
409
410 module_init(zcrypt_pcica_init);
411 module_exit(zcrypt_pcica_exit);