Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, STMicroelectronics International N.V. |
| 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 | #include <stdint.h> |
| 28 | |
| 29 | #include <ta_rpc.h> |
| 30 | #include <tee_api.h> |
| 31 | #include <trace.h> |
| 32 | #include <ta_crypt.h> |
| 33 | #include <ta_sims_test.h> |
| 34 | |
| 35 | static TEE_UUID cryp_uuid = TA_CRYPT_UUID; |
| 36 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 37 | static TEE_Result rpc_call_cryp(bool sec_mem, uint32_t nParamTypes, |
| 38 | TEE_Param pParams[4], uint32_t cmd) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 39 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame^] | 40 | TEE_TASessionHandle cryp_session = TEE_HANDLE_NULL; |
| 41 | TEE_Result res = TEE_ERROR_GENERIC; |
| 42 | uint32_t origin = 0; |
| 43 | TEE_Param params[4] = { }; |
| 44 | size_t n = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 45 | uint32_t types = |
| 46 | TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE, |
| 47 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE); |
| 48 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 49 | res = TEE_OpenTASession(&cryp_uuid, 0, types, params, &cryp_session, |
| 50 | &origin); |
| 51 | |
| 52 | if (res != TEE_SUCCESS) { |
| 53 | EMSG("rpc_sha256 - TEE_OpenTASession returned 0x%x\n", |
| 54 | (unsigned int)res); |
| 55 | return res; |
| 56 | } |
| 57 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 58 | types = nParamTypes; |
| 59 | if (sec_mem) { |
| 60 | TEE_MemFill(params, 0, sizeof(params)); |
| 61 | for (n = 0; n < 4; n++) { |
| 62 | switch (TEE_PARAM_TYPE_GET(types, n)) { |
| 63 | case TEE_PARAM_TYPE_VALUE_INPUT: |
| 64 | case TEE_PARAM_TYPE_VALUE_INOUT: |
| 65 | params[n].value = pParams[n].value; |
| 66 | break; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 67 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 68 | case TEE_PARAM_TYPE_MEMREF_INPUT: |
| 69 | case TEE_PARAM_TYPE_MEMREF_OUTPUT: |
| 70 | case TEE_PARAM_TYPE_MEMREF_INOUT: |
| 71 | params[n].memref.buffer = |
| 72 | TEE_Malloc(pParams[n].memref.size, 0); |
| 73 | if (!params[n].memref.buffer) |
| 74 | TEE_Panic(0); |
| 75 | params[n].memref.size = pParams[n].memref.size; |
| 76 | if (TEE_PARAM_TYPE_GET(types, n) != |
| 77 | TEE_PARAM_TYPE_MEMREF_OUTPUT) |
| 78 | TEE_MemMove(params[n].memref.buffer, |
| 79 | pParams[n].memref.buffer, |
| 80 | pParams[n].memref.size); |
| 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | TEE_MemMove(params, pParams, sizeof(params)); |
| 88 | } |
| 89 | |
| 90 | res = TEE_InvokeTACommand(cryp_session, 0, cmd, types, params, &origin); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 91 | if (res != TEE_SUCCESS) { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 92 | EMSG("rpc_call_cryp - TEE_InvokeTACommand returned 0x%x\n", |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 93 | (unsigned int)res); |
| 94 | } |
| 95 | |
| 96 | TEE_CloseTASession(cryp_session); |
| 97 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 98 | if (sec_mem) { |
| 99 | for (n = 0; n < 4; n++) { |
| 100 | switch (TEE_PARAM_TYPE_GET(types, n)) { |
| 101 | case TEE_PARAM_TYPE_VALUE_INOUT: |
| 102 | case TEE_PARAM_TYPE_VALUE_OUTPUT: |
| 103 | pParams[n].value = params[n].value; |
| 104 | break; |
| 105 | |
| 106 | case TEE_PARAM_TYPE_MEMREF_INPUT: |
| 107 | case TEE_PARAM_TYPE_MEMREF_OUTPUT: |
| 108 | case TEE_PARAM_TYPE_MEMREF_INOUT: |
| 109 | if (TEE_PARAM_TYPE_GET(types, n) != |
| 110 | TEE_PARAM_TYPE_MEMREF_INPUT) |
| 111 | TEE_MemMove(pParams[n].memref.buffer, |
| 112 | params[n].memref.buffer, |
| 113 | params[n].memref.size); |
| 114 | pParams[n].memref.size = params[n].memref.size; |
| 115 | TEE_Free(params[n].memref.buffer); |
| 116 | break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 124 | return res; |
| 125 | } |
| 126 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 127 | TEE_Result rpc_sha224(bool sec_mem, uint32_t nParamTypes, TEE_Param pParams[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 128 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 129 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 130 | TA_CRYPT_CMD_SHA224); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 133 | TEE_Result rpc_sha256(bool sec_mem, uint32_t nParamTypes, TEE_Param pParams[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 134 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 135 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 136 | TA_CRYPT_CMD_SHA256); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 139 | TEE_Result rpc_aes256ecb_encrypt(bool sec_mem, uint32_t nParamTypes, |
| 140 | TEE_Param pParams[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 141 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 142 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 143 | TA_CRYPT_CMD_AES256ECB_ENC); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 146 | TEE_Result rpc_aes256ecb_decrypt(bool sec_mem, uint32_t nParamTypes, |
| 147 | TEE_Param pParams[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 148 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 149 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 150 | TA_CRYPT_CMD_AES256ECB_DEC); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEE_Result rpc_open(void *session_context, uint32_t param_types, |
| 154 | TEE_Param params[4]) |
| 155 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame^] | 156 | TEE_TASessionHandle session = TEE_HANDLE_NULL; |
| 157 | uint32_t orig = 0; |
| 158 | TEE_Result res = TEE_ERROR_GENERIC; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 159 | TEE_UUID uuid = TA_SIMS_TEST_UUID; |
| 160 | uint32_t types = |
| 161 | TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, TEE_PARAM_TYPE_NONE, |
| 162 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE); |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame^] | 163 | TEE_Param par[4] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 164 | |
| 165 | (void)session_context; |
| 166 | (void)param_types; |
| 167 | |
| 168 | res = TEE_OpenTASession(&uuid, 0, 0, NULL, &session, &orig); |
| 169 | |
| 170 | if (res != TEE_SUCCESS) |
| 171 | return res; |
| 172 | |
| 173 | TEE_MemFill(params, 0, sizeof(TEE_Param) * 4); |
| 174 | res = |
| 175 | TEE_InvokeTACommand(session, 0, TA_SIMS_CMD_GET_COUNTER, types, par, |
| 176 | &orig); |
| 177 | |
| 178 | if (res != TEE_SUCCESS) |
| 179 | goto exit; |
| 180 | |
| 181 | exit: |
| 182 | TEE_CloseTASession(session); |
| 183 | |
| 184 | return res; |
| 185 | } |