1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#define KMSG_COMPONENT "zcrypt"
29#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
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>
37
38#include "ap_bus.h"
39#include "zcrypt_api.h"
40#include "zcrypt_error.h"
41#include "zcrypt_msgtype50.h"
42
43#define CEX3A_MAX_MOD_SIZE 512
44
45#define CEX2A_MAX_RESPONSE_SIZE 0x110
46
47#define CEX3A_MAX_RESPONSE_SIZE 0x210
48
49
50
51MODULE_AUTHOR("IBM Corporation");
52MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
53 "Copyright IBM Corp. 2001, 2012");
54MODULE_LICENSE("GPL");
55
56
57
58
59
60
61
62
63
64
65
66struct type50_hdr {
67 unsigned char reserved1;
68 unsigned char msg_type_code;
69 unsigned short msg_len;
70 unsigned char reserved2;
71 unsigned char ignored;
72 unsigned short reserved3;
73} __packed;
74
75#define TYPE50_TYPE_CODE 0x50
76
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
83
84
85struct type50_meb1_msg {
86 struct type50_hdr header;
87 unsigned short keyblock_type;
88 unsigned char reserved[6];
89 unsigned char exponent[128];
90 unsigned char modulus[128];
91 unsigned char message[128];
92} __packed;
93
94
95struct type50_meb2_msg {
96 struct type50_hdr header;
97 unsigned short keyblock_type;
98 unsigned char reserved[6];
99 unsigned char exponent[256];
100 unsigned char modulus[256];
101 unsigned char message[256];
102} __packed;
103
104
105struct type50_meb3_msg {
106 struct type50_hdr header;
107 unsigned short keyblock_type;
108 unsigned char reserved[6];
109 unsigned char exponent[512];
110 unsigned char modulus[512];
111 unsigned char message[512];
112} __packed;
113
114
115struct type50_crb1_msg {
116 struct type50_hdr header;
117 unsigned short keyblock_type;
118 unsigned char reserved[6];
119 unsigned char p[64];
120 unsigned char q[64];
121 unsigned char dp[64];
122 unsigned char dq[64];
123 unsigned char u[64];
124 unsigned char message[128];
125} __packed;
126
127
128struct type50_crb2_msg {
129 struct type50_hdr header;
130 unsigned short keyblock_type;
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];
138} __packed;
139
140
141struct type50_crb3_msg {
142 struct type50_hdr header;
143 unsigned short keyblock_type;
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];
151} __packed;
152
153
154
155
156
157
158
159
160
161
162#define TYPE80_RSP_CODE 0x80
163
164struct type80_hdr {
165 unsigned char reserved1;
166 unsigned char type;
167 unsigned short len;
168 unsigned char code;
169 unsigned char reserved2[3];
170 unsigned char reserved3[8];
171} __packed;
172
173unsigned int get_rsa_modex_fc(struct ica_rsa_modexpo *mex, int *fcode)
174{
175
176 if (!mex->inputdatalength)
177 return -EINVAL;
178
179 if (mex->inputdatalength <= 128)
180 *fcode = MEX_1K;
181 else if (mex->inputdatalength <= 256)
182 *fcode = MEX_2K;
183 else
184 *fcode = MEX_4K;
185
186 return 0;
187}
188
189unsigned int get_rsa_crt_fc(struct ica_rsa_modexpo_crt *crt, int *fcode)
190{
191
192 if (!crt->inputdatalength)
193 return -EINVAL;
194
195 if (crt->inputdatalength <= 128)
196 *fcode = CRT_1K;
197 else if (crt->inputdatalength <= 256)
198 *fcode = CRT_2K;
199 else
200 *fcode = CRT_4K;
201
202 return 0;
203}
204
205
206
207
208
209
210
211
212
213
214static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue *zq,
215 struct ap_message *ap_msg,
216 struct ica_rsa_modexpo *mex)
217{
218 unsigned char *mod, *exp, *inp;
219 int mod_len;
220
221 mod_len = mex->inputdatalength;
222
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;
243 } else {
244
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;
254 }
255
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))
259 return -EFAULT;
260 return 0;
261}
262
263
264
265
266
267
268
269
270
271
272static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue *zq,
273 struct ap_message *ap_msg,
274 struct ica_rsa_modexpo_crt *crt)
275{
276 int mod_len, short_len;
277 unsigned char *p, *q, *dp, *dq, *u, *inp;
278
279 mod_len = crt->inputdatalength;
280 short_len = (mod_len + 1) / 2;
281
282
283
284
285
286
287
288 if (mod_len <= 128) {
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) {
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) &&
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;
328 } else
329 return -EINVAL;
330
331
332
333
334
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))
341 return -EFAULT;
342
343 return 0;
344}
345
346
347
348
349
350
351
352
353
354
355
356static int convert_type80(struct zcrypt_queue *zq,
357 struct ap_message *reply,
358 char __user *outputdata,
359 unsigned int outputdatalength)
360{
361 struct type80_hdr *t80h = reply->message;
362 unsigned char *data;
363
364 if (t80h->len < sizeof(*t80h) + outputdatalength) {
365
366 zq->online = 0;
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));
370 ZCRYPT_DBF(DBF_ERR,
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),
374 t80h->code);
375 return -EAGAIN;
376 }
377 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
378 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
379 else
380 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
381 data = reply->message + t80h->len - outputdatalength;
382 if (copy_to_user(outputdata, data, outputdatalength))
383 return -EFAULT;
384 return 0;
385}
386
387static int convert_response(struct zcrypt_queue *zq,
388 struct ap_message *reply,
389 char __user *outputdata,
390 unsigned int outputdatalength)
391{
392
393 unsigned char rtype = ((unsigned char *) reply->message)[1];
394
395 switch (rtype) {
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:
403 zq->online = 0;
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));
407 ZCRYPT_DBF(DBF_ERR,
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;
413 }
414}
415
416
417
418
419
420
421
422
423
424static void zcrypt_cex2a_receive(struct ap_queue *aq,
425 struct ap_message *msg,
426 struct ap_message *reply)
427{
428 static struct error_hdr error_reply = {
429 .type = TYPE82_RSP_CODE,
430 .reply_code = REP82_ERROR_MACHINE_FAILURE,
431 };
432 struct type80_hdr *t80h;
433 int length;
434
435
436 if (!reply)
437 goto out;
438 t80h = reply->message;
439 if (t80h->type == TYPE80_RSP_CODE) {
440 if (aq->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A)
441 length = min_t(int,
442 CEX2A_MAX_RESPONSE_SIZE, t80h->len);
443 else
444 length = min_t(int,
445 CEX3A_MAX_RESPONSE_SIZE, t80h->len);
446 memcpy(msg->message, reply->message, length);
447 } else
448 memcpy(msg->message, reply->message, sizeof(error_reply));
449out:
450 complete((struct completion *) msg->private);
451}
452
453static atomic_t zcrypt_step = ATOMIC_INIT(0);
454
455
456
457
458
459
460
461
462static long zcrypt_cex2a_modexpo(struct zcrypt_queue *zq,
463 struct ica_rsa_modexpo *mex)
464{
465 struct ap_message ap_msg;
466 struct completion work;
467 int rc;
468
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,
472 GFP_KERNEL);
473 else
474 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
475 GFP_KERNEL);
476 if (!ap_msg.message)
477 return -ENOMEM;
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);
483 if (rc)
484 goto out_free;
485 init_completion(&work);
486 ap_queue_message(zq->queue, &ap_msg);
487 rc = wait_for_completion_interruptible(&work);
488 if (rc == 0) {
489 rc = ap_msg.rc;
490 if (rc == 0)
491 rc = convert_response(zq, &ap_msg, mex->outputdata,
492 mex->outputdatalength);
493 } else
494
495 ap_cancel_message(zq->queue, &ap_msg);
496out_free:
497 kfree(ap_msg.message);
498 return rc;
499}
500
501
502
503
504
505
506
507
508static long zcrypt_cex2a_modexpo_crt(struct zcrypt_queue *zq,
509 struct ica_rsa_modexpo_crt *crt)
510{
511 struct ap_message ap_msg;
512 struct completion work;
513 int rc;
514
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,
518 GFP_KERNEL);
519 else
520 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
521 GFP_KERNEL);
522 if (!ap_msg.message)
523 return -ENOMEM;
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);
529 if (rc)
530 goto out_free;
531 init_completion(&work);
532 ap_queue_message(zq->queue, &ap_msg);
533 rc = wait_for_completion_interruptible(&work);
534 if (rc == 0) {
535 rc = ap_msg.rc;
536 if (rc == 0)
537 rc = convert_response(zq, &ap_msg, crt->outputdata,
538 crt->outputdatalength);
539 } else
540
541 ap_cancel_message(zq->queue, &ap_msg);
542out_free:
543 kfree(ap_msg.message);
544 return rc;
545}
546
547
548
549
550static 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,
556};
557
558void __init zcrypt_msgtype50_init(void)
559{
560 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
561}
562
563void __exit zcrypt_msgtype50_exit(void)
564{
565 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
566}
567