tlcDpHdcp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Copyright (c) 2020 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include "tee_client_api.h"
  16. #include "tlcDpHdcp.h"
  17. #define LOG_TAG "DP_HDCP_CA"
  18. #define AN_LEN 8
  19. #define AKSV_LEN 5
  20. #define DEFAULT_WRITE_VAL_LEN 1
  21. #define DEFAULT_WRITE_VAL 0
  22. //key id used in keyinstall
  23. #define HDCP_1X_TX_ID 2
  24. #define HDCP_2X_TX_ID 3
  25. #define TZCMD_DRMKEY_LOAD_HDCPKEY 12
  26. const struct TEEC_UUID uuid = {
  27. 0xabcd270e, 0xa5c4, 0x4c58,
  28. {0xbc, 0xd3, 0x38, 0x4a, 0x2f, 0xa2, 0x53, 0x9e}
  29. };
  30. const struct TEEC_UUID KI_TA_UUID = {
  31. 0x08110000, 0x0000, 0x0000,
  32. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  33. };
  34. const static size_t MAX_SHARED_MEMORY_SIZE = 4 * 1024; // 4K
  35. static struct TEEC_SharedMemory sSharedMemoryMain;
  36. struct TEEC_Context sContext;
  37. struct TEEC_Session sSession;
  38. static bool g_init;
  39. int tee_addDevice(uint32_t version)
  40. {
  41. uint32_t ret = TEEC_SUCCESS;
  42. struct tci_t *tci;
  43. struct TEEC_Operation op;
  44. bool needLoadKey = false;
  45. if (g_init)
  46. tee_removeDevice();
  47. #ifndef HDCP_SETKEY_FROM_KERNEL
  48. //load key first
  49. if (version > 0) {
  50. ret = tee_loadHdcpKeyById(version);
  51. if (ret != TEEC_SUCCESS) {
  52. TLCERR("ret = 0x%x", ret);
  53. return ret;
  54. }
  55. needLoadKey = true;
  56. }
  57. #endif
  58. ret = TEEC_InitializeContext(NULL, &sContext);
  59. if (ret != TEEC_SUCCESS) {
  60. TLCERR("ret = 0x%x", ret);
  61. return ret;
  62. }
  63. ret = TEEC_OpenSession(&sContext, &sSession, &uuid, TEEC_LOGIN_PUBLIC,
  64. NULL, NULL, NULL);
  65. if (ret != TEEC_SUCCESS) {
  66. TLCERR("TEEC_OpenSession ret = 0x%x", ret);
  67. TEEC_FinalizeContext(&sContext);
  68. return ret;
  69. }
  70. // Try to register one 4M main share memory, if failed(means current
  71. // tee don't support), register four 1M extra share memory instead.
  72. // Only do this when first initialize.
  73. sSharedMemoryMain.buffer = NULL;
  74. sSharedMemoryMain.size = MAX_SHARED_MEMORY_SIZE;
  75. sSharedMemoryMain.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
  76. ret = TEEC_AllocateSharedMemory(&sContext, &sSharedMemoryMain);
  77. if (ret == TEEC_SUCCESS) {
  78. TLCINFO("Register 4k share memory successfully, (%p)",
  79. sSharedMemoryMain.buffer);
  80. } else {
  81. TLCERR("Register 4k share memory failed(0x%x), (%p)",
  82. ret, sSharedMemoryMain.buffer);
  83. if (sSharedMemoryMain.buffer != NULL) {
  84. TEEC_ReleaseSharedMemory(&sSharedMemoryMain);
  85. sSharedMemoryMain.buffer = NULL;
  86. }
  87. }
  88. // Copy parameter for add new device
  89. tci = (struct tci_t *)sSharedMemoryMain.buffer;
  90. memset(tci, 0, TCI_LENGTH);
  91. tci->message.cmdHDCP.commandHeader.commandId = CMD_DEVICE_ADDED;
  92. tci->message.cmdHDCP.cmdBody.cmdHDCPInitForVerion.version = version;
  93. tci->message.cmdHDCP.cmdBody.cmdHDCPInitForVerion.needLoadKey =
  94. needLoadKey;
  95. memset(&op, 0, sizeof(struct TEEC_Operation));
  96. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  97. TEEC_NONE, TEEC_NONE);
  98. op.params[0].memref.parent = &sSharedMemoryMain;
  99. op.params[0].memref.size = TCI_LENGTH;
  100. ret = TEEC_InvokeCommand(&sSession, CMD_DEVICE_ADDED, &op, NULL);
  101. if (ret != TEEC_SUCCESS) {
  102. TLCERR("InvokeCommand ret = 0x%x", ret);
  103. tee_removeDevice();
  104. }
  105. g_init = true;
  106. return ret;
  107. }
  108. void tee_removeDevice(void)
  109. {
  110. uint32_t ret = TEEC_SUCCESS;
  111. struct TEEC_Operation op;
  112. // Copy parameter for add new device
  113. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  114. if (!g_init)
  115. return;
  116. g_init = false;
  117. memset(tci, 0, TCI_LENGTH);
  118. tci->message.cmdHDCP.commandHeader.commandId = CMD_DEVICE_REMOVE;
  119. memset(&op, 0, sizeof(struct TEEC_Operation));
  120. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  121. TEEC_NONE, TEEC_NONE);
  122. op.params[0].memref.parent = &sSharedMemoryMain;
  123. op.params[0].memref.size = TCI_LENGTH;
  124. ret = TEEC_InvokeCommand(&sSession, CMD_DEVICE_REMOVE, &op, NULL);
  125. if (ret != TEEC_SUCCESS)
  126. TLCERR("ret = 0x%x", ret);
  127. //Release all resources
  128. if (sSharedMemoryMain.buffer != NULL) {
  129. TEEC_ReleaseSharedMemory(&sSharedMemoryMain);
  130. sSharedMemoryMain.buffer = NULL;
  131. }
  132. TEEC_CloseSession(&sSession);
  133. TEEC_FinalizeContext(&sContext);
  134. }
  135. int tee_clearParing(void)
  136. {
  137. uint32_t ret = TEEC_SUCCESS;
  138. struct TEEC_Operation op;
  139. // Copy parameter for add new device
  140. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  141. memset(tci, 0, TCI_LENGTH);
  142. tci->message.cmdHDCP.commandHeader.commandId = CMD_DEVICE_CLEAN;
  143. memset(&op, 0, sizeof(struct TEEC_Operation));
  144. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  145. TEEC_NONE, TEEC_NONE);
  146. op.params[0].memref.parent = &sSharedMemoryMain;
  147. op.params[0].memref.size = TCI_LENGTH;
  148. ret = TEEC_InvokeCommand(&sSession, CMD_DEVICE_CLEAN, &op, NULL);
  149. if (ret != TEEC_SUCCESS)
  150. TLCERR("ret = 0x%x", ret);
  151. return ret;
  152. }
  153. int tee_hdcp1x_setTxAn(uint8_t *an_code)
  154. {
  155. uint32_t ret = TEEC_SUCCESS;
  156. struct TEEC_Operation op;
  157. uint8_t an_len = 8;
  158. // Copy parameter for add new device
  159. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  160. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  161. memset(tci, 0, TCI_LENGTH);
  162. tci->message.cmdHDCP.commandHeader.commandId = CMD_WRITE_VAL;
  163. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.len = an_len;
  164. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.type = TYPE_HDCP_PARAM_AN;
  165. memcpy(share_buffer + TCI_LENGTH, an_code, an_len);
  166. memset(&op, 0, sizeof(struct TEEC_Operation));
  167. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  168. TEEC_NONE, TEEC_NONE);
  169. op.params[0].memref.parent = &sSharedMemoryMain;
  170. op.params[0].memref.size = TCI_LENGTH + an_len;
  171. ret = TEEC_InvokeCommand(&sSession, CMD_WRITE_VAL, &op, NULL);
  172. if (ret != TEEC_SUCCESS)
  173. TLCERR("ret = 0x%x", ret);
  174. return ret;
  175. }
  176. int tee_hdcp_enableEncrypt(bool bEnable, uint8_t version)
  177. {
  178. uint32_t ret = TEEC_SUCCESS;
  179. struct TEEC_Operation op;
  180. // Copy parameter for add new device
  181. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  182. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  183. memset(tci, 0, TCI_LENGTH);
  184. tci->message.cmdHDCP.commandHeader.commandId = CMD_ENABLE_ENCRYPT;
  185. if (bEnable)
  186. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.type
  187. = TYPE_HDCP_ENABLE_ENCRYPT;
  188. else
  189. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.type
  190. = TYPE_HDCP_DISABLE_ENCRYPT;
  191. //set HDCP version supportted by device
  192. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.len = 1;
  193. memset(share_buffer + TCI_LENGTH, version, 1);
  194. memset(&op, 0, sizeof(struct TEEC_Operation));
  195. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  196. TEEC_NONE, TEEC_NONE);
  197. op.params[0].memref.parent = &sSharedMemoryMain;
  198. op.params[0].memref.size = TCI_LENGTH + 1;
  199. ret = TEEC_InvokeCommand(&sSession, CMD_ENABLE_ENCRYPT, &op, NULL);
  200. if (ret != TEEC_SUCCESS)
  201. TLCERR("ret = 0x%x", ret);
  202. return ret;
  203. }
  204. int tee_hdcp1x_softRst(void)
  205. {
  206. uint32_t ret = TEEC_SUCCESS;
  207. struct TEEC_Operation op;
  208. // Copy parameter for add new device
  209. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  210. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  211. memset(tci, 0, TCI_LENGTH);
  212. tci->message.cmdHDCP.commandHeader.commandId = CMD_WRITE_VAL;
  213. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.type
  214. = TYPE_HDCP_PARAM_RST_1;
  215. //No need input. Set default value 0 for check
  216. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.len =
  217. DEFAULT_WRITE_VAL_LEN;
  218. memset(share_buffer + TCI_LENGTH, DEFAULT_WRITE_VAL,
  219. DEFAULT_WRITE_VAL_LEN);
  220. memset(&op, 0, sizeof(struct TEEC_Operation));
  221. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  222. TEEC_NONE, TEEC_NONE);
  223. op.params[0].memref.parent = &sSharedMemoryMain;
  224. op.params[0].memref.size = TCI_LENGTH + DEFAULT_WRITE_VAL_LEN;
  225. ret = TEEC_InvokeCommand(&sSession, CMD_WRITE_VAL, &op, NULL);
  226. if (ret != TEEC_SUCCESS)
  227. TLCERR("ret = 0x%x", ret);
  228. return ret;
  229. }
  230. int tee_hdcp2_softRst(void)
  231. {
  232. uint32_t ret = TEEC_SUCCESS;
  233. struct TEEC_Operation op;
  234. // Copy parameter for add new device
  235. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  236. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  237. memset(tci, 0, TCI_LENGTH);
  238. tci->message.cmdHDCP.commandHeader.commandId = CMD_WRITE_VAL;
  239. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.type
  240. = TYPE_HDCP_PARAM_RST_2;
  241. //No need input. Set default value 0 for check
  242. tci->message.cmdHDCP.cmdBody.cmdHDCPWriteVal.len =
  243. DEFAULT_WRITE_VAL_LEN;
  244. memset(share_buffer + TCI_LENGTH, DEFAULT_WRITE_VAL,
  245. DEFAULT_WRITE_VAL_LEN);
  246. memset(&op, 0, sizeof(struct TEEC_Operation));
  247. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  248. TEEC_NONE, TEEC_NONE);
  249. op.params[0].memref.parent = &sSharedMemoryMain;
  250. op.params[0].memref.size = TCI_LENGTH + DEFAULT_WRITE_VAL_LEN;
  251. ret = TEEC_InvokeCommand(&sSession, CMD_WRITE_VAL, &op, NULL);
  252. if (ret != TEEC_SUCCESS)
  253. TLCERR("ret = 0x%x", ret);
  254. return ret;
  255. }
  256. /** V1.X **/
  257. int tee_getAksv(uint8_t *Aksv)
  258. {
  259. uint32_t ret = TEEC_SUCCESS;
  260. struct tci_t *tci;
  261. struct TEEC_Operation op;
  262. // Copy parameters
  263. tci = (struct tci_t *)sSharedMemoryMain.buffer;
  264. memset(tci, 0, TCI_LENGTH);
  265. tci->message.cmdHDCP.commandHeader.commandId = CMD_GET_AKSV;
  266. memset(&op, 0, sizeof(struct TEEC_Operation));
  267. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  268. TEEC_NONE, TEEC_NONE);
  269. op.params[0].memref.parent = &sSharedMemoryMain;
  270. op.params[0].memref.size = TCI_LENGTH;
  271. ret = TEEC_InvokeCommand(&sSession, CMD_GET_AKSV, &op, NULL);
  272. if (ret != TEEC_SUCCESS)
  273. TLCERR("InvokeCommand ret = 0x%x", ret);
  274. memcpy(Aksv, tci->message.cmdHDCP.cmdBody.cmdHDCPGetAksv.aksv,
  275. AKSV_LEN);
  276. return ret;
  277. }
  278. int tee_calculateLm(uint8_t *Bksv)
  279. {
  280. uint32_t ret = TEEC_SUCCESS;
  281. struct tci_t *tci;
  282. struct TEEC_Operation op;
  283. // Copy parameters
  284. tci = (struct tci_t *)sSharedMemoryMain.buffer;
  285. memset(tci, 0, TCI_LENGTH);
  286. tci->message.cmdHDCP.commandHeader.commandId = CMD_CALCULATE_LM;
  287. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPCalculateLm.bksv, Bksv,
  288. BKSV_LEN);
  289. memset(&op, 0, sizeof(struct TEEC_Operation));
  290. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  291. TEEC_NONE, TEEC_NONE);
  292. op.params[0].memref.parent = &sSharedMemoryMain;
  293. op.params[0].memref.size = TCI_LENGTH;
  294. ret = TEEC_InvokeCommand(&sSession, CMD_CALCULATE_LM, &op, NULL);
  295. if (ret != TEEC_SUCCESS)
  296. TLCERR("InvokeCommand ret = 0x%x", ret);
  297. return ret;
  298. }
  299. int tee_compareR0(uint8_t *r0, uint32_t len)
  300. {
  301. uint32_t ret = TEEC_SUCCESS;
  302. struct TEEC_Operation op;
  303. // Copy parameters
  304. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  305. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  306. memset(tci, 0, TCI_LENGTH);
  307. tci->message.cmdHDCP.commandHeader.commandId = CMD_COMPARE_R0;
  308. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.rx_val_len = len;
  309. memcpy(share_buffer + TCI_LENGTH, r0, len);
  310. memset(&op, 0, sizeof(struct TEEC_Operation));
  311. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  312. TEEC_NONE, TEEC_NONE);
  313. op.params[0].memref.parent = &sSharedMemoryMain;
  314. op.params[0].memref.size = TCI_LENGTH + len;
  315. ret = TEEC_InvokeCommand(&sSession, CMD_COMPARE_R0, &op, NULL);
  316. if (ret != TEEC_SUCCESS)
  317. TLCERR("InvokeCommand ret = 0x%x", ret);
  318. return ret;
  319. }
  320. int tee_hdcp1x_ComputeCompareV(uint8_t *cryptoParam, uint32_t paramLen,
  321. uint8_t *rxV)
  322. {
  323. uint32_t ret = TEEC_SUCCESS;
  324. struct TEEC_Operation op;
  325. // Copy parameters
  326. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  327. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  328. memset(tci, 0, TCI_LENGTH);
  329. tci->message.cmdHDCP.commandHeader.commandId = CMD_COMPARE_V1;
  330. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.rx_val_len = 20;
  331. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.param_len = paramLen;
  332. memcpy(share_buffer + TCI_LENGTH, rxV, 20);
  333. memcpy(share_buffer + TCI_LENGTH + 20, cryptoParam, paramLen);
  334. memset(&op, 0, sizeof(struct TEEC_Operation));
  335. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  336. TEEC_NONE, TEEC_NONE);
  337. op.params[0].memref.parent = &sSharedMemoryMain;
  338. op.params[0].memref.size = TCI_LENGTH + paramLen + 20;
  339. ret = TEEC_InvokeCommand(&sSession, CMD_COMPARE_V1, &op, NULL);
  340. if (ret != TEEC_SUCCESS)
  341. TLCERR("InvokeCommand ret = 0x%x", ret);
  342. return ret;
  343. }
  344. /**** V2.X****/
  345. int tee_akeCertificate(uint8_t *certificate, bool *bStored, uint8_t *out_m,
  346. uint8_t *out_ekm)
  347. {
  348. uint32_t ret = TEEC_SUCCESS;
  349. struct TEEC_Operation op;
  350. // Copy parameters
  351. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  352. memset(tci, 0, TCI_LENGTH);
  353. tci->message.cmdHDCP.commandHeader.commandId = CMD_AKE_CERTIFICATE;
  354. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPAKECertificate.certification,
  355. certificate, CERT_LEN);
  356. memset(&op, 0, sizeof(struct TEEC_Operation));
  357. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  358. TEEC_NONE, TEEC_NONE);
  359. op.params[0].memref.parent = &sSharedMemoryMain;
  360. op.params[0].memref.size = TCI_LENGTH;
  361. ret = TEEC_InvokeCommand(&sSession, CMD_AKE_CERTIFICATE, &op, NULL);
  362. if (ret != TEEC_SUCCESS) {
  363. TLCERR("InvokeCommand ret = 0x%x", ret);
  364. return ret;
  365. }
  366. TLCINFO("verify signature: result %d", ret);
  367. *bStored = tci->message.cmdHDCP.cmdBody.cmdHDCPAKECertificate.bStored;
  368. memcpy(out_m, tci->message.cmdHDCP.cmdBody.cmdHDCPAKECertificate.m,
  369. M_LEN);
  370. memcpy(out_ekm, tci->message.cmdHDCP.cmdBody.cmdHDCPAKECertificate.ekm,
  371. EKM_LEN);
  372. return ret;
  373. }
  374. int tee_encRsaesOaep(uint8_t *ekm)
  375. {
  376. uint32_t ret = TEEC_SUCCESS;
  377. struct TEEC_Operation op;
  378. // Copy parameters
  379. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  380. memset(tci, 0, TCI_LENGTH);
  381. tci->message.cmdHDCP.commandHeader.commandId = CMD_ENC_KM;
  382. memset(&op, 0, sizeof(struct TEEC_Operation));
  383. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  384. TEEC_NONE, TEEC_NONE);
  385. op.params[0].memref.parent = &sSharedMemoryMain;
  386. op.params[0].memref.size = TCI_LENGTH;
  387. ret = TEEC_InvokeCommand(&sSession, CMD_ENC_KM, &op, NULL);
  388. if (ret != TEEC_SUCCESS)
  389. TLCERR("InvokeCommand ret = 0x%x", ret);
  390. memcpy(ekm, tci->message.cmdHDCP.cmdBody.cmdHDCPEncKm.encKm,
  391. ENC_KM_LEN);
  392. return ret;
  393. }
  394. int tee_akeHPrime(uint8_t *rtx, uint8_t *rrx, uint8_t *rxCaps, uint8_t *txCaps,
  395. uint8_t *rxH, uint32_t rxH_len)
  396. {
  397. uint32_t ret = TEEC_SUCCESS;
  398. struct TEEC_Operation op;
  399. // Copy parameters
  400. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  401. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  402. memset(tci, 0, TCI_LENGTH);
  403. tci->message.cmdHDCP.commandHeader.commandId = CMD_AKE_H_PRIME;
  404. tci->message.cmdHDCP.cmdBody.cmdHDCPAKEHPrime.rxH_len = rxH_len;
  405. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPAKEHPrime.rtx, rtx, RXX_LEN);
  406. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPAKEHPrime.rrx, rrx, RXX_LEN);
  407. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPAKEHPrime.rxCaps, rxCaps,
  408. CAPS_LEN);
  409. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPAKEHPrime.txCaps, txCaps,
  410. CAPS_LEN);
  411. memcpy(share_buffer + TCI_LENGTH, rxH, rxH_len);
  412. memset(&op, 0, sizeof(struct TEEC_Operation));
  413. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  414. TEEC_NONE, TEEC_NONE);
  415. op.params[0].memref.parent = &sSharedMemoryMain;
  416. op.params[0].memref.size = TCI_LENGTH + rxH_len;
  417. ret = TEEC_InvokeCommand(&sSession, CMD_AKE_H_PRIME, &op, NULL);
  418. if (ret != TEEC_SUCCESS) {
  419. TLCERR("InvokeCommand ret = 0x%x", ret);
  420. return ret;
  421. }
  422. return tci->message.cmdHDCP.responseHeader.returnCode;
  423. }
  424. int tee_akeParing(uint8_t *rx_ekm)
  425. {
  426. uint32_t ret = TEEC_SUCCESS;
  427. struct TEEC_Operation op;
  428. // Copy parameters
  429. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  430. memset(tci, 0, TCI_LENGTH);
  431. tci->message.cmdHDCP.commandHeader.commandId = CMD_AKE_PARING;
  432. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPAKEParing.ekm, rx_ekm,
  433. EKM_LEN);
  434. memset(&op, 0, sizeof(struct TEEC_Operation));
  435. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  436. TEEC_NONE, TEEC_NONE);
  437. op.params[0].memref.parent = &sSharedMemoryMain;
  438. op.params[0].memref.size = TCI_LENGTH;
  439. ret = TEEC_InvokeCommand(&sSession, CMD_AKE_PARING, &op, NULL);
  440. if (ret != TEEC_SUCCESS)
  441. TLCERR("InvokeCommand ret = 0x%x", ret);
  442. return ret;
  443. }
  444. int tee_lcLPrime(uint8_t *rn, uint8_t *rxL, uint32_t len)
  445. {
  446. uint32_t ret = TEEC_SUCCESS;
  447. struct TEEC_Operation op;
  448. // Copy parameters
  449. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  450. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  451. memset(tci, 0, TCI_LENGTH);
  452. tci->message.cmdHDCP.commandHeader.commandId = CMD_LC_L_PRIME;
  453. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPLcLPrime.rn, rn, RN_LEN);
  454. tci->message.cmdHDCP.cmdBody.cmdHDCPLcLPrime.rxL_len = len;
  455. memcpy(share_buffer + TCI_LENGTH, rxL, len);
  456. memset(&op, 0, sizeof(struct TEEC_Operation));
  457. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  458. TEEC_NONE, TEEC_NONE);
  459. op.params[0].memref.parent = &sSharedMemoryMain;
  460. op.params[0].memref.size = TCI_LENGTH + len;
  461. ret = TEEC_InvokeCommand(&sSession, CMD_LC_L_PRIME, &op, NULL);
  462. if (ret != TEEC_SUCCESS) {
  463. TLCERR("InvokeCommand ret = 0x%x", ret);
  464. return ret;
  465. }
  466. return tci->message.cmdHDCP.responseHeader.returnCode;
  467. }
  468. int tee_skeEncKs(uint8_t *riv, uint8_t *eks)
  469. {
  470. uint32_t ret = TEEC_SUCCESS;
  471. struct TEEC_Operation op;
  472. // Copy parameters
  473. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  474. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  475. memset(tci, 0, TCI_LENGTH);
  476. tci->message.cmdHDCP.commandHeader.commandId = CMD_SKE_CAL_EKS;
  477. memcpy(tci->message.cmdHDCP.cmdBody.cmdHDCPSKEEks.riv, riv, RIV_LEN);
  478. memset(&op, 0, sizeof(struct TEEC_Operation));
  479. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  480. TEEC_NONE, TEEC_NONE);
  481. op.params[0].memref.parent = &sSharedMemoryMain;
  482. op.params[0].memref.size = TCI_LENGTH + 16;
  483. ret = TEEC_InvokeCommand(&sSession, CMD_SKE_CAL_EKS, &op, NULL);
  484. if (ret != TEEC_SUCCESS)
  485. TLCERR("InvokeCommand ret = 0x%x", ret);
  486. memcpy(eks, share_buffer + TCI_LENGTH, 16);
  487. return ret;
  488. }
  489. int tee_hdcp2_ComputeCompareV(uint8_t *cryptoParam, uint32_t paramLen,
  490. uint8_t *rxV, uint8_t *txV)
  491. {
  492. uint32_t ret = TEEC_SUCCESS;
  493. struct TEEC_Operation op;
  494. // Copy parameters
  495. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  496. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  497. memset(tci, 0, TCI_LENGTH);
  498. tci->message.cmdHDCP.commandHeader.commandId = CMD_COMPARE_V2;
  499. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.rx_val_len = 16;
  500. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.param_len = paramLen;
  501. memcpy(share_buffer + TCI_LENGTH, rxV, 16);
  502. memcpy(share_buffer + TCI_LENGTH + 16, cryptoParam, paramLen);
  503. memset(&op, 0, sizeof(struct TEEC_Operation));
  504. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, TEEC_NONE,
  505. TEEC_NONE, TEEC_NONE);
  506. op.params[0].memref.parent = &sSharedMemoryMain;
  507. op.params[0].memref.size = TCI_LENGTH + paramLen + 16;
  508. ret = TEEC_InvokeCommand(&sSession, CMD_COMPARE_V2, &op, NULL);
  509. if (ret != TEEC_SUCCESS)
  510. TLCERR("InvokeCommand ret = 0x%x", ret);
  511. else
  512. memcpy(txV, share_buffer + TCI_LENGTH, 16);
  513. return ret;
  514. }
  515. int tee_hdcp2_ComputeCompareM(uint8_t *cryptoParam, uint32_t paramLen,
  516. uint8_t *rxM)
  517. {
  518. uint32_t ret = TEEC_SUCCESS;
  519. struct TEEC_Operation op;
  520. // Copy parameters
  521. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  522. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  523. memset(tci, 0, TCI_LENGTH);
  524. tci->message.cmdHDCP.commandHeader.commandId = CMD_COMPARE_M;
  525. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.rx_val_len = 32;
  526. tci->message.cmdHDCP.cmdBody.cmdHDCPCompare.param_len = paramLen;
  527. memcpy(share_buffer + TCI_LENGTH, rxM, 32);
  528. memcpy(share_buffer + TCI_LENGTH + 32, cryptoParam, paramLen);
  529. memset(&op, 0, sizeof(struct TEEC_Operation));
  530. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  531. TEEC_NONE, TEEC_NONE);
  532. op.params[0].memref.parent = &sSharedMemoryMain;
  533. op.params[0].memref.size = TCI_LENGTH + paramLen + 32;
  534. ret = TEEC_InvokeCommand(&sSession, CMD_COMPARE_M, &op, NULL);
  535. if (ret != TEEC_SUCCESS)
  536. TLCERR("InvokeCommand ret = 0x%x", ret);
  537. return ret;
  538. }
  539. int tee_loadHdcpKeyById(uint32_t version)
  540. {
  541. uint32_t ret = TEEC_SUCCESS;
  542. struct TEEC_Operation sOperation;
  543. struct TEEC_Context context;
  544. struct TEEC_Session session;
  545. uint32_t keyID = 0;
  546. switch (version) {
  547. case HDCP_VERSION_1X:
  548. keyID = HDCP_1X_TX_ID;
  549. break;
  550. case HDCP_VERSION_2X:
  551. keyID = HDCP_2X_TX_ID;
  552. break;
  553. default:
  554. TLCERR("Version not support %d", version);
  555. return TEEC_ERROR_NOT_SUPPORTED;
  556. }
  557. /* Create Device context */
  558. ret = TEEC_InitializeContext(NULL, &context);
  559. if (ret != TEEC_SUCCESS) {
  560. TLCERR("load key ret = 0x%x", ret);
  561. return ret;
  562. }
  563. memset(&sOperation, 0, sizeof(struct TEEC_Operation));
  564. sOperation.paramTypes = 0;
  565. ret = TEEC_OpenSession(&context,
  566. &session, /* OUT session */
  567. &KI_TA_UUID, /* destination UUID */
  568. TEEC_LOGIN_PUBLIC, /* connectionMethod */
  569. NULL, /* connectionData */
  570. &sOperation, /* IN OUT operation */
  571. NULL /* OUT returnOrigin, optional */
  572. );
  573. if (ret != TEEC_SUCCESS) {
  574. TLCERR("load key ret = 0x%x", ret);
  575. TEEC_FinalizeContext(&context);
  576. return ret;
  577. }
  578. memset(&sOperation, 0, sizeof(struct TEEC_Operation));
  579. sOperation.paramTypes = TEEC_PARAM_TYPES(
  580. TEEC_VALUE_INPUT,
  581. TEEC_NONE,
  582. TEEC_NONE,
  583. TEEC_NONE);
  584. sOperation.params[0].value.a = keyID;
  585. ret = TEEC_InvokeCommand(&session,
  586. TZCMD_DRMKEY_LOAD_HDCPKEY,
  587. &sOperation, /* IN OUT operation */
  588. NULL /* OUT returnOrigin, optional */
  589. );
  590. TEEC_CloseSession(&session);
  591. TEEC_FinalizeContext(&context);
  592. return ret;
  593. }
  594. /*for test and need remove in furture*/
  595. int tee_hdcp1x_setKey(uint8_t *key)
  596. {
  597. #ifdef HDCP_SETKEY_FROM_KERNEL
  598. uint32_t ret = TEEC_SUCCESS;
  599. struct TEEC_Operation op;
  600. // Copy parameters
  601. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  602. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  603. memset(tci, 0, TCI_LENGTH);
  604. tci->message.cmdHDCP.commandHeader.commandId = CMD_LOAD_KEY;
  605. tci->message.cmdHDCP.cmdBody.cryptokeys.type = TYPE_HDCP13_KEY;
  606. tci->message.cmdHDCP.cmdBody.cryptokeys.len = 289;
  607. memcpy(share_buffer + TCI_LENGTH, key, 289);
  608. memset(&op, 0, sizeof(struct TEEC_Operation));
  609. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  610. TEEC_NONE, TEEC_NONE);
  611. op.params[0].memref.parent = &sSharedMemoryMain;
  612. op.params[0].memref.size = TCI_LENGTH + 289;
  613. ret = TEEC_InvokeCommand(&sSession, CMD_LOAD_KEY, &op, NULL);
  614. if (ret != TEEC_SUCCESS)
  615. TLCERR("InvokeCommand ret = 0x%x", ret);
  616. return ret;
  617. #else
  618. return TEEC_SUCCESS;
  619. #endif
  620. }
  621. int tee_hdcp2_setKey(uint8_t *kpubdcp, uint8_t *lc128)
  622. {
  623. #ifdef HDCP_SETKEY_FROM_KERNEL
  624. uint32_t ret = TEEC_SUCCESS;
  625. struct TEEC_Operation op;
  626. // Copy parameters
  627. struct tci_t *tci = (struct tci_t *)sSharedMemoryMain.buffer;
  628. uint8_t *share_buffer = (uint8_t *)sSharedMemoryMain.buffer;
  629. uint32_t len = 385 + 16;
  630. memset(tci, 0, TCI_LENGTH);
  631. tci->message.cmdHDCP.commandHeader.commandId = CMD_LOAD_KEY;
  632. tci->message.cmdHDCP.cmdBody.cryptokeys.type = TYPE_HDCP22_KEY;
  633. tci->message.cmdHDCP.cmdBody.cryptokeys.len = len;
  634. memcpy(share_buffer + TCI_LENGTH, kpubdcp, 385);
  635. memcpy(share_buffer + TCI_LENGTH + 385, lc128, 16);
  636. memset(&op, 0, sizeof(struct TEEC_Operation));
  637. op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, TEEC_NONE,
  638. TEEC_NONE, TEEC_NONE);
  639. op.params[0].memref.parent = &sSharedMemoryMain;
  640. op.params[0].memref.size = TCI_LENGTH + len;
  641. ret = TEEC_InvokeCommand(&sSession, CMD_LOAD_KEY, &op, NULL);
  642. if (ret != TEEC_SUCCESS)
  643. TLCERR("InvokeCommand ret = 0x%x", ret);
  644. return ret;
  645. #else
  646. return TEEC_SUCCESS;
  647. #endif
  648. }