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
;
245 unsigned char *p
, *q
, *dp
, *dq
, *u
, *inp
;
247 mod_len
= crt
->inputdatalength
;
248 short_len
= mod_len
/ 2;
251 * CEX2A and CEX3A w/o FW update can handle requests up to
252 * 256 byte modulus (2k keys).
253 * CEX3A with FW update and CEX4A cards are able to handle
254 * 512 byte modulus (4k keys).
256 if (mod_len
<= 128) { /* up to 1024 bit key size */
257 struct type50_crb1_msg
*crb1
= ap_msg
->message
;
258 memset(crb1
, 0, sizeof(*crb1
));
259 ap_msg
->length
= sizeof(*crb1
);
260 crb1
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
261 crb1
->header
.msg_len
= sizeof(*crb1
);
262 crb1
->keyblock_type
= TYPE50_CRB1_FMT
;
263 p
= crb1
->p
+ sizeof(crb1
->p
) - short_len
;
264 q
= crb1
->q
+ sizeof(crb1
->q
) - short_len
;
265 dp
= crb1
->dp
+ sizeof(crb1
->dp
) - short_len
;
266 dq
= crb1
->dq
+ sizeof(crb1
->dq
) - short_len
;
267 u
= crb1
->u
+ sizeof(crb1
->u
) - short_len
;
268 inp
= crb1
->message
+ sizeof(crb1
->message
) - mod_len
;
269 } else if (mod_len
<= 256) { /* up to 2048 bit key size */
270 struct type50_crb2_msg
*crb2
= ap_msg
->message
;
271 memset(crb2
, 0, sizeof(*crb2
));
272 ap_msg
->length
= sizeof(*crb2
);
273 crb2
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
274 crb2
->header
.msg_len
= sizeof(*crb2
);
275 crb2
->keyblock_type
= TYPE50_CRB2_FMT
;
276 p
= crb2
->p
+ sizeof(crb2
->p
) - short_len
;
277 q
= crb2
->q
+ sizeof(crb2
->q
) - short_len
;
278 dp
= crb2
->dp
+ sizeof(crb2
->dp
) - short_len
;
279 dq
= crb2
->dq
+ sizeof(crb2
->dq
) - short_len
;
280 u
= crb2
->u
+ sizeof(crb2
->u
) - short_len
;
281 inp
= crb2
->message
+ sizeof(crb2
->message
) - mod_len
;
282 } else if ((mod_len
<= 512) && /* up to 4096 bit key size */
283 (zdev
->max_mod_size
== CEX3A_MAX_MOD_SIZE
)) { /* >= CEX3A */
284 struct type50_crb3_msg
*crb3
= ap_msg
->message
;
285 memset(crb3
, 0, sizeof(*crb3
));
286 ap_msg
->length
= sizeof(*crb3
);
287 crb3
->header
.msg_type_code
= TYPE50_TYPE_CODE
;
288 crb3
->header
.msg_len
= sizeof(*crb3
);
289 crb3
->keyblock_type
= TYPE50_CRB3_FMT
;
290 p
= crb3
->p
+ sizeof(crb3
->p
) - short_len
;
291 q
= crb3
->q
+ sizeof(crb3
->q
) - short_len
;
292 dp
= crb3
->dp
+ sizeof(crb3
->dp
) - short_len
;
293 dq
= crb3
->dq
+ sizeof(crb3
->dq
) - short_len
;
294 u
= crb3
->u
+ sizeof(crb3
->u
) - short_len
;
295 inp
= crb3
->message
+ sizeof(crb3
->message
) - mod_len
;
300 * correct the offset of p, bp and mult_inv according zcrypt.h
301 * block size right aligned (skip the first byte)
303 if (copy_from_user(p
, crt
->np_prime
+ MSGTYPE_ADJUSTMENT
, short_len
) ||
304 copy_from_user(q
, crt
->nq_prime
, short_len
) ||
305 copy_from_user(dp
, crt
->bp_key
+ MSGTYPE_ADJUSTMENT
, short_len
) ||
306 copy_from_user(dq
, crt
->bq_key
, short_len
) ||
307 copy_from_user(u
, crt
->u_mult_inv
+ MSGTYPE_ADJUSTMENT
, short_len
) ||
308 copy_from_user(inp
, crt
->inputdata
, mod_len
))
315 * Copy results from a type 80 reply message back to user space.
317 * @zdev: crypto device pointer
318 * @reply: reply AP message.
319 * @data: pointer to user output data
320 * @length: size of user output data
322 * Returns 0 on success or -EFAULT.
324 static int convert_type80(struct zcrypt_device
*zdev
,
325 struct ap_message
*reply
,
326 char __user
*outputdata
,
327 unsigned int outputdatalength
)
329 struct type80_hdr
*t80h
= reply
->message
;
332 if (t80h
->len
< sizeof(*t80h
) + outputdatalength
) {
333 /* The result is too short, the CEX2A card may not do that.. */
335 return -EAGAIN
; /* repeat the request on a different device. */
337 if (zdev
->user_space_type
== ZCRYPT_CEX2A
)
338 BUG_ON(t80h
->len
> CEX2A_MAX_RESPONSE_SIZE
);
340 BUG_ON(t80h
->len
> CEX3A_MAX_RESPONSE_SIZE
);
341 data
= reply
->message
+ t80h
->len
- outputdatalength
;
342 if (copy_to_user(outputdata
, data
, outputdatalength
))
347 static int convert_response(struct zcrypt_device
*zdev
,
348 struct ap_message
*reply
,
349 char __user
*outputdata
,
350 unsigned int outputdatalength
)
352 /* Response type byte is the second byte in the response. */
353 switch (((unsigned char *) reply
->message
)[1]) {
354 case TYPE82_RSP_CODE
:
355 case TYPE88_RSP_CODE
:
356 return convert_error(zdev
, reply
);
357 case TYPE80_RSP_CODE
:
358 return convert_type80(zdev
, reply
,
359 outputdata
, outputdatalength
);
360 default: /* Unknown response type, this should NEVER EVER happen */
362 return -EAGAIN
; /* repeat the request on a different device. */
367 * This function is called from the AP bus code after a crypto request
368 * "msg" has finished with the reply message "reply".
369 * It is called from tasklet context.
370 * @ap_dev: pointer to the AP device
371 * @msg: pointer to the AP message
372 * @reply: pointer to the AP reply message
374 static void zcrypt_cex2a_receive(struct ap_device
*ap_dev
,
375 struct ap_message
*msg
,
376 struct ap_message
*reply
)
378 static struct error_hdr error_reply
= {
379 .type
= TYPE82_RSP_CODE
,
380 .reply_code
= REP82_ERROR_MACHINE_FAILURE
,
382 struct type80_hdr
*t80h
;
385 /* Copy the reply message to the request message buffer. */
387 memcpy(msg
->message
, &error_reply
, sizeof(error_reply
));
390 t80h
= reply
->message
;
391 if (t80h
->type
== TYPE80_RSP_CODE
) {
392 if (ap_dev
->device_type
== AP_DEVICE_TYPE_CEX2A
)
394 CEX2A_MAX_RESPONSE_SIZE
, t80h
->len
);
397 CEX3A_MAX_RESPONSE_SIZE
, t80h
->len
);
398 memcpy(msg
->message
, reply
->message
, length
);
400 memcpy(msg
->message
, reply
->message
, sizeof(error_reply
));
402 complete((struct completion
*) msg
->private);
405 static atomic_t zcrypt_step
= ATOMIC_INIT(0);
408 * The request distributor calls this function if it picked the CEX2A
409 * device to handle a modexpo request.
410 * @zdev: pointer to zcrypt_device structure that identifies the
411 * CEX2A device to the request distributor
412 * @mex: pointer to the modexpo request buffer
414 static long zcrypt_cex2a_modexpo(struct zcrypt_device
*zdev
,
415 struct ica_rsa_modexpo
*mex
)
417 struct ap_message ap_msg
;
418 struct completion work
;
421 ap_init_message(&ap_msg
);
422 if (zdev
->user_space_type
== ZCRYPT_CEX2A
)
423 ap_msg
.message
= kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE
,
426 ap_msg
.message
= kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE
,
430 ap_msg
.receive
= zcrypt_cex2a_receive
;
431 ap_msg
.psmid
= (((unsigned long long) current
->pid
) << 32) +
432 atomic_inc_return(&zcrypt_step
);
433 ap_msg
.private = &work
;
434 rc
= ICAMEX_msg_to_type50MEX_msg(zdev
, &ap_msg
, mex
);
437 init_completion(&work
);
438 ap_queue_message(zdev
->ap_dev
, &ap_msg
);
439 rc
= wait_for_completion_interruptible(&work
);
441 rc
= convert_response(zdev
, &ap_msg
, mex
->outputdata
,
442 mex
->outputdatalength
);
444 /* Signal pending. */
445 ap_cancel_message(zdev
->ap_dev
, &ap_msg
);
447 kfree(ap_msg
.message
);
452 * The request distributor calls this function if it picked the CEX2A
453 * device to handle a modexpo_crt request.
454 * @zdev: pointer to zcrypt_device structure that identifies the
455 * CEX2A device to the request distributor
456 * @crt: pointer to the modexpoc_crt request buffer
458 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device
*zdev
,
459 struct ica_rsa_modexpo_crt
*crt
)
461 struct ap_message ap_msg
;
462 struct completion work
;
465 ap_init_message(&ap_msg
);
466 if (zdev
->user_space_type
== ZCRYPT_CEX2A
)
467 ap_msg
.message
= kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE
,
470 ap_msg
.message
= kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE
,
474 ap_msg
.receive
= zcrypt_cex2a_receive
;
475 ap_msg
.psmid
= (((unsigned long long) current
->pid
) << 32) +
476 atomic_inc_return(&zcrypt_step
);
477 ap_msg
.private = &work
;
478 rc
= ICACRT_msg_to_type50CRT_msg(zdev
, &ap_msg
, crt
);
481 init_completion(&work
);
482 ap_queue_message(zdev
->ap_dev
, &ap_msg
);
483 rc
= wait_for_completion_interruptible(&work
);
485 rc
= convert_response(zdev
, &ap_msg
, crt
->outputdata
,
486 crt
->outputdatalength
);
488 /* Signal pending. */
489 ap_cancel_message(zdev
->ap_dev
, &ap_msg
);
491 kfree(ap_msg
.message
);
496 * The crypto operations for message type 50.
498 static struct zcrypt_ops zcrypt_msgtype50_ops
= {
499 .rsa_modexpo
= zcrypt_cex2a_modexpo
,
500 .rsa_modexpo_crt
= zcrypt_cex2a_modexpo_crt
,
501 .owner
= THIS_MODULE
,
502 .variant
= MSGTYPE50_VARIANT_DEFAULT
,
505 int __init
zcrypt_msgtype50_init(void)
507 zcrypt_msgtype_register(&zcrypt_msgtype50_ops
);
511 void __exit
zcrypt_msgtype50_exit(void)
513 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops
);
516 module_init(zcrypt_msgtype50_init
);
517 module_exit(zcrypt_msgtype50_exit
);