Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +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 | |
Etienne Carriere | a465355 | 2017-01-11 10:04:24 +0100 | [diff] [blame] | 14 | #include <limits.h> |
| 15 | #include <pthread.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 17 | #include <stdlib.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 18 | #include <string.h> |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
Etienne Carriere | a465355 | 2017-01-11 10:04:24 +0100 | [diff] [blame] | 21 | #include <unistd.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 22 | |
| 23 | #include "xtest_test.h" |
| 24 | #include "xtest_helpers.h" |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 25 | #include <signed_hdr.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 26 | |
Etienne Carriere | 726d8bc | 2017-03-21 15:45:59 +0100 | [diff] [blame] | 27 | #include <pta_invoke_tests.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 28 | #include <ta_crypt.h> |
| 29 | #include <ta_os_test.h> |
| 30 | #include <ta_create_fail_test.h> |
| 31 | #include <ta_rpc_test.h> |
| 32 | #include <ta_sims_test.h> |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 33 | #include <ta_concurrent.h> |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 34 | #include <sdp_basic.h> |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 35 | #ifdef CFG_SECSTOR_TA_MGMT_PTA |
| 36 | #include <pta_secstor_ta_mgmt.h> |
| 37 | #endif |
| 38 | |
| 39 | #ifndef MIN |
| 40 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 41 | #endif |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 42 | |
| 43 | static void xtest_tee_test_1001(ADBG_Case_t *Case_p); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 44 | static void xtest_tee_test_1002(ADBG_Case_t *Case_p); |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 45 | static void xtest_tee_test_1003(ADBG_Case_t *Case_p); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 46 | static void xtest_tee_test_1004(ADBG_Case_t *Case_p); |
| 47 | static void xtest_tee_test_1005(ADBG_Case_t *Case_p); |
| 48 | static void xtest_tee_test_1006(ADBG_Case_t *Case_p); |
| 49 | static void xtest_tee_test_1007(ADBG_Case_t *Case_p); |
| 50 | static void xtest_tee_test_1008(ADBG_Case_t *Case_p); |
| 51 | static void xtest_tee_test_1009(ADBG_Case_t *Case_p); |
| 52 | static void xtest_tee_test_1010(ADBG_Case_t *Case_p); |
| 53 | static void xtest_tee_test_1011(ADBG_Case_t *Case_p); |
| 54 | static void xtest_tee_test_1012(ADBG_Case_t *Case_p); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 55 | static void xtest_tee_test_1013(ADBG_Case_t *Case_p); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 56 | #ifdef CFG_SECURE_DATA_PATH |
| 57 | static void xtest_tee_test_1014(ADBG_Case_t *Case_p); |
| 58 | #endif |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 59 | static void xtest_tee_test_1015(ADBG_Case_t *Case_p); |
Jerome Forissier | e916b10 | 2017-06-07 17:55:52 +0200 | [diff] [blame] | 60 | static void xtest_tee_test_1016(ADBG_Case_t *Case_p); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 61 | |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 62 | ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests"); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 63 | ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters"); |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 64 | ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003, |
| 65 | "Core internal read/write mutex"); |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 66 | ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA"); |
| 67 | ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions"); |
| 68 | ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006, |
| 69 | "Test Basic OS features"); |
| 70 | ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic"); |
| 71 | ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008, |
Jens Wiklander | 25a57fe | 2016-12-26 21:46:24 +0100 | [diff] [blame] | 72 | "TEE internal client API"); |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 73 | ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait"); |
| 74 | ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010, |
| 75 | "Invalid memory access"); |
| 76 | ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011, |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 77 | "Test TA-to-TA features with User Crypt TA"); |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 78 | ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012, |
Jens Wiklander | 25a57fe | 2016-12-26 21:46:24 +0100 | [diff] [blame] | 79 | "Test Single Instance Multi Session features with SIMS TA"); |
Jens Wiklander | 74abfe3 | 2017-01-03 14:17:47 +0100 | [diff] [blame] | 80 | ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013, |
Jens Wiklander | 25a57fe | 2016-12-26 21:46:24 +0100 | [diff] [blame] | 81 | "Test concurency with concurrent TA"); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 82 | #ifdef CFG_SECURE_DATA_PATH |
| 83 | ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014, |
| 84 | "Test secure data path against SDP TAs and pTAs"); |
| 85 | #endif |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 86 | ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015, |
| 87 | "FS hash-tree corner cases"); |
Jerome Forissier | e916b10 | 2017-06-07 17:55:52 +0200 | [diff] [blame] | 88 | ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016, |
| 89 | "Test TA to TA transfers (in/out/inout memrefs on the stack)"); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 90 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 91 | struct xtest_crypto_session { |
| 92 | ADBG_Case_t *c; |
| 93 | TEEC_Session *session; |
| 94 | uint32_t cmd_id_sha256; |
| 95 | uint32_t cmd_id_aes256ecb_encrypt; |
| 96 | uint32_t cmd_id_aes256ecb_decrypt; |
| 97 | }; |
| 98 | |
| 99 | static void xtest_crypto_test(struct xtest_crypto_session *cs) |
| 100 | { |
| 101 | uint32_t ret_orig; |
| 102 | uint8_t crypt_out[16]; |
| 103 | uint8_t crypt_in[16] = { 22, 17 }; |
| 104 | |
| 105 | crypt_in[15] = 60; |
| 106 | |
| 107 | Do_ADBG_BeginSubCase(cs->c, "AES encrypt"); |
| 108 | { |
| 109 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 110 | |
| 111 | op.params[0].tmpref.buffer = crypt_in; |
| 112 | op.params[0].tmpref.size = sizeof(crypt_in); |
| 113 | op.params[1].tmpref.buffer = crypt_out; |
| 114 | op.params[1].tmpref.size = sizeof(crypt_out); |
| 115 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 116 | TEEC_MEMREF_TEMP_OUTPUT, |
| 117 | TEEC_NONE, TEEC_NONE); |
| 118 | |
| 119 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 120 | TEEC_InvokeCommand(cs->session, |
| 121 | cs-> |
| 122 | cmd_id_aes256ecb_encrypt, |
| 123 | &op, |
| 124 | &ret_orig)); |
| 125 | } |
| 126 | Do_ADBG_EndSubCase(cs->c, "AES encrypt"); |
| 127 | |
| 128 | Do_ADBG_BeginSubCase(cs->c, "AES decrypt"); |
| 129 | { |
| 130 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 131 | uint8_t out[16]; |
| 132 | |
| 133 | op.params[0].tmpref.buffer = crypt_out; |
| 134 | op.params[0].tmpref.size = sizeof(crypt_out); |
| 135 | op.params[1].tmpref.buffer = out; |
| 136 | op.params[1].tmpref.size = sizeof(out); |
| 137 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 138 | TEEC_MEMREF_TEMP_OUTPUT, |
| 139 | TEEC_NONE, TEEC_NONE); |
| 140 | |
| 141 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 142 | TEEC_InvokeCommand(cs->session, |
| 143 | cs-> |
| 144 | cmd_id_aes256ecb_decrypt, |
| 145 | &op, |
| 146 | &ret_orig)); |
| 147 | |
| 148 | if (!ADBG_EXPECT(cs->c, 0, |
| 149 | memcmp(crypt_in, out, sizeof(crypt_in)))) { |
| 150 | Do_ADBG_Log("crypt_in:"); |
| 151 | Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16); |
| 152 | Do_ADBG_Log("out:"); |
| 153 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 154 | } |
| 155 | } |
| 156 | Do_ADBG_EndSubCase(cs->c, "AES decrypt"); |
| 157 | |
| 158 | Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input"); |
| 159 | { |
| 160 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 161 | static const uint8_t sha256_in[] = { 'a', 'b', 'c' }; |
| 162 | static const uint8_t sha256_out[] = { |
| 163 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
| 164 | 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
| 165 | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 166 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
| 167 | }; |
| 168 | uint8_t out[32] = { 0 }; |
| 169 | |
| 170 | op.params[0].tmpref.buffer = (void *)sha256_in; |
| 171 | op.params[0].tmpref.size = sizeof(sha256_in); |
| 172 | op.params[1].tmpref.buffer = out; |
| 173 | op.params[1].tmpref.size = sizeof(out); |
| 174 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 175 | TEEC_MEMREF_TEMP_OUTPUT, |
| 176 | TEEC_NONE, TEEC_NONE); |
| 177 | |
| 178 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 179 | TEEC_InvokeCommand(cs->session, |
| 180 | cs-> |
| 181 | cmd_id_sha256, |
| 182 | &op, |
| 183 | &ret_orig)); |
| 184 | |
| 185 | if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out, |
| 186 | sizeof(sha256_out)))) { |
| 187 | Do_ADBG_Log("sha256_out:"); |
| 188 | Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16); |
| 189 | Do_ADBG_Log("out:"); |
| 190 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 191 | } |
| 192 | } |
| 193 | Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input"); |
| 194 | |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 195 | Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 196 | { |
| 197 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 198 | static const uint8_t in[] = { |
| 199 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 200 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 201 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 202 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 203 | }; |
| 204 | static const uint8_t exp_out[] = { |
| 205 | 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96, |
| 206 | 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92, |
| 207 | 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6, |
| 208 | 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E |
| 209 | }; |
| 210 | uint8_t out[sizeof(exp_out)]; |
| 211 | |
| 212 | op.params[0].tmpref.buffer = (void *)in; |
| 213 | op.params[0].tmpref.size = sizeof(in); |
| 214 | op.params[1].tmpref.buffer = out; |
| 215 | op.params[1].tmpref.size = sizeof(out); |
| 216 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 217 | TEEC_MEMREF_TEMP_OUTPUT, |
| 218 | TEEC_NONE, TEEC_NONE); |
| 219 | |
| 220 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 221 | TEEC_InvokeCommand(cs->session, |
| 222 | cs-> |
| 223 | cmd_id_aes256ecb_encrypt, |
| 224 | &op, |
| 225 | &ret_orig)); |
| 226 | |
| 227 | if (!ADBG_EXPECT(cs->c, 0, |
| 228 | memcmp(exp_out, out, sizeof(exp_out)))) { |
| 229 | Do_ADBG_Log("exp_out:"); |
| 230 | Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16); |
| 231 | Do_ADBG_Log("out:"); |
| 232 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 233 | } |
| 234 | } |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 235 | Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 236 | |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 237 | Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 238 | { |
| 239 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 240 | static const uint8_t in[] = { |
| 241 | 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96, |
| 242 | 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92, |
| 243 | 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6, |
| 244 | 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E |
| 245 | }; |
| 246 | static const uint8_t exp_out[] = { |
| 247 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 248 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 249 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 250 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f |
| 251 | }; |
| 252 | uint8_t out[sizeof(exp_out)]; |
| 253 | |
| 254 | op.params[0].tmpref.buffer = (void *)in; |
| 255 | op.params[0].tmpref.size = sizeof(in); |
| 256 | op.params[1].tmpref.buffer = out; |
| 257 | op.params[1].tmpref.size = sizeof(out); |
| 258 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 259 | TEEC_MEMREF_TEMP_OUTPUT, |
| 260 | TEEC_NONE, TEEC_NONE); |
| 261 | |
| 262 | (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c, |
| 263 | TEEC_InvokeCommand(cs->session, |
| 264 | cs-> |
| 265 | cmd_id_aes256ecb_decrypt, |
| 266 | &op, |
| 267 | &ret_orig)); |
| 268 | |
| 269 | if (!ADBG_EXPECT(cs->c, 0, |
| 270 | memcmp(exp_out, out, sizeof(exp_out)))) { |
| 271 | Do_ADBG_Log("exp_out:"); |
| 272 | Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16); |
| 273 | Do_ADBG_Log("out:"); |
| 274 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 275 | } |
| 276 | } |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 277 | Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void xtest_tee_test_1001(ADBG_Case_t *c) |
| 281 | { |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 282 | TEEC_Result res; |
| 283 | TEEC_Session session = { 0 }; |
| 284 | uint32_t ret_orig; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 285 | |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 286 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
Etienne Carriere | 726d8bc | 2017-03-21 15:45:59 +0100 | [diff] [blame] | 287 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 288 | &ret_orig); |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 289 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 290 | Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found"); |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 291 | return; |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 292 | } |
| 293 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 294 | |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 295 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand( |
Etienne Carriere | 726d8bc | 2017-03-21 15:45:59 +0100 | [diff] [blame] | 296 | &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig)); |
Jens Wiklander | cf16e84 | 2016-02-10 09:07:09 +0100 | [diff] [blame] | 297 | TEEC_CloseSession(&session); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 300 | static void xtest_tee_test_1002(ADBG_Case_t *c) |
| 301 | { |
| 302 | TEEC_Result res; |
| 303 | TEEC_Session session = { 0 }; |
| 304 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 305 | uint32_t ret_orig; |
| 306 | uint8_t buf[16 * 1024]; |
| 307 | uint8_t exp_sum = 0; |
| 308 | size_t n; |
| 309 | |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 310 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 311 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 312 | &ret_orig); |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 313 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 314 | Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found"); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 315 | return; |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 316 | } |
| 317 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 318 | |
| 319 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE, |
| 320 | TEEC_NONE, TEEC_NONE); |
| 321 | op.params[0].tmpref.size = sizeof(buf); |
| 322 | op.params[0].tmpref.buffer = buf; |
| 323 | |
| 324 | for (n = 0; n < sizeof(buf); n++) |
| 325 | buf[n] = n + 1; |
| 326 | for (n = 0; n < sizeof(buf); n++) |
| 327 | exp_sum += buf[n]; |
| 328 | |
| 329 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand( |
| 330 | &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig))) |
| 331 | goto out; |
| 332 | |
| 333 | ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]); |
| 334 | out: |
| 335 | TEEC_CloseSession(&session); |
| 336 | } |
| 337 | |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 338 | struct test_1003_arg { |
| 339 | uint32_t test_type; |
| 340 | size_t repeat; |
| 341 | size_t max_before_lockers; |
| 342 | size_t max_during_lockers; |
| 343 | size_t before_lockers; |
| 344 | size_t during_lockers; |
| 345 | TEEC_Result res; |
| 346 | uint32_t error_orig; |
| 347 | }; |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 348 | |
Jens Wiklander | 0c86bc3 | 2017-11-13 19:52:03 +0100 | [diff] [blame] | 349 | static void *test_1003_thread(void *arg) |
| 350 | { |
| 351 | struct test_1003_arg *a = arg; |
| 352 | TEEC_Session session = { 0 }; |
| 353 | size_t rounds = 64 * 1024; |
| 354 | size_t n; |
| 355 | |
| 356 | a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, |
| 357 | NULL, &a->error_orig); |
| 358 | if (a->res != TEEC_SUCCESS) |
| 359 | return NULL; |
| 360 | |
| 361 | for (n = 0; n < a->repeat; n++) { |
| 362 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 363 | |
| 364 | op.params[0].value.a = a->test_type; |
| 365 | op.params[0].value.b = rounds; |
| 366 | |
| 367 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 368 | TEEC_VALUE_OUTPUT, |
| 369 | TEEC_NONE, TEEC_NONE); |
| 370 | a->res = TEEC_InvokeCommand(&session, |
| 371 | PTA_INVOKE_TESTS_CMD_MUTEX, |
| 372 | &op, &a->error_orig); |
| 373 | if (a->test_type == PTA_MUTEX_TEST_WRITER && |
| 374 | op.params[1].value.b != 1) { |
| 375 | Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b); |
| 376 | a->res = TEEC_ERROR_BAD_STATE; |
| 377 | a->error_orig = 42; |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | if (a->test_type == PTA_MUTEX_TEST_READER) { |
| 382 | if (op.params[1].value.a > a->max_before_lockers) |
| 383 | a->max_before_lockers = op.params[1].value.a; |
| 384 | |
| 385 | if (op.params[1].value.b > a->max_during_lockers) |
| 386 | a->max_during_lockers = op.params[1].value.b; |
| 387 | |
| 388 | a->before_lockers += op.params[1].value.a; |
| 389 | a->during_lockers += op.params[1].value.b; |
| 390 | } |
| 391 | } |
| 392 | TEEC_CloseSession(&session); |
| 393 | |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | static void xtest_tee_test_1003(ADBG_Case_t *c) |
| 398 | { |
| 399 | size_t num_threads = 3 * 2; |
| 400 | TEEC_Result res; |
| 401 | TEEC_Session session = { 0 }; |
| 402 | uint32_t ret_orig; |
| 403 | size_t repeat = 20; |
| 404 | pthread_t thr[num_threads]; |
| 405 | struct test_1003_arg arg[num_threads]; |
| 406 | size_t max_read_concurrency = 0; |
| 407 | size_t max_read_waiters = 0; |
| 408 | size_t num_concurrent_read_lockers = 0; |
| 409 | size_t num_concurrent_read_waiters = 0; |
| 410 | size_t n; |
| 411 | size_t nt = num_threads; |
| 412 | double mean_read_concurrency; |
| 413 | double mean_read_waiters; |
| 414 | size_t num_writers = 0; |
| 415 | size_t num_readers = 0; |
| 416 | |
| 417 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
| 418 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 419 | &ret_orig); |
| 420 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 421 | Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found"); |
| 422 | return; |
| 423 | } |
| 424 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
| 425 | TEEC_CloseSession(&session); |
| 426 | |
| 427 | memset(arg, 0, sizeof(arg)); |
| 428 | |
| 429 | for (n = 0; n < nt; n++) { |
| 430 | if (n % 3) { |
| 431 | arg[n].test_type = PTA_MUTEX_TEST_READER; |
| 432 | num_readers++; |
| 433 | } else { |
| 434 | arg[n].test_type = PTA_MUTEX_TEST_WRITER; |
| 435 | num_writers++; |
| 436 | } |
| 437 | arg[n].repeat = repeat; |
| 438 | if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL, |
| 439 | test_1003_thread, arg + n))) |
| 440 | nt = n; /* break loop and start cleanup */ |
| 441 | } |
| 442 | |
| 443 | for (n = 0; n < nt; n++) { |
| 444 | ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)); |
| 445 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res)) |
| 446 | Do_ADBG_Log("error origin %" PRIu32, |
| 447 | arg[n].error_orig); |
| 448 | if (arg[n].test_type == PTA_MUTEX_TEST_READER) { |
| 449 | if (arg[n].max_during_lockers > max_read_concurrency) |
| 450 | max_read_concurrency = |
| 451 | arg[n].max_during_lockers; |
| 452 | |
| 453 | if (arg[n].max_before_lockers > max_read_waiters) |
| 454 | max_read_waiters = arg[n].max_before_lockers; |
| 455 | |
| 456 | num_concurrent_read_lockers += arg[n].during_lockers; |
| 457 | num_concurrent_read_waiters += arg[n].before_lockers; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | mean_read_concurrency = (double)num_concurrent_read_lockers / |
| 462 | (double)(repeat * num_readers); |
| 463 | mean_read_waiters = (double)num_concurrent_read_waiters / |
| 464 | (double)(repeat * num_readers); |
| 465 | |
| 466 | Do_ADBG_Log(" Number of parallel threads: %zu (%zu writers and %zu readers)", |
| 467 | num_threads, num_writers, num_readers); |
| 468 | Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency); |
| 469 | Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters); |
| 470 | Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency); |
| 471 | Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters); |
| 472 | } |
Jens Wiklander | 1d70a11 | 2017-10-16 15:16:39 +0200 | [diff] [blame] | 473 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 474 | static void xtest_tee_test_1004(ADBG_Case_t *c) |
| 475 | { |
| 476 | TEEC_Session session = { 0 }; |
| 477 | uint32_t ret_orig; |
| 478 | struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256, |
| 479 | TA_CRYPT_CMD_AES256ECB_ENC, |
| 480 | TA_CRYPT_CMD_AES256ECB_DEC }; |
| 481 | |
| 482 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 483 | &session, &crypt_user_ta_uuid, |
| 484 | NULL, &ret_orig))) |
| 485 | return; |
| 486 | |
| 487 | /* Run the "complete crypto test suite" */ |
| 488 | xtest_crypto_test(&cs); |
| 489 | |
| 490 | TEEC_CloseSession(&session); |
| 491 | } |
| 492 | |
| 493 | #ifndef TEEC_ERROR_TARGET_DEAD |
| 494 | /* To be removed when we have TEEC_ERROR_TARGET_DEAD from tee_client_api.h */ |
| 495 | #define TEEC_ERROR_TARGET_DEAD 0xFFFF3024 |
| 496 | #endif |
| 497 | |
| 498 | static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, uint32_t n) |
| 499 | { |
| 500 | TEEC_Session session = { 0 }; |
| 501 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 502 | uint32_t ret_orig; |
| 503 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 504 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 505 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 506 | &ret_orig))) |
| 507 | return; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 508 | |
| 509 | op.params[0].value.a = n; |
| 510 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, |
| 511 | TEEC_NONE); |
| 512 | |
| 513 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 514 | TEEC_ERROR_TARGET_DEAD, |
| 515 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op, |
| 516 | &ret_orig)); |
| 517 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 518 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 519 | TEEC_ERROR_TARGET_DEAD, |
| 520 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 521 | &ret_orig)); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 522 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 523 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 524 | |
| 525 | TEEC_CloseSession(&session); |
| 526 | } |
| 527 | |
| 528 | static void xtest_tee_test_1005(ADBG_Case_t *c) |
| 529 | { |
| 530 | uint32_t ret_orig; |
| 531 | #define MAX_SESSIONS 3 |
| 532 | TEEC_Session sessions[MAX_SESSIONS]; |
| 533 | int i; |
| 534 | |
| 535 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 536 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Jens Wiklander | eb6bce7 | 2016-09-23 11:37:33 +0200 | [diff] [blame] | 537 | xtest_teec_open_session(&sessions[i], |
| 538 | &concurrent_ta_uuid, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 539 | NULL, &ret_orig))) |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | for (; --i >= 0; ) |
| 544 | TEEC_CloseSession(&sessions[i]); |
| 545 | } |
| 546 | |
| 547 | static void xtest_tee_test_1006(ADBG_Case_t *c) |
| 548 | { |
| 549 | TEEC_Session session = { 0 }; |
| 550 | uint32_t ret_orig; |
| 551 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 552 | uint8_t buf[32]; |
| 553 | |
| 554 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 555 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 556 | &ret_orig))) |
| 557 | return; |
| 558 | |
| 559 | op.params[0].tmpref.buffer = buf; |
| 560 | op.params[0].tmpref.size = sizeof(buf); |
| 561 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, |
| 562 | TEEC_NONE, TEEC_NONE); |
| 563 | |
| 564 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 565 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op, |
| 566 | &ret_orig)); |
| 567 | |
| 568 | TEEC_CloseSession(&session); |
| 569 | } |
| 570 | |
| 571 | static void xtest_tee_test_1007(ADBG_Case_t *c) |
| 572 | { |
| 573 | TEEC_Session session = { 0 }; |
| 574 | uint32_t ret_orig; |
| 575 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 576 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 577 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 578 | &ret_orig))) |
| 579 | return; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 580 | |
| 581 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 582 | TEEC_ERROR_TARGET_DEAD, |
| 583 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL, |
| 584 | &ret_orig)); |
| 585 | |
| 586 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 587 | |
| 588 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 589 | TEEC_ERROR_TARGET_DEAD, |
| 590 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL, |
| 591 | &ret_orig)); |
| 592 | |
| 593 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig); |
| 594 | |
| 595 | TEEC_CloseSession(&session); |
| 596 | } |
| 597 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 598 | #ifdef CFG_SECSTOR_TA_MGMT_PTA |
Jerome Forissier | f02a221 | 2015-10-29 14:33:35 +0100 | [diff] [blame] | 599 | #ifndef TA_DIR |
Victor Chong | 3d8798f | 2017-03-01 18:31:48 +0000 | [diff] [blame] | 600 | # ifdef __ANDROID__ |
| 601 | #define TA_DIR "/system/lib/optee_armtz" |
| 602 | # else |
Jerome Forissier | f02a221 | 2015-10-29 14:33:35 +0100 | [diff] [blame] | 603 | #define TA_DIR "/lib/optee_armtz" |
Victor Chong | 3d8798f | 2017-03-01 18:31:48 +0000 | [diff] [blame] | 604 | # endif |
Jerome Forissier | f02a221 | 2015-10-29 14:33:35 +0100 | [diff] [blame] | 605 | #endif |
| 606 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 607 | static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode) |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 608 | { |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 609 | char buf[PATH_MAX]; |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 610 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 611 | snprintf(buf, sizeof(buf), |
Jens Wiklander | 6203b87 | 2016-12-08 19:18:29 +0100 | [diff] [blame] | 612 | "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta", |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 613 | TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion, |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 614 | uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1], |
| 615 | uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3], |
| 616 | uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5], |
David Brown | b2865ab | 2016-08-02 11:44:41 -0600 | [diff] [blame] | 617 | uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]); |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 618 | |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 619 | return fopen(buf, mode); |
| 620 | } |
| 621 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 622 | static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask) |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 623 | { |
| 624 | TEEC_Session session = { 0 }; |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 625 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 626 | TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 627 | TEEC_Result res; |
| 628 | uint32_t ret_orig; |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 629 | FILE *f = NULL; |
| 630 | bool r = false; |
| 631 | uint8_t *buf = NULL; |
| 632 | size_t sz; |
| 633 | size_t fread_res; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 634 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 635 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 636 | xtest_teec_open_session(&session, &uuid, NULL, &ret_orig))) |
| 637 | goto out; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 638 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 639 | f = open_ta_file(&create_fail_test_ta_uuid, "rb"); |
| 640 | if (!ADBG_EXPECT_NOT_NULL(c, f)) |
| 641 | goto out; |
| 642 | if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END))) |
| 643 | goto out; |
| 644 | sz = ftell(f); |
| 645 | rewind(f); |
| 646 | |
| 647 | buf = malloc(sz); |
| 648 | if (!ADBG_EXPECT_NOT_NULL(c, buf)) |
| 649 | goto out; |
| 650 | |
| 651 | fread_res = fread(buf, 1, sz, f); |
| 652 | if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz)) |
| 653 | goto out; |
| 654 | |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 655 | fclose(f); |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 656 | f = NULL; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 657 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 658 | buf[MIN(offs, sz)] ^= mask; |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 659 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 660 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE, |
| 661 | TEEC_NONE, TEEC_NONE); |
| 662 | op.params[0].tmpref.buffer = buf; |
| 663 | op.params[0].tmpref.size = sz; |
| 664 | |
| 665 | res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op, |
| 666 | &ret_orig); |
| 667 | r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res); |
| 668 | out: |
| 669 | free(buf); |
| 670 | if (f) |
| 671 | fclose(f); |
| 672 | TEEC_CloseSession(&session); |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 673 | return r; |
| 674 | } |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 675 | #endif /*CFG_SECSTOR_TA_MGMT_PTA*/ |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 676 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 677 | static void xtest_tee_test_1008(ADBG_Case_t *c) |
| 678 | { |
| 679 | TEEC_Session session = { 0 }; |
| 680 | TEEC_Session session_crypt = { 0 }; |
| 681 | uint32_t ret_orig; |
| 682 | |
| 683 | Do_ADBG_BeginSubCase(c, "Invoke command"); |
| 684 | { |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 685 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 686 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 687 | NULL, &ret_orig))) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 688 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 689 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 690 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT, |
| 691 | NULL, &ret_orig)); |
| 692 | TEEC_CloseSession(&session); |
| 693 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 694 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 695 | } |
| 696 | Do_ADBG_EndSubCase(c, "Invoke command"); |
| 697 | |
| 698 | Do_ADBG_BeginSubCase(c, "Invoke command with timeout"); |
| 699 | { |
| 700 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 701 | |
| 702 | op.params[0].value.a = 2000; |
| 703 | op.paramTypes = TEEC_PARAM_TYPES( |
| 704 | TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE); |
| 705 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 706 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 707 | xtest_teec_open_session(&session, |
| 708 | &os_test_ta_uuid, |
| 709 | NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 710 | &ret_orig))) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 711 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 712 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 713 | TEEC_InvokeCommand(&session, |
| 714 | TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT, |
| 715 | &op, &ret_orig)); |
| 716 | TEEC_CloseSession(&session); |
| 717 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 718 | } |
| 719 | Do_ADBG_EndSubCase(c, "Invoke command with timeout"); |
| 720 | |
| 721 | Do_ADBG_BeginSubCase(c, "Create session fail"); |
| 722 | { |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 723 | size_t n; |
| 724 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 725 | (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC, |
| 726 | xtest_teec_open_session(&session_crypt, |
| 727 | &create_fail_test_ta_uuid, NULL, |
| 728 | &ret_orig)); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 729 | /* |
| 730 | * Run this several times to see that there's no memory leakage. |
| 731 | */ |
| 732 | for (n = 0; n < 100; n++) { |
| 733 | Do_ADBG_Log("n = %zu", n); |
| 734 | (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC, |
| 735 | xtest_teec_open_session(&session_crypt, |
| 736 | &create_fail_test_ta_uuid, |
| 737 | NULL, &ret_orig)); |
| 738 | } |
| 739 | } |
| 740 | Do_ADBG_EndSubCase(c, "Create session fail"); |
Jens Wiklander | b794089 | 2015-10-23 16:02:40 +0200 | [diff] [blame] | 741 | |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 742 | #ifdef CFG_SECSTOR_TA_MGMT_PTA |
Jens Wiklander | 4441fe2 | 2015-10-23 16:53:02 +0200 | [diff] [blame] | 743 | Do_ADBG_BeginSubCase(c, "Load corrupt TA"); |
| 744 | ADBG_EXPECT_TRUE(c, |
| 745 | load_corrupt_ta(c, offsetof(struct shdr, magic), 1)); |
| 746 | ADBG_EXPECT_TRUE(c, |
| 747 | load_corrupt_ta(c, offsetof(struct shdr, img_type), 1)); |
| 748 | ADBG_EXPECT_TRUE(c, |
| 749 | load_corrupt_ta(c, offsetof(struct shdr, img_size), 1)); |
| 750 | ADBG_EXPECT_TRUE(c, |
| 751 | load_corrupt_ta(c, offsetof(struct shdr, algo), 1)); |
| 752 | ADBG_EXPECT_TRUE(c, |
| 753 | load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1)); |
| 754 | ADBG_EXPECT_TRUE(c, |
| 755 | load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1)); |
| 756 | ADBG_EXPECT_TRUE(c, |
| 757 | load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */ |
| 758 | ADBG_EXPECT_TRUE(c, |
| 759 | load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */ |
| 760 | ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */ |
| 761 | ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 30000, 1)); /* payload */ |
| 762 | Do_ADBG_EndSubCase(c, "Load corrupt TA"); |
Jens Wiklander | ec545fb | 2017-11-24 16:58:07 +0100 | [diff] [blame] | 763 | #endif /*CFG_SECSTOR_TA_MGMT_PTA*/ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 766 | static void *cancellation_thread(void *arg) |
| 767 | { |
| 768 | /* |
| 769 | * Sleep 0.5 seconds before cancellation to make sure that the other |
| 770 | * thread is in RPC_WAIT. |
| 771 | */ |
| 772 | (void)usleep(500000); |
| 773 | TEEC_RequestCancellation(arg); |
| 774 | return NULL; |
| 775 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 776 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 777 | static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase, |
| 778 | uint32_t timeout, bool cancel) |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 779 | { |
| 780 | TEEC_Session session = { 0 }; |
| 781 | uint32_t ret_orig; |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 782 | pthread_t thr; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 783 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 784 | Do_ADBG_BeginSubCase(c, "%s", subcase); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 785 | { |
| 786 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 787 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 788 | if (ADBG_EXPECT_TEEC_SUCCESS(c, |
| 789 | xtest_teec_open_session(&session, &os_test_ta_uuid, |
| 790 | NULL, &ret_orig))) { |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 791 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 792 | (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, |
| 793 | TEEC_ORIGIN_TRUSTED_APP, |
| 794 | ret_orig); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 795 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 796 | op.params[0].value.a = timeout; |
| 797 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 798 | TEEC_NONE, |
| 799 | TEEC_NONE, TEEC_NONE); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 800 | if (cancel) { |
| 801 | (void)ADBG_EXPECT(c, 0, |
| 802 | pthread_create(&thr, NULL, |
| 803 | cancellation_thread, &op)); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 804 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 805 | (void)ADBG_EXPECT_TEEC_RESULT(c, |
| 806 | TEEC_ERROR_CANCEL, |
| 807 | TEEC_InvokeCommand(&session, |
| 808 | TA_OS_TEST_CMD_WAIT, |
| 809 | &op, |
| 810 | &ret_orig)); |
| 811 | } else |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 812 | |
| 813 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 814 | TEEC_InvokeCommand(&session, |
| 815 | TA_OS_TEST_CMD_WAIT, |
| 816 | &op, |
| 817 | &ret_orig)); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 818 | if (cancel) |
| 819 | (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL)); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 820 | |
| 821 | TEEC_CloseSession(&session); |
| 822 | } |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 823 | } |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 824 | Do_ADBG_EndSubCase(c, "%s", subcase); |
| 825 | } |
| 826 | |
| 827 | static void xtest_tee_test_1009(ADBG_Case_t *c) |
| 828 | { |
| 829 | xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false); |
| 830 | xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 831 | xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true); |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 832 | xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | static void xtest_tee_test_1010(ADBG_Case_t *c) |
| 836 | { |
| 837 | unsigned n; |
| 838 | |
| 839 | for (n = 1; n <= 5; n++) { |
| 840 | Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n); |
| 841 | xtest_tee_test_invalid_mem_access(c, n); |
| 842 | Do_ADBG_EndSubCase(c, "Invalid memory access %u", n); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | static void xtest_tee_test_1011(ADBG_Case_t *c) |
| 847 | { |
| 848 | TEEC_Session session = { 0 }; |
| 849 | uint32_t ret_orig; |
| 850 | struct xtest_crypto_session cs = { |
| 851 | c, &session, TA_RPC_CMD_CRYPT_SHA256, |
| 852 | TA_RPC_CMD_CRYPT_AES256ECB_ENC, |
| 853 | TA_RPC_CMD_CRYPT_AES256ECB_DEC |
| 854 | }; |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 855 | struct xtest_crypto_session cs_privmem = { |
| 856 | c, &session, |
| 857 | TA_RPC_CMD_CRYPT_PRIVMEM_SHA256, |
| 858 | TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC, |
| 859 | TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC |
| 860 | }; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 861 | TEEC_UUID uuid = rpc_test_ta_uuid; |
| 862 | |
| 863 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 864 | xtest_teec_open_session(&session, &uuid, NULL, &ret_orig))) |
| 865 | return; |
| 866 | |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 867 | Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory"); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 868 | /* |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 869 | * Run the "complete crypto test suite" using TA-to-TA |
| 870 | * communication |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 871 | */ |
| 872 | xtest_crypto_test(&cs); |
Jens Wiklander | f7b9c63 | 2017-01-03 17:32:26 +0100 | [diff] [blame] | 873 | Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory"); |
| 874 | |
| 875 | Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory"); |
| 876 | /* |
| 877 | * Run the "complete crypto test suite" using TA-to-TA |
| 878 | * communication via TA private memory. |
| 879 | */ |
| 880 | xtest_crypto_test(&cs_privmem); |
| 881 | Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory"); |
| 882 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 883 | TEEC_CloseSession(&session); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Note that this test is failing when |
| 888 | * - running twice in a raw |
| 889 | * - and the user TA is statically linked |
| 890 | * This is because the counter is not reseted when opening the first session |
| 891 | * in case the TA is statically linked |
| 892 | */ |
| 893 | static void xtest_tee_test_1012(ADBG_Case_t *c) |
| 894 | { |
| 895 | TEEC_Session session1 = { 0 }; |
| 896 | TEEC_Session session2 = { 0 }; |
| 897 | uint32_t ret_orig; |
| 898 | TEEC_UUID uuid = sims_test_ta_uuid; |
| 899 | |
| 900 | Do_ADBG_BeginSubCase(c, "Single Instance Multi Session"); |
| 901 | { |
| 902 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 903 | static const uint8_t in[] = { |
| 904 | 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96, |
| 905 | 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92, |
| 906 | 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6, |
| 907 | 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E |
| 908 | }; |
| 909 | uint8_t out[32] = { 0 }; |
| 910 | int i; |
| 911 | |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 912 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 913 | xtest_teec_open_session(&session1, &uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 914 | &ret_orig))) |
| 915 | return; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 916 | |
| 917 | op.params[0].value.a = 0; |
| 918 | op.params[1].tmpref.buffer = (void *)in; |
| 919 | op.params[1].tmpref.size = sizeof(in); |
| 920 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 921 | TEEC_MEMREF_TEMP_INPUT, |
| 922 | TEEC_NONE, TEEC_NONE); |
| 923 | |
| 924 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 925 | TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op, |
| 926 | &ret_orig)); |
| 927 | |
Jerome Forissier | 3cc0a42 | 2017-12-14 09:53:24 +0100 | [diff] [blame] | 928 | for (i = 1; i < 3; i++) { |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 929 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 930 | xtest_teec_open_session(&session2, &uuid, NULL, |
Volodymyr Babchuk | ae3dcd7 | 2016-10-20 16:51:59 +0300 | [diff] [blame] | 931 | &ret_orig))) |
| 932 | continue; |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 933 | |
| 934 | op.params[0].value.a = 0; |
| 935 | op.params[1].tmpref.buffer = out; |
| 936 | op.params[1].tmpref.size = sizeof(out); |
| 937 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 938 | TEEC_MEMREF_TEMP_OUTPUT, |
| 939 | TEEC_NONE, TEEC_NONE); |
| 940 | |
| 941 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 942 | TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ, |
| 943 | &op, &ret_orig)); |
| 944 | |
| 945 | if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out, |
| 946 | sizeof(out))) { |
| 947 | Do_ADBG_Log("in:"); |
| 948 | Do_ADBG_HexLog(in, sizeof(in), 16); |
| 949 | Do_ADBG_Log("out:"); |
| 950 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 951 | } |
| 952 | |
| 953 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, |
| 954 | TEEC_NONE, TEEC_NONE, |
| 955 | TEEC_NONE); |
| 956 | |
| 957 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 958 | TEEC_InvokeCommand(&session1, |
| 959 | TA_SIMS_CMD_GET_COUNTER, |
| 960 | &op, &ret_orig)); |
| 961 | |
| 962 | (void)ADBG_EXPECT(c, 0, op.params[0].value.a); |
| 963 | |
| 964 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 965 | TEEC_InvokeCommand(&session2, |
| 966 | TA_SIMS_CMD_GET_COUNTER, &op, |
| 967 | &ret_orig)); |
| 968 | |
| 969 | (void)ADBG_EXPECT(c, i, op.params[0].value.a); |
| 970 | TEEC_CloseSession(&session2); |
| 971 | } |
| 972 | |
| 973 | memset(out, 0, sizeof(out)); |
| 974 | op.params[0].value.a = 0; |
| 975 | op.params[1].tmpref.buffer = out; |
| 976 | op.params[1].tmpref.size = sizeof(out); |
| 977 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, |
| 978 | TEEC_MEMREF_TEMP_OUTPUT, |
| 979 | TEEC_NONE, TEEC_NONE); |
| 980 | |
| 981 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 982 | TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op, |
| 983 | &ret_orig)); |
| 984 | |
| 985 | if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) { |
| 986 | Do_ADBG_Log("in:"); |
| 987 | Do_ADBG_HexLog(in, sizeof(in), 16); |
| 988 | Do_ADBG_Log("out:"); |
| 989 | Do_ADBG_HexLog(out, sizeof(out), 16); |
| 990 | } |
| 991 | |
| 992 | TEEC_CloseSession(&session1); |
| 993 | } |
| 994 | } |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 995 | |
| 996 | struct test_1013_thread_arg { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 997 | const TEEC_UUID *uuid; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 998 | uint32_t cmd; |
| 999 | uint32_t repeat; |
| 1000 | TEEC_SharedMemory *shm; |
| 1001 | uint32_t error_orig; |
| 1002 | TEEC_Result res; |
| 1003 | uint32_t max_concurrency; |
| 1004 | const uint8_t *in; |
| 1005 | size_t in_len; |
| 1006 | uint8_t *out; |
| 1007 | size_t out_len; |
| 1008 | }; |
| 1009 | |
| 1010 | static void *test_1013_thread(void *arg) |
| 1011 | { |
| 1012 | struct test_1013_thread_arg *a = arg; |
| 1013 | TEEC_Session session = { 0 }; |
| 1014 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1015 | uint8_t p2 = TEEC_NONE; |
| 1016 | uint8_t p3 = TEEC_NONE; |
| 1017 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1018 | a->res = xtest_teec_open_session(&session, a->uuid, NULL, |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1019 | &a->error_orig); |
| 1020 | if (a->res != TEEC_SUCCESS) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | op.params[0].memref.parent = a->shm; |
| 1024 | op.params[0].memref.size = a->shm->size; |
| 1025 | op.params[0].memref.offset = 0; |
| 1026 | op.params[1].value.a = a->repeat; |
| 1027 | op.params[1].value.b = 0; |
| 1028 | op.params[2].tmpref.buffer = (void *)a->in; |
| 1029 | op.params[2].tmpref.size = a->in_len; |
| 1030 | op.params[3].tmpref.buffer = a->out; |
| 1031 | op.params[3].tmpref.size = a->out_len; |
| 1032 | |
| 1033 | if (a->in_len) |
| 1034 | p2 = TEEC_MEMREF_TEMP_INPUT; |
| 1035 | if (a->out_len) |
| 1036 | p3 = TEEC_MEMREF_TEMP_OUTPUT; |
| 1037 | |
| 1038 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, |
| 1039 | TEEC_VALUE_INOUT, p2, p3); |
| 1040 | |
| 1041 | a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig); |
| 1042 | a->max_concurrency = op.params[1].value.b; |
| 1043 | a->out_len = op.params[3].tmpref.size; |
| 1044 | TEEC_CloseSession(&session); |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1048 | #define NUM_THREADS 3 |
| 1049 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1050 | static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency, |
| 1051 | const TEEC_UUID *uuid) |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1052 | { |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1053 | size_t num_threads = NUM_THREADS; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1054 | size_t nt; |
| 1055 | size_t n; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1056 | size_t repeat = 1000; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1057 | pthread_t thr[num_threads]; |
| 1058 | TEEC_SharedMemory shm; |
| 1059 | size_t max_concurrency; |
| 1060 | struct test_1013_thread_arg arg[num_threads]; |
| 1061 | static const uint8_t sha256_in[] = { 'a', 'b', 'c' }; |
| 1062 | static const uint8_t sha256_out[] = { |
| 1063 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
| 1064 | 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
| 1065 | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
| 1066 | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad |
| 1067 | }; |
| 1068 | uint8_t out[32] = { 0 }; |
| 1069 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1070 | Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1071 | *mean_concurrency = 0; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1072 | |
| 1073 | memset(&shm, 0, sizeof(shm)); |
| 1074 | shm.size = sizeof(struct ta_concurrent_shm); |
| 1075 | shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; |
| 1076 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1077 | TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm))) |
| 1078 | return; |
| 1079 | |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1080 | memset(shm.buffer, 0, shm.size); |
| 1081 | memset(arg, 0, sizeof(arg)); |
| 1082 | max_concurrency = 0; |
| 1083 | nt = num_threads; |
| 1084 | |
| 1085 | for (n = 0; n < nt; n++) { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1086 | arg[n].uuid = uuid; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1087 | arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1088 | arg[n].repeat = repeat * 10; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1089 | arg[n].shm = &shm; |
| 1090 | if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL, |
| 1091 | test_1013_thread, arg + n))) |
| 1092 | nt = n; /* break loop and start cleanup */ |
| 1093 | } |
| 1094 | |
| 1095 | for (n = 0; n < nt; n++) { |
| 1096 | ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)); |
| 1097 | ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res); |
| 1098 | if (arg[n].max_concurrency > max_concurrency) |
| 1099 | max_concurrency = arg[n].max_concurrency; |
| 1100 | } |
| 1101 | |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1102 | /* |
| 1103 | * Concurrency can be limited by several factors, for instance in a |
| 1104 | * single CPU system it's dependent on the Preemtion Model used by |
| 1105 | * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the |
| 1106 | * best result there). |
| 1107 | */ |
| 1108 | (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0); |
| 1109 | (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, num_threads); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1110 | *mean_concurrency += max_concurrency; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1111 | Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1112 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1113 | Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1114 | memset(shm.buffer, 0, shm.size); |
| 1115 | memset(arg, 0, sizeof(arg)); |
| 1116 | max_concurrency = 0; |
| 1117 | nt = num_threads; |
| 1118 | |
| 1119 | for (n = 0; n < nt; n++) { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1120 | arg[n].uuid = uuid; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1121 | arg[n].cmd = TA_CONCURRENT_CMD_SHA256; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1122 | arg[n].repeat = repeat; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1123 | arg[n].shm = &shm; |
| 1124 | arg[n].in = sha256_in; |
| 1125 | arg[n].in_len = sizeof(sha256_in); |
| 1126 | arg[n].out = out; |
| 1127 | arg[n].out_len = sizeof(out); |
| 1128 | if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL, |
| 1129 | test_1013_thread, arg + n))) |
| 1130 | nt = n; /* break loop and start cleanup */ |
| 1131 | } |
| 1132 | |
| 1133 | for (n = 0; n < nt; n++) { |
| 1134 | if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) && |
| 1135 | ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res)) |
| 1136 | ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out), |
| 1137 | arg[n].out, arg[n].out_len); |
| 1138 | if (arg[n].max_concurrency > max_concurrency) |
| 1139 | max_concurrency = arg[n].max_concurrency; |
| 1140 | } |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1141 | *mean_concurrency += max_concurrency; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1142 | Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat); |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1143 | |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1144 | *mean_concurrency /= 2.0; |
Jens Wiklander | ac27ec1 | 2015-07-15 15:23:14 +0200 | [diff] [blame] | 1145 | TEEC_ReleaseSharedMemory(&shm); |
| 1146 | } |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1147 | |
| 1148 | static void xtest_tee_test_1013(ADBG_Case_t *c) |
| 1149 | { |
| 1150 | int i; |
| 1151 | double mean_concurrency; |
| 1152 | double concurrency; |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1153 | int nb_loops = 24; |
Jens Wiklander | 6a9d15a | 2016-02-01 10:29:42 +0100 | [diff] [blame] | 1154 | |
| 1155 | if (level == 0) |
| 1156 | nb_loops /= 2; |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1157 | |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1158 | Do_ADBG_BeginSubCase(c, "Using small concurrency TA"); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1159 | mean_concurrency = 0; |
| 1160 | for (i = 0; i < nb_loops; i++) { |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1161 | xtest_tee_test_1013_single(c, &concurrency, |
| 1162 | &concurrent_ta_uuid); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1163 | mean_concurrency += concurrency; |
| 1164 | } |
| 1165 | mean_concurrency /= nb_loops; |
| 1166 | |
| 1167 | Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS); |
| 1168 | Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency); |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1169 | Do_ADBG_EndSubCase(c, "Using small concurrency TA"); |
Pascal Brand | 4fa3558 | 2015-12-17 10:59:12 +0100 | [diff] [blame] | 1170 | |
Jens Wiklander | c9d7b24 | 2016-06-28 18:35:08 +0200 | [diff] [blame] | 1171 | #ifndef CFG_PAGED_USER_TA |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1172 | Do_ADBG_BeginSubCase(c, "Using large concurrency TA"); |
| 1173 | mean_concurrency = 0; |
| 1174 | for (i = 0; i < nb_loops; i++) { |
| 1175 | xtest_tee_test_1013_single(c, &concurrency, |
| 1176 | &concurrent_large_ta_uuid); |
| 1177 | mean_concurrency += concurrency; |
| 1178 | } |
| 1179 | mean_concurrency /= nb_loops; |
| 1180 | |
| 1181 | Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS); |
| 1182 | Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency); |
| 1183 | Do_ADBG_EndSubCase(c, "Using large concurrency TA"); |
Jens Wiklander | c9d7b24 | 2016-06-28 18:35:08 +0200 | [diff] [blame] | 1184 | #endif |
Jens Wiklander | 7067297 | 2016-04-06 00:01:45 +0200 | [diff] [blame] | 1185 | } |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1186 | |
| 1187 | #ifdef CFG_SECURE_DATA_PATH |
| 1188 | static void xtest_tee_test_1014(ADBG_Case_t *c) |
| 1189 | { |
| 1190 | UNUSED(c); |
| 1191 | |
| 1192 | int size = 17000; |
| 1193 | int loop = 10; |
| 1194 | int ion_heap = DEFAULT_ION_HEAP_TYPE; |
| 1195 | int rnd_offset = 1; |
| 1196 | int test; |
| 1197 | int ret; |
| 1198 | |
| 1199 | test = TEST_NS_TO_TA; |
| 1200 | Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA"); |
Etienne Carriere | b9a9582 | 2017-04-26 15:03:53 +0200 | [diff] [blame] | 1201 | ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1202 | ADBG_EXPECT(c, 0, ret); |
| 1203 | Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA"); |
| 1204 | |
| 1205 | test = TEST_TA_TO_TA; |
| 1206 | Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA"); |
Etienne Carriere | b9a9582 | 2017-04-26 15:03:53 +0200 | [diff] [blame] | 1207 | ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1208 | ADBG_EXPECT(c, 0, ret); |
| 1209 | Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA"); |
| 1210 | |
| 1211 | test = TEST_TA_TO_PTA; |
| 1212 | Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA"); |
Etienne Carriere | b9a9582 | 2017-04-26 15:03:53 +0200 | [diff] [blame] | 1213 | ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1214 | ADBG_EXPECT(c, 0, ret); |
| 1215 | Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA"); |
| 1216 | |
| 1217 | test = TEST_NS_TO_PTA; |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 1218 | Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)"); |
Etienne Carriere | b9a9582 | 2017-04-26 15:03:53 +0200 | [diff] [blame] | 1219 | ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1220 | ADBG_EXPECT(c, 1, ret); |
Etienne Carriere | a319852 | 2017-10-26 09:48:55 +0200 | [diff] [blame] | 1221 | Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)"); |
Etienne Carriere | 50abf9a | 2017-03-24 11:33:50 +0100 | [diff] [blame] | 1222 | } |
| 1223 | #endif |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1224 | |
| 1225 | static void xtest_tee_test_1015(ADBG_Case_t *c) |
| 1226 | { |
| 1227 | TEEC_Result res; |
| 1228 | TEEC_Session session = { 0 }; |
| 1229 | uint32_t ret_orig; |
| 1230 | |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 1231 | /* Pseudo TA is optional: warn and nicely exit if not found */ |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1232 | res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL, |
| 1233 | &ret_orig); |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 1234 | if (res == TEEC_ERROR_ITEM_NOT_FOUND) { |
| 1235 | Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found"); |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1236 | return; |
Etienne Carriere | 1109316 | 2017-10-26 09:49:04 +0200 | [diff] [blame] | 1237 | } |
| 1238 | ADBG_EXPECT_TEEC_SUCCESS(c, res); |
Jens Wiklander | 272d364 | 2017-04-03 13:03:47 +0200 | [diff] [blame] | 1239 | |
| 1240 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1241 | TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE, |
| 1242 | NULL, &ret_orig)); |
| 1243 | TEEC_CloseSession(&session); |
| 1244 | } |
Jerome Forissier | e916b10 | 2017-06-07 17:55:52 +0200 | [diff] [blame] | 1245 | |
| 1246 | static void xtest_tee_test_1016(ADBG_Case_t *c) |
| 1247 | { |
| 1248 | TEEC_Session session = { 0 }; |
| 1249 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 1250 | uint32_t ret_orig; |
| 1251 | |
| 1252 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1253 | xtest_teec_open_session(&session, &os_test_ta_uuid, NULL, |
| 1254 | &ret_orig))) |
| 1255 | return; |
| 1256 | |
| 1257 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, |
| 1258 | TEEC_NONE); |
| 1259 | |
| 1260 | (void)ADBG_EXPECT_TEEC_SUCCESS(c, |
| 1261 | TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op, |
| 1262 | &ret_orig)); |
| 1263 | |
| 1264 | TEEC_CloseSession(&session); |
| 1265 | } |