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#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>
34
35#include "ap_bus.h"
36#include "zcrypt_api.h"
37#include "zcrypt_error.h"
38#include "zcrypt_msgtype50.h"
39
40#define CEX3A_MAX_MOD_SIZE 512
41
42#define CEX2A_MAX_RESPONSE_SIZE 0x110
43
44#define CEX3A_MAX_RESPONSE_SIZE 0x210
45
46
47
48MODULE_AUTHOR("IBM Corporation");
49MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
50 "Copyright IBM Corp. 2001, 2012");
51MODULE_LICENSE("GPL");
52
53static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
54 struct ap_message *);
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
173
174
175
176
177
178
179
180
181
182static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
183 struct ap_message *ap_msg,
184 struct ica_rsa_modexpo *mex)
185{
186 unsigned char *mod, *exp, *inp;
187 int mod_len;
188
189 mod_len = mex->inputdatalength;
190
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;
211 } else {
212
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;
222 }
223
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))
227 return -EFAULT;
228 return 0;
229}
230
231
232
233
234
235
236
237
238
239
240static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
241 struct ap_message *ap_msg,
242 struct ica_rsa_modexpo_crt *crt)
243{
244 int mod_len, short_len;
245 unsigned char *p, *q, *dp, *dq, *u, *inp;
246
247 mod_len = crt->inputdatalength;
248 short_len = mod_len / 2;
249
250
251
252
253
254
255
256 if (mod_len <= 128) {
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) {
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) &&
283 (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)) {
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;
296 } else
297 return -EINVAL;
298
299
300
301
302
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))
309 return -EFAULT;
310
311 return 0;
312}
313
314
315
316
317
318
319
320
321
322
323
324static int convert_type80(struct zcrypt_device *zdev,
325 struct ap_message *reply,
326 char __user *outputdata,
327 unsigned int outputdatalength)
328{
329 struct type80_hdr *t80h = reply->message;
330 unsigned char *data;
331
332 if (t80h->len < sizeof(*t80h) + outputdatalength) {
333
334 zdev->online = 0;
335 return -EAGAIN;
336 }
337 if (zdev->user_space_type == ZCRYPT_CEX2A)
338 BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
339 else
340 BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
341 data = reply->message + t80h->len - outputdatalength;
342 if (copy_to_user(outputdata, data, outputdatalength))
343 return -EFAULT;
344 return 0;
345}
346
347static int convert_response(struct zcrypt_device *zdev,
348 struct ap_message *reply,
349 char __user *outputdata,
350 unsigned int outputdatalength)
351{
352
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:
361 zdev->online = 0;
362 return -EAGAIN;
363 }
364}
365
366
367
368
369
370
371
372
373
374static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
375 struct ap_message *msg,
376 struct ap_message *reply)
377{
378 static struct error_hdr error_reply = {
379 .type = TYPE82_RSP_CODE,
380 .reply_code = REP82_ERROR_MACHINE_FAILURE,
381 };
382 struct type80_hdr *t80h;
383 int length;
384
385
386 if (IS_ERR(reply)) {
387 memcpy(msg->message, &error_reply, sizeof(error_reply));
388 goto out;
389 }
390 t80h = reply->message;
391 if (t80h->type == TYPE80_RSP_CODE) {
392 if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
393 length = min_t(int,
394 CEX2A_MAX_RESPONSE_SIZE, t80h->len);
395 else
396 length = min_t(int,
397 CEX3A_MAX_RESPONSE_SIZE, t80h->len);
398 memcpy(msg->message, reply->message, length);
399 } else
400 memcpy(msg->message, reply->message, sizeof(error_reply));
401out:
402 complete((struct completion *) msg->private);
403}
404
405static atomic_t zcrypt_step = ATOMIC_INIT(0);
406
407
408
409
410
411
412
413
414static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
415 struct ica_rsa_modexpo *mex)
416{
417 struct ap_message ap_msg;
418 struct completion work;
419 int rc;
420
421 ap_init_message(&ap_msg);
422 if (zdev->user_space_type == ZCRYPT_CEX2A)
423 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
424 GFP_KERNEL);
425 else
426 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
427 GFP_KERNEL);
428 if (!ap_msg.message)
429 return -ENOMEM;
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);
435 if (rc)
436 goto out_free;
437 init_completion(&work);
438 ap_queue_message(zdev->ap_dev, &ap_msg);
439 rc = wait_for_completion_interruptible(&work);
440 if (rc == 0)
441 rc = convert_response(zdev, &ap_msg, mex->outputdata,
442 mex->outputdatalength);
443 else
444
445 ap_cancel_message(zdev->ap_dev, &ap_msg);
446out_free:
447 kfree(ap_msg.message);
448 return rc;
449}
450
451
452
453
454
455
456
457
458static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
459 struct ica_rsa_modexpo_crt *crt)
460{
461 struct ap_message ap_msg;
462 struct completion work;
463 int rc;
464
465 ap_init_message(&ap_msg);
466 if (zdev->user_space_type == ZCRYPT_CEX2A)
467 ap_msg.message = kmalloc(MSGTYPE50_CRB2_MAX_MSG_SIZE,
468 GFP_KERNEL);
469 else
470 ap_msg.message = kmalloc(MSGTYPE50_CRB3_MAX_MSG_SIZE,
471 GFP_KERNEL);
472 if (!ap_msg.message)
473 return -ENOMEM;
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);
479 if (rc)
480 goto out_free;
481 init_completion(&work);
482 ap_queue_message(zdev->ap_dev, &ap_msg);
483 rc = wait_for_completion_interruptible(&work);
484 if (rc == 0)
485 rc = convert_response(zdev, &ap_msg, crt->outputdata,
486 crt->outputdatalength);
487 else
488
489 ap_cancel_message(zdev->ap_dev, &ap_msg);
490out_free:
491 kfree(ap_msg.message);
492 return rc;
493}
494
495
496
497
498static 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,
503};
504
505int __init zcrypt_msgtype50_init(void)
506{
507 zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
508 return 0;
509}
510
511void __exit zcrypt_msgtype50_exit(void)
512{
513 zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
514}
515
516module_init(zcrypt_msgtype50_init);
517module_exit(zcrypt_msgtype50_exit);
518