zcrypt_pcicc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/init.h>
  28. #include <linux/gfp.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_pcicc.h"
  36. #include "zcrypt_cca_key.h"
  37. #define PCICC_MIN_MOD_SIZE 64 /* 512 bits */
  38. #define PCICC_MAX_MOD_SIZE_OLD 128 /* 1024 bits */
  39. #define PCICC_MAX_MOD_SIZE 256 /* 2048 bits */
  40. /*
  41. * PCICC cards need a speed rating of 0. This keeps them at the end of
  42. * the zcrypt device list (see zcrypt_api.c). PCICC cards are only
  43. * used if no other cards are present because they are slow and can only
  44. * cope with PKCS12 padded requests. The logic is queer. PKCS11 padded
  45. * requests are rejected. The modexpo function encrypts PKCS12 padded data
  46. * and decrypts any non-PKCS12 padded data (except PKCS11) in the assumption
  47. * that it's encrypted PKCS12 data. The modexpo_crt function always decrypts
  48. * the data in the assumption that its PKCS12 encrypted data.
  49. */
  50. #define PCICC_SPEED_RATING 0
  51. #define PCICC_MAX_MESSAGE_SIZE 0x710 /* max size type6 v1 crt message */
  52. #define PCICC_MAX_RESPONSE_SIZE 0x710 /* max size type86 v1 reply */
  53. #define PCICC_CLEANUP_TIME (15*HZ)
  54. static struct ap_device_id zcrypt_pcicc_ids[] = {
  55. { AP_DEVICE(AP_DEVICE_TYPE_PCICC) },
  56. { /* end of list */ },
  57. };
  58. MODULE_DEVICE_TABLE(ap, zcrypt_pcicc_ids);
  59. MODULE_AUTHOR("IBM Corporation");
  60. MODULE_DESCRIPTION("PCICC Cryptographic Coprocessor device driver, "
  61. "Copyright IBM Corp. 2001, 2006");
  62. MODULE_LICENSE("GPL");
  63. static int zcrypt_pcicc_probe(struct ap_device *ap_dev);
  64. static void zcrypt_pcicc_remove(struct ap_device *ap_dev);
  65. static void zcrypt_pcicc_receive(struct ap_device *, struct ap_message *,
  66. struct ap_message *);
  67. static struct ap_driver zcrypt_pcicc_driver = {
  68. .probe = zcrypt_pcicc_probe,
  69. .remove = zcrypt_pcicc_remove,
  70. .ids = zcrypt_pcicc_ids,
  71. .request_timeout = PCICC_CLEANUP_TIME,
  72. };
  73. /**
  74. * The following is used to initialize the CPRB passed to the PCICC card
  75. * in a type6 message. The 3 fields that must be filled in at execution
  76. * time are req_parml, rpl_parml and usage_domain. Note that all three
  77. * fields are *little*-endian. Actually, everything about this interface
  78. * is ascii/little-endian, since the device has 'Intel inside'.
  79. *
  80. * The CPRB is followed immediately by the parm block.
  81. * The parm block contains:
  82. * - function code ('PD' 0x5044 or 'PK' 0x504B)
  83. * - rule block (0x0A00 'PKCS-1.2' or 0x0A00 'ZERO-PAD')
  84. * - VUD block
  85. */
  86. static struct CPRB static_cprb = {
  87. .cprb_len = __constant_cpu_to_le16(0x0070),
  88. .cprb_ver_id = 0x41,
  89. .func_id = {0x54,0x32},
  90. .checkpoint_flag= 0x01,
  91. .svr_namel = __constant_cpu_to_le16(0x0008),
  92. .svr_name = {'I','C','S','F',' ',' ',' ',' '}
  93. };
  94. /**
  95. * Check the message for PKCS11 padding.
  96. */
  97. static inline int is_PKCS11_padded(unsigned char *buffer, int length)
  98. {
  99. int i;
  100. if ((buffer[0] != 0x00) || (buffer[1] != 0x01))
  101. return 0;
  102. for (i = 2; i < length; i++)
  103. if (buffer[i] != 0xFF)
  104. break;
  105. if (i < 10 || i == length)
  106. return 0;
  107. if (buffer[i] != 0x00)
  108. return 0;
  109. return 1;
  110. }
  111. /**
  112. * Check the message for PKCS12 padding.
  113. */
  114. static inline int is_PKCS12_padded(unsigned char *buffer, int length)
  115. {
  116. int i;
  117. if ((buffer[0] != 0x00) || (buffer[1] != 0x02))
  118. return 0;
  119. for (i = 2; i < length; i++)
  120. if (buffer[i] == 0x00)
  121. break;
  122. if ((i < 10) || (i == length))
  123. return 0;
  124. if (buffer[i] != 0x00)
  125. return 0;
  126. return 1;
  127. }
  128. /**
  129. * Convert a ICAMEX message to a type6 MEX message.
  130. *
  131. * @zdev: crypto device pointer
  132. * @zreq: crypto request pointer
  133. * @mex: pointer to user input data
  134. *
  135. * Returns 0 on success or -EFAULT.
  136. */
  137. static int ICAMEX_msg_to_type6MEX_msg(struct zcrypt_device *zdev,
  138. struct ap_message *ap_msg,
  139. struct ica_rsa_modexpo *mex)
  140. {
  141. static struct type6_hdr static_type6_hdr = {
  142. .type = 0x06,
  143. .offset1 = 0x00000058,
  144. .agent_id = {0x01,0x00,0x43,0x43,0x41,0x2D,0x41,0x50,
  145. 0x50,0x4C,0x20,0x20,0x20,0x01,0x01,0x01},
  146. .function_code = {'P','K'},
  147. };
  148. static struct function_and_rules_block static_pke_function_and_rules ={
  149. .function_code = {'P','K'},
  150. .ulen = __constant_cpu_to_le16(10),
  151. .only_rule = {'P','K','C','S','-','1','.','2'}
  152. };
  153. struct {
  154. struct type6_hdr hdr;
  155. struct CPRB cprb;
  156. struct function_and_rules_block fr;
  157. unsigned short length;
  158. char text[0];
  159. } __attribute__((packed)) *msg = ap_msg->message;
  160. int vud_len, pad_len, size;
  161. /* VUD.ciphertext */
  162. if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
  163. return -EFAULT;
  164. if (is_PKCS11_padded(msg->text, mex->inputdatalength))
  165. return -EINVAL;
  166. /* static message header and f&r */
  167. msg->hdr = static_type6_hdr;
  168. msg->fr = static_pke_function_and_rules;
  169. if (is_PKCS12_padded(msg->text, mex->inputdatalength)) {
  170. /* strip the padding and adjust the data length */
  171. pad_len = strnlen(msg->text + 2, mex->inputdatalength - 2) + 3;
  172. if (pad_len <= 9 || pad_len >= mex->inputdatalength)
  173. return -ENODEV;
  174. vud_len = mex->inputdatalength - pad_len;
  175. memmove(msg->text, msg->text + pad_len, vud_len);
  176. msg->length = cpu_to_le16(vud_len + 2);
  177. /* Set up key after the variable length text. */
  178. size = zcrypt_type6_mex_key_en(mex, msg->text + vud_len, 0);
  179. if (size < 0)
  180. return size;
  181. size += sizeof(*msg) + vud_len; /* total size of msg */
  182. } else {
  183. vud_len = mex->inputdatalength;
  184. msg->length = cpu_to_le16(2 + vud_len);
  185. msg->hdr.function_code[1] = 'D';
  186. msg->fr.function_code[1] = 'D';
  187. /* Set up key after the variable length text. */
  188. size = zcrypt_type6_mex_key_de(mex, msg->text + vud_len, 0);
  189. if (size < 0)
  190. return size;
  191. size += sizeof(*msg) + vud_len; /* total size of msg */
  192. }
  193. /* message header, cprb and f&r */
  194. msg->hdr.ToCardLen1 = (size - sizeof(msg->hdr) + 3) & -4;
  195. msg->hdr.FromCardLen1 = PCICC_MAX_RESPONSE_SIZE - sizeof(msg->hdr);
  196. msg->cprb = static_cprb;
  197. msg->cprb.usage_domain[0]= AP_QID_QUEUE(zdev->ap_dev->qid);
  198. msg->cprb.req_parml = cpu_to_le16(size - sizeof(msg->hdr) -
  199. sizeof(msg->cprb));
  200. msg->cprb.rpl_parml = cpu_to_le16(msg->hdr.FromCardLen1);
  201. ap_msg->length = (size + 3) & -4;
  202. return 0;
  203. }
  204. /**
  205. * Convert a ICACRT message to a type6 CRT message.
  206. *
  207. * @zdev: crypto device pointer
  208. * @zreq: crypto request pointer
  209. * @crt: pointer to user input data
  210. *
  211. * Returns 0 on success or -EFAULT.
  212. */
  213. static int ICACRT_msg_to_type6CRT_msg(struct zcrypt_device *zdev,
  214. struct ap_message *ap_msg,
  215. struct ica_rsa_modexpo_crt *crt)
  216. {
  217. static struct type6_hdr static_type6_hdr = {
  218. .type = 0x06,
  219. .offset1 = 0x00000058,
  220. .agent_id = {0x01,0x00,0x43,0x43,0x41,0x2D,0x41,0x50,
  221. 0x50,0x4C,0x20,0x20,0x20,0x01,0x01,0x01},
  222. .function_code = {'P','D'},
  223. };
  224. static struct function_and_rules_block static_pkd_function_and_rules ={
  225. .function_code = {'P','D'},
  226. .ulen = __constant_cpu_to_le16(10),
  227. .only_rule = {'P','K','C','S','-','1','.','2'}
  228. };
  229. struct {
  230. struct type6_hdr hdr;
  231. struct CPRB cprb;
  232. struct function_and_rules_block fr;
  233. unsigned short length;
  234. char text[0];
  235. } __attribute__((packed)) *msg = ap_msg->message;
  236. int size;
  237. /* VUD.ciphertext */
  238. msg->length = cpu_to_le16(2 + crt->inputdatalength);
  239. if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
  240. return -EFAULT;
  241. if (is_PKCS11_padded(msg->text, crt->inputdatalength))
  242. return -EINVAL;
  243. /* Set up key after the variable length text. */
  244. size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength, 0);
  245. if (size < 0)
  246. return size;
  247. size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
  248. /* message header, cprb and f&r */
  249. msg->hdr = static_type6_hdr;
  250. msg->hdr.ToCardLen1 = (size - sizeof(msg->hdr) + 3) & -4;
  251. msg->hdr.FromCardLen1 = PCICC_MAX_RESPONSE_SIZE - sizeof(msg->hdr);
  252. msg->cprb = static_cprb;
  253. msg->cprb.usage_domain[0] = AP_QID_QUEUE(zdev->ap_dev->qid);
  254. msg->cprb.req_parml = msg->cprb.rpl_parml =
  255. cpu_to_le16(size - sizeof(msg->hdr) - sizeof(msg->cprb));
  256. msg->fr = static_pkd_function_and_rules;
  257. ap_msg->length = (size + 3) & -4;
  258. return 0;
  259. }
  260. /**
  261. * Copy results from a type 86 reply message back to user space.
  262. *
  263. * @zdev: crypto device pointer
  264. * @reply: reply AP message.
  265. * @data: pointer to user output data
  266. * @length: size of user output data
  267. *
  268. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  269. */
  270. struct type86_reply {
  271. struct type86_hdr hdr;
  272. struct type86_fmt2_ext fmt2;
  273. struct CPRB cprb;
  274. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  275. unsigned short length;
  276. char text[0];
  277. } __attribute__((packed));
  278. static int convert_type86(struct zcrypt_device *zdev,
  279. struct ap_message *reply,
  280. char __user *outputdata,
  281. unsigned int outputdatalength)
  282. {
  283. static unsigned char static_pad[] = {
  284. 0x00,0x02,
  285. 0x1B,0x7B,0x5D,0xB5,0x75,0x01,0x3D,0xFD,
  286. 0x8D,0xD1,0xC7,0x03,0x2D,0x09,0x23,0x57,
  287. 0x89,0x49,0xB9,0x3F,0xBB,0x99,0x41,0x5B,
  288. 0x75,0x21,0x7B,0x9D,0x3B,0x6B,0x51,0x39,
  289. 0xBB,0x0D,0x35,0xB9,0x89,0x0F,0x93,0xA5,
  290. 0x0B,0x47,0xF1,0xD3,0xBB,0xCB,0xF1,0x9D,
  291. 0x23,0x73,0x71,0xFF,0xF3,0xF5,0x45,0xFB,
  292. 0x61,0x29,0x23,0xFD,0xF1,0x29,0x3F,0x7F,
  293. 0x17,0xB7,0x1B,0xA9,0x19,0xBD,0x57,0xA9,
  294. 0xD7,0x95,0xA3,0xCB,0xED,0x1D,0xDB,0x45,
  295. 0x7D,0x11,0xD1,0x51,0x1B,0xED,0x71,0xE9,
  296. 0xB1,0xD1,0xAB,0xAB,0x21,0x2B,0x1B,0x9F,
  297. 0x3B,0x9F,0xF7,0xF7,0xBD,0x63,0xEB,0xAD,
  298. 0xDF,0xB3,0x6F,0x5B,0xDB,0x8D,0xA9,0x5D,
  299. 0xE3,0x7D,0x77,0x49,0x47,0xF5,0xA7,0xFD,
  300. 0xAB,0x2F,0x27,0x35,0x77,0xD3,0x49,0xC9,
  301. 0x09,0xEB,0xB1,0xF9,0xBF,0x4B,0xCB,0x2B,
  302. 0xEB,0xEB,0x05,0xFF,0x7D,0xC7,0x91,0x8B,
  303. 0x09,0x83,0xB9,0xB9,0x69,0x33,0x39,0x6B,
  304. 0x79,0x75,0x19,0xBF,0xBB,0x07,0x1D,0xBD,
  305. 0x29,0xBF,0x39,0x95,0x93,0x1D,0x35,0xC7,
  306. 0xC9,0x4D,0xE5,0x97,0x0B,0x43,0x9B,0xF1,
  307. 0x16,0x93,0x03,0x1F,0xA5,0xFB,0xDB,0xF3,
  308. 0x27,0x4F,0x27,0x61,0x05,0x1F,0xB9,0x23,
  309. 0x2F,0xC3,0x81,0xA9,0x23,0x71,0x55,0x55,
  310. 0xEB,0xED,0x41,0xE5,0xF3,0x11,0xF1,0x43,
  311. 0x69,0x03,0xBD,0x0B,0x37,0x0F,0x51,0x8F,
  312. 0x0B,0xB5,0x89,0x5B,0x67,0xA9,0xD9,0x4F,
  313. 0x01,0xF9,0x21,0x77,0x37,0x73,0x79,0xC5,
  314. 0x7F,0x51,0xC1,0xCF,0x97,0xA1,0x75,0xAD,
  315. 0x35,0x9D,0xD3,0xD3,0xA7,0x9D,0x5D,0x41,
  316. 0x6F,0x65,0x1B,0xCF,0xA9,0x87,0x91,0x09
  317. };
  318. struct type86_reply *msg = reply->message;
  319. unsigned short service_rc, service_rs;
  320. unsigned int reply_len, pad_len;
  321. char *data;
  322. service_rc = le16_to_cpu(msg->cprb.ccp_rtcode);
  323. if (unlikely(service_rc != 0)) {
  324. service_rs = le16_to_cpu(msg->cprb.ccp_rscode);
  325. if (service_rc == 8 && service_rs == 66)
  326. return -EINVAL;
  327. if (service_rc == 8 && service_rs == 65)
  328. return -EINVAL;
  329. if (service_rc == 8 && service_rs == 770) {
  330. zdev->max_mod_size = PCICC_MAX_MOD_SIZE_OLD;
  331. return -EAGAIN;
  332. }
  333. if (service_rc == 8 && service_rs == 783) {
  334. zdev->max_mod_size = PCICC_MAX_MOD_SIZE_OLD;
  335. return -EAGAIN;
  336. }
  337. if (service_rc == 8 && service_rs == 72)
  338. return -EINVAL;
  339. zdev->online = 0;
  340. return -EAGAIN; /* repeat the request on a different device. */
  341. }
  342. data = msg->text;
  343. reply_len = le16_to_cpu(msg->length) - 2;
  344. if (reply_len > outputdatalength)
  345. return -EINVAL;
  346. /*
  347. * For all encipher requests, the length of the ciphertext (reply_len)
  348. * will always equal the modulus length. For MEX decipher requests
  349. * the output needs to get padded. Minimum pad size is 10.
  350. *
  351. * Currently, the cases where padding will be added is for:
  352. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  353. * ZERO-PAD and CRT is only supported for PKD requests)
  354. * - PCICC, always
  355. */
  356. pad_len = outputdatalength - reply_len;
  357. if (pad_len > 0) {
  358. if (pad_len < 10)
  359. return -EINVAL;
  360. /* 'restore' padding left in the PCICC/PCIXCC card. */
  361. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  362. return -EFAULT;
  363. if (put_user(0, outputdata + pad_len - 1))
  364. return -EFAULT;
  365. }
  366. /* Copy the crypto response to user space. */
  367. if (copy_to_user(outputdata + pad_len, data, reply_len))
  368. return -EFAULT;
  369. return 0;
  370. }
  371. static int convert_response(struct zcrypt_device *zdev,
  372. struct ap_message *reply,
  373. char __user *outputdata,
  374. unsigned int outputdatalength)
  375. {
  376. struct type86_reply *msg = reply->message;
  377. /* Response type byte is the second byte in the response. */
  378. switch (msg->hdr.type) {
  379. case TYPE82_RSP_CODE:
  380. case TYPE88_RSP_CODE:
  381. return convert_error(zdev, reply);
  382. case TYPE86_RSP_CODE:
  383. if (msg->hdr.reply_code)
  384. return convert_error(zdev, reply);
  385. if (msg->cprb.cprb_ver_id == 0x01)
  386. return convert_type86(zdev, reply,
  387. outputdata, outputdatalength);
  388. /* no break, incorrect cprb version is an unknown response */
  389. default: /* Unknown response type, this should NEVER EVER happen */
  390. zdev->online = 0;
  391. return -EAGAIN; /* repeat the request on a different device. */
  392. }
  393. }
  394. /**
  395. * This function is called from the AP bus code after a crypto request
  396. * "msg" has finished with the reply message "reply".
  397. * It is called from tasklet context.
  398. * @ap_dev: pointer to the AP device
  399. * @msg: pointer to the AP message
  400. * @reply: pointer to the AP reply message
  401. */
  402. static void zcrypt_pcicc_receive(struct ap_device *ap_dev,
  403. struct ap_message *msg,
  404. struct ap_message *reply)
  405. {
  406. static struct error_hdr error_reply = {
  407. .type = TYPE82_RSP_CODE,
  408. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  409. };
  410. struct type86_reply *t86r;
  411. int length;
  412. /* Copy the reply message to the request message buffer. */
  413. if (IS_ERR(reply)) {
  414. memcpy(msg->message, &error_reply, sizeof(error_reply));
  415. goto out;
  416. }
  417. t86r = reply->message;
  418. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  419. t86r->cprb.cprb_ver_id == 0x01) {
  420. length = sizeof(struct type86_reply) + t86r->length - 2;
  421. length = min(PCICC_MAX_RESPONSE_SIZE, length);
  422. memcpy(msg->message, reply->message, length);
  423. } else
  424. memcpy(msg->message, reply->message, sizeof error_reply);
  425. out:
  426. complete((struct completion *) msg->private);
  427. }
  428. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  429. /**
  430. * The request distributor calls this function if it picked the PCICC
  431. * device to handle a modexpo request.
  432. * @zdev: pointer to zcrypt_device structure that identifies the
  433. * PCICC device to the request distributor
  434. * @mex: pointer to the modexpo request buffer
  435. */
  436. static long zcrypt_pcicc_modexpo(struct zcrypt_device *zdev,
  437. struct ica_rsa_modexpo *mex)
  438. {
  439. struct ap_message ap_msg;
  440. struct completion work;
  441. int rc;
  442. ap_init_message(&ap_msg);
  443. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  444. if (!ap_msg.message)
  445. return -ENOMEM;
  446. ap_msg.receive = zcrypt_pcicc_receive;
  447. ap_msg.length = PAGE_SIZE;
  448. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  449. atomic_inc_return(&zcrypt_step);
  450. ap_msg.private = &work;
  451. rc = ICAMEX_msg_to_type6MEX_msg(zdev, &ap_msg, mex);
  452. if (rc)
  453. goto out_free;
  454. init_completion(&work);
  455. ap_queue_message(zdev->ap_dev, &ap_msg);
  456. rc = wait_for_completion_interruptible(&work);
  457. if (rc == 0)
  458. rc = convert_response(zdev, &ap_msg, mex->outputdata,
  459. mex->outputdatalength);
  460. else
  461. /* Signal pending. */
  462. ap_cancel_message(zdev->ap_dev, &ap_msg);
  463. out_free:
  464. free_page((unsigned long) ap_msg.message);
  465. return rc;
  466. }
  467. /**
  468. * The request distributor calls this function if it picked the PCICC
  469. * device to handle a modexpo_crt request.
  470. * @zdev: pointer to zcrypt_device structure that identifies the
  471. * PCICC device to the request distributor
  472. * @crt: pointer to the modexpoc_crt request buffer
  473. */
  474. static long zcrypt_pcicc_modexpo_crt(struct zcrypt_device *zdev,
  475. struct ica_rsa_modexpo_crt *crt)
  476. {
  477. struct ap_message ap_msg;
  478. struct completion work;
  479. int rc;
  480. ap_init_message(&ap_msg);
  481. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  482. if (!ap_msg.message)
  483. return -ENOMEM;
  484. ap_msg.receive = zcrypt_pcicc_receive;
  485. ap_msg.length = PAGE_SIZE;
  486. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  487. atomic_inc_return(&zcrypt_step);
  488. ap_msg.private = &work;
  489. rc = ICACRT_msg_to_type6CRT_msg(zdev, &ap_msg, crt);
  490. if (rc)
  491. goto out_free;
  492. init_completion(&work);
  493. ap_queue_message(zdev->ap_dev, &ap_msg);
  494. rc = wait_for_completion_interruptible(&work);
  495. if (rc == 0)
  496. rc = convert_response(zdev, &ap_msg, crt->outputdata,
  497. crt->outputdatalength);
  498. else
  499. /* Signal pending. */
  500. ap_cancel_message(zdev->ap_dev, &ap_msg);
  501. out_free:
  502. free_page((unsigned long) ap_msg.message);
  503. return rc;
  504. }
  505. /**
  506. * The crypto operations for a PCICC card.
  507. */
  508. static struct zcrypt_ops zcrypt_pcicc_ops = {
  509. .rsa_modexpo = zcrypt_pcicc_modexpo,
  510. .rsa_modexpo_crt = zcrypt_pcicc_modexpo_crt,
  511. };
  512. /**
  513. * Probe function for PCICC cards. It always accepts the AP device
  514. * since the bus_match already checked the hardware type.
  515. * @ap_dev: pointer to the AP device.
  516. */
  517. static int zcrypt_pcicc_probe(struct ap_device *ap_dev)
  518. {
  519. struct zcrypt_device *zdev;
  520. int rc;
  521. zdev = zcrypt_device_alloc(PCICC_MAX_RESPONSE_SIZE);
  522. if (!zdev)
  523. return -ENOMEM;
  524. zdev->ap_dev = ap_dev;
  525. zdev->ops = &zcrypt_pcicc_ops;
  526. zdev->online = 1;
  527. zdev->user_space_type = ZCRYPT_PCICC;
  528. zdev->type_string = "PCICC";
  529. zdev->min_mod_size = PCICC_MIN_MOD_SIZE;
  530. zdev->max_mod_size = PCICC_MAX_MOD_SIZE;
  531. zdev->speed_rating = PCICC_SPEED_RATING;
  532. zdev->max_exp_bit_length = PCICC_MAX_MOD_SIZE;
  533. ap_dev->reply = &zdev->reply;
  534. ap_dev->private = zdev;
  535. rc = zcrypt_device_register(zdev);
  536. if (rc)
  537. goto out_free;
  538. return 0;
  539. out_free:
  540. ap_dev->private = NULL;
  541. zcrypt_device_free(zdev);
  542. return rc;
  543. }
  544. /**
  545. * This is called to remove the extended PCICC driver information
  546. * if an AP device is removed.
  547. */
  548. static void zcrypt_pcicc_remove(struct ap_device *ap_dev)
  549. {
  550. struct zcrypt_device *zdev = ap_dev->private;
  551. zcrypt_device_unregister(zdev);
  552. }
  553. int __init zcrypt_pcicc_init(void)
  554. {
  555. return ap_driver_register(&zcrypt_pcicc_driver, THIS_MODULE, "pcicc");
  556. }
  557. void zcrypt_pcicc_exit(void)
  558. {
  559. ap_driver_unregister(&zcrypt_pcicc_driver);
  560. }
  561. module_init(zcrypt_pcicc_init);
  562. module_exit(zcrypt_pcicc_exit);