zcrypt_cex2a.c 15 KB

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