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 | |
Cedric Auger | e668b3f | 2019-09-11 13:41:21 +0200 | [diff] [blame^] | 49 | res = TEE_OpenTASession(&cryp_uuid, TEE_TIMEOUT_INFINITE, types, |
| 50 | params, &cryp_session, &origin); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 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 | |
Cedric Auger | e668b3f | 2019-09-11 13:41:21 +0200 | [diff] [blame^] | 90 | res = TEE_InvokeTACommand(cryp_session, TEE_TIMEOUT_INFINITE, cmd, |
| 91 | types, params, &origin); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 92 | if (res != TEE_SUCCESS) { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 93 | EMSG("rpc_call_cryp - TEE_InvokeTACommand returned 0x%x\n", |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 94 | (unsigned int)res); |
| 95 | } |
| 96 | |
| 97 | TEE_CloseTASession(cryp_session); |
| 98 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 99 | if (sec_mem) { |
| 100 | for (n = 0; n < 4; n++) { |
| 101 | switch (TEE_PARAM_TYPE_GET(types, n)) { |
| 102 | case TEE_PARAM_TYPE_VALUE_INOUT: |
| 103 | case TEE_PARAM_TYPE_VALUE_OUTPUT: |
| 104 | pParams[n].value = params[n].value; |
| 105 | break; |
| 106 | |
| 107 | case TEE_PARAM_TYPE_MEMREF_INPUT: |
| 108 | case TEE_PARAM_TYPE_MEMREF_OUTPUT: |
| 109 | case TEE_PARAM_TYPE_MEMREF_INOUT: |
| 110 | if (TEE_PARAM_TYPE_GET(types, n) != |
| 111 | TEE_PARAM_TYPE_MEMREF_INPUT) |
| 112 | TEE_MemMove(pParams[n].memref.buffer, |
| 113 | params[n].memref.buffer, |
| 114 | params[n].memref.size); |
| 115 | pParams[n].memref.size = params[n].memref.size; |
| 116 | TEE_Free(params[n].memref.buffer); |
| 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 125 | return res; |
| 126 | } |
| 127 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 128 | 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] | 129 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 130 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 131 | TA_CRYPT_CMD_SHA224); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 134 | 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] | 135 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 136 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 137 | TA_CRYPT_CMD_SHA256); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 140 | TEE_Result rpc_aes256ecb_encrypt(bool sec_mem, uint32_t nParamTypes, |
| 141 | TEE_Param pParams[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 142 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 143 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 144 | TA_CRYPT_CMD_AES256ECB_ENC); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 147 | TEE_Result rpc_aes256ecb_decrypt(bool sec_mem, uint32_t nParamTypes, |
| 148 | TEE_Param pParams[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 149 | { |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 150 | return rpc_call_cryp(sec_mem, nParamTypes, pParams, |
| 151 | TA_CRYPT_CMD_AES256ECB_DEC); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEE_Result rpc_open(void *session_context, uint32_t param_types, |
| 155 | TEE_Param params[4]) |
| 156 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 157 | TEE_TASessionHandle session = TEE_HANDLE_NULL; |
| 158 | uint32_t orig = 0; |
| 159 | TEE_Result res = TEE_ERROR_GENERIC; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 160 | TEE_UUID uuid = TA_SIMS_TEST_UUID; |
| 161 | uint32_t types = |
| 162 | TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, TEE_PARAM_TYPE_NONE, |
| 163 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE); |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 164 | TEE_Param par[4] = { }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 165 | |
| 166 | (void)session_context; |
| 167 | (void)param_types; |
| 168 | |
Cedric Auger | e668b3f | 2019-09-11 13:41:21 +0200 | [diff] [blame^] | 169 | res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL, &session, |
| 170 | &orig); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 171 | |
| 172 | if (res != TEE_SUCCESS) |
| 173 | return res; |
| 174 | |
| 175 | TEE_MemFill(params, 0, sizeof(TEE_Param) * 4); |
| 176 | res = |
Cedric Auger | e668b3f | 2019-09-11 13:41:21 +0200 | [diff] [blame^] | 177 | TEE_InvokeTACommand(session, TEE_TIMEOUT_INFINITE, |
| 178 | TA_SIMS_CMD_GET_COUNTER, types, par, &orig); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 179 | |
| 180 | if (res != TEE_SUCCESS) |
| 181 | goto exit; |
| 182 | |
| 183 | exit: |
| 184 | TEE_CloseTASession(session); |
| 185 | |
| 186 | return res; |
| 187 | } |