zcrypt_pcica.c 12 KB

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