]>
Commit | Line | Data |
---|---|---|
963ed931 MS |
1 | /* |
2 | * linux/drivers/s390/crypto/zcrypt_cex2a.c | |
3 | * | |
5432114b | 4 | * zcrypt 2.1.0 |
963ed931 MS |
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) }, | |
d8c33d32 | 52 | { AP_DEVICE(AP_DEVICE_TYPE_CEX2A2) }, |
963ed931 MS |
53 | { /* end of list */ }, |
54 | }; | |
55 | ||
56 | #ifndef CONFIG_ZCRYPT_MONOLITHIC | |
57 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids); | |
58 | MODULE_AUTHOR("IBM Corporation"); | |
59 | MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " | |
60 | "Copyright 2001, 2006 IBM Corporation"); | |
61 | MODULE_LICENSE("GPL"); | |
62 | #endif | |
63 | ||
64 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev); | |
65 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev); | |
66 | static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *, | |
67 | struct ap_message *); | |
68 | ||
69 | static struct ap_driver zcrypt_cex2a_driver = { | |
70 | .probe = zcrypt_cex2a_probe, | |
71 | .remove = zcrypt_cex2a_remove, | |
72 | .receive = zcrypt_cex2a_receive, | |
73 | .ids = zcrypt_cex2a_ids, | |
af512ed0 | 74 | .request_timeout = CEX2A_CLEANUP_TIME, |
963ed931 MS |
75 | }; |
76 | ||
77 | /** | |
78 | * Convert a ICAMEX message to a type50 MEX message. | |
79 | * | |
80 | * @zdev: crypto device pointer | |
81 | * @zreq: crypto request pointer | |
82 | * @mex: pointer to user input data | |
83 | * | |
84 | * Returns 0 on success or -EFAULT. | |
85 | */ | |
86 | static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev, | |
87 | struct ap_message *ap_msg, | |
88 | struct ica_rsa_modexpo *mex) | |
89 | { | |
90 | unsigned char *mod, *exp, *inp; | |
91 | int mod_len; | |
92 | ||
93 | mod_len = mex->inputdatalength; | |
94 | ||
95 | if (mod_len <= 128) { | |
96 | struct type50_meb1_msg *meb1 = ap_msg->message; | |
97 | memset(meb1, 0, sizeof(*meb1)); | |
98 | ap_msg->length = sizeof(*meb1); | |
99 | meb1->header.msg_type_code = TYPE50_TYPE_CODE; | |
100 | meb1->header.msg_len = sizeof(*meb1); | |
101 | meb1->keyblock_type = TYPE50_MEB1_FMT; | |
102 | mod = meb1->modulus + sizeof(meb1->modulus) - mod_len; | |
103 | exp = meb1->exponent + sizeof(meb1->exponent) - mod_len; | |
104 | inp = meb1->message + sizeof(meb1->message) - mod_len; | |
105 | } else { | |
106 | struct type50_meb2_msg *meb2 = ap_msg->message; | |
107 | memset(meb2, 0, sizeof(*meb2)); | |
108 | ap_msg->length = sizeof(*meb2); | |
109 | meb2->header.msg_type_code = TYPE50_TYPE_CODE; | |
110 | meb2->header.msg_len = sizeof(*meb2); | |
111 | meb2->keyblock_type = TYPE50_MEB2_FMT; | |
112 | mod = meb2->modulus + sizeof(meb2->modulus) - mod_len; | |
113 | exp = meb2->exponent + sizeof(meb2->exponent) - mod_len; | |
114 | inp = meb2->message + sizeof(meb2->message) - mod_len; | |
115 | } | |
116 | ||
117 | if (copy_from_user(mod, mex->n_modulus, mod_len) || | |
118 | copy_from_user(exp, mex->b_key, mod_len) || | |
119 | copy_from_user(inp, mex->inputdata, mod_len)) | |
120 | return -EFAULT; | |
121 | return 0; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Convert a ICACRT message to a type50 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_type50CRT_msg(struct zcrypt_device *zdev, | |
134 | struct ap_message *ap_msg, | |
135 | struct ica_rsa_modexpo_crt *crt) | |
136 | { | |
137 | int mod_len, short_len, long_len, long_offset; | |
138 | unsigned char *p, *q, *dp, *dq, *u, *inp; | |
139 | ||
140 | mod_len = crt->inputdatalength; | |
141 | short_len = mod_len / 2; | |
142 | long_len = mod_len / 2 + 8; | |
143 | ||
144 | /* | |
145 | * CEX2A cannot handle p, dp, or U > 128 bytes. | |
146 | * If we have one of these, we need to do extra checking. | |
147 | */ | |
148 | if (long_len > 128) { | |
149 | /* | |
150 | * zcrypt_rsa_crt already checked for the leading | |
151 | * zeroes of np_prime, bp_key and u_mult_inc. | |
152 | */ | |
153 | long_offset = long_len - 128; | |
154 | long_len = 128; | |
155 | } else | |
156 | long_offset = 0; | |
157 | ||
158 | /* | |
159 | * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use | |
160 | * the larger message structure. | |
161 | */ | |
162 | if (long_len <= 64) { | |
163 | struct type50_crb1_msg *crb1 = ap_msg->message; | |
164 | memset(crb1, 0, sizeof(*crb1)); | |
165 | ap_msg->length = sizeof(*crb1); | |
166 | crb1->header.msg_type_code = TYPE50_TYPE_CODE; | |
167 | crb1->header.msg_len = sizeof(*crb1); | |
168 | crb1->keyblock_type = TYPE50_CRB1_FMT; | |
169 | p = crb1->p + sizeof(crb1->p) - long_len; | |
170 | q = crb1->q + sizeof(crb1->q) - short_len; | |
171 | dp = crb1->dp + sizeof(crb1->dp) - long_len; | |
172 | dq = crb1->dq + sizeof(crb1->dq) - short_len; | |
173 | u = crb1->u + sizeof(crb1->u) - long_len; | |
174 | inp = crb1->message + sizeof(crb1->message) - mod_len; | |
175 | } else { | |
176 | struct type50_crb2_msg *crb2 = ap_msg->message; | |
177 | memset(crb2, 0, sizeof(*crb2)); | |
178 | ap_msg->length = sizeof(*crb2); | |
179 | crb2->header.msg_type_code = TYPE50_TYPE_CODE; | |
180 | crb2->header.msg_len = sizeof(*crb2); | |
181 | crb2->keyblock_type = TYPE50_CRB2_FMT; | |
182 | p = crb2->p + sizeof(crb2->p) - long_len; | |
183 | q = crb2->q + sizeof(crb2->q) - short_len; | |
184 | dp = crb2->dp + sizeof(crb2->dp) - long_len; | |
185 | dq = crb2->dq + sizeof(crb2->dq) - short_len; | |
186 | u = crb2->u + sizeof(crb2->u) - long_len; | |
187 | inp = crb2->message + sizeof(crb2->message) - mod_len; | |
188 | } | |
189 | ||
190 | if (copy_from_user(p, crt->np_prime + long_offset, long_len) || | |
191 | copy_from_user(q, crt->nq_prime, short_len) || | |
192 | copy_from_user(dp, crt->bp_key + long_offset, long_len) || | |
193 | copy_from_user(dq, crt->bq_key, short_len) || | |
194 | copy_from_user(u, crt->u_mult_inv + long_offset, long_len) || | |
195 | copy_from_user(inp, crt->inputdata, mod_len)) | |
196 | return -EFAULT; | |
197 | ||
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
202 | /** | |
203 | * Copy results from a type 80 reply message back to user space. | |
204 | * | |
205 | * @zdev: crypto device pointer | |
206 | * @reply: reply AP message. | |
207 | * @data: pointer to user output data | |
208 | * @length: size of user output data | |
209 | * | |
210 | * Returns 0 on success or -EFAULT. | |
211 | */ | |
212 | static int convert_type80(struct zcrypt_device *zdev, | |
213 | struct ap_message *reply, | |
214 | char __user *outputdata, | |
215 | unsigned int outputdatalength) | |
216 | { | |
217 | struct type80_hdr *t80h = reply->message; | |
218 | unsigned char *data; | |
219 | ||
220 | if (t80h->len < sizeof(*t80h) + outputdatalength) { | |
221 | /* The result is too short, the CEX2A card may not do that.. */ | |
222 | zdev->online = 0; | |
223 | return -EAGAIN; /* repeat the request on a different device. */ | |
224 | } | |
225 | BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE); | |
226 | data = reply->message + t80h->len - outputdatalength; | |
227 | if (copy_to_user(outputdata, data, outputdatalength)) | |
228 | return -EFAULT; | |
229 | return 0; | |
230 | } | |
231 | ||
232 | static int convert_response(struct zcrypt_device *zdev, | |
233 | struct ap_message *reply, | |
234 | char __user *outputdata, | |
235 | unsigned int outputdatalength) | |
236 | { | |
237 | /* Response type byte is the second byte in the response. */ | |
238 | switch (((unsigned char *) reply->message)[1]) { | |
239 | case TYPE82_RSP_CODE: | |
240 | case TYPE88_RSP_CODE: | |
241 | return convert_error(zdev, reply); | |
242 | case TYPE80_RSP_CODE: | |
243 | return convert_type80(zdev, reply, | |
244 | outputdata, outputdatalength); | |
245 | default: /* Unknown response type, this should NEVER EVER happen */ | |
963ed931 MS |
246 | zdev->online = 0; |
247 | return -EAGAIN; /* repeat the request on a different device. */ | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
252 | * This function is called from the AP bus code after a crypto request | |
253 | * "msg" has finished with the reply message "reply". | |
254 | * It is called from tasklet context. | |
255 | * @ap_dev: pointer to the AP device | |
256 | * @msg: pointer to the AP message | |
257 | * @reply: pointer to the AP reply message | |
258 | */ | |
259 | static void zcrypt_cex2a_receive(struct ap_device *ap_dev, | |
260 | struct ap_message *msg, | |
261 | struct ap_message *reply) | |
262 | { | |
263 | static struct error_hdr error_reply = { | |
264 | .type = TYPE82_RSP_CODE, | |
265 | .reply_code = REP82_ERROR_MACHINE_FAILURE, | |
266 | }; | |
267 | struct type80_hdr *t80h = reply->message; | |
268 | int length; | |
269 | ||
270 | /* Copy the reply message to the request message buffer. */ | |
271 | if (IS_ERR(reply)) | |
272 | memcpy(msg->message, &error_reply, sizeof(error_reply)); | |
273 | else if (t80h->type == TYPE80_RSP_CODE) { | |
274 | length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len); | |
275 | memcpy(msg->message, reply->message, length); | |
276 | } else | |
277 | memcpy(msg->message, reply->message, sizeof error_reply); | |
278 | complete((struct completion *) msg->private); | |
279 | } | |
280 | ||
281 | static atomic_t zcrypt_step = ATOMIC_INIT(0); | |
282 | ||
283 | /** | |
284 | * The request distributor calls this function if it picked the CEX2A | |
285 | * device to handle a modexpo request. | |
286 | * @zdev: pointer to zcrypt_device structure that identifies the | |
287 | * CEX2A device to the request distributor | |
288 | * @mex: pointer to the modexpo request buffer | |
289 | */ | |
290 | static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev, | |
291 | struct ica_rsa_modexpo *mex) | |
292 | { | |
293 | struct ap_message ap_msg; | |
294 | struct completion work; | |
295 | int rc; | |
296 | ||
5cbded58 | 297 | ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL); |
963ed931 MS |
298 | if (!ap_msg.message) |
299 | return -ENOMEM; | |
300 | ap_msg.psmid = (((unsigned long long) current->pid) << 32) + | |
301 | atomic_inc_return(&zcrypt_step); | |
302 | ap_msg.private = &work; | |
303 | rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex); | |
304 | if (rc) | |
305 | goto out_free; | |
306 | init_completion(&work); | |
307 | ap_queue_message(zdev->ap_dev, &ap_msg); | |
af512ed0 RW |
308 | rc = wait_for_completion_interruptible(&work); |
309 | if (rc == 0) | |
963ed931 MS |
310 | rc = convert_response(zdev, &ap_msg, mex->outputdata, |
311 | mex->outputdatalength); | |
af512ed0 RW |
312 | else |
313 | /* Signal pending. */ | |
963ed931 | 314 | ap_cancel_message(zdev->ap_dev, &ap_msg); |
963ed931 MS |
315 | out_free: |
316 | kfree(ap_msg.message); | |
317 | return rc; | |
318 | } | |
319 | ||
320 | /** | |
321 | * The request distributor calls this function if it picked the CEX2A | |
322 | * device to handle a modexpo_crt request. | |
323 | * @zdev: pointer to zcrypt_device structure that identifies the | |
324 | * CEX2A device to the request distributor | |
325 | * @crt: pointer to the modexpoc_crt request buffer | |
326 | */ | |
327 | static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev, | |
328 | struct ica_rsa_modexpo_crt *crt) | |
329 | { | |
330 | struct ap_message ap_msg; | |
331 | struct completion work; | |
332 | int rc; | |
333 | ||
5cbded58 | 334 | ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL); |
963ed931 MS |
335 | if (!ap_msg.message) |
336 | return -ENOMEM; | |
337 | ap_msg.psmid = (((unsigned long long) current->pid) << 32) + | |
338 | atomic_inc_return(&zcrypt_step); | |
339 | ap_msg.private = &work; | |
340 | rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt); | |
341 | if (rc) | |
342 | goto out_free; | |
343 | init_completion(&work); | |
344 | ap_queue_message(zdev->ap_dev, &ap_msg); | |
af512ed0 RW |
345 | rc = wait_for_completion_interruptible(&work); |
346 | if (rc == 0) | |
963ed931 MS |
347 | rc = convert_response(zdev, &ap_msg, crt->outputdata, |
348 | crt->outputdatalength); | |
af512ed0 RW |
349 | else |
350 | /* Signal pending. */ | |
963ed931 | 351 | ap_cancel_message(zdev->ap_dev, &ap_msg); |
963ed931 MS |
352 | out_free: |
353 | kfree(ap_msg.message); | |
354 | return rc; | |
355 | } | |
356 | ||
357 | /** | |
358 | * The crypto operations for a CEX2A card. | |
359 | */ | |
360 | static struct zcrypt_ops zcrypt_cex2a_ops = { | |
361 | .rsa_modexpo = zcrypt_cex2a_modexpo, | |
362 | .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt, | |
363 | }; | |
364 | ||
365 | /** | |
366 | * Probe function for CEX2A cards. It always accepts the AP device | |
367 | * since the bus_match already checked the hardware type. | |
368 | * @ap_dev: pointer to the AP device. | |
369 | */ | |
370 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev) | |
371 | { | |
372 | struct zcrypt_device *zdev; | |
373 | int rc; | |
374 | ||
375 | zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE); | |
376 | if (!zdev) | |
377 | return -ENOMEM; | |
378 | zdev->ap_dev = ap_dev; | |
379 | zdev->ops = &zcrypt_cex2a_ops; | |
380 | zdev->online = 1; | |
381 | zdev->user_space_type = ZCRYPT_CEX2A; | |
382 | zdev->type_string = "CEX2A"; | |
383 | zdev->min_mod_size = CEX2A_MIN_MOD_SIZE; | |
384 | zdev->max_mod_size = CEX2A_MAX_MOD_SIZE; | |
385 | zdev->short_crt = 1; | |
386 | zdev->speed_rating = CEX2A_SPEED_RATING; | |
387 | ap_dev->reply = &zdev->reply; | |
388 | ap_dev->private = zdev; | |
389 | rc = zcrypt_device_register(zdev); | |
390 | if (rc) | |
391 | goto out_free; | |
392 | return 0; | |
393 | ||
394 | out_free: | |
395 | ap_dev->private = NULL; | |
396 | zcrypt_device_free(zdev); | |
397 | return rc; | |
398 | } | |
399 | ||
400 | /** | |
401 | * This is called to remove the extended CEX2A driver information | |
402 | * if an AP device is removed. | |
403 | */ | |
404 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev) | |
405 | { | |
406 | struct zcrypt_device *zdev = ap_dev->private; | |
407 | ||
408 | zcrypt_device_unregister(zdev); | |
409 | } | |
410 | ||
411 | int __init zcrypt_cex2a_init(void) | |
412 | { | |
413 | return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a"); | |
414 | } | |
415 | ||
416 | void __exit zcrypt_cex2a_exit(void) | |
417 | { | |
418 | ap_driver_unregister(&zcrypt_cex2a_driver); | |
419 | } | |
420 | ||
421 | #ifndef CONFIG_ZCRYPT_MONOLITHIC | |
422 | module_init(zcrypt_cex2a_init); | |
423 | module_exit(zcrypt_cex2a_exit); | |
424 | #endif |