Igor Opaniuk | 06fe31c | 2017-08-07 02:37:51 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Linaro Limited |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #define STR_TRACE_USER_TA "RANDOM_EXAMPLE_TA" |
| 29 | |
| 30 | #include <tee_internal_api.h> |
| 31 | #include <tee_internal_api_extensions.h> |
| 32 | |
| 33 | #include "random_example_ta.h" |
| 34 | |
| 35 | TEE_Result TA_CreateEntryPoint(void) |
| 36 | { |
| 37 | return TEE_SUCCESS; |
| 38 | } |
| 39 | |
| 40 | void TA_DestroyEntryPoint(void) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types, |
| 45 | TEE_Param __maybe_unused params[4], |
| 46 | void __maybe_unused **sess_ctx) |
| 47 | { |
| 48 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, |
| 49 | TEE_PARAM_TYPE_NONE, |
| 50 | TEE_PARAM_TYPE_NONE, |
| 51 | TEE_PARAM_TYPE_NONE); |
| 52 | if (param_types != exp_param_types) |
| 53 | return TEE_ERROR_BAD_PARAMETERS; |
| 54 | |
| 55 | (void)¶ms; |
| 56 | (void)&sess_ctx; |
| 57 | |
| 58 | return TEE_SUCCESS; |
| 59 | } |
| 60 | |
| 61 | void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx) |
| 62 | { |
| 63 | (void)&sess_ctx; |
| 64 | } |
| 65 | |
| 66 | static TEE_Result random_number_generate(uint32_t param_types, |
| 67 | TEE_Param params[4]) |
| 68 | { |
| 69 | uint32_t exp_param_types = TEE_PARAM_TYPES |
| 70 | (TEE_PARAM_TYPE_MEMREF_OUTPUT, TEE_PARAM_TYPE_NONE, |
| 71 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE); |
| 72 | |
| 73 | DMSG("has been called"); |
| 74 | if (param_types != exp_param_types) |
| 75 | return TEE_ERROR_BAD_PARAMETERS; |
| 76 | |
| 77 | IMSG("Generating random data over %u bytes.", params[0].memref.size); |
| 78 | /* |
| 79 | * The TEE_GenerateRandom function is a part of TEE Internal Core API, |
| 80 | * which generates random data |
| 81 | * |
| 82 | * Parameters: |
| 83 | * @ randomBuffer : Reference to generated random data |
| 84 | * @ randomBufferLen : Byte length of requested random data |
| 85 | */ |
| 86 | TEE_GenerateRandom(params[0].memref.buffer, params[0].memref.size); |
| 87 | |
| 88 | return TEE_SUCCESS; |
| 89 | } |
| 90 | |
| 91 | TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx, |
| 92 | uint32_t cmd_id, |
| 93 | uint32_t param_types, TEE_Param params[4]) |
| 94 | { |
| 95 | (void)&sess_ctx; |
| 96 | |
| 97 | switch (cmd_id) { |
| 98 | case TA_EXAMPLE_RANDOM_GENERATE: |
| 99 | return random_number_generate(param_types, params); |
| 100 | default: |
| 101 | return TEE_ERROR_BAD_PARAMETERS; |
| 102 | } |
| 103 | } |