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");
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];
173 unsigned int get_rsa_modex_fc(struct ica_rsa_modexpo
*mex
, int *fcode
)
176 if (!mex
->inputdatalength
)
179 if (mex
->inputdatalength
<= 128) /* 1024 bit */
181 else if (mex
->inputdatalength
<= 256) /* 2048 bit */
189 unsigned int get_rsa_crt_fc(struct ica_rsa_modexpo_crt
*crt
, int *fcode
)
192 if (!crt
->inputdatalength
)
195 if (crt
->inputdatalength
<= 128) /* 1024 bit */
197 else if (crt
->inputdatalength
<= 256) /* 2048 bit */
206 * Convert a ICAMEX message to a type50 MEX message.
208 * @zq: crypto queue pointer
209 * @ap_msg: crypto request pointer
210 * @mex: pointer to user input data
212 * Returns 0 on success or -EFAULT.
214 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue
*zq
,
215 struct ap_message
*ap_msg
,
216 struct ica_rsa_modexpo
*mex
)
218 unsigned char *mod
, *exp
, *inp
;
221 mod_len
= mex
->inputdatalength
;
223 if (mod_len
<= 128) {
224 struct type50_meb1_msg
*meb1
= ap_msg
->message
;
225 memset(meb1
, 0, sizeof(*meb1
));
226 ap_msg
->length
= sizeof(*meb1
);
227 meb1
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
228 meb1
->header
.msg_len
= sizeof(*meb1
);
229 meb1
->keyblock_type
= TYPE50_MEB1_FMT
;
230 mod
= meb1
->modulus
+ sizeof(meb1
->modulus
) - mod_len
;
231 exp
= meb1
->exponent
+ sizeof(meb1
->exponent
) - mod_len
;
232 inp
= meb1
->message
+ sizeof(meb1
->message
) - mod_len
;
233 } else if (mod_len
<= 256) {
234 struct type50_meb2_msg
*meb2
= ap_msg
->message
;
235 memset(meb2
, 0, sizeof(*meb2
));
236 ap_msg
->length
= sizeof(*meb2
);
237 meb2
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
238 meb2
->header
.msg_len
= sizeof(*meb2
);
239 meb2
->keyblock_type
= TYPE50_MEB2_FMT
;
240 mod
= meb2
->modulus
+ sizeof(meb2
->modulus
) - mod_len
;
241 exp
= meb2
->exponent
+ sizeof(meb2
->exponent
) - mod_len
;
242 inp
= meb2
->message
+ sizeof(meb2
->message
) - mod_len
;
244 /* mod_len > 256 = 4096 bit RSA Key */
245 struct type50_meb3_msg
*meb3
= ap_msg
->message
;
246 memset(meb3
, 0, sizeof(*meb3
));
247 ap_msg
->length
= sizeof(*meb3
);
248 meb3
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
249 meb3
->header
.msg_len
= sizeof(*meb3
);
250 meb3
->keyblock_type
= TYPE50_MEB3_FMT
;
251 mod
= meb3
->modulus
+ sizeof(meb3
->modulus
) - mod_len
;
252 exp
= meb3
->exponent
+ sizeof(meb3
->exponent
) - mod_len
;
253 inp
= meb3
->message
+ sizeof(meb3
->message
) - mod_len
;
256 if (copy_from_user(mod
, mex
->n_modulus
, mod_len
) ||
257 copy_from_user(exp
, mex
->b_key
, mod_len
) ||
258 copy_from_user(inp
, mex
->inputdata
, mod_len
))
264 * Convert a ICACRT message to a type50 CRT message.
266 * @zq: crypto queue pointer
267 * @ap_msg: crypto request pointer
268 * @crt: pointer to user input data
270 * Returns 0 on success or -EFAULT.
272 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue
*zq
,
273 struct ap_message
*ap_msg
,
274 struct ica_rsa_modexpo_crt
*crt
)
276 int mod_len
, short_len
;
277 unsigned char *p
, *q
, *dp
, *dq
, *u
, *inp
;
279 mod_len
= crt
->inputdatalength
;
280 short_len
= (mod_len
+ 1) / 2;
283 * CEX2A and CEX3A w/o FW update can handle requests up to
284 * 256 byte modulus (2k keys).
285 * CEX3A with FW update and CEX4A cards are able to handle
286 * 512 byte modulus (4k keys).
288 if (mod_len
<= 128) { /* up to 1024 bit key size */
289 struct type50_crb1_msg
*crb1
= ap_msg
->message
;
290 memset(crb1
, 0, sizeof(*crb1
));
291 ap_msg
->length
= sizeof(*crb1
);
292 crb1
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
293 crb1
->header
.msg_len
= sizeof(*crb1
);
294 crb1
->keyblock_type
= TYPE50_CRB1_FMT
;
295 p
= crb1
->p
+ sizeof(crb1
->p
) - short_len
;
296 q
= crb1
->q
+ sizeof(crb1
->q
) - short_len
;
297 dp
= crb1
->dp
+ sizeof(crb1
->dp
) - short_len
;
298 dq
= crb1
->dq
+ sizeof(crb1
->dq
) - short_len
;
299 u
= crb1
->u
+ sizeof(crb1
->u
) - short_len
;
300 inp
= crb1
->message
+ sizeof(crb1
->message
) - mod_len
;
301 } else if (mod_len
<= 256) { /* up to 2048 bit key size */
302 struct type50_crb2_msg
*crb2
= ap_msg
->message
;
303 memset(crb2
, 0, sizeof(*crb2
));
304 ap_msg
->length
= sizeof(*crb2
);
305 crb2
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
306 crb2
->header
.msg_len
= sizeof(*crb2
);
307 crb2
->keyblock_type
= TYPE50_CRB2_FMT
;
308 p
= crb2
->p
+ sizeof(crb2
->p
) - short_len
;
309 q
= crb2
->q
+ sizeof(crb2
->q
) - short_len
;
310 dp
= crb2
->dp
+ sizeof(crb2
->dp
) - short_len
;
311 dq
= crb2
->dq
+ sizeof(crb2
->dq
) - short_len
;
312 u
= crb2
->u
+ sizeof(crb2
->u
) - short_len
;
313 inp
= crb2
->message
+ sizeof(crb2
->message
) - mod_len
;
314 } else if ((mod_len
<= 512) && /* up to 4096 bit key size */
315 (zq
->zcard
->max_mod_size
== CEX3A_MAX_MOD_SIZE
)) {
316 struct type50_crb3_msg
*crb3
= ap_msg
->message
;
317 memset(crb3
, 0, sizeof(*crb3
));
318 ap_msg
->length
= sizeof(*crb3
);
319 crb3
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
320 crb3
->header
.msg_len
= sizeof(*crb3
);
321 crb3
->keyblock_type
= TYPE50_CRB3_FMT
;
322 p
= crb3
->p
+ sizeof(crb3
->p
) - short_len
;
323 q
= crb3
->q
+ sizeof(crb3
->q
) - short_len
;
324 dp
= crb3
->dp
+ sizeof(crb3
->dp
) - short_len
;
325 dq
= crb3
->dq
+ sizeof(crb3
->dq
) - short_len
;
326 u
= crb3
->u
+ sizeof(crb3
->u
) - short_len
;
327 inp
= crb3
->message
+ sizeof(crb3
->message
) - mod_len
;
332 * correct the offset of p, bp and mult_inv according zcrypt.h
333 * block size right aligned (skip the first byte)
335 if (copy_from_user(p
, crt
->np_prime
+ MSGTYPE_ADJUSTMENT
, short_len
) ||
336 copy_from_user(q
, crt
->nq_prime
, short_len
) ||
337 copy_from_user(dp
, crt
->bp_key
+ MSGTYPE_ADJUSTMENT
, short_len
) ||
338 copy_from_user(dq
, crt
->bq_key
, short_len
) ||
339 copy_from_user(u
, crt
->u_mult_inv
+ MSGTYPE_ADJUSTMENT
, short_len
) ||
340 copy_from_user(inp
, crt
->inputdata
, mod_len
))
347 * Copy results from a type 80 reply message back to user space.
349 * @zq: crypto device pointer
350 * @reply: reply AP message.
351 * @data: pointer to user output data
352 * @length: size of user output data
354 * Returns 0 on success or -EFAULT.
356 static int convert_type80(struct zcrypt_queue
*zq
,
357 struct ap_message
*reply
,
358 char __user
*outputdata
,
359 unsigned int outputdatalength
)
361 struct type80_hdr
*t80h
= reply
->message
;
364 if (t80h
->len
< sizeof(*t80h
) + outputdatalength
) {
365 /* The result is too short, the CEX2A card may not do that.. */
367 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
368 AP_QID_CARD(zq
->queue
->qid
),
369 AP_QID_QUEUE(zq
->queue
->qid
));
371 "device=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
372 AP_QID_CARD(zq
->queue
->qid
),
373 AP_QID_QUEUE(zq
->queue
->qid
),
375 return -EAGAIN
; /* repeat the request on a different device. */
377 if (zq
->zcard
->user_space_type
== ZCRYPT_CEX2A
)
378 BUG_ON(t80h
->len
> CEX2A_MAX_RESPONSE_SIZE
);
380 BUG_ON(t80h
->len
> CEX3A_MAX_RESPONSE_SIZE
);
381 data
= reply
->message
+ t80h
->len
- outputdatalength
;
382 if (copy_to_user(outputdata
, data
, outputdatalength
))
387 static int convert_response(struct zcrypt_queue
*zq
,
388 struct ap_message
*reply
,
389 char __user
*outputdata
,
390 unsigned int outputdatalength
)
392 /* Response type byte is the second byte in the response. */
393 unsigned char rtype
= ((unsigned char *) reply
->message
)[1];
396 case TYPE82_RSP_CODE
:
397 case TYPE88_RSP_CODE
:
398 return convert_error(zq
, reply
);
399 case TYPE80_RSP_CODE
:
400 return convert_type80(zq
, reply
,
401 outputdata
, outputdatalength
);
402 default: /* Unknown response type, this should NEVER EVER happen */
404 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
405 AP_QID_CARD(zq
->queue
->qid
),
406 AP_QID_QUEUE(zq
->queue
->qid
));
408 "device=%02x.%04x rtype=0x%02x => online=0 rc=EAGAIN\n",
409 AP_QID_CARD(zq
->queue
->qid
),
410 AP_QID_QUEUE(zq
->queue
->qid
),
411 (unsigned int) rtype
);
412 return -EAGAIN
; /* repeat the request on a different device. */
417 * This function is called from the AP bus code after a crypto request
418 * "msg" has finished with the reply message "reply".
419 * It is called from tasklet context.
420 * @aq: pointer to the AP device
421 * @msg: pointer to the AP message
422 * @reply: pointer to the AP reply message
424 static void zcrypt_cex2a_receive(struct ap_queue
*aq
,
425 struct ap_message
*msg
,
426 struct ap_message
*reply
)
428 static struct error_hdr error_reply
= {
429 .type
= TYPE82_RSP_CODE
,
430 .reply_code
= REP82_ERROR_MACHINE_FAILURE
,
432 struct type80_hdr
*t80h
;
435 /* Copy the reply message to the request message buffer. */
437 goto out
; /* ap_msg->rc indicates the error */
438 t80h
= reply
->message
;
439 if (t80h
->type
== TYPE80_RSP_CODE
) {
440 if (aq
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX2A
)
442 CEX2A_MAX_RESPONSE_SIZE
, t80h
->len
);
445 CEX3A_MAX_RESPONSE_SIZE
, t80h
->len
);
446 memcpy(msg
->message
, reply
->message
, length
);
448 memcpy(msg
->message
, reply
->message
, sizeof(error_reply
));
450 complete((struct completion
*) msg
->private);
453 static atomic_t zcrypt_step
= ATOMIC_INIT(0);
456 * The request distributor calls this function if it picked the CEX2A
457 * device to handle a modexpo request.
458 * @zq: pointer to zcrypt_queue structure that identifies the
459 * CEX2A device to the request distributor
460 * @mex: pointer to the modexpo request buffer
462 static long zcrypt_cex2a_modexpo(struct zcrypt_queue
*zq
,
463 struct ica_rsa_modexpo
*mex
)
465 struct ap_message ap_msg
;
466 struct completion work
;
469 ap_init_message(&ap_msg
);
470 if (zq
->zcard
->user_space_type
== ZCRYPT_CEX2A
)
471 ap_msg
.message
= kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE
,
474 ap_msg
.message
= kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE
,
478 ap_msg
.receive
= zcrypt_cex2a_receive
;
479 ap_msg
.psmid
= (((unsigned long long) current
->pid
) << 32) +
480 atomic_inc_return(&zcrypt_step
);
481 ap_msg
.private = &work
;
482 rc
= ICAMEX_msg_to_type50MEX_msg(zq
, &ap_msg
, mex
);
485 init_completion(&work
);
486 ap_queue_message(zq
->queue
, &ap_msg
);
487 rc
= wait_for_completion_interruptible(&work
);
491 rc
= convert_response(zq
, &ap_msg
, mex
->outputdata
,
492 mex
->outputdatalength
);
494 /* Signal pending. */
495 ap_cancel_message(zq
->queue
, &ap_msg
);
497 kfree(ap_msg
.message
);
502 * The request distributor calls this function if it picked the CEX2A
503 * device to handle a modexpo_crt request.
504 * @zq: pointer to zcrypt_queue structure that identifies the
505 * CEX2A device to the request distributor
506 * @crt: pointer to the modexpoc_crt request buffer
508 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_queue
*zq
,
509 struct ica_rsa_modexpo_crt
*crt
)
511 struct ap_message ap_msg
;
512 struct completion work
;
515 ap_init_message(&ap_msg
);
516 if (zq
->zcard
->user_space_type
== ZCRYPT_CEX2A
)
517 ap_msg
.message
= kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE
,
520 ap_msg
.message
= kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE
,
524 ap_msg
.receive
= zcrypt_cex2a_receive
;
525 ap_msg
.psmid
= (((unsigned long long) current
->pid
) << 32) +
526 atomic_inc_return(&zcrypt_step
);
527 ap_msg
.private = &work
;
528 rc
= ICACRT_msg_to_type50CRT_msg(zq
, &ap_msg
, crt
);
531 init_completion(&work
);
532 ap_queue_message(zq
->queue
, &ap_msg
);
533 rc
= wait_for_completion_interruptible(&work
);
537 rc
= convert_response(zq
, &ap_msg
, crt
->outputdata
,
538 crt
->outputdatalength
);
540 /* Signal pending. */
541 ap_cancel_message(zq
->queue
, &ap_msg
);
543 kfree(ap_msg
.message
);
548 * The crypto operations for message type 50.
550 static struct zcrypt_ops zcrypt_msgtype50_ops
= {
551 .rsa_modexpo
= zcrypt_cex2a_modexpo
,
552 .rsa_modexpo_crt
= zcrypt_cex2a_modexpo_crt
,
553 .owner
= THIS_MODULE
,
554 .name
= MSGTYPE50_NAME
,
555 .variant
= MSGTYPE50_VARIANT_DEFAULT
,
558 void __init
zcrypt_msgtype50_init(void)
560 zcrypt_msgtype_register(&zcrypt_msgtype50_ops
);
563 void __exit
zcrypt_msgtype50_exit(void)
565 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops
);