zcrypt_cex2a.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. { AP_DEVICE(AP_DEVICE_TYPE_CEX2A2) },
  46. { /* end of list */ },
  47. };
  48. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  49. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
  50. MODULE_AUTHOR("IBM Corporation");
  51. MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, "
  52. "Copyright 2001, 2006 IBM Corporation");
  53. MODULE_LICENSE("GPL");
  54. #endif
  55. static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
  56. static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
  57. static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
  58. struct ap_message *);
  59. static struct ap_driver zcrypt_cex2a_driver = {
  60. .probe = zcrypt_cex2a_probe,
  61. .remove = zcrypt_cex2a_remove,
  62. .receive = zcrypt_cex2a_receive,
  63. .ids = zcrypt_cex2a_ids,
  64. .request_timeout = CEX2A_CLEANUP_TIME,
  65. };
  66. /**
  67. * Convert a ICAMEX message to a type50 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_type50MEX_msg(struct zcrypt_device *zdev,
  76. struct ap_message *ap_msg,
  77. struct ica_rsa_modexpo *mex)
  78. {
  79. unsigned char *mod, *exp, *inp;
  80. int mod_len;
  81. mod_len = mex->inputdatalength;
  82. if (mod_len <= 128) {
  83. struct type50_meb1_msg *meb1 = ap_msg->message;
  84. memset(meb1, 0, sizeof(*meb1));
  85. ap_msg->length = sizeof(*meb1);
  86. meb1->header.msg_type_code = TYPE50_TYPE_CODE;
  87. meb1->header.msg_len = sizeof(*meb1);
  88. meb1->keyblock_type = TYPE50_MEB1_FMT;
  89. mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
  90. exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
  91. inp = meb1->message + sizeof(meb1->message) - mod_len;
  92. } else {
  93. struct type50_meb2_msg *meb2 = ap_msg->message;
  94. memset(meb2, 0, sizeof(*meb2));
  95. ap_msg->length = sizeof(*meb2);
  96. meb2->header.msg_type_code = TYPE50_TYPE_CODE;
  97. meb2->header.msg_len = sizeof(*meb2);
  98. meb2->keyblock_type = TYPE50_MEB2_FMT;
  99. mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
  100. exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
  101. inp = meb2->message + sizeof(meb2->message) - mod_len;
  102. }
  103. if (copy_from_user(mod, mex->n_modulus, mod_len) ||
  104. copy_from_user(exp, mex->b_key, mod_len) ||
  105. copy_from_user(inp, mex->inputdata, mod_len))
  106. return -EFAULT;
  107. return 0;
  108. }
  109. /**
  110. * Convert a ICACRT message to a type50 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_type50CRT_msg(struct zcrypt_device *zdev,
  119. struct ap_message *ap_msg,
  120. struct ica_rsa_modexpo_crt *crt)
  121. {
  122. int mod_len, short_len, long_len, long_offset;
  123. unsigned char *p, *q, *dp, *dq, *u, *inp;
  124. mod_len = crt->inputdatalength;
  125. short_len = mod_len / 2;
  126. long_len = mod_len / 2 + 8;
  127. /*
  128. * CEX2A cannot handle p, dp, or U > 128 bytes.
  129. * If we have one of these, we need to do extra checking.
  130. */
  131. if (long_len > 128) {
  132. /*
  133. * zcrypt_rsa_crt already checked for the leading
  134. * zeroes of np_prime, bp_key and u_mult_inc.
  135. */
  136. long_offset = long_len - 128;
  137. long_len = 128;
  138. } else
  139. long_offset = 0;
  140. /*
  141. * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
  142. * the larger message structure.
  143. */
  144. if (long_len <= 64) {
  145. struct type50_crb1_msg *crb1 = ap_msg->message;
  146. memset(crb1, 0, sizeof(*crb1));
  147. ap_msg->length = sizeof(*crb1);
  148. crb1->header.msg_type_code = TYPE50_TYPE_CODE;
  149. crb1->header.msg_len = sizeof(*crb1);
  150. crb1->keyblock_type = TYPE50_CRB1_FMT;
  151. p = crb1->p + sizeof(crb1->p) - long_len;
  152. q = crb1->q + sizeof(crb1->q) - short_len;
  153. dp = crb1->dp + sizeof(crb1->dp) - long_len;
  154. dq = crb1->dq + sizeof(crb1->dq) - short_len;
  155. u = crb1->u + sizeof(crb1->u) - long_len;
  156. inp = crb1->message + sizeof(crb1->message) - mod_len;
  157. } else {
  158. struct type50_crb2_msg *crb2 = ap_msg->message;
  159. memset(crb2, 0, sizeof(*crb2));
  160. ap_msg->length = sizeof(*crb2);
  161. crb2->header.msg_type_code = TYPE50_TYPE_CODE;
  162. crb2->header.msg_len = sizeof(*crb2);
  163. crb2->keyblock_type = TYPE50_CRB2_FMT;
  164. p = crb2->p + sizeof(crb2->p) - long_len;
  165. q = crb2->q + sizeof(crb2->q) - short_len;
  166. dp = crb2->dp + sizeof(crb2->dp) - long_len;
  167. dq = crb2->dq + sizeof(crb2->dq) - short_len;
  168. u = crb2->u + sizeof(crb2->u) - long_len;
  169. inp = crb2->message + sizeof(crb2->message) - mod_len;
  170. }
  171. if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
  172. copy_from_user(q, crt->nq_prime, short_len) ||
  173. copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
  174. copy_from_user(dq, crt->bq_key, short_len) ||
  175. copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
  176. copy_from_user(inp, crt->inputdata, mod_len))
  177. return -EFAULT;
  178. return 0;
  179. }
  180. /**
  181. * Copy results from a type 80 reply message back to user space.
  182. *
  183. * @zdev: crypto device pointer
  184. * @reply: reply AP message.
  185. * @data: pointer to user output data
  186. * @length: size of user output data
  187. *
  188. * Returns 0 on success or -EFAULT.
  189. */
  190. static int convert_type80(struct zcrypt_device *zdev,
  191. struct ap_message *reply,
  192. char __user *outputdata,
  193. unsigned int outputdatalength)
  194. {
  195. struct type80_hdr *t80h = reply->message;
  196. unsigned char *data;
  197. if (t80h->len < sizeof(*t80h) + outputdatalength) {
  198. /* The result is too short, the CEX2A card may not do that.. */
  199. zdev->online = 0;
  200. return -EAGAIN; /* repeat the request on a different device. */
  201. }
  202. BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
  203. data = reply->message + t80h->len - outputdatalength;
  204. if (copy_to_user(outputdata, data, outputdatalength))
  205. return -EFAULT;
  206. return 0;
  207. }
  208. static int convert_response(struct zcrypt_device *zdev,
  209. struct ap_message *reply,
  210. char __user *outputdata,
  211. unsigned int outputdatalength)
  212. {
  213. /* Response type byte is the second byte in the response. */
  214. switch (((unsigned char *) reply->message)[1]) {
  215. case TYPE82_RSP_CODE:
  216. case TYPE88_RSP_CODE:
  217. return convert_error(zdev, reply);
  218. case TYPE80_RSP_CODE:
  219. return convert_type80(zdev, reply,
  220. outputdata, outputdatalength);
  221. default: /* Unknown response type, this should NEVER EVER happen */
  222. zdev->online = 0;
  223. return -EAGAIN; /* repeat the request on a different device. */
  224. }
  225. }
  226. /**
  227. * This function is called from the AP bus code after a crypto request
  228. * "msg" has finished with the reply message "reply".
  229. * It is called from tasklet context.
  230. * @ap_dev: pointer to the AP device
  231. * @msg: pointer to the AP message
  232. * @reply: pointer to the AP reply message
  233. */
  234. static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
  235. struct ap_message *msg,
  236. struct ap_message *reply)
  237. {
  238. static struct error_hdr error_reply = {
  239. .type = TYPE82_RSP_CODE,
  240. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  241. };
  242. struct type80_hdr *t80h = reply->message;
  243. int length;
  244. /* Copy the reply message to the request message buffer. */
  245. if (IS_ERR(reply))
  246. memcpy(msg->message, &error_reply, sizeof(error_reply));
  247. else if (t80h->type == TYPE80_RSP_CODE) {
  248. length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len);
  249. memcpy(msg->message, reply->message, length);
  250. } else
  251. memcpy(msg->message, reply->message, sizeof error_reply);
  252. complete((struct completion *) msg->private);
  253. }
  254. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  255. /**
  256. * The request distributor calls this function if it picked the CEX2A
  257. * device to handle a modexpo request.
  258. * @zdev: pointer to zcrypt_device structure that identifies the
  259. * CEX2A device to the request distributor
  260. * @mex: pointer to the modexpo request buffer
  261. */
  262. static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
  263. struct ica_rsa_modexpo *mex)
  264. {
  265. struct ap_message ap_msg;
  266. struct completion work;
  267. int rc;
  268. ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  269. if (!ap_msg.message)
  270. return -ENOMEM;
  271. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  272. atomic_inc_return(&zcrypt_step);
  273. ap_msg.private = &work;
  274. rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
  275. if (rc)
  276. goto out_free;
  277. init_completion(&work);
  278. ap_queue_message(zdev->ap_dev, &ap_msg);
  279. rc = wait_for_completion_interruptible(&work);
  280. if (rc == 0)
  281. rc = convert_response(zdev, &ap_msg, mex->outputdata,
  282. mex->outputdatalength);
  283. else
  284. /* Signal pending. */
  285. ap_cancel_message(zdev->ap_dev, &ap_msg);
  286. out_free:
  287. kfree(ap_msg.message);
  288. return rc;
  289. }
  290. /**
  291. * The request distributor calls this function if it picked the CEX2A
  292. * device to handle a modexpo_crt request.
  293. * @zdev: pointer to zcrypt_device structure that identifies the
  294. * CEX2A device to the request distributor
  295. * @crt: pointer to the modexpoc_crt request buffer
  296. */
  297. static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
  298. struct ica_rsa_modexpo_crt *crt)
  299. {
  300. struct ap_message ap_msg;
  301. struct completion work;
  302. int rc;
  303. ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  304. if (!ap_msg.message)
  305. return -ENOMEM;
  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_type50CRT_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 CEX2A card.
  327. */
  328. static struct zcrypt_ops zcrypt_cex2a_ops = {
  329. .rsa_modexpo = zcrypt_cex2a_modexpo,
  330. .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
  331. };
  332. /**
  333. * Probe function for CEX2A 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_cex2a_probe(struct ap_device *ap_dev)
  338. {
  339. struct zcrypt_device *zdev;
  340. int rc;
  341. zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
  342. if (!zdev)
  343. return -ENOMEM;
  344. zdev->ap_dev = ap_dev;
  345. zdev->ops = &zcrypt_cex2a_ops;
  346. zdev->online = 1;
  347. zdev->user_space_type = ZCRYPT_CEX2A;
  348. zdev->type_string = "CEX2A";
  349. zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
  350. zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
  351. zdev->short_crt = 1;
  352. zdev->speed_rating = CEX2A_SPEED_RATING;
  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 CEX2A driver information
  366. * if an AP device is removed.
  367. */
  368. static void zcrypt_cex2a_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_cex2a_init(void)
  374. {
  375. return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
  376. }
  377. void __exit zcrypt_cex2a_exit(void)
  378. {
  379. ap_driver_unregister(&zcrypt_cex2a_driver);
  380. }
  381. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  382. module_init(zcrypt_cex2a_init);
  383. module_exit(zcrypt_cex2a_exit);
  384. #endif