pkey_api.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * pkey device driver
  4. *
  5. * Copyright IBM Corp. 2017
  6. * Author(s): Harald Freudenberger
  7. */
  8. #define KMSG_COMPONENT "pkey"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/random.h>
  18. #include <linux/cpufeature.h>
  19. #include <asm/zcrypt.h>
  20. #include <asm/cpacf.h>
  21. #include <asm/pkey.h>
  22. #include <crypto/aes.h>
  23. #include "zcrypt_api.h"
  24. MODULE_LICENSE("GPL");
  25. MODULE_AUTHOR("IBM Corporation");
  26. MODULE_DESCRIPTION("s390 protected key interface");
  27. /* Size of parameter block used for all cca requests/replies */
  28. #define PARMBSIZE 512
  29. /* Size of vardata block used for some of the cca requests/replies */
  30. #define VARDATASIZE 4096
  31. /* mask of available pckmo subfunctions, fetched once at module init */
  32. static cpacf_mask_t pckmo_functions;
  33. /*
  34. * debug feature data and functions
  35. */
  36. static debug_info_t *debug_info;
  37. #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
  38. #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
  39. #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
  40. #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
  41. static void __init pkey_debug_init(void)
  42. {
  43. debug_info = debug_register("pkey", 1, 1, 4 * sizeof(long));
  44. debug_register_view(debug_info, &debug_sprintf_view);
  45. debug_set_level(debug_info, 3);
  46. }
  47. static void __exit pkey_debug_exit(void)
  48. {
  49. debug_unregister(debug_info);
  50. }
  51. /* Key token types */
  52. #define TOKTYPE_NON_CCA 0x00 /* Non-CCA key token */
  53. #define TOKTYPE_CCA_INTERNAL 0x01 /* CCA internal key token */
  54. /* For TOKTYPE_NON_CCA: */
  55. #define TOKVER_PROTECTED_KEY 0x01 /* Protected key token */
  56. /* For TOKTYPE_CCA_INTERNAL: */
  57. #define TOKVER_CCA_AES 0x04 /* CCA AES key token */
  58. /* header part of a key token */
  59. struct keytoken_header {
  60. u8 type; /* one of the TOKTYPE values */
  61. u8 res0[3];
  62. u8 version; /* one of the TOKVER values */
  63. u8 res1[3];
  64. } __packed;
  65. /* inside view of a secure key token (only type 0x01 version 0x04) */
  66. struct secaeskeytoken {
  67. u8 type; /* 0x01 for internal key token */
  68. u8 res0[3];
  69. u8 version; /* should be 0x04 */
  70. u8 res1[1];
  71. u8 flag; /* key flags */
  72. u8 res2[1];
  73. u64 mkvp; /* master key verification pattern */
  74. u8 key[32]; /* key value (encrypted) */
  75. u8 cv[8]; /* control vector */
  76. u16 bitsize; /* key bit size */
  77. u16 keysize; /* key byte size */
  78. u8 tvv[4]; /* token validation value */
  79. } __packed;
  80. /* inside view of a protected key token (only type 0x00 version 0x01) */
  81. struct protaeskeytoken {
  82. u8 type; /* 0x00 for PAES specific key tokens */
  83. u8 res0[3];
  84. u8 version; /* should be 0x01 for protected AES key token */
  85. u8 res1[3];
  86. u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
  87. u32 len; /* bytes actually stored in protkey[] */
  88. u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */
  89. } __packed;
  90. /*
  91. * Simple check if the token is a valid CCA secure AES key
  92. * token. If keybitsize is given, the bitsize of the key is
  93. * also checked. Returns 0 on success or errno value on failure.
  94. */
  95. static int check_secaeskeytoken(const u8 *token, int keybitsize)
  96. {
  97. struct secaeskeytoken *t = (struct secaeskeytoken *) token;
  98. if (t->type != TOKTYPE_CCA_INTERNAL) {
  99. DEBUG_ERR(
  100. "%s secure token check failed, type mismatch 0x%02x != 0x%02x\n",
  101. __func__, (int) t->type, TOKTYPE_CCA_INTERNAL);
  102. return -EINVAL;
  103. }
  104. if (t->version != TOKVER_CCA_AES) {
  105. DEBUG_ERR(
  106. "%s secure token check failed, version mismatch 0x%02x != 0x%02x\n",
  107. __func__, (int) t->version, TOKVER_CCA_AES);
  108. return -EINVAL;
  109. }
  110. if (keybitsize > 0 && t->bitsize != keybitsize) {
  111. DEBUG_ERR(
  112. "%s secure token check failed, bitsize mismatch %d != %d\n",
  113. __func__, (int) t->bitsize, keybitsize);
  114. return -EINVAL;
  115. }
  116. return 0;
  117. }
  118. /*
  119. * Allocate consecutive memory for request CPRB, request param
  120. * block, reply CPRB and reply param block and fill in values
  121. * for the common fields. Returns 0 on success or errno value
  122. * on failure.
  123. */
  124. static int alloc_and_prep_cprbmem(size_t paramblen,
  125. u8 **pcprbmem,
  126. struct CPRBX **preqCPRB,
  127. struct CPRBX **prepCPRB)
  128. {
  129. u8 *cprbmem;
  130. size_t cprbplusparamblen = sizeof(struct CPRBX) + paramblen;
  131. struct CPRBX *preqcblk, *prepcblk;
  132. /*
  133. * allocate consecutive memory for request CPRB, request param
  134. * block, reply CPRB and reply param block
  135. */
  136. cprbmem = kcalloc(2, cprbplusparamblen, GFP_KERNEL);
  137. if (!cprbmem)
  138. return -ENOMEM;
  139. preqcblk = (struct CPRBX *) cprbmem;
  140. prepcblk = (struct CPRBX *) (cprbmem + cprbplusparamblen);
  141. /* fill request cprb struct */
  142. preqcblk->cprb_len = sizeof(struct CPRBX);
  143. preqcblk->cprb_ver_id = 0x02;
  144. memcpy(preqcblk->func_id, "T2", 2);
  145. preqcblk->rpl_msgbl = cprbplusparamblen;
  146. if (paramblen) {
  147. preqcblk->req_parmb =
  148. ((u8 *) preqcblk) + sizeof(struct CPRBX);
  149. preqcblk->rpl_parmb =
  150. ((u8 *) prepcblk) + sizeof(struct CPRBX);
  151. }
  152. *pcprbmem = cprbmem;
  153. *preqCPRB = preqcblk;
  154. *prepCPRB = prepcblk;
  155. return 0;
  156. }
  157. /*
  158. * Free the cprb memory allocated with the function above.
  159. * If the scrub value is not zero, the memory is filled
  160. * with zeros before freeing (useful if there was some
  161. * clear key material in there).
  162. */
  163. static void free_cprbmem(void *mem, size_t paramblen, int scrub)
  164. {
  165. if (scrub)
  166. memzero_explicit(mem, 2 * (sizeof(struct CPRBX) + paramblen));
  167. kfree(mem);
  168. }
  169. /*
  170. * Helper function to prepare the xcrb struct
  171. */
  172. static inline void prep_xcrb(struct ica_xcRB *pxcrb,
  173. u16 cardnr,
  174. struct CPRBX *preqcblk,
  175. struct CPRBX *prepcblk)
  176. {
  177. memset(pxcrb, 0, sizeof(*pxcrb));
  178. pxcrb->agent_ID = 0x4341; /* 'CA' */
  179. pxcrb->user_defined = (cardnr == 0xFFFF ? AUTOSELECT : cardnr);
  180. pxcrb->request_control_blk_length =
  181. preqcblk->cprb_len + preqcblk->req_parml;
  182. pxcrb->request_control_blk_addr = (void __user *) preqcblk;
  183. pxcrb->reply_control_blk_length = preqcblk->rpl_msgbl;
  184. pxcrb->reply_control_blk_addr = (void __user *) prepcblk;
  185. }
  186. /*
  187. * Helper function which calls zcrypt_send_cprb with
  188. * memory management segment adjusted to kernel space
  189. * so that the copy_from_user called within this
  190. * function do in fact copy from kernel space.
  191. */
  192. static inline int _zcrypt_send_cprb(struct ica_xcRB *xcrb)
  193. {
  194. int rc;
  195. mm_segment_t old_fs = get_fs();
  196. set_fs(KERNEL_DS);
  197. rc = zcrypt_send_cprb(xcrb);
  198. set_fs(old_fs);
  199. return rc;
  200. }
  201. /*
  202. * Generate (random) AES secure key.
  203. */
  204. int pkey_genseckey(u16 cardnr, u16 domain,
  205. u32 keytype, struct pkey_seckey *seckey)
  206. {
  207. int i, rc, keysize;
  208. int seckeysize;
  209. u8 *mem;
  210. struct CPRBX *preqcblk, *prepcblk;
  211. struct ica_xcRB xcrb;
  212. struct kgreqparm {
  213. u8 subfunc_code[2];
  214. u16 rule_array_len;
  215. struct lv1 {
  216. u16 len;
  217. char key_form[8];
  218. char key_length[8];
  219. char key_type1[8];
  220. char key_type2[8];
  221. } lv1;
  222. struct lv2 {
  223. u16 len;
  224. struct keyid {
  225. u16 len;
  226. u16 attr;
  227. u8 data[SECKEYBLOBSIZE];
  228. } keyid[6];
  229. } lv2;
  230. } *preqparm;
  231. struct kgrepparm {
  232. u8 subfunc_code[2];
  233. u16 rule_array_len;
  234. struct lv3 {
  235. u16 len;
  236. u16 keyblocklen;
  237. struct {
  238. u16 toklen;
  239. u16 tokattr;
  240. u8 tok[0];
  241. /* ... some more data ... */
  242. } keyblock;
  243. } lv3;
  244. } *prepparm;
  245. /* get already prepared memory for 2 cprbs with param block each */
  246. rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
  247. if (rc)
  248. return rc;
  249. /* fill request cprb struct */
  250. preqcblk->domain = domain;
  251. /* fill request cprb param block with KG request */
  252. preqparm = (struct kgreqparm *) preqcblk->req_parmb;
  253. memcpy(preqparm->subfunc_code, "KG", 2);
  254. preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
  255. preqparm->lv1.len = sizeof(struct lv1);
  256. memcpy(preqparm->lv1.key_form, "OP ", 8);
  257. switch (keytype) {
  258. case PKEY_KEYTYPE_AES_128:
  259. keysize = 16;
  260. memcpy(preqparm->lv1.key_length, "KEYLN16 ", 8);
  261. break;
  262. case PKEY_KEYTYPE_AES_192:
  263. keysize = 24;
  264. memcpy(preqparm->lv1.key_length, "KEYLN24 ", 8);
  265. break;
  266. case PKEY_KEYTYPE_AES_256:
  267. keysize = 32;
  268. memcpy(preqparm->lv1.key_length, "KEYLN32 ", 8);
  269. break;
  270. default:
  271. DEBUG_ERR(
  272. "%s unknown/unsupported keytype %d\n",
  273. __func__, keytype);
  274. rc = -EINVAL;
  275. goto out;
  276. }
  277. memcpy(preqparm->lv1.key_type1, "AESDATA ", 8);
  278. preqparm->lv2.len = sizeof(struct lv2);
  279. for (i = 0; i < 6; i++) {
  280. preqparm->lv2.keyid[i].len = sizeof(struct keyid);
  281. preqparm->lv2.keyid[i].attr = (i == 2 ? 0x30 : 0x10);
  282. }
  283. preqcblk->req_parml = sizeof(struct kgreqparm);
  284. /* fill xcrb struct */
  285. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  286. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  287. rc = _zcrypt_send_cprb(&xcrb);
  288. if (rc) {
  289. DEBUG_ERR(
  290. "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  291. __func__, (int) cardnr, (int) domain, rc);
  292. goto out;
  293. }
  294. /* check response returncode and reasoncode */
  295. if (prepcblk->ccp_rtcode != 0) {
  296. DEBUG_ERR(
  297. "%s secure key generate failure, card response %d/%d\n",
  298. __func__,
  299. (int) prepcblk->ccp_rtcode,
  300. (int) prepcblk->ccp_rscode);
  301. rc = -EIO;
  302. goto out;
  303. }
  304. /* process response cprb param block */
  305. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  306. prepparm = (struct kgrepparm *) prepcblk->rpl_parmb;
  307. /* check length of the returned secure key token */
  308. seckeysize = prepparm->lv3.keyblock.toklen
  309. - sizeof(prepparm->lv3.keyblock.toklen)
  310. - sizeof(prepparm->lv3.keyblock.tokattr);
  311. if (seckeysize != SECKEYBLOBSIZE) {
  312. DEBUG_ERR(
  313. "%s secure token size mismatch %d != %d bytes\n",
  314. __func__, seckeysize, SECKEYBLOBSIZE);
  315. rc = -EIO;
  316. goto out;
  317. }
  318. /* check secure key token */
  319. rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
  320. if (rc) {
  321. rc = -EIO;
  322. goto out;
  323. }
  324. /* copy the generated secure key token */
  325. memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
  326. out:
  327. free_cprbmem(mem, PARMBSIZE, 0);
  328. return rc;
  329. }
  330. EXPORT_SYMBOL(pkey_genseckey);
  331. /*
  332. * Generate an AES secure key with given key value.
  333. */
  334. int pkey_clr2seckey(u16 cardnr, u16 domain, u32 keytype,
  335. const struct pkey_clrkey *clrkey,
  336. struct pkey_seckey *seckey)
  337. {
  338. int rc, keysize, seckeysize;
  339. u8 *mem;
  340. struct CPRBX *preqcblk, *prepcblk;
  341. struct ica_xcRB xcrb;
  342. struct cmreqparm {
  343. u8 subfunc_code[2];
  344. u16 rule_array_len;
  345. char rule_array[8];
  346. struct lv1 {
  347. u16 len;
  348. u8 clrkey[0];
  349. } lv1;
  350. struct lv2 {
  351. u16 len;
  352. struct keyid {
  353. u16 len;
  354. u16 attr;
  355. u8 data[SECKEYBLOBSIZE];
  356. } keyid;
  357. } lv2;
  358. } *preqparm;
  359. struct lv2 *plv2;
  360. struct cmrepparm {
  361. u8 subfunc_code[2];
  362. u16 rule_array_len;
  363. struct lv3 {
  364. u16 len;
  365. u16 keyblocklen;
  366. struct {
  367. u16 toklen;
  368. u16 tokattr;
  369. u8 tok[0];
  370. /* ... some more data ... */
  371. } keyblock;
  372. } lv3;
  373. } *prepparm;
  374. /* get already prepared memory for 2 cprbs with param block each */
  375. rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
  376. if (rc)
  377. return rc;
  378. /* fill request cprb struct */
  379. preqcblk->domain = domain;
  380. /* fill request cprb param block with CM request */
  381. preqparm = (struct cmreqparm *) preqcblk->req_parmb;
  382. memcpy(preqparm->subfunc_code, "CM", 2);
  383. memcpy(preqparm->rule_array, "AES ", 8);
  384. preqparm->rule_array_len =
  385. sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
  386. switch (keytype) {
  387. case PKEY_KEYTYPE_AES_128:
  388. keysize = 16;
  389. break;
  390. case PKEY_KEYTYPE_AES_192:
  391. keysize = 24;
  392. break;
  393. case PKEY_KEYTYPE_AES_256:
  394. keysize = 32;
  395. break;
  396. default:
  397. DEBUG_ERR(
  398. "%s unknown/unsupported keytype %d\n",
  399. __func__, keytype);
  400. rc = -EINVAL;
  401. goto out;
  402. }
  403. preqparm->lv1.len = sizeof(struct lv1) + keysize;
  404. memcpy(preqparm->lv1.clrkey, clrkey->clrkey, keysize);
  405. plv2 = (struct lv2 *) (((u8 *) &preqparm->lv2) + keysize);
  406. plv2->len = sizeof(struct lv2);
  407. plv2->keyid.len = sizeof(struct keyid);
  408. plv2->keyid.attr = 0x30;
  409. preqcblk->req_parml = sizeof(struct cmreqparm) + keysize;
  410. /* fill xcrb struct */
  411. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  412. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  413. rc = _zcrypt_send_cprb(&xcrb);
  414. if (rc) {
  415. DEBUG_ERR(
  416. "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  417. __func__, (int) cardnr, (int) domain, rc);
  418. goto out;
  419. }
  420. /* check response returncode and reasoncode */
  421. if (prepcblk->ccp_rtcode != 0) {
  422. DEBUG_ERR(
  423. "%s clear key import failure, card response %d/%d\n",
  424. __func__,
  425. (int) prepcblk->ccp_rtcode,
  426. (int) prepcblk->ccp_rscode);
  427. rc = -EIO;
  428. goto out;
  429. }
  430. /* process response cprb param block */
  431. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  432. prepparm = (struct cmrepparm *) prepcblk->rpl_parmb;
  433. /* check length of the returned secure key token */
  434. seckeysize = prepparm->lv3.keyblock.toklen
  435. - sizeof(prepparm->lv3.keyblock.toklen)
  436. - sizeof(prepparm->lv3.keyblock.tokattr);
  437. if (seckeysize != SECKEYBLOBSIZE) {
  438. DEBUG_ERR(
  439. "%s secure token size mismatch %d != %d bytes\n",
  440. __func__, seckeysize, SECKEYBLOBSIZE);
  441. rc = -EIO;
  442. goto out;
  443. }
  444. /* check secure key token */
  445. rc = check_secaeskeytoken(prepparm->lv3.keyblock.tok, 8*keysize);
  446. if (rc) {
  447. rc = -EIO;
  448. goto out;
  449. }
  450. /* copy the generated secure key token */
  451. memcpy(seckey->seckey, prepparm->lv3.keyblock.tok, SECKEYBLOBSIZE);
  452. out:
  453. free_cprbmem(mem, PARMBSIZE, 1);
  454. return rc;
  455. }
  456. EXPORT_SYMBOL(pkey_clr2seckey);
  457. /*
  458. * Derive a proteced key from the secure key blob.
  459. */
  460. int pkey_sec2protkey(u16 cardnr, u16 domain,
  461. const struct pkey_seckey *seckey,
  462. struct pkey_protkey *protkey)
  463. {
  464. int rc;
  465. u8 *mem;
  466. struct CPRBX *preqcblk, *prepcblk;
  467. struct ica_xcRB xcrb;
  468. struct uskreqparm {
  469. u8 subfunc_code[2];
  470. u16 rule_array_len;
  471. struct lv1 {
  472. u16 len;
  473. u16 attr_len;
  474. u16 attr_flags;
  475. } lv1;
  476. struct lv2 {
  477. u16 len;
  478. u16 attr_len;
  479. u16 attr_flags;
  480. u8 token[0]; /* cca secure key token */
  481. } lv2 __packed;
  482. } *preqparm;
  483. struct uskrepparm {
  484. u8 subfunc_code[2];
  485. u16 rule_array_len;
  486. struct lv3 {
  487. u16 len;
  488. u16 attr_len;
  489. u16 attr_flags;
  490. struct cpacfkeyblock {
  491. u8 version; /* version of this struct */
  492. u8 flags[2];
  493. u8 algo;
  494. u8 form;
  495. u8 pad1[3];
  496. u16 keylen;
  497. u8 key[64]; /* the key (keylen bytes) */
  498. u16 keyattrlen;
  499. u8 keyattr[32];
  500. u8 pad2[1];
  501. u8 vptype;
  502. u8 vp[32]; /* verification pattern */
  503. } keyblock;
  504. } lv3 __packed;
  505. } *prepparm;
  506. /* get already prepared memory for 2 cprbs with param block each */
  507. rc = alloc_and_prep_cprbmem(PARMBSIZE, &mem, &preqcblk, &prepcblk);
  508. if (rc)
  509. return rc;
  510. /* fill request cprb struct */
  511. preqcblk->domain = domain;
  512. /* fill request cprb param block with USK request */
  513. preqparm = (struct uskreqparm *) preqcblk->req_parmb;
  514. memcpy(preqparm->subfunc_code, "US", 2);
  515. preqparm->rule_array_len = sizeof(preqparm->rule_array_len);
  516. preqparm->lv1.len = sizeof(struct lv1);
  517. preqparm->lv1.attr_len = sizeof(struct lv1) - sizeof(preqparm->lv1.len);
  518. preqparm->lv1.attr_flags = 0x0001;
  519. preqparm->lv2.len = sizeof(struct lv2) + SECKEYBLOBSIZE;
  520. preqparm->lv2.attr_len = sizeof(struct lv2)
  521. - sizeof(preqparm->lv2.len) + SECKEYBLOBSIZE;
  522. preqparm->lv2.attr_flags = 0x0000;
  523. memcpy(preqparm->lv2.token, seckey->seckey, SECKEYBLOBSIZE);
  524. preqcblk->req_parml = sizeof(struct uskreqparm) + SECKEYBLOBSIZE;
  525. /* fill xcrb struct */
  526. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  527. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  528. rc = _zcrypt_send_cprb(&xcrb);
  529. if (rc) {
  530. DEBUG_ERR(
  531. "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  532. __func__, (int) cardnr, (int) domain, rc);
  533. goto out;
  534. }
  535. /* check response returncode and reasoncode */
  536. if (prepcblk->ccp_rtcode != 0) {
  537. DEBUG_ERR(
  538. "%s unwrap secure key failure, card response %d/%d\n",
  539. __func__,
  540. (int) prepcblk->ccp_rtcode,
  541. (int) prepcblk->ccp_rscode);
  542. rc = -EIO;
  543. goto out;
  544. }
  545. if (prepcblk->ccp_rscode != 0) {
  546. DEBUG_WARN(
  547. "%s unwrap secure key warning, card response %d/%d\n",
  548. __func__,
  549. (int) prepcblk->ccp_rtcode,
  550. (int) prepcblk->ccp_rscode);
  551. }
  552. /* process response cprb param block */
  553. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  554. prepparm = (struct uskrepparm *) prepcblk->rpl_parmb;
  555. /* check the returned keyblock */
  556. if (prepparm->lv3.keyblock.version != 0x01) {
  557. DEBUG_ERR(
  558. "%s reply param keyblock version mismatch 0x%02x != 0x01\n",
  559. __func__, (int) prepparm->lv3.keyblock.version);
  560. rc = -EIO;
  561. goto out;
  562. }
  563. /* copy the tanslated protected key */
  564. switch (prepparm->lv3.keyblock.keylen) {
  565. case 16+32:
  566. protkey->type = PKEY_KEYTYPE_AES_128;
  567. break;
  568. case 24+32:
  569. protkey->type = PKEY_KEYTYPE_AES_192;
  570. break;
  571. case 32+32:
  572. protkey->type = PKEY_KEYTYPE_AES_256;
  573. break;
  574. default:
  575. DEBUG_ERR("%s unknown/unsupported keytype %d\n",
  576. __func__, prepparm->lv3.keyblock.keylen);
  577. rc = -EIO;
  578. goto out;
  579. }
  580. protkey->len = prepparm->lv3.keyblock.keylen;
  581. memcpy(protkey->protkey, prepparm->lv3.keyblock.key, protkey->len);
  582. out:
  583. free_cprbmem(mem, PARMBSIZE, 0);
  584. return rc;
  585. }
  586. EXPORT_SYMBOL(pkey_sec2protkey);
  587. /*
  588. * Create a protected key from a clear key value.
  589. */
  590. int pkey_clr2protkey(u32 keytype,
  591. const struct pkey_clrkey *clrkey,
  592. struct pkey_protkey *protkey)
  593. {
  594. long fc;
  595. int keysize;
  596. u8 paramblock[64];
  597. switch (keytype) {
  598. case PKEY_KEYTYPE_AES_128:
  599. keysize = 16;
  600. fc = CPACF_PCKMO_ENC_AES_128_KEY;
  601. break;
  602. case PKEY_KEYTYPE_AES_192:
  603. keysize = 24;
  604. fc = CPACF_PCKMO_ENC_AES_192_KEY;
  605. break;
  606. case PKEY_KEYTYPE_AES_256:
  607. keysize = 32;
  608. fc = CPACF_PCKMO_ENC_AES_256_KEY;
  609. break;
  610. default:
  611. DEBUG_ERR("%s unknown/unsupported keytype %d\n",
  612. __func__, keytype);
  613. return -EINVAL;
  614. }
  615. /*
  616. * Check if the needed pckmo subfunction is available.
  617. * These subfunctions can be enabled/disabled by customers
  618. * in the LPAR profile or may even change on the fly.
  619. */
  620. if (!cpacf_test_func(&pckmo_functions, fc)) {
  621. DEBUG_ERR("%s pckmo functions not available\n", __func__);
  622. return -EOPNOTSUPP;
  623. }
  624. /* prepare param block */
  625. memset(paramblock, 0, sizeof(paramblock));
  626. memcpy(paramblock, clrkey->clrkey, keysize);
  627. /* call the pckmo instruction */
  628. cpacf_pckmo(fc, paramblock);
  629. /* copy created protected key */
  630. protkey->type = keytype;
  631. protkey->len = keysize + 32;
  632. memcpy(protkey->protkey, paramblock, keysize + 32);
  633. return 0;
  634. }
  635. EXPORT_SYMBOL(pkey_clr2protkey);
  636. /*
  637. * query cryptographic facility from adapter
  638. */
  639. static int query_crypto_facility(u16 cardnr, u16 domain,
  640. const char *keyword,
  641. u8 *rarray, size_t *rarraylen,
  642. u8 *varray, size_t *varraylen)
  643. {
  644. int rc;
  645. u16 len;
  646. u8 *mem, *ptr;
  647. struct CPRBX *preqcblk, *prepcblk;
  648. struct ica_xcRB xcrb;
  649. struct fqreqparm {
  650. u8 subfunc_code[2];
  651. u16 rule_array_len;
  652. char rule_array[8];
  653. struct lv1 {
  654. u16 len;
  655. u8 data[VARDATASIZE];
  656. } lv1;
  657. u16 dummylen;
  658. } *preqparm;
  659. size_t parmbsize = sizeof(struct fqreqparm);
  660. struct fqrepparm {
  661. u8 subfunc_code[2];
  662. u8 lvdata[0];
  663. } *prepparm;
  664. /* get already prepared memory for 2 cprbs with param block each */
  665. rc = alloc_and_prep_cprbmem(parmbsize, &mem, &preqcblk, &prepcblk);
  666. if (rc)
  667. return rc;
  668. /* fill request cprb struct */
  669. preqcblk->domain = domain;
  670. /* fill request cprb param block with FQ request */
  671. preqparm = (struct fqreqparm *) preqcblk->req_parmb;
  672. memcpy(preqparm->subfunc_code, "FQ", 2);
  673. memcpy(preqparm->rule_array, keyword, sizeof(preqparm->rule_array));
  674. preqparm->rule_array_len =
  675. sizeof(preqparm->rule_array_len) + sizeof(preqparm->rule_array);
  676. preqparm->lv1.len = sizeof(preqparm->lv1);
  677. preqparm->dummylen = sizeof(preqparm->dummylen);
  678. preqcblk->req_parml = parmbsize;
  679. /* fill xcrb struct */
  680. prep_xcrb(&xcrb, cardnr, preqcblk, prepcblk);
  681. /* forward xcrb with request CPRB and reply CPRB to zcrypt dd */
  682. rc = _zcrypt_send_cprb(&xcrb);
  683. if (rc) {
  684. DEBUG_ERR(
  685. "%s zcrypt_send_cprb (cardnr=%d domain=%d) failed with errno %d\n",
  686. __func__, (int) cardnr, (int) domain, rc);
  687. goto out;
  688. }
  689. /* check response returncode and reasoncode */
  690. if (prepcblk->ccp_rtcode != 0) {
  691. DEBUG_ERR(
  692. "%s unwrap secure key failure, card response %d/%d\n",
  693. __func__,
  694. (int) prepcblk->ccp_rtcode,
  695. (int) prepcblk->ccp_rscode);
  696. rc = -EIO;
  697. goto out;
  698. }
  699. /* process response cprb param block */
  700. prepcblk->rpl_parmb = ((u8 *) prepcblk) + sizeof(struct CPRBX);
  701. prepparm = (struct fqrepparm *) prepcblk->rpl_parmb;
  702. ptr = prepparm->lvdata;
  703. /* check and possibly copy reply rule array */
  704. len = *((u16 *) ptr);
  705. if (len > sizeof(u16)) {
  706. ptr += sizeof(u16);
  707. len -= sizeof(u16);
  708. if (rarray && rarraylen && *rarraylen > 0) {
  709. *rarraylen = (len > *rarraylen ? *rarraylen : len);
  710. memcpy(rarray, ptr, *rarraylen);
  711. }
  712. ptr += len;
  713. }
  714. /* check and possible copy reply var array */
  715. len = *((u16 *) ptr);
  716. if (len > sizeof(u16)) {
  717. ptr += sizeof(u16);
  718. len -= sizeof(u16);
  719. if (varray && varraylen && *varraylen > 0) {
  720. *varraylen = (len > *varraylen ? *varraylen : len);
  721. memcpy(varray, ptr, *varraylen);
  722. }
  723. ptr += len;
  724. }
  725. out:
  726. free_cprbmem(mem, parmbsize, 0);
  727. return rc;
  728. }
  729. /*
  730. * Fetch the current and old mkvp values via
  731. * query_crypto_facility from adapter.
  732. */
  733. static int fetch_mkvp(u16 cardnr, u16 domain, u64 mkvp[2])
  734. {
  735. int rc, found = 0;
  736. size_t rlen, vlen;
  737. u8 *rarray, *varray, *pg;
  738. pg = (u8 *) __get_free_page(GFP_KERNEL);
  739. if (!pg)
  740. return -ENOMEM;
  741. rarray = pg;
  742. varray = pg + PAGE_SIZE/2;
  743. rlen = vlen = PAGE_SIZE/2;
  744. rc = query_crypto_facility(cardnr, domain, "STATICSA",
  745. rarray, &rlen, varray, &vlen);
  746. if (rc == 0 && rlen > 8*8 && vlen > 184+8) {
  747. if (rarray[8*8] == '2') {
  748. /* current master key state is valid */
  749. mkvp[0] = *((u64 *)(varray + 184));
  750. mkvp[1] = *((u64 *)(varray + 172));
  751. found = 1;
  752. }
  753. }
  754. free_page((unsigned long) pg);
  755. return found ? 0 : -ENOENT;
  756. }
  757. /* struct to hold cached mkvp info for each card/domain */
  758. struct mkvp_info {
  759. struct list_head list;
  760. u16 cardnr;
  761. u16 domain;
  762. u64 mkvp[2];
  763. };
  764. /* a list with mkvp_info entries */
  765. static LIST_HEAD(mkvp_list);
  766. static DEFINE_SPINLOCK(mkvp_list_lock);
  767. static int mkvp_cache_fetch(u16 cardnr, u16 domain, u64 mkvp[2])
  768. {
  769. int rc = -ENOENT;
  770. struct mkvp_info *ptr;
  771. spin_lock_bh(&mkvp_list_lock);
  772. list_for_each_entry(ptr, &mkvp_list, list) {
  773. if (ptr->cardnr == cardnr &&
  774. ptr->domain == domain) {
  775. memcpy(mkvp, ptr->mkvp, 2 * sizeof(u64));
  776. rc = 0;
  777. break;
  778. }
  779. }
  780. spin_unlock_bh(&mkvp_list_lock);
  781. return rc;
  782. }
  783. static void mkvp_cache_update(u16 cardnr, u16 domain, u64 mkvp[2])
  784. {
  785. int found = 0;
  786. struct mkvp_info *ptr;
  787. spin_lock_bh(&mkvp_list_lock);
  788. list_for_each_entry(ptr, &mkvp_list, list) {
  789. if (ptr->cardnr == cardnr &&
  790. ptr->domain == domain) {
  791. memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
  792. found = 1;
  793. break;
  794. }
  795. }
  796. if (!found) {
  797. ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
  798. if (!ptr) {
  799. spin_unlock_bh(&mkvp_list_lock);
  800. return;
  801. }
  802. ptr->cardnr = cardnr;
  803. ptr->domain = domain;
  804. memcpy(ptr->mkvp, mkvp, 2 * sizeof(u64));
  805. list_add(&ptr->list, &mkvp_list);
  806. }
  807. spin_unlock_bh(&mkvp_list_lock);
  808. }
  809. static void mkvp_cache_scrub(u16 cardnr, u16 domain)
  810. {
  811. struct mkvp_info *ptr;
  812. spin_lock_bh(&mkvp_list_lock);
  813. list_for_each_entry(ptr, &mkvp_list, list) {
  814. if (ptr->cardnr == cardnr &&
  815. ptr->domain == domain) {
  816. list_del(&ptr->list);
  817. kfree(ptr);
  818. break;
  819. }
  820. }
  821. spin_unlock_bh(&mkvp_list_lock);
  822. }
  823. static void __exit mkvp_cache_free(void)
  824. {
  825. struct mkvp_info *ptr, *pnext;
  826. spin_lock_bh(&mkvp_list_lock);
  827. list_for_each_entry_safe(ptr, pnext, &mkvp_list, list) {
  828. list_del(&ptr->list);
  829. kfree(ptr);
  830. }
  831. spin_unlock_bh(&mkvp_list_lock);
  832. }
  833. /*
  834. * Search for a matching crypto card based on the Master Key
  835. * Verification Pattern provided inside a secure key.
  836. */
  837. int pkey_findcard(const struct pkey_seckey *seckey,
  838. u16 *pcardnr, u16 *pdomain, int verify)
  839. {
  840. struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
  841. struct zcrypt_device_status_ext *device_status;
  842. u16 card, dom;
  843. u64 mkvp[2];
  844. int i, rc, oi = -1;
  845. /* mkvp must not be zero */
  846. if (t->mkvp == 0)
  847. return -EINVAL;
  848. /* fetch status of all crypto cards */
  849. device_status = kmalloc_array(MAX_ZDEV_ENTRIES_EXT,
  850. sizeof(struct zcrypt_device_status_ext),
  851. GFP_KERNEL);
  852. if (!device_status)
  853. return -ENOMEM;
  854. zcrypt_device_status_mask_ext(device_status);
  855. /* walk through all crypto cards */
  856. for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
  857. card = AP_QID_CARD(device_status[i].qid);
  858. dom = AP_QID_QUEUE(device_status[i].qid);
  859. if (device_status[i].online &&
  860. device_status[i].functions & 0x04) {
  861. /* an enabled CCA Coprocessor card */
  862. /* try cached mkvp */
  863. if (mkvp_cache_fetch(card, dom, mkvp) == 0 &&
  864. t->mkvp == mkvp[0]) {
  865. if (!verify)
  866. break;
  867. /* verify: fetch mkvp from adapter */
  868. if (fetch_mkvp(card, dom, mkvp) == 0) {
  869. mkvp_cache_update(card, dom, mkvp);
  870. if (t->mkvp == mkvp[0])
  871. break;
  872. }
  873. }
  874. } else {
  875. /* Card is offline and/or not a CCA card. */
  876. /* del mkvp entry from cache if it exists */
  877. mkvp_cache_scrub(card, dom);
  878. }
  879. }
  880. if (i >= MAX_ZDEV_ENTRIES_EXT) {
  881. /* nothing found, so this time without cache */
  882. for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
  883. if (!(device_status[i].online &&
  884. device_status[i].functions & 0x04))
  885. continue;
  886. card = AP_QID_CARD(device_status[i].qid);
  887. dom = AP_QID_QUEUE(device_status[i].qid);
  888. /* fresh fetch mkvp from adapter */
  889. if (fetch_mkvp(card, dom, mkvp) == 0) {
  890. mkvp_cache_update(card, dom, mkvp);
  891. if (t->mkvp == mkvp[0])
  892. break;
  893. if (t->mkvp == mkvp[1] && oi < 0)
  894. oi = i;
  895. }
  896. }
  897. if (i >= MAX_ZDEV_ENTRIES_EXT && oi >= 0) {
  898. /* old mkvp matched, use this card then */
  899. card = AP_QID_CARD(device_status[oi].qid);
  900. dom = AP_QID_QUEUE(device_status[oi].qid);
  901. }
  902. }
  903. if (i < MAX_ZDEV_ENTRIES_EXT || oi >= 0) {
  904. if (pcardnr)
  905. *pcardnr = card;
  906. if (pdomain)
  907. *pdomain = dom;
  908. rc = 0;
  909. } else
  910. rc = -ENODEV;
  911. kfree(device_status);
  912. return rc;
  913. }
  914. EXPORT_SYMBOL(pkey_findcard);
  915. /*
  916. * Find card and transform secure key into protected key.
  917. */
  918. int pkey_skey2pkey(const struct pkey_seckey *seckey,
  919. struct pkey_protkey *protkey)
  920. {
  921. u16 cardnr, domain;
  922. int rc, verify;
  923. /*
  924. * The pkey_sec2protkey call may fail when a card has been
  925. * addressed where the master key was changed after last fetch
  926. * of the mkvp into the cache. So first try without verify then
  927. * with verify enabled (thus refreshing the mkvp for each card).
  928. */
  929. for (verify = 0; verify < 2; verify++) {
  930. rc = pkey_findcard(seckey, &cardnr, &domain, verify);
  931. if (rc)
  932. continue;
  933. rc = pkey_sec2protkey(cardnr, domain, seckey, protkey);
  934. if (rc == 0)
  935. break;
  936. }
  937. if (rc)
  938. DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
  939. return rc;
  940. }
  941. EXPORT_SYMBOL(pkey_skey2pkey);
  942. /*
  943. * Verify key and give back some info about the key.
  944. */
  945. int pkey_verifykey(const struct pkey_seckey *seckey,
  946. u16 *pcardnr, u16 *pdomain,
  947. u16 *pkeysize, u32 *pattributes)
  948. {
  949. struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
  950. u16 cardnr, domain;
  951. u64 mkvp[2];
  952. int rc;
  953. /* check the secure key for valid AES secure key */
  954. rc = check_secaeskeytoken((u8 *) seckey, 0);
  955. if (rc)
  956. goto out;
  957. if (pattributes)
  958. *pattributes = PKEY_VERIFY_ATTR_AES;
  959. if (pkeysize)
  960. *pkeysize = t->bitsize;
  961. /* try to find a card which can handle this key */
  962. rc = pkey_findcard(seckey, &cardnr, &domain, 1);
  963. if (rc)
  964. goto out;
  965. /* check mkvp for old mkvp match */
  966. rc = mkvp_cache_fetch(cardnr, domain, mkvp);
  967. if (rc)
  968. goto out;
  969. if (t->mkvp == mkvp[1]) {
  970. DEBUG_DBG("%s secure key has old mkvp\n", __func__);
  971. if (pattributes)
  972. *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
  973. }
  974. if (pcardnr)
  975. *pcardnr = cardnr;
  976. if (pdomain)
  977. *pdomain = domain;
  978. out:
  979. DEBUG_DBG("%s rc=%d\n", __func__, rc);
  980. return rc;
  981. }
  982. EXPORT_SYMBOL(pkey_verifykey);
  983. /*
  984. * Generate a random protected key
  985. */
  986. int pkey_genprotkey(__u32 keytype, struct pkey_protkey *protkey)
  987. {
  988. struct pkey_clrkey clrkey;
  989. int keysize;
  990. int rc;
  991. switch (keytype) {
  992. case PKEY_KEYTYPE_AES_128:
  993. keysize = 16;
  994. break;
  995. case PKEY_KEYTYPE_AES_192:
  996. keysize = 24;
  997. break;
  998. case PKEY_KEYTYPE_AES_256:
  999. keysize = 32;
  1000. break;
  1001. default:
  1002. DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
  1003. keytype);
  1004. return -EINVAL;
  1005. }
  1006. /* generate a dummy random clear key */
  1007. get_random_bytes(clrkey.clrkey, keysize);
  1008. /* convert it to a dummy protected key */
  1009. rc = pkey_clr2protkey(keytype, &clrkey, protkey);
  1010. if (rc)
  1011. return rc;
  1012. /* replace the key part of the protected key with random bytes */
  1013. get_random_bytes(protkey->protkey, keysize);
  1014. return 0;
  1015. }
  1016. EXPORT_SYMBOL(pkey_genprotkey);
  1017. /*
  1018. * Verify if a protected key is still valid
  1019. */
  1020. int pkey_verifyprotkey(const struct pkey_protkey *protkey)
  1021. {
  1022. unsigned long fc;
  1023. struct {
  1024. u8 iv[AES_BLOCK_SIZE];
  1025. u8 key[MAXPROTKEYSIZE];
  1026. } param;
  1027. u8 null_msg[AES_BLOCK_SIZE];
  1028. u8 dest_buf[AES_BLOCK_SIZE];
  1029. unsigned int k;
  1030. switch (protkey->type) {
  1031. case PKEY_KEYTYPE_AES_128:
  1032. fc = CPACF_KMC_PAES_128;
  1033. break;
  1034. case PKEY_KEYTYPE_AES_192:
  1035. fc = CPACF_KMC_PAES_192;
  1036. break;
  1037. case PKEY_KEYTYPE_AES_256:
  1038. fc = CPACF_KMC_PAES_256;
  1039. break;
  1040. default:
  1041. DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
  1042. protkey->type);
  1043. return -EINVAL;
  1044. }
  1045. memset(null_msg, 0, sizeof(null_msg));
  1046. memset(param.iv, 0, sizeof(param.iv));
  1047. memcpy(param.key, protkey->protkey, sizeof(param.key));
  1048. k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
  1049. sizeof(null_msg));
  1050. if (k != sizeof(null_msg)) {
  1051. DEBUG_ERR("%s protected key is not valid\n", __func__);
  1052. return -EKEYREJECTED;
  1053. }
  1054. return 0;
  1055. }
  1056. EXPORT_SYMBOL(pkey_verifyprotkey);
  1057. /*
  1058. * Transform a non-CCA key token into a protected key
  1059. */
  1060. static int pkey_nonccatok2pkey(const __u8 *key, __u32 keylen,
  1061. struct pkey_protkey *protkey)
  1062. {
  1063. struct keytoken_header *hdr = (struct keytoken_header *)key;
  1064. struct protaeskeytoken *t;
  1065. switch (hdr->version) {
  1066. case TOKVER_PROTECTED_KEY:
  1067. if (keylen != sizeof(struct protaeskeytoken))
  1068. return -EINVAL;
  1069. t = (struct protaeskeytoken *)key;
  1070. protkey->len = t->len;
  1071. protkey->type = t->keytype;
  1072. memcpy(protkey->protkey, t->protkey,
  1073. sizeof(protkey->protkey));
  1074. return pkey_verifyprotkey(protkey);
  1075. default:
  1076. DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
  1077. __func__, hdr->version);
  1078. return -EINVAL;
  1079. }
  1080. }
  1081. /*
  1082. * Transform a CCA internal key token into a protected key
  1083. */
  1084. static int pkey_ccainttok2pkey(const __u8 *key, __u32 keylen,
  1085. struct pkey_protkey *protkey)
  1086. {
  1087. struct keytoken_header *hdr = (struct keytoken_header *)key;
  1088. switch (hdr->version) {
  1089. case TOKVER_CCA_AES:
  1090. if (keylen != sizeof(struct secaeskeytoken))
  1091. return -EINVAL;
  1092. return pkey_skey2pkey((struct pkey_seckey *)key,
  1093. protkey);
  1094. default:
  1095. DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
  1096. __func__, hdr->version);
  1097. return -EINVAL;
  1098. }
  1099. }
  1100. /*
  1101. * Transform a key blob (of any type) into a protected key
  1102. */
  1103. int pkey_keyblob2pkey(const __u8 *key, __u32 keylen,
  1104. struct pkey_protkey *protkey)
  1105. {
  1106. struct keytoken_header *hdr = (struct keytoken_header *)key;
  1107. if (keylen < sizeof(struct keytoken_header))
  1108. return -EINVAL;
  1109. switch (hdr->type) {
  1110. case TOKTYPE_NON_CCA:
  1111. return pkey_nonccatok2pkey(key, keylen, protkey);
  1112. case TOKTYPE_CCA_INTERNAL:
  1113. return pkey_ccainttok2pkey(key, keylen, protkey);
  1114. default:
  1115. DEBUG_ERR("%s unknown/unsupported blob type %d\n", __func__,
  1116. hdr->type);
  1117. return -EINVAL;
  1118. }
  1119. }
  1120. EXPORT_SYMBOL(pkey_keyblob2pkey);
  1121. /*
  1122. * File io functions
  1123. */
  1124. static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
  1125. unsigned long arg)
  1126. {
  1127. int rc;
  1128. switch (cmd) {
  1129. case PKEY_GENSECK: {
  1130. struct pkey_genseck __user *ugs = (void __user *) arg;
  1131. struct pkey_genseck kgs;
  1132. if (copy_from_user(&kgs, ugs, sizeof(kgs)))
  1133. return -EFAULT;
  1134. rc = pkey_genseckey(kgs.cardnr, kgs.domain,
  1135. kgs.keytype, &kgs.seckey);
  1136. DEBUG_DBG("%s pkey_genseckey()=%d\n", __func__, rc);
  1137. if (rc)
  1138. break;
  1139. if (copy_to_user(ugs, &kgs, sizeof(kgs)))
  1140. return -EFAULT;
  1141. break;
  1142. }
  1143. case PKEY_CLR2SECK: {
  1144. struct pkey_clr2seck __user *ucs = (void __user *) arg;
  1145. struct pkey_clr2seck kcs;
  1146. if (copy_from_user(&kcs, ucs, sizeof(kcs)))
  1147. return -EFAULT;
  1148. rc = pkey_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
  1149. &kcs.clrkey, &kcs.seckey);
  1150. DEBUG_DBG("%s pkey_clr2seckey()=%d\n", __func__, rc);
  1151. if (rc)
  1152. break;
  1153. if (copy_to_user(ucs, &kcs, sizeof(kcs)))
  1154. return -EFAULT;
  1155. memzero_explicit(&kcs, sizeof(kcs));
  1156. break;
  1157. }
  1158. case PKEY_SEC2PROTK: {
  1159. struct pkey_sec2protk __user *usp = (void __user *) arg;
  1160. struct pkey_sec2protk ksp;
  1161. if (copy_from_user(&ksp, usp, sizeof(ksp)))
  1162. return -EFAULT;
  1163. rc = pkey_sec2protkey(ksp.cardnr, ksp.domain,
  1164. &ksp.seckey, &ksp.protkey);
  1165. DEBUG_DBG("%s pkey_sec2protkey()=%d\n", __func__, rc);
  1166. if (rc)
  1167. break;
  1168. if (copy_to_user(usp, &ksp, sizeof(ksp)))
  1169. return -EFAULT;
  1170. break;
  1171. }
  1172. case PKEY_CLR2PROTK: {
  1173. struct pkey_clr2protk __user *ucp = (void __user *) arg;
  1174. struct pkey_clr2protk kcp;
  1175. if (copy_from_user(&kcp, ucp, sizeof(kcp)))
  1176. return -EFAULT;
  1177. rc = pkey_clr2protkey(kcp.keytype,
  1178. &kcp.clrkey, &kcp.protkey);
  1179. DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
  1180. if (rc)
  1181. break;
  1182. if (copy_to_user(ucp, &kcp, sizeof(kcp)))
  1183. return -EFAULT;
  1184. memzero_explicit(&kcp, sizeof(kcp));
  1185. break;
  1186. }
  1187. case PKEY_FINDCARD: {
  1188. struct pkey_findcard __user *ufc = (void __user *) arg;
  1189. struct pkey_findcard kfc;
  1190. if (copy_from_user(&kfc, ufc, sizeof(kfc)))
  1191. return -EFAULT;
  1192. rc = pkey_findcard(&kfc.seckey,
  1193. &kfc.cardnr, &kfc.domain, 1);
  1194. DEBUG_DBG("%s pkey_findcard()=%d\n", __func__, rc);
  1195. if (rc)
  1196. break;
  1197. if (copy_to_user(ufc, &kfc, sizeof(kfc)))
  1198. return -EFAULT;
  1199. break;
  1200. }
  1201. case PKEY_SKEY2PKEY: {
  1202. struct pkey_skey2pkey __user *usp = (void __user *) arg;
  1203. struct pkey_skey2pkey ksp;
  1204. if (copy_from_user(&ksp, usp, sizeof(ksp)))
  1205. return -EFAULT;
  1206. rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
  1207. DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
  1208. if (rc)
  1209. break;
  1210. if (copy_to_user(usp, &ksp, sizeof(ksp)))
  1211. return -EFAULT;
  1212. break;
  1213. }
  1214. case PKEY_VERIFYKEY: {
  1215. struct pkey_verifykey __user *uvk = (void __user *) arg;
  1216. struct pkey_verifykey kvk;
  1217. if (copy_from_user(&kvk, uvk, sizeof(kvk)))
  1218. return -EFAULT;
  1219. rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
  1220. &kvk.keysize, &kvk.attributes);
  1221. DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
  1222. if (rc)
  1223. break;
  1224. if (copy_to_user(uvk, &kvk, sizeof(kvk)))
  1225. return -EFAULT;
  1226. break;
  1227. }
  1228. case PKEY_GENPROTK: {
  1229. struct pkey_genprotk __user *ugp = (void __user *) arg;
  1230. struct pkey_genprotk kgp;
  1231. if (copy_from_user(&kgp, ugp, sizeof(kgp)))
  1232. return -EFAULT;
  1233. rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
  1234. DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
  1235. if (rc)
  1236. break;
  1237. if (copy_to_user(ugp, &kgp, sizeof(kgp)))
  1238. return -EFAULT;
  1239. break;
  1240. }
  1241. case PKEY_VERIFYPROTK: {
  1242. struct pkey_verifyprotk __user *uvp = (void __user *) arg;
  1243. struct pkey_verifyprotk kvp;
  1244. if (copy_from_user(&kvp, uvp, sizeof(kvp)))
  1245. return -EFAULT;
  1246. rc = pkey_verifyprotkey(&kvp.protkey);
  1247. DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
  1248. break;
  1249. }
  1250. case PKEY_KBLOB2PROTK: {
  1251. struct pkey_kblob2pkey __user *utp = (void __user *) arg;
  1252. struct pkey_kblob2pkey ktp;
  1253. __u8 __user *ukey;
  1254. __u8 *kkey;
  1255. if (copy_from_user(&ktp, utp, sizeof(ktp)))
  1256. return -EFAULT;
  1257. if (ktp.keylen < MINKEYBLOBSIZE ||
  1258. ktp.keylen > MAXKEYBLOBSIZE)
  1259. return -EINVAL;
  1260. ukey = ktp.key;
  1261. kkey = kmalloc(ktp.keylen, GFP_KERNEL);
  1262. if (kkey == NULL)
  1263. return -ENOMEM;
  1264. if (copy_from_user(kkey, ukey, ktp.keylen)) {
  1265. kfree(kkey);
  1266. return -EFAULT;
  1267. }
  1268. rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
  1269. DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
  1270. kfree(kkey);
  1271. if (rc)
  1272. break;
  1273. if (copy_to_user(utp, &ktp, sizeof(ktp)))
  1274. return -EFAULT;
  1275. break;
  1276. }
  1277. default:
  1278. /* unknown/unsupported ioctl cmd */
  1279. return -ENOTTY;
  1280. }
  1281. return rc;
  1282. }
  1283. /*
  1284. * Sysfs and file io operations
  1285. */
  1286. /*
  1287. * Sysfs attribute read function for all protected key binary attributes.
  1288. * The implementation can not deal with partial reads, because a new random
  1289. * protected key blob is generated with each read. In case of partial reads
  1290. * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
  1291. */
  1292. static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
  1293. loff_t off, size_t count)
  1294. {
  1295. struct protaeskeytoken protkeytoken;
  1296. struct pkey_protkey protkey;
  1297. int rc;
  1298. if (off != 0 || count < sizeof(protkeytoken))
  1299. return -EINVAL;
  1300. if (is_xts)
  1301. if (count < 2 * sizeof(protkeytoken))
  1302. return -EINVAL;
  1303. memset(&protkeytoken, 0, sizeof(protkeytoken));
  1304. protkeytoken.type = TOKTYPE_NON_CCA;
  1305. protkeytoken.version = TOKVER_PROTECTED_KEY;
  1306. protkeytoken.keytype = keytype;
  1307. rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
  1308. if (rc)
  1309. return rc;
  1310. protkeytoken.len = protkey.len;
  1311. memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
  1312. memcpy(buf, &protkeytoken, sizeof(protkeytoken));
  1313. if (is_xts) {
  1314. rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
  1315. if (rc)
  1316. return rc;
  1317. protkeytoken.len = protkey.len;
  1318. memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
  1319. memcpy(buf + sizeof(protkeytoken), &protkeytoken,
  1320. sizeof(protkeytoken));
  1321. return 2 * sizeof(protkeytoken);
  1322. }
  1323. return sizeof(protkeytoken);
  1324. }
  1325. static ssize_t protkey_aes_128_read(struct file *filp,
  1326. struct kobject *kobj,
  1327. struct bin_attribute *attr,
  1328. char *buf, loff_t off,
  1329. size_t count)
  1330. {
  1331. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
  1332. off, count);
  1333. }
  1334. static ssize_t protkey_aes_192_read(struct file *filp,
  1335. struct kobject *kobj,
  1336. struct bin_attribute *attr,
  1337. char *buf, loff_t off,
  1338. size_t count)
  1339. {
  1340. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
  1341. off, count);
  1342. }
  1343. static ssize_t protkey_aes_256_read(struct file *filp,
  1344. struct kobject *kobj,
  1345. struct bin_attribute *attr,
  1346. char *buf, loff_t off,
  1347. size_t count)
  1348. {
  1349. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
  1350. off, count);
  1351. }
  1352. static ssize_t protkey_aes_128_xts_read(struct file *filp,
  1353. struct kobject *kobj,
  1354. struct bin_attribute *attr,
  1355. char *buf, loff_t off,
  1356. size_t count)
  1357. {
  1358. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
  1359. off, count);
  1360. }
  1361. static ssize_t protkey_aes_256_xts_read(struct file *filp,
  1362. struct kobject *kobj,
  1363. struct bin_attribute *attr,
  1364. char *buf, loff_t off,
  1365. size_t count)
  1366. {
  1367. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
  1368. off, count);
  1369. }
  1370. static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
  1371. static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
  1372. static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
  1373. static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
  1374. static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
  1375. static struct bin_attribute *protkey_attrs[] = {
  1376. &bin_attr_protkey_aes_128,
  1377. &bin_attr_protkey_aes_192,
  1378. &bin_attr_protkey_aes_256,
  1379. &bin_attr_protkey_aes_128_xts,
  1380. &bin_attr_protkey_aes_256_xts,
  1381. NULL
  1382. };
  1383. static struct attribute_group protkey_attr_group = {
  1384. .name = "protkey",
  1385. .bin_attrs = protkey_attrs,
  1386. };
  1387. /*
  1388. * Sysfs attribute read function for all secure key ccadata binary attributes.
  1389. * The implementation can not deal with partial reads, because a new random
  1390. * protected key blob is generated with each read. In case of partial reads
  1391. * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
  1392. */
  1393. static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
  1394. loff_t off, size_t count)
  1395. {
  1396. int rc;
  1397. if (off != 0 || count < sizeof(struct secaeskeytoken))
  1398. return -EINVAL;
  1399. if (is_xts)
  1400. if (count < 2 * sizeof(struct secaeskeytoken))
  1401. return -EINVAL;
  1402. rc = pkey_genseckey(-1, -1, keytype, (struct pkey_seckey *)buf);
  1403. if (rc)
  1404. return rc;
  1405. if (is_xts) {
  1406. buf += sizeof(struct pkey_seckey);
  1407. rc = pkey_genseckey(-1, -1, keytype, (struct pkey_seckey *)buf);
  1408. if (rc)
  1409. return rc;
  1410. return 2 * sizeof(struct secaeskeytoken);
  1411. }
  1412. return sizeof(struct secaeskeytoken);
  1413. }
  1414. static ssize_t ccadata_aes_128_read(struct file *filp,
  1415. struct kobject *kobj,
  1416. struct bin_attribute *attr,
  1417. char *buf, loff_t off,
  1418. size_t count)
  1419. {
  1420. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
  1421. off, count);
  1422. }
  1423. static ssize_t ccadata_aes_192_read(struct file *filp,
  1424. struct kobject *kobj,
  1425. struct bin_attribute *attr,
  1426. char *buf, loff_t off,
  1427. size_t count)
  1428. {
  1429. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
  1430. off, count);
  1431. }
  1432. static ssize_t ccadata_aes_256_read(struct file *filp,
  1433. struct kobject *kobj,
  1434. struct bin_attribute *attr,
  1435. char *buf, loff_t off,
  1436. size_t count)
  1437. {
  1438. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
  1439. off, count);
  1440. }
  1441. static ssize_t ccadata_aes_128_xts_read(struct file *filp,
  1442. struct kobject *kobj,
  1443. struct bin_attribute *attr,
  1444. char *buf, loff_t off,
  1445. size_t count)
  1446. {
  1447. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
  1448. off, count);
  1449. }
  1450. static ssize_t ccadata_aes_256_xts_read(struct file *filp,
  1451. struct kobject *kobj,
  1452. struct bin_attribute *attr,
  1453. char *buf, loff_t off,
  1454. size_t count)
  1455. {
  1456. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
  1457. off, count);
  1458. }
  1459. static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
  1460. static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
  1461. static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
  1462. static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
  1463. static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
  1464. static struct bin_attribute *ccadata_attrs[] = {
  1465. &bin_attr_ccadata_aes_128,
  1466. &bin_attr_ccadata_aes_192,
  1467. &bin_attr_ccadata_aes_256,
  1468. &bin_attr_ccadata_aes_128_xts,
  1469. &bin_attr_ccadata_aes_256_xts,
  1470. NULL
  1471. };
  1472. static struct attribute_group ccadata_attr_group = {
  1473. .name = "ccadata",
  1474. .bin_attrs = ccadata_attrs,
  1475. };
  1476. static const struct attribute_group *pkey_attr_groups[] = {
  1477. &protkey_attr_group,
  1478. &ccadata_attr_group,
  1479. NULL,
  1480. };
  1481. static const struct file_operations pkey_fops = {
  1482. .owner = THIS_MODULE,
  1483. .open = nonseekable_open,
  1484. .llseek = no_llseek,
  1485. .unlocked_ioctl = pkey_unlocked_ioctl,
  1486. };
  1487. static struct miscdevice pkey_dev = {
  1488. .name = "pkey",
  1489. .minor = MISC_DYNAMIC_MINOR,
  1490. .mode = 0666,
  1491. .fops = &pkey_fops,
  1492. .groups = pkey_attr_groups,
  1493. };
  1494. /*
  1495. * Module init
  1496. */
  1497. static int __init pkey_init(void)
  1498. {
  1499. cpacf_mask_t kmc_functions;
  1500. /*
  1501. * The pckmo instruction should be available - even if we don't
  1502. * actually invoke it. This instruction comes with MSA 3 which
  1503. * is also the minimum level for the kmc instructions which
  1504. * are able to work with protected keys.
  1505. */
  1506. if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
  1507. return -EOPNOTSUPP;
  1508. /* check for kmc instructions available */
  1509. if (!cpacf_query(CPACF_KMC, &kmc_functions))
  1510. return -EOPNOTSUPP;
  1511. if (!cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
  1512. !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
  1513. !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256))
  1514. return -EOPNOTSUPP;
  1515. pkey_debug_init();
  1516. return misc_register(&pkey_dev);
  1517. }
  1518. /*
  1519. * Module exit
  1520. */
  1521. static void __exit pkey_exit(void)
  1522. {
  1523. misc_deregister(&pkey_dev);
  1524. mkvp_cache_free();
  1525. pkey_debug_exit();
  1526. }
  1527. module_cpu_feature_match(MSA, pkey_init);
  1528. module_exit(pkey_exit);