Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, STMicroelectronics International N.V. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 28 | #include <aes_taf.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 29 | #include <cryp_taf.h> |
Jens Wiklander | e9babd9 | 2018-04-20 11:20:59 +0200 | [diff] [blame] | 30 | #include <mbedtls_taf.h> |
Igor Opaniuk | 7ddaa78 | 2018-05-25 15:14:05 +0300 | [diff] [blame] | 31 | #include <seed_rng_taf.h> |
Jens Wiklander | e9babd9 | 2018-04-20 11:20:59 +0200 | [diff] [blame] | 32 | #include <sha2_taf.h> |
| 33 | #include <ta_crypt.h> |
| 34 | #include <tee_ta_api.h> |
Jens Wiklander | 065ccfa | 2015-08-21 14:39:20 +0200 | [diff] [blame] | 35 | #include <trace.h> |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 36 | |
| 37 | static TEE_Result set_global(uint32_t param_types, TEE_Param params[4]); |
| 38 | static TEE_Result get_global(uint32_t param_types, TEE_Param params[4]); |
| 39 | static int _globalvalue; |
| 40 | |
| 41 | /* |
| 42 | * Trusted Application Entry Points |
| 43 | */ |
| 44 | |
| 45 | /* Called each time a new instance is created */ |
| 46 | TEE_Result TA_CreateEntryPoint(void) |
| 47 | { |
| 48 | return TEE_SUCCESS; |
| 49 | } |
| 50 | |
| 51 | /* Called each time an instance is destroyed */ |
| 52 | void TA_DestroyEntryPoint(void) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /* Called each time a session is opened */ |
| 57 | TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes, |
| 58 | TEE_Param pParams[4], |
| 59 | void **ppSessionContext) |
| 60 | { |
| 61 | (void)nParamTypes; |
| 62 | (void)pParams; |
| 63 | (void)ppSessionContext; |
| 64 | return TEE_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | /* Called each time a session is closed */ |
| 68 | void TA_CloseSessionEntryPoint(void *pSessionContext) |
| 69 | { |
| 70 | (void)pSessionContext; |
| 71 | } |
| 72 | |
Jens Wiklander | 065ccfa | 2015-08-21 14:39:20 +0200 | [diff] [blame] | 73 | /* |
| 74 | * To provoke the linker to produce R_ARM_ABS32 relocations we need to |
| 75 | * pre-initilize a pointer to the function and then also call the function |
| 76 | * directly. |
| 77 | */ |
| 78 | static TEE_Result (*ta_cmd_entries[])(uint32_t, TEE_Param *) = { |
| 79 | [TA_CRYPT_CMD_SHA224] = ta_entry_sha224, |
| 80 | [TA_CRYPT_CMD_SHA256] = ta_entry_sha256, |
| 81 | }; |
| 82 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 83 | /* Called when a command is invoked */ |
| 84 | TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext, |
| 85 | uint32_t nCommandID, uint32_t nParamTypes, |
| 86 | TEE_Param pParams[4]) |
| 87 | { |
Jens Wiklander | 065ccfa | 2015-08-21 14:39:20 +0200 | [diff] [blame] | 88 | static bool use_fptr = false; |
| 89 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 90 | (void)pSessionContext; |
| 91 | |
Jens Wiklander | 065ccfa | 2015-08-21 14:39:20 +0200 | [diff] [blame] | 92 | |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 93 | switch (nCommandID) { |
| 94 | case TA_CRYPT_CMD_SHA224: |
Jens Wiklander | 065ccfa | 2015-08-21 14:39:20 +0200 | [diff] [blame] | 95 | use_fptr = !use_fptr; |
| 96 | if (use_fptr) |
| 97 | return ta_cmd_entries[nCommandID](nParamTypes, pParams); |
| 98 | else |
| 99 | return ta_entry_sha224(nParamTypes, pParams); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 100 | |
| 101 | case TA_CRYPT_CMD_SHA256: |
Jens Wiklander | 065ccfa | 2015-08-21 14:39:20 +0200 | [diff] [blame] | 102 | use_fptr = !use_fptr; |
| 103 | if (use_fptr) |
| 104 | return ta_cmd_entries[nCommandID](nParamTypes, pParams); |
| 105 | else |
| 106 | return ta_entry_sha256(nParamTypes, pParams); |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 107 | |
| 108 | case TA_CRYPT_CMD_AES256ECB_ENC: |
| 109 | return ta_entry_aes256ecb_encrypt(nParamTypes, pParams); |
| 110 | |
| 111 | case TA_CRYPT_CMD_AES256ECB_DEC: |
| 112 | return ta_entry_aes256ecb_decrypt(nParamTypes, pParams); |
| 113 | |
| 114 | case TA_CRYPT_CMD_ALLOCATE_OPERATION: |
| 115 | return ta_entry_allocate_operation(nParamTypes, pParams); |
| 116 | |
| 117 | case TA_CRYPT_CMD_FREE_OPERATION: |
| 118 | return ta_entry_free_operation(nParamTypes, pParams); |
| 119 | |
| 120 | case TA_CRYPT_CMD_GET_OPERATION_INFO: |
| 121 | return ta_entry_get_operation_info(nParamTypes, pParams); |
| 122 | |
| 123 | case TA_CRYPT_CMD_RESET_OPERATION: |
| 124 | return ta_entry_reset_operation(nParamTypes, pParams); |
| 125 | |
| 126 | case TA_CRYPT_CMD_SET_OPERATION_KEY: |
| 127 | return ta_entry_set_operation_key(nParamTypes, pParams); |
| 128 | |
| 129 | case TA_CRYPT_CMD_SET_OPERATION_KEY2: |
| 130 | return ta_entry_set_operation_key2(nParamTypes, pParams); |
| 131 | |
| 132 | case TA_CRYPT_CMD_COPY_OPERATION: |
| 133 | return ta_entry_copy_operation(nParamTypes, pParams); |
| 134 | |
| 135 | case TA_CRYPT_CMD_DIGEST_UPDATE: |
| 136 | return ta_entry_digest_update(nParamTypes, pParams); |
| 137 | |
| 138 | case TA_CRYPT_CMD_DIGEST_DO_FINAL: |
| 139 | return ta_entry_digest_do_final(nParamTypes, pParams); |
| 140 | |
| 141 | case TA_CRYPT_CMD_CIPHER_INIT: |
| 142 | return ta_entry_cipher_init(nParamTypes, pParams); |
| 143 | |
| 144 | case TA_CRYPT_CMD_CIPHER_UPDATE: |
| 145 | return ta_entry_cipher_update(nParamTypes, pParams); |
| 146 | |
| 147 | case TA_CRYPT_CMD_CIPHER_DO_FINAL: |
| 148 | return ta_entry_cipher_do_final(nParamTypes, pParams); |
| 149 | |
| 150 | case TA_CRYPT_CMD_MAC_INIT: |
| 151 | return ta_entry_mac_init(nParamTypes, pParams); |
| 152 | |
| 153 | case TA_CRYPT_CMD_MAC_UPDATE: |
| 154 | return ta_entry_mac_update(nParamTypes, pParams); |
| 155 | |
| 156 | case TA_CRYPT_CMD_MAC_FINAL_COMPUTE: |
| 157 | return ta_entry_mac_final_compute(nParamTypes, pParams); |
| 158 | |
| 159 | case TA_CRYPT_CMD_MAC_FINAL_COMPARE: |
| 160 | return ta_entry_mac_final_compare(nParamTypes, pParams); |
| 161 | |
| 162 | case TA_CRYPT_CMD_ALLOCATE_TRANSIENT_OBJECT: |
| 163 | return ta_entry_allocate_transient_object(nParamTypes, pParams); |
| 164 | |
| 165 | case TA_CRYPT_CMD_FREE_TRANSIENT_OBJECT: |
| 166 | return ta_entry_free_transient_object(nParamTypes, pParams); |
| 167 | |
| 168 | case TA_CRYPT_CMD_RESET_TRANSIENT_OBJECT: |
| 169 | return ta_entry_reset_transient_object(nParamTypes, pParams); |
| 170 | |
| 171 | case TA_CRYPT_CMD_POPULATE_TRANSIENT_OBJECT: |
| 172 | return ta_entry_populate_transient_object(nParamTypes, pParams); |
| 173 | |
| 174 | case TA_CRYPT_CMD_COPY_OBJECT_ATTRIBUTES: |
| 175 | return ta_entry_copy_object_attributes(nParamTypes, pParams); |
| 176 | |
| 177 | case TA_CRYPT_CMD_GENERATE_KEY: |
| 178 | return ta_entry_generate_key(nParamTypes, pParams); |
| 179 | |
| 180 | case TA_CRYPT_CMD_ASYMMETRIC_ENCRYPT: |
| 181 | return ta_entry_asymmetric_encrypt(nParamTypes, pParams); |
| 182 | |
| 183 | case TA_CRYPT_CMD_ASYMMETRIC_DECRYPT: |
| 184 | return ta_entry_asymmetric_decrypt(nParamTypes, pParams); |
| 185 | |
| 186 | case TA_CRYPT_CMD_ASYMMETRIC_SIGN_DIGEST: |
| 187 | return ta_entry_asymmetric_sign_digest(nParamTypes, pParams); |
| 188 | |
| 189 | case TA_CRYPT_CMD_ASYMMETRIC_VERIFY_DIGEST: |
| 190 | return ta_entry_asymmetric_verify_digest(nParamTypes, pParams); |
| 191 | |
| 192 | case TA_CRYPT_CMD_DERIVE_KEY: |
| 193 | return ta_entry_derive_key(nParamTypes, pParams); |
| 194 | |
| 195 | case TA_CRYPT_CMD_RANDOM_NUMBER_GENEREATE: |
| 196 | return ta_entry_random_number_generate(nParamTypes, pParams); |
| 197 | |
| 198 | case TA_CRYPT_CMD_AE_INIT: |
| 199 | return ta_entry_ae_init(nParamTypes, pParams); |
| 200 | |
| 201 | case TA_CRYPT_CMD_AE_UPDATE_AAD: |
| 202 | return ta_entry_ae_update_aad(nParamTypes, pParams); |
| 203 | |
| 204 | case TA_CRYPT_CMD_AE_UPDATE: |
| 205 | return ta_entry_ae_update(nParamTypes, pParams); |
| 206 | |
| 207 | case TA_CRYPT_CMD_AE_ENCRYPT_FINAL: |
| 208 | return ta_entry_ae_encrypt_final(nParamTypes, pParams); |
| 209 | |
| 210 | case TA_CRYPT_CMD_AE_DECRYPT_FINAL: |
| 211 | return ta_entry_ae_decrypt_final(nParamTypes, pParams); |
| 212 | |
| 213 | case TA_CRYPT_CMD_GET_OBJECT_BUFFER_ATTRIBUTE: |
| 214 | return ta_entry_get_object_buffer_attribute(nParamTypes, |
| 215 | pParams); |
| 216 | case TA_CRYPT_CMD_GET_OBJECT_VALUE_ATTRIBUTE: |
| 217 | return ta_entry_get_object_value_attribute(nParamTypes, |
| 218 | pParams); |
| 219 | case TA_CRYPT_CMD_SETGLOBAL: |
| 220 | return set_global(nParamTypes, pParams); |
| 221 | |
| 222 | case TA_CRYPT_CMD_GETGLOBAL: |
| 223 | return get_global(nParamTypes, pParams); |
| 224 | |
Jens Wiklander | e9babd9 | 2018-04-20 11:20:59 +0200 | [diff] [blame] | 225 | #ifdef CFG_TA_MBEDTLS |
| 226 | case TA_CRYPT_CMD_MBEDTLS_SELF_TESTS: |
| 227 | return ta_entry_mbedtls_self_tests(nParamTypes, pParams); |
Jens Wiklander | f7ffa64 | 2018-04-20 16:25:21 +0200 | [diff] [blame] | 228 | case TA_CRYPT_CMD_MBEDTLS_CHECK_CERT: |
| 229 | return ta_entry_mbedtls_check_cert(nParamTypes, pParams); |
Jens Wiklander | da0208e | 2018-04-30 09:34:01 +0200 | [diff] [blame] | 230 | case TA_CRYPT_CMD_MBEDTLS_SIGN_CERT: |
| 231 | return ta_entry_mbedtls_sign_cert(nParamTypes, pParams); |
Jens Wiklander | e9babd9 | 2018-04-20 11:20:59 +0200 | [diff] [blame] | 232 | #endif |
Igor Opaniuk | 7ddaa78 | 2018-05-25 15:14:05 +0300 | [diff] [blame] | 233 | #ifdef CFG_SYSTEM_PTA |
| 234 | case TA_CRYPT_CMD_SEED_RNG_POOL: |
| 235 | return seed_rng_pool(nParamTypes, pParams); |
| 236 | #endif |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 237 | default: |
| 238 | return TEE_ERROR_BAD_PARAMETERS; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static TEE_Result set_global(uint32_t param_types, TEE_Param params[4]) |
| 243 | { |
| 244 | int i; |
| 245 | |
| 246 | /* Param 0 is a memref, input/output */ |
| 247 | if (TEE_PARAM_TYPE_VALUE_INPUT != TEE_PARAM_TYPE_GET(param_types, 0)) |
| 248 | return TEE_ERROR_BAD_PARAMETERS; |
| 249 | |
| 250 | /* Other parameters must be of type TEE_PARAM_TYPE_NONE */ |
| 251 | for (i = 1; i < 4; i++) { |
| 252 | if (TEE_PARAM_TYPE_NONE != TEE_PARAM_TYPE_GET(param_types, i)) |
| 253 | return TEE_ERROR_BAD_PARAMETERS; |
| 254 | } |
| 255 | |
| 256 | _globalvalue = params[0].value.a; |
| 257 | return TEE_SUCCESS; |
| 258 | } |
| 259 | |
| 260 | static TEE_Result get_global(uint32_t param_types, TEE_Param params[4]) |
| 261 | { |
| 262 | int i; |
| 263 | |
| 264 | /* Param 0 is a memref, input/output */ |
| 265 | if (TEE_PARAM_TYPE_VALUE_OUTPUT != TEE_PARAM_TYPE_GET(param_types, 0)) |
| 266 | return TEE_ERROR_BAD_PARAMETERS; |
| 267 | |
| 268 | /* Other parameters must be of type TEE_PARAM_TYPE_NONE */ |
| 269 | for (i = 1; i < 4; i++) { |
| 270 | if (TEE_PARAM_TYPE_NONE != TEE_PARAM_TYPE_GET(param_types, i)) |
| 271 | return TEE_ERROR_BAD_PARAMETERS; |
| 272 | } |
| 273 | |
| 274 | params[0].value.a = _globalvalue; |
| 275 | return TEE_SUCCESS; |
| 276 | } |