]> Pileus Git - ~andy/linux/blob - drivers/s390/crypto/zcrypt_msgtype50.c
Merge branch 'drm-fixes-3.11' of git://people.freedesktop.org/~agd5f/linux
[~andy/linux] / drivers / s390 / crypto / zcrypt_msgtype50.c
1 /*
2  *  zcrypt 2.1.0
3  *
4  *  Copyright IBM Corp. 2001, 2012
5  *  Author(s): Robert Burroughs
6  *             Eric Rossman (edrossma@us.ibm.com)
7  *
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>
12  *
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)
16  * any later version.
17  *
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.
22  *
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.
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     /* 4096 bits    */
41
42 #define CEX2A_MAX_RESPONSE_SIZE 0x110   /* max outputdatalength + type80_hdr */
43
44 #define CEX3A_MAX_RESPONSE_SIZE 0x210   /* 512 bit modulus
45                                          * (max outputdatalength) +
46                                          * type80_hdr*/
47
48 MODULE_AUTHOR("IBM Corporation");
49 MODULE_DESCRIPTION("Cryptographic Accelerator (message type 50), " \
50                    "Copyright IBM Corp. 2001, 2012");
51 MODULE_LICENSE("GPL");
52
53 static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
54                                  struct ap_message *);
55
56 /**
57  * The type 50 message family is associated with a CEX2A card.
58  *
59  * The four members of the family are described below.
60  *
61  * Note that all unsigned char arrays are right-justified and left-padded
62  * with zeroes.
63  *
64  * Note that all reserved fields must be zeroes.
65  */
66 struct type50_hdr {
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;
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 /* 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];
92 } __packed;
93
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];
102 } __packed;
103
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];
112 } __packed;
113
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];
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 /* 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];
138 } __packed;
139
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];
151 } __packed;
152
153 /**
154  * The type 80 response family is associated with a CEX2A card.
155  *
156  * Note that all unsigned char arrays are right-justified and left-padded
157  * with zeroes.
158  *
159  * Note that all reserved fields must be zeroes.
160  */
161
162 #define TYPE80_RSP_CODE 0x80
163
164 struct type80_hdr {
165         unsigned char   reserved1;
166         unsigned char   type;           /* 0x80 */
167         unsigned short  len;
168         unsigned char   code;           /* 0x00 */
169         unsigned char   reserved2[3];
170         unsigned char   reserved3[8];
171 } __packed;
172
173 /**
174  * Convert a ICAMEX message to a type50 MEX message.
175  *
176  * @zdev: crypto device pointer
177  * @zreq: crypto request pointer
178  * @mex: pointer to user input data
179  *
180  * Returns 0 on success or -EFAULT.
181  */
182 static 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                 /* 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;
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  * Convert a ICACRT message to a type50 CRT message.
233  *
234  * @zdev: crypto device pointer
235  * @zreq: crypto request pointer
236  * @crt: pointer to user input data
237  *
238  * Returns 0 on success or -EFAULT.
239  */
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)
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          * 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).
255          */
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;
296         } else
297                 return -EINVAL;
298
299         /*
300          * correct the offset of p, bp and mult_inv according zcrypt.h
301          * block size right aligned (skip the first byte)
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  * Copy results from a type 80 reply message back to user space.
316  *
317  * @zdev: crypto device pointer
318  * @reply: reply AP message.
319  * @data: pointer to user output data
320  * @length: size of user output data
321  *
322  * Returns 0 on success or -EFAULT.
323  */
324 static 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                 /* The result is too short, the CEX2A card may not do that.. */
334                 zdev->online = 0;
335                 return -EAGAIN; /* repeat the request on a different device. */
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
347 static int convert_response(struct zcrypt_device *zdev,
348                             struct ap_message *reply,
349                             char __user *outputdata,
350                             unsigned int outputdatalength)
351 {
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 */
361                 zdev->online = 0;
362                 return -EAGAIN; /* repeat the request on a different device. */
363         }
364 }
365
366 /**
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
373  */
374 static 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         /* Copy the reply message to the request message buffer. */
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));
401 out:
402         complete((struct completion *) msg->private);
403 }
404
405 static atomic_t zcrypt_step = ATOMIC_INIT(0);
406
407 /**
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
413  */
414 static 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                 /* Signal pending. */
445                 ap_cancel_message(zdev->ap_dev, &ap_msg);
446 out_free:
447         kfree(ap_msg.message);
448         return rc;
449 }
450
451 /**
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
457  */
458 static 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                 /* Signal pending. */
489                 ap_cancel_message(zdev->ap_dev, &ap_msg);
490 out_free:
491         kfree(ap_msg.message);
492         return rc;
493 }
494
495 /**
496  * The crypto operations for message type 50.
497  */
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,
503 };
504
505 int __init zcrypt_msgtype50_init(void)
506 {
507         zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
508         return 0;
509 }
510
511 void __exit zcrypt_msgtype50_exit(void)
512 {
513         zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
514 }
515
516 module_init(zcrypt_msgtype50_init);
517 module_exit(zcrypt_msgtype50_exit);