socfpga_sip_fcs.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. /*
  2. * Copyright (c) 2020-2022, Intel Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <lib/mmio.h>
  8. #include "socfpga_fcs.h"
  9. #include "socfpga_mailbox.h"
  10. #include "socfpga_sip_svc.h"
  11. /* FCS static variables */
  12. static fcs_crypto_service_aes_data fcs_aes_init_payload;
  13. static fcs_crypto_service_data fcs_sha_get_digest_param;
  14. static fcs_crypto_service_data fcs_sha_mac_verify_param;
  15. static fcs_crypto_service_data fcs_ecdsa_hash_sign_param;
  16. static fcs_crypto_service_data fcs_ecdsa_hash_sig_verify_param;
  17. static fcs_crypto_service_data fcs_sha2_data_sign_param;
  18. static fcs_crypto_service_data fcs_sha2_data_sig_verify_param;
  19. static fcs_crypto_service_data fcs_ecdsa_get_pubkey_param;
  20. static fcs_crypto_service_data fcs_ecdh_request_param;
  21. bool is_size_4_bytes_aligned(uint32_t size)
  22. {
  23. if ((size % MBOX_WORD_BYTE) != 0U) {
  24. return false;
  25. } else {
  26. return true;
  27. }
  28. }
  29. static bool is_8_bytes_aligned(uint32_t data)
  30. {
  31. if ((data % (MBOX_WORD_BYTE * 2U)) != 0U) {
  32. return false;
  33. } else {
  34. return true;
  35. }
  36. }
  37. static bool is_32_bytes_aligned(uint32_t data)
  38. {
  39. if ((data % (8U * MBOX_WORD_BYTE)) != 0U) {
  40. return false;
  41. } else {
  42. return true;
  43. }
  44. }
  45. static int intel_fcs_crypto_service_init(uint32_t session_id,
  46. uint32_t context_id, uint32_t key_id,
  47. uint32_t param_size, uint64_t param_data,
  48. fcs_crypto_service_data *data_addr,
  49. uint32_t *mbox_error)
  50. {
  51. if (mbox_error == NULL) {
  52. return INTEL_SIP_SMC_STATUS_REJECTED;
  53. }
  54. if (param_size != 4) {
  55. return INTEL_SIP_SMC_STATUS_REJECTED;
  56. }
  57. memset(data_addr, 0, sizeof(fcs_crypto_service_data));
  58. data_addr->session_id = session_id;
  59. data_addr->context_id = context_id;
  60. data_addr->key_id = key_id;
  61. data_addr->crypto_param_size = param_size;
  62. data_addr->crypto_param = param_data;
  63. data_addr->is_updated = 0;
  64. *mbox_error = 0;
  65. return INTEL_SIP_SMC_STATUS_OK;
  66. }
  67. uint32_t intel_fcs_random_number_gen(uint64_t addr, uint64_t *ret_size,
  68. uint32_t *mbox_error)
  69. {
  70. int status;
  71. unsigned int i;
  72. unsigned int resp_len = FCS_RANDOM_WORD_SIZE;
  73. uint32_t random_data[FCS_RANDOM_WORD_SIZE] = {0U};
  74. if (!is_address_in_ddr_range(addr, FCS_RANDOM_BYTE_SIZE)) {
  75. return INTEL_SIP_SMC_STATUS_REJECTED;
  76. }
  77. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_RANDOM_GEN, NULL, 0U,
  78. CMD_CASUAL, random_data, &resp_len);
  79. if (status < 0) {
  80. *mbox_error = -status;
  81. return INTEL_SIP_SMC_STATUS_ERROR;
  82. }
  83. if (resp_len != FCS_RANDOM_WORD_SIZE) {
  84. *mbox_error = GENERIC_RESPONSE_ERROR;
  85. return INTEL_SIP_SMC_STATUS_ERROR;
  86. }
  87. *ret_size = FCS_RANDOM_BYTE_SIZE;
  88. for (i = 0U; i < FCS_RANDOM_WORD_SIZE; i++) {
  89. mmio_write_32(addr, random_data[i]);
  90. addr += MBOX_WORD_BYTE;
  91. }
  92. flush_dcache_range(addr - *ret_size, *ret_size);
  93. return INTEL_SIP_SMC_STATUS_OK;
  94. }
  95. int intel_fcs_random_number_gen_ext(uint32_t session_id, uint32_t context_id,
  96. uint32_t size, uint32_t *send_id)
  97. {
  98. int status;
  99. uint32_t payload_size;
  100. uint32_t crypto_header;
  101. if (size > (FCS_RANDOM_EXT_MAX_WORD_SIZE *
  102. MBOX_WORD_BYTE) || size == 0U) {
  103. return INTEL_SIP_SMC_STATUS_REJECTED;
  104. }
  105. if (!is_size_4_bytes_aligned(size)) {
  106. return INTEL_SIP_SMC_STATUS_REJECTED;
  107. }
  108. crypto_header = (FCS_CS_FIELD_FLAG_INIT | FCS_CS_FIELD_FLAG_FINALIZE) <<
  109. FCS_CS_FIELD_FLAG_OFFSET;
  110. fcs_rng_payload payload = {
  111. session_id,
  112. context_id,
  113. crypto_header,
  114. size
  115. };
  116. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  117. status = mailbox_send_cmd_async(send_id, MBOX_FCS_RANDOM_GEN,
  118. (uint32_t *) &payload, payload_size,
  119. CMD_INDIRECT);
  120. if (status < 0) {
  121. return INTEL_SIP_SMC_STATUS_ERROR;
  122. }
  123. return INTEL_SIP_SMC_STATUS_OK;
  124. }
  125. uint32_t intel_fcs_send_cert(uint64_t addr, uint64_t size,
  126. uint32_t *send_id)
  127. {
  128. int status;
  129. if (!is_address_in_ddr_range(addr, size)) {
  130. return INTEL_SIP_SMC_STATUS_REJECTED;
  131. }
  132. if (!is_size_4_bytes_aligned(size)) {
  133. return INTEL_SIP_SMC_STATUS_REJECTED;
  134. }
  135. status = mailbox_send_cmd_async(send_id, MBOX_CMD_VAB_SRC_CERT,
  136. (uint32_t *)addr, size / MBOX_WORD_BYTE,
  137. CMD_DIRECT);
  138. flush_dcache_range(addr, size);
  139. if (status < 0) {
  140. return INTEL_SIP_SMC_STATUS_ERROR;
  141. }
  142. return INTEL_SIP_SMC_STATUS_OK;
  143. }
  144. uint32_t intel_fcs_get_provision_data(uint32_t *send_id)
  145. {
  146. int status;
  147. status = mailbox_send_cmd_async(send_id, MBOX_FCS_GET_PROVISION,
  148. NULL, 0U, CMD_DIRECT);
  149. if (status < 0) {
  150. return INTEL_SIP_SMC_STATUS_ERROR;
  151. }
  152. return INTEL_SIP_SMC_STATUS_OK;
  153. }
  154. uint32_t intel_fcs_cntr_set_preauth(uint8_t counter_type, int32_t counter_value,
  155. uint32_t test_bit, uint32_t *mbox_error)
  156. {
  157. int status;
  158. uint32_t first_word;
  159. uint32_t payload_size;
  160. if ((test_bit != MBOX_TEST_BIT) &&
  161. (test_bit != 0)) {
  162. return INTEL_SIP_SMC_STATUS_REJECTED;
  163. }
  164. if ((counter_type < FCS_BIG_CNTR_SEL) ||
  165. (counter_type > FCS_SVN_CNTR_3_SEL)) {
  166. return INTEL_SIP_SMC_STATUS_REJECTED;
  167. }
  168. if ((counter_type == FCS_BIG_CNTR_SEL) &&
  169. (counter_value > FCS_BIG_CNTR_VAL_MAX)) {
  170. return INTEL_SIP_SMC_STATUS_REJECTED;
  171. }
  172. if ((counter_type >= FCS_SVN_CNTR_0_SEL) &&
  173. (counter_type <= FCS_SVN_CNTR_3_SEL) &&
  174. (counter_value > FCS_SVN_CNTR_VAL_MAX)) {
  175. return INTEL_SIP_SMC_STATUS_REJECTED;
  176. }
  177. first_word = test_bit | counter_type;
  178. fcs_cntr_set_preauth_payload payload = {
  179. first_word,
  180. counter_value
  181. };
  182. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  183. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_CNTR_SET_PREAUTH,
  184. (uint32_t *) &payload, payload_size,
  185. CMD_CASUAL, NULL, NULL);
  186. if (status < 0) {
  187. *mbox_error = -status;
  188. return INTEL_SIP_SMC_STATUS_ERROR;
  189. }
  190. return INTEL_SIP_SMC_STATUS_OK;
  191. }
  192. uint32_t intel_fcs_encryption(uint32_t src_addr, uint32_t src_size,
  193. uint32_t dst_addr, uint32_t dst_size, uint32_t *send_id)
  194. {
  195. int status;
  196. uint32_t load_size;
  197. fcs_encrypt_payload payload = {
  198. FCS_ENCRYPTION_DATA_0,
  199. src_addr,
  200. src_size,
  201. dst_addr,
  202. dst_size };
  203. load_size = sizeof(payload) / MBOX_WORD_BYTE;
  204. if (!is_address_in_ddr_range(src_addr, src_size) ||
  205. !is_address_in_ddr_range(dst_addr, dst_size)) {
  206. return INTEL_SIP_SMC_STATUS_REJECTED;
  207. }
  208. if (!is_size_4_bytes_aligned(src_size)) {
  209. return INTEL_SIP_SMC_STATUS_REJECTED;
  210. }
  211. status = mailbox_send_cmd_async(send_id, MBOX_FCS_ENCRYPT_REQ,
  212. (uint32_t *) &payload, load_size,
  213. CMD_INDIRECT);
  214. inv_dcache_range(dst_addr, dst_size);
  215. if (status < 0) {
  216. return INTEL_SIP_SMC_STATUS_REJECTED;
  217. }
  218. return INTEL_SIP_SMC_STATUS_OK;
  219. }
  220. uint32_t intel_fcs_decryption(uint32_t src_addr, uint32_t src_size,
  221. uint32_t dst_addr, uint32_t dst_size, uint32_t *send_id)
  222. {
  223. int status;
  224. uint32_t load_size;
  225. uintptr_t id_offset;
  226. id_offset = src_addr + FCS_OWNER_ID_OFFSET;
  227. fcs_decrypt_payload payload = {
  228. FCS_DECRYPTION_DATA_0,
  229. {mmio_read_32(id_offset),
  230. mmio_read_32(id_offset + MBOX_WORD_BYTE)},
  231. src_addr,
  232. src_size,
  233. dst_addr,
  234. dst_size };
  235. load_size = sizeof(payload) / MBOX_WORD_BYTE;
  236. if (!is_address_in_ddr_range(src_addr, src_size) ||
  237. !is_address_in_ddr_range(dst_addr, dst_size)) {
  238. return INTEL_SIP_SMC_STATUS_REJECTED;
  239. }
  240. if (!is_size_4_bytes_aligned(src_size)) {
  241. return INTEL_SIP_SMC_STATUS_REJECTED;
  242. }
  243. status = mailbox_send_cmd_async(send_id, MBOX_FCS_DECRYPT_REQ,
  244. (uint32_t *) &payload, load_size,
  245. CMD_INDIRECT);
  246. inv_dcache_range(dst_addr, dst_size);
  247. if (status < 0) {
  248. return INTEL_SIP_SMC_STATUS_REJECTED;
  249. }
  250. return INTEL_SIP_SMC_STATUS_OK;
  251. }
  252. int intel_fcs_encryption_ext(uint32_t session_id, uint32_t context_id,
  253. uint32_t src_addr, uint32_t src_size,
  254. uint32_t dst_addr, uint32_t *dst_size, uint32_t *mbox_error)
  255. {
  256. int status;
  257. uint32_t payload_size;
  258. uint32_t resp_len = FCS_CRYPTION_RESP_WORD_SIZE;
  259. uint32_t resp_data[FCS_CRYPTION_RESP_WORD_SIZE] = {0U};
  260. if ((dst_size == NULL) || (mbox_error == NULL)) {
  261. return INTEL_SIP_SMC_STATUS_REJECTED;
  262. }
  263. if (!is_address_in_ddr_range(src_addr, src_size) ||
  264. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  265. return INTEL_SIP_SMC_STATUS_REJECTED;
  266. }
  267. if (!is_size_4_bytes_aligned(src_size)) {
  268. return INTEL_SIP_SMC_STATUS_REJECTED;
  269. }
  270. fcs_encrypt_ext_payload payload = {
  271. session_id,
  272. context_id,
  273. FCS_CRYPTION_CRYPTO_HEADER,
  274. src_addr,
  275. src_size,
  276. dst_addr,
  277. *dst_size
  278. };
  279. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  280. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_ENCRYPT_REQ,
  281. (uint32_t *) &payload, payload_size,
  282. CMD_CASUAL, resp_data, &resp_len);
  283. if (status < 0) {
  284. *mbox_error = -status;
  285. return INTEL_SIP_SMC_STATUS_ERROR;
  286. }
  287. if (resp_len != FCS_CRYPTION_RESP_WORD_SIZE) {
  288. *mbox_error = MBOX_RET_ERROR;
  289. return INTEL_SIP_SMC_STATUS_ERROR;
  290. }
  291. *dst_size = resp_data[FCS_CRYPTION_RESP_SIZE_OFFSET];
  292. inv_dcache_range(dst_addr, *dst_size);
  293. return INTEL_SIP_SMC_STATUS_OK;
  294. }
  295. int intel_fcs_decryption_ext(uint32_t session_id, uint32_t context_id,
  296. uint32_t src_addr, uint32_t src_size,
  297. uint32_t dst_addr, uint32_t *dst_size, uint32_t *mbox_error)
  298. {
  299. int status;
  300. uintptr_t id_offset;
  301. uint32_t payload_size;
  302. uint32_t resp_len = FCS_CRYPTION_RESP_WORD_SIZE;
  303. uint32_t resp_data[FCS_CRYPTION_RESP_WORD_SIZE] = {0U};
  304. if ((dst_size == NULL) || (mbox_error == NULL)) {
  305. return INTEL_SIP_SMC_STATUS_REJECTED;
  306. }
  307. if (!is_address_in_ddr_range(src_addr, src_size) ||
  308. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  309. return INTEL_SIP_SMC_STATUS_REJECTED;
  310. }
  311. if (!is_size_4_bytes_aligned(src_size)) {
  312. return INTEL_SIP_SMC_STATUS_REJECTED;
  313. }
  314. id_offset = src_addr + FCS_OWNER_ID_OFFSET;
  315. fcs_decrypt_ext_payload payload = {
  316. session_id,
  317. context_id,
  318. FCS_CRYPTION_CRYPTO_HEADER,
  319. {mmio_read_32(id_offset),
  320. mmio_read_32(id_offset + MBOX_WORD_BYTE)},
  321. src_addr,
  322. src_size,
  323. dst_addr,
  324. *dst_size
  325. };
  326. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  327. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_DECRYPT_REQ,
  328. (uint32_t *) &payload, payload_size,
  329. CMD_CASUAL, resp_data, &resp_len);
  330. if (status < 0) {
  331. *mbox_error = -status;
  332. return INTEL_SIP_SMC_STATUS_ERROR;
  333. }
  334. if (resp_len != FCS_CRYPTION_RESP_WORD_SIZE) {
  335. *mbox_error = MBOX_RET_ERROR;
  336. return INTEL_SIP_SMC_STATUS_ERROR;
  337. }
  338. *dst_size = resp_data[FCS_CRYPTION_RESP_SIZE_OFFSET];
  339. inv_dcache_range(dst_addr, *dst_size);
  340. return INTEL_SIP_SMC_STATUS_OK;
  341. }
  342. int intel_fcs_sigma_teardown(uint32_t session_id, uint32_t *mbox_error)
  343. {
  344. int status;
  345. if ((session_id != PSGSIGMA_SESSION_ID_ONE) &&
  346. (session_id != PSGSIGMA_UNKNOWN_SESSION)) {
  347. return INTEL_SIP_SMC_STATUS_REJECTED;
  348. }
  349. psgsigma_teardown_msg message = {
  350. RESERVED_AS_ZERO,
  351. PSGSIGMA_TEARDOWN_MAGIC,
  352. session_id
  353. };
  354. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_PSG_SIGMA_TEARDOWN,
  355. (uint32_t *) &message, sizeof(message) / MBOX_WORD_BYTE,
  356. CMD_CASUAL, NULL, NULL);
  357. if (status < 0) {
  358. *mbox_error = -status;
  359. return INTEL_SIP_SMC_STATUS_ERROR;
  360. }
  361. return INTEL_SIP_SMC_STATUS_OK;
  362. }
  363. int intel_fcs_chip_id(uint32_t *id_low, uint32_t *id_high, uint32_t *mbox_error)
  364. {
  365. int status;
  366. uint32_t load_size;
  367. uint32_t chip_id[2];
  368. load_size = sizeof(chip_id) / MBOX_WORD_BYTE;
  369. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_GET_CHIPID, NULL,
  370. 0U, CMD_CASUAL, (uint32_t *) chip_id, &load_size);
  371. if (status < 0) {
  372. *mbox_error = -status;
  373. return INTEL_SIP_SMC_STATUS_ERROR;
  374. }
  375. *id_low = chip_id[0];
  376. *id_high = chip_id[1];
  377. return INTEL_SIP_SMC_STATUS_OK;
  378. }
  379. int intel_fcs_attestation_subkey(uint64_t src_addr, uint32_t src_size,
  380. uint64_t dst_addr, uint32_t *dst_size, uint32_t *mbox_error)
  381. {
  382. int status;
  383. uint32_t send_size = src_size / MBOX_WORD_BYTE;
  384. uint32_t ret_size = *dst_size / MBOX_WORD_BYTE;
  385. if (!is_address_in_ddr_range(src_addr, src_size) ||
  386. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  387. return INTEL_SIP_SMC_STATUS_REJECTED;
  388. }
  389. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_ATTESTATION_SUBKEY,
  390. (uint32_t *) src_addr, send_size, CMD_CASUAL,
  391. (uint32_t *) dst_addr, &ret_size);
  392. if (status < 0) {
  393. *mbox_error = -status;
  394. return INTEL_SIP_SMC_STATUS_ERROR;
  395. }
  396. *dst_size = ret_size * MBOX_WORD_BYTE;
  397. flush_dcache_range(dst_addr, *dst_size);
  398. return INTEL_SIP_SMC_STATUS_OK;
  399. }
  400. int intel_fcs_get_measurement(uint64_t src_addr, uint32_t src_size,
  401. uint64_t dst_addr, uint32_t *dst_size, uint32_t *mbox_error)
  402. {
  403. int status;
  404. uint32_t send_size = src_size / MBOX_WORD_BYTE;
  405. uint32_t ret_size = *dst_size / MBOX_WORD_BYTE;
  406. if (!is_address_in_ddr_range(src_addr, src_size) ||
  407. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  408. return INTEL_SIP_SMC_STATUS_REJECTED;
  409. }
  410. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_GET_MEASUREMENT,
  411. (uint32_t *) src_addr, send_size, CMD_CASUAL,
  412. (uint32_t *) dst_addr, &ret_size);
  413. if (status < 0) {
  414. *mbox_error = -status;
  415. return INTEL_SIP_SMC_STATUS_ERROR;
  416. }
  417. *dst_size = ret_size * MBOX_WORD_BYTE;
  418. flush_dcache_range(dst_addr, *dst_size);
  419. return INTEL_SIP_SMC_STATUS_OK;
  420. }
  421. uint32_t intel_fcs_get_rom_patch_sha384(uint64_t addr, uint64_t *ret_size,
  422. uint32_t *mbox_error)
  423. {
  424. int status;
  425. unsigned int resp_len = FCS_SHA384_WORD_SIZE;
  426. if (!is_address_in_ddr_range(addr, FCS_SHA384_BYTE_SIZE)) {
  427. return INTEL_SIP_SMC_STATUS_REJECTED;
  428. }
  429. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_GET_ROM_PATCH_SHA384, NULL, 0U,
  430. CMD_CASUAL, (uint32_t *) addr, &resp_len);
  431. if (status < 0) {
  432. *mbox_error = -status;
  433. return INTEL_SIP_SMC_STATUS_ERROR;
  434. }
  435. if (resp_len != FCS_SHA384_WORD_SIZE) {
  436. *mbox_error = GENERIC_RESPONSE_ERROR;
  437. return INTEL_SIP_SMC_STATUS_ERROR;
  438. }
  439. *ret_size = FCS_SHA384_BYTE_SIZE;
  440. flush_dcache_range(addr, *ret_size);
  441. return INTEL_SIP_SMC_STATUS_OK;
  442. }
  443. int intel_fcs_get_attestation_cert(uint32_t cert_request, uint64_t dst_addr,
  444. uint32_t *dst_size, uint32_t *mbox_error)
  445. {
  446. int status;
  447. uint32_t ret_size = *dst_size / MBOX_WORD_BYTE;
  448. if (mbox_error == NULL) {
  449. return INTEL_SIP_SMC_STATUS_REJECTED;
  450. }
  451. if (cert_request < FCS_ATTEST_FIRMWARE_CERT ||
  452. cert_request > FCS_ATTEST_CERT_MAX_REQ_PARAM) {
  453. return INTEL_SIP_SMC_STATUS_REJECTED;
  454. }
  455. if (!is_address_in_ddr_range(dst_addr, *dst_size)) {
  456. return INTEL_SIP_SMC_STATUS_REJECTED;
  457. }
  458. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_GET_ATTESTATION_CERT,
  459. (uint32_t *) &cert_request, 1U, CMD_CASUAL,
  460. (uint32_t *) dst_addr, &ret_size);
  461. if (status < 0) {
  462. *mbox_error = -status;
  463. return INTEL_SIP_SMC_STATUS_ERROR;
  464. }
  465. *dst_size = ret_size * MBOX_WORD_BYTE;
  466. flush_dcache_range(dst_addr, *dst_size);
  467. return INTEL_SIP_SMC_STATUS_OK;
  468. }
  469. int intel_fcs_create_cert_on_reload(uint32_t cert_request,
  470. uint32_t *mbox_error)
  471. {
  472. int status;
  473. if (mbox_error == NULL) {
  474. return INTEL_SIP_SMC_STATUS_REJECTED;
  475. }
  476. if (cert_request < FCS_ATTEST_FIRMWARE_CERT ||
  477. cert_request > FCS_ATTEST_CERT_MAX_REQ_PARAM) {
  478. return INTEL_SIP_SMC_STATUS_REJECTED;
  479. }
  480. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_CREATE_CERT_ON_RELOAD,
  481. (uint32_t *) &cert_request, 1U, CMD_CASUAL,
  482. NULL, NULL);
  483. if (status < 0) {
  484. *mbox_error = -status;
  485. return INTEL_SIP_SMC_STATUS_ERROR;
  486. }
  487. return INTEL_SIP_SMC_STATUS_OK;
  488. }
  489. int intel_fcs_open_crypto_service_session(uint32_t *session_id,
  490. uint32_t *mbox_error)
  491. {
  492. int status;
  493. uint32_t resp_len = 1U;
  494. if ((session_id == NULL) || (mbox_error == NULL)) {
  495. return INTEL_SIP_SMC_STATUS_REJECTED;
  496. }
  497. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_OPEN_CS_SESSION,
  498. NULL, 0U, CMD_CASUAL, session_id, &resp_len);
  499. if (status < 0) {
  500. *mbox_error = -status;
  501. return INTEL_SIP_SMC_STATUS_ERROR;
  502. }
  503. return INTEL_SIP_SMC_STATUS_OK;
  504. }
  505. int intel_fcs_close_crypto_service_session(uint32_t session_id,
  506. uint32_t *mbox_error)
  507. {
  508. int status;
  509. if (mbox_error == NULL) {
  510. return INTEL_SIP_SMC_STATUS_REJECTED;
  511. }
  512. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_CLOSE_CS_SESSION,
  513. &session_id, 1U, CMD_CASUAL, NULL, NULL);
  514. if (status < 0) {
  515. *mbox_error = -status;
  516. return INTEL_SIP_SMC_STATUS_ERROR;
  517. }
  518. return INTEL_SIP_SMC_STATUS_OK;
  519. }
  520. int intel_fcs_import_crypto_service_key(uint64_t src_addr, uint32_t src_size,
  521. uint32_t *send_id)
  522. {
  523. int status;
  524. if (src_size > (FCS_CS_KEY_OBJ_MAX_WORD_SIZE *
  525. MBOX_WORD_BYTE)) {
  526. return INTEL_SIP_SMC_STATUS_REJECTED;
  527. }
  528. if (!is_address_in_ddr_range(src_addr, src_size)) {
  529. return INTEL_SIP_SMC_STATUS_REJECTED;
  530. }
  531. status = mailbox_send_cmd_async(send_id, MBOX_FCS_IMPORT_CS_KEY,
  532. (uint32_t *)src_addr, src_size / MBOX_WORD_BYTE,
  533. CMD_INDIRECT);
  534. if (status < 0) {
  535. return INTEL_SIP_SMC_STATUS_ERROR;
  536. }
  537. return INTEL_SIP_SMC_STATUS_OK;
  538. }
  539. int intel_fcs_export_crypto_service_key(uint32_t session_id, uint32_t key_id,
  540. uint64_t dst_addr, uint32_t *dst_size,
  541. uint32_t *mbox_error)
  542. {
  543. int status;
  544. uint32_t i;
  545. uint32_t payload_size;
  546. uint32_t resp_len = FCS_CS_KEY_OBJ_MAX_WORD_SIZE;
  547. uint32_t resp_data[FCS_CS_KEY_OBJ_MAX_WORD_SIZE] = {0U};
  548. uint32_t op_status = 0U;
  549. if ((dst_size == NULL) || (mbox_error == NULL)) {
  550. return INTEL_SIP_SMC_STATUS_REJECTED;
  551. }
  552. if (!is_address_in_ddr_range(dst_addr, *dst_size)) {
  553. return INTEL_SIP_SMC_STATUS_REJECTED;
  554. }
  555. fcs_cs_key_payload payload = {
  556. session_id,
  557. RESERVED_AS_ZERO,
  558. RESERVED_AS_ZERO,
  559. key_id
  560. };
  561. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  562. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_EXPORT_CS_KEY,
  563. (uint32_t *) &payload, payload_size,
  564. CMD_CASUAL, resp_data, &resp_len);
  565. if (resp_len > 0) {
  566. op_status = resp_data[0] & FCS_CS_KEY_RESP_STATUS_MASK;
  567. }
  568. if (status < 0) {
  569. *mbox_error = (-status) | (op_status << FCS_CS_KEY_RESP_STATUS_OFFSET);
  570. return INTEL_SIP_SMC_STATUS_ERROR;
  571. }
  572. if (resp_len > 1) {
  573. /* Export key object is start at second response data */
  574. *dst_size = (resp_len - 1) * MBOX_WORD_BYTE;
  575. for (i = 1U; i < resp_len; i++) {
  576. mmio_write_32(dst_addr, resp_data[i]);
  577. dst_addr += MBOX_WORD_BYTE;
  578. }
  579. flush_dcache_range(dst_addr - *dst_size, *dst_size);
  580. } else {
  581. /* Unexpected response, missing key object in response */
  582. *mbox_error = MBOX_RET_ERROR;
  583. return INTEL_SIP_SMC_STATUS_ERROR;
  584. }
  585. return INTEL_SIP_SMC_STATUS_OK;
  586. }
  587. int intel_fcs_remove_crypto_service_key(uint32_t session_id, uint32_t key_id,
  588. uint32_t *mbox_error)
  589. {
  590. int status;
  591. uint32_t payload_size;
  592. uint32_t resp_len = 1U;
  593. uint32_t resp_data = 0U;
  594. uint32_t op_status = 0U;
  595. if (mbox_error == NULL) {
  596. return INTEL_SIP_SMC_STATUS_REJECTED;
  597. }
  598. fcs_cs_key_payload payload = {
  599. session_id,
  600. RESERVED_AS_ZERO,
  601. RESERVED_AS_ZERO,
  602. key_id
  603. };
  604. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  605. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_REMOVE_CS_KEY,
  606. (uint32_t *) &payload, payload_size,
  607. CMD_CASUAL, &resp_data, &resp_len);
  608. if (resp_len > 0) {
  609. op_status = resp_data & FCS_CS_KEY_RESP_STATUS_MASK;
  610. }
  611. if (status < 0) {
  612. *mbox_error = (-status) | (op_status << FCS_CS_KEY_RESP_STATUS_OFFSET);
  613. return INTEL_SIP_SMC_STATUS_ERROR;
  614. }
  615. return INTEL_SIP_SMC_STATUS_OK;
  616. }
  617. int intel_fcs_get_crypto_service_key_info(uint32_t session_id, uint32_t key_id,
  618. uint64_t dst_addr, uint32_t *dst_size,
  619. uint32_t *mbox_error)
  620. {
  621. int status;
  622. uint32_t payload_size;
  623. uint32_t resp_len = FCS_CS_KEY_INFO_MAX_WORD_SIZE;
  624. uint32_t op_status = 0U;
  625. if ((dst_size == NULL) || (mbox_error == NULL)) {
  626. return INTEL_SIP_SMC_STATUS_REJECTED;
  627. }
  628. if (!is_address_in_ddr_range(dst_addr, *dst_size)) {
  629. return INTEL_SIP_SMC_STATUS_REJECTED;
  630. }
  631. fcs_cs_key_payload payload = {
  632. session_id,
  633. RESERVED_AS_ZERO,
  634. RESERVED_AS_ZERO,
  635. key_id
  636. };
  637. payload_size = sizeof(payload) / MBOX_WORD_BYTE;
  638. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_GET_CS_KEY_INFO,
  639. (uint32_t *) &payload, payload_size,
  640. CMD_CASUAL, (uint32_t *) dst_addr, &resp_len);
  641. if (resp_len > 0) {
  642. op_status = mmio_read_32(dst_addr) &
  643. FCS_CS_KEY_RESP_STATUS_MASK;
  644. }
  645. if (status < 0) {
  646. *mbox_error = (-status) | (op_status << FCS_CS_KEY_RESP_STATUS_OFFSET);
  647. return INTEL_SIP_SMC_STATUS_ERROR;
  648. }
  649. *dst_size = resp_len * MBOX_WORD_BYTE;
  650. flush_dcache_range(dst_addr, *dst_size);
  651. return INTEL_SIP_SMC_STATUS_OK;
  652. }
  653. int intel_fcs_get_digest_init(uint32_t session_id, uint32_t context_id,
  654. uint32_t key_id, uint32_t param_size,
  655. uint64_t param_data, uint32_t *mbox_error)
  656. {
  657. return intel_fcs_crypto_service_init(session_id, context_id,
  658. key_id, param_size, param_data,
  659. (void *) &fcs_sha_get_digest_param,
  660. mbox_error);
  661. }
  662. int intel_fcs_get_digest_update_finalize(uint32_t session_id,
  663. uint32_t context_id, uint32_t src_addr,
  664. uint32_t src_size, uint64_t dst_addr,
  665. uint32_t *dst_size, uint8_t is_finalised,
  666. uint32_t *mbox_error)
  667. {
  668. int status;
  669. uint32_t i;
  670. uint32_t flag;
  671. uint32_t crypto_header;
  672. uint32_t resp_len;
  673. uint32_t payload[FCS_GET_DIGEST_CMD_MAX_WORD_SIZE] = {0U};
  674. if (dst_size == NULL || mbox_error == NULL) {
  675. return INTEL_SIP_SMC_STATUS_REJECTED;
  676. }
  677. if (fcs_sha_get_digest_param.session_id != session_id ||
  678. fcs_sha_get_digest_param.context_id != context_id) {
  679. return INTEL_SIP_SMC_STATUS_REJECTED;
  680. }
  681. /* Source data must be 8 bytes aligned */
  682. if (!is_8_bytes_aligned(src_size)) {
  683. return INTEL_SIP_SMC_STATUS_REJECTED;
  684. }
  685. if (!is_address_in_ddr_range(src_addr, src_size) ||
  686. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  687. return INTEL_SIP_SMC_STATUS_REJECTED;
  688. }
  689. resp_len = *dst_size / MBOX_WORD_BYTE;
  690. /* Prepare crypto header */
  691. flag = 0;
  692. if (fcs_sha_get_digest_param.is_updated) {
  693. fcs_sha_get_digest_param.crypto_param_size = 0;
  694. } else {
  695. flag |= FCS_CS_FIELD_FLAG_INIT;
  696. }
  697. if (is_finalised != 0U) {
  698. flag |= FCS_CS_FIELD_FLAG_FINALIZE;
  699. } else {
  700. flag |= FCS_CS_FIELD_FLAG_UPDATE;
  701. fcs_sha_get_digest_param.is_updated = 1;
  702. }
  703. crypto_header = ((flag << FCS_CS_FIELD_FLAG_OFFSET) |
  704. (fcs_sha_get_digest_param.crypto_param_size &
  705. FCS_CS_FIELD_SIZE_MASK));
  706. /* Prepare command payload */
  707. i = 0;
  708. payload[i] = fcs_sha_get_digest_param.session_id;
  709. i++;
  710. payload[i] = fcs_sha_get_digest_param.context_id;
  711. i++;
  712. payload[i] = crypto_header;
  713. i++;
  714. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  715. FCS_CS_FIELD_FLAG_INIT) {
  716. payload[i] = fcs_sha_get_digest_param.key_id;
  717. i++;
  718. /* Crypto parameters */
  719. payload[i] = fcs_sha_get_digest_param.crypto_param
  720. & INTEL_SIP_SMC_FCS_SHA_MODE_MASK;
  721. payload[i] |= ((fcs_sha_get_digest_param.crypto_param
  722. >> INTEL_SIP_SMC_FCS_DIGEST_SIZE_OFFSET)
  723. & INTEL_SIP_SMC_FCS_DIGEST_SIZE_MASK)
  724. << FCS_SHA_HMAC_CRYPTO_PARAM_SIZE_OFFSET;
  725. i++;
  726. }
  727. /* Data source address and size */
  728. payload[i] = src_addr;
  729. i++;
  730. payload[i] = src_size;
  731. i++;
  732. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_GET_DIGEST_REQ,
  733. payload, i, CMD_CASUAL,
  734. (uint32_t *) dst_addr, &resp_len);
  735. if (is_finalised != 0U) {
  736. memset((void *)&fcs_sha_get_digest_param, 0,
  737. sizeof(fcs_crypto_service_data));
  738. }
  739. if (status < 0) {
  740. *mbox_error = -status;
  741. return INTEL_SIP_SMC_STATUS_ERROR;
  742. }
  743. *dst_size = resp_len * MBOX_WORD_BYTE;
  744. flush_dcache_range(dst_addr, *dst_size);
  745. return INTEL_SIP_SMC_STATUS_OK;
  746. }
  747. int intel_fcs_mac_verify_init(uint32_t session_id, uint32_t context_id,
  748. uint32_t key_id, uint32_t param_size,
  749. uint64_t param_data, uint32_t *mbox_error)
  750. {
  751. return intel_fcs_crypto_service_init(session_id, context_id,
  752. key_id, param_size, param_data,
  753. (void *) &fcs_sha_mac_verify_param,
  754. mbox_error);
  755. }
  756. int intel_fcs_mac_verify_update_finalize(uint32_t session_id,
  757. uint32_t context_id, uint32_t src_addr,
  758. uint32_t src_size, uint64_t dst_addr,
  759. uint32_t *dst_size, uint32_t data_size,
  760. uint8_t is_finalised, uint32_t *mbox_error)
  761. {
  762. int status;
  763. uint32_t i;
  764. uint32_t flag;
  765. uint32_t crypto_header;
  766. uint32_t resp_len;
  767. uint32_t payload[FCS_MAC_VERIFY_CMD_MAX_WORD_SIZE] = {0U};
  768. uintptr_t mac_offset;
  769. if (dst_size == NULL || mbox_error == NULL) {
  770. return INTEL_SIP_SMC_STATUS_REJECTED;
  771. }
  772. if (fcs_sha_mac_verify_param.session_id != session_id ||
  773. fcs_sha_mac_verify_param.context_id != context_id) {
  774. return INTEL_SIP_SMC_STATUS_REJECTED;
  775. }
  776. if (data_size > src_size) {
  777. return INTEL_SIP_SMC_STATUS_REJECTED;
  778. }
  779. if (!is_size_4_bytes_aligned(src_size) ||
  780. !is_8_bytes_aligned(data_size)) {
  781. return INTEL_SIP_SMC_STATUS_REJECTED;
  782. }
  783. if (!is_address_in_ddr_range(src_addr, src_size) ||
  784. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  785. return INTEL_SIP_SMC_STATUS_REJECTED;
  786. }
  787. resp_len = *dst_size / MBOX_WORD_BYTE;
  788. /* Prepare crypto header */
  789. flag = 0;
  790. if (fcs_sha_mac_verify_param.is_updated) {
  791. fcs_sha_mac_verify_param.crypto_param_size = 0;
  792. } else {
  793. flag |= FCS_CS_FIELD_FLAG_INIT;
  794. }
  795. if (is_finalised) {
  796. flag |= FCS_CS_FIELD_FLAG_FINALIZE;
  797. } else {
  798. flag |= FCS_CS_FIELD_FLAG_UPDATE;
  799. fcs_sha_mac_verify_param.is_updated = 1;
  800. }
  801. crypto_header = ((flag << FCS_CS_FIELD_FLAG_OFFSET) |
  802. (fcs_sha_mac_verify_param.crypto_param_size &
  803. FCS_CS_FIELD_SIZE_MASK));
  804. /* Prepare command payload */
  805. i = 0;
  806. payload[i] = fcs_sha_mac_verify_param.session_id;
  807. i++;
  808. payload[i] = fcs_sha_mac_verify_param.context_id;
  809. i++;
  810. payload[i] = crypto_header;
  811. i++;
  812. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  813. FCS_CS_FIELD_FLAG_INIT) {
  814. payload[i] = fcs_sha_mac_verify_param.key_id;
  815. i++;
  816. /* Crypto parameters */
  817. payload[i] = ((fcs_sha_mac_verify_param.crypto_param
  818. >> INTEL_SIP_SMC_FCS_DIGEST_SIZE_OFFSET)
  819. & INTEL_SIP_SMC_FCS_DIGEST_SIZE_MASK)
  820. << FCS_SHA_HMAC_CRYPTO_PARAM_SIZE_OFFSET;
  821. i++;
  822. }
  823. /* Data source address and size */
  824. payload[i] = src_addr;
  825. i++;
  826. payload[i] = data_size;
  827. i++;
  828. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  829. FCS_CS_FIELD_FLAG_FINALIZE) {
  830. /* Copy mac data to command */
  831. mac_offset = src_addr + data_size;
  832. memcpy((uint8_t *) &payload[i], (uint8_t *) mac_offset,
  833. src_size - data_size);
  834. i += (src_size - data_size) / MBOX_WORD_BYTE;
  835. }
  836. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_MAC_VERIFY_REQ,
  837. payload, i, CMD_CASUAL,
  838. (uint32_t *) dst_addr, &resp_len);
  839. if (is_finalised) {
  840. memset((void *)&fcs_sha_mac_verify_param, 0,
  841. sizeof(fcs_crypto_service_data));
  842. }
  843. if (status < 0) {
  844. *mbox_error = -status;
  845. return INTEL_SIP_SMC_STATUS_ERROR;
  846. }
  847. *dst_size = resp_len * MBOX_WORD_BYTE;
  848. flush_dcache_range(dst_addr, *dst_size);
  849. return INTEL_SIP_SMC_STATUS_OK;
  850. }
  851. int intel_fcs_ecdsa_hash_sign_init(uint32_t session_id, uint32_t context_id,
  852. uint32_t key_id, uint32_t param_size,
  853. uint64_t param_data, uint32_t *mbox_error)
  854. {
  855. return intel_fcs_crypto_service_init(session_id, context_id,
  856. key_id, param_size, param_data,
  857. (void *) &fcs_ecdsa_hash_sign_param,
  858. mbox_error);
  859. }
  860. int intel_fcs_ecdsa_hash_sign_finalize(uint32_t session_id, uint32_t context_id,
  861. uint32_t src_addr, uint32_t src_size,
  862. uint64_t dst_addr, uint32_t *dst_size,
  863. uint32_t *mbox_error)
  864. {
  865. int status;
  866. uint32_t i;
  867. uint32_t payload[FCS_ECDSA_HASH_SIGN_CMD_MAX_WORD_SIZE] = {0U};
  868. uint32_t resp_len;
  869. uintptr_t hash_data_addr;
  870. if ((dst_size == NULL) || (mbox_error == NULL)) {
  871. return INTEL_SIP_SMC_STATUS_REJECTED;
  872. }
  873. if (fcs_ecdsa_hash_sign_param.session_id != session_id ||
  874. fcs_ecdsa_hash_sign_param.context_id != context_id) {
  875. return INTEL_SIP_SMC_STATUS_REJECTED;
  876. }
  877. if (!is_address_in_ddr_range(src_addr, src_size) ||
  878. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  879. return INTEL_SIP_SMC_STATUS_REJECTED;
  880. }
  881. resp_len = *dst_size / MBOX_WORD_BYTE;
  882. /* Prepare command payload */
  883. /* Crypto header */
  884. i = 0;
  885. payload[i] = fcs_ecdsa_hash_sign_param.session_id;
  886. i++;
  887. payload[i] = fcs_ecdsa_hash_sign_param.context_id;
  888. i++;
  889. payload[i] = fcs_ecdsa_hash_sign_param.crypto_param_size
  890. & FCS_CS_FIELD_SIZE_MASK;
  891. payload[i] |= (FCS_CS_FIELD_FLAG_INIT | FCS_CS_FIELD_FLAG_UPDATE
  892. | FCS_CS_FIELD_FLAG_FINALIZE)
  893. << FCS_CS_FIELD_FLAG_OFFSET;
  894. i++;
  895. payload[i] = fcs_ecdsa_hash_sign_param.key_id;
  896. /* Crypto parameters */
  897. i++;
  898. payload[i] = fcs_ecdsa_hash_sign_param.crypto_param
  899. & INTEL_SIP_SMC_FCS_ECC_ALGO_MASK;
  900. /* Hash Data */
  901. i++;
  902. hash_data_addr = src_addr;
  903. memcpy((uint8_t *) &payload[i], (uint8_t *) hash_data_addr,
  904. src_size);
  905. i += src_size / MBOX_WORD_BYTE;
  906. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_ECDSA_HASH_SIGN_REQ,
  907. payload, i, CMD_CASUAL, (uint32_t *) dst_addr,
  908. &resp_len);
  909. memset((void *) &fcs_ecdsa_hash_sign_param,
  910. 0, sizeof(fcs_crypto_service_data));
  911. if (status < 0) {
  912. *mbox_error = -status;
  913. return INTEL_SIP_SMC_STATUS_ERROR;
  914. }
  915. *dst_size = resp_len * MBOX_WORD_BYTE;
  916. flush_dcache_range(dst_addr, *dst_size);
  917. return INTEL_SIP_SMC_STATUS_OK;
  918. }
  919. int intel_fcs_ecdsa_hash_sig_verify_init(uint32_t session_id, uint32_t context_id,
  920. uint32_t key_id, uint32_t param_size,
  921. uint64_t param_data, uint32_t *mbox_error)
  922. {
  923. return intel_fcs_crypto_service_init(session_id, context_id,
  924. key_id, param_size, param_data,
  925. (void *) &fcs_ecdsa_hash_sig_verify_param,
  926. mbox_error);
  927. }
  928. int intel_fcs_ecdsa_hash_sig_verify_finalize(uint32_t session_id, uint32_t context_id,
  929. uint32_t src_addr, uint32_t src_size,
  930. uint64_t dst_addr, uint32_t *dst_size,
  931. uint32_t *mbox_error)
  932. {
  933. int status;
  934. uint32_t i = 0;
  935. uint32_t payload[FCS_ECDSA_HASH_SIG_VERIFY_CMD_MAX_WORD_SIZE] = {0U};
  936. uint32_t resp_len;
  937. uintptr_t hash_sig_pubkey_addr;
  938. if ((dst_size == NULL) || (mbox_error == NULL)) {
  939. return INTEL_SIP_SMC_STATUS_REJECTED;
  940. }
  941. if (fcs_ecdsa_hash_sig_verify_param.session_id != session_id ||
  942. fcs_ecdsa_hash_sig_verify_param.context_id != context_id) {
  943. return INTEL_SIP_SMC_STATUS_REJECTED;
  944. }
  945. if (!is_address_in_ddr_range(src_addr, src_size) ||
  946. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  947. return INTEL_SIP_SMC_STATUS_REJECTED;
  948. }
  949. resp_len = *dst_size / MBOX_WORD_BYTE;
  950. /* Prepare command payload */
  951. /* Crypto header */
  952. i = 0;
  953. payload[i] = fcs_ecdsa_hash_sig_verify_param.session_id;
  954. i++;
  955. payload[i] = fcs_ecdsa_hash_sig_verify_param.context_id;
  956. i++;
  957. payload[i] = fcs_ecdsa_hash_sig_verify_param.crypto_param_size
  958. & FCS_CS_FIELD_SIZE_MASK;
  959. payload[i] |= (FCS_CS_FIELD_FLAG_INIT | FCS_CS_FIELD_FLAG_UPDATE
  960. | FCS_CS_FIELD_FLAG_FINALIZE)
  961. << FCS_CS_FIELD_FLAG_OFFSET;
  962. i++;
  963. payload[i] = fcs_ecdsa_hash_sig_verify_param.key_id;
  964. /* Crypto parameters */
  965. i++;
  966. payload[i] = fcs_ecdsa_hash_sig_verify_param.crypto_param
  967. & INTEL_SIP_SMC_FCS_ECC_ALGO_MASK;
  968. /* Hash Data Word, Signature Data Word and Public Key Data word */
  969. i++;
  970. hash_sig_pubkey_addr = src_addr;
  971. memcpy((uint8_t *) &payload[i],
  972. (uint8_t *) hash_sig_pubkey_addr, src_size);
  973. i += (src_size / MBOX_WORD_BYTE);
  974. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_ECDSA_HASH_SIG_VERIFY,
  975. payload, i, CMD_CASUAL, (uint32_t *) dst_addr,
  976. &resp_len);
  977. memset((void *)&fcs_ecdsa_hash_sig_verify_param,
  978. 0, sizeof(fcs_crypto_service_data));
  979. if (status < 0) {
  980. *mbox_error = -status;
  981. return INTEL_SIP_SMC_STATUS_ERROR;
  982. }
  983. *dst_size = resp_len * MBOX_WORD_BYTE;
  984. flush_dcache_range(dst_addr, *dst_size);
  985. return INTEL_SIP_SMC_STATUS_OK;
  986. }
  987. int intel_fcs_ecdsa_sha2_data_sign_init(uint32_t session_id,
  988. uint32_t context_id, uint32_t key_id,
  989. uint32_t param_size, uint64_t param_data,
  990. uint32_t *mbox_error)
  991. {
  992. return intel_fcs_crypto_service_init(session_id, context_id,
  993. key_id, param_size, param_data,
  994. (void *) &fcs_sha2_data_sign_param,
  995. mbox_error);
  996. }
  997. int intel_fcs_ecdsa_sha2_data_sign_update_finalize(uint32_t session_id,
  998. uint32_t context_id, uint32_t src_addr,
  999. uint32_t src_size, uint64_t dst_addr,
  1000. uint32_t *dst_size, uint8_t is_finalised,
  1001. uint32_t *mbox_error)
  1002. {
  1003. int status;
  1004. int i;
  1005. uint32_t flag;
  1006. uint32_t crypto_header;
  1007. uint32_t payload[FCS_ECDSA_SHA2_DATA_SIGN_CMD_MAX_WORD_SIZE] = {0U};
  1008. uint32_t resp_len;
  1009. if ((dst_size == NULL) || (mbox_error == NULL)) {
  1010. return INTEL_SIP_SMC_STATUS_REJECTED;
  1011. }
  1012. if (fcs_sha2_data_sign_param.session_id != session_id ||
  1013. fcs_sha2_data_sign_param.context_id != context_id) {
  1014. return INTEL_SIP_SMC_STATUS_REJECTED;
  1015. }
  1016. /* Source data must be 8 bytes aligned */
  1017. if (!is_8_bytes_aligned(src_size)) {
  1018. return INTEL_SIP_SMC_STATUS_REJECTED;
  1019. }
  1020. if (!is_address_in_ddr_range(src_addr, src_size) ||
  1021. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  1022. return INTEL_SIP_SMC_STATUS_REJECTED;
  1023. }
  1024. resp_len = *dst_size / MBOX_WORD_BYTE;
  1025. /* Prepare crypto header */
  1026. flag = 0;
  1027. if (fcs_sha2_data_sign_param.is_updated) {
  1028. fcs_sha2_data_sign_param.crypto_param_size = 0;
  1029. } else {
  1030. flag |= FCS_CS_FIELD_FLAG_INIT;
  1031. }
  1032. if (is_finalised != 0U) {
  1033. flag |= FCS_CS_FIELD_FLAG_FINALIZE;
  1034. } else {
  1035. flag |= FCS_CS_FIELD_FLAG_UPDATE;
  1036. fcs_sha2_data_sign_param.is_updated = 1;
  1037. }
  1038. crypto_header = (flag << FCS_CS_FIELD_FLAG_OFFSET) |
  1039. fcs_sha2_data_sign_param.crypto_param_size;
  1040. /* Prepare command payload */
  1041. i = 0;
  1042. payload[i] = fcs_sha2_data_sign_param.session_id;
  1043. i++;
  1044. payload[i] = fcs_sha2_data_sign_param.context_id;
  1045. i++;
  1046. payload[i] = crypto_header;
  1047. i++;
  1048. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  1049. FCS_CS_FIELD_FLAG_INIT) {
  1050. payload[i] = fcs_sha2_data_sign_param.key_id;
  1051. /* Crypto parameters */
  1052. i++;
  1053. payload[i] = fcs_sha2_data_sign_param.crypto_param
  1054. & INTEL_SIP_SMC_FCS_ECC_ALGO_MASK;
  1055. i++;
  1056. }
  1057. /* Data source address and size */
  1058. payload[i] = src_addr;
  1059. i++;
  1060. payload[i] = src_size;
  1061. i++;
  1062. status = mailbox_send_cmd(MBOX_JOB_ID,
  1063. MBOX_FCS_ECDSA_SHA2_DATA_SIGN_REQ, payload,
  1064. i, CMD_CASUAL, (uint32_t *) dst_addr,
  1065. &resp_len);
  1066. if (is_finalised != 0U) {
  1067. memset((void *)&fcs_sha2_data_sign_param, 0,
  1068. sizeof(fcs_crypto_service_data));
  1069. }
  1070. if (status < 0) {
  1071. *mbox_error = -status;
  1072. return INTEL_SIP_SMC_STATUS_ERROR;
  1073. }
  1074. *dst_size = resp_len * MBOX_WORD_BYTE;
  1075. flush_dcache_range(dst_addr, *dst_size);
  1076. return INTEL_SIP_SMC_STATUS_OK;
  1077. }
  1078. int intel_fcs_ecdsa_sha2_data_sig_verify_init(uint32_t session_id,
  1079. uint32_t context_id, uint32_t key_id,
  1080. uint32_t param_size, uint64_t param_data,
  1081. uint32_t *mbox_error)
  1082. {
  1083. return intel_fcs_crypto_service_init(session_id, context_id,
  1084. key_id, param_size, param_data,
  1085. (void *) &fcs_sha2_data_sig_verify_param,
  1086. mbox_error);
  1087. }
  1088. int intel_fcs_ecdsa_sha2_data_sig_verify_update_finalize(uint32_t session_id,
  1089. uint32_t context_id, uint32_t src_addr,
  1090. uint32_t src_size, uint64_t dst_addr,
  1091. uint32_t *dst_size, uint32_t data_size,
  1092. uint8_t is_finalised, uint32_t *mbox_error)
  1093. {
  1094. int status;
  1095. uint32_t i;
  1096. uint32_t flag;
  1097. uint32_t crypto_header;
  1098. uint32_t payload[FCS_ECDSA_SHA2_DATA_SIG_VERIFY_CMD_MAX_WORD_SIZE] = {0U};
  1099. uint32_t resp_len;
  1100. uintptr_t sig_pubkey_offset;
  1101. if ((dst_size == NULL) || (mbox_error == NULL)) {
  1102. return INTEL_SIP_SMC_STATUS_REJECTED;
  1103. }
  1104. if (fcs_sha2_data_sig_verify_param.session_id != session_id ||
  1105. fcs_sha2_data_sig_verify_param.context_id != context_id) {
  1106. return INTEL_SIP_SMC_STATUS_REJECTED;
  1107. }
  1108. if (!is_size_4_bytes_aligned(src_size)) {
  1109. return INTEL_SIP_SMC_STATUS_REJECTED;
  1110. }
  1111. if (!is_8_bytes_aligned(data_size) ||
  1112. !is_8_bytes_aligned(src_addr)) {
  1113. return INTEL_SIP_SMC_STATUS_REJECTED;
  1114. }
  1115. if (!is_address_in_ddr_range(src_addr, src_size) ||
  1116. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  1117. return INTEL_SIP_SMC_STATUS_REJECTED;
  1118. }
  1119. resp_len = *dst_size / MBOX_WORD_BYTE;
  1120. /* Prepare crypto header */
  1121. flag = 0;
  1122. if (fcs_sha2_data_sig_verify_param.is_updated)
  1123. fcs_sha2_data_sig_verify_param.crypto_param_size = 0;
  1124. else
  1125. flag |= FCS_CS_FIELD_FLAG_INIT;
  1126. if (is_finalised != 0U)
  1127. flag |= FCS_CS_FIELD_FLAG_FINALIZE;
  1128. else {
  1129. flag |= FCS_CS_FIELD_FLAG_UPDATE;
  1130. fcs_sha2_data_sig_verify_param.is_updated = 1;
  1131. }
  1132. crypto_header = (flag << FCS_CS_FIELD_FLAG_OFFSET) |
  1133. fcs_sha2_data_sig_verify_param.crypto_param_size;
  1134. /* Prepare command payload */
  1135. i = 0;
  1136. payload[i] = fcs_sha2_data_sig_verify_param.session_id;
  1137. i++;
  1138. payload[i] = fcs_sha2_data_sig_verify_param.context_id;
  1139. i++;
  1140. payload[i] = crypto_header;
  1141. i++;
  1142. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  1143. FCS_CS_FIELD_FLAG_INIT) {
  1144. payload[i] = fcs_sha2_data_sig_verify_param.key_id;
  1145. i++;
  1146. /* Crypto parameters */
  1147. payload[i] = fcs_sha2_data_sig_verify_param.crypto_param
  1148. & INTEL_SIP_SMC_FCS_ECC_ALGO_MASK;
  1149. i++;
  1150. }
  1151. /* Data source address and size */
  1152. payload[i] = src_addr;
  1153. i++;
  1154. payload[i] = data_size;
  1155. i++;
  1156. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  1157. FCS_CS_FIELD_FLAG_FINALIZE) {
  1158. /* Signature + Public Key Data */
  1159. sig_pubkey_offset = src_addr + data_size;
  1160. memcpy((uint8_t *) &payload[i], (uint8_t *) sig_pubkey_offset,
  1161. src_size - data_size);
  1162. i += (src_size - data_size) / MBOX_WORD_BYTE;
  1163. }
  1164. status = mailbox_send_cmd(MBOX_JOB_ID,
  1165. MBOX_FCS_ECDSA_SHA2_DATA_SIGN_VERIFY, payload, i,
  1166. CMD_CASUAL, (uint32_t *) dst_addr, &resp_len);
  1167. if (is_finalised != 0U) {
  1168. memset((void *) &fcs_sha2_data_sig_verify_param, 0,
  1169. sizeof(fcs_crypto_service_data));
  1170. }
  1171. if (status < 0) {
  1172. *mbox_error = -status;
  1173. return INTEL_SIP_SMC_STATUS_ERROR;
  1174. }
  1175. *dst_size = resp_len * MBOX_WORD_BYTE;
  1176. flush_dcache_range(dst_addr, *dst_size);
  1177. return INTEL_SIP_SMC_STATUS_OK;
  1178. }
  1179. int intel_fcs_ecdsa_get_pubkey_init(uint32_t session_id, uint32_t context_id,
  1180. uint32_t key_id, uint32_t param_size,
  1181. uint64_t param_data, uint32_t *mbox_error)
  1182. {
  1183. return intel_fcs_crypto_service_init(session_id, context_id,
  1184. key_id, param_size, param_data,
  1185. (void *) &fcs_ecdsa_get_pubkey_param,
  1186. mbox_error);
  1187. }
  1188. int intel_fcs_ecdsa_get_pubkey_finalize(uint32_t session_id, uint32_t context_id,
  1189. uint64_t dst_addr, uint32_t *dst_size,
  1190. uint32_t *mbox_error)
  1191. {
  1192. int status;
  1193. int i;
  1194. uint32_t crypto_header;
  1195. uint32_t ret_size;
  1196. uint32_t payload[FCS_ECDSA_GET_PUBKEY_MAX_WORD_SIZE] = {0U};
  1197. if ((dst_size == NULL) || (mbox_error == NULL)) {
  1198. return INTEL_SIP_SMC_STATUS_REJECTED;
  1199. }
  1200. if (fcs_ecdsa_get_pubkey_param.session_id != session_id ||
  1201. fcs_ecdsa_get_pubkey_param.context_id != context_id) {
  1202. return INTEL_SIP_SMC_STATUS_REJECTED;
  1203. }
  1204. ret_size = *dst_size / MBOX_WORD_BYTE;
  1205. crypto_header = ((FCS_CS_FIELD_FLAG_INIT |
  1206. FCS_CS_FIELD_FLAG_UPDATE |
  1207. FCS_CS_FIELD_FLAG_FINALIZE) <<
  1208. FCS_CS_FIELD_FLAG_OFFSET) |
  1209. fcs_ecdsa_get_pubkey_param.crypto_param_size;
  1210. i = 0;
  1211. /* Prepare command payload */
  1212. payload[i] = session_id;
  1213. i++;
  1214. payload[i] = context_id;
  1215. i++;
  1216. payload[i] = crypto_header;
  1217. i++;
  1218. payload[i] = fcs_ecdsa_get_pubkey_param.key_id;
  1219. i++;
  1220. payload[i] = (uint32_t) fcs_ecdsa_get_pubkey_param.crypto_param &
  1221. INTEL_SIP_SMC_FCS_ECC_ALGO_MASK;
  1222. i++;
  1223. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_ECDSA_GET_PUBKEY,
  1224. payload, i, CMD_CASUAL,
  1225. (uint32_t *) dst_addr, &ret_size);
  1226. memset((void *) &fcs_ecdsa_get_pubkey_param, 0,
  1227. sizeof(fcs_crypto_service_data));
  1228. if (status < 0) {
  1229. *mbox_error = -status;
  1230. return INTEL_SIP_SMC_STATUS_ERROR;
  1231. }
  1232. *dst_size = ret_size * MBOX_WORD_BYTE;
  1233. flush_dcache_range(dst_addr, *dst_size);
  1234. return INTEL_SIP_SMC_STATUS_OK;
  1235. }
  1236. int intel_fcs_ecdh_request_init(uint32_t session_id, uint32_t context_id,
  1237. uint32_t key_id, uint32_t param_size,
  1238. uint64_t param_data, uint32_t *mbox_error)
  1239. {
  1240. return intel_fcs_crypto_service_init(session_id, context_id,
  1241. key_id, param_size, param_data,
  1242. (void *) &fcs_ecdh_request_param,
  1243. mbox_error);
  1244. }
  1245. int intel_fcs_ecdh_request_finalize(uint32_t session_id, uint32_t context_id,
  1246. uint32_t src_addr, uint32_t src_size,
  1247. uint64_t dst_addr, uint32_t *dst_size,
  1248. uint32_t *mbox_error)
  1249. {
  1250. int status;
  1251. uint32_t i;
  1252. uint32_t payload[FCS_ECDH_REQUEST_CMD_MAX_WORD_SIZE] = {0U};
  1253. uint32_t resp_len;
  1254. uintptr_t pubkey;
  1255. if ((dst_size == NULL) || (mbox_error == NULL)) {
  1256. return INTEL_SIP_SMC_STATUS_REJECTED;
  1257. }
  1258. if (fcs_ecdh_request_param.session_id != session_id ||
  1259. fcs_ecdh_request_param.context_id != context_id) {
  1260. return INTEL_SIP_SMC_STATUS_REJECTED;
  1261. }
  1262. if (!is_address_in_ddr_range(src_addr, src_size) ||
  1263. !is_address_in_ddr_range(dst_addr, *dst_size)) {
  1264. return INTEL_SIP_SMC_STATUS_REJECTED;
  1265. }
  1266. resp_len = *dst_size / MBOX_WORD_BYTE;
  1267. /* Prepare command payload */
  1268. i = 0;
  1269. /* Crypto header */
  1270. payload[i] = fcs_ecdh_request_param.session_id;
  1271. i++;
  1272. payload[i] = fcs_ecdh_request_param.context_id;
  1273. i++;
  1274. payload[i] = fcs_ecdh_request_param.crypto_param_size
  1275. & FCS_CS_FIELD_SIZE_MASK;
  1276. payload[i] |= (FCS_CS_FIELD_FLAG_INIT | FCS_CS_FIELD_FLAG_UPDATE
  1277. | FCS_CS_FIELD_FLAG_FINALIZE)
  1278. << FCS_CS_FIELD_FLAG_OFFSET;
  1279. i++;
  1280. payload[i] = fcs_ecdh_request_param.key_id;
  1281. i++;
  1282. /* Crypto parameters */
  1283. payload[i] = fcs_ecdh_request_param.crypto_param
  1284. & INTEL_SIP_SMC_FCS_ECC_ALGO_MASK;
  1285. i++;
  1286. /* Public key data */
  1287. pubkey = src_addr;
  1288. memcpy((uint8_t *) &payload[i], (uint8_t *) pubkey, src_size);
  1289. i += src_size / MBOX_WORD_BYTE;
  1290. status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_FCS_ECDH_REQUEST,
  1291. payload, i, CMD_CASUAL, (uint32_t *) dst_addr,
  1292. &resp_len);
  1293. memset((void *)&fcs_ecdh_request_param, 0,
  1294. sizeof(fcs_crypto_service_data));
  1295. if (status < 0) {
  1296. *mbox_error = -status;
  1297. return INTEL_SIP_SMC_STATUS_ERROR;
  1298. }
  1299. *dst_size = resp_len * MBOX_WORD_BYTE;
  1300. flush_dcache_range(dst_addr, *dst_size);
  1301. return INTEL_SIP_SMC_STATUS_OK;
  1302. }
  1303. int intel_fcs_aes_crypt_init(uint32_t session_id, uint32_t context_id,
  1304. uint32_t key_id, uint64_t param_addr,
  1305. uint32_t param_size, uint32_t *mbox_error)
  1306. {
  1307. /* ptr to get param_addr value */
  1308. uint64_t *param_addr_ptr;
  1309. param_addr_ptr = (uint64_t *) param_addr;
  1310. /*
  1311. * Since crypto param size vary between mode.
  1312. * Check ECB here and limit to size 12 bytes
  1313. */
  1314. if (((*param_addr_ptr & FCS_CRYPTO_BLOCK_MODE_MASK) == FCS_CRYPTO_ECB_MODE) &&
  1315. (param_size > FCS_CRYPTO_ECB_BUFFER_SIZE)) {
  1316. return INTEL_SIP_SMC_STATUS_REJECTED;
  1317. }
  1318. /*
  1319. * Since crypto param size vary between mode.
  1320. * Check CBC/CTR here and limit to size 28 bytes
  1321. */
  1322. if ((((*param_addr_ptr & FCS_CRYPTO_BLOCK_MODE_MASK) == FCS_CRYPTO_CBC_MODE) ||
  1323. ((*param_addr_ptr & FCS_CRYPTO_BLOCK_MODE_MASK) == FCS_CRYPTO_CTR_MODE)) &&
  1324. (param_size > FCS_CRYPTO_CBC_CTR_BUFFER_SIZE)) {
  1325. return INTEL_SIP_SMC_STATUS_REJECTED;
  1326. }
  1327. if (mbox_error == NULL) {
  1328. return INTEL_SIP_SMC_STATUS_REJECTED;
  1329. }
  1330. memset((void *)&fcs_aes_init_payload, 0U, sizeof(fcs_aes_init_payload));
  1331. fcs_aes_init_payload.session_id = session_id;
  1332. fcs_aes_init_payload.context_id = context_id;
  1333. fcs_aes_init_payload.param_size = param_size;
  1334. fcs_aes_init_payload.key_id = key_id;
  1335. memcpy((uint8_t *) fcs_aes_init_payload.crypto_param,
  1336. (uint8_t *) param_addr, param_size);
  1337. fcs_aes_init_payload.is_updated = 0;
  1338. *mbox_error = 0;
  1339. return INTEL_SIP_SMC_STATUS_OK;
  1340. }
  1341. int intel_fcs_aes_crypt_update_finalize(uint32_t session_id,
  1342. uint32_t context_id, uint64_t src_addr,
  1343. uint32_t src_size, uint64_t dst_addr,
  1344. uint32_t dst_size, uint8_t is_finalised,
  1345. uint32_t *send_id)
  1346. {
  1347. int status;
  1348. int i;
  1349. uint32_t flag;
  1350. uint32_t crypto_header;
  1351. uint32_t fcs_aes_crypt_payload[FCS_AES_CMD_MAX_WORD_SIZE];
  1352. if (fcs_aes_init_payload.session_id != session_id ||
  1353. fcs_aes_init_payload.context_id != context_id) {
  1354. return INTEL_SIP_SMC_STATUS_REJECTED;
  1355. }
  1356. if ((!is_8_bytes_aligned(src_addr)) ||
  1357. (!is_32_bytes_aligned(src_size)) ||
  1358. (!is_address_in_ddr_range(src_addr, src_size))) {
  1359. return INTEL_SIP_SMC_STATUS_REJECTED;
  1360. }
  1361. if ((!is_8_bytes_aligned(dst_addr)) ||
  1362. (!is_32_bytes_aligned(dst_size))) {
  1363. return INTEL_SIP_SMC_STATUS_REJECTED;
  1364. }
  1365. if ((dst_size > FCS_AES_MAX_DATA_SIZE ||
  1366. dst_size < FCS_AES_MIN_DATA_SIZE) ||
  1367. (src_size > FCS_AES_MAX_DATA_SIZE ||
  1368. src_size < FCS_AES_MIN_DATA_SIZE)) {
  1369. return INTEL_SIP_SMC_STATUS_REJECTED;
  1370. }
  1371. /* Prepare crypto header*/
  1372. flag = 0;
  1373. if (fcs_aes_init_payload.is_updated) {
  1374. fcs_aes_init_payload.param_size = 0;
  1375. } else {
  1376. flag |= FCS_CS_FIELD_FLAG_INIT;
  1377. }
  1378. if (is_finalised != 0U) {
  1379. flag |= FCS_CS_FIELD_FLAG_FINALIZE;
  1380. } else {
  1381. flag |= FCS_CS_FIELD_FLAG_UPDATE;
  1382. fcs_aes_init_payload.is_updated = 1;
  1383. }
  1384. crypto_header = (flag << FCS_CS_FIELD_FLAG_OFFSET) |
  1385. fcs_aes_init_payload.param_size;
  1386. i = 0U;
  1387. fcs_aes_crypt_payload[i] = session_id;
  1388. i++;
  1389. fcs_aes_crypt_payload[i] = context_id;
  1390. i++;
  1391. fcs_aes_crypt_payload[i] = crypto_header;
  1392. i++;
  1393. if ((crypto_header >> FCS_CS_FIELD_FLAG_OFFSET) &
  1394. FCS_CS_FIELD_FLAG_INIT) {
  1395. fcs_aes_crypt_payload[i] = fcs_aes_init_payload.key_id;
  1396. i++;
  1397. memcpy((uint8_t *) &fcs_aes_crypt_payload[i],
  1398. (uint8_t *) fcs_aes_init_payload.crypto_param,
  1399. fcs_aes_init_payload.param_size);
  1400. i += fcs_aes_init_payload.param_size / MBOX_WORD_BYTE;
  1401. }
  1402. fcs_aes_crypt_payload[i] = (uint32_t) src_addr;
  1403. i++;
  1404. fcs_aes_crypt_payload[i] = src_size;
  1405. i++;
  1406. fcs_aes_crypt_payload[i] = (uint32_t) dst_addr;
  1407. i++;
  1408. fcs_aes_crypt_payload[i] = dst_size;
  1409. i++;
  1410. status = mailbox_send_cmd_async(send_id, MBOX_FCS_AES_CRYPT_REQ,
  1411. fcs_aes_crypt_payload, i,
  1412. CMD_INDIRECT);
  1413. if (is_finalised != 0U) {
  1414. memset((void *)&fcs_aes_init_payload, 0,
  1415. sizeof(fcs_aes_init_payload));
  1416. }
  1417. if (status < 0U) {
  1418. return INTEL_SIP_SMC_STATUS_ERROR;
  1419. }
  1420. return INTEL_SIP_SMC_STATUS_OK;
  1421. }