49b6b32d2cc39b03670ce731d78ab65088e0a06c
[platform/core/security/libwebappenc.git] / tests / wae_tests.c
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  *
16  *
17  * @file        wae_tests.c
18  * @author      Dongsun Lee (ds73.lee@samsung.com)
19  * @version     1.0
20  * @brief       internal test cases for libwebappenc.
21  */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "web_app_enc.h"
29 #include "key_handler.h"
30 #include "crypto_service.h"
31
32 #include <tzplatform_config.h>
33
34 static size_t tc_seq = 0;
35 static size_t tc_succ = 0;
36 static size_t tc_fail = 0;
37
38 #define FPRINTF(format, args...) fprintf(stdout, format, ##args)
39
40 static int RUNTC(int (*tc_method)(), const char* tc_name)
41 {
42     int ret = WAE_ERROR_NONE;
43     FPRINTF("[%02d:%s]started...\n", tc_seq, tc_name);
44     ret = tc_method();
45     if(ret == WAE_ERROR_NONE) {
46         FPRINTF("[%02d:%s]ended. SUCCESS\n\n", tc_seq, tc_name);
47         tc_succ++;
48     } else {
49         FPRINTF("[%02d:%s]ended. FAIL. error=%d\n\n", tc_seq, tc_name, ret);
50         tc_fail++;
51     }
52     tc_seq++;
53     return ret;
54 }
55
56 static void PRINT_TC_SUMMARY()
57 {
58     FPRINTF("\n");
59     FPRINTF("===============================================\n");
60     FPRINTF(" TOTAL = %d, SUCCESS = %d, FAIL = %d\n", tc_seq, tc_succ, tc_fail);
61     FPRINTF("===============================================\n");
62 }
63
64 void _print_binary_to_hex(const char* msg, unsigned char* bin, size_t len)
65 {
66     size_t i = 0;
67     FPRINTF("%s", msg);
68     for(i=0; i<len; i++) {
69         FPRINTF("%02x", bin[i]);
70     }
71     FPRINTF("\n");
72 }
73
74 int _compare_binary(const unsigned char* b1, size_t b1Len, const unsigned char* b2, size_t b2Len)
75 {
76     size_t i = 0;
77     if(b1Len != b2Len)
78         return b1Len - b2Len;
79     for(i=0; i<b1Len; i++) {
80         if(b1[i] != b2[i])
81             return b1[i] - b2[i];
82     }
83     return 0;
84 }
85
86
87 //=================================================================================
88 // tests for crypto_service.h
89 //=================================================================================
90 int wae_tc_encrypt_decrypt_app_dek()
91 {
92     int ret = WAE_ERROR_NONE;
93     unsigned char dek[32];
94     unsigned char* encryptedDek = NULL;
95     size_t encryptedDekLen = 0;
96     unsigned char* decryptedDek = NULL;
97     size_t decryptedDekLen = 0;
98
99     const char* priKey =
100             "-----BEGIN RSA PRIVATE KEY-----\n"
101             "MIIEpgIBAAKCAQEA0kWtjpRO7Zh2KX2naVE/BDJdrfwK9xexfNA0MkY2VJ4J2AKM\n"
102             "YTj1D1jntceryupCEHOvP3rum+WsFvPXduz9+VKnSsSqj4jcTUubtpDUGA5G79Iq\n"
103             "LEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq8JkqBPoCCwtUs73ruE9VbtsBO/kT\n"
104             "lASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCwS2OTvQDNvsXfFnA0ZJEEYw/rZLir\n"
105             "j7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12YxqHdy7gnJXodLhvE/cR4SN9VW7+qmC\n"
106             "MBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4+wIDAQABAoIBAQCwxqV/vc2RUGDe\n"
107             "xuXM0+IvrAw37jJlw4SS0xNexMp+XxMViCbuwYy851h96azS/himbiuCKd6aL/96\n"
108             "mGunbtyiFEvSvv5Jh5z2Wr9BQAcfZjla+4w7BIsg9UNifE/OfgLsQBu34xhsHtfK\n"
109             "7nFehCOl/I5n+qtnD5KZPe0DWacQdwY4vEAj6YyXdb2bBg+MiwE9KVxGEIUDbklh\n"
110             "Is70JXczjLZCS+lIpOKh0/lbZmBZePoUbVTtS+GvtPTpQC/aTHRkwGoEtuPEWpbL\n"
111             "0Q1d6zO+vDJVLJlb5FF2haghs8IlqAxkkPjeUTNye+WktRrDQxmPu/blbxQrygfq\n"
112             "Au5tBnsxAoGBAOiVtcpg32puo3Yq2Y78oboe9PuHaQP0d3DhwP3/7J0BeNslpjW7\n"
113             "E1LWsVsCanxTE8XPUdFfAWgMk7lQqESN0wawGmSmWk+eQPZdjHanBaC8vh7aKjo6\n"
114             "q9FdT1DKjrRi23QyDco3f3E7hvM93IAAhw1ikNu8DT19JAxtdeMh5WAZAoGBAOdw\n"
115             "6neEvIFXh3RWEv2/GKVhVR8mxDqxmuFdXpOF+YWsK0Tg4uC8jm9kUGnwXgT2Mjke\n"
116             "oAwYAFcRbHQQGsxy/vkV16kv4aurTE2hMpjeXCAakwV0Pi2w1f9WnDokjgORkOmc\n"
117             "+QK9I8egdFPMVDfQjhLslhSUY0Eb4qcJ6q9WxfQzAoGBANSsAFybk+7oWAO3TtQW\n"
118             "YXOk1vIgcYAyS/0mEKixGZS/QdlxZbf/5b17nxTO8rvX416fIftG2ixgQ7vR6us0\n"
119             "m9+jq56ZFj9zP4eHJudf9h9yNo5TgwVXnMCGh/4iGbcMJgrrsfxUHu5VNiK5UCSj\n"
120             "VtqAZGDoZVryUMIkXQVhezIRAoGBAN7QUIqcGbcUA24257Wu4hVlrUN+WPCAyDEr\n"
121             "aL/x/ZV5eXaoYwQlw6LuGpTDOmDgfN2M5FyARuOL/LOIRaSLGXnIU4WoeUSCd8VM\n"
122             "6Z9Og7bMnrpjfPEUDBH02hcH1kkNPUwLOZgva2Dm0tdSIcpSWFVTu/E4Io4uQHi8\n"
123             "DVqc2ZsNAoGBAJT76ezXNSSv8hnrKqTpwgTicpqhRZ3eFQjyl4HRL26AJMKv++x8\n"
124             "4/IsVIwxaHzpbN3nnCjmAHV4gX9YpxVnvYcZflC9WZeDkwNMLmPYb3Zg27EzSMfQ\n"
125             "8yrfWJZo3qobipcHf1yohAt4fHk9kUKtPHEwp0xKe//rfhswLb3VCzvQ\n"
126             "-----END RSA PRIVATE KEY-----";
127     const char* pubKey =
128             "-----BEGIN PUBLIC KEY-----\n"
129             "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0kWtjpRO7Zh2KX2naVE/\n"
130             "BDJdrfwK9xexfNA0MkY2VJ4J2AKMYTj1D1jntceryupCEHOvP3rum+WsFvPXduz9\n"
131             "+VKnSsSqj4jcTUubtpDUGA5G79IqLEPFuSBaqI8Uwkzd08pE+s30oaJDnNazMhSq\n"
132             "8JkqBPoCCwtUs73ruE9VbtsBO/kTlASIAfe8nXqcJLcDQgWYhizjJw0Pi6d74oCw\n"
133             "S2OTvQDNvsXfFnA0ZJEEYw/rZLirj7OHoOjz+Sh5N+1uA3Up6SPPEbHuP6L12Yxq\n"
134             "Hdy7gnJXodLhvE/cR4SN9VW7+qmCMBjmLkBejGrEX3STS9sLI7MZHu9Y26dwuYb4\n"
135             "+wIDAQAB\n"
136             "-----END PUBLIC KEY-----";
137
138     ret = encrypt_app_dek((const unsigned char*) pubKey, strlen(pubKey),
139                           dek, sizeof(dek), &encryptedDek, &encryptedDekLen);
140     if(ret != WAE_ERROR_NONE){
141         FPRINTF("...FAIL: encrypt_app_dek. ret=%d\n", ret);
142         ret = WAE_ERROR_CRYPTO;
143         goto error;
144     }
145
146     ret = decrypt_app_dek((const unsigned char*) priKey, strlen(priKey), NULL,
147                           encryptedDek, encryptedDekLen, &decryptedDek, &decryptedDekLen);
148     if(ret != WAE_ERROR_NONE){
149         FPRINTF("...FAIL: decrypt_app_dek. ret=%d\n", ret);
150         ret = WAE_ERROR_CRYPTO;
151         goto error;
152     }
153
154     _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
155     _print_binary_to_hex("...ENC  DEK= ", encryptedDek, encryptedDekLen);
156     _print_binary_to_hex("...DEC  DEK= ", decryptedDek, decryptedDekLen);
157
158     if(_compare_binary(dek, sizeof(dek), decryptedDek, decryptedDekLen) != 0) {
159         FPRINTF("...FAIL: ORIG DEK != decrypted DEK\n");
160         ret = WAE_ERROR_CRYPTO;
161         goto error;
162     }
163
164     ret = WAE_ERROR_NONE;
165
166 error:
167     if(encryptedDek != NULL)
168         free(encryptedDek);
169     if(decryptedDek != NULL)
170         free(decryptedDek);
171
172     return ret;
173 }
174
175 int wae_tc_encrypt_decrypt_aes_cbc()
176 {
177     int ret = WAE_ERROR_NONE;
178
179     unsigned char dek[32] = {0, };
180     size_t keyLen = 32;
181     const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
182     size_t plaintextLen = strlen(plaintext);
183     unsigned char* encrypted = NULL;
184     size_t encLen = 0;
185     unsigned char* decrypted = NULL;
186     size_t decLen = 0;
187     char decrypted_str[1024] = {0, };
188
189     ret = _get_random(keyLen, dek);
190
191     ret = encrypt_aes_cbc(dek, keyLen, (const unsigned char*)plaintext, plaintextLen, &encrypted, &encLen);
192     if(ret != WAE_ERROR_NONE){
193         FPRINTF("...FAIL: encrypt_aes_cbc. ret=%d\n", ret);
194         ret = WAE_ERROR_CRYPTO;
195         goto error;
196     }
197
198     ret = decrypt_aes_cbc(dek, keyLen, encrypted, encLen, &decrypted, &decLen);
199     if(ret != WAE_ERROR_NONE){
200         FPRINTF("...FAIL: decrypt_aes_cbc. ret=%d\n", ret);
201         ret = WAE_ERROR_CRYPTO;
202         goto error;
203     }
204
205     if(plaintextLen != decLen) {
206         FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", plaintextLen, decLen);
207         ret = WAE_ERROR_CRYPTO;
208         goto error;
209     }
210
211     memcpy(decrypted_str, decrypted, decLen);
212     FPRINTF("...plaintext = %s\n", plaintext);
213     FPRINTF("...decrypted = %s\n", decrypted_str);
214     if(strcmp(plaintext, decrypted_str) != 0) {
215         FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
216         ret = WAE_ERROR_CRYPTO;
217         goto error;
218     }
219
220 error:
221     if(encrypted != NULL)
222         free(encrypted);
223     if(decrypted != NULL)
224         free(decrypted);
225
226     return ret;
227 }
228
229 //=================================================================================
230 // tests for key_handler.h
231 //=================================================================================
232 int wae_tc_cache()
233 {
234     int ret = WAE_ERROR_NONE;
235
236     const char* pkg1 = "pkg1";
237     const char* pkg2 = "pkg2";
238     const char* pkg3 = "pkg3";
239     const char* pkgDummy = "dummy";
240
241     unsigned char dek1[32] = {1, };
242     unsigned char dek2[32] = {2, };
243     unsigned char dek3[32] = {3, };
244     unsigned char* retDek = NULL;
245
246     _initialize_cache();
247
248     _add_app_dek_to_cache(pkg1, dek1);
249     _add_app_dek_to_cache(pkg2, dek2);
250     _add_app_dek_to_cache(pkg3, dek3);
251
252     retDek = NULL;
253     retDek = _get_app_dek_from_cache(pkg1);
254     if(retDek == NULL || _compare_binary(dek1, 32, retDek, 32) != 0) {
255         FPRINTF("failed in cache. Diffent DEK1\n");
256         ret = WAE_ERROR_UNKNOWN;
257         goto error;
258     }
259     _print_binary_to_hex("...DEK1         : ", dek1, 32);
260     _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
261
262     retDek = NULL;
263     retDek = _get_app_dek_from_cache(pkg2);
264     if(retDek == NULL || _compare_binary(dek2, 32, retDek, 32) != 0) {
265         FPRINTF("failed in cache. Diffent DEK2\n");
266         ret = WAE_ERROR_UNKNOWN;
267         goto error;
268     }
269     _print_binary_to_hex("...DEK2         : ", dek2, 32);
270     _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
271
272     retDek = NULL;
273     retDek = _get_app_dek_from_cache(pkg3);
274     if(retDek == NULL || _compare_binary(dek3, 32, retDek, 32) != 0) {
275         FPRINTF("failed in cache. Diffent DEK3\n");
276         ret = WAE_ERROR_UNKNOWN;
277         goto error;
278     }
279     _print_binary_to_hex("...DEK3         : ", dek3, 32);
280     _print_binary_to_hex("...Returen DEK3 : ", retDek, 32);
281
282     retDek = NULL;
283     retDek = _get_app_dek_from_cache(pkgDummy);
284     if(retDek != NULL) {
285         FPRINTF("failed in cache. Wrong DEK_DUMMY1 returned\n");
286         _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
287         ret = WAE_ERROR_UNKNOWN;
288         goto error;
289     }
290
291     _remove_app_dek_from_cache(pkg3);
292     retDek = NULL;
293     retDek = _get_app_dek_from_cache(pkg3);
294     if(retDek != NULL) {
295         FPRINTF("fail to remove app dek from cache\n");
296         ret = WAE_ERROR_UNKNOWN;
297         goto error;
298     }
299
300     _initialize_cache();
301
302     _add_app_dek_to_cache(pkg1, dek1);
303
304     retDek = NULL;
305     retDek = _get_app_dek_from_cache(pkg2);
306     if(retDek != NULL) {
307         FPRINTF("failed in cache. Wrong DEK_DUMMY2 returned\n");
308         _print_binary_to_hex("retured wrong DEK : ", retDek, 32);
309         ret = WAE_ERROR_UNKNOWN;
310         goto error;
311     }
312
313     ret = WAE_ERROR_NONE;
314 error:
315     return ret;
316 }
317
318 int wae_tc_get_random()
319 {
320     int ret = WAE_ERROR_NONE;
321
322     size_t rand_len = 32;
323     unsigned char random[32] = {0, };
324
325     ret = _get_random(rand_len, random);
326
327     _print_binary_to_hex("...RANDOM = ", random, sizeof(random));
328
329     return ret;
330 }
331
332 int wae_tc_get_alias()
333 {
334     int ret = WAE_ERROR_NONE;
335
336     const char* pkgId = "TEST_PKG_ID";
337     char sys_alias[256] = {0, };
338
339     _get_alias(pkgId, sys_alias, sizeof(sys_alias));
340
341     FPRINTF("...pkgid=%s, system alias=%s\n", pkgId, sys_alias);
342
343     return ret;
344 }
345
346 int wae_tc_add_get_remove_dek()
347 {
348     int ret = WAE_ERROR_NONE;
349
350     const char* pkgId = "TEST_PKG_ID";
351
352     size_t dekLen= 32;
353     unsigned char dek[32] = {0, };
354     size_t storedDekLen = 0;
355     unsigned char* storedDek = NULL;
356
357     ret = _get_random(dekLen, dek);
358
359     remove_app_dek(pkgId);
360
361     ret = _add_dek_to_key_manager(pkgId, dek, dekLen);
362     if(ret != WAE_ERROR_NONE) {
363         FPRINTF("...FAIL: _add_dek_to_key_manager. ret=%d\n", ret);
364         goto error;
365     }
366
367     ret = get_app_dek(pkgId, &storedDek, &storedDekLen);
368     if(ret != WAE_ERROR_NONE) {
369         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
370         goto error;
371     }
372
373     if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
374         ret = WAE_ERROR_KEY_MANAGER;
375         FPRINTF("...FAIL: DEK != STORED_DEK.\n");
376         goto error;
377     }
378
379     ret = remove_app_dek(pkgId);
380     if(ret != WAE_ERROR_NONE) {
381         FPRINTF("...FAIL: remove_app_dek. ret=%d\n", ret);
382         goto error;
383     }
384
385     ret = get_app_dek(pkgId, &storedDek, &storedDekLen);
386     if(ret == WAE_ERROR_NONE) {
387         ret = WAE_ERROR_UNKNOWN;
388         FPRINTF("...FAIL: APP DEK still exists in key_manager.\n");
389         goto error;
390     }
391
392     ret = WAE_ERROR_NONE;
393 error:
394     if(storedDek != NULL)
395         free(storedDek);
396
397     return ret;
398 }
399
400 int wae_tc_get_preloaded_app_dek_file_path()
401 {
402     int ret = WAE_ERROR_NONE;
403
404     const char *pkgId = "test_pkg";
405     const char *expectedPath = tzplatform_mkpath4(TZ_SYS_SHARE,
406                                     "wae", "app_dek", "WAE_APP_DEK_test_pkg.adek");
407     char path[100];
408
409     ret = _get_preloaded_app_dek_file_path(pkgId, path);
410     FPRINTF("...expected path : %s\n", expectedPath);
411     FPRINTF("...returned path : %s\n", path);
412
413     if(ret != WAE_ERROR_NONE || strncmp(expectedPath, path, strlen(expectedPath)) != 0) {
414         ret = WAE_ERROR_UNKNOWN;
415         goto error;
416     }
417 error:
418     return ret;
419 }
420
421 int wae_tc_extract_pkg_id_from_file_name()
422 {
423     int ret = WAE_ERROR_NONE;
424     const char* fileName = "WAE_APP_DEK_test_pkg.adek";
425     const char* expectedPkgId = "test_pkg";
426     char pkgId[100];
427
428     ret = _extract_pkg_id_from_file_name(fileName, pkgId);
429     FPRINTF("...expected pkgId: %s\n", expectedPkgId);
430     FPRINTF("...returned pkgId: %s\n", pkgId);
431
432     if(ret != WAE_ERROR_NONE || strncmp(expectedPkgId, pkgId, strlen(expectedPkgId)) != 0) {
433         ret = WAE_ERROR_UNKNOWN;
434         goto error;
435     }
436 error:
437     return ret;
438
439 }
440
441 int wae_tc_read_write_encrypted_app_dek()
442 {
443     int ret = WAE_ERROR_NONE;
444     const char* pkgId = "write_test_pkg";
445     unsigned char dek[256];
446     unsigned char* readDek = NULL;
447     size_t readDekLen = 0;
448
449     ret = _write_encrypted_app_dek_to_file(pkgId, dek, sizeof(dek));
450     if(ret != WAE_ERROR_NONE) {
451         FPRINTF("Fail to _write_encrypted_app_dek_to_file. pkgId=%s\n", pkgId);
452         goto error;
453     }
454
455     ret = _read_encrypted_app_dek_from_file(pkgId, &readDek, &readDekLen);
456     if(ret != WAE_ERROR_NONE) {
457         FPRINTF("Fail to _read_encrypted_app_dek_from_file. pkgId=%s\n", pkgId);
458         goto error;
459     }
460
461     _print_binary_to_hex("...ORIG DEK= ", dek, sizeof(dek));
462     _print_binary_to_hex("...READ DEK= ", readDek, readDekLen);
463     if(_compare_binary(dek, sizeof(dek), readDek, readDekLen) != 0 ) {
464         ret = WAE_ERROR_UNKNOWN;
465         FPRINTF("...FAIL: DEK != read_DEK.\n");
466         goto error;
467     }
468
469 error:
470     if(readDek != NULL)
471         free(readDek);
472     return ret;
473 }
474
475
476 int wae_tc_create_app_dek()
477 {
478     int ret = WAE_ERROR_NONE;
479
480     const char* pkgId = "TEST_PKG_ID";
481     unsigned char* dek = NULL;
482     size_t dekLen = 0;
483
484     size_t storedDekLen = 0;
485     unsigned char* storedDek = NULL;
486
487     remove_app_dek(pkgId);
488
489     ret = create_app_dek(pkgId, &dek, &dekLen);
490     if(ret != WAE_ERROR_NONE) {
491         FPRINTF("...FAIL: create_app_dek. ret=%d\n", ret);
492         goto error;
493     }
494
495     ret = get_app_dek(pkgId, &storedDek, &storedDekLen);
496     if(ret != WAE_ERROR_NONE) {
497         ret = WAE_ERROR_KEY_MANAGER;
498         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
499         goto error;
500     }
501
502     _print_binary_to_hex("...CREATED  DEK = ", dek, dekLen);
503     _print_binary_to_hex("...STORED   DEK = ", storedDek, storedDekLen);
504     if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
505         ret = WAE_ERROR_FILE;
506         FPRINTF("...FAIL: DEK != STORED_DEK.\n");
507         goto error;
508     }
509
510     remove_app_dek(pkgId);
511
512     ret = WAE_ERROR_NONE;
513 error:
514     if(dek != NULL)
515         free(dek);
516     if(storedDek != NULL)
517         free(storedDek);
518     return ret;
519 }
520
521 int wae_tc_get_create_preloaded_app_dek()
522 {
523     int ret = WAE_ERROR_NONE;
524
525     const char* pkgId = "TEST_PKG_ID_FOR_CREATE";
526     unsigned char *dek = NULL;
527     unsigned char *readDek = NULL;
528     size_t readDekLen = 0;
529     size_t dekLen = 0;
530
531     ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
532     if(ret != WAE_ERROR_NO_KEY) {
533         FPRINTF("...FAIL: There should be no APP DEK.  get_preloaded_app_dek. ret=%d\n", ret);
534         ret = WAE_ERROR_FILE;
535         goto error;
536     }
537
538     ret = create_preloaded_app_dek(pkgId, &dek, &dekLen);
539     if(ret != WAE_ERROR_NONE) {
540         FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
541         goto error;
542     }
543
544     ret = get_preloaded_app_dek(pkgId, &readDek, &readDekLen);
545     if(ret != WAE_ERROR_NONE) {
546         FPRINTF("...FAIL: get_preloaded_app_dek. ret=%d\n", ret);
547         goto error;
548     }
549
550     _print_binary_to_hex("...CREATED DEK = ", dek, dekLen);
551     _print_binary_to_hex("...READ    DEK = ", readDek, readDekLen);
552
553     if(_compare_binary(dek, dekLen, readDek, readDekLen) != 0 ) {
554         ret = WAE_ERROR_FILE;
555         FPRINTF("...FAIL: DEK != READ_DEK.\n");
556         goto error;
557     }
558
559     ret = WAE_ERROR_NONE;
560 error:
561     if(dek != NULL)
562         free(dek);
563     if(readDek != NULL)
564         free(readDek);
565     return ret;
566 }
567
568 int wae_tc_load_preloaded_app_deks()
569 {
570     int ret = WAE_ERROR_NONE;
571
572     const char* pkgId1 = "TEST_PKGID_1";
573     unsigned char* dek1 = NULL;
574     size_t dekLen1 = 0;
575     unsigned char* readDek1 = NULL;
576     size_t readDekLen1 = 0;
577     char path1[MAX_PATH_LEN] = {0, };
578     FILE *f1 = NULL;
579
580     const char* pkgId2 = "TEST_PKGID_2";
581     unsigned char* dek2 = NULL;
582     size_t dekLen2 = 0;
583     unsigned char* readDek2 = NULL;
584     size_t readDekLen2 = 0;
585     char path2[MAX_PATH_LEN] = {0, };
586     FILE *f2 = NULL;
587
588     _get_preloaded_app_dek_file_path(pkgId1, path1);
589     _get_preloaded_app_dek_file_path(pkgId2, path2);
590
591     // remove old test data
592     remove_app_dek(pkgId1);
593     remove_app_dek(pkgId2);
594     unlink(path1);
595     unlink(path2);
596
597     // create 2 dek for preloaded app
598     ret = create_preloaded_app_dek(pkgId1, &dek1, &dekLen1);
599     if(ret != WAE_ERROR_NONE) {
600         FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
601         goto error;
602     }
603
604     ret = create_preloaded_app_dek(pkgId2, &dek2, &dekLen2);
605     if(ret != WAE_ERROR_NONE) {
606         FPRINTF("...FAIL: create_preloaded_app_dek. ret=%d\n", ret);
607         goto error;
608     }
609
610     // load_preloaded_app_deks
611     ret = load_preloaded_app_deks(WAE_TRUE);
612     if(ret != WAE_ERROR_NONE) {
613         FPRINTF("...FAIL: load_preloaded_app_deks. ret=%d\n", ret);
614         goto error;
615     }
616
617     // get_app_dek
618     ret = get_app_dek(pkgId1, &readDek1, &readDekLen1);
619     if(ret != WAE_ERROR_NONE) {
620         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
621         goto error;
622     }
623
624     ret = get_app_dek(pkgId2, &readDek2, &readDekLen2);
625     if(ret != WAE_ERROR_NONE) {
626         FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
627         goto error;
628     }
629
630     _print_binary_to_hex("...CREATED DEK1 = ", dek1, dekLen1);
631     _print_binary_to_hex("...READ    DEK1 = ", readDek1, readDekLen1);
632     if(_compare_binary(dek1, dekLen1, readDek1, readDekLen1) != 0 ) {
633         ret = WAE_ERROR_FILE;
634         FPRINTF("...FAIL: DEK1 != READ_DEK1.\n");
635         goto error;
636     }
637
638     _print_binary_to_hex("...CREATED DEK2 = ", dek2, dekLen2);
639     _print_binary_to_hex("...READ    DEK2 = ", readDek2, readDekLen2);
640     if(_compare_binary(dek2, dekLen2, readDek2, readDekLen2) != 0 ) {
641         ret = WAE_ERROR_FILE;
642         FPRINTF("...FAIL: DEK2 != READ_DEK2.\n");
643         goto error;
644     }
645
646     // remove_app_dek
647     remove_app_dek(pkgId1);
648     remove_app_dek(pkgId2);
649
650     ret = WAE_ERROR_NONE;
651 error:
652     if(dek1 != NULL)
653         free(dek1);
654     if(readDek1 != NULL)
655         free(readDek1);
656     if(f1 != NULL)
657         fclose(f1);
658     if(dek2 != NULL)
659         free(dek2);
660     if(readDek2 != NULL)
661         free(readDek2);
662     if(f2 != NULL)
663         fclose(f2);
664
665     return ret;
666 }
667
668
669 int wae_tc_encrypt_decrypt_web_application()
670 {
671     int ret = WAE_ERROR_NONE;
672
673     const char* pkgId1 = "testpkg_for_downloaded";
674     const char* pkgId2 = "testpkg_for_preloaded";
675     const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
676     size_t plaintextLen = strlen(plaintext);
677     unsigned char* encrypted = NULL;
678     size_t encLen = 0;
679     unsigned char* decrypted = NULL;
680     size_t decLen = 0;
681     char decrypted_str[1024] = {0, };
682
683     int isPreloaded = 0; // Downloaded
684
685     // remove old test data
686     ret = wae_remove_app_dek(pkgId1);
687     ret = wae_remove_app_dek(pkgId2);
688     ret = _clear_app_deks_loaded();
689
690     // test for downloaded web application
691     ret = wae_encrypt_web_application(pkgId1, isPreloaded,
692                                       (const unsigned char*)plaintext, plaintextLen,
693                                       &encrypted, &encLen);
694     if(ret != WAE_ERROR_NONE){
695         FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
696         goto error;
697     }
698
699     _remove_app_dek_from_cache(pkgId1);
700
701     ret = wae_decrypt_web_application(pkgId1, isPreloaded, encrypted, encLen, &decrypted, &decLen);
702     if(ret != WAE_ERROR_NONE){
703         FPRINTF("...FAIL: wae_decrypt_web_application. ret=%d\n", ret);
704         goto error;
705     }
706
707     if(plaintextLen != decLen) {
708         FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", plaintextLen, decLen);
709         ret = WAE_ERROR_CRYPTO;
710         goto error;
711     }
712
713     memcpy(decrypted_str, decrypted, decLen);
714     FPRINTF("...plaintext(downloaded) = %s\n", plaintext);
715     FPRINTF("...decrypted(downloaded) = %s\n", decrypted_str);
716     if(strcmp(plaintext, decrypted_str) != 0) {
717         FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
718         ret = WAE_ERROR_CRYPTO;
719         goto error;
720     }
721
722     ret = wae_remove_app_dek(pkgId1);
723     if(ret != WAE_ERROR_NONE){
724         FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
725         goto error;
726     }
727
728
729     // test for preloaded web application
730     isPreloaded = 1;
731
732     ret = wae_encrypt_web_application(pkgId2, isPreloaded,
733                                       (const unsigned char*)plaintext, plaintextLen,
734                                       &encrypted, &encLen);
735     if(ret != WAE_ERROR_NONE){
736         FPRINTF("...FAIL: wae_encrypt_web_application. ret=%d\n", ret);
737         goto error;
738     }
739     // encrypt test twice
740     ret = wae_encrypt_web_application(pkgId2, isPreloaded,
741                                       (const unsigned char*)plaintext, plaintextLen,
742                                       &encrypted, &encLen);
743     if(ret != WAE_ERROR_NONE){
744         FPRINTF("...FAIL: wae_encrypt_web_application2. ret=%d\n", ret);
745         goto error;
746     }
747
748     ret = wae_decrypt_web_application(pkgId2, isPreloaded, encrypted, encLen, &decrypted, &decLen);
749     if(ret != WAE_ERROR_NONE){
750         FPRINTF("...FAIL: wae_decrypt_web_application. ret=%d\n", ret);
751         goto error;
752     }
753
754     _remove_app_dek_from_cache(pkgId2);
755
756     if(plaintextLen != decLen) {
757         FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", plaintextLen, decLen);
758         ret = WAE_ERROR_CRYPTO;
759         goto error;
760     }
761
762     memcpy(decrypted_str, decrypted, decLen);
763     FPRINTF("...plaintext(preloaded) = %s\n", plaintext);
764     FPRINTF("...decrypted(preloaded) = %s\n", decrypted_str);
765     if(strcmp(plaintext, decrypted_str) != 0) {
766         FPRINTF("...FAIL: plaintext(%s) != decrypted(%s)\n", plaintext, decrypted_str);
767         ret = WAE_ERROR_CRYPTO;
768         goto error;
769     }
770
771     ret = wae_remove_app_dek(pkgId2);
772     if(ret != WAE_ERROR_NONE){
773         FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
774         goto error;
775     }
776
777 error:
778     if(encrypted != NULL)
779         free(encrypted);
780     if(decrypted != NULL)
781         free(decrypted);
782
783     return ret;
784 }
785
786
787 int run_test_cases()
788 {
789     RUNTC(wae_tc_encrypt_decrypt_app_dek, "wae_tc_encrypt_decrypt_app_dek");
790     RUNTC(wae_tc_encrypt_decrypt_aes_cbc, "wae_tc_encrypt_decrypt_aes_cbc");
791     RUNTC(wae_tc_cache, "wae_tc_cache");
792
793     RUNTC(wae_tc_get_random, "wae_tc_get_random");
794     RUNTC(wae_tc_get_alias, "wae_tc_get_alias");
795     RUNTC(wae_tc_add_get_remove_dek, "wae_tc_add_get_remove_dek");
796     RUNTC(wae_tc_get_preloaded_app_dek_file_path, "wae_tc_get_preloaded_app_dek_file_path");
797     RUNTC(wae_tc_extract_pkg_id_from_file_name, "wae_tc_extract_pkg_id_from_file_name");
798     RUNTC(wae_tc_read_write_encrypted_app_dek, "wae_tc_read_write_encrypted_app_dek");
799     RUNTC(wae_tc_create_app_dek, "wae_tc_create_app_dek");
800     RUNTC(wae_tc_get_create_preloaded_app_dek, "wae_tc_get_create_preloaded_app_dek");
801     RUNTC(wae_tc_load_preloaded_app_deks, "wae_tc_load_preloaded_app_deks");
802     RUNTC(wae_tc_encrypt_decrypt_web_application, "wae_tc_encrypt_decrypt_web_application");
803
804     PRINT_TC_SUMMARY();
805     return 0;
806 }
807
808 int main(void)
809 {
810     int ret = 0;
811
812     ret = run_test_cases();
813
814     return ret;
815 }