zcrypt_ep11misc.c 36 KB

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