zcrypt_ep11misc.c 40 KB

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