Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015, Linaro Limited |
| 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_internal_api.h> |
| 29 | #include <tee_ta_api.h> |
| 30 | #include <string.h> |
| 31 | #include <trace.h> |
| 32 | |
| 33 | #include "ta_aes_perf.h" |
| 34 | #include "ta_aes_perf_priv.h" |
| 35 | |
| 36 | #define CHECK(res, name, action) do { \ |
| 37 | if ((res) != TEE_SUCCESS) { \ |
| 38 | DMSG(name ": 0x%08x", (res)); \ |
| 39 | action \ |
| 40 | } \ |
| 41 | } while(0) |
| 42 | |
| 43 | static uint8_t iv[] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, |
| 44 | 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF }; |
| 45 | static int use_iv; |
| 46 | |
| 47 | static TEE_OperationHandle crypto_op = NULL; |
| 48 | |
| 49 | |
| 50 | |
| 51 | TEE_Result cmd_process(uint32_t param_types, TEE_Param params[4]) |
| 52 | { |
| 53 | TEE_Result res; |
| 54 | int n; |
| 55 | void *in, *out; |
| 56 | uint32_t insz; |
| 57 | uint32_t outsz; |
| 58 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT, |
| 59 | TEE_PARAM_TYPE_MEMREF_INOUT, |
| 60 | TEE_PARAM_TYPE_VALUE_INPUT, |
| 61 | TEE_PARAM_TYPE_NONE); |
| 62 | |
| 63 | if (param_types != exp_param_types) |
| 64 | return TEE_ERROR_BAD_PARAMETERS; |
| 65 | |
| 66 | in = params[0].memref.buffer; |
| 67 | insz = params[0].memref.size; |
| 68 | out = params[1].memref.buffer; |
| 69 | outsz = params[1].memref.size; |
| 70 | n = params[2].value.a; |
| 71 | |
| 72 | while (n--) { |
| 73 | res = TEE_CipherUpdate(crypto_op, in, insz, out, &outsz); |
| 74 | CHECK(res, "TEE_CipherUpdate", return res;); |
| 75 | } |
| 76 | return TEE_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | TEE_Result cmd_prepare_key(uint32_t param_types, TEE_Param params[4]) |
| 80 | { |
| 81 | TEE_Result res; |
| 82 | TEE_ObjectHandle hkey; |
| 83 | TEE_ObjectHandle hkey2; |
| 84 | TEE_Attribute attr; |
| 85 | uint32_t mode; |
| 86 | uint32_t op_keysize; |
| 87 | uint32_t keysize; |
| 88 | uint32_t algo; |
| 89 | static uint8_t aes_key[] = { 0x00, 0x01, 0x02, 0x03, |
| 90 | 0x04, 0x05, 0x06, 0x07, |
| 91 | 0x08, 0x09, 0x0A, 0x0B, |
| 92 | 0x0C, 0x0D, 0x0E, 0x0F, |
| 93 | 0x10, 0x11, 0x12, 0x13, |
| 94 | 0x14, 0x15, 0x16, 0x17, |
| 95 | 0x18, 0x19, 0x1A, 0x1B, |
| 96 | 0x1C, 0x1D, 0x1E, 0x1F }; |
| 97 | static uint8_t aes_key2[] = { 0x20, 0x21, 0x22, 0x23, |
| 98 | 0x24, 0x25, 0x26, 0x27, |
| 99 | 0x28, 0x29, 0x2A, 0x2B, |
| 100 | 0x2C, 0x2D, 0x2E, 0x2F, |
| 101 | 0x30, 0x31, 0x32, 0x33, |
| 102 | 0x34, 0x35, 0x36, 0x37, |
| 103 | 0x38, 0x39, 0x3A, 0x3B, |
| 104 | 0x3C, 0x3D, 0x3E, 0x3F }; |
| 105 | |
| 106 | uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, |
| 107 | TEE_PARAM_TYPE_VALUE_INPUT, |
| 108 | TEE_PARAM_TYPE_NONE, |
| 109 | TEE_PARAM_TYPE_NONE); |
| 110 | if (param_types != exp_param_types) |
| 111 | return TEE_ERROR_BAD_PARAMETERS; |
| 112 | |
| 113 | mode = params[0].value.a ? TEE_MODE_DECRYPT : TEE_MODE_ENCRYPT; |
| 114 | keysize = params[0].value.b; |
| 115 | op_keysize = keysize; |
| 116 | |
| 117 | switch (params[1].value.a) { |
| 118 | case TA_AES_ECB: |
| 119 | algo = TEE_ALG_AES_ECB_NOPAD; |
| 120 | use_iv = 0; |
| 121 | break; |
| 122 | case TA_AES_CBC: |
| 123 | algo = TEE_ALG_AES_CBC_NOPAD; |
| 124 | use_iv = 1; |
| 125 | break; |
| 126 | case TA_AES_CTR: |
| 127 | algo = TEE_ALG_AES_CTR; |
| 128 | use_iv = 1; |
| 129 | break; |
| 130 | case TA_AES_XTS: |
| 131 | algo = TEE_ALG_AES_XTS; |
| 132 | use_iv = 1; |
| 133 | op_keysize *= 2; |
| 134 | break; |
| 135 | default: |
| 136 | return TEE_ERROR_BAD_PARAMETERS; |
| 137 | } |
| 138 | |
| 139 | cmd_clean_res(); |
| 140 | |
| 141 | res = TEE_AllocateOperation(&crypto_op, algo, mode, op_keysize); |
| 142 | CHECK(res, "TEE_AllocateOperation", return res;); |
| 143 | |
| 144 | res = TEE_AllocateTransientObject(TEE_TYPE_AES, keysize, &hkey); |
| 145 | CHECK(res, "TEE_AllocateTransientObject", return res;); |
| 146 | |
| 147 | attr.attributeID = TEE_ATTR_SECRET_VALUE; |
| 148 | attr.content.ref.buffer = aes_key; |
| 149 | attr.content.ref.length = keysize / 8; |
| 150 | |
| 151 | res = TEE_PopulateTransientObject(hkey, &attr, 1); |
| 152 | CHECK(res, "TEE_PopulateTransientObject", return res;); |
| 153 | |
| 154 | if (algo == TEE_ALG_AES_XTS) { |
| 155 | res = TEE_AllocateTransientObject(TEE_TYPE_AES, keysize, |
| 156 | &hkey2); |
| 157 | CHECK(res, "TEE_AllocateTransientObject", return res;); |
| 158 | |
| 159 | attr.content.ref.buffer = aes_key2; |
| 160 | |
| 161 | res = TEE_PopulateTransientObject(hkey2, &attr, 1); |
| 162 | CHECK(res, "TEE_PopulateTransientObject", return res;); |
| 163 | |
| 164 | res = TEE_SetOperationKey2(crypto_op, hkey, hkey2); |
| 165 | CHECK(res, "TEE_SetOperationKey2", return res;); |
| 166 | |
| 167 | TEE_FreeTransientObject(hkey2); |
| 168 | } else { |
| 169 | res = TEE_SetOperationKey(crypto_op, hkey); |
| 170 | CHECK(res, "TEE_SetOperationKey", return res;); |
| 171 | } |
| 172 | |
| 173 | TEE_FreeTransientObject(hkey); |
| 174 | |
| 175 | if (use_iv) |
| 176 | TEE_CipherInit(crypto_op, iv, sizeof(iv)); |
| 177 | else |
| 178 | TEE_CipherInit(crypto_op, NULL, 0); |
| 179 | |
| 180 | return TEE_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | void cmd_clean_res(void) |
| 184 | { |
| 185 | if (crypto_op) |
| 186 | TEE_FreeOperation(crypto_op); |
| 187 | } |