blob: eda486fd7c8c20242be088a9480cb07706d768d8 [file] [log] [blame]
Etienne Carrierea8118d82017-11-08 15:37:04 +01001/*
2 * Copyright (c) 2017, 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 <err.h>
29#include <stdio.h>
30#include <string.h>
31
32/* OP-TEE TEE client API (built by optee_client) */
33#include <tee_client_api.h>
34
35/* To the the UUID (found the the TA's h-file(s)) */
36#include <aes_ta.h>
37
38#define AES_TEST_BUFFER_SIZE 4096
39#define AES_TEST_KEY_SIZE 16
40
41#define DECODE 0
42#define ENCODE 1
43
44/* TEE resources */
45struct test_ctx {
46 TEEC_Context ctx;
47 TEEC_Session sess;
48};
49
50void prepare_tee_session(struct test_ctx *ctx)
51{
52 TEEC_UUID uuid = TA_AES_UUID;
53 uint32_t origin;
54 TEEC_Result res;
55
56 /* Initialize a context connecting us to the TEE */
57 res = TEEC_InitializeContext(NULL, &ctx->ctx);
58 if (res != TEEC_SUCCESS)
59 errx(1, "TEEC_InitializeContext failed with code 0x%x", res);
60
61 /* Open a session with the TA */
62 res = TEEC_OpenSession(&ctx->ctx, &ctx->sess, &uuid,
63 TEEC_LOGIN_PUBLIC, NULL, NULL, &origin);
64 if (res != TEEC_SUCCESS)
65 errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
66 res, origin);
67}
68
69void terminate_tee_session(struct test_ctx *ctx)
70{
71 TEEC_CloseSession(&ctx->sess);
72 TEEC_FinalizeContext(&ctx->ctx);
73}
74
75void prepare_aes(struct test_ctx *ctx, int encode)
76{
77 TEEC_Operation op;
78 uint32_t origin;
79 TEEC_Result res;
80
81 memset(&op, 0, sizeof(op));
82 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
83 TEEC_VALUE_INPUT,
84 TEEC_VALUE_INPUT,
85 TEEC_NONE);
86
87 op.params[0].value.a = TA_AES_ALGO_CTR;
88 op.params[1].value.a = TA_AES_SIZE_128BIT;
89 op.params[2].value.a = encode ? TA_AES_MODE_ENCODE :
90 TA_AES_MODE_DECODE;
91
92 res = TEEC_InvokeCommand(&ctx->sess, TA_AES_CMD_PREPARE,
93 &op, &origin);
94 if (res != TEEC_SUCCESS)
95 errx(1, "TEEC_InvokeCommand(PREPARE) failed 0x%x origin 0x%x",
96 res, origin);
97}
98
99void set_key(struct test_ctx *ctx, char *key, size_t key_sz)
100{
101 TEEC_Operation op;
102 uint32_t origin;
103 TEEC_Result res;
104
105 memset(&op, 0, sizeof(op));
106 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
107 TEEC_NONE, TEEC_NONE, TEEC_NONE);
108
109 op.params[0].tmpref.buffer = key;
110 op.params[0].tmpref.size = key_sz;
111
112 res = TEEC_InvokeCommand(&ctx->sess, TA_AES_CMD_SET_KEY,
113 &op, &origin);
114 if (res != TEEC_SUCCESS)
115 errx(1, "TEEC_InvokeCommand(SET_KEY) failed 0x%x origin 0x%x",
116 res, origin);
117}
118
119void set_iv(struct test_ctx *ctx, char *iv, size_t iv_sz)
120{
121 TEEC_Operation op;
122 uint32_t origin;
123 TEEC_Result res;
124
125 memset(&op, 0, sizeof(op));
126 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
127 TEEC_NONE, TEEC_NONE, TEEC_NONE);
128 op.params[0].tmpref.buffer = iv;
129 op.params[0].tmpref.size = iv_sz;
130
131 res = TEEC_InvokeCommand(&ctx->sess, TA_AES_CMD_SET_IV,
132 &op, &origin);
133 if (res != TEEC_SUCCESS)
134 errx(1, "TEEC_InvokeCommand(SET_IV) failed 0x%x origin 0x%x",
135 res, origin);
136}
137
138void cipher_buffer(struct test_ctx *ctx, char *in, char *out, size_t sz)
139{
140 TEEC_Operation op;
141 uint32_t origin;
142 TEEC_Result res;
143
144 memset(&op, 0, sizeof(op));
145 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
146 TEEC_MEMREF_TEMP_OUTPUT,
147 TEEC_NONE, TEEC_NONE);
148 op.params[0].tmpref.buffer = in;
149 op.params[0].tmpref.size = sz;
150 op.params[1].tmpref.buffer = out;
151 op.params[1].tmpref.size = sz;
152
153 res = TEEC_InvokeCommand(&ctx->sess, TA_AES_CMD_CIPHER,
154 &op, &origin);
155 if (res != TEEC_SUCCESS)
156 errx(1, "TEEC_InvokeCommand(CIPHER) failed 0x%x origin 0x%x",
157 res, origin);
158}
159
Victor Chongfdee74d2018-07-10 07:52:24 +0100160int main(void)
Etienne Carrierea8118d82017-11-08 15:37:04 +0100161{
162 struct test_ctx ctx;
163 char key[AES_TEST_KEY_SIZE];
164 char iv[AES_TEST_KEY_SIZE];
165 char clear[AES_TEST_BUFFER_SIZE];
166 char ciph[AES_TEST_BUFFER_SIZE];
167 char temp[AES_TEST_BUFFER_SIZE];
168
169 printf("Prepare session with the TA\n");
170 prepare_tee_session(&ctx);
171
172 printf("Prepare encode operation\n");
173 prepare_aes(&ctx, ENCODE);
174
175 printf("Load key in TA\n");
176 memset(key, 0xa5, sizeof(key)); /* Load some dummy value */
177 set_key(&ctx, key, AES_TEST_KEY_SIZE);
178
179 printf("Reset ciphering operation in TA (provides the initial vector)\n");
180 memset(iv, 0, sizeof(iv)); /* Load some dummy value */
181 set_iv(&ctx, iv, AES_TEST_KEY_SIZE);
182
183 printf("Encore buffer from TA\n");
184 memset(clear, 0x5a, sizeof(clear)); /* Load some dummy value */
185 cipher_buffer(&ctx, clear, ciph, AES_TEST_BUFFER_SIZE);
186
187 printf("Prepare decode operation\n");
188 prepare_aes(&ctx, DECODE);
189
190 printf("Load key in TA\n");
191 memset(key, 0xa5, sizeof(key)); /* Load some dummy value */
192 set_key(&ctx, key, AES_TEST_KEY_SIZE);
193
194 printf("Reset ciphering operation in TA (provides the initial vector)\n");
195 memset(iv, 0, sizeof(iv)); /* Load some dummy value */
196 set_iv(&ctx, iv, AES_TEST_KEY_SIZE);
197
198 printf("Decode buffer from TA\n");
199 memset(clear, 0x5a, sizeof(clear)); /* Load some dummy value */
200 cipher_buffer(&ctx, ciph, temp, AES_TEST_BUFFER_SIZE);
201
202 /* Check decoded is the clear content */
203 if (memcmp(clear, temp, AES_TEST_BUFFER_SIZE))
204 printf("Clear text and decoded text differ => ERROR\n");
205 else
206 printf("Clear text and decoded text match\n");
207
208 terminate_tee_session(&ctx);
209 return 0;
210}