tme_hwkm_master.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/err.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include "tme_hwkm_master_intf.h"
  10. #include "tmecom.h"
  11. #define TME_MSG_CBOR_TAG_HWKM (303)
  12. #define TME_CLEAR_KEY_CBOR_TAG 0x482F01D9 /* _be32 0xD9012F48 */
  13. #define TME_DERIVE_KEY_CBOR_TAG 0x582F01D9 /* _be32 0xD9012F58 */
  14. #define TME_GENERATE_KEY_CBOR_TAG 0x542F01D9 /* _be32 0xD9012F54 */
  15. #define TME_IMPORT_KEY_CBOR_TAG 0x582F01D9 /* _be32 0xD9012F58 */
  16. #define TME_WRAP_KEY_CBOR_TAG 0x502F01D9 /* _be32 0xD9012F50 */
  17. #define TME_UNWRAP_KEY_CBOR_TAG 0x582F01D9 /* _be32 0xD9012F58 */
  18. #define TME_BORADCAST_KEY_CBOR_TAG 0x442F01D9 /* _be32 0xD9012F44 */
  19. /*
  20. * Static alloc for wrapped key
  21. * Protected by tmecom dev mutex
  22. */
  23. static struct wrap_key_resp gwrpk_response = {0};
  24. static inline uint32_t update_ext_err(
  25. struct tme_ext_err_info *err_info,
  26. struct tme_response_sts *result)
  27. {
  28. bool is_failure = false;
  29. err_info->tme_err_status = result->tme_err_status;
  30. err_info->seq_err_status = result->seq_err_status;
  31. err_info->seq_kp_err_status0 = result->seq_kp_err_status0;
  32. err_info->seq_kp_err_status1 = result->seq_kp_err_status1;
  33. err_info->seq_rsp_status = result->seq_rsp_status;
  34. is_failure = err_info->tme_err_status ||
  35. err_info->seq_err_status ||
  36. err_info->seq_kp_err_status0 ||
  37. err_info->seq_kp_err_status1;
  38. print_hex_dump_bytes("err_info decoded bytes : ",
  39. DUMP_PREFIX_ADDRESS, (void *)err_info,
  40. sizeof(*err_info));
  41. return is_failure ? 1 : 0;
  42. }
  43. uint32_t tme_hwkm_master_clearkey(uint32_t key_id,
  44. struct tme_ext_err_info *err_info)
  45. {
  46. struct clear_key_req *request = NULL;
  47. struct tme_response_sts *response = NULL;
  48. uint32_t ret = 0;
  49. size_t response_len = sizeof(*response);
  50. if (!err_info)
  51. return -EINVAL;
  52. request = kzalloc(sizeof(*request), GFP_KERNEL);
  53. response = kzalloc(response_len, GFP_KERNEL);
  54. if (!request || !response) {
  55. ret = -ENOMEM;
  56. goto err_exit;
  57. }
  58. request->cmd.code = TME_HWKM_CMD_CLEAR_KEY;
  59. request->key_id = key_id;
  60. request->cbor_header = TME_CLEAR_KEY_CBOR_TAG;
  61. ret = tmecom_process_request(request, sizeof(*request), response,
  62. &response_len);
  63. if (ret != 0) {
  64. pr_err("HWKM clear key request failed for %d\n", key_id);
  65. goto err_exit;
  66. }
  67. if (response_len != sizeof(*response)) {
  68. pr_err("HWKM response failed with invalid length: %u, %u\n",
  69. response_len, sizeof(response));
  70. ret = -EBADMSG;
  71. goto err_exit;
  72. }
  73. ret = update_ext_err(err_info, response);
  74. err_exit:
  75. kfree(request);
  76. kfree(response);
  77. return ret;
  78. }
  79. EXPORT_SYMBOL(tme_hwkm_master_clearkey);
  80. uint32_t tme_hwkm_master_generatekey(uint32_t key_id,
  81. struct tme_key_policy *policy,
  82. uint32_t cred_slot,
  83. struct tme_ext_err_info *err_info)
  84. {
  85. struct gen_key_req *request = NULL;
  86. struct tme_response_sts *response = NULL;
  87. uint32_t ret = 0;
  88. size_t response_len = sizeof(*response);
  89. if (!err_info || !policy)
  90. return -EINVAL;
  91. request = kzalloc(sizeof(*request), GFP_KERNEL);
  92. response = kzalloc(response_len, GFP_KERNEL);
  93. if (!request || !response) {
  94. ret = -ENOMEM;
  95. goto err_exit;
  96. }
  97. request->cmd.code = TME_HWKM_CMD_GENERATE_KEY;
  98. request->key_id = key_id;
  99. request->cred_slot = cred_slot;
  100. request->cbor_header = TME_GENERATE_KEY_CBOR_TAG;
  101. memcpy(&request->key_policy, policy, sizeof(*policy));
  102. ret = tmecom_process_request(request, sizeof(*request), response,
  103. &response_len);
  104. if (ret != 0) {
  105. pr_err("HWKM generate key request failed for %d\n", key_id);
  106. goto err_exit;
  107. }
  108. if (response_len != sizeof(*response)) {
  109. pr_err("HWKM response failed with invalid length: %u, %u\n",
  110. response_len, sizeof(response));
  111. ret = -EBADMSG;
  112. goto err_exit;
  113. }
  114. ret = update_ext_err(err_info, response);
  115. err_exit:
  116. kfree(request);
  117. kfree(response);
  118. return ret;
  119. }
  120. EXPORT_SYMBOL(tme_hwkm_master_generatekey);
  121. uint32_t tme_hwkm_master_derivekey(uint32_t key_id,
  122. struct tme_kdf_spec *kdf_info,
  123. uint32_t cred_slot,
  124. struct tme_ext_err_info *err_info)
  125. {
  126. struct derive_key_req *request = NULL;
  127. struct tme_response_sts *response = NULL;
  128. uint32_t ret = 0;
  129. size_t response_len = sizeof(*response);
  130. if (!kdf_info || !err_info)
  131. return -EINVAL;
  132. request = kzalloc(sizeof(*request), GFP_KERNEL);
  133. response = kzalloc(response_len, GFP_KERNEL);
  134. if (!request || !response) {
  135. ret = -ENOMEM;
  136. goto err_exit;
  137. }
  138. request->cmd.code = TME_HWKM_CMD_DERIVE_KEY;
  139. request->key_id = key_id;
  140. request->cred_slot = cred_slot;
  141. request->cbor_header = TME_DERIVE_KEY_CBOR_TAG;
  142. memcpy(&request->kdf_info, kdf_info, sizeof(*kdf_info));
  143. ret = tmecom_process_request(request, sizeof(*request), response,
  144. &response_len);
  145. if (ret != 0) {
  146. pr_err("HWKM derive key request failed for %d\n", key_id);
  147. goto err_exit;
  148. }
  149. if (response_len != sizeof(*response)) {
  150. pr_err("HWKM response failed with invalid length: %u, %u\n",
  151. response_len, sizeof(response));
  152. ret = -EBADMSG;
  153. goto err_exit;
  154. }
  155. ret = update_ext_err(err_info, response);
  156. err_exit:
  157. kfree(request);
  158. kfree(response);
  159. return ret;
  160. }
  161. EXPORT_SYMBOL(tme_hwkm_master_derivekey);
  162. uint32_t tme_hwkm_master_wrapkey(uint32_t key_id,
  163. uint32_t targetkey_id,
  164. uint32_t cred_slot,
  165. struct tme_wrapped_key *wrapped,
  166. struct tme_ext_err_info *err_info)
  167. {
  168. struct wrap_key_req *request = NULL;
  169. struct wrap_key_resp *wrpk_response = NULL;
  170. uint32_t ret = 0;
  171. size_t response_len = sizeof(*wrpk_response);
  172. if (!wrapped || !err_info)
  173. return -EINVAL;
  174. request = kzalloc(sizeof(*request), GFP_KERNEL);
  175. wrpk_response = &gwrpk_response;
  176. if (!request)
  177. return -ENOMEM;
  178. request->cmd.code = TME_HWKM_CMD_WRAP_KEY;
  179. request->key_id = key_id;
  180. request->target_key_id = targetkey_id;
  181. request->cbor_header = TME_WRAP_KEY_CBOR_TAG;
  182. ret = tmecom_process_request(request, sizeof(*request), wrpk_response,
  183. &response_len);
  184. if (ret != 0) {
  185. pr_err("HWKM wrap key request failed for %d\n", key_id);
  186. goto err_exit;
  187. }
  188. if (response_len != sizeof(*wrpk_response)) {
  189. pr_err("HWKM response failed with invalid length: %u, %u\n",
  190. response_len, sizeof(wrpk_response));
  191. ret = -EBADMSG;
  192. goto err_exit;
  193. }
  194. ret = update_ext_err(err_info, &wrpk_response->status);
  195. if (!ret)
  196. memcpy(wrapped, &wrpk_response->wrapped_key, sizeof(*wrapped));
  197. err_exit:
  198. kfree(request);
  199. return ret;
  200. }
  201. EXPORT_SYMBOL(tme_hwkm_master_wrapkey);
  202. uint32_t tme_hwkm_master_unwrapkey(uint32_t key_id,
  203. uint32_t kwkey_id,
  204. uint32_t cred_slot,
  205. struct tme_wrapped_key *wrapped,
  206. struct tme_ext_err_info *err_info)
  207. {
  208. struct unwrap_key_req *request = NULL;
  209. struct tme_response_sts *response = NULL;
  210. uint32_t ret = 0;
  211. size_t response_len = sizeof(*response);
  212. if (!wrapped || !err_info)
  213. return -EINVAL;
  214. request = kzalloc(sizeof(*request), GFP_KERNEL);
  215. response = kzalloc(response_len, GFP_KERNEL);
  216. if (!request || !response) {
  217. ret = -ENOMEM;
  218. goto err_exit;
  219. }
  220. request->cmd.code = TME_HWKM_CMD_UNWRAP_KEY;
  221. request->key_id = key_id;
  222. request->kw_key_id = kwkey_id;
  223. request->cbor_header = TME_UNWRAP_KEY_CBOR_TAG;
  224. memcpy(&request->wrapped, wrapped, sizeof(*wrapped));
  225. ret = tmecom_process_request(request, sizeof(*request), response,
  226. &response_len);
  227. if (ret != 0) {
  228. pr_err("HWKM unwrap key request failed for %d\n", key_id);
  229. goto err_exit;
  230. }
  231. if (response_len != sizeof(*response)) {
  232. pr_err("HWKM response failed with invalid length: %u, %u\n",
  233. response_len, sizeof(response));
  234. ret = -EBADMSG;
  235. goto err_exit;
  236. }
  237. ret = update_ext_err(err_info, response);
  238. err_exit:
  239. kfree(request);
  240. kfree(response);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL(tme_hwkm_master_unwrapkey);
  244. uint32_t tme_hwkm_master_importkey(uint32_t key_id,
  245. struct tme_key_policy *policy,
  246. struct tme_plaintext_key *key_material,
  247. uint32_t cred_slot,
  248. struct tme_ext_err_info *err_info)
  249. {
  250. struct import_key_req *request = NULL;
  251. struct tme_response_sts *response = NULL;
  252. uint32_t ret = 0;
  253. size_t response_len = sizeof(*response);
  254. if (!key_material || !err_info || !policy)
  255. return -EINVAL;
  256. request = kzalloc(sizeof(*request), GFP_KERNEL);
  257. response = kzalloc(response_len, GFP_KERNEL);
  258. if (!request || !response) {
  259. ret = -ENOMEM;
  260. goto err_exit;
  261. }
  262. request->cmd.code = TME_HWKM_CMD_IMPORT_KEY;
  263. request->key_id = key_id;
  264. request->cred_slot = cred_slot;
  265. request->cbor_header = TME_IMPORT_KEY_CBOR_TAG;
  266. memcpy(&request->key_policy, policy, sizeof(*policy));
  267. memcpy(&request->key_material, key_material, sizeof(*key_material));
  268. ret = tmecom_process_request(request, sizeof(*request), response,
  269. &response_len);
  270. if (ret != 0) {
  271. pr_err("HWKM import key request failed for %d\n", key_id);
  272. goto err_exit;
  273. }
  274. if (response_len != sizeof(*response)) {
  275. pr_err("HWKM response failed with invalid length: %u, %u\n",
  276. response_len, sizeof(response));
  277. ret = -EBADMSG;
  278. goto err_exit;
  279. }
  280. ret = update_ext_err(err_info, response);
  281. err_exit:
  282. kfree(request);
  283. kfree(response);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL(tme_hwkm_master_importkey);
  287. uint32_t tme_hwkm_master_broadcast_transportkey(
  288. struct tme_ext_err_info *err_info)
  289. {
  290. struct broadcast_tpkey_req *request = NULL;
  291. struct tme_response_sts *response = NULL;
  292. uint32_t ret = 0;
  293. size_t response_len = sizeof(*response);
  294. if (!err_info)
  295. return -EINVAL;
  296. request = kzalloc(sizeof(*request), GFP_KERNEL);
  297. response = kzalloc(response_len, GFP_KERNEL);
  298. if (!request || !response) {
  299. ret = -ENOMEM;
  300. goto err_exit;
  301. }
  302. request->cbor_header = TME_BORADCAST_KEY_CBOR_TAG;
  303. request->cmd.code = TME_HWKM_CMD_BROADCAST_TP_KEY;
  304. ret = tmecom_process_request(request, sizeof(*request), response,
  305. &response_len);
  306. if (ret != 0) {
  307. pr_err("HWKM broadcast TP key request failed\n");
  308. goto err_exit;
  309. }
  310. if (response_len != sizeof(*response)) {
  311. pr_err("HWKM response failed with invalid length: %u, %u\n",
  312. response_len, sizeof(response));
  313. ret = -EBADMSG;
  314. goto err_exit;
  315. }
  316. ret = update_ext_err(err_info, response);
  317. err_exit:
  318. kfree(request);
  319. kfree(response);
  320. return ret;
  321. }
  322. EXPORT_SYMBOL(tme_hwkm_master_broadcast_transportkey);