pkey_api.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * pkey device driver
  4. *
  5. * Copyright IBM Corp. 2017,2019
  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. #include "zcrypt_ccamisc.h"
  25. #include "zcrypt_ep11misc.h"
  26. MODULE_LICENSE("GPL");
  27. MODULE_AUTHOR("IBM Corporation");
  28. MODULE_DESCRIPTION("s390 protected key interface");
  29. #define KEYBLOBBUFSIZE 8192 /* key buffer size used for internal processing */
  30. #define PROTKEYBLOBBUFSIZE 256 /* protected key buffer size used internal */
  31. #define MAXAPQNSINLIST 64 /* max 64 apqns within a apqn list */
  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. /* 5 arguments per dbf entry (including the format string ptr) */
  43. debug_info = debug_register("pkey", 1, 1, 5 * 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. /* inside view of a protected key token (only type 0x00 version 0x01) */
  52. struct protaeskeytoken {
  53. u8 type; /* 0x00 for PAES specific key tokens */
  54. u8 res0[3];
  55. u8 version; /* should be 0x01 for protected AES key token */
  56. u8 res1[3];
  57. u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
  58. u32 len; /* bytes actually stored in protkey[] */
  59. u8 protkey[MAXPROTKEYSIZE]; /* the protected key blob */
  60. } __packed;
  61. /* inside view of a clear key token (type 0x00 version 0x02) */
  62. struct clearaeskeytoken {
  63. u8 type; /* 0x00 for PAES specific key tokens */
  64. u8 res0[3];
  65. u8 version; /* 0x02 for clear AES key token */
  66. u8 res1[3];
  67. u32 keytype; /* key type, one of the PKEY_KEYTYPE values */
  68. u32 len; /* bytes actually stored in clearkey[] */
  69. u8 clearkey[]; /* clear key value */
  70. } __packed;
  71. /*
  72. * Create a protected key from a clear key value.
  73. */
  74. static int pkey_clr2protkey(u32 keytype,
  75. const struct pkey_clrkey *clrkey,
  76. struct pkey_protkey *protkey)
  77. {
  78. /* mask of available pckmo subfunctions */
  79. static cpacf_mask_t pckmo_functions;
  80. long fc;
  81. int keysize;
  82. u8 paramblock[64];
  83. switch (keytype) {
  84. case PKEY_KEYTYPE_AES_128:
  85. keysize = 16;
  86. fc = CPACF_PCKMO_ENC_AES_128_KEY;
  87. break;
  88. case PKEY_KEYTYPE_AES_192:
  89. keysize = 24;
  90. fc = CPACF_PCKMO_ENC_AES_192_KEY;
  91. break;
  92. case PKEY_KEYTYPE_AES_256:
  93. keysize = 32;
  94. fc = CPACF_PCKMO_ENC_AES_256_KEY;
  95. break;
  96. default:
  97. DEBUG_ERR("%s unknown/unsupported keytype %d\n",
  98. __func__, keytype);
  99. return -EINVAL;
  100. }
  101. /* Did we already check for PCKMO ? */
  102. if (!pckmo_functions.bytes[0]) {
  103. /* no, so check now */
  104. if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
  105. return -ENODEV;
  106. }
  107. /* check for the pckmo subfunction we need now */
  108. if (!cpacf_test_func(&pckmo_functions, fc)) {
  109. DEBUG_ERR("%s pckmo functions not available\n", __func__);
  110. return -ENODEV;
  111. }
  112. /* prepare param block */
  113. memset(paramblock, 0, sizeof(paramblock));
  114. memcpy(paramblock, clrkey->clrkey, keysize);
  115. /* call the pckmo instruction */
  116. cpacf_pckmo(fc, paramblock);
  117. /* copy created protected key */
  118. protkey->type = keytype;
  119. protkey->len = keysize + 32;
  120. memcpy(protkey->protkey, paramblock, keysize + 32);
  121. return 0;
  122. }
  123. /*
  124. * Find card and transform secure key into protected key.
  125. */
  126. static int pkey_skey2pkey(const u8 *key, struct pkey_protkey *pkey)
  127. {
  128. int rc, verify;
  129. u16 cardnr, domain;
  130. struct keytoken_header *hdr = (struct keytoken_header *)key;
  131. zcrypt_wait_api_operational();
  132. /*
  133. * The cca_xxx2protkey call may fail when a card has been
  134. * addressed where the master key was changed after last fetch
  135. * of the mkvp into the cache. Try 3 times: First without verify
  136. * then with verify and last round with verify and old master
  137. * key verification pattern match not ignored.
  138. */
  139. for (verify = 0; verify < 3; verify++) {
  140. rc = cca_findcard(key, &cardnr, &domain, verify);
  141. if (rc < 0)
  142. continue;
  143. if (rc > 0 && verify < 2)
  144. continue;
  145. switch (hdr->version) {
  146. case TOKVER_CCA_AES:
  147. rc = cca_sec2protkey(cardnr, domain,
  148. key, pkey->protkey,
  149. &pkey->len, &pkey->type);
  150. break;
  151. case TOKVER_CCA_VLSC:
  152. rc = cca_cipher2protkey(cardnr, domain,
  153. key, pkey->protkey,
  154. &pkey->len, &pkey->type);
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. if (rc == 0)
  160. break;
  161. }
  162. if (rc)
  163. DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
  164. return rc;
  165. }
  166. /*
  167. * Construct EP11 key with given clear key value.
  168. */
  169. static int pkey_clr2ep11key(const u8 *clrkey, size_t clrkeylen,
  170. u8 *keybuf, size_t *keybuflen)
  171. {
  172. int i, rc;
  173. u16 card, dom;
  174. u32 nr_apqns, *apqns = NULL;
  175. zcrypt_wait_api_operational();
  176. /* build a list of apqns suitable for ep11 keys with cpacf support */
  177. rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
  178. ZCRYPT_CEX7, EP11_API_V, NULL);
  179. if (rc)
  180. goto out;
  181. /* go through the list of apqns and try to bild an ep11 key */
  182. for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
  183. card = apqns[i] >> 16;
  184. dom = apqns[i] & 0xFFFF;
  185. rc = ep11_clr2keyblob(card, dom, clrkeylen * 8,
  186. 0, clrkey, keybuf, keybuflen,
  187. PKEY_TYPE_EP11);
  188. if (rc == 0)
  189. break;
  190. }
  191. out:
  192. kfree(apqns);
  193. if (rc)
  194. DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
  195. return rc;
  196. }
  197. /*
  198. * Find card and transform EP11 secure key into protected key.
  199. */
  200. static int pkey_ep11key2pkey(const u8 *key, struct pkey_protkey *pkey)
  201. {
  202. int i, rc;
  203. u16 card, dom;
  204. u32 nr_apqns, *apqns = NULL;
  205. struct ep11keyblob *kb = (struct ep11keyblob *)key;
  206. zcrypt_wait_api_operational();
  207. /* build a list of apqns suitable for this key */
  208. rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
  209. ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
  210. if (rc)
  211. goto out;
  212. /* go through the list of apqns and try to derive an pkey */
  213. for (rc = -ENODEV, i = 0; i < nr_apqns; i++) {
  214. card = apqns[i] >> 16;
  215. dom = apqns[i] & 0xFFFF;
  216. pkey->len = sizeof(pkey->protkey);
  217. rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
  218. pkey->protkey, &pkey->len, &pkey->type);
  219. if (rc == 0)
  220. break;
  221. }
  222. out:
  223. kfree(apqns);
  224. if (rc)
  225. DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
  226. return rc;
  227. }
  228. /*
  229. * Verify key and give back some info about the key.
  230. */
  231. static int pkey_verifykey(const struct pkey_seckey *seckey,
  232. u16 *pcardnr, u16 *pdomain,
  233. u16 *pkeysize, u32 *pattributes)
  234. {
  235. struct secaeskeytoken *t = (struct secaeskeytoken *)seckey;
  236. u16 cardnr, domain;
  237. int rc;
  238. /* check the secure key for valid AES secure key */
  239. rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *)seckey, 0);
  240. if (rc)
  241. goto out;
  242. if (pattributes)
  243. *pattributes = PKEY_VERIFY_ATTR_AES;
  244. if (pkeysize)
  245. *pkeysize = t->bitsize;
  246. /* try to find a card which can handle this key */
  247. rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
  248. if (rc < 0)
  249. goto out;
  250. if (rc > 0) {
  251. /* key mkvp matches to old master key mkvp */
  252. DEBUG_DBG("%s secure key has old mkvp\n", __func__);
  253. if (pattributes)
  254. *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
  255. rc = 0;
  256. }
  257. if (pcardnr)
  258. *pcardnr = cardnr;
  259. if (pdomain)
  260. *pdomain = domain;
  261. out:
  262. DEBUG_DBG("%s rc=%d\n", __func__, rc);
  263. return rc;
  264. }
  265. /*
  266. * Generate a random protected key
  267. */
  268. static int pkey_genprotkey(u32 keytype, struct pkey_protkey *protkey)
  269. {
  270. struct pkey_clrkey clrkey;
  271. int keysize;
  272. int rc;
  273. switch (keytype) {
  274. case PKEY_KEYTYPE_AES_128:
  275. keysize = 16;
  276. break;
  277. case PKEY_KEYTYPE_AES_192:
  278. keysize = 24;
  279. break;
  280. case PKEY_KEYTYPE_AES_256:
  281. keysize = 32;
  282. break;
  283. default:
  284. DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
  285. keytype);
  286. return -EINVAL;
  287. }
  288. /* generate a dummy random clear key */
  289. get_random_bytes(clrkey.clrkey, keysize);
  290. /* convert it to a dummy protected key */
  291. rc = pkey_clr2protkey(keytype, &clrkey, protkey);
  292. if (rc)
  293. return rc;
  294. /* replace the key part of the protected key with random bytes */
  295. get_random_bytes(protkey->protkey, keysize);
  296. return 0;
  297. }
  298. /*
  299. * Verify if a protected key is still valid
  300. */
  301. static int pkey_verifyprotkey(const struct pkey_protkey *protkey)
  302. {
  303. unsigned long fc;
  304. struct {
  305. u8 iv[AES_BLOCK_SIZE];
  306. u8 key[MAXPROTKEYSIZE];
  307. } param;
  308. u8 null_msg[AES_BLOCK_SIZE];
  309. u8 dest_buf[AES_BLOCK_SIZE];
  310. unsigned int k;
  311. switch (protkey->type) {
  312. case PKEY_KEYTYPE_AES_128:
  313. fc = CPACF_KMC_PAES_128;
  314. break;
  315. case PKEY_KEYTYPE_AES_192:
  316. fc = CPACF_KMC_PAES_192;
  317. break;
  318. case PKEY_KEYTYPE_AES_256:
  319. fc = CPACF_KMC_PAES_256;
  320. break;
  321. default:
  322. DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
  323. protkey->type);
  324. return -EINVAL;
  325. }
  326. memset(null_msg, 0, sizeof(null_msg));
  327. memset(param.iv, 0, sizeof(param.iv));
  328. memcpy(param.key, protkey->protkey, sizeof(param.key));
  329. k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
  330. sizeof(null_msg));
  331. if (k != sizeof(null_msg)) {
  332. DEBUG_ERR("%s protected key is not valid\n", __func__);
  333. return -EKEYREJECTED;
  334. }
  335. return 0;
  336. }
  337. /*
  338. * Transform a non-CCA key token into a protected key
  339. */
  340. static int pkey_nonccatok2pkey(const u8 *key, u32 keylen,
  341. struct pkey_protkey *protkey)
  342. {
  343. int rc = -EINVAL;
  344. u8 *tmpbuf = NULL;
  345. struct keytoken_header *hdr = (struct keytoken_header *)key;
  346. switch (hdr->version) {
  347. case TOKVER_PROTECTED_KEY: {
  348. struct protaeskeytoken *t;
  349. if (keylen != sizeof(struct protaeskeytoken))
  350. goto out;
  351. t = (struct protaeskeytoken *)key;
  352. protkey->len = t->len;
  353. protkey->type = t->keytype;
  354. memcpy(protkey->protkey, t->protkey,
  355. sizeof(protkey->protkey));
  356. rc = pkey_verifyprotkey(protkey);
  357. break;
  358. }
  359. case TOKVER_CLEAR_KEY: {
  360. struct clearaeskeytoken *t;
  361. struct pkey_clrkey ckey;
  362. union u_tmpbuf {
  363. u8 skey[SECKEYBLOBSIZE];
  364. u8 ep11key[MAXEP11AESKEYBLOBSIZE];
  365. };
  366. size_t tmpbuflen = sizeof(union u_tmpbuf);
  367. if (keylen < sizeof(struct clearaeskeytoken))
  368. goto out;
  369. t = (struct clearaeskeytoken *)key;
  370. if (keylen != sizeof(*t) + t->len)
  371. goto out;
  372. if ((t->keytype == PKEY_KEYTYPE_AES_128 && t->len == 16) ||
  373. (t->keytype == PKEY_KEYTYPE_AES_192 && t->len == 24) ||
  374. (t->keytype == PKEY_KEYTYPE_AES_256 && t->len == 32))
  375. memcpy(ckey.clrkey, t->clearkey, t->len);
  376. else
  377. goto out;
  378. /* alloc temp key buffer space */
  379. tmpbuf = kmalloc(tmpbuflen, GFP_ATOMIC);
  380. if (!tmpbuf) {
  381. rc = -ENOMEM;
  382. goto out;
  383. }
  384. /* try direct way with the PCKMO instruction */
  385. rc = pkey_clr2protkey(t->keytype, &ckey, protkey);
  386. if (rc == 0)
  387. break;
  388. /* PCKMO failed, so try the CCA secure key way */
  389. zcrypt_wait_api_operational();
  390. rc = cca_clr2seckey(0xFFFF, 0xFFFF, t->keytype,
  391. ckey.clrkey, tmpbuf);
  392. if (rc == 0)
  393. rc = pkey_skey2pkey(tmpbuf, protkey);
  394. if (rc == 0)
  395. break;
  396. /* if the CCA way also failed, let's try via EP11 */
  397. rc = pkey_clr2ep11key(ckey.clrkey, t->len,
  398. tmpbuf, &tmpbuflen);
  399. if (rc == 0)
  400. rc = pkey_ep11key2pkey(tmpbuf, protkey);
  401. /* now we should really have an protected key */
  402. DEBUG_ERR("%s unable to build protected key from clear",
  403. __func__);
  404. break;
  405. }
  406. case TOKVER_EP11_AES: {
  407. /* check ep11 key for exportable as protected key */
  408. rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
  409. if (rc)
  410. goto out;
  411. rc = pkey_ep11key2pkey(key, protkey);
  412. break;
  413. }
  414. case TOKVER_EP11_AES_WITH_HEADER:
  415. /* check ep11 key with header for exportable as protected key */
  416. rc = ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1);
  417. if (rc)
  418. goto out;
  419. rc = pkey_ep11key2pkey(key + sizeof(struct ep11kblob_header),
  420. protkey);
  421. break;
  422. default:
  423. DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
  424. __func__, hdr->version);
  425. rc = -EINVAL;
  426. }
  427. out:
  428. kfree(tmpbuf);
  429. return rc;
  430. }
  431. /*
  432. * Transform a CCA internal key token into a protected key
  433. */
  434. static int pkey_ccainttok2pkey(const u8 *key, u32 keylen,
  435. struct pkey_protkey *protkey)
  436. {
  437. struct keytoken_header *hdr = (struct keytoken_header *)key;
  438. switch (hdr->version) {
  439. case TOKVER_CCA_AES:
  440. if (keylen != sizeof(struct secaeskeytoken))
  441. return -EINVAL;
  442. break;
  443. case TOKVER_CCA_VLSC:
  444. if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
  445. return -EINVAL;
  446. break;
  447. default:
  448. DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
  449. __func__, hdr->version);
  450. return -EINVAL;
  451. }
  452. return pkey_skey2pkey(key, protkey);
  453. }
  454. /*
  455. * Transform a key blob (of any type) into a protected key
  456. */
  457. int pkey_keyblob2pkey(const u8 *key, u32 keylen,
  458. struct pkey_protkey *protkey)
  459. {
  460. int rc;
  461. struct keytoken_header *hdr = (struct keytoken_header *)key;
  462. if (keylen < sizeof(struct keytoken_header)) {
  463. DEBUG_ERR("%s invalid keylen %d\n", __func__, keylen);
  464. return -EINVAL;
  465. }
  466. switch (hdr->type) {
  467. case TOKTYPE_NON_CCA:
  468. rc = pkey_nonccatok2pkey(key, keylen, protkey);
  469. break;
  470. case TOKTYPE_CCA_INTERNAL:
  471. rc = pkey_ccainttok2pkey(key, keylen, protkey);
  472. break;
  473. default:
  474. DEBUG_ERR("%s unknown/unsupported blob type %d\n",
  475. __func__, hdr->type);
  476. return -EINVAL;
  477. }
  478. DEBUG_DBG("%s rc=%d\n", __func__, rc);
  479. return rc;
  480. }
  481. EXPORT_SYMBOL(pkey_keyblob2pkey);
  482. static int pkey_genseckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
  483. enum pkey_key_type ktype, enum pkey_key_size ksize,
  484. u32 kflags, u8 *keybuf, size_t *keybufsize)
  485. {
  486. int i, card, dom, rc;
  487. /* check for at least one apqn given */
  488. if (!apqns || !nr_apqns)
  489. return -EINVAL;
  490. /* check key type and size */
  491. switch (ktype) {
  492. case PKEY_TYPE_CCA_DATA:
  493. case PKEY_TYPE_CCA_CIPHER:
  494. if (*keybufsize < SECKEYBLOBSIZE)
  495. return -EINVAL;
  496. break;
  497. case PKEY_TYPE_EP11:
  498. if (*keybufsize < MINEP11AESKEYBLOBSIZE)
  499. return -EINVAL;
  500. break;
  501. case PKEY_TYPE_EP11_AES:
  502. if (*keybufsize < (sizeof(struct ep11kblob_header) +
  503. MINEP11AESKEYBLOBSIZE))
  504. return -EINVAL;
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. switch (ksize) {
  510. case PKEY_SIZE_AES_128:
  511. case PKEY_SIZE_AES_192:
  512. case PKEY_SIZE_AES_256:
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. /* simple try all apqns from the list */
  518. for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
  519. card = apqns[i].card;
  520. dom = apqns[i].domain;
  521. if (ktype == PKEY_TYPE_EP11 ||
  522. ktype == PKEY_TYPE_EP11_AES) {
  523. rc = ep11_genaeskey(card, dom, ksize, kflags,
  524. keybuf, keybufsize, ktype);
  525. } else if (ktype == PKEY_TYPE_CCA_DATA) {
  526. rc = cca_genseckey(card, dom, ksize, keybuf);
  527. *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
  528. } else {
  529. /* TOKVER_CCA_VLSC */
  530. rc = cca_gencipherkey(card, dom, ksize, kflags,
  531. keybuf, keybufsize);
  532. }
  533. if (rc == 0)
  534. break;
  535. }
  536. return rc;
  537. }
  538. static int pkey_clr2seckey2(const struct pkey_apqn *apqns, size_t nr_apqns,
  539. enum pkey_key_type ktype, enum pkey_key_size ksize,
  540. u32 kflags, const u8 *clrkey,
  541. u8 *keybuf, size_t *keybufsize)
  542. {
  543. int i, card, dom, rc;
  544. /* check for at least one apqn given */
  545. if (!apqns || !nr_apqns)
  546. return -EINVAL;
  547. /* check key type and size */
  548. switch (ktype) {
  549. case PKEY_TYPE_CCA_DATA:
  550. case PKEY_TYPE_CCA_CIPHER:
  551. if (*keybufsize < SECKEYBLOBSIZE)
  552. return -EINVAL;
  553. break;
  554. case PKEY_TYPE_EP11:
  555. if (*keybufsize < MINEP11AESKEYBLOBSIZE)
  556. return -EINVAL;
  557. break;
  558. case PKEY_TYPE_EP11_AES:
  559. if (*keybufsize < (sizeof(struct ep11kblob_header) +
  560. MINEP11AESKEYBLOBSIZE))
  561. return -EINVAL;
  562. break;
  563. default:
  564. return -EINVAL;
  565. }
  566. switch (ksize) {
  567. case PKEY_SIZE_AES_128:
  568. case PKEY_SIZE_AES_192:
  569. case PKEY_SIZE_AES_256:
  570. break;
  571. default:
  572. return -EINVAL;
  573. }
  574. zcrypt_wait_api_operational();
  575. /* simple try all apqns from the list */
  576. for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
  577. card = apqns[i].card;
  578. dom = apqns[i].domain;
  579. if (ktype == PKEY_TYPE_EP11 ||
  580. ktype == PKEY_TYPE_EP11_AES) {
  581. rc = ep11_clr2keyblob(card, dom, ksize, kflags,
  582. clrkey, keybuf, keybufsize,
  583. ktype);
  584. } else if (ktype == PKEY_TYPE_CCA_DATA) {
  585. rc = cca_clr2seckey(card, dom, ksize,
  586. clrkey, keybuf);
  587. *keybufsize = (rc ? 0 : SECKEYBLOBSIZE);
  588. } else {
  589. /* TOKVER_CCA_VLSC */
  590. rc = cca_clr2cipherkey(card, dom, ksize, kflags,
  591. clrkey, keybuf, keybufsize);
  592. }
  593. if (rc == 0)
  594. break;
  595. }
  596. return rc;
  597. }
  598. static int pkey_verifykey2(const u8 *key, size_t keylen,
  599. u16 *cardnr, u16 *domain,
  600. enum pkey_key_type *ktype,
  601. enum pkey_key_size *ksize, u32 *flags)
  602. {
  603. int rc;
  604. u32 _nr_apqns, *_apqns = NULL;
  605. struct keytoken_header *hdr = (struct keytoken_header *)key;
  606. if (keylen < sizeof(struct keytoken_header))
  607. return -EINVAL;
  608. if (hdr->type == TOKTYPE_CCA_INTERNAL &&
  609. hdr->version == TOKVER_CCA_AES) {
  610. struct secaeskeytoken *t = (struct secaeskeytoken *)key;
  611. rc = cca_check_secaeskeytoken(debug_info, 3, key, 0);
  612. if (rc)
  613. goto out;
  614. if (ktype)
  615. *ktype = PKEY_TYPE_CCA_DATA;
  616. if (ksize)
  617. *ksize = (enum pkey_key_size)t->bitsize;
  618. rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
  619. ZCRYPT_CEX3C, AES_MK_SET, t->mkvp, 0, 1);
  620. if (rc == 0 && flags)
  621. *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
  622. if (rc == -ENODEV) {
  623. rc = cca_findcard2(&_apqns, &_nr_apqns,
  624. *cardnr, *domain,
  625. ZCRYPT_CEX3C, AES_MK_SET,
  626. 0, t->mkvp, 1);
  627. if (rc == 0 && flags)
  628. *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
  629. }
  630. if (rc)
  631. goto out;
  632. *cardnr = ((struct pkey_apqn *)_apqns)->card;
  633. *domain = ((struct pkey_apqn *)_apqns)->domain;
  634. } else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
  635. hdr->version == TOKVER_CCA_VLSC) {
  636. struct cipherkeytoken *t = (struct cipherkeytoken *)key;
  637. rc = cca_check_secaescipherkey(debug_info, 3, key, 0, 1);
  638. if (rc)
  639. goto out;
  640. if (ktype)
  641. *ktype = PKEY_TYPE_CCA_CIPHER;
  642. if (ksize) {
  643. *ksize = PKEY_SIZE_UNKNOWN;
  644. if (!t->plfver && t->wpllen == 512)
  645. *ksize = PKEY_SIZE_AES_128;
  646. else if (!t->plfver && t->wpllen == 576)
  647. *ksize = PKEY_SIZE_AES_192;
  648. else if (!t->plfver && t->wpllen == 640)
  649. *ksize = PKEY_SIZE_AES_256;
  650. }
  651. rc = cca_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
  652. ZCRYPT_CEX6, AES_MK_SET, t->mkvp0, 0, 1);
  653. if (rc == 0 && flags)
  654. *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
  655. if (rc == -ENODEV) {
  656. rc = cca_findcard2(&_apqns, &_nr_apqns,
  657. *cardnr, *domain,
  658. ZCRYPT_CEX6, AES_MK_SET,
  659. 0, t->mkvp0, 1);
  660. if (rc == 0 && flags)
  661. *flags = PKEY_FLAGS_MATCH_ALT_MKVP;
  662. }
  663. if (rc)
  664. goto out;
  665. *cardnr = ((struct pkey_apqn *)_apqns)->card;
  666. *domain = ((struct pkey_apqn *)_apqns)->domain;
  667. } else if (hdr->type == TOKTYPE_NON_CCA &&
  668. hdr->version == TOKVER_EP11_AES) {
  669. struct ep11keyblob *kb = (struct ep11keyblob *)key;
  670. rc = ep11_check_aes_key(debug_info, 3, key, keylen, 1);
  671. if (rc)
  672. goto out;
  673. if (ktype)
  674. *ktype = PKEY_TYPE_EP11;
  675. if (ksize)
  676. *ksize = kb->head.bitlen;
  677. rc = ep11_findcard2(&_apqns, &_nr_apqns, *cardnr, *domain,
  678. ZCRYPT_CEX7, EP11_API_V, kb->wkvp);
  679. if (rc)
  680. goto out;
  681. if (flags)
  682. *flags = PKEY_FLAGS_MATCH_CUR_MKVP;
  683. *cardnr = ((struct pkey_apqn *)_apqns)->card;
  684. *domain = ((struct pkey_apqn *)_apqns)->domain;
  685. } else {
  686. rc = -EINVAL;
  687. }
  688. out:
  689. kfree(_apqns);
  690. return rc;
  691. }
  692. static int pkey_keyblob2pkey2(const struct pkey_apqn *apqns, size_t nr_apqns,
  693. const u8 *key, size_t keylen,
  694. struct pkey_protkey *pkey)
  695. {
  696. int i, card, dom, rc;
  697. struct keytoken_header *hdr = (struct keytoken_header *)key;
  698. /* check for at least one apqn given */
  699. if (!apqns || !nr_apqns)
  700. return -EINVAL;
  701. if (keylen < sizeof(struct keytoken_header))
  702. return -EINVAL;
  703. if (hdr->type == TOKTYPE_CCA_INTERNAL) {
  704. if (hdr->version == TOKVER_CCA_AES) {
  705. if (keylen != sizeof(struct secaeskeytoken))
  706. return -EINVAL;
  707. if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
  708. return -EINVAL;
  709. } else if (hdr->version == TOKVER_CCA_VLSC) {
  710. if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
  711. return -EINVAL;
  712. if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
  713. return -EINVAL;
  714. } else {
  715. DEBUG_ERR("%s unknown CCA internal token version %d\n",
  716. __func__, hdr->version);
  717. return -EINVAL;
  718. }
  719. } else if (hdr->type == TOKTYPE_NON_CCA) {
  720. if (hdr->version == TOKVER_EP11_AES) {
  721. if (keylen < sizeof(struct ep11keyblob))
  722. return -EINVAL;
  723. if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
  724. return -EINVAL;
  725. } else {
  726. return pkey_nonccatok2pkey(key, keylen, pkey);
  727. }
  728. } else {
  729. DEBUG_ERR("%s unknown/unsupported blob type %d\n",
  730. __func__, hdr->type);
  731. return -EINVAL;
  732. }
  733. zcrypt_wait_api_operational();
  734. /* simple try all apqns from the list */
  735. for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
  736. card = apqns[i].card;
  737. dom = apqns[i].domain;
  738. if (hdr->type == TOKTYPE_CCA_INTERNAL &&
  739. hdr->version == TOKVER_CCA_AES) {
  740. rc = cca_sec2protkey(card, dom, key, pkey->protkey,
  741. &pkey->len, &pkey->type);
  742. } else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
  743. hdr->version == TOKVER_CCA_VLSC) {
  744. rc = cca_cipher2protkey(card, dom, key, pkey->protkey,
  745. &pkey->len, &pkey->type);
  746. } else {
  747. /* EP11 AES secure key blob */
  748. struct ep11keyblob *kb = (struct ep11keyblob *)key;
  749. pkey->len = sizeof(pkey->protkey);
  750. rc = ep11_kblob2protkey(card, dom, key, kb->head.len,
  751. pkey->protkey, &pkey->len,
  752. &pkey->type);
  753. }
  754. if (rc == 0)
  755. break;
  756. }
  757. return rc;
  758. }
  759. static int pkey_apqns4key(const u8 *key, size_t keylen, u32 flags,
  760. struct pkey_apqn *apqns, size_t *nr_apqns)
  761. {
  762. int rc;
  763. u32 _nr_apqns, *_apqns = NULL;
  764. struct keytoken_header *hdr = (struct keytoken_header *)key;
  765. if (keylen < sizeof(struct keytoken_header) || flags == 0)
  766. return -EINVAL;
  767. zcrypt_wait_api_operational();
  768. if (hdr->type == TOKTYPE_NON_CCA &&
  769. (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
  770. hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
  771. is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
  772. int minhwtype = 0, api = 0;
  773. struct ep11keyblob *kb = (struct ep11keyblob *)
  774. (key + sizeof(struct ep11kblob_header));
  775. if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
  776. return -EINVAL;
  777. if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
  778. minhwtype = ZCRYPT_CEX7;
  779. api = EP11_API_V;
  780. }
  781. rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  782. minhwtype, api, kb->wkvp);
  783. if (rc)
  784. goto out;
  785. } else if (hdr->type == TOKTYPE_NON_CCA &&
  786. hdr->version == TOKVER_EP11_AES &&
  787. is_ep11_keyblob(key)) {
  788. int minhwtype = 0, api = 0;
  789. struct ep11keyblob *kb = (struct ep11keyblob *)key;
  790. if (flags != PKEY_FLAGS_MATCH_CUR_MKVP)
  791. return -EINVAL;
  792. if (kb->attr & EP11_BLOB_PKEY_EXTRACTABLE) {
  793. minhwtype = ZCRYPT_CEX7;
  794. api = EP11_API_V;
  795. }
  796. rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  797. minhwtype, api, kb->wkvp);
  798. if (rc)
  799. goto out;
  800. } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
  801. int minhwtype = ZCRYPT_CEX3C;
  802. u64 cur_mkvp = 0, old_mkvp = 0;
  803. if (hdr->version == TOKVER_CCA_AES) {
  804. struct secaeskeytoken *t = (struct secaeskeytoken *)key;
  805. if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
  806. cur_mkvp = t->mkvp;
  807. if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
  808. old_mkvp = t->mkvp;
  809. } else if (hdr->version == TOKVER_CCA_VLSC) {
  810. struct cipherkeytoken *t = (struct cipherkeytoken *)key;
  811. minhwtype = ZCRYPT_CEX6;
  812. if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
  813. cur_mkvp = t->mkvp0;
  814. if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
  815. old_mkvp = t->mkvp0;
  816. } else {
  817. /* unknown cca internal token type */
  818. return -EINVAL;
  819. }
  820. rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  821. minhwtype, AES_MK_SET,
  822. cur_mkvp, old_mkvp, 1);
  823. if (rc)
  824. goto out;
  825. } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
  826. u64 cur_mkvp = 0, old_mkvp = 0;
  827. struct eccprivkeytoken *t = (struct eccprivkeytoken *)key;
  828. if (t->secid == 0x20) {
  829. if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
  830. cur_mkvp = t->mkvp;
  831. if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
  832. old_mkvp = t->mkvp;
  833. } else {
  834. /* unknown cca internal 2 token type */
  835. return -EINVAL;
  836. }
  837. rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  838. ZCRYPT_CEX7, APKA_MK_SET,
  839. cur_mkvp, old_mkvp, 1);
  840. if (rc)
  841. goto out;
  842. } else {
  843. return -EINVAL;
  844. }
  845. if (apqns) {
  846. if (*nr_apqns < _nr_apqns)
  847. rc = -ENOSPC;
  848. else
  849. memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
  850. }
  851. *nr_apqns = _nr_apqns;
  852. out:
  853. kfree(_apqns);
  854. return rc;
  855. }
  856. static int pkey_apqns4keytype(enum pkey_key_type ktype,
  857. u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
  858. struct pkey_apqn *apqns, size_t *nr_apqns)
  859. {
  860. int rc;
  861. u32 _nr_apqns, *_apqns = NULL;
  862. zcrypt_wait_api_operational();
  863. if (ktype == PKEY_TYPE_CCA_DATA || ktype == PKEY_TYPE_CCA_CIPHER) {
  864. u64 cur_mkvp = 0, old_mkvp = 0;
  865. int minhwtype = ZCRYPT_CEX3C;
  866. if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
  867. cur_mkvp = *((u64 *)cur_mkvp);
  868. if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
  869. old_mkvp = *((u64 *)alt_mkvp);
  870. if (ktype == PKEY_TYPE_CCA_CIPHER)
  871. minhwtype = ZCRYPT_CEX6;
  872. rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  873. minhwtype, AES_MK_SET,
  874. cur_mkvp, old_mkvp, 1);
  875. if (rc)
  876. goto out;
  877. } else if (ktype == PKEY_TYPE_CCA_ECC) {
  878. u64 cur_mkvp = 0, old_mkvp = 0;
  879. if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
  880. cur_mkvp = *((u64 *)cur_mkvp);
  881. if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
  882. old_mkvp = *((u64 *)alt_mkvp);
  883. rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  884. ZCRYPT_CEX7, APKA_MK_SET,
  885. cur_mkvp, old_mkvp, 1);
  886. if (rc)
  887. goto out;
  888. } else if (ktype == PKEY_TYPE_EP11 ||
  889. ktype == PKEY_TYPE_EP11_AES ||
  890. ktype == PKEY_TYPE_EP11_ECC) {
  891. u8 *wkvp = NULL;
  892. if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
  893. wkvp = cur_mkvp;
  894. rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
  895. ZCRYPT_CEX7, EP11_API_V, wkvp);
  896. if (rc)
  897. goto out;
  898. } else {
  899. return -EINVAL;
  900. }
  901. if (apqns) {
  902. if (*nr_apqns < _nr_apqns)
  903. rc = -ENOSPC;
  904. else
  905. memcpy(apqns, _apqns, _nr_apqns * sizeof(u32));
  906. }
  907. *nr_apqns = _nr_apqns;
  908. out:
  909. kfree(_apqns);
  910. return rc;
  911. }
  912. static int pkey_keyblob2pkey3(const struct pkey_apqn *apqns, size_t nr_apqns,
  913. const u8 *key, size_t keylen, u32 *protkeytype,
  914. u8 *protkey, u32 *protkeylen)
  915. {
  916. int i, card, dom, rc;
  917. struct keytoken_header *hdr = (struct keytoken_header *)key;
  918. /* check for at least one apqn given */
  919. if (!apqns || !nr_apqns)
  920. return -EINVAL;
  921. if (keylen < sizeof(struct keytoken_header))
  922. return -EINVAL;
  923. if (hdr->type == TOKTYPE_NON_CCA &&
  924. hdr->version == TOKVER_EP11_AES_WITH_HEADER &&
  925. is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
  926. /* EP11 AES key blob with header */
  927. if (ep11_check_aes_key_with_hdr(debug_info, 3, key, keylen, 1))
  928. return -EINVAL;
  929. } else if (hdr->type == TOKTYPE_NON_CCA &&
  930. hdr->version == TOKVER_EP11_ECC_WITH_HEADER &&
  931. is_ep11_keyblob(key + sizeof(struct ep11kblob_header))) {
  932. /* EP11 ECC key blob with header */
  933. if (ep11_check_ecc_key_with_hdr(debug_info, 3, key, keylen, 1))
  934. return -EINVAL;
  935. } else if (hdr->type == TOKTYPE_NON_CCA &&
  936. hdr->version == TOKVER_EP11_AES &&
  937. is_ep11_keyblob(key)) {
  938. /* EP11 AES key blob with header in session field */
  939. if (ep11_check_aes_key(debug_info, 3, key, keylen, 1))
  940. return -EINVAL;
  941. } else if (hdr->type == TOKTYPE_CCA_INTERNAL) {
  942. if (hdr->version == TOKVER_CCA_AES) {
  943. /* CCA AES data key */
  944. if (keylen != sizeof(struct secaeskeytoken))
  945. return -EINVAL;
  946. if (cca_check_secaeskeytoken(debug_info, 3, key, 0))
  947. return -EINVAL;
  948. } else if (hdr->version == TOKVER_CCA_VLSC) {
  949. /* CCA AES cipher key */
  950. if (keylen < hdr->len || keylen > MAXCCAVLSCTOKENSIZE)
  951. return -EINVAL;
  952. if (cca_check_secaescipherkey(debug_info, 3, key, 0, 1))
  953. return -EINVAL;
  954. } else {
  955. DEBUG_ERR("%s unknown CCA internal token version %d\n",
  956. __func__, hdr->version);
  957. return -EINVAL;
  958. }
  959. } else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA) {
  960. /* CCA ECC (private) key */
  961. if (keylen < sizeof(struct eccprivkeytoken))
  962. return -EINVAL;
  963. if (cca_check_sececckeytoken(debug_info, 3, key, keylen, 1))
  964. return -EINVAL;
  965. } else if (hdr->type == TOKTYPE_NON_CCA) {
  966. struct pkey_protkey pkey;
  967. rc = pkey_nonccatok2pkey(key, keylen, &pkey);
  968. if (rc)
  969. return rc;
  970. memcpy(protkey, pkey.protkey, pkey.len);
  971. *protkeylen = pkey.len;
  972. *protkeytype = pkey.type;
  973. return 0;
  974. } else {
  975. DEBUG_ERR("%s unknown/unsupported blob type %d\n",
  976. __func__, hdr->type);
  977. return -EINVAL;
  978. }
  979. /* simple try all apqns from the list */
  980. for (rc = -ENODEV, i = 0; rc && i < nr_apqns; i++) {
  981. card = apqns[i].card;
  982. dom = apqns[i].domain;
  983. if (hdr->type == TOKTYPE_NON_CCA &&
  984. (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
  985. hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
  986. is_ep11_keyblob(key + sizeof(struct ep11kblob_header)))
  987. rc = ep11_kblob2protkey(card, dom, key, hdr->len,
  988. protkey, protkeylen, protkeytype);
  989. else if (hdr->type == TOKTYPE_NON_CCA &&
  990. hdr->version == TOKVER_EP11_AES &&
  991. is_ep11_keyblob(key))
  992. rc = ep11_kblob2protkey(card, dom, key, hdr->len,
  993. protkey, protkeylen, protkeytype);
  994. else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
  995. hdr->version == TOKVER_CCA_AES)
  996. rc = cca_sec2protkey(card, dom, key, protkey,
  997. protkeylen, protkeytype);
  998. else if (hdr->type == TOKTYPE_CCA_INTERNAL &&
  999. hdr->version == TOKVER_CCA_VLSC)
  1000. rc = cca_cipher2protkey(card, dom, key, protkey,
  1001. protkeylen, protkeytype);
  1002. else if (hdr->type == TOKTYPE_CCA_INTERNAL_PKA)
  1003. rc = cca_ecc2protkey(card, dom, key, protkey,
  1004. protkeylen, protkeytype);
  1005. else
  1006. return -EINVAL;
  1007. }
  1008. return rc;
  1009. }
  1010. /*
  1011. * File io functions
  1012. */
  1013. static void *_copy_key_from_user(void __user *ukey, size_t keylen)
  1014. {
  1015. if (!ukey || keylen < MINKEYBLOBSIZE || keylen > KEYBLOBBUFSIZE)
  1016. return ERR_PTR(-EINVAL);
  1017. return memdup_user(ukey, keylen);
  1018. }
  1019. static void *_copy_apqns_from_user(void __user *uapqns, size_t nr_apqns)
  1020. {
  1021. if (!uapqns || nr_apqns == 0)
  1022. return NULL;
  1023. return memdup_user(uapqns, nr_apqns * sizeof(struct pkey_apqn));
  1024. }
  1025. static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
  1026. unsigned long arg)
  1027. {
  1028. int rc;
  1029. switch (cmd) {
  1030. case PKEY_GENSECK: {
  1031. struct pkey_genseck __user *ugs = (void __user *)arg;
  1032. struct pkey_genseck kgs;
  1033. if (copy_from_user(&kgs, ugs, sizeof(kgs)))
  1034. return -EFAULT;
  1035. rc = cca_genseckey(kgs.cardnr, kgs.domain,
  1036. kgs.keytype, kgs.seckey.seckey);
  1037. DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
  1038. if (rc)
  1039. break;
  1040. if (copy_to_user(ugs, &kgs, sizeof(kgs)))
  1041. return -EFAULT;
  1042. break;
  1043. }
  1044. case PKEY_CLR2SECK: {
  1045. struct pkey_clr2seck __user *ucs = (void __user *)arg;
  1046. struct pkey_clr2seck kcs;
  1047. if (copy_from_user(&kcs, ucs, sizeof(kcs)))
  1048. return -EFAULT;
  1049. rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
  1050. kcs.clrkey.clrkey, kcs.seckey.seckey);
  1051. DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
  1052. if (rc)
  1053. break;
  1054. if (copy_to_user(ucs, &kcs, sizeof(kcs)))
  1055. return -EFAULT;
  1056. memzero_explicit(&kcs, sizeof(kcs));
  1057. break;
  1058. }
  1059. case PKEY_SEC2PROTK: {
  1060. struct pkey_sec2protk __user *usp = (void __user *)arg;
  1061. struct pkey_sec2protk ksp;
  1062. if (copy_from_user(&ksp, usp, sizeof(ksp)))
  1063. return -EFAULT;
  1064. rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
  1065. ksp.seckey.seckey, ksp.protkey.protkey,
  1066. &ksp.protkey.len, &ksp.protkey.type);
  1067. DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
  1068. if (rc)
  1069. break;
  1070. if (copy_to_user(usp, &ksp, sizeof(ksp)))
  1071. return -EFAULT;
  1072. break;
  1073. }
  1074. case PKEY_CLR2PROTK: {
  1075. struct pkey_clr2protk __user *ucp = (void __user *)arg;
  1076. struct pkey_clr2protk kcp;
  1077. if (copy_from_user(&kcp, ucp, sizeof(kcp)))
  1078. return -EFAULT;
  1079. rc = pkey_clr2protkey(kcp.keytype,
  1080. &kcp.clrkey, &kcp.protkey);
  1081. DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
  1082. if (rc)
  1083. break;
  1084. if (copy_to_user(ucp, &kcp, sizeof(kcp)))
  1085. return -EFAULT;
  1086. memzero_explicit(&kcp, sizeof(kcp));
  1087. break;
  1088. }
  1089. case PKEY_FINDCARD: {
  1090. struct pkey_findcard __user *ufc = (void __user *)arg;
  1091. struct pkey_findcard kfc;
  1092. if (copy_from_user(&kfc, ufc, sizeof(kfc)))
  1093. return -EFAULT;
  1094. rc = cca_findcard(kfc.seckey.seckey,
  1095. &kfc.cardnr, &kfc.domain, 1);
  1096. DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
  1097. if (rc < 0)
  1098. break;
  1099. if (copy_to_user(ufc, &kfc, sizeof(kfc)))
  1100. return -EFAULT;
  1101. break;
  1102. }
  1103. case PKEY_SKEY2PKEY: {
  1104. struct pkey_skey2pkey __user *usp = (void __user *)arg;
  1105. struct pkey_skey2pkey ksp;
  1106. if (copy_from_user(&ksp, usp, sizeof(ksp)))
  1107. return -EFAULT;
  1108. rc = pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey);
  1109. DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
  1110. if (rc)
  1111. break;
  1112. if (copy_to_user(usp, &ksp, sizeof(ksp)))
  1113. return -EFAULT;
  1114. break;
  1115. }
  1116. case PKEY_VERIFYKEY: {
  1117. struct pkey_verifykey __user *uvk = (void __user *)arg;
  1118. struct pkey_verifykey kvk;
  1119. if (copy_from_user(&kvk, uvk, sizeof(kvk)))
  1120. return -EFAULT;
  1121. rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
  1122. &kvk.keysize, &kvk.attributes);
  1123. DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
  1124. if (rc)
  1125. break;
  1126. if (copy_to_user(uvk, &kvk, sizeof(kvk)))
  1127. return -EFAULT;
  1128. break;
  1129. }
  1130. case PKEY_GENPROTK: {
  1131. struct pkey_genprotk __user *ugp = (void __user *)arg;
  1132. struct pkey_genprotk kgp;
  1133. if (copy_from_user(&kgp, ugp, sizeof(kgp)))
  1134. return -EFAULT;
  1135. rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
  1136. DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
  1137. if (rc)
  1138. break;
  1139. if (copy_to_user(ugp, &kgp, sizeof(kgp)))
  1140. return -EFAULT;
  1141. break;
  1142. }
  1143. case PKEY_VERIFYPROTK: {
  1144. struct pkey_verifyprotk __user *uvp = (void __user *)arg;
  1145. struct pkey_verifyprotk kvp;
  1146. if (copy_from_user(&kvp, uvp, sizeof(kvp)))
  1147. return -EFAULT;
  1148. rc = pkey_verifyprotkey(&kvp.protkey);
  1149. DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
  1150. break;
  1151. }
  1152. case PKEY_KBLOB2PROTK: {
  1153. struct pkey_kblob2pkey __user *utp = (void __user *)arg;
  1154. struct pkey_kblob2pkey ktp;
  1155. u8 *kkey;
  1156. if (copy_from_user(&ktp, utp, sizeof(ktp)))
  1157. return -EFAULT;
  1158. kkey = _copy_key_from_user(ktp.key, ktp.keylen);
  1159. if (IS_ERR(kkey))
  1160. return PTR_ERR(kkey);
  1161. rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
  1162. DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
  1163. memzero_explicit(kkey, ktp.keylen);
  1164. kfree(kkey);
  1165. if (rc)
  1166. break;
  1167. if (copy_to_user(utp, &ktp, sizeof(ktp)))
  1168. return -EFAULT;
  1169. break;
  1170. }
  1171. case PKEY_GENSECK2: {
  1172. struct pkey_genseck2 __user *ugs = (void __user *)arg;
  1173. struct pkey_genseck2 kgs;
  1174. struct pkey_apqn *apqns;
  1175. size_t klen = KEYBLOBBUFSIZE;
  1176. u8 *kkey;
  1177. if (copy_from_user(&kgs, ugs, sizeof(kgs)))
  1178. return -EFAULT;
  1179. apqns = _copy_apqns_from_user(kgs.apqns, kgs.apqn_entries);
  1180. if (IS_ERR(apqns))
  1181. return PTR_ERR(apqns);
  1182. kkey = kzalloc(klen, GFP_KERNEL);
  1183. if (!kkey) {
  1184. kfree(apqns);
  1185. return -ENOMEM;
  1186. }
  1187. rc = pkey_genseckey2(apqns, kgs.apqn_entries,
  1188. kgs.type, kgs.size, kgs.keygenflags,
  1189. kkey, &klen);
  1190. DEBUG_DBG("%s pkey_genseckey2()=%d\n", __func__, rc);
  1191. kfree(apqns);
  1192. if (rc) {
  1193. kfree(kkey);
  1194. break;
  1195. }
  1196. if (kgs.key) {
  1197. if (kgs.keylen < klen) {
  1198. kfree(kkey);
  1199. return -EINVAL;
  1200. }
  1201. if (copy_to_user(kgs.key, kkey, klen)) {
  1202. kfree(kkey);
  1203. return -EFAULT;
  1204. }
  1205. }
  1206. kgs.keylen = klen;
  1207. if (copy_to_user(ugs, &kgs, sizeof(kgs)))
  1208. rc = -EFAULT;
  1209. kfree(kkey);
  1210. break;
  1211. }
  1212. case PKEY_CLR2SECK2: {
  1213. struct pkey_clr2seck2 __user *ucs = (void __user *)arg;
  1214. struct pkey_clr2seck2 kcs;
  1215. struct pkey_apqn *apqns;
  1216. size_t klen = KEYBLOBBUFSIZE;
  1217. u8 *kkey;
  1218. if (copy_from_user(&kcs, ucs, sizeof(kcs)))
  1219. return -EFAULT;
  1220. apqns = _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries);
  1221. if (IS_ERR(apqns))
  1222. return PTR_ERR(apqns);
  1223. kkey = kzalloc(klen, GFP_KERNEL);
  1224. if (!kkey) {
  1225. kfree(apqns);
  1226. return -ENOMEM;
  1227. }
  1228. rc = pkey_clr2seckey2(apqns, kcs.apqn_entries,
  1229. kcs.type, kcs.size, kcs.keygenflags,
  1230. kcs.clrkey.clrkey, kkey, &klen);
  1231. DEBUG_DBG("%s pkey_clr2seckey2()=%d\n", __func__, rc);
  1232. kfree(apqns);
  1233. if (rc) {
  1234. kfree(kkey);
  1235. break;
  1236. }
  1237. if (kcs.key) {
  1238. if (kcs.keylen < klen) {
  1239. kfree(kkey);
  1240. return -EINVAL;
  1241. }
  1242. if (copy_to_user(kcs.key, kkey, klen)) {
  1243. kfree(kkey);
  1244. return -EFAULT;
  1245. }
  1246. }
  1247. kcs.keylen = klen;
  1248. if (copy_to_user(ucs, &kcs, sizeof(kcs)))
  1249. rc = -EFAULT;
  1250. memzero_explicit(&kcs, sizeof(kcs));
  1251. kfree(kkey);
  1252. break;
  1253. }
  1254. case PKEY_VERIFYKEY2: {
  1255. struct pkey_verifykey2 __user *uvk = (void __user *)arg;
  1256. struct pkey_verifykey2 kvk;
  1257. u8 *kkey;
  1258. if (copy_from_user(&kvk, uvk, sizeof(kvk)))
  1259. return -EFAULT;
  1260. kkey = _copy_key_from_user(kvk.key, kvk.keylen);
  1261. if (IS_ERR(kkey))
  1262. return PTR_ERR(kkey);
  1263. rc = pkey_verifykey2(kkey, kvk.keylen,
  1264. &kvk.cardnr, &kvk.domain,
  1265. &kvk.type, &kvk.size, &kvk.flags);
  1266. DEBUG_DBG("%s pkey_verifykey2()=%d\n", __func__, rc);
  1267. kfree(kkey);
  1268. if (rc)
  1269. break;
  1270. if (copy_to_user(uvk, &kvk, sizeof(kvk)))
  1271. return -EFAULT;
  1272. break;
  1273. }
  1274. case PKEY_KBLOB2PROTK2: {
  1275. struct pkey_kblob2pkey2 __user *utp = (void __user *)arg;
  1276. struct pkey_kblob2pkey2 ktp;
  1277. struct pkey_apqn *apqns = NULL;
  1278. u8 *kkey;
  1279. if (copy_from_user(&ktp, utp, sizeof(ktp)))
  1280. return -EFAULT;
  1281. apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
  1282. if (IS_ERR(apqns))
  1283. return PTR_ERR(apqns);
  1284. kkey = _copy_key_from_user(ktp.key, ktp.keylen);
  1285. if (IS_ERR(kkey)) {
  1286. kfree(apqns);
  1287. return PTR_ERR(kkey);
  1288. }
  1289. rc = pkey_keyblob2pkey2(apqns, ktp.apqn_entries,
  1290. kkey, ktp.keylen, &ktp.protkey);
  1291. DEBUG_DBG("%s pkey_keyblob2pkey2()=%d\n", __func__, rc);
  1292. kfree(apqns);
  1293. memzero_explicit(kkey, ktp.keylen);
  1294. kfree(kkey);
  1295. if (rc)
  1296. break;
  1297. if (copy_to_user(utp, &ktp, sizeof(ktp)))
  1298. return -EFAULT;
  1299. break;
  1300. }
  1301. case PKEY_APQNS4K: {
  1302. struct pkey_apqns4key __user *uak = (void __user *)arg;
  1303. struct pkey_apqns4key kak;
  1304. struct pkey_apqn *apqns = NULL;
  1305. size_t nr_apqns, len;
  1306. u8 *kkey;
  1307. if (copy_from_user(&kak, uak, sizeof(kak)))
  1308. return -EFAULT;
  1309. nr_apqns = kak.apqn_entries;
  1310. if (nr_apqns) {
  1311. apqns = kmalloc_array(nr_apqns,
  1312. sizeof(struct pkey_apqn),
  1313. GFP_KERNEL);
  1314. if (!apqns)
  1315. return -ENOMEM;
  1316. }
  1317. kkey = _copy_key_from_user(kak.key, kak.keylen);
  1318. if (IS_ERR(kkey)) {
  1319. kfree(apqns);
  1320. return PTR_ERR(kkey);
  1321. }
  1322. rc = pkey_apqns4key(kkey, kak.keylen, kak.flags,
  1323. apqns, &nr_apqns);
  1324. DEBUG_DBG("%s pkey_apqns4key()=%d\n", __func__, rc);
  1325. kfree(kkey);
  1326. if (rc && rc != -ENOSPC) {
  1327. kfree(apqns);
  1328. break;
  1329. }
  1330. if (!rc && kak.apqns) {
  1331. if (nr_apqns > kak.apqn_entries) {
  1332. kfree(apqns);
  1333. return -EINVAL;
  1334. }
  1335. len = nr_apqns * sizeof(struct pkey_apqn);
  1336. if (len) {
  1337. if (copy_to_user(kak.apqns, apqns, len)) {
  1338. kfree(apqns);
  1339. return -EFAULT;
  1340. }
  1341. }
  1342. }
  1343. kak.apqn_entries = nr_apqns;
  1344. if (copy_to_user(uak, &kak, sizeof(kak)))
  1345. rc = -EFAULT;
  1346. kfree(apqns);
  1347. break;
  1348. }
  1349. case PKEY_APQNS4KT: {
  1350. struct pkey_apqns4keytype __user *uat = (void __user *)arg;
  1351. struct pkey_apqns4keytype kat;
  1352. struct pkey_apqn *apqns = NULL;
  1353. size_t nr_apqns, len;
  1354. if (copy_from_user(&kat, uat, sizeof(kat)))
  1355. return -EFAULT;
  1356. nr_apqns = kat.apqn_entries;
  1357. if (nr_apqns) {
  1358. apqns = kmalloc_array(nr_apqns,
  1359. sizeof(struct pkey_apqn),
  1360. GFP_KERNEL);
  1361. if (!apqns)
  1362. return -ENOMEM;
  1363. }
  1364. rc = pkey_apqns4keytype(kat.type, kat.cur_mkvp, kat.alt_mkvp,
  1365. kat.flags, apqns, &nr_apqns);
  1366. DEBUG_DBG("%s pkey_apqns4keytype()=%d\n", __func__, rc);
  1367. if (rc && rc != -ENOSPC) {
  1368. kfree(apqns);
  1369. break;
  1370. }
  1371. if (!rc && kat.apqns) {
  1372. if (nr_apqns > kat.apqn_entries) {
  1373. kfree(apqns);
  1374. return -EINVAL;
  1375. }
  1376. len = nr_apqns * sizeof(struct pkey_apqn);
  1377. if (len) {
  1378. if (copy_to_user(kat.apqns, apqns, len)) {
  1379. kfree(apqns);
  1380. return -EFAULT;
  1381. }
  1382. }
  1383. }
  1384. kat.apqn_entries = nr_apqns;
  1385. if (copy_to_user(uat, &kat, sizeof(kat)))
  1386. rc = -EFAULT;
  1387. kfree(apqns);
  1388. break;
  1389. }
  1390. case PKEY_KBLOB2PROTK3: {
  1391. struct pkey_kblob2pkey3 __user *utp = (void __user *)arg;
  1392. struct pkey_kblob2pkey3 ktp;
  1393. struct pkey_apqn *apqns = NULL;
  1394. u32 protkeylen = PROTKEYBLOBBUFSIZE;
  1395. u8 *kkey, *protkey;
  1396. if (copy_from_user(&ktp, utp, sizeof(ktp)))
  1397. return -EFAULT;
  1398. apqns = _copy_apqns_from_user(ktp.apqns, ktp.apqn_entries);
  1399. if (IS_ERR(apqns))
  1400. return PTR_ERR(apqns);
  1401. kkey = _copy_key_from_user(ktp.key, ktp.keylen);
  1402. if (IS_ERR(kkey)) {
  1403. kfree(apqns);
  1404. return PTR_ERR(kkey);
  1405. }
  1406. protkey = kmalloc(protkeylen, GFP_KERNEL);
  1407. if (!protkey) {
  1408. kfree(apqns);
  1409. kfree(kkey);
  1410. return -ENOMEM;
  1411. }
  1412. rc = pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey,
  1413. ktp.keylen, &ktp.pkeytype,
  1414. protkey, &protkeylen);
  1415. DEBUG_DBG("%s pkey_keyblob2pkey3()=%d\n", __func__, rc);
  1416. kfree(apqns);
  1417. memzero_explicit(kkey, ktp.keylen);
  1418. kfree(kkey);
  1419. if (rc) {
  1420. kfree(protkey);
  1421. break;
  1422. }
  1423. if (ktp.pkey && ktp.pkeylen) {
  1424. if (protkeylen > ktp.pkeylen) {
  1425. kfree(protkey);
  1426. return -EINVAL;
  1427. }
  1428. if (copy_to_user(ktp.pkey, protkey, protkeylen)) {
  1429. kfree(protkey);
  1430. return -EFAULT;
  1431. }
  1432. }
  1433. kfree(protkey);
  1434. ktp.pkeylen = protkeylen;
  1435. if (copy_to_user(utp, &ktp, sizeof(ktp)))
  1436. return -EFAULT;
  1437. break;
  1438. }
  1439. default:
  1440. /* unknown/unsupported ioctl cmd */
  1441. return -ENOTTY;
  1442. }
  1443. return rc;
  1444. }
  1445. /*
  1446. * Sysfs and file io operations
  1447. */
  1448. /*
  1449. * Sysfs attribute read function for all protected key binary attributes.
  1450. * The implementation can not deal with partial reads, because a new random
  1451. * protected key blob is generated with each read. In case of partial reads
  1452. * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
  1453. */
  1454. static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
  1455. loff_t off, size_t count)
  1456. {
  1457. struct protaeskeytoken protkeytoken;
  1458. struct pkey_protkey protkey;
  1459. int rc;
  1460. if (off != 0 || count < sizeof(protkeytoken))
  1461. return -EINVAL;
  1462. if (is_xts)
  1463. if (count < 2 * sizeof(protkeytoken))
  1464. return -EINVAL;
  1465. memset(&protkeytoken, 0, sizeof(protkeytoken));
  1466. protkeytoken.type = TOKTYPE_NON_CCA;
  1467. protkeytoken.version = TOKVER_PROTECTED_KEY;
  1468. protkeytoken.keytype = keytype;
  1469. rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
  1470. if (rc)
  1471. return rc;
  1472. protkeytoken.len = protkey.len;
  1473. memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
  1474. memcpy(buf, &protkeytoken, sizeof(protkeytoken));
  1475. if (is_xts) {
  1476. rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
  1477. if (rc)
  1478. return rc;
  1479. protkeytoken.len = protkey.len;
  1480. memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
  1481. memcpy(buf + sizeof(protkeytoken), &protkeytoken,
  1482. sizeof(protkeytoken));
  1483. return 2 * sizeof(protkeytoken);
  1484. }
  1485. return sizeof(protkeytoken);
  1486. }
  1487. static ssize_t protkey_aes_128_read(struct file *filp,
  1488. struct kobject *kobj,
  1489. struct bin_attribute *attr,
  1490. char *buf, loff_t off,
  1491. size_t count)
  1492. {
  1493. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
  1494. off, count);
  1495. }
  1496. static ssize_t protkey_aes_192_read(struct file *filp,
  1497. struct kobject *kobj,
  1498. struct bin_attribute *attr,
  1499. char *buf, loff_t off,
  1500. size_t count)
  1501. {
  1502. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
  1503. off, count);
  1504. }
  1505. static ssize_t protkey_aes_256_read(struct file *filp,
  1506. struct kobject *kobj,
  1507. struct bin_attribute *attr,
  1508. char *buf, loff_t off,
  1509. size_t count)
  1510. {
  1511. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
  1512. off, count);
  1513. }
  1514. static ssize_t protkey_aes_128_xts_read(struct file *filp,
  1515. struct kobject *kobj,
  1516. struct bin_attribute *attr,
  1517. char *buf, loff_t off,
  1518. size_t count)
  1519. {
  1520. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
  1521. off, count);
  1522. }
  1523. static ssize_t protkey_aes_256_xts_read(struct file *filp,
  1524. struct kobject *kobj,
  1525. struct bin_attribute *attr,
  1526. char *buf, loff_t off,
  1527. size_t count)
  1528. {
  1529. return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
  1530. off, count);
  1531. }
  1532. static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
  1533. static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
  1534. static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
  1535. static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
  1536. static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
  1537. static struct bin_attribute *protkey_attrs[] = {
  1538. &bin_attr_protkey_aes_128,
  1539. &bin_attr_protkey_aes_192,
  1540. &bin_attr_protkey_aes_256,
  1541. &bin_attr_protkey_aes_128_xts,
  1542. &bin_attr_protkey_aes_256_xts,
  1543. NULL
  1544. };
  1545. static struct attribute_group protkey_attr_group = {
  1546. .name = "protkey",
  1547. .bin_attrs = protkey_attrs,
  1548. };
  1549. /*
  1550. * Sysfs attribute read function for all secure key ccadata binary attributes.
  1551. * The implementation can not deal with partial reads, because a new random
  1552. * protected key blob is generated with each read. In case of partial reads
  1553. * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
  1554. */
  1555. static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
  1556. loff_t off, size_t count)
  1557. {
  1558. int rc;
  1559. struct pkey_seckey *seckey = (struct pkey_seckey *)buf;
  1560. if (off != 0 || count < sizeof(struct secaeskeytoken))
  1561. return -EINVAL;
  1562. if (is_xts)
  1563. if (count < 2 * sizeof(struct secaeskeytoken))
  1564. return -EINVAL;
  1565. rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
  1566. if (rc)
  1567. return rc;
  1568. if (is_xts) {
  1569. seckey++;
  1570. rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
  1571. if (rc)
  1572. return rc;
  1573. return 2 * sizeof(struct secaeskeytoken);
  1574. }
  1575. return sizeof(struct secaeskeytoken);
  1576. }
  1577. static ssize_t ccadata_aes_128_read(struct file *filp,
  1578. struct kobject *kobj,
  1579. struct bin_attribute *attr,
  1580. char *buf, loff_t off,
  1581. size_t count)
  1582. {
  1583. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
  1584. off, count);
  1585. }
  1586. static ssize_t ccadata_aes_192_read(struct file *filp,
  1587. struct kobject *kobj,
  1588. struct bin_attribute *attr,
  1589. char *buf, loff_t off,
  1590. size_t count)
  1591. {
  1592. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
  1593. off, count);
  1594. }
  1595. static ssize_t ccadata_aes_256_read(struct file *filp,
  1596. struct kobject *kobj,
  1597. struct bin_attribute *attr,
  1598. char *buf, loff_t off,
  1599. size_t count)
  1600. {
  1601. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
  1602. off, count);
  1603. }
  1604. static ssize_t ccadata_aes_128_xts_read(struct file *filp,
  1605. struct kobject *kobj,
  1606. struct bin_attribute *attr,
  1607. char *buf, loff_t off,
  1608. size_t count)
  1609. {
  1610. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
  1611. off, count);
  1612. }
  1613. static ssize_t ccadata_aes_256_xts_read(struct file *filp,
  1614. struct kobject *kobj,
  1615. struct bin_attribute *attr,
  1616. char *buf, loff_t off,
  1617. size_t count)
  1618. {
  1619. return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
  1620. off, count);
  1621. }
  1622. static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
  1623. static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
  1624. static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
  1625. static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
  1626. static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
  1627. static struct bin_attribute *ccadata_attrs[] = {
  1628. &bin_attr_ccadata_aes_128,
  1629. &bin_attr_ccadata_aes_192,
  1630. &bin_attr_ccadata_aes_256,
  1631. &bin_attr_ccadata_aes_128_xts,
  1632. &bin_attr_ccadata_aes_256_xts,
  1633. NULL
  1634. };
  1635. static struct attribute_group ccadata_attr_group = {
  1636. .name = "ccadata",
  1637. .bin_attrs = ccadata_attrs,
  1638. };
  1639. #define CCACIPHERTOKENSIZE (sizeof(struct cipherkeytoken) + 80)
  1640. /*
  1641. * Sysfs attribute read function for all secure key ccacipher binary attributes.
  1642. * The implementation can not deal with partial reads, because a new random
  1643. * secure key blob is generated with each read. In case of partial reads
  1644. * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
  1645. */
  1646. static ssize_t pkey_ccacipher_aes_attr_read(enum pkey_key_size keybits,
  1647. bool is_xts, char *buf, loff_t off,
  1648. size_t count)
  1649. {
  1650. int i, rc, card, dom;
  1651. u32 nr_apqns, *apqns = NULL;
  1652. size_t keysize = CCACIPHERTOKENSIZE;
  1653. if (off != 0 || count < CCACIPHERTOKENSIZE)
  1654. return -EINVAL;
  1655. if (is_xts)
  1656. if (count < 2 * CCACIPHERTOKENSIZE)
  1657. return -EINVAL;
  1658. /* build a list of apqns able to generate an cipher key */
  1659. rc = cca_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
  1660. ZCRYPT_CEX6, 0, 0, 0, 0);
  1661. if (rc)
  1662. return rc;
  1663. memset(buf, 0, is_xts ? 2 * keysize : keysize);
  1664. /* simple try all apqns from the list */
  1665. for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
  1666. card = apqns[i] >> 16;
  1667. dom = apqns[i] & 0xFFFF;
  1668. rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
  1669. if (rc == 0)
  1670. break;
  1671. }
  1672. if (rc)
  1673. return rc;
  1674. if (is_xts) {
  1675. keysize = CCACIPHERTOKENSIZE;
  1676. buf += CCACIPHERTOKENSIZE;
  1677. rc = cca_gencipherkey(card, dom, keybits, 0, buf, &keysize);
  1678. if (rc == 0)
  1679. return 2 * CCACIPHERTOKENSIZE;
  1680. }
  1681. return CCACIPHERTOKENSIZE;
  1682. }
  1683. static ssize_t ccacipher_aes_128_read(struct file *filp,
  1684. struct kobject *kobj,
  1685. struct bin_attribute *attr,
  1686. char *buf, loff_t off,
  1687. size_t count)
  1688. {
  1689. return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
  1690. off, count);
  1691. }
  1692. static ssize_t ccacipher_aes_192_read(struct file *filp,
  1693. struct kobject *kobj,
  1694. struct bin_attribute *attr,
  1695. char *buf, loff_t off,
  1696. size_t count)
  1697. {
  1698. return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
  1699. off, count);
  1700. }
  1701. static ssize_t ccacipher_aes_256_read(struct file *filp,
  1702. struct kobject *kobj,
  1703. struct bin_attribute *attr,
  1704. char *buf, loff_t off,
  1705. size_t count)
  1706. {
  1707. return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
  1708. off, count);
  1709. }
  1710. static ssize_t ccacipher_aes_128_xts_read(struct file *filp,
  1711. struct kobject *kobj,
  1712. struct bin_attribute *attr,
  1713. char *buf, loff_t off,
  1714. size_t count)
  1715. {
  1716. return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
  1717. off, count);
  1718. }
  1719. static ssize_t ccacipher_aes_256_xts_read(struct file *filp,
  1720. struct kobject *kobj,
  1721. struct bin_attribute *attr,
  1722. char *buf, loff_t off,
  1723. size_t count)
  1724. {
  1725. return pkey_ccacipher_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
  1726. off, count);
  1727. }
  1728. static BIN_ATTR_RO(ccacipher_aes_128, CCACIPHERTOKENSIZE);
  1729. static BIN_ATTR_RO(ccacipher_aes_192, CCACIPHERTOKENSIZE);
  1730. static BIN_ATTR_RO(ccacipher_aes_256, CCACIPHERTOKENSIZE);
  1731. static BIN_ATTR_RO(ccacipher_aes_128_xts, 2 * CCACIPHERTOKENSIZE);
  1732. static BIN_ATTR_RO(ccacipher_aes_256_xts, 2 * CCACIPHERTOKENSIZE);
  1733. static struct bin_attribute *ccacipher_attrs[] = {
  1734. &bin_attr_ccacipher_aes_128,
  1735. &bin_attr_ccacipher_aes_192,
  1736. &bin_attr_ccacipher_aes_256,
  1737. &bin_attr_ccacipher_aes_128_xts,
  1738. &bin_attr_ccacipher_aes_256_xts,
  1739. NULL
  1740. };
  1741. static struct attribute_group ccacipher_attr_group = {
  1742. .name = "ccacipher",
  1743. .bin_attrs = ccacipher_attrs,
  1744. };
  1745. /*
  1746. * Sysfs attribute read function for all ep11 aes key binary attributes.
  1747. * The implementation can not deal with partial reads, because a new random
  1748. * secure key blob is generated with each read. In case of partial reads
  1749. * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
  1750. * This function and the sysfs attributes using it provide EP11 key blobs
  1751. * padded to the upper limit of MAXEP11AESKEYBLOBSIZE which is currently
  1752. * 336 bytes.
  1753. */
  1754. static ssize_t pkey_ep11_aes_attr_read(enum pkey_key_size keybits,
  1755. bool is_xts, char *buf, loff_t off,
  1756. size_t count)
  1757. {
  1758. int i, rc, card, dom;
  1759. u32 nr_apqns, *apqns = NULL;
  1760. size_t keysize = MAXEP11AESKEYBLOBSIZE;
  1761. if (off != 0 || count < MAXEP11AESKEYBLOBSIZE)
  1762. return -EINVAL;
  1763. if (is_xts)
  1764. if (count < 2 * MAXEP11AESKEYBLOBSIZE)
  1765. return -EINVAL;
  1766. /* build a list of apqns able to generate an cipher key */
  1767. rc = ep11_findcard2(&apqns, &nr_apqns, 0xFFFF, 0xFFFF,
  1768. ZCRYPT_CEX7, EP11_API_V, NULL);
  1769. if (rc)
  1770. return rc;
  1771. memset(buf, 0, is_xts ? 2 * keysize : keysize);
  1772. /* simple try all apqns from the list */
  1773. for (i = 0, rc = -ENODEV; i < nr_apqns; i++) {
  1774. card = apqns[i] >> 16;
  1775. dom = apqns[i] & 0xFFFF;
  1776. rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize,
  1777. PKEY_TYPE_EP11_AES);
  1778. if (rc == 0)
  1779. break;
  1780. }
  1781. if (rc)
  1782. return rc;
  1783. if (is_xts) {
  1784. keysize = MAXEP11AESKEYBLOBSIZE;
  1785. buf += MAXEP11AESKEYBLOBSIZE;
  1786. rc = ep11_genaeskey(card, dom, keybits, 0, buf, &keysize,
  1787. PKEY_TYPE_EP11_AES);
  1788. if (rc == 0)
  1789. return 2 * MAXEP11AESKEYBLOBSIZE;
  1790. }
  1791. return MAXEP11AESKEYBLOBSIZE;
  1792. }
  1793. static ssize_t ep11_aes_128_read(struct file *filp,
  1794. struct kobject *kobj,
  1795. struct bin_attribute *attr,
  1796. char *buf, loff_t off,
  1797. size_t count)
  1798. {
  1799. return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, false, buf,
  1800. off, count);
  1801. }
  1802. static ssize_t ep11_aes_192_read(struct file *filp,
  1803. struct kobject *kobj,
  1804. struct bin_attribute *attr,
  1805. char *buf, loff_t off,
  1806. size_t count)
  1807. {
  1808. return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_192, false, buf,
  1809. off, count);
  1810. }
  1811. static ssize_t ep11_aes_256_read(struct file *filp,
  1812. struct kobject *kobj,
  1813. struct bin_attribute *attr,
  1814. char *buf, loff_t off,
  1815. size_t count)
  1816. {
  1817. return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, false, buf,
  1818. off, count);
  1819. }
  1820. static ssize_t ep11_aes_128_xts_read(struct file *filp,
  1821. struct kobject *kobj,
  1822. struct bin_attribute *attr,
  1823. char *buf, loff_t off,
  1824. size_t count)
  1825. {
  1826. return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_128, true, buf,
  1827. off, count);
  1828. }
  1829. static ssize_t ep11_aes_256_xts_read(struct file *filp,
  1830. struct kobject *kobj,
  1831. struct bin_attribute *attr,
  1832. char *buf, loff_t off,
  1833. size_t count)
  1834. {
  1835. return pkey_ep11_aes_attr_read(PKEY_SIZE_AES_256, true, buf,
  1836. off, count);
  1837. }
  1838. static BIN_ATTR_RO(ep11_aes_128, MAXEP11AESKEYBLOBSIZE);
  1839. static BIN_ATTR_RO(ep11_aes_192, MAXEP11AESKEYBLOBSIZE);
  1840. static BIN_ATTR_RO(ep11_aes_256, MAXEP11AESKEYBLOBSIZE);
  1841. static BIN_ATTR_RO(ep11_aes_128_xts, 2 * MAXEP11AESKEYBLOBSIZE);
  1842. static BIN_ATTR_RO(ep11_aes_256_xts, 2 * MAXEP11AESKEYBLOBSIZE);
  1843. static struct bin_attribute *ep11_attrs[] = {
  1844. &bin_attr_ep11_aes_128,
  1845. &bin_attr_ep11_aes_192,
  1846. &bin_attr_ep11_aes_256,
  1847. &bin_attr_ep11_aes_128_xts,
  1848. &bin_attr_ep11_aes_256_xts,
  1849. NULL
  1850. };
  1851. static struct attribute_group ep11_attr_group = {
  1852. .name = "ep11",
  1853. .bin_attrs = ep11_attrs,
  1854. };
  1855. static const struct attribute_group *pkey_attr_groups[] = {
  1856. &protkey_attr_group,
  1857. &ccadata_attr_group,
  1858. &ccacipher_attr_group,
  1859. &ep11_attr_group,
  1860. NULL,
  1861. };
  1862. static const struct file_operations pkey_fops = {
  1863. .owner = THIS_MODULE,
  1864. .open = nonseekable_open,
  1865. .llseek = no_llseek,
  1866. .unlocked_ioctl = pkey_unlocked_ioctl,
  1867. };
  1868. static struct miscdevice pkey_dev = {
  1869. .name = "pkey",
  1870. .minor = MISC_DYNAMIC_MINOR,
  1871. .mode = 0666,
  1872. .fops = &pkey_fops,
  1873. .groups = pkey_attr_groups,
  1874. };
  1875. /*
  1876. * Module init
  1877. */
  1878. static int __init pkey_init(void)
  1879. {
  1880. cpacf_mask_t func_mask;
  1881. /*
  1882. * The pckmo instruction should be available - even if we don't
  1883. * actually invoke it. This instruction comes with MSA 3 which
  1884. * is also the minimum level for the kmc instructions which
  1885. * are able to work with protected keys.
  1886. */
  1887. if (!cpacf_query(CPACF_PCKMO, &func_mask))
  1888. return -ENODEV;
  1889. /* check for kmc instructions available */
  1890. if (!cpacf_query(CPACF_KMC, &func_mask))
  1891. return -ENODEV;
  1892. if (!cpacf_test_func(&func_mask, CPACF_KMC_PAES_128) ||
  1893. !cpacf_test_func(&func_mask, CPACF_KMC_PAES_192) ||
  1894. !cpacf_test_func(&func_mask, CPACF_KMC_PAES_256))
  1895. return -ENODEV;
  1896. pkey_debug_init();
  1897. return misc_register(&pkey_dev);
  1898. }
  1899. /*
  1900. * Module exit
  1901. */
  1902. static void __exit pkey_exit(void)
  1903. {
  1904. misc_deregister(&pkey_dev);
  1905. pkey_debug_exit();
  1906. }
  1907. module_cpu_feature_match(S390_CPU_FEATURE_MSA, pkey_init);
  1908. module_exit(pkey_exit);