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