Commit | Line | Data |
---|---|---|
963ed931 MS |
1 | /* |
2 | * linux/drivers/s390/crypto/zcrypt_pcica.c | |
3 | * | |
4 | * zcrypt 2.0.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_pcica.h" | |
39 | ||
40 | #define PCICA_MIN_MOD_SIZE 1 /* 8 bits */ | |
41 | #define PCICA_MAX_MOD_SIZE 256 /* 2048 bits */ | |
42 | ||
43 | #define PCICA_SPEED_RATING 2800 | |
44 | ||
45 | #define PCICA_MAX_MESSAGE_SIZE 0x3a0 /* sizeof(struct type4_lcr) */ | |
46 | #define PCICA_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */ | |
47 | ||
48 | #define PCICA_CLEANUP_TIME (15*HZ) | |
49 | ||
50 | static struct ap_device_id zcrypt_pcica_ids[] = { | |
51 | { AP_DEVICE(AP_DEVICE_TYPE_PCICA) }, | |
52 | { /* end of list */ }, | |
53 | }; | |
54 | ||
55 | #ifndef CONFIG_ZCRYPT_MONOLITHIC | |
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 | #endif | |
62 | ||
63 | static int zcrypt_pcica_probe(struct ap_device *ap_dev); | |
64 | static void zcrypt_pcica_remove(struct ap_device *ap_dev); | |
65 | static void zcrypt_pcica_receive(struct ap_device *, struct ap_message *, | |
66 | struct ap_message *); | |
67 | ||
68 | static struct ap_driver zcrypt_pcica_driver = { | |
69 | .probe = zcrypt_pcica_probe, | |
70 | .remove = zcrypt_pcica_remove, | |
71 | .receive = zcrypt_pcica_receive, | |
72 | .ids = zcrypt_pcica_ids, | |
73 | }; | |
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 | */ | |
84 | static 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 | */ | |
133 | static 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 | */ | |
194 | static inline int convert_type84(struct zcrypt_device *zdev, | |
195 | struct ap_message *reply, | |
196 | char __user *outputdata, | |
197 | unsigned int outputdatalength) | |
198 | { | |
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; | |
205 | return -EAGAIN; /* repeat the request on a different device. */ | |
206 | } | |
207 | BUG_ON(t84h->len > PCICA_MAX_RESPONSE_SIZE); | |
208 | data = reply->message + t84h->len - outputdatalength; | |
209 | if (copy_to_user(outputdata, data, outputdatalength)) | |
210 | return -EFAULT; | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static int convert_response(struct zcrypt_device *zdev, | |
215 | struct ap_message *reply, | |
216 | char __user *outputdata, | |
217 | unsigned int outputdatalength) | |
218 | { | |
219 | /* Response type byte is the second byte in the response. */ | |
220 | switch (((unsigned char *) reply->message)[1]) { | |
221 | case TYPE82_RSP_CODE: | |
222 | case TYPE88_RSP_CODE: | |
223 | return convert_error(zdev, reply); | |
224 | case TYPE84_RSP_CODE: | |
225 | return convert_type84(zdev, reply, | |
226 | outputdata, outputdatalength); | |
227 | default: /* Unknown response type, this should NEVER EVER happen */ | |
228 | PRINTK("Unrecognized Message Header: %08x%08x\n", | |
229 | *(unsigned int *) reply->message, | |
230 | *(unsigned int *) (reply->message+4)); | |
231 | zdev->online = 0; | |
232 | return -EAGAIN; /* repeat the request on a different device. */ | |
233 | } | |
234 | } | |
235 | ||
236 | /** | |
237 | * This function is called from the AP bus code after a crypto request | |
238 | * "msg" has finished with the reply message "reply". | |
239 | * It is called from tasklet context. | |
240 | * @ap_dev: pointer to the AP device | |
241 | * @msg: pointer to the AP message | |
242 | * @reply: pointer to the AP reply message | |
243 | */ | |
244 | static void zcrypt_pcica_receive(struct ap_device *ap_dev, | |
245 | struct ap_message *msg, | |
246 | struct ap_message *reply) | |
247 | { | |
248 | static struct error_hdr error_reply = { | |
249 | .type = TYPE82_RSP_CODE, | |
250 | .reply_code = REP82_ERROR_MACHINE_FAILURE, | |
251 | }; | |
252 | struct type84_hdr *t84h = reply->message; | |
253 | int length; | |
254 | ||
255 | /* Copy the reply message to the request message buffer. */ | |
256 | if (IS_ERR(reply)) | |
257 | memcpy(msg->message, &error_reply, sizeof(error_reply)); | |
258 | else if (t84h->code == TYPE84_RSP_CODE) { | |
259 | length = min(PCICA_MAX_RESPONSE_SIZE, (int) t84h->len); | |
260 | memcpy(msg->message, reply->message, length); | |
261 | } else | |
262 | memcpy(msg->message, reply->message, sizeof error_reply); | |
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_msg.message = (void *) kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL); | |
283 | if (!ap_msg.message) | |
284 | return -ENOMEM; | |
285 | ap_msg.psmid = (((unsigned long long) current->pid) << 32) + | |
286 | atomic_inc_return(&zcrypt_step); | |
287 | ap_msg.private = &work; | |
288 | rc = ICAMEX_msg_to_type4MEX_msg(zdev, &ap_msg, mex); | |
289 | if (rc) | |
290 | goto out_free; | |
291 | init_completion(&work); | |
292 | ap_queue_message(zdev->ap_dev, &ap_msg); | |
293 | rc = wait_for_completion_interruptible_timeout( | |
294 | &work, PCICA_CLEANUP_TIME); | |
295 | if (rc > 0) | |
296 | rc = convert_response(zdev, &ap_msg, mex->outputdata, | |
297 | mex->outputdatalength); | |
298 | else { | |
299 | /* Signal pending or message timed out. */ | |
300 | ap_cancel_message(zdev->ap_dev, &ap_msg); | |
301 | if (rc == 0) | |
302 | /* Message timed out. */ | |
303 | rc = -ETIME; | |
304 | } | |
305 | out_free: | |
306 | kfree(ap_msg.message); | |
307 | return rc; | |
308 | } | |
309 | ||
310 | /** | |
311 | * The request distributor calls this function if it picked the PCICA | |
312 | * device to handle a modexpo_crt request. | |
313 | * @zdev: pointer to zcrypt_device structure that identifies the | |
314 | * PCICA device to the request distributor | |
315 | * @crt: pointer to the modexpoc_crt request buffer | |
316 | */ | |
317 | static long zcrypt_pcica_modexpo_crt(struct zcrypt_device *zdev, | |
318 | struct ica_rsa_modexpo_crt *crt) | |
319 | { | |
320 | struct ap_message ap_msg; | |
321 | struct completion work; | |
322 | int rc; | |
323 | ||
324 | ap_msg.message = (void *) kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL); | |
325 | if (!ap_msg.message) | |
326 | return -ENOMEM; | |
327 | ap_msg.psmid = (((unsigned long long) current->pid) << 32) + | |
328 | atomic_inc_return(&zcrypt_step); | |
329 | ap_msg.private = &work; | |
330 | rc = ICACRT_msg_to_type4CRT_msg(zdev, &ap_msg, crt); | |
331 | if (rc) | |
332 | goto out_free; | |
333 | init_completion(&work); | |
334 | ap_queue_message(zdev->ap_dev, &ap_msg); | |
335 | rc = wait_for_completion_interruptible_timeout( | |
336 | &work, PCICA_CLEANUP_TIME); | |
337 | if (rc > 0) | |
338 | rc = convert_response(zdev, &ap_msg, crt->outputdata, | |
339 | crt->outputdatalength); | |
340 | else { | |
341 | /* Signal pending or message timed out. */ | |
342 | ap_cancel_message(zdev->ap_dev, &ap_msg); | |
343 | if (rc == 0) | |
344 | /* Message timed out. */ | |
345 | rc = -ETIME; | |
346 | } | |
347 | out_free: | |
348 | kfree(ap_msg.message); | |
349 | return rc; | |
350 | } | |
351 | ||
352 | /** | |
353 | * The crypto operations for a PCICA card. | |
354 | */ | |
355 | static struct zcrypt_ops zcrypt_pcica_ops = { | |
356 | .rsa_modexpo = zcrypt_pcica_modexpo, | |
357 | .rsa_modexpo_crt = zcrypt_pcica_modexpo_crt, | |
358 | }; | |
359 | ||
360 | /** | |
361 | * Probe function for PCICA cards. It always accepts the AP device | |
362 | * since the bus_match already checked the hardware type. | |
363 | * @ap_dev: pointer to the AP device. | |
364 | */ | |
365 | static int zcrypt_pcica_probe(struct ap_device *ap_dev) | |
366 | { | |
367 | struct zcrypt_device *zdev; | |
368 | int rc; | |
369 | ||
370 | zdev = zcrypt_device_alloc(PCICA_MAX_RESPONSE_SIZE); | |
371 | if (!zdev) | |
372 | return -ENOMEM; | |
373 | zdev->ap_dev = ap_dev; | |
374 | zdev->ops = &zcrypt_pcica_ops; | |
375 | zdev->online = 1; | |
376 | zdev->user_space_type = ZCRYPT_PCICA; | |
377 | zdev->type_string = "PCICA"; | |
378 | zdev->min_mod_size = PCICA_MIN_MOD_SIZE; | |
379 | zdev->max_mod_size = PCICA_MAX_MOD_SIZE; | |
380 | zdev->speed_rating = PCICA_SPEED_RATING; | |
381 | ap_dev->reply = &zdev->reply; | |
382 | ap_dev->private = zdev; | |
383 | rc = zcrypt_device_register(zdev); | |
384 | if (rc) | |
385 | goto out_free; | |
386 | return 0; | |
387 | ||
388 | out_free: | |
389 | ap_dev->private = NULL; | |
390 | zcrypt_device_free(zdev); | |
391 | return rc; | |
392 | } | |
393 | ||
394 | /** | |
395 | * This is called to remove the extended PCICA driver information | |
396 | * if an AP device is removed. | |
397 | */ | |
398 | static void zcrypt_pcica_remove(struct ap_device *ap_dev) | |
399 | { | |
400 | struct zcrypt_device *zdev = ap_dev->private; | |
401 | ||
402 | zcrypt_device_unregister(zdev); | |
403 | } | |
404 | ||
405 | int __init zcrypt_pcica_init(void) | |
406 | { | |
407 | return ap_driver_register(&zcrypt_pcica_driver, THIS_MODULE, "pcica"); | |
408 | } | |
409 | ||
410 | void zcrypt_pcica_exit(void) | |
411 | { | |
412 | ap_driver_unregister(&zcrypt_pcica_driver); | |
413 | } | |
414 | ||
415 | #ifndef CONFIG_ZCRYPT_MONOLITHIC | |
416 | module_init(zcrypt_pcica_init); | |
417 | module_exit(zcrypt_pcica_exit); | |
418 | #endif |