pkey_api.c 54 KB

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