zcrypt_ep11misc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2019
  4. * Author(s): Harald Freudenberger <[email protected]>
  5. *
  6. * Collection of EP11 misc functions used by zcrypt and pkey
  7. */
  8. #define KMSG_COMPONENT "zcrypt"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/random.h>
  14. #include <asm/zcrypt.h>
  15. #include <asm/pkey.h>
  16. #include <crypto/aes.h>
  17. #include "ap_bus.h"
  18. #include "zcrypt_api.h"
  19. #include "zcrypt_debug.h"
  20. #include "zcrypt_msgtype6.h"
  21. #include "zcrypt_ep11misc.h"
  22. #include "zcrypt_ccamisc.h"
  23. #define DEBUG_DBG(...) ZCRYPT_DBF(DBF_DEBUG, ##__VA_ARGS__)
  24. #define DEBUG_INFO(...) ZCRYPT_DBF(DBF_INFO, ##__VA_ARGS__)
  25. #define DEBUG_WARN(...) ZCRYPT_DBF(DBF_WARN, ##__VA_ARGS__)
  26. #define DEBUG_ERR(...) ZCRYPT_DBF(DBF_ERR, ##__VA_ARGS__)
  27. /* default iv used here */
  28. static const u8 def_iv[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  29. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
  30. /* ep11 card info cache */
  31. struct card_list_entry {
  32. struct list_head list;
  33. u16 cardnr;
  34. struct ep11_card_info info;
  35. };
  36. static LIST_HEAD(card_list);
  37. static DEFINE_SPINLOCK(card_list_lock);
  38. static int card_cache_fetch(u16 cardnr, struct ep11_card_info *ci)
  39. {
  40. int rc = -ENOENT;
  41. struct card_list_entry *ptr;
  42. spin_lock_bh(&card_list_lock);
  43. list_for_each_entry(ptr, &card_list, list) {
  44. if (ptr->cardnr == cardnr) {
  45. memcpy(ci, &ptr->info, sizeof(*ci));
  46. rc = 0;
  47. break;
  48. }
  49. }
  50. spin_unlock_bh(&card_list_lock);
  51. return rc;
  52. }
  53. static void card_cache_update(u16 cardnr, const struct ep11_card_info *ci)
  54. {
  55. int found = 0;
  56. struct card_list_entry *ptr;
  57. spin_lock_bh(&card_list_lock);
  58. list_for_each_entry(ptr, &card_list, list) {
  59. if (ptr->cardnr == cardnr) {
  60. memcpy(&ptr->info, ci, sizeof(*ci));
  61. found = 1;
  62. break;
  63. }
  64. }
  65. if (!found) {
  66. ptr = kmalloc(sizeof(*ptr), GFP_ATOMIC);
  67. if (!ptr) {
  68. spin_unlock_bh(&card_list_lock);
  69. return;
  70. }
  71. ptr->cardnr = cardnr;
  72. memcpy(&ptr->info, ci, sizeof(*ci));
  73. list_add(&ptr->list, &card_list);
  74. }
  75. spin_unlock_bh(&card_list_lock);
  76. }
  77. static void card_cache_scrub(u16 cardnr)
  78. {
  79. struct card_list_entry *ptr;
  80. spin_lock_bh(&card_list_lock);
  81. list_for_each_entry(ptr, &card_list, list) {
  82. if (ptr->cardnr == cardnr) {
  83. list_del(&ptr->list);
  84. kfree(ptr);
  85. break;
  86. }
  87. }
  88. spin_unlock_bh(&card_list_lock);
  89. }
  90. static void __exit card_cache_free(void)
  91. {
  92. struct card_list_entry *ptr, *pnext;
  93. spin_lock_bh(&card_list_lock);
  94. list_for_each_entry_safe(ptr, pnext, &card_list, list) {
  95. list_del(&ptr->list);
  96. kfree(ptr);
  97. }
  98. spin_unlock_bh(&card_list_lock);
  99. }
  100. static int ep11_kb_split(const u8 *kb, size_t kblen, u32 kbver,
  101. struct ep11kblob_header **kbhdr, size_t *kbhdrsize,
  102. u8 **kbpl, size_t *kbplsize)
  103. {
  104. struct ep11kblob_header *hdr = NULL;
  105. size_t hdrsize, plsize = 0;
  106. int rc = -EINVAL;
  107. u8 *pl = NULL;
  108. if (kblen < sizeof(struct ep11kblob_header))
  109. goto out;
  110. hdr = (struct ep11kblob_header *)kb;
  111. switch (kbver) {
  112. case TOKVER_EP11_AES:
  113. /* header overlays the payload */
  114. hdrsize = 0;
  115. break;
  116. case TOKVER_EP11_ECC_WITH_HEADER:
  117. case TOKVER_EP11_AES_WITH_HEADER:
  118. /* payload starts after the header */
  119. hdrsize = sizeof(struct ep11kblob_header);
  120. break;
  121. default:
  122. goto out;
  123. }
  124. plsize = kblen - hdrsize;
  125. pl = (u8 *)kb + hdrsize;
  126. if (kbhdr)
  127. *kbhdr = hdr;
  128. if (kbhdrsize)
  129. *kbhdrsize = hdrsize;
  130. if (kbpl)
  131. *kbpl = pl;
  132. if (kbplsize)
  133. *kbplsize = plsize;
  134. rc = 0;
  135. out:
  136. return rc;
  137. }
  138. /*
  139. * Simple check if the key blob is a valid EP11 AES key blob with header.
  140. */
  141. int ep11_check_aes_key_with_hdr(debug_info_t *dbg, int dbflvl,
  142. const u8 *key, size_t keylen, int checkcpacfexp)
  143. {
  144. struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
  145. struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
  146. #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
  147. if (keylen < sizeof(*hdr) + sizeof(*kb)) {
  148. DBF("%s key check failed, keylen %zu < %zu\n",
  149. __func__, keylen, sizeof(*hdr) + sizeof(*kb));
  150. return -EINVAL;
  151. }
  152. if (hdr->type != TOKTYPE_NON_CCA) {
  153. if (dbg)
  154. DBF("%s key check failed, type 0x%02x != 0x%02x\n",
  155. __func__, (int)hdr->type, TOKTYPE_NON_CCA);
  156. return -EINVAL;
  157. }
  158. if (hdr->hver != 0x00) {
  159. if (dbg)
  160. DBF("%s key check failed, header version 0x%02x != 0x00\n",
  161. __func__, (int)hdr->hver);
  162. return -EINVAL;
  163. }
  164. if (hdr->version != TOKVER_EP11_AES_WITH_HEADER) {
  165. if (dbg)
  166. DBF("%s key check failed, version 0x%02x != 0x%02x\n",
  167. __func__, (int)hdr->version, TOKVER_EP11_AES_WITH_HEADER);
  168. return -EINVAL;
  169. }
  170. if (hdr->len > keylen) {
  171. if (dbg)
  172. DBF("%s key check failed, header len %d keylen %zu mismatch\n",
  173. __func__, (int)hdr->len, keylen);
  174. return -EINVAL;
  175. }
  176. if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
  177. if (dbg)
  178. DBF("%s key check failed, header len %d < %zu\n",
  179. __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
  180. return -EINVAL;
  181. }
  182. if (kb->version != EP11_STRUCT_MAGIC) {
  183. if (dbg)
  184. DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
  185. __func__, (int)kb->version, EP11_STRUCT_MAGIC);
  186. return -EINVAL;
  187. }
  188. if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
  189. if (dbg)
  190. DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
  191. __func__);
  192. return -EINVAL;
  193. }
  194. #undef DBF
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(ep11_check_aes_key_with_hdr);
  198. /*
  199. * Simple check if the key blob is a valid EP11 ECC key blob with header.
  200. */
  201. int ep11_check_ecc_key_with_hdr(debug_info_t *dbg, int dbflvl,
  202. const u8 *key, size_t keylen, int checkcpacfexp)
  203. {
  204. struct ep11kblob_header *hdr = (struct ep11kblob_header *)key;
  205. struct ep11keyblob *kb = (struct ep11keyblob *)(key + sizeof(*hdr));
  206. #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
  207. if (keylen < sizeof(*hdr) + sizeof(*kb)) {
  208. DBF("%s key check failed, keylen %zu < %zu\n",
  209. __func__, keylen, sizeof(*hdr) + sizeof(*kb));
  210. return -EINVAL;
  211. }
  212. if (hdr->type != TOKTYPE_NON_CCA) {
  213. if (dbg)
  214. DBF("%s key check failed, type 0x%02x != 0x%02x\n",
  215. __func__, (int)hdr->type, TOKTYPE_NON_CCA);
  216. return -EINVAL;
  217. }
  218. if (hdr->hver != 0x00) {
  219. if (dbg)
  220. DBF("%s key check failed, header version 0x%02x != 0x00\n",
  221. __func__, (int)hdr->hver);
  222. return -EINVAL;
  223. }
  224. if (hdr->version != TOKVER_EP11_ECC_WITH_HEADER) {
  225. if (dbg)
  226. DBF("%s key check failed, version 0x%02x != 0x%02x\n",
  227. __func__, (int)hdr->version, TOKVER_EP11_ECC_WITH_HEADER);
  228. return -EINVAL;
  229. }
  230. if (hdr->len > keylen) {
  231. if (dbg)
  232. DBF("%s key check failed, header len %d keylen %zu mismatch\n",
  233. __func__, (int)hdr->len, keylen);
  234. return -EINVAL;
  235. }
  236. if (hdr->len < sizeof(*hdr) + sizeof(*kb)) {
  237. if (dbg)
  238. DBF("%s key check failed, header len %d < %zu\n",
  239. __func__, (int)hdr->len, sizeof(*hdr) + sizeof(*kb));
  240. return -EINVAL;
  241. }
  242. if (kb->version != EP11_STRUCT_MAGIC) {
  243. if (dbg)
  244. DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
  245. __func__, (int)kb->version, EP11_STRUCT_MAGIC);
  246. return -EINVAL;
  247. }
  248. if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
  249. if (dbg)
  250. DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
  251. __func__);
  252. return -EINVAL;
  253. }
  254. #undef DBF
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(ep11_check_ecc_key_with_hdr);
  258. /*
  259. * Simple check if the key blob is a valid EP11 AES key blob with
  260. * the header in the session field (old style EP11 AES key).
  261. */
  262. int ep11_check_aes_key(debug_info_t *dbg, int dbflvl,
  263. const u8 *key, size_t keylen, int checkcpacfexp)
  264. {
  265. struct ep11keyblob *kb = (struct ep11keyblob *)key;
  266. #define DBF(...) debug_sprintf_event(dbg, dbflvl, ##__VA_ARGS__)
  267. if (keylen < sizeof(*kb)) {
  268. DBF("%s key check failed, keylen %zu < %zu\n",
  269. __func__, keylen, sizeof(*kb));
  270. return -EINVAL;
  271. }
  272. if (kb->head.type != TOKTYPE_NON_CCA) {
  273. if (dbg)
  274. DBF("%s key check failed, type 0x%02x != 0x%02x\n",
  275. __func__, (int)kb->head.type, TOKTYPE_NON_CCA);
  276. return -EINVAL;
  277. }
  278. if (kb->head.version != TOKVER_EP11_AES) {
  279. if (dbg)
  280. DBF("%s key check failed, version 0x%02x != 0x%02x\n",
  281. __func__, (int)kb->head.version, TOKVER_EP11_AES);
  282. return -EINVAL;
  283. }
  284. if (kb->head.len > keylen) {
  285. if (dbg)
  286. DBF("%s key check failed, header len %d keylen %zu mismatch\n",
  287. __func__, (int)kb->head.len, keylen);
  288. return -EINVAL;
  289. }
  290. if (kb->head.len < sizeof(*kb)) {
  291. if (dbg)
  292. DBF("%s key check failed, header len %d < %zu\n",
  293. __func__, (int)kb->head.len, sizeof(*kb));
  294. return -EINVAL;
  295. }
  296. if (kb->version != EP11_STRUCT_MAGIC) {
  297. if (dbg)
  298. DBF("%s key check failed, blob magic 0x%04x != 0x%04x\n",
  299. __func__, (int)kb->version, EP11_STRUCT_MAGIC);
  300. return -EINVAL;
  301. }
  302. if (checkcpacfexp && !(kb->attr & EP11_BLOB_PKEY_EXTRACTABLE)) {
  303. if (dbg)
  304. DBF("%s key check failed, PKEY_EXTRACTABLE is off\n",
  305. __func__);
  306. return -EINVAL;
  307. }
  308. #undef DBF
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(ep11_check_aes_key);
  312. /*
  313. * Allocate and prepare ep11 cprb plus additional payload.
  314. */
  315. static inline struct ep11_cprb *alloc_cprb(size_t payload_len)
  316. {
  317. size_t len = sizeof(struct ep11_cprb) + payload_len;
  318. struct ep11_cprb *cprb;
  319. cprb = kzalloc(len, GFP_KERNEL);
  320. if (!cprb)
  321. return NULL;
  322. cprb->cprb_len = sizeof(struct ep11_cprb);
  323. cprb->cprb_ver_id = 0x04;
  324. memcpy(cprb->func_id, "T4", 2);
  325. cprb->ret_code = 0xFFFFFFFF;
  326. cprb->payload_len = payload_len;
  327. return cprb;
  328. }
  329. /*
  330. * Some helper functions related to ASN1 encoding.
  331. * Limited to length info <= 2 byte.
  332. */
  333. #define ASN1TAGLEN(x) (2 + (x) + ((x) > 127 ? 1 : 0) + ((x) > 255 ? 1 : 0))
  334. static int asn1tag_write(u8 *ptr, u8 tag, const u8 *pvalue, u16 valuelen)
  335. {
  336. ptr[0] = tag;
  337. if (valuelen > 255) {
  338. ptr[1] = 0x82;
  339. *((u16 *)(ptr + 2)) = valuelen;
  340. memcpy(ptr + 4, pvalue, valuelen);
  341. return 4 + valuelen;
  342. }
  343. if (valuelen > 127) {
  344. ptr[1] = 0x81;
  345. ptr[2] = (u8)valuelen;
  346. memcpy(ptr + 3, pvalue, valuelen);
  347. return 3 + valuelen;
  348. }
  349. ptr[1] = (u8)valuelen;
  350. memcpy(ptr + 2, pvalue, valuelen);
  351. return 2 + valuelen;
  352. }
  353. /* EP11 payload > 127 bytes starts with this struct */
  354. struct pl_head {
  355. u8 tag;
  356. u8 lenfmt;
  357. u16 len;
  358. u8 func_tag;
  359. u8 func_len;
  360. u32 func;
  361. u8 dom_tag;
  362. u8 dom_len;
  363. u32 dom;
  364. } __packed;
  365. /* prep ep11 payload head helper function */
  366. static inline void prep_head(struct pl_head *h,
  367. size_t pl_size, int api, int func)
  368. {
  369. h->tag = 0x30;
  370. h->lenfmt = 0x82;
  371. h->len = pl_size - 4;
  372. h->func_tag = 0x04;
  373. h->func_len = sizeof(u32);
  374. h->func = (api << 16) + func;
  375. h->dom_tag = 0x04;
  376. h->dom_len = sizeof(u32);
  377. }
  378. /* prep urb helper function */
  379. static inline void prep_urb(struct ep11_urb *u,
  380. struct ep11_target_dev *t, int nt,
  381. struct ep11_cprb *req, size_t req_len,
  382. struct ep11_cprb *rep, size_t rep_len)
  383. {
  384. u->targets = (u8 __user *)t;
  385. u->targets_num = nt;
  386. u->req = (u8 __user *)req;
  387. u->req_len = req_len;
  388. u->resp = (u8 __user *)rep;
  389. u->resp_len = rep_len;
  390. }
  391. /* Check ep11 reply payload, return 0 or suggested errno value. */
  392. static int check_reply_pl(const u8 *pl, const char *func)
  393. {
  394. int len;
  395. u32 ret;
  396. /* start tag */
  397. if (*pl++ != 0x30) {
  398. DEBUG_ERR("%s reply start tag mismatch\n", func);
  399. return -EIO;
  400. }
  401. /* payload length format */
  402. if (*pl < 127) {
  403. len = *pl;
  404. pl++;
  405. } else if (*pl == 0x81) {
  406. pl++;
  407. len = *pl;
  408. pl++;
  409. } else if (*pl == 0x82) {
  410. pl++;
  411. len = *((u16 *)pl);
  412. pl += 2;
  413. } else {
  414. DEBUG_ERR("%s reply start tag lenfmt mismatch 0x%02hhx\n",
  415. func, *pl);
  416. return -EIO;
  417. }
  418. /* len should cover at least 3 fields with 32 bit value each */
  419. if (len < 3 * 6) {
  420. DEBUG_ERR("%s reply length %d too small\n", func, len);
  421. return -EIO;
  422. }
  423. /* function tag, length and value */
  424. if (pl[0] != 0x04 || pl[1] != 0x04) {
  425. DEBUG_ERR("%s function tag or length mismatch\n", func);
  426. return -EIO;
  427. }
  428. pl += 6;
  429. /* dom tag, length and value */
  430. if (pl[0] != 0x04 || pl[1] != 0x04) {
  431. DEBUG_ERR("%s dom tag or length mismatch\n", func);
  432. return -EIO;
  433. }
  434. pl += 6;
  435. /* return value tag, length and value */
  436. if (pl[0] != 0x04 || pl[1] != 0x04) {
  437. DEBUG_ERR("%s return value tag or length mismatch\n", func);
  438. return -EIO;
  439. }
  440. pl += 2;
  441. ret = *((u32 *)pl);
  442. if (ret != 0) {
  443. DEBUG_ERR("%s return value 0x%04x != 0\n", func, ret);
  444. return -EIO;
  445. }
  446. return 0;
  447. }
  448. /*
  449. * Helper function which does an ep11 query with given query type.
  450. */
  451. static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
  452. size_t buflen, u8 *buf)
  453. {
  454. struct ep11_info_req_pl {
  455. struct pl_head head;
  456. u8 query_type_tag;
  457. u8 query_type_len;
  458. u32 query_type;
  459. u8 query_subtype_tag;
  460. u8 query_subtype_len;
  461. u32 query_subtype;
  462. } __packed * req_pl;
  463. struct ep11_info_rep_pl {
  464. struct pl_head head;
  465. u8 rc_tag;
  466. u8 rc_len;
  467. u32 rc;
  468. u8 data_tag;
  469. u8 data_lenfmt;
  470. u16 data_len;
  471. } __packed * rep_pl;
  472. struct ep11_cprb *req = NULL, *rep = NULL;
  473. struct ep11_target_dev target;
  474. struct ep11_urb *urb = NULL;
  475. int api = 1, rc = -ENOMEM;
  476. /* request cprb and payload */
  477. req = alloc_cprb(sizeof(struct ep11_info_req_pl));
  478. if (!req)
  479. goto out;
  480. req_pl = (struct ep11_info_req_pl *)(((u8 *)req) + sizeof(*req));
  481. prep_head(&req_pl->head, sizeof(*req_pl), api, 38); /* get xcp info */
  482. req_pl->query_type_tag = 0x04;
  483. req_pl->query_type_len = sizeof(u32);
  484. req_pl->query_type = query_type;
  485. req_pl->query_subtype_tag = 0x04;
  486. req_pl->query_subtype_len = sizeof(u32);
  487. /* reply cprb and payload */
  488. rep = alloc_cprb(sizeof(struct ep11_info_rep_pl) + buflen);
  489. if (!rep)
  490. goto out;
  491. rep_pl = (struct ep11_info_rep_pl *)(((u8 *)rep) + sizeof(*rep));
  492. /* urb and target */
  493. urb = kmalloc(sizeof(*urb), GFP_KERNEL);
  494. if (!urb)
  495. goto out;
  496. target.ap_id = cardnr;
  497. target.dom_id = domain;
  498. prep_urb(urb, &target, 1,
  499. req, sizeof(*req) + sizeof(*req_pl),
  500. rep, sizeof(*rep) + sizeof(*rep_pl) + buflen);
  501. rc = zcrypt_send_ep11_cprb(urb);
  502. if (rc) {
  503. DEBUG_ERR(
  504. "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
  505. __func__, (int)cardnr, (int)domain, rc);
  506. goto out;
  507. }
  508. rc = check_reply_pl((u8 *)rep_pl, __func__);
  509. if (rc)
  510. goto out;
  511. if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
  512. DEBUG_ERR("%s unknown reply data format\n", __func__);
  513. rc = -EIO;
  514. goto out;
  515. }
  516. if (rep_pl->data_len > buflen) {
  517. DEBUG_ERR("%s mismatch between reply data len and buffer len\n",
  518. __func__);
  519. rc = -ENOSPC;
  520. goto out;
  521. }
  522. memcpy(buf, ((u8 *)rep_pl) + sizeof(*rep_pl), rep_pl->data_len);
  523. out:
  524. kfree(req);
  525. kfree(rep);
  526. kfree(urb);
  527. return rc;
  528. }
  529. /*
  530. * Provide information about an EP11 card.
  531. */
  532. int ep11_get_card_info(u16 card, struct ep11_card_info *info, int verify)
  533. {
  534. int rc;
  535. struct ep11_module_query_info {
  536. u32 API_ord_nr;
  537. u32 firmware_id;
  538. u8 FW_major_vers;
  539. u8 FW_minor_vers;
  540. u8 CSP_major_vers;
  541. u8 CSP_minor_vers;
  542. u8 fwid[32];
  543. u8 xcp_config_hash[32];
  544. u8 CSP_config_hash[32];
  545. u8 serial[16];
  546. u8 module_date_time[16];
  547. u64 op_mode;
  548. u32 PKCS11_flags;
  549. u32 ext_flags;
  550. u32 domains;
  551. u32 sym_state_bytes;
  552. u32 digest_state_bytes;
  553. u32 pin_blob_bytes;
  554. u32 SPKI_bytes;
  555. u32 priv_key_blob_bytes;
  556. u32 sym_blob_bytes;
  557. u32 max_payload_bytes;
  558. u32 CP_profile_bytes;
  559. u32 max_CP_index;
  560. } __packed * pmqi = NULL;
  561. rc = card_cache_fetch(card, info);
  562. if (rc || verify) {
  563. pmqi = kmalloc(sizeof(*pmqi), GFP_KERNEL);
  564. if (!pmqi)
  565. return -ENOMEM;
  566. rc = ep11_query_info(card, AUTOSEL_DOM,
  567. 0x01 /* module info query */,
  568. sizeof(*pmqi), (u8 *)pmqi);
  569. if (rc) {
  570. if (rc == -ENODEV)
  571. card_cache_scrub(card);
  572. goto out;
  573. }
  574. memset(info, 0, sizeof(*info));
  575. info->API_ord_nr = pmqi->API_ord_nr;
  576. info->FW_version =
  577. (pmqi->FW_major_vers << 8) + pmqi->FW_minor_vers;
  578. memcpy(info->serial, pmqi->serial, sizeof(info->serial));
  579. info->op_mode = pmqi->op_mode;
  580. card_cache_update(card, info);
  581. }
  582. out:
  583. kfree(pmqi);
  584. return rc;
  585. }
  586. EXPORT_SYMBOL(ep11_get_card_info);
  587. /*
  588. * Provide information about a domain within an EP11 card.
  589. */
  590. int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
  591. {
  592. int rc;
  593. struct ep11_domain_query_info {
  594. u32 dom_index;
  595. u8 cur_WK_VP[32];
  596. u8 new_WK_VP[32];
  597. u32 dom_flags;
  598. u64 op_mode;
  599. } __packed * p_dom_info;
  600. p_dom_info = kmalloc(sizeof(*p_dom_info), GFP_KERNEL);
  601. if (!p_dom_info)
  602. return -ENOMEM;
  603. rc = ep11_query_info(card, domain, 0x03 /* domain info query */,
  604. sizeof(*p_dom_info), (u8 *)p_dom_info);
  605. if (rc)
  606. goto out;
  607. memset(info, 0, sizeof(*info));
  608. info->cur_wk_state = '0';
  609. info->new_wk_state = '0';
  610. if (p_dom_info->dom_flags & 0x10 /* left imprint mode */) {
  611. if (p_dom_info->dom_flags & 0x02 /* cur wk valid */) {
  612. info->cur_wk_state = '1';
  613. memcpy(info->cur_wkvp, p_dom_info->cur_WK_VP, 32);
  614. }
  615. if (p_dom_info->dom_flags & 0x04 || /* new wk present */
  616. p_dom_info->dom_flags & 0x08 /* new wk committed */) {
  617. info->new_wk_state =
  618. p_dom_info->dom_flags & 0x08 ? '2' : '1';
  619. memcpy(info->new_wkvp, p_dom_info->new_WK_VP, 32);
  620. }
  621. }
  622. info->op_mode = p_dom_info->op_mode;
  623. out:
  624. kfree(p_dom_info);
  625. return rc;
  626. }
  627. EXPORT_SYMBOL(ep11_get_domain_info);
  628. /*
  629. * Default EP11 AES key generate attributes, used when no keygenflags given:
  630. * XCP_BLOB_ENCRYPT | XCP_BLOB_DECRYPT | XCP_BLOB_PROTKEY_EXTRACTABLE
  631. */
  632. #define KEY_ATTR_DEFAULTS 0x00200c00
  633. static int _ep11_genaeskey(u16 card, u16 domain,
  634. u32 keybitsize, u32 keygenflags,
  635. u8 *keybuf, size_t *keybufsize)
  636. {
  637. struct keygen_req_pl {
  638. struct pl_head head;
  639. u8 var_tag;
  640. u8 var_len;
  641. u32 var;
  642. u8 keybytes_tag;
  643. u8 keybytes_len;
  644. u32 keybytes;
  645. u8 mech_tag;
  646. u8 mech_len;
  647. u32 mech;
  648. u8 attr_tag;
  649. u8 attr_len;
  650. u32 attr_header;
  651. u32 attr_bool_mask;
  652. u32 attr_bool_bits;
  653. u32 attr_val_len_type;
  654. u32 attr_val_len_value;
  655. u8 pin_tag;
  656. u8 pin_len;
  657. } __packed * req_pl;
  658. struct keygen_rep_pl {
  659. struct pl_head head;
  660. u8 rc_tag;
  661. u8 rc_len;
  662. u32 rc;
  663. u8 data_tag;
  664. u8 data_lenfmt;
  665. u16 data_len;
  666. u8 data[512];
  667. } __packed * rep_pl;
  668. struct ep11_cprb *req = NULL, *rep = NULL;
  669. struct ep11_target_dev target;
  670. struct ep11_urb *urb = NULL;
  671. int api, rc = -ENOMEM;
  672. switch (keybitsize) {
  673. case 128:
  674. case 192:
  675. case 256:
  676. break;
  677. default:
  678. DEBUG_ERR(
  679. "%s unknown/unsupported keybitsize %d\n",
  680. __func__, keybitsize);
  681. rc = -EINVAL;
  682. goto out;
  683. }
  684. /* request cprb and payload */
  685. req = alloc_cprb(sizeof(struct keygen_req_pl));
  686. if (!req)
  687. goto out;
  688. req_pl = (struct keygen_req_pl *)(((u8 *)req) + sizeof(*req));
  689. api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
  690. prep_head(&req_pl->head, sizeof(*req_pl), api, 21); /* GenerateKey */
  691. req_pl->var_tag = 0x04;
  692. req_pl->var_len = sizeof(u32);
  693. req_pl->keybytes_tag = 0x04;
  694. req_pl->keybytes_len = sizeof(u32);
  695. req_pl->keybytes = keybitsize / 8;
  696. req_pl->mech_tag = 0x04;
  697. req_pl->mech_len = sizeof(u32);
  698. req_pl->mech = 0x00001080; /* CKM_AES_KEY_GEN */
  699. req_pl->attr_tag = 0x04;
  700. req_pl->attr_len = 5 * sizeof(u32);
  701. req_pl->attr_header = 0x10010000;
  702. req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
  703. req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
  704. req_pl->attr_val_len_type = 0x00000161; /* CKA_VALUE_LEN */
  705. req_pl->attr_val_len_value = keybitsize / 8;
  706. req_pl->pin_tag = 0x04;
  707. /* reply cprb and payload */
  708. rep = alloc_cprb(sizeof(struct keygen_rep_pl));
  709. if (!rep)
  710. goto out;
  711. rep_pl = (struct keygen_rep_pl *)(((u8 *)rep) + sizeof(*rep));
  712. /* urb and target */
  713. urb = kmalloc(sizeof(*urb), GFP_KERNEL);
  714. if (!urb)
  715. goto out;
  716. target.ap_id = card;
  717. target.dom_id = domain;
  718. prep_urb(urb, &target, 1,
  719. req, sizeof(*req) + sizeof(*req_pl),
  720. rep, sizeof(*rep) + sizeof(*rep_pl));
  721. rc = zcrypt_send_ep11_cprb(urb);
  722. if (rc) {
  723. DEBUG_ERR(
  724. "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
  725. __func__, (int)card, (int)domain, rc);
  726. goto out;
  727. }
  728. rc = check_reply_pl((u8 *)rep_pl, __func__);
  729. if (rc)
  730. goto out;
  731. if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
  732. DEBUG_ERR("%s unknown reply data format\n", __func__);
  733. rc = -EIO;
  734. goto out;
  735. }
  736. if (rep_pl->data_len > *keybufsize) {
  737. DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
  738. __func__);
  739. rc = -ENOSPC;
  740. goto out;
  741. }
  742. /* copy key blob */
  743. memcpy(keybuf, rep_pl->data, rep_pl->data_len);
  744. *keybufsize = rep_pl->data_len;
  745. out:
  746. kfree(req);
  747. kfree(rep);
  748. kfree(urb);
  749. return rc;
  750. }
  751. int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
  752. u8 *keybuf, size_t *keybufsize, u32 keybufver)
  753. {
  754. struct ep11kblob_header *hdr;
  755. size_t hdr_size, pl_size;
  756. u8 *pl;
  757. int rc;
  758. switch (keybufver) {
  759. case TOKVER_EP11_AES:
  760. case TOKVER_EP11_AES_WITH_HEADER:
  761. break;
  762. default:
  763. return -EINVAL;
  764. }
  765. rc = ep11_kb_split(keybuf, *keybufsize, keybufver,
  766. &hdr, &hdr_size, &pl, &pl_size);
  767. if (rc)
  768. return rc;
  769. rc = _ep11_genaeskey(card, domain, keybitsize, keygenflags,
  770. pl, &pl_size);
  771. if (rc)
  772. return rc;
  773. *keybufsize = hdr_size + pl_size;
  774. /* update header information */
  775. hdr->type = TOKTYPE_NON_CCA;
  776. hdr->len = *keybufsize;
  777. hdr->version = keybufver;
  778. hdr->bitlen = keybitsize;
  779. return 0;
  780. }
  781. EXPORT_SYMBOL(ep11_genaeskey);
  782. static int ep11_cryptsingle(u16 card, u16 domain,
  783. u16 mode, u32 mech, const u8 *iv,
  784. const u8 *key, size_t keysize,
  785. const u8 *inbuf, size_t inbufsize,
  786. u8 *outbuf, size_t *outbufsize)
  787. {
  788. struct crypt_req_pl {
  789. struct pl_head head;
  790. u8 var_tag;
  791. u8 var_len;
  792. u32 var;
  793. u8 mech_tag;
  794. u8 mech_len;
  795. u32 mech;
  796. /*
  797. * maybe followed by iv data
  798. * followed by key tag + key blob
  799. * followed by plaintext tag + plaintext
  800. */
  801. } __packed * req_pl;
  802. struct crypt_rep_pl {
  803. struct pl_head head;
  804. u8 rc_tag;
  805. u8 rc_len;
  806. u32 rc;
  807. u8 data_tag;
  808. u8 data_lenfmt;
  809. /* data follows */
  810. } __packed * rep_pl;
  811. struct ep11_cprb *req = NULL, *rep = NULL;
  812. struct ep11_target_dev target;
  813. struct ep11_urb *urb = NULL;
  814. size_t req_pl_size, rep_pl_size;
  815. int n, api = 1, rc = -ENOMEM;
  816. u8 *p;
  817. /* the simple asn1 coding used has length limits */
  818. if (keysize > 0xFFFF || inbufsize > 0xFFFF)
  819. return -EINVAL;
  820. /* request cprb and payload */
  821. req_pl_size = sizeof(struct crypt_req_pl) + (iv ? 16 : 0)
  822. + ASN1TAGLEN(keysize) + ASN1TAGLEN(inbufsize);
  823. req = alloc_cprb(req_pl_size);
  824. if (!req)
  825. goto out;
  826. req_pl = (struct crypt_req_pl *)(((u8 *)req) + sizeof(*req));
  827. prep_head(&req_pl->head, req_pl_size, api, (mode ? 20 : 19));
  828. req_pl->var_tag = 0x04;
  829. req_pl->var_len = sizeof(u32);
  830. /* mech is mech + mech params (iv here) */
  831. req_pl->mech_tag = 0x04;
  832. req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
  833. req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
  834. p = ((u8 *)req_pl) + sizeof(*req_pl);
  835. if (iv) {
  836. memcpy(p, iv, 16);
  837. p += 16;
  838. }
  839. /* key and input data */
  840. p += asn1tag_write(p, 0x04, key, keysize);
  841. p += asn1tag_write(p, 0x04, inbuf, inbufsize);
  842. /* reply cprb and payload, assume out data size <= in data size + 32 */
  843. rep_pl_size = sizeof(struct crypt_rep_pl) + ASN1TAGLEN(inbufsize + 32);
  844. rep = alloc_cprb(rep_pl_size);
  845. if (!rep)
  846. goto out;
  847. rep_pl = (struct crypt_rep_pl *)(((u8 *)rep) + sizeof(*rep));
  848. /* urb and target */
  849. urb = kmalloc(sizeof(*urb), GFP_KERNEL);
  850. if (!urb)
  851. goto out;
  852. target.ap_id = card;
  853. target.dom_id = domain;
  854. prep_urb(urb, &target, 1,
  855. req, sizeof(*req) + req_pl_size,
  856. rep, sizeof(*rep) + rep_pl_size);
  857. rc = zcrypt_send_ep11_cprb(urb);
  858. if (rc) {
  859. DEBUG_ERR(
  860. "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
  861. __func__, (int)card, (int)domain, rc);
  862. goto out;
  863. }
  864. rc = check_reply_pl((u8 *)rep_pl, __func__);
  865. if (rc)
  866. goto out;
  867. if (rep_pl->data_tag != 0x04) {
  868. DEBUG_ERR("%s unknown reply data format\n", __func__);
  869. rc = -EIO;
  870. goto out;
  871. }
  872. p = ((u8 *)rep_pl) + sizeof(*rep_pl);
  873. if (rep_pl->data_lenfmt <= 127) {
  874. n = rep_pl->data_lenfmt;
  875. } else if (rep_pl->data_lenfmt == 0x81) {
  876. n = *p++;
  877. } else if (rep_pl->data_lenfmt == 0x82) {
  878. n = *((u16 *)p);
  879. p += 2;
  880. } else {
  881. DEBUG_ERR("%s unknown reply data length format 0x%02hhx\n",
  882. __func__, rep_pl->data_lenfmt);
  883. rc = -EIO;
  884. goto out;
  885. }
  886. if (n > *outbufsize) {
  887. DEBUG_ERR("%s mismatch reply data len %d / output buffer %zu\n",
  888. __func__, n, *outbufsize);
  889. rc = -ENOSPC;
  890. goto out;
  891. }
  892. memcpy(outbuf, p, n);
  893. *outbufsize = n;
  894. out:
  895. kfree(req);
  896. kfree(rep);
  897. kfree(urb);
  898. return rc;
  899. }
  900. static int _ep11_unwrapkey(u16 card, u16 domain,
  901. const u8 *kek, size_t keksize,
  902. const u8 *enckey, size_t enckeysize,
  903. u32 mech, const u8 *iv,
  904. u32 keybitsize, u32 keygenflags,
  905. u8 *keybuf, size_t *keybufsize)
  906. {
  907. struct uw_req_pl {
  908. struct pl_head head;
  909. u8 attr_tag;
  910. u8 attr_len;
  911. u32 attr_header;
  912. u32 attr_bool_mask;
  913. u32 attr_bool_bits;
  914. u32 attr_key_type;
  915. u32 attr_key_type_value;
  916. u32 attr_val_len;
  917. u32 attr_val_len_value;
  918. u8 mech_tag;
  919. u8 mech_len;
  920. u32 mech;
  921. /*
  922. * maybe followed by iv data
  923. * followed by kek tag + kek blob
  924. * followed by empty mac tag
  925. * followed by empty pin tag
  926. * followed by encryted key tag + bytes
  927. */
  928. } __packed * req_pl;
  929. struct uw_rep_pl {
  930. struct pl_head head;
  931. u8 rc_tag;
  932. u8 rc_len;
  933. u32 rc;
  934. u8 data_tag;
  935. u8 data_lenfmt;
  936. u16 data_len;
  937. u8 data[512];
  938. } __packed * rep_pl;
  939. struct ep11_cprb *req = NULL, *rep = NULL;
  940. struct ep11_target_dev target;
  941. struct ep11_urb *urb = NULL;
  942. size_t req_pl_size;
  943. int api, rc = -ENOMEM;
  944. u8 *p;
  945. /* request cprb and payload */
  946. req_pl_size = sizeof(struct uw_req_pl) + (iv ? 16 : 0)
  947. + ASN1TAGLEN(keksize) + 4 + ASN1TAGLEN(enckeysize);
  948. req = alloc_cprb(req_pl_size);
  949. if (!req)
  950. goto out;
  951. req_pl = (struct uw_req_pl *)(((u8 *)req) + sizeof(*req));
  952. api = (!keygenflags || keygenflags & 0x00200000) ? 4 : 1;
  953. prep_head(&req_pl->head, req_pl_size, api, 34); /* UnwrapKey */
  954. req_pl->attr_tag = 0x04;
  955. req_pl->attr_len = 7 * sizeof(u32);
  956. req_pl->attr_header = 0x10020000;
  957. req_pl->attr_bool_mask = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
  958. req_pl->attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS;
  959. req_pl->attr_key_type = 0x00000100; /* CKA_KEY_TYPE */
  960. req_pl->attr_key_type_value = 0x0000001f; /* CKK_AES */
  961. req_pl->attr_val_len = 0x00000161; /* CKA_VALUE_LEN */
  962. req_pl->attr_val_len_value = keybitsize / 8;
  963. /* mech is mech + mech params (iv here) */
  964. req_pl->mech_tag = 0x04;
  965. req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
  966. req_pl->mech = (mech ? mech : 0x00001085); /* CKM_AES_CBC_PAD */
  967. p = ((u8 *)req_pl) + sizeof(*req_pl);
  968. if (iv) {
  969. memcpy(p, iv, 16);
  970. p += 16;
  971. }
  972. /* kek */
  973. p += asn1tag_write(p, 0x04, kek, keksize);
  974. /* empty mac key tag */
  975. *p++ = 0x04;
  976. *p++ = 0;
  977. /* empty pin tag */
  978. *p++ = 0x04;
  979. *p++ = 0;
  980. /* encrypted key value tag and bytes */
  981. p += asn1tag_write(p, 0x04, enckey, enckeysize);
  982. /* reply cprb and payload */
  983. rep = alloc_cprb(sizeof(struct uw_rep_pl));
  984. if (!rep)
  985. goto out;
  986. rep_pl = (struct uw_rep_pl *)(((u8 *)rep) + sizeof(*rep));
  987. /* urb and target */
  988. urb = kmalloc(sizeof(*urb), GFP_KERNEL);
  989. if (!urb)
  990. goto out;
  991. target.ap_id = card;
  992. target.dom_id = domain;
  993. prep_urb(urb, &target, 1,
  994. req, sizeof(*req) + req_pl_size,
  995. rep, sizeof(*rep) + sizeof(*rep_pl));
  996. rc = zcrypt_send_ep11_cprb(urb);
  997. if (rc) {
  998. DEBUG_ERR(
  999. "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
  1000. __func__, (int)card, (int)domain, rc);
  1001. goto out;
  1002. }
  1003. rc = check_reply_pl((u8 *)rep_pl, __func__);
  1004. if (rc)
  1005. goto out;
  1006. if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
  1007. DEBUG_ERR("%s unknown reply data format\n", __func__);
  1008. rc = -EIO;
  1009. goto out;
  1010. }
  1011. if (rep_pl->data_len > *keybufsize) {
  1012. DEBUG_ERR("%s mismatch reply data len / key buffer len\n",
  1013. __func__);
  1014. rc = -ENOSPC;
  1015. goto out;
  1016. }
  1017. /* copy key blob */
  1018. memcpy(keybuf, rep_pl->data, rep_pl->data_len);
  1019. *keybufsize = rep_pl->data_len;
  1020. out:
  1021. kfree(req);
  1022. kfree(rep);
  1023. kfree(urb);
  1024. return rc;
  1025. }
  1026. static int ep11_unwrapkey(u16 card, u16 domain,
  1027. const u8 *kek, size_t keksize,
  1028. const u8 *enckey, size_t enckeysize,
  1029. u32 mech, const u8 *iv,
  1030. u32 keybitsize, u32 keygenflags,
  1031. u8 *keybuf, size_t *keybufsize,
  1032. u8 keybufver)
  1033. {
  1034. struct ep11kblob_header *hdr;
  1035. size_t hdr_size, pl_size;
  1036. u8 *pl;
  1037. int rc;
  1038. rc = ep11_kb_split(keybuf, *keybufsize, keybufver,
  1039. &hdr, &hdr_size, &pl, &pl_size);
  1040. if (rc)
  1041. return rc;
  1042. rc = _ep11_unwrapkey(card, domain, kek, keksize, enckey, enckeysize,
  1043. mech, iv, keybitsize, keygenflags,
  1044. pl, &pl_size);
  1045. if (rc)
  1046. return rc;
  1047. *keybufsize = hdr_size + pl_size;
  1048. /* update header information */
  1049. hdr = (struct ep11kblob_header *)keybuf;
  1050. hdr->type = TOKTYPE_NON_CCA;
  1051. hdr->len = *keybufsize;
  1052. hdr->version = keybufver;
  1053. hdr->bitlen = keybitsize;
  1054. return 0;
  1055. }
  1056. static int ep11_wrapkey(u16 card, u16 domain,
  1057. const u8 *key, size_t keysize,
  1058. u32 mech, const u8 *iv,
  1059. u8 *databuf, size_t *datasize)
  1060. {
  1061. struct wk_req_pl {
  1062. struct pl_head head;
  1063. u8 var_tag;
  1064. u8 var_len;
  1065. u32 var;
  1066. u8 mech_tag;
  1067. u8 mech_len;
  1068. u32 mech;
  1069. /*
  1070. * followed by iv data
  1071. * followed by key tag + key blob
  1072. * followed by dummy kek param
  1073. * followed by dummy mac param
  1074. */
  1075. } __packed * req_pl;
  1076. struct wk_rep_pl {
  1077. struct pl_head head;
  1078. u8 rc_tag;
  1079. u8 rc_len;
  1080. u32 rc;
  1081. u8 data_tag;
  1082. u8 data_lenfmt;
  1083. u16 data_len;
  1084. u8 data[1024];
  1085. } __packed * rep_pl;
  1086. struct ep11_cprb *req = NULL, *rep = NULL;
  1087. struct ep11_target_dev target;
  1088. struct ep11_urb *urb = NULL;
  1089. struct ep11keyblob *kb;
  1090. size_t req_pl_size;
  1091. int api, rc = -ENOMEM;
  1092. bool has_header = false;
  1093. u8 *p;
  1094. /* maybe the session field holds a header with key info */
  1095. kb = (struct ep11keyblob *)key;
  1096. if (kb->head.type == TOKTYPE_NON_CCA &&
  1097. kb->head.version == TOKVER_EP11_AES) {
  1098. has_header = true;
  1099. keysize = min_t(size_t, kb->head.len, keysize);
  1100. }
  1101. /* request cprb and payload */
  1102. req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
  1103. + ASN1TAGLEN(keysize) + 4;
  1104. req = alloc_cprb(req_pl_size);
  1105. if (!req)
  1106. goto out;
  1107. if (!mech || mech == 0x80060001)
  1108. req->flags |= 0x20; /* CPACF_WRAP needs special bit */
  1109. req_pl = (struct wk_req_pl *)(((u8 *)req) + sizeof(*req));
  1110. api = (!mech || mech == 0x80060001) ? 4 : 1; /* CKM_IBM_CPACF_WRAP */
  1111. prep_head(&req_pl->head, req_pl_size, api, 33); /* WrapKey */
  1112. req_pl->var_tag = 0x04;
  1113. req_pl->var_len = sizeof(u32);
  1114. /* mech is mech + mech params (iv here) */
  1115. req_pl->mech_tag = 0x04;
  1116. req_pl->mech_len = sizeof(u32) + (iv ? 16 : 0);
  1117. req_pl->mech = (mech ? mech : 0x80060001); /* CKM_IBM_CPACF_WRAP */
  1118. p = ((u8 *)req_pl) + sizeof(*req_pl);
  1119. if (iv) {
  1120. memcpy(p, iv, 16);
  1121. p += 16;
  1122. }
  1123. /* key blob */
  1124. p += asn1tag_write(p, 0x04, key, keysize);
  1125. /* maybe the key argument needs the head data cleaned out */
  1126. if (has_header) {
  1127. kb = (struct ep11keyblob *)(p - keysize);
  1128. memset(&kb->head, 0, sizeof(kb->head));
  1129. }
  1130. /* empty kek tag */
  1131. *p++ = 0x04;
  1132. *p++ = 0;
  1133. /* empty mac tag */
  1134. *p++ = 0x04;
  1135. *p++ = 0;
  1136. /* reply cprb and payload */
  1137. rep = alloc_cprb(sizeof(struct wk_rep_pl));
  1138. if (!rep)
  1139. goto out;
  1140. rep_pl = (struct wk_rep_pl *)(((u8 *)rep) + sizeof(*rep));
  1141. /* urb and target */
  1142. urb = kmalloc(sizeof(*urb), GFP_KERNEL);
  1143. if (!urb)
  1144. goto out;
  1145. target.ap_id = card;
  1146. target.dom_id = domain;
  1147. prep_urb(urb, &target, 1,
  1148. req, sizeof(*req) + req_pl_size,
  1149. rep, sizeof(*rep) + sizeof(*rep_pl));
  1150. rc = zcrypt_send_ep11_cprb(urb);
  1151. if (rc) {
  1152. DEBUG_ERR(
  1153. "%s zcrypt_send_ep11_cprb(card=%d dom=%d) failed, rc=%d\n",
  1154. __func__, (int)card, (int)domain, rc);
  1155. goto out;
  1156. }
  1157. rc = check_reply_pl((u8 *)rep_pl, __func__);
  1158. if (rc)
  1159. goto out;
  1160. if (rep_pl->data_tag != 0x04 || rep_pl->data_lenfmt != 0x82) {
  1161. DEBUG_ERR("%s unknown reply data format\n", __func__);
  1162. rc = -EIO;
  1163. goto out;
  1164. }
  1165. if (rep_pl->data_len > *datasize) {
  1166. DEBUG_ERR("%s mismatch reply data len / data buffer len\n",
  1167. __func__);
  1168. rc = -ENOSPC;
  1169. goto out;
  1170. }
  1171. /* copy the data from the cprb to the data buffer */
  1172. memcpy(databuf, rep_pl->data, rep_pl->data_len);
  1173. *datasize = rep_pl->data_len;
  1174. out:
  1175. kfree(req);
  1176. kfree(rep);
  1177. kfree(urb);
  1178. return rc;
  1179. }
  1180. int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
  1181. const u8 *clrkey, u8 *keybuf, size_t *keybufsize,
  1182. u32 keytype)
  1183. {
  1184. int rc;
  1185. u8 encbuf[64], *kek = NULL;
  1186. size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
  1187. if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256) {
  1188. clrkeylen = keybitsize / 8;
  1189. } else {
  1190. DEBUG_ERR(
  1191. "%s unknown/unsupported keybitsize %d\n",
  1192. __func__, keybitsize);
  1193. return -EINVAL;
  1194. }
  1195. /* allocate memory for the temp kek */
  1196. keklen = MAXEP11AESKEYBLOBSIZE;
  1197. kek = kmalloc(keklen, GFP_ATOMIC);
  1198. if (!kek) {
  1199. rc = -ENOMEM;
  1200. goto out;
  1201. }
  1202. /* Step 1: generate AES 256 bit random kek key */
  1203. rc = _ep11_genaeskey(card, domain, 256,
  1204. 0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
  1205. kek, &keklen);
  1206. if (rc) {
  1207. DEBUG_ERR(
  1208. "%s generate kek key failed, rc=%d\n",
  1209. __func__, rc);
  1210. goto out;
  1211. }
  1212. /* Step 2: encrypt clear key value with the kek key */
  1213. rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
  1214. clrkey, clrkeylen, encbuf, &encbuflen);
  1215. if (rc) {
  1216. DEBUG_ERR(
  1217. "%s encrypting key value with kek key failed, rc=%d\n",
  1218. __func__, rc);
  1219. goto out;
  1220. }
  1221. /* Step 3: import the encrypted key value as a new key */
  1222. rc = ep11_unwrapkey(card, domain, kek, keklen,
  1223. encbuf, encbuflen, 0, def_iv,
  1224. keybitsize, 0, keybuf, keybufsize, keytype);
  1225. if (rc) {
  1226. DEBUG_ERR(
  1227. "%s importing key value as new key failed,, rc=%d\n",
  1228. __func__, rc);
  1229. goto out;
  1230. }
  1231. out:
  1232. kfree(kek);
  1233. return rc;
  1234. }
  1235. EXPORT_SYMBOL(ep11_clr2keyblob);
  1236. int ep11_kblob2protkey(u16 card, u16 dom, const u8 *keyblob, size_t keybloblen,
  1237. u8 *protkey, u32 *protkeylen, u32 *protkeytype)
  1238. {
  1239. int rc = -EIO;
  1240. u8 *wkbuf = NULL;
  1241. size_t wkbuflen, keylen;
  1242. struct wk_info {
  1243. u16 version;
  1244. u8 res1[16];
  1245. u32 pkeytype;
  1246. u32 pkeybitsize;
  1247. u64 pkeysize;
  1248. u8 res2[8];
  1249. u8 pkey[0];
  1250. } __packed * wki;
  1251. const u8 *key;
  1252. struct ep11kblob_header *hdr;
  1253. /* key with or without header ? */
  1254. hdr = (struct ep11kblob_header *)keyblob;
  1255. if (hdr->type == TOKTYPE_NON_CCA &&
  1256. (hdr->version == TOKVER_EP11_AES_WITH_HEADER ||
  1257. hdr->version == TOKVER_EP11_ECC_WITH_HEADER) &&
  1258. is_ep11_keyblob(keyblob + sizeof(struct ep11kblob_header))) {
  1259. /* EP11 AES or ECC key with header */
  1260. key = keyblob + sizeof(struct ep11kblob_header);
  1261. keylen = hdr->len - sizeof(struct ep11kblob_header);
  1262. } else if (hdr->type == TOKTYPE_NON_CCA &&
  1263. hdr->version == TOKVER_EP11_AES &&
  1264. is_ep11_keyblob(keyblob)) {
  1265. /* EP11 AES key (old style) */
  1266. key = keyblob;
  1267. keylen = hdr->len;
  1268. } else if (is_ep11_keyblob(keyblob)) {
  1269. /* raw EP11 key blob */
  1270. key = keyblob;
  1271. keylen = keybloblen;
  1272. } else {
  1273. return -EINVAL;
  1274. }
  1275. /* alloc temp working buffer */
  1276. wkbuflen = (keylen + AES_BLOCK_SIZE) & (~(AES_BLOCK_SIZE - 1));
  1277. wkbuf = kmalloc(wkbuflen, GFP_ATOMIC);
  1278. if (!wkbuf)
  1279. return -ENOMEM;
  1280. /* ep11 secure key -> protected key + info */
  1281. rc = ep11_wrapkey(card, dom, key, keylen,
  1282. 0, def_iv, wkbuf, &wkbuflen);
  1283. if (rc) {
  1284. DEBUG_ERR(
  1285. "%s rewrapping ep11 key to pkey failed, rc=%d\n",
  1286. __func__, rc);
  1287. goto out;
  1288. }
  1289. wki = (struct wk_info *)wkbuf;
  1290. /* check struct version and pkey type */
  1291. if (wki->version != 1 || wki->pkeytype < 1 || wki->pkeytype > 5) {
  1292. DEBUG_ERR("%s wk info version %d or pkeytype %d mismatch.\n",
  1293. __func__, (int)wki->version, (int)wki->pkeytype);
  1294. rc = -EIO;
  1295. goto out;
  1296. }
  1297. /* check protected key type field */
  1298. switch (wki->pkeytype) {
  1299. case 1: /* AES */
  1300. switch (wki->pkeysize) {
  1301. case 16 + 32:
  1302. /* AES 128 protected key */
  1303. if (protkeytype)
  1304. *protkeytype = PKEY_KEYTYPE_AES_128;
  1305. break;
  1306. case 24 + 32:
  1307. /* AES 192 protected key */
  1308. if (protkeytype)
  1309. *protkeytype = PKEY_KEYTYPE_AES_192;
  1310. break;
  1311. case 32 + 32:
  1312. /* AES 256 protected key */
  1313. if (protkeytype)
  1314. *protkeytype = PKEY_KEYTYPE_AES_256;
  1315. break;
  1316. default:
  1317. DEBUG_ERR("%s unknown/unsupported AES pkeysize %d\n",
  1318. __func__, (int)wki->pkeysize);
  1319. rc = -EIO;
  1320. goto out;
  1321. }
  1322. break;
  1323. case 3: /* EC-P */
  1324. case 4: /* EC-ED */
  1325. case 5: /* EC-BP */
  1326. if (protkeytype)
  1327. *protkeytype = PKEY_KEYTYPE_ECC;
  1328. break;
  1329. case 2: /* TDES */
  1330. default:
  1331. DEBUG_ERR("%s unknown/unsupported key type %d\n",
  1332. __func__, (int)wki->pkeytype);
  1333. rc = -EIO;
  1334. goto out;
  1335. }
  1336. /* copy the tanslated protected key */
  1337. if (wki->pkeysize > *protkeylen) {
  1338. DEBUG_ERR("%s wk info pkeysize %llu > protkeysize %u\n",
  1339. __func__, wki->pkeysize, *protkeylen);
  1340. rc = -EINVAL;
  1341. goto out;
  1342. }
  1343. memcpy(protkey, wki->pkey, wki->pkeysize);
  1344. *protkeylen = wki->pkeysize;
  1345. out:
  1346. kfree(wkbuf);
  1347. return rc;
  1348. }
  1349. EXPORT_SYMBOL(ep11_kblob2protkey);
  1350. int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
  1351. int minhwtype, int minapi, const u8 *wkvp)
  1352. {
  1353. struct zcrypt_device_status_ext *device_status;
  1354. u32 *_apqns = NULL, _nr_apqns = 0;
  1355. int i, card, dom, rc = -ENOMEM;
  1356. struct ep11_domain_info edi;
  1357. struct ep11_card_info eci;
  1358. /* fetch status of all crypto cards */
  1359. device_status = kvmalloc_array(MAX_ZDEV_ENTRIES_EXT,
  1360. sizeof(struct zcrypt_device_status_ext),
  1361. GFP_KERNEL);
  1362. if (!device_status)
  1363. return -ENOMEM;
  1364. zcrypt_device_status_mask_ext(device_status);
  1365. /* allocate 1k space for up to 256 apqns */
  1366. _apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
  1367. if (!_apqns) {
  1368. kvfree(device_status);
  1369. return -ENOMEM;
  1370. }
  1371. /* walk through all the crypto apqnss */
  1372. for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
  1373. card = AP_QID_CARD(device_status[i].qid);
  1374. dom = AP_QID_QUEUE(device_status[i].qid);
  1375. /* check online state */
  1376. if (!device_status[i].online)
  1377. continue;
  1378. /* check for ep11 functions */
  1379. if (!(device_status[i].functions & 0x01))
  1380. continue;
  1381. /* check cardnr */
  1382. if (cardnr != 0xFFFF && card != cardnr)
  1383. continue;
  1384. /* check domain */
  1385. if (domain != 0xFFFF && dom != domain)
  1386. continue;
  1387. /* check min hardware type */
  1388. if (minhwtype && device_status[i].hwtype < minhwtype)
  1389. continue;
  1390. /* check min api version if given */
  1391. if (minapi > 0) {
  1392. if (ep11_get_card_info(card, &eci, 0))
  1393. continue;
  1394. if (minapi > eci.API_ord_nr)
  1395. continue;
  1396. }
  1397. /* check wkvp if given */
  1398. if (wkvp) {
  1399. if (ep11_get_domain_info(card, dom, &edi))
  1400. continue;
  1401. if (edi.cur_wk_state != '1')
  1402. continue;
  1403. if (memcmp(wkvp, edi.cur_wkvp, 16))
  1404. continue;
  1405. }
  1406. /* apqn passed all filtering criterons, add to the array */
  1407. if (_nr_apqns < 256)
  1408. _apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
  1409. }
  1410. /* nothing found ? */
  1411. if (!_nr_apqns) {
  1412. kfree(_apqns);
  1413. rc = -ENODEV;
  1414. } else {
  1415. /* no re-allocation, simple return the _apqns array */
  1416. *apqns = _apqns;
  1417. *nr_apqns = _nr_apqns;
  1418. rc = 0;
  1419. }
  1420. kvfree(device_status);
  1421. return rc;
  1422. }
  1423. EXPORT_SYMBOL(ep11_findcard2);
  1424. void __exit zcrypt_ep11misc_exit(void)
  1425. {
  1426. card_cache_free();
  1427. }