b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #include <common.h> |
| 2 | #include <sha256.h> |
| 3 | #include <sha1.h> |
| 4 | #include <tee.h> |
| 5 | #include <malloc.h> |
| 6 | #include <asm/errno.h> |
| 7 | #include "sha_optee.h" |
| 8 | #include <asm/arch/cpu.h> |
| 9 | |
| 10 | struct tee_contex { |
| 11 | struct tee_device *tdev; |
| 12 | struct tee_open_session_arg arg; |
| 13 | }; |
| 14 | |
| 15 | static struct tee_contex tee_ctx = {0}; |
| 16 | |
| 17 | void sha256_starts_optee(sha256_context * ctx) |
| 18 | { |
| 19 | int ret; |
| 20 | struct tee_invoke_arg arg_func = {0}; |
| 21 | struct tee_param param[1] = {0}; |
| 22 | |
| 23 | const struct tee_optee_ta_uuid uuid = ASR_SHA_UUID; |
| 24 | |
| 25 | tee_ctx.tdev = tee_find_device(NULL, NULL, NULL, NULL); |
| 26 | if (!tee_ctx.tdev) { |
| 27 | printf("Cannot get OP-TEE device\n"); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | /* Set TA UUID */ |
| 32 | tee_optee_ta_uuid_to_octets(tee_ctx.arg.uuid, &uuid); |
| 33 | |
| 34 | /* Open TA session */ |
| 35 | ret = tee_open_session(tee_ctx.tdev , &tee_ctx.arg, 0, NULL); |
| 36 | if (ret < 0) { |
| 37 | printf("Cannot open session with PTA Blob 0x%X\n", ret); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 42 | param[0].u.value.a = TEE_ALG_SHA256; |
| 43 | arg_func.func = CMD_SHA_INIT; |
| 44 | arg_func.session = tee_ctx.arg.session; |
| 45 | |
| 46 | ret = tee_invoke_func(tee_ctx.tdev , &arg_func, 1, param); |
| 47 | if (ret) { |
| 48 | printf("Cannot get sha data from OP-TEE PTA_SHA\n"); |
| 49 | } |
| 50 | ctx->tee_sha256 = arg_func.ret; |
| 51 | |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | void sha256_update_optee(sha256_context *ctx, const uint8_t *input, uint32_t length) |
| 56 | { |
| 57 | int ret; |
| 58 | struct tee_invoke_arg arg_func = {0}; |
| 59 | struct tee_param param[2] = {0}; |
| 60 | uint32_t step_size; |
| 61 | |
| 62 | param[0].u.value.a = (u64)input; |
| 63 | param[0].u.value.b = (u64)length; |
| 64 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 65 | |
| 66 | param[1].u.value.a = 1; |
| 67 | param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 68 | arg_func.func = CMD_SHA_UPDATE; |
| 69 | arg_func.session = tee_ctx.arg.session; |
| 70 | |
| 71 | flush_dcache_range(input, (uint32_t)input + length); |
| 72 | ret = tee_invoke_func(tee_ctx.tdev, &arg_func, 2, param); |
| 73 | if (ret) { |
| 74 | printf("Cannot get sha data from OP-TEE PTA_SHA\n"); |
| 75 | goto err; |
| 76 | } |
| 77 | |
| 78 | return; |
| 79 | |
| 80 | err: |
| 81 | ret = tee_close_session(tee_ctx.tdev, tee_ctx.arg.session); |
| 82 | if (ret < 0) |
| 83 | printf("Cannot close session with PTA_SHA 0x%X\n", ret); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | void sha256_finish_optee(sha256_context * ctx, uint8_t digest[32]) |
| 88 | { |
| 89 | int ret; |
| 90 | struct tee_invoke_arg arg_func = {0}; |
| 91 | struct tee_param param[1] = {0}; |
| 92 | struct tee_shm *shm_output; |
| 93 | uint8_t out[32] = {0}; |
| 94 | |
| 95 | ret = tee_shm_register(tee_ctx.tdev, (void *)(ulong)out, 32, |
| 96 | 0x0, &shm_output); |
| 97 | if (ret < 0) { |
| 98 | printf("Cannot register output shared memory 0x%X\n", ret); |
| 99 | goto err; |
| 100 | } |
| 101 | |
| 102 | param[0].u.memref.shm = shm_output; |
| 103 | param[0].u.memref.size = shm_output->size; |
| 104 | param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT; |
| 105 | arg_func.func = CMD_SHA_FINAL; |
| 106 | arg_func.session = tee_ctx.arg.session; |
| 107 | |
| 108 | ret = tee_invoke_func(tee_ctx.tdev, &arg_func, 1, param); |
| 109 | if (ret) { |
| 110 | printf("Cannot get sha data from OP-TEE PTA_SHA\n"); |
| 111 | goto err; |
| 112 | } |
| 113 | |
| 114 | memcpy(digest, out, 32); |
| 115 | return; |
| 116 | |
| 117 | err: |
| 118 | ret = tee_close_session(tee_ctx.tdev, tee_ctx.arg.session); |
| 119 | if (ret < 0) |
| 120 | printf("Cannot close session with PTA_SHA 0x%X\n", ret); |
| 121 | return; |
| 122 | } |