Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zcrypt_msgtype50.c
Go to the documentation of this file.
1 /*
2  * zcrypt 2.1.0
3  *
4  * Copyright IBM Corp. 2001, 2012
5  * Author(s): Robert Burroughs
6  * Eric Rossman ([email protected])
7  *
8  * Hotplug & misc device support: Jochen Roehrig ([email protected])
9  * Major cleanup & driver split: Martin Schwidefsky <[email protected]>
10  * Ralph Wuerthner <[email protected]>
11  * MSGTYPE restruct: Holger Dengler <[email protected]>
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 
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;
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 
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 
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);
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);
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);
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 
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, long_len, long_offset, limit;
245  unsigned char *p, *q, *dp, *dq, *u, *inp;
246 
247  mod_len = crt->inputdatalength;
248  short_len = mod_len / 2;
249  long_len = mod_len / 2 + 8;
250 
251  /*
252  * CEX2A cannot handle p, dp, or U > 128 bytes.
253  * If we have one of these, we need to do extra checking.
254  * For CEX3A the limit is 256 bytes.
255  */
256  if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)
257  limit = 256;
258  else
259  limit = 128;
260 
261  if (long_len > limit) {
262  /*
263  * zcrypt_rsa_crt already checked for the leading
264  * zeroes of np_prime, bp_key and u_mult_inc.
265  */
266  long_offset = long_len - limit;
267  long_len = limit;
268  } else
269  long_offset = 0;
270 
271  /*
272  * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
273  * the larger message structure.
274  */
275  if (long_len <= 64) {
276  struct type50_crb1_msg *crb1 = ap_msg->message;
277  memset(crb1, 0, sizeof(*crb1));
278  ap_msg->length = sizeof(*crb1);
279  crb1->header.msg_type_code = TYPE50_TYPE_CODE;
280  crb1->header.msg_len = sizeof(*crb1);
282  p = crb1->p + sizeof(crb1->p) - long_len;
283  q = crb1->q + sizeof(crb1->q) - short_len;
284  dp = crb1->dp + sizeof(crb1->dp) - long_len;
285  dq = crb1->dq + sizeof(crb1->dq) - short_len;
286  u = crb1->u + sizeof(crb1->u) - long_len;
287  inp = crb1->message + sizeof(crb1->message) - mod_len;
288  } else if (long_len <= 128) {
289  struct type50_crb2_msg *crb2 = ap_msg->message;
290  memset(crb2, 0, sizeof(*crb2));
291  ap_msg->length = sizeof(*crb2);
292  crb2->header.msg_type_code = TYPE50_TYPE_CODE;
293  crb2->header.msg_len = sizeof(*crb2);
295  p = crb2->p + sizeof(crb2->p) - long_len;
296  q = crb2->q + sizeof(crb2->q) - short_len;
297  dp = crb2->dp + sizeof(crb2->dp) - long_len;
298  dq = crb2->dq + sizeof(crb2->dq) - short_len;
299  u = crb2->u + sizeof(crb2->u) - long_len;
300  inp = crb2->message + sizeof(crb2->message) - mod_len;
301  } else {
302  /* long_len >= 256 */
303  struct type50_crb3_msg *crb3 = ap_msg->message;
304  memset(crb3, 0, sizeof(*crb3));
305  ap_msg->length = sizeof(*crb3);
306  crb3->header.msg_type_code = TYPE50_TYPE_CODE;
307  crb3->header.msg_len = sizeof(*crb3);
309  p = crb3->p + sizeof(crb3->p) - long_len;
310  q = crb3->q + sizeof(crb3->q) - short_len;
311  dp = crb3->dp + sizeof(crb3->dp) - long_len;
312  dq = crb3->dq + sizeof(crb3->dq) - short_len;
313  u = crb3->u + sizeof(crb3->u) - long_len;
314  inp = crb3->message + sizeof(crb3->message) - mod_len;
315  }
316 
317  if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
318  copy_from_user(q, crt->nq_prime, short_len) ||
319  copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
320  copy_from_user(dq, crt->bq_key, short_len) ||
321  copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
322  copy_from_user(inp, crt->inputdata, mod_len))
323  return -EFAULT;
324 
325  return 0;
326 }
327 
338 static int convert_type80(struct zcrypt_device *zdev,
339  struct ap_message *reply,
340  char __user *outputdata,
341  unsigned int outputdatalength)
342 {
343  struct type80_hdr *t80h = reply->message;
344  unsigned char *data;
345 
346  if (t80h->len < sizeof(*t80h) + outputdatalength) {
347  /* The result is too short, the CEX2A card may not do that.. */
348  zdev->online = 0;
349  return -EAGAIN; /* repeat the request on a different device. */
350  }
351  if (zdev->user_space_type == ZCRYPT_CEX2A)
353  else
355  data = reply->message + t80h->len - outputdatalength;
356  if (copy_to_user(outputdata, data, outputdatalength))
357  return -EFAULT;
358  return 0;
359 }
360 
361 static int convert_response(struct zcrypt_device *zdev,
362  struct ap_message *reply,
363  char __user *outputdata,
364  unsigned int outputdatalength)
365 {
366  /* Response type byte is the second byte in the response. */
367  switch (((unsigned char *) reply->message)[1]) {
368  case TYPE82_RSP_CODE:
369  case TYPE88_RSP_CODE:
370  return convert_error(zdev, reply);
371  case TYPE80_RSP_CODE:
372  return convert_type80(zdev, reply,
373  outputdata, outputdatalength);
374  default: /* Unknown response type, this should NEVER EVER happen */
375  zdev->online = 0;
376  return -EAGAIN; /* repeat the request on a different device. */
377  }
378 }
379 
388 static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
389  struct ap_message *msg,
390  struct ap_message *reply)
391 {
392  static struct error_hdr error_reply = {
394  .reply_code = REP82_ERROR_MACHINE_FAILURE,
395  };
396  struct type80_hdr *t80h;
397  int length;
398 
399  /* Copy the reply message to the request message buffer. */
400  if (IS_ERR(reply)) {
401  memcpy(msg->message, &error_reply, sizeof(error_reply));
402  goto out;
403  }
404  t80h = reply->message;
405  if (t80h->type == TYPE80_RSP_CODE) {
406  if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
407  length = min_t(int,
409  else
410  length = min_t(int,
412  memcpy(msg->message, reply->message, length);
413  } else
414  memcpy(msg->message, reply->message, sizeof(error_reply));
415 out:
416  complete((struct completion *) msg->private);
417 }
418 
419 static atomic_t zcrypt_step = ATOMIC_INIT(0);
420 
428 static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
429  struct ica_rsa_modexpo *mex)
430 {
431  struct ap_message ap_msg;
432  struct completion work;
433  int rc;
434 
435  ap_init_message(&ap_msg);
436  if (zdev->user_space_type == ZCRYPT_CEX2A)
438  GFP_KERNEL);
439  else
441  GFP_KERNEL);
442  if (!ap_msg.message)
443  return -ENOMEM;
444  ap_msg.receive = zcrypt_cex2a_receive;
445  ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
446  atomic_inc_return(&zcrypt_step);
447  ap_msg.private = &work;
448  rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
449  if (rc)
450  goto out_free;
451  init_completion(&work);
452  ap_queue_message(zdev->ap_dev, &ap_msg);
454  if (rc == 0)
455  rc = convert_response(zdev, &ap_msg, mex->outputdata,
456  mex->outputdatalength);
457  else
458  /* Signal pending. */
459  ap_cancel_message(zdev->ap_dev, &ap_msg);
460 out_free:
461  kfree(ap_msg.message);
462  return rc;
463 }
464 
472 static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
473  struct ica_rsa_modexpo_crt *crt)
474 {
475  struct ap_message ap_msg;
476  struct completion work;
477  int rc;
478 
479  ap_init_message(&ap_msg);
480  if (zdev->user_space_type == ZCRYPT_CEX2A)
482  GFP_KERNEL);
483  else
485  GFP_KERNEL);
486  if (!ap_msg.message)
487  return -ENOMEM;
488  ap_msg.receive = zcrypt_cex2a_receive;
489  ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
490  atomic_inc_return(&zcrypt_step);
491  ap_msg.private = &work;
492  rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
493  if (rc)
494  goto out_free;
495  init_completion(&work);
496  ap_queue_message(zdev->ap_dev, &ap_msg);
498  if (rc == 0)
499  rc = convert_response(zdev, &ap_msg, crt->outputdata,
500  crt->outputdatalength);
501  else
502  /* Signal pending. */
503  ap_cancel_message(zdev->ap_dev, &ap_msg);
504 out_free:
505  kfree(ap_msg.message);
506  return rc;
507 }
508 
512 static struct zcrypt_ops zcrypt_msgtype50_ops = {
513  .rsa_modexpo = zcrypt_cex2a_modexpo,
514  .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
515  .owner = THIS_MODULE,
516  .variant = MSGTYPE50_VARIANT_DEFAULT,
517 };
518 
520 {
521  zcrypt_msgtype_register(&zcrypt_msgtype50_ops);
522  return 0;
523 }
524 
525 void __exit zcrypt_msgtype50_exit(void)
526 {
527  zcrypt_msgtype_unregister(&zcrypt_msgtype50_ops);
528 }
529