This source file includes following definitions.
- get_rsa_modex_fc
- get_rsa_crt_fc
- ICAMEX_msg_to_type50MEX_msg
- ICACRT_msg_to_type50CRT_msg
- convert_type80
- convert_response
- zcrypt_cex2a_receive
- zcrypt_cex2a_modexpo
- zcrypt_cex2a_modexpo_crt
- zcrypt_msgtype50_init
- zcrypt_msgtype50_exit
1
2
3
4
5
6
7
8
9
10
11
12
13 #define KMSG_COMPONENT "zcrypt"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/atomic.h>
21 #include <linux/uaccess.h>
22
23 #include "ap_bus.h"
24 #include "zcrypt_api.h"
25 #include "zcrypt_error.h"
26 #include "zcrypt_msgtype50.h"
27
28
29 #define CEX3A_MAX_MOD_SIZE 512
30
31
32 #define CEX2A_MAX_RESPONSE_SIZE 0x110
33
34
35 #define CEX3A_MAX_RESPONSE_SIZE 0x210
36
37 MODULE_AUTHOR("IBM Corporation");
38 MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
39 "Copyright IBM Corp. 2001, 2012");
40 MODULE_LICENSE("GPL");
41
42
43
44
45
46
47
48
49
50
51
52 struct type50_hdr {
53 unsigned char reserved1;
54 unsigned char msg_type_code;
55 unsigned short msg_len;
56 unsigned char reserved2;
57 unsigned char ignored;
58 unsigned short reserved3;
59 } __packed;
60
61 #define TYPE50_TYPE_CODE 0x50
62
63 #define TYPE50_MEB1_FMT 0x0001
64 #define TYPE50_MEB2_FMT 0x0002
65 #define TYPE50_MEB3_FMT 0x0003
66 #define TYPE50_CRB1_FMT 0x0011
67 #define TYPE50_CRB2_FMT 0x0012
68 #define TYPE50_CRB3_FMT 0x0013
69
70
71 struct type50_meb1_msg {
72 struct type50_hdr header;
73 unsigned short keyblock_type;
74 unsigned char reserved[6];
75 unsigned char exponent[128];
76 unsigned char modulus[128];
77 unsigned char message[128];
78 } __packed;
79
80
81 struct type50_meb2_msg {
82 struct type50_hdr header;
83 unsigned short keyblock_type;
84 unsigned char reserved[6];
85 unsigned char exponent[256];
86 unsigned char modulus[256];
87 unsigned char message[256];
88 } __packed;
89
90
91 struct type50_meb3_msg {
92 struct type50_hdr header;
93 unsigned short keyblock_type;
94 unsigned char reserved[6];
95 unsigned char exponent[512];
96 unsigned char modulus[512];
97 unsigned char message[512];
98 } __packed;
99
100
101 struct type50_crb1_msg {
102 struct type50_hdr header;
103 unsigned short keyblock_type;
104 unsigned char reserved[6];
105 unsigned char p[64];
106 unsigned char q[64];
107 unsigned char dp[64];
108 unsigned char dq[64];
109 unsigned char u[64];
110 unsigned char message[128];
111 } __packed;
112
113
114 struct type50_crb2_msg {
115 struct type50_hdr header;
116 unsigned short keyblock_type;
117 unsigned char reserved[6];
118 unsigned char p[128];
119 unsigned char q[128];
120 unsigned char dp[128];
121 unsigned char dq[128];
122 unsigned char u[128];
123 unsigned char message[256];
124 } __packed;
125
126
127 struct type50_crb3_msg {
128 struct type50_hdr header;
129 unsigned short keyblock_type;
130 unsigned char reserved[6];
131 unsigned char p[256];
132 unsigned char q[256];
133 unsigned char dp[256];
134 unsigned char dq[256];
135 unsigned char u[256];
136 unsigned char message[512];
137 } __packed;
138
139
140
141
142
143
144
145
146
147
148 #define TYPE80_RSP_CODE 0x80
149
150 struct type80_hdr {
151 unsigned char reserved1;
152 unsigned char type;
153 unsigned short len;
154 unsigned char code;
155 unsigned char reserved2[3];
156 unsigned char reserved3[8];
157 } __packed;
158
159 unsigned int get_rsa_modex_fc(struct ica_rsa_modexpo *mex, int *fcode)
160 {
161
162 if (!mex->inputdatalength)
163 return -EINVAL;
164
165 if (mex->inputdatalength <= 128)
166 *fcode = MEX_1K;
167 else if (mex->inputdatalength <= 256)
168 *fcode = MEX_2K;
169 else
170 *fcode = MEX_4K;
171
172 return 0;
173 }
174
175 unsigned int get_rsa_crt_fc(struct ica_rsa_modexpo_crt *crt, int *fcode)
176 {
177
178 if (!crt->inputdatalength)
179 return -EINVAL;
180
181 if (crt->inputdatalength <= 128)
182 *fcode = CRT_1K;
183 else if (crt->inputdatalength <= 256)
184 *fcode = CRT_2K;
185 else
186 *fcode = CRT_4K;
187
188 return 0;
189 }
190
191
192
193
194
195
196
197
198
199
200 static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_queue *zq,
201 struct ap_message *ap_msg,
202 struct ica_rsa_modexpo *mex)
203 {
204 unsigned char *mod, *exp, *inp;
205 int mod_len;
206
207 mod_len = mex->inputdatalength;
208
209 if (mod_len <= 128) {
210 struct type50_meb1_msg *meb1 = ap_msg->message;
211
212 memset(meb1, 0, sizeof(*meb1));
213 ap_msg->length = sizeof(*meb1);
214 meb1->header.msg_type_code = TYPE50_TYPE_CODE;
215 meb1->header.msg_len = sizeof(*meb1);
216 meb1->keyblock_type = TYPE50_MEB1_FMT;
217 mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
218 exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
219 inp = meb1->message + sizeof(meb1->message) - mod_len;
220 } else if (mod_len <= 256) {
221 struct type50_meb2_msg *meb2 = ap_msg->message;
222
223 memset(meb2, 0, sizeof(*meb2));
224 ap_msg->length = sizeof(*meb2);
225 meb2->header.msg_type_code = TYPE50_TYPE_CODE;
226 meb2->header.msg_len = sizeof(*meb2);
227 meb2->keyblock_type = TYPE50_MEB2_FMT;
228 mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
229 exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
230 inp = meb2->message + sizeof(meb2->message) - mod_len;
231 } else if (mod_len <= 512) {
232 struct type50_meb3_msg *meb3 = ap_msg->message;
233
234 memset(meb3, 0, sizeof(*meb3));
235 ap_msg->length = sizeof(*meb3);
236 meb3->header.msg_type_code = TYPE50_TYPE_CODE;
237 meb3->header.msg_len = sizeof(*meb3);
238 meb3->keyblock_type = TYPE50_MEB3_FMT;
239 mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
240 exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
241 inp = meb3->message + sizeof(meb3->message) - mod_len;
242 } else
243 return -EINVAL;
244
245 if (copy_from_user(mod, mex->n_modulus, mod_len) ||
246 copy_from_user(exp, mex->b_key, mod_len) ||
247 copy_from_user(inp, mex->inputdata, mod_len))
248 return -EFAULT;
249 return 0;
250 }
251
252
253
254
255
256
257
258
259
260
261 static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue *zq,
262 struct ap_message *ap_msg,
263 struct ica_rsa_modexpo_crt *crt)
264 {
265 int mod_len, short_len;
266 unsigned char *p, *q, *dp, *dq, *u, *inp;
267
268 mod_len = crt->inputdatalength;
269 short_len = (mod_len + 1) / 2;
270
271
272
273
274
275
276
277 if (mod_len <= 128) {
278 struct type50_crb1_msg *crb1 = ap_msg->message;
279
280 memset(crb1, 0, sizeof(*crb1));
281 ap_msg->length = sizeof(*crb1);
282 crb1->header.msg_type_code = TYPE50_TYPE_CODE;
283 crb1->header.msg_len = sizeof(*crb1);
284 crb1->keyblock_type = TYPE50_CRB1_FMT;
285 p = crb1->p + sizeof(crb1->p) - short_len;
286 q = crb1->q + sizeof(crb1->q) - short_len;
287 dp = crb1->dp + sizeof(crb1->dp) - short_len;
288 dq = crb1->dq + sizeof(crb1->dq) - short_len;
289 u = crb1->u + sizeof(crb1->u) - short_len;
290 inp = crb1->message + sizeof(crb1->message) - mod_len;
291 } else if (mod_len <= 256) {
292 struct type50_crb2_msg *crb2 = ap_msg->message;
293
294 memset(crb2, 0, sizeof(*crb2));
295 ap_msg->length = sizeof(*crb2);
296 crb2->header.msg_type_code = TYPE50_TYPE_CODE;
297 crb2->header.msg_len = sizeof(*crb2);
298 crb2->keyblock_type = TYPE50_CRB2_FMT;
299 p = crb2->p + sizeof(crb2->p) - short_len;
300 q = crb2->q + sizeof(crb2->q) - short_len;
301 dp = crb2->dp + sizeof(crb2->dp) - short_len;
302 dq = crb2->dq + sizeof(crb2->dq) - short_len;
303 u = crb2->u + sizeof(crb2->u) - short_len;
304 inp = crb2->message + sizeof(crb2->message) - mod_len;
305 } else if ((mod_len <= 512) &&
306 (zq->zcard->max_mod_size == CEX3A_MAX_MOD_SIZE)) {
307 struct type50_crb3_msg *crb3 = ap_msg->message;
308
309 memset(crb3, 0, sizeof(*crb3));
310 ap_msg->length = sizeof(*crb3);
311 crb3->header.msg_type_code = TYPE50_TYPE_CODE;
312 crb3->header.msg_len = sizeof(*crb3);
313 crb3->keyblock_type = TYPE50_CRB3_FMT;
314 p = crb3->p + sizeof(crb3->p) - short_len;
315 q = crb3->q + sizeof(crb3->q) - short_len;
316 dp = crb3->dp + sizeof(crb3->dp) - short_len;
317 dq = crb3->dq + sizeof(crb3->dq) - short_len;
318 u = crb3->u + sizeof(crb3->u) - short_len;
319 inp = crb3->message + sizeof(crb3->message) - mod_len;
320 } else
321 return -EINVAL;
322
323
324
325
326
327 if (copy_from_user(p, crt->np_prime + MSGTYPE_ADJUSTMENT, short_len) ||
328 copy_from_user(q, crt->nq_prime, short_len) ||
329 copy_from_user(dp, crt->bp_key + MSGTYPE_ADJUSTMENT, short_len) ||
330 copy_from_user(dq, crt->bq_key, short_len) ||
331 copy_from_user(u, crt->u_mult_inv + MSGTYPE_ADJUSTMENT, short_len) ||
332 copy_from_user(inp, crt->inputdata, mod_len))
333 return -EFAULT;
334
335 return 0;
336 }
337
338
339
340
341
342
343
344
345
346
347
348 static int convert_type80(struct zcrypt_queue *zq,
349 struct ap_message *reply,
350 char __user *outputdata,
351 unsigned int outputdatalength)
352 {
353 struct type80_hdr *t80h = reply->message;
354 unsigned char *data;
355
356 if (t80h->len < sizeof(*t80h) + outputdatalength) {
357
358 zq->online = 0;
359 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
360 AP_QID_CARD(zq->queue->qid),
361 AP_QID_QUEUE(zq->queue->qid));
362 ZCRYPT_DBF(DBF_ERR,
363 "device=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
364 AP_QID_CARD(zq->queue->qid),
365 AP_QID_QUEUE(zq->queue->qid),
366 t80h->code);
367 return -EAGAIN;
368 }
369 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
370 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
371 else
372 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
373 data = reply->message + t80h->len - outputdatalength;
374 if (copy_to_user(outputdata, data, outputdatalength))
375 return -EFAULT;
376 return 0;
377 }
378
379 static int convert_response(struct zcrypt_queue *zq,
380 struct ap_message *reply,
381 char __user *outputdata,
382 unsigned int outputdatalength)
383 {
384
385 unsigned char rtype = ((unsigned char *) reply->message)[1];
386
387 switch (rtype) {
388 case TYPE82_RSP_CODE:
389 case TYPE88_RSP_CODE:
390 return convert_error(zq, reply);
391 case TYPE80_RSP_CODE:
392 return convert_type80(zq, reply,
393 outputdata, outputdatalength);
394 default:
395 zq->online = 0;
396 pr_err("Cryptographic device %02x.%04x failed and was set offline\n",
397 AP_QID_CARD(zq->queue->qid),
398 AP_QID_QUEUE(zq->queue->qid));
399 ZCRYPT_DBF(DBF_ERR,
400 "device=%02x.%04x rtype=0x%02x => online=0 rc=EAGAIN\n",
401 AP_QID_CARD(zq->queue->qid),
402 AP_QID_QUEUE(zq->queue->qid),
403 (unsigned int) rtype);
404 return -EAGAIN;
405 }
406 }
407
408
409
410
411
412
413
414
415
416 static void zcrypt_cex2a_receive(struct ap_queue *aq,
417 struct ap_message *msg,
418 struct ap_message *reply)
419 {
420 static struct error_hdr error_reply = {
421 .type = TYPE82_RSP_CODE,
422 .reply_code = REP82_ERROR_MACHINE_FAILURE,
423 };
424 struct type80_hdr *t80h;
425 int length;
426
427
428 if (!reply)
429 goto out;
430 t80h = reply->message;
431 if (t80h->type == TYPE80_RSP_CODE) {
432 if (aq->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A)
433 length = min_t(int,
434 CEX2A_MAX_RESPONSE_SIZE, t80h->len);
435 else
436 length = min_t(int,
437 CEX3A_MAX_RESPONSE_SIZE, t80h->len);
438 memcpy(msg->message, reply->message, length);
439 } else
440 memcpy(msg->message, reply->message, sizeof(error_reply));
441 out:
442 complete((struct completion *) msg->private);
443 }
444
445 static atomic_t zcrypt_step = ATOMIC_INIT(0);
446
447
448
449
450
451
452
453
454 static long zcrypt_cex2a_modexpo(struct zcrypt_queue *zq,
455 struct ica_rsa_modexpo *mex)
456 {
457 struct ap_message ap_msg;
458 struct completion work;
459 int rc;
460
461 ap_init_message(&ap_msg);
462 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
463 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
464 GFP_KERNEL);
465 else
466 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
467 GFP_KERNEL);
468 if (!ap_msg.message)
469 return -ENOMEM;
470 ap_msg.receive = zcrypt_cex2a_receive;
471 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
472 atomic_inc_return(&zcrypt_step);
473 ap_msg.private = &work;
474 rc = ICAMEX_msg_to_type50MEX_msg(zq, &ap_msg, mex);
475 if (rc)
476 goto out_free;
477 init_completion(&work);
478 ap_queue_message(zq->queue, &ap_msg);
479 rc = wait_for_completion_interruptible(&work);
480 if (rc == 0) {
481 rc = ap_msg.rc;
482 if (rc == 0)
483 rc = convert_response(zq, &ap_msg, mex->outputdata,
484 mex->outputdatalength);
485 } else
486
487 ap_cancel_message(zq->queue, &ap_msg);
488 out_free:
489 kfree(ap_msg.message);
490 return rc;
491 }
492
493
494
495
496
497
498
499
500 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_queue *zq,
501 struct ica_rsa_modexpo_crt *crt)
502 {
503 struct ap_message ap_msg;
504 struct completion work;
505 int rc;
506
507 ap_init_message(&ap_msg);
508 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
509 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
510 GFP_KERNEL);
511 else
512 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
513 GFP_KERNEL);
514 if (!ap_msg.message)
515 return -ENOMEM;
516 ap_msg.receive = zcrypt_cex2a_receive;
517 ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
518 atomic_inc_return(&zcrypt_step);
519 ap_msg.private = &work;
520 rc = ICACRT_msg_to_type50CRT_msg(zq, &ap_msg, crt);
521 if (rc)
522 goto out_free;
523 init_completion(&work);
524 ap_queue_message(zq->queue, &ap_msg);
525 rc = wait_for_completion_interruptible(&work);
526 if (rc == 0) {
527 rc = ap_msg.rc;
528 if (rc == 0)
529 rc = convert_response(zq, &ap_msg, crt->outputdata,
530 crt->outputdatalength);
531 } else
532
533 ap_cancel_message(zq->queue, &ap_msg);
534 out_free:
535 kfree(ap_msg.message);
536 return rc;
537 }
538
539
540
541
542 static struct zcrypt_ops zcrypt_msgtype50_ops = {
543 .rsa_modexpo = zcrypt_cex2a_modexpo,
544 .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
545 .owner = THIS_MODULE,
546 .name = MSGTYPE50_NAME,
547 .variant = MSGTYPE50_VARIANT_DEFAULT,
548 };
549
550 void __init zcrypt_msgtype50_init(void)
551 {
552 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
553 }
554
555 void __exit zcrypt_msgtype50_exit(void)
556 {
557 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
558 }