zcrypt_cex2a.c 15 KB

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