pkey_api.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * pkey device driver
  3. *
  4. * Copyright IBM Corp. 2017
  5. * Author(s): Harald Freudenberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (version 2 only)
  9. * as published by the Free Software Foundation.
  10. *
  11. */
  12. #define KMSG_COMPONENT "pkey"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/fs.h>
  15. #include <linux/init.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/debugfs.h>
  21. #include <asm/zcrypt.h>
  22. #include <asm/cpacf.h>
  23. #include <asm/pkey.h>
  24. #include "zcrypt_api.h"
  25. MODULE_LICENSE("GPL");
  26. MODULE_AUTHOR("IBM Corporation");
  27. MODULE_DESCRIPTION("s390 protected key interface");
  28. /* Size of parameter block used for all cca requests/replies */
  29. #define PARMBSIZE 512
  30. /* Size of vardata block used for some of the cca requests/replies */
  31. #define VARDATASIZE 4096
  32. /*
  33. * debug feature data and functions
  34. */
  35. static debug_info_t *debug_info;
  36. #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
  37. #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
  38. #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
  39. #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
  40. static void __init pkey_debug_init(void)
  41. {
  42. debug_info = debug_register("pkey", 1, 1, 4 * sizeof(long));
  43. debug_register_view(debug_info, &debug_sprintf_view);
  44. debug_set_level(debug_info, 3);
  45. }
  46. static void __exit pkey_debug_exit(void)
  47. {
  48. debug_unregister(debug_info);
  49. }
  50. /* inside view of a secure key token (only type 0x01 version 0x04) */
  51. struct secaeskeytoken {
  52. u8 type; /* 0x01 for internal key token */
  53. u8 res0[3];
  54. u8 version; /* should be 0x04 */
  55. u8 res1[1];
  56. u8 flag; /* key flags */
  57. u8 res2[1];
  58. u64 mkvp; /* master key verification pattern */
  59. u8 key[32]; /* key value (encrypted) */
  60. u8 cv[8]; /* control vector */
  61. u16 bitsize; /* key bit size */
  62. u16 keysize; /* key byte size */
  63. u8 tvv[4]; /* token validation value */
  64. } __packed;
  65. /*
  66. * Simple check if the token is a valid CCA secure AES key
  67. * token. If keybitsize is given, the bitsize of the key is
  68. * also checked. Returns 0 on success or errno value on failure.
  69. */
  70. static int check_secaeskeytoken(u8 *token, int keybitsize)
  71. {
  72. struct secaeskeytoken *t = (struct secaeskeytoken *) token;
  73. if (t->type != 0x01) {
  74. DEBUG_ERR(
  75. "check_secaeskeytoken secure token check failed, type mismatch 0x%02x != 0x01\n",
  76. (int) t->type);
  77. return -EINVAL;
  78. }
  79. if (t->version != 0x04) {
  80. DEBUG_ERR(
  81. "check_secaeskeytoken secure token check failed, version mismatch 0x%02x != 0x04\n",
  82. (int) t->version);
  83. return -EINVAL;
  84. }
  85. if (keybitsize > 0 && t->bitsize != keybitsize) {
  86. DEBUG_ERR(
  87. "check_secaeskeytoken secure token check failed, bitsize mismatch %d != %d\n",
  88. (int) t->bitsize, keybitsize);
  89. return -EINVAL;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Allocate consecutive memory for request CPRB, request param
  95. * block, reply CPRB and reply param block and fill in values
  96. * for the common fields. Returns 0 on success or errno value
  97. * on failure.
  98. */
  99. static int alloc_and_prep_cprbmem(size_t paramblen,
  100. u8 **pcprbmem,
  101. struct CPRBX **preqCPRB,
  102. struct CPRBX **prepCPRB)
  103. {
  104. u8 *cprbmem;
  105. size_t cprbplusparamblen = sizeof(struct CPRBX) + paramblen;
  106. struct CPRBX *preqcblk, *prepcblk;
  107. /*
  108. * allocate consecutive memory for request CPRB, request param
  109. * block, reply CPRB and reply param block
  110. */
  111. cprbmem = kmalloc(2 * cprbplusparamblen, GFP_KERNEL);
  112. if (!cprbmem)
  113. return -ENOMEM;
  114. memset(cprbmem, 0, 2 * cprbplusparamblen);
  115. preqcblk = (struct CPRBX *) cprbmem;
  116. prepcblk = (struct CPRBX *) (cprbmem + cprbplusparamblen);
  117. /* fill request cprb struct */
  118. preqcblk->cprb_len = sizeof(struct CPRBX);
  119. preqcblk->cprb_ver_id = 0x02;
  120. memcpy(preqcblk->func_id, "T2", 2);
  121. preqcblk->rpl_msgbl = cprbplusparamblen;
  122. if (paramblen) {
  123. preqcblk->req_parmb =
  124. ((u8 *) preqcblk) + sizeof(struct CPRBX);
  125. preqcblk->rpl_parmb =
  126. ((u8 *) prepcblk) + sizeof(struct CPRBX);
  127. }
  128. *pcprbmem = cprbmem;
  129. *preqCPRB = preqcblk;
  130. *prepCPRB = prepcblk;
  131. return 0;
  132. }
  133. /*
  134. * Free the cprb memory allocated with the function above.
  135. * If the scrub value is not zero, the memory is filled
  136. * with zeros before freeing (useful if there was some
  137. * clear key material in there).
  138. */
  139. static void free_cprbmem(void *mem, size_t paramblen, int scrub)
  140. {
  141. if (scrub)
  142. memzero_explicit(mem, 2 * (sizeof(struct CPRBX) + paramblen));
  143. kfree(mem);
  144. }
  145. /*
  146. * Helper function to prepare the xcrb struct
  147. */
  148. static inline void prep_xcrb(struct ica_xcRB *pxcrb,
  149. u16 cardnr,
  150. struct CPRBX *preqcblk,
  151. struct CPRBX *prepcblk)
  152. {
  153. memset(pxcrb, 0, sizeof(*pxcrb));
  154. pxcrb->agent_ID = 0x4341; /* 'CA' */
  155. pxcrb->user_defined = (cardnr == 0xFFFF ? AUTOSELECT : cardnr);
  156. pxcrb->request_control_blk_length =
  157. preqcblk->cprb_len + preqcblk->req_parml;
  158. pxcrb->request_control_blk_addr = (void *) preqcblk;
  159. pxcrb->reply_control_blk_length = preqcblk->rpl_msgbl;
  160. pxcrb->reply_control_blk_addr = (void *) prepcblk;
  161. }
  162. /*
  163. * Helper function which calls zcrypt_send_cprb with
  164. * memory management segment adjusted to kernel space
  165. * so that the copy_from_user called within this
  166. * function do in fact copy from kernel space.
  167. */
  168. static inline int _zcrypt_send_cprb(struct ica_xcRB *xcrb)
  169. {
  170. int rc;
  171. mm_segment_t old_fs = get_fs();
  172. set_fs(KERNEL_DS);
  173. rc = zcrypt_send_cprb(xcrb);
  174. set_fs(old_fs);
  175. return rc;
  176. }
  177. /*
  178. * Generate (random) AES secure key.
  179. */
  180. int pkey_genseckey(u16 cardnr, u16 domain,
  181. u32 keytype, struct pkey_seckey *seckey)
  182. {
  183. int i, rc, keysize;
  184. int seckeysize;
  185. u8 *mem;
  186. struct CPRBX *preqcblk, *prepcblk;
  187. struct ica_xcRB xcrb;
  188. struct kgreqparm {
  189. u8 subfunc_code[2];
  190. u16 rule_array_len;
  191. struct lv1 {
  192. u16 len;
  193. char key_form[8];
  194. char key_length[8];
  195. char key_type1[8];
  196. char key_type2[8];
  197. } lv1;
  198. struct lv2 {
  199. u16 len;
  200. struct keyid {
  201. u16 len;
  202. u16 attr;
  203. u8 data[SECKEYBLOBSIZE];
  204. } keyid[6];
  205. } lv2;
  206. } *preqparm;
  207. struct kgrepparm {
  208. u8 subfunc_code[2];
  209. u16 rule_array_len;
  210. struct lv3 {
  211. u16 len;
  212. u16 keyblocklen;
  213. struct {
  214. u16 toklen;
  215. u16 tokattr;
  216. u8 tok[0];
  217. /* ... some more data ... */
  218. } keyblock;
  219. } lv3;
  220. } *prepparm;
  221. /* get already prepared memory for 2 cprbs with param block each */
  222. rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
  223. if (rc)
  224. return rc;
  225. /* fill request cprb struct */
  226. preqcblk->domain = domain;
  227. /* fill request cprb param block with KG request */
  228. preqparm = (struct kgreqparm *) preqcblk->req_parmb;
  229. memcpy(preqparm->subfunc_code, "KG", 2);
  230. preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
  231. preqparm->lv1.len = sizeof(struct lv1);
  232. memcpy(preqparm->lv1.key_form, "OP ", 8);
  233. switch (keytype) {
  234. case PKEY_KEYTYPE_AES_128:
  235. keysize = 16;
  236. memcpy(preqparm->lv1.key_length, "KEYLN16 ", 8);
  237. break;
  238. case PKEY_KEYTYPE_AES_192:
  239. keysize = 24;
  240. memcpy(preqparm->lv1.key_length, "KEYLN24 ", 8);
  241. break;
  242. case PKEY_KEYTYPE_AES_256:
  243. keysize = 32;
  244. memcpy(preqparm->lv1.key_length, "KEYLN32 ", 8);
  245. break;
  246. default:
  247. DEBUG_ERR(
  248. "pkey_genseckey unknown/unsupported keytype %d\n",
  249. keytype);
  250. rc = -EINVAL;
  251. goto out;
  252. }
  253. memcpy(preqparm->lv1.key_type1, "AESDATA ", 8);
  254. preqparm->lv2.len = sizeof(struct lv2);
  255. for (i = 0; i < 6; i++) {
  256. preqparm->lv2.keyid[i].len = sizeof(struct keyid);
  257. preqparm->lv2.keyid[i].attr = (i == 2 ? 0x30 : 0x10);
  258. }
  259. preqcblk->req_parml = sizeof(struct kgreqparm);
  260. /* fill xcrb struct */
  261. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  262. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  263. rc = _zcrypt_send_cprb(&xcrb);
  264. if (rc) {
  265. DEBUG_ERR(
  266. "pkey_genseckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  267. (int) cardnr, (int) domain, rc);
  268. goto out;
  269. }
  270. /* check response returncode and reasoncode */
  271. if (prepcblk->ccp_rtcode != 0) {
  272. DEBUG_ERR(
  273. "pkey_genseckey secure key generate failure, card response %d/%d\n",
  274. (int) prepcblk->ccp_rtcode,
  275. (int) prepcblk->ccp_rscode);
  276. rc = -EIO;
  277. goto out;
  278. }
  279. /* process response cprb param block */
  280. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  281. prepparm = (struct kgrepparm *) prepcblk->rpl_parmb;
  282. /* check length of the returned secure key token */
  283. seckeysize = prepparm->lv3.keyblock.toklen
  284. - sizeof(prepparm->lv3.keyblock.toklen)
  285. - sizeof(prepparm->lv3.keyblock.tokattr);
  286. if (seckeysize != SECKEYBLOBSIZE) {
  287. DEBUG_ERR(
  288. "pkey_genseckey secure token size mismatch %d != %d bytes\n",
  289. seckeysize, SECKEYBLOBSIZE);
  290. rc = -EIO;
  291. goto out;
  292. }
  293. /* check secure key token */
  294. rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
  295. if (rc) {
  296. rc = -EIO;
  297. goto out;
  298. }
  299. /* copy the generated secure key token */
  300. memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
  301. out:
  302. free_cprbmem(mem, PARMBSIZE, 0);
  303. return rc;
  304. }
  305. EXPORT_SYMBOL(pkey_genseckey);
  306. /*
  307. * Generate an AES secure key with given key value.
  308. */
  309. int pkey_clr2seckey(u16 cardnr, u16 domain, u32 keytype,
  310. const struct pkey_clrkey *clrkey,
  311. struct pkey_seckey *seckey)
  312. {
  313. int rc, keysize, seckeysize;
  314. u8 *mem;
  315. struct CPRBX *preqcblk, *prepcblk;
  316. struct ica_xcRB xcrb;
  317. struct cmreqparm {
  318. u8 subfunc_code[2];
  319. u16 rule_array_len;
  320. char rule_array[8];
  321. struct lv1 {
  322. u16 len;
  323. u8 clrkey[0];
  324. } lv1;
  325. struct lv2 {
  326. u16 len;
  327. struct keyid {
  328. u16 len;
  329. u16 attr;
  330. u8 data[SECKEYBLOBSIZE];
  331. } keyid;
  332. } lv2;
  333. } *preqparm;
  334. struct lv2 *plv2;
  335. struct cmrepparm {
  336. u8 subfunc_code[2];
  337. u16 rule_array_len;
  338. struct lv3 {
  339. u16 len;
  340. u16 keyblocklen;
  341. struct {
  342. u16 toklen;
  343. u16 tokattr;
  344. u8 tok[0];
  345. /* ... some more data ... */
  346. } keyblock;
  347. } lv3;
  348. } *prepparm;
  349. /* get already prepared memory for 2 cprbs with param block each */
  350. rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
  351. if (rc)
  352. return rc;
  353. /* fill request cprb struct */
  354. preqcblk->domain = domain;
  355. /* fill request cprb param block with CM request */
  356. preqparm = (struct cmreqparm *) preqcblk->req_parmb;
  357. memcpy(preqparm->subfunc_code, "CM", 2);
  358. memcpy(preqparm->rule_array, "AES ", 8);
  359. preqparm->rule_array_len =
  360. sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
  361. switch (keytype) {
  362. case PKEY_KEYTYPE_AES_128:
  363. keysize = 16;
  364. break;
  365. case PKEY_KEYTYPE_AES_192:
  366. keysize = 24;
  367. break;
  368. case PKEY_KEYTYPE_AES_256:
  369. keysize = 32;
  370. break;
  371. default:
  372. DEBUG_ERR(
  373. "pkey_clr2seckey unknown/unsupported keytype %d\n",
  374. keytype);
  375. rc = -EINVAL;
  376. goto out;
  377. }
  378. preqparm->lv1.len = sizeof(struct lv1) + keysize;
  379. memcpy(preqparm->lv1.clrkey, clrkey->clrkey, keysize);
  380. plv2 = (struct lv2 *) (((u8 *) &preqparm->lv2) + keysize);
  381. plv2->len = sizeof(struct lv2);
  382. plv2->keyid.len = sizeof(struct keyid);
  383. plv2->keyid.attr = 0x30;
  384. preqcblk->req_parml = sizeof(struct cmreqparm) + keysize;
  385. /* fill xcrb struct */
  386. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  387. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  388. rc = _zcrypt_send_cprb(&xcrb);
  389. if (rc) {
  390. DEBUG_ERR(
  391. "pkey_clr2seckey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  392. (int) cardnr, (int) domain, rc);
  393. goto out;
  394. }
  395. /* check response returncode and reasoncode */
  396. if (prepcblk->ccp_rtcode != 0) {
  397. DEBUG_ERR(
  398. "pkey_clr2seckey clear key import failure, card response %d/%d\n",
  399. (int) prepcblk->ccp_rtcode,
  400. (int) prepcblk->ccp_rscode);
  401. rc = -EIO;
  402. goto out;
  403. }
  404. /* process response cprb param block */
  405. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  406. prepparm = (struct cmrepparm *) prepcblk->rpl_parmb;
  407. /* check length of the returned secure key token */
  408. seckeysize = prepparm->lv3.keyblock.toklen
  409. - sizeof(prepparm->lv3.keyblock.toklen)
  410. - sizeof(prepparm->lv3.keyblock.tokattr);
  411. if (seckeysize != SECKEYBLOBSIZE) {
  412. DEBUG_ERR(
  413. "pkey_clr2seckey secure token size mismatch %d != %d bytes\n",
  414. seckeysize, SECKEYBLOBSIZE);
  415. rc = -EIO;
  416. goto out;
  417. }
  418. /* check secure key token */
  419. rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
  420. if (rc) {
  421. rc = -EIO;
  422. goto out;
  423. }
  424. /* copy the generated secure key token */
  425. memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
  426. out:
  427. free_cprbmem(mem, PARMBSIZE, 1);
  428. return rc;
  429. }
  430. EXPORT_SYMBOL(pkey_clr2seckey);
  431. /*
  432. * Derive a proteced key from the secure key blob.
  433. */
  434. int pkey_sec2protkey(u16 cardnr, u16 domain,
  435. const struct pkey_seckey *seckey,
  436. struct pkey_protkey *protkey)
  437. {
  438. int rc;
  439. u8 *mem;
  440. struct CPRBX *preqcblk, *prepcblk;
  441. struct ica_xcRB xcrb;
  442. struct uskreqparm {
  443. u8 subfunc_code[2];
  444. u16 rule_array_len;
  445. struct lv1 {
  446. u16 len;
  447. u16 attr_len;
  448. u16 attr_flags;
  449. } lv1;
  450. struct lv2 {
  451. u16 len;
  452. u16 attr_len;
  453. u16 attr_flags;
  454. u8 token[0]; /* cca secure key token */
  455. } lv2 __packed;
  456. } *preqparm;
  457. struct uskrepparm {
  458. u8 subfunc_code[2];
  459. u16 rule_array_len;
  460. struct lv3 {
  461. u16 len;
  462. u16 attr_len;
  463. u16 attr_flags;
  464. struct cpacfkeyblock {
  465. u8 version; /* version of this struct */
  466. u8 flags[2];
  467. u8 algo;
  468. u8 form;
  469. u8 pad1[3];
  470. u16 keylen;
  471. u8 key[64]; /* the key (keylen bytes) */
  472. u16 keyattrlen;
  473. u8 keyattr[32];
  474. u8 pad2[1];
  475. u8 vptype;
  476. u8 vp[32]; /* verification pattern */
  477. } keyblock;
  478. } lv3 __packed;
  479. } *prepparm;
  480. /* get already prepared memory for 2 cprbs with param block each */
  481. rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
  482. if (rc)
  483. return rc;
  484. /* fill request cprb struct */
  485. preqcblk->domain = domain;
  486. /* fill request cprb param block with USK request */
  487. preqparm = (struct uskreqparm *) preqcblk->req_parmb;
  488. memcpy(preqparm->subfunc_code, "US", 2);
  489. preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
  490. preqparm->lv1.len = sizeof(struct lv1);
  491. preqparm->lv1.attr_len = sizeof(struct lv1) - sizeof(preqparm->lv1.len);
  492. preqparm->lv1.attr_flags = 0x0001;
  493. preqparm->lv2.len = sizeof(struct lv2) + SECKEYBLOBSIZE;
  494. preqparm->lv2.attr_len = sizeof(struct lv2)
  495. - sizeof(preqparm->lv2.len) + SECKEYBLOBSIZE;
  496. preqparm->lv2.attr_flags = 0x0000;
  497. memcpy(preqparm->lv2.token, seckey->seckey, SECKEYBLOBSIZE);
  498. preqcblk->req_parml = sizeof(struct uskreqparm) + SECKEYBLOBSIZE;
  499. /* fill xcrb struct */
  500. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  501. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  502. rc = _zcrypt_send_cprb(&xcrb);
  503. if (rc) {
  504. DEBUG_ERR(
  505. "pkey_sec2protkey zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  506. (int) cardnr, (int) domain, rc);
  507. goto out;
  508. }
  509. /* check response returncode and reasoncode */
  510. if (prepcblk->ccp_rtcode != 0) {
  511. DEBUG_ERR(
  512. "pkey_sec2protkey unwrap secure key failure, card response %d/%d\n",
  513. (int) prepcblk->ccp_rtcode,
  514. (int) prepcblk->ccp_rscode);
  515. rc = -EIO;
  516. goto out;
  517. }
  518. /* process response cprb param block */
  519. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  520. prepparm = (struct uskrepparm *) prepcblk->rpl_parmb;
  521. /* check the returned keyblock */
  522. if (prepparm->lv3.keyblock.version != 0x01) {
  523. DEBUG_ERR(
  524. "pkey_sec2protkey reply param keyblock version mismatch 0x%02x != 0x01\n",
  525. (int) prepparm->lv3.keyblock.version);
  526. rc = -EIO;
  527. goto out;
  528. }
  529. /* copy the tanslated protected key */
  530. switch (prepparm->lv3.keyblock.keylen) {
  531. case 16+32:
  532. protkey->type = PKEY_KEYTYPE_AES_128;
  533. break;
  534. case 24+32:
  535. protkey->type = PKEY_KEYTYPE_AES_192;
  536. break;
  537. case 32+32:
  538. protkey->type = PKEY_KEYTYPE_AES_256;
  539. break;
  540. default:
  541. DEBUG_ERR("pkey_sec2protkey unknown/unsupported keytype %d\n",
  542. prepparm->lv3.keyblock.keylen);
  543. rc = -EIO;
  544. goto out;
  545. }
  546. protkey->len = prepparm->lv3.keyblock.keylen;
  547. memcpy(protkey->protkey, prepparm->lv3.keyblock.key, protkey->len);
  548. out:
  549. free_cprbmem(mem, PARMBSIZE, 0);
  550. return rc;
  551. }
  552. EXPORT_SYMBOL(pkey_sec2protkey);
  553. /*
  554. * Create a protected key from a clear key value.
  555. */
  556. int pkey_clr2protkey(u32 keytype,
  557. const struct pkey_clrkey *clrkey,
  558. struct pkey_protkey *protkey)
  559. {
  560. long fc;
  561. int keysize;
  562. u8 paramblock[64];
  563. switch (keytype) {
  564. case PKEY_KEYTYPE_AES_128:
  565. keysize = 16;
  566. fc = CPACF_PCKMO_ENC_AES_128_KEY;
  567. break;
  568. case PKEY_KEYTYPE_AES_192:
  569. keysize = 24;
  570. fc = CPACF_PCKMO_ENC_AES_192_KEY;
  571. break;
  572. case PKEY_KEYTYPE_AES_256:
  573. keysize = 32;
  574. fc = CPACF_PCKMO_ENC_AES_256_KEY;
  575. break;
  576. default:
  577. DEBUG_ERR("pkey_clr2protkey unknown/unsupported keytype %d\n",
  578. keytype);
  579. return -EINVAL;
  580. }
  581. /* prepare param block */
  582. memset(paramblock, 0, sizeof(paramblock));
  583. memcpy(paramblock, clrkey->clrkey, keysize);
  584. /* call the pckmo instruction */
  585. cpacf_pckmo(fc, paramblock);
  586. /* copy created protected key */
  587. protkey->type = keytype;
  588. protkey->len = keysize + 32;
  589. memcpy(protkey->protkey, paramblock, keysize + 32);
  590. return 0;
  591. }
  592. EXPORT_SYMBOL(pkey_clr2protkey);
  593. /*
  594. * query cryptographic facility from adapter
  595. */
  596. static int query_crypto_facility(u16 cardnr, u16 domain,
  597. const char *keyword,
  598. u8 *rarray, size_t *rarraylen,
  599. u8 *varray, size_t *varraylen)
  600. {
  601. int rc;
  602. u16 len;
  603. u8 *mem, *ptr;
  604. struct CPRBX *preqcblk, *prepcblk;
  605. struct ica_xcRB xcrb;
  606. struct fqreqparm {
  607. u8 subfunc_code[2];
  608. u16 rule_array_len;
  609. char rule_array[8];
  610. struct lv1 {
  611. u16 len;
  612. u8 data[VARDATASIZE];
  613. } lv1;
  614. u16 dummylen;
  615. } *preqparm;
  616. size_t parmbsize = sizeof(struct fqreqparm);
  617. struct fqrepparm {
  618. u8 subfunc_code[2];
  619. u8 lvdata[0];
  620. } *prepparm;
  621. /* get already prepared memory for 2 cprbs with param block each */
  622. rc = alloc_and_prep_cprbmem(parmbsize, &mem, &preqcblk, &prepcblk);
  623. if (rc)
  624. return rc;
  625. /* fill request cprb struct */
  626. preqcblk->domain = domain;
  627. /* fill request cprb param block with FQ request */
  628. preqparm = (struct fqreqparm *) preqcblk->req_parmb;
  629. memcpy(preqparm->subfunc_code, "FQ", 2);
  630. strncpy(preqparm->rule_array, keyword, sizeof(preqparm->rule_array));
  631. preqparm->rule_array_len =
  632. sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
  633. preqparm->lv1.len = sizeof(preqparm->lv1);
  634. preqparm->dummylen = sizeof(preqparm->dummylen);
  635. preqcblk->req_parml = parmbsize;
  636. /* fill xcrb struct */
  637. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  638. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  639. rc = _zcrypt_send_cprb(&xcrb);
  640. if (rc) {
  641. DEBUG_ERR(
  642. "query_crypto_facility zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  643. (int) cardnr, (int) domain, rc);
  644. goto out;
  645. }
  646. /* check response returncode and reasoncode */
  647. if (prepcblk->ccp_rtcode != 0) {
  648. DEBUG_ERR(
  649. "query_crypto_facility unwrap secure key failure, card response %d/%d\n",
  650. (int) prepcblk->ccp_rtcode,
  651. (int) prepcblk->ccp_rscode);
  652. rc = -EIO;
  653. goto out;
  654. }
  655. /* process response cprb param block */
  656. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  657. prepparm = (struct fqrepparm *) prepcblk->rpl_parmb;
  658. ptr = prepparm->lvdata;
  659. /* check and possibly copy reply rule array */
  660. len = *((u16 *) ptr);
  661. if (len > sizeof(u16)) {
  662. ptr += sizeof(u16);
  663. len -= sizeof(u16);
  664. if (rarray && rarraylen && *rarraylen > 0) {
  665. *rarraylen = (len > *rarraylen ? *rarraylen : len);
  666. memcpy(rarray, ptr, *rarraylen);
  667. }
  668. ptr += len;
  669. }
  670. /* check and possible copy reply var array */
  671. len = *((u16 *) ptr);
  672. if (len > sizeof(u16)) {
  673. ptr += sizeof(u16);
  674. len -= sizeof(u16);
  675. if (varray && varraylen && *varraylen > 0) {
  676. *varraylen = (len > *varraylen ? *varraylen : len);
  677. memcpy(varray, ptr, *varraylen);
  678. }
  679. ptr += len;
  680. }
  681. out:
  682. free_cprbmem(mem, parmbsize, 0);
  683. return rc;
  684. }
  685. /*
  686. * Fetch just the mkvp value via query_crypto_facility from adapter.
  687. */
  688. static int fetch_mkvp(u16 cardnr, u16 domain, u64 *mkvp)
  689. {
  690. int rc, found = 0;
  691. size_t rlen, vlen;
  692. u8 *rarray, *varray, *pg;
  693. pg = (u8 *) __get_free_page(GFP_KERNEL);
  694. if (!pg)
  695. return -ENOMEM;
  696. rarray = pg;
  697. varray = pg + PAGE_SIZE/2;
  698. rlen = vlen = PAGE_SIZE/2;
  699. rc = query_crypto_facility(cardnr, domain, "STATICSA",
  700. rarray, &rlen, varray, &vlen);
  701. if (rc == 0 && rlen > 8*8 && vlen > 184+8) {
  702. if (rarray[64] == '2') {
  703. /* current master key state is valid */
  704. *mkvp = *((u64 *)(varray + 184));
  705. found = 1;
  706. }
  707. }
  708. free_page((unsigned long) pg);
  709. return found ? 0 : -ENOENT;
  710. }
  711. /* struct to hold cached mkvp info for each card/domain */
  712. struct mkvp_info {
  713. struct list_head list;
  714. u16 cardnr;
  715. u16 domain;
  716. u64 mkvp;
  717. };
  718. /* a list with mkvp_info entries */
  719. static LIST_HEAD(mkvp_list);
  720. static DEFINE_SPINLOCK(mkvp_list_lock);
  721. static int mkvp_cache_fetch(u16 cardnr, u16 domain, u64 *mkvp)
  722. {
  723. int rc = -ENOENT;
  724. struct mkvp_info *ptr;
  725. spin_lock_bh(&mkvp_list_lock);
  726. list_for_each_entry(ptr, &mkvp_list, list) {
  727. if (ptr->cardnr == cardnr &&
  728. ptr->domain == domain) {
  729. *mkvp = ptr->mkvp;
  730. rc = 0;
  731. break;
  732. }
  733. }
  734. spin_unlock_bh(&mkvp_list_lock);
  735. return rc;
  736. }
  737. static void mkvp_cache_update(u16 cardnr, u16 domain, u64 mkvp)
  738. {
  739. int found = 0;
  740. struct mkvp_info *ptr;
  741. spin_lock_bh(&mkvp_list_lock);
  742. list_for_each_entry(ptr, &mkvp_list, list) {
  743. if (ptr->cardnr == cardnr &&
  744. ptr->domain == domain) {
  745. ptr->mkvp = mkvp;
  746. found = 1;
  747. break;
  748. }
  749. }
  750. if (!found) {
  751. ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
  752. if (!ptr) {
  753. spin_unlock_bh(&mkvp_list_lock);
  754. return;
  755. }
  756. ptr->cardnr = cardnr;
  757. ptr->domain = domain;
  758. ptr->mkvp = mkvp;
  759. list_add(&ptr->list, &mkvp_list);
  760. }
  761. spin_unlock_bh(&mkvp_list_lock);
  762. }
  763. static void mkvp_cache_scrub(u16 cardnr, u16 domain)
  764. {
  765. struct mkvp_info *ptr;
  766. spin_lock_bh(&mkvp_list_lock);
  767. list_for_each_entry(ptr, &mkvp_list, list) {
  768. if (ptr->cardnr == cardnr &&
  769. ptr->domain == domain) {
  770. list_del(&ptr->list);
  771. kfree(ptr);
  772. break;
  773. }
  774. }
  775. spin_unlock_bh(&mkvp_list_lock);
  776. }
  777. static void __exit mkvp_cache_free(void)
  778. {
  779. struct mkvp_info *ptr, *pnext;
  780. spin_lock_bh(&mkvp_list_lock);
  781. list_for_each_entry_safe(ptr, pnext, &mkvp_list, list) {
  782. list_del(&ptr->list);
  783. kfree(ptr);
  784. }
  785. spin_unlock_bh(&mkvp_list_lock);
  786. }
  787. /*
  788. * Search for a matching crypto card based on the Master Key
  789. * Verification Pattern provided inside a secure key.
  790. */
  791. int pkey_findcard(const struct pkey_seckey *seckey,
  792. u16 *pcardnr, u16 *pdomain, int verify)
  793. {
  794. struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
  795. struct zcrypt_device_matrix *device_matrix;
  796. u16 card, dom;
  797. u64 mkvp;
  798. int i, rc;
  799. /* mkvp must not be zero */
  800. if (t->mkvp == 0)
  801. return -EINVAL;
  802. /* fetch status of all crypto cards */
  803. device_matrix = kmalloc(sizeof(struct zcrypt_device_matrix),
  804. GFP_KERNEL);
  805. if (!device_matrix)
  806. return -ENOMEM;
  807. zcrypt_device_status_mask(device_matrix);
  808. /* walk through all crypto cards */
  809. for (i = 0; i < MAX_ZDEV_ENTRIES; i++) {
  810. card = AP_QID_CARD(device_matrix->device[i].qid);
  811. dom = AP_QID_QUEUE(device_matrix->device[i].qid);
  812. if (device_matrix->device[i].online &&
  813. device_matrix->device[i].functions & 0x04) {
  814. /* an enabled CCA Coprocessor card */
  815. /* try cached mkvp */
  816. if (mkvp_cache_fetch(card, dom, &mkvp) == 0 &&
  817. t->mkvp == mkvp) {
  818. if (!verify)
  819. break;
  820. /* verify: fetch mkvp from adapter */
  821. if (fetch_mkvp(card, dom, &mkvp) == 0) {
  822. mkvp_cache_update(card, dom, mkvp);
  823. if (t->mkvp == mkvp)
  824. break;
  825. }
  826. }
  827. } else {
  828. /* Card is offline and/or not a CCA card. */
  829. /* del mkvp entry from cache if it exists */
  830. mkvp_cache_scrub(card, dom);
  831. }
  832. }
  833. if (i >= MAX_ZDEV_ENTRIES) {
  834. /* nothing found, so this time without cache */
  835. for (i = 0; i < MAX_ZDEV_ENTRIES; i++) {
  836. if (!(device_matrix->device[i].online &&
  837. device_matrix->device[i].functions & 0x04))
  838. continue;
  839. card = AP_QID_CARD(device_matrix->device[i].qid);
  840. dom = AP_QID_QUEUE(device_matrix->device[i].qid);
  841. /* fresh fetch mkvp from adapter */
  842. if (fetch_mkvp(card, dom, &mkvp) == 0) {
  843. mkvp_cache_update(card, dom, mkvp);
  844. if (t->mkvp == mkvp)
  845. break;
  846. }
  847. }
  848. }
  849. if (i < MAX_ZDEV_ENTRIES) {
  850. if (pcardnr)
  851. *pcardnr = card;
  852. if (pdomain)
  853. *pdomain = dom;
  854. rc = 0;
  855. } else
  856. rc = -ENODEV;
  857. kfree(device_matrix);
  858. return rc;
  859. }
  860. EXPORT_SYMBOL(pkey_findcard);
  861. /*
  862. * Find card and transform secure key into protected key.
  863. */
  864. int pkey_skey2pkey(const struct pkey_seckey *seckey,
  865. struct pkey_protkey *protkey)
  866. {
  867. u16 cardnr, domain;
  868. int rc, verify;
  869. /*
  870. * The pkey_sec2protkey call may fail when a card has been
  871. * addressed where the master key was changed after last fetch
  872. * of the mkvp into the cache. So first try without verify then
  873. * with verify enabled (thus refreshing the mkvp for each card).
  874. */
  875. for (verify = 0; verify < 2; verify++) {
  876. rc = pkey_findcard(seckey, &cardnr, &domain, verify);
  877. if (rc)
  878. continue;
  879. rc = pkey_sec2protkey(cardnr, domain, seckey, protkey);
  880. if (rc == 0)
  881. break;
  882. }
  883. if (rc)
  884. DEBUG_DBG("pkey_skey2pkey failed rc=%d\n", rc);
  885. return rc;
  886. }
  887. EXPORT_SYMBOL(pkey_skey2pkey);
  888. /*
  889. * File io functions
  890. */
  891. static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
  892. unsigned long arg)
  893. {
  894. int rc;
  895. switch (cmd) {
  896. case PKEY_GENSECK: {
  897. struct pkey_genseck __user *ugs = (void __user *) arg;
  898. struct pkey_genseck kgs;
  899. if (copy_from_user(&kgs, ugs, sizeof(kgs)))
  900. return -EFAULT;
  901. rc = pkey_genseckey(kgs.cardnr, kgs.domain,
  902. kgs.keytype, &kgs.seckey);
  903. DEBUG_DBG("pkey_ioctl pkey_genseckey()=%d\n", rc);
  904. if (rc)
  905. break;
  906. if (copy_to_user(ugs, &kgs, sizeof(kgs)))
  907. return -EFAULT;
  908. break;
  909. }
  910. case PKEY_CLR2SECK: {
  911. struct pkey_clr2seck __user *ucs = (void __user *) arg;
  912. struct pkey_clr2seck kcs;
  913. if (copy_from_user(&kcs, ucs, sizeof(kcs)))
  914. return -EFAULT;
  915. rc = pkey_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
  916. &kcs.clrkey, &kcs.seckey);
  917. DEBUG_DBG("pkey_ioctl pkey_clr2seckey()=%d\n", rc);
  918. if (rc)
  919. break;
  920. if (copy_to_user(ucs, &kcs, sizeof(kcs)))
  921. return -EFAULT;
  922. memzero_explicit(&kcs, sizeof(kcs));
  923. break;
  924. }
  925. case PKEY_SEC2PROTK: {
  926. struct pkey_sec2protk __user *usp = (void __user *) arg;
  927. struct pkey_sec2protk ksp;
  928. if (copy_from_user(&ksp, usp, sizeof(ksp)))
  929. return -EFAULT;
  930. rc = pkey_sec2protkey(ksp.cardnr, ksp.domain,
  931. &ksp.seckey, &ksp.protkey);
  932. DEBUG_DBG("pkey_ioctl pkey_sec2protkey()=%d\n", rc);
  933. if (rc)
  934. break;
  935. if (copy_to_user(usp, &ksp, sizeof(ksp)))
  936. return -EFAULT;
  937. break;
  938. }
  939. case PKEY_CLR2PROTK: {
  940. struct pkey_clr2protk __user *ucp = (void __user *) arg;
  941. struct pkey_clr2protk kcp;
  942. if (copy_from_user(&kcp, ucp, sizeof(kcp)))
  943. return -EFAULT;
  944. rc = pkey_clr2protkey(kcp.keytype,
  945. &kcp.clrkey, &kcp.protkey);
  946. DEBUG_DBG("pkey_ioctl pkey_clr2protkey()=%d\n", rc);
  947. if (rc)
  948. break;
  949. if (copy_to_user(ucp, &kcp, sizeof(kcp)))
  950. return -EFAULT;
  951. memzero_explicit(&kcp, sizeof(kcp));
  952. break;
  953. }
  954. case PKEY_FINDCARD: {
  955. struct pkey_findcard __user *ufc = (void __user *) arg;
  956. struct pkey_findcard kfc;
  957. if (copy_from_user(&kfc, ufc, sizeof(kfc)))
  958. return -EFAULT;
  959. rc = pkey_findcard(&kfc.seckey,
  960. &kfc.cardnr, &kfc.domain, 1);
  961. DEBUG_DBG("pkey_ioctl pkey_findcard()=%d\n", rc);
  962. if (rc)
  963. break;
  964. if (copy_to_user(ufc, &kfc, sizeof(kfc)))
  965. return -EFAULT;
  966. break;
  967. }
  968. case PKEY_SKEY2PKEY: {
  969. struct pkey_skey2pkey __user *usp = (void __user *) arg;
  970. struct pkey_skey2pkey ksp;
  971. if (copy_from_user(&ksp, usp, sizeof(ksp)))
  972. return -EFAULT;
  973. rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
  974. DEBUG_DBG("pkey_ioctl pkey_skey2pkey()=%d\n", rc);
  975. if (rc)
  976. break;
  977. if (copy_to_user(usp, &ksp, sizeof(ksp)))
  978. return -EFAULT;
  979. break;
  980. }
  981. default:
  982. /* unknown/unsupported ioctl cmd */
  983. return -ENOTTY;
  984. }
  985. return rc;
  986. }
  987. /*
  988. * Sysfs and file io operations
  989. */
  990. static const struct file_operations pkey_fops = {
  991. .owner = THIS_MODULE,
  992. .open = nonseekable_open,
  993. .llseek = no_llseek,
  994. .unlocked_ioctl = pkey_unlocked_ioctl,
  995. };
  996. static struct miscdevice pkey_dev = {
  997. .name = "pkey",
  998. .minor = MISC_DYNAMIC_MINOR,
  999. .mode = 0666,
  1000. .fops = &pkey_fops,
  1001. };
  1002. /*
  1003. * Module init
  1004. */
  1005. int __init pkey_init(void)
  1006. {
  1007. cpacf_mask_t pckmo_functions;
  1008. /* check for pckmo instructions available */
  1009. if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
  1010. return -EOPNOTSUPP;
  1011. if (!cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_128_KEY) ||
  1012. !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_192_KEY) ||
  1013. !cpacf_test_func(&pckmo_functions, CPACF_PCKMO_ENC_AES_256_KEY))
  1014. return -EOPNOTSUPP;
  1015. pkey_debug_init();
  1016. return misc_register(&pkey_dev);
  1017. }
  1018. /*
  1019. * Module exit
  1020. */
  1021. static void __exit pkey_exit(void)
  1022. {
  1023. misc_deregister(&pkey_dev);
  1024. mkvp_cache_free();
  1025. pkey_debug_exit();
  1026. }
  1027. module_init(pkey_init);
  1028. module_exit(pkey_exit);