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> |
| 33 | |
| 34 | static TEE_Result set_global(uint32_t param_types, TEE_Param params[4]); |
| 35 | static TEE_Result get_global(uint32_t param_types, TEE_Param params[4]); |
| 36 | static int _globalvalue; |
| 37 | |
| 38 | /* |
| 39 | * Trusted Application Entry Points |
| 40 | */ |
| 41 | |
| 42 | /* Called each time a new instance is created */ |
| 43 | TEE_Result TA_CreateEntryPoint(void) |
| 44 | { |
| 45 | return TEE_SUCCESS; |
| 46 | } |
| 47 | |
| 48 | /* Called each time an instance is destroyed */ |
| 49 | void TA_DestroyEntryPoint(void) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | /* Called each time a session is opened */ |
| 54 | TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes, |
| 55 | TEE_Param pParams[4], |
| 56 | void **ppSessionContext) |
| 57 | { |
| 58 | (void)nParamTypes; |
| 59 | (void)pParams; |
| 60 | (void)ppSessionContext; |
| 61 | return TEE_SUCCESS; |
| 62 | } |
| 63 | |
| 64 | /* Called each time a session is closed */ |
| 65 | void TA_CloseSessionEntryPoint(void *pSessionContext) |
| 66 | { |
| 67 | (void)pSessionContext; |
| 68 | } |
| 69 | |
| 70 | /* Called when a command is invoked */ |
| 71 | TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext, |
| 72 | uint32_t nCommandID, uint32_t nParamTypes, |
| 73 | TEE_Param pParams[4]) |
| 74 | { |
| 75 | (void)pSessionContext; |
| 76 | |
| 77 | switch (nCommandID) { |
| 78 | case TA_CRYPT_CMD_SHA224: |
| 79 | return ta_entry_sha224(nParamTypes, pParams); |
| 80 | |
| 81 | case TA_CRYPT_CMD_SHA256: |
| 82 | return ta_entry_sha256(nParamTypes, pParams); |
| 83 | |
| 84 | case TA_CRYPT_CMD_AES256ECB_ENC: |
| 85 | return ta_entry_aes256ecb_encrypt(nParamTypes, pParams); |
| 86 | |
| 87 | case TA_CRYPT_CMD_AES256ECB_DEC: |
| 88 | return ta_entry_aes256ecb_decrypt(nParamTypes, pParams); |
| 89 | |
| 90 | case TA_CRYPT_CMD_ALLOCATE_OPERATION: |
| 91 | return ta_entry_allocate_operation(nParamTypes, pParams); |
| 92 | |
| 93 | case TA_CRYPT_CMD_FREE_OPERATION: |
| 94 | return ta_entry_free_operation(nParamTypes, pParams); |
| 95 | |
| 96 | case TA_CRYPT_CMD_GET_OPERATION_INFO: |
| 97 | return ta_entry_get_operation_info(nParamTypes, pParams); |
| 98 | |
| 99 | case TA_CRYPT_CMD_RESET_OPERATION: |
| 100 | return ta_entry_reset_operation(nParamTypes, pParams); |
| 101 | |
| 102 | case TA_CRYPT_CMD_SET_OPERATION_KEY: |
| 103 | return ta_entry_set_operation_key(nParamTypes, pParams); |
| 104 | |
| 105 | case TA_CRYPT_CMD_SET_OPERATION_KEY2: |
| 106 | return ta_entry_set_operation_key2(nParamTypes, pParams); |
| 107 | |
| 108 | case TA_CRYPT_CMD_COPY_OPERATION: |
| 109 | return ta_entry_copy_operation(nParamTypes, pParams); |
| 110 | |
| 111 | case TA_CRYPT_CMD_DIGEST_UPDATE: |
| 112 | return ta_entry_digest_update(nParamTypes, pParams); |
| 113 | |
| 114 | case TA_CRYPT_CMD_DIGEST_DO_FINAL: |
| 115 | return ta_entry_digest_do_final(nParamTypes, pParams); |
| 116 | |
| 117 | case TA_CRYPT_CMD_CIPHER_INIT: |
| 118 | return ta_entry_cipher_init(nParamTypes, pParams); |
| 119 | |
| 120 | case TA_CRYPT_CMD_CIPHER_UPDATE: |
| 121 | return ta_entry_cipher_update(nParamTypes, pParams); |
| 122 | |
| 123 | case TA_CRYPT_CMD_CIPHER_DO_FINAL: |
| 124 | return ta_entry_cipher_do_final(nParamTypes, pParams); |
| 125 | |
| 126 | case TA_CRYPT_CMD_MAC_INIT: |
| 127 | return ta_entry_mac_init(nParamTypes, pParams); |
| 128 | |
| 129 | case TA_CRYPT_CMD_MAC_UPDATE: |
| 130 | return ta_entry_mac_update(nParamTypes, pParams); |
| 131 | |
| 132 | case TA_CRYPT_CMD_MAC_FINAL_COMPUTE: |
| 133 | return ta_entry_mac_final_compute(nParamTypes, pParams); |
| 134 | |
| 135 | case TA_CRYPT_CMD_MAC_FINAL_COMPARE: |
| 136 | return ta_entry_mac_final_compare(nParamTypes, pParams); |
| 137 | |
| 138 | case TA_CRYPT_CMD_ALLOCATE_TRANSIENT_OBJECT: |
| 139 | return ta_entry_allocate_transient_object(nParamTypes, pParams); |
| 140 | |
| 141 | case TA_CRYPT_CMD_FREE_TRANSIENT_OBJECT: |
| 142 | return ta_entry_free_transient_object(nParamTypes, pParams); |
| 143 | |
| 144 | case TA_CRYPT_CMD_RESET_TRANSIENT_OBJECT: |
| 145 | return ta_entry_reset_transient_object(nParamTypes, pParams); |
| 146 | |
| 147 | case TA_CRYPT_CMD_POPULATE_TRANSIENT_OBJECT: |
| 148 | return ta_entry_populate_transient_object(nParamTypes, pParams); |
| 149 | |
| 150 | case TA_CRYPT_CMD_COPY_OBJECT_ATTRIBUTES: |
| 151 | return ta_entry_copy_object_attributes(nParamTypes, pParams); |
| 152 | |
| 153 | case TA_CRYPT_CMD_GENERATE_KEY: |
| 154 | return ta_entry_generate_key(nParamTypes, pParams); |
| 155 | |
| 156 | case TA_CRYPT_CMD_ASYMMETRIC_ENCRYPT: |
| 157 | return ta_entry_asymmetric_encrypt(nParamTypes, pParams); |
| 158 | |
| 159 | case TA_CRYPT_CMD_ASYMMETRIC_DECRYPT: |
| 160 | return ta_entry_asymmetric_decrypt(nParamTypes, pParams); |
| 161 | |
| 162 | case TA_CRYPT_CMD_ASYMMETRIC_SIGN_DIGEST: |
| 163 | return ta_entry_asymmetric_sign_digest(nParamTypes, pParams); |
| 164 | |
| 165 | case TA_CRYPT_CMD_ASYMMETRIC_VERIFY_DIGEST: |
| 166 | return ta_entry_asymmetric_verify_digest(nParamTypes, pParams); |
| 167 | |
| 168 | case TA_CRYPT_CMD_DERIVE_KEY: |
| 169 | return ta_entry_derive_key(nParamTypes, pParams); |
| 170 | |
| 171 | case TA_CRYPT_CMD_RANDOM_NUMBER_GENEREATE: |
| 172 | return ta_entry_random_number_generate(nParamTypes, pParams); |
| 173 | |
| 174 | case TA_CRYPT_CMD_AE_INIT: |
| 175 | return ta_entry_ae_init(nParamTypes, pParams); |
| 176 | |
| 177 | case TA_CRYPT_CMD_AE_UPDATE_AAD: |
| 178 | return ta_entry_ae_update_aad(nParamTypes, pParams); |
| 179 | |
| 180 | case TA_CRYPT_CMD_AE_UPDATE: |
| 181 | return ta_entry_ae_update(nParamTypes, pParams); |
| 182 | |
| 183 | case TA_CRYPT_CMD_AE_ENCRYPT_FINAL: |
| 184 | return ta_entry_ae_encrypt_final(nParamTypes, pParams); |
| 185 | |
| 186 | case TA_CRYPT_CMD_AE_DECRYPT_FINAL: |
| 187 | return ta_entry_ae_decrypt_final(nParamTypes, pParams); |
| 188 | |
| 189 | case TA_CRYPT_CMD_GET_OBJECT_BUFFER_ATTRIBUTE: |
| 190 | return ta_entry_get_object_buffer_attribute(nParamTypes, |
| 191 | pParams); |
| 192 | case TA_CRYPT_CMD_GET_OBJECT_VALUE_ATTRIBUTE: |
| 193 | return ta_entry_get_object_value_attribute(nParamTypes, |
| 194 | pParams); |
| 195 | case TA_CRYPT_CMD_SETGLOBAL: |
| 196 | return set_global(nParamTypes, pParams); |
| 197 | |
| 198 | case TA_CRYPT_CMD_GETGLOBAL: |
| 199 | return get_global(nParamTypes, pParams); |
| 200 | |
| 201 | default: |
| 202 | return TEE_ERROR_BAD_PARAMETERS; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static TEE_Result set_global(uint32_t param_types, TEE_Param params[4]) |
| 207 | { |
| 208 | int i; |
| 209 | |
| 210 | /* Param 0 is a memref, input/output */ |
| 211 | if (TEE_PARAM_TYPE_VALUE_INPUT != TEE_PARAM_TYPE_GET(param_types, 0)) |
| 212 | return TEE_ERROR_BAD_PARAMETERS; |
| 213 | |
| 214 | /* Other parameters must be of type TEE_PARAM_TYPE_NONE */ |
| 215 | for (i = 1; i < 4; i++) { |
| 216 | if (TEE_PARAM_TYPE_NONE != TEE_PARAM_TYPE_GET(param_types, i)) |
| 217 | return TEE_ERROR_BAD_PARAMETERS; |
| 218 | } |
| 219 | |
| 220 | _globalvalue = params[0].value.a; |
| 221 | return TEE_SUCCESS; |
| 222 | } |
| 223 | |
| 224 | static TEE_Result get_global(uint32_t param_types, TEE_Param params[4]) |
| 225 | { |
| 226 | int i; |
| 227 | |
| 228 | /* Param 0 is a memref, input/output */ |
| 229 | if (TEE_PARAM_TYPE_VALUE_OUTPUT != TEE_PARAM_TYPE_GET(param_types, 0)) |
| 230 | return TEE_ERROR_BAD_PARAMETERS; |
| 231 | |
| 232 | /* Other parameters must be of type TEE_PARAM_TYPE_NONE */ |
| 233 | for (i = 1; i < 4; i++) { |
| 234 | if (TEE_PARAM_TYPE_NONE != TEE_PARAM_TYPE_GET(param_types, i)) |
| 235 | return TEE_ERROR_BAD_PARAMETERS; |
| 236 | } |
| 237 | |
| 238 | params[0].value.a = _globalvalue; |
| 239 | return TEE_SUCCESS; |
| 240 | } |