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