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 | #include <stdint.h> |
| 28 | |
| 29 | #include <ta_rpc.h> |
| 30 | #include <tee_api.h> |
| 31 | #include <trace.h> |
| 32 | #include <ta_crypt.h> |
| 33 | #include <ta_sims_test.h> |
| 34 | |
| 35 | static TEE_UUID cryp_uuid = TA_CRYPT_UUID; |
| 36 | |
| 37 | static TEE_Result rpc_call_cryp(uint32_t nParamTypes, TEE_Param pParams[4], |
| 38 | uint32_t cmd) |
| 39 | { |
| 40 | TEE_TASessionHandle cryp_session; |
| 41 | TEE_Result res; |
| 42 | uint32_t origin; |
| 43 | TEE_Param params[4]; |
| 44 | |
| 45 | uint32_t types = |
| 46 | TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE, |
| 47 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE); |
| 48 | |
| 49 | TEE_MemFill(params, 0, sizeof(TEE_Param) * 4); |
| 50 | |
| 51 | res = TEE_OpenTASession(&cryp_uuid, 0, types, params, &cryp_session, |
| 52 | &origin); |
| 53 | |
| 54 | if (res != TEE_SUCCESS) { |
| 55 | EMSG("rpc_sha256 - TEE_OpenTASession returned 0x%x\n", |
| 56 | (unsigned int)res); |
| 57 | return res; |
| 58 | } |
| 59 | |
| 60 | res = TEE_InvokeTACommand(cryp_session, 0, cmd, nParamTypes, |
| 61 | pParams, &origin); |
| 62 | |
| 63 | if (res != TEE_SUCCESS) { |
| 64 | EMSG("rpc_sha256 - TEE_InvokeTACommand returned 0x%x\n", |
| 65 | (unsigned int)res); |
| 66 | } |
| 67 | |
| 68 | TEE_CloseTASession(cryp_session); |
| 69 | |
| 70 | return res; |
| 71 | } |
| 72 | |
| 73 | TEE_Result rpc_sha224(uint32_t nParamTypes, TEE_Param pParams[4]) |
| 74 | { |
| 75 | return rpc_call_cryp(nParamTypes, pParams, TA_CRYPT_CMD_SHA224); |
| 76 | } |
| 77 | |
| 78 | TEE_Result rpc_sha256(uint32_t nParamTypes, TEE_Param pParams[4]) |
| 79 | { |
| 80 | return rpc_call_cryp(nParamTypes, pParams, TA_CRYPT_CMD_SHA256); |
| 81 | } |
| 82 | |
| 83 | TEE_Result rpc_aes256ecb_encrypt(uint32_t nParamTypes, TEE_Param pParams[4]) |
| 84 | { |
| 85 | return rpc_call_cryp(nParamTypes, pParams, TA_CRYPT_CMD_AES256ECB_ENC); |
| 86 | } |
| 87 | |
| 88 | TEE_Result rpc_aes256ecb_decrypt(uint32_t nParamTypes, TEE_Param pParams[4]) |
| 89 | { |
| 90 | return rpc_call_cryp(nParamTypes, pParams, TA_CRYPT_CMD_AES256ECB_DEC); |
| 91 | } |
| 92 | |
| 93 | TEE_Result rpc_open(void *session_context, uint32_t param_types, |
| 94 | TEE_Param params[4]) |
| 95 | { |
| 96 | TEE_TASessionHandle session; |
| 97 | uint32_t orig; |
| 98 | TEE_Result res; |
| 99 | TEE_UUID uuid = TA_SIMS_TEST_UUID; |
| 100 | uint32_t types = |
| 101 | TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, TEE_PARAM_TYPE_NONE, |
| 102 | TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE); |
| 103 | TEE_Param par[4]; |
| 104 | |
| 105 | (void)session_context; |
| 106 | (void)param_types; |
| 107 | |
| 108 | res = TEE_OpenTASession(&uuid, 0, 0, NULL, &session, &orig); |
| 109 | |
| 110 | if (res != TEE_SUCCESS) |
| 111 | return res; |
| 112 | |
| 113 | TEE_MemFill(params, 0, sizeof(TEE_Param) * 4); |
| 114 | res = |
| 115 | TEE_InvokeTACommand(session, 0, TA_SIMS_CMD_GET_COUNTER, types, par, |
| 116 | &orig); |
| 117 | |
| 118 | if (res != TEE_SUCCESS) |
| 119 | goto exit; |
| 120 | |
| 121 | exit: |
| 122 | TEE_CloseTASession(session); |
| 123 | |
| 124 | return res; |
| 125 | } |