zcrypt_pcixcc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. * linux/drivers/s390/crypto/zcrypt_pcixcc.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 <linux/delay.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_pcicc.h"
  38. #include "zcrypt_pcixcc.h"
  39. #include "zcrypt_cca_key.h"
  40. #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
  41. #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
  42. #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
  43. #define PCIXCC_MCL2_SPEED_RATING 7870 /* FIXME: needs finetuning */
  44. #define PCIXCC_MCL3_SPEED_RATING 7870
  45. #define CEX2C_SPEED_RATING 8540
  46. #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
  47. #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
  48. #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
  49. #define PCIXCC_MAX_XCRB_RESPONSE_SIZE PCIXCC_MAX_XCRB_MESSAGE_SIZE
  50. #define PCIXCC_MAX_XCRB_DATA_SIZE (11*1024)
  51. #define PCIXCC_MAX_XCRB_REPLY_SIZE (5*1024)
  52. #define PCIXCC_MAX_RESPONSE_SIZE PCIXCC_MAX_XCRB_RESPONSE_SIZE
  53. #define PCIXCC_CLEANUP_TIME (15*HZ)
  54. #define CEIL4(x) ((((x)+3)/4)*4)
  55. struct response_type {
  56. struct completion work;
  57. int type;
  58. };
  59. #define PCIXCC_RESPONSE_TYPE_ICA 0
  60. #define PCIXCC_RESPONSE_TYPE_XCRB 1
  61. static struct ap_device_id zcrypt_pcixcc_ids[] = {
  62. { AP_DEVICE(AP_DEVICE_TYPE_PCIXCC) },
  63. { AP_DEVICE(AP_DEVICE_TYPE_CEX2C) },
  64. { /* end of list */ },
  65. };
  66. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  67. MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_ids);
  68. MODULE_AUTHOR("IBM Corporation");
  69. MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, "
  70. "Copyright 2001, 2006 IBM Corporation");
  71. MODULE_LICENSE("GPL");
  72. #endif
  73. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev);
  74. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev);
  75. static void zcrypt_pcixcc_receive(struct ap_device *, struct ap_message *,
  76. struct ap_message *);
  77. static struct ap_driver zcrypt_pcixcc_driver = {
  78. .probe = zcrypt_pcixcc_probe,
  79. .remove = zcrypt_pcixcc_remove,
  80. .receive = zcrypt_pcixcc_receive,
  81. .ids = zcrypt_pcixcc_ids,
  82. .request_timeout = PCIXCC_CLEANUP_TIME,
  83. };
  84. /**
  85. * The following is used to initialize the CPRBX passed to the PCIXCC/CEX2C
  86. * card in a type6 message. The 3 fields that must be filled in at execution
  87. * time are req_parml, rpl_parml and usage_domain.
  88. * Everything about this interface is ascii/big-endian, since the
  89. * device does *not* have 'Intel inside'.
  90. *
  91. * The CPRBX is followed immediately by the parm block.
  92. * The parm block contains:
  93. * - function code ('PD' 0x5044 or 'PK' 0x504B)
  94. * - rule block (one of:)
  95. * + 0x000A 'PKCS-1.2' (MCL2 'PD')
  96. * + 0x000A 'ZERO-PAD' (MCL2 'PK')
  97. * + 0x000A 'ZERO-PAD' (MCL3 'PD' or CEX2C 'PD')
  98. * + 0x000A 'MRP ' (MCL3 'PK' or CEX2C 'PK')
  99. * - VUD block
  100. */
  101. static struct CPRBX static_cprbx = {
  102. .cprb_len = 0x00DC,
  103. .cprb_ver_id = 0x02,
  104. .func_id = {0x54,0x32},
  105. };
  106. /**
  107. * Convert a ICAMEX message to a type6 MEX message.
  108. *
  109. * @zdev: crypto device pointer
  110. * @ap_msg: pointer to AP message
  111. * @mex: pointer to user input data
  112. *
  113. * Returns 0 on success or -EFAULT.
  114. */
  115. static int ICAMEX_msg_to_type6MEX_msgX(struct zcrypt_device *zdev,
  116. struct ap_message *ap_msg,
  117. struct ica_rsa_modexpo *mex)
  118. {
  119. static struct type6_hdr static_type6_hdrX = {
  120. .type = 0x06,
  121. .offset1 = 0x00000058,
  122. .agent_id = {'C','A',},
  123. .function_code = {'P','K'},
  124. };
  125. static struct function_and_rules_block static_pke_fnr = {
  126. .function_code = {'P','K'},
  127. .ulen = 10,
  128. .only_rule = {'M','R','P',' ',' ',' ',' ',' '}
  129. };
  130. static struct function_and_rules_block static_pke_fnr_MCL2 = {
  131. .function_code = {'P','K'},
  132. .ulen = 10,
  133. .only_rule = {'Z','E','R','O','-','P','A','D'}
  134. };
  135. struct {
  136. struct type6_hdr hdr;
  137. struct CPRBX cprbx;
  138. struct function_and_rules_block fr;
  139. unsigned short length;
  140. char text[0];
  141. } __attribute__((packed)) *msg = ap_msg->message;
  142. int size;
  143. /* VUD.ciphertext */
  144. msg->length = mex->inputdatalength + 2;
  145. if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
  146. return -EFAULT;
  147. /* Set up key which is located after the variable length text. */
  148. size = zcrypt_type6_mex_key_en(mex, msg->text+mex->inputdatalength, 1);
  149. if (size < 0)
  150. return size;
  151. size += sizeof(*msg) + mex->inputdatalength;
  152. /* message header, cprbx and f&r */
  153. msg->hdr = static_type6_hdrX;
  154. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  155. msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  156. msg->cprbx = static_cprbx;
  157. msg->cprbx.domain = AP_QID_QUEUE(zdev->ap_dev->qid);
  158. msg->cprbx.rpl_msgbl = msg->hdr.FromCardLen1;
  159. msg->fr = (zdev->user_space_type == ZCRYPT_PCIXCC_MCL2) ?
  160. static_pke_fnr_MCL2 : static_pke_fnr;
  161. msg->cprbx.req_parml = size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  162. ap_msg->length = size;
  163. return 0;
  164. }
  165. /**
  166. * Convert a ICACRT message to a type6 CRT message.
  167. *
  168. * @zdev: crypto device pointer
  169. * @ap_msg: pointer to AP message
  170. * @crt: pointer to user input data
  171. *
  172. * Returns 0 on success or -EFAULT.
  173. */
  174. static int ICACRT_msg_to_type6CRT_msgX(struct zcrypt_device *zdev,
  175. struct ap_message *ap_msg,
  176. struct ica_rsa_modexpo_crt *crt)
  177. {
  178. static struct type6_hdr static_type6_hdrX = {
  179. .type = 0x06,
  180. .offset1 = 0x00000058,
  181. .agent_id = {'C','A',},
  182. .function_code = {'P','D'},
  183. };
  184. static struct function_and_rules_block static_pkd_fnr = {
  185. .function_code = {'P','D'},
  186. .ulen = 10,
  187. .only_rule = {'Z','E','R','O','-','P','A','D'}
  188. };
  189. static struct function_and_rules_block static_pkd_fnr_MCL2 = {
  190. .function_code = {'P','D'},
  191. .ulen = 10,
  192. .only_rule = {'P','K','C','S','-','1','.','2'}
  193. };
  194. struct {
  195. struct type6_hdr hdr;
  196. struct CPRBX cprbx;
  197. struct function_and_rules_block fr;
  198. unsigned short length;
  199. char text[0];
  200. } __attribute__((packed)) *msg = ap_msg->message;
  201. int size;
  202. /* VUD.ciphertext */
  203. msg->length = crt->inputdatalength + 2;
  204. if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
  205. return -EFAULT;
  206. /* Set up key which is located after the variable length text. */
  207. size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength, 1);
  208. if (size < 0)
  209. return size;
  210. size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
  211. /* message header, cprbx and f&r */
  212. msg->hdr = static_type6_hdrX;
  213. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  214. msg->hdr.FromCardLen1 = PCIXCC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  215. msg->cprbx = static_cprbx;
  216. msg->cprbx.domain = AP_QID_QUEUE(zdev->ap_dev->qid);
  217. msg->cprbx.req_parml = msg->cprbx.rpl_msgbl =
  218. size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  219. msg->fr = (zdev->user_space_type == ZCRYPT_PCIXCC_MCL2) ?
  220. static_pkd_fnr_MCL2 : static_pkd_fnr;
  221. ap_msg->length = size;
  222. return 0;
  223. }
  224. /**
  225. * Convert a XCRB message to a type6 CPRB message.
  226. *
  227. * @zdev: crypto device pointer
  228. * @ap_msg: pointer to AP message
  229. * @xcRB: pointer to user input data
  230. *
  231. * Returns 0 on success or -EFAULT.
  232. */
  233. struct type86_fmt2_msg {
  234. struct type86_hdr hdr;
  235. struct type86_fmt2_ext fmt2;
  236. } __attribute__((packed));
  237. static int XCRB_msg_to_type6CPRB_msgX(struct zcrypt_device *zdev,
  238. struct ap_message *ap_msg,
  239. struct ica_xcRB *xcRB)
  240. {
  241. static struct type6_hdr static_type6_hdrX = {
  242. .type = 0x06,
  243. .offset1 = 0x00000058,
  244. };
  245. struct {
  246. struct type6_hdr hdr;
  247. struct ica_CPRBX cprbx;
  248. } __attribute__((packed)) *msg = ap_msg->message;
  249. int rcblen = CEIL4(xcRB->request_control_blk_length);
  250. int replylen;
  251. char *req_data = ap_msg->message + sizeof(struct type6_hdr) + rcblen;
  252. char *function_code;
  253. /* length checks */
  254. ap_msg->length = sizeof(struct type6_hdr) +
  255. CEIL4(xcRB->request_control_blk_length) +
  256. xcRB->request_data_length;
  257. if (ap_msg->length > PCIXCC_MAX_XCRB_MESSAGE_SIZE) {
  258. PRINTK("Combined message is too large (%ld/%d/%d).\n",
  259. sizeof(struct type6_hdr),
  260. xcRB->request_control_blk_length,
  261. xcRB->request_data_length);
  262. return -EFAULT;
  263. }
  264. if (CEIL4(xcRB->reply_control_blk_length) >
  265. PCIXCC_MAX_XCRB_REPLY_SIZE) {
  266. PDEBUG("Reply CPRB length is too large (%d).\n",
  267. xcRB->request_control_blk_length);
  268. return -EFAULT;
  269. }
  270. if (CEIL4(xcRB->reply_data_length) > PCIXCC_MAX_XCRB_DATA_SIZE) {
  271. PDEBUG("Reply data block length is too large (%d).\n",
  272. xcRB->reply_data_length);
  273. return -EFAULT;
  274. }
  275. replylen = CEIL4(xcRB->reply_control_blk_length) +
  276. CEIL4(xcRB->reply_data_length) +
  277. sizeof(struct type86_fmt2_msg);
  278. if (replylen > PCIXCC_MAX_XCRB_RESPONSE_SIZE) {
  279. PDEBUG("Reply CPRB + data block > PCIXCC_MAX_XCRB_RESPONSE_SIZE"
  280. " (%d/%d/%d).\n",
  281. sizeof(struct type86_fmt2_msg),
  282. xcRB->reply_control_blk_length,
  283. xcRB->reply_data_length);
  284. xcRB->reply_control_blk_length = PCIXCC_MAX_XCRB_RESPONSE_SIZE -
  285. (sizeof(struct type86_fmt2_msg) +
  286. CEIL4(xcRB->reply_data_length));
  287. PDEBUG("Capping Reply CPRB length at %d\n",
  288. xcRB->reply_control_blk_length);
  289. }
  290. /* prepare type6 header */
  291. msg->hdr = static_type6_hdrX;
  292. memcpy(msg->hdr.agent_id , &(xcRB->agent_ID), sizeof(xcRB->agent_ID));
  293. msg->hdr.ToCardLen1 = xcRB->request_control_blk_length;
  294. if (xcRB->request_data_length) {
  295. msg->hdr.offset2 = msg->hdr.offset1 + rcblen;
  296. msg->hdr.ToCardLen2 = xcRB->request_data_length;
  297. }
  298. msg->hdr.FromCardLen1 = xcRB->reply_control_blk_length;
  299. msg->hdr.FromCardLen2 = xcRB->reply_data_length;
  300. /* prepare CPRB */
  301. if (copy_from_user(&(msg->cprbx), xcRB->request_control_blk_addr,
  302. xcRB->request_control_blk_length))
  303. return -EFAULT;
  304. if (msg->cprbx.cprb_len + sizeof(msg->hdr.function_code) >
  305. xcRB->request_control_blk_length) {
  306. PDEBUG("cprb_len too large (%d/%d)\n", msg->cprbx.cprb_len,
  307. xcRB->request_control_blk_length);
  308. return -EFAULT;
  309. }
  310. function_code = ((unsigned char *)&msg->cprbx) + msg->cprbx.cprb_len;
  311. memcpy(msg->hdr.function_code, function_code, sizeof(msg->hdr.function_code));
  312. /* copy data block */
  313. if (xcRB->request_data_length &&
  314. copy_from_user(req_data, xcRB->request_data_address,
  315. xcRB->request_data_length))
  316. return -EFAULT;
  317. return 0;
  318. }
  319. /**
  320. * Copy results from a type 86 ICA reply message back to user space.
  321. *
  322. * @zdev: crypto device pointer
  323. * @reply: reply AP message.
  324. * @data: pointer to user output data
  325. * @length: size of user output data
  326. *
  327. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  328. */
  329. struct type86x_reply {
  330. struct type86_hdr hdr;
  331. struct type86_fmt2_ext fmt2;
  332. struct CPRBX cprbx;
  333. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  334. unsigned short length;
  335. char text[0];
  336. } __attribute__((packed));
  337. static int convert_type86_ica(struct zcrypt_device *zdev,
  338. struct ap_message *reply,
  339. char __user *outputdata,
  340. unsigned int outputdatalength)
  341. {
  342. static unsigned char static_pad[] = {
  343. 0x00,0x02,
  344. 0x1B,0x7B,0x5D,0xB5,0x75,0x01,0x3D,0xFD,
  345. 0x8D,0xD1,0xC7,0x03,0x2D,0x09,0x23,0x57,
  346. 0x89,0x49,0xB9,0x3F,0xBB,0x99,0x41,0x5B,
  347. 0x75,0x21,0x7B,0x9D,0x3B,0x6B,0x51,0x39,
  348. 0xBB,0x0D,0x35,0xB9,0x89,0x0F,0x93,0xA5,
  349. 0x0B,0x47,0xF1,0xD3,0xBB,0xCB,0xF1,0x9D,
  350. 0x23,0x73,0x71,0xFF,0xF3,0xF5,0x45,0xFB,
  351. 0x61,0x29,0x23,0xFD,0xF1,0x29,0x3F,0x7F,
  352. 0x17,0xB7,0x1B,0xA9,0x19,0xBD,0x57,0xA9,
  353. 0xD7,0x95,0xA3,0xCB,0xED,0x1D,0xDB,0x45,
  354. 0x7D,0x11,0xD1,0x51,0x1B,0xED,0x71,0xE9,
  355. 0xB1,0xD1,0xAB,0xAB,0x21,0x2B,0x1B,0x9F,
  356. 0x3B,0x9F,0xF7,0xF7,0xBD,0x63,0xEB,0xAD,
  357. 0xDF,0xB3,0x6F,0x5B,0xDB,0x8D,0xA9,0x5D,
  358. 0xE3,0x7D,0x77,0x49,0x47,0xF5,0xA7,0xFD,
  359. 0xAB,0x2F,0x27,0x35,0x77,0xD3,0x49,0xC9,
  360. 0x09,0xEB,0xB1,0xF9,0xBF,0x4B,0xCB,0x2B,
  361. 0xEB,0xEB,0x05,0xFF,0x7D,0xC7,0x91,0x8B,
  362. 0x09,0x83,0xB9,0xB9,0x69,0x33,0x39,0x6B,
  363. 0x79,0x75,0x19,0xBF,0xBB,0x07,0x1D,0xBD,
  364. 0x29,0xBF,0x39,0x95,0x93,0x1D,0x35,0xC7,
  365. 0xC9,0x4D,0xE5,0x97,0x0B,0x43,0x9B,0xF1,
  366. 0x16,0x93,0x03,0x1F,0xA5,0xFB,0xDB,0xF3,
  367. 0x27,0x4F,0x27,0x61,0x05,0x1F,0xB9,0x23,
  368. 0x2F,0xC3,0x81,0xA9,0x23,0x71,0x55,0x55,
  369. 0xEB,0xED,0x41,0xE5,0xF3,0x11,0xF1,0x43,
  370. 0x69,0x03,0xBD,0x0B,0x37,0x0F,0x51,0x8F,
  371. 0x0B,0xB5,0x89,0x5B,0x67,0xA9,0xD9,0x4F,
  372. 0x01,0xF9,0x21,0x77,0x37,0x73,0x79,0xC5,
  373. 0x7F,0x51,0xC1,0xCF,0x97,0xA1,0x75,0xAD,
  374. 0x35,0x9D,0xD3,0xD3,0xA7,0x9D,0x5D,0x41,
  375. 0x6F,0x65,0x1B,0xCF,0xA9,0x87,0x91,0x09
  376. };
  377. struct type86x_reply *msg = reply->message;
  378. unsigned short service_rc, service_rs;
  379. unsigned int reply_len, pad_len;
  380. char *data;
  381. service_rc = msg->cprbx.ccp_rtcode;
  382. if (unlikely(service_rc != 0)) {
  383. service_rs = msg->cprbx.ccp_rscode;
  384. if (service_rc == 8 && service_rs == 66) {
  385. PDEBUG("Bad block format on PCIXCC/CEX2C\n");
  386. return -EINVAL;
  387. }
  388. if (service_rc == 8 && service_rs == 65) {
  389. PDEBUG("Probably an even modulus on PCIXCC/CEX2C\n");
  390. return -EINVAL;
  391. }
  392. if (service_rc == 8 && service_rs == 770) {
  393. PDEBUG("Invalid key length on PCIXCC/CEX2C\n");
  394. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  395. return -EAGAIN;
  396. }
  397. if (service_rc == 8 && service_rs == 783) {
  398. PDEBUG("Extended bitlengths not enabled on PCIXCC/CEX2C\n");
  399. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  400. return -EAGAIN;
  401. }
  402. PRINTK("Unknown service rc/rs (PCIXCC/CEX2C): %d/%d\n",
  403. service_rc, service_rs);
  404. zdev->online = 0;
  405. return -EAGAIN; /* repeat the request on a different device. */
  406. }
  407. data = msg->text;
  408. reply_len = msg->length - 2;
  409. if (reply_len > outputdatalength)
  410. return -EINVAL;
  411. /**
  412. * For all encipher requests, the length of the ciphertext (reply_len)
  413. * will always equal the modulus length. For MEX decipher requests
  414. * the output needs to get padded. Minimum pad size is 10.
  415. *
  416. * Currently, the cases where padding will be added is for:
  417. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  418. * ZERO-PAD and CRT is only supported for PKD requests)
  419. * - PCICC, always
  420. */
  421. pad_len = outputdatalength - reply_len;
  422. if (pad_len > 0) {
  423. if (pad_len < 10)
  424. return -EINVAL;
  425. /* 'restore' padding left in the PCICC/PCIXCC card. */
  426. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  427. return -EFAULT;
  428. if (put_user(0, outputdata + pad_len - 1))
  429. return -EFAULT;
  430. }
  431. /* Copy the crypto response to user space. */
  432. if (copy_to_user(outputdata + pad_len, data, reply_len))
  433. return -EFAULT;
  434. return 0;
  435. }
  436. /**
  437. * Copy results from a type 86 XCRB reply message back to user space.
  438. *
  439. * @zdev: crypto device pointer
  440. * @reply: reply AP message.
  441. * @xcRB: pointer to XCRB
  442. *
  443. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  444. */
  445. static int convert_type86_xcrb(struct zcrypt_device *zdev,
  446. struct ap_message *reply,
  447. struct ica_xcRB *xcRB)
  448. {
  449. struct type86_fmt2_msg *msg = reply->message;
  450. char *data = reply->message;
  451. /* Copy CPRB to user */
  452. if (copy_to_user(xcRB->reply_control_blk_addr,
  453. data + msg->fmt2.offset1, msg->fmt2.count1))
  454. return -EFAULT;
  455. xcRB->reply_control_blk_length = msg->fmt2.count1;
  456. /* Copy data buffer to user */
  457. if (msg->fmt2.count2)
  458. if (copy_to_user(xcRB->reply_data_addr,
  459. data + msg->fmt2.offset2, msg->fmt2.count2))
  460. return -EFAULT;
  461. xcRB->reply_data_length = msg->fmt2.count2;
  462. return 0;
  463. }
  464. static int convert_response_ica(struct zcrypt_device *zdev,
  465. struct ap_message *reply,
  466. char __user *outputdata,
  467. unsigned int outputdatalength)
  468. {
  469. struct type86x_reply *msg = reply->message;
  470. /* Response type byte is the second byte in the response. */
  471. switch (((unsigned char *) reply->message)[1]) {
  472. case TYPE82_RSP_CODE:
  473. case TYPE88_RSP_CODE:
  474. return convert_error(zdev, reply);
  475. case TYPE86_RSP_CODE:
  476. if (msg->hdr.reply_code)
  477. return convert_error(zdev, reply);
  478. if (msg->cprbx.cprb_ver_id == 0x02)
  479. return convert_type86_ica(zdev, reply,
  480. outputdata, outputdatalength);
  481. /* no break, incorrect cprb version is an unknown response */
  482. default: /* Unknown response type, this should NEVER EVER happen */
  483. PRINTK("Unrecognized Message Header: %08x%08x\n",
  484. *(unsigned int *) reply->message,
  485. *(unsigned int *) (reply->message+4));
  486. zdev->online = 0;
  487. return -EAGAIN; /* repeat the request on a different device. */
  488. }
  489. }
  490. static int convert_response_xcrb(struct zcrypt_device *zdev,
  491. struct ap_message *reply,
  492. struct ica_xcRB *xcRB)
  493. {
  494. struct type86x_reply *msg = reply->message;
  495. /* Response type byte is the second byte in the response. */
  496. switch (((unsigned char *) reply->message)[1]) {
  497. case TYPE82_RSP_CODE:
  498. case TYPE88_RSP_CODE:
  499. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  500. return convert_error(zdev, reply);
  501. case TYPE86_RSP_CODE:
  502. if (msg->hdr.reply_code) {
  503. memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
  504. return convert_error(zdev, reply);
  505. }
  506. if (msg->cprbx.cprb_ver_id == 0x02)
  507. return convert_type86_xcrb(zdev, reply, xcRB);
  508. /* no break, incorrect cprb version is an unknown response */
  509. default: /* Unknown response type, this should NEVER EVER happen */
  510. PRINTK("Unrecognized Message Header: %08x%08x\n",
  511. *(unsigned int *) reply->message,
  512. *(unsigned int *) (reply->message+4));
  513. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  514. zdev->online = 0;
  515. return -EAGAIN; /* repeat the request on a different device. */
  516. }
  517. }
  518. /**
  519. * This function is called from the AP bus code after a crypto request
  520. * "msg" has finished with the reply message "reply".
  521. * It is called from tasklet context.
  522. * @ap_dev: pointer to the AP device
  523. * @msg: pointer to the AP message
  524. * @reply: pointer to the AP reply message
  525. */
  526. static void zcrypt_pcixcc_receive(struct ap_device *ap_dev,
  527. struct ap_message *msg,
  528. struct ap_message *reply)
  529. {
  530. static struct error_hdr error_reply = {
  531. .type = TYPE82_RSP_CODE,
  532. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  533. };
  534. struct response_type *resp_type =
  535. (struct response_type *) msg->private;
  536. struct type86x_reply *t86r = reply->message;
  537. int length;
  538. /* Copy the reply message to the request message buffer. */
  539. if (IS_ERR(reply))
  540. memcpy(msg->message, &error_reply, sizeof(error_reply));
  541. else if (t86r->hdr.type == TYPE86_RSP_CODE &&
  542. t86r->cprbx.cprb_ver_id == 0x02) {
  543. switch (resp_type->type) {
  544. case PCIXCC_RESPONSE_TYPE_ICA:
  545. length = sizeof(struct type86x_reply)
  546. + t86r->length - 2;
  547. length = min(PCIXCC_MAX_ICA_RESPONSE_SIZE, length);
  548. memcpy(msg->message, reply->message, length);
  549. break;
  550. case PCIXCC_RESPONSE_TYPE_XCRB:
  551. length = t86r->fmt2.offset2 + t86r->fmt2.count2;
  552. length = min(PCIXCC_MAX_XCRB_RESPONSE_SIZE, length);
  553. memcpy(msg->message, reply->message, length);
  554. break;
  555. default:
  556. PRINTK("Invalid internal response type: %i\n",
  557. resp_type->type);
  558. memcpy(msg->message, &error_reply,
  559. sizeof error_reply);
  560. }
  561. } else
  562. memcpy(msg->message, reply->message, sizeof error_reply);
  563. complete(&(resp_type->work));
  564. }
  565. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  566. /**
  567. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  568. * device to handle a modexpo request.
  569. * @zdev: pointer to zcrypt_device structure that identifies the
  570. * PCIXCC/CEX2C device to the request distributor
  571. * @mex: pointer to the modexpo request buffer
  572. */
  573. static long zcrypt_pcixcc_modexpo(struct zcrypt_device *zdev,
  574. struct ica_rsa_modexpo *mex)
  575. {
  576. struct ap_message ap_msg;
  577. struct response_type resp_type = {
  578. .type = PCIXCC_RESPONSE_TYPE_ICA,
  579. };
  580. int rc;
  581. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  582. if (!ap_msg.message)
  583. return -ENOMEM;
  584. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  585. atomic_inc_return(&zcrypt_step);
  586. ap_msg.private = &resp_type;
  587. rc = ICAMEX_msg_to_type6MEX_msgX(zdev, &ap_msg, mex);
  588. if (rc)
  589. goto out_free;
  590. init_completion(&resp_type.work);
  591. ap_queue_message(zdev->ap_dev, &ap_msg);
  592. rc = wait_for_completion_interruptible(&resp_type.work);
  593. if (rc == 0)
  594. rc = convert_response_ica(zdev, &ap_msg, mex->outputdata,
  595. mex->outputdatalength);
  596. else
  597. /* Signal pending. */
  598. ap_cancel_message(zdev->ap_dev, &ap_msg);
  599. out_free:
  600. free_page((unsigned long) ap_msg.message);
  601. return rc;
  602. }
  603. /**
  604. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  605. * device to handle a modexpo_crt request.
  606. * @zdev: pointer to zcrypt_device structure that identifies the
  607. * PCIXCC/CEX2C device to the request distributor
  608. * @crt: pointer to the modexpoc_crt request buffer
  609. */
  610. static long zcrypt_pcixcc_modexpo_crt(struct zcrypt_device *zdev,
  611. struct ica_rsa_modexpo_crt *crt)
  612. {
  613. struct ap_message ap_msg;
  614. struct response_type resp_type = {
  615. .type = PCIXCC_RESPONSE_TYPE_ICA,
  616. };
  617. int rc;
  618. ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
  619. if (!ap_msg.message)
  620. return -ENOMEM;
  621. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  622. atomic_inc_return(&zcrypt_step);
  623. ap_msg.private = &resp_type;
  624. rc = ICACRT_msg_to_type6CRT_msgX(zdev, &ap_msg, crt);
  625. if (rc)
  626. goto out_free;
  627. init_completion(&resp_type.work);
  628. ap_queue_message(zdev->ap_dev, &ap_msg);
  629. rc = wait_for_completion_interruptible(&resp_type.work);
  630. if (rc == 0)
  631. rc = convert_response_ica(zdev, &ap_msg, crt->outputdata,
  632. crt->outputdatalength);
  633. else
  634. /* Signal pending. */
  635. ap_cancel_message(zdev->ap_dev, &ap_msg);
  636. out_free:
  637. free_page((unsigned long) ap_msg.message);
  638. return rc;
  639. }
  640. /**
  641. * The request distributor calls this function if it picked the PCIXCC/CEX2C
  642. * device to handle a send_cprb request.
  643. * @zdev: pointer to zcrypt_device structure that identifies the
  644. * PCIXCC/CEX2C device to the request distributor
  645. * @xcRB: pointer to the send_cprb request buffer
  646. */
  647. static long zcrypt_pcixcc_send_cprb(struct zcrypt_device *zdev,
  648. struct ica_xcRB *xcRB)
  649. {
  650. struct ap_message ap_msg;
  651. struct response_type resp_type = {
  652. .type = PCIXCC_RESPONSE_TYPE_XCRB,
  653. };
  654. int rc;
  655. ap_msg.message = kmalloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE, GFP_KERNEL);
  656. if (!ap_msg.message)
  657. return -ENOMEM;
  658. ap_msg.psmid = (((unsigned long long) current->pid) << 32) +
  659. atomic_inc_return(&zcrypt_step);
  660. ap_msg.private = &resp_type;
  661. rc = XCRB_msg_to_type6CPRB_msgX(zdev, &ap_msg, xcRB);
  662. if (rc)
  663. goto out_free;
  664. init_completion(&resp_type.work);
  665. ap_queue_message(zdev->ap_dev, &ap_msg);
  666. rc = wait_for_completion_interruptible(&resp_type.work);
  667. if (rc == 0)
  668. rc = convert_response_xcrb(zdev, &ap_msg, xcRB);
  669. else
  670. /* Signal pending. */
  671. ap_cancel_message(zdev->ap_dev, &ap_msg);
  672. out_free:
  673. memset(ap_msg.message, 0x0, ap_msg.length);
  674. kfree(ap_msg.message);
  675. return rc;
  676. }
  677. /**
  678. * The crypto operations for a PCIXCC/CEX2C card.
  679. */
  680. static struct zcrypt_ops zcrypt_pcixcc_ops = {
  681. .rsa_modexpo = zcrypt_pcixcc_modexpo,
  682. .rsa_modexpo_crt = zcrypt_pcixcc_modexpo_crt,
  683. .send_cprb = zcrypt_pcixcc_send_cprb,
  684. };
  685. /**
  686. * Micro-code detection function. Its sends a message to a pcixcc card
  687. * to find out the microcode level.
  688. * @ap_dev: pointer to the AP device.
  689. */
  690. static int zcrypt_pcixcc_mcl(struct ap_device *ap_dev)
  691. {
  692. static unsigned char msg[] = {
  693. 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
  694. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  695. 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
  696. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  697. 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00,
  698. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  699. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00,
  700. 0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00,
  701. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  702. 0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00,
  703. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  704. 0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32,
  705. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8,
  706. 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24,
  707. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  708. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  709. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  710. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  711. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  712. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  713. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  714. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  719. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  720. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  721. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  722. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  723. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  724. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  725. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,
  726. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  727. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  728. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  729. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  730. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  731. 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A,
  732. 0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20,
  733. 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05,
  734. 0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
  735. 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55,
  736. 0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,
  737. 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA,
  738. 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,
  739. 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB,
  740. 0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54,
  741. 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00,
  742. 0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00,
  743. 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40,
  744. 0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C,
  745. 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF,
  746. 0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9,
  747. 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63,
  748. 0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5,
  749. 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A,
  750. 0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01,
  751. 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28,
  752. 0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91,
  753. 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5,
  754. 0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C,
  755. 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98,
  756. 0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96,
  757. 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19,
  758. 0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47,
  759. 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36,
  760. 0xF1,0x3D,0x93,0x53
  761. };
  762. unsigned long long psmid;
  763. struct CPRBX *cprbx;
  764. char *reply;
  765. int rc, i;
  766. reply = (void *) get_zeroed_page(GFP_KERNEL);
  767. if (!reply)
  768. return -ENOMEM;
  769. rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, msg, sizeof(msg));
  770. if (rc)
  771. goto out_free;
  772. /* Wait for the test message to complete. */
  773. for (i = 0; i < 6; i++) {
  774. mdelay(300);
  775. rc = ap_recv(ap_dev->qid, &psmid, reply, 4096);
  776. if (rc == 0 && psmid == 0x0102030405060708ULL)
  777. break;
  778. }
  779. if (i >= 6) {
  780. /* Got no answer. */
  781. rc = -ENODEV;
  782. goto out_free;
  783. }
  784. cprbx = (struct CPRBX *) (reply + 48);
  785. if (cprbx->ccp_rtcode == 8 && cprbx->ccp_rscode == 33)
  786. rc = ZCRYPT_PCIXCC_MCL2;
  787. else
  788. rc = ZCRYPT_PCIXCC_MCL3;
  789. out_free:
  790. free_page((unsigned long) reply);
  791. return rc;
  792. }
  793. /**
  794. * Probe function for PCIXCC/CEX2C cards. It always accepts the AP device
  795. * since the bus_match already checked the hardware type. The PCIXCC
  796. * cards come in two flavours: micro code level 2 and micro code level 3.
  797. * This is checked by sending a test message to the device.
  798. * @ap_dev: pointer to the AP device.
  799. */
  800. static int zcrypt_pcixcc_probe(struct ap_device *ap_dev)
  801. {
  802. struct zcrypt_device *zdev;
  803. int rc;
  804. zdev = zcrypt_device_alloc(PCIXCC_MAX_RESPONSE_SIZE);
  805. if (!zdev)
  806. return -ENOMEM;
  807. zdev->ap_dev = ap_dev;
  808. zdev->ops = &zcrypt_pcixcc_ops;
  809. zdev->online = 1;
  810. if (ap_dev->device_type == AP_DEVICE_TYPE_PCIXCC) {
  811. rc = zcrypt_pcixcc_mcl(ap_dev);
  812. if (rc < 0) {
  813. zcrypt_device_free(zdev);
  814. return rc;
  815. }
  816. zdev->user_space_type = rc;
  817. if (rc == ZCRYPT_PCIXCC_MCL2) {
  818. zdev->type_string = "PCIXCC_MCL2";
  819. zdev->speed_rating = PCIXCC_MCL2_SPEED_RATING;
  820. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD;
  821. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  822. } else {
  823. zdev->type_string = "PCIXCC_MCL3";
  824. zdev->speed_rating = PCIXCC_MCL3_SPEED_RATING;
  825. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  826. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  827. }
  828. } else {
  829. zdev->user_space_type = ZCRYPT_CEX2C;
  830. zdev->type_string = "CEX2C";
  831. zdev->speed_rating = CEX2C_SPEED_RATING;
  832. zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE;
  833. zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE;
  834. }
  835. ap_dev->reply = &zdev->reply;
  836. ap_dev->private = zdev;
  837. rc = zcrypt_device_register(zdev);
  838. if (rc)
  839. goto out_free;
  840. return 0;
  841. out_free:
  842. ap_dev->private = NULL;
  843. zcrypt_device_free(zdev);
  844. return rc;
  845. }
  846. /**
  847. * This is called to remove the extended PCIXCC/CEX2C driver information
  848. * if an AP device is removed.
  849. */
  850. static void zcrypt_pcixcc_remove(struct ap_device *ap_dev)
  851. {
  852. struct zcrypt_device *zdev = ap_dev->private;
  853. zcrypt_device_unregister(zdev);
  854. }
  855. int __init zcrypt_pcixcc_init(void)
  856. {
  857. return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc");
  858. }
  859. void zcrypt_pcixcc_exit(void)
  860. {
  861. ap_driver_unregister(&zcrypt_pcixcc_driver);
  862. }
  863. #ifndef CONFIG_ZCRYPT_MONOLITHIC
  864. module_init(zcrypt_pcixcc_init);
  865. module_exit(zcrypt_pcixcc_exit);
  866. #endif