| #include <common.h> |
| #include <sha256.h> |
| #include <sha1.h> |
| #include <tee.h> |
| #include <malloc.h> |
| #include <asm/errno.h> |
| #include "sha_optee.h" |
| #include <asm/arch/cpu.h> |
| |
| struct tee_contex { |
| struct tee_device *tdev; |
| struct tee_open_session_arg arg; |
| }; |
| |
| static struct tee_contex tee_ctx = {0}; |
| |
| void sha256_starts_optee(sha256_context * ctx) |
| { |
| int ret; |
| struct tee_invoke_arg arg_func = {0}; |
| struct tee_param param[1] = {0}; |
| |
| const struct tee_optee_ta_uuid uuid = ASR_SHA_UUID; |
| |
| tee_ctx.tdev = tee_find_device(NULL, NULL, NULL, NULL); |
| if (!tee_ctx.tdev) { |
| printf("Cannot get OP-TEE device\n"); |
| return; |
| } |
| |
| /* Set TA UUID */ |
| tee_optee_ta_uuid_to_octets(tee_ctx.arg.uuid, &uuid); |
| |
| /* Open TA session */ |
| ret = tee_open_session(tee_ctx.tdev , &tee_ctx.arg, 0, NULL); |
| if (ret < 0) { |
| printf("Cannot open session with PTA Blob 0x%X\n", ret); |
| return; |
| } |
| |
| param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| param[0].u.value.a = TEE_ALG_SHA256; |
| arg_func.func = CMD_SHA_INIT; |
| arg_func.session = tee_ctx.arg.session; |
| |
| ret = tee_invoke_func(tee_ctx.tdev , &arg_func, 1, param); |
| if (ret) { |
| printf("Cannot get sha data from OP-TEE PTA_SHA\n"); |
| } |
| ctx->tee_sha256 = arg_func.ret; |
| |
| return; |
| } |
| |
| void sha256_update_optee(sha256_context *ctx, const uint8_t *input, uint32_t length) |
| { |
| int ret; |
| struct tee_invoke_arg arg_func = {0}; |
| struct tee_param param[2] = {0}; |
| uint32_t step_size; |
| |
| param[0].u.value.a = (u64)input; |
| param[0].u.value.b = (u64)length; |
| param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| |
| param[1].u.value.a = 1; |
| param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| arg_func.func = CMD_SHA_UPDATE; |
| arg_func.session = tee_ctx.arg.session; |
| |
| flush_dcache_range(input, (uint32_t)input + length); |
| ret = tee_invoke_func(tee_ctx.tdev, &arg_func, 2, param); |
| if (ret) { |
| printf("Cannot get sha data from OP-TEE PTA_SHA\n"); |
| goto err; |
| } |
| |
| return; |
| |
| err: |
| ret = tee_close_session(tee_ctx.tdev, tee_ctx.arg.session); |
| if (ret < 0) |
| printf("Cannot close session with PTA_SHA 0x%X\n", ret); |
| return; |
| } |
| |
| void sha256_finish_optee(sha256_context * ctx, uint8_t digest[32]) |
| { |
| int ret; |
| struct tee_invoke_arg arg_func = {0}; |
| struct tee_param param[1] = {0}; |
| struct tee_shm *shm_output; |
| uint8_t out[32] = {0}; |
| |
| ret = tee_shm_register(tee_ctx.tdev, (void *)(ulong)out, 32, |
| 0x0, &shm_output); |
| if (ret < 0) { |
| printf("Cannot register output shared memory 0x%X\n", ret); |
| goto err; |
| } |
| |
| param[0].u.memref.shm = shm_output; |
| param[0].u.memref.size = shm_output->size; |
| param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT; |
| arg_func.func = CMD_SHA_FINAL; |
| arg_func.session = tee_ctx.arg.session; |
| |
| ret = tee_invoke_func(tee_ctx.tdev, &arg_func, 1, param); |
| if (ret) { |
| printf("Cannot get sha data from OP-TEE PTA_SHA\n"); |
| goto err; |
| } |
| |
| memcpy(digest, out, 32); |
| return; |
| |
| err: |
| ret = tee_close_session(tee_ctx.tdev, tee_ctx.arg.session); |
| if (ret < 0) |
| printf("Cannot close session with PTA_SHA 0x%X\n", ret); |
| return; |
| } |