Cedric Chaumont | 1390f3a | 2015-08-31 13:55:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, STMicroelectronics International N.V. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #ifdef CFG_ENC_FS |
| 15 | |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <stdio.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <adbg.h> |
| 23 | #include <xtest_test.h> |
| 24 | #include <xtest_helpers.h> |
| 25 | |
| 26 | #include <tee_fs_key_manager.h> |
| 27 | #include <tee_client_api.h> |
| 28 | #include <ta_storage.h> |
| 29 | #include <tee_api_defines.h> |
| 30 | #include <tee_api_types.h> |
| 31 | |
| 32 | #define SIZE 1 |
| 33 | #define NUMELEM 1 |
| 34 | #define DUMPFILE 0 |
| 35 | #define DUMPLIMIT 128 |
| 36 | |
| 37 | #define CORRUPT_META_KEY_OFFSET offsetof(struct meta_header, encrypted_key) |
| 38 | #define CORRUPT_META_IV_OFFSET (offsetof(struct meta_header, common) + \ |
| 39 | offsetof(struct common_header, iv)) |
| 40 | #define CORRUPT_META_TAG_OFFSET (offsetof(struct meta_header, common) + \ |
| 41 | offsetof(struct common_header, tag)) |
| 42 | #define CORRUPT_META_DATA_OFFSET sizeof(struct meta_header) |
| 43 | |
| 44 | #define CORRUPT_BLOCK_IV_OFFSET offsetof(struct common_header, iv) |
| 45 | #define CORRUPT_BLOCK_TAG_OFFSET offsetof(struct common_header, tag) |
| 46 | #define CORRUPT_BLOCK_DATA_OFFSET sizeof(struct block_header) |
| 47 | |
| 48 | #define CORRUPT_FILE_RAND_BYTE 1024*4096+2 |
| 49 | #define CORRUPT_FILE_FIRST_BYTE 1024*4096+1 |
| 50 | #define CORRUPT_FILE_LAST_BYTE 1024*4096 |
| 51 | |
| 52 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 53 | |
| 54 | #ifndef MIN |
| 55 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| 56 | #endif |
| 57 | |
| 58 | #define SWAP_BYTES_16(w16) ((((w16) & 0xFF00) >> 8) | (((w16) & 0xFF) << 8)) |
| 59 | #define SWAP_BYTES_32(w32) ((((w32) & 0xFF000000) >> 24) |\ |
| 60 | (((w32) & 0xFF0000) >> 8) |\ |
| 61 | (((w32) & 0xFF00) << 8) |\ |
| 62 | (((w32) & 0xFF) << 24)) |
| 63 | |
| 64 | #define XTEST_ENC_FS(level, data_len, meta, block_num, version) \ |
| 65 | { \ |
| 66 | level, \ |
| 67 | data_len, \ |
| 68 | meta, block_num, version \ |
| 69 | } |
| 70 | |
| 71 | enum meta { |
| 72 | META0, |
| 73 | META1 |
| 74 | }; |
| 75 | |
| 76 | enum version { |
| 77 | VERSION0, |
| 78 | VERSION1 |
| 79 | }; |
| 80 | |
| 81 | enum block_num { |
| 82 | BLOCK0, |
| 83 | BLOCK1, |
| 84 | BLOCK2, |
| 85 | BLOCK3, |
| 86 | BLOCK4, |
| 87 | BLOCK5 |
| 88 | }; |
| 89 | |
| 90 | struct xtest_enc_fs_case { |
| 91 | uint8_t level; |
| 92 | uint32_t data_len; |
| 93 | uint8_t meta; |
| 94 | uint8_t block_num; |
| 95 | uint8_t version; |
| 96 | }; |
| 97 | |
| 98 | static const struct xtest_enc_fs_case xtest_enc_fs_cases[] = { |
| 99 | XTEST_ENC_FS(1, 1, META0, BLOCK0, VERSION1), |
| 100 | XTEST_ENC_FS(1, 2, META0, BLOCK0, VERSION1), |
| 101 | XTEST_ENC_FS(1, 3, META0, BLOCK0, VERSION1), |
| 102 | XTEST_ENC_FS(1, 4, META0, BLOCK0, VERSION1), |
| 103 | XTEST_ENC_FS(1, 8, META0, BLOCK0, VERSION1), |
| 104 | XTEST_ENC_FS(1, 16, META0, BLOCK0, VERSION1), |
| 105 | XTEST_ENC_FS(1, 32, META0, BLOCK0, VERSION1), |
| 106 | XTEST_ENC_FS(1, 64, META0, BLOCK0, VERSION1), |
| 107 | XTEST_ENC_FS(1, 128, META0, BLOCK0, VERSION1), |
| 108 | XTEST_ENC_FS(1, 256, META0, BLOCK0, VERSION1), |
| 109 | XTEST_ENC_FS(1, 512, META0, BLOCK0, VERSION1), |
| 110 | XTEST_ENC_FS(1, 1024, META0, BLOCK0, VERSION1), |
| 111 | XTEST_ENC_FS(1, 2048, META0, BLOCK0, VERSION1), |
| 112 | XTEST_ENC_FS(1, 3072, META0, BLOCK0, VERSION1), |
| 113 | XTEST_ENC_FS(1, 4094, META0, BLOCK0, VERSION1), |
| 114 | XTEST_ENC_FS(1, 4095, META0, BLOCK0, VERSION1), |
| 115 | XTEST_ENC_FS(0, 4097, META0, BLOCK0, VERSION1), |
| 116 | XTEST_ENC_FS(0, 4097, META0, BLOCK1, VERSION0), |
| 117 | XTEST_ENC_FS(1, 4098, META0, BLOCK0, VERSION1), |
| 118 | XTEST_ENC_FS(1, 4098, META0, BLOCK1, VERSION0), |
| 119 | XTEST_ENC_FS(1, 1*4096, META0, BLOCK1, VERSION0), |
| 120 | XTEST_ENC_FS(1, 2*4096, META0, BLOCK2, VERSION0), |
| 121 | XTEST_ENC_FS(1, 3*4096, META0, BLOCK3, VERSION0), |
| 122 | XTEST_ENC_FS(1, 4*4096, META0, BLOCK3, VERSION0), |
| 123 | XTEST_ENC_FS(1, 4*4096, META0, BLOCK4, VERSION0), |
| 124 | }; |
| 125 | |
| 126 | static TEEC_Result obj_open(TEEC_Session *sess, void *id, uint32_t id_size, |
| 127 | uint32_t flags, uint32_t *obj) |
| 128 | { |
| 129 | TEEC_Operation op; |
| 130 | TEEC_Result res; |
| 131 | uint32_t org; |
| 132 | |
| 133 | memset(&op, 0, sizeof(op)); |
| 134 | op.params[0].tmpref.buffer = id; |
| 135 | op.params[0].tmpref.size = id_size; |
| 136 | op.params[1].value.a = flags; |
| 137 | op.params[1].value.b = 0; |
| 138 | |
| 139 | op.paramTypes = TEEC_PARAM_TYPES( |
| 140 | TEEC_MEMREF_TEMP_INPUT, TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE); |
| 141 | |
| 142 | res = TEEC_InvokeCommand(sess, TA_STORAGE_CMD_OPEN, &op, &org); |
| 143 | |
| 144 | if (res == TEEC_SUCCESS) |
| 145 | *obj = op.params[1].value.b; |
| 146 | |
| 147 | return res; |
| 148 | } |
| 149 | |
| 150 | static TEEC_Result obj_create(TEEC_Session *sess, void *id, uint32_t id_size, |
| 151 | uint32_t flags, uint32_t attr, void *data, |
| 152 | uint32_t data_size, uint32_t *obj) |
| 153 | { |
| 154 | TEEC_Operation op; |
| 155 | TEEC_Result res; |
| 156 | uint32_t org; |
| 157 | |
| 158 | memset(&op, 0, sizeof(op)); |
| 159 | op.params[0].tmpref.buffer = id; |
| 160 | op.params[0].tmpref.size = id_size; |
| 161 | op.params[1].value.a = flags; |
| 162 | op.params[1].value.b = 0; |
| 163 | op.params[2].value.a = attr; |
| 164 | op.params[2].value.b = 0; |
| 165 | op.params[3].tmpref.buffer = data; |
| 166 | op.params[3].tmpref.size = data_size; |
| 167 | |
| 168 | op.paramTypes = TEEC_PARAM_TYPES( |
| 169 | TEEC_MEMREF_TEMP_INPUT, TEEC_VALUE_INOUT, TEEC_VALUE_INPUT, |
| 170 | TEEC_MEMREF_TEMP_INPUT); |
| 171 | |
| 172 | res = TEEC_InvokeCommand(sess, TA_STORAGE_CMD_CREATE, &op, &org); |
| 173 | |
| 174 | if (res == TEEC_SUCCESS) |
| 175 | *obj = op.params[1].value.b; |
| 176 | |
| 177 | return res; |
| 178 | } |
| 179 | |
| 180 | static TEEC_Result obj_read(TEEC_Session *sess, uint32_t obj, void *data, |
| 181 | uint32_t data_size, uint32_t *count) |
| 182 | { |
| 183 | TEEC_Result res; |
| 184 | TEEC_Operation op; |
| 185 | uint32_t org; |
| 186 | |
| 187 | memset(&op, 0, sizeof(op)); |
| 188 | op.params[0].tmpref.buffer = data; |
| 189 | op.params[0].tmpref.size = data_size; |
| 190 | op.params[1].value.a = obj; |
| 191 | op.params[1].value.b = 0; |
| 192 | |
| 193 | op.paramTypes = TEEC_PARAM_TYPES( |
| 194 | TEEC_MEMREF_TEMP_OUTPUT, TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE); |
| 195 | |
| 196 | res = TEEC_InvokeCommand(sess, TA_STORAGE_CMD_READ, &op, &org); |
| 197 | |
| 198 | if (res == TEEC_SUCCESS) |
| 199 | *count = op.params[1].value.b; |
| 200 | |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | static TEEC_Result obj_close(TEEC_Session *sess, uint32_t obj) |
| 205 | { |
| 206 | TEEC_Operation op; |
| 207 | uint32_t org; |
| 208 | |
| 209 | memset(&op, 0, sizeof(op)); |
| 210 | op.params[0].value.a = obj; |
| 211 | |
| 212 | op.paramTypes = TEEC_PARAM_TYPES( |
| 213 | TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 214 | |
| 215 | return TEEC_InvokeCommand(sess, TA_STORAGE_CMD_CLOSE, &op, &org); |
| 216 | } |
| 217 | |
| 218 | static int get_ta_dirname(TEEC_UUID *p_uuid, char *buffer, uint32_t len) |
| 219 | { |
| 220 | |
| 221 | if (p_uuid == NULL || buffer == NULL) |
| 222 | return 0; |
| 223 | |
| 224 | return snprintf(buffer, len, |
| 225 | "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X", |
| 226 | SWAP_BYTES_32(p_uuid->timeLow), |
| 227 | SWAP_BYTES_16(p_uuid->timeMid), |
| 228 | SWAP_BYTES_16(p_uuid->timeHiAndVersion), |
| 229 | p_uuid->clockSeqAndNode[0], |
| 230 | p_uuid->clockSeqAndNode[1], |
| 231 | p_uuid->clockSeqAndNode[2], |
| 232 | p_uuid->clockSeqAndNode[3], |
| 233 | p_uuid->clockSeqAndNode[4], |
| 234 | p_uuid->clockSeqAndNode[5], |
| 235 | p_uuid->clockSeqAndNode[6], |
| 236 | p_uuid->clockSeqAndNode[7]); |
| 237 | } |
| 238 | |
| 239 | static int get_obj_filename(void *file_id, uint32_t file_id_length, |
| 240 | char *buffer, uint32_t len) |
| 241 | { |
| 242 | char *p = buffer; |
| 243 | uint32_t i; |
| 244 | |
| 245 | if (file_id == NULL || buffer == NULL) |
| 246 | return 0; |
| 247 | |
| 248 | for (i=0; i<file_id_length; i++) |
| 249 | p += snprintf(p, len, "%02X", ((uint8_t *)file_id)[i]); |
| 250 | |
| 251 | return p-buffer; |
| 252 | } |
| 253 | |
| 254 | static int dump_file(FILE *fd, char *buffer, uint32_t len) |
| 255 | { |
| 256 | |
| 257 | uint16_t format = 16; |
| 258 | uint16_t size; |
| 259 | int res=-1; |
| 260 | |
| 261 | printf("o Dump data (limit %d bytes)\n", DUMPLIMIT); |
| 262 | if ( 0 != fseek(fd, 0, SEEK_SET)) |
| 263 | goto exit; |
| 264 | fread(buffer, 1, len, fd); |
| 265 | size = ( len < DUMPLIMIT ) ? len : DUMPLIMIT; |
| 266 | Do_ADBG_HexLog(buffer, size, format); |
| 267 | res=0; |
| 268 | exit: |
| 269 | return res; |
| 270 | } |
| 271 | |
| 272 | static int is_obj_present(TEEC_UUID *p_uuid, void *file_id, |
| 273 | uint32_t file_id_length, |
| 274 | enum tee_fs_file_type file_type, |
| 275 | uint8_t block_num, uint8_t version) |
| 276 | { |
| 277 | char ta_dirname[32 + 1]; |
| 278 | char obj_filename[2*file_id_length + 1]; |
| 279 | char path[150]; |
| 280 | struct stat sb; |
| 281 | |
| 282 | if (get_ta_dirname(p_uuid, ta_dirname, sizeof(ta_dirname)) && |
| 283 | get_obj_filename(file_id, file_id_length, obj_filename, |
| 284 | sizeof(obj_filename))) { |
| 285 | if (file_type == META_FILE) { |
| 286 | snprintf(path, sizeof(path), "/data/tee/%s/%s/meta.%d", |
| 287 | ta_dirname, obj_filename, version); |
| 288 | } else if (file_type == BLOCK_FILE) { |
| 289 | snprintf(path, sizeof(path), |
| 290 | "/data/tee/%s/%s/block%d.%d", ta_dirname, |
| 291 | obj_filename, block_num, version); |
| 292 | } else |
| 293 | goto err; |
| 294 | |
| 295 | return !stat(path, &sb); |
| 296 | } |
| 297 | err: |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int obj_corrupt(TEEC_UUID *p_uuid, void *file_id, |
| 302 | uint32_t file_id_length, |
| 303 | uint32_t offset, enum tee_fs_file_type file_type, |
| 304 | uint8_t block_num, uint8_t version, uint32_t data_len) |
| 305 | { |
| 306 | char ta_dirname[32 + 1]; |
| 307 | char obj_filename[2*file_id_length + 1]; |
| 308 | char name[500]; |
| 309 | FILE *fd; |
| 310 | uint8_t byte; |
| 311 | int res=-1; |
| 312 | char buffer[data_len]; |
| 313 | struct stat st; |
| 314 | |
| 315 | if (get_ta_dirname(p_uuid, ta_dirname, sizeof(ta_dirname)) && |
| 316 | get_obj_filename(file_id, file_id_length, obj_filename, |
| 317 | sizeof(obj_filename))) { |
| 318 | |
| 319 | /* |
| 320 | * read the byte at the given offset, |
| 321 | * do a bitwise negation, |
| 322 | * and rewrite this value at the same offset |
| 323 | */ |
| 324 | |
| 325 | if (file_type == META_FILE) { |
| 326 | snprintf(name, sizeof(name), |
| 327 | "/data/tee/%s/%s/meta.%d", |
| 328 | ta_dirname, obj_filename, version); |
| 329 | } |
| 330 | |
| 331 | if (file_type == BLOCK_FILE) { |
| 332 | snprintf(name, sizeof(name), |
| 333 | "/data/tee/%s/%s/block%d.%d", |
| 334 | ta_dirname, obj_filename, block_num, version); |
| 335 | } |
| 336 | |
| 337 | res = stat(name, &st); |
| 338 | if (res < 0) |
| 339 | goto exit; |
| 340 | |
| 341 | if (offset == CORRUPT_FILE_FIRST_BYTE) { |
| 342 | offset = 0; |
| 343 | |
| 344 | } else if (offset == CORRUPT_FILE_LAST_BYTE) { |
| 345 | offset = st.st_size-1; |
| 346 | |
| 347 | } else if (offset == CORRUPT_FILE_RAND_BYTE) { |
| 348 | srand(time(NULL)); |
| 349 | offset = rand() % (st.st_size-1); |
| 350 | } |
| 351 | |
Jerome Forissier | 679ee82 | 2015-10-29 11:05:33 +0100 | [diff] [blame] | 352 | fd = fopen(name, "r+"); |
Cedric Chaumont | 1390f3a | 2015-08-31 13:55:16 +0200 | [diff] [blame] | 353 | if (!fd) |
| 354 | goto exit; |
| 355 | |
| 356 | if (DUMPFILE) |
| 357 | dump_file(fd, buffer, sizeof(buffer)); |
| 358 | |
| 359 | if (0 != fseek(fd, offset, SEEK_SET)) |
| 360 | goto exit; |
| 361 | |
| 362 | if (SIZE*NUMELEM != fread(&byte, SIZE, NUMELEM,fd)) |
| 363 | goto exit; |
| 364 | |
| 365 | printf("o Corrupt %s\n", name); |
| 366 | printf("o Size: %ld byte(s)\n", st.st_size); |
| 367 | printf("o Byte offset: %u (0x%04X), ", offset, offset); |
| 368 | printf("Old value: 0x%x, ", byte); |
| 369 | byte += 1; |
| 370 | printf("New value: 0x%x\n", byte); |
| 371 | |
| 372 | if ( 0 != fseek(fd, offset, SEEK_SET)) |
| 373 | goto exit; |
| 374 | |
| 375 | if (SIZE*NUMELEM != fwrite(&byte, SIZE, NUMELEM,fd)) |
| 376 | goto exit; |
| 377 | |
| 378 | if (DUMPFILE) |
| 379 | dump_file(fd, buffer, sizeof(buffer)); |
| 380 | |
| 381 | fclose(fd); |
| 382 | |
| 383 | res = 0; |
| 384 | goto exit; |
| 385 | } |
| 386 | |
| 387 | exit: |
| 388 | return res; |
| 389 | } |
| 390 | |
| 391 | static void storage_corrupt(ADBG_Case_t *c, |
| 392 | enum tee_fs_file_type file_type, |
| 393 | uint32_t offset |
| 394 | ) |
| 395 | { |
| 396 | TEEC_Context ctx; |
| 397 | TEEC_Session sess; |
| 398 | TEEC_UUID uuid = TA_STORAGE_UUID; |
| 399 | unsigned int error; |
| 400 | uint32_t obj_id; |
| 401 | uint32_t nb; |
| 402 | size_t n; |
| 403 | |
| 404 | |
| 405 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 406 | TEEC_InitializeContext(_device, &ctx)); |
| 407 | |
| 408 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 409 | TEEC_OpenSession(&ctx, &sess, &uuid, |
| 410 | TEEC_LOGIN_PUBLIC, NULL, NULL, &error)); |
| 411 | |
| 412 | for (n = 0; n < ARRAY_SIZE(xtest_enc_fs_cases); n++) { |
| 413 | |
| 414 | const struct xtest_enc_fs_case *tv = xtest_enc_fs_cases + n; |
| 415 | |
| 416 | if (tv->level > level) |
| 417 | continue; |
| 418 | |
| 419 | char buffer[tv->data_len]; |
| 420 | char filename[20]; |
| 421 | char *filedata = NULL; |
| 422 | size_t p; |
| 423 | uint8_t data_byte = 0; |
| 424 | snprintf(filename, sizeof(filename), "file_%dB", tv->data_len); |
| 425 | filedata = malloc(tv->data_len * sizeof(*filedata)); |
| 426 | if (!filedata) |
| 427 | goto exit; |
| 428 | |
| 429 | for (p = 0; p < tv->data_len; p++) { |
| 430 | filedata[p] = data_byte; |
| 431 | data_byte++; |
| 432 | }; |
| 433 | |
| 434 | Do_ADBG_BeginSubCase(c, "| filename: %s , data size: %d byte(s)", |
| 435 | filename, tv->data_len); |
| 436 | |
| 437 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 438 | obj_create(&sess, filename, ARRAY_SIZE(filename), |
| 439 | TEE_DATA_FLAG_ACCESS_WRITE, 0, filedata, |
| 440 | tv->data_len, &obj_id)); |
| 441 | |
| 442 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 443 | obj_close(&sess, obj_id)); |
| 444 | |
| 445 | if (file_type == META_FILE) |
| 446 | ADBG_EXPECT_COMPARE_UNSIGNED(c, 0, !=, |
| 447 | is_obj_present(&uuid, filename, |
| 448 | ARRAY_SIZE(filename), file_type, |
| 449 | tv->meta, tv->meta)); |
| 450 | |
| 451 | if (file_type == BLOCK_FILE) |
| 452 | ADBG_EXPECT_COMPARE_UNSIGNED(c, 0, !=, |
| 453 | is_obj_present(&uuid, filename, |
| 454 | ARRAY_SIZE(filename), file_type, |
| 455 | tv->block_num, tv->version)); |
| 456 | |
| 457 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 458 | obj_open(&sess, filename, ARRAY_SIZE(filename), |
| 459 | TEE_DATA_FLAG_ACCESS_READ | |
| 460 | TEE_DATA_FLAG_ACCESS_WRITE_META, &obj_id)); |
| 461 | |
| 462 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 463 | obj_read(&sess, obj_id, buffer, tv->data_len, &nb)); |
| 464 | |
| 465 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 466 | obj_close(&sess, obj_id)); |
| 467 | |
| 468 | switch (file_type) { |
| 469 | case META_FILE: |
| 470 | /* corrupt object */ |
| 471 | if (!ADBG_EXPECT(c, TEE_SUCCESS, |
| 472 | obj_corrupt(&uuid, filename, |
| 473 | ARRAY_SIZE(filename), offset, |
| 474 | file_type, tv->meta, tv->meta, |
| 475 | tv->data_len))) |
| 476 | goto exit; |
| 477 | |
| 478 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_CORRUPT_OBJECT, |
| 479 | obj_open(&sess, filename, |
| 480 | ARRAY_SIZE(filename), |
| 481 | TEE_DATA_FLAG_ACCESS_READ | |
| 482 | TEE_DATA_FLAG_ACCESS_WRITE_META, |
| 483 | &obj_id)); |
| 484 | |
| 485 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_ITEM_NOT_FOUND, |
| 486 | obj_open(&sess, filename, |
| 487 | ARRAY_SIZE(filename), |
| 488 | TEE_DATA_FLAG_ACCESS_READ | |
| 489 | TEE_DATA_FLAG_ACCESS_WRITE_META, |
| 490 | &obj_id)); |
| 491 | |
| 492 | ADBG_EXPECT_COMPARE_UNSIGNED(c, 0, ==, |
| 493 | is_obj_present(&uuid, |
| 494 | filename, |
| 495 | ARRAY_SIZE(filename), file_type, |
| 496 | tv->meta, tv->meta)); |
| 497 | break; |
| 498 | |
| 499 | case BLOCK_FILE: |
| 500 | /* corrupt object */ |
| 501 | if (!ADBG_EXPECT(c, TEE_SUCCESS, |
| 502 | obj_corrupt(&uuid, filename, |
| 503 | ARRAY_SIZE(filename), offset, |
| 504 | file_type, tv->block_num, tv->version, |
| 505 | tv->data_len))) |
| 506 | goto exit; |
| 507 | |
| 508 | if ( tv->block_num == BLOCK0 ) { |
| 509 | ADBG_EXPECT(c, TEE_ERROR_CORRUPT_OBJECT, |
| 510 | obj_open(&sess, filename, |
| 511 | ARRAY_SIZE(filename), |
| 512 | TEE_DATA_FLAG_ACCESS_READ | |
| 513 | TEE_DATA_FLAG_ACCESS_WRITE_META, |
| 514 | &obj_id)); |
| 515 | } else { |
| 516 | ADBG_EXPECT(c, TEE_SUCCESS, |
| 517 | obj_open(&sess, filename, |
| 518 | ARRAY_SIZE(filename), |
| 519 | TEE_DATA_FLAG_ACCESS_READ | |
| 520 | TEE_DATA_FLAG_ACCESS_WRITE_META, |
| 521 | &obj_id)); |
| 522 | |
| 523 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_CORRUPT_OBJECT, |
| 524 | obj_read(&sess, obj_id, buffer, |
| 525 | tv->data_len, &nb)); |
| 526 | } |
| 527 | |
| 528 | ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_ITEM_NOT_FOUND, |
| 529 | obj_open(&sess, filename, ARRAY_SIZE(filename), |
| 530 | TEE_DATA_FLAG_ACCESS_READ | |
| 531 | TEE_DATA_FLAG_ACCESS_WRITE_META, &obj_id)); |
| 532 | |
| 533 | ADBG_EXPECT_COMPARE_UNSIGNED(c, 0, ==, |
| 534 | is_obj_present(&uuid, |
| 535 | filename, |
| 536 | ARRAY_SIZE(filename), |
| 537 | file_type, tv->block_num, |
| 538 | tv->version)); |
| 539 | break; |
| 540 | |
| 541 | default: |
| 542 | printf("ERROR : Wrong file type\n"); |
| 543 | |
| 544 | } |
| 545 | |
| 546 | free(filedata); |
| 547 | Do_ADBG_EndSubCase(c, NULL); |
| 548 | }; |
| 549 | |
| 550 | exit: |
| 551 | TEEC_CloseSession(&sess); |
| 552 | TEEC_FinalizeContext(&ctx); |
| 553 | } |
| 554 | |
| 555 | /* Corrupt Meta Encrypted Key */ |
| 556 | static void xtest_tee_test_20001(ADBG_Case_t *c) |
| 557 | { |
| 558 | storage_corrupt(c, META_FILE, CORRUPT_META_KEY_OFFSET |
| 559 | ); |
| 560 | } |
| 561 | |
| 562 | /* Corrupt Meta IV */ |
| 563 | static void xtest_tee_test_20002(ADBG_Case_t *c) |
| 564 | { |
| 565 | storage_corrupt(c, META_FILE, CORRUPT_META_IV_OFFSET |
| 566 | ); |
| 567 | } |
| 568 | |
| 569 | /* Corrupt Meta Tag */ |
| 570 | static void xtest_tee_test_20003(ADBG_Case_t *c) |
| 571 | { |
| 572 | storage_corrupt(c, META_FILE, CORRUPT_META_TAG_OFFSET |
| 573 | ); |
| 574 | } |
| 575 | |
| 576 | /* Corrupt Meta Data */ |
| 577 | static void xtest_tee_test_20004(ADBG_Case_t *c) |
| 578 | { |
| 579 | storage_corrupt(c, |
| 580 | META_FILE, CORRUPT_META_DATA_OFFSET |
| 581 | ); |
| 582 | } |
| 583 | |
| 584 | /* Corrupt Meta File : first byte */ |
| 585 | static void xtest_tee_test_20021(ADBG_Case_t *c) |
| 586 | { |
| 587 | storage_corrupt(c, META_FILE, CORRUPT_FILE_FIRST_BYTE |
| 588 | ); |
| 589 | |
| 590 | } |
| 591 | |
| 592 | /* Corrupt Meta File : last byte */ |
| 593 | static void xtest_tee_test_20022(ADBG_Case_t *c) |
| 594 | { |
| 595 | storage_corrupt(c, META_FILE, CORRUPT_FILE_LAST_BYTE |
| 596 | ); |
| 597 | |
| 598 | } |
| 599 | |
| 600 | /* Corrupt Meta File : random byte */ |
| 601 | static void xtest_tee_test_20023(ADBG_Case_t *c) |
| 602 | { |
| 603 | storage_corrupt(c, META_FILE, CORRUPT_FILE_RAND_BYTE |
| 604 | ); |
| 605 | |
| 606 | } |
| 607 | |
| 608 | /* Corrupt Block IV */ |
| 609 | static void xtest_tee_test_20501(ADBG_Case_t *c) |
| 610 | { |
| 611 | storage_corrupt(c, BLOCK_FILE, CORRUPT_BLOCK_IV_OFFSET |
| 612 | ); |
| 613 | |
| 614 | } |
| 615 | |
| 616 | /* Corrupt Block Tag */ |
| 617 | static void xtest_tee_test_20502(ADBG_Case_t *c) |
| 618 | { |
| 619 | storage_corrupt(c, BLOCK_FILE, CORRUPT_BLOCK_TAG_OFFSET |
| 620 | ); |
| 621 | } |
| 622 | |
| 623 | /* Corrupt Block Data */ |
| 624 | static void xtest_tee_test_20503(ADBG_Case_t *c) |
| 625 | { |
| 626 | |
| 627 | storage_corrupt(c, BLOCK_FILE, CORRUPT_BLOCK_DATA_OFFSET |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | /* Corrupt Block File : first byte */ |
| 632 | static void xtest_tee_test_20521(ADBG_Case_t *c) |
| 633 | { |
| 634 | storage_corrupt(c, BLOCK_FILE, CORRUPT_FILE_FIRST_BYTE |
| 635 | ); |
| 636 | |
| 637 | } |
| 638 | |
| 639 | /* Corrupt Block File : last byte */ |
| 640 | static void xtest_tee_test_20522(ADBG_Case_t *c) |
| 641 | { |
| 642 | storage_corrupt(c, BLOCK_FILE, CORRUPT_FILE_LAST_BYTE |
| 643 | ); |
| 644 | |
| 645 | } |
| 646 | |
| 647 | /* Corrupt Block File : random byte */ |
| 648 | static void xtest_tee_test_20523(ADBG_Case_t *c) |
| 649 | { |
| 650 | storage_corrupt(c, BLOCK_FILE, CORRUPT_FILE_RAND_BYTE |
| 651 | ); |
| 652 | |
| 653 | } |
| 654 | |
| 655 | ADBG_CASE_DEFINE( |
| 656 | XTEST_TEE_20001, xtest_tee_test_20001, |
| 657 | /* Title */ |
| 658 | "Sanity Test Corrupt Meta Encrypted Key", |
| 659 | /* Short description */ |
| 660 | "Short description ...", |
| 661 | /* Requirement IDs */ |
| 662 | "TEE-??", |
| 663 | /* How to implement */ |
| 664 | "Description of how to implement ..." |
| 665 | ); |
| 666 | |
| 667 | ADBG_CASE_DEFINE( |
| 668 | XTEST_TEE_20002, xtest_tee_test_20002, |
| 669 | /* Title */ |
| 670 | "Sanity Test Corrupt Meta IV", |
| 671 | /* Short description */ |
| 672 | "Short description ...", |
| 673 | /* Requirement IDs */ |
| 674 | "TEE-??", |
| 675 | /* How to implement */ |
| 676 | "Description of how to implement ..." |
| 677 | ); |
| 678 | |
| 679 | ADBG_CASE_DEFINE( |
| 680 | XTEST_TEE_20003, xtest_tee_test_20003, |
| 681 | /* Title */ |
| 682 | "Sanity Test Corrupt Meta Tag", |
| 683 | /* Short description */ |
| 684 | "Short description ...", |
| 685 | /* Requirement IDs */ |
| 686 | "TEE-??", |
| 687 | /* How to implement */ |
| 688 | "Description of how to implement ..." |
| 689 | ); |
| 690 | |
| 691 | ADBG_CASE_DEFINE( |
| 692 | XTEST_TEE_20004, xtest_tee_test_20004, |
| 693 | /* Title */ |
| 694 | "Sanity Test Corrupt Meta Data", |
| 695 | /* Short description */ |
| 696 | "Short description ...", |
| 697 | /* Requirement IDs */ |
| 698 | "TEE-??", |
| 699 | /* How to implement */ |
| 700 | "Description of how to implement ..." |
| 701 | ); |
| 702 | |
| 703 | ADBG_CASE_DEFINE( |
| 704 | XTEST_TEE_20021, xtest_tee_test_20021, |
| 705 | /* Title */ |
| 706 | "Sanity Test Corrupt Meta File : first byte", |
| 707 | /* Short description */ |
| 708 | "Short description ...", |
| 709 | /* Requirement IDs */ |
| 710 | "TEE-??", |
| 711 | /* How to implement */ |
| 712 | "Description of how to implement ..." |
| 713 | ); |
| 714 | |
| 715 | ADBG_CASE_DEFINE( |
| 716 | XTEST_TEE_20022, xtest_tee_test_20022, |
| 717 | /* Title */ |
| 718 | "Sanity Test Corrupt Meta File : last byte", |
| 719 | /* Short description */ |
| 720 | "Short description ...", |
| 721 | /* Requirement IDs */ |
| 722 | "TEE-??", |
| 723 | /* How to implement */ |
| 724 | "Description of how to implement ..." |
| 725 | ); |
| 726 | |
| 727 | ADBG_CASE_DEFINE( |
| 728 | XTEST_TEE_20023, xtest_tee_test_20023, |
| 729 | /* Title */ |
| 730 | "Sanity Test Corrupt Meta File : random byte", |
| 731 | /* Short description */ |
| 732 | "Short description ...", |
| 733 | /* Requirement IDs */ |
| 734 | "TEE-??", |
| 735 | /* How to implement */ |
| 736 | "Description of how to implement ..." |
| 737 | ); |
| 738 | |
| 739 | ADBG_CASE_DEFINE( |
| 740 | XTEST_TEE_20501, xtest_tee_test_20501, |
| 741 | /* Title */ |
| 742 | "Sanity Test Corrupt Block IV", |
| 743 | /* Short description */ |
| 744 | "Short description ...", |
| 745 | /* Requirement IDs */ |
| 746 | "TEE-??", |
| 747 | /* How to implement */ |
| 748 | "Description of how to implement ..." |
| 749 | ); |
| 750 | |
| 751 | ADBG_CASE_DEFINE( |
| 752 | XTEST_TEE_20502, xtest_tee_test_20502, |
| 753 | /* Title */ |
| 754 | "Sanity Test Corrupt Block Tag", |
| 755 | /* Short description */ |
| 756 | "Short description ...", |
| 757 | /* Requirement IDs */ |
| 758 | "TEE-??", |
| 759 | /* How to implement */ |
| 760 | "Description of how to implement ..." |
| 761 | ); |
| 762 | |
| 763 | ADBG_CASE_DEFINE( |
| 764 | XTEST_TEE_20503, xtest_tee_test_20503, |
| 765 | /* Title */ |
| 766 | "Sanity Test Corrupt Block Data", |
| 767 | /* Short description */ |
| 768 | "Short description ...", |
| 769 | /* Requirement IDs */ |
| 770 | "TEE-??", |
| 771 | /* How to implement */ |
| 772 | "Description of how to implement ..." |
| 773 | ); |
| 774 | |
| 775 | ADBG_CASE_DEFINE( |
| 776 | XTEST_TEE_20521, xtest_tee_test_20521, |
| 777 | /* Title */ |
| 778 | "Sanity Test Corrupt Block File : first byte", |
| 779 | /* Short description */ |
| 780 | "Short description ...", |
| 781 | /* Requirement IDs */ |
| 782 | "TEE-??", |
| 783 | /* How to implement */ |
| 784 | "Description of how to implement ..." |
| 785 | ); |
| 786 | |
| 787 | ADBG_CASE_DEFINE( |
| 788 | XTEST_TEE_20522, xtest_tee_test_20522, |
| 789 | /* Title */ |
| 790 | "Sanity Test Corrupt Block File : last byte", |
| 791 | /* Short description */ |
| 792 | "Short description ...", |
| 793 | /* Requirement IDs */ |
| 794 | "TEE-??", |
| 795 | /* How to implement */ |
| 796 | "Description of how to implement ..." |
| 797 | ); |
| 798 | |
| 799 | ADBG_CASE_DEFINE( |
| 800 | XTEST_TEE_20523, xtest_tee_test_20523, |
| 801 | /* Title */ |
| 802 | "Sanity Test Corrupt Block File : random byte", |
| 803 | /* Short description */ |
| 804 | "Short description ...", |
| 805 | /* Requirement IDs */ |
| 806 | "TEE-??", |
| 807 | /* How to implement */ |
| 808 | "Description of how to implement ..." |
| 809 | ); |
| 810 | |
| 811 | #endif /* CFG_ENC_FS */ |