zcrypt_cex2a.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * linux/drivers/s390/crypto/zcrypt_cex2a.c
  3. *
  4. * zcrypt 2.1.0
  5. *
  6. * Copyright (C) 2001, 2006 IBM Corporation
  7. * Author(s): Robert Burroughs
  8. * Eric Rossman (edrossma@us.ibm.com)
  9. *
  10. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  11. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  12. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/err.h>
  31. #include <asm/atomic.h>
  32. #include <asm/uaccess.h>
  33. #include "ap_bus.h"
  34. #include "zcrypt_api.h"
  35. #include "zcrypt_error.h"
  36. #include "zcrypt_cex2a.h"
  37. #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
  38. #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
  39. #define CEX2A_SPEED_RATING 970
  40. #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
  41. #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
  42. #define CEX2A_CLEANUP_TIME (15*HZ)
  43. static struct ap_device_id zcrypt_cex2a_ids[] = {
  44. { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
  45. { /* end of list */ },
  46. };
  47. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  48. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
  49. MODULE_AUTHOR("IBM Corporation");
  50. MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, "
  51. "Copyright 2001, 2006 IBM Corporation");
  52. MODULE_LICENSE("GPL");
  53. #endif
  54. static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
  55. static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
  56. static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
  57. struct ap_message *);
  58. static struct ap_driver zcrypt_cex2a_driver = {
  59. .probe = zcrypt_cex2a_probe,
  60. .remove = zcrypt_cex2a_remove,
  61. .receive = zcrypt_cex2a_receive,
  62. .ids = zcrypt_cex2a_ids,
  63. .request_timeout = CEX2A_CLEANUP_TIME,
  64. };
  65. /**
  66. * Convert a ICAMEX message to a type50 MEX message.
  67. *
  68. * @zdev: crypto device pointer
  69. * @zreq: crypto request pointer
  70. * @mex: pointer to user input data
  71. *
  72. * Returns 0 on success or -EFAULT.
  73. */
  74. static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
  75. struct ap_message *ap_msg,
  76. struct ica_rsa_modexpo *mex)
  77. {
  78. unsigned char *mod, *exp, *inp;
  79. int mod_len;
  80. mod_len = mex->inputdatalength;
  81. if (mod_len <= 128) {
  82. struct type50_meb1_msg *meb1 = ap_msg->message;
  83. memset(meb1, 0, sizeof(*meb1));
  84. ap_msg->length = sizeof(*meb1);
  85. meb1->header.msg_type_code = TYPE50_TYPE_CODE;
  86. meb1->header.msg_len = sizeof(*meb1);
  87. meb1->keyblock_type = TYPE50_MEB1_FMT;
  88. mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
  89. exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
  90. inp = meb1->message + sizeof(meb1->message) - mod_len;
  91. } else {
  92. struct type50_meb2_msg *meb2 = ap_msg->message;
  93. memset(meb2, 0, sizeof(*meb2));
  94. ap_msg->length = sizeof(*meb2);
  95. meb2->header.msg_type_code = TYPE50_TYPE_CODE;
  96. meb2->header.msg_len = sizeof(*meb2);
  97. meb2->keyblock_type = TYPE50_MEB2_FMT;
  98. mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
  99. exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
  100. inp = meb2->message + sizeof(meb2->message) - mod_len;
  101. }
  102. if (copy_from_user(mod, mex->n_modulus, mod_len) ||
  103. copy_from_user(exp, mex->b_key, mod_len) ||
  104. copy_from_user(inp, mex->inputdata, mod_len))
  105. return -EFAULT;
  106. return 0;
  107. }
  108. /**
  109. * Convert a ICACRT message to a type50 CRT message.
  110. *
  111. * @zdev: crypto device pointer
  112. * @zreq: crypto request pointer
  113. * @crt: pointer to user input data
  114. *
  115. * Returns 0 on success or -EFAULT.
  116. */
  117. static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
  118. struct ap_message *ap_msg,
  119. struct ica_rsa_modexpo_crt *crt)
  120. {
  121. int mod_len, short_len, long_len, long_offset;
  122. unsigned char *p, *q, *dp, *dq, *u, *inp;
  123. mod_len = crt->inputdatalength;
  124. short_len = mod_len / 2;
  125. long_len = mod_len / 2 + 8;
  126. /*
  127. * CEX2A cannot handle p, dp, or U > 128 bytes.
  128. * If we have one of these, we need to do extra checking.
  129. */
  130. if (long_len > 128) {
  131. /*
  132. * zcrypt_rsa_crt already checked for the leading
  133. * zeroes of np_prime, bp_key and u_mult_inc.
  134. */
  135. long_offset = long_len - 128;
  136. long_len = 128;
  137. } else
  138. long_offset = 0;
  139. /*
  140. * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
  141. * the larger message structure.
  142. */
  143. if (long_len <= 64) {
  144. struct type50_crb1_msg *crb1 = ap_msg->message;
  145. memset(crb1, 0, sizeof(*crb1));
  146. ap_msg->length = sizeof(*crb1);
  147. crb1->header.msg_type_code = TYPE50_TYPE_CODE;
  148. crb1->header.msg_len = sizeof(*crb1);
  149. crb1->keyblock_type = TYPE50_CRB1_FMT;
  150. p = crb1->p + sizeof(crb1->p) - long_len;
  151. q = crb1->q + sizeof(crb1->q) - short_len;
  152. dp = crb1->dp + sizeof(crb1->dp) - long_len;
  153. dq = crb1->dq + sizeof(crb1->dq) - short_len;
  154. u = crb1->u + sizeof(crb1->u) - long_len;
  155. inp = crb1->message + sizeof(crb1->message) - mod_len;
  156. } else {
  157. struct type50_crb2_msg *crb2 = ap_msg->message;
  158. memset(crb2, 0, sizeof(*crb2));
  159. ap_msg->length = sizeof(*crb2);
  160. crb2->header.msg_type_code = TYPE50_TYPE_CODE;
  161. crb2->header.msg_len = sizeof(*crb2);
  162. crb2->keyblock_type = TYPE50_CRB2_FMT;
  163. p = crb2->p + sizeof(crb2->p) - long_len;
  164. q = crb2->q + sizeof(crb2->q) - short_len;
  165. dp = crb2->dp + sizeof(crb2->dp) - long_len;
  166. dq = crb2->dq + sizeof(crb2->dq) - short_len;
  167. u = crb2->u + sizeof(crb2->u) - long_len;
  168. inp = crb2->message + sizeof(crb2->message) - mod_len;
  169. }
  170. if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
  171. copy_from_user(q, crt->nq_prime, short_len) ||
  172. copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
  173. copy_from_user(dq, crt->bq_key, short_len) ||
  174. copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
  175. copy_from_user(inp, crt->inputdata, mod_len))
  176. return -EFAULT;
  177. return 0;
  178. }
  179. /**
  180. * Copy results from a type 80 reply message back to user space.
  181. *
  182. * @zdev: crypto device pointer
  183. * @reply: reply AP message.
  184. * @data: pointer to user output data
  185. * @length: size of user output data
  186. *
  187. * Returns 0 on success or -EFAULT.
  188. */
  189. static int convert_type80(struct zcrypt_device *zdev,
  190. struct ap_message *reply,
  191. char __user *outputdata,
  192. unsigned int outputdatalength)
  193. {
  194. struct type80_hdr *t80h = reply->message;
  195. unsigned char *data;
  196. if (t80h->len < sizeof(*t80h) + outputdatalength) {
  197. /* The result is too short, the CEX2A card may not do that.. */
  198. zdev->online = 0;
  199. return -EAGAIN; /* repeat the request on a different device. */
  200. }
  201. BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
  202. data = reply->message + t80h->len - outputdatalength;
  203. if (copy_to_user(outputdata, data, outputdatalength))
  204. return -EFAULT;
  205. return 0;
  206. }
  207. static int convert_response(struct zcrypt_device *zdev,
  208. struct ap_message *reply,
  209. char __user *outputdata,
  210. unsigned int outputdatalength)
  211. {
  212. /* Response type byte is the second byte in the response. */
  213. switch (((unsigned char *) reply->message)[1]) {
  214. case TYPE82_RSP_CODE:
  215. case TYPE88_RSP_CODE:
  216. return convert_error(zdev, reply);
  217. case TYPE80_RSP_CODE:
  218. return convert_type80(zdev, reply,
  219. outputdata, outputdatalength);
  220. default: /* Unknown response type, this should NEVER EVER happen */
  221. PRINTK("Unrecognized Message Header: %08x%08x\n",
  222. *(unsigned int *) reply->message,
  223. *(unsigned int *) (reply->message+4));
  224. zdev->online = 0;
  225. return -EAGAIN; /* repeat the request on a different device. */
  226. }
  227. }
  228. /**
  229. * This function is called from the AP bus code after a crypto request
  230. * "msg" has finished with the reply message "reply".
  231. * It is called from tasklet context.
  232. * @ap_dev: pointer to the AP device
  233. * @msg: pointer to the AP message
  234. * @reply: pointer to the AP reply message
  235. */
  236. static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
  237. struct ap_message *msg,
  238. struct ap_message *reply)
  239. {
  240. static struct error_hdr error_reply = {
  241. .type = TYPE82_RSP_CODE,
  242. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  243. };
  244. struct type80_hdr *t80h = reply->message;
  245. int length;
  246. /* Copy the reply message to the request message buffer. */
  247. if (IS_ERR(reply))
  248. memcpy(msg->message, &error_reply, sizeof(error_reply));
  249. else if (t80h->type == TYPE80_RSP_CODE) {
  250. length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len);
  251. memcpy(msg->message, reply->message, length);
  252. } else
  253. memcpy(msg->message, reply->message, sizeof error_reply);
  254. complete((struct completion *) msg->private);
  255. }
  256. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  257. /**
  258. * The request distributor calls this function if it picked the CEX2A
  259. * device to handle a modexpo request.
  260. * @zdev: pointer to zcrypt_device structure that identifies the
  261. * CEX2A device to the request distributor
  262. * @mex: pointer to the modexpo request buffer
  263. */
  264. static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
  265. struct ica_rsa_modexpo *mex)
  266. {
  267. struct ap_message ap_msg;
  268. struct completion work;
  269. int rc;
  270. ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  271. if (!ap_msg.message)
  272. return -ENOMEM;
  273. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  274. atomic_inc_return(&zcrypt_step);
  275. ap_msg.private = &work;
  276. rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
  277. if (rc)
  278. goto out_free;
  279. init_completion(&work);
  280. ap_queue_message(zdev->ap_dev, &ap_msg);
  281. rc = wait_for_completion_interruptible(&work);
  282. if (rc == 0)
  283. rc = convert_response(zdev, &ap_msg, mex->outputdata,
  284. mex->outputdatalength);
  285. else
  286. /* Signal pending. */
  287. ap_cancel_message(zdev->ap_dev, &ap_msg);
  288. out_free:
  289. kfree(ap_msg.message);
  290. return rc;
  291. }
  292. /**
  293. * The request distributor calls this function if it picked the CEX2A
  294. * device to handle a modexpo_crt request.
  295. * @zdev: pointer to zcrypt_device structure that identifies the
  296. * CEX2A device to the request distributor
  297. * @crt: pointer to the modexpoc_crt request buffer
  298. */
  299. static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
  300. struct ica_rsa_modexpo_crt *crt)
  301. {
  302. struct ap_message ap_msg;
  303. struct completion work;
  304. int rc;
  305. ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  306. if (!ap_msg.message)
  307. return -ENOMEM;
  308. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  309. atomic_inc_return(&zcrypt_step);
  310. ap_msg.private = &work;
  311. rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
  312. if (rc)
  313. goto out_free;
  314. init_completion(&work);
  315. ap_queue_message(zdev->ap_dev, &ap_msg);
  316. rc = wait_for_completion_interruptible(&work);
  317. if (rc == 0)
  318. rc = convert_response(zdev, &ap_msg, crt->outputdata,
  319. crt->outputdatalength);
  320. else
  321. /* Signal pending. */
  322. ap_cancel_message(zdev->ap_dev, &ap_msg);
  323. out_free:
  324. kfree(ap_msg.message);
  325. return rc;
  326. }
  327. /**
  328. * The crypto operations for a CEX2A card.
  329. */
  330. static struct zcrypt_ops zcrypt_cex2a_ops = {
  331. .rsa_modexpo = zcrypt_cex2a_modexpo,
  332. .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
  333. };
  334. /**
  335. * Probe function for CEX2A cards. It always accepts the AP device
  336. * since the bus_match already checked the hardware type.
  337. * @ap_dev: pointer to the AP device.
  338. */
  339. static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
  340. {
  341. struct zcrypt_device *zdev;
  342. int rc;
  343. zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
  344. if (!zdev)
  345. return -ENOMEM;
  346. zdev->ap_dev = ap_dev;
  347. zdev->ops = &zcrypt_cex2a_ops;
  348. zdev->online = 1;
  349. zdev->user_space_type = ZCRYPT_CEX2A;
  350. zdev->type_string = "CEX2A";
  351. zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
  352. zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
  353. zdev->short_crt = 1;
  354. zdev->speed_rating = CEX2A_SPEED_RATING;
  355. ap_dev->reply = &zdev->reply;
  356. ap_dev->private = zdev;
  357. rc = zcrypt_device_register(zdev);
  358. if (rc)
  359. goto out_free;
  360. return 0;
  361. out_free:
  362. ap_dev->private = NULL;
  363. zcrypt_device_free(zdev);
  364. return rc;
  365. }
  366. /**
  367. * This is called to remove the extended CEX2A driver information
  368. * if an AP device is removed.
  369. */
  370. static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
  371. {
  372. struct zcrypt_device *zdev = ap_dev->private;
  373. zcrypt_device_unregister(zdev);
  374. }
  375. int __init zcrypt_cex2a_init(void)
  376. {
  377. return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
  378. }
  379. void __exit zcrypt_cex2a_exit(void)
  380. {
  381. ap_driver_unregister(&zcrypt_cex2a_driver);
  382. }
  383. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  384. module_init(zcrypt_cex2a_init);
  385. module_exit(zcrypt_cex2a_exit);
  386. #endif