blob: 7a743f4c646c0e46dadcaed195acc9bccf1bf183 [file] [log] [blame]
Martin Schwidefsky963ed932006-09-20 15:58:29 +02001/*
Ralph Wuerthner54321142006-09-20 15:58:36 +02002 * zcrypt 2.1.0
Martin Schwidefsky963ed932006-09-20 15:58:29 +02003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2001, 2006
Martin Schwidefsky963ed932006-09-20 15:58:29 +02005 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
7 *
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Ralph Wuerthner <rwuerthn@de.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +010027#define KMSG_COMPONENT "zcrypt"
28#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29
Martin Schwidefsky963ed932006-09-20 15:58:29 +020030#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Martin Schwidefsky963ed932006-09-20 15:58:29 +020032#include <linux/init.h>
33#include <linux/err.h>
Arun Sharma600634972011-07-26 16:09:06 -070034#include <linux/atomic.h>
Martin Schwidefsky963ed932006-09-20 15:58:29 +020035#include <asm/uaccess.h>
36
37#include "ap_bus.h"
38#include "zcrypt_api.h"
39#include "zcrypt_error.h"
40#include "zcrypt_pcica.h"
41
42#define PCICA_MIN_MOD_SIZE 1 /* 8 bits */
43#define PCICA_MAX_MOD_SIZE 256 /* 2048 bits */
44
45#define PCICA_SPEED_RATING 2800
46
47#define PCICA_MAX_MESSAGE_SIZE 0x3a0 /* sizeof(struct type4_lcr) */
48#define PCICA_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
49
50#define PCICA_CLEANUP_TIME (15*HZ)
51
52static struct ap_device_id zcrypt_pcica_ids[] = {
53 { AP_DEVICE(AP_DEVICE_TYPE_PCICA) },
54 { /* end of list */ },
55};
56
Martin Schwidefsky963ed932006-09-20 15:58:29 +020057MODULE_DEVICE_TABLE(ap, zcrypt_pcica_ids);
58MODULE_AUTHOR("IBM Corporation");
59MODULE_DESCRIPTION("PCICA Cryptographic Coprocessor device driver, "
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020060 "Copyright IBM Corp. 2001, 2006");
Martin Schwidefsky963ed932006-09-20 15:58:29 +020061MODULE_LICENSE("GPL");
Martin Schwidefsky963ed932006-09-20 15:58:29 +020062
63static int zcrypt_pcica_probe(struct ap_device *ap_dev);
64static void zcrypt_pcica_remove(struct ap_device *ap_dev);
65static void zcrypt_pcica_receive(struct ap_device *, struct ap_message *,
66 struct ap_message *);
67
68static struct ap_driver zcrypt_pcica_driver = {
69 .probe = zcrypt_pcica_probe,
70 .remove = zcrypt_pcica_remove,
Martin Schwidefsky963ed932006-09-20 15:58:29 +020071 .ids = zcrypt_pcica_ids,
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +020072 .request_timeout = PCICA_CLEANUP_TIME,
Martin Schwidefsky963ed932006-09-20 15:58:29 +020073};
74
75/**
76 * Convert a ICAMEX message to a type4 MEX message.
77 *
78 * @zdev: crypto device pointer
79 * @zreq: crypto request pointer
80 * @mex: pointer to user input data
81 *
82 * Returns 0 on success or -EFAULT.
83 */
84static int ICAMEX_msg_to_type4MEX_msg(struct zcrypt_device *zdev,
85 struct ap_message *ap_msg,
86 struct ica_rsa_modexpo *mex)
87{
88 unsigned char *modulus, *exponent, *message;
89 int mod_len;
90
91 mod_len = mex->inputdatalength;
92
93 if (mod_len <= 128) {
94 struct type4_sme *sme = ap_msg->message;
95 memset(sme, 0, sizeof(*sme));
96 ap_msg->length = sizeof(*sme);
97 sme->header.msg_fmt = TYPE4_SME_FMT;
98 sme->header.msg_len = sizeof(*sme);
99 sme->header.msg_type_code = TYPE4_TYPE_CODE;
100 sme->header.request_code = TYPE4_REQU_CODE;
101 modulus = sme->modulus + sizeof(sme->modulus) - mod_len;
102 exponent = sme->exponent + sizeof(sme->exponent) - mod_len;
103 message = sme->message + sizeof(sme->message) - mod_len;
104 } else {
105 struct type4_lme *lme = ap_msg->message;
106 memset(lme, 0, sizeof(*lme));
107 ap_msg->length = sizeof(*lme);
108 lme->header.msg_fmt = TYPE4_LME_FMT;
109 lme->header.msg_len = sizeof(*lme);
110 lme->header.msg_type_code = TYPE4_TYPE_CODE;
111 lme->header.request_code = TYPE4_REQU_CODE;
112 modulus = lme->modulus + sizeof(lme->modulus) - mod_len;
113 exponent = lme->exponent + sizeof(lme->exponent) - mod_len;
114 message = lme->message + sizeof(lme->message) - mod_len;
115 }
116
117 if (copy_from_user(modulus, mex->n_modulus, mod_len) ||
118 copy_from_user(exponent, mex->b_key, mod_len) ||
119 copy_from_user(message, mex->inputdata, mod_len))
120 return -EFAULT;
121 return 0;
122}
123
124/**
125 * Convert a ICACRT message to a type4 CRT message.
126 *
127 * @zdev: crypto device pointer
128 * @zreq: crypto request pointer
129 * @crt: pointer to user input data
130 *
131 * Returns 0 on success or -EFAULT.
132 */
133static int ICACRT_msg_to_type4CRT_msg(struct zcrypt_device *zdev,
134 struct ap_message *ap_msg,
135 struct ica_rsa_modexpo_crt *crt)
136{
137 unsigned char *p, *q, *dp, *dq, *u, *inp;
138 int mod_len, short_len, long_len;
139
140 mod_len = crt->inputdatalength;
141 short_len = mod_len / 2;
142 long_len = mod_len / 2 + 8;
143
144 if (mod_len <= 128) {
145 struct type4_scr *scr = ap_msg->message;
146 memset(scr, 0, sizeof(*scr));
147 ap_msg->length = sizeof(*scr);
148 scr->header.msg_type_code = TYPE4_TYPE_CODE;
149 scr->header.request_code = TYPE4_REQU_CODE;
150 scr->header.msg_fmt = TYPE4_SCR_FMT;
151 scr->header.msg_len = sizeof(*scr);
152 p = scr->p + sizeof(scr->p) - long_len;
153 q = scr->q + sizeof(scr->q) - short_len;
154 dp = scr->dp + sizeof(scr->dp) - long_len;
155 dq = scr->dq + sizeof(scr->dq) - short_len;
156 u = scr->u + sizeof(scr->u) - long_len;
157 inp = scr->message + sizeof(scr->message) - mod_len;
158 } else {
159 struct type4_lcr *lcr = ap_msg->message;
160 memset(lcr, 0, sizeof(*lcr));
161 ap_msg->length = sizeof(*lcr);
162 lcr->header.msg_type_code = TYPE4_TYPE_CODE;
163 lcr->header.request_code = TYPE4_REQU_CODE;
164 lcr->header.msg_fmt = TYPE4_LCR_FMT;
165 lcr->header.msg_len = sizeof(*lcr);
166 p = lcr->p + sizeof(lcr->p) - long_len;
167 q = lcr->q + sizeof(lcr->q) - short_len;
168 dp = lcr->dp + sizeof(lcr->dp) - long_len;
169 dq = lcr->dq + sizeof(lcr->dq) - short_len;
170 u = lcr->u + sizeof(lcr->u) - long_len;
171 inp = lcr->message + sizeof(lcr->message) - mod_len;
172 }
173
174 if (copy_from_user(p, crt->np_prime, long_len) ||
175 copy_from_user(q, crt->nq_prime, short_len) ||
176 copy_from_user(dp, crt->bp_key, long_len) ||
177 copy_from_user(dq, crt->bq_key, short_len) ||
178 copy_from_user(u, crt->u_mult_inv, long_len) ||
179 copy_from_user(inp, crt->inputdata, mod_len))
180 return -EFAULT;
181 return 0;
182}
183
184/**
185 * Copy results from a type 84 reply message back to user space.
186 *
187 * @zdev: crypto device pointer
188 * @reply: reply AP message.
189 * @data: pointer to user output data
190 * @length: size of user output data
191 *
192 * Returns 0 on success or -EFAULT.
193 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100194static int convert_type84(struct zcrypt_device *zdev,
195 struct ap_message *reply,
196 char __user *outputdata,
197 unsigned int outputdatalength)
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200198{
199 struct type84_hdr *t84h = reply->message;
200 char *data;
201
202 if (t84h->len < sizeof(*t84h) + outputdatalength) {
203 /* The result is too short, the PCICA card may not do that.. */
204 zdev->online = 0;
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100205 pr_err("Cryptographic device %x failed and was set offline\n",
206 zdev->ap_dev->qid);
207 ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%drc%d",
208 zdev->ap_dev->qid, zdev->online, t84h->code);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200209 return -EAGAIN; /* repeat the request on a different device. */
210 }
211 BUG_ON(t84h->len > PCICA_MAX_RESPONSE_SIZE);
212 data = reply->message + t84h->len - outputdatalength;
213 if (copy_to_user(outputdata, data, outputdatalength))
214 return -EFAULT;
215 return 0;
216}
217
218static int convert_response(struct zcrypt_device *zdev,
219 struct ap_message *reply,
220 char __user *outputdata,
221 unsigned int outputdatalength)
222{
223 /* Response type byte is the second byte in the response. */
224 switch (((unsigned char *) reply->message)[1]) {
225 case TYPE82_RSP_CODE:
226 case TYPE88_RSP_CODE:
227 return convert_error(zdev, reply);
228 case TYPE84_RSP_CODE:
229 return convert_type84(zdev, reply,
230 outputdata, outputdatalength);
231 default: /* Unknown response type, this should NEVER EVER happen */
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200232 zdev->online = 0;
Ingo Tuchscherer91f3e3e2013-11-20 10:47:13 +0100233 pr_err("Cryptographic device %x failed and was set offline\n",
234 zdev->ap_dev->qid);
235 ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%dfail",
236 zdev->ap_dev->qid, zdev->online);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200237 return -EAGAIN; /* repeat the request on a different device. */
238 }
239}
240
241/**
242 * This function is called from the AP bus code after a crypto request
243 * "msg" has finished with the reply message "reply".
244 * It is called from tasklet context.
245 * @ap_dev: pointer to the AP device
246 * @msg: pointer to the AP message
247 * @reply: pointer to the AP reply message
248 */
249static void zcrypt_pcica_receive(struct ap_device *ap_dev,
250 struct ap_message *msg,
251 struct ap_message *reply)
252{
253 static struct error_hdr error_reply = {
254 .type = TYPE82_RSP_CODE,
255 .reply_code = REP82_ERROR_MACHINE_FAILURE,
256 };
Julia Lawall21e7b2c2008-12-25 13:39:28 +0100257 struct type84_hdr *t84h;
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200258 int length;
259
260 /* Copy the reply message to the request message buffer. */
Julia Lawall21e7b2c2008-12-25 13:39:28 +0100261 if (IS_ERR(reply)) {
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200262 memcpy(msg->message, &error_reply, sizeof(error_reply));
Julia Lawall21e7b2c2008-12-25 13:39:28 +0100263 goto out;
264 }
265 t84h = reply->message;
266 if (t84h->code == TYPE84_RSP_CODE) {
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200267 length = min(PCICA_MAX_RESPONSE_SIZE, (int) t84h->len);
268 memcpy(msg->message, reply->message, length);
269 } else
270 memcpy(msg->message, reply->message, sizeof error_reply);
Julia Lawall21e7b2c2008-12-25 13:39:28 +0100271out:
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200272 complete((struct completion *) msg->private);
273}
274
275static atomic_t zcrypt_step = ATOMIC_INIT(0);
276
277/**
278 * The request distributor calls this function if it picked the PCICA
279 * device to handle a modexpo request.
280 * @zdev: pointer to zcrypt_device structure that identifies the
281 * PCICA device to the request distributor
282 * @mex: pointer to the modexpo request buffer
283 */
284static long zcrypt_pcica_modexpo(struct zcrypt_device *zdev,
285 struct ica_rsa_modexpo *mex)
286{
287 struct ap_message ap_msg;
288 struct completion work;
289 int rc;
290
Felix Beck468ffdd2009-12-07 12:51:54 +0100291 ap_init_message(&ap_msg);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800292 ap_msg.message = kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200293 if (!ap_msg.message)
294 return -ENOMEM;
Holger Dengler54a8f562012-05-16 14:08:22 +0200295 ap_msg.receive = zcrypt_pcica_receive;
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200296 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
297 atomic_inc_return(&zcrypt_step);
298 ap_msg.private = &work;
299 rc = ICAMEX_msg_to_type4MEX_msg(zdev, &ap_msg, mex);
300 if (rc)
301 goto out_free;
302 init_completion(&work);
303 ap_queue_message(zdev->ap_dev, &ap_msg);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200304 rc = wait_for_completion_interruptible(&work);
305 if (rc == 0)
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200306 rc = convert_response(zdev, &ap_msg, mex->outputdata,
307 mex->outputdatalength);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200308 else
309 /* Signal pending. */
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200310 ap_cancel_message(zdev->ap_dev, &ap_msg);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200311out_free:
312 kfree(ap_msg.message);
313 return rc;
314}
315
316/**
317 * The request distributor calls this function if it picked the PCICA
318 * device to handle a modexpo_crt request.
319 * @zdev: pointer to zcrypt_device structure that identifies the
320 * PCICA device to the request distributor
321 * @crt: pointer to the modexpoc_crt request buffer
322 */
323static long zcrypt_pcica_modexpo_crt(struct zcrypt_device *zdev,
324 struct ica_rsa_modexpo_crt *crt)
325{
326 struct ap_message ap_msg;
327 struct completion work;
328 int rc;
329
Felix Beck468ffdd2009-12-07 12:51:54 +0100330 ap_init_message(&ap_msg);
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800331 ap_msg.message = kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200332 if (!ap_msg.message)
333 return -ENOMEM;
Holger Dengler54a8f562012-05-16 14:08:22 +0200334 ap_msg.receive = zcrypt_pcica_receive;
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200335 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
336 atomic_inc_return(&zcrypt_step);
337 ap_msg.private = &work;
338 rc = ICACRT_msg_to_type4CRT_msg(zdev, &ap_msg, crt);
339 if (rc)
340 goto out_free;
341 init_completion(&work);
342 ap_queue_message(zdev->ap_dev, &ap_msg);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200343 rc = wait_for_completion_interruptible(&work);
344 if (rc == 0)
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200345 rc = convert_response(zdev, &ap_msg, crt->outputdata,
346 crt->outputdatalength);
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +0200347 else
348 /* Signal pending. */
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200349 ap_cancel_message(zdev->ap_dev, &ap_msg);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200350out_free:
351 kfree(ap_msg.message);
352 return rc;
353}
354
355/**
356 * The crypto operations for a PCICA card.
357 */
358static struct zcrypt_ops zcrypt_pcica_ops = {
359 .rsa_modexpo = zcrypt_pcica_modexpo,
360 .rsa_modexpo_crt = zcrypt_pcica_modexpo_crt,
361};
362
363/**
364 * Probe function for PCICA cards. It always accepts the AP device
365 * since the bus_match already checked the hardware type.
366 * @ap_dev: pointer to the AP device.
367 */
368static int zcrypt_pcica_probe(struct ap_device *ap_dev)
369{
370 struct zcrypt_device *zdev;
371 int rc;
372
373 zdev = zcrypt_device_alloc(PCICA_MAX_RESPONSE_SIZE);
374 if (!zdev)
375 return -ENOMEM;
376 zdev->ap_dev = ap_dev;
377 zdev->ops = &zcrypt_pcica_ops;
378 zdev->online = 1;
379 zdev->user_space_type = ZCRYPT_PCICA;
380 zdev->type_string = "PCICA";
381 zdev->min_mod_size = PCICA_MIN_MOD_SIZE;
382 zdev->max_mod_size = PCICA_MAX_MOD_SIZE;
383 zdev->speed_rating = PCICA_SPEED_RATING;
Felix Beckc2567f82011-01-05 12:47:47 +0100384 zdev->max_exp_bit_length = PCICA_MAX_MOD_SIZE;
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200385 ap_dev->reply = &zdev->reply;
386 ap_dev->private = zdev;
387 rc = zcrypt_device_register(zdev);
388 if (rc)
389 goto out_free;
390 return 0;
391
392out_free:
393 ap_dev->private = NULL;
394 zcrypt_device_free(zdev);
395 return rc;
396}
397
398/**
399 * This is called to remove the extended PCICA driver information
400 * if an AP device is removed.
401 */
402static void zcrypt_pcica_remove(struct ap_device *ap_dev)
403{
404 struct zcrypt_device *zdev = ap_dev->private;
405
406 zcrypt_device_unregister(zdev);
407}
408
409int __init zcrypt_pcica_init(void)
410{
411 return ap_driver_register(&zcrypt_pcica_driver, THIS_MODULE, "pcica");
412}
413
414void zcrypt_pcica_exit(void)
415{
416 ap_driver_unregister(&zcrypt_pcica_driver);
417}
418
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200419module_init(zcrypt_pcica_init);
420module_exit(zcrypt_pcica_exit);