2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Dongsun Lee (ds73.lee@samsung.com)
20 * @brief internal test cases for libwebappenc.
28 #include "web_app_enc.h"
29 #include "key_handler.h"
30 #include "crypto_service.h"
32 #include <tzplatform_config.h>
34 static size_t tc_seq = 0;
35 static size_t tc_succ = 0;
36 static size_t tc_fail = 0;
38 #define FPRINTF(format, args...) fprintf(stdout, format, ##args)
40 static int RUNTC(int (*tc_method)(), const char* tc_name)
42 int ret = WAE_ERROR_NONE;
43 FPRINTF("[%02d:%s]started...\n", tc_seq, tc_name);
45 if(ret == WAE_ERROR_NONE) {
46 FPRINTF("[%02d:%s]ended. SUCCESS\n\n", tc_seq, tc_name);
49 FPRINTF("[%02d:%s]ended. FAIL. error=%d\n\n", tc_seq, tc_name, ret);
56 static void PRINT_TC_SUMMARY()
59 FPRINTF("===============================================\n");
60 FPRINTF(" TOTAL = %d, SUCCESS = %d, FAIL = %d\n", tc_seq, tc_succ, tc_fail);
61 FPRINTF("===============================================\n");
64 void _print_binary_to_hex(const char* msg, unsigned char* bin, size_t len)
68 for(i=0; i<len; i++) {
69 FPRINTF("%02x", bin[i]);
74 int _compare_binary(const unsigned char* b1, size_t b1Len, const unsigned char* b2, size_t b2Len)
79 for(i=0; i<b1Len; i++) {
87 //=================================================================================
88 // tests for crypto_service.h
89 //=================================================================================
90 int wae_tc_encrypt_decrypt_app_dek()
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;
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-----";
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"
136 "-----END PUBLIC KEY-----";
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;
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;
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);
158 if(_compare_binary(dek, sizeof(dek), decryptedDek, decryptedDekLen) != 0) {
159 FPRINTF("...FAIL: ORIG DEK != decrypted DEK\n");
160 ret = WAE_ERROR_CRYPTO;
164 ret = WAE_ERROR_NONE;
167 if(encryptedDek != NULL)
169 if(decryptedDek != NULL)
175 int wae_tc_encrypt_decrypt_aes_cbc()
177 int ret = WAE_ERROR_NONE;
179 unsigned char dek[32] = {0, };
181 const char* plaintext= "adbdfdfdfdfdererfdfdfererfdrerfdrer";
182 size_t plaintextLen = strlen(plaintext);
183 unsigned char* encrypted = NULL;
185 unsigned char* decrypted = NULL;
187 char decrypted_str[1024] = {0, };
189 ret = _get_random(keyLen, dek);
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;
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;
205 if(plaintextLen != decLen) {
206 FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", plaintextLen, decLen);
207 ret = WAE_ERROR_CRYPTO;
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;
221 if(encrypted != NULL)
223 if(decrypted != NULL)
229 //=================================================================================
230 // tests for key_handler.h
231 //=================================================================================
234 int ret = WAE_ERROR_NONE;
236 const char* pkg1 = "pkg1";
237 const char* pkg2 = "pkg2";
238 const char* pkg3 = "pkg3";
239 const char* pkgDummy = "dummy";
241 unsigned char dek1[32] = {1, };
242 unsigned char dek2[32] = {2, };
243 unsigned char dek3[32] = {3, };
244 unsigned char* retDek = NULL;
248 _add_app_dek_to_cache(pkg1, dek1);
249 _add_app_dek_to_cache(pkg2, dek2);
250 _add_app_dek_to_cache(pkg3, dek3);
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;
259 _print_binary_to_hex("...DEK1 : ", dek1, 32);
260 _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
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;
269 _print_binary_to_hex("...DEK2 : ", dek2, 32);
270 _print_binary_to_hex("...Returen DEK1 : ", retDek, 32);
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;
279 _print_binary_to_hex("...DEK3 : ", dek3, 32);
280 _print_binary_to_hex("...Returen DEK3 : ", retDek, 32);
283 retDek = _get_app_dek_from_cache(pkgDummy);
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;
291 _remove_app_dek_from_cache(pkg3);
293 retDek = _get_app_dek_from_cache(pkg3);
295 FPRINTF("fail to remove app dek from cache\n");
296 ret = WAE_ERROR_UNKNOWN;
302 _add_app_dek_to_cache(pkg1, dek1);
305 retDek = _get_app_dek_from_cache(pkg2);
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;
313 ret = WAE_ERROR_NONE;
318 int wae_tc_get_random()
320 int ret = WAE_ERROR_NONE;
322 size_t rand_len = 32;
323 unsigned char random[32] = {0, };
325 ret = _get_random(rand_len, random);
327 _print_binary_to_hex("...RANDOM = ", random, sizeof(random));
332 int wae_tc_get_alias()
334 int ret = WAE_ERROR_NONE;
336 const char* pkgId = "TEST_PKG_ID";
337 char sys_alias[256] = {0, };
339 _get_alias(pkgId, sys_alias, sizeof(sys_alias));
341 FPRINTF("...pkgid=%s, system alias=%s\n", pkgId, sys_alias);
346 int wae_tc_add_get_remove_dek()
348 int ret = WAE_ERROR_NONE;
350 const char* pkgId = "TEST_PKG_ID";
353 unsigned char dek[32] = {0, };
354 size_t storedDekLen = 0;
355 unsigned char* storedDek = NULL;
357 ret = _get_random(dekLen, dek);
359 remove_app_dek(pkgId);
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);
367 ret = get_app_dek(pkgId, &storedDek, &storedDekLen);
368 if(ret != WAE_ERROR_NONE) {
369 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
373 if(_compare_binary(dek, dekLen, storedDek, storedDekLen) != 0 ) {
374 ret = WAE_ERROR_KEY_MANAGER;
375 FPRINTF("...FAIL: DEK != STORED_DEK.\n");
379 ret = remove_app_dek(pkgId);
380 if(ret != WAE_ERROR_NONE) {
381 FPRINTF("...FAIL: remove_app_dek. ret=%d\n", ret);
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");
392 ret = WAE_ERROR_NONE;
394 if(storedDek != NULL)
400 int wae_tc_get_preloaded_app_dek_file_path()
402 int ret = WAE_ERROR_NONE;
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");
409 ret = _get_preloaded_app_dek_file_path(pkgId, path);
410 FPRINTF("...expected path : %s\n", expectedPath);
411 FPRINTF("...returned path : %s\n", path);
413 if(ret != WAE_ERROR_NONE || strncmp(expectedPath, path, strlen(expectedPath)) != 0) {
414 ret = WAE_ERROR_UNKNOWN;
421 int wae_tc_extract_pkg_id_from_file_name()
423 int ret = WAE_ERROR_NONE;
424 const char* fileName = "WAE_APP_DEK_test_pkg.adek";
425 const char* expectedPkgId = "test_pkg";
428 ret = _extract_pkg_id_from_file_name(fileName, pkgId);
429 FPRINTF("...expected pkgId: %s\n", expectedPkgId);
430 FPRINTF("...returned pkgId: %s\n", pkgId);
432 if(ret != WAE_ERROR_NONE || strncmp(expectedPkgId, pkgId, strlen(expectedPkgId)) != 0) {
433 ret = WAE_ERROR_UNKNOWN;
441 int wae_tc_read_write_encrypted_app_dek()
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;
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);
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);
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");
476 int wae_tc_create_app_dek()
478 int ret = WAE_ERROR_NONE;
480 const char* pkgId = "TEST_PKG_ID";
481 unsigned char* dek = NULL;
484 size_t storedDekLen = 0;
485 unsigned char* storedDek = NULL;
487 remove_app_dek(pkgId);
489 ret = create_app_dek(pkgId, &dek, &dekLen);
490 if(ret != WAE_ERROR_NONE) {
491 FPRINTF("...FAIL: create_app_dek. ret=%d\n", ret);
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);
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");
510 remove_app_dek(pkgId);
512 ret = WAE_ERROR_NONE;
516 if(storedDek != NULL)
521 int wae_tc_get_create_preloaded_app_dek()
523 int ret = WAE_ERROR_NONE;
525 const char* pkgId = "TEST_PKG_ID_FOR_CREATE";
526 unsigned char *dek = NULL;
527 unsigned char *readDek = NULL;
528 size_t readDekLen = 0;
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;
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);
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);
550 _print_binary_to_hex("...CREATED DEK = ", dek, dekLen);
551 _print_binary_to_hex("...READ DEK = ", readDek, readDekLen);
553 if(_compare_binary(dek, dekLen, readDek, readDekLen) != 0 ) {
554 ret = WAE_ERROR_FILE;
555 FPRINTF("...FAIL: DEK != READ_DEK.\n");
559 ret = WAE_ERROR_NONE;
568 int wae_tc_load_preloaded_app_deks()
570 int ret = WAE_ERROR_NONE;
572 const char* pkgId1 = "TEST_PKGID_1";
573 unsigned char* dek1 = NULL;
575 unsigned char* readDek1 = NULL;
576 size_t readDekLen1 = 0;
577 char path1[MAX_PATH_LEN] = {0, };
580 const char* pkgId2 = "TEST_PKGID_2";
581 unsigned char* dek2 = NULL;
583 unsigned char* readDek2 = NULL;
584 size_t readDekLen2 = 0;
585 char path2[MAX_PATH_LEN] = {0, };
588 _get_preloaded_app_dek_file_path(pkgId1, path1);
589 _get_preloaded_app_dek_file_path(pkgId2, path2);
591 // remove old test data
592 remove_app_dek(pkgId1);
593 remove_app_dek(pkgId2);
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);
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);
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);
618 ret = get_app_dek(pkgId1, &readDek1, &readDekLen1);
619 if(ret != WAE_ERROR_NONE) {
620 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
624 ret = get_app_dek(pkgId2, &readDek2, &readDekLen2);
625 if(ret != WAE_ERROR_NONE) {
626 FPRINTF("...FAIL: get_app_dek. ret=%d\n", ret);
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");
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");
647 remove_app_dek(pkgId1);
648 remove_app_dek(pkgId2);
650 ret = WAE_ERROR_NONE;
669 int wae_tc_encrypt_decrypt_web_application()
671 int ret = WAE_ERROR_NONE;
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;
679 unsigned char* decrypted = NULL;
681 char decrypted_str[1024] = {0, };
683 int isPreloaded = 0; // Downloaded
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();
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);
699 _remove_app_dek_from_cache(pkgId1);
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);
707 if(plaintextLen != decLen) {
708 FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", plaintextLen, decLen);
709 ret = WAE_ERROR_CRYPTO;
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;
722 ret = wae_remove_app_dek(pkgId1);
723 if(ret != WAE_ERROR_NONE){
724 FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
729 // test for preloaded web application
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);
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);
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);
754 _remove_app_dek_from_cache(pkgId2);
756 if(plaintextLen != decLen) {
757 FPRINTF("...FAIL: plaintextLen(%d) != decLen(%d)\n", plaintextLen, decLen);
758 ret = WAE_ERROR_CRYPTO;
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;
771 ret = wae_remove_app_dek(pkgId2);
772 if(ret != WAE_ERROR_NONE){
773 FPRINTF("...FAIL: wae_remove_app_dek. ret=%d\n", ret);
778 if(encrypted != NULL)
780 if(decrypted != NULL)
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");
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");
812 ret = run_test_cases();