4 * Copyright IBM Corp. 2001, 2006
5 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
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>
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)
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.
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.
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/err.h>
31 #include <linux/atomic.h>
32 #include <asm/uaccess.h>
35 #include "zcrypt_api.h"
36 #include "zcrypt_error.h"
37 #include "zcrypt_cex2a.h"
39 #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
40 #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
41 #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
42 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
44 #define CEX2A_SPEED_RATING 970
45 #define CEX3A_SPEED_RATING 900 /* Fixme: Needs finetuning */
47 #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
48 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
50 #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
51 * (max outputdatalength) +
53 #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
55 #define CEX2A_CLEANUP_TIME (15*HZ)
56 #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
58 static struct ap_device_id zcrypt_cex2a_ids[] = {
59 { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
60 { AP_DEVICE(AP_DEVICE_TYPE_CEX3A) },
61 { /* end of list */ },
64 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
65 MODULE_AUTHOR("IBM Corporation");
66 MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, "
67 "Copyright IBM Corp. 2001, 2006");
68 MODULE_LICENSE("GPL");
70 static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
71 static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
72 static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
75 static struct ap_driver zcrypt_cex2a_driver = {
76 .probe = zcrypt_cex2a_probe,
77 .remove = zcrypt_cex2a_remove,
78 .ids = zcrypt_cex2a_ids,
79 .request_timeout = CEX2A_CLEANUP_TIME,
83 * Convert a ICAMEX message to a type50 MEX message.
85 * @zdev: crypto device pointer
86 * @zreq: crypto request pointer
87 * @mex: pointer to user input data
89 * Returns 0 on success or -EFAULT.
91 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
92 struct ap_message *ap_msg,
93 struct ica_rsa_modexpo *mex)
95 unsigned char *mod, *exp, *inp;
98 mod_len = mex->inputdatalength;
100 if (mod_len <= 128) {
101 struct type50_meb1_msg *meb1 = ap_msg->message;
102 memset(meb1, 0, sizeof(*meb1));
103 ap_msg->length = sizeof(*meb1);
104 meb1->header.msg_type_code = TYPE50_TYPE_CODE;
105 meb1->header.msg_len = sizeof(*meb1);
106 meb1->keyblock_type = TYPE50_MEB1_FMT;
107 mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
108 exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
109 inp = meb1->message + sizeof(meb1->message) - mod_len;
110 } else if (mod_len <= 256) {
111 struct type50_meb2_msg *meb2 = ap_msg->message;
112 memset(meb2, 0, sizeof(*meb2));
113 ap_msg->length = sizeof(*meb2);
114 meb2->header.msg_type_code = TYPE50_TYPE_CODE;
115 meb2->header.msg_len = sizeof(*meb2);
116 meb2->keyblock_type = TYPE50_MEB2_FMT;
117 mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
118 exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
119 inp = meb2->message + sizeof(meb2->message) - mod_len;
121 /* mod_len > 256 = 4096 bit RSA Key */
122 struct type50_meb3_msg *meb3 = ap_msg->message;
123 memset(meb3, 0, sizeof(*meb3));
124 ap_msg->length = sizeof(*meb3);
125 meb3->header.msg_type_code = TYPE50_TYPE_CODE;
126 meb3->header.msg_len = sizeof(*meb3);
127 meb3->keyblock_type = TYPE50_MEB3_FMT;
128 mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
129 exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
130 inp = meb3->message + sizeof(meb3->message) - mod_len;
133 if (copy_from_user(mod, mex->n_modulus, mod_len) ||
134 copy_from_user(exp, mex->b_key, mod_len) ||
135 copy_from_user(inp, mex->inputdata, mod_len))
141 * Convert a ICACRT message to a type50 CRT message.
143 * @zdev: crypto device pointer
144 * @zreq: crypto request pointer
145 * @crt: pointer to user input data
147 * Returns 0 on success or -EFAULT.
149 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
150 struct ap_message *ap_msg,
151 struct ica_rsa_modexpo_crt *crt)
153 int mod_len, short_len, long_len, long_offset, limit;
154 unsigned char *p, *q, *dp, *dq, *u, *inp;
156 mod_len = crt->inputdatalength;
157 short_len = mod_len / 2;
158 long_len = mod_len / 2 + 8;
161 * CEX2A cannot handle p, dp, or U > 128 bytes.
162 * If we have one of these, we need to do extra checking.
163 * For CEX3A the limit is 256 bytes.
165 if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)
170 if (long_len > limit) {
172 * zcrypt_rsa_crt already checked for the leading
173 * zeroes of np_prime, bp_key and u_mult_inc.
175 long_offset = long_len - limit;
181 * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
182 * the larger message structure.
184 if (long_len <= 64) {
185 struct type50_crb1_msg *crb1 = ap_msg->message;
186 memset(crb1, 0, sizeof(*crb1));
187 ap_msg->length = sizeof(*crb1);
188 crb1->header.msg_type_code = TYPE50_TYPE_CODE;
189 crb1->header.msg_len = sizeof(*crb1);
190 crb1->keyblock_type = TYPE50_CRB1_FMT;
191 p = crb1->p + sizeof(crb1->p) - long_len;
192 q = crb1->q + sizeof(crb1->q) - short_len;
193 dp = crb1->dp + sizeof(crb1->dp) - long_len;
194 dq = crb1->dq + sizeof(crb1->dq) - short_len;
195 u = crb1->u + sizeof(crb1->u) - long_len;
196 inp = crb1->message + sizeof(crb1->message) - mod_len;
197 } else if (long_len <= 128) {
198 struct type50_crb2_msg *crb2 = ap_msg->message;
199 memset(crb2, 0, sizeof(*crb2));
200 ap_msg->length = sizeof(*crb2);
201 crb2->header.msg_type_code = TYPE50_TYPE_CODE;
202 crb2->header.msg_len = sizeof(*crb2);
203 crb2->keyblock_type = TYPE50_CRB2_FMT;
204 p = crb2->p + sizeof(crb2->p) - long_len;
205 q = crb2->q + sizeof(crb2->q) - short_len;
206 dp = crb2->dp + sizeof(crb2->dp) - long_len;
207 dq = crb2->dq + sizeof(crb2->dq) - short_len;
208 u = crb2->u + sizeof(crb2->u) - long_len;
209 inp = crb2->message + sizeof(crb2->message) - mod_len;
211 /* long_len >= 256 */
212 struct type50_crb3_msg *crb3 = ap_msg->message;
213 memset(crb3, 0, sizeof(*crb3));
214 ap_msg->length = sizeof(*crb3);
215 crb3->header.msg_type_code = TYPE50_TYPE_CODE;
216 crb3->header.msg_len = sizeof(*crb3);
217 crb3->keyblock_type = TYPE50_CRB3_FMT;
218 p = crb3->p + sizeof(crb3->p) - long_len;
219 q = crb3->q + sizeof(crb3->q) - short_len;
220 dp = crb3->dp + sizeof(crb3->dp) - long_len;
221 dq = crb3->dq + sizeof(crb3->dq) - short_len;
222 u = crb3->u + sizeof(crb3->u) - long_len;
223 inp = crb3->message + sizeof(crb3->message) - mod_len;
226 if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
227 copy_from_user(q, crt->nq_prime, short_len) ||
228 copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
229 copy_from_user(dq, crt->bq_key, short_len) ||
230 copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
231 copy_from_user(inp, crt->inputdata, mod_len))
238 * Copy results from a type 80 reply message back to user space.
240 * @zdev: crypto device pointer
241 * @reply: reply AP message.
242 * @data: pointer to user output data
243 * @length: size of user output data
245 * Returns 0 on success or -EFAULT.
247 static int convert_type80(struct zcrypt_device *zdev,
248 struct ap_message *reply,
249 char __user *outputdata,
250 unsigned int outputdatalength)
252 struct type80_hdr *t80h = reply->message;
255 if (t80h->len < sizeof(*t80h) + outputdatalength) {
256 /* The result is too short, the CEX2A card may not do that.. */
258 return -EAGAIN; /* repeat the request on a different device. */
260 if (zdev->user_space_type == ZCRYPT_CEX2A)
261 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
263 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
264 data = reply->message + t80h->len - outputdatalength;
265 if (copy_to_user(outputdata, data, outputdatalength))
270 static int convert_response(struct zcrypt_device *zdev,
271 struct ap_message *reply,
272 char __user *outputdata,
273 unsigned int outputdatalength)
275 /* Response type byte is the second byte in the response. */
276 switch (((unsigned char *) reply->message)[1]) {
277 case TYPE82_RSP_CODE:
278 case TYPE88_RSP_CODE:
279 return convert_error(zdev, reply);
280 case TYPE80_RSP_CODE:
281 return convert_type80(zdev, reply,
282 outputdata, outputdatalength);
283 default: /* Unknown response type, this should NEVER EVER happen */
285 return -EAGAIN; /* repeat the request on a different device. */
290 * This function is called from the AP bus code after a crypto request
291 * "msg" has finished with the reply message "reply".
292 * It is called from tasklet context.
293 * @ap_dev: pointer to the AP device
294 * @msg: pointer to the AP message
295 * @reply: pointer to the AP reply message
297 static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
298 struct ap_message *msg,
299 struct ap_message *reply)
301 static struct error_hdr error_reply = {
302 .type = TYPE82_RSP_CODE,
303 .reply_code = REP82_ERROR_MACHINE_FAILURE,
305 struct type80_hdr *t80h;
308 /* Copy the reply message to the request message buffer. */
310 memcpy(msg->message, &error_reply, sizeof(error_reply));
313 t80h = reply->message;
314 if (t80h->type == TYPE80_RSP_CODE) {
315 if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
316 length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len);
318 length = min(CEX3A_MAX_RESPONSE_SIZE, (int) t80h->len);
319 memcpy(msg->message, reply->message, length);
321 memcpy(msg->message, reply->message, sizeof error_reply);
323 complete((struct completion *) msg->private);
326 static atomic_t zcrypt_step = ATOMIC_INIT(0);
329 * The request distributor calls this function if it picked the CEX2A
330 * device to handle a modexpo request.
331 * @zdev: pointer to zcrypt_device structure that identifies the
332 * CEX2A device to the request distributor
333 * @mex: pointer to the modexpo request buffer
335 static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
336 struct ica_rsa_modexpo *mex)
338 struct ap_message ap_msg;
339 struct completion work;
342 ap_init_message(&ap_msg);
343 if (zdev->user_space_type == ZCRYPT_CEX2A)
344 ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
346 ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL);
349 ap_msg.receive = zcrypt_cex2a_receive;
350 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
351 atomic_inc_return(&zcrypt_step);
352 ap_msg.private = &work;
353 rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
356 init_completion(&work);
357 ap_queue_message(zdev->ap_dev, &ap_msg);
358 rc = wait_for_completion_interruptible(&work);
360 rc = convert_response(zdev, &ap_msg, mex->outputdata,
361 mex->outputdatalength);
363 /* Signal pending. */
364 ap_cancel_message(zdev->ap_dev, &ap_msg);
366 kfree(ap_msg.message);
371 * The request distributor calls this function if it picked the CEX2A
372 * device to handle a modexpo_crt request.
373 * @zdev: pointer to zcrypt_device structure that identifies the
374 * CEX2A device to the request distributor
375 * @crt: pointer to the modexpoc_crt request buffer
377 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
378 struct ica_rsa_modexpo_crt *crt)
380 struct ap_message ap_msg;
381 struct completion work;
384 ap_init_message(&ap_msg);
385 if (zdev->user_space_type == ZCRYPT_CEX2A)
386 ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
388 ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL);
391 ap_msg.receive = zcrypt_cex2a_receive;
392 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
393 atomic_inc_return(&zcrypt_step);
394 ap_msg.private = &work;
395 rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
398 init_completion(&work);
399 ap_queue_message(zdev->ap_dev, &ap_msg);
400 rc = wait_for_completion_interruptible(&work);
402 rc = convert_response(zdev, &ap_msg, crt->outputdata,
403 crt->outputdatalength);
405 /* Signal pending. */
406 ap_cancel_message(zdev->ap_dev, &ap_msg);
408 kfree(ap_msg.message);
413 * The crypto operations for a CEX2A card.
415 static struct zcrypt_ops zcrypt_cex2a_ops = {
416 .rsa_modexpo = zcrypt_cex2a_modexpo,
417 .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
421 * Probe function for CEX2A cards. It always accepts the AP device
422 * since the bus_match already checked the hardware type.
423 * @ap_dev: pointer to the AP device.
425 static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
427 struct zcrypt_device *zdev = NULL;
430 switch (ap_dev->device_type) {
431 case AP_DEVICE_TYPE_CEX2A:
432 zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
435 zdev->user_space_type = ZCRYPT_CEX2A;
436 zdev->type_string = "CEX2A";
437 zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
438 zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
440 zdev->speed_rating = CEX2A_SPEED_RATING;
441 zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
443 case AP_DEVICE_TYPE_CEX3A:
444 zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE);
447 zdev->user_space_type = ZCRYPT_CEX3A;
448 zdev->type_string = "CEX3A";
449 zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
450 zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
451 zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
452 if (ap_4096_commands_available(ap_dev->qid)) {
453 zdev->max_mod_size = CEX3A_MAX_MOD_SIZE;
454 zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
457 zdev->speed_rating = CEX3A_SPEED_RATING;
461 zdev->ap_dev = ap_dev;
462 zdev->ops = &zcrypt_cex2a_ops;
464 ap_dev->reply = &zdev->reply;
465 ap_dev->private = zdev;
466 rc = zcrypt_device_register(zdev);
469 ap_dev->private = NULL;
470 zcrypt_device_free(zdev);
476 * This is called to remove the extended CEX2A driver information
477 * if an AP device is removed.
479 static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
481 struct zcrypt_device *zdev = ap_dev->private;
483 zcrypt_device_unregister(zdev);
486 int __init zcrypt_cex2a_init(void)
488 return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
491 void __exit zcrypt_cex2a_exit(void)
493 ap_driver_unregister(&zcrypt_cex2a_driver);
496 module_init(zcrypt_cex2a_init);
497 module_exit(zcrypt_cex2a_exit);