ToLive

캡스톤디자인2

No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
1 +void main (void) {
2 + char test_in [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3 + int test_in_len = strlen (test_in);
4 +
5 + char test_decrypted [512];
6 + int test_decrypted_len;
7 +
8 + TEEC_Result rc;
9 + TEEC_Context ctx;
10 + TEEC_Session sess;
11 + TEEC_Operation op;
12 + TEEC_SharedMemory field_in;
13 + TEEC_SharedMemory field_back;
14 + TEEC_SharedMemory dummy;
15 + TEEC_UUID uuid = FIM_TA_UUID;
16 + uint32_t err_origin;
17 +
18 + rc = TEEC_InitializeContext(NULL, &ctx);
19 + rc = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
20 + check_rc(rc, "TEEC_OpenSession", &err_origin);
21 +
22 + field_in.buffer = NULL;
23 + field_in.size = 256;
24 + field_in.flags = TEEC_MEM_INPUT;
25 + rc = TEEC_AllocateSharedMemory(&ctx, &field_in);
26 + check_rc(rc, "TEEC_AllocateSharedMemory for field_in", NULL);
27 +
28 + field_back.buffer = NULL;
29 + field_back.size = 256;
30 + field_back.flags = TEEC_MEM_OUTPUT;
31 + rc = TEEC_AllocateSharedMemory(&ctx, &field_back);
32 + check_rc(rc, "TEEC_AllocateSharedMemory for field_back", NULL);
33 +
34 + dummy.buffer = NULL;
35 + dummy.size = 1;
36 + dummy.flags = TEEC_MEM_INPUT;
37 + rc = TEEC_AllocateSharedMemory(&ctx, &dummy);
38 + check_rc(rc, "TEEC_AllocateSharedMemory for dummy parameter", NULL);
39 +
40 + /* Clear the TEEC_Operation struct */
41 + memset(&op, 0, sizeof(op));
42 +
43 + op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_WHOLE, TEEC_MEMREF_WHOLE,
44 + TEEC_MEMREF_WHOLE, TEEC_VALUE_OUTPUT);
45 + op.params[0].memref.parent = &field_in;
46 + op.params[1].memref.parent = &field_back;
47 + op.params[2].memref.parent = &dummy;
48 + op.params[3].value.a = 0;
49 +
50 + memcpy(field_in.buffer, test_in, test_in_len);
51 + field_in.size = test_in_len;
52 + rc = TEEC_InvokeCommand(&sess, TEST_ENCRYPT_IN_TA, &op, &err_origin);
53 + decrypt_using_public_key (CA_public_key_copy, (char *)field_back.buffer, field_back.size, test_decrypted, &test_decrypted_len);
54 + printf ("In string: %s\n", test_in);
55 + printf ("Test in len: %i\n", test_in_len);
56 + printf ("Encryted value: %s\n", (char *) field_back.buffer);
57 + printf ("Encryted len: %i\n", (int) field_back.size);
58 + printf ("Decrypted value: %s\n", test_decrypted);
59 + printf ("Decrypted len: %i\n", test_decrypted_len);
60 +}
61 +
62 +char CA_public_key_copy [] =
63 + "-----BEGIN PUBLIC KEY-----\n"
64 + "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAL5c51/v1osjr5+lRPykmpQKyGdXMG0g\n"
65 + "S6Du1l8Hm0qYXc+azq6qqZvr39zeufw/VLKTfeKeKVJX1D28TImn6cUCAwEAAQ==\n"
66 + "-----END PUBLIC KEY-----\n";
67 +
68 +
69 +
70 +BOOLEAN decrypt_using_public_key (char * public_key, char * in, int in_len, char * out, int * out_len) {
71 +
72 + RSA * rsa = createRSA ((unsigned char *) public_key, 1);
73 + *out_len = RSA_public_decrypt (in_len, (unsigned char *)in, (unsigned char *) out, rsa, RSA_PKCS1_PADDING);
74 +
75 + if (*out_len == -1)
76 + return FALSE;
77 + else
78 + return TRUE;
79 +
80 +}
81 +
82 +
83 +RSA *createRSA(unsigned char *key, int public) {
84 + RSA *rsa = NULL;
85 + BIO *keybio;
86 + keybio = BIO_new_mem_buf(key, -1);
87 + if (keybio == NULL) {
88 + printf("Failed to create key BIO");
89 + return 0;
90 + }
91 + if (public) {
92 + rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
93 + } else {
94 + rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
95 + }
96 + if (rsa == NULL) {
97 + printf("Failed to create RSA");
98 + }
99 + return rsa;
100 +}
...\ No newline at end of file ...\ No newline at end of file
1 +/*****************************************************************************
2 +** Copyright (C) 2013 Secure Systems Group. **
3 +** Copyright (C) 2015 Intel Corporation. **
4 +** **
5 +** Licensed under the Apache License, Version 2.0 (the "License"); **
6 +** you may not use this file except in compliance with the License. **
7 +** You may obtain a copy of the License at **
8 +** **
9 +** http://www.apache.org/licenses/LICENSE-2.0 **
10 +** **
11 +** Unless required by applicable law or agreed to in writing, software **
12 +** distributed under the License is distributed on an "AS IS" BASIS, **
13 +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. **
14 +** See the License for the specific language governing permissions and **
15 +** limitations under the License. **
16 +*****************************************************************************/
17 +
18 +/* Extreme simply smoke tests. */
19 +
20 +#include "crypto_test.h"
21 +#include "../include/tee_internal_api.h"
22 +
23 +/* Start Open-TEE spesifics. NOT GP Compliant. For debugin sake */
24 +#include "../include/tee_logging.h"
25 +#define PRI_STR(str) OT_LOG1(LOG_DEBUG, str);
26 +#define PRI(str, ...) OT_LOG1(LOG_DEBUG, "%s : " str "\n", __func__, ##__VA_ARGS__);
27 +#define PRI_OK(str, ...) OT_LOG1(LOG_DEBUG, " [OK] : %s : " str "\n", __func__, ##__VA_ARGS__);
28 +#define PRI_YES(str, ...) OT_LOG1(LOG_DEBUG, " YES? : %s : " str "\n", __func__, ##__VA_ARGS__);
29 +#define PRI_FAIL(str, ...) OT_LOG1(LOG_DEBUG, "FAIL : %s : " str "\n", __func__, ##__VA_ARGS__);
30 +#define PRI_ABORT(str, ...) OT_LOG1(LOG_DEBUG, "ABORT!: %s : " str "\n", __func__, ##__VA_ARGS__);
31 +/* End Open-TEE spesifics */
32 +
33 +#define SIZE_OF_VEC(vec) (sizeof(vec) - 1)
34 +#define MAX_HASH_OUTPUT_LENGTH 64 /* sha512 */
35 +
36 +/* sha256 (NIST) */
37 +uint8_t sha256msg[] = "\x45\x11\x01\x25\x0e\xc6\xf2\x66\x52\x24\x9d\x59\xdc\x97\x4b\x73"
38 + "\x61\xd5\x71\xa8\x10\x1c\xdf\xd3\x6a\xba\x3b\x58\x54\xd3\xae\x08"
39 + "\x6b\x5f\xdd\x45\x97\x72\x1b\x66\xe3\xc0\xdc\x5d\x8c\x60\x6d\x96"
40 + "\x57\xd0\xe3\x23\x28\x3a\x52\x17\xd1\xf5\x3f\x2f\x28\x4f\x57\xb8"
41 + "\x5c\x8a\x61\xac\x89\x24\x71\x1f\x89\x5c\x5e\xd9\x0e\xf1\x77\x45"
42 + "\xed\x2d\x72\x8a\xbd\x22\xa5\xf7\xa1\x34\x79\xa4\x62\xd7\x1b\x56"
43 + "\xc1\x9a\x74\xa4\x0b\x65\x5c\x58\xed\xfe\x0a\x18\x8a\xd2\xcf\x46"
44 + "\xcb\xf3\x05\x24\xf6\x5d\x42\x3c\x83\x7d\xd1\xff\x2b\xf4\x62\xac"
45 + "\x41\x98\x00\x73\x45\xbb\x44\xdb\xb7\xb1\xc8\x61\x29\x8c\xdf\x61"
46 + "\x98\x2a\x83\x3a\xfc\x72\x8f\xae\x1e\xda\x2f\x87\xaa\x2c\x94\x80"
47 + "\x85\x8b\xec";
48 +
49 +uint8_t sha256hash[] = "\x3c\x59\x3a\xa5\x39\xfd\xcd\xae\x51\x6c\xdf\x2f\x15\x00\x0f\x66"
50 + "\x34\x18\x5c\x88\xf5\x05\xb3\x97\x75\xfb\x9a\xb1\x37\xa1\x0a\xa2";
51 +
52 +/* RSA (NIST) */
53 +uint8_t modulus[] = "\xa8\xd6\x8a\xcd\x41\x3c\x5e\x19\x5d\x5e\xf0\x4e\x1b\x4f\xaa\xf2"
54 + "\x42\x36\x5c\xb4\x50\x19\x67\x55\xe9\x2e\x12\x15\xba\x59\x80\x2a"
55 + "\xaf\xba\xdb\xf2\x56\x4d\xd5\x50\x95\x6a\xbb\x54\xf8\xb1\xc9\x17"
56 + "\x84\x4e\x5f\x36\x19\x5d\x10\x88\xc6\x00\xe0\x7c\xad\xa5\xc0\x80"
57 + "\xed\xe6\x79\xf5\x0b\x3d\xe3\x2c\xf4\x02\x6e\x51\x45\x42\x49\x5c"
58 + "\x54\xb1\x90\x37\x68\x79\x1a\xae\x9e\x36\xf0\x82\xcd\x38\xe9\x41"
59 + "\xad\xa8\x9b\xae\xca\xda\x61\xab\x0d\xd3\x7a\xd5\x36\xbc\xb0\xa0"
60 + "\x94\x62\x71\x59\x48\x36\xe9\x2a\xb5\x51\x73\x01\xd4\x51\x76\xb5";
61 +
62 +uint8_t public_exp[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
63 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
64 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
65 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
67 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
68 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
69 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03";
70 +
71 +uint8_t private_exp[] = "\x1c\x23\xc1\xcc\xe0\x34\xba\x59\x8f\x8f\xd2\xb7\xaf\x37\xf1\xd3"
72 + "\x0b\x09\x0f\x73\x62\xae\xe6\x8e\x51\x87\xad\xae\x49\xb9\x95\x5c"
73 + "\x72\x9f\x24\xa8\x63\xb7\xa3\x8d\x6e\x3c\x74\x8e\x29\x72\xf6\xd9"
74 + "\x40\xb7\xba\x89\x04\x3a\x2d\x6c\x21\x00\x25\x6a\x1c\xf0\xf5\x6a"
75 + "\x8c\xd3\x5f\xc6\xee\x20\x52\x44\x87\x66\x42\xf6\xf9\xc3\x82\x0a"
76 + "\x3d\x9d\x2c\x89\x21\xdf\x7d\x82\xaa\xad\xca\xf2\xd7\x33\x4d\x39"
77 + "\x89\x31\xdd\xbb\xa5\x53\x19\x0b\x3a\x41\x60\x99\xf3\xaa\x07\xfd"
78 + "\x5b\x26\x21\x46\x45\xa8\x28\x41\x9e\x12\x2c\xfb\x85\x7a\xd7\x3b";
79 +
80 +uint8_t rsa_msg[] = "\xd7\x38\x29\x49\x7c\xdd\xbe\x41\xb7\x05\xfa\xac\x50\xe7\x89\x9f"
81 + "\xdb\x5a\x38\xbf\x3a\x45\x9e\x53\x63\x57\x02\x9e\x64\xf8\x79\x6b"
82 + "\xa4\x7f\x4f\xe9\x6b\xa5\xa8\xb9\xa4\x39\x67\x46\xe2\x16\x4f\x55"
83 + "\xa2\x53\x68\xdd\xd0\xb9\xa5\x18\x8c\x7a\xc3\xda\x2d\x1f\x74\x22"
84 + "\x86\xc3\xbd\xee\x69\x7f\x9d\x54\x6a\x25\xef\xcf\xe5\x31\x91\xd7"
85 + "\x43\xfc\xc6\xb4\x78\x33\xd9\x93\xd0\x88\x04\xda\xec\xa7\x8f\xb9"
86 + "\x07\x6c\x3c\x01\x7f\x53\xe3\x3a\x90\x30\x5a\xf0\x62\x20\x97\x4d"
87 + "\x46\xbf\x19\xed\x3c\x9b\x84\xed\xba\xe9\x8b\x45\xa8\x77\x12\x58";
88 +
89 +uint8_t rsa_sig[] = "\x17\x50\x15\xbd\xa5\x0a\xbe\x0f\xa7\xd3\x9a\x83\x53\x88\x5c\xa0"
90 + "\x1b\xe3\xa7\xe7\xfc\xc5\x50\x45\x74\x41\x11\x36\x2e\xe1\x91\x44"
91 + "\x73\xa4\x8d\xc5\x37\xd9\x56\x29\x4b\x9e\x20\xa1\xef\x66\x1d\x58"
92 + "\x53\x7a\xcd\xc8\xde\x90\x8f\xa0\x50\x63\x0f\xcc\x27\x2e\x6d\x00"
93 + "\x10\x45\xe6\xfd\xee\xd2\xd1\x05\x31\xc8\x60\x33\x34\xc2\xe8\xdb"
94 + "\x39\xe7\x3e\x6d\x96\x65\xee\x13\x43\xf9\xe4\x19\x83\x02\xd2\x20"
95 + "\x1b\x44\xe8\xe8\xd0\x6b\x3e\xf4\x9c\xee\x61\x97\x58\x21\x63\xa8"
96 + "\x49\x00\x89\xca\x65\x4c\x00\x12\xfc\xe1\xba\x65\x11\x08\x97\x50";
97 +
98 +static int calc_digest(algorithm_Identifier hash_alg,
99 + void *msg,
100 + uint32_t msg_len,
101 + void *hash,
102 + uint32_t *hash_len)
103 +{
104 + TEE_OperationHandle operation = (TEE_OperationHandle)NULL;
105 + TEE_Result ret;
106 +
107 + ret = TEE_AllocateOperation(&operation, hash_alg, TEE_MODE_DIGEST, 0);
108 + if (ret != TEE_SUCCESS) {
109 + PRI_FAIL("Failed allocate digest operation");
110 + return 1;
111 + }
112 +
113 + ret = TEE_DigestDoFinal(operation, msg, msg_len, hash, hash_len);
114 + TEE_FreeOperation(operation);
115 +
116 + if (ret != TEE_SUCCESS) {
117 + PRI_FAIL("Final failed");
118 + return 1;
119 + }
120 +
121 + return 0;
122 +}
123 +
124 +static uint32_t sha256_digest()
125 +{
126 + TEE_Result ret = TEE_SUCCESS;
127 + TEE_OperationHandle digest_handler = (TEE_OperationHandle)NULL;
128 + TEE_OperationHandle digest_handler_2 = (TEE_OperationHandle)NULL;
129 + void *rand_msg = NULL;
130 + void *rand_msg_2 = NULL;
131 + char hash[64] = {0};
132 + char hash_2[64] = {0};
133 + uint32_t rand_msg_len = 1000;
134 + uint32_t hash_len = 64;
135 + uint32_t hash_len_2 = 64;
136 + uint32_t fn_ret = 1; /* Initialized error return */
137 +
138 + rand_msg = TEE_Malloc(rand_msg_len, 0);
139 + rand_msg_2 = TEE_Malloc(rand_msg_len, 0);
140 + if (rand_msg == NULL || rand_msg_2 == NULL) {
141 + PRI_FAIL("Out of memory");
142 + goto err;
143 + }
144 +
145 + TEE_GenerateRandom(rand_msg, rand_msg_len);
146 + TEE_MemMove(rand_msg_2, rand_msg, rand_msg_len);
147 +
148 + ret = TEE_AllocateOperation(&digest_handler, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
149 + if (ret != TEE_SUCCESS) {
150 + PRI_FAIL("Cant alloc first handler");
151 + goto err;
152 + }
153 +
154 + ret = TEE_AllocateOperation(&digest_handler_2, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
155 + if (ret != TEE_SUCCESS) {
156 + PRI_FAIL("Cant alloc second handler");
157 + goto err;
158 + }
159 +
160 + TEE_DigestUpdate(digest_handler, rand_msg, rand_msg_len);
161 + TEE_DigestUpdate(digest_handler, rand_msg, rand_msg_len);
162 +
163 + TEE_DigestUpdate(digest_handler_2, rand_msg_2, rand_msg_len);
164 + TEE_DigestUpdate(digest_handler_2, rand_msg_2, rand_msg_len);
165 +
166 + ret = TEE_DigestDoFinal(digest_handler, NULL, 0, hash, &hash_len);
167 + if (ret != TEE_SUCCESS) {
168 + PRI_FAIL("Failed final first");
169 + goto err;
170 + }
171 +
172 + ret = TEE_DigestDoFinal(digest_handler_2, NULL, 0, hash_2, &hash_len_2);
173 + if (ret != TEE_SUCCESS) {
174 + PRI_FAIL("Failed final second");
175 + goto err;
176 + }
177 +
178 + if (hash_len_2 != hash_len) {
179 + PRI_FAIL("Length bhould be same");
180 + goto err;
181 + }
182 +
183 + if (TEE_MemCompare(hash, hash_2, hash_len_2)) {
184 + PRI_FAIL("Hashes should be same");
185 + goto err;
186 + }
187 +
188 + fn_ret = 0; /* OK */
189 +
190 +err:
191 + TEE_FreeOperation(digest_handler);
192 + TEE_FreeOperation(digest_handler_2);
193 + TEE_Free(rand_msg);
194 + TEE_Free(rand_msg_2);
195 +
196 + if (fn_ret == 0)
197 + PRI_OK("-");
198 +
199 + return fn_ret;
200 +}
201 +
202 +static uint32_t sha256_digest_nist()
203 +{
204 + TEE_Result ret = TEE_SUCCESS;
205 + TEE_OperationHandle sha256_operation = (TEE_OperationHandle)NULL;
206 + char hash[64] = {0};
207 + uint32_t hash_len = MAX_HASH_OUTPUT_LENGTH, fn_ret = 1; /* Initialized error return */
208 +
209 + ret = TEE_AllocateOperation(&sha256_operation, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
210 + if (ret != TEE_SUCCESS) {
211 + PRI_FAIL("Cant alloc sha256 handler");
212 + goto err;
213 + }
214 +
215 + ret = TEE_DigestDoFinal(sha256_operation,
216 + sha256msg, SIZE_OF_VEC(sha256msg), hash, &hash_len);
217 + if (ret != TEE_SUCCESS) {
218 + PRI_FAIL("Final failed");
219 + goto err;
220 + }
221 +
222 + if (hash_len != SIZE_OF_VEC(sha256hash)) {
223 + PRI_FAIL("Length bhould be same");
224 + goto err;
225 + }
226 +
227 + if (TEE_MemCompare(hash, sha256hash, hash_len)) {
228 + PRI_FAIL("Hashes should be same");
229 + goto err;
230 + }
231 +
232 + fn_ret = 0; /* OK */
233 +
234 +err:
235 + TEE_FreeOperation(sha256_operation);
236 +
237 + if (fn_ret == 0)
238 + PRI_OK("-");
239 +
240 + return fn_ret;
241 +}
242 +
243 +static int warp_sym_op(TEE_ObjectHandle key,
244 + TEE_OperationMode mode,
245 + void *IV,
246 + size_t IV_len,
247 + uint32_t alg,
248 + void *in_chunk,
249 + size_t in_chunk_len,
250 + void *out_chunk,
251 + size_t *out_chunk_len)
252 +{
253 + TEE_Result ret = TEE_SUCCESS;
254 + TEE_OperationHandle handle = (TEE_OperationHandle)NULL;
255 + uint32_t write_bytes = 0;
256 + uint32_t total_write_bytes = 0;
257 + TEE_ObjectInfo info;
258 +
259 + TEE_GetObjectInfo(key, &info);
260 +
261 + ret = TEE_AllocateOperation(&handle, alg, mode, info.maxObjectSize);
262 + if (ret != TEE_SUCCESS) {
263 + PRI_FAIL("Failed to alloc operation handle : 0x%x", ret);
264 + goto err;
265 + }
266 +
267 + ret = TEE_SetOperationKey(handle, key);
268 + if (ret != TEE_SUCCESS) {
269 + PRI_FAIL("Failed to set key : 0x%x", ret);
270 + goto err;
271 + }
272 +
273 + TEE_CipherInit(handle, IV, IV_len);
274 +
275 + write_bytes = *out_chunk_len;
276 +
277 + ret = TEE_CipherUpdate(handle, in_chunk, in_chunk_len, out_chunk, &write_bytes);
278 + if (ret != TEE_SUCCESS) {
279 + PRI_FAIL("Updated failure : 0x%x", ret);
280 + goto err;
281 + }
282 +
283 + total_write_bytes += write_bytes;
284 + write_bytes = *out_chunk_len - total_write_bytes;
285 +
286 + ret = TEE_CipherDoFinal(handle, NULL, 0,
287 + (unsigned char *)out_chunk + total_write_bytes, &write_bytes);
288 + if (ret != TEE_SUCCESS) {
289 + PRI_FAIL("Do final failure : 0x%x", ret);
290 + goto err;
291 + }
292 +
293 + *out_chunk_len = total_write_bytes + write_bytes;
294 + TEE_FreeOperation(handle);
295 + return 0;
296 +err:
297 + TEE_FreeOperation(handle);
298 + return 1;
299 +}
300 +
301 +static uint32_t aes_256_cbc_enc_dec()
302 +{
303 + TEE_Result ret = TEE_SUCCESS;
304 + size_t key_size = 256;
305 + uint32_t obj_type = TEE_TYPE_AES;
306 + uint32_t alg = TEE_ALG_AES_CBC_NOPAD;
307 + TEE_ObjectHandle key = (TEE_ObjectHandle)NULL;
308 + char *plain_msg = "TEST";
309 + uint32_t fn_ret = 1; /* Initialized error return */
310 +
311 + size_t plain_len = 32;
312 + size_t cipher_len = 32;
313 + size_t dec_plain_len = plain_len;
314 + size_t IVlen = 16;
315 +
316 + void *plain = NULL;
317 + void *cipher = NULL;
318 + void *dec_plain = NULL;
319 + void *IV = NULL;
320 +
321 + IV = TEE_Malloc(IVlen, 0);
322 + plain = TEE_Malloc(plain_len, 0);
323 + cipher = TEE_Malloc(cipher_len, 0);
324 + dec_plain = TEE_Malloc(dec_plain_len, 0);
325 + if (!IV || !plain || !cipher || !dec_plain) {
326 + PRI_FAIL("Out of memory");
327 + goto err;
328 + }
329 + TEE_GenerateRandom(IV, IVlen);
330 + TEE_MemMove(plain, plain_msg, 5);
331 +
332 + /* Alloc and gen keys */
333 + ret = TEE_AllocateTransientObject(obj_type, key_size, &key);
334 + if (ret != TEE_SUCCESS) {
335 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
336 + goto err;
337 + }
338 +
339 + ret = TEE_GenerateKey(key, key_size, (TEE_Attribute *)NULL, 0);
340 + if (ret != TEE_SUCCESS) {
341 + PRI_FAIL("Generate key failure : 0x%x", ret);
342 + goto err;
343 + }
344 +
345 + if (warp_sym_op(key, TEE_MODE_ENCRYPT, IV, IVlen, alg,
346 + plain, plain_len, cipher, &cipher_len))
347 + goto err;
348 +
349 + if (warp_sym_op(key, TEE_MODE_DECRYPT, IV, IVlen, alg,
350 + cipher, cipher_len, dec_plain, &dec_plain_len))
351 + goto err;
352 +
353 + if (TEE_MemCompare(dec_plain, plain, dec_plain_len)) {
354 + PRI_FAIL("Plain text is not matching");
355 + goto err;
356 + }
357 +
358 + fn_ret = 0; /* OK */
359 +err:
360 + TEE_FreeTransientObject(key);
361 + TEE_Free(plain);
362 + TEE_Free(IV);
363 + TEE_Free(cipher);
364 + TEE_Free(dec_plain);
365 +
366 + if (fn_ret == 0)
367 + PRI_OK("-");
368 +
369 + return fn_ret;
370 +}
371 +
372 +static uint32_t aes_128_cbc_enc_dec()
373 +{
374 + TEE_Result ret = TEE_SUCCESS;
375 + size_t key_size = 128;
376 + uint32_t obj_type = TEE_TYPE_AES;
377 + uint32_t alg = TEE_ALG_AES_CBC_NOPAD;
378 + TEE_ObjectHandle key = (TEE_ObjectHandle)NULL;
379 + char *plain_msg = "TEST";
380 + uint32_t fn_ret = 1; /* Initialized error return */
381 +
382 + size_t plain_len = 32;
383 + size_t cipher_len = 32;
384 + size_t dec_plain_len = plain_len;
385 + size_t IVlen = 16;
386 +
387 + void *plain = NULL;
388 + void *cipher = NULL;
389 + void *dec_plain = NULL;
390 + void *IV = NULL;
391 +
392 + IV = TEE_Malloc(IVlen, 0);
393 + plain = TEE_Malloc(plain_len, 0);
394 + cipher = TEE_Malloc(cipher_len, 0);
395 + dec_plain = TEE_Malloc(dec_plain_len, 0);
396 + if (!IV || !plain || !cipher || !dec_plain) {
397 + PRI_FAIL("Out of memory");
398 + goto err;
399 + }
400 + TEE_GenerateRandom(IV, IVlen);
401 + TEE_MemMove(plain, plain_msg, 5);
402 +
403 + /* Alloc and gen keys */
404 + ret = TEE_AllocateTransientObject(obj_type, key_size, &key);
405 + if (ret != TEE_SUCCESS) {
406 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
407 + goto err;
408 + }
409 +
410 + ret = TEE_GenerateKey(key, key_size, (TEE_Attribute *)NULL, 0);
411 + if (ret != TEE_SUCCESS) {
412 + PRI_FAIL("Generate key failure : 0x%x", ret);
413 + goto err;
414 + }
415 +
416 + if (warp_sym_op(key, TEE_MODE_ENCRYPT, IV, IVlen, alg,
417 + plain, plain_len, cipher, &cipher_len))
418 + goto err;
419 +
420 + if (warp_sym_op(key, TEE_MODE_DECRYPT, IV, IVlen, alg,
421 + cipher, cipher_len, dec_plain, &dec_plain_len))
422 + goto err;
423 +
424 + if (TEE_MemCompare(dec_plain, plain, dec_plain_len)) {
425 + PRI_FAIL("Plain text is not matching");
426 + goto err;
427 + }
428 +
429 + fn_ret = 0; /* OK */
430 +err:
431 + TEE_FreeTransientObject(key);
432 + TEE_Free(plain);
433 + TEE_Free(IV);
434 + TEE_Free(cipher);
435 + TEE_Free(dec_plain);
436 +
437 + if (fn_ret == 0)
438 + PRI_OK("-");
439 +
440 + return fn_ret;
441 +}
442 +
443 +static int warp_asym_op(TEE_ObjectHandle key,
444 + TEE_OperationMode mode,
445 + uint32_t alg,
446 + TEE_Attribute *params,
447 + uint32_t paramCount,
448 + void *in_chunk,
449 + uint32_t in_chunk_len,
450 + void *out_chunk,
451 + uint32_t *out_chunk_len)
452 +{
453 + TEE_Result ret = TEE_SUCCESS;
454 + TEE_OperationHandle handle = (TEE_OperationHandle)NULL;
455 + TEE_ObjectInfo info;
456 +
457 + TEE_GetObjectInfo(key, &info);
458 +
459 + ret = TEE_AllocateOperation(&handle, alg, mode, info.maxObjectSize);
460 + if (ret != TEE_SUCCESS) {
461 + PRI_FAIL("Failed to alloc operation handle : 0x%x", ret);
462 + goto err;
463 + }
464 +
465 + ret = TEE_SetOperationKey(handle, key);
466 + if (ret != TEE_SUCCESS) {
467 + PRI_FAIL("Failed to set key : 0x%x", ret);
468 + goto err;
469 + }
470 +
471 + if (mode == TEE_MODE_SIGN) {
472 +
473 + ret = TEE_AsymmetricSignDigest(handle, params, paramCount,
474 + in_chunk, in_chunk_len, out_chunk, out_chunk_len);
475 + if (ret != TEE_SUCCESS) {
476 + PRI_FAIL("Sign failed : 0x%x", ret);
477 + goto err;
478 + }
479 +
480 + } else if (mode == TEE_MODE_VERIFY) {
481 +
482 + ret = TEE_AsymmetricVerifyDigest(handle, params, paramCount,
483 + in_chunk, in_chunk_len, out_chunk, *out_chunk_len);
484 + if (ret == TEE_SUCCESS) {
485 + /* Do nothing */
486 + } else if (ret == TEE_ERROR_SIGNATURE_INVALID) {
487 + PRI_FAIL("Signature invalid");
488 + goto err;
489 + } else {
490 + PRI_FAIL("Verify failed : 0x%x", ret);
491 + goto err;
492 + }
493 +
494 + } else if (mode == TEE_MODE_ENCRYPT) {
495 +
496 + ret = TEE_AsymmetricEncrypt(handle, params, paramCount,
497 + in_chunk, in_chunk_len, out_chunk, out_chunk_len);
498 + if (ret != TEE_SUCCESS) {
499 + PRI_FAIL("Encrypt failed : 0x%x", ret);
500 + goto err;
501 + }
502 +
503 + } else if (mode == TEE_MODE_DECRYPT) {
504 +
505 + ret = TEE_AsymmetricDecrypt(handle, params, paramCount,
506 + in_chunk, in_chunk_len, out_chunk, out_chunk_len);
507 + if (ret != TEE_SUCCESS) {
508 + PRI_FAIL("Decrypt failed : 0x%x", ret);
509 + goto err;
510 + }
511 +
512 + } else {
513 + goto err;
514 + }
515 +
516 + TEE_FreeOperation(handle);
517 + return 0;
518 +
519 +err:
520 + TEE_FreeOperation(handle);
521 + return 1;
522 +}
523 +
524 +static uint32_t rsa_sign_nist_sha1_pkcs()
525 +{
526 + TEE_Result ret = TEE_SUCCESS;
527 + TEE_ObjectHandle object = (TEE_ObjectHandle)NULL;
528 + char hash[64] = {0}; /*sha1*/
529 + char signature[256] = {0}; /* 1024 */
530 + uint32_t signature_len = 256, hash_len = 64;
531 + TEE_Attribute rsa_attrs[3];
532 + uint32_t rsa_alg = TEE_ALG_RSASSA_PKCS1_V1_5_SHA1, fn_ret = 1; /* Init error return */;
533 +
534 + /* Modulo */
535 + rsa_attrs[0].attributeID = TEE_ATTR_RSA_MODULUS;
536 + rsa_attrs[0].content.ref.buffer = modulus;
537 + rsa_attrs[0].content.ref.length = SIZE_OF_VEC(modulus);
538 +
539 + /* Public exp */
540 + rsa_attrs[1].attributeID = TEE_ATTR_RSA_PUBLIC_EXPONENT;
541 + rsa_attrs[1].content.ref.buffer = public_exp;
542 + rsa_attrs[1].content.ref.length = SIZE_OF_VEC(public_exp);
543 +
544 + /* Private exp */
545 + rsa_attrs[2].attributeID = TEE_ATTR_RSA_PRIVATE_EXPONENT;
546 + rsa_attrs[2].content.ref.buffer = private_exp;
547 + rsa_attrs[2].content.ref.length = SIZE_OF_VEC(private_exp);
548 +
549 + ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, 1024, &object);
550 + if (ret != TEE_SUCCESS) {
551 + PRI_FAIL("Cant alloc object handler");
552 + goto err;
553 + }
554 +
555 + ret = TEE_PopulateTransientObject(object, (TEE_Attribute *)&rsa_attrs, 3);
556 + if (ret != TEE_SUCCESS) {
557 + PRI_FAIL("RSA key population failed");
558 + goto err;
559 + }
560 +
561 + if (calc_digest(TEE_ALG_SHA1, rsa_msg, SIZE_OF_VEC(rsa_msg), hash, &hash_len))
562 + goto err;
563 +
564 + if (warp_asym_op(object, TEE_MODE_SIGN, rsa_alg, (TEE_Attribute *)NULL, 0,
565 + (void *)hash, hash_len, (void *)signature, &signature_len))
566 + goto err;
567 +
568 + if (SIZE_OF_VEC(rsa_sig) != signature_len) {
569 + PRI_FAIL("Signature length invalid");
570 + goto err;
571 + }
572 +
573 + if (TEE_MemCompare(rsa_sig, signature, signature_len)) {
574 + PRI_FAIL("Signature length invalid");
575 + goto err;
576 + }
577 +
578 + if (warp_asym_op(object, TEE_MODE_VERIFY, rsa_alg, (TEE_Attribute *)NULL, 0,
579 + (void *)hash, hash_len, (void *)signature, &signature_len))
580 + goto err;
581 +
582 + fn_ret = 0; /* OK */
583 +
584 +err:
585 + TEE_FreeTransientObject(object);
586 +
587 + if (fn_ret == 0)
588 + PRI_OK("-");
589 +
590 + return fn_ret;
591 +}
592 +
593 +static uint32_t ECDSA_sig_and_ver(uint32_t curve, uint32_t keysize, uint32_t alg)
594 +{
595 + TEE_Result ret;
596 + TEE_ObjectHandle ecdsa_keypair = (TEE_ObjectHandle)NULL;
597 + TEE_Attribute ecdsa_attrs[1];
598 + size_t key_size = keysize;
599 + uint32_t ecdsa_alg = alg;
600 + char *dig_msg = "TEST";
601 + uint32_t fn_ret = 1; /* Initialized error return */
602 +
603 + uint32_t dig_len = 20;
604 + /* enough for a p521 sig */
605 + uint32_t sig_len = 160;
606 +
607 + void *dig = NULL;
608 + void *sig = NULL;
609 +
610 + dig = TEE_Malloc(dig_len, 0);
611 + sig = TEE_Malloc(sig_len, 0);
612 + if (!dig || !sig) {
613 + PRI_FAIL("Out of memory");
614 + goto err;
615 + }
616 +
617 + TEE_MemMove(dig, dig_msg, 5);
618 +
619 + /* Curve */
620 + ecdsa_attrs[0].attributeID = TEE_ATTR_ECC_CURVE;
621 + ecdsa_attrs[0].content.value.a = curve;
622 + ecdsa_attrs[0].content.value.b = 0;
623 +
624 + ret = TEE_AllocateTransientObject(TEE_TYPE_ECDSA_KEYPAIR, key_size, &ecdsa_keypair);
625 + if (ret != TEE_SUCCESS) {
626 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
627 + goto err;
628 + }
629 +
630 + ret = TEE_GenerateKey(ecdsa_keypair, key_size, ecdsa_attrs, 1);
631 + if (ret != TEE_SUCCESS) {
632 + PRI_FAIL("Generate key failure : 0x%x", ret);
633 + goto err;
634 + }
635 +
636 + if (warp_asym_op(ecdsa_keypair, TEE_MODE_SIGN, ecdsa_alg, ecdsa_attrs, 1,
637 + dig, dig_len, sig, &sig_len))
638 + goto err;
639 +
640 + if (warp_asym_op(ecdsa_keypair, TEE_MODE_VERIFY, ecdsa_alg, ecdsa_attrs, 1,
641 + dig, dig_len, sig, &sig_len))
642 + goto err;
643 +
644 + fn_ret = 0; /* OK */
645 +err:
646 + TEE_FreeTransientObject(ecdsa_keypair);
647 + TEE_Free(dig);
648 + TEE_Free(sig);
649 +
650 + if (fn_ret == 0)
651 + PRI_OK("- with key size %u", keysize);
652 +
653 + return fn_ret;
654 +}
655 +
656 +static uint32_t ECDSA_set_key_and_rm_and_do_crypto(uint32_t curve, uint32_t keysize, uint32_t alg)
657 +{
658 + TEE_Result ret;
659 + TEE_ObjectHandle ecdsa_keypair = (TEE_ObjectHandle)NULL;
660 + TEE_OperationHandle sign_op = (TEE_OperationHandle)NULL,
661 + verify_op = (TEE_OperationHandle)NULL;
662 + TEE_Attribute ecdsa_attrs[1];
663 + size_t key_size = keysize;
664 + uint32_t ecdsa_alg = alg;
665 + char *dig_seed = "TEST";
666 + uint32_t dig_len = 32, sig_len = 160;
667 + char dig[32] = {0}, sig[160] = {0};
668 + uint32_t fn_ret = 1; /* Initialized error return */
669 +
670 + TEE_MemMove(dig, dig_seed, 5);
671 +
672 + /* Curve */
673 + ecdsa_attrs[0].attributeID = TEE_ATTR_ECC_CURVE;
674 + ecdsa_attrs[0].content.value.a = curve;
675 + ecdsa_attrs[0].content.value.b = 0;
676 +
677 + ret = TEE_AllocateTransientObject(TEE_TYPE_ECDSA_KEYPAIR, key_size, &ecdsa_keypair);
678 + if (ret != TEE_SUCCESS) {
679 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
680 + goto err;
681 + }
682 +
683 + ret = TEE_GenerateKey(ecdsa_keypair, key_size, ecdsa_attrs, 1);
684 + if (ret != TEE_SUCCESS) {
685 + PRI_FAIL("Generate key failure : 0x%x", ret);
686 + goto err;
687 + }
688 +
689 + ret = TEE_AllocateOperation(&sign_op, ecdsa_alg, TEE_MODE_SIGN, key_size);
690 + if (ret != TEE_SUCCESS) {
691 + PRI_FAIL("Failed to alloc sign operation handle : 0x%x", ret);
692 + goto err;
693 + }
694 +
695 + ret = TEE_AllocateOperation(&verify_op, ecdsa_alg, TEE_MODE_VERIFY, key_size);
696 + if (ret != TEE_SUCCESS) {
697 + PRI_FAIL("Failed to alloc verify operation handle : 0x%x", ret);
698 + goto err;
699 + }
700 +
701 + ret = TEE_SetOperationKey(sign_op, ecdsa_keypair);
702 + if (ret != TEE_SUCCESS) {
703 + PRI_FAIL("Failed to set sign operation key : 0x%x", ret);
704 + goto err;
705 + }
706 +
707 + ret = TEE_SetOperationKey(verify_op, ecdsa_keypair);
708 + if (ret != TEE_SUCCESS) {
709 + PRI_FAIL("Failed to set verify operation key : 0x%x", ret);
710 + goto err;
711 + }
712 +
713 + TEE_FreeTransientObject(ecdsa_keypair);
714 + ecdsa_keypair = (TEE_ObjectHandle)NULL;
715 +
716 + ret = TEE_AsymmetricSignDigest(sign_op, (TEE_Attribute *)NULL, 0,
717 + dig, dig_len, sig, &sig_len);
718 + if (ret != TEE_SUCCESS) {
719 + PRI_FAIL("Sign failed : 0x%x", ret);
720 + goto err;
721 + }
722 +
723 + ret = TEE_AsymmetricVerifyDigest(verify_op, (TEE_Attribute *)NULL, 0,
724 + dig, dig_len, sig, sig_len);
725 + if (ret == TEE_SUCCESS) {
726 + /* Do nothing */
727 + } else if (ret == TEE_ERROR_SIGNATURE_INVALID) {
728 + PRI_FAIL("Signature invalid");
729 + goto err;
730 + } else {
731 + PRI_FAIL("Verify failed : 0x%x", ret);
732 + goto err;
733 + }
734 +
735 + fn_ret = 0; /* OK */
736 +err:
737 + TEE_FreeTransientObject(ecdsa_keypair);
738 + TEE_FreeOperation(sign_op);
739 + TEE_FreeOperation(verify_op);
740 +
741 + if (fn_ret == 0)
742 + PRI_OK("- with key size: %u", keysize);
743 +
744 + return fn_ret;
745 +}
746 +
747 +static uint32_t run_ecdsa_tests()
748 +{
749 + /* run the tests for all the curves */
750 + if (ECDSA_sig_and_ver(TEE_ECC_CURVE_NIST_P192, 192, TEE_ALG_ECDSA_P192) ||
751 + ECDSA_sig_and_ver(TEE_ECC_CURVE_NIST_P224, 224, TEE_ALG_ECDSA_P224) ||
752 + ECDSA_sig_and_ver(TEE_ECC_CURVE_NIST_P256, 256, TEE_ALG_ECDSA_P256) ||
753 + ECDSA_sig_and_ver(TEE_ECC_CURVE_NIST_P384, 384, TEE_ALG_ECDSA_P384) ||
754 + ECDSA_sig_and_ver(TEE_ECC_CURVE_NIST_P521, 521, TEE_ALG_ECDSA_P521) ||
755 + ECDSA_set_key_and_rm_and_do_crypto(TEE_ECC_CURVE_NIST_P192, 192, TEE_ALG_ECDSA_P192) ||
756 + ECDSA_set_key_and_rm_and_do_crypto(TEE_ECC_CURVE_NIST_P224, 224, TEE_ALG_ECDSA_P224) ||
757 + ECDSA_set_key_and_rm_and_do_crypto(TEE_ECC_CURVE_NIST_P256, 256, TEE_ALG_ECDSA_P256) ||
758 + ECDSA_set_key_and_rm_and_do_crypto(TEE_ECC_CURVE_NIST_P384, 384, TEE_ALG_ECDSA_P384) ||
759 + ECDSA_set_key_and_rm_and_do_crypto(TEE_ECC_CURVE_NIST_P521, 521, TEE_ALG_ECDSA_P521))
760 + return TEE_ERROR_GENERIC;
761 + return TEE_SUCCESS;
762 +}
763 +
764 +static uint32_t RSA_sig_and_ver()
765 +{
766 + TEE_Result ret;
767 + TEE_ObjectHandle rsa_keypair = (TEE_ObjectHandle)NULL;
768 + size_t key_size = 512;
769 + uint32_t rsa_alg = TEE_ALG_RSASSA_PKCS1_V1_5_SHA1;
770 + char *dig_msg = "TEST";
771 + uint32_t fn_ret = 1; /* Initialized error return */
772 +
773 + uint32_t dig_len = 20;
774 + uint32_t sig_len = 64;
775 +
776 + void *dig = NULL;
777 + void *sig = NULL;
778 +
779 + dig = TEE_Malloc(dig_len, 0);
780 + sig = TEE_Malloc(sig_len, 0);
781 + if (!dig || !sig) {
782 + PRI_FAIL("Out of memory");
783 + goto err;
784 + }
785 +
786 + TEE_MemMove(dig, dig_msg, 5);
787 +
788 + ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, key_size, &rsa_keypair);
789 + if (ret != TEE_SUCCESS) {
790 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
791 + goto err;
792 + }
793 +
794 + ret = TEE_GenerateKey(rsa_keypair, key_size, (TEE_Attribute *)NULL, 0);
795 + if (ret != TEE_SUCCESS) {
796 + PRI_FAIL("Generate key failure : 0x%x", ret);
797 + goto err;
798 + }
799 +
800 + if (warp_asym_op(rsa_keypair, TEE_MODE_SIGN, rsa_alg, (TEE_Attribute *)NULL, 0,
801 + dig, dig_len, sig, &sig_len))
802 + goto err;
803 +
804 + if (warp_asym_op(rsa_keypair, TEE_MODE_VERIFY, rsa_alg, (TEE_Attribute *)NULL, 0,
805 + dig, dig_len, sig, &sig_len))
806 + goto err;
807 +
808 + fn_ret = 0; /* OK */
809 +err:
810 + TEE_FreeTransientObject(rsa_keypair);
811 + TEE_Free(dig);
812 + TEE_Free(sig);
813 +
814 + if (fn_ret == 0)
815 + PRI_OK("-");
816 +
817 + return fn_ret;
818 +}
819 +
820 +static uint32_t HMAC_computation()
821 +{
822 + TEE_Result ret;
823 + TEE_ObjectHandle hmac_key = (TEE_ObjectHandle)NULL;
824 + TEE_OperationHandle hmac_handle = (TEE_OperationHandle)NULL;
825 + TEE_OperationHandle hmac_handle2 = (TEE_OperationHandle)NULL;
826 + size_t key_size = 256;
827 + uint32_t alg = TEE_ALG_HMAC_SHA256;
828 + uint32_t alg2 = TEE_ALG_HMAC_SHA256;
829 + char *seed_msg = "TEST";
830 + uint32_t fn_ret = 1; /* Initialized error return */
831 +
832 + uint32_t mac_len = 64;
833 + size_t msg_len = 100;
834 +
835 + void *mac = NULL;
836 + void *msg = NULL;
837 +
838 + mac = TEE_Malloc(mac_len, 0);
839 + msg = TEE_Malloc(msg_len, 0);
840 + if (!mac || !msg) {
841 + PRI_FAIL("Out of memory");
842 + goto err;
843 + }
844 +
845 + TEE_MemMove(msg, seed_msg, 5);
846 +
847 + ret = TEE_AllocateTransientObject(TEE_TYPE_HMAC_SHA256, key_size, &hmac_key);
848 + if (ret != TEE_SUCCESS) {
849 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
850 + goto err;
851 + }
852 +
853 + ret = TEE_GenerateKey(hmac_key, key_size, (TEE_Attribute *)NULL, 0);
854 + if (ret != TEE_SUCCESS) {
855 + PRI_FAIL("Generate key failure : 0x%x", ret);
856 + goto err;
857 + }
858 +
859 + ret = TEE_AllocateOperation(&hmac_handle, alg, TEE_MODE_MAC, key_size);
860 + if (ret != TEE_SUCCESS) {
861 + PRI_FAIL("Cant alloc first handler");
862 + goto err;
863 + }
864 +
865 + ret = TEE_AllocateOperation(&hmac_handle2, alg2, TEE_MODE_MAC, key_size);
866 + if (ret != TEE_SUCCESS) {
867 + PRI_FAIL("Cant alloc second handler");
868 + goto err;
869 + }
870 +
871 + ret = TEE_SetOperationKey(hmac_handle, hmac_key);
872 + if (ret != TEE_SUCCESS) {
873 + PRI_FAIL("Failed to set first operation key : 0x%x", ret);
874 + goto err;
875 + }
876 +
877 + ret = TEE_SetOperationKey(hmac_handle2, hmac_key);
878 + if (ret != TEE_SUCCESS) {
879 + PRI_FAIL("Failed to set second operation key : 0x%x", ret);
880 + goto err;
881 + }
882 +
883 + TEE_MACInit(hmac_handle, NULL, 0);
884 +
885 + TEE_MACUpdate(hmac_handle, msg, msg_len);
886 +
887 + ret = TEE_MACComputeFinal(hmac_handle, NULL, 0, mac, &mac_len);
888 + if (ret != TEE_SUCCESS) {
889 + PRI_FAIL("First final failed : 0x%x", ret);
890 + goto err;
891 + }
892 +
893 + TEE_MACInit(hmac_handle2, NULL, 0);
894 +
895 + ret = TEE_MACCompareFinal(hmac_handle2, msg, msg_len, mac, mac_len);
896 + if (ret != TEE_SUCCESS) {
897 + PRI_FAIL("MAC Invalid");
898 + goto err;
899 + }
900 +
901 + fn_ret = 0; /* OK */
902 +err:
903 + TEE_FreeTransientObject(hmac_key);
904 + TEE_FreeOperation(hmac_handle);
905 + TEE_FreeOperation(hmac_handle2);
906 + TEE_Free(mac);
907 + TEE_Free(msg);
908 +
909 + if (fn_ret == 0)
910 + PRI_OK("-");
911 +
912 + return fn_ret;
913 +}
914 +
915 +static uint32_t RSA_keypair_enc_dec()
916 +{
917 + TEE_Result ret;
918 + TEE_ObjectHandle rsa_keypair = (TEE_ObjectHandle)NULL;
919 + size_t key_size = 512;
920 + uint32_t rsa_alg = TEE_ALG_RSAES_PKCS1_V1_5;
921 + char *plain_msg = "TEST";
922 + uint32_t fn_ret = 1; /* Initialized error return */
923 +
924 + uint32_t plain_len = 10;
925 + uint32_t cipher_len = 64;
926 + uint32_t dec_plain_len = 64;
927 +
928 + void *plain = NULL;
929 + void *cipher = NULL;
930 + void *dec_plain = NULL;
931 +
932 + plain = TEE_Malloc(plain_len, 0);
933 + cipher = TEE_Malloc(cipher_len, 0);
934 + dec_plain = TEE_Malloc(dec_plain_len, 0);
935 + if (!plain || !cipher || !dec_plain) {
936 + PRI_FAIL("Out of memory");
937 + goto err;
938 + }
939 +
940 + TEE_MemMove(plain, plain_msg, 5);
941 +
942 + ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, key_size, &rsa_keypair);
943 + if (ret != TEE_SUCCESS) {
944 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
945 + goto err;
946 + }
947 +
948 + ret = TEE_GenerateKey(rsa_keypair, key_size, (TEE_Attribute *)NULL, 0);
949 + if (ret != TEE_SUCCESS) {
950 + PRI_FAIL("Generate key failure : 0x%x", ret);
951 + goto err;
952 + }
953 +
954 + if (warp_asym_op(rsa_keypair, TEE_MODE_ENCRYPT, rsa_alg, (TEE_Attribute *)NULL, 0,
955 + plain, plain_len, cipher, &cipher_len))
956 + goto err;
957 +
958 + if (warp_asym_op(rsa_keypair, TEE_MODE_DECRYPT, rsa_alg, (TEE_Attribute *)NULL, 0,
959 + (unsigned char *)cipher, cipher_len, dec_plain, &dec_plain_len))
960 + goto err;
961 +
962 + if (TEE_MemCompare(dec_plain, plain, plain_len)) {
963 + PRI_FAIL("Decrypted not matching to original\n");
964 + goto err;
965 + }
966 +
967 + fn_ret = 0; /* OK */
968 +err:
969 + TEE_FreeTransientObject(rsa_keypair);
970 + TEE_Free(plain);
971 + TEE_Free(dec_plain);
972 + TEE_Free(cipher);
973 +
974 + if (fn_ret == 0)
975 + PRI_OK("-");
976 +
977 + return fn_ret;
978 +}
979 +
980 +
981 +static uint32_t set_key_and_rm_and_do_crypto()
982 +{
983 + TEE_Result ret;
984 + TEE_ObjectHandle rsa_keypair = (TEE_ObjectHandle)NULL;
985 + TEE_OperationHandle sign_op = (TEE_OperationHandle)NULL,
986 + verify_op = (TEE_OperationHandle)NULL;
987 + size_t key_size = 512;
988 + uint32_t rsa_alg = TEE_ALG_RSASSA_PKCS1_V1_5_SHA256;
989 + char *dig_seed = "TEST";
990 + uint32_t dig_len = 32, sig_len = 64;
991 + char dig[32] = {0}, sig[64] = {0};
992 + uint32_t fn_ret = 1; /* Initialized error return */
993 +
994 + TEE_MemMove(dig, dig_seed, 5);
995 +
996 + ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, key_size, &rsa_keypair);
997 + if (ret != TEE_SUCCESS) {
998 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
999 + goto err;
1000 + }
1001 +
1002 + ret = TEE_GenerateKey(rsa_keypair, key_size, (TEE_Attribute *)NULL, 0);
1003 + if (ret != TEE_SUCCESS) {
1004 + PRI_FAIL("Generate key failure : 0x%x", ret);
1005 + goto err;
1006 + }
1007 +
1008 + ret = TEE_AllocateOperation(&sign_op, rsa_alg, TEE_MODE_SIGN, key_size);
1009 + if (ret != TEE_SUCCESS) {
1010 + PRI_FAIL("Failed to alloc sign operation handle : 0x%x", ret);
1011 + goto err;
1012 + }
1013 +
1014 + ret = TEE_AllocateOperation(&verify_op, rsa_alg, TEE_MODE_VERIFY, key_size * 2);
1015 + if (ret != TEE_SUCCESS) {
1016 + PRI_FAIL("Failed to alloc verify operation handle : 0x%x", ret);
1017 + goto err;
1018 + }
1019 +
1020 + ret = TEE_SetOperationKey(sign_op, rsa_keypair);
1021 + if (ret != TEE_SUCCESS) {
1022 + PRI_FAIL("Failed to set sign operation key : 0x%x", ret);
1023 + goto err;
1024 + }
1025 +
1026 + ret = TEE_SetOperationKey(verify_op, rsa_keypair);
1027 + if (ret != TEE_SUCCESS) {
1028 + PRI_FAIL("Failed to set verify operation key : 0x%x", ret);
1029 + goto err;
1030 + }
1031 +
1032 + TEE_FreeTransientObject(rsa_keypair);
1033 + rsa_keypair = (TEE_ObjectHandle)NULL;
1034 +
1035 + ret = TEE_AsymmetricSignDigest(sign_op, (TEE_Attribute *)NULL, 0,
1036 + dig, dig_len, sig, &sig_len);
1037 + if (ret != TEE_SUCCESS) {
1038 + PRI_FAIL("Sign failed : 0x%x", ret);
1039 + goto err;
1040 + }
1041 +
1042 + ret = TEE_AsymmetricVerifyDigest(verify_op, (TEE_Attribute *)NULL, 0,
1043 + dig, dig_len, sig, sig_len);
1044 + if (ret == TEE_SUCCESS) {
1045 + /* Do nothing */
1046 + } else if (ret == TEE_ERROR_SIGNATURE_INVALID) {
1047 + PRI_FAIL("Signature invalid");
1048 + goto err;
1049 + } else {
1050 + PRI_FAIL("Verify failed : 0x%x", ret);
1051 + goto err;
1052 + }
1053 +
1054 + fn_ret = 0; /* OK */
1055 +err:
1056 + TEE_FreeTransientObject(rsa_keypair);
1057 + TEE_FreeOperation(sign_op);
1058 + TEE_FreeOperation(verify_op);
1059 +
1060 + if (fn_ret == 0)
1061 + PRI_OK("-");
1062 +
1063 + return fn_ret;
1064 +}
1065 +
1066 +static uint32_t read_key_and_do_crypto()
1067 +{
1068 + TEE_Result ret;
1069 + TEE_ObjectHandle rsa_keypair = (TEE_ObjectHandle)NULL,
1070 + persisten_rsa_keypair = (TEE_ObjectHandle)NULL;
1071 + char objID[] = "56c5d1b260704de30fe99f67e5b9327613abebe6172a2b4e949d84b8e561e2fb";
1072 + uint32_t objID_len = 45;
1073 + uint32_t flags = TEE_DATA_FLAG_ACCESS_WRITE_META;
1074 + uint32_t rsa_alg = TEE_ALG_RSAES_PKCS1_V1_5, key_size = 512;
1075 + char *plain_msg = "TEST";
1076 + uint32_t plain_len = 10, cipher_len = 64, dec_plain_len = 64,
1077 + per_cipher_len = 64, per_dec_plain_len = 64;
1078 + char plain[10] = {0}, cipher[64] = {0}, dec_plain[64] = {0},
1079 + per_cipher[64] = {0}, per_dec_plain[64] = {0};
1080 + uint32_t fn_ret = 1; /* Initialized error return */
1081 +
1082 + TEE_MemMove(plain, plain_msg, 5);
1083 +
1084 + ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, key_size, &rsa_keypair);
1085 + if (ret != TEE_SUCCESS) {
1086 + PRI_FAIL("Failed to alloc transient object handle : 0x%x", ret);
1087 + goto err;
1088 + }
1089 +
1090 + ret = TEE_GenerateKey(rsa_keypair, key_size, (TEE_Attribute *)NULL, 0);
1091 + if (ret != TEE_SUCCESS) {
1092 + PRI_FAIL("Generate key failure : 0x%x", ret);
1093 + goto err;
1094 + }
1095 +
1096 + ret = TEE_CreatePersistentObject(TEE_STORAGE_PRIVATE, (void *)objID, objID_len,
1097 + 0, rsa_keypair, NULL, 0,
1098 + (TEE_ObjectHandle *)NULL);
1099 + if (ret != TEE_SUCCESS) {
1100 + PRI_FAIL("Create persisten object failed : 0x%x", ret);
1101 + goto err;
1102 + }
1103 +
1104 + ret = TEE_OpenPersistentObject(TEE_STORAGE_PRIVATE,
1105 + (void *)objID, objID_len, flags, &persisten_rsa_keypair);
1106 + if (ret != TEE_SUCCESS) {
1107 + PRI_FAIL("Open failed : 0x%x", ret);
1108 + goto err;
1109 + }
1110 +
1111 + /* Transient object */
1112 + if (warp_asym_op(rsa_keypair, TEE_MODE_ENCRYPT, rsa_alg, (TEE_Attribute *)NULL, 0,
1113 + plain, plain_len, cipher, &cipher_len))
1114 + goto err;
1115 +
1116 + if (warp_asym_op(rsa_keypair, TEE_MODE_DECRYPT, rsa_alg, (TEE_Attribute *)NULL, 0,
1117 + (unsigned char *)cipher, cipher_len, dec_plain, &dec_plain_len))
1118 + goto err;
1119 +
1120 + if (dec_plain_len != plain_len || TEE_MemCompare(dec_plain, plain, plain_len)) {
1121 + PRI_FAIL("Decrypted not matching to original\n");
1122 + goto err;
1123 + }
1124 +
1125 + /* Persistent object */
1126 + if (warp_asym_op(persisten_rsa_keypair, TEE_MODE_ENCRYPT, rsa_alg, (TEE_Attribute *)NULL, 0,
1127 + plain, plain_len, per_cipher, &per_cipher_len))
1128 + goto err;
1129 +
1130 + if (warp_asym_op(persisten_rsa_keypair, TEE_MODE_DECRYPT, rsa_alg, (TEE_Attribute *)NULL, 0,
1131 + (unsigned char *)per_cipher, per_cipher_len,
1132 + per_dec_plain, &per_dec_plain_len))
1133 + goto err;
1134 +
1135 + if (per_dec_plain_len != plain_len ||
1136 + TEE_MemCompare(dec_plain, per_dec_plain, dec_plain_len)) {
1137 + PRI_FAIL("Persisten decrypted not matching plain text\n");
1138 + goto err;
1139 + }
1140 +
1141 + fn_ret = 0; /* OK */
1142 +err:
1143 + TEE_FreeTransientObject(rsa_keypair);
1144 + TEE_CloseAndDeletePersistentObject(persisten_rsa_keypair);
1145 +
1146 + if (fn_ret == 0)
1147 + PRI_OK("-");
1148 +
1149 + return fn_ret;
1150 +}
1151 +
1152 +uint32_t crypto_test(uint32_t loop_count)
1153 +{
1154 + uint32_t i, test_have_fail = 0;
1155 +
1156 + PRI_STR("START: crypto tests");
1157 +
1158 + PRI_STR("----Begin-with-test-cases----\n");
1159 +
1160 + for (i = 0; i < loop_count; ++i) {
1161 +
1162 + if (sha256_digest() ||
1163 + sha256_digest_nist() ||
1164 + aes_256_cbc_enc_dec() ||
1165 + aes_128_cbc_enc_dec() ||
1166 + RSA_sig_and_ver() ||
1167 + RSA_keypair_enc_dec() ||
1168 + HMAC_computation() ||
1169 + set_key_and_rm_and_do_crypto() ||
1170 + read_key_and_do_crypto() ||
1171 + rsa_sign_nist_sha1_pkcs() ||
1172 + run_ecdsa_tests()) {
1173 + test_have_fail = 1;
1174 + break;
1175 + }
1176 + }
1177 +
1178 + PRI_STR("----Test-has-reached-end----\n");
1179 +
1180 + PRI_STR("END: crypto tests");
1181 +
1182 + return test_have_fail ? 1 : 0;
1183 +}
...\ No newline at end of file ...\ No newline at end of file
1 +static TEE_Result test_encrypt_ta(TEE_Param params[4])
2 +{
3 +
4 + char *in = (char *)params[TEST_STRING].memref.buffer;
5 + int in_len = params[TEST_STRING].memref.size;
6 +
7 + char encrypted[512];
8 + int encrypted_len;
9 +
10 + encrypt_using_private_key(in, in_len, encrypted, &encrypted_len);
11 + memcpy(params[TEST_EN_DECRYPTED].memref.buffer, encrypted, encrypted_len);
12 + params[TEST_EN_DECRYPTED].memref.size = encrypted_len;
13 + params[TEST_RC_POS].value.a = ALL_OK;
14 + return TEE_SUCCESS;
15 +}
16 +
17 +bool encrypt_using_private_key(char *in, int in_len, char *out, int *out_len)
18 +{
19 +
20 + TEE_Result ret = TEE_SUCCESS; // return code
21 + TEE_ObjectHandle key = (TEE_ObjectHandle)NULL;
22 + TEE_Attribute rsa_attrs[3];
23 + void *to_encrypt = NULL;
24 + uint32_t cipher_len = 256;
25 + void *cipher = NULL;
26 +
27 + rsa_attrs[0].attributeID = TEE_ATTR_RSA_MODULUS;
28 + rsa_attrs[0].content.ref.buffer = CA_modulus;
29 + rsa_attrs[0].content.ref.length = SIZE_OF_VEC(CA_modulus);
30 +
31 + rsa_attrs[1].attributeID = TEE_ATTR_RSA_PUBLIC_EXPONENT;
32 + rsa_attrs[1].content.ref.buffer = CA_public_key;
33 + rsa_attrs[1].content.ref.length = SIZE_OF_VEC(CA_public_key);
34 +
35 + rsa_attrs[2].attributeID = TEE_ATTR_RSA_PRIVATE_EXPONENT;
36 + rsa_attrs[2].content.ref.buffer = CA_private_key;
37 + rsa_attrs[2].content.ref.length = SIZE_OF_VEC(CA_private_key);
38 +
39 + ret = TEE_AllocateTransientObject(TEE_TYPE_RSA_KEYPAIR, 1024, &key);
40 + ret = TEE_PopulateTransientObject(key, (TEE_Attribute *)&rsa_attrs, 3);
41 + to_encrypt = TEE_Malloc(in_len, 0);
42 + cipher = TEE_Malloc(cipher_len, 0);
43 + TEE_MemMove(to_encrypt, in, in_len - 1);
44 + if (!perform_rsa_operation(key, TEE_MODE_ENCRYPT, to_encrypt, in_len, cipher, &cipher_len))
45 + {
46 + DMSG("Encrypt failed : 0x%x", ret);
47 + return TEE_ERROR_BAD_PARAMETERS;
48 + }
49 +
50 + memcpy(out, cipher, cipher_len);
51 + *out_len = cipher_len;
52 + out[cipher_len] = '\0';
53 + DMSG("Return: %s", out);
54 + DMSG("Return lenght: %i", *out_len);
55 +
56 + return TRUE;
57 +}
58 +
59 +#define SIZE_OF_VEC(vec) (sizeof(vec) - 1)
60 +
61 +uint8_t CA_modulus[] =
62 + "\xbe\x5c\xe7\x5f\xef\xd6\x8b\x23\xaf\x9f\xa5\x44\xfc\xa4\x9a"
63 + "\x94\x0a\xc8\x67\x57\x30\x6d\x20\x4b\xa0\xee\xd6\x5f\x07\x9b"
64 + "\x4a\x98\x5d\xcf\x9a\xce\xae\xaa\xa9\x9b\xeb\xdf\xdc\xde\xb9"
65 + "\xfc\x3f\x54\xb2\x93\x7d\xe2\x9e\x29\x52\x57\xd4\x3d\xbc\x4c"
66 + "\x89\xa7\xe9\xc5";
67 +
68 +uint8_t CA_public_key[] =
69 + "\x01\x00\x01";
70 +
71 +uint8_t CA_private_key[] =
72 + "\x48\x30\x89\x19\xcb\xa5\x2b\xac\xc3\xcc\x21\xeb\x90\x77\x87"
73 + "\x9b\x3e\x9f\x92\xf8\xf0\x87\x61\xa8\xec\x85\xc6\x4b\xd1\x61"
74 + "\xa5\x9e\x8b\xc7\xa1\x5a\x72\xf0\x04\xc8\x04\x5d\x5e\x52\x18"
75 + "\x5c\xd4\x68\x82\x21\x17\xdd\xa1\xcc\x42\x87\xe5\x84\xe1\x58"
76 + "\x20\xc2\x03\x7d";
77 +
78 +B perform_rsa_operation(TEE_ObjectHandle key, TEE_OperationMode mode,
79 + void *in_chunk, uint32_t in_chunk_len,
80 + void *out_chunk, uint32_t *out_chunk_len)
81 +{
82 +
83 + TEE_ObjectInfo info;
84 + TEE_OperationHandle handle = (TEE_OperationHandle)NULL;
85 + TEE_Result ret = TEE_SUCCESS;
86 +
87 + TEE_GetObjectInfo(key, &info);
88 + ret = TEE_AllocateOperation(&handle, TEE_ALG_RSAES_PKCS1_V1_5, mode, info.maxObjectSize);
89 + ret = TEE_SetOperationKey(handle, key);
90 +
91 + if (mode == TEE_MODE_ENCRYPT)
92 + {
93 + DMSG("Encrypting values\n");
94 + ret = TEE_AsymmetricEncrypt(handle, (TEE_Attribute *)NULL, 0, in_chunk, in_chunk_len, out_chunk, out_chunk_len);
95 + }
96 +
97 + if (mode == TEE_MODE_DECRYPT)
98 + {
99 + DMSG("Decrypting values\n");
100 + ret = TEE_AsymmetricDecrypt(handle, (TEE_Attribute *)NULL, 0, in_chunk, in_chunk_len, out_chunk, out_chunk_len);
101 + }
102 +
103 + TEE_FreeOperation(handle);
104 + return TRUE;
105 +}
...\ No newline at end of file ...\ No newline at end of file