4 * Copyright IBM Corp. 2001, 2012
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>
11 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/err.h>
32 #include <linux/atomic.h>
33 #include <linux/uaccess.h>
36 #include "zcrypt_api.h"
37 #include "zcrypt_error.h"
38 #include "zcrypt_msgtype50.h"
40 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
42 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
44 #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
45 * (max outputdatalength) +
48 MODULE_AUTHOR("IBM Corporation");
49 MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
50 "Copyright IBM Corp. 2001, 2012");
51 MODULE_LICENSE("GPL");
53 static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
57 * The type 50 message family is associated with a CEX2A card.
59 * The four members of the family are described below.
61 * Note that all unsigned char arrays are right-justified and left-padded
64 * Note that all reserved fields must be zeroes.
67 unsigned char reserved1;
68 unsigned char msg_type_code; /* 0x50 */
69 unsigned short msg_len;
70 unsigned char reserved2;
71 unsigned char ignored;
72 unsigned short reserved3;
75 #define TYPE50_TYPE_CODE 0x50
77 #define TYPE50_MEB1_FMT 0x0001
78 #define TYPE50_MEB2_FMT 0x0002
79 #define TYPE50_MEB3_FMT 0x0003
80 #define TYPE50_CRB1_FMT 0x0011
81 #define TYPE50_CRB2_FMT 0x0012
82 #define TYPE50_CRB3_FMT 0x0013
84 /* Mod-Exp, with a small modulus */
85 struct type50_meb1_msg {
86 struct type50_hdr header;
87 unsigned short keyblock_type; /* 0x0001 */
88 unsigned char reserved[6];
89 unsigned char exponent[128];
90 unsigned char modulus[128];
91 unsigned char message[128];
94 /* Mod-Exp, with a large modulus */
95 struct type50_meb2_msg {
96 struct type50_hdr header;
97 unsigned short keyblock_type; /* 0x0002 */
98 unsigned char reserved[6];
99 unsigned char exponent[256];
100 unsigned char modulus[256];
101 unsigned char message[256];
104 /* Mod-Exp, with a larger modulus */
105 struct type50_meb3_msg {
106 struct type50_hdr header;
107 unsigned short keyblock_type; /* 0x0003 */
108 unsigned char reserved[6];
109 unsigned char exponent[512];
110 unsigned char modulus[512];
111 unsigned char message[512];
114 /* CRT, with a small modulus */
115 struct type50_crb1_msg {
116 struct type50_hdr header;
117 unsigned short keyblock_type; /* 0x0011 */
118 unsigned char reserved[6];
121 unsigned char dp[64];
122 unsigned char dq[64];
124 unsigned char message[128];
127 /* CRT, with a large modulus */
128 struct type50_crb2_msg {
129 struct type50_hdr header;
130 unsigned short keyblock_type; /* 0x0012 */
131 unsigned char reserved[6];
132 unsigned char p[128];
133 unsigned char q[128];
134 unsigned char dp[128];
135 unsigned char dq[128];
136 unsigned char u[128];
137 unsigned char message[256];
140 /* CRT, with a larger modulus */
141 struct type50_crb3_msg {
142 struct type50_hdr header;
143 unsigned short keyblock_type; /* 0x0013 */
144 unsigned char reserved[6];
145 unsigned char p[256];
146 unsigned char q[256];
147 unsigned char dp[256];
148 unsigned char dq[256];
149 unsigned char u[256];
150 unsigned char message[512];
154 * The type 80 response family is associated with a CEX2A card.
156 * Note that all unsigned char arrays are right-justified and left-padded
159 * Note that all reserved fields must be zeroes.
162 #define TYPE80_RSP_CODE 0x80
165 unsigned char reserved1;
166 unsigned char type; /* 0x80 */
168 unsigned char code; /* 0x00 */
169 unsigned char reserved2[3];
170 unsigned char reserved3[8];
174 * Convert a ICAMEX message to a type50 MEX message.
176 * @zdev: crypto device pointer
177 * @zreq: crypto request pointer
178 * @mex: pointer to user input data
180 * Returns 0 on success or -EFAULT.
182 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
183 struct ap_message *ap_msg,
184 struct ica_rsa_modexpo *mex)
186 unsigned char *mod, *exp, *inp;
189 mod_len = mex->inputdatalength;
191 if (mod_len <= 128) {
192 struct type50_meb1_msg *meb1 = ap_msg->message;
193 memset(meb1, 0, sizeof(*meb1));
194 ap_msg->length = sizeof(*meb1);
195 meb1->header.msg_type_code = TYPE50_TYPE_CODE;
196 meb1->header.msg_len = sizeof(*meb1);
197 meb1->keyblock_type = TYPE50_MEB1_FMT;
198 mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
199 exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
200 inp = meb1->message + sizeof(meb1->message) - mod_len;
201 } else if (mod_len <= 256) {
202 struct type50_meb2_msg *meb2 = ap_msg->message;
203 memset(meb2, 0, sizeof(*meb2));
204 ap_msg->length = sizeof(*meb2);
205 meb2->header.msg_type_code = TYPE50_TYPE_CODE;
206 meb2->header.msg_len = sizeof(*meb2);
207 meb2->keyblock_type = TYPE50_MEB2_FMT;
208 mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
209 exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
210 inp = meb2->message + sizeof(meb2->message) - mod_len;
212 /* mod_len > 256 = 4096 bit RSA Key */
213 struct type50_meb3_msg *meb3 = ap_msg->message;
214 memset(meb3, 0, sizeof(*meb3));
215 ap_msg->length = sizeof(*meb3);
216 meb3->header.msg_type_code = TYPE50_TYPE_CODE;
217 meb3->header.msg_len = sizeof(*meb3);
218 meb3->keyblock_type = TYPE50_MEB3_FMT;
219 mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
220 exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
221 inp = meb3->message + sizeof(meb3->message) - mod_len;
224 if (copy_from_user(mod, mex->n_modulus, mod_len) ||
225 copy_from_user(exp, mex->b_key, mod_len) ||
226 copy_from_user(inp, mex->inputdata, mod_len))
232 * Convert a ICACRT message to a type50 CRT message.
234 * @zdev: crypto device pointer
235 * @zreq: crypto request pointer
236 * @crt: pointer to user input data
238 * Returns 0 on success or -EFAULT.
240 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
241 struct ap_message *ap_msg,
242 struct ica_rsa_modexpo_crt *crt)
244 int mod_len, short_len, long_len, long_offset, limit;
245 unsigned char *p, *q, *dp, *dq, *u, *inp;
247 mod_len = crt->inputdatalength;
248 short_len = mod_len / 2;
249 long_len = mod_len / 2 + 8;
252 * CEX2A cannot handle p, dp, or U > 128 bytes.
253 * If we have one of these, we need to do extra checking.
254 * For CEX3A the limit is 256 bytes.
256 if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)
261 if (long_len > limit) {
263 * zcrypt_rsa_crt already checked for the leading
264 * zeroes of np_prime, bp_key and u_mult_inc.
266 long_offset = long_len - limit;
272 * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
273 * the larger message structure.
275 if (long_len <= 64) {
276 struct type50_crb1_msg *crb1 = ap_msg->message;
277 memset(crb1, 0, sizeof(*crb1));
278 ap_msg->length = sizeof(*crb1);
279 crb1->header.msg_type_code = TYPE50_TYPE_CODE;
280 crb1->header.msg_len = sizeof(*crb1);
281 crb1->keyblock_type = TYPE50_CRB1_FMT;
282 p = crb1->p + sizeof(crb1->p) - long_len;
283 q = crb1->q + sizeof(crb1->q) - short_len;
284 dp = crb1->dp + sizeof(crb1->dp) - long_len;
285 dq = crb1->dq + sizeof(crb1->dq) - short_len;
286 u = crb1->u + sizeof(crb1->u) - long_len;
287 inp = crb1->message + sizeof(crb1->message) - mod_len;
288 } else if (long_len <= 128) {
289 struct type50_crb2_msg *crb2 = ap_msg->message;
290 memset(crb2, 0, sizeof(*crb2));
291 ap_msg->length = sizeof(*crb2);
292 crb2->header.msg_type_code = TYPE50_TYPE_CODE;
293 crb2->header.msg_len = sizeof(*crb2);
294 crb2->keyblock_type = TYPE50_CRB2_FMT;
295 p = crb2->p + sizeof(crb2->p) - long_len;
296 q = crb2->q + sizeof(crb2->q) - short_len;
297 dp = crb2->dp + sizeof(crb2->dp) - long_len;
298 dq = crb2->dq + sizeof(crb2->dq) - short_len;
299 u = crb2->u + sizeof(crb2->u) - long_len;
300 inp = crb2->message + sizeof(crb2->message) - mod_len;
302 /* long_len >= 256 */
303 struct type50_crb3_msg *crb3 = ap_msg->message;
304 memset(crb3, 0, sizeof(*crb3));
305 ap_msg->length = sizeof(*crb3);
306 crb3->header.msg_type_code = TYPE50_TYPE_CODE;
307 crb3->header.msg_len = sizeof(*crb3);
308 crb3->keyblock_type = TYPE50_CRB3_FMT;
309 p = crb3->p + sizeof(crb3->p) - long_len;
310 q = crb3->q + sizeof(crb3->q) - short_len;
311 dp = crb3->dp + sizeof(crb3->dp) - long_len;
312 dq = crb3->dq + sizeof(crb3->dq) - short_len;
313 u = crb3->u + sizeof(crb3->u) - long_len;
314 inp = crb3->message + sizeof(crb3->message) - mod_len;
317 if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
318 copy_from_user(q, crt->nq_prime, short_len) ||
319 copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
320 copy_from_user(dq, crt->bq_key, short_len) ||
321 copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
322 copy_from_user(inp, crt->inputdata, mod_len))
329 * Copy results from a type 80 reply message back to user space.
331 * @zdev: crypto device pointer
332 * @reply: reply AP message.
333 * @data: pointer to user output data
334 * @length: size of user output data
336 * Returns 0 on success or -EFAULT.
338 static int convert_type80(struct zcrypt_device *zdev,
339 struct ap_message *reply,
340 char __user *outputdata,
341 unsigned int outputdatalength)
343 struct type80_hdr *t80h = reply->message;
346 if (t80h->len < sizeof(*t80h) + outputdatalength) {
347 /* The result is too short, the CEX2A card may not do that.. */
349 return -EAGAIN; /* repeat the request on a different device. */
351 if (zdev->user_space_type == ZCRYPT_CEX2A)
352 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
354 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
355 data = reply->message + t80h->len - outputdatalength;
356 if (copy_to_user(outputdata, data, outputdatalength))
361 static int convert_response(struct zcrypt_device *zdev,
362 struct ap_message *reply,
363 char __user *outputdata,
364 unsigned int outputdatalength)
366 /* Response type byte is the second byte in the response. */
367 switch (((unsigned char *) reply->message)[1]) {
368 case TYPE82_RSP_CODE:
369 case TYPE88_RSP_CODE:
370 return convert_error(zdev, reply);
371 case TYPE80_RSP_CODE:
372 return convert_type80(zdev, reply,
373 outputdata, outputdatalength);
374 default: /* Unknown response type, this should NEVER EVER happen */
376 return -EAGAIN; /* repeat the request on a different device. */
381 * This function is called from the AP bus code after a crypto request
382 * "msg" has finished with the reply message "reply".
383 * It is called from tasklet context.
384 * @ap_dev: pointer to the AP device
385 * @msg: pointer to the AP message
386 * @reply: pointer to the AP reply message
388 static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
389 struct ap_message *msg,
390 struct ap_message *reply)
392 static struct error_hdr error_reply = {
393 .type = TYPE82_RSP_CODE,
394 .reply_code = REP82_ERROR_MACHINE_FAILURE,
396 struct type80_hdr *t80h;
399 /* Copy the reply message to the request message buffer. */
401 memcpy(msg->message, &error_reply, sizeof(error_reply));
404 t80h = reply->message;
405 if (t80h->type == TYPE80_RSP_CODE) {
406 if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
408 CEX2A_MAX_RESPONSE_SIZE, t80h->len);
411 CEX3A_MAX_RESPONSE_SIZE, t80h->len);
412 memcpy(msg->message, reply->message, length);
414 memcpy(msg->message, reply->message, sizeof(error_reply));
416 complete((struct completion *) msg->private);
419 static atomic_t zcrypt_step = ATOMIC_INIT(0);
422 * The request distributor calls this function if it picked the CEX2A
423 * device to handle a modexpo request.
424 * @zdev: pointer to zcrypt_device structure that identifies the
425 * CEX2A device to the request distributor
426 * @mex: pointer to the modexpo request buffer
428 static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
429 struct ica_rsa_modexpo *mex)
431 struct ap_message ap_msg;
432 struct completion work;
435 ap_init_message(&ap_msg);
436 if (zdev->user_space_type == ZCRYPT_CEX2A)
437 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
440 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
444 ap_msg.receive = zcrypt_cex2a_receive;
445 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
446 atomic_inc_return(&zcrypt_step);
447 ap_msg.private = &work;
448 rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
451 init_completion(&work);
452 ap_queue_message(zdev->ap_dev, &ap_msg);
453 rc = wait_for_completion_interruptible(&work);
455 rc = convert_response(zdev, &ap_msg, mex->outputdata,
456 mex->outputdatalength);
458 /* Signal pending. */
459 ap_cancel_message(zdev->ap_dev, &ap_msg);
461 kfree(ap_msg.message);
466 * The request distributor calls this function if it picked the CEX2A
467 * device to handle a modexpo_crt request.
468 * @zdev: pointer to zcrypt_device structure that identifies the
469 * CEX2A device to the request distributor
470 * @crt: pointer to the modexpoc_crt request buffer
472 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
473 struct ica_rsa_modexpo_crt *crt)
475 struct ap_message ap_msg;
476 struct completion work;
479 ap_init_message(&ap_msg);
480 if (zdev->user_space_type == ZCRYPT_CEX2A)
481 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
484 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
488 ap_msg.receive = zcrypt_cex2a_receive;
489 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
490 atomic_inc_return(&zcrypt_step);
491 ap_msg.private = &work;
492 rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
495 init_completion(&work);
496 ap_queue_message(zdev->ap_dev, &ap_msg);
497 rc = wait_for_completion_interruptible(&work);
499 rc = convert_response(zdev, &ap_msg, crt->outputdata,
500 crt->outputdatalength);
502 /* Signal pending. */
503 ap_cancel_message(zdev->ap_dev, &ap_msg);
505 kfree(ap_msg.message);
510 * The crypto operations for message type 50.
512 static struct zcrypt_ops zcrypt_msgtype50_ops = {
513 .rsa_modexpo = zcrypt_cex2a_modexpo,
514 .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
515 .owner = THIS_MODULE,
516 .variant = MSGTYPE50_VARIANT_DEFAULT,
519 int __init zcrypt_msgtype50_init(void)
521 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
525 void __exit zcrypt_msgtype50_exit(void)
527 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
530 module_init(zcrypt_msgtype50_init);
531 module_exit(zcrypt_msgtype50_exit);