zcrypt_pcica.c 12 KB

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