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
37MODULE_AUTHOR("IBM Corporation");
38MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
39 "Copyright IBM Corp. 2001, 2012");
40MODULE_LICENSE("GPL");
41
42
43
44
45
46
47
48
49
50
51
52struct 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
71struct 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
81struct 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
91struct 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
101struct 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
114struct 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
127struct 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
150struct 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
159unsigned 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
175unsigned 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
200static 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->msg;
211
212 memset(meb1, 0, sizeof(*meb1));
213 ap_msg->len = 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->msg;
222
223 memset(meb2, 0, sizeof(*meb2));
224 ap_msg->len = 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->msg;
233
234 memset(meb3, 0, sizeof(*meb3));
235 ap_msg->len = 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
250#ifdef CONFIG_ZCRYPT_DEBUG
251 if (ap_msg->fi.flags & AP_FI_FLAG_TOGGLE_SPECIAL)
252 ap_msg->flags ^= AP_MSG_FLAG_SPECIAL;
253#endif
254
255 return 0;
256}
257
258
259
260
261
262
263
264
265
266
267static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue *zq,
268 struct ap_message *ap_msg,
269 struct ica_rsa_modexpo_crt *crt)
270{
271 int mod_len, short_len;
272 unsigned char *p, *q, *dp, *dq, *u, *inp;
273
274 mod_len = crt->inputdatalength;
275 short_len = (mod_len + 1) / 2;
276
277
278
279
280
281
282
283 if (mod_len <= 128) {
284 struct type50_crb1_msg *crb1 = ap_msg->msg;
285
286 memset(crb1, 0, sizeof(*crb1));
287 ap_msg->len = sizeof(*crb1);
288 crb1->header.msg_type_code = TYPE50_TYPE_CODE;
289 crb1->header.msg_len = sizeof(*crb1);
290 crb1->keyblock_type = TYPE50_CRB1_FMT;
291 p = crb1->p + sizeof(crb1->p) - short_len;
292 q = crb1->q + sizeof(crb1->q) - short_len;
293 dp = crb1->dp + sizeof(crb1->dp) - short_len;
294 dq = crb1->dq + sizeof(crb1->dq) - short_len;
295 u = crb1->u + sizeof(crb1->u) - short_len;
296 inp = crb1->message + sizeof(crb1->message) - mod_len;
297 } else if (mod_len <= 256) {
298 struct type50_crb2_msg *crb2 = ap_msg->msg;
299
300 memset(crb2, 0, sizeof(*crb2));
301 ap_msg->len = sizeof(*crb2);
302 crb2->header.msg_type_code = TYPE50_TYPE_CODE;
303 crb2->header.msg_len = sizeof(*crb2);
304 crb2->keyblock_type = TYPE50_CRB2_FMT;
305 p = crb2->p + sizeof(crb2->p) - short_len;
306 q = crb2->q + sizeof(crb2->q) - short_len;
307 dp = crb2->dp + sizeof(crb2->dp) - short_len;
308 dq = crb2->dq + sizeof(crb2->dq) - short_len;
309 u = crb2->u + sizeof(crb2->u) - short_len;
310 inp = crb2->message + sizeof(crb2->message) - mod_len;
311 } else if ((mod_len <= 512) &&
312 (zq->zcard->max_mod_size == CEX3A_MAX_MOD_SIZE)) {
313 struct type50_crb3_msg *crb3 = ap_msg->msg;
314
315 memset(crb3, 0, sizeof(*crb3));
316 ap_msg->len = sizeof(*crb3);
317 crb3->header.msg_type_code = TYPE50_TYPE_CODE;
318 crb3->header.msg_len = sizeof(*crb3);
319 crb3->keyblock_type = TYPE50_CRB3_FMT;
320 p = crb3->p + sizeof(crb3->p) - short_len;
321 q = crb3->q + sizeof(crb3->q) - short_len;
322 dp = crb3->dp + sizeof(crb3->dp) - short_len;
323 dq = crb3->dq + sizeof(crb3->dq) - short_len;
324 u = crb3->u + sizeof(crb3->u) - short_len;
325 inp = crb3->message + sizeof(crb3->message) - mod_len;
326 } else
327 return -EINVAL;
328
329
330
331
332
333 if (copy_from_user(p, crt->np_prime + MSGTYPE_ADJUSTMENT, short_len) ||
334 copy_from_user(q, crt->nq_prime, short_len) ||
335 copy_from_user(dp, crt->bp_key + MSGTYPE_ADJUSTMENT, short_len) ||
336 copy_from_user(dq, crt->bq_key, short_len) ||
337 copy_from_user(u, crt->u_mult_inv + MSGTYPE_ADJUSTMENT, short_len) ||
338 copy_from_user(inp, crt->inputdata, mod_len))
339 return -EFAULT;
340
341#ifdef CONFIG_ZCRYPT_DEBUG
342 if (ap_msg->fi.flags & AP_FI_FLAG_TOGGLE_SPECIAL)
343 ap_msg->flags ^= AP_MSG_FLAG_SPECIAL;
344#endif
345
346 return 0;
347}
348
349
350
351
352
353
354
355
356
357
358
359static int convert_type80(struct zcrypt_queue *zq,
360 struct ap_message *reply,
361 char __user *outputdata,
362 unsigned int outputdatalength)
363{
364 struct type80_hdr *t80h = reply->msg;
365 unsigned char *data;
366
367 if (t80h->len < sizeof(*t80h) + outputdatalength) {
368
369 zq->online = 0;
370 pr_err("Crypto dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
371 AP_QID_CARD(zq->queue->qid),
372 AP_QID_QUEUE(zq->queue->qid),
373 t80h->code);
374 ZCRYPT_DBF_ERR("dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
375 AP_QID_CARD(zq->queue->qid),
376 AP_QID_QUEUE(zq->queue->qid),
377 t80h->code);
378 return -EAGAIN;
379 }
380 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
381 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
382 else
383 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
384 data = reply->msg + t80h->len - outputdatalength;
385 if (copy_to_user(outputdata, data, outputdatalength))
386 return -EFAULT;
387 return 0;
388}
389
390static int convert_response_cex2a(struct zcrypt_queue *zq,
391 struct ap_message *reply,
392 char __user *outputdata,
393 unsigned int outputdatalength)
394{
395
396 unsigned char rtype = ((unsigned char *) reply->msg)[1];
397
398 switch (rtype) {
399 case TYPE82_RSP_CODE:
400 case TYPE88_RSP_CODE:
401 return convert_error(zq, reply);
402 case TYPE80_RSP_CODE:
403 return convert_type80(zq, reply,
404 outputdata, outputdatalength);
405 default:
406 zq->online = 0;
407 pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
408 AP_QID_CARD(zq->queue->qid),
409 AP_QID_QUEUE(zq->queue->qid),
410 (int) rtype);
411 ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
412 AP_QID_CARD(zq->queue->qid),
413 AP_QID_QUEUE(zq->queue->qid),
414 (int) rtype);
415 return -EAGAIN;
416 }
417}
418
419
420
421
422
423
424
425
426
427static void zcrypt_cex2a_receive(struct ap_queue *aq,
428 struct ap_message *msg,
429 struct ap_message *reply)
430{
431 static struct error_hdr error_reply = {
432 .type = TYPE82_RSP_CODE,
433 .reply_code = REP82_ERROR_MACHINE_FAILURE,
434 };
435 struct type80_hdr *t80h;
436 int len;
437
438
439 if (!reply)
440 goto out;
441 t80h = reply->msg;
442 if (t80h->type == TYPE80_RSP_CODE) {
443 if (aq->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A)
444 len = min_t(int, CEX2A_MAX_RESPONSE_SIZE, t80h->len);
445 else
446 len = min_t(int, CEX3A_MAX_RESPONSE_SIZE, t80h->len);
447 memcpy(msg->msg, reply->msg, len);
448 } else
449 memcpy(msg->msg, reply->msg, sizeof(error_reply));
450out:
451 complete((struct completion *) msg->private);
452}
453
454static atomic_t zcrypt_step = ATOMIC_INIT(0);
455
456
457
458
459
460
461
462
463static long zcrypt_cex2a_modexpo(struct zcrypt_queue *zq,
464 struct ica_rsa_modexpo *mex,
465 struct ap_message *ap_msg)
466{
467 struct completion work;
468 int rc;
469
470 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
471 ap_msg->msg = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE, GFP_KERNEL);
472 else
473 ap_msg->msg = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE, GFP_KERNEL);
474 if (!ap_msg->msg)
475 return -ENOMEM;
476 ap_msg->receive = zcrypt_cex2a_receive;
477 ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
478 atomic_inc_return(&zcrypt_step);
479 ap_msg->private = &work;
480 rc = ICAMEX_msg_to_type50MEX_msg(zq, ap_msg, mex);
481 if (rc)
482 goto out;
483 init_completion(&work);
484 rc = ap_queue_message(zq->queue, ap_msg);
485 if (rc)
486 goto out;
487 rc = wait_for_completion_interruptible(&work);
488 if (rc == 0) {
489 rc = ap_msg->rc;
490 if (rc == 0)
491 rc = convert_response_cex2a(zq, ap_msg,
492 mex->outputdata,
493 mex->outputdatalength);
494 } else
495
496 ap_cancel_message(zq->queue, ap_msg);
497out:
498 ap_msg->private = NULL;
499 return rc;
500}
501
502
503
504
505
506
507
508
509static long zcrypt_cex2a_modexpo_crt(struct zcrypt_queue *zq,
510 struct ica_rsa_modexpo_crt *crt,
511 struct ap_message *ap_msg)
512{
513 struct completion work;
514 int rc;
515
516 if (zq->zcard->user_space_type == ZCRYPT_CEX2A)
517 ap_msg->msg = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE, GFP_KERNEL);
518 else
519 ap_msg->msg = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE, GFP_KERNEL);
520 if (!ap_msg->msg)
521 return -ENOMEM;
522 ap_msg->receive = zcrypt_cex2a_receive;
523 ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
524 atomic_inc_return(&zcrypt_step);
525 ap_msg->private = &work;
526 rc = ICACRT_msg_to_type50CRT_msg(zq, ap_msg, crt);
527 if (rc)
528 goto out;
529 init_completion(&work);
530 rc = ap_queue_message(zq->queue, ap_msg);
531 if (rc)
532 goto out;
533 rc = wait_for_completion_interruptible(&work);
534 if (rc == 0) {
535 rc = ap_msg->rc;
536 if (rc == 0)
537 rc = convert_response_cex2a(zq, ap_msg,
538 crt->outputdata,
539 crt->outputdatalength);
540 } else
541
542 ap_cancel_message(zq->queue, ap_msg);
543out:
544 ap_msg->private = NULL;
545 return rc;
546}
547
548
549
550
551static struct zcrypt_ops zcrypt_msgtype50_ops = {
552 .rsa_modexpo = zcrypt_cex2a_modexpo,
553 .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
554 .owner = THIS_MODULE,
555 .name = MSGTYPE50_NAME,
556 .variant = MSGTYPE50_VARIANT_DEFAULT,
557};
558
559void __init zcrypt_msgtype50_init(void)
560{
561 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
562}
563
564void __exit zcrypt_msgtype50_exit(void)
565{
566 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
567}
568