zcrypt_msgtype6.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2012
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman (edrossma@us.ibm.com)
  6. *
  7. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  8. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  10. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  11. */
  12. #define KMSG_COMPONENT "zcrypt"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <linux/atomic.h>
  20. #include <linux/uaccess.h>
  21. #include "ap_bus.h"
  22. #include "zcrypt_api.h"
  23. #include "zcrypt_error.h"
  24. #include "zcrypt_msgtype6.h"
  25. #include "zcrypt_cca_key.h"
  26. #define CEXXC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
  27. #define CEIL4(x) ((((x)+3)/4)*4)
  28. struct response_type {
  29. struct completion work;
  30. int type;
  31. };
  32. #define CEXXC_RESPONSE_TYPE_ICA 0
  33. #define CEXXC_RESPONSE_TYPE_XCRB 1
  34. #define CEXXC_RESPONSE_TYPE_EP11 2
  35. MODULE_AUTHOR("IBM Corporation");
  36. MODULE_DESCRIPTION("Cryptographic Coprocessor (message type 6), " \
  37. "Copyright IBM Corp. 2001, 2012");
  38. MODULE_LICENSE("GPL");
  39. /**
  40. * CPRB
  41. * Note that all shorts, ints and longs are little-endian.
  42. * All pointer fields are 32-bits long, and mean nothing
  43. *
  44. * A request CPRB is followed by a request_parameter_block.
  45. *
  46. * The request (or reply) parameter block is organized thus:
  47. * function code
  48. * VUD block
  49. * key block
  50. */
  51. struct CPRB {
  52. unsigned short cprb_len; /* CPRB length */
  53. unsigned char cprb_ver_id; /* CPRB version id. */
  54. unsigned char pad_000; /* Alignment pad byte. */
  55. unsigned char srpi_rtcode[4]; /* SRPI return code LELONG */
  56. unsigned char srpi_verb; /* SRPI verb type */
  57. unsigned char flags; /* flags */
  58. unsigned char func_id[2]; /* function id */
  59. unsigned char checkpoint_flag; /* */
  60. unsigned char resv2; /* reserved */
  61. unsigned short req_parml; /* request parameter buffer */
  62. /* length 16-bit little endian */
  63. unsigned char req_parmp[4]; /* request parameter buffer *
  64. * pointer (means nothing: the *
  65. * parameter buffer follows *
  66. * the CPRB). */
  67. unsigned char req_datal[4]; /* request data buffer */
  68. /* length ULELONG */
  69. unsigned char req_datap[4]; /* request data buffer */
  70. /* pointer */
  71. unsigned short rpl_parml; /* reply parameter buffer */
  72. /* length 16-bit little endian */
  73. unsigned char pad_001[2]; /* Alignment pad bytes. ULESHORT */
  74. unsigned char rpl_parmp[4]; /* reply parameter buffer *
  75. * pointer (means nothing: the *
  76. * parameter buffer follows *
  77. * the CPRB). */
  78. unsigned char rpl_datal[4]; /* reply data buffer len ULELONG */
  79. unsigned char rpl_datap[4]; /* reply data buffer */
  80. /* pointer */
  81. unsigned short ccp_rscode; /* server reason code ULESHORT */
  82. unsigned short ccp_rtcode; /* server return code ULESHORT */
  83. unsigned char repd_parml[2]; /* replied parameter len ULESHORT*/
  84. unsigned char mac_data_len[2]; /* Mac Data Length ULESHORT */
  85. unsigned char repd_datal[4]; /* replied data length ULELONG */
  86. unsigned char req_pc[2]; /* PC identifier */
  87. unsigned char res_origin[8]; /* resource origin */
  88. unsigned char mac_value[8]; /* Mac Value */
  89. unsigned char logon_id[8]; /* Logon Identifier */
  90. unsigned char usage_domain[2]; /* cdx */
  91. unsigned char resv3[18]; /* reserved for requestor */
  92. unsigned short svr_namel; /* server name length ULESHORT */
  93. unsigned char svr_name[8]; /* server name */
  94. } __packed;
  95. struct function_and_rules_block {
  96. unsigned char function_code[2];
  97. unsigned short ulen;
  98. unsigned char only_rule[8];
  99. } __packed;
  100. /**
  101. * The following is used to initialize the CPRBX passed to the CEXxC/CEXxP
  102. * card in a type6 message. The 3 fields that must be filled in at execution
  103. * time are req_parml, rpl_parml and usage_domain.
  104. * Everything about this interface is ascii/big-endian, since the
  105. * device does *not* have 'Intel inside'.
  106. *
  107. * The CPRBX is followed immediately by the parm block.
  108. * The parm block contains:
  109. * - function code ('PD' 0x5044 or 'PK' 0x504B)
  110. * - rule block (one of:)
  111. * + 0x000A 'PKCS-1.2' (MCL2 'PD')
  112. * + 0x000A 'ZERO-PAD' (MCL2 'PK')
  113. * + 0x000A 'ZERO-PAD' (MCL3 'PD' or CEX2C 'PD')
  114. * + 0x000A 'MRP ' (MCL3 'PK' or CEX2C 'PK')
  115. * - VUD block
  116. */
  117. static const struct CPRBX static_cprbx = {
  118. .cprb_len = 0x00DC,
  119. .cprb_ver_id = 0x02,
  120. .func_id = {0x54, 0x32},
  121. };
  122. int speed_idx_cca(int req_type)
  123. {
  124. switch (req_type) {
  125. case 0x4142:
  126. case 0x4149:
  127. case 0x414D:
  128. case 0x4341:
  129. case 0x4344:
  130. case 0x4354:
  131. case 0x4358:
  132. case 0x444B:
  133. case 0x4558:
  134. case 0x4643:
  135. case 0x4651:
  136. case 0x4C47:
  137. case 0x4C4B:
  138. case 0x4C51:
  139. case 0x4F48:
  140. case 0x504F:
  141. case 0x5053:
  142. case 0x5058:
  143. case 0x5343:
  144. case 0x5344:
  145. case 0x5345:
  146. case 0x5350:
  147. return LOW;
  148. case 0x414B:
  149. case 0x4345:
  150. case 0x4349:
  151. case 0x434D:
  152. case 0x4847:
  153. case 0x4849:
  154. case 0x484D:
  155. case 0x4850:
  156. case 0x4851:
  157. case 0x4954:
  158. case 0x4958:
  159. case 0x4B43:
  160. case 0x4B44:
  161. case 0x4B45:
  162. case 0x4B47:
  163. case 0x4B48:
  164. case 0x4B49:
  165. case 0x4B4E:
  166. case 0x4B50:
  167. case 0x4B52:
  168. case 0x4B54:
  169. case 0x4B58:
  170. case 0x4D50:
  171. case 0x4D53:
  172. case 0x4D56:
  173. case 0x4D58:
  174. case 0x5044:
  175. case 0x5045:
  176. case 0x5046:
  177. case 0x5047:
  178. case 0x5049:
  179. case 0x504B:
  180. case 0x504D:
  181. case 0x5254:
  182. case 0x5347:
  183. case 0x5349:
  184. case 0x534B:
  185. case 0x534D:
  186. case 0x5356:
  187. case 0x5358:
  188. case 0x5443:
  189. case 0x544B:
  190. case 0x5647:
  191. return HIGH;
  192. default:
  193. return MEDIUM;
  194. }
  195. }
  196. int speed_idx_ep11(int req_type)
  197. {
  198. switch (req_type) {
  199. case 1:
  200. case 2:
  201. case 36:
  202. case 37:
  203. case 38:
  204. case 39:
  205. case 40:
  206. return LOW;
  207. case 17:
  208. case 18:
  209. case 19:
  210. case 20:
  211. case 21:
  212. case 22:
  213. case 26:
  214. case 30:
  215. case 31:
  216. case 32:
  217. case 33:
  218. case 34:
  219. case 35:
  220. return HIGH;
  221. default:
  222. return MEDIUM;
  223. }
  224. }
  225. /**
  226. * Convert a ICAMEX message to a type6 MEX message.
  227. *
  228. * @zq: crypto device pointer
  229. * @ap_msg: pointer to AP message
  230. * @mex: pointer to user input data
  231. *
  232. * Returns 0 on success or negative errno value.
  233. */
  234. static int ICAMEX_msg_to_type6MEX_msgX(struct zcrypt_queue *zq,
  235. struct ap_message *ap_msg,
  236. struct ica_rsa_modexpo *mex)
  237. {
  238. static struct type6_hdr static_type6_hdrX = {
  239. .type = 0x06,
  240. .offset1 = 0x00000058,
  241. .agent_id = {'C', 'A',},
  242. .function_code = {'P', 'K'},
  243. };
  244. static struct function_and_rules_block static_pke_fnr = {
  245. .function_code = {'P', 'K'},
  246. .ulen = 10,
  247. .only_rule = {'M', 'R', 'P', ' ', ' ', ' ', ' ', ' '}
  248. };
  249. struct {
  250. struct type6_hdr hdr;
  251. struct CPRBX cprbx;
  252. struct function_and_rules_block fr;
  253. unsigned short length;
  254. char text[0];
  255. } __packed * msg = ap_msg->msg;
  256. int size;
  257. /*
  258. * The inputdatalength was a selection criteria in the dispatching
  259. * function zcrypt_rsa_modexpo(). However, make sure the following
  260. * copy_from_user() never exceeds the allocated buffer space.
  261. */
  262. if (WARN_ON_ONCE(mex->inputdatalength > PAGE_SIZE))
  263. return -EINVAL;
  264. /* VUD.ciphertext */
  265. msg->length = mex->inputdatalength + 2;
  266. if (copy_from_user(msg->text, mex->inputdata, mex->inputdatalength))
  267. return -EFAULT;
  268. /* Set up key which is located after the variable length text. */
  269. size = zcrypt_type6_mex_key_en(mex, msg->text+mex->inputdatalength);
  270. if (size < 0)
  271. return size;
  272. size += sizeof(*msg) + mex->inputdatalength;
  273. /* message header, cprbx and f&r */
  274. msg->hdr = static_type6_hdrX;
  275. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  276. msg->hdr.FromCardLen1 = CEXXC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  277. msg->cprbx = static_cprbx;
  278. msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
  279. msg->cprbx.rpl_msgbl = msg->hdr.FromCardLen1;
  280. msg->fr = static_pke_fnr;
  281. msg->cprbx.req_parml = size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  282. ap_msg->len = size;
  283. return 0;
  284. }
  285. /**
  286. * Convert a ICACRT message to a type6 CRT message.
  287. *
  288. * @zq: crypto device pointer
  289. * @ap_msg: pointer to AP message
  290. * @crt: pointer to user input data
  291. *
  292. * Returns 0 on success or negative errno value.
  293. */
  294. static int ICACRT_msg_to_type6CRT_msgX(struct zcrypt_queue *zq,
  295. struct ap_message *ap_msg,
  296. struct ica_rsa_modexpo_crt *crt)
  297. {
  298. static struct type6_hdr static_type6_hdrX = {
  299. .type = 0x06,
  300. .offset1 = 0x00000058,
  301. .agent_id = {'C', 'A',},
  302. .function_code = {'P', 'D'},
  303. };
  304. static struct function_and_rules_block static_pkd_fnr = {
  305. .function_code = {'P', 'D'},
  306. .ulen = 10,
  307. .only_rule = {'Z', 'E', 'R', 'O', '-', 'P', 'A', 'D'}
  308. };
  309. struct {
  310. struct type6_hdr hdr;
  311. struct CPRBX cprbx;
  312. struct function_and_rules_block fr;
  313. unsigned short length;
  314. char text[0];
  315. } __packed * msg = ap_msg->msg;
  316. int size;
  317. /*
  318. * The inputdatalength was a selection criteria in the dispatching
  319. * function zcrypt_rsa_crt(). However, make sure the following
  320. * copy_from_user() never exceeds the allocated buffer space.
  321. */
  322. if (WARN_ON_ONCE(crt->inputdatalength > PAGE_SIZE))
  323. return -EINVAL;
  324. /* VUD.ciphertext */
  325. msg->length = crt->inputdatalength + 2;
  326. if (copy_from_user(msg->text, crt->inputdata, crt->inputdatalength))
  327. return -EFAULT;
  328. /* Set up key which is located after the variable length text. */
  329. size = zcrypt_type6_crt_key(crt, msg->text + crt->inputdatalength);
  330. if (size < 0)
  331. return size;
  332. size += sizeof(*msg) + crt->inputdatalength; /* total size of msg */
  333. /* message header, cprbx and f&r */
  334. msg->hdr = static_type6_hdrX;
  335. msg->hdr.ToCardLen1 = size - sizeof(msg->hdr);
  336. msg->hdr.FromCardLen1 = CEXXC_MAX_ICA_RESPONSE_SIZE - sizeof(msg->hdr);
  337. msg->cprbx = static_cprbx;
  338. msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
  339. msg->cprbx.req_parml = msg->cprbx.rpl_msgbl =
  340. size - sizeof(msg->hdr) - sizeof(msg->cprbx);
  341. msg->fr = static_pkd_fnr;
  342. ap_msg->len = size;
  343. return 0;
  344. }
  345. /**
  346. * Convert a XCRB message to a type6 CPRB message.
  347. *
  348. * @zq: crypto device pointer
  349. * @ap_msg: pointer to AP message
  350. * @xcRB: pointer to user input data
  351. *
  352. * Returns 0 on success or -EFAULT, -EINVAL.
  353. */
  354. struct type86_fmt2_msg {
  355. struct type86_hdr hdr;
  356. struct type86_fmt2_ext fmt2;
  357. } __packed;
  358. static int XCRB_msg_to_type6CPRB_msgX(bool userspace, struct ap_message *ap_msg,
  359. struct ica_xcRB *xcRB,
  360. unsigned int *fcode,
  361. unsigned short **dom)
  362. {
  363. static struct type6_hdr static_type6_hdrX = {
  364. .type = 0x06,
  365. .offset1 = 0x00000058,
  366. };
  367. struct {
  368. struct type6_hdr hdr;
  369. struct CPRBX cprbx;
  370. } __packed * msg = ap_msg->msg;
  371. int rcblen = CEIL4(xcRB->request_control_blk_length);
  372. int replylen, req_sumlen, resp_sumlen;
  373. char *req_data = ap_msg->msg + sizeof(struct type6_hdr) + rcblen;
  374. char *function_code;
  375. if (CEIL4(xcRB->request_control_blk_length) <
  376. xcRB->request_control_blk_length)
  377. return -EINVAL; /* overflow after alignment*/
  378. /* length checks */
  379. ap_msg->len = sizeof(struct type6_hdr) +
  380. CEIL4(xcRB->request_control_blk_length) +
  381. xcRB->request_data_length;
  382. if (ap_msg->len > MSGTYPE06_MAX_MSG_SIZE)
  383. return -EINVAL;
  384. /*
  385. * Overflow check
  386. * sum must be greater (or equal) than the largest operand
  387. */
  388. req_sumlen = CEIL4(xcRB->request_control_blk_length) +
  389. xcRB->request_data_length;
  390. if ((CEIL4(xcRB->request_control_blk_length) <=
  391. xcRB->request_data_length) ?
  392. (req_sumlen < xcRB->request_data_length) :
  393. (req_sumlen < CEIL4(xcRB->request_control_blk_length))) {
  394. return -EINVAL;
  395. }
  396. if (CEIL4(xcRB->reply_control_blk_length) <
  397. xcRB->reply_control_blk_length)
  398. return -EINVAL; /* overflow after alignment*/
  399. replylen = sizeof(struct type86_fmt2_msg) +
  400. CEIL4(xcRB->reply_control_blk_length) +
  401. xcRB->reply_data_length;
  402. if (replylen > MSGTYPE06_MAX_MSG_SIZE)
  403. return -EINVAL;
  404. /*
  405. * Overflow check
  406. * sum must be greater (or equal) than the largest operand
  407. */
  408. resp_sumlen = CEIL4(xcRB->reply_control_blk_length) +
  409. xcRB->reply_data_length;
  410. if ((CEIL4(xcRB->reply_control_blk_length) <= xcRB->reply_data_length) ?
  411. (resp_sumlen < xcRB->reply_data_length) :
  412. (resp_sumlen < CEIL4(xcRB->reply_control_blk_length))) {
  413. return -EINVAL;
  414. }
  415. /* prepare type6 header */
  416. msg->hdr = static_type6_hdrX;
  417. memcpy(msg->hdr.agent_id, &(xcRB->agent_ID), sizeof(xcRB->agent_ID));
  418. msg->hdr.ToCardLen1 = xcRB->request_control_blk_length;
  419. if (xcRB->request_data_length) {
  420. msg->hdr.offset2 = msg->hdr.offset1 + rcblen;
  421. msg->hdr.ToCardLen2 = xcRB->request_data_length;
  422. }
  423. msg->hdr.FromCardLen1 = xcRB->reply_control_blk_length;
  424. msg->hdr.FromCardLen2 = xcRB->reply_data_length;
  425. /* prepare CPRB */
  426. if (z_copy_from_user(userspace, &(msg->cprbx), xcRB->request_control_blk_addr,
  427. xcRB->request_control_blk_length))
  428. return -EFAULT;
  429. if (msg->cprbx.cprb_len + sizeof(msg->hdr.function_code) >
  430. xcRB->request_control_blk_length)
  431. return -EINVAL;
  432. function_code = ((unsigned char *)&msg->cprbx) + msg->cprbx.cprb_len;
  433. memcpy(msg->hdr.function_code, function_code,
  434. sizeof(msg->hdr.function_code));
  435. *fcode = (msg->hdr.function_code[0] << 8) | msg->hdr.function_code[1];
  436. *dom = (unsigned short *)&msg->cprbx.domain;
  437. if (memcmp(function_code, "US", 2) == 0
  438. || memcmp(function_code, "AU", 2) == 0)
  439. ap_msg->flags |= AP_MSG_FLAG_SPECIAL;
  440. #ifdef CONFIG_ZCRYPT_DEBUG
  441. if (ap_msg->fi.flags & AP_FI_FLAG_TOGGLE_SPECIAL)
  442. ap_msg->flags ^= AP_MSG_FLAG_SPECIAL;
  443. #endif
  444. /* copy data block */
  445. if (xcRB->request_data_length &&
  446. z_copy_from_user(userspace, req_data, xcRB->request_data_address,
  447. xcRB->request_data_length))
  448. return -EFAULT;
  449. return 0;
  450. }
  451. static int xcrb_msg_to_type6_ep11cprb_msgx(bool userspace, struct ap_message *ap_msg,
  452. struct ep11_urb *xcRB,
  453. unsigned int *fcode)
  454. {
  455. unsigned int lfmt;
  456. static struct type6_hdr static_type6_ep11_hdr = {
  457. .type = 0x06,
  458. .rqid = {0x00, 0x01},
  459. .function_code = {0x00, 0x00},
  460. .agent_id[0] = 0x58, /* {'X'} */
  461. .agent_id[1] = 0x43, /* {'C'} */
  462. .offset1 = 0x00000058,
  463. };
  464. struct {
  465. struct type6_hdr hdr;
  466. struct ep11_cprb cprbx;
  467. unsigned char pld_tag; /* fixed value 0x30 */
  468. unsigned char pld_lenfmt; /* payload length format */
  469. } __packed * msg = ap_msg->msg;
  470. struct pld_hdr {
  471. unsigned char func_tag; /* fixed value 0x4 */
  472. unsigned char func_len; /* fixed value 0x4 */
  473. unsigned int func_val; /* function ID */
  474. unsigned char dom_tag; /* fixed value 0x4 */
  475. unsigned char dom_len; /* fixed value 0x4 */
  476. unsigned int dom_val; /* domain id */
  477. } __packed * payload_hdr = NULL;
  478. if (CEIL4(xcRB->req_len) < xcRB->req_len)
  479. return -EINVAL; /* overflow after alignment*/
  480. /* length checks */
  481. ap_msg->len = sizeof(struct type6_hdr) + xcRB->req_len;
  482. if (CEIL4(xcRB->req_len) > MSGTYPE06_MAX_MSG_SIZE -
  483. (sizeof(struct type6_hdr)))
  484. return -EINVAL;
  485. if (CEIL4(xcRB->resp_len) < xcRB->resp_len)
  486. return -EINVAL; /* overflow after alignment*/
  487. if (CEIL4(xcRB->resp_len) > MSGTYPE06_MAX_MSG_SIZE -
  488. (sizeof(struct type86_fmt2_msg)))
  489. return -EINVAL;
  490. /* prepare type6 header */
  491. msg->hdr = static_type6_ep11_hdr;
  492. msg->hdr.ToCardLen1 = xcRB->req_len;
  493. msg->hdr.FromCardLen1 = xcRB->resp_len;
  494. /* Import CPRB data from the ioctl input parameter */
  495. if (z_copy_from_user(userspace, &(msg->cprbx.cprb_len),
  496. (char __force __user *)xcRB->req, xcRB->req_len)) {
  497. return -EFAULT;
  498. }
  499. if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
  500. switch (msg->pld_lenfmt & 0x03) {
  501. case 1:
  502. lfmt = 2;
  503. break;
  504. case 2:
  505. lfmt = 3;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. } else {
  511. lfmt = 1; /* length format #1 */
  512. }
  513. payload_hdr = (struct pld_hdr *)((&(msg->pld_lenfmt))+lfmt);
  514. *fcode = payload_hdr->func_val & 0xFFFF;
  515. /* enable special processing based on the cprbs flags special bit */
  516. if (msg->cprbx.flags & 0x20)
  517. ap_msg->flags |= AP_MSG_FLAG_SPECIAL;
  518. #ifdef CONFIG_ZCRYPT_DEBUG
  519. if (ap_msg->fi.flags & AP_FI_FLAG_TOGGLE_SPECIAL)
  520. ap_msg->flags ^= AP_MSG_FLAG_SPECIAL;
  521. #endif
  522. return 0;
  523. }
  524. /**
  525. * Copy results from a type 86 ICA reply message back to user space.
  526. *
  527. * @zq: crypto device pointer
  528. * @reply: reply AP message.
  529. * @data: pointer to user output data
  530. * @length: size of user output data
  531. *
  532. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  533. */
  534. struct type86x_reply {
  535. struct type86_hdr hdr;
  536. struct type86_fmt2_ext fmt2;
  537. struct CPRBX cprbx;
  538. unsigned char pad[4]; /* 4 byte function code/rules block ? */
  539. unsigned short length;
  540. char text[];
  541. } __packed;
  542. struct type86_ep11_reply {
  543. struct type86_hdr hdr;
  544. struct type86_fmt2_ext fmt2;
  545. struct ep11_cprb cprbx;
  546. } __packed;
  547. static int convert_type86_ica(struct zcrypt_queue *zq,
  548. struct ap_message *reply,
  549. char __user *outputdata,
  550. unsigned int outputdatalength)
  551. {
  552. static unsigned char static_pad[] = {
  553. 0x00, 0x02,
  554. 0x1B, 0x7B, 0x5D, 0xB5, 0x75, 0x01, 0x3D, 0xFD,
  555. 0x8D, 0xD1, 0xC7, 0x03, 0x2D, 0x09, 0x23, 0x57,
  556. 0x89, 0x49, 0xB9, 0x3F, 0xBB, 0x99, 0x41, 0x5B,
  557. 0x75, 0x21, 0x7B, 0x9D, 0x3B, 0x6B, 0x51, 0x39,
  558. 0xBB, 0x0D, 0x35, 0xB9, 0x89, 0x0F, 0x93, 0xA5,
  559. 0x0B, 0x47, 0xF1, 0xD3, 0xBB, 0xCB, 0xF1, 0x9D,
  560. 0x23, 0x73, 0x71, 0xFF, 0xF3, 0xF5, 0x45, 0xFB,
  561. 0x61, 0x29, 0x23, 0xFD, 0xF1, 0x29, 0x3F, 0x7F,
  562. 0x17, 0xB7, 0x1B, 0xA9, 0x19, 0xBD, 0x57, 0xA9,
  563. 0xD7, 0x95, 0xA3, 0xCB, 0xED, 0x1D, 0xDB, 0x45,
  564. 0x7D, 0x11, 0xD1, 0x51, 0x1B, 0xED, 0x71, 0xE9,
  565. 0xB1, 0xD1, 0xAB, 0xAB, 0x21, 0x2B, 0x1B, 0x9F,
  566. 0x3B, 0x9F, 0xF7, 0xF7, 0xBD, 0x63, 0xEB, 0xAD,
  567. 0xDF, 0xB3, 0x6F, 0x5B, 0xDB, 0x8D, 0xA9, 0x5D,
  568. 0xE3, 0x7D, 0x77, 0x49, 0x47, 0xF5, 0xA7, 0xFD,
  569. 0xAB, 0x2F, 0x27, 0x35, 0x77, 0xD3, 0x49, 0xC9,
  570. 0x09, 0xEB, 0xB1, 0xF9, 0xBF, 0x4B, 0xCB, 0x2B,
  571. 0xEB, 0xEB, 0x05, 0xFF, 0x7D, 0xC7, 0x91, 0x8B,
  572. 0x09, 0x83, 0xB9, 0xB9, 0x69, 0x33, 0x39, 0x6B,
  573. 0x79, 0x75, 0x19, 0xBF, 0xBB, 0x07, 0x1D, 0xBD,
  574. 0x29, 0xBF, 0x39, 0x95, 0x93, 0x1D, 0x35, 0xC7,
  575. 0xC9, 0x4D, 0xE5, 0x97, 0x0B, 0x43, 0x9B, 0xF1,
  576. 0x16, 0x93, 0x03, 0x1F, 0xA5, 0xFB, 0xDB, 0xF3,
  577. 0x27, 0x4F, 0x27, 0x61, 0x05, 0x1F, 0xB9, 0x23,
  578. 0x2F, 0xC3, 0x81, 0xA9, 0x23, 0x71, 0x55, 0x55,
  579. 0xEB, 0xED, 0x41, 0xE5, 0xF3, 0x11, 0xF1, 0x43,
  580. 0x69, 0x03, 0xBD, 0x0B, 0x37, 0x0F, 0x51, 0x8F,
  581. 0x0B, 0xB5, 0x89, 0x5B, 0x67, 0xA9, 0xD9, 0x4F,
  582. 0x01, 0xF9, 0x21, 0x77, 0x37, 0x73, 0x79, 0xC5,
  583. 0x7F, 0x51, 0xC1, 0xCF, 0x97, 0xA1, 0x75, 0xAD,
  584. 0x35, 0x9D, 0xD3, 0xD3, 0xA7, 0x9D, 0x5D, 0x41,
  585. 0x6F, 0x65, 0x1B, 0xCF, 0xA9, 0x87, 0x91, 0x09
  586. };
  587. struct type86x_reply *msg = reply->msg;
  588. unsigned short service_rc, service_rs;
  589. unsigned int reply_len, pad_len;
  590. char *data;
  591. service_rc = msg->cprbx.ccp_rtcode;
  592. if (unlikely(service_rc != 0)) {
  593. service_rs = msg->cprbx.ccp_rscode;
  594. if ((service_rc == 8 && service_rs == 66) ||
  595. (service_rc == 8 && service_rs == 65) ||
  596. (service_rc == 8 && service_rs == 72) ||
  597. (service_rc == 8 && service_rs == 770) ||
  598. (service_rc == 12 && service_rs == 769)) {
  599. ZCRYPT_DBF_WARN("dev=%02x.%04x rc/rs=%d/%d => rc=EINVAL\n",
  600. AP_QID_CARD(zq->queue->qid),
  601. AP_QID_QUEUE(zq->queue->qid),
  602. (int) service_rc, (int) service_rs);
  603. return -EINVAL;
  604. }
  605. zq->online = 0;
  606. pr_err("Crypto dev=%02x.%04x rc/rs=%d/%d online=0 rc=EAGAIN\n",
  607. AP_QID_CARD(zq->queue->qid),
  608. AP_QID_QUEUE(zq->queue->qid),
  609. (int) service_rc, (int) service_rs);
  610. ZCRYPT_DBF_ERR("dev=%02x.%04x rc/rs=%d/%d => online=0 rc=EAGAIN\n",
  611. AP_QID_CARD(zq->queue->qid),
  612. AP_QID_QUEUE(zq->queue->qid),
  613. (int) service_rc, (int) service_rs);
  614. return -EAGAIN;
  615. }
  616. data = msg->text;
  617. reply_len = msg->length - 2;
  618. if (reply_len > outputdatalength)
  619. return -EINVAL;
  620. /*
  621. * For all encipher requests, the length of the ciphertext (reply_len)
  622. * will always equal the modulus length. For MEX decipher requests
  623. * the output needs to get padded. Minimum pad size is 10.
  624. *
  625. * Currently, the cases where padding will be added is for:
  626. * - PCIXCC_MCL2 using a CRT form token (since PKD didn't support
  627. * ZERO-PAD and CRT is only supported for PKD requests)
  628. * - PCICC, always
  629. */
  630. pad_len = outputdatalength - reply_len;
  631. if (pad_len > 0) {
  632. if (pad_len < 10)
  633. return -EINVAL;
  634. /* 'restore' padding left in the CEXXC card. */
  635. if (copy_to_user(outputdata, static_pad, pad_len - 1))
  636. return -EFAULT;
  637. if (put_user(0, outputdata + pad_len - 1))
  638. return -EFAULT;
  639. }
  640. /* Copy the crypto response to user space. */
  641. if (copy_to_user(outputdata + pad_len, data, reply_len))
  642. return -EFAULT;
  643. return 0;
  644. }
  645. /**
  646. * Copy results from a type 86 XCRB reply message back to user space.
  647. *
  648. * @zq: crypto device pointer
  649. * @reply: reply AP message.
  650. * @xcRB: pointer to XCRB
  651. *
  652. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  653. */
  654. static int convert_type86_xcrb(bool userspace, struct zcrypt_queue *zq,
  655. struct ap_message *reply,
  656. struct ica_xcRB *xcRB)
  657. {
  658. struct type86_fmt2_msg *msg = reply->msg;
  659. char *data = reply->msg;
  660. /* Copy CPRB to user */
  661. if (z_copy_to_user(userspace, xcRB->reply_control_blk_addr,
  662. data + msg->fmt2.offset1, msg->fmt2.count1))
  663. return -EFAULT;
  664. xcRB->reply_control_blk_length = msg->fmt2.count1;
  665. /* Copy data buffer to user */
  666. if (msg->fmt2.count2)
  667. if (z_copy_to_user(userspace, xcRB->reply_data_addr,
  668. data + msg->fmt2.offset2, msg->fmt2.count2))
  669. return -EFAULT;
  670. xcRB->reply_data_length = msg->fmt2.count2;
  671. return 0;
  672. }
  673. /**
  674. * Copy results from a type 86 EP11 XCRB reply message back to user space.
  675. *
  676. * @zq: crypto device pointer
  677. * @reply: reply AP message.
  678. * @xcRB: pointer to EP11 user request block
  679. *
  680. * Returns 0 on success or -EINVAL, -EFAULT, -EAGAIN in case of an error.
  681. */
  682. static int convert_type86_ep11_xcrb(bool userspace, struct zcrypt_queue *zq,
  683. struct ap_message *reply,
  684. struct ep11_urb *xcRB)
  685. {
  686. struct type86_fmt2_msg *msg = reply->msg;
  687. char *data = reply->msg;
  688. if (xcRB->resp_len < msg->fmt2.count1)
  689. return -EINVAL;
  690. /* Copy response CPRB to user */
  691. if (z_copy_to_user(userspace, (char __force __user *)xcRB->resp,
  692. data + msg->fmt2.offset1, msg->fmt2.count1))
  693. return -EFAULT;
  694. xcRB->resp_len = msg->fmt2.count1;
  695. return 0;
  696. }
  697. static int convert_type86_rng(struct zcrypt_queue *zq,
  698. struct ap_message *reply,
  699. char *buffer)
  700. {
  701. struct {
  702. struct type86_hdr hdr;
  703. struct type86_fmt2_ext fmt2;
  704. struct CPRBX cprbx;
  705. } __packed * msg = reply->msg;
  706. char *data = reply->msg;
  707. if (msg->cprbx.ccp_rtcode != 0 || msg->cprbx.ccp_rscode != 0)
  708. return -EINVAL;
  709. memcpy(buffer, data + msg->fmt2.offset2, msg->fmt2.count2);
  710. return msg->fmt2.count2;
  711. }
  712. static int convert_response_ica(struct zcrypt_queue *zq,
  713. struct ap_message *reply,
  714. char __user *outputdata,
  715. unsigned int outputdatalength)
  716. {
  717. struct type86x_reply *msg = reply->msg;
  718. switch (msg->hdr.type) {
  719. case TYPE82_RSP_CODE:
  720. case TYPE88_RSP_CODE:
  721. return convert_error(zq, reply);
  722. case TYPE86_RSP_CODE:
  723. if (msg->cprbx.ccp_rtcode &&
  724. (msg->cprbx.ccp_rscode == 0x14f) &&
  725. (outputdatalength > 256)) {
  726. if (zq->zcard->max_exp_bit_length <= 17) {
  727. zq->zcard->max_exp_bit_length = 17;
  728. return -EAGAIN;
  729. } else
  730. return -EINVAL;
  731. }
  732. if (msg->hdr.reply_code)
  733. return convert_error(zq, reply);
  734. if (msg->cprbx.cprb_ver_id == 0x02)
  735. return convert_type86_ica(zq, reply,
  736. outputdata, outputdatalength);
  737. fallthrough; /* wrong cprb version is an unknown response */
  738. default:
  739. /* Unknown response type, this should NEVER EVER happen */
  740. zq->online = 0;
  741. pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  742. AP_QID_CARD(zq->queue->qid),
  743. AP_QID_QUEUE(zq->queue->qid),
  744. (int) msg->hdr.type);
  745. ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  746. AP_QID_CARD(zq->queue->qid),
  747. AP_QID_QUEUE(zq->queue->qid),
  748. (int) msg->hdr.type);
  749. return -EAGAIN;
  750. }
  751. }
  752. static int convert_response_xcrb(bool userspace, struct zcrypt_queue *zq,
  753. struct ap_message *reply,
  754. struct ica_xcRB *xcRB)
  755. {
  756. struct type86x_reply *msg = reply->msg;
  757. switch (msg->hdr.type) {
  758. case TYPE82_RSP_CODE:
  759. case TYPE88_RSP_CODE:
  760. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  761. return convert_error(zq, reply);
  762. case TYPE86_RSP_CODE:
  763. if (msg->hdr.reply_code) {
  764. memcpy(&(xcRB->status), msg->fmt2.apfs, sizeof(u32));
  765. return convert_error(zq, reply);
  766. }
  767. if (msg->cprbx.cprb_ver_id == 0x02)
  768. return convert_type86_xcrb(userspace, zq, reply, xcRB);
  769. fallthrough; /* wrong cprb version is an unknown response */
  770. default: /* Unknown response type, this should NEVER EVER happen */
  771. xcRB->status = 0x0008044DL; /* HDD_InvalidParm */
  772. zq->online = 0;
  773. pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  774. AP_QID_CARD(zq->queue->qid),
  775. AP_QID_QUEUE(zq->queue->qid),
  776. (int) msg->hdr.type);
  777. ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  778. AP_QID_CARD(zq->queue->qid),
  779. AP_QID_QUEUE(zq->queue->qid),
  780. (int) msg->hdr.type);
  781. return -EAGAIN;
  782. }
  783. }
  784. static int convert_response_ep11_xcrb(bool userspace, struct zcrypt_queue *zq,
  785. struct ap_message *reply, struct ep11_urb *xcRB)
  786. {
  787. struct type86_ep11_reply *msg = reply->msg;
  788. switch (msg->hdr.type) {
  789. case TYPE82_RSP_CODE:
  790. case TYPE87_RSP_CODE:
  791. return convert_error(zq, reply);
  792. case TYPE86_RSP_CODE:
  793. if (msg->hdr.reply_code)
  794. return convert_error(zq, reply);
  795. if (msg->cprbx.cprb_ver_id == 0x04)
  796. return convert_type86_ep11_xcrb(userspace, zq, reply, xcRB);
  797. fallthrough; /* wrong cprb version is an unknown resp */
  798. default: /* Unknown response type, this should NEVER EVER happen */
  799. zq->online = 0;
  800. pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  801. AP_QID_CARD(zq->queue->qid),
  802. AP_QID_QUEUE(zq->queue->qid),
  803. (int) msg->hdr.type);
  804. ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  805. AP_QID_CARD(zq->queue->qid),
  806. AP_QID_QUEUE(zq->queue->qid),
  807. (int) msg->hdr.type);
  808. return -EAGAIN;
  809. }
  810. }
  811. static int convert_response_rng(struct zcrypt_queue *zq,
  812. struct ap_message *reply,
  813. char *data)
  814. {
  815. struct type86x_reply *msg = reply->msg;
  816. switch (msg->hdr.type) {
  817. case TYPE82_RSP_CODE:
  818. case TYPE88_RSP_CODE:
  819. return -EINVAL;
  820. case TYPE86_RSP_CODE:
  821. if (msg->hdr.reply_code)
  822. return -EINVAL;
  823. if (msg->cprbx.cprb_ver_id == 0x02)
  824. return convert_type86_rng(zq, reply, data);
  825. fallthrough; /* wrong cprb version is an unknown response */
  826. default: /* Unknown response type, this should NEVER EVER happen */
  827. zq->online = 0;
  828. pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  829. AP_QID_CARD(zq->queue->qid),
  830. AP_QID_QUEUE(zq->queue->qid),
  831. (int) msg->hdr.type);
  832. ZCRYPT_DBF_ERR("dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
  833. AP_QID_CARD(zq->queue->qid),
  834. AP_QID_QUEUE(zq->queue->qid),
  835. (int) msg->hdr.type);
  836. return -EAGAIN;
  837. }
  838. }
  839. /**
  840. * This function is called from the AP bus code after a crypto request
  841. * "msg" has finished with the reply message "reply".
  842. * It is called from tasklet context.
  843. * @aq: pointer to the AP queue
  844. * @msg: pointer to the AP message
  845. * @reply: pointer to the AP reply message
  846. */
  847. static void zcrypt_msgtype6_receive(struct ap_queue *aq,
  848. struct ap_message *msg,
  849. struct ap_message *reply)
  850. {
  851. static struct error_hdr error_reply = {
  852. .type = TYPE82_RSP_CODE,
  853. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  854. };
  855. struct response_type *resp_type =
  856. (struct response_type *) msg->private;
  857. struct type86x_reply *t86r;
  858. int len;
  859. /* Copy the reply message to the request message buffer. */
  860. if (!reply)
  861. goto out; /* ap_msg->rc indicates the error */
  862. t86r = reply->msg;
  863. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  864. t86r->cprbx.cprb_ver_id == 0x02) {
  865. switch (resp_type->type) {
  866. case CEXXC_RESPONSE_TYPE_ICA:
  867. len = sizeof(struct type86x_reply) + t86r->length - 2;
  868. len = min_t(int, CEXXC_MAX_ICA_RESPONSE_SIZE, len);
  869. memcpy(msg->msg, reply->msg, len);
  870. break;
  871. case CEXXC_RESPONSE_TYPE_XCRB:
  872. len = t86r->fmt2.offset2 + t86r->fmt2.count2;
  873. len = min_t(int, MSGTYPE06_MAX_MSG_SIZE, len);
  874. memcpy(msg->msg, reply->msg, len);
  875. break;
  876. default:
  877. memcpy(msg->msg, &error_reply, sizeof(error_reply));
  878. }
  879. } else
  880. memcpy(msg->msg, reply->msg, sizeof(error_reply));
  881. out:
  882. complete(&(resp_type->work));
  883. }
  884. /**
  885. * This function is called from the AP bus code after a crypto request
  886. * "msg" has finished with the reply message "reply".
  887. * It is called from tasklet context.
  888. * @aq: pointer to the AP queue
  889. * @msg: pointer to the AP message
  890. * @reply: pointer to the AP reply message
  891. */
  892. static void zcrypt_msgtype6_receive_ep11(struct ap_queue *aq,
  893. struct ap_message *msg,
  894. struct ap_message *reply)
  895. {
  896. static struct error_hdr error_reply = {
  897. .type = TYPE82_RSP_CODE,
  898. .reply_code = REP82_ERROR_MACHINE_FAILURE,
  899. };
  900. struct response_type *resp_type =
  901. (struct response_type *)msg->private;
  902. struct type86_ep11_reply *t86r;
  903. int len;
  904. /* Copy the reply message to the request message buffer. */
  905. if (!reply)
  906. goto out; /* ap_msg->rc indicates the error */
  907. t86r = reply->msg;
  908. if (t86r->hdr.type == TYPE86_RSP_CODE &&
  909. t86r->cprbx.cprb_ver_id == 0x04) {
  910. switch (resp_type->type) {
  911. case CEXXC_RESPONSE_TYPE_EP11:
  912. len = t86r->fmt2.offset1 + t86r->fmt2.count1;
  913. len = min_t(int, MSGTYPE06_MAX_MSG_SIZE, len);
  914. memcpy(msg->msg, reply->msg, len);
  915. break;
  916. default:
  917. memcpy(msg->msg, &error_reply, sizeof(error_reply));
  918. }
  919. } else {
  920. memcpy(msg->msg, reply->msg, sizeof(error_reply));
  921. }
  922. out:
  923. complete(&(resp_type->work));
  924. }
  925. static atomic_t zcrypt_step = ATOMIC_INIT(0);
  926. /**
  927. * The request distributor calls this function if it picked the CEXxC
  928. * device to handle a modexpo request.
  929. * @zq: pointer to zcrypt_queue structure that identifies the
  930. * CEXxC device to the request distributor
  931. * @mex: pointer to the modexpo request buffer
  932. */
  933. static long zcrypt_msgtype6_modexpo(struct zcrypt_queue *zq,
  934. struct ica_rsa_modexpo *mex,
  935. struct ap_message *ap_msg)
  936. {
  937. struct response_type resp_type = {
  938. .type = CEXXC_RESPONSE_TYPE_ICA,
  939. };
  940. int rc;
  941. ap_msg->msg = (void *) get_zeroed_page(GFP_KERNEL);
  942. if (!ap_msg->msg)
  943. return -ENOMEM;
  944. ap_msg->receive = zcrypt_msgtype6_receive;
  945. ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
  946. atomic_inc_return(&zcrypt_step);
  947. ap_msg->private = &resp_type;
  948. rc = ICAMEX_msg_to_type6MEX_msgX(zq, ap_msg, mex);
  949. if (rc)
  950. goto out_free;
  951. init_completion(&resp_type.work);
  952. rc = ap_queue_message(zq->queue, ap_msg);
  953. if (rc)
  954. goto out_free;
  955. rc = wait_for_completion_interruptible(&resp_type.work);
  956. if (rc == 0) {
  957. rc = ap_msg->rc;
  958. if (rc == 0)
  959. rc = convert_response_ica(zq, ap_msg,
  960. mex->outputdata,
  961. mex->outputdatalength);
  962. } else
  963. /* Signal pending. */
  964. ap_cancel_message(zq->queue, ap_msg);
  965. out_free:
  966. free_page((unsigned long) ap_msg->msg);
  967. ap_msg->private = NULL;
  968. ap_msg->msg = NULL;
  969. return rc;
  970. }
  971. /**
  972. * The request distributor calls this function if it picked the CEXxC
  973. * device to handle a modexpo_crt request.
  974. * @zq: pointer to zcrypt_queue structure that identifies the
  975. * CEXxC device to the request distributor
  976. * @crt: pointer to the modexpoc_crt request buffer
  977. */
  978. static long zcrypt_msgtype6_modexpo_crt(struct zcrypt_queue *zq,
  979. struct ica_rsa_modexpo_crt *crt,
  980. struct ap_message *ap_msg)
  981. {
  982. struct response_type resp_type = {
  983. .type = CEXXC_RESPONSE_TYPE_ICA,
  984. };
  985. int rc;
  986. ap_msg->msg = (void *) get_zeroed_page(GFP_KERNEL);
  987. if (!ap_msg->msg)
  988. return -ENOMEM;
  989. ap_msg->receive = zcrypt_msgtype6_receive;
  990. ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
  991. atomic_inc_return(&zcrypt_step);
  992. ap_msg->private = &resp_type;
  993. rc = ICACRT_msg_to_type6CRT_msgX(zq, ap_msg, crt);
  994. if (rc)
  995. goto out_free;
  996. init_completion(&resp_type.work);
  997. rc = ap_queue_message(zq->queue, ap_msg);
  998. if (rc)
  999. goto out_free;
  1000. rc = wait_for_completion_interruptible(&resp_type.work);
  1001. if (rc == 0) {
  1002. rc = ap_msg->rc;
  1003. if (rc == 0)
  1004. rc = convert_response_ica(zq, ap_msg,
  1005. crt->outputdata,
  1006. crt->outputdatalength);
  1007. } else {
  1008. /* Signal pending. */
  1009. ap_cancel_message(zq->queue, ap_msg);
  1010. }
  1011. out_free:
  1012. free_page((unsigned long) ap_msg->msg);
  1013. ap_msg->private = NULL;
  1014. ap_msg->msg = NULL;
  1015. return rc;
  1016. }
  1017. /**
  1018. * Fetch function code from cprb.
  1019. * Extracting the fc requires to copy the cprb from userspace.
  1020. * So this function allocates memory and needs an ap_msg prepared
  1021. * by the caller with ap_init_message(). Also the caller has to
  1022. * make sure ap_release_message() is always called even on failure.
  1023. */
  1024. unsigned int get_cprb_fc(bool userspace, struct ica_xcRB *xcRB,
  1025. struct ap_message *ap_msg,
  1026. unsigned int *func_code, unsigned short **dom)
  1027. {
  1028. struct response_type resp_type = {
  1029. .type = CEXXC_RESPONSE_TYPE_XCRB,
  1030. };
  1031. ap_msg->msg = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
  1032. if (!ap_msg->msg)
  1033. return -ENOMEM;
  1034. ap_msg->receive = zcrypt_msgtype6_receive;
  1035. ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
  1036. atomic_inc_return(&zcrypt_step);
  1037. ap_msg->private = kmemdup(&resp_type, sizeof(resp_type), GFP_KERNEL);
  1038. if (!ap_msg->private)
  1039. return -ENOMEM;
  1040. return XCRB_msg_to_type6CPRB_msgX(userspace, ap_msg, xcRB, func_code, dom);
  1041. }
  1042. /**
  1043. * The request distributor calls this function if it picked the CEXxC
  1044. * device to handle a send_cprb request.
  1045. * @zq: pointer to zcrypt_queue structure that identifies the
  1046. * CEXxC device to the request distributor
  1047. * @xcRB: pointer to the send_cprb request buffer
  1048. */
  1049. static long zcrypt_msgtype6_send_cprb(bool userspace, struct zcrypt_queue *zq,
  1050. struct ica_xcRB *xcRB,
  1051. struct ap_message *ap_msg)
  1052. {
  1053. int rc;
  1054. struct response_type *rtype = (struct response_type *)(ap_msg->private);
  1055. init_completion(&rtype->work);
  1056. rc = ap_queue_message(zq->queue, ap_msg);
  1057. if (rc)
  1058. goto out;
  1059. rc = wait_for_completion_interruptible(&rtype->work);
  1060. if (rc == 0) {
  1061. rc = ap_msg->rc;
  1062. if (rc == 0)
  1063. rc = convert_response_xcrb(userspace, zq, ap_msg, xcRB);
  1064. } else
  1065. /* Signal pending. */
  1066. ap_cancel_message(zq->queue, ap_msg);
  1067. out:
  1068. return rc;
  1069. }
  1070. /**
  1071. * Fetch function code from ep11 cprb.
  1072. * Extracting the fc requires to copy the ep11 cprb from userspace.
  1073. * So this function allocates memory and needs an ap_msg prepared
  1074. * by the caller with ap_init_message(). Also the caller has to
  1075. * make sure ap_release_message() is always called even on failure.
  1076. */
  1077. unsigned int get_ep11cprb_fc(bool userspace, struct ep11_urb *xcrb,
  1078. struct ap_message *ap_msg,
  1079. unsigned int *func_code)
  1080. {
  1081. struct response_type resp_type = {
  1082. .type = CEXXC_RESPONSE_TYPE_EP11,
  1083. };
  1084. ap_msg->msg = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
  1085. if (!ap_msg->msg)
  1086. return -ENOMEM;
  1087. ap_msg->receive = zcrypt_msgtype6_receive_ep11;
  1088. ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
  1089. atomic_inc_return(&zcrypt_step);
  1090. ap_msg->private = kmemdup(&resp_type, sizeof(resp_type), GFP_KERNEL);
  1091. if (!ap_msg->private)
  1092. return -ENOMEM;
  1093. return xcrb_msg_to_type6_ep11cprb_msgx(userspace, ap_msg, xcrb, func_code);
  1094. }
  1095. /**
  1096. * The request distributor calls this function if it picked the CEX4P
  1097. * device to handle a send_ep11_cprb request.
  1098. * @zq: pointer to zcrypt_queue structure that identifies the
  1099. * CEX4P device to the request distributor
  1100. * @xcRB: pointer to the ep11 user request block
  1101. */
  1102. static long zcrypt_msgtype6_send_ep11_cprb(bool userspace, struct zcrypt_queue *zq,
  1103. struct ep11_urb *xcrb,
  1104. struct ap_message *ap_msg)
  1105. {
  1106. int rc;
  1107. unsigned int lfmt;
  1108. struct response_type *rtype = (struct response_type *)(ap_msg->private);
  1109. struct {
  1110. struct type6_hdr hdr;
  1111. struct ep11_cprb cprbx;
  1112. unsigned char pld_tag; /* fixed value 0x30 */
  1113. unsigned char pld_lenfmt; /* payload length format */
  1114. } __packed * msg = ap_msg->msg;
  1115. struct pld_hdr {
  1116. unsigned char func_tag; /* fixed value 0x4 */
  1117. unsigned char func_len; /* fixed value 0x4 */
  1118. unsigned int func_val; /* function ID */
  1119. unsigned char dom_tag; /* fixed value 0x4 */
  1120. unsigned char dom_len; /* fixed value 0x4 */
  1121. unsigned int dom_val; /* domain id */
  1122. } __packed * payload_hdr = NULL;
  1123. /**
  1124. * The target domain field within the cprb body/payload block will be
  1125. * replaced by the usage domain for non-management commands only.
  1126. * Therefore we check the first bit of the 'flags' parameter for
  1127. * management command indication.
  1128. * 0 - non management command
  1129. * 1 - management command
  1130. */
  1131. if (!((msg->cprbx.flags & 0x80) == 0x80)) {
  1132. msg->cprbx.target_id = (unsigned int)
  1133. AP_QID_QUEUE(zq->queue->qid);
  1134. if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
  1135. switch (msg->pld_lenfmt & 0x03) {
  1136. case 1:
  1137. lfmt = 2;
  1138. break;
  1139. case 2:
  1140. lfmt = 3;
  1141. break;
  1142. default:
  1143. return -EINVAL;
  1144. }
  1145. } else {
  1146. lfmt = 1; /* length format #1 */
  1147. }
  1148. payload_hdr = (struct pld_hdr *)((&(msg->pld_lenfmt))+lfmt);
  1149. payload_hdr->dom_val = (unsigned int)
  1150. AP_QID_QUEUE(zq->queue->qid);
  1151. }
  1152. init_completion(&rtype->work);
  1153. rc = ap_queue_message(zq->queue, ap_msg);
  1154. if (rc)
  1155. goto out;
  1156. rc = wait_for_completion_interruptible(&rtype->work);
  1157. if (rc == 0) {
  1158. rc = ap_msg->rc;
  1159. if (rc == 0)
  1160. rc = convert_response_ep11_xcrb(userspace, zq, ap_msg, xcrb);
  1161. } else
  1162. /* Signal pending. */
  1163. ap_cancel_message(zq->queue, ap_msg);
  1164. out:
  1165. return rc;
  1166. }
  1167. unsigned int get_rng_fc(struct ap_message *ap_msg, int *func_code,
  1168. unsigned int *domain)
  1169. {
  1170. struct response_type resp_type = {
  1171. .type = CEXXC_RESPONSE_TYPE_XCRB,
  1172. };
  1173. ap_msg->msg = kmalloc(MSGTYPE06_MAX_MSG_SIZE, GFP_KERNEL);
  1174. if (!ap_msg->msg)
  1175. return -ENOMEM;
  1176. ap_msg->receive = zcrypt_msgtype6_receive;
  1177. ap_msg->psmid = (((unsigned long long) current->pid) << 32) +
  1178. atomic_inc_return(&zcrypt_step);
  1179. ap_msg->private = kmemdup(&resp_type, sizeof(resp_type), GFP_KERNEL);
  1180. if (!ap_msg->private)
  1181. return -ENOMEM;
  1182. rng_type6CPRB_msgX(ap_msg, ZCRYPT_RNG_BUFFER_SIZE, domain);
  1183. *func_code = HWRNG;
  1184. return 0;
  1185. }
  1186. /**
  1187. * The request distributor calls this function if it picked the CEXxC
  1188. * device to generate random data.
  1189. * @zq: pointer to zcrypt_queue structure that identifies the
  1190. * CEXxC device to the request distributor
  1191. * @buffer: pointer to a memory page to return random data
  1192. */
  1193. static long zcrypt_msgtype6_rng(struct zcrypt_queue *zq,
  1194. char *buffer, struct ap_message *ap_msg)
  1195. {
  1196. struct {
  1197. struct type6_hdr hdr;
  1198. struct CPRBX cprbx;
  1199. char function_code[2];
  1200. short int rule_length;
  1201. char rule[8];
  1202. short int verb_length;
  1203. short int key_length;
  1204. } __packed * msg = ap_msg->msg;
  1205. struct response_type *rtype = (struct response_type *)(ap_msg->private);
  1206. int rc;
  1207. msg->cprbx.domain = AP_QID_QUEUE(zq->queue->qid);
  1208. init_completion(&rtype->work);
  1209. rc = ap_queue_message(zq->queue, ap_msg);
  1210. if (rc)
  1211. goto out;
  1212. rc = wait_for_completion_interruptible(&rtype->work);
  1213. if (rc == 0) {
  1214. rc = ap_msg->rc;
  1215. if (rc == 0)
  1216. rc = convert_response_rng(zq, ap_msg, buffer);
  1217. } else
  1218. /* Signal pending. */
  1219. ap_cancel_message(zq->queue, ap_msg);
  1220. out:
  1221. return rc;
  1222. }
  1223. /**
  1224. * The crypto operations for a CEXxC card.
  1225. */
  1226. static struct zcrypt_ops zcrypt_msgtype6_norng_ops = {
  1227. .owner = THIS_MODULE,
  1228. .name = MSGTYPE06_NAME,
  1229. .variant = MSGTYPE06_VARIANT_NORNG,
  1230. .rsa_modexpo = zcrypt_msgtype6_modexpo,
  1231. .rsa_modexpo_crt = zcrypt_msgtype6_modexpo_crt,
  1232. .send_cprb = zcrypt_msgtype6_send_cprb,
  1233. };
  1234. static struct zcrypt_ops zcrypt_msgtype6_ops = {
  1235. .owner = THIS_MODULE,
  1236. .name = MSGTYPE06_NAME,
  1237. .variant = MSGTYPE06_VARIANT_DEFAULT,
  1238. .rsa_modexpo = zcrypt_msgtype6_modexpo,
  1239. .rsa_modexpo_crt = zcrypt_msgtype6_modexpo_crt,
  1240. .send_cprb = zcrypt_msgtype6_send_cprb,
  1241. .rng = zcrypt_msgtype6_rng,
  1242. };
  1243. static struct zcrypt_ops zcrypt_msgtype6_ep11_ops = {
  1244. .owner = THIS_MODULE,
  1245. .name = MSGTYPE06_NAME,
  1246. .variant = MSGTYPE06_VARIANT_EP11,
  1247. .rsa_modexpo = NULL,
  1248. .rsa_modexpo_crt = NULL,
  1249. .send_ep11_cprb = zcrypt_msgtype6_send_ep11_cprb,
  1250. };
  1251. void __init zcrypt_msgtype6_init(void)
  1252. {
  1253. zcrypt_msgtype_register(&zcrypt_msgtype6_norng_ops);
  1254. zcrypt_msgtype_register(&zcrypt_msgtype6_ops);
  1255. zcrypt_msgtype_register(&zcrypt_msgtype6_ep11_ops);
  1256. }
  1257. void __exit zcrypt_msgtype6_exit(void)
  1258. {
  1259. zcrypt_msgtype_unregister(&zcrypt_msgtype6_norng_ops);
  1260. zcrypt_msgtype_unregister(&zcrypt_msgtype6_ops);
  1261. zcrypt_msgtype_unregister(&zcrypt_msgtype6_ep11_ops);
  1262. }