zcrypt_pcica.c 12 KB

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