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 #define KMSG_COMPONENT "zcrypt"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/err.h>
35 #include <linux/atomic.h>
36 #include <linux/uaccess.h>
39 #include "zcrypt_api.h"
40 #include "zcrypt_error.h"
41 #include "zcrypt_msgtype50.h"
43 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
45 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
47 #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
48 * (max outputdatalength) +
51 MODULE_AUTHOR("IBM Corporation");
52 MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
53 "Copyright IBM Corp. 2001, 2012");
54 MODULE_LICENSE("GPL");
56 static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
60 * The type 50 message family is associated with a CEX2A card.
62 * The four members of the family are described below.
64 * Note that all unsigned char arrays are right-justified and left-padded
67 * Note that all reserved fields must be zeroes.
70 unsigned char reserved1;
71 unsigned char msg_type_code; /* 0x50 */
72 unsigned short msg_len;
73 unsigned char reserved2;
74 unsigned char ignored;
75 unsigned short reserved3;
78 #define TYPE50_TYPE_CODE 0x50
80 #define TYPE50_MEB1_FMT 0x0001
81 #define TYPE50_MEB2_FMT 0x0002
82 #define TYPE50_MEB3_FMT 0x0003
83 #define TYPE50_CRB1_FMT 0x0011
84 #define TYPE50_CRB2_FMT 0x0012
85 #define TYPE50_CRB3_FMT 0x0013
87 /* Mod-Exp, with a small modulus */
88 struct type50_meb1_msg {
89 struct type50_hdr header;
90 unsigned short keyblock_type; /* 0x0001 */
91 unsigned char reserved[6];
92 unsigned char exponent[128];
93 unsigned char modulus[128];
94 unsigned char message[128];
97 /* Mod-Exp, with a large modulus */
98 struct type50_meb2_msg {
99 struct type50_hdr header;
100 unsigned short keyblock_type; /* 0x0002 */
101 unsigned char reserved[6];
102 unsigned char exponent[256];
103 unsigned char modulus[256];
104 unsigned char message[256];
107 /* Mod-Exp, with a larger modulus */
108 struct type50_meb3_msg {
109 struct type50_hdr header;
110 unsigned short keyblock_type; /* 0x0003 */
111 unsigned char reserved[6];
112 unsigned char exponent[512];
113 unsigned char modulus[512];
114 unsigned char message[512];
117 /* CRT, with a small modulus */
118 struct type50_crb1_msg {
119 struct type50_hdr header;
120 unsigned short keyblock_type; /* 0x0011 */
121 unsigned char reserved[6];
124 unsigned char dp[64];
125 unsigned char dq[64];
127 unsigned char message[128];
130 /* CRT, with a large modulus */
131 struct type50_crb2_msg {
132 struct type50_hdr header;
133 unsigned short keyblock_type; /* 0x0012 */
134 unsigned char reserved[6];
135 unsigned char p[128];
136 unsigned char q[128];
137 unsigned char dp[128];
138 unsigned char dq[128];
139 unsigned char u[128];
140 unsigned char message[256];
143 /* CRT, with a larger modulus */
144 struct type50_crb3_msg {
145 struct type50_hdr header;
146 unsigned short keyblock_type; /* 0x0013 */
147 unsigned char reserved[6];
148 unsigned char p[256];
149 unsigned char q[256];
150 unsigned char dp[256];
151 unsigned char dq[256];
152 unsigned char u[256];
153 unsigned char message[512];
157 * The type 80 response family is associated with a CEX2A card.
159 * Note that all unsigned char arrays are right-justified and left-padded
162 * Note that all reserved fields must be zeroes.
165 #define TYPE80_RSP_CODE 0x80
168 unsigned char reserved1;
169 unsigned char type; /* 0x80 */
171 unsigned char code; /* 0x00 */
172 unsigned char reserved2[3];
173 unsigned char reserved3[8];
177 * Convert a ICAMEX message to a type50 MEX message.
179 * @zdev: crypto device pointer
180 * @zreq: crypto request pointer
181 * @mex: pointer to user input data
183 * Returns 0 on success or -EFAULT.
185 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
186 struct ap_message *ap_msg,
187 struct ica_rsa_modexpo *mex)
189 unsigned char *mod, *exp, *inp;
192 mod_len = mex->inputdatalength;
194 if (mod_len <= 128) {
195 struct type50_meb1_msg *meb1 = ap_msg->message;
196 memset(meb1, 0, sizeof(*meb1));
197 ap_msg->length = sizeof(*meb1);
198 meb1->header.msg_type_code = TYPE50_TYPE_CODE;
199 meb1->header.msg_len = sizeof(*meb1);
200 meb1->keyblock_type = TYPE50_MEB1_FMT;
201 mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
202 exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
203 inp = meb1->message + sizeof(meb1->message) - mod_len;
204 } else if (mod_len <= 256) {
205 struct type50_meb2_msg *meb2 = ap_msg->message;
206 memset(meb2, 0, sizeof(*meb2));
207 ap_msg->length = sizeof(*meb2);
208 meb2->header.msg_type_code = TYPE50_TYPE_CODE;
209 meb2->header.msg_len = sizeof(*meb2);
210 meb2->keyblock_type = TYPE50_MEB2_FMT;
211 mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
212 exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
213 inp = meb2->message + sizeof(meb2->message) - mod_len;
215 /* mod_len > 256 = 4096 bit RSA Key */
216 struct type50_meb3_msg *meb3 = ap_msg->message;
217 memset(meb3, 0, sizeof(*meb3));
218 ap_msg->length = sizeof(*meb3);
219 meb3->header.msg_type_code = TYPE50_TYPE_CODE;
220 meb3->header.msg_len = sizeof(*meb3);
221 meb3->keyblock_type = TYPE50_MEB3_FMT;
222 mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
223 exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
224 inp = meb3->message + sizeof(meb3->message) - mod_len;
227 if (copy_from_user(mod, mex->n_modulus, mod_len) ||
228 copy_from_user(exp, mex->b_key, mod_len) ||
229 copy_from_user(inp, mex->inputdata, mod_len))
235 * Convert a ICACRT message to a type50 CRT message.
237 * @zdev: crypto device pointer
238 * @zreq: crypto request pointer
239 * @crt: pointer to user input data
241 * Returns 0 on success or -EFAULT.
243 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
244 struct ap_message *ap_msg,
245 struct ica_rsa_modexpo_crt *crt)
247 int mod_len, short_len;
248 unsigned char *p, *q, *dp, *dq, *u, *inp;
250 mod_len = crt->inputdatalength;
251 short_len = (mod_len + 1) / 2;
254 * CEX2A and CEX3A w/o FW update can handle requests up to
255 * 256 byte modulus (2k keys).
256 * CEX3A with FW update and CEX4A cards are able to handle
257 * 512 byte modulus (4k keys).
259 if (mod_len <= 128) { /* up to 1024 bit key size */
260 struct type50_crb1_msg *crb1 = ap_msg->message;
261 memset(crb1, 0, sizeof(*crb1));
262 ap_msg->length = sizeof(*crb1);
263 crb1->header.msg_type_code = TYPE50_TYPE_CODE;
264 crb1->header.msg_len = sizeof(*crb1);
265 crb1->keyblock_type = TYPE50_CRB1_FMT;
266 p = crb1->p + sizeof(crb1->p) - short_len;
267 q = crb1->q + sizeof(crb1->q) - short_len;
268 dp = crb1->dp + sizeof(crb1->dp) - short_len;
269 dq = crb1->dq + sizeof(crb1->dq) - short_len;
270 u = crb1->u + sizeof(crb1->u) - short_len;
271 inp = crb1->message + sizeof(crb1->message) - mod_len;
272 } else if (mod_len <= 256) { /* up to 2048 bit key size */
273 struct type50_crb2_msg *crb2 = ap_msg->message;
274 memset(crb2, 0, sizeof(*crb2));
275 ap_msg->length = sizeof(*crb2);
276 crb2->header.msg_type_code = TYPE50_TYPE_CODE;
277 crb2->header.msg_len = sizeof(*crb2);
278 crb2->keyblock_type = TYPE50_CRB2_FMT;
279 p = crb2->p + sizeof(crb2->p) - short_len;
280 q = crb2->q + sizeof(crb2->q) - short_len;
281 dp = crb2->dp + sizeof(crb2->dp) - short_len;
282 dq = crb2->dq + sizeof(crb2->dq) - short_len;
283 u = crb2->u + sizeof(crb2->u) - short_len;
284 inp = crb2->message + sizeof(crb2->message) - mod_len;
285 } else if ((mod_len <= 512) && /* up to 4096 bit key size */
286 (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)) { /* >= CEX3A */
287 struct type50_crb3_msg *crb3 = ap_msg->message;
288 memset(crb3, 0, sizeof(*crb3));
289 ap_msg->length = sizeof(*crb3);
290 crb3->header.msg_type_code = TYPE50_TYPE_CODE;
291 crb3->header.msg_len = sizeof(*crb3);
292 crb3->keyblock_type = TYPE50_CRB3_FMT;
293 p = crb3->p + sizeof(crb3->p) - short_len;
294 q = crb3->q + sizeof(crb3->q) - short_len;
295 dp = crb3->dp + sizeof(crb3->dp) - short_len;
296 dq = crb3->dq + sizeof(crb3->dq) - short_len;
297 u = crb3->u + sizeof(crb3->u) - short_len;
298 inp = crb3->message + sizeof(crb3->message) - mod_len;
303 * correct the offset of p, bp and mult_inv according zcrypt.h
304 * block size right aligned (skip the first byte)
306 if (copy_from_user(p, crt->np_prime + MSGTYPE_ADJUSTMENT, short_len) ||
307 copy_from_user(q, crt->nq_prime, short_len) ||
308 copy_from_user(dp, crt->bp_key + MSGTYPE_ADJUSTMENT, short_len) ||
309 copy_from_user(dq, crt->bq_key, short_len) ||
310 copy_from_user(u, crt->u_mult_inv + MSGTYPE_ADJUSTMENT, short_len) ||
311 copy_from_user(inp, crt->inputdata, mod_len))
318 * Copy results from a type 80 reply message back to user space.
320 * @zdev: crypto device pointer
321 * @reply: reply AP message.
322 * @data: pointer to user output data
323 * @length: size of user output data
325 * Returns 0 on success or -EFAULT.
327 static int convert_type80(struct zcrypt_device *zdev,
328 struct ap_message *reply,
329 char __user *outputdata,
330 unsigned int outputdatalength)
332 struct type80_hdr *t80h = reply->message;
335 if (t80h->len < sizeof(*t80h) + outputdatalength) {
336 /* The result is too short, the CEX2A card may not do that.. */
338 pr_err("Cryptographic device %x failed and was set offline\n",
340 ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%drc%d",
341 zdev->ap_dev->qid, zdev->online, t80h->code);
343 return -EAGAIN; /* repeat the request on a different device. */
345 if (zdev->user_space_type == ZCRYPT_CEX2A)
346 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
348 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
349 data = reply->message + t80h->len - outputdatalength;
350 if (copy_to_user(outputdata, data, outputdatalength))
355 static int convert_response(struct zcrypt_device *zdev,
356 struct ap_message *reply,
357 char __user *outputdata,
358 unsigned int outputdatalength)
360 /* Response type byte is the second byte in the response. */
361 switch (((unsigned char *) reply->message)[1]) {
362 case TYPE82_RSP_CODE:
363 case TYPE88_RSP_CODE:
364 return convert_error(zdev, reply);
365 case TYPE80_RSP_CODE:
366 return convert_type80(zdev, reply,
367 outputdata, outputdatalength);
368 default: /* Unknown response type, this should NEVER EVER happen */
370 pr_err("Cryptographic device %x failed and was set offline\n",
372 ZCRYPT_DBF_DEV(DBF_ERR, zdev, "dev%04xo%dfail",
373 zdev->ap_dev->qid, zdev->online);
374 return -EAGAIN; /* repeat the request on a different device. */
379 * This function is called from the AP bus code after a crypto request
380 * "msg" has finished with the reply message "reply".
381 * It is called from tasklet context.
382 * @ap_dev: pointer to the AP device
383 * @msg: pointer to the AP message
384 * @reply: pointer to the AP reply message
386 static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
387 struct ap_message *msg,
388 struct ap_message *reply)
390 static struct error_hdr error_reply = {
391 .type = TYPE82_RSP_CODE,
392 .reply_code = REP82_ERROR_MACHINE_FAILURE,
394 struct type80_hdr *t80h;
397 /* Copy the reply message to the request message buffer. */
399 goto out; /* ap_msg->rc indicates the error */
400 t80h = reply->message;
401 if (t80h->type == TYPE80_RSP_CODE) {
402 if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
404 CEX2A_MAX_RESPONSE_SIZE, t80h->len);
407 CEX3A_MAX_RESPONSE_SIZE, t80h->len);
408 memcpy(msg->message, reply->message, length);
410 memcpy(msg->message, reply->message, sizeof(error_reply));
412 complete((struct completion *) msg->private);
415 static atomic_t zcrypt_step = ATOMIC_INIT(0);
418 * The request distributor calls this function if it picked the CEX2A
419 * device to handle a modexpo request.
420 * @zdev: pointer to zcrypt_device structure that identifies the
421 * CEX2A device to the request distributor
422 * @mex: pointer to the modexpo request buffer
424 static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
425 struct ica_rsa_modexpo *mex)
427 struct ap_message ap_msg;
428 struct completion work;
431 ap_init_message(&ap_msg);
432 if (zdev->user_space_type == ZCRYPT_CEX2A)
433 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
436 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
440 ap_msg.receive = zcrypt_cex2a_receive;
441 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
442 atomic_inc_return(&zcrypt_step);
443 ap_msg.private = &work;
444 rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
447 init_completion(&work);
448 ap_queue_message(zdev->ap_dev, &ap_msg);
449 rc = wait_for_completion_interruptible(&work);
453 rc = convert_response(zdev, &ap_msg, mex->outputdata,
454 mex->outputdatalength);
456 /* Signal pending. */
457 ap_cancel_message(zdev->ap_dev, &ap_msg);
459 kfree(ap_msg.message);
464 * The request distributor calls this function if it picked the CEX2A
465 * device to handle a modexpo_crt request.
466 * @zdev: pointer to zcrypt_device structure that identifies the
467 * CEX2A device to the request distributor
468 * @crt: pointer to the modexpoc_crt request buffer
470 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
471 struct ica_rsa_modexpo_crt *crt)
473 struct ap_message ap_msg;
474 struct completion work;
477 ap_init_message(&ap_msg);
478 if (zdev->user_space_type == ZCRYPT_CEX2A)
479 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
482 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
486 ap_msg.receive = zcrypt_cex2a_receive;
487 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
488 atomic_inc_return(&zcrypt_step);
489 ap_msg.private = &work;
490 rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
493 init_completion(&work);
494 ap_queue_message(zdev->ap_dev, &ap_msg);
495 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 .name = MSGTYPE50_NAME,
517 .variant = MSGTYPE50_VARIANT_DEFAULT,
520 int __init zcrypt_msgtype50_init(void)
522 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
526 void __exit zcrypt_msgtype50_exit(void)
528 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
531 module_init(zcrypt_msgtype50_init);
532 module_exit(zcrypt_msgtype50_exit);