zcrypt_pcica.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * zcrypt 2.1.0
  3. *
  4. * Copyright IBM Corp. 2001, 2006
  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. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/err.h>
  30. #include <linux/atomic.h>
  31. #include <asm/uaccess.h>
  32. #include "ap_bus.h"
  33. #include "zcrypt_api.h"
  34. #include "zcrypt_error.h"
  35. #include "zcrypt_pcica.h"
  36. #define PCICA_MIN_MOD_SIZE 1 /* 8 bits */
  37. #define PCICA_MAX_MOD_SIZE 256 /* 2048 bits */
  38. #define PCICA_SPEED_RATING 2800
  39. #define PCICA_MAX_MESSAGE_SIZE 0x3a0 /* sizeof(struct type4_lcr) */
  40. #define PCICA_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
  41. #define PCICA_CLEANUP_TIME (15*HZ)
  42. static struct ap_device_id zcrypt_pcica_ids[] = {
  43. { AP_DEVICE(AP_DEVICE_TYPE_PCICA) },
  44. { /* end of list */ },
  45. };
  46. MODULE_DEVICE_TABLE(ap, zcrypt_pcica_ids);
  47. MODULE_AUTHOR("IBM Corporation");
  48. MODULE_DESCRIPTION("PCICA Cryptographic Coprocessor device driver, "
  49. "Copyright IBM Corp. 2001, 2006");
  50. MODULE_LICENSE("GPL");
  51. static int zcrypt_pcica_probe(struct ap_device *ap_dev);
  52. static void zcrypt_pcica_remove(struct ap_device *ap_dev);
  53. static void zcrypt_pcica_receive(struct ap_device *, struct ap_message *,
  54. struct ap_message *);
  55. static struct ap_driver zcrypt_pcica_driver = {
  56. .probe = zcrypt_pcica_probe,
  57. .remove = zcrypt_pcica_remove,
  58. .ids = zcrypt_pcica_ids,
  59. .request_timeout = PCICA_CLEANUP_TIME,
  60. };
  61. /**
  62. * Convert a ICAMEX message to a type4 MEX message.
  63. *
  64. * @zdev: crypto device pointer
  65. * @zreq: crypto request pointer
  66. * @mex: pointer to user input data
  67. *
  68. * Returns 0 on success or -EFAULT.
  69. */
  70. static int ICAMEX_msg_to_type4MEX_msg(struct zcrypt_device *zdev,
  71. struct ap_message *ap_msg,
  72. struct ica_rsa_modexpo *mex)
  73. {
  74. unsigned char *modulus, *exponent, *message;
  75. int mod_len;
  76. mod_len = mex->inputdatalength;
  77. if (mod_len <= 128) {
  78. struct type4_sme *sme = ap_msg->message;
  79. memset(sme, 0, sizeof(*sme));
  80. ap_msg->length = sizeof(*sme);
  81. sme->header.msg_fmt = TYPE4_SME_FMT;
  82. sme->header.msg_len = sizeof(*sme);
  83. sme->header.msg_type_code = TYPE4_TYPE_CODE;
  84. sme->header.request_code = TYPE4_REQU_CODE;
  85. modulus = sme->modulus + sizeof(sme->modulus) - mod_len;
  86. exponent = sme->exponent + sizeof(sme->exponent) - mod_len;
  87. message = sme->message + sizeof(sme->message) - mod_len;
  88. } else {
  89. struct type4_lme *lme = ap_msg->message;
  90. memset(lme, 0, sizeof(*lme));
  91. ap_msg->length = sizeof(*lme);
  92. lme->header.msg_fmt = TYPE4_LME_FMT;
  93. lme->header.msg_len = sizeof(*lme);
  94. lme->header.msg_type_code = TYPE4_TYPE_CODE;
  95. lme->header.request_code = TYPE4_REQU_CODE;
  96. modulus = lme->modulus + sizeof(lme->modulus) - mod_len;
  97. exponent = lme->exponent + sizeof(lme->exponent) - mod_len;
  98. message = lme->message + sizeof(lme->message) - mod_len;
  99. }
  100. if (copy_from_user(modulus, mex->n_modulus, mod_len) ||
  101. copy_from_user(exponent, mex->b_key, mod_len) ||
  102. copy_from_user(message, mex->inputdata, mod_len))
  103. return -EFAULT;
  104. return 0;
  105. }
  106. /**
  107. * Convert a ICACRT message to a type4 CRT message.
  108. *
  109. * @zdev: crypto device pointer
  110. * @zreq: crypto request pointer
  111. * @crt: pointer to user input data
  112. *
  113. * Returns 0 on success or -EFAULT.
  114. */
  115. static int ICACRT_msg_to_type4CRT_msg(struct zcrypt_device *zdev,
  116. struct ap_message *ap_msg,
  117. struct ica_rsa_modexpo_crt *crt)
  118. {
  119. unsigned char *p, *q, *dp, *dq, *u, *inp;
  120. int mod_len, short_len, long_len;
  121. mod_len = crt->inputdatalength;
  122. short_len = mod_len / 2;
  123. long_len = mod_len / 2 + 8;
  124. if (mod_len <= 128) {
  125. struct type4_scr *scr = ap_msg->message;
  126. memset(scr, 0, sizeof(*scr));
  127. ap_msg->length = sizeof(*scr);
  128. scr->header.msg_type_code = TYPE4_TYPE_CODE;
  129. scr->header.request_code = TYPE4_REQU_CODE;
  130. scr->header.msg_fmt = TYPE4_SCR_FMT;
  131. scr->header.msg_len = sizeof(*scr);
  132. p = scr->p + sizeof(scr->p) - long_len;
  133. q = scr->q + sizeof(scr->q) - short_len;
  134. dp = scr->dp + sizeof(scr->dp) - long_len;
  135. dq = scr->dq + sizeof(scr->dq) - short_len;
  136. u = scr->u + sizeof(scr->u) - long_len;
  137. inp = scr->message + sizeof(scr->message) - mod_len;
  138. } else {
  139. struct type4_lcr *lcr = ap_msg->message;
  140. memset(lcr, 0, sizeof(*lcr));
  141. ap_msg->length = sizeof(*lcr);
  142. lcr->header.msg_type_code = TYPE4_TYPE_CODE;
  143. lcr->header.request_code = TYPE4_REQU_CODE;
  144. lcr->header.msg_fmt = TYPE4_LCR_FMT;
  145. lcr->header.msg_len = sizeof(*lcr);
  146. p = lcr->p + sizeof(lcr->p) - long_len;
  147. q = lcr->q + sizeof(lcr->q) - short_len;
  148. dp = lcr->dp + sizeof(lcr->dp) - long_len;
  149. dq = lcr->dq + sizeof(lcr->dq) - short_len;
  150. u = lcr->u + sizeof(lcr->u) - long_len;
  151. inp = lcr->message + sizeof(lcr->message) - mod_len;
  152. }
  153. if (copy_from_user(p, crt->np_prime, long_len) ||
  154. copy_from_user(q, crt->nq_prime, short_len) ||
  155. copy_from_user(dp, crt->bp_key, long_len) ||
  156. copy_from_user(dq, crt->bq_key, short_len) ||
  157. copy_from_user(u, crt->u_mult_inv, long_len) ||
  158. copy_from_user(inp, crt->inputdata, mod_len))
  159. return -EFAULT;
  160. return 0;
  161. }
  162. /**
  163. * Copy results from a type 84 reply message back to user space.
  164. *
  165. * @zdev: crypto device pointer
  166. * @reply: reply AP message.
  167. * @data: pointer to user output data
  168. * @length: size of user output data
  169. *
  170. * Returns 0 on success or -EFAULT.
  171. */
  172. static int convert_type84(struct zcrypt_device *zdev,
  173. struct ap_message *reply,
  174. char __user *outputdata,
  175. unsigned int outputdatalength)
  176. {
  177. struct type84_hdr *t84h = reply->message;
  178. char *data;
  179. if (t84h->len < sizeof(*t84h) + outputdatalength) {
  180. /* The result is too short, the PCICA card may not do that.. */
  181. zdev->online = 0;
  182. return -EAGAIN; /* repeat the request on a different device. */
  183. }
  184. BUG_ON(t84h->len > PCICA_MAX_RESPONSE_SIZE);
  185. data = reply->message + t84h->len - outputdatalength;
  186. if (copy_to_user(outputdata, data, outputdatalength))
  187. return -EFAULT;
  188. return 0;
  189. }
  190. static int convert_response(struct zcrypt_device *zdev,
  191. struct ap_message *reply,
  192. char __user *outputdata,
  193. unsigned int outputdatalength)
  194. {
  195. /* Response type byte is the second byte in the response. */
  196. switch (((unsigned char *) reply->message)[1]) {
  197. case TYPE82_RSP_CODE:
  198. case TYPE88_RSP_CODE:
  199. return convert_error(zdev, reply);
  200. case TYPE84_RSP_CODE:
  201. return convert_type84(zdev, reply,
  202. outputdata, outputdatalength);
  203. default: /* Unknown response type, this should NEVER EVER happen */
  204. zdev->online = 0;
  205. return -EAGAIN; /* repeat the request on a different device. */
  206. }
  207. }
  208. /**
  209. * This function is called from the AP bus code after a crypto request
  210. * "msg" has finished with the reply message "reply".
  211. * It is called from tasklet context.
  212. * @ap_dev: pointer to the AP device
  213. * @msg: pointer to the AP message
  214. * @reply: pointer to the AP reply message
  215. */
  216. static void zcrypt_pcica_receive(struct ap_device *ap_dev,
  217. struct ap_message *msg,
  218. struct ap_message *reply)
  219. {
  220. static struct error_hdr error_reply = {
  221. .type = TYPE82_RSP_CODE,
  222. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  223. };
  224. struct type84_hdr *t84h;
  225. int length;
  226. /* Copy the reply message to the request message buffer. */
  227. if (IS_ERR(reply)) {
  228. memcpy(msg->message, &error_reply, sizeof(error_reply));
  229. goto out;
  230. }
  231. t84h = reply->message;
  232. if (t84h->code == TYPE84_RSP_CODE) {
  233. length = min(PCICA_MAX_RESPONSE_SIZE, (int) t84h->len);
  234. memcpy(msg->message, reply->message, length);
  235. } else
  236. memcpy(msg->message, reply->message, sizeof error_reply);
  237. out:
  238. complete((struct completion *) msg->private);
  239. }
  240. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  241. /**
  242. * The request distributor calls this function if it picked the PCICA
  243. * device to handle a modexpo request.
  244. * @zdev: pointer to zcrypt_device structure that identifies the
  245. * PCICA device to the request distributor
  246. * @mex: pointer to the modexpo request buffer
  247. */
  248. static long zcrypt_pcica_modexpo(struct zcrypt_device *zdev,
  249. struct ica_rsa_modexpo *mex)
  250. {
  251. struct ap_message ap_msg;
  252. struct completion work;
  253. int rc;
  254. ap_init_message(&ap_msg);
  255. ap_msg.message = kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL);
  256. if (!ap_msg.message)
  257. return -ENOMEM;
  258. ap_msg.receive = zcrypt_pcica_receive;
  259. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  260. atomic_inc_return(&zcrypt_step);
  261. ap_msg.private = &work;
  262. rc = ICAMEX_msg_to_type4MEX_msg(zdev, &ap_msg, mex);
  263. if (rc)
  264. goto out_free;
  265. init_completion(&work);
  266. ap_queue_message(zdev->ap_dev, &ap_msg);
  267. rc = wait_for_completion_interruptible(&work);
  268. if (rc == 0)
  269. rc = convert_response(zdev, &ap_msg, mex->outputdata,
  270. mex->outputdatalength);
  271. else
  272. /* Signal pending. */
  273. ap_cancel_message(zdev->ap_dev, &ap_msg);
  274. out_free:
  275. kfree(ap_msg.message);
  276. return rc;
  277. }
  278. /**
  279. * The request distributor calls this function if it picked the PCICA
  280. * device to handle a modexpo_crt request.
  281. * @zdev: pointer to zcrypt_device structure that identifies the
  282. * PCICA device to the request distributor
  283. * @crt: pointer to the modexpoc_crt request buffer
  284. */
  285. static long zcrypt_pcica_modexpo_crt(struct zcrypt_device *zdev,
  286. struct ica_rsa_modexpo_crt *crt)
  287. {
  288. struct ap_message ap_msg;
  289. struct completion work;
  290. int rc;
  291. ap_init_message(&ap_msg);
  292. ap_msg.message = kmalloc(PCICA_MAX_MESSAGE_SIZE, GFP_KERNEL);
  293. if (!ap_msg.message)
  294. return -ENOMEM;
  295. ap_msg.receive = zcrypt_pcica_receive;
  296. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  297. atomic_inc_return(&zcrypt_step);
  298. ap_msg.private = &work;
  299. rc = ICACRT_msg_to_type4CRT_msg(zdev, &ap_msg, crt);
  300. if (rc)
  301. goto out_free;
  302. init_completion(&work);
  303. ap_queue_message(zdev->ap_dev, &ap_msg);
  304. rc = wait_for_completion_interruptible(&work);
  305. if (rc == 0)
  306. rc = convert_response(zdev, &ap_msg, crt->outputdata,
  307. crt->outputdatalength);
  308. else
  309. /* Signal pending. */
  310. ap_cancel_message(zdev->ap_dev, &ap_msg);
  311. out_free:
  312. kfree(ap_msg.message);
  313. return rc;
  314. }
  315. /**
  316. * The crypto operations for a PCICA card.
  317. */
  318. static struct zcrypt_ops zcrypt_pcica_ops = {
  319. .rsa_modexpo = zcrypt_pcica_modexpo,
  320. .rsa_modexpo_crt = zcrypt_pcica_modexpo_crt,
  321. };
  322. /**
  323. * Probe function for PCICA cards. It always accepts the AP device
  324. * since the bus_match already checked the hardware type.
  325. * @ap_dev: pointer to the AP device.
  326. */
  327. static int zcrypt_pcica_probe(struct ap_device *ap_dev)
  328. {
  329. struct zcrypt_device *zdev;
  330. int rc;
  331. zdev = zcrypt_device_alloc(PCICA_MAX_RESPONSE_SIZE);
  332. if (!zdev)
  333. return -ENOMEM;
  334. zdev->ap_dev = ap_dev;
  335. zdev->ops = &zcrypt_pcica_ops;
  336. zdev->online = 1;
  337. zdev->user_space_type = ZCRYPT_PCICA;
  338. zdev->type_string = "PCICA";
  339. zdev->min_mod_size = PCICA_MIN_MOD_SIZE;
  340. zdev->max_mod_size = PCICA_MAX_MOD_SIZE;
  341. zdev->speed_rating = PCICA_SPEED_RATING;
  342. zdev->max_exp_bit_length = PCICA_MAX_MOD_SIZE;
  343. ap_dev->reply = &zdev->reply;
  344. ap_dev->private = zdev;
  345. rc = zcrypt_device_register(zdev);
  346. if (rc)
  347. goto out_free;
  348. return 0;
  349. out_free:
  350. ap_dev->private = NULL;
  351. zcrypt_device_free(zdev);
  352. return rc;
  353. }
  354. /**
  355. * This is called to remove the extended PCICA driver information
  356. * if an AP device is removed.
  357. */
  358. static void zcrypt_pcica_remove(struct ap_device *ap_dev)
  359. {
  360. struct zcrypt_device *zdev = ap_dev->private;
  361. zcrypt_device_unregister(zdev);
  362. }
  363. int __init zcrypt_pcica_init(void)
  364. {
  365. return ap_driver_register(&zcrypt_pcica_driver, THIS_MODULE, "pcica");
  366. }
  367. void zcrypt_pcica_exit(void)
  368. {
  369. ap_driver_unregister(&zcrypt_pcica_driver);
  370. }
  371. module_init(zcrypt_pcica_init);
  372. module_exit(zcrypt_pcica_exit);