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