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 | |
| 28 | #include "storage.h" |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 29 | #include "ta_storage.h" |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 30 | |
| 31 | #include <tee_api.h> |
Pascal Brand | 90f2335 | 2016-05-19 15:15:47 +0200 | [diff] [blame] | 32 | #include <trace.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 33 | |
| 34 | #define ASSERT_PARAM_TYPE(pt) \ |
| 35 | do { \ |
| 36 | if ((pt) != param_types) \ |
| 37 | return TEE_ERROR_BAD_PARAMETERS; \ |
| 38 | } while (0) |
| 39 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 40 | #define VAL2HANDLE(v) (void *)(uintptr_t)(v) |
| 41 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 42 | TEE_Result ta_storage_cmd_open(uint32_t command, |
| 43 | uint32_t param_types, TEE_Param params[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 44 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 45 | TEE_Result res = TEE_ERROR_GENERIC; |
| 46 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
| 47 | void *object_id = NULL; |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 48 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 49 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 50 | (TEE_PARAM_TYPE_MEMREF_INPUT, |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 51 | TEE_PARAM_TYPE_VALUE_INOUT, |
| 52 | TEE_PARAM_TYPE_VALUE_INPUT, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 53 | TEE_PARAM_TYPE_NONE)); |
| 54 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 55 | switch (command) { |
| 56 | case TA_STORAGE_CMD_OPEN: |
| 57 | object_id = TEE_Malloc(params[0].memref.size, 0); |
| 58 | if (!object_id) |
| 59 | return TEE_ERROR_OUT_OF_MEMORY; |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 60 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 61 | TEE_MemMove(object_id, params[0].memref.buffer, |
| 62 | params[0].memref.size); |
| 63 | break; |
| 64 | case TA_STORAGE_CMD_OPEN_ID_IN_SHM: |
| 65 | object_id = params[0].memref.buffer; |
| 66 | break; |
| 67 | default: |
| 68 | return TEE_ERROR_NOT_SUPPORTED; |
| 69 | } |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 70 | |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 71 | res = TEE_OpenPersistentObject(params[2].value.a, |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 72 | object_id, params[0].memref.size, |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 73 | params[1].value.a, &o); |
| 74 | |
| 75 | params[1].value.b = (uintptr_t)o; |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 76 | |
| 77 | if (command == TA_STORAGE_CMD_OPEN) |
| 78 | TEE_Free(object_id); |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 79 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 80 | return res; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 83 | TEE_Result ta_storage_cmd_create(uint32_t command, |
| 84 | uint32_t param_types, TEE_Param params[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 85 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 86 | TEE_Result res = TEE_ERROR_GENERIC; |
| 87 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
| 88 | void *object_id = NULL; |
| 89 | TEE_ObjectHandle ref_handle = TEE_HANDLE_NULL; |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 90 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 91 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 92 | (TEE_PARAM_TYPE_MEMREF_INPUT, |
| 93 | TEE_PARAM_TYPE_VALUE_INOUT, |
| 94 | TEE_PARAM_TYPE_VALUE_INPUT, |
| 95 | TEE_PARAM_TYPE_MEMREF_INPUT)); |
| 96 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 97 | switch (command) { |
| 98 | case TA_STORAGE_CMD_CREATE: |
| 99 | object_id = TEE_Malloc(params[0].memref.size, 0); |
| 100 | if (!object_id) |
| 101 | return TEE_ERROR_OUT_OF_MEMORY; |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 102 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 103 | TEE_MemMove(object_id, params[0].memref.buffer, |
| 104 | params[0].memref.size); |
| 105 | break; |
| 106 | case TA_STORAGE_CMD_CREATE_ID_IN_SHM: |
| 107 | object_id = params[0].memref.buffer; |
| 108 | break; |
| 109 | default: |
| 110 | return TEE_ERROR_NOT_SUPPORTED; |
| 111 | } |
| 112 | |
| 113 | ref_handle = (TEE_ObjectHandle)(uintptr_t)params[2].value.a; |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 114 | |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 115 | res = TEE_CreatePersistentObject(params[2].value.b, |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 116 | object_id, params[0].memref.size, |
| 117 | params[1].value.a, ref_handle, |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 118 | params[3].memref.buffer, |
| 119 | params[3].memref.size, &o); |
| 120 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 121 | if (command == TA_STORAGE_CMD_CREATE) |
| 122 | TEE_Free(object_id); |
| 123 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 124 | params[1].value.b = (uintptr_t)o; |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 125 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 126 | return res; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 129 | TEE_Result ta_storage_cmd_create_overwrite(uint32_t command, |
| 130 | uint32_t param_types, |
Pascal Brand | eb84c44 | 2016-04-19 17:49:49 +0200 | [diff] [blame] | 131 | TEE_Param params[4]) |
| 132 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 133 | TEE_Result res = TEE_ERROR_GENERIC; |
Cedric Auger | 3ad6b8a | 2019-09-11 16:38:42 +0200 | [diff] [blame^] | 134 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 135 | void *object_id = NULL; |
Pascal Brand | eb84c44 | 2016-04-19 17:49:49 +0200 | [diff] [blame] | 136 | |
| 137 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 138 | (TEE_PARAM_TYPE_MEMREF_INPUT, |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 139 | TEE_PARAM_TYPE_VALUE_INPUT, |
Pascal Brand | eb84c44 | 2016-04-19 17:49:49 +0200 | [diff] [blame] | 140 | TEE_PARAM_TYPE_NONE, |
| 141 | TEE_PARAM_TYPE_NONE)); |
| 142 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 143 | switch (command) { |
| 144 | case TA_STORAGE_CMD_CREATE_OVERWRITE: |
| 145 | object_id = TEE_Malloc(params[0].memref.size, 0); |
| 146 | if (!object_id) |
| 147 | return TEE_ERROR_OUT_OF_MEMORY; |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 148 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 149 | TEE_MemMove(object_id, params[0].memref.buffer, |
| 150 | params[0].memref.size); |
| 151 | break; |
| 152 | case TA_STORAGE_CMD_CREATEOVER_ID_IN_SHM: |
| 153 | object_id = params[0].memref.buffer; |
| 154 | break; |
| 155 | default: |
| 156 | return TEE_ERROR_NOT_SUPPORTED; |
| 157 | } |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 158 | |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 159 | res = TEE_CreatePersistentObject(params[1].value.a, |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 160 | object_id, params[0].memref.size, |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 161 | TEE_DATA_FLAG_OVERWRITE, |
Cedric Auger | 3ad6b8a | 2019-09-11 16:38:42 +0200 | [diff] [blame^] | 162 | NULL, NULL, 0, &o); |
| 163 | TEE_CloseObject(o); |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 164 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 165 | if (command == TA_STORAGE_CMD_CREATE_OVERWRITE) |
| 166 | TEE_Free(object_id); |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 167 | |
Pascal Brand | eb84c44 | 2016-04-19 17:49:49 +0200 | [diff] [blame] | 168 | return res; |
| 169 | } |
| 170 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 171 | TEE_Result ta_storage_cmd_close(uint32_t param_types, TEE_Param params[4]) |
| 172 | { |
| 173 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 174 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 175 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 176 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 177 | TEE_CloseObject((TEE_ObjectHandle)(uintptr_t)params[0].value.a); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 178 | |
| 179 | return TEE_SUCCESS; |
| 180 | } |
| 181 | |
| 182 | TEE_Result ta_storage_cmd_read(uint32_t param_types, TEE_Param params[4]) |
| 183 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 184 | TEE_ObjectHandle o = VAL2HANDLE(params[1].value.a); |
| 185 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 186 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 187 | (TEE_PARAM_TYPE_MEMREF_OUTPUT, |
| 188 | TEE_PARAM_TYPE_VALUE_INOUT, TEE_PARAM_TYPE_NONE, |
| 189 | TEE_PARAM_TYPE_NONE)); |
| 190 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 191 | return TEE_ReadObjectData(o, params[0].memref.buffer, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 192 | params[0].memref.size, ¶ms[1].value.b); |
| 193 | } |
| 194 | |
| 195 | TEE_Result ta_storage_cmd_write(uint32_t param_types, TEE_Param params[4]) |
| 196 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 197 | TEE_ObjectHandle o = VAL2HANDLE(params[1].value.a); |
| 198 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 199 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 200 | (TEE_PARAM_TYPE_MEMREF_INPUT, |
| 201 | TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 202 | TEE_PARAM_TYPE_NONE)); |
| 203 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 204 | return TEE_WriteObjectData(o, params[0].memref.buffer, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 205 | params[0].memref.size); |
| 206 | } |
| 207 | |
| 208 | TEE_Result ta_storage_cmd_seek(uint32_t param_types, TEE_Param params[4]) |
| 209 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 210 | TEE_Result res = TEE_ERROR_GENERIC; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 211 | TEE_ObjectInfo info; |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 212 | TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a); |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 213 | int32_t offs = 0; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 214 | |
| 215 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 216 | (TEE_PARAM_TYPE_VALUE_INPUT, |
| 217 | TEE_PARAM_TYPE_VALUE_INOUT, TEE_PARAM_TYPE_NONE, |
| 218 | TEE_PARAM_TYPE_NONE)); |
| 219 | |
Jens Wiklander | e6d4ddd | 2016-09-14 15:50:48 +0200 | [diff] [blame] | 220 | offs = *(int32_t *)¶ms[0].value.b; |
| 221 | res = TEE_SeekObjectData(o, offs, params[1].value.a); |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 222 | if (res != TEE_SUCCESS) |
| 223 | return res; |
| 224 | res = TEE_GetObjectInfo1(o, &info); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 225 | |
| 226 | params[1].value.b = info.dataPosition; |
| 227 | |
| 228 | return res; |
| 229 | } |
| 230 | |
| 231 | TEE_Result ta_storage_cmd_unlink(uint32_t param_types, TEE_Param params[4]) |
| 232 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 233 | TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a); |
| 234 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 235 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 236 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 237 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 238 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 239 | TEE_CloseAndDeletePersistentObject1(o); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 240 | |
| 241 | return TEE_SUCCESS; |
| 242 | } |
| 243 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 244 | TEE_Result ta_storage_cmd_rename(uint32_t command, uint32_t param_types, |
| 245 | TEE_Param params[4]) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 246 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 247 | TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a); |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 248 | void *object_id = NULL; |
| 249 | TEE_Result res = TEE_ERROR_GENERIC; |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 250 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 251 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 252 | (TEE_PARAM_TYPE_VALUE_INPUT, |
| 253 | TEE_PARAM_TYPE_MEMREF_INPUT, TEE_PARAM_TYPE_NONE, |
| 254 | TEE_PARAM_TYPE_NONE)); |
| 255 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 256 | switch (command) { |
| 257 | case TA_STORAGE_CMD_RENAME: |
| 258 | object_id = TEE_Malloc(params[1].memref.size, 0); |
| 259 | if (!object_id) |
| 260 | return TEE_ERROR_OUT_OF_MEMORY; |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 261 | |
Etienne Carriere | 294ffbd | 2018-04-26 14:20:35 +0200 | [diff] [blame] | 262 | TEE_MemMove(object_id, params[1].memref.buffer, |
| 263 | params[1].memref.size); |
| 264 | break; |
| 265 | case TA_STORAGE_CMD_RENAME_ID_IN_SHM: |
| 266 | object_id = params[1].memref.buffer; |
| 267 | break; |
| 268 | default: |
| 269 | return TEE_ERROR_NOT_SUPPORTED; |
| 270 | } |
| 271 | |
| 272 | res = TEE_RenamePersistentObject(o, object_id, params[1].memref.size); |
| 273 | |
| 274 | if (command == TA_STORAGE_CMD_RENAME) |
| 275 | TEE_Free(object_id); |
Etienne Carriere | 9811a80 | 2018-02-16 16:11:42 +0100 | [diff] [blame] | 276 | |
| 277 | return res; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | TEE_Result ta_storage_cmd_trunc(uint32_t param_types, TEE_Param params[4]) |
| 281 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 282 | TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a); |
| 283 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 284 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 285 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 286 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 287 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 288 | return TEE_TruncateObjectData(o, params[0].value.b); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | TEE_Result ta_storage_cmd_alloc_enum(uint32_t param_types, TEE_Param params[4]) |
| 292 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 293 | TEE_Result res = TEE_ERROR_GENERIC; |
| 294 | TEE_ObjectEnumHandle oe = TEE_HANDLE_NULL; |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 295 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 296 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 297 | (TEE_PARAM_TYPE_VALUE_OUTPUT, TEE_PARAM_TYPE_NONE, |
| 298 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 299 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 300 | res = TEE_AllocatePersistentObjectEnumerator(&oe); |
| 301 | params[0].value.a = (uintptr_t)oe; |
| 302 | return res; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | TEE_Result ta_storage_cmd_free_enum(uint32_t param_types, TEE_Param params[4]) |
| 306 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 307 | TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a); |
| 308 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 309 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 310 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 311 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 312 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 313 | TEE_FreePersistentObjectEnumerator(oe); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 314 | return TEE_SUCCESS; |
| 315 | } |
| 316 | |
| 317 | TEE_Result ta_storage_cmd_reset_enum(uint32_t param_types, TEE_Param params[4]) |
| 318 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 319 | TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a); |
| 320 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 321 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 322 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 323 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 324 | |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 325 | TEE_ResetPersistentObjectEnumerator(oe); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 326 | return TEE_SUCCESS; |
| 327 | } |
| 328 | |
| 329 | TEE_Result ta_storage_cmd_start_enum(uint32_t param_types, TEE_Param params[4]) |
| 330 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 331 | TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a); |
| 332 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 333 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 334 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 335 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 336 | |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 337 | return TEE_StartPersistentObjectEnumerator(oe, params[0].value.b); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEE_Result ta_storage_cmd_next_enum(uint32_t param_types, TEE_Param params[4]) |
| 341 | { |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 342 | TEE_ObjectEnumHandle oe = VAL2HANDLE(params[0].value.a); |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 343 | TEE_ObjectInfo *obj = NULL; |
Jens Wiklander | c523159 | 2015-11-11 09:27:27 +0100 | [diff] [blame] | 344 | |
Pascal Brand | c603e0d | 2016-04-25 12:37:18 +0200 | [diff] [blame] | 345 | if (TEE_PARAM_TYPE_GET(param_types, 0) != TEE_PARAM_TYPE_VALUE_INPUT) |
| 346 | return TEE_ERROR_BAD_PARAMETERS; |
| 347 | if (TEE_PARAM_TYPE_GET(param_types, 2) != TEE_PARAM_TYPE_MEMREF_OUTPUT) |
| 348 | return TEE_ERROR_BAD_PARAMETERS; |
| 349 | if (TEE_PARAM_TYPE_GET(param_types, 3) != TEE_PARAM_TYPE_NONE) |
| 350 | return TEE_ERROR_BAD_PARAMETERS; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 351 | |
Pascal Brand | c603e0d | 2016-04-25 12:37:18 +0200 | [diff] [blame] | 352 | if (TEE_PARAM_TYPE_GET(param_types, 1) == TEE_PARAM_TYPE_NONE) |
| 353 | obj = NULL; |
| 354 | else if (TEE_PARAM_TYPE_GET(param_types, 1) == |
| 355 | TEE_PARAM_TYPE_MEMREF_OUTPUT) { |
| 356 | if (params[1].memref.size < sizeof(TEE_ObjectInfo)) { |
| 357 | params[1].memref.size = sizeof(TEE_ObjectInfo); |
| 358 | return TEE_ERROR_SHORT_BUFFER; |
| 359 | } |
| 360 | params[1].memref.size = sizeof(TEE_ObjectInfo); |
| 361 | obj = (TEE_ObjectInfo *)params[1].memref.buffer; |
| 362 | } else |
| 363 | return TEE_ERROR_BAD_PARAMETERS; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 364 | |
| 365 | if (params[2].memref.size < TEE_OBJECT_ID_MAX_LEN) |
| 366 | return TEE_ERROR_SHORT_BUFFER; |
| 367 | |
Pascal Brand | c603e0d | 2016-04-25 12:37:18 +0200 | [diff] [blame] | 368 | return TEE_GetNextPersistentObject(oe, obj, |
| 369 | params[2].memref.buffer, |
| 370 | ¶ms[2].memref.size); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 371 | } |
Pascal Brand | 90f2335 | 2016-05-19 15:15:47 +0200 | [diff] [blame] | 372 | |
| 373 | static TEE_Result check_obj(TEE_ObjectInfo *o1, TEE_ObjectInfo *o2) |
| 374 | { |
| 375 | if ((o1->objectType != o2->objectType) || |
| 376 | (o1->keySize != o2->keySize) || |
| 377 | (o1->maxKeySize != o2->maxKeySize) || |
| 378 | (o1->objectUsage != o2->objectUsage)) |
| 379 | return TEE_ERROR_GENERIC; |
| 380 | return TEE_SUCCESS; |
| 381 | } |
| 382 | |
| 383 | TEE_Result ta_storage_cmd_key_in_persistent(uint32_t param_types, |
| 384 | TEE_Param params[4]) |
| 385 | { |
| 386 | TEE_Result result = TEE_SUCCESS; |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 387 | TEE_ObjectHandle transient_key = TEE_HANDLE_NULL; |
| 388 | TEE_ObjectHandle persistent_key = TEE_HANDLE_NULL; |
| 389 | TEE_ObjectHandle key = TEE_HANDLE_NULL; |
| 390 | TEE_OperationHandle encrypt_op = TEE_HANDLE_NULL; |
Pascal Brand | 90f2335 | 2016-05-19 15:15:47 +0200 | [diff] [blame] | 391 | TEE_ObjectInfo keyInfo; |
| 392 | TEE_ObjectInfo keyInfo2; |
| 393 | TEE_ObjectInfo keyInfo3; |
| 394 | uint32_t alg = TEE_ALG_AES_CBC_NOPAD; |
| 395 | void *IV = NULL; |
| 396 | size_t IVlen = 16; |
| 397 | size_t key_size = 256; |
| 398 | uint32_t objectID = 1; |
| 399 | uint32_t flags = TEE_DATA_FLAG_ACCESS_READ | |
| 400 | TEE_DATA_FLAG_ACCESS_WRITE | |
| 401 | TEE_DATA_FLAG_ACCESS_WRITE_META | |
| 402 | TEE_DATA_FLAG_SHARE_READ | |
| 403 | TEE_DATA_FLAG_SHARE_WRITE; |
| 404 | |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 405 | TEE_MemFill(&keyInfo, 0, sizeof(keyInfo)); |
| 406 | TEE_MemFill(&keyInfo2, 0, sizeof(keyInfo2)); |
| 407 | TEE_MemFill(&keyInfo3, 0, sizeof(keyInfo3)); |
| 408 | |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 409 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 410 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 411 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
Pascal Brand | 90f2335 | 2016-05-19 15:15:47 +0200 | [diff] [blame] | 412 | |
| 413 | result = TEE_AllocateTransientObject(TEE_TYPE_AES, key_size, |
| 414 | &transient_key); |
| 415 | if (result != TEE_SUCCESS) { |
| 416 | EMSG("Failed to Allocate transient object handle : 0x%x", |
| 417 | result); |
| 418 | goto cleanup1; |
| 419 | } |
| 420 | |
| 421 | result = TEE_GenerateKey(transient_key, key_size, NULL, 0); |
| 422 | if (result != TEE_SUCCESS) { |
| 423 | EMSG("Failed to generate a transient key: 0x%x", result); |
| 424 | goto cleanup2; |
| 425 | } |
| 426 | |
| 427 | TEE_GetObjectInfo1(transient_key, &keyInfo); |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 428 | result = TEE_CreatePersistentObject(params[0].value.a, |
Pascal Brand | 90f2335 | 2016-05-19 15:15:47 +0200 | [diff] [blame] | 429 | &objectID, sizeof(objectID), |
| 430 | flags, transient_key, NULL, 0, |
| 431 | &persistent_key); |
| 432 | if (result != TEE_SUCCESS) { |
| 433 | EMSG("Failed to create a persistent key: 0x%x", result); |
| 434 | goto cleanup2; |
| 435 | } |
| 436 | |
| 437 | TEE_GetObjectInfo1(persistent_key, &keyInfo2); |
| 438 | result = check_obj(&keyInfo, &keyInfo2); |
| 439 | if (result != TEE_SUCCESS) { |
| 440 | EMSG("keyInfo and keyInfo2 are different"); |
| 441 | goto cleanup2; |
| 442 | } |
| 443 | |
| 444 | TEE_CloseObject(persistent_key); |
| 445 | |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 446 | result = TEE_OpenPersistentObject(params[0].value.a, |
Pascal Brand | 90f2335 | 2016-05-19 15:15:47 +0200 | [diff] [blame] | 447 | &objectID, sizeof(objectID), |
| 448 | flags, &key); |
| 449 | if (result != TEE_SUCCESS) { |
| 450 | EMSG("Failed to open persistent key: 0x%x", result); |
| 451 | goto cleanup2; |
| 452 | } |
| 453 | |
| 454 | TEE_GetObjectInfo(key, &keyInfo3); |
| 455 | result = check_obj(&keyInfo3, &keyInfo2); |
| 456 | if (result != TEE_SUCCESS) { |
| 457 | EMSG("keyInfo2 and keyInfo3 are different"); |
| 458 | goto cleanup2; |
| 459 | } |
| 460 | |
| 461 | result = TEE_AllocateOperation(&encrypt_op, alg, TEE_MODE_ENCRYPT, |
| 462 | keyInfo3.maxObjectSize); |
| 463 | if (result != TEE_SUCCESS) { |
| 464 | EMSG("Failed to allocate an operation: 0x%x", result); |
| 465 | goto cleanup3; |
| 466 | } |
| 467 | |
| 468 | result = TEE_SetOperationKey(encrypt_op, key); |
| 469 | if (result != TEE_SUCCESS) { |
| 470 | EMSG("Failed to set operation key: 0x%x", result); |
| 471 | goto cleanup4; |
| 472 | } |
| 473 | |
| 474 | IV = TEE_Malloc(IVlen, 0); |
| 475 | if (!IV) { |
| 476 | EMSG("Out of memory for IV."); |
| 477 | result = TEE_ERROR_OUT_OF_MEMORY; |
| 478 | goto cleanup4; |
| 479 | } |
| 480 | |
| 481 | TEE_CipherInit(encrypt_op, IV, IVlen); |
| 482 | TEE_Free(IV); |
| 483 | |
| 484 | cleanup4: |
| 485 | TEE_FreeOperation(encrypt_op); |
| 486 | cleanup3: |
| 487 | TEE_CloseAndDeletePersistentObject1(key); |
| 488 | cleanup2: |
| 489 | TEE_FreeTransientObject(transient_key); |
| 490 | cleanup1: |
| 491 | return result; |
| 492 | } |
| 493 | |
Pascal Brand | 29ee18f | 2016-05-23 14:13:56 +0200 | [diff] [blame] | 494 | TEE_Result ta_storage_cmd_loop(uint32_t param_types, TEE_Param params[4]) |
| 495 | { |
| 496 | TEE_ObjectHandle object = TEE_HANDLE_NULL; |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 497 | TEE_Result res = TEE_ERROR_GENERIC; |
Pascal Brand | 29ee18f | 2016-05-23 14:13:56 +0200 | [diff] [blame] | 498 | int object_id = 0; |
| 499 | uint32_t flags = TEE_DATA_FLAG_OVERWRITE | |
| 500 | TEE_DATA_FLAG_ACCESS_WRITE_META; |
| 501 | int i = 0; |
| 502 | |
Pascal Brand | 29ee18f | 2016-05-23 14:13:56 +0200 | [diff] [blame] | 503 | (void)params; |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 504 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 505 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 506 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
Pascal Brand | 29ee18f | 2016-05-23 14:13:56 +0200 | [diff] [blame] | 507 | |
| 508 | for (i = 0; i < 20; i++) { |
| 509 | DMSG("\n\nLOOP : %d", i); |
| 510 | object = TEE_HANDLE_NULL; |
| 511 | object_id = i; |
Jerome Forissier | 0e99d6a | 2016-07-25 14:21:43 +0200 | [diff] [blame] | 512 | res = TEE_CreatePersistentObject(params[0].value.a, |
Pascal Brand | 29ee18f | 2016-05-23 14:13:56 +0200 | [diff] [blame] | 513 | &object_id, sizeof(int), flags, |
| 514 | TEE_HANDLE_NULL, NULL, 0, |
| 515 | &object); |
| 516 | |
| 517 | if (res != TEE_SUCCESS) { |
| 518 | EMSG("FAIL"); |
| 519 | return res; |
| 520 | } |
| 521 | |
| 522 | res = TEE_CloseAndDeletePersistentObject1(object); |
| 523 | if (res != TEE_SUCCESS) { |
| 524 | EMSG("FAIL"); |
| 525 | return res; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | return TEE_SUCCESS; |
| 530 | } |
Jens Wiklander | e6d4ddd | 2016-09-14 15:50:48 +0200 | [diff] [blame] | 531 | |
| 532 | TEE_Result ta_storage_cmd_restrict_usage(uint32_t param_types, |
| 533 | TEE_Param params[4]) |
| 534 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 535 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
Jens Wiklander | e6d4ddd | 2016-09-14 15:50:48 +0200 | [diff] [blame] | 536 | |
| 537 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 538 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 539 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 540 | |
| 541 | o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a; |
| 542 | TEE_RestrictObjectUsage1(o, params[0].value.b); |
| 543 | return TEE_SUCCESS; |
| 544 | } |
| 545 | |
| 546 | TEE_Result ta_storage_cmd_alloc_obj(uint32_t param_types, TEE_Param params[4]) |
| 547 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 548 | TEE_Result res = TEE_ERROR_GENERIC; |
| 549 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
Jens Wiklander | e6d4ddd | 2016-09-14 15:50:48 +0200 | [diff] [blame] | 550 | |
| 551 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 552 | (TEE_PARAM_TYPE_VALUE_INPUT, |
| 553 | TEE_PARAM_TYPE_VALUE_OUTPUT, |
| 554 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 555 | |
| 556 | res = TEE_AllocateTransientObject(params[0].value.a, params[0].value.b, |
| 557 | &o); |
| 558 | params[1].value.a = (uint32_t)(uintptr_t)o; |
| 559 | return res; |
| 560 | } |
| 561 | |
| 562 | TEE_Result ta_storage_cmd_free_obj(uint32_t param_types, TEE_Param params[4]) |
| 563 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 564 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
Jens Wiklander | e6d4ddd | 2016-09-14 15:50:48 +0200 | [diff] [blame] | 565 | |
| 566 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 567 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 568 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 569 | |
| 570 | o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a; |
| 571 | TEE_FreeTransientObject(o); |
| 572 | return TEE_SUCCESS; |
| 573 | } |
| 574 | |
| 575 | TEE_Result ta_storage_cmd_reset_obj(uint32_t param_types, TEE_Param params[4]) |
| 576 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 577 | TEE_ObjectHandle o = TEE_HANDLE_NULL; |
Jens Wiklander | e6d4ddd | 2016-09-14 15:50:48 +0200 | [diff] [blame] | 578 | |
| 579 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 580 | (TEE_PARAM_TYPE_VALUE_INPUT, TEE_PARAM_TYPE_NONE, |
| 581 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE)); |
| 582 | |
| 583 | o = (TEE_ObjectHandle)(uintptr_t)params[0].value.a; |
| 584 | TEE_ResetTransientObject(o); |
| 585 | return TEE_SUCCESS; |
| 586 | } |
Guanchao Liang | 31a9cbf | 2016-12-20 00:35:26 +0800 | [diff] [blame] | 587 | |
| 588 | TEE_Result ta_storage_cmd_get_obj_info(uint32_t param_types, |
| 589 | TEE_Param params[4]) |
| 590 | { |
Etienne Carriere | 102092e | 2019-03-28 15:24:22 +0100 | [diff] [blame] | 591 | TEE_Result res = TEE_ERROR_GENERIC; |
| 592 | TEE_ObjectInfo *info = NULL; |
Guanchao Liang | 31a9cbf | 2016-12-20 00:35:26 +0800 | [diff] [blame] | 593 | TEE_ObjectHandle o = VAL2HANDLE(params[0].value.a); |
| 594 | |
| 595 | ASSERT_PARAM_TYPE(TEE_PARAM_TYPES |
| 596 | (TEE_PARAM_TYPE_VALUE_INPUT, |
| 597 | TEE_PARAM_TYPE_MEMREF_OUTPUT, TEE_PARAM_TYPE_NONE, |
| 598 | TEE_PARAM_TYPE_NONE)); |
| 599 | |
| 600 | info = (TEE_ObjectInfo *)params[1].memref.buffer; |
| 601 | res = TEE_GetObjectInfo1(o, info); |
| 602 | |
| 603 | return res; |
| 604 | } |