zcrypt_cex2a.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_cex2a.h"
  36. #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
  37. #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
  38. #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
  39. #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
  40. #define CEX2A_SPEED_RATING 970
  41. #define CEX3A_SPEED_RATING 900 /* Fixme: Needs finetuning */
  42. #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
  43. #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
  44. #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
  45. * (max outputdatalength) +
  46. * type80_hdr*/
  47. #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
  48. #define CEX2A_CLEANUP_TIME (15*HZ)
  49. #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
  50. static struct ap_device_id zcrypt_cex2a_ids[] = {
  51. { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
  52. { AP_DEVICE(AP_DEVICE_TYPE_CEX3A) },
  53. { /* end of list */ },
  54. };
  55. MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
  56. MODULE_AUTHOR("IBM Corporation");
  57. MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, "
  58. "Copyright IBM Corp. 2001, 2006");
  59. MODULE_LICENSE("GPL");
  60. static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
  61. static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
  62. static void zcrypt_cex2a_receive(struct ap_device *, struct ap_message *,
  63. struct ap_message *);
  64. static struct ap_driver zcrypt_cex2a_driver = {
  65. .probe = zcrypt_cex2a_probe,
  66. .remove = zcrypt_cex2a_remove,
  67. .ids = zcrypt_cex2a_ids,
  68. .request_timeout = CEX2A_CLEANUP_TIME,
  69. };
  70. /**
  71. * Convert a ICAMEX message to a type50 MEX message.
  72. *
  73. * @zdev: crypto device pointer
  74. * @zreq: crypto request pointer
  75. * @mex: pointer to user input data
  76. *
  77. * Returns 0 on success or -EFAULT.
  78. */
  79. static int ICAMEX_msg_to_type50MEX_msg(struct zcrypt_device *zdev,
  80. struct ap_message *ap_msg,
  81. struct ica_rsa_modexpo *mex)
  82. {
  83. unsigned char *mod, *exp, *inp;
  84. int mod_len;
  85. mod_len = mex->inputdatalength;
  86. if (mod_len <= 128) {
  87. struct type50_meb1_msg *meb1 = ap_msg->message;
  88. memset(meb1, 0, sizeof(*meb1));
  89. ap_msg->length = sizeof(*meb1);
  90. meb1->header.msg_type_code = TYPE50_TYPE_CODE;
  91. meb1->header.msg_len = sizeof(*meb1);
  92. meb1->keyblock_type = TYPE50_MEB1_FMT;
  93. mod = meb1->modulus + sizeof(meb1->modulus) - mod_len;
  94. exp = meb1->exponent + sizeof(meb1->exponent) - mod_len;
  95. inp = meb1->message + sizeof(meb1->message) - mod_len;
  96. } else if (mod_len <= 256) {
  97. struct type50_meb2_msg *meb2 = ap_msg->message;
  98. memset(meb2, 0, sizeof(*meb2));
  99. ap_msg->length = sizeof(*meb2);
  100. meb2->header.msg_type_code = TYPE50_TYPE_CODE;
  101. meb2->header.msg_len = sizeof(*meb2);
  102. meb2->keyblock_type = TYPE50_MEB2_FMT;
  103. mod = meb2->modulus + sizeof(meb2->modulus) - mod_len;
  104. exp = meb2->exponent + sizeof(meb2->exponent) - mod_len;
  105. inp = meb2->message + sizeof(meb2->message) - mod_len;
  106. } else {
  107. /* mod_len > 256 = 4096 bit RSA Key */
  108. struct type50_meb3_msg *meb3 = ap_msg->message;
  109. memset(meb3, 0, sizeof(*meb3));
  110. ap_msg->length = sizeof(*meb3);
  111. meb3->header.msg_type_code = TYPE50_TYPE_CODE;
  112. meb3->header.msg_len = sizeof(*meb3);
  113. meb3->keyblock_type = TYPE50_MEB3_FMT;
  114. mod = meb3->modulus + sizeof(meb3->modulus) - mod_len;
  115. exp = meb3->exponent + sizeof(meb3->exponent) - mod_len;
  116. inp = meb3->message + sizeof(meb3->message) - mod_len;
  117. }
  118. if (copy_from_user(mod, mex->n_modulus, mod_len) ||
  119. copy_from_user(exp, mex->b_key, mod_len) ||
  120. copy_from_user(inp, mex->inputdata, mod_len))
  121. return -EFAULT;
  122. return 0;
  123. }
  124. /**
  125. * Convert a ICACRT message to a type50 CRT message.
  126. *
  127. * @zdev: crypto device pointer
  128. * @zreq: crypto request pointer
  129. * @crt: pointer to user input data
  130. *
  131. * Returns 0 on success or -EFAULT.
  132. */
  133. static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_device *zdev,
  134. struct ap_message *ap_msg,
  135. struct ica_rsa_modexpo_crt *crt)
  136. {
  137. int mod_len, short_len, long_len, long_offset, limit;
  138. unsigned char *p, *q, *dp, *dq, *u, *inp;
  139. mod_len = crt->inputdatalength;
  140. short_len = mod_len / 2;
  141. long_len = mod_len / 2 + 8;
  142. /*
  143. * CEX2A cannot handle p, dp, or U > 128 bytes.
  144. * If we have one of these, we need to do extra checking.
  145. * For CEX3A the limit is 256 bytes.
  146. */
  147. if (zdev->max_mod_size == CEX3A_MAX_MOD_SIZE)
  148. limit = 256;
  149. else
  150. limit = 128;
  151. if (long_len > limit) {
  152. /*
  153. * zcrypt_rsa_crt already checked for the leading
  154. * zeroes of np_prime, bp_key and u_mult_inc.
  155. */
  156. long_offset = long_len - limit;
  157. long_len = limit;
  158. } else
  159. long_offset = 0;
  160. /*
  161. * Instead of doing extra work for p, dp, U > 64 bytes, we'll just use
  162. * the larger message structure.
  163. */
  164. if (long_len <= 64) {
  165. struct type50_crb1_msg *crb1 = ap_msg->message;
  166. memset(crb1, 0, sizeof(*crb1));
  167. ap_msg->length = sizeof(*crb1);
  168. crb1->header.msg_type_code = TYPE50_TYPE_CODE;
  169. crb1->header.msg_len = sizeof(*crb1);
  170. crb1->keyblock_type = TYPE50_CRB1_FMT;
  171. p = crb1->p + sizeof(crb1->p) - long_len;
  172. q = crb1->q + sizeof(crb1->q) - short_len;
  173. dp = crb1->dp + sizeof(crb1->dp) - long_len;
  174. dq = crb1->dq + sizeof(crb1->dq) - short_len;
  175. u = crb1->u + sizeof(crb1->u) - long_len;
  176. inp = crb1->message + sizeof(crb1->message) - mod_len;
  177. } else if (long_len <= 128) {
  178. struct type50_crb2_msg *crb2 = ap_msg->message;
  179. memset(crb2, 0, sizeof(*crb2));
  180. ap_msg->length = sizeof(*crb2);
  181. crb2->header.msg_type_code = TYPE50_TYPE_CODE;
  182. crb2->header.msg_len = sizeof(*crb2);
  183. crb2->keyblock_type = TYPE50_CRB2_FMT;
  184. p = crb2->p + sizeof(crb2->p) - long_len;
  185. q = crb2->q + sizeof(crb2->q) - short_len;
  186. dp = crb2->dp + sizeof(crb2->dp) - long_len;
  187. dq = crb2->dq + sizeof(crb2->dq) - short_len;
  188. u = crb2->u + sizeof(crb2->u) - long_len;
  189. inp = crb2->message + sizeof(crb2->message) - mod_len;
  190. } else {
  191. /* long_len >= 256 */
  192. struct type50_crb3_msg *crb3 = ap_msg->message;
  193. memset(crb3, 0, sizeof(*crb3));
  194. ap_msg->length = sizeof(*crb3);
  195. crb3->header.msg_type_code = TYPE50_TYPE_CODE;
  196. crb3->header.msg_len = sizeof(*crb3);
  197. crb3->keyblock_type = TYPE50_CRB3_FMT;
  198. p = crb3->p + sizeof(crb3->p) - long_len;
  199. q = crb3->q + sizeof(crb3->q) - short_len;
  200. dp = crb3->dp + sizeof(crb3->dp) - long_len;
  201. dq = crb3->dq + sizeof(crb3->dq) - short_len;
  202. u = crb3->u + sizeof(crb3->u) - long_len;
  203. inp = crb3->message + sizeof(crb3->message) - mod_len;
  204. }
  205. if (copy_from_user(p, crt->np_prime + long_offset, long_len) ||
  206. copy_from_user(q, crt->nq_prime, short_len) ||
  207. copy_from_user(dp, crt->bp_key + long_offset, long_len) ||
  208. copy_from_user(dq, crt->bq_key, short_len) ||
  209. copy_from_user(u, crt->u_mult_inv + long_offset, long_len) ||
  210. copy_from_user(inp, crt->inputdata, mod_len))
  211. return -EFAULT;
  212. return 0;
  213. }
  214. /**
  215. * Copy results from a type 80 reply message back to user space.
  216. *
  217. * @zdev: crypto device pointer
  218. * @reply: reply AP message.
  219. * @data: pointer to user output data
  220. * @length: size of user output data
  221. *
  222. * Returns 0 on success or -EFAULT.
  223. */
  224. static int convert_type80(struct zcrypt_device *zdev,
  225. struct ap_message *reply,
  226. char __user *outputdata,
  227. unsigned int outputdatalength)
  228. {
  229. struct type80_hdr *t80h = reply->message;
  230. unsigned char *data;
  231. if (t80h->len < sizeof(*t80h) + outputdatalength) {
  232. /* The result is too short, the CEX2A card may not do that.. */
  233. zdev->online = 0;
  234. return -EAGAIN; /* repeat the request on a different device. */
  235. }
  236. if (zdev->user_space_type == ZCRYPT_CEX2A)
  237. BUG_ON(t80h->len > CEX2A_MAX_RESPONSE_SIZE);
  238. else
  239. BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
  240. data = reply->message + t80h->len - outputdatalength;
  241. if (copy_to_user(outputdata, data, outputdatalength))
  242. return -EFAULT;
  243. return 0;
  244. }
  245. static int convert_response(struct zcrypt_device *zdev,
  246. struct ap_message *reply,
  247. char __user *outputdata,
  248. unsigned int outputdatalength)
  249. {
  250. /* Response type byte is the second byte in the response. */
  251. switch (((unsigned char *) reply->message)[1]) {
  252. case TYPE82_RSP_CODE:
  253. case TYPE88_RSP_CODE:
  254. return convert_error(zdev, reply);
  255. case TYPE80_RSP_CODE:
  256. return convert_type80(zdev, reply,
  257. outputdata, outputdatalength);
  258. default: /* Unknown response type, this should NEVER EVER happen */
  259. zdev->online = 0;
  260. return -EAGAIN; /* repeat the request on a different device. */
  261. }
  262. }
  263. /**
  264. * This function is called from the AP bus code after a crypto request
  265. * "msg" has finished with the reply message "reply".
  266. * It is called from tasklet context.
  267. * @ap_dev: pointer to the AP device
  268. * @msg: pointer to the AP message
  269. * @reply: pointer to the AP reply message
  270. */
  271. static void zcrypt_cex2a_receive(struct ap_device *ap_dev,
  272. struct ap_message *msg,
  273. struct ap_message *reply)
  274. {
  275. static struct error_hdr error_reply = {
  276. .type = TYPE82_RSP_CODE,
  277. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  278. };
  279. struct type80_hdr *t80h;
  280. int length;
  281. /* Copy the reply message to the request message buffer. */
  282. if (IS_ERR(reply)) {
  283. memcpy(msg->message, &error_reply, sizeof(error_reply));
  284. goto out;
  285. }
  286. t80h = reply->message;
  287. if (t80h->type == TYPE80_RSP_CODE) {
  288. if (ap_dev->device_type == AP_DEVICE_TYPE_CEX2A)
  289. length = min(CEX2A_MAX_RESPONSE_SIZE, (int) t80h->len);
  290. else
  291. length = min(CEX3A_MAX_RESPONSE_SIZE, (int) t80h->len);
  292. memcpy(msg->message, reply->message, length);
  293. } else
  294. memcpy(msg->message, reply->message, sizeof error_reply);
  295. out:
  296. complete((struct completion *) msg->private);
  297. }
  298. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  299. /**
  300. * The request distributor calls this function if it picked the CEX2A
  301. * device to handle a modexpo request.
  302. * @zdev: pointer to zcrypt_device structure that identifies the
  303. * CEX2A device to the request distributor
  304. * @mex: pointer to the modexpo request buffer
  305. */
  306. static long zcrypt_cex2a_modexpo(struct zcrypt_device *zdev,
  307. struct ica_rsa_modexpo *mex)
  308. {
  309. struct ap_message ap_msg;
  310. struct completion work;
  311. int rc;
  312. ap_init_message(&ap_msg);
  313. if (zdev->user_space_type == ZCRYPT_CEX2A)
  314. ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  315. else
  316. ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  317. if (!ap_msg.message)
  318. return -ENOMEM;
  319. ap_msg.receive = zcrypt_cex2a_receive;
  320. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  321. atomic_inc_return(&zcrypt_step);
  322. ap_msg.private = &work;
  323. rc = ICAMEX_msg_to_type50MEX_msg(zdev, &ap_msg, mex);
  324. if (rc)
  325. goto out_free;
  326. init_completion(&work);
  327. ap_queue_message(zdev->ap_dev, &ap_msg);
  328. rc = wait_for_completion_interruptible(&work);
  329. if (rc == 0)
  330. rc = convert_response(zdev, &ap_msg, mex->outputdata,
  331. mex->outputdatalength);
  332. else
  333. /* Signal pending. */
  334. ap_cancel_message(zdev->ap_dev, &ap_msg);
  335. out_free:
  336. kfree(ap_msg.message);
  337. return rc;
  338. }
  339. /**
  340. * The request distributor calls this function if it picked the CEX2A
  341. * device to handle a modexpo_crt request.
  342. * @zdev: pointer to zcrypt_device structure that identifies the
  343. * CEX2A device to the request distributor
  344. * @crt: pointer to the modexpoc_crt request buffer
  345. */
  346. static long zcrypt_cex2a_modexpo_crt(struct zcrypt_device *zdev,
  347. struct ica_rsa_modexpo_crt *crt)
  348. {
  349. struct ap_message ap_msg;
  350. struct completion work;
  351. int rc;
  352. ap_init_message(&ap_msg);
  353. if (zdev->user_space_type == ZCRYPT_CEX2A)
  354. ap_msg.message = kmalloc(CEX2A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  355. else
  356. ap_msg.message = kmalloc(CEX3A_MAX_MESSAGE_SIZE, GFP_KERNEL);
  357. if (!ap_msg.message)
  358. return -ENOMEM;
  359. ap_msg.receive = zcrypt_cex2a_receive;
  360. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  361. atomic_inc_return(&zcrypt_step);
  362. ap_msg.private = &work;
  363. rc = ICACRT_msg_to_type50CRT_msg(zdev, &ap_msg, crt);
  364. if (rc)
  365. goto out_free;
  366. init_completion(&work);
  367. ap_queue_message(zdev->ap_dev, &ap_msg);
  368. rc = wait_for_completion_interruptible(&work);
  369. if (rc == 0)
  370. rc = convert_response(zdev, &ap_msg, crt->outputdata,
  371. crt->outputdatalength);
  372. else
  373. /* Signal pending. */
  374. ap_cancel_message(zdev->ap_dev, &ap_msg);
  375. out_free:
  376. kfree(ap_msg.message);
  377. return rc;
  378. }
  379. /**
  380. * The crypto operations for a CEX2A card.
  381. */
  382. static struct zcrypt_ops zcrypt_cex2a_ops = {
  383. .rsa_modexpo = zcrypt_cex2a_modexpo,
  384. .rsa_modexpo_crt = zcrypt_cex2a_modexpo_crt,
  385. };
  386. /**
  387. * Probe function for CEX2A cards. It always accepts the AP device
  388. * since the bus_match already checked the hardware type.
  389. * @ap_dev: pointer to the AP device.
  390. */
  391. static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
  392. {
  393. struct zcrypt_device *zdev = NULL;
  394. int rc = 0;
  395. switch (ap_dev->device_type) {
  396. case AP_DEVICE_TYPE_CEX2A:
  397. zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
  398. if (!zdev)
  399. return -ENOMEM;
  400. zdev->user_space_type = ZCRYPT_CEX2A;
  401. zdev->type_string = "CEX2A";
  402. zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
  403. zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
  404. zdev->short_crt = 1;
  405. zdev->speed_rating = CEX2A_SPEED_RATING;
  406. zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  407. break;
  408. case AP_DEVICE_TYPE_CEX3A:
  409. zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE);
  410. if (!zdev)
  411. return -ENOMEM;
  412. zdev->user_space_type = ZCRYPT_CEX3A;
  413. zdev->type_string = "CEX3A";
  414. zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
  415. zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
  416. zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
  417. if (ap_4096_commands_available(ap_dev->qid)) {
  418. zdev->max_mod_size = CEX3A_MAX_MOD_SIZE;
  419. zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
  420. }
  421. zdev->short_crt = 1;
  422. zdev->speed_rating = CEX3A_SPEED_RATING;
  423. break;
  424. }
  425. if (zdev != NULL) {
  426. zdev->ap_dev = ap_dev;
  427. zdev->ops = &zcrypt_cex2a_ops;
  428. zdev->online = 1;
  429. ap_dev->reply = &zdev->reply;
  430. ap_dev->private = zdev;
  431. rc = zcrypt_device_register(zdev);
  432. }
  433. if (rc) {
  434. ap_dev->private = NULL;
  435. zcrypt_device_free(zdev);
  436. }
  437. return rc;
  438. }
  439. /**
  440. * This is called to remove the extended CEX2A driver information
  441. * if an AP device is removed.
  442. */
  443. static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
  444. {
  445. struct zcrypt_device *zdev = ap_dev->private;
  446. zcrypt_device_unregister(zdev);
  447. }
  448. int __init zcrypt_cex2a_init(void)
  449. {
  450. return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
  451. }
  452. void __exit zcrypt_cex2a_exit(void)
  453. {
  454. ap_driver_unregister(&zcrypt_cex2a_driver);
  455. }
  456. module_init(zcrypt_cex2a_init);
  457. module_exit(zcrypt_cex2a_exit);